cmd_config.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. //#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  8. #include <stdio.h>
  9. #include "cmd_config.h"
  10. #include "argtable3/argtable3.h"
  11. #include "platform_console.h"
  12. #include "esp_log.h"
  13. #include "string.h"
  14. #include "stdio.h"
  15. #include "platform_config.h"
  16. #include "trace.h"
  17. #include "messaging.h"
  18. #include "accessors.h"
  19. const char * desc_squeezelite ="Squeezelite Options";
  20. const char * desc_dac= "DAC Options";
  21. const char * desc_spdif= "SPDIF Options";
  22. const char * desc_audio= "General Audio Options";
  23. const char * desc_bt_source= "Bluetooth Audio Output Options";
  24. const char * desc_rotary= "Rotary Control";
  25. #define CODECS_BASE "flac|pcm|mp3|ogg"
  26. #if NO_FAAD
  27. #define CODECS_AAC ""
  28. #else
  29. #define CODECS_AAC "|aac"
  30. #endif
  31. #if FFMPEG
  32. #define CODECS_FF "|wma|alac"
  33. #else
  34. #define CODECS_FF ""
  35. #endif
  36. #if DSD
  37. #define CODECS_DSD "|dsd"
  38. #else
  39. #define CODECS_DSD ""
  40. #endif
  41. #define CODECS_MP3 "|mad|mpg"
  42. #if !defined(MODEL_NAME)
  43. #define MODEL_NAME SqueezeLite
  44. #endif
  45. #ifndef QUOTE
  46. #define QUOTE(name) #name
  47. #define STR(macro) QUOTE(macro)
  48. #endif
  49. #ifndef MODEL_NAME_STRING
  50. #define MODEL_NAME_STRING STR(MODEL_NAME)
  51. #endif
  52. #define CODECS CODECS_BASE CODECS_AAC CODECS_FF CODECS_DSD CODECS_MP3
  53. #define NOT_OUTPUT "has input capabilities only"
  54. #define NOT_GPIO "is not a GPIO"
  55. typedef enum {
  56. SEARCHING_FOR_BT,
  57. SEARCHING_FOR_NAME,
  58. SEARCHING_FOR_NAME_START,
  59. SEARCHING_FOR_NAME_END,
  60. SEARCHING_FOR_BT_CMD_END,
  61. FINISHING
  62. } parse_state_t;
  63. static const char *TAG = "cmd_config";
  64. extern struct arg_end *getParmsEnd(struct arg_hdr * * argtable);
  65. //bck=<gpio>,ws=<gpio>,do=<gpio>[,mute=<gpio>[:0|1][,model=TAS57xx|TAS5713|AC101|WM8978|I2S][,sda=<gpio>,scl=gpio[,i2c=<addr>]]
  66. static struct {
  67. struct arg_str *model_name;
  68. struct arg_int *clock;
  69. struct arg_int *wordselect;
  70. struct arg_int *data;
  71. struct arg_int *mute_gpio;
  72. struct arg_lit *mute_level;
  73. struct arg_int *dac_sda;
  74. struct arg_int *dac_scl;
  75. struct arg_int *dac_i2c;
  76. struct arg_lit *clear;
  77. struct arg_end *end;
  78. } i2s_args;
  79. static struct {
  80. struct arg_rem * rem;
  81. struct arg_int * A;
  82. struct arg_int * B;
  83. struct arg_int * SW;
  84. struct arg_lit * volume_lock;
  85. struct arg_lit * longpress;
  86. struct arg_lit * knobonly;
  87. struct arg_int * timer;
  88. struct arg_lit * clear;
  89. struct arg_end * end;
  90. } rotary_args;
  91. //config_rotary_get
  92. static struct{
  93. struct arg_str *sink_name;
  94. struct arg_str *pin_code;
  95. // struct arg_dbl *connect_timeout_delay;
  96. // struct arg_dbl *control_delay;
  97. struct arg_end *end;
  98. } bt_source_args;
  99. static struct {
  100. struct arg_int *clock;
  101. struct arg_int *wordselect;
  102. struct arg_int *data;
  103. struct arg_lit *clear;
  104. struct arg_end *end;
  105. } spdif_args;
  106. static struct {
  107. struct arg_str *jack_behavior;
  108. struct arg_end *end;
  109. } audio_args;
  110. static struct {
  111. struct arg_str * output_device; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  112. struct arg_str * name;// " -n <name>\t\tSet the player name\n"
  113. struct arg_str * server; // -s <server>[:<port>]\tConnect to specified server, otherwise uses autodiscovery to find server\n"
  114. struct arg_str * buffers;// " -b <stream>:<output>\tSpecify internal Stream and Output buffer sizes in Kbytes\n"
  115. struct arg_str * codecs;// " -c <codec1>,<codec2>\tRestrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS "\n"
  116. 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"
  117. struct arg_str * log_level; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  118. // struct arg_str * log_level_all; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  119. // struct arg_str * log_level_slimproto; // " -d <log>=<level>\tSet logging level, logs: all|slimproto|stream|decode|output|ir, level: info|debug|sdebug\n"
  120. // struct arg_str * log_level_stream;
  121. // struct arg_str * log_level_decode;
  122. // struct arg_str * log_level_output;
  123. #if IR
  124. struct arg_str * log_level_ir;
  125. #endif
  126. // " -e <codec1>,<codec2>\tExplicitly exclude native support of one or more codecs; known codecs: " CODECS "\n"
  127. // " -f <logfile>\t\tWrite debug to logfile\n"
  128. // #if IR
  129. // " -i [<filename>]\tEnable lirc remote control support (lirc config file ~/.lircrc used if filename not specified)\n"
  130. // #endif
  131. struct arg_str * mac_addr; // " -m <mac addr>\t\tSet mac address, format: ab:cd:ef:12:34:56\n"
  132. struct arg_str * model_name;// " -M <modelname>\tSet the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")\n"
  133. struct arg_lit * header_format;// " -W\t\t\tRead wave and aiff format from header, ignore server parameters\n"
  134. 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"
  135. #if RESAMPLE
  136. struct arg_lit * resample;
  137. struct arg_str * resample_parms; //" -R -u [params]\tResample, params = <recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>,\n"
  138. #endif
  139. #if RESAMPLE16
  140. struct arg_lit * resample;
  141. struct arg_str * resample_parms; //" -R -u [params]\tResample, params = (b|l|m)[:i],\n"
  142. // " \t\t\t b = basic linear interpolation, l = 13 taps, m = 21 taps, i = interpolate filter coefficients\n"
  143. #endif
  144. struct arg_int * rate;// " -Z <rate>\t\tReport rate to server in helo as the maximum sample rate we can support\n"
  145. struct arg_end *end;
  146. } squeezelite_args;
  147. int is_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory, bool output){
  148. int res = 0;
  149. const char * name = gpio->hdr.longopts?gpio->hdr.longopts:gpio->hdr.glossary;
  150. *gpio_out=-1;
  151. int t_gpio=gpio->ival[0];
  152. if(gpio->count==0){
  153. if(mandatory){
  154. fprintf(f,"Missing: %s\n", name);
  155. res++;
  156. }
  157. } else if((output && !GPIO_IS_VALID_OUTPUT_GPIO(t_gpio)) || (!GPIO_IS_VALID_GPIO(t_gpio))){
  158. fprintf(f,"Invalid %s gpio: [%d] %s\n",name, t_gpio, GPIO_IS_VALID_GPIO(t_gpio)?NOT_OUTPUT:NOT_GPIO );
  159. res++;
  160. }
  161. else{
  162. *gpio_out = t_gpio;
  163. }
  164. return res;
  165. }
  166. int is_output_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory){
  167. return is_gpio(gpio,f,gpio_out,mandatory,true);
  168. }
  169. int check_missing_parm(struct arg_int * int_parm, FILE * f){
  170. int res=0;
  171. const char * name = int_parm->hdr.longopts?int_parm->hdr.longopts:int_parm->hdr.glossary;
  172. if(int_parm->count==0){
  173. fprintf(f,"Missing: %s\n", name);
  174. res++;
  175. }
  176. return res;
  177. }
  178. char * strip_bt_name(char * opt_str)
  179. {
  180. char *result = malloc(strlen(opt_str)+1);
  181. memset(result, 0x00, strlen(opt_str)+1);
  182. char *str = strdup(opt_str);
  183. const char * output_marker=" -o";
  184. if(!result ){
  185. ESP_LOGE(TAG,"Error allocating memory for result.");
  186. return opt_str;
  187. }
  188. if(!str){
  189. ESP_LOGE(TAG,"Error duplicating command line string.");
  190. return opt_str;
  191. }
  192. bool quoted=false;
  193. parse_state_t state = SEARCHING_FOR_BT;
  194. char *start = strstr(str, output_marker);
  195. if (start)
  196. {
  197. ESP_LOGV(TAG,"Found output option : %s\n",start);
  198. start+=strlen(output_marker);
  199. strncpy(result, str, (size_t)(start - str));
  200. char * pch=strtok(start," ");
  201. while(pch){
  202. ESP_LOGV(TAG,"Current output: %s\n[%s]",result,pch);
  203. switch (state)
  204. {
  205. case SEARCHING_FOR_BT:
  206. if (strcasestr(pch, "BT") )
  207. {
  208. state = SEARCHING_FOR_NAME;
  209. quoted=strcasestr(pch, "BT")!=NULL;
  210. ESP_LOGV(TAG," - fount BT Start %s", quoted?"quoted":"");
  211. }
  212. else
  213. {
  214. ESP_LOGV(TAG," - Searching for BT, Ignoring");
  215. }
  216. strcat(result, " ");
  217. strcat(result, pch);
  218. break;
  219. case SEARCHING_FOR_NAME:
  220. if (strcasestr(pch, "name") || strcasestr(pch, "n"))
  221. {
  222. ESP_LOGV(TAG," - Found name tag");
  223. state = SEARCHING_FOR_NAME_START;
  224. }
  225. else
  226. {
  227. strcat(result, " ");
  228. strcat(result, pch);
  229. ESP_LOGV(TAG," - Searching for name - added ");;
  230. }
  231. break;
  232. case SEARCHING_FOR_NAME_START:
  233. ESP_LOGV(TAG," - Name start");
  234. state = SEARCHING_FOR_NAME_END;
  235. break;
  236. case SEARCHING_FOR_NAME_END:
  237. if (strcasestr(pch, "\"")){
  238. ESP_LOGV(TAG," - got quoted string");
  239. state = FINISHING;
  240. }
  241. else if(pch[0]== '-'){
  242. strcat(result, " ");
  243. strcat(result, pch);
  244. ESP_LOGV(TAG," - got parameter marker");
  245. state = quoted?SEARCHING_FOR_BT_CMD_END:FINISHING;
  246. }
  247. else {
  248. ESP_LOGV(TAG," - name continued");
  249. }
  250. break;
  251. case SEARCHING_FOR_BT_CMD_END:
  252. ESP_LOGV(TAG," - looking for quoted BT cmd end");
  253. if (strcasestr(pch, "\"")){
  254. ESP_LOGV(TAG," - got quote termination");
  255. state = FINISHING;
  256. }
  257. strcat(result, " ");
  258. strcat(result, pch);
  259. break;
  260. case FINISHING:
  261. strcat(result, " ");
  262. strcat(result, pch);
  263. break;
  264. default:
  265. break;
  266. }
  267. pch = strtok(NULL, " ");
  268. ESP_LOGV(TAG,"\n");
  269. }
  270. }
  271. else
  272. {
  273. ESP_LOGE(TAG,"output option not found in %s\n",str);
  274. strcpy(result,str);
  275. }
  276. ESP_LOGV(TAG,"Result commmand : %s\n", result);
  277. free(str);
  278. return result;
  279. }
  280. static int do_bt_source_cmd(int argc, char **argv){
  281. esp_err_t err=ESP_OK;
  282. int nerrors = arg_parse(argc, argv,(void **)&bt_source_args);
  283. char *buf = NULL;
  284. size_t buf_size = 0;
  285. // char value[100] ={0};
  286. FILE *f = open_memstream(&buf, &buf_size);
  287. if (f == NULL) {
  288. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n");
  289. return 1;
  290. }
  291. if(nerrors >0){
  292. arg_print_errors(f,bt_source_args.end,desc_bt_source);
  293. fclose(f);
  294. return 1;
  295. }
  296. if(bt_source_args.sink_name->count >0){
  297. err = config_set_value(NVS_TYPE_STR, "a2dp_sink_name", bt_source_args.sink_name->sval[0]);
  298. if(err!=ESP_OK){
  299. nerrors++;
  300. fprintf(f,"Error setting Bluetooth audio device name %s. %s\n",bt_source_args.sink_name->sval[0], esp_err_to_name(err));
  301. }
  302. else {
  303. fprintf(f,"Bluetooth audio device name changed to %s\n",bt_source_args.sink_name->sval[0]);
  304. }
  305. char * squeezelite_cmd = config_alloc_get_default(NVS_TYPE_STR, "autoexec1", NULL, 0);
  306. if( squeezelite_cmd && strstr(squeezelite_cmd," -o ") ){
  307. char * new_cmd = strip_bt_name(squeezelite_cmd);
  308. if(strcmp(new_cmd,squeezelite_cmd)!=0){
  309. fprintf(f,"Replacing old squeezelite command [%s] with [%s].\n",squeezelite_cmd,new_cmd);
  310. config_set_value(NVS_TYPE_STR, "autoexec1", new_cmd);
  311. if(err!=ESP_OK){
  312. nerrors++;
  313. fprintf(f,"Error updating squeezelite command line options . %s\n", esp_err_to_name(err));
  314. }
  315. }
  316. free(squeezelite_cmd);
  317. free(new_cmd);
  318. }
  319. }
  320. if(bt_source_args.pin_code->count >0){
  321. const char * v=bt_source_args.pin_code->sval[0];
  322. bool bInvalid=false;
  323. for(int i=0;i<strlen(v) && !bInvalid;i++){
  324. if(v[i]<'0' || v[i]>'9'){
  325. bInvalid=true;
  326. }
  327. }
  328. if(bInvalid || strlen(bt_source_args.pin_code->sval[0])>16 || strlen(bt_source_args.pin_code->sval[0])<4){
  329. nerrors++;
  330. 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]);
  331. }
  332. else {
  333. err = config_set_value(NVS_TYPE_STR, "a2dp_spin", bt_source_args.pin_code->sval[0]);
  334. if(err!=ESP_OK){
  335. nerrors++;
  336. fprintf(f,"Error setting Bluetooth source pin to %s. %s\n",bt_source_args.pin_code->sval[0], esp_err_to_name(err));
  337. }
  338. else {
  339. fprintf(f,"Bluetooth source pin changed to %s\n",bt_source_args.pin_code->sval[0]);
  340. }
  341. }
  342. }
  343. // if(bt_source_args.connect_timeout_delay->count >0){
  344. // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.connect_timeout_delay->dval[0]*1000.0));
  345. // if(bt_source_args.connect_timeout_delay->dval[0] <0.5 || bt_source_args.connect_timeout_delay->dval[0] >5.0){
  346. // nerrors++;
  347. // 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 );
  348. // }
  349. // else {
  350. // err = config_set_value(NVS_TYPE_STR, "a2dp_ctmt", value);
  351. // if(err!=ESP_OK){
  352. // nerrors++;
  353. // 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));
  354. // }
  355. // else {
  356. // fprintf(f,"Connection timeout changed to %0.0f sec (%s milliseconds)\n",bt_source_args.connect_timeout_delay->dval[0],value);
  357. // }
  358. // }
  359. // }
  360. // if(bt_source_args.control_delay->count >0){
  361. // snprintf(value,sizeof(value),"%d",(int)(bt_source_args.control_delay->dval[0]*1000.0));
  362. // if(bt_source_args.control_delay->dval[0] <0.1 || bt_source_args.control_delay->dval[0] >2.0){
  363. // nerrors++;
  364. // 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 );
  365. // }
  366. // else {
  367. // err = config_set_value(NVS_TYPE_STR, "a2dp_ctrld", value);
  368. // if(err!=ESP_OK){
  369. // nerrors++;
  370. // 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));
  371. // }
  372. // else {
  373. // fprintf(f,"Control delay changed to %0.0f sec (%s milliseconds)\n",bt_source_args.control_delay->dval[0],value);
  374. // }
  375. // }
  376. // }
  377. if(!nerrors ){
  378. fprintf(f,"Done.\n");
  379. }
  380. fflush (f);
  381. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  382. fclose(f);
  383. FREE_AND_NULL(buf);
  384. return (nerrors==0 && err==ESP_OK)?0:1;
  385. }
  386. static int do_audio_cmd(int argc, char **argv){
  387. esp_err_t err=ESP_OK;
  388. int nerrors = arg_parse(argc, argv,(void **)&audio_args);
  389. char *buf = NULL;
  390. size_t buf_size = 0;
  391. FILE *f = open_memstream(&buf, &buf_size);
  392. if (f == NULL) {
  393. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n");
  394. return 1;
  395. }
  396. if(nerrors >0){
  397. arg_print_errors(f,audio_args.end,desc_audio);
  398. fclose(f);
  399. return 1;
  400. }
  401. if(audio_args.jack_behavior->count>0){
  402. err = ESP_OK; // suppress any error code that might have happened in a previous step
  403. if(strcasecmp(audio_args.jack_behavior->sval[0],"Headphones")==0){
  404. err = config_set_value(NVS_TYPE_STR, "jack_mutes_amp", "y");
  405. }
  406. else if(strcasecmp(audio_args.jack_behavior->sval[0],"Subwoofer")==0){
  407. err = config_set_value(NVS_TYPE_STR, "jack_mutes_amp", "n");
  408. }
  409. else {
  410. nerrors++;
  411. fprintf(f,"Unknown Audio Jack Behavior %s.\n",audio_args.jack_behavior->sval[0]);
  412. }
  413. if(err!=ESP_OK){
  414. nerrors++;
  415. fprintf(f,"Error setting Audio Jack Behavior %s. %s\n",audio_args.jack_behavior->sval[0], esp_err_to_name(err));
  416. }
  417. else {
  418. fprintf(f,"Audio Jack Behavior changed to %s\n",audio_args.jack_behavior->sval[0]);
  419. }
  420. }
  421. if(!nerrors ){
  422. fprintf(f,"Done.\n");
  423. }
  424. fflush (f);
  425. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  426. fclose(f);
  427. FREE_AND_NULL(buf);
  428. return (nerrors==0 && err==ESP_OK)?0:1;
  429. }
  430. static int do_spdif_cmd(int argc, char **argv){
  431. i2s_platform_config_t i2s_dac_pin = {
  432. .i2c_addr = -1,
  433. .sda= -1,
  434. .scl = -1,
  435. .mute_gpio = -1,
  436. .mute_level = -1
  437. };
  438. if(is_spdif_config_locked()){
  439. cmd_send_messaging(argv[0],MESSAGING_ERROR,"SPDIF Configuration is locked on this platform\n");
  440. return 1;
  441. }
  442. esp_err_t err=ESP_OK;
  443. int nerrors = arg_parse(argc, argv,(void **)&spdif_args);
  444. if (spdif_args.clear->count) {
  445. cmd_send_messaging(argv[0],MESSAGING_WARNING,"SPDIF config cleared\n");
  446. config_set_value(NVS_TYPE_STR, "spdif_config", "");
  447. return 0;
  448. }
  449. char *buf = NULL;
  450. size_t buf_size = 0;
  451. FILE *f = open_memstream(&buf, &buf_size);
  452. if (f == NULL) {
  453. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n");
  454. return 1;
  455. }
  456. if(nerrors >0){
  457. arg_print_errors(f,spdif_args.end,desc_dac);
  458. fclose(f);
  459. return 1;
  460. }
  461. nerrors+=is_output_gpio(spdif_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true);
  462. nerrors+=is_output_gpio(spdif_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true);
  463. nerrors+=is_output_gpio(spdif_args.data, f, &i2s_dac_pin.pin.data_out_num, true);
  464. if(!nerrors ){
  465. fprintf(f,"Storing SPDIF parameters.\n");
  466. nerrors+=(config_spdif_set(&i2s_dac_pin )!=ESP_OK);
  467. }
  468. if(!nerrors ){
  469. fprintf(f,"Done.\n");
  470. }
  471. fflush (f);
  472. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  473. fclose(f);
  474. FREE_AND_NULL(buf);
  475. return (nerrors==0 && err==ESP_OK)?0:1;
  476. }
  477. static int do_rotary_cmd(int argc, char **argv){
  478. rotary_struct_t rotary={ .A = -1, .B = -1, .SW = -1, .longpress = 0, .knobonly=0,.volume_lock=false};
  479. esp_err_t err=ESP_OK;
  480. int nerrors = arg_parse(argc, argv,(void **)&rotary_args);
  481. if (rotary_args.clear->count) {
  482. cmd_send_messaging(argv[0],MESSAGING_WARNING,"rotary config cleared\n");
  483. config_set_value(NVS_TYPE_STR, "rotary_config", "");
  484. return 0;
  485. }
  486. char *buf = NULL;
  487. size_t buf_size = 0;
  488. FILE *f = open_memstream(&buf, &buf_size);
  489. if (f == NULL) {
  490. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n");
  491. return 1;
  492. }
  493. if(nerrors >0){
  494. arg_print_errors(f,rotary_args.end,desc_rotary);
  495. fclose(f);
  496. return 1;
  497. }
  498. nerrors+=is_gpio(rotary_args.A, f, &rotary.A, true,false);
  499. nerrors+=is_gpio(rotary_args.B, f, &rotary.B, true,false);
  500. nerrors+=is_gpio(rotary_args.SW, f, &rotary.SW,false,false);
  501. if(rotary_args.knobonly->count>0 && (rotary_args.volume_lock->count>0 || rotary_args.longpress->count>0)){
  502. fprintf(f,"error: Cannot use volume lock or longpress option when knob only option selected\n");
  503. nerrors++;
  504. }
  505. if(rotary_args.timer->count>0 && rotary_args.timer->ival[0]<0){
  506. fprintf(f,"error: knob only timer should be greater than or equal to zero.\n");
  507. nerrors++;
  508. }
  509. else {
  510. rotary.timer = rotary_args.timer->count>0?rotary_args.timer->ival[0]:0;
  511. }
  512. rotary.knobonly = rotary_args.knobonly->count>0;
  513. rotary.volume_lock= rotary_args.volume_lock->count>0;
  514. rotary.longpress = rotary_args.longpress->count>0;
  515. if(!nerrors ){
  516. fprintf(f,"Storing rotary parameters.\n");
  517. nerrors+=(config_rotary_set(&rotary )!=ESP_OK);
  518. }
  519. if(!nerrors ){
  520. fprintf(f,"Done.\n");
  521. }
  522. fflush (f);
  523. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  524. fclose(f);
  525. FREE_AND_NULL(buf);
  526. return (nerrors==0 && err==ESP_OK)?0:1;
  527. }
  528. static int do_i2s_cmd(int argc, char **argv)
  529. {
  530. i2s_platform_config_t i2s_dac_pin = {
  531. .i2c_addr = -1,
  532. .sda= -1,
  533. .scl = -1,
  534. .mute_gpio = -1,
  535. .mute_level = -1
  536. };
  537. if(is_dac_config_locked()){
  538. cmd_send_messaging(argv[0],MESSAGING_ERROR,"DAC Configuration is locked on this platform\n");
  539. return 1;
  540. }
  541. strcpy(i2s_dac_pin.model, "I2S");
  542. esp_err_t err=ESP_OK;
  543. int nerrors = arg_parse(argc, argv,(void **)&i2s_args);
  544. if (i2s_args.clear->count) {
  545. cmd_send_messaging(argv[0],MESSAGING_WARNING,"DAC config cleared\n");
  546. config_set_value(NVS_TYPE_STR, "dac_config", "");
  547. return 0;
  548. }
  549. char *buf = NULL;
  550. size_t buf_size = 0;
  551. FILE *f = open_memstream(&buf, &buf_size);
  552. if (f == NULL) {
  553. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n");
  554. return 1;
  555. }
  556. if(nerrors >0){
  557. arg_print_errors(f,i2s_args.end,desc_dac);
  558. fclose(f);
  559. return 1;
  560. }
  561. nerrors+=is_output_gpio(i2s_args.clock, f, &i2s_dac_pin.pin.bck_io_num, true);
  562. nerrors+=is_output_gpio(i2s_args.wordselect, f, &i2s_dac_pin.pin.ws_io_num, true);
  563. nerrors+=is_output_gpio(i2s_args.data, f, &i2s_dac_pin.pin.data_out_num, true);
  564. nerrors+=is_output_gpio(i2s_args.mute_gpio, f, &i2s_dac_pin.mute_gpio, false);
  565. if(i2s_dac_pin.mute_gpio>0){
  566. i2s_dac_pin.mute_level = i2s_args.mute_level->count>0?1:0;
  567. }
  568. if(i2s_args.dac_sda->count>0 && i2s_args.dac_sda->ival[0]>=0){
  569. // if SDA specified, then SDA and SCL are both mandatory
  570. nerrors+=is_output_gpio(i2s_args.dac_sda, f, &i2s_dac_pin.sda, false);
  571. nerrors+=is_output_gpio(i2s_args.dac_scl, f, &i2s_dac_pin.scl, false);
  572. }
  573. if(i2s_args.dac_sda->count==0&& i2s_args.dac_i2c->count>0){
  574. fprintf(f,"warning: ignoring i2c address, since dac i2c gpios config is incomplete\n");
  575. }
  576. else if(i2s_args.dac_i2c->count>0){
  577. i2s_dac_pin.i2c_addr = i2s_args.dac_i2c->ival[0];
  578. }
  579. if(i2s_args.model_name->count>0 && strlen(i2s_args.model_name->sval[0])>0){
  580. strncpy(i2s_dac_pin.model,i2s_args.model_name->sval[0],sizeof(i2s_dac_pin.model));
  581. i2s_dac_pin.model[sizeof(i2s_dac_pin.model) - 1] = '\0';
  582. }
  583. if(!nerrors ){
  584. fprintf(f,"Storing i2s parameters.\n");
  585. nerrors+=(config_i2s_set(&i2s_dac_pin, "dac_config")!=ESP_OK);
  586. }
  587. if(!nerrors ){
  588. fprintf(f,"Done.\n");
  589. }
  590. fflush (f);
  591. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  592. fclose(f);
  593. FREE_AND_NULL(buf);
  594. return (nerrors==0 && err==ESP_OK)?0:1;
  595. }
  596. cJSON * example_cb(){
  597. cJSON * values = cJSON_CreateObject();
  598. // int i2c_port;
  599. // const i2c_config_t * i2c= config_i2c_get(&i2c_port);
  600. // if(i2c->scl_io_num>0) {
  601. // cJSON_AddNumberToObject(values,"scl",i2c->scl_io_num);
  602. // }
  603. // if(i2c->sda_io_num>0) {
  604. // cJSON_AddNumberToObject(values,"sda",i2c->sda_io_num);
  605. // }
  606. // if(i2c->master.clk_speed>0) {
  607. // cJSON_AddNumberToObject(values,"freq",i2c->master.clk_speed);
  608. // }
  609. // if(i2c_port>0) {
  610. // cJSON_AddNumberToObject(values,"port",i2c_port);
  611. // }
  612. return values;
  613. }
  614. //const i2s_pin_config_t * config_get_spdif_pin_struct( );
  615. cJSON * i2s_cb(){
  616. cJSON * values = cJSON_CreateObject();
  617. const i2s_platform_config_t * i2s_conf= config_dac_get( );
  618. if(i2s_conf->pin.bck_io_num>0 ) {
  619. cJSON_AddNumberToObject(values,"clock",i2s_conf->pin.bck_io_num);
  620. }
  621. if(i2s_conf->pin.ws_io_num>=0 ) {
  622. cJSON_AddNumberToObject(values,"wordselect",i2s_conf->pin.ws_io_num);
  623. }
  624. if(i2s_conf->pin.data_out_num>=0 ) {
  625. cJSON_AddNumberToObject(values,"data",i2s_conf->pin.data_out_num);
  626. }
  627. if(i2s_conf->sda>=0 ) {
  628. cJSON_AddNumberToObject(values,"dac_sda",i2s_conf->sda);
  629. }
  630. if(i2s_conf->scl>=0 ) {
  631. cJSON_AddNumberToObject(values,"dac_scl",i2s_conf->scl);
  632. }
  633. if(i2s_conf->i2c_addr>=0 ) {
  634. cJSON_AddNumberToObject(values,"dac_i2c",i2s_conf->i2c_addr);
  635. }
  636. if(i2s_conf->mute_gpio>=0 ) {
  637. cJSON_AddNumberToObject(values,"mute_gpio",i2s_conf->mute_gpio);
  638. }
  639. if(i2s_conf->mute_level>=0 ) {
  640. cJSON_AddBoolToObject(values,"mute_level",i2s_conf->mute_level>0);
  641. }
  642. if(strlen(i2s_conf->model)>0){
  643. cJSON_AddStringToObject(values,"model_name",i2s_conf->model);
  644. }
  645. else {
  646. cJSON_AddStringToObject(values,"model_name","I2S");
  647. }
  648. return values;
  649. }
  650. cJSON * spdif_cb(){
  651. cJSON * values = cJSON_CreateObject();
  652. const i2s_platform_config_t * spdif_conf= config_spdif_get( );
  653. if(spdif_conf->pin.bck_io_num>0 ) {
  654. cJSON_AddNumberToObject(values,"clock",spdif_conf->pin.bck_io_num);
  655. }
  656. if(spdif_conf->pin.ws_io_num>=0 ) {
  657. cJSON_AddNumberToObject(values,"wordselect",spdif_conf->pin.ws_io_num);
  658. }
  659. if(spdif_conf->pin.data_out_num>=0 ) {
  660. cJSON_AddNumberToObject(values,"data",spdif_conf->pin.data_out_num);
  661. }
  662. return values;
  663. }
  664. cJSON * rotary_cb(){
  665. cJSON * values = cJSON_CreateObject();
  666. const rotary_struct_t *rotary= config_rotary_get();
  667. if(GPIO_IS_VALID_GPIO(rotary->A ) && rotary->A>=0 && GPIO_IS_VALID_GPIO(rotary->B) && rotary->B>=0){
  668. cJSON_AddNumberToObject(values,"A",rotary->A);
  669. cJSON_AddNumberToObject(values,"B",rotary->B);
  670. if(GPIO_IS_VALID_GPIO(rotary->SW ) && rotary->SW>=0 ){
  671. cJSON_AddNumberToObject(values,"SW",rotary->SW);
  672. }
  673. cJSON_AddBoolToObject(values,"volume_lock",rotary->volume_lock);
  674. cJSON_AddBoolToObject(values,"longpress",rotary->longpress);
  675. cJSON_AddBoolToObject(values,"knobonly",rotary->knobonly);
  676. cJSON_AddNumberToObject(values,"timer",rotary->timer);
  677. }
  678. return values;
  679. }
  680. cJSON * audio_cb(){
  681. cJSON * values = cJSON_CreateObject();
  682. char * p = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
  683. cJSON_AddStringToObject(values,"jack_behavior",(strcmp(p,"1") == 0 ||strcasecmp(p,"y") == 0)?"Headphones":"Subwoofer");
  684. FREE_AND_NULL(p);
  685. return values;
  686. }
  687. cJSON * bt_source_cb(){
  688. cJSON * values = cJSON_CreateObject();
  689. char * p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_sink_name", NULL, 0);
  690. if(p){
  691. cJSON_AddStringToObject(values,"sink_name",p);
  692. }
  693. FREE_AND_NULL(p);
  694. // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctmt", NULL, 0);
  695. // if(p){
  696. // cJSON_AddNumberToObject(values,"connect_timeout_delay",((double)atoi(p)/1000.0));
  697. // }
  698. // FREE_AND_NULL(p);
  699. p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_spin", "0000", 0);
  700. if(p){
  701. cJSON_AddStringToObject(values,"pin_code",p);
  702. }
  703. FREE_AND_NULL(p);
  704. // p = config_alloc_get_default(NVS_TYPE_STR, "a2dp_ctrld", NULL, 0);
  705. // if(p){
  706. // cJSON_AddNumberToObject(values,"control_delay",((double)atoi(p)/1000.0));
  707. // }
  708. // FREE_AND_NULL(p);
  709. return values;
  710. }
  711. void get_str_parm_json(struct arg_str * parm, cJSON * entry){
  712. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  713. if(parm->count>0){
  714. cJSON_AddStringToObject(entry,name,parm->sval[0]);
  715. }
  716. }
  717. void get_file_parm_json(struct arg_file * parm, cJSON * entry){
  718. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  719. if(parm->count>0){
  720. cJSON_AddStringToObject(entry,name,parm->filename[0]);
  721. }
  722. }
  723. void get_lit_parm_json(struct arg_lit * parm, cJSON * entry){
  724. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  725. cJSON_AddBoolToObject(entry,name,(parm->count>0));
  726. }
  727. void get_int_parm_json(struct arg_int * parm, cJSON * entry){
  728. const char * name = parm->hdr.longopts?parm->hdr.longopts:parm->hdr.glossary;
  729. if(parm->count>0){
  730. cJSON_AddNumberToObject(entry,name,parm->ival[0]);
  731. }
  732. }
  733. static int do_squeezelite_cmd(int argc, char **argv)
  734. {
  735. esp_err_t err=ESP_OK;
  736. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr ** )&squeezelite_args);
  737. char *buf = NULL;
  738. size_t buf_size = 0;
  739. FILE *f = open_memstream(&buf, &buf_size);
  740. if (f == NULL) {
  741. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Unable to open memory stream.\n");
  742. return 1;
  743. }
  744. fprintf(f,"Not yet implemented!");
  745. nerrors+=1;
  746. fflush (f);
  747. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  748. fclose(f);
  749. FREE_AND_NULL(buf);
  750. return (nerrors==0 && err==ESP_OK)?0:1;
  751. }
  752. cJSON * squeezelite_cb(){
  753. cJSON * values = cJSON_CreateObject();
  754. char * nvs_config= config_alloc_get(NVS_TYPE_STR, "autoexec1");
  755. char **argv = NULL;
  756. char *buf = NULL;
  757. size_t buf_size = 0;
  758. int nerrors=1;
  759. FILE *f = open_memstream(&buf, &buf_size);
  760. if (f == NULL) {
  761. log_send_messaging(MESSAGING_ERROR,"Unable to parse squeezelite parameters");
  762. }
  763. else {
  764. if(nvs_config && strlen(nvs_config)>0){
  765. ESP_LOGD(TAG,"Parsing command %s",nvs_config);
  766. argv = (char **) calloc(22, sizeof(char *));
  767. if (argv == NULL) {
  768. FREE_AND_NULL(nvs_config);
  769. fclose(f);
  770. return values;
  771. }
  772. size_t argc = esp_console_split_argv(nvs_config, argv,22);
  773. if (argc != 0) {
  774. nerrors = arg_parse(argc, argv,(void **)&squeezelite_args);
  775. ESP_LOGD(TAG,"Parsing completed");
  776. }
  777. }
  778. if (nerrors == 0) {
  779. get_str_parm_json(squeezelite_args.buffers, values);
  780. get_str_parm_json(squeezelite_args.codecs, values);
  781. get_lit_parm_json(squeezelite_args.header_format, values);
  782. get_str_parm_json(squeezelite_args.log_level, values);
  783. // get_str_parm_json(squeezelite_args.log_level_all, values);
  784. // get_str_parm_json(squeezelite_args.log_level_decode, values);
  785. // get_str_parm_json(squeezelite_args.log_level_output, values);
  786. // get_str_parm_json(squeezelite_args.log_level_slimproto, values);
  787. // get_str_parm_json(squeezelite_args.log_level_stream, values);
  788. get_str_parm_json(squeezelite_args.mac_addr, values);
  789. get_str_parm_json(squeezelite_args.output_device, values);
  790. get_str_parm_json(squeezelite_args.model_name, values);
  791. get_str_parm_json(squeezelite_args.name, values);
  792. get_int_parm_json(squeezelite_args.rate, values);
  793. get_str_parm_json(squeezelite_args.rates, values);
  794. get_str_parm_json(squeezelite_args.server, values);
  795. get_int_parm_json(squeezelite_args.timeout, values);
  796. char * p = cJSON_Print(values);
  797. ESP_LOGD(TAG,"%s",p);
  798. free(p);
  799. }
  800. else {
  801. arg_print_errors(f, squeezelite_args.end, desc_squeezelite);
  802. }
  803. fflush (f);
  804. if(strlen(buf)>0){
  805. log_send_messaging(nerrors?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  806. }
  807. fclose(f);
  808. FREE_AND_NULL(buf);
  809. }
  810. FREE_AND_NULL(nvs_config);
  811. FREE_AND_NULL(argv);
  812. return values;
  813. }
  814. static char * get_log_level_options(const char * longname){
  815. const char * template = "<%s=info|%s=debug|%s=sdebug>";
  816. char * options = NULL;
  817. int len = snprintf(NULL,0,template,longname,longname,longname);
  818. if(len>0){
  819. options = malloc(len+1);
  820. snprintf(options,len,template,longname,longname,longname);
  821. }
  822. return options;
  823. }
  824. static void register_i2s_config(void){
  825. i2s_args.model_name = arg_str1(NULL,"model_name","TAS57xx|TAS5713|AC101|WM8978|I2S","DAC Model Name");
  826. i2s_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  827. i2s_args.clock = arg_int1(NULL,"clock","<n>","Clock GPIO. e.g. 33");
  828. i2s_args.wordselect = arg_int1(NULL,"wordselect","<n>","Word Select GPIO. e.g. 25");
  829. i2s_args.data = arg_int1(NULL,"data","<n>","Data GPIO. e.g. 32");
  830. i2s_args.mute_gpio = arg_int0(NULL,"mute_gpio", "<n>", "Mute GPIO. e.g. 14");
  831. i2s_args.mute_level = arg_lit0(NULL,"mute_level","Mute GPIO level. Checked=HIGH, Unchecked=LOW");
  832. i2s_args.dac_sda = arg_int0(NULL,"dac_sda", "<n>", "SDA GPIO. e.g. 27");
  833. i2s_args.dac_scl = arg_int0(NULL,"dac_scl", "<n>", "SCL GPIO. e.g. 26");
  834. i2s_args.dac_i2c = arg_int0(NULL,"dac_i2c", "<n>", "I2C device address. e.g. 106");
  835. i2s_args.end = arg_end(6);
  836. const esp_console_cmd_t cmd = {
  837. .command = CFG_TYPE_HW("dac"),
  838. .help = desc_dac,
  839. .hint = NULL,
  840. .func = &do_i2s_cmd,
  841. .argtable = &i2s_args
  842. };
  843. cmd_to_json_with_cb(&cmd,&i2s_cb);
  844. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  845. }
  846. static void register_bt_source_config(void){
  847. bt_source_args.sink_name= arg_str1("n","sink_name", "name","Bluetooth audio device name. This applies when output mode is Bluetooth");
  848. bt_source_args.pin_code= arg_str1("p","pin_code", "pin","Bluetooth security/pin code. Usually 0000. This applies when output mode is Bluetooth");
  849. // 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.");
  850. // 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.");
  851. bt_source_args.end= arg_end(1);
  852. const esp_console_cmd_t cmd = {
  853. .command = CFG_TYPE_AUDIO("bt_source"),
  854. .help = desc_bt_source,
  855. .hint = NULL,
  856. .func = &do_bt_source_cmd,
  857. .argtable = &bt_source_args
  858. };
  859. cmd_to_json_with_cb(&cmd,&bt_source_cb);
  860. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  861. }
  862. static void register_rotary_config(void){
  863. 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.");
  864. rotary_args.A = arg_int1(NULL,"A","gpio","A/DT gpio");
  865. rotary_args.B = arg_int1(NULL,"B","gpio","B/CLK gpio");
  866. rotary_args.SW = arg_int0(NULL,"SW","gpio","Switch gpio");
  867. 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'");
  868. 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. ");
  869. rotary_args.volume_lock = arg_lit0(NULL,"volume_lock", "Force Volume down/up/play toggle all the time (even in LMS). ");
  870. 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').");
  871. rotary_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  872. rotary_args.end = arg_end(3);
  873. const esp_console_cmd_t cmd = {
  874. .command = CFG_TYPE_HW("rotary"),
  875. .help = desc_rotary,
  876. .hint = NULL,
  877. .func = &do_rotary_cmd,
  878. .argtable = &rotary_args
  879. };
  880. cmd_to_json_with_cb(&cmd,&rotary_cb);
  881. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  882. }
  883. static void register_audio_config(void){
  884. 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.");
  885. audio_args.end = arg_end(6);
  886. const esp_console_cmd_t cmd = {
  887. .command = CFG_TYPE_AUDIO("general"),
  888. .help = desc_audio,
  889. .hint = NULL,
  890. .func = &do_audio_cmd,
  891. .argtable = &audio_args
  892. };
  893. cmd_to_json_with_cb(&cmd,&audio_cb);
  894. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  895. }
  896. static void register_spdif_config(void){
  897. spdif_args.clear = arg_lit0(NULL, "clear", "Clear configuration");
  898. spdif_args.clock = arg_int1(NULL,"clock","<n>","Clock GPIO. e.g. 33");
  899. spdif_args.wordselect = arg_int1(NULL,"wordselect","<n>","Word Select GPIO. e.g. 25");
  900. spdif_args.data = arg_int1(NULL,"data","<n>","Data GPIO. e.g. 32");
  901. spdif_args.end = arg_end(6);
  902. const esp_console_cmd_t cmd = {
  903. .command = CFG_TYPE_HW("spdif"),
  904. .help = desc_spdif,
  905. .hint = NULL,
  906. .func = &do_spdif_cmd,
  907. .argtable = &spdif_args
  908. };
  909. cmd_to_json_with_cb(&cmd,&spdif_cb);
  910. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  911. }
  912. static void register_squeezelite_config(void){
  913. squeezelite_args.server = arg_str0("s","server","<server>[:<port>]","Connect to specified server, otherwise uses autodiscovery to find server");
  914. squeezelite_args.buffers = arg_str0("b","buffers","<stream>:<output>","Internal Stream and Output buffer sizes in Kbytes");
  915. squeezelite_args.codecs = arg_strn("c","codecs","+" CODECS "+",0,20,"Restrict codecs to those specified, otherwise load all available codecs; known codecs: " CODECS );
  916. 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");
  917. 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"
  918. // squeezelite_args.log_level_all = arg_str0(NULL,"all",get_log_level_options("all"),"Overall Logging Level");
  919. // squeezelite_args.log_level_slimproto = arg_str0(NULL,"loglevel_slimproto",get_log_level_options("slimproto"),"Slimproto Logging Level");
  920. // squeezelite_args.log_level_stream= arg_str0(NULL,"loglevel_stream",get_log_level_options("stream"),"Stream Logging Level");
  921. // squeezelite_args.log_level_decode= arg_str0(NULL,"loglevel_decode",get_log_level_options("decode"),"Decode Logging Level");
  922. // squeezelite_args.log_level_output= arg_str0(NULL,"loglevel_output",get_log_level_options("output"),"Output Logging Level");
  923. #if IR
  924. squeezelite_args.log_level_ir= arg_str0(NULL,"loglevel_ir",get_log_level_options("ir"),"IR Logging Level");
  925. #endif
  926. squeezelite_args.output_device = arg_str0("o","output_device","<string>","Output device (BT, I2S or SPDIF)");
  927. squeezelite_args.mac_addr = arg_str0("m","mac_addr","<string>","Mac address, format: ab:cd:ef:12:34:56.");
  928. squeezelite_args.model_name = arg_str0("M", "modelname", "<string>","Set the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")");
  929. 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.");
  930. squeezelite_args.header_format = arg_lit0("W","header_format","Read wave and aiff format from header, ignore server parameters");
  931. 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");
  932. #if RESAMPLE
  933. squeezelite_args.resample = arg_lit0("R","resample","Activate Resample");
  934. squeezelite_args.resample_parms = arg_str0("u","resample_parms","<recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>","Resample, params");
  935. #endif
  936. #if RESAMPLE16
  937. squeezelite_args.resample = arg_lit0("R","resample","Activate Resample");
  938. 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");
  939. #endif
  940. squeezelite_args.rate = arg_int0("Z","max_rate", "<n>", "Report rate to server in helo as the maximum sample rate we can support");
  941. squeezelite_args.end = arg_end(6);
  942. const esp_console_cmd_t cmd = {
  943. .command = CFG_TYPE_AUDIO("squeezelite"),
  944. .help = desc_squeezelite,
  945. .hint = NULL,
  946. .func = &do_squeezelite_cmd,
  947. .argtable = &squeezelite_args
  948. };
  949. cmd_to_json_with_cb(&cmd,&squeezelite_cb);
  950. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  951. }
  952. void register_config_cmd(void){
  953. register_audio_config();
  954. // register_squeezelite_config();
  955. register_bt_source_config();
  956. if(!is_dac_config_locked()){
  957. register_i2s_config();
  958. }
  959. if(!is_spdif_config_locked()){
  960. register_spdif_config();
  961. }
  962. register_rotary_config();
  963. }