From 547dea26e31477c32bcaafa0683197895b0d2b52 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Thu, 19 Nov 2026 00:24:44 +0100 Subject: [PATCH] mcu-its-g5: decoded header as JSON on mcu-its-g5/decoded, 1 Hz per station Message type, station type and id, position, speed and heading from the GeoNetworking header, so consumers don't need an ASN.1 decoder. Outside its//#, so the EMQX bridge keeps it off OpenTrafficMap. --- common/its_g5/decode.h | 25 +++++++++++++++++++++++++ common/its_g5/decode_test.cpp | 2 ++ mcu-its-g5.yaml | 12 ++++++++++++ 3 files changed, 39 insertions(+) diff --git a/common/its_g5/decode.h b/common/its_g5/decode.h index 93dc646..3136ada 100644 --- a/common/its_g5/decode.h +++ b/common/its_g5/decode.h @@ -13,6 +13,7 @@ namespace its_g5_decode { struct Info { uint16_t port{0}; // BTP destination port, 0 = none uint8_t station_type{0}; // ETSI station type from the GN address + uint64_t addr{0}; // GN address (M bit, station type, MID), identifies the station bool secured{false}; // carried in an ETSI TS 103 097 signed packet (signature not verified) double lat{0}, lon{0}; // degrees float speed_kmh{0}; @@ -116,6 +117,8 @@ inline bool parse(const std::vector &frame, Info &out) { // Long position vector: GN address (8), timestamp (4), lat, lon (1/10 µdeg), PAI | speed, heading out.station_type = (f[pv] >> 2) & 0x1F; + for (size_t k = 0; k < 8; k++) + out.addr = (out.addr << 8) | f[pv + k]; out.lat = be32(f + pv + 12) / 1e7; out.lon = be32(f + pv + 16) / 1e7; int16_t speed = be16(f + pv + 20) & 0x7FFF; @@ -151,4 +154,26 @@ inline bool handle(const std::vector &frame) { return true; } +// Rate limit per station: true if addr was not let through in the last period_ms. +// ponytail: 32 slots, the oldest is evicted; with more stations in range some publish more often. +inline bool due(uint64_t addr, uint32_t now, uint32_t period_ms) { + static struct { + uint64_t addr; + uint32_t ms; + } slots[32]{}; + auto *slot = &slots[0]; + for (auto &s : slots) { + if (s.addr == addr) { + slot = &s; + break; + } + if (now - s.ms > now - slot->ms) + slot = &s; + } + if (slot->addr == addr && now - slot->ms < period_ms) + return false; + *slot = {addr, now}; + return true; +} + } // namespace its_g5_decode diff --git a/common/its_g5/decode_test.cpp b/common/its_g5/decode_test.cpp index 4631a88..0f109d1 100644 --- a/common/its_g5/decode_test.cpp +++ b/common/its_g5/decode_test.cpp @@ -25,7 +25,9 @@ int main() { assert(i.secured && i.port == 2001 && i.station_type == 5); assert(std::fabs(i.lat - 48.1528384) < 1e-7 && std::fabs(i.lon - 17.1203168) < 1e-7); assert(std::fabs(i.speed_kmh - 50.04f) < 0.01f && std::fabs(i.heading - 123.4f) < 0.01f); + assert(i.addr == 0x1400010203040506ULL); assert(handle(f) && stats.cam == 1); + assert(due(i.addr, 1000, 1000) && !due(i.addr, 1999, 1000) && due(2, 1999, 1000) && due(i.addr, 2000, 1000)); f[30] = 0x48; assert(!parse(f, i)); // not GeoNetworking std::puts("ok"); } diff --git a/mcu-its-g5.yaml b/mcu-its-g5.yaml index afbac89..3017430 100644 --- a/mcu-its-g5.yaml +++ b/mcu-its-g5.yaml @@ -137,6 +137,18 @@ its_g5_receiver: ESP_LOGD("its_g5", "%s from %s %.6f,%.6f %.0f km/h %.0f deg%s, rssi %.0f dBm", its_g5_decode::port_name(i.port), its_g5_decode::station_name(i.station_type), i.lat, i.lon, i.speed_kmh, i.heading, i.secured ? ", signed" : "", rssi); + // Decoded header for HA/maptool, 1 Hz per station (CAMs come up to 10 Hz per vehicle) + if (!its_g5_decode::due(i.addr, millis(), 1000)) + return; + char json[192]; + int n = snprintf(json, sizeof(json), + "{\"msg\":\"%s\",\"station\":\"%s\",\"id\":\"%016llx\",\"lat\":%.7f,\"lon\":%.7f," + "\"speed\":%.1f,\"heading\":%.1f,\"secured\":%s,\"rssi\":%.0f}", + its_g5_decode::port_name(i.port), its_g5_decode::station_name(i.station_type), + (unsigned long long) i.addr, i.lat, i.lon, i.speed_kmh, i.heading, + i.secured ? "true" : "false", rssi); + // outside its/${node_id}/#, so the EMQX bridge keeps it off OpenTrafficMap + id(mqtt_client).publish("mcu-${device}/decoded", json, n, 0, false); sensor: - platform: its_g5_receiver