esp_console_repl.c 19 KB

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