Skip to content

Commit f1bf5db

Browse files
authored
Merge pull request #9520 from VeijoPesonen/bugfix-esp8266_reduce_sigio
ESP8266: reduces SIGIO signaled to the upper layers
2 parents 7ba83a5 + 53c2879 commit f1bf5db

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#include "features/netsocket/nsapi_types.h"
2727
#include "mbed_trace.h"
2828
#include "platform/Callback.h"
29+
#include "platform/mbed_critical.h"
2930
#include "platform/mbed_debug.h"
3031
#include "platform/mbed_wait_api.h"
31-
#include "Kernel.h"
3232

3333
#ifndef MBED_CONF_ESP8266_DEBUG
3434
#define MBED_CONF_ESP8266_DEBUG false
@@ -495,6 +495,10 @@ int ESP8266Interface::socket_close(void *handle)
495495
err = NSAPI_ERROR_DEVICE_ERROR;
496496
}
497497

498+
_cbs[socket->id].callback = NULL;
499+
_cbs[socket->id].data = NULL;
500+
core_util_atomic_store_u8(&_cbs[socket->id].deferred, false);
501+
498502
socket->connected = false;
499503
_sock_i[socket->id].open = false;
500504
_sock_i[socket->id].sport = 0;
@@ -563,6 +567,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
563567
{
564568
nsapi_error_t status;
565569
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
570+
uint8_t expect_false = false;
566571

567572
if (!socket) {
568573
return NSAPI_ERROR_NO_SOCKET;
@@ -573,15 +578,13 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
573578
return socket->proto == NSAPI_TCP ? 0 : NSAPI_ERROR_UNSUPPORTED;
574579
}
575580

576-
unsigned long int sendStartTime = rtos::Kernel::get_ms_count();
577-
do {
578-
status = _esp.send(socket->id, data, size);
579-
} while ((sendStartTime - rtos::Kernel::get_ms_count() < 50)
580-
&& (status != NSAPI_ERROR_OK));
581+
status = _esp.send(socket->id, data, size);
581582

582-
if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_TCP) {
583-
tr_debug("ESP8266Interface::socket_send(): enqueuing the event call");
584-
_global_event_queue->call_in(100, callback(this, &ESP8266Interface::event));
583+
if (status == NSAPI_ERROR_WOULD_BLOCK
584+
&& socket->proto == NSAPI_TCP
585+
&& core_util_atomic_cas_u8(&_cbs[socket->id].deferred, &expect_false, true)) {
586+
tr_debug("Postponing SIGIO from the device");
587+
_global_event_queue->call_in(50, callback(this, &ESP8266Interface::event_deferred));
585588
} else if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_UDP) {
586589
status = NSAPI_ERROR_DEVICE_ERROR;
587590
}
@@ -731,6 +734,16 @@ void ESP8266Interface::event()
731734
}
732735
}
733736

737+
void ESP8266Interface::event_deferred()
738+
{
739+
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
740+
uint8_t expect_true = true;
741+
if (core_util_atomic_cas_u8(&_cbs[i].deferred, &expect_true, false) && _cbs[i].callback) {
742+
_cbs[i].callback(_cbs[i].data);
743+
}
744+
}
745+
}
746+
734747
void ESP8266Interface::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
735748
{
736749
_conn_stat_cb = status_cb;

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,10 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
373373
struct {
374374
void (*callback)(void *);
375375
void *data;
376+
uint8_t deferred;
376377
} _cbs[ESP8266_SOCKET_COUNT];
377378
void event();
379+
void event_deferred();
378380

379381
// Connection state reporting to application
380382
nsapi_connection_status_t _conn_stat;

0 commit comments

Comments
 (0)