Change to my own struct for the virtual files
init the index.txt from the tftp_server_init
add tftp_server_deinit + add to docs
This commit is contained in:
2023-11-11 00:46:02 +01:00
parent e1118908d9
commit d35c79480e
3 changed files with 70 additions and 14 deletions

View File

@@ -6,6 +6,13 @@ The TFTP task is initialized in the main function.
// Initialize TFTP task // Initialize TFTP task
tftp_init(); tftp_init();
``` ```
## Deinitialization
If you would ever want to deinitialize the TFTP task, you can do so by calling the following function.
```c
// Deinitialize TFTP task
tftp_server_deinit();
```
## Usage ## Usage
The TFTP task is used to receive and send files via TFTP. The TFTP task is used to receive and send files via TFTP.
### Receive a file ### Receive a file

View File

@@ -16,5 +16,13 @@
#define TFTP_READ 0 #define TFTP_READ 0
typedef struct tftp_custom_file_s {
char* data;
size_t len;
char*name;
size_t ofset;
}tftp_custom_file_t;
void tftp_server_init(void); void tftp_server_init(void);
void tftp_server_deinit(void);
#endif // PROJECT_TFTP_H #endif // PROJECT_TFTP_H

View File

@@ -10,9 +10,10 @@ static const char* TAG = "tftp_server";
extern struct llfs_data_file* llfs_root; extern struct llfs_data_file* llfs_root;
static llfs_file_t virt_file[2] = { static tftp_custom_file_t virt_file[] =
{.name = "index.txt", .data = "test", .len = 4}, {
{.name = "virtImage.raw", .data = "test", .len = 4} {.name = "index.txt",.data = NULL, .len = 0, .ofset = 0},
{.name = "virtImage.raw",.data = NULL, .len = 0, .ofset = 0}
}; };
int str_cat_str(char* dest, int dest_size, const char* src) { int str_cat_str(char* dest, int dest_size, const char* src) {
@@ -99,19 +100,12 @@ int tftp_read(void* handle, void* buf, int bytes) {
return -1; return -1;
} }
FILE* file = (FILE*)handle; FILE* file = (FILE*)handle;
LOG_INFO(TAG, "reading file: %s", file->name);
if (file == &virt_file[0]) { if (file == &virt_file[0]) {
const struct llfs_data_file* root = llfs_root; // TODO: read index.txt using tftp_custom_file
str_cat_str(buf, bytes, virt_file[0].name); LOG_CRIT(TAG, "Reading from index.txt is not implemented yet");
str_cat(buf, bytes, '\n'); return -1;
str_cat_str(buf, bytes, virt_file[1].name);
str_cat(buf, bytes, '\n');
while(root != NULL) {
str_cat(buf, bytes, root->name);
str_cat(buf, bytes, '\n');
file = root->next;
}
} else if (file == &virt_file[1]) { } else if (file == &virt_file[1]) {
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;
@@ -134,6 +128,39 @@ int tftp_write(void* handle, struct pbuf* p) {
LOG_DEBUG(TAG, "Not implemented yet"); LOG_DEBUG(TAG, "Not implemented yet");
} }
/**
* @brief This function creates the file list for index.txt
*/
void init_index(void)
{
int len = 0;
// Add len of the virt files to the size
for (int i = 0; i < 2; i++)
{
len += strlen(virt_file[i].name) + 1;
}
const struct llfs_data_file* root = llfs_root;
while(root != NULL) {
len += strlen(root->name) + 1;
root = root->next;
}
virt_file[0].data = malloc(len);
virt_file[0].len = len;
for (int i = 0; i < 2; i++)
{
str_cat_str(virt_file[0].data, len, virt_file[i].name);
str_cat(virt_file[0].data, len, '\n');
}
root = llfs_root;
while(root != NULL) {
str_cat_str(virt_file[0].data, len, root->name);
str_cat(virt_file[0].data, len, '\n');
root = root->next;
}
}
struct tftp_context tftpContext_s = { struct tftp_context tftpContext_s = {
.open = tftp_open, .open = tftp_open,
.close = tftp_close, .close = tftp_close,
@@ -146,9 +173,23 @@ struct tftp_context tftpContext_s = {
*/ */
void tftp_server_init(void) { void tftp_server_init(void) {
LOG_INFO(TAG, "Initializing tftp server"); LOG_INFO(TAG, "Initializing tftp server");
// init the index.txt virt_file
init_index();
// Init the tftp server
if (tftp_init(&tftpContext_s) != ERR_OK) { if (tftp_init(&tftpContext_s) != ERR_OK) {
LOG_FATAL(TAG, "Could not initialize tftp server"); LOG_FATAL(TAG, "Could not initialize tftp server");
return; return;
} }
LOG_INFO(TAG, "tftp server initialized successfully"); LOG_INFO(TAG, "tftp server initialized successfully");
}
void tftp_server_deinit(void) {
LOG_INFO(TAG, "Deinitializing tftp server");
tftp_cleanup();
LOG_INFO(TAG, "tftp server deinitialized successfully");
free(virt_file[0].data);
virt_file[0].data = NULL;
virt_file[0].len = 0;
virt_file[0].ofset = 0;
} }