Skip to content

Commit 1695cc4

Browse files
author
Veijo Pesonen
committed
Makes possible to reset the modem if connection dropped
Earlier internal state stayed consistent if connection drop happened by calling disconnect(). Now spontaneous disconnect updates also the drivers internal state.
1 parent 3399fa3 commit 1695cc4

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

ESP8266/ESP8266.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ nsapi_error_t ESP8266::connect(const char *ap, const char *passPhrase)
238238
{
239239
_smutex.lock();
240240
set_timeout(ESP8266_CONNECT_TIMEOUT);
241-
_connection_status = NSAPI_STATUS_CONNECTING;
242-
if(_connection_status_cb)
243-
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
244241

245242
_parser.send("AT+CWJAP_CUR=\"%s\",\"%s\"", ap, passPhrase);
246243
if (!_parser.recv("OK\n")) {
@@ -783,6 +780,11 @@ void ESP8266::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
783780
_connection_status_cb = status_cb;
784781
}
785782

783+
void ESP8266::attach_int(mbed::Callback<void()> status_cb)
784+
{
785+
_conn_state_drv_cb = status_cb;
786+
}
787+
786788
bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
787789
{
788790
int sec;
@@ -856,16 +858,28 @@ void ESP8266::_oob_connection_status()
856858
{
857859
char status[13];
858860
if (_parser.recv("%12[^\"]\n", status)) {
859-
if (strcmp(status, "GOT IP\n") == 0)
861+
if (strcmp(status, "GOT IP\n") == 0) {
860862
_connection_status = NSAPI_STATUS_GLOBAL_UP;
861-
else if (strcmp(status, "DISCONNECT\n") == 0)
863+
} else if (strcmp(status, "DISCONNECT\n") == 0) {
862864
_connection_status = NSAPI_STATUS_DISCONNECTED;
863-
else
864-
return;
865+
} else if (strcmp(status, "CONNECTED\n") == 0) {
866+
_connection_status = NSAPI_STATUS_CONNECTING;
867+
} else {
868+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_EBADMSG), \
869+
"ESP8266::_oob_connection_status: invalid AT cmd\n");
870+
}
871+
} else {
872+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMSG), \
873+
"ESP8266::_oob_connection_status: network status timed out\n");
874+
}
875+
876+
//debug("ESP8266::_oob_connection_status: network state changed to WIFI \"%s\" (%d)\n", status, _connection_status);
865877

866-
if(_connection_status_cb)
867-
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
878+
if(_connection_status_cb) {
879+
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
868880
}
881+
882+
_conn_state_drv_cb();
869883
}
870884

871885
int8_t ESP8266::default_wifi_mode()

ESP8266/ESP8266.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,24 @@ class ESP8266
294294
}
295295

296296
/**
297-
* Attach a function to call whenever network state has changed
297+
* Attach a function to call whenever network state has changed. Driver external
298298
*
299299
* @param func A pointer to a void function, or 0 to set as none
300300
*/
301301
void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
302302

303+
/**
304+
* Attach a function to call whenever network state has changed. Driver internal
305+
*
306+
* @param func A pointer to a void function, or 0 to set as none
307+
*/
308+
void attach_int(mbed::Callback<void()> status_cb);
309+
310+
template <typename T, typename M>
311+
void attach_int(T *obj, M method) {
312+
attach_int(Callback<void()>(obj, method));
313+
}
314+
303315
/**
304316
* Read default Wifi mode from flash
305317
*
@@ -412,7 +424,8 @@ class ESP8266
412424

413425
// Connection state reporting
414426
nsapi_connection_status_t _connection_status;
415-
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
427+
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb; // Application registered
428+
Callback<void()> _conn_state_drv_cb; // ESP8266Interface registered
416429
};
417430

418431
#endif

ESP8266Interface.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ ESP8266Interface::ESP8266Interface()
5151

5252
_esp.sigio(this, &ESP8266Interface::event);
5353
_esp.set_timeout();
54+
_esp.attach_int(this, &ESP8266Interface::update_conn_state_cb);
5455
}
5556

5657
// ESP8266Interface implementation
@@ -68,6 +69,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
6869

6970
_esp.sigio(this, &ESP8266Interface::event);
7071
_esp.set_timeout();
72+
_esp.attach_int(this, &ESP8266Interface::update_conn_state_cb);
7173
}
7274

7375
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
@@ -586,3 +588,23 @@ WiFiInterface *WiFiInterface::get_default_instance() {
586588
}
587589

588590
#endif
591+
592+
void ESP8266Interface::update_conn_state_cb()
593+
{
594+
switch(_esp.connection_status()) {
595+
// Doesn't require changes
596+
case NSAPI_STATUS_CONNECTING:
597+
case NSAPI_STATUS_GLOBAL_UP:
598+
break;
599+
// Start from scratch if connection drops/is dropped
600+
case NSAPI_STATUS_DISCONNECTED:
601+
_started = false;
602+
_initialized = false;
603+
break;
604+
// Handled on AT layer
605+
case NSAPI_STATUS_LOCAL_UP:
606+
case NSAPI_STATUS_ERROR_UNSUPPORTED:
607+
default:
608+
break;
609+
}
610+
}

ESP8266Interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
333333

334334
bool _disable_default_softap();
335335
void event();
336+
void update_conn_state_cb();
336337
bool _get_firmware_ok();
337338
nsapi_error_t _init(void);
338339
nsapi_error_t _startup(const int8_t wifi_mode);

0 commit comments

Comments
 (0)