basic shell commands implemented
This commit is contained in:
17
source/firmware/kernel/include/shell_commands.h
Normal file
17
source/firmware/kernel/include/shell_commands.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* shell_commands.h
|
||||
*
|
||||
* Created on: Aug 11, 2016
|
||||
* Author: tkl
|
||||
*/
|
||||
|
||||
#ifndef SOURCE_FIRMWARE_KERNEL_INCLUDE_SHELL_COMMANDS_H_
|
||||
#define SOURCE_FIRMWARE_KERNEL_INCLUDE_SHELL_COMMANDS_H_
|
||||
|
||||
struct command cmd_kosmos_version;
|
||||
struct command cmd_list_all_commands;
|
||||
struct command cmd_echo_on;
|
||||
struct command cmd_echo_off;
|
||||
|
||||
|
||||
#endif /* SOURCE_FIRMWARE_KERNEL_INCLUDE_SHELL_COMMANDS_H_ */
|
19
source/firmware/kernel/include/shell_data.h
Normal file
19
source/firmware/kernel/include/shell_data.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* shell_data.h
|
||||
*
|
||||
* Created on: Aug 11, 2016
|
||||
* Author: tkl
|
||||
*/
|
||||
|
||||
#ifndef SOURCE_FIRMWARE_KERNEL_INCLUDE_SHELL_DATA_H_
|
||||
#define SOURCE_FIRMWARE_KERNEL_INCLUDE_SHELL_DATA_H_
|
||||
|
||||
struct shell_object {
|
||||
struct list command_list;
|
||||
const struct driver *shell_device;
|
||||
bool echo_on;
|
||||
};
|
||||
|
||||
struct shell_object shell_object;
|
||||
|
||||
#endif /* SOURCE_FIRMWARE_KERNEL_INCLUDE_SHELL_DATA_H_ */
|
@@ -20,5 +20,6 @@ struct list {
|
||||
|
||||
int list_init(struct list *head);
|
||||
int list_add(struct list *head, struct list_node *node);
|
||||
int list_get_len(struct list *head);
|
||||
|
||||
#endif /* SOURCE_FIRMWARE_KERNEL_LIST_H_ */
|
||||
|
@@ -12,6 +12,7 @@ typedef void *(*command_callback)(const char*);
|
||||
|
||||
struct command {
|
||||
const char *command;
|
||||
const char *description;
|
||||
const command_callback command_callback;
|
||||
struct list_node item;
|
||||
};
|
||||
|
@@ -34,3 +34,16 @@ int list_add(struct list *head, struct list_node *node)
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int list_get_len(struct list *head)
|
||||
{
|
||||
int ret = 0;
|
||||
if(NULL == head)
|
||||
return -1;
|
||||
struct list_node *it = head->front;
|
||||
while(NULL != it) {
|
||||
ret++;
|
||||
it = it->next;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@@ -14,14 +14,10 @@
|
||||
#include "driver.h"
|
||||
#include "kernel.h"
|
||||
#include "list.h"
|
||||
#include "shell_data.h"
|
||||
#include "shell.h"
|
||||
#include "shell_commands.h"
|
||||
|
||||
struct shell_object {
|
||||
struct list command_list;
|
||||
const struct driver *shell_device;
|
||||
};
|
||||
|
||||
struct shell_object shell_object;
|
||||
|
||||
#define TH_STACK_SIZE 256
|
||||
stack_t th_stack[TH_STACK_SIZE];
|
||||
@@ -52,7 +48,17 @@ static void rx_func(void *arg)
|
||||
ret = read(shell_object.shell_device, &buffer[index],
|
||||
sizeof(buffer) / sizeof(buffer[0]) - index - 1);
|
||||
if(ret) {
|
||||
write(shell_object.shell_device, &buffer[index], ret); // echo
|
||||
if(shell_object.echo_on) {
|
||||
if((buffer[index + ret - 1] == '\n') && (buffer[index + ret -2] != '\r')) {
|
||||
write(shell_object.shell_device, "\r\n", 2);
|
||||
}
|
||||
else if((buffer[index + ret - 1] == '\r') && (buffer[index + ret -2] != '\n')) {
|
||||
write(shell_object.shell_device, "\r\n", 2);
|
||||
}
|
||||
else {
|
||||
write(shell_object.shell_device, &buffer[index], ret); // echo
|
||||
}
|
||||
}
|
||||
if((buffer[index + ret - 1] == '\n') || (buffer[index + ret - 1] == '\r')) {
|
||||
buffer[index + ret - 1] = '\n';
|
||||
parse(buffer, index + ret);
|
||||
@@ -71,6 +77,13 @@ int shell_init(const struct driver *shell_device)
|
||||
return -1;
|
||||
list_init(&shell_object.command_list);
|
||||
shell_object.shell_device = shell_device;
|
||||
shell_object.echo_on = true;
|
||||
|
||||
shell_add_command(&cmd_kosmos_version);
|
||||
shell_add_command(&cmd_list_all_commands);
|
||||
shell_add_command(&cmd_echo_on);
|
||||
shell_add_command(&cmd_echo_off);
|
||||
|
||||
thread_create(&th_ctx, th_stack, TH_STACK_SIZE, rx_func, NULL, THREAD_PRIO_LOW);
|
||||
return 0;
|
||||
}
|
||||
|
91
source/firmware/kernel/shell_commands.c
Normal file
91
source/firmware/kernel/shell_commands.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* shell_commands.c
|
||||
*
|
||||
* Created on: Aug 11, 2016
|
||||
* Author: tkl
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "driver.h"
|
||||
#include "shell_data.h"
|
||||
#include "shell.h"
|
||||
#include "version.h"
|
||||
#include "shell_commands.h"
|
||||
|
||||
static void *cmd_kosmos_version_cb(const char *cmd);
|
||||
static void *cmd_list_all_commands_cb(const char *cmd);
|
||||
static void *cmd_echo_on_cb(const char *cmd);
|
||||
static void *cmd_echo_off_cb(const char *cmd);
|
||||
|
||||
struct command cmd_kosmos_version = {
|
||||
.command = "uname",
|
||||
.description = "Print current kosmos version.",
|
||||
.command_callback = cmd_kosmos_version_cb
|
||||
};
|
||||
|
||||
struct command cmd_list_all_commands = {
|
||||
.command = "ls",
|
||||
.description = "List all installed commands.",
|
||||
.command_callback = cmd_list_all_commands_cb
|
||||
};
|
||||
|
||||
struct command cmd_echo_on = {
|
||||
.command = "echo on",
|
||||
.description = "Switch echo on.",
|
||||
.command_callback = cmd_echo_on_cb
|
||||
};
|
||||
|
||||
struct command cmd_echo_off = {
|
||||
.command = "echo off",
|
||||
.description = "Switch echo off.",
|
||||
.command_callback = cmd_echo_off_cb
|
||||
};
|
||||
|
||||
|
||||
static void *cmd_kosmos_version_cb(const char *cmd)
|
||||
{
|
||||
char *greeter = "Kosmos Version: ";
|
||||
|
||||
write(shell_object.shell_device, greeter, strlen(greeter));
|
||||
write(shell_object.shell_device, KERNEL_VERSION, strlen(KERNEL_VERSION));
|
||||
write(shell_object.shell_device, ".", 1);
|
||||
write(shell_object.shell_device, MAJOR_VERSION, strlen(MAJOR_VERSION));
|
||||
write(shell_object.shell_device, ".", 1);
|
||||
write(shell_object.shell_device, MINOR_VERSION, strlen(MINOR_VERSION));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *cmd_list_all_commands_cb(const char *cmd)
|
||||
{
|
||||
char *greeter = "Command list: \r\n";
|
||||
struct list_node *it = shell_object.command_list.front;
|
||||
int i, len = list_get_len(&shell_object.command_list);
|
||||
|
||||
write(shell_object.shell_device, greeter, strlen(greeter));
|
||||
for(i = 0; i < (len - 1); i++) {
|
||||
if(NULL != it) {
|
||||
struct command *cmd = (struct command *)it->data;
|
||||
write(shell_object.shell_device, cmd->command, strlen(cmd->command));
|
||||
write(shell_object.shell_device, " - ", 3);
|
||||
write(shell_object.shell_device, cmd->description, strlen(cmd->description));
|
||||
write(shell_object.shell_device, "\r\n", 2);
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *cmd_echo_on_cb(const char *cmd)
|
||||
{
|
||||
shell_object.echo_on = true;
|
||||
return NULL;
|
||||
}
|
||||
static void *cmd_echo_off_cb(const char *cmd)
|
||||
{
|
||||
shell_object.echo_on = false;
|
||||
return NULL;
|
||||
}
|
Reference in New Issue
Block a user