瀏覽代碼

safe NVS parsing

Philippe G 3 年之前
父節點
當前提交
a9efcfeca8

+ 1 - 1
components/display/SSD1675.c

@@ -243,7 +243,7 @@ struct GDS_Device* SSD1675_Detect(char *Driver, struct GDS_Device* Device) {
 	char *p;
 	struct PrivateSpace* Private = (struct PrivateSpace*) Device->Private;
 	Private->ReadyPin = -1;
-	if ((p = strcasestr(Driver, "ready")) != NULL) Private->ReadyPin = atoi(strchr(p, '=') + 1);
+	if ((p = strcasestr(Driver, "ready")) && (p = strchr(p, '='))) Private->ReadyPin = atoi(p + 1);
 	
 	ESP_LOGI(TAG, "SSD1675 driver with ready GPIO %d", Private->ReadyPin);
 	

+ 10 - 10
components/display/display.c

@@ -71,11 +71,11 @@ void display_init(char *welcome) {
 	char *config = config_alloc_get_str("display_config", CONFIG_DISPLAY_CONFIG, "N/A");
 	
 	int width = -1, height = -1, backlight_pin = -1;
-	char *p, *drivername = strstr(config, "driver");
+	char *drivername = strstr(config, "driver");
 
-	if ((p = strcasestr(config, "width")) != NULL) width = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "height")) != NULL) height = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "back")) != NULL) backlight_pin = atoi(strchr(p, '=') + 1);	
+	PARSE_PARAM(config, "width", '=', width);
+	PARSE_PARAM(config, "height", '=', height);
+	PARSE_PARAM(config, "back", '=', backlight_pin);
 		
 	// query drivers to see if we have a match
 	ESP_LOGI(TAG, "Trying to configure display with %s", config);
