esp_console_repl.c 14 KB

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