diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 30cb820..f5631ac 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -30,7 +30,7 @@ #define F_REPLY "format_reply" // Defines used by UDP callback -#define MAX_DATA_SIZE 50 // Define the maximum expected data size +#define MAX_DATA_SIZE 30 // Define the maximum expected data size #define UDP_QUESTION1 "Where are you?v1.0" /** @@ -43,7 +43,6 @@ 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) @@ -51,7 +50,7 @@ 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_reply(); // The following functions are used for UDP (those that must be available in main) err_t init_UDP_server(); diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 9009fbe..9014598 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -4,11 +4,13 @@ * Created on: Nov 6, 2023 * Author: joran */ - //| +//| // Includes #include "UDP_broadcast.h" +// 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 @@ -48,19 +50,14 @@ static void owner_details_error_handler(owner_details_t* owner, char* funct,char */ static uint8_t set_owner_details_mac(owner_details_t* owner){ - - if(owner != NULL){ - for(int i = 0; i < 6; i++){ - owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address - } - return 1; - } - else{ + if(owner == NULL){ owner_details_error_handler(owner, SOD_MAC,""); return 0; } - - + for(uint8_t i = 0; i < 6; i++){ + owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address + } + return 1; } /** @@ -75,16 +72,13 @@ static uint8_t set_owner_details_mac(owner_details_t* owner){ */ 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{ + if(name == NULL || owner == NULL){ owner_details_error_handler(owner, SOD_NAME, name); return 0; } - + LOG_DEBUG(TAG,"set: %s",name); + strncpy(owner->name,name,sizeof(owner->name)); + return 1; } /** @@ -98,39 +92,33 @@ static uint8_t set_owner_details_name(owner_details_t *owner, char *name){ * - 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){ - if(surname != NULL && owner != NULL){ - LOG_DEBUG(TAG,"set: %s",surname); - strncpy(owner->surname,surname,sizeof(owner->surname)); - return 1; - } - else{ + if(surname == NULL || owner == NULL){ owner_details_error_handler(owner, SOD_SURNAME, surname); return 0; } + LOG_DEBUG(TAG,"set: %s",surname); + strncpy(owner->surname,surname,sizeof(owner->surname)); + return 1; } /** - * @fn uint8_t set_owner_details_reply(owner_details_t, char*) + * @fn uint8_t set_owner_details_reply(char*) * @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct * - * @param owner owner_details_t structure, it contains information about the owner * @param reply string used to reply to the UDP broadcast * @return setting owner reply error * - 1: no error occured, reply was set * - 0: an error occured, reply pointer is null or owner pointer is NULL */ -static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ - if(reply != NULL && owner != NULL){ - LOG_DEBUG(TAG,"set: %s",reply); - strncpy(owner->reply,reply,sizeof(owner->reply)); - strncpy(reply_str,reply,sizeof(reply_str)); - return 1; - } - else{ - owner_details_error_handler(owner, SOD_REPLY, reply); +static uint8_t set_owner_details_reply(char *reply){ + if(reply == NULL){ + LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_REPLY); return 0; } + LOG_DEBUG(TAG,"set: %s",reply); + strncpy(reply_str,reply,sizeof(reply_str)); + return 1; } /** @@ -147,24 +135,22 @@ static uint8_t format_reply(owner_details_t *owner){ size_t reply_len = 0; char mac_addr_str[18]; char reply_buf[100]; - if (owner != NULL) { - reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(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]); - - 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{ + if (owner == NULL) { owner_details_error_handler(owner, F_REPLY,""); return 0; } + reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(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]); + + 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(reply_buf); + return 1; + } /** @@ -180,23 +166,14 @@ static uint8_t format_reply(owner_details_t *owner){ * - 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){ - 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{ + if(owner == NULL){ owner_details_error_handler(owner, SOD,""); return 0; } + else if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner) && format_reply(owner)){ + return 1; + } + return 0; } @@ -235,20 +212,15 @@ char* get_owner_details_surname(owner_details_t* owner){ } /** - * @fn char get_owner_details_reply*(owner_details_t) + * @fn char get_owner_details_reply*() * @brief get_owner_details_reply() can be used to get the current UDP reply * - * @param owner owner_details_t structure, it contains information about the owner * @return reply for UDP broadcast * this reply is formatted by @see format_reply() */ -char* get_owner_details_reply(owner_details_t *owner){ - if(owner == NULL || owner->reply == NULL){ - owner_details_error_handler(owner, GOD_REPLY,""); - return "|no reply formatted yet|"; - } - return owner->reply; +char* get_owner_details_reply(){ + return reply_str; } @@ -266,7 +238,6 @@ char* get_owner_details_reply(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){ - int i; int len; char *pc; char data[MAX_DATA_SIZE]; @@ -285,15 +256,15 @@ 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"); } else if(len <= MAX_DATA_SIZE){ - for(i = 0; i < len; i++) { + for(uint8_t i = 0; i < len; i++) { data[i] = pc[i]; } 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 = sizeof(reply_str); - p_data->tot_len = sizeof(reply_str); + p_data->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, 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); @@ -319,8 +290,7 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p * * @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_USE. The specified ipaddr and port are already bound to by another UDP PCB. */ err_t init_UDP_server(){ @@ -330,13 +300,13 @@ err_t init_UDP_server(){ connection = udp_new(); if(connection != NULL){ err = udp_bind(connection, IP_ANY_TYPE, 64000); - if(err == ERR_OK){ - udp_recv(connection, udp_receive_callback,NULL); - LOG_INFO(TAG,"initialising UDP server succesfull, callback running"); + if(err == ERR_OK){ + udp_recv(connection, udp_receive_callback,NULL); + LOG_INFO(TAG,"initialising UDP server succesfull, callback running"); } else{ - udp_remove(connection); - LOG_WARN(TAG,"initialising UDP server failed, err not ok"); + udp_remove(connection); + LOG_WARN(TAG,"initialising UDP server failed, err not ok"); } } else{