#!/usr/bin/env python3 """Evaluate the TX-decodability proof phase. The DUT transmits synthetic C1 frames twice, separated by the configured repeat delay (`test_burst`). The SDR must independently decode them as Wireless M-Bus C1. This proves, without depending on real meter traffic: - the Heltec emits standards-shaped C1 frames - a second frame at the repeat delay is independently observable - the exact bytes match what the firmware said it sent Usage: verify_tx.py --serial burst_serial.log --sdr burst_sdr.log [--json] Exit 0 if at least one transmitted frame was decoded by the SDR. """ import argparse import json import sys sys.path.insert(0, __file__.rsplit("/", 1)[0]) from correlate import cook_a, SDR_RE # noqa: E402 ap = argparse.ArgumentParser() ap.add_argument("--serial", required=True) ap.add_argument("--sdr", required=True) ap.add_argument("--json", action="store_true") args = ap.parse_args() frames = [] # synthetic frames the DUT reported building tx_results = [] for line in open(args.serial, errors="replace"): line = line.strip() if not line.startswith("{"): continue try: ev = json.loads(line) except json.JSONDecodeError: continue if ev.get("event") == "test_burst_frame": frames.append(ev) elif ev.get("event") == "wmbus_tx": tx_results.append(ev) sdr_counts = {} for line in open(args.sdr, errors="replace"): m = SDR_RE.match(line.strip()) if not m or m["mode"] != "C1" or m["crc"] != "1": continue sdr_counts[m["hex"].lower()] = sdr_counts.get(m["hex"].lower(), 0) + 1 per_frame = [] for f in frames: raw = bytes.fromhex(f["raw"]) cooked = cook_a(raw) n = sdr_counts.get(cooked.hex(), 0) if cooked else 0 # each airing yields 1-2 lines (rtl_wmbus runs two demodulators) airings = 0 if n == 0 else (1 if n <= 2 else 2) per_frame.append({ "lfield": f.get("lfield"), "delay_ms": f.get("delay_ms"), "sdr_lines": n, "airings_decoded": airings, }) decoded = sum(1 for p in per_frame if p["airings_decoded"] >= 1) both = sum(1 for p in per_frame if p["airings_decoded"] >= 2) tx_ok = sum(1 for t in tx_results if t.get("result", "").startswith("ok")) result = { "bursts_requested": len(frames), "tx_events": len(tx_results), "tx_reported_ok": tx_ok, "bursts_with_sdr_decode": decoded, "bursts_with_both_airings_decoded": both, "per_burst": per_frame, } if args.json: print(json.dumps(result, indent=2)) else: print(f"TX_PROOF bursts={len(frames)} tx_ok={tx_ok} " f"sdr_decoded={decoded} both_airings={both}") for p in per_frame: print(f" L={p['lfield']} delay={p['delay_ms']}ms " f"sdr_lines={p['sdr_lines']} airings={p['airings_decoded']}") sys.exit(0 if decoded >= 1 else 1)