cmd_config.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. /* cmd_i2ctools.c
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "cmd_config.h"
  9. #include "argtable3/argtable3.h"
  10. #include "platform_console.h"
  11. #include "esp_log.h"
  12. #include "string.h"
  13. #include "stdio.h"
  14. #include "platform_config.h"
  15. #include "messaging.h"
  16. #include "accessors.h"
  17. #include "adac.h"
  18. #include "tools.h"
  19. #include "cJSON.h"
  20. #include "cmd_i2ctools.h"
  21. #if defined(CONFIG_WITH_METRICS)
  22. #include "metrics.h"
  23. #endif
  24. #include "cmd_system.h"
  25. const char * desc_squeezelite ="Squeezelite Options";
  26. const char * desc_dac= "DAC Options";
  27. const char * desc_cspotc= "Spotify (cSpot) Options";
  28. const char * desc_preset= "Preset Options";
  29. const char * desc_spdif= "SPDIF Options";
  30. const char * desc_audio= "General Audio Options";
  31. const char * desc_bt_source= "Bluetooth Audio Output Options";
  32. const char * desc_rotary= "Rotary Control";
  33. const char * desc_ledvu= "Led Strip Options";
  34. extern const struct adac_s *dac_set[];
  35. extern void equalizer_set_loudness(uint8_t);
  36. extern void register_optional_cmd(void);
  37. #define CODECS_BASE "flac|pcm|mp3|ogg"
  38. #if NO_FAAD
  39. #define CODECS_AAC ""
  40. #else
  41. #define CODECS_AAC "|aac"
  42. #endif
  43. #if FFMPEG
  44. #define CODECS_FF "|wma|alac"
  45. #else
  46. #define CODECS_FF ""
  47. #endif
  48. #if DSD
  49. #define CODECS_DSD "|dsd"
  50. #else
  51. #define CODECS_DSD ""
  52. #endif
  53. #define CODECS_MP3 "|mad|mpg"
  54. #if !defined(MODEL_NAME)
  55. #define MODEL_NAME SqueezeLite
  56. #endif
  57. #ifndef QUOTE
  58. #define QUOTE(name) #name
  59. #define STR(macro) QUOTE(macro)
  60. #endif
  61. #ifndef MODEL_NAME_STRING
  62. #define MODEL_NAME_STRING STR(MODEL_NAME)
  63. #endif
  64. #define CODECS CODECS_BASE CODECS_AAC CODECS_FF CODECS_DSD CODECS_MP3
  65. #define NOT_OUTPUT "has input capabilities only"
  66. #define NOT_GPIO "is not a GPIO"
  67. typedef enum {
  68. SEARCHING_FOR_BT,
  69. SEARCHING_FOR_NAME,
  70. SEARCHING_FOR_NAME_START,
  71. SEARCHING_FOR_NAME_END,
  72. SEARCHING_FOR_BT_CMD_END,
  73. FINISHING
  74. } parse_state_t;
  75. static const char *TAG = "cmd_config";
  76. extern struct arg_end *getParmsEnd(struct arg_hdr * * argtable);
  77. //bck=<gpio>,ws=<gpio>,do=<gpio>[,mute=<gpio>[:0|1][,model=TAS57xx|TAS5713|AC101|WM8978|I2S][,sda=<gpio>,scl=gpio[,i2c=<addr>]]
  78. static struct {
  79. struct arg_str *model_name;
  80. struct arg_int *clock;
  81. struct arg_int *wordselect;
  82. struct arg_int *data;
  83. struct arg_int *mute_gpio;
  84. struct arg_lit *mute_level;
  85. struct arg_int *dac_sda;
  86. struct arg_int *dac_scl;
  87. struct arg_int *dac_i2c;
  88. struct arg_lit *clear;
  89. struct arg_end *end;
  90. } i2s_args;
  91. static struct {
  92. struct arg_str *model_config;
  93. struct arg_end *end;
  94. } known_model_args;
  95. static struct {
  96. struct arg_rem * rem;
  97. struct arg_int * A;
  98. struct arg_int * B;
  99. struct arg_int * SW;
  100. struct arg_lit * volume_lock;
  101. struct arg_lit * longpress;
  102. struct arg_lit * knobonly;
  103. struct arg_int * timer;
  104. struct arg_lit * clear;
  105. struct arg_lit * raw_mode;
  106. struct arg_end * end;
  107. } rotary_args;
  108. //config_rotary_get
  109. static struct {
  110. struct arg_str * type;
  111. struct arg_int * length;
  112. struct arg_int * gpio;
  113. struct arg_lit * clear;
  114. struct arg_end * end;
  115. } ledvu_args;
  116. static struct{
  117. struct arg_str *sink_name;
  118. struct arg_str *pin_code;
  119. // struct arg_dbl *connect_timeout_delay;
  120. // struct arg_dbl *control_delay;
  121. struct arg_end *end;
  122. } bt_source_args;
  123. static struct {
  124. struct arg_str *deviceName;
  125. // struct arg_int *volume;
  126. struct arg_int *bitrate;
  127. struct arg_int *zeroConf;
  128. struct arg_end *end;
  129. } cspot_args;
  130. static struct {
  131. struct arg_int *clock;
  132. struct arg_int *wordselect;
  133. struct arg_int *data;
  134. struct arg_lit *clear;
  135. struct arg_end *end;
  136. } spdif_args;
  137. static struct {
  138. struct arg_str *jack_behavior;
  139. struct arg_int *loudness;
  140. struct arg_end *end;
  141. } audio_args;
  142. static struct {
  143. struct arg_str * output_device; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  144. struct arg_str * name;// " -n <name>\t\tSet the player name\n"
  145. struct arg_str * server; // -s <server>[:<port>]\tConnect to specified server, otherwise uses autodiscovery to find server\n"
  146. struct arg_str * buffers;// " -b <stream>:<output>\tSpecify internal Stream and Output buffer sizes in Kbytes\n"
  147. struct arg_str * codecs;// " -c <codec1>,<codec2>\tRestrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS "\n"
  148. struct arg_int * timeout;// " -C <timeout>\t\tClose output device when idle after timeout seconds, default is to keep it open while player is 'on'\n"
  149. struct arg_str * log_level; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  150. // struct arg_str * log_level_all; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  151. // struct arg_str * log_level_slimproto; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  152. // struct arg_str * log_level_stream;
  153. // struct arg_str * log_level_decode;
  154. // struct arg_str * log_level_output;
  155. #if IR
  156. struct arg_str * log_level_ir;
  157. #endif
  158. // " -e <codec1>,<codec2>\tExplicitly exclude native support of one or more codecs; known codecs: " CODECS "\n"
  159. // " -f <logfile>\t\tWrite debug to logfile\n"
  160. // #if IR
  161. // " -i [<filename>]\tEnable lirc remote control support (lirc config file ~/.lircrc used if filename not specified)\n"
  162. // #endif
  163. struct arg_str * mac_addr; // " -m <mac addr>\t\tSet mac address, format: ab:cd:ef:12:34:56\n"
  164. struct arg_str * model_name;// " -M <modelname>\tSet the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")\n"
  165. struct arg_lit * header_format;// " -W\t\t\tRead wave and aiff format from header, ignore server parameters\n"
  166. struct arg_str * rates; // " -r <rates>[:<delay>]\tSample rates supported, allows output to be off when squeezelite is started; rates = <maxrate>|<minrate>-<maxrate>|<rate1>,<rate2>,<rate3>; delay = optional delay switching rates in ms\n"
  167. #if RESAMPLE
  168. struct arg_lit * resample;
  169. struct arg_str * resample_parms; //" -R -u [params]\tResample, params = <recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>,\n"
  170. #endif
  171. #if RESAMPLE16
  172. struct arg_lit * resample;
  173. struct arg_str * resample_parms; //" -R -u [params]\tResample, params = (b|l|m)[:i],\n"
  174. // " \t\t\t b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients\n"
  175. #endif
  176. struct arg_int * rate;// " -Z <rate>\t\tReport rate to server in helo as the maximum sample rate we can support\n"
  177. struct arg_end *end;
  178. } squeezelite_args;
  179. int is_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory, bool output){
  180. int res = 0;
  181. const char * name = gpio->hdr.longopts?gpio->hdr.longopts:gpio->hdr.glossary;
  182. *gpio_out=-1;
  183. int t_gpio=gpio->ival[0];
  184. if(gpio->count==0){
  185. if(mandatory){
  186. fprintf(f,"Missing: %s\n", name);
  187. res++;
  188. }
  189. } else if((output && !GPIO_IS_VALID_OUTPUT_GPIO(t_gpio)) || (!GPIO_IS_VALID_GPIO(t_gpio))){
  190. fprintf(f,"Invalid %s gpio: [%d] %s\n",name, t_gpio, GPIO_IS_VALID_GPIO(t_gpio)?NOT_OUTPUT:NOT_GPIO );
  191. res++;
  192. }
  193. else{
  194. *gpio_out = t_gpio;
  195. }
  196. return res;
  197. }
  198. int is_output_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory){
  199. return is_gpio(gpio,f,gpio_out,mandatory,true);
  200. }
  201. int check_missing_parm(struct arg_int * int_parm, FILE * f){
  202. int res=0;
  203. const char * name = int_parm->hdr.longopts?int_parm->hdr.longopts:int_parm->hdr.glossary;
  204. if(int_parm->count==0){
  205. fprintf(f,"Missing: %s\n", name);
  206. res++;
  207. }
  208. return res;
  209. }
  210. char * strip_bt_name(char * opt_str)
  211. {
  212. if(!opt_str || strlen(opt_str)==0){
  213. ESP_LOGW(TAG,"strip_bt_name: opt_str is NULL");
  214. return NULL;
  215. }
  216. char *result = malloc_init_external(strlen(opt_str)+1);
  217. char *str = strdup_psram(opt_str);
  218. const char * output_marker=" -o";
  219. if(!result ){
  220. ESP_LOGE(TAG,"Error allocating memory for result.");
  221. return opt_str;
  222. }
  223. if(!str){
  224. ESP_LOGE(TAG,"Error duplicating command line string.");
  225. return opt_str;
  226. }
  227. bool quoted=false;
  228. parse_state_t state = SEARCHING_FOR_BT;
  229. char *start = strstr(str, output_marker);
  230. if (start)
  231. {
  232. ESP_LOGV(TAG,"Found output option : %s\n",start);
  233. start+=strlen(output_marker);
  234. strncpy(result, str, (size_t)(start - str));
  235. char * pch=strtok(start," ");
  236. while(pch){
  237. ESP_LOGV(TAG,"Current output: %s\n[%s]",result,pch);
  238. switch (state)
  239. {
  240. case SEARCHING_FOR_BT:
  241. if (strcasestr(pch, "BT") )
  242. {
  243. state = SEARCHING_FOR_NAME;
  244. quoted=strcasestr(pch, "BT")!=NULL;
  245. ESP_LOGV(TAG," - fount BT Start %s", quoted?"quoted":"");
  246. }
  247. else
  248. {
  249. ESP_LOGV(TAG," - Searching for BT, Ignoring");
  250. }
  251. strcat(result, " ");
  252. strcat(result, pch);
  253. break;
  254. case SEARCHING_FOR_NAME:
  255. if (strcasestr(pch, "name") || strcasestr(pch, "n"))
  256. {
  257. ESP_LOGV(TAG," - Found name tag");
  258. state = SEARCHING_FOR_NAME_START;
  259. }
  260. else
  261. {
  262. strcat(result, " ");
  263. strcat(result, pch);
  264. ESP_LOGV(TAG," - Searching for name - added ");;
  265. }
  266. break;
  267. case SEARCHING_FOR_NAME_START:
  268. ESP_LOGV(TAG," - Name start");
  269. state = SEARCHING_FOR_NAME_END;
  270. break;
  271. case SEARCHING_FOR_NAME_END:
  272. if (strcasestr(pch, "\"")){
  273. ESP_LOGV(TAG," - got quoted string");
  274. state = FINISHING;
  275. }
  276. else if(pch[0]== '-'){
  277. strcat(result, " ");
  278. strcat(result, pch);
  279. ESP_LOGV(TAG," - got parameter marker");
  280. state = quoted?SEARCHING_FOR_BT_CMD_END:FINISHING;
  281. }
  282. else {
  283. ESP_LOGV(TAG," - name continued");
  284. }
  285. break;
  286. case SEARCHING_FOR_BT_CMD_END:
  287. ESP_LOGV(TAG," - looking for quoted BT cmd end");
  288. if (strcasestr(pch, "\"")){
  289. ESP_LOGV(TAG," - got quote termination");
  290. state = FINISHING;
  291. }
  292. strcat(result, " ");
  293. strcat(result, pch);
  294. break;
  295. case FINISHING:
  296. strcat(result, " ");
  297. strcat(result, pch);
  298. break;
  299. default:
  300. break;
  301. }
  302. pch = strtok(NULL, " ");
  303. ESP_LOGV(TAG,"\n");
  304. }
  305. }
  306. else
  307. {
  308. ESP_LOGE(TAG,"output option not found in %s\n",str);
  309. strcpy(result,str);
  310. }
  311. ESP_LOGV(TAG,"Result commmand : %s\n", result);
  312. free(str);
  313. return result;
  314. }
  315. static int do_bt_source_cmd(int argc, char **argv){
  316. esp_err_t err=ESP_OK;
  317. int nerrors = arg_parse(argc, argv,(void **)&bt_source_args);
  318. char *buf = NULL;
  319. size_t buf_size = 0;
  320. // char value[100] ={0};
  321. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  322. if (f == NULL) {
  323. return 1;
  324. }
  325. if(nerrors >0){
  326. arg_print_errors(f,bt_source_args.end,desc_bt_source);
  327. fclose(f);
  328. return 1;
  329. }
  330. if(bt_source_args.sink_name->count >0){
  331. err = config_set_value(NVS_TYPE_STR, "a2dp_sink_name", bt_source_args.sink_name->sval[0]);
  332. if(err!=ESP_OK){
  333. nerrors++;
  334. fprintf(f,"Error setting Bluetooth audio device name %s. %s\n",bt_source_args.sink_name->sval[0], esp_err_to_name(err));
  335. }
  336. else {
  337. fprintf(f,"Bluetooth audio device name changed to %s\n",bt_source_args.sink_name->sval[0]);
  338. }
  339. char * squeezelite_cmd = config_alloc_get_default(NVS_TYPE_STR, "autoexec1", NULL, 0);
  340. if( squeezelite_cmd && strstr(squeezelite_cmd," -o ") ){
  341. char * new_cmd = strip_bt_name(squeezelite_cmd);
  342. if(strcmp(new_cmd,squeezelite_cmd)!=0){
  343. fprintf(f,"Replacing old squeezelite command [%s] with [%s].\n",squeezelite_cmd,new_cmd);
  344. config_set_value(NVS_TYPE_STR, "autoexec1", new_cmd);
  345. if(err!=ESP_OK){
  346. nerrors++;
  347. fprintf(f,"Error updating squeezelite command line options . %s\n", esp_err_to_name(err));
  348. }
  349. }
  350. free(squeezelite_cmd);
  351. free(new_cmd);
  352. }
  353. }
  354. if(bt_source_args.pin_code->count >0){
  355. const char * v=bt_source_args.pin_code->sval[0];
  356. bool bInvalid=false;
  357. for(int i=0;i<strlen(v) && !bInvalid;i++){
  358. if(v[i]<'0' || v[i]>'9'){
  359. bInvalid=true;
  360. }
  361. }
  362. if(bInvalid || strlen(bt_source_args.pin_code->sval[0])>16 || strlen(bt_source_args.pin_code->sval[0])<4){
  363. nerrors++;
  364. 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]);
  365. }
  366. else {
  367. err = config_set_value(NVS_TYPE_STR, "a2dp_spin", bt_source_args.pin_code->sval[0]);
  368. if(err!=ESP_OK){
  369. nerrors++;
  370. fprintf(f,"Error setting Bluetooth source pin to %s. %s\n",bt_source_args.pin_code->sval[0], esp_err_to_name(err));
  371. }
  372. else {
  373. fprintf(f,"Bluetooth source pin changed to %s\n",bt_source_args.pin_code->sval[0]);
  374. }
  375. }
  376. }
  377. // if(bt_source_args.connect_timeout_delay->count >0){
  378. // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.connect_timeout_delay->dval[0]*1000.0));
  379. // if(bt_source_args.connect_timeout_delay->dval[0] <0.5 || bt_source_args.connect_timeout_delay->dval[0] >5.0){
  380. // nerrors++;
  381. // 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 );
  382. // }
  383. // else {
  384. // err = config_set_value(NVS_TYPE_STR, "a2dp_ctmt", value);
  385. // if(err!=ESP_OK){
  386. // nerrors++;
  387. // 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));
  388. // }
  389. // else {
  390. // fprintf(f,"Connection timeout changed to %0.0f sec (%s milliseconds)\n",bt_source_args.connect_timeout_delay->dval[0],value);
  391. // }
  392. // }
  393. // }
  394. // if(bt_source_args.control_delay->count >0){
  395. // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.control_delay->dval[0]*1000.0));
  396. // if(bt_source_args.control_delay->dval[0] <0.1 || bt_source_args.control_delay->dval[0] >2.0){
  397. // nerrors++;
  398. // 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 );
  399. // }
  400. // else {
  401. // err = config_set_value(NVS_TYPE_STR, "a2dp_ctrld", value);
  402. // if(err!=ESP_OK){
  403. // nerrors++;
  404. // 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));
  405. // }
  406. // else {
  407. // fprintf(f,"Control delay changed to %0.0f sec (%s milliseconds)\n",bt_source_args.control_delay->dval[0],value);
  408. // }
  409. // }
  410. // }
  411. if(!nerrors ){
  412. fprintf(f,"Done.\n");
  413. }
  414. fflush (f);
  415. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  416. fclose(f);
  417. FREE_AND_NULL(buf);
  418. return (nerrors==0 && err==ESP_OK)?0:1;
  419. }
  420. static int do_audio_cmd(int argc, char **argv){
  421. esp_err_t err=ESP_OK;
  422. int nerrors = arg_parse(argc, argv,(void **)&audio_args);
  423. char *buf = NULL;
  424. size_t buf_size = 0;
  425. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  426. if (f == NULL) {
  427. return 1;
  428. }
  429. if(nerrors >0){
  430. arg_print_errors(f,audio_args.end,desc_audio);
  431. fclose(f);
  432. return 1;
  433. }
  434. err = ESP_OK; // suppress any error code that might have happened in a previous step
  435. if(audio_args.loudness->count>0){
  436. char p[4]={0};
  437. int loudness_val = audio_args.loudness->ival[0];
  438. if( loudness_val < 0 || loudness_val>10){
  439. nerrors++;
  440. fprintf(f,"Invalid loudness value %d. Valid values are between 0 and 10.\n",loudness_val);
  441. }
  442. // it's not necessary to store loudness in NVS as set_loudness does it, but it does not hurt
  443. else {
  444. itoa(loudness_val,p,10);
  445. err = config_set_value(NVS_TYPE_STR, "loudness", p);
  446. }
  447. if(err!=ESP_OK){
  448. nerrors++;
  449. fprintf(f,"Error setting Loudness value %s. %s\n",p, esp_err_to_name(err));
  450. }
  451. else {
  452. fprintf(f,"Loudness changed to %s\n",p);
  453. equalizer_set_loudness(loudness_val);
  454. }
  455. }
  456. if(audio_args.jack_behavior->count>0){
  457. err = ESP_OK; // suppress any error code that might have happened in a previous step
  458. if(strcasecmp(audio_args.jack_behavior->sval[0],"Headphones")==0){
  459. err = config_set_value(NVS_TYPE_STR, "jack_mutes_amp", "y");
  460. }
  461. else if(strcasecmp(audio_args.jack_behavior->sval[0],"Subwoofer")==0){
  462. err = config_set_value(NVS_TYPE_STR, "jack_mutes_amp", "n");
  463. }
  464. else {
  465. nerrors++;
  466. fprintf(f,"Unknown Audio Jack Behavior %s.\n",audio_args.jack_behavior->sval[0]);
  467. }
  468. if(err!=ESP_OK){
  469. nerrors++;
  470. fprintf(f,"Error setting Audio Jack Behavior %s. %s\n",audio_args.jack_behavior->sval[0], esp_err_to_name(err));
  471. }
  472. else {
  473. fprintf(f,"Audio Jack Behavior changed to %s\n",audio_args.jack_behavior->sval[0]);
  474. }
  475. }
  476. if(!nerrors ){
  477. fprintf(f,"Done.\n");
  478. }
  479. fflush (f);
  480. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  481. fclose(f);
  482. FREE_AND_NULL(buf);
  483. return (nerrors==0 && err==ESP_OK)?0:1;
  484. }
  485. static int do_spdif_cmd(int argc, char **argv){
  486. i2s_platform_config_t i2s_dac_pin = {
  487. .i2c_addr = -1,
  488. .sda= -1,
  489. .scl = -1,
  490. .mute_gpio = -1,
  491. .mute_level = -1
  492. };
  493. if(is_spdif_config_locked()){
  494. cmd_send_messaging(argv[0],MESSAGING_ERROR,"SPDIF Configuration is locked on this platform\n");
  495. return 1;
  496. }
  497. esp_err_t err=ESP_OK;
  498. int nerrors = arg_parse(argc, argv,(void **)&spdif_args);
  499. if (spdif_args.clear->count) {
  500. cmd_send_messaging(argv[0],MESSAGING_WARNING,"SPDIF config cleared\n");
  501. config_set_value(NVS_TYPE_STR, "spdif_config", "");
  502. return 0;
  503. }
  504. char *buf = NULL;
  505. size_t buf_size = 0;
  506. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  507. if (f == NULL) {
  508. return 1;
  509. }
  510. if(nerrors >0){
  511. arg_print_errors(f,spdif_args.end,desc_dac);
  512. fclose(f);
  513. return 1;
  514. }
  515. nerrors+=is_output_gpio(spdif_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true);
  516. nerrors+=is_output_gpio(spdif_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true);
  517. nerrors+=is_output_gpio(spdif_args.data, f, &i2s_dac_pin.pin.data_out_num, true);
  518. if(!nerrors ){
  519. fprintf(f,"Storing SPDIF parameters.\n");
  520. nerrors+=(config_spdif_set(&i2s_dac_pin )!=ESP_OK);
  521. }
  522. if(!nerrors ){
  523. fprintf(f,"Done.\n");
  524. }
  525. fflush (f);
  526. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  527. fclose(f);
  528. FREE_AND_NULL(buf);
  529. return (nerrors==0 && err==ESP_OK)?0:1;
  530. }
  531. static int do_rotary_cmd(int argc, char **argv){
  532. rotary_struct_t rotary={ .A = -1, .B = -1, .SW = -1, .longpress = 0, .knobonly=0,.volume_lock=false};
  533. esp_err_t err=ESP_OK;
  534. int nerrors = arg_parse(argc, argv,(void **)&rotary_args);
  535. if (rotary_args.clear->count) {
  536. cmd_send_messaging(argv[0],MESSAGING_WARNING,"rotary config cleared\n");
  537. config_set_value(NVS_TYPE_STR, "rotary_config", "");
  538. return 0;
  539. }
  540. char *buf = NULL;
  541. size_t buf_size = 0;
  542. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  543. if (f == NULL) {
  544. return 1;
  545. }
  546. if(nerrors >0){
  547. arg_print_errors(f,rotary_args.end,desc_rotary);
  548. fclose(f);
  549. return 1;
  550. }
  551. nerrors+=is_gpio(rotary_args.A, f, &rotary.A, true,false);
  552. nerrors+=is_gpio(rotary_args.B, f, &rotary.B, true,false);
  553. nerrors+=is_gpio(rotary_args.SW, f, &rotary.SW,false,false);
  554. if(rotary_args.knobonly->count>0 && (rotary_args.volume_lock->count>0 || rotary_args.longpress->count>0)){
  555. fprintf(f,"error: Cannot use volume lock or longpress option when knob only option selected\n");
  556. nerrors++;
  557. }
  558. if(rotary_args.timer->count>0 && rotary_args.timer->ival[0]<0){
  559. fprintf(f,"error: knob only timer should be greater than or equal to zero.\n");
  560. nerrors++;
  561. }
  562. else {
  563. rotary.timer = rotary_args.timer->count>0?rotary_args.timer->ival[0]:0;
  564. }
  565. rotary.knobonly = rotary_args.knobonly->count>0;
  566. rotary.volume_lock= rotary_args.volume_lock->count>0;
  567. rotary.longpress = rotary_args.longpress->count>0;
  568. if(!nerrors ){
  569. fprintf(f,"Storing rotary parameters.\n");
  570. nerrors+=(config_rotary_set(&rotary )!=ESP_OK);
  571. }
  572. if(!nerrors ){
  573. fprintf(f,"Storing raw mode parameter.\n");
  574. nerrors+=(config_set_value(NVS_TYPE_STR, "lms_ctrls_raw", rotary_args.raw_mode->count>0?"Y":"N")!=ESP_OK);
  575. if(nerrors>0){
  576. fprintf(f,"error: Unable to store raw mode parameter.\n");
  577. }
  578. }
  579. if(!nerrors ){
  580. fprintf(f,"Done.\n");
  581. }
  582. fflush (f);
  583. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  584. fclose(f);
  585. FREE_AND_NULL(buf);
  586. return (nerrors==0 && err==ESP_OK)?0:1;
  587. }
  588. static int is_valid_gpio_number(int gpio, const char * name, FILE *f, bool mandatory, struct arg_int * target, bool output){
  589. bool invalid = (!GPIO_IS_VALID_GPIO(gpio) ||(output && !GPIO_IS_VALID_OUTPUT_GPIO(gpio))) ;
  590. if(invalid && mandatory && gpio!=-1){
  591. fprintf(f,"Error: Invalid mandatory gpio %d for %s\n",gpio,name);
  592. return 1;
  593. }
  594. if(target && !invalid){
  595. target->count=1;
  596. target->ival[0]=gpio;
  597. }
  598. return 0;
  599. }
  600. #ifdef CONFIG_CSPOT_SINK
  601. static int do_cspot_config(int argc, char **argv){
  602. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&cspot_args);
  603. if (nerrors != 0) {
  604. return 1;
  605. }
  606. char *buf = NULL;
  607. size_t buf_size = 0;
  608. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  609. if (f == NULL) {
  610. return 1;
  611. }
  612. cJSON * cspot_config = config_alloc_get_cjson("cspot_config");
  613. if(!cspot_config){
  614. nerrors++;
  615. fprintf(f,"error: Unable to get default cspot config.\n");
  616. }
  617. if(cspot_args.deviceName->count>0){
  618. cjson_update_string(&cspot_config,cspot_args.deviceName->hdr.longopts,cspot_args.deviceName->sval[0]);
  619. }
  620. if(cspot_args.bitrate->count>0){
  621. cjson_update_number(&cspot_config,cspot_args.bitrate->hdr.longopts,cspot_args.bitrate->ival[0]);
  622. }
  623. if(cspot_args.zeroConf->count>0){
  624. cjson_update_number(&cspot_config,cspot_args.zeroConf->hdr.longopts,cspot_args.zeroConf->ival[0]);
  625. }
  626. if(!nerrors ){
  627. fprintf(f,"Storing cspot parameters.\n");
  628. nerrors+=(config_set_cjson_str_and_free("cspot_config",cspot_config) !=ESP_OK);
  629. }
  630. if(nerrors==0 ){
  631. if(cspot_args.deviceName->count>0){
  632. fprintf(f,"Device name changed to %s\n",cspot_args.deviceName->sval[0]);
  633. }
  634. if(cspot_args.bitrate->count>0){
  635. fprintf(f,"Bitrate changed to %u\n",cspot_args.bitrate->ival[0]);
  636. }
  637. if(cspot_args.zeroConf->count>0){
  638. fprintf(f,"ZeroConf changed to %u\n",cspot_args.zeroConf->ival[0]);
  639. }
  640. }
  641. if(!nerrors ){
  642. fprintf(f,"Done.\n");
  643. }
  644. fflush (f);
  645. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  646. fclose(f);
  647. FREE_AND_NULL(buf);
  648. return nerrors;
  649. }
  650. #endif
  651. static int do_ledvu_cmd(int argc, char **argv){
  652. ledvu_struct_t ledvu={ .type = "WS2812", .gpio = -1, .length = 0};
  653. esp_err_t err=ESP_OK;
  654. int nerrors = arg_parse(argc, argv,(void **)&ledvu_args);
  655. if (ledvu_args.clear->count) {
  656. cmd_send_messaging(argv[0],MESSAGING_WARNING,"ledvu config cleared\n");
  657. config_set_value(NVS_TYPE_STR, "led_vu_config", "");
  658. return 0;
  659. }
  660. char *buf = NULL;
  661. size_t buf_size = 0;
  662. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  663. if (f == NULL) {
  664. return 1;
  665. }
  666. if(nerrors >0){
  667. arg_print_errors(f,ledvu_args.end,desc_ledvu);
  668. return 1;
  669. }
  670. nerrors+=is_output_gpio(ledvu_args.gpio, f, &ledvu.gpio, true);
  671. if(ledvu_args.length->count==0 || ledvu_args.length->ival[0]<1 || ledvu_args.length->ival[0]>255){
  672. fprintf(f,"error: strip length must be greater than 0 and no more than 255\n");
  673. nerrors++;
  674. }
  675. else {
  676. ledvu.length = ledvu_args.length->count>0?ledvu_args.length->ival[0]:0;
  677. }
  678. if(!nerrors ){
  679. fprintf(f,"Storing ledvu parameters.\n");
  680. nerrors+=(config_ledvu_set(&ledvu )!=ESP_OK);
  681. }
  682. if(!nerrors ){
  683. fprintf(f,"Done.\n");
  684. }
  685. fflush (f);
  686. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  687. fclose(f);
  688. FREE_AND_NULL(buf);
  689. return (nerrors==0 && err==ESP_OK)?0:1;
  690. }
  691. static int do_i2s_cmd(int argc, char **argv)
  692. {
  693. i2s_platform_config_t i2s_dac_pin = {
  694. .i2c_addr = -1,
  695. .sda= -1,
  696. .scl = -1,
  697. .mute_gpio = -1,
  698. .mute_level = -1
  699. };
  700. if(is_dac_config_locked()){
  701. cmd_send_messaging(argv[0],MESSAGING_ERROR,"DAC Configuration is locked on this platform\n");
  702. return 1;
  703. }
  704. ESP_LOGD(TAG,"Processing i2s command %s with %d parameters",argv[0],argc);
  705. esp_err_t err=ESP_OK;
  706. int nerrors = arg_parse(argc, argv,(void **)&i2s_args);
  707. if (i2s_args.clear->count) {
  708. cmd_send_messaging(argv[0],MESSAGING_WARNING,"DAC config cleared\n");
  709. config_set_value(NVS_TYPE_STR, "dac_config", "");
  710. return 0;
  711. }
  712. char *buf = NULL;
  713. size_t buf_size = 0;
  714. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  715. if (f == NULL) {
  716. return 1;
  717. }
  718. if(nerrors >0){
  719. ESP_LOGE(TAG,"do_i2s_cmd: %d errors parsing arguments",nerrors);
  720. arg_print_errors(f,i2s_args.end,desc_dac);
  721. }
  722. else {
  723. strncpy(i2s_dac_pin.model,i2s_args.model_name->sval[0],sizeof(i2s_dac_pin.model));
  724. i2s_dac_pin.model[sizeof(i2s_dac_pin.model) - 1] = '\0';
  725. nerrors += is_output_gpio(i2s_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true);
  726. nerrors += is_output_gpio(i2s_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true);
  727. nerrors += is_output_gpio(i2s_args.data, f, &i2s_dac_pin.pin.data_out_num, true);
  728. nerrors += is_output_gpio(i2s_args.mute_gpio, f, &i2s_dac_pin.mute_gpio, false);
  729. if (i2s_dac_pin.mute_gpio >= 0) {
  730. i2s_dac_pin.mute_level = i2s_args.mute_level->count > 0 ? 1 : 0;
  731. }
  732. if (i2s_args.dac_sda->count > 0 && i2s_args.dac_sda->ival[0] >= 0) {
  733. // if SDA specified, then SDA and SCL are both mandatory
  734. nerrors += is_output_gpio(i2s_args.dac_sda, f, &i2s_dac_pin.sda, false);
  735. nerrors += is_output_gpio(i2s_args.dac_scl, f, &i2s_dac_pin.scl, false);
  736. }
  737. if (i2s_args.dac_sda->count == 0 && i2s_args.dac_i2c->count > 0) {
  738. fprintf(f, "warning: ignoring i2c address, since dac i2c gpios config is incomplete\n");
  739. } else if (i2s_args.dac_i2c->count > 0) {
  740. i2s_dac_pin.i2c_addr = i2s_args.dac_i2c->ival[0];
  741. }
  742. if (!nerrors) {
  743. fprintf(f, "Storing i2s parameters.\n");
  744. nerrors += (config_i2s_set(&i2s_dac_pin, "dac_config") != ESP_OK);
  745. }
  746. }
  747. if(!nerrors ){
  748. fprintf(f,"Done.\n");
  749. }
  750. fflush (f);
  751. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  752. fclose(f);
  753. FREE_AND_NULL(buf);
  754. return (nerrors==0 && err==ESP_OK)?0:1;
  755. }
  756. cJSON * example_cb(){
  757. cJSON * values = cJSON_CreateObject();
  758. // int i2c_port;
  759. // const i2c_config_t * i2c= config_i2c_get(&i2c_port);
  760. // if(i2c->scl_io_num>0) {
  761. // cJSON_AddNumberToObject(values,"scl",i2c->scl_io_num);
  762. // }
  763. // if(i2c->sda_io_num>0) {
  764. // cJSON_AddNumberToObject(values,"sda",i2c->sda_io_num);
  765. // }
  766. // if(i2c->master.clk_speed>0) {
  767. // cJSON_AddNumberToObject(values,"freq",i2c->master.clk_speed);
  768. // }
  769. // if(i2c_port>0) {
  770. // cJSON_AddNumberToObject(values,"port",i2c_port);
  771. // }
  772. return values;
  773. }
  774. cJSON * known_model_cb(){
  775. cJSON * values = cJSON_CreateObject();
  776. if(!values){
  777. ESP_LOGE(TAG,"known_model_cb: Failed to create JSON object");
  778. return NULL;
  779. }
  780. char * name = config_alloc_get_default(NVS_TYPE_STR,known_model_args.model_config->hdr.longopts,"",0);
  781. if(!name){
  782. ESP_LOGE(TAG,"Failed to get board model from nvs key %s ",known_model_args.model_config->hdr.longopts);
  783. }
  784. else {
  785. cJSON_AddStringToObject(values,known_model_args.model_config->hdr.longopts,name);
  786. }
  787. return values;
  788. }
  789. #ifdef CONFIG_CSPOT_SINK
  790. cJSON * cspot_cb(){
  791. cJSON * values = cJSON_CreateObject();
  792. if(!values){
  793. ESP_LOGE(TAG,"cspot_cb: Failed to create JSON object");
  794. return NULL;
  795. }
  796. cJSON * cspot_config = config_alloc_get_cjson("cspot_config");
  797. if(!cspot_config){
  798. ESP_LOGE(TAG,"cspot_cb: Failed to get cspot config");
  799. return NULL;
  800. }
  801. cJSON * cspot_values = cJSON_GetObjectItem(cspot_config,cspot_args.deviceName->hdr.longopts);
  802. if(cspot_values){
  803. cJSON_AddStringToObject(values,cspot_args.deviceName->hdr.longopts,cJSON_GetStringValue(cspot_values));
  804. }
  805. cspot_values = cJSON_GetObjectItem(cspot_config,cspot_args.bitrate->hdr.longopts);
  806. if(cspot_values){
  807. cJSON_AddNumberToObject(values,cspot_args.bitrate->hdr.longopts,cJSON_GetNumberValue(cspot_values));
  808. }
  809. cspot_values = cJSON_GetObjectItem(cspot_config,cspot_args.zeroConf->hdr.longopts);
  810. if(cspot_values){
  811. cJSON_AddNumberToObject(values,cspot_args.zeroConf->hdr.longopts,cJSON_GetNumberValue(cspot_values));
  812. }
  813. cJSON_Delete(cspot_config);
  814. return values;
  815. }
  816. #endif
  817. cJSON * i2s_cb(){
  818. cJSON * values = cJSON_CreateObject();
  819. const i2s_platform_config_t * i2s_conf= config_dac_get( );
  820. #if defined(CONFIG_WITH_METRICS)
  821. metrics_add_feature("i2s",i2s_conf->pin.data_out_num>=0);
  822. #endif
  823. if(i2s_conf->pin.bck_io_num>0 ) {
  824. cJSON_AddNumberToObject(values,i2s_args.clock->hdr.longopts,i2s_conf->pin.bck_io_num);
  825. }
  826. if(i2s_conf->pin.ws_io_num>=0 ) {
  827. cJSON_AddNumberToObject(values,i2s_args.wordselect->hdr.longopts,i2s_conf->pin.ws_io_num);
  828. }
  829. if(i2s_conf->pin.data_out_num>=0 ) {
  830. cJSON_AddNumberToObject(values,i2s_args.data->hdr.longopts,i2s_conf->pin.data_out_num);
  831. }
  832. if(i2s_conf->sda>=0 ) {
  833. cJSON_AddNumberToObject(values,i2s_args.dac_sda->hdr.longopts,i2s_conf->sda);
  834. }
  835. if(i2s_conf->scl>=0 ) {
  836. cJSON_AddNumberToObject(values,i2s_args.dac_scl->hdr.longopts,i2s_conf->scl);
  837. }
  838. if(i2s_conf->i2c_addr>=0 ) {
  839. cJSON_AddNumberToObject(values,i2s_args.dac_i2c->hdr.longopts,i2s_conf->i2c_addr);
  840. }
  841. if(i2s_conf->mute_gpio>=0 ) {
  842. cJSON_AddNumberToObject(values,i2s_args.mute_gpio->hdr.longopts,i2s_conf->mute_gpio);
  843. }
  844. if(i2s_conf->mute_level>=0 ) {
  845. cJSON_AddBoolToObject(values,i2s_args.mute_level->hdr.longopts,i2s_conf->mute_level>0);
  846. }
  847. if(strlen(i2s_conf->model)>0){
  848. cJSON_AddStringToObject(values,i2s_args.model_name->hdr.longopts,i2s_conf->model);
  849. }
  850. else {
  851. cJSON_AddStringToObject(values,i2s_args.model_name->hdr.longopts,"I2S");
  852. }
  853. return values;
  854. }
  855. cJSON * spdif_cb(){
  856. cJSON * values = cJSON_CreateObject();
  857. const i2s_platform_config_t * spdif_conf= config_spdif_get( );
  858. if(spdif_conf->pin.data_out_num>=0) {
  859. #if defined(CONFIG_WITH_METRICS)
  860. metrics_add_feature("spdif","enabled");
  861. #endif
  862. }
  863. if(spdif_conf->pin.bck_io_num>0 ) {
  864. cJSON_AddNumberToObject(values,"clock",spdif_conf->pin.bck_io_num);
  865. }
  866. if(spdif_conf->pin.ws_io_num>=0 ) {
  867. cJSON_AddNumberToObject(values,"wordselect",spdif_conf->pin.ws_io_num);
  868. }
  869. if(spdif_conf->pin.data_out_num>=0 ) {
  870. cJSON_AddNumberToObject(values,"data",spdif_conf->pin.data_out_num);
  871. }
  872. return values;
  873. }
  874. cJSON * rotary_cb(){
  875. cJSON * values = cJSON_CreateObject();
  876. char *p = config_alloc_get_default(NVS_TYPE_STR, "lms_ctrls_raw", "n", 0);
  877. bool raw_mode = p && (*p == '1' || *p == 'Y' || *p == 'y');
  878. free(p);
  879. const rotary_struct_t *rotary= config_rotary_get();
  880. #if defined(CONFIG_WITH_METRICS)
  881. metrics_add_feature("rotary",GPIO_IS_VALID_GPIO(rotary->A ));
  882. #endif
  883. if(GPIO_IS_VALID_GPIO(rotary->A ) && rotary->A>=0 && GPIO_IS_VALID_GPIO(rotary->B) && rotary->B>=0){
  884. cJSON_AddNumberToObject(values,rotary_args.A->hdr.longopts,rotary->A);
  885. cJSON_AddNumberToObject(values,rotary_args.B->hdr.longopts,rotary->B);
  886. if(GPIO_IS_VALID_GPIO(rotary->SW ) && rotary->SW>=0 ){
  887. cJSON_AddNumberToObject(values,rotary_args.SW->hdr.longopts,rotary->SW);
  888. }
  889. cJSON_AddBoolToObject(values,rotary_args.volume_lock->hdr.longopts,rotary->volume_lock);
  890. cJSON_AddBoolToObject(values,rotary_args.longpress->hdr.longopts,rotary->longpress);
  891. cJSON_AddBoolToObject(values,rotary_args.knobonly->hdr.longopts,rotary->knobonly);
  892. cJSON_AddNumberToObject(values,rotary_args.timer->hdr.longopts,rotary->timer);
  893. cJSON_AddNumberToObject(values,rotary_args.raw_mode->hdr.longopts,raw_mode);
  894. }
  895. return values;
  896. }
  897. cJSON * ledvu_cb(){
  898. cJSON * values = cJSON_CreateObject();
  899. const ledvu_struct_t *ledvu= config_ledvu_get();
  900. if(GPIO_IS_VALID_GPIO(ledvu->gpio )){
  901. #if defined(CONFIG_WITH_METRICS)
  902. metrics_add_feature("led_vu","enabled");
  903. #endif
  904. }
  905. if(GPIO_IS_VALID_GPIO(ledvu->gpio) && ledvu->gpio>=0 && ledvu->length > 0){
  906. cJSON_AddNumberToObject(values,"gpio",ledvu->gpio);
  907. cJSON_AddNumberToObject(values,"length",ledvu->length);
  908. }
  909. if(strlen(ledvu->type)>0){
  910. cJSON_AddStringToObject(values,"type",ledvu->type);
  911. }
  912. else {
  913. cJSON_AddStringToObject(values,"type","WS2812");
  914. }
  915. return values;
  916. }
  917. cJSON * audio_cb(){
  918. cJSON * values = cJSON_CreateObject();
  919. char * p = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
  920. cJSON_AddStringToObject(values,"jack_behavior",(strcmp(p,"1") == 0 ||strcasecmp(p,"y") == 0)?"Headphones":"Subwoofer");
  921. #if defined(CONFIG_WITH_METRICS)
  922. metrics_add_feature("jack_mute",atoi(p)>=0);
  923. #endif
  924. FREE_AND_NULL(p);
  925. p = config_alloc_get_default(NVS_TYPE_STR, "loudness", "0", 0);
  926. #if defined(CONFIG_WITH_METRICS)
  927. metrics_add_feature("loudness",atoi(p)>=0);
  928. #endif
  929. cJSON_AddStringToObject(values,"loudness",p);
  930. FREE_AND_NULL(p);
  931. return values;
  932. }
  933. cJSON * bt_source_cb(){
  934. cJSON * values = cJSON_CreateObject();
  935. char * p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_sink_name", NULL, 0);
  936. if(p){
  937. cJSON_AddStringToObject(values,"sink_name",p);
  938. #if defined(CONFIG_WITH_METRICS)
  939. metrics_add_feature("btsource",strlen(p)>0);
  940. #endif
  941. }
  942. FREE_AND_NULL(p);
  943. // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctmt", NULL, 0);
  944. // if(p){
  945. // cJSON_AddNumberToObject(values,"connect_timeout_delay",((double)atoi(p)/1000.0));
  946. // }
  947. // FREE_AND_NULL(p);
  948. p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_spin", "0000", 0);
  949. if(p){
  950. cJSON_AddStringToObject(values,"pin_code",p);
  951. }
  952. FREE_AND_NULL(p);
  953. // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctrld", NULL, 0);
  954. // if(p){
  955. // cJSON_AddNumberToObject(values,"control_delay",((double)atoi(p)/1000.0));
  956. // }
  957. // FREE_AND_NULL(p);
  958. return values;
  959. }
  960. void get_str_parm_json(struct arg_str * parm, cJSON * entry){
  961. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  962. if(parm->count>0){
  963. cJSON_AddStringToObject(entry,name,parm->sval[0]);
  964. }
  965. }
  966. void get_file_parm_json(struct arg_file * parm, cJSON * entry){
  967. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  968. if(parm->count>0){
  969. cJSON_AddStringToObject(entry,name,parm->filename[0]);
  970. }
  971. }
  972. void get_lit_parm_json(struct arg_lit * parm, cJSON * entry){
  973. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  974. cJSON_AddBoolToObject(entry,name,(parm->count>0));
  975. }
  976. void get_int_parm_json(struct arg_int * parm, cJSON * entry){
  977. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  978. if(parm->count>0){
  979. cJSON_AddNumberToObject(entry,name,parm->ival[0]);
  980. }
  981. }
  982. static int do_squeezelite_cmd(int argc, char **argv)
  983. {
  984. esp_err_t err=ESP_OK;
  985. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr ** )&squeezelite_args);
  986. char *buf = NULL;
  987. size_t buf_size = 0;
  988. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  989. if (f == NULL) {
  990. return 1;
  991. }
  992. fprintf(f,"Not yet implemented!");
  993. nerrors+=1;
  994. fflush (f);
  995. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  996. fclose(f);
  997. FREE_AND_NULL(buf);
  998. return (nerrors==0 && err==ESP_OK)?0:1;
  999. }
  1000. cJSON * squeezelite_cb(){
  1001. cJSON * values = cJSON_CreateObject();
  1002. char * nvs_config= config_alloc_get(NVS_TYPE_STR, "autoexec1");
  1003. char **argv = NULL;
  1004. char *buf = NULL;
  1005. size_t buf_size = 0;
  1006. int nerrors=1;
  1007. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  1008. if (f == NULL) {
  1009. return values;
  1010. }
  1011. if(nvs_config && strlen(nvs_config)>0){
  1012. ESP_LOGD(TAG,"Parsing command %s",nvs_config);
  1013. argv = (char **) calloc(22, sizeof(char *));
  1014. if (argv == NULL) {
  1015. FREE_AND_NULL(nvs_config);
  1016. fclose(f);
  1017. return values;
  1018. }
  1019. size_t argc = esp_console_split_argv(nvs_config, argv,22);
  1020. if (argc != 0) {
  1021. nerrors = arg_parse(argc, argv,(void **)&squeezelite_args);
  1022. ESP_LOGD(TAG,"Parsing completed");
  1023. }
  1024. }
  1025. if (nerrors == 0) {
  1026. get_str_parm_json(squeezelite_args.buffers, values);
  1027. get_str_parm_json(squeezelite_args.codecs, values);
  1028. get_lit_parm_json(squeezelite_args.header_format, values);
  1029. get_str_parm_json(squeezelite_args.log_level, values);
  1030. // get_str_parm_json(squeezelite_args.log_level_all, values);
  1031. // get_str_parm_json(squeezelite_args.log_level_decode, values);
  1032. // get_str_parm_json(squeezelite_args.log_level_output, values);
  1033. // get_str_parm_json(squeezelite_args.log_level_slimproto, values);
  1034. // get_str_parm_json(squeezelite_args.log_level_stream, values);
  1035. get_str_parm_json(squeezelite_args.mac_addr, values);
  1036. get_str_parm_json(squeezelite_args.output_device, values);
  1037. #if defined(CONFIG_WITH_METRICS)
  1038. if(squeezelite_args.output_device->sval[0]!=NULL && strlen(squeezelite_args.output_device->sval[0])>0){
  1039. metrics_add_feature_variant("output",squeezelite_args.output_device->sval[0]);
  1040. }
  1041. #endif
  1042. get_str_parm_json(squeezelite_args.model_name, values);
  1043. get_str_parm_json(squeezelite_args.name, values);
  1044. get_int_parm_json(squeezelite_args.rate, values);
  1045. get_str_parm_json(squeezelite_args.rates, values);
  1046. get_str_parm_json(squeezelite_args.server, values);
  1047. get_int_parm_json(squeezelite_args.timeout, values);
  1048. char * p = cJSON_Print(values);
  1049. ESP_LOGD(TAG,"%s",p);
  1050. free(p);
  1051. }
  1052. else {
  1053. arg_print_errors(f, squeezelite_args.end, desc_squeezelite);
  1054. }
  1055. fflush (f);
  1056. if(strlen(buf)>0){
  1057. log_send_messaging(nerrors?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  1058. }
  1059. fclose(f);
  1060. FREE_AND_NULL(buf);
  1061. FREE_AND_NULL(nvs_config);
  1062. FREE_AND_NULL(argv);
  1063. return values;
  1064. }
  1065. static char * get_log_level_options(const char * longname){
  1066. const char * template = "<%s=info|%s=debug|%s=sdebug>";
  1067. char * options = NULL;
  1068. int len = snprintf(NULL,0,template,longname,longname,longname);
  1069. if(len>0){
  1070. options = malloc_init_external(len+1);
  1071. snprintf(options,len,template,longname,longname,longname);
  1072. }
  1073. return options;
  1074. }
  1075. // loop through dac_set and concatenate model name separated with |
  1076. static char * get_dac_list(){
  1077. const char * EXTRA_MODEL_NAMES = "ES8388|I2S";
  1078. char * dac_list=NULL;
  1079. size_t total_len=0;
  1080. for(int i=0;dac_set[i];i++){
  1081. if(dac_set[i]->model && strlen(dac_set[i]->model)>0){
  1082. total_len+=strlen(dac_set[i]->model)+1;
  1083. }
  1084. else {
  1085. break;
  1086. }
  1087. }
  1088. total_len+=strlen(EXTRA_MODEL_NAMES);
  1089. dac_list = malloc_init_external(total_len+1);
  1090. if(dac_list){
  1091. for(int i=0;dac_set[i];i++){
  1092. if(dac_set[i]->model && strlen(dac_set[i]->model)>0){
  1093. strcat(dac_list,dac_set[i]->model);
  1094. strcat(dac_list,"|");
  1095. }
  1096. else {
  1097. break;
  1098. }
  1099. }
  1100. strcat(dac_list,EXTRA_MODEL_NAMES);
  1101. }
  1102. return dac_list;
  1103. }
  1104. void replace_char_in_string(char * str, char find, char replace){
  1105. for(int i=0;str[i];i++){
  1106. if(str[i]==find){
  1107. str[i]=replace;
  1108. }
  1109. }
  1110. }
  1111. static esp_err_t save_known_config(cJSON * known_item, const char * name,FILE * f){
  1112. esp_err_t err = ESP_OK;
  1113. char * json_string=NULL;
  1114. json_string = cJSON_Print(known_item);
  1115. ESP_LOGD(TAG,"known_item_string: %s",STR_OR_BLANK(json_string));
  1116. FREE_AND_NULL(json_string);
  1117. cJSON * kvp=NULL;
  1118. cJSON * config_array = cJSON_GetObjectItem(known_item,"config");
  1119. if(config_array && cJSON_IsArray(config_array)){
  1120. json_string = cJSON_Print(config_array);
  1121. ESP_LOGD(TAG,"config_array: %s",STR_OR_BLANK(json_string));
  1122. FREE_AND_NULL(json_string);
  1123. cJSON_ArrayForEach(kvp, config_array){
  1124. cJSON * kvp_value=kvp->child;
  1125. if(!kvp_value){
  1126. printf("config entry is not an object!\n");
  1127. err=ESP_FAIL;
  1128. continue;
  1129. }
  1130. char * key = kvp_value->string;
  1131. char * value = kvp_value->valuestring;
  1132. if(!key || !value || strlen(key)==0){
  1133. printf("Invalid config entry %s:%s\n",STR_OR_BLANK(key),STR_OR_BLANK(value));
  1134. err=ESP_FAIL;
  1135. continue;
  1136. }
  1137. fprintf(f,"Storing %s=%s\n",key,value);
  1138. err = config_set_value(NVS_TYPE_STR,key,value);
  1139. if(err){
  1140. fprintf(f,"Failed to store config value: %s\n",esp_err_to_name(err));
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. else {
  1146. json_string = cJSON_Print(config_array);
  1147. char * known_item_string = cJSON_Print(known_item);
  1148. 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));
  1149. FREE_AND_NULL(json_string);
  1150. FREE_AND_NULL(known_item_string);
  1151. err = ESP_FAIL;
  1152. }
  1153. if(err==ESP_OK){
  1154. err = config_set_value(NVS_TYPE_STR,"board_model",name);
  1155. if(err!=ESP_OK){
  1156. fprintf(f,"Failed to save board model %s\n",name);
  1157. }
  1158. }
  1159. return err;
  1160. }
  1161. static int do_register_known_templates_config(int argc, char **argv){
  1162. esp_err_t err=ESP_OK;
  1163. int nerrors = arg_parse(argc, argv,(void **)&known_model_args);
  1164. char *buf = NULL;
  1165. size_t buf_size = 0;
  1166. cJSON * config_name =NULL;
  1167. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  1168. if (f == NULL) {
  1169. return 1;
  1170. }
  1171. if(nerrors >0){
  1172. arg_print_errors(f,known_model_args.end,desc_preset);
  1173. }
  1174. else {
  1175. ESP_LOGD(TAG,"arg: %s",STR_OR_BLANK(known_model_args.model_config->sval[0]));
  1176. char * model_config = strdup_psram(known_model_args.model_config->sval[0]);
  1177. char * t = model_config;
  1178. for(const char * p=known_model_args.model_config->sval[0];*p;p++){
  1179. if(*p=='\\' && *(p+1)=='"'){
  1180. *t++='"';
  1181. p++;
  1182. }
  1183. else {
  1184. *t++=*p;
  1185. }
  1186. }
  1187. *t=0;
  1188. cJSON * known_item = cJSON_Parse(model_config);
  1189. if(known_item){
  1190. ESP_LOGD(TAG,"Parsing success");
  1191. config_name= cJSON_GetObjectItem(known_item,"name");
  1192. if(!config_name || !cJSON_IsString(config_name) || strlen(config_name->valuestring)==0){
  1193. fprintf(f,"Failed to find name in config\n");
  1194. err=ESP_FAIL;
  1195. nerrors++;
  1196. }
  1197. if(nerrors==0){
  1198. const char * name = cJSON_GetStringValue(config_name);
  1199. nerrors+=(err = save_known_config(known_item,name,f)!=ESP_OK);
  1200. if(nerrors==0){
  1201. const i2s_platform_config_t * i2s_config= config_dac_get();
  1202. if(i2s_config->scl!=-1 && i2s_config->sda!=-1 && GPIO_IS_VALID_GPIO(i2s_config->scl) && GPIO_IS_VALID_GPIO(i2s_config->sda)){
  1203. fprintf(f,"Scanning i2c bus for devices\n");
  1204. cmd_i2ctools_scan_bus(f,i2s_config->sda, i2s_config->scl);
  1205. }
  1206. }
  1207. }
  1208. cJSON_Delete(known_item);
  1209. }
  1210. else {
  1211. ESP_LOGE(TAG,"Parsing error: %s",cJSON_GetErrorPtr());
  1212. fprintf(f,"Failed to parse JSON: %s\n",cJSON_GetErrorPtr());
  1213. err=ESP_FAIL;
  1214. }
  1215. if(err!=ESP_OK){
  1216. nerrors++;
  1217. fprintf(f,"Error registering known config %s.\n",known_model_args.model_config->sval[0]);
  1218. }
  1219. else {
  1220. fprintf(f,"Registered known config %s.\n",known_model_args.model_config->sval[0]);
  1221. }
  1222. }
  1223. if(!nerrors ){
  1224. fprintf(f,"Done.\n");
  1225. }
  1226. fflush (f);
  1227. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  1228. fclose(f);
  1229. FREE_AND_NULL(buf);
  1230. return (nerrors==0 && err==ESP_OK)?0:1;
  1231. }
  1232. static void register_known_templates_config(){
  1233. 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");
  1234. known_model_args.end = arg_end(1);
  1235. const esp_console_cmd_t cmd = {
  1236. .command = CFG_TYPE_HW("preset"),
  1237. .help = desc_preset,
  1238. .hint = NULL,
  1239. .func = &do_register_known_templates_config,
  1240. .argtable = &known_model_args
  1241. };
  1242. cmd_to_json_with_cb(&cmd,&known_model_cb);
  1243. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1244. }
  1245. #ifdef CONFIG_CSPOT_SINK
  1246. static void register_cspot_config(){
  1247. cspot_args.deviceName = arg_str1(NULL,"deviceName","","Device Name");
  1248. cspot_args.bitrate = arg_int1(NULL,"bitrate","96|160|320","Streaming Bitrate (kbps)");
  1249. cspot_args.zeroConf = arg_int1(NULL,"zeroConf","0|1","Force use of ZeroConf");
  1250. // cspot_args.volume = arg_int1(NULL,"volume","","Spotify Volume");
  1251. cspot_args.end = arg_end(1);
  1252. const esp_console_cmd_t cmd = {
  1253. .command = CFG_TYPE_SYST("cspot"),
  1254. .help = desc_cspotc,
  1255. .hint = NULL,
  1256. .func = &do_cspot_config,
  1257. .argtable = &cspot_args
  1258. };
  1259. cmd_to_json_with_cb(&cmd,&cspot_cb);
  1260. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1261. }
  1262. #endif
  1263. static void register_i2s_config(void){
  1264. i2s_args.model_name = arg_str0(NULL,"model_name",STR_OR_BLANK(get_dac_list()),"DAC Model Name");
  1265. i2s_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  1266. i2s_args.clock = arg_int0(NULL,"clock","<n>","Clock GPIO. e.g. 33");
  1267. i2s_args.wordselect = arg_int0(NULL,"wordselect","<n>","Word Select GPIO. e.g. 25");
  1268. i2s_args.data = arg_int0(NULL,"data","<n>","Data GPIO. e.g. 32");
  1269. i2s_args.mute_gpio = arg_int0(NULL,"mute_gpio", "<n>", "Mute GPIO. e.g. 14");
  1270. i2s_args.mute_level = arg_lit0(NULL,"mute_level","Mute GPIO level. Checked=HIGH, Unchecked=LOW");
  1271. i2s_args.dac_sda = arg_int0(NULL,"dac_sda", "<n>", "SDA GPIO. e.g. 27");
  1272. i2s_args.dac_scl = arg_int0(NULL,"dac_scl", "<n>", "SCL GPIO. e.g. 26");
  1273. i2s_args.dac_i2c = arg_int0(NULL,"dac_i2c", "<n>", "I2C device address. e.g. 106");
  1274. i2s_args.end = arg_end(6);
  1275. const esp_console_cmd_t cmd = {
  1276. .command = CFG_TYPE_HW("dac"),
  1277. .help = desc_dac,
  1278. .hint = NULL,
  1279. .func = &do_i2s_cmd,
  1280. .argtable = &i2s_args
  1281. };
  1282. cmd_to_json_with_cb(&cmd,&i2s_cb);
  1283. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1284. }
  1285. static void register_bt_source_config(void){
  1286. bt_source_args.sink_name= arg_str1("n","sink_name", "name","Bluetooth audio device name. This applies when output mode is Bluetooth");
  1287. bt_source_args.pin_code= arg_str1("p","pin_code", "pin","Bluetooth security/pin code. Usually 0000. This applies when output mode is Bluetooth");
  1288. // 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.");
  1289. // 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.");
  1290. bt_source_args.end= arg_end(1);
  1291. const esp_console_cmd_t cmd = {
  1292. .command = CFG_TYPE_AUDIO("bt_source"),
  1293. .help = desc_bt_source,
  1294. .hint = NULL,
  1295. .func = &do_bt_source_cmd,
  1296. .argtable = &bt_source_args
  1297. };
  1298. cmd_to_json_with_cb(&cmd,&bt_source_cb);
  1299. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1300. }
  1301. void register_rotary_config(void){
  1302. 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.");
  1303. rotary_args.A = arg_int1(NULL,"A","gpio","A/DT gpio");
  1304. rotary_args.B = arg_int1(NULL,"B","gpio","B/CLK gpio");
  1305. rotary_args.SW = arg_int0(NULL,"SW","gpio","Switch gpio");
  1306. 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'");
  1307. 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. ");
  1308. rotary_args.volume_lock = arg_lit0(NULL,"volume_lock", "Force Volume down/up/play toggle all the time (even in LMS). ");
  1309. 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').");
  1310. rotary_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  1311. 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");
  1312. rotary_args.end = arg_end(3);
  1313. const esp_console_cmd_t cmd = {
  1314. .command = CFG_TYPE_HW("rotary"),
  1315. .help = desc_rotary,
  1316. .hint = NULL,
  1317. .func = &do_rotary_cmd,
  1318. .argtable = &rotary_args
  1319. };
  1320. cmd_to_json_with_cb(&cmd,&rotary_cb);
  1321. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1322. }
  1323. void register_ledvu_config(void){
  1324. ledvu_args.type = arg_str1(NULL,"type","<none>|WS2812","Led type (supports one rgb strip to display built in effects and allow remote control through 'dmx' messaging)");
  1325. ledvu_args.length = arg_int1(NULL,"length","<1..255>","Strip length (1-255 supported)");
  1326. ledvu_args.gpio = arg_int1(NULL,"gpio","gpio","Data pin");
  1327. ledvu_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  1328. ledvu_args.end = arg_end(4);
  1329. const esp_console_cmd_t cmd = {
  1330. .command = CFG_TYPE_HW("ledvu"),
  1331. .help = desc_ledvu,
  1332. .hint = NULL,
  1333. .func = &do_ledvu_cmd,
  1334. .argtable = &ledvu_args
  1335. };
  1336. cmd_to_json_with_cb(&cmd,&ledvu_cb);
  1337. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1338. }
  1339. void register_audio_config(void){
  1340. 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.");
  1341. 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.");
  1342. audio_args.end = arg_end(6);
  1343. audio_args.end = arg_end(6);
  1344. const esp_console_cmd_t cmd = {
  1345. .command = CFG_TYPE_AUDIO("general"),
  1346. .help = desc_audio,
  1347. .hint = NULL,
  1348. .func = &do_audio_cmd,
  1349. .argtable = &audio_args
  1350. };
  1351. cmd_to_json_with_cb(&cmd,&audio_cb);
  1352. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1353. }
  1354. static void register_spdif_config(void){
  1355. spdif_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  1356. spdif_args.clock = arg_int1(NULL,"clock","<n>","Clock GPIO. e.g. 33");
  1357. spdif_args.wordselect = arg_int1(NULL,"wordselect","<n>","Word Select GPIO. e.g. 25");
  1358. spdif_args.data = arg_int1(NULL,"data","<n>","Data GPIO. e.g. 32");
  1359. spdif_args.end = arg_end(6);
  1360. const esp_console_cmd_t cmd = {
  1361. .command = CFG_TYPE_HW("spdif"),
  1362. .help = desc_spdif,
  1363. .hint = NULL,
  1364. .func = &do_spdif_cmd,
  1365. .argtable = &spdif_args
  1366. };
  1367. cmd_to_json_with_cb(&cmd,&spdif_cb);
  1368. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1369. }
  1370. static void register_squeezelite_config(void){
  1371. squeezelite_args.server = arg_str0("s","server","<server>[:<port>]","Connect to specified server, otherwise uses autodiscovery to find server");
  1372. squeezelite_args.buffers = arg_str0("b","buffers","<stream>:<output>","Internal Stream and Output buffer sizes in Kbytes");
  1373. squeezelite_args.codecs = arg_strn("c","codecs","+" CODECS "+",0,20,"Restrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS );
  1374. squeezelite_args.timeout = arg_int0("C","timeout","<n>","Close output device when idle after timeout seconds, default is to keep it open while player is 'on");
  1375. 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 <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  1376. // squeezelite_args.log_level_all = arg_str0(NULL,"all",get_log_level_options("all"),"Overall Logging Level");
  1377. // squeezelite_args.log_level_slimproto = arg_str0(NULL,"loglevel_slimproto",get_log_level_options("slimproto"),"Slimproto Logging Level");
  1378. // squeezelite_args.log_level_stream= arg_str0(NULL,"loglevel_stream",get_log_level_options("stream"),"Stream Logging Level");
  1379. // squeezelite_args.log_level_decode= arg_str0(NULL,"loglevel_decode",get_log_level_options("decode"),"Decode Logging Level");
  1380. // squeezelite_args.log_level_output= arg_str0(NULL,"loglevel_output",get_log_level_options("output"),"Output Logging Level");
  1381. #if IR
  1382. squeezelite_args.log_level_ir= arg_str0(NULL,"loglevel_ir",get_log_level_options("ir"),"IR Logging Level");
  1383. #endif
  1384. squeezelite_args.output_device = arg_str0("o","output_device","<string>","Output device (BT, I2S or SPDIF)");
  1385. squeezelite_args.mac_addr = arg_str0("m","mac_addr","<string>","Mac address, format: ab:cd:ef:12:34:56.");
  1386. squeezelite_args.model_name = arg_str0("M", "modelname", "<string>","Set the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")");
  1387. squeezelite_args.name = arg_str0("n","name","<string>","Player name, if different from the current host name. Name can alternatively be assigned from the system/device name configuration.");
  1388. squeezelite_args.header_format = arg_lit0("W","header_format","Read wave and aiff format from header, ignore server parameters");
  1389. squeezelite_args.rates = arg_str0("r","rates","<rates>[:<delay>]", "Sample rates supported, allows output to be off when squeezelite is started; rates = <maxrate>|<minrate>-<maxrate>|<rate1>,<rate2>,<rate3>; delay = optional delay switching rates in ms\n");
  1390. #if RESAMPLE
  1391. squeezelite_args.resample = arg_lit0("R","resample","Activate Resample");
  1392. squeezelite_args.resample_parms = arg_str0("u","resample_parms","<recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>","Resample, params");
  1393. #endif
  1394. #if RESAMPLE16
  1395. squeezelite_args.resample = arg_lit0("R","resample","Activate Resample");
  1396. 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");
  1397. #endif
  1398. squeezelite_args.rate = arg_int0("Z","max_rate", "<n>", "Report rate to server in helo as the maximum sample rate we can support");
  1399. squeezelite_args.end = arg_end(6);
  1400. const esp_console_cmd_t cmd = {
  1401. .command = CFG_TYPE_AUDIO("squeezelite"),
  1402. .help = desc_squeezelite,
  1403. .hint = NULL,
  1404. .func = &do_squeezelite_cmd,
  1405. .argtable = &squeezelite_args
  1406. };
  1407. cmd_to_json_with_cb(&cmd,&squeezelite_cb);
  1408. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  1409. }
  1410. void dummy_register_cmd(){
  1411. }
  1412. void register_config_cmd(void){
  1413. if(!is_dac_config_locked()){
  1414. register_known_templates_config();
  1415. }
  1416. #ifdef CONFIG_CSPOT_SINK
  1417. register_cspot_config();
  1418. #endif
  1419. register_bt_source_config();
  1420. #if CONFIG_WITH_CONFIG_UI
  1421. if(!is_dac_config_locked()){
  1422. register_i2s_config();
  1423. }
  1424. else {
  1425. #if defined(CONFIG_WITH_METRICS)
  1426. metrics_add_feature("i2s",true);
  1427. #endif
  1428. }
  1429. if(!is_spdif_config_locked()){
  1430. register_spdif_config();
  1431. }
  1432. else {
  1433. #if defined(CONFIG_WITH_METRICS)
  1434. metrics_add_feature("spdif",true);
  1435. #endif
  1436. }
  1437. #endif
  1438. register_optional_cmd();
  1439. }