Cleaned the way config parameters are applied

This commit is contained in:
anschrammh 2022-04-25 21:25:32 +02:00
parent f96ccfd8a1
commit a4da8959e9

View File

@ -1,5 +1,7 @@
#include "ConnectivityManager.h"
//#define DEBUG_CONMAN
ConnectivityManager::ConnectivityManager(bool persistent)
{
ConnectivityManager::persistent(persistent);
@ -16,7 +18,7 @@ boolean ConnectivityManager::connect()
{
if(!WiFi.disconnect(true))_error |= STA_ENABLED_DISABLE_ERR;
if(!WiFi.softAPdisconnect(true))_error |= AP_ENABLED_DISABLE_ERR;
if(!softAP("ESP8266SwissArmyBoard", NULL, 1, false, 8))_error |= AP_SETUP_ERR;
if(!softAP("ESP8266SwissArmyBoard"))_error |= AP_SETUP_ERR;
return _error == NO_ERROR;
}
@ -32,30 +34,30 @@ boolean ConnectivityManager::connectToSTA()
CFGFileParser cfgFileParserSTA(*_sdCardManager, STA_CFG_FILE);
CFGDictionary<CFGParameterValue> *cfgDictionary = (CFGDictionary<CFGParameterValue> *) cfgFileParserSTA.parseFile();
const char *PSK(nullptr);
uint8_t channel(0);
boolean toBeReturned(true);
if(!WiFi.disconnect(true))_error |= STA_ENABLED_DISABLE_ERR;
if(cfgDictionary != NULL)
if(cfgDictionary != nullptr)
{
if((*cfgDictionary)("SSID") != NULL && (*cfgDictionary)("PASSWORD") != NULL && (*cfgDictionary)("ENABLED") != NULL)
if((*cfgDictionary)("ENABLED") != nullptr && (*cfgDictionary)("SSID") != nullptr)
{
if((*cfgDictionary)("ENABLED")->booleanValue())
{
if(!begin((*cfgDictionary)("SSID")->stringValue(), (*cfgDictionary)("PASSWORD")->stringValue()))
if((*cfgDictionary)("PASSWORD") != nullptr)
PSK = (*cfgDictionary)("PASSWORD")->stringValue();
if((*cfgDictionary)("CHANNEL") != nullptr)
channel = (*cfgDictionary)("CHANNEL")->intValue();
if(!begin((*cfgDictionary)("SSID")->stringValue(), PSK, channel))
{
_error |= STA_SETUP_ERR;
toBeReturned = false;
}
}
}
}
else
toBeReturned = false;
delete cfgDictionary;
}
else
toBeReturned = false;
return toBeReturned;
}
@ -63,35 +65,76 @@ boolean ConnectivityManager::startAP()
{
CFGFileParser cfgFileParser(*_sdCardManager, AP_CFG_FILE);
CFGDictionary<CFGParameterValue> *cfgDictionary = (CFGDictionary<CFGParameterValue> *) cfgFileParser.parseFile();
const char defaultSSID[] = "ESP8266SwissArmyBoard";
const char *SSID(defaultSSID);
const char *PSK(nullptr);
uint8_t channel(1), maxConnection(4);
boolean hideSSID(false);
boolean toBeReturned(true);
if(!WiFi.softAPdisconnect(true))_error |= AP_ENABLED_DISABLE_ERR;
if(cfgDictionary == NULL)
if(cfgDictionary == nullptr)
{
if(!softAP("ESP8266SwissArmyBoard", NULL, 1, false, 8))_error |= AP_SETUP_ERR;
return false;
}
else if((*cfgDictionary)("SSID") != NULL && (*cfgDictionary)("PASSWORD") != NULL && (*cfgDictionary)("CHANNEL") != NULL && (*cfgDictionary)("HIDE_SSID") != NULL && (*cfgDictionary)("AP_MAX_CONNECTION") != NULL && (*cfgDictionary)("ENABLED") != NULL)
{
if((*cfgDictionary)("ENABLED")->booleanValue())
if(!softAP(defaultSSID))
{
if(!softAP((*cfgDictionary)("SSID")->stringValue(), strcmp((*cfgDictionary)("PASSWORD")->stringValue(),"") == 0 ? NULL:(*cfgDictionary)("PASSWORD")->stringValue(), (*cfgDictionary)("CHANNEL")->intValue(), (*cfgDictionary)("HIDE_SSID")->booleanValue(), (*cfgDictionary)("AP_MAX_CONNECTION")->intValue()))
_error |= AP_SETUP_ERR;
toBeReturned = false;
}
}
else
{
if((*cfgDictionary)("ENABLED") != nullptr)
{
if((*cfgDictionary)("ENABLED")->booleanValue())
{
if((*cfgDictionary)("SSID") != nullptr)
SSID = (*cfgDictionary)("SSID")->stringValue();
if((*cfgDictionary)("PASSWORD") != nullptr)
PSK = (*cfgDictionary)("PASSWORD")->stringValue();
if((*cfgDictionary)("CHANNEL") != nullptr)
channel = (*cfgDictionary)("CHANNEL")->intValue();
if((*cfgDictionary)("HIDE_SSID") != nullptr)
hideSSID = (*cfgDictionary)("HIDE_SSID")->booleanValue();
if((*cfgDictionary)("AP_MAX_CONNECTION") != nullptr)
maxConnection = (*cfgDictionary)("AP_MAX_CONNECTION")->intValue();
#ifdef DEBUG_CONMAN
Serial.printf("AP is enabled, params are :\nSSID : %s\nPSK : %s\nCHAN : %u\nHIDE_SSID : %u\nMAX_CON : %u\n",
SSID,
PSK ? PSK : "",
channel,
hideSSID,
maxConnection);
#endif
if(!softAP(SSID, PSK, channel, hideSSID, maxConnection))
{
_error |= AP_SETUP_ERR;
toBeReturned = false;
}
}
else
{
#ifdef DEBUG_CONMAN
Serial.printf("AP is disabled, nothing to be done\n");
#endif
}
}
else //If the enable parameter is not specified we start the AP by default.
{
#ifdef DEBUG_CONMAN
Serial.printf("ENABLED param missing, enabling AP\n");
#endif
if(!softAP(SSID, PSK, channel, hideSSID, maxConnection))
{
_error |= AP_SETUP_ERR;
toBeReturned = false;
}
}
delete cfgDictionary;
}
else
{
if(!softAP("ESP8266SwissArmyBoard", NULL, 1, false, 8))_error |= AP_SETUP_ERR;
toBeReturned = false;
}
delete cfgDictionary;
return toBeReturned;
}