Skip to content

ESP8266: reduces SIGIO signaled to the upper layers #9520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include "features/netsocket/nsapi_types.h"
#include "mbed_trace.h"
#include "platform/Callback.h"
#include "platform/mbed_critical.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
Expand Down Expand Up @@ -495,6 +495,10 @@ int ESP8266Interface::socket_close(void *handle)
err = NSAPI_ERROR_DEVICE_ERROR;
}

_cbs[socket->id].callback = NULL;
_cbs[socket->id].data = NULL;
core_util_atomic_store_u8(&_cbs[socket->id].deferred, false);

socket->connected = false;
_sock_i[socket->id].open = false;
_sock_i[socket->id].sport = 0;
Expand Down Expand Up @@ -563,6 +567,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
{
nsapi_error_t status;
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
uint8_t expect_false = false;

if (!socket) {
return NSAPI_ERROR_NO_SOCKET;
Expand All @@ -573,15 +578,13 @@ 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
&& socket->proto == NSAPI_TCP
&& core_util_atomic_cas_u8(&_cbs[socket->id].deferred, &expect_false, true)) {
tr_debug("Postponing SIGIO from the device");
_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;
}
Expand Down Expand Up @@ -731,6 +734,16 @@ void ESP8266Interface::event()
}
}

void ESP8266Interface::event_deferred()
{
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
uint8_t expect_true = true;
if (core_util_atomic_cas_u8(&_cbs[i].deferred, &expect_true, false) && _cbs[i].callback) {
_cbs[i].callback(_cbs[i].data);
}
}
}

void ESP8266Interface::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_conn_stat_cb = status_cb;
Expand Down
2 changes: 2 additions & 0 deletions components/wifi/esp8266-driver/ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
struct {
void (*callback)(void *);
void *data;
uint8_t deferred;
} _cbs[ESP8266_SOCKET_COUNT];
void event();
void event_deferred();

// Connection state reporting to application
nsapi_connection_status_t _conn_stat;
Expand Down