From f78eee4550dc747b558c1dfc333ba9b629216f39 Mon Sep 17 00:00:00 2001 From: Keith Short Date: Tue, 10 Aug 2021 09:28:53 -0600 Subject: zephyr: reduce code duplication of console shim Change the console command shim to pass in a struct, reducing the amount of duplicated code. This change saves 500 bytes on Volteer BUG=none BRANCH=none TEST=zmake testall TEST=verify console on Volteer Signed-off-by: Keith Short Change-Id: I2da374b9a3b3b78a3e7b66d5c31f4ed2131aef01 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3093491 Reviewed-by: Jack Rosenthal Commit-Queue: Jack Rosenthal --- zephyr/shim/include/zephyr_console_shim.h | 51 ++++++++++++++++++------------- zephyr/shim/src/console.c | 21 ++++++------- 2 files changed, 39 insertions(+), 33 deletions(-) (limited to 'zephyr/shim') diff --git a/zephyr/shim/include/zephyr_console_shim.h b/zephyr/shim/include/zephyr_console_shim.h index df9f189c9a..a45d65659b 100644 --- a/zephyr/shim/include/zephyr_console_shim.h +++ b/zephyr/shim/include/zephyr_console_shim.h @@ -8,39 +8,48 @@ #include +struct zephyr_console_command { + /* Handler for the command. argv[0] will be the command name. */ + int (*handler)(int argc, char **argv); + /* Description of args */ + const char *argdesc; + /* Short help for command */ + const char *help; +}; + /** * zshim_run_ec_console_command() - Dispatch a CrOS EC console command * using Zephyr's shell * - * @handler: A CrOS EC shell command handler. - * @shell: The Zephyr shell to run on. + * @command: Pointer to a struct zephyr_console_command * @argc: The number of command line arguments. * @argv: The NULL-terminated list of arguments. - * @help_str: The help string to display when "-h" is passed. - * @argdesc: The string describing the arguments to the command. * * Return: the return value from the handler. */ -int zshim_run_ec_console_command(int (*handler)(int argc, char **argv), - const struct shell *shell, size_t argc, - char **argv, const char *help_str, - const char *argdesc); +int zshim_run_ec_console_command(const struct zephyr_console_command *command, + size_t argc, char **argv); /* Internal wrappers for DECLARE_CONSOLE_COMMAND_* macros. */ -#define _ZEPHYR_SHELL_COMMAND_SHIM_2(NAME, ROUTINE_ID, ARGDESC, HELP, \ - WRAPPER_ID) \ - static int WRAPPER_ID(const struct shell *shell, size_t argc, \ - char **argv) \ - { \ - return zshim_run_ec_console_command(ROUTINE_ID, shell, argc, \ - argv, HELP, ARGDESC); \ - } \ - SHELL_CMD_ARG_REGISTER(NAME, NULL, HELP, WRAPPER_ID, 0, \ +#define _ZEPHYR_SHELL_COMMAND_SHIM_2(NAME, ROUTINE_ID, ARGDESC, HELP, \ + WRAPPER_ID, ENTRY_ID) \ + static const struct zephyr_console_command ENTRY_ID = { \ + .handler = ROUTINE_ID, \ + .argdesc = ARGDESC, \ + .help = HELP, \ + }; \ + static int WRAPPER_ID(const struct shell *shell, size_t argc, \ + char **argv) \ + { \ + return zshim_run_ec_console_command(&ENTRY_ID, argc, argv); \ + } \ + SHELL_CMD_ARG_REGISTER(NAME, NULL, HELP, WRAPPER_ID, 0, \ SHELL_OPT_ARG_MAX) -#define _ZEPHYR_SHELL_COMMAND_SHIM(NAME, ROUTINE_ID, ARGDESC, HELP) \ - _ZEPHYR_SHELL_COMMAND_SHIM_2(NAME, ROUTINE_ID, ARGDESC, HELP, \ - UTIL_CAT(zshim_wrapper_, ROUTINE_ID)) +#define _ZEPHYR_SHELL_COMMAND_SHIM(NAME, ROUTINE_ID, ARGDESC, HELP) \ + _ZEPHYR_SHELL_COMMAND_SHIM_2(NAME, ROUTINE_ID, ARGDESC, HELP, \ + UTIL_CAT(zshim_wrapper_, ROUTINE_ID), \ + UTIL_CAT(zshim_entry_, ROUTINE_ID)) /* These macros mirror the macros provided by the CrOS EC. */ #define DECLARE_CONSOLE_COMMAND(NAME, ROUTINE, ARGDESC, HELP) \ @@ -64,4 +73,4 @@ int zshim_run_ec_console_command(int (*handler)(int argc, char **argv), */ void console_buf_notify_chars(const char *s, size_t len); -#endif /* __CROS_EC_ZEPHYR_CONSOLE_SHIM_H */ +#endif /* __CROS_EC_ZEPHYR_CONSOLE_SHIM_H */ diff --git a/zephyr/shim/src/console.c b/zephyr/shim/src/console.c index 17797fb991..929f7a9d1c 100644 --- a/zephyr/shim/src/console.c +++ b/zephyr/shim/src/console.c @@ -18,6 +18,7 @@ #include "printf.h" #include "uart.h" #include "usb_console.h" +#include "zephyr_console_shim.h" LOG_MODULE_REGISTER(shim_console, LOG_LEVEL_ERR); @@ -160,26 +161,22 @@ void uart_shell_start(void) k_poll(&event, 1, K_FOREVER); } -int zshim_run_ec_console_command(int (*handler)(int argc, char **argv), - const struct shell *shell, size_t argc, - char **argv, const char *help_str, - const char *argdesc) +int zshim_run_ec_console_command(const struct zephyr_console_command *command, + size_t argc, char **argv) { - ARG_UNUSED(shell); - for (int i = 1; i < argc; i++) { - if (!help_str && !argdesc) + if (!command->help && !command->argdesc) break; if (!strcmp(argv[i], "-h")) { - if (help_str) - printk("%s\n", help_str); - if (argdesc) - printk("Usage: %s\n", argdesc); + if (command->help) + printk("%s\n", command->help); + if (command->argdesc) + printk("Usage: %s\n", command->argdesc); return 0; } } - return handler(argc, argv); + return command->handler(argc, argv); } #if defined(CONFIG_CONSOLE_CHANNEL) && DT_NODE_EXISTS(DT_PATH(ec_console)) -- cgit v1.2.1