Skip to content

Commit 1a8854c

Browse files
Marcus ChangVeijo Pesonen
authored andcommitted
Prevent unnecessary polling in passive receive mode
socket_recv calls are passed all the way to the ESP8266 regardless of data availability. With this change the data availability variable is checked before sending the receive AT command. The return value is also changed so that the caller will know to wait for an event before calling again in non-blocking mode.
1 parent 5da2fe7 commit 1a8854c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

ESP8266/ESP8266.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
3535
: _sdk_v(-1, -1, -1),
3636
_at_v(-1, -1, -1),
3737
_tcp_passive(false),
38+
_callback(0),
3839
_serial(tx, rx, ESP8266_DEFAULT_BAUD_RATE),
3940
_serial_rts(rts),
4041
_serial_cts(cts),
@@ -560,7 +561,12 @@ void ESP8266::_oob_packet_hdlr()
560561

561562
if(_tcp_passive && _sock_i[id].open == true && _sock_i[id].proto == NSAPI_TCP) {
562563
if (_parser.recv("%d\n", &amount)) {
563-
_sock_i[id].tcp_data_avbl = amount; // Not used but stored for the sake of visibility
564+
_sock_i[id].tcp_data_avbl = amount;
565+
566+
// notify data is available
567+
if (_callback) {
568+
_callback();
569+
}
564570
}
565571
return;
566572
} else if (!_parser.recv("%d:", &amount)) {
@@ -617,6 +623,11 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
617623
{
618624
int32_t ret;
619625

626+
// return immediately if no data is available
627+
if (_sock_i[id].tcp_data_avbl == 0) {
628+
return NSAPI_ERROR_WOULD_BLOCK;
629+
}
630+
620631
_smutex.lock();
621632

622633
_sock_i[id].tcp_data = (char*)data;
@@ -823,6 +834,7 @@ bool ESP8266::writeable()
823834
void ESP8266::sigio(Callback<void()> func)
824835
{
825836
_serial.sigio(func);
837+
_callback = func;
826838
}
827839

828840
void ESP8266::attach(Callback<void()> status_cb)

ESP8266/ESP8266.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ class ESP8266 {
372372
// FW version specific settings and functionalities
373373
bool _tcp_passive;
374374
int32_t _recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout);
375+
mbed::Callback<void()> _callback;
375376

376377
// UART settings
377378
mbed::UARTSerial _serial;

0 commit comments

Comments
 (0)