Skip to content

Commit 5bbb5d5

Browse files
committed
fix(asyncudp): Fixes and implements tcpip thread locking
1 parent 733373a commit 5bbb5d5

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Diff for: libraries/AsyncUDP/src/AsyncUDP.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ extern "C" {
1515

1616
#include "lwip/priv/tcpip_priv.h"
1717

18+
#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
19+
#define UDP_MUTEX_LOCK() if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
20+
LOCK_TCPIP_CORE(); \
21+
}
22+
23+
#define UDP_MUTEX_UNLOCK() if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
24+
UNLOCK_TCPIP_CORE(); \
25+
}
26+
#else // CONFIG_LWIP_TCPIP_CORE_LOCKING
27+
#define UDP_MUTEX_LOCK()
28+
#define UDP_MUTEX_UNLOCK()
29+
#endif // CONFIG_LWIP_TCPIP_CORE_LOCKING
30+
1831
static const char *netif_ifkeys[TCPIP_ADAPTER_IF_MAX] = {"WIFI_STA_DEF", "WIFI_AP_DEF", "ETH_DEF", "PPP_DEF"};
1932

2033
static esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **netif) {
@@ -28,7 +41,9 @@ static esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **net
2841
if (netif_index < 0) {
2942
return ESP_FAIL;
3043
}
44+
UDP_MUTEX_LOCK();
3145
*netif = (void *)netif_get_by_index(netif_index);
46+
UDP_MUTEX_UNLOCK();
3247
} else {
3348
*netif = netif_default;
3449
}
@@ -232,9 +247,6 @@ static bool _udp_task_stop(){
232247
}
233248
*/
234249

235-
#define UDP_MUTEX_LOCK() //xSemaphoreTake(_lock, portMAX_DELAY)
236-
#define UDP_MUTEX_UNLOCK() //xSemaphoreGive(_lock)
237-
238250
AsyncUDPMessage::AsyncUDPMessage(size_t size) {
239251
_index = 0;
240252
if (size > CONFIG_TCP_MSS) {
@@ -473,12 +485,13 @@ bool AsyncUDP::_init() {
473485
if (_pcb) {
474486
return true;
475487
}
488+
UDP_MUTEX_LOCK();
476489
_pcb = udp_new();
477490
if (!_pcb) {
478491
return false;
479492
}
480-
//_lock = xSemaphoreCreateMutex();
481493
udp_recv(_pcb, &_udp_recv, (void *)this);
494+
UDP_MUTEX_UNLOCK();
482495
return true;
483496
}
484497

@@ -493,22 +506,19 @@ AsyncUDP::~AsyncUDP() {
493506
close();
494507
UDP_MUTEX_LOCK();
495508
udp_recv(_pcb, NULL, NULL);
509+
UDP_MUTEX_UNLOCK();
496510
_udp_remove(_pcb);
497511
_pcb = NULL;
498-
UDP_MUTEX_UNLOCK();
499-
//vSemaphoreDelete(_lock);
500512
}
501513

502514
void AsyncUDP::close() {
503-
UDP_MUTEX_LOCK();
504515
if (_pcb != NULL) {
505516
if (_connected) {
506517
_udp_disconnect(_pcb);
507518
}
508519
_connected = false;
509520
//todo: unjoin multicast group
510521
}
511-
UDP_MUTEX_UNLOCK();
512522
}
513523

514524
bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) {
@@ -520,14 +530,11 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) {
520530
return false;
521531
}
522532
close();
523-
UDP_MUTEX_LOCK();
524533
_lastErr = _udp_connect(_pcb, addr, port);
525534
if (_lastErr != ERR_OK) {
526-
UDP_MUTEX_UNLOCK();
527535
return false;
528536
}
529537
_connected = true;
530-
UDP_MUTEX_UNLOCK();
531538
return true;
532539
}
533540

@@ -544,13 +551,10 @@ bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port) {
544551
IP_SET_TYPE_VAL(_pcb->local_ip, addr->type);
545552
IP_SET_TYPE_VAL(_pcb->remote_ip, addr->type);
546553
}
547-
UDP_MUTEX_LOCK();
548554
if (_udp_bind(_pcb, addr, port) != ERR_OK) {
549-
UDP_MUTEX_UNLOCK();
550555
return false;
551556
}
552557
_connected = true;
553-
UDP_MUTEX_UNLOCK();
554558
return true;
555559
}
556560

@@ -624,12 +628,10 @@ bool AsyncUDP::listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl
624628
return false;
625629
}
626630

627-
UDP_MUTEX_LOCK();
628631
_pcb->mcast_ttl = ttl;
629632
_pcb->remote_port = port;
630633
ip_addr_copy(_pcb->remote_ip, *addr);
631634
//ip_addr_copy(_pcb->remote_ip, ip_addr_any_type);
632-
UDP_MUTEX_UNLOCK();
633635

634636
return true;
635637
}
@@ -651,7 +653,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
651653
if (pbt != NULL) {
652654
uint8_t *dst = reinterpret_cast<uint8_t *>(pbt->payload);
653655
memcpy(dst, data, len);
654-
UDP_MUTEX_LOCK();
655656
if (tcpip_if < TCPIP_ADAPTER_IF_MAX) {
656657
void *nif = NULL;
657658
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
@@ -663,7 +664,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
663664
} else {
664665
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
665666
}
666-
UDP_MUTEX_UNLOCK();
667667
pbuf_free(pbt);
668668
if (_lastErr < ERR_OK) {
669669
return 0;

0 commit comments

Comments
 (0)