3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2017 Google LLC
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Header that defines a generic shell API for CHIP examples
31 #ifndef CHIP_SHELL_PROMPT
32 #define CHIP_SHELL_PROMPT "> "
33 #endif // CHIP_SHELL_PROMPT
35 #ifndef CHIP_SHELL_MAX_MODULES
36 #define CHIP_SHELL_MAX_MODULES 10
37 #endif // CHIP_SHELL_MAX_MODULES
39 #ifndef CHIP_SHELL_MAX_LINE_SIZE
40 #define CHIP_SHELL_MAX_LINE_SIZE 256
41 #endif // CHIP_SHELL_MAX_LINE_SIZE
43 #ifndef CHIP_SHELL_MAX_TOKENS
44 #define CHIP_SHELL_MAX_TOKENS 10
45 #endif // CHIP_SHELL_MAX_TOKENS
51 * Callback to execute an individual shell command.
53 * @param argc Number of arguments passed.
54 * @param argv Array of option strings. The command name is not included.
56 * @return 0 on success; CHIP_ERROR[...] on failure.
58 typedef int shell_command_fn(int argc, char * argv[]);
61 * Descriptor structure for a single command.
63 * Typically a set of commands are defined as an array of this structure
64 * and passed to the `shell_register()` during application initialization.
66 * An example command set definition follows:
68 * static shell_command_t cmds[] = {
69 * { &cmd_echo, "echo", "Echo back provided inputs" },
70 * { &cmd_exit, "exit", "Exit the shell application" },
71 * { &cmd_help, "help", "List out all top level commands" },
72 * { &cmd_version, "version", "Output the software version" },
77 shell_command_fn * cmd_func;
78 const char * cmd_name;
79 const char * cmd_help;
82 typedef const struct shell_command shell_command_t;
85 * Execution callback for a shell command.
87 * @param command The shell command being iterated.
88 * @param arg A context variable passed to the iterator function.
90 * @return 0 continue iteration; 1 break iteration.
92 typedef int shell_command_iterator_t(shell_command_t * command, void * arg);
97 static Shell theShellRoot;
99 shell_command_t * _commandSet[CHIP_SHELL_MAX_MODULES];
100 unsigned _commandSetSize[CHIP_SHELL_MAX_MODULES];
101 unsigned _commandSetCount;
104 * Registers a set of defaults commands (help) for all Shell and sub-Shell instances.
106 * help - list the top-level brief of all registered commands
107 * echo - echo back all argument characters passed
108 * exit - quit out of the shell
109 * version - return the version of the CHIP library
111 void RegisterDefaultCommands();
116 /** Return the root singleton for the Shell command hierarchy. */
117 static Shell & Root() { return theShellRoot; }
120 * Execution callback for a shell command.
122 * @param on_command An iterator callback to be called for each command.
123 * @param arg A context variable to be passed to each command iterated.
125 void ForEachCommand(shell_command_iterator_t * on_command, void * arg);
128 * Dispatch and execute the command for the given argument list.
130 * @param argc Number of arguments in argv.
131 * @param argv Array of arguments in the tokenized command line to execute.
133 * @return 0 on success; CHIP_ERROR[...] on failure.
135 int ExecCommand(int argc, char * argv[]);
138 * Registers a command set, or array of commands with the shell.
140 * @param command_set An array of commands to add to the shell.
141 * @param count The number of commands in the command set array.
143 void RegisterCommands(shell_command_t * command_set, unsigned count);
146 * Utility function for converting a raw line typed into a shell into an array of words or tokens.
148 * @param buffer String of the raw line typed into shell.
149 * @param tokens Array of words to be created by the tokenizer.
150 * This array will point to the same memory as passed in
151 * via buffer. Spaces will be replaced with NULL characters.
152 * @param max_tokens Maximum size of token array.
154 * @return Number of tokens generated (argc).
156 static int TokenizeLine(char * buffer, char ** tokens, int max_tokens);
159 * Main loop for shell.
161 * @param arg Unused context block for shell task to comply with task function syntax.
163 static void TaskLoop(void * arg);
166 /** Utility macro for running ForEachCommand on root shell. */
167 static inline void shell_command_foreach(shell_command_iterator_t * on_command, void * arg)
169 return Shell::Root().ForEachCommand(on_command, arg);
172 /** Utility macro for running ForEachCommand on Root shell. */
173 static inline void shell_register(shell_command_t * command_set, unsigned count)
175 return Shell::Root().RegisterCommands(command_set, count);
178 /** Utility macro for to tokenize an input line. */
179 static inline int shell_line_tokenize(char * buffer, char ** tokens, int max_tokens)
181 return Shell::TokenizeLine(buffer, tokens, max_tokens);
184 /** Utility macro to run main shell task loop. */
185 static inline void shell_task(void * arg)
187 return Shell::TaskLoop(arg);