Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFiManagerParameter API Changes #1773

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 49 additions & 31 deletions WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,30 @@ WiFiManagerParameter::WiFiManagerParameter(const char *custom) {
_customHTML = custom;
}

WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label) {
init(id, label, "", 0, "", WFM_LABEL_DEFAULT);
WiFiManagerParameter::WiFiManagerParameter(const char *name, const char *label) {
init(name, label, "", 0, "", WFM_LABEL_DEFAULT);
}

WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length) {
init(id, label, defaultValue, length, "", WFM_LABEL_DEFAULT);
WiFiManagerParameter::WiFiManagerParameter(const char *name, const char *label, const char *defaultValue, int maxLength) {
init(name, label, defaultValue, maxLength, "", WFM_LABEL_DEFAULT);
}

WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom) {
init(id, label, defaultValue, length, custom, WFM_LABEL_DEFAULT);
WiFiManagerParameter::WiFiManagerParameter(const char *name, const char *label, const char *defaultValue, int maxLength, const char *custom) {
init(name, label, defaultValue, maxLength, custom, WFM_LABEL_DEFAULT);
}

WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement) {
init(id, label, defaultValue, length, custom, labelPlacement);
WiFiManagerParameter::WiFiManagerParameter(const char *name, const char *label, const char *defaultValue, int maxLength, const char *custom, int labelPlacement) {
init(name, label, defaultValue, maxLength, custom, labelPlacement);
}

void WiFiManagerParameter::init(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement) {
_id = id;
void WiFiManagerParameter::init(const char *name, const char *label, const char *defaultValue, int maxLength, const char *custom, int labelPlacement) {
_id = name;
_label = label;
_labelPlacement = labelPlacement;
_customHTML = custom;
_length = 0;
_value = nullptr;
setValue(defaultValue,length);
setValue(defaultValue, maxLength);
}

WiFiManagerParameter::~WiFiManagerParameter() {
Expand All @@ -77,19 +77,24 @@ WiFiManagerParameter::~WiFiManagerParameter() {
// }

// @note debug is not available in wmparameter class
void WiFiManagerParameter::setValue(const char *defaultValue, int length) {
void WiFiManagerParameter::setValue(const char *defaultValue, int maxLength) {
if(!_id){
// Serial.println("cannot set value of this parameter");
return;
}

if(maxLength < 0){
// Serial.println("cannot set length below zero");
return;
}

// if(strlen(defaultValue) > length){
// if(strlen(defaultValue) > maxLength){
// // Serial.println("defaultValue length mismatch");
// // return false; //@todo bail
// }

if(_length != length || _value == nullptr){
_length = length;
if(_length != maxLength || _value == nullptr){
_length = maxLength;
if( _value != nullptr){
delete[] _value;
}
Expand All @@ -102,10 +107,20 @@ void WiFiManagerParameter::setValue(const char *defaultValue, int length) {
strncpy(_value, defaultValue, _length);
}
}

void WiFiManagerParameter::setValueReceived(const char* value) {
// by default, this just passes through to 'setValue'
// derived classes can intercept the received value here
setValue(value, getValueMaxLength());
}

const char* WiFiManagerParameter::getValue() const {
// Serial.println(printf("Address of _value is %p\n", (void *)_value));
return _value;
}
const char* WiFiManagerParameter::getName() const {
return _id;
}
const char* WiFiManagerParameter::getID() const {
return _id;
}
Expand All @@ -118,6 +133,9 @@ const char* WiFiManagerParameter::getLabel() const {
int WiFiManagerParameter::getValueLength() const {
return _length;
}
int WiFiManagerParameter::getValueMaxLength() const {
return _length;
}
int WiFiManagerParameter::getLabelPlacement() const {
return _labelPlacement;
}
Expand All @@ -132,12 +150,12 @@ const char* WiFiManagerParameter::getCustomHTML() const {
*/
bool WiFiManager::addParameter(WiFiManagerParameter *p) {

// check param id is valid, unless null
if(p->getID()){
for (size_t i = 0; i < strlen(p->getID()); i++){
if(!(isAlphaNumeric(p->getID()[i])) && !(p->getID()[i]=='_')){
// check param name is valid, unless null
if(p->getName()){
for (size_t i = 0; i < strlen(p->getName()); i++){
if(!(isAlphaNumeric(p->getName()[i])) && !(p->getName()[i]=='_')){
#ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_ERROR,F("[ERROR] parameter IDs can only contain alpha numeric chars"));
DEBUG_WM(WM_DEBUG_ERROR,F("[ERROR] parameter names can only contain alpha numeric chars"));
#endif
return false;
}
Expand Down Expand Up @@ -179,7 +197,7 @@ bool WiFiManager::addParameter(WiFiManagerParameter *p) {
_paramsCount++;

#ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,F("Added Parameter:"),p->getID());
DEBUG_WM(WM_DEBUG_VERBOSE,F("Added Parameter:"),p->getName());
#endif
return true;
}
Expand Down Expand Up @@ -1735,8 +1753,8 @@ String WiFiManager::getParamOut(){
char valLength[5];

for (int i = 0; i < _paramsCount; i++) {
//Serial.println((String)_params[i]->_length);
if (_params[i] == NULL || _params[i]->_length > 99999) {
//Serial.println((String)_params[i]->getValueMaxLength());
if (_params[i] == NULL || _params[i]->getValueMaxLength() > 99999) {
// try to detect param scope issues, doesnt always catch but works ok
#ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_ERROR,F("[ERROR] WiFiManagerParameter is out of scope"));
Expand Down Expand Up @@ -1767,13 +1785,13 @@ String WiFiManager::getParamOut(){
// Input templating
// "<br/><input id='{i}' name='{n}' maxlength='{l}' value='{v}' {c}>";
// if no ID use customhtml for item, else generate from param string
if (_params[i]->getID() != NULL) {
if (_params[i]->getName() != NULL) {
if(tok_I)pitem.replace(FPSTR(T_I), (String)FPSTR(S_parampre)+(String)i); // T_I id number
if(tok_i)pitem.replace(FPSTR(T_i), _params[i]->getID()); // T_i id name
if(tok_n)pitem.replace(FPSTR(T_n), _params[i]->getID()); // T_n id name alias
if(tok_i)pitem.replace(FPSTR(T_i), _params[i]->getName()); // T_i name
if(tok_n)pitem.replace(FPSTR(T_n), _params[i]->getName()); // T_n name alias
if(tok_p)pitem.replace(FPSTR(T_p), FPSTR(T_t)); // T_p replace legacy placeholder token
if(tok_t)pitem.replace(FPSTR(T_t), _params[i]->getLabel()); // T_t title/label
snprintf(valLength, 5, "%d", _params[i]->getValueLength());
snprintf(valLength, 5, "%d", _params[i]->getValueMaxLength());
if(tok_l)pitem.replace(FPSTR(T_l), valLength); // T_l value length
if(tok_v)pitem.replace(FPSTR(T_v), _params[i]->getValue()); // T_v value
if(tok_c)pitem.replace(FPSTR(T_c), _params[i]->getCustomHTML()); // T_c meant for additional attributes, not html, but can stuff
Expand Down Expand Up @@ -1937,7 +1955,7 @@ void WiFiManager::doParamSave(){
#endif

for (int i = 0; i < _paramsCount; i++) {
if (_params[i] == NULL || _params[i]->_length > 99999) {
if (_params[i] == NULL || _params[i]->getValueMaxLength() > 99999) {
#ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_ERROR,F("[ERROR] WiFiManagerParameter is out of scope"));
#endif
Expand All @@ -1949,13 +1967,13 @@ void WiFiManager::doParamSave(){
if(server->hasArg(name)) {
value = server->arg(name);
} else {
value = server->arg(_params[i]->getID());
value = server->arg(_params[i]->getName());
}

//store it in params array
value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated
_params[i]->setValueReceived(value.c_str());
#ifdef WM_DEBUG_LEVEL
DEBUG_WM(WM_DEBUG_VERBOSE,(String)_params[i]->getID() + ":",value);
DEBUG_WM(WM_DEBUG_VERBOSE,(String)_params[i]->getName() + ":",value);
#endif
}
#ifdef WM_DEBUG_LEVEL
Expand Down
27 changes: 15 additions & 12 deletions WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,34 +203,37 @@ class WiFiManagerParameter {
*/
WiFiManagerParameter();
WiFiManagerParameter(const char *custom);
WiFiManagerParameter(const char *id, const char *label);
WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length);
WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom);
WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement);
~WiFiManagerParameter();
WiFiManagerParameter(const char *name, const char *label);
WiFiManagerParameter(const char *name, const char *label, const char *defaultValue, int maxLength);
WiFiManagerParameter(const char *name, const char *label, const char *defaultValue, int maxLength, const char *custom);
WiFiManagerParameter(const char *name, const char *label, const char *defaultValue, int maxLength, const char *custom, int labelPlacement);
virtual ~WiFiManagerParameter();
// WiFiManagerParameter& operator=(const WiFiManagerParameter& rhs);

const char *getID() const;
const char *getName() const;
const char *getID() const; // @deprecated, use getName
const char *getValue() const;
const char *getLabel() const;
const char *getPlaceholder() const; // @deprecated, use getLabel
int getValueLength() const;
int getValueLength() const; // @deprecated, use getValueMaxLength
int getValueMaxLength() const;
int getLabelPlacement() const;
virtual const char *getCustomHTML() const;
void setValue(const char *defaultValue, int length);
void setValue(const char *defaultValue, int maxLength);

virtual void setValueReceived(const char* value);

protected:
void init(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement);
void init(const char *name, const char *label, const char *defaultValue, int maxLength, const char *custom, int labelPlacement);

WiFiManagerParameter& operator=(const WiFiManagerParameter&);
const char *_id;
const char *_id; // @deprecated this should be _name
const char *_label;
char *_value;
int _length;
int _length; // @deprecated this should be _maxLength
int _labelPlacement;

const char *_customHTML;
friend class WiFiManager;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void loop() {

void saveParamsCallback () {
Serial.println("Get Params:");
Serial.print(custom_mqtt_server.getID());
Serial.print(custom_mqtt_server.getName());
Serial.print(" : ");
Serial.println(custom_mqtt_server.getValue());
}
4 changes: 2 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ setSTAStaticIPConfig KEYWORD2
setAPCallback KEYWORD2
setSaveConfigCallback KEYWORD2
addParameter KEYWORD2
getID KEYWORD2
getName KEYWORD2
getValue KEYWORD2
getPlaceholder KEYWORD2
getValueLength KEYWORD2
getValueMaxLength KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down