TFTP
Add tftp_custom_write and test functions
This commit is contained in:
@@ -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/*.*")
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
#ifndef PROJECT_TFTP_H
|
||||
#define PROJECT_TFTP_H
|
||||
#include <tftp_server.h>
|
||||
#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
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user