esp_console_repl.c 18 KB

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