diff --git a/include/shell/shell.h b/include/shell/shell.h index a8751d6e761..47087714954 100644 --- a/include/shell/shell.h +++ b/include/shell/shell.h @@ -585,6 +585,20 @@ bool shell_cmd_precheck(const struct shell *shell, void shell_print_stream(const void *user_ctx, const char *data, size_t data_len); +/** @brief Execute command. + * + * Pass command line to shell to execute. + * + * Note: This by no means makes any of the commands a stable interface, so + * this function should only be used for debugging/diagnostic. + * + * @param[in] shell Pointer to the shell instance. + * @param[in] cmd Command to be executed. + * + * @returns Result of the execution + */ +int shell_execute_cmd(const struct shell *shell, const char *cmd); + /** * @} */ diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index 980ad5c6965..172c740926e 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -1720,3 +1720,21 @@ bool shell_cmd_precheck(const struct shell *shell, return true; } + +int shell_execute_cmd(const struct shell *shell, const char *cmd) +{ + u16_t cmd_len = shell_strlen(cmd); + + if ((cmd == NULL) || (shell == NULL)) { + return -ENOEXEC; + } + + if (cmd_len > (CONFIG_SHELL_CMD_BUFF_SIZE - 1)) { + return -ENOEXEC; + } + + strcpy(shell->ctx->cmd_buff, cmd); + shell->ctx->cmd_buff_len = cmd_len; + + return shell_execute(shell); +}