esp_console_repl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 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. .source_clk = UART_SCLK_REF_TICK,
  142. };
  143. uart_param_config(dev_config->channel, &uart_config);
  144. uart_set_pin(dev_config->channel, dev_config->tx_gpio_num, dev_config->rx_gpio_num, -1, -1);
  145. /* Install UART driver for interrupt-driven reads and writes */
  146. ret = uart_driver_install(dev_config->channel, 256, 0, 0, NULL, 0);
  147. if (ret != ESP_OK) {
  148. goto _exit;
  149. }
  150. /* Tell VFS to use UART driver */
  151. esp_vfs_dev_uart_use_driver(dev_config->channel);
  152. // initialize console, common part
  153. ret = esp_console_common_init(&uart_repl->repl_com);
  154. if (ret != ESP_OK) {
  155. goto _exit;
  156. }
  157. // setup history
  158. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &uart_repl->repl_com);
  159. if (ret != ESP_OK) {
  160. goto _exit;
  161. }
  162. // setup prompt
  163. esp_console_setup_prompt(repl_config->prompt, &uart_repl->repl_com);
  164. /* spawn a single thread to run REPL */
  165. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  166. &uart_repl->repl_com, repl_config->task_priority, &uart_repl->repl_com.task_hdl) != pdTRUE) {
  167. ret = ESP_FAIL;
  168. goto _exit;
  169. }
  170. uart_repl->uart_channel = dev_config->channel;
  171. uart_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  172. uart_repl->repl_com.repl_core.del = esp_console_repl_uart_delete;
  173. *ret_repl = &uart_repl->repl_com.repl_core;
  174. return ESP_OK;
  175. _exit:
  176. if (uart_repl) {
  177. esp_console_deinit();
  178. uart_driver_delete(dev_config->channel);
  179. free(uart_repl);
  180. }
  181. if (ret_repl) {
  182. *ret_repl = NULL;
  183. }
  184. return ret;
  185. }
  186. esp_err_t esp_console_start_repl(esp_console_repl_t *repl)
  187. {
  188. esp_err_t ret = ESP_OK;
  189. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  190. // check if already initialized
  191. if (repl_com->state != CONSOLE_REPL_STATE_INIT) {
  192. ret = ESP_ERR_INVALID_STATE;
  193. goto _exit;
  194. }
  195. repl_com->state = CONSOLE_REPL_STATE_START;
  196. xTaskNotifyGive(repl_com->task_hdl);
  197. return ESP_OK;
  198. _exit:
  199. return ret;
  200. }
  201. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com)
  202. {
  203. /* set command line prompt */
  204. const char *prompt_temp = "esp>";
  205. if (prompt) {
  206. prompt_temp = prompt;
  207. }
  208. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, LOG_COLOR_I "%s " LOG_RESET_COLOR, prompt_temp);
  209. printf("\r\n"
  210. "Type 'help' to get the list of commands.\r\n"
  211. "Use UP/DOWN arrows to navigate through command history.\r\n"
  212. "Press TAB when typing command name to auto-complete.\r\n");
  213. /* Figure out if the terminal supports escape sequences */
  214. int probe_status = linenoiseProbe();
  215. if (probe_status) {
  216. /* zero indicates success */
  217. printf("\r\n"
  218. "Your terminal application does not support escape sequences.\n\n"
  219. "Line editing and history features are disabled.\n\n"
  220. "On Windows, try using Putty instead.\r\n");
  221. linenoiseSetDumbMode(1);
  222. #if CONFIG_LOG_COLORS
  223. /* Since the terminal doesn't support escape sequences,
  224. * don't use color codes in the s_prompt.
  225. */
  226. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, "%s ", prompt_temp);
  227. #endif //CONFIG_LOG_COLORS
  228. }
  229. return ESP_OK;
  230. }
  231. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com)
  232. {
  233. esp_err_t ret = ESP_OK;
  234. repl_com->history_save_path = history_path;
  235. if (history_path) {
  236. /* Load command history from filesystem */
  237. linenoiseHistoryLoad(history_path);
  238. }
  239. /* Set command history size */
  240. if (linenoiseHistorySetMaxLen(max_history_len) != 1) {
  241. ESP_LOGE(TAG, "set max history length to %d failed", max_history_len);
  242. ret = ESP_FAIL;
  243. goto _exit;
  244. }
  245. return ESP_OK;
  246. _exit:
  247. return ret;
  248. }
  249. static esp_err_t esp_console_common_init(esp_console_repl_com_t *repl_com)
  250. {
  251. esp_err_t ret = ESP_OK;
  252. /* Initialize the console */
  253. esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
  254. #if CONFIG_LOG_COLORS
  255. console_config.hint_color = atoi(LOG_COLOR_CYAN);
  256. #endif
  257. ret = esp_console_init(&console_config);
  258. if (ret != ESP_OK) {
  259. goto _exit;
  260. }
  261. ret = esp_console_register_help_command();
  262. if (ret != ESP_OK) {
  263. goto _exit;
  264. }
  265. /* Configure linenoise line completion library */
  266. /* Enable multiline editing. If not set, long commands will scroll within single line */
  267. linenoiseSetMultiLine(1);
  268. /* Tell linenoise where to get command completions and hints */
  269. linenoiseSetCompletionCallback(&esp_console_get_completion);
  270. linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
  271. return ESP_OK;
  272. _exit:
  273. return ret;
  274. }
  275. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl)
  276. {
  277. esp_err_t ret = ESP_OK;
  278. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  279. esp_console_repl_uart_t *uart_repl = __containerof(repl_com, esp_console_repl_uart_t, repl_com);
  280. // check if already de-initialized
  281. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  282. ESP_LOGE(TAG, "already de-initialized");
  283. ret = ESP_ERR_INVALID_STATE;
  284. goto _exit;
  285. }
  286. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  287. esp_console_deinit();
  288. esp_vfs_dev_uart_use_nonblocking(uart_repl->uart_channel);
  289. uart_driver_delete(uart_repl->uart_channel);
  290. free(uart_repl);
  291. _exit:
  292. return ret;
  293. }
  294. static esp_err_t esp_console_repl_usb_cdc_delete(esp_console_repl_t *repl)
  295. {
  296. esp_err_t ret = ESP_OK;
  297. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  298. esp_console_repl_usb_cdc_t *cdc_repl = __containerof(repl_com, esp_console_repl_usb_cdc_t, repl_com);
  299. // check if already de-initialized
  300. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  301. ESP_LOGE(TAG, "already de-initialized");
  302. ret = ESP_ERR_INVALID_STATE;
  303. goto _exit;
  304. }
  305. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  306. esp_console_deinit();
  307. free(cdc_repl);
  308. _exit:
  309. return ret;
  310. }
  311. static void esp_console_repl_task(void *args)
  312. {
  313. esp_console_repl_com_t *repl_com = (esp_console_repl_com_t *)args;
  314. // waiting for task notify
  315. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  316. while (repl_com->state == CONSOLE_REPL_STATE_START) {
  317. char *line = linenoise(repl_com->prompt);
  318. if (line == NULL) {
  319. ESP_LOGD(TAG, "empty line");
  320. /* Ignore empty lines */
  321. continue;
  322. }
  323. /* Add the command to the history */
  324. linenoiseHistoryAdd(line);
  325. /* Save command history to filesystem */
  326. if (repl_com->history_save_path) {
  327. linenoiseHistorySave(repl_com->history_save_path);
  328. }
  329. /* Try to run the command */
  330. int ret;
  331. esp_err_t err = esp_console_run(line, &ret);
  332. if (err == ESP_ERR_NOT_FOUND) {
  333. printf("Unrecognized command\n");
  334. } else if (err == ESP_ERR_INVALID_ARG) {
  335. // command was empty
  336. } else if (err == ESP_OK && ret != ESP_OK) {
  337. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
  338. } else if (err != ESP_OK) {
  339. printf("Internal error: %s\n", esp_err_to_name(err));
  340. }
  341. /* linenoise allocates line buffer on the heap, so need to free it */
  342. linenoiseFree(line);
  343. }
  344. ESP_LOGD(TAG, "The End");
  345. vTaskDelete(NULL);
  346. }