@@ -89,13 +89,13 @@ void display_init(char *welcome) {
 	// so far so good
 	if (display && width > 0 && height > 0) {
 		int RST_pin = -1;
-		if ((p = strcasestr(config, "reset")) != NULL) RST_pin = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM(config, "reset", '=', RST_pin);
 		
 		// Detect driver interface
 		if (strcasestr(config, "I2C") && i2c_system_port != -1) {
 			int address = 0x3C;
 				
-			if ((p = strcasestr(config, "address")) != NULL) address = atoi(strchr(p, '=') + 1);
+			PARSE_PARAM(config, "address", '=', address);
 				
 			init = true;
 			GDS_I2CInit( i2c_system_port, -1, -1, i2c_system_speed ) ;
@@ -105,8 +105,8 @@ void display_init(char *welcome) {
 		} else if (strcasestr(config, "SPI") && spi_system_host != -1) {
 			int CS_pin = -1, speed = 0;
 		
-			if ((p = strcasestr(config, "cs")) != NULL) CS_pin = atoi(strchr(p, '=') + 1);
-			if ((p = strcasestr(config, "speed")) != NULL) speed = atoi(strchr(p, '=') + 1);
+			PARSE_PARAM(config, "cs", '=', CS_pin);
+			PARSE_PARAM(config, "speed", '=', speed);
 		
 			init = true;
 			GDS_SPIInit( spi_system_host, spi_system_dc_gpio );
@@ -279,8 +279,8 @@ void displayer_metadata(char *artist, char *album, char *title) {
 	}
 	
 	// get optional scroll speed & pause
-	if ((p = strcasestr(displayer.metadata_config, "speed")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.speed);
-	if ((p = strcasestr(displayer.metadata_config, "pause")) != NULL) sscanf(p, "%*[^=]=%d", &displayer.pause);
+	PARSE_PARAM(displayer.metadata_config, "speed", '=', displayer.speed);
+	PARSE_PARAM(displayer.metadata_config, "pause", '=', displayer.pause);
 	
 	displayer.offset = 0;	
 	utf8_decode(displayer.string);

+ 13 - 0
components/platform_config/platform_config.h

@@ -9,6 +9,19 @@
 extern "C" {
 #endif
 
+#define PARSE_PARAM(S,P,C,V) do {												\
+	char *__p;																	\
+	if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); 	\
+} while (0)
+
+#define PARSE_PARAM_STR(S,P,C,V,I) do {						\
+	char *__p;                                              \
+	if ((__p = strstr(S, P)) && (__p = strchr(__p, C))) {	\
+		while (*++__p == ' ');								\
+		sscanf(__p,"%" #I "[^,]", V);						\
+	}   													\
+} while (0)
+
 #define DECLARE_SET_DEFAULT(t) void config_set_default_## t (const char *key, t  value);
 #define DECLARE_GET_NUM(t) esp_err_t config_get_## t (const char *key, t *  value);
 #ifndef FREE_RESET

+ 68 - 71
components/services/accessors.c

@@ -15,7 +15,6 @@
 #include "accessors.h"
 #include "globdefs.h"
 #include "display.h"
-#include "display.h"
 #include "cJSON.h"
 #include "driver/gpio.h"
 #include "stdbool.h"
@@ -109,9 +108,9 @@ bool is_spdif_config_locked(){
 static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) {
 	char *p;
 	pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1; 				
-	if ((p = strcasestr(config, "bck")) != NULL) pin_config->bck_io_num = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "ws")) != NULL) pin_config->ws_io_num = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "do")) != NULL) pin_config->data_out_num = atoi(strchr(p, '=') + 1);
+	if ((p = strcasestr(config, "bck"))) sscanf(p, "bck%*[^=]=%d", &pin_config->bck_io_num);
+	if ((p = strcasestr(config, "ws"))) sscanf(p, "ws%*[^=]=%d", &pin_config->ws_io_num);
+	if ((p = strcasestr(config, "do"))) sscanf(p, "do%*[^=]=%d", &pin_config->data_out_num);
 }
 
 /****************************************************************************************
@@ -129,15 +128,15 @@ const i2s_platform_config_t * config_get_i2s_from_str(char * dac_config ){
 	strcpy(i2s_dac_pin.model, "i2s");
 	char * p=NULL;
 
-	if ((p = strcasestr(dac_config, "i2c")) != NULL) i2s_dac_pin.i2c_addr = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(dac_config, "sda")) != NULL) i2s_dac_pin.sda = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(dac_config, "scl")) != NULL) i2s_dac_pin.scl = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(dac_config, "model")) != NULL) sscanf(p, "%*[^=]=%31[^,]", i2s_dac_pin.model);
-	if ((p = strcasestr(dac_config, "mute")) != NULL) {
+	PARSE_PARAM(dac_config, "i2c", '=', i2s_dac_pin.i2c_addr);
+	PARSE_PARAM(dac_config, "sda", '=', i2s_dac_pin.sda);
+	PARSE_PARAM(dac_config, "scl", '=', i2s_dac_pin.scl);
+	PARSE_PARAM_STR(dac_config, "model", '=', i2s_dac_pin.model, 31);
+	if ((p = strcasestr(dac_config, "mute"))) {
 		char mute[8] = "";
 		sscanf(p, "%*[^=]=%7[^,]", mute);
 		i2s_dac_pin.mute_gpio = atoi(mute);
-		if ((p = strchr(mute, ':')) != NULL) i2s_dac_pin.mute_level = atoi(p + 1);
+		PARSE_PARAM(p, "mute", ':', i2s_dac_pin.mute_level);
 	}	
 	return &i2s_dac_pin;
 }
@@ -145,28 +144,29 @@ const i2s_platform_config_t * config_get_i2s_from_str(char * dac_config ){
 /****************************************************************************************
  * Get eth config structure from config string
  */
-const eth_config_t * config_get_eth_from_str(char * eth_config ){
-	static eth_config_t eth_pin = {
+const eth_config_t * config_get_eth_from_str(char * config ){
+	static eth_config_t eth_config = {
 		.rmii = false,
 		.model = "",
 	};
-	char * p=NULL;
-
-	if ((p = strcasestr(eth_config, "model")) != NULL) sscanf(p, "%*[^=]=%15[^,]", eth_pin.model);
-	if ((p = strcasestr(eth_config, "mdc")) != NULL) eth_pin.mdc = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "mdio")) != NULL) eth_pin.mdio = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "rst")) != NULL) eth_pin.rst = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "mosi")) != NULL) eth_pin.mosi = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "miso")) != NULL) eth_pin.miso = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "intr")) != NULL) eth_pin.intr = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "cs")) != NULL) eth_pin.cs = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "speed")) != NULL) eth_pin.speed = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "clk")) != NULL) eth_pin.clk = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(eth_config, "host")) != NULL) eth_pin.host = atoi(strchr(p, '=') + 1);
 	
-	if (strcasestr(eth_pin.model, "lan8720")) eth_pin.rmii = true;
+	PARSE_PARAM_STR(config, "model", '=', eth_config.model, 15);
+	PARSE_PARAM(config, "mdc", '=', eth_config.mdc);
+	PARSE_PARAM(config, "mdio", '=', eth_config.mdio);
+	PARSE_PARAM(config, "rst", '=', eth_config.rst);
+	PARSE_PARAM(config, "mosi", '=', eth_config.mosi);
+	PARSE_PARAM(config, "miso", '=', eth_config.miso);
+	PARSE_PARAM(config, "intr", '=', eth_config.intr);
+	PARSE_PARAM(config, "cs", '=', eth_config.cs);
+	PARSE_PARAM(config, "speed", '=', eth_config.speed);
+	PARSE_PARAM(config, "clk", '=', eth_config.clk);
+
+	// only system host is available
+	eth_config.host = spi_system_host;
+
+	if (strcasestr(eth_config.model, "lan8720")) eth_config.rmii = true;
 	
