From 6739c7c6bf07be0a4d277dd44c18bb8339d1889a Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 21:14:40 +0100 Subject: [PATCH] there can only be one there's only one owner struct needed, thus adding a static one in the c file is better than having the main file to initialize one and pass it on again and again,. some code wasn't needed anymore and some had to change --- project/Core/Inc/UDP_broadcast.h | 32 +++++--- project/Core/Src/UDP_broadcast.c | 132 +++++++++++++------------------ project/Core/Src/main.c | 10 +-- 3 files changed, 79 insertions(+), 95 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 929004a..100c5b2 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -15,7 +15,7 @@ #include #include "lwip/netif.h" #include "lwip.h" -#define LOGGER_LEVEL_ALL +#define LOGGER_LEVEL_INFO #include "log.h" #include "udp.h" @@ -44,6 +44,7 @@ typedef struct { char name[20]; char surname[20]; uint8_t mac_address[6]; + char reply[100]; }owner_details_t; // The following functions are used for owner details (those that must be available in main) @@ -53,34 +54,31 @@ typedef struct { * @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*); +uint8_t udp_broadcast_set_owner_details(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*); +char* udp_broadcast_get_owner_details_name(); /** * @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*); +char* udp_broadcast_get_owner_details_surname(); /** * @fn char udp_broadcast_get_owner_details_reply*() @@ -91,12 +89,9 @@ char* udp_broadcast_get_owner_details_surname(owner_details_t*); */ 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 + * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() * * @return lwIP error code. * - ERR_OK. Successful. No error occurred. @@ -105,4 +100,19 @@ char* udp_broadcast_get_owner_details_reply(); err_t udp_broadcast_init(); +// The following functions are used for UDP (those that must be available in main) + +/** + * @fn err_t udp_broadcast_connection_init() + * @brief udp_broadcast_connection_init() initializes the UDP connection so that it listens for all traffic on + * port 6400 + * it is called by @see udp_broadcast_init() aswell but can be used separately if it failed before + * + * @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_connection_init(); + #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index a0a056c..6469c7c 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -13,39 +13,16 @@ // Global variables 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 owner_details_t udp_owner; // 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); -static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, const char* surname); +static uint8_t udp_broadcast_set_owner_details_mac(); +static uint8_t udp_broadcast_set_owner_details_name(const char* name); +static uint8_t udp_broadcast_set_owner_details_surname(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 uint8_t udp_broadcast_format_reply(); 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 - * it checks which of the parameters in the function where the error occured, is a NULL pointer - * and logs a warning depending on that - * - * - * @param[in] owner owner_details_t structure, it contains information about the owner - * @param[in] word string parameter that was used in the function that triggered this handler - * @param[in] funct name of the function where the error occured - */ - -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){ - 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 udp_broadcast_set_owner_details_mac(owner_details_t*) * @brief set_owner_details_mac() gets the MAC address from the default netif @@ -57,13 +34,9 @@ static void udp_broadcast_owner_details_error_handler(const owner_details_t* own * - 0: an error occured, owner pointer is NULL */ -static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){ - if (owner == NULL){ - udp_broadcast_owner_details_error_handler(owner, SOD_MAC,""); - return 0; - } +static uint8_t udp_broadcast_set_owner_details_mac(){ for (uint8_t i = 0; i < 6; i++){ - owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address + udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address } return 1; } @@ -81,13 +54,13 @@ static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){ * - 0: an error occured, name pointer is NULL or owner pointer is NULL */ -static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, const char* name){ - if (name == NULL || owner == NULL){ - udp_broadcast_owner_details_error_handler(owner, SOD_NAME, name); +static uint8_t udp_broadcast_set_owner_details_name(const char* name){ + if (name == NULL){ + LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_NAME); return 0; } LOG_DEBUG(TAG,"set: %s",name); - strncpy(owner->name,name,sizeof(owner->name)); + strncpy(udp_owner.name,name,sizeof(udp_owner.name)); return 1; } @@ -103,13 +76,13 @@ static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, cons * - 1: no error occured, surname was set * - 0: an error occured, surname pointer is NULL or owner pointer is NULL */ -static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, const char* surname){ - if (surname == NULL || owner == NULL){ - udp_broadcast_owner_details_error_handler(owner, SOD_SURNAME, surname); +static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){ + if (surname == NULL){ + LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_SURNAME); return 0; } LOG_DEBUG(TAG,"set: %s",surname); - strncpy(owner->surname,surname,sizeof(owner->surname)); + strncpy(udp_owner.surname,surname,sizeof(udp_owner.surname)); return 1; } @@ -117,8 +90,7 @@ 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 + * strncpy is used to copy the function paremeter safely to the owner_details_t's reply * * @param[in] reply string used to reply to the UDP broadcast * @return setting owner reply error @@ -132,7 +104,7 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ return 0; } LOG_DEBUG(TAG,"set: %s",reply); - strncpy(reply_str,reply,sizeof(reply_str)); + strncpy(udp_owner.reply,reply,sizeof(udp_owner.reply)); return 1; } @@ -149,22 +121,19 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ * - 0: an error occured, owner pointer is NULL */ -static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){ +static uint8_t udp_broadcast_format_reply(){ size_t reply_len = 0; char mac_addr_str[18]; char reply_buf[100]; - if (owner == NULL) { - udp_broadcast_owner_details_error_handler(owner, F_REPLY,""); - return 0; - } - reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name); + + reply_len = 20 + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); 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]); + udp_owner.mac_address[0], udp_owner.mac_address[1], udp_owner.mac_address[2], + udp_owner.mac_address[3], udp_owner.mac_address[4], udp_owner.mac_address[5]); snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s", - mac_addr_str, owner->surname, owner->name); + mac_addr_str, udp_owner.surname, udp_owner.name); udp_broadcast_set_owner_details_reply(reply_buf); return 1; @@ -185,12 +154,9 @@ static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){ * - 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* owner, const char* name, const char* surname){ - 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)){ +uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname){ + if (udp_broadcast_set_owner_details_name(name) && udp_broadcast_set_owner_details_surname(surname) + && udp_broadcast_set_owner_details_mac() && udp_broadcast_format_reply()){ return 1; } return 0; @@ -206,12 +172,8 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name * this name is set by @see udp_broadcast_set_owner_details_name() */ -char* udp_broadcast_get_owner_details_name(owner_details_t* owner){ - if (owner == NULL || owner->name == NULL){ - udp_broadcast_owner_details_error_handler(owner, GOD_NAME,""); - return "|no name yet|"; - } - return owner->name; +char* udp_broadcast_get_owner_details_name(){ + return udp_owner.name; } /** @@ -223,12 +185,8 @@ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){ * this name is set by @see udp_broadcast_set_owner_details_surname() */ -char* udp_broadcast_get_owner_details_surname(owner_details_t* owner){ - if (owner == NULL || owner->surname == NULL){ - udp_broadcast_owner_details_error_handler(owner, GOD_SURNAME,""); - return "|no surname yet|"; - } - return owner->surname; +char* udp_broadcast_get_owner_details_surname(){ + return udp_owner.surname; } /** @@ -240,7 +198,7 @@ char* udp_broadcast_get_owner_details_surname(owner_details_t* owner){ */ char* udp_broadcast_get_owner_details_reply(){ - return reply_str; + return udp_owner.reply; } @@ -271,7 +229,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p if (p != NULL) { pc = (char*)p->payload; len = p->tot_len; - p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM); + p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(udp_owner.reply), 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){ @@ -281,13 +239,13 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p LOG_INFO(TAG,"udp_receive_callback: received data from %s at port: %d: %s",source_ip_str,port,data); if (strcmp(data,UDP_QUESTION1) == 0){ - p_data->payload = reply_str; - p_data->len = strlen(reply_str); - p_data->tot_len = strlen(reply_str); + p_data->payload = udp_owner.reply; + p_data->len = strlen(udp_owner.reply); + p_data->tot_len = strlen(udp_owner.reply); 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 */ - 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,udp_owner.reply); } }else{ @@ -312,7 +270,7 @@ 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_t udp_broadcast_init(){ +err_t udp_broadcast_connection_init(){ struct udp_pcb* connection; err_t err; @@ -332,3 +290,19 @@ err_t udp_broadcast_init(){ } return err; } + +/** + * @fn err_t udp_broadcast_init() + * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() + * + * @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(){ + err_t err; + + udp_broadcast_set_owner_details("name", "default"); + err = udp_broadcast_connection_init(); + return err; +} diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index ea729c6..0613e39 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -128,13 +128,13 @@ int main(void) llfs_init(); // Initialize the UDP broadcast service - owner_details_t owner; - udp_broadcast_init(); - - if(!udp_broadcast_set_owner_details(&owner, "Joran", "Van Nieuwenhoven")){ + if (udp_broadcast_init() != ERR_OK && udp_broadcast_connection_init() != ERR_OK){ + LOG_WARN(TAG,"error initializing udp connection"); + } + if (!udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ LOG_WARN(TAG,"error setting owner's details"); } - LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply()); /* USER CODE END 2 */ /* Infinite loop */