esp_console.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2016-2017 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. {
  18. #endif
  19. #include <stddef.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 initialize console module
  34. * Call this once before using other console module features
  35. * @return
  36. * - ESP_OK on success
  37. * - ESP_ERR_NO_MEM if out of memory
  38. * - ESP_ERR_INVALID_STATE if already initialized
  39. */
  40. esp_err_t esp_console_init(const esp_console_config_t* config);
  41. /**
  42. * @brief de-initialize console module
  43. * Call this once when done using console module functions
  44. * @return
  45. * - ESP_OK on success
  46. * - ESP_ERR_INVALID_STATE if not initialized yet
  47. */
  48. esp_err_t esp_console_deinit();
  49. /**
  50. * @brief Console command main function
  51. * @param argc number of arguments
  52. * @param argv array with argc entries, each pointing to a zero-terminated string argument
  53. * @return console command return code, 0 indicates "success"
  54. */
  55. typedef int (*esp_console_cmd_func_t)(int argc, char** argv);
  56. /**
  57. * @brief Console command description
  58. */
  59. typedef struct {
  60. /**
  61. * Command name. Must not be NULL, must not contain spaces.
  62. * The pointer must be valid until the call to esp_console_deinit.
  63. */
  64. const char* command; //!< command name
  65. /**
  66. * Help text for the command, shown by help command.
  67. * If set, the pointer must be valid until the call to esp_console_deinit.
  68. * If not set, the command will not be listed in 'help' output.
  69. */
  70. const char* help;
  71. /**
  72. * Hint text, usually lists possible arguments.
  73. * If set to NULL, and 'argtable' field is non-NULL, hint will be generated
  74. * automatically
  75. */
  76. const char* hint;
  77. /**
  78. * Pointer to a function which implements the command.
  79. */
  80. esp_console_cmd_func_t func;
  81. /**
  82. * Array or structure of pointers to arg_xxx structures, may be NULL.
  83. * Used to generate hint text if 'hint' is set to NULL.
  84. * Array/structure which this field points to must end with an arg_end.
  85. * Only used for the duration of esp_console_cmd_register call.
  86. */
  87. void* argtable;
  88. } esp_console_cmd_t;
  89. /**
  90. * @brief Register console command
  91. * @param cmd pointer to the command description; can point to a temporary value
  92. * @return
  93. * - ESP_OK on success
  94. * - ESP_ERR_NO_MEM if out of memory
  95. */
  96. esp_err_t esp_console_cmd_register(const esp_console_cmd_t *cmd);
  97. /**
  98. * @brief Run command line
  99. * @param cmdline command line (command name followed by a number of arguments)
  100. * @param[out] cmd_ret return code from the command (set if command was run)
  101. * @return
  102. * - ESP_OK, if command was run
  103. * - ESP_ERR_INVALID_ARG, if the command line is empty, or only contained
  104. * whitespace
  105. * - ESP_ERR_NOT_FOUND, if command with given name wasn't registered
  106. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  107. */
  108. esp_err_t esp_console_run(const char* cmdline, int* cmd_ret);
  109. /**
  110. * @brief Split command line into arguments in place
  111. *
  112. * - This function finds whitespace-separated arguments in the given input line.
  113. *
  114. * 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
  115. *
  116. * - Argument which include spaces may be surrounded with quotes. In this case
  117. * spaces are preserved and quotes are stripped.
  118. *
  119. * 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
  120. *
  121. * - Escape sequences may be used to produce backslash, double quote, and space:
  122. *
  123. * 'a\ b\\c\"' -> [ 'a b\c"' ]
  124. *
  125. * Pointers to at most argv_size - 1 arguments are returned in argv array.
  126. * The pointer after the last one (i.e. argv[argc]) is set to NULL.
  127. *
  128. * @param line pointer to buffer to parse; it is modified in place
  129. * @param argv array where the pointers to arguments are written
  130. * @param argv_size number of elements in argv_array (max. number of arguments)
  131. * @return number of arguments found (argc)
  132. */
  133. size_t esp_console_split_argv(char *line, char **argv, size_t argv_size);
  134. /**
  135. * @brief Callback which provides command completion for linenoise library
  136. *
  137. * When using linenoise for line editing, command completion support
  138. * can be enabled like this:
  139. *
  140. * linenoiseSetCompletionCallback(&esp_console_get_completion);
  141. *
  142. * @param buf the string typed by the user
  143. * @param lc linenoiseCompletions to be filled in
  144. */
  145. void esp_console_get_completion(const char *buf, linenoiseCompletions *lc);
  146. /**
  147. * @brief Callback which provides command hints for linenoise library
  148. *
  149. * When using linenoise for line editing, hints support can be enabled as
  150. * follows:
  151. *
  152. * linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  153. *
  154. * The extra cast is needed because linenoiseHintsCallback is defined as
  155. * returning a char* instead of const char*.
  156. *
  157. * @param buf line typed by the user
  158. * @param[out] color ANSI color code to be used when displaying the hint
  159. * @param[out] bold set to 1 if hint has to be displayed in bold
  160. * @return string containing the hint text. This string is persistent and should
  161. * not be freed (i.e. linenoiseSetFreeHintsCallback should not be used).
  162. */
  163. const char *esp_console_get_hint(const char *buf, int *color, int *bold);
  164. /**
  165. * @brief Register a 'help' command
  166. * Default 'help' command prints the list of registered commands along with
  167. * hints and help strings.
  168. * @return
  169. * - ESP_OK on success
  170. * - ESP_ERR_INVALID_STATE, if esp_console_init wasn't called
  171. */
  172. esp_err_t esp_console_register_help_command();
  173. #ifdef __cplusplus
  174. }
  175. #endif