style guide part 2

This commit is contained in:
joran2738
2023-11-13 18:12:43 +01:00
parent 1360b0cd11
commit 5609c2ccb9
2 changed files with 77 additions and 26 deletions

View File

@@ -30,8 +30,8 @@
#define F_REPLY "format_reply"
// Defines used by UDP callback
#define MAX_DATA_SIZE 30 // Define the maximum expected data size
#define UDP_QUESTION1 "Where are you?v1.0"
#define MAX_DATA_SIZE 30 // Define the maximum expected data size
#define UDP_QUESTION1 "Where are you?v1.0" // Expected request from UDP client
/**
* @struct owner_details_t
@@ -46,13 +46,62 @@ typedef struct {
}owner_details_t;
// The following functions are used for owner details (those that must be available in main)
/**
* @fn uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char*, const char*)
* @brief set_owner_details() is the interface that can be used in other files
* to set the owner's details
*
* @param owner owner_details_t structure, it contains information about the owner
* @param name string containing the new owner's name
* @param surname string containing the new owner's surname
* @return setting owner details error
* - 1: no error occured, details were set
* - 0: an error occured, all or some details weren't set or owner pointer is NULL
*/
uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char* , const char*);
/**
* @fn char udp_broadcast_get_owner_details_name*(owner_details_t)
* @brief get_owner_details_name() can be used to get the current owner's name
*
* @param owner owner_details_t structure, it contains information about the owner
* @return name of owner
* this name is set by @see udp_broadcast_set_owner_details_name()
*/
char* udp_broadcast_get_owner_details_name(owner_details_t*);
/**
* @fn char udp_broadcast_get_owner_details_surname*(const owner_details_t)
* @brief get_owner_details_surname() can be used to get the current owner's surname
*
* @param owner owner_details_t structure, it contains information about the owner
* @return surname of owner
* this name is set by @see udp_broadcast_set_owner_details_surname()
*/
char* udp_broadcast_get_owner_details_surname(owner_details_t*);
/**
* @fn char udp_broadcast_get_owner_details_reply*()
* @brief get_owner_details_reply() can be used to get the current UDP reply
*
* @return reply for UDP broadcast
* this reply is formatted by @see format_reply()
*/
char* udp_broadcast_get_owner_details_reply();
// The following functions are used for UDP (those that must be available in main)
/**
* @fn err_t udp_broadcast_init()
* @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on
* port 6400
*
* @return lwIP error code.
* - ERR_OK. Successful. No error occurred.
* - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB.
*/
err_t udp_broadcast_init();
#endif /* INC_UDP_BROADCAST_H_ */

View File

