add mutex protection to UDP client table
This commit is contained in:
parent
5f550f031a
commit
3e0cbfd131
|
|
@ -5,6 +5,7 @@
|
|||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_wifi_ap_get_sta_list.h"
|
||||
#include "esp_netif.h"
|
||||
|
|
@ -31,13 +32,18 @@ static struct {
|
|||
} clients[UDP_CLIENTS_MAX];
|
||||
|
||||
static int client_count;
|
||||
static SemaphoreHandle_t client_mutex;
|
||||
|
||||
static void client_touch(const struct sockaddr_in *addr)
|
||||
{
|
||||
xSemaphoreTake(client_mutex, portMAX_DELAY);
|
||||
|
||||
for (int i = 0; i < client_count; i++) {
|
||||
if (clients[i].addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
|
||||
clients[i].addr.sin_port == addr->sin_port)
|
||||
clients[i].addr.sin_port == addr->sin_port) {
|
||||
xSemaphoreGive(client_mutex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (client_count < UDP_CLIENTS_MAX) {
|
||||
|
|
@ -60,10 +66,13 @@ static void client_touch(const struct sockaddr_in *addr)
|
|||
client_count++;
|
||||
printf("UDP: client added (%d)\n", client_count);
|
||||
}
|
||||
|
||||
xSemaphoreGive(client_mutex);
|
||||
}
|
||||
|
||||
static void client_remove_by_mac(const uint8_t *mac)
|
||||
{
|
||||
xSemaphoreTake(client_mutex, portMAX_DELAY);
|
||||
for (int i = 0; i < client_count; ) {
|
||||
if (memcmp(clients[i].mac, mac, 6) == 0) {
|
||||
clients[i] = clients[--client_count];
|
||||
|
|
@ -72,6 +81,7 @@ static void client_remove_by_mac(const uint8_t *mac)
|
|||
i++;
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(client_mutex);
|
||||
}
|
||||
|
||||
static void parse_udp_sysex(const uint8_t *data, uint16_t len)
|
||||
|
|
@ -221,6 +231,7 @@ int wifi_send_sysex(const uint8_t *sysex, uint16_t len)
|
|||
if (udp_sock < 0 || client_count == 0)
|
||||
return -1;
|
||||
|
||||
xSemaphoreTake(client_mutex, portMAX_DELAY);
|
||||
int sent = 0;
|
||||
for (int i = 0; i < client_count; i++) {
|
||||
int r = sendto(udp_sock, sysex, len, 0,
|
||||
|
|
@ -228,6 +239,7 @@ int wifi_send_sysex(const uint8_t *sysex, uint16_t len)
|
|||
sizeof(clients[i].addr));
|
||||
if (r > 0) sent++;
|
||||
}
|
||||
xSemaphoreGive(client_mutex);
|
||||
return sent > 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
|
|
@ -347,6 +359,8 @@ static int udp_init(void)
|
|||
|
||||
int wifi_transport_init(void)
|
||||
{
|
||||
client_mutex = xSemaphoreCreateMutex();
|
||||
|
||||
int rc = wifi_ap_init();
|
||||
if (rc) {
|
||||
printf("WiFi: AP init failed: %d\n", rc);
|
||||
|
|
|
|||
Loading…
Reference in New Issue