Start rigctld when the radio is plugged in, and stop it when it goes

systemd makes a device unit for each USB serial port and its
/dev/serial/by-id link, so rigctld's user service is now wanted by and
bound to the radio's port: it starts when the radio is plugged in or
switched on (or at login if it's there already) and stops when it goes,
instead of retrying every 5 seconds. reenable clears the login link
earlier versions of the unit had. With --flrig, rigctld talks to flrig
rather than the port, so it still starts at login.

PLAN.md drops running more than one radio at once, for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
David Young
2026-11-28 13:50:09 -07:00
co-authored by Claude Opus 5.5
parent 357f72583e
commit fee5ce341a
5 changed files with 83 additions and 24 deletions
+35 -12
View File
@@ -1377,13 +1377,24 @@ configure_graywolf() {
say_changes Graywolf
}
# Write and (re)start rigctld's user service: write_rigctld DESCRIPTION
# RIGCTLD_ARGS..., the arguments that say which radio and how to reach it
# Write rigctld's user service, and start it if it can run now:
# write_rigctld DESCRIPTION PORT RIGCTLD_ARGS..., with PORT the radio's
# serial port, or nothing when rigctld doesn't open it itself (with flrig).
# With a port, rigctld is bound to it: systemd starts it when the port
# appears (the radio plugged in or switched on, or at login if it's there
# already) and stops it when the port goes. Without one, it starts at login.
# Prints "started", or "waiting" for a radio that isn't there yet
write_rigctld() {
local unit description="$1"
shift
local unit description="$1" port="$2" device="" wanted=default.target
shift 2
if [ -n "$port" ]; then
device="$(systemd-escape --path --suffix=device "$port")" || return 1
wanted="$device"
fi
unit="[Unit]
Description=Hamlib rigctld for $description (written by HamPack's radio configure)
Description=Hamlib rigctld for $description (written by HamPack's radio configure)${device:+
BindsTo=$device
After=$device}
[Service]
ExecStart=/usr/bin/rigctld $* -T ${RIGCTLD_ADDR%:*} -t ${RIGCTLD_ADDR#*:}
@@ -1391,12 +1402,16 @@ Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target"
WantedBy=$wanted"
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
# reenable clears what an earlier version of the unit was wanted by
systemctl --user daemon-reload && systemctl --user reenable hampack-rigctld 2> /dev/null || return 1
if [ -n "$port" ] && [ ! -e "$DEVDIR/${port#/dev/}" ]; then
echo waiting
else
systemctl --user restart hampack-rigctld && echo started
fi
}
# Print a field of a radio in radios.conf: conf_field NAME FIELD
@@ -1510,10 +1525,18 @@ configure() {
echo "Setting up the $name (Hamlib model $model), on $port at $baud baud$( (( USE_FLRIG )) && echo ", through flrig")"
echo
if (( USE_FLRIG )) && write_rigctld "flrig, which controls the $name" -m 4 -r "127.0.0.1:$flrig_port"; then
local rigctld=""
if (( USE_FLRIG )); then
rigctld="$(write_rigctld "flrig, which controls the $name" "" -m 4 -r "127.0.0.1:$flrig_port")"
else
rigctld="$(write_rigctld "the $name" "$port" -m "$model" -r "$port" -s "$baud")"
fi
if [ "$rigctld" = started ] && (( USE_FLRIG )); then
report rigctld "runs as the hampack-rigctld user service, on $RIGCTLD_ADDR, and passes everything to flrig"
elif (( ! USE_FLRIG )) && write_rigctld "the $name" -m "$model" -r "$port" -s "$baud"; then
report rigctld "runs as the hampack-rigctld user service, on $RIGCTLD_ADDR"
elif [ "$rigctld" = started ]; then
report rigctld "runs as the hampack-rigctld user service, on $RIGCTLD_ADDR, whenever the radio is plugged in and on"
elif [ "$rigctld" = waiting ]; then
report rigctld "runs as the hampack-rigctld user service, on $RIGCTLD_ADDR, from when the radio is plugged in and on"
else
report rigctld "couldn't start: see \`systemctl --user status hampack-rigctld\`"
fi