1 /* Console example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include "esp_system.h"
13 #include "esp_log.h"
14 #include "esp_console.h"
15 #include "esp_vfs_dev.h"
16 #include "driver/uart.h"
17 #include "linenoise/linenoise.h"
18 #include "argtable3/argtable3.h"
19 #include "cmd_decl.h"
20 #include "esp_vfs_fat.h"
21 #include "nvs.h"
22 #include "nvs_flash.h"
23
24 #ifdef CONFIG_ESP_CONSOLE_USB_CDC
25 #error This example is incompatible with USB CDC console. Please try "console_usb" example instead.
26 #endif // CONFIG_ESP_CONSOLE_USB_CDC
27
28 static const char* TAG = "example";
29 #define PROMPT_STR CONFIG_IDF_TARGET
30
31 /* Console command history can be stored to and loaded from a file.
32 * The easiest way to do this is to use FATFS filesystem on top of
33 * wear_levelling library.
34 */
35 #if CONFIG_STORE_HISTORY
36
37 #define MOUNT_PATH "/data"
38 #define HISTORY_PATH MOUNT_PATH "/history.txt"
39
initialize_filesystem(void)40 static void initialize_filesystem(void)
41 {
42 static wl_handle_t wl_handle;
43 const esp_vfs_fat_mount_config_t mount_config = {
44 .max_files = 4,
45 .format_if_mount_failed = true
46 };
47 esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
48 if (err != ESP_OK) {
49 ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
50 return;
51 }
52 }
53 #endif // CONFIG_STORE_HISTORY
54
initialize_nvs(void)55 static void initialize_nvs(void)
56 {
57 esp_err_t err = nvs_flash_init();
58 if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
59 ESP_ERROR_CHECK( nvs_flash_erase() );
60 err = nvs_flash_init();
61 }
62 ESP_ERROR_CHECK(err);
63 }
64
initialize_console(void)65 static void initialize_console(void)
66 {
67 /* Drain stdout before reconfiguring it */
68 fflush(stdout);
69 fsync(fileno(stdout));
70
71 /* Disable buffering on stdin */
72 setvbuf(stdin, NULL, _IONBF, 0);
73
74 /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
75 esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
76 /* Move the caret to the beginning of the next line on '\n' */
77 esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
78
79 /* Configure UART. Note that REF_TICK is used so that the baud rate remains
80 * correct while APB frequency is changing in light sleep mode.
81 */
82 const uart_config_t uart_config = {
83 .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
84 .data_bits = UART_DATA_8_BITS,
85 .parity = UART_PARITY_DISABLE,
86 .stop_bits = UART_STOP_BITS_1,
87 #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
88 .source_clk = UART_SCLK_REF_TICK,
89 #else
90 .source_clk = UART_SCLK_XTAL,
91 #endif
92 };
93 /* Install UART driver for interrupt-driven reads and writes */
94 ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
95 256, 0, 0, NULL, 0) );
96 ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );
97
98 /* Tell VFS to use UART driver */
99 esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
100
101 /* Initialize the console */
102 esp_console_config_t console_config = {
103 .max_cmdline_args = 8,
104 .max_cmdline_length = 256,
105 #if CONFIG_LOG_COLORS
106 .hint_color = atoi(LOG_COLOR_CYAN)
107 #endif
108 };
109 ESP_ERROR_CHECK( esp_console_init(&console_config) );
110
111 /* Configure linenoise line completion library */
112 /* Enable multiline editing. If not set, long commands will scroll within
113 * single line.
114 */
115 linenoiseSetMultiLine(1);
116
117 /* Tell linenoise where to get command completions and hints */
118 linenoiseSetCompletionCallback(&esp_console_get_completion);
119 linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
120
121 /* Set command history size */
122 linenoiseHistorySetMaxLen(100);
123
124 /* Set command maximum length */
125 linenoiseSetMaxLineLen(console_config.max_cmdline_length);
126
127 /* Don't return empty lines */
128 linenoiseAllowEmpty(false);
129
130 #if CONFIG_STORE_HISTORY
131 /* Load command history from filesystem */
132 linenoiseHistoryLoad(HISTORY_PATH);
133 #endif
134 }
135
app_main(void)136 void app_main(void)
137 {
138 initialize_nvs();
139
140 #if CONFIG_STORE_HISTORY
141 initialize_filesystem();
142 ESP_LOGI(TAG, "Command history enabled");
143 #else
144 ESP_LOGI(TAG, "Command history disabled");
145 #endif
146
147 initialize_console();
148
149 /* Register commands */
150 esp_console_register_help_command();
151 register_system();
152 register_wifi();
153 register_nvs();
154
155 /* Prompt to be printed before each line.
156 * This can be customized, made dynamic, etc.
157 */
158 const char* prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR;
159
160 printf("\n"
161 "This is an example of ESP-IDF console component.\n"
162 "Type 'help' to get the list of commands.\n"
163 "Use UP/DOWN arrows to navigate through command history.\n"
164 "Press TAB when typing command name to auto-complete.\n"
165 "Press Enter or Ctrl+C will terminate the console environment.\n");
166
167 /* Figure out if the terminal supports escape sequences */
168 int probe_status = linenoiseProbe();
169 if (probe_status) { /* zero indicates success */
170 printf("\n"
171 "Your terminal application does not support escape sequences.\n"
172 "Line editing and history features are disabled.\n"
173 "On Windows, try using Putty instead.\n");
174 linenoiseSetDumbMode(1);
175 #if CONFIG_LOG_COLORS
176 /* Since the terminal doesn't support escape sequences,
177 * don't use color codes in the prompt.
178 */
179 prompt = PROMPT_STR "> ";
180 #endif //CONFIG_LOG_COLORS
181 }
182
183 /* Main loop */
184 while(true) {
185 /* Get a line using linenoise.
186 * The line is returned when ENTER is pressed.
187 */
188 char* line = linenoise(prompt);
189 if (line == NULL) { /* Break on EOF or error */
190 break;
191 }
192 /* Add the command to the history if not empty*/
193 if (strlen(line) > 0) {
194 linenoiseHistoryAdd(line);
195 #if CONFIG_STORE_HISTORY
196 /* Save command history to filesystem */
197 linenoiseHistorySave(HISTORY_PATH);
198 #endif
199 }
200
201 /* Try to run the command */
202 int ret;
203 esp_err_t err = esp_console_run(line, &ret);
204 if (err == ESP_ERR_NOT_FOUND) {
205 printf("Unrecognized command\n");
206 } else if (err == ESP_ERR_INVALID_ARG) {
207 // command was empty
208 } else if (err == ESP_OK && ret != ESP_OK) {
209 printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
210 } else if (err != ESP_OK) {
211 printf("Internal error: %s\n", esp_err_to_name(err));
212 }
213 /* linenoise allocates line buffer on the heap, so need to free it */
214 linenoiseFree(line);
215 }
216
217 ESP_LOGE(TAG, "Error or end-of-input, terminating console");
218 esp_console_deinit();
219 }
220