From 7d3621dae45e5657651997e768d37ffb0b525024 Mon Sep 17 00:00:00 2001 From: Veijo Pesonen Date: Tue, 18 Dec 2018 12:33:22 +0200 Subject: [PATCH] ESP8266: reduces SIGIO signaled to the upper layers --- .../wifi/esp8266-driver/ESP8266Interface.cpp | 28 +++++++++++++------ .../wifi/esp8266-driver/ESP8266Interface.h | 2 ++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index eea53d71a4..48644a40c8 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -28,7 +28,6 @@ #include "platform/Callback.h" #include "platform/mbed_debug.h" #include "platform/mbed_wait_api.h" -#include "Kernel.h" #ifndef MBED_CONF_ESP8266_DEBUG #define MBED_CONF_ESP8266_DEBUG false @@ -495,6 +494,10 @@ int ESP8266Interface::socket_close(void *handle) err = NSAPI_ERROR_DEVICE_ERROR; } + _cbs[socket->id].callback = NULL; + _cbs[socket->id].data = NULL; + _cbs[socket->id].deferred = false; + socket->connected = false; _sock_i[socket->id].open = false; _sock_i[socket->id].sport = 0; @@ -573,15 +576,12 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) return socket->proto == NSAPI_TCP ? 0 : NSAPI_ERROR_UNSUPPORTED; } - unsigned long int sendStartTime = rtos::Kernel::get_ms_count(); - do { - status = _esp.send(socket->id, data, size); - } while ((sendStartTime - rtos::Kernel::get_ms_count() < 50) - && (status != NSAPI_ERROR_OK)); + status = _esp.send(socket->id, data, size); - if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_TCP) { - tr_debug("ESP8266Interface::socket_send(): enqueuing the event call"); - _global_event_queue->call_in(100, callback(this, &ESP8266Interface::event)); + if (status == NSAPI_ERROR_WOULD_BLOCK && !_cbs[socket->id].deferred && socket->proto == NSAPI_TCP) { + tr_debug("Postponing SIGIO from the device"); + _cbs[socket->id].deferred = true; + _global_event_queue->call_in(50, callback(this, &ESP8266Interface::event_deferred)); } else if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_UDP) { status = NSAPI_ERROR_DEVICE_ERROR; } @@ -731,6 +731,16 @@ void ESP8266Interface::event() } } +void ESP8266Interface::event_deferred() +{ + for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) { + if (_cbs[i].deferred && _cbs[i].callback) { + _cbs[i].deferred = false; + _cbs[i].callback(_cbs[i].data); + } + } +} + void ESP8266Interface::attach(Callback status_cb) { _conn_stat_cb = status_cb; diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index f123cf27cd..580d358158 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.h +++ b/components/wifi/esp8266-driver/ESP8266Interface.h @@ -373,8 +373,10 @@ private: struct { void (*callback)(void *); void *data; + bool deferred; } _cbs[ESP8266_SOCKET_COUNT]; void event(); + void event_deferred(); // Connection state reporting to application nsapi_connection_status_t _conn_stat;