@@ -15,6 +15,21 @@ extern "C" {
15
15
16
16
#include " lwip/priv/tcpip_priv.h"
17
17
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
+
18
33
static const char *netif_ifkeys[TCPIP_ADAPTER_IF_MAX] = {" WIFI_STA_DEF" , " WIFI_AP_DEF" , " ETH_DEF" , " PPP_DEF" };
19
34
20
35
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
28
43
if (netif_index < 0 ) {
29
44
return ESP_FAIL;
30
45
}
46
+ UDP_MUTEX_LOCK ();
31
47
*netif = (void *)netif_get_by_index (netif_index);
48
+ UDP_MUTEX_UNLOCK ();
32
49
} else {
33
50
*netif = netif_default;
34
51
}
@@ -232,9 +249,6 @@ static bool _udp_task_stop(){
232
249
}
233
250
*/
234
251
235
- #define UDP_MUTEX_LOCK () // xSemaphoreTake(_lock, portMAX_DELAY)
236
- #define UDP_MUTEX_UNLOCK () // xSemaphoreGive(_lock)
237
-
238
252
AsyncUDPMessage::AsyncUDPMessage (size_t size) {
239
253
_index = 0 ;
240
254
if (size > CONFIG_TCP_MSS) {
@@ -473,12 +487,14 @@ bool AsyncUDP::_init() {
473
487
if (_pcb) {
474
488
return true ;
475
489
}
490
+ UDP_MUTEX_LOCK ();
476
491
_pcb = udp_new ();
477
492
if (!_pcb) {
493
+ UDP_MUTEX_UNLOCK ();
478
494
return false ;
479
495
}
480
- // _lock = xSemaphoreCreateMutex();
481
496
udp_recv (_pcb, &_udp_recv, (void *)this );
497
+ UDP_MUTEX_UNLOCK ();
482
498
return true ;
483
499
}
484
500
@@ -493,22 +509,19 @@ AsyncUDP::~AsyncUDP() {
493
509
close ();
494
510
UDP_MUTEX_LOCK ();
495
511
udp_recv (_pcb, NULL , NULL );
512
+ UDP_MUTEX_UNLOCK ();
496
513
_udp_remove (_pcb);
497
514
_pcb = NULL ;
498
- UDP_MUTEX_UNLOCK ();
499
- // vSemaphoreDelete(_lock);
500
515
}
501
516
502
517
void AsyncUDP::close () {
503
- UDP_MUTEX_LOCK ();
504
518
if (_pcb != NULL ) {
505
519
if (_connected) {
506
520
_udp_disconnect (_pcb);
507
521
}
508
522
_connected = false ;
509
523
// todo: unjoin multicast group
510
524
}
511
- UDP_MUTEX_UNLOCK ();
512
525
}
513
526
514
527
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) {
520
533
return false ;
521
534
}
522
535
close ();
523
- UDP_MUTEX_LOCK ();
524
536
_lastErr = _udp_connect (_pcb, addr, port);
525
537
if (_lastErr != ERR_OK) {
526
- UDP_MUTEX_UNLOCK ();
527
538
return false ;
528
539
}
529
540
_connected = true ;
530
- UDP_MUTEX_UNLOCK ();
531
541
return true ;
532
542
}
533
543
@@ -544,13 +554,10 @@ bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port) {
544
554
IP_SET_TYPE_VAL (_pcb->local_ip , addr->type );
545
555
IP_SET_TYPE_VAL (_pcb->remote_ip , addr->type );
546
556
}
547
- UDP_MUTEX_LOCK ();
548
557
if (_udp_bind (_pcb, addr, port) != ERR_OK) {
549
- UDP_MUTEX_UNLOCK ();
550
558
return false ;
551
559
}
552
560
_connected = true ;
553
- UDP_MUTEX_UNLOCK ();
554
561
return true ;
555
562
}
556
563
@@ -624,12 +631,10 @@ bool AsyncUDP::listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl
624
631
return false ;
625
632
}
626
633
627
- UDP_MUTEX_LOCK ();
628
634
_pcb->mcast_ttl = ttl;
629
635
_pcb->remote_port = port;
630
636
ip_addr_copy (_pcb->remote_ip , *addr);
631
637
// ip_addr_copy(_pcb->remote_ip, ip_addr_any_type);
632
- UDP_MUTEX_UNLOCK ();
633
638
634
639
return true ;
635
640
}
@@ -651,7 +656,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
651
656
if (pbt != NULL ) {
652
657
uint8_t *dst = reinterpret_cast <uint8_t *>(pbt->payload );
653
658
memcpy (dst, data, len);
654
- UDP_MUTEX_LOCK ();
655
659
if (tcpip_if < TCPIP_ADAPTER_IF_MAX) {
656
660
void *nif = NULL ;
657
661
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,
663
667
} else {
664
668
_lastErr = _udp_sendto (_pcb, pbt, addr, port);
665
669
}
666
- UDP_MUTEX_UNLOCK ();
667
670
pbuf_free (pbt);
668
671
if (_lastErr < ERR_OK) {
669
672
return 0 ;
0 commit comments