From 362a561a35960ea8ae8a67f2ab2daa5af2f242df Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Sun, 4 Jun 2017 06:41:27 -0700 Subject: [PATCH 1/7] use malloc/free for WiFiManagerParameter* array instead of hard coding. add alternate constructor to specifiy max parameter count origional default constructor uses old hard codded value --- README.md | 5 +++++ WiFiManager.cpp | 27 +++++++++++++++++++++++++-- WiFiManager.h | 6 +++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a329f2b..3e83d4b 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,11 @@ You should also take a look at adding custom HTML to your form. - Save and load custom parameters to file system in json form [AutoConnectWithFSParameters](https://github.com/tzapu/WiFiManager/tree/master/examples/AutoConnectWithFSParameters) - *Save and load custom parameters to EEPROM* (not done yet) +NOTE: if your need more than 10 (the default) custom parameters you will need to use the alternate constructor: +```cpp +WiFiManager wifiManager(15); // pass the number of params you will be adding +``` + #### Custom IP Configuration You can set a custom IP for both AP (access point, config mode) and STA (station mode, client mode, normal project state) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4fba355..4be0105 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -60,14 +60,37 @@ const char* WiFiManagerParameter::getCustomHTML() { return _customHTML; } + WiFiManager::WiFiManager() { + init(WIFI_MANAGER_MAX_PARAMS); +} + +WiFiManager::WiFiManager(int max_params) { + init(max_params); +} + +void WiFiManager::init(int max_params) +{ + DEBUG_WM(F("setting max_params to:")); + DEBUG_WM(max_params); + _max_params = max_params; + _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); +} + +WiFiManager::~WiFiManager() +{ + if (_params != NULL) + { + DEBUG_WM(F("freeing allocated params!")); + free(_params); + } } void WiFiManager::addParameter(WiFiManagerParameter *p) { - if(_paramsCount + 1 > WIFI_MANAGER_MAX_PARAMS) + if(_paramsCount + 1 > _max_params) { //Max parameters exceeded! - DEBUG_WM("WIFI_MANAGER_MAX_PARAMS exceeded, increase number (in WiFiManager.h) before adding more parameters!"); + DEBUG_WM("max_params value exceeded, use WiFiManager(int max_params) constructor with a larger value!"); DEBUG_WM("Skipping parameter with ID:"); DEBUG_WM(p->getID()); return; diff --git a/WiFiManager.h b/WiFiManager.h index d86b904..2c8dece 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -65,6 +65,8 @@ class WiFiManager { public: WiFiManager(); + WiFiManager(int max_params); + ~WiFiManager(); boolean autoConnect(); boolean autoConnect(char const *apName, char const *apPassword = NULL); @@ -119,6 +121,7 @@ class WiFiManager //const String HTTP_HEAD = "{v}"; + void init(int max_params); void setupConfigPortal(); void startWPS(); @@ -176,7 +179,8 @@ class WiFiManager void (*_apcallback)(WiFiManager*) = NULL; void (*_savecallback)(void) = NULL; - WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS]; + int _max_params; + WiFiManagerParameter** _params; template void DEBUG_WM(Generic text); From 3950a3b859f9a3c85dbcce21165f71ff1ff2b6c5 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Tue, 27 Jun 2017 16:24:23 -0700 Subject: [PATCH 2/7] use single constructor with default value for size --- WiFiManager.cpp | 4 ---- WiFiManager.h | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4be0105..081c714 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -61,10 +61,6 @@ const char* WiFiManagerParameter::getCustomHTML() { } -WiFiManager::WiFiManager() { - init(WIFI_MANAGER_MAX_PARAMS); -} - WiFiManager::WiFiManager(int max_params) { init(max_params); } diff --git a/WiFiManager.h b/WiFiManager.h index 2c8dece..ee1521e 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -65,7 +65,7 @@ class WiFiManager { public: WiFiManager(); - WiFiManager(int max_params); + WiFiManager(int max_params=10); ~WiFiManager(); boolean autoConnect(); From c4905c23b789df4f687d18923ebcd0855ded7968 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Tue, 27 Jun 2017 16:27:37 -0700 Subject: [PATCH 3/7] remove init function as its not needed with one constructor --- WiFiManager.cpp | 5 ----- WiFiManager.h | 1 - 2 files changed, 6 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 081c714..a24cbca 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -62,11 +62,6 @@ const char* WiFiManagerParameter::getCustomHTML() { WiFiManager::WiFiManager(int max_params) { - init(max_params); -} - -void WiFiManager::init(int max_params) -{ DEBUG_WM(F("setting max_params to:")); DEBUG_WM(max_params); _max_params = max_params; diff --git a/WiFiManager.h b/WiFiManager.h index ee1521e..6b59ecd 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -121,7 +121,6 @@ class WiFiManager //const String HTTP_HEAD = "{v}"; - void init(int max_params); void setupConfigPortal(); void startWPS(); From 898768092cab07c748c70b00dbbb6a627e50a00a Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Tue, 27 Jun 2017 16:35:48 -0700 Subject: [PATCH 4/7] removed un-needed constructor declaration --- WiFiManager.h | 1 - 1 file changed, 1 deletion(-) diff --git a/WiFiManager.h b/WiFiManager.h index 6b59ecd..5b7893c 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -64,7 +64,6 @@ class WiFiManagerParameter { class WiFiManager { public: - WiFiManager(); WiFiManager(int max_params=10); ~WiFiManager(); From 6465d72f6e76cea610496240c682bdbce30b0ea3 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Thu, 26 Oct 2017 15:23:47 -0700 Subject: [PATCH 5/7] Refactored to use only a default constructor and automatically resize the parameter array each time the size limit is reached. I used the initial size (WIFI_MANAGER_MAX_PARAMS) as the increment when resizing. Existing data is coppied to the new array and the old one is free'd. --- WiFiManager.cpp | 21 ++++++++++++--------- WiFiManager.h | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 16d9f5f..cf5d801 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -61,10 +61,8 @@ const char* WiFiManagerParameter::getCustomHTML() { } -WiFiManager::WiFiManager(int max_params) { - DEBUG_WM(F("setting max_params to:")); - DEBUG_WM(max_params); - _max_params = max_params; +WiFiManager::WiFiManager() { + _max_params = WIFI_MANAGER_MAX_PARAMS; _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); } @@ -80,12 +78,17 @@ WiFiManager::~WiFiManager() void WiFiManager::addParameter(WiFiManagerParameter *p) { if(_paramsCount + 1 > _max_params) { - //Max parameters exceeded! - DEBUG_WM("max_params value exceeded, use WiFiManager(int max_params) constructor with a larger value!"); - DEBUG_WM("Skipping parameter with ID:"); - DEBUG_WM(p->getID()); - return; + // rezise the params array + _max_params += WIFI_MANAGER_MAX_PARAMS; + DEBUG_WM(F("Increasing _max_params to:")); + DEBUG_WM(_max_params); + WiFiManagerParameter** new_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); + // copy old data + memcpy(new_params, _params, _paramsCount * sizeof(WiFiManagerParameter*)); + free(_params); + _params = new_params; } + _params[_paramsCount] = p; _paramsCount++; DEBUG_WM("Adding parameter"); diff --git a/WiFiManager.h b/WiFiManager.h index 364b7b7..f95ce1b 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -64,7 +64,7 @@ class WiFiManagerParameter { class WiFiManager { public: - WiFiManager(int max_params=10); + WiFiManager(); ~WiFiManager(); boolean autoConnect(); From 0b6341bea431767d95008f9a36c9b5d5c538ff08 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Thu, 26 Oct 2017 16:10:34 -0700 Subject: [PATCH 6/7] cleanup readme (removed hint about alternate constructor) --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 3f8996f..10da7f6 100644 --- a/README.md +++ b/README.md @@ -207,11 +207,6 @@ You should also take a look at adding custom HTML to your form. - Save and load custom parameters to file system in json form [AutoConnectWithFSParameters](https://github.com/tzapu/WiFiManager/tree/master/examples/AutoConnectWithFSParameters) - *Save and load custom parameters to EEPROM* (not done yet) -NOTE: if your need more than 10 (the default) custom parameters you will need to use the alternate constructor: -```cpp -WiFiManager wifiManager(15); // pass the number of params you will be adding -``` - #### Custom IP Configuration You can set a custom IP for both AP (access point, config mode) and STA (station mode, client mode, normal project state) From d66cd47df87f2b478e7f7568b7c8e141c02dd53a Mon Sep 17 00:00:00 2001 From: Christopher Liebman Date: Sat, 9 Dec 2017 06:04:07 -0800 Subject: [PATCH 7/7] use realloc() when increasing the size of _params. addParameter() now returns bool indicating the status of adding the parameter where false indicates it failed to increase the size of _params and did not add the parameter. --- WiFiManager.cpp | 15 +++++++++------ WiFiManager.h | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index cf5d801..a82480a 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -75,24 +75,27 @@ WiFiManager::~WiFiManager() } } -void WiFiManager::addParameter(WiFiManagerParameter *p) { +bool WiFiManager::addParameter(WiFiManagerParameter *p) { if(_paramsCount + 1 > _max_params) { // rezise the params array _max_params += WIFI_MANAGER_MAX_PARAMS; DEBUG_WM(F("Increasing _max_params to:")); DEBUG_WM(_max_params); - WiFiManagerParameter** new_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); - // copy old data - memcpy(new_params, _params, _paramsCount * sizeof(WiFiManagerParameter*)); - free(_params); - _params = new_params; + WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*)); + if (new_params != NULL) { + _params = new_params; + } else { + DEBUG_WM("ERROR: failed to realloc params, size not increased!"); + return false; + } } _params[_paramsCount] = p; _paramsCount++; DEBUG_WM("Adding parameter"); DEBUG_WM(p->getID()); + return true; } void WiFiManager::setupConfigPortal() { diff --git a/WiFiManager.h b/WiFiManager.h index f95ce1b..9a22573 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -100,8 +100,8 @@ class WiFiManager void setAPCallback( void (*func)(WiFiManager*) ); //called when settings have been changed and connection was successful void setSaveConfigCallback( void (*func)(void) ); - //adds a custom parameter - void addParameter(WiFiManagerParameter *p); + //adds a custom parameter, returns false on failure + bool addParameter(WiFiManagerParameter *p); //if this is set, it will exit after config, even if connection is unsuccessful. void setBreakAfterConfig(boolean shouldBreak); //if this is set, try WPS setup when starting (this will delay config portal for up to 2 mins)