Skip to content

Commit

Permalink
Merge pull request #374 from liebman/allocate-params-array
Browse files Browse the repository at this point in the history
use malloc/free for WiFiManagerParameter* array instead of hard coding.
  • Loading branch information
tablatronix authored Jan 30, 2018
2 parents a832af0 + d66cd47 commit a4795b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
34 changes: 27 additions & 7 deletions WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,42 @@ const char* WiFiManagerParameter::getCustomHTML() {
return _customHTML;
}


WiFiManager::WiFiManager() {
_max_params = WIFI_MANAGER_MAX_PARAMS;
_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*));
}

void WiFiManager::addParameter(WiFiManagerParameter *p) {
if(_paramsCount + 1 > WIFI_MANAGER_MAX_PARAMS)
WiFiManager::~WiFiManager()
{
if (_params != NULL)
{
DEBUG_WM(F("freeing allocated params!"));
free(_params);
}
}

bool WiFiManager::addParameter(WiFiManagerParameter *p) {
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("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**)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() {
Expand Down
8 changes: 5 additions & 3 deletions WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class WiFiManager
{
public:
WiFiManager();
~WiFiManager();

boolean autoConnect();
boolean autoConnect(char const *apName, char const *apPassword = NULL);
Expand Down Expand Up @@ -100,8 +101,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)
Expand Down Expand Up @@ -177,7 +178,8 @@ class WiFiManager
void (*_apcallback)(WiFiManager*) = NULL;
void (*_savecallback)(void) = NULL;

WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS];
int _max_params;
WiFiManagerParameter** _params;

template <typename Generic>
void DEBUG_WM(Generic text);
Expand Down

0 comments on commit a4795b7

Please sign in to comment.