diff --git a/Dockerfile b/Dockerfile index fb8815c6..716f93c3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,11 @@ ENV GCC_TOOLS_BASE=/opt/esp/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtens # pushd components/wifi-manager/webapp/ && npm install && npm run-script build && popd # # to run the docker with netwotrk port published on the host: +# (windows) # docker run --rm -p 5000:5000/tcp -v %cd%:/project -w /project -it sle118/squeezelite-esp32-idfv435 +# (linux) +# docker run --rm -p 5000:5000/tcp -v `pwd`:/project -w /project -it sle118/squeezelite-esp32-idfv435 + ARG IDF_CLONE_URL=https://github.com/espressif/esp-idf.git ARG IDF_CLONE_BRANCH_OR_TAG=master diff --git a/components/platform_console/app_squeezelite/cmd_squeezelite.c b/components/platform_console/app_squeezelite/cmd_squeezelite.c index 88000a8f..075e1410 100644 --- a/components/platform_console/app_squeezelite/cmd_squeezelite.c +++ b/components/platform_console/app_squeezelite/cmd_squeezelite.c @@ -43,11 +43,15 @@ 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_ledvu_config(void); +extern void register_nvs(); void register_optional_cmd(void) { + #if CONFIG_WITH_CONFIG_UI register_rotary_config(); - register_ledvu_config(); +#endif register_audio_config(); + register_ledvu_config(); + register_nvs(); } extern int squeezelite_main(int argc, char **argv); diff --git a/components/platform_console/cmd_config.c b/components/platform_console/cmd_config.c index b5d89d4e..e2db2c9c 100644 --- a/components/platform_console/cmd_config.c +++ b/components/platform_console/cmd_config.c @@ -6,30 +6,30 @@ software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include #include "cmd_config.h" -#include "argtable3/argtable3.h" -#include "platform_console.h" -#include "esp_log.h" -#include "string.h" -#include "stdio.h" -#include "platform_config.h" -#include "messaging.h" #include "accessors.h" #include "adac.h" -#include "tools.h" +#include "argtable3/argtable3.h" #include "cJSON.h" #include "cmd_i2ctools.h" - -const char * desc_squeezelite ="Squeezelite Options"; -const char * desc_dac= "DAC Options"; -const char * desc_cspotc= "Spotify (cSpot) Options"; -const char * desc_preset= "Preset Options"; -const char * desc_spdif= "SPDIF Options"; -const char * desc_audio= "General Audio Options"; -const char * desc_bt_source= "Bluetooth Audio Output Options"; -const char * desc_rotary= "Rotary Control"; -const char * desc_ledvu= "Led Strip Options"; +#include "cmd_system.h" +#include "esp_log.h" +#include "messaging.h" +#include "platform_config.h" +#include "platform_console.h" +#include "stdio.h" +#include "string.h" +#include "tools.h" +#include +const char *desc_squeezelite = "Squeezelite Options"; +const char *desc_dac = "DAC Options"; +const char *desc_cspotc = "Spotify (cSpot) Options"; +const char *desc_preset = "Preset Options"; +const char *desc_spdif = "SPDIF Options"; +const char *desc_audio = "General Audio Options"; +const char *desc_bt_source = "Bluetooth Audio Output Options"; +const char *desc_rotary = "Rotary Control"; +const char *desc_ledvu = "Led Strip Options"; extern const struct adac_s *dac_set[]; extern void equalizer_set_loudness(uint8_t); @@ -37,21 +37,21 @@ extern void register_optional_cmd(void); #define CODECS_BASE "flac|pcm|mp3|ogg" #if NO_FAAD -#define CODECS_AAC "" +#define CODECS_AAC "" #else -#define CODECS_AAC "|aac" +#define CODECS_AAC "|aac" #endif #if FFMPEG -#define CODECS_FF "|wma|alac" +#define CODECS_FF "|wma|alac" #else -#define CODECS_FF "" +#define CODECS_FF "" #endif #if DSD -#define CODECS_DSD "|dsd" +#define CODECS_DSD "|dsd" #else -#define CODECS_DSD "" +#define CODECS_DSD "" #endif -#define CODECS_MP3 "|mad|mpg" +#define CODECS_MP3 "|mad|mpg" #if !defined(MODEL_NAME) #define MODEL_NAME SqueezeLite @@ -59,13 +59,12 @@ extern void register_optional_cmd(void); #ifndef QUOTE #define QUOTE(name) #name -#define STR(macro) QUOTE(macro) +#define STR(macro) QUOTE(macro) #endif #ifndef MODEL_NAME_STRING #define MODEL_NAME_STRING STR(MODEL_NAME) #endif - #define CODECS CODECS_BASE CODECS_AAC CODECS_FF CODECS_DSD CODECS_MP3 #define NOT_OUTPUT "has input capabilities only" #define NOT_GPIO "is not a GPIO" @@ -78,1412 +77,1336 @@ typedef enum { FINISHING } parse_state_t; static const char *TAG = "cmd_config"; -extern struct arg_end *getParmsEnd(struct arg_hdr * * argtable); -//bck=,ws=,do=[,mute=[:0|1][,model=TAS57xx|TAS5713|AC101|WM8978|I2S][,sda=,scl=gpio[,i2c=]] +extern struct arg_end *getParmsEnd(struct arg_hdr **argtable); +// bck=,ws=,do=[,mute=[:0|1][,model=TAS57xx|TAS5713|AC101|WM8978|I2S][,sda=,scl=gpio[,i2c=]] static struct { - struct arg_str *model_name; + struct arg_str *model_name; struct arg_int *clock; struct arg_int *wordselect; struct arg_int *data; struct arg_int *mute_gpio; - struct arg_lit *mute_level; + struct arg_lit *mute_level; struct arg_int *dac_sda; struct arg_int *dac_scl; struct arg_int *dac_i2c; - struct arg_lit *clear; + struct arg_lit *clear; struct arg_end *end; } i2s_args; static struct { - struct arg_str *model_config; + struct arg_str *model_config; struct arg_end *end; } known_model_args; static struct { - struct arg_rem * rem; - struct arg_int * A; - struct arg_int * B; - struct arg_int * SW; - struct arg_lit * volume_lock; - struct arg_lit * longpress; - struct arg_lit * knobonly; - struct arg_int * timer; - struct arg_lit * clear; - struct arg_lit * raw_mode; - struct arg_end * end; + struct arg_rem *rem; + struct arg_int *A; + struct arg_int *B; + struct arg_int *SW; + struct arg_lit *volume_lock; + struct arg_lit *longpress; + struct arg_lit *knobonly; + struct arg_int *timer; + struct arg_lit *clear; + struct arg_lit *raw_mode; + struct arg_end *end; } rotary_args; -//config_rotary_get +// config_rotary_get static struct { - struct arg_str * type; - struct arg_int * length; - struct arg_int * gpio; - struct arg_lit * clear; - struct arg_end * end; + struct arg_str *type; + struct arg_int *length; + struct arg_int *gpio; + struct arg_lit *clear; + struct arg_end *end; } ledvu_args; -static struct{ - struct arg_str *sink_name; - struct arg_str *pin_code; -// struct arg_dbl *connect_timeout_delay; -// struct arg_dbl *control_delay; - struct arg_end *end; +static struct { + struct arg_str *sink_name; + struct arg_str *pin_code; + // struct arg_dbl *connect_timeout_delay; + // struct arg_dbl *control_delay; + struct arg_end *end; } bt_source_args; static struct { - struct arg_str *deviceName; -// struct arg_int *volume; - struct arg_int *bitrate; + struct arg_str *deviceName; + // struct arg_int *volume; + struct arg_int *bitrate; struct arg_int *zeroConf; - struct arg_end *end; + struct arg_end *end; } cspot_args; static struct { struct arg_int *clock; struct arg_int *wordselect; struct arg_int *data; - struct arg_lit *clear; + struct arg_lit *clear; struct arg_end *end; } spdif_args; static struct { - struct arg_str *jack_behavior; - struct arg_int *loudness; + struct arg_str *jack_behavior; + struct arg_int *loudness; struct arg_end *end; } audio_args; static struct { - struct arg_str * output_device; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" - struct arg_str * name;// " -n \t\tSet the player name\n" - struct arg_str * server; // -s [:]\tConnect to specified server, otherwise uses autodiscovery to find server\n" - struct arg_str * buffers;// " -b :\tSpecify internal Stream and Output buffer sizes in Kbytes\n" - struct arg_str * codecs;// " -c ,\tRestrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS "\n" - struct arg_int * timeout;// " -C \t\tClose output device when idle after timeout seconds, default is to keep it open while player is 'on'\n" - struct arg_str * log_level; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" - // struct arg_str * log_level_all; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" - // struct arg_str * log_level_slimproto; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" - // struct arg_str * log_level_stream; - // struct arg_str * log_level_decode; - // struct arg_str * log_level_output; - #if IR - struct arg_str * log_level_ir; - #endif - // " -e ,\tExplicitly exclude native support of one or more codecs; known codecs: " CODECS "\n" - // " -f \t\tWrite debug to logfile\n" - // #if IR - // " -i []\tEnable lirc remote control support (lirc config file ~/.lircrc used if filename not specified)\n" - // #endif - struct arg_str * mac_addr; // " -m \t\tSet mac address, format: ab:cd:ef:12:34:56\n" - struct arg_str * model_name;// " -M \tSet the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")\n" - struct arg_lit * header_format;// " -W\t\t\tRead wave and aiff format from header, ignore server parameters\n" - struct arg_str * rates; // " -r [:]\tSample rates supported, allows output to be off when squeezelite is started; rates = |-|,,; delay = optional delay switching rates in ms\n" - #if RESAMPLE - struct arg_lit * resample; - struct arg_str * resample_parms; //" -R -u [params]\tResample, params = ::::::,\n" - #endif - #if RESAMPLE16 - struct arg_lit * resample; - struct arg_str * resample_parms; //" -R -u [params]\tResample, params = (b|l|m)[:i],\n" - // " \t\t\t b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients\n" - #endif - struct arg_int * rate;// " -Z \t\tReport rate to server in helo as the maximum sample rate we can support\n" + struct arg_str *output_device; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" + struct arg_str *name; // " -n \t\tSet the player name\n" + struct arg_str *server; // -s [:]\tConnect to specified server, otherwise uses autodiscovery to find server\n" + struct arg_str *buffers; // " -b :\tSpecify internal Stream and Output buffer sizes in Kbytes\n" + struct arg_str *codecs; // " -c ,\tRestrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS "\n" + struct arg_int *timeout; // " -C \t\tClose output device when idle after timeout seconds, default is to keep it open while player is 'on'\n" + struct arg_str *log_level; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" +// struct arg_str * log_level_all; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" +// struct arg_str * log_level_slimproto; // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" +// struct arg_str * log_level_stream; +// struct arg_str * log_level_decode; +// struct arg_str * log_level_output; +#if IR + struct arg_str *log_level_ir; +#endif + // " -e ,\tExplicitly exclude native support of one or more codecs; known codecs: " CODECS "\n" + // " -f \t\tWrite debug to logfile\n" + // #if IR + // " -i []\tEnable lirc remote control support (lirc config file ~/.lircrc used if filename not specified)\n" + // #endif + struct arg_str *mac_addr; // " -m \t\tSet mac address, format: ab:cd:ef:12:34:56\n" + struct arg_str *model_name; // " -M \tSet the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")\n" + struct arg_lit *header_format; // " -W\t\t\tRead wave and aiff format from header, ignore server parameters\n" + struct arg_str *rates; // " -r [:]\tSample rates supported, allows output to be off when squeezelite is started; rates = |-|,,; delay = optional delay switching rates in ms\n" +#if RESAMPLE + struct arg_lit *resample; + struct arg_str *resample_parms; //" -R -u [params]\tResample, params = ::::::,\n" +#endif +#if RESAMPLE16 + struct arg_lit *resample; + struct arg_str *resample_parms; //" -R -u [params]\tResample, params = (b|l|m)[:i],\n" +// " \t\t\t b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients\n" +#endif + struct arg_int *rate; // " -Z \t\tReport rate to server in helo as the maximum sample rate we can support\n" struct arg_end *end; } squeezelite_args; -int is_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory, bool output){ - int res = 0; - const char * name = gpio->hdr.longopts?gpio->hdr.longopts:gpio->hdr.glossary; - *gpio_out=-1; - int t_gpio=gpio->ival[0]; - if(gpio->count==0){ - if(mandatory){ - fprintf(f,"Missing: %s\n", name); - res++; - } - } else if((output && !GPIO_IS_VALID_OUTPUT_GPIO(t_gpio)) || (!GPIO_IS_VALID_GPIO(t_gpio))){ - fprintf(f,"Invalid %s gpio: [%d] %s\n",name, t_gpio, GPIO_IS_VALID_GPIO(t_gpio)?NOT_OUTPUT:NOT_GPIO ); - res++; - } - else{ - *gpio_out = t_gpio; - } - return res; +int is_gpio(struct arg_int *gpio, FILE *f, int *gpio_out, bool mandatory, bool output) { + int res = 0; + const char *name = gpio->hdr.longopts ? gpio->hdr.longopts : gpio->hdr.glossary; + *gpio_out = -1; + int t_gpio = gpio->ival[0]; + if (gpio->count == 0) { + if (mandatory) { + fprintf(f, "Missing: %s\n", name); + res++; + } + } else if ((output && !GPIO_IS_VALID_OUTPUT_GPIO(t_gpio)) || (!GPIO_IS_VALID_GPIO(t_gpio))) { + fprintf(f, "Invalid %s gpio: [%d] %s\n", name, t_gpio, GPIO_IS_VALID_GPIO(t_gpio) ? NOT_OUTPUT : NOT_GPIO); + res++; + } else { + *gpio_out = t_gpio; + } + return res; } -int is_output_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory){ - return is_gpio(gpio,f,gpio_out,mandatory,true); +int is_output_gpio(struct arg_int *gpio, FILE *f, int *gpio_out, bool mandatory) { + return is_gpio(gpio, f, gpio_out, mandatory, true); } -int check_missing_parm(struct arg_int * int_parm, FILE * f){ - int res=0; - const char * name = int_parm->hdr.longopts?int_parm->hdr.longopts:int_parm->hdr.glossary; - if(int_parm->count==0){ - fprintf(f,"Missing: %s\n", name); - res++; - } - return res; +int check_missing_parm(struct arg_int *int_parm, FILE *f) { + int res = 0; + const char *name = int_parm->hdr.longopts ? int_parm->hdr.longopts : int_parm->hdr.glossary; + if (int_parm->count == 0) { + fprintf(f, "Missing: %s\n", name); + res++; + } + return res; } -char * strip_bt_name(char * opt_str) -{ - if(!opt_str || strlen(opt_str)==0){ - ESP_LOGW(TAG,"strip_bt_name: opt_str is NULL"); - return NULL; - } - char *result = malloc_init_external(strlen(opt_str)+1); +char *strip_bt_name(char *opt_str) { + if (!opt_str || strlen(opt_str) == 0) { + ESP_LOGW(TAG, "strip_bt_name: opt_str is NULL"); + return NULL; + } + char *result = malloc_init_external(strlen(opt_str) + 1); char *str = strdup_psram(opt_str); - const char * output_marker=" -o"; - - if(!result ){ - ESP_LOGE(TAG,"Error allocating memory for result."); + const char *output_marker = " -o"; + + if (!result) { + ESP_LOGE(TAG, "Error allocating memory for result."); return opt_str; } - if(!str){ - ESP_LOGE(TAG,"Error duplicating command line string."); + if (!str) { + ESP_LOGE(TAG, "Error duplicating command line string."); return opt_str; - } - bool quoted=false; + } + bool quoted = false; parse_state_t state = SEARCHING_FOR_BT; char *start = strstr(str, output_marker); - if (start) - { - ESP_LOGV(TAG,"Found output option : %s\n",start); - start+=strlen(output_marker); + if (start) { + ESP_LOGV(TAG, "Found output option : %s\n", start); + start += strlen(output_marker); strncpy(result, str, (size_t)(start - str)); - char * pch=strtok(start," "); - while(pch){ - ESP_LOGV(TAG,"Current output: %s\n[%s]",result,pch); - switch (state) - { - case SEARCHING_FOR_BT: - if (strcasestr(pch, "BT") ) - { - state = SEARCHING_FOR_NAME; - quoted=strcasestr(pch, "BT")!=NULL; - ESP_LOGV(TAG," - fount BT Start %s", quoted?"quoted":""); - } - else - { - ESP_LOGV(TAG," - Searching for BT, Ignoring"); - } - strcat(result, " "); - strcat(result, pch); - break; - case SEARCHING_FOR_NAME: - if (strcasestr(pch, "name") || strcasestr(pch, "n")) - { - ESP_LOGV(TAG," - Found name tag"); - state = SEARCHING_FOR_NAME_START; - } - else - { - strcat(result, " "); - strcat(result, pch); - ESP_LOGV(TAG," - Searching for name - added ");; - } - break; - case SEARCHING_FOR_NAME_START: - ESP_LOGV(TAG," - Name start"); - state = SEARCHING_FOR_NAME_END; + char *pch = strtok(start, " "); + while (pch) { + ESP_LOGV(TAG, "Current output: %s\n[%s]", result, pch); + switch (state) { + case SEARCHING_FOR_BT: + if (strcasestr(pch, "BT")) { + state = SEARCHING_FOR_NAME; + quoted = strcasestr(pch, "BT") != NULL; + ESP_LOGV(TAG, " - fount BT Start %s", quoted ? "quoted" : ""); + } else { + ESP_LOGV(TAG, " - Searching for BT, Ignoring"); + } + strcat(result, " "); + strcat(result, pch); break; - case SEARCHING_FOR_NAME_END: - if (strcasestr(pch, "\"")){ - ESP_LOGV(TAG," - got quoted string"); - state = FINISHING; - } - else if(pch[0]== '-'){ - strcat(result, " "); - strcat(result, pch); - ESP_LOGV(TAG," - got parameter marker"); - state = quoted?SEARCHING_FOR_BT_CMD_END:FINISHING; - } - else { - ESP_LOGV(TAG," - name continued"); - } - break; - case SEARCHING_FOR_BT_CMD_END: - ESP_LOGV(TAG," - looking for quoted BT cmd end"); - if (strcasestr(pch, "\"")){ - ESP_LOGV(TAG," - got quote termination"); - state = FINISHING; - } + case SEARCHING_FOR_NAME: + if (strcasestr(pch, "name") || strcasestr(pch, "n")) { + ESP_LOGV(TAG, " - Found name tag"); + state = SEARCHING_FOR_NAME_START; + } else { strcat(result, " "); strcat(result, pch); - break; - case FINISHING: + ESP_LOGV(TAG, " - Searching for name - added "); + ; + } + break; + case SEARCHING_FOR_NAME_START: + ESP_LOGV(TAG, " - Name start"); + state = SEARCHING_FOR_NAME_END; + break; + case SEARCHING_FOR_NAME_END: + if (strcasestr(pch, "\"")) { + ESP_LOGV(TAG, " - got quoted string"); + state = FINISHING; + } else if (pch[0] == '-') { strcat(result, " "); strcat(result, pch); - break; - default: - - break; + ESP_LOGV(TAG, " - got parameter marker"); + state = quoted ? SEARCHING_FOR_BT_CMD_END : FINISHING; + } else { + ESP_LOGV(TAG, " - name continued"); + } + break; + case SEARCHING_FOR_BT_CMD_END: + ESP_LOGV(TAG, " - looking for quoted BT cmd end"); + if (strcasestr(pch, "\"")) { + ESP_LOGV(TAG, " - got quote termination"); + state = FINISHING; + } + strcat(result, " "); + strcat(result, pch); + break; + case FINISHING: + strcat(result, " "); + strcat(result, pch); + break; + default: + + break; } pch = strtok(NULL, " "); - ESP_LOGV(TAG,"\n"); + ESP_LOGV(TAG, "\n"); } - } - else - { - ESP_LOGE(TAG,"output option not found in %s\n",str); - strcpy(result,str); + } else { + ESP_LOGE(TAG, "output option not found in %s\n", str); + strcpy(result, str); } - ESP_LOGV(TAG,"Result commmand : %s\n", result); + ESP_LOGV(TAG, "Result commmand : %s\n", result); free(str); return result; } -static int do_bt_source_cmd(int argc, char **argv){ - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&bt_source_args); - char *buf = NULL; - size_t buf_size = 0; -// char value[100] ={0}; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - arg_print_errors(f,bt_source_args.end,desc_bt_source); - fclose(f); - return 1; - } +static int do_bt_source_cmd(int argc, char **argv) { + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&bt_source_args); + char *buf = NULL; + size_t buf_size = 0; + // char value[100] ={0}; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + arg_print_errors(f, bt_source_args.end, desc_bt_source); + fclose(f); + return 1; + } - if(bt_source_args.sink_name->count >0){ - err = config_set_value(NVS_TYPE_STR, "a2dp_sink_name", bt_source_args.sink_name->sval[0]); - if(err!=ESP_OK){ + if (bt_source_args.sink_name->count > 0) { + err = config_set_value(NVS_TYPE_STR, "a2dp_sink_name", bt_source_args.sink_name->sval[0]); + if (err != ESP_OK) { nerrors++; - fprintf(f,"Error setting Bluetooth audio device name %s. %s\n",bt_source_args.sink_name->sval[0], esp_err_to_name(err)); + fprintf(f, "Error setting Bluetooth audio device name %s. %s\n", bt_source_args.sink_name->sval[0], esp_err_to_name(err)); + } else { + fprintf(f, "Bluetooth audio device name changed to %s\n", bt_source_args.sink_name->sval[0]); } - else { - fprintf(f,"Bluetooth audio device name changed to %s\n",bt_source_args.sink_name->sval[0]); - } - char * squeezelite_cmd = config_alloc_get_default(NVS_TYPE_STR, "autoexec1", NULL, 0); - if( squeezelite_cmd && strstr(squeezelite_cmd," -o ") ){ - char * new_cmd = strip_bt_name(squeezelite_cmd); - if(strcmp(new_cmd,squeezelite_cmd)!=0){ - fprintf(f,"Replacing old squeezelite command [%s] with [%s].\n",squeezelite_cmd,new_cmd); - config_set_value(NVS_TYPE_STR, "autoexec1", new_cmd); - if(err!=ESP_OK){ - nerrors++; - fprintf(f,"Error updating squeezelite command line options . %s\n", esp_err_to_name(err)); - } - } - free(squeezelite_cmd); - free(new_cmd); - } - - } - if(bt_source_args.pin_code->count >0){ - const char * v=bt_source_args.pin_code->sval[0]; - bool bInvalid=false; - for(int i=0;i'9'){ - bInvalid=true; - } - } - if(bInvalid || strlen(bt_source_args.pin_code->sval[0])>16 || strlen(bt_source_args.pin_code->sval[0])<4){ + char *squeezelite_cmd = config_alloc_get_default(NVS_TYPE_STR, "autoexec1", NULL, 0); + if (squeezelite_cmd && strstr(squeezelite_cmd, " -o ")) { + char *new_cmd = strip_bt_name(squeezelite_cmd); + if (strcmp(new_cmd, squeezelite_cmd) != 0) { + fprintf(f, "Replacing old squeezelite command [%s] with [%s].\n", squeezelite_cmd, new_cmd); + config_set_value(NVS_TYPE_STR, "autoexec1", new_cmd); + if (err != ESP_OK) { + nerrors++; + fprintf(f, "Error updating squeezelite command line options . %s\n", esp_err_to_name(err)); + } + } + free(squeezelite_cmd); + free(new_cmd); + } + } + if (bt_source_args.pin_code->count > 0) { + const char *v = bt_source_args.pin_code->sval[0]; + bool bInvalid = false; + for (int i = 0; i < strlen(v) && !bInvalid; i++) { + if (v[i] < '0' || v[i] > '9') { + bInvalid = true; + } + } + if (bInvalid || strlen(bt_source_args.pin_code->sval[0]) > 16 || strlen(bt_source_args.pin_code->sval[0]) < 4) { nerrors++; - fprintf(f,"Pin code %s invalid. Should be numbers only with length between 4 and 16 characters. \n",bt_source_args.pin_code->sval[0]); - } - else { - err = config_set_value(NVS_TYPE_STR, "a2dp_spin", bt_source_args.pin_code->sval[0]); - if(err!=ESP_OK){ - nerrors++; - fprintf(f,"Error setting Bluetooth source pin to %s. %s\n",bt_source_args.pin_code->sval[0], esp_err_to_name(err)); - } - else { - fprintf(f,"Bluetooth source pin changed to %s\n",bt_source_args.pin_code->sval[0]); - } - } - } - // if(bt_source_args.connect_timeout_delay->count >0){ - - // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.connect_timeout_delay->dval[0]*1000.0)); - // if(bt_source_args.connect_timeout_delay->dval[0] <0.5 || bt_source_args.connect_timeout_delay->dval[0] >5.0){ - // nerrors++; - // fprintf(f,"Invalid connection timeout %0.0f (%s milliseconds). Value must be between 0.5 sec and 5 sec.\n", bt_source_args.connect_timeout_delay->dval[0], value ); - // } - // else { - // err = config_set_value(NVS_TYPE_STR, "a2dp_ctmt", value); - // if(err!=ESP_OK){ - // nerrors++; - // fprintf(f,"Error setting connection timeout %0.0f sec (%s milliseconds). %s\n", bt_source_args.connect_timeout_delay->dval[0],value, esp_err_to_name(err)); - // } - // else { - // fprintf(f,"Connection timeout changed to %0.0f sec (%s milliseconds)\n",bt_source_args.connect_timeout_delay->dval[0],value); - // } - // } - // } + fprintf(f, "Pin code %s invalid. Should be numbers only with length between 4 and 16 characters. \n", bt_source_args.pin_code->sval[0]); + } else { + err = config_set_value(NVS_TYPE_STR, "a2dp_spin", bt_source_args.pin_code->sval[0]); + if (err != ESP_OK) { + nerrors++; + fprintf(f, "Error setting Bluetooth source pin to %s. %s\n", bt_source_args.pin_code->sval[0], esp_err_to_name(err)); + } else { + fprintf(f, "Bluetooth source pin changed to %s\n", bt_source_args.pin_code->sval[0]); + } + } + } + // if(bt_source_args.connect_timeout_delay->count >0){ - // if(bt_source_args.control_delay->count >0){ - // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.control_delay->dval[0]*1000.0)); - // if(bt_source_args.control_delay->dval[0] <0.1 || bt_source_args.control_delay->dval[0] >2.0){ - // nerrors++; - // fprintf(f,"Invalid control delay %0.0f (%s milliseconds). Value must be between 0.1s and 2s.\n", bt_source_args.control_delay->dval[0], value ); - // } - // else { - // err = config_set_value(NVS_TYPE_STR, "a2dp_ctrld", value); - // if(err!=ESP_OK){ - // nerrors++; - // fprintf(f,"Error setting control delay to %0.0f sec (%s milliseconds). %s\n",bt_source_args.control_delay->dval[0],value, esp_err_to_name(err)); - // } - // else { - // fprintf(f,"Control delay changed to %0.0f sec (%s milliseconds)\n",bt_source_args.control_delay->dval[0],value); - // } - // } - // } + // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.connect_timeout_delay->dval[0]*1000.0)); + // if(bt_source_args.connect_timeout_delay->dval[0] <0.5 || bt_source_args.connect_timeout_delay->dval[0] >5.0){ + // nerrors++; + // fprintf(f,"Invalid connection timeout %0.0f (%s milliseconds). Value must be between 0.5 sec and 5 sec.\n", bt_source_args.connect_timeout_delay->dval[0], value ); + // } + // else { + // err = config_set_value(NVS_TYPE_STR, "a2dp_ctmt", value); + // if(err!=ESP_OK){ + // nerrors++; + // fprintf(f,"Error setting connection timeout %0.0f sec (%s milliseconds). %s\n", bt_source_args.connect_timeout_delay->dval[0],value, esp_err_to_name(err)); + // } + // else { + // fprintf(f,"Connection timeout changed to %0.0f sec (%s milliseconds)\n",bt_source_args.connect_timeout_delay->dval[0],value); + // } + // } + // } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + // if(bt_source_args.control_delay->count >0){ + // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.control_delay->dval[0]*1000.0)); + // if(bt_source_args.control_delay->dval[0] <0.1 || bt_source_args.control_delay->dval[0] >2.0){ + // nerrors++; + // fprintf(f,"Invalid control delay %0.0f (%s milliseconds). Value must be between 0.1s and 2s.\n", bt_source_args.control_delay->dval[0], value ); + // } + // else { + // err = config_set_value(NVS_TYPE_STR, "a2dp_ctrld", value); + // if(err!=ESP_OK){ + // nerrors++; + // fprintf(f,"Error setting control delay to %0.0f sec (%s milliseconds). %s\n",bt_source_args.control_delay->dval[0],value, esp_err_to_name(err)); + // } + // else { + // fprintf(f,"Control delay changed to %0.0f sec (%s milliseconds)\n",bt_source_args.control_delay->dval[0],value); + // } + // } + // } + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -static int do_audio_cmd(int argc, char **argv){ - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&audio_args); - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - arg_print_errors(f,audio_args.end,desc_audio); - fclose(f); - return 1; - } - +static int do_audio_cmd(int argc, char **argv) { + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&audio_args); + char *buf = NULL; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + arg_print_errors(f, audio_args.end, desc_audio); + fclose(f); + return 1; + } + err = ESP_OK; // suppress any error code that might have happened in a previous step - if(audio_args.loudness->count>0){ - char p[4]={0}; - int loudness_val = audio_args.loudness->ival[0]; - if( loudness_val < 0 || loudness_val>10){ - nerrors++; - fprintf(f,"Invalid loudness value %d. Valid values are between 0 and 10.\n",loudness_val); - } + if (audio_args.loudness->count > 0) { + char p[4] = {0}; + int loudness_val = audio_args.loudness->ival[0]; + if (loudness_val < 0 || loudness_val > 10) { + nerrors++; + fprintf(f, "Invalid loudness value %d. Valid values are between 0 and 10.\n", loudness_val); + } // it's not necessary to store loudness in NVS as set_loudness does it, but it does not hurt - else { - itoa(loudness_val,p,10); - err = config_set_value(NVS_TYPE_STR, "loudness", p); - } - if(err!=ESP_OK){ - nerrors++; - fprintf(f,"Error setting Loudness value %s. %s\n",p, esp_err_to_name(err)); - } else { - fprintf(f,"Loudness changed to %s\n",p); - equalizer_set_loudness(loudness_val); - } + itoa(loudness_val, p, 10); + err = config_set_value(NVS_TYPE_STR, "loudness", p); + } + if (err != ESP_OK) { + nerrors++; + fprintf(f, "Error setting Loudness value %s. %s\n", p, esp_err_to_name(err)); + } else { + fprintf(f, "Loudness changed to %s\n", p); + equalizer_set_loudness(loudness_val); + } } - if(audio_args.jack_behavior->count>0){ + if (audio_args.jack_behavior->count > 0) { err = ESP_OK; // suppress any error code that might have happened in a previous step - if(strcasecmp(audio_args.jack_behavior->sval[0],"Headphones")==0){ + if (strcasecmp(audio_args.jack_behavior->sval[0], "Headphones") == 0) { err = config_set_value(NVS_TYPE_STR, "jack_mutes_amp", "y"); - } - else if(strcasecmp(audio_args.jack_behavior->sval[0],"Subwoofer")==0){ + } else if (strcasecmp(audio_args.jack_behavior->sval[0], "Subwoofer") == 0) { err = config_set_value(NVS_TYPE_STR, "jack_mutes_amp", "n"); - } - else { + } else { nerrors++; - fprintf(f,"Unknown Audio Jack Behavior %s.\n",audio_args.jack_behavior->sval[0]); + fprintf(f, "Unknown Audio Jack Behavior %s.\n", audio_args.jack_behavior->sval[0]); } - if(err!=ESP_OK){ + if (err != ESP_OK) { nerrors++; - fprintf(f,"Error setting Audio Jack Behavior %s. %s\n",audio_args.jack_behavior->sval[0], esp_err_to_name(err)); + fprintf(f, "Error setting Audio Jack Behavior %s. %s\n", audio_args.jack_behavior->sval[0], esp_err_to_name(err)); + } else { + fprintf(f, "Audio Jack Behavior changed to %s\n", audio_args.jack_behavior->sval[0]); } - else { - fprintf(f,"Audio Jack Behavior changed to %s\n",audio_args.jack_behavior->sval[0]); - } } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -static int do_spdif_cmd(int argc, char **argv){ - i2s_platform_config_t i2s_dac_pin = { - .i2c_addr = -1, - .sda= -1, - .scl = -1, - .mute_gpio = -1, - .mute_level = -1 - }; - if(is_spdif_config_locked()){ - cmd_send_messaging(argv[0],MESSAGING_ERROR,"SPDIF Configuration is locked on this platform\n"); - return 1; - } - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&spdif_args); - if (spdif_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"SPDIF config cleared\n"); - config_set_value(NVS_TYPE_STR, "spdif_config", ""); - return 0; - } +static int do_spdif_cmd(int argc, char **argv) { + i2s_platform_config_t i2s_dac_pin = { + .i2c_addr = -1, + .sda = -1, + .scl = -1, + .mute_gpio = -1, + .mute_level = -1}; + if (is_spdif_config_locked()) { + cmd_send_messaging(argv[0], MESSAGING_ERROR, "SPDIF Configuration is locked on this platform\n"); + return 1; + } + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&spdif_args); + if (spdif_args.clear->count) { + cmd_send_messaging(argv[0], MESSAGING_WARNING, "SPDIF config cleared\n"); + config_set_value(NVS_TYPE_STR, "spdif_config", ""); + return 0; + } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - arg_print_errors(f,spdif_args.end,desc_dac); - fclose(f); - return 1; - } - nerrors+=is_output_gpio(spdif_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true); - nerrors+=is_output_gpio(spdif_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true); - nerrors+=is_output_gpio(spdif_args.data, f, &i2s_dac_pin.pin.data_out_num, true); - if(!nerrors ){ - fprintf(f,"Storing SPDIF parameters.\n"); - nerrors+=(config_spdif_set(&i2s_dac_pin )!=ESP_OK); - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + char *buf = NULL; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + arg_print_errors(f, spdif_args.end, desc_dac); + fclose(f); + return 1; + } + nerrors += is_output_gpio(spdif_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true); + nerrors += is_output_gpio(spdif_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true); + nerrors += is_output_gpio(spdif_args.data, f, &i2s_dac_pin.pin.data_out_num, true); + if (!nerrors) { + fprintf(f, "Storing SPDIF parameters.\n"); + nerrors += (config_spdif_set(&i2s_dac_pin) != ESP_OK); + } + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -static int do_rotary_cmd(int argc, char **argv){ - rotary_struct_t rotary={ .A = -1, .B = -1, .SW = -1, .longpress = 0, .knobonly=0,.volume_lock=false}; - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&rotary_args); - if (rotary_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"rotary config cleared\n"); - config_set_value(NVS_TYPE_STR, "rotary_config", ""); - return 0; - } +static int do_rotary_cmd(int argc, char **argv) { + rotary_struct_t rotary = {.A = -1, .B = -1, .SW = -1, .longpress = 0, .knobonly = 0, .volume_lock = false}; + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&rotary_args); + if (rotary_args.clear->count) { + cmd_send_messaging(argv[0], MESSAGING_WARNING, "rotary config cleared\n"); + config_set_value(NVS_TYPE_STR, "rotary_config", ""); + return 0; + } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - arg_print_errors(f,rotary_args.end,desc_rotary); - fclose(f); - return 1; - } - nerrors+=is_gpio(rotary_args.A, f, &rotary.A, true,false); - nerrors+=is_gpio(rotary_args.B, f, &rotary.B, true,false); - nerrors+=is_gpio(rotary_args.SW, f, &rotary.SW,false,false); + char *buf = NULL; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + arg_print_errors(f, rotary_args.end, desc_rotary); + fclose(f); + return 1; + } + nerrors += is_gpio(rotary_args.A, f, &rotary.A, true, false); + nerrors += is_gpio(rotary_args.B, f, &rotary.B, true, false); + nerrors += is_gpio(rotary_args.SW, f, &rotary.SW, false, false); + if (rotary_args.knobonly->count > 0 && (rotary_args.volume_lock->count > 0 || rotary_args.longpress->count > 0)) { + fprintf(f, "error: Cannot use volume lock or longpress option when knob only option selected\n"); + nerrors++; + } - if(rotary_args.knobonly->count>0 && (rotary_args.volume_lock->count>0 || rotary_args.longpress->count>0)){ - fprintf(f,"error: Cannot use volume lock or longpress option when knob only option selected\n"); - nerrors++; - } - - if(rotary_args.timer->count>0 && rotary_args.timer->ival[0]<0){ - fprintf(f,"error: knob only timer should be greater than or equal to zero.\n"); - nerrors++; - } - else { - rotary.timer = rotary_args.timer->count>0?rotary_args.timer->ival[0]:0; - } - rotary.knobonly = rotary_args.knobonly->count>0; - rotary.volume_lock= rotary_args.volume_lock->count>0; - rotary.longpress = rotary_args.longpress->count>0; - if(!nerrors ){ - fprintf(f,"Storing rotary parameters.\n"); - nerrors+=(config_rotary_set(&rotary )!=ESP_OK); - } - if(!nerrors ){ - fprintf(f,"Storing raw mode parameter.\n"); - nerrors+=(config_set_value(NVS_TYPE_STR, "lms_ctrls_raw", rotary_args.raw_mode->count>0?"Y":"N")!=ESP_OK); - if(nerrors>0){ - fprintf(f,"error: Unable to store raw mode parameter.\n"); - } - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + if (rotary_args.timer->count > 0 && rotary_args.timer->ival[0] < 0) { + fprintf(f, "error: knob only timer should be greater than or equal to zero.\n"); + nerrors++; + } else { + rotary.timer = rotary_args.timer->count > 0 ? rotary_args.timer->ival[0] : 0; + } + rotary.knobonly = rotary_args.knobonly->count > 0; + rotary.volume_lock = rotary_args.volume_lock->count > 0; + rotary.longpress = rotary_args.longpress->count > 0; + if (!nerrors) { + fprintf(f, "Storing rotary parameters.\n"); + nerrors += (config_rotary_set(&rotary) != ESP_OK); + } + if (!nerrors) { + fprintf(f, "Storing raw mode parameter.\n"); + nerrors += (config_set_value(NVS_TYPE_STR, "lms_ctrls_raw", rotary_args.raw_mode->count > 0 ? "Y" : "N") != ESP_OK); + if (nerrors > 0) { + fprintf(f, "error: Unable to store raw mode parameter.\n"); + } + } + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -static int is_valid_gpio_number(int gpio, const char * name, FILE *f, bool mandatory, struct arg_int * target, bool output){ - bool invalid = (!GPIO_IS_VALID_GPIO(gpio) ||(output && !GPIO_IS_VALID_OUTPUT_GPIO(gpio))) ; - if(invalid && mandatory && gpio!=-1){ - fprintf(f,"Error: Invalid mandatory gpio %d for %s\n",gpio,name); - return 1; - } - if(target && !invalid){ - target->count=1; - target->ival[0]=gpio; - } - return 0; +static int is_valid_gpio_number(int gpio, const char *name, FILE *f, bool mandatory, struct arg_int *target, bool output) { + bool invalid = (!GPIO_IS_VALID_GPIO(gpio) || (output && !GPIO_IS_VALID_OUTPUT_GPIO(gpio))); + if (invalid && mandatory && gpio != -1) { + fprintf(f, "Error: Invalid mandatory gpio %d for %s\n", gpio, name); + return 1; + } + if (target && !invalid) { + target->count = 1; + target->ival[0] = gpio; + } + return 0; } #ifdef CONFIG_CSPOT_SINK -static int do_cspot_config(int argc, char **argv){ - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&cspot_args); +static int do_cspot_config(int argc, char **argv) { + int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr **)&cspot_args); if (nerrors != 0) { return 1; } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream."); - return 1; - } + char *buf = NULL; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } - cJSON * cspot_config = config_alloc_get_cjson("cspot_config"); - if(!cspot_config){ - nerrors++; - fprintf(f,"error: Unable to get default cspot config.\n"); - } - if(cspot_args.deviceName->count>0){ - cjson_update_string(&cspot_config,cspot_args.deviceName->hdr.longopts,cspot_args.deviceName->sval[0]); - } - if(cspot_args.bitrate->count>0){ - cjson_update_number(&cspot_config,cspot_args.bitrate->hdr.longopts,cspot_args.bitrate->ival[0]); - } - if(cspot_args.zeroConf->count>0){ - cjson_update_number(&cspot_config,cspot_args.zeroConf->hdr.longopts,cspot_args.zeroConf->ival[0]); - } - - if(!nerrors ){ - fprintf(f,"Storing cspot parameters.\n"); - nerrors+=(config_set_cjson_str_and_free("cspot_config",cspot_config) !=ESP_OK); - } - if(nerrors==0 ){ - if(cspot_args.deviceName->count>0){ - fprintf(f,"Device name changed to %s\n",cspot_args.deviceName->sval[0]); - } - if(cspot_args.bitrate->count>0){ - fprintf(f,"Bitrate changed to %u\n",cspot_args.bitrate->ival[0]); - } - if(cspot_args.zeroConf->count>0){ - fprintf(f,"ZeroConf changed to %u\n",cspot_args.zeroConf->ival[0]); - } - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return nerrors; + cJSON *cspot_config = config_alloc_get_cjson("cspot_config"); + if (!cspot_config) { + nerrors++; + fprintf(f, "error: Unable to get default cspot config.\n"); + } + if (cspot_args.deviceName->count > 0) { + cjson_update_string(&cspot_config, cspot_args.deviceName->hdr.longopts, cspot_args.deviceName->sval[0]); + } + if (cspot_args.bitrate->count > 0) { + cjson_update_number(&cspot_config, cspot_args.bitrate->hdr.longopts, cspot_args.bitrate->ival[0]); + } + if (cspot_args.zeroConf->count > 0) { + cjson_update_number(&cspot_config, cspot_args.zeroConf->hdr.longopts, cspot_args.zeroConf->ival[0]); + } + + if (!nerrors) { + fprintf(f, "Storing cspot parameters.\n"); + nerrors += (config_set_cjson_str_and_free("cspot_config", cspot_config) != ESP_OK); + } + if (nerrors == 0) { + if (cspot_args.deviceName->count > 0) { + fprintf(f, "Device name changed to %s\n", cspot_args.deviceName->sval[0]); + } + if (cspot_args.bitrate->count > 0) { + fprintf(f, "Bitrate changed to %u\n", cspot_args.bitrate->ival[0]); + } + if (cspot_args.zeroConf->count > 0) { + fprintf(f, "ZeroConf changed to %u\n", cspot_args.zeroConf->ival[0]); + } + } + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return nerrors; } #endif -static int do_ledvu_cmd(int argc, char **argv){ - ledvu_struct_t ledvu={ .type = "WS2812", .gpio = -1, .length = 0}; - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&ledvu_args); - if (ledvu_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"ledvu config cleared\n"); - config_set_value(NVS_TYPE_STR, "led_vu_config", ""); - return 0; - } +static int do_ledvu_cmd(int argc, char **argv) { + ledvu_struct_t ledvu = {.type = "WS2812", .gpio = -1, .length = 0}; + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&ledvu_args); + if (ledvu_args.clear->count) { + cmd_send_messaging(argv[0], MESSAGING_WARNING, "ledvu config cleared\n"); + config_set_value(NVS_TYPE_STR, "led_vu_config", ""); + return 0; + } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - arg_print_errors(f,ledvu_args.end,desc_ledvu); - return 1; - } + char *buf = NULL; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + arg_print_errors(f, ledvu_args.end, desc_ledvu); + return 1; + } - nerrors+=is_output_gpio(ledvu_args.gpio, f, &ledvu.gpio, true); - - if(ledvu_args.length->count==0 || ledvu_args.length->ival[0]<1 || ledvu_args.length->ival[0]>255){ - fprintf(f,"error: strip length must be greater than 0 and no more than 255\n"); - nerrors++; - } - else { - ledvu.length = ledvu_args.length->count>0?ledvu_args.length->ival[0]:0; - } - - if(!nerrors ){ - fprintf(f,"Storing ledvu parameters.\n"); - nerrors+=(config_ledvu_set(&ledvu )!=ESP_OK); - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + nerrors += is_output_gpio(ledvu_args.gpio, f, &ledvu.gpio, true); + + if (ledvu_args.length->count == 0 || ledvu_args.length->ival[0] < 1 || ledvu_args.length->ival[0] > 255) { + fprintf(f, "error: strip length must be greater than 0 and no more than 255\n"); + nerrors++; + } else { + ledvu.length = ledvu_args.length->count > 0 ? ledvu_args.length->ival[0] : 0; + } + + if (!nerrors) { + fprintf(f, "Storing ledvu parameters.\n"); + nerrors += (config_ledvu_set(&ledvu) != ESP_OK); + } + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -static int do_i2s_cmd(int argc, char **argv) -{ - i2s_platform_config_t i2s_dac_pin = { - .i2c_addr = -1, - .sda= -1, - .scl = -1, - .mute_gpio = -1, - .mute_level = -1 - }; - if(is_dac_config_locked()){ - cmd_send_messaging(argv[0],MESSAGING_ERROR,"DAC Configuration is locked on this platform\n"); - return 1; - } +static int do_i2s_cmd(int argc, char **argv) { + i2s_platform_config_t i2s_dac_pin = { + .i2c_addr = -1, + .sda = -1, + .scl = -1, + .mute_gpio = -1, + .mute_level = -1}; + if (is_dac_config_locked()) { + cmd_send_messaging(argv[0], MESSAGING_ERROR, "DAC Configuration is locked on this platform\n"); + return 1; + } - ESP_LOGD(TAG,"Processing i2s command %s with %d parameters",argv[0],argc); + ESP_LOGD(TAG, "Processing i2s command %s with %d parameters", argv[0], argc); - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&i2s_args); - if (i2s_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"DAC config cleared\n"); - config_set_value(NVS_TYPE_STR, "dac_config", ""); - return 0; - } + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&i2s_args); + if (i2s_args.clear->count) { + cmd_send_messaging(argv[0], MESSAGING_WARNING, "DAC config cleared\n"); + config_set_value(NVS_TYPE_STR, "dac_config", ""); + return 0; + } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - ESP_LOGE(TAG, "do_i2s_cmd: Failed to open memstream"); - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - ESP_LOGE(TAG,"do_i2s_cmd: %d errors parsing arguments",nerrors); - arg_print_errors(f,i2s_args.end,desc_dac); - } - else { - strncpy(i2s_dac_pin.model,i2s_args.model_name->sval[0],sizeof(i2s_dac_pin.model)); - i2s_dac_pin.model[sizeof(i2s_dac_pin.model) - 1] = '\0'; - nerrors += is_output_gpio(i2s_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true); - nerrors += is_output_gpio(i2s_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true); - nerrors += is_output_gpio(i2s_args.data, f, &i2s_dac_pin.pin.data_out_num, true); - nerrors += is_output_gpio(i2s_args.mute_gpio, f, &i2s_dac_pin.mute_gpio, false); - if (i2s_dac_pin.mute_gpio >= 0) { - i2s_dac_pin.mute_level = i2s_args.mute_level->count > 0 ? 1 : 0; - } - if (i2s_args.dac_sda->count > 0 && i2s_args.dac_sda->ival[0] >= 0) { - // if SDA specified, then SDA and SCL are both mandatory - nerrors += is_output_gpio(i2s_args.dac_sda, f, &i2s_dac_pin.sda, false); - nerrors += is_output_gpio(i2s_args.dac_scl, f, &i2s_dac_pin.scl, false); - } - if (i2s_args.dac_sda->count == 0 && i2s_args.dac_i2c->count > 0) { - fprintf(f, "warning: ignoring i2c address, since dac i2c gpios config is incomplete\n"); - } else if (i2s_args.dac_i2c->count > 0) { - i2s_dac_pin.i2c_addr = i2s_args.dac_i2c->ival[0]; - } + char *buf = NULL; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + ESP_LOGE(TAG, "do_i2s_cmd: %d errors parsing arguments", nerrors); + arg_print_errors(f, i2s_args.end, desc_dac); + } else { + strncpy(i2s_dac_pin.model, i2s_args.model_name->sval[0], sizeof(i2s_dac_pin.model)); + i2s_dac_pin.model[sizeof(i2s_dac_pin.model) - 1] = '\0'; + nerrors += is_output_gpio(i2s_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true); + nerrors += is_output_gpio(i2s_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true); + nerrors += is_output_gpio(i2s_args.data, f, &i2s_dac_pin.pin.data_out_num, true); + nerrors += is_output_gpio(i2s_args.mute_gpio, f, &i2s_dac_pin.mute_gpio, false); + if (i2s_dac_pin.mute_gpio >= 0) { + i2s_dac_pin.mute_level = i2s_args.mute_level->count > 0 ? 1 : 0; + } + if (i2s_args.dac_sda->count > 0 && i2s_args.dac_sda->ival[0] >= 0) { + // if SDA specified, then SDA and SCL are both mandatory + nerrors += is_output_gpio(i2s_args.dac_sda, f, &i2s_dac_pin.sda, false); + nerrors += is_output_gpio(i2s_args.dac_scl, f, &i2s_dac_pin.scl, false); + } + if (i2s_args.dac_sda->count == 0 && i2s_args.dac_i2c->count > 0) { + fprintf(f, "warning: ignoring i2c address, since dac i2c gpios config is incomplete\n"); + } else if (i2s_args.dac_i2c->count > 0) { + i2s_dac_pin.i2c_addr = i2s_args.dac_i2c->ival[0]; + } - if (!nerrors) { - fprintf(f, "Storing i2s parameters.\n"); - nerrors += (config_i2s_set(&i2s_dac_pin, "dac_config") != ESP_OK); - } - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); + if (!nerrors) { + fprintf(f, "Storing i2s parameters.\n"); + nerrors += (config_i2s_set(&i2s_dac_pin, "dac_config") != ESP_OK); + } + } + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -cJSON * example_cb(){ - cJSON * values = cJSON_CreateObject(); -// int i2c_port; -// const i2c_config_t * i2c= config_i2c_get(&i2c_port); -// if(i2c->scl_io_num>0) { -// cJSON_AddNumberToObject(values,"scl",i2c->scl_io_num); -// } -// if(i2c->sda_io_num>0) { -// cJSON_AddNumberToObject(values,"sda",i2c->sda_io_num); -// } -// if(i2c->master.clk_speed>0) { -// cJSON_AddNumberToObject(values,"freq",i2c->master.clk_speed); -// } -// if(i2c_port>0) { -// cJSON_AddNumberToObject(values,"port",i2c_port); -// } - return values; +cJSON *example_cb() { + cJSON *values = cJSON_CreateObject(); + // int i2c_port; + // const i2c_config_t * i2c= config_i2c_get(&i2c_port); + // if(i2c->scl_io_num>0) { + // cJSON_AddNumberToObject(values,"scl",i2c->scl_io_num); + // } + // if(i2c->sda_io_num>0) { + // cJSON_AddNumberToObject(values,"sda",i2c->sda_io_num); + // } + // if(i2c->master.clk_speed>0) { + // cJSON_AddNumberToObject(values,"freq",i2c->master.clk_speed); + // } + // if(i2c_port>0) { + // cJSON_AddNumberToObject(values,"port",i2c_port); + // } + return values; } -cJSON * known_model_cb(){ - cJSON * values = cJSON_CreateObject(); - if(!values){ - ESP_LOGE(TAG,"known_model_cb: Failed to create JSON object"); - return NULL; - } - char * name = config_alloc_get_default(NVS_TYPE_STR,known_model_args.model_config->hdr.longopts,"",0); - if(!name){ - ESP_LOGE(TAG,"Failed to get board model from nvs key %s ",known_model_args.model_config->hdr.longopts); - } - else { - cJSON_AddStringToObject(values,known_model_args.model_config->hdr.longopts,name); - } - return values; +cJSON *known_model_cb() { + cJSON *values = cJSON_CreateObject(); + if (!values) { + ESP_LOGE(TAG, "known_model_cb: Failed to create JSON object"); + return NULL; + } + char *name = config_alloc_get_default(NVS_TYPE_STR, known_model_args.model_config->hdr.longopts, "", 0); + if (!name) { + ESP_LOGE(TAG, "Failed to get board model from nvs key %s ", known_model_args.model_config->hdr.longopts); + } else { + cJSON_AddStringToObject(values, known_model_args.model_config->hdr.longopts, name); + } + return values; } #ifdef CONFIG_CSPOT_SINK -cJSON * cspot_cb(){ - cJSON * values = cJSON_CreateObject(); - if(!values){ - ESP_LOGE(TAG,"cspot_cb: Failed to create JSON object"); - return NULL; - } - cJSON * cspot_config = config_alloc_get_cjson("cspot_config"); - if(!cspot_config){ - ESP_LOGE(TAG,"cspot_cb: Failed to get cspot config"); - return NULL; - } - cJSON * cspot_values = cJSON_GetObjectItem(cspot_config,cspot_args.deviceName->hdr.longopts); - if(cspot_values){ - cJSON_AddStringToObject(values,cspot_args.deviceName->hdr.longopts,cJSON_GetStringValue(cspot_values)); - } - cspot_values = cJSON_GetObjectItem(cspot_config,cspot_args.bitrate->hdr.longopts); - if(cspot_values){ - cJSON_AddNumberToObject(values,cspot_args.bitrate->hdr.longopts,cJSON_GetNumberValue(cspot_values)); - } - cspot_values = cJSON_GetObjectItem(cspot_config,cspot_args.zeroConf->hdr.longopts); - if(cspot_values){ - cJSON_AddNumberToObject(values,cspot_args.zeroConf->hdr.longopts,cJSON_GetNumberValue(cspot_values)); - } +cJSON *cspot_cb() { + cJSON *values = cJSON_CreateObject(); + if (!values) { + ESP_LOGE(TAG, "cspot_cb: Failed to create JSON object"); + return NULL; + } + cJSON *cspot_config = config_alloc_get_cjson("cspot_config"); + if (!cspot_config) { + ESP_LOGE(TAG, "cspot_cb: Failed to get cspot config"); + return NULL; + } + cJSON *cspot_values = cJSON_GetObjectItem(cspot_config, cspot_args.deviceName->hdr.longopts); + if (cspot_values) { + cJSON_AddStringToObject(values, cspot_args.deviceName->hdr.longopts, cJSON_GetStringValue(cspot_values)); + } + cspot_values = cJSON_GetObjectItem(cspot_config, cspot_args.bitrate->hdr.longopts); + if (cspot_values) { + cJSON_AddNumberToObject(values, cspot_args.bitrate->hdr.longopts, cJSON_GetNumberValue(cspot_values)); + } + cspot_values = cJSON_GetObjectItem(cspot_config, cspot_args.zeroConf->hdr.longopts); + if (cspot_values) { + cJSON_AddNumberToObject(values, cspot_args.zeroConf->hdr.longopts, cJSON_GetNumberValue(cspot_values)); + } - cJSON_Delete(cspot_config); - return values; + cJSON_Delete(cspot_config); + return values; } #endif -cJSON * i2s_cb(){ - cJSON * values = cJSON_CreateObject(); +cJSON *i2s_cb() { + cJSON *values = cJSON_CreateObject(); - const i2s_platform_config_t * i2s_conf= config_dac_get( ); - - if(i2s_conf->pin.bck_io_num>0 ) { - cJSON_AddNumberToObject(values,i2s_args.clock->hdr.longopts,i2s_conf->pin.bck_io_num); - } - if(i2s_conf->pin.ws_io_num>=0 ) { - cJSON_AddNumberToObject(values,i2s_args.wordselect->hdr.longopts,i2s_conf->pin.ws_io_num); - } - if(i2s_conf->pin.data_out_num>=0 ) { - cJSON_AddNumberToObject(values,i2s_args.data->hdr.longopts,i2s_conf->pin.data_out_num); - } - if(i2s_conf->sda>=0 ) { - cJSON_AddNumberToObject(values,i2s_args.dac_sda->hdr.longopts,i2s_conf->sda); - } - if(i2s_conf->scl>=0 ) { - cJSON_AddNumberToObject(values,i2s_args.dac_scl->hdr.longopts,i2s_conf->scl); - } - if(i2s_conf->i2c_addr>=0 ) { - cJSON_AddNumberToObject(values,i2s_args.dac_i2c->hdr.longopts,i2s_conf->i2c_addr); - } - if(i2s_conf->mute_gpio>=0 ) { - cJSON_AddNumberToObject(values,i2s_args.mute_gpio->hdr.longopts,i2s_conf->mute_gpio); - } - if(i2s_conf->mute_level>=0 ) { - cJSON_AddBoolToObject(values,i2s_args.mute_level->hdr.longopts,i2s_conf->mute_level>0); - } - if(strlen(i2s_conf->model)>0){ - cJSON_AddStringToObject(values,i2s_args.model_name->hdr.longopts,i2s_conf->model); - } - else { - cJSON_AddStringToObject(values,i2s_args.model_name->hdr.longopts,"I2S"); - } - - return values; + const i2s_platform_config_t *i2s_conf = config_dac_get(); + if (i2s_conf->pin.bck_io_num > 0) { + cJSON_AddNumberToObject(values, i2s_args.clock->hdr.longopts, i2s_conf->pin.bck_io_num); + } + if (i2s_conf->pin.ws_io_num >= 0) { + cJSON_AddNumberToObject(values, i2s_args.wordselect->hdr.longopts, i2s_conf->pin.ws_io_num); + } + if (i2s_conf->pin.data_out_num >= 0) { + cJSON_AddNumberToObject(values, i2s_args.data->hdr.longopts, i2s_conf->pin.data_out_num); + } + if (i2s_conf->sda >= 0) { + cJSON_AddNumberToObject(values, i2s_args.dac_sda->hdr.longopts, i2s_conf->sda); + } + if (i2s_conf->scl >= 0) { + cJSON_AddNumberToObject(values, i2s_args.dac_scl->hdr.longopts, i2s_conf->scl); + } + if (i2s_conf->i2c_addr >= 0) { + cJSON_AddNumberToObject(values, i2s_args.dac_i2c->hdr.longopts, i2s_conf->i2c_addr); + } + if (i2s_conf->mute_gpio >= 0) { + cJSON_AddNumberToObject(values, i2s_args.mute_gpio->hdr.longopts, i2s_conf->mute_gpio); + } + if (i2s_conf->mute_level >= 0) { + cJSON_AddBoolToObject(values, i2s_args.mute_level->hdr.longopts, i2s_conf->mute_level > 0); + } + if (strlen(i2s_conf->model) > 0) { + cJSON_AddStringToObject(values, i2s_args.model_name->hdr.longopts, i2s_conf->model); + } else { + cJSON_AddStringToObject(values, i2s_args.model_name->hdr.longopts, "I2S"); + } + + return values; } -cJSON * spdif_cb(){ - cJSON * values = cJSON_CreateObject(); - const i2s_platform_config_t * spdif_conf= config_spdif_get( ); - if(spdif_conf->pin.bck_io_num>0 ) { - cJSON_AddNumberToObject(values,"clock",spdif_conf->pin.bck_io_num); - } - if(spdif_conf->pin.ws_io_num>=0 ) { - cJSON_AddNumberToObject(values,"wordselect",spdif_conf->pin.ws_io_num); - } - if(spdif_conf->pin.data_out_num>=0 ) { - cJSON_AddNumberToObject(values,"data",spdif_conf->pin.data_out_num); - } - - return values; +cJSON *spdif_cb() { + cJSON *values = cJSON_CreateObject(); + const i2s_platform_config_t *spdif_conf = config_spdif_get(); + if (spdif_conf->pin.bck_io_num > 0) { + cJSON_AddNumberToObject(values, "clock", spdif_conf->pin.bck_io_num); + } + if (spdif_conf->pin.ws_io_num >= 0) { + cJSON_AddNumberToObject(values, "wordselect", spdif_conf->pin.ws_io_num); + } + if (spdif_conf->pin.data_out_num >= 0) { + cJSON_AddNumberToObject(values, "data", spdif_conf->pin.data_out_num); + } + + return values; } -cJSON * rotary_cb(){ - cJSON * values = cJSON_CreateObject(); - char *p = config_alloc_get_default(NVS_TYPE_STR, "lms_ctrls_raw", "n", 0); - bool raw_mode = p && (*p == '1' || *p == 'Y' || *p == 'y'); - free(p); - const rotary_struct_t *rotary= config_rotary_get(); - - if(GPIO_IS_VALID_GPIO(rotary->A ) && rotary->A>=0 && GPIO_IS_VALID_GPIO(rotary->B) && rotary->B>=0){ - cJSON_AddNumberToObject(values,rotary_args.A->hdr.longopts,rotary->A); - cJSON_AddNumberToObject(values,rotary_args.B->hdr.longopts,rotary->B); - if(GPIO_IS_VALID_GPIO(rotary->SW ) && rotary->SW>=0 ){ - cJSON_AddNumberToObject(values,rotary_args.SW->hdr.longopts,rotary->SW); - } - cJSON_AddBoolToObject(values,rotary_args.volume_lock->hdr.longopts,rotary->volume_lock); - cJSON_AddBoolToObject(values,rotary_args.longpress->hdr.longopts,rotary->longpress); - cJSON_AddBoolToObject(values,rotary_args.knobonly->hdr.longopts,rotary->knobonly); - cJSON_AddNumberToObject(values,rotary_args.timer->hdr.longopts,rotary->timer); - cJSON_AddNumberToObject(values,rotary_args.raw_mode->hdr.longopts,raw_mode); - } - return values; +cJSON *rotary_cb() { + cJSON *values = cJSON_CreateObject(); + char *p = config_alloc_get_default(NVS_TYPE_STR, "lms_ctrls_raw", "n", 0); + bool raw_mode = p && (*p == '1' || *p == 'Y' || *p == 'y'); + free(p); + const rotary_struct_t *rotary = config_rotary_get(); + if (GPIO_IS_VALID_GPIO(rotary->A) && rotary->A >= 0 && GPIO_IS_VALID_GPIO(rotary->B) && rotary->B >= 0) { + cJSON_AddNumberToObject(values, rotary_args.A->hdr.longopts, rotary->A); + cJSON_AddNumberToObject(values, rotary_args.B->hdr.longopts, rotary->B); + if (GPIO_IS_VALID_GPIO(rotary->SW) && rotary->SW >= 0) { + cJSON_AddNumberToObject(values, rotary_args.SW->hdr.longopts, rotary->SW); + } + cJSON_AddBoolToObject(values, rotary_args.volume_lock->hdr.longopts, rotary->volume_lock); + cJSON_AddBoolToObject(values, rotary_args.longpress->hdr.longopts, rotary->longpress); + cJSON_AddBoolToObject(values, rotary_args.knobonly->hdr.longopts, rotary->knobonly); + cJSON_AddNumberToObject(values, rotary_args.timer->hdr.longopts, rotary->timer); + cJSON_AddNumberToObject(values, rotary_args.raw_mode->hdr.longopts, raw_mode); + } + return values; } -cJSON * ledvu_cb(){ - cJSON * values = cJSON_CreateObject(); - const ledvu_struct_t *ledvu= config_ledvu_get(); - - if(GPIO_IS_VALID_GPIO(ledvu->gpio) && ledvu->gpio>=0 && ledvu->length > 0){ - cJSON_AddNumberToObject(values,"gpio",ledvu->gpio); - cJSON_AddNumberToObject(values,"length",ledvu->length); - } - if(strlen(ledvu->type)>0){ - cJSON_AddStringToObject(values,"type",ledvu->type); - } - else { - cJSON_AddStringToObject(values,"type","WS2812"); - } - return values; +cJSON *ledvu_cb() { + cJSON *values = cJSON_CreateObject(); + const ledvu_struct_t *ledvu = config_ledvu_get(); + if (GPIO_IS_VALID_GPIO(ledvu->gpio) && ledvu->gpio >= 0 && ledvu->length > 0) { + cJSON_AddNumberToObject(values, "gpio", ledvu->gpio); + cJSON_AddNumberToObject(values, "length", ledvu->length); + } + if (strlen(ledvu->type) > 0) { + cJSON_AddStringToObject(values, "type", ledvu->type); + } else { + cJSON_AddStringToObject(values, "type", "WS2812"); + } + return values; } -cJSON * audio_cb(){ - cJSON * values = cJSON_CreateObject(); - char * p = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0); - cJSON_AddStringToObject(values,"jack_behavior",(strcmp(p,"1") == 0 ||strcasecmp(p,"y") == 0)?"Headphones":"Subwoofer"); +cJSON *audio_cb() { + cJSON *values = cJSON_CreateObject(); + char *p = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0); + cJSON_AddStringToObject(values, "jack_behavior", (strcmp(p, "1") == 0 || strcasecmp(p, "y") == 0) ? "Headphones" : "Subwoofer"); FREE_AND_NULL(p); p = config_alloc_get_default(NVS_TYPE_STR, "loudness", "0", 0); - cJSON_AddStringToObject(values,"loudness",p); - FREE_AND_NULL(p); - return values; + cJSON_AddStringToObject(values, "loudness", p); + FREE_AND_NULL(p); + return values; } -cJSON * bt_source_cb(){ - cJSON * values = cJSON_CreateObject(); - char * p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_sink_name", NULL, 0); - if(p){ - cJSON_AddStringToObject(values,"sink_name",p); - } - FREE_AND_NULL(p); - // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctmt", NULL, 0); - // if(p){ - // cJSON_AddNumberToObject(values,"connect_timeout_delay",((double)atoi(p)/1000.0)); - // } - // FREE_AND_NULL(p); - p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_spin", "0000", 0); - if(p){ - cJSON_AddStringToObject(values,"pin_code",p); - } - FREE_AND_NULL(p); - // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctrld", NULL, 0); - // if(p){ - // cJSON_AddNumberToObject(values,"control_delay",((double)atoi(p)/1000.0)); - // } - // FREE_AND_NULL(p); - return values; +cJSON *bt_source_cb() { + cJSON *values = cJSON_CreateObject(); + char *p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_sink_name", NULL, 0); + if (p) { + cJSON_AddStringToObject(values, "sink_name", p); + } + FREE_AND_NULL(p); + // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctmt", NULL, 0); + // if(p){ + // cJSON_AddNumberToObject(values,"connect_timeout_delay",((double)atoi(p)/1000.0)); + // } + // FREE_AND_NULL(p); + p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_spin", "0000", 0); + if (p) { + cJSON_AddStringToObject(values, "pin_code", p); + } + FREE_AND_NULL(p); + // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctrld", NULL, 0); + // if(p){ + // cJSON_AddNumberToObject(values,"control_delay",((double)atoi(p)/1000.0)); + // } + // FREE_AND_NULL(p); + return values; } - -void get_str_parm_json(struct arg_str * parm, cJSON * entry){ - const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary; - if(parm->count>0){ - cJSON_AddStringToObject(entry,name,parm->sval[0]); - } +void get_str_parm_json(struct arg_str *parm, cJSON *entry) { + const char *name = parm->hdr.longopts ? parm->hdr.longopts : parm->hdr.glossary; + if (parm->count > 0) { + cJSON_AddStringToObject(entry, name, parm->sval[0]); + } } -void get_file_parm_json(struct arg_file * parm, cJSON * entry){ - const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary; - if(parm->count>0){ - cJSON_AddStringToObject(entry,name,parm->filename[0]); - } +void get_file_parm_json(struct arg_file *parm, cJSON *entry) { + const char *name = parm->hdr.longopts ? parm->hdr.longopts : parm->hdr.glossary; + if (parm->count > 0) { + cJSON_AddStringToObject(entry, name, parm->filename[0]); + } } -void get_lit_parm_json(struct arg_lit * parm, cJSON * entry){ - const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary; - cJSON_AddBoolToObject(entry,name,(parm->count>0)); +void get_lit_parm_json(struct arg_lit *parm, cJSON *entry) { + const char *name = parm->hdr.longopts ? parm->hdr.longopts : parm->hdr.glossary; + cJSON_AddBoolToObject(entry, name, (parm->count > 0)); } -void get_int_parm_json(struct arg_int * parm, cJSON * entry){ - const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary; - if(parm->count>0){ - cJSON_AddNumberToObject(entry,name,parm->ival[0]); - } +void get_int_parm_json(struct arg_int *parm, cJSON *entry) { + const char *name = parm->hdr.longopts ? parm->hdr.longopts : parm->hdr.glossary; + if (parm->count > 0) { + cJSON_AddNumberToObject(entry, name, parm->ival[0]); + } } -static int do_squeezelite_cmd(int argc, char **argv) -{ - esp_err_t err=ESP_OK; - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr ** )&squeezelite_args); +static int do_squeezelite_cmd(int argc, char **argv) { + esp_err_t err = ESP_OK; + int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr **)&squeezelite_args); char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - fprintf(f,"Not yet implemented!"); - nerrors+=1; - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + size_t buf_size = 0; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + fprintf(f, "Not yet implemented!"); + nerrors += 1; + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; } -cJSON * squeezelite_cb(){ - cJSON * values = cJSON_CreateObject(); - char * nvs_config= config_alloc_get(NVS_TYPE_STR, "autoexec1"); - char **argv = NULL; - char *buf = NULL; - size_t buf_size = 0; - int nerrors=1; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - log_send_messaging(MESSAGING_ERROR,"Unable to parse squeezelite parameters"); - } - else { +cJSON *squeezelite_cb() { + cJSON *values = cJSON_CreateObject(); + char *nvs_config = config_alloc_get(NVS_TYPE_STR, "autoexec1"); + char **argv = NULL; + char *buf = NULL; + size_t buf_size = 0; + int nerrors = 1; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return values; + } - if(nvs_config && strlen(nvs_config)>0){ - ESP_LOGD(TAG,"Parsing command %s",nvs_config); - argv = (char **) calloc(22, sizeof(char *)); - if (argv == NULL) { - FREE_AND_NULL(nvs_config); - fclose(f); - return values; - } - size_t argc = esp_console_split_argv(nvs_config, argv,22); - if (argc != 0) { - nerrors = arg_parse(argc, argv,(void **)&squeezelite_args); - ESP_LOGD(TAG,"Parsing completed"); - } - } - if (nerrors == 0) { - get_str_parm_json(squeezelite_args.buffers, values); - get_str_parm_json(squeezelite_args.codecs, values); - get_lit_parm_json(squeezelite_args.header_format, values); - get_str_parm_json(squeezelite_args.log_level, values); - - // get_str_parm_json(squeezelite_args.log_level_all, values); - // get_str_parm_json(squeezelite_args.log_level_decode, values); - // get_str_parm_json(squeezelite_args.log_level_output, values); - // get_str_parm_json(squeezelite_args.log_level_slimproto, values); - // get_str_parm_json(squeezelite_args.log_level_stream, values); - get_str_parm_json(squeezelite_args.mac_addr, values); - get_str_parm_json(squeezelite_args.output_device, values); - get_str_parm_json(squeezelite_args.model_name, values); - get_str_parm_json(squeezelite_args.name, values); - get_int_parm_json(squeezelite_args.rate, values); - get_str_parm_json(squeezelite_args.rates, values); - get_str_parm_json(squeezelite_args.server, values); - get_int_parm_json(squeezelite_args.timeout, values); - char * p = cJSON_Print(values); - ESP_LOGD(TAG,"%s",p); - free(p); - } - else { - arg_print_errors(f, squeezelite_args.end, desc_squeezelite); - } - fflush (f); - if(strlen(buf)>0){ - log_send_messaging(nerrors?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - } - fclose(f); - FREE_AND_NULL(buf); - } - FREE_AND_NULL(nvs_config); - FREE_AND_NULL(argv); - return values; + if (nvs_config && strlen(nvs_config) > 0) { + ESP_LOGD(TAG, "Parsing command %s", nvs_config); + argv = (char **)calloc(22, sizeof(char *)); + if (argv == NULL) { + FREE_AND_NULL(nvs_config); + fclose(f); + return values; + } + size_t argc = esp_console_split_argv(nvs_config, argv, 22); + if (argc != 0) { + nerrors = arg_parse(argc, argv, (void **)&squeezelite_args); + ESP_LOGD(TAG, "Parsing completed"); + } + } + if (nerrors == 0) { + get_str_parm_json(squeezelite_args.buffers, values); + get_str_parm_json(squeezelite_args.codecs, values); + get_lit_parm_json(squeezelite_args.header_format, values); + get_str_parm_json(squeezelite_args.log_level, values); + + // get_str_parm_json(squeezelite_args.log_level_all, values); + // get_str_parm_json(squeezelite_args.log_level_decode, values); + // get_str_parm_json(squeezelite_args.log_level_output, values); + // get_str_parm_json(squeezelite_args.log_level_slimproto, values); + // get_str_parm_json(squeezelite_args.log_level_stream, values); + get_str_parm_json(squeezelite_args.mac_addr, values); + get_str_parm_json(squeezelite_args.output_device, values); + get_str_parm_json(squeezelite_args.model_name, values); + get_str_parm_json(squeezelite_args.name, values); + get_int_parm_json(squeezelite_args.rate, values); + get_str_parm_json(squeezelite_args.rates, values); + get_str_parm_json(squeezelite_args.server, values); + get_int_parm_json(squeezelite_args.timeout, values); + char *p = cJSON_Print(values); + ESP_LOGD(TAG, "%s", p); + free(p); + } else { + arg_print_errors(f, squeezelite_args.end, desc_squeezelite); + } + fflush(f); + if (strlen(buf) > 0) { + log_send_messaging(nerrors ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + } + fclose(f); + FREE_AND_NULL(buf); + FREE_AND_NULL(nvs_config); + FREE_AND_NULL(argv); + return values; } -static char * get_log_level_options(const char * longname){ - const char * template = "<%s=info|%s=debug|%s=sdebug>"; - char * options = NULL; - int len = snprintf(NULL,0,template,longname,longname,longname); - if(len>0){ - options = malloc_init_external(len+1); - snprintf(options,len,template,longname,longname,longname); - } - return options; +static char *get_log_level_options(const char *longname) { + const char *template = "<%s=info|%s=debug|%s=sdebug>"; + char *options = NULL; + int len = snprintf(NULL, 0, template, longname, longname, longname); + if (len > 0) { + options = malloc_init_external(len + 1); + snprintf(options, len, template, longname, longname, longname); + } + return options; } // loop through dac_set and concatenate model name separated with | -static char * get_dac_list(){ - const char * EXTRA_MODEL_NAMES = "ES8388|I2S"; - char * dac_list=NULL; - size_t total_len=0; - for(int i=0;dac_set[i];i++){ - if(dac_set[i]->model && strlen(dac_set[i]->model)>0){ - total_len+=strlen(dac_set[i]->model)+1; - } - else { - break; - } - } - total_len+=strlen(EXTRA_MODEL_NAMES); - dac_list = malloc_init_external(total_len+1); - if(dac_list){ - for(int i=0;dac_set[i];i++){ - if(dac_set[i]->model && strlen(dac_set[i]->model)>0){ - strcat(dac_list,dac_set[i]->model); - strcat(dac_list,"|"); - } - else { - break; - } - } - strcat(dac_list,EXTRA_MODEL_NAMES); - } - return dac_list; -} -void replace_char_in_string(char * str, char find, char replace){ - for(int i=0;str[i];i++){ - if(str[i]==find){ - str[i]=replace; - } - } -} -static esp_err_t save_known_config(cJSON * known_item, const char * name,FILE * f){ - esp_err_t err = ESP_OK; - char * json_string=NULL; - json_string = cJSON_Print(known_item); - ESP_LOGD(TAG,"known_item_string: %s",STR_OR_BLANK(json_string)); - FREE_AND_NULL(json_string); - cJSON * kvp=NULL; - cJSON * config_array = cJSON_GetObjectItem(known_item,"config"); - if(config_array && cJSON_IsArray(config_array)){ - json_string = cJSON_Print(config_array); - ESP_LOGD(TAG,"config_array: %s",STR_OR_BLANK(json_string)); - FREE_AND_NULL(json_string); - cJSON_ArrayForEach(kvp, config_array){ - cJSON * kvp_value=kvp->child; - if(!kvp_value){ - printf("config entry is not an object!\n"); - err=ESP_FAIL; - continue; - } - char * key = kvp_value->string; - char * value = kvp_value->valuestring; - if(!key || !value || strlen(key)==0){ - printf("Invalid config entry %s:%s\n",STR_OR_BLANK(key),STR_OR_BLANK(value)); - err=ESP_FAIL; - continue; - } - - fprintf(f,"Storing %s=%s\n",key,value); - err = config_set_value(NVS_TYPE_STR,key,value); - if(err){ - fprintf(f,"Failed to store config value: %s\n",esp_err_to_name(err)); - break; - } - } - } - else { - json_string = cJSON_Print(config_array); - char * known_item_string = cJSON_Print(known_item); - fprintf(f,"Failed to parse config array. %s\n%s\nKnown item found: %s\n",config_array?cJSON_IsArray(config_array)?"":"NOT AN ARRAY":"config entry not found",STR_OR_BLANK(json_string),STR_OR_BLANK(known_item_string)); - FREE_AND_NULL(json_string); - FREE_AND_NULL(known_item_string); - err = ESP_FAIL; - } - - - if(err==ESP_OK){ - err = config_set_value(NVS_TYPE_STR,"board_model",name); - if(err!=ESP_OK){ - fprintf(f,"Failed to save board model %s\n",name); - } - } - - return err; -} - -static int do_register_known_templates_config(int argc, char **argv){ - esp_err_t err=ESP_OK; - int nerrors = arg_parse(argc, argv,(void **)&known_model_args); - char *buf = NULL; - size_t buf_size = 0; - cJSON * config_name =NULL; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors >0){ - arg_print_errors(f,known_model_args.end,desc_preset); - } - else { - ESP_LOGD(TAG,"arg: %s",STR_OR_BLANK(known_model_args.model_config->sval[0])); - char * model_config = strdup_psram(known_model_args.model_config->sval[0]); - char * t = model_config; - for(const char * p=known_model_args.model_config->sval[0];*p;p++){ - if(*p=='\\' && *(p+1)=='"'){ - *t++='"'; - p++; - } - else { - *t++=*p; - } - } - *t=0; - cJSON * known_item = cJSON_Parse(model_config); - if(known_item){ - ESP_LOGD(TAG,"Parsing success"); - config_name= cJSON_GetObjectItem(known_item,"name"); - if(!config_name || !cJSON_IsString(config_name) || strlen(config_name->valuestring)==0){ - fprintf(f,"Failed to find name in config\n"); - err=ESP_FAIL; - nerrors++; - } - if(nerrors==0){ - const char * name = cJSON_GetStringValue(config_name); - nerrors+=(err = save_known_config(known_item,name,f)!=ESP_OK); - if(nerrors==0){ - const i2s_platform_config_t * i2s_config= config_dac_get(); - if(i2s_config->scl!=-1 && i2s_config->sda!=-1 && GPIO_IS_VALID_GPIO(i2s_config->scl) && GPIO_IS_VALID_GPIO(i2s_config->sda)){ - fprintf(f,"Scanning i2c bus for devices\n"); - cmd_i2ctools_scan_bus(f,i2s_config->sda, i2s_config->scl); - } - } - } - cJSON_Delete(known_item); - } - else { - ESP_LOGE(TAG,"Parsing error: %s",cJSON_GetErrorPtr()); - fprintf(f,"Failed to parse JSON: %s\n",cJSON_GetErrorPtr()); - err=ESP_FAIL; - } - if(err!=ESP_OK){ - nerrors++; - fprintf(f,"Error registering known config %s.\n",known_model_args.model_config->sval[0]); +static char *get_dac_list() { + const char *EXTRA_MODEL_NAMES = "ES8388|I2S"; + char *dac_list = NULL; + size_t total_len = 0; + for (int i = 0; dac_set[i]; i++) { + if (dac_set[i]->model && strlen(dac_set[i]->model) > 0) { + total_len += strlen(dac_set[i]->model) + 1; + } else { + break; } - else { - fprintf(f,"Registered known config %s.\n",known_model_args.model_config->sval[0]); - } } - - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return (nerrors==0 && err==ESP_OK)?0:1; + total_len += strlen(EXTRA_MODEL_NAMES); + dac_list = malloc_init_external(total_len + 1); + if (dac_list) { + for (int i = 0; dac_set[i]; i++) { + if (dac_set[i]->model && strlen(dac_set[i]->model) > 0) { + strcat(dac_list, dac_set[i]->model); + strcat(dac_list, "|"); + } else { + break; + } + } + strcat(dac_list, EXTRA_MODEL_NAMES); + } + return dac_list; } -static void register_known_templates_config(){ +void replace_char_in_string(char *str, char find, char replace) { + for (int i = 0; str[i]; i++) { + if (str[i] == find) { + str[i] = replace; + } + } +} +static esp_err_t save_known_config(cJSON *known_item, const char *name, FILE *f) { + esp_err_t err = ESP_OK; + char *json_string = NULL; + json_string = cJSON_Print(known_item); + ESP_LOGD(TAG, "known_item_string: %s", STR_OR_BLANK(json_string)); + FREE_AND_NULL(json_string); + cJSON *kvp = NULL; + cJSON *config_array = cJSON_GetObjectItem(known_item, "config"); + if (config_array && cJSON_IsArray(config_array)) { + json_string = cJSON_Print(config_array); + ESP_LOGD(TAG, "config_array: %s", STR_OR_BLANK(json_string)); + FREE_AND_NULL(json_string); + cJSON_ArrayForEach(kvp, config_array) { + cJSON *kvp_value = kvp->child; + if (!kvp_value) { + printf("config entry is not an object!\n"); + err = ESP_FAIL; + continue; + } + char *key = kvp_value->string; + char *value = kvp_value->valuestring; + if (!key || !value || strlen(key) == 0) { + printf("Invalid config entry %s:%s\n", STR_OR_BLANK(key), STR_OR_BLANK(value)); + err = ESP_FAIL; + continue; + } - known_model_args.model_config = arg_str1(NULL,"model_config","SqueezeAMP|T-WATCH2020 by LilyGo","Known Board Name.\nFor known boards, several systems parameters will be updated"); - known_model_args.end = arg_end(1); - const esp_console_cmd_t cmd = { + fprintf(f, "Storing %s=%s\n", key, value); + err = config_set_value(NVS_TYPE_STR, key, value); + if (err) { + fprintf(f, "Failed to store config value: %s\n", esp_err_to_name(err)); + break; + } + } + } else { + json_string = cJSON_Print(config_array); + char *known_item_string = cJSON_Print(known_item); + fprintf(f, "Failed to parse config array. %s\n%s\nKnown item found: %s\n", config_array ? cJSON_IsArray(config_array) ? "" : "NOT AN ARRAY" : "config entry not found", STR_OR_BLANK(json_string), STR_OR_BLANK(known_item_string)); + FREE_AND_NULL(json_string); + FREE_AND_NULL(known_item_string); + err = ESP_FAIL; + } + + if (err == ESP_OK) { + err = config_set_value(NVS_TYPE_STR, "board_model", name); + if (err != ESP_OK) { + fprintf(f, "Failed to save board model %s\n", name); + } + } + + return err; +} + +static int do_register_known_templates_config(int argc, char **argv) { + esp_err_t err = ESP_OK; + int nerrors = arg_parse(argc, argv, (void **)&known_model_args); + char *buf = NULL; + size_t buf_size = 0; + cJSON *config_name = NULL; + FILE *f = system_open_memstream(argv[0], &buf, &buf_size); + if (f == NULL) { + return 1; + } + if (nerrors > 0) { + arg_print_errors(f, known_model_args.end, desc_preset); + } else { + ESP_LOGD(TAG, "arg: %s", STR_OR_BLANK(known_model_args.model_config->sval[0])); + char *model_config = strdup_psram(known_model_args.model_config->sval[0]); + char *t = model_config; + for (const char *p = known_model_args.model_config->sval[0]; *p; p++) { + if (*p == '\\' && *(p + 1) == '"') { + *t++ = '"'; + p++; + } else { + *t++ = *p; + } + } + *t = 0; + cJSON *known_item = cJSON_Parse(model_config); + if (known_item) { + ESP_LOGD(TAG, "Parsing success"); + config_name = cJSON_GetObjectItem(known_item, "name"); + if (!config_name || !cJSON_IsString(config_name) || strlen(config_name->valuestring) == 0) { + fprintf(f, "Failed to find name in config\n"); + err = ESP_FAIL; + nerrors++; + } + if (nerrors == 0) { + const char *name = cJSON_GetStringValue(config_name); + nerrors += (err = save_known_config(known_item, name, f) != ESP_OK); + if (nerrors == 0) { + const i2s_platform_config_t *i2s_config = config_dac_get(); + if (i2s_config->scl != -1 && i2s_config->sda != -1 && GPIO_IS_VALID_GPIO(i2s_config->scl) && GPIO_IS_VALID_GPIO(i2s_config->sda)) { + fprintf(f, "Scanning i2c bus for devices\n"); + cmd_i2ctools_scan_bus(f, i2s_config->sda, i2s_config->scl); + } + } + } + cJSON_Delete(known_item); + } else { + ESP_LOGE(TAG, "Parsing error: %s", cJSON_GetErrorPtr()); + fprintf(f, "Failed to parse JSON: %s\n", cJSON_GetErrorPtr()); + err = ESP_FAIL; + } + if (err != ESP_OK) { + nerrors++; + fprintf(f, "Error registering known config %s.\n", known_model_args.model_config->sval[0]); + } else { + fprintf(f, "Registered known config %s.\n", known_model_args.model_config->sval[0]); + } + } + + if (!nerrors) { + fprintf(f, "Done.\n"); + } + fflush(f); + cmd_send_messaging(argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose(f); + FREE_AND_NULL(buf); + return (nerrors == 0 && err == ESP_OK) ? 0 : 1; +} +static void register_known_templates_config() { + known_model_args.model_config = arg_str1(NULL, "model_config", "SqueezeAMP|T-WATCH2020 by LilyGo", "Known Board Name.\nFor known boards, several systems parameters will be updated"); + known_model_args.end = arg_end(1); + const esp_console_cmd_t cmd = { .command = CFG_TYPE_HW("preset"), .help = desc_preset, .hint = NULL, .func = &do_register_known_templates_config, - .argtable = &known_model_args - }; - cmd_to_json_with_cb(&cmd,&known_model_cb); + .argtable = &known_model_args}; + cmd_to_json_with_cb(&cmd, &known_model_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } #ifdef CONFIG_CSPOT_SINK -static void register_cspot_config(){ - cspot_args.deviceName = arg_str1(NULL,"deviceName","","Device Name"); - cspot_args.bitrate = arg_int1(NULL,"bitrate","96|160|320","Streaming Bitrate (kbps)"); - cspot_args.zeroConf = arg_int1(NULL,"zeroConf","0|1","Force use of ZeroConf"); -// cspot_args.volume = arg_int1(NULL,"volume","","Spotify Volume"); - cspot_args.end = arg_end(1); - const esp_console_cmd_t cmd = { - .command = CFG_TYPE_SYST("cspot"), - .help = desc_cspotc, - .hint = NULL, - .func = &do_cspot_config, - .argtable = &cspot_args - }; - cmd_to_json_with_cb(&cmd,&cspot_cb); - ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); +static void register_cspot_config() { + cspot_args.deviceName = arg_str1(NULL, "deviceName", "", "Device Name"); + cspot_args.bitrate = arg_int1(NULL, "bitrate", "96|160|320", "Streaming Bitrate (kbps)"); + cspot_args.zeroConf = arg_int1(NULL, "zeroConf", "0|1", "Force use of ZeroConf"); + // cspot_args.volume = arg_int1(NULL,"volume","","Spotify Volume"); + cspot_args.end = arg_end(1); + const esp_console_cmd_t cmd = { + .command = CFG_TYPE_SYST("cspot"), + .help = desc_cspotc, + .hint = NULL, + .func = &do_cspot_config, + .argtable = &cspot_args}; + cmd_to_json_with_cb(&cmd, &cspot_cb); + ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } #endif -static void register_i2s_config(void){ - i2s_args.model_name = arg_str0(NULL,"model_name",STR_OR_BLANK(get_dac_list()),"DAC Model Name"); - i2s_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); - i2s_args.clock = arg_int0(NULL,"clock","","Clock GPIO. e.g. 33"); - i2s_args.wordselect = arg_int0(NULL,"wordselect","","Word Select GPIO. e.g. 25"); - i2s_args.data = arg_int0(NULL,"data","","Data GPIO. e.g. 32"); - i2s_args.mute_gpio = arg_int0(NULL,"mute_gpio", "", "Mute GPIO. e.g. 14"); - i2s_args.mute_level = arg_lit0(NULL,"mute_level","Mute GPIO level. Checked=HIGH, Unchecked=LOW"); - i2s_args.dac_sda = arg_int0(NULL,"dac_sda", "", "SDA GPIO. e.g. 27"); - i2s_args.dac_scl = arg_int0(NULL,"dac_scl", "", "SCL GPIO. e.g. 26"); - i2s_args.dac_i2c = arg_int0(NULL,"dac_i2c", "", "I2C device address. e.g. 106"); +static void register_i2s_config(void) { + i2s_args.model_name = arg_str0(NULL, "model_name", STR_OR_BLANK(get_dac_list()), "DAC Model Name"); + i2s_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); + i2s_args.clock = arg_int0(NULL, "clock", "", "Clock GPIO. e.g. 33"); + i2s_args.wordselect = arg_int0(NULL, "wordselect", "", "Word Select GPIO. e.g. 25"); + i2s_args.data = arg_int0(NULL, "data", "", "Data GPIO. e.g. 32"); + i2s_args.mute_gpio = arg_int0(NULL, "mute_gpio", "", "Mute GPIO. e.g. 14"); + i2s_args.mute_level = arg_lit0(NULL, "mute_level", "Mute GPIO level. Checked=HIGH, Unchecked=LOW"); + i2s_args.dac_sda = arg_int0(NULL, "dac_sda", "", "SDA GPIO. e.g. 27"); + i2s_args.dac_scl = arg_int0(NULL, "dac_scl", "", "SCL GPIO. e.g. 26"); + i2s_args.dac_i2c = arg_int0(NULL, "dac_i2c", "", "I2C device address. e.g. 106"); i2s_args.end = arg_end(6); - const esp_console_cmd_t cmd = { + const esp_console_cmd_t cmd = { .command = CFG_TYPE_HW("dac"), .help = desc_dac, .hint = NULL, .func = &do_i2s_cmd, - .argtable = &i2s_args - }; - cmd_to_json_with_cb(&cmd,&i2s_cb); + .argtable = &i2s_args}; + cmd_to_json_with_cb(&cmd, &i2s_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -static void register_bt_source_config(void){ - - bt_source_args.sink_name= arg_str1("n","sink_name", "name","Bluetooth audio device name. This applies when output mode is Bluetooth"); - bt_source_args.pin_code= arg_str1("p","pin_code", "pin","Bluetooth security/pin code. Usually 0000. This applies when output mode is Bluetooth"); -// bt_source_args.control_delay= arg_dbl0("d","control_delay","seconds","Control response delay, in seconds. This determines the response time of the system Bluetooth events. The default value should work for the majority of cases and changing this could lead to instabilities."); -// bt_source_args.connect_timeout_delay= arg_dbl0("t","connect_timeout_delay","seconds","Connection timeout. Determines the maximum amount of time, in seconds, that the system will wait when connecting to a bluetooth device. Beyond this delay, a new connect attempt will be made."); - bt_source_args.end= arg_end(1); - const esp_console_cmd_t cmd = { +static void register_bt_source_config(void) { + bt_source_args.sink_name = arg_str1("n", "sink_name", "name", "Bluetooth audio device name. This applies when output mode is Bluetooth"); + bt_source_args.pin_code = arg_str1("p", "pin_code", "pin", "Bluetooth security/pin code. Usually 0000. This applies when output mode is Bluetooth"); + // bt_source_args.control_delay= arg_dbl0("d","control_delay","seconds","Control response delay, in seconds. This determines the response time of the system Bluetooth events. The default value should work for the majority of cases and changing this could lead to instabilities."); + // bt_source_args.connect_timeout_delay= arg_dbl0("t","connect_timeout_delay","seconds","Connection timeout. Determines the maximum amount of time, in seconds, that the system will wait when connecting to a bluetooth device. Beyond this delay, a new connect attempt will be made."); + bt_source_args.end = arg_end(1); + const esp_console_cmd_t cmd = { .command = CFG_TYPE_AUDIO("bt_source"), .help = desc_bt_source, .hint = NULL, .func = &do_bt_source_cmd, - .argtable = &bt_source_args - }; - cmd_to_json_with_cb(&cmd,&bt_source_cb); + .argtable = &bt_source_args}; + cmd_to_json_with_cb(&cmd, &bt_source_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -void register_rotary_config(void){ - rotary_args.rem = arg_rem("remark","One rotary encoder is supported, quadrature shift with press. Such encoders usually have 2 pins for encoders (A and B), and common C that must be set to ground and an optional SW pin for press. A, B and SW must be pulled up, so automatic pull-up is provided by ESP32, but you can add your own resistors. A bit of filtering on A and B (~470nF) helps for debouncing which is not made by software.\r\nEncoder is normally hard-coded to respectively knob left, right and push on LMS and to volume down/up/play toggle on BT and AirPlay."); - rotary_args.A = arg_int1(NULL,"A","gpio","A/DT gpio"); - rotary_args.B = arg_int1(NULL,"B","gpio","B/CLK gpio"); - rotary_args.SW = arg_int0(NULL,"SW","gpio","Switch gpio"); - rotary_args.knobonly = arg_lit0(NULL,"knobonly","Single knob full navigation. Left, Right and Press is navigation, with Press always going to lower submenu item. Longpress is 'Play', Double press is 'Back', a quick left-right movement on the encoder is 'Pause'"); - rotary_args.timer = arg_int0(NULL,"timer","ms","The speed of double click (or left-right) when knob only option is enabled. Be aware that the longer you set double click speed, the less responsive the interface will be. "); - rotary_args.volume_lock = arg_lit0(NULL,"volume_lock", "Force Volume down/up/play toggle all the time (even in LMS). "); - rotary_args.longpress = arg_lit0(NULL,"longpress","Enable alternate mode mode on long-press. In that mode, left is previous, right is next and press is toggle. Every long press on SW alternates between modes (the main mode actual behavior depends on 'volume')."); - rotary_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); - rotary_args.raw_mode = arg_lit0(NULL, "raw_mode", "Send button events as raw values to LMS. No remapping is possible when this is enabled"); - rotary_args.end = arg_end(3); - const esp_console_cmd_t cmd = { +void register_rotary_config(void) { + rotary_args.rem = arg_rem("remark", "One rotary encoder is supported, quadrature shift with press. Such encoders usually have 2 pins for encoders (A and B), and common C that must be set to ground and an optional SW pin for press. A, B and SW must be pulled up, so automatic pull-up is provided by ESP32, but you can add your own resistors. A bit of filtering on A and B (~470nF) helps for debouncing which is not made by software.\r\nEncoder is normally hard-coded to respectively knob left, right and push on LMS and to volume down/up/play toggle on BT and AirPlay."); + rotary_args.A = arg_int1(NULL, "A", "gpio", "A/DT gpio"); + rotary_args.B = arg_int1(NULL, "B", "gpio", "B/CLK gpio"); + rotary_args.SW = arg_int0(NULL, "SW", "gpio", "Switch gpio"); + rotary_args.knobonly = arg_lit0(NULL, "knobonly", "Single knob full navigation. Left, Right and Press is navigation, with Press always going to lower submenu item. Longpress is 'Play', Double press is 'Back', a quick left-right movement on the encoder is 'Pause'"); + rotary_args.timer = arg_int0(NULL, "timer", "ms", "The speed of double click (or left-right) when knob only option is enabled. Be aware that the longer you set double click speed, the less responsive the interface will be. "); + rotary_args.volume_lock = arg_lit0(NULL, "volume_lock", "Force Volume down/up/play toggle all the time (even in LMS). "); + rotary_args.longpress = arg_lit0(NULL, "longpress", "Enable alternate mode mode on long-press. In that mode, left is previous, right is next and press is toggle. Every long press on SW alternates between modes (the main mode actual behavior depends on 'volume')."); + rotary_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); + rotary_args.raw_mode = arg_lit0(NULL, "raw_mode", "Send button events as raw values to LMS. No remapping is possible when this is enabled"); + rotary_args.end = arg_end(3); + const esp_console_cmd_t cmd = { .command = CFG_TYPE_HW("rotary"), .help = desc_rotary, .hint = NULL, .func = &do_rotary_cmd, - .argtable = &rotary_args - }; - cmd_to_json_with_cb(&cmd,&rotary_cb); + .argtable = &rotary_args}; + cmd_to_json_with_cb(&cmd, &rotary_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -void register_ledvu_config(void){ - ledvu_args.type = arg_str1(NULL,"type","|WS2812","Led type (supports one rgb strip to display built in effects and allow remote control through 'dmx' messaging)"); - ledvu_args.length = arg_int1(NULL,"length","<1..255>","Strip length (1-255 supported)"); - ledvu_args.gpio = arg_int1(NULL,"gpio","gpio","Data pin"); - ledvu_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); - ledvu_args.end = arg_end(4); +void register_ledvu_config(void) { + ledvu_args.type = arg_str1(NULL, "type", "|WS2812", "Led type (supports one rgb strip to display built in effects and allow remote control through 'dmx' messaging)"); + ledvu_args.length = arg_int1(NULL, "length", "<1..255>", "Strip length (1-255 supported)"); + ledvu_args.gpio = arg_int1(NULL, "gpio", "gpio", "Data pin"); + ledvu_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); + ledvu_args.end = arg_end(4); - const esp_console_cmd_t cmd = { + const esp_console_cmd_t cmd = { .command = CFG_TYPE_HW("ledvu"), .help = desc_ledvu, .hint = NULL, .func = &do_ledvu_cmd, - .argtable = &ledvu_args - }; - cmd_to_json_with_cb(&cmd,&ledvu_cb); + .argtable = &ledvu_args}; + cmd_to_json_with_cb(&cmd, &ledvu_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -void register_audio_config(void){ - audio_args.jack_behavior = arg_str0("j", "jack_behavior","Headphones|Subwoofer","On supported DAC, determines the audio jack behavior. Selecting headphones will cause the external amp to be muted on insert, while selecting Subwoofer will keep the amp active all the time."); - audio_args.loudness = arg_int0("l", "loudness","0-10","Sets the loudness level, from 0 to 10. 0 will disable the loudness completely."); +void register_audio_config(void) { + audio_args.jack_behavior = arg_str0("j", "jack_behavior", "Headphones|Subwoofer", "On supported DAC, determines the audio jack behavior. Selecting headphones will cause the external amp to be muted on insert, while selecting Subwoofer will keep the amp active all the time."); + audio_args.loudness = arg_int0("l", "loudness", "0-10", "Sets a loudness level, from 0 to 10. 0 will disable the loudness completely. Note that LMS has priority over setting this value, so use it only when away from your server."); audio_args.end = arg_end(6); audio_args.end = arg_end(6); - const esp_console_cmd_t cmd = { + const esp_console_cmd_t cmd = { .command = CFG_TYPE_AUDIO("general"), .help = desc_audio, .hint = NULL, .func = &do_audio_cmd, - .argtable = &audio_args - }; - cmd_to_json_with_cb(&cmd,&audio_cb); + .argtable = &audio_args}; + cmd_to_json_with_cb(&cmd, &audio_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -static void register_spdif_config(void){ - spdif_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); - spdif_args.clock = arg_int1(NULL,"clock","","Clock GPIO. e.g. 33"); - spdif_args.wordselect = arg_int1(NULL,"wordselect","","Word Select GPIO. e.g. 25"); - spdif_args.data = arg_int1(NULL,"data","","Data GPIO. e.g. 32"); +static void register_spdif_config(void) { + spdif_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); + spdif_args.clock = arg_int1(NULL, "clock", "", "Clock GPIO. e.g. 33"); + spdif_args.wordselect = arg_int1(NULL, "wordselect", "", "Word Select GPIO. e.g. 25"); + spdif_args.data = arg_int1(NULL, "data", "", "Data GPIO. e.g. 32"); spdif_args.end = arg_end(6); - const esp_console_cmd_t cmd = { + const esp_console_cmd_t cmd = { .command = CFG_TYPE_HW("spdif"), .help = desc_spdif, .hint = NULL, .func = &do_spdif_cmd, - .argtable = &spdif_args - }; - cmd_to_json_with_cb(&cmd,&spdif_cb); + .argtable = &spdif_args}; + cmd_to_json_with_cb(&cmd, &spdif_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -static void register_squeezelite_config(void){ - squeezelite_args.server = arg_str0("s","server","[:]","Connect to specified server, otherwise uses autodiscovery to find server"); - squeezelite_args.buffers = arg_str0("b","buffers",":","Internal Stream and Output buffer sizes in Kbytes"); - squeezelite_args.codecs = arg_strn("c","codecs","+" CODECS "+",0,20,"Restrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS ); - squeezelite_args.timeout = arg_int0("C","timeout","","Close output device when idle after timeout seconds, default is to keep it open while player is 'on"); - squeezelite_args.log_level = arg_str0("d","loglevel","log=level","Set logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug"); // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" -// squeezelite_args.log_level_all = arg_str0(NULL,"all",get_log_level_options("all"),"Overall Logging Level"); -// squeezelite_args.log_level_slimproto = arg_str0(NULL,"loglevel_slimproto",get_log_level_options("slimproto"),"Slimproto Logging Level"); -// squeezelite_args.log_level_stream= arg_str0(NULL,"loglevel_stream",get_log_level_options("stream"),"Stream Logging Level"); -// squeezelite_args.log_level_decode= arg_str0(NULL,"loglevel_decode",get_log_level_options("decode"),"Decode Logging Level"); -// squeezelite_args.log_level_output= arg_str0(NULL,"loglevel_output",get_log_level_options("output"),"Output Logging Level"); - #if IR - squeezelite_args.log_level_ir= arg_str0(NULL,"loglevel_ir",get_log_level_options("ir"),"IR Logging Level"); - #endif +static void register_squeezelite_config(void) { + squeezelite_args.server = arg_str0("s", "server", "[:]", "Connect to specified server, otherwise uses autodiscovery to find server"); + squeezelite_args.buffers = arg_str0("b", "buffers", ":", "Internal Stream and Output buffer sizes in Kbytes"); + squeezelite_args.codecs = arg_strn("c", "codecs", "+" CODECS "+", 0, 20, "Restrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS); + squeezelite_args.timeout = arg_int0("C", "timeout", "", "Close output device when idle after timeout seconds, default is to keep it open while player is 'on"); + squeezelite_args.log_level = arg_str0("d", "loglevel", "log=level", "Set logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug"); // " -d =\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n" +#if IR + squeezelite_args.log_level_ir = arg_str0(NULL, "loglevel_ir", get_log_level_options("ir"), "IR Logging Level"); +#endif - squeezelite_args.output_device = arg_str0("o","output_device","","Output device (BT, I2S or SPDIF)"); - squeezelite_args.mac_addr = arg_str0("m","mac_addr","","Mac address, format: ab:cd:ef:12:34:56."); - squeezelite_args.model_name = arg_str0("M", "modelname", "","Set the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")"); - squeezelite_args.name = arg_str0("n","name","","Player name, if different from the current host name. Name can alternatively be assigned from the system/device name configuration."); - squeezelite_args.header_format = arg_lit0("W","header_format","Read wave and aiff format from header, ignore server parameters"); - squeezelite_args.rates = arg_str0("r","rates","[:]", "Sample rates supported, allows output to be off when squeezelite is started; rates = |-|,,; delay = optional delay switching rates in ms\n"); - #if RESAMPLE - squeezelite_args.resample = arg_lit0("R","resample","Activate Resample"); - squeezelite_args.resample_parms = arg_str0("u","resample_parms","::::::","Resample, params"); - #endif - #if RESAMPLE16 - squeezelite_args.resample = arg_lit0("R","resample","Activate Resample"); - squeezelite_args.resample_parms = arg_str0("u","resample_parms","(b|l|m)[:i]","Resample, params. b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients"); - #endif - squeezelite_args.rate = arg_int0("Z","max_rate", "", "Report rate to server in helo as the maximum sample rate we can support"); - squeezelite_args.end = arg_end(6); + squeezelite_args.output_device = arg_str0("o", "output_device", "", "Output device (BT, I2S or SPDIF)"); + squeezelite_args.mac_addr = arg_str0("m", "mac_addr", "", "Mac address, format: ab:cd:ef:12:34:56."); + squeezelite_args.model_name = arg_str0("M", "modelname", "", "Set the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")"); + squeezelite_args.name = arg_str0("n", "name", "", "Player name, if different from the current host name. Name can alternatively be assigned from the system/device name configuration."); + squeezelite_args.header_format = arg_lit0("W", "header_format", "Read wave and aiff format from header, ignore server parameters"); + squeezelite_args.rates = arg_str0("r", "rates", "[:]", "Sample rates supported, allows output to be off when squeezelite is started; rates = |-|,,; delay = optional delay switching rates in ms\n"); +#if RESAMPLE + squeezelite_args.resample = arg_lit0("R", "resample", "Activate Resample"); + squeezelite_args.resample_parms = arg_str0("u", "resample_parms", "::::::", "Resample, params"); +#endif +#if RESAMPLE16 + squeezelite_args.resample = arg_lit0("R", "resample", "Activate Resample"); + squeezelite_args.resample_parms = arg_str0("u", "resample_parms", "(b|l|m)[:i]", "Resample, params. b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients"); +#endif + squeezelite_args.rate = arg_int0("Z", "max_rate", "", "Report rate to server in helo as the maximum sample rate we can support"); + squeezelite_args.end = arg_end(6); const esp_console_cmd_t cmd = { .command = CFG_TYPE_AUDIO("squeezelite"), .help = desc_squeezelite, .hint = NULL, .func = &do_squeezelite_cmd, - .argtable = &squeezelite_args - }; - cmd_to_json_with_cb(&cmd,&squeezelite_cb); + .argtable = &squeezelite_args}; + cmd_to_json_with_cb(&cmd, &squeezelite_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); } -void register_config_cmd(void){ - if(!is_dac_config_locked()){ - register_known_templates_config(); - - } -#ifdef CONFIG_CSPOT_SINK - register_cspot_config(); -#endif - register_bt_source_config(); - if(!is_dac_config_locked()){ - register_i2s_config(); - } - if(!is_spdif_config_locked()){ - register_spdif_config(); - } - register_optional_cmd(); +void register_config_cmd(void) { + if (!is_dac_config_locked()) { + register_known_templates_config(); + } +#ifdef CONFIG_CSPOT_SINK + register_cspot_config(); +#endif + register_bt_source_config(); + if (!is_dac_config_locked()) { + register_i2s_config(); + } + if (!is_spdif_config_locked()) { + register_spdif_config(); + } + register_optional_cmd(); } - diff --git a/components/platform_console/cmd_i2ctools.c b/components/platform_console/cmd_i2ctools.c index 54e56bc8..08ec4e68 100644 --- a/components/platform_console/cmd_i2ctools.c +++ b/components/platform_console/cmd_i2ctools.c @@ -6,22 +6,22 @@ software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -#include #include "cmd_i2ctools.h" -#include "argtable3/argtable3.h" -#include "driver/i2c.h" -#include "platform_console.h" -#include "esp_log.h" -#include "string.h" -#include "stdio.h" -#include "platform_config.h" +#include #include "accessors.h" -#include "trace.h" -#include "messaging.h" -#include "display.h" -#include "config.h" -#include "tools.h" #include "adac.h" +#include "argtable3/argtable3.h" +#include "config.h" +#include "display.h" +#include "driver/i2c.h" +#include "esp_log.h" +#include "messaging.h" +#include "platform_config.h" +#include "platform_console.h" +#include "stdio.h" +#include "string.h" +#include "tools.h" +#include "trace.h" #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ @@ -33,11 +33,11 @@ #define NACK_VAL 0x1 /*!< I2C nack value */ extern int spi_system_host; extern int spi_system_dc_gpio; -extern int is_output_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory); -static const char *TAG = "cmd_i2ctools"; -const char * desc_spiconfig="SPI Bus Parameters"; -const char * desc_i2c = "I2C Bus Parameters"; -const char * desc_display="Display"; +extern int is_output_gpio (struct arg_int* gpio, FILE* f, int* gpio_out, bool mandatory); +static const char* TAG = "cmd_i2ctools"; +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; @@ -46,565 +46,544 @@ static i2c_port_t i2c_port = I2C_NUM_0; #endif static struct { - struct arg_int *chip_address; - struct arg_int *register_address; - struct arg_int *data_length; - struct arg_end *end; + 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 *chip_address; - struct arg_int *port; - struct arg_int *register_address; - struct arg_int *data; - struct arg_end *end; + struct arg_int* chip_address; + struct arg_int* port; + struct arg_int* register_address; + struct arg_int* data; + struct arg_end* end; } i2cset_args; static struct { - struct arg_int *chip_address; - struct arg_int *size; - struct arg_end *end; + struct arg_int* chip_address; + struct arg_int* size; + struct arg_end* end; } i2cdump_args; static struct { - struct arg_int *port; - struct arg_int *freq; - struct arg_int *sda; - struct arg_int *scl; - struct arg_lit *clear; - struct arg_end *end; + struct arg_int* port; + struct arg_int* freq; + struct arg_int* sda; + struct arg_int* scl; + struct arg_lit* clear; + struct arg_end* end; } i2cconfig_args; static struct { - struct arg_int *data; - struct arg_int *miso; - struct arg_int *clk; - struct arg_int *dc; - struct arg_int *host; - struct arg_lit *clear; - struct arg_end *end; + struct arg_int* data; + struct arg_int* miso; + struct arg_int* clk; + struct arg_int* dc; + struct arg_int* host; + struct arg_lit* clear; + struct arg_end* end; } spiconfig_args; static struct { - struct arg_str *type; - struct arg_str *driver; - struct arg_int *depth; - struct arg_int *address; - struct arg_int *width; - struct arg_int *height; - struct arg_lit *rotate; - struct arg_lit *hflip; - struct arg_lit *vflip; - struct arg_int *speed; - struct arg_int *cs; - struct arg_int *back; - struct arg_int *reset; - struct arg_lit *clear; - struct arg_lit *invert; - struct arg_int *mode; - struct arg_end *end; + struct arg_str* type; + struct arg_str* driver; + struct arg_int* depth; + struct arg_int* address; + struct arg_int* width; + struct arg_int* height; + struct arg_lit* rotate; + struct arg_lit* hflip; + struct arg_lit* vflip; + struct arg_int* speed; + struct arg_int* cs; + struct arg_int* back; + struct arg_int* reset; + struct arg_lit* clear; + struct arg_lit* invert; + struct arg_int* mode; + struct arg_end* end; } i2cdisp_args; -bool is_i2c_started(i2c_port_t port){ - esp_err_t ret = ESP_OK; - ESP_LOGD(TAG,"Determining if i2c is started on port %u", port); - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - ret = i2c_master_start(cmd); - if(ret == ESP_OK){ - ret = i2c_master_write_byte(cmd,WRITE_BIT, ACK_CHECK_EN); +bool is_i2c_started (i2c_port_t port) { + esp_err_t ret = ESP_OK; + ESP_LOGD (TAG, "Determining if i2c is started on port %u", port); + i2c_cmd_handle_t cmd = i2c_cmd_link_create (); + ret = i2c_master_start (cmd); + if (ret == ESP_OK) { + ret = i2c_master_write_byte (cmd, WRITE_BIT, ACK_CHECK_EN); } - if(ret == ESP_OK){ - ret = i2c_master_stop(cmd); + if (ret == ESP_OK) { + ret = i2c_master_stop (cmd); } - if(ret == ESP_OK){ - ret = i2c_master_cmd_begin(port, cmd, 50 / portTICK_RATE_MS); + if (ret == ESP_OK) { + ret = i2c_master_cmd_begin (port, cmd, 50 / portTICK_RATE_MS); } - i2c_cmd_link_delete(cmd); - ESP_LOGD(TAG,"i2c is %s. %s",ret!=ESP_ERR_INVALID_STATE?"started":"not started", esp_err_to_name(ret)); - return (ret!=ESP_ERR_INVALID_STATE); + i2c_cmd_link_delete (cmd); + ESP_LOGD (TAG, "i2c is %s. %s", ret != ESP_ERR_INVALID_STATE ? "started" : "not started", esp_err_to_name (ret)); + return (ret != ESP_ERR_INVALID_STATE); } typedef struct { - uint8_t address; - const char * description; + uint8_t address; + const char* description; } i2c_db_t; - // the list was taken from https://i2cdevices.org/addresses // on 2020-01-16 static const i2c_db_t i2c_db[] = { -{ .address = 0x00, .description="Unknown"}, -{ .address = 0x01, .description="Unknown"}, -{ .address = 0x02, .description="Unknown"}, -{ .address = 0x03, .description="Unknown"}, -{ .address = 0x04, .description="Unknown"}, -{ .address = 0x05, .description="Unknown"}, -{ .address = 0x06, .description="Unknown"}, -{ .address = 0x07, .description="Unknown"}, -{ .address = 0x08, .description="Unknown"}, -{ .address = 0x0c, .description="AK8975"}, -{ .address = 0x0d, .description="AK8975"}, -{ .address = 0x0e, .description="MAG3110 AK8975 IST-8310"}, -{ .address = 0x0f, .description="AK8975"}, -{ .address = 0x10, .description="VEML7700 VML6075 VEML6075 ES8388"}, -{ .address = 0x11, .description="Si4713 SAA5246 SAA5243P/K SAA5243P/L SAA5243P/E SAA5243P/H ES8388"}, -{ .address = 0x12, .description="SEN-17374"}, -{ .address = 0x13, .description="VCNL40x0 SEN-17374"}, -{ .address = 0x18, .description="MCP9808 LIS3DH LSM303 COM-15093"}, -{ .address = 0x19, .description="MCP9808 LIS3DH LSM303 COM-15093"}, -{ .address = 0x1a, .description="AC101 MCP9808"}, -{ .address = 0x1b, .description="MCP9808"}, -{ .address = 0x1c, .description="MCP9808 MMA845x FXOS8700"}, -{ .address = 0x1d, .description="MCP9808 MMA845x ADXL345 FXOS8700"}, -{ .address = 0x1e, .description="HMC5883 LSM303 MCP9808 LSM303 FXOS8700"}, -{ .address = 0x1f, .description="MCP9808 FXOS8700"}, -{ .address = 0x20, .description="TCA9554 MCP23008 MA12070P MCP23017 Chirp! FXAS21002"}, -{ .address = 0x21, .description="FXAS21002 MCP23008 MCP23017 SAA4700 MA12070P TCA9554"}, -{ .address = 0x22, .description="ES8388 MCP23008 MCP23017 PCA1070 MA12070P TCA9554"}, -{ .address = 0x23, .description="MCP23008 MCP23017 SAA4700 MA12070P TCA9554"}, -{ .address = 0x24, .description="TCA9554 MCP23008 PCD3312C MCP23017 PCD3311C"}, -{ .address = 0x25, .description="TCA9554 MCP23008 PCD3312C MCP23017 PCD3311C"}, -{ .address = 0x26, .description="MCP23008 MCP23017 TCA9554"}, -{ .address = 0x27, .description="MCP23008 MCP23017 HIH6130 TCA9554"}, -{ .address = 0x28, .description="BNO055 CAP1188"}, -{ .address = 0x29, .description="BNO055 VL53L0x VL6180X CAP1188 TCS34725 TSL2591"}, -{ .address = 0x2a, .description="CAP1188"}, -{ .address = 0x2b, .description="CAP1188"}, -{ .address = 0x2c, .description="CAP1188 AD5248 AD5251 AD5252 CAT5171"}, -{ .address = 0x2d, .description="CAP1188 AD5248 AD5251 AD5252 CAT5171"}, -{ .address = 0x2e, .description="AD5248 AD5251 AD5252 LPS22HB"}, -{ .address = 0x2f, .description="AD5248 AD5243 AD5251 AD5252"}, -{ .address = 0x30, .description="SAA2502"}, -{ .address = 0x31, .description="SAA2502"}, -{ .address = 0x33, .description="MLX90640"}, -{ .address = 0x38, .description="FT6x06 VEML6070 BMA150 SAA1064 SEN-15892 PCF8574AP"}, -{ .address = 0x39, .description="TSL2561 APDS-9960 VEML6070 SAA1064 PCF8574AP"}, -{ .address = 0x3a, .description="PCF8577C SAA1064 PCF8574AP"}, -{ .address = 0x3b, .description="SAA1064 PCF8569 PCF8574AP"}, -{ .address = 0x3c, .description="SSD1305 SSD1306 PCF8578 PCF8569 SH1106 PCF8574AP"}, -{ .address = 0x3d, .description="SSD1305 SSD1306 PCF8578 SH1106 PCF8574AP"}, -{ .address = 0x3e, .description="PCF8574AP"}, -{ .address = 0x3f, .description="PCF8574AP"}, -{ .address = 0x40, .description="Si7021 HTU21D-F TMP007 TMP006 PCA9685 INA219 TEA6330 TEA6300 TDA9860 TEA6320 TDA8421 NE5751 INA260 PCF8574"}, -{ .address = 0x41, .description="TMP007 TDA8421 TDA8424 STMPE610 PCF8574 STMPE811 NE5751 INA260 TDA8425 TMP006 TDA9860 PCA9685 INA219 TDA8426"}, -{ .address = 0x42, .description="TMP007 TDA8417 HDC1008 PCF8574 INA260 TDA8415 TMP006 PCA9685 INA219"}, -{ .address = 0x43, .description="TMP007 HDC1008 PCF8574 INA260 TMP006 PCA9685 INA219"}, -{ .address = 0x44, .description="TMP007 TMP006 PCA9685 INA219 STMPE610 SHT31 ISL29125 STMPE811 TDA4688 TDA4672 TDA4780 TDA4670 TDA8442 TDA4687 TDA4671 TDA4680 INA260 PCF8574"}, -{ .address = 0x45, .description="TMP007 TDA7433 PCF8574 TDA8376 INA260 TMP006 PCA9685 INA219 SHT31"}, -{ .address = 0x46, .description="TMP007 PCF8574 TDA8370 INA260 TMP006 PCA9685 INA219 TDA9150"}, -{ .address = 0x47, .description="TMP007 PCF8574 INA260 TMP006 PCA9685 INA219"}, -{ .address = 0x48, .description="PCA9685 INA219 PN532 TMP102 INA260 ADS1115 PCF8574 ADS7828"}, -{ .address = 0x49, .description="TSL2561 PCA9685 INA219 TMP102 INA260 ADS1115 AS7262 PCF8574 ADS7828"}, -{ .address = 0x4a, .description="ADS7828 PCF8574 ADS1115 INA260 PCA9685 MAX44009 INA219 TMP102"}, -{ .address = 0x4b, .description="ADS7828 PCF8574 ADS1115 INA260 PCA9685 MAX44009 INA219 TMP102"}, -{ .address = 0x4c, .description="PCA9685 INA219 INA260 PCF8574"}, -{ .address = 0x4d, .description="PCA9685 INA219 INA260 PCF8574"}, -{ .address = 0x4e, .description="PCA9685 INA219 INA260 PCF8574"}, -{ .address = 0x4f, .description="PCA9685 INA219 INA260 PCF8574"}, -{ .address = 0x50, .description="PCA9685 MB85RC"}, -{ .address = 0x51, .description="PCA9685 MB85RC VCNL4200"}, -{ .address = 0x52, .description="PCA9685 MB85RC Nunchuck controller APDS-9250 SI1133"}, -{ .address = 0x53, .description="ADXL345 PCA9685 MB85RC"}, -{ .address = 0x54, .description="PCA9685 MB85RC"}, -{ .address = 0x55, .description="PCA9685 MB85RC MAX30101 SI1133"}, -{ .address = 0x56, .description="PCA9685 MB85RC"}, -{ .address = 0x57, .description="PCA9685 MB85RC MAX3010x"}, -{ .address = 0x58, .description="PCA9685 TPA2016 SGP30"}, -{ .address = 0x59, .description="PCA9685"}, -{ .address = 0x5a, .description="MPR121 MLX90614 CCS811 PCA9685 DRV2605"}, -{ .address = 0x5b, .description="PCA9685 CCS811 MPR121"}, -{ .address = 0x5c, .description="PCA9685 AM2315 MPR121"}, -{ .address = 0x5d, .description="PCA9685 MPR121"}, -{ .address = 0x5e, .description="PCA9685"}, -{ .address = 0x5f, .description="PCA9685 HTS221"}, -{ .address = 0x60, .description="SI1132 Si5351A ATECC608A TSA5511 ATECC508A SAB3035 MCP4725A0 SAB3037 PCA9685 MCP4725A1 TEA5767 MPL3115A2 MPL115A2 Si1145"}, -{ .address = 0x61, .description="Si5351A TSA5511 SAB3035 MCP4725A0 SAB3037 TEA6100 PCA9685 MCP4725A1"}, -{ .address = 0x62, .description="SCD40-D-R2 TSA5511 SAB3035 UMA1014T SAB3037 PCA9685 MCP4725A1"}, -{ .address = 0x63, .description="Si4713 TSA5511 SAB3035 UMA1014T SAB3037 PCA9685 MCP4725A1"}, -{ .address = 0x64, .description="PCA9685 MCP4725A2 MCP4725A1"}, -{ .address = 0x65, .description="PCA9685 MCP4725A2 MCP4725A1"}, -{ .address = 0x66, .description="PCA9685 MCP4725A3 IS31FL3731 MCP4725A1"}, -{ .address = 0x67, .description="PCA9685 MCP4725A3 MCP4725A1"}, -{ .address = 0x68, .description="MPU-9250 ICM-20948 MPU6050 AMG8833 DS3231 PCA9685 PCF8573 PCF8523 DS1307 ITG3200"}, -{ .address = 0x69, .description="MPU-9250 ICM-20948 MPU6050 AMG8833 PCA9685 PCF8573 ITG3200 SPS30"}, -{ .address = 0x6a, .description="PCA9685 L3GD20H PCF8573"}, -{ .address = 0x6b, .description="PCA9685 L3GD20H PCF8573"}, -{ .address = 0x6c, .description="PCA9685"}, -{ .address = 0x6d, .description="PCA9685"}, -{ .address = 0x6e, .description="PCA9685"}, -{ .address = 0x6f, .description="PCA9685 MCP7940N"}, -{ .address = 0x70, .description="PCA9685 TCA9548 HT16K33 SHTC3"}, -{ .address = 0x71, .description="PCA9685 TCA9548 HT16K33"}, -{ .address = 0x72, .description="PCA9685 TCA9548 HT16K33"}, -{ .address = 0x73, .description="PCA9685 TCA9548 HT16K33"}, -{ .address = 0x74, .description="PCA9685 TCA9548 HT16K33"}, -{ .address = 0x75, .description="PCA9685 TCA9548 HT16K33"}, -{ .address = 0x76, .description="BME688 BME680 MS5611 MS5607 HT16K33 PCA9685 BME280 BMP280 TCA9548"}, -{ .address = 0x77, .description="PCA9685 TCA9548 HT16K33 IS31FL3731 BME280 BMP280 MS5607 BMP180 BMP085 BMA180 MS5611 BME680 BME688"}, -{ .address = 0x78, .description="PCA9685"}, -{ .address = 0x79, .description="PCA9685"}, -{ .address = 0x7a, .description="PCA9685"}, -{ .address = 0x7b, .description="PCA9685"}, -{ .address = 0x7c, .description="PCA9685"}, -{ .address = 0x7d, .description="PCA9685"}, -{ .address = 0x7e, .description="PCA9685"}, -{ .address = 0x7f, .description="PCA9685"}, - { .address = 0, .description = NULL} -}; + {.address = 0x00, .description = "Unknown"}, + {.address = 0x01, .description = "Unknown"}, + {.address = 0x02, .description = "Unknown"}, + {.address = 0x03, .description = "Unknown"}, + {.address = 0x04, .description = "Unknown"}, + {.address = 0x05, .description = "Unknown"}, + {.address = 0x06, .description = "Unknown"}, + {.address = 0x07, .description = "Unknown"}, + {.address = 0x08, .description = "Unknown"}, + {.address = 0x0c, .description = "AK8975"}, + {.address = 0x0d, .description = "AK8975"}, + {.address = 0x0e, .description = "MAG3110 AK8975 IST-8310"}, + {.address = 0x0f, .description = "AK8975"}, + {.address = 0x10, .description = "VEML7700 VML6075 VEML6075 ES8388"}, + {.address = 0x11, .description = "Si4713 SAA5246 SAA5243P/K SAA5243P/L SAA5243P/E SAA5243P/H ES8388"}, + {.address = 0x12, .description = "SEN-17374"}, + {.address = 0x13, .description = "VCNL40x0 SEN-17374"}, + {.address = 0x18, .description = "MCP9808 LIS3DH LSM303 COM-15093"}, + {.address = 0x19, .description = "MCP9808 LIS3DH LSM303 COM-15093"}, + {.address = 0x1a, .description = "AC101 MCP9808"}, + {.address = 0x1b, .description = "MCP9808"}, + {.address = 0x1c, .description = "MCP9808 MMA845x FXOS8700"}, + {.address = 0x1d, .description = "MCP9808 MMA845x ADXL345 FXOS8700"}, + {.address = 0x1e, .description = "HMC5883 LSM303 MCP9808 LSM303 FXOS8700"}, + {.address = 0x1f, .description = "MCP9808 FXOS8700"}, + {.address = 0x20, .description = "TCA9554 MCP23008 MA12070P MCP23017 Chirp! FXAS21002"}, + {.address = 0x21, .description = "FXAS21002 MCP23008 MCP23017 SAA4700 MA12070P TCA9554"}, + {.address = 0x22, .description = "ES8388 MCP23008 MCP23017 PCA1070 MA12070P TCA9554"}, + {.address = 0x23, .description = "MCP23008 MCP23017 SAA4700 MA12070P TCA9554"}, + {.address = 0x24, .description = "TCA9554 MCP23008 PCD3312C MCP23017 PCD3311C"}, + {.address = 0x25, .description = "TCA9554 MCP23008 PCD3312C MCP23017 PCD3311C"}, + {.address = 0x26, .description = "MCP23008 MCP23017 TCA9554"}, + {.address = 0x27, .description = "MCP23008 MCP23017 HIH6130 TCA9554"}, + {.address = 0x28, .description = "BNO055 CAP1188"}, + {.address = 0x29, .description = "BNO055 VL53L0x VL6180X CAP1188 TCS34725 TSL2591"}, + {.address = 0x2a, .description = "CAP1188"}, + {.address = 0x2b, .description = "CAP1188"}, + {.address = 0x2c, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"}, + {.address = 0x2d, .description = "CAP1188 AD5248 AD5251 AD5252 CAT5171"}, + {.address = 0x2e, .description = "AD5248 AD5251 AD5252 LPS22HB"}, + {.address = 0x2f, .description = "AD5248 AD5243 AD5251 AD5252"}, + {.address = 0x30, .description = "SAA2502"}, + {.address = 0x31, .description = "SAA2502"}, + {.address = 0x33, .description = "MLX90640"}, + {.address = 0x38, .description = "FT6x06 VEML6070 BMA150 SAA1064 SEN-15892 PCF8574AP"}, + {.address = 0x39, .description = "TSL2561 APDS-9960 VEML6070 SAA1064 PCF8574AP"}, + {.address = 0x3a, .description = "PCF8577C SAA1064 PCF8574AP"}, + {.address = 0x3b, .description = "SAA1064 PCF8569 PCF8574AP"}, + {.address = 0x3c, .description = "SSD1305 SSD1306 PCF8578 PCF8569 SH1106 PCF8574AP"}, + {.address = 0x3d, .description = "SSD1305 SSD1306 PCF8578 SH1106 PCF8574AP"}, + {.address = 0x3e, .description = "PCF8574AP"}, + {.address = 0x3f, .description = "PCF8574AP"}, + {.address = 0x40, .description = "Si7021 HTU21D-F TMP007 TMP006 PCA9685 INA219 TEA6330 TEA6300 TDA9860 TEA6320 TDA8421 NE5751 INA260 PCF8574"}, + {.address = 0x41, .description = "TMP007 TDA8421 TDA8424 STMPE610 PCF8574 STMPE811 NE5751 INA260 TDA8425 TMP006 TDA9860 PCA9685 INA219 TDA8426"}, + {.address = 0x42, .description = "TMP007 TDA8417 HDC1008 PCF8574 INA260 TDA8415 TMP006 PCA9685 INA219"}, + {.address = 0x43, .description = "TMP007 HDC1008 PCF8574 INA260 TMP006 PCA9685 INA219"}, + {.address = 0x44, .description = "TMP007 TMP006 PCA9685 INA219 STMPE610 SHT31 ISL29125 STMPE811 TDA4688 TDA4672 TDA4780 TDA4670 TDA8442 TDA4687 TDA4671 TDA4680 INA260 PCF8574"}, + {.address = 0x45, .description = "TMP007 TDA7433 PCF8574 TDA8376 INA260 TMP006 PCA9685 INA219 SHT31"}, + {.address = 0x46, .description = "TMP007 PCF8574 TDA8370 INA260 TMP006 PCA9685 INA219 TDA9150"}, + {.address = 0x47, .description = "TMP007 PCF8574 INA260 TMP006 PCA9685 INA219"}, + {.address = 0x48, .description = "PCA9685 INA219 PN532 TMP102 INA260 ADS1115 PCF8574 ADS7828"}, + {.address = 0x49, .description = "TSL2561 PCA9685 INA219 TMP102 INA260 ADS1115 AS7262 PCF8574 ADS7828"}, + {.address = 0x4a, .description = "ADS7828 PCF8574 ADS1115 INA260 PCA9685 MAX44009 INA219 TMP102"}, + {.address = 0x4b, .description = "ADS7828 PCF8574 ADS1115 INA260 PCA9685 MAX44009 INA219 TMP102"}, + {.address = 0x4c, .description = "PCA9685 INA219 INA260 PCF8574"}, + {.address = 0x4d, .description = "PCA9685 INA219 INA260 PCF8574"}, + {.address = 0x4e, .description = "PCA9685 INA219 INA260 PCF8574"}, + {.address = 0x4f, .description = "PCA9685 INA219 INA260 PCF8574"}, + {.address = 0x50, .description = "PCA9685 MB85RC"}, + {.address = 0x51, .description = "PCA9685 MB85RC VCNL4200"}, + {.address = 0x52, .description = "PCA9685 MB85RC Nunchuck controller APDS-9250 SI1133"}, + {.address = 0x53, .description = "ADXL345 PCA9685 MB85RC"}, + {.address = 0x54, .description = "PCA9685 MB85RC"}, + {.address = 0x55, .description = "PCA9685 MB85RC MAX30101 SI1133"}, + {.address = 0x56, .description = "PCA9685 MB85RC"}, + {.address = 0x57, .description = "PCA9685 MB85RC MAX3010x"}, + {.address = 0x58, .description = "PCA9685 TPA2016 SGP30"}, + {.address = 0x59, .description = "PCA9685"}, + {.address = 0x5a, .description = "MPR121 MLX90614 CCS811 PCA9685 DRV2605"}, + {.address = 0x5b, .description = "PCA9685 CCS811 MPR121"}, + {.address = 0x5c, .description = "PCA9685 AM2315 MPR121"}, + {.address = 0x5d, .description = "PCA9685 MPR121"}, + {.address = 0x5e, .description = "PCA9685"}, + {.address = 0x5f, .description = "PCA9685 HTS221"}, + {.address = 0x60, .description = "SI1132 Si5351A ATECC608A TSA5511 ATECC508A SAB3035 MCP4725A0 SAB3037 PCA9685 MCP4725A1 TEA5767 MPL3115A2 MPL115A2 Si1145"}, + {.address = 0x61, .description = "Si5351A TSA5511 SAB3035 MCP4725A0 SAB3037 TEA6100 PCA9685 MCP4725A1"}, + {.address = 0x62, .description = "SCD40-D-R2 TSA5511 SAB3035 UMA1014T SAB3037 PCA9685 MCP4725A1"}, + {.address = 0x63, .description = "Si4713 TSA5511 SAB3035 UMA1014T SAB3037 PCA9685 MCP4725A1"}, + {.address = 0x64, .description = "PCA9685 MCP4725A2 MCP4725A1"}, + {.address = 0x65, .description = "PCA9685 MCP4725A2 MCP4725A1"}, + {.address = 0x66, .description = "PCA9685 MCP4725A3 IS31FL3731 MCP4725A1"}, + {.address = 0x67, .description = "PCA9685 MCP4725A3 MCP4725A1"}, + {.address = 0x68, .description = "MPU-9250 ICM-20948 MPU6050 AMG8833 DS3231 PCA9685 PCF8573 PCF8523 DS1307 ITG3200"}, + {.address = 0x69, .description = "MPU-9250 ICM-20948 MPU6050 AMG8833 PCA9685 PCF8573 ITG3200 SPS30"}, + {.address = 0x6a, .description = "PCA9685 L3GD20H PCF8573"}, + {.address = 0x6b, .description = "PCA9685 L3GD20H PCF8573"}, + {.address = 0x6c, .description = "PCA9685"}, + {.address = 0x6d, .description = "PCA9685"}, + {.address = 0x6e, .description = "PCA9685"}, + {.address = 0x6f, .description = "PCA9685 MCP7940N"}, + {.address = 0x70, .description = "PCA9685 TCA9548 HT16K33 SHTC3"}, + {.address = 0x71, .description = "PCA9685 TCA9548 HT16K33"}, + {.address = 0x72, .description = "PCA9685 TCA9548 HT16K33"}, + {.address = 0x73, .description = "PCA9685 TCA9548 HT16K33"}, + {.address = 0x74, .description = "PCA9685 TCA9548 HT16K33"}, + {.address = 0x75, .description = "PCA9685 TCA9548 HT16K33"}, + {.address = 0x76, .description = "BME688 BME680 MS5611 MS5607 HT16K33 PCA9685 BME280 BMP280 TCA9548"}, + {.address = 0x77, .description = "PCA9685 TCA9548 HT16K33 IS31FL3731 BME280 BMP280 MS5607 BMP180 BMP085 BMA180 MS5611 BME680 BME688"}, + {.address = 0x78, .description = "PCA9685"}, + {.address = 0x79, .description = "PCA9685"}, + {.address = 0x7a, .description = "PCA9685"}, + {.address = 0x7b, .description = "PCA9685"}, + {.address = 0x7c, .description = "PCA9685"}, + {.address = 0x7d, .description = "PCA9685"}, + {.address = 0x7e, .description = "PCA9685"}, + {.address = 0x7f, .description = "PCA9685"}, + {.address = 0, .description = NULL}}; -const char * i2c_get_description(uint8_t address){ - uint8_t i=0; - while(i2c_db[i].description && i2c_db[i].address!=address) i++; - return i2c_db[i].description?i2c_db[i].description:"Unlisted"; +const char* i2c_get_description (uint8_t address) { + uint8_t i = 0; + while (i2c_db[i].description && i2c_db[i].address != address) + i++; + return i2c_db[i].description ? i2c_db[i].description : "Unlisted"; } -static esp_err_t i2c_get_port(int port, i2c_port_t *i2c_port) -{ +static esp_err_t i2c_get_port (int port, i2c_port_t* i2c_port) { if (port >= I2C_NUM_MAX) { - log_send_messaging(MESSAGING_ERROR,"Wrong port number: %d", port); + log_send_messaging (MESSAGING_ERROR, "Wrong port number: %d", port); return ESP_FAIL; } switch (port) { - case 0: - *i2c_port = I2C_NUM_0; - break; - case 1: - *i2c_port = I2C_NUM_1; - break; - default: - *i2c_port = I2C_NUM_0; - break; + case 0: + *i2c_port = I2C_NUM_0; + break; + case 1: + *i2c_port = I2C_NUM_1; + break; + default: + *i2c_port = I2C_NUM_0; + break; } 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_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)); +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 int do_i2c_set_display(int argc, char **argv) -{ - bool bHasi2cConfig = false, bHasSpiConfig=false; - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&i2cdisp_args); - display_config_t config = { - .back = -1, - .CS_pin = -1, - .RST_pin = -1, - .depth = 0 - }; - char * nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config"); - if (nvs_item && strlen(nvs_item)>0) { - bHasi2cConfig=true; - } - FREE_AND_NULL(nvs_item); - - nvs_item = config_alloc_get(NVS_TYPE_STR, "spi_config"); - if (nvs_item && strlen(nvs_item)>0) { - bHasSpiConfig=true; - } - FREE_AND_NULL(nvs_item); - - /* Check "--clear" option */ - if (i2cdisp_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"Display config cleared"); - config_set_value(NVS_TYPE_STR, "display_config", ""); - return 0; - } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream."); - return 1; - } - if(nerrors>0){ - arg_print_errors(f,i2cdisp_args.end,desc_display); - fclose(f); - return 1; - } - /* Check "--type" option */ - if (i2cdisp_args.type->count ) { - if(strcasecmp(i2c_name_type, i2cdisp_args.type->sval[0])==0){ - config.type = i2c_name_type; - } - else { - config.type = spi_name_type; - } - } - else { - config.type = i2c_name_type; - } - /* Check "--address" option */ - if(strcasecmp(config.type,"I2C")==0){ - if (i2cdisp_args.address->count>0) { - config.address=i2cdisp_args.address->ival[0]; - } - else { - config.address = 60; - } - if(!bHasi2cConfig){ - fprintf(f,"I2C bus has to be configured first. \n"); - nerrors++; - } - } - else { - // SPI Bus connection - if(!bHasSpiConfig){ - fprintf(f,"SPI bus has to be configured first. \n"); - nerrors++; - } - /* Check "--speed" option */ - if (i2cdisp_args.speed->count) { - config.speed=i2cdisp_args.speed->ival[0]; - } - else { - config.speed = 8000000; - } - /* Check "--cs" option */ - nerrors +=is_output_gpio(i2cdisp_args.cs,f,&config.CS_pin, false); - /* Check "--mode" option */ - if (i2cdisp_args.mode->count) { - config.mode=i2cdisp_args.mode->ival[0]; - } - else { - config.mode = 0; - } - } - - nerrors +=is_output_gpio(i2cdisp_args.reset,f,&config.RST_pin, false); - /* Check "--width" option */ - if (i2cdisp_args.width->count) { - config.width=i2cdisp_args.width->ival[0]; - } - /* Check "--height" option */ - if (i2cdisp_args.height->count) { - config.height=i2cdisp_args.height->ival[0]; - } - - /* Check "--driver" option */ - if (i2cdisp_args.driver->count) { - config.drivername=display_conf_get_driver_name(i2cdisp_args.driver->sval[0]) ; - } - if(i2cdisp_args.depth->count > 0 && strcasecmp(config.drivername,"SSD1326")==0) { - config.depth = i2cdisp_args.depth->ival[0]; - } - /* Check "--back" option */ - nerrors +=is_output_gpio(i2cdisp_args.back,f,&config.back, false); - if(!config.drivername || !display_is_valid_driver(config.drivername)){ - fprintf(f,"Unsupported display driver %s\n",config.drivername); - nerrors++; - } - if(!strcasestr(config.type,"I2C") && !strcasestr(config.type,"SPI")){ - fprintf(f,"Invalid display type %s\n",config.type); - nerrors++; - } - config.rotate = i2cdisp_args.rotate->count>0; - config.hflip = i2cdisp_args.hflip->count>0; - config.vflip = i2cdisp_args.vflip->count>0; - config.invert = i2cdisp_args.invert->count>0; - - if(nerrors==0){ - fprintf(f,"Saving display configuration\n"); - esp_err_t err = config_display_set(&config); - if(err!=ESP_OK){ - fprintf(f,"Error: %s\n",esp_err_to_name(err)); - nerrors++; - } - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return nerrors; +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 int do_i2c_set_display (int argc, char** argv) { + bool bHasi2cConfig = false, bHasSpiConfig = false; + int nerrors = arg_parse_msg (argc, argv, (struct arg_hdr**)&i2cdisp_args); + display_config_t config = { + .back = -1, + .CS_pin = -1, + .RST_pin = -1, + .depth = 0}; + char* nvs_item = config_alloc_get (NVS_TYPE_STR, "i2c_config"); + if (nvs_item && strlen (nvs_item) > 0) { + bHasi2cConfig = true; + } + FREE_AND_NULL (nvs_item); -static int do_spiconfig_cmd(int argc, char **argv){ - static spi_bus_config_t spi_config = { - .mosi_io_num = -1, - .sclk_io_num = -1, - .miso_io_num = -1, - .quadwp_io_num = -1, - .quadhd_io_num = -1 - }; - int dc=-1,host = 0; - esp_err_t err=ESP_OK; - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&spiconfig_args); + nvs_item = config_alloc_get (NVS_TYPE_STR, "spi_config"); + if (nvs_item && strlen (nvs_item) > 0) { + bHasSpiConfig = true; + } + FREE_AND_NULL (nvs_item); - /* Check "--clear" option */ - if (spiconfig_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"SPI config cleared.\n"); - config_set_value(NVS_TYPE_STR, "spi_config", ""); - return 0; - } + /* Check "--clear" option */ + if (i2cdisp_args.clear->count) { + cmd_send_messaging (argv[0], MESSAGING_WARNING, "Display config cleared"); + config_set_value (NVS_TYPE_STR, "display_config", ""); + return 0; + } + char* buf = NULL; + size_t buf_size = 0; + FILE* f = open_memstream (&buf, &buf_size); + if (f == NULL) { + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Unable to open memory stream."); + return 1; + } + if (nerrors > 0) { + arg_print_errors (f, i2cdisp_args.end, desc_display); + fclose (f); + return 1; + } + /* Check "--type" option */ + if (i2cdisp_args.type->count) { + if (strcasecmp (i2c_name_type, i2cdisp_args.type->sval[0]) == 0) { + config.type = i2c_name_type; + } else { + config.type = spi_name_type; + } + } else { + config.type = i2c_name_type; + } + /* Check "--address" option */ + if (strcasecmp (config.type, "I2C") == 0) { + if (i2cdisp_args.address->count > 0) { + config.address = i2cdisp_args.address->ival[0]; + } else { + config.address = 60; + } + if (!bHasi2cConfig) { + fprintf (f, "I2C bus has to be configured first. \n"); + nerrors++; + } + } else { + // SPI Bus connection + if (!bHasSpiConfig) { + fprintf (f, "SPI bus has to be configured first. \n"); + nerrors++; + } + /* Check "--speed" option */ + if (i2cdisp_args.speed->count) { + config.speed = i2cdisp_args.speed->ival[0]; + } else { + config.speed = 8000000; + } + /* Check "--cs" option */ + nerrors += is_output_gpio (i2cdisp_args.cs, f, &config.CS_pin, false); + /* Check "--mode" option */ + if (i2cdisp_args.mode->count) { + config.mode = i2cdisp_args.mode->ival[0]; + } else { + config.mode = 0; + } + } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors>0){ - arg_print_errors(f,spiconfig_args.end,desc_spiconfig); - fclose(f); - return 1; - } - /* Check "--clk" option */ - nerrors+=is_output_gpio(spiconfig_args.clk, f, &spi_config.sclk_io_num, true); - nerrors+=is_output_gpio(spiconfig_args.data, f, &spi_config.mosi_io_num, true); - nerrors+=is_output_gpio(spiconfig_args.miso, f, &spi_config.miso_io_num, true); - nerrors+=is_output_gpio(spiconfig_args.dc, f, &dc, true); - nerrors+=is_output_gpio(spiconfig_args.host, f, &host, true); + nerrors += is_output_gpio (i2cdisp_args.reset, f, &config.RST_pin, false); + /* Check "--width" option */ + if (i2cdisp_args.width->count) { + config.width = i2cdisp_args.width->ival[0]; + } + /* Check "--height" option */ + if (i2cdisp_args.height->count) { + config.height = i2cdisp_args.height->ival[0]; + } - if(!nerrors){ - fprintf(f,"Configuring SPI data=%d clock=%d host=%u dc: %d\n", spi_config.mosi_io_num, spi_config.sclk_io_num, host, dc); - err=spi_bus_initialize( host, &spi_config, SPI_DMA_CH_AUTO ); - if(err!=ESP_OK){ - if(err==ESP_ERR_INVALID_STATE){ - // if user is changing the host number, we need to try freeing both hosts - if((err = spi_bus_free(host))!=ESP_OK && (err = spi_bus_free(host==1?2:1))!=ESP_OK){ - fprintf(f,"SPI bus init failed. Please clear SPI configuration, restart the device and try again. %s\n", esp_err_to_name(err)); - nerrors++; - } - else if((err=spi_bus_initialize( host, &spi_config, SPI_DMA_CH_AUTO ))!=ESP_OK){ - fprintf(f,"Failed to initialize SPI Bus. %s\n", esp_err_to_name(err)); - nerrors++; - } - } - else { - fprintf(f,"SPI bus initialization failed. %s\n", esp_err_to_name(err)); - nerrors++; - } - } - } + /* Check "--driver" option */ + if (i2cdisp_args.driver->count) { + config.drivername = display_conf_get_driver_name (i2cdisp_args.driver->sval[0]); + } + if (i2cdisp_args.depth->count > 0 && strcasecmp (config.drivername, "SSD1326") == 0) { + config.depth = i2cdisp_args.depth->ival[0]; + } + /* Check "--back" option */ + nerrors += is_output_gpio (i2cdisp_args.back, f, &config.back, false); + if (!config.drivername || !display_is_valid_driver (config.drivername)) { + fprintf (f, "Unsupported display driver %s\n", config.drivername); + nerrors++; + } + if (!strcasestr (config.type, "I2C") && !strcasestr (config.type, "SPI")) { + fprintf (f, "Invalid display type %s\n", config.type); + nerrors++; + } + config.rotate = i2cdisp_args.rotate->count > 0; + config.hflip = i2cdisp_args.hflip->count > 0; + config.vflip = i2cdisp_args.vflip->count > 0; + config.invert = i2cdisp_args.invert->count > 0; - if(!nerrors){ - fprintf(f,"Storing SPI parameters.\n"); - nerrors += (config_spi_set(&spi_config, host, dc)!=ESP_OK); - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); - return nerrors; + if (nerrors == 0) { + fprintf (f, "Saving display configuration\n"); + esp_err_t err = config_display_set (&config); + if (err != ESP_OK) { + fprintf (f, "Error: %s\n", esp_err_to_name (err)); + nerrors++; + } + } + if (!nerrors) { + fprintf (f, "Done.\n"); + } + fflush (f); + cmd_send_messaging (argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose (f); + FREE_AND_NULL (buf); + return nerrors; } +static int do_spiconfig_cmd (int argc, char** argv) { + static spi_bus_config_t spi_config = { + .mosi_io_num = -1, + .sclk_io_num = -1, + .miso_io_num = -1, + .quadwp_io_num = -1, + .quadhd_io_num = -1}; + int dc = -1, host = 0; + esp_err_t err = ESP_OK; + int nerrors = arg_parse_msg (argc, argv, (struct arg_hdr**)&spiconfig_args); -static int do_i2cconfig_cmd(int argc, char **argv) -{ - esp_err_t err=ESP_OK; - i2c_config_t conf = { + /* Check "--clear" option */ + if (spiconfig_args.clear->count) { + cmd_send_messaging (argv[0], MESSAGING_WARNING, "SPI config cleared.\n"); + config_set_value (NVS_TYPE_STR, "spi_config", ""); + return 0; + } + + char* buf = NULL; + size_t buf_size = 0; + FILE* f = open_memstream (&buf, &buf_size); + if (f == NULL) { + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n"); + return 1; + } + if (nerrors > 0) { + arg_print_errors (f, spiconfig_args.end, desc_spiconfig); + fclose (f); + return 1; + } + /* Check "--clk" option */ + nerrors += is_output_gpio (spiconfig_args.clk, f, &spi_config.sclk_io_num, true); + nerrors += is_output_gpio (spiconfig_args.data, f, &spi_config.mosi_io_num, true); + nerrors += is_output_gpio (spiconfig_args.miso, f, &spi_config.miso_io_num, true); + nerrors += is_output_gpio (spiconfig_args.dc, f, &dc, true); + nerrors += is_output_gpio (spiconfig_args.host, f, &host, true); + + if (!nerrors) { + fprintf (f, "Configuring SPI data=%d clock=%d host=%u dc: %d\n", spi_config.mosi_io_num, spi_config.sclk_io_num, host, dc); + err = spi_bus_initialize (host, &spi_config, SPI_DMA_CH_AUTO); + if (err != ESP_OK) { + if (err == ESP_ERR_INVALID_STATE) { + // if user is changing the host number, we need to try freeing both hosts + if ((err = spi_bus_free (host)) != ESP_OK && (err = spi_bus_free (host == 1 ? 2 : 1)) != ESP_OK) { + fprintf (f, "SPI bus init failed. Please clear SPI configuration, restart the device and try again. %s\n", esp_err_to_name (err)); + nerrors++; + } else if ((err = spi_bus_initialize (host, &spi_config, SPI_DMA_CH_AUTO)) != ESP_OK) { + fprintf (f, "Failed to initialize SPI Bus. %s\n", esp_err_to_name (err)); + nerrors++; + } + } else { + fprintf (f, "SPI bus initialization failed. %s\n", esp_err_to_name (err)); + nerrors++; + } + } + } + + if (!nerrors) { + fprintf (f, "Storing SPI parameters.\n"); + nerrors += (config_spi_set (&spi_config, host, dc) != ESP_OK); + } + if (!nerrors) { + fprintf (f, "Done.\n"); + } + fflush (f); + cmd_send_messaging (argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose (f); + FREE_AND_NULL (buf); + return nerrors; +} + +static int do_i2cconfig_cmd (int argc, char** argv) { + esp_err_t err = ESP_OK; + i2c_config_t conf = { .mode = I2C_MODE_MASTER, .sda_io_num = 19, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_io_num = 18, .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = 100000 - }; - - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&i2cconfig_args); - /* Check "--clear" option */ - if (i2cconfig_args.clear->count) { - cmd_send_messaging(argv[0],MESSAGING_WARNING,"i2c config cleared\n"); - config_set_value(NVS_TYPE_STR, "i2c_config", ""); - return 0; - } + .master.clk_speed = 100000}; - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - if(nerrors>0){ - arg_print_errors(f,i2cconfig_args.end,desc_i2c); - fclose(f); - return 1; - } - /* Check "--port" option */ - if (i2cconfig_args.port->count) { - if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) { - fprintf(f,"Invalid port %u \n",i2cconfig_args.port->ival[0]); - nerrors ++; - } - } - /* Check "--freq" option */ - if (i2cconfig_args.freq->count) { - conf.master.clk_speed = i2cconfig_args.freq->ival[0]; - } + int nerrors = arg_parse_msg (argc, argv, (struct arg_hdr**)&i2cconfig_args); + /* Check "--clear" option */ + if (i2cconfig_args.clear->count) { + cmd_send_messaging (argv[0], MESSAGING_WARNING, "i2c config cleared\n"); + config_set_value (NVS_TYPE_STR, "i2c_config", ""); + return 0; + } - nerrors +=is_output_gpio(i2cconfig_args.sda,f,&conf.sda_io_num, true); - nerrors +=is_output_gpio(i2cconfig_args.scl,f,&conf.scl_io_num, true); + char* buf = NULL; + size_t buf_size = 0; + FILE* f = open_memstream (&buf, &buf_size); + if (f == NULL) { + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n"); + return 1; + } + if (nerrors > 0) { + arg_print_errors (f, i2cconfig_args.end, desc_i2c); + fclose (f); + return 1; + } + /* Check "--port" option */ + if (i2cconfig_args.port->count) { + if (i2c_get_port (i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) { + fprintf (f, "Invalid port %u \n", i2cconfig_args.port->ival[0]); + nerrors++; + } + } + /* Check "--freq" option */ + if (i2cconfig_args.freq->count) { + conf.master.clk_speed = i2cconfig_args.freq->ival[0]; + } + + nerrors += is_output_gpio (i2cconfig_args.sda, f, &conf.sda_io_num, true); + nerrors += is_output_gpio (i2cconfig_args.scl, f, &conf.scl_io_num, true); #ifdef CONFIG_SQUEEZEAMP - if (i2c_port == I2C_NUM_0) { - i2c_port = I2C_NUM_1; - fprintf(f,"can't use i2c port 0 on SqueezeAMP. Changing to port 1.\n"); - } + if (i2c_port == I2C_NUM_0) { + i2c_port = I2C_NUM_1; + fprintf (f, "can't use i2c port 0 on SqueezeAMP. Changing to port 1.\n"); + } #endif - if(!nerrors){ - fprintf(f,"Uninstalling i2c driver from port %u if needed\n",i2c_port); - if(is_i2c_started(i2c_port)){ - if((err=i2c_driver_delete(i2c_port))!=ESP_OK){ - fprintf(f,"i2c driver delete failed. %s\n", esp_err_to_name(err)); - nerrors++; - } - } - } - if(!nerrors){ - if((err=i2c_master_driver_initialize(argv[0],&conf))==ESP_OK){ - if((err=i2c_master_driver_install(argv[0]))!=ESP_OK){ - nerrors++; - } - else { - fprintf(f,"i2c driver successfully started.\n"); - } - } - else { - nerrors++; - } - } - if(!nerrors ){ - fprintf(f,"Storing i2c parameters.\n"); - config_i2c_set(&conf, i2c_port); - } - if(!nerrors ){ - fprintf(f,"Done.\n"); - } - fflush (f); - cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); + if (!nerrors) { + fprintf (f, "Uninstalling i2c driver from port %u if needed\n", i2c_port); + if (is_i2c_started (i2c_port)) { + if ((err = i2c_driver_delete (i2c_port)) != ESP_OK) { + fprintf (f, "i2c driver delete failed. %s\n", esp_err_to_name (err)); + nerrors++; + } + } + } + if (!nerrors) { + if ((err = i2c_master_driver_initialize (argv[0], &conf)) == ESP_OK) { + if ((err = i2c_master_driver_install (argv[0])) != ESP_OK) { + nerrors++; + } else { + fprintf (f, "i2c driver successfully started.\n"); + } + } else { + nerrors++; + } + } + if (!nerrors) { + fprintf (f, "Storing i2c parameters.\n"); + config_i2c_set (&conf, i2c_port); + } + if (!nerrors) { + fprintf (f, "Done.\n"); + } + fflush (f); + cmd_send_messaging (argv[0], nerrors > 0 ? MESSAGING_ERROR : MESSAGING_INFO, "%s", buf); + fclose (f); + FREE_AND_NULL (buf); - return nerrors; + return nerrors; } -static int do_i2cdump_cmd(int argc, char **argv) -{ - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&i2cdump_args); +static int do_i2cdump_cmd (int argc, char** argv) { + int nerrors = arg_parse_msg (argc, argv, (struct arg_hdr**)&i2cdump_args); if (nerrors != 0) { return 1; } @@ -616,84 +595,84 @@ static int do_i2cdump_cmd(int argc, char **argv) if (i2cdump_args.size->count) { size = i2cdump_args.size->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 = i2c_port; + if (i2cset_args.port->count && i2c_get_port (i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) { + return 1; } if (size != 1 && size != 2 && size != 4) { - cmd_send_messaging(argv[0],MESSAGING_ERROR, "Wrong read size. Only support 1,2,4\n"); + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Wrong read size. Only support 1,2,4\n"); + return 1; + } + char* buf = NULL; + size_t buf_size = 0; + FILE* f = open_memstream (&buf, &buf_size); + if (f == NULL) { + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n"); return 1; } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } uint8_t data_addr; uint8_t data[4]; int32_t block[16]; - fprintf(f,"\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f" - " 0123456789abcdef\r\n"); + fprintf (f, + "\n 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f" + " 0123456789abcdef\r\n"); for (int i = 0; i < 128; i += 16) { - fprintf(f,"%02x: ", i); + fprintf (f, "%02x: ", i); for (int j = 0; j < 16; j += size) { - fflush(stdout); + fflush (stdout); data_addr = i + j; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); - i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN); + i2c_cmd_handle_t cmd = i2c_cmd_link_create (); + i2c_master_start (cmd); + i2c_master_write_byte (cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); + i2c_master_write_byte (cmd, data_addr, ACK_CHECK_EN); + i2c_master_start (cmd); + i2c_master_write_byte (cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN); if (size > 1) { - i2c_master_read(cmd, data, size - 1, ACK_VAL); + i2c_master_read (cmd, data, size - 1, ACK_VAL); } - i2c_master_read_byte(cmd, data + size - 1, NACK_VAL); - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 50 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); + i2c_master_read_byte (cmd, data + size - 1, NACK_VAL); + i2c_master_stop (cmd); + esp_err_t ret = i2c_master_cmd_begin (loc_i2c_port, cmd, 50 / portTICK_RATE_MS); + i2c_cmd_link_delete (cmd); if (ret == ESP_OK) { for (int k = 0; k < size; k++) { - fprintf(f,"%02x ", data[k]); + fprintf (f, "%02x ", data[k]); block[j + k] = data[k]; } } else { for (int k = 0; k < size; k++) { - fprintf(f,"XX "); + fprintf (f, "XX "); block[j + k] = -1; } } } - fprintf(f," "); + fprintf (f, " "); for (int k = 0; k < 16; k++) { if (block[k] < 0) { - fprintf(f,"X"); + fprintf (f, "X"); } if ((block[k] & 0xff) == 0x00 || (block[k] & 0xff) == 0xff) { - fprintf(f,"."); + fprintf (f, "."); } else if ((block[k] & 0xff) < 32 || (block[k] & 0xff) >= 127) { - fprintf(f,"?"); + fprintf (f, "?"); } else { - fprintf(f,"%c", block[k] & 0xff); + fprintf (f, "%c", block[k] & 0xff); } } - fprintf(f,"\r\n"); + fprintf (f, "\r\n"); } // Don't stop the driver; our firmware may be using it for screen, etc - //i2c_driver_delete(i2c_port); - fflush (f); - cmd_send_messaging(argv[0],MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); + // i2c_driver_delete(i2c_port); + fflush (f); + cmd_send_messaging (argv[0], MESSAGING_INFO, "%s", buf); + fclose (f); + FREE_AND_NULL (buf); return 0; } -static int do_i2cset_cmd(int argc, char **argv) -{ - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&i2cset_args); +static int do_i2cset_cmd (int argc, char** argv) { + int nerrors = arg_parse_msg (argc, argv, (struct arg_hdr**)&i2cset_args); if (nerrors != 0) { return 1; } @@ -706,41 +685,40 @@ static int do_i2cset_cmd(int argc, char **argv) data_addr = i2cset_args.register_address->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 = i2c_port; + if (i2cset_args.port->count && i2c_get_port (i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) { + return 1; } /* Check data: "-d" option */ int len = i2cset_args.data->count; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); + i2c_cmd_handle_t cmd = i2c_cmd_link_create (); + i2c_master_start (cmd); + i2c_master_write_byte (cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); if (i2cset_args.register_address->count) { - i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN); + i2c_master_write_byte (cmd, data_addr, ACK_CHECK_EN); } for (int i = 0; i < len; i++) { - i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN); + i2c_master_write_byte (cmd, i2cset_args.data->ival[i], ACK_CHECK_EN); } - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); + i2c_master_stop (cmd); + esp_err_t ret = i2c_master_cmd_begin (loc_i2c_port, cmd, 1000 / portTICK_RATE_MS); + i2c_cmd_link_delete (cmd); if (ret == ESP_OK) { - cmd_send_messaging(argv[0],MESSAGING_INFO, "i2c Write OK\n"); + cmd_send_messaging (argv[0], MESSAGING_INFO, "i2c Write OK\n"); } else if (ret == ESP_ERR_TIMEOUT) { - cmd_send_messaging(argv[0],MESSAGING_WARNING, "i2c Bus is busy\n"); + cmd_send_messaging (argv[0], MESSAGING_WARNING, "i2c Bus is busy\n"); } else { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"i2c Read failed\n"); + cmd_send_messaging (argv[0], MESSAGING_ERROR, "i2c Read failed\n"); } // Don't stop the driver; our firmware may be using it for screen, etc - //i2c_driver_delete(i2c_port); + // i2c_driver_delete(i2c_port); return 0; } -static int do_i2cget_cmd(int argc, char **argv) -{ - int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&i2cget_args); +static int do_i2cget_cmd (int argc, char** argv) { + int nerrors = arg_parse_msg (argc, argv, (struct arg_hdr**)&i2cget_args); if (nerrors != 0) { return 1; } @@ -757,404 +735,385 @@ 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 = i2c_port; + if (i2cset_args.port->count && i2c_get_port (i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) { + return 1; } - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n"); - return 1; - } - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - uint8_t *data = malloc_init_external(len); + char* buf = NULL; + size_t buf_size = 0; + FILE* f = open_memstream (&buf, &buf_size); + if (f == NULL) { + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Unable to open memory stream.\n"); + return 1; + } + i2c_cmd_handle_t cmd = i2c_cmd_link_create (); + i2c_master_start (cmd); + uint8_t* data = malloc_init_external (len); if (data_addr != -1) { - i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); - i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN); - i2c_master_start(cmd); + i2c_master_write_byte (cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN); + i2c_master_write_byte (cmd, data_addr, ACK_CHECK_EN); + i2c_master_start (cmd); } - i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN); + i2c_master_write_byte (cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN); if (len > 1) { - i2c_master_read(cmd, data, len - 1, ACK_VAL); + i2c_master_read (cmd, data, len - 1, ACK_VAL); } - i2c_master_read_byte(cmd, data + len - 1, NACK_VAL); - i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 1000 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); + i2c_master_read_byte (cmd, data + len - 1, NACK_VAL); + i2c_master_stop (cmd); + esp_err_t ret = i2c_master_cmd_begin (loc_i2c_port, cmd, 1000 / portTICK_RATE_MS); + i2c_cmd_link_delete (cmd); if (ret == ESP_OK) { for (int i = 0; i < len; i++) { - fprintf(f,"0x%02x ", data[i]); + fprintf (f, "0x%02x ", data[i]); if ((i + 1) % 16 == 0) { - fprintf(f,"\r\n"); + fprintf (f, "\r\n"); } } if (len % 16) { - fprintf(f,"\r\n"); + fprintf (f, "\r\n"); } } else if (ret == ESP_ERR_TIMEOUT) { - cmd_send_messaging(argv[0],MESSAGING_WARNING, "i2c Bus is busy\n"); + cmd_send_messaging (argv[0], MESSAGING_WARNING, "i2c Bus is busy\n"); } else { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"i2c Read failed\n"); + cmd_send_messaging (argv[0], MESSAGING_ERROR, "i2c Read failed\n"); } - free(data); + free (data); // Don't stop the driver; our firmware may be using it for screen, etc - //i2c_driver_delete(i2c_port); - fflush (f); - cmd_send_messaging(argv[0],MESSAGING_INFO,"%s", buf); - fclose(f); - FREE_AND_NULL(buf); + // i2c_driver_delete(i2c_port); + fflush (f); + cmd_send_messaging (argv[0], MESSAGING_INFO, "%s", buf); + fclose (f); + FREE_AND_NULL (buf); return 0; } -esp_err_t cmd_i2ctools_scan_bus(FILE *f,int sda, int scl){ - uint8_t matches[128]={}; - int last_match=0; - esp_err_t ret = ESP_OK; - - if(!GPIO_IS_VALID_GPIO(scl) || !GPIO_IS_VALID_GPIO(sda)){ - fprintf(f,"Invalid GPIO. Cannot scan bus\n"); - return 1; - } - - // configure i2c - i2c_config_t i2c_config = { - .mode = I2C_MODE_MASTER, - .sda_io_num = -1, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_io_num = -1, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = 250000, - }; +esp_err_t cmd_i2ctools_scan_bus (FILE* f, int sda, int scl) { + uint8_t matches[128] = {}; + int last_match = 0; + esp_err_t ret = ESP_OK; - i2c_config.sda_io_num = sda; - i2c_config.scl_io_num = scl; - // we have an I2C configured - i2c_port_t i2c_port = 0; - // make sure that we don't have an i2c driver running - i2c_driver_delete(i2c_port); - ret = i2c_param_config(i2c_port, &i2c_config); - if (ret != ESP_OK) { - fprintf(f,"I2C Param Config failed %s\n", esp_err_to_name(ret)); - return ret; - } - ret=i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false); - if (ret != ESP_OK) { - fprintf(f,"I2C driver install failed %s\n", esp_err_to_name(ret)); - return ret; - } - for (int i = 0; i < 128 ; i ++) { + if (!GPIO_IS_VALID_GPIO (scl) || !GPIO_IS_VALID_GPIO (sda)) { + fprintf (f, "Invalid GPIO. Cannot scan bus\n"); + return 1; + } - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (i << 1) | WRITE_BIT, ACK_CHECK_EN); - i2c_master_stop(cmd); - ret = i2c_master_cmd_begin(i2c_port, cmd, 50 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); - if (ret == ESP_OK) { - matches[++last_match-1] = i; - } + // configure i2c + i2c_config_t i2c_config = { + .mode = I2C_MODE_MASTER, + .sda_io_num = -1, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = -1, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = 250000, + }; + + i2c_config.sda_io_num = sda; + i2c_config.scl_io_num = scl; + // we have an I2C configured + i2c_port_t i2c_port = 0; + // make sure that we don't have an i2c driver running + i2c_driver_delete (i2c_port); + ret = i2c_param_config (i2c_port, &i2c_config); + if (ret != ESP_OK) { + fprintf (f, "I2C Param Config failed %s\n", esp_err_to_name (ret)); + return ret; } - i2c_driver_delete(i2c_port); - if(last_match) { - fprintf(f,"i2c device detected (names provided by https://i2cdevices.org/addresses).\n"); - for(int i=0;icount && i2c_get_port(i2cset_args.port->ival[0], &loc_i2c_port) != ESP_OK) { - return 1; +static int do_i2cdetect_cmd (int argc, char** argv) { + uint8_t matches[128] = {}; + int last_match = 0; + esp_err_t ret = ESP_OK; + 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; } uint8_t address; - char *buf = NULL; - size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); - if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream."); - return 1; - } + char* buf = NULL; + size_t buf_size = 0; + FILE* f = open_memstream (&buf, &buf_size); + if (f == NULL) { + cmd_send_messaging (argv[0], MESSAGING_ERROR, "Unable to open memory stream."); + return 1; + } - - fprintf(f,"\n 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); - for (int i = 0; i < 128 ; i += 16) { - fprintf(f,"%02x: ", i); - for (int j = 0; j < 16 ; j++) { + fprintf (f, "\n 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); + for (int i = 0; i < 128; i += 16) { + fprintf (f, "%02x: ", i); + for (int j = 0; j < 16; j++) { address = i + j; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN); - i2c_master_stop(cmd); - ret = i2c_master_cmd_begin(loc_i2c_port, cmd, 50 / portTICK_RATE_MS); - i2c_cmd_link_delete(cmd); + i2c_cmd_handle_t cmd = i2c_cmd_link_create (); + i2c_master_start (cmd); + i2c_master_write_byte (cmd, (address << 1) | WRITE_BIT, ACK_CHECK_EN); + i2c_master_stop (cmd); + ret = i2c_master_cmd_begin (loc_i2c_port, cmd, 50 / portTICK_RATE_MS); + i2c_cmd_link_delete (cmd); if (ret == ESP_OK) { - fprintf(f,"%02x ", address); - matches[++last_match-1] = address; + fprintf (f, "%02x ", address); + matches[++last_match - 1] = address; } else if (ret == ESP_ERR_TIMEOUT) { - fprintf(f,"UU "); + fprintf (f, "UU "); } else { - fprintf(f,"-- "); + fprintf (f, "-- "); } } - fprintf(f,"\r\n"); - + fprintf (f, "\r\n"); } - if(last_match) { - fprintf(f,"\r\n------------------------------------------------------------------------------------" - "\r\nDetected the following devices (names provided by https://i2cdevices.org/addresses)."); + if (last_match) { + fprintf (f, + "\r\n------------------------------------------------------------------------------------" + "\r\nDetected the following devices (names provided by https://i2cdevices.org/addresses)."); - for(int i=0;iwidth>0){ - cJSON_AddNumberToObject(values,"width",conf->width); - } - if(conf->height>0) { - cJSON_AddNumberToObject(values,"height",conf->height); - } - if(conf->address>0){ - cJSON_AddNumberToObject(values,"address",conf->address); - } - if(conf->RST_pin>=0) { - cJSON_AddNumberToObject(values,"reset",conf->RST_pin); - } - if(conf->drivername && strlen(conf->drivername)>0){ - cJSON_AddStringToObject(values,"driver",conf->drivername); - } - if(conf->CS_pin>=0) { - cJSON_AddNumberToObject(values,"cs",conf->CS_pin); - } - if(conf->speed>0){ - cJSON_AddNumberToObject(values,"speed",conf->speed); - } - if(conf->back>=0){ - cJSON_AddNumberToObject(values,"back",conf->back); - } - if(conf->depth>0){ - cJSON_AddNumberToObject(values,"depth",conf->depth); - } - if(conf->type && strlen(conf->type)){ - cJSON_AddStringToObject(values,"type",conf->type); - } - cJSON_AddBoolToObject(values,"rotate",conf->rotate); - cJSON_AddBoolToObject(values,"hf",conf->hflip); - cJSON_AddBoolToObject(values,"vf",conf->vflip); - cJSON_AddBoolToObject(values,"invert",conf->invert); - if(conf->mode>=0){ - cJSON_AddNumberToObject(values,"mode",conf->mode); - } - } - return values; +cJSON* i2c_set_display_cb () { + cJSON* values = cJSON_CreateObject (); + const display_config_t* conf = config_display_get (); + if (conf) { + if (conf->width > 0) { + cJSON_AddNumberToObject (values, "width", conf->width); + } + if (conf->height > 0) { + cJSON_AddNumberToObject (values, "height", conf->height); + } + if (conf->address > 0) { + cJSON_AddNumberToObject (values, "address", conf->address); + } + if (conf->RST_pin >= 0) { + cJSON_AddNumberToObject (values, "reset", conf->RST_pin); + } + if (conf->drivername && strlen (conf->drivername) > 0) { + cJSON_AddStringToObject (values, "driver", conf->drivername); + } + if (conf->CS_pin >= 0) { + cJSON_AddNumberToObject (values, "cs", conf->CS_pin); + } + if (conf->speed > 0) { + cJSON_AddNumberToObject (values, "speed", conf->speed); + } + if (conf->back >= 0) { + cJSON_AddNumberToObject (values, "back", conf->back); + } + if (conf->depth > 0) { + cJSON_AddNumberToObject (values, "depth", conf->depth); + } + if (conf->type && strlen (conf->type)) { + cJSON_AddStringToObject (values, "type", conf->type); + } + cJSON_AddBoolToObject (values, "rotate", conf->rotate); + cJSON_AddBoolToObject (values, "hf", conf->hflip); + cJSON_AddBoolToObject (values, "vf", conf->vflip); + cJSON_AddBoolToObject (values, "invert", conf->invert); + if (conf->mode >= 0) { + cJSON_AddNumberToObject (values, "mode", conf->mode); + } + } + return values; } -static void register_i2c_set_display(){ - char * supported_drivers = display_get_supported_drivers(); +static void register_i2c_set_display () { + char* supported_drivers = display_get_supported_drivers (); - i2cdisp_args.width = arg_int1("w", "width", "", "Width"); - i2cdisp_args.height = arg_int1("h", "height", "", "Height"); - i2cdisp_args.address = arg_int0("a", "address", "", "I2C address (default 60)"); - i2cdisp_args.reset = arg_int0(NULL, "reset", "", "Reset GPIO"); - i2cdisp_args.hflip = arg_lit0(NULL, "hf", "Flip horizontally"); - i2cdisp_args.vflip = arg_lit0(NULL, "vf", "Flip vertically"); - i2cdisp_args.driver = arg_str0("d", "driver", supported_drivers?supported_drivers:"", "Driver"); - i2cdisp_args.cs = arg_int0("b", "cs", "","SPI Only. CS GPIO (for SPI displays)"); - i2cdisp_args.speed = arg_int0("s", "speed", "","SPI Only. Bus Speed (Default 8000000). SPI interface can work up to 26MHz~40MHz"); - i2cdisp_args.back = arg_int0("b", "back", "","Backlight GPIO (if applicable)"); - i2cdisp_args.depth = arg_int0("p", "depth", "-1|1|4", "Bit Depth (only for SSD1326 displays)"); - i2cdisp_args.type = arg_str0("t", "type", "", "Interface (default I2C)"); - i2cdisp_args.rotate = arg_lit0("r", "rotate", "Rotate 180 degrees"); - i2cdisp_args.invert = arg_lit0("i", "invert", "Invert colors"); - i2cdisp_args.clear = arg_lit0(NULL, "clear", "clear configuration and return"); - i2cdisp_args.mode = arg_int0("m", "mode", "","SPI Only. Transaction Line Mode (Default 0)"); - i2cdisp_args.end = arg_end(8); - const esp_console_cmd_t i2c_set_display= { - .command = CFG_TYPE_HW("display"), - .help=desc_display, - .hint = NULL, - .func = &do_i2c_set_display, - .argtable = &i2cdisp_args - }; - cmd_to_json_with_cb(&i2c_set_display,&i2c_set_display_cb); - ESP_ERROR_CHECK(esp_console_cmd_register(&i2c_set_display)); + i2cdisp_args.width = arg_int1 ("w", "width", "", "Width"); + i2cdisp_args.height = arg_int1 ("h", "height", "", "Height"); + i2cdisp_args.address = arg_int0 ("a", "address", "", "I2C address (default 60)"); + i2cdisp_args.reset = arg_int0 (NULL, "reset", "", "Reset GPIO"); + i2cdisp_args.hflip = arg_lit0 (NULL, "hf", "Flip horizontally"); + i2cdisp_args.vflip = arg_lit0 (NULL, "vf", "Flip vertically"); + i2cdisp_args.driver = arg_str0 ("d", "driver", supported_drivers ? supported_drivers : "", "Driver"); + i2cdisp_args.cs = arg_int0 ("b", "cs", "", "SPI Only. CS GPIO (for SPI displays)"); + i2cdisp_args.speed = arg_int0 ("s", "speed", "", "SPI Only. Bus Speed (Default 8000000). SPI interface can work up to 26MHz~40MHz"); + i2cdisp_args.back = arg_int0 ("b", "back", "", "Backlight GPIO (if applicable)"); + i2cdisp_args.depth = arg_int0 ("p", "depth", "-1|1|4", "Bit Depth (only for SSD1326 displays)"); + i2cdisp_args.type = arg_str0 ("t", "type", "", "Interface (default I2C)"); + i2cdisp_args.rotate = arg_lit0 ("r", "rotate", "Rotate 180 degrees"); + i2cdisp_args.invert = arg_lit0 ("i", "invert", "Invert colors"); + i2cdisp_args.clear = arg_lit0 (NULL, "clear", "clear configuration and return"); + i2cdisp_args.mode = arg_int0 ("m", "mode", "", "SPI Only. Transaction Line Mode (Default 0)"); + i2cdisp_args.end = arg_end (8); + const esp_console_cmd_t i2c_set_display = { + .command = CFG_TYPE_HW ("display"), + .help = desc_display, + .hint = NULL, + .func = &do_i2c_set_display, + .argtable = &i2cdisp_args}; + cmd_to_json_with_cb (&i2c_set_display, &i2c_set_display_cb); + ESP_ERROR_CHECK (esp_console_cmd_register (&i2c_set_display)); } -static void register_i2cdectect(void) -{ +static void register_i2cdectect (void) { 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); - ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdetect_cmd)); + .argtable = NULL}; + cmd_to_json (&i2cdetect_cmd); + ESP_ERROR_CHECK (esp_console_cmd_register (&i2cdetect_cmd)); } -static void register_i2cget(void) -{ - i2cget_args.chip_address = arg_int1("c", "chip", "", "Specify the address of the chip on that bus"); - i2cget_args.register_address = arg_int0("r", "register", "", "Specify the address on that chip to read from"); - i2cget_args.data_length = arg_int0("l", "length", "", "Specify the length to read from that data address"); - i2cget_args.end = arg_end(1); +static void register_i2cget (void) { + i2cget_args.chip_address = arg_int1 ("c", "chip", "", "Specify the address of the chip on that bus"); + i2cget_args.register_address = arg_int0 ("r", "register", "", "Specify the address on that chip to read from"); + i2cget_args.data_length = arg_int0 ("l", "length", "", "Specify the length to read from that data address"); + i2cget_args.end = arg_end (1); const esp_console_cmd_t i2cget_cmd = { .command = "i2cget", .help = "Read registers visible through the I2C bus", .hint = NULL, .func = &do_i2cget_cmd, - .argtable = &i2cget_args - }; - cmd_to_json(&i2cget_cmd); - ESP_ERROR_CHECK(esp_console_cmd_register(&i2cget_cmd)); + .argtable = &i2cget_args}; + cmd_to_json (&i2cget_cmd); + ESP_ERROR_CHECK (esp_console_cmd_register (&i2cget_cmd)); } -static void register_i2cset(void) -{ - i2cset_args.chip_address = arg_int1("c", "chip", "", "Specify the address of the chip on that bus"); - i2cset_args.register_address = arg_int0("r", "register", "", "Specify the address on that chip to read from"); - i2cset_args.data = arg_intn(NULL, NULL, "", 0, 256, "Specify the data to write to that data address"); - i2cset_args.port = arg_intn("p","port","",0,1,"Specify the i2c port (0|2)"); +static void register_i2cset (void) { + i2cset_args.chip_address = arg_int1 ("c", "chip", "", "Specify the address of the chip on that bus"); + i2cset_args.register_address = arg_int0 ("r", "register", "", "Specify the address on that chip to read from"); + i2cset_args.data = arg_intn (NULL, NULL, "", 0, 256, "Specify the data to write to that data address"); + i2cset_args.port = arg_intn ("p", "port", "", 0, 1, "Specify the i2c port (0|2)"); - i2cset_args.end = arg_end(2); + i2cset_args.end = arg_end (2); const esp_console_cmd_t i2cset_cmd = { .command = "i2cset", .help = "Set registers visible through the I2C bus", .hint = NULL, .func = &do_i2cset_cmd, - .argtable = &i2cset_args - }; - cmd_to_json(&i2cset_cmd); - ESP_ERROR_CHECK(esp_console_cmd_register(&i2cset_cmd)); + .argtable = &i2cset_args}; + cmd_to_json (&i2cset_cmd); + ESP_ERROR_CHECK (esp_console_cmd_register (&i2cset_cmd)); } -static void register_i2cdump(void) -{ - i2cdump_args.chip_address = arg_int1("c", "chip", "", "Specify the address of the chip on that bus"); - i2cdump_args.size = arg_int0("s", "size", "", "Specify the size of each read"); - i2cdump_args.end = arg_end(3); +static void register_i2cdump (void) { + i2cdump_args.chip_address = arg_int1 ("c", "chip", "", "Specify the address of the chip on that bus"); + i2cdump_args.size = arg_int0 ("s", "size", "", "Specify the size of each read"); + i2cdump_args.end = arg_end (3); const esp_console_cmd_t i2cdump_cmd = { .command = "i2cdump", .help = "Examine registers visible through the I2C bus", .hint = NULL, .func = &do_i2cdump_cmd, - .argtable = &i2cdump_args - }; - cmd_to_json(&i2cdump_cmd); - ESP_ERROR_CHECK(esp_console_cmd_register(&i2cdump_cmd)); + .argtable = &i2cdump_args}; + cmd_to_json (&i2cdump_cmd); + ESP_ERROR_CHECK (esp_console_cmd_register (&i2cdump_cmd)); } - -cJSON * i2config_cb(){ - cJSON * values = cJSON_CreateObject(); - int i2c_port; - const i2c_config_t * i2c= config_i2c_get(&i2c_port); - if(i2c->scl_io_num>0) { - cJSON_AddNumberToObject(values,"scl",i2c->scl_io_num); - } - if(i2c->sda_io_num>0) { - cJSON_AddNumberToObject(values,"sda",i2c->sda_io_num); - } - if(i2c->master.clk_speed>0) { - cJSON_AddNumberToObject(values,"speed",i2c->master.clk_speed); - } - if(i2c_port>=0) { - cJSON_AddNumberToObject(values,"port",i2c_port); - } - return values; +cJSON* i2config_cb () { + cJSON* values = cJSON_CreateObject (); + int i2c_port; + const i2c_config_t* i2c = config_i2c_get (&i2c_port); + if (i2c->scl_io_num > 0) { + cJSON_AddNumberToObject (values, "scl", i2c->scl_io_num); + } + if (i2c->sda_io_num > 0) { + cJSON_AddNumberToObject (values, "sda", i2c->sda_io_num); + } + if (i2c->master.clk_speed > 0) { + cJSON_AddNumberToObject (values, "speed", i2c->master.clk_speed); + } + if (i2c_port >= 0) { + cJSON_AddNumberToObject (values, "port", i2c_port); + } + return values; } -cJSON * spiconfig_cb(){ - cJSON * values = cJSON_CreateObject(); - const spi_bus_config_t * spi_config= config_spi_get(NULL); - if(spi_config->mosi_io_num>0){ - cJSON_AddNumberToObject(values,"data",spi_config->mosi_io_num); - } - if(spi_config->sclk_io_num>0){ - cJSON_AddNumberToObject(values,"clk",spi_config->sclk_io_num); - } - if(spi_system_dc_gpio>0){ - cJSON_AddNumberToObject(values,"dc",spi_system_dc_gpio); - } - if(spi_system_host>0){ - cJSON_AddNumberToObject(values,"host",spi_system_host); - } - return values; +cJSON* spiconfig_cb () { + cJSON* values = cJSON_CreateObject (); + const spi_bus_config_t* spi_config = config_spi_get (NULL); + if (spi_config->mosi_io_num > 0) { + cJSON_AddNumberToObject (values, "data", spi_config->mosi_io_num); + } + if (spi_config->sclk_io_num > 0) { + cJSON_AddNumberToObject (values, "clk", spi_config->sclk_io_num); + } + if (spi_system_dc_gpio > 0) { + cJSON_AddNumberToObject (values, "dc", spi_system_dc_gpio); + } + if (spi_system_host > 0) { + cJSON_AddNumberToObject (values, "host", spi_system_host); + } + return values; } -static void register_spiconfig(void) -{ - spiconfig_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); - spiconfig_args.clk = arg_int0("k", "clk", "", "Clock GPIO"); - spiconfig_args.data = arg_int0("d","data", "","Data OUT GPIO"); - spiconfig_args.miso = arg_int0("d","miso", "","Data IN GPIO"); - spiconfig_args.dc = arg_int0("c","dc", "", "DC GPIO"); - spiconfig_args.host= arg_int0("h", "host", "1|2", "SPI Host Number"); - spiconfig_args.end = arg_end(4); +static void register_spiconfig (void) { + spiconfig_args.clear = arg_lit0 (NULL, "clear", "Clear configuration"); + spiconfig_args.clk = arg_int0 ("k", "clk", "", "Clock GPIO"); + spiconfig_args.data = arg_int0 ("d", "data", "", "Data OUT GPIO"); + spiconfig_args.miso = arg_int0 ("d", "miso", "", "Data IN GPIO"); + spiconfig_args.dc = arg_int0 ("c", "dc", "", "DC GPIO"); + spiconfig_args.host = arg_int0 ("h", "host", "1|2", "SPI Host Number"); + spiconfig_args.end = arg_end (4); const esp_console_cmd_t spiconfig_cmd = { - .command = CFG_TYPE_HW("spi"), + .command = CFG_TYPE_HW ("spi"), .help = desc_spiconfig, .hint = NULL, .func = &do_spiconfig_cmd, - .argtable = &spiconfig_args - }; - cmd_to_json_with_cb(&spiconfig_cmd,&spiconfig_cb); - ESP_ERROR_CHECK(esp_console_cmd_register(&spiconfig_cmd)); + .argtable = &spiconfig_args}; + cmd_to_json_with_cb (&spiconfig_cmd, &spiconfig_cb); + ESP_ERROR_CHECK (esp_console_cmd_register (&spiconfig_cmd)); } -static void register_i2cconfig(void) -{ - i2cconfig_args.clear = arg_lit0(NULL, "clear", "Clear configuration"); - i2cconfig_args.port = arg_int0("p", "port", "0|1", "Port"); - i2cconfig_args.freq = arg_int0("f", "speed", "int", "Frequency (Hz) e.g. 100000"); - i2cconfig_args.sda = arg_int0("d", "sda", "", "SDA GPIO. e.g. 19"); - i2cconfig_args.scl = arg_int0("c", "scl", "", "SCL GPIO. e.g. 18"); - i2cconfig_args.end = arg_end(4); +static void register_i2cconfig (void) { + i2cconfig_args.clear = arg_lit0 (NULL, "clear", "Clear configuration"); + i2cconfig_args.port = arg_int0 ("p", "port", "0|1", "Port"); + i2cconfig_args.freq = arg_int0 ("f", "speed", "int", "Frequency (Hz) e.g. 100000"); + i2cconfig_args.sda = arg_int0 ("d", "sda", "", "SDA GPIO. e.g. 19"); + i2cconfig_args.scl = arg_int0 ("c", "scl", "", "SCL GPIO. e.g. 18"); + i2cconfig_args.end = arg_end (4); const esp_console_cmd_t i2cconfig_cmd = { - .command = CFG_TYPE_HW("i2c"), + .command = CFG_TYPE_HW ("i2c"), .help = desc_i2c, .hint = NULL, .func = &do_i2cconfig_cmd, - .argtable = &i2cconfig_args - }; - cmd_to_json_with_cb(&i2cconfig_cmd,&i2config_cb); - ESP_ERROR_CHECK(esp_console_cmd_register(&i2cconfig_cmd)); + .argtable = &i2cconfig_args}; + cmd_to_json_with_cb (&i2cconfig_cmd, &i2config_cb); + ESP_ERROR_CHECK (esp_console_cmd_register (&i2cconfig_cmd)); } -void register_i2ctools(void) -{ - register_i2cconfig(); - register_spiconfig(); - register_i2cdectect(); - register_i2cget(); - register_i2cset(); - register_i2cdump(); - register_i2c_set_display(); -} +void register_i2ctools (void) { + register_i2cconfig (); + register_spiconfig (); + register_i2cdectect (); + register_i2cget (); + register_i2cset (); + register_i2cdump (); + register_i2c_set_display (); +} \ No newline at end of file diff --git a/components/platform_console/cmd_system.c b/components/platform_console/cmd_system.c index 45df5192..cffe39b3 100644 --- a/components/platform_console/cmd_system.c +++ b/components/platform_console/cmd_system.c @@ -73,24 +73,42 @@ static void register_set_services(); static void register_tasks(); #endif extern BaseType_t network_manager_task; +FILE * system_open_memstream(const char * cmdname,char **buf,size_t *buf_size){ + FILE *f = open_memstream(buf, buf_size); + if (f == NULL) { + cmd_send_messaging(cmdname,MESSAGING_ERROR,"Unable to open memory stream."); + } + return f; +} void register_system() { - register_free(); + + register_setdevicename(); register_set_services(); + register_free(); register_heap(); register_dump_heap(); - register_setdevicename(); register_version(); register_restart(); - register_deep_sleep(); - register_light_sleep(); register_factory_boot(); register_restart_ota(); #if WITH_TASKS_INFO register_tasks(); #endif +#if CONFIG_WITH_CONFIG_UI + register_deep_sleep(); + register_light_sleep(); +#endif +} +void simple_restart() +{ + log_send_messaging(MESSAGING_WARNING,"Rebooting."); + if(!wait_for_commit()){ + log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration. "); + } + vTaskDelay(750/ portTICK_PERIOD_MS); + esp_restart(); } - /* 'version' command */ static int get_version(int argc, char **argv) { @@ -128,36 +146,23 @@ esp_err_t guided_boot(esp_partition_subtype_t partition_subtype) { if(is_recovery_running){ if(partition_subtype ==ESP_PARTITION_SUBTYPE_APP_FACTORY){ - log_send_messaging(MESSAGING_WARNING,"RECOVERY application is already active"); - if(!wait_for_commit()){ - log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration. "); - } - - vTaskDelay(750/ portTICK_PERIOD_MS); - esp_restart(); - return ESP_OK; + // log_send_messaging(MESSAGING_WARNING,"RECOVERY application is already active"); + simple_restart(); } } else { if(partition_subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){ - log_send_messaging(MESSAGING_WARNING,"SQUEEZELITE application is already active"); - if(!wait_for_commit()){ - log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration. "); - } - - vTaskDelay(750/ portTICK_PERIOD_MS); - esp_restart(); - return ESP_OK; + // log_send_messaging(MESSAGING_WARNING,"SQUEEZELITE application is already active"); + simple_restart(); } } esp_err_t err = ESP_OK; - bool bFound=false; - log_send_messaging(MESSAGING_INFO, "Looking for partition type %u",partition_subtype); + // log_send_messaging(MESSAGING_INFO, "Looking for partition type %u",partition_subtype); const esp_partition_t *partition; esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, partition_subtype, NULL); if(it == NULL){ - log_send_messaging(MESSAGING_ERROR,"Reboot failed. Cannot iterate through partitions"); + log_send_messaging(MESSAGING_ERROR,"Reboot failed. Partitions error"); } else { @@ -166,15 +171,11 @@ esp_err_t guided_boot(esp_partition_subtype_t partition_subtype) ESP_LOGD(TAG, "Releasing partition iterator"); esp_partition_iterator_release(it); if(partition != NULL){ - log_send_messaging(MESSAGING_INFO, "Found application partition %s sub type %u", partition->label,partition_subtype); + log_send_messaging(MESSAGING_INFO, "Rebooting to %s", partition->label); err=esp_ota_set_boot_partition(partition); if(err!=ESP_OK){ - bFound=false; log_send_messaging(MESSAGING_ERROR,"Unable to select partition for reboot: %s",esp_err_to_name(err)); } - else{ - bFound=true; - } } else { @@ -183,13 +184,7 @@ esp_err_t guided_boot(esp_partition_subtype_t partition_subtype) } ESP_LOGD(TAG, "Yielding to other processes"); taskYIELD(); - if(bFound) { - if(!wait_for_commit()){ - log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration changes. "); - } - vTaskDelay(750/ portTICK_PERIOD_MS); - esp_restart(); - } + simple_restart(); } return ESP_OK; @@ -197,46 +192,31 @@ esp_err_t guided_boot(esp_partition_subtype_t partition_subtype) static int restart(int argc, char **argv) { - log_send_messaging(MESSAGING_WARNING, "\n\nPerforming a simple restart to the currently active partition."); - if(!wait_for_commit()){ - cmd_send_messaging(argv[0],MESSAGING_WARNING,"Unable to commit configuration. "); - } - vTaskDelay(750/ portTICK_PERIOD_MS); - esp_restart(); + simple_restart(); return 0; } -void simple_restart() -{ - log_send_messaging(MESSAGING_WARNING,"System reboot requested."); - if(!wait_for_commit()){ - log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration. "); - } - - vTaskDelay(750/ portTICK_PERIOD_MS); - esp_restart(); -} esp_err_t guided_restart_ota(){ - log_send_messaging(MESSAGING_WARNING,"System reboot to Application requested"); + log_send_messaging(MESSAGING_WARNING,"Booting to Squeezelite"); guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0); return ESP_FAIL; // return fail. This should never return... we're rebooting! } esp_err_t guided_factory(){ - log_send_messaging(MESSAGING_WARNING,"System reboot to recovery requested"); + log_send_messaging(MESSAGING_WARNING,"Booting to recovery"); guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY); return ESP_FAIL; // return fail. This should never return... we're rebooting! } static int restart_factory(int argc, char **argv) { - cmd_send_messaging(argv[0],MESSAGING_WARNING, "Executing guided boot into recovery"); + cmd_send_messaging(argv[0],MESSAGING_WARNING, "Booting to Recovery"); guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY); return 0; // return fail. This should never return... we're rebooting! } static int restart_ota(int argc, char **argv) { - cmd_send_messaging(argv[0],MESSAGING_WARNING, "Executing guided boot into ota app 0"); + cmd_send_messaging(argv[0],MESSAGING_WARNING, "Booting to Squeezelite"); guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0); return 0; // return fail. This should never return... we're rebooting! } @@ -248,7 +228,9 @@ static void register_restart() .hint = NULL, .func = &restart, }; +#if CONFIG_WITH_CONFIG_UI cmd_to_json(&cmd); +#endif ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); } static void register_restart_ota() @@ -259,7 +241,9 @@ static void register_restart_ota() .hint = NULL, .func = &restart_ota, }; +#if CONFIG_WITH_CONFIG_UI cmd_to_json(&cmd); +#endif ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); } @@ -271,7 +255,9 @@ static void register_factory_boot() .hint = NULL, .func = &restart_factory, }; +#if CONFIG_WITH_CONFIG_UI cmd_to_json(&cmd); +#endif ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); } /** 'free' command prints available heap memory */ @@ -287,11 +273,14 @@ static void register_free() { const esp_console_cmd_t cmd = { .command = "free", - .help = "Get the current size of free heap memory", + .help = "Get free heap memory", .hint = NULL, .func = &free_mem, }; +#if CONFIG_WITH_CONFIG_UI cmd_to_json(&cmd); +#endif + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); } static int dump_heap(int argc, char **argv) @@ -303,16 +292,16 @@ static int dump_heap(int argc, char **argv) /* 'heap' command prints minumum heap size */ static int heap_size(int argc, char **argv) { - ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)", - heap_caps_get_free_size(MALLOC_CAP_INTERNAL), - heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL), - heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), - heap_caps_get_free_size(MALLOC_CAP_SPIRAM), - heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM), - heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), - heap_caps_get_free_size(MALLOC_CAP_DMA), - heap_caps_get_minimum_free_size(MALLOC_CAP_DMA), - heap_caps_get_largest_free_block(MALLOC_CAP_DMA)); + // ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)", + // heap_caps_get_free_size(MALLOC_CAP_INTERNAL), + // heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL), + // heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), + // heap_caps_get_free_size(MALLOC_CAP_SPIRAM), + // heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM), + // heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), + // heap_caps_get_free_size(MALLOC_CAP_DMA), + // heap_caps_get_minimum_free_size(MALLOC_CAP_DMA), + // heap_caps_get_largest_free_block(MALLOC_CAP_DMA)); cmd_send_messaging(argv[0],MESSAGING_INFO,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)", heap_caps_get_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL), @@ -457,9 +446,8 @@ static int setdevicename(int argc, char **argv) char *buf = NULL; size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); + FILE *f = system_open_memstream(argv[0],&buf, &buf_size); if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream."); return 1; } nerrors+=setnamevar("a2dp_dev_name", f, name); @@ -488,11 +476,13 @@ static void register_heap() { const esp_console_cmd_t heap_cmd = { .command = "heap", - .help = "Get minimum size of free heap memory found during execution", + .help = "Get minimum size of free heap memory", .hint = NULL, .func = &heap_size, }; +#if CONFIG_WITH_CONFIG_UI cmd_to_json(&heap_cmd); +#endif ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) ); } @@ -521,6 +511,7 @@ static void register_setdevicename() .func = &setdevicename, .argtable = &name_args }; + cmd_to_json_with_cb(&set_name,&setdevicename_cb); ESP_ERROR_CHECK(esp_console_cmd_register(&set_name)); } @@ -618,9 +609,7 @@ static void register_deep_sleep() const esp_console_cmd_t cmd = { .command = "deep_sleep", - .help = "Enter deep sleep mode. " - "Two wakeup modes are supported: timer and GPIO. " - "If no wakeup option is specified, will sleep indefinitely.", + .help = "Enter deep sleep mode. ", .hint = NULL, .func = &deep_sleep, .argtable = &deep_sleep_args @@ -649,9 +638,8 @@ static int do_set_services(int argc, char **argv) } char *buf = NULL; size_t buf_size = 0; - FILE *f = open_memstream(&buf, &buf_size); + FILE *f = system_open_memstream(argv[0],&buf, &buf_size); if (f == NULL) { - cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream."); return 1; } @@ -674,7 +662,7 @@ static int do_set_services(int argc, char **argv) if(err!=ESP_OK){ nerrors++; - fprintf(f,"Error setting telnet service to %s. %s\n",set_services_args.telnet->sval[0], esp_err_to_name(err)); + fprintf(f,"Error setting telnet to %s. %s\n",set_services_args.telnet->sval[0], esp_err_to_name(err)); } else { fprintf(f,"Telnet service changed to %s\n",set_services_args.telnet->sval[0]); @@ -706,7 +694,6 @@ cJSON * set_services_cb(){ #if WITH_TASKS_INFO console_set_bool_parameter(values,"stats",set_services_args.stats); #endif - if ((p = config_alloc_get(NVS_TYPE_STR, "telnet_enable")) != NULL) { if(strcasestr("YX",p)!=NULL){ cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet Only"); @@ -717,7 +704,6 @@ cJSON * set_services_cb(){ else { cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Disabled"); } - FREE_AND_NULL(p); } diff --git a/components/platform_console/cmd_system.h b/components/platform_console/cmd_system.h index f3c6ad33..4a6898d2 100644 --- a/components/platform_console/cmd_system.h +++ b/components/platform_console/cmd_system.h @@ -17,6 +17,7 @@ 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 } diff --git a/components/platform_console/cmd_wifi.c b/components/platform_console/cmd_wifi.c index 048a1cf5..60eb481a 100644 --- a/components/platform_console/cmd_wifi.c +++ b/components/platform_console/cmd_wifi.c @@ -204,8 +204,10 @@ void register_wifi_join() void register_wifi() { + #ifdef WIFI_CMDLINE register_wifi_join(); if(bypass_network_manager){ initialise_wifi(); } +#endif } diff --git a/components/services/accessors.h b/components/services/accessors.h index 5382239e..67360f2f 100644 --- a/components/services/accessors.h +++ b/components/services/accessors.h @@ -13,7 +13,7 @@ #include "driver/i2s.h" #include "driver/spi_master.h" #include "gpio_exp.h" - +#include "cJSON.h" extern const char *i2c_name_type; extern const char *spi_name_type; diff --git a/components/wifi-manager/webapp/package.json b/components/wifi-manager/webapp/package.json index 1d5ac53e..33a10c33 100644 --- a/components/wifi-manager/webapp/package.json +++ b/components/wifi-manager/webapp/package.json @@ -22,7 +22,6 @@ "babel": "^6.23.0", "babel-loader": "^8.2.3", "babel-runtime": "^6.26.0", - "bootswatch": "file:src/bootswatch", "clean-webpack-plugin": "^4.0.0", "commander": "^8.3.0", "compression-webpack-plugin": "^9.2.0", @@ -75,8 +74,10 @@ "@babel/runtime": "^7.16.7", "async-mutex": "^0.3.2", "bootstrap": "^5.1.3", + "bootswatch": "^5.3.2", "jquery": "^3.6.0", "npm": "^10.1.0", + "optipng-bin": "^9.0.0", "popper.js": "^1.16.1", "webpack-visualizer-plugin": "^0.1.11", "webpack-visualizer-plugin2": "^1.0.0" diff --git a/components/wifi-manager/webapp/src/index.ejs b/components/wifi-manager/webapp/src/index.ejs index 8b70bcbc..2a86f720 100644 --- a/components/wifi-manager/webapp/src/index.ejs +++ b/components/wifi-manager/webapp/src/index.ejs @@ -99,7 +99,7 @@
-
+
Software Updates
@@ -171,7 +171,7 @@ -
+
Local Firmware Upload
@@ -216,7 +216,7 @@
-
+
Usage Templates
@@ -355,7 +355,7 @@
-
+
WiFi Status
-
+
Credits
-
+
Extras/Overrides
diff --git a/components/wifi-manager/webapp/src/js/custom.js b/components/wifi-manager/webapp/src/js/custom.js index ddb6721b..eec22fc0 100644 --- a/components/wifi-manager/webapp/src/js/custom.js +++ b/components/wifi-manager/webapp/src/js/custom.js @@ -2097,7 +2097,7 @@ function getCommands() { const isConfig = cmdParts[0] === 'cfg'; const targetDiv = '#tab-' + cmdParts[0] + '-' + cmdParts[1]; let innerhtml = ''; - innerhtml += `
${command.help.encodeHTML().replace(/\n/g, '
')}
`; + innerhtml += `
${command.help.encodeHTML().replace(/\n/g, '
')}
`; if (command.argtable) { command.argtable.forEach(function (arg) { let placeholder = arg.datatype || ''; diff --git a/components/wifi-manager/webapp/src/sass/main.scss b/components/wifi-manager/webapp/src/sass/main.scss index 66ab125c..548c75fa 100644 --- a/components/wifi-manager/webapp/src/sass/main.scss +++ b/components/wifi-manager/webapp/src/sass/main.scss @@ -1,5 +1,5 @@ -@import "~bootswatch/dist/darkly/variables"; +// @import "~bootswatch/dist/darkly/variables"; -- remove darkly until bootswatch color is resolved @import "utils/variables"; @import "~bootstrap/scss/bootstrap"; // @import "~bootstrap/scss/functions"; @@ -38,5 +38,5 @@ // // Utilities // @import "~bootstrap/scss/utilities/api"; -@import "~bootswatch/dist/darkly/bootswatch"; +// @import "~bootswatch/dist/darkly/bootswatch"; -- remove darkly until bootswatch color is resolved @import "utils/style"; \ No newline at end of file diff --git a/components/wifi-manager/webapp/webpack/BuildEventsHook.js b/components/wifi-manager/webapp/webpack/BuildEventsHook.js new file mode 100644 index 00000000..6e8d97aa --- /dev/null +++ b/components/wifi-manager/webapp/webpack/BuildEventsHook.js @@ -0,0 +1,117 @@ +const path = require("path"); +const fs = require('fs'); +const zlib = require("zlib"); +const glob = require('glob'); + + +class BuildEventsHook { + constructor(name, fn, stage = 'afterEmit') { + this.name = name; + this.stage = stage; + this.function = fn; + } + apply(compiler) { + compiler.hooks[this.stage].tap(this.name, this.function); + } + } + + function createBuildEventsHook(options){ + return new BuildEventsHook('Update C App', + function (stats, arguments) { + + if (options.mode !== "production") return; + let buildRootPath = path.join(process.cwd(), '..', '..', '..'); + let wifiManagerPath = glob.sync(path.join(buildRootPath, 'components/**/wifi-manager*'))[0]; + let buildCRootPath = glob.sync(buildRootPath)[0]; + fs.appendFileSync('./dist/index.html.gz', + zlib.gzipSync(fs.readFileSync('./dist/index.html'), + { + chunckSize: 65536, + level: zlib.constants.Z_BEST_COMPRESSION + })); + + let getDirectories = function getDirectories (src, callback) { + let searchPath = path.posix.join(src, '/**/*(*.gz|favicon-32x32.png)'); + console.log(`Post build: Getting file list from ${searchPath}`); + glob(searchPath, callback); + }; + let cleanUpPath = path.posix.join(buildCRootPath, '/build/*.S'); + console.log(`Post build: Cleaning up previous builds in ${cleanUpPath}`); + glob(cleanUpPath, function (err, list) { + if (err) { + console.error('Error', err); + } else { + list.forEach(fileName => { + try { + console.log(`Post build: Purging old binary file ${fileName} from C project.`); + fs.unlinkSync(fileName) + //file removed + } catch (ferr) { + console.error(ferr) + } + }); + } + }, + 'afterEmit' + ); + console.log('Generating C include files from webpack build output'); + getDirectories('./dist', function (err, list) { + console.log(`Post build: found ${list.length} files. Relative path: ${wifiManagerPath}.`); + if (err) { + console.log('Error', err); + } else { + + let exportDefHead = + `/*********************************** +webpack_headers +${arguments[1]} +***********************************/ +#pragma once +#include +extern const char * resource_lookups[]; +extern const uint8_t * resource_map_start[]; +extern const uint8_t * resource_map_end[];`; + let exportDef = '// Automatically generated. Do not edit manually!.\n' + + '#include \n'; + let lookupDef = 'const char * resource_lookups[] = {\n'; + let lookupMapStart = 'const uint8_t * resource_map_start[] = {\n'; + let lookupMapEnd = 'const uint8_t * resource_map_end[] = {\n'; + let cMake = ''; + + list.forEach(foundFile => { + let exportName = path.basename(foundFile).replace(/[\. \-]/gm, '_'); + //take the full path of the file and make it relative to the build directory + let cmakeFileName = path.posix.relative(wifiManagerPath, glob.sync(path.resolve(foundFile))[0]); + let httpRelativePath = path.posix.join('/', path.posix.relative('dist', foundFile)); + exportDef += `extern const uint8_t _${exportName}_start[] asm("_binary_${exportName}_start");\nextern const uint8_t _${exportName}_end[] asm("_binary_${exportName}_end");\n`; + lookupDef += `\t"${httpRelativePath}",\n`; + lookupMapStart += '\t_' + exportName + '_start,\n'; + lookupMapEnd += '\t_' + exportName + '_end,\n'; + cMake += `target_add_binary_data( __idf_wifi-manager ${cmakeFileName} BINARY)\n`; + console.log(`Post build: adding cmake file reference to ${cmakeFileName} from C project, with web path ${httpRelativePath}.`); + }); + + lookupDef += '""\n};\n'; + lookupMapStart = lookupMapStart.substring(0, lookupMapStart.length - 2) + '\n};\n'; + lookupMapEnd = lookupMapEnd.substring(0, lookupMapEnd.length - 2) + '\n};\n'; + try { + fs.writeFileSync('webapp.cmake', cMake); + fs.writeFileSync('webpack.c', exportDef + lookupDef + lookupMapStart + lookupMapEnd); + fs.writeFileSync('webpack.h', exportDefHead); + //file written successfully + } catch (e) { + console.error(e); + } + } + }); + console.log('Post build completed.'); + + }) + } + + + module.exports = { + BuildEventsHook, + createBuildEventsHook + } + \ No newline at end of file diff --git a/components/wifi-manager/webapp/webpack/webpack.dev.js b/components/wifi-manager/webapp/webpack/webpack.dev.js index 2c0e3ec7..a3946a06 100644 --- a/components/wifi-manager/webapp/webpack/webpack.dev.js +++ b/components/wifi-manager/webapp/webpack/webpack.dev.js @@ -6,7 +6,7 @@ const HtmlWebPackPlugin = require('html-webpack-plugin'); const { Command } = require('commander'); let cmdLines= { }; var { parseArgsStringToArgv } = require('string-argv'); -const PORT = 9100; +const PORT = 5000; const data = { messages: require("../mock/messages.json"), @@ -159,7 +159,7 @@ module.exports ={ open: true, compress: true, port: PORT, - host: '127.0.0.1',//your ip address + host: '0.0.0.0', allowedHosts: "all", headers: {'Access-Control-Allow-Origin': '*', 'Accept-Encoding': 'identity'}, client: { diff --git a/docker/ToggleGitTracking.ps1 b/docker/ToggleGitTracking.ps1 new file mode 100644 index 00000000..73c5c9b7 --- /dev/null +++ b/docker/ToggleGitTracking.ps1 @@ -0,0 +1,91 @@ +param ( + [Parameter(Position=0, Mandatory=$false)] + [ValidateSet("t", "u", "d")] + [string]$option +) + +# Get the current directory +$currentDir = Get-Location + +# Define target directories +$targetDir = "components\wifi-manager\webapp" +$distDir = "$targetDir\dist" + +# Get list of files from the 'dist' directory +$fsFiles = Get-ChildItem -Recurse $distDir -File | ForEach-Object { + $_.FullName.Substring($currentDir.Path.Length + 1).Replace("\", "/") +} + +# Define additional files to include +$additionalFiles = @("webpack.c", "webpack.h", "webapp.cmake") + +# Check if additional files exist in $targetDir and format them +$additionalFilesFormatted = @() +Get-ChildItem $targetDir -File | ForEach-Object { + if ($additionalFiles -contains $_.Name) { + $formatted = $_.FullName.Substring($currentDir.Path.Length + 1).Replace("\", "/") + $additionalFilesFormatted += $formatted + Write-Host "Found $formatted" + } +} + +# Get list of files from the Git index +$indexFiles = git ls-files -s $distDir | ForEach-Object { + ($_ -split "\s+")[3] +} + +# Combine and remove duplicates +$allFiles = $fsFiles + $additionalFilesFormatted + $indexFiles | Sort-Object -Unique +# ... (previous code remains unchanged) + +# Apply the git command based on the option +$allFiles | ForEach-Object { + $relativePath = $_ + $isInIndex = $indexFiles -contains $relativePath + + if ($null -eq $option) { + $gitStatus = & git status --porcelain -- $relativePath + if ($gitStatus) { + $status = ($gitStatus -split "\s")[0] + Write-Host "$relativePath has Git status: $status" + } else { + Write-Host "$relativePath is not tracked" + } + } + elseif ($isInIndex) { + if ($option -eq "d") { + $resetResult = & git reset -- $relativePath 2>&1 + if ($resetResult -match 'error:') { + Write-Host "Error resetting ${relativePath}: $resetResult" + + continue + } + $checkoutResult = & git checkout -- $relativePath 2>&1 + if ($checkoutResult -match 'error:') { + Write-Host "Error checking out ${relativePath}: $checkoutResult" + + continue + } + Write-Host "Discarded changes in $relativePath" + } + # ... (rest of the code remains unchanged) + } + # else { + # # if ($option -eq "d") { + # # Remove-Item -Path $relativePath -Force + # # Write-Host "Removed untracked file $relativePath" + # # } else { + # # Write-Host "File $relativePath is not tracked." + # # } + + # } + else { + if ($option -eq "t") { + git add $relativePath + git update-index --no-skip-worktree $relativePath + Write-Host "Started tracking changes in $relativePath" + } else { + Write-Host "File $relativePath is not tracked." + } + } +} diff --git a/main/esp_app_main.c b/main/esp_app_main.c index f76f7454..6087de12 100644 --- a/main/esp_app_main.c +++ b/main/esp_app_main.c @@ -68,7 +68,68 @@ bool cold_boot=true; extern const char _ctype_[]; const char* __ctype_ptr__ = _ctype_; #endif +typedef struct { + const char *key; + const char *value; +} DefaultStringVal; +typedef struct { + const char *key; + unsigned int uint_value; + bool is_signed; +} DefaultNumVal; +const DefaultNumVal defaultNumVals[] = { + {"ota_erase_blk", OTA_FLASH_ERASE_BLOCK, 0}, + {"ota_stack", OTA_STACK_SIZE, 0}, + {"ota_prio", OTA_TASK_PRIOTITY, 1} +}; +const DefaultStringVal defaultStringVals[] = { + {"equalizer", ""}, + {"loudness", "0"}, + {"actrls_config", ""}, + {"lms_ctrls_raw", "n"}, + {"rotary_config", CONFIG_ROTARY_ENCODER}, + {"display_config", CONFIG_DISPLAY_CONFIG}, + {"eth_config", CONFIG_ETH_CONFIG}, + {"i2c_config", CONFIG_I2C_CONFIG}, + {"spi_config", CONFIG_SPI_CONFIG}, + {"set_GPIO", CONFIG_SET_GPIO}, + {"sleep_config", ""}, + {"led_brightness", ""}, + {"spdif_config", ""}, + {"dac_config", ""}, + {"dac_controlset", ""}, + {"jack_mutes_amp", "n"}, + {"gpio_exp_config", CONFIG_GPIO_EXP_CONFIG}, + {"bat_config", ""}, + {"metadata_config", ""}, + {"telnet_enable", ""}, + {"telnet_buffer", "40000"}, + {"telnet_block", "500"}, + {"stats", "n"}, + {"rel_api", CONFIG_RELEASE_API}, + {"pollmx", "600"}, + {"pollmin", "15"}, + {"ethtmout", "8"}, + {"dhcp_tmout", "8"}, + {"target", CONFIG_TARGET}, + {"led_vu_config", ""}, +#ifdef CONFIG_BT_SINK + {"bt_sink_pin", STR(CONFIG_BT_SINK_PIN)}, + {"bt_sink_volume", "127"}, + // Note: register_default_with_mac("bt_name", CONFIG_BT_NAME); is a special case + {"enable_bt_sink", STR(CONFIG_BT_SINK)}, + {"a2dp_dev_name", CONFIG_A2DP_DEV_NAME}, + {"a2dp_ctmt", STR(CONFIG_A2DP_CONNECT_TIMEOUT_MS)}, + {"a2dp_ctrld", STR(CONFIG_A2DP_CONTROL_DELAY_MS)}, + {"a2dp_sink_name", CONFIG_A2DP_SINK_NAME}, + {"autoexec", "1"}, +#ifdef CONFIG_AIRPLAY_SINK + {"airplay_port", CONFIG_AIRPLAY_PORT}, + {"enable_airplay", STR(CONFIG_AIRPLAY_SINK)} +#endif +#endif +}; static bool bNetworkConnected=false; // as an exception _init function don't need include @@ -80,7 +141,9 @@ extern void target_init(char *target); const char * str_or_unknown(const char * str) { return (str?str:unknown_string_placeholder); } const char * str_or_null(const char * str) { return (str?str:null_string_placeholder); } bool is_recovery_running; - +bool is_network_connected(){ + return bNetworkConnected; +} void cb_connection_got_ip(nm_state_t new_state, int sub_state){ const char *hostname; static ip4_addr_t ip; @@ -163,7 +226,7 @@ void set_log_level(char * tag, char * level){ } #define DEFAULT_NAME_WITH_MAC(var,defval) char var[strlen(defval)+sizeof(macStr)]; strcpy(var,defval); strcat(var,macStr) -void register_default_string_val(const char * key, char * value){ +void register_default_string_val(const char * key, const char * value){ char * existing =(char *)config_alloc_get(NVS_TYPE_STR,key ); ESP_LOGD(TAG,"Register default called with: %s= %s",key,value ); if(!existing) { @@ -175,7 +238,15 @@ void register_default_string_val(const char * key, char * value){ } FREE_AND_NULL(existing); } - +void register_single_default_num_val(const DefaultNumVal *entry) { + char number_buffer[101] = {}; + if (entry->is_signed) { + snprintf(number_buffer, sizeof(number_buffer) - 1, "%d", entry->uint_value); + } else { + snprintf(number_buffer, sizeof(number_buffer) - 1, "%u", entry->uint_value); + } + register_default_string_val(entry->key, number_buffer); +} char * alloc_get_string_with_mac(const char * val) { uint8_t mac[6]; char macStr[LOCAL_MAC_SIZE + 1]; @@ -220,77 +291,25 @@ void register_default_nvs(){ else { register_default_string_val("cspot_config", ""); } - } - #endif #ifdef CONFIG_AIRPLAY_SINK register_default_with_mac("airplay_name", CONFIG_AIRPLAY_NAME); - register_default_string_val("airplay_port", CONFIG_AIRPLAY_PORT); - register_default_string_val( "enable_airplay", STR(CONFIG_AIRPLAY_SINK)); #endif - #ifdef CONFIG_BT_SINK - register_default_string_val( "bt_sink_pin", STR(CONFIG_BT_SINK_PIN)); - register_default_string_val( "bt_sink_volume", "127"); register_default_with_mac("bt_name", CONFIG_BT_NAME); - register_default_string_val( "enable_bt_sink", STR(CONFIG_BT_SINK)); - register_default_string_val("a2dp_dev_name", CONFIG_A2DP_DEV_NAME); - register_default_string_val("a2dp_ctmt", STR(CONFIG_A2DP_CONNECT_TIMEOUT_MS)); - register_default_string_val("a2dp_ctrld", STR(CONFIG_A2DP_CONTROL_DELAY_MS)); - register_default_string_val("a2dp_sink_name", CONFIG_A2DP_SINK_NAME); #endif - register_default_with_mac("host_name", DEFAULT_HOST_NAME); register_default_with_mac("ap_ssid", CONFIG_DEFAULT_AP_SSID); - register_default_string_val("autoexec","1"); register_default_with_mac("autoexec1",CONFIG_DEFAULT_COMMAND_LINE " -n " DEFAULT_HOST_NAME); + for (int i = 0; i < sizeof(defaultStringVals) / sizeof(DefaultStringVal); ++i) { + register_default_string_val(defaultStringVals[i].key, defaultStringVals[i].value); + } + for (int i = 0; i < sizeof(defaultNumVals) / sizeof(DefaultNumVal); ++i) { + register_single_default_num_val(&defaultNumVals[i]); + } - register_default_string_val("release_url", CONFIG_SQUEEZELITE_ESP32_RELEASE_URL); - register_default_string_val("ap_ip_address",CONFIG_DEFAULT_AP_IP); - register_default_string_val("ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY ); - register_default_string_val("ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK); - register_default_string_val("ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL)); - register_default_string_val("ap_pwd", CONFIG_DEFAULT_AP_PASSWORD); - register_default_string_val("bypass_wm", "0"); - register_default_string_val("equalizer", ""); - register_default_string_val("loudness", "0"); - register_default_string_val("actrls_config", ""); - register_default_string_val("lms_ctrls_raw", "n"); - register_default_string_val("rotary_config", CONFIG_ROTARY_ENCODER); - char number_buffer[101] = {}; - snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_FLASH_ERASE_BLOCK); - register_default_string_val( "ota_erase_blk", number_buffer); - snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_STACK_SIZE); - register_default_string_val( "ota_stack", number_buffer); - snprintf(number_buffer,sizeof(number_buffer)-1,"%d",OTA_TASK_PRIOTITY); - register_default_string_val( "ota_prio", number_buffer); - register_default_string_val( "display_config", CONFIG_DISPLAY_CONFIG); - register_default_string_val( "eth_config", CONFIG_ETH_CONFIG); - register_default_string_val( "i2c_config", CONFIG_I2C_CONFIG); - register_default_string_val( "spi_config", CONFIG_SPI_CONFIG); - register_default_string_val( "set_GPIO", CONFIG_SET_GPIO); - register_default_string_val( "sleep_config", ""); - register_default_string_val( "led_brightness", ""); - register_default_string_val( "spdif_config", ""); - register_default_string_val( "dac_config", ""); - register_default_string_val( "dac_controlset", ""); - register_default_string_val( "jack_mutes_amp", "n"); - register_default_string_val("gpio_exp_config", CONFIG_GPIO_EXP_CONFIG); - register_default_string_val( "bat_config", ""); - register_default_string_val( "metadata_config", ""); - register_default_string_val( "telnet_enable", ""); - register_default_string_val( "telnet_buffer", "40000"); - register_default_string_val( "telnet_block", "500"); - register_default_string_val( "stats", "n"); - register_default_string_val( "rel_api", CONFIG_RELEASE_API); - register_default_string_val("pollmx","600"); - register_default_string_val("pollmin","15"); - register_default_string_val("ethtmout","8"); - register_default_string_val("dhcp_tmout","8"); - register_default_string_val("target", CONFIG_TARGET); - register_default_string_val("led_vu_config", ""); wait_for_commit(); ESP_LOGD(TAG,"Done setting default values in nvs."); }