Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f083e2d

Browse files
HamzaHajeirpre-commit-ci-lite[bot]
andauthored
fix(asyncudp): Fixes and implements tcpip thread locking (#10415)
* fix(asyncudp): Fixes and implements tcpip thread locking * fix(asyncudp): Adds missing unlock * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 3445164 commit f083e2d

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

‎libraries/AsyncUDP/src/AsyncUDP.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ extern "C" {
1515

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

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

2035
static esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **netif) {
@@ -28,7 +43,9 @@ static esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **net
2843
if (netif_index < 0) {
2944
return ESP_FAIL;
3045
}
46+
UDP_MUTEX_LOCK();
3147
*netif = (void *)netif_get_by_index(netif_index);
48+
UDP_MUTEX_UNLOCK();
3249
} else {
3350
*netif = netif_default;
3451
}
@@ -232,9 +249,6 @@ static bool _udp_task_stop(){
232249
}
233250
*/
234251

235-
#define UDP_MUTEX_LOCK() //xSemaphoreTake(_lock, portMAX_DELAY)
236-
#define UDP_MUTEX_UNLOCK() //xSemaphoreGive(_lock)
237-
238252
AsyncUDPMessage::AsyncUDPMessage(size_t size) {
239253
_index = 0;
240254
if (size > CONFIG_TCP_MSS) {
@@ -473,12 +487,14 @@ bool AsyncUDP::_init() {
473487
if (_pcb) {
474488
return true;
475489
}
490+
UDP_MUTEX_LOCK();
476491
_pcb = udp_new();
477492
if (!_pcb) {
493+
UDP_MUTEX_UNLOCK();
478494
return false;
479495
}
480-
//_lock = xSemaphoreCreateMutex();
481496
udp_recv(_pcb, &_udp_recv, (void *)this);
497+
UDP_MUTEX_UNLOCK();
482498
return true;
483499
}
484500

@@ -493,22 +509,19 @@ AsyncUDP::~AsyncUDP() {
493509
close();
494510
UDP_MUTEX_LOCK();
495511
udp_recv(_pcb, NULL, NULL);
512+
UDP_MUTEX_UNLOCK();
496513
_udp_remove(_pcb);
497514
_pcb = NULL;
498-
UDP_MUTEX_UNLOCK();
499-
//vSemaphoreDelete(_lock);
500515
}
501516

502517
void AsyncUDP::close() {
503-
UDP_MUTEX_LOCK();
504518
if (_pcb != NULL) {
505519
if (_connected) {
506520
_udp_disconnect(_pcb);
507521
}
508522
_connected = false;
509523
//todo: unjoin multicast group
510524
}
511-
UDP_MUTEX_UNLOCK();
512525
}
513526

514527
bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) {
@@ -520,14 +533,11 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) {
520533
return false;
521534
}
522535
close();
523-
UDP_MUTEX_LOCK();
524536
_lastErr = _udp_connect(_pcb, addr, port);
525537
if (_lastErr != ERR_OK) {
526-
UDP_MUTEX_UNLOCK();
527538
return false;
528539
}
529540
_connected = true;
530-
UDP_MUTEX_UNLOCK();
531541
return true;
532542
}
533543

@@ -544,13 +554,10 @@ bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port) {
544554
IP_SET_TYPE_VAL(_pcb->local_ip, addr->type);
545555
IP_SET_TYPE_VAL(_pcb->remote_ip, addr->type);
546556
}
547-
UDP_MUTEX_LOCK();
548557
if (_udp_bind(_pcb, addr, port) != ERR_OK) {
549-
UDP_MUTEX_UNLOCK();
550558
return false;
551559
}
552560
_connected = true;
553-
UDP_MUTEX_UNLOCK();
554561
return true;
555562
}
556563

@@ -624,12 +631,10 @@ bool AsyncUDP::listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl
624631
return false;
625632
}
626633

627-
UDP_MUTEX_LOCK();
628634
_pcb->mcast_ttl = ttl;
629635
_pcb->remote_port = port;
630636
ip_addr_copy(_pcb->remote_ip, *addr);
631637
//ip_addr_copy(_pcb->remote_ip, ip_addr_any_type);
632-
UDP_MUTEX_UNLOCK();
633638

634639
return true;
635640
}
@@ -651,7 +656,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
651656
if (pbt != NULL) {
652657
uint8_t *dst = reinterpret_cast<uint8_t *>(pbt->payload);
653658
memcpy(dst, data, len);
654-
UDP_MUTEX_LOCK();
655659
if (tcpip_if < TCPIP_ADAPTER_IF_MAX) {
656660
void *nif = NULL;
657661
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
@@ -663,7 +667,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
663667
} else {
664668
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
665669
}
666-
UDP_MUTEX_UNLOCK();
667670
pbuf_free(pbt);
668671
if (_lastErr < ERR_OK) {
669672
return 0;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /