esp_console_repl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 "esp_vfs_usb_serial_jtag.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "driver/uart.h"
  20. #include "driver/usb_serial_jtag.h"
  21. #include "linenoise/linenoise.h"
  22. static const char *TAG = "console.repl";
  23. #define CONSOLE_PROMPT_MAX_LEN (32)
  24. #define CONSOLE_PATH_MAX_LEN (ESP_VFS_PATH_MAX)
  25. typedef enum {
  26. CONSOLE_REPL_STATE_DEINIT,
  27. CONSOLE_REPL_STATE_INIT,
  28. CONSOLE_REPL_STATE_START,
  29. } repl_state_t;
  30. typedef struct {
  31. esp_console_repl_t repl_core; // base class
  32. char prompt[CONSOLE_PROMPT_MAX_LEN]; // Prompt to be printed before each line
  33. repl_state_t state;
  34. const char *history_save_path;
  35. TaskHandle_t task_hdl; // REPL task handle
  36. size_t max_cmdline_length; // Maximum length of a command line. If 0, default value will be used.
  37. } esp_console_repl_com_t;
  38. typedef struct {
  39. esp_console_repl_com_t repl_com; // base class
  40. int uart_channel; // uart channel number
  41. } esp_console_repl_universal_t;
  42. static void esp_console_repl_task(void *args);
  43. #if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  44. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl);
  45. #endif // CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  46. #if CONFIG_ESP_CONSOLE_USB_CDC
  47. static esp_err_t esp_console_repl_usb_cdc_delete(esp_console_repl_t *repl);
  48. #endif // CONFIG_ESP_CONSOLE_USB_CDC
  49. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  50. static esp_err_t esp_console_repl_usb_serial_jtag_delete(esp_console_repl_t *repl);
  51. #endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  52. static esp_err_t esp_console_common_init(size_t max_cmdline_length, esp_console_repl_com_t *repl_com);
  53. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com);
  54. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com);
  55. #if CONFIG_ESP_CONSOLE_USB_CDC
  56. 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)
  57. {
  58. esp_err_t ret = ESP_OK;
  59. esp_console_repl_universal_t *cdc_repl = NULL;
  60. if (!repl_config | !dev_config | !ret_repl) {
  61. ret = ESP_ERR_INVALID_ARG;
  62. goto _exit;
  63. }
  64. // allocate memory for console REPL context
  65. cdc_repl = calloc(1, sizeof(esp_console_repl_universal_t));
  66. if (!cdc_repl) {
  67. ret = ESP_ERR_NO_MEM;
  68. goto _exit;
  69. }
  70. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  71. esp_vfs_dev_cdcacm_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  72. /* Move the caret to the beginning of the next line on '\n' */
  73. esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  74. /* Enable blocking mode on stdin and stdout */
  75. fcntl(fileno(stdout), F_SETFL, 0);
  76. fcntl(fileno(stdin), F_SETFL, 0);
  77. // initialize console, common part
  78. ret = esp_console_common_init(repl_config->max_cmdline_length, &cdc_repl->repl_com);
  79. if (ret != ESP_OK) {
  80. goto _exit;
  81. }
  82. // setup history
  83. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &cdc_repl->repl_com);
  84. if (ret != ESP_OK) {
  85. goto _exit;
  86. }
  87. // setup prompt
  88. esp_console_setup_prompt(repl_config->prompt, &cdc_repl->repl_com);
  89. /* Fill the structure here as it will be used directly by the created task. */
  90. cdc_repl->uart_channel = CONFIG_ESP_CONSOLE_UART_NUM;
  91. cdc_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  92. cdc_repl->repl_com.repl_core.del = esp_console_repl_usb_cdc_delete;
  93. /* spawn a single thread to run REPL */
  94. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  95. cdc_repl, repl_config->task_priority, &cdc_repl->repl_com.task_hdl) != pdTRUE) {
  96. ret = ESP_FAIL;
  97. goto _exit;
  98. }
  99. *ret_repl = &cdc_repl->repl_com.repl_core;
  100. return ESP_OK;
  101. _exit:
  102. if (cdc_repl) {
  103. esp_console_deinit();
  104. free(cdc_repl);
  105. }
  106. if (ret_repl) {
  107. *ret_repl = NULL;
  108. }
  109. return ret;
  110. }
  111. #endif // CONFIG_ESP_CONSOLE_USB_CDC
  112. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  113. esp_err_t esp_console_new_repl_usb_serial_jtag(const esp_console_dev_usb_serial_jtag_config_t *dev_config, const esp_console_repl_config_t *repl_config, esp_console_repl_t **ret_repl)
  114. {
  115. esp_console_repl_universal_t *usb_serial_jtag_repl = NULL;
  116. if (!repl_config | !dev_config | !ret_repl) {
  117. return ESP_ERR_INVALID_ARG;
  118. }
  119. esp_err_t ret = ESP_OK;
  120. // allocate memory for console REPL context
  121. usb_serial_jtag_repl = calloc(1, sizeof(esp_console_repl_universal_t));
  122. if (!usb_serial_jtag_repl) {
  123. ret = ESP_ERR_NO_MEM;
  124. goto _exit;
  125. }
  126. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  127. esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  128. /* Move the caret to the beginning of the next line on '\n' */
  129. esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  130. /* Enable blocking mode on stdin and stdout */
  131. fcntl(fileno(stdout), F_SETFL, 0);
  132. fcntl(fileno(stdin), F_SETFL, 0);
  133. usb_serial_jtag_driver_config_t usb_serial_jtag_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
  134. /* Install USB-SERIAL-JTAG driver for interrupt-driven reads and writes */
  135. ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
  136. if (ret != ESP_OK) {
  137. goto _exit;
  138. }
  139. // initialize console, common part
  140. ret = esp_console_common_init(repl_config->max_cmdline_length, &usb_serial_jtag_repl->repl_com);
  141. if (ret != ESP_OK) {
  142. goto _exit;
  143. }
  144. /* Tell vfs to use usb-serial-jtag driver */
  145. esp_vfs_usb_serial_jtag_use_driver();
  146. // setup history
  147. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &usb_serial_jtag_repl->repl_com);
  148. if (ret != ESP_OK) {
  149. goto _exit;
  150. }
  151. // setup prompt
  152. esp_console_setup_prompt(repl_config->prompt, &usb_serial_jtag_repl->repl_com);
  153. /* Fill the structure here as it will be used directly by the created task. */
  154. usb_serial_jtag_repl->uart_channel = CONFIG_ESP_CONSOLE_UART_NUM;
  155. usb_serial_jtag_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  156. usb_serial_jtag_repl->repl_com.repl_core.del = esp_console_repl_usb_serial_jtag_delete;
  157. /* spawn a single thread to run REPL */
  158. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  159. usb_serial_jtag_repl, repl_config->task_priority, &usb_serial_jtag_repl->repl_com.task_hdl) != pdTRUE) {
  160. ret = ESP_FAIL;
  161. goto _exit;
  162. }
  163. *ret_repl = &usb_serial_jtag_repl->repl_com.repl_core;
  164. return ESP_OK;
  165. _exit:
  166. if (usb_serial_jtag_repl) {
  167. esp_console_deinit();
  168. free(usb_serial_jtag_repl);
  169. }
  170. if (ret_repl) {
  171. *ret_repl = NULL;
  172. }
  173. return ret;
  174. }
  175. #endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  176. #if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  177. 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)
  178. {
  179. esp_err_t ret = ESP_OK;
  180. esp_console_repl_universal_t *uart_repl = NULL;
  181. if (!repl_config | !dev_config | !ret_repl) {
  182. ret = ESP_ERR_INVALID_ARG;
  183. goto _exit;
  184. }
  185. // allocate memory for console REPL context
  186. uart_repl = calloc(1, sizeof(esp_console_repl_universal_t));
  187. if (!uart_repl) {
  188. ret = ESP_ERR_NO_MEM;
  189. goto _exit;
  190. }
  191. /* Drain stdout before reconfiguring it */
  192. fflush(stdout);
  193. fsync(fileno(stdout));
  194. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  195. esp_vfs_dev_uart_port_set_rx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CR);
  196. /* Move the caret to the beginning of the next line on '\n' */
  197. esp_vfs_dev_uart_port_set_tx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CRLF);
  198. /* Configure UART. Note that REF_TICK/XTAL is used so that the baud rate remains
  199. * correct while APB frequency is changing in light sleep mode.
  200. */
  201. #if SOC_UART_SUPPORT_REF_TICK
  202. uart_sclk_t clk_source = UART_SCLK_REF_TICK;
  203. // REF_TICK clock can't provide a high baudrate
  204. if (dev_config->baud_rate > 1 * 1000 * 1000) {
  205. clk_source = UART_SCLK_DEFAULT;
  206. ESP_LOGW(TAG, "light sleep UART wakeup might not work at the configured baud rate");
  207. }
  208. #elif SOC_UART_SUPPORT_XTAL_CLK
  209. uart_sclk_t clk_source = UART_SCLK_XTAL;
  210. #else
  211. #error "No UART clock source is aware of DFS"
  212. #endif // SOC_UART_SUPPORT_xxx
  213. const uart_config_t uart_config = {
  214. .baud_rate = dev_config->baud_rate,
  215. .data_bits = UART_DATA_8_BITS,
  216. .parity = UART_PARITY_DISABLE,
  217. .stop_bits = UART_STOP_BITS_1,
  218. .source_clk = clk_source,
  219. };
  220. uart_param_config(dev_config->channel, &uart_config);
  221. uart_set_pin(dev_config->channel, dev_config->tx_gpio_num, dev_config->rx_gpio_num, -1, -1);
  222. /* Install UART driver for interrupt-driven reads and writes */
  223. ret = uart_driver_install(dev_config->channel, 256, 0, 0, NULL, 0);
  224. if (ret != ESP_OK) {
  225. goto _exit;
  226. }
  227. /* Tell VFS to use UART driver */
  228. esp_vfs_dev_uart_use_driver(dev_config->channel);
  229. // initialize console, common part
  230. ret = esp_console_common_init(repl_config->max_cmdline_length, &uart_repl->repl_com);
  231. if (ret != ESP_OK) {
  232. goto _exit;
  233. }
  234. // setup history
  235. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &uart_repl->repl_com);
  236. if (ret != ESP_OK) {
  237. goto _exit;
  238. }
  239. // setup prompt
  240. esp_console_setup_prompt(repl_config->prompt, &uart_repl->repl_com);
  241. /* Fill the structure here as it will be used directly by the created task. */
  242. uart_repl->uart_channel = dev_config->channel;
  243. uart_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  244. uart_repl->repl_com.repl_core.del = esp_console_repl_uart_delete;
  245. /* Spawn a single thread to run REPL, we need to pass `uart_repl` to it as
  246. * it also requires the uart channel. */
  247. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  248. uart_repl, repl_config->task_priority, &uart_repl->repl_com.task_hdl) != pdTRUE) {
  249. ret = ESP_FAIL;
  250. goto _exit;
  251. }
  252. *ret_repl = &uart_repl->repl_com.repl_core;
  253. return ESP_OK;
  254. _exit:
  255. if (uart_repl) {
  256. esp_console_deinit();
  257. uart_driver_delete(dev_config->channel);
  258. free(uart_repl);
  259. }
  260. if (ret_repl) {
  261. *ret_repl = NULL;
  262. }
  263. return ret;
  264. }
  265. #endif // CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  266. esp_err_t esp_console_start_repl(esp_console_repl_t *repl)
  267. {
  268. esp_err_t ret = ESP_OK;
  269. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  270. // check if already initialized
  271. if (repl_com->state != CONSOLE_REPL_STATE_INIT) {
  272. ret = ESP_ERR_INVALID_STATE;
  273. goto _exit;
  274. }
  275. repl_com->state = CONSOLE_REPL_STATE_START;
  276. xTaskNotifyGive(repl_com->task_hdl);
  277. return ESP_OK;
  278. _exit:
  279. return ret;
  280. }
  281. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com)
  282. {
  283. /* set command line prompt */
  284. const char *prompt_temp = "esp>";
  285. if (prompt) {
  286. prompt_temp = prompt;
  287. }
  288. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, LOG_COLOR_I "%s " LOG_RESET_COLOR, prompt_temp);
  289. /* Figure out if the terminal supports escape sequences */
  290. int probe_status = linenoiseProbe();
  291. if (probe_status) {
  292. /* zero indicates success */
  293. linenoiseSetDumbMode(1);
  294. #if CONFIG_LOG_COLORS
  295. /* Since the terminal doesn't support escape sequences,
  296. * don't use color codes in the s_prompt.
  297. */
  298. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, "%s ", prompt_temp);
  299. #endif //CONFIG_LOG_COLORS
  300. }
  301. return ESP_OK;
  302. }
  303. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com)
  304. {
  305. esp_err_t ret = ESP_OK;
  306. repl_com->history_save_path = history_path;
  307. if (history_path) {
  308. /* Load command history from filesystem */
  309. linenoiseHistoryLoad(history_path);
  310. }
  311. /* Set command history size */
  312. if (linenoiseHistorySetMaxLen(max_history_len) != 1) {
  313. ESP_LOGE(TAG, "set max history length to %"PRIu32" failed", max_history_len);
  314. ret = ESP_FAIL;
  315. goto _exit;
  316. }
  317. return ESP_OK;
  318. _exit:
  319. return ret;
  320. }
  321. static esp_err_t esp_console_common_init(size_t max_cmdline_length, esp_console_repl_com_t *repl_com)
  322. {
  323. esp_err_t ret = ESP_OK;
  324. /* Initialize the console */
  325. esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
  326. repl_com->max_cmdline_length = console_config.max_cmdline_length;
  327. /* Replace the default command line length if passed as a parameter */
  328. if (max_cmdline_length != 0) {
  329. console_config.max_cmdline_length = max_cmdline_length;
  330. repl_com->max_cmdline_length = max_cmdline_length;
  331. }
  332. #if CONFIG_LOG_COLORS
  333. console_config.hint_color = atoi(LOG_COLOR_CYAN);
  334. #else
  335. console_config.hint_color = -1;
  336. #endif
  337. ret = esp_console_init(&console_config);
  338. if (ret != ESP_OK) {
  339. goto _exit;
  340. }
  341. ret = esp_console_register_help_command();
  342. if (ret != ESP_OK) {
  343. goto _exit;
  344. }
  345. /* Configure linenoise line completion library */
  346. /* Enable multiline editing. If not set, long commands will scroll within single line */
  347. linenoiseSetMultiLine(1);
  348. /* Tell linenoise where to get command completions and hints */
  349. linenoiseSetCompletionCallback(&esp_console_get_completion);
  350. linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
  351. return ESP_OK;
  352. _exit:
  353. return ret;
  354. }
  355. #if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  356. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl)
  357. {
  358. esp_err_t ret = ESP_OK;
  359. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  360. esp_console_repl_universal_t *uart_repl = __containerof(repl_com, esp_console_repl_universal_t, repl_com);
  361. // check if already de-initialized
  362. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  363. ESP_LOGE(TAG, "already de-initialized");
  364. ret = ESP_ERR_INVALID_STATE;
  365. goto _exit;
  366. }
  367. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  368. esp_console_deinit();
  369. esp_vfs_dev_uart_use_nonblocking(uart_repl->uart_channel);
  370. uart_driver_delete(uart_repl->uart_channel);
  371. free(uart_repl);
  372. _exit:
  373. return ret;
  374. }
  375. #endif // CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
  376. #if CONFIG_ESP_CONSOLE_USB_CDC
  377. static esp_err_t esp_console_repl_usb_cdc_delete(esp_console_repl_t *repl)
  378. {
  379. esp_err_t ret = ESP_OK;
  380. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  381. esp_console_repl_universal_t *cdc_repl = __containerof(repl_com, esp_console_repl_universal_t, repl_com);
  382. // check if already de-initialized
  383. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  384. ESP_LOGE(TAG, "already de-initialized");
  385. ret = ESP_ERR_INVALID_STATE;
  386. goto _exit;
  387. }
  388. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  389. esp_console_deinit();
  390. free(cdc_repl);
  391. _exit:
  392. return ret;
  393. }
  394. #endif // CONFIG_ESP_CONSOLE_USB_CDC
  395. #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  396. static esp_err_t esp_console_repl_usb_serial_jtag_delete(esp_console_repl_t *repl)
  397. {
  398. esp_err_t ret = ESP_OK;
  399. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  400. esp_console_repl_universal_t *usb_serial_jtag_repl = __containerof(repl_com, esp_console_repl_universal_t, repl_com);
  401. // check if already de-initialized
  402. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  403. ESP_LOGE(TAG, "already de-initialized");
  404. ret = ESP_ERR_INVALID_STATE;
  405. goto _exit;
  406. }
  407. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  408. esp_console_deinit();
  409. esp_vfs_usb_serial_jtag_use_nonblocking();
  410. usb_serial_jtag_driver_uninstall();
  411. free(usb_serial_jtag_repl);
  412. _exit:
  413. return ret;
  414. }
  415. #endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
  416. static void esp_console_repl_task(void *args)
  417. {
  418. esp_console_repl_universal_t *repl_conf = (esp_console_repl_universal_t *) args;
  419. esp_console_repl_com_t *repl_com = &repl_conf->repl_com;
  420. const int uart_channel = repl_conf->uart_channel;
  421. /* Waiting for task notify. This happens when `esp_console_start_repl()`
  422. * function is called. */
  423. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  424. /* Change standard input and output of the task if the requested UART is
  425. * NOT the default one. This block will replace stdin, stdout and stderr.
  426. */
  427. if (uart_channel != CONFIG_ESP_CONSOLE_UART_NUM) {
  428. char path[CONSOLE_PATH_MAX_LEN] = { 0 };
  429. snprintf(path, CONSOLE_PATH_MAX_LEN, "/dev/uart/%d", uart_channel);
  430. stdin = fopen(path, "r");
  431. stdout = fopen(path, "w");
  432. stderr = stdout;
  433. }
  434. /* Disable buffering on stdin of the current task.
  435. * If the console is ran on a different UART than the default one,
  436. * buffering shall only be disabled for the current one. */
  437. setvbuf(stdin, NULL, _IONBF, 0);
  438. /* This message shall be printed here and not earlier as the stdout
  439. * has just been set above. */
  440. printf("\r\n"
  441. "Type 'help' to get the list of commands.\r\n"
  442. "Use UP/DOWN arrows to navigate through command history.\r\n"
  443. "Press TAB when typing command name to auto-complete.\r\n");
  444. if (linenoiseIsDumbMode()) {
  445. printf("\r\n"
  446. "Your terminal application does not support escape sequences.\n\n"
  447. "Line editing and history features are disabled.\n\n"
  448. "On Windows, try using Putty instead.\r\n");
  449. }
  450. linenoiseSetMaxLineLen(repl_com->max_cmdline_length);
  451. while (repl_com->state == CONSOLE_REPL_STATE_START) {
  452. char *line = linenoise(repl_com->prompt);
  453. if (line == NULL) {
  454. ESP_LOGD(TAG, "empty line");
  455. /* Ignore empty lines */
  456. continue;
  457. }
  458. /* Add the command to the history */
  459. linenoiseHistoryAdd(line);
  460. /* Save command history to filesystem */
  461. if (repl_com->history_save_path) {
  462. linenoiseHistorySave(repl_com->history_save_path);
  463. }
  464. /* Try to run the command */
  465. int ret;
  466. esp_err_t err = esp_console_run(line, &ret);
  467. if (err == ESP_ERR_NOT_FOUND) {
  468. printf("Unrecognized command\n");
  469. } else if (err == ESP_ERR_INVALID_ARG) {
  470. // command was empty
  471. } else if (err == ESP_OK && ret != ESP_OK) {
  472. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
  473. } else if (err != ESP_OK) {
  474. printf("Internal error: %s\n", esp_err_to_name(err));
  475. }
  476. /* linenoise allocates line buffer on the heap, so need to free it */
  477. linenoiseFree(line);
  478. }
  479. ESP_LOGD(TAG, "The End");
  480. vTaskDelete(NULL);
  481. }