#include "bridge.h" #include #include #include #include "esp_log.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "lwip/netdb.h" #include "lwip/sockets.h" #include "sdkconfig.h" #include "bridge_proto.h" #if CONFIG_IDF_TARGET_LINUX #include "bridge_serial.h" #else typedef struct bridge_serial bridge_serial_t; // serial boards are linux-only; the device never opens one #endif static const char *TAG = "bridge"; // One running bridge. A slot keeps its UDP tasks once it has them; closing only clears used. typedef struct { bool used; bool serial; // a USB board (bridge_serial), else UDP bool browser; // the rig page of the host build (sim_rig.c), over its WebSocket char addr[BRIDGE_ADDR_MAX]; // as the settings store it bridge_serial_t *board; // serial: the board (linux) int sock; // UDP data socket (port), -1 when closed int ctl; // UDP control socket (port + 1): HELLO/INFO and microphone audio bool rx_task, ctl_task; // this slot's receive tasks exist uint32_t tx, rx, audio; // UDP datagrams sent, received, and audio datagrams received // from the control port: the board's INFO, and when it last answered bool linked; int64_t seen_ms; uint8_t mac[6], channel, caps, ip[4]; } link_t; static link_t s_links[BRIDGE_MAX_LINKS]; static SemaphoreHandle_t s_lock; static bridge_rx_cb_t s_cb; static bridge_audio_cb_t s_audio_cb; static int s_mic_slot = -1; // the UDP board recording (-1: none) // Frames heard by more than one bridge arrive once: a frame seen again within DEDUPE_MS is dropped #define DEDUPE_MS 300 #define DEDUPE_LEN 64 #define CTL_TIMEOUT_MS 3000 static struct { uint32_t hash; int64_t ms; } s_seen[DEDUPE_LEN]; static unsigned s_seen_next; // MACs whose frames came in on the browser bridge: the simulated drivers of the rig page #define BROWSER_MACS_MAX 64 static uint8_t s_browser_macs[BROWSER_MACS_MAX][6]; static int s_n_browser_macs; static bool is_serial_addr(const char *addr) { return strncmp(addr, "/dev/", 5) == 0 || strncmp(addr, "serial:", 7) == 0; } static bool is_browser_addr(const char *addr) { return strcmp(addr, BRIDGE_BROWSER) == 0; } #if CONFIG_IDF_TARGET_LINUX // The rig page's link (sim-controller/components/sim/sim_rig.c): dst_mac(6) + packet to the page, if it is open bool sim_rig_tx(const uint8_t *frame, size_t len); bool sim_rig_page_open(void); #endif static int64_t now_ms(void) { return esp_timer_get_time() / 1000; } static uint32_t fnv1a(const uint8_t *p, size_t n, uint32_t h) { for (size_t i = 0; i < n; i++) { h = (h ^ p[i]) * 16777619u; } return h; } // A frame from any bridge, once static void deliver(const uint8_t *src_mac, const uint8_t *data, int len) { uint32_t h = fnv1a(src_mac, 6, 2166136261u); h = fnv1a(data, (size_t)len, h); int64_t now = now_ms(); for (unsigned i = 0; i < DEDUPE_LEN; i++) { if (s_seen[i].hash == h && now - s_seen[i].ms < DEDUPE_MS) { return; } } s_seen[s_seen_next].hash = h; s_seen[s_seen_next].ms = now; s_seen_next = (s_seen_next + 1) % DEDUPE_LEN; if (s_cb) { s_cb(src_mac, data, len); } } static void rx_task(void *arg) { int i = (int)(intptr_t)arg; uint8_t buf[6 + 256]; for (;;) { int sock = s_links[i].sock; if (sock < 0) { vTaskDelay(pdMS_TO_TICKS(100)); continue; } int n = recv(sock, buf, sizeof(buf), 0); if (n < 0) { vTaskDelay(pdMS_TO_TICKS(100)); continue; } if (n > 6) { s_links[i].rx++; deliver(buf, buf + 6, n - 6); } } } // The control port: INFO answers HELLO, and AUDIO datagrams carry the board's microphone static void ctl_rx_task(void *arg) { int i = (int)(intptr_t)arg; uint8_t buf[1 + BRIDGE_AUDIO_BYTES + 8]; int16_t pcm[BRIDGE_AUDIO_SAMPLES]; for (;;) { int sock = s_links[i].ctl; if (sock < 0) { vTaskDelay(pdMS_TO_TICKS(100)); continue; } int n = recv(sock, buf, sizeof(buf), 0); if (n < 1) { vTaskDelay(pdMS_TO_TICKS(100)); continue; } if (buf[0] == BRIDGE_MSG_INFO && n >= 1 + 24 && buf[1] == BRIDGE_PROTO_VERSION) { const uint8_t *b = buf + 1; if (!s_lock) { continue; } xSemaphoreTake(s_lock, portMAX_DELAY); link_t *l = &s_links[i]; memcpy(l->mac, b + 1, 6); l->channel = b[7]; l->caps = n >= 1 + 25 ? b[24] : 0; if (n >= 1 + BRIDGE_INFO_LEN) { memcpy(l->ip, b + 25, 4); } l->linked = true; l->seen_ms = now_ms(); xSemaphoreGive(s_lock); } else if (buf[0] == BRIDGE_MSG_AUDIO && n == 1 + BRIDGE_AUDIO_BYTES) { for (size_t k = 0; k < BRIDGE_AUDIO_SAMPLES; k++) { pcm[k] = (int16_t)((uint16_t)buf[1 + 2 * k] | ((uint16_t)buf[2 + 2 * k] << 8)); } s_links[i].audio++; if (s_audio_cb) { s_audio_cb(pcm, BRIDGE_AUDIO_SAMPLES); } } } } // Every 500 ms: HELLO to each UDP board every second, the microphone request to the recording board, and the // link state from how recently each board answered static void keepalive_task(void *arg) { (void)arg; unsigned tick = 0; for (;;) { vTaskDelay(pdMS_TO_TICKS(500)); tick++; if (!s_lock) { continue; } xSemaphoreTake(s_lock, portMAX_DELAY); int64_t now = now_ms(); for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { link_t *l = &s_links[i]; if (!l->used || l->serial || l->ctl < 0) { continue; } if (tick % 2 == 0) { uint8_t hello = BRIDGE_MSG_HELLO; send(l->ctl, &hello, 1, 0); } if (l->linked && now - l->seen_ms > CTL_TIMEOUT_MS) { l->linked = false; } if (i == s_mic_slot) { uint8_t on[2] = { BRIDGE_MSG_SET_MIC, 1 }; send(l->ctl, on, sizeof(on), 0); } } xSemaphoreGive(s_lock); } } // host:port: the address, split. Returns false if it does not parse. static bool split_udp(const char *addr, char *host, size_t host_len, int *port) { snprintf(host, host_len, "%s", addr); *port = BRIDGE_DEFAULT_PORT; char *colon = strrchr(host, ':'); if (colon) { *colon = 0; *port = atoi(colon + 1); } return *port > 0 && *port < 65535 && host[0]; } // A connected UDP socket to host:port, or -1. The peer is written to peer_out. static int connect_udp(const char *host, int port, struct sockaddr_in *peer_out) { struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_DGRAM }, *res = NULL; if (getaddrinfo(host, NULL, &hints, &res) != 0 || !res) { ESP_LOGE(TAG, "cannot resolve %s", host); return -1; } struct sockaddr_in peer; memcpy(&peer, res->ai_addr, sizeof(peer)); freeaddrinfo(res); peer.sin_port = htons(port); int s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { return -1; } // connect() fixes the peer, so only its datagrams are received if (connect(s, (struct sockaddr *)&peer, sizeof(peer)) != 0) { close(s); return -1; } if (peer_out) { *peer_out = peer; } return s; } static int find_link(const char *addr) { for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { if (s_links[i].used && strcmp(s_links[i].addr, addr) == 0) { return i; } } return -1; } // Open one bridge into a free slot. Lock held. static esp_err_t activate(const char *addr) { int slot = -1; for (int i = 0; i < BRIDGE_MAX_LINKS && slot < 0; i++) { if (!s_links[i].used) { slot = i; } } if (slot < 0) { return ESP_ERR_NO_MEM; } link_t *l = &s_links[slot]; l->browser = false; if (is_browser_addr(addr)) { #if CONFIG_IDF_TARGET_LINUX l->browser = true; l->serial = false; l->sock = -1; l->ctl = -1; ESP_LOGW(TAG, "ESP-NOW frames go to the rig page (/sim/), not the radio"); #else return ESP_ERR_NOT_SUPPORTED; #endif } else if (is_serial_addr(addr)) { #if CONFIG_IDF_TARGET_LINUX const char *dev = strncmp(addr, "serial:", 7) == 0 ? addr + 7 : addr; l->board = bridge_serial_open(dev, deliver); if (!l->board) { return ESP_FAIL; } l->serial = true; #else return ESP_ERR_NOT_SUPPORTED; #endif } else { char host[64]; int port; if (!split_udp(addr, host, sizeof(host), &port)) { ESP_LOGE(TAG, "bad bridge address %s", addr); return ESP_ERR_INVALID_ARG; } struct sockaddr_in peer; int s = connect_udp(host, port, &peer); if (s < 0) { return ESP_FAIL; } // The control port is the next one up: HELLO and INFO, and microphone audio int c = connect_udp(host, port + 1, NULL); l->sock = s; l->ctl = c; // -1 if it could not open: the link still works, it just reports no board l->serial = false; if (!l->rx_task) { xTaskCreate(rx_task, "bridge_rx", 4096, (void *)(intptr_t)slot, 6, NULL); l->rx_task = true; } if (!l->ctl_task) { xTaskCreate(ctl_rx_task, "bridge_ctl", 4096, (void *)(intptr_t)slot, 6, NULL); l->ctl_task = true; } ESP_LOGW(TAG, "ESP-NOW frames go to the UDP bridge %s, not the radio", addr); } snprintf(l->addr, sizeof(l->addr), "%s", addr); l->tx = l->rx = l->audio = 0; l->linked = false; l->caps = 0; l->used = true; return ESP_OK; } // Close one running bridge. Lock held. static void deactivate(int i) { link_t *l = &s_links[i]; #if CONFIG_IDF_TARGET_LINUX if (l->serial) { bridge_serial_close(l->board); } #endif if (!l->serial && l->sock >= 0) { close(l->sock); l->sock = -1; } if (!l->serial && l->ctl >= 0) { close(l->ctl); l->ctl = -1; } if (s_mic_slot == i) { s_mic_slot = -1; } l->used = false; } static void ensure_tasks(void) { static bool started; if (!started) { started = true; xTaskCreate(keepalive_task, "bridge_keep", 3072, NULL, 4, NULL); } } esp_err_t bridge_set_all(const char addrs[][BRIDGE_ADDR_MAX], size_t n, bridge_rx_cb_t cb) { if (n > BRIDGE_MAX_LINKS) { return ESP_ERR_INVALID_ARG; } if (!s_lock) { s_lock = xSemaphoreCreateMutex(); } xSemaphoreTake(s_lock, portMAX_DELAY); s_cb = cb; for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { if (!s_links[i].used) { continue; } bool wanted = false; for (size_t j = 0; j < n; j++) { wanted = wanted || strcmp(s_links[i].addr, addrs[j]) == 0; } if (!wanted) { deactivate(i); } } esp_err_t first = ESP_OK; for (size_t j = 0; j < n; j++) { if (!addrs[j][0] || find_link(addrs[j]) >= 0) { continue; } esp_err_t err = activate(addrs[j]); if (err != ESP_OK && first == ESP_OK) { first = err; } } xSemaphoreGive(s_lock); ensure_tasks(); return first; } bool bridge_active(void) { for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { if (s_links[i].used) { return true; } } return false; } size_t bridge_list(char addrs[][BRIDGE_ADDR_MAX], size_t max) { size_t n = 0; for (int i = 0; i < BRIDGE_MAX_LINKS && n < max; i++) { if (s_links[i].used) { snprintf(addrs[n], BRIDGE_ADDR_MAX, "%s", s_links[i].addr); n++; } } return n; } esp_err_t bridge_send(const uint8_t *dst_mac, const void *data, size_t len) { if (len > 250) { return ESP_ERR_INVALID_SIZE; } uint8_t buf[6 + 250]; memcpy(buf, dst_mac, 6); memcpy(buf + 6, data, len); bool any = false; if (!s_lock) { return ESP_ERR_INVALID_STATE; } xSemaphoreTake(s_lock, portMAX_DELAY); for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { link_t *l = &s_links[i]; if (!l->used) { continue; } any = true; if (l->browser) { #if CONFIG_IDF_TARGET_LINUX if (sim_rig_tx(buf, 6 + len)) { // no page open: the frame is lost, as on air l->tx++; } #endif } else if (l->serial) { #if CONFIG_IDF_TARGET_LINUX bridge_serial_send(l->board, dst_mac, data, len); // a closed port loses the frame, as on air #endif } else if (l->sock >= 0) { // A refused datagram (the simulator is not running yet) is a lost frame, as on air send(l->sock, buf, 6 + len, 0); l->tx++; } } xSemaphoreGive(s_lock); return any ? ESP_OK : ESP_ERR_INVALID_STATE; } size_t bridge_status_json(char *out, size_t n) { size_t used = (size_t)snprintf(out, n, "["); bool first = true; if (!s_lock) { return used + (size_t)snprintf(out + used, n > used ? n - used : 0, "]"); } xSemaphoreTake(s_lock, portMAX_DELAY); for (int i = 0; i < BRIDGE_MAX_LINKS && used < n; i++) { link_t *l = &s_links[i]; if (!l->used) { continue; } char body[400]; if (l->browser) { #if CONFIG_IDF_TARGET_LINUX snprintf(body, sizeof(body), "{\"page\":%s,\"tx\":%lu,\"rx\":%lu}", sim_rig_page_open() ? "true" : "false", (unsigned long)l->tx, (unsigned long)l->rx); #else snprintf(body, sizeof(body), "{}"); #endif } else if (l->serial) { #if CONFIG_IDF_TARGET_LINUX bridge_serial_status(l->board, body, sizeof(body)); #else snprintf(body, sizeof(body), "{}"); #endif } else { char ip[24] = "null"; if (l->ip[0] || l->ip[1] || l->ip[2] || l->ip[3]) { snprintf(ip, sizeof(ip), "\"%u.%u.%u.%u\"", l->ip[0], l->ip[1], l->ip[2], l->ip[3]); } snprintf(body, sizeof(body), "{\"port\":null,\"linked\":%s,\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"channel\":%u," "\"mic\":%s,\"ip\":%s,\"tx\":%lu,\"rx\":%lu,\"audio\":%lu}", l->linked ? "true" : "false", l->mac[0], l->mac[1], l->mac[2], l->mac[3], l->mac[4], l->mac[5], (unsigned)l->channel, (l->caps & BRIDGE_CAP_MIC) ? "true" : "false", ip, (unsigned long)l->tx, (unsigned long)l->rx, (unsigned long)l->audio); } int w = snprintf(out + used, n - used, "%s{\"addr\":\"%s\",\"kind\":\"%s\",\"status\":%s}", first ? "" : ",", l->addr, l->browser ? "browser" : (l->serial ? "serial" : "udp"), body); if (w < 0 || (size_t)w >= n - used) { break; } used += (size_t)w; first = false; } xSemaphoreGive(s_lock); if (used + 2 <= n) { out[used++] = ']'; out[used] = 0; } return used; } size_t bridge_ports_json(char *out, size_t n) { #if CONFIG_IDF_TARGET_LINUX return bridge_serial_ports_json(out, n); #else return (size_t)snprintf(out, n, "[]"); #endif } esp_err_t bridge_board_set_channel(const char *addr, uint8_t channel) { #if CONFIG_IDF_TARGET_LINUX if (!s_lock) { return ESP_ERR_NOT_FOUND; } xSemaphoreTake(s_lock, portMAX_DELAY); int i = find_link(addr); esp_err_t err = (i >= 0 && s_links[i].serial) ? bridge_serial_set_channel(s_links[i].board, channel) : ESP_ERR_NOT_FOUND; xSemaphoreGive(s_lock); return err; #else (void)addr; (void)channel; return ESP_ERR_NOT_SUPPORTED; #endif } void bridge_browser_rx(const uint8_t *frame, int len) { if (len <= 6 || !s_lock) { return; } bool on = false; xSemaphoreTake(s_lock, portMAX_DELAY); for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { if (s_links[i].used && s_links[i].browser) { s_links[i].rx++; on = true; } } bool known = false; for (int i = 0; i < s_n_browser_macs && !known; i++) { known = memcmp(s_browser_macs[i], frame, 6) == 0; } if (on && !known && s_n_browser_macs < BROWSER_MACS_MAX) { memcpy(s_browser_macs[s_n_browser_macs++], frame, 6); } xSemaphoreGive(s_lock); if (on) { // with no browser bridge in the list, the page's frames are not heard deliver(frame, frame + 6, len - 6); } } bool bridge_mac_is_browser(const uint8_t mac[6]) { bool hit = false; if (!s_lock) { return false; } xSemaphoreTake(s_lock, portMAX_DELAY); for (int i = 0; i < s_n_browser_macs && !hit; i++) { hit = memcmp(s_browser_macs[i], mac, 6) == 0; } xSemaphoreGive(s_lock); return hit; } bool bridge_has_browser(void) { bool on = false; if (!s_lock) { return false; } xSemaphoreTake(s_lock, portMAX_DELAY); for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { on = on || (s_links[i].used && s_links[i].browser); } xSemaphoreGive(s_lock); return on; } void bridge_set_audio_cb(bridge_audio_cb_t cb) { s_audio_cb = cb; #if CONFIG_IDF_TARGET_LINUX bridge_serial_set_audio_cb(cb); #endif } static bool serial_has_mic(void) { #if CONFIG_IDF_TARGET_LINUX return bridge_serial_has_mic(); #else return false; #endif } bool bridge_has_mic(void) { if (serial_has_mic()) { return true; } bool any = false; if (!s_lock) { return false; } xSemaphoreTake(s_lock, portMAX_DELAY); for (int i = 0; i < BRIDGE_MAX_LINKS; i++) { any = any || (s_links[i].used && !s_links[i].serial && s_links[i].linked && (s_links[i].caps & BRIDGE_CAP_MIC)); } xSemaphoreGive(s_lock); return any; } // The microphone comes from a USB board when one has a mic, else from the first network board that does esp_err_t bridge_mic(bool on) { if (!s_lock) { return ESP_ERR_INVALID_STATE; } xSemaphoreTake(s_lock, portMAX_DELAY); esp_err_t err = ESP_OK; if (!on) { if (s_mic_slot >= 0 && s_links[s_mic_slot].used && s_links[s_mic_slot].ctl >= 0) { uint8_t off[2] = { BRIDGE_MSG_SET_MIC, 0 }; send(s_links[s_mic_slot].ctl, off, sizeof(off), 0); } s_mic_slot = -1; } else if (serial_has_mic()) { s_mic_slot = -1; err = ESP_OK; } else { int pick = -1; for (int i = 0; i < BRIDGE_MAX_LINKS && pick < 0; i++) { link_t *l = &s_links[i]; if (l->used && !l->serial && l->linked && (l->caps & BRIDGE_CAP_MIC) && l->ctl >= 0) { pick = i; } } if (pick < 0) { err = ESP_ERR_NOT_SUPPORTED; } else { s_mic_slot = pick; uint8_t onb[2] = { BRIDGE_MSG_SET_MIC, 1 }; send(s_links[pick].ctl, onb, sizeof(onb), 0); } } xSemaphoreGive(s_lock); #if CONFIG_IDF_TARGET_LINUX if (!on || s_mic_slot < 0) { bridge_serial_mic(on && serial_has_mic()); } else { bridge_serial_mic(false); } #endif return err; }