Skip to content

Commit 71123ed

Browse files
author
Veijo Pesonen
committed
Fix network status callback layering
1 parent 7edc785 commit 71123ed

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

ESP8266/ESP8266.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,9 @@ void ESP8266::sigio(Callback<void()> func)
774774
_serial.sigio(func);
775775
}
776776

777-
void ESP8266::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
777+
void ESP8266::attach(Callback<void()> status_cb)
778778
{
779-
_connection_status_cb = status_cb;
780-
}
781-
782-
void ESP8266::attach_int(mbed::Callback<void()> status_cb)
783-
{
784-
_conn_state_drv_cb = status_cb;
779+
_conn_stat_cb = status_cb;
785780
}
786781

787782
bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
@@ -872,11 +867,8 @@ void ESP8266::_oob_connection_status()
872867
"ESP8266::_oob_connection_status: network status timed out\n");
873868
}
874869

875-
if(_connection_status_cb) {
876-
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
877-
}
878-
879-
_conn_state_drv_cb();
870+
MBED_ASSERT(_conn_stat_cb);
871+
_conn_stat_cb();
880872
}
881873

882874
int8_t ESP8266::default_wifi_mode()

ESP8266/ESP8266.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,22 +294,15 @@ class ESP8266
294294
}
295295

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

310303
template <typename T, typename M>
311-
void attach_int(T *obj, M method) {
312-
attach_int(Callback<void()>(obj, method));
304+
void attach(T *obj, M method) {
305+
attach(mbed::Callback<void()>(obj, method));
313306
}
314307

315308
/**
@@ -424,8 +417,7 @@ class ESP8266
424417

425418
// Connection state reporting
426419
nsapi_connection_status_t _connection_status;
427-
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb; // Application registered
428-
Callback<void()> _conn_state_drv_cb; // ESP8266Interface registered
420+
Callback<void()> _conn_stat_cb; // ESP8266Interface registered
429421
};
430422

431423
#endif

ESP8266Interface.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ ESP8266Interface::ESP8266Interface()
4747
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
4848
_initialized(false),
4949
_started(false),
50-
_ap_sec(NSAPI_SECURITY_UNKNOWN)
50+
_ap_sec(NSAPI_SECURITY_UNKNOWN),
51+
_conn_stat(NSAPI_STATUS_DISCONNECTED),
52+
_conn_stat_cb(NULL)
5153
{
5254
memset(_ids, 0, sizeof(_ids));
5355
memset(_cbs, 0, sizeof(_cbs));
@@ -57,7 +59,7 @@ ESP8266Interface::ESP8266Interface()
5759

5860
_esp.sigio(this, &ESP8266Interface::event);
5961
_esp.set_timeout();
60-
_esp.attach_int(this, &ESP8266Interface::update_conn_state_cb);
62+
_esp.attach(this, &ESP8266Interface::update_conn_state_cb);
6163
}
6264
#endif
6365

@@ -66,7 +68,9 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
6668
: _esp(tx, rx, debug, rts, cts),
6769
_initialized(false),
6870
_started(false),
69-
_ap_sec(NSAPI_SECURITY_UNKNOWN)
71+
_ap_sec(NSAPI_SECURITY_UNKNOWN),
72+
_conn_stat(NSAPI_STATUS_DISCONNECTED),
73+
_conn_stat_cb(NULL)
7074
{
7175
memset(_ids, 0, sizeof(_ids));
7276
memset(_cbs, 0, sizeof(_cbs));
@@ -76,7 +80,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
7680

7781
_esp.sigio(this, &ESP8266Interface::event);
7882
_esp.set_timeout();
79-
_esp.attach_int(this, &ESP8266Interface::update_conn_state_cb);
83+
_esp.attach(this, &ESP8266Interface::update_conn_state_cb);
8084
}
8185

8286
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
@@ -579,12 +583,12 @@ void ESP8266Interface::event()
579583

580584
void ESP8266Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
581585
{
582-
_esp.attach(status_cb);
586+
_conn_stat_cb = status_cb;
583587
}
584588

585589
nsapi_connection_status_t ESP8266Interface::get_connection_status() const
586590
{
587-
return _esp.connection_status();
591+
return _conn_stat;
588592
}
589593

590594
#if MBED_CONF_ESP8266_PROVIDE_DEFAULT
@@ -598,7 +602,9 @@ WiFiInterface *WiFiInterface::get_default_instance() {
598602

599603
void ESP8266Interface::update_conn_state_cb()
600604
{
601-
switch(_esp.connection_status()) {
605+
_conn_stat = _esp.connection_status();
606+
607+
switch(_conn_stat) {
602608
// Doesn't require changes
603609
case NSAPI_STATUS_CONNECTING:
604610
case NSAPI_STATUS_GLOBAL_UP:
@@ -612,6 +618,11 @@ void ESP8266Interface::update_conn_state_cb()
612618
case NSAPI_STATUS_LOCAL_UP:
613619
case NSAPI_STATUS_ERROR_UNSUPPORTED:
614620
default:
615-
break;
621+
MBED_ASSERT(false);
622+
}
623+
624+
// Inform upper layers
625+
if (_conn_stat_cb) {
626+
_conn_stat_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
616627
}
617628
}

ESP8266Interface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
343343
void (*callback)(void *);
344344
void *data;
345345
} _cbs[ESP8266_SOCKET_COUNT];
346+
347+
// Connection state reporting
348+
nsapi_connection_status_t _conn_stat;
349+
Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb; // Application registered
346350
};
347351

348352
#endif

0 commit comments

Comments
 (0)