diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 06255a2..c49cfc8 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -19,6 +19,8 @@ #include "log.h" #include "udp.h" +#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" @@ -31,9 +33,8 @@ #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 63 // Define the maximum expected data size #define UDP_QUESTION1 "Where are you?v1.0" // Expected request from UDP client -#define UDP_QUESTION2 "Change details to:" // Expected request from UDP client /** * @struct owner_details_t @@ -45,7 +46,7 @@ typedef struct { char name[20]; char surname[20]; uint8_t mac_address[6]; - char reply[100]; + char reply[120]; }owner_details_t; // The following functions are used for owner details (those that must be available in main) @@ -94,12 +95,14 @@ char* udp_broadcast_get_owner_details_reply(); * @fn err_t udp_broadcast_init() * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() * + * @param x_pos : uint16_t that sets the x coordinate the owner's name will be written on the LCD + * @param y_pos : uint16_t that sets the y coordinate the owner's name will be written on the LCD * @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 udp_broadcast_init(uint16_t x_pos, uint16_t y_pos); // The following functions are used for UDP (those that must be available in main) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 4a756ad..d28aae7 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -14,6 +14,8 @@ static const char *TAG = "UDP_broadcast"; // Tag used in logs static owner_details_t udp_owner; +static uint16_t owner_name_x_pos = 10; +static uint16_t owner_name_y_pos = 10; // Functions static uint8_t udp_broadcast_set_owner_details_mac(); @@ -46,6 +48,7 @@ static uint8_t udp_broadcast_set_owner_details_mac(){ * @brief set_owner_details_name() sets the owner's name in the owner_details_t struct * if one of the pointers given is NULL it calls the error handler * strncpy is used to copy the function paremeter safely to the owner_details_t's name + * it also uses the lcd api to display the latest owner's name * * @param[out] owner owner_details_t structure, it contains information about the owner * @param[in] name string containing the owner's name @@ -60,7 +63,9 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* 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; } @@ -124,9 +129,9 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ static uint8_t udp_broadcast_format_reply(){ size_t reply_len = 0; char mac_addr_str[18]; - char reply_buf[100]; + char reply_buf[120]; - reply_len = 20 + 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], @@ -135,6 +140,7 @@ static uint8_t udp_broadcast_format_reply(){ 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); return 1; @@ -226,10 +232,12 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ counter++; } } - if(enders[2] - enders[1] < 20 && strlen(data) - enders[3] < 20 && 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];i++){ + for (uint8_t i = enders[1]+2; i< enders[2] && data[i] != '\0';i++){ buffer[counter] = data[i]; + LOG_DEBUG(TAG,"%d",counter); counter++; } if (strcmp(buffer,"") == 0){ @@ -239,7 +247,7 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ udp_broadcast_set_owner_details_name(buffer); memset(buffer, 0, sizeof(buffer)); counter = 0; - for (uint8_t i = enders[3]+2; i< strlen(data);i++){ + for (uint8_t i = enders[3]+2; i< strlen(data) && data[i] != '\0';i++){ buffer[counter] = data[i]; counter++; } @@ -296,7 +304,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p 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,udp_owner.reply); + 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); @@ -306,7 +314,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p 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,udp_owner.reply); + LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,64000,udp_owner.reply); } }else{ @@ -360,9 +368,10 @@ 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(){ +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; 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 0613e39..465131d 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -29,7 +29,6 @@ #include "llfs.h" #include "lcd_api.h" #include "UDP_broadcast.h" - /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -128,13 +127,13 @@ int main(void) llfs_init(); // Initialize the UDP broadcast service - if (udp_broadcast_init() != ERR_OK && udp_broadcast_connection_init() != ERR_OK){ + if (udp_broadcast_init(270,255) != ERR_OK && udp_broadcast_connection_init() != ERR_OK){ LOG_WARN(TAG,"error initializing udp connection"); } - if (!udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ + if (!udp_broadcast_set_owner_details("0123456879012345678", "default")){ LOG_WARN(TAG,"error setting owner's details"); } - LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply()); + /* USER CODE END 2 */ /* Infinite loop */