Skip to content

Commit f13ff65

Browse files
authored
AsyncUDP: Added lastErr helper variable (espressif#4789)
The variable is useful when debugging AsyncUDP send problems. The upper application can read and analyze the error reason.
1 parent e831680 commit f13ff65

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

libraries/AsyncUDP/src/AsyncUDP.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ AsyncUDP::AsyncUDP()
479479
{
480480
_pcb = NULL;
481481
_connected = false;
482+
_lastErr = ERR_OK;
482483
_handler = NULL;
483484
}
484485

@@ -517,8 +518,8 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port)
517518
}
518519
close();
519520
UDP_MUTEX_LOCK();
520-
err_t err = _udp_connect(_pcb, addr, port);
521-
if(err != ERR_OK) {
521+
_lastErr = _udp_connect(_pcb, addr, port);
522+
if(_lastErr != ERR_OK) {
522523
UDP_MUTEX_UNLOCK();
523524
return false;
524525
}
@@ -646,7 +647,7 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add
646647
if(len > CONFIG_TCP_MSS) {
647648
len = CONFIG_TCP_MSS;
648649
}
649-
err_t err = ERR_OK;
650+
_lastErr = ERR_OK;
650651
pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
651652
if(pbt != NULL) {
652653
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
656657
void * nif = NULL;
657658
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
658659
if(!nif){
659-
err = _udp_sendto(_pcb, pbt, addr, port);
660+
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
660661
} else {
661-
err = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif);
662+
_lastErr = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif);
662663
}
663664
} else {
664-
err = _udp_sendto(_pcb, pbt, addr, port);
665+
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
665666
}
666667
UDP_MUTEX_UNLOCK();
667668
pbuf_free(pbt);
668-
if(err < ERR_OK) {
669+
if(_lastErr < ERR_OK) {
669670
return 0;
670671
}
671672
return len;
@@ -870,6 +871,10 @@ bool AsyncUDP::connected()
870871
return _connected;
871872
}
872873

874+
esp_err_t AsyncUDP::lastErr() {
875+
return _lastErr;
876+
}
877+
873878
void AsyncUDP::onPacket(AuPacketHandlerFunctionWithArg cb, void * arg)
874879
{
875880
onPacket(std::bind(cb, arg, std::placeholders::_1));

libraries/AsyncUDP/src/AsyncUDP.h

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class AsyncUDP : public Print
9595
udp_pcb *_pcb;
9696
//xSemaphoreHandle _lock;
9797
bool _connected;
98+
esp_err_t _lastErr;
9899
AuPacketHandlerFunction _handler;
99100

100101
bool _init();
@@ -144,6 +145,7 @@ class AsyncUDP : public Print
144145
IPAddress listenIP();
145146
IPv6Address listenIPv6();
146147
bool connected();
148+
esp_err_t lastErr();
147149
operator bool();
148150

149151
static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif);

0 commit comments

Comments
 (0)