Skip to content

Commit ea2f36e

Browse files
ESP8266: accept partial writes for TCP and clean up code
1 parent 4405ab4 commit ea2f36e

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,14 @@ nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount)
697697
} else if (_sock_i[id].proto == NSAPI_UDP) {
698698
ret = NSAPI_ERROR_NO_MEMORY;
699699
}
700-
goto END;
701-
} else if (bytes_confirmed != amount) {
700+
} else if (bytes_confirmed != amount && _sock_i[id].proto == NSAPI_UDP) {
702701
tr_debug("send(): Error: confirmed %d bytes, but expected %d.", bytes_confirmed, amount);
703702
ret = NSAPI_ERROR_DEVICE_ERROR;
704-
goto END;
703+
} else {
704+
// TCP can accept partial writes (if they ever happen)
705+
ret = bytes_confirmed;
705706
}
706707

707-
ret = amount;
708-
709708
END:
710709
_process_oob(ESP8266_RECV_TIMEOUT, true); // Drain USART receive register to avoid data overrun
711710

@@ -1009,6 +1008,14 @@ void ESP8266::_clear_socket_packets(int id)
10091008
}
10101009
}
10111010

1011+
void ESP8266::_clear_socket_sending(int id)
1012+
{
1013+
if (id == _sock_sending_id) {
1014+
_sock_sending_id = -1;
1015+
}
1016+
_sock_i[id].send_fail = false;
1017+
}
1018+
10121019
bool ESP8266::close(int id)
10131020
{
10141021
//May take a second try if device is busy
@@ -1021,10 +1028,7 @@ bool ESP8266::close(int id)
10211028
_sock_i[id].open = false;
10221029
_clear_socket_packets(id);
10231030
// Closed, so this socket escapes from SEND FAIL status.
1024-
if (id == _sock_sending_id) {
1025-
_sock_sending_id = -1;
1026-
}
1027-
_sock_i[id].send_fail = false;
1031+
_clear_socket_sending(id);
10281032
_smutex.unlock();
10291033
// ESP8266 has a habit that it might close a socket on its own.
10301034
tr_debug("close(%d): socket close OK with UNLINK ERROR", id);
@@ -1034,10 +1038,7 @@ bool ESP8266::close(int id)
10341038
// _sock_i[id].open set to false with an OOB
10351039
_clear_socket_packets(id);
10361040
// Closed, so this socket escapes from SEND FAIL status
1037-
if (id == _sock_sending_id) {
1038-
_sock_sending_id = -1;
1039-
}
1040-
_sock_i[id].send_fail = false;
1041+
_clear_socket_sending(id);
10411042
_smutex.unlock();
10421043
tr_debug("close(%d): socket close OK with AT+CIPCLOSE OK", id);
10431044
return true;
@@ -1224,10 +1225,7 @@ void ESP8266::_oob_socket0_closed()
12241225
static const int id = 0;
12251226
_sock_i[id].open = false;
12261227
// Closed, so this socket escapes from SEND FAIL status
1227-
if (id == _sock_sending_id) {
1228-
_sock_sending_id = -1;
1229-
}
1230-
_sock_i[id].send_fail = false;
1228+
_clear_socket_sending(id);
12311229
tr_debug("_oob_socket0_closed(): Socket %d closed.", id);
12321230
}
12331231

@@ -1236,34 +1234,23 @@ void ESP8266::_oob_socket1_closed()
12361234
static const int id = 1;
12371235
_sock_i[id].open = false;
12381236
// Closed, so this socket escapes from SEND FAIL status
1239-
if (id == _sock_sending_id) {
1240-
_sock_sending_id = -1;
1241-
}
1242-
_sock_i[id].send_fail = false;
1237+
_clear_socket_sending(id);
12431238
tr_debug("_oob_socket1_closed(): Socket %d closed.", id);
12441239
}
12451240

12461241
void ESP8266::_oob_socket2_closed()
12471242
{
12481243
static const int id = 2;
12491244
_sock_i[id].open = false;
1250-
// Closed, so this socket escapes from SEND FAIL status
1251-
if (id == _sock_sending_id) {
1252-
_sock_sending_id = -1;
1253-
}
1254-
_sock_i[id].send_fail = false;
1245+
_clear_socket_sending(id);
12551246
tr_debug("_oob_socket2_closed(): Socket %d closed.", id);
12561247
}
12571248

12581249
void ESP8266::_oob_socket3_closed()
12591250
{
12601251
static const int id = 3;
12611252
_sock_i[id].open = false;
1262-
// Closed, so this socket escapes from SEND FAIL status
1263-
if (id == _sock_sending_id) {
1264-
_sock_sending_id = -1;
1265-
}
1266-
_sock_i[id].send_fail = false;
1253+
_clear_socket_sending(id);
12671254
tr_debug("_oob_socket3_closed(): %d closed.", id);
12681255
}
12691256

@@ -1272,10 +1259,7 @@ void ESP8266::_oob_socket4_closed()
12721259
static const int id = 4;
12731260
_sock_i[id].open = false;
12741261
// Closed, so this socket escapes from SEND FAIL status
1275-
if (id == _sock_sending_id) {
1276-
_sock_sending_id = -1;
1277-
}
1278-
_sock_i[id].send_fail = false;
1262+
_clear_socket_sending(id);
12791263
tr_debug("_oob_socket0_closed(): Socket %d closed.", id);
12801264
}
12811265

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ class ESP8266 {
444444
// data follows
445445
} *_packets, * *_packets_end;
446446
void _clear_socket_packets(int id);
447+
void _clear_socket_sending(int id);
447448
int _sock_active_id;
448449

449450
// Memory statistics

0 commit comments

Comments
 (0)