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