Skip to content

Commit 40da463

Browse files
committed
ESP8266WiFi library: add persistent option, fix #1054
1 parent 8bf1e98 commit 40da463

File tree

2 files changed

+95
-17
lines changed

2 files changed

+95
-17
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+90-15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C" {
3636

3737
#include "WiFiClient.h"
3838
#include "WiFiUdp.h"
39+
#include "debug.h"
3940

4041
extern "C" void esp_schedule();
4142
extern "C" void esp_yield();
@@ -44,13 +45,20 @@ ESP8266WiFiClass::ESP8266WiFiClass()
4445
: _smartConfigStarted(false)
4546
, _smartConfigDone(false)
4647
, _useStaticIp(false)
48+
, _persistent(true)
4749
{
4850
uint8 m = wifi_get_opmode();
4951
_useClientMode = (m & WIFI_STA);
5052
_useApMode = (m & WIFI_AP);
5153
wifi_set_event_handler_cb((wifi_event_handler_cb_t)&ESP8266WiFiClass::_eventCallback);
5254
}
5355

56+
void ESP8266WiFiClass::persistent(bool persistent)
57+
{
58+
_persistent = persistent;
59+
}
60+
61+
5462
void ESP8266WiFiClass::mode(WiFiMode m)
5563
{
5664
if(wifi_get_opmode() == (uint8)m) {
@@ -69,9 +77,7 @@ void ESP8266WiFiClass::mode(WiFiMode m)
6977
_useClientMode = false;
7078
}
7179

72-
ETS_UART_INTR_DISABLE();
73-
wifi_set_opmode(m);
74-
ETS_UART_INTR_ENABLE();
80+
_mode(m);
7581
}
7682

7783
WiFiMode ESP8266WiFiClass::getMode()
@@ -86,15 +92,44 @@ void ESP8266WiFiClass::_mode(WiFiMode m)
8692
}
8793

8894
ETS_UART_INTR_DISABLE();
89-
wifi_set_opmode(m);
95+
if (_persistent)
96+
wifi_set_opmode(m);
97+
else
98+
wifi_set_opmode_current(m);
9099
ETS_UART_INTR_ENABLE();
100+
101+
}
102+
103+
static bool sta_config_equal(const station_config& lhs, const station_config& rhs)
104+
{
105+
if (strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0)
106+
return false;
107+
108+
if (strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0)
109+
return false;
110+
111+
if (lhs.bssid_set) {
112+
if (!rhs.bssid_set)
113+
return false;
114+
115+
if (memcmp(lhs.bssid, rhs.bssid, 6) != 0)
116+
return false;
117+
}
118+
else {
119+
if (rhs.bssid_set)
120+
return false;
121+
}
122+
123+
return true;
91124
}
92125

93-
int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, uint8_t bssid[6]){
126+
int ESP8266WiFiClass::begin(char* ssid, char *passphrase, int32_t channel, const uint8_t* bssid)
127+
{
94128
return begin((const char*) ssid, (const char*) passphrase, channel, bssid);
95129
}
96130

97-
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, uint8_t bssid[6]){
131+
int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t channel, const uint8_t* bssid)
132+
{
98133
_useClientMode = true;
99134

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

108143
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
109-
// fail SSID to long or missing!
144+
// fail SSID too long or missing!
110145
return WL_CONNECT_FAILED;
111146
}
112147

113148
if(passphrase && strlen(passphrase) > 63) {
114-
// fail passphrase to long!
149+
// fail passphrase too long!
115150
return WL_CONNECT_FAILED;
116151
}
117152

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

169+
struct station_config current_conf;
170+
wifi_station_get_config(&current_conf);
171+
if (sta_config_equal(current_conf, conf)) {
172+
DEBUGV("sta config unchanged");
173+
return status();
174+
}
175+
134176
ETS_UART_INTR_DISABLE();
135-
wifi_station_set_config(&conf);
177+
if (_persistent)
178+
wifi_station_set_config(&conf);
179+
else
180+
wifi_station_set_config_current(&conf);
136181
wifi_station_connect();
137182
ETS_UART_INTR_ENABLE();
138183

@@ -203,8 +248,10 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff)
203248
*conf.ssid = 0;
204249
*conf.password = 0;
205250
ETS_UART_INTR_DISABLE();
206-
wifi_softap_set_config(&conf);
207-
wifi_station_disconnect();
251+
if (_persistent)
252+
wifi_softap_set_config(&conf);
253+
else
254+
wifi_softap_set_config_current(&conf);
208255
ETS_UART_INTR_ENABLE();
209256

210257
if(wifioff) {
@@ -228,7 +275,10 @@ int ESP8266WiFiClass::disconnect(bool wifioff)
228275
*conf.ssid = 0;
229276
*conf.password = 0;
230277
ETS_UART_INTR_DISABLE();
231-
wifi_station_set_config(&conf);
278+
if (_persistent)
279+
wifi_station_set_config(&conf);
280+
else
281+
wifi_station_set_config_current(&conf);
232282
wifi_station_disconnect();
233283
ETS_UART_INTR_ENABLE();
234284

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

300+
static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs)
301+
{
302+
if (strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0)
303+
return false;
304+
if (strcmp(reinterpret_cast<const char*>(lhs.password), reinterpret_cast<const char*>(rhs.password)) != 0)
305+
return false;
306+
if (lhs.channel != rhs.channel)
307+
return false;
308+
if (lhs.ssid_hidden != rhs.ssid_hidden)
309+
return false;
310+
return true;
311+
}
312+
313+
250314
void ESP8266WiFiClass::softAP(const char* ssid)
251315
{
252316
softAP(ssid, 0);
@@ -264,8 +328,8 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan
264328
_mode(WIFI_AP);
265329
}
266330

267-
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
268-
// fail SSID to long or missing!
331+
if(!ssid || strlen(ssid) || strlen(ssid) > 31) {
332+
// fail SSID too long or missing!
269333
return;
270334
}
271335

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

361+
struct softap_config conf_current;
362+
wifi_softap_get_config(&conf_current);
363+
if (!softap_config_equal(conf, conf_current))
364+
{
365+
DEBUGV("softap config unchanged");
366+
return;
367+
}
368+
297369
ETS_UART_INTR_DISABLE();
298-
wifi_softap_set_config(&conf);
370+
if (_persistent)
371+
wifi_softap_set_config(&conf);
372+
else
373+
wifi_softap_set_config_current(&conf);
299374
ETS_UART_INTR_ENABLE();
300375
}
301376

libraries/ESP8266WiFi/src/ESP8266WiFi.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ESP8266WiFiClass
4444

4545
ESP8266WiFiClass();
4646

47+
void persistent(bool persistent);
48+
4749
void mode(WiFiMode);
4850
WiFiMode getMode();
4951

@@ -56,8 +58,8 @@ class ESP8266WiFiClass
5658
* @param channel Optional. Channel of AP
5759
* @return
5860
*/
59-
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
60-
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
61+
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
62+
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL);
6163

6264
// Use sdk config to connect.
6365
int begin();
@@ -385,6 +387,7 @@ class ESP8266WiFiClass
385387
bool _useApMode;
386388
bool _useClientMode;
387389
bool _useStaticIp;
390+
bool _persistent;
388391

389392
static bool _scanAsync;
390393
static bool _scanStarted;

0 commit comments

Comments
 (0)