From 46f2fcacd25b4e47b63937f42dde37c82a1107a3 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 20 Nov 2023 15:44:38 +0100 Subject: [PATCH] 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;