Skip to content

Commit 979e9f3

Browse files
author
Jarno Lämsä
authored
Merge pull request #71 from ARMmbed/status-callbacks
Add support for attaching a status callback for esp8266 driver
2 parents 5a0a2cb + ceed8ee commit 979e9f3

File tree

4 files changed

+89
-12
lines changed

4 files changed

+89
-12
lines changed

ESP8266/ESP8266.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
2929
_packets_end(&_packets),
3030
_connect_error(0),
3131
_fail(false),
32-
_socket_open()
32+
_socket_open(),
33+
_connection_status(NSAPI_STATUS_DISCONNECTED)
3334
{
3435
_serial.set_baud( ESP8266_DEFAULT_BAUD_RATE );
3536
_parser.debug_on(debug);
@@ -45,6 +46,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
4546
_parser.oob("2,CLOSED", callback(this, &ESP8266::_oob_socket2_closed_handler));
4647
_parser.oob("3,CLOSED", callback(this, &ESP8266::_oob_socket3_closed_handler));
4748
_parser.oob("4,CLOSED", callback(this, &ESP8266::_oob_socket4_closed_handler));
49+
_parser.oob("WIFI ", callback(this, &ESP8266::_connection_status_handler));
4850
}
4951

5052
int ESP8266::get_firmware_version()
@@ -122,6 +124,9 @@ nsapi_error_t ESP8266::connect(const char *ap, const char *passPhrase)
122124
{
123125
_smutex.lock();
124126
setTimeout(ESP8266_CONNECT_TIMEOUT);
127+
_connection_status = NSAPI_STATUS_CONNECTING;
128+
if(_connection_status_cb)
129+
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
125130

126131
_parser.send("AT+CWJAP_CUR=\"%s\",\"%s\"", ap, passPhrase);
127132
if (!_parser.recv("OK\n")) {
@@ -503,11 +508,16 @@ bool ESP8266::writeable()
503508
return _serial.FileHandle::writable();
504509
}
505510

506-
void ESP8266::attach(Callback<void()> func)
511+
void ESP8266::sigio(Callback<void()> func)
507512
{
508513
_serial.sigio(func);
509514
}
510515

516+
void ESP8266::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
517+
{
518+
_connection_status_cb = status_cb;
519+
}
520+
511521
bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
512522
{
513523
int sec;
@@ -562,6 +572,22 @@ void ESP8266::_oob_socket4_closed_handler()
562572
_socket_open[4] = 0;
563573
}
564574

575+
void ESP8266::_connection_status_handler()
576+
{
577+
char status[13];
578+
if (_parser.recv("%12[^\"]\n", status)) {
579+
if (strcmp(status, "GOT IP\n") == 0)
580+
_connection_status = NSAPI_STATUS_GLOBAL_UP;
581+
else if (strcmp(status, "DISCONNECT\n") == 0)
582+
_connection_status = NSAPI_STATUS_DISCONNECTED;
583+
else
584+
return;
585+
586+
if(_connection_status_cb)
587+
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
588+
}
589+
}
590+
565591
int8_t ESP8266::get_default_wifi_mode()
566592
{
567593
int8_t mode;
@@ -587,3 +613,8 @@ bool ESP8266::set_default_wifi_mode(const int8_t mode)
587613

588614
return done;
589615
}
616+
617+
nsapi_connection_status_t ESP8266::get_connection_status() const
618+
{
619+
return _connection_status;
620+
}

ESP8266/ESP8266.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,30 @@ class ESP8266
223223
bool writeable();
224224

225225
/**
226-
* Attach a function to call whenever network state has changed
226+
* Attach a function to call whenever sigio happens in the serial
227227
*
228228
* @param func A pointer to a void function, or 0 to set as none
229229
*/
230-
void attach(Callback<void()> func);
230+
void sigio(Callback<void()> func);
231231

232232
/**
233-
* Attach a function to call whenever network state has changed
233+
* Attach a function to call whenever sigio happens in the serial
234234
*
235235
* @param obj pointer to the object to call the member function on
236236
* @param method pointer to the member function to call
237237
*/
238238
template <typename T, typename M>
239-
void attach(T *obj, M method) {
240-
attach(Callback<void()>(obj, method));
239+
void sigio(T *obj, M method) {
240+
sigio(Callback<void()>(obj, method));
241241
}
242242

243+
/**
244+
* Attach a function to call whenever network state has changed
245+
*
246+
* @param func A pointer to a void function, or 0 to set as none
247+
*/
248+
void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
249+
243250
/**
244251
* Read default Wifi mode from flash
245252
*
@@ -252,6 +259,12 @@ class ESP8266
252259
*/
253260
bool set_default_wifi_mode(const int8_t mode);
254261

262+
/** Get the connection status
263+
*
264+
* @return The connection status according to ConnectionStatusType
265+
*/
266+
nsapi_connection_status_t get_connection_status() const;
267+
255268
static const int8_t WIFIMODE_STATION = 1;
256269
static const int8_t WIFIMODE_SOFTAP = 2;
257270
static const int8_t WIFIMODE_STATION_SOFTAP = 3;
@@ -276,7 +289,7 @@ class ESP8266
276289
void _oob_socket2_closed_handler();
277290
void _oob_socket3_closed_handler();
278291
void _oob_socket4_closed_handler();
279-
292+
void _connection_status_handler();
280293

281294
char _ip_buffer[16];
282295
char _gateway_buffer[16];
@@ -286,6 +299,8 @@ class ESP8266
286299
int _connect_error;
287300
bool _fail;
288301
int _socket_open[SOCKET_COUNT];
302+
nsapi_connection_status_t _connection_status;
303+
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
289304
};
290305

291306
#endif

ESP8266Interface.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
3737
memset(_local_ports, 0, sizeof(_local_ports));
3838
ap_sec = NSAPI_SECURITY_UNKNOWN;
3939

40-
_esp.attach(this, &ESP8266Interface::event);
40+
_esp.sigio(this, &ESP8266Interface::event);
4141
_esp.setTimeout();
4242
}
4343

