diff --git a/include/shell/shell.h b/include/shell/shell.h index a1ea0c1d56f..c334adb4024 100644 --- a/include/shell/shell.h +++ b/include/shell/shell.h @@ -837,6 +837,19 @@ void shell_help(const struct shell *shell); */ int shell_execute_cmd(const struct shell *shell, const char *cmd); +/** @brief Set root command for all shell instances. + * + * It allows setting from the code the root command. It is an equivalent of + * calling select command with one of the root commands as the argument + * (e.g "select log") except it sets command for all shell instances. + * + * @param cmd String with one of the root commands or null pointer to reset. + * + * @retval 0 if root command is set. + * @retval -EINVAL if invalid root command is provided. + */ +int shell_set_root_cmd(const char *cmd); + /** * @} */ diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index f8183819839..227bb48eee9 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -638,8 +638,7 @@ static int execute(const struct shell *shell) SHELL_MSG_CMD_NOT_FOUND); return -ENOEXEC; } - if (IS_ENABLED(CONFIG_SHELL_CMDS_SELECT) && - shell_in_select_mode(shell) && + if (shell_in_select_mode(shell) && shell->ctx->selected_cmd->handler != NULL) { p_static_entry = shell->ctx->selected_cmd; shell->ctx->active_cmd = *p_static_entry; diff --git a/subsys/shell/shell_utils.c b/subsys/shell/shell_utils.c index 9751f37d5b6..b53231d5a9e 100644 --- a/subsys/shell/shell_utils.c +++ b/subsys/shell/shell_utils.c @@ -252,8 +252,7 @@ void shell_cmd_get(const struct shell *shell, *entry = NULL; if (lvl == SHELL_CMD_ROOT_LVL) { - if (shell_in_select_mode(shell) && - IS_ENABLED(CONFIG_SHELL_CMDS_SELECT)) { + if (shell_in_select_mode(shell)) { const struct shell_static_entry *ptr = shell->ctx->selected_cmd; if (ptr->subcmd->u.entry[idx].syntax != NULL) { @@ -362,6 +361,23 @@ const struct shell_static_entry *shell_get_last_command( return entry; } +int shell_set_root_cmd(const char *cmd) +{ + const struct shell_static_entry *entry; + + entry = cmd ? shell_root_cmd_find(cmd) : NULL; + + if (cmd && (entry == NULL)) { + return -EINVAL; + } + + Z_STRUCT_SECTION_FOREACH(shell, sh) { + sh->ctx->selected_cmd = entry; + } + + return 0; +} + int shell_command_add(char *buff, u16_t *buff_len, const char *new_cmd, const char *pattern) { diff --git a/tests/shell/src/main.c b/tests/shell/src/main.c index 9cf5c65dd8a..a2152f284e7 100644 --- a/tests/shell/src/main.c +++ b/tests/shell/src/main.c @@ -295,6 +295,27 @@ static void test_cmd_select(void) test_shell_execute_cmd("on", -ENOEXEC); } +static void test_set_root_cmd(void) +{ + int err; + + test_shell_execute_cmd("shell colors on", 0); + err = shell_set_root_cmd("__shell__"); + zassert_equal(err, -EINVAL, "Unexpected error %d", err); + + err = shell_set_root_cmd("shell"); + zassert_equal(err, 0, "Unexpected error %d", err); + + test_shell_execute_cmd("shell colors", -ENOEXEC); + test_shell_execute_cmd("colors on", 0); + + err = shell_set_root_cmd(NULL); + zassert_equal(err, 0, "Unexpected error %d", err); + + test_shell_execute_cmd("colors", -ENOEXEC); + test_shell_execute_cmd("shell colors on", 0); +} + void test_main(void) { ztest_test_suite(shell_test_suite, @@ -303,6 +324,7 @@ void test_main(void) ztest_unit_test(test_cmd_shell), ztest_unit_test(test_cmd_history), ztest_unit_test(test_cmd_select), + ztest_unit_test(test_set_root_cmd), ztest_unit_test(test_cmd_resize), ztest_unit_test(test_shell_module), ztest_unit_test(test_shell_wildcards_static),