diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 67b9776..7f9ccfc 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -10,11 +10,11 @@ #define INC_UDP_BROADCAST_H_ // includes -#include #include #include -#include "lwip/netif.h" +#include #include "lwip.h" +#include "lwip/netif.h" #define LOGGER_LEVEL_INFO #include "log.h" #include "udp.h" @@ -22,19 +22,19 @@ #include "lcd_api.h" // Defines used by owner details error handler -#define SOD_NAME "set_owner_details_name" -#define GOD_NAME "get_owner_details_name" +#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" +#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" // Defines used by UDP callback -#define MAX_DATA_SIZE 63 // Define the maximum expected data size -#define UDP_QUESTION1 "Where are you?v1.0" // Expected request from UDP client +#define MAX_DATA_SIZE 63 // Define the maximum expected data size +#define UDP_QUESTION1 "Where are you?v1.0" // Expected request from UDP client /** * @struct owner_details_t @@ -42,12 +42,12 @@ * */ -typedef struct { +typedef struct { char name[20]; char surname[20]; uint8_t mac_address[6]; char reply[120]; -}owner_details_t; +} owner_details_t; // The following functions are used for owner details (those that must be available in main) @@ -62,7 +62,7 @@ typedef struct { * - 1: no error occured, details were set * - 0: an error occured, all or some details weren't set */ -uint8_t udp_broadcast_set_owner_details(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) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index deb4f58..78781c0 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -5,14 +5,14 @@ * 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 const char* TAG = "UDP_broadcast"; // Tag used in logs static owner_details_t udp_owner; static uint16_t owner_name_x_pos; static uint16_t owner_name_y_pos; @@ -23,7 +23,11 @@ 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 void 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); +static void udp_receive_callback(void* arg, + struct udp_pcb* connection, + struct pbuf* p, + const ip_addr_t* addr, + u16_t port); /** * @fn uint8_t udp_broadcast_set_owner_details_mac(owner_details_t*) @@ -31,10 +35,10 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p * and sets it in the owner_details_t struct */ -static void udp_broadcast_set_owner_details_mac(){ - for (uint8_t i = 0; i < 6; i++){ - udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address - } +static void udp_broadcast_set_owner_details_mac() { + for (uint8_t i = 0; i < 6; i++) { + udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address + } } /** @@ -49,16 +53,16 @@ static void udp_broadcast_set_owner_details_mac(){ * - 0: an error occured, name pointer is NULL */ -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); - lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16); - strncpy(udp_owner.name,name,sizeof(udp_owner.name)); - lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16); - return 1; +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); + lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16); + strncpy(udp_owner.name, name, sizeof(udp_owner.name)); + lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16); + return 1; } /** @@ -71,14 +75,14 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name){ * - 1: no error occured, surname was set * - 0: an error occured, surname pointer is NULL */ -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(udp_owner.surname,surname,sizeof(udp_owner.surname)); - return 1; +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(udp_owner.surname, surname, sizeof(udp_owner.surname)); + return 1; } /** @@ -92,14 +96,14 @@ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){ * - 0: an error occured, reply pointer is 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; - } - LOG_DEBUG(TAG,"set: %s",reply); - strncpy(udp_owner.reply,reply,sizeof(udp_owner.reply)); - return 1; +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; + } + LOG_DEBUG(TAG, "set: %s", reply); + strncpy(udp_owner.reply, reply, sizeof(udp_owner.reply)); + return 1; } /** @@ -109,23 +113,22 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ * it sets this reply with @see udp_broadcast_set_owner_details_reply() */ -static void udp_broadcast_format_reply(){ - size_t reply_len = 0; - char mac_addr_str[18]; - char reply_buf[120]; +static void udp_broadcast_format_reply() { + size_t reply_len = 0; + char mac_addr_str[18]; + char reply_buf[120]; - reply_len = 27 + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); + reply_len = 27 + 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", - 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(mac_addr_str, sizeof(mac_addr_str), "%02X:%02X:%02X:%02X:%02X:%02X", 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, udp_owner.surname, udp_owner.name); - - LOG_DEBUG(TAG,"reply_buf: %s",reply_buf); - udp_broadcast_set_owner_details_reply(reply_buf); + snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s", mac_addr_str, udp_owner.surname, + udp_owner.name); + LOG_DEBUG(TAG, "reply_buf: %s", reply_buf); + udp_broadcast_set_owner_details_reply(reply_buf); } /** @@ -140,14 +143,13 @@ static void udp_broadcast_format_reply(){ * - 1: no error occured, details were set * - 0: an error occured, all or some details weren't set */ -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; - +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; } /** @@ -158,8 +160,8 @@ uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname){ * this name is set by @see udp_broadcast_set_owner_details_name() */ -char* udp_broadcast_get_owner_details_name(){ - return udp_owner.name; +char* udp_broadcast_get_owner_details_name() { + return udp_owner.name; } /** @@ -170,8 +172,8 @@ char* udp_broadcast_get_owner_details_name(){ * this name is set by @see udp_broadcast_set_owner_details_surname() */ -char* udp_broadcast_get_owner_details_surname(){ - return udp_owner.surname; +char* udp_broadcast_get_owner_details_surname() { + return udp_owner.surname; } /** @@ -182,7 +184,7 @@ char* udp_broadcast_get_owner_details_surname(){ * this reply is formatted by @see format_reply() */ -char* udp_broadcast_get_owner_details_reply(){ +char* udp_broadcast_get_owner_details_reply() { return udp_owner.reply; } @@ -194,7 +196,7 @@ char* udp_broadcast_get_owner_details_reply(){ * @param[in] data the datagram received on port 64000 */ -static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ +static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]) { char func[7]; char buffer[20]; uint8_t enders[4]; @@ -202,39 +204,41 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ memset(func, 0, sizeof(func)); memset(buffer, 0, sizeof(buffer)); - for (uint8_t i = 0; i<6;i++){ + for (uint8_t i = 0; i < 6; i++) { func[i] = data[i]; } - if (strcmp(func,"func1:")==0){ - for (uint8_t i = 0; i < strlen(data); i++){ - if (data[i] == ',' || data[i] == ':'){ + if (strcmp(func, "func1:") == 0) { + for (uint8_t i = 0; i < strlen(data); i++) { + if (data[i] == ',' || data[i] == ':') { enders[counter] = i; counter++; } } - LOG_DEBUG(TAG,"%d-%d=%d, %d-%d=%d",enders[2],enders[1],enders[2] - enders[1],strlen(data),enders[3],strlen(data) - enders[3]); - if(enders[2] - enders[1] < 22 && strlen(data) - enders[3] < 22 && strncmp(data+enders[0],":name",5) == 0 && strncmp(data+enders[2],", surname",9) == 0 ){ + LOG_DEBUG(TAG, "%d-%d=%d, %d-%d=%d", enders[2], enders[1], enders[2] - enders[1], strlen(data), enders[3], + strlen(data) - enders[3]); + if (enders[2] - enders[1] < 22 && strlen(data) - enders[3] < 22 && strncmp(data + enders[0], ":name", 5) == 0 + && strncmp(data + enders[2], ", surname", 9) == 0) { counter = 0; - for (uint8_t i = enders[1]+2; i< enders[2] && data[i] != '\0';i++){ + for (uint8_t i = enders[1] + 2; i < enders[2] && data[i] != '\0'; i++) { buffer[counter] = data[i]; - LOG_DEBUG(TAG,"%d",counter); + LOG_DEBUG(TAG, "%d", counter); counter++; } - if (strcmp(buffer,"") == 0){ - strncpy(buffer,"name",sizeof(buffer)); + if (strcmp(buffer, "") == 0) { + strncpy(buffer, "name", sizeof(buffer)); } - LOG_INFO(TAG,"new owner name:%s",buffer); + LOG_INFO(TAG, "new owner name:%s", buffer); udp_broadcast_set_owner_details_name(buffer); memset(buffer, 0, sizeof(buffer)); counter = 0; - for (uint8_t i = enders[3]+2; i< strlen(data) && data[i] != '\0';i++){ + for (uint8_t i = enders[3] + 2; i < strlen(data) && data[i] != '\0'; i++) { buffer[counter] = data[i]; counter++; } - if (strcmp(buffer,"") == 0){ - strncpy(buffer,"default",sizeof(buffer)); + if (strcmp(buffer, "") == 0) { + strncpy(buffer, "default", sizeof(buffer)); } - LOG_INFO(TAG,"new owner surname:%s",buffer); + LOG_INFO(TAG, "new owner surname:%s", buffer); udp_broadcast_set_owner_details_surname(buffer); udp_broadcast_format_reply(); } @@ -255,58 +259,62 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ * @param[in] 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){ - struct pbuf *p_data; +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]; + char* pc; + char data[MAX_DATA_SIZE]; + char source_ip_str[16]; - memset(data, 0, sizeof(data)); + memset(data, 0, sizeof(data)); - ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); // Convert the source IP address to a string + ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); // Convert the source IP address to a string - if (p != NULL) { - pc = (char*)p->payload; - len = p->tot_len; - 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){ - for (size_t i = 0; i < len; i++) { - data[i] = pc[i]; - } + if (p != NULL) { + pc = (char*)p->payload; + len = p->tot_len; + 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) { + 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){ - 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,64000,udp_owner.reply); - }else{ - LOG_INFO(TAG,"other function called"); - udp_broadcast_check_function(data); - 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,64000,udp_owner.reply); - } + 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 = 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, 64000, udp_owner.reply); + } else { + LOG_INFO(TAG, "other function called"); + udp_broadcast_check_function(data); + 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, 64000, udp_owner.reply); + } - }else{ - LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); - } + } else { + LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", MAX_DATA_SIZE); + } - }else{ - LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer"); - } - pbuf_free(p); - pbuf_free(p_data); + } else { + LOG_WARN(TAG, "udp_receive_callback: input buffer was a NULL pointer"); + } + pbuf_free(p); + pbuf_free(p_data); } /** @@ -320,25 +328,25 @@ 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_connection_init(){ - struct udp_pcb* connection; - err_t err; +err_t udp_broadcast_connection_init() { + struct udp_pcb* connection; + err_t err; - LOG_INFO(TAG,"initialising 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"); - }else{ - udp_remove(connection); - LOG_WARN(TAG,"initialising UDP server failed, err not ok"); - } - }else{ - LOG_WARN(TAG,"initialising UDP server failed, connection is null"); - } - return err; + LOG_INFO(TAG, "initialising 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"); + } else { + udp_remove(connection); + LOG_WARN(TAG, "initialising UDP server failed, err not ok"); + } + } else { + LOG_WARN(TAG, "initialising UDP server failed, connection is null"); + } + return err; } /** @@ -349,7 +357,7 @@ err_t udp_broadcast_connection_init(){ * - 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(uint16_t x_pos, uint16_t y_pos){ +err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos) { err_t err; owner_name_x_pos = x_pos; owner_name_y_pos = y_pos;