kosmos/source/firmware/kernel/shell.c
2016-08-02 11:41:47 +02:00

83 lines
1.6 KiB
C

/*
* shell.c
*
* Created on: Aug 1, 2016
* Author: tkl
*/
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#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;
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 0;
}
int shell_add_command(struct command *command)
{
if(NULL == command)
return -1;
command->item.data = (unsigned int) command;
list_add(&shell_object.command_list, &command->item);
return 1;
}