From 6633cd6f92f20c4b26a4318de7ac44caf860ece5 Mon Sep 17 00:00:00 2001 From: Lars Nolden Date: Tue, 1 Sep 2026 11:31:26 +0200 Subject: [PATCH] hil: wmbusmeters link-layer cross-check; firmware duty-cycle accounting --- firmware/main/repeater.c | 12 ++++++++++-- firmware/main/repeater.h | 1 + hil/run.sh | 25 +++++++++++++++++++++++++ hil/verify_frame_semantics.sh | 31 +++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100755 hil/verify_frame_semantics.sh diff --git a/firmware/main/repeater.c b/firmware/main/repeater.c index d9d1dac..f695fc7 100644 --- a/firmware/main/repeater.c +++ b/firmware/main/repeater.c @@ -237,6 +237,9 @@ static const char *do_tx(const uint8_t *payload, int payload_len, } if (irq & SX126X_IRQ_TX_DONE) { g_stats.tx_success++; + if (*tx_done_us > *tx_start_us) { + g_stats.tx_airtime_us += (uint64_t)(*tx_done_us - *tx_start_us); + } return notify_seen ? "ok" : "ok_polled"; } if (irq & SX126X_IRQ_TIMEOUT) { @@ -613,14 +616,19 @@ void repeater_print_stats(void) "\"tx_timeout\":%" PRIu32 ",\"tx_failures\":%" PRIu32 "," "\"tx_suppressed_dup\":%" PRIu32 ",\"tx_suppressed_whitelist\":%" PRIu32 "," "\"radio_reinit\":%" PRIu32 ",\"fem_reinit\":%" PRIu32 "," - "\"recoveries\":%" PRIu32, + "\"recoveries\":%" PRIu32 ",\"tx_airtime_us\":%llu," + "\"tx_duty_cycle_ppm\":%llu", (long long)esp_timer_get_time(), g_stats.rx_candidates, g_stats.rx_valid, g_stats.rx_crc_fail, g_stats.rx_not_c1, g_stats.rx_len_invalid, g_stats.rx_bytes, g_stats.tx_attempts, g_stats.tx_success, g_stats.tx_timeout, g_stats.tx_failures, g_stats.tx_suppressed_dup, g_stats.tx_suppressed_whitelist, g_stats.radio_reinit, - g_stats.fem_reinit, g_stats.recoveries); + g_stats.fem_reinit, g_stats.recoveries, + (unsigned long long)g_stats.tx_airtime_us, + (unsigned long long)(esp_timer_get_time() > 0 + ? (g_stats.tx_airtime_us * 1000000ULL) / (uint64_t)esp_timer_get_time() + : 0)); } void repeater_print_radio_status(void) diff --git a/firmware/main/repeater.h b/firmware/main/repeater.h index 29ee30a..cf4fbc4 100644 --- a/firmware/main/repeater.h +++ b/firmware/main/repeater.h @@ -13,6 +13,7 @@ typedef struct { uint32_t tx_attempts; uint32_t tx_success; uint32_t tx_timeout; + uint64_t tx_airtime_us; // cumulative, for duty-cycle accounting uint32_t tx_failures; uint32_t tx_suppressed_dup; uint32_t tx_suppressed_whitelist; diff --git a/hil/run.sh b/hil/run.sh index 59b71a2..0556427 100755 --- a/hil/run.sh +++ b/hil/run.sh @@ -103,6 +103,31 @@ if [ "$PROOF" = 1 ]; then $PY hil/verify_tx.py --serial "$RUN_DIR/proof_serial.log" \ --sdr "$RUN_DIR/proof_sdr.log" --json > "$RUN_DIR/tx_proof.json" 2>&1 || true cat "$RUN_DIR/tx_proof.txt" + + # Second, independent decoder opinion on one of our transmitted frames: + # rtl-wmbus proves it demodulates, wmbusmeters proves the link layer is + # a structurally valid wM-Bus telegram. + FRAME=$(grep -o '"raw":"[0-9A-Fa-f]*"' "$RUN_DIR/proof_serial.log" \ + | head -1 | sed 's/.*"raw":"//;s/"//') + if [ -n "${FRAME:-}" ]; then + COOKED=$($PY - "$FRAME" <<'PYCOOK' +import sys +sys.path.insert(0, "hil") +from correlate import cook_a +raw = bytes.fromhex(sys.argv[1]) +c = cook_a(raw) +print(c.hex() if c else "") +PYCOOK +) + if [ -n "$COOKED" ]; then + if ./hil/verify_frame_semantics.sh "$COOKED" \ + "$RUN_DIR/frame_semantics.txt"; then + echo "wmbusmeters link-layer cross-check: PASS" + else + echo "wmbusmeters link-layer cross-check: FAIL (see frame_semantics.txt)" + fi + fi + fi fi # --- phase B: real meter traffic, decodes + IQ from ONE stream ------------- diff --git a/hil/verify_frame_semantics.sh b/hil/verify_frame_semantics.sh new file mode 100755 index 0000000..f98f409 --- /dev/null +++ b/hil/verify_frame_semantics.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Independent semantic check of a transmitted frame using wmbusmeters. +# +# rtl-wmbus proves the PHY/framing is demodulable and the CRCs are right. +# wmbusmeters is a separate implementation with its own link-layer parser, +# so agreeing with it shows the frame is a structurally valid Wireless M-Bus +# telegram (manufacturer, ID, version, device type, CI field) and not merely +# something that happens to satisfy a CRC. +# +# Usage: verify_frame_semantics.sh COOKED_HEX [OUTFILE] +# Exit 0 if wmbusmeters parsed the link layer, 1 otherwise. +set -euo pipefail +HEX=$1 +OUT=${2:-/dev/stdout} + +if ! command -v wmbusmeters >/dev/null 2>&1; then + echo "wmbusmeters not on PATH; skipping semantic cross-check" > "$OUT" + exit 0 +fi + +ANALYSIS=$(wmbusmeters --analyze "$HEX" 2>&1 || true) +echo "$ANALYSIS" > "$OUT" + +# Require the link-layer fields to have been recognised. +for field in "dll-mfct" "dll-id" "dll-version" "dll-type" "tpl-ci-field"; do + if ! grep -q "$field" <<< "$ANALYSIS"; then + echo "MISSING: $field" >> "$OUT" + exit 1 + fi +done +exit 0