1
6
7
13
14
15
16
17
18
19
20
23
24
25
26
27
37
38
39
40
41
42
43
44
45
46
50
51
52
57
58
59
60
61
62
63
64
65
66
67
68
69
73
74
75
76
77
78
79
80
83
84
85
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
127
128
129
132
133
134
135
136
137
138
139
140
141
144
145
146
148
149
150
151
152
153
159
160
161
162
163
168
169
170
171
174
175
176
177
178
187
188
189
190
191
192
/* ... */
#include "sdkconfig.h"
#include <sys/cdefs.h>
#include "esp_console.h"
#include "console_private.h"
#include "esp_log.h"
#include "linenoise/linenoise.h"6 includes
static const char *TAG = "console.common";
esp_err_t esp_console_setup_prompt(const char *prompt, esp_console_repl_com_t *repl_com)
{
const char *prompt_temp = "esp>";
if (prompt) {
prompt_temp = prompt;
}{...}
snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, LOG_COLOR_I "%s " LOG_RESET_COLOR, prompt_temp);
int probe_status = linenoiseProbe();
if (probe_status) {
linenoiseSetDumbMode(1);
#if CONFIG_LOG_COLORS
/* ... */
snprintf(repl_com->prompt, CONSOLE_PROMPT_MAX_LEN - 1, "%s ", prompt_temp);/* ... */
#endif
}{...}
return ESP_OK;
}{ ... }
esp_err_t esp_console_setup_history(const char *history_path, uint32_t max_history_len, esp_console_repl_com_t *repl_com)
{
esp_err_t ret = ESP_OK;
repl_com->history_save_path = history_path;
if (history_path) {
linenoiseHistoryLoad(history_path);
}{...}
if (linenoiseHistorySetMaxLen(max_history_len) != 1) {
ESP_LOGE(TAG, "set max history length to %"PRIu32" failed", max_history_len);
ret = ESP_FAIL;
goto _exit;
}{...}
return ESP_OK;
_exit:
return ret;
}{ ... }
esp_err_t esp_console_common_init(size_t max_cmdline_length, esp_console_repl_com_t *repl_com)
{
esp_err_t ret = ESP_OK;
esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT();
repl_com->max_cmdline_length = console_config.max_cmdline_length;
if (max_cmdline_length != 0) {
console_config.max_cmdline_length = max_cmdline_length;
repl_com->max_cmdline_length = max_cmdline_length;
}{...}
#if CONFIG_LOG_COLORS
console_config.hint_color = atoi(LOG_COLOR_CYAN);
#else
console_config.hint_color = -1;
#endif
ret = esp_console_init(&console_config);
if (ret != ESP_OK) {
goto _exit;
}{...}
ret = esp_console_register_help_command();
if (ret != ESP_OK) {
goto _exit;
}{...}
linenoiseSetMultiLine(1);
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
return ESP_OK;
_exit:
return ret;
}{ ... }
esp_err_t esp_console_start_repl(esp_console_repl_t *repl)
{
esp_err_t ret = ESP_OK;
esp_console_repl_com_t *repl_com = __containerof(repl, esp_console_repl_com_t, repl_core);
if (repl_com->state != CONSOLE_REPL_STATE_INIT) {
ret = ESP_ERR_INVALID_STATE;
goto _exit;
}{...}
repl_com->state = CONSOLE_REPL_STATE_START;
xTaskNotifyGive(repl_com->task_hdl);
return ESP_OK;
_exit:
return ret;
}{ ... }
void esp_console_repl_task(void *args)
{
esp_console_repl_universal_t *repl_conf = (esp_console_repl_universal_t *) args;
esp_console_repl_com_t *repl_com = &repl_conf->repl_com;
const int uart_channel = repl_conf->uart_channel;
/* ... */
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
/* ... */
if (uart_channel != CONFIG_ESP_CONSOLE_UART_NUM) {
char path[CONSOLE_PATH_MAX_LEN] = { 0 };
snprintf(path, CONSOLE_PATH_MAX_LEN, "/dev/uart/%d", uart_channel);
stdin = fopen(path, "r");
stdout = fopen(path, "w");
stderr = stdout;
}{...}
/* ... */
setvbuf(stdin, NULL, _IONBF, 0);
/* ... */
printf("\r\n"
"Type 'help' to get the list of commands.\r\n"
"Use UP/DOWN arrows to navigate through command history.\r\n"
"Press TAB when typing command name to auto-complete.\r\n");
if (linenoiseIsDumbMode()) {
printf("\r\n"
"Your terminal application does not support escape sequences.\n\n"
"Line editing and history features are disabled.\n\n"
"On Windows, try using Putty instead.\r\n");
}{...}
linenoiseSetMaxLineLen(repl_com->max_cmdline_length);
while (repl_com->state == CONSOLE_REPL_STATE_START) {
char *line = linenoise(repl_com->prompt);
if (line == NULL) {
ESP_LOGD(TAG, "empty line");
continue;
}{...}
linenoiseHistoryAdd(line);
if (repl_com->history_save_path) {
linenoiseHistorySave(repl_com->history_save_path);
}{...}
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_LOGD(TAG, "The End");
vTaskDelete(NULL);
}{ ... }