-	return &eth_pin;
+	return &eth_config;
 }
 
 /****************************************************************************************
@@ -432,16 +432,18 @@ const display_config_t * config_display_get(){
 		sscanf(p, "%*[^:]:%u", &dstruct.depth);
 		dstruct.drivername = display_conf_get_driver_name(strchr(p, '=') + 1);
 	}
-	
-	if ((p = strcasestr(config, "width")) != NULL) dstruct.width = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "height")) != NULL) dstruct.height = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "reset")) != NULL) dstruct.RST_pin = atoi(strchr(p, '=') + 1);
+
+	PARSE_PARAM(config, "width", '=', dstruct.width);
+	PARSE_PARAM(config, "height", '=', dstruct.height);
+	PARSE_PARAM(config, "reset", '=', dstruct.RST_pin);
+	PARSE_PARAM(config, "address", '=', dstruct.address);
+	PARSE_PARAM(config, "cs", '=', dstruct.CS_pin);
+	PARSE_PARAM(config, "speed", '=', dstruct.speed);
+	PARSE_PARAM(config, "back", '=', dstruct.back);
+
 	if (strstr(config, "I2C") ) dstruct.type=i2c_name_type;
-	if ((p = strcasestr(config, "address")) != NULL) dstruct.address = atoi(strchr(p, '=') + 1);
 	if (strstr(config, "SPI") ) dstruct.type=spi_name_type;
-	if ((p = strcasestr(config, "cs")) != NULL) dstruct.CS_pin = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "speed")) != NULL) dstruct.speed = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "back")) != NULL) dstruct.back = atoi(strchr(p, '=') + 1);
+
 	dstruct.hflip= strcasestr(config, "HFlip") ? true : false;
 	dstruct.vflip= strcasestr(config, "VFlip") ? true : false;
 	dstruct.rotate= strcasestr(config, "rotate") ? true : false;
@@ -452,7 +454,7 @@ const display_config_t * config_display_get(){
  * 
  */
 const i2c_config_t * config_i2c_get(int * i2c_port) {
-	char *nvs_item, *p;
+	char *nvs_item;
 	static i2c_config_t i2c = {
 		.mode = I2C_MODE_MASTER,
 		.sda_io_num = -1,
@@ -466,10 +468,10 @@ const i2c_config_t * config_i2c_get(int * i2c_port) {
 	
 	nvs_item = config_alloc_get(NVS_TYPE_STR, "i2c_config");
 	if (nvs_item) {
-		if ((p = strcasestr(nvs_item, "scl")) != NULL) i2c.scl_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "sda")) != NULL) i2c.sda_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "speed")) != NULL) i2c.master.clk_speed = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "port")) != NULL) i2c_system_port = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM(nvs_item, "scl", '=', i2c.scl_io_num);
+		PARSE_PARAM(nvs_item, "sda", '=', i2c.sda_io_num);
+		PARSE_PARAM(nvs_item, "speed", '=', i2c.master.clk_speed);
+		PARSE_PARAM(nvs_item, "port", '=', i2c_system_port);
 		free(nvs_item);
 	}
 	if(i2c_port) {
@@ -504,13 +506,14 @@ const gpio_exp_config_t* config_gpio_exp_get(int index) {
 		}
 	}
 
