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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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/77] 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 3e37fe8892d1079319ddd669ce78b55b90a70c1d Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:00:14 +0100 Subject: [PATCH 14/77] Added mug.h with mug_init() --- project/Core/Inc/mug.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 project/Core/Inc/mug.h diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h new file mode 100644 index 0000000..f2cbbe2 --- /dev/null +++ b/project/Core/Inc/mug.h @@ -0,0 +1,12 @@ +/** + * @file mug.h + * @header for mosquitto application of the groups assignment + * @author RobinVdB + */ + +#ifndef INC_MUG_H_ +#define INC_MUG_H_ + +void mug_init(); + +#endif /* INC_MUG_H_ */ From 9aabdac7aaa713a2cbd400ae8d41e8542f8aca01 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:06:45 +0100 Subject: [PATCH 15/77] Added mug.c with functions Functions are: mqtt_pub_request_cb example_publish mqtt_incoming_publish_cb mqtt_incoming_data_cb mqtt_sub_request_cb mqtt_connection_cb example_do_connect --- project/Core/Src/mug.c | 144 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 project/Core/Src/mug.c diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c new file mode 100644 index 0000000..b1eba9a --- /dev/null +++ b/project/Core/Src/mug.c @@ -0,0 +1,144 @@ +/** + * @file mug.c + * @mosquitto application for group assignment + * @author RobinVdB + */ + +#include "mdns.h" +#include "httpd.h" +#include "lwip/apps/fs.h" +#include +#include "lwip/ip_addr.h" +#include "mqtt.h" +#include "mug.h" + +static void mqtt_pub_request_cb(void*, err_t); +void example_publish(mqtt_client_client_t, void*); +static void mqtt_incoming_publish_cb(void*, const char*, u32_t); +static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); +static void mqtt_sub_request_cb(void*, err_t*); +static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); +void example_do_connect(mqtt_client_t*); + + +static void mqtt_pub_request_cb(void *arg, err_t result) +{ + if(result != ERR_OK) { + printf("Publish result: %d\n", result); + } +} + +int teller = 0; +void example_publish(mqtt_client_t *client, void *arg) +{ + printf("testing publish\n"); + const char *pub_payload[20]; + err_t err; + u8_t qos = 2; + u8_t retain = 0; + itoa(teller, &pub_payload, 10); + err = mqtt_publish(client, "pushbutton_event", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + if(err != ERR_OK) { + printf("Publish err: %d\n", err); + } +} + + +static int inpub_id; +static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) +{ + printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); + + //check for which topic a publish was received for + if(strcmp(topic, "/input/setText") == 0) { + inpub_id = 0; + } else if(strcmp(topic, "/input/setImage") == 0) { + inpub_id = 1; + } else if(strcmp(topic, "/input/setTextColor") == 0) { + inpub_id = 2; + } else if(strcmp(topic, "/input/setColor") == 0) { + inpub_id = 3; + } else { + //in case of wrong topic + inpub_id = 4; + } +} + +static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) +{ + printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + + if(flags & MQTT_DATA_FLAG_LAST) { + if (inpub_id == 0) { + //places text on the lcd + } else if (inpub_id == 1) { + //places an image on the lcd + } else if (inpub_id == 2) { + //changes the text color on the lcd + } else if (inpub_id == 3) { + //changes the background color + } else { + printf("mqtt_incoming_data_cb: Ignoring payload...\n"); + } + } else { + } +} + +static void mqtt_sub_request_cb(void *arg, err_t result) +{ + printf("Subscribe result: %d\n", result); +} + +static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) +{ + printf("entered connection cb function\n"); + err_t err; + if(status == MQTT_CONNECT_ACCEPTED) { + printf("mqtt_connection_cb: Successfully connected\n"); + + //set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + + //subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + + if(err != ERR_OK) { + printf("mqtt_subscribe return: %d\n", err); + } + //publish list of images here + } else { + printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); + + //try to reconnect + example_do_connect(client); + } +} + +void example_do_connect(mqtt_client_t *client) +{ + printf("testing the connection\n"); + struct mqtt_connect_client_info_t ci; + err_t err; + + memset(&ci, 0, sizeof(ci)); + + ci.client_id = "lwip_test"; + ip_addr_t server_ip; + IP4_ADDR(&server_ip, 192,168,69,11); + uint16_t server_port = 1883; + err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); + if(err != ERR_OK) { + printf("mqtt_connect return %d\n", err); + } + if(err == ERR_OK) { + printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); + } +} + +void mug_init() { + mqtt_client_t *client = mqtt_client_new(); + if(client != NULL) { + printf("Starting connection test\n"); + example_do_connect(client); + } +} From 15bd0f39691575a016d01d798e2b0e322fd712c5 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:14:00 +0100 Subject: [PATCH 16/77] Fixed position of '{' in mug.c --- project/Core/Src/mug.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index b1eba9a..c85a17f 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -21,16 +21,14 @@ static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); void example_do_connect(mqtt_client_t*); -static void mqtt_pub_request_cb(void *arg, err_t result) -{ +static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { printf("Publish result: %d\n", result); } } int teller = 0; -void example_publish(mqtt_client_t *client, void *arg) -{ +void example_publish(mqtt_client_t *client, void *arg) { printf("testing publish\n"); const char *pub_payload[20]; err_t err; @@ -45,8 +43,7 @@ void example_publish(mqtt_client_t *client, void *arg) static int inpub_id; -static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) -{ +static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); //check for which topic a publish was received for @@ -64,8 +61,7 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } } -static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) -{ +static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST) { @@ -84,13 +80,11 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } } -static void mqtt_sub_request_cb(void *arg, err_t result) -{ +static void mqtt_sub_request_cb(void *arg, err_t result) { printf("Subscribe result: %d\n", result); } -static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) -{ +static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { printf("entered connection cb function\n"); err_t err; if(status == MQTT_CONNECT_ACCEPTED) { @@ -114,8 +108,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } } -void example_do_connect(mqtt_client_t *client) -{ +void example_do_connect(mqtt_client_t *client) { printf("testing the connection\n"); struct mqtt_connect_client_info_t ci; err_t err; @@ -136,9 +129,9 @@ void example_do_connect(mqtt_client_t *client) } void mug_init() { - mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { - printf("Starting connection test\n"); - example_do_connect(client); - } + mqtt_client_t *client = mqtt_client_new(); + if(client != NULL) { + printf("Starting connection test\n"); + example_do_connect(client); + } } From d8e4471e7bd0780696039a20b2a1e94a7ff44be3 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:18:16 +0100 Subject: [PATCH 17/77] solved errors in mug.c --- project/Core/Src/mug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index c85a17f..71917eb 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -13,10 +13,10 @@ #include "mug.h" static void mqtt_pub_request_cb(void*, err_t); -void example_publish(mqtt_client_client_t, void*); +void example_publish(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); -static void mqtt_sub_request_cb(void*, err_t*); +static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); void example_do_connect(mqtt_client_t*); 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 18/77] 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 0aed4429dab7ba9b07527bbff9dddeef431b8db3 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 14:49:03 +0100 Subject: [PATCH 19/77] Updated mug.h --- project/Core/Inc/mug.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index f2cbbe2..5285f89 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,6 +7,6 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ -void mug_init(); +void mug_init(void); #endif /* INC_MUG_H_ */ From 8e47a28ece4481847c5bc6286f7caea69ea1211a Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 14:50:26 +0100 Subject: [PATCH 20/77] Fixed indentation and updated functions --- project/Core/Src/mug.c | 196 +++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 94 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 71917eb..40cc015 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -12,126 +12,134 @@ #include "mqtt.h" #include "mug.h" +#define LOGGER_LEVEL_INFO +#include "log.h" + +//Function prototypes static void mqtt_pub_request_cb(void*, err_t); -void example_publish(mqtt_client_t* , void*); +static void example_publish(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); -void example_do_connect(mqtt_client_t*); +static void example_do_connect(mqtt_client_t*); +//global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics +static int inpub_id; +static const char *TAG = "mug"; static void mqtt_pub_request_cb(void *arg, err_t result) { - if(result != ERR_OK) { - printf("Publish result: %d\n", result); - } + if(result != ERR_OK) { + LOG_INFO(TAG, "Publish result: %d\n", result); + } } -int teller = 0; -void example_publish(mqtt_client_t *client, void *arg) { - printf("testing publish\n"); - const char *pub_payload[20]; - err_t err; - u8_t qos = 2; - u8_t retain = 0; - itoa(teller, &pub_payload, 10); - err = mqtt_publish(client, "pushbutton_event", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); - if(err != ERR_OK) { - printf("Publish err: %d\n", err); - } +static void example_publish(mqtt_client_t *client, void *arg) { + printf("testing publish\n"); + const char *pub_payload[20]; + err_t err; + u8_t qos = 2; + u8_t retain = 0; + + err = mqtt_publish(client, "getImageList", pub_payload, sizeof(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + if(err != ERR_OK) { + printf("Publish err: %d\n", err); + } } - -static int inpub_id; static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); - - //check for which topic a publish was received for - if(strcmp(topic, "/input/setText") == 0) { - inpub_id = 0; - } else if(strcmp(topic, "/input/setImage") == 0) { - inpub_id = 1; - } else if(strcmp(topic, "/input/setTextColor") == 0) { - inpub_id = 2; - } else if(strcmp(topic, "/input/setColor") == 0) { - inpub_id = 3; - } else { - //in case of wrong topic - inpub_id = 4; - } + printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); + //check for which topic a publish was received + if(strcmp(topic, "/input/setText") == 0) { + inpub_id = 0; + } else if(strcmp(topic, "/input/setImage") == 0) { + inpub_id = 1; + } else if(strcmp(topic, "/input/setTextColor") == 0) { + inpub_id = 2; + } else if(strcmp(topic, "/input/setColor") == 0) { + inpub_id = 3; + } else { + //in case of wrong topic + inpub_id = 4; + } } static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + if(flags & MQTT_DATA_FLAG_LAST) { + switch(inpub_id) { + case 0: + //places text on the lcd + break; + case 1: + //places an image on the lcd + break; + case 2: + //changes the text color on the lcd + break; + case 3: + //changes the background color + break; + default: + LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); + } + } else { - if(flags & MQTT_DATA_FLAG_LAST) { - if (inpub_id == 0) { - //places text on the lcd - } else if (inpub_id == 1) { - //places an image on the lcd - } else if (inpub_id == 2) { - //changes the text color on the lcd - } else if (inpub_id == 3) { - //changes the background color - } else { - printf("mqtt_incoming_data_cb: Ignoring payload...\n"); - } - } else { - } + } } static void mqtt_sub_request_cb(void *arg, err_t result) { - printf("Subscribe result: %d\n", result); + LOG_INFO(TAG, "Subscribe result: %d\n", result); } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - printf("entered connection cb function\n"); + printf("entered connection cb function\n"); err_t err; - if(status == MQTT_CONNECT_ACCEPTED) { - printf("mqtt_connection_cb: Successfully connected\n"); + if(status == MQTT_CONNECT_ACCEPTED) { + printf("mqtt_connection_cb: Successfully connected\n"); - //set up callback function for input - mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + //set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - //subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + //subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + if(err != ERR_OK) { + printf("mqtt_subscribe return: %d\n", err); + } + //publish list of images here + example_publish(client, NULL); + } else { + printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); - if(err != ERR_OK) { - printf("mqtt_subscribe return: %d\n", err); - } - //publish list of images here - } else { - printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); - - //try to reconnect - example_do_connect(client); - } -} - -void example_do_connect(mqtt_client_t *client) { - printf("testing the connection\n"); - struct mqtt_connect_client_info_t ci; - err_t err; - - memset(&ci, 0, sizeof(ci)); - - ci.client_id = "lwip_test"; - ip_addr_t server_ip; - IP4_ADDR(&server_ip, 192,168,69,11); - uint16_t server_port = 1883; - err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); - if(err != ERR_OK) { - printf("mqtt_connect return %d\n", err); - } - if(err == ERR_OK) { - printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); - } -} - -void mug_init() { - mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { - printf("Starting connection test\n"); - example_do_connect(client); + //try to reconnect + example_do_connect(client); + } +} + +static void example_do_connect(mqtt_client_t *client) { + printf("testing the connection\n"); + struct mqtt_connect_client_info_t ci; + err_t err; + + memset(&ci, 0, sizeof(ci)); + + ci.client_id = "lwip_test"; + ip_addr_t server_ip; + IP4_ADDR(&server_ip, 192,168,69,11); + uint16_t server_port = 1883; + err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); + if(err != ERR_OK) { + printf("mqtt_connect return %d\n", err); + } + if(err == ERR_OK) { + printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); + } +} + +void mug_init(void) { + mqtt_client_t *client = mqtt_client_new(); + if(client != NULL) { + printf("Starting connection test\n"); + example_do_connect(client); } } 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 21/77] 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 22/77] 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 23/77] 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 24/77] 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 25/77] 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 26/77] 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 27/77] 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 28/77] 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 b79a42b767fc0c4c3fd3f99424296e135dc49541 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 19 Nov 2023 18:23:02 +0100 Subject: [PATCH 29/77] Implemented LCD and filesystem --- project/Core/Src/main.c | 2 + project/Core/Src/mug.c | 169 ++++++++++++++++++++++++++++++++++------ 2 files changed, 147 insertions(+), 24 deletions(-) diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 54e3083..7cfff86 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 "mug.h" /* USER CODE END Includes */ @@ -130,6 +131,7 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ + mug_init(); while (1) { /* USER CODE END WHILE */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 40cc015..f116a7a 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -11,6 +11,7 @@ #include "lwip/ip_addr.h" #include "mqtt.h" #include "mug.h" +#include "lcd_api.h" #define LOGGER_LEVEL_INFO #include "log.h" @@ -19,44 +20,64 @@ static void mqtt_pub_request_cb(void*, err_t); static void example_publish(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); -static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); +static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void example_do_connect(mqtt_client_t*); +static uint32_t color_picker(uint8_t*); //global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static int inpub_id; static const char *TAG = "mug"; +static uint16_t xpos; +static uint16_t ypos; +static sFONT* font = LCD_FONT16; +static uint32_t color; +static uint32_t bgcolor; static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { - LOG_INFO(TAG, "Publish result: %d\n", result); + LOG_INFO(TAG, "Publish result: %d", result); } } static void example_publish(mqtt_client_t *client, void *arg) { - printf("testing publish\n"); - const char *pub_payload[20]; + LOG_INFO(TAG, "Entering publish"); + char pub_payload[200]; err_t err; u8_t qos = 2; - u8_t retain = 0; + u8_t retain = 1 ; + size_t max_files = 20; + llfs_file_t file_list[max_files]; + size_t num_files; - err = mqtt_publish(client, "getImageList", pub_payload, sizeof(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + num_files = llfs_file_list(file_list, max_files, "*.png"); + + if(num_files == 0) { + strcpy(pub_payload, "No images found"); + } else { + for (size_t i = 0; i < num_files; i++) { + // Concatenate file names into the payload string + strcat(pub_payload, file_list[i].name); + strcat(pub_payload, ", "); // Add a space between file names + } + } + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK) { - printf("Publish err: %d\n", err); + LOG_INFO(TAG, "Publish err: %d", err); } } static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); + LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); //check for which topic a publish was received - if(strcmp(topic, "/input/setText") == 0) { + if(strcmp(topic, "input/setText") == 0) { inpub_id = 0; - } else if(strcmp(topic, "/input/setImage") == 0) { + } else if(strcmp(topic, "input/setImage") == 0) { inpub_id = 1; - } else if(strcmp(topic, "/input/setTextColor") == 0) { + } else if(strcmp(topic, "input/setTextColor") == 0) { inpub_id = 2; - } else if(strcmp(topic, "/input/setColor") == 0) { + } else if(strcmp(topic, "input/setColor") == 0) { inpub_id = 3; } else { //in case of wrong topic @@ -65,20 +86,30 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + uint8_t data_buffer[len + 1]; + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST) { + memcpy(data_buffer, data, len); + data_buffer[len] = '\0'; switch(inpub_id) { case 0: - //places text on the lcd + //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); + LOG_INFO(TAG, "incoming data on input/setText:\n %s.", data); + lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: //places an image on the lcd + LOG_INFO(TAG, "incoming data on input/setImage:\n %s.", data); break; case 2: //changes the text color on the lcd + LOG_INFO(TAG, "incoming data on input/setTextColor:\n %s.", data); + color = color_picker(data_buffer); break; case 3: //changes the background color + LOG_INFO(TAG, "incoming data on input/setColor:\n %s.", data); + bgcolor = color_picker(data_buffer); break; default: LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); @@ -89,27 +120,28 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } static void mqtt_sub_request_cb(void *arg, err_t result) { - LOG_INFO(TAG, "Subscribe result: %d\n", result); + LOG_INFO(TAG, "Subscribe result: %d", result); } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - printf("entered connection cb function\n"); + LOG_INFO(TAG, "entered connection cb function"); err_t err; if(status == MQTT_CONNECT_ACCEPTED) { - printf("mqtt_connection_cb: Successfully connected\n"); + LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); //set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); //subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); if(err != ERR_OK) { - printf("mqtt_subscribe return: %d\n", err); + LOG_INFO(TAG, "mqtt_subscribe return: %d", err); } //publish list of images here + example_publish(client, NULL); } else { - printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); + LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); //try to reconnect example_do_connect(client); @@ -117,7 +149,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } static void example_do_connect(mqtt_client_t *client) { - printf("testing the connection\n"); + LOG_INFO(TAG, "testing the connection"); struct mqtt_connect_client_info_t ci; err_t err; @@ -129,17 +161,106 @@ static void example_do_connect(mqtt_client_t *client) { uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if(err != ERR_OK) { - printf("mqtt_connect return %d\n", err); + LOG_INFO(TAG, "mqtt_connect return %d", err); } if(err == ERR_OK) { - printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); + LOG_INFO(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } } void mug_init(void) { + color = LCD_BLACK; + bgcolor = LCD_WHITE; + xpos = 50; + ypos = 50; + mqtt_client_t *client = mqtt_client_new(); if(client != NULL) { - printf("Starting connection test\n"); + LOG_INFO(TAG, "Starting connection test"); example_do_connect(client); } } + +uint32_t color_picker(uint8_t* color) { + uint32_t output = LCD_BLACK; + if(strcmp((const char*)color, "blue") == 0) { + output = LCD_BLUE; + } + if(strcmp((const char*)color, "green") == 0) { + output = LCD_GREEN; + } + if(strcmp((const char*)color, "red") == 0) { + output = LCD_RED; + } + if(strcmp((const char*)color, "cyan") == 0) { + output = LCD_CYAN; + } + if(strcmp((const char*)color, "magenta") == 0) { + output = LCD_MAGENTA; + } + if(strcmp((const char*)color, "yellow") == 0) { + output = LCD_YELLOW; + } + if(strcmp((const char*)color, "light blue") == 0) { + output = LCD_LIGHTBLUE; + } + if(strcmp((const char*)color, "light green") == 0) { + output = LCD_LIGHTGREEN; + } + if(strcmp((const char*)color, "light red") == 0) { + output = LCD_LIGHTRED; + } + if(strcmp((const char*)color, "light cyan") == 0) { + output = LCD_LIGHTCYAN; + } + if(strcmp((const char*)color, "light magenta") == 0) { + output = LCD_LIGHTMAGENTA; + } + if(strcmp((const char*)color, "light yellow") == 0) { + output = LCD_LIGHTYELLOW; + } + if(strcmp((const char*)color, "dark blue") == 0) { + output = LCD_DARKBLUE; + } + if(strcmp((const char*)color, "dark green") == 0) { + output = LCD_DARKGREEN; + } + if(strcmp((const char*)color, "dark red") == 0) { + output = LCD_DARKRED; + } + if(strcmp((const char*)color, "dark cyan") == 0) { + output = LCD_DARKCYAN; + } + if(strcmp((const char*)color, "dark magenta") == 0) { + output = LCD_DARKMAGENTA; + } + if(strcmp((const char*)color, "dark yellow") == 0) { + output = LCD_DARKYELLOW; + } + if(strcmp((const char*)color, "white") == 0) { + output = LCD_WHITE; + } + if(strcmp((const char*)color, "light gray") == 0) { + output = LCD_LIGHTGRAY; + } + if(strcmp((const char*)color, "gray") == 0) { + output = LCD_GRAY; + } + if(strcmp((const char*)color, "dark gray") == 0) { + output = LCD_DARKGRAY; + } + if(strcmp((const char*)color, "black") == 0) { + output = LCD_BLACK; + } + if(strcmp((const char*)color, "brown") == 0) { + output = LCD_BROWN; + } + if(strcmp((const char*)color, "orange") == 0) { + output = LCD_ORANGE; + } + if(strcmp((const char*)color, "transparent") == 0) { + output = LCD_TRANSPARENT; + } + + return output; +} 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 30/77] 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 31/77] 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 32/77] 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 33/77] 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 34/77] 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 35/77] 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 bbbf17b5ca4ff5311eb0d3b69d733b805d3d25c2 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 20 Nov 2023 13:20:46 +0100 Subject: [PATCH 36/77] Added styleguid.xml to editor --- project/Core/Src/mug.c | 318 +++++++++++++++++++++-------------------- 1 file changed, 160 insertions(+), 158 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index f116a7a..4e2bbf1 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -31,7 +31,7 @@ static int inpub_id; static const char *TAG = "mug"; static uint16_t xpos; static uint16_t ypos; -static sFONT* font = LCD_FONT16; +static sFONT* font; static uint32_t color; static uint32_t bgcolor; @@ -54,102 +54,103 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.png"); if(num_files == 0) { - strcpy(pub_payload, "No images found"); + strcpy(pub_payload, "No images found"); } else { - for (size_t i = 0; i < num_files; i++) { - // Concatenate file names into the payload string - strcat(pub_payload, file_list[i].name); - strcat(pub_payload, ", "); // Add a space between file names - } + for (size_t i = 0; i < num_files; i++) { + // Concatenate file names into the payload string + strcat(pub_payload, file_list[i].name); + strcat(pub_payload, ", "); // Add a space between file names + } } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK) { - LOG_INFO(TAG, "Publish err: %d", err); + LOG_INFO(TAG, "Publish err: %d", err); } } static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); - //check for which topic a publish was received - if(strcmp(topic, "input/setText") == 0) { - inpub_id = 0; - } else if(strcmp(topic, "input/setImage") == 0) { - inpub_id = 1; - } else if(strcmp(topic, "input/setTextColor") == 0) { - inpub_id = 2; - } else if(strcmp(topic, "input/setColor") == 0) { - inpub_id = 3; - } else { - //in case of wrong topic - inpub_id = 4; - } + LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); + //check for which topic a publish was received + if(strcmp(topic, "input/setText") == 0) { + inpub_id = 0; + } else if(strcmp(topic, "input/setImage") == 0) { + inpub_id = 1; + } else if(strcmp(topic, "input/setTextColor") == 0) { + inpub_id = 2; + } else if(strcmp(topic, "input/setColor") == 0) { + inpub_id = 3; + } else { + //in case of wrong topic + inpub_id = 4; + } } static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - uint8_t data_buffer[len + 1]; - LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); - if(flags & MQTT_DATA_FLAG_LAST) { - memcpy(data_buffer, data, len); - data_buffer[len] = '\0'; - switch(inpub_id) { - case 0: - //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); - LOG_INFO(TAG, "incoming data on input/setText:\n %s.", data); - lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); - break; - case 1: - //places an image on the lcd - LOG_INFO(TAG, "incoming data on input/setImage:\n %s.", data); - break; - case 2: - //changes the text color on the lcd - LOG_INFO(TAG, "incoming data on input/setTextColor:\n %s.", data); - color = color_picker(data_buffer); - break; - case 3: - //changes the background color - LOG_INFO(TAG, "incoming data on input/setColor:\n %s.", data); - bgcolor = color_picker(data_buffer); - break; - default: - LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); - } - } else { + uint8_t data_buffer[len + 1]; + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); + if(flags & MQTT_DATA_FLAG_LAST) { + memcpy(data_buffer, data, len); + data_buffer[len] = '\0'; + switch(inpub_id) { + case 0: + //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); + LOG_INFO(TAG, "incoming data on input/setText: %s.", data); + lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); + break; + case 1: + //places an image on the lcd + LOG_INFO(TAG, "incoming data on input/setImage: %s.", data); + //call function here + break; + case 2: + //changes the text color on the lcd + LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data); + color = color_picker(data_buffer); + break; + case 3: + //changes the background color + LOG_INFO(TAG, "incoming data on input/setColor: %s.", data); + bgcolor = color_picker(data_buffer); + break; + default: + LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); + } + } else { - } + } } static void mqtt_sub_request_cb(void *arg, err_t result) { - LOG_INFO(TAG, "Subscribe result: %d", result); + LOG_INFO(TAG, "Subscribe result: %d", result); } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - LOG_INFO(TAG, "entered connection cb function"); - err_t err; - if(status == MQTT_CONNECT_ACCEPTED) { - LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); + LOG_INFO(TAG, "entered connection cb function"); + err_t err; + if(status == MQTT_CONNECT_ACCEPTED) { + LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); - //set up callback function for input - mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + //set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - //subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); - if(err != ERR_OK) { - LOG_INFO(TAG, "mqtt_subscribe return: %d", err); - } - //publish list of images here + //subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); + if(err != ERR_OK) { + LOG_INFO(TAG, "mqtt_subscribe return: %d", err); + } + //publish list of images here - example_publish(client, NULL); - } else { - LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); + example_publish(client, NULL); + } else { + LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - //try to reconnect - example_do_connect(client); - } + //try to reconnect + example_do_connect(client); + } } static void example_do_connect(mqtt_client_t *client) { - LOG_INFO(TAG, "testing the connection"); + LOG_INFO(TAG, "testing the connection"); struct mqtt_connect_client_info_t ci; err_t err; @@ -169,98 +170,99 @@ static void example_do_connect(mqtt_client_t *client) { } void mug_init(void) { - color = LCD_BLACK; - bgcolor = LCD_WHITE; - xpos = 50; - ypos = 50; + color = LCD_BLACK; + bgcolor = LCD_WHITE; + font = LCD_FONT16; + xpos = 50; + ypos = 50; mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { + if(client != NULL) { LOG_INFO(TAG, "Starting connection test"); example_do_connect(client); - } + } } uint32_t color_picker(uint8_t* color) { - uint32_t output = LCD_BLACK; - if(strcmp((const char*)color, "blue") == 0) { - output = LCD_BLUE; - } - if(strcmp((const char*)color, "green") == 0) { - output = LCD_GREEN; - } - if(strcmp((const char*)color, "red") == 0) { - output = LCD_RED; - } - if(strcmp((const char*)color, "cyan") == 0) { - output = LCD_CYAN; - } - if(strcmp((const char*)color, "magenta") == 0) { - output = LCD_MAGENTA; - } - if(strcmp((const char*)color, "yellow") == 0) { - output = LCD_YELLOW; - } - if(strcmp((const char*)color, "light blue") == 0) { - output = LCD_LIGHTBLUE; - } - if(strcmp((const char*)color, "light green") == 0) { - output = LCD_LIGHTGREEN; - } - if(strcmp((const char*)color, "light red") == 0) { - output = LCD_LIGHTRED; - } - if(strcmp((const char*)color, "light cyan") == 0) { - output = LCD_LIGHTCYAN; - } - if(strcmp((const char*)color, "light magenta") == 0) { - output = LCD_LIGHTMAGENTA; - } - if(strcmp((const char*)color, "light yellow") == 0) { - output = LCD_LIGHTYELLOW; - } - if(strcmp((const char*)color, "dark blue") == 0) { - output = LCD_DARKBLUE; - } - if(strcmp((const char*)color, "dark green") == 0) { - output = LCD_DARKGREEN; - } - if(strcmp((const char*)color, "dark red") == 0) { - output = LCD_DARKRED; - } - if(strcmp((const char*)color, "dark cyan") == 0) { - output = LCD_DARKCYAN; - } - if(strcmp((const char*)color, "dark magenta") == 0) { - output = LCD_DARKMAGENTA; - } - if(strcmp((const char*)color, "dark yellow") == 0) { - output = LCD_DARKYELLOW; - } - if(strcmp((const char*)color, "white") == 0) { - output = LCD_WHITE; - } - if(strcmp((const char*)color, "light gray") == 0) { - output = LCD_LIGHTGRAY; - } - if(strcmp((const char*)color, "gray") == 0) { - output = LCD_GRAY; - } - if(strcmp((const char*)color, "dark gray") == 0) { - output = LCD_DARKGRAY; - } - if(strcmp((const char*)color, "black") == 0) { - output = LCD_BLACK; - } - if(strcmp((const char*)color, "brown") == 0) { - output = LCD_BROWN; - } - if(strcmp((const char*)color, "orange") == 0) { - output = LCD_ORANGE; - } - if(strcmp((const char*)color, "transparent") == 0) { - output = LCD_TRANSPARENT; - } + uint32_t output = LCD_BLACK; + if(strcmp((const char*)color, "blue") == 0) { + output = LCD_BLUE; + } + if(strcmp((const char*)color, "green") == 0) { + output = LCD_GREEN; + } + if(strcmp((const char*)color, "red") == 0) { + output = LCD_RED; + } + if(strcmp((const char*)color, "cyan") == 0) { + output = LCD_CYAN; + } + if(strcmp((const char*)color, "magenta") == 0) { + output = LCD_MAGENTA; + } + if(strcmp((const char*)color, "yellow") == 0) { + output = LCD_YELLOW; + } + if(strcmp((const char*)color, "light blue") == 0) { + output = LCD_LIGHTBLUE; + } + if(strcmp((const char*)color, "light green") == 0) { + output = LCD_LIGHTGREEN; + } + if(strcmp((const char*)color, "light red") == 0) { + output = LCD_LIGHTRED; + } + if(strcmp((const char*)color, "light cyan") == 0) { + output = LCD_LIGHTCYAN; + } + if(strcmp((const char*)color, "light magenta") == 0) { + output = LCD_LIGHTMAGENTA; + } + if(strcmp((const char*)color, "light yellow") == 0) { + output = LCD_LIGHTYELLOW; + } + if(strcmp((const char*)color, "dark blue") == 0) { + output = LCD_DARKBLUE; + } + if(strcmp((const char*)color, "dark green") == 0) { + output = LCD_DARKGREEN; + } + if(strcmp((const char*)color, "dark red") == 0) { + output = LCD_DARKRED; + } + if(strcmp((const char*)color, "dark cyan") == 0) { + output = LCD_DARKCYAN; + } + if(strcmp((const char*)color, "dark magenta") == 0) { + output = LCD_DARKMAGENTA; + } + if(strcmp((const char*)color, "dark yellow") == 0) { + output = LCD_DARKYELLOW; + } + if(strcmp((const char*)color, "white") == 0) { + output = LCD_WHITE; + } + if(strcmp((const char*)color, "light gray") == 0) { + output = LCD_LIGHTGRAY; + } + if(strcmp((const char*)color, "gray") == 0) { + output = LCD_GRAY; + } + if(strcmp((const char*)color, "dark gray") == 0) { + output = LCD_DARKGRAY; + } + if(strcmp((const char*)color, "black") == 0) { + output = LCD_BLACK; + } + if(strcmp((const char*)color, "brown") == 0) { + output = LCD_BROWN; + } + if(strcmp((const char*)color, "orange") == 0) { + output = LCD_ORANGE; + } + if(strcmp((const char*)color, "transparent") == 0) { + output = LCD_TRANSPARENT; + } - return output; + return output; } From ef4df1e5f569390eabb60ca6da936d26d7a11c84 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 20 Nov 2023 15:15:26 +0100 Subject: [PATCH 37/77] Added image application --- project/Core/Src/mug.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 4e2bbf1..d6fa9f5 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -51,16 +51,32 @@ static void example_publish(mqtt_client_t *client, void *arg) { llfs_file_t file_list[max_files]; size_t num_files; - num_files = llfs_file_list(file_list, max_files, "*.png"); + num_files = llfs_file_list(file_list, max_files, "*.bmp"); if(num_files == 0) { strcpy(pub_payload, "No images found"); } else { + strcat(pub_payload, "Available images: "); for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string strcat(pub_payload, file_list[i].name); - strcat(pub_payload, ", "); // Add a space between file names + strcat(pub_payload, ", "); // Add a comma between file names } + strcat(pub_payload, "\0"); + } + + num_files = llfs_file_list(file_list, max_files, "*.gif"); + + if(num_files == 0) { + strcpy(pub_payload, "No gifs found"); + } else { + strcat(pub_payload, "Available gifs: "); + for (size_t i = 0; i < num_files; i++) { + // Concatenate file names into the payload string + strcat(pub_payload, file_list[i].name); + strcat(pub_payload, ", "); // Add a comma between file names + } + strcat(pub_payload, "\0"); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK) { @@ -94,22 +110,22 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f switch(inpub_id) { case 0: //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); - LOG_INFO(TAG, "incoming data on input/setText: %s.", data); + LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: //places an image on the lcd - LOG_INFO(TAG, "incoming data on input/setImage: %s.", data); - //call function here + LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); break; case 2: //changes the text color on the lcd - LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data); + LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); color = color_picker(data_buffer); break; case 3: //changes the background color - LOG_INFO(TAG, "incoming data on input/setColor: %s.", data); + LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); bgcolor = color_picker(data_buffer); break; default: From b52e7b0bc8c4894072d6c0a53de9be8510953950 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 20 Nov 2023 15:42:43 +0100 Subject: [PATCH 38/77] Added support for gifs --- project/Core/Src/mug.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index d6fa9f5..c6d0843 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -53,6 +53,8 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); + strcpy(pub_payload, "\0"); + if(num_files == 0) { strcpy(pub_payload, "No images found"); } else { @@ -116,7 +118,12 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f case 1: //places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); - lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + if(data_buffer[len-3] == 'b') { + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + } + if(data_buffer[len-3] == 'g') { + lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + } break; case 2: //changes the text color on the lcd 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 39/77] 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 40/77] 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 41/77] 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 4da408dadf25cc41fe6ab8b16cbc9b80d92b12ce Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Thu, 23 Nov 2023 17:57:26 +0100 Subject: [PATCH 42/77] Added lcd_clear_text() and lcd_clear_images() --- project/Core/Src/mug.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index c6d0843..a8c887a 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -24,7 +24,7 @@ static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void example_do_connect(mqtt_client_t*); -static uint32_t color_picker(uint8_t*); +static uint32_t color_picker(char*); //global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static int inpub_id; @@ -41,6 +41,10 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { } } +/** + * @brief Publishes data + * Publishes the names of all the .bmp and .gif files on the topic getImageList + */ static void example_publish(mqtt_client_t *client, void *arg) { LOG_INFO(TAG, "Entering publish"); char pub_payload[200]; @@ -86,6 +90,10 @@ static void example_publish(mqtt_client_t *client, void *arg) { } } +/** + * @brief Handles incoming publish + * Callback function for when data was published to a subscribed topic + */ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); //check for which topic a publish was received @@ -103,21 +111,27 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } } +/** + * @brief Handles incoming publish data + * Handles the recieved data from a publish to a subscribed topic + */ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - uint8_t data_buffer[len + 1]; + char data_buffer[len + 1]; LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST) { memcpy(data_buffer, data, len); data_buffer[len] = '\0'; switch(inpub_id) { case 0: - //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); + //places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); - lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); + lcd_clear_text(); + lcd_display_text((const char*)data_buffer, xpos, ypos, color, font); break; case 1: //places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); + lcd_clear_images(); if(data_buffer[len-3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } @@ -126,12 +140,12 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } break; case 2: - //changes the text color on the lcd + //changes the text color for the next time text is written LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); color = color_picker(data_buffer); break; case 3: - //changes the background color + //changes the background color for the next time text is written LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); bgcolor = color_picker(data_buffer); break; @@ -143,6 +157,9 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } } +/** + * @brief Callback function for outgoing subscribe request + */ static void mqtt_sub_request_cb(void *arg, err_t result) { LOG_INFO(TAG, "Subscribe result: %d", result); } @@ -161,8 +178,8 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection if(err != ERR_OK) { LOG_INFO(TAG, "mqtt_subscribe return: %d", err); } - //publish list of images here + //publish list of images here example_publish(client, NULL); } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); @@ -206,7 +223,7 @@ void mug_init(void) { } } -uint32_t color_picker(uint8_t* color) { +uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; if(strcmp((const char*)color, "blue") == 0) { output = LCD_BLUE; From 7f5ba2a6f76832bab428d5f8efa0b70e8246d3e1 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Thu, 23 Nov 2023 18:57:53 +0100 Subject: [PATCH 43/77] Started implementing style guide --- project/Core/Src/mug.c | 125 +++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 60 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index a8c887a..0eae03d 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -18,22 +18,22 @@ //Function prototypes static void mqtt_pub_request_cb(void*, err_t); -static void example_publish(mqtt_client_t* , void*); +static void publish_data(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); -static void example_do_connect(mqtt_client_t*); +static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); -//global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics -static int inpub_id; -static const char *TAG = "mug"; +//global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics +static sFONT* font; static uint16_t xpos; static uint16_t ypos; -static sFONT* font; static uint32_t color; static uint32_t bgcolor; +static int inpub_id; +static const char *TAG = "mug"; static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { @@ -45,21 +45,22 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { * @brief Publishes data * Publishes the names of all the .bmp and .gif files on the topic getImageList */ -static void example_publish(mqtt_client_t *client, void *arg) { - LOG_INFO(TAG, "Entering publish"); +static void publish_data(mqtt_client_t *client, void *arg) { char pub_payload[200]; err_t err; + size_t max_files = 20; + size_t num_files; + llfs_file_t file_list[max_files]; u8_t qos = 2; u8_t retain = 1 ; - size_t max_files = 20; - llfs_file_t file_list[max_files]; - size_t num_files; + + LOG_DEBUG(TAG, "Entering publish"); num_files = llfs_file_list(file_list, max_files, "*.bmp"); strcpy(pub_payload, "\0"); - if(num_files == 0) { + if (num_files == 0) { strcpy(pub_payload, "No images found"); } else { strcat(pub_payload, "Available images: "); @@ -73,7 +74,7 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.gif"); - if(num_files == 0) { + if (num_files == 0) { strcpy(pub_payload, "No gifs found"); } else { strcat(pub_payload, "Available gifs: "); @@ -85,7 +86,7 @@ static void example_publish(mqtt_client_t *client, void *arg) { strcat(pub_payload, "\0"); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); - if(err != ERR_OK) { + if (err != ERR_OK) { LOG_INFO(TAG, "Publish err: %d", err); } } @@ -94,16 +95,16 @@ static void example_publish(mqtt_client_t *client, void *arg) { * @brief Handles incoming publish * Callback function for when data was published to a subscribed topic */ -static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); +static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_len) { + LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); //check for which topic a publish was received - if(strcmp(topic, "input/setText") == 0) { + if (strcmp(topic, "input/setText") == 0) { inpub_id = 0; - } else if(strcmp(topic, "input/setImage") == 0) { + } else if (strcmp(topic, "input/setImage") == 0) { inpub_id = 1; - } else if(strcmp(topic, "input/setTextColor") == 0) { + } else if (strcmp(topic, "input/setTextColor") == 0) { inpub_id = 2; - } else if(strcmp(topic, "input/setColor") == 0) { + } else if (strcmp(topic, "input/setColor") == 0) { inpub_id = 3; } else { //in case of wrong topic @@ -115,13 +116,14 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len * @brief Handles incoming publish data * Handles the recieved data from a publish to a subscribed topic */ -static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { +static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); - if(flags & MQTT_DATA_FLAG_LAST) { + if (flags & MQTT_DATA_FLAG_LAST) { memcpy(data_buffer, data, len); data_buffer[len] = '\0'; - switch(inpub_id) { + switch (inpub_id) { case 0: //places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); @@ -165,9 +167,10 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - LOG_INFO(TAG, "entered connection cb function"); err_t err; - if(status == MQTT_CONNECT_ACCEPTED) { + + LOG_INFO(TAG, "entered connection cb function"); + if (status == MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); //set up callback function for input @@ -175,25 +178,26 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection //subscribe to the topics setText, setImage, setColor and setTextcolor err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); - if(err != ERR_OK) { + if (err != ERR_OK) { LOG_INFO(TAG, "mqtt_subscribe return: %d", err); } //publish list of images here - example_publish(client, NULL); + publish_data(client, NULL); } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); //try to reconnect - example_do_connect(client); + mosquitto_connect(client); } } -static void example_do_connect(mqtt_client_t *client) { - LOG_INFO(TAG, "testing the connection"); +static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; err_t err; + LOG_INFO(TAG, "testing the connection"); + memset(&ci, 0, sizeof(ci)); ci.client_id = "lwip_test"; @@ -201,10 +205,10 @@ static void example_do_connect(mqtt_client_t *client) { IP4_ADDR(&server_ip, 192,168,69,11); uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); - if(err != ERR_OK) { + if (err != ERR_OK) { LOG_INFO(TAG, "mqtt_connect return %d", err); } - if(err == ERR_OK) { + if (err == ERR_OK) { LOG_INFO(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } } @@ -217,90 +221,91 @@ void mug_init(void) { ypos = 50; mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { + if (client != NULL) { LOG_INFO(TAG, "Starting connection test"); - example_do_connect(client); + mosquitto_connect(client); } } uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; - if(strcmp((const char*)color, "blue") == 0) { + + if (strcmp((const char*)color, "blue") == 0) { output = LCD_BLUE; } - if(strcmp((const char*)color, "green") == 0) { + if (strcmp((const char*)color, "green") == 0) { output = LCD_GREEN; } - if(strcmp((const char*)color, "red") == 0) { + if (strcmp((const char*)color, "red") == 0) { output = LCD_RED; } - if(strcmp((const char*)color, "cyan") == 0) { + if (strcmp((const char*)color, "cyan") == 0) { output = LCD_CYAN; } - if(strcmp((const char*)color, "magenta") == 0) { + if (strcmp((const char*)color, "magenta") == 0) { output = LCD_MAGENTA; } - if(strcmp((const char*)color, "yellow") == 0) { + if (strcmp((const char*)color, "yellow") == 0) { output = LCD_YELLOW; } - if(strcmp((const char*)color, "light blue") == 0) { + if (strcmp((const char*)color, "light blue") == 0) { output = LCD_LIGHTBLUE; } - if(strcmp((const char*)color, "light green") == 0) { + if (strcmp((const char*)color, "light green") == 0) { output = LCD_LIGHTGREEN; } - if(strcmp((const char*)color, "light red") == 0) { + if (strcmp((const char*)color, "light red") == 0) { output = LCD_LIGHTRED; } - if(strcmp((const char*)color, "light cyan") == 0) { + if (strcmp((const char*)color, "light cyan") == 0) { output = LCD_LIGHTCYAN; } - if(strcmp((const char*)color, "light magenta") == 0) { + if (strcmp((const char*)color, "light magenta") == 0) { output = LCD_LIGHTMAGENTA; } - if(strcmp((const char*)color, "light yellow") == 0) { + if (strcmp((const char*)color, "light yellow") == 0) { output = LCD_LIGHTYELLOW; } - if(strcmp((const char*)color, "dark blue") == 0) { + if (strcmp((const char*)color, "dark blue") == 0) { output = LCD_DARKBLUE; } - if(strcmp((const char*)color, "dark green") == 0) { + if (strcmp((const char*)color, "dark green") == 0) { output = LCD_DARKGREEN; } - if(strcmp((const char*)color, "dark red") == 0) { + if (strcmp((const char*)color, "dark red") == 0) { output = LCD_DARKRED; } - if(strcmp((const char*)color, "dark cyan") == 0) { + if (strcmp((const char*)color, "dark cyan") == 0) { output = LCD_DARKCYAN; } - if(strcmp((const char*)color, "dark magenta") == 0) { + if (strcmp((const char*)color, "dark magenta") == 0) { output = LCD_DARKMAGENTA; } - if(strcmp((const char*)color, "dark yellow") == 0) { + if (strcmp((const char*)color, "dark yellow") == 0) { output = LCD_DARKYELLOW; } - if(strcmp((const char*)color, "white") == 0) { + if (strcmp((const char*)color, "white") == 0) { output = LCD_WHITE; } - if(strcmp((const char*)color, "light gray") == 0) { + if (strcmp((const char*)color, "light gray") == 0) { output = LCD_LIGHTGRAY; } - if(strcmp((const char*)color, "gray") == 0) { + if (strcmp((const char*)color, "gray") == 0) { output = LCD_GRAY; } - if(strcmp((const char*)color, "dark gray") == 0) { + if (strcmp((const char*)color, "dark gray") == 0) { output = LCD_DARKGRAY; } - if(strcmp((const char*)color, "black") == 0) { + if (strcmp((const char*)color, "black") == 0) { output = LCD_BLACK; } - if(strcmp((const char*)color, "brown") == 0) { + if (strcmp((const char*)color, "brown") == 0) { output = LCD_BROWN; } - if(strcmp((const char*)color, "orange") == 0) { + if (strcmp((const char*)color, "orange") == 0) { output = LCD_ORANGE; } - if(strcmp((const char*)color, "transparent") == 0) { + if (strcmp((const char*)color, "transparent") == 0) { output = LCD_TRANSPARENT; } From fb8abbb7532e650ba48257eee754ba3e68726484 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 24 Nov 2023 02:27:31 +0100 Subject: [PATCH 44/77] MQTT Change color_picker by using if is blue return color blue --- project/Core/Src/mug.c | 55 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index a8c887a..3dc8522 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -224,85 +224,84 @@ void mug_init(void) { } uint32_t color_picker(char* color) { - uint32_t output = LCD_BLACK; if(strcmp((const char*)color, "blue") == 0) { - output = LCD_BLUE; + return LCD_BLUE; } if(strcmp((const char*)color, "green") == 0) { - output = LCD_GREEN; + return LCD_GREEN; } if(strcmp((const char*)color, "red") == 0) { - output = LCD_RED; + return LCD_RED; } if(strcmp((const char*)color, "cyan") == 0) { - output = LCD_CYAN; + return LCD_CYAN; } if(strcmp((const char*)color, "magenta") == 0) { - output = LCD_MAGENTA; + return LCD_MAGENTA; } if(strcmp((const char*)color, "yellow") == 0) { - output = LCD_YELLOW; + return LCD_YELLOW; } if(strcmp((const char*)color, "light blue") == 0) { - output = LCD_LIGHTBLUE; + return LCD_LIGHTBLUE; } if(strcmp((const char*)color, "light green") == 0) { - output = LCD_LIGHTGREEN; + return LCD_LIGHTGREEN; } if(strcmp((const char*)color, "light red") == 0) { - output = LCD_LIGHTRED; + return LCD_LIGHTRED; } if(strcmp((const char*)color, "light cyan") == 0) { - output = LCD_LIGHTCYAN; + return LCD_LIGHTCYAN; } if(strcmp((const char*)color, "light magenta") == 0) { - output = LCD_LIGHTMAGENTA; + return LCD_LIGHTMAGENTA; } if(strcmp((const char*)color, "light yellow") == 0) { - output = LCD_LIGHTYELLOW; + return LCD_LIGHTYELLOW; } if(strcmp((const char*)color, "dark blue") == 0) { - output = LCD_DARKBLUE; + return LCD_DARKBLUE; } if(strcmp((const char*)color, "dark green") == 0) { - output = LCD_DARKGREEN; + return LCD_DARKGREEN; } if(strcmp((const char*)color, "dark red") == 0) { - output = LCD_DARKRED; + return LCD_DARKRED; } if(strcmp((const char*)color, "dark cyan") == 0) { - output = LCD_DARKCYAN; + return LCD_DARKCYAN; } if(strcmp((const char*)color, "dark magenta") == 0) { - output = LCD_DARKMAGENTA; + return LCD_DARKMAGENTA; } if(strcmp((const char*)color, "dark yellow") == 0) { - output = LCD_DARKYELLOW; + return LCD_DARKYELLOW; } if(strcmp((const char*)color, "white") == 0) { - output = LCD_WHITE; + return LCD_WHITE; } if(strcmp((const char*)color, "light gray") == 0) { - output = LCD_LIGHTGRAY; + return LCD_LIGHTGRAY; } if(strcmp((const char*)color, "gray") == 0) { - output = LCD_GRAY; + return LCD_GRAY; } if(strcmp((const char*)color, "dark gray") == 0) { - output = LCD_DARKGRAY; + return LCD_DARKGRAY; } if(strcmp((const char*)color, "black") == 0) { - output = LCD_BLACK; + return LCD_BLACK; } if(strcmp((const char*)color, "brown") == 0) { - output = LCD_BROWN; + return LCD_BROWN; } if(strcmp((const char*)color, "orange") == 0) { - output = LCD_ORANGE; + return LCD_ORANGE; } if(strcmp((const char*)color, "transparent") == 0) { - output = LCD_TRANSPARENT; + return LCD_TRANSPARENT; } - return output; + return LCD_BLACK; } From d819c82e59ffa3ca819130c216779731870f4069 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 24 Nov 2023 02:28:31 +0100 Subject: [PATCH 45/77] MQTT Change payload clearing to initialisation instead of using strcpy --- project/Core/Src/mug.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 3dc8522..9553afe 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -47,7 +47,7 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { */ static void example_publish(mqtt_client_t *client, void *arg) { LOG_INFO(TAG, "Entering publish"); - char pub_payload[200]; + char pub_payload[200] = {0}; err_t err; u8_t qos = 2; u8_t retain = 1 ; @@ -57,8 +57,6 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); - strcpy(pub_payload, "\0"); - if(num_files == 0) { strcpy(pub_payload, "No images found"); } else { From 21f30bfd557ed089cd8e89a5a6ebb830155ac3c6 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Fri, 24 Nov 2023 17:34:24 +0100 Subject: [PATCH 46/77] Added documentation --- project/Core/Src/mug.c | 72 +++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 0eae03d..e50afbf 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -46,7 +46,7 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { * Publishes the names of all the .bmp and .gif files on the topic getImageList */ static void publish_data(mqtt_client_t *client, void *arg) { - char pub_payload[200]; + char pub_payload[200] = {0}; err_t err; size_t max_files = 20; size_t num_files; @@ -58,8 +58,6 @@ static void publish_data(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); - strcpy(pub_payload, "\0"); - if (num_files == 0) { strcpy(pub_payload, "No images found"); } else { @@ -166,6 +164,11 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { LOG_INFO(TAG, "Subscribe result: %d", result); } +/** + * @brief Callback function for attempting a connection + * If a connection was made setup a callback function for incoming publishes. + * subscribes to the input topics and calls the publish_data function. + */ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { err_t err; @@ -192,6 +195,10 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } } +/** + * @brief Attempts to create a connection to the mosquitto broker + * Creates a mqtt client and sets up a connection callback function + */ static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; err_t err; @@ -213,6 +220,10 @@ static void mosquitto_connect(mqtt_client_t *client) { } } +/** + * @brief Init function for the mosquitto application of the assignment + * Gives the global variables a value and calls the mosquitto_connect function + */ void mug_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; @@ -227,86 +238,89 @@ void mug_init(void) { } } +/** + * @brief Reads the color input string and outputs it to a useable value for LCD_APi + */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; if (strcmp((const char*)color, "blue") == 0) { - output = LCD_BLUE; + return LCD_BLUE; } if (strcmp((const char*)color, "green") == 0) { - output = LCD_GREEN; + return LCD_GREEN; } if (strcmp((const char*)color, "red") == 0) { - output = LCD_RED; + return LCD_RED; } if (strcmp((const char*)color, "cyan") == 0) { - output = LCD_CYAN; + return LCD_CYAN; } if (strcmp((const char*)color, "magenta") == 0) { - output = LCD_MAGENTA; + return LCD_MAGENTA; } if (strcmp((const char*)color, "yellow") == 0) { - output = LCD_YELLOW; + return LCD_YELLOW; } if (strcmp((const char*)color, "light blue") == 0) { - output = LCD_LIGHTBLUE; + return LCD_LIGHTBLUE; } if (strcmp((const char*)color, "light green") == 0) { - output = LCD_LIGHTGREEN; + return LCD_LIGHTGREEN; } if (strcmp((const char*)color, "light red") == 0) { - output = LCD_LIGHTRED; + return LCD_LIGHTRED; } if (strcmp((const char*)color, "light cyan") == 0) { - output = LCD_LIGHTCYAN; + return LCD_LIGHTCYAN; } if (strcmp((const char*)color, "light magenta") == 0) { - output = LCD_LIGHTMAGENTA; + return LCD_LIGHTMAGENTA; } if (strcmp((const char*)color, "light yellow") == 0) { - output = LCD_LIGHTYELLOW; + return LCD_LIGHTYELLOW; } if (strcmp((const char*)color, "dark blue") == 0) { - output = LCD_DARKBLUE; + return LCD_DARKBLUE; } if (strcmp((const char*)color, "dark green") == 0) { - output = LCD_DARKGREEN; + return LCD_DARKGREEN; } if (strcmp((const char*)color, "dark red") == 0) { - output = LCD_DARKRED; + return LCD_DARKRED; } if (strcmp((const char*)color, "dark cyan") == 0) { - output = LCD_DARKCYAN; + return LCD_DARKCYAN; } if (strcmp((const char*)color, "dark magenta") == 0) { - output = LCD_DARKMAGENTA; + return LCD_DARKMAGENTA; } if (strcmp((const char*)color, "dark yellow") == 0) { - output = LCD_DARKYELLOW; + return LCD_DARKYELLOW; } if (strcmp((const char*)color, "white") == 0) { - output = LCD_WHITE; + return LCD_WHITE; } if (strcmp((const char*)color, "light gray") == 0) { - output = LCD_LIGHTGRAY; + return LCD_LIGHTGRAY; } if (strcmp((const char*)color, "gray") == 0) { - output = LCD_GRAY; + return LCD_GRAY; } if (strcmp((const char*)color, "dark gray") == 0) { - output = LCD_DARKGRAY; + return LCD_DARKGRAY; } if (strcmp((const char*)color, "black") == 0) { - output = LCD_BLACK; + return LCD_BLACK; } if (strcmp((const char*)color, "brown") == 0) { - output = LCD_BROWN; + return LCD_BROWN; } if (strcmp((const char*)color, "orange") == 0) { - output = LCD_ORANGE; + return LCD_ORANGE; } if (strcmp((const char*)color, "transparent") == 0) { - output = LCD_TRANSPARENT; + return LCD_TRANSPARENT; } return output; From 7fc1560bb730ba3ac4e9764ef002b83d58a94c69 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Fri, 24 Nov 2023 17:55:51 +0100 Subject: [PATCH 47/77] Changed some LOG_INFO to LOG_DEBUG --- project/Core/Src/mug.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index e50afbf..002cc7b 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -37,7 +37,7 @@ static const char *TAG = "mug"; static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { - LOG_INFO(TAG, "Publish result: %d", result); + LOG_DEBUG(TAG, "Publish result: %d", result); } } @@ -85,7 +85,7 @@ static void publish_data(mqtt_client_t *client, void *arg) { } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { - LOG_INFO(TAG, "Publish err: %d", err); + LOG_DEBUG(TAG, "Publish err: %d", err); } } @@ -126,12 +126,13 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, //places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); lcd_clear_text(); - lcd_display_text((const char*)data_buffer, xpos, ypos, color, font); + lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: //places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); + lcd_set_bg_color_layer0(bgcolor); if(data_buffer[len-3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } @@ -161,7 +162,7 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, * @brief Callback function for outgoing subscribe request */ static void mqtt_sub_request_cb(void *arg, err_t result) { - LOG_INFO(TAG, "Subscribe result: %d", result); + LOG_DEBUG(TAG, "Subscribe result: %d", result); } /** @@ -172,9 +173,8 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { err_t err; - LOG_INFO(TAG, "entered connection cb function"); if (status == MQTT_CONNECT_ACCEPTED) { - LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); + LOG_INFO(TAG, "Successfully connected"); //set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); @@ -182,7 +182,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection //subscribe to the topics setText, setImage, setColor and setTextcolor err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); if (err != ERR_OK) { - LOG_INFO(TAG, "mqtt_subscribe return: %d", err); + LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); } //publish list of images here @@ -203,20 +203,20 @@ static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; err_t err; - LOG_INFO(TAG, "testing the connection"); + LOG_INFO(TAG, "Attempting MQTT Connection"); memset(&ci, 0, sizeof(ci)); - ci.client_id = "lwip_test"; + ci.client_id = "STM32"; ip_addr_t server_ip; IP4_ADDR(&server_ip, 192,168,69,11); uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { - LOG_INFO(TAG, "mqtt_connect return %d", err); + LOG_DEBUG(TAG, "mqtt_connect return %d", err); } if (err == ERR_OK) { - LOG_INFO(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); + LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } } @@ -233,7 +233,7 @@ void mug_init(void) { mqtt_client_t *client = mqtt_client_new(); if (client != NULL) { - LOG_INFO(TAG, "Starting connection test"); + LOG_DEBUG(TAG, "Starting connection test"); mosquitto_connect(client); } } From e6dcbebdc8ffb9958884a1675ec46fd52a4ef932 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Fri, 24 Nov 2023 18:00:02 +0100 Subject: [PATCH 48/77] Fixed infinit loop when MQTT broker closed --- project/Core/Src/mug.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 002cc7b..160560c 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -30,6 +30,7 @@ static uint32_t color_picker(char*); static sFONT* font; static uint16_t xpos; static uint16_t ypos; +static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; static int inpub_id; @@ -190,8 +191,12 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - //try to reconnect - mosquitto_connect(client); + while (connection_attempt_counter < 50) { + connection_attempt_counter++; + //try to reconnect + mosquitto_connect(client); + } + } } @@ -230,6 +235,7 @@ void mug_init(void) { font = LCD_FONT16; xpos = 50; ypos = 50; + connection_attempt_counter = 0; mqtt_client_t *client = mqtt_client_new(); if (client != NULL) { 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 49/77] 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 50/77] 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 51/77] 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 52/77] 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 60b9b53b28b5b13e3d9572eb3ebbb8e639477867 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 17:24:45 +0100 Subject: [PATCH 53/77] Added reset for connection_attempt_counter --- project/Core/Src/mug.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 160560c..cfe8603 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -177,6 +177,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection if (status == MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "Successfully connected"); + connection_attempt_counter = 0; //set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); From 4147f53802f7beb415982aaf06ba34d5d81b6944 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 17:41:25 +0100 Subject: [PATCH 54/77] Added documentation to .h file --- project/Core/Inc/mug.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index 5285f89..788610e 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,6 +7,8 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ +/* @brief Initialise MQTT application + */ void mug_init(void); #endif /* INC_MUG_H_ */ From 7cbee6afbbcb3bf3fbf31c5e033d994d71828ba0 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 20:01:45 +0100 Subject: [PATCH 55/77] Added parameter info to doxygen --- project/Core/Src/mug.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index cfe8603..b830ca1 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -36,6 +36,12 @@ static uint32_t bgcolor; static int inpub_id; static const char *TAG = "mug"; +/** + * @brief callback function for publishing data + * + * @param[in] arg + * @param[in] result whether the publish was successfull or not + */ static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { LOG_DEBUG(TAG, "Publish result: %d", result); @@ -45,6 +51,9 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { /** * @brief Publishes data * Publishes the names of all the .bmp and .gif files on the topic getImageList + * + * @param[in] client pointer to the MQTT client + * @param[in] arg */ static void publish_data(mqtt_client_t *client, void *arg) { char pub_payload[200] = {0}; @@ -93,6 +102,10 @@ static void publish_data(mqtt_client_t *client, void *arg) { /** * @brief Handles incoming publish * Callback function for when data was published to a subscribed topic + * + * @param[in] arg + * @param[in] topic The topic on which an incomming publish was received + * @param[in] tot_len length of the incoming data */ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_len) { LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); @@ -114,6 +127,11 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_ /** * @brief Handles incoming publish data * Handles the recieved data from a publish to a subscribed topic + * + * @param[in] arg + * @param[in] data the incoming data + * @param[in] len length of the data + * @param[in] flags Whether this is the last fragment of the incoming data */ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; @@ -161,6 +179,9 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, /** * @brief Callback function for outgoing subscribe request + * + * @param[in] arg + * @param[in] result */ static void mqtt_sub_request_cb(void *arg, err_t result) { LOG_DEBUG(TAG, "Subscribe result: %d", result); @@ -170,6 +191,10 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { * @brief Callback function for attempting a connection * If a connection was made setup a callback function for incoming publishes. * subscribes to the input topics and calls the publish_data function. + * + * @param[in] client pointer to the MQTT client + * @param[in] arg + * @param[in] status Connect result code or disconnection notification */ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { err_t err; @@ -204,6 +229,8 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection /** * @brief Attempts to create a connection to the mosquitto broker * Creates a mqtt client and sets up a connection callback function + * + * @param[in] client pointer to the MQTT client */ static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; @@ -247,6 +274,9 @@ void mug_init(void) { /** * @brief Reads the color input string and outputs it to a useable value for LCD_APi + * + * @param[out] color define to use with the LCD_API + * @param[in] color input string to select a color */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; From ee1753b43e77acb90ecce9c96d005fd6f626d14e Mon Sep 17 00:00:00 2001 From: L-diy Date: Sat, 25 Nov 2023 20:24:57 +0100 Subject: [PATCH 56/77] Reformat `mug.c` and `mug.h` --- project/Core/Inc/mug.h | 3 +- project/Core/Src/mug.c | 86 +++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index 788610e..ba1baa8 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,7 +7,8 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ -/* @brief Initialise MQTT application +/** + * @brief Initialise MQTT application */ void mug_init(void); diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index b830ca1..61bcf24 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -4,21 +4,21 @@ * @author RobinVdB */ -#include "mdns.h" -#include "httpd.h" -#include "lwip/apps/fs.h" #include +#include "httpd.h" +#include "lcd_api.h" +#include "lwip/apps/fs.h" #include "lwip/ip_addr.h" +#include "mdns.h" #include "mqtt.h" #include "mug.h" -#include "lcd_api.h" #define LOGGER_LEVEL_INFO #include "log.h" -//Function prototypes +// Function prototypes static void mqtt_pub_request_cb(void*, err_t); -static void publish_data(mqtt_client_t* , void*); +static void publish_data(mqtt_client_t*, void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); @@ -26,7 +26,7 @@ static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); -//global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics +// Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static sFONT* font; static uint16_t xpos; static uint16_t ypos; @@ -34,16 +34,16 @@ static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; static int inpub_id; -static const char *TAG = "mug"; +static const char* TAG = "mug"; /** * @brief callback function for publishing data * * @param[in] arg - * @param[in] result whether the publish was successfull or not + * @param[in] result Whether the publish was successful or not */ -static void mqtt_pub_request_cb(void *arg, err_t result) { - if(result != ERR_OK) { +static void mqtt_pub_request_cb(void* arg, err_t result) { + if (result != ERR_OK) { LOG_DEBUG(TAG, "Publish result: %d", result); } } @@ -52,17 +52,17 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { * @brief Publishes data * Publishes the names of all the .bmp and .gif files on the topic getImageList * - * @param[in] client pointer to the MQTT client + * @param[in] client Pointer to the MQTT client * @param[in] arg */ -static void publish_data(mqtt_client_t *client, void *arg) { +static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; err_t err; size_t max_files = 20; size_t num_files; llfs_file_t file_list[max_files]; u8_t qos = 2; - u8_t retain = 1 ; + u8_t retain = 1; LOG_DEBUG(TAG, "Entering publish"); @@ -104,12 +104,12 @@ static void publish_data(mqtt_client_t *client, void *arg) { * Callback function for when data was published to a subscribed topic * * @param[in] arg - * @param[in] topic The topic on which an incomming publish was received - * @param[in] tot_len length of the incoming data + * @param[in] topic The topic on which an incoming publish was received + * @param[in] tot_len Length of the incoming data */ -static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_len) { +static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_len) { LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); - //check for which topic a publish was received + // Check for which topic a publish was received if (strcmp(topic, "input/setText") == 0) { inpub_id = 0; } else if (strcmp(topic, "input/setImage") == 0) { @@ -119,7 +119,7 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_ } else if (strcmp(topic, "input/setColor") == 0) { inpub_id = 3; } else { - //in case of wrong topic + // In case of wrong topic inpub_id = 4; } } @@ -129,11 +129,11 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_ * Handles the recieved data from a publish to a subscribed topic * * @param[in] arg - * @param[in] data the incoming data - * @param[in] len length of the data + * @param[in] data The incoming data + * @param[in] len Length of the data * @param[in] flags Whether this is the last fragment of the incoming data */ -static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, uint8_t flags) { +static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); @@ -142,30 +142,30 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, data_buffer[len] = '\0'; switch (inpub_id) { case 0: - //places text on the lcd + // Places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); lcd_clear_text(); lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: - //places an image on the lcd + // Places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); lcd_set_bg_color_layer0(bgcolor); - if(data_buffer[len-3] == 'b') { + if (data_buffer[len - 3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } - if(data_buffer[len-3] == 'g') { + if (data_buffer[len - 3] == 'g') { lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); } break; case 2: - //changes the text color for the next time text is written + // Changes the text color for the next time text is written LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); color = color_picker(data_buffer); break; case 3: - //changes the background color for the next time text is written + // Changes the background color for the next time text is written LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); bgcolor = color_picker(data_buffer); break; @@ -173,7 +173,6 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); } } else { - } } @@ -183,7 +182,7 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, * @param[in] arg * @param[in] result */ -static void mqtt_sub_request_cb(void *arg, err_t result) { +static void mqtt_sub_request_cb(void* arg, err_t result) { LOG_DEBUG(TAG, "Subscribe result: %d", result); } @@ -192,37 +191,36 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { * If a connection was made setup a callback function for incoming publishes. * subscribes to the input topics and calls the publish_data function. * - * @param[in] client pointer to the MQTT client + * @param[in] client Pointer to the MQTT client * @param[in] arg * @param[in] status Connect result code or disconnection notification */ -static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { +static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) { err_t err; if (status == MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "Successfully connected"); connection_attempt_counter = 0; - //set up callback function for input + // Set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - //subscribe to the topics setText, setImage, setColor and setTextcolor + // Subscribe to the topics setText, setImage, setColor and setTextcolor err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); } - //publish list of images here + // Publish list of images here publish_data(client, NULL); } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); while (connection_attempt_counter < 50) { connection_attempt_counter++; - //try to reconnect + // Try to reconnect mosquitto_connect(client); } - } } @@ -230,9 +228,9 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection * @brief Attempts to create a connection to the mosquitto broker * Creates a mqtt client and sets up a connection callback function * - * @param[in] client pointer to the MQTT client + * @param[in] client Pointer to the MQTT client */ -static void mosquitto_connect(mqtt_client_t *client) { +static void mosquitto_connect(mqtt_client_t* client) { struct mqtt_connect_client_info_t ci; err_t err; @@ -242,7 +240,7 @@ static void mosquitto_connect(mqtt_client_t *client) { ci.client_id = "STM32"; ip_addr_t server_ip; - IP4_ADDR(&server_ip, 192,168,69,11); + IP4_ADDR(&server_ip, 192, 168, 69, 11); uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { @@ -265,7 +263,7 @@ void mug_init(void) { ypos = 50; connection_attempt_counter = 0; - mqtt_client_t *client = mqtt_client_new(); + mqtt_client_t* client = mqtt_client_new(); if (client != NULL) { LOG_DEBUG(TAG, "Starting connection test"); mosquitto_connect(client); @@ -273,10 +271,10 @@ void mug_init(void) { } /** - * @brief Reads the color input string and outputs it to a useable value for LCD_APi + * @brief Reads the color input string and outputs it to a usable value for LCD_APi * - * @param[out] color define to use with the LCD_API - * @param[in] color input string to select a color + * @param[out] color Define to use with the LCD_API + * @param[in] color Input string to select a color */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; From 875edb52b589f1b89482544c3c89b3a152467a5d Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 20:30:12 +0100 Subject: [PATCH 57/77] Solved reviews --- project/Core/Inc/mug.h | 3 ++- project/Core/Src/main.c | 4 +++- project/Core/Src/mug.c | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index 788610e..a1df453 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,7 +7,8 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ -/* @brief Initialise MQTT application +/** + * @brief Initialize MQTT application */ void mug_init(void); diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 6e0105e..9adc667 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -133,11 +133,13 @@ int main(void) /* Initialize the tftp server */ tftp_server_init(); + + /* Initialize the MQTT application */ + mug_init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ - mug_init(); while (1) { /* USER CODE END WHILE */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index b830ca1..193b030 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -4,10 +4,10 @@ * @author RobinVdB */ +#include #include "mdns.h" #include "httpd.h" #include "lwip/apps/fs.h" -#include #include "lwip/ip_addr.h" #include "mqtt.h" #include "mug.h" @@ -18,7 +18,7 @@ //Function prototypes static void mqtt_pub_request_cb(void*, err_t); -static void publish_data(mqtt_client_t* , void*); +static void publish_data(mqtt_client_t*, void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); @@ -62,7 +62,7 @@ static void publish_data(mqtt_client_t *client, void *arg) { size_t num_files; llfs_file_t file_list[max_files]; u8_t qos = 2; - u8_t retain = 1 ; + u8_t retain = 1; LOG_DEBUG(TAG, "Entering publish"); @@ -275,7 +275,7 @@ void mug_init(void) { /** * @brief Reads the color input string and outputs it to a useable value for LCD_APi * - * @param[out] color define to use with the LCD_API + * @return color define to use with the LCD_API * @param[in] color input string to select a color */ uint32_t color_picker(char* color) { From 14e01e84fa595f8de8a8c7796e9aea16462dc404 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 20:43:08 +0100 Subject: [PATCH 58/77] Changed doxyegen order for color_picker --- project/Core/Src/mug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index f87ebc7..1bfd330 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -273,8 +273,8 @@ void mug_init(void) { /** * @brief Reads the color input string and outputs it to a usable value for LCD_APi * - * @return color Define to use with the LCD_API * @param[in] color Input string to select a color + * @return color Define to use with the LCD_API */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; From dea0d26141499ce1dc326fc078209035df416b43 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 21:57:54 +0100 Subject: [PATCH 59/77] Solved some reviews --- project/Core/Inc/mug.h | 2 +- project/Core/Src/mug.c | 81 ++++++++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index a1df453..1e3776c 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -1,6 +1,6 @@ /** * @file mug.h - * @header for mosquitto application of the groups assignment + * header for mosquitto application of the groups assignment * @author RobinVdB */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 1bfd330..26ab3b6 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -1,6 +1,6 @@ /** * @file mug.c - * @mosquitto application for group assignment + * mosquitto application for group assignment * @author RobinVdB */ @@ -16,6 +16,13 @@ #define LOGGER_LEVEL_INFO #include "log.h" +#define ATTEMPT_RECONNECT_AMOUNT 50 +#define PUBLISH_QOS 2 +#define PUBLISH_RETAIN 1 +#define MQTT_SERVER_PORT 1883 +#define PRINT_XPOS 50 +#define PRINT_YPOS 50 + // Function prototypes static void mqtt_pub_request_cb(void*, err_t); static void publish_data(mqtt_client_t*, void*); @@ -61,8 +68,8 @@ static void publish_data(mqtt_client_t* client, void* arg) { size_t max_files = 20; size_t num_files; llfs_file_t file_list[max_files]; - u8_t qos = 2; - u8_t retain = 1; + u8_t qos = PUBLISH_QOS; + u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); @@ -108,7 +115,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { * @param[in] tot_len Length of the incoming data */ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_len) { - LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); + LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %lu", topic, tot_len); // Check for which topic a publish was received if (strcmp(topic, "input/setText") == 0) { inpub_id = 0; @@ -135,6 +142,7 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ */ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; + lcd_gif_t* gif; LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); if (flags & MQTT_DATA_FLAG_LAST) { @@ -156,7 +164,10 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } if (data_buffer[len - 3] == 'g') { - lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + if (gif == NULL) { + LOG_INFO(TAG, "GIF could not be drawn"); + } } break; case 2: @@ -216,7 +227,7 @@ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - while (connection_attempt_counter < 50) { + while (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { connection_attempt_counter++; // Try to reconnect mosquitto_connect(client); @@ -259,8 +270,8 @@ void mug_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; font = LCD_FONT16; - xpos = 50; - ypos = 50; + xpos = PRINT_XPOS; + ypos = PRINT_YPOS; connection_attempt_counter = 0; mqtt_client_t* client = mqtt_client_new(); @@ -276,85 +287,85 @@ void mug_init(void) { * @param[in] color Input string to select a color * @return color Define to use with the LCD_API */ -uint32_t color_picker(char* color) { +uint32_t color_picker(char* color_in) { uint32_t output = LCD_BLACK; - if (strcmp((const char*)color, "blue") == 0) { + if (strcmp((const char*)color_in, "blue") == 0) { return LCD_BLUE; } - if (strcmp((const char*)color, "green") == 0) { + if (strcmp((const char*)color_in, "green") == 0) { return LCD_GREEN; } - if (strcmp((const char*)color, "red") == 0) { + if (strcmp((const char*)color_in, "red") == 0) { return LCD_RED; } - if (strcmp((const char*)color, "cyan") == 0) { + if (strcmp((const char*)color_in, "cyan") == 0) { return LCD_CYAN; } - if (strcmp((const char*)color, "magenta") == 0) { + if (strcmp((const char*)color_in, "magenta") == 0) { return LCD_MAGENTA; } - if (strcmp((const char*)color, "yellow") == 0) { + if (strcmp((const char*)color_in, "yellow") == 0) { return LCD_YELLOW; } - if (strcmp((const char*)color, "light blue") == 0) { + if (strcmp((const char*)color_in, "light blue") == 0) { return LCD_LIGHTBLUE; } - if (strcmp((const char*)color, "light green") == 0) { + if (strcmp((const char*)color_in, "light green") == 0) { return LCD_LIGHTGREEN; } - if (strcmp((const char*)color, "light red") == 0) { + if (strcmp((const char*)color_in, "light red") == 0) { return LCD_LIGHTRED; } - if (strcmp((const char*)color, "light cyan") == 0) { + if (strcmp((const char*)color_in, "light cyan") == 0) { return LCD_LIGHTCYAN; } - if (strcmp((const char*)color, "light magenta") == 0) { + if (strcmp((const char*)color_in, "light magenta") == 0) { return LCD_LIGHTMAGENTA; } - if (strcmp((const char*)color, "light yellow") == 0) { + if (strcmp((const char*)color_in, "light yellow") == 0) { return LCD_LIGHTYELLOW; } - if (strcmp((const char*)color, "dark blue") == 0) { + if (strcmp((const char*)color_in, "dark blue") == 0) { return LCD_DARKBLUE; } - if (strcmp((const char*)color, "dark green") == 0) { + if (strcmp((const char*)color_in, "dark green") == 0) { return LCD_DARKGREEN; } - if (strcmp((const char*)color, "dark red") == 0) { + if (strcmp((const char*)color_in, "dark red") == 0) { return LCD_DARKRED; } - if (strcmp((const char*)color, "dark cyan") == 0) { + if (strcmp((const char*)color_in, "dark cyan") == 0) { return LCD_DARKCYAN; } - if (strcmp((const char*)color, "dark magenta") == 0) { + if (strcmp((const char*)color_in, "dark magenta") == 0) { return LCD_DARKMAGENTA; } - if (strcmp((const char*)color, "dark yellow") == 0) { + if (strcmp((const char*)color_in, "dark yellow") == 0) { return LCD_DARKYELLOW; } - if (strcmp((const char*)color, "white") == 0) { + if (strcmp((const char*)color_in, "white") == 0) { return LCD_WHITE; } - if (strcmp((const char*)color, "light gray") == 0) { + if (strcmp((const char*)color_in, "light gray") == 0) { return LCD_LIGHTGRAY; } - if (strcmp((const char*)color, "gray") == 0) { + if (strcmp((const char*)color_in, "gray") == 0) { return LCD_GRAY; } - if (strcmp((const char*)color, "dark gray") == 0) { + if (strcmp((const char*)color_in, "dark gray") == 0) { return LCD_DARKGRAY; } - if (strcmp((const char*)color, "black") == 0) { + if (strcmp((const char*)color_in, "black") == 0) { return LCD_BLACK; } - if (strcmp((const char*)color, "brown") == 0) { + if (strcmp((const char*)color_in, "brown") == 0) { return LCD_BROWN; } - if (strcmp((const char*)color, "orange") == 0) { + if (strcmp((const char*)color_in, "orange") == 0) { return LCD_ORANGE; } - if (strcmp((const char*)color, "transparent") == 0) { + if (strcmp((const char*)color_in, "transparent") == 0) { return LCD_TRANSPARENT; } From da5b02d5da57aced42bdbba5fe773ac04210d78b Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 09:54:58 +0100 Subject: [PATCH 60/77] Created enum for inpub_id changed strcat to strncat --- project/Core/Src/mug.c | 56 +++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 26ab3b6..0d7f377 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -22,6 +22,15 @@ #define MQTT_SERVER_PORT 1883 #define PRINT_XPOS 50 #define PRINT_YPOS 50 +#define MAX_FILES 20 + +typedef enum input_topic { + set_text, + set_text_color, + set_color, + set_image, + other_topic +} input_topic_t; // Function prototypes static void mqtt_pub_request_cb(void*, err_t); @@ -40,7 +49,7 @@ static uint16_t ypos; static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; -static int inpub_id; +static input_topic_t inpub_id; static const char* TAG = "mug"; /** @@ -65,7 +74,7 @@ static void mqtt_pub_request_cb(void* arg, err_t result) { static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; err_t err; - size_t max_files = 20; + size_t max_files = MAX_FILES; size_t num_files; llfs_file_t file_list[max_files]; u8_t qos = PUBLISH_QOS; @@ -76,29 +85,36 @@ static void publish_data(mqtt_client_t* client, void* arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); if (num_files == 0) { - strcpy(pub_payload, "No images found"); + strncpy(pub_payload, "No images found", sizeof(pub_payload)); } else { - strcat(pub_payload, "Available images: "); + strncat(pub_payload, "Available images: ", sizeof(pub_payload) - strlen(pub_payload) - 1); for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string - strcat(pub_payload, file_list[i].name); - strcat(pub_payload, ", "); // Add a comma between file names + strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1); + strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names } - strcat(pub_payload, "\0"); + strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1); } + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + if (err != ERR_OK) { + LOG_DEBUG(TAG, "Publish err: %d", err); + } + + pub_payload[0] = '\0'; + num_files = llfs_file_list(file_list, max_files, "*.gif"); if (num_files == 0) { strcpy(pub_payload, "No gifs found"); } else { - strcat(pub_payload, "Available gifs: "); + strcpy(pub_payload, "Available gifs: "); for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string - strcat(pub_payload, file_list[i].name); - strcat(pub_payload, ", "); // Add a comma between file names + strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1); + strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names } - strcat(pub_payload, "\0"); + strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -118,16 +134,16 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %lu", topic, tot_len); // Check for which topic a publish was received if (strcmp(topic, "input/setText") == 0) { - inpub_id = 0; + inpub_id = set_text; } else if (strcmp(topic, "input/setImage") == 0) { - inpub_id = 1; + inpub_id = set_image; } else if (strcmp(topic, "input/setTextColor") == 0) { - inpub_id = 2; + inpub_id = set_text_color; } else if (strcmp(topic, "input/setColor") == 0) { - inpub_id = 3; + inpub_id = set_color; } else { // In case of wrong topic - inpub_id = 4; + inpub_id = other_topic; } } @@ -149,13 +165,13 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, memcpy(data_buffer, data, len); data_buffer[len] = '\0'; switch (inpub_id) { - case 0: + case set_text: // Places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); lcd_clear_text(); lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); break; - case 1: + case set_image: // Places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); @@ -170,12 +186,12 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, } } break; - case 2: + case set_text_color: // Changes the text color for the next time text is written LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); color = color_picker(data_buffer); break; - case 3: + case set_color: // Changes the background color for the next time text is written LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); bgcolor = color_picker(data_buffer); From 70b980dcfa37d50bfa72d4f27663094eab62342e Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 10:22:31 +0100 Subject: [PATCH 61/77] Turned server IP and Port into defines --- project/Core/Src/mug.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 0d7f377..b6d20c9 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -23,6 +23,11 @@ #define PRINT_XPOS 50 #define PRINT_YPOS 50 #define MAX_FILES 20 +#define SERVER_IP4_A 192 +#define SERVER_IP4_B 168 +#define SERVER_IP4_C 69 +#define SERVER_IP4_D 11 +#define SERVER_PORT 1883 typedef enum input_topic { set_text, @@ -267,8 +272,8 @@ static void mosquitto_connect(mqtt_client_t* client) { ci.client_id = "STM32"; ip_addr_t server_ip; - IP4_ADDR(&server_ip, 192, 168, 69, 11); - uint16_t server_port = 1883; + IP4_ADDR(&server_ip, SERVER_IP4_A, SERVER_IP4_B, SERVER_IP4_C, SERVER_IP4_D); + uint16_t server_port = SERVER_PORT; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { LOG_DEBUG(TAG, "mqtt_connect return %d", err); From d88d35c6cf6a8591df4583c744b1b1cc65142846 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 12:39:57 +0100 Subject: [PATCH 62/77] Renamed files from mug to mqtt_application --- project/Core/Inc/{mug.h => mqtt_application.h} | 8 ++++---- project/Core/Src/main.c | 2 +- project/Core/Src/{mug.c => mqtt_application.c} | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) rename project/Core/Inc/{mug.h => mqtt_application.h} (62%) rename project/Core/Src/{mug.c => mqtt_application.c} (99%) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mqtt_application.h similarity index 62% rename from project/Core/Inc/mug.h rename to project/Core/Inc/mqtt_application.h index 1e3776c..9e8cca6 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mqtt_application.h @@ -1,15 +1,15 @@ /** - * @file mug.h + * @file mqtt_application.h * header for mosquitto application of the groups assignment * @author RobinVdB */ -#ifndef INC_MUG_H_ -#define INC_MUG_H_ +#ifndef INC_MQTTA_H_ +#define INC_MQTTA_H_ /** * @brief Initialize MQTT application */ void mug_init(void); -#endif /* INC_MUG_H_ */ +#endif /* INC_MQTTA_H_ */ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 9adc667..5b47a18 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -28,7 +28,7 @@ #include "log.h" #include "llfs.h" #include "lcd_api.h" -#include "mug.h" +#include #include "tftp.h" /* USER CODE END Includes */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mqtt_application.c similarity index 99% rename from project/Core/Src/mug.c rename to project/Core/Src/mqtt_application.c index b6d20c9..5d283ec 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mqtt_application.c @@ -1,9 +1,10 @@ /** - * @file mug.c + * @file mqtt_application.c * mosquitto application for group assignment * @author RobinVdB */ +#include #include #include "httpd.h" #include "lcd_api.h" @@ -11,7 +12,6 @@ #include "lwip/ip_addr.h" #include "mdns.h" #include "mqtt.h" -#include "mug.h" #define LOGGER_LEVEL_INFO #include "log.h" @@ -55,7 +55,7 @@ static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; static input_topic_t inpub_id; -static const char* TAG = "mug"; +static const char* TAG = "MQTT"; /** * @brief callback function for publishing data From b50900e713b7b8ae43420b27de68b70910260204 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 12:43:38 +0100 Subject: [PATCH 63/77] Changed function name mug_init to mqtt_application_init --- project/Core/Inc/mqtt_application.h | 2 +- project/Core/Src/main.c | 2 +- project/Core/Src/mqtt_application.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 9e8cca6..087e8de 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -10,6 +10,6 @@ /** * @brief Initialize MQTT application */ -void mug_init(void); +void mqtt_application_init(void); #endif /* INC_MQTTA_H_ */ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 5b47a18..d3a955a 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -135,7 +135,7 @@ int main(void) tftp_server_init(); /* Initialize the MQTT application */ - mug_init(); + mqtt_application_init(); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 5d283ec..bb45846 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -287,7 +287,7 @@ static void mosquitto_connect(mqtt_client_t* client) { * @brief Init function for the mosquitto application of the assignment * Gives the global variables a value and calls the mosquitto_connect function */ -void mug_init(void) { +void mqtt_application_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; font = LCD_FONT16; From 1550ccd0ba977b9d2d0c5299d9faf8aa12006a1a Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 13:33:47 +0100 Subject: [PATCH 64/77] Made some requested changes from reviews --- project/Core/Inc/mqtt_application.h | 2 +- project/Core/Src/main.c | 2 +- project/Core/Src/mqtt_application.c | 173 ++++++++++++++-------------- 3 files changed, 91 insertions(+), 86 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 087e8de..117a893 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -10,6 +10,6 @@ /** * @brief Initialize MQTT application */ -void mqtt_application_init(void); +uint8_t mqtt_application_init(void); #endif /* INC_MQTTA_H_ */ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index d3a955a..336157c 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -28,7 +28,7 @@ #include "log.h" #include "llfs.h" #include "lcd_api.h" -#include +#include "mqtt_application.h" #include "tftp.h" /* USER CODE END Includes */ diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index bb45846..5437c9b 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -4,7 +4,6 @@ * @author RobinVdB */ -#include #include #include "httpd.h" #include "lcd_api.h" @@ -12,6 +11,7 @@ #include "lwip/ip_addr.h" #include "mdns.h" #include "mqtt.h" +#include "mqtt_application.h" #define LOGGER_LEVEL_INFO #include "log.h" @@ -64,9 +64,7 @@ static const char* TAG = "MQTT"; * @param[in] result Whether the publish was successful or not */ static void mqtt_pub_request_cb(void* arg, err_t result) { - if (result != ERR_OK) { - LOG_DEBUG(TAG, "Publish result: %d", result); - } + LOG_DEBUG(TAG, "Publish result: %d", result); } /** @@ -79,15 +77,14 @@ static void mqtt_pub_request_cb(void* arg, err_t result) { static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; err_t err; - size_t max_files = MAX_FILES; size_t num_files; - llfs_file_t file_list[max_files]; + llfs_file_t file_list[MAX_FILES]; u8_t qos = PUBLISH_QOS; u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); - num_files = llfs_file_list(file_list, max_files, "*.bmp"); + num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); if (num_files == 0) { strncpy(pub_payload, "No images found", sizeof(pub_payload)); @@ -108,12 +105,12 @@ static void publish_data(mqtt_client_t* client, void* arg) { pub_payload[0] = '\0'; - num_files = llfs_file_list(file_list, max_files, "*.gif"); + num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); if (num_files == 0) { - strcpy(pub_payload, "No gifs found"); + strncpy(pub_payload, "No gifs found", sizeof(pub_payload)); } else { - strcpy(pub_payload, "Available gifs: "); + strncpy(pub_payload, "Available gifs: ", sizeof(pub_payload)); for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1); @@ -140,16 +137,22 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ // Check for which topic a publish was received if (strcmp(topic, "input/setText") == 0) { inpub_id = set_text; - } else if (strcmp(topic, "input/setImage") == 0) { - inpub_id = set_image; - } else if (strcmp(topic, "input/setTextColor") == 0) { - inpub_id = set_text_color; - } else if (strcmp(topic, "input/setColor") == 0) { - inpub_id = set_color; - } else { - // In case of wrong topic - inpub_id = other_topic; + return; } + if (strcmp(topic, "input/setImage") == 0) { + inpub_id = set_image; + return; + } + if (strcmp(topic, "input/setTextColor") == 0) { + inpub_id = set_text_color; + return; + } + if (strcmp(topic, "input/setColor") == 0) { + inpub_id = set_color; + return; + } + // In case of wrong topic + inpub_id = other_topic; } /** @@ -165,46 +168,47 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, char data_buffer[len + 1]; lcd_gif_t* gif; - LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); - if (flags & MQTT_DATA_FLAG_LAST) { - memcpy(data_buffer, data, len); - data_buffer[len] = '\0'; - switch (inpub_id) { - case set_text: - // Places text on the lcd - LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); - lcd_clear_text(); - lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); - break; - case set_image: - // Places an image on the lcd - LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); - lcd_clear_images(); - lcd_set_bg_color_layer0(bgcolor); - if (data_buffer[len - 3] == 'b') { - lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %d", len, flags); + if (!(flags & MQTT_DATA_FLAG_LAST)) { + LOG_WARN(TAG, "incoming data too big to fit in buffer."); + return; + } + memcpy(data_buffer, data, len); + data_buffer[len] = '\0'; + switch (inpub_id) { + case set_text: + // Places text on the lcd + LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); + lcd_clear_text(); + lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); + break; + case set_image: + // Places an image on the lcd + LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); + lcd_clear_images(); + lcd_set_bg_color_layer0(bgcolor); + if (data_buffer[len - 3] == 'b') { + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + } + if (data_buffer[len - 3] == 'g') { + gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + if (gif == NULL) { + LOG_INFO(TAG, "GIF could not be drawn"); } - if (data_buffer[len - 3] == 'g') { - gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); - if (gif == NULL) { - LOG_INFO(TAG, "GIF could not be drawn"); - } - } - break; - case set_text_color: - // Changes the text color for the next time text is written - LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); - color = color_picker(data_buffer); - break; - case set_color: - // Changes the background color for the next time text is written - LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); - bgcolor = color_picker(data_buffer); - break; - default: - LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); - } - } else { + } + break; + case set_text_color: + // Changes the text color for the next time text is written + LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); + color = color_picker(data_buffer); + break; + case set_color: + // Changes the background color for the next time text is written + LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); + bgcolor = color_picker(data_buffer); + break; + default: + LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); } } @@ -230,22 +234,7 @@ static void mqtt_sub_request_cb(void* arg, err_t result) { static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) { err_t err; - if (status == MQTT_CONNECT_ACCEPTED) { - LOG_INFO(TAG, "Successfully connected"); - - connection_attempt_counter = 0; - // Set up callback function for input - mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - - // Subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); - if (err != ERR_OK) { - LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); - } - - // Publish list of images here - publish_data(client, NULL); - } else { + if (status != MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); while (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { @@ -253,7 +242,22 @@ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection // Try to reconnect mosquitto_connect(client); } + return; } + LOG_INFO(TAG, "Successfully connected"); + + connection_attempt_counter = 0; + // Set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + + // Subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); + if (err != ERR_OK) { + LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); + } + + // Publish list of images here + publish_data(client, NULL); } /** @@ -277,17 +281,16 @@ static void mosquitto_connect(mqtt_client_t* client) { err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { LOG_DEBUG(TAG, "mqtt_connect return %d", err); + return; } - if (err == ERR_OK) { - LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); - } + LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } /** * @brief Init function for the mosquitto application of the assignment * Gives the global variables a value and calls the mosquitto_connect function */ -void mqtt_application_init(void) { +uint8_t mqtt_application_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; font = LCD_FONT16; @@ -296,10 +299,14 @@ void mqtt_application_init(void) { connection_attempt_counter = 0; mqtt_client_t* client = mqtt_client_new(); - if (client != NULL) { - LOG_DEBUG(TAG, "Starting connection test"); - mosquitto_connect(client); + if (client == NULL) { + LOG_CRIT(TAG, "%s: client == NULL", __func__); + return 1; } + LOG_DEBUG(TAG, "Starting connection test"); + mosquitto_connect(client); + + return 0; } /** @@ -309,8 +316,6 @@ void mqtt_application_init(void) { * @return color Define to use with the LCD_API */ uint32_t color_picker(char* color_in) { - uint32_t output = LCD_BLACK; - if (strcmp((const char*)color_in, "blue") == 0) { return LCD_BLUE; } @@ -390,5 +395,5 @@ uint32_t color_picker(char* color_in) { return LCD_TRANSPARENT; } - return output; + return LCD_BLACK; } 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 65/77] 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 */ From 2d86900a4ac2d1610cfeeb0e7a18d3a3a9e9b18f Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 14:34:27 +0100 Subject: [PATCH 66/77] Created new function to fill payload string for publish --- project/Core/Src/mqtt_application.c | 44 ++++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 5437c9b..4580b1e 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -46,6 +46,7 @@ static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); +static void create_publish_string(char*, char*, llfs_file_t*, size_t, size_t); // Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static sFONT* font; @@ -87,17 +88,11 @@ static void publish_data(mqtt_client_t* client, void* arg) { num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); if (num_files == 0) { - strncpy(pub_payload, "No images found", sizeof(pub_payload)); + strncat(pub_payload, "No images found", sizeof(pub_payload) - strlen(pub_payload) - 1); + LOG_INFO(TAG, "%s: No images found", __func__); } else { - strncat(pub_payload, "Available images: ", sizeof(pub_payload) - strlen(pub_payload) - 1); - for (size_t i = 0; i < num_files; i++) { - // Concatenate file names into the payload string - strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1); - strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names - } - strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1); + create_publish_string("image", pub_payload, file_list, sizeof(pub_payload), num_files); } - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); @@ -108,15 +103,10 @@ static void publish_data(mqtt_client_t* client, void* arg) { num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); if (num_files == 0) { - strncpy(pub_payload, "No gifs found", sizeof(pub_payload)); + strncat(pub_payload, "No gifs found", sizeof(pub_payload) - strlen(pub_payload) - 1); + LOG_INFO(TAG, "%s: No gifs found", __func__); } else { - strncpy(pub_payload, "Available gifs: ", sizeof(pub_payload)); - for (size_t i = 0; i < num_files; i++) { - // Concatenate file names into the payload string - strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1); - strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names - } - strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1); + create_publish_string("gif", pub_payload, file_list, sizeof(pub_payload), num_files); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -397,3 +387,23 @@ uint32_t color_picker(char* color_in) { return LCD_BLACK; } + +static void create_publish_string(char* file_type, char* payload_buffer, llfs_file_t* file_list, size_t buffer_size, size_t files_amount) { + if (strcmp(file_type, "image") == 0) { + strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1); + } else if (strcmp(file_type, "gif") == 0) { + strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1); + } else { + LOG_WARN(TAG, "No application for given file type: %s", file_type); + return; + } + for (size_t i = 0; i < files_amount; i++) { + // Concatenate file names into the payload string + strncat(payload_buffer, file_list[i].name, buffer_size - strlen(payload_buffer) - 1); + strncat(payload_buffer, ", ", buffer_size - strlen(payload_buffer) - 1); // Add a comma between file names + } + strncat(payload_buffer, "\0", buffer_size - strlen(payload_buffer) - 1); + LOG_DEBUG(TAG, "String: %s", payload_buffer); + + return; +} From 907970ee5919e4cb2f1adaf90268508d2b313169 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 14:49:17 +0100 Subject: [PATCH 67/77] Added tolower() to color_picker() --- project/Core/Src/mqtt_application.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 4580b1e..2f0e546 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -306,6 +306,9 @@ uint8_t mqtt_application_init(void) { * @return color Define to use with the LCD_API */ uint32_t color_picker(char* color_in) { + for (int i = 0; i < strlen(color_in); i++) { + color_in[i] = tolower(color_in[i]); + } if (strcmp((const char*)color_in, "blue") == 0) { return LCD_BLUE; } From b302ab2e11af28281c364eeca1f7e2d3c14635fd Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:02:01 +0100 Subject: [PATCH 68/77] Updated doxygen --- project/Core/Inc/mqtt_application.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 117a893..59bbcbe 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -9,6 +9,8 @@ /** * @brief Initialize MQTT application + * + * @output returns 1 if the init failed to create a client and start an MQTT connection */ uint8_t mqtt_application_init(void); From be0339b2cf573d0d3f10452df649eecb2de670d2 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:15:56 +0100 Subject: [PATCH 69/77] Updated create_publish_string --- project/Core/Src/mqtt_application.c | 55 +++++++++++++++++------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 2f0e546..203830a 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -46,7 +46,7 @@ static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); -static void create_publish_string(char*, char*, llfs_file_t*, size_t, size_t); +static void create_publish_string(char*, char*, size_t); // Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static sFONT* font; @@ -78,36 +78,21 @@ static void mqtt_pub_request_cb(void* arg, err_t result) { static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; err_t err; - size_t num_files; - llfs_file_t file_list[MAX_FILES]; u8_t qos = PUBLISH_QOS; u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); - num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); + create_publish_string("image", pub_payload,sizeof(pub_payload)); - if (num_files == 0) { - strncat(pub_payload, "No images found", sizeof(pub_payload) - strlen(pub_payload) - 1); - LOG_INFO(TAG, "%s: No images found", __func__); - } else { - create_publish_string("image", pub_payload, file_list, sizeof(pub_payload), num_files); - } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); - if (err != ERR_OK) { - LOG_DEBUG(TAG, "Publish err: %d", err); - } + if (err != ERR_OK) { + LOG_DEBUG(TAG, "Publish err: %d", err); + } pub_payload[0] = '\0'; + create_publish_string("gif", pub_payload, sizeof(pub_payload)); - num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); - - if (num_files == 0) { - strncat(pub_payload, "No gifs found", sizeof(pub_payload) - strlen(pub_payload) - 1); - LOG_INFO(TAG, "%s: No gifs found", __func__); - } else { - create_publish_string("gif", pub_payload, file_list, sizeof(pub_payload), num_files); - } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); @@ -391,16 +376,40 @@ uint32_t color_picker(char* color_in) { return LCD_BLACK; } -static void create_publish_string(char* file_type, char* payload_buffer, llfs_file_t* file_list, size_t buffer_size, size_t files_amount) { +/** + * @brief creates a string to publish on the getImageList topic + * + * @param[in] file_type The type of file file_list is made out of + * @param[in] payload_buffer The string to be published + * @param[in] buffer_size Size of payload_buffer + */ +static void create_publish_string(char* file_type, char* payload_buffer, size_t buffer_size) { + size_t num_files; + llfs_file_t file_list[MAX_FILES]; + if (strcmp(file_type, "image") == 0) { + num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); + + if (num_files == 0) { + strncat(payload_buffer, "No images found", buffer_size - strlen(payload_buffer) - 1); + LOG_INFO(TAG, "%s: No images found", __func__); + return; + } strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1); } else if (strcmp(file_type, "gif") == 0) { + num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); + + if (num_files == 0) { + strncat(payload_buffer, "No gifs found", buffer_size - strlen(payload_buffer) - 1); + LOG_INFO(TAG, "%s: No gifs found", __func__); + return; + } strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1); } else { LOG_WARN(TAG, "No application for given file type: %s", file_type); return; } - for (size_t i = 0; i < files_amount; i++) { + for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string strncat(payload_buffer, file_list[i].name, buffer_size - strlen(payload_buffer) - 1); strncat(payload_buffer, ", ", buffer_size - strlen(payload_buffer) - 1); // Add a comma between file names From 8263855ce7430f18f793c709c98db8a8b7a49c6e Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:39:16 +0100 Subject: [PATCH 70/77] Updated create_publish_string as requested --- project/Core/Src/mqtt_application.c | 31 ++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 203830a..f648725 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -83,7 +83,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { LOG_DEBUG(TAG, "Entering publish"); - create_publish_string("image", pub_payload,sizeof(pub_payload)); + create_publish_string("*.bmp", pub_payload,sizeof(pub_payload)); err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -91,7 +91,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { } pub_payload[0] = '\0'; - create_publish_string("gif", pub_payload, sizeof(pub_payload)); + create_publish_string("*.gif", pub_payload, sizeof(pub_payload)); err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -379,7 +379,7 @@ uint32_t color_picker(char* color_in) { /** * @brief creates a string to publish on the getImageList topic * - * @param[in] file_type The type of file file_list is made out of + * @param[in] file_type The file extension asked to be published * @param[in] payload_buffer The string to be published * @param[in] buffer_size Size of payload_buffer */ @@ -387,23 +387,18 @@ static void create_publish_string(char* file_type, char* payload_buffer, size_t size_t num_files; llfs_file_t file_list[MAX_FILES]; - if (strcmp(file_type, "image") == 0) { - num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); + num_files = llfs_file_list(file_list, MAX_FILES, file_type); - if (num_files == 0) { - strncat(payload_buffer, "No images found", buffer_size - strlen(payload_buffer) - 1); - LOG_INFO(TAG, "%s: No images found", __func__); - return; - } + if (num_files == 0) { + strncat(payload_buffer, "No files found of type: ", buffer_size - strlen(payload_buffer) - 1); + strncat(payload_buffer, file_type, buffer_size - strlen(payload_buffer) - 1); + LOG_INFO(TAG, "%s: No files found of type %s", __func__, file_type); + return; + } + + if (strcmp(file_type, "*.bmp") == 0) { strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1); - } else if (strcmp(file_type, "gif") == 0) { - num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); - - if (num_files == 0) { - strncat(payload_buffer, "No gifs found", buffer_size - strlen(payload_buffer) - 1); - LOG_INFO(TAG, "%s: No gifs found", __func__); - return; - } + } else if (strcmp(file_type, "*.gif") == 0) { strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1); } else { LOG_WARN(TAG, "No application for given file type: %s", file_type); From fdc5eb865e0bffedc0268b531863e09d0093d540 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:45:36 +0100 Subject: [PATCH 71/77] Updated publish_data to use defines directly --- project/Core/Src/mqtt_application.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index f648725..96de1dc 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -78,14 +78,12 @@ static void mqtt_pub_request_cb(void* arg, err_t result) { static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; err_t err; - u8_t qos = PUBLISH_QOS; - u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); create_publish_string("*.bmp", pub_payload,sizeof(pub_payload)); - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); } @@ -93,7 +91,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { pub_payload[0] = '\0'; create_publish_string("*.gif", pub_payload, sizeof(pub_payload)); - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); } From def43a37a76a73d8677a7d97d8a6019b464e7ce2 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:48:47 +0100 Subject: [PATCH 72/77] Changed topic to public gifs so both messages are retained --- project/Core/Src/mqtt_application.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 96de1dc..b92219e 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -91,7 +91,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { pub_payload[0] = '\0'; create_publish_string("*.gif", pub_payload, sizeof(pub_payload)); - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); + err = mqtt_publish(client, "getGifList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); } From 1e7638c63d7a1ba5e07b683c85c9266d9e3e1929 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 17:05:05 +0100 Subject: [PATCH 73/77] Solved reviews --- project/Core/Inc/mqtt_application.h | 2 ++ project/Core/Src/mqtt_application.c | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 59bbcbe..ae9452d 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -7,6 +7,8 @@ #ifndef INC_MQTTA_H_ #define INC_MQTTA_H_ +#include + /** * @brief Initialize MQTT application * diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index b92219e..6692046 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -130,7 +130,7 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ /** * @brief Handles incoming publish data - * Handles the recieved data from a publish to a subscribed topic + * Handles the received data from a publish to a subscribed topic * * @param[in] arg * @param[in] data The incoming data @@ -160,13 +160,15 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); lcd_set_bg_color_layer0(bgcolor); - if (data_buffer[len - 3] == 'b') { - lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); - } - if (data_buffer[len - 3] == 'g') { - gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); - if (gif == NULL) { - LOG_INFO(TAG, "GIF could not be drawn"); + if (strlen(data_buffer) >= 3) { + if (data_buffer[len - 3] == 'b') { + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + } + if (data_buffer[len - 3] == 'g') { + gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + if (gif == NULL) { + LOG_INFO(TAG, "GIF could not be drawn"); + } } } break; @@ -210,7 +212,7 @@ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection if (status != MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - while (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { + if (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { connection_attempt_counter++; // Try to reconnect mosquitto_connect(client); @@ -409,6 +411,4 @@ static void create_publish_string(char* file_type, char* payload_buffer, size_t } strncat(payload_buffer, "\0", buffer_size - strlen(payload_buffer) - 1); LOG_DEBUG(TAG, "String: %s", payload_buffer); - - return; } From 5600aa20f35df36ad8f26cef552e38d94c05ace3 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 17:13:15 +0100 Subject: [PATCH 74/77] Updated doxygen --- project/Core/Src/mqtt_application.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 6692046..4ecaea8 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -61,7 +61,7 @@ static const char* TAG = "MQTT"; /** * @brief callback function for publishing data * - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] result Whether the publish was successful or not */ static void mqtt_pub_request_cb(void* arg, err_t result) { @@ -73,7 +73,7 @@ static void mqtt_pub_request_cb(void* arg, err_t result) { * Publishes the names of all the .bmp and .gif files on the topic getImageList * * @param[in] client Pointer to the MQTT client - * @param[in] arg + * @param[in] arg Additional argument to pass to the callback function */ static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; @@ -101,7 +101,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { * @brief Handles incoming publish * Callback function for when data was published to a subscribed topic * - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] topic The topic on which an incoming publish was received * @param[in] tot_len Length of the incoming data */ @@ -132,7 +132,7 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ * @brief Handles incoming publish data * Handles the received data from a publish to a subscribed topic * - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] data The incoming data * @param[in] len Length of the data * @param[in] flags Whether this is the last fragment of the incoming data @@ -190,8 +190,8 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, /** * @brief Callback function for outgoing subscribe request * - * @param[in] arg - * @param[in] result + * @param[in] arg User supplied argument to connection callback + * @param[in] result Result code for the subscribe request */ static void mqtt_sub_request_cb(void* arg, err_t result) { LOG_DEBUG(TAG, "Subscribe result: %d", result); @@ -203,7 +203,7 @@ static void mqtt_sub_request_cb(void* arg, err_t result) { * subscribes to the input topics and calls the publish_data function. * * @param[in] client Pointer to the MQTT client - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] status Connect result code or disconnection notification */ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) { From 752ada6867ac4c08b8bcf7ed3b52171b81f6eaf5 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 17:15:22 +0100 Subject: [PATCH 75/77] Changed strlen(data_buffer) to len --- project/Core/Src/mqtt_application.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 4ecaea8..21c0a3e 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -160,7 +160,7 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); lcd_set_bg_color_layer0(bgcolor); - if (strlen(data_buffer) >= 3) { + if (len >= 3) { if (data_buffer[len - 3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } From 5917bcf329f917cf6b8b76e1f269430bca1cfd5d Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 26 Nov 2023 22:22:03 +0100 Subject: [PATCH 76/77] MQTT Add brief tag --- project/Core/Inc/mqtt_application.h | 2 +- project/Core/Src/mqtt_application.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index ae9452d..389f3b4 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -1,6 +1,6 @@ /** * @file mqtt_application.h - * header for mosquitto application of the groups assignment + * @brief header for mosquitto application of the groups assignment * @author RobinVdB */ diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 21c0a3e..53f72e4 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -1,6 +1,6 @@ /** * @file mqtt_application.c - * mosquitto application for group assignment + * @brief mosquitto application for group assignment * @author RobinVdB */ From e4b50559c454a7ce147c3b14e3351137d971145e Mon Sep 17 00:00:00 2001 From: L-diy Date: Sun, 26 Nov 2023 22:35:21 +0100 Subject: [PATCH 77/77] Reformat `modbus_tcp.c/h` and fix some typos --- project/Core/Inc/modbus_tcp.h | 6 +- project/Core/Src/modbus_tcp.c | 154 ++++++++++++++++------------------ 2 files changed, 74 insertions(+), 86 deletions(-) diff --git a/project/Core/Inc/modbus_tcp.h b/project/Core/Inc/modbus_tcp.h index 10c993f..2812d7c 100644 --- a/project/Core/Inc/modbus_tcp.h +++ b/project/Core/Inc/modbus_tcp.h @@ -8,7 +8,8 @@ #ifndef INC_MODBUS_H_ #define INC_MODBUS_H_ -#define MODBUSPORT 502 //is the default + +#define MODBUSPORT 502 // 502 is the default #include @@ -21,9 +22,8 @@ /** * @fn void modbus_init - * @brief Initiallises the modbus tcp + * @brief Initializes the modbus tcp */ - void modbus_init(void); #endif /* INC_MODBUS_H_ */ diff --git a/project/Core/Src/modbus_tcp.c b/project/Core/Src/modbus_tcp.c index d974226..9558268 100644 --- a/project/Core/Src/modbus_tcp.c +++ b/project/Core/Src/modbus_tcp.c @@ -11,38 +11,36 @@ #include "log.h" // Defines -#define MAX_REG 250 +#define MAX_REG 250 #define EXTENSION_LENGHT 4 -#define TEXT_LENGHT 200 -#define MULTIPLE_REG 0x10 -#define REG_LENGTH 428 -#define START_DATA 28 -#define REG_01 14 -#define REG_02 16 -#define REG_03 18 -#define REG_04 20 -#define REG_05 22 -#define REG_06 24 -#define REG_07 26 +#define TEXT_LENGHT 200 +#define MULTIPLE_REG 0x10 +#define REG_LENGTH 428 +#define START_DATA 28 +#define REG_01 14 +#define REG_02 16 +#define REG_03 18 +#define REG_04 20 +#define REG_05 22 +#define REG_06 24 +#define REG_07 26 // Global variables char registers[MAX_REG]; -//const char* TAG = "Modbus_TCP"; // Tag used in logs +// const char* TAG = "Modbus_TCP"; // Tag used in logs // Functions -static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); -static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err); - +static err_t modbus_incoming_data(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err); +static err_t modbus_accept(void* arg, struct tcp_pcb* pcb, err_t err); /** - * @fn static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) - * @brief Function thats called when theris a new request on port 502. - * It handles the incomming data from Qmodmaster + * @fn static err_t modbus_incoming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) + * @brief Function that's called when there is a new request on port 502. + * It handles the incoming data from Qmodmaster */ - -static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){ +static err_t modbus_incoming_data(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) { uint8_t counter; - char *pc; + char* pc; char text[TEXT_LENGHT]; uint8_t background_red = 0; uint8_t background_green = 0; @@ -54,34 +52,35 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf * uint32_t result_background = 0; uint32_t result_txt = 0; - memcpy(text,'_',TEXT_LENGHT); // Putting underscores in the whole array - text[TEXT_LENGHT-1] = '\0'; + memcpy(text, '_', TEXT_LENGHT); // Putting underscores in the whole array + text[TEXT_LENGHT - 1] = '\0'; - if (p != NULL){ - //LOG_INFO(TAG, "data not null\n"); - // here im going to procces the modbusdata - tcp_recved( pcb, p->tot_len ); + if (p != NULL) { + // LOG_INFO(TAG, "data not null\n"); + // Procces the modbusdata + tcp_recved(pcb, p->tot_len); pc = (char*)p->payload; - for(uint16_t i = 0; i < p->tot_len; i++) { // putting the bufer in the register array - registers[i] = pc[i]; // getting the error "void value not ignored as it ought to be" on this line + for (uint16_t i = 0; i < p->tot_len; i++) { // Putting the bufer in the register array + registers[i] = pc[i]; // Getting the error "void value not ignored as it ought to be" on this line } - if(registers[7] == MULTIPLE_REG){ // Check if it's a Modbus Write Multiple Registers request (0x10) - //LOG_INFO(TAG, "in writing multiple register mode\n"); - background_red = (uint8_t)(registers[REG_01]); // 01 - background_green = (uint8_t)(registers[REG_02]); // 02 - background_blue = (uint8_t)(registers[REG_03]); // 03 - text_color_red = (uint8_t)(registers[REG_04]); // 04 - text_color_green = (uint8_t)(registers[REG_05]); // 05 - text_color_blue = (uint8_t)(registers[REG_06]); // 06 - nr_img = (uint8_t)(registers[REG_07]); // 07 + if (registers[7] == MULTIPLE_REG) { // Check if it's a Modbus Write Multiple Registers request (0x10) + // LOG_INFO(TAG, "in writing multiple register mode\n"); + background_red = (uint8_t)(registers[REG_01]); // 01 + background_green = (uint8_t)(registers[REG_02]); // 02 + background_blue = (uint8_t)(registers[REG_03]); // 03 + text_color_red = (uint8_t)(registers[REG_04]); // 04 + text_color_green = (uint8_t)(registers[REG_05]); // 05 + text_color_blue = (uint8_t)(registers[REG_06]); // 06 + nr_img = (uint8_t)(registers[REG_07]); // 07 - //LOG_INFO(TAG, "%d %d %d %d %d %d %d ",background_red,background_green,background_blue,text_color_red,text_color_green,text_color_blue,nr_img); + // LOG_INFO(TAG, "%d %d %d %d %d %d %d + // ",background_red,background_green,background_blue,text_color_red,text_color_green,text_color_blue,nr_img); counter = 0; - for(int i = START_DATA; i < REG_LENGTH; i++){ - if(i % 2 == 0){ + for (int i = START_DATA; i < REG_LENGTH; i++) { + if (i % 2 == 0) { text[counter] = registers[i]; counter++; } @@ -96,80 +95,69 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf * result_txt |= ((uint32_t)text_color_red) << 16; result_txt |= ((uint32_t)text_color_green) << 8; result_txt |= text_color_blue; - // proccesing the image index - size_t number_of_files = llfs_file_count(); // How many files that there are + // Processing the image index + size_t number_of_files = llfs_file_count(); // How many files that there are - llfs_file_t file_list[number_of_files]; // reserving memory for the list + llfs_file_t file_list[number_of_files]; // Reserving memory for the list - number_of_files = llfs_file_list(file_list, number_of_files, NULL); // freeed memory filled with the list + number_of_files = llfs_file_list(file_list, number_of_files, NULL); // Freed memory filled with the list - if(number_of_files < nr_img){ + if (number_of_files < nr_img) { lcd_clear_text(); lcd_clear_images(); lcd_stop_all_gifs(); - lcd_display_text(text, 10, 10, result_txt, result_background, LCD_FONT24); //When no image + lcd_display_text(text, 10, 10, result_txt, result_background, LCD_FONT24); // When no image lcd_display_text("FILE NOT IN FILESYSTEM", 10, 75, LCD_RED, LCD_BLACK, LCD_FONT24); - } - else{ - const char * ext = strrchr(file_list[nr_img - 1].name, '.'); + } else { + const char* ext = strrchr(file_list[nr_img - 1].name, '.'); if (ext == NULL) { - // No valid extension found + // No valid extension found } - if (strcmp(ext,".gif") == 0) { + if (strcmp(ext, ".gif") == 0) { lcd_clear_text(); lcd_clear_images(); lcd_stop_all_gifs(); lcd_display_text(text, 10, 10, result_txt, result_background, LCD_FONT24); - lcd_gif_t* gif = lcd_draw_gif_from_fs(file_list[nr_img - 1].name, 0, 75); //GIF on screen - } else if (strcmp(ext,".bmp") == 0) { + lcd_gif_t* gif = lcd_draw_gif_from_fs(file_list[nr_img - 1].name, 0, 75); // GIF on screen + } else if (strcmp(ext, ".bmp") == 0) { lcd_clear_text(); lcd_clear_images(); lcd_stop_all_gifs(); lcd_display_text(text, 10, 10, result_txt, result_background, LCD_FONT24); - lcd_draw_img_from_fs(file_list[nr_img - 1].name, 0, 75); //BMP on screen + lcd_draw_img_from_fs(file_list[nr_img - 1].name, 0, 75); // BMP on screen } } + } else { + // LOG_INFO(TAG, "not in writing multiple register mode!!!\n"); } - else{ - //LOG_INFO(TAG, "not in writing multiple register mode!!!\n"); - } - } else if (err == ERR_OK){ - tcp_close(pcb); // when everithing was ok close the tcpconnection + } else if (err == ERR_OK) { + tcp_close(pcb); // When everything was ok close the TCP connection } return ERR_OK; } - /** * @fn static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err) - * @brief Sets the function thats being called when theirs incoming data + * @brief Sets the function that's being called when theirs incoming data */ - -static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err) -{ - LWIP_UNUSED_ARG(arg); // Eliminates compiler warning about unused arguments - LWIP_UNUSED_ARG(err); // Eliminates compiler warning about unused arguments - tcp_setprio(pcb, TCP_PRIO_MIN); // Sets the priority of a connection. - tcp_recv(pcb, modbus_incomming_data); // sets which function is being called when new data arives +static err_t modbus_accept(void* arg, struct tcp_pcb* pcb, err_t err) { + LWIP_UNUSED_ARG(arg); // Eliminates compiler warning about unused arguments + LWIP_UNUSED_ARG(err); // Eliminates compiler warning about unused arguments + tcp_setprio(pcb, TCP_PRIO_MIN); // Sets the priority of a connection. + tcp_recv(pcb, modbus_incoming_data); // sets which function is being called when new data arrives return ERR_OK; } - /** * @fn void modbus_init - * @brief Initiallises the modbus tcp + * @brief Initializes the modbus tcp */ +void modbus_init(void) { + struct tcp_pcb* modbus_pcb; + modbus_pcb = tcp_new(); // Creating a new tcp pcb + tcp_bind(modbus_pcb, IP_ADDR_ANY, MODBUSPORT); // Bind the modbus_pcb to port 502 -void modbus_init(void) -{ - struct tcp_pcb *modbus_pcb; - modbus_pcb = tcp_new(); // creating a new tcp pcb - tcp_bind(modbus_pcb, IP_ADDR_ANY, MODBUSPORT); // bind the modbus_pcb to port 502 - - modbus_pcb = tcp_listen(modbus_pcb); // listen - tcp_accept(modbus_pcb, modbus_accept); // set callback function + modbus_pcb = tcp_listen(modbus_pcb); // Listen + tcp_accept(modbus_pcb, modbus_accept); // Set callback function } - - -