esp_console.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. } esp_console_repl_config_t;
  54. /**
  55. * @brief Default console repl configuration value
  56. *
  57. */
  58. #define ESP_CONSOLE_REPL_CONFIG_DEFAULT() \
  59. { \
  60. .max_history_len = 32, \
  61. .history_save_path = NULL, \
  62. .task_stack_size = 4096, \
  63. .task_priority = 2, \
  64. .prompt = NULL, \
  65. }
  66. /**
  67. * @brief Parameters for console device: UART
  68. *
  69. */
  70. typedef struct {
  71. int channel; //!< UART channel number (count from zero)
  72. int baud_rate; //!< Comunication baud rate
  73. int tx_gpio_num; //!< GPIO number for TX path, -1 means using default one
  74. int rx_gpio_num; //!< GPIO number for RX path, -1 means using default one
  75. } esp_console_dev_uart_config_t;
  76. #ifdef CONFIG_ESP_CONSOLE_UART_CUSTOM
  77. #define ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT() \
  78. { \
  79. .channel = CONFIG_ESP_CONSOLE_UART_NUM, \
  80. .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, \
  81. .tx_gpio_num = CONFIG_ESP_CONSOLE_UART_TX_GPIO, \
  82. .rx_gpio_num = CONFIG_ESP_CONSOLE_UART_RX_GPIO, \
  83. }
  84. #else
  85. #define ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT() \
  86. { \
  87. .channel = CONFIG_ESP_CONSOLE_UART_NUM, \
  88. .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE, \
  89. .tx_gpio_num = -1, \
  90. .rx_gpio_num = -1, \
  91. }
  92. #endif
  93. /**
  94. * @brief initialize console module
  95. * @param config console configuration
  96. * @note Call this once before using other console module features
  97. * @return
  98. * - ESP_OK on success
  99. * - ESP_ERR_NO_MEM if out of memory
  100. * - ESP_ERR_INVALID_STATE if already initialized
  101. * - ESP_ERR_INVALID_ARG if the configuration is invalid
  102. */
  103. esp_err_t esp_console_init(const esp_console_config_t *config);
  104. /**
  105. * @brief de-initialize console module
  106. * @note Call this once when done using console module functions
  107. * @return
  108. * - ESP_OK on success
  109. * - ESP_ERR_INVALID_STATE if not initialized yet
  110. */
  111. esp_err_t esp_console_deinit(void);
  112. /**
  113. * @brief Console command main function
  114. * @param argc number of arguments
  115. * @param argv array with argc entries, each pointing to a zero-terminated string argument
  116. * @return console command return code, 0 indicates "success"
  117. */
  118. typedef int (*esp_console_cmd_func_t)(int argc, char **argv);
  119. /**
  120. * @brief Console command description
  121. */
  122. typedef struct {
  123. /**
  124. * Command name. Must not be NULL, must not contain spaces.
  125. * The pointer must be valid until the call to esp_console_deinit.
  126. */
  127. const char *command;
  128. /**
  129. * Help text for the command, shown by help command.
  130. * If set, the pointer must be valid until the call to esp_console_deinit.
  131. * If not set, the command will not be listed in 'help' output.
  132. */
  133. const char *help;
  134. /**
  135. * Hint text, usually lists possible arguments.
  136. * If set to NULL, and 'argtable' field is non-NULL, hint will be generated
  137. * automatically
  138. */
  139. const char *hint;
  140. /**
  141. * Pointer to a function which implements the command.
  142. */
  143. esp_console_cmd_func_t func;
  144. /**
  145. * Array or structure of pointers to arg_xxx structures, may be NULL.
  146. * Used to generate hint text if 'hint' is set to NULL.
  147. * Array/structure which this field points to must end with an arg_end.
  148. * Only used for the duration of esp_console_cmd_register call.
  149. */
  150. void *argtable;
  151. } esp_console_cmd_t;
  152. /**
  153. * @brief Register console command
  154. * @param cmd pointer to the command description; can point to a temporary value
  155. * @return
  156. * - ESP_OK on success
  157. * - ESP_ERR_NO_MEM if out of memory
  158. * - ESP_ERR_INVALID_ARG if command description includes invalid arguments
  159. */
  160. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd);
  161. /**
  162. * @brief Run command line
  163. * @param cmdline command line (command name followed by a number of arguments)
  164. * @param[out] cmd_ret return code from the command (set if command was run)
  165. * @return
  166. * - ESP_OK, if command was run
  167. * - ESP_ERR_INVALID_ARG, if the command line is empty, or only contained
  168. * whitespace
  169. * - ESP_ERR_NOT_FOUND, if command with given name wasn't registered
  170. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  171. */
  172. esp_err_t esp_console_run(const char *cmdline, int *cmd_ret);
  173. /**
  174. * @brief Split command line into arguments in place
  175. * @verbatim
  176. * - This function finds whitespace-separated arguments in the given input line.
  177. *
  178. * 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
  179. *
  180. * - Argument which include spaces may be surrounded with quotes. In this case
  181. * spaces are preserved and quotes are stripped.
  182. *
  183. * 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
  184. *
  185. * - Escape sequences may be used to produce backslash, double quote, and space:
  186. *
  187. * 'a\ b\\c\"' -> [ 'a b\c"' ]
  188. * @endverbatim
  189. * @note Pointers to at most argv_size - 1 arguments are returned in argv array.
  190. * The pointer after the last one (i.e. argv[argc]) is set to NULL.
  191. *
  192. * @param line pointer to buffer to parse; it is modified in place
  193. * @param argv array where the pointers to arguments are written
  194. * @param argv_size number of elements in argv_array (max. number of arguments)
  195. * @return number of arguments found (argc)
  196. */
  197. size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
  198. /**
  199. * @brief Callback which provides command completion for linenoise library
  200. *
  201. * When using linenoise for line editing, command completion support
  202. * can be enabled like this:
  203. *
  204. * linenoiseSetCompletionCallback(&esp_console_get_completion);
  205. *
  206. * @param buf the string typed by the user
  207. * @param lc linenoiseCompletions to be filled in
  208. */
  209. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc);
  210. /**
  211. * @brief Callback which provides command hints for linenoise library
  212. *
  213. * When using linenoise for line editing, hints support can be enabled as
  214. * follows:
  215. *
  216. * linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  217. *
  218. * The extra cast is needed because linenoiseHintsCallback is defined as
  219. * returning a char* instead of const char*.
  220. *
  221. * @param buf line typed by the user
  222. * @param[out] color ANSI color code to be used when displaying the hint
  223. * @param[out] bold set to 1 if hint has to be displayed in bold
  224. * @return string containing the hint text. This string is persistent and should
  225. * not be freed (i.e. linenoiseSetFreeHintsCallback should not be used).
  226. */
  227. const char *esp_console_get_hint(const char *buf, int *color, int *bold);
  228. /**
  229. * @brief Register a 'help' command
  230. *
  231. * Default 'help' command prints the list of registered commands along with
  232. * hints and help strings.
  233. *
  234. * @return
  235. * - ESP_OK on success
  236. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  237. */
  238. esp_err_t esp_console_register_help_command(void);
  239. /******************************************************************************
  240. * Console REPL
  241. ******************************************************************************/
  242. /**
  243. * @brief Type defined for console REPL
  244. *
  245. */
  246. typedef struct esp_console_repl_s esp_console_repl_t;
  247. /**
  248. * @brief Console REPL base structure
  249. *
  250. */
  251. struct esp_console_repl_s {
  252. /**
  253. * @brief Delete console REPL environment
  254. * @param[in] repl REPL handle returned from esp_console_new_repl_xxx
  255. * @return
  256. * - ESP_OK on success
  257. * - ESP_FAIL on errors
  258. */
  259. esp_err_t (*del)(esp_console_repl_t *repl);
  260. };
  261. /**
  262. * @brief Establish a console REPL environment over UART driver
  263. *
  264. * @param[in] dev_config UART device configuration
  265. * @param[in] repl_config REPL configuration
  266. * @param[out] ret_repl return REPL handle after initialization succeed, return NULL otherwise
  267. *
  268. * @note This is a all-in-one function to establish the environment needed for REPL, includes:
  269. * - Install the UART driver on the console UART (8n1, 115200, REF_TICK clock source)
  270. * - Configures the stdin/stdout to go through the UART driver
  271. * - Initializes linenoise
  272. * - Spawn new thread to run REPL in the background
  273. *
  274. * @attention This function is meant to be used in the examples to make the code more compact.
  275. * Applications which use console functionality should be based on
  276. * the underlying linenoise and esp_console functions.
  277. *
  278. * @return
  279. * - ESP_OK on success
  280. * - ESP_FAIL Parameter error
  281. */
  282. 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);
  283. /**
  284. * @brief Start REPL environment
  285. * @param[in] repl REPL handle returned from esp_console_new_repl_xxx
  286. * @note Once the REPL got started, it won't be stopped until user call repl->del(repl) to destory the REPL environment.
  287. * @return
  288. * - ESP_OK on success
  289. * - ESP_ERR_INVALID_STATE, if repl has started already
  290. */
  291. esp_err_t esp_console_start_repl(esp_console_repl_t *repl);
  292. #ifdef __cplusplus
  293. }
  294. #endif