esp_console_repl.c 19 KB

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