From f13ff656910ef5d93f6adce1f1a80f982fb469d4 Mon Sep 17 00:00:00 2001 From: Andre Lorbach Date: Tue, 16 Feb 2021 01:21:53 +0100 Subject: [PATCH] AsyncUDP: Added lastErr helper variable (#4789) The variable is useful when debugging AsyncUDP send problems. The upper application can read and analyze the error reason. --- libraries/AsyncUDP/src/AsyncUDP.cpp | 19 ++++++++++++------- libraries/AsyncUDP/src/AsyncUDP.h | 2 ++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/libraries/AsyncUDP/src/AsyncUDP.cpp b/libraries/AsyncUDP/src/AsyncUDP.cpp index ea2fc04e..2bacb151 100644 --- a/libraries/AsyncUDP/src/AsyncUDP.cpp +++ b/libraries/AsyncUDP/src/AsyncUDP.cpp @@ -479,6 +479,7 @@ AsyncUDP::AsyncUDP() { _pcb = NULL; _connected = false; + _lastErr = ERR_OK; _handler = NULL; } @@ -517,8 +518,8 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) } close(); UDP_MUTEX_LOCK(); - err_t err = _udp_connect(_pcb, addr, port); - if(err != ERR_OK) { + _lastErr = _udp_connect(_pcb, addr, port); + if(_lastErr != ERR_OK) { UDP_MUTEX_UNLOCK(); return false; } @@ -646,7 +647,7 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add if(len > CONFIG_TCP_MSS) { len = CONFIG_TCP_MSS; } - err_t err = ERR_OK; + _lastErr = ERR_OK; pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM); if(pbt != NULL) { uint8_t* dst = reinterpret_cast(pbt->payload); @@ -656,16 +657,16 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add void * nif = NULL; tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif); if(!nif){ - err = _udp_sendto(_pcb, pbt, addr, port); + _lastErr = _udp_sendto(_pcb, pbt, addr, port); } else { - err = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif); + _lastErr = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif); } } else { - err = _udp_sendto(_pcb, pbt, addr, port); + _lastErr = _udp_sendto(_pcb, pbt, addr, port); } UDP_MUTEX_UNLOCK(); pbuf_free(pbt); - if(err < ERR_OK) { + if(_lastErr < ERR_OK) { return 0; } return len; @@ -870,6 +871,10 @@ bool AsyncUDP::connected() return _connected; } +esp_err_t AsyncUDP::lastErr() { + return _lastErr; +} + void AsyncUDP::onPacket(AuPacketHandlerFunctionWithArg cb, void * arg) { onPacket(std::bind(cb, arg, std::placeholders::_1)); diff --git a/libraries/AsyncUDP/src/AsyncUDP.h b/libraries/AsyncUDP/src/AsyncUDP.h index 2ac48a69..80b0c755 100644 --- a/libraries/AsyncUDP/src/AsyncUDP.h +++ b/libraries/AsyncUDP/src/AsyncUDP.h @@ -95,6 +95,7 @@ protected: udp_pcb *_pcb; //xSemaphoreHandle _lock; bool _connected; + esp_err_t _lastErr; AuPacketHandlerFunction _handler; bool _init(); @@ -144,6 +145,7 @@ public: IPAddress listenIP(); IPv6Address listenIPv6(); bool connected(); + esp_err_t lastErr(); operator bool(); static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif);