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