@@ -36,6 +36,7 @@ extern "C" {
36
36
37
37
#include " WiFiClient.h"
38
38
#include " WiFiUdp.h"
39
+ #include " debug.h"
39
40
40
41
extern " C" void esp_schedule ();
41
42
extern " C" void esp_yield ();
@@ -44,13 +45,20 @@ ESP8266WiFiClass::ESP8266WiFiClass()
44
45
: _smartConfigStarted(false )
45
46
, _smartConfigDone(false )
46
47
, _useStaticIp(false )
48
+ , _persistent(true )
47
49
{
48
50
uint8 m = wifi_get_opmode ();
49
51
_useClientMode = (m & WIFI_STA);
50
52
_useApMode = (m & WIFI_AP);
51
53
wifi_set_event_handler_cb ((wifi_event_handler_cb_t )&ESP8266WiFiClass::_eventCallback);
52
54
}
53
55
56
+ void ESP8266WiFiClass::persistent (bool persistent)
57
+ {
58
+ _persistent = persistent;
59
+ }
60
+
61
+
54
62
void ESP8266WiFiClass::mode (WiFiMode m)
55
63
{
56
64
if (wifi_get_opmode () == (uint8)m) {
@@ -69,9 +77,7 @@ void ESP8266WiFiClass::mode(WiFiMode m)
69
77
_useClientMode = false ;
70
78
}
71
79
72
- ETS_UART_INTR_DISABLE ();
73
- wifi_set_opmode (m);
74
- ETS_UART_INTR_ENABLE ();
80
+ _mode (m);
75
81
}
76
82
77
83
WiFiMode ESP8266WiFiClass::getMode ()
@@ -86,15 +92,44 @@ void ESP8266WiFiClass::_mode(WiFiMode m)
86
92
}
87
93
88
94
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);
90
99
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 ;
91
124
}
92
125
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
+ {
94
128
return begin ((const char *) ssid, (const char *) passphrase, channel, bssid);
95
129
}
96
130
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
+ {
98
133
_useClientMode = true ;
99
134
100
135
if (_useApMode) {
@@ -106,12 +141,12 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
106
141
}
107
142
108
143
if (!ssid || *ssid == 0x00 || strlen (ssid) > 31 ) {
109
- // fail SSID to long or missing!
144
+ // fail SSID too long or missing!
110
145
return WL_CONNECT_FAILED;
111
146
}
112
147
113
148
if (passphrase && strlen (passphrase) > 63 ) {
114
- // fail passphrase to long!
149
+ // fail passphrase too long!
115
150
return WL_CONNECT_FAILED;
116
151
}
117
152
@@ -131,8 +166,18 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
131
166
conf.bssid_set = 0 ;
132
167
}
133
168
169
+ struct station_config current_conf;
170
+ wifi_station_get_config (¤t_conf);
171
+ if (sta_config_equal (current_conf, conf)) {
172
+ DEBUGV (" sta config unchanged" );
173
+ return status ();
174
+ }
175
+
134
176
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);
136
181
wifi_station_connect ();
137
182
ETS_UART_INTR_ENABLE ();
138
183
@@ -203,8 +248,10 @@ int ESP8266WiFiClass::softAPdisconnect(bool wifioff)
203
248
*conf.ssid = 0 ;
204
249
*conf.password = 0 ;
205
250
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);
208
255
ETS_UART_INTR_ENABLE ();
209
256
210
257
if (wifioff) {
@@ -228,7 +275,10 @@ int ESP8266WiFiClass::disconnect(bool wifioff)
228
275
*conf.ssid = 0 ;
229
276
*conf.password = 0 ;
230
277
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);
232
282
wifi_station_disconnect ();
233
283
ETS_UART_INTR_ENABLE ();
234
284
@@ -247,6 +297,20 @@ int ESP8266WiFiClass::disconnect(bool wifioff)
247
297
return 0 ;
248
298
}
249
299
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
+
250
314
void ESP8266WiFiClass::softAP (const char * ssid)
251
315
{
252
316
softAP (ssid, 0 );
@@ -264,8 +328,8 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan
264
328
_mode (WIFI_AP);
265
329
}
266
330
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!
269
333
return ;
270
334
}
271
335
@@ -294,8 +358,19 @@ void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int chan
294
358
strcpy (reinterpret_cast <char *>(conf.password ), passphrase);
295
359
}
296
360
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
+
297
369
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);
299
374
ETS_UART_INTR_ENABLE ();
300
375
}
301
376
0 commit comments