From 3eaa1beb565bc65d2609a69dc2fb8529b2b88222 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 22 Feb 2021 12:36:39 +0100 Subject: [PATCH 1/4] sys/shell: initial XFA support --- sys/include/shell.h | 32 ++++++++++++++++++++++++++++++++ sys/shell/shell.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/sys/include/shell.h b/sys/include/shell.h index ee2cc882f680..9854a821845d 100644 --- a/sys/include/shell.h +++ b/sys/include/shell.h @@ -24,6 +24,7 @@ #include "periph/pm.h" #include "kernel_defines.h" +#include "xfa.h" #ifdef __cplusplus extern "C" { @@ -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 diff --git a/sys/shell/shell.c b/sys/shell/shell.c index bae4cda24fbf..45bbcc2784bf 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -36,9 +36,13 @@ #include #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" */ @@ -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) { @@ -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; } @@ -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" @@ -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(); } /** From 5975fabd9f835e6210cc83990fa17439ce72d2f8 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 22 Feb 2021 12:37:04 +0100 Subject: [PATCH 2/4] tests/shell: add tests for XFA shell commands --- tests/shell/main.c | 24 ++++++++++++++++++++++++ tests/shell/tests/01-run.py | 8 +++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/shell/main.c b/tests/shell/main.c index 376b6e700804..58924fd6a3a2 100644 --- a/tests/shell/main.c +++ b/tests/shell/main.c @@ -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"); diff --git a/tests/shell/tests/01-run.py b/tests/shell/tests/01-run.py index 967eda74ea18..a4b423ad8db1 100755 --- a/tests/shell/tests/01-run.py +++ b/tests/shell/tests/01-run.py @@ -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 = ( @@ -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.'), From f454cb7a2403459ee9c6c58432d2e693bb1a52f5 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Tue, 23 Feb 2021 11:07:34 +0100 Subject: [PATCH 3/4] tests/shell: uncrustify main.c --- tests/shell/main.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tests/shell/main.c b/tests/shell/main.c index 58924fd6a3a2..4556873f2af5 100644 --- a/tests/shell/main.c +++ b/tests/shell/main.c @@ -52,8 +52,8 @@ 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; @@ -61,8 +61,8 @@ static int print_teststart(int argc, char **argv) static int print_testend(int argc, char **argv) { - (void) argc; - (void) argv; + (void)argc; + (void)argv; printf("[TEST_END]\n"); return 0; @@ -80,8 +80,8 @@ 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; @@ -89,8 +89,8 @@ static int print_shell_bufsize(int argc, char **argv) static int print_empty(int argc, char **argv) { - (void) argc; - (void) argv; + (void)argc; + (void)argv; return 0; } @@ -105,8 +105,8 @@ static const shell_command_t shell_commands[] = { static int _xfa_test1(int argc, char **argv) { - (void) argc; - (void) argv; + (void)argc; + (void)argv; printf("[XFA TEST 1 OK]\n"); return 0; @@ -114,8 +114,8 @@ static int _xfa_test1(int argc, char **argv) static int _xfa_test2(int argc, char **argv) { - (void) argc; - (void) argv; + (void)argc; + (void)argv; printf("[XFA TEST 2 OK]\n"); return 0; @@ -124,8 +124,8 @@ static int _xfa_test2(int argc, char **argv) /* 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); +SHELL_COMMAND(xfa_test2, "xfa test command 2", _xfa_test2); +SHELL_COMMAND(xfa_test1, "xfa test command 1", _xfa_test1); int main(void) { @@ -144,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; } From d31934223c062a1c6a1fa11c6b6dc9511cf25605 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 25 Feb 2021 11:51:36 +0100 Subject: [PATCH 4/4] tests/periph_spi_dma: blacklist samd10-xmini --- tests/periph_spi_dma/Makefile.ci | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/periph_spi_dma/Makefile.ci b/tests/periph_spi_dma/Makefile.ci index 9a9f98fad960..d0a370493e36 100644 --- a/tests/periph_spi_dma/Makefile.ci +++ b/tests/periph_spi_dma/Makefile.ci @@ -2,4 +2,5 @@ # this test. include ../periph_spi/Makefile.ci BOARD_INSUFFICIENT_MEMORY += \ + samd10-xmini \ #