diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 2c5126c..8276c65 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -19,7 +19,7 @@ #include "lcd_api.h" -// Defines used by owner details error handler +// Defines used by owner details #define SOD_NAME "set_owner_details_name" #define GOD_NAME "get_owner_details_name" #define SOD_SURNAME "set_owner_details_surname" @@ -40,6 +40,8 @@ #define MAX_NAME_SIZE 20 #define MAX_REPLY_SIZE 120 +#define MAX_MAX_ADDR_LEN 18 +#define MAX_EXTRA_REPLY_CHARS 27 /** @@ -58,21 +60,21 @@ typedef struct { // The following functions are used for owner details (those that must be available in main) /** - * @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 + * @fn err_t udp_broadcast_set_owner_details(const char*, const char*) + * @brief udp_broadcast_set_owner_details() is the interface that can be used in other files * to set the owner's details * * @param[in] name string containing the new owner's name * @param[in] 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 + * @return lwIP error code. + * - ERR_OK. Successful. No error occurred. + * - ERR_ARG. one or both arguments are NULL pointers */ -uint8_t udp_broadcast_set_owner_details(const char*, const char*); +err_t udp_broadcast_set_owner_details(const char*, const char*); /** * @fn char udp_broadcast_get_owner_details_name*(void) - * @brief get_owner_details_name() can be used to get the current owner's name + * @brief udp_broadcast_get_owner_details_name() can be used to get the current owner's name * * @return name of owner * this name is set by @see udp_broadcast_set_owner_details_name() @@ -81,7 +83,7 @@ char* udp_broadcast_get_owner_details_name(void); /** * @fn char udp_broadcast_get_owner_details_surname*(void) - * @brief get_owner_details_surname() can be used to get the current owner's surname + * @brief udp_broadcast_get_owner_details_surname() can be used to get the current owner's surname * * @return surname of owner * this name is set by @see udp_broadcast_set_owner_details_surname() @@ -90,13 +92,15 @@ char* udp_broadcast_get_owner_details_surname(void); /** * @fn char udp_broadcast_get_owner_details_reply*(void) - * @brief get_owner_details_reply() can be used to get the current UDP reply + * @brief udp_broadcast_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* udp_broadcast_get_owner_details_reply(void); +// Initialization functions + /** * @fn err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos) * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() @@ -106,12 +110,13 @@ char* udp_broadcast_get_owner_details_reply(void); * @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_MEM. udp pcb couldn't be created + * + * - ERR_ARG. one or both arguments of udp_broadcast_set_owner_details() are NULL pointers */ 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) - /** * @fn err_t udp_broadcast_connection_init() * @brief udp_broadcast_connection_init() initializes the UDP connection so that it listens for all traffic on @@ -121,6 +126,7 @@ err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos); * @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_MEM. udp pcb couldn't be created */ err_t udp_broadcast_connection_init(void); diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index f7660cf..3e172f9 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -116,10 +116,10 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply) { static void udp_broadcast_format_reply(void) { size_t reply_len = 0; - char mac_addr_str[18]; + char mac_addr_str[MAX_MAX_ADDR_LEN]; char reply_buf[MAX_REPLY_SIZE]; - reply_len = 27 + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); + reply_len = MAX_EXTRA_REPLY_CHARS + 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], @@ -127,30 +127,28 @@ static void udp_broadcast_format_reply(void) { 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); } /** - * @fn uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char*, const char*) + * @fn err_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 * the pointers get checked by the functions that are called in this function * * @param[in] name string containing the new owner's name * @param[in] 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 + * @return lwIP error code. + * - ERR_OK. Successful. No error occurred. + * - ERR_ARG. one or both arguments are NULL pointers */ -uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname) { +err_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 ERR_ARG; } - return 0; + return ERR_OK; } /** @@ -212,8 +210,8 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]) { LOG_WARN(TAG, "udp_broadcast_check_function: datagram does not contain function that's currently available"); return; } - for (uint8_t i = 0; i < data_len; i++) { - if ((data[i] == ',' || data[i] == ':') && counter <= MAX_COLON_COMMA_COUNT) { + for (uint8_t i = 0; i < data_len && counter < MAX_COLON_COMMA_COUNT; i++) { + if ((data[i] == ',' || data[i] == ':')) { enders[counter] = i; counter++; } @@ -327,6 +325,7 @@ defer: * @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_MEM. udp pcb couldn't be created */ err_t udp_broadcast_connection_init(void) { @@ -337,6 +336,7 @@ err_t udp_broadcast_connection_init(void) { connection = udp_new(); if (connection == NULL) { LOG_WARN(TAG, "Initializing UDP server failed, connection is null"); + return ERR_MEM; } err = udp_bind(connection, IP_ANY_TYPE, 64000); if (err != ERR_OK) { @@ -358,10 +358,16 @@ err_t udp_broadcast_connection_init(void) { * @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_MEM. udp pcb couldn't be created + * + * - ERR_ARG. one or both arguments of udp_broadcast_set_owner_details() are NULL pointers */ err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos) { owner_name_x_pos = x_pos; owner_name_y_pos = y_pos; - udp_broadcast_set_owner_details("name", "default"); + if(udp_broadcast_set_owner_details("name", "default") != ERR_OK){ + LOG_WARN(TAG, "udp_broadcast_init: don't give NULL pointers as arguments for the owner's details"); + return ERR_ARG; + } return udp_broadcast_connection_init(); } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 0678478..6506a54 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -138,7 +138,7 @@ int main(void) 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("Joran", "Van Nieuwenhoven")){ LOG_WARN(TAG,"error setting owner's details"); } @@ -148,10 +148,10 @@ int main(void) /* USER CODE BEGIN WHILE */ while (1) { - /* USER CODE END WHILE */ + /* USER CODE END WHILE */ - /* USER CODE BEGIN 3 */ - MX_LWIP_Process(); + /* USER CODE BEGIN 3 */ + MX_LWIP_Process(); lcd_task(); } /* USER CODE END 3 */