|
|
|
|
@@ -24,27 +24,11 @@ static tftp_custom_file_t virt_file[] = {{.name = "index.txt", .data = NULL, .le
|
|
|
|
|
{.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}};
|
|
|
|
|
|
|
|
|
|
static int str_cat_str(char* dest, size_t dest_size, const char* src) {
|
|
|
|
|
size_t dest_len = strlen(dest);
|
|
|
|
|
size_t src_len = strlen(src);
|
|
|
|
|
if (dest_len + src_len > dest_size) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
memcpy(dest + dest_len, src, src_len);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int str_cat(char* dest, size_t dest_size, char c) {
|
|
|
|
|
size_t dest_len = strlen(dest);
|
|
|
|
|
if (dest_len + 1 > dest_size) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
dest[dest_len] = c;
|
|
|
|
|
dest[dest_len + 1] = '\0';
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void* tftp_open(const char* fname, const char* mode, uint8_t write);
|
|
|
|
|
static void tftp_close(void* handle);
|
|
|
|
|
static int tftp_read(void* handle, void* buf, int bytes);
|
|
|
|
|
static int tftp_write(void* handle, struct pbuf* p);
|
|
|
|
|
static struct tftp_context tftpContext_s = {.open = tftp_open, .close = tftp_close, .read = tftp_read, .write = tftp_write};
|
|
|
|
|
/**
|
|
|
|
|
* @brief tftp custom file functions to set the offset and read the data
|
|
|
|
|
* @param[in,out] handle Custom file handles
|
|
|
|
|
@@ -72,6 +56,7 @@ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
|
|
|
|
|
* auto rolling over the offset
|
|
|
|
|
* if the bytes to read is bigger than the remaining bytes
|
|
|
|
|
* it will read the remaining bytes and set the bytes to 0
|
|
|
|
|
* this function does not handle null termination
|
|
|
|
|
* @param[out] buf The buffer to write the data to
|
|
|
|
|
* @param[in] bytes The number of bytes to read
|
|
|
|
|
* @param[in,out] handle Custom file handles
|
|
|
|
|
@@ -82,7 +67,6 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
|
|
|
|
}
|
|
|
|
|
memcpy(buf, handle->data + handle->offset, bytes);
|
|
|
|
|
handle->offset += bytes;
|
|
|
|
|
((char*)buf)[bytes] = '\0';
|
|
|
|
|
if (handle->offset > handle->len) {
|
|
|
|
|
bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
@@ -91,7 +75,12 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief tftp custom file functions to write the data
|
|
|
|
|
* auto rolling over the offset
|
|
|
|
|
* auto rolling over the offset
|
|
|
|
|
* if the bytes to write is bigger than the remaining bytes
|
|
|
|
|
* it will write the remaining bytes and set the bytes to 0
|
|
|
|
|
* this function does not handle null termination
|
|
|
|
|
* if you want to write a null termination you have to add it to the data
|
|
|
|
|
* and set the len to the new length before calling this function
|
|
|
|
|
*
|
|
|
|
|
* @param buf The buffer to write the data to
|
|
|
|
|
* @param bytes The number of bytes to write
|
|
|
|
|
@@ -104,7 +93,7 @@ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* han
|
|
|
|
|
}
|
|
|
|
|
memcpy(handle->data + handle->offset, buf, bytes);
|
|
|
|
|
handle->offset += bytes;
|
|
|
|
|
if (handle->offset > handle->len) {
|
|
|
|
|
if (handle->offset >= handle->len) {
|
|
|
|
|
bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
return bytes;
|
|
|
|
|
@@ -124,7 +113,7 @@ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* han
|
|
|
|
|
* @param write Flag indicating read (0) or write (!= 0) access
|
|
|
|
|
* @return void* File handle supplied to other functions
|
|
|
|
|
*/
|
|
|
|
|
void* tftp_open(const char* fname, const char* mode, uint8_t write) {
|
|
|
|
|
static void* tftp_open(const char* fname, const char* mode, uint8_t write) {
|
|
|
|
|
LOG_INFO(TAG, "Opening %s", fname);
|
|
|
|
|
|
|
|
|
|
UNUSED(mode);
|
|
|
|
|
@@ -148,7 +137,7 @@ void* tftp_open(const char* fname, const char* mode, uint8_t write) {
|
|
|
|
|
*
|
|
|
|
|
* @param handle The handle to the file to close
|
|
|
|
|
*/
|
|
|
|
|
void tftp_close(void* handle) {
|
|
|
|
|
static void tftp_close(void* handle) {
|
|
|
|
|
LOG_INFO(TAG, "closing file");
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
|
LOG_CRIT(TAG, "handle is null");
|
|
|
|
|
@@ -170,7 +159,7 @@ void tftp_close(void* handle) {
|
|
|
|
|
if (handle == &virt_file[VIRT_TEXT_TXT]) {
|
|
|
|
|
lcd_clear_images();
|
|
|
|
|
lcd_clear_text();
|
|
|
|
|
lcd_display_text((const char*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_TRANSPARENT, LCD_FONT16);
|
|
|
|
|
lcd_display_text((char*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_TRANSPARENT, LCD_FONT16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP]
|
|
|
|
|
@@ -192,7 +181,7 @@ void tftp_close(void* handle) {
|
|
|
|
|
* @param bytes Number of bytes to copy to buf
|
|
|
|
|
* @return int >= 0: Success; < 0: Error
|
|
|
|
|
*/
|
|
|
|
|
int tftp_read(void* handle, void* buf, int bytes) {
|
|
|
|
|
static int tftp_read(void* handle, void* buf, int bytes) {
|
|
|
|
|
int ret = 0;
|
|
|
|
|
LOG_INFO(TAG, "reading file");
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
|
@@ -230,7 +219,7 @@ int tftp_read(void* handle, void* buf, int bytes) {
|
|
|
|
|
* In other words, TFTP headers are stripped off.
|
|
|
|
|
* @return int >= 0: Success; < 0: Error
|
|
|
|
|
*/
|
|
|
|
|
int tftp_write(void* handle, struct pbuf* p) {
|
|
|
|
|
static int tftp_write(void* handle, struct pbuf* p) {
|
|
|
|
|
LOG_INFO(TAG, "Writing file");
|
|
|
|
|
tftp_custom_file_t* file = (tftp_custom_file_t*)handle;
|
|
|
|
|
if (file == &virt_file[VIRT_IMAGE_BMP] || file == &virt_file[VIRT_IMAGE_GIF] || file == &virt_file[VIRT_TEXT_TXT]) {
|
|
|
|
|
@@ -262,20 +251,22 @@ void init_index(void) {
|
|
|
|
|
virt_file[VIRT_INDEX_TXT].len = len;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_VIRT_FILES; i++) {
|
|
|
|
|
str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, virt_file[i].name);
|
|
|
|
|
str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n');
|
|
|
|
|
strncat(virt_file[VIRT_INDEX_TXT].data, virt_file[i].name, len);
|
|
|
|
|
strncat(virt_file[VIRT_INDEX_TXT].data, "\n", len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mem = NULL;
|
|
|
|
|
file = NULL;
|
|
|
|
|
|
|
|
|
|
while ((file = llfs_next_file(&mem, NULL)) != NULL) {
|
|
|
|
|
str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, file->name);
|
|
|
|
|
str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n');
|
|
|
|
|
strncat(virt_file[VIRT_INDEX_TXT].data, file->name, len);
|
|
|
|
|
strncat(virt_file[VIRT_INDEX_TXT].data, "\n", len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tftp_context tftpContext_s = {.open = tftp_open, .close = tftp_close, .read = tftp_read, .write = tftp_write};
|
|
|
|
|
LOG_DEBUG(TAG, "index.txt:\n%s", virt_file[VIRT_INDEX_TXT].data);
|
|
|
|
|
LOG_DEBUG(TAG, "index.txt: %u strlen: %u", virt_file[VIRT_INDEX_TXT].len, strlen(virt_file[VIRT_INDEX_TXT].data));
|
|
|
|
|
LOG_INFO(TAG, "index.txt created");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Initialize tftp server
|
|
|
|
|
@@ -284,8 +275,6 @@ void tftp_server_init(void) {
|
|
|
|
|
LOG_INFO(TAG, "Initializing tftp server");
|
|
|
|
|
init_index();
|
|
|
|
|
|
|
|
|
|
LOG_DEBUG(TAG, "index.txt: %s", virt_file[VIRT_INDEX_TXT].data);
|
|
|
|
|
|
|
|
|
|
// init the virtImage virt_file with 80kb of ram
|
|
|
|
|
virt_file[VIRT_IMAGE_BMP].data = calloc(IMAGE_BUFFER_SIZE, sizeof(char));
|
|
|
|
|
if (virt_file[VIRT_IMAGE_BMP].data == NULL) {
|
|
|
|
|
|