@@ -490,10 +490,21 @@ nsapi_error_t ESP8266Interface::getsockopt(nsapi_socket_t handle, int level, int
490490
}
491491

492492

493-
void ESP8266Interface::event() {
493+
void ESP8266Interface::event()
494+
{
494495
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
495496
if (_cbs[i].callback) {
496497
_cbs[i].callback(_cbs[i].data);
497498
}
498499
}
499500
}
501+
502+
void ESP8266Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
503+
{
504+
_esp.attach(status_cb);
505+
}
506+
507+
nsapi_connection_status_t ESP8266Interface::get_connection_status() const
508+
{
509+
return _esp.get_connection_status();
510+
}

ESP8266Interface.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
147147
*/
148148
using NetworkInterface::add_dns_server;
149149

150-
/* Set socket options
150+
/** Set socket options
151151
*
152152
* The setsockopt allow an application to pass stack-specific hints
153153
* to the underlying stack. For unsupported options,
@@ -163,7 +163,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
163163
virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
164164
int optname, const void *optval, unsigned optlen);
165165

166-
/* Get socket options
166+
/** Get socket options
167167
*
168168
* getsockopt allows an application to retrieve stack-specific options
169169
* from the underlying stack using stack-specific level and option names,
@@ -181,6 +181,26 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
181181
virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname,
182182
void *optval, unsigned *optlen);
183183

184+
/** Register callback for status reporting
185+
*
186+
* The specified status callback function will be called on status changes
187+
* on the network. The parameters on the callback are the event type and
188+
* event-type dependent reason parameter.
189+
*
190+
* In ESP8266 the callback will be called when processing OOB-messages via
191+
* AT-parser. Do NOT call any ESP8266Interface -functions or do extensive
192+
* processing in the callback.
193+
*
194+
* @param status_cb The callback for status changes
195+
*/
196+
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
197+
198+
/** Get the connection status
199+
*
200+
* @return The connection status according to ConnectionStatusType
201+
*/
202+
virtual nsapi_connection_status_t get_connection_status() const;
203+
184204
protected:
185205
/** Open a socket
186206
* @param handle Handle in which to store new socket

0 commit comments

Comments
 (0)