@@ -55,7 +55,6 @@ ESP8266Interface::ESP8266Interface()
55
55
_rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset
56
56
_ap_sec(NSAPI_SECURITY_UNKNOWN),
57
57
_initialized(false ),
58
- _started(false ),
59
58
_conn_stat(NSAPI_STATUS_DISCONNECTED),
60
59
_conn_stat_cb(NULL ),
61
60
_global_event_queue(NULL ),
@@ -73,6 +72,8 @@ ESP8266Interface::ESP8266Interface()
73
72
_sock_i[i].open = false ;
74
73
_sock_i[i].sport = 0 ;
75
74
}
75
+
76
+ _oob2global_event_queue ();
76
77
}
77
78
#endif
78
79
@@ -82,7 +83,6 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
82
83
_rst_pin(rst),
83
84
_ap_sec(NSAPI_SECURITY_UNKNOWN),
84
85
_initialized(false ),
85
- _started(false ),
86
86
_conn_stat(NSAPI_STATUS_DISCONNECTED),
87
87
_conn_stat_cb(NULL ),
88
88
_global_event_queue(NULL ),
@@ -100,6 +100,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
100
100
_sock_i[i].open = false ;
101
101
_sock_i[i].sport = 0 ;
102
102
}
103
+
104
+ _oob2global_event_queue ();
103
105
}
104
106
105
107
ESP8266Interface::~ESP8266Interface ()
@@ -183,10 +185,6 @@ int ESP8266Interface::connect()
183
185
return status;
184
186
}
185
187
186
- if (!_oob_event_id) {
187
- _oob2global_event_queue ();
188
- }
189
-
190
188
if (get_ip_address ()) {
191
189
return NSAPI_ERROR_IS_CONNECTED;
192
190
}
@@ -195,22 +193,12 @@ int ESP8266Interface::connect()
195
193
if (status != NSAPI_ERROR_OK) {
196
194
return status;
197
195
}
198
- _started = true ;
199
196
200
197
if (!_esp.dhcp (true , 1 )) {
201
198
return NSAPI_ERROR_DHCP_FAILURE;
202
199
}
203
200
204
- int connect_error = _esp.connect (ap_ssid, ap_pass);
205
- if (connect_error) {
206
- return connect_error;
207
- }
208
-
209
- if (!get_ip_address ()) {
210
- return NSAPI_ERROR_DHCP_FAILURE;
211
- }
212
-
213
- return NSAPI_ERROR_OK;
201
+ return _esp.connect (ap_ssid, ap_pass);
214
202
}
215
203
216
204
int ESP8266Interface::set_credentials (const char *ssid, const char *pass, nsapi_security_t security)
@@ -272,19 +260,20 @@ int ESP8266Interface::disconnect()
272
260
if (ret == NSAPI_ERROR_OK) {
273
261
// Try to lure the nw status update from ESP8266, might come later
274
262
_esp.bg_process_oob (ESP8266_RECV_TIMEOUT, true );
275
- // In case the status update arrives later
276
- _conn_stat = NSAPI_STATUS_DISCONNECTED;
263
+ // In case the status update arrives later inform upper layers manually
264
+ if (_conn_stat != NSAPI_STATUS_DISCONNECTED) {
265
+ _conn_stat = NSAPI_STATUS_DISCONNECTED;
266
+ if (_conn_stat_cb) {
267
+ _conn_stat_cb (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
268
+ }
269
+ }
277
270
}
278
271
279
272
return ret;
280
273
}
281
274
282
275
const char *ESP8266Interface::get_ip_address ()
283
276
{
284
- if (!_started) {
285
- return NULL ;
286
- }
287
-
288
277
const char *ip_buff = _esp.ip_addr ();
289
278
if (!ip_buff || strcmp (ip_buff, " 0.0.0.0" ) == 0 ) {
290
279
return NULL ;
@@ -300,17 +289,17 @@ const char *ESP8266Interface::get_mac_address()
300
289
301
290
const char *ESP8266Interface::get_gateway ()
302
291
{
303
- return _started ? _esp.gateway () : NULL ;
292
+ return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.gateway () : NULL ;
304
293
}
305
294
306
295
const char *ESP8266Interface::get_netmask ()
307
296
{
308
- return _started ? _esp.netmask () : NULL ;
297
+ return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.netmask () : NULL ;
309
298
}
310
299
311
300
int8_t ESP8266Interface::get_rssi ()
312
301
{
313
- return _started ? _esp.rssi () : 0 ;
302
+ return _esp.rssi ();
314
303
}
315
304
316
305
int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count)
@@ -391,7 +380,7 @@ void ESP8266Interface::_hw_reset()
391
380
392
381
nsapi_error_t ESP8266Interface::_startup (const int8_t wifi_mode)
393
382
{
394
- if (!_started ) {
383
+ if (_conn_stat == NSAPI_STATUS_DISCONNECTED ) {
395
384
if (!_esp.startup (wifi_mode)) {
396
385
return NSAPI_ERROR_DEVICE_ERROR;
397
386
}
@@ -692,25 +681,26 @@ WiFiInterface *WiFiInterface::get_default_instance()
692
681
693
682
void ESP8266Interface::update_conn_state_cb ()
694
683
{
684
+ nsapi_connection_status_t prev_stat = _conn_stat;
695
685
_conn_stat = _esp.connection_status ();
696
686
687
+ if (prev_stat == _conn_stat) {
688
+ return ;
689
+ }
690
+
697
691
switch (_conn_stat) {
698
692
// Doesn't require changes
699
693
case NSAPI_STATUS_CONNECTING:
700
694
case NSAPI_STATUS_GLOBAL_UP:
701
695
break ;
702
696
// Start from scratch if connection drops/is dropped
703
697
case NSAPI_STATUS_DISCONNECTED:
704
- _started = false ;
705
698
break ;
706
699
// Handled on AT layer
707
700
case NSAPI_STATUS_LOCAL_UP:
708
701
case NSAPI_STATUS_ERROR_UNSUPPORTED:
709
702
default :
710
- _started = false ;
711
703
_initialized = false ;
712
- _global_event_queue->cancel (_oob_event_id);
713
- _oob_event_id = 0 ;
714
704
_conn_stat = NSAPI_STATUS_DISCONNECTED;
715
705
}
716
706
@@ -722,8 +712,6 @@ void ESP8266Interface::update_conn_state_cb()
722
712
723
713
void ESP8266Interface::proc_oob_evnt ()
724
714
{
725
- if (_initialized) {
726
715
_esp.bg_process_oob (ESP8266_RECV_TIMEOUT, true );
727
- }
728
716
}
729
717
#endif
0 commit comments