1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef SHELL_H__
8 #define SHELL_H__
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/shell/shell_types.h>
12 #include <zephyr/shell/shell_history.h>
13 #include <zephyr/shell/shell_fprintf.h>
14 #include <zephyr/shell/shell_log_backend.h>
15 #include <zephyr/shell/shell_string_conv.h>
16 #include <zephyr/logging/log_instance.h>
17 #include <zephyr/logging/log.h>
18 #include <zephyr/sys/iterable_sections.h>
19 #include <zephyr/sys/util.h>
20 
21 #if defined CONFIG_SHELL_GETOPT
22 #include <getopt.h>
23 #endif
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #ifndef CONFIG_SHELL_CMD_BUFF_SIZE
30 #define CONFIG_SHELL_CMD_BUFF_SIZE 0
31 #endif
32 
33 #ifndef CONFIG_SHELL_PRINTF_BUFF_SIZE
34 #define CONFIG_SHELL_PRINTF_BUFF_SIZE 0
35 #endif
36 
37 #ifndef CONFIG_SHELL_HISTORY_BUFFER
38 #define CONFIG_SHELL_HISTORY_BUFFER 0
39 #endif
40 
41 #define Z_SHELL_CMD_ROOT_LVL		(0u)
42 
43 #define SHELL_HEXDUMP_BYTES_IN_LINE	16
44 
45 /**
46  * @brief Flag indicates that optional arguments will be treated as one,
47  *	  unformatted argument.
48  *
49  * By default, shell is parsing all arguments, treats all spaces as argument
50  * separators unless they are within quotation marks which are removed in that
51  * case. If command rely on unformatted argument then this flag shall be used
52  * in place of number of optional arguments in command definition to indicate
53  * that only mandatory arguments shall be parsed and remaining command string is
54  * passed as a raw string.
55  */
56 #define SHELL_OPT_ARG_RAW	(0xFE)
57 
58 /**
59  * @brief Flag indicating that number of optional arguments is not limited.
60  */
61 #define SHELL_OPT_ARG_CHECK_SKIP (0xFF)
62 
63 /**
64  * @brief Flag indicating maximum number of optional arguments that can be
65  *	  validated.
66  */
67 #define SHELL_OPT_ARG_MAX		(0xFD)
68 
69 /**
70  * @brief Shell API
71  * @defgroup shell_api Shell API
72  * @ingroup os_services
73  * @{
74  */
75 
76 struct shell_static_entry;
77 
78 /**
79  * @brief Shell dynamic command descriptor.
80  *
81  * @details Function shall fill the received shell_static_entry structure
82  * with requested (idx) dynamic subcommand data. If there is more than
83  * one dynamic subcommand available, the function shall ensure that the
84  * returned commands: entry->syntax are sorted in alphabetical order.
85  * If idx exceeds the available dynamic subcommands, the function must
86  * write to entry->syntax NULL value. This will indicate to the shell
87  * module that there are no more dynamic commands to read.
88  */
89 typedef void (*shell_dynamic_get)(size_t idx,
90 				  struct shell_static_entry *entry);
91 
92 /**
93  * @brief Shell command descriptor.
94  */
95 union shell_cmd_entry {
96 	/*!< Pointer to function returning dynamic commands.*/
97 	shell_dynamic_get dynamic_get;
98 
99 	/*!< Pointer to array of static commands. */
100 	const struct shell_static_entry *entry;
101 };
102 
103 struct shell;
104 
105 struct shell_static_args {
106 	uint8_t mandatory; /*!< Number of mandatory arguments. */
107 	uint8_t optional;  /*!< Number of optional arguments. */
108 };
109 
110 /**
111  * @brief Get by index a device that matches .
112  *
113  * This can be used, for example, to identify I2C_1 as the second I2C
114  * device.
115  *
116  * Devices that failed to initialize or do not have a non-empty name
117  * are excluded from the candidates for a match.
118  *
119  * @param idx the device number starting from zero.
120  *
121  * @param prefix optional name prefix used to restrict candidate
122  * devices.  Indexing is done relative to devices with names that
123  * start with this text.  Pass null if no prefix match is required.
124  */
125 const struct device *shell_device_lookup(size_t idx,
126 				   const char *prefix);
127 
128 /**
129  * @brief Shell command handler prototype.
130  *
131  * @param sh Shell instance.
132  * @param argc  Arguments count.
133  * @param argv  Arguments.
134  *
135  * @retval 0 Successful command execution.
136  * @retval 1 Help printed and command not executed.
137  * @retval -EINVAL Argument validation failed.
138  * @retval -ENOEXEC Command not executed.
139  */
140 typedef int (*shell_cmd_handler)(const struct shell *sh,
141 				 size_t argc, char **argv);
142 
143 /**
144  * @brief Shell dictionary command handler prototype.
145  *
146  * @param sh Shell instance.
147  * @param argc  Arguments count.
148  * @param argv  Arguments.
149  * @param data  Pointer to the user data.
150  *
151  * @retval 0 Successful command execution.
152  * @retval 1 Help printed and command not executed.
153  * @retval -EINVAL Argument validation failed.
154  * @retval -ENOEXEC Command not executed.
155  */
156 typedef int (*shell_dict_cmd_handler)(const struct shell *sh, size_t argc,
157 				      char **argv, void *data);
158 
159 /* When entries are added to the memory section a padding is applied for
160  * native_posix_64 and x86_64 targets. Adding padding to allow handle data
161  * in the memory section as array.
162  */
163 #if (defined(CONFIG_ARCH_POSIX) && defined(CONFIG_64BIT)) || defined(CONFIG_X86_64)
164 #define Z_SHELL_STATIC_ENTRY_PADDING 24
165 #else
166 #define Z_SHELL_STATIC_ENTRY_PADDING 0
167 #endif
168 
169 /*
170  * @brief Shell static command descriptor.
171  */
172 struct shell_static_entry {
173 	const char *syntax;			/*!< Command syntax strings. */
174 	const char *help;			/*!< Command help string. */
175 	const union shell_cmd_entry *subcmd;	/*!< Pointer to subcommand. */
176 	shell_cmd_handler handler;		/*!< Command handler. */
177 	struct shell_static_args args;		/*!< Command arguments. */
178 	uint8_t padding[Z_SHELL_STATIC_ENTRY_PADDING];
179 };
180 
181 /**
182  * @brief Macro for defining and adding a root command (level 0) with required
183  * number of arguments.
184  *
185  * @note Each root command shall have unique syntax. If a command will be called
186  * with wrong number of arguments shell will print an error message and command
187  * handler will not be called.
188  *
189  * @param[in] syntax	Command syntax (for example: history).
190  * @param[in] subcmd	Pointer to a subcommands array.
191  * @param[in] help	Pointer to a command help string.
192  * @param[in] handler	Pointer to a function handler.
193  * @param[in] mandatory	Number of mandatory arguments including command name.
194  * @param[in] optional	Number of optional arguments.
195  */
196 #define SHELL_CMD_ARG_REGISTER(syntax, subcmd, help, handler,		   \
197 			       mandatory, optional)			   \
198 	static const struct shell_static_entry UTIL_CAT(_shell_, syntax) = \
199 	SHELL_CMD_ARG(syntax, subcmd, help, handler, mandatory, optional); \
200 	static const TYPE_SECTION_ITERABLE(union shell_cmd_entry,	   \
201 		UTIL_CAT(shell_cmd_, syntax), shell_root_cmds,		   \
202 		UTIL_CAT(shell_cmd_, syntax)				   \
203 	) = {								   \
204 		.entry = &UTIL_CAT(_shell_, syntax)			   \
205 	}
206 
207 /**
208  * @brief Macro for defining and adding a conditional root command (level 0)
209  * with required number of arguments.
210  *
211  * @see SHELL_CMD_ARG_REGISTER for details.
212  *
213  * Macro can be used to create a command which can be conditionally present.
214  * It is and alternative to \#ifdefs around command registration and command
215  * handler. If command is disabled handler and subcommands are removed from
216  * the application.
217  *
218  * @param[in] flag	Compile time flag. Command is present only if flag
219  *			exists and equals 1.
220  * @param[in] syntax	Command syntax (for example: history).
221  * @param[in] subcmd	Pointer to a subcommands array.
222  * @param[in] help	Pointer to a command help string.
223  * @param[in] handler	Pointer to a function handler.
224  * @param[in] mandatory	Number of mandatory arguments including command name.
225  * @param[in] optional	Number of optional arguments.
226  */
227 #define SHELL_COND_CMD_ARG_REGISTER(flag, syntax, subcmd, help, handler, \
228 					mandatory, optional) \
229 	COND_CODE_1(\
230 		flag, \
231 		(\
232 		SHELL_CMD_ARG_REGISTER(syntax, subcmd, help, handler, \
233 					mandatory, optional) \
234 		), \
235 		(\
236 		static shell_cmd_handler dummy_##syntax##_handler __unused = \
237 								handler;\
238 		static const union shell_cmd_entry *dummy_subcmd_##syntax \
239 			__unused = subcmd\
240 		) \
241 	)
242 /**
243  * @brief Macro for defining and adding a root command (level 0) with
244  * arguments.
245  *
246  * @note All root commands must have different name.
247  *
248  * @param[in] syntax	Command syntax (for example: history).
249  * @param[in] subcmd	Pointer to a subcommands array.
250  * @param[in] help	Pointer to a command help string.
251  * @param[in] handler	Pointer to a function handler.
252  */
253 #define SHELL_CMD_REGISTER(syntax, subcmd, help, handler) \
254 	SHELL_CMD_ARG_REGISTER(syntax, subcmd, help, handler, 0, 0)
255 
256 /**
257  * @brief Macro for defining and adding a conditional root command (level 0)
258  * with arguments.
259  *
260  * @see SHELL_COND_CMD_ARG_REGISTER.
261  *
262  * @param[in] flag	Compile time flag. Command is present only if flag
263  *			exists and equals 1.
264  * @param[in] syntax	Command syntax (for example: history).
265  * @param[in] subcmd	Pointer to a subcommands array.
266  * @param[in] help	Pointer to a command help string.
267  * @param[in] handler	Pointer to a function handler.
268  */
269 #define SHELL_COND_CMD_REGISTER(flag, syntax, subcmd, help, handler) \
270 	SHELL_COND_CMD_ARG_REGISTER(flag, syntax, subcmd, help, handler, 0, 0)
271 
272 /**
273  * @brief Macro for creating a subcommand set. It must be used outside of any
274  * function body.
275  *
276  * Example usage:
277  * SHELL_STATIC_SUBCMD_SET_CREATE(
278  *	foo,
279  *	SHELL_CMD(abc, ...),
280  *	SHELL_CMD(def, ...),
281  *	SHELL_SUBCMD_SET_END
282  * )
283  *
284  * @param[in] name	Name of the subcommand set.
285  * @param[in] ...	List of commands created with @ref SHELL_CMD_ARG or
286  *			or @ref SHELL_CMD
287  */
288 #define SHELL_STATIC_SUBCMD_SET_CREATE(name, ...)			\
289 	static const struct shell_static_entry shell_##name[] = {	\
290 		__VA_ARGS__						\
291 	};								\
292 	static const union shell_cmd_entry name = {			\
293 		.entry = shell_##name					\
294 	}
295 
296 #define Z_SHELL_UNDERSCORE(x) _##x
297 #define Z_SHELL_SUBCMD_NAME(...) \
298 	UTIL_CAT(shell_subcmds, MACRO_MAP_CAT(Z_SHELL_UNDERSCORE, __VA_ARGS__))
299 #define Z_SHELL_SUBCMD_SECTION_TAG(...) MACRO_MAP_CAT(Z_SHELL_UNDERSCORE, __VA_ARGS__)
300 #define Z_SHELL_SUBCMD_SET_SECTION_TAG(x) \
301 	Z_SHELL_SUBCMD_SECTION_TAG(NUM_VA_ARGS_LESS_1 x, __DEBRACKET x)
302 #define Z_SHELL_SUBCMD_ADD_SECTION_TAG(x, y) \
303 	Z_SHELL_SUBCMD_SECTION_TAG(NUM_VA_ARGS_LESS_1 x, __DEBRACKET x, y)
304 
305 /** @brief Create set of subcommands.
306  *
307  * Commands to this set are added using @ref SHELL_SUBCMD_ADD and @ref SHELL_SUBCMD_COND_ADD.
308  * Commands can be added from multiple files.
309  *
310  * @param[in] _name		Name of the set. @p _name is used to refer the set in the parent
311  * command.
312  *
313  * @param[in] _parent	Set of comma separated parent commands in parenthesis, e.g.
314  * (foo_cmd) if subcommands are for the root command "foo_cmd".
315  */
316 
317 #define SHELL_SUBCMD_SET_CREATE(_name, _parent) \
318 	static const TYPE_SECTION_ITERABLE(struct shell_static_entry, _name, shell_subcmds, \
319 					  Z_SHELL_SUBCMD_SET_SECTION_TAG(_parent))
320 
321 
322 /** @brief Conditionally add command to the set of subcommands.
323  *
324  * Add command to the set created with @ref SHELL_SUBCMD_SET_CREATE.
325  *
326  * @note The name of the section is formed as concatenation of number of parent
327  * commands, names of all parent commands and own syntax. Number of parent commands
328  * is added to ensure that section prefix is unique. Without it subcommands of
329  * (foo) and (foo, cmd1) would mix.
330  *
331  * @param[in] _flag	 Compile time flag. Command is present only if flag
332  *			 exists and equals 1.
333  * @param[in] _parent	 Parent command sequence. Comma separated in parenthesis.
334  * @param[in] _syntax	 Command syntax (for example: history).
335  * @param[in] _subcmd	 Pointer to a subcommands array.
336  * @param[in] _help	 Pointer to a command help string.
337  * @param[in] _handler	 Pointer to a function handler.
338  * @param[in] _mand	 Number of mandatory arguments including command name.
339  * @param[in] _opt	 Number of optional arguments.
340  */
341 #define SHELL_SUBCMD_COND_ADD(_flag, _parent, _syntax, _subcmd, _help, _handler, \
342 			   _mand, _opt) \
343 	COND_CODE_1(_flag, \
344 		(static const TYPE_SECTION_ITERABLE(struct shell_static_entry, \
345 					Z_SHELL_SUBCMD_NAME(__DEBRACKET _parent, _syntax), \
346 					shell_subcmds, \
347 					Z_SHELL_SUBCMD_ADD_SECTION_TAG(_parent, _syntax)) = \
348 			SHELL_EXPR_CMD_ARG(1, _syntax, _subcmd, _help, \
349 					   _handler, _mand, _opt)\
350 		), \
351 		(static shell_cmd_handler dummy_##syntax##_handler __unused = _handler;\
352 		 static const union shell_cmd_entry dummy_subcmd_##syntax __unused = { \
353 			.entry = (const struct shell_static_entry *)_subcmd\
354 		 } \
355 		) \
356 	)
357 
358 /** @brief Add command to the set of subcommands.
359  *
360  * Add command to the set created with @ref SHELL_SUBCMD_SET_CREATE.
361  *
362  * @param[in] _parent	 Parent command sequence. Comma separated in parenthesis.
363  * @param[in] _syntax	 Command syntax (for example: history).
364  * @param[in] _subcmd	 Pointer to a subcommands array.
365  * @param[in] _help	 Pointer to a command help string.
366  * @param[in] _handler	 Pointer to a function handler.
367  * @param[in] _mand	 Number of mandatory arguments including command name.
368  * @param[in] _opt	 Number of optional arguments.
369  */
370 #define SHELL_SUBCMD_ADD(_parent, _syntax, _subcmd, _help, _handler, _mand, _opt) \
371 	SHELL_SUBCMD_COND_ADD(1, _parent, _syntax, _subcmd, _help, _handler, _mand, _opt)
372 
373 /**
374  * @brief Define ending subcommands set.
375  *
376  */
377 #define SHELL_SUBCMD_SET_END {NULL}
378 
379 /**
380  * @brief Macro for creating a dynamic entry.
381  *
382  * @param[in] name	Name of the dynamic entry.
383  * @param[in] get	Pointer to the function returning dynamic commands array
384  */
385 #define SHELL_DYNAMIC_CMD_CREATE(name, get)					\
386 	static const TYPE_SECTION_ITERABLE(union shell_cmd_entry, name,		\
387 		shell_dynamic_subcmds, name) =					\
388 	{									\
389 		.dynamic_get = get						\
390 	}
391 
392 /**
393  * @brief Initializes a shell command with arguments.
394  *
395  * @note If a command will be called with wrong number of arguments shell will
396  * print an error message and command handler will not be called.
397  *
398  * @param[in] syntax	 Command syntax (for example: history).
399  * @param[in] subcmd	 Pointer to a subcommands array.
400  * @param[in] help	 Pointer to a command help string.
401  * @param[in] handler	 Pointer to a function handler.
402  * @param[in] mand	 Number of mandatory arguments including command name.
403  * @param[in] opt	 Number of optional arguments.
404  */
405 #define SHELL_CMD_ARG(syntax, subcmd, help, handler, mand, opt) \
406 	SHELL_EXPR_CMD_ARG(1, syntax, subcmd, help, handler, mand, opt)
407 
408 /**
409  * @brief Initializes a conditional shell command with arguments.
410  *
411  * @see SHELL_CMD_ARG. Based on the flag, creates a valid entry or an empty
412  * command which is ignored by the shell. It is an alternative to \#ifdefs
413  * around command registration and command handler. However, empty structure is
414  * present in the flash even if command is disabled (subcommands and handler are
415  * removed). Macro internally handles case if flag is not defined so flag must
416  * be provided without any wrapper, e.g.: SHELL_COND_CMD_ARG(CONFIG_FOO, ...)
417  *
418  * @param[in] flag	 Compile time flag. Command is present only if flag
419  *			 exists and equals 1.
420  * @param[in] syntax	 Command syntax (for example: history).
421  * @param[in] subcmd	 Pointer to a subcommands array.
422  * @param[in] help	 Pointer to a command help string.
423  * @param[in] handler	 Pointer to a function handler.
424  * @param[in] mand	 Number of mandatory arguments including command name.
425  * @param[in] opt	 Number of optional arguments.
426  */
427 #define SHELL_COND_CMD_ARG(flag, syntax, subcmd, help, handler, mand, opt) \
428 	SHELL_EXPR_CMD_ARG(IS_ENABLED(flag), syntax, subcmd, help, \
429 			  handler, mand, opt)
430 
431 /**
432  * @brief Initializes a conditional shell command with arguments if expression
433  *	  gives non-zero result at compile time.
434  *
435  * @see SHELL_CMD_ARG. Based on the expression, creates a valid entry or an
436  * empty command which is ignored by the shell. It should be used instead of
437  * @ref SHELL_COND_CMD_ARG if condition is not a single configuration flag,
438  * e.g.:
439  * SHELL_EXPR_CMD_ARG(IS_ENABLED(CONFIG_FOO) &&
440  *		      IS_ENABLED(CONFIG_FOO_SETTING_1), ...)
441  *
442  * @param[in] _expr	 Expression.
443  * @param[in] _syntax	 Command syntax (for example: history).
444  * @param[in] _subcmd	 Pointer to a subcommands array.
445  * @param[in] _help	 Pointer to a command help string.
446  * @param[in] _handler	 Pointer to a function handler.
447  * @param[in] _mand	 Number of mandatory arguments including command name.
448  * @param[in] _opt	 Number of optional arguments.
449  */
450 #define SHELL_EXPR_CMD_ARG(_expr, _syntax, _subcmd, _help, _handler, \
451 			   _mand, _opt) \
452 	{ \
453 		.syntax = (_expr) ? (const char *)STRINGIFY(_syntax) : "", \
454 		.help  = (_expr) ? (const char *)_help : NULL, \
455 		.subcmd = (const union shell_cmd_entry *)((_expr) ? \
456 				_subcmd : NULL), \
457 		.handler = (shell_cmd_handler)((_expr) ? _handler : NULL), \
458 		.args = { .mandatory = _mand, .optional = _opt} \
459 	}
460 
461 /**
462  * @brief Initializes a shell command.
463  *
464  * @param[in] _syntax	Command syntax (for example: history).
465  * @param[in] _subcmd	Pointer to a subcommands array.
466  * @param[in] _help	Pointer to a command help string.
467  * @param[in] _handler	Pointer to a function handler.
468  */
469 #define SHELL_CMD(_syntax, _subcmd, _help, _handler) \
470 	SHELL_CMD_ARG(_syntax, _subcmd, _help, _handler, 0, 0)
471 
472 /**
473  * @brief Initializes a conditional shell command.
474  *
475  * @see SHELL_COND_CMD_ARG.
476  *
477  * @param[in] _flag	Compile time flag. Command is present only if flag
478  *			exists and equals 1.
479  * @param[in] _syntax	Command syntax (for example: history).
480  * @param[in] _subcmd	Pointer to a subcommands array.
481  * @param[in] _help	Pointer to a command help string.
482  * @param[in] _handler	Pointer to a function handler.
483  */
484 #define SHELL_COND_CMD(_flag, _syntax, _subcmd, _help, _handler) \
485 	SHELL_COND_CMD_ARG(_flag, _syntax, _subcmd, _help, _handler, 0, 0)
486 
487 /**
488  * @brief Initializes shell command if expression gives non-zero result at
489  *	  compile time.
490  *
491  * @see SHELL_EXPR_CMD_ARG.
492  *
493  * @param[in] _expr	Compile time expression. Command is present only if
494  *			expression is non-zero.
495  * @param[in] _syntax	Command syntax (for example: history).
496  * @param[in] _subcmd	Pointer to a subcommands array.
497  * @param[in] _help	Pointer to a command help string.
498  * @param[in] _handler	Pointer to a function handler.
499  */
500 #define SHELL_EXPR_CMD(_expr, _syntax, _subcmd, _help, _handler) \
501 	SHELL_EXPR_CMD_ARG(_expr, _syntax, _subcmd, _help, _handler, 0, 0)
502 
503 /* Internal macro used for creating handlers for dictionary commands. */
504 #define Z_SHELL_CMD_DICT_HANDLER_CREATE(_data, _handler)		\
505 static int UTIL_CAT(UTIL_CAT(cmd_dict_, UTIL_CAT(_handler, _)),		\
506 			GET_ARG_N(1, __DEBRACKET _data))(		\
507 		const struct shell *sh, size_t argc, char **argv)	\
508 {									\
509 	return _handler(sh, argc, argv,					\
510 			(void *)GET_ARG_N(2, __DEBRACKET _data));	\
511 }
512 
513 /* Internal macro used for creating dictionary commands. */
514 #define SHELL_CMD_DICT_CREATE(_data, _handler)				\
515 	SHELL_CMD_ARG(GET_ARG_N(1, __DEBRACKET _data), NULL, GET_ARG_N(3, __DEBRACKET _data),	\
516 		UTIL_CAT(UTIL_CAT(cmd_dict_, UTIL_CAT(_handler, _)),	\
517 			GET_ARG_N(1, __DEBRACKET _data)), 1, 0)
518 
519 /**
520  * @brief Initializes shell dictionary commands.
521  *
522  * This is a special kind of static commands. Dictionary commands can be used
523  * every time you want to use a pair: (string <-> corresponding data) in
524  * a command handler. The string is usually a verbal description of a given
525  * data. The idea is to use the string as a command syntax that can be prompted
526  * by the shell and corresponding data can be used to process the command.
527  *
528  * @param[in] _name	Name of the dictionary subcommand set
529  * @param[in] _handler	Command handler common for all dictionary commands.
530  *			@see shell_dict_cmd_handler
531  * @param[in] ...	Dictionary pairs: (command_syntax, value). Value will be
532  *			passed to the _handler as user data.
533  *
534  * Example usage:
535  *	static int my_handler(const struct shell *sh,
536  *			      size_t argc, char **argv, void *data)
537  *	{
538  *		int val = (int)data;
539  *
540  *		shell_print(sh, "(syntax, value) : (%s, %d)", argv[0], val);
541  *		return 0;
542  *	}
543  *
544  *	SHELL_SUBCMD_DICT_SET_CREATE(sub_dict_cmds, my_handler,
545  *		(value_0, 0, "value 0"), (value_1, 1, "value 1"),
546  *		(value_2, 2, "value 2"), (value_3, 3, "value 3")
547  *	);
548  *	SHELL_CMD_REGISTER(dictionary, &sub_dict_cmds, NULL, NULL);
549  */
550 #define SHELL_SUBCMD_DICT_SET_CREATE(_name, _handler, ...)		\
551 	FOR_EACH_FIXED_ARG(Z_SHELL_CMD_DICT_HANDLER_CREATE, (),		\
552 			   _handler, __VA_ARGS__)			\
553 	SHELL_STATIC_SUBCMD_SET_CREATE(_name,				\
554 		FOR_EACH_FIXED_ARG(SHELL_CMD_DICT_CREATE, (,), _handler, __VA_ARGS__),	\
555 		SHELL_SUBCMD_SET_END					\
556 	)
557 
558 /**
559  * @internal @brief Internal shell state in response to data received from the
560  * terminal.
561  */
562 enum shell_receive_state {
563 	SHELL_RECEIVE_DEFAULT,
564 	SHELL_RECEIVE_ESC,
565 	SHELL_RECEIVE_ESC_SEQ,
566 	SHELL_RECEIVE_TILDE_EXP
567 };
568 
569 /**
570  * @internal @brief Internal shell state.
571  */
572 enum shell_state {
573 	SHELL_STATE_UNINITIALIZED,
574 	SHELL_STATE_INITIALIZED,
575 	SHELL_STATE_ACTIVE,
576 	SHELL_STATE_PANIC_MODE_ACTIVE,  /*!< Panic activated.*/
577 	SHELL_STATE_PANIC_MODE_INACTIVE /*!< Panic requested, not supported.*/
578 };
579 
580 /** @brief Shell transport event. */
581 enum shell_transport_evt {
582 	SHELL_TRANSPORT_EVT_RX_RDY,
583 	SHELL_TRANSPORT_EVT_TX_RDY
584 };
585 
586 typedef void (*shell_transport_handler_t)(enum shell_transport_evt evt,
587 					  void *context);
588 
589 
590 typedef void (*shell_uninit_cb_t)(const struct shell *sh, int res);
591 
592 /** @brief Bypass callback.
593  *
594  * @param sh Shell instance.
595  * @param data  Raw data from transport.
596  * @param len   Data length.
597  */
598 typedef void (*shell_bypass_cb_t)(const struct shell *sh,
599 				  uint8_t *data,
600 				  size_t len);
601 
602 struct shell_transport;
603 
604 /**
605  * @brief Unified shell transport interface.
606  */
607 struct shell_transport_api {
608 	/**
609 	 * @brief Function for initializing the shell transport interface.
610 	 *
611 	 * @param[in] transport   Pointer to the transfer instance.
612 	 * @param[in] config      Pointer to instance configuration.
613 	 * @param[in] evt_handler Event handler.
614 	 * @param[in] context     Pointer to the context passed to event
615 	 *			  handler.
616 	 *
617 	 * @return Standard error code.
618 	 */
619 	int (*init)(const struct shell_transport *transport,
620 		    const void *config,
621 		    shell_transport_handler_t evt_handler,
622 		    void *context);
623 
624 	/**
625 	 * @brief Function for uninitializing the shell transport interface.
626 	 *
627 	 * @param[in] transport  Pointer to the transfer instance.
628 	 *
629 	 * @return Standard error code.
630 	 */
631 	int (*uninit)(const struct shell_transport *transport);
632 
633 	/**
634 	 * @brief Function for enabling transport in given TX mode.
635 	 *
636 	 * Function can be used to reconfigure TX to work in blocking mode.
637 	 *
638 	 * @param transport   Pointer to the transfer instance.
639 	 * @param blocking_tx If true, the transport TX is enabled in blocking
640 	 *		      mode.
641 	 *
642 	 * @return NRF_SUCCESS on successful enabling, error otherwise (also if
643 	 * not supported).
644 	 */
645 	int (*enable)(const struct shell_transport *transport,
646 		      bool blocking_tx);
647 
648 	/**
649 	 * @brief Function for writing data to the transport interface.
650 	 *
651 	 * @param[in]  transport  Pointer to the transfer instance.
652 	 * @param[in]  data       Pointer to the source buffer.
653 	 * @param[in]  length     Source buffer length.
654 	 * @param[out] cnt        Pointer to the sent bytes counter.
655 	 *
656 	 * @return Standard error code.
657 	 */
658 	int (*write)(const struct shell_transport *transport,
659 		     const void *data, size_t length, size_t *cnt);
660 
661 	/**
662 	 * @brief Function for reading data from the transport interface.
663 	 *
664 	 * @param[in]  p_transport  Pointer to the transfer instance.
665 	 * @param[in]  p_data       Pointer to the destination buffer.
666 	 * @param[in]  length       Destination buffer length.
667 	 * @param[out] cnt          Pointer to the received bytes counter.
668 	 *
669 	 * @return Standard error code.
670 	 */
671 	int (*read)(const struct shell_transport *transport,
672 		    void *data, size_t length, size_t *cnt);
673 
674 	/**
675 	 * @brief Function called in shell thread loop.
676 	 *
677 	 * Can be used for backend operations that require longer execution time
678 	 *
679 	 * @param[in] transport Pointer to the transfer instance.
680 	 */
681 	void (*update)(const struct shell_transport *transport);
682 
683 };
684 
685 struct shell_transport {
686 	const struct shell_transport_api *api;
687 	void *ctx;
688 };
689 
690 /**
691  * @brief Shell statistics structure.
692  */
693 struct shell_stats {
694 	atomic_t log_lost_cnt; /*!< Lost log counter.*/
695 };
696 
697 #ifdef CONFIG_SHELL_STATS
698 #define Z_SHELL_STATS_DEFINE(_name) static struct shell_stats _name##_stats
699 #define Z_SHELL_STATS_PTR(_name) (&(_name##_stats))
700 #else
701 #define Z_SHELL_STATS_DEFINE(_name)
702 #define Z_SHELL_STATS_PTR(_name) NULL
703 #endif /* CONFIG_SHELL_STATS */
704 
705 /**
706  * @internal @brief Flags for shell backend configuration.
707  */
708 struct shell_backend_config_flags {
709 	uint32_t insert_mode :1; /*!< Controls insert mode for text introduction */
710 	uint32_t echo        :1; /*!< Controls shell echo */
711 	uint32_t obscure     :1; /*!< If echo on, print asterisk instead */
712 	uint32_t mode_delete :1; /*!< Operation mode of backspace key */
713 	uint32_t use_colors  :1; /*!< Controls colored syntax */
714 	uint32_t use_vt100   :1; /*!< Controls VT100 commands usage in shell */
715 };
716 
717 BUILD_ASSERT((sizeof(struct shell_backend_config_flags) == sizeof(uint32_t)),
718 	     "Structure must fit in 4 bytes");
719 
720 /**
721  * @internal @brief Default backend configuration.
722  */
723 #define SHELL_DEFAULT_BACKEND_CONFIG_FLAGS				\
724 {									\
725 	.insert_mode	= 0,						\
726 	.echo		= 1,						\
727 	.obscure	= IS_ENABLED(CONFIG_SHELL_START_OBSCURED),	\
728 	.mode_delete	= 1,						\
729 	.use_colors	= 1,						\
730 	.use_vt100	= 1,						\
731 };
732 
733 struct shell_backend_ctx_flags {
734 	uint32_t processing   :1; /*!< Shell is executing process function */
735 	uint32_t tx_rdy       :1;
736 	uint32_t history_exit :1; /*!< Request to exit history mode */
737 	uint32_t last_nl      :8; /*!< Last received new line character */
738 	uint32_t cmd_ctx      :1; /*!< Shell is executing command */
739 	uint32_t print_noinit :1; /*!< Print request from not initialized shell */
740 	uint32_t sync_mode    :1; /*!< Shell in synchronous mode */
741 };
742 
743 BUILD_ASSERT((sizeof(struct shell_backend_ctx_flags) == sizeof(uint32_t)),
744 	     "Structure must fit in 4 bytes");
745 
746 /**
747  * @internal @brief Union for internal shell usage.
748  */
749 union shell_backend_cfg {
750 	atomic_t value;
751 	struct shell_backend_config_flags flags;
752 };
753 
754 /**
755  * @internal @brief Union for internal shell usage.
756  */
757 union shell_backend_ctx {
758 	uint32_t value;
759 	struct shell_backend_ctx_flags flags;
760 };
761 
762 enum shell_signal {
763 	SHELL_SIGNAL_RXRDY,
764 	SHELL_SIGNAL_LOG_MSG,
765 	SHELL_SIGNAL_KILL,
766 	SHELL_SIGNAL_TXDONE, /* TXDONE must be last one before SHELL_SIGNALS */
767 	SHELL_SIGNALS
768 };
769 
770 /**
771  * @brief Shell instance context.
772  */
773 struct shell_ctx {
774 	const char *prompt; /*!< shell current prompt. */
775 
776 	enum shell_state state; /*!< Internal module state.*/
777 	enum shell_receive_state receive_state;/*!< Escape sequence indicator.*/
778 
779 	/*!< Currently executed command.*/
780 	struct shell_static_entry active_cmd;
781 
782 	/* New root command. If NULL shell uses default root commands. */
783 	const struct shell_static_entry *selected_cmd;
784 
785 	/*!< VT100 color and cursor position, terminal width.*/
786 	struct shell_vt100_ctx vt100_ctx;
787 
788 	/*!< Callback called from shell thread context when unitialization is
789 	 * completed just before aborting shell thread.
790 	 */
791 	shell_uninit_cb_t uninit_cb;
792 
793 	/*!< When bypass is set, all incoming data is passed to the callback. */
794 	shell_bypass_cb_t bypass;
795 
796 #if defined CONFIG_SHELL_GETOPT
797 	/*!< getopt context for a shell backend. */
798 	struct getopt_state getopt;
799 #endif
800 
801 	uint16_t cmd_buff_len; /*!< Command length.*/
802 	uint16_t cmd_buff_pos; /*!< Command buffer cursor position.*/
803 
804 	uint16_t cmd_tmp_buff_len; /*!< Command length in tmp buffer.*/
805 
806 	/*!< Command input buffer.*/
807 	char cmd_buff[CONFIG_SHELL_CMD_BUFF_SIZE];
808 
809 	/*!< Command temporary buffer.*/
810 	char temp_buff[CONFIG_SHELL_CMD_BUFF_SIZE];
811 
812 	/*!< Printf buffer size.*/
813 	char printf_buff[CONFIG_SHELL_PRINTF_BUFF_SIZE];
814 
815 	volatile union shell_backend_cfg cfg;
816 	volatile union shell_backend_ctx ctx;
817 
818 	struct k_poll_signal signals[SHELL_SIGNALS];
819 
820 	/*!< Events that should be used only internally by shell thread.
821 	 * Event for SHELL_SIGNAL_TXDONE is initialized but unused.
822 	 */
823 	struct k_poll_event events[SHELL_SIGNALS];
824 
825 	struct k_mutex wr_mtx;
826 	k_tid_t tid;
827 	int ret_val;
828 };
829 
830 extern const struct log_backend_api log_backend_shell_api;
831 
832 /**
833  * @brief Flags for setting shell output newline sequence.
834  */
835 enum shell_flag {
836 	SHELL_FLAG_CRLF_DEFAULT	= (1<<0),	/* Do not map CR or LF */
837 	SHELL_FLAG_OLF_CRLF	= (1<<1)	/* Map LF to CRLF on output */
838 };
839 
840 /**
841  * @brief Shell instance internals.
842  */
843 struct shell {
844 	const char *default_prompt; /*!< shell default prompt. */
845 
846 	const struct shell_transport *iface; /*!< Transport interface.*/
847 	struct shell_ctx *ctx; /*!< Internal context.*/
848 
849 	struct shell_history *history;
850 
851 	const enum shell_flag shell_flag;
852 
853 	const struct shell_fprintf *fprintf_ctx;
854 
855 	struct shell_stats *stats;
856 
857 	const struct shell_log_backend *log_backend;
858 
859 	LOG_INSTANCE_PTR_DECLARE(log);
860 
861 	const char *thread_name;
862 	struct k_thread *thread;
863 	k_thread_stack_t *stack;
864 };
865 
866 extern void z_shell_print_stream(const void *user_ctx, const char *data,
867 				 size_t data_len);
868 /**
869  * @brief Macro for defining a shell instance.
870  *
871  * @param[in] _name		Instance name.
872  * @param[in] _prompt		Shell default prompt string.
873  * @param[in] _transport_iface	Pointer to the transport interface.
874  * @param[in] _log_queue_size	Logger processing queue size.
875  * @param[in] _log_timeout	Logger thread timeout in milliseconds on full
876  *				log queue. If queue is full logger thread is
877  *				blocked for given amount of time before log
878  *				message is dropped.
879  * @param[in] _shell_flag	Shell output newline sequence.
880  */
881 #define SHELL_DEFINE(_name, _prompt, _transport_iface,			      \
882 		     _log_queue_size, _log_timeout, _shell_flag)	      \
883 	static const struct shell _name;				      \
884 	static struct shell_ctx UTIL_CAT(_name, _ctx);			      \
885 	static uint8_t _name##_out_buffer[CONFIG_SHELL_PRINTF_BUFF_SIZE];     \
886 	Z_SHELL_LOG_BACKEND_DEFINE(_name, _name##_out_buffer,		      \
887 				 CONFIG_SHELL_PRINTF_BUFF_SIZE,		      \
888 				 _log_queue_size, _log_timeout);	      \
889 	Z_SHELL_HISTORY_DEFINE(_name##_history, CONFIG_SHELL_HISTORY_BUFFER); \
890 	Z_SHELL_FPRINTF_DEFINE(_name##_fprintf, &_name, _name##_out_buffer,   \
891 			     CONFIG_SHELL_PRINTF_BUFF_SIZE,		      \
892 			     true, z_shell_print_stream);		      \
893 	LOG_INSTANCE_REGISTER(shell, _name, CONFIG_SHELL_LOG_LEVEL);	      \
894 	Z_SHELL_STATS_DEFINE(_name);					      \
895 	static K_KERNEL_STACK_DEFINE(_name##_stack, CONFIG_SHELL_STACK_SIZE); \
896 	static struct k_thread _name##_thread;				      \
897 	static const STRUCT_SECTION_ITERABLE(shell, _name) = {		      \
898 		.default_prompt = _prompt,				      \
899 		.iface = _transport_iface,				      \
900 		.ctx = &UTIL_CAT(_name, _ctx),				      \
901 		.history = IS_ENABLED(CONFIG_SHELL_HISTORY) ?		      \
902 				&_name##_history : NULL,		      \
903 		.shell_flag = _shell_flag,				      \
904 		.fprintf_ctx = &_name##_fprintf,			      \
905 		.stats = Z_SHELL_STATS_PTR(_name),			      \
906 		.log_backend = Z_SHELL_LOG_BACKEND_PTR(_name),		      \
907 		LOG_INSTANCE_PTR_INIT(log, shell, _name)		      \
908 		.thread_name = STRINGIFY(_name),			      \
909 		.thread = &_name##_thread,				      \
910 		.stack = _name##_stack					      \
911 	}
912 
913 /**
914  * @brief Function for initializing a transport layer and internal shell state.
915  *
916  * @param[in] sh		Pointer to shell instance.
917  * @param[in] transport_config	Transport configuration during initialization.
918  * @param[in] cfg_flags		Initial backend configuration flags.
919  *				Shell will copy this data.
920  * @param[in] log_backend	If true, the console will be used as logger
921  *				backend.
922  * @param[in] init_log_level	Default severity level for the logger.
923  *
924  * @return Standard error code.
925  */
926 int shell_init(const struct shell *sh, const void *transport_config,
927 	       struct shell_backend_config_flags cfg_flags,
928 	       bool log_backend, uint32_t init_log_level);
929 
930 /**
931  * @brief Uninitializes the transport layer and the internal shell state.
932  *
933  * @param sh Pointer to shell instance.
934  * @param cb Callback called when uninitialization is completed.
935  */
936 void shell_uninit(const struct shell *sh, shell_uninit_cb_t cb);
937 
938 /**
939  * @brief Function for starting shell processing.
940  *
941  * @param sh Pointer to the shell instance.
942  *
943  * @return Standard error code.
944  */
945 int shell_start(const struct shell *sh);
946 
947 /**
948  * @brief Function for stopping shell processing.
949  *
950  * @param sh Pointer to shell instance.
951  *
952  * @return Standard error code.
953  */
954 int shell_stop(const struct shell *sh);
955 
956 /**
957  * @brief Terminal default text color for shell_fprintf function.
958  */
959 #define SHELL_NORMAL	SHELL_VT100_COLOR_DEFAULT
960 
961 /**
962  * @brief Green text color for shell_fprintf function.
963  */
964 #define SHELL_INFO	SHELL_VT100_COLOR_GREEN
965 
966 /**
967  * @brief Cyan text color for shell_fprintf function.
968  */
969 #define SHELL_OPTION	SHELL_VT100_COLOR_CYAN
970 
971 /**
972  * @brief Yellow text color for shell_fprintf function.
973  */
974 #define SHELL_WARNING	SHELL_VT100_COLOR_YELLOW
975 
976 /**
977  * @brief Red text color for shell_fprintf function.
978  */
979 #define SHELL_ERROR	SHELL_VT100_COLOR_RED
980 
981 /**
982  * @brief printf-like function which sends formatted data stream to the shell.
983  *
984  * This function can be used from the command handler or from threads, but not
985  * from an interrupt context.
986  *
987  * @param[in] sh	Pointer to the shell instance.
988  * @param[in] color	Printed text color.
989  * @param[in] fmt	Format string.
990  * @param[in] ...	List of parameters to print.
991  */
992 void __printf_like(3, 4) shell_fprintf(const struct shell *sh,
993 				       enum shell_vt100_color color,
994 				       const char *fmt, ...);
995 
996 /**
997  * @brief vprintf-like function which sends formatted data stream to the shell.
998  *
999  * This function can be used from the command handler or from threads, but not
1000  * from an interrupt context. It is similar to shell_fprintf() but takes a
1001  * va_list instead of variable arguments.
1002  *
1003  * @param[in] sh	Pointer to the shell instance.
1004  * @param[in] color	Printed text color.
1005  * @param[in] fmt	Format string.
1006  * @param[in] args	List of parameters to print.
1007  */
1008 void shell_vfprintf(const struct shell *sh, enum shell_vt100_color color,
1009 		   const char *fmt, va_list args);
1010 
1011 /**
1012  * @brief Print a line of data in hexadecimal format.
1013  *
1014  * Each line shows the offset, bytes and then ASCII representation.
1015  *
1016  * For example:
1017  *
1018  * 00008010: 20 25 00 20 2f 48 00 08  80 05 00 20 af 46 00
1019  *	| %. /H.. ... .F. |
1020  *
1021  * @param[in] sh	Pointer to the shell instance.
1022  * @param[in] offset	Offset to show for this line.
1023  * @param[in] data	Pointer to data.
1024  * @param[in] len	Length of data.
1025  */
1026 void shell_hexdump_line(const struct shell *sh, unsigned int offset,
1027 			const uint8_t *data, size_t len);
1028 
1029 /**
1030  * @brief Print data in hexadecimal format.
1031  *
1032  * @param[in] sh	Pointer to the shell instance.
1033  * @param[in] data	Pointer to data.
1034  * @param[in] len	Length of data.
1035  */
1036 void shell_hexdump(const struct shell *sh, const uint8_t *data, size_t len);
1037 
1038 /**
1039  * @brief Print info message to the shell.
1040  *
1041  * See @ref shell_fprintf.
1042  *
1043  * @param[in] _sh Pointer to the shell instance.
1044  * @param[in] _ft Format string.
1045  * @param[in] ... List of parameters to print.
1046  */
1047 #define shell_info(_sh, _ft, ...) \
1048 	shell_fprintf(_sh, SHELL_INFO, _ft "\n", ##__VA_ARGS__)
1049 
1050 /**
1051  * @brief Print normal message to the shell.
1052  *
1053  * See @ref shell_fprintf.
1054  *
1055  * @param[in] _sh Pointer to the shell instance.
1056  * @param[in] _ft Format string.
1057  * @param[in] ... List of parameters to print.
1058  */
1059 #define shell_print(_sh, _ft, ...) \
1060 	shell_fprintf(_sh, SHELL_NORMAL, _ft "\n", ##__VA_ARGS__)
1061 
1062 /**
1063  * @brief Print warning message to the shell.
1064  *
1065  * See @ref shell_fprintf.
1066  *
1067  * @param[in] _sh Pointer to the shell instance.
1068  * @param[in] _ft Format string.
1069  * @param[in] ... List of parameters to print.
1070  */
1071 #define shell_warn(_sh, _ft, ...) \
1072 	shell_fprintf(_sh, SHELL_WARNING, _ft "\n", ##__VA_ARGS__)
1073 
1074 /**
1075  * @brief Print error message to the shell.
1076  *
1077  * See @ref shell_fprintf.
1078  *
1079  * @param[in] _sh Pointer to the shell instance.
1080  * @param[in] _ft Format string.
1081  * @param[in] ... List of parameters to print.
1082  */
1083 #define shell_error(_sh, _ft, ...) \
1084 	shell_fprintf(_sh, SHELL_ERROR, _ft "\n", ##__VA_ARGS__)
1085 
1086 /**
1087  * @brief Process function, which should be executed when data is ready in the
1088  *	  transport interface. To be used if shell thread is disabled.
1089  *
1090  * @param[in] sh Pointer to the shell instance.
1091  */
1092 void shell_process(const struct shell *sh);
1093 
1094 /**
1095  * @brief Change displayed shell prompt.
1096  *
1097  * @param[in] sh	Pointer to the shell instance.
1098  * @param[in] prompt	New shell prompt.
1099  *
1100  * @return 0		Success.
1101  * @return -EINVAL	Pointer to new prompt is not correct.
1102  */
1103 int shell_prompt_change(const struct shell *sh, const char *prompt);
1104 
1105 /**
1106  * @brief Prints the current command help.
1107  *
1108  * Function will print a help string with: the currently entered command
1109  * and subcommands (if they exist).
1110  *
1111  * @param[in] sh      Pointer to the shell instance.
1112  */
1113 void shell_help(const struct shell *sh);
1114 
1115 /* @brief Command's help has been printed */
1116 #define SHELL_CMD_HELP_PRINTED	(1)
1117 
1118 /** @brief Execute command.
1119  *
1120  * Pass command line to shell to execute.
1121  *
1122  * Note: This by no means makes any of the commands a stable interface, so
1123  *	 this function should only be used for debugging/diagnostic.
1124  *
1125  *	 This function must not be called from shell command context!
1126 
1127  *
1128  * @param[in] sh	Pointer to the shell instance.
1129  *			It can be NULL when the
1130  *			@kconfig{CONFIG_SHELL_BACKEND_DUMMY} option is enabled.
1131  * @param[in] cmd	Command to be executed.
1132  *
1133  * @return		Result of the execution
1134  */
1135 int shell_execute_cmd(const struct shell *sh, const char *cmd);
1136 
1137 /** @brief Set root command for all shell instances.
1138  *
1139  * It allows setting from the code the root command. It is an equivalent of
1140  * calling select command with one of the root commands as the argument
1141  * (e.g "select log") except it sets command for all shell instances.
1142  *
1143  * @param cmd String with one of the root commands or null pointer to reset.
1144  *
1145  * @retval 0 if root command is set.
1146  * @retval -EINVAL if invalid root command is provided.
1147  */
1148 int shell_set_root_cmd(const char *cmd);
1149 
1150 /** @brief Set bypass callback.
1151  *
1152  * Bypass callback is called whenever data is received. Shell is bypassed and
1153  * data is passed directly to the callback. Use null to disable bypass functionality.
1154  *
1155  * @param[in] sh	Pointer to the shell instance.
1156  * @param[in] bypass	Bypass callback or null to disable.
1157  */
1158 void shell_set_bypass(const struct shell *sh, shell_bypass_cb_t bypass);
1159 
1160 /** @brief Get shell readiness to execute commands.
1161  *
1162  * @param[in] sh	Pointer to the shell instance.
1163  *
1164  * @retval true		Shell backend is ready to execute commands.
1165  * @retval false	Shell backend is not initialized or not started.
1166  */
1167 bool shell_ready(const struct shell *sh);
1168 
1169 /**
1170  * @brief Allow application to control text insert mode.
1171  * Value is modified atomically and the previous value is returned.
1172  *
1173  * @param[in] sh	Pointer to the shell instance.
1174  * @param[in] val	Insert mode.
1175  *
1176  * @retval 0 or 1: previous value
1177  * @retval -EINVAL if shell is NULL.
1178  */
1179 int shell_insert_mode_set(const struct shell *sh, bool val);
1180 
1181 /**
1182  * @brief Allow application to control whether terminal output uses colored
1183  * syntax.
1184  * Value is modified atomically and the previous value is returned.
1185  *
1186  * @param[in] sh	Pointer to the shell instance.
1187  * @param[in] val	Color mode.
1188  *
1189  * @retval 0 or 1: previous value
1190  * @retval -EINVAL if shell is NULL.
1191  */
1192 int shell_use_colors_set(const struct shell *sh, bool val);
1193 
1194 /**
1195  * @brief Allow application to control whether terminal is using vt100 commands.
1196  * Value is modified atomically and the previous value is returned.
1197  *
1198  * @param[in] sh	Pointer to the shell instance.
1199  * @param[in] val	vt100 mode.
1200  *
1201  * @retval 0 or 1: previous value
1202  * @retval -EINVAL if shell is NULL.
1203  */
1204 int shell_use_vt100_set(const struct shell *sh, bool val);
1205 
1206 /**
1207  * @brief Allow application to control whether user input is echoed back.
1208  * Value is modified atomically and the previous value is returned.
1209  *
1210  * @param[in] sh	Pointer to the shell instance.
1211  * @param[in] val	Echo mode.
1212  *
1213  * @retval 0 or 1: previous value
1214  * @retval -EINVAL if shell is NULL.
1215  */
1216 int shell_echo_set(const struct shell *sh, bool val);
1217 
1218 /**
1219  * @brief Allow application to control whether user input is obscured with
1220  * asterisks -- useful for implementing passwords.
1221  * Value is modified atomically and the previous value is returned.
1222  *
1223  * @param[in] sh	Pointer to the shell instance.
1224  * @param[in] obscure	Obscure mode.
1225  *
1226  * @retval 0 or 1: previous value.
1227  * @retval -EINVAL if shell is NULL.
1228  */
1229 int shell_obscure_set(const struct shell *sh, bool obscure);
1230 
1231 /**
1232  * @brief Allow application to control whether the delete key backspaces or
1233  * deletes.
1234  * Value is modified atomically and the previous value is returned.
1235  *
1236  * @param[in] sh	Pointer to the shell instance.
1237  * @param[in] val	Delete mode.
1238  *
1239  * @retval 0 or 1: previous value
1240  * @retval -EINVAL if shell is NULL.
1241  */
1242 int shell_mode_delete_set(const struct shell *sh, bool val);
1243 
1244 /**
1245  * @brief Retrieve return value of most recently executed shell command.
1246  *
1247  * @param[in] sh Pointer to the shell instance
1248  *
1249  * @retval return value of previous command
1250  */
1251 int shell_get_return_value(const struct shell *sh);
1252 
1253 /**
1254  * @}
1255  */
1256 
1257 #ifdef __cplusplus
1258 }
1259 #endif
1260 
1261 #endif /* SHELL_H__ */
1262