114 lines
3.3 KiB
C++
114 lines
3.3 KiB
C++
#include "ConnectivityManager.h"
|
|
|
|
ConnectivityManager::ConnectivityManager() : _error(0), _sdCardManager(NULL)
|
|
{
|
|
persistent(false);
|
|
|
|
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;
|
|
}
|
|
|
|
ConnectivityManager::ConnectivityManager(SDCardManager &sdCardManager) : _error(0), _sdCardManager(&sdCardManager)
|
|
{
|
|
persistent(false);
|
|
|
|
startAP();
|
|
connectToSTA();
|
|
}
|
|
|
|
boolean ConnectivityManager::connectToSTA()
|
|
{
|
|
//STATION PART
|
|
CFGFileParser cfgFileParserSTA(*_sdCardManager, STA_CFG_FILE);
|
|
CFGDictionary<CFGParameterValue> *cfgDictionary = (CFGDictionary<CFGParameterValue> *) cfgFileParserSTA.parseFile();
|
|
|
|
boolean toBeReturned(true);
|
|
if(!WiFi.disconnect(true))_error |= STA_ENABLED_DISABLE_ERR;
|
|
|
|
if(cfgDictionary != NULL)
|
|
{
|
|
if((*cfgDictionary)("SSID") != NULL && (*cfgDictionary)("PASSWORD") != NULL && (*cfgDictionary)("ENABLED") != NULL)
|
|
{
|
|
if((*cfgDictionary)("ENABLED")->booleanValue())
|
|
{
|
|
if(!begin((*cfgDictionary)("SSID")->stringValue(), (*cfgDictionary)("PASSWORD")->stringValue()))
|
|
{
|
|
_error |= STA_SETUP_ERR;
|
|
toBeReturned = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
toBeReturned = false;
|
|
|
|
delete cfgDictionary;
|
|
}
|
|
else
|
|
toBeReturned = false;
|
|
|
|
return toBeReturned;
|
|
}
|
|
|
|
boolean ConnectivityManager::startAP()
|
|
{
|
|
CFGFileParser cfgFileParser(*_sdCardManager, AP_CFG_FILE);
|
|
CFGDictionary<CFGParameterValue> *cfgDictionary = (CFGDictionary<CFGParameterValue> *) cfgFileParser.parseFile();
|
|
|
|
boolean toBeReturned(true);
|
|
if(!WiFi.softAPdisconnect(true))_error |= AP_ENABLED_DISABLE_ERR;
|
|
|
|
if(cfgDictionary == NULL)
|
|
{
|
|
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)("SSID_HIDDEN") != NULL && (*cfgDictionary)("AP_MAX_CONNECTION") != NULL && (*cfgDictionary)("ENABLED") != NULL)
|
|
{
|
|
if((*cfgDictionary)("ENABLED")->booleanValue())
|
|
{
|
|
if(!softAP((*cfgDictionary)("SSID")->stringValue(), strcmp((*cfgDictionary)("PASSWORD")->stringValue(),"") == 0 ? NULL:(*cfgDictionary)("PASSWORD")->stringValue(), (*cfgDictionary)("CHANNEL")->intValue(), (*cfgDictionary)("SSID_HIDDEN")->booleanValue(), (*cfgDictionary)("AP_MAX_CONNECTION")->intValue()))
|
|
{
|
|
_error |= AP_SETUP_ERR;
|
|
toBeReturned = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(!softAP("ESP8266SwissArmyBoard", NULL, 1, false, 8))_error |= AP_SETUP_ERR;
|
|
toBeReturned = false;
|
|
}
|
|
|
|
delete cfgDictionary;
|
|
|
|
return toBeReturned;
|
|
}
|
|
|
|
boolean ConnectivityManager::enableSTAAndAP(boolean enabled)
|
|
{
|
|
return enableSTA(enabled) && enableAP(enabled);
|
|
}
|
|
|
|
boolean ConnectivityManager::isSTAEnabled()
|
|
{
|
|
return (getMode() == WIFI_AP_STA || getMode() == WIFI_STA);
|
|
}
|
|
|
|
boolean ConnectivityManager::isAPEnabled()
|
|
{
|
|
return (getMode() == WIFI_AP_STA || getMode() == WIFI_AP);
|
|
}
|
|
|
|
unsigned char ConnectivityManager::RSSIPercent()
|
|
{
|
|
int RSSIdBm = RSSI();
|
|
return (RSSIdBm < MIN_RSSI) ? 0 : COEF_RSSI * ((double)RSSIdBm - (double)MIN_RSSI);
|
|
}
|
|
|
|
unsigned char ConnectivityManager::getError() const
|
|
{
|
|
return _error;
|
|
}
|