style guide part 1

This commit is contained in:
joran2738
2023-11-13 16:25:21 +01:00
parent a3907c36d9
commit 1360b0cd11
3 changed files with 74 additions and 63 deletions

View File

@@ -46,13 +46,13 @@ typedef struct {
}owner_details_t; }owner_details_t;
// The following functions are used for owner details (those that must be available in main) // The following functions are used for owner details (those that must be available in main)
uint8_t set_owner_details(owner_details_t*, char* , char*); uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char* , const char*);
char* get_owner_details_name(owner_details_t*); char* udp_broadcast_get_owner_details_name(owner_details_t*);
char* get_owner_details_surname(owner_details_t*); char* udp_broadcast_get_owner_details_surname(owner_details_t*);
char* get_owner_details_reply(); char* udp_broadcast_get_owner_details_reply();
// The following functions are used for UDP (those that must be available in main) // The following functions are used for UDP (those that must be available in main)
err_t init_UDP_server(); err_t udp_broadcast_init();
#endif /* INC_UDP_BROADCAST_H_ */ #endif /* INC_UDP_BROADCAST_H_ */

View File

@@ -14,8 +14,20 @@
static const char *TAG = "UDP_broadcast"; // Tag used in logs 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 static char reply_str[100] = "|no reply formatted yet|"; // Global reply string for UDP broadcast
// Function
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);
static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, const char* surname);
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 owner_details_error_handler(owner_details_t*, char*, char*) * @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 * @brief owner_details_error_handler() is called when one of the owner details functions had an error
* it checks which of the parameters in the function where the error occured, is a NULL pointer * it checks which of the parameters in the function where the error occured, is a NULL pointer
* and logs a warning depending on that * and logs a warning depending on that
@@ -26,7 +38,7 @@ static char reply_str[100] = "|no reply formatted yet|"; // Global reply stri
* @param funct name of the function where the error occured * @param funct name of the function where the error occured
*/ */
static void owner_details_error_handler(owner_details_t* owner, char* funct,char* word){ static void udp_broadcast_owner_details_error_handler(const owner_details_t* owner, const char* funct, const char* word){
if (owner == NULL && word == NULL){ if (owner == NULL && word == NULL){
LOG_WARN(TAG,"%s: owner and string given are both NULL pointers",funct); LOG_WARN(TAG,"%s: owner and string given are both NULL pointers",funct);
} }
@@ -39,7 +51,7 @@ static void owner_details_error_handler(owner_details_t* owner, char* funct,char
} }
/** /**
* @fn uint8_t set_owner_details_mac(owner_details_t*) * @fn uint8_t udp_broadcast_set_owner_details_mac(owner_details_t*)
* @brief set_owner_details_mac() gets the MAC address from the default netif * @brief set_owner_details_mac() gets the MAC address from the default netif
* and sets it in the owner_details_t struct * and sets it in the owner_details_t struct
* *
@@ -49,9 +61,9 @@ static void owner_details_error_handler(owner_details_t* owner, char* funct,char
* - 0: an error occured, owner pointer is NULL * - 0: an error occured, owner pointer is NULL
*/ */
static uint8_t set_owner_details_mac(owner_details_t* owner){ static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){
if (owner == NULL){ if (owner == NULL){
owner_details_error_handler(owner, SOD_MAC,""); udp_broadcast_owner_details_error_handler(owner, SOD_MAC,"");
return 0; return 0;
} }
for (uint8_t i = 0; i < 6; i++){ for (uint8_t i = 0; i < 6; i++){
@@ -61,7 +73,7 @@ static uint8_t set_owner_details_mac(owner_details_t* owner){
} }
/** /**
* @fn uint8_t set_owner_details_name(owner_details_t*, char*) * @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 * @brief set_owner_details_name() sets the owner's name in the owner_details_t struct
* *
* @param owner owner_details_t structure, it contains information about the owner * @param owner owner_details_t structure, it contains information about the owner
@@ -71,9 +83,9 @@ static uint8_t set_owner_details_mac(owner_details_t* owner){
* - 0: an error occured, name pointer is NULL or owner pointer is NULL * - 0: an error occured, name pointer is NULL or owner pointer is NULL
*/ */
static uint8_t set_owner_details_name(owner_details_t *owner, char *name){ static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, const char* name){
if (name == NULL || owner == NULL){ if (name == NULL || owner == NULL){
owner_details_error_handler(owner, SOD_NAME, name); udp_broadcast_owner_details_error_handler(owner, SOD_NAME, name);
return 0; return 0;
} }
LOG_DEBUG(TAG,"set: %s",name); LOG_DEBUG(TAG,"set: %s",name);
@@ -82,7 +94,7 @@ static uint8_t set_owner_details_name(owner_details_t *owner, char *name){
} }
/** /**
* @fn uint8_t set_owner_details_surname(owner_details_t*, char*) * @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 * @brief set_owner_details_surname() sets the owner's surname in the owner_details_t struct
* *
* @param owner owner_details_t structure, it contains information about the owner * @param owner owner_details_t structure, it contains information about the owner
@@ -91,9 +103,9 @@ static uint8_t set_owner_details_name(owner_details_t *owner, char *name){
* - 1: no error occured, surname was set * - 1: no error occured, surname was set
* - 0: an error occured, surname pointer is NULL or owner pointer is NULL * - 0: an error occured, surname pointer is NULL or owner pointer is NULL
*/ */
static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, const char* surname){
if (surname == NULL || owner == NULL){ if (surname == NULL || owner == NULL){
owner_details_error_handler(owner, SOD_SURNAME, surname); udp_broadcast_owner_details_error_handler(owner, SOD_SURNAME, surname);
return 0; return 0;
} }
LOG_DEBUG(TAG,"set: %s",surname); LOG_DEBUG(TAG,"set: %s",surname);
@@ -102,7 +114,7 @@ static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){
} }
/** /**
* @fn uint8_t set_owner_details_reply(char*) * @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 * @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct
* *
* @param reply string used to reply to the UDP broadcast * @param reply string used to reply to the UDP broadcast
@@ -111,7 +123,7 @@ static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){
* - 0: an error occured, reply pointer is null or owner pointer is NULL * - 0: an error occured, reply pointer is null or owner pointer is NULL
*/ */
static uint8_t set_owner_details_reply(char *reply){ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){
if (reply == NULL){ if (reply == NULL){
LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_REPLY); LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_REPLY);
return 0; return 0;
@@ -122,7 +134,7 @@ static uint8_t set_owner_details_reply(char *reply){
} }
/** /**
* @fn uint8_t format_reply(owner_details_t*) * @fn uint8_t udp_broadcast_format_reply(const owner_details_t*)
* @brief format_reply() formats all the owner's details into a string * @brief format_reply() formats all the owner's details into a string
* *
* @param owner owner_details_t structure, it contains information about the owner * @param owner owner_details_t structure, it contains information about the owner
@@ -131,12 +143,12 @@ static uint8_t set_owner_details_reply(char *reply){
* - 0: an error occured, owner pointer is NULL * - 0: an error occured, owner pointer is NULL
*/ */
static uint8_t format_reply(owner_details_t *owner){ static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){
size_t reply_len = 0; size_t reply_len = 0;
char mac_addr_str[18]; char mac_addr_str[18];
char reply_buf[100]; char reply_buf[100];
if (owner == NULL) { if (owner == NULL) {
owner_details_error_handler(owner, F_REPLY,""); udp_broadcast_owner_details_error_handler(owner, F_REPLY,"");
return 0; return 0;
} }
reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name); reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name);
@@ -148,13 +160,13 @@ static uint8_t format_reply(owner_details_t *owner){
snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s", snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s",
mac_addr_str, owner->surname, owner->name); mac_addr_str, owner->surname, owner->name);
set_owner_details_reply(reply_buf); udp_broadcast_set_owner_details_reply(reply_buf);
return 1; return 1;
} }
/** /**
* @fn uint8_t set_owner_details(owner_details_t*, char*, char*) * @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 * @brief set_owner_details() is the interface that can be used in other files
* to set the owner's details * to set the owner's details
* *
@@ -165,12 +177,12 @@ static uint8_t format_reply(owner_details_t *owner){
* - 1: no error occured, details were set * - 1: no error occured, details were set
* - 0: an error occured, all or some details weren't set or owner pointer is NULL * - 0: an error occured, all or some details weren't set or owner pointer is NULL
*/ */
uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name, const char* surname){
if (owner == NULL){ if (owner == NULL){
owner_details_error_handler(owner, SOD,""); udp_broadcast_owner_details_error_handler(owner, SOD,"");
return 0; return 0;
} }
else if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner) && 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 1;
} }
return 0; return 0;
@@ -178,7 +190,7 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){
} }
/** /**
* @fn char get_owner_details_name*(owner_details_t) * @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 * @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 * @param owner owner_details_t structure, it contains information about the owner
@@ -186,16 +198,16 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){
* this name is set by @see set_owner_details_name() * this name is set by @see set_owner_details_name()
*/ */
char* get_owner_details_name(owner_details_t *owner){ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){
if (owner == NULL || owner->name == NULL){ if (owner == NULL || owner->name == NULL){
owner_details_error_handler(owner, GOD_NAME,""); udp_broadcast_owner_details_error_handler(owner, GOD_NAME,"");
return "|no name yet|"; return "|no name yet|";
} }
return owner->name; return owner->name;
} }
/** /**
* @fn char get_owner_details_surname*(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 * @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 * @param owner owner_details_t structure, it contains information about the owner
@@ -203,23 +215,23 @@ char* get_owner_details_name(owner_details_t *owner){
* this name is set by @see set_owner_details_surname() * this name is set by @see set_owner_details_surname()
*/ */
char* get_owner_details_surname(owner_details_t* owner){ char* udp_broadcast_get_owner_details_surname(owner_details_t* owner){
if (owner == NULL || owner->surname == NULL){ if (owner == NULL || owner->surname == NULL){
owner_details_error_handler(owner, GOD_SURNAME,""); udp_broadcast_owner_details_error_handler(owner, GOD_SURNAME,"");
return "|no surname yet|"; return "|no surname yet|";
} }
return owner->surname; return owner->surname;
} }
/** /**
* @fn char get_owner_details_reply*() * @fn char udp_broadcast_get_owner_details_reply*()
* @brief get_owner_details_reply() can be used to get the current UDP reply * @brief get_owner_details_reply() can be used to get the current UDP reply
* *
* @return reply for UDP broadcast * @return reply for UDP broadcast
* this reply is formatted by @see format_reply() * this reply is formatted by @see format_reply()
*/ */
char* get_owner_details_reply(){ char* udp_broadcast_get_owner_details_reply(){
return reply_str; return reply_str;
} }
@@ -238,11 +250,11 @@ char* get_owner_details_reply(){
*/ */
static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct pbuf* p, const ip_addr_t* addr, u16_t port){ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct pbuf* p, const ip_addr_t* addr, u16_t port){
int len; struct pbuf *p_data;
size_t len;
char *pc; char *pc;
char data[MAX_DATA_SIZE]; char data[MAX_DATA_SIZE];
char source_ip_str[16]; char source_ip_str[16];
struct pbuf *p_data;
memset(data, 0, sizeof(data)); memset(data, 0, sizeof(data));
@@ -256,7 +268,7 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p
LOG_WARN(TAG,"udp_receive_callback: unable to allocate data buffer for reply"); 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(uint8_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
data[i] = pc[i]; data[i] = pc[i];
} }
@@ -265,8 +277,9 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p
p_data->payload = reply_str; p_data->payload = reply_str;
p_data->len = strlen(reply_str); p_data->len = strlen(reply_str);
p_data->tot_len = strlen(reply_str); p_data->tot_len = strlen(reply_str);
udp_sendto(connection, p_data, addr, 64000); /*was using the sending port of the pc, udp_sendto(connection, p_data, addr, 64000); /* Was using the sending port of the pc,
this is not the port that Qt is listening to*/ * this is not the port that Qt is listening to
*/
LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str); LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str);
} }
@@ -284,7 +297,7 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p
} }
/** /**
* @fn err_t init_UDP_server() * @fn err_t udp_broadcast_init()
* @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on * @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on
* port 6400 * port 6400
* *
@@ -293,9 +306,10 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p
* - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB. * - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB.
*/ */
err_t init_UDP_server(){ err_t udp_broadcast_init(){
struct udp_pcb* connection; struct udp_pcb* connection;
err_t err; err_t err;
LOG_INFO(TAG,"initialising UDP server"); LOG_INFO(TAG,"initialising UDP server");
connection = udp_new(); connection = udp_new();
if (connection != NULL){ if (connection != NULL){
@@ -314,6 +328,3 @@ err_t init_UDP_server(){
} }
return err; return err;
} }

View File

@@ -111,12 +111,12 @@ int main(void)
MX_QUADSPI_Init(); MX_QUADSPI_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
owner_details_t owner; owner_details_t owner;
init_UDP_server(); udp_broadcast_init();
if(!set_owner_details(&owner, "Joran", "Van Nieuwenhoven")){ if(!udp_broadcast_set_owner_details(&owner, "Joran", "Van Nieuwenhoven")){
LOG_DEBUG(TAG,"error");; LOG_DEBUG(TAG,"error");;
} }
LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply(&owner));
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */