Start of 5.X work

This commit is contained in:
Sebastien L
2025-03-18 17:38:34 -04:00
parent c0ddf0a997
commit 73bd096f37
442 changed files with 227862 additions and 21075 deletions

View File

@@ -7,7 +7,7 @@ idf_component_register( SRCS
cmd_config.c
INCLUDE_DIRS .
REQUIRES nvs_flash
PRIV_REQUIRES console app_update tools services spi_flash platform_config vfs pthread wifi-manager platform_config newlib telnet display squeezelite tools metrics)
PRIV_REQUIRES console app_update tools services spi_flash tools platform_config vfs pthread wifi-manager newlib telnet display squeezelite metrics)
set_source_files_properties(cmd_config.c
PROPERTIES COMPILE_FLAGS

View File

@@ -1,7 +1,7 @@
idf_build_get_property(idf_path IDF_PATH)
idf_component_register( SRCS cmd_squeezelite.c
INCLUDE_DIRS .
PRIV_REQUIRES spi_flash bootloader_support partition_table bootloader_support console codecs squeezelite newlib pthread tools platform_config display tools services)
PRIV_REQUIRES spi_flash bootloader_support partition_table bootloader_support console codecs squeezelite newlib pthread tools platform_config display services)
target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--undefined=feof")

View File

@@ -1,23 +1,27 @@
#include <stdio.h>
#include <string.h>
#include "application_name.h"
#include "esp_log.h"
#include "esp_console.h"
#include "../cmd_system.h"
#include "Config.h"
#include "application_name.h"
#include "argtable3/argtable3.h"
#include "esp_app_format.h"
#include "esp_console.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "pthread.h"
#include "platform_esp32.h"
#include "Configurator.h"
#include "esp_app_format.h"
#include "tools.h"
#include "messaging.h"
extern esp_err_t process_recovery_ota(const char * bin_url, char * bin_buffer, uint32_t length);
extern int squeezelite_main_start();
#include "platform_esp32.h"
#include "pthread.h"
#include "tools.h"
#include <stdio.h>
#include <string.h>
#include "bootstate.h"
static const char * TAG = "squeezelite_cmd";
#define SQUEEZELITE_THREAD_STACK_SIZE (8*1024)
extern esp_err_t process_recovery_ota(const char* bin_url, char* bin_buffer, uint32_t length);
extern int squeezelite_main_start();
extern sys_dac_default_sets* default_dac_sets;
static const char* TAG = "squeezelite_cmd";
#define SQUEEZELITE_THREAD_STACK_SIZE (8 * 1024)
#define ADDITIONAL_SQUEEZELITE_ARGS 5
const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
@@ -45,112 +49,91 @@ const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
extern void register_audio_config(void);
// extern void register_rotary_config(void);
extern void register_nvs();
extern cJSON * get_gpio_list_handler(bool refresh);
cJSON * get_gpio_list(bool refresh){
#if CONFIG_WITH_CONFIG_UI
return get_gpio_list_handler(refresh);
extern cJSON* get_gpio_list_handler(bool refresh);
cJSON* get_gpio_list(bool refresh) {
#if CONFIG_WITH_CONFIG_UI
return get_gpio_list_handler(refresh);
#else
return cJSON_CreateArray();
return cJSON_CreateArray();
#endif
}
void register_optional_cmd(void) {
// #if CONFIG_WITH_CONFIG_UI
// register_rotary_config();
// #endif
// #if CONFIG_WITH_CONFIG_UI
// register_rotary_config();
// #endif
register_audio_config();
// register_ledvu_config();
// register_ledvu_config();
}
/** Arguments used by 'squeezelite' function */
static struct {
struct arg_str *parameters;
struct arg_end *end;
} squeezelite_args;
static struct {
int argc;
char ** argv;
} thread_parms ;
int argc;
char** argv;
} thread_parms;
static void squeezelite_thread(void *arg){
ESP_LOGV(TAG ,"Number of args received: %u",thread_parms.argc );
ESP_LOGV(TAG ,"Values:");
for(int i = 0;i<thread_parms.argc; i++){
ESP_LOGV(TAG ," %s",thread_parms.argv[i]);
}
ESP_LOGI(TAG ,"Calling squeezelite");
int ret = squeezelite_main_start(thread_parms.argc, thread_parms.argv);
cmd_send_messaging("cfg-audio-tmpl",ret > 1 ? MESSAGING_ERROR : MESSAGING_WARNING,"squeezelite exited with error code %d\n", ret);
static void squeezelite_thread(void* arg) {
ESP_LOGI(TAG, "Calling squeezelite");
int wait = 60;
int ret = squeezelite_main_start();
if (ret <= 1) {
int wait = 60;
// wait_for_commit();
// TODO: Add support for the commented code
cmd_send_messaging("cfg-audio-tmpl",MESSAGING_ERROR,"Rebooting in %d sec\n", wait);
vTaskDelay( pdMS_TO_TICKS(wait * 1000));
esp_restart();
if (ret == -2) {
cmd_send_messaging("cfg-audio-tmpl", MESSAGING_ERROR,
"Squeezelite not configured. Rebooting to factory in %d sec\n", wait);
vTaskDelay(pdMS_TO_TICKS(wait * 1000));
guided_factory();
} else {
cmd_send_messaging("cfg-audio-tmpl",MESSAGING_ERROR,"Correct command line and reboot\n");
vTaskSuspend(NULL);
cmd_send_messaging("cfg-audio-tmpl", ret > 1 ? MESSAGING_ERROR : MESSAGING_WARNING,
"squeezelite exited with error code %d\n", ret);
if (ret <= 1) {
cmd_send_messaging(
"cfg-audio-tmpl", MESSAGING_ERROR, "Rebooting to factory in %d sec\n", wait);
vTaskDelay(pdMS_TO_TICKS(wait * 1000));
guided_factory();
} else {
cmd_send_messaging(
"cfg-audio-tmpl", MESSAGING_ERROR, "Correct command line and reboot\n");
vTaskSuspend(NULL);
}
}
ESP_LOGV(TAG, "Exited from squeezelite's main(). Freeing argv structure.");
ESP_LOGV(TAG, "Exited from squeezelite's main(). Freeing argv structure.");
for(int i=0;i<thread_parms.argc;i++) free(thread_parms.argv[i]);
free(thread_parms.argv);
for (int i = 0; i < thread_parms.argc; i++)
free(thread_parms.argv[i]);
free(thread_parms.argv);
}
static int launchsqueezelite(int argc, char **argv) {
static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__ ((aligned (4)));
static EXT_RAM_ATTR StackType_t xStack[SQUEEZELITE_THREAD_STACK_SIZE] __attribute__ ((aligned (4)));
static bool isRunning = false;
static int launchsqueezelite(int argc, char** argv) {
static DRAM_ATTR StaticTask_t xTaskBuffer __attribute__((aligned(4)));
static EXT_RAM_ATTR StackType_t xStack[SQUEEZELITE_THREAD_STACK_SIZE]
__attribute__((aligned(4)));
static bool isRunning = false;
if (isRunning) {
ESP_LOGE(TAG,"Squeezelite already running. Exiting!");
return -1;
}
ESP_LOGV(TAG ,"Begin");
isRunning = true;
if (isRunning) {
ESP_LOGE(TAG, "Squeezelite already running. Exiting!");
return -1;
}
// ESP_LOGV(TAG, "Parameters:");
// for(int i = 0;i<argc; i++){
// ESP_LOGV(TAG, " %s",argv[i]);
// }
// ESP_LOGV(TAG,"Saving args in thread structure");
ESP_LOGV(TAG, "Begin");
isRunning = true;
// thread_parms.argc=0;
// thread_parms.argv = malloc_init_external(sizeof(char**)*(argc+ADDITIONAL_SQUEEZELITE_ARGS));
// load the presets here, as the squeezelite cannot access the
// spiffs storage
// proto_load_file("/spiffs/presets/default_sets.bin", &sys_dac_default_sets_msg, default_dac_sets, false);
// for(int i=0;i<argc;i++){
// ESP_LOGD(TAG ,"assigning parm %u : %s",i,argv[i]);
// thread_parms.argv[thread_parms.argc++]=strdup(argv[i]);
// }
// if(argc==1){
// // There isn't a default configuration that would actually work
// // if no parameter is passed.
// ESP_LOGV(TAG ,"Adding argv value at %u. Prev value: %s",thread_parms.argc,thread_parms.argv[thread_parms.argc-1]);
// thread_parms.argv[thread_parms.argc++]=strdup("-?");
// }
ESP_LOGD(TAG,"Starting Squeezelite Thread");
xTaskCreateStaticPinnedToCore(squeezelite_thread, "squeezelite", SQUEEZELITE_THREAD_STACK_SIZE,
NULL, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT, xStack, &xTaskBuffer, CONFIG_PTHREAD_TASK_CORE_DEFAULT);
ESP_LOGD(TAG ,"Back to console thread!");
ESP_LOGD(TAG, "Starting Squeezelite Thread");
xTaskCreateStaticPinnedToCore(squeezelite_thread, "squeezelite", SQUEEZELITE_THREAD_STACK_SIZE,
NULL, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT, xStack, &xTaskBuffer,
CONFIG_PTHREAD_TASK_CORE_DEFAULT);
return 0;
}
void start_squeezelite(){
launchsqueezelite(0,NULL);
}
void start_squeezelite() { launchsqueezelite(0, NULL); }
// void register_squeezelite() {
// squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h for help, --defaults to launch with default values.");
// squeezelite_args.end = arg_end(1);
// const esp_console_cmd_t launch_squeezelite = {
// .command = "squeezelite",
// .help = "Starts squeezelite",
// squeezelite_args.parameters = arg_str0(NULL, NULL, "<parms>", "command line for squeezelite. -h
// for help, --defaults to launch with default values."); squeezelite_args.end = arg_end(1); const
// esp_console_cmd_t launch_squeezelite = { .command = "squeezelite", .help = "Starts squeezelite",
// .hint = NULL,
// .func = &launchsqueezelite,
// .argtable = &squeezelite_args
@@ -158,19 +141,19 @@ void start_squeezelite(){
// ESP_ERROR_CHECK( esp_console_cmd_register(&launch_squeezelite) );
// }
esp_err_t start_ota(const char * bin_url, char * bin_buffer, uint32_t length) {
if(!bin_url || strlen(bin_url)==0){
ESP_LOGE(TAG,"missing URL parameter. Unable to start OTA");
return ESP_ERR_INVALID_ARG;
}
ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
configurator_set_string(&sys_State_msg,sys_State_ota_url_tag,sys_state,bin_url);
configurator_raise_state_changed();
esp_err_t start_ota(const char* bin_url, char* bin_buffer, uint32_t length) {
if (!bin_url || strlen(bin_url) == 0) {
ESP_LOGE(TAG, "missing URL parameter. Unable to start OTA");
return ESP_ERR_INVALID_ARG;
}
ESP_LOGW(TAG, "Called to update the firmware from url: %s", bin_url);
system_set_string(&sys_state_data_msg, sys_state_data_ota_url_tag, sys_state, bin_url);
config_raise_state_changed();
if(!configurator_waitcommit()){
ESP_LOGW(TAG,"Unable to commit configuration. ");
}
ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
return guided_factory();
return ESP_OK;
if (!config_waitcommit()) {
ESP_LOGW(TAG, "Unable to commit configuration. ");
}
ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
return guided_factory();
return ESP_OK;
}

File diff suppressed because it is too large Load Diff

View File

@@ -16,7 +16,7 @@
#include "driver/i2c.h"
#include "esp_log.h"
#include "messaging.h"
// #include "Configurator.h"
// #include "Config.h"
#pragma message("fixme: look for TODO below")
#include "platform_console.h"
#include "stdio.h"
@@ -41,19 +41,18 @@ const char* desc_spiconfig = "SPI Bus Parameters";
const char* desc_i2c = "I2C Bus Parameters";
const char* desc_display = "Display";
#ifdef CONFIG_I2C_LOCKED
static i2c_port_t i2c_port = I2C_NUM_1;
#else
static i2c_port_t i2c_port = I2C_NUM_0;
#endif
static struct {
struct arg_int* port;
struct arg_int* chip_address;
struct arg_int* register_address;
struct arg_int* data_length;
struct arg_end* end;
} i2cget_args;
static struct {
struct arg_int* port;
struct arg_end* end;
} i2cdetect_args;
static struct {
struct arg_int* chip_address;
struct arg_int* port;
@@ -288,31 +287,31 @@ static esp_err_t i2c_get_port(int port, i2c_port_t* i2c_port) {
}
return ESP_OK;
}
static esp_err_t i2c_master_driver_install(const char* cmdname) {
esp_err_t err = ESP_OK;
cmd_send_messaging(cmdname, MESSAGING_INFO, "Installing i2c driver on port %u\n", i2c_port);
if ((err = i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE,
I2C_MASTER_TX_BUF_DISABLE, 0)) != ESP_OK) {
cmd_send_messaging(
cmdname, MESSAGING_ERROR, "Driver install failed! %s\n", esp_err_to_name(err));
}
return err;
}
// static esp_err_t i2c_master_driver_install(const char* cmdname) {
// esp_err_t err = ESP_OK;
// cmd_send_messaging(cmdname, MESSAGING_INFO, "Installing i2c driver on port %u\n", i2c_port);
// if ((err = i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE,
// I2C_MASTER_TX_BUF_DISABLE, 0)) != ESP_OK) {
// cmd_send_messaging(
// cmdname, MESSAGING_ERROR, "Driver install failed! %s\n", esp_err_to_name(err));
// }
// return err;
// }
static esp_err_t i2c_master_driver_initialize(const char* cmdname, i2c_config_t* conf) {
esp_err_t err = ESP_OK;
cmd_send_messaging(cmdname, MESSAGING_INFO,
"Initializing i2c driver configuration.\n mode = I2C_MODE_MASTER, \n scl_pullup_en = "
"GPIO_PULLUP_ENABLE, \n i2c port = %u, \n sda_io_num = %u, \n sda_pullup_en = "
"GPIO_PULLUP_ENABLE, \n scl_io_num = %u, \n scl_pullup_en = GPIO_PULLUP_ENABLE, \n "
"master.clk_speed = %u\n",
i2c_port, conf->sda_io_num, conf->scl_io_num, conf->master.clk_speed);
if ((err = i2c_param_config(i2c_port, conf)) != ESP_OK) {
cmd_send_messaging(
cmdname, MESSAGING_ERROR, "i2c driver config load failed. %s\n", esp_err_to_name(err));
}
return err;
}
// static esp_err_t i2c_master_driver_initialize(const char* cmdname, i2c_config_t* conf) {
// esp_err_t err = ESP_OK;
// cmd_send_messaging(cmdname, MESSAGING_INFO,
// "Initializing i2c driver configuration.\n mode = I2C_MODE_MASTER, \n scl_pullup_en = "
// "GPIO_PULLUP_ENABLE, \n i2c port = %u, \n sda_io_num = %u, \n sda_pullup_en = "
// "GPIO_PULLUP_ENABLE, \n scl_io_num = %u, \n scl_pullup_en = GPIO_PULLUP_ENABLE, \n "
// "master.clk_speed = %u\n",
// i2c_port, conf->sda_io_num, conf->scl_io_num, conf->master.clk_speed);
// if ((err = i2c_param_config(i2c_port, conf)) != ESP_OK) {
// cmd_send_messaging(
// cmdname, MESSAGING_ERROR, "i2c driver config load failed. %s\n", esp_err_to_name(err));
// }
// return err;
// }
#if CONFIG_WITH_CONFIG_UI
static int do_i2c_set_display(int argc, char** argv) {
@@ -763,12 +762,9 @@ static int do_i2cget_cmd(int argc, char** argv) {
if (i2cget_args.data_length->count) {
len = i2cget_args.data_length->ival[0];
}
i2c_port_t loc_i2c_port = i2c_port;
if (i2cset_args.port->count &&
i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) {
return 1;
}
i2c_port_t loc_i2c_port = i2cget_args.port->ival[0];
const sys_i2c_bus*i2c = get_i2c_bus(loc_i2c_port);
#pragma message("TODO: Fix this!")
char* buf = NULL;
size_t buf_size = 0;
FILE* f = open_memstream(&buf, &buf_size);
@@ -897,18 +893,21 @@ esp_err_t cmd_i2ctools_scan_bus(FILE* f, int sda, int scl) {
return 0;
}
static int do_i2cdetect_cmd(int argc, char** argv) {
#ifdef CONFIG_WITH_CONFIG_UI
uint8_t matches[128] = {};
int last_match = 0;
#endif
esp_err_t ret = ESP_OK;
i2c_port_t loc_i2c_port = i2c_port;
i2c_port_t loc_i2c_port = i2cdetect_args.port->ival[0];
// if (i2cset_args.port->count && i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) !=
// ESP_OK) { return 1;
// }
int i2c_port;
const i2c_config_t* i2c = config_i2c_get(&i2c_port);
const sys_i2c_bus*i2c = get_i2c_bus(loc_i2c_port);
uint8_t address;
char* buf = NULL;
@@ -919,8 +918,7 @@ static int do_i2cdetect_cmd(int argc, char** argv) {
return 1;
}
if (!GPIO_IS_VALID_OUTPUT_GPIO(i2c->scl_io_num) || !GPIO_IS_VALID_GPIO(i2c->scl_io_num) ||
!GPIO_IS_VALID_OUTPUT_GPIO(i2c->sda_io_num) || !GPIO_IS_VALID_GPIO(i2c->sda_io_num)) {
if (!i2c) {
fprintf(f, "I2C bus configuration invalid.\r\n");
} else {
@@ -1007,12 +1005,13 @@ static void register_i2c_set_display() {
}
#endif
static void register_i2cdectect(void) {
i2cdetect_args.port = arg_int1(NULL,NULL, "<0|1>", "Specify the i2c port number");
i2cdetect_args.end = arg_end(1);
const esp_console_cmd_t i2cdetect_cmd = {.command = "i2cdetect",
.help = "Scan I2C bus for devices",
.hint = NULL,
.func = &do_i2cdetect_cmd,
.argtable = NULL};
cmd_to_json(&i2cdetect_cmd);
.argtable = &i2cdetect_args};
ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdetect_cmd));
}
@@ -1023,6 +1022,7 @@ static void register_i2cget(void) {
"r", "register", "<register_addr>", "Specify the address on that chip to read from");
i2cget_args.data_length =
arg_int0("l", "length", "<length>", "Specify the length to read from that data address");
i2cget_args.port = arg_int1(NULL,NULL, "<0|1>", "Specify the i2c port number");
i2cget_args.end = arg_end(1);
const esp_console_cmd_t i2cget_cmd = {.command = "i2cget",
.help = "Read registers visible through the I2C bus",
@@ -1070,8 +1070,7 @@ static void register_i2cdump(void) {
cJSON* i2config_cb() {
cJSON* values = cJSON_CreateObject();
int i2c_port;
const i2c_config_t* i2c = config_i2c_get(&i2c_port);
const i2c_config_t* i2c = config_i2c_get(&platform->dev.i2c);
if (i2c->scl_io_num > 0) {
cJSON_AddNumberToObject(values, "scl", i2c->scl_io_num);
}
@@ -1081,8 +1080,8 @@ cJSON* i2config_cb() {
if (i2c->master.clk_speed > 0) {
cJSON_AddNumberToObject(values, "speed", i2c->master.clk_speed);
}
if (i2c_port >= 0) {
cJSON_AddNumberToObject(values, "port", i2c_port);
if (platform->dev.i2c.port-sys_i2c_port_PORT0 >= 0) {
cJSON_AddNumberToObject(values, "port", platform->dev.i2c.port-sys_i2c_port_PORT0);
}
return values;
}

File diff suppressed because it is too large Load Diff

View File

@@ -14,9 +14,7 @@ extern "C" {
// Register system functions
void register_system();
esp_err_t guided_factory();
esp_err_t guided_restart_ota();
void simple_restart();
FILE * system_open_memstream(const char * cmdname,char **buf,size_t *buf_size);
#ifdef __cplusplus
}

View File

@@ -54,7 +54,7 @@ static struct {
// todo: implement access point config - cmd_to_json(&i2cdetect_cmd);
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)

View File

@@ -9,24 +9,24 @@
#include "platform_console.h"
#include "argtable3/argtable3.h"
#include "bootstate.h"
#include "cmd_decl.h"
#include "driver/uart.h"
#include "esp_console.h"
#include "esp_log.h"
#include "esp_vfs_dev.h"
#include "linenoise/linenoise.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "platform_esp32.h"
#include "pthread.h"
#include "trace.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"
#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "driver/uart.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "pthread.h"
#include "platform_esp32.h"
#include "cmd_decl.h"
#include "trace.h"
// #include "Configurator.h"
#pragma message("fixme: search for TODO below")
#include "telnet.h"
#include "telnet.h"
#include "tools.h"
#if defined(CONFIG_WITH_METRICS)
#include "metrics.h"
@@ -35,19 +35,19 @@
#include "config.h"
static pthread_t thread_console;
static void * console_thread();
static void* console_thread();
void console_start();
static const char * TAG = "console";
static const char* TAG = "console";
extern bool bypass_network_manager;
extern void launchsqueezelite();
static EXT_RAM_ATTR QueueHandle_t uart_queue;
static EXT_RAM_ATTR struct {
uint8_t _buf[512];
StaticRingbuffer_t _ringbuf;
RingbufHandle_t handle;
QueueSetHandle_t queue_set;
} stdin_redir;
uint8_t _buf[512];
StaticRingbuffer_t _ringbuf;
RingbufHandle_t handle;
QueueSetHandle_t queue_set;
} stdin_redir;
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
@@ -62,94 +62,99 @@ const char* recovery_prompt = LOG_COLOR_E "recovery-squeezelite-esp32> " LOG_RES
#define MOUNT_PATH "/data"
#define HISTORY_PATH MOUNT_PATH "/history.txt"
static esp_err_t run_command(char * line);
#define ADD_TO_JSON(o,t,n) if (t->n) cJSON_AddStringToObject(o,QUOTE(n),t->n);
#define ADD_PARMS_TO_CMD(o,t,n) { cJSON * parms = ParmsToJSON(&t.n->hdr); if(parms) cJSON_AddItemToObject(o,QUOTE(n),parms); }
cJSON * cmdList;
cJSON * values_fn_list;
cJSON * get_cmd_list(){
cJSON * element;
cJSON * values=cJSON_CreateObject();
cJSON * list = cJSON_CreateObject();
cJSON_AddItemReferenceToObject(list,"commands",cmdList);
cJSON_AddItemToObject(list,"values",values);
cJSON_ArrayForEach(element,cmdList){
cJSON * name = cJSON_GetObjectItem(element,"name");
cJSON * vals_fn = cJSON_GetObjectItem(values_fn_list,cJSON_GetStringValue(name));
if(vals_fn!=NULL ){
parm_values_fn_t *parm_values_fn = (parm_values_fn_t *)strtoul(cJSON_GetStringValue(vals_fn), NULL, 16);;
static esp_err_t run_command(char* line);
#define ADD_TO_JSON(o, t, n) \
if (t->n) cJSON_AddStringToObject(o, QUOTE(n), t->n);
#define ADD_PARMS_TO_CMD(o, t, n) \
{ \
cJSON* parms = ParmsToJSON(&t.n->hdr); \
if (parms) cJSON_AddItemToObject(o, QUOTE(n), parms); \
}
cJSON* cmdList;
cJSON* values_fn_list;
cJSON* get_cmd_list() {
cJSON* element;
cJSON* values = cJSON_CreateObject();
cJSON* list = cJSON_CreateObject();
cJSON_AddItemReferenceToObject(list, "commands", cmdList);
cJSON_AddItemToObject(list, "values", values);
cJSON_ArrayForEach(element, cmdList) {
cJSON* name = cJSON_GetObjectItem(element, "name");
cJSON* vals_fn = cJSON_GetObjectItem(values_fn_list, cJSON_GetStringValue(name));
if (vals_fn != NULL) {
parm_values_fn_t* parm_values_fn = (parm_values_fn_t*)strtoul(cJSON_GetStringValue(vals_fn), NULL, 16);
;
if(parm_values_fn){
cJSON_AddItemToObject(values,cJSON_GetStringValue(name),parm_values_fn());
}
}
}
return list;
if (parm_values_fn) {
cJSON_AddItemToObject(values, cJSON_GetStringValue(name), parm_values_fn());
}
}
}
return list;
}
void console_set_bool_parameter(cJSON * root,char * nvs_name, struct arg_lit *arg){
char * p=NULL;
bool enabled = false;
if(!root) {
ESP_LOGE(TAG,"Invalid json parameter. Cannot set %s from %s",arg->hdr.longopts?arg->hdr.longopts:arg->hdr.glossary,nvs_name);
void console_set_bool_parameter(cJSON* root, char* nvs_name, struct arg_lit* arg) {
char* p = NULL;
bool enabled = false;
if (!root) {
ESP_LOGE(TAG, "Invalid json parameter. Cannot set %s from %s", arg->hdr.longopts ? arg->hdr.longopts : arg->hdr.glossary, nvs_name);
return;
}
// if ((p = config_alloc_get(NVS_TYPE_STR, nvs_name)) != NULL) {
// enabled = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
// cJSON_AddBoolToObject(root,arg->hdr.longopts,enabled);
// FREE_AND_NULL(p);
// }
// #if defined(CONFIG_WITH_METRICS)
// if(enabled){
// metrics_add_feature(nvs_name,"enabled");
// }
// #endif
// TODO: Add support for the commented code
// if ((p = config_alloc_get(NVS_TYPE_STR, nvs_name)) != NULL) {
// enabled = strcmp(p,"1") == 0 || strcasecmp(p,"y") == 0;
// cJSON_AddBoolToObject(root,arg->hdr.longopts,enabled);
// FREE_AND_NULL(p);
// }
// #if defined(CONFIG_WITH_METRICS)
// if(enabled){
// metrics_add_feature(nvs_name,"enabled");
// }
// #endif
// TODO: Add support for the commented code
}
struct arg_end *getParmsEnd(struct arg_hdr * * argtable){
if(!argtable) return NULL;
struct arg_hdr * *table = (struct arg_hdr * *)argtable;
int tabindex = 0;
while (!(table[tabindex]->flag & ARG_TERMINATOR))
{
tabindex++;
}
return (struct arg_end *)table[tabindex];
struct arg_end* getParmsEnd(struct arg_hdr** argtable) {
if (!argtable) return NULL;
struct arg_hdr** table = (struct arg_hdr**)argtable;
int tabindex = 0;
while (!(table[tabindex]->flag & ARG_TERMINATOR)) {
tabindex++;
}
return (struct arg_end*)table[tabindex];
}
cJSON * ParmsToJSON(struct arg_hdr * * argtable){
if(!argtable) return NULL;
cJSON * arg_list = cJSON_CreateArray();
struct arg_hdr * *table = (struct arg_hdr * *)argtable;
int tabindex = 0;
while (!(table[tabindex]->flag & ARG_TERMINATOR))
{
cJSON * entry = cJSON_CreateObject();
ADD_TO_JSON(entry,table[tabindex],datatype);
ADD_TO_JSON(entry,table[tabindex],glossary);
ADD_TO_JSON(entry,table[tabindex],longopts);
ADD_TO_JSON(entry,table[tabindex],shortopts);
cJSON_AddBoolToObject(entry, "checkbox", (table[tabindex]->flag & ARG_HASOPTVALUE)==0 && (table[tabindex]->flag & ARG_HASVALUE)==0 && (table[tabindex]->longopts || table[tabindex]->shortopts) );
cJSON_AddBoolToObject(entry, "remark", (table[tabindex]->flag & ARG_HASOPTVALUE)==0 && (table[tabindex]->flag & ARG_HASVALUE)==0 && (!table[tabindex]->longopts && !table[tabindex]->shortopts));
cJSON_AddBoolToObject(entry, "hasvalue", table[tabindex]->flag & ARG_HASVALUE);
cJSON_AddNumberToObject(entry,"mincount",table[tabindex]->mincount);
cJSON_AddNumberToObject(entry,"maxcount",table[tabindex]->maxcount);
cJSON_AddItemToArray(arg_list, entry);
tabindex++;
}
return arg_list;
cJSON* ParmsToJSON(struct arg_hdr** argtable) {
if (!argtable) return NULL;
cJSON* arg_list = cJSON_CreateArray();
struct arg_hdr** table = (struct arg_hdr**)argtable;
int tabindex = 0;
while (!(table[tabindex]->flag & ARG_TERMINATOR)) {
cJSON* entry = cJSON_CreateObject();
ADD_TO_JSON(entry, table[tabindex], datatype);
ADD_TO_JSON(entry, table[tabindex], glossary);
ADD_TO_JSON(entry, table[tabindex], longopts);
ADD_TO_JSON(entry, table[tabindex], shortopts);
cJSON_AddBoolToObject(entry, "checkbox",
(table[tabindex]->flag & ARG_HASOPTVALUE) == 0 && (table[tabindex]->flag & ARG_HASVALUE) == 0 &&
(table[tabindex]->longopts || table[tabindex]->shortopts));
cJSON_AddBoolToObject(entry, "remark",
(table[tabindex]->flag & ARG_HASOPTVALUE) == 0 && (table[tabindex]->flag & ARG_HASVALUE) == 0 &&
(!table[tabindex]->longopts && !table[tabindex]->shortopts));
cJSON_AddBoolToObject(entry, "hasvalue", table[tabindex]->flag & ARG_HASVALUE);
cJSON_AddNumberToObject(entry, "mincount", table[tabindex]->mincount);
cJSON_AddNumberToObject(entry, "maxcount", table[tabindex]->maxcount);
cJSON_AddItemToArray(arg_list, entry);
tabindex++;
}
return arg_list;
}
esp_err_t cmd_to_json(const esp_console_cmd_t *cmd){
return cmd_to_json_with_cb(cmd, NULL);
}
esp_err_t cmd_to_json(const esp_console_cmd_t* cmd) { return cmd_to_json_with_cb(cmd, NULL); }
esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t parm_values_fn){
if(!cmdList){
cmdList=cJSON_CreateArray();
}
if(!values_fn_list){
values_fn_list=cJSON_CreateObject();
}
esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t* cmd, parm_values_fn_t parm_values_fn) {
if (!cmdList) {
cmdList = cJSON_CreateArray();
}
if (!values_fn_list) {
values_fn_list = cJSON_CreateObject();
}
if (cmd->command == NULL) {
return ESP_ERR_INVALID_ARG;
@@ -157,27 +162,26 @@ esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t par
if (strchr(cmd->command, ' ') != NULL) {
return ESP_ERR_INVALID_ARG;
}
cJSON * jsoncmd = cJSON_CreateObject();
ADD_TO_JSON(jsoncmd,cmd,help);
ADD_TO_JSON(jsoncmd,cmd,hint);
if(parm_values_fn){
char addr[11]={0};
snprintf(addr,sizeof(addr),"%lx",(unsigned long)parm_values_fn);
cJSON_AddStringToObject(values_fn_list,cmd->command,addr);
}
cJSON_AddBoolToObject(jsoncmd,"hascb",parm_values_fn!=NULL);
cJSON* jsoncmd = cJSON_CreateObject();
ADD_TO_JSON(jsoncmd, cmd, help);
ADD_TO_JSON(jsoncmd, cmd, hint);
if (parm_values_fn) {
char addr[11] = {0};
snprintf(addr, sizeof(addr), "%lx", (unsigned long)parm_values_fn);
cJSON_AddStringToObject(values_fn_list, cmd->command, addr);
}
cJSON_AddBoolToObject(jsoncmd, "hascb", parm_values_fn != NULL);
if(cmd->argtable){
cJSON_AddItemToObject(jsoncmd,"argtable",ParmsToJSON(cmd->argtable));
if (cmd->argtable) {
cJSON_AddItemToObject(jsoncmd, "argtable", ParmsToJSON(cmd->argtable));
}
if (cmd->hint) {
cJSON_AddStringToObject(jsoncmd, "hint", cmd->hint);
}
else if (cmd->argtable) {
cJSON_AddStringToObject(jsoncmd, "hint", cmd->hint);
} else if (cmd->argtable) {
/* Generate hint based on cmd->argtable */
char *buf = NULL;
char* buf = NULL;
size_t buf_size = 0;
FILE *f = open_memstream(&buf, &buf_size);
FILE* f = open_memstream(&buf, &buf_size);
if (f != NULL) {
arg_print_syntax(f, cmd->argtable, NULL);
fflush(f);
@@ -187,303 +191,237 @@ esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t par
FREE_AND_NULL(buf);
}
cJSON_AddStringToObject(jsoncmd, "name", cmd->command);
char * b=cJSON_Print(jsoncmd);
if(b){
ESP_LOGD(TAG,"Adding command table %s",b);
free(b);
char* b = cJSON_Print(jsoncmd);
if (b) {
ESP_LOGD(TAG, "Adding command table %s", b);
free(b);
}
cJSON_AddItemToArray(cmdList, jsoncmd);
return ESP_OK;
}
int arg_parse_msg(int argc, char **argv, struct arg_hdr ** args){
int nerrors = arg_parse(argc, argv, (void **)args);
int arg_parse_msg(int argc, char** argv, struct arg_hdr** args) {
int nerrors = arg_parse(argc, argv, (void**)args);
if (nerrors != 0) {
char *buf = NULL;
size_t buf_size = 0;
FILE *f = open_memstream(&buf, &buf_size);
if (f != NULL) {
arg_print_errors(f, getParmsEnd(args), argv[0]);
fflush (f);
cmd_send_messaging(argv[0],MESSAGING_ERROR,"%s", buf);
}
char* buf = NULL;
size_t buf_size = 0;
FILE* f = open_memstream(&buf, &buf_size);
if (f != NULL) {
arg_print_errors(f, getParmsEnd(args), argv[0]);
fflush(f);
cmd_send_messaging(argv[0], MESSAGING_ERROR, "%s", buf);
}
fclose(f);
FREE_AND_NULL(buf);
}
return nerrors;
}
void process_autoexec(){
int i=1;
char autoexec_name[21]={0};
char * autoexec_value=NULL;
uint8_t autoexec_flag=0;
// TODO: Add support for the commented code
void * cmd = run_command;
// char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
// if(!bypass_network_manager){
// ESP_LOGW(TAG, "Processing autoexec commands while network manager active. Wifi related commands will be ignored.");
// }
// if(is_recovery_running){
// ESP_LOGD(TAG, "Processing autoexec commands in recovery mode. Squeezelite commands will be ignored.");
// }
// if(str_flag !=NULL ){
// autoexec_flag=atoi(str_flag);
// ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
// if(autoexec_flag == 1) {
// do {
// snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
// ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
// autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
// if(autoexec_value!=NULL ){
// if(!bypass_network_manager && strstr(autoexec_value, "join ")!=NULL ){
// ESP_LOGW(TAG,"Ignoring wifi join command.");
// }
// else if(is_recovery_running && !strstr(autoexec_value, "squeezelite " ) ){
// ESP_LOGW(TAG,"Ignoring command. ");
// }
// else {
// ESP_LOGI(TAG,"Running command %s = %s", autoexec_name, autoexec_value);
// run_command(autoexec_value);
// }
// ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name);
// free(autoexec_value);
// }
// else {
// ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
// break;
// }
// } while(1);
// }
// free(str_flag);
// }
// else
// {
// ESP_LOGD(TAG,"No matching command found for name autoexec.");
// }
// TODO: Add support for the commented code
}
static ssize_t stdin_read(int fd, void* data, size_t size) {
size_t bytes = -1;
while (1) {
QueueSetMemberHandle_t activated = xQueueSelectFromSet(stdin_redir.queue_set, portMAX_DELAY);
if (activated == uart_queue) {
uart_event_t event;
xQueueReceive(uart_queue, &event, 0);
if (event.type == UART_DATA) {
bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < event.size ? size : event.size, 0);
// we have to do our own line ending translation here
for (int i = 0; i < bytes; i++) if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
break;
}
} else if (xRingbufferCanRead(stdin_redir.handle, activated)) {
char *p = xRingbufferReceiveUpTo(stdin_redir.handle, &bytes, 0, size);
// we might receive strings, replace null by \n
for (int i = 0; i < bytes; i++) if (p[i] == '\0' || p[i] == '\r') p[i] = '\n';
memcpy(data, p, bytes);
vRingbufferReturnItem(stdin_redir.handle, p);
break;
}
}
return bytes;
size_t bytes = -1;
static size_t remain = 0;
if (remain > 0) {
bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < remain ? size : remain, 0);
remain -= bytes;
for (int i = 0; i < bytes; i++)
if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
return bytes;
}
while (1) {
QueueSetMemberHandle_t activated = xQueueSelectFromSet(stdin_redir.queue_set, portMAX_DELAY);
if (activated == uart_queue) {
uart_event_t event;
xQueueReceive(uart_queue, &event, 0);
if (event.type == UART_DATA) {
bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < event.size ? size : event.size, 0);
// we have to do our own line ending translation here
for (int i = 0; i < bytes; i++)
if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
if (event.size > bytes) {
remain = event.size - bytes;
}
break;
}
} else if (xRingbufferCanRead(stdin_redir.handle, activated)) {
char* p = xRingbufferReceiveUpTo(stdin_redir.handle, &bytes, 0, size);
// we might receive strings, replace null by \n
for (int i = 0; i < bytes; i++)
if (p[i] == '\0' || p[i] == '\r') p[i] = '\n';
memcpy(data, p, bytes);
vRingbufferReturnItem(stdin_redir.handle, p);
xRingbufferPrintInfo(stdin_redir.handle);
break;
}
}
return bytes;
}
static int stdin_dummy(const char * path, int flags, int mode) { return 0; }
static int stdin_dummy(const char* path, int flags, int mode) { return 0; }
void initialize_console() {
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed (unused if we redirect stdin) */
esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed (unused if we redirect stdin) */
esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
/* Move the caret to the beginning of the next line on '\n' */
esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
* correct while APB frequency is changing in light sleep mode.
*/
const uart_config_t uart_config = { .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1,
};
ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
* correct while APB frequency is changing in light sleep mode.
*/
const uart_config_t uart_config = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
};
ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 3, &uart_queue, 0));
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
/* re-direct stdin to our own driver so we can gather data from various sources */
stdin_redir.queue_set = xQueueCreateSet(2);
stdin_redir.handle = xRingbufferCreateStatic(sizeof(stdin_redir._buf), RINGBUF_TYPE_BYTEBUF, stdin_redir._buf, &stdin_redir._ringbuf);
xRingbufferAddToQueueSetRead(stdin_redir.handle, stdin_redir.queue_set);
xQueueAddToSet(uart_queue, stdin_redir.queue_set);
esp_vfs_t vfs = { };
vfs.flags = ESP_VFS_FLAG_DEFAULT;
vfs.open = stdin_dummy;
vfs.read = stdin_read;
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 3, &uart_queue, 0));
ESP_ERROR_CHECK(esp_vfs_register("/dev/redirect", &vfs, NULL));
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
/* re-direct stdin to our own driver so we can gather data from various sources */
stdin_redir.queue_set = xQueueCreateSet(2);
stdin_redir.handle = xRingbufferCreateStatic(sizeof(stdin_redir._buf), RINGBUF_TYPE_BYTEBUF, stdin_redir._buf, &stdin_redir._ringbuf);
xRingbufferAddToQueueSetRead(stdin_redir.handle, stdin_redir.queue_set);
xQueueAddToSet(uart_queue, stdin_redir.queue_set);
esp_vfs_t vfs = {};
vfs.flags = ESP_VFS_FLAG_DEFAULT;
vfs.open = stdin_dummy;
vfs.read = stdin_read;
ESP_ERROR_CHECK(esp_vfs_register("/dev/redirect", &vfs, NULL));
freopen("/dev/redirect", "r", stdin);
/* Disable buffering on stdin */
setvbuf(stdin, NULL, _IONBF, 0);
/* Disable buffering on stdin */
setvbuf(stdin, NULL, _IONBF, 0);
/* Initialize the console */
esp_console_config_t console_config = { .max_cmdline_args = 28,
.max_cmdline_length = 600,
/* Initialize the console */
esp_console_config_t console_config = {
.max_cmdline_args = 28,
.max_cmdline_length = 600,
#if CONFIG_LOG_COLORS
.hint_color = atoi(LOG_COLOR_CYAN)
.hint_color = atoi(LOG_COLOR_CYAN)
#endif
};
ESP_ERROR_CHECK(esp_console_init(&console_config));
};
ESP_ERROR_CHECK(esp_console_init(&console_config));
/* Configure linenoise line completion library */
/* Enable multiline editing. If not set, long commands will scroll within
* single line.
*/
linenoiseSetMultiLine(1);
/* Configure linenoise line completion library */
/* Enable multiline editing. If not set, long commands will scroll within
* single line.
*/
linenoiseSetMultiLine(1);
/* Tell linenoise where to get command completions and hints */
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
/* Tell linenoise where to get command completions and hints */
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback*)&esp_console_get_hint);
/* Set command history size */
linenoiseHistorySetMaxLen(100);
/* Set command history size */
linenoiseHistorySetMaxLen(100);
/* Load command history from filesystem */
//linenoiseHistoryLoad(HISTORY_PATH);
}
bool console_push(const char *data, size_t size) {
return xRingbufferSend(stdin_redir.handle, data, size, pdMS_TO_TICKS(100)) == pdPASS;
}
bool console_push(const char* data, size_t size) { return xRingbufferSend(stdin_redir.handle, data, size, pdMS_TO_TICKS(100)) == pdPASS; }
void console_start() {
/* we always run console b/c telnet sends commands to stdin */
initialize_console();
/* Register commands */
MEMTRACE_PRINT_DELTA_MESSAGE("Registering help command");
esp_console_register_help_command();
MEMTRACE_PRINT_DELTA_MESSAGE("Registering system commands");
register_system();
MEMTRACE_PRINT_DELTA_MESSAGE("Registering config commands");
register_config_cmd();
MEMTRACE_PRINT_DELTA_MESSAGE("Registering wifi commands");
register_wifi();
/* Register commands */
esp_console_register_help_command();
register_system();
register_config_cmd();
register_wifi();
if(is_recovery_running){
MEMTRACE_PRINT_DELTA_MESSAGE("Registering recovery commands");
register_ota_cmd();
}
MEMTRACE_PRINT_DELTA_MESSAGE("Registering i2c commands");
register_i2ctools();
printf("\n");
if(is_recovery_running){
printf("****************************************************************\n"
"RECOVERY APPLICATION\n"
"This mode is used to flash Squeezelite into the OTA partition\n"
"****\n\n");
}
printf("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"
"\n");
if(!is_recovery_running){
printf("To automatically execute lines at startup:\n"
"\tSet NVS variable autoexec (U8) = 1 to enable, 0 to disable automatic execution.\n"
"\tSet NVS variable autoexec[1~9] (string)to a command that should be executed automatically\n");
}
printf("\n\n");
if (is_recovery_running) {
register_ota_cmd();
}
register_i2ctools();
/* Figure out if the terminal supports escape sequences */
int probe_status = linenoiseProbe();
if (probe_status) { /* zero indicates success */
printf("\n****************************\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"
"****************************\n");
linenoiseSetDumbMode(1);
printf("\n");
if (is_recovery_running) {
printf("****************************************************************\n"
"RECOVERY APPLICATION\n"
"This mode is used to flash Squeezelite into the OTA partition\n"
"****\n\n");
}
printf("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"
"\n");
printf("\n\n");
/* Figure out if the terminal supports escape sequences */
int probe_status = linenoiseProbe();
if (probe_status) { /* zero indicates success */
printf("\n****************************\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"
"****************************\n");
linenoiseSetDumbMode(1);
#if CONFIG_LOG_COLORS
/* Since the terminal doesn't support escape sequences,
* don't use color codes in the prompt.
*/
if(is_recovery_running){
recovery_prompt= "recovery-squeezelite-esp32>";
}
prompt = "squeezelite-esp32> ";
#endif //CONFIG_LOG_COLORS
}
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
cfg.thread_name= "console";
cfg.inherit_cfg = true;
cfg.stack_size = 4*1024;
if(is_recovery_running){
prompt = recovery_prompt;
}
MEMTRACE_PRINT_DELTA_MESSAGE("Creating console thread with stack size of 4096 bytes");
esp_pthread_set_cfg(&cfg);
pthread_create(&thread_console, NULL, console_thread, NULL);
MEMTRACE_PRINT_DELTA_MESSAGE("Console thread created");
/* Since the terminal doesn't support escape sequences,
* don't use color codes in the prompt.
*/
if (is_recovery_running) {
recovery_prompt = "recovery-squeezelite-esp32>";
}
prompt = "squeezelite-esp32> ";
#endif // CONFIG_LOG_COLORS
}
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
cfg.thread_name = "console";
cfg.inherit_cfg = true;
cfg.stack_size = 4 * 1024;
if (is_recovery_running) {
prompt = recovery_prompt;
}
esp_pthread_set_cfg(&cfg);
pthread_create(&thread_console, NULL, console_thread, NULL);
}
static esp_err_t run_command(char * line){
/* Try to run the command */
int ret;
esp_err_t err = esp_console_run(line, &ret);
static esp_err_t run_command(char* line) {
/* Try to run the command */
int ret;
esp_err_t err = esp_console_run(line, &ret);
if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGE(TAG,"Unrecognized command: %s", line);
} else if (err == ESP_ERR_INVALID_ARG) {
// command was empty
} else if (err != ESP_OK && ret != ESP_OK) {
ESP_LOGW(TAG,"Command returned non-zero error code: 0x%x (%s)", ret,
esp_err_to_name(err));
} else if (err == ESP_OK && ret != ESP_OK) {
ESP_LOGW(TAG,"Command returned in error");
err = ESP_FAIL;
} else if (err != ESP_OK) {
ESP_LOGE(TAG,"Internal error: %s", esp_err_to_name(err));
}
return err;
if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGE(TAG, "Unrecognized command: %s", line);
} else if (err == ESP_ERR_INVALID_ARG) {
// command was empty
} else if (err != ESP_OK && ret != ESP_OK) {
ESP_LOGW(TAG, "Command returned non-zero error code: 0x%x (%s)", ret, esp_err_to_name(err));
} else if (err == ESP_OK && ret != ESP_OK) {
ESP_LOGW(TAG, "Command returned in error");
err = ESP_FAIL;
} else if (err != ESP_OK) {
ESP_LOGE(TAG, "Internal error: %s", esp_err_to_name(err));
}
return err;
}
static void * console_thread() {
if(!is_recovery_running){
MEMTRACE_PRINT_DELTA_MESSAGE("Running autoexec");
process_autoexec();
MEMTRACE_PRINT_DELTA_MESSAGE("Autoexec done");
}
/* Main loop */
while (1) {
/* Get a line using linenoise.
* The line is returned when ENTER is pressed.
*/
char* line = linenoise(prompt);
if (line == NULL) { /* Ignore empty lines */
continue;
}
/* Add the command to the history */
linenoiseHistoryAdd(line);
static void* console_thread() {
/* Main loop */
while (1) {
/* Get a line using linenoise.
* The line is returned when ENTER is pressed.
*/
char* line = linenoise(prompt);
if (line == NULL) { /* Ignore empty lines */
continue;
}
/* Add the command to the history */
linenoiseHistoryAdd(line);
/* Save command history to filesystem */
linenoiseHistorySave(HISTORY_PATH);
printf("\n");
run_command(line);
/* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line);
taskYIELD();
}
return NULL;
/* Save command history to filesystem */
linenoiseHistorySave(HISTORY_PATH);
printf("\n");
run_command(line);
/* linenoise allocates line buffer on the heap, so need to free it */
linenoiseFree(line);
taskYIELD();
}
return NULL;
}

View File

@@ -22,6 +22,7 @@ typedef cJSON * parm_values_fn_t(void);
esp_err_t cmd_to_json(const esp_console_cmd_t *cmd);
esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t parm_values_fn);
int arg_parse_msg(int argc, char **argv, struct arg_hdr ** args);
void initialize_console();
cJSON * get_cmd_list();
#ifdef __cplusplus
}

View File

@@ -11,7 +11,7 @@
#include "unity.h"
#include "platform_console.h"
#include "platform_esp32.h"
// #include "Configurator.h"
// #include "Config.h"
#pragma message("fixme: search for TODO below")
#include "string.h"
struct arg_lit *arglit;