1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
29
30
33
34
35
36
37
38
42
43
61
62
63
64
73
74
75
76
77
78
79
81
82
83
84
85
86
87
88
89
90
91
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
123
124
125
126
129
130
131
132
136
140
141
142
149
150
151
152
153
162
163
164
165
166
167
168
/* ... */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "esp_vfs_fat.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "soc/soc_caps.h"
#include "cmd_system.h"
#include "cmd_wifi.h"
#include "cmd_nvs.h"
#include "console_settings.h"16 includes
/* ... */
#if SOC_USB_SERIAL_JTAG_SUPPORTED
#if !CONFIG_ESP_CONSOLE_SECONDARY_NONE
#warning "A secondary serial console is not useful when using the console component. Please disable it in menuconfig."
#endif/* ... */
#endif
static const char* TAG = "example";
#define PROMPT_STR CONFIG_IDF_TARGET
/* ... */
#if CONFIG_CONSOLE_STORE_HISTORY
#define MOUNT_PATH "/data"
#define HISTORY_PATH MOUNT_PATH "/history.txt"
static void initialize_filesystem(void)
{
static wl_handle_t wl_handle;
const esp_vfs_fat_mount_config_t mount_config = {
.max_files = 4,
.format_if_mount_failed = true
}{...};
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}{...}
}{ ... }
#else/* ... */
#define HISTORY_PATH NULL
#endif
static void initialize_nvs(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
err = nvs_flash_init();
}{...}
ESP_ERROR_CHECK(err);
}{ ... }
void app_main(void)
{
initialize_nvs();
#if CONFIG_CONSOLE_STORE_HISTORY
initialize_filesystem();
ESP_LOGI(TAG, "Command history enabled");/* ... */
#else
ESP_LOGI(TAG, "Command history disabled");
#endif
initialize_console_peripheral();
initialize_console_library(HISTORY_PATH);
/* ... */
const char *prompt = setup_prompt(PROMPT_STR ">");
esp_console_register_help_command();
register_system_common();
#if SOC_LIGHT_SLEEP_SUPPORTED
register_system_light_sleep();
#endif
#if SOC_DEEP_SLEEP_SUPPORTED
register_system_deep_sleep();
#endif
#if (CONFIG_ESP_WIFI_ENABLED || CONFIG_ESP_HOST_WIFI_ENABLED)
register_wifi();
#endif
register_nvs();
printf("\n"
"This is an example of ESP-IDF console component.\n"
"Type 'help' to get the list of commands.\n"
"Use UP/DOWN arrows to navigate through command history.\n"
"Press TAB when typing command name to auto-complete.\n"
"Ctrl+C will terminate the console environment.\n");
if (linenoiseIsDumbMode()) {
printf("\n"
"Your terminal application does not support escape sequences.\n"
"Line editing and history features are disabled.\n"
"On Windows, try using Putty instead.\n");
}{...}
while(true) {
/* ... */
char* line = linenoise(prompt);
#if CONFIG_CONSOLE_IGNORE_EMPTY_LINES
if (line == NULL) {
continue;;
}{...}
#else/* ... */
if (line == NULL) {
break;
}{...}
#endif/* ... */
if (strlen(line) > 0) {
linenoiseHistoryAdd(line);
#if CONFIG_CONSOLE_STORE_HISTORY
linenoiseHistorySave(HISTORY_PATH);/* ... */
#endif
}{...}
int ret;
esp_err_t err = esp_console_run(line, &ret);
if (err == ESP_ERR_NOT_FOUND) {
printf("Unrecognized command\n");
}{...} else if (err == ESP_ERR_INVALID_ARG) {
}{...} else if (err == ESP_OK && ret != ESP_OK) {
printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
}{...} else if (err != ESP_OK) {
printf("Internal error: %s\n", esp_err_to_name(err));
}{...}
linenoiseFree(line);
}{...}
ESP_LOGE(TAG, "Error or end-of-input, terminating console");
esp_console_deinit();
}{ ... }