esp_console.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2016-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stddef.h>
  19. #include "sdkconfig.h"
  20. #include "esp_err.h"
  21. // Forward declaration. Definition in linenoise/linenoise.h.
  22. typedef struct linenoiseCompletions linenoiseCompletions;
  23. /**
  24. * @brief Parameters for console initialization
  25. */
  26. typedef struct {
  27. size_t max_cmdline_length; //!< length of command line buffer, in bytes
  28. size_t max_cmdline_args; //!< maximum number of command line arguments to parse
  29. int hint_color; //!< ASCII color code of hint text
  30. int hint_bold; //!< Set to 1 to print hint text in bold
  31. } esp_console_config_t;
  32. /**
  33. * @brief Default console configuration value
  34. *
  35. */
  36. #define ESP_CONSOLE_CONFIG_DEFAULT() \
  37. { \
  38. .max_cmdline_length = 256, \
  39. .max_cmdline_args = 32, \
  40. .hint_color = 39, \
  41. .hint_bold = 0 \
  42. }
  43. /**
  44. * @brief Parameters for console REPL (Read Eval Print Loop)
  45. *
  46. */
  47. typedef struct {
  48. uint32_t max_history_len; //!< maximum length for the history
  49. const char *history_save_path; //!< file path used to save history commands, set to NULL won't save to file system
  50. uint32_t task_stack_size; //!< repl task stack size
  51. uint32_t task_priority; //!< repl task priority
  52. const char *prompt; //!< prompt (NULL represents default: "esp> ")
  53. union {
  54. struct {
  55. int channel; //!< UART channel
  56. uint32_t baud_rate; //!< Comunication baud rate
  57. int tx_gpio; //!< GPIO number for TX path, -1 means using the default
  58. int rx_gpio; //!< GPIO number for RX path, -1 means using the default
  59. } uart; //!< UART specific configuration
  60. } device; //!< device configuration
  61. } esp_console_repl_config_t;
  62. #ifdef CONFIG_ESP_CONSOLE_UART_NUM
  63. #define CONSOLE_DEFAULT_UART_CHANNEL CONFIG_ESP_CONSOLE_UART_NUM
  64. #else
  65. #define CONSOLE_DEFAULT_UART_CHANNEL 0
  66. #endif
  67. #ifdef CONFIG_ESP_CONSOLE_UART_BAUDRATE
  68. #define CONSOLE_DEFAULT_UART_BAUDRATE CONFIG_ESP_CONSOLE_UART_BAUDRATE
  69. #else
  70. #define CONSOLE_DEFAULT_UART_BAUDRATE 115200
  71. #endif
  72. #ifdef CONFIG_ESP_CONSOLE_UART_TX_GPIO
  73. #define CONSOLE_DEFAULT_UART_TX_GPIO CONFIG_ESP_CONSOLE_UART_TX_GPIO
  74. #else
  75. #define CONSOLE_DEFAULT_UART_TX_GPIO 1
  76. #endif
  77. #ifdef CONFIG_ESP_CONSOLE_UART_RX_GPIO
  78. #define CONSOLE_DEFAULT_UART_RX_GPIO CONFIG_ESP_CONSOLE_UART_RX_GPIO
  79. #else
  80. #define CONSOLE_DEFAULT_UART_RX_GPIO 3
  81. #endif
  82. /**
  83. * @brief Default console repl configuration value
  84. *
  85. */
  86. #define ESP_CONSOLE_REPL_CONFIG_DEFAULT() \
  87. { \
  88. .max_history_len = 32, \
  89. .history_save_path = NULL, \
  90. .task_stack_size = 4096, \
  91. .task_priority = 2, \
  92. .prompt = NULL, \
  93. .device = { \
  94. .uart = { \
  95. .channel = CONSOLE_DEFAULT_UART_CHANNEL, \
  96. .baud_rate = CONSOLE_DEFAULT_UART_BAUDRATE, \
  97. .tx_gpio = CONSOLE_DEFAULT_UART_TX_GPIO, \
  98. .rx_gpio = CONSOLE_DEFAULT_UART_RX_GPIO, \
  99. } \
  100. } \
  101. }
  102. /**
  103. * @brief initialize console module
  104. * @param config console configuration
  105. * @note Call this once before using other console module features
  106. * @return
  107. * - ESP_OK on success
  108. * - ESP_ERR_NO_MEM if out of memory
  109. * - ESP_ERR_INVALID_STATE if already initialized
  110. * - ESP_ERR_INVALID_ARG if the configuration is invalid
  111. */
  112. esp_err_t esp_console_init(const esp_console_config_t *config);
  113. /**
  114. * @brief de-initialize console module
  115. * @note Call this once when done using console module functions
  116. * @return
  117. * - ESP_OK on success
  118. * - ESP_ERR_INVALID_STATE if not initialized yet
  119. */
  120. esp_err_t esp_console_deinit(void);
  121. /**
  122. * @brief Console command main function
  123. * @param argc number of arguments
  124. * @param argv array with argc entries, each pointing to a zero-terminated string argument
  125. * @return console command return code, 0 indicates "success"
  126. */
  127. typedef int (*esp_console_cmd_func_t)(int argc, char **argv);
  128. /**
  129. * @brief Console command description
  130. */
  131. typedef struct {
  132. /**
  133. * Command name. Must not be NULL, must not contain spaces.
  134. * The pointer must be valid until the call to esp_console_deinit.
  135. */
  136. const char *command;
  137. /**
  138. * Help text for the command, shown by help command.
  139. * If set, the pointer must be valid until the call to esp_console_deinit.
  140. * If not set, the command will not be listed in 'help' output.
  141. */
  142. const char *help;
  143. /**
  144. * Hint text, usually lists possible arguments.
  145. * If set to NULL, and 'argtable' field is non-NULL, hint will be generated
  146. * automatically
  147. */
  148. const char *hint;
  149. /**
  150. * Pointer to a function which implements the command.
  151. */
  152. esp_console_cmd_func_t func;
  153. /**
  154. * Array or structure of pointers to arg_xxx structures, may be NULL.
  155. * Used to generate hint text if 'hint' is set to NULL.
  156. * Array/structure which this field points to must end with an arg_end.
  157. * Only used for the duration of esp_console_cmd_register call.
  158. */
  159. void *argtable;
  160. } esp_console_cmd_t;
  161. /**
  162. * @brief Register console command
  163. * @param cmd pointer to the command description; can point to a temporary value
  164. * @return
  165. * - ESP_OK on success
  166. * - ESP_ERR_NO_MEM if out of memory
  167. * - ESP_ERR_INVALID_ARG if command description includes invalid arguments
  168. */
  169. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd);
  170. /**
  171. * @brief Run command line
  172. * @param cmdline command line (command name followed by a number of arguments)
  173. * @param[out] cmd_ret return code from the command (set if command was run)
  174. * @return
  175. * - ESP_OK, if command was run
  176. * - ESP_ERR_INVALID_ARG, if the command line is empty, or only contained
  177. * whitespace
  178. * - ESP_ERR_NOT_FOUND, if command with given name wasn't registered
  179. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  180. */
  181. esp_err_t esp_console_run(const char *cmdline, int *cmd_ret);
  182. /**
  183. * @brief Split command line into arguments in place
  184. * @verbatim
  185. * - This function finds whitespace-separated arguments in the given input line.
  186. *
  187. * 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
  188. *
  189. * - Argument which include spaces may be surrounded with quotes. In this case
  190. * spaces are preserved and quotes are stripped.
  191. *
  192. * 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
  193. *
  194. * - Escape sequences may be used to produce backslash, double quote, and space:
  195. *
  196. * 'a\ b\\c\"' -> [ 'a b\c"' ]
  197. * @endverbatim
  198. * @note Pointers to at most argv_size - 1 arguments are returned in argv array.
  199. * The pointer after the last one (i.e. argv[argc]) is set to NULL.
  200. *
  201. * @param line pointer to buffer to parse; it is modified in place
  202. * @param argv array where the pointers to arguments are written
  203. * @param argv_size number of elements in argv_array (max. number of arguments)
  204. * @return number of arguments found (argc)
  205. */
  206. size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
  207. /**
  208. * @brief Callback which provides command completion for linenoise library
  209. *
  210. * When using linenoise for line editing, command completion support
  211. * can be enabled like this:
  212. *
  213. * linenoiseSetCompletionCallback(&esp_console_get_completion);
  214. *
  215. * @param buf the string typed by the user
  216. * @param lc linenoiseCompletions to be filled in
  217. */
  218. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc);
  219. /**
  220. * @brief Callback which provides command hints for linenoise library
  221. *
  222. * When using linenoise for line editing, hints support can be enabled as
  223. * follows:
  224. *
  225. * linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  226. *
  227. * The extra cast is needed because linenoiseHintsCallback is defined as
  228. * returning a char* instead of const char*.
  229. *
  230. * @param buf line typed by the user
  231. * @param[out] color ANSI color code to be used when displaying the hint
  232. * @param[out] bold set to 1 if hint has to be displayed in bold
  233. * @return string containing the hint text. This string is persistent and should
  234. * not be freed (i.e. linenoiseSetFreeHintsCallback should not be used).
  235. */
  236. const char *esp_console_get_hint(const char *buf, int *color, int *bold);
  237. /**
  238. * @brief Register a 'help' command
  239. *
  240. * Default 'help' command prints the list of registered commands along with
  241. * hints and help strings.
  242. *
  243. * @return
  244. * - ESP_OK on success
  245. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  246. */
  247. esp_err_t esp_console_register_help_command(void);
  248. /******************************************************************************
  249. * Console REPL
  250. ******************************************************************************/
  251. /**
  252. * @brief Initialize console REPL environment
  253. *
  254. * @param config REPL configuration
  255. *
  256. * @note This is a all-in-one function to establish the environment needed for REPL, includes:
  257. * - Install the UART driver on the console UART (8n1, 115200, REF_TICK clock source)
  258. * - Configures the stdin/stdout to go through the UART driver
  259. * - Initializes linenoise
  260. * - Spawn new thread to run REPL in the background
  261. *
  262. * @attention This function is meant to be used in the examples to make the code more compact.
  263. * Applications which use console functionality should be based on
  264. * the underlying linenoise and esp_console functions.
  265. *
  266. * @return
  267. * - ESP_OK on success
  268. * - ESP_FAIL Parameter error
  269. */
  270. esp_err_t esp_console_repl_init(const esp_console_repl_config_t *config);
  271. /**
  272. * @brief Start REPL task
  273. *
  274. * @return
  275. * - ESP_OK on success
  276. * - ESP_ERR_INVALID_STATE, if repl has started already
  277. */
  278. esp_err_t esp_console_repl_start(void);
  279. /**
  280. * @brief Register a 'quit' command
  281. *
  282. * Default 'quit' command will destory resources and exit REPL environment.
  283. *
  284. * @return
  285. * - ESP_OK on success
  286. * - others on failed
  287. */
  288. esp_err_t esp_console_register_quit_command(void);
  289. #ifdef __cplusplus
  290. }
  291. #endif