Start radio configure: rigctld for the radio, with WSJT-X, JS8Call and Pat on it

`radio configure` asks the radio what it is and the baud rate it answers
at, then runs rigctld for it as the hampack-rigctld systemd user service
on 127.0.0.1:4532, using the port's /dev/serial/by-id name. WSJT-X (the
Flatpak) and JS8Call get Hamlib NET rigctl with PTT by CAT; Pat gets a
"hampack" rig, used by ARDOP and VARA where they had none. Rig settings
you've set are left alone unless --force, each file is backed up first,
and programs that are running are skipped. With several radios, give the
number `radio` now lists; --model sets the Hamlib model for a radio it
can't tell apart from others. Sound cards are left for you to choose.

hampackremove stops and removes the service. PLAN.md lists the programs
still to do; TEST.md has the checks on a real radio.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David Young
2026-11-28 12:22:16 -07:00
co-authored by Claude Opus 5.5
parent e71f1e59c2
commit eddd363e86
7 changed files with 627 additions and 92 deletions
+383 -72
View File
@@ -1,10 +1,12 @@
#!/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
# serial port, its sound card, and what model it is. Then set up
# rigctld for one, and point HamPack's programs at it
#
# Usage : radio [detect] [--probe]
# radio probe PORT
# radio configure [--force] [--model N] [NUMBER]
# radio help
SELF="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
@@ -20,6 +22,17 @@ LOCKDIR="${RADIO_LOCKDIR:-/run/lock}"
# CAT baud rates to try, most likely first
BAUDS=(38400 115200 19200 9600 4800)
CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
RIGCTLD_UNIT="$CONFIG_HOME/systemd/user/hampack-rigctld.service"
RIGCTLD_ADDR=127.0.0.1:4532
WSJTX_FLATPAK=io.sourceforge.wsjt.wsjtx
WSJTX_CONF="$HOME/.var/app/$WSJTX_FLATPAK/config/WSJT-X.ini"
JS8CALL_CONF="$CONFIG_HOME/JS8Call.ini"
PAT_CONF="$CONFIG_HOME/pat/config.json"
# WSJT-X and JS8Call save PTT_method_CAT like this: a Qt variant holding the
# enum's name. The bytes before each name are its length, with the NUL
PTT_CAT='@Variant(\0\0\0\x7f\0\0\0\x1eTransceiverFactory::PTTMethod\0\0\0\0\xfPTT_method_CAT\0)'
usage() {
cat << EOF
Usage: radio [detect] [--probe]
@@ -31,6 +44,14 @@ Usage: radio [detect] [--probe]
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
configure Set up the radio (if there's more than one, give its number
from \`radio\`): run rigctld for it as a systemd user service,
and point WSJT-X, JS8Call and Pat at rigctld. Only changes
settings you haven't set, and backs up each file it changes.
Quit those programs first
--force Replace rig settings you've set yourself
--model N Use Hamlib model N (\`rigctl -l\`), for a radio that radio
can't tell apart from others, or doesn't know
EOF
}
@@ -117,8 +138,9 @@ port_path() {
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)
# Print the radios in radios.conf whose field matches, as NAME|HAMLIB, one to
# a line: 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
@@ -133,7 +155,7 @@ match_radio() {
else
[ "${want,,}" = "${MATCH_VALUE,,}" ] || return 0
fi
MATCHES+=("${CONF[_name]} (Hamlib model ${CONF[hamlib]})")
MATCHES+=("${CONF[_name]}|${CONF[hamlib]}")
}
# Print why a port can't be probed, or nothing if it can
@@ -233,12 +255,27 @@ probe_port() {
[ -n "$ANSWER_VALUE" ]
}
# Join the radios matching prints into one line
one_of() {
if [ "$(wc -l <<< "$1")" -eq 1 ]; then
echo "$1"
# Describe the radios matching prints, in one line
models_text() {
local line names=()
while IFS= read -r line; do
names+=("${line%|*} (Hamlib model ${line##*|})")
done <<< "$1"
if [ ${#names[@]} -eq 1 ]; then
echo "${names[0]}"
else
echo "one of: $(paste -sd '|' <<< "$1" | sed 's/|/, /g')"
local IFS='|'
line="${names[*]}"
echo "one of: ${line//|/, }"
fi
}
# Describe a radio that answered, but that radios.conf doesn't know
unknown_answer_text() {
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
}
@@ -246,76 +283,106 @@ one_of() {
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
if [ -n "$models" ]; then models_text "$models"; else unknown_answer_text; 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 the radios: RADIOS lists them (the USB device or hub each is), and
# RADIO_PORTS[radio] its serial ports
find_radios() {
local port radio
declare -gA RADIO_PORTS=()
RADIOS=()
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
mapfile -t RADIOS < <(printf '%s\n' "${!RADIO_PORTS[@]}" | sort | grep .)
[ ${#RADIOS[@]} -gt 0 ]
}
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]}"
# Work out what a radio is: identify RADIO WHEN, where WHEN says when to ask
# it: never, unknown (only if its USB serial number doesn't say) or always
# (to find its CAT port and baud rate). Sets:
# ID_PORTS, ID_USB its serial ports, and the USB devices they're on
# ID_MODELS the radios it could be, as matching prints them
# ID_HOW how it was recognised
# ID_CAT_PORT, ID_BAUD the port and baud rate it answered on
# ID_ANSWER what it answered, if radios.conf doesn't know it
# ID_NOTES ports it couldn't be asked on, and why
# ID_ASKED 1 if any port could be asked
identify() {
local radio="$1" when="$2" port dev serial reason
ID_PORTS=() ID_USB=() ID_MODELS="" ID_HOW="" ID_CAT_PORT="" ID_BAUD="" ID_ANSWER="" ID_NOTES=() ID_ASKED=0
read -ra ID_PORTS <<< "${RADIO_PORTS[$radio]}"
for port in "${ID_PORTS[@]}"; do
dev="${PORT_USB[$port]}"
[[ " ${ID_USB[*]} " == *" $dev "* ]] || ID_USB+=("$dev")
done
# 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"
for dev in "${ID_USB[@]}"; do
serial="$(attr "$dev" serial)"
[ -n "$serial" ] && ID_MODELS="$(matching usb_serial "$serial")" && break
done
[ -z "$ID_MODELS" ] || ID_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
[ "$when" = always ] || { [ "$when" = unknown ] && [ -z "$ID_MODELS" ]; } || return 0
for port in "${ID_PORTS[@]}"; do
reason="$(cant_probe "$DEVDIR/$port")"
if [ -n "$reason" ]; then
ID_NOTES+=("Couldn't ask on $port: $reason")
continue
fi
ID_ASKED=1
if probe_port "$DEVDIR/$port"; then
ID_CAT_PORT="$port" ID_BAUD="$ANSWER_BAUD"
if [ -z "$ID_MODELS" ]; then
ID_MODELS="$(matching "$ANSWER_FIELD" "$ANSWER_VALUE")"
[ -n "$ID_MODELS" ] || ID_ANSWER="$(unknown_answer_text)"
ID_HOW="answered on $port at $ANSWER_BAUD baud"
fi
return 0
elif [ -n "$PROBE_ERROR" ]; then
ID_NOTES+=("Couldn't ask on $port: $PROBE_ERROR")
fi
done
}
if [ -z "$models" ]; then
echo "Unknown radio or interface"
# Print the radio's sound card as ALSA names it (hw:CARD=CODEC), or nothing
card_name() {
local card
card="$(card_of "$1")"
[ -z "$card" ] || echo "hw:CARD=$(attr "$SYSFS/class/sound/$card" id)"
}
# radio detect: list each radio and what's known about it
detect() {
local when=never radio port name card dev serial n=0
case "${1:-}" in
--probe) when=unknown ;;
"") ;;
*) usage >&2; return 2 ;;
esac
if ! find_radios; then
echo "No USB serial ports found: is the radio plugged in and switched on?"
return 1
fi
for radio in "${RADIOS[@]}"; do
(( n++ )) && echo
identify "$radio" "$when"
if [ -n "$ID_MODELS" ]; then
name="$(models_text "$ID_MODELS")"
echo "$n. ${name^}, $ID_HOW"
elif [ -n "$ID_ANSWER" ]; then
echo "$n. ${ID_ANSWER^}, $ID_HOW"
else
echo "${models^}, $how"
echo "$n. Unknown radio or interface"
fi
for port in "${ports[@]}"; do
if [ "$port" = "$cat_port" ]; then
for port in "${ID_PORTS[@]}"; do
if [ "$port" = "$ID_CAT_PORT" ]; then
name="CAT port: "
else
name="Serial port:"
@@ -324,22 +391,21 @@ detect() {
done
card="$(card_of "$radio")"
if [ -n "$card" ]; then
echo " Sound card: hw:CARD=$(attr "$SYSFS/class/sound/$card" id) ($card)"
echo " Sound card: $(card_name "$radio") ($card)"
else
echo " Sound card: none found with it"
fi
for dev in "${usb_devs[@]}"; do
for dev in "${ID_USB[@]}"; 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[@]}"
[ ${#ID_NOTES[@]} -eq 0 ] || printf ' %s\n' "${ID_NOTES[@]}"
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
if [ -z "$ID_MODELS$ID_ANSWER" ]; then
if [ "$when" = never ]; then
echo " Run \`radio detect --probe\` to ask it what it is"
elif (( ID_ASKED )); then
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"
fi
fi
done
@@ -362,9 +428,254 @@ probe() {
fi
}
# Print a key's value from a section of an INI file: ini_get FILE SECTION KEY
ini_get() {
[ -f "$1" ] || return 0
S="$2" K="$3" awk '
/^\[/ { in_section = ($0 == "[" ENVIRON["S"] "]"); next }
in_section && index($0, ENVIRON["K"] "=") == 1 { print substr($0, length(ENVIRON["K"]) + 2); exit }
' "$1"
}
# Set a key in a section of an INI file, adding the section (and the file) if
# need be: ini_set FILE SECTION KEY VALUE. Values are written as they are, so
# Qt's escapes (\0, \x7f) stay as Qt wrote them
ini_set() {
local tmp
mkdir -p "$(dirname "$1")" && touch "$1" && tmp="$(mktemp "$1.XXXXXX")" || return 1
S="$2" K="$3" V="$4" awk '
function put() { print ENVIRON["K"] "=" ENVIRON["V"]; done = 1 }
/^\[/ {
if (in_section && !done) put()
in_section = ($0 == "[" ENVIRON["S"] "]")
if (in_section) found = 1
print
next
}
in_section && index($0, ENVIRON["K"] "=") == 1 { if (!done) put(); next }
{ print }
END {
if (in_section && !done) put()
if (!found) { if (NR) print ""; print "[" ENVIRON["S"] "]"; put() }
}
' "$1" > "$tmp" && chmod --reference="$1" "$tmp" && mv -f "$tmp" "$1"
}
# Copy a file before changing it, and say so
back_up() {
[ -f "$1" ] || return 0
local copy
copy="$1.bak-$(date +%Y%m%d-%H%M%S)"
cp -p "$1" "$copy" && BACKUPS+=("$copy")
}
# Say what happened to one program: report NAME MESSAGE
report() {
printf ' %-9s %s\n' "$1:" "$2"
}
running() {
pgrep -x "$1" > /dev/null 2>&1
}
# WSJT-X and JS8Call share their settings: point one at rigctld, with PTT by
# CAT. configure_qt_app NAME FILE PROCESS
configure_qt_app() {
local name="$1" file="$2" rig net
if running "$3"; then
report "$name" "skipped, because it's running and would overwrite its settings when it quits. Quit it and run \`radio configure\` again"
return
fi
rig="$(ini_get "$file" Configuration Rig)"
net="$(ini_get "$file" Configuration CATNetworkPort)"
if [ "$rig" = "Hamlib NET rigctl" ] && [ "$net" = "$RIGCTLD_ADDR" ]; then
report "$name" "already set up"
return
fi
if [ -n "$rig" ] && [ "$rig" != None ] && (( ! FORCE )); then
report "$name" "left alone: it's set up for $rig. \`radio configure --force\` replaces that"
return
fi
back_up "$file"
if ! { ini_set "$file" Configuration Rig "Hamlib NET rigctl" &&
ini_set "$file" Configuration CATNetworkPort "$RIGCTLD_ADDR" &&
ini_set "$file" Configuration PTTMethod "$PTT_CAT"; }; then
report "$name" "couldn't change $file"
return 1
fi
report "$name" "rig control through rigctld, PTT by CAT"
}
# Pat: add rigctld to its rigs as "hampack", and use it for ARDOP and VARA
# where no rig is set
configure_pat() {
local tmp addr modes='["ardop","varahf","varafm"]'
if ! command -v pat > /dev/null; then
report Pat "not installed"
return
fi
if [ ! -f "$PAT_CONF" ]; then
report Pat "skipped: it has no settings yet. Run \`pat configure\` once, then \`radio configure\` again"
return
fi
if running pat; then
report Pat "skipped, because it's running. Stop it and run \`radio configure\` again"
return
fi
addr="$(jq -r '.hamlib_rigs.hampack.address // empty' "$PAT_CONF")" ||
{ report Pat "couldn't read $PAT_CONF"; return 1; }
if [ "$addr" = "$RIGCTLD_ADDR" ] && (( ! FORCE )); then
report Pat "already set up"
return
fi
back_up "$PAT_CONF"
tmp="$(mktemp "$PAT_CONF.XXXXXX")" || return 1
if jq --arg addr "$RIGCTLD_ADDR" --argjson modes "$modes" --argjson force "$FORCE" '
.hamlib_rigs = ((.hamlib_rigs // {}) + {hampack: {address: $addr, network: "tcp"}})
| reduce $modes[] as $m (.;
if .[$m] != null and ($force == 1 or (.[$m].rig // "") == "") then .[$m].rig = "hampack" else . end)
' "$PAT_CONF" > "$tmp" && chmod --reference="$PAT_CONF" "$tmp" && mv -f "$tmp" "$PAT_CONF"; then
report Pat "rig \"hampack\" is rigctld, used by ARDOP and VARA where they had no rig"
else
rm -f "$tmp"
report Pat "couldn't change $PAT_CONF"
return 1
fi
}
# Write and (re)start rigctld's user service: write_rigctld NAME HAMLIB PORT BAUD
write_rigctld() {
local unit
unit="[Unit]
Description=Hamlib rigctld for the $1 (written by HamPack's radio configure)
[Service]
ExecStart=/usr/bin/rigctld -m $2 -r $3 -s $4 -T ${RIGCTLD_ADDR%:*} -t ${RIGCTLD_ADDR#*:}
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target"
mkdir -p "$(dirname "$RIGCTLD_UNIT")" || return 1
printf '%s\n' "$unit" > "$RIGCTLD_UNIT" || return 1
systemctl --user daemon-reload &&
systemctl --user enable hampack-rigctld 2> /dev/null &&
systemctl --user restart hampack-rigctld
}
# Print the name radios.conf gives a Hamlib model, or nothing
hamlib_name() {
awk -F= -v m="$1" '/^\[/ { name = substr($0, 2, length($0) - 2) } $1 == "hamlib" && $2 == m { print name; exit }' "$RADIOS_CONF"
}
# radio configure: set up rigctld for a radio and point the programs at it
configure() {
local number="" model="" radio was_running=0 name port baud card
FORCE=0 BACKUPS=()
while [ $# -gt 0 ]; do
case "$1" in
--force) FORCE=1 ;;
--model)
[[ "${2:-}" =~ ^[0-9]+$ ]] || { usage >&2; return 2; }
model="$2"
shift ;;
[1-9]*)
[[ "$1" =~ ^[0-9]+$ ]] || { usage >&2; return 2; }
number="$1" ;;
*) usage >&2; return 2 ;;
esac
shift
done
if ! find_radios; then
echo "No USB serial ports found: is the radio plugged in and switched on?"
return 1
fi
if [ -z "$number" ]; then
if [ ${#RADIOS[@]} -gt 1 ]; then
echo "There are ${#RADIOS[@]} radios or interfaces plugged in. Give the number \`radio\` lists for the one to set up, as in \`radio configure 2\`"
return 2
fi
number=1
fi
if (( number > ${#RADIOS[@]} )); then
echo "There's no radio $number: \`radio\` lists ${#RADIOS[@]}"
return 2
fi
radio="${RADIOS[number - 1]}"
# rigctld has the port open, if radio configure has run before
if systemctl --user is-active --quiet hampack-rigctld 2> /dev/null; then
systemctl --user stop hampack-rigctld
was_running=1
fi
identify "$radio" always
if [ -z "$ID_CAT_PORT" ]; then
echo "Couldn't set it up: nothing answered on ${ID_PORTS[*]}."
[ ${#ID_NOTES[@]} -eq 0 ] || printf ' %s\n' "${ID_NOTES[@]}"
(( ! ID_ASKED )) || echo "Check it's switched on and its CAT rate is one of ${BAUDS[*]} baud."
(( ! was_running )) || systemctl --user start hampack-rigctld
return 1
fi
if [ -n "$model" ]; then
name="$(hamlib_name "$model")"
name="${name:-radio}"
elif [ -n "$ID_MODELS" ] && [ "$(wc -l <<< "$ID_MODELS")" -eq 1 ]; then
name="${ID_MODELS%|*}"
model="${ID_MODELS#*|}"
else
if [ -n "$ID_MODELS" ]; then
echo "Couldn't tell which radio it is: it's $(models_text "$ID_MODELS")."
else
echo "Couldn't tell which radio it is: it's ${ID_ANSWER}."
fi
echo "Give its Hamlib model number (\`rigctl -l\` lists them), as in \`radio configure --model 2045\`"
(( ! was_running )) || systemctl --user start hampack-rigctld
return 1
fi
port="$(port_path "$ID_CAT_PORT")"
baud="$ID_BAUD"
card="$(card_name "$radio")"
echo "Setting up the $name (Hamlib model $model), on $port at $baud baud"
echo
if write_rigctld "$name" "$model" "$port" "$baud"; then
report rigctld "runs as the hampack-rigctld user service, on $RIGCTLD_ADDR"
else
report rigctld "couldn't start: see \`systemctl --user status hampack-rigctld\`"
fi
[[ "$port" == /dev/serial/by-id/* ]] ||
report "" "$port has no stable name, so it may be a different port after a reboot. Run \`radio configure\` again if so"
if flatpak info "$WSJTX_FLATPAK" > /dev/null 2>&1; then
configure_qt_app WSJT-X "$WSJTX_CONF" wsjtx
else
report WSJT-X "not installed"
fi
if [ -x "$HOME/.local/bin/JS8Call" ]; then
configure_qt_app JS8Call "$JS8CALL_CONF" JS8Call
else
report JS8Call "not installed"
fi
configure_pat
mkdir -p "$HAMPACK_STATE" &&
printf 'name=%s\nhamlib=%s\nport=%s\nbaud=%s\ncard=%s\n' "$name" "$model" "$port" "$baud" "$card" > "$HAMPACK_STATE/radio"
echo
if [ -n "$card" ]; then
echo "Choose the radio's sound card in each program yourself: $card, \"$(attr "${CARD_USB[$(card_of "$radio")]}" product)\"."
else
echo "No sound card was found with the radio: choose the one it's connected to in each program yourself."
fi
[ ${#BACKUPS[@]} -eq 0 ] || { echo "The settings were backed up first, to:"; printf ' %s\n' "${BACKUPS[@]}"; }
}
case "${1:-detect}" in
detect) shift; detect "$@" ;;
probe) shift; probe "$@" ;;
configure) shift; configure "$@" ;;
help | -h | --help) usage ;;
--probe) detect --probe ;;
*) usage >&2; exit 2 ;;