esp_console_repl.c 13 KB

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