Skip to content

Commit 02050c5

Browse files
ESP8266: add a retry mechanism to avoid duplicate data sends
We are now checking if ESP8266 has confirmed receiving data over serial port with an undocumented (but existing) "Recv x bytes" message. Next we are explicitly waiting for an official "SEND OK".
1 parent 789a193 commit 02050c5

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
615615
nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
616616
{
617617
nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
618+
unsigned int send_ack_retries = 3;
618619
// +CIPSEND supports up to 2048 bytes at a time
619620
// Data stream can be truncated
620621
if (amount > 2048 && _sock_i[id].proto == NSAPI_TCP) {
@@ -643,8 +644,10 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
643644
_parser.remove_oob("OK");
644645
if (_busy) {
645646
if (_ok_received) {
647+
tr_debug("send(): _ok_received.");
646648
goto RETRY;
647649
} else if (_parser.recv("OK")) {
650+
tr_debug("send(): parser found OK");
648651
goto RETRY;
649652
}
650653
}
@@ -655,7 +658,37 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
655658
_ok_received = false;
656659
_parser.remove_oob("OK");
657660

658-
if (_parser.write((char *)data, (int)amount) >= 0 && _parser.recv("SEND OK")) {
661+
if (_parser.write((char *)data, (int)amount) < 0) {
662+
tr_debug("Failed to write data");
663+
ret = NSAPI_ERROR_DEVICE_ERROR;
664+
goto END;
665+
}
666+
667+
// The "Recv X bytes" is not documented.
668+
int bytes_confirmed;
669+
if (!_parser.recv("Recv %d bytes", &bytes_confirmed)) {
670+
tr_debug("Bytes not confirmed.");
671+
ret = NSAPI_ERROR_DEVICE_ERROR;
672+
goto END;
673+
} else if (bytes_confirmed != amount) {
674+
tr_debug("Error: confirmed %d bytes, but expected %d.", bytes_confirmed, amount);
675+
ret = NSAPI_ERROR_DEVICE_ERROR;
676+
goto END;
677+
}
678+
679+
send_ack_retries = 3;
680+
RETRY_SEND_ACK:
681+
if (!_parser.recv("SEND OK")) {
682+
send_ack_retries--;
683+
if (_busy) {
684+
_busy = false;
685+
tr_debug("Busy, retry...");
686+
goto RETRY_SEND_ACK;
687+
} else if (send_ack_retries > 0) {
688+
tr_debug("Not busy, but no SEND OK. %d retries left...", send_ack_retries);
689+
goto RETRY_SEND_ACK;
690+
}
691+
} else {
659692
ret = NSAPI_ERROR_OK;
660693
}
661694

0 commit comments

Comments
 (0)