@@ -58,8 +58,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
5858 _error(false ),
5959 _busy(false ),
6060 _reset_done(false ),
61- _prev_send_ok_pending(false ),
62- _send_fail_received(false ),
61+ _send_status(SEND_STATUS_OK),
6362 _conn_status(NSAPI_STATUS_DISCONNECTED)
6463{
6564 _serial.set_baud (MBED_CONF_ESP8266_SERIAL_BAUDRATE);
@@ -290,6 +289,9 @@ bool ESP8266::reset(void)
290289 tr_debug (" reset(): Done: %s." , done ? " OK" : " FAIL" );
291290
292291 _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" );
293295 set_timeout ();
294296 _smutex.unlock ();
295297
@@ -616,13 +618,18 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
616618
617619nsapi_size_or_error_t ESP8266::send (int id, const void *data, uint32_t amount)
618620{
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+ }
622630 }
623631
624632 nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
625- _send_fail_received = false ;
626633 int bytes_confirmed = 0 ;
627634 constexpr unsigned int send_ack_retries = 3 ;
628635
@@ -666,7 +673,6 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
666673 // The "Recv X bytes" is not documented.
667674 if (!_parser.recv (" Recv %d bytes" , &bytes_confirmed)) {
668675 tr_debug (" send(): Bytes not confirmed." );
669- ret = NSAPI_ERROR_DEVICE_ERROR;
670676 if (_sock_i[id].proto == NSAPI_TCP) {
671677 ret = NSAPI_ERROR_WOULD_BLOCK;
672678 } 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)
683689 _parser.oob (" SEND FAIL" , callback (this , &ESP8266::_oob_send_fail_received));
684690 for (unsigned int i = send_ack_retries; i > 0 ; i--) {
685691 if (!_parser.recv (" SEND OK" )) {
686- if (_error || _send_fail_received ) {
692+ if (_error || _send_status == SEND_STATUS_FAILED ) {
687693 _parser.remove_oob (" SEND FAIL" );
688694 goto END;
689695 }
690696 if (_busy) {
691697 _busy = false ;
692698 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 );
695699 }
696700 } else {
697701 ret = amount; // Got "SEND OK" - return number of bytes.
698702 goto END;
699703 }
700704 }
701705
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 ;
704708 _parser.oob (" SEND OK" , callback (this , &ESP8266::_oob_send_ok_received));
705709 ret = amount;
706710
@@ -718,7 +722,7 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
718722 tr_debug (" send(): Connection disrupted." );
719723 }
720724
721- if (_send_fail_received ) {
725+ if (_send_status == SEND_STATUS_FAILED ) {
722726 if (_sock_i[id].proto == NSAPI_TCP) {
723727 ret = NSAPI_ERROR_DEVICE_ERROR;
724728 } else {
@@ -1001,6 +1005,14 @@ void ESP8266::_clear_socket_packets(int id)
10011005 _sock_i[id].tcp_data_avbl = 0 ;
10021006 }
10031007}
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+ }
10041016
10051017bool ESP8266::close (int id)
10061018{
@@ -1204,34 +1216,39 @@ void ESP8266::_oob_socket0_closed()
12041216{
12051217 static const int id = 0 ;
12061218 _sock_i[id].open = false ;
1219+ _clear_send_status ();
12071220 tr_debug (" _oob_socket0_closed(): Socket %d closed." , id);
12081221}
12091222
12101223void ESP8266::_oob_socket1_closed ()
12111224{
12121225 static const int id = 1 ;
12131226 _sock_i[id].open = false ;
1227+ _clear_send_status ();
12141228 tr_debug (" _oob_socket1_closed(): Socket %d closed." , id);
12151229}
12161230
12171231void ESP8266::_oob_socket2_closed ()
12181232{
12191233 static const int id = 2 ;
12201234 _sock_i[id].open = false ;
1235+ _clear_send_status ();
12211236 tr_debug (" _oob_socket2_closed(): Socket %d closed." , id);
12221237}
12231238
12241239void ESP8266::_oob_socket3_closed ()
12251240{
12261241 static const int id = 3 ;
12271242 _sock_i[id].open = false ;
1243+ _clear_send_status ();
12281244 tr_debug (" _oob_socket3_closed(): %d closed." , id);
12291245}
12301246
12311247void ESP8266::_oob_socket4_closed ()
12321248{
12331249 static const int id = 4 ;
12341250 _sock_i[id].open = false ;
1251+ _clear_send_status ();
12351252 tr_debug (" _oob_socket0_closed(): Socket %d closed." , id);
12361253}
12371254
@@ -1271,14 +1288,22 @@ void ESP8266::_oob_connection_status()
12711288
12721289void ESP8266::_oob_send_ok_received ()
12731290{
1274- tr_debug (" _oob_send_ok_received called" );
1275- _prev_send_ok_pending = false ;
1291+ tr_debug (" _oob_send_ok_received called with _send_status %d" , _send_status);
1292+ if (_send_status == SEND_STATUS_PENDING) {
1293+ _send_status = SEND_STATUS_OK;
1294+ }
1295+ _parser.remove_oob (" SEND OK" );
1296+ _parser.remove_oob (" SEND FAIL" );
12761297}
12771298
12781299void ESP8266::_oob_send_fail_received ()
12791300{
1280- tr_debug (" _oob_send_fail_received called" );
1281- _send_fail_received = true ;
1301+ tr_debug (" _oob_send_fail_received called with _send_status %d" , _send_status);
1302+ if (_send_status == SEND_STATUS_PENDING) {
1303+ _send_status = SEND_STATUS_FAILED;
1304+ }
1305+ _parser.remove_oob (" SEND FAIL" );
1306+ _parser.remove_oob (" SEND OK" );
12821307}
12831308
12841309int8_t ESP8266::default_wifi_mode ()
0 commit comments