diff --git a/project/Core/Inc/tcp_cmd.h b/project/Core/Inc/tcp_cmd.h index 4134085..8466a38 100644 --- a/project/Core/Inc/tcp_cmd.h +++ b/project/Core/Inc/tcp_cmd.h @@ -1,9 +1,8 @@ -/* - * cmd.h - * - * Created on: 13 Nov 2023 - * Author: gertr - */ +/** +* @file tcp_cmd.h +* @brief TCP CMD interface +* @author Gert R. +*/ #ifndef INC_TCP_CMD_H_ #define INC_TCP_CMD_H_ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index b5f6500..0bcb11d 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -32,6 +32,7 @@ #include "tftp.h" #include "modbus_tcp.h" #include "UDP_broadcast.h" +#include "tcp_cmd.h" /* USER CODE END Includes */ @@ -136,6 +137,9 @@ int main(void) /* Initialize the tftp server */ tftp_server_init(); + /* Initialize tcp command interface*/ + tcp_cmd_init(); + /* Initialize Modbus*/ modbus_init(); diff --git a/project/Core/Src/tcp_cmd.c b/project/Core/Src/tcp_cmd.c index 1e21d90..2d32a56 100644 --- a/project/Core/Src/tcp_cmd.c +++ b/project/Core/Src/tcp_cmd.c @@ -1,13 +1,12 @@ -/* - * cmd.c - * - * Created on: 13 Nov 2023 - * Author: Gert Roelandts +/** + * @file tcp_cmd.c + * @brief TCP CMD interface + * @author Gert R. */ -#include +#include "tcp_cmd.h" -uint32_t result_txt = 0xff000000; // Store text color -uint32_t result_bg = 0xff000000; // Store background color +static uint32_t result_txt = 0xff000000; // Store text color +static uint32_t result_bg = 0xff000000; // Store background color static void tcp_cmd_close(struct tcp_pcb* pcb) { tcp_arg(pcb, NULL); @@ -17,7 +16,6 @@ static void tcp_cmd_close(struct tcp_pcb* pcb) { } static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) { - size_t i; size_t len; size_t number_of_files; uint8_t file_in_fs; @@ -51,14 +49,14 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t if (!strncmp(tcp_buffer, "help", 4)) { check = 1; tcp_write(pcb, "help : laat lijst zien met alle commando's\r\n" - "text : geeft tekst mee die op LCD komt (uw_text)\r\n" - "color : kleur achtergrond van scherm (255 255 255)\r\n" - "textColor : kleur van tekst (255 255 255)\r\n" - "listImages: laat een lijst zien van de mogelijke afbeeldingen\r\n" - "setImage : veranderd te afbeelding (naam_afbeelding)\r\n" - "exit : sluit de verbinding\r\n", 354, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); - tcp_output(pcb); + "text : geeft tekst mee die op LCD komt (uw_text)\r\n" + "color : kleur achtergrond van scherm (255 255 255)\r\n" + "textColor : kleur van tekst (255 255 255)\r\n" + "listImages: laat een lijst zien van de mogelijke afbeeldingen\r\n" + "setImage : veranderd te afbeelding (naam_afbeelding)\r\n" + "exit : sluit de verbinding\r\n", 354, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); } else if (!strncmp(tcp_buffer, "text ", 5)) { + size_t i; for (i = 0; i < len - 4; i++) { text[i] = tcp_buffer[i + 5]; } @@ -93,13 +91,16 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t } else if (!strncmp(tcp_buffer, "listImages", 10)) { number_of_files = llfs_file_count(); - llfs_file_t file_list[number_of_files]; + if (number_of_files > 0) { + llfs_file_t file_list[number_of_files]; + number_of_files = llfs_file_list(file_list, number_of_files, NULL); - number_of_files = llfs_file_list(file_list, number_of_files, NULL); - - for (size_t i = 0; i < number_of_files; i++) { - tcp_write(pcb, file_list[i].name, strlen(file_list[i].name), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); - tcp_write(pcb, "\r\n", 2, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + for (size_t i = 0; i < number_of_files; i++) { + tcp_write(pcb, file_list[i].name, strlen(file_list[i].name), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_write(pcb, "\r\n", 2, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + } + } else { + tcp_write(pcb, "NO files in filesystem\r\n", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); } check = 1; @@ -116,28 +117,32 @@ static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t number_of_files = llfs_file_count(); - llfs_file_t file_list[number_of_files]; + if(number_of_files > 0) { + llfs_file_t file_list[number_of_files]; - number_of_files = llfs_file_list(file_list, number_of_files, NULL); + number_of_files = llfs_file_list(file_list, number_of_files, NULL); - file_in_fs = 0; - for (size_t i = 0; i < number_of_files; i++) { - if (!strcmp(filename, file_list[i].name)) { - file_in_fs = 1; + file_in_fs = 0; + for (size_t i = 0; i < number_of_files; i++) { + if (!strcmp(filename, file_list[i].name)) { + file_in_fs = 1; + } } - } - // Check which file extension is used and call right function - if (!strncmp(extension, "bmp", 3) && file_in_fs) { - lcd_clear_images(); - lcd_draw_img_from_fs(filename, 10, 10); - } else if (!strncmp(extension, "gif", 3) && file_in_fs) { - lcd_clear_images(); - lcd_draw_gif_from_fs(filename, 10, 10); - } else if (!file_in_fs) { - tcp_write(pcb, "File NOT in filesystem\n\r", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + // Check which file extension is used and call right function + if (!strncmp(extension, "bmp", 3) && file_in_fs) { + lcd_clear_images(); + lcd_draw_img_from_fs(filename, 10, 10); + } else if (!strncmp(extension, "gif", 3) && file_in_fs) { + lcd_clear_images(); + lcd_draw_gif_from_fs(filename, 10, 10); + } else if (!file_in_fs) { + tcp_write(pcb, "File NOT in filesystem\n\r", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + } else { + tcp_write(pcb, "Extension NOT supported\n\r", 25, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + } } else { - tcp_write(pcb, "Extension NOT supported\n\r", 25, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); + tcp_write(pcb, "NO files in filesystem\r\n", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); } check = 1; @@ -189,7 +194,6 @@ static err_t tcp_cmd_accept(void* arg, struct tcp_pcb* pcb, err_t err) { void tcp_cmd_init(void) { struct tcp_pcb* tcp_pcb; - tcp_pcb = tcp_new(); tcp_bind(tcp_pcb, IP_ADDR_ANY, 23); tcp_pcb = tcp_listen(tcp_pcb);