@@ -58,8 +58,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
58
58
_error(false ),
59
59
_busy(false ),
60
60
_reset_done(false ),
61
- _prev_send_ok_pending(false ),
62
- _send_fail_received(false ),
61
+ _send_status(SEND_STATUS_OK),
63
62
_conn_status(NSAPI_STATUS_DISCONNECTED)
64
63
{
65
64
_serial.set_baud (MBED_CONF_ESP8266_SERIAL_BAUDRATE);
@@ -290,6 +289,9 @@ bool ESP8266::reset(void)
290
289
tr_debug (" reset(): Done: %s." , done ? " OK" : " FAIL" );
291
290
292
291
_clear_socket_packets (ESP8266_ALL_SOCKET_IDS);
292
+ _send_status = SEND_STATUS_OK;
293
+ _parser.remove_oob (" SEND OK" );
294
+ _parser.remove_oob (" SEND FAIL" );
293
295
set_timeout ();
294
296
_smutex.unlock ();
295
297
@@ -616,13 +618,18 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
616
618
617
619
nsapi_size_or_error_t ESP8266::send (int id, const void *data, uint32_t amount)
618
620
{
619
- if (_prev_send_ok_pending && _sock_i[id].proto == NSAPI_TCP) {
620
- tr_debug (" send(): Previous packet was not ACK-ed with SEND OK." );
621
- return NSAPI_ERROR_WOULD_BLOCK;
621
+ if (_sock_i[id].proto == NSAPI_TCP) {
622
+ if (_send_status == SEND_STATUS_PENDING) {
623
+ tr_debug (" send(): Previous packet was not yet ACK-ed with SEND OK." );
624
+ return NSAPI_ERROR_WOULD_BLOCK;
625
+ } else if (_send_status == SEND_STATUS_FAILED) {
626
+ tr_debug (" send(): Previous packet failed." );
627
+ _send_status = SEND_STATUS_OK;
628
+ return NSAPI_ERROR_DEVICE_ERROR;
629
+ }
622
630
}
623
631
624
632
nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
625
- _send_fail_received = false ;
626
633
int bytes_confirmed = 0 ;
627
634
constexpr unsigned int send_ack_retries = 3 ;
628
635
@@ -666,7 +673,6 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
666
673
// The "Recv X bytes" is not documented.
667
674
if (!_parser.recv (" Recv %d bytes" , &bytes_confirmed)) {
668
675
tr_debug (" send(): Bytes not confirmed." );
669
- ret = NSAPI_ERROR_DEVICE_ERROR;
670
676
if (_sock_i[id].proto == NSAPI_TCP) {
671
677
ret = NSAPI_ERROR_WOULD_BLOCK;
672
678
} else if (_sock_i[id].proto == NSAPI_UDP) {
@@ -683,24 +689,22 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
683
689
_parser.oob (" SEND FAIL" , callback (this , &ESP8266::_oob_send_fail_received));
684
690
for (unsigned int i = send_ack_retries; i > 0 ; i--) {
685
691
if (!_parser.recv (" SEND OK" )) {
686
- if (_error || _send_fail_received ) {
692
+ if (_error || _send_status == SEND_STATUS_FAILED ) {
687
693
_parser.remove_oob (" SEND FAIL" );
688
694
goto END;
689
695
}
690
696
if (_busy) {
691
697
_busy = false ;
692
698
tr_debug (" send(): Busy, %d retries left..." , i - 1 );
693
- } else {
694
- tr_debug (" send(): Not busy, but no SEND OK. %d retries left..." , i - 1 );
695
699
}
696
700
} else {
697
701
ret = amount; // Got "SEND OK" - return number of bytes.
698
702
goto END;
699
703
}
700
704
}
701
705
702
- // ESP8266 ACKed data over serial, but did not ACK over TCP or report any error.
703
- _prev_send_ok_pending = true ;
706
+ // ESP8266 ACKed data over serial, but did not ACK with SEND OK or report any error.
707
+ _send_status = SEND_STATUS_PENDING ;
704
708
_parser.oob (" SEND OK" , callback (this , &ESP8266::_oob_send_ok_received));
705
709
ret = amount;
706
710
@@ -718,7 +722,7 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
718
722
tr_debug (" send(): Connection disrupted." );
719
723
}
720
724
721
- if (_send_fail_received ) {
725
+ if (_send_status == SEND_STATUS_FAILED ) {
722
726
if (_sock_i[id].proto == NSAPI_TCP) {
723
727
ret = NSAPI_ERROR_DEVICE_ERROR;
724
728
} else {
@@ -1001,6 +1005,14 @@ void ESP8266::_clear_socket_packets(int id)
1001
1005
_sock_i[id].tcp_data_avbl = 0 ;
1002
1006
}
1003
1007
}
1008
+ void ESP8266::_clear_send_status (void )
1009
+ {
1010
+ _smutex.lock (); // remove_oob doesn't use serial, but we don't want to race against it.
1011
+ _send_status = SEND_STATUS_OK;
1012
+ _parser.remove_oob (" SEND OK" );
1013
+ _parser.remove_oob (" SEND FAIL" );
1014
+ _smutex.unlock ();
1015
+ }
1004
1016
1005
1017
bool ESP8266::close (int id)
1006
1018
{
@@ -1203,34 +1215,39 @@ void ESP8266::_oob_socket0_closed()
1203
1215
{
1204
1216
static const int id = 0 ;
1205
1217
_sock_i[id].open = false ;
1218
+ _clear_send_status ();
1206
1219
tr_debug (" _oob_socket0_closed(): Socket %d closed." , id);
1207
1220
}
1208
1221
1209
1222
void ESP8266::_oob_socket1_closed ()
1210
1223
{
1211
1224
static const int id = 1 ;
1212
1225
_sock_i[id].open = false ;
1226
+ _clear_send_status ();
1213
1227
tr_debug (" _oob_socket1_closed(): Socket %d closed." , id);
1214
1228
}
1215
1229
1216
1230
void ESP8266::_oob_socket2_closed ()
1217
1231
{
1218
1232
static const int id = 2 ;
1219
1233
_sock_i[id].open = false ;
1234
+ _clear_send_status ();
1220
1235
tr_debug (" _oob_socket2_closed(): Socket %d closed." , id);
1221
1236
}
1222
1237
1223
1238
void ESP8266::_oob_socket3_closed ()
1224
1239
{
1225
1240
static const int id = 3 ;
1226
1241
_sock_i[id].open = false ;
1242
+ _clear_send_status ();
1227
1243
tr_debug (" _oob_socket3_closed(): %d closed." , id);
1228
1244
}
1229
1245
1230
1246
void ESP8266::_oob_socket4_closed ()
1231
1247
{
1232
1248
static const int id = 4 ;
1233
1249
_sock_i[id].open = false ;
1250
+ _clear_send_status ();
1234
1251
tr_debug (" _oob_socket0_closed(): Socket %d closed." , id);
1235
1252
}
1236
1253
@@ -1270,14 +1287,22 @@ void ESP8266::_oob_connection_status()
1270
1287
1271
1288
void ESP8266::_oob_send_ok_received ()
1272
1289
{
1273
- tr_debug (" _oob_send_ok_received called" );
1274
- _prev_send_ok_pending = false ;
1290
+ tr_debug (" _oob_send_ok_received called with _send_status %d" , _send_status);
1291
+ if (_send_status == SEND_STATUS_PENDING) {
1292
+ _send_status = SEND_STATUS_OK;
1293
+ }
1294
+ _parser.remove_oob (" SEND OK" );
1295
+ _parser.remove_oob (" SEND FAIL" );
1275
1296
}
1276
1297
1277
1298
void ESP8266::_oob_send_fail_received ()
1278
1299
{
1279
- tr_debug (" _oob_send_fail_received called" );
1280
- _send_fail_received = true ;
1300
+ tr_debug (" _oob_send_fail_received called with _send_status %d" , _send_status);
1301
+ if (_send_status == SEND_STATUS_PENDING) {
1302
+ _send_status = SEND_STATUS_FAILED;
1303
+ }
1304
+ _parser.remove_oob (" SEND FAIL" );
1305
+ _parser.remove_oob (" SEND OK" );
1281
1306
}
1282
1307
1283
1308
int8_t ESP8266::default_wifi_mode ()
0 commit comments