Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(asyncudp): Fixes and implements tcpip thread locking #10415

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions libraries/AsyncUDP/src/AsyncUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ extern "C" {

#include "lwip/priv/tcpip_priv.h"

#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
#define UDP_MUTEX_LOCK() \
if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
LOCK_TCPIP_CORE(); \
}

#define UDP_MUTEX_UNLOCK() \
if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { \
UNLOCK_TCPIP_CORE(); \
}
#else // CONFIG_LWIP_TCPIP_CORE_LOCKING
#define UDP_MUTEX_LOCK()
#define UDP_MUTEX_UNLOCK()
#endif // CONFIG_LWIP_TCPIP_CORE_LOCKING

static const char *netif_ifkeys[TCPIP_ADAPTER_IF_MAX] = {"WIFI_STA_DEF", "WIFI_AP_DEF", "ETH_DEF", "PPP_DEF"};

static esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **netif) {
Expand All @@ -28,7 +43,9 @@ static esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void **net
if (netif_index < 0) {
return ESP_FAIL;
}
UDP_MUTEX_LOCK();
*netif = (void *)netif_get_by_index(netif_index);
UDP_MUTEX_UNLOCK();
} else {
*netif = netif_default;
}
Expand Down Expand Up @@ -232,9 +249,6 @@ static bool _udp_task_stop(){
}
*/

#define UDP_MUTEX_LOCK() //xSemaphoreTake(_lock, portMAX_DELAY)
#define UDP_MUTEX_UNLOCK() //xSemaphoreGive(_lock)

AsyncUDPMessage::AsyncUDPMessage(size_t size) {
_index = 0;
if (size > CONFIG_TCP_MSS) {
Expand Down Expand Up @@ -473,12 +487,14 @@ bool AsyncUDP::_init() {
if (_pcb) {
return true;
}
UDP_MUTEX_LOCK();
_pcb = udp_new();
if (!_pcb) {
UDP_MUTEX_UNLOCK();
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be an unlock before the return?

}
//_lock = xSemaphoreCreateMutex();
udp_recv(_pcb, &_udp_recv, (void *)this);
UDP_MUTEX_UNLOCK();
return true;
}

Expand All @@ -493,22 +509,19 @@ AsyncUDP::~AsyncUDP() {
close();
UDP_MUTEX_LOCK();
udp_recv(_pcb, NULL, NULL);
UDP_MUTEX_UNLOCK();
_udp_remove(_pcb);
_pcb = NULL;
UDP_MUTEX_UNLOCK();
//vSemaphoreDelete(_lock);
}

void AsyncUDP::close() {
UDP_MUTEX_LOCK();
if (_pcb != NULL) {
if (_connected) {
_udp_disconnect(_pcb);
}
_connected = false;
//todo: unjoin multicast group
}
UDP_MUTEX_UNLOCK();
}

bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) {
Expand All @@ -520,14 +533,11 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port) {
return false;
}
close();
UDP_MUTEX_LOCK();
_lastErr = _udp_connect(_pcb, addr, port);
if (_lastErr != ERR_OK) {
UDP_MUTEX_UNLOCK();
return false;
}
_connected = true;
UDP_MUTEX_UNLOCK();
return true;
}

Expand All @@ -544,13 +554,10 @@ bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port) {
IP_SET_TYPE_VAL(_pcb->local_ip, addr->type);
IP_SET_TYPE_VAL(_pcb->remote_ip, addr->type);
}
UDP_MUTEX_LOCK();
if (_udp_bind(_pcb, addr, port) != ERR_OK) {
UDP_MUTEX_UNLOCK();
return false;
}
_connected = true;
UDP_MUTEX_UNLOCK();
return true;
}

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

UDP_MUTEX_LOCK();
_pcb->mcast_ttl = ttl;
_pcb->remote_port = port;
ip_addr_copy(_pcb->remote_ip, *addr);
//ip_addr_copy(_pcb->remote_ip, ip_addr_any_type);
UDP_MUTEX_UNLOCK();

return true;
}
Expand All @@ -651,7 +656,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
if (pbt != NULL) {
uint8_t *dst = reinterpret_cast<uint8_t *>(pbt->payload);
memcpy(dst, data, len);
UDP_MUTEX_LOCK();
if (tcpip_if < TCPIP_ADAPTER_IF_MAX) {
void *nif = NULL;
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
Expand All @@ -663,7 +667,6 @@ size_t AsyncUDP::writeTo(const uint8_t *data, size_t len, const ip_addr_t *addr,
} else {
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
}
UDP_MUTEX_UNLOCK();
pbuf_free(pbt);
if (_lastErr < ERR_OK) {
return 0;
Expand Down
Loading