// Diagnostic C-ITS sniffer for Lilygo T-Dongle C5, based on bertrik/its-g5-receiver // (proven fixed-ringbuffer approach, Arduino/pioarduino = ESP-IDF 5.5.5). // Changes vs bertrik: frames+stats on Serial (USB-CDC, the T-Dongle's only USB port), // no NeoPixel/MiniShell, cb counters BEFORE filtering to prove driver RX delivery. #include #include #include #include #include "esp_wifi.h" #include "esp_wifi_secret.h" static const int CHANNEL = 5900; static const size_t QUEUE_SIZE = 8; static const int MAX_PACKET_LEN = 2400; typedef struct { unsigned long millis; int rssi; size_t len; uint8_t data[MAX_PACKET_LEN]; } packet_t; static packet_t queue[QUEUE_SIZE]; static std::atomic writeIndex = 0; static std::atomic readIndex = 0; static std::atomic droppedPackets = 0; // diagnostic counters (incremented in the RX callback, before any filtering) static std::atomic cb_all = 0; // every callback invocation static std::atomic cb_nonmisc = 0; // non-MISC frames static std::atomic its_frames = 0; // broadcast frames enqueued static std::atomic last_rssi = 0; static volatile int armed = 0; static bool enqueue(unsigned long ms, int rssi, size_t length, const uint8_t *data) { if (length > MAX_PACKET_LEN) return false; int w = writeIndex.load(std::memory_order_relaxed); int r = readIndex.load(std::memory_order_acquire); int next = (w + 1) % QUEUE_SIZE; if (next == r) return false; // full packet_t &pkt = queue[w]; pkt.millis = ms; pkt.rssi = rssi; pkt.len = length; memcpy(pkt.data, data, length); writeIndex.store(next, std::memory_order_release); return true; } static bool dequeue(packet_t *pkt) { int r = readIndex.load(std::memory_order_relaxed); int w = writeIndex.load(std::memory_order_acquire); if (r == w) return false; // empty *pkt = queue[r]; readIndex.store((r + 1) % QUEUE_SIZE, std::memory_order_release); return true; } static void wifi_sniffer_cb(void *rawpkt, wifi_promiscuous_pkt_type_t type) { cb_all.fetch_add(1, std::memory_order_relaxed); wifi_promiscuous_pkt_t *pkt = (wifi_promiscuous_pkt_t *)rawpkt; if (pkt->rx_ctrl.sig_len <= 4) return; size_t len = pkt->rx_ctrl.sig_len - 4; if (type == WIFI_PKT_MISC) return; cb_nonmisc.fetch_add(1, std::memory_order_relaxed); last_rssi.store(pkt->rx_ctrl.rssi, std::memory_order_relaxed); if (pkt->rx_ctrl.rx_state) return; // broadcast only (802.11p DA = ff:ff:ff:ff:ff:ff at payload[4]) if (len < (4 + 6) || memcmp(&pkt->payload[4], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) return; its_frames.fetch_add(1, std::memory_order_relaxed); if (!enqueue(millis(), pkt->rx_ctrl.rssi, len, pkt->payload)) { droppedPackets.fetch_add(1, std::memory_order_relaxed); } } void setup(void) { Serial.begin(115200); // USB-CDC (ARDUINO_USB_CDC_ON_BOOT=1) delay(2000); Serial.println("BOOT: T-Dongle C5 ITS-G5 sniffer (bertrik fixed-buffer, IDF5.5)"); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); wifi_promiscuous_filter_t wifi_filter; wifi_filter.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL & ~WIFI_PROMIS_FILTER_MASK_FCSFAIL; ESP_ERROR_CHECK(esp_wifi_set_promiscuous_filter(&wifi_filter)); ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(wifi_sniffer_cb)); ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // C-ITS ITS-G5: 802.11p @ 5900 MHz (ch140), 10 MHz bandwidth. phy_11p_set(1, 0); ESP_ERROR_CHECK(esp_wifi_set_channel(140, WIFI_SECOND_CHAN_NONE)); phy_change_channel(CHANNEL, 1, 0, 0); armed = 1; Serial.println("ARMED: ch140 / 5900 MHz 802.11p, promiscuous on"); } static void print_stats(void) { Serial.printf("STATS cb_all=%lu cb_nonmisc=%lu its=%lu dropped=%lu rssi=%d armed=%d heap=%u\n", (unsigned long)cb_all.load(), (unsigned long)cb_nonmisc.load(), (unsigned long)its_frames.load(), (unsigned long)droppedPackets.load(), last_rssi.load(), armed, (unsigned)ESP.getFreeHeap()); } void loop(void) { static unsigned long last_stats = 0; packet_t pkt; while (dequeue(&pkt)) { Serial.printf("FRAME len=%u rssi=%d ms=%lu:", (unsigned)pkt.len, pkt.rssi, pkt.millis); for (size_t i = 0; i < pkt.len && i < 48; i++) Serial.printf(" %02X", pkt.data[i]); Serial.println(); } if (millis() - last_stats >= 2000) { last_stats = millis(); print_stats(); } }