esp_console.h 6.3 KB

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