diff --git a/PLAN.md b/PLAN.md index 7adf629..1e9ec63 100644 --- a/PLAN.md +++ b/PLAN.md @@ -8,20 +8,13 @@ keeps the record. Checks that need real radios are in `TEST.md`. Find the radios plugged into USB and set up HamPack's programs for them. Done: `radio detect` and `radio probe`, `radios.conf`, adding you to the uucp group (6c76f94), and `radio configure` for each of HamPack's programs that -control a radio. +control a radio, with rigctld started when the radio is plugged in. - [ ] Run `TEST.md` against real radios, and mark the `radios.conf` values they confirm `# verified`. Fix or remove the ones they don't - [ ] Tell Elecraft's radios apart. The K3, KX3, KX2 and K4 all answer `ID017;`, so `radio` lists all four. Find an Elecraft command whose reply names the model -- [ ] `radio configure`, the rest. It writes the rigctld user service and - points WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm, QLog, POTACAT - and Graywolf at it, or with --flrig, puts flrig in charge of the radio - behind rigctld. Still to do: - - [ ] More than one radio at once: a rigctld for each, on its own port - - [ ] rigctld retries every 5 seconds while the radio is off or unplugged. - Start it when the radio is plugged in instead (see hotplug below) - [ ] PTT through a CM108 sound card (Digirig, AIOC, SignaLink-style interfaces): a udev rule so you can open its `hidraw` device, for Dire Wolf and others diff --git a/README.md b/README.md index dc37899..ffac7ec 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Controlling a radio means opening its serial port, and on Arch only members of t 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. -`radio configure` sets up the radio for HamPack's programs. It asks the radio what it is and at what baud rate it answers. Then it runs Hamlib's rigctld for it, as the `hampack-rigctld` systemd user service on 127.0.0.1:4532, and points WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm, QLog, POTACAT and Graywolf at rigctld, with PTT through it. QLog gets a rig profile named HamPack in its log, which is backed up first. Graywolf's channels that transmit get PTT through rigctld; it's stopped for that with sudo, and started again. They then share the radio through rigctld, without fighting over its serial port. Quit them first, since they save their settings when they quit. +`radio configure` sets up the radio for HamPack's programs. It asks the radio what it is and at what baud rate it answers. Then it runs Hamlib's rigctld for it, as the `hampack-rigctld` systemd user service on 127.0.0.1:4532, which systemd starts whenever the radio is plugged in and switched on, and stops when it isn't, and points WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm, QLog, POTACAT and Graywolf at rigctld, with PTT through it. QLog gets a rig profile named HamPack in its log, which is backed up first. Graywolf's channels that transmit get PTT through rigctld; it's stopped for that with sudo, and started again. They then share the radio through rigctld, without fighting over its serial port. Quit them first, since they save their settings when they quit. It also gives WSJT-X, JS8Call, fldigi, Dire Wolf, not1mm's voice keyer and POTACAT the radio's sound card. POTACAT finds it by name the next time it starts. WSJT-X and JS8Call get it as PipeWire names it, so PipeWire must be running. fldigi and not1mm get PipeWire's name for it too where PortAudio lists it (through PipeWire's JACK support), since ALSA's name for it has a card number that can change between boots; if it does, run `radio configure` again. If Graywolf has no channels yet, it gets one on the radio's sound card, for 1200 baud APRS. diff --git a/TEST.md b/TEST.md index 33b5475..7affefe 100644 --- a/TEST.md +++ b/TEST.md @@ -119,8 +119,14 @@ Dire Wolf, not1mm, QLog and POTACAT quit: 1.4.5's preferences format. 14. With a rig set by hand in WSJT-X, `radio configure` leaves it alone, and `--force` replaces it. The `.bak-` copy has the old settings. -15. Unplug the radio, plug it in again, and check rigctld reconnects within - a few seconds. Reboot and check it starts at login. +15. Unplug the radio (or switch it off, if that takes its USB away): + `systemctl --user status hampack-rigctld` shows it stopped. Plug it in + again: rigctld is running again within a few seconds, without + `radio configure`. Log out with the radio unplugged, log in, and plug it + in: rigctld starts then. Log in with it plugged in: rigctld starts at + login. If rigctld doesn't start on its own, check + `systemctl --user list-units --type=device | grep serial` lists the + radio's port: rigctld is started by that device unit. | Date | Commit | Radio | rigctld | WSJT-X | JS8Call | Pat | fldigi | Dire Wolf | not1mm | QLog | POTACAT | Graywolf | flrig | Notes | |------|--------|-------|---------|--------|---------|-----|--------|-----------|--------|------|---------|----------|-------|-------| diff --git a/radio b/radio index cae4882..9224ad5 100755 --- a/radio +++ b/radio @@ -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 diff --git a/tests/radio.bats b/tests/radio.bats index c2d7408..5f8640f 100644 --- a/tests/radio.bats +++ b/tests/radio.bats @@ -472,7 +472,7 @@ DIREWOLF_CONF() { echo "$HAMPACK_STATE/conf.d/direwolf.conf"; } [ "${lines[0]}" = "Setting up the Icom IC-7300 (Hamlib model 3073), on /dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_IC-7300_03001234-if00-port0 at 38400 baud" ] local unit="$HOME/.config/systemd/user/hampack-rigctld.service" grep -qx "ExecStart=/usr/bin/rigctld -m 3073 -r /dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_IC-7300_03001234-if00-port0 -s 38400 -T 127.0.0.1 -t 4532" "$unit" - grep -qx "systemctl --user enable hampack-rigctld" "$STUB_LOG" + grep -qx "systemctl --user reenable hampack-rigctld" "$STUB_LOG" grep -qx "systemctl --user restart hampack-rigctld" "$STUB_LOG" [[ "$output" == *"PipeWire didn't list the radio's sound card, hw:CARD=CODEC (\"USB Audio CODEC\"), so choose it yourself in WSJT-X, JS8Call and POTACAT."* ]] grep -qx "hamlib=3073" "$HAMPACK_STATE/radio" @@ -1189,3 +1189,40 @@ FLRIG_AUTOSTART() { echo "$HOME/.config/autostart/hampack-flrig.desktop"; } [ -e "$(FLRIG_AUTOSTART)" ] [ -e "$HOME/.config/autostart/my-flrig.desktop" ] } + +# --- rigctld and the radio's port --- + +@test "configure ties rigctld to the radio's port, so it runs when the radio is plugged in" { + configurable + run bash "$REPO/radio" configure + [[ "$output" == *"rigctld: runs as the hampack-rigctld user service, on 127.0.0.1:4532, whenever the radio is plugged in and on"* ]] + local unit="$HOME/.config/systemd/user/hampack-rigctld.service" device + device="$(systemd-escape --path --suffix=device "$IC7300_PORT")" + [[ "$device" == dev-serial-by*id-usb*IC*7300*.device ]] + grep -qxF "BindsTo=$device" "$unit" + grep -qxF "After=$device" "$unit" + grep -qxF "WantedBy=$device" "$unit" + ! grep -q "default.target" "$unit" +} + +@test "configure --flrig starts rigctld at login, since flrig has the port" { + configurable + run bash "$REPO/radio" configure --flrig + local unit="$HOME/.config/systemd/user/hampack-rigctld.service" + grep -qx "WantedBy=default.target" "$unit" + ! grep -q "^BindsTo=" "$unit" +} + +@test "rigctld waits for a radio that isn't plugged in" { + export RADIO_DEV="$BATS_TEST_TMPDIR/dev" + run bash -c ' + source <(sed -n "/^write_rigctld() {/,/^}/p" "$1") + RIGCTLD_UNIT="$HOME/.config/systemd/user/hampack-rigctld.service" RIGCTLD_ADDR=127.0.0.1:4532 DEVDIR="$RADIO_DEV" + write_rigctld "the Icom IC-7300" /dev/serial/by-id/usb-gone-if00-port0 -m 3073 -r /dev/serial/by-id/usb-gone-if00-port0 -s 38400 + ' _ "$REPO/radio" + [ "$status" -eq 0 ] + [ "$output" = waiting ] + grep -qx "systemctl --user reenable hampack-rigctld" "$STUB_LOG" + ! grep -q "systemctl --user restart" "$STUB_LOG" + grep -qx "ExecStart=/usr/bin/rigctld -m 3073 -r /dev/serial/by-id/usb-gone-if00-port0 -s 38400 -T 127.0.0.1 -t 4532" "$HOME/.config/systemd/user/hampack-rigctld.service" +}