fix the bug of tos shell
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
#define SHELL_PARSER_TASK_STACK_SIZE 1024
|
#define SHELL_PARSER_TASK_STACK_SIZE 1024
|
||||||
#define SHELL_PARSER_TASK_PRIO 3
|
#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 {
|
typedef struct shell_control_st {
|
||||||
k_task_t parser;
|
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_printf(const char *format, ...);
|
||||||
|
|
||||||
|
__API__ void tos_shell_printfln(const char *format, ...);
|
||||||
|
|
||||||
__API__ void tos_shell_input_byte(uint8_t data);
|
__API__ void tos_shell_input_byte(uint8_t data);
|
||||||
|
|
||||||
#endif /* _TOS_SHELL_H_ */
|
#endif /* _TOS_SHELL_H_ */
|
||||||
|
@@ -33,6 +33,8 @@ typedef struct shell_command_set_st {
|
|||||||
const shell_cmd_t *const commands;
|
const shell_cmd_t *const commands;
|
||||||
} shell_cmd_set_t;
|
} 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_regiser(const shell_cmd_set_t *cmd_set);
|
||||||
|
|
||||||
__KNL__ int shell_cmd_set_unregiser(const shell_cmd_set_t *cmd_set);
|
__KNL__ int shell_cmd_set_unregiser(const shell_cmd_set_t *cmd_set);
|
||||||
|
@@ -35,10 +35,23 @@ __STATIC__ int shell_getchar(void)
|
|||||||
return err == K_ERR_NONE ? chr : -1;
|
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)
|
__STATIC__ int shell_readline(void)
|
||||||
{
|
{
|
||||||
int chr, last_chr = 0;
|
int chr;
|
||||||
char *buf = SHELL_CTL->cmd_buffer;
|
char *buf = SHELL_CTL->cmd_buffer;
|
||||||
|
uint16_t line_length = 0;
|
||||||
|
|
||||||
while (K_TRUE) {
|
while (K_TRUE) {
|
||||||
if (buf - SHELL_CTL->cmd_buffer >= (SHELL_CTL->cmd_buffer_size - 1)) {
|
if (buf - SHELL_CTL->cmd_buffer >= (SHELL_CTL->cmd_buffer_size - 1)) {
|
||||||
@@ -50,23 +63,29 @@ __STATIC__ int shell_readline(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chr == '\n' && last_chr == '\r') {
|
if (chr == '\n' || chr == '\r') {
|
||||||
*--buf = '\0';
|
|
||||||
return 0;
|
|
||||||
} else if (chr == '\n') {
|
|
||||||
*buf = '\0';
|
*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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shell_putchar(chr);
|
||||||
|
|
||||||
*buf++ = chr;
|
*buf++ = chr;
|
||||||
last_chr = chr;
|
line_length++;
|
||||||
}
|
}
|
||||||
|
return line_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
__STATIC__ void shell_cmd_do_process(int argc, char *argv[])
|
__STATIC__ void shell_cmd_do_process(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
const shell_cmd_t *cmd;
|
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]);
|
cmd = shell_cmd_find(argv[0]);
|
||||||
if (!cmd) {
|
if (!cmd) {
|
||||||
@@ -82,8 +101,6 @@ __STATIC__ void shell_cmd_process(void)
|
|||||||
static char *argv[SHELL_CMD_ARGV_MAX];
|
static char *argv[SHELL_CMD_ARGV_MAX];
|
||||||
char *pos = SHELL_CTL->cmd_buffer;
|
char *pos = SHELL_CTL->cmd_buffer;
|
||||||
|
|
||||||
tos_shell_printf("%s\n", SHELL_CTL->cmd_buffer);
|
|
||||||
|
|
||||||
// left strip
|
// left strip
|
||||||
while (*pos == ' ' || *pos == '\t') {
|
while (*pos == ' ' || *pos == '\t') {
|
||||||
++pos;
|
++pos;
|
||||||
@@ -113,7 +130,7 @@ __STATIC__ void shell_cmd_process(void)
|
|||||||
|
|
||||||
__STATIC__ void shell_prompt(void)
|
__STATIC__ void shell_prompt(void)
|
||||||
{
|
{
|
||||||
tos_shell_printf("> ");
|
tos_shell_printf("tshell>");
|
||||||
}
|
}
|
||||||
|
|
||||||
__STATIC__ void shell_parser(void *arg)
|
__STATIC__ void shell_parser(void *arg)
|
||||||
@@ -125,7 +142,7 @@ __STATIC__ void shell_parser(void *arg)
|
|||||||
while (K_TRUE) {
|
while (K_TRUE) {
|
||||||
rc = shell_readline();
|
rc = shell_readline();
|
||||||
|
|
||||||
if (!rc) {
|
if (rc > 0) {
|
||||||
shell_cmd_process();
|
shell_cmd_process();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +225,20 @@ __API__ void tos_shell_printf(const char *format, ...)
|
|||||||
vsnprintf(buffer, sizeof(buffer), format, args);
|
vsnprintf(buffer, sizeof(buffer), format, args);
|
||||||
va_end(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)
|
__API__ void tos_shell_input_byte(uint8_t data)
|
||||||
|
@@ -20,23 +20,72 @@
|
|||||||
|
|
||||||
__STATIC__ TOS_SLIST_DEFINE(cmd_set_list);
|
__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;
|
shell_cmd_set_t *cmd_set;
|
||||||
const shell_cmd_t *cmd;
|
const shell_cmd_t *cmd;
|
||||||
|
|
||||||
TOS_SLIST_FOR_EACH_ENTRY(cmd_set, shell_cmd_set_t, list, &cmd_set_list) {
|
TOS_SLIST_FOR_EACH_ENTRY(cmd_set, shell_cmd_set_t, list, &cmd_set_list) {
|
||||||
for (cmd = cmd_set->commands; cmd->name; ++cmd) {
|
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;
|
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[])
|
__STATIC__ int cmd_ps(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
tos_task_info_display();
|
tos_task_walkthru(task_shell_walker);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,9 +6,9 @@ char cmd_buf[CMD_LEN_MAX];
|
|||||||
|
|
||||||
hal_uart_t shell_uart;
|
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 0
|
||||||
/* if using c lib printf through uart, a simpler one is: */
|
/* 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[])
|
__STATIC__ int cmd_test00(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
tos_shell_printf("test00:\n");
|
tos_shell_printf("test00:\r\n");
|
||||||
|
|
||||||
for (i = 0; i < argc; ++i) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
__STATIC__ int cmd_test01(int argc, char *argv[])
|
__STATIC__ int cmd_test01(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
tos_shell_printf("test01:\n");
|
tos_shell_printf("test01:\r\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
__STATIC__ int cmd_test10(int argc, char *argv[])
|
__STATIC__ int cmd_test10(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
tos_shell_printf("test10:\n");
|
tos_shell_printf("test10:\r\n");
|
||||||
|
|
||||||
for (i = 0; i < argc; ++i) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
__STATIC__ int cmd_test11(int argc, char *argv[])
|
__STATIC__ int cmd_test11(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
tos_shell_printf("test11:\n");
|
tos_shell_printf("test11:\r\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,10 +74,16 @@ __STATIC__ shell_cmd_set_t custom_shell_cmd_set1 = {
|
|||||||
|
|
||||||
void application_entry(void *arg)
|
void application_entry(void *arg)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
/* if test on ALIENTEK_STM32F429, switch HAL_UART_PORT_2 to HAL_UART_PORT_1 */
|
/* 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_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);
|
tos_shell_cmd_set_regiser(&custom_shell_cmd_set1);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user