-	if ((p = strcasestr(item, "addr")) != NULL) config.phy.addr = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(item, "cs_pin")) != NULL) config.phy.cs_pin = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(item, "speed")) != NULL) config.phy.speed = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(item, "intr")) != NULL) config.intr = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(item, "base")) != NULL) config.base = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(item, "count")) != NULL) config.count = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(item, "model")) != NULL) sscanf(p, "%*[^=]=%31[^,]", config.model);
+	PARSE_PARAM(item, "addr", '=', config.phy.addr);
+	PARSE_PARAM(item, "cs_pin", '=', config.phy.cs_pin);
+	PARSE_PARAM(item, "speed", '=', config.phy.speed);
+	PARSE_PARAM(item, "intr", '=', config.intr);
+	PARSE_PARAM(item, "base", '=', config.base);
+	PARSE_PARAM(item, "count", '=', config.count);
+	PARSE_PARAM_STR(item, "model", '=', config.model, 31);
+
 	if ((p = strcasestr(item, "port")) != NULL) {
 		char port[8] = "";
 		sscanf(p, "%*[^=]=%7[^,]", port);
@@ -599,7 +602,7 @@ const set_GPIO_struct_t * get_gpio_struct(){
  * 
  */
 const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
-	char *nvs_item, *p;
+	char *nvs_item;
 	static spi_bus_config_t spi = {
 		.mosi_io_num = -1,
         .sclk_io_num = -1,
@@ -610,11 +613,11 @@ const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
 
 	nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
 	if (nvs_item) {
-		if ((p = strcasestr(nvs_item, "data")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "mosi")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "miso")) != NULL) spi.miso_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "clk")) != NULL) spi.sclk_io_num = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "dc")) != NULL) spi_system_dc_gpio = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM(nvs_item, "data", '=', spi.mosi_io_num);
+		PARSE_PARAM(nvs_item, "mosi", '=', spi.mosi_io_num);
+		PARSE_PARAM(nvs_item, "miso", '=', spi.miso_io_num);
+		PARSE_PARAM(nvs_item, "clk", '=', spi.sclk_io_num);
+		PARSE_PARAM(nvs_item, "dd", '=', spi_system_dc_gpio);
 		// only VSPI (1) can be used as Flash and PSRAM run at 80MHz
 		// if ((p = strcasestr(nvs_item, "host")) != NULL) spi_system_host = atoi(strchr(p, '=') + 1);
 		free(nvs_item);
@@ -651,11 +654,11 @@ const rotary_struct_t * config_rotary_get() {
 	char *config = config_alloc_get_default(NVS_TYPE_STR, "rotary_config", NULL, 0);
 	if (config && *config) {
 		char *p;
-		
+
 		// parse config
-		if ((p = strcasestr(config, "A")) != NULL) rotary.A = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(config, "B")) != NULL) rotary.B = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(config, "SW")) != NULL) rotary.SW = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM(config, "A", '=', rotary.A);
+		PARSE_PARAM(config, "B", '=', rotary.B);
+		PARSE_PARAM(config, "SW", '=', rotary.SW);
 		if ((p = strcasestr(config, "knobonly")) != NULL) {
 			p = strchr(p, '=');
 			rotary.knobonly = true;
@@ -700,13 +703,10 @@ cJSON * add_gpio_for_value(cJSON * list,const char * name,int gpio, const char *
  */
 cJSON * add_gpio_for_name(cJSON * list,const char * nvs_entry,const char * name, const char * prefix, bool fixed){
 	cJSON * llist = list?list:cJSON_CreateArray();
-	char *p;
 	int gpioNum=0;
-	if ((p = strcasestr(nvs_entry, name)) != NULL) {
-		gpioNum = atoi(strchr(p, '=') + 1);
-		if(gpioNum>=0){
-			cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpioNum,fixed));
-		}
+	PARSE_PARAM(nvs_entry, name, '=', gpioNum);
+	if(gpioNum>=0){
+		cJSON_AddItemToArray(llist,get_gpio_entry(name,prefix,gpioNum,fixed));
 	}
 	return llist;
 }
@@ -1028,14 +1028,11 @@ cJSON * get_gpio_list(bool refresh) {
 #ifndef CONFIG_BAT_LOCKED
 	char *bat_config = config_alloc_get_default(NVS_TYPE_STR, "bat_config", NULL, 0);
 	if (bat_config) {
-		char *p;
-		int channel;
-		if ((p = strcasestr(bat_config, "channel") ) != NULL) {
-			channel = atoi(strchr(p, '=') + 1);
-			if(channel != -1){
-				if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
-					cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,false));
-				}
+		int channel = -1;
+		PARSE_PARAM(bat_config, "channem", '=', channel);
+		if(channel != -1){
+			if(adc1_pad_get_io_num(channel,&gpio_num )==ESP_OK){
+				cJSON_AddItemToArray(gpio_list,get_gpio_entry("bat","other",gpio_num,false));
 			}
 		}
 		free(bat_config);

+ 4 - 4
components/services/audio_controls.c

@@ -137,10 +137,10 @@ esp_err_t actrls_init(const char *profile_name) {
 		int A = -1, B = -1, SW = -1, longpress = 0;
 		
 		// parse config
-		if ((p = strcasestr(config, "A")) != NULL) A = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(config, "B")) != NULL) B = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(config, "SW")) != NULL) SW = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(config, "knobonly")) != NULL) {
+		PARSE_PARAM(config, "A", '=', A);
+		PARSE_PARAM(config, "B", '=', B);
+		PARSE_PARAM(config, "SW", '=', SW);
+		if ((p = strcasestr(config, "knobonly"))) {
 			p = strchr(p, '=');
 			int double_press = p ? atoi(p + 1) : 350;
 			rotary.timer = xTimerCreate("knobTimer", double_press / portTICK_RATE_MS, pdFALSE, NULL, rotary_timer);

+ 4 - 5
components/services/battery.c

@@ -79,13 +79,12 @@ void battery_svc_init(void) {
 
 	char *nvs_item = config_alloc_get_default(NVS_TYPE_STR, "bat_config", "n", 0);
 	if (nvs_item) {
-		char *p;		
 #ifndef CONFIG_BAT_LOCKED		
-		if ((p = strcasestr(nvs_item, "channel")) != NULL) battery.channel = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "scale")) != NULL) battery.scale = atof(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "atten")) != NULL) battery.attenuation = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM(nvs_item, "channel", '=', battery.channel);
+		PARSE_PARAM(nvs_item, "scale", '=', battery.scale);
+		PARSE_PARAM(nvs_item, "atten", '=', battery.attenuation);
 #endif		
