Skip to content

Commit

Permalink
ESP8266WiFi library: add persistent option, fix #1054
Browse files Browse the repository at this point in the history
  • Loading branch information
igrr committed Nov 21, 2015
1 parent 8bf1e98 commit 40da463
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 17 deletions.
105 changes: 90 additions & 15 deletions libraries/ESP8266WiFi/src/ESP8266WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern "C" {

#include "WiFiClient.h"
#include "WiFiUdp.h"
#include "debug.h"

extern "C" void esp_schedule();
extern "C" void esp_yield();
Expand All @@ -44,13 +45,20 @@ ESP8266WiFiClass::ESP8266WiFiClass()
: _smartConfigStarted(false)
, _smartConfigDone(false)
, _useStaticIp(false)
, _persistent(true)
{
uint8 m = wifi_get_opmode();
_useClientMode = (m & WIFI_STA);
_useApMode = (m & WIFI_AP);
wifi_set_event_handler_cb((wifi_event_handler_cb_t)&ESP8266WiFiClass::_eventCallback);
}

void ESP8266WiFiClass::persistent(bool persistent)
{
_persistent = persistent;
}


void ESP8266WiFiClass::mode(WiFiMode m)
{
if(wifi_get_opmode() == (uint8)m) {
Expand All @@ -69,9 +77,7 @@ void ESP8266WiFiClass::mode(WiFiMode m)
_useClientMode = false;
}

ETS_UART_INTR_DISABLE();
wifi_set_opmode(m);
ETS_UART_INTR_ENABLE();
_mode(m);
}

WiFiMode ESP8266WiFiClass::getMode()
Expand All @@ -86,15 +92,44 @@ void ESP8266WiFiClass::_mode(WiFiMode m)
}

ETS_UART_INTR_DISABLE();
wifi_set_opmode(m);
if (_persistent)
wifi_set_opmode(m);
else
wifi_set_opmode_current(m);
ETS_UART_INTR_ENABLE();

}

static bool sta_config_equal(const station_config& lhs, const station_config& rhs)
{
if (strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0)
return false;

if (strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0)
return false;

if (lhs.bssid_set) {
if (!rhs.bssid_set)
return false;

if (memcmp(lhs.bssid, rhs.bssid, 6) != 0)
return false;
}
else {
if (rhs.bssid_set)
return false;
}

return true;
}

int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, uint8_t bssid[6]){
int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid)
{
return begin((const char*) ssid, (const char*) passphrase, channel, bssid);
}

int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, uint8_t bssid[6]){
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid)
{
_useClientMode = true;

if(_useApMode) {
Expand All @@ -106,12 +141,12 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
}

if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID to long or missing!
// fail SSID too long or missing!
return WL_CONNECT_FAILED;
}

if(passphrase && strlen(passphrase) > 63) {
// fail passphrase to long!
// fail passphrase too long!
return WL_CONNECT_FAILED;
}

Expand All @@ -131,8 +166,18 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
conf.bssid_set = 0;
}

struct station_config current_conf;
wifi_station_get_config(&current_conf);
if (sta_config_equal(current_conf, conf)) {
DEBUGV("sta config unchanged");
return status();
}

ETS_UART_INTR_DISABLE();
wifi_station_set_config(&conf);
if (_persistent)
wifi_station_set_config(&conf);
else
wifi_station_set_config_current(&conf);
wifi_station_connect();
ETS_UART_INTR_ENABLE();

Expand Down Expand Up @@ -203,8 +248,10 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff)
*conf.ssid = 0;
*conf.password = 0;
ETS_UART_INTR_DISABLE();
wifi_softap_set_config(&conf);
wifi_station_disconnect();
if (_persistent)
wifi_softap_set_config(&conf);
else
wifi_softap_set_config_current(&conf);
ETS_UART_INTR_ENABLE();

if(wifioff) {
Expand All @@ -228,7 +275,10 @@ int ESP8266WiFiClass::disconnect(bool wifioff)
*conf.ssid = 0;
*conf.password = 0;
ETS_UART_INTR_DISABLE();
wifi_station_set_config(&conf);
if (_persistent)
wifi_station_set_config(&conf);
else
wifi_station_set_config_current(&conf);
wifi_station_disconnect();
ETS_UART_INTR_ENABLE();

Expand All @@ -247,6 +297,20 @@ int ESP8266WiFiClass::disconnect(bool wifioff)
return 0;
}

static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs)
{
if (strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0)
return false;
if (strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0)
return false;
if (lhs.channel != rhs.channel)
return false;
if (lhs.ssid_hidden != rhs.ssid_hidden)
return false;
return true;
}


void ESP8266WiFiClass::softAP(const char* ssid)
{
softAP(ssid, 0);
Expand All @@ -264,8 +328,8 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan
_mode(WIFI_AP);
}

if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID to long or missing!
if(!ssid || strlen(ssid) || strlen(ssid) > 31) {
// fail SSID too long or missing!
return;
}

Expand Down Expand Up @@ -294,8 +358,19 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan
strcpy(reinterpret_cast<char*>(conf.password), passphrase);
}

struct softap_config conf_current;
wifi_softap_get_config(&conf_current);
if (!softap_config_equal(conf, conf_current))
{
DEBUGV("softap config unchanged");
return;
}

ETS_UART_INTR_DISABLE();
wifi_softap_set_config(&conf);
if (_persistent)
wifi_softap_set_config(&conf);
else
wifi_softap_set_config_current(&conf);
ETS_UART_INTR_ENABLE();
}

Expand Down
7 changes: 5 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ESP8266WiFiClass

ESP8266WiFiClass();

void persistent(bool persistent);

void mode(WiFiMode);
WiFiMode getMode();

Expand All @@ -56,8 +58,8 @@ class ESP8266WiFiClass
* @param channel Optional. Channel of AP
* @return
*/
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);

// Use sdk config to connect.
int begin();
Expand Down Expand Up @@ -385,6 +387,7 @@ class ESP8266WiFiClass
bool _useApMode;
bool _useClientMode;
bool _useStaticIp;
bool _persistent;

static bool _scanAsync;
static bool _scanStarted;
Expand Down

0 comments on commit 40da463

Please sign in to comment.