From 566bdb00edab6af42686791bd050f05e58c5dc3f Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 6 Nov 2023 23:57:04 +0100 Subject: [PATCH] change of struct struct no longer contains allocated arrays problem with the reply string --- project/Core/Inc/UDP_broadcast.h | 14 ++++--- project/Core/Src/UDP_broadcast.c | 65 +++++++++++++------------------- project/Core/Src/main.c | 16 +++++--- 3 files changed, 44 insertions(+), 51 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 5edc19c..7584abc 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -13,20 +13,22 @@ #include #include "lwip/netif.h" #include "lwip.h" +#define LOGGER_LEVEL_ALL +#include "log.h" typedef struct { - char* name; - char* surname; + char name[20]; + char surname[20]; uint8_t mac_address[6]; - char* reply; + char reply[200]; }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); -char* get_owner_details_surname(owner_details_t); -char* get_owner_details_reply(owner_details_t); +char* get_owner_details_name(owner_details_t*); +char* get_owner_details_surname(owner_details_t*); +char* get_owner_details_reply(owner_details_t*); #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index f207df4..413801e 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -36,13 +36,8 @@ static void set_owner_details_mac(owner_details_t* owner){ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ if(name != NULL){ - if(owner->name == NULL){ - owner->name = (char*)malloc(strlen(name) + 1); - } - else{ - owner->name = (char*)realloc(owner->name,strlen(name) + 1); - } - strncpy(owner->name,name,strlen(owner->name)); + LOG_DEBUG("set_owner_details_name","set: %s",name); + strncpy(owner->name,name,sizeof(owner->name)); return 1; } else{ @@ -61,13 +56,8 @@ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ */ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ if(surname != NULL){ - if(owner->surname == NULL){ - owner->surname = (char*)malloc(strlen(surname) + 1); - } - else{ - owner->surname = (char*)realloc(owner->surname,strlen(surname) + 1); - } - strncpy(owner->surname,surname,strlen(owner->surname)); + LOG_DEBUG("set_owner_details_surname","set: %s",surname); + strncpy(owner->surname,surname,sizeof(owner->surname)); return 1; } else{ @@ -86,13 +76,8 @@ 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(owner->reply == NULL){ - owner->reply = (char*)malloc(strlen(reply) + 1); - } - else{ - owner->reply = (char*)realloc(owner->reply,strlen(reply) + 1); - } - strncpy(owner->reply,reply,strlen(owner->reply)); + LOG_DEBUG("set_owner_details_reply","set: %s",reply); + strncpy(owner->reply,reply,sizeof(owner->reply)); return 1; } else{ @@ -145,6 +130,10 @@ 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; + } + else{ + return 0; } } @@ -156,13 +145,12 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ * @return */ -char* get_owner_details_name(owner_details_t owner){ - char *err_reply = "no name yet"; - if(owner.name == NULL){ - owner.name = (char*)malloc(strlen(err_reply)); - strncpy(owner.name,err_reply,strlen(owner.name)); +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)); } - return owner.name; + return *owner->name; } /** @@ -173,13 +161,12 @@ char* get_owner_details_name(owner_details_t owner){ * @return */ -char* get_owner_details_surname(owner_details_t owner){ - char *err_reply = "no surname yet"; - if(owner.surname == NULL){ - owner.surname = (char*)malloc(strlen(err_reply)); - strncpy(owner.surname,err_reply,strlen(owner.surname)); +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)); } - return owner.surname; + return owner->surname; } /** @@ -190,13 +177,13 @@ char* get_owner_details_surname(owner_details_t owner){ * @return */ -char* get_owner_details_reply(owner_details_t owner){ - char *err_reply = "no reply yet"; - if(owner.reply == NULL){ - owner.reply = (char*)malloc(strlen(err_reply)); - strncpy(owner.reply,err_reply,strlen(owner.reply)); +char* get_owner_details_reply(owner_details_t *owner){ + LOG_DEBUG("get_owner_details_reply","getting reply"); + char err_reply[20] = "no reply yet"; + if(owner->reply == NULL){ + strncpy(owner->reply,err_reply,sizeof(owner->reply)); } - return owner.reply; + return owner->reply; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 97fab40..f98c628 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,12 +111,16 @@ int main(void) MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ owner_details_t owner; - LOG_DEBUG("main1","helloworld"); - LOG_DEBUG("main2","%s",get_owner_details_reply(owner)); - set_owner_details(&owner, "joran", "vn"); - LOG_DEBUG("main3","%s",get_owner_details_reply(owner)); - set_owner_details(&owner, "joran", "Van Nieuwenhoven"); - LOG_DEBUG("main4","%s",get_owner_details_reply(owner)); + LOG_DEBUG("main1","\nhelloworld"); + LOG_DEBUG("main2","%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "vn")){ + LOG_DEBUG("main3","error");; + } + LOG_DEBUG("main4","%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){ + LOG_DEBUG("main5","error"); + } + LOG_DEBUG("main6","%s",get_owner_details_reply(&owner)); /* USER CODE END 2 */ /* Infinite loop */