Skip to content

Commit

Permalink
Merge pull request RIOT-OS#16061 from kaspar030/xfa_shell
Browse files Browse the repository at this point in the history
sys/shell: add XFA support
  • Loading branch information
kaspar030 authored Mar 9, 2021
2 parents d27ea02 + d319342 commit 010ba56
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
32 changes: 32 additions & 0 deletions sys/include/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "periph/pm.h"

#include "kernel_defines.h"
#include "xfa.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -196,6 +197,37 @@ static inline void shell_run(const shell_command_t *commands,
shell_run_forever(commands, line_buf, len);
}

/**
* @brief Define shell command
*
* This macro is a helper for defining a shell command and adding it to the
* shell commands XFA (cross file array).
*
* Shell commands added using this macros will be sorted *after* builtins and
* commands passed via parameter to `shell_run_once()`. If a command with the
* same name exists in any of those, they will make a command added via this
* macro inaccassible.
*
* Commands added with this macro will be sorted alphanumerically by `name`.
*
* @experimental This should be considered experimental API, subject to change
* without notice!
*
* Example:
*
* ```.c
* #include "shell.h"
* static int _my_command(int argc, char **argv) {
* // ...
* }
* SHELL_COMMAND(my_command, "my command help text", _my_command);
* ```
*/
#define SHELL_COMMAND(name, help, func) \
XFA_USE_CONST(shell_command_t*, shell_commands_xfa); \
static const shell_command_t _xfa_ ## name ## _cmd = { #name, help, &func }; \
XFA_ADD_PTR(shell_commands_xfa, name, name, &_xfa_ ## name ## _cmd)

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 32 additions & 0 deletions sys/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
#include <errno.h>

#include "kernel_defines.h"
#include "xfa.h"
#include "shell.h"
#include "shell_commands.h"

/* define shell command cross file array */
XFA_INIT_CONST(shell_command_t*, shell_commands_xfa);

#define ETX '\x03' /** ASCII "End-of-Text", or Ctrl-C */
#define EOT '\x04' /** ASCII "End-of-Transmission", or Ctrl-D */
#define BS '\x08' /** ASCII "Backspace" */
Expand Down Expand Up @@ -92,6 +96,19 @@ static shell_command_handler_t search_commands(const shell_command_t *entry,
return NULL;
}

static shell_command_handler_t search_commands_xfa(char *command)
{
unsigned n = XFA_LEN(shell_command_t*, shell_commands_xfa);

for (unsigned i = 0; i < n; i++) {
const volatile shell_command_t *entry = shell_commands_xfa[i];
if (strcmp(entry->name, command) == 0) {
return entry->handler;
}
}
return NULL;
}

static shell_command_handler_t find_handler(
const shell_command_t *command_list, char *command)
{
Expand All @@ -104,6 +121,10 @@ static shell_command_handler_t find_handler(
handler = search_commands(_builtin_cmds, command);
}

if (handler == NULL) {
handler = search_commands_xfa(command);
}

return handler;
}

Expand All @@ -114,6 +135,15 @@ static void print_commands(const shell_command_t *entry)
}
}

static void print_commands_xfa(void)
{
unsigned n = XFA_LEN(shell_command_t*, shell_commands_xfa);
for (unsigned i = 0; i < n; i++) {
const volatile shell_command_t *entry = shell_commands_xfa[i];
printf("%-20s %s\n", entry->name, entry->desc);
}
}

static void print_help(const shell_command_t *command_list)
{
puts("Command Description"
Expand All @@ -125,6 +155,8 @@ static void print_help(const shell_command_t *command_list)
if (_builtin_cmds != NULL) {
print_commands(_builtin_cmds);
}

print_commands_xfa();
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/periph_spi_dma/Makefile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# this test.
include ../periph_spi/Makefile.ci
BOARD_INSUFFICIENT_MEMORY += \
samd10-xmini \
#
44 changes: 33 additions & 11 deletions tests/shell/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ void shell_post_command_hook(int ret, int argc, char **argv)

static int print_teststart(int argc, char **argv)
{
(void) argc;
(void) argv;
(void)argc;
(void)argv;
printf("[TEST_START]\n");

return 0;
}

static int print_testend(int argc, char **argv)
{
(void) argc;
(void) argv;
(void)argc;
(void)argv;
printf("[TEST_END]\n");

return 0;
Expand All @@ -80,17 +80,17 @@ static int print_echo(int argc, char **argv)

static int print_shell_bufsize(int argc, char **argv)
{
(void) argc;
(void) argv;
(void)argc;
(void)argv;
printf("%d\n", SHELL_DEFAULT_BUFSIZE);

return 0;
}

static int print_empty(int argc, char **argv)
{
(void) argc;
(void) argv;
(void)argc;
(void)argv;
return 0;
}

Expand All @@ -103,6 +103,30 @@ static const shell_command_t shell_commands[] = {
{ NULL, NULL, NULL }
};

static int _xfa_test1(int argc, char **argv)
{
(void)argc;
(void)argv;
printf("[XFA TEST 1 OK]\n");

return 0;
}

static int _xfa_test2(int argc, char **argv)
{
(void)argc;
(void)argv;
printf("[XFA TEST 2 OK]\n");

return 0;
}

/* Add above commands to the shell commands XFA using helper macro.
* Intentionally reversed order to test linker script based alphanumeric
* ordering. */
SHELL_COMMAND(xfa_test2, "xfa test command 2", _xfa_test2);
SHELL_COMMAND(xfa_test1, "xfa test command 1", _xfa_test1);

int main(void)
{
printf("test_shell.\n");
Expand All @@ -120,9 +144,7 @@ int main(void)
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

/* or use only system shell commands */
/*
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
*/
/* shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE); */

return 0;
}
8 changes: 7 additions & 1 deletion tests/shell/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
'echo prints the input command',
'reboot Reboot the node',
'ps Prints information about running threads.',
'app_metadata Returns application metadata'
'app_metadata Returns application metadata',
'xfa_test1 xfa test command 1',
'xfa_test2 xfa test command 2'
)

EXPECTED_PS = (
Expand Down Expand Up @@ -96,6 +98,10 @@
('ps', EXPECTED_PS),
('help', EXPECTED_HELP),

# test commands added to shell_commands_xfa
('xfa_test1', '[XFA TEST 1 OK]'),
('xfa_test2', '[XFA TEST 2 OK]'),

# test reboot
('reboot', 'test_shell.'),

Expand Down

0 comments on commit 010ba56

Please sign in to comment.