first shell implementation
This commit is contained in:
parent
a2cddd208f
commit
35af737f68
@ -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_ */
|
||||
|
@ -7,28 +7,76 @@
|
||||
|
||||
#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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user