C-ITS Sniffer v2: ST7735-Display, SPI2-Fix, Single-Core-Fix, 5-min-Zähler

- ST7735 SPI-Treiber (80x160, 5x7-Font) mit Debug-Panel (5MIN/FRM/DROP/SRC/FREE/CH)
- SPI2_HOST statt SPI1_HOST (SPI1 = Flash-reserviert auf ESP32-C5)
- xTaskCreate statt xTaskCreatePinnedToCore (C5 ist Single-Core)
- SD-Karte (SPI 1-bit, 32 GB, FAT32) + Flash-storage-Fallback
- Serial-REPL: stats, seron, seroff, reboot
- APA102 RGB-Status-LED (blau=boot, gruen=armed, rot=fehler)
- Build: ESP-IDF v6.1-dev, RISC-V, ~920 KB

Replaced old block1-4 prototype with full modular firmware.
This commit is contained in:
clawdia committed 2026-11-10 10:21:23 +01:00
1 parent b852826f5c
commit d1b3d1bae6
24 files changed
+1552 -698

No files matched your search

+232
View File
@@ -0,0 +1,232 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_heap_caps.h"
#include "esp_timer.h"
#include "esp_err.h"
#include "phy_11p.h"
#include "cits_parse.h"
#include "cits_store.h"
#include "cits_rx.h"
static const char *TAG = "cits_rx";
#define FRAME_MAX 2400
#define Q_DEPTH 16
typedef struct {
uint16_t len;
uint8_t data[FRAME_MAX];
} frame_t;
static QueueHandle_t s_q = NULL;
static volatile uint64_t s_rx = 0;
static volatile uint64_t s_drop = 0;
static volatile uint64_t s_cits = 0;
static volatile bool s_serial = true;
/* 5-minute sliding window of C-ITS ASdu counts (10 buckets x 30s). */
#define WB_N 10
#define WB_SEC 30
static uint32_t wb_cnt[WB_N] = {0};
static uint32_t wb_pid[WB_N] = {0};
static void cits_rx_event(void)
{
uint32_t pid = (uint32_t)(esp_timer_get_time() / 1000000ULL) / WB_SEC;
int idx = pid % WB_N;
if (wb_pid[idx] != pid) { wb_pid[idx] = pid; wb_cnt[idx] = 1; }
else { wb_cnt[idx]++; }
}
uint32_t cits_rx_count_5min(void)
{
uint32_t cur = (uint32_t)(esp_timer_get_time() / 1000000ULL) / WB_SEC;
uint32_t sum = 0;
for (int i = 0; i < WB_N; i++) {
if (wb_pid[i] != 0 && wb_pid[i] >= cur - (WB_N - 1)) {
sum += wb_cnt[i];
}
}
return sum;
}
/* Promiscuous RX callback (driver context) - copy fast, defer parsing. */
static void on_rx(void *buf, wifi_promiscuous_pkt_type_t type)
{
if (type != WIFI_PKT_MGMT && type != WIFI_PKT_CTRL && type != WIFI_PKT_DATA) {
return; /* WIFI_PKT_MISC: not a real frame */
}
wifi_promiscuous_pkt_t *p = (wifi_promiscuous_pkt_t *)buf;
if (p->rx_ctrl.rx_state != 0) {
return; /* errored frame */
}
uint16_t len = (uint16_t)p->rx_ctrl.sig_len; /* includes FCS */
if (len > FRAME_MAX) {
len = FRAME_MAX;
}
frame_t *f = (frame_t *)heap_caps_malloc(sizeof(frame_t), MALLOC_CAP_DEFAULT);
if (!f) {
s_drop++;
return;
}
f->len = len;
memcpy(f->data, p->payload, len);
if (xQueueSend(s_q, f, 0) != pdPASS) {
heap_caps_free(f);
s_drop++;
} else {
s_rx++;
}
}
static void hexn(const uint8_t *d, int n, char *out, int outsz)
{
static const char *H = "0123456789abcdef";
int i;
for (i = 0; i < n && (2 * i + 2) <= outsz; i++) {
out[2 * i] = H[d[i] >> 4];
out[2 * i + 1] = H[d[i] & 0x0F];
}
out[2 * i] = '\0';
}
static void process_task(void *arg)
{
(void)arg;
for (;;) {
frame_t *f = NULL;
if (xQueueReceive(s_q, &f, portMAX_DELAY) != pdPASS) {
continue;
}
if (!f) {
continue;
}
ieee80211_hdr_t h;
bool ok = (cits_parse_80211(f->data, f->len, &h) == ESP_OK);
cits_msg_t m;
bool have = false;
if (ok && h.is_data && h.payload && h.payload_len >= 7) {
if (cits_parse_asdu(h.payload, h.payload_len, &m) == ESP_OK && m.type != CITS_UNKNOWN) {
have = true;
s_cits++;
cits_rx_event();
}
}
uint64_t now_ms = (uint64_t)(esp_timer_get_time() / 1000);
if (s_serial) {
if (have) {
ESP_LOGI(TAG, "%-5s id=0x%02x site=%lu ph=%u st0=%u len=%u sa=%02x:%02x:%02x:%02x:%02x:%02x",
cits_type_name(m.type), m.asdu_id, (unsigned long)m.site_id,
m.num_phases, m.phase0_state, h.payload_len,
h.sa[0], h.sa[1], h.sa[2], h.sa[3], h.sa[4], h.sa[5]);
} else if (ok) {
ESP_LOGI(TAG, "80211 t=%u st=%u len=%u sa=%02x:%02x:%02x:%02x:%02x:%02x",
h.type, h.subtype, f->len,
h.sa[0], h.sa[1], h.sa[2], h.sa[3], h.sa[4], h.sa[5]);
}
}
if (cits_store_ready()) {
char hex[2402];
int hn = (int)f->len;
if (hn > 1200) {
hn = 1200;
}
hexn(f->data, hn, hex, sizeof(hex));
char rec[2600];
if (have) {
snprintf(rec, sizeof(rec), "%llu,%s,0x%02x,%u,%lu,%u,%u,%lu,%s",
(unsigned long long)now_ms, cits_type_name(m.type), m.asdu_id,
m.asdu_version, (unsigned long)m.site_id, m.num_phases, m.phase0_state,
(unsigned long)m.station_id, hex);
} else {
const char *cls = ok ? (h.is_data ? "data" : "mgmt") : "raw";
snprintf(rec, sizeof(rec), "%llu,%s,0x00,0,0,0,0,0,%s",
(unsigned long long)now_ms, cls, hex);
}
cits_store_log(rec);
}
heap_caps_free(f);
}
}
esp_err_t cits_rx_start(void)
{
s_q = xQueueCreate(Q_DEPTH, sizeof(frame_t));
if (!s_q) {
return ESP_ERR_NO_MEM;
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_err_t e = esp_wifi_init(&cfg);
if (e != ESP_OK && e != ESP_ERR_INVALID_STATE) {
return e;
}
e = esp_wifi_set_storage(WIFI_STORAGE_RAM);
if (e != ESP_OK) {
return e;
}
e = esp_wifi_set_mode(WIFI_MODE_NULL);
if (e != ESP_OK) {
return e;
}
e = esp_wifi_start();
if (e != ESP_OK) {
return e;
}
wifi_promiscuous_filter_t filt;
filt.filter_mask = WIFI_PROMIS_FILTER_MASK_ALL;
esp_wifi_set_promiscuous_filter(&filt);
e = esp_wifi_set_promiscuous_rx_cb(on_rx);
if (e != ESP_OK) {
return e;
}
/* --- The 5.9 GHz / 802.11p "secret command" sequence ---
* (undocumented libphy.a APIs: phy_11p_set + phy_change_channel). */
e = esp_wifi_set_promiscuous(true);
if (e != ESP_OK) {
return e;
}
phy_11p_set(1, 0);
e = esp_wifi_set_channel(140, WIFI_SECOND_CHAN_NONE);
if (e != ESP_OK) {
return e;
}
phy_change_channel(5900, 1, 0, 0);
/* ESP32-C5 is single-core: use xTaskCreate (no core pinning). */
if (xTaskCreate(process_task, "cits_proc", 8192, NULL, 5, NULL) != pdPASS) {
return ESP_FAIL;
}
ESP_LOGI(TAG, "armed: 5.9 GHz C-ITS promiscuous capture (ch 140 / 5900 MHz)");
return ESP_OK;
}
void cits_rx_set_serial(bool on)
{
s_serial = on;
}
uint64_t cits_rx_frames(void)
{
return s_rx;
}
uint64_t cits_rx_cits(void)
{
return s_cits;
}
uint64_t cits_rx_drops(void)
{
return s_drop;
}