Skip to content

Commit e25611a

Browse files
authored
Merge pull request #9309 from VeijoPesonen/bugfix-esp8266_send_busy
ESP8266 - fix send buffer exhaustion handling
2 parents f4487b7 + 7c112b2 commit e25611a

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

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

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
557557

558558
nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
559559
{
560+
nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
560561
// +CIPSEND supports up to 2048 bytes at a time
561562
// Data stream can be truncated
562563
if (amount > 2048 && _sock_i[id].proto == NSAPI_TCP) {
@@ -570,31 +571,50 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
570571
_smutex.lock();
571572
set_timeout(ESP8266_SEND_TIMEOUT);
572573
_busy = false;
573-
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
574-
&& _parser.recv(">")
575-
&& _parser.write((char *)data, (int)amount) >= 0
576-
&& _parser.recv("SEND OK")) {
577-
// No flow control, data overrun is possible
578-
if (_serial_rts == NC) {
579-
while (_parser.process_oob()); // Drain USART receive register
580-
}
581-
_smutex.unlock();
582-
return NSAPI_ERROR_OK;
574+
_error = false;
575+
if (!_parser.send("AT+CIPSEND=%d,%lu", id, amount)) {
576+
tr_debug("ESP8266::send(): AT+CIPSEND failed");
577+
goto END;
583578
}
584-
if (_error) {
585-
_error = false;
579+
580+
if(!_parser.recv(">")) {
581+
tr_debug("ESP8266::send(): didn't get \">\"");
582+
ret = NSAPI_ERROR_WOULD_BLOCK;
583+
goto END;
586584
}
585+
586+
if (_parser.write((char *)data, (int)amount) >= 0 && _parser.recv("SEND OK")) {
587+
ret = NSAPI_ERROR_OK;
588+
}
589+
590+
END:
591+
_process_oob(ESP8266_RECV_TIMEOUT, true); // Drain USART receive register to avoid data overrun
592+
593+
// error hierarchy, from low to high
587594
if (_busy) {
588-
set_timeout();
589-
_smutex.unlock();
590-
tr_debug("returning WOULD_BLOCK");
591-
return NSAPI_ERROR_WOULD_BLOCK;
595+
ret = NSAPI_ERROR_WOULD_BLOCK;
596+
tr_debug("ESP8266::send(): modem busy");
597+
}
598+
599+
if (ret == NSAPI_ERROR_DEVICE_ERROR) {
600+
ret = NSAPI_ERROR_WOULD_BLOCK;
601+
tr_debug("ESP8266::send(): send failed");
602+
}
603+
604+
if (_error) {
605+
ret = NSAPI_ERROR_CONNECTION_LOST;
606+
tr_debug("ESP8266::send(): connection disrupted");
607+
}
608+
609+
if (!_sock_i[id].open && ret != NSAPI_ERROR_OK) {
610+
ret = NSAPI_ERROR_CONNECTION_LOST;
611+
tr_debug("ESP8266::send(): socket closed abruptly");
592612
}
593613

594614
set_timeout();
595615
_smutex.unlock();
596616

597-
return NSAPI_ERROR_DEVICE_ERROR;
617+
return ret;
598618
}
599619

600620
void ESP8266::_oob_packet_hdlr()

0 commit comments

Comments
 (0)