#!/usr/bin/env bash # Full HIL loop: discover -> (build+flash) -> capture serial+SDR -> correlate -> report. # # Usage: ./hil/run.sh [--duration N] [--no-flash] [--tx-power N] [--delay MS] # [--gain G] [--min-repeats N] # Exit: 0 = PASS, nonzero = FAIL (report thresholds or infrastructure error). set -euo pipefail cd "$(dirname "$0")/.." DURATION=300 FLASH=1 TXPOW=-9 DELAY=50 GAIN=42.1 MIN_REPEATS=1 while [ $# -gt 0 ]; do case "$1" in --duration) DURATION=$2; shift 2 ;; --no-flash) FLASH=0; shift ;; --tx-power) TXPOW=$2; shift 2 ;; --delay) DELAY=$2; shift 2 ;; --gain) GAIN=$2; shift 2 ;; --min-repeats) MIN_REPEATS=$2; shift 2 ;; *) echo "unknown arg: $1" >&2; exit 2 ;; esac done PY=./result-py/bin/python3 [ -x "$PY" ] || { echo "run 'nix build .#hil-python -o result-py' first" >&2; exit 2; } # 1-2. discover devices (abort on ambiguity) eval "$(./tools/discover_devices.sh)" echo "DUT: $DUT_PORT" echo "SDR: Nooelec NESDR SMArt v5, index $SDR_INDEX, serial $SDR_SERIAL" # 3. SDR sanity: reset USB (rtl PLL can wedge; see docs/FINDINGS.md) and # verify the device opens. usbreset 0bda:2838 >/dev/null 2>&1 || true sleep 1 # 4-6. build + flash + reset if [ "$FLASH" = 1 ]; then (cd firmware && idf.py build) >/dev/null (cd firmware && idf.py -p "$DUT_PORT" flash) >/dev/null 2>&1 || { # port re-enumerates when entering download mode; rediscover once sleep 2 eval "$(./tools/discover_devices.sh)" (cd firmware && idf.py -p "$DUT_PORT" flash) >/dev/null } sleep 2 eval "$(./tools/discover_devices.sh)" fi RUN_DIR="captures/$(date +%Y-%m-%dT%H-%M-%S)-hil" mkdir -p "$RUN_DIR" echo "run dir: $RUN_DIR" # 7. configure DUT + start serial capture $PY hil/dut_cmd.py "$DUT_PORT" --listen 2 \ "set repeat_delay_ms $DELAY" "set tx_power $TXPOW" "clear_stats" \ > "$RUN_DIR/setup.log" 2>&1 $PY hil/dut_cmd.py "$DUT_PORT" --listen "$DURATION" status \ > "$RUN_DIR/serial.log" 2>&1 & SERIAL_PID=$! # 8. SDR capture (timeout applied to rtl_sdr itself; children die with it) ./hil/capture_sdr.sh "$DURATION" "$RUN_DIR/rtlwmbus.log" "$GAIN" & SDR_PID=$! # 9-12. wait for both collectors wait "$SDR_PID" || true wait "$SERIAL_PID" || true # final stats snapshot $PY hil/dut_cmd.py "$DUT_PORT" --listen 3 stats > "$RUN_DIR/final_stats.log" 2>&1 || true cat "$RUN_DIR/final_stats.log" >> "$RUN_DIR/serial.log" # 13-14. correlate $PY hil/correlate.py --serial "$RUN_DIR/serial.log" --sdr "$RUN_DIR/rtlwmbus.log" \ > "$RUN_DIR/report.json" # 15. lockup detection: heartbeats should keep arriving HB=$(grep -c '"event":"heartbeat"' "$RUN_DIR/serial.log" || true) EXPECTED_HB=$((DURATION / 10 - 2)) if [ "$HB" -lt "$EXPECTED_HB" ]; then echo "WARNING: only $HB heartbeats seen (expected >= $EXPECTED_HB) - possible lockup" \ | tee "$RUN_DIR/lockup_warning.txt" fi # 16-18. report $PY hil/report.py "$RUN_DIR/report.json" \ --min-repeats "$MIN_REPEATS" --duration "$DURATION" \ --tx-power "$TXPOW" --repeat-delay "$DELAY" \ | tee "$RUN_DIR/report.txt" RC=${PIPESTATUS[0]} echo "artifacts: $RUN_DIR" exit "$RC"