Set up a radio radio knows when it's plugged in, with radio configure --auto
A udev rule starts the hampack-radio-plugged user service when a USB serial port appears, and it runs `radio configure --auto`. That sets up a radio recognised by its USB serial number, unless the radio set up already is plugged in, and says what it did in a desktop notification. It never asks a radio what it is, since that may key the transmitter, and never asks for sudo, so a running Graywolf is skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f2e29ab46d
commit
8d15d2c81e
@@ -7,6 +7,7 @@
|
||||
# Usage : radio [detect] [--probe]
|
||||
# radio probe PORT
|
||||
# radio configure [--force] [--model N] [--flrig] [--no-cat --ptt HOW] [NUMBER]
|
||||
# radio configure --auto
|
||||
# radio help
|
||||
|
||||
SELF="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
|
||||
@@ -19,6 +20,10 @@ SYSFS="${RADIO_SYSFS:-/sys}"
|
||||
DEVDIR="${RADIO_DEV:-/dev}"
|
||||
PROC="${RADIO_PROC:-/proc}"
|
||||
LOCKDIR="${RADIO_LOCKDIR:-/run/lock}"
|
||||
# How long radio configure --auto waits for PipeWire to list a radio just
|
||||
# plugged in, in seconds
|
||||
AUTO_WAIT="${RADIO_AUTO_WAIT:-10}"
|
||||
AUTO=0
|
||||
# CAT baud rates to try, most likely first
|
||||
BAUDS=(38400 115200 19200 9600 4800)
|
||||
|
||||
@@ -79,6 +84,11 @@ Usage: radio [detect] [--probe]
|
||||
PTT pin), rts or dtr (its serial port's lines), or vox
|
||||
(the radio keys itself on the audio). A sound card is
|
||||
cm108 if it can be, and vox otherwise
|
||||
configure --auto
|
||||
What runs when a USB serial port appears: set up a radio
|
||||
radio recognises by its USB serial number, without asking
|
||||
it, unless the radio set up already is plugged in. Stop it
|
||||
with \`systemctl --user mask hampack-radio-plugged\`
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -1494,6 +1504,10 @@ configure_graywolf() {
|
||||
if [ "$added" = 1 ] || [[ "$changed" =~ ^[1-9][0-9]*$ ]]; then
|
||||
# It runs as a system service, and reads its settings when it starts
|
||||
if systemctl is-active --quiet graywolf 2> /dev/null; then
|
||||
if (( AUTO )); then
|
||||
report Graywolf "skipped: it's running, and stopping it to change its settings needs sudo. Run \`radio configure\` to set it up"
|
||||
return
|
||||
fi
|
||||
if ! sudo systemctl stop graywolf; then
|
||||
report Graywolf "skipped: couldn't stop it to change its settings. Instead, $manual"
|
||||
return
|
||||
@@ -1575,6 +1589,11 @@ configure() {
|
||||
local number="" model="" radio="" sound_card="" sound_dev="" was_running=0 name port="" baud="" card card_sys
|
||||
local flrig_name="" flrig_port=12345 no_cat=0 ptt="" ptt_type="" ptt_device="" ptt_text="" sound_only total
|
||||
FORCE=0 BACKUPS=() USE_FLRIG=0
|
||||
if [ "${1:-}" = --auto ]; then
|
||||
[ $# -eq 1 ] || { usage >&2; return 2; }
|
||||
auto_configure
|
||||
return
|
||||
fi
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--force) FORCE=1 ;;
|
||||
@@ -1804,6 +1823,84 @@ configure() {
|
||||
[ ${#BACKUPS[@]} -eq 0 ] || { echo "The settings were backed up first, to:"; printf ' %s\n' "${BACKUPS[@]}"; }
|
||||
}
|
||||
|
||||
# Whether the radio radio configure set up last is plugged in: its serial
|
||||
# port is there, or for a sound card with none, the card is
|
||||
set_up_radio_present() {
|
||||
local state="$HAMPACK_STATE/radio" port card c
|
||||
[ -f "$state" ] || return 1
|
||||
port="$(sed -n 's/^port=//p' "$state")"
|
||||
card="$(sed -n 's/^card=hw:CARD=//p' "$state")"
|
||||
if [ -n "$port" ]; then
|
||||
[ -e "$DEVDIR/${port#/dev/}" ]
|
||||
return
|
||||
fi
|
||||
[ -n "$card" ] || return 1
|
||||
for c in "$SYSFS"/class/sound/card*; do
|
||||
[ "$(attr "$c" id)" = "$card" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Show a desktop notification, if there's something to show it with
|
||||
notify() {
|
||||
command -v notify-send > /dev/null && notify-send --app-name=HamPack "$1" "$2" 2> /dev/null
|
||||
return 0
|
||||
}
|
||||
|
||||
# radio configure --auto, which the hampack-radio-plugged user service runs
|
||||
# when a USB serial port appears (udev/70-hampack-radio.rules): set up the
|
||||
# radio just plugged in, if radio recognises it by its USB serial number,
|
||||
# and the radio set up already isn't plugged in. Radios it would have to ask
|
||||
# what they are are left for you to set up: asking raises the port's DTR and
|
||||
# RTS lines, which may key the transmitter, and nobody may be there.
|
||||
# Graywolf is left alone while it runs, since stopping it needs sudo
|
||||
auto_configure() {
|
||||
local i known=() name="" card_sys out status tries=0 skipped
|
||||
# The radio's sound card may come up after its serial port
|
||||
udevadm settle --timeout=10 > /dev/null 2>&1
|
||||
find_radios
|
||||
for i in "${!RADIOS[@]}"; do
|
||||
identify "${RADIOS[i]}" never
|
||||
if [ -n "$ID_MODELS" ] && [ "$(wc -l <<< "$ID_MODELS")" -eq 1 ]; then
|
||||
known+=("$i")
|
||||
name="${ID_MODELS%|*}"
|
||||
fi
|
||||
done
|
||||
if [ ${#known[@]} -eq 0 ]; then
|
||||
echo "No radio that radio recognises by its USB serial number is plugged in: run \`radio configure\` to set one up."
|
||||
return 0
|
||||
elif [ ${#known[@]} -gt 1 ]; then
|
||||
echo "${#known[@]} radios that radio recognises are plugged in: run \`radio configure N\` to set one up."
|
||||
return 0
|
||||
fi
|
||||
if set_up_radio_present; then
|
||||
echo "The radio set up already, the $(sed -n 's/^name=//p' "$HAMPACK_STATE/radio"), is plugged in, so it stays set up."
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Wait for PipeWire to list its sound card, for the programs that use PipeWire's names
|
||||
card_sys="$(card_of "${RADIOS[known[0]]}")"
|
||||
while [ -n "$card_sys" ] && ! find_pipewire_nodes "${card_sys#card}" && (( tries++ < AUTO_WAIT )); do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
AUTO=1
|
||||
out="$(configure "$((known[0] + 1))" 2>&1)"
|
||||
status=$?
|
||||
printf '%s\n' "$out"
|
||||
local details="\`journalctl --user -u hampack-radio-plugged\` says what changed."
|
||||
if (( status != 0 )); then
|
||||
notify "HamPack couldn't set up the $name" "Run \`radio configure\` to see why."
|
||||
return "$status"
|
||||
fi
|
||||
skipped="$(sed -n 's/^ \([^ :][^:]*\): *skipped.*/\1/p' <<< "$out" | paste -sd, - | sed 's/,/, /g')"
|
||||
if [ -n "$skipped" ]; then
|
||||
notify "HamPack set up the $name" "Skipped: $skipped. Run \`radio configure\` to see why, and set them up. $details"
|
||||
else
|
||||
notify "HamPack set up the $name" "HamPack's programs reach it through rigctld. $details"
|
||||
fi
|
||||
}
|
||||
|
||||
case "${1:-detect}" in
|
||||
detect) shift; detect "$@" ;;
|
||||
probe) shift; probe "$@" ;;
|
||||
|
||||
Reference in New Issue
Block a user