The module also provides utilities that make it easy to add commands for users to reconfigure parameters of various types, obtain help on the available commands, and implement nested commands.
Below is a capture from an interactive session with the application. Where the sequence <TAB>
appears a tab character was typed. Where the sequence <UP>
appears the up-arrow on an ANSI terminal was pressed. See iBSP430cliConsoleBufferProcessInput() for a table showing command metacharacters.
#include <string.h>
#include <ctype.h>
struct data_t {
int ival;
unsigned int uival;
long lval;
unsigned long ulval;
} data;
void
dumpData (void)
{
cprintf(
"uival %u (0x%x)\n", data.uival, data.uival);
cprintf(
"ulval %lu (0x%lx)\n", data.ulval, data.ulval);
}
#define LAST_COMMAND NULL
static int
void * param,
const char * argstr,
size_t argstr_len)
{
if (0 == argstr_len) {
cputs(
"\nWTH are we fighting for? Walk!");
}
return 0;
}
static int
cmd_hup_two (const char * argstr)
{
return 0;
}
.handler = cmd_h234
};
.help = "# invalid if no four",
.child = &dcmd_hup_two_three_four,
};
.help = "# valid if no three",
.child = &dcmd_hup_two_three,
.param.simple_handler = cmd_hup_two
};
.child = &dcmd_hup_two,
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_hup
static int
cmd_uptime (const char * argstr)
{
return 0;
}
.help = "# Show system uptime",
.next = LAST_COMMAND,
.param.simple_handler = cmd_uptime
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_uptime
static int
void * param,
const char * argstr,
size_t argstr_len)
{
return 0;
}
static int
cmd_expand (const char * argstr)
{
}
.help = "[command...] # Show the expansion of the command without executing it",
.next = LAST_COMMAND,
.param.simple_handler = cmd_expand
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_expand
static int
cmd_show (const char * argstr)
{
dumpData();
return 0;
}
.help = "# Display the value of variables",
.next = LAST_COMMAND,
.param.simple_handler = cmd_show
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_show
static int
cmd_set_all (const char * argstr)
{
size_t argstr_len = strlen(argstr);
int rv;
if (0 == rv) {
}
if (0 == rv) {
}
if (0 == rv) {
}
return rv;
}
.help = "[ival] [ [uival] [ [lval] [ [ulval] ] ] ]",
.param.simple_handler = cmd_set_all
};
.help = "[signed 16-bit integer in octal, decimal, or hex]",
.next = &dcmd_set_all,
.param.ptr = &data.ival
};
.help = "[unsigned 16-bit integer in octal, decimal, or hex]",
.next = &dcmd_set_ival,
.param.ptr = &data.uival
};
.help = "[signed 32-bit integer in octal, decimal, or hex]",
.next = &dcmd_set_uival,
.param.ptr = &data.lval
};
.help = "[unsigned 32-bit integer in octal, decimal, or hex]",
.next = &dcmd_set_lval,
.param.ptr = &data.ulval
};
.child = &dcmd_set_ulval,
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_set
static int
cmd_quote (const char * argstr)
{
size_t arglen = strlen(argstr);
size_t len;
cprintf(
"Extracting text tokens from %u characters\n", (
unsigned int)arglen);
while (0 < arglen) {
cprintf(
"%u-char token <", (
unsigned int)len);
while (len--) {
}
}
return 0;
}
.help = "[qstr]... # Extract one or more quoted tokens",
.next = LAST_COMMAND,
.param.simple_handler = cmd_quote
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_quote
static int
cmd_dummy (const char * argstr)
{
return 0;
}
#undef LAST_SUBCOMMAND
#define LAST_SUBCOMMAND NULL
.next = LAST_SUBCOMMAND,
.param.simple_handler = cmd_dummy
};
#undef LAST_SUBCOMMAND
#define LAST_SUBCOMMAND &dcmd_complete_common
.next = LAST_SUBCOMMAND,
.param.simple_handler = cmd_dummy
};
#undef LAST_SUBCOMMAND
#define LAST_SUBCOMMAND &dcmd_complete_component
.next = LAST_COMMAND,
.child = LAST_SUBCOMMAND
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_complete
static const char * const numbers[] = {
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten"
};
static const size_t number_len = sizeof(numbers)/sizeof(*numbers);
#if (configBSP430_CLI_COMMAND_COMPLETION_HELPER - 0)
.strings = numbers,
.len = sizeof(numbers)/sizeof(*numbers)
};
#endif
static int
void * param,
const char * command,
size_t command_len)
{
const char * const * np = &command;
int nmatches = 0;
while (NULL != np) {
size_t ocl = command_len;
if (NULL != np) {
++nmatches;
cprintf(
"Match %s for %u consumed position %u\n", *np,
(unsigned int)(ocl - command_len),
(unsigned int)(np - numbers));
}
}
cprintf(
"%u matches found\n", nmatches);
return 0;
}
.help = "{word}... # Customized completion demonstration",
#if (configBSP430_CLI_COMMAND_COMPLETION_HELPER - 0)
#endif
.next = LAST_COMMAND,
.handler = cmd_say,
};
#undef LAST_COMMAND
#define LAST_COMMAND (&dcmd_say)
#ifndef RESPONSIVE_CCIDX
#define RESPONSIVE_CCIDX 1
#endif
typedef struct sResponsive {
volatile unsigned long wakeups;
unsigned long interval_tck;
volatile unsigned long max_late;
unsigned long lastDump_tck;
} sResponsive;
static sResponsive responsiveData;
static int
{
sResponsive * rdp = (sResponsive *)alarm;
unsigned long setting_tck;
int rc;
if (0 == rdp->wakeups) {
rdp->max_late = late_tck;
} else if (late_tck > rdp->max_late) {
rdp->max_late = late_tck;
}
rdp->wakeups++;
setting_tck += (late_tck / rdp->interval_tck) * rdp->interval_tck;
do {
setting_tck += rdp->interval_tck;
} while (0 < rc);
return 0;
}
static int
cmd_responsive (const char * argstr)
{
sResponsive state;
unsigned long resp_Hz;
unsigned long now_utt;
do {
state = responsiveData;
responsiveData.wakeups = 0;
responsiveData.lastDump_tck = now_utt;
} while (0);
cprintf(
"State: %lu tick span with %lu wakeups\n"
"\twakeup interval %lu tick (%lu us)\n"
"\tlate maximum %lu tick (%lu us)\n",
(now_utt - state.lastDump_tck), state.wakeups,
return 0;
}
.help = "# Display responsiveness data",
.next = LAST_COMMAND,
.param.simple_handler = cmd_responsive
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_responsive
static int
void * param,
const char * command,
size_t command_len)
{
return 0;
}
.help = "[cmd] # Show help on cmd or all commands",
.next = LAST_COMMAND,
.handler = cmd_help
};
#undef LAST_COMMAND
#define LAST_COMMAND &dcmd_help
void main ()
{
const char * command;
int flags;
cprintf(
"\ncli example " __DATE__
" " __TIME__
"\n");
#if (configBSP430_CLI_COMMAND_COMPLETION - 0)
cprintf(
"Command completion is available.\n");
#endif
#if (BSP430_PMM_SUPPORTS_SVSM - 0)
#endif // BSP430_PMM_SUPPORTS_SVSM
RESPONSIVE_CCIDX,
responsive_cb_ni);
if (NULL == rh) {
cprintf(
"Failed to initialize responsiveness alarm\n");
} else {
int rc;
responsiveData.wakeups = 0;
responsiveData.interval_tck = 10;
responsiveData.max_late = 0;
if (0 == rc) {
}
if (0 != rc) {
cprintf(
"Failed to initialize alarm\n");
}
}
cprintf(
"\nLED lit when not awaiting input\n");
commandSet = LAST_COMMAND;
command = NULL;
while (1) {
int c;
cprintf(
"escape char 0x%02x (%u) '%c'\n", c, c, isprint(c) ? c :
'.');
#define KEY_CSI '['
flags &= ~eBSP430cliConsole_PROCESS_ESCAPE;
} else if ((64 <= c) && (c <= 126)) {
flags &= ~eBSP430cliConsole_ANY_ESCAPE;
break;
}
}
}
if (flags & eBSP430cliConsole_DO_COMPLETION) {
flags &= ~eBSP430cliConsole_DO_COMPLETION;
}
if (flags & eBSP430cliConsole_READY) {
int rv;
if (0 != rv) {
cprintf(
"Command execution returned %d\n", rv);
}
command = NULL;
}
if (flags & eBSP430cliConsole_REPAINT) {
cprintf(
"\r> %s", command ? command :
"");
flags &= ~eBSP430cliConsole_REPAINT;
}
if (flags & eBSP430cliConsole_REPAINT_BEL) {
flags &= ~eBSP430cliConsole_REPAINT_BEL;
}
if (flags & eBSP430cliConsole_READY) {
flags &= ~eBSP430cliConsole_READY;
}
do {
if (! (flags & eBSP430cliConsole_ANY_ESCAPE)) {
}
if (0 == flags) {
}
} while (! flags);
}
}