Fix StrCaseStr_P() and refactor GUI save settings

This commit is contained in:
Theo Arends 2021-05-02 15:28:47 +02:00
parent 6b56cc9da3
commit 5b94f4450a
2 changed files with 34 additions and 63 deletions

View File

@ -516,9 +516,9 @@ char* UpperCase_P(char* dest, const char* source)
} }
char* StrCaseStr_P(const char* source, const char* search) { char* StrCaseStr_P(const char* source, const char* search) {
char case_source[strlen(source) +1]; char case_source[strlen_P(source) +1];
UpperCase_P(case_source, source); UpperCase_P(case_source, source);
char case_search[strlen(search) +1]; char case_search[strlen_P(search) +1];
UpperCase_P(case_search, search); UpperCase_P(case_search, search);
return strstr(case_source, case_search); return strstr(case_source, case_search);
} }

View File

@ -1975,27 +1975,22 @@ void HandleWifiConfiguration(void) {
WSContentStop(); WSContentStop();
} }
void WifiSaveSettings(void) { String AddWebCommand(const char* command, const char* webarg, const char* dflt) {
char tmp[100]; // Allow parameter with lenght up to 99 characters char arg[100]; // Allow parameter with lenght up to 99 characters
String cmnd = F(D_CMND_BACKLOG "0 " D_CMND_HOSTNAME " "); WebGetArg(webarg, arg, sizeof(arg));
WebGetArg(PSTR("h"), tmp, sizeof(tmp)); // Host name char cmnd[120];
cmnd += (!strlen(tmp)) ? "1" : tmp; snprintf_P(cmnd, sizeof(cmnd), PSTR(";%s %s"), command, (!strlen(arg)) ? dflt : (StrCaseStr_P(command, PSTR("Password")) && (strlen(arg) < 5)) ? "" : arg);
cmnd += F(";" D_CMND_CORS " "); return String(cmnd);
WebGetArg(PSTR("c"), tmp, sizeof(tmp)); // Cors domain }
cmnd += (!strlen(tmp)) ? "1" : tmp;
cmnd += F(";" D_CMND_SSID "1 ");
WebGetArg(PSTR("s1"), tmp, sizeof(tmp)); // Ssid1
cmnd += (!strlen(tmp)) ? "1" : tmp;
cmnd += F(";" D_CMND_SSID "2 ");
WebGetArg(PSTR("s2"), tmp, sizeof(tmp)); // Ssid2
cmnd += (!strlen(tmp)) ? "1" : tmp;
cmnd += F(";" D_CMND_PASSWORD "3 ");
WebGetArg(PSTR("p1"), tmp, sizeof(tmp)); // Password1
cmnd += (!strlen(tmp)) ? "\"" : (strlen(tmp) < 5) ? "" : tmp;
cmnd += F(";" D_CMND_PASSWORD "4 ");
WebGetArg(PSTR("p2"), tmp, sizeof(tmp)); // Password2
cmnd += (!strlen(tmp)) ? "\"" : (strlen(tmp) < 5) ? "" : tmp;
void WifiSaveSettings(void) {
String cmnd = F(D_CMND_BACKLOG "0 ");
cmnd += AddWebCommand(PSTR(D_CMND_HOSTNAME), PSTR("h"), PSTR("1"));
cmnd += AddWebCommand(PSTR(D_CMND_CORS), PSTR("c"), PSTR("1"));
cmnd += AddWebCommand(PSTR(D_CMND_SSID "1"), PSTR("s1"), PSTR("1"));
cmnd += AddWebCommand(PSTR(D_CMND_SSID "2"), PSTR("s2"), PSTR("1"));
cmnd += AddWebCommand(PSTR(D_CMND_PASSWORD "3"), PSTR("p1"), PSTR("\""));
cmnd += AddWebCommand(PSTR(D_CMND_PASSWORD "4"), PSTR("p2"), PSTR("\""));
ExecuteWebCommand((char*)cmnd.c_str()); ExecuteWebCommand((char*)cmnd.c_str());
} }
@ -2039,29 +2034,14 @@ void HandleLoggingConfiguration(void) {
} }
void LoggingSaveSettings(void) { void LoggingSaveSettings(void) {
char tmp[100]; // Allow parameter with lenght up to 99 characters String cmnd = F(D_CMND_BACKLOG "0 ");
String cmnd = F(D_CMND_BACKLOG "0 " D_CMND_SERIALLOG " "); cmnd += AddWebCommand(PSTR(D_CMND_SERIALLOG), PSTR("l0"), STR(SERIAL_LOG_LEVEL));
WebGetArg(PSTR("l0"), tmp, sizeof(tmp)); // Serial log level cmnd += AddWebCommand(PSTR(D_CMND_WEBLOG), PSTR("l1"), STR(WEB_LOG_LEVEL));
cmnd += (!strlen(tmp)) ? STR(SERIAL_LOG_LEVEL) : tmp; cmnd += AddWebCommand(PSTR(D_CMND_MQTTLOG), PSTR("l2"), STR(MQTT_LOG_LEVEL));
cmnd += F(";" D_CMND_WEBLOG " "); cmnd += AddWebCommand(PSTR(D_CMND_SYSLOG), PSTR("l3"), STR(SYS_LOG_LEVEL));
WebGetArg(PSTR("l1"), tmp, sizeof(tmp)); // Web log level cmnd += AddWebCommand(PSTR(D_CMND_LOGHOST), PSTR("lh"), PSTR("1"));
cmnd += (!strlen(tmp)) ? STR(WEB_LOG_LEVEL) : tmp; cmnd += AddWebCommand(PSTR(D_CMND_LOGPORT), PSTR("lp"), PSTR("1"));
cmnd += F(";" D_CMND_MQTTLOG " "); cmnd += AddWebCommand(PSTR(D_CMND_TELEPERIOD), PSTR("lt"), PSTR("1"));
WebGetArg(PSTR("l2"), tmp, sizeof(tmp)); // Mqtt log level
cmnd += (!strlen(tmp)) ? STR(MQTT_LOG_LEVEL) : tmp;
cmnd += F(";" D_CMND_SYSLOG " ");
WebGetArg(PSTR("l3"), tmp, sizeof(tmp)); // Syslog level
cmnd += (!strlen(tmp)) ? STR(SYS_LOG_LEVEL) : tmp;
cmnd += F(";" D_CMND_LOGHOST " ");
WebGetArg(PSTR("lh"), tmp, sizeof(tmp)); // Syslog host name
cmnd += (!strlen(tmp)) ? SYS_LOG_HOST : tmp;
cmnd += F(";" D_CMND_LOGPORT " ");
WebGetArg(PSTR("lp"), tmp, sizeof(tmp)); // Syslog port number
cmnd += (!strlen(tmp)) ? STR(SYS_LOG_PORT) : tmp;
cmnd += F(";" D_CMND_TELEPERIOD " ");
WebGetArg(PSTR("lt"), tmp, sizeof(tmp)); // Teleperiod
cmnd += (!strlen(tmp)) ? STR(TELE_PERIOD) : tmp;
ExecuteWebCommand((char*)cmnd.c_str()); ExecuteWebCommand((char*)cmnd.c_str());
} }
@ -2130,39 +2110,30 @@ void HandleOtherConfiguration(void) {
} }
void OtherSaveSettings(void) { void OtherSaveSettings(void) {
char tmp[100]; // Allow parameter with lenght up to 99 characters String cmnd = F(D_CMND_BACKLOG "0 ");
String cmnd = F(D_CMND_BACKLOG "0 " D_CMND_WEBPASSWORD "2 "); cmnd += AddWebCommand(PSTR(D_CMND_WEBPASSWORD "2"), PSTR("wp"), PSTR("\""));
WebGetArg(PSTR("wp"), tmp, sizeof(tmp)); // Web password
cmnd += (!strlen(tmp)) ? "\"" : (strlen(tmp) < 5) ? "" : tmp;
cmnd += F(";" D_CMND_SO "3 "); cmnd += F(";" D_CMND_SO "3 ");
cmnd += Webserver->hasArg(F("b1")); cmnd += Webserver->hasArg(F("b1"));
cmnd += F(";" D_CMND_DEVICENAME " "); cmnd += AddWebCommand(PSTR(D_CMND_DEVICENAME), PSTR("dn"), PSTR("\""));
WebGetArg(PSTR("dn"), tmp, sizeof(tmp)); // Device name
cmnd += (!strlen(tmp)) ? "\"" : tmp;
char webindex[5]; char webindex[5];
char tmp2[110]; char cmnd2[24]; // ";Module 0;Template "
for (uint32_t i = 0; i < MAX_FRIENDLYNAMES; i++) { for (uint32_t i = 0; i < MAX_FRIENDLYNAMES; i++) {
snprintf_P(webindex, sizeof(webindex), PSTR("a%d"), i); snprintf_P(webindex, sizeof(webindex), PSTR("a%d"), i);
WebGetArg(webindex, tmp, sizeof(tmp)); // Friendly name 1 to 8 snprintf_P(cmnd2, sizeof(cmnd2), PSTR(D_CMND_FN "%d"), i +1);
snprintf_P(tmp2, sizeof(tmp2), PSTR(";" D_CMND_FN "%d %s"), i +1, (!strlen(tmp)) ? "\"" : tmp); cmnd += AddWebCommand(cmnd2, webindex, PSTR("\""));
cmnd += tmp2;
} }
#ifdef USE_EMULATION #ifdef USE_EMULATION
#if defined(USE_EMULATION_WEMO) || defined(USE_EMULATION_HUE) #if defined(USE_EMULATION_WEMO) || defined(USE_EMULATION_HUE)
WebGetArg(PSTR("b2"), tmp, sizeof(tmp)); // Emulation cmnd += AddWebCommand(PSTR(D_CMND_EMULATION), PSTR("b2"), PSTR("0"));
cmnd += F(";" D_CMND_EMULATION " ");
cmnd += (!strlen(tmp)) ? "0" : tmp;
#endif // USE_EMULATION_WEMO || USE_EMULATION_HUE #endif // USE_EMULATION_WEMO || USE_EMULATION_HUE
#endif // USE_EMULATION #endif // USE_EMULATION
String tmpl = Webserver->arg(F("t1")); // {"NAME":"12345678901234","GPIO":[255,255,255,255,255,255,255,255,255,255,255,255,255],"FLAG":255,"BASE":255,"CMND":"SO123 1;SO99 0"} String tmpl = Webserver->arg(F("t1")); // {"NAME":"12345678901234","GPIO":[255,255,255,255,255,255,255,255,255,255,255,255,255],"FLAG":255,"BASE":255,"CMND":"SO123 1;SO99 0"}
if (tmpl.length() && (tmpl.length() < MQTT_MAX_PACKET_SIZE)) { if (tmpl.length() && (tmpl.length() < MQTT_MAX_PACKET_SIZE)) {
snprintf_P(tmp, sizeof(tmp), PSTR(";%s" D_CMND_TEMPLATE " "), (Webserver->hasArg(F("t2"))) ? PSTR(D_CMND_MODULE " 0;") : ""); snprintf_P(cmnd2, sizeof(cmnd2), PSTR(";%s" D_CMND_TEMPLATE " "), (Webserver->hasArg(F("t2"))) ? PSTR(D_CMND_MODULE " 0;") : "");
cmnd += tmp + tmpl; cmnd += cmnd2 + tmpl;
} }
ExecuteWebCommand((char*)cmnd.c_str()); ExecuteWebCommand((char*)cmnd.c_str());
} }