esp_console.h 9.2 KB

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