diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp index 26bfd1b6c8..734f53d498 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp @@ -697,15 +697,14 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount) } else if (_sock_i[id].proto == NSAPI_UDP) { ret = NSAPI_ERROR_NO_MEMORY; } - goto END; - } else if (bytes_confirmed != amount) { + } else if (bytes_confirmed != amount && _sock_i[id].proto == NSAPI_UDP) { tr_debug("send(): Error: confirmed %d bytes, but expected %d.", bytes_confirmed, amount); ret = NSAPI_ERROR_DEVICE_ERROR; - goto END; + } else { + // TCP can accept partial writes (if they ever happen) + ret = bytes_confirmed; } - ret = amount; - END: _process_oob(ESP8266_RECV_TIMEOUT, true); // Drain USART receive register to avoid data overrun @@ -1009,6 +1008,14 @@ void ESP8266::_clear_socket_packets(int id) } } +void ESP8266::_clear_socket_sending(int id) +{ + if (id == _sock_sending_id) { + _sock_sending_id = -1; + } + _sock_i[id].send_fail = false; +} + bool ESP8266::close(int id) { //May take a second try if device is busy @@ -1021,10 +1028,7 @@ bool ESP8266::close(int id) _sock_i[id].open = false; _clear_socket_packets(id); // Closed, so this socket escapes from SEND FAIL status. - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); _smutex.unlock(); // ESP8266 has a habit that it might close a socket on its own. tr_debug("close(%d): socket close OK with UNLINK ERROR", id); @@ -1034,10 +1038,7 @@ bool ESP8266::close(int id) // _sock_i[id].open set to false with an OOB _clear_socket_packets(id); // Closed, so this socket escapes from SEND FAIL status - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); _smutex.unlock(); tr_debug("close(%d): socket close OK with AT+CIPCLOSE OK", id); return true; @@ -1224,10 +1225,7 @@ void ESP8266::_oob_socket0_closed() static const int id = 0; _sock_i[id].open = false; // Closed, so this socket escapes from SEND FAIL status - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); tr_debug("_oob_socket0_closed(): Socket %d closed.", id); } @@ -1236,10 +1234,7 @@ void ESP8266::_oob_socket1_closed() static const int id = 1; _sock_i[id].open = false; // Closed, so this socket escapes from SEND FAIL status - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); tr_debug("_oob_socket1_closed(): Socket %d closed.", id); } @@ -1247,11 +1242,7 @@ void ESP8266::_oob_socket2_closed() { static const int id = 2; _sock_i[id].open = false; - // Closed, so this socket escapes from SEND FAIL status - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); tr_debug("_oob_socket2_closed(): Socket %d closed.", id); } @@ -1259,11 +1250,7 @@ void ESP8266::_oob_socket3_closed() { static const int id = 3; _sock_i[id].open = false; - // Closed, so this socket escapes from SEND FAIL status - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); tr_debug("_oob_socket3_closed(): %d closed.", id); } @@ -1272,10 +1259,7 @@ void ESP8266::_oob_socket4_closed() static const int id = 4; _sock_i[id].open = false; // Closed, so this socket escapes from SEND FAIL status - if (id == _sock_sending_id) { - _sock_sending_id = -1; - } - _sock_i[id].send_fail = false; + _clear_socket_sending(id); tr_debug("_oob_socket0_closed(): Socket %d closed.", id); } diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.h b/components/wifi/esp8266-driver/ESP8266/ESP8266.h index 249c11e989..9c865af743 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.h +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.h @@ -444,6 +444,7 @@ private: // data follows } *_packets, * *_packets_end; void _clear_socket_packets(int id); + void _clear_socket_sending(int id); int _sock_active_id; // Memory statistics