esp_console_repl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include <stdint.h>
  15. #include <stdio.h>
  16. #include "sdkconfig.h"
  17. #include "esp_err.h"
  18. #include "esp_log.h"
  19. #include "esp_console.h"
  20. #include "esp_vfs_dev.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "driver/uart.h"
  24. #include "linenoise/linenoise.h"
  25. static const char *TAG = "console.repl";
  26. #define CONSOLE_PROMPT_LEN_MAX (32)
  27. typedef enum {
  28. CONSOLE_REPL_STATE_DEINIT,
  29. CONSOLE_REPL_STATE_INIT,
  30. CONSOLE_REPL_STATE_START,
  31. } repl_state_t;
  32. static repl_state_t s_repl_state = CONSOLE_REPL_STATE_DEINIT;
  33. /**
  34. * @brief Prompt to be printed before each line.
  35. *
  36. */
  37. static char s_prompt[CONSOLE_PROMPT_LEN_MAX];
  38. /**
  39. * @brief path to save history commands in file system
  40. *
  41. */
  42. static const char *s_history_save_path = NULL;
  43. /**
  44. * @brief REPL task handle
  45. *
  46. */
  47. static TaskHandle_t s_repl_task_hdl = NULL;
  48. static void esp_console_repl_thread(void *param)
  49. {
  50. // waiting for task notify
  51. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  52. while (s_repl_state == CONSOLE_REPL_STATE_START) {
  53. char *line = linenoise(s_prompt);
  54. if (line == NULL) {
  55. ESP_LOGD(TAG, "empty line");
  56. /* Ignore empty lines */
  57. continue;
  58. }
  59. /* Add the command to the history */
  60. linenoiseHistoryAdd(line);
  61. /* Save command history to filesystem */
  62. if (s_history_save_path) {
  63. linenoiseHistorySave(s_history_save_path);
  64. }
  65. /* Try to run the command */
  66. int ret;
  67. esp_err_t err = esp_console_run(line, &ret);
  68. if (err == ESP_ERR_NOT_FOUND) {
  69. printf("Unrecognized command\n");
  70. } else if (err == ESP_ERR_INVALID_ARG) {
  71. // command was empty
  72. } else if (err == ESP_OK && ret != ESP_OK) {
  73. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
  74. } else if (err != ESP_OK) {
  75. printf("Internal error: %s\n", esp_err_to_name(err));
  76. }
  77. /* linenoise allocates line buffer on the heap, so need to free it */
  78. linenoiseFree(line);
  79. }
  80. ESP_LOGD(TAG, "The End");
  81. vTaskDelete(NULL);
  82. }
  83. esp_err_t esp_console_repl_init(const esp_console_repl_config_t *config)
  84. {
  85. esp_err_t ret = ESP_OK;
  86. if (!config) {
  87. ret = ESP_ERR_INVALID_ARG;
  88. goto _exit;
  89. }
  90. // check if already initialized
  91. if (s_repl_state != CONSOLE_REPL_STATE_DEINIT) {
  92. ESP_LOGE(TAG, "already initialized");
  93. ret = ESP_ERR_INVALID_STATE;
  94. goto _exit;
  95. }
  96. /* Drain stdout before reconfiguring it */
  97. fflush(stdout);
  98. fsync(fileno(stdout));
  99. /* Disable buffering on stdin */
  100. setvbuf(stdin, NULL, _IONBF, 0);
  101. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  102. esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  103. /* Move the caret to the beginning of the next line on '\n' */
  104. esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  105. /* Configure UART. Note that REF_TICK is used so that the baud rate remains
  106. * correct while APB frequency is changing in light sleep mode.
  107. */
  108. const uart_config_t uart_config = {
  109. .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
  110. .data_bits = UART_DATA_8_BITS,
  111. .parity = UART_PARITY_DISABLE,
  112. .stop_bits = UART_STOP_BITS_1,
  113. .source_clk = UART_SCLK_REF_TICK,
  114. };
  115. /* Install UART driver for interrupt-driven reads and writes */
  116. ret = uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0);
  117. if (ret != ESP_OK) {
  118. goto _exit;
  119. }
  120. uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config);
  121. /* Tell VFS to use UART driver */
  122. esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
  123. /* Initialize the console */
  124. esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
  125. #if CONFIG_LOG_COLORS
  126. console_config.hint_color = atoi(LOG_COLOR_CYAN);
  127. #endif
  128. ret = esp_console_init(&console_config);
  129. if (ret != ESP_OK) {
  130. goto _console_del;
  131. }
  132. ret = esp_console_register_help_command();
  133. if (ret != ESP_OK) {
  134. goto _console_del;
  135. }
  136. ret = esp_console_register_quit_command();
  137. if (ret != ESP_OK) {
  138. goto _console_del;
  139. }
  140. /* Configure linenoise line completion library */
  141. /* Enable multiline editing. If not set, long commands will scroll within single line */
  142. linenoiseSetMultiLine(1);
  143. /* Tell linenoise where to get command completions and hints */
  144. linenoiseSetCompletionCallback(&esp_console_get_completion);
  145. linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
  146. if (config->history_save_path) {
  147. s_history_save_path = config->history_save_path;
  148. /* Load command history from filesystem */
  149. linenoiseHistoryLoad(s_history_save_path);
  150. }
  151. /* Set command history size */
  152. if (linenoiseHistorySetMaxLen(config->max_history_len) != 1) {
  153. ESP_LOGE(TAG, "set max history length to %d failed", config->max_history_len);
  154. ret = ESP_FAIL;
  155. goto _console_del;
  156. }
  157. /* set command line prompt */
  158. const char *prompt_temp = "esp>";
  159. if (config->prompt) {
  160. prompt_temp = config->prompt;
  161. }
  162. snprintf(s_prompt, CONSOLE_PROMPT_LEN_MAX - 1, LOG_COLOR_I "%s " LOG_RESET_COLOR, prompt_temp);
  163. printf("\r\n"
  164. "Type 'help' to get the list of commands.\r\n"
  165. "Use UP/DOWN arrows to navigate through command history.\r\n"
  166. "Press TAB when typing command name to auto-complete.\r\n");
  167. /* Figure out if the terminal supports escape sequences */
  168. int probe_status = linenoiseProbe();
  169. if (probe_status) {
  170. /* zero indicates success */
  171. printf("\r\n"
  172. "Your terminal application does not support escape sequences.\n\n"
  173. "Line editing and history features are disabled.\n\n"
  174. "On Windows, try using Putty instead.\r\n");
  175. linenoiseSetDumbMode(1);
  176. #if CONFIG_LOG_COLORS
  177. /* Since the terminal doesn't support escape sequences,
  178. * don't use color codes in the s_prompt.
  179. */
  180. snprintf(s_prompt, CONSOLE_PROMPT_LEN_MAX - 1, "%s ", prompt_temp);
  181. #endif //CONFIG_LOG_COLORS
  182. }
  183. /* spawn a single thread to run REPL */
  184. if (xTaskCreate(esp_console_repl_thread, "console_repl", config->task_stack_size,
  185. NULL, config->task_priority, &s_repl_task_hdl) != pdTRUE) {
  186. ret = ESP_FAIL;
  187. goto _console_del;
  188. }
  189. s_repl_state = CONSOLE_REPL_STATE_INIT;
  190. return ret;
  191. _console_del:
  192. esp_console_deinit();
  193. esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM);
  194. uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM);
  195. s_repl_state = CONSOLE_REPL_STATE_DEINIT;
  196. _exit:
  197. return ret;
  198. }
  199. esp_err_t esp_console_repl_deinit(void)
  200. {
  201. esp_err_t ret = ESP_OK;
  202. // check if already de-initialized
  203. if (s_repl_state == CONSOLE_REPL_STATE_DEINIT) {
  204. ESP_LOGE(TAG, "not initialized yet");
  205. ret = ESP_ERR_INVALID_STATE;
  206. goto _exit;
  207. }
  208. s_repl_state = CONSOLE_REPL_STATE_DEINIT;
  209. esp_console_deinit();
  210. esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM);
  211. uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM);
  212. s_repl_task_hdl = NULL;
  213. s_history_save_path = NULL;
  214. _exit:
  215. return ret;
  216. }
  217. esp_err_t esp_console_repl_start(void)
  218. {
  219. esp_err_t ret = ESP_OK;
  220. // check if already initialized
  221. if (s_repl_state != CONSOLE_REPL_STATE_INIT) {
  222. ret = ESP_ERR_INVALID_STATE;
  223. goto _exit;
  224. }
  225. s_repl_state = CONSOLE_REPL_STATE_START;
  226. xTaskNotifyGive(s_repl_task_hdl);
  227. _exit:
  228. return ret;
  229. }
  230. /* handle 'quit' command */
  231. static int do_cmd_quit(int argc, char **argv)
  232. {
  233. printf("ByeBye\r\n");
  234. esp_console_repl_deinit();
  235. return 0;
  236. }
  237. esp_err_t esp_console_register_quit_command(void)
  238. {
  239. esp_console_cmd_t command = {
  240. .command = "quit",
  241. .help = "Quit REPL environment",
  242. .func = &do_cmd_quit
  243. };
  244. return esp_console_cmd_register(&command);
  245. }