kosmos/source/firmware/kernel/shell.c

83 lines
1.6 KiB
C
Raw Normal View History

2016-08-01 14:53:06 +00:00
/*
* shell.c
*
* Created on: Aug 1, 2016
* Author: tkl
*/
#include <stddef.h>
#include <stdbool.h>
2016-08-02 09:41:47 +00:00
#include <string.h>
2016-08-01 14:53:06 +00:00
#include "stack.h"
#include "queue.h"
#include "driver.h"
#include "kernel.h"
2016-08-02 09:41:47 +00:00
#include "list.h"
#include "shell.h"
struct shell_object {
struct list command_list;
const struct driver *shell_device;
};
struct shell_object shell_object;
2016-08-01 14:53:06 +00:00
#define RX_STACK_SIZE 256
stack_t rx_stack[RX_STACK_SIZE];
struct thread_context rx_thread;
2016-08-02 09:41:47 +00:00
static void parse(const char *buffer, unsigned int len)
2016-08-01 14:53:06 +00:00
{
2016-08-02 09:41:47 +00:00
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;
}
2016-08-01 14:53:06 +00:00
}
2016-08-02 09:41:47 +00:00
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;
}
}
}
2016-08-01 14:53:06 +00:00
int shell_init(const struct driver *shell_device)
{
2016-08-02 09:41:47 +00:00
if(NULL == shell_device)
return -1;
list_init(&shell_object.command_list);
shell_object.shell_device = shell_device;
2016-08-01 14:53:06 +00:00
thread_create(&rx_thread, rx_stack, RX_STACK_SIZE, rx_func, NULL, THREAD_PRIO_LOW);
2016-08-02 09:41:47 +00:00
return 0;
2016-08-01 14:53:06 +00:00
}
2016-08-02 09:41:47 +00:00
int shell_add_command(struct command *command)
2016-08-01 14:53:06 +00:00
{
2016-08-02 09:41:47 +00:00
if(NULL == command)
return -1;
command->item.data = (unsigned int) command;
list_add(&shell_object.command_list, &command->item);
return 1;
2016-08-01 14:53:06 +00:00
}