-		if ((p = strcasestr(nvs_item, "cells")) != NULL) battery.cells = atof(strchr(p, '=') + 1);		
+		PARSE_PARAM(nvs_item, "cells", '=', battery.cells);
 		free(nvs_item);
 	}	
 

+ 3 - 3
components/services/led.c

@@ -234,10 +234,10 @@ void led_svc_init(void) {
 	parse_set_GPIO(set_led_gpio);
 #endif
 
-	char *nvs_item = config_alloc_get(NVS_TYPE_STR, "led_brightness"), *p; 
+	char *nvs_item = config_alloc_get(NVS_TYPE_STR, "led_brightness"); 
 	if (nvs_item) {
-		if ((p = strcasestr(nvs_item, "green")) != NULL) green.pwm = atoi(strchr(p, '=') + 1);
-		if ((p = strcasestr(nvs_item, "red")) != NULL) red.pwm = atoi(strchr(p, '=') + 1);
+		PARSE_PARAM(nvs_item, "green", '=', green.pwm);
+		PARSE_PARAM(nvs_item, "red", '=', red.pwm);
 		free(nvs_item);
 	}
 

+ 8 - 3
components/squeezelite/adac_core.c

@@ -17,6 +17,11 @@
 #include "esp_log.h"
 #include "adac.h"
 
+#define PARSE_PARAM(S,P,C,V) do {									\
+	char *__p;														\
+	if ((__p = strcasestr(S, P)) && (__p = strchr(__p, C))) V = atoi(__p+1); \
+} while (0)
+
 static const char TAG[] = "DAC core";
 static int i2c_port = -1;
 
@@ -46,9 +51,9 @@ int adac_init(char *config, int i2c_port_num) {
 			.master.clk_speed = 250000,
 		};
 
-	if ((p = strcasestr(config, "i2c")) != NULL) i2c_addr = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "sda")) != NULL) i2c_config.sda_io_num = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "scl")) != NULL) i2c_config.scl_io_num = atoi(strchr(p, '=') + 1);
+	PARSE_PARAM(config, "i2c", '=', i2c_addr);
+	PARSE_PARAM(config, "sda", '=', i2c_config.sda_io_num);
+	PARSE_PARAM(config, "scl", '=', i2c_config.scl_io_num);
 
 	if (i2c_config.sda_io_num == -1 || i2c_config.scl_io_num == -1) {
 		ESP_LOGW(TAG, "DAC does not use i2c");

+ 3 - 4
components/squeezelite/output_i2s.c

@@ -190,11 +190,10 @@ static void set_amp_gpio(int gpio, char *value) {
  * Set pin from config string
  */
 static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) {
-	char *p;
 	pin_config->bck_io_num = pin_config->ws_io_num = pin_config->data_out_num = pin_config->data_in_num = -1; 				
-	if ((p = strcasestr(config, "bck")) != NULL) pin_config->bck_io_num = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "ws")) != NULL) pin_config->ws_io_num = atoi(strchr(p, '=') + 1);
-	if ((p = strcasestr(config, "do")) != NULL) pin_config->data_out_num = atoi(strchr(p, '=') + 1);
+	PARSE_PARAM(config, "bck", '=', pin_config->bck_io_num);
+	PARSE_PARAM(config, "ws", '=', pin_config->ws_io_num);
+	PARSE_PARAM(config, "do", '=', pin_config->data_out_num);
 }
 
 /****************************************************************************************