From 28a3d252b34161462cba816134e3520a5cf429e8 Mon Sep 17 00:00:00 2001 From: iysheng Date: Fri, 15 Apr 2022 15:45:44 +0800 Subject: [PATCH] fix the bug of tos shell --- components/shell/include/tos_shell.h | 4 +- components/shell/include/tos_shell_command.h | 2 + components/shell/tos_shell.c | 60 +++++++++++++++----- components/shell/tos_shell_commands.c | 55 +++++++++++++++++- examples/shell/shell_sample.c | 24 +++++--- 5 files changed, 117 insertions(+), 28 deletions(-) diff --git a/components/shell/include/tos_shell.h b/components/shell/include/tos_shell.h index 2d6c3893..77742e8e 100644 --- a/components/shell/include/tos_shell.h +++ b/components/shell/include/tos_shell.h @@ -26,7 +26,7 @@ #define SHELL_PARSER_TASK_STACK_SIZE 1024 #define SHELL_PARSER_TASK_PRIO 3 -typedef void (*shell_output_t)(const char *str); +typedef void (*shell_output_t)(const char ch); typedef struct shell_control_st { k_task_t parser; @@ -53,6 +53,8 @@ __API__ int tos_shell_cmd_set_unregiser(const shell_cmd_set_t *cmd_set); __API__ void tos_shell_printf(const char *format, ...); +__API__ void tos_shell_printfln(const char *format, ...); + __API__ void tos_shell_input_byte(uint8_t data); #endif /* _TOS_SHELL_H_ */ diff --git a/components/shell/include/tos_shell_command.h b/components/shell/include/tos_shell_command.h index 16e8019b..02089e3c 100644 --- a/components/shell/include/tos_shell_command.h +++ b/components/shell/include/tos_shell_command.h @@ -33,6 +33,8 @@ typedef struct shell_command_set_st { const shell_cmd_t *const commands; } shell_cmd_set_t; +__KNL__ int cmd_help(int argc, char *argv[]); + __KNL__ int shell_cmd_set_regiser(const shell_cmd_set_t *cmd_set); __KNL__ int shell_cmd_set_unregiser(const shell_cmd_set_t *cmd_set); diff --git a/components/shell/tos_shell.c b/components/shell/tos_shell.c index f940b33e..0646e731 100644 --- a/components/shell/tos_shell.c +++ b/components/shell/tos_shell.c @@ -35,10 +35,23 @@ __STATIC__ int shell_getchar(void) return err == K_ERR_NONE ? chr : -1; } +__STATIC__ void shell_putchar(const char ch) +{ + SHELL_CTL->output(ch); +} + +__STATIC__ void shell_puts(const char *s) +{ + while (*s) { + SHELL_CTL->output(*s++); + } +} + __STATIC__ int shell_readline(void) { - int chr, last_chr = 0; + int chr; char *buf = SHELL_CTL->cmd_buffer; + uint16_t line_length = 0; while (K_TRUE) { if (buf - SHELL_CTL->cmd_buffer >= (SHELL_CTL->cmd_buffer_size - 1)) { @@ -50,23 +63,29 @@ __STATIC__ int shell_readline(void) return -1; } - if (chr == '\n' && last_chr == '\r') { - *--buf = '\0'; - return 0; - } else if (chr == '\n') { + if (chr == '\n' || chr == '\r') { *buf = '\0'; - return 0; + tos_shell_printf("\r\n"); + break; + } else if (chr == '\t'){ + *buf = '\0'; + tos_shell_printf("\r\n"); + cmd_help(0, NULL); + break; } - - *buf++ = chr; - last_chr = chr; + + shell_putchar(chr); + + *buf++ = chr; + line_length++; } + return line_length; } __STATIC__ void shell_cmd_do_process(int argc, char *argv[]) { const shell_cmd_t *cmd; - static const char *cmd_not_found = "command not found\n"; + static const char *cmd_not_found = "command not found\r\n"; cmd = shell_cmd_find(argv[0]); if (!cmd) { @@ -82,8 +101,6 @@ __STATIC__ void shell_cmd_process(void) static char *argv[SHELL_CMD_ARGV_MAX]; char *pos = SHELL_CTL->cmd_buffer; - tos_shell_printf("%s\n", SHELL_CTL->cmd_buffer); - // left strip while (*pos == ' ' || *pos == '\t') { ++pos; @@ -113,7 +130,7 @@ __STATIC__ void shell_cmd_process(void) __STATIC__ void shell_prompt(void) { - tos_shell_printf("> "); + tos_shell_printf("tshell>"); } __STATIC__ void shell_parser(void *arg) @@ -125,7 +142,7 @@ __STATIC__ void shell_parser(void *arg) while (K_TRUE) { rc = shell_readline(); - if (!rc) { + if (rc > 0) { shell_cmd_process(); } @@ -208,7 +225,20 @@ __API__ void tos_shell_printf(const char *format, ...) vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); - (SHELL_CTL->output)(buffer); + shell_puts(buffer); +} + +__API__ void tos_shell_printfln(const char *format, ...) +{ + va_list args; + static char buffer[SHELL_OUTPUT_MAX]; + + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + va_end(args); + + shell_puts(buffer); + shell_puts("\r\n"); } __API__ void tos_shell_input_byte(uint8_t data) diff --git a/components/shell/tos_shell_commands.c b/components/shell/tos_shell_commands.c index acfcb8b7..85ef186b 100644 --- a/components/shell/tos_shell_commands.c +++ b/components/shell/tos_shell_commands.c @@ -20,23 +20,72 @@ __STATIC__ TOS_SLIST_DEFINE(cmd_set_list); -__STATIC__ int cmd_help(int argc, char *argv[]) +int cmd_help(int argc, char *argv[]) { shell_cmd_set_t *cmd_set; const shell_cmd_t *cmd; TOS_SLIST_FOR_EACH_ENTRY(cmd_set, shell_cmd_set_t, list, &cmd_set_list) { for (cmd = cmd_set->commands; cmd->name; ++cmd) { - tos_shell_printf("%-8s: %s\n", cmd->name, cmd->help); + tos_shell_printf("%-8s: %s\r\n", cmd->name, cmd->help); } } return 0; } +static void task_shell_walker(k_task_t *task) +{ + char *state_str = "ABNORMAL"; + + if (!task) { + return; + } + + state_str = state_str; + tos_shell_printfln("task name: %s", task->name); + + if (tos_task_curr_task_get() == task) { + state_str = "RUNNING"; + } else if (task->state == K_TASK_STATE_PENDTIMEOUT_SUSPENDED) { + state_str = "PENDTIMEOUT_SUSPENDED"; + } else if (task->state == K_TASK_STATE_PEND_SUSPENDED) { + state_str = "PEND_SUSPENDED"; + } else if (task->state == K_TASK_STATE_SLEEP_SUSPENDED) { + state_str = "SLEEP_SUSPENDED"; + } else if (task->state == K_TASK_STATE_PENDTIMEOUT) { + state_str = "PENDTIMEOUT"; + } else if (task->state == K_TASK_STATE_SUSPENDED) { + state_str = "SUSPENDED"; + } else if (task->state == K_TASK_STATE_PEND) { + state_str = "PEND"; + } else if (task->state == K_TASK_STATE_SLEEP) { + state_str = "SLEEP"; + } else if (task->state == K_TASK_STATE_READY) { + state_str = "READY"; + } + + tos_shell_printfln("task stat: %s", state_str); + tos_shell_printfln("stk size : %d", task->stk_size); + tos_shell_printfln("stk base : 0x%p", task->stk_base); + tos_shell_printfln("stk top : 0x%p", task->stk_base + task->stk_size); + +#if TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN > 0u + int depth; + + if (tos_task_stack_draught_depth(task, &depth) != K_ERR_NONE) { + depth = -1; + } + + tos_shell_printfln("stk depth: %d", depth); +#endif /* TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN */ + + tos_shell_printf("\r\n"); +} + __STATIC__ int cmd_ps(int argc, char *argv[]) { - tos_task_info_display(); + tos_task_walkthru(task_shell_walker); return 0; } diff --git a/examples/shell/shell_sample.c b/examples/shell/shell_sample.c index 919868be..5eb53b17 100644 --- a/examples/shell/shell_sample.c +++ b/examples/shell/shell_sample.c @@ -6,9 +6,9 @@ char cmd_buf[CMD_LEN_MAX]; hal_uart_t shell_uart; -void uart_output(const char *str) +void uart_output(const char ch) { - tos_hal_uart_write(&shell_uart, (const uint8_t *)str, strlen(str), 0xFF); + tos_hal_uart_write(&shell_uart, (const uint8_t *)&ch, 1, 0xFF); #if 0 /* if using c lib printf through uart, a simpler one is: */ @@ -19,34 +19,34 @@ void uart_output(const char *str) __STATIC__ int cmd_test00(int argc, char *argv[]) { int i = 0; - tos_shell_printf("test00:\n"); + tos_shell_printf("test00:\r\n"); for (i = 0; i < argc; ++i) { - tos_shell_printf("argv[%d]: %s\n", i, argv[i]); + tos_shell_printf("argv[%d]: %s\r\n", i, argv[i]); } return 0; } __STATIC__ int cmd_test01(int argc, char *argv[]) { - tos_shell_printf("test01:\n"); + tos_shell_printf("test01:\r\n"); return 0; } __STATIC__ int cmd_test10(int argc, char *argv[]) { int i = 0; - tos_shell_printf("test10:\n"); + tos_shell_printf("test10:\r\n"); for (i = 0; i < argc; ++i) { - tos_shell_printf("argv[%d]: %s\n", i, argv[i]); + tos_shell_printf("argv[%d]: %s\r\n", i, argv[i]); } return 0; } __STATIC__ int cmd_test11(int argc, char *argv[]) { - tos_shell_printf("test11:\n"); + tos_shell_printf("test11:\r\n"); return 0; } @@ -74,10 +74,16 @@ __STATIC__ shell_cmd_set_t custom_shell_cmd_set1 = { void application_entry(void *arg) { + int ret; + /* if test on ALIENTEK_STM32F429, switch HAL_UART_PORT_2 to HAL_UART_PORT_1 */ tos_hal_uart_init(&shell_uart, HAL_UART_PORT_2); - tos_shell_init(cmd_buf, sizeof(cmd_buf), uart_output, &custom_shell_cmd_set0); + ret = tos_shell_init(cmd_buf, sizeof(cmd_buf), uart_output, &custom_shell_cmd_set0); + if (ret < 0) { + printf("tos shell init fail, ret is %d\r\n", ret); + } + printf("tos shell init success\r\n"); tos_shell_cmd_set_regiser(&custom_shell_cmd_set1);