// The ESP32-P4-Nano's C6 as the controller's ESP-NOW radio, and the UDP bridge service on top of it. // See bridge_hosted.h and bridge/main/radio_proto.h. #include "sdkconfig.h" #if CONFIG_IDF_TARGET_ESP32P4 #include "bridge_hosted.h" #include #include #include "eh_host_feat_peer_data.h" #include "esp_hosted.h" #include "esp_log.h" #include "esp_mac.h" #include "esp_netif.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "lwip/sockets.h" #include "bridge_proto.h" #include "c6_update.h" #include "mic.h" #include "radio_proto.h" static const char *TAG = "bridge_c6"; #define TXQ_LEN 64 #define DESKTOP_HOLD_MS 2000 // the desktop owns the radio until it has been quiet this long #define INFO_TIMEOUT_MS 3000 #define MIC_UDP_TIMEOUT_MS 2000 typedef struct { uint8_t mac[6]; uint8_t len; uint8_t data[BRIDGE_PKT_MAX]; } pkt_t; static QueueHandle_t s_txq; static bridge_rx_cb_t s_rx_cb; static bool s_started; // The C6's last INFO static SemaphoreHandle_t s_lock; static uint8_t s_info[BRIDGE_INFO_LEN]; static int64_t s_info_ms = -1; static uint32_t s_tx_msgs, s_tx_frames, s_rx_frames, s_tx_drop, s_own_dropped; // The UDP service: the desktop that last sent a frame, and the one asking for microphone audio static int s_udp = -1, s_ctl = -1; static struct sockaddr_in s_desk; static volatile int64_t s_desk_ms = -1; static struct sockaddr_in s_mic_peer; static volatile int64_t s_mic_ms = -1; static bool s_desk_logged; static int64_t now_ms(void) { return esp_timer_get_time() / 1000; } static bool desktop_active(void) { int64_t t = s_desk_ms; return t >= 0 && now_ms() - t < DESKTOP_HOLD_MS; } /* ---- the C6 ------------------------------------------------------------------------------------- */ static void on_rx(uint32_t id, const uint8_t *data, size_t len, void *ctx) { (void)id; (void)ctx; bool desk = desktop_active(); uint8_t up[6 + BRIDGE_PKT_MAX]; size_t off = 0; while (off + RADIO_ENTRY_HDR <= len) { uint8_t n = data[off + 6]; if (n == 0 || n > BRIDGE_PKT_MAX || off + RADIO_ENTRY_HDR + n > len) { break; } const uint8_t *mac = data + off; const uint8_t *pkt = data + off + RADIO_ENTRY_HDR; s_rx_frames++; if (s_rx_cb) { s_rx_cb(mac, pkt, n); } if (desk && s_udp >= 0) { memcpy(up, mac, 6); memcpy(up + 6, pkt, n); sendto(s_udp, up, 6 + n, 0, (const struct sockaddr *)&s_desk, sizeof(s_desk)); } off += RADIO_ENTRY_HDR + n; } } static void on_info(uint32_t id, const uint8_t *data, size_t len, void *ctx) { (void)id; (void)ctx; if (len < 24 || data[0] != BRIDGE_PROTO_VERSION) { return; } xSemaphoreTake(s_lock, portMAX_DELAY); memset(s_info, 0, sizeof(s_info)); memcpy(s_info, data, len < sizeof(s_info) ? len : sizeof(s_info)); s_info_ms = now_ms(); xSemaphoreGive(s_lock); } static void ctl_send(uint8_t type, const uint8_t *body, size_t n) { uint8_t m[8]; m[0] = type; if (n) { memcpy(m + 1, body, n); } eh_host_peer_data_send(RADIO_MSG_CTL, m, 1 + n); } // Packs every queued frame into one peer-data message: each costs an SDIO round trip static void tx_task(void *arg) { (void)arg; static uint8_t batch[RADIO_BATCH_MAX]; pkt_t p; for (;;) { xQueueReceive(s_txq, &p, portMAX_DELAY); size_t n = 0, frames = 0; do { memcpy(batch + n, p.mac, 6); batch[n + 6] = p.len; memcpy(batch + n + RADIO_ENTRY_HDR, p.data, p.len); n += RADIO_ENTRY_HDR + p.len; frames++; } while (n + RADIO_ENTRY_HDR + BRIDGE_PKT_MAX <= sizeof(batch) && xQueueReceive(s_txq, &p, 0) == pdTRUE); if (eh_host_peer_data_send(RADIO_MSG_TX, batch, n) == ESP_OK) { s_tx_msgs++; s_tx_frames += frames; } else { s_tx_drop += frames; } } } static esp_err_t queue_frame(const uint8_t *dst, const void *data, size_t len) { if (!s_started) { return ESP_ERR_INVALID_STATE; } if (len == 0 || len > BRIDGE_PKT_MAX) { return ESP_ERR_INVALID_SIZE; } pkt_t p; memcpy(p.mac, dst, 6); p.len = (uint8_t)len; memcpy(p.data, data, len); if (xQueueSend(s_txq, &p, 0) != pdTRUE) { s_tx_drop++; return ESP_ERR_NO_MEM; } return ESP_OK; } esp_err_t bridge_hosted_send(const uint8_t *dst_mac, const void *data, size_t len) { if (desktop_active()) { s_own_dropped++; return ESP_OK; // the desktop has the radio: lost, as if on air } return queue_frame(dst_mac, data, len); } esp_err_t bridge_hosted_set_channel(uint8_t channel) { if (!s_started || channel < 1 || channel > 13) { return ESP_ERR_INVALID_ARG; } ctl_send(BRIDGE_MSG_SET_CHANNEL, &channel, 1); return ESP_OK; } void bridge_hosted_set_rx(bridge_rx_cb_t cb) { s_rx_cb = cb; } // HELLO every second keeps the INFO fresh; the desktop hand-over is logged here, once each way static void keepalive_task(void *arg) { (void)arg; for (;;) { ctl_send(BRIDGE_MSG_HELLO, NULL, 0); bool desk = desktop_active(); if (desk != s_desk_logged) { s_desk_logged = desk; if (desk) { ESP_LOGW(TAG, "desktop " IPSTR " uses the radio: this controller's frames pause", IP2STR((esp_ip4_addr_t *)&s_desk.sin_addr.s_addr)); } else { ESP_LOGI(TAG, "desktop gone quiet: this controller has the radio again"); } } vTaskDelay(pdMS_TO_TICKS(1000)); } } size_t bridge_hosted_status(char *out, size_t n) { uint8_t info[BRIDGE_INFO_LEN]; int64_t seen; xSemaphoreTake(s_lock, portMAX_DELAY); memcpy(info, s_info, sizeof(info)); seen = s_info_ms; xSemaphoreGive(s_lock); bool linked = seen >= 0 && now_ms() - seen < INFO_TIMEOUT_MS; uint32_t c6_tx = info[8] | info[9] << 8 | info[10] << 16 | (uint32_t)info[11] << 24; uint32_t c6_fail = info[12] | info[13] << 8 | info[14] << 16 | (uint32_t)info[15] << 24; uint32_t c6_rx = info[16] | info[17] << 8 | info[18] << 16 | (uint32_t)info[19] << 24; uint32_t c6_drop = info[20] | info[21] << 8 | info[22] << 16 | (uint32_t)info[23] << 24; char desk[24] = "null"; if (desktop_active()) { snprintf(desk, sizeof(desk), "\"" IPSTR "\"", IP2STR((esp_ip4_addr_t *)&s_desk.sin_addr.s_addr)); } return (size_t)snprintf(out, n, "{\"port\":null,\"linked\":%s,\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"channel\":%u,\"mic\":false," "\"ip\":null,\"tx\":%lu,\"rx\":%lu,\"tx_fail\":%lu,\"drop\":%lu,\"sdio_msgs\":%lu,\"sdio_drop\":%lu," "\"paused\":%lu,\"desktop\":%s}", linked ? "true" : "false", info[1], info[2], info[3], info[4], info[5], info[6], (unsigned)info[7], (unsigned long)c6_tx, (unsigned long)c6_rx, (unsigned long)c6_fail, (unsigned long)c6_drop, (unsigned long)s_tx_msgs, (unsigned long)s_tx_drop, (unsigned long)s_own_dropped, desk); } esp_err_t bridge_hosted_start(void) { if (s_started) { return ESP_OK; } s_lock = xSemaphoreCreateMutex(); s_txq = xQueueCreate(TXQ_LEN, sizeof(pkt_t)); if (!s_lock || !s_txq) { return ESP_ERR_NO_MEM; } int rc = esp_hosted_connect_to_slave(); if (rc != 0) { ESP_LOGE(TAG, "C6 not reachable over SDIO (%d): no ESP-NOW and no Wi-Fi", rc); return ESP_FAIL; } c6_update_if_needed(); // restarts the board if it updated the C6 if (eh_host_peer_data_register(RADIO_MSG_RX, on_rx, NULL) != ESP_OK || eh_host_peer_data_register(RADIO_MSG_INFO, on_info, NULL) != ESP_OK) { ESP_LOGE(TAG, "peer data handlers not registered"); return ESP_FAIL; } s_started = true; xTaskCreate(tx_task, "c6_tx", 4096, NULL, 6, NULL); xTaskCreate(keepalive_task, "c6_keep", 3072, NULL, 4, NULL); ESP_LOGI(TAG, "C6 radio link up"); return ESP_OK; } /* ---- the UDP bridge service for a desktop ---------------------------------------------------------- */ static void build_udp_info(uint8_t *b) { xSemaphoreTake(s_lock, portMAX_DELAY); memcpy(b, s_info, BRIDGE_INFO_LEN); xSemaphoreGive(s_lock); b[0] = BRIDGE_PROTO_VERSION; b[24] = BRIDGE_CAP_MIC; memset(b + 25, 0, 4); esp_netif_t *eth = esp_netif_get_handle_from_ifkey("ETH_DEF"); esp_netif_ip_info_t ip; if (eth && esp_netif_get_ip_info(eth, &ip) == ESP_OK && ip.ip.addr) { b[24] |= BRIDGE_CAP_ETH; memcpy(b + 25, &ip.ip.addr, 4); // network order: a.b.c.d } } static void udp_task(void *arg) { (void)arg; uint8_t buf[6 + BRIDGE_PKT_MAX]; for (;;) { struct sockaddr_in from; socklen_t fl = sizeof(from); int n = recvfrom(s_udp, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl); if (n <= 6) { if (n < 0) { vTaskDelay(pdMS_TO_TICKS(10)); } continue; } s_desk = from; s_desk_ms = now_ms(); queue_frame(buf, buf + 6, (size_t)(n - 6)); } } // The microphone to a desktop that asked for it (SET_MIC 1, repeated while it records) static void mic_udp_task(void *arg) { (void)arg; uint8_t out[1 + BRIDGE_AUDIO_BYTES]; out[0] = BRIDGE_MSG_AUDIO; bool open = false; for (;;) { int64_t t = s_mic_ms; bool want = t >= 0 && now_ms() - t < MIC_UDP_TIMEOUT_MS; if (want && !open) { open = mic_stream_open() == ESP_OK; // busy while the web UI streams: try again later } else if (!want && open) { mic_stream_close(); open = false; } if (!open) { vTaskDelay(pdMS_TO_TICKS(100)); continue; } size_t got = 0; while (got < BRIDGE_AUDIO_BYTES) { size_t r = mic_stream_read(out + 1 + got, BRIDGE_AUDIO_BYTES - got, pdMS_TO_TICKS(100)); if (r == 0) { break; } got += r; } if (got == BRIDGE_AUDIO_BYTES) { sendto(s_ctl, out, sizeof(out), 0, (const struct sockaddr *)&s_mic_peer, sizeof(s_mic_peer)); } } } static void ctl_task(void *arg) { (void)arg; uint8_t buf[32]; for (;;) { struct sockaddr_in from; socklen_t fl = sizeof(from); int n = recvfrom(s_ctl, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl); if (n < 1) { vTaskDelay(pdMS_TO_TICKS(10)); continue; } if (buf[0] == BRIDGE_MSG_HELLO) { uint8_t out[1 + BRIDGE_INFO_LEN]; out[0] = BRIDGE_MSG_INFO; build_udp_info(out + 1); sendto(s_ctl, out, sizeof(out), 0, (struct sockaddr *)&from, fl); } else if (buf[0] == BRIDGE_MSG_SET_MIC && n >= 2) { s_mic_peer = from; s_mic_ms = buf[1] ? now_ms() : -1; } else if (buf[0] == BRIDGE_MSG_SET_CHANNEL && n >= 2) { bridge_hosted_set_channel(buf[1]); } } } static int udp_bind(int port) { int s = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr.s_addr = htonl(INADDR_ANY) }; if (s < 0 || bind(s, (struct sockaddr *)&a, sizeof(a)) != 0) { ESP_LOGE(TAG, "UDP port %d not open", port); if (s >= 0) { close(s); } return -1; } return s; } void bridge_hosted_udp_start(void) { if (s_udp >= 0 || !s_started) { return; } s_udp = udp_bind(BRIDGE_DEFAULT_PORT); s_ctl = udp_bind(BRIDGE_DEFAULT_PORT + 1); if (s_udp < 0 || s_ctl < 0) { return; } xTaskCreate(udp_task, "c6_udp", 4096, NULL, 5, NULL); xTaskCreate(ctl_task, "c6_udp_ctl", 4096, NULL, 5, NULL); xTaskCreate(mic_udp_task, "c6_udp_mic", 3072, NULL, 5, NULL); ESP_LOGI(TAG, "network bridge on UDP %d (control %d) for a desktop led-controller", BRIDGE_DEFAULT_PORT, BRIDGE_DEFAULT_PORT + 1); } #endif // CONFIG_IDF_TARGET_ESP32P4