cmd_config.c 58 KB

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