Skip to content

Commit 5a163b1

Browse files
michalpasztamobicaccli8
authored andcommitted
ESP8266: accept partial writes for TCP and clean up code
1 parent 89e2406 commit 5a163b1

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;
@@ -1225,10 +1226,7 @@ void ESP8266::_oob_socket0_closed()
12251226
static const int id = 0;
12261227
_sock_i[id].open = false;
12271228
// Closed, so this socket escapes from SEND FAIL status
1228-
if (id == _sock_sending_id) {
1229-
_sock_sending_id = -1;
1230-
}
1231-
_sock_i[id].send_fail = false;
1229+
_clear_socket_sending(id);
12321230
tr_debug("_oob_socket0_closed(): Socket %d closed.", id);
12331231
}
12341232

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

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

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

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

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)