Skip to content

Commit 8415e49

Browse files
author
Veijo Pesonen
committed
Fixes connect, and network-status logic
1 parent 12fa3e5 commit 8415e49

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ ESP8266Interface::ESP8266Interface()
5555
_rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset
5656
_ap_sec(NSAPI_SECURITY_UNKNOWN),
5757
_initialized(false),
58-
_started(false),
5958
_conn_stat(NSAPI_STATUS_DISCONNECTED),
6059
_conn_stat_cb(NULL),
6160
_global_event_queue(NULL),
@@ -73,6 +72,8 @@ ESP8266Interface::ESP8266Interface()
7372
_sock_i[i].open = false;
7473
_sock_i[i].sport = 0;
7574
}
75+
76+
_oob2global_event_queue();
7677
}
7778
#endif
7879

@@ -82,7 +83,6 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
8283
_rst_pin(rst),
8384
_ap_sec(NSAPI_SECURITY_UNKNOWN),
8485
_initialized(false),
85-
_started(false),
8686
_conn_stat(NSAPI_STATUS_DISCONNECTED),
8787
_conn_stat_cb(NULL),
8888
_global_event_queue(NULL),
@@ -100,6 +100,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
100100
_sock_i[i].open = false;
101101
_sock_i[i].sport = 0;
102102
}
103+
104+
_oob2global_event_queue();
103105
}
104106

105107
ESP8266Interface::~ESP8266Interface()
@@ -183,10 +185,6 @@ int ESP8266Interface::connect()
183185
return status;
184186
}
185187

186-
if (!_oob_event_id) {
187-
_oob2global_event_queue();
188-
}
189-
190188
if (get_ip_address()) {
191189
return NSAPI_ERROR_IS_CONNECTED;
192190
}
@@ -195,22 +193,12 @@ int ESP8266Interface::connect()
195193
if (status != NSAPI_ERROR_OK) {
196194
return status;
197195
}
198-
_started = true;
199196

200197
if (!_esp.dhcp(true, 1)) {
201198
return NSAPI_ERROR_DHCP_FAILURE;
202199
}
203200

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);
214202
}
215203

216204
int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
@@ -272,19 +260,20 @@ int ESP8266Interface::disconnect()
272260
if (ret == NSAPI_ERROR_OK) {
273261
// Try to lure the nw status update from ESP8266, might come later
274262
_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+
}
277270
}
278271

279272
return ret;
280273
}
281274

282275
const char *ESP8266Interface::get_ip_address()
283276
{
284-
if (!_started) {
285-
return NULL;
286-
}
287-
288277
const char *ip_buff = _esp.ip_addr();
289278
if (!ip_buff || strcmp(ip_buff, "0.0.0.0") == 0) {
290279
return NULL;
@@ -300,17 +289,17 @@ const char *ESP8266Interface::get_mac_address()
300289

301290
const char *ESP8266Interface::get_gateway()
302291
{
303-
return _started ? _esp.gateway() : NULL;
292+
return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.gateway() : NULL;
304293
}
305294

306295
const char *ESP8266Interface::get_netmask()
307296
{
308-
return _started ? _esp.netmask() : NULL;
297+
return _conn_stat != NSAPI_STATUS_DISCONNECTED ? _esp.netmask() : NULL;
309298
}
310299

311300
int8_t ESP8266Interface::get_rssi()
312301
{
313-
return _started ? _esp.rssi() : 0;
302+
return _esp.rssi();
314303
}
315304

316305
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
@@ -391,7 +380,7 @@ void ESP8266Interface::_hw_reset()
391380

392381
nsapi_error_t ESP8266Interface::_startup(const int8_t wifi_mode)
393382
{
394-
if (!_started) {
383+
if (_conn_stat == NSAPI_STATUS_DISCONNECTED) {
395384
if (!_esp.startup(wifi_mode)) {
396385
return NSAPI_ERROR_DEVICE_ERROR;
397386
}
@@ -692,25 +681,26 @@ WiFiInterface *WiFiInterface::get_default_instance()
692681

693682
void ESP8266Interface::update_conn_state_cb()
694683
{
684+
nsapi_connection_status_t prev_stat = _conn_stat;
695685
_conn_stat = _esp.connection_status();
696686

687+
if (prev_stat == _conn_stat) {
688+
return;
689+
}
690+
697691
switch (_conn_stat) {
698692
// Doesn't require changes
699693
case NSAPI_STATUS_CONNECTING:
700694
case NSAPI_STATUS_GLOBAL_UP:
701695
break;
702696
// Start from scratch if connection drops/is dropped
703697
case NSAPI_STATUS_DISCONNECTED:
704-
_started = false;
705698
break;
706699
// Handled on AT layer
707700
case NSAPI_STATUS_LOCAL_UP:
708701
case NSAPI_STATUS_ERROR_UNSUPPORTED:
709702
default:
710-
_started = false;
711703
_initialized = false;
712-
_global_event_queue->cancel(_oob_event_id);
713-
_oob_event_id = 0;
714704
_conn_stat = NSAPI_STATUS_DISCONNECTED;
715705
}
716706

@@ -722,8 +712,6 @@ void ESP8266Interface::update_conn_state_cb()
722712

723713
void ESP8266Interface::proc_oob_evnt()
724714
{
725-
if (_initialized) {
726715
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
727-
}
728716
}
729717
#endif

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
353353
bool _get_firmware_ok();
354354
nsapi_error_t _init(void);
355355
void _hw_reset();
356-
int _started;
357356
nsapi_error_t _startup(const int8_t wifi_mode);
358357

359358
//sigio

0 commit comments

Comments
 (0)