#!/usr/bin/env bash # Capture wM-Bus frames with the RTL-SDR for a fixed duration. # Usage: capture_sdr.sh DURATION_S OUTFILE [GAIN] # # Robustness notes (see docs/FINDINGS.md): # - timeout is applied to rtl_sdr itself; killing a wrapping shell orphans # the children and keeps the device claimed. # - "[R82XX] PLL not locked!" during tuning is BENIGN noise emitted while # rtl_sdr probes/disables direct-sampling mode. It is NOT a wedge # indicator; do not reset on it. # - The real failure is "Library error -5" (USB transfer death) which ends # the stream early. We restart until the deadline and record the total # streaming time so the report can tell "SDR was deaf" from "no traffic". set -euo pipefail cd "$(dirname "$0")/.." DUR=$1; OUT=$2; GAIN=${3:-42.1} : > "$OUT" ERR="${OUT%.log}.err" STATS="${OUT%.log}.stats" : > "$ERR" deadline=$(( $(date +%s) + DUR )) attempt=0 streamed=0 while :; do now=$(date +%s) remaining=$(( deadline - now )) [ "$remaining" -le 3 ] && break attempt=$((attempt + 1)) echo "--- rtl_sdr attempt $attempt (${remaining}s remaining)" >> "$ERR" t0=$(date +%s) timeout "$remaining" rtl_sdr -f 868950000 -s 1600000 -g "$GAIN" - 2>>"$ERR" \ | rtl_wmbus -f >> "$OUT" 2>/dev/null || true t1=$(date +%s) streamed=$(( streamed + t1 - t0 )) remaining=$(( deadline - t1 )) if [ "$remaining" -gt 3 ]; then echo "--- stream died after $((t1 - t0))s; usbreset + settle" >> "$ERR" usbreset 0bda:2838 >> "$ERR" 2>&1 || true sleep 4 fi done printf 'streamed_seconds=%s\nrequested_seconds=%s\nattempts=%s\n' \ "$streamed" "$DUR" "$attempt" > "$STATS" echo "--- streamed ${streamed}s of ${DUR}s in $attempt attempt(s)" >> "$ERR"