#!/usr/bin/env bash # Deterministic discovery of DUT (Heltec V4.3) and SDR (Nooelec NESDR SMArt v5). # Prints selected devices; exits nonzero on ambiguity or absence. # # Output (eval-able): # DUT_PORT=/dev/serial/by-id/... # SDR_INDEX=0 # SDR_SERIAL=33999948 set -euo pipefail fail() { echo "DISCOVER_ERROR: $*" >&2; exit 1; } # --- DUT: Heltec WiFi LoRa 32 V4 (ESP32-S3 native USB CDC) --- shopt -s nullglob dut_candidates=(/dev/serial/by-id/*heltec_wifi_lora_32_v4*) if [ ${#dut_candidates[@]} -eq 0 ]; then # fall back to any Espressif device, but require uniqueness dut_candidates=(/dev/serial/by-id/usb-Espressif*) fi [ ${#dut_candidates[@]} -eq 0 ] && fail "no Heltec/Espressif serial device found in /dev/serial/by-id/" [ ${#dut_candidates[@]} -gt 1 ] && fail "ambiguous DUT: ${dut_candidates[*]}" DUT_PORT="${dut_candidates[0]}" [ -e "$DUT_PORT" ] || fail "DUT path vanished: $DUT_PORT" # --- SDR: Nooelec NESDR SMArt v5 (RTL2832U) --- # rtl_test -t enumerates devices without streaming. sdr_list=$(timeout 10 rtl_test -t 2>&1 | grep -E '^\s+[0-9]+:' || true) [ -z "$sdr_list" ] && fail "no RTL-SDR device found (rtl_test)" sdr_matches=$(echo "$sdr_list" | grep -i "Nooelec" || true) [ -z "$sdr_matches" ] && fail "RTL-SDR present but not a Nooelec: $sdr_list" [ "$(echo "$sdr_matches" | wc -l)" -gt 1 ] && fail "ambiguous SDR: $sdr_matches" SDR_INDEX=$(echo "$sdr_matches" | sed -E 's/^\s*([0-9]+):.*/\1/') SDR_SERIAL=$(echo "$sdr_matches" | sed -E 's/.*SN: ([^ ]+).*/\1/') echo "DUT_PORT=$DUT_PORT" echo "SDR_INDEX=$SDR_INDEX" echo "SDR_SERIAL=$SDR_SERIAL" if [ "${1:-}" = "-v" ]; then echo "---" >&2 echo "DUT: $DUT_PORT -> $(readlink -f "$DUT_PORT")" >&2 echo "SDR: index $SDR_INDEX, serial $SDR_SERIAL" >&2 echo "$sdr_matches" >&2 fi