esp_console.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stddef.h>
  11. #include "sdkconfig.h"
  12. #include "esp_heap_caps.h"
  13. #include "esp_err.h"
  14. // Forward declaration. Definition in linenoise/linenoise.h.
  15. typedef struct linenoiseCompletions linenoiseCompletions;
  16. /**
  17. * @brief Parameters for console initialization
  18. */
  19. typedef struct {
  20. size_t max_cmdline_length; //!< length of command line buffer, in bytes
  21. size_t max_cmdline_args; //!< maximum number of command line arguments to parse
  22. uint32_t heap_alloc_caps; //!< where to (e.g. MALLOC_CAP_SPIRAM) allocate heap objects such as cmds used by esp_console
  23. int hint_color; //!< ASCII color code of hint text
  24. int hint_bold; //!< Set to 1 to print hint text in bold
  25. } esp_console_config_t;
  26. /**
  27. * @brief Default console configuration value
  28. *
  29. */
  30. #define ESP_CONSOLE_CONFIG_DEFAULT() \
  31. { \
  32. .max_cmdline_length = 256, \
  33. .max_cmdline_args = 32, \
  34. .heap_alloc_caps = MALLOC_CAP_DEFAULT, \
  35. .hint_color = 39, \
  36. .hint_bold = 0 \
  37. }
  38. /**
  39. * @brief Parameters for console REPL (Read Eval Print Loop)
  40. *
  41. */
  42. typedef struct {
  43. uint32_t max_history_len; //!< maximum length for the history
  44. const char *history_save_path; //!< file path used to save history commands, set to NULL won't save to file system
  45. uint32_t task_stack_size; //!< repl task stack size
  46. uint32_t task_priority; //!< repl task priority
  47. const char *prompt; //!< prompt (NULL represents default: "esp> ")
  48. size_t max_cmdline_length; //!< maximum length of a command line. If 0, default value will be used
  49. } esp_console_repl_config_t;
  50. /**
  51. * @brief Default console repl configuration value
  52. *
  53. */
  54. #define ESP_CONSOLE_REPL_CONFIG_DEFAULT() \
  55. { \
  56. .max_history_len = 32, \
  57. .history_save_path = NULL, \
  58. .task_stack_size = 4096, \
  59. .task_priority = 2, \
  60. .prompt = NULL, \
  61. .max_cmdline_length = 0, \
  62. }
  63. #if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  64. /**
  65. * @brief Parameters for console device: UART
  66. *
  67. */
  68. typedef struct {
  69. int channel; //!< UART channel number (count from zero)
  70. int baud_rate; //!< Comunication baud rate
  71. int tx_gpio_num; //!< GPIO number for TX path, -1 means using default one
  72. int rx_gpio_num; //!< GPIO number for RX path, -1 means using default one
  73. } esp_console_dev_uart_config_t;
  74. #if CONFIG_ESP_CONSOLE_UART_CUSTOM
  75. #define ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT() \
  76. { \
  77. .channel = CONFIG_ESP_CONSOLE_UART_NUM, \
  78. .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, \
  79. .tx_gpio_num = CONFIG_ESP_CONSOLE_UART_TX_GPIO, \
  80. .rx_gpio_num = CONFIG_ESP_CONSOLE_UART_RX_GPIO, \
  81. }
  82. #else
  83. #define ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT() \
  84. { \
  85. .channel = CONFIG_ESP_CONSOLE_UART_NUM, \
  86. .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, \
  87. .tx_gpio_num = -1, \
  88. .rx_gpio_num = -1, \
  89. }
  90. #endif // CONFIG_ESP_CONSOLE_UART_CUSTOM
  91. #endif // CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  92. #if CONFIG_ESP_CONSOLE_USB_CDC || (defined __DOXYGEN__ && SOC_USB_OTG_SUPPORTED)
  93. /**
  94. * @brief Parameters for console device: USB CDC
  95. *
  96. * @note It's an empty structure for now, reserved for future
  97. *
  98. */
  99. typedef struct {
  100. } esp_console_dev_usb_cdc_config_t;
  101. #define ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT() {}
  102. #endif // CONFIG_ESP_CONSOLE_USB_CDC || (defined __DOXYGEN__ && SOC_USB_OTG_SUPPORTED)
  103. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || (defined __DOXYGEN__ && SOC_USB_SERIAL_JTAG_SUPPORTED)
  104. /**
  105. * @brief Parameters for console device: USB-SERIAL-JTAG
  106. *
  107. * @note It's an empty structure for now, reserved for future
  108. *
  109. */
  110. typedef struct {
  111. } esp_console_dev_usb_serial_jtag_config_t;
  112. #define ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT() {}
  113. #endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || (defined __DOXYGEN__ && SOC_USB_SERIAL_JTAG_SUPPORTED)
  114. /**
  115. * @brief initialize console module
  116. * @param config console configuration
  117. * @note Call this once before using other console module features
  118. * @return
  119. * - ESP_OK on success
  120. * - ESP_ERR_NO_MEM if out of memory
  121. * - ESP_ERR_INVALID_STATE if already initialized
  122. * - ESP_ERR_INVALID_ARG if the configuration is invalid
  123. */
  124. esp_err_t esp_console_init(const esp_console_config_t *config);
  125. /**
  126. * @brief de-initialize console module
  127. * @note Call this once when done using console module functions
  128. * @return
  129. * - ESP_OK on success
  130. * - ESP_ERR_INVALID_STATE if not initialized yet
  131. */
  132. esp_err_t esp_console_deinit(void);
  133. /**
  134. * @brief Console command main function
  135. * @param argc number of arguments
  136. * @param argv array with argc entries, each pointing to a zero-terminated string argument
  137. * @return console command return code, 0 indicates "success"
  138. */
  139. typedef int (*esp_console_cmd_func_t)(int argc, char **argv);
  140. /**
  141. * @brief Console command description
  142. */
  143. typedef struct {
  144. /**
  145. * Command name. Must not be NULL, must not contain spaces.
  146. * The pointer must be valid until the call to esp_console_deinit.
  147. */
  148. const char *command;
  149. /**
  150. * Help text for the command, shown by help command.
  151. * If set, the pointer must be valid until the call to esp_console_deinit.
  152. * If not set, the command will not be listed in 'help' output.
  153. */
  154. const char *help;
  155. /**
  156. * Hint text, usually lists possible arguments.
  157. * If set to NULL, and 'argtable' field is non-NULL, hint will be generated
  158. * automatically
  159. */
  160. const char *hint;
  161. /**
  162. * Pointer to a function which implements the command.
  163. */
  164. esp_console_cmd_func_t func;
  165. /**
  166. * Array or structure of pointers to arg_xxx structures, may be NULL.
  167. * Used to generate hint text if 'hint' is set to NULL.
  168. * Array/structure which this field points to must end with an arg_end.
  169. * Only used for the duration of esp_console_cmd_register call.
  170. */
  171. void *argtable;
  172. } esp_console_cmd_t;
  173. /**
  174. * @brief Register console command
  175. * @param cmd pointer to the command description; can point to a temporary value
  176. * @return
  177. * - ESP_OK on success
  178. * - ESP_ERR_NO_MEM if out of memory
  179. * - ESP_ERR_INVALID_ARG if command description includes invalid arguments
  180. */
  181. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd);
  182. /**
  183. * @brief Run command line
  184. * @param cmdline command line (command name followed by a number of arguments)
  185. * @param[out] cmd_ret return code from the command (set if command was run)
  186. * @return
  187. * - ESP_OK, if command was run
  188. * - ESP_ERR_INVALID_ARG, if the command line is empty, or only contained
  189. * whitespace
  190. * - ESP_ERR_NOT_FOUND, if command with given name wasn't registered
  191. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  192. */
  193. esp_err_t esp_console_run(const char *cmdline, int *cmd_ret);
  194. /**
  195. * @brief Split command line into arguments in place
  196. * @verbatim
  197. * - This function finds whitespace-separated arguments in the given input line.
  198. *
  199. * 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
  200. *
  201. * - Argument which include spaces may be surrounded with quotes. In this case
  202. * spaces are preserved and quotes are stripped.
  203. *
  204. * 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
  205. *
  206. * - Escape sequences may be used to produce backslash, double quote, and space:
  207. *
  208. * 'a\ b\\c\"' -> [ 'a b\c"' ]
  209. * @endverbatim
  210. * @note Pointers to at most argv_size - 1 arguments are returned in argv array.
  211. * The pointer after the last one (i.e. argv[argc]) is set to NULL.
  212. *
  213. * @param line pointer to buffer to parse; it is modified in place
  214. * @param argv array where the pointers to arguments are written
  215. * @param argv_size number of elements in argv_array (max. number of arguments)
  216. * @return number of arguments found (argc)
  217. */
  218. size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
  219. /**
  220. * @brief Callback which provides command completion for linenoise library
  221. *
  222. * When using linenoise for line editing, command completion support
  223. * can be enabled like this:
  224. *
  225. * linenoiseSetCompletionCallback(&esp_console_get_completion);
  226. *
  227. * @param buf the string typed by the user
  228. * @param lc linenoiseCompletions to be filled in
  229. */
  230. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc);
  231. /**
  232. * @brief Callback which provides command hints for linenoise library
  233. *
  234. * When using linenoise for line editing, hints support can be enabled as
  235. * follows:
  236. *
  237. * linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  238. *
  239. * The extra cast is needed because linenoiseHintsCallback is defined as
  240. * returning a char* instead of const char*.
  241. *
  242. * @param buf line typed by the user
  243. * @param[out] color ANSI color code to be used when displaying the hint
  244. * @param[out] bold set to 1 if hint has to be displayed in bold
  245. * @return string containing the hint text. This string is persistent and should
  246. * not be freed (i.e. linenoiseSetFreeHintsCallback should not be used).
  247. */
  248. const char *esp_console_get_hint(const char *buf, int *color, int *bold);
  249. /**
  250. * @brief Register a 'help' command
  251. *
  252. * Default 'help' command prints the list of registered commands along with
  253. * hints and help strings if no additional argument is given. If an additional
  254. * argument is given, the help command will look for a command with the same
  255. * name and only print the hints and help strings of that command.
  256. *
  257. * @return
  258. * - ESP_OK on success
  259. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  260. */
  261. esp_err_t esp_console_register_help_command(void);
  262. /******************************************************************************
  263. * Console REPL
  264. ******************************************************************************/
  265. /**
  266. * @brief Type defined for console REPL
  267. *
  268. */
  269. typedef struct esp_console_repl_s esp_console_repl_t;
  270. /**
  271. * @brief Console REPL base structure
  272. *
  273. */
  274. struct esp_console_repl_s {
  275. /**
  276. * @brief Delete console REPL environment
  277. * @param[in] repl REPL handle returned from esp_console_new_repl_xxx
  278. * @return
  279. * - ESP_OK on success
  280. * - ESP_FAIL on errors
  281. */
  282. esp_err_t (*del)(esp_console_repl_t *repl);
  283. };
  284. #if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  285. /**
  286. * @brief Establish a console REPL environment over UART driver
  287. *
  288. * @param[in] dev_config UART device configuration
  289. * @param[in] repl_config REPL configuration
  290. * @param[out] ret_repl return REPL handle after initialization succeed, return NULL otherwise
  291. *
  292. * @note This is an all-in-one function to establish the environment needed for REPL, includes:
  293. * - Install the UART driver on the console UART (8n1, 115200, REF_TICK clock source)
  294. * - Configures the stdin/stdout to go through the UART driver
  295. * - Initializes linenoise
  296. * - Spawn new thread to run REPL in the background
  297. *
  298. * @attention This function is meant to be used in the examples to make the code more compact.
  299. * Applications which use console functionality should be based on
  300. * the underlying linenoise and esp_console functions.
  301. *
  302. * @return
  303. * - ESP_OK on success
  304. * - ESP_FAIL Parameter error
  305. */
  306. esp_err_t esp_console_new_repl_uart(const esp_console_dev_uart_config_t *dev_config, const esp_console_repl_config_t *repl_config, esp_console_repl_t **ret_repl);
  307. #endif // CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  308. #if CONFIG_ESP_CONSOLE_USB_CDC || (defined __DOXYGEN__ && SOC_USB_OTG_SUPPORTED)
  309. /**
  310. * @brief Establish a console REPL environment over USB CDC
  311. *
  312. * @param[in] dev_config USB CDC configuration
  313. * @param[in] repl_config REPL configuration
  314. * @param[out] ret_repl return REPL handle after initialization succeed, return NULL otherwise
  315. *
  316. * @note This is a all-in-one function to establish the environment needed for REPL, includes:
  317. * - Initializes linenoise
  318. * - Spawn new thread to run REPL in the background
  319. *
  320. * @attention This function is meant to be used in the examples to make the code more compact.
  321. * Applications which use console functionality should be based on
  322. * the underlying linenoise and esp_console functions.
  323. *
  324. * @return
  325. * - ESP_OK on success
  326. * - ESP_FAIL Parameter error
  327. */
  328. esp_err_t esp_console_new_repl_usb_cdc(const esp_console_dev_usb_cdc_config_t *dev_config, const esp_console_repl_config_t *repl_config, esp_console_repl_t **ret_repl);
  329. #endif // CONFIG_ESP_CONSOLE_USB_CDC || (defined __DOXYGEN__ && SOC_USB_OTG_SUPPORTED)
  330. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || (defined __DOXYGEN__ && SOC_USB_SERIAL_JTAG_SUPPORTED)
  331. /**
  332. * @brief Establish a console REPL (Read-eval-print loop) environment over USB-SERIAL-JTAG
  333. *
  334. * @param[in] dev_config USB-SERIAL-JTAG configuration
  335. * @param[in] repl_config REPL configuration
  336. * @param[out] ret_repl return REPL handle after initialization succeed, return NULL otherwise
  337. *
  338. * @note This is an all-in-one function to establish the environment needed for REPL, includes:
  339. * - Initializes linenoise
  340. * - Spawn new thread to run REPL in the background
  341. *
  342. * @attention This function is meant to be used in the examples to make the code more compact.
  343. * Applications which use console functionality should be based on
  344. * the underlying linenoise and esp_console functions.
  345. *
  346. * @return
  347. * - ESP_OK on success
  348. * - ESP_FAIL Parameter error
  349. */
  350. esp_err_t esp_console_new_repl_usb_serial_jtag(const esp_console_dev_usb_serial_jtag_config_t *dev_config, const esp_console_repl_config_t *repl_config, esp_console_repl_t **ret_repl);
  351. #endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || (defined __DOXYGEN__ && SOC_USB_SERIAL_JTAG_SUPPORTED)
  352. /**
  353. * @brief Start REPL environment
  354. * @param[in] repl REPL handle returned from esp_console_new_repl_xxx
  355. * @note Once the REPL gets started, it won't be stopped until the user calls repl->del(repl) to destroy the REPL environment.
  356. * @return
  357. * - ESP_OK on success
  358. * - ESP_ERR_INVALID_STATE, if repl has started already
  359. */
  360. esp_err_t esp_console_start_repl(esp_console_repl_t *repl);
  361. #ifdef __cplusplus
  362. }
  363. #endif