diff --git a/WiFiManager.cpp b/WiFiManager.cpp index fdeae7c..c21374f 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -59,16 +59,25 @@ WiFiManagerParameter::~WiFiManagerParameter() { if (_value != NULL) { delete[] _value; } + _length=0; // setting length 0, ideally the entire parameter should be removed, or added to wifimanager scope so it follows } void WiFiManagerParameter::setValue(const char *defaultValue, int length) { - _length = length; - _value = new char[length + 1]; - for (int i = 0; i < length + 1; i++) { + _length = length; + int deflength = strlen(defaultValue); // length actual + // use the defult length if it's longer, + // @todo consider it might be useful to fail, so user knows they were wrong + if(_length < length){ + // Serial.println("defaultValue length mismatch"); + _length = deflength; + } + _value = new char[_length + 1]; + for (int i = 0; i < _length + 1; i++) { _value[i] = 0; } if (defaultValue != NULL) { - strncpy(_value, defaultValue, length + 1); // length+1 due to null terminated string + strncpy(_value, defaultValue, _length); + _value[_length] = '\0'; // explicit null } } const char* WiFiManagerParameter::getValue() { @@ -654,6 +663,8 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(DEBUG_VERBOSE,F("Connection result:"),getWLStatusString(connRes)); +// WPS enabled? https://github.com/esp8266/Arduino/pull/4889 +#ifdef NO_EXTRA_4K_HEAP // do WPS, if WPS options enabled and not connected and no password was supplied // @todo this seems like wrong place for this, is it a fallback or option? if (_tryWPS && connRes != WL_CONNECTED && pass == "") { @@ -661,6 +672,7 @@ uint8_t WiFiManager::connectWifi(String ssid, String pass) { // should be connected at the end of WPS connRes = waitForConnectResult(); } +#endif if(connRes != WL_SCAN_COMPLETED){ updateConxResult(connRes); @@ -782,6 +794,8 @@ uint8_t WiFiManager::waitForConnectResult(uint16_t timeout) { return status; } +// WPS enabled? https://github.com/esp8266/Arduino/pull/4889 +#ifdef NO_EXTRA_4K_HEAP void WiFiManager::startWPS() { DEBUG_WM(F("START WPS")); #ifdef ESP8266 @@ -791,11 +805,11 @@ void WiFiManager::startWPS() { #endif DEBUG_WM(F("END WPS")); } - +#endif String WiFiManager::getHTTPHead(String title){ String page; - page += FPSTR(HTTP_HEAD); + page += FPSTR(HTTP_HEAD_START); page.replace(FPSTR(T_v), title); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); @@ -1073,7 +1087,7 @@ String WiFiManager::getParamOut(){ String HTTP_PARAM_temp = FPSTR(HTTP_FORM_LABEL); HTTP_PARAM_temp += FPSTR(HTTP_FORM_PARAM); - bool tok_I = HTTP_PARAM_temp.indexOf(FPSTR(T_i)) > 0; + bool tok_I = HTTP_PARAM_temp.indexOf(FPSTR(T_I)) > 0; bool tok_i = HTTP_PARAM_temp.indexOf(FPSTR(T_i)) > 0; bool tok_n = HTTP_PARAM_temp.indexOf(FPSTR(T_n)) > 0; bool tok_p = HTTP_PARAM_temp.indexOf(FPSTR(T_p)) > 0; @@ -1085,7 +1099,8 @@ String WiFiManager::getParamOut(){ char parLength[5]; // add the extra parameters to the form for (int i = 0; i < _paramsCount; i++) { - if (_params[i] == NULL) { + if (_params[i] == NULL || _params[i]->_length == 0) { + DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope"); break; } @@ -1108,7 +1123,7 @@ String WiFiManager::getParamOut(){ // if no ID use customhtml for item, else generate from param string if (_params[i]->getID() != NULL) { - if(tok_I)pitem.replace(FPSTR(T_i), (String)FPSTR(S_parampre)+(String)i); + if(tok_I)pitem.replace(FPSTR(T_I), (String)FPSTR(S_parampre)+(String)i); if(tok_i)pitem.replace(FPSTR(T_i), _params[i]->getID()); if(tok_n)pitem.replace(FPSTR(T_n), _params[i]->getID()); if(tok_p)pitem.replace(FPSTR(T_p), FPSTR(T_t)); @@ -1181,7 +1196,7 @@ void WiFiManager::handleWifiSave() { page += FPSTR(HTTP_END); server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length())); - server->sendHeader(HTTP_HEAD_CORS, HTTP_HEAD_CORS_ALLOW_ALL); + server->sendHeader(FPSTR(HTTP_HEAD_CORS), FPSTR(HTTP_HEAD_CORS_ALLOW_ALL)); server->send(200, FPSTR(HTTP_HEAD_CT), page); DEBUG_WM(DEBUG_DEV,F("Sent wifi save page")); @@ -1216,9 +1231,13 @@ void WiFiManager::doParamSave(){ break; } //read parameter from server + String name = (String)FPSTR(S_parampre)+(String)i; String value; - if(server->arg((String)FPSTR(S_parampre)+(String)i) != NULL) value = server->arg((String)FPSTR(S_parampre)+(String)i).c_str(); - else value = server->arg(_params[i]->getID()).c_str(); + if(server->hasArg(name)) { + value = server->arg(name); + } else { + value = server->arg(_params[i]->getID()); + } //store it in params array value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated diff --git a/WiFiManager.h b/WiFiManager.h index 6779dcb..795f67d 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -290,7 +290,9 @@ class WiFiManager int _staShowDns = 0; // ternary always show dns, only if not set in code, never(cannot change dns via web!) boolean _removeDuplicateAPs = true; // remove dup aps from wifiscan boolean _shouldBreakAfterConfig = false; // stop configportal on save failure +#ifdef NO_EXTRA_4K_HEAP boolean _tryWPS = false; // try WPS on save failure, unsupported +#endif boolean _configPortalIsBlocking = true; // configportal enters blocking loop boolean _enableCaptivePortal = true; // enable captive portal redirection boolean _userpersistent = true; // users preffered persistence to restore @@ -316,7 +318,9 @@ class WiFiManager void _end(); void setupConfigPortal(); +#ifdef NO_EXTRA_4K_HEAP void startWPS(); +#endif bool startAP(); uint8_t connectWifi(String ssid, String pass); diff --git a/strings_en.h b/strings_en.h index 5444d00..60850a2 100644 --- a/strings_en.h +++ b/strings_en.h @@ -17,7 +17,7 @@ // #define WIFI_MANAGER_OVERRIDE_STRINGS // Copy and paste ALL of the const char lines below and modify to suit. -const char HTTP_HEAD[] PROGMEM = "{v}"; +const char HTTP_HEAD_START[] PROGMEM = "{v}"; const char HTTP_SCRIPT[] PROGMEM = ""; const char HTTP_HEAD_END[] PROGMEM = "
";