From d84bd877e124ae02684d2f2b6a5a0a5eafd140e2 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 6 Nov 2023 21:38:19 +0100 Subject: [PATCH 01/46] TFTP Add initial framework, not doing anything at the moment --- project/Core/Inc/tftp.h | 12 ++++++++++++ project/Core/Src/tftp.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 project/Core/Inc/tftp.h create mode 100644 project/Core/Src/tftp.c diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h new file mode 100644 index 0000000..3ff4cbb --- /dev/null +++ b/project/Core/Inc/tftp.h @@ -0,0 +1,12 @@ +// +// Created by sanderspeetjens on 06/11/23. +// + +#ifndef PROJECT_TFTP_H +#define PROJECT_TFTP_H +#include + +extern struct tftp_context tftpContext_s; + +void tftp_server_init(void); +#endif //PROJECT_TFTP_H diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c new file mode 100644 index 0000000..8c880c4 --- /dev/null +++ b/project/Core/Src/tftp.c @@ -0,0 +1,37 @@ +// +// Created by sanderspeetjens on 06/11/23. +// + +#include "tftp.h" + +void *tftp_open(const char *fname, const char *mode, u8_t write) +{ + +} + +void tftp_close(void *handle) +{ + +} +int tftp_read(void* handle, void* buf, int bytes) +{ + +} + +int tftp_write(void* handle, struct pbuf* p) +{ + +} + +struct tftp_context tftpContext_s = + { + tftp_open, + tftp_close, + tftp_read, + tftp_write + }; + +void tftp_server_init(void) +{ + tftp_init(&tftpContext_s); +} \ No newline at end of file From 315871af607d9045ec1ca7a94c3d1d6b7d36da3f Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 6 Nov 2023 21:44:18 +0100 Subject: [PATCH 02/46] TFTP Add logger and checks for initialising --- project/Core/Inc/tftp.h | 2 ++ project/Core/Src/tftp.c | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 3ff4cbb..6446e4e 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -4,6 +4,8 @@ #ifndef PROJECT_TFTP_H #define PROJECT_TFTP_H +#define LOGGER_LEVEL_ALL +#include "log.h" #include extern struct tftp_context tftpContext_s; diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 8c880c4..9876e9c 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -4,34 +4,35 @@ #include "tftp.h" -void *tftp_open(const char *fname, const char *mode, u8_t write) -{ +static const char* TAG = "tftp_server"; + +void *tftp_open(const char *fname, const char *mode, u8_t write) { } -void tftp_close(void *handle) -{ +void tftp_close(void *handle) { } -int tftp_read(void* handle, void* buf, int bytes) -{ +int tftp_read(void* handle, void* buf, int bytes) { } -int tftp_write(void* handle, struct pbuf* p) -{ +int tftp_write(void* handle, struct pbuf* p) { } -struct tftp_context tftpContext_s = - { +struct tftp_context tftpContext_s = { tftp_open, tftp_close, tftp_read, tftp_write }; -void tftp_server_init(void) -{ - tftp_init(&tftpContext_s); +void tftp_server_init(void) { + LOG_INFO(TAG, "Initializing tftp server"); + if (tftp_init(&tftpContext_s) != ERR_OK) { + LOG_FATAL(TAG, "Could not initialize tftp server"); + return; + } + LOG_INFO(TAG, "tftp server initialized successfully"); } \ No newline at end of file From 20650b9c03dbb7e6dc7272f297daa8395e2b3e1a Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 6 Nov 2023 21:53:06 +0100 Subject: [PATCH 03/46] TFTP Change headers + Add logging --- project/Core/Inc/tftp.h | 8 +++++--- project/Core/Src/tftp.c | 16 +++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 6446e4e..f4b152b 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -1,6 +1,8 @@ -// -// Created by sanderspeetjens on 06/11/23. -// +/** + * @file tftp.h + * @brief tftp server + * @author Speetjens S. + */ #ifndef PROJECT_TFTP_H #define PROJECT_TFTP_H diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 9876e9c..e219c54 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -1,24 +1,26 @@ -// -// Created by sanderspeetjens on 06/11/23. -// +/** + * @file tftp.c + * @brief tftp server + * @author Speetjens S. + */ #include "tftp.h" static const char* TAG = "tftp_server"; void *tftp_open(const char *fname, const char *mode, u8_t write) { - + LOG_INFO(TAG, "Opening %s", fname); } void tftp_close(void *handle) { - + LOG_INFO(TAG, "closing file"); } int tftp_read(void* handle, void* buf, int bytes) { - + LOG_INFO(TAG, "reading file"); } int tftp_write(void* handle, struct pbuf* p) { - + LOG_INFO(TAG, "Writing file"); } struct tftp_context tftpContext_s = { From 1a6a2db34e957f1ffa34ca0525802c3a7deb586c Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 6 Nov 2023 22:05:43 +0100 Subject: [PATCH 04/46] TFTP make the context more readable --- project/Core/Src/tftp.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index e219c54..ddbfc5d 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -24,10 +24,10 @@ int tftp_write(void* handle, struct pbuf* p) { } struct tftp_context tftpContext_s = { - tftp_open, - tftp_close, - tftp_read, - tftp_write + .open = tftp_open, + .close = tftp_close, + .read = tftp_read, + .write = tftp_write }; void tftp_server_init(void) { From 12370baf22a9ff4e110a2f9565f4cbd053453b40 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 6 Nov 2023 22:30:29 +0100 Subject: [PATCH 05/46] TFTP Format --- project/Core/Inc/tftp.h | 6 +++--- project/Core/Src/tftp.c | 11 +++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index f4b152b..1e3a520 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -7,10 +7,10 @@ #ifndef PROJECT_TFTP_H #define PROJECT_TFTP_H #define LOGGER_LEVEL_ALL -#include "log.h" #include +#include "log.h" -extern struct tftp_context tftpContext_s; +extern struct tftp_context tftpContext_s; void tftp_server_init(void); -#endif //PROJECT_TFTP_H +#endif // PROJECT_TFTP_H diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index ddbfc5d..0ca2083 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -8,11 +8,11 @@ static const char* TAG = "tftp_server"; -void *tftp_open(const char *fname, const char *mode, u8_t write) { +void* tftp_open(const char* fname, const char* mode, u8_t write) { LOG_INFO(TAG, "Opening %s", fname); } -void tftp_close(void *handle) { +void tftp_close(void* handle) { LOG_INFO(TAG, "closing file"); } int tftp_read(void* handle, void* buf, int bytes) { @@ -23,12 +23,7 @@ int tftp_write(void* handle, struct pbuf* p) { LOG_INFO(TAG, "Writing file"); } -struct tftp_context tftpContext_s = { - .open = tftp_open, - .close = tftp_close, - .read = tftp_read, - .write = tftp_write - }; +struct tftp_context tftpContext_s = {.open = tftp_open, .close = tftp_close, .read = tftp_read, .write = tftp_write}; void tftp_server_init(void) { LOG_INFO(TAG, "Initializing tftp server"); From a1f7b0ad337bd6b764aaab91c73aaa239982c0df Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 16:39:08 +0100 Subject: [PATCH 06/46] TFTP First implementation of the read and open functions Write is not implemented yet --- project/Core/Inc/tftp.h | 5 +++ project/Core/Src/tftp.c | 75 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 1e3a520..02c0683 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -9,6 +9,11 @@ #define LOGGER_LEVEL_ALL #include #include "log.h" +#include "llfs.h" +#include "stdlib.h" +#include "string.h" + +#define TFTP_READ 0 extern struct tftp_context tftpContext_s; diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 0ca2083..44adf2c 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -8,23 +8,96 @@ static const char* TAG = "tftp_server"; +extern struct llfs_data_file* llfs_root; + +static llfs_file_t virt_file[2] = { + {.name = "index.txt", .data = "test", .len = 4}, + {.name = "virtImage.raw", .data = "test", .len = 4} + }; + +/** + * @brief tftp helper functions + */ + +/** + * @brief This function is called when a file is opened + * It should return a handle to the file or NULL if the file does not exist + * The handle contains a ptr to or the actual file data or a virtual file + * + * @param fname The name of the file to open + * @param mode Mode string from TFTP RFC + * @param write Flag indicating read (0) or write (!= 0) access + * @return void* File handle supplied to other functions + */ void* tftp_open(const char* fname, const char* mode, u8_t write) { LOG_INFO(TAG, "Opening %s", fname); + llfs_file_t* file; + + if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { + file = &virt_file[0]; + } else if (strcmp(fname, virt_file[1].name) == 0 && write != TFTP_READ) { + file = &virt_file[1]; + } + + file = llfs_file_open(fname); + return file; } +/** + * @brief This function is called when a file is closed + * + * @param handle The handle to the file to close + */ void tftp_close(void* handle) { LOG_INFO(TAG, "closing file"); } + +/** + * @brief This function is called when a file is read + * + * @param handle File handle returned by open() + * @param buf Target buffer to copy read data to + * @param bytes Number of bytes to copy to buf + * @return int >= 0: Success; < 0: Error + */ int tftp_read(void* handle, void* buf, int bytes) { LOG_INFO(TAG, "reading file"); + if (handle == NULL) { + LOG_ERROR(TAG, "handle is null"); + return -1; + } + llfs_file_t* file = (llfs_file_t*)handle; + LOG_INFO(TAG, "reading file: %s", file->name); + + if (file == &virt_file[0]) { + const struct llfs_data_file* root = llfs_root; + while(root != NULL) { + snprintf(buf, bytes, "%s\n", root->name); + file = root->next; + } + } else if (file == &virt_file[1]) { + LOG_CRIT(TAG, "Exception: Trying to read a write only file"); + return -1; + } + memcpy(buf, file->data, bytes); + return 0; } int tftp_write(void* handle, struct pbuf* p) { LOG_INFO(TAG, "Writing file"); + LOG_DEBUG(TAG, "Not implemented yet"); } -struct tftp_context tftpContext_s = {.open = tftp_open, .close = tftp_close, .read = tftp_read, .write = tftp_write}; +struct tftp_context tftpContext_s = { + .open = tftp_open, + .close = tftp_close, + .read = tftp_read, + .write = tftp_write + }; +/** + * @brief Initialize tftp server + */ void tftp_server_init(void) { LOG_INFO(TAG, "Initializing tftp server"); if (tftp_init(&tftpContext_s) != ERR_OK) { From 6b0707cfa244b2ff1565e92a41a21a903f689e7d Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 16:42:20 +0100 Subject: [PATCH 07/46] TFTP Fix critical error in open implementation --- project/Core/Src/tftp.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 44adf2c..532a5d6 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -34,13 +34,12 @@ void* tftp_open(const char* fname, const char* mode, u8_t write) { llfs_file_t* file; if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { - file = &virt_file[0]; + return &virt_file[0]; } else if (strcmp(fname, virt_file[1].name) == 0 && write != TFTP_READ) { - file = &virt_file[1]; + return &virt_file[1]; } - file = llfs_file_open(fname); - return file; + return llfs_file_open(fname); } /** @@ -54,6 +53,8 @@ void tftp_close(void* handle) { /** * @brief This function is called when a file is read + * The virtual files are filtered out first + * then the file is trying to get read from the llfs * * @param handle File handle returned by open() * @param buf Target buffer to copy read data to From 989b8b620c6289814f89e9bffa9e38319ece16ad Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 16:47:49 +0100 Subject: [PATCH 08/46] TFTP Add virtual files to the file list --- project/Core/Src/tftp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 532a5d6..1cc52dd 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -72,6 +72,8 @@ int tftp_read(void* handle, void* buf, int bytes) { if (file == &virt_file[0]) { const struct llfs_data_file* root = llfs_root; + snprintf(buf, bytes, "%s\n", virt_file[0].name); + snprintf(buf, bytes, "%s\n", virt_file[1].name); while(root != NULL) { snprintf(buf, bytes, "%s\n", root->name); file = root->next; From b2345b66a09acb481fa9df006bb6710d4a2f112e Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 16:48:00 +0100 Subject: [PATCH 09/46] TFTP Add initial doc --- docs/tftp.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 docs/tftp.md diff --git a/docs/tftp.md b/docs/tftp.md new file mode 100644 index 0000000..42bfc79 --- /dev/null +++ b/docs/tftp.md @@ -0,0 +1,21 @@ +# TFTP +This is the documentation of the TFTP task +## Initialization +The TFTP task is initialized in the main function. +```c +// Initialize TFTP task +tftp_init(); +``` +## Usage +The TFTP task is used to receive and send files via TFTP. +### Receive a file +index.txt contains a list of files on the file system. +```bash +tftp -g -r +``` +### Send a file +You can only write to the following files: +- virtImage.raw +```bash +tftp -p -l +``` From 73f9228afeba704a3907986d468f4bf34d2ab8e5 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 16:55:30 +0100 Subject: [PATCH 10/46] TFTP Add doc to writ function --- project/Core/Src/tftp.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 1cc52dd..61ba699 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -57,9 +57,9 @@ void tftp_close(void* handle) { * then the file is trying to get read from the llfs * * @param handle File handle returned by open() - * @param buf Target buffer to copy read data to - * @param bytes Number of bytes to copy to buf - * @return int >= 0: Success; < 0: Error + * @param buf Target buffer to copy read data to + * @param bytes Number of bytes to copy to buf + * @return int >= 0: Success; < 0: Error */ int tftp_read(void* handle, void* buf, int bytes) { LOG_INFO(TAG, "reading file"); @@ -86,6 +86,14 @@ int tftp_read(void* handle, void* buf, int bytes) { return 0; } +/** + * @brief This function is called when a file is written + * + * @param handle File handle returned by open() + * @param p PBUF adjusted such that payload pointer points to the beginning of write data. + * In other words, TFTP headers are stripped off. + * @return int >= 0: Success; < 0: Error + */ int tftp_write(void* handle, struct pbuf* p) { LOG_INFO(TAG, "Writing file"); LOG_DEBUG(TAG, "Not implemented yet"); From 8237f7911b2c9a175bc0683348b7be252997dc36 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 16:56:43 +0100 Subject: [PATCH 11/46] TFTP Remove tftp context from header file It should not be used anywhere else --- project/Core/Inc/tftp.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 02c0683..c820e63 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -15,7 +15,5 @@ #define TFTP_READ 0 -extern struct tftp_context tftpContext_s; - void tftp_server_init(void); #endif // PROJECT_TFTP_H From 81da833c5b947e3814968f3d4b481a617772af0c Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 9 Nov 2023 18:07:47 +0100 Subject: [PATCH 12/46] TFTP Fix error of not writing the index.txt correctly --- project/Core/Src/tftp.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 61ba699..70423df 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -13,7 +13,28 @@ extern struct llfs_data_file* llfs_root; static llfs_file_t virt_file[2] = { {.name = "index.txt", .data = "test", .len = 4}, {.name = "virtImage.raw", .data = "test", .len = 4} - }; + }; + +int str_cat_str(char* dest, int dest_size, const char* src) { + int dest_len = strlen(dest); + int src_len = strlen(src); + if (dest_len + src_len > dest_size) { + return -1; + } + memcpy(dest + dest_len, src, src_len); + return 0; +} + +int str_cat(char* dest, int dest_size, char c) +{ + int dest_len = strlen(dest); + if (dest_len + 1 > dest_size) { + return -1; + } + dest[dest_len] = c; + dest[dest_len + 1] = '\0'; + return 0; +} /** * @brief tftp helper functions @@ -72,10 +93,13 @@ int tftp_read(void* handle, void* buf, int bytes) { if (file == &virt_file[0]) { const struct llfs_data_file* root = llfs_root; - snprintf(buf, bytes, "%s\n", virt_file[0].name); - snprintf(buf, bytes, "%s\n", virt_file[1].name); + str_cat_str(buf, bytes, virt_file[0].name); + str_cat(buf, bytes, '\n'); + str_cat_str(buf, bytes, virt_file[1].name); + str_cat(buf, bytes, '\n'); while(root != NULL) { - snprintf(buf, bytes, "%s\n", root->name); + str_cat(buf, bytes, root->name); + str_cat(buf, bytes, '\n'); file = root->next; } } else if (file == &virt_file[1]) { From e1118908d92c2e88e7d798a44a4c98d36beaa760 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sat, 11 Nov 2023 00:38:39 +0100 Subject: [PATCH 13/46] TFTP Move over to the new fread that Lorentz is implementing --- project/Core/Inc/tftp.h | 5 +++-- project/Core/Src/tftp.c | 21 ++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index c820e63..4e72e16 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -10,8 +10,9 @@ #include #include "log.h" #include "llfs.h" -#include "stdlib.h" -#include "string.h" +#include +#include +#include #define TFTP_READ 0 diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 70423df..15a8759 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -52,7 +52,6 @@ int str_cat(char* dest, int dest_size, char c) */ void* tftp_open(const char* fname, const char* mode, u8_t write) { LOG_INFO(TAG, "Opening %s", fname); - llfs_file_t* file; if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { return &virt_file[0]; @@ -60,7 +59,8 @@ void* tftp_open(const char* fname, const char* mode, u8_t write) { return &virt_file[1]; } - return llfs_file_open(fname); + // TODO: waiting on Lorentz to finish creating f* functions for LLFS + return fopen(fname, write ? "wb" : "rb"); } /** @@ -70,6 +70,16 @@ void* tftp_open(const char* fname, const char* mode, u8_t write) { */ void tftp_close(void* handle) { LOG_INFO(TAG, "closing file"); + if (handle == NULL) { + LOG_CRIT(TAG, "handle is null"); + return; + } + if (handle == &virt_file[0] || handle == &virt_file[1]) { + return; + } + + // TODO: waiting on Lorentz to finish creating f* functions for LLFS + fclose((FILE*)handle); } /** @@ -85,10 +95,10 @@ void tftp_close(void* handle) { int tftp_read(void* handle, void* buf, int bytes) { LOG_INFO(TAG, "reading file"); if (handle == NULL) { - LOG_ERROR(TAG, "handle is null"); + LOG_CRIT(TAG, "handle is null"); return -1; } - llfs_file_t* file = (llfs_file_t*)handle; + FILE* file = (FILE*)handle; LOG_INFO(TAG, "reading file: %s", file->name); if (file == &virt_file[0]) { @@ -106,7 +116,8 @@ int tftp_read(void* handle, void* buf, int bytes) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; } - memcpy(buf, file->data, bytes); + // TODO: waiting on Lorentz to finish creating f* functions for LLFS + fread(buf, sizeof(uint8_t), bytes, file); return 0; } From d35c79480ef2749596867ace17c845df47ff3ff7 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sat, 11 Nov 2023 00:46:02 +0100 Subject: [PATCH 14/46] TFTP Change to my own struct for the virtual files init the index.txt from the tftp_server_init add tftp_server_deinit + add to docs --- docs/tftp.md | 7 +++++ project/Core/Inc/tftp.h | 8 +++++ project/Core/Src/tftp.c | 69 ++++++++++++++++++++++++++++++++--------- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/docs/tftp.md b/docs/tftp.md index 42bfc79..a9cc1c2 100644 --- a/docs/tftp.md +++ b/docs/tftp.md @@ -6,6 +6,13 @@ The TFTP task is initialized in the main function. // Initialize TFTP task tftp_init(); ``` +## Deinitialization +If you would ever want to deinitialize the TFTP task, you can do so by calling the following function. +```c +// Deinitialize TFTP task +tftp_server_deinit(); +``` + ## Usage The TFTP task is used to receive and send files via TFTP. ### Receive a file diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 4e72e16..0ed381e 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -16,5 +16,13 @@ #define TFTP_READ 0 +typedef struct tftp_custom_file_s { + char* data; + size_t len; + char*name; + size_t ofset; +}tftp_custom_file_t; + void tftp_server_init(void); +void tftp_server_deinit(void); #endif // PROJECT_TFTP_H diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 15a8759..e50ee3a 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -10,9 +10,10 @@ static const char* TAG = "tftp_server"; extern struct llfs_data_file* llfs_root; -static llfs_file_t virt_file[2] = { - {.name = "index.txt", .data = "test", .len = 4}, - {.name = "virtImage.raw", .data = "test", .len = 4} +static tftp_custom_file_t virt_file[] = + { + {.name = "index.txt",.data = NULL, .len = 0, .ofset = 0}, + {.name = "virtImage.raw",.data = NULL, .len = 0, .ofset = 0} }; int str_cat_str(char* dest, int dest_size, const char* src) { @@ -99,19 +100,12 @@ int tftp_read(void* handle, void* buf, int bytes) { return -1; } FILE* file = (FILE*)handle; - LOG_INFO(TAG, "reading file: %s", file->name); if (file == &virt_file[0]) { - const struct llfs_data_file* root = llfs_root; - str_cat_str(buf, bytes, virt_file[0].name); - str_cat(buf, bytes, '\n'); - str_cat_str(buf, bytes, virt_file[1].name); - str_cat(buf, bytes, '\n'); - while(root != NULL) { - str_cat(buf, bytes, root->name); - str_cat(buf, bytes, '\n'); - file = root->next; - } + // TODO: read index.txt using tftp_custom_file + LOG_CRIT(TAG, "Reading from index.txt is not implemented yet"); + return -1; + } else if (file == &virt_file[1]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; @@ -134,6 +128,39 @@ int tftp_write(void* handle, struct pbuf* p) { LOG_DEBUG(TAG, "Not implemented yet"); } +/** + * @brief This function creates the file list for index.txt + */ +void init_index(void) +{ + int len = 0; + // Add len of the virt files to the size + for (int i = 0; i < 2; i++) + { + len += strlen(virt_file[i].name) + 1; + } + const struct llfs_data_file* root = llfs_root; + while(root != NULL) { + len += strlen(root->name) + 1; + root = root->next; + } + + virt_file[0].data = malloc(len); + virt_file[0].len = len; + + for (int i = 0; i < 2; i++) + { + str_cat_str(virt_file[0].data, len, virt_file[i].name); + str_cat(virt_file[0].data, len, '\n'); + } + root = llfs_root; + while(root != NULL) { + str_cat_str(virt_file[0].data, len, root->name); + str_cat(virt_file[0].data, len, '\n'); + root = root->next; + } +} + struct tftp_context tftpContext_s = { .open = tftp_open, .close = tftp_close, @@ -146,9 +173,23 @@ struct tftp_context tftpContext_s = { */ void tftp_server_init(void) { LOG_INFO(TAG, "Initializing tftp server"); + // init the index.txt virt_file + init_index(); + + // Init the tftp server if (tftp_init(&tftpContext_s) != ERR_OK) { LOG_FATAL(TAG, "Could not initialize tftp server"); return; } LOG_INFO(TAG, "tftp server initialized successfully"); +} + +void tftp_server_deinit(void) { + LOG_INFO(TAG, "Deinitializing tftp server"); + tftp_cleanup(); + LOG_INFO(TAG, "tftp server deinitialized successfully"); + free(virt_file[0].data); + virt_file[0].data = NULL; + virt_file[0].len = 0; + virt_file[0].ofset = 0; } \ No newline at end of file From d436447a214acde5053f0782d7e03b3ffdb4c3ed Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sun, 12 Nov 2023 14:02:37 +0100 Subject: [PATCH 15/46] TFTP Change str_cat* functions to use size_t instead of int --- project/Core/Src/tftp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index e50ee3a..8d085bd 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -16,7 +16,7 @@ static tftp_custom_file_t virt_file[] = {.name = "virtImage.raw",.data = NULL, .len = 0, .ofset = 0} }; -int str_cat_str(char* dest, int dest_size, const char* src) { +int str_cat_str(char* dest, size_t dest_size, const char* src) { int dest_len = strlen(dest); int src_len = strlen(src); if (dest_len + src_len > dest_size) { @@ -26,7 +26,7 @@ int str_cat_str(char* dest, int dest_size, const char* src) { return 0; } -int str_cat(char* dest, int dest_size, char c) +int str_cat(char* dest, size_t dest_size, char c) { int dest_len = strlen(dest); if (dest_len + 1 > dest_size) { From 7c76a37ec95a30278bdd6aa773b97f6afa495fef Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sun, 12 Nov 2023 14:33:15 +0100 Subject: [PATCH 16/46] TFTP Add custom fseek and fread Add test framework gtest and added my custom fread and fseek tests + functions --- .gitignore | 3 ++- CMakeLists.txt | 22 +++++++++++++++ project/Core/Src/tftp.c | 60 ++++++++++++++++++++++++++++++++++++----- tests/CMakeLists.txt | 28 +++++++++++++++++++ tests/tfpt_tests.cpp | 45 +++++++++++++++++++++++++++++++ tests/tftp.cpp | 45 +++++++++++++++++++++++++++++++ tests/tftp.hpp | 17 ++++++++++++ 7 files changed, 212 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 tests/CMakeLists.txt create mode 100644 tests/tfpt_tests.cpp create mode 100644 tests/tftp.cpp create mode 100644 tests/tftp.hpp diff --git a/.gitignore b/.gitignore index e818d98..be632a7 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ project/.mxproject project/project.launch -project/.cproject \ No newline at end of file +project/.cproject +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d4d1025 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) + +project(WSAA_tests LANGUAGES CXX C) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(VERSION_MAJOR 1) +set(VERSION_MINOR 0) + +include(GNUInstallDirs) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY + ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) +set(PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) + +enable_testing() +add_subdirectory(tests) \ No newline at end of file diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 8d085bd..863e269 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -37,6 +37,50 @@ int str_cat(char* dest, size_t dest_size, char c) return 0; } +/** + * @brief tftp custom file functions to set the offset and read the data + * @param[in,out] handle Custom file handles + * @param[in] offset The offset to set + * @param[in] whence The origin of the offset + */ +void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { + switch (whence) { + case SEEK_SET: + handle->ofset = offset; + break; + case SEEK_CUR: + handle->ofset += offset; + break; + case SEEK_END: + break; + } + if (handle->ofset > handle->len) { + handle->ofset = handle->len; + } +} + +/** + * @brief tftp custom file functions to read the data + * auto rolling over the offset + * if the bytes to read is bigger than the remaining bytes + * it will read the remaining bytes and set the bytes to 0 + * @param[out] buf The buffer to write the data to + * @param[in] bytes The number of bytes to read + * @param[in,out] handle Custom file handles + */ +size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { + if (handle->ofset + bytes > handle->len) { + bytes = handle->len - handle->ofset; + } + memcpy(buf, handle->data + handle->ofset, bytes); + handle->ofset += bytes; + ((char*)buf)[bytes] = '\0'; + if (handle->ofset > handle->len) { + bytes = 0; + } + return bytes; +} + /** * @brief tftp helper functions */ @@ -94,6 +138,7 @@ void tftp_close(void* handle) { * @return int >= 0: Success; < 0: Error */ int tftp_read(void* handle, void* buf, int bytes) { + int ret = 0; LOG_INFO(TAG, "reading file"); if (handle == NULL) { LOG_CRIT(TAG, "handle is null"); @@ -102,17 +147,18 @@ int tftp_read(void* handle, void* buf, int bytes) { FILE* file = (FILE*)handle; if (file == &virt_file[0]) { - // TODO: read index.txt using tftp_custom_file - LOG_CRIT(TAG, "Reading from index.txt is not implemented yet"); - return -1; - + ret = tftp_custom_fread(buf, bytes, file); + return ret; } else if (file == &virt_file[1]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; } // TODO: waiting on Lorentz to finish creating f* functions for LLFS - fread(buf, sizeof(uint8_t), bytes, file); - return 0; + ret = fread(buf, sizeof(uint8_t), bytes, file); + if (ret <= 0) { + return -1; + } + return ret; } /** @@ -144,7 +190,7 @@ void init_index(void) len += strlen(root->name) + 1; root = root->next; } - + len++; // +1 for the \0 virt_file[0].data = malloc(len); virt_file[0].len = len; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3b36bc7 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,28 @@ +find_package(GTest REQUIRED) + +# Third Party +include_directories(${GTEST_INCLUDE_DIR}) + +link_directories(${GTEST_LIB_DIR}) + +# tests +file(GLOB_RECURSE TEST_SOURCES "*.cpp") +add_executable(tests ${TEST_SOURCES}) + +target_compile_options(tests PRIVATE $<$: + -Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion + >) +target_link_libraries(tests + PRIVATE + gtest + GTest::gtest_main +) + +target_include_directories(tests + PUBLIC + ${CMAKE_CURRENT_LIST_DIR} + ${PROJECT_BINARY_DIR} +) + +include(GoogleTest) +gtest_discover_tests(tests) \ No newline at end of file diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp new file mode 100644 index 0000000..e6642e2 --- /dev/null +++ b/tests/tfpt_tests.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +#include "tftp.hpp" + +tftp_custom_file_t file = { + .data = (char*)"1234567890", + .len = 11, + .name = (char*)"test.txt", + .ofset = 0 + }; + +TEST(TFTP, custom_fseek) +{ + tftp_custom_fseek(&file, 5, SEEK_SET); + EXPECT_EQ(file.ofset, 5); + tftp_custom_fseek(&file, 5, SEEK_CUR); + EXPECT_EQ(file.ofset, 10); +} + +TEST(TFTP, custom_fread) +{ + char buf[11]; + tftp_custom_fseek(&file, 0, SEEK_SET); + size_t bytes = tftp_custom_fread(buf, 11, &file); + EXPECT_EQ(bytes, 11); + EXPECT_EQ(file.ofset, 11); + + memset(buf, 0, 11); + + tftp_custom_fseek(&file, 0, SEEK_SET); + bytes = tftp_custom_fread(buf, 11, &file); + EXPECT_EQ(bytes, 11); + EXPECT_EQ(memcmp(buf, "1234567890", 10), 0); + + tftp_custom_fseek(&file, 0, SEEK_SET); + bytes = tftp_custom_fread(buf, 5, &file); + EXPECT_EQ(bytes, 5); + EXPECT_EQ(memcmp(buf, "12345", 5), 0); +} + + diff --git a/tests/tftp.cpp b/tests/tftp.cpp new file mode 100644 index 0000000..4def0eb --- /dev/null +++ b/tests/tftp.cpp @@ -0,0 +1,45 @@ +#include "tftp.hpp" + +/** + * @brief tftp custom file functions to set the offset and read the data + * @param[in,out] handle Custom file handles + * @param[in] offset The offset to set + * @param[in] whence The origin of the offset + */ +void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { + switch (whence) { + case SEEK_SET: + handle->ofset = offset; + break; + case SEEK_CUR: + handle->ofset += offset; + break; + case SEEK_END: + break; + } + if (handle->ofset > handle->len) { + handle->ofset = handle->len; + } +} + +/** + * @brief tftp custom file functions to read the data + * auto rolling over the offset + * if the bytes to read is bigger than the remaining bytes + * it will read the remaining bytes and set the bytes to 0 + * @param[out] buf The buffer to write the data to + * @param[in] bytes The number of bytes to read + * @param[in,out] handle Custom file handles + */ +size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { + if (handle->ofset + bytes > handle->len) { + bytes = handle->len - handle->ofset; + } + memcpy(buf, handle->data + handle->ofset, bytes); + handle->ofset += bytes; + ((char*)buf)[bytes] = '\0'; + if (handle->ofset > handle->len) { + bytes = 0; + } + return bytes; +} \ No newline at end of file diff --git a/tests/tftp.hpp b/tests/tftp.hpp new file mode 100644 index 0000000..868b1da --- /dev/null +++ b/tests/tftp.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include +#include +#include + +typedef struct tftp_custom_file_s { + const char* data; + size_t len; + char*name; + size_t ofset; +}tftp_custom_file_t; + +void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); +size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); \ No newline at end of file From 34c0beb43d413ad2cb8c4b3b28ab37467f621cd9 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sun, 12 Nov 2023 14:36:15 +0100 Subject: [PATCH 17/46] TFTP Forgot to reset the offset to 0 when opening a custom file --- project/Core/Src/tftp.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 863e269..a467be6 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -99,6 +99,7 @@ void* tftp_open(const char* fname, const char* mode, u8_t write) { LOG_INFO(TAG, "Opening %s", fname); if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { + tftp_custom_fseek(&virt_file[0], 0, SEEK_SET); return &virt_file[0]; } else if (strcmp(fname, virt_file[1].name) == 0 && write != TFTP_READ) { return &virt_file[1]; From 435212d8071cc858b566762d06bf6a81f6ce508d Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sun, 12 Nov 2023 14:39:34 +0100 Subject: [PATCH 18/46] TFTP Fix warnings of not type casting a ptr --- project/Core/Src/tftp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index a467be6..2796715 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -147,10 +147,10 @@ int tftp_read(void* handle, void* buf, int bytes) { } FILE* file = (FILE*)handle; - if (file == &virt_file[0]) { - ret = tftp_custom_fread(buf, bytes, file); + if ((tftp_custom_file_t*)file == &virt_file[0]) { + ret = tftp_custom_fread(buf, bytes, (tftp_custom_file_t*)file); return ret; - } else if (file == &virt_file[1]) { + } else if ((tftp_custom_file_t*)file == &virt_file[1]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; } From 693865a70f8788fa8bf961048b3699339fea54ac Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sun, 12 Nov 2023 14:46:52 +0100 Subject: [PATCH 19/46] TFTP Add extra test case for tftp_custom_fread --- tests/tfpt_tests.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp index e6642e2..96d0e11 100644 --- a/tests/tfpt_tests.cpp +++ b/tests/tfpt_tests.cpp @@ -36,10 +36,18 @@ TEST(TFTP, custom_fread) EXPECT_EQ(bytes, 11); EXPECT_EQ(memcmp(buf, "1234567890", 10), 0); + memset(buf, 0, 11); + tftp_custom_fseek(&file, 0, SEEK_SET); bytes = tftp_custom_fread(buf, 5, &file); EXPECT_EQ(bytes, 5); EXPECT_EQ(memcmp(buf, "12345", 5), 0); + + memset(buf, 0, 11); + + bytes = tftp_custom_fread(buf, 5, &file); + EXPECT_EQ(bytes, 5); + EXPECT_EQ(memcmp(buf, "67890", 5), 0); } From 91bd047201746dec0096f9ae5f1879caed629b67 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Sun, 12 Nov 2023 16:14:26 +0100 Subject: [PATCH 20/46] TFTP Change header to match the others --- project/Core/Inc/tftp.h | 2 +- project/Core/Src/tftp.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 0ed381e..57ae68d 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -1,7 +1,7 @@ /** * @file tftp.h * @brief tftp server - * @author Speetjens S. + * @author Sander S. */ #ifndef PROJECT_TFTP_H diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 2796715..d276477 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -1,7 +1,7 @@ /** * @file tftp.c * @brief tftp server - * @author Speetjens S. + * @author Sander S. */ #include "tftp.h" From 04f9dd3d2cfcc6ca829ea11a46a543fcd61078f2 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 12 Nov 2023 17:06:56 +0100 Subject: [PATCH 21/46] TFTP Fix some warnings --- project/Core/Src/tftp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index d276477..454e7d9 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -17,8 +17,8 @@ static tftp_custom_file_t virt_file[] = }; int str_cat_str(char* dest, size_t dest_size, const char* src) { - int dest_len = strlen(dest); - int src_len = strlen(src); + size_t dest_len = strlen(dest); + size_t src_len = strlen(src); if (dest_len + src_len > dest_size) { return -1; } @@ -28,7 +28,7 @@ int str_cat_str(char* dest, size_t dest_size, const char* src) { int str_cat(char* dest, size_t dest_size, char c) { - int dest_len = strlen(dest); + size_t dest_len = strlen(dest); if (dest_len + 1 > dest_size) { return -1; } @@ -95,7 +95,7 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { * @param write Flag indicating read (0) or write (!= 0) access * @return void* File handle supplied to other functions */ -void* tftp_open(const char* fname, const char* mode, u8_t write) { +void* tftp_open(const char* fname, const char* mode, uint8_t write) { LOG_INFO(TAG, "Opening %s", fname); if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { @@ -180,7 +180,7 @@ int tftp_write(void* handle, struct pbuf* p) { */ void init_index(void) { - int len = 0; + size_t len = 0; // Add len of the virt files to the size for (int i = 0; i < 2; i++) { From e682528e126c47b11ce9f11273e17a1670c2196f Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 12 Nov 2023 17:07:20 +0100 Subject: [PATCH 22/46] TFTP Change tests from a copy to using the original with mocs --- project/Core/Inc/tftp.h | 12 ++++++++++- tests/CMakeLists.txt | 11 ++++++++-- tests/mocs.c | 19 +++++++++++++++++ tests/tfpt_tests.cpp | 2 +- tests/tftp.cpp | 45 ----------------------------------------- tests/tftp.hpp | 17 ---------------- tests/tftp_server.h | 37 +++++++++++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 66 deletions(-) create mode 100644 tests/mocs.c delete mode 100644 tests/tftp.cpp delete mode 100644 tests/tftp.hpp create mode 100644 tests/tftp_server.h diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 57ae68d..e33e855 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -6,8 +6,11 @@ #ifndef PROJECT_TFTP_H #define PROJECT_TFTP_H -#define LOGGER_LEVEL_ALL #include +#ifdef __cplusplus +extern "C" { +#endif +#define LOGGER_LEVEL_ALL #include "log.h" #include "llfs.h" #include @@ -25,4 +28,11 @@ typedef struct tftp_custom_file_s { void tftp_server_init(void); void tftp_server_deinit(void); +void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); +size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); + +#ifdef __cplusplus +} +#endif + #endif // PROJECT_TFTP_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3b36bc7..1b931ee 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,8 +6,14 @@ include_directories(${GTEST_INCLUDE_DIR}) link_directories(${GTEST_LIB_DIR}) # tests -file(GLOB_RECURSE TEST_SOURCES "*.cpp") -add_executable(tests ${TEST_SOURCES}) +file(GLOB_RECURSE TEST_SOURCES "*.cpp" "*.c") +add_executable(tests) + +target_sources(tests + PRIVATE + ${TEST_SOURCES} + ../project/Core/Src/tftp.c +) target_compile_options(tests PRIVATE $<$: -Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion @@ -22,6 +28,7 @@ target_include_directories(tests PUBLIC ${CMAKE_CURRENT_LIST_DIR} ${PROJECT_BINARY_DIR} + ../project/Core/Inc/ ) include(GoogleTest) diff --git a/tests/mocs.c b/tests/mocs.c new file mode 100644 index 0000000..26fff82 --- /dev/null +++ b/tests/mocs.c @@ -0,0 +1,19 @@ +#include "tftp.h" + +struct llfs_data_file llfs_root = { + .data = NULL, + .len = 0, + .name = "root", + .next = NULL, +}; + +void tftp_cleanup(void) { + +} +uint32_t logger_get_timestamp(void) { + return 0; +} + +int tftp_init(struct tftp_context* context) { + return 0; +} diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp index 96d0e11..4fd19f2 100644 --- a/tests/tfpt_tests.cpp +++ b/tests/tfpt_tests.cpp @@ -4,7 +4,7 @@ #include #include -#include "tftp.hpp" +#include "tftp.h" tftp_custom_file_t file = { .data = (char*)"1234567890", diff --git a/tests/tftp.cpp b/tests/tftp.cpp deleted file mode 100644 index 4def0eb..0000000 --- a/tests/tftp.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "tftp.hpp" - -/** - * @brief tftp custom file functions to set the offset and read the data - * @param[in,out] handle Custom file handles - * @param[in] offset The offset to set - * @param[in] whence The origin of the offset - */ -void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { - switch (whence) { - case SEEK_SET: - handle->ofset = offset; - break; - case SEEK_CUR: - handle->ofset += offset; - break; - case SEEK_END: - break; - } - if (handle->ofset > handle->len) { - handle->ofset = handle->len; - } -} - -/** - * @brief tftp custom file functions to read the data - * auto rolling over the offset - * if the bytes to read is bigger than the remaining bytes - * it will read the remaining bytes and set the bytes to 0 - * @param[out] buf The buffer to write the data to - * @param[in] bytes The number of bytes to read - * @param[in,out] handle Custom file handles - */ -size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { - if (handle->ofset + bytes > handle->len) { - bytes = handle->len - handle->ofset; - } - memcpy(buf, handle->data + handle->ofset, bytes); - handle->ofset += bytes; - ((char*)buf)[bytes] = '\0'; - if (handle->ofset > handle->len) { - bytes = 0; - } - return bytes; -} \ No newline at end of file diff --git a/tests/tftp.hpp b/tests/tftp.hpp deleted file mode 100644 index 868b1da..0000000 --- a/tests/tftp.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -typedef struct tftp_custom_file_s { - const char* data; - size_t len; - char*name; - size_t ofset; -}tftp_custom_file_t; - -void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); -size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); \ No newline at end of file diff --git a/tests/tftp_server.h b/tests/tftp_server.h new file mode 100644 index 0000000..f5d45d9 --- /dev/null +++ b/tests/tftp_server.h @@ -0,0 +1,37 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +struct pbuf { + struct pbuf *next; + void *payload; + uint16_t tot_len; + uint16_t len; + uint8_t type_internal; + uint8_t flags; + //LWIP_PBUF_REF_T ref; + + uint8_t if_idx; +}; + +#define ERR_OK 0 + +struct tftp_context { + void* (*open)(const char* fname, const char* mode, uint8_t write); + void (*close)(void* handle); + int (*read)(void* handle, void* buf, int bytes); + int (*write)(void* handle, struct pbuf* p); +}; + +void tftp_cleanup(void); +uint32_t logger_get_timestamp(void); +int tftp_init(struct tftp_context* context); + +#ifdef __cplusplus +} +#endif \ No newline at end of file From d36ce9ae1e7b3457d7d8c6bdfc3302519d026e97 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Mon, 13 Nov 2023 13:26:08 +0100 Subject: [PATCH 23/46] TFTP Add virtText.txt --- project/Core/Src/tftp.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 454e7d9..6a4b937 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -13,7 +13,8 @@ extern struct llfs_data_file* llfs_root; static tftp_custom_file_t virt_file[] = { {.name = "index.txt",.data = NULL, .len = 0, .ofset = 0}, - {.name = "virtImage.raw",.data = NULL, .len = 0, .ofset = 0} + {.name = "virtImage.raw",.data = NULL, .len = 0, .ofset = 0}, + {.name = "virtText.txt",.data = NULL, .len = 0, .ofset = 0} }; int str_cat_str(char* dest, size_t dest_size, const char* src) { @@ -103,6 +104,8 @@ void* tftp_open(const char* fname, const char* mode, uint8_t write) { return &virt_file[0]; } else if (strcmp(fname, virt_file[1].name) == 0 && write != TFTP_READ) { return &virt_file[1]; + } else if (strcmp(fname, virt_file[2].name) == 0 && write != TFTP_READ) { + return &virt_file[2]; } // TODO: waiting on Lorentz to finish creating f* functions for LLFS @@ -120,7 +123,9 @@ void tftp_close(void* handle) { LOG_CRIT(TAG, "handle is null"); return; } - if (handle == &virt_file[0] || handle == &virt_file[1]) { + + if (handle == &virt_file[0] || handle == &virt_file[1] || handle == &virt_file[2]) { + ((tftp_custom_file_t*)handle)->ofset = 0; return; } @@ -153,6 +158,9 @@ int tftp_read(void* handle, void* buf, int bytes) { } else if ((tftp_custom_file_t*)file == &virt_file[1]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; + } else if ((tftp_custom_file_t*)file == &virt_file[2]) { + LOG_CRIT(TAG, "Exception: Trying to read a write only file"); + return -1; } // TODO: waiting on Lorentz to finish creating f* functions for LLFS ret = fread(buf, sizeof(uint8_t), bytes, file); @@ -206,6 +214,9 @@ void init_index(void) str_cat(virt_file[0].data, len, '\n'); root = root->next; } + + virt_file[2].data = malloc(100); + virt_file[2].len = 100; } struct tftp_context tftpContext_s = { @@ -239,4 +250,7 @@ void tftp_server_deinit(void) { virt_file[0].data = NULL; virt_file[0].len = 0; virt_file[0].ofset = 0; + free(virt_file[2].data); + virt_file[2].data = NULL; + virt_file[2].len = 0; } \ No newline at end of file From c568acc9e6b6463cf2f118ca2594440a3dcb810a Mon Sep 17 00:00:00 2001 From: Sani7 Date: Mon, 13 Nov 2023 13:28:57 +0100 Subject: [PATCH 24/46] TFTP Add tftp_custom_fwrite --- project/Core/Inc/tftp.h | 1 + project/Core/Src/tftp.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index e33e855..5a99147 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -30,6 +30,7 @@ void tftp_server_init(void); void tftp_server_deinit(void); void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); +size_t tftp_custom_fwrite(void* buf, size_t bytes, tftp_custom_file_t* handle); #ifdef __cplusplus } diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 6a4b937..1a46967 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -82,6 +82,27 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { return bytes; } +/** + * @brief tftp custom file functions to write the data + * auto rolling over the offset + * + * @param buf The buffer to write the data to + * @param bytes The number of bytes to write + * @param handle The handle to the file to write to + * @return The number of bytes written + */ +size_t tftp_custom_fwrite(void* buf, size_t bytes, tftp_custom_file_t* handle) { + if (handle->ofset + bytes > handle->len) { + bytes = handle->len - handle->ofset; + } + memcpy(handle->data + handle->ofset, buf, bytes); + handle->ofset += bytes; + if (handle->ofset > handle->len) { + bytes = 0; + } + return bytes; +} + /** * @brief tftp helper functions */ From c97d24e578611e1c6713598862702b408db56442 Mon Sep 17 00:00:00 2001 From: Sani7 Date: Mon, 13 Nov 2023 14:10:24 +0100 Subject: [PATCH 25/46] LLFS quick fix ls --- project/Core/Src/llfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/llfs.c b/project/Core/Src/llfs.c index 62588de..cd08cd3 100644 --- a/project/Core/Src/llfs.c +++ b/project/Core/Src/llfs.c @@ -57,7 +57,7 @@ size_t llfs_file_list(llfs_file_t* file_list, size_t max_files, char* filter) { } // Iterate over all files in the filesystem - while (file != NULL && file_count < max_files) { + while (file != NULL && count < max_files) { // Filter out files with a filename that does not match the filter if (filter != NULL) { if (!file_ext_cmp(file->name, filter)) { From 623cf3749cfaadd3cc6c4351563e072764253489 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Nov 2023 16:03:14 +0100 Subject: [PATCH 26/46] TFTP Add tftp_custom_write and test functions --- project/CMakeLists.txt | 2 +- project/Core/Inc/tftp.h | 5 ++++- project/Core/Src/tftp.c | 12 +++++++++++- tests/CMakeLists.txt | 5 +++++ tests/mocs.c | 4 ++++ tests/tfpt_tests.cpp | 23 +++++++++++++++++++++++ tests/tftp_server.h | 6 ++++++ 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/project/CMakeLists.txt b/project/CMakeLists.txt index 06d823d..430cf6d 100644 --- a/project/CMakeLists.txt +++ b/project/CMakeLists.txt @@ -53,7 +53,7 @@ include_directories(LWIP/App LWIP/Target Core/Inc Middlewares/Third_Party/LwIP/s add_definitions(-DDEBUG -DUSE_HAL_DRIVER -DSTM32F746xx) -#file(GLOB_RECURSE SOURCES "Core/*.*" "LWIP/*.*" "Middlewares/*.*" "Drivers/*.*") +#file(GLOB_RECURSE SOURCES "Core/*.*" "Drivers/*.*" "LWIP/*.*" "Middlewares/*.*") file(GLOB_RECURSE SOURCES "Core/Src/*.*" "Core/Startup/*.*" "LWIP/*.*" "Middlewares/*.*" "Drivers/*.*") diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 5a99147..66cdcce 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -7,6 +7,9 @@ #ifndef PROJECT_TFTP_H #define PROJECT_TFTP_H #include +#ifndef TESTING +#include "lcd_api.h" +#endif #ifdef __cplusplus extern "C" { #endif @@ -30,7 +33,7 @@ void tftp_server_init(void); void tftp_server_deinit(void); void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); -size_t tftp_custom_fwrite(void* buf, size_t bytes, tftp_custom_file_t* handle); +size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle); #ifdef __cplusplus } diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 1a46967..bbf63d6 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -91,7 +91,7 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { * @param handle The handle to the file to write to * @return The number of bytes written */ -size_t tftp_custom_fwrite(void* buf, size_t bytes, tftp_custom_file_t* handle) { +size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) { if (handle->ofset + bytes > handle->len) { bytes = handle->len - handle->ofset; } @@ -145,6 +145,11 @@ void tftp_close(void* handle) { return; } + if (handle == &virt_file[2]) { + // TODO: Clear display + lcd_display_text((uint8_t*)virt_file[2].data, 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + } + if (handle == &virt_file[0] || handle == &virt_file[1] || handle == &virt_file[2]) { ((tftp_custom_file_t*)handle)->ofset = 0; return; @@ -202,6 +207,11 @@ int tftp_read(void* handle, void* buf, int bytes) { int tftp_write(void* handle, struct pbuf* p) { LOG_INFO(TAG, "Writing file"); LOG_DEBUG(TAG, "Not implemented yet"); + tftp_custom_file_t *file = (tftp_custom_file_t*)handle; + if (file == &virt_file[1] || file == &virt_file[2]) { + return tftp_custom_fwrite(p->payload, p->len, file); + } + return -1; } /** diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1b931ee..ab2ac7f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,6 +9,11 @@ link_directories(${GTEST_LIB_DIR}) file(GLOB_RECURSE TEST_SOURCES "*.cpp" "*.c") add_executable(tests) +target_compile_definitions(tests + PRIVATE + "TESTING" +) + target_sources(tests PRIVATE ${TEST_SOURCES} diff --git a/tests/mocs.c b/tests/mocs.c index 26fff82..cb8dab5 100644 --- a/tests/mocs.c +++ b/tests/mocs.c @@ -17,3 +17,7 @@ uint32_t logger_get_timestamp(void) { int tftp_init(struct tftp_context* context) { return 0; } + +void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font) { + +} \ No newline at end of file diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp index 4fd19f2..3424a70 100644 --- a/tests/tfpt_tests.cpp +++ b/tests/tfpt_tests.cpp @@ -12,6 +12,12 @@ tftp_custom_file_t file = { .name = (char*)"test.txt", .ofset = 0 }; +tftp_custom_file_t write_file = { + .data = NULL, + .len = 0, + .name = (char*)"test.txt", + .ofset = 0 + }; TEST(TFTP, custom_fseek) { @@ -28,6 +34,7 @@ TEST(TFTP, custom_fread) size_t bytes = tftp_custom_fread(buf, 11, &file); EXPECT_EQ(bytes, 11); EXPECT_EQ(file.ofset, 11); + EXPECT_EQ(memcmp(buf, "1234567890", 10), 0); memset(buf, 0, 11); @@ -50,4 +57,20 @@ TEST(TFTP, custom_fread) EXPECT_EQ(memcmp(buf, "67890", 5), 0); } +TEST(TFTP, custom_fwrite) { + write_file.data = (char*)malloc(21 * sizeof(char)); + write_file.len = 21; + tftp_custom_fwrite("0987654321", 10, &write_file); + EXPECT_EQ(write_file.ofset, 10); + EXPECT_EQ(write_file.len, 21); + EXPECT_EQ(memcmp(write_file.data, "0987654321", 10), 0); + tftp_custom_fwrite("1234567890", 10, &write_file); + EXPECT_EQ(write_file.ofset, 20); + EXPECT_EQ(write_file.len, 21); + EXPECT_EQ(memcmp(write_file.data, "09876543211234567890", 20), 0); + + free(write_file.data); + write_file.data = NULL; + write_file.len = 0; +} \ No newline at end of file diff --git a/tests/tftp_server.h b/tests/tftp_server.h index f5d45d9..27fde1c 100644 --- a/tests/tftp_server.h +++ b/tests/tftp_server.h @@ -18,8 +18,13 @@ struct pbuf { uint8_t if_idx; }; +typedef void sFONT; #define ERR_OK 0 +#define LCD_COLOR_BLACK 0 +#define LCD_COLOR_WHITE 1 + +#define LCD_FONT16 0 struct tftp_context { void* (*open)(const char* fname, const char* mode, uint8_t write); @@ -31,6 +36,7 @@ struct tftp_context { void tftp_cleanup(void); uint32_t logger_get_timestamp(void); int tftp_init(struct tftp_context* context); +void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font); #ifdef __cplusplus } From eb7dcce09ed4281dd3704fc84f2dcd7e2eebdda4 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Nov 2023 17:44:23 +0100 Subject: [PATCH 27/46] TFTP Add display text for virtImage and clear display for virtText --- project/CMakeLists.txt | 2 +- project/Core/Src/tftp.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/project/CMakeLists.txt b/project/CMakeLists.txt index 430cf6d..06d823d 100644 --- a/project/CMakeLists.txt +++ b/project/CMakeLists.txt @@ -53,7 +53,7 @@ include_directories(LWIP/App LWIP/Target Core/Inc Middlewares/Third_Party/LwIP/s add_definitions(-DDEBUG -DUSE_HAL_DRIVER -DSTM32F746xx) -#file(GLOB_RECURSE SOURCES "Core/*.*" "Drivers/*.*" "LWIP/*.*" "Middlewares/*.*") +#file(GLOB_RECURSE SOURCES "Core/*.*" "LWIP/*.*" "Middlewares/*.*" "Drivers/*.*") file(GLOB_RECURSE SOURCES "Core/Src/*.*" "Core/Startup/*.*" "LWIP/*.*" "Middlewares/*.*" "Drivers/*.*") diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index bbf63d6..45f2c01 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -145,8 +145,14 @@ void tftp_close(void* handle) { return; } + if (handle == &virt_file[1]) { + lcd_clear(LCD_COLOR_BLACK); + lcd_display_text("show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + } + if (handle == &virt_file[2]) { // TODO: Clear display + lcd_clear(LCD_COLOR_BLACK); lcd_display_text((uint8_t*)virt_file[2].data, 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); } From 8efac8948a71c25a8b57cca4f8743065ea0aaddb Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Nov 2023 18:13:09 +0100 Subject: [PATCH 28/46] TFTP format + remove todo --- project/Core/Src/tftp.c | 56 ++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 45f2c01..b27bb0a 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -10,12 +10,9 @@ static const char* TAG = "tftp_server"; extern struct llfs_data_file* llfs_root; -static tftp_custom_file_t virt_file[] = - { - {.name = "index.txt",.data = NULL, .len = 0, .ofset = 0}, - {.name = "virtImage.raw",.data = NULL, .len = 0, .ofset = 0}, - {.name = "virtText.txt",.data = NULL, .len = 0, .ofset = 0} - }; +static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .len = 0, .ofset = 0}, + {.name = "virtImage.raw", .data = NULL, .len = 0, .ofset = 0}, + {.name = "virtText.txt", .data = NULL, .len = 0, .ofset = 0}}; int str_cat_str(char* dest, size_t dest_size, const char* src) { size_t dest_len = strlen(dest); @@ -27,8 +24,7 @@ int str_cat_str(char* dest, size_t dest_size, const char* src) { return 0; } -int str_cat(char* dest, size_t dest_size, char c) -{ +int str_cat(char* dest, size_t dest_size, char c) { size_t dest_len = strlen(dest); if (dest_len + 1 > dest_size) { return -1; @@ -111,11 +107,11 @@ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* han * @brief This function is called when a file is opened * It should return a handle to the file or NULL if the file does not exist * The handle contains a ptr to or the actual file data or a virtual file - * + * * @param fname The name of the file to open * @param mode Mode string from TFTP RFC * @param write Flag indicating read (0) or write (!= 0) access - * @return void* File handle supplied to other functions + * @return void* File handle supplied to other functions */ void* tftp_open(const char* fname, const char* mode, uint8_t write) { LOG_INFO(TAG, "Opening %s", fname); @@ -135,7 +131,7 @@ void* tftp_open(const char* fname, const char* mode, uint8_t write) { /** * @brief This function is called when a file is closed - * + * * @param handle The handle to the file to close */ void tftp_close(void* handle) { @@ -146,12 +142,12 @@ void tftp_close(void* handle) { } if (handle == &virt_file[1]) { + // TODO: waiting on pr of tim to merge so we can use the bmp lcd function lcd_clear(LCD_COLOR_BLACK); lcd_display_text("show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); } if (handle == &virt_file[2]) { - // TODO: Clear display lcd_clear(LCD_COLOR_BLACK); lcd_display_text((uint8_t*)virt_file[2].data, 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); } @@ -169,11 +165,11 @@ void tftp_close(void* handle) { * @brief This function is called when a file is read * The virtual files are filtered out first * then the file is trying to get read from the llfs - * + * * @param handle File handle returned by open() - * @param buf Target buffer to copy read data to - * @param bytes Number of bytes to copy to buf - * @return int >= 0: Success; < 0: Error + * @param buf Target buffer to copy read data to + * @param bytes Number of bytes to copy to buf + * @return int >= 0: Success; < 0: Error */ int tftp_read(void* handle, void* buf, int bytes) { int ret = 0; @@ -204,16 +200,16 @@ int tftp_read(void* handle, void* buf, int bytes) { /** * @brief This function is called when a file is written - * + * * @param handle File handle returned by open() * @param p PBUF adjusted such that payload pointer points to the beginning of write data. - * In other words, TFTP headers are stripped off. - * @return int >= 0: Success; < 0: Error + * In other words, TFTP headers are stripped off. + * @return int >= 0: Success; < 0: Error */ int tftp_write(void* handle, struct pbuf* p) { LOG_INFO(TAG, "Writing file"); LOG_DEBUG(TAG, "Not implemented yet"); - tftp_custom_file_t *file = (tftp_custom_file_t*)handle; + tftp_custom_file_t* file = (tftp_custom_file_t*)handle; if (file == &virt_file[1] || file == &virt_file[2]) { return tftp_custom_fwrite(p->payload, p->len, file); } @@ -223,16 +219,14 @@ int tftp_write(void* handle, struct pbuf* p) { /** * @brief This function creates the file list for index.txt */ -void init_index(void) -{ +void init_index(void) { size_t len = 0; // Add len of the virt files to the size - for (int i = 0; i < 2; i++) - { + for (int i = 0; i < 2; i++) { len += strlen(virt_file[i].name) + 1; } const struct llfs_data_file* root = llfs_root; - while(root != NULL) { + while (root != NULL) { len += strlen(root->name) + 1; root = root->next; } @@ -240,13 +234,12 @@ void init_index(void) virt_file[0].data = malloc(len); virt_file[0].len = len; - for (int i = 0; i < 2; i++) - { + for (int i = 0; i < 2; i++) { str_cat_str(virt_file[0].data, len, virt_file[i].name); str_cat(virt_file[0].data, len, '\n'); } root = llfs_root; - while(root != NULL) { + while (root != NULL) { str_cat_str(virt_file[0].data, len, root->name); str_cat(virt_file[0].data, len, '\n'); root = root->next; @@ -256,12 +249,7 @@ void init_index(void) virt_file[2].len = 100; } -struct tftp_context tftpContext_s = { - .open = tftp_open, - .close = tftp_close, - .read = tftp_read, - .write = tftp_write - }; +struct tftp_context tftpContext_s = {.open = tftp_open, .close = tftp_close, .read = tftp_read, .write = tftp_write}; /** * @brief Initialize tftp server From 40bc58bdd9a7ce84984935839c1a589194018d35 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Nov 2023 18:21:14 +0100 Subject: [PATCH 29/46] TFTP Update mocs, header and fix an error --- project/Core/Src/tftp.c | 2 +- tests/mocs.c | 4 ++++ tests/tftp_server.h | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index b27bb0a..765b597 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -144,7 +144,7 @@ void tftp_close(void* handle) { if (handle == &virt_file[1]) { // TODO: waiting on pr of tim to merge so we can use the bmp lcd function lcd_clear(LCD_COLOR_BLACK); - lcd_display_text("show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + lcd_display_text((uint8_t*)"show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); } if (handle == &virt_file[2]) { diff --git a/tests/mocs.c b/tests/mocs.c index cb8dab5..da69c34 100644 --- a/tests/mocs.c +++ b/tests/mocs.c @@ -20,4 +20,8 @@ int tftp_init(struct tftp_context* context) { void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font) { +} + +void lcd_clear(uint32_t color) { + } \ No newline at end of file diff --git a/tests/tftp_server.h b/tests/tftp_server.h index 27fde1c..45a1d8c 100644 --- a/tests/tftp_server.h +++ b/tests/tftp_server.h @@ -37,6 +37,7 @@ void tftp_cleanup(void); uint32_t logger_get_timestamp(void); int tftp_init(struct tftp_context* context); void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font); +void lcd_clear(uint32_t color); #ifdef __cplusplus } From c90a827dcab5b4144948f5b156b0d7882ba6a5d7 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Nov 2023 18:45:45 +0100 Subject: [PATCH 30/46] TFTP move init of virtImage to init instead of init index --- project/Core/Src/tftp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 765b597..81e5859 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -244,9 +244,6 @@ void init_index(void) { str_cat(virt_file[0].data, len, '\n'); root = root->next; } - - virt_file[2].data = malloc(100); - virt_file[2].len = 100; } struct tftp_context tftpContext_s = {.open = tftp_open, .close = tftp_close, .read = tftp_read, .write = tftp_write}; @@ -258,6 +255,9 @@ void tftp_server_init(void) { LOG_INFO(TAG, "Initializing tftp server"); // init the index.txt virt_file init_index(); + // init the virtImage.raw virt_file + virt_file[2].data = malloc(81920 * sizeof(char)); + virt_file[2].len = 81920; // Init the tftp server if (tftp_init(&tftpContext_s) != ERR_OK) { From e5b2bb0737a726ef78e9632cfe6771b6d43ffdad Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Tue, 14 Nov 2023 14:52:18 +0100 Subject: [PATCH 31/46] README Link to docs/tftp --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d8ba21..50b4c82 100644 --- a/README.md +++ b/README.md @@ -60,4 +60,5 @@ This folder contains the following documents: - [llfs.md](docs/llfs.md): Linked List File System - [logger.md](docs/logger.md): Logging and Debugging Messages - [mkllfs.md](docs/mkllfs.md): Make Linked List File System -- [style_guide.md](docs/style_guide.md): Style Guide \ No newline at end of file +- [style_guide.md](docs/style_guide.md): Style Guide +- [tftp.md](docs/tftp.md): Trivial File Transfer Protocol \ No newline at end of file From 520cfa6954ca38ed7554effcf738020a5a313c97 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Tue, 14 Nov 2023 14:52:57 +0100 Subject: [PATCH 32/46] TFTP Add include and init in main --- project/Core/Src/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index a5e14bf..136545c 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 "tftp.h" /* USER CODE END Includes */ @@ -126,6 +127,8 @@ int main(void) /* Initialize the filesystem */ llfs_init(); + /* Initialize the tftp server */ + tftp_server_init(); /* USER CODE END 2 */ /* Infinite loop */ From 48ad17e0dc6eceae5b91fba8327893ee075fb843 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Tue, 14 Nov 2023 15:02:51 +0100 Subject: [PATCH 33/46] TFTP Add commented lcd_draw_bmp_img --- project/Core/Src/tftp.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 81e5859..2899362 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -145,6 +145,7 @@ void tftp_close(void* handle) { // TODO: waiting on pr of tim to merge so we can use the bmp lcd function lcd_clear(LCD_COLOR_BLACK); lcd_display_text((uint8_t*)"show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + //lcd_draw_bmp_img((uint8_t*)virt_file[1].data, 0, 0); } if (handle == &virt_file[2]) { From c9eedde905b865f8da76729fa6c9383a2310107f Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Tue, 14 Nov 2023 17:30:08 +0100 Subject: [PATCH 34/46] TFTP - Change to lcd_draw_bmp_img - Add moc for this function --- project/Core/Src/tftp.c | 4 ++-- tests/mocs.c | 4 ++++ tests/tftp_server.h | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 2899362..8328eea 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -144,8 +144,8 @@ void tftp_close(void* handle) { if (handle == &virt_file[1]) { // TODO: waiting on pr of tim to merge so we can use the bmp lcd function lcd_clear(LCD_COLOR_BLACK); - lcd_display_text((uint8_t*)"show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); - //lcd_draw_bmp_img((uint8_t*)virt_file[1].data, 0, 0); + //lcd_display_text((uint8_t*)"show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + lcd_draw_bmp_img((uint8_t*)virt_file[1].data, 0, 0); } if (handle == &virt_file[2]) { diff --git a/tests/mocs.c b/tests/mocs.c index da69c34..05e5989 100644 --- a/tests/mocs.c +++ b/tests/mocs.c @@ -24,4 +24,8 @@ void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t co void lcd_clear(uint32_t color) { +} + +void lcd_draw_bmp_img(uint8_t* bmp_buff, uint32_t x_pos, uint32_t y_pos) { + } \ No newline at end of file diff --git a/tests/tftp_server.h b/tests/tftp_server.h index 45a1d8c..00677be 100644 --- a/tests/tftp_server.h +++ b/tests/tftp_server.h @@ -37,6 +37,7 @@ void tftp_cleanup(void); uint32_t logger_get_timestamp(void); int tftp_init(struct tftp_context* context); void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font); +void lcd_draw_bmp_img(uint8_t* bmp_buff, uint32_t x_pos, uint32_t y_pos); void lcd_clear(uint32_t color); #ifdef __cplusplus From 0da6b4a5ad609dfaa8e82c6903151af5eda44efd Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 16 Nov 2023 14:48:28 +0100 Subject: [PATCH 35/46] TFTP Change comment of virtImage init --- project/Core/Src/tftp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 8328eea..4909456 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -256,7 +256,7 @@ void tftp_server_init(void) { LOG_INFO(TAG, "Initializing tftp server"); // init the index.txt virt_file init_index(); - // init the virtImage.raw virt_file + // init the virtImage.raw virt_file with 80kb of ram virt_file[2].data = malloc(81920 * sizeof(char)); virt_file[2].len = 81920; From f82634daa0f43cb617def0713f6df19c263354bd Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 17 Nov 2023 16:27:26 +0100 Subject: [PATCH 36/46] TFTP Fix all warnings --- project/Core/Inc/tftp.h | 2 ++ project/Core/Src/tftp.c | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 66cdcce..4b28c33 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -22,6 +22,8 @@ extern "C" { #define TFTP_READ 0 +#define UNUSED(x) (void)(x) + typedef struct tftp_custom_file_s { char* data; size_t len; diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 4909456..716096b 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -116,6 +116,8 @@ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* han void* tftp_open(const char* fname, const char* mode, uint8_t write) { LOG_INFO(TAG, "Opening %s", fname); + UNUSED(mode); + if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { tftp_custom_fseek(&virt_file[0], 0, SEEK_SET); return &virt_file[0]; @@ -182,7 +184,7 @@ int tftp_read(void* handle, void* buf, int bytes) { FILE* file = (FILE*)handle; if ((tftp_custom_file_t*)file == &virt_file[0]) { - ret = tftp_custom_fread(buf, bytes, (tftp_custom_file_t*)file); + ret = (int)tftp_custom_fread(buf, (size_t)bytes, (tftp_custom_file_t*)file); return ret; } else if ((tftp_custom_file_t*)file == &virt_file[1]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); @@ -191,8 +193,8 @@ int tftp_read(void* handle, void* buf, int bytes) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; } - // TODO: waiting on Lorentz to finish creating f* functions for LLFS - ret = fread(buf, sizeof(uint8_t), bytes, file); + + ret = (int)fread(buf, sizeof(uint8_t), (size_t)bytes, file); if (ret <= 0) { return -1; } @@ -212,7 +214,7 @@ int tftp_write(void* handle, struct pbuf* p) { LOG_DEBUG(TAG, "Not implemented yet"); tftp_custom_file_t* file = (tftp_custom_file_t*)handle; if (file == &virt_file[1] || file == &virt_file[2]) { - return tftp_custom_fwrite(p->payload, p->len, file); + return (int)tftp_custom_fwrite(p->payload, (size_t)(p->len), file); } return -1; } From 0a9082678d3cac4658b171ca329e3a9aff1ce02f Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 17 Nov 2023 16:29:40 +0100 Subject: [PATCH 37/46] TFTP Fix unused if it is already defined --- project/Core/Inc/tftp.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index 4b28c33..de01a2a 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -22,7 +22,9 @@ extern "C" { #define TFTP_READ 0 +#ifndef UNUSED #define UNUSED(x) (void)(x) +#endif typedef struct tftp_custom_file_s { char* data; From 1850efde2a474f92c803a5aeeb59e217dd7e0e15 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 12:45:28 +0100 Subject: [PATCH 38/46] TFTP Change from raw to bmp --- project/Core/Src/tftp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 716096b..0eb2689 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -11,7 +11,7 @@ static const char* TAG = "tftp_server"; extern struct llfs_data_file* llfs_root; static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .len = 0, .ofset = 0}, - {.name = "virtImage.raw", .data = NULL, .len = 0, .ofset = 0}, + {.name = "virtImage.bmp", .data = NULL, .len = 0, .ofset = 0}, {.name = "virtText.txt", .data = NULL, .len = 0, .ofset = 0}}; int str_cat_str(char* dest, size_t dest_size, const char* src) { From 2aad741b564454409240912b15e04c730f5b96fb Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 14:46:26 +0100 Subject: [PATCH 39/46] TFTP * Change form indexes to defines * add gif support --- project/Core/Src/tftp.c | 93 +++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 0eb2689..fdff8ea 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -6,12 +6,20 @@ #include "tftp.h" +#define VIRT_INDEX_TXT 0 +#define VIRT_IMAGE_BMP 1 +#define VIRT_IMAGE_GIF 2 +#define VIRT_TEXT_TXT 3 + +#define IMAGE_BUFFER_SIZE 81920 + static const char* TAG = "tftp_server"; extern struct llfs_data_file* llfs_root; static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .len = 0, .ofset = 0}, {.name = "virtImage.bmp", .data = NULL, .len = 0, .ofset = 0}, + {.name = "virtImage.gif", .data = NULL, .len = 0, .ofset = 0}, {.name = "virtText.txt", .data = NULL, .len = 0, .ofset = 0}}; int str_cat_str(char* dest, size_t dest_size, const char* src) { @@ -24,6 +32,7 @@ int str_cat_str(char* dest, size_t dest_size, const char* src) { return 0; } + int str_cat(char* dest, size_t dest_size, char c) { size_t dest_len = strlen(dest); if (dest_len + 1 > dest_size) { @@ -118,16 +127,17 @@ void* tftp_open(const char* fname, const char* mode, uint8_t write) { UNUSED(mode); - if (strcmp(fname, virt_file[0].name) == 0 && write == TFTP_READ) { - tftp_custom_fseek(&virt_file[0], 0, SEEK_SET); + if (strcmp(fname, virt_file[VIRT_INDEX_TXT].name) == 0 && write == TFTP_READ) { + tftp_custom_fseek(&virt_file[VIRT_INDEX_TXT], 0, SEEK_SET); return &virt_file[0]; - } else if (strcmp(fname, virt_file[1].name) == 0 && write != TFTP_READ) { - return &virt_file[1]; - } else if (strcmp(fname, virt_file[2].name) == 0 && write != TFTP_READ) { - return &virt_file[2]; + } else if (strcmp(fname, virt_file[VIRT_IMAGE_BMP].name) == 0 && write != TFTP_READ) { + return &virt_file[VIRT_IMAGE_BMP]; + } else if (strcmp(fname, virt_file[VIRT_IMAGE_GIF].name) == 0 && write != TFTP_READ) { + return &virt_file[VIRT_IMAGE_GIF]; + } else if (strcmp(fname, virt_file[VIRT_TEXT_TXT].name) == 0 && write != TFTP_READ) { + return &virt_file[VIRT_TEXT_TXT]; } - // TODO: waiting on Lorentz to finish creating f* functions for LLFS return fopen(fname, write ? "wb" : "rb"); } @@ -143,24 +153,26 @@ void tftp_close(void* handle) { return; } - if (handle == &virt_file[1]) { - // TODO: waiting on pr of tim to merge so we can use the bmp lcd function + if (handle == &virt_file[VIRT_IMAGE_BMP]) { lcd_clear(LCD_COLOR_BLACK); - //lcd_display_text((uint8_t*)"show me what you got virtImage.raw", 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); - lcd_draw_bmp_img((uint8_t*)virt_file[1].data, 0, 0); + lcd_draw_bmp_img((uint8_t*)virt_file[VIRT_IMAGE_BMP].data, 0, 0); } - if (handle == &virt_file[2]) { + if (handle == &virt_file[VIRT_IMAGE_GIF]) { lcd_clear(LCD_COLOR_BLACK); - lcd_display_text((uint8_t*)virt_file[2].data, 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + lcd_draw_gif((uint8_t*)virt_file[VIRT_IMAGE_GIF].data, virt_file[VIRT_IMAGE_GIF].ofset,0, 0); } - if (handle == &virt_file[0] || handle == &virt_file[1] || handle == &virt_file[2]) { + if (handle == &virt_file[VIRT_TEXT_TXT]) { + lcd_clear(LCD_COLOR_BLACK); + lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + } + + if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP] || handle == &virt_file[VIRT_IMAGE_GIF] || handle == &virt_file[VIRT_TEXT_TXT]) { ((tftp_custom_file_t*)handle)->ofset = 0; return; } - // TODO: waiting on Lorentz to finish creating f* functions for LLFS fclose((FILE*)handle); } @@ -183,13 +195,16 @@ int tftp_read(void* handle, void* buf, int bytes) { } FILE* file = (FILE*)handle; - if ((tftp_custom_file_t*)file == &virt_file[0]) { + if ((tftp_custom_file_t*)file == &virt_file[VIRT_INDEX_TXT]) { ret = (int)tftp_custom_fread(buf, (size_t)bytes, (tftp_custom_file_t*)file); return ret; - } else if ((tftp_custom_file_t*)file == &virt_file[1]) { + } else if ((tftp_custom_file_t*)file == &virt_file[VIRT_IMAGE_BMP]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; - } else if ((tftp_custom_file_t*)file == &virt_file[2]) { + } else if ((tftp_custom_file_t*)file == &virt_file[VIRT_IMAGE_GIF]) { + LOG_CRIT(TAG, "Exception: Trying to read a write only file"); + return -1; + } else if ((tftp_custom_file_t*)file == &virt_file[VIRT_TEXT_TXT]) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; } @@ -211,9 +226,8 @@ int tftp_read(void* handle, void* buf, int bytes) { */ int tftp_write(void* handle, struct pbuf* p) { LOG_INFO(TAG, "Writing file"); - LOG_DEBUG(TAG, "Not implemented yet"); tftp_custom_file_t* file = (tftp_custom_file_t*)handle; - if (file == &virt_file[1] || file == &virt_file[2]) { + if (file == &virt_file[VIRT_IMAGE_BMP] || file == &virt_file[VIRT_IMAGE_GIF] || file == &virt_file[VIRT_TEXT_TXT]) { return (int)tftp_custom_fwrite(p->payload, (size_t)(p->len), file); } return -1; @@ -234,17 +248,17 @@ void init_index(void) { root = root->next; } len++; // +1 for the \0 - virt_file[0].data = malloc(len); - virt_file[0].len = len; + virt_file[VIRT_INDEX_TXT].data = malloc(len); + virt_file[VIRT_INDEX_TXT].len = len; for (int i = 0; i < 2; i++) { - str_cat_str(virt_file[0].data, len, virt_file[i].name); - str_cat(virt_file[0].data, len, '\n'); + str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, virt_file[i].name); + str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n'); } root = llfs_root; while (root != NULL) { - str_cat_str(virt_file[0].data, len, root->name); - str_cat(virt_file[0].data, len, '\n'); + str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, root->name); + str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n'); root = root->next; } } @@ -259,8 +273,15 @@ void tftp_server_init(void) { // init the index.txt virt_file init_index(); // init the virtImage.raw virt_file with 80kb of ram - virt_file[2].data = malloc(81920 * sizeof(char)); - virt_file[2].len = 81920; + virt_file[VIRT_IMAGE_BMP].data = calloc(IMAGE_BUFFER_SIZE, sizeof(char)); + if (virt_file[VIRT_IMAGE_BMP].data == NULL) { + LOG_FATAL(TAG, "Could not allocate memory for virtImage.raw"); + return; + } + virt_file[VIRT_IMAGE_BMP].len = IMAGE_BUFFER_SIZE; + + virt_file[VIRT_IMAGE_GIF].data = virt_file[VIRT_IMAGE_BMP].data; + virt_file[VIRT_IMAGE_GIF].len = virt_file[VIRT_IMAGE_BMP].len; // Init the tftp server if (tftp_init(&tftpContext_s) != ERR_OK) { @@ -274,11 +295,13 @@ void tftp_server_deinit(void) { LOG_INFO(TAG, "Deinitializing tftp server"); tftp_cleanup(); LOG_INFO(TAG, "tftp server deinitialized successfully"); - free(virt_file[0].data); - virt_file[0].data = NULL; - virt_file[0].len = 0; - virt_file[0].ofset = 0; - free(virt_file[2].data); - virt_file[2].data = NULL; - virt_file[2].len = 0; + free(virt_file[VIRT_INDEX_TXT].data); + virt_file[VIRT_INDEX_TXT].data = NULL; + virt_file[VIRT_INDEX_TXT].len = 0; + virt_file[VIRT_INDEX_TXT].ofset = 0; + free(virt_file[VIRT_IMAGE_BMP].data); + virt_file[VIRT_IMAGE_BMP].data = NULL; + virt_file[VIRT_IMAGE_BMP].len = 0; + virt_file[VIRT_IMAGE_GIF].data = NULL; + virt_file[VIRT_IMAGE_GIF].len = 0; } \ No newline at end of file From fbeec7bfbc3f279b637277c213ff3a85dabd9704 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 15:15:31 +0100 Subject: [PATCH 40/46] TFTP * Add text support * Change from Black text on White background to the reverse --- project/Core/Src/tftp.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index fdff8ea..d56b107 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -165,7 +165,7 @@ void tftp_close(void* handle) { if (handle == &virt_file[VIRT_TEXT_TXT]) { lcd_clear(LCD_COLOR_BLACK); - lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_BLACK, LCD_COLOR_WHITE, LCD_FONT16); + lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0,LCD_COLOR_WHITE, LCD_COLOR_BLACK, LCD_FONT16); } if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP] || handle == &virt_file[VIRT_IMAGE_GIF] || handle == &virt_file[VIRT_TEXT_TXT]) { @@ -275,7 +275,7 @@ void tftp_server_init(void) { // init the virtImage.raw virt_file with 80kb of ram virt_file[VIRT_IMAGE_BMP].data = calloc(IMAGE_BUFFER_SIZE, sizeof(char)); if (virt_file[VIRT_IMAGE_BMP].data == NULL) { - LOG_FATAL(TAG, "Could not allocate memory for virtImage.raw"); + LOG_FATAL(TAG, "Could not allocate memory for virtImage.bmp/virtImage.gif"); return; } virt_file[VIRT_IMAGE_BMP].len = IMAGE_BUFFER_SIZE; @@ -283,6 +283,9 @@ void tftp_server_init(void) { virt_file[VIRT_IMAGE_GIF].data = virt_file[VIRT_IMAGE_BMP].data; virt_file[VIRT_IMAGE_GIF].len = virt_file[VIRT_IMAGE_BMP].len; + virt_file[VIRT_TEXT_TXT].data = virt_file[VIRT_IMAGE_BMP].data; + virt_file[VIRT_TEXT_TXT].len = virt_file[VIRT_IMAGE_BMP].len; + // Init the tftp server if (tftp_init(&tftpContext_s) != ERR_OK) { LOG_FATAL(TAG, "Could not initialize tftp server"); @@ -302,6 +305,10 @@ void tftp_server_deinit(void) { free(virt_file[VIRT_IMAGE_BMP].data); virt_file[VIRT_IMAGE_BMP].data = NULL; virt_file[VIRT_IMAGE_BMP].len = 0; + virt_file[VIRT_IMAGE_GIF].data = NULL; virt_file[VIRT_IMAGE_GIF].len = 0; + + virt_file[VIRT_TEXT_TXT].data = NULL; + virt_file[VIRT_TEXT_TXT].len = 0; } \ No newline at end of file From ff72c9a5ebd450072ad58210341196916022204c Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 15:43:23 +0100 Subject: [PATCH 41/46] TFTP Update docs --- docs/tftp.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/tftp.md b/docs/tftp.md index a9cc1c2..db3a6c4 100644 --- a/docs/tftp.md +++ b/docs/tftp.md @@ -18,11 +18,18 @@ The TFTP task is used to receive and send files via TFTP. ### Receive a file index.txt contains a list of files on the file system. ```bash -tftp -g -r +bash $ tftp +tftp $ get index.txt ``` ### Send a file You can only write to the following files: -- virtImage.raw +- virtImage.bmp ```bash -tftp -p -l +bash $ tftp +tftp $ put virtImage.bmp +``` +- virtImage.gif +```bash +bash $ tftp +tftp $ put virtImage.gif ``` From 46f2fcacd25b4e47b63937f42dde37c82a1107a3 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 15:44:38 +0100 Subject: [PATCH 42/46] TFTP * Change typo * Format * Add doxygen --- project/Core/Inc/tftp.h | 45 ++++++++++++++++++++++++++++++----- project/Core/Src/tftp.c | 52 ++++++++++++++++++++--------------------- 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index de01a2a..48afcfa 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -14,11 +14,11 @@ extern "C" { #endif #define LOGGER_LEVEL_ALL -#include "log.h" -#include "llfs.h" -#include #include +#include #include +#include "llfs.h" +#include "log.h" #define TFTP_READ 0 @@ -29,14 +29,47 @@ extern "C" { typedef struct tftp_custom_file_s { char* data; size_t len; - char*name; - size_t ofset; -}tftp_custom_file_t; + char* name; + size_t offset; +} tftp_custom_file_t; +/** + * @brief Initialize the TFTP server + */ void tftp_server_init(void); + +/** + * @brief Uninitialize the TFTP server + */ void tftp_server_deinit(void); + +/** + * @brief Custom fseek function + * + * @param handle The custom file handle + * @param offset The offset + * @param whence The whence + */ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); + +/** + * @brief Custom fread function + * + * @param buf The buffer to read from + * @param bytes The amount of bytes to read + * @param handle The custom file handle + * @return The amount of bytes read + */ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); + +/** + * @brief Custom fwrite function + * + * @param buf The buffer to write to + * @param bytes The amount of bytes to write + * @param handle The custom file handle + * @return The amount of bytes written + */ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle); #ifdef __cplusplus diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index d56b107..897e990 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -9,7 +9,7 @@ #define VIRT_INDEX_TXT 0 #define VIRT_IMAGE_BMP 1 #define VIRT_IMAGE_GIF 2 -#define VIRT_TEXT_TXT 3 +#define VIRT_TEXT_TXT 3 #define IMAGE_BUFFER_SIZE 81920 @@ -17,10 +17,10 @@ static const char* TAG = "tftp_server"; extern struct llfs_data_file* llfs_root; -static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .len = 0, .ofset = 0}, - {.name = "virtImage.bmp", .data = NULL, .len = 0, .ofset = 0}, - {.name = "virtImage.gif", .data = NULL, .len = 0, .ofset = 0}, - {.name = "virtText.txt", .data = NULL, .len = 0, .ofset = 0}}; +static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .len = 0, .offset = 0}, + {.name = "virtImage.bmp", .data = NULL, .len = 0, .offset = 0}, + {.name = "virtImage.gif", .data = NULL, .len = 0, .offset = 0}, + {.name = "virtText.txt", .data = NULL, .len = 0, .offset = 0}}; int str_cat_str(char* dest, size_t dest_size, const char* src) { size_t dest_len = strlen(dest); @@ -32,7 +32,6 @@ int str_cat_str(char* dest, size_t dest_size, const char* src) { return 0; } - int str_cat(char* dest, size_t dest_size, char c) { size_t dest_len = strlen(dest); if (dest_len + 1 > dest_size) { @@ -52,16 +51,16 @@ int str_cat(char* dest, size_t dest_size, char c) { void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { switch (whence) { case SEEK_SET: - handle->ofset = offset; + handle->offset = offset; break; case SEEK_CUR: - handle->ofset += offset; + handle->offset += offset; break; case SEEK_END: break; } - if (handle->ofset > handle->len) { - handle->ofset = handle->len; + if (handle->offset > handle->len) { + handle->offset = handle->len; } } @@ -75,13 +74,13 @@ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { * @param[in,out] handle Custom file handles */ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { - if (handle->ofset + bytes > handle->len) { - bytes = handle->len - handle->ofset; + if (handle->offset + bytes > handle->len) { + bytes = handle->len - handle->offset; } - memcpy(buf, handle->data + handle->ofset, bytes); - handle->ofset += bytes; + memcpy(buf, handle->data + handle->offset, bytes); + handle->offset += bytes; ((char*)buf)[bytes] = '\0'; - if (handle->ofset > handle->len) { + if (handle->offset > handle->len) { bytes = 0; } return bytes; @@ -97,12 +96,12 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { * @return The number of bytes written */ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) { - if (handle->ofset + bytes > handle->len) { - bytes = handle->len - handle->ofset; + if (handle->offset + bytes > handle->len) { + bytes = handle->len - handle->offset; } - memcpy(handle->data + handle->ofset, buf, bytes); - handle->ofset += bytes; - if (handle->ofset > handle->len) { + memcpy(handle->data + handle->offset, buf, bytes); + handle->offset += bytes; + if (handle->offset > handle->len) { bytes = 0; } return bytes; @@ -160,16 +159,17 @@ void tftp_close(void* handle) { if (handle == &virt_file[VIRT_IMAGE_GIF]) { lcd_clear(LCD_COLOR_BLACK); - lcd_draw_gif((uint8_t*)virt_file[VIRT_IMAGE_GIF].data, virt_file[VIRT_IMAGE_GIF].ofset,0, 0); + lcd_draw_gif((uint8_t*)virt_file[VIRT_IMAGE_GIF].data, virt_file[VIRT_IMAGE_GIF].offset, 0, 0); } if (handle == &virt_file[VIRT_TEXT_TXT]) { lcd_clear(LCD_COLOR_BLACK); - lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0,LCD_COLOR_WHITE, LCD_COLOR_BLACK, LCD_FONT16); + lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_COLOR_BLACK, LCD_FONT16); } - if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP] || handle == &virt_file[VIRT_IMAGE_GIF] || handle == &virt_file[VIRT_TEXT_TXT]) { - ((tftp_custom_file_t*)handle)->ofset = 0; + if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP] + || handle == &virt_file[VIRT_IMAGE_GIF] || handle == &virt_file[VIRT_TEXT_TXT]) { + ((tftp_custom_file_t*)handle)->offset = 0; return; } @@ -208,7 +208,7 @@ int tftp_read(void* handle, void* buf, int bytes) { LOG_CRIT(TAG, "Exception: Trying to read a write only file"); return -1; } - + ret = (int)fread(buf, sizeof(uint8_t), (size_t)bytes, file); if (ret <= 0) { return -1; @@ -301,7 +301,7 @@ void tftp_server_deinit(void) { free(virt_file[VIRT_INDEX_TXT].data); virt_file[VIRT_INDEX_TXT].data = NULL; virt_file[VIRT_INDEX_TXT].len = 0; - virt_file[VIRT_INDEX_TXT].ofset = 0; + virt_file[VIRT_INDEX_TXT].offset = 0; free(virt_file[VIRT_IMAGE_BMP].data); virt_file[VIRT_IMAGE_BMP].data = NULL; virt_file[VIRT_IMAGE_BMP].len = 0; From 18f20e045cf1d237f51fa1c24530805519cf4010 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 16:04:53 +0100 Subject: [PATCH 43/46] TFTP Fix broken lcd_api functions --- project/Core/Src/tftp.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 897e990..d4fa7b3 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -153,18 +153,21 @@ void tftp_close(void* handle) { } if (handle == &virt_file[VIRT_IMAGE_BMP]) { - lcd_clear(LCD_COLOR_BLACK); + lcd_clear_images(); + lcd_clear_text(); lcd_draw_bmp_img((uint8_t*)virt_file[VIRT_IMAGE_BMP].data, 0, 0); } if (handle == &virt_file[VIRT_IMAGE_GIF]) { - lcd_clear(LCD_COLOR_BLACK); + lcd_clear_images(); + lcd_clear_text(); lcd_draw_gif((uint8_t*)virt_file[VIRT_IMAGE_GIF].data, virt_file[VIRT_IMAGE_GIF].offset, 0, 0); } if (handle == &virt_file[VIRT_TEXT_TXT]) { - lcd_clear(LCD_COLOR_BLACK); - lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_COLOR_BLACK, LCD_FONT16); + lcd_clear_images(); + lcd_clear_text(); + lcd_display_text((uint8_t*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_FONT16); } if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP] From eea21fc15d209eafeaa377354adb38464009de4a Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 16:07:35 +0100 Subject: [PATCH 44/46] TFTP Make str functions static --- project/Core/Src/tftp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index d4fa7b3..86d1a37 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -22,7 +22,7 @@ static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .le {.name = "virtImage.gif", .data = NULL, .len = 0, .offset = 0}, {.name = "virtText.txt", .data = NULL, .len = 0, .offset = 0}}; -int str_cat_str(char* dest, size_t dest_size, const char* src) { +static int str_cat_str(char* dest, size_t dest_size, const char* src) { size_t dest_len = strlen(dest); size_t src_len = strlen(src); if (dest_len + src_len > dest_size) { @@ -32,7 +32,7 @@ int str_cat_str(char* dest, size_t dest_size, const char* src) { return 0; } -int str_cat(char* dest, size_t dest_size, char c) { +static int str_cat(char* dest, size_t dest_size, char c) { size_t dest_len = strlen(dest); if (dest_len + 1 > dest_size) { return -1; From 38bdf7376353f12f9ef0107ad426821e51cbafc4 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 16:14:04 +0100 Subject: [PATCH 45/46] TFTP * Remove the dependency on llfs_root * Change over to llfs_next_file --- project/Core/Src/tftp.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 86d1a37..718a0f6 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -15,8 +15,6 @@ static const char* TAG = "tftp_server"; -extern struct llfs_data_file* llfs_root; - static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .len = 0, .offset = 0}, {.name = "virtImage.bmp", .data = NULL, .len = 0, .offset = 0}, {.name = "virtImage.gif", .data = NULL, .len = 0, .offset = 0}, @@ -245,10 +243,10 @@ void init_index(void) { for (int i = 0; i < 2; i++) { len += strlen(virt_file[i].name) + 1; } - const struct llfs_data_file* root = llfs_root; - while (root != NULL) { - len += strlen(root->name) + 1; - root = root->next; + void* mem = NULL; // Pointer for internal use by the llfs library + llfs_file_t* file; + while ((file = llfs_next_file(&mem, NULL)) != NULL) { + len += strlen(file->name) + 1; } len++; // +1 for the \0 virt_file[VIRT_INDEX_TXT].data = malloc(len); @@ -258,11 +256,13 @@ void init_index(void) { str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, virt_file[i].name); str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n'); } - root = llfs_root; - while (root != NULL) { - str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, root->name); + + mem = NULL; + file = NULL; + + while ((file = llfs_next_file(&mem, NULL)) != NULL) { + str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, file->name); str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n'); - root = root->next; } } From 65ad130c09fc4bdcf5cf6ab03b8f891569059c13 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 16:36:09 +0100 Subject: [PATCH 46/46] TFTP * Fix null terminated string in init_index * Add check if out of memory * Add debug message for file index.txt * Add MAX_VIRT_FILES --- project/Core/Src/tftp.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 718a0f6..9164403 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -10,6 +10,7 @@ #define VIRT_IMAGE_BMP 1 #define VIRT_IMAGE_GIF 2 #define VIRT_TEXT_TXT 3 +#define MAX_VIRT_FILES 4 #define IMAGE_BUFFER_SIZE 81920 @@ -240,7 +241,7 @@ int tftp_write(void* handle, struct pbuf* p) { void init_index(void) { size_t len = 0; // Add len of the virt files to the size - for (int i = 0; i < 2; i++) { + for (int i = 0; i < MAX_VIRT_FILES; i++) { len += strlen(virt_file[i].name) + 1; } void* mem = NULL; // Pointer for internal use by the llfs library @@ -249,10 +250,14 @@ void init_index(void) { len += strlen(file->name) + 1; } len++; // +1 for the \0 - virt_file[VIRT_INDEX_TXT].data = malloc(len); + virt_file[VIRT_INDEX_TXT].data = calloc(len, sizeof(char)); + if (virt_file[VIRT_INDEX_TXT].data == NULL) { + LOG_FATAL(TAG, "Could not allocate memory for index.txt"); + return; + } virt_file[VIRT_INDEX_TXT].len = len; - for (int i = 0; i < 2; i++) { + for (int i = 0; i < MAX_VIRT_FILES; i++) { str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, virt_file[i].name); str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n'); } @@ -275,6 +280,7 @@ void tftp_server_init(void) { LOG_INFO(TAG, "Initializing tftp server"); // init the index.txt virt_file init_index(); + LOG_DEBUG(TAG, "index.txt: %s", virt_file[VIRT_INDEX_TXT].data); // init the virtImage.raw virt_file with 80kb of ram virt_file[VIRT_IMAGE_BMP].data = calloc(IMAGE_BUFFER_SIZE, sizeof(char)); if (virt_file[VIRT_IMAGE_BMP].data == NULL) {