capture_sdr: PLL wedge probe before attempts

This commit is contained in:
Lars Nolden
2026-08-31 23:17:43 +02:00
parent 5c8d15d089
commit 19d9067156
+29 -6
View File
@@ -2,11 +2,12 @@
# Capture wM-Bus frames with the RTL-SDR for a fixed duration.
# Usage: capture_sdr.sh DURATION_S OUTFILE [GAIN]
#
# Robustness notes (learned the hard way):
# Robustness notes (learned the hard way; see docs/FINDINGS.md):
# - timeout is applied to rtl_sdr itself; killing a wrapping shell orphans
# the children and keeps the device claimed.
# - rtl_sdr occasionally dies mid-stream with USB transfer errors
# ("Library error -5"); we usbreset and restart until the deadline.
# - The R820T PLL wedges ("PLL not locked") and rtl_sdr dies mid-stream
# with "Library error -5". usbreset recovers it; sometimes it needs a
# cool-down. We probe for the wedge before each capture attempt.
set -euo pipefail
cd "$(dirname "$0")/.."
DUR=$1; OUT=$2; GAIN=${3:-42.1}
@@ -15,19 +16,41 @@ DUR=$1; OUT=$2; GAIN=${3:-42.1}
ERR="${OUT%.log}.err"
: > "$ERR"
sdr_healthy() {
# 2-second probe; wedged tuner prints "PLL not locked" on tune
local probe
probe=$(timeout 6 rtl_sdr -f 868950000 -s 1600000 -g "$GAIN" -n 3200000 /dev/null 2>&1 || true)
echo "$probe" >> "$ERR"
! grep -q "PLL not locked" <<< "$probe"
}
ensure_healthy() {
for i in 1 2 3; do
if sdr_healthy; then return 0; fi
echo "--- PLL wedged; usbreset (try $i)" >> "$ERR"
usbreset 0bda:2838 >> "$ERR" 2>&1 || true
sleep $((i * 5))
done
echo "--- SDR still wedged after 3 resets" >> "$ERR"
return 1
}
deadline=$(( $(date +%s) + DUR ))
attempt=0
while :; do
remaining=$(( deadline - $(date +%s) ))
[ "$remaining" -le 2 ] && break
[ "$remaining" -le 10 ] && break
attempt=$((attempt + 1))
echo "--- rtl_sdr attempt $attempt (${remaining}s remaining)" >> "$ERR"
ensure_healthy || true
remaining=$(( deadline - $(date +%s) ))
[ "$remaining" -le 2 ] && break
timeout "$remaining" rtl_sdr -f 868950000 -s 1600000 -g "$GAIN" - 2>>"$ERR" \
| rtl_wmbus -f >> "$OUT" 2>/dev/null || true
remaining=$(( deadline - $(date +%s) ))
if [ "$remaining" -gt 2 ]; then
if [ "$remaining" -gt 10 ]; then
echo "--- rtl_sdr died early; usbreset and retry" >> "$ERR"
usbreset 0bda:2838 >> "$ERR" 2>&1 || true
sleep 3
sleep 5
fi
done