add return parameter to tcp_cmd_parser to keep the connection open or closed
This commit is contained in:
2023-11-30 11:22:40 +01:00
parent 0f1f041cbf
commit ed2e7ef650

View File

@@ -83,55 +83,64 @@ char* get_next_token(char* input, const char* delimiters, char** next) {
return input;
}
static void tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv) {
/**
* @brief This function parses the command and calls the appropriate function
*
* @param pcb Pointer to the tcp_pcb struct to write to
* @param argc Count of arguments
* @param argv Array of arguments
* @return true Connection should be closed
* @return false Connection should be kept open
*/
static bool tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv) {
char* ext;
if (argc == 0) {
LOG_WARN(TAG, "No command given");
return;
return false;
}
str_tolower(argv[0]);
if (strcmp(argv[0], "help") == 0) {
tcp_cmd_print_help(pcb);
return;
return false;
} if (strcmp(argv[0], "text") == 0) {
if (argc == 2) {
lcd_clear_text();
lcd_display_text((uint8_t*)argv[1], 10, 10, color_txt, color_bg, LCD_FONT24);
return;
return false;
}
tcp_cmd_write(pcb, "Usage: text \"<text>\"\n");
tcp_cmd_write(pcb, "Usage: text <word>\n");
return;
return false;
} if (strcmp(argv[0], "bgcolor") == 0) {
if (argc == 2) {
color_bg = (uint32_t)strtol(argv[1], NULL, 16);
color_bg |= 0xff000000;
return;
return false;
} if (argc == 4) {
color_bg = 0xff000000;
color_bg |= (uint32_t)strtol(argv[1], NULL, 10) << 16;
color_bg |= (uint32_t)strtol(argv[2], NULL, 10) << 8;
color_bg |= (uint32_t)strtol(argv[3], NULL, 10);
return;
return false;
}
tcp_cmd_write(pcb, "Usage: bgcolor <background color>\n");
tcp_cmd_write(pcb, "Usage: bgcolor r g b\n");
return;
return false;
} if (strcmp(argv[0], "color") == 0) {
if (argc == 2) {
color_txt = (uint32_t)strtol(argv[1], NULL, 16);
color_txt |= 0xff000000;
return;
return false;
} if (argc == 4) {
color_txt = 0xff000000;
color_txt |= (uint32_t)strtol(argv[1], NULL, 10) << 16;
color_txt |= (uint32_t)strtol(argv[2], NULL, 10) << 8;
color_txt |= (uint32_t)strtol(argv[3], NULL, 10);
return;
return false;
}
tcp_cmd_write(pcb, "Usage: color 0x<txt color>\n");
tcp_cmd_write(pcb, "Usage: color <r> <g> <b>\n");
return;
return false;
} if (strcmp(argv[0], "listimages") == 0) {
void* mem = NULL; // Pointer for internal use by the llfs library
llfs_file_t* file;
@@ -139,62 +148,64 @@ static void tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv) {
tcp_cmd_write(pcb, file->name);
tcp_cmd_write(pcb, "\r\n");
}
return;
return false;
} if (strcmp(argv[0], "setimage") == 0) {
if (argc >= 2) {
ext = get_filename_ext(argv[1]);
if (strcmp(ext, "bmp") != 0) {
tcp_cmd_write(pcb, "File is not a bmp\n");
return;
return false;
}
} if (argc == 2) {
lcd_clear_images();
lcd_draw_img_from_fs(argv[1], 0, 0);
return;
return false;
} if (argc == 4) {
lcd_clear_images();
lcd_draw_img_from_fs(argv[1], (uint32_t)atoi(argv[2]), (uint32_t)atoi(argv[3]));
return;
return false;
}
tcp_cmd_write(pcb, "Usage: setimage <filename>\n");
tcp_cmd_write(pcb, "Usage: setimage <filename> <x> <y>\n");
return;
return false;
} if (strcmp(argv[0], "setgif") == 0) {
if (argc >= 2) {
char* ext = get_filename_ext(argv[1]);
if (strcmp(ext, "gif") != 0) {
tcp_cmd_write(pcb, "File is not a gif\n");
return;
return false;
}
} if (argc == 2) {
lcd_clear_images();
lcd_draw_gif_from_fs(argv[1], 0, 0);
return;
return false;
} if (argc == 4) {
lcd_clear_images();
lcd_draw_gif_from_fs(argv[1], (uint32_t)atoi(argv[2]), (uint32_t)atoi(argv[3]));
return;
return false;
}
tcp_cmd_write(pcb, "Usage: setgif <filename>\n");
tcp_cmd_write(pcb, "Usage: setgif <filename> <x> <y>\n");
return;
return false;
} if (strcmp(argv[0], "exit") == 0) {
tcp_cmd_write(pcb, "Exiting...\n");
lcd_clear_images();
lcd_clear_text();
tcp_close(pcb);
return;
return true;
}
tcp_cmd_write(pcb, "Unknown command: ");
tcp_cmd_write(pcb, argv[0]);
tcp_cmd_write(pcb, "\n");
tcp_cmd_write(pcb, "Type help for list of commands\r\n");
return false;
}
static err_t tcp_cmd_recv_new(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
int argc = 0;
char cmd[MAX_CMD_LEN];
char* argv[MAX_TOKENS];
bool close_conn = false;
LWIP_UNUSED_ARG(arg);
LOG_DEBUG(TAG, "TCP data received");
@@ -236,9 +247,13 @@ static err_t tcp_cmd_recv_new(void* arg, struct tcp_pcb* pcb, struct pbuf* p, er
argc++;
}
tcp_cmd_parser(pcb, argc, argv);
close_conn = tcp_cmd_parser(pcb, argc, argv);
tcp_cmd_write(pcb, "$> ");
if (close_conn) {
tcp_cmd_close(pcb);
} else {
tcp_cmd_write(pcb, "$> ");
}
defer:
pbuf_free(p);