From 01f018536f1112761425a94692732696cb38645b Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 6 Nov 2023 19:41:24 +0100 Subject: [PATCH 01/36] added header file with first functions wrote functions to work with the owner data --- project/Core/Inc/UDP_broadcast.h | 26 +++++ project/Core/Src/UDP_broadcast.c | 168 +++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 project/Core/Inc/UDP_broadcast.h create mode 100644 project/Core/Src/UDP_broadcast.c diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h new file mode 100644 index 0000000..f4e062d --- /dev/null +++ b/project/Core/Inc/UDP_broadcast.h @@ -0,0 +1,26 @@ +/* + * UDP_broadcast.h + * + * Created on: Nov 6, 2023 + * Author: joran + */ + +#ifndef INC_UDP_BROADCAST_H_ +#define INC_UDP_BROADCAST_H_ + +typedef struct { + char* name; + char* surname; + uint8_t mac_address[6]; + char* reply; +}owner_details_t; + +uint8_t set_owner_details_name(owner_details_t*, char* ); +uint8_t set_owner_details_sirname(owner_details_t*, char* ); +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); + + +#endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c new file mode 100644 index 0000000..64a9b46 --- /dev/null +++ b/project/Core/Src/UDP_broadcast.c @@ -0,0 +1,168 @@ +/* + * UDP_broadcast.c + * + * Created on: Nov 6, 2023 + * Author: joran + */ + //| +#include "UDP_broadcast.h" +#include + +/** + * @fn void set_owner_details_mac(owner_details_t*) + * @brief + * + * @param owner + */ + +static void set_owner_details_mac(owner_details_t* owner){ + uint32_t uID[3]; + + HAL_GetUID(uID); // Read the UID registers + + owner->mac_address[0] = (uID[0] >> 0) & 0xFF; + owner->mac_address[1] = (uID[0] >> 8) & 0xFF; + owner->mac_address[2] = (uID[0] >> 16) & 0xFF; + owner->mac_address[3] = (uID[1] >> 0) & 0xFF; + owner->mac_address[4] = (uID[1] >> 8) & 0xFF; + owner->mac_address[5] = (uID[1] >> 16) & 0xFF; +} + +/** + * @fn uint8_t set_owner_details_name(owner_details_t*, char*) + * @brief + * + * @param owner + * @param name + * @return + */ + +uint8_t set_owner_details_name(owner_details_t* owner, char* name){ + if(name != NULL){ + if(owner->name == NULL){ + owner->name = (char*)malloc(strlen(name) + 1); + } + else{ + owner->name = (char*)realloc(owner->name,strlen(name) + 1); + } + strcpy(owner->name,name); + return 1; + } + else{ + return 0; + } + +} + +/** + * @fn uint8_t set_owner_details_surname(owner_details_t*, char*) + * @brief + * + * @param owner + * @param surname + * @return + */ +uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ + if(surname != NULL){ + if(owner->surname == NULL){ + owner->surname = (char*)malloc(strlen(surname) + 1); + } + else{ + owner->surname = (char*)realloc(owner->surname,strlen(surname) + 1); + } + strcpy(owner->surname,surname); + return 1; + } + else{ + return 0; + } +} + +/** + * @fn uint8_t set_owner_details_reply(owner_details_t, char*) + * @brief + * + * @param owner + * @param reply + * @return + */ + +static uint8_t set_owner_details_reply(owner_details_t owner, char * reply){ + if(reply != NULL){ + if(owner->reply == NULL){ + owner->reply = (char*)malloc(strlen(reply) + 1); + } + else{ + owner->reply = (char*)realloc(owner->reply,strlen(reply) + 1); + } + strcpy(owner->reply,reply); + return 1; + } + else{ + return 0; + } +} + +/** + * @fn void format_reply(owner_details_t*) + * @brief + * + * @param owner + */ + +static void format_reply(owner_details_t *owner){ + size_t reply_len = 0; + char mac_addr_str[18]; + char* reply_buf = NULL; + if (owner != NULL) { + reply_len = 20 + strlen(mac_addr_str) + strlen(owner->surname) + strlen(owner->name); + reply_buf = (char*)malloc(reply_len); + + + 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]); + + if (replyBuffer != NULL) { + 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); + + free(reply_buf); // Free the temporary buffer + } + + } +} + +/** + * @fn uint8_t set_owner_details(owner_details_t*, char*, char*) + * @brief + * + * @param owner + * @param name + * @param surname + * @return + */ +uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ + if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner)){ + format_reply(owner); + } +} + +char* get_owner_details_name(owner_details_t owner){ + char *err_reply = "no name yet"; + if(owner == NULL || owner->name == NULL){ + owner->name = (char*)malloc(strlen(err_reply)); + } + return owner->name; +} +char* get_owner_details_surname(owner_details_t owner){ + char *err_reply = "no surname yet"; + if(owner == NULL || owner->surname == NULL){ + owner->surname = (char*)malloc(strlen(err_reply)); + } + return owner->name; +} + + From 5942274e6a4cc4637c4ea0bdbe79c59b995f4cb3 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 6 Nov 2023 20:15:00 +0100 Subject: [PATCH 02/36] edit and testing code edited some functions and testing code in main --- project/Core/Inc/UDP_broadcast.h | 1 + project/Core/Src/UDP_broadcast.c | 36 ++++++++++++++++++++++++++++++++ project/Core/Src/main.c | 17 +++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index f4e062d..74deda5 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -21,6 +21,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); #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 64a9b46..3d45bc8 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -150,17 +150,53 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ } } +/** + * @fn char get_owner_details_name*(owner_details_t) + * @brief + * + * @param owner + * @return + */ + char* get_owner_details_name(owner_details_t owner){ char *err_reply = "no name yet"; if(owner == NULL || owner->name == NULL){ owner->name = (char*)malloc(strlen(err_reply)); + strcpy(owner->name,err_reply); } return owner->name; } + +/** + * @fn char get_owner_details_surname*(owner_details_t) + * @brief + * + * @param owner + * @return + */ + char* get_owner_details_surname(owner_details_t owner){ char *err_reply = "no surname yet"; if(owner == NULL || owner->surname == NULL){ owner->surname = (char*)malloc(strlen(err_reply)); + strcpy(owner->name,err_reply); + } + return owner->name; +} + +/** + * @fn char get_owner_details_reply*(owner_details_t) + * @brief + * + * @param + * @return + */ + +char* get_owner_details_reply(owner_details_t){ + char *err_reply = "no reply yet"; + if(owner == NULL || owner->reply == NULL){ + owner->reply = (char*)malloc(strlen(err_reply)); + strcpy(owner->name,err_reply); } return owner->name; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 691c4fe..ac808b3 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -23,6 +23,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "log.h" +#include "UDP_broadcast.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -70,7 +71,17 @@ static void MX_QUADSPI_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ +int _write(int file, char *ptr, int len) { + for (int x = 0;x Date: Mon, 6 Nov 2023 20:18:22 +0100 Subject: [PATCH 03/36] Create fsdata_custom.c got this error again, rookie mistake --- project/Core/Inc/fsdata_custom.c | 156 +++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 project/Core/Inc/fsdata_custom.c diff --git a/project/Core/Inc/fsdata_custom.c b/project/Core/Inc/fsdata_custom.c new file mode 100644 index 0000000..55cf48e --- /dev/null +++ b/project/Core/Inc/fsdata_custom.c @@ -0,0 +1,156 @@ +#include "lwip/apps/fs.h" +#include "lwip/def.h" + + +#define file_NULL (struct fsdata_file *) NULL + + +#ifndef FS_FILE_FLAGS_HEADER_INCLUDED +#define FS_FILE_FLAGS_HEADER_INCLUDED 1 +#endif +#ifndef FS_FILE_FLAGS_HEADER_PERSISTENT +#define FS_FILE_FLAGS_HEADER_PERSISTENT 0 +#endif +/* FSDATA_FILE_ALIGNMENT: 0=off, 1=by variable, 2=by include */ +#ifndef FSDATA_FILE_ALIGNMENT +#define FSDATA_FILE_ALIGNMENT 0 +#endif +#ifndef FSDATA_ALIGN_PRE +#define FSDATA_ALIGN_PRE +#endif +#ifndef FSDATA_ALIGN_POST +#define FSDATA_ALIGN_POST +#endif +#if FSDATA_FILE_ALIGNMENT==2 +#include "fsdata_alignment.h" +#endif +#if FSDATA_FILE_ALIGNMENT==1 +static const unsigned int dummy_align__index_html = 0; +#endif +static const unsigned char FSDATA_ALIGN_PRE data__index_html[] FSDATA_ALIGN_POST = { +/* /index.html (12 chars) */ +0x2f,0x69,0x6e,0x64,0x65,0x78,0x2e,0x68,0x74,0x6d,0x6c,0x00, + +/* HTTP header */ +/* "HTTP/1.0 200 OK +" (17 bytes) */ +0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d, +0x0a, +/* "Server: lwIP/2.1.2 (http://savannah.nongnu.org/projects/lwip) +" (63 bytes) */ +0x53,0x65,0x72,0x76,0x65,0x72,0x3a,0x20,0x6c,0x77,0x49,0x50,0x2f,0x32,0x2e,0x31, +0x2e,0x32,0x20,0x28,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x73,0x61,0x76,0x61,0x6e, +0x6e,0x61,0x68,0x2e,0x6e,0x6f,0x6e,0x67,0x6e,0x75,0x2e,0x6f,0x72,0x67,0x2f,0x70, +0x72,0x6f,0x6a,0x65,0x63,0x74,0x73,0x2f,0x6c,0x77,0x69,0x70,0x29,0x0d,0x0a, +/* "Content-Length: 481 +" (18+ bytes) */ +0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20, +0x34,0x38,0x31,0x0d,0x0a, +/* "Content-Encoding: deflate +" (27 bytes) */ +0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,0x69,0x6e,0x67, +0x3a,0x20,0x64,0x65,0x66,0x6c,0x61,0x74,0x65,0x0d,0x0a, +/* "Content-Type: text/html + +" (27 bytes) */ +0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x3a,0x20,0x74,0x65, +0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x0d,0x0a, +/* raw file data (481 bytes) */ +0xad,0x55,0x5d,0x6f,0xda,0x30,0x14,0x7d,0x9f,0xb4,0xff,0xe0,0x79,0x55,0x15,0xa4, +0xcd,0x29,0x9b,0xb4,0x07,0x16,0x32,0xd1,0x16,0x4d,0x9b,0xd0,0xa8,0xa6,0x3e,0x6c, +0x8f,0xc1,0x71,0x1a,0x43,0x62,0x33,0xfb,0x5a,0x2b,0xaa,0xfa,0xdf,0x6b,0x93,0x2f, +0x52,0x12,0x44,0x55,0xfc,0x40,0xc2,0xbd,0xe7,0xfa,0x9c,0xeb,0x73,0x15,0x07,0xef, +0xae,0xe7,0x57,0xb7,0x7f,0x6f,0xa6,0x28,0x85,0x3c,0x0b,0xdf,0xbe,0x09,0xea,0x27, +0x8b,0x62,0xfb,0x44,0x76,0x05,0xc0,0x21,0x63,0xe1,0xe4,0xe7,0xe4,0x0f,0x9a,0x4d, +0xaf,0xd1,0x95,0x14,0xa0,0x64,0x16,0xf8,0x45,0xbc,0x04,0x69,0xaa,0xf8,0x1a,0x90, +0x56,0x74,0x8c,0x53,0x80,0xb5,0x1e,0xf9,0x3e,0x95,0x31,0x23,0xcb,0x7f,0x86,0xa9, +0x0d,0xa1,0x32,0xf7,0x8b,0xd7,0x8f,0x9f,0xc9,0x17,0x72,0x41,0x72,0x2e,0xc8,0x52, +0xe3,0x30,0xf0,0x8b,0x52,0xc7,0xea,0x97,0xb4,0xc1,0x42,0xc6,0x9b,0x6a,0xe7,0x74, +0x18,0xb6,0x68,0xed,0xff,0x22,0x53,0xe6,0x17,0x06,0x40,0x0a,0xc4,0xe3,0x31,0xb6, +0x38,0x30,0x4a,0xcc,0x05,0xde,0x96,0xcc,0x45,0xe0,0x17,0xd9,0xb0,0x1f,0x9b,0x24, +0x25,0x38,0x49,0x9e,0xa1,0x1b,0xfe,0x45,0x44,0x57,0x19,0xbf,0x4b,0xe1,0x28,0x15, +0x97,0xb3,0x4a,0xc4,0xe5,0xec,0xb0,0x86,0x12,0xe9,0x24,0x38,0x68,0x4b,0xc1,0xcb, +0xbb,0x9f,0xcc,0x6a,0x62,0xfb,0x7a,0x98,0xb9,0xc2,0x3a,0xea,0x2d,0xb8,0xa3,0xfb, +0x96,0xb3,0x65,0xd4,0xad,0x33,0x2f,0x96,0xd4,0xe4,0x4c,0xc0,0x80,0x28,0x6b,0xd8, +0xc6,0x4b,0x8c,0xa0,0xc0,0xa5,0xf0,0x06,0xe8,0xa1,0xc1,0x15,0x58,0xfc,0xbe,0x71, +0x65,0x40,0x68,0xc6,0xe9,0xea,0x00,0xde,0x2d,0xcd,0x44,0xfc,0x9b,0xd9,0x59,0xd1, +0xe0,0x61,0x9f,0x8b,0x98,0xdd,0x13,0x37,0x97,0xdf,0x6e,0x7e,0xfc,0x1a,0x8e,0x87, +0x78,0xf0,0xb5,0x5d,0xf3,0xe8,0x02,0xfd,0xb4,0xb6,0xc5,0x53,0xf0,0x5e,0x1c,0xcb, +0x5b,0xdb,0xff,0x7a,0xd6,0x4f,0xc7,0x77,0xdb,0x8c,0xd2,0x29,0x68,0x5d,0xb3,0xc7, +0xf1,0x36,0x43,0x77,0x12,0x73,0xcf,0x5f,0xd6,0xf5,0xce,0x18,0x9f,0xc4,0xe3,0xf3, +0xba,0xfb,0x0e,0xfa,0xdd,0xc0,0x73,0x31,0x15,0x6b,0x8b,0xc0,0xa8,0xac,0x53,0xc4, +0x19,0x89,0x96,0xd1,0xbd,0xd7,0x91,0x71,0xcb,0x56,0x8d,0xdc,0xcf,0x87,0xee,0x34, +0x6c,0xd6,0x6c,0x84,0xf0,0xf7,0xe9,0x2d,0xee,0x41,0x68,0x43,0x29,0xd3,0x7a,0x54, +0x8b,0xf2,0x14,0xd3,0x6b,0x29,0x34,0xeb,0x54,0xb3,0xed,0xaf,0x67,0x2b,0xa6,0x94, +0x54,0x3b,0x1b,0xf5,0x6e,0x50,0x19,0xa2,0x21,0x02,0xa3,0xad,0x19,0xee,0x50,0x3d, +0x3c,0x75,0xf5,0x48,0x52,0x6a,0x94,0x62,0x31,0xfa,0x9f,0xf2,0x8c,0xa1,0x3c,0x5a, +0x71,0x71,0x87,0x20,0x65,0x48,0x15,0x27,0x45,0xf6,0x0e,0xbc,0x16,0xb6,0x1f,0xde, +0xf3,0x62,0x07,0x53,0xe7,0x5a,0xb7,0x4a,0x79,0x9b,0xd8,0x8f,0x67,0x71,0xbb,0x3d, +0x01,}; + +#if FSDATA_FILE_ALIGNMENT==1 +static const unsigned int dummy_align__info_shtml = 1; +#endif +static const unsigned char FSDATA_ALIGN_PRE data__info_shtml[] FSDATA_ALIGN_POST = { +/* /info.shtml (12 chars) */ +0x2f,0x69,0x6e,0x66,0x6f,0x2e,0x73,0x68,0x74,0x6d,0x6c,0x00, + +/* HTTP header */ +/* "HTTP/1.0 200 OK +" (17 bytes) */ +0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d, +0x0a, +/* "Server: lwIP/2.1.2 (http://savannah.nongnu.org/projects/lwip) +" (63 bytes) */ +0x53,0x65,0x72,0x76,0x65,0x72,0x3a,0x20,0x6c,0x77,0x49,0x50,0x2f,0x32,0x2e,0x31, +0x2e,0x32,0x20,0x28,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x73,0x61,0x76,0x61,0x6e, +0x6e,0x61,0x68,0x2e,0x6e,0x6f,0x6e,0x67,0x6e,0x75,0x2e,0x6f,0x72,0x67,0x2f,0x70, +0x72,0x6f,0x6a,0x65,0x63,0x74,0x73,0x2f,0x6c,0x77,0x69,0x70,0x29,0x0d,0x0a, +/* "Content-Type: text/html +Expires: Fri, 10 Apr 2008 14:00:00 GMT +Pragma: no-cache + +" (85 bytes) */ +0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x3a,0x20,0x74,0x65, +0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x45,0x78,0x70,0x69,0x72,0x65,0x73, +0x3a,0x20,0x46,0x72,0x69,0x2c,0x20,0x31,0x30,0x20,0x41,0x70,0x72,0x20,0x32,0x30, +0x30,0x38,0x20,0x31,0x34,0x3a,0x30,0x30,0x3a,0x30,0x30,0x20,0x47,0x4d,0x54,0x0d, +0x0a,0x50,0x72,0x61,0x67,0x6d,0x61,0x3a,0x20,0x6e,0x6f,0x2d,0x63,0x61,0x63,0x68, +0x65,0x0d,0x0a,0x0d,0x0a, +/* raw file data (217 bytes) */ +0x3c,0x21,0x44,0x4f,0x43,0x54,0x59,0x50,0x45,0x20,0x68,0x74,0x6d,0x6c,0x3e,0x0d, +0x0a,0x3c,0x68,0x74,0x6d,0x6c,0x3e,0x0d,0x0a,0x3c,0x68,0x65,0x61,0x64,0x3e,0x0d, +0x0a,0x20,0x20,0x20,0x20,0x3c,0x74,0x69,0x74,0x6c,0x65,0x3e,0x4d,0x79,0x20,0x53, +0x54,0x4d,0x33,0x32,0x20,0x57,0x65,0x62,0x20,0x50,0x61,0x67,0x65,0x3c,0x2f,0x74, +0x69,0x74,0x6c,0x65,0x3e,0x0d,0x0a,0x3c,0x2f,0x68,0x65,0x61,0x64,0x3e,0x0d,0x0a, +0x3c,0x62,0x6f,0x64,0x79,0x3e,0x0d,0x0a,0x20,0x20,0x20,0x20,0x3c,0x68,0x31,0x3e, +0x57,0x65,0x6c,0x63,0x6f,0x6d,0x65,0x20,0x74,0x6f,0x20,0x6d,0x79,0x20,0x53,0x54, +0x4d,0x33,0x32,0x20,0x57,0x65,0x62,0x20,0x50,0x61,0x67,0x65,0x3c,0x2f,0x68,0x31, +0x3e,0x0d,0x0a,0x20,0x20,0x20,0x20,0x3c,0x70,0x3e,0x56,0x65,0x72,0x73,0x69,0x6f, +0x6e,0x3a,0x20,0x3c,0x21,0x2d,0x2d,0x23,0x56,0x45,0x52,0x53,0x49,0x4f,0x4e,0x2d, +0x2d,0x3e,0x3c,0x2f,0x70,0x3e,0x0d,0x0a,0x20,0x20,0x20,0x20,0x3c,0x70,0x3e,0x44, +0x61,0x74,0x65,0x3a,0x20,0x3c,0x21,0x2d,0x2d,0x23,0x44,0x41,0x54,0x45,0x2d,0x2d, +0x3e,0x3c,0x2f,0x70,0x3e,0x0d,0x0a,0x3c,0x2f,0x62,0x6f,0x64,0x79,0x3e,0x0d,0x0a, +0x3c,0x2f,0x68,0x74,0x6d,0x6c,0x3e,0x0d,0x0a,}; + + + +const struct fsdata_file file__index_html[] = { { +file_NULL, +data__index_html, +data__index_html + 12, +sizeof(data__index_html) - 12, +FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT, +}}; + +const struct fsdata_file file__info_shtml[] = { { +file__index_html, +data__info_shtml, +data__info_shtml + 12, +sizeof(data__info_shtml) - 12, +FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_SSI, +}}; + +#define FS_ROOT file__info_shtml +#define FS_NUMFILES 2 + From 5df86b3b8321eca42f33bab5a4753c116a06b488 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 6 Nov 2023 22:23:11 +0100 Subject: [PATCH 04/36] resolving "some" errors LOG_EDBUG and printf don't seem to do anything --- project/Core/Inc/UDP_broadcast.h | 5 ++++ project/Core/Src/UDP_broadcast.c | 50 +++++++++++++++----------------- project/Core/Src/main.c | 22 +++++--------- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 74deda5..5edc19c 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -8,6 +8,11 @@ #ifndef INC_UDP_BROADCAST_H_ #define INC_UDP_BROADCAST_H_ +#include +#include +#include +#include "lwip/netif.h" +#include "lwip.h" typedef struct { char* name; char* surname; diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 3d45bc8..ca65710 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -6,7 +6,7 @@ */ //| #include "UDP_broadcast.h" -#include + /** * @fn void set_owner_details_mac(owner_details_t*) @@ -16,16 +16,13 @@ */ static void set_owner_details_mac(owner_details_t* owner){ - uint32_t uID[3]; + // Access the MAC address - HAL_GetUID(uID); // Read the UID registers - owner->mac_address[0] = (uID[0] >> 0) & 0xFF; - owner->mac_address[1] = (uID[0] >> 8) & 0xFF; - owner->mac_address[2] = (uID[0] >> 16) & 0xFF; - owner->mac_address[3] = (uID[1] >> 0) & 0xFF; - owner->mac_address[4] = (uID[1] >> 8) & 0xFF; - owner->mac_address[5] = (uID[1] >> 16) & 0xFF; + for(int i = 0; i < 6; i++){ + owner->mac_address[i] = netif_default->hwaddr[i]; + } + } /** @@ -37,7 +34,7 @@ static void set_owner_details_mac(owner_details_t* owner){ * @return */ -uint8_t set_owner_details_name(owner_details_t* owner, char* name){ +uint8_t set_owner_details_name(owner_details_t *owner, char *name){ if(name != NULL){ if(owner->name == NULL){ owner->name = (char*)malloc(strlen(name) + 1); @@ -87,7 +84,7 @@ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ * @return */ -static uint8_t set_owner_details_reply(owner_details_t owner, char * reply){ +static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ if(reply != NULL){ if(owner->reply == NULL){ owner->reply = (char*)malloc(strlen(reply) + 1); @@ -123,7 +120,7 @@ static void format_reply(owner_details_t *owner){ owner->mac_address[0], owner->mac_address[1], owner->mac_address[2], owner->mac_address[3], owner->mac_address[4], owner->mac_address[5]); - if (replyBuffer != NULL) { + if (reply_buf != NULL) { snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s", mac_addr_str, owner->surname, owner->name); @@ -145,7 +142,8 @@ static void format_reply(owner_details_t *owner){ * @return */ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ - if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner)){ + if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname)){ + set_owner_details_mac(owner); format_reply(owner); } } @@ -160,11 +158,11 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ char* get_owner_details_name(owner_details_t owner){ char *err_reply = "no name yet"; - if(owner == NULL || owner->name == NULL){ - owner->name = (char*)malloc(strlen(err_reply)); - strcpy(owner->name,err_reply); + if(owner.name == NULL){ + owner.name = (char*)malloc(strlen(err_reply)); + strcpy(owner.name,err_reply); } - return owner->name; + return owner.name; } /** @@ -177,11 +175,11 @@ char* get_owner_details_name(owner_details_t owner){ char* get_owner_details_surname(owner_details_t owner){ char *err_reply = "no surname yet"; - if(owner == NULL || owner->surname == NULL){ - owner->surname = (char*)malloc(strlen(err_reply)); - strcpy(owner->name,err_reply); + if(owner.surname == NULL){ + owner.surname = (char*)malloc(strlen(err_reply)); + strcpy(owner.name,err_reply); } - return owner->name; + return owner.name; } /** @@ -192,13 +190,13 @@ char* get_owner_details_surname(owner_details_t owner){ * @return */ -char* get_owner_details_reply(owner_details_t){ +char* get_owner_details_reply(owner_details_t owner){ char *err_reply = "no reply yet"; - if(owner == NULL || owner->reply == NULL){ - owner->reply = (char*)malloc(strlen(err_reply)); - strcpy(owner->name,err_reply); + if(owner.reply == NULL){ + owner.reply = (char*)malloc(strlen(err_reply)); + strcpy(owner.name,err_reply); } - return owner->name; + return owner.name; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index c5584ec..01bdcbb 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -72,17 +72,7 @@ static void MX_QUADSPI_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -int _write(int file, char *ptr, int len) { - for (int x = 0;x Date: Mon, 6 Nov 2023 23:09:15 +0100 Subject: [PATCH 05/36] strcpy -> strncpy main was before uart init --- project/Core/Src/UDP_broadcast.c | 16 ++++++++-------- project/Core/Src/main.c | 16 +++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index ca65710..f207df4 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -42,7 +42,7 @@ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ else{ owner->name = (char*)realloc(owner->name,strlen(name) + 1); } - strcpy(owner->name,name); + strncpy(owner->name,name,strlen(owner->name)); return 1; } else{ @@ -67,7 +67,7 @@ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ else{ owner->surname = (char*)realloc(owner->surname,strlen(surname) + 1); } - strcpy(owner->surname,surname); + strncpy(owner->surname,surname,strlen(owner->surname)); return 1; } else{ @@ -92,7 +92,7 @@ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ else{ owner->reply = (char*)realloc(owner->reply,strlen(reply) + 1); } - strcpy(owner->reply,reply); + strncpy(owner->reply,reply,strlen(owner->reply)); return 1; } else{ @@ -160,7 +160,7 @@ char* get_owner_details_name(owner_details_t owner){ char *err_reply = "no name yet"; if(owner.name == NULL){ owner.name = (char*)malloc(strlen(err_reply)); - strcpy(owner.name,err_reply); + strncpy(owner.name,err_reply,strlen(owner.name)); } return owner.name; } @@ -177,9 +177,9 @@ char* get_owner_details_surname(owner_details_t owner){ char *err_reply = "no surname yet"; if(owner.surname == NULL){ owner.surname = (char*)malloc(strlen(err_reply)); - strcpy(owner.name,err_reply); + strncpy(owner.surname,err_reply,strlen(owner.surname)); } - return owner.name; + return owner.surname; } /** @@ -194,9 +194,9 @@ char* get_owner_details_reply(owner_details_t owner){ char *err_reply = "no reply yet"; if(owner.reply == NULL){ owner.reply = (char*)malloc(strlen(err_reply)); - strcpy(owner.name,err_reply); + strncpy(owner.reply,err_reply,strlen(owner.reply)); } - return owner.name; + return owner.reply; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 01bdcbb..97fab40 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -82,14 +82,6 @@ static void MX_QUADSPI_Init(void); int main(void) { /* USER CODE BEGIN 1 */ - owner_details_t owner; - printf("hello world"); - LOG_DEBUG("main","helloworld"); - LOG_DEBUG("main","%s",get_owner_details_reply(owner)); - set_owner_details(&owner, "joran", "vn"); - LOG_DEBUG("main","%s",get_owner_details_reply(owner)); - set_owner_details(&owner, "joran", "Van Nieuwenhoven"); - LOG_DEBUG("main","%s",get_owner_details_reply(owner)); /* USER CODE END 1 */ @@ -118,7 +110,13 @@ int main(void) MX_LWIP_Init(); MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ - + owner_details_t owner; + LOG_DEBUG("main1","helloworld"); + LOG_DEBUG("main2","%s",get_owner_details_reply(owner)); + set_owner_details(&owner, "joran", "vn"); + LOG_DEBUG("main3","%s",get_owner_details_reply(owner)); + set_owner_details(&owner, "joran", "Van Nieuwenhoven"); + LOG_DEBUG("main4","%s",get_owner_details_reply(owner)); /* USER CODE END 2 */ /* Infinite loop */ From 566bdb00edab6af42686791bd050f05e58c5dc3f Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 6 Nov 2023 23:57:04 +0100 Subject: [PATCH 06/36] change of struct struct no longer contains allocated arrays problem with the reply string --- project/Core/Inc/UDP_broadcast.h | 14 ++++--- project/Core/Src/UDP_broadcast.c | 65 +++++++++++++------------------- project/Core/Src/main.c | 16 +++++--- 3 files changed, 44 insertions(+), 51 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 5edc19c..7584abc 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -13,20 +13,22 @@ #include #include "lwip/netif.h" #include "lwip.h" +#define LOGGER_LEVEL_ALL +#include "log.h" typedef struct { - char* name; - char* surname; + char name[20]; + char surname[20]; uint8_t mac_address[6]; - char* reply; + char reply[200]; }owner_details_t; uint8_t set_owner_details_name(owner_details_t*, char* ); uint8_t set_owner_details_sirname(owner_details_t*, char* ); 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_name(owner_details_t*); +char* get_owner_details_surname(owner_details_t*); +char* get_owner_details_reply(owner_details_t*); #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index f207df4..413801e 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -36,13 +36,8 @@ static void set_owner_details_mac(owner_details_t* owner){ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ if(name != NULL){ - if(owner->name == NULL){ - owner->name = (char*)malloc(strlen(name) + 1); - } - else{ - owner->name = (char*)realloc(owner->name,strlen(name) + 1); - } - strncpy(owner->name,name,strlen(owner->name)); + LOG_DEBUG("set_owner_details_name","set: %s",name); + strncpy(owner->name,name,sizeof(owner->name)); return 1; } else{ @@ -61,13 +56,8 @@ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ */ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ if(surname != NULL){ - if(owner->surname == NULL){ - owner->surname = (char*)malloc(strlen(surname) + 1); - } - else{ - owner->surname = (char*)realloc(owner->surname,strlen(surname) + 1); - } - strncpy(owner->surname,surname,strlen(owner->surname)); + LOG_DEBUG("set_owner_details_surname","set: %s",surname); + strncpy(owner->surname,surname,sizeof(owner->surname)); return 1; } else{ @@ -86,13 +76,8 @@ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ if(reply != NULL){ - if(owner->reply == NULL){ - owner->reply = (char*)malloc(strlen(reply) + 1); - } - else{ - owner->reply = (char*)realloc(owner->reply,strlen(reply) + 1); - } - strncpy(owner->reply,reply,strlen(owner->reply)); + LOG_DEBUG("set_owner_details_reply","set: %s",reply); + strncpy(owner->reply,reply,sizeof(owner->reply)); return 1; } else{ @@ -145,6 +130,10 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname)){ set_owner_details_mac(owner); format_reply(owner); + return 1; + } + else{ + return 0; } } @@ -156,13 +145,12 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ * @return */ -char* get_owner_details_name(owner_details_t owner){ - char *err_reply = "no name yet"; - if(owner.name == NULL){ - owner.name = (char*)malloc(strlen(err_reply)); - strncpy(owner.name,err_reply,strlen(owner.name)); +char* get_owner_details_name(owner_details_t *owner){ + char err_reply[20] = "no name yet"; + if(owner->name == NULL){ + strncpy(owner->name,err_reply,sizeof(owner->name)); } - return owner.name; + return *owner->name; } /** @@ -173,13 +161,12 @@ char* get_owner_details_name(owner_details_t owner){ * @return */ -char* get_owner_details_surname(owner_details_t owner){ - char *err_reply = "no surname yet"; - if(owner.surname == NULL){ - owner.surname = (char*)malloc(strlen(err_reply)); - strncpy(owner.surname,err_reply,strlen(owner.surname)); +char* get_owner_details_surname(owner_details_t* owner){ + char err_reply[20] = "no surname yet"; + if(owner->surname == NULL){ + strncpy(owner->surname,err_reply,sizeof(owner->surname)); } - return owner.surname; + return owner->surname; } /** @@ -190,13 +177,13 @@ char* get_owner_details_surname(owner_details_t owner){ * @return */ -char* get_owner_details_reply(owner_details_t owner){ - char *err_reply = "no reply yet"; - if(owner.reply == NULL){ - owner.reply = (char*)malloc(strlen(err_reply)); - strncpy(owner.reply,err_reply,strlen(owner.reply)); +char* get_owner_details_reply(owner_details_t *owner){ + LOG_DEBUG("get_owner_details_reply","getting reply"); + char err_reply[20] = "no reply yet"; + if(owner->reply == NULL){ + strncpy(owner->reply,err_reply,sizeof(owner->reply)); } - return owner.reply; + return owner->reply; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 97fab40..f98c628 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,12 +111,16 @@ int main(void) MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ owner_details_t owner; - LOG_DEBUG("main1","helloworld"); - LOG_DEBUG("main2","%s",get_owner_details_reply(owner)); - set_owner_details(&owner, "joran", "vn"); - LOG_DEBUG("main3","%s",get_owner_details_reply(owner)); - set_owner_details(&owner, "joran", "Van Nieuwenhoven"); - LOG_DEBUG("main4","%s",get_owner_details_reply(owner)); + LOG_DEBUG("main1","\nhelloworld"); + LOG_DEBUG("main2","%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "vn")){ + LOG_DEBUG("main3","error");; + } + LOG_DEBUG("main4","%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){ + LOG_DEBUG("main5","error"); + } + LOG_DEBUG("main6","%s",get_owner_details_reply(&owner)); /* USER CODE END 2 */ /* Infinite loop */ From 00678a938272b1409d537cf4550a87ea233c8a4e Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 6 Nov 2023 23:59:21 +0100 Subject: [PATCH 07/36] Update UDP_broadcast.c problem resolved --- project/Core/Src/UDP_broadcast.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 413801e..70b29e3 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -97,7 +97,7 @@ static void format_reply(owner_details_t *owner){ char mac_addr_str[18]; char* reply_buf = NULL; if (owner != NULL) { - reply_len = 20 + strlen(mac_addr_str) + strlen(owner->surname) + strlen(owner->name); + reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name); reply_buf = (char*)malloc(reply_len); From 4b188b1ff02d80190b1d100f275c2db1ef6ac928 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Tue, 7 Nov 2023 12:37:21 +0100 Subject: [PATCH 08/36] changes to LOG_DEBUG tags changed logger level to info for now in the UDP_broadcast header file , debug not needed fixed some tags --- project/Core/Inc/UDP_broadcast.h | 2 +- project/Core/Src/UDP_broadcast.c | 9 +++++---- project/Core/Src/main.c | 12 ++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 7584abc..8b24ff6 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -13,7 +13,7 @@ #include #include "lwip/netif.h" #include "lwip.h" -#define LOGGER_LEVEL_ALL +#define LOGGER_LEVEL_INFO #include "log.h" typedef struct { char name[20]; diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 70b29e3..18b4ec9 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -7,6 +7,7 @@ //| #include "UDP_broadcast.h" +static const char *TAG = "UDP_broadcast"; /** * @fn void set_owner_details_mac(owner_details_t*) @@ -36,7 +37,7 @@ static void set_owner_details_mac(owner_details_t* owner){ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ if(name != NULL){ - LOG_DEBUG("set_owner_details_name","set: %s",name); + LOG_DEBUG(TAG,"set: %s",name); strncpy(owner->name,name,sizeof(owner->name)); return 1; } @@ -56,7 +57,7 @@ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ */ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ if(surname != NULL){ - LOG_DEBUG("set_owner_details_surname","set: %s",surname); + LOG_DEBUG(TAG,"set: %s",surname); strncpy(owner->surname,surname,sizeof(owner->surname)); return 1; } @@ -76,7 +77,7 @@ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ if(reply != NULL){ - LOG_DEBUG("set_owner_details_reply","set: %s",reply); + LOG_DEBUG(TAG,"set: %s",reply); strncpy(owner->reply,reply,sizeof(owner->reply)); return 1; } @@ -178,7 +179,7 @@ char* get_owner_details_surname(owner_details_t* owner){ */ char* get_owner_details_reply(owner_details_t *owner){ - LOG_DEBUG("get_owner_details_reply","getting reply"); + LOG_DEBUG(TAG,"getting reply"); char err_reply[20] = "no reply yet"; if(owner->reply == NULL){ strncpy(owner->reply,err_reply,sizeof(owner->reply)); diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index f98c628..959aedd 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,16 +111,16 @@ int main(void) MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ owner_details_t owner; - LOG_DEBUG("main1","\nhelloworld"); - LOG_DEBUG("main2","%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"\nhelloworld"); + LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); if(!set_owner_details(&owner, "joran", "vn")){ - LOG_DEBUG("main3","error");; + LOG_DEBUG(TAG,"error");; } - LOG_DEBUG("main4","%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){ - LOG_DEBUG("main5","error"); + LOG_DEBUG(TAG,"error"); } - LOG_DEBUG("main6","%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); /* USER CODE END 2 */ /* Infinite loop */ From 142aad264c428d408aeecbcc9659bc76ac11b659 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:09:38 +0100 Subject: [PATCH 09/36] err_handler added an error handler for when a pointer is NULL got rid of malloc in format reply --- project/Core/Inc/UDP_broadcast.h | 18 +++-- project/Core/Src/UDP_broadcast.c | 118 +++++++++++++++++++++---------- project/Core/Src/main.c | 29 +++++++- 3 files changed, 119 insertions(+), 46 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 8b24ff6..07abd29 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -13,17 +13,27 @@ #include #include "lwip/netif.h" #include "lwip.h" -#define LOGGER_LEVEL_INFO +#define LOGGER_LEVEL_ALL #include "log.h" + +#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" + + typedef struct { char name[20]; char surname[20]; uint8_t mac_address[6]; - char reply[200]; + char reply[100]; }owner_details_t; -uint8_t set_owner_details_name(owner_details_t*, char* ); -uint8_t set_owner_details_sirname(owner_details_t*, char* ); uint8_t set_owner_details(owner_details_t*, char* , char*); char* get_owner_details_name(owner_details_t*); diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 18b4ec9..8ae2aa1 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -10,19 +10,47 @@ static const char *TAG = "UDP_broadcast"; /** - * @fn void set_owner_details_mac(owner_details_t*) + * @fn void owner_details_error_handler(owner_details_t*, char*, char*) * @brief * * @param owner + * @param word + * @param funct */ -static void set_owner_details_mac(owner_details_t* owner){ - // Access the MAC address - - - for(int i = 0; i < 6; i++){ - owner->mac_address[i] = netif_default->hwaddr[i]; +static void owner_details_error_handler(owner_details_t* owner, char* funct,char* word){ + if(owner == NULL && word == NULL){ + LOG_WARN(TAG,"%s: owner and string given are both NULL pointers",funct); } + else if(owner == NULL){ + LOG_WARN(TAG,"%s: owner given is a NULL pointer",funct); + } + else{ + LOG_WARN(TAG,"%s: string given is a NULL pointer",funct); + } +} + +/** + * @fn uint8_t set_owner_details_mac(owner_details_t*) + * @brief + * + * @param owner + * @return + */ + +static uint8_t set_owner_details_mac(owner_details_t* owner){ + // Access the MAC address + if(owner != NULL){ + for(int i = 0; i < 6; i++){ + owner->mac_address[i] = netif_default->hwaddr[i]; + } + return 1; + } + else{ + owner_details_error_handler(owner, sod_mac,""); + return 0; + } + } @@ -35,13 +63,14 @@ static void set_owner_details_mac(owner_details_t* owner){ * @return */ -uint8_t set_owner_details_name(owner_details_t *owner, char *name){ - if(name != NULL){ +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{ + owner_details_error_handler(owner, sod_name, name); return 0; } @@ -55,13 +84,14 @@ uint8_t set_owner_details_name(owner_details_t *owner, char *name){ * @param surname * @return */ -uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ - if(surname != 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{ + owner_details_error_handler(owner, sod_surname, surname); return 0; } } @@ -76,45 +106,46 @@ uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ */ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ - if(reply != NULL){ + if(reply != NULL && owner != NULL){ LOG_DEBUG(TAG,"set: %s",reply); strncpy(owner->reply,reply,sizeof(owner->reply)); return 1; } else{ + owner_details_error_handler(owner, sod_reply, reply); return 0; } } /** - * @fn void format_reply(owner_details_t*) + * @fn uint8_t format_reply(owner_details_t*) * @brief * * @param owner + * @return */ -static void format_reply(owner_details_t *owner){ +static uint8_t format_reply(owner_details_t *owner){ size_t reply_len = 0; char mac_addr_str[18]; - char* reply_buf = NULL; + char reply_buf[100]; if (owner != NULL) { reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name); - reply_buf = (char*)malloc(reply_len); 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]); - if (reply_buf != NULL) { - 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); - - free(reply_buf); // Free the temporary buffer - } + 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{ + owner_details_error_handler(owner, f_reply,""); + return 0; } } @@ -128,14 +159,24 @@ static void format_reply(owner_details_t *owner){ * @return */ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ - if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname)){ - set_owner_details_mac(owner); - format_reply(owner); - return 1; + 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{ + owner_details_error_handler(owner, f_reply,""); return 0; } + } /** @@ -147,11 +188,11 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ */ char* get_owner_details_name(owner_details_t *owner){ - char err_reply[20] = "no name yet"; - if(owner->name == NULL){ - strncpy(owner->name,err_reply,sizeof(owner->name)); + if(owner == NULL || owner->name == NULL){ + owner_details_error_handler(owner, god_name,""); + return "|no name yet|"; } - return *owner->name; + return owner->name; } /** @@ -163,9 +204,9 @@ char* get_owner_details_name(owner_details_t *owner){ */ char* get_owner_details_surname(owner_details_t* owner){ - char err_reply[20] = "no surname yet"; - if(owner->surname == NULL){ - strncpy(owner->surname,err_reply,sizeof(owner->surname)); + if(owner == NULL || owner->surname == NULL){ + owner_details_error_handler(owner, god_surname,""); + return "|no surname yet|"; } return owner->surname; } @@ -179,10 +220,9 @@ char* get_owner_details_surname(owner_details_t* owner){ */ char* get_owner_details_reply(owner_details_t *owner){ - LOG_DEBUG(TAG,"getting reply"); - char err_reply[20] = "no reply yet"; - if(owner->reply == NULL){ - strncpy(owner->reply,err_reply,sizeof(owner->reply)); + if(owner == NULL || owner->reply == NULL){ + owner_details_error_handler(owner, god_reply,""); + return "|no reply formatted yet|"; } return owner->reply; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 959aedd..4565b6e 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,16 +111,39 @@ int main(void) MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ owner_details_t owner; + owner_details_t* owner_error = NULL; + char* test_error = NULL; LOG_DEBUG(TAG,"\nhelloworld"); - LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"owner1:%s",get_owner_details_reply(&owner)); if(!set_owner_details(&owner, "joran", "vn")){ LOG_DEBUG(TAG,"error");; } - LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"owner2:%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){ LOG_DEBUG(TAG,"error"); } - LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"owner3:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(&owner, "joran", "")){ + LOG_DEBUG(TAG,"error"); + } + LOG_DEBUG(TAG,"owner4:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(&owner, "joran", test_error)){ + LOG_DEBUG(TAG,"error setting owner details"); + } + LOG_DEBUG(TAG,"owner5:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(&owner, test_error, "Van Nieuwenhoven")){ + LOG_DEBUG(TAG,"error setting owner details"); + } + LOG_DEBUG(TAG,"owner6:%s",get_owner_details_reply(&owner)); + + if(!set_owner_details(owner_error, test_error, "Van Nieuwenhoven")){ + LOG_DEBUG(TAG,"error setting owner details"); + } + LOG_DEBUG(TAG,"owner7:%s",get_owner_details_reply(owner_error)); /* USER CODE END 2 */ /* Infinite loop */ From bdf445b708c8556c0006b0391cfc6b08cab55b49 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:37:16 +0100 Subject: [PATCH 10/36] added some UDP server code initialising UDP server succesfull, callback running have not yet tried to receive a broadcast --- project/Core/Inc/UDP_broadcast.h | 5 +++ project/Core/Src/UDP_broadcast.c | 64 ++++++++++++++++++++++++++++++++ project/Core/Src/main.c | 34 ++--------------- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 07abd29..65ac3be 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -40,5 +40,10 @@ char* get_owner_details_name(owner_details_t*); char* get_owner_details_surname(owner_details_t*); char* get_owner_details_reply(owner_details_t*); +err_t init_UDP_server(); + + + + #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 8ae2aa1..3f75cbe 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -227,4 +227,68 @@ char* get_owner_details_reply(owner_details_t *owner){ return owner->reply; } +/** + * @fn void udp_receive_callback(void*, struct udp_pcb*, struct pbuf*, const ip_addr_t*, u16_t) + * @brief + * + * @param arg + * @param upcb + * @param p + * @param addr + * @param port + */ + +static void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){ + int i; + int len; + char *pc; + char data[1024]; + char source_ip_str[16]; + + // Convert the source IP address to a string for printing. + ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); + + if (p != NULL) { + pc = (char*)p->payload; + len = p->tot_len; + + for(i = 0; i < len; i++) { + data[i] = pc[i]; + } + pbuf_free(p); + + LOG_DEBUG(TAG,"received data from %s at port: %d: %s",source_ip_str,port,data); + } +} + +/** + * @fn err_t init_UDP_server() + * @brief + * + * @return + */ + +err_t init_UDP_server(){ + 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; +} + + diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 4565b6e..99f6437 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -25,6 +25,7 @@ #define LOGGER_LEVEL_ALL #include "log.h" #include "UDP_broadcast.h" +#include "udp.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -110,40 +111,13 @@ int main(void) MX_LWIP_Init(); MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ + init_UDP_server(); owner_details_t owner; - owner_details_t* owner_error = NULL; - char* test_error = NULL; - LOG_DEBUG(TAG,"\nhelloworld"); - LOG_DEBUG(TAG,"owner1:%s",get_owner_details_reply(&owner)); + if(!set_owner_details(&owner, "joran", "vn")){ LOG_DEBUG(TAG,"error");; } - LOG_DEBUG(TAG,"owner2:%s",get_owner_details_reply(&owner)); - - if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){ - LOG_DEBUG(TAG,"error"); - } - LOG_DEBUG(TAG,"owner3:%s",get_owner_details_reply(&owner)); - - if(!set_owner_details(&owner, "joran", "")){ - LOG_DEBUG(TAG,"error"); - } - LOG_DEBUG(TAG,"owner4:%s",get_owner_details_reply(&owner)); - - if(!set_owner_details(&owner, "joran", test_error)){ - LOG_DEBUG(TAG,"error setting owner details"); - } - LOG_DEBUG(TAG,"owner5:%s",get_owner_details_reply(&owner)); - - if(!set_owner_details(&owner, test_error, "Van Nieuwenhoven")){ - LOG_DEBUG(TAG,"error setting owner details"); - } - LOG_DEBUG(TAG,"owner6:%s",get_owner_details_reply(&owner)); - - if(!set_owner_details(owner_error, test_error, "Van Nieuwenhoven")){ - LOG_DEBUG(TAG,"error setting owner details"); - } - LOG_DEBUG(TAG,"owner7:%s",get_owner_details_reply(owner_error)); + LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); /* USER CODE END 2 */ /* Infinite loop */ From 5e464fd5fd46a03c84949836329ae78b3e47b781 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:42:15 +0100 Subject: [PATCH 11/36] added to the udp server code request is received well, no reply seen in the Qt application, could be because i had to correct code to this version of Qt creator. Waiting on validation of correct changes from Wim Dams --- project/Core/Src/UDP_broadcast.c | 47 ++++++++++++++++++++++++++------ project/Core/Src/main.c | 2 +- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 3f75cbe..5c99a12 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -8,6 +8,7 @@ #include "UDP_broadcast.h" static const char *TAG = "UDP_broadcast"; +static char reply_str[100] = "|no reply formatted yet|"; /** * @fn void owner_details_error_handler(owner_details_t*, char*, char*) @@ -109,6 +110,7 @@ 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{ @@ -238,12 +240,21 @@ char* get_owner_details_reply(owner_details_t *owner){ * @param port */ -static void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){ +#define MAX_DATA_SIZE 50 // Define the maximum expected data size +#define UDP_QUESTION1 "Where are you?v1.0" + +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[1024]; + char data[MAX_DATA_SIZE]; char source_ip_str[16]; + struct pbuf *p_data; + + memset(data, 0, sizeof(data)); + + + // Convert the source IP address to a string for printing. ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); @@ -251,14 +262,34 @@ static void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p if (p != NULL) { pc = (char*)p->payload; len = p->tot_len; - - for(i = 0; i < len; i++) { - data[i] = pc[i]; + p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM); + if (p_data == NULL){ + LOG_WARN(TAG,"udp_receive_callback: unable to allocated data buffer for reply"); + } + else if(len <= MAX_DATA_SIZE){ + for(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){ + *((uint32_t*)p_data->payload) = reply_str; + p_data->len = sizeof(reply_str); + p_data->tot_len = sizeof(reply_str); + udp_sendto(connection, p_data, addr, port); + LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str); + } + + } + else{ + LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); } - pbuf_free(p); - LOG_DEBUG(TAG,"received data from %s at port: %d: %s",source_ip_str,port,data); } + else{ + LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer"); + } + pbuf_free(p); } /** @@ -276,7 +307,7 @@ err_t init_UDP_server(){ if(connection != NULL){ err = udp_bind(connection, IP_ANY_TYPE, 64000); if(err == ERR_OK){ - udp_recv(connection, udp_receive_callback, NULL); + udp_recv(connection, udp_receive_callback,NULL); LOG_INFO(TAG,"initialising UDP server succesfull, callback running"); } else{ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 99f6437..48d20c3 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,8 +111,8 @@ int main(void) MX_LWIP_Init(); MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ - init_UDP_server(); owner_details_t owner; + init_UDP_server(); if(!set_owner_details(&owner, "joran", "vn")){ LOG_DEBUG(TAG,"error");; From e47772ebf8d664b88a4a9852f2bec3f3212655cb Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Wed, 8 Nov 2023 22:47:25 +0100 Subject: [PATCH 12/36] Update UDP_broadcast.c --- project/Core/Src/UDP_broadcast.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 5c99a12..b57a60b 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -252,10 +252,6 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p struct pbuf *p_data; memset(data, 0, sizeof(data)); - - - - // Convert the source IP address to a string for printing. ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); @@ -290,6 +286,7 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer"); } pbuf_free(p); + pbuf_free(p_data); } /** From 123653cc037038a3011bec4f37067677104fe07c Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Thu, 9 Nov 2023 19:36:13 +0100 Subject: [PATCH 13/36] adding comments --- project/Core/Inc/UDP_broadcast.h | 35 +++++--- project/Core/Src/UDP_broadcast.c | 150 ++++++++++++++++++------------- 2 files changed, 110 insertions(+), 75 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 65ac3be..bfbe66d 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -8,6 +8,7 @@ #ifndef INC_UDP_BROADCAST_H_ #define INC_UDP_BROADCAST_H_ +// includes #include #include #include @@ -16,16 +17,26 @@ #define LOGGER_LEVEL_ALL #include "log.h" -#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" +// Defines used by owner details error handler +#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" +// Defines used by UDP callback +#define MAX_DATA_SIZE 50 // Define the maximum expected data size +#define UDP_QUESTION1 "Where are you?v1.0" + +/** + * @struct owner_details_t + * @brief contains information about the owner + * + */ typedef struct { char name[20]; @@ -34,16 +45,14 @@ typedef struct { char reply[100]; }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*); char* get_owner_details_name(owner_details_t*); char* get_owner_details_surname(owner_details_t*); char* get_owner_details_reply(owner_details_t*); +// The following functions are used for UDP (those that must be available in main) err_t init_UDP_server(); - - - - #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index b57a60b..fcc699e 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -5,18 +5,23 @@ * Author: joran */ //| + +// Includes #include "UDP_broadcast.h" -static const char *TAG = "UDP_broadcast"; -static char reply_str[100] = "|no reply formatted yet|"; +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 /** * @fn void owner_details_error_handler(owner_details_t*, char*, char*) - * @brief + * @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 * - * @param owner - * @param word - * @param funct + * + * @param owner owner_details_t structure, it contains information about the owner + * @param word string parameter that was used in the function that triggered this handler + * @param funct name of the function where the error occured */ static void owner_details_error_handler(owner_details_t* owner, char* funct,char* word){ @@ -33,22 +38,25 @@ static void owner_details_error_handler(owner_details_t* owner, char* funct,char /** * @fn uint8_t set_owner_details_mac(owner_details_t*) - * @brief + * @brief set_owner_details_mac() gets the MAC address from the default netif + * and sets it in the owner_details_t struct * - * @param owner - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @return setting owner mac address error + * - 1: no error occured, mac address was set + * - 0: an error occured, owner pointer is NULL */ static uint8_t set_owner_details_mac(owner_details_t* owner){ - // Access the MAC address + if(owner != NULL){ for(int i = 0; i < 6; i++){ - owner->mac_address[i] = netif_default->hwaddr[i]; + owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address } return 1; } else{ - owner_details_error_handler(owner, sod_mac,""); + owner_details_error_handler(owner, SOD_MAC,""); return 0; } @@ -57,11 +65,13 @@ static uint8_t set_owner_details_mac(owner_details_t* owner){ /** * @fn uint8_t set_owner_details_name(owner_details_t*, char*) - * @brief + * @brief set_owner_details_name() sets the owner's name in the owner_details_t struct * - * @param owner - * @param name - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @param name string containing the owner's name + * @return setting owner name error + * - 1: no error occured, name was set + * - 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){ @@ -71,7 +81,7 @@ static uint8_t set_owner_details_name(owner_details_t *owner, char *name){ return 1; } else{ - owner_details_error_handler(owner, sod_name, name); + owner_details_error_handler(owner, SOD_NAME, name); return 0; } @@ -79,11 +89,13 @@ static uint8_t set_owner_details_name(owner_details_t *owner, char *name){ /** * @fn uint8_t set_owner_details_surname(owner_details_t*, char*) - * @brief + * @brief set_owner_details_surname() sets the owner's surname in the owner_details_t struct * - * @param owner - * @param surname - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @param surname string containing the owner's surname + * @return setting owner surname error + * - 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){ @@ -92,18 +104,20 @@ static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){ return 1; } else{ - owner_details_error_handler(owner, sod_surname, surname); + owner_details_error_handler(owner, SOD_SURNAME, surname); return 0; } } /** * @fn uint8_t set_owner_details_reply(owner_details_t, char*) - * @brief + * @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct * - * @param owner - * @param reply - * @return + * @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){ @@ -114,17 +128,19 @@ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ return 1; } else{ - owner_details_error_handler(owner, sod_reply, reply); + owner_details_error_handler(owner, SOD_REPLY, reply); return 0; } } /** * @fn uint8_t format_reply(owner_details_t*) - * @brief + * @brief format_reply() formats all the owner's details into a string * - * @param owner - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @return formatting reply error + * - 1: no error occured, reply was formatted + * - 0: an error occured, owner pointer is NULL */ static uint8_t format_reply(owner_details_t *owner){ @@ -146,19 +162,22 @@ static uint8_t format_reply(owner_details_t *owner){ return 1; } else{ - owner_details_error_handler(owner, f_reply,""); + owner_details_error_handler(owner, F_REPLY,""); return 0; } } /** * @fn uint8_t set_owner_details(owner_details_t*, char*, char*) - * @brief + * @brief set_owner_details() is the interface that can be used in other files + * to set the owner's details * - * @param owner - * @param name - * @param surname - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @param name string containing the new owner's name + * @param 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 or owner pointer is NULL */ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ if(owner != NULL){ @@ -175,7 +194,7 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ } } else{ - owner_details_error_handler(owner, f_reply,""); + owner_details_error_handler(owner, SOD,""); return 0; } @@ -183,15 +202,16 @@ uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){ /** * @fn char get_owner_details_name*(owner_details_t) - * @brief + * @brief get_owner_details_name() can be used to get the current owner's name * - * @param owner - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @return name of owner + * 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,""); + owner_details_error_handler(owner, GOD_NAME,""); return "|no name yet|"; } return owner->name; @@ -199,15 +219,16 @@ char* get_owner_details_name(owner_details_t *owner){ /** * @fn char get_owner_details_surname*(owner_details_t) - * @brief + * @brief get_owner_details_surname() can be used to get the current owner's surname * - * @param owner - * @return + * @param owner owner_details_t structure, it contains information about the owner + * @return surname of 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,""); + owner_details_error_handler(owner, GOD_SURNAME,""); return "|no surname yet|"; } return owner->surname; @@ -215,34 +236,35 @@ char* get_owner_details_surname(owner_details_t* owner){ /** * @fn char get_owner_details_reply*(owner_details_t) - * @brief + * @brief get_owner_details_reply() can be used to get the current UDP reply * - * @param - * @return + * @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,""); + owner_details_error_handler(owner, GOD_REPLY,""); return "|no reply formatted yet|"; } return owner->reply; } + /** * @fn void udp_receive_callback(void*, struct udp_pcb*, struct pbuf*, const ip_addr_t*, u16_t) - * @brief + * @brief udp_receive_callback() callback function for when a UDP packet has been received. + * it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string + * @see reply_str back to the client * - * @param arg - * @param upcb - * @param p - * @param addr - * @param port + * @param arg a pointer to some user-defined data or context + * @param connection UDP PCB to be bound with a local address ipaddr and port. + * @param p packet buffern it holds the incoming UDP packet data, including its payload and length + * @param addr ip_addr_t structure that contains the IP address of the sender of the UDP packet + * @param port the source port number of the sender's UDP packet */ -#define MAX_DATA_SIZE 50 // Define the maximum expected data size -#define UDP_QUESTION1 "Where are you?v1.0" - 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; @@ -252,8 +274,8 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p struct pbuf *p_data; memset(data, 0, sizeof(data)); - // Convert the source IP address to a string for printing. - ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); + + 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; @@ -291,9 +313,13 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p /** * @fn err_t init_UDP_server() - * @brief + * @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on + * port 6400 * - * @return + * @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 init_UDP_server(){ From 984c573ae62423a4c0f4f14bfba78ee4508b01b1 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 13:43:15 +0100 Subject: [PATCH 14/36] changes to udp code now working as intended --- project/Core/Inc/UDP_broadcast.h | 1 + project/Core/Src/UDP_broadcast.c | 7 ++++--- project/Core/Src/main.c | 3 +-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index bfbe66d..30cb820 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -16,6 +16,7 @@ #include "lwip.h" #define LOGGER_LEVEL_ALL #include "log.h" +#include "udp.h" // Defines used by owner details error handler #define SOD_NAME "set_owner_details_name" diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index fcc699e..9009fbe 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -282,7 +282,7 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p len = p->tot_len; p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM); if (p_data == NULL){ - LOG_WARN(TAG,"udp_receive_callback: unable to allocated data buffer for reply"); + 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++) { @@ -291,10 +291,11 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p 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){ - *((uint32_t*)p_data->payload) = reply_str; + p_data->payload = reply_str; p_data->len = sizeof(reply_str); p_data->tot_len = sizeof(reply_str); - udp_sendto(connection, p_data, addr, port); + 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); } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 48d20c3..ae71e4c 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -25,7 +25,6 @@ #define LOGGER_LEVEL_ALL #include "log.h" #include "UDP_broadcast.h" -#include "udp.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -114,7 +113,7 @@ int main(void) owner_details_t owner; init_UDP_server(); - if(!set_owner_details(&owner, "joran", "vn")){ + if(!set_owner_details(&owner, "Joran", "Van Nieuwenhoven")){ LOG_DEBUG(TAG,"error");; } LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner)); From a3907c36d9c04853258c503b61e768ad7029cd23 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 15:09:21 +0100 Subject: [PATCH 15/36] cleaning --- project/Core/Inc/UDP_broadcast.h | 5 +- project/Core/Src/UDP_broadcast.c | 136 ++++++++++++------------------- 2 files changed, 55 insertions(+), 86 deletions(-) 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{ From 1360b0cd112b11d086327adb4c77b3f49d63d749 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 16:25:21 +0100 Subject: [PATCH 16/36] style guide part 1 --- project/Core/Inc/UDP_broadcast.h | 10 +-- project/Core/Src/UDP_broadcast.c | 121 +++++++++++++++++-------------- project/Core/Src/main.c | 6 +- 3 files changed, 74 insertions(+), 63 deletions(-) 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 */ From 5609c2ccb9cf8679674c6733da49d76ab26356b3 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:12:43 +0100 Subject: [PATCH 17/36] style guide part 2 --- project/Core/Inc/UDP_broadcast.h | 53 ++++++++++++++++++++++++++++++-- project/Core/Src/UDP_broadcast.c | 50 +++++++++++++++--------------- 2 files changed, 77 insertions(+), 26 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 97f24e6..1aa339e 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -30,8 +30,8 @@ #define F_REPLY "format_reply" // Defines used by UDP callback -#define MAX_DATA_SIZE 30 // Define the maximum expected data size -#define UDP_QUESTION1 "Where are you?v1.0" +#define MAX_DATA_SIZE 30 // Define the maximum expected data size +#define UDP_QUESTION1 "Where are you?v1.0" // Expected request from UDP client /** * @struct owner_details_t @@ -46,13 +46,62 @@ typedef struct { }owner_details_t; // 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 + * to set the owner's details + * + * @param owner owner_details_t structure, it contains information about the owner + * @param name string containing the new owner's name + * @param 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 or owner pointer is NULL + */ uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char* , const char*); +/** + * @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 + * @return name of owner + * this name is set by @see udp_broadcast_set_owner_details_name() + */ char* udp_broadcast_get_owner_details_name(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 + * @return surname of owner + * this name is set by @see udp_broadcast_set_owner_details_surname() + */ char* udp_broadcast_get_owner_details_surname(owner_details_t*); + +/** + * @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* udp_broadcast_get_owner_details_reply(); // The following functions are used for UDP (those that must be available in main) + +/** + * @fn err_t udp_broadcast_init() + * @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on + * port 6400 + * + * @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(); #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index b9d6dfa..9d45b64 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -14,7 +14,7 @@ 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 +// Functions 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); @@ -23,9 +23,6 @@ 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 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 @@ -41,11 +38,9 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p 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{ + }else{ LOG_WARN(TAG,"%s: string given is a NULL pointer",funct); } } @@ -75,6 +70,8 @@ static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){ /** * @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 + * 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 * * @param owner owner_details_t structure, it contains information about the owner * @param name string containing the owner's name @@ -96,6 +93,8 @@ static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, cons /** * @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 + * 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 surname * * @param owner owner_details_t structure, it contains information about the owner * @param surname string containing the owner's surname @@ -116,6 +115,9 @@ static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, c /** * @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 + * if the pointer given is NULL it calls the error handler + * strncpy is used to copy the function paremeter safely to a global variable + * the reason this one is global is so that the udp_callback function can access it easily * * @param reply string used to reply to the UDP broadcast * @return setting owner reply error @@ -136,6 +138,9 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ /** * @fn uint8_t udp_broadcast_format_reply(const owner_details_t*) * @brief format_reply() formats all the owner's details into a string + * if the pointer given is NULL it calls the error handler + * it formats a string using the owner's details using snprintf + * it sets this reply with @see udp_broadcast_set_owner_details_reply() * * @param owner owner_details_t structure, it contains information about the owner * @return formatting reply error @@ -169,6 +174,8 @@ static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){ * @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 + * if the pointer given is NULL it calls the error handler, + * the other pointers get checked by the functions that are called in this function * * @param owner owner_details_t structure, it contains information about the owner * @param name string containing the new owner's name @@ -181,8 +188,7 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name if (owner == NULL){ udp_broadcast_owner_details_error_handler(owner, SOD,""); return 0; - } - 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)){ + }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; @@ -195,7 +201,7 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name * * @param owner owner_details_t structure, it contains information about the owner * @return name of owner - * this name is set by @see set_owner_details_name() + * this name is set by @see udp_broadcast_set_owner_details_name() */ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){ @@ -212,7 +218,7 @@ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){ * * @param owner owner_details_t structure, it contains information about the owner * @return surname of owner - * this name is set by @see set_owner_details_surname() + * this name is set by @see udp_broadcast_set_owner_details_surname() */ char* udp_broadcast_get_owner_details_surname(owner_details_t* owner){ @@ -239,8 +245,8 @@ char* udp_broadcast_get_owner_details_reply(){ /** * @fn void udp_receive_callback(void*, struct udp_pcb*, struct pbuf*, const ip_addr_t*, u16_t) * @brief udp_receive_callback() callback function for when a UDP packet has been received. - * it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string - * @see reply_str back to the client + * it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string, + * @see reply_str, back to the client * * @param arg a pointer to some user-defined data or context * @param connection UDP PCB to be bound with a local address ipaddr and port. @@ -266,8 +272,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), 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){ + }else if (len <= MAX_DATA_SIZE){ for (size_t i = 0; i < len; i++) { data[i] = pc[i]; } @@ -283,13 +288,11 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str); } - } - else{ + }else{ LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); } - } - else{ + }else{ LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer"); } pbuf_free(p); @@ -300,6 +303,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p * @fn err_t udp_broadcast_init() * @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on * port 6400 + * it makes a udp_pcb, binds it to port 64000 and initializes the callback function for when data is received * * @return lwIP error code. * - ERR_OK. Successful. No error occurred. @@ -317,13 +321,11 @@ err_t udp_broadcast_init(){ if (err == ERR_OK){ udp_recv(connection, udp_receive_callback,NULL); LOG_INFO(TAG,"initialising UDP server succesfull, callback running"); - } - else{ + }else{ udp_remove(connection); LOG_WARN(TAG,"initialising UDP server failed, err not ok"); } - } - else{ + }else{ LOG_WARN(TAG,"initialising UDP server failed, connection is null"); } return err; From e03e757e018882a3ae2b83ba016ba20d835f4e11 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:19:56 +0100 Subject: [PATCH 18/36] Update main.c --- project/Core/Src/main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 4b6725c..8bec33e 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -126,6 +126,14 @@ int main(void) /* Initialize the filesystem */ llfs_init(); + // Initialize the UDP broadcast service + owner_details_t owner; + udp_broadcast_init(); + + if(!udp_broadcast_set_owner_details(&owner, "Joran", "Van Nieuwenhoven")){ + LOG_WARN(TAG,"error setting owner's details"); + } + LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply(&owner)); /* USER CODE END 2 */ /* Infinite loop */ From 4a4a35848d1a5bc24d0f8fb27ce65c68f040271d Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 19:31:56 +0100 Subject: [PATCH 19/36] style guide part 3 --- project/Core/Inc/UDP_broadcast.h | 5 ++-- project/Core/Src/UDP_broadcast.c | 50 +++++++++++++++++--------------- project/Core/Src/main.c | 1 + 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 1aa339e..929004a 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -1,6 +1,7 @@ -/* - * UDP_broadcast.h +/** + * @file UDP_broadcast.h * + * @brief UDP broadcast handler * Created on: Nov 6, 2023 * Author: joran */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 9d45b64..a0a056c 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -1,10 +1,11 @@ -/* - * UDP_broadcast.c +/** + * @file UDP_broadcast.c * + * @brief UDP broadcast handler * Created on: Nov 6, 2023 * Author: joran */ -//| + //| // Includes #include "UDP_broadcast.h" @@ -30,9 +31,9 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p * and logs a warning depending on that * * - * @param owner owner_details_t structure, it contains information about the owner - * @param word string parameter that was used in the function that triggered this handler - * @param funct name of the function where the error occured + * @param[in] owner owner_details_t structure, it contains information about the owner + * @param[in] word string parameter that was used in the function that triggered this handler + * @param[in] funct name of the function where the error occured */ static void udp_broadcast_owner_details_error_handler(const owner_details_t* owner, const char* funct, const char* word){ @@ -50,7 +51,7 @@ static void udp_broadcast_owner_details_error_handler(const owner_details_t* own * @brief set_owner_details_mac() gets the MAC address from the default netif * and sets it in the owner_details_t struct * - * @param owner owner_details_t structure, it contains information about the owner + * @param[out] owner owner_details_t structure, it contains information about the owner * @return setting owner mac address error * - 1: no error occured, mac address was set * - 0: an error occured, owner pointer is NULL @@ -73,8 +74,8 @@ static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){ * 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 * - * @param owner owner_details_t structure, it contains information about the owner - * @param name string containing the 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 * @return setting owner name error * - 1: no error occured, name was set * - 0: an error occured, name pointer is NULL or owner pointer is NULL @@ -96,8 +97,8 @@ static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, cons * 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 surname * - * @param owner owner_details_t structure, it contains information about the owner - * @param surname string containing the owner's surname + * @param[out] owner owner_details_t structure, it contains information about the owner + * @param[in] surname string containing the owner's surname * @return setting owner surname error * - 1: no error occured, surname was set * - 0: an error occured, surname pointer is NULL or owner pointer is NULL @@ -119,7 +120,7 @@ static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, c * strncpy is used to copy the function paremeter safely to a global variable * the reason this one is global is so that the udp_callback function can access it easily * - * @param reply string used to reply to the UDP broadcast + * @param[in] 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 @@ -142,7 +143,7 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ * it formats a string using the owner's details using snprintf * it sets this reply with @see udp_broadcast_set_owner_details_reply() * - * @param owner owner_details_t structure, it contains information about the owner + * @param[in] owner owner_details_t structure, it contains information about the owner * @return formatting reply error * - 1: no error occured, reply was formatted * - 0: an error occured, owner pointer is NULL @@ -177,9 +178,9 @@ static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){ * if the pointer given is NULL it calls the error handler, * the other pointers get checked by the functions that are called in this function * - * @param owner owner_details_t structure, it contains information about the owner - * @param name string containing the new owner's name - * @param surname string containing the new owner's surname + * @param[out] owner owner_details_t structure, it contains information about the owner + * @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 or owner pointer is NULL @@ -188,7 +189,8 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name if (owner == NULL){ udp_broadcast_owner_details_error_handler(owner, SOD,""); return 0; - }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)){ + }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; @@ -199,7 +201,7 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name * @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 + * @param[out] owner owner_details_t structure, it contains information about the owner * @return name of owner * this name is set by @see udp_broadcast_set_owner_details_name() */ @@ -216,7 +218,7 @@ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){ * @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 + * @param[out] owner owner_details_t structure, it contains information about the owner * @return surname of owner * this name is set by @see udp_broadcast_set_owner_details_surname() */ @@ -248,11 +250,11 @@ char* udp_broadcast_get_owner_details_reply(){ * it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string, * @see reply_str, back to the client * - * @param arg a pointer to some user-defined data or context - * @param connection UDP PCB to be bound with a local address ipaddr and port. - * @param p packet buffern it holds the incoming UDP packet data, including its payload and length - * @param addr ip_addr_t structure that contains the IP address of the sender of the UDP packet - * @param port the source port number of the sender's UDP packet + * @param[in] arg a pointer to some user-defined data or context + * @param[in] connection UDP PCB to be bound with a local address ipaddr and port. + * @param[in] p packet buffer it holds the incoming UDP packet data, including its payload and length + * @param[in] addr ip_addr_t structure that contains the IP address of the sender of the UDP packet + * @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){ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 8bec33e..ea729c6 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -28,6 +28,7 @@ #include "log.h" #include "llfs.h" #include "lcd_api.h" +#include "UDP_broadcast.h" /* USER CODE END Includes */ From 6739c7c6bf07be0a4d277dd44c18bb8339d1889a Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 21:14:40 +0100 Subject: [PATCH 20/36] there can only be one there's only one owner struct needed, thus adding a static one in the c file is better than having the main file to initialize one and pass it on again and again,. some code wasn't needed anymore and some had to change --- project/Core/Inc/UDP_broadcast.h | 32 +++++--- project/Core/Src/UDP_broadcast.c | 132 +++++++++++++------------------ project/Core/Src/main.c | 10 +-- 3 files changed, 79 insertions(+), 95 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 929004a..100c5b2 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -15,7 +15,7 @@ #include #include "lwip/netif.h" #include "lwip.h" -#define LOGGER_LEVEL_ALL +#define LOGGER_LEVEL_INFO #include "log.h" #include "udp.h" @@ -44,6 +44,7 @@ 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) @@ -53,34 +54,31 @@ typedef struct { * @brief set_owner_details() is the interface that can be used in other files * to set the owner's details * - * @param owner owner_details_t structure, it contains information about the owner * @param name string containing the new owner's name * @param 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 or owner pointer is NULL */ -uint8_t udp_broadcast_set_owner_details(owner_details_t*, 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) * @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 * @return name of owner * this name is set by @see udp_broadcast_set_owner_details_name() */ -char* udp_broadcast_get_owner_details_name(owner_details_t*); +char* udp_broadcast_get_owner_details_name(); /** * @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 * @return surname of owner * this name is set by @see udp_broadcast_set_owner_details_surname() */ -char* udp_broadcast_get_owner_details_surname(owner_details_t*); +char* udp_broadcast_get_owner_details_surname(); /** * @fn char udp_broadcast_get_owner_details_reply*() @@ -91,12 +89,9 @@ 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) - /** * @fn err_t udp_broadcast_init() - * @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on - * port 6400 + * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() * * @return lwIP error code. * - ERR_OK. Successful. No error occurred. @@ -105,4 +100,19 @@ char* udp_broadcast_get_owner_details_reply(); err_t udp_broadcast_init(); +// 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 + * port 6400 + * it is called by @see udp_broadcast_init() aswell but can be used separately if it failed before + * + * @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_connection_init(); + #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index a0a056c..6469c7c 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -13,39 +13,16 @@ // 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 +static owner_details_t udp_owner; // Functions -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_mac(); +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 uint8_t udp_broadcast_format_reply(const owner_details_t* owner); +static uint8_t 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); -/** - * @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 - * - * - * @param[in] owner owner_details_t structure, it contains information about the owner - * @param[in] word string parameter that was used in the function that triggered this handler - * @param[in] funct name of the function where the error occured - */ - -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){ - LOG_WARN(TAG,"%s: owner given is a NULL pointer",funct); - }else{ - LOG_WARN(TAG,"%s: string given is a NULL pointer",funct); - } -} - /** * @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 @@ -57,13 +34,9 @@ static void udp_broadcast_owner_details_error_handler(const owner_details_t* own * - 0: an error occured, owner pointer is NULL */ -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; - } +static uint8_t udp_broadcast_set_owner_details_mac(){ for (uint8_t i = 0; i < 6; i++){ - owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address + udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address } return 1; } @@ -81,13 +54,13 @@ static uint8_t udp_broadcast_set_owner_details_mac(owner_details_t* owner){ * - 0: an error occured, name pointer is NULL or owner pointer is NULL */ -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); +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); - strncpy(owner->name,name,sizeof(owner->name)); + strncpy(udp_owner.name,name,sizeof(udp_owner.name)); return 1; } @@ -103,13 +76,13 @@ static uint8_t udp_broadcast_set_owner_details_name(owner_details_t* owner, cons * - 1: no error occured, surname was set * - 0: an error occured, surname pointer is NULL or owner pointer is NULL */ -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); +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(owner->surname,surname,sizeof(owner->surname)); + strncpy(udp_owner.surname,surname,sizeof(udp_owner.surname)); return 1; } @@ -117,8 +90,7 @@ static uint8_t udp_broadcast_set_owner_details_surname(owner_details_t* owner, c * @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 * if the pointer given is NULL it calls the error handler - * strncpy is used to copy the function paremeter safely to a global variable - * the reason this one is global is so that the udp_callback function can access it easily + * strncpy is used to copy the function paremeter safely to the owner_details_t's reply * * @param[in] reply string used to reply to the UDP broadcast * @return setting owner reply error @@ -132,7 +104,7 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ return 0; } LOG_DEBUG(TAG,"set: %s",reply); - strncpy(reply_str,reply,sizeof(reply_str)); + strncpy(udp_owner.reply,reply,sizeof(udp_owner.reply)); return 1; } @@ -149,22 +121,19 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ * - 0: an error occured, owner pointer is NULL */ -static uint8_t udp_broadcast_format_reply(const owner_details_t* owner){ +static uint8_t udp_broadcast_format_reply(){ size_t reply_len = 0; char mac_addr_str[18]; char reply_buf[100]; - if (owner == NULL) { - udp_broadcast_owner_details_error_handler(owner, F_REPLY,""); - return 0; - } - reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name); + + reply_len = 20 + 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", - owner->mac_address[0], owner->mac_address[1], owner->mac_address[2], - owner->mac_address[3], owner->mac_address[4], owner->mac_address[5]); + 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, owner->surname, owner->name); + mac_addr_str, udp_owner.surname, udp_owner.name); udp_broadcast_set_owner_details_reply(reply_buf); return 1; @@ -185,12 +154,9 @@ static uint8_t udp_broadcast_format_reply(const 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 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 (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)){ +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; @@ -206,12 +172,8 @@ uint8_t udp_broadcast_set_owner_details(owner_details_t* owner, const char* name * this name is set by @see udp_broadcast_set_owner_details_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; +char* udp_broadcast_get_owner_details_name(){ + return udp_owner.name; } /** @@ -223,12 +185,8 @@ char* udp_broadcast_get_owner_details_name(owner_details_t* owner){ * this name is set by @see udp_broadcast_set_owner_details_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; +char* udp_broadcast_get_owner_details_surname(){ + return udp_owner.surname; } /** @@ -240,7 +198,7 @@ char* udp_broadcast_get_owner_details_surname(owner_details_t* owner){ */ char* udp_broadcast_get_owner_details_reply(){ - return reply_str; + return udp_owner.reply; } @@ -271,7 +229,7 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p if (p != NULL) { pc = (char*)p->payload; len = p->tot_len; - p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM); + 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){ @@ -281,13 +239,13 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p 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 = strlen(reply_str); - p_data->tot_len = strlen(reply_str); + 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,port,reply_str); + LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,udp_owner.reply); } }else{ @@ -312,7 +270,7 @@ 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_init(){ +err_t udp_broadcast_connection_init(){ struct udp_pcb* connection; err_t err; @@ -332,3 +290,19 @@ err_t udp_broadcast_init(){ } return err; } + +/** + * @fn err_t udp_broadcast_init() + * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() + * + * @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 err; + + 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 ea729c6..0613e39 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -128,13 +128,13 @@ int main(void) llfs_init(); // Initialize the UDP broadcast service - owner_details_t owner; - udp_broadcast_init(); - - if(!udp_broadcast_set_owner_details(&owner, "Joran", "Van Nieuwenhoven")){ + if (udp_broadcast_init() != ERR_OK && udp_broadcast_connection_init() != ERR_OK){ + LOG_WARN(TAG,"error initializing udp connection"); + } + if (!udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ LOG_WARN(TAG,"error setting owner's details"); } - LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply(&owner)); + LOG_DEBUG(TAG,"%s",udp_broadcast_get_owner_details_reply()); /* USER CODE END 2 */ /* Infinite loop */ From 597d79efceec6aef4de6e76a578b3a4812f9353a Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Mon, 13 Nov 2023 23:42:45 +0100 Subject: [PATCH 21/36] added set owner details functionality i added some code to the Qt application so that it can send the owner name and surname to set on the microcontroller --- project/Core/Inc/UDP_broadcast.h | 3 +- project/Core/Src/UDP_broadcast.c | 91 ++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 100c5b2..06255a2 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -31,8 +31,9 @@ #define F_REPLY "format_reply" // Defines used by UDP callback -#define MAX_DATA_SIZE 30 // Define the maximum expected data size +#define MAX_DATA_SIZE 50 // 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 diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 6469c7c..4a756ad 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -198,9 +198,60 @@ char* udp_broadcast_get_owner_details_surname(){ */ char* udp_broadcast_get_owner_details_reply(){ - return udp_owner.reply; + return udp_owner.reply; } +/** + * @fn void udp_broadcast_check_function(const char[]) + * @brief + * + * @param data + */ + +static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ + char func[7]; + char buffer[20]; + uint8_t enders[4]; + uint8_t counter = 0; + memset(func, 0, sizeof(func)); + memset(buffer, 0, sizeof(buffer)); + + 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] == ':'){ + enders[counter] = i; + 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 ){ + counter = 0; + for (uint8_t i = enders[1]+2; i< enders[2];i++){ + buffer[counter] = data[i]; + counter++; + } + if (strcmp(buffer,"") == 0){ + strncpy(buffer,"name",sizeof(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);i++){ + buffer[counter] = data[i]; + counter++; + } + if (strcmp(buffer,"") == 0){ + strncpy(buffer,"default",sizeof(buffer)); + } + LOG_INFO(TAG,"new owner surname:%s",buffer); + udp_broadcast_set_owner_details_surname(buffer); + udp_broadcast_format_reply(); + } + } +} /** * @fn void udp_receive_callback(void*, struct udp_pcb*, struct pbuf*, const ip_addr_t*, u16_t) @@ -233,23 +284,33 @@ 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 (size_t i = 0; i < len; i++) { - data[i] = pc[i]; - } + 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,port,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,port,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,port,udp_owner.reply); + } }else{ - LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); + LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); } }else{ From 68b29d444f0c32cdf31981964ca8126cc24f6fa8 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sat, 18 Nov 2023 23:41:41 +0100 Subject: [PATCH 22/36] writing to lcd api added writing the owner's name on the LCD, fixing some bugs --- project/Core/Inc/UDP_broadcast.h | 11 +++++++---- project/Core/Src/UDP_broadcast.c | 27 ++++++++++++++++++--------- project/Core/Src/main.c | 7 +++---- 3 files changed, 28 insertions(+), 17 deletions(-) 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 */ From 220cc89ca1a72f1ba979fdc0c9919f155b9b49c3 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sun, 19 Nov 2023 22:18:33 +0100 Subject: [PATCH 23/36] adding markdown file + minor correction to code --- docs/udp_broadcast.md | 109 ++++++++++++++++++++++++++++++++++++++++ project/Core/Src/main.c | 2 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 docs/udp_broadcast.md diff --git a/docs/udp_broadcast.md b/docs/udp_broadcast.md new file mode 100644 index 0000000..d8aa96d --- /dev/null +++ b/docs/udp_broadcast.md @@ -0,0 +1,109 @@ +# UDP broadcast + +## Introduction + +The UDP broadcast code is used to handle incoming UDP datagrams. +there are currently 2 types of datagrams it processes: +- broadcasts: "Where are you?v1.0", replies with current owner details. +- change of details: "func1:name: ..., surname: ...", replies with changed owner details. + +It also writes the current owner's name on the screen and updates it everytime it's changed. + +## Table of contents +- [Introduction](#introduction) +- [Table of contents](#table-of-contents) +- [Usage of UDP broadcast](#usage-of-udp-broadcast) + - [Initialization of UDP broadcast](#initialization-of-udp-broadcast) + - [Initialization of UDP connection](#initialization-of-udp-connection) + - [Owner details interface](#owner-details-interface) + - [Setting owner details](#setting-owner-details) + - [Getting owner details](#getting-owner-details) + +## Usage of UDP broadcast +### Initialization of UDP broadcast +The 'udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)' function does 4 things: +1. It initializes the coördinates of where the owner's name has to be written on the LCD. +2. [It initializes the UDP connection](#initialization-of-udp-connection) +3. It initializes the owner's details with a default name. +4. Returns the error value of [udp_broadcast_connection_init()](#initialization-of-udp-connection). This way the user can use some code to check whether the "connection" was initialized correctly. +```c +#include "UDP_broadcast.h' + +... + +void main(void){ + ... + if (udp_broadcast_init(270,255) != ERR_OK){ + ... + } + ... +} +``` + +### Initialization of UDP connection +The 'udp_broadcast_connection_init()' funciton does 2 things: +1. Initializes the UDP "connection" so that incoming datagrams can be processed and replied to. It binds to port 64000 and listens to every IP-address in the local network. +2. returns the LWIP error code so that [err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)](#initialization-of-udp-broadcast) knows the "connection" is initializes correctly + +This function can be used seperately from [err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)](#initialization-of-udp-broadcast), this gives the possibility to try "connecting" again after the first time failed. +```c +#include "UDP_broadcast.h' + +... + +void main(void){ + ... + if (udp_broadcast_init(270,255) != ERR_OK || udp_broadcast_connection_init() != ERR_OK){ + ... + } + ... +} + +``` +### Owner details interface +The interface to ask for the owner's details and change them is a modified version of the [Qt application](https://github.com/wimdams/Device_finder) Wim Dams build. His only has the functionality to ask for the owner's details. + +Just because the owner's details might want to be used in other code, some functions have been written for obtaining these in the STM32 code aswell. +#### Setting owner details +THe 'udp_broadcast_set_owner_details(const char* , const char*)' function does 2 things: +1. Set the owner details, the order of the parameters is: name, surname +2. Return 1 if the owner's details have been set correctly and 0 if not. +```c +#include "UDP_broadcast.h' + +... + +void main(void){ + ... + if (!udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ + ... + } + ... +} +``` +#### Getting owner details +There are 3 functions: +- udp_broadcast_get_owner_details_name(): returns the owner's name. +- udp_broadcast_get_owner_details_surname(): returns the owner's surname. +- udp_broadcast_get_owner_details_reply(): returns what would be replied to a UDP broadcast with datagram "Where are you?v1.0". + +```c +#include +#include "UDP_broadcast.h' + +... + +void main(void){ + ... + char name[20]; + char surname[20]; + char reply[120]; + + strncp(name,udp_broadcast_get_owner_details_name(),sizeof(name)); + + strncp(surname,udp_broadcast_get_owner_details_surname(),sizeof(surname)); + + strncp(reply,udp_broadcast_get_owner_details_reply(),sizeof(reply)); + ... +} +``` \ No newline at end of file diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 465131d..a5fcd16 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -127,7 +127,7 @@ int main(void) llfs_init(); // Initialize the UDP broadcast service - if (udp_broadcast_init(270,255) != 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("0123456879012345678", "default")){ From 33235e551d194762c7adfc4504c47f9304d9a448 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sun, 19 Nov 2023 22:30:39 +0100 Subject: [PATCH 24/36] Update main.c --- project/Core/Src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index a5fcd16..0209e78 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -130,7 +130,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("0123456879012345678", "default")){ + if (!udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ LOG_WARN(TAG,"error setting owner's details"); } From 751b6746733aa43be66d9e08f140e58084a202d4 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sun, 19 Nov 2023 22:50:27 +0100 Subject: [PATCH 25/36] Update main.c --- project/Core/Src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 0209e78..2254af0 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -144,6 +144,7 @@ int main(void) /* USER CODE BEGIN 3 */ MX_LWIP_Process(); + lcd_task(); } /* USER CODE END 3 */ } From 47b1cd167bfc0448ffed5c972142154555bb467d Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sun, 19 Nov 2023 22:57:41 +0100 Subject: [PATCH 26/36] Update UDP_broadcast.c --- project/Core/Src/UDP_broadcast.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index d28aae7..8b7beb1 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -121,9 +121,7 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ * it sets this reply with @see udp_broadcast_set_owner_details_reply() * * @param[in] owner owner_details_t structure, it contains information about the owner - * @return formatting reply error - * - 1: no error occured, reply was formatted - * - 0: an error occured, owner pointer is NULL + * @return 1 makes it easier to run @see udp_broadcast_set_owner_details() */ static uint8_t udp_broadcast_format_reply(){ From 48acc38493b6183852ecf899ceb25cd6c6536092 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sun, 19 Nov 2023 23:29:54 +0100 Subject: [PATCH 27/36] minor changes --- project/Core/Inc/UDP_broadcast.h | 10 +++--- project/Core/Src/UDP_broadcast.c | 53 +++++++++++--------------------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index c49cfc8..67b9776 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -56,11 +56,11 @@ typedef struct { * @brief set_owner_details() is the interface that can be used in other files * to set the owner's details * - * @param name string containing the new owner's name - * @param surname string containing the new owner's surname + * @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 or owner pointer is NULL + * - 0: an error occured, all or some details weren't set */ uint8_t udp_broadcast_set_owner_details(const char* , const char*); @@ -95,8 +95,8 @@ 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 + * @param[in] x_pos : uint16_t that sets the x coordinate the owner's name will be written on the LCD + * @param[in] 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. diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 8b7beb1..deb4f58 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -14,47 +14,39 @@ 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; +static uint16_t owner_name_x_pos; +static uint16_t owner_name_y_pos; // Functions -static uint8_t udp_broadcast_set_owner_details_mac(); +static void udp_broadcast_set_owner_details_mac(); 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 uint8_t udp_broadcast_format_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); /** * @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 - * - * @param[out] owner owner_details_t structure, it contains information about the owner - * @return setting owner mac address error - * - 1: no error occured, mac address was set - * - 0: an error occured, owner pointer is NULL */ -static uint8_t udp_broadcast_set_owner_details_mac(){ +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 } - return 1; } /** * @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 - * 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 * @return setting owner name error * - 1: no error occured, name was set - * - 0: an error occured, name pointer is NULL or owner pointer is NULL + * - 0: an error occured, name pointer is NULL */ static uint8_t udp_broadcast_set_owner_details_name(const char* name){ @@ -72,14 +64,12 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name){ /** * @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 - * 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 surname * - * @param[out] owner owner_details_t structure, it contains information about the owner * @param[in] surname string containing the owner's surname * @return setting owner surname error * - 1: no error occured, surname was set - * - 0: an error occured, surname pointer is NULL or owner pointer is NULL + * - 0: an error occured, surname pointer is NULL */ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){ if (surname == NULL){ @@ -94,13 +84,12 @@ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){ /** * @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 - * if the pointer given is NULL it calls the error handler * strncpy is used to copy the function paremeter safely to the owner_details_t's reply * * @param[in] 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 + * - 0: an error occured, reply pointer is null */ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ @@ -116,15 +105,11 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ /** * @fn uint8_t udp_broadcast_format_reply(const owner_details_t*) * @brief format_reply() formats all the owner's details into a string - * if the pointer given is NULL it calls the error handler * it formats a string using the owner's details using snprintf * it sets this reply with @see udp_broadcast_set_owner_details_reply() - * - * @param[in] owner owner_details_t structure, it contains information about the owner - * @return 1 makes it easier to run @see udp_broadcast_set_owner_details() */ -static uint8_t udp_broadcast_format_reply(){ +static void udp_broadcast_format_reply(){ size_t reply_len = 0; char mac_addr_str[18]; char reply_buf[120]; @@ -140,7 +125,6 @@ static uint8_t udp_broadcast_format_reply(){ LOG_DEBUG(TAG,"reply_buf: %s",reply_buf); udp_broadcast_set_owner_details_reply(reply_buf); - return 1; } @@ -148,19 +132,18 @@ static uint8_t udp_broadcast_format_reply(){ * @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 - * if the pointer given is NULL it calls the error handler, - * the other pointers get checked by the functions that are called in this function + * the pointers get checked by the functions that are called in this function * - * @param[out] owner owner_details_t structure, it contains information about the owner * @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 or owner pointer is NULL + * - 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()){ + 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; @@ -171,7 +154,6 @@ uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname){ * @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[out] owner owner_details_t structure, it contains information about the owner * @return name of owner * this name is set by @see udp_broadcast_set_owner_details_name() */ @@ -184,7 +166,6 @@ char* udp_broadcast_get_owner_details_name(){ * @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[out] owner owner_details_t structure, it contains information about the owner * @return surname of owner * this name is set by @see udp_broadcast_set_owner_details_surname() */ @@ -207,9 +188,10 @@ char* udp_broadcast_get_owner_details_reply(){ /** * @fn void udp_broadcast_check_function(const char[]) - * @brief + * @brief checks what the UDP datagram asked to do if it was not @see UDP_QUESTION1 + * and processes the datagram * - * @param data + * @param[in] data the datagram received on port 64000 */ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ @@ -264,6 +246,7 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ * @brief udp_receive_callback() callback function for when a UDP packet has been received. * it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string, * @see reply_str, back to the client + * if it was not @see UDP_QUESTION1, it checks what function was called with @see udp_broadcast_check_function() * * @param[in] arg a pointer to some user-defined data or context * @param[in] connection UDP PCB to be bound with a local address ipaddr and port. From c2dc36c765cda8a46f3b15cbefc1cbdae75fa5c1 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 00:46:32 +0100 Subject: [PATCH 28/36] Fix style guide issues by auto formatting --- project/Core/Inc/UDP_broadcast.h | 28 +-- project/Core/Src/UDP_broadcast.c | 286 ++++++++++++++++--------------- 2 files changed, 161 insertions(+), 153 deletions(-) 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; From 30a93425c9f24d19ceb41e2143eb965b72db04f5 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Wed, 22 Nov 2023 15:04:55 +0100 Subject: [PATCH 29/36] correcting reviews sorry if i missed something, being ill and reviewing code doesn't really go well together --- project/Core/Inc/UDP_broadcast.h | 36 +++--- project/Core/Src/UDP_broadcast.c | 208 +++++++++++++++---------------- project/Core/Src/main.c | 10 ++ 3 files changed, 135 insertions(+), 119 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 7f9ccfc..2c5126c 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -15,8 +15,6 @@ #include #include "lwip.h" #include "lwip/netif.h" -#define LOGGER_LEVEL_INFO -#include "log.h" #include "udp.h" #include "lcd_api.h" @@ -33,8 +31,16 @@ #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 question from UDP client +#define FUNC_LEN 7 +#define MAX_COLON_COMMA_COUNT 4 + +// Defines used by owner details + +#define MAX_NAME_SIZE 20 +#define MAX_REPLY_SIZE 120 + /** * @struct owner_details_t @@ -43,10 +49,10 @@ */ typedef struct { - char name[20]; - char surname[20]; + char name[MAX_NAME_SIZE]; + char surname[MAX_NAME_SIZE]; uint8_t mac_address[6]; - char reply[120]; + char reply[MAX_REPLY_SIZE]; } owner_details_t; // The following functions are used for owner details (those that must be available in main) @@ -65,34 +71,34 @@ typedef struct { uint8_t udp_broadcast_set_owner_details(const char*, const char*); /** - * @fn char udp_broadcast_get_owner_details_name*(owner_details_t) + * @fn char udp_broadcast_get_owner_details_name*(void) * @brief 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() */ -char* udp_broadcast_get_owner_details_name(); +char* udp_broadcast_get_owner_details_name(void); /** - * @fn char udp_broadcast_get_owner_details_surname*(const owner_details_t) + * @fn char udp_broadcast_get_owner_details_surname*(void) * @brief 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() */ -char* udp_broadcast_get_owner_details_surname(); +char* udp_broadcast_get_owner_details_surname(void); /** - * @fn char udp_broadcast_get_owner_details_reply*() + * @fn char udp_broadcast_get_owner_details_reply*(void) * @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* udp_broadcast_get_owner_details_reply(); +char* udp_broadcast_get_owner_details_reply(void); /** - * @fn err_t udp_broadcast_init() + * @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() * * @param[in] x_pos : uint16_t that sets the x coordinate the owner's name will be written on the LCD @@ -117,6 +123,6 @@ err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos); * - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB. */ -err_t udp_broadcast_connection_init(); +err_t udp_broadcast_connection_init(void); #endif /* INC_UDP_BROADCAST_H_ */ diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 78781c0..f7660cf 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -5,10 +5,11 @@ * Created on: Nov 6, 2023 * Author: joran */ -//| // Includes #include "UDP_broadcast.h" +#define LOGGER_LEVEL_INFO +#include "log.h" // Global variables @@ -18,11 +19,11 @@ static uint16_t owner_name_x_pos; static uint16_t owner_name_y_pos; // Functions -static void udp_broadcast_set_owner_details_mac(); +static void udp_broadcast_set_owner_details_mac(void); 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_broadcast_format_reply(void); static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct pbuf* p, @@ -35,7 +36,7 @@ static void udp_receive_callback(void* arg, * and sets it in the owner_details_t struct */ -static void udp_broadcast_set_owner_details_mac() { +static void udp_broadcast_set_owner_details_mac(void) { for (uint8_t i = 0; i < 6; i++) { udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address } @@ -59,9 +60,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); + lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN,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); + lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_FONT16); return 1; } @@ -113,10 +114,10 @@ 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() { +static void udp_broadcast_format_reply(void) { size_t reply_len = 0; char mac_addr_str[18]; - char reply_buf[120]; + char reply_buf[MAX_REPLY_SIZE]; reply_len = 27 + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); @@ -153,38 +154,38 @@ uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname) { } /** - * @fn char udp_broadcast_get_owner_details_name*(owner_details_t) + * @fn char udp_broadcast_get_owner_details_name*(void) * @brief 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() */ -char* udp_broadcast_get_owner_details_name() { +char* udp_broadcast_get_owner_details_name(void) { return udp_owner.name; } /** - * @fn char udp_broadcast_get_owner_details_surname*(const owner_details_t) + * @fn char udp_broadcast_get_owner_details_surname*(void) * @brief 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() */ -char* udp_broadcast_get_owner_details_surname() { +char* udp_broadcast_get_owner_details_surname(void) { return udp_owner.surname; } /** - * @fn char udp_broadcast_get_owner_details_reply*() + * @fn char udp_broadcast_get_owner_details_reply*(void) * @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* udp_broadcast_get_owner_details_reply() { +char* udp_broadcast_get_owner_details_reply(void) { return udp_owner.reply; } @@ -197,51 +198,50 @@ char* udp_broadcast_get_owner_details_reply() { */ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]) { - char func[7]; - char buffer[20]; - uint8_t enders[4]; + char func[FUNC_LEN]; + char buffer[MAX_NAME_SIZE]; + uint8_t enders[MAX_COLON_COMMA_COUNT]; uint8_t counter = 0; + size_t data_len = strlen(data); + memset(func, 0, sizeof(func)); memset(buffer, 0, sizeof(buffer)); - for (uint8_t i = 0; i < 6; i++) { - func[i] = data[i]; + memcpy(func,data,FUNC_LEN-1); + if (strcmp(func, "func1:") != 0) { + LOG_WARN(TAG, "udp_broadcast_check_function: datagram does not contain function that's currently available"); + return; } - if (strcmp(func, "func1:") == 0) { - for (uint8_t i = 0; i < strlen(data); i++) { - if (data[i] == ',' || data[i] == ':') { - enders[counter] = i; - counter++; - } + for (uint8_t i = 0; i < data_len; i++) { + if ((data[i] == ',' || data[i] == ':') && counter <= MAX_COLON_COMMA_COUNT) { + 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 + } + if (enders[2] - enders[1] < MAX_NAME_SIZE + 2 && data_len - enders[3] < MAX_NAME_SIZE + 2 && 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++) { - buffer[counter] = data[i]; - LOG_DEBUG(TAG, "%d", counter); - counter++; - } - if (strcmp(buffer, "") == 0) { - strncpy(buffer, "name", sizeof(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++) { - buffer[counter] = data[i]; - counter++; - } - if (strcmp(buffer, "") == 0) { - strncpy(buffer, "default", sizeof(buffer)); - } - LOG_INFO(TAG, "new owner surname:%s", buffer); - udp_broadcast_set_owner_details_surname(buffer); - udp_broadcast_format_reply(); + counter = 0; + for (uint8_t i = enders[1] + 2; i < enders[2] && data[i] != '\0'; i++) { + buffer[counter] = data[i]; + counter++; } + if (strcmp(buffer, "") == 0) { + strncpy(buffer, "name", sizeof(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 < data_len; i++) { + buffer[counter] = data[i]; + counter++; + } + if (buffer[0]=='\0') { + strncpy(buffer, "default", sizeof(buffer)); + } + LOG_INFO(TAG, "new owner surname:%s", buffer); + udp_broadcast_set_owner_details_surname(buffer); + udp_broadcast_format_reply(); } } @@ -274,51 +274,52 @@ static void udp_receive_callback(void* arg, 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]; - } - - 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 { + if (p == NULL) { LOG_WARN(TAG, "udp_receive_callback: input buffer was a NULL pointer"); + return; } + 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"); + goto defer; + } if (len > MAX_DATA_SIZE) { + LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", MAX_DATA_SIZE); + goto defer; + } + 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); + goto defer; + } + 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); + +defer: pbuf_free(p); pbuf_free(p_data); } /** - * @fn err_t udp_broadcast_init() + * @fn err_t udp_broadcast_init(void) * @brief init_UDP_server() initialises the UDP connection so that it listens for all traffic on * port 6400 * it makes a udp_pcb, binds it to port 64000 and initializes the callback function for when data is received @@ -328,27 +329,28 @@ static void udp_receive_callback(void* arg, * - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB. */ -err_t udp_broadcast_connection_init() { +err_t udp_broadcast_connection_init(void) { 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"); + if (connection == NULL) { + LOG_WARN(TAG, "Initializing UDP server failed, connection is null"); } + err = udp_bind(connection, IP_ANY_TYPE, 64000); + if (err != ERR_OK) { + LOG_WARN(TAG, "Initializing UDP server failed, err not ok"); + udp_remove(connection); + return err; + } + udp_recv(connection, udp_receive_callback, NULL); + LOG_INFO(TAG, "Initializing UDP server successful, callback running"); return err; } + + /** * @fn err_t udp_broadcast_init() * @brief udp_broadcast_init() initializes the owner's variables and calls upon @see udp_broadcast_connection_init() @@ -358,10 +360,8 @@ err_t udp_broadcast_connection_init() { * - 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 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; + return udp_broadcast_connection_init(); } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 14db670..0678478 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -29,6 +29,7 @@ #include "llfs.h" #include "lcd_api.h" #include "tftp.h" +#include "UDP_broadcast.h" /* USER CODE END Includes */ @@ -132,6 +133,15 @@ int main(void) /* Initialize the tftp server */ tftp_server_init(); + + // Initialize the UDP broadcast service + 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")){ + LOG_WARN(TAG,"error setting owner's details"); + } + /* USER CODE END 2 */ /* Infinite loop */ From 0ff34a233ee3fb9a3e4a4bc9354ceec305a93fbe Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Wed, 22 Nov 2023 22:21:07 +0100 Subject: [PATCH 30/36] solving reviews pt2 --- project/Core/Inc/UDP_broadcast.h | 30 +++++++++++++++++----------- project/Core/Src/UDP_broadcast.c | 34 +++++++++++++++++++------------- project/Core/Src/main.c | 8 ++++---- 3 files changed, 42 insertions(+), 30 deletions(-) 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 */ From 43fc97c10983bfdac6b6116da9d8b7d1ed1160cb Mon Sep 17 00:00:00 2001 From: Sani7 Date: Wed, 22 Nov 2023 22:31:44 +0100 Subject: [PATCH 31/36] Fix typo in define MAX_MAC_ADDR_LEN --- project/Core/Inc/UDP_broadcast.h | 2 +- project/Core/Src/UDP_broadcast.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 8276c65..8c9e095 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -40,7 +40,7 @@ #define MAX_NAME_SIZE 20 #define MAX_REPLY_SIZE 120 -#define MAX_MAX_ADDR_LEN 18 +#define MAX_MAC_ADDR_LEN 18 #define MAX_EXTRA_REPLY_CHARS 27 diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 3e172f9..c537338 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -116,7 +116,7 @@ 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[MAX_MAX_ADDR_LEN]; + char mac_addr_str[MAX_MAC_ADDR_LEN]; char reply_buf[MAX_REPLY_SIZE]; reply_len = MAX_EXTRA_REPLY_CHARS + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); From 82d7972728523c2ade5efb46d00fb37873f6a58b Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Fri, 24 Nov 2023 22:38:04 +0100 Subject: [PATCH 32/36] solving reviews pt3 --- project/Core/Inc/UDP_broadcast.h | 34 ++++------- project/Core/Src/UDP_broadcast.c | 102 +++++++++++++++---------------- project/Core/Src/main.c | 12 +++- 3 files changed, 72 insertions(+), 76 deletions(-) diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index 8c9e095..a5f0047 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -19,29 +19,17 @@ #include "lcd_api.h" -// 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" -#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" - // 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 question from UDP client -#define FUNC_LEN 7 -#define MAX_COLON_COMMA_COUNT 4 +#define UDP_BROADCAST_MAX_DATA_SIZE 63 // Define the maximum expected data size +#define UDP_BROADCAST_UDP_QUESTION1 "Where are you?v1.0" // Expected question from UDP client +#define UDP_BROADCAST_FUNC_LEN 7 +#define UDP_BROADCAST_MAX_COLON_COMMA_COUNT 4 // Defines used by owner details - -#define MAX_NAME_SIZE 20 -#define MAX_REPLY_SIZE 120 -#define MAX_MAC_ADDR_LEN 18 -#define MAX_EXTRA_REPLY_CHARS 27 +#define UDP_BROADCAST_MAX_NAME_SIZE 21 // Code automatically leaves 1 char for '\0' (actual length = length - 1) +#define UDP_BROADCAST_MAX_REPLY_SIZE 120 // Code automatically leaves 1 char for '\0' (actual length = length - 1) +#define UDP_BROADCAST_MAX_MAC_ADDR_LEN 19 +#define UDP_BROADCAST_MAX_EXTRA_REPLY_CHARS 27 /** @@ -51,10 +39,10 @@ */ typedef struct { - char name[MAX_NAME_SIZE]; - char surname[MAX_NAME_SIZE]; + char name[UDP_BROADCAST_MAX_NAME_SIZE]; + char surname[UDP_BROADCAST_MAX_NAME_SIZE]; uint8_t mac_address[6]; - char reply[MAX_REPLY_SIZE]; + char reply[UDP_BROADCAST_MAX_REPLY_SIZE]; } owner_details_t; // The following functions are used for owner details (those that must be available in main) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index c537338..ae823f5 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -50,20 +50,20 @@ static void udp_broadcast_set_owner_details_mac(void) { * * @param[in] name string containing the owner's name * @return setting owner name error - * - 1: no error occured, name was set - * - 0: an error occured, name pointer is NULL + * - 0: no error occured, name was set + * - 1: 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_WARN(TAG, "set_owner_details_name: string given is a NULL pointer"); + return 1; } LOG_DEBUG(TAG, "set: %s", name); lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN,LCD_FONT16); - strncpy(udp_owner.name, name, sizeof(udp_owner.name)); + strncpy(udp_owner.name, name, sizeof(udp_owner.name)-1); // -1: compensate for '\0' lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_FONT16); - return 1; + return 0; } /** @@ -73,17 +73,17 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name) { * * @param[in] surname string containing the owner's surname * @return setting owner surname error - * - 1: no error occured, surname was set - * - 0: an error occured, surname pointer is NULL + * - 0: no error occured, surname was set + * - 1: 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_WARN(TAG, "set_owner_details_surname: string given is a NULL pointer"); + return 1; } LOG_DEBUG(TAG, "set: %s", surname); - strncpy(udp_owner.surname, surname, sizeof(udp_owner.surname)); - return 1; + strncpy(udp_owner.surname, surname, sizeof(udp_owner.surname)-1); // -1: compensate for '\0' + return 0; } /** @@ -93,18 +93,18 @@ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname) { * * @param[in] 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 + * - 0: no error occured, reply was set + * - 1: 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_WARN(TAG, "set_owner_details_reply: string given is a NULL pointer"); + return 1; } LOG_DEBUG(TAG, "set: %s", reply); - strncpy(udp_owner.reply, reply, sizeof(udp_owner.reply)); - return 1; + strncpy(udp_owner.reply, reply, sizeof(udp_owner.reply)-1); // -1: compensate for '\0' + return 0; } /** @@ -115,17 +115,14 @@ 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[MAX_MAC_ADDR_LEN]; - char reply_buf[MAX_REPLY_SIZE]; - - reply_len = MAX_EXTRA_REPLY_CHARS + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); + char mac_addr_str[UDP_BROADCAST_MAX_MAC_ADDR_LEN]; + char reply_buf[UDP_BROADCAST_MAX_REPLY_SIZE]; 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, + snprintf(reply_buf, UDP_BROADCAST_MAX_REPLY_SIZE, "%s is present and my owner is %s %s", mac_addr_str, udp_owner.surname, udp_owner.name); udp_broadcast_set_owner_details_reply(reply_buf); } @@ -143,12 +140,14 @@ static void udp_broadcast_format_reply(void) { * - ERR_ARG. one or both arguments are NULL pointers */ 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)) { + if (!udp_broadcast_set_owner_details_name(name) && !udp_broadcast_set_owner_details_surname(surname)) { + + // If both return 0 it's okay udp_broadcast_set_owner_details_mac(); udp_broadcast_format_reply(); - return ERR_ARG; + return ERR_OK; } - return ERR_OK; + return ERR_ARG; } /** @@ -195,36 +194,37 @@ char* udp_broadcast_get_owner_details_reply(void) { * @param[in] data the datagram received on port 64000 */ -static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]) { - char func[FUNC_LEN]; - char buffer[MAX_NAME_SIZE]; - uint8_t enders[MAX_COLON_COMMA_COUNT]; +static void udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DATA_SIZE]) { + char func[UDP_BROADCAST_FUNC_LEN]; + char buffer[UDP_BROADCAST_MAX_NAME_SIZE]; + uint8_t enders[UDP_BROADCAST_MAX_COLON_COMMA_COUNT]; uint8_t counter = 0; - size_t data_len = strlen(data); + uint8_t data_len = strlen(data); memset(func, 0, sizeof(func)); memset(buffer, 0, sizeof(buffer)); - memcpy(func,data,FUNC_LEN-1); + memcpy(func,data,UDP_BROADCAST_FUNC_LEN-1); if (strcmp(func, "func1:") != 0) { 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 && counter < MAX_COLON_COMMA_COUNT; i++) { + for (uint8_t i = 0; i < data_len && counter < UDP_BROADCAST_MAX_COLON_COMMA_COUNT; i++) { if ((data[i] == ',' || data[i] == ':')) { enders[counter] = i; counter++; } } - if (enders[2] - enders[1] < MAX_NAME_SIZE + 2 && data_len - enders[3] < MAX_NAME_SIZE + 2 && strncmp(data + enders[0], ":name", 5) == 0 - && strncmp(data + enders[2], ", surname", 9) == 0) { + if (enders[2] - enders[1] < UDP_BROADCAST_MAX_NAME_SIZE + 1 && data_len - enders[3] < UDP_BROADCAST_MAX_NAME_SIZE + 1 + && 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]; i++) { buffer[counter] = data[i]; counter++; } - if (strcmp(buffer, "") == 0) { - strncpy(buffer, "name", sizeof(buffer)); + if (buffer[0]=='\0') { + strncpy(buffer, "name", sizeof(buffer)-1); // -1: compensate for '\0', just for safety, not really needed } LOG_INFO(TAG, "new owner name:%s", buffer); udp_broadcast_set_owner_details_name(buffer); @@ -235,7 +235,7 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]) { counter++; } if (buffer[0]=='\0') { - strncpy(buffer, "default", sizeof(buffer)); + strncpy(buffer, "default", sizeof(buffer)-1); // -1: compensate for '\0', just for safety, not really needed } LOG_INFO(TAG, "new owner surname:%s", buffer); udp_broadcast_set_owner_details_surname(buffer); @@ -265,7 +265,7 @@ static void udp_receive_callback(void* arg, struct pbuf* p_data; size_t len; char* pc; - char data[MAX_DATA_SIZE]; + char data[UDP_BROADCAST_MAX_DATA_SIZE]; char source_ip_str[16]; memset(data, 0, sizeof(data)); @@ -282,8 +282,8 @@ static void udp_receive_callback(void* arg, if (p_data == NULL) { LOG_WARN(TAG, "udp_receive_callback: unable to allocate data buffer for reply"); goto defer; - } if (len > MAX_DATA_SIZE) { - LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", MAX_DATA_SIZE); + } if (len > UDP_BROADCAST_MAX_DATA_SIZE) { + LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", UDP_BROADCAST_MAX_DATA_SIZE); goto defer; } for (size_t i = 0; i < len; i++) { @@ -291,17 +291,17 @@ static void udp_receive_callback(void* arg, } 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_BROADCAST_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); + LOG_INFO(TAG, "udp_receive_callback: tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply); goto defer; } - LOG_INFO(TAG, "other function called"); + LOG_INFO(TAG, "udp_receive_callback: checking which function was called"); udp_broadcast_check_function(data); p_data->payload = udp_owner.reply; p_data->len = strlen(udp_owner.reply); @@ -309,7 +309,7 @@ static void udp_receive_callback(void* arg, 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: tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply); defer: pbuf_free(p); @@ -332,20 +332,20 @@ err_t udp_broadcast_connection_init(void) { struct udp_pcb* connection; err_t err; - LOG_INFO(TAG, "initialising UDP server"); + LOG_INFO(TAG, "udp_broadcast_connection_init: initializing UDP server"); connection = udp_new(); if (connection == NULL) { - LOG_WARN(TAG, "Initializing UDP server failed, connection is null"); + LOG_WARN(TAG, "udp_broadcast_connection_init: Initializing UDP server failed, connection is null"); return ERR_MEM; } err = udp_bind(connection, IP_ANY_TYPE, 64000); if (err != ERR_OK) { - LOG_WARN(TAG, "Initializing UDP server failed, err not ok"); + LOG_WARN(TAG, "udp_broadcast_connection_init: Initializing UDP server failed, err not ok"); udp_remove(connection); return err; } udp_recv(connection, udp_receive_callback, NULL); - LOG_INFO(TAG, "Initializing UDP server successful, callback running"); + LOG_INFO(TAG, "udp_broadcast_connection_init: Initializing UDP server successful, callback running"); return err; } diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 6506a54..53afcb7 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -135,9 +135,17 @@ int main(void) tftp_server_init(); // Initialize the UDP broadcast service - if (udp_broadcast_init(270,255) != ERR_OK || udp_broadcast_connection_init() != ERR_OK){ - LOG_WARN(TAG,"error initializing udp connection"); + + if (udp_broadcast_init(270,255) == ERR_OK){ + goto connected; } + LOG_WARN(TAG,"error initializing udp connection, trying again in 500ms"); + HAL_Delay(500); + if(udp_broadcast_connection_init() != ERR_OK){ + LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()"); + } + +connected: if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ LOG_WARN(TAG,"error setting owner's details"); } From 0da6259ae504a78407fe18c7a2d44f66eb9f2e88 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:33:36 +0100 Subject: [PATCH 33/36] solving reviews pt4 + some changes --- docs/udp_broadcast.md | 17 ++++-- project/Core/Inc/UDP_broadcast.h | 19 ++++--- project/Core/Src/UDP_broadcast.c | 89 +++++++++++++++++++++----------- project/Core/Src/main.c | 2 +- 4 files changed, 83 insertions(+), 44 deletions(-) diff --git a/docs/udp_broadcast.md b/docs/udp_broadcast.md index d8aa96d..c9d53aa 100644 --- a/docs/udp_broadcast.md +++ b/docs/udp_broadcast.md @@ -34,8 +34,8 @@ The 'udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)' function does 4 things: void main(void){ ... if (udp_broadcast_init(270,255) != ERR_OK){ - ... - } + ... + } ... } ``` @@ -53,9 +53,16 @@ This function can be used seperately from [err_t udp_broadcast_init(uint16_t x_p void main(void){ ... - if (udp_broadcast_init(270,255) != ERR_OK || udp_broadcast_connection_init() != ERR_OK){ - ... - } + if (udp_broadcast_init(10,255) == ERR_OK){ + goto connected; + } + LOG_WARN(TAG,"error initializing udp connection, trying again in 500ms"); + HAL_Delay(500); + if(udp_broadcast_connection_init() != ERR_OK){ + LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()"); + } + +connected: ... } diff --git a/project/Core/Inc/UDP_broadcast.h b/project/Core/Inc/UDP_broadcast.h index a5f0047..ff26d10 100644 --- a/project/Core/Inc/UDP_broadcast.h +++ b/project/Core/Inc/UDP_broadcast.h @@ -20,16 +20,21 @@ #include "lcd_api.h" // Defines used by UDP callback -#define UDP_BROADCAST_MAX_DATA_SIZE 63 // Define the maximum expected data size -#define UDP_BROADCAST_UDP_QUESTION1 "Where are you?v1.0" // Expected question from UDP client -#define UDP_BROADCAST_FUNC_LEN 7 +#define UDP_BROADCAST_MAX_DATA_SIZE ((UDP_BROADCAST_MAX_NAME_SIZE * 2) - 2 + 25) // Define the maximum expected data size +#define UDP_BROADCAST_UDP_QUESTION1 "Where are you?v1.0" // Expected question from UDP client +#define UDP_BROADCAST_MAX_FUNC_LEN 7 #define UDP_BROADCAST_MAX_COLON_COMMA_COUNT 4 // Defines used by owner details -#define UDP_BROADCAST_MAX_NAME_SIZE 21 // Code automatically leaves 1 char for '\0' (actual length = length - 1) -#define UDP_BROADCAST_MAX_REPLY_SIZE 120 // Code automatically leaves 1 char for '\0' (actual length = length - 1) -#define UDP_BROADCAST_MAX_MAC_ADDR_LEN 19 -#define UDP_BROADCAST_MAX_EXTRA_REPLY_CHARS 27 +#define UDP_BROADCAST_MAX_REPLY_SIZE (UDP_BROADCAST_MAX_MAC_ADDR_LEN + sizeof(UDP_BROADCAST_REPLY_MIDDLE_TEXT) + (UDP_BROADCAST_MAX_NAME_SIZE * 2) + UDP_BROADCAST_MAX_REPLY_SIZE_EXTRA) +#define UDP_BROADCAST_REPLY_MIDDLE_TEXT "is present and my owner is" + +#define UDP_BROADCAST_MAX_MAC_ADDR_LEN 19 // Format is: "xx:xx:xx:xx:xx:xx" +#define UDP_BROADCAST_MAX_NAME_SIZE 21 // Code automatically leaves 1 char for '\0' (actual length = length - 1) +#define UDP_BROADCAST_MAX_REPLY_SIZE_EXTRA 20 // Just a bit extra + +#define UDP_BROADCAST_LCD_NAME_PRE_TEXT "New owner: " +#define UDP_BROADCAST_LCD_TEXT_SIZE (strlen(UDP_BROADCAST_LCD_NAME_PRE_TEXT) + UDP_BROADCAST_MAX_NAME_SIZE) /** diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index ae823f5..b24ed9d 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -20,6 +20,7 @@ static uint16_t owner_name_y_pos; // Functions static void udp_broadcast_set_owner_details_mac(void); +static void udp_broadcast_name_to_lcd(void); 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); @@ -42,6 +43,26 @@ static void udp_broadcast_set_owner_details_mac(void) { } } +/** + * @fn void udp_broadcast_name_to_lcd(void) + * @brief prints the owner's name with + * @see UDP_BROADCAST_LCD_NAME_PRE_TEXT in front of it + */ + +static void udp_broadcast_name_to_lcd(void){ + char text[UDP_BROADCAST_LCD_TEXT_SIZE]; + + memset(text,' ',UDP_BROADCAST_LCD_TEXT_SIZE); // Fill with spaces + text[UDP_BROADCAST_LCD_TEXT_SIZE - 1] = '\0'; // Make the last a NULL byte + lcd_display_text(text, owner_name_x_pos, owner_name_y_pos, LCD_BLACK, LCD_WHITE, LCD_FONT12); + + snprintf(text, UDP_BROADCAST_LCD_TEXT_SIZE, "%s%s",UDP_BROADCAST_LCD_NAME_PRE_TEXT, + udp_owner.name); + + lcd_display_text(text, owner_name_x_pos, owner_name_y_pos, LCD_BLACK, LCD_WHITE, LCD_FONT12); +} + + /** * @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 @@ -55,14 +76,15 @@ static void udp_broadcast_set_owner_details_mac(void) { */ static uint8_t udp_broadcast_set_owner_details_name(const char* name) { + if (name == NULL) { LOG_WARN(TAG, "set_owner_details_name: string given is a NULL pointer"); return 1; } LOG_DEBUG(TAG, "set: %s", name); - lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN,LCD_FONT16); - strncpy(udp_owner.name, name, sizeof(udp_owner.name)-1); // -1: compensate for '\0' - lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_FONT16); + strncpy(udp_owner.name, name, sizeof(udp_owner.name) - 1); // -1: compensate for '\0' + + udp_broadcast_name_to_lcd(); return 0; } @@ -103,7 +125,7 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply) { return 1; } LOG_DEBUG(TAG, "set: %s", reply); - strncpy(udp_owner.reply, reply, sizeof(udp_owner.reply)-1); // -1: compensate for '\0' + strncpy(udp_owner.reply, reply, sizeof(udp_owner.reply) - 1); // -1: compensate for '\0' return 0; } @@ -122,7 +144,7 @@ static void udp_broadcast_format_reply(void) { 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, UDP_BROADCAST_MAX_REPLY_SIZE, "%s is present and my owner is %s %s", mac_addr_str, udp_owner.surname, + snprintf(reply_buf, UDP_BROADCAST_MAX_REPLY_SIZE, "%s %s %s %s", mac_addr_str, UDP_BROADCAST_REPLY_MIDDLE_TEXT, udp_owner.surname, udp_owner.name); udp_broadcast_set_owner_details_reply(reply_buf); } @@ -188,26 +210,34 @@ char* udp_broadcast_get_owner_details_reply(void) { /** * @fn void udp_broadcast_check_function(const char[]) - * @brief checks what the UDP datagram asked to do if it was not @see UDP_QUESTION1 - * and processes the datagram + * @brief checks what the UDP datagram asked to do + * and processes the datagram if it was not @see UDP_QUESTION1 * * @param[in] data the datagram received on port 64000 + * + * @return checked + * - 0: a function was found and processed if necessary + * - 1: datagram didn't have a known function */ -static void udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DATA_SIZE]) { - char func[UDP_BROADCAST_FUNC_LEN]; +static uint8_t udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DATA_SIZE]) { + char func[UDP_BROADCAST_MAX_FUNC_LEN]; char buffer[UDP_BROADCAST_MAX_NAME_SIZE]; uint8_t enders[UDP_BROADCAST_MAX_COLON_COMMA_COUNT]; uint8_t counter = 0; uint8_t data_len = strlen(data); + if (strcmp(data, UDP_BROADCAST_UDP_QUESTION1) == 0) { + return 0; + } + memset(func, 0, sizeof(func)); memset(buffer, 0, sizeof(buffer)); - memcpy(func,data,UDP_BROADCAST_FUNC_LEN-1); + memcpy(func,data,UDP_BROADCAST_MAX_FUNC_LEN - 1); if (strcmp(func, "func1:") != 0) { LOG_WARN(TAG, "udp_broadcast_check_function: datagram does not contain function that's currently available"); - return; + return 1; } for (uint8_t i = 0; i < data_len && counter < UDP_BROADCAST_MAX_COLON_COMMA_COUNT; i++) { if ((data[i] == ',' || data[i] == ':')) { @@ -215,7 +245,7 @@ static void udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DATA_ counter++; } } - if (enders[2] - enders[1] < UDP_BROADCAST_MAX_NAME_SIZE + 1 && data_len - enders[3] < UDP_BROADCAST_MAX_NAME_SIZE + 1 + if (enders[2] - enders[1] < UDP_BROADCAST_MAX_NAME_SIZE + 2 && data_len - enders[3] < UDP_BROADCAST_MAX_NAME_SIZE + 2 && strncmp(data + enders[0], ":name", 5) == 0 && strncmp(data + enders[2], ", surname", 9) == 0) { counter = 0; @@ -224,7 +254,7 @@ static void udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DATA_ counter++; } if (buffer[0]=='\0') { - strncpy(buffer, "name", sizeof(buffer)-1); // -1: compensate for '\0', just for safety, not really needed + strncpy(buffer, "name", sizeof(buffer) - 1); // -1: compensate for '\0' } LOG_INFO(TAG, "new owner name:%s", buffer); udp_broadcast_set_owner_details_name(buffer); @@ -235,11 +265,15 @@ static void udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DATA_ counter++; } if (buffer[0]=='\0') { - strncpy(buffer, "default", sizeof(buffer)-1); // -1: compensate for '\0', just for safety, not really needed + strncpy(buffer, "default", sizeof(buffer) - 1); // -1: compensate for '\0' } LOG_INFO(TAG, "new owner surname:%s", buffer); udp_broadcast_set_owner_details_surname(buffer); udp_broadcast_format_reply(); + return 0; + }else{ + LOG_WARN(TAG,"udp_broadcast_check_function: function didn't receive the right formatting"); + return 1; } } @@ -278,38 +312,31 @@ static void udp_receive_callback(void* arg, } pc = (char*)p->payload; len = p->tot_len; + if (len >= UDP_BROADCAST_MAX_DATA_SIZE) { // >= : only if it's smaller to compensate for '\0' + LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", UDP_BROADCAST_MAX_DATA_SIZE); + return; + } + 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"); goto defer; - } if (len > UDP_BROADCAST_MAX_DATA_SIZE) { - LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", UDP_BROADCAST_MAX_DATA_SIZE); - goto defer; } 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_BROADCAST_UDP_QUESTION1) == 0) { + LOG_INFO(TAG, "udp_receive_callback: checking which function was called"); + + if(!udp_broadcast_check_function(data)){ // Should return 0 to reply 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 - */ + udp_sendto(connection, p_data, addr, 64000); // QT app listens on port 64000 LOG_INFO(TAG, "udp_receive_callback: tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply); - goto defer; } - LOG_INFO(TAG, "udp_receive_callback: checking which function was 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, "udp_receive_callback: tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply); + defer: pbuf_free(p); diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 53afcb7..4c3890a 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -136,7 +136,7 @@ int main(void) // Initialize the UDP broadcast service - if (udp_broadcast_init(270,255) == ERR_OK){ + if (udp_broadcast_init(10,255) == ERR_OK){ goto connected; } LOG_WARN(TAG,"error initializing udp connection, trying again in 500ms"); From 9bec13a217cbb8470097b28837c7846cfa8a727d Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sat, 25 Nov 2023 14:46:58 +0100 Subject: [PATCH 34/36] Update UDP_broadcast.c --- project/Core/Src/UDP_broadcast.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index b24ed9d..032629b 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -313,7 +313,7 @@ static void udp_receive_callback(void* arg, pc = (char*)p->payload; len = p->tot_len; if (len >= UDP_BROADCAST_MAX_DATA_SIZE) { // >= : only if it's smaller to compensate for '\0' - LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", UDP_BROADCAST_MAX_DATA_SIZE); + LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than or was max size %d", UDP_BROADCAST_MAX_DATA_SIZE); return; } From a73080b1fc57b8ca1799afa9d358285ff00ae32a Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sat, 25 Nov 2023 15:16:09 +0100 Subject: [PATCH 35/36] set_owner_details returns an error code not 0 or 1 --- docs/udp_broadcast.md | 14 +++++++------- project/Core/Src/main.c | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/udp_broadcast.md b/docs/udp_broadcast.md index c9d53aa..8e8cf46 100644 --- a/docs/udp_broadcast.md +++ b/docs/udp_broadcast.md @@ -59,7 +59,7 @@ void main(void){ LOG_WARN(TAG,"error initializing udp connection, trying again in 500ms"); HAL_Delay(500); if(udp_broadcast_connection_init() != ERR_OK){ - LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()"); + LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()"); } connected: @@ -82,9 +82,9 @@ THe 'udp_broadcast_set_owner_details(const char* , const char*)' function does 2 void main(void){ ... - if (!udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ - ... - } + if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven") != ERR_OK){ + ... + } ... } ``` @@ -106,11 +106,11 @@ void main(void){ char surname[20]; char reply[120]; - strncp(name,udp_broadcast_get_owner_details_name(),sizeof(name)); + strncp(name, udp_broadcast_get_owner_details_name(), sizeof(name) - 1); - strncp(surname,udp_broadcast_get_owner_details_surname(),sizeof(surname)); + strncp(surname, udp_broadcast_get_owner_details_surname(), sizeof(surname) - 1); - strncp(reply,udp_broadcast_get_owner_details_reply(),sizeof(reply)); + strncp(reply, udp_broadcast_get_owner_details_reply(), sizeof(reply) - 1); ... } ``` \ No newline at end of file diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 4c3890a..c32ec1b 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -146,7 +146,7 @@ int main(void) } connected: - if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven")){ + if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven") != ERR_OK){ LOG_WARN(TAG,"error setting owner's details"); } From 4eb6264a2756b855ba080630b33f92aba97a30d4 Mon Sep 17 00:00:00 2001 From: joran2738 <101818067+joran2738@users.noreply.github.com> Date: Sun, 26 Nov 2023 13:53:24 +0100 Subject: [PATCH 36/36] solving reviews pt5 --- project/Core/Src/UDP_broadcast.c | 35 ++++++++++++++++---------------- project/Core/Src/main.c | 10 +-------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 032629b..d7261be 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -78,7 +78,7 @@ static void udp_broadcast_name_to_lcd(void){ static uint8_t udp_broadcast_set_owner_details_name(const char* name) { if (name == NULL) { - LOG_WARN(TAG, "set_owner_details_name: string given is a NULL pointer"); + LOG_WARN(TAG, "%s: string given is a NULL pointer", __func__); return 1; } LOG_DEBUG(TAG, "set: %s", name); @@ -100,7 +100,7 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name) { */ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname) { if (surname == NULL) { - LOG_WARN(TAG, "set_owner_details_surname: string given is a NULL pointer"); + LOG_WARN(TAG, "%s: string given is a NULL pointer", __func__); return 1; } LOG_DEBUG(TAG, "set: %s", surname); @@ -121,7 +121,7 @@ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname) { static uint8_t udp_broadcast_set_owner_details_reply(const char* reply) { if (reply == NULL) { - LOG_WARN(TAG, "set_owner_details_reply: string given is a NULL pointer"); + LOG_WARN(TAG, "%s: string given is a NULL pointer", __func__); return 1; } LOG_DEBUG(TAG, "set: %s", reply); @@ -236,7 +236,7 @@ static uint8_t udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DA memcpy(func,data,UDP_BROADCAST_MAX_FUNC_LEN - 1); if (strcmp(func, "func1:") != 0) { - LOG_WARN(TAG, "udp_broadcast_check_function: datagram does not contain function that's currently available"); + LOG_WARN(TAG, "%s: datagram does not contain function that's currently available", __func__); return 1; } for (uint8_t i = 0; i < data_len && counter < UDP_BROADCAST_MAX_COLON_COMMA_COUNT; i++) { @@ -271,10 +271,9 @@ static uint8_t udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DA udp_broadcast_set_owner_details_surname(buffer); udp_broadcast_format_reply(); return 0; - }else{ - LOG_WARN(TAG,"udp_broadcast_check_function: function didn't receive the right formatting"); - return 1; } + LOG_WARN(TAG,"%s: function didn't receive the right formatting", __func__); + return 1; } /** @@ -307,34 +306,34 @@ static void udp_receive_callback(void* arg, ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); // Convert the source IP address to a string if (p == NULL) { - LOG_WARN(TAG, "udp_receive_callback: input buffer was a NULL pointer"); + LOG_WARN(TAG, "%s: input buffer was a NULL pointer", __func__); return; } pc = (char*)p->payload; len = p->tot_len; if (len >= UDP_BROADCAST_MAX_DATA_SIZE) { // >= : only if it's smaller to compensate for '\0' - LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than or was max size %d", UDP_BROADCAST_MAX_DATA_SIZE); + LOG_WARN(TAG, "%s: input buffer was bigger than or was max size %d", __func__, UDP_BROADCAST_MAX_DATA_SIZE); return; } 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"); + LOG_WARN(TAG, "%s: unable to allocate data buffer for reply", __func__); goto defer; } 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); - LOG_INFO(TAG, "udp_receive_callback: checking which function was called"); + LOG_INFO(TAG, "%s: received data from %s at port: %d: %s", __func__, source_ip_str, port, data); + LOG_INFO(TAG, "%s: checking which function was called", __func__); if(!udp_broadcast_check_function(data)){ // Should return 0 to reply 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); // QT app listens on port 64000 - LOG_INFO(TAG, "udp_receive_callback: tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply); + LOG_INFO(TAG, "%s: tried to reply to %s at port: %d: %s", __func__, source_ip_str, 64000, udp_owner.reply); } @@ -359,20 +358,20 @@ err_t udp_broadcast_connection_init(void) { struct udp_pcb* connection; err_t err; - LOG_INFO(TAG, "udp_broadcast_connection_init: initializing UDP server"); + LOG_INFO(TAG, "%s: initializing UDP server", __func__); connection = udp_new(); if (connection == NULL) { - LOG_WARN(TAG, "udp_broadcast_connection_init: Initializing UDP server failed, connection is null"); + LOG_WARN(TAG, "%s: Initializing UDP server failed, connection is null", __func__); return ERR_MEM; } err = udp_bind(connection, IP_ANY_TYPE, 64000); if (err != ERR_OK) { - LOG_WARN(TAG, "udp_broadcast_connection_init: Initializing UDP server failed, err not ok"); + LOG_WARN(TAG, "%s: Initializing UDP server failed, err not ok", __func__); udp_remove(connection); return err; } udp_recv(connection, udp_receive_callback, NULL); - LOG_INFO(TAG, "udp_broadcast_connection_init: Initializing UDP server successful, callback running"); + LOG_INFO(TAG, "%s: Initializing UDP server successful, callback running", __func__); return err; } @@ -393,7 +392,7 @@ 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; 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"); + LOG_WARN(TAG, "%s: don't give NULL pointers as arguments for the owner's details", __func__); return ERR_ARG; } return udp_broadcast_connection_init(); diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index c32ec1b..f32cb20 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -136,20 +136,12 @@ int main(void) // Initialize the UDP broadcast service - if (udp_broadcast_init(10,255) == ERR_OK){ - goto connected; - } - LOG_WARN(TAG,"error initializing udp connection, trying again in 500ms"); - HAL_Delay(500); - if(udp_broadcast_connection_init() != ERR_OK){ + if (udp_broadcast_init(10,255) != ERR_OK){ LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()"); } - -connected: if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven") != ERR_OK){ LOG_WARN(TAG,"error setting owner's details"); } - /* USER CODE END 2 */ /* Infinite loop */