Loading...
Searching...
No Matches
shell.c
1/***********************************************************************************
2 * @file shell.c *
3 * @author Matt Ricci *
4 * @addtogroup Shell *
5 * *
6 * @todo Add commands to buffer to allow managing shell history for frontend *
7 ***********************************************************************************/
8
9#include "shell.h"
10#include "devices.h"
11#include "drivers.h"
12
13
14/* =============================================================================== */
25void vShellExec(void *argument) {
26 ShellTaskParams params = *(ShellTaskParams *)argument;
27
28 for (;;) {
29 params.shell->run(params.shell, params.str);
30 vTaskDelete(NULL);
31 }
32}
33
34/* =============================================================================== */
45int Shell_init(Shell *shell) {
46 shell->usb = *(UART *)DeviceHandle_getHandle("USB").device;
47 shell->help = Shell_help;
48 shell->run = Shell_run;
49 shell->runTask = Shell_runTask;
50 shell->clear = Shell_clear;
51
52 // Return error code if shell vector exceeds max allowable programs
53 if (((&__shell_vector_end - &__shell_vector_start) / sizeof(uint32_t)) > SHELL_MAX_PROGRAMS)
54 return 1;
55
56 // Register programs in vector to shell
57 int idx = 0;
58 for (uint32_t *i = (uint32_t *)&__shell_vector_start; i < (uint32_t *)&__shell_vector_end; i++) {
59 // Dereference function pointer
60 ShellProgramHandle_t (*registerShellProgram)() = (ShellProgramHandle_t (*)())(*i);
61
62 // Register program handle to shell
63 shell->programHandles[idx] = registerShellProgram();
64 idx++;
65 }
66
67 return 0;
68}
69
70/* =============================================================================== */
80void Shell_help(Shell *shell) {
81 shell->usb.print(&shell->usb, "Use `help [name]` for more information on a specific command\n\r");
82 shell->usb.print(&shell->usb, "The following commands are currently available:\n\r");
83 for(int i = 0; i < SHELL_MAX_PROGRAMS; i++) {
84 if(strcmp(shell->programHandles[i].name, "")) {
85 shell->usb.print(&shell->usb, ":");
86 shell->usb.print(&shell->usb, shell->programHandles[i].name);
87 shell->usb.print(&shell->usb, "\n\r");
88 }
89 }
90}
91
92/* =============================================================================== */
104void Shell_run(Shell *shell, uint8_t *programName) {
105 char *token = strtok((char *)programName, " ");
106 char *flags = strchr(token, '\0') + 1;
107
108 // Iterate through shell vector and execute function from handle
109 // with matching name (if any).
110 for(int i = 0; i < SHELL_MAX_PROGRAMS; i++) {
111 if (!strcmp(shell->programHandles[i].name, programName)) {
112 shell->programHandles[i].exec(shell, flags);
113 return; // Early exit if program is found
114 }
115 }
116
117 // Print help string if no matching command is found
118 shell->usb.print(&shell->usb, (char *)programName);
119 shell->usb.print(&shell->usb, ": command not recognized. Run `help` for a list of available commands\n\r");
120}
121
122/* =============================================================================== */
134void Shell_runTask(Shell *shell, uint8_t *str) {
135
136 static ShellTaskParams params;
137 params.shell = shell;
138 params.str = str;
139
140 // Create new task to parse and execute shell program
141 xTaskCreate(
142 vShellExec,
143 "ShellProgram",
144 256,
145 (void *)&params,
146 configMAX_PRIORITIES - 6,
147 &shell->taskHandle
148 );
149}
150
151/* =============================================================================== */
159bool Shell_clear(Shell *shell) {
160 shell->usb.sendBytes(&shell->usb, (uint8_t *)"\033[3J\033[H\033[2J", 11);
161 // Delete any running task
162 if (shell->taskHandle != NULL)
163 vTaskDelete(shell->taskHandle);
164 return true;
165}
void(* print)(struct UART *, char *)
UART print string method.
Definition uart.h:62
void(* sendBytes)(struct UART *, uint8_t *, int)
UART send multiple bytes method.
Definition uart.h:61
Struct definition for UART interface.
Definition uart.h:53
int Shell_init(Shell *)
Initializes the shell, registering programs from shell vector.
Definition shell.c:45
void Shell_help(Shell *)
Displays available shell commands.
Definition shell.c:80
bool Shell_clear(Shell *)
Send clear sequence to host terminal.
Definition shell.c:159
void Shell_runTask(Shell *, uint8_t *)
Creates a task to run a shell program.
Definition shell.c:134
void Shell_run(Shell *, uint8_t *)
Executes a shell program by name.
Definition shell.c:104
Definition shell.h:34