From 142aad264c428d408aeecbcc9659bc76ac11b659 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:09:38 +0100 Subject: [PATCH] err_handler added an error handler for when a pointer is NULL got rid of malloc in format reply --- project/Core/Inc/UDP_broadcast.h | 18 +++-- project/Core/Src/UDP_broadcast.c | 118 +++++++++++++++++++++---------- project/Core/Src/main.c | 29 +++++++- 3 files changed, 119 insertions(+), 46 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 8b24ff6..07abd29 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -13,17 +13,27 @@ #include #include "lwip/netif.h" #include "lwip.h" -#define LOGGER_LEVEL_INFO +#define LOGGER_LEVEL_ALL #include "log.h" + +#define sod_name "set_owner_details_name" +#define god_name "get_owner_details_name" +#define sod_surname "set_owner_details_surname" +#define god_surname "get_owner_details_surname" +#define sod_reply "set_owner_details_reply" +#define god_reply "get_owner_details_reply" +#define sod_mac "set_owner_details_mac" +#define sod "set_owner_details" +#define f_reply "format_reply" + + typedef struct { char name[20]; char surname[20]; uint8_t mac_address[6]; - char reply[200]; + char reply[100]; }owner_details_t; -uint8_t set_owner_details_name(owner_details_t*, char* ); -uint8_t set_owner_details_sirname(owner_details_t*, char* ); uint8_t set_owner_details(owner_details_t*, char* , char*); char* get_owner_details_name(owner_details_t*); diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 18b4ec9..8ae2aa1 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -10,19 +10,47 @@ static const char *TAG = "UDP_broadcast"; /** - * @fn void set_owner_details_mac(owner_details_t*) + * @fn void owner_details_error_handler(owner_details_t*, char*, char*) * @brief * * @param owner + * @param word + * @param funct */ -static void set_owner_details_mac(owner_details_t* owner){ - // Access the MAC address - - - for(int i = 0; i < 6; i++){ - owner->mac_address[i] = netif_default->hwaddr[i]; +static void owner_details_error_handler(owner_details_t* owner, char* funct,char* word){ + if(owner == NULL && word == NULL){ + LOG_WARN(TAG,"%s: owner and string given are both NULL pointers",funct); } + else if(owner == NULL){ + LOG_WARN(TAG,"%s: owner given is a NULL pointer",funct); + } + else{ + LOG_WARN(TAG,"%s: string given is a NULL pointer",funct); + } +} + +/** + * @fn uint8_t set_owner_details_mac(owner_details_t*) + * @brief + * + * @param owner + * @return + */ + +static uint8_t set_owner_details_mac(owner_details_t* owner){ + // Access the MAC address + if(owner != NULL){ + for(int i = 0; i < 6; i++){ + owner->mac_address[i] = netif_default->hwaddr[i]; + } + return 1; + } + else{ + owner_details_error_handler(owner, sod_mac,""); + return 0; + } + } @@ -35,13 +63,14 @@ static void set_owner_details_mac(owner_details_t* owner){ * @return */ -uint8_t set_owner_details_name(owner_details_t *owner, char *name){ - if(name != NULL){ +static uint8_t set_owner_details_name(owner_details_t *owner, char *name){ + if(name != NULL && owner != NULL){ LOG_DEBUG(TAG,"set: %s",name); strncpy(owner->name,name,sizeof(owner->name)); return 1; } else{ + owner_details_error_handler(owner, sod_name, name); return 0; } @@ -55,13 +84,14 @@ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ * @param surname * @return */ -uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ - if(surname != NULL){ +static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ + if(surname != NULL && owner != NULL){ LOG_DEBUG(TAG,"set: %s",surname); strncpy(owner->surname,surname,sizeof(owner->surname)); return 1; } else{ + owner_details_error_handler(owner, sod_surname, surname); return 0; } } @@ -76,45 +106,46 @@ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ */ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ - if(reply != NULL){ + if(reply != NULL && owner != NULL){ LOG_DEBUG(TAG,"set: %s",reply); strncpy(owner->reply,reply,sizeof(owner->reply)); return 1; } else{ + owner_details_error_handler(owner, sod_reply, reply); return 0; } } /** - * @fn void format_reply(owner_details_t*) + * @fn uint8_t format_reply(owner_details_t*) * @brief * * @param owner + * @return */ -static void format_reply(owner_details_t *owner){ +static uint8_t format_reply(owner_details_t *owner){ size_t reply_len = 0; char mac_addr_str[18]; - char* reply_buf = NULL; + char reply_buf[100]; if (owner != NULL) { reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name); - reply_buf = (char*)malloc(reply_len); snprintf(mac_addr_str, sizeof(mac_addr_str), "%02X:%02X:%02X:%02X:%02X:%02X", owner->mac_address[0], owner->mac_address[1], owner->mac_address[2], owner->mac_address[3], owner->mac_address[4], owner->mac_address[5]); - if (reply_buf != NULL) { - snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s", - mac_addr_str, owner->surname, owner->name); - - set_owner_details_reply(owner, reply_buf); - - free(reply_buf); // Free the temporary buffer - } + snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s", + mac_addr_str, owner->surname, owner->name); + set_owner_details_reply(owner, reply_buf); + return 1; + } + else{ + owner_details_error_handler(owner, f_reply,""); + return 0; } } @@ -128,14 +159,24 @@ static void format_reply(owner_details_t *owner){ * @return */ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ - if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname)){ - set_owner_details_mac(owner); - format_reply(owner); - return 1; + if(owner != NULL){ + if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner)){ + if(format_reply(owner)){ + return 1;; + } + else{ + return 0; + } + } + else{ + return 0; + } } else{ + owner_details_error_handler(owner, f_reply,""); return 0; } + } /** @@ -147,11 +188,11 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ */ char* get_owner_details_name(owner_details_t *owner){ - char err_reply[20] = "no name yet"; - if(owner->name == NULL){ - strncpy(owner->name,err_reply,sizeof(owner->name)); + if(owner == NULL || owner->name == NULL){ + owner_details_error_handler(owner, god_name,""); + return "|no name yet|"; } - return *owner->name; + return owner->name; } /** @@ -163,9 +204,9 @@ char* get_owner_details_name(owner_details_t *owner){ */ char* get_owner_details_surname(owner_details_t* owner){ - char err_reply[20] = "no surname yet"; - if(owner->surname == NULL){ - strncpy(owner->surname,err_reply,sizeof(owner->surname)); + if(owner == NULL || owner->surname == NULL){ + owner_details_error_handler(owner, god_surname,""); + return "|no surname yet|"; } return owner->surname; } @@ -179,10 +220,9 @@ char* get_owner_details_surname(owner_details_t* owner){ */ char* get_owner_details_reply(owner_details_t *owner){ - LOG_DEBUG(TAG,"getting reply"); - char err_reply[20] = "no reply yet"; - if(owner->reply == NULL){ - strncpy(owner->reply,err_reply,sizeof(owner->reply)); + if(owner == NULL || owner->reply == NULL){ + owner_details_error_handler(owner, god_reply,""); + return "|no reply formatted yet|"; } return owner->reply; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 959aedd..4565b6e 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,16 +111,39 @@ int main(void) MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ owner_details_t owner; + owner_details_t* owner_error = NULL; + char* test_error = NULL; LOG_DEBUG(TAG,"\nhelloworld"); - LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"owner1:%s",get_owner_details_reply(&owner)); if(!set_owner_details(&owner, "joran", "vn")){ LOG_DEBUG(TAG,"error");; } - LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"owner2:%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){ LOG_DEBUG(TAG,"error"); } - LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"owner3:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(&owner, "joran", "")){ + LOG_DEBUG(TAG,"error"); + } + LOG_DEBUG(TAG,"owner4:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(&owner, "joran", test_error)){ + LOG_DEBUG(TAG,"error setting owner details"); + } + LOG_DEBUG(TAG,"owner5:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(&owner, test_error, "Van Nieuwenhoven")){ + LOG_DEBUG(TAG,"error setting owner details"); + } + LOG_DEBUG(TAG,"owner6:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(owner_error, test_error, "Van Nieuwenhoven")){ + LOG_DEBUG(TAG,"error setting owner details"); + } + LOG_DEBUG(TAG,"owner7:%s",get_owner_details_reply(owner_error)); /* USER CODE END 2 */ /* Infinite loop */