Files
HamPack/tests/radio.bats
T
David YoungandClaude Opus 5.5 6c76f94ac6
check / lint-and-test (push) Successful in 2m2s
check / packages (push) Successful in 23s
Add radio, which finds radios on USB, and put you in the uucp group
`radio` lists each radio plugged into USB: its serial ports, the sound
card that came with it (the same USB device, or behind the radio's own
hub), and its model and Hamlib model number. Icoms are recognised by
their USB serial number. `radio detect --probe` asks the others with the
CAT ID; command or Icom's CI-V transceiver ID, at the usual baud rates,
on ports no other program has open; `radio probe PORT` asks one port.
It drops DTR and RTS as soon as a port opens. The radios it knows are
in radios.conf.

update.sh links ~/.local/bin/radio to the clone, unless a different
radio is already there, and adds you to uucp, which owns serial ports on
Arch. hampackremove removes the link, and takes you out of uucp only if
HamPack put you in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 12:12:31 -07:00

311 lines
11 KiB
Bash

#!/usr/bin/env bats
load helpers
# `radio` reads a fake sysfs, /dev, /proc and lock directory. USB devices are
# made under one root hub (usb1); a radio's serial chip and sound card sit
# behind its own hub, as they do in the real radios.
setup() {
setup_fake_home
export RADIO_SYSFS="$BATS_TEST_TMPDIR/sys"
export RADIO_DEV="$BATS_TEST_TMPDIR/dev"
export RADIO_PROC="$BATS_TEST_TMPDIR/proc"
export RADIO_LOCKDIR="$BATS_TEST_TMPDIR/lock"
export FAKE_RADIOS="$BATS_TEST_TMPDIR/radios"
ROOT_HUB="$RADIO_SYSFS/devices/pci0000:00/0000:00:14.0/usb1"
mkdir -p "$ROOT_HUB" "$RADIO_SYSFS/class/tty" "$RADIO_SYSFS/class/sound" \
"$RADIO_DEV/serial/by-id" "$RADIO_PROC" "$RADIO_LOCKDIR" "$FAKE_RADIOS"
}
# usb PATH VID:PID [MANUFACTURER PRODUCT SERIAL]: a USB device under the root hub
usb() {
local dir="$ROOT_HUB/$1"
mkdir -p "$dir"
echo "${2%:*}" > "$dir/idVendor"
echo "${2#*:}" > "$dir/idProduct"
[ -z "${3:-}" ] || echo "$3" > "$dir/manufacturer"
[ -z "${4:-}" ] || echo "$4" > "$dir/product"
[ -z "${5:-}" ] || echo "$5" > "$dir/serial"
}
# port USBPATH TTY [BY-ID]: a serial port on interface N of the device, with a
# /dev/serial/by-id link if BY-ID is given
port() {
local iface="$ROOT_HUB/$1/${1##*/}:1.${4:-0}"
mkdir -p "$iface/$2/tty/$2" "$RADIO_SYSFS/class/tty/$2"
ln -s "$iface/$2" "$RADIO_SYSFS/class/tty/$2/device"
touch "$RADIO_DEV/$2"
[ -z "${3:-}" ] || ln -s "../../$2" "$RADIO_DEV/serial/by-id/$3"
}
# card USBPATH CARD ID: a sound card on the device
card() {
local iface="$ROOT_HUB/$1/${1##*/}:1.0"
mkdir -p "$iface/sound/$2" "$RADIO_SYSFS/class/sound/$2"
ln -s "$iface" "$RADIO_SYSFS/class/sound/$2/device"
echo "$3" > "$RADIO_SYSFS/class/sound/$2/id"
}
# A computer's own sound card, not on USB
onboard_card() {
mkdir -p "$RADIO_SYSFS/devices/pci0000:00/0000:00:1f.3/sound/card0" "$RADIO_SYSFS/class/sound/card0"
ln -s "$RADIO_SYSFS/devices/pci0000:00/0000:00:1f.3" "$RADIO_SYSFS/class/sound/card0/device"
echo PCH > "$RADIO_SYSFS/class/sound/card0/id"
}
ic7300() {
usb 1-2 10c4:0001
usb 1-2/1-2.1 10c4:ea60 "Silicon Labs" "CP2102 USB to UART Bridge Controller" "IC-7300 03001234"
port 1-2/1-2.1 ttyUSB0 usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_IC-7300_03001234-if00-port0
usb 1-2/1-2.2 08bb:29c0 "Burr-Brown from TI" "USB Audio CODEC"
card 1-2/1-2.2 card1 CODEC
}
# A Yaesu: a CP2105 with two ports, and a sound card, behind its own hub
yaesu() {
usb "$1" 0424:2512
usb "$1/$1.1" 10c4:ea70 "Silicon Labs" "CP2105 Dual USB to UART Bridge Controller" "${2:-00A50791}"
port "$1/$1.1" "${3:-ttyUSB0}" "" 0
port "$1/$1.1" "${4:-ttyUSB1}" "" 1
usb "$1/$1.2" 08bb:2902 "Burr-Brown from TI" "USB Audio CODEC"
card "$1/$1.2" "${5:-card1}" "${6:-CODEC}"
}
# The radio on a port answers the probe with ANSWER ("38400 cat_id 0670")
answers() {
echo "$2" > "$FAKE_RADIOS/$1"
stub python3 'f="$FAKE_RADIOS/${4##*/}"; [ -f "$f" ] && cat "$f"; exit 0'
}
@test "recognises an Icom by its USB serial number, with its port and sound card" {
onboard_card
ic7300
run bash "$REPO/radio"
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Icom IC-7300 (Hamlib model 3073), recognised by its USB serial number" ]
[ "${lines[1]}" = " Serial port: /dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_IC-7300_03001234-if00-port0" ]
[ "${lines[2]}" = " Sound card: hw:CARD=CODEC (card1)" ]
[[ "${lines[3]}" == " USB: 10c4:ea60 Silicon Labs CP2102 USB to UART Bridge Controller, serial IC-7300 03001234" ]]
[ ${#lines[@]} -eq 4 ]
}
@test "doesn't talk to a radio it can't recognise unless asked to" {
yaesu 1-3
answers ttyUSB0 "38400 cat_id 0670"
run bash "$REPO/radio" detect
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Unknown radio or interface" ]
[ "${lines[1]}" = " Serial port: /dev/ttyUSB0" ]
[ "${lines[2]}" = " Serial port: /dev/ttyUSB1" ]
[ "${lines[3]}" = " Sound card: hw:CARD=CODEC (card1)" ]
[[ "$output" == *"Run \`radio detect --probe\` to ask it what it is"* ]]
! grep -q python3 "$STUB_LOG"
}
@test "--probe asks each port until one answers, and marks it the CAT port" {
yaesu 1-3
answers ttyUSB1 "38400 cat_id 0670"
run bash "$REPO/radio" detect --probe
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Yaesu FT-991A (Hamlib model 1035), answered on ttyUSB1 at 38400 baud" ]
[ "${lines[1]}" = " Serial port: /dev/ttyUSB0" ]
[ "${lines[2]}" = " CAT port: /dev/ttyUSB1" ]
[ "$(grep -c '^python3' "$STUB_LOG")" -eq 2 ]
}
@test "--probe recognises an Icom by its CI-V address" {
usb 1-4 0403:6001 FTDI "FT232R USB UART" A12345
port 1-4 ttyUSB0
answers ttyUSB0 "19200 civ 94"
run bash "$REPO/radio" --probe
[ "${lines[0]}" = "Icom IC-7300 (Hamlib model 3073), answered on ttyUSB0 at 19200 baud" ]
[ "${lines[2]}" = " Sound card: none found with it" ]
}
@test "lists every radio a reply could come from" {
usb 1-4 0403:6001 FTDI "FT232R USB UART" A12345
port 1-4 ttyUSB0
answers ttyUSB0 "38400 cat_id 017"
run bash "$REPO/radio" --probe
[ "${lines[0]}" = "One of: Elecraft K3 (Hamlib model 2029), Elecraft KX3 (Hamlib model 2045), Elecraft KX2 (Hamlib model 2044), Elecraft K4 (Hamlib model 2047), answered on ttyUSB0 at 38400 baud" ]
}
@test "says what answered when it doesn't know the radio" {
usb 1-4 0403:6001
port 1-4 ttyUSB0
answers ttyUSB0 "9600 civ 5E"
run bash "$REPO/radio" --probe
[ "${lines[0]}" = "An Icom radio at CI-V address 5E that radio doesn't know, answered on ttyUSB0 at 9600 baud" ]
}
@test "says when nothing answers" {
usb 1-4 0403:6001
port 1-4 ttyUSB0
answers ttyUSB1 "38400 cat_id 0670"
run bash "$REPO/radio" --probe
[ "${lines[0]}" = "Unknown radio or interface" ]
[[ "$output" == *"Nothing answered. If it's a radio, check it's switched on"* ]]
}
@test "won't probe a port another program has open or locked" {
yaesu 1-3
answers ttyUSB0 "38400 cat_id 0670"
mkdir -p "$RADIO_PROC/4242/fd"
echo rigctld > "$RADIO_PROC/4242/comm"
ln -s "$RADIO_DEV/ttyUSB0" "$RADIO_PROC/4242/fd/5"
touch "$RADIO_LOCKDIR/LCK..ttyUSB1"
run bash "$REPO/radio" --probe
[ "${lines[0]}" = "Unknown radio or interface" ]
[[ "$output" == *"Couldn't ask on ttyUSB0: $RADIO_DEV/ttyUSB0 is in use by rigctld (process 4242)"* ]]
[[ "$output" == *"Couldn't ask on ttyUSB1: $RADIO_DEV/ttyUSB1 is locked by another program"* ]]
[[ "$output" != *"Nothing answered"* ]]
! grep -q python3 "$STUB_LOG"
}
@test "pairs each radio with its own sound card" {
onboard_card
ic7300
yaesu 1-3 00B00001 ttyUSB1 ttyUSB2 card2 CODEC_1
run bash "$REPO/radio"
[ "$status" -eq 0 ]
# bats leaves the blank line between radios out of lines
[ "${lines[0]}" = "Icom IC-7300 (Hamlib model 3073), recognised by its USB serial number" ]
[ "${lines[2]}" = " Sound card: hw:CARD=CODEC (card1)" ]
[ "${lines[4]}" = "Unknown radio or interface" ]
[ "${lines[5]}" = " Serial port: /dev/ttyUSB1" ]
[ "${lines[6]}" = " Serial port: /dev/ttyUSB2" ]
[ "${lines[7]}" = " Sound card: hw:CARD=CODEC_1 (card2)" ]
}
@test "pairs a serial port and sound card that are one USB device" {
usb 1-5 0c26:0036 Icom "IC-705" "IC-705 12001234"
port 1-5 ttyACM0 "" 0
card 1-5 card1 IC705
run bash "$REPO/radio"
[ "${lines[0]}" = "Icom IC-705 (Hamlib model 3085), recognised by its USB serial number" ]
[ "${lines[2]}" = " Sound card: hw:CARD=IC705 (card1)" ]
}
@test "doesn't pair a port with a sound card on a hub with other things on it" {
usb 1-6 05e3:0610
usb 1-6/1-6.1 0403:6001 FTDI "FT232R USB UART" A12345
port 1-6/1-6.1 ttyUSB0
usb 1-6/1-6.2 0d8c:0012 C-Media "USB Audio Device"
card 1-6/1-6.2 card1 Device
usb 1-6/1-6.3 046d:c52b Logitech "USB Receiver"
run bash "$REPO/radio"
[ "${lines[2]}" = " Sound card: none found with it" ]
}
@test "doesn't pair a port with a sound card plugged straight into the computer" {
usb 1-4 0403:6001
port 1-4 ttyUSB0
usb 1-5 0d8c:0012 C-Media "USB Audio Device"
card 1-5 card1 Device
run bash "$REPO/radio"
[ "${lines[2]}" = " Sound card: none found with it" ]
}
@test "says when there's nothing plugged in" {
onboard_card
run bash "$REPO/radio"
[ "$status" -eq 1 ]
[ "$output" = "No USB serial ports found: is the radio plugged in and switched on?" ]
}
@test "probe asks one port" {
touch "$RADIO_DEV/ttyUSB3"
answers ttyUSB3 "4800 cat_id 024"
run bash "$REPO/radio" probe "$RADIO_DEV/ttyUSB3"
[ "$status" -eq 0 ]
[ "$output" = "Kenwood TS-890S (Hamlib model 2041), at 4800 baud" ]
}
@test "probe says how to get into the serial port's group" {
touch "$RADIO_DEV/ttyUSB3"
chmod 000 "$RADIO_DEV/ttyUSB3"
# Owned by uucp, as real serial ports are
local group=uucp
stub stat 'echo uucp'
run bash "$REPO/radio" probe "$RADIO_DEV/ttyUSB3"
[ "$status" -eq 1 ]
[[ "$output" == *"add yourself to the $group group with \`sudo usermod -aG $group $USER\`"* ]]
# In the group, but this login started before
echo "$group" > "$FAKE_GROUPS"
export FAKE_SESSION_GROUPS="$BATS_TEST_TMPDIR/session-groups"
: > "$FAKE_SESSION_GROUPS"
run bash "$REPO/radio" probe "$RADIO_DEV/ttyUSB3"
[[ "$output" == *"you can't open $RADIO_DEV/ttyUSB3 yet: log out and back in, so you're in the $group group"* ]]
}
@test "probe says why it can't open a port" {
run bash "$REPO/radio" probe "$RADIO_DEV/ttyUSB9"
[ "$status" -eq 1 ]
[[ "$output" == *"Can't ask: $RADIO_DEV/ttyUSB9 doesn't exist"* ]]
}
@test "unknown commands print usage" {
run bash "$REPO/radio" frobnicate
[ "$status" -eq 2 ]
[[ "$output" == *"Usage: radio"* ]]
}
# The real probe, talking to a fake radio on a pseudo-terminal
fake_radio() {
cat > "$BATS_TEST_TMPDIR/fake_radio.py" << 'EOF'
import os, select, sys, time, tty
master, slave = os.openpty()
tty.setraw(slave)
with open(sys.argv[1] + ".new", "w") as f:
f.write(os.ttyname(slave))
os.rename(sys.argv[1] + ".new", sys.argv[1])
civ_request = bytes.fromhex("fefe00e01900fd")
heard, end = b"", time.monotonic() + 20
while time.monotonic() < end:
if not select.select([master], [], [], 0.1)[0]:
continue
heard += os.read(master, 256)
if sys.argv[2] == "kenwood" and b"ID;" in heard:
os.write(master, b"ID0761;")
heard = b""
if sys.argv[2] == "icom" and civ_request in heard:
# With CI-V USB echo on, the radio echoes the request first
os.write(master, civ_request + bytes.fromhex("fefee0a41900a4fd"))
heard = b""
EOF
python3 "$BATS_TEST_TMPDIR/fake_radio.py" "$BATS_TEST_TMPDIR/pty" "$1" 3>&- &
FAKE_RADIO_PID=$!
local i
for i in $(seq 50); do
[ -f "$BATS_TEST_TMPDIR/pty" ] && break
sleep 0.1
done
PTY="$(cat "$BATS_TEST_TMPDIR/pty")"
}
teardown() {
[ -z "${FAKE_RADIO_PID:-}" ] || kill "$FAKE_RADIO_PID" 2> /dev/null || true
}
@test "probe hears a Kenwood-style answer from a real port" {
fake_radio kenwood
run bash "$REPO/radio" probe "$PTY"
[ "$status" -eq 0 ]
[ "$output" = "Yaesu FTDX10 (Hamlib model 1042), at 38400 baud" ]
}
@test "probe hears an Icom's answer over its echo, from a real port" {
fake_radio icom
run bash "$REPO/radio" probe "$PTY"
[ "$status" -eq 0 ]
[ "$output" = "Icom IC-705 (Hamlib model 3085), at 38400 baud" ]
}
@test "probe says when nothing answers on a real port" {
fake_radio silent
run bash "$REPO/radio" probe "$PTY"
[ "$status" -eq 1 ]
[ "$output" = "Nothing answered on $PTY at 38400 115200 19200 9600 4800 baud" ]
}