From 902feddf2e98be688df4877dd8816b2d30149b74 Mon Sep 17 00:00:00 2001 From: Michal Paszta Date: Fri, 27 Dec 2019 16:03:36 +0200 Subject: [PATCH] ESP8266: Improve error handling and partial sends --- components/wifi/esp8266-driver/ESP8266/ESP8266.cpp | 11 ++++++++--- components/wifi/esp8266-driver/ESP8266/ESP8266.h | 6 +++--- components/wifi/esp8266-driver/ESP8266Interface.cpp | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp index 9a26abd54b..1fc2ab4054 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp @@ -614,7 +614,7 @@ bool ESP8266::dns_lookup(const char *name, char *ip) return done; } -nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount) +nsapi_size_or_error_t ESP8266::send(int id, const void *data, uint32_t amount) { if (_prev_send_ok_pending && _sock_i[id].proto == NSAPI_TCP) { tr_debug("send(): Previous packet was not ACK-ed with SEND OK."); @@ -649,7 +649,7 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount) // This means ESP8266 hasn't even started to receive data tr_debug("send(): Didn't get \">\""); if (_sock_i[id].proto == NSAPI_TCP) { - ret = NSAPI_ERROR_WOULD_BLOCK; // Not neccesarily critical error. + ret = NSAPI_ERROR_WOULD_BLOCK; // Not necessarily critical error. } else if (_sock_i[id].proto == NSAPI_UDP) { ret = NSAPI_ERROR_NO_MEMORY; } @@ -667,6 +667,11 @@ nsapi_error_t ESP8266::send(int id, const void *data, uint32_t amount) if (!_parser.recv("Recv %d bytes", &bytes_confirmed)) { tr_debug("send(): Bytes not confirmed."); ret = NSAPI_ERROR_DEVICE_ERROR; + if (_sock_i[id].proto == NSAPI_TCP) { + ret = NSAPI_ERROR_WOULD_BLOCK; + } else if (_sock_i[id].proto == NSAPI_UDP) { + ret = NSAPI_ERROR_NO_MEMORY; + } goto END; } else if (bytes_confirmed != amount) { tr_debug("send(): Error: confirmed %d bytes, but expected %d.", bytes_confirmed, amount); @@ -724,7 +729,7 @@ END: if (!_sock_i[id].open && ret < 0) { ret = NSAPI_ERROR_CONNECTION_LOST; - tr_debug("send(): Socket closed abruptly."); + tr_debug("send(): Socket %d closed abruptly.", id); } set_timeout(); diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.h b/components/wifi/esp8266-driver/ESP8266/ESP8266.h index 48e66e585f..babb929e6e 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.h +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.h @@ -255,10 +255,10 @@ public: * * @param id id of socket to send to * @param data data to be sent - * @param amount amount of data to be sent - max 1024 - * @return NSAPI_ERROR_OK in success, negative error code in failure + * @param amount amount of data to be sent - max 2048 + * @return number of bytes on success, negative error code in failure */ - nsapi_error_t send(int id, const void *data, uint32_t amount); + nsapi_size_or_error_t send(int id, const void *data, uint32_t amount); /** * Receives datagram from an open UDP socket diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index 3aeb82a5b9..255232386c 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -849,7 +849,7 @@ int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress * int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) { - nsapi_error_t status; + nsapi_size_or_error_t status; struct esp8266_socket *socket = (struct esp8266_socket *)handle; uint8_t expect_false = false; @@ -881,7 +881,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size) status = NSAPI_ERROR_DEVICE_ERROR; } - return status != NSAPI_ERROR_OK ? status : size; + return status; } int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)