ESP8266: Improve error handling and partial sends

pull/12157/head
Michal Paszta 2019-12-27 16:03:36 +02:00
parent de2896c659
commit 902feddf2e
3 changed files with 13 additions and 8 deletions

View File

@ -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();

View File

@ -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

View File

@ -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)