* Change typo
* Format
* Add doxygen
This commit is contained in:
2023-11-20 15:44:38 +01:00
parent ff72c9a5eb
commit 46f2fcacd2
2 changed files with 65 additions and 32 deletions

View File

@@ -14,11 +14,11 @@
extern "C" { extern "C" {
#endif #endif
#define LOGGER_LEVEL_ALL #define LOGGER_LEVEL_ALL
#include "log.h"
#include "llfs.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "llfs.h"
#include "log.h"
#define TFTP_READ 0 #define TFTP_READ 0
@@ -29,14 +29,47 @@ extern "C" {
typedef struct tftp_custom_file_s { typedef struct tftp_custom_file_s {
char* data; char* data;
size_t len; size_t len;
char*name; char* name;
size_t ofset; size_t offset;
}tftp_custom_file_t; } tftp_custom_file_t;
/**
* @brief Initialize the TFTP server
*/
void tftp_server_init(void); void tftp_server_init(void);
/**
* @brief Uninitialize the TFTP server
*/
void tftp_server_deinit(void); 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); 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); 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); size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -9,7 +9,7 @@
#define VIRT_INDEX_TXT 0 #define VIRT_INDEX_TXT 0
#define VIRT_IMAGE_BMP 1 #define VIRT_IMAGE_BMP 1
#define VIRT_IMAGE_GIF 2 #define VIRT_IMAGE_GIF 2
#define VIRT_TEXT_TXT 3 #define VIRT_TEXT_TXT 3
#define IMAGE_BUFFER_SIZE 81920 #define IMAGE_BUFFER_SIZE 81920
@@ -17,10 +17,10 @@ static const char* TAG = "tftp_server";
extern struct llfs_data_file* llfs_root; extern struct llfs_data_file* llfs_root;
static tftp_custom_file_t virt_file[] = {{.name = "index.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, .ofset = 0}, {.name = "virtImage.bmp", .data = NULL, .len = 0, .offset = 0},
{.name = "virtImage.gif", .data = NULL, .len = 0, .ofset = 0}, {.name = "virtImage.gif", .data = NULL, .len = 0, .offset = 0},
{.name = "virtText.txt", .data = NULL, .len = 0, .ofset = 0}}; {.name = "virtText.txt", .data = NULL, .len = 0, .offset = 0}};
int str_cat_str(char* dest, size_t dest_size, const char* src) { int str_cat_str(char* dest, size_t dest_size, const char* src) {
size_t dest_len = strlen(dest); 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; 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); size_t dest_len = strlen(dest);
if (dest_len + 1 > dest_size) { 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) { void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
switch (whence) { switch (whence) {
case SEEK_SET: case SEEK_SET:
handle->ofset = offset; handle->offset = offset;
break; break;
case SEEK_CUR: case SEEK_CUR:
handle->ofset += offset; handle->offset += offset;
break; break;
case SEEK_END: case SEEK_END:
break; break;
} }
if (handle->ofset > handle->len) { if (handle->offset > handle->len) {
handle->ofset = 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 * @param[in,out] handle Custom file handles
*/ */
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
if (handle->ofset + bytes > handle->len) { if (handle->offset + bytes > handle->len) {
bytes = handle->len - handle->ofset; bytes = handle->len - handle->offset;
} }
memcpy(buf, handle->data + handle->ofset, bytes); memcpy(buf, handle->data + handle->offset, bytes);
handle->ofset += bytes; handle->offset += bytes;
((char*)buf)[bytes] = '\0'; ((char*)buf)[bytes] = '\0';
if (handle->ofset > handle->len) { if (handle->offset > handle->len) {
bytes = 0; bytes = 0;
} }
return bytes; 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 * @return The number of bytes written
*/ */
size_t tftp_custom_fwrite(const 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) { if (handle->offset + bytes > handle->len) {
bytes = handle->len - handle->ofset; bytes = handle->len - handle->offset;
} }
memcpy(handle->data + handle->ofset, buf, bytes); memcpy(handle->data + handle->offset, buf, bytes);
handle->ofset += bytes; handle->offset += bytes;
if (handle->ofset > handle->len) { if (handle->offset > handle->len) {
bytes = 0; bytes = 0;
} }
return bytes; return bytes;
@@ -160,16 +159,17 @@ void tftp_close(void* handle) {
if (handle == &virt_file[VIRT_IMAGE_GIF]) { if (handle == &virt_file[VIRT_IMAGE_GIF]) {
lcd_clear(LCD_COLOR_BLACK); 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]) { if (handle == &virt_file[VIRT_TEXT_TXT]) {
lcd_clear(LCD_COLOR_BLACK); 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]) { if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP]
((tftp_custom_file_t*)handle)->ofset = 0; || handle == &virt_file[VIRT_IMAGE_GIF] || handle == &virt_file[VIRT_TEXT_TXT]) {
((tftp_custom_file_t*)handle)->offset = 0;
return; 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"); LOG_CRIT(TAG, "Exception: Trying to read a write only file");
return -1; return -1;
} }
ret = (int)fread(buf, sizeof(uint8_t), (size_t)bytes, file); ret = (int)fread(buf, sizeof(uint8_t), (size_t)bytes, file);
if (ret <= 0) { if (ret <= 0) {
return -1; return -1;
@@ -301,7 +301,7 @@ void tftp_server_deinit(void) {
free(virt_file[VIRT_INDEX_TXT].data); free(virt_file[VIRT_INDEX_TXT].data);
virt_file[VIRT_INDEX_TXT].data = NULL; virt_file[VIRT_INDEX_TXT].data = NULL;
virt_file[VIRT_INDEX_TXT].len = 0; 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); free(virt_file[VIRT_IMAGE_BMP].data);
virt_file[VIRT_IMAGE_BMP].data = NULL; virt_file[VIRT_IMAGE_BMP].data = NULL;
virt_file[VIRT_IMAGE_BMP].len = 0; virt_file[VIRT_IMAGE_BMP].len = 0;