Move radio's detection into radio-detect.sh, for HamPackServer to share
Finding the radios, their ports, sound cards and CM108 pins, probing them and looking them up in radios.conf move, unchanged, from radio into radio-detect.sh, which radio sources. It needs nothing else of HamPack (it has parse_conf itself where lib.sh isn't loaded), so HamPackServer's configure can keep a copy. The messages that named radio's commands take them from RADIO_PROGRAM, RADIO_CONFIGURE and RADIO_CM108_ACCESS, whose defaults say what they said before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7697a785d3
commit
a88f09a1cd
+524
@@ -0,0 +1,524 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Find the radios plugged into USB: each one's serial ports, the
|
||||
# sound card that came with it, a CM108 PTT pin, and what model it
|
||||
# is, from its USB serial number or by asking it (radios.conf).
|
||||
# Source it; it doesn't do anything on its own.
|
||||
#
|
||||
# Shared by HamPack's `radio` and HamPackServer's `configure`, which keeps a
|
||||
# copy: change it in HamPack, and copy it (and radios.conf) to HamPackServer.
|
||||
# It needs nothing else of either project.
|
||||
#
|
||||
# Set before sourcing, or the defaults are used:
|
||||
# RADIOS_CONF the radios it knows (radios.conf beside this file)
|
||||
# RADIO_PROGRAM what to call the program in messages (radio)
|
||||
# RADIO_CONFIGURE the command that sets a radio up again (radio configure)
|
||||
# RADIO_CM108_ACCESS how a CM108 PTT pin you can't open yet gets opened
|
||||
# RADIO_SYSFS, RADIO_DEV, RADIO_PROC and RADIO_LOCKDIR point it at fake trees
|
||||
# for tests.
|
||||
|
||||
RADIOS_CONF="${RADIOS_CONF:-$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/radios.conf}"
|
||||
RADIO_PROGRAM="${RADIO_PROGRAM:-radio}"
|
||||
RADIO_CONFIGURE="${RADIO_CONFIGURE:-radio configure}"
|
||||
if [ -z "${RADIO_CM108_ACCESS:-}" ]; then
|
||||
# shellcheck disable=SC2016 # the backquotes are for the message
|
||||
RADIO_CM108_ACCESS='`update` installs HamPack'"'"'s udev rule for it, which lets whoever is at the desktop use it'
|
||||
fi
|
||||
# Where to look for devices
|
||||
SYSFS="${RADIO_SYSFS:-/sys}"
|
||||
DEVDIR="${RADIO_DEV:-/dev}"
|
||||
PROC="${RADIO_PROC:-/proc}"
|
||||
LOCKDIR="${RADIO_LOCKDIR:-/run/lock}"
|
||||
# CAT baud rates to try, most likely first
|
||||
BAUDS=(38400 115200 19200 9600 4800)
|
||||
|
||||
# Read an INI-style conf file and call CALLBACK once for each [section], with
|
||||
# its name in CONF[_name] and its keys in CONF[key]: HamPack's lib.sh has it,
|
||||
# and this is the same, for a project without lib.sh
|
||||
if ! declare -F parse_conf > /dev/null; then
|
||||
# shellcheck disable=SC2034 # CONF is read by the callbacks
|
||||
parse_conf() {
|
||||
local file="$1" callback="$2" line name have=0
|
||||
declare -gA CONF=()
|
||||
while IFS= read -r -u 3 line || [ -n "$line" ]; do
|
||||
[[ "$line" =~ ^[[:space:]]*(#|$) ]] && continue
|
||||
if [[ "$line" =~ ^\[(.+)\]$ ]]; then
|
||||
name="${BASH_REMATCH[1]}"
|
||||
(( have )) && "$callback"
|
||||
CONF=([_name]="$name")
|
||||
have=1
|
||||
continue
|
||||
fi
|
||||
(( have )) && CONF["${line%%=*}"]="${line#*=}"
|
||||
done 3< "$file"
|
||||
(( have )) && "$callback"
|
||||
return 0
|
||||
}
|
||||
fi
|
||||
|
||||
# Print a sysfs attribute, or nothing if the device hasn't got it
|
||||
attr() {
|
||||
[ -r "$1/$2" ] && printf '%s' "$(< "$1/$2")"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Print the USB device (the sysfs directory with idVendor) a device belongs to
|
||||
usb_device() {
|
||||
local d
|
||||
d="$(readlink -f "$1")" || return 1
|
||||
while [ "$d" != / ] && [ -n "$d" ]; do
|
||||
[ -f "$d/idVendor" ] && { echo "$d"; return 0; }
|
||||
d="$(dirname "$d")"
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Find the USB serial ports and USB sound cards: PORT_USB[ttyUSB0] and
|
||||
# CARD_USB[card1] are the USB devices they belong to
|
||||
find_devices() {
|
||||
declare -gA PORT_USB=() CARD_USB=() HIDRAW_USB=()
|
||||
local t c h dev
|
||||
for t in "$SYSFS"/class/tty/tty{USB,ACM}*; do
|
||||
[ -e "$t/device" ] && dev="$(usb_device "$t/device")" && PORT_USB["${t##*/}"]="$dev"
|
||||
done
|
||||
for c in "$SYSFS"/class/sound/card*; do
|
||||
[ -e "$c/device" ] && dev="$(usb_device "$c/device")" && CARD_USB["${c##*/}"]="$dev"
|
||||
done
|
||||
for h in "$SYSFS"/class/hidraw/hidraw*; do
|
||||
[ -e "$h/device" ] && dev="$(usb_device "$h/device")" && HIDRAW_USB["${h##*/}"]="$dev"
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# Whether a USB device is a CM108-family sound card, whose GPIO pin can key a
|
||||
# radio: the devices Dire Wolf 1.8 treats as CM108-compatible, as
|
||||
# udev/70-hampack-cm108.rules does. cm108_compatible USB_DEVICE
|
||||
cm108_compatible() {
|
||||
case "$(attr "$1" idVendor):$(attr "$1" idProduct)" in
|
||||
0d8c:000[89a-f] | 0d8c:0012 | 0d8c:0013 | 0d8c:0139 | 0d8c:013a | 0d8c:013c) return 0 ;;
|
||||
0c76:1605 | 0c76:1607 | 0c76:160b) return 0 ;;
|
||||
1209:7388) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Describe the CM108 PTT on a sound card's USB device, if it has one: the
|
||||
# hidraw device it's keyed through, and whether you can open it
|
||||
cm108_ptt() {
|
||||
local h
|
||||
cm108_compatible "$1" || return 1
|
||||
for h in $(printf '%s\n' "${!HIDRAW_USB[@]}" | sort -V); do
|
||||
[ "${HIDRAW_USB[$h]}" = "$1" ] || continue
|
||||
if [ -r "$DEVDIR/$h" ] && [ -w "$DEVDIR/$h" ]; then
|
||||
echo "/dev/$h"
|
||||
else
|
||||
echo "/dev/$h, which you can't open yet: $RADIO_CM108_ACCESS"
|
||||
fi
|
||||
return 0
|
||||
done
|
||||
echo "its GPIO pin, though its hidraw device is missing"
|
||||
}
|
||||
|
||||
# Print the stable name of the hidraw device on a CM108 sound card's USB
|
||||
# device: its link in /dev/input/by-id, unless another device has the same
|
||||
# id (two cards of a kind, with no serial number), then its link in
|
||||
# /dev/input/by-path, or else its /dev name. hidraw_path USB_DEVICE
|
||||
hidraw_path() {
|
||||
local h="" other link same=0
|
||||
for other in $(printf '%s\n' "${!HIDRAW_USB[@]}" | sort -V); do
|
||||
[ "${HIDRAW_USB[$other]}" = "$1" ] && { h="$other"; break; }
|
||||
done
|
||||
[ -n "$h" ] || return 1
|
||||
for other in "${!HIDRAW_USB[@]}"; do
|
||||
[ "$other" != "$h" ] || continue
|
||||
[ "${HIDRAW_USB[$other]}" != "$1" ] || continue
|
||||
[ "$(attr "${HIDRAW_USB[$other]}" idVendor):$(attr "${HIDRAW_USB[$other]}" idProduct):$(attr "${HIDRAW_USB[$other]}" serial)" = \
|
||||
"$(attr "$1" idVendor):$(attr "$1" idProduct):$(attr "$1" serial)" ] && same=1
|
||||
done
|
||||
for link in $( ((same)) || echo "$DEVDIR"/input/by-id/*-hidraw) "$DEVDIR"/input/by-path/*-hidraw; do
|
||||
[ -L "$link" ] && [ "$(basename "$(readlink -f "$link")")" = "$h" ] && { echo "/dev/${link#"$DEVDIR"/}"; return 0; }
|
||||
done
|
||||
echo "/dev/$h"
|
||||
}
|
||||
|
||||
# Whether a sound card can both play and record, as an interface to a radio
|
||||
# must: not a webcam's microphone, say
|
||||
plays_and_records() {
|
||||
local n="${1#card}" p c
|
||||
for p in "$SYSFS"/class/sound/pcmC"${n}"D*p; do
|
||||
[ -e "$p" ] || continue
|
||||
for c in "$SYSFS"/class/sound/pcmC"${n}"D*c; do
|
||||
[ -e "$c" ] && return 0
|
||||
done
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Print the USB sound cards that no serial port goes with, as interfaces to
|
||||
# a radio with no CAT control: "CARD USB_DEVICE", one to a line
|
||||
sound_only_cards() {
|
||||
local card radio paired=" "
|
||||
for radio in "${RADIOS[@]}"; do
|
||||
paired+="$(card_of "$radio") "
|
||||
done
|
||||
for card in $(printf '%s\n' "${!CARD_USB[@]}" | sort -V); do
|
||||
[[ "$paired" == *" $card "* ]] && continue
|
||||
plays_and_records "$card" && echo "$card ${CARD_USB[$card]}"
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
has_ports() {
|
||||
local p
|
||||
for p in "${!PORT_USB[@]}"; do [ "${PORT_USB[$p]}" = "$1" ] && return 0; done
|
||||
return 1
|
||||
}
|
||||
|
||||
has_card() {
|
||||
local c
|
||||
for c in "${!CARD_USB[@]}"; do [ "${CARD_USB[$c]}" = "$1" ] && return 0; done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Print the radio a USB serial device belongs to. A radio's serial chip and sound
|
||||
# card are either one USB device or two behind the radio's own hub, so: the
|
||||
# device itself if it has a sound card too, or its hub if that holds one sound
|
||||
# card and nothing but serial devices, or else just the device. Nothing tells
|
||||
# a radio's hub from a hub you've plugged a serial cable and a sound card into.
|
||||
radio_of() {
|
||||
local dev="$1" hub child cards=0
|
||||
hub="$(dirname "$dev")"
|
||||
if ! has_card "$dev" && [[ "${hub##*/}" != usb* ]] && [ -f "$hub/idVendor" ]; then
|
||||
for child in "$hub"/*/; do
|
||||
child="${child%/}"
|
||||
[ -f "$child/idVendor" ] || continue
|
||||
if has_card "$child"; then
|
||||
cards=$((cards + 1))
|
||||
elif ! has_ports "$child"; then
|
||||
cards=99 # something else is on this hub
|
||||
fi
|
||||
done
|
||||
(( cards == 1 )) && { echo "$hub"; return; }
|
||||
fi
|
||||
echo "$dev"
|
||||
}
|
||||
|
||||
# Print the radio's sound card, as card1
|
||||
card_of() {
|
||||
local c
|
||||
for c in $(printf '%s\n' "${!CARD_USB[@]}" | sort -V); do
|
||||
[ "${CARD_USB[$c]}" = "$1" ] || [ "$(dirname "${CARD_USB[$c]}")" = "$1" ] && { echo "$c"; return; }
|
||||
done
|
||||
}
|
||||
|
||||
# Print the port's stable name in /dev/serial/by-id, or its /dev name if it has none
|
||||
# Print the stable name of a serial port: its link in /dev/serial/by-id,
|
||||
# unless a twin of its USB device is plugged in, when udev gives both the
|
||||
# same by-id name and the link is to whichever came last; then its link in
|
||||
# /dev/serial/by-path, which follows the USB socket. Or else its /dev name
|
||||
port_path() {
|
||||
local link dir=by-id
|
||||
! has_twin "${PORT_USB[$1]:-}" || dir=by-path
|
||||
for link in "$DEVDIR"/serial/"$dir"/*; do
|
||||
[ -L "$link" ] && [ "$(basename "$(readlink -f "$link")")" = "$1" ] && { echo "/dev/serial/$dir/${link##*/}"; return; }
|
||||
done
|
||||
echo "/dev/$1"
|
||||
}
|
||||
|
||||
# Whether a USB device has a twin plugged in: another serial port, sound card
|
||||
# or hidraw device's USB device with its vendor, product and serial number
|
||||
# (none, often), which nothing then tells apart but the USB socket
|
||||
has_twin() {
|
||||
[ -n "$1" ] || return 1
|
||||
local other me
|
||||
me="$(attr "$1" idVendor):$(attr "$1" idProduct):$(attr "$1" serial)"
|
||||
for other in "${PORT_USB[@]}" "${CARD_USB[@]}" "${HIDRAW_USB[@]}"; do
|
||||
[ "$other" != "$1" ] || continue
|
||||
[ "$(attr "$other" idVendor):$(attr "$other" idProduct):$(attr "$other" serial)" = "$me" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Say how radio knows an interface that has a twin plugged in, if it has:
|
||||
# twin_note PORT_USB_DEVICE CARD_USB_DEVICE, either of them empty
|
||||
twin_note() {
|
||||
local dev
|
||||
for dev in "$1" "$2"; do
|
||||
has_twin "$dev" || continue
|
||||
echo "There's another $(attr "$dev" manufacturer) $(attr "$dev" product) plugged in, which nothing tells apart from this one but the USB socket each is in. Its serial port and PTT are known by socket, and its sound card by the order the two came up. Keep each in its socket, and if the radios get each other's audio, run \`$RADIO_CONFIGURE\` again"
|
||||
return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# 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
|
||||
[ ${#MATCHES[@]} -gt 0 ] && printf '%s\n' "${MATCHES[@]}"
|
||||
}
|
||||
match_radio() {
|
||||
local want="${CONF[$MATCH_FIELD]}"
|
||||
[ -n "$want" ] || return 0
|
||||
if [ "$MATCH_FIELD" = usb_serial ]; then
|
||||
# shellcheck disable=SC2053 # want is a pattern
|
||||
[[ "$MATCH_VALUE" == $want ]] || return 0
|
||||
else
|
||||
[ "${want,,}" = "${MATCH_VALUE,,}" ] || return 0
|
||||
fi
|
||||
MATCHES+=("${CONF[_name]}|${CONF[hamlib]}")
|
||||
}
|
||||
|
||||
# Print why a port can't be probed, or nothing if it can
|
||||
cant_probe() {
|
||||
local port="$1" dev fd pid group
|
||||
dev="$(readlink -f "$port")"
|
||||
if [ ! -e "$port" ]; then
|
||||
echo "$port doesn't exist"
|
||||
elif [ ! -r "$port" ] || [ ! -w "$port" ]; then
|
||||
group="$(stat -c %G "$dev")"
|
||||
if id -nG "$USER" | tr ' ' '\n' | grep -qx "$group"; then
|
||||
echo "you can't open $port yet: log out and back in, so you're in the $group group"
|
||||
else
|
||||
echo "you can't open $port: add yourself to the $group group with \`sudo usermod -aG $group $USER\`, then log out and back in"
|
||||
fi
|
||||
elif [ -e "$LOCKDIR/LCK..${dev##*/}" ]; then
|
||||
echo "$port is locked by another program ($LOCKDIR/LCK..${dev##*/})"
|
||||
else
|
||||
for fd in "$PROC"/[0-9]*/fd/*; do
|
||||
[ "$(readlink "$fd" 2> /dev/null)" = "$dev" ] || continue
|
||||
pid="${fd#"$PROC"/}"
|
||||
pid="${pid%%/*}"
|
||||
echo "$port is in use by $(cat "$PROC/$pid/comm" 2> /dev/null || echo "another program") (process $pid)"
|
||||
return
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# Ask the radio on PORT what it is, at each baud rate in turn: the Kenwood-style
|
||||
# ID; (Kenwood, Yaesu, Elecraft) and Icom's CI-V "read transceiver ID". Prints
|
||||
# "BAUD cat_id DIGITS" or "BAUD civ ADDRESS" for the first answer. DTR and RTS
|
||||
# are dropped as soon as the port opens, since many interfaces key the
|
||||
# transmitter with one of them; the port opening still raises them for a moment.
|
||||
ask_radio() {
|
||||
python3 -I -c '
|
||||
import fcntl, os, re, select, struct, sys, termios, time
|
||||
|
||||
def unkey():
|
||||
try:
|
||||
fcntl.ioctl(fd, termios.TIOCMBIC, struct.pack("I", termios.TIOCM_DTR | termios.TIOCM_RTS))
|
||||
except OSError:
|
||||
pass # not a real serial port
|
||||
|
||||
def ask(command, wait=0.4):
|
||||
termios.tcflush(fd, termios.TCIOFLUSH)
|
||||
os.write(fd, command)
|
||||
reply, end = b"", time.monotonic() + wait
|
||||
while (left := end - time.monotonic()) > 0:
|
||||
if not select.select([fd], [], [], left)[0]:
|
||||
break
|
||||
try:
|
||||
chunk = os.read(fd, 256)
|
||||
except BlockingIOError:
|
||||
continue
|
||||
if not chunk:
|
||||
break
|
||||
reply += chunk
|
||||
return reply
|
||||
|
||||
try:
|
||||
fd = os.open(sys.argv[1], os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
|
||||
except OSError as e:
|
||||
print("error", e.strerror)
|
||||
sys.exit(1)
|
||||
unkey()
|
||||
for baud in sys.argv[2:]:
|
||||
attrs = termios.tcgetattr(fd)
|
||||
speed = getattr(termios, "B" + baud)
|
||||
# Raw 8N1, no flow control
|
||||
attrs[0:6] = [0, 0, termios.CS8 | termios.CREAD | termios.CLOCAL, 0, speed, speed]
|
||||
attrs[6][termios.VMIN] = attrs[6][termios.VTIME] = 0
|
||||
termios.tcsetattr(fd, termios.TCSANOW, attrs)
|
||||
unkey()
|
||||
found = re.search(rb"ID(\d{3,4});", ask(b"ID;"))
|
||||
if found:
|
||||
# Elecraft radios all answer ID017;. Their option modules (OM;) tell
|
||||
# them apart: twelve characters, which radio reads
|
||||
options = ""
|
||||
if found.group(1) == b"017":
|
||||
om = re.search(rb"OM ([A-Za-z0-9-]{12});", ask(b"OM;"))
|
||||
options = om.group(1).decode() if om else ""
|
||||
print(baud, "cat_id", found.group(1).decode(), options)
|
||||
break
|
||||
# To any address (00) from a computer (E0); the radio answers to E0 from
|
||||
# its address, with its ID: one byte from an Icom, two from some Xiegus
|
||||
found = re.search(rb"\xfe\xfe\xe0(.)\x19\x00[^\xfd]{1,2}\xfd", ask(bytes.fromhex("fefe00e01900fd")), re.S)
|
||||
if found:
|
||||
print(baud, "civ", found.group(1).hex().upper())
|
||||
break
|
||||
' "$1" "${BAUDS[@]}"
|
||||
}
|
||||
|
||||
# Probe PORT and set ANSWER_BAUD, ANSWER_FIELD and ANSWER_VALUE from its
|
||||
# answer, or PROBE_ERROR if the port couldn't be opened
|
||||
probe_port() {
|
||||
local reply
|
||||
ANSWER_BAUD="" ANSWER_FIELD="" ANSWER_VALUE="" ANSWER_OPTIONS="" PROBE_ERROR=""
|
||||
if ! reply="$(ask_radio "$1")"; then
|
||||
PROBE_ERROR="${reply#error }"
|
||||
PROBE_ERROR="${PROBE_ERROR:-python3 failed}"
|
||||
return 1
|
||||
fi
|
||||
read -r ANSWER_BAUD ANSWER_FIELD ANSWER_VALUE ANSWER_OPTIONS <<< "$reply"
|
||||
[ -n "$ANSWER_VALUE" ]
|
||||
}
|
||||
|
||||
# Which Elecraft radio's option modules these are (its reply to OM;, after
|
||||
# "OM "), as Hamlib 4.7's elecraft_open() reads them: a KX2 or KX3 has 0 and
|
||||
# its model number (1 or 2) last; a K4 has S and 4 among them; a K3S has R;
|
||||
# anything else is a K3. elecraft_model OPTIONS
|
||||
elecraft_model() {
|
||||
if [ "${1:10:1}" = 0 ]; then
|
||||
case "${1:11:1}" in
|
||||
1) echo KX2 ;;
|
||||
2) echo KX3 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
elif [[ "$1" == *S* && "$1" == *4* ]]; then
|
||||
echo K4
|
||||
elif [[ "$1" == *R* ]]; then
|
||||
echo K3S
|
||||
else
|
||||
echo K3
|
||||
fi
|
||||
}
|
||||
|
||||
# The radios that could have given the probe's answer, as matching prints
|
||||
# them: those with its ID, narrowed by an Elecraft radio's option modules
|
||||
answer_models() {
|
||||
local models model kind narrowed=""
|
||||
models="$(matching "$ANSWER_FIELD" "$ANSWER_VALUE")" || return 1
|
||||
if [ -n "${ANSWER_OPTIONS:-}" ] && kind="$(elecraft_model "$ANSWER_OPTIONS")"; then
|
||||
while IFS= read -r model; do
|
||||
[ "$(conf_field "${model%|*}" elecraft)" = "$kind" ] && narrowed+="$model"$'\n'
|
||||
done <<< "$models"
|
||||
[ -z "$narrowed" ] || models="${narrowed%$'\n'}"
|
||||
fi
|
||||
echo "$models"
|
||||
}
|
||||
|
||||
# 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
|
||||
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_PROGRAM doesn't know"
|
||||
else
|
||||
echo "a radio that answered ID$ANSWER_VALUE; that $RADIO_PROGRAM doesn't know"
|
||||
fi
|
||||
}
|
||||
|
||||
# Describe the radio that answered
|
||||
describe_answer() {
|
||||
local models
|
||||
models="$(answer_models)"
|
||||
if [ -n "$models" ]; then models_text "$models"; else unknown_answer_text; fi
|
||||
}
|
||||
|
||||
# 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
|
||||
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 ]
|
||||
}
|
||||
|
||||
# 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
|
||||
# shellcheck disable=SC2034 # ID_* are for the scripts that source this
|
||||
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
|
||||
|
||||
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"
|
||||
|
||||
[ "$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="$(answer_models)"
|
||||
[ -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
|
||||
}
|
||||
|
||||
# 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)"
|
||||
}
|
||||
|
||||
# Print a field of a radio in radios.conf: conf_field NAME FIELD
|
||||
conf_field() {
|
||||
awk -v name="$1" -v field="$2" '
|
||||
/^\[/ { in_radio = ($0 == "[" name "]"); next }
|
||||
in_radio && index($0, field "=") == 1 { print substr($0, length(field) + 2); exit }
|
||||
' "$RADIOS_CONF"
|
||||
}
|
||||
|
||||
# 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"
|
||||
}
|
||||
Reference in New Issue
Block a user