Add radio, which finds radios on USB, and put you in the uucp group
check / lint-and-test (push) Successful in 2m2s
check / packages (push) Successful in 23s

`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>
This commit is contained in:
David Young
2026-11-28 12:12:31 -07:00
co-authored by Claude Opus 5.5
parent 48d94ca432
commit 6c76f94ac6
14 changed files with 944 additions and 3 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ jobs:
steps:
# nodejs is for actions/checkout itself
- name: Install tools
run: pacman -Syu --noconfirm --needed bats shellcheck make git jq zip unzip curl which diffutils file nodejs
run: pacman -Syu --noconfirm --needed bats shellcheck make git jq zip unzip curl which diffutils file python nodejs
- uses: actions/checkout@v4
# As an ordinary user, as a developer runs them: root ignores file permissions
- name: make lint test
+1 -1
View File
@@ -6,7 +6,7 @@
# make hooks - run `make lint test` before each commit
SCRIPTS := install.sh update.sh lib.sh install-binaries.sh install-compiled.sh \
install-flatpaks.sh install-windows-apps.sh patch-potacat.sh hampackcheck hampackremove \
install-flatpaks.sh install-windows-apps.sh patch-potacat.sh hampackcheck hampackremove radio \
smoketest/setup smoketest/health smoketest/lib.sh $(wildcard smoketest/scenarios/*.sh)
.PHONY: check lint test check-packages hooks
+10
View File
@@ -57,6 +57,16 @@ winlink.org only lets web browsers download its programs, so HamPack can't fetch
Graywolf's web UI is at http://localhost:8080 and, by default, can only be reached from the machine it runs on. To reach it from other machines, run `sudo systemctl edit graywolf` and override `ExecStart` with `-http 0.0.0.0:8080`. Changes made that way survive HamPack updates; edits to `/etc/systemd/system/graywolf.service` itself don't.
### Finding your radio
`radio` lists the radios plugged into USB: each one's serial port, the sound card that came with it, and its model and Hamlib model number when it recognises it. Most Icoms are recognised from their USB serial number alone. For other radios, run `radio detect --probe`. This asks each one what it is (the CAT `ID;` command, or Icom's CI-V equivalent) over every serial port no other program is using. `radio probe /dev/ttyUSB0` asks a single port.
Controlling a radio means opening its serial port, and on Arch only members of the `uucp` group can. HamPack adds you to it when it installs. Log out and back in once afterwards for it to take effect. `hampackremove` takes you out of the group again, unless you were already in it before HamPack.
Probing opens the port, which raises its DTR and RTS lines for a moment before `radio` drops them. Interfaces that key the transmitter with DTR or RTS may key it briefly, so probe with the radio on a dummy load or an antenna.
The radios it knows are in `radios.conf`. If your radio isn't recognised, or answers with something `radio` doesn't know, please open an issue with what `radio detect --probe` printed.
## Why CoreDesktop and/or Arch Linux?
There's no reason HamPack couldn't run on Debian, Ubuntu, Fedora or any of the many other distributions of Linux, with all the libraries and utilities required for getting the ham radio software offered running. And let's hope that happens, in time.
+7
View File
@@ -256,11 +256,18 @@ update-mime-database "$HOME/.local/share/mime" &> /dev/null
report_unrecorded
# Take you out of the groups update.sh added you to
for group in $(recorded_installs added-groups); do
echo "Removing you from the $group group..."
sudo gpasswd -d "$USER" "$group" > /dev/null
done
echo ""
echo "Removing HamPack state and data..."
rm -rf "$HAMPACK_STATE" "$HOME/.local/share/HamPack"
rm -f "$COREDESKTOP"/updates.d/[0-9][0-9][0-9]-hampack-*
rm -f "$BIN/hampackremove"
[[ "$(readlink "$BIN/radio")" != "$HAMPACK_DIR"/radio ]] || rm -f "$BIN/radio"
# Remove the sibling clone, so CoreDesktop's `update` stops updating HamPack
rm -rf "$HAMPACK_DIR"
Executable
+371
View File
@@ -0,0 +1,371 @@
#!/bin/bash
#
# Purpose : Find the radios plugged into this computer's USB: each one's CAT
# serial port, its sound card, and what model it is
#
# Usage : radio [detect] [--probe]
# radio probe PORT
# radio help
SELF="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
# shellcheck source=lib.sh
source "$SELF/lib.sh"
RADIOS_CONF="${HAMPACK_RADIOS_CONF:-$SELF/radios.conf}"
# Where to look for devices. Tests point these at fake trees
SYSFS="${RADIO_SYSFS:-/sys}"
DEVDIR="${RADIO_DEV:-/dev}"
PROC="${RADIO_PROC:-/proc}"
LOCKDIR="${RADIO_LOCKDIR:-/run/lock}"
# CAT baud rates to try, most likely first
BAUDS=(38400 115200 19200 9600 4800)
usage() {
cat << EOF
Usage: radio [detect] [--probe]
radio probe PORT
detect List the radios plugged into USB: each one's serial port,
its sound card and, if radio recognises it, its model.
Doesn't send the radios anything
detect --probe Also ask radios it doesn't recognise what they are, over each
serial port that no other program is using
probe PORT Ask the radio on serial port PORT what it is
EOF
}
# Print a sysfs attribute, or nothing if the device hasn't got it
attr() {
[ -r "$1/$2" ] && printf '%s' "$(< "$1/$2")"
return 0
}
# Print the USB device (the sysfs directory with idVendor) a device belongs to
usb_device() {
local d
d="$(readlink -f "$1")" || return 1
while [ "$d" != / ] && [ -n "$d" ]; do
[ -f "$d/idVendor" ] && { echo "$d"; return 0; }
d="$(dirname "$d")"
done
return 1
}
# Find the USB serial ports and USB sound cards: PORT_USB[ttyUSB0] and
# CARD_USB[card1] are the USB devices they belong to
find_devices() {
declare -gA PORT_USB=() CARD_USB=()
local t c dev
for t in "$SYSFS"/class/tty/tty{USB,ACM}*; do
[ -e "$t/device" ] && dev="$(usb_device "$t/device")" && PORT_USB["${t##*/}"]="$dev"
done
for c in "$SYSFS"/class/sound/card*; do
[ -e "$c/device" ] && dev="$(usb_device "$c/device")" && CARD_USB["${c##*/}"]="$dev"
done
return 0
}
has_ports() {
local p
for p in "${!PORT_USB[@]}"; do [ "${PORT_USB[$p]}" = "$1" ] && return 0; done
return 1
}
has_card() {
local c
for c in "${!CARD_USB[@]}"; do [ "${CARD_USB[$c]}" = "$1" ] && return 0; done
return 1
}
# Print the radio a USB serial device belongs to. A radio's serial chip and sound
# card are either one USB device or two behind the radio's own hub, so: the
# device itself if it has a sound card too, or its hub if that holds one sound
# card and nothing but serial devices, or else just the device. Nothing tells
# a radio's hub from a hub you've plugged a serial cable and a sound card into.
radio_of() {
local dev="$1" hub child cards=0
hub="$(dirname "$dev")"
if ! has_card "$dev" && [[ "${hub##*/}" != usb* ]] && [ -f "$hub/idVendor" ]; then
for child in "$hub"/*/; do
child="${child%/}"
[ -f "$child/idVendor" ] || continue
if has_card "$child"; then
cards=$((cards + 1))
elif ! has_ports "$child"; then
cards=99 # something else is on this hub
fi
done
(( cards == 1 )) && { echo "$hub"; return; }
fi
echo "$dev"
}
# Print the radio's sound card, as card1
card_of() {
local c
for c in $(printf '%s\n' "${!CARD_USB[@]}" | sort -V); do
[ "${CARD_USB[$c]}" = "$1" ] || [ "$(dirname "${CARD_USB[$c]}")" = "$1" ] && { echo "$c"; return; }
done
}
# Print the port's stable name in /dev/serial/by-id, or its /dev name if it has none
port_path() {
local link
for link in "$DEVDIR"/serial/by-id/*; do
[ -L "$link" ] && [ "$(basename "$(readlink -f "$link")")" = "$1" ] && { echo "/dev/serial/by-id/${link##*/}"; return; }
done
echo "/dev/$1"
}
# Print the radios in radios.conf whose field matches: matching FIELD VALUE.
# usb_serial= is a pattern; the others must be equal (ignoring case)
matching() {
MATCH_FIELD="$1" MATCH_VALUE="$2" MATCHES=()
parse_conf "$RADIOS_CONF" match_radio
[ ${#MATCHES[@]} -gt 0 ] && printf '%s\n' "${MATCHES[@]}"
}
match_radio() {
local want="${CONF[$MATCH_FIELD]}"
[ -n "$want" ] || return 0
if [ "$MATCH_FIELD" = usb_serial ]; then
# shellcheck disable=SC2053 # want is a pattern
[[ "$MATCH_VALUE" == $want ]] || return 0
else
[ "${want,,}" = "${MATCH_VALUE,,}" ] || return 0
fi
MATCHES+=("${CONF[_name]} (Hamlib model ${CONF[hamlib]})")
}
# Print why a port can't be probed, or nothing if it can
cant_probe() {
local port="$1" dev fd pid group
dev="$(readlink -f "$port")"
if [ ! -e "$port" ]; then
echo "$port doesn't exist"
elif [ ! -r "$port" ] || [ ! -w "$port" ]; then
group="$(stat -c %G "$dev")"
if id -nG "$USER" | tr ' ' '\n' | grep -qx "$group"; then
echo "you can't open $port yet: log out and back in, so you're in the $group group"
else
echo "you can't open $port: add yourself to the $group group with \`sudo usermod -aG $group $USER\`, then log out and back in"
fi
elif [ -e "$LOCKDIR/LCK..${dev##*/}" ]; then
echo "$port is locked by another program ($LOCKDIR/LCK..${dev##*/})"
else
for fd in "$PROC"/[0-9]*/fd/*; do
[ "$(readlink "$fd" 2> /dev/null)" = "$dev" ] || continue
pid="${fd#"$PROC"/}"
pid="${pid%%/*}"
echo "$port is in use by $(cat "$PROC/$pid/comm" 2> /dev/null || echo "another program") (process $pid)"
return
done
fi
}
# Ask the radio on PORT what it is, at each baud rate in turn: the Kenwood-style
# ID; (Kenwood, Yaesu, Elecraft) and Icom's CI-V "read transceiver ID". Prints
# "BAUD cat_id DIGITS" or "BAUD civ ADDRESS" for the first answer. DTR and RTS
# are dropped as soon as the port opens, since many interfaces key the
# transmitter with one of them; the port opening still raises them for a moment.
ask_radio() {
python3 -I -c '
import fcntl, os, re, select, struct, sys, termios, time
def unkey():
try:
fcntl.ioctl(fd, termios.TIOCMBIC, struct.pack("I", termios.TIOCM_DTR | termios.TIOCM_RTS))
except OSError:
pass # not a real serial port
def ask(command, wait=0.4):
termios.tcflush(fd, termios.TCIOFLUSH)
os.write(fd, command)
reply, end = b"", time.monotonic() + wait
while (left := end - time.monotonic()) > 0:
if not select.select([fd], [], [], left)[0]:
break
try:
chunk = os.read(fd, 256)
except BlockingIOError:
continue
if not chunk:
break
reply += chunk
return reply
try:
fd = os.open(sys.argv[1], os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
except OSError as e:
print("error", e.strerror)
sys.exit(1)
unkey()
for baud in sys.argv[2:]:
attrs = termios.tcgetattr(fd)
speed = getattr(termios, "B" + baud)
# Raw 8N1, no flow control
attrs[0:6] = [0, 0, termios.CS8 | termios.CREAD | termios.CLOCAL, 0, speed, speed]
attrs[6][termios.VMIN] = attrs[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, attrs)
unkey()
found = re.search(rb"ID(\d{3,4});", ask(b"ID;"))
if found:
print(baud, "cat_id", found.group(1).decode())
break
# To any address (00) from a computer (E0); the radio answers to E0 from its address
found = re.search(rb"\xfe\xfe\xe0(.)\x19\x00.\xfd", ask(bytes.fromhex("fefe00e01900fd")), re.S)
if found:
print(baud, "civ", found.group(1).hex().upper())
break
' "$1" "${BAUDS[@]}"
}
# Probe PORT and set ANSWER_BAUD, ANSWER_FIELD and ANSWER_VALUE from its
# answer, or PROBE_ERROR if the port couldn't be opened
probe_port() {
local reply
ANSWER_BAUD="" ANSWER_FIELD="" ANSWER_VALUE="" PROBE_ERROR=""
if ! reply="$(ask_radio "$1")"; then
PROBE_ERROR="${reply#error }"
PROBE_ERROR="${PROBE_ERROR:-python3 failed}"
return 1
fi
read -r ANSWER_BAUD ANSWER_FIELD ANSWER_VALUE <<< "$reply"
[ -n "$ANSWER_VALUE" ]
}
# Join the radios matching prints into one line
one_of() {
if [ "$(wc -l <<< "$1")" -eq 1 ]; then
echo "$1"
else
echo "one of: $(paste -sd '|' <<< "$1" | sed 's/|/, /g')"
fi
}
# Describe the radio that answered
describe_answer() {
local models
models="$(matching "$ANSWER_FIELD" "$ANSWER_VALUE")"
if [ -z "$models" ]; then
if [ "$ANSWER_FIELD" = civ ]; then
echo "an Icom radio at CI-V address $ANSWER_VALUE that radio doesn't know"
else
echo "a radio that answered ID$ANSWER_VALUE; that radio doesn't know"
fi
else
one_of "$models"
fi
}
# radio detect: list each radio and what's known about it
detect() {
local probe=0 radio port dev card name serial reason found=0
[ "${1:-}" = --probe ] && probe=1
[ "${1:-}" = --probe ] || [ -z "${1:-}" ] || { usage >&2; return 2; }
find_devices
if [ ${#PORT_USB[@]} -eq 0 ]; then
echo "No USB serial ports found: is the radio plugged in and switched on?"
return 1
fi
# Group the ports by radio
declare -A RADIO_PORTS=()
for port in $(printf '%s\n' "${!PORT_USB[@]}" | sort -V); do
radio="$(radio_of "${PORT_USB[$port]}")"
RADIO_PORTS["$radio"]+="$port "
done
for radio in $(printf '%s\n' "${!RADIO_PORTS[@]}" | sort); do
(( found++ )) && echo
local ports=() usb_devs=() models="" how="" cat_port="" lines=()
read -ra ports <<< "${RADIO_PORTS[$radio]}"
# Recognise it by its USB serial number, or else by asking it
for port in "${ports[@]}"; do
dev="${PORT_USB[$port]}"
[[ " ${usb_devs[*]} " == *" $dev "* ]] || usb_devs+=("$dev")
done
for dev in "${usb_devs[@]}"; do
serial="$(attr "$dev" serial)"
[ -n "$serial" ] && models="$(matching usb_serial "$serial")" && break
done
[ -n "$models" ] && models="$(one_of "$models")" && how="recognised by its USB serial number"
if [ -z "$models" ] && (( probe )); then
for port in "${ports[@]}"; do
reason="$(cant_probe "$DEVDIR/$port")"
if [ -n "$reason" ]; then
lines+=(" Couldn't ask on $port: $reason")
elif probe_port "$DEVDIR/$port"; then
models="$(describe_answer)"
how="answered on $port at $ANSWER_BAUD baud"
cat_port="$port"
break
elif [ -n "$PROBE_ERROR" ]; then
lines+=(" Couldn't ask on $port: $PROBE_ERROR")
fi
done
fi
if [ -z "$models" ]; then
echo "Unknown radio or interface"
else
echo "${models^}, $how"
fi
for port in "${ports[@]}"; do
if [ "$port" = "$cat_port" ]; then
name="CAT port: "
else
name="Serial port:"
fi
echo " $name $(port_path "$port")"
done
card="$(card_of "$radio")"
if [ -n "$card" ]; then
echo " Sound card: hw:CARD=$(attr "$SYSFS/class/sound/$card" id) ($card)"
else
echo " Sound card: none found with it"
fi
for dev in "${usb_devs[@]}"; do
serial="$(attr "$dev" serial)"
echo " USB: $(attr "$dev" idVendor):$(attr "$dev" idProduct) $(attr "$dev" manufacturer) $(attr "$dev" product)${serial:+, serial $serial}"
done
[ ${#lines[@]} -eq 0 ] || printf '%s\n' "${lines[@]}"
if [ -z "$models" ]; then
if (( probe )); then
[ ${#lines[@]} -eq ${#ports[@]} ] ||
echo " Nothing answered. If it's a radio, check it's switched on and its CAT rate is one of ${BAUDS[*]} baud. Interfaces like the Digirig answer only for a radio behind them"
else
echo " Run \`radio detect --probe\` to ask it what it is"
fi
fi
done
}
# radio probe PORT: ask one port
probe() {
local port="${1:-}" reason
[ -n "$port" ] || { usage >&2; return 2; }
reason="$(cant_probe "$port")"
[ -z "$reason" ] || { echo "Can't ask: $reason" >&2; return 1; }
if probe_port "$port"; then
echo "$(describe_answer), at $ANSWER_BAUD baud"
elif [ -n "$PROBE_ERROR" ]; then
echo "Can't ask: $PROBE_ERROR" >&2
return 1
else
echo "Nothing answered on $port at ${BAUDS[*]} baud"
return 1
fi
}
case "${1:-detect}" in
detect) shift; detect "$@" ;;
probe) shift; probe "$@" ;;
help | -h | --help) usage ;;
--probe) detect --probe ;;
*) usage >&2; exit 2 ;;
esac
+136
View File
@@ -0,0 +1,136 @@
# radios.conf - The radios `radio` recognises
#
# Each [section] is a radio, named as `radio` shows it. Fields:
# hamlib= - Hamlib model number, as `rigctl -l` lists it (required)
# usb_serial= - pattern (as in a shell `case`) matching the serial number of
# the radio's USB serial chip, for radios that put their model
# there. Recognises the radio without talking to it
# cat_id= - the digits of the radio's reply to the Kenwood-style CAT
# command ID; (Kenwood, Yaesu and Elecraft). Several radios
# may give the same reply
# civ= - the radio's default CI-V address in hex (Icom). The radio
# gives it in its reply to "read transceiver ID"
#
# A radio needs usb_serial=, cat_id= or civ=, or `radio` can't recognise it.
# The Hamlib model numbers were checked against Hamlib 4.6. The other values
# come from the radios' CAT manuals and Hamlib's tables; mark one "# verified"
# once a real radio has answered with it.
# Icom
[Icom IC-7300]
hamlib=3073
usb_serial=IC-7300 *
civ=94
[Icom IC-705]
hamlib=3085
usb_serial=IC-705 *
civ=A4
[Icom IC-7610]
hamlib=3078
usb_serial=IC-7610 *
civ=98
[Icom IC-9700]
hamlib=3081
usb_serial=IC-9700 *
civ=A2
[Icom IC-7100]
hamlib=3070
usb_serial=IC-7100 *
civ=88
[Icom IC-905]
hamlib=3090
usb_serial=IC-905 *
civ=AC
# Yaesu. Their USB serial chips (CP2105) carry no model, so they're recognised
# by asking them, with `radio detect --probe`
[Yaesu FT-991A]
hamlib=1035
cat_id=0670
[Yaesu FT-991]
hamlib=1035
cat_id=0570
[Yaesu FT-891]
hamlib=1036
cat_id=0650
[Yaesu FTDX10]
hamlib=1042
cat_id=0761
[Yaesu FTDX101D]
hamlib=1040
cat_id=0681
[Yaesu FTDX101MP]
hamlib=1044
cat_id=0682
[Yaesu FT-710]
hamlib=1049
cat_id=0800
[Yaesu FTDX3000]
hamlib=1037
cat_id=0460
[Yaesu FT-950]
hamlib=1028
cat_id=0310
[Yaesu FT-450D]
hamlib=1046
cat_id=0241
# Kenwood
[Kenwood TS-2000]
hamlib=2014
cat_id=019
[Kenwood TS-480]
hamlib=2028
cat_id=020
[Kenwood TS-590S]
hamlib=2031
cat_id=021
[Kenwood TS-990S]
hamlib=2039
cat_id=022
[Kenwood TS-590SG]
hamlib=2037
cat_id=023
[Kenwood TS-890S]
hamlib=2041
cat_id=024
# Elecraft. They all answer ID; as a Kenwood TS-570 would
[Elecraft K3]
hamlib=2029
cat_id=017
[Elecraft KX3]
hamlib=2045
cat_id=017
[Elecraft KX2]
hamlib=2044
cat_id=017
[Elecraft K4]
hamlib=2047
cat_id=017
+2 -1
View File
@@ -74,7 +74,8 @@ systemctl is-enabled --quiet graywolf 2> /dev/null || problem "graywolf's servic
uv tool list 2> /dev/null | grep -q '^not1mm ' || problem "not1mm isn't installed"
[ -f "$APPLICATIONS_DIR/k6gte-not1mm.desktop" ] || problem "not1mm has no desktop file"
[ -x "$HOME/.local/bin/hampackremove" ] || problem "hampackremove isn't installed"
echo "checked graywolf's service, not1mm and hampackremove"
"$HOME/.local/bin/radio" help > /dev/null 2>&1 || problem "radio isn't installed"
echo "checked graywolf's service, not1mm, hampackremove and radio"
if (( ${#problems[@]} > 0 )); then
echo "${#problems[@]} problem(s)"
+15
View File
@@ -116,6 +116,21 @@ EOF
[[ "$output" == *"[broken] install= should be under \$HOME"* ]]
}
check_radio() {
check_keys hamlib usb_serial cat_id civ
check_required hamlib
[[ "${CONF[hamlib]}" =~ ^[0-9]+$ ]] || problem "hamlib= should be a Hamlib model number"
[ -n "${CONF[usb_serial]}${CONF[cat_id]}${CONF[civ]}" ] || problem "needs usb_serial=, cat_id= or civ="
[ -z "${CONF[cat_id]}" ] || [[ "${CONF[cat_id]}" =~ ^[0-9]{3,4}$ ]] || problem "cat_id= should be 3 or 4 digits"
[ -z "${CONF[civ]}" ] || [[ "${CONF[civ]}" =~ ^[0-9A-F]{2}$ ]] || problem "civ= should be two hex digits, in capitals"
}
@test "radios.conf: every radio has a Hamlib model and a way to recognise it" {
parse_conf "$REPO/radios.conf" check_radio
check_unique_sections "$REPO/radios.conf"
no_problems
}
@test "packages.conf: names are valid, listed once, and the Flatpaks look like app IDs" {
local p
while IFS= read -r p; do
+27
View File
@@ -83,6 +83,33 @@ remove() {
[ ! -e "$SIBLING" ]
}
@test "removes the radio command, but not someone else's" {
ln -s "$SIBLING/radio" "$HOME/.local/bin/radio"
remove
[ ! -L "$HOME/.local/bin/radio" ]
setup
echo theirs > "$HOME/.local/bin/radio"
remove
[ "$(cat "$HOME/.local/bin/radio")" = theirs ]
}
@test "takes you out of the uucp group only if HamPack put you in" {
echo uucp > "$FAKE_GROUPS"
mkdir -p "$HAMPACK_STATE"
echo uucp > "$HAMPACK_STATE/added-groups"
remove
[ "$status" -eq 0 ]
grep -qx "gpasswd -d $USER uucp" "$STUB_LOG"
! grep -qx uucp "$FAKE_GROUPS"
setup
echo uucp > "$FAKE_GROUPS"
remove
! grep -q gpasswd "$STUB_LOG"
grep -qx uucp "$FAKE_GROUPS"
}
@test "does nothing unless confirmed" {
mark_installed fldigi
mkdir -p "$HAMPACK_STATE"
+13
View File
@@ -33,6 +33,19 @@ setup_fake_home() {
case "$1" in -v|-n) exit 0 ;; esac
if [ -x "$STUB_DIR/$1" ]; then "$@"; else exit 0; fi'
# Fake group database: the groups you're in, one per line, in $FAKE_GROUPS.
# `id -nG` (this login's groups) reads $FAKE_SESSION_GROUPS if set, as a
# login from before usermod would
export FAKE_GROUPS="$BATS_TEST_TMPDIR/groups"
unset FAKE_SESSION_GROUPS
: > "$FAKE_GROUPS"
stub id '
[ "$1" = -nG ] || exit 1
if [ -n "$2" ]; then groups="$FAKE_GROUPS"; else groups="${FAKE_SESSION_GROUPS:-$FAKE_GROUPS}"; fi
echo "$USER" $(cat "$groups")'
stub usermod '[ "$1" = -aG ] && echo "$2" >> "$FAKE_GROUPS"'
stub gpasswd '[ "$1" = -d ] && grep -vx "$3" "$FAKE_GROUPS" > "$FAKE_GROUPS.new"; mv "$FAKE_GROUPS.new" "$FAKE_GROUPS"'
stub pacman '
case "$1" in
-Qi|-Q|-Qq)
+310
View File
@@ -0,0 +1,310 @@
#!/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" ]
}
+1
View File
@@ -37,6 +37,7 @@ fake_installed_machine() {
)
touch "$HOME/.local/share/applications/k6gte-not1mm.desktop" "$HOME/.local/bin/hampackremove"
chmod +x "$HOME/.local/bin/hampackremove"
ln -s "$REPO/radio" "$HOME/.local/bin/radio"
}
@test "smoketest: health passes on a machine with everything installed" {
+31
View File
@@ -40,6 +40,37 @@ setup() {
[ "$(wc -l < "$HAMPACK_STATE/installed-flatpaks")" -eq "$(all_hampack_flatpaks | wc -l)" ]
grep -qx "uv tool install not1mm" "$STUB_LOG"
[ -x "$HOME/.local/bin/hampackremove" ]
[ "$(readlink "$HOME/.local/bin/radio")" = "$REPO/radio" ]
"$HOME/.local/bin/radio" help | grep -q "Usage: radio"
}
@test "adds you to the uucp group once, and records it" {
run bash "$REPO/update.sh"
[ "$status" -eq 0 ]
[[ "$output" == *"Added you to the uucp group"*"Log out and back in"* ]]
grep -qx "usermod -aG uucp $USER" "$STUB_LOG"
grep -qx uucp "$HAMPACK_STATE/added-groups"
: > "$STUB_LOG"
run bash "$REPO/update.sh"
[[ "$output" != *"Added you to the uucp group"* ]]
! grep -q usermod "$STUB_LOG"
}
@test "doesn't claim the uucp group if you were already in it" {
echo uucp > "$FAKE_GROUPS"
run bash "$REPO/update.sh"
[ "$status" -eq 0 ]
! grep -q usermod "$STUB_LOG"
[ ! -e "$HAMPACK_STATE/added-groups" ]
}
@test "update leaves alone a radio command that isn't HamPack's" {
echo "theirs" > "$HOME/.local/bin/radio"
run bash "$REPO/update.sh"
[ "$status" -eq 0 ]
[[ "$output" == *"Not installing HamPack's radio command"* ]]
[ "$(cat "$HOME/.local/bin/radio")" = theirs ]
}
@test "a later update installs and records nothing new" {
+19
View File
@@ -70,6 +70,24 @@ remove_stale_files() {
install_tools() {
mkdir -p "$HOME/.local/bin"
install_script "$SELF/hampackremove" "$HOME/.local/bin/hampackremove"
# A link, so radio finds radios.conf beside it and updates with HamPack. A
# radio that isn't HamPack's is left alone
local radio="$HOME/.local/bin/radio"
if [ -e "$radio" ] && [ "$(readlink -f "$radio")" != "$(readlink -f "$SELF/radio")" ]; then
echo " Not installing HamPack's radio command: $radio is something else. Run $SELF/radio instead"
else
ln -sfn "$SELF/radio" "$radio"
fi
}
# Radios' CAT ports, like all serial ports on Arch, belong to the uucp group,
# so you need to be in it to control a radio. It's recorded, so hampackremove
# takes you out again only if HamPack put you in.
join_serial_group() {
id -nG "$USER" | tr ' ' '\n' | grep -qx uucp && return 0
sudo usermod -aG uucp "$USER" || return 1
record_installed added-groups uucp
echo " Added you to the uucp group, so programs can control your radio. Log out and back in for it to take effect."
}
# Install the packages in packages.conf, and record the ones that weren't
@@ -145,6 +163,7 @@ fi
step "Removing stale HamPack files" remove_stale_files
step "Installing HamPack tools" install_tools
step "Adding you to the uucp group, for radios' serial ports" join_serial_group
step "Installing HamPack packages" install_hampack_packages
step "Installing Flatpak applications" bash "$SELF/install-flatpaks.sh"
step "Installing pre-built binaries" bash "$SELF/install-binaries.sh"