Skip to content

Commit 7c112b2

Browse files
author
Veijo Pesonen
committed
[ESP8266] socket_send() returns WOULD_BLOCK if server won't accept
Driver must return NSAPI_ERROR_WOULD_BLOCK if a server won't accept data from the modem and the modem's buffers are full. In case that socket is closed driver returns NSAPI_ERROR_CONNECTION_LOST.
1 parent c27dabe commit 7c112b2

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
@@ -563,6 +563,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip)
563563

564564
nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
565565
{
566+
nsapi_error_t ret = NSAPI_ERROR_DEVICE_ERROR;
566567
// +CIPSEND supports up to 2048 bytes at a time
567568
// Data stream can be truncated
568569
if (amount > 2048 && _sock_i[id].proto == NSAPI_TCP) {
@@ -576,31 +577,50 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount)
576577
_smutex.lock();
577578
set_timeout(ESP8266_SEND_TIMEOUT);
578579
_busy = false;
579-
if (_parser.send("AT+CIPSEND=%d,%lu", id, amount)
580-
&& _parser.recv(">")
581-
&& _parser.write((char *)data, (int)amount) >= 0
582-
&& _parser.recv("SEND OK")) {
583-
// No flow control, data overrun is possible
584-
if (_serial_rts == NC) {
585-
while (_parser.process_oob()); // Drain USART receive register
586-
}
587-
_smutex.unlock();
588-
return NSAPI_ERROR_OK;
580+
_error = false;
581+
if (!_parser.send("AT+CIPSEND=%d,%lu", id, amount)) {
582+
tr_debug("ESP8266::send(): AT+CIPSEND failed");
583+
goto END;
589584
}
590-
if (_error) {
591-
_error = false;
585+
586+
if(!_parser.recv(">")) {
587+
tr_debug("ESP8266::send(): didn't get \">\"");
588+
ret = NSAPI_ERROR_WOULD_BLOCK;
589+
goto END;
592590
}
591+
592+
if (_parser.write((char *)data, (int)amount) >= 0 && _parser.recv("SEND OK")) {
593+
ret = NSAPI_ERROR_OK;
594+
}
595+
596+
END:
597+
_process_oob(ESP8266_RECV_TIMEOUT, true); // Drain USART receive register to avoid data overrun
598+
599+
// error hierarchy, from low to high
593600
if (_busy) {
594-
set_timeout();
595-
_smutex.unlock();
596-
tr_debug("returning WOULD_BLOCK");
597-
return NSAPI_ERROR_WOULD_BLOCK;
601+
ret = NSAPI_ERROR_WOULD_BLOCK;
602+
tr_debug("ESP8266::send(): modem busy");
603+
}
604+
605+
if (ret == NSAPI_ERROR_DEVICE_ERROR) {
606+
ret = NSAPI_ERROR_WOULD_BLOCK;
607+
tr_debug("ESP8266::send(): send failed");
608+
}
609+
610+
if (_error) {
611+
ret = NSAPI_ERROR_CONNECTION_LOST;
612+
tr_debug("ESP8266::send(): connection disrupted");
613+
}
614+
615+
if (!_sock_i[id].open && ret != NSAPI_ERROR_OK) {
616+
ret = NSAPI_ERROR_CONNECTION_LOST;
617+
tr_debug("ESP8266::send(): socket closed abruptly");
598618
}
599619

600620
set_timeout();
601621
_smutex.unlock();
602622

603-
return NSAPI_ERROR_DEVICE_ERROR;
623+
return ret;
604624
}
605625

606626
void ESP8266::_oob_packet_hdlr()

0 commit comments

Comments
 (0)