Skip to content

Commit 89ccbb7

Browse files
author
Veijo Pesonen
committed
Fixes TCP passive recv()
1 parent 28b927d commit 89ccbb7

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

ESP8266/ESP8266.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -622,36 +622,31 @@ void ESP8266::bg_process_oob(uint32_t timeout, bool all)
622622

623623
int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout)
624624
{
625-
int32_t ret;
625+
int32_t ret = NSAPI_ERROR_WOULD_BLOCK;
626626

627627
_smutex.lock();
628628

629629
_process_oob(timeout, true);
630630

631-
// return immediately if no data is available
632-
if (_sock_i[id].tcp_data_avbl == 0 && _sock_i[id].open) {
633-
_smutex.unlock();
634-
return NSAPI_ERROR_WOULD_BLOCK;
635-
}
631+
if (_sock_i[id].tcp_data_avbl != 0) {
632+
_sock_i[id].tcp_data = (char*)data;
633+
_sock_i[id].tcp_data_rcvd = NSAPI_ERROR_WOULD_BLOCK;
634+
_sock_active_id = id;
636635

637-
_sock_i[id].tcp_data = (char*)data;
638-
_sock_i[id].tcp_data_rcvd = NSAPI_ERROR_WOULD_BLOCK;
639-
_sock_active_id = id;
636+
// +CIPRECVDATA supports up to 2048 bytes at a time
637+
amount = amount > 2048 ? 2048 : amount;
640638

641-
// +CIPRECVDATA supports up to 2048 bytes at a time
642-
if (amount > 2048) {
643-
amount = 2048;
644-
}
645-
646-
// NOTE: documentation v3.0 says '+CIPRECVDATA:<data_len>,' but it's not how the FW responds...
647-
bool done = _parser.send("AT+CIPRECVDATA=%d,%lu", id, amount)
648-
&& _parser.recv("OK\n");
649-
650-
(void)done;
651-
_sock_i[id].tcp_data = NULL;
652-
_sock_active_id = -1;
639+
// NOTE: documentation v3.0 says '+CIPRECVDATA:<data_len>,' but it's not how the FW responds...
640+
bool done = _parser.send("AT+CIPRECVDATA=%d,%lu", id, amount)
641+
&& _parser.recv("OK\n");
642+
if (!done) {
643+
tr_debug("data request failed");
644+
}
645+
_sock_i[id].tcp_data = NULL;
646+
_sock_active_id = -1;
653647

654-
ret = _sock_i[id].tcp_data_rcvd;
648+
ret = _sock_i[id].tcp_data_rcvd;
649+
}
655650

656651
if (!_sock_i[id].open && ret == NSAPI_ERROR_WOULD_BLOCK) {
657652
ret = 0;

ESP8266Interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ESP8266Interface::ESP8266Interface()
6363

6464
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
6565
_sock_i[i].open = false;
66-
_sock_i[i].sport = -1;
66+
_sock_i[i].sport = 0;
6767
}
6868
}
6969
#endif
@@ -89,7 +89,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
8989

9090
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
9191
_sock_i[i].open = false;
92-
_sock_i[i].sport = -1;
92+
_sock_i[i].sport = 0;
9393
}
9494
}
9595

0 commit comments

Comments
 (0)