26
26
#include " features/netsocket/nsapi_types.h"
27
27
#include " mbed_trace.h"
28
28
#include " platform/Callback.h"
29
+ #include " platform/mbed_critical.h"
29
30
#include " platform/mbed_debug.h"
30
31
#include " platform/mbed_wait_api.h"
31
- #include " Kernel.h"
32
32
33
33
#ifndef MBED_CONF_ESP8266_DEBUG
34
34
#define MBED_CONF_ESP8266_DEBUG false
@@ -495,6 +495,10 @@ int ESP8266Interface::socket_close(void *handle)
495
495
err = NSAPI_ERROR_DEVICE_ERROR;
496
496
}
497
497
498
+ _cbs[socket->id ].callback = NULL ;
499
+ _cbs[socket->id ].data = NULL ;
500
+ core_util_atomic_store_u8 (&_cbs[socket->id ].deferred , false );
501
+
498
502
socket->connected = false ;
499
503
_sock_i[socket->id ].open = false ;
500
504
_sock_i[socket->id ].sport = 0 ;
@@ -563,6 +567,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
563
567
{
564
568
nsapi_error_t status;
565
569
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
570
+ uint8_t expect_false = false ;
566
571
567
572
if (!socket) {
568
573
return NSAPI_ERROR_NO_SOCKET;
@@ -573,15 +578,13 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
573
578
return socket->proto == NSAPI_TCP ? 0 : NSAPI_ERROR_UNSUPPORTED;
574
579
}
575
580
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);
581
582
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));
585
588
} else if (status == NSAPI_ERROR_WOULD_BLOCK && socket->proto == NSAPI_UDP) {
586
589
status = NSAPI_ERROR_DEVICE_ERROR;
587
590
}
@@ -731,6 +734,16 @@ void ESP8266Interface::event()
731
734
}
732
735
}
733
736
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
+
734
747
void ESP8266Interface::attach (Callback<void (nsapi_event_t , intptr_t )> status_cb)
735
748
{
736
749
_conn_stat_cb = status_cb;
0 commit comments