esp_console_repl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <sys/cdefs.h>
  17. #include "sdkconfig.h"
  18. #include "esp_err.h"
  19. #include "esp_log.h"
  20. #include "esp_console.h"
  21. #include "esp_vfs_dev.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "driver/uart.h"
  25. #include "linenoise/linenoise.h"
  26. static const char *TAG = "console.repl.uart";
  27. #define CONSOLE_PROMPT_MAX_LEN (32)
  28. typedef enum {
  29. CONSOLE_REPL_STATE_DEINIT,
  30. CONSOLE_REPL_STATE_INIT,
  31. CONSOLE_REPL_STATE_START,
  32. } repl_state_t;
  33. typedef struct {
  34. esp_console_repl_t repl_core; // base class
  35. char prompt[CONSOLE_PROMPT_MAX_LEN]; // Prompt to be printed before each line
  36. repl_state_t state;
  37. const char *history_save_path;
  38. TaskHandle_t task_hdl; // REPL task handle
  39. } esp_console_repl_com_t;
  40. typedef struct {
  41. esp_console_repl_com_t repl_com; // base class
  42. int uart_channel; // uart channel number
  43. } esp_console_repl_uart_t;
  44. static void esp_console_repl_task(void *args);
  45. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl);
  46. static esp_err_t esp_console_common_init(esp_console_repl_com_t *repl_com);
  47. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com);
  48. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com);
  49. 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)
  50. {
  51. esp_err_t ret = ESP_OK;
  52. esp_console_repl_uart_t *uart_repl = NULL;
  53. if (!repl_config | !dev_config | !ret_repl) {
  54. ret = ESP_ERR_INVALID_ARG;
  55. goto _exit;
  56. }
  57. // allocate memory for console REPL context
  58. uart_repl = calloc(1, sizeof(esp_console_repl_uart_t));
  59. if (!uart_repl) {
  60. ret = ESP_ERR_NO_MEM;
  61. goto _exit;
  62. }
  63. /* Drain stdout before reconfiguring it */
  64. fflush(stdout);
  65. fsync(fileno(stdout));
  66. /* Disable buffering on stdin */
  67. setvbuf(stdin, NULL, _IONBF, 0);
  68. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  69. esp_vfs_dev_uart_port_set_rx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CR);
  70. /* Move the caret to the beginning of the next line on '\n' */
  71. esp_vfs_dev_uart_port_set_tx_line_endings(dev_config->channel, ESP_LINE_ENDINGS_CRLF);
  72. /* Configure UART. Note that REF_TICK is used so that the baud rate remains
  73. * correct while APB frequency is changing in light sleep mode.
  74. */
  75. const uart_config_t uart_config = {
  76. .baud_rate = dev_config->baud_rate,
  77. .data_bits = UART_DATA_8_BITS,
  78. .parity = UART_PARITY_DISABLE,
  79. .stop_bits = UART_STOP_BITS_1,
  80. .source_clk = UART_SCLK_REF_TICK,
  81. };
  82. uart_param_config(dev_config->channel, &uart_config);
  83. uart_set_pin(dev_config->channel, dev_config->tx_gpio_num, dev_config->rx_gpio_num, -1, -1);
  84. /* Install UART driver for interrupt-driven reads and writes */
  85. ret = uart_driver_install(dev_config->channel, 256, 0, 0, NULL, 0);
  86. if (ret != ESP_OK) {
  87. goto _exit;
  88. }
  89. /* Tell VFS to use UART driver */
  90. esp_vfs_dev_uart_use_driver(dev_config->channel);
  91. // initialize console, common part
  92. ret = esp_console_common_init(&uart_repl->repl_com);
  93. if (ret != ESP_OK) {
  94. goto _exit;
  95. }
  96. // setup history
  97. ret = esp_console_setup_history(repl_config->history_save_path, repl_config->max_history_len, &uart_repl->repl_com);
  98. if (ret != ESP_OK) {
  99. goto _exit;
  100. }
  101. // setup prompt
  102. esp_console_setup_prompt(repl_config->prompt, &uart_repl->repl_com);
  103. /* spawn a single thread to run REPL */
  104. if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
  105. &uart_repl->repl_com, repl_config->task_priority, &uart_repl->repl_com.task_hdl) != pdTRUE) {
  106. ret = ESP_FAIL;
  107. goto _exit;
  108. }
  109. uart_repl->uart_channel = dev_config->channel;
  110. uart_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
  111. uart_repl->repl_com.repl_core.del = esp_console_repl_uart_delete;
  112. *ret_repl = &uart_repl->repl_com.repl_core;
  113. return ESP_OK;
  114. _exit:
  115. if (uart_repl) {
  116. esp_console_deinit();
  117. uart_driver_delete(dev_config->channel);
  118. free(uart_repl);
  119. }
  120. if (ret_repl) {
  121. *ret_repl = NULL;
  122. }
  123. return ret;
  124. }
  125. esp_err_t esp_console_start_repl(esp_console_repl_t *repl)
  126. {
  127. esp_err_t ret = ESP_OK;
  128. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  129. // check if already initialized
  130. if (repl_com->state != CONSOLE_REPL_STATE_INIT) {
  131. ret = ESP_ERR_INVALID_STATE;
  132. goto _exit;
  133. }
  134. repl_com->state = CONSOLE_REPL_STATE_START;
  135. xTaskNotifyGive(repl_com->task_hdl);
  136. return ESP_OK;
  137. _exit:
  138. return ret;
  139. }
  140. static esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com)
  141. {
  142. /* set command line prompt */
  143. const char *prompt_temp = "esp>";
  144. if (prompt) {
  145. prompt_temp = prompt;
  146. }
  147. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, LOG_COLOR_I "%s " LOG_RESET_COLOR, prompt_temp);
  148. printf("\r\n"
  149. "Type 'help' to get the list of commands.\r\n"
  150. "Use UP/DOWN arrows to navigate through command history.\r\n"
  151. "Press TAB when typing command name to auto-complete.\r\n");
  152. /* Figure out if the terminal supports escape sequences */
  153. int probe_status = linenoiseProbe();
  154. if (probe_status) {
  155. /* zero indicates success */
  156. printf("\r\n"
  157. "Your terminal application does not support escape sequences.\n\n"
  158. "Line editing and history features are disabled.\n\n"
  159. "On Windows, try using Putty instead.\r\n");
  160. linenoiseSetDumbMode(1);
  161. #if CONFIG_LOG_COLORS
  162. /* Since the terminal doesn't support escape sequences,
  163. * don't use color codes in the s_prompt.
  164. */
  165. snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, "%s ", prompt_temp);
  166. #endif //CONFIG_LOG_COLORS
  167. }
  168. return ESP_OK;
  169. }
  170. static esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com)
  171. {
  172. esp_err_t ret = ESP_OK;
  173. repl_com->history_save_path = history_path;
  174. if (history_path) {
  175. /* Load command history from filesystem */
  176. linenoiseHistoryLoad(history_path);
  177. }
  178. /* Set command history size */
  179. if (linenoiseHistorySetMaxLen(max_history_len) != 1) {
  180. ESP_LOGE(TAG, "set max history length to %d failed", max_history_len);
  181. ret = ESP_FAIL;
  182. goto _exit;
  183. }
  184. return ESP_OK;
  185. _exit:
  186. return ret;
  187. }
  188. static esp_err_t esp_console_common_init(esp_console_repl_com_t *repl_com)
  189. {
  190. esp_err_t ret = ESP_OK;
  191. /* Initialize the console */
  192. esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
  193. #if CONFIG_LOG_COLORS
  194. console_config.hint_color = atoi(LOG_COLOR_CYAN);
  195. #endif
  196. ret = esp_console_init(&console_config);
  197. if (ret != ESP_OK) {
  198. goto _exit;
  199. }
  200. ret = esp_console_register_help_command();
  201. if (ret != ESP_OK) {
  202. goto _exit;
  203. }
  204. /* Configure linenoise line completion library */
  205. /* Enable multiline editing. If not set, long commands will scroll within single line */
  206. linenoiseSetMultiLine(1);
  207. /* Tell linenoise where to get command completions and hints */
  208. linenoiseSetCompletionCallback(&esp_console_get_completion);
  209. linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
  210. return ESP_OK;
  211. _exit:
  212. return ret;
  213. }
  214. static esp_err_t esp_console_repl_uart_delete(esp_console_repl_t *repl)
  215. {
  216. esp_err_t ret = ESP_OK;
  217. esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
  218. esp_console_repl_uart_t *uart_repl = __containerof(repl_com, esp_console_repl_uart_t, repl_com);
  219. // check if already de-initialized
  220. if (repl_com->state == CONSOLE_REPL_STATE_DEINIT) {
  221. ESP_LOGE(TAG, "already de-initialized");
  222. ret = ESP_ERR_INVALID_STATE;
  223. goto _exit;
  224. }
  225. repl_com->state = CONSOLE_REPL_STATE_DEINIT;
  226. esp_console_deinit();
  227. esp_vfs_dev_uart_use_nonblocking(uart_repl->uart_channel);
  228. uart_driver_delete(uart_repl->uart_channel);
  229. free(uart_repl);
  230. _exit:
  231. return ret;
  232. }
  233. static void esp_console_repl_task(void *args)
  234. {
  235. esp_console_repl_com_t *repl_com = (esp_console_repl_com_t *)args;
  236. // waiting for task notify
  237. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  238. while (repl_com->state == CONSOLE_REPL_STATE_START) {
  239. char *line = linenoise(repl_com->prompt);
  240. if (line == NULL) {
  241. ESP_LOGD(TAG, "empty line");
  242. /* Ignore empty lines */
  243. continue;
  244. }
  245. /* Add the command to the history */
  246. linenoiseHistoryAdd(line);
  247. /* Save command history to filesystem */
  248. if (repl_com->history_save_path) {
  249. linenoiseHistorySave(repl_com->history_save_path);
  250. }
  251. /* Try to run the command */
  252. int ret;
  253. esp_err_t err = esp_console_run(line, &ret);
  254. if (err == ESP_ERR_NOT_FOUND) {
  255. printf("Unrecognized command\n");
  256. } else if (err == ESP_ERR_INVALID_ARG) {
  257. // command was empty
  258. } else if (err == ESP_OK && ret != ESP_OK) {
  259. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
  260. } else if (err != ESP_OK) {
  261. printf("Internal error: %s\n", esp_err_to_name(err));
  262. }
  263. /* linenoise allocates line buffer on the heap, so need to free it */
  264. linenoiseFree(line);
  265. }
  266. ESP_LOGD(TAG, "The End");
  267. vTaskDelete(NULL);
  268. }