2023-11-22 11:48:03 +00:00
|
|
|
#include "console_task.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "esp_system.h"
|
2023-11-22 11:58:43 +00:00
|
|
|
#include "linenoise-async.h"
|
2023-11-22 11:48:03 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_console.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
|
|
|
|
#define PROMPT_STR "[root@can_wiz ~]$ "
|
|
|
|
|
|
|
|
static const char* TAG = "console task";
|
|
|
|
|
|
|
|
|
|
|
|
void console_task(void* arg) {
|
|
|
|
|
|
|
|
/* Prompt to be printed before each line.
|
|
|
|
* This can be customized, made dynamic, etc.
|
|
|
|
*/
|
|
|
|
const char* prompt = LOG_COLOR_E PROMPT_STR LOG_RESET_COLOR;
|
|
|
|
|
|
|
|
printf("\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");
|
|
|
|
|
|
|
|
/* Figure out if the terminal supports escape sequences */
|
|
|
|
int probe_status = linenoiseProbe();
|
|
|
|
if (probe_status) { /* zero indicates success */
|
|
|
|
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");
|
|
|
|
linenoiseSetDumbMode(1);
|
|
|
|
#if CONFIG_LOG_COLORS
|
|
|
|
/* Since the terminal doesn't support escape sequences,
|
|
|
|
* don't use color codes in the prompt.
|
|
|
|
*/
|
|
|
|
prompt = PROMPT_STR;
|
|
|
|
#endif // CONFIG_LOG_COLORS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
while (true) {
|
|
|
|
/* Get a line using linenoise.
|
|
|
|
* The line is returned when ENTER is pressed.
|
|
|
|
*/
|
|
|
|
char* line = linenoise(prompt);
|
|
|
|
if (line == NULL) { /* Break on EOF or error */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Add the command to the history if not empty*/
|
|
|
|
if (strlen(line) > 0) {
|
|
|
|
linenoiseHistoryAdd(line);
|
|
|
|
/* Save command history to filesystem */
|
|
|
|
linenoiseHistorySave(HISTORY_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to run the command */
|
|
|
|
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) {
|
|
|
|
// command was empty
|
|
|
|
} 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));
|
|
|
|
}
|
|
|
|
/* linenoise allocates line buffer on the heap, so need to free it */
|
|
|
|
linenoiseFree(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGE(TAG, "Error or end-of-input, terminating console");
|
|
|
|
esp_console_deinit();
|
|
|
|
}
|