2009-12-10 15:44:55 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2012-01-21 14:29:42 +00:00
|
|
|
2011,2012 Giovanni Di Sirio.
|
2009-12-10 15:44:55 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-06-28 06:29:04 +00:00
|
|
|
* @file shell.c
|
|
|
|
* @brief Simple CLI shell code.
|
|
|
|
*
|
2009-12-10 15:44:55 +00:00
|
|
|
* @addtogroup SHELL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ch.h"
|
2010-02-22 16:29:48 +00:00
|
|
|
#include "hal.h"
|
2009-12-10 15:44:55 +00:00
|
|
|
#include "shell.h"
|
2011-06-28 06:29:04 +00:00
|
|
|
#include "chprintf.h"
|
2009-12-10 15:44:55 +00:00
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Shell termination event source.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
EventSource shell_terminated;
|
|
|
|
|
2011-12-18 08:27:30 +00:00
|
|
|
static char *_strtok(char *str, const char *delim, char **saveptr) {
|
2009-12-10 15:44:55 +00:00
|
|
|
char *token;
|
|
|
|
if (str)
|
|
|
|
*saveptr = str;
|
|
|
|
token = *saveptr;
|
|
|
|
|
|
|
|
if (!token)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
token += strspn(token, delim);
|
|
|
|
*saveptr = strpbrk(token, delim);
|
|
|
|
if (*saveptr)
|
|
|
|
*(*saveptr)++ = '\0';
|
|
|
|
|
|
|
|
return *token ? token : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(BaseChannel *chp, char *p) {
|
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Usage: %s\r\n", p);
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void list_commands(BaseChannel *chp, const ShellCommand *scp) {
|
|
|
|
|
|
|
|
while (scp->sc_name != NULL) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "%s ", scp->sc_name);
|
2009-12-10 15:44:55 +00:00
|
|
|
scp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_info(BaseChannel *chp, int argc, char *argv[]) {
|
|
|
|
|
|
|
|
(void)argv;
|
|
|
|
if (argc > 0) {
|
|
|
|
usage(chp, "info");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Kernel: %s\r\n", CH_KERNEL_VERSION);
|
|
|
|
#ifdef CH_COMPILER_NAME
|
|
|
|
chprintf(chp, "Compiler: %s\r\n", CH_COMPILER_NAME);
|
2010-02-22 16:29:48 +00:00
|
|
|
#endif
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Architecture: %s\r\n", CH_ARCHITECTURE_NAME);
|
2010-02-22 16:29:48 +00:00
|
|
|
#ifdef CH_CORE_VARIANT_NAME
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Core Variant: %s\r\n", CH_CORE_VARIANT_NAME);
|
|
|
|
#endif
|
|
|
|
#ifdef CH_PORT_INFO
|
|
|
|
chprintf(chp, "Port Info: %s\r\n", CH_PORT_INFO);
|
2010-02-22 16:29:48 +00:00
|
|
|
#endif
|
|
|
|
#ifdef PLATFORM_NAME
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Platform: %s\r\n", PLATFORM_NAME);
|
2010-02-22 16:29:48 +00:00
|
|
|
#endif
|
|
|
|
#ifdef BOARD_NAME
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "Board: %s\r\n", BOARD_NAME);
|
2009-12-10 15:44:55 +00:00
|
|
|
#endif
|
2011-09-06 14:06:43 +00:00
|
|
|
#ifdef __DATE__
|
|
|
|
#ifdef __TIME__
|
|
|
|
chprintf(chp, "Build time: %s%s%s\r\n", __DATE__, " - ", __TIME__);
|
|
|
|
#endif
|
|
|
|
#endif
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_systime(BaseChannel *chp, int argc, char *argv[]) {
|
|
|
|
|
|
|
|
(void)argv;
|
|
|
|
if (argc > 0) {
|
|
|
|
usage(chp, "systime");
|
|
|
|
return;
|
|
|
|
}
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "%lu\r\n", (unsigned long)chTimeNow());
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Array of the default commands.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
static ShellCommand local_commands[] = {
|
|
|
|
{"info", cmd_info},
|
|
|
|
{"systime", cmd_systime},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool_t cmdexec(const ShellCommand *scp, BaseChannel *chp,
|
|
|
|
char *name, int argc, char *argv[]) {
|
|
|
|
|
|
|
|
while (scp->sc_name != NULL) {
|
2009-12-23 09:40:59 +00:00
|
|
|
if (strcasecmp(scp->sc_name, name) == 0) {
|
2009-12-10 15:44:55 +00:00
|
|
|
scp->sc_function(chp, argc, argv);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
scp++;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Shell thread function.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2011-09-23 15:48:55 +00:00
|
|
|
* @param[in] p pointer to a @p BaseChannel object
|
|
|
|
* @return Termination reason.
|
|
|
|
* @retval RDY_OK terminated by command.
|
|
|
|
* @retval RDY_RESET terminated by reset condition on the I/O channel.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
static msg_t shell_thread(void *p) {
|
|
|
|
int n;
|
|
|
|
msg_t msg = RDY_OK;
|
|
|
|
BaseChannel *chp = ((ShellConfig *)p)->sc_channel;
|
|
|
|
const ShellCommand *scp = ((ShellConfig *)p)->sc_commands;
|
|
|
|
char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH];
|
|
|
|
char *args[SHELL_MAX_ARGUMENTS + 1];
|
|
|
|
|
2011-07-09 07:32:27 +00:00
|
|
|
chRegSetThreadName("shell");
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\nChibiOS/RT Shell\r\n");
|
2009-12-10 15:44:55 +00:00
|
|
|
while (TRUE) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "ch> ");
|
2009-12-10 15:44:55 +00:00
|
|
|
if (shellGetLine(chp, line, sizeof(line))) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\nlogout");
|
2009-12-10 15:44:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-18 08:27:30 +00:00
|
|
|
lp = _strtok(line, " \009", &tokp);
|
2009-12-10 15:44:55 +00:00
|
|
|
cmd = lp;
|
|
|
|
n = 0;
|
2011-12-18 08:27:30 +00:00
|
|
|
while ((lp = _strtok(NULL, " \009", &tokp)) != NULL) {
|
2009-12-10 15:44:55 +00:00
|
|
|
if (n >= SHELL_MAX_ARGUMENTS) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "too many arguments\r\n");
|
2009-12-10 15:44:55 +00:00
|
|
|
cmd = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
args[n++] = lp;
|
|
|
|
}
|
|
|
|
args[n] = NULL;
|
|
|
|
if (cmd != NULL) {
|
2009-12-23 09:40:59 +00:00
|
|
|
if (strcasecmp(cmd, "exit") == 0) {
|
2011-06-28 06:29:04 +00:00
|
|
|
if (n > 0) {
|
2009-12-10 15:44:55 +00:00
|
|
|
usage(chp, "exit");
|
2011-06-28 06:29:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-10 15:44:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-12-23 09:40:59 +00:00
|
|
|
else if (strcasecmp(cmd, "help") == 0) {
|
2011-06-28 06:29:04 +00:00
|
|
|
if (n > 0) {
|
2009-12-10 15:44:55 +00:00
|
|
|
usage(chp, "help");
|
2011-06-28 06:29:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
chprintf(chp, "Commands: help exit ");
|
2009-12-10 15:44:55 +00:00
|
|
|
list_commands(chp, local_commands);
|
|
|
|
if (scp != NULL)
|
|
|
|
list_commands(chp, scp);
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\n");
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
else if (cmdexec(local_commands, chp, cmd, n, args) &&
|
|
|
|
((scp == NULL) || cmdexec(scp, chp, cmd, n, args))) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "%s", cmd);
|
|
|
|
chprintf(chp, " ?\r\n");
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-20 17:08:22 +00:00
|
|
|
/* Atomically broadcasting the event source and terminating the thread,
|
|
|
|
there is not a chSysUnlock() because the thread terminates upon return.*/
|
2009-12-10 15:44:55 +00:00
|
|
|
chSysLock();
|
|
|
|
chEvtBroadcastI(&shell_terminated);
|
2011-10-23 12:21:44 +00:00
|
|
|
chThdExitS(msg);
|
2011-11-03 18:07:58 +00:00
|
|
|
return 0; /* Never executed.*/
|
2009-12-10 15:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Shell manager initialization.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
void shellInit(void) {
|
|
|
|
|
|
|
|
chEvtInit(&shell_terminated);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Spawns a new shell.
|
|
|
|
* @pre @p CH_USE_MALLOC_HEAP and @p CH_USE_DYNAMIC must be enabled.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2011-09-23 15:48:55 +00:00
|
|
|
* @param[in] scp pointer to a @p ShellConfig object
|
|
|
|
* @param[in] size size of the shell working area to be allocated
|
|
|
|
* @param[in] prio priority level for the new shell
|
|
|
|
* @return A pointer to the shell thread.
|
|
|
|
* @retval NULL thread creation failed because memory allocation.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
2011-09-17 21:06:25 +00:00
|
|
|
#if CH_USE_HEAP && CH_USE_DYNAMIC
|
2009-12-10 15:44:55 +00:00
|
|
|
Thread *shellCreate(const ShellConfig *scp, size_t size, tprio_t prio) {
|
|
|
|
|
|
|
|
return chThdCreateFromHeap(NULL, size, prio, shell_thread, (void *)scp);
|
|
|
|
}
|
2011-09-17 21:06:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Create statically allocated shell thread.
|
2011-09-17 21:06:25 +00:00
|
|
|
*
|
2011-09-23 15:48:55 +00:00
|
|
|
* @param[in] scp pointer to a @p ShellConfig object
|
|
|
|
* @param[in] wsp pointer to a working area dedicated to the shell thread stack
|
|
|
|
* @param[in] size size of the shell working area
|
|
|
|
* @param[in] prio priority level for the new shell
|
|
|
|
* @return A pointer to the shell thread.
|
2011-09-17 21:06:25 +00:00
|
|
|
*/
|
|
|
|
Thread *shellCreateStatic(const ShellConfig *scp, void *wsp,
|
|
|
|
size_t size, tprio_t prio) {
|
|
|
|
|
|
|
|
return chThdCreateStatic(wsp, size, prio, shell_thread, (void *)scp);
|
|
|
|
}
|
|
|
|
|
2009-12-10 15:44:55 +00:00
|
|
|
/**
|
2011-09-23 15:48:55 +00:00
|
|
|
* @brief Reads a whole line from the input channel.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2011-09-23 15:48:55 +00:00
|
|
|
* @param[in] chp pointer to a @p BaseChannel object
|
|
|
|
* @param[in] line pointer to the line buffer
|
|
|
|
* @param[in] size buffer maximum length
|
|
|
|
* @return The operation status.
|
|
|
|
* @retval TRUE the channel was reset or CTRL-D pressed.
|
|
|
|
* @retval FALSE operation successful.
|
2009-12-10 15:44:55 +00:00
|
|
|
*/
|
|
|
|
bool_t shellGetLine(BaseChannel *chp, char *line, unsigned size) {
|
|
|
|
char *p = line;
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
short c = (short)chIOGet(chp);
|
|
|
|
if (c < 0)
|
|
|
|
return TRUE;
|
|
|
|
if (c == 4) {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "^D");
|
2009-12-10 15:44:55 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (c == 8) {
|
|
|
|
if (p != line) {
|
|
|
|
chIOPut(chp, (uint8_t)c);
|
|
|
|
chIOPut(chp, 0x20);
|
|
|
|
chIOPut(chp, (uint8_t)c);
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
2011-06-28 06:29:04 +00:00
|
|
|
chprintf(chp, "\r\n");
|
2009-12-10 15:44:55 +00:00
|
|
|
*p = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (c < 0x20)
|
|
|
|
continue;
|
|
|
|
if (p < line + size - 1) {
|
|
|
|
chIOPut(chp, (uint8_t)c);
|
|
|
|
*p++ = (char)c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|