diff --git a/PLAN.md b/PLAN.md index 9bdf8c9..1a200a4 100644 --- a/PLAN.md +++ b/PLAN.md @@ -15,10 +15,9 @@ group (6c76f94), and the start of `radio configure`. so `radio` lists all four. Find an Elecraft command whose reply names the model - [ ] `radio configure`, the rest. Started: it writes the rigctld user - service and points WSJT-X, JS8Call, Pat, fldigi and Dire Wolf at it. - Still to do: - - [ ] QLog, not1mm (its rigctld setting in `not1mm.json`), POTACAT and - Graywolf + service and points WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm and + QLog at it. Still to do: + - [ ] POTACAT and Graywolf - [ ] Sound cards. Each program saves its sound device under a name from its audio backend (WSJT-X saved "pipewire" here), so find what each saves for a USB codec on a real machine before writing it. Dire Wolf diff --git a/README.md b/README.md index a6030eb..8cd1385 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 and Dire Wolf at rigctld, with PTT through it. Dire Wolf also gets the radio's sound card. 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, and points WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm and QLog at rigctld, with PTT through it. QLog gets a rig profile named HamPack in its log, which is backed up first. Dire Wolf also gets the radio's sound card. 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` only fills in rig settings you haven't set. `radio configure --force` replaces them, and every file it changes is backed up first. With more than one radio plugged in, give the number `radio` lists for it: `radio configure 2`. For a radio it can't tell apart from others, give its Hamlib model number: `radio configure --model 2045`. Choose the radio's sound card in the other programs yourself; `radio configure` says which it is. diff --git a/TEST.md b/TEST.md index 8dc76c5..6d57e1c 100644 --- a/TEST.md +++ b/TEST.md @@ -45,8 +45,8 @@ cable. ## radio configure -With the radio on a dummy load or an antenna, and WSJT-X, JS8Call, Pat, fldigi -and Dire Wolf quit: +With the radio on a dummy load or an antenna, and WSJT-X, JS8Call, Pat, fldigi, +Dire Wolf, not1mm and QLog quit: 1. Run `radio configure`. It should name the radio, its by-id port and a baud rate, and say what it did for each program. @@ -68,16 +68,24 @@ and Dire Wolf quit: It should open plughw:CARD=...,DEV=0 and not complain about "PTT RIG 2 127.0.0.1:4532". If it says RIG needs hamlib support, its build didn't find Hamlib. Send a beacon and check it keys the radio. -8. Run `radio configure` again: each program is "already set up", and +8. Open not1mm (after running it once). Its settings show rigctld at + 127.0.0.1:4532, and the frequency follows the radio. +9. Open QLog (after running it once, so it has a log). Settings > Equipment > + Rig has a "HamPack" profile (Hamlib, NET rigctl, 127.0.0.1:4532), it's the + one selected, and connecting shows the radio's frequency. The profile was + written straight into QLog's log, with the values QLog 0.54.2's own form + gives a new profile, so check that QLog shows and saves it without + complaint, and that your QSOs are all still there. +10. Run `radio configure` again: each program is "already set up", and rigctld restarts. -9. With a rig set by hand in WSJT-X, `radio configure` leaves it alone, and +11. 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. -10. Unplug the radio, plug it in again, and check rigctld reconnects within +12. Unplug the radio, plug it in again, and check rigctld reconnects within a few seconds. Reboot and check it starts at login. -| Date | Commit | Radio | rigctld | WSJT-X | JS8Call | Pat | fldigi | Dire Wolf | Notes | -|------|--------|-------|---------|--------|---------|-----|--------|-----------|-------| -| | | | | | | | | | | +| Date | Commit | Radio | rigctld | WSJT-X | JS8Call | Pat | fldigi | Dire Wolf | not1mm | QLog | Notes | +|------|--------|-------|---------|--------|---------|-----|--------|-----------|--------|------|-------| +| | | | | | | | | | | | | ## Pairing radios with sound cards diff --git a/radio b/radio index a62c985..1bdb7a2 100755 --- a/radio +++ b/radio @@ -30,6 +30,9 @@ WSJTX_CONF="$HOME/.var/app/$WSJTX_FLATPAK/config/WSJT-X.ini" JS8CALL_CONF="$CONFIG_HOME/JS8Call.ini" PAT_CONF="$CONFIG_HOME/pat/config.json" FLDIGI_CONF="$HOME/.fldigi/fldigi_def.xml" +NOT1MM_CONF="$CONFIG_HOME/not1mm/not1mm.json" +QLOG_FLATPAK=io.github.foldynl.QLog +QLOG_DB="$HOME/.var/app/$QLOG_FLATPAK/data/hamradio/QLog/qlog.db" DIREWOLF_CONF="$HAMPACK_STATE/conf.d/direwolf.conf" # What HamPack's conf.d/direwolf.conf ships with, which configure replaces DIREWOLF_DEFAULT_ADEVICE="plughw:0,0" @@ -51,8 +54,8 @@ Usage: radio [detect] [--probe] 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, Pat, fldigi and Dire Wolf at - rigctld. Only changes + and point WSJT-X, JS8Call, Pat, fldigi, Dire Wolf, not1mm + and QLog 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 @@ -700,6 +703,129 @@ configure_direwolf() { report "Dire Wolf" "$message" } +# not1mm: rig control through rigctld, unless it uses flrig, TCI or another +# rigctld already +configure_not1mm() { + local tmp + if [ ! -x "$HOME/.local/bin/not1mm" ]; then + report not1mm "not installed" + return + fi + if [ ! -f "$NOT1MM_CONF" ]; then + report not1mm "skipped: it has no settings yet. Run not1mm once, then \`radio configure\` again" + return + fi + if running not1mm; then + report not1mm "skipped, because it's running and would overwrite its settings when it quits. Quit it and run \`radio configure\` again" + return + fi + case "$(jq -r --arg addr "$RIGCTLD_ADDR" ' + if .userigctld == true and "\(.CAT_ip):\(.CAT_port)" == $addr then "done" + elif .userigctld == true or .useflrig == true or .usetci == true then "set" + else "unset" end' "$NOT1MM_CONF")" in + done) + report not1mm "already set up" + return ;; + set) + if (( ! FORCE )); then + report not1mm "left alone: it has rig control set up already. \`radio configure --force\` replaces that" + return + fi ;; + unset) ;; + *) + report not1mm "couldn't read $NOT1MM_CONF" + return 1 ;; + esac + back_up "$NOT1MM_CONF" + tmp="$(mktemp "$NOT1MM_CONF.XXXXXX")" || return 1 + if jq --arg ip "${RIGCTLD_ADDR%:*}" --argjson port "${RIGCTLD_ADDR#*:}" ' + .userigctld = true | .useflrig = false | .usetci = false | .CAT_ip = $ip | .CAT_port = $port + ' "$NOT1MM_CONF" > "$tmp" && chmod --reference="$NOT1MM_CONF" "$tmp" && mv -f "$tmp" "$NOT1MM_CONF"; then + report not1mm "rig control through rigctld" + else + rm -f "$tmp" + report not1mm "couldn't change $NOT1MM_CONF" + return 1 + fi +} + +# QLog keeps its rig profiles in its log database. Add a "HamPack" profile for +# rigctld (Hamlib's NET rigctl) and select it, if there are no profiles yet. +# The values are those QLog 0.54's settings dialog gives a new profile. QLog +# keeps recent changes in qlog.db-wal, so the log is backed up (to BACKUP) +# through SQLite, which includes them. qlog_profile BACKUP prints done, kept, +# added, old (a database without the columns these need) or the error +qlog_profile() { + python3 -I -c ' +import sqlite3, sys +db, backup, host, port, force = sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]), sys.argv[5] == "1" +profile = { + "profile_name": "HamPack", "model": 2, "port_pathname": "", "hostname": host, "netport": port, + "baudrate": 0, "databits": 0, "stopbits": 0.0, "flowcontrol": "", "parity": "", + "pollinterval": 500, "txfreq_start": 0.0, "txfreq_end": 0.0, "get_freq": 1, "get_mode": 1, + "get_vfo": 1, "get_pwr": 1, "rit_offset": 0.0, "xit_offset": 0.0, "get_rit": 0, "get_xit": 0, + "default_pwr": 100.0, "get_ptt": 0, "qsy_wiping": 1, "get_key_speed": 1, "assigned_cw_key": " ", + "key_speed_sync": 0, "driver": 1, "dxspot2rig": 0, "ptt_type": "", "ptt_port_pathname": "", + "rts": "", "dtr": "", "civaddr": None, "share_rigctld": 0, "rigctld_port": 4532, + "rigctld_path": "", "rigctld_args": "", "get_split": 0, "cwkey_output": 0, "selected": 1, +} +try: + con = sqlite3.connect(db) + columns = {row[1].lower() for row in con.execute("PRAGMA table_info(rig_profiles)")} + if not set(profile) <= columns: + print("old") + sys.exit(0) + if con.execute("SELECT 1 FROM rig_profiles WHERE driver = 1 AND model = 2 AND hostname = ? AND netport = ? AND IFNULL(selected, 0) = 1", + (host, port)).fetchone(): + print("done") + sys.exit(0) + if con.execute("SELECT 1 FROM rig_profiles WHERE profile_name != ?", ("HamPack",)).fetchone() and not force: + print("kept") + sys.exit(0) + copy = sqlite3.connect(backup) + con.backup(copy) + copy.close() + with con: + con.execute("DELETE FROM rig_profiles WHERE profile_name = ?", ("HamPack",)) + con.execute("UPDATE rig_profiles SET selected = 0") + con.execute("INSERT INTO rig_profiles (%s) VALUES (%s)" % (", ".join(profile), ", ".join("?" * len(profile))), + list(profile.values())) + print("added") +except (sqlite3.Error, OSError) as e: + print(e) +' "$QLOG_DB" "$1" "${RIGCTLD_ADDR%:*}" "${RIGCTLD_ADDR#*:}" "$FORCE" +} + +configure_qlog() { + local result + if ! flatpak info "$QLOG_FLATPAK" > /dev/null 2>&1; then + report QLog "not installed" + return + fi + if [ ! -f "$QLOG_DB" ]; then + report QLog "skipped: it has no log yet. Run QLog once, then \`radio configure\` again" + return + fi + if running qlog; then + report QLog "skipped, because it's running and would overwrite its rig profiles. Quit it and run \`radio configure\` again" + return + fi + local backup + backup="$QLOG_DB.bak-$(date +%Y%m%d-%H%M%S)" + result="$(qlog_profile "$backup")" + case "$result" in + added) + BACKUPS+=("$backup") + report QLog "rig profile \"HamPack\" is rigctld, and selected" ;; + done) report QLog "already set up" ;; + kept) report QLog "left alone: it has rig profiles already. \`radio configure --force\` adds one for rigctld and selects it" ;; + old) report QLog "skipped: its log is older than this radio knows. Open QLog once to update it, then \`radio configure\` again" ;; + *) + report QLog "couldn't change $QLOG_DB: $result" + return 1 ;; + esac +} + # Write and (re)start rigctld's user service: write_rigctld NAME HAMLIB PORT BAUD write_rigctld() { local unit @@ -818,13 +944,15 @@ configure() { configure_pat configure_fldigi configure_direwolf "$card" + configure_not1mm + configure_qlog 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)\"." + echo "Choose the radio's sound card in the other programs 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 diff --git a/tests/radio.bats b/tests/radio.bats index d04a5d7..9fd81a7 100644 --- a/tests/radio.bats +++ b/tests/radio.bats @@ -13,6 +13,8 @@ setup() { 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" + REAL_PYTHON3="$(command -v python3)" + export REAL_PYTHON3 mkdir -p "$ROOT_HUB" "$RADIO_SYSFS/class/tty" "$RADIO_SYSFS/class/sound" \ "$RADIO_DEV/serial/by-id" "$RADIO_PROC" "$RADIO_LOCKDIR" "$FAKE_RADIOS" } @@ -74,7 +76,12 @@ yaesu() { # 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' + # Only the probe is faked; other Python (QLog's log) runs for real + stub python3 ' + case "$3" in + *TIOCMBIC*) f="$FAKE_RADIOS/${4##*/}"; [ -f "$f" ] && cat "$f"; exit 0 ;; + esac + exec "$REAL_PYTHON3" "$@"' } @test "recognises an Icom by its USB serial number, with its port and sound card" { @@ -339,9 +346,49 @@ JSON chmod +x "$HOME/.local/bin/direwolf" mkdir -p "$HAMPACK_STATE/conf.d" cp "$REPO/conf.d/direwolf.conf" "$HAMPACK_STATE/conf.d/" + touch "$HOME/.local/bin/not1mm" + chmod +x "$HOME/.local/bin/not1mm" + mkdir -p "$HOME/.config/not1mm" + echo '{"mycall": "N0CALL", "userigctld": false, "useflrig": false, "usetci": false, "CAT_ip": "127.0.0.1", "CAT_port": 12345}' > "$HOME/.config/not1mm/not1mm.json" + touch "$FAKE_FLATPAKS/io.github.foldynl.QLog" + qlog_log } FLDIGI_XML() { echo "$HOME/.fldigi/fldigi_def.xml"; } +NOT1MM_JSON() { echo "$HOME/.config/not1mm/not1mm.json"; } +QLOG_DB() { echo "$HOME/.var/app/io.github.foldynl.QLog/data/hamradio/QLog/qlog.db"; } + +# QLog's log, with rig_profiles as QLog 0.54 has it, and a QSO to show the +# rest of the log is untouched. qlog_log [old] leaves out the newest column +qlog_log() { + mkdir -p "$(dirname "$(QLOG_DB)")" + "$REAL_PYTHON3" -I - "$(QLOG_DB)" "${1:-}" << 'PY' +import sqlite3, sys +columns = ("profile_name TEXT PRIMARY KEY, model NUMBER NOT NULL, port_pathname TEXT, hostname TEXT, " + "netport NUMBER, baudrate NUMBER, databits NUMBER, stopbits REAL, flowcontrol TEXT, parity TEXT, " + "pollinterval INTEGER, txfreq_start REAL, txfreq_end REAL, get_freq INTEGER, get_mode INTEGER, " + "get_vfo INTEGER, get_pwr INTEGER, rit_offset REAL, xit_offset REAL, get_rit INTEGER, get_xit INTEGER, " + "get_ptt INTEGER DEFAULT 0, default_pwr REAL DEFAULT 100.0, qsy_wiping INTEGER DEFAULT 0, " + "get_key_speed INTEGER DEFAULT 1, assigned_cw_key TEXT DEFAULT ' ', key_speed_sync INTEGER DEFAULT 0, " + "dxspot2rig INTEGER DEFAULT 0, driver INTEGER DEFAULT 0, selected INTEGER, ptt_type TEXT, " + "ptt_port_pathname TEXT, rts TEXT, dtr TEXT, civaddr INTEGER, get_split INTEGER DEFAULT 0, " + "share_rigctld INTEGER DEFAULT 0, rigctld_port INTEGER DEFAULT 4532, rigctld_path TEXT, rigctld_args TEXT") +if sys.argv[2] != "old": + columns += ", cwkey_output INTEGER DEFAULT 0" +con = sqlite3.connect(sys.argv[1]) +con.execute("PRAGMA journal_mode = WAL") +con.execute("CREATE TABLE rig_profiles (%s)" % columns) +con.execute("CREATE TABLE contacts (callsign TEXT)") +con.execute("INSERT INTO contacts VALUES ('W1AW')") +con.commit() +con.close() +PY +} + +qlog_query() { "$REAL_PYTHON3" -I -c 'import sqlite3, sys +con = sqlite3.connect(sys.argv[1]) +for row in con.execute(sys.argv[2]): print("|".join(map(str, row))) +con.commit()' "${2:-$(QLOG_DB)}" "$1"; } DIREWOLF_CONF() { echo "$HAMPACK_STATE/conf.d/direwolf.conf"; } @test "configure runs rigctld for the radio on its stable port, at the rate it answered" { @@ -353,7 +400,7 @@ DIREWOLF_CONF() { echo "$HAMPACK_STATE/conf.d/direwolf.conf"; } 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 restart hampack-rigctld" "$STUB_LOG" - [[ "$output" == *"Choose the radio's sound card in each program yourself: hw:CARD=CODEC, \"USB Audio CODEC\""* ]] + [[ "$output" == *"Choose the radio's sound card in the other programs yourself: hw:CARD=CODEC, \"USB Audio CODEC\""* ]] grep -qx "hamlib=3073" "$HAMPACK_STATE/radio" grep -qx "card=hw:CARD=CODEC" "$HAMPACK_STATE/radio" } @@ -567,3 +614,77 @@ DIREWOLF_CONF() { echo "$HAMPACK_STATE/conf.d/direwolf.conf"; } [[ "$output" == *"Dire Wolf: PTT through rigctld"* ]] grep -qx "ADEVICE plughw:0,0" "$(DIREWOLF_CONF)" } + +@test "configure points not1mm at rigctld" { + configurable + run bash "$REPO/radio" configure + [ "$status" -eq 0 ] + [[ "$output" == *"not1mm: rig control through rigctld"* ]] + [ "$(jq -c '[.userigctld, .useflrig, .usetci, .CAT_ip, .CAT_port, .mycall]' "$(NOT1MM_JSON)")" = '[true,false,false,"127.0.0.1",4532,"N0CALL"]' ] + + run bash "$REPO/radio" configure + [[ "$output" == *"not1mm: already set up"* ]] +} + +@test "configure leaves not1mm on flrig unless --force, and skips it while it runs" { + configurable + jq '.useflrig = true' "$(NOT1MM_JSON)" > "$BATS_TEST_TMPDIR/n.json" && mv "$BATS_TEST_TMPDIR/n.json" "$(NOT1MM_JSON)" + run bash "$REPO/radio" configure + [[ "$output" == *"not1mm: left alone: it has rig control set up already"* ]] + [ "$(jq .useflrig "$(NOT1MM_JSON)")" = true ] + + FAKE_RUNNING=not1mm run bash "$REPO/radio" configure --force + [[ "$output" == *"not1mm: skipped, because it's running"* ]] + [ "$(jq .useflrig "$(NOT1MM_JSON)")" = true ] + + run bash "$REPO/radio" configure --force + [ "$(jq -c '[.userigctld, .useflrig]' "$(NOT1MM_JSON)")" = '[true,false]' ] +} + +@test "configure adds and selects a rigctld profile in QLog, backing up the log first" { + configurable + run bash "$REPO/radio" configure + [ "$status" -eq 0 ] + [[ "$output" == *"QLog: rig profile \"HamPack\" is rigctld, and selected"* ]] + [ "$(qlog_query "SELECT profile_name, driver, model, hostname, netport, selected, get_freq, pollinterval FROM rig_profiles")" = "HamPack|1|2|127.0.0.1|4532|1|1|500" ] + [ "$(qlog_query "SELECT callsign FROM contacts")" = W1AW ] + # The backup is the log as it was + local backup + backup="$(ls "$(QLOG_DB)".bak-*)" + [ "$(qlog_query "SELECT COUNT(*) FROM rig_profiles" "$backup")" = 0 ] + [ "$(qlog_query "SELECT callsign FROM contacts" "$backup")" = W1AW ] + + run bash "$REPO/radio" configure + [[ "$output" == *"QLog: already set up"* ]] + [ "$(ls "$(QLOG_DB)".bak-* | wc -l)" -eq 1 ] +} + +@test "configure leaves QLog's own rig profiles unless --force, which selects its own" { + configurable + qlog_query "INSERT INTO rig_profiles (profile_name, model, driver, port_pathname, selected) VALUES ('IC-7300', 3073, 1, '/dev/ttyUSB0', 1)" > /dev/null + run bash "$REPO/radio" configure + [[ "$output" == *"QLog: left alone: it has rig profiles already"* ]] + [ "$(qlog_query "SELECT profile_name FROM rig_profiles")" = "IC-7300" ] + ! ls "$(QLOG_DB)".bak-* 2> /dev/null + + run bash "$REPO/radio" configure --force + [ "$(qlog_query "SELECT profile_name, selected FROM rig_profiles ORDER BY profile_name")" = "$(printf 'HamPack|1\nIC-7300|0')" ] +} + +@test "configure skips QLog when its log is missing, older than it knows, or QLog is running" { + configurable + rm "$(QLOG_DB)" + run bash "$REPO/radio" configure + [[ "$output" == *"QLog: skipped: it has no log yet. Run QLog once"* ]] + + qlog_log old + run bash "$REPO/radio" configure + [[ "$output" == *"QLog: skipped: its log is older than this radio knows"* ]] + [ "$(qlog_query "SELECT COUNT(*) FROM rig_profiles")" = 0 ] + + rm "$(QLOG_DB)"* + qlog_log + FAKE_RUNNING=qlog run bash "$REPO/radio" configure + [[ "$output" == *"QLog: skipped, because it's running"* ]] + [ "$(qlog_query "SELECT COUNT(*) FROM rig_profiles")" = 0 ] +}