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.
This commit is contained in:
Andre Lorbach 2021-02-16 01:21:53 +01:00 committed by GitHub
parent e831680a41
commit f13ff65691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -479,6 +479,7 @@ AsyncUDP::AsyncUDP()
{ {
_pcb = NULL; _pcb = NULL;
_connected = false; _connected = false;
_lastErr = ERR_OK;
_handler = NULL; _handler = NULL;
} }
@ -517,8 +518,8 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port)
} }
close(); close();
UDP_MUTEX_LOCK(); UDP_MUTEX_LOCK();
err_t err = _udp_connect(_pcb, addr, port); _lastErr = _udp_connect(_pcb, addr, port);
if(err != ERR_OK) { if(_lastErr != ERR_OK) {
UDP_MUTEX_UNLOCK(); UDP_MUTEX_UNLOCK();
return false; 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) { if(len > CONFIG_TCP_MSS) {
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); pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if(pbt != NULL) { if(pbt != NULL) {
uint8_t* dst = reinterpret_cast<uint8_t*>(pbt->payload); uint8_t* dst = reinterpret_cast<uint8_t*>(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; void * nif = NULL;
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif); tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
if(!nif){ if(!nif){
err = _udp_sendto(_pcb, pbt, addr, port); _lastErr = _udp_sendto(_pcb, pbt, addr, port);
} else { } else {
err = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif); _lastErr = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif);
} }
} else { } else {
err = _udp_sendto(_pcb, pbt, addr, port); _lastErr = _udp_sendto(_pcb, pbt, addr, port);
} }
UDP_MUTEX_UNLOCK(); UDP_MUTEX_UNLOCK();
pbuf_free(pbt); pbuf_free(pbt);
if(err < ERR_OK) { if(_lastErr < ERR_OK) {
return 0; return 0;
} }
return len; return len;
@ -870,6 +871,10 @@ bool AsyncUDP::connected()
return _connected; return _connected;
} }
esp_err_t AsyncUDP::lastErr() {
return _lastErr;
}
void AsyncUDP::onPacket(AuPacketHandlerFunctionWithArg cb, void * arg) void AsyncUDP::onPacket(AuPacketHandlerFunctionWithArg cb, void * arg)
{ {
onPacket(std::bind(cb, arg, std::placeholders::_1)); onPacket(std::bind(cb, arg, std::placeholders::_1));

View File

@ -95,6 +95,7 @@ protected:
udp_pcb *_pcb; udp_pcb *_pcb;
//xSemaphoreHandle _lock; //xSemaphoreHandle _lock;
bool _connected; bool _connected;
esp_err_t _lastErr;
AuPacketHandlerFunction _handler; AuPacketHandlerFunction _handler;
bool _init(); bool _init();
@ -144,6 +145,7 @@ public:
IPAddress listenIP(); IPAddress listenIP();
IPv6Address listenIPv6(); IPv6Address listenIPv6();
bool connected(); bool connected();
esp_err_t lastErr();
operator bool(); operator bool();
static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif); static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif);