1.. _shell_api: 2 3Shell 4###### 5 6.. contents:: 7 :local: 8 :depth: 2 9 10Overview 11******** 12 13This module allows you to create and handle a shell with a user-defined command 14set. You can use it in examples where more than simple button or LED user 15interaction is required. This module is a Unix-like shell with these features: 16 17* Support for multiple instances. 18* Advanced cooperation with the :ref:`logging_api`. 19* Support for static and dynamic commands. 20* Support for dictionary commands. 21* Smart command completion with the :kbd:`Tab` key. 22* Built-in commands: :command:`clear`, :command:`shell`, :command:`colors`, 23 :command:`echo`, :command:`history` and :command:`resize`. 24* Viewing recently executed commands using keys: :kbd:`↑` :kbd:`↓` or meta keys. 25* Text edition using keys: :kbd:`←`, :kbd:`→`, :kbd:`Backspace`, 26 :kbd:`Delete`, :kbd:`End`, :kbd:`Home`, :kbd:`Insert`. 27* Support for ANSI escape codes: ``VT100`` and ``ESC[n~`` for cursor control 28 and color printing. 29* Support for editing multiline commands. 30* Built-in handler to display help for the commands. 31* Support for wildcards: ``*`` and ``?``. 32* Support for meta keys. 33* Support for getopt and getopt_long. 34* Kconfig configuration to optimize memory usage. 35 36.. note:: 37 Some of these features have a significant impact on RAM and flash usage, 38 but many can be disabled when not needed. To default to options which 39 favor reduced RAM and flash requirements instead of features, you should 40 enable :kconfig:option:`CONFIG_SHELL_MINIMAL` and selectively enable just the 41 features you want. 42 43.. _backends: 44 45Backends 46******** 47 48The module can be connected to any transport for command input and output. 49At this point, the following transport layers are implemented: 50 51* MQTT 52* Segger RTT 53* SMP 54* Telnet 55* UART 56* USB 57* Bluetooth LE (NUS) 58* RPMSG 59* DUMMY - not a physical transport layer. 60 61Telnet 62====== 63 64Enabling :kconfig:option:`CONFIG_SHELL_BACKEND_TELNET` will allow users to use telnet 65as a shell backend. Connecting to it can be done using PuTTY or any ``telnet`` client. 66For example: 67 68.. code-block:: none 69 70 telnet <ip address> <port> 71 72By default the telnet client won't handle telnet commands and configuration. Although 73command support can be enabled with :kconfig:option:`CONFIG_SHELL_TELNET_SUPPORT_COMMAND`. 74This will give the telnet client access to a very limited set of supported commands but 75still can be turned on if needed. One of the command options it supports is the ``ECHO`` 76option. This will allow the client to be in character mode (character at a time), 77similar to a UART backend in that regard. This will make the client send a character 78as soon as it is typed having the effect of increasing the network traffic 79considerably. For that cost, it will enable the line editing, 80`tab completion <tab-feature_>`_, and `history <history-feature_>`_ 81features of the shell. 82 83USB CDC ACM 84=========== 85 86To configure Shell USB CDC ACM backend, simply add the snippet ``cdc-acm-console`` 87to your build: 88 89.. code-block:: console 90 91 west build -S cdc-acm-console [...] 92 93Details on the configuration settings are captured in the following files: 94 95- :zephyr_file:`snippets/cdc-acm-console/cdc-acm-console.conf`. 96- :zephyr_file:`snippets/cdc-acm-console/cdc-acm-console.overlay`. 97 98Bluetooth LE (NUS) 99================== 100 101To configure Bluetooth LE (NUS) backend, simply add the snippet ``nus-console`` 102to your build: 103 104.. code-block:: console 105 106 west build -S nus-console [...] 107 108Details on the configuration settings are captured in the following files: 109 110- :zephyr_file:`snippets/nus-console/nus-console.conf`. 111- :zephyr_file:`snippets/nus-console/nus-console.overlay`. 112 113Segget RTT 114========== 115 116To configure Segger RTT backend, add the following configurations to your build: 117 118- :kconfig:option:`CONFIG_USE_SEGGER_RTT` 119- :kconfig:option:`CONFIG_SHELL_BACKEND_RTT` 120- :kconfig:option:`CONFIG_SHELL_BACKEND_SERIAL` 121 122Details on additional configuration settings are captured in: 123:zephyr_file:`samples/subsys/shell/shell_module/prj_minimal_rtt.conf`. 124 125.. _shell_rtt_putty: 126 127Using PuTTY 128----------- 129 130Use following procedure: 131 132* Open debug session and continue running the application. 133 134 .. code-block:: none 135 136 west attach 137 138* Open ``PuTTY``. Use telnet port 19021 and specific Terminal configuration. Set ``Local echo`` 139 to ``Force off`` and ``Local line editing`` to ``Force off`` (see image below). 140 141 142.. image:: images/putty_rtt.png 143 :align: center 144 :alt: RTT PuTTY terminal configuration. 145 146* Now you should have a network connection to RTT that will let you enter input 147 to the shell. 148 149Connecting to Segger RTT via TCP (on macOS, for example) 150-------------------------------------------------------- 151 152On macOS JLinkRTTClient won't let you enter input. Instead, please use following 153procedure: 154 155* Open up a first Terminal window and enter: 156 157 .. code-block:: none 158 159 JLinkRTTLogger -Device NRF52840_XXAA -RTTChannel 1 -if SWD -Speed 4000 ~/rtt.log 160 161 (change device if required) 162 163* Open up a second Terminal window and enter: 164 165 .. code-block:: none 166 167 nc localhost 19021 168 169* Now you should have a network connection to RTT that will let you enter input 170 to the shell. However, contrary to `PuTTY <shell_rtt_putty_>`_ some features like 171 ``Tab`` completion do not work. 172 173 174Commands 175******** 176 177Shell commands are organized in a tree structure and grouped into the following 178types: 179 180* Root command (level 0): Gathered and alphabetically sorted in a dedicated 181 memory section. 182* Static subcommand (level > 0): Number and syntax must be known during compile 183 time. Created in the software module. 184* Dynamic subcommand (level > 0): Number and syntax does not need to be known 185 during compile time. Created in the software module. 186 187 188Commonly-used command groups 189============================ 190 191The following list is a set of useful command groups and how to enable them: 192 193GPIO 194---- 195 196- :kconfig:option:`CONFIG_GPIO` 197- :kconfig:option:`CONFIG_GPIO_SHELL` 198 199I2C 200--- 201 202- :kconfig:option:`CONFIG_I2C` 203- :kconfig:option:`CONFIG_I2C_SHELL` 204 205Sensor 206------ 207 208- :kconfig:option:`CONFIG_SENSOR` 209- :kconfig:option:`CONFIG_SENSOR_SHELL` 210 211Flash 212----- 213 214- :kconfig:option:`CONFIG_FLASH` 215- :kconfig:option:`CONFIG_FLASH_SHELL` 216 217File-System 218----------- 219 220- :kconfig:option:`CONFIG_FILE_SYSTEM` 221- :kconfig:option:`CONFIG_FILE_SYSTEM_SHELL` 222 223Creating commands 224================= 225 226Use the following macros for adding shell commands: 227 228* :c:macro:`SHELL_CMD_REGISTER` - Create root command. All root commands must 229 have different name. 230* :c:macro:`SHELL_COND_CMD_REGISTER` - Conditionally (if compile time flag is 231 set) create root command. All root commands must have different name. 232* :c:macro:`SHELL_CMD_ARG_REGISTER` - Create root command with arguments. 233 All root commands must have different name. 234* :c:macro:`SHELL_COND_CMD_ARG_REGISTER` - Conditionally (if compile time flag 235 is set) create root command with arguments. All root commands must have 236 different name. 237* :c:macro:`SHELL_CMD` - Initialize a command. 238* :c:macro:`SHELL_COND_CMD` - Initialize a command if compile time flag is set. 239* :c:macro:`SHELL_EXPR_CMD` - Initialize a command if compile time expression is 240 non-zero. 241* :c:macro:`SHELL_CMD_ARG` - Initialize a command with arguments. 242* :c:macro:`SHELL_COND_CMD_ARG` - Initialize a command with arguments if compile 243 time flag is set. 244* :c:macro:`SHELL_EXPR_CMD_ARG` - Initialize a command with arguments if compile 245 time expression is non-zero. 246* :c:macro:`SHELL_STATIC_SUBCMD_SET_CREATE` - Create a static subcommands 247 array. 248* :c:macro:`SHELL_SUBCMD_DICT_SET_CREATE` - Create a dictionary subcommands 249 array. 250* :c:macro:`SHELL_DYNAMIC_CMD_CREATE` - Create a dynamic subcommands array. 251 252Commands can be created in any file in the system that includes 253:zephyr_file:`include/zephyr/shell/shell.h`. All created commands are available for all 254shell instances. 255 256Static commands 257--------------- 258 259Example code demonstrating how to create a root command with static 260subcommands. 261 262.. image:: images/static_cmd.PNG 263 :align: center 264 :alt: Command tree with static commands. 265 266.. code-block:: c 267 268 /* Creating subcommands (level 1 command) array for command "demo". */ 269 SHELL_STATIC_SUBCMD_SET_CREATE(sub_demo, 270 SHELL_CMD(params, NULL, "Print params command.", 271 cmd_demo_params), 272 SHELL_CMD(ping, NULL, "Ping command.", cmd_demo_ping), 273 SHELL_SUBCMD_SET_END 274 ); 275 /* Creating root (level 0) command "demo" */ 276 SHELL_CMD_REGISTER(demo, &sub_demo, "Demo commands", NULL); 277 278Example implementation can be found under following location: 279:zephyr_file:`samples/subsys/shell/shell_module/src/main.c`. 280 281Dictionary commands 282=================== 283This is a special kind of static commands. Dictionary commands can be used 284every time you want to use a pair: (string <-> corresponding data) in 285a command handler. The string is usually a verbal description of a given data. 286The idea is to use the string as a command syntax that can be prompted by the 287shell and corresponding data can be used to process the command. 288 289Let's use an example. Suppose you created a command to set an ADC gain. 290It is a perfect place where a dictionary can be used. The dictionary would 291be a set of pairs: (string: gain_value, int: value) where int value could 292be used with the ADC driver API. 293 294Abstract code for this task would look like this: 295 296.. code-block:: c 297 298 static int gain_cmd_handler(const struct shell *sh, 299 size_t argc, char **argv, void *data) 300 { 301 int gain; 302 303 /* data is a value corresponding to called command syntax */ 304 gain = (int)data; 305 adc_set_gain(gain); 306 307 shell_print(sh, "ADC gain set to: %s\n" 308 "Value send to ADC driver: %d", 309 argv[0], 310 gain); 311 312 return 0; 313 } 314 315 SHELL_SUBCMD_DICT_SET_CREATE(sub_gain, gain_cmd_handler, 316 (gain_1, 1, "gain 1"), (gain_2, 2, "gain 2"), 317 (gain_1_2, 3, "gain 1/2"), (gain_1_4, 4, "gain 1/4") 318 ); 319 SHELL_CMD_REGISTER(gain, &sub_gain, "Set ADC gain", NULL); 320 321 322This is how it would look like in the shell: 323 324.. image:: images/dict_cmd.png 325 :align: center 326 :alt: Dictionary commands example. 327 328Dynamic commands 329---------------- 330 331Example code demonstrating how to create a root command with static and dynamic 332subcommands. At the beginning dynamic command list is empty. New commands 333can be added by typing: 334 335.. code-block:: none 336 337 dynamic add <new_dynamic_command> 338 339Newly added commands can be prompted or autocompleted with the :kbd:`Tab` key. 340 341.. image:: images/dynamic_cmd.PNG 342 :align: center 343 :alt: Command tree with static and dynamic commands. 344 345.. code-block:: c 346 347 /* Buffer for 10 dynamic commands */ 348 static char dynamic_cmd_buffer[10][50]; 349 350 /* commands counter */ 351 static uint8_t dynamic_cmd_cnt; 352 353 /* Function returning command dynamically created 354 * in dynamic_cmd_buffer. 355 */ 356 static void dynamic_cmd_get(size_t idx, 357 struct shell_static_entry *entry) 358 { 359 if (idx < dynamic_cmd_cnt) { 360 entry->syntax = dynamic_cmd_buffer[idx]; 361 entry->handler = NULL; 362 entry->subcmd = NULL; 363 entry->help = "Show dynamic command name."; 364 } else { 365 /* if there are no more dynamic commands available 366 * syntax must be set to NULL. 367 */ 368 entry->syntax = NULL; 369 } 370 } 371 372 SHELL_DYNAMIC_CMD_CREATE(m_sub_dynamic_set, dynamic_cmd_get); 373 SHELL_STATIC_SUBCMD_SET_CREATE(m_sub_dynamic, 374 SHELL_CMD(add, NULL,"Add new command to dynamic_cmd_buffer and" 375 " sort them alphabetically.", 376 cmd_dynamic_add), 377 SHELL_CMD(execute, &m_sub_dynamic_set, 378 "Execute a command.", cmd_dynamic_execute), 379 SHELL_CMD(remove, &m_sub_dynamic_set, 380 "Remove a command from dynamic_cmd_buffer.", 381 cmd_dynamic_remove), 382 SHELL_CMD(show, NULL, 383 "Show all commands in dynamic_cmd_buffer.", 384 cmd_dynamic_show), 385 SHELL_SUBCMD_SET_END 386 ); 387 SHELL_CMD_REGISTER(dynamic, &m_sub_dynamic, 388 "Demonstrate dynamic command usage.", cmd_dynamic); 389 390Example implementation can be found under following location: 391:zephyr_file:`samples/subsys/shell/shell_module/src/dynamic_cmd.c`. 392 393Commands execution 394================== 395 396Each command or subcommand may have a handler. The shell executes the handler 397that is found deepest in the command tree and further subcommands (without a 398handler) are passed as arguments. Characters within parentheses are treated 399as one argument. If shell won't find a handler it will display an error message. 400 401Commands can be also executed from a user application using any active backend 402and a function :c:func:`shell_execute_cmd`, as shown in this example: 403 404.. code-block:: c 405 406 int main(void) 407 { 408 /* Below code will execute "clear" command on a DUMMY backend */ 409 shell_execute_cmd(NULL, "clear"); 410 411 /* Below code will execute "shell colors off" command on 412 * an UART backend 413 */ 414 shell_execute_cmd(shell_backend_uart_get_ptr(), 415 "shell colors off"); 416 } 417 418Enable the DUMMY backend by setting the Kconfig 419:kconfig:option:`CONFIG_SHELL_BACKEND_DUMMY` option. 420 421Commands execution example 422-------------------------- 423 424Let's assume a command structure as in the following figure, where: 425 426* :c:macro:`root_cmd` - root command without a handler 427* :c:macro:`cmd_xxx_h` - command has a handler 428* :c:macro:`cmd_xxx` - command does not have a handler 429 430.. image:: images/execution.png 431 :align: center 432 :alt: Command tree with static commands. 433 434Example 1 435^^^^^^^^^ 436Sequence: :c:macro:`root_cmd` :c:macro:`cmd_1_h` :c:macro:`cmd_12_h` 437:c:macro:`cmd_121_h` :c:macro:`parameter` will execute command 438:c:macro:`cmd_121_h` and :c:macro:`parameter` will be passed as an argument. 439 440Example 2 441^^^^^^^^^ 442Sequence: :c:macro:`root_cmd` :c:macro:`cmd_2` :c:macro:`cmd_22_h` 443:c:macro:`parameter1` :c:macro:`parameter2` will execute command 444:c:macro:`cmd_22_h` and :c:macro:`parameter1` :c:macro:`parameter2` 445will be passed as an arguments. 446 447Example 3 448^^^^^^^^^ 449Sequence: :c:macro:`root_cmd` :c:macro:`cmd_1_h` :c:macro:`parameter1` 450:c:macro:`cmd_121_h` :c:macro:`parameter2` will execute command 451:c:macro:`cmd_1_h` and :c:macro:`parameter1`, :c:macro:`cmd_121_h` and 452:c:macro:`parameter2` will be passed as an arguments. 453 454Example 4 455^^^^^^^^^ 456Sequence: :c:macro:`root_cmd` :c:macro:`parameter` :c:macro:`cmd_121_h` 457:c:macro:`parameter2` will not execute any command. 458 459 460Command handler 461---------------- 462 463Simple command handler implementation: 464 465.. code-block:: c 466 467 static int cmd_handler(const struct shell *sh, size_t argc, 468 char **argv) 469 { 470 ARG_UNUSED(argc); 471 ARG_UNUSED(argv); 472 473 shell_fprintf(shell, SHELL_INFO, "Print info message\n"); 474 475 shell_print(sh, "Print simple text."); 476 477 shell_warn(sh, "Print warning text."); 478 479 shell_error(sh, "Print error text."); 480 481 return 0; 482 } 483 484Function :c:func:`shell_fprintf` or the shell print macros: 485:c:macro:`shell_print`, :c:macro:`shell_info`, :c:macro:`shell_warn` and 486:c:macro:`shell_error` can be used from the command handler or from threads, 487but not from an interrupt context. Instead, interrupt handlers should use 488:ref:`logging_api` for printing. 489 490Command help 491------------ 492 493Every user-defined command or subcommand can have its own help description. 494The help for commands and subcommands can be created with respective macros: 495:c:macro:`SHELL_CMD_REGISTER`, :c:macro:`SHELL_CMD_ARG_REGISTER`, 496:c:macro:`SHELL_CMD`, and :c:macro:`SHELL_CMD_ARG`. 497 498Shell prints this help message when you call a command 499or subcommand with ``-h`` or ``--help`` parameter. 500 501Parent commands 502--------------- 503 504In the subcommand handler, you can access both the parameters passed to 505commands or the parent commands, depending on how you index ``argv``. 506 507* When indexing ``argv`` with positive numbers, you can access the parameters. 508* When indexing ``argv`` with negative numbers, you can access the parent 509 commands. 510* The subcommand to which the handler belongs has the ``argv`` index of 0. 511 512.. code-block:: c 513 514 static int cmd_handler(const struct shell *sh, size_t argc, 515 char **argv) 516 { 517 ARG_UNUSED(argc); 518 519 /* If it is a subcommand handler parent command syntax 520 * can be found using argv[-1]. 521 */ 522 shell_print(sh, "This command has a parent command: %s", 523 argv[-1]); 524 525 /* Print this command syntax */ 526 shell_print(sh, "This command syntax is: %s", argv[0]); 527 528 /* Print first argument */ 529 shell_print(sh, "%s", argv[1]); 530 531 return 0; 532 } 533 534Built-in commands 535================= 536 537These commands are activated by :kconfig:option:`CONFIG_SHELL_CMDS` set to ``y``. 538 539* :command:`clear` - Clears the screen. 540* :command:`history` - Shows the recently entered commands. 541* :command:`resize` - Must be executed when terminal width is different than 80 542 characters or after each change of terminal width. It ensures proper 543 multiline text display and :kbd:`←`, :kbd:`→`, :kbd:`End`, :kbd:`Home` keys 544 handling. Currently this command works only with UART flow control switched 545 on. It can be also called with a subcommand: 546 547 * :command:`default` - Shell will send terminal width = 80 to the 548 terminal and assume successful delivery. 549 550 These command needs extra activation: 551 :kconfig:option:`CONFIG_SHELL_CMDS_RESIZE` set to ``y``. 552* :command:`select` - It can be used to set new root command. Exit to main 553 command tree is with alt+r. This command needs extra activation: 554 :kconfig:option:`CONFIG_SHELL_CMDS_SELECT` set to ``y``. 555* :command:`shell` - Root command with useful shell-related subcommands like: 556 557 * :command:`echo` - Toggles shell echo. 558 * :command:`colors` - Toggles colored syntax. This might be helpful in 559 case of Bluetooth shell to limit the amount of transferred bytes. 560 * :command:`stats` - Shows shell statistics. 561 562.. _tab-feature: 563 564Tab Feature 565*********** 566 567The Tab button can be used to suggest commands or subcommands. This feature 568is enabled by :kconfig:option:`CONFIG_SHELL_TAB` set to ``y``. 569It can also be used for partial or complete auto-completion of commands. 570This feature is activated by 571:kconfig:option:`CONFIG_SHELL_TAB_AUTOCOMPLETION` set to ``y``. 572When user starts writing a command and presses the :kbd:`Tab` button then 573the shell will do one of 3 possible things: 574 575* Autocomplete the command. 576* Prompts available commands and if possible partly completes the command. 577* Will not do anything if there are no available or matching commands. 578 579.. image:: images/tab_prompt.png 580 :align: center 581 :alt: Tab Feature usage example 582 583.. _history-feature: 584 585History Feature 586*************** 587 588This feature enables commands history in the shell. It is activated by: 589:kconfig:option:`CONFIG_SHELL_HISTORY` set to ``y``. History can be accessed 590using keys: :kbd:`↑` :kbd:`↓` or :kbd:`Ctrl+n` and :kbd:`Ctrl+p` 591if meta keys are active. 592Number of commands that can be stored depends on size 593of :kconfig:option:`CONFIG_SHELL_HISTORY_BUFFER` parameter. 594 595Wildcards Feature 596***************** 597 598The shell module can handle wildcards. Wildcards are interpreted correctly 599when expanded command and its subcommands do not have a handler. For example, 600if you want to set logging level to ``err`` for the ``app`` and ``app_test`` 601modules you can execute the following command: 602 603.. code-block:: none 604 605 log enable err a* 606 607.. image:: images/wildcard.png 608 :align: center 609 :alt: Wildcard usage example 610 611This feature is activated by :kconfig:option:`CONFIG_SHELL_WILDCARD` set to ``y``. 612 613Meta Keys Feature 614***************** 615 616The shell module supports the following meta keys: 617 618.. list-table:: Implemented meta keys 619 :widths: 10 40 620 :header-rows: 1 621 622 * - Meta keys 623 - Action 624 * - :kbd:`Ctrl+a` 625 - Moves the cursor to the beginning of the line. 626 * - :kbd:`Ctrl+b` 627 - Moves the cursor backward one character. 628 * - :kbd:`Ctrl+c` 629 - Preserves the last command on the screen and starts a new command in 630 a new line. 631 * - :kbd:`Ctrl+d` 632 - Deletes the character under the cursor. 633 * - :kbd:`Ctrl+e` 634 - Moves the cursor to the end of the line. 635 * - :kbd:`Ctrl+f` 636 - Moves the cursor forward one character. 637 * - :kbd:`Ctrl+k` 638 - Deletes from the cursor to the end of the line. 639 * - :kbd:`Ctrl+l` 640 - Clears the screen and leaves the currently typed command at the top of 641 the screen. 642 * - :kbd:`Ctrl+n` 643 - Moves in history to next entry. 644 * - :kbd:`Ctrl+p` 645 - Moves in history to previous entry. 646 * - :kbd:`Ctrl+u` 647 - Clears the currently typed command. 648 * - :kbd:`Ctrl+w` 649 - Removes the word or part of the word to the left of the cursor. Words 650 separated by period instead of space are treated as one word. 651 * - :kbd:`Alt+b` 652 - Moves the cursor backward one word. 653 * - :kbd:`Alt+f` 654 - Moves the cursor forward one word. 655 656This feature is activated by :kconfig:option:`CONFIG_SHELL_METAKEYS` set to ``y``. 657 658Getopt Feature 659***************** 660 661Some shell users apart from subcommands might need to use options as well. 662the arguments string, looking for supported options. Typically, this task 663is accomplished by the ``getopt`` family functions. 664 665For this purpose shell supports the getopt and getopt_long libraries available 666in the FreeBSD project. This feature is activated by: 667:kconfig:option:`CONFIG_POSIX_C_LIB_EXT` set to ``y`` and :kconfig:option:`CONFIG_GETOPT_LONG` 668set to ``y``. 669 670This feature can be used in thread safe as well as non thread safe manner. 671The former is full compatible with regular getopt usage while the latter 672a bit differs. 673 674An example non-thread safe usage: 675 676.. code-block:: c 677 678 char *cvalue = NULL; 679 while ((char c = getopt(argc, argv, "abhc:")) != -1) { 680 switch (c) { 681 case 'c': 682 cvalue = optarg; 683 break; 684 default: 685 break; 686 } 687 } 688 689An example thread safe usage: 690 691.. code-block:: c 692 693 char *cvalue = NULL; 694 struct getopt_state *state; 695 while ((char c = getopt(argc, argv, "abhc:")) != -1) { 696 state = getopt_state_get(); 697 switch (c) { 698 case 'c': 699 cvalue = state->optarg; 700 break; 701 default: 702 break; 703 } 704 } 705 706Thread safe getopt functionality is activated by 707:kconfig:option:`CONFIG_SHELL_GETOPT` set to ``y``. 708 709Obscured Input Feature 710********************** 711 712With the obscured input feature, the shell can be used for implementing a login 713prompt or other user interaction whereby the characters the user types should 714not be revealed on screen, such as when entering a password. 715 716Once the obscured input has been accepted, it is normally desired to return the 717shell to normal operation. Such runtime control is possible with the 718``shell_obscure_set`` function. 719 720An example of login and logout commands using this feature is located in 721:zephyr_file:`samples/subsys/shell/shell_module/src/main.c` and the config file 722:zephyr_file:`samples/subsys/shell/shell_module/prj_login.conf`. 723 724This feature is activated upon startup by :kconfig:option:`CONFIG_SHELL_START_OBSCURED` 725set to ``y``. With this set either way, the option can still be controlled later 726at runtime. :kconfig:option:`CONFIG_SHELL_CMDS_SELECT` is useful to prevent entry 727of any other command besides a login command, by means of the 728``shell_set_root_cmd`` function. Likewise, :kconfig:option:`CONFIG_SHELL_PROMPT_UART` 729allows you to set the prompt upon startup, but it can be changed later with the 730``shell_prompt_change`` function. 731 732Shell Logger Backend Feature 733**************************** 734 735Shell instance can act as the :ref:`logging_api` backend. Shell ensures that log 736messages are correctly multiplexed with shell output. Log messages from logger 737thread are enqueued and processed in the shell thread. Logger thread will block 738for configurable amount of time if queue is full, blocking logger thread context 739for that time. Oldest log message is removed from the queue after timeout and 740new message is enqueued. Use the ``shell stats show`` command to retrieve 741number of log messages dropped by the shell instance. Log queue size and timeout 742are :c:macro:`SHELL_DEFINE` arguments. 743 744This feature is activated by: :kconfig:option:`CONFIG_SHELL_LOG_BACKEND` set to ``y``. 745 746.. warning:: 747 Enqueuing timeout must be set carefully when multiple backends are used 748 in the system. The shell instance could have a slow transport or could 749 block, for example, by a UART with hardware flow control. If timeout is 750 set too high, the logger thread could be blocked and impact other logger 751 backends. 752 753.. warning:: 754 As the shell is a complex logger backend, it can not output logs if 755 the application crashes before the shell thread is running. In this 756 situation, you can enable one of the simple logging backends instead, 757 such as UART (:kconfig:option:`CONFIG_LOG_BACKEND_UART`) or 758 RTT (:kconfig:option:`CONFIG_LOG_BACKEND_RTT`), which are available earlier 759 during system initialization. 760 761RTT Backend Channel Selection 762***************************** 763 764Instead of using the shell as a logger backend, RTT shell backend and RTT log 765backend can also be used simultaneously, but over different channels. By 766separating them, the log can be captured or monitored without shell output or 767the shell may be scripted without log interference. Enabling both the Shell RTT 768backend and the Log RTT backend does not work by default, because both default 769to channel ``0``. There are two options: 770 7711. The Shell buffer can use an alternate channel, for example using 772:kconfig:option:`CONFIG_SHELL_BACKEND_RTT_BUFFER` set to ``1``. 773This allows monitoring the log using `JLinkRTTViewer 774<https://www.segger.com/products/debug-probes/j-link/technology/about-real-time-transfer/#j-link-rtt-viewer>`_ 775while a script interfaces over channel 1. 776 7772. The Log buffer can use an alternate channel, for example using 778:kconfig:option:`CONFIG_LOG_BACKEND_RTT_BUFFER` set to ``1``. 779This allows interactive use of the shell through JLinkRTTViewer, while the log 780is written to file. 781 782See `shell backends <backends_>`_ for details on how to enable RTT as a Shell backend. 783 784Usage 785***** 786 787The following code shows a simple use case of this library: 788 789.. code-block:: c 790 791 int main(void) 792 { 793 794 } 795 796 static int cmd_demo_ping(const struct shell *sh, size_t argc, 797 char **argv) 798 { 799 ARG_UNUSED(argc); 800 ARG_UNUSED(argv); 801 802 shell_print(sh, "pong"); 803 return 0; 804 } 805 806 static int cmd_demo_params(const struct shell *sh, size_t argc, 807 char **argv) 808 { 809 int cnt; 810 811 shell_print(sh, "argc = %d", argc); 812 for (cnt = 0; cnt < argc; cnt++) { 813 shell_print(sh, " argv[%d] = %s", cnt, argv[cnt]); 814 } 815 return 0; 816 } 817 818 /* Creating subcommands (level 1 command) array for command "demo". */ 819 SHELL_STATIC_SUBCMD_SET_CREATE(sub_demo, 820 SHELL_CMD(params, NULL, "Print params command.", 821 cmd_demo_params), 822 SHELL_CMD(ping, NULL, "Ping command.", cmd_demo_ping), 823 SHELL_SUBCMD_SET_END 824 ); 825 /* Creating root (level 0) command "demo" without a handler */ 826 SHELL_CMD_REGISTER(demo, &sub_demo, "Demo commands", NULL); 827 828 /* Creating root (level 0) command "version" */ 829 SHELL_CMD_REGISTER(version, NULL, "Show kernel version", cmd_version); 830 831 832Users may use the :kbd:`Tab` key to complete a command/subcommand or to see the 833available subcommands for the currently entered command level. 834For example, when the cursor is positioned at the beginning of the command 835line and the :kbd:`Tab` key is pressed, the user will see all root (level 0) 836commands: 837 838.. code-block:: none 839 840 clear demo shell history log resize version 841 842 843.. note:: 844 To view the subcommands that are available for a specific command, you 845 must first type a :kbd:`space` after this command and then hit 846 :kbd:`Tab`. 847 848These commands are registered by various modules, for example: 849 850* :command:`clear`, :command:`shell`, :command:`history`, and :command:`resize` 851 are built-in commands which have been registered by 852 :zephyr_file:`subsys/shell/shell.c` 853* :command:`demo` and :command:`version` have been registered in example code 854 above by main.c 855* :command:`log` has been registered by :zephyr_file:`subsys/logging/log_cmds.c` 856 857Then, if a user types a :command:`demo` command and presses the :kbd:`Tab` key, 858the shell will only print the subcommands registered for this command: 859 860.. code-block:: none 861 862 params ping 863 864API Reference 865************* 866 867.. doxygengroup:: shell_api 868