esp_console_repl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <sys/cdefs.h>
  10. #include "sdkconfig.h"
  11. #include "esp_err.h"
  12. #include "esp_log.h"
  13. #include "esp_console.h"
  14. #include "esp_vfs_dev.h"
  15. #include "esp_vfs_cdcacm.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "driver/uart.h"
  19. #include "linenoise/linenoise.h"
  20. static const char *TAG = "console.repl";
  21. #define CONSOLE_PROMPT_MAX_LEN (32)
  22. typedef enum {
  23. CONSOLE_REPL_STATE_DEINIT,
  24. CONSOLE_REPL_STATE_INIT,
  25. CONSOLE_REPL_STATE_START,
  26. } repl_state_t;
  27. typedef struct {
  28. esp_console_repl_t repl_core; // base class
  29. char prompt[CONSOLE_PROMPT_MAX_LEN]; // Prompt to be printed before each line
  30. repl_state_t state;
  31. const char *history_save_path;
  32. TaskHandle_t task_hdl; // REPL task handle
  33. } esp_console_repl_com_t;
  34. typedef struct {
  35. esp_console_repl_com_t repl_com; // base class
  36. int uart_channel; // uart channel number
  37. } esp_console_repl_uart_t;
  38. typedef struct {
  39. esp_console_repl_com_t repl_com; // base class
  40. } esp_console_repl_usb_cdc_t;
  41. static void esp_console_repl_task(void *args);
  42. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl);
  43. static esp_err_t esp_console_repl_usb_cdc_delete(esp_console_repl_t *repl);
  44. static esp_err_t esp_console_common_init(esp_console_repl_com_t *repl_com);
  45. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com);
  46. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com);
  47. esp_err_t esp_console_new_repl_usb_cdc(const esp_console_dev_usb_cdc_config_t *dev_config, const esp_console_repl_config_t *repl_config, esp_console_repl_t **ret_repl)
  48. {
  49. esp_err_t ret = ESP_OK;
  50. esp_console_repl_usb_cdc_t *cdc_repl = NULL;
  51. if (!repl_config | !dev_config | !ret_repl) {
  52. ret = ESP_ERR_INVALID_ARG;
  53. goto _exit;
  54. }
  55. // allocate memory for console REPL context
  56. cdc_repl = calloc(1, sizeof(esp_console_repl_usb_cdc_t));
  57. if (!cdc_repl) {
  58. ret = ESP_ERR_NO_MEM;
  59. goto _exit;
  60. }
  61. /* Disable buffering on stdin */
  62. setvbuf(stdin, NULL, _IONBF, 0);
  63. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  64. esp_vfs_dev_cdcacm_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  65. /* Move the caret to the beginning of the next line on '\n' */
  66. esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  67. /* Enable non-blocking mode on stdin and stdout */
  68. fcntl(fileno(stdout), F_SETFL, 0);
  69. fcntl(fileno(stdin), F_SETFL, 0);
  70. // initialize console, common part
  71. ret = esp_console_common_init(&cdc_repl->repl_com);
  72. if (ret != ESP_OK) {
  73. goto _exit;
  74. }
  75. // setup history
  76. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &cdc_repl->repl_com);
  77. if (ret != ESP_OK) {
  78. goto _exit;
  79. }
  80. // setup prompt
  81. esp_console_setup_prompt(repl_config->prompt, &cdc_repl->repl_com);
  82. /* spawn a single thread to run REPL */
  83. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  84. &cdc_repl->repl_com, repl_config->task_priority, &cdc_repl->repl_com.task_hdl) != pdTRUE) {
  85. ret = ESP_FAIL;
  86. goto _exit;
  87. }
  88. cdc_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  89. cdc_repl->repl_com.repl_core.del = esp_console_repl_usb_cdc_delete;
  90. *ret_repl = &cdc_repl->repl_com.repl_core;
  91. return ESP_OK;
  92. _exit:
  93. if (cdc_repl) {
  94. esp_console_deinit();
  95. free(cdc_repl);
  96. }
  97. if (ret_repl) {
  98. *ret_repl = NULL;
  99. }
  100. return ret;
  101. }
  102. esp_err_t esp_console_new_repl_uart(const esp_console_dev_uart_config_t *dev_config, const esp_console_repl_config_t *repl_config, esp_console_repl_t **ret_repl)
  103. {
  104. esp_err_t ret = ESP_OK;
  105. esp_console_repl_uart_t *uart_repl = NULL;
  106. if (!repl_config | !dev_config | !ret_repl) {
  107. ret = ESP_ERR_INVALID_ARG;
  108. goto _exit;
  109. }
  110. // allocate memory for console REPL context
  111. uart_repl = calloc(1, sizeof(esp_console_repl_uart_t));
  112. if (!uart_repl) {
  113. ret = ESP_ERR_NO_MEM;
  114. goto _exit;
  115. }
  116. /* Drain stdout before reconfiguring it */
  117. fflush(stdout);
  118. fsync(fileno(stdout));
  119. /* Disable buffering on stdin */
  120. setvbuf(stdin, NULL, _IONBF, 0);
  121. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  122. esp_vfs_dev_uart_port_set_rx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CR);
  123. /* Move the caret to the beginning of the next line on '\n' */
  124. esp_vfs_dev_uart_port_set_tx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CRLF);
  125. /* Configure UART. Note that REF_TICK/XTAL is used so that the baud rate remains
  126. * correct while APB frequency is changing in light sleep mode.
  127. */
  128. const uart_config_t uart_config = {
  129. .baud_rate = dev_config->baud_rate,
  130. .data_bits = UART_DATA_8_BITS,
  131. .parity = UART_PARITY_DISABLE,
  132. .stop_bits = UART_STOP_BITS_1,
  133. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  134. .source_clk = UART_SCLK_REF_TICK,
  135. #else
  136. .source_clk = UART_SCLK_XTAL,
  137. #endif
  138. };
  139. uart_param_config(dev_config->channel, &uart_config);
  140. uart_set_pin(dev_config->channel, dev_config->tx_gpio_num, dev_config->rx_gpio_num, -1, -1);
  141. /* Install UART driver for interrupt-driven reads and writes */
  142. ret = uart_driver_install(dev_config->channel, 256, 0, 0, NULL, 0);
  143. if (ret != ESP_OK) {
  144. goto _exit;
  145. }
  146. /* Tell VFS to use UART driver */
  147. esp_vfs_dev_uart_use_driver(dev_config->channel);
  148. // initialize console, common part
  149. ret = esp_console_common_init(&uart_repl->repl_com);
  150. if (ret != ESP_OK) {
  151. goto _exit;
  152. }
  153. // setup history
  154. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &uart_repl->repl_com);
  155. if (ret != ESP_OK) {
  156. goto _exit;
  157. }
  158. // setup prompt
  159. esp_console_setup_prompt(repl_config->prompt, &uart_repl->repl_com);
  160. /* spawn a single thread to run REPL */
  161. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  162. &uart_repl->repl_com, repl_config->task_priority, &uart_repl->repl_com.task_hdl) != pdTRUE) {
  163. ret = ESP_FAIL;
  164. goto _exit;
  165. }
  166. uart_repl->uart_channel = dev_config->channel;
  167. uart_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  168. uart_repl->repl_com.repl_core.del = esp_console_repl_uart_delete;
  169. *ret_repl = &uart_repl->repl_com.repl_core;
  170. return ESP_OK;
  171. _exit:
  172. if (uart_repl) {
  173. esp_console_deinit();
  174. uart_driver_delete(dev_config->channel);
  175. free(uart_repl);
  176. }
  177. if (ret_repl) {
  178. *ret_repl = NULL;
  179. }
  180. return ret;
  181. }
  182. esp_err_t esp_console_start_repl(esp_console_repl_t *repl)
  183. {
  184. esp_err_t ret = ESP_OK;
  185. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  186. // check if already initialized
  187. if (repl_com->state != CONSOLE_REPL_STATE_INIT) {
  188. ret = ESP_ERR_INVALID_STATE;
  189. goto _exit;
  190. }
  191. repl_com->state = CONSOLE_REPL_STATE_START;
  192. xTaskNotifyGive(repl_com->task_hdl);
  193. return ESP_OK;
  194. _exit:
  195. return ret;
  196. }
  197. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com)
  198. {
  199. /* set command line prompt */
  200. const char *prompt_temp = "esp>";
  201. if (prompt) {
  202. prompt_temp = prompt;
  203. }
  204. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, LOG_COLOR_I "%s " LOG_RESET_COLOR, prompt_temp);
  205. printf("\r\n"
  206. "Type 'help' to get the list of commands.\r\n"
  207. "Use UP/DOWN arrows to navigate through command history.\r\n"
  208. "Press TAB when typing command name to auto-complete.\r\n");
  209. /* Figure out if the terminal supports escape sequences */
  210. int probe_status = linenoiseProbe();
  211. if (probe_status) {
  212. /* zero indicates success */
  213. printf("\r\n"
  214. "Your terminal application does not support escape sequences.\n\n"
  215. "Line editing and history features are disabled.\n\n"
  216. "On Windows, try using Putty instead.\r\n");
  217. linenoiseSetDumbMode(1);
  218. #if CONFIG_LOG_COLORS
  219. /* Since the terminal doesn't support escape sequences,
  220. * don't use color codes in the s_prompt.
  221. */
  222. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, "%s ", prompt_temp);
  223. #endif //CONFIG_LOG_COLORS
  224. }
  225. return ESP_OK;
  226. }
  227. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com)
  228. {
  229. esp_err_t ret = ESP_OK;
  230. repl_com->history_save_path = history_path;
  231. if (history_path) {
  232. /* Load command history from filesystem */
  233. linenoiseHistoryLoad(history_path);
  234. }
  235. /* Set command history size */
  236. if (linenoiseHistorySetMaxLen(max_history_len) != 1) {
  237. ESP_LOGE(TAG, "set max history length to %d failed", max_history_len);
  238. ret = ESP_FAIL;
  239. goto _exit;
  240. }
  241. return ESP_OK;
  242. _exit:
  243. return ret;
  244. }
  245. static esp_err_t esp_console_common_init(esp_console_repl_com_t *repl_com)
  246. {
  247. esp_err_t ret = ESP_OK;
  248. /* Initialize the console */
  249. esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
  250. #if CONFIG_LOG_COLORS
  251. console_config.hint_color = atoi(LOG_COLOR_CYAN);
  252. #endif
  253. ret = esp_console_init(&console_config);
  254. if (ret != ESP_OK) {
  255. goto _exit;
  256. }
  257. ret = esp_console_register_help_command();
  258. if (ret != ESP_OK) {
  259. goto _exit;
  260. }
  261. /* Configure linenoise line completion library */
  262. /* Enable multiline editing. If not set, long commands will scroll within single line */
  263. linenoiseSetMultiLine(1);
  264. /* Tell linenoise where to get command completions and hints */
  265. linenoiseSetCompletionCallback(&esp_console_get_completion);
  266. linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
  267. return ESP_OK;
  268. _exit:
  269. return ret;
  270. }
  271. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl)
  272. {
  273. esp_err_t ret = ESP_OK;
  274. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  275. esp_console_repl_uart_t *uart_repl = __containerof(repl_com, esp_console_repl_uart_t, repl_com);
  276. // check if already de-initialized
  277. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  278. ESP_LOGE(TAG, "already de-initialized");
  279. ret = ESP_ERR_INVALID_STATE;
  280. goto _exit;
  281. }
  282. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  283. esp_console_deinit();
  284. esp_vfs_dev_uart_use_nonblocking(uart_repl->uart_channel);
  285. uart_driver_delete(uart_repl->uart_channel);
  286. free(uart_repl);
  287. _exit:
  288. return ret;
  289. }
  290. static esp_err_t esp_console_repl_usb_cdc_delete(esp_console_repl_t *repl)
  291. {
  292. esp_err_t ret = ESP_OK;
  293. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  294. esp_console_repl_usb_cdc_t *cdc_repl = __containerof(repl_com, esp_console_repl_usb_cdc_t, repl_com);
  295. // check if already de-initialized
  296. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  297. ESP_LOGE(TAG, "already de-initialized");
  298. ret = ESP_ERR_INVALID_STATE;
  299. goto _exit;
  300. }
  301. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  302. esp_console_deinit();
  303. free(cdc_repl);
  304. _exit:
  305. return ret;
  306. }
  307. static void esp_console_repl_task(void *args)
  308. {
  309. esp_console_repl_com_t *repl_com = (esp_console_repl_com_t *)args;
  310. // waiting for task notify
  311. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  312. while (repl_com->state == CONSOLE_REPL_STATE_START) {
  313. char *line = linenoise(repl_com->prompt);
  314. if (line == NULL) {
  315. ESP_LOGD(TAG, "empty line");
  316. /* Ignore empty lines */
  317. continue;
  318. }
  319. /* Add the command to the history */
  320. linenoiseHistoryAdd(line);
  321. /* Save command history to filesystem */
  322. if (repl_com->history_save_path) {
  323. linenoiseHistorySave(repl_com->history_save_path);
  324. }
  325. /* Try to run the command */
  326. int ret;
  327. esp_err_t err = esp_console_run(line, &ret);
  328. if (err == ESP_ERR_NOT_FOUND) {
  329. printf("Unrecognized command\n");
  330. } else if (err == ESP_ERR_INVALID_ARG) {
  331. // command was empty
  332. } else if (err == ESP_OK && ret != ESP_OK) {
  333. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
  334. } else if (err != ESP_OK) {
  335. printf("Internal error: %s\n", esp_err_to_name(err));
  336. }
  337. /* linenoise allocates line buffer on the heap, so need to free it */
  338. linenoiseFree(line);
  339. }
  340. ESP_LOGD(TAG, "The End");
  341. vTaskDelete(NULL);
  342. }