From 35af737f68e06957daf7e8ed0586654b1f810b43 Mon Sep 17 00:00:00 2001 From: tkl Date: Tue, 2 Aug 2016 11:41:47 +0200 Subject: [PATCH] first shell implementation --- .../kernel/{include => interface}/list.h | 0 source/firmware/kernel/interface/shell.h | 10 +++- source/firmware/kernel/shell.c | 56 +++++++++++++++++-- 3 files changed, 61 insertions(+), 5 deletions(-) rename source/firmware/kernel/{include => interface}/list.h (100%) diff --git a/source/firmware/kernel/include/list.h b/source/firmware/kernel/interface/list.h similarity index 100% rename from source/firmware/kernel/include/list.h rename to source/firmware/kernel/interface/list.h diff --git a/source/firmware/kernel/interface/shell.h b/source/firmware/kernel/interface/shell.h index ef5cf24..81fd032 100644 --- a/source/firmware/kernel/interface/shell.h +++ b/source/firmware/kernel/interface/shell.h @@ -8,7 +8,15 @@ #ifndef SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ #define SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ +typedef void *(*command_callback)(const char*); + +struct command { + const char *command; + const command_callback command_callback; + struct list_node item; +}; + int shell_init(const struct driver *shell_device); -int shell_add_command(const char *command, const void *callback, const void *param); +int shell_add_command(struct command *command); #endif /* SOURCE_FIRMWARE_KERNEL_INTERFACE_SHELL_H_ */ diff --git a/source/firmware/kernel/shell.c b/source/firmware/kernel/shell.c index 020adb2..1f32d3b 100644 --- a/source/firmware/kernel/shell.c +++ b/source/firmware/kernel/shell.c @@ -7,28 +7,76 @@ #include #include +#include #include "stack.h" #include "queue.h" #include "driver.h" #include "kernel.h" +#include "list.h" +#include "shell.h" + +struct shell_object { + struct list command_list; + const struct driver *shell_device; +}; + +struct shell_object shell_object; #define RX_STACK_SIZE 256 stack_t rx_stack[RX_STACK_SIZE]; struct thread_context rx_thread; -void rx_func(void *arg) +static void parse(const char *buffer, unsigned int len) { + if(NULL == buffer) + return; + struct list_node *it = shell_object.command_list.front; + while(it != NULL) { + struct command *cmd = (struct command *)it->data; + if(strstr(buffer, cmd->command)) { + cmd->command_callback(buffer); + return; + } + it = it->next; + } } +static void rx_func(void *arg) +{ + char buffer[80]; + unsigned int index = 0; + int ret = 0; + open(shell_object.shell_device); + while(1) { + ret = read(shell_object.shell_device, &buffer[index], + sizeof(buffer) / sizeof(buffer[0]) - index); + if(ret) { + if(buffer[index + ret - 1] == '\n') { + parse(buffer, index + ret); + index = 0; + } + else + index += ret; + } + } +} int shell_init(const struct driver *shell_device) { + if(NULL == shell_device) + return -1; + list_init(&shell_object.command_list); + shell_object.shell_device = shell_device; thread_create(&rx_thread, rx_stack, RX_STACK_SIZE, rx_func, NULL, THREAD_PRIO_LOW); - return -1; + return 0; } -int shell_add_command(const char *command, const void *callback, const void *param) +int shell_add_command(struct command *command) { - return -1; + if(NULL == command) + return -1; + command->item.data = (unsigned int) command; + list_add(&shell_object.command_list, &command->item); + return 1; }