Files
bananarchy/overlay/rootfs/usr/bin/bananarchy-gps-modem.sh
T

70 lines
2.0 KiB
Bash
Raw Normal View History

#!/bin/sh
# Resolve the ModemManager modem that owns the GNSS/Location interface.
#
# On the 2720 Flip (and any dual-SIM QMI target) "mmcli -m any" can resolve to
# the wrong modem or fail outright with "too many modems matched", which made
# every GPS enable/query a no-op. Prefer the modem that advertises gps-nmea;
# print "any" only when ModemManager reports no usable modem at all.
set -u
MMCLI="${BANANARCHY_MMCLI:-/usr/bin/mmcli}"
list_modems() {
"$MMCLI" -L 2>/dev/null |
awk '{ for (i = 1; i <= NF; i++)
if ($i ~ /^\/org\/freedesktop\/ModemManager1\/Modem\/[0-9]+$/)
print $i }'
}
resolve_modem() {
first=""
for m in $(list_modems); do
[ -n "$first" ] || first="$m"
if "$MMCLI" -m "$m" --location-status 2>/dev/null | grep -q 'gps-nmea'; then
printf '%s\n' "$m"
return 0
fi
done
if [ -n "$first" ]; then
printf '%s\n' "$first"
else
printf 'any\n'
fi
}
enable_gps() {
m="$(resolve_modem)"
# A disabled modem cannot start a QMI LOC session. These calls are
# idempotent and each failure is non-fatal so the CLI stays usable while
# ModemManager is still probing the modem.
"$MMCLI" -m "$m" --enable >/dev/null 2>&1 || true
"$MMCLI" -m "$m" --location-enable-3gpp >/dev/null 2>&1 || true
"$MMCLI" -m "$m" --location-enable-gps-nmea \
--location-enable-gps-raw >/dev/null 2>&1 || true
"$MMCLI" -m "$m" --location-set-gps-refresh-rate=2 >/dev/null 2>&1 || true
printf '%s\n' "$m"
}
disable_gps() {
m="$(resolve_modem)"
"$MMCLI" -m "$m" --location-disable-gps-nmea \
--location-disable-gps-raw >/dev/null 2>&1 || true
printf '%s\n' "$m"
}
case "${1:-path}" in
path) resolve_modem ;;
list) list_modems ;;
enable) enable_gps ;;
disable) disable_gps ;;
status)
m="$(resolve_modem)"
printf 'modem=%s\n' "$m"
"$MMCLI" -m "$m" --location-status 2>/dev/null || true
;;
*)
printf 'Usage: %s [path|list|enable|disable|status]\n' "${0##*/}" >&2
exit 2
;;
esac
exit 0