Skip to content

Commit a13388d

Browse files
Marcus ChangVeijo Pesonen
authored andcommitted
Fix socket state freshness and data availability
When opening TCP and UDP sockets there is a check to see whether the socket is already in use or not. Because OOB messages are not handled immediately there might be a mismatch between the stored state and the actual state. This change processes any outstanding OOB messages before opening the socket. Similarly, when receiving data passively there is an internal variable for keeping track of available data on the wifi modem. This change processes any outstanding OOB messages before receiving data.
1 parent 44c955c commit a13388d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

ESP8266/ESP8266.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,16 @@ nsapi_error_t ESP8266::open_udp(int id, const char *addr, int port, int local_po
427427
static const char *type = "UDP";
428428
bool done = false;
429429

430+
_smutex.lock();
431+
432+
// process OOB so that _sock_i reflects the correct state of the socket
433+
_process_oob(ESP8266_SEND_TIMEOUT, true);
434+
430435
if (id >= SOCKET_COUNT || _sock_i[id].open) {
436+
_smutex.unlock();
431437
return NSAPI_ERROR_PARAMETER;
432438
}
433439

434-
_smutex.lock();
435-
436440
for (int i = 0; i < 2; i++) {
437441
if (local_port) {
438442
done = _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d,%d", id, type, addr, port, local_port);
@@ -477,12 +481,16 @@ nsapi_error_t ESP8266::open_tcp(int id, const char *addr, int port, int keepaliv
477481
static const char *type = "TCP";
478482
bool done = false;
479483

484+
_smutex.lock();
485+
486+
// process OOB so that _sock_i reflects the correct state of the socket
487+
_process_oob(ESP8266_SEND_TIMEOUT, true);
488+
480489
if (id >= SOCKET_COUNT || _sock_i[id].open) {
490+
_smutex.unlock();
481491
return NSAPI_ERROR_PARAMETER;
482492
}
483493

484-
_smutex.lock();
485-
486494
for (int i = 0; i < 2; i++) {
487495
if (keepalive) {
488496
done = _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d,%d", id, type, addr, port, keepalive);
@@ -655,6 +663,15 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
655663
_sock_i[id].tcp_data = NULL;
656664
_sock_active_id = -1;
657665

666+
// update internal variable tcp_data_avbl to reflect the remaining data
667+
if (_sock_i[id].tcp_data_rcvd > 0) {
668+
if (_sock_i[id].tcp_data_avbl > _sock_i[id].tcp_data_rcvd) {
669+
_sock_i[id].tcp_data_avbl -= _sock_i[id].tcp_data_rcvd;
670+
} else {
671+
_sock_i[id].tcp_data_avbl = 0;
672+
}
673+
}
674+
658675
ret = _sock_i[id].tcp_data_rcvd;
659676
}
660677

0 commit comments

Comments
 (0)