diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index f5631ac..97f24e6 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -46,13 +46,13 @@ typedef struct { }owner_details_t; // 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* get_owner_details_surname(owner_details_t*); -char* get_owner_details_reply(); +char* udp_broadcast_get_owner_details_name(owner_details_t*); +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) -err_t init_UDP_server(); +err_t udp_broadcast_init(); #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 9014598..b9d6dfa 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -14,8 +14,20 @@ 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 +// 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 * it checks which of the parameters in the function where the error occured, is a NULL pointer * and logs a warning depending on that @@ -26,11 +38,11 @@ static char reply_str[100] = "|no reply formatted yet|"; // Global reply stri * @param funct name of the function where the error occured */ -static void owner_details_error_handler(owner_details_t* owner, char* funct,char* word){ - if(owner == NULL && word == NULL){ +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){ + else if (owner == NULL){ LOG_WARN(TAG,"%s: owner given is a NULL pointer",funct); } else{ @@ -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 * and sets it in the owner_details_t struct * @@ -49,19 +61,19 @@ static void owner_details_error_handler(owner_details_t* owner, char* funct,char * - 0: an error occured, owner pointer is NULL */ -static uint8_t set_owner_details_mac(owner_details_t* owner){ - if(owner == NULL){ - owner_details_error_handler(owner, SOD_MAC,""); +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; } - for(uint8_t i = 0; i < 6; i++){ + for (uint8_t i = 0; i < 6; i++){ owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address } return 1; } /** - * @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 * * @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 */ -static uint8_t set_owner_details_name(owner_details_t *owner, char *name){ - if(name == NULL || owner == NULL){ - owner_details_error_handler(owner, SOD_NAME, name); +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); return 0; } 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 * * @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 * - 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){ - owner_details_error_handler(owner, SOD_SURNAME, surname); +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); return 0; } 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 * * @param reply string used to reply to the UDP broadcast @@ -111,8 +123,8 @@ 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 */ -static uint8_t set_owner_details_reply(char *reply){ - if(reply == NULL){ +static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ + if (reply == NULL){ LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_REPLY); 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 * * @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 */ -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; char mac_addr_str[18]; char reply_buf[100]; if (owner == NULL) { - owner_details_error_handler(owner, F_REPLY,""); + udp_broadcast_owner_details_error_handler(owner, F_REPLY,""); return 0; } 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", mac_addr_str, owner->surname, owner->name); - set_owner_details_reply(reply_buf); + udp_broadcast_set_owner_details_reply(reply_buf); 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 * 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 * - 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){ - owner_details_error_handler(owner, SOD,""); +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(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 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 * * @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() */ -char* get_owner_details_name(owner_details_t *owner){ - if(owner == NULL || owner->name == NULL){ - owner_details_error_handler(owner, GOD_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; } /** - * @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 * * @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() */ -char* get_owner_details_surname(owner_details_t* owner){ - if(owner == NULL || owner->surname == NULL){ - owner_details_error_handler(owner, GOD_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; } /** - * @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 * * @return reply for UDP broadcast * this reply is formatted by @see format_reply() */ -char* get_owner_details_reply(){ +char* udp_broadcast_get_owner_details_reply(){ return reply_str; } @@ -237,12 +249,12 @@ char* get_owner_details_reply(){ * @param port the source port number of the sender's UDP packet */ -static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct pbuf *p, const ip_addr_t *addr, u16_t port){ - int len; +static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct pbuf* p, const ip_addr_t* addr, u16_t port){ + struct pbuf *p_data; + size_t len; char *pc; char data[MAX_DATA_SIZE]; char source_ip_str[16]; - struct pbuf *p_data; memset(data, 0, sizeof(data)); @@ -255,18 +267,19 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p if (p_data == NULL){ LOG_WARN(TAG,"udp_receive_callback: unable to allocate data buffer for reply"); } - else if(len <= MAX_DATA_SIZE){ - for(uint8_t i = 0; i < len; i++) { + else if (len <= MAX_DATA_SIZE){ + for (size_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){ + if (strcmp(data,UDP_QUESTION1) == 0){ p_data->payload = 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*/ + 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); } @@ -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 * port 6400 * @@ -293,14 +306,15 @@ 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 init_UDP_server(){ - struct udp_pcb *connection; +err_t udp_broadcast_init(){ + struct udp_pcb* connection; err_t err; + LOG_INFO(TAG,"initialising UDP server"); connection = udp_new(); - if(connection != NULL){ + if (connection != NULL){ err = udp_bind(connection, IP_ANY_TYPE, 64000); - if(err == ERR_OK){ + if (err == ERR_OK){ udp_recv(connection, udp_receive_callback,NULL); LOG_INFO(TAG,"initialising UDP server succesfull, callback running"); } @@ -314,6 +328,3 @@ err_t init_UDP_server(){ } return err; } - - - diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index ae71e4c..bbfd7a2 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,12 +111,12 @@ int main(void) MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ 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,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply(&owner)); /* USER CODE END 2 */ /* Infinite loop */