@@ -14,7 +14,7 @@
static const char *TAG = "UDP_broadcast"; // Tag used in logs
static char reply_str[100] = "|no reply formatted yet|"; // Global reply string for UDP broadcast
// Function
// Functions
static void udp_broadcast_owner_details_error_handler(const owner_details_t* owner, const char* funct, const char* word);
static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner);
static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, const char* name);
@@ -23,9 +23,6 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply);
static uint8_t udp_broadcast_format_reply(const owner_details_t* owner);
static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct pbuf* p, const ip_addr_t* addr, u16_t port);
/**
* @fn void udp_broadcast_owner_details_error_handler(owner_details_t*, const char*, const char*)
* @brief owner_details_error_handler() is called when one of the owner details functions had an error
@@ -41,11 +38,9 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
static void udp_broadcast_owner_details_error_handler(const owner_details_t* owner, const char* funct, const char* word){
if (owner == NULL && word == NULL){
LOG_WARN(TAG,"%s: owner and string given are both NULL pointers",funct);
}
else if (owner == NULL){
}else if (owner == NULL){
LOG_WARN(TAG,"%s: owner given is a NULL pointer",funct);
}
else{
}else{
LOG_WARN(TAG,"%s: string given is a NULL pointer",funct);
}
}
@@ -75,6 +70,8 @@ static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){
/**
* @fn uint8_t udp_broadcast_set_owner_details_name(owner_details_t*, const char*)
* @brief set_owner_details_name() sets the owner's name in the owner_details_t struct
* if one of the pointers given is NULL it calls the error handler
* strncpy is used to copy the function paremeter safely to the owner_details_t's name
*
* @param owner owner_details_t structure, it contains information about the owner
* @param name string containing the owner's name
@@ -96,6 +93,8 @@ static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, cons
/**
* @fn uint8_t udp_broadcast_set_owner_details_surname(owner_details_t*, const char*)
* @brief set_owner_details_surname() sets the owner's surname in the owner_details_t struct
* if one of the pointers given is NULL it calls the error handler
* strncpy is used to copy the function paremeter safely to the owner_details_t's surname
*
* @param owner owner_details_t structure, it contains information about the owner
* @param surname string containing the owner's surname
@@ -116,6 +115,9 @@ static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, c
/**
* @fn uint8_t udp_broadcast_set_owner_details_reply(const char*)
* @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct
* if the pointer given is NULL it calls the error handler
* strncpy is used to copy the function paremeter safely to a global variable
* the reason this one is global is so that the udp_callback function can access it easily
*
* @param reply string used to reply to the UDP broadcast
* @return setting owner reply error
@@ -136,6 +138,9 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){
/**
* @fn uint8_t udp_broadcast_format_reply(const owner_details_t*)
* @brief format_reply() formats all the owner's details into a string
* if the pointer given is NULL it calls the error handler
* it formats a string using the owner's details using snprintf
* it sets this reply with @see udp_broadcast_set_owner_details_reply()
*
* @param owner owner_details_t structure, it contains information about the owner
* @return formatting reply error
@@ -169,6 +174,8 @@ static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){
* @fn uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char*, const char*)
* @brief set_owner_details() is the interface that can be used in other files
* to set the owner's details
* if the pointer given is NULL it calls the error handler,
* the other pointers get checked by the functions that are called in this function
*
* @param owner owner_details_t structure, it contains information about the owner
* @param name string containing the new owner's name
@@ -181,8 +188,7 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name
if (owner == NULL){
udp_broadcast_owner_details_error_handler(owner, SOD,"");
return 0;
}
else if (udp_broadcast_set_owner_details_name(owner, name) && udp_broadcast_set_owner_details_surname(owner, surname) && udp_broadcast_set_owner_details_mac(owner) && udp_broadcast_format_reply(owner)){
}else if (udp_broadcast_set_owner_details_name(owner, name) && udp_broadcast_set_owner_details_surname(owner, surname) && udp_broadcast_set_owner_details_mac(owner) && udp_broadcast_format_reply(owner)){
return 1;
}
return 0;
@@ -195,7 +201,7 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name
*
* @param owner owner_details_t structure, it contains information about the owner
* @return name of owner
* this name is set by @see set_owner_details_name()
* this name is set by @see udp_broadcast_set_owner_details_name()
*/
char* udp_broadcast_get_owner_details_name(owner_details_t* owner){
@@ -212,7 +218,7 @@ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){
*
* @param owner owner_details_t structure, it contains information about the owner
* @return surname of owner
* this name is set by @see set_owner_details_surname()
* this name is set by @see udp_broadcast_set_owner_details_surname()
*/
char* udp_broadcast_get_owner_details_surname(owner_details_t* owner){
@@ -239,8 +245,8 @@ char* udp_broadcast_get_owner_details_reply(){
/**
* @fn void udp_receive_callback(void*, struct udp_pcb*, struct pbuf*, const ip_addr_t*, u16_t)
* @brief udp_receive_callback() callback function for when a UDP packet has been received.
* it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string
* @see reply_str back to the client
* it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string,
* @see reply_str, back to the client
*
* @param arg a pointer to some user-defined data or context
* @param connection UDP PCB to be bound with a local address ipaddr and port.
@@ -266,8 +272,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM);
if (p_data == NULL){
LOG_WARN(TAG,"udp_receive_callback: unable to allocate data buffer for reply");
}
else if (len <= MAX_DATA_SIZE){
}else if (len <= MAX_DATA_SIZE){
for (size_t i = 0; i < len; i++) {
data[i] = pc[i];
}
@@ -283,13 +288,11 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str);
}
}
else{
}else{
LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE);
}
}
else{
}else{
LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer");
}
pbuf_free(p);
@@ -300,6 +303,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
* @fn err_t udp_broadcast_init()
* @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on
* port 6400
* it makes a udp_pcb, binds it to port 64000 and initializes the callback function for when data is received
*
* @return lwIP error code.
* - ERR_OK. Successful. No error occurred.
@@ -317,13 +321,11 @@ err_t udp_broadcast_init(){
if (err == ERR_OK){
udp_recv(connection, udp_receive_callback,NULL);
LOG_INFO(TAG,"initialising UDP server succesfull, callback running");
}
else{
}else{
udp_remove(connection);
LOG_WARN(TAG,"initialising UDP server failed, err not ok");
}
}
else{
}else{
LOG_WARN(TAG,"initialising UDP server failed, connection is null");
}
return err;