esp_console_repl.c 8.7 KB

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