Files

62 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Long-running beacon soak: segmented captures, meter-anchored verdict per
# segment, dongle auto-recovery. Appends one JSON line per segment.
# Usage: beacon_soak.sh SEGMENTS [SEG_SECONDS]
set -u
SEGS=${1:-30}
DUR=${2:-120}
cd "$(dirname "$0")/.."
OUT="captures/$(date +%Y-%m-%dT%H-%M-%S)-beacon-soak"
mkdir -p "$OUT"
echo "soak dir: $OUT"
for i in $(seq 1 "$SEGS"); do
P=$(ls /dev/serial/by-id/usb-Espressif* 2>/dev/null | head -1)
if [ -z "$P" ]; then
echo "{\"seg\":$i,\"error\":\"dut_gone\",\"t\":$(date +%s)}" >> "$OUT/soak.jsonl"
sleep 30; continue
fi
D="$OUT/seg$i"
mkdir -p "$D"
( timeout $((DUR + 8)) rtl_sdr -f 868950000 -s 1600000 -g 42.1 \
-n $((DUR * 1600000)) "$D/iq.raw" 2>"$D/rtl_sdr.err" ) &
./result-py/bin/python3 hil/dut_cmd.py "$P" --listen $((DUR + 4)) \
> "$D/serial.log" 2>&1
wait
SZ=$(stat -c%s "$D/iq.raw" 2>/dev/null || echo 0)
if [ "$SZ" -lt $((DUR * 3200000 * 8 / 10)) ]; then
echo "{\"seg\":$i,\"error\":\"short_capture\",\"bytes\":$SZ,\"t\":$(date +%s)}" >> "$OUT/soak.jsonl"
rm -f "$D/iq.raw"
usbreset 0bda:2838 >/dev/null 2>&1
sleep 10
continue
fi
V=$(./result-py/bin/python3 hil/meter_anchored.py \
--iq "$D/iq.raw" --serial "$D/serial.log" --json 2>"$D/verdict.err")
RC=$?
STATE=""
if [ -n "$V" ]; then
STATE=$(printf '%s\n' "$V" | ./result-py/bin/python3 -c \
"import json,sys; print(json.load(sys.stdin).get('rf_state','unknown'))")
printf '%s\n' "$V" | ./result-py/bin/python3 -c \
"import json,sys,time; d=json.load(sys.stdin); d['seg']=$i; d['rc']=$RC; d['t']=int(time.time()); print(json.dumps(d))" \
>> "$OUT/soak.jsonl"
else
echo "{\"seg\":$i,\"rc\":$RC,\"error\":\"no_verdict\",\"t\":$(date +%s)}" >> "$OUT/soak.jsonl"
fi
# Strong segments are summarized and discarded. Retain every weak,
# absent, or unclassified segment so a hardware conclusion is auditable.
if [ "$STATE" = "strong" ] && [ "$RC" = "0" ]; then
rm -f "$D/iq.raw"
else
echo "{\"seg\":$i,\"note\":\"iq_retained\",\"rf_state\":\"$STATE\",\"t\":$(date +%s)}" >> "$OUT/soak.jsonl"
fi
if grep -q "lost at least" "$D/rtl_sdr.err" 2>/dev/null; then
echo "{\"seg\":$i,\"note\":\"sample_drops\",\"t\":$(date +%s)}" >> "$OUT/soak.jsonl"
fi
if [ "$RC" != "0" ] && [ "$RC" != "1" ]; then
usbreset 0bda:2838 >/dev/null 2>&1
sleep 8
fi
done
echo "soak complete: $OUT/soak.jsonl"