From dac872885679aed05eaf7610815baf1bd95ac18f Mon Sep 17 00:00:00 2001 From: Jakub Rzeszutko Date: Mon, 3 Aug 2020 15:12:17 +0200 Subject: [PATCH] shell: Add warning about not initialized backend Shell will display a warning message if there was a request to print a message on the not initialized shell backend. Signed-off-by: Jakub Rzeszutko --- include/shell/shell.h | 3 ++- subsys/shell/shell.c | 8 ++++++++ subsys/shell/shell_ops.h | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/shell/shell.h b/include/shell/shell.h index a67415f9a78..91e9afac8b5 100644 --- a/include/shell/shell.h +++ b/include/shell/shell.h @@ -541,8 +541,9 @@ struct shell_flags { uint32_t tx_rdy :1; uint32_t mode_delete :1; /*!< Operation mode of backspace key */ uint32_t history_exit:1; /*!< Request to exit history mode */ - uint32_t cmd_ctx :1; /*!< Shell is executing command */ + uint32_t cmd_ctx :1; /*!< Shell is executing command */ uint32_t last_nl :8; /*!< Last received new line character */ + uint32_t print_noinit:1; /*!< Print request from not initialized shell*/ }; BUILD_ASSERT((sizeof(struct shell_flags) == sizeof(uint32_t)), diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index e4b92afbf9c..b38501a5923 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -25,6 +25,8 @@ #endif #define SHELL_MSG_CMD_NOT_FOUND ": command not found" +#define SHELL_MSG_BACKEND_NOT_ACTIVE \ + "WARNING: A print request was detected on not active shell backend.\n" #define SHELL_INIT_OPTION_PRINTER (NULL) @@ -85,6 +87,11 @@ static void state_set(const struct shell *shell, enum shell_state state) if (state == SHELL_STATE_ACTIVE) { cmd_buffer_clear(shell); + if (flag_print_noinit_get(shell)) { + shell_internal_fprintf(shell, SHELL_WARNING, "%s", + SHELL_MSG_BACKEND_NOT_ACTIVE); + flag_print_noinit_set(shell, false); + } shell_print_prompt_and_cmd(shell); } } @@ -1354,6 +1361,7 @@ void shell_vfprintf(const struct shell *shell, enum shell_vt100_color color, /* Sending a message to a non-active shell leads to a dead lock. */ if (shell->ctx->state != SHELL_STATE_ACTIVE) { + flag_print_noinit_set(shell, true); return; } diff --git a/subsys/shell/shell_ops.h b/subsys/shell/shell_ops.h index 4a0141baada..5d4779bde72 100644 --- a/subsys/shell/shell_ops.h +++ b/subsys/shell/shell_ops.h @@ -151,6 +151,16 @@ static inline void flag_last_nl_set(const struct shell *shell, uint8_t val) shell->ctx->internal.flags.last_nl = val; } +static inline bool flag_print_noinit_get(const struct shell *shell) +{ + return shell->ctx->internal.flags.print_noinit == 1 ? true : false; +} + +static inline void flag_print_noinit_set(const struct shell *shell, bool val) +{ + shell->ctx->internal.flags.print_noinit = val ? 1 : 0; +} + void shell_op_cursor_vert_move(const struct shell *shell, int32_t delta); void shell_op_cursor_horiz_move(const struct shell *shell, int32_t delta);