#!/usr/bin/env bash # Capture rtl_wmbus decodes AND raw IQ from a single rtl_sdr stream, in # bounded segments with recovery between them. # # Two hardware facts drive this design (docs/FINDINGS.md): # 1. This dongle's R820T tuner dies after a few minutes of continuous # streaming ("r82xx_write: i2c wr failed"). rtl_sdr does NOT exit -- it # keeps producing nothing, so a naive long capture silently goes deaf. # Bounded segments with -n make it exit cleanly, and we usbreset between # segments instead of relying on failure detection. # 2. Sensitivity drifts ~17 dB over minutes, so meter decodes and our own # TX measurements must come from the same stream to be comparable. # # Usage: capture_both.sh DURATION_S DECODE_LOG IQ_FILE [GAIN] [IQ_SEGMENTS] set -euo pipefail cd "$(dirname "$0")/.." DUR=$1; OUT=$2; IQ=$3; GAIN=${4:-42.1}; IQ_SEGMENTS=${5:-1} SEG=${SEG_SECONDS:-140} # below the observed tuner death point IQ_SEG=${IQ_SEG_SECONDS:-90} # bounded IQ window (~290 MB) RATE=1600000 : > "$OUT" : > "$IQ" ERR="${OUT%.log}.err" STATS="${OUT%.log}.stats" : > "$ERR" health() { ./result-py/bin/python3 hil/sdr_health.py >> "$ERR" 2>&1 } usable=0 segments=0 recovered=0 iq_seconds=0 deadline=$(( $(date +%s) + DUR )) while :; do remaining=$(( deadline - $(date +%s) )) [ "$remaining" -le 10 ] && break this=$(( remaining < SEG ? remaining : SEG )) segments=$((segments + 1)) if ! health; then echo "--- segment $segments: SDR unhealthy, usbreset" >> "$ERR" usbreset 0bda:2838 >> "$ERR" 2>&1 || true recovered=$((recovered + 1)) sleep 5 health || { echo "--- still unhealthy, skipping segment" >> "$ERR"; sleep 2; continue; } fi echo "--- segment $segments: ${this}s (gain $GAIN)" >> "$ERR" t0=$(date +%s) if [ "$segments" -le "$IQ_SEGMENTS" ]; then # IQ segment: write samples straight to disk with no decoder in the # pipeline. Feeding rtl_wmbus through tee throttled the writer and # only 1.5 % of the samples reached the file. The IQ proof needs # firmware TX timestamps, not decodes, so the two do not have to be # simultaneous -- and the IQ still contains both our transmissions # and meter bursts, so relative levels remain measurable. iqlen=$(( this < IQ_SEG ? this : IQ_SEG )) timeout $((iqlen + 20)) rtl_sdr -f 868950000 -s "$RATE" -g "$GAIN" \ -n $((iqlen * RATE)) "$IQ.seg" 2>>"$ERR" || true cat "$IQ.seg" >> "$IQ" 2>/dev/null || true rm -f "$IQ.seg" iq_seconds=$((iq_seconds + iqlen)) else timeout $((this + 20)) rtl_sdr -f 868950000 -s "$RATE" -g "$GAIN" \ -n $((this * RATE)) - 2>>"$ERR" \ | rtl_wmbus -f >> "$OUT" 2>/dev/null || true fi t1=$(date +%s) usable=$(( usable + t1 - t0 )) # tuner commonly dies during a segment; reset before the next one if grep -q "i2c wr failed" "$ERR"; then echo "--- tuner fault seen; usbreset" >> "$ERR" usbreset 0bda:2838 >> "$ERR" 2>&1 || true recovered=$((recovered + 1)) : > "$ERR.tmp"; tail -c 20000 "$ERR" > "$ERR.tmp"; mv "$ERR.tmp" "$ERR" sleep 5 fi done printf 'usable_seconds=%s\nrequested_seconds=%s\nsegments=%s\nrecoveries=%s\ngain=%s\niq_seconds=%s\niq_bytes=%s\n' \ "$usable" "$DUR" "$segments" "$recovered" "$GAIN" "$iq_seconds" \ "$(stat -c%s "$IQ" 2>/dev/null || echo 0)" > "$STATS" echo "--- usable ${usable}s of ${DUR}s in $segments segment(s), $recovered recovery(ies)" >> "$ERR"