Skip to content

Commit 7d3621d

Browse files
author
Veijo Pesonen
committed
ESP8266: reduces SIGIO signaled to the upper layers
1 parent a4ed473 commit 7d3621d

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "platform/Callback.h"
2929
#include "platform/mbed_debug.h"
3030
#include "platform/mbed_wait_api.h"
31-
#include "Kernel.h"
3231

3332
#ifndef MBED_CONF_ESP8266_DEBUG
3433
#define MBED_CONF_ESP8266_DEBUG false
@@ -495,6 +494,10 @@ int ESP8266Interface::socket_close(void *handle)
495494
err = NSAPI_ERROR_DEVICE_ERROR;
496495
}
497496

497+
_cbs[socket->id].callback = NULL;
498+
_cbs[socket->id].data = NULL;
499+
_cbs[socket->id].deferred = false;
500+
498501
socket->connected = false;
499502
_sock_i[socket->id].open = false;
500503
_sock_i[socket->id].sport = 0;
@@ -573,15 +576,12 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
573576
return socket->proto == NSAPI_TCP ? 0 : NSAPI_ERROR_UNSUPPORTED;
574577
}
575578

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));
579+
status = _esp.send(socket->id, data, size);
581580

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));
581+
if (status == NSAPI_ERROR_WOULD_BLOCK && !_cbs[socket->id].deferred && socket->proto == NSAPI_TCP) {
582+
tr_debug("Postponing SIGIO from the device");
583+
_cbs[socket->id].deferred = true;
584+
_global_event_queue->call_in(50, callback(this, &ESP8266Interface::event_deferred));
585585
} else if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_UDP) {
586586
status = NSAPI_ERROR_DEVICE_ERROR;
587587
}
@@ -731,6 +731,16 @@ void ESP8266Interface::event()
731731
}
732732
}
733733

734+
void ESP8266Interface::event_deferred()
735+
{
736+
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
737+
if (_cbs[i].deferred && _cbs[i].callback) {
738+
_cbs[i].deferred = false;
739+
_cbs[i].callback(_cbs[i].data);
740+
}
741+
}
742+
}
743+
734744
void ESP8266Interface::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
735745
{
736746
_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+
bool 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)