shell: add init backend configuration

Each backend can configure separately features like colors on/off, VT100
handling and so on. This can be very handy for planned MQTT backend.
Shell will not send VT100 commands when VT100 is not enabled globally or
for particular backend.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2021-09-20 14:21:57 +02:00 committed by Christopher Friedt
commit 2b5723d455
8 changed files with 252 additions and 188 deletions

View file

@ -615,31 +615,59 @@ struct shell_stats {
#endif /* CONFIG_SHELL_STATS */
/**
* @internal @brief Flags for internal shell usage.
* @internal @brief Flags for shell backend configuration.
*/
struct shell_flags {
struct shell_backend_config_flags {
uint32_t insert_mode :1; /*!< Controls insert mode for text introduction.*/
uint32_t use_colors :1; /*!< Controls colored syntax.*/
uint32_t echo :1; /*!< Controls shell echo.*/
uint32_t obscure :1; /*!< If echo on, print asterisk instead */
uint32_t processing :1; /*!< Shell is executing process function.*/
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 last_nl :8; /*!< Last received new line character */
uint32_t cmd_ctx :1; /*!< Shell is executing command */
uint32_t print_noinit:1; /*!< Print request from not initialized shell*/
uint32_t use_colors :1; /*!< Controls colored syntax.*/
uint32_t use_vt100 :1; /*!< Controls VT100 commands usage in shell */
};
BUILD_ASSERT((sizeof(struct shell_flags) == sizeof(uint32_t)),
BUILD_ASSERT((sizeof(struct shell_backend_config_flags) == sizeof(uint32_t)),
"Structure must fit in 4 bytes");
/**
* @internal @brief Default backend configuration.
*/
#define SHELL_DEFAULT_BACKEND_CONFIG_FLAGS \
{ \
.insert_mode = 0, \
.echo = 1, \
.obscure = 0, \
.mode_delete = 1, \
.use_colors = 1, \
.use_vt100 = 1, \
};
struct shell_backend_ctx_flags {
uint32_t processing :1; /*!< Shell is executing process function.*/
uint32_t tx_rdy :1;
uint32_t history_exit :1; /*!< Request to exit history mode */
uint32_t last_nl :8; /*!< Last received new line character */
uint32_t cmd_ctx :1; /*!< Shell is executing command */
uint32_t print_noinit :1; /*!< Print request from not initialized shell*/
};
BUILD_ASSERT((sizeof(struct shell_backend_ctx_flags) == sizeof(uint32_t)),
"Structure must fit in 4 bytes");
/**
* @internal @brief Union for internal shell usage.
*/
union shell_internal {
union shell_backend_cfg {
uint32_t value;
struct shell_flags flags;
struct shell_backend_config_flags flags;
};
/**
* @internal @brief Union for internal shell usage.
*/
union shell_backend_ctx {
uint32_t value;
struct shell_backend_ctx_flags flags;
};
enum shell_signal {
@ -695,7 +723,8 @@ struct shell_ctx {
/*!< Printf buffer size.*/
char printf_buff[CONFIG_SHELL_PRINTF_BUFF_SIZE];
volatile union shell_internal internal; /*!< Internal shell data.*/
volatile union shell_backend_cfg cfg;
volatile union shell_backend_ctx ctx;
struct k_poll_signal signals[SHELL_SIGNALS];
@ -796,7 +825,8 @@ extern void z_shell_print_stream(const void *user_ctx, const char *data,
*
* @param[in] shell Pointer to shell instance.
* @param[in] transport_config Transport configuration during initialization.
* @param[in] use_colors Enables colored prints.
* @param[in] cfg_flags Initial backend configuration flags.
* Shell will copy this data.
* @param[in] log_backend If true, the console will be used as logger
* backend.
* @param[in] init_log_level Default severity level for the logger.
@ -804,7 +834,8 @@ extern void z_shell_print_stream(const void *user_ctx, const char *data,
* @return Standard error code.
*/
int shell_init(const struct shell *shell, const void *transport_config,
bool use_colors, bool log_backend, uint32_t init_log_level);
struct shell_backend_config_flags cfg_flags,
bool log_backend, uint32_t init_log_level);
/**
* @brief Uninitializes the transport layer and the internal shell state.