From 623cf3749cfaadd3cc6c4351563e072764253489 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 13 Nov 2023 16:03:14 +0100 Subject: [PATCH] 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 }