Fix GPS modem selection and add a GPS-only time source
This commit is contained in:
1 parent
eaf08abcd4
commit
a50c01b416
18 files changed
+682
-16
No files matched your search
@@ -1,24 +1,36 @@
|
||||
#!/bin/sh
|
||||
# Bananarchy GPS reception & coordinate detection CLI utility
|
||||
# Queries Qualcomm QDSP6 / ModemManager GNSS (gps-nmea + gps-raw + 3gpp-lac-ci).
|
||||
# The modem path is resolved by bananarchy-gps-modem.sh: "mmcli -m any" can
|
||||
# pick the wrong modem (or fail) on the dual-SIM 2720 Flip.
|
||||
set -u
|
||||
|
||||
ACTION="${1:-status}"
|
||||
MMCLI="${BANANARCHY_MMCLI:-/usr/bin/mmcli}"
|
||||
GPS_MODEM="${BANANARCHY_GPS_MODEM:-/usr/bin/bananarchy-gps-modem.sh}"
|
||||
|
||||
modem_path() {
|
||||
m="$("$GPS_MODEM" path 2>/dev/null | head -n1)"
|
||||
case "$m" in ''|*[!0-9a-zA-Z/_-]*) m=any ;; esac
|
||||
printf '%s\n' "$m"
|
||||
}
|
||||
|
||||
enable_gps() {
|
||||
mmcli -m any --location-enable-gps-nmea --location-enable-gps-raw >/dev/null 2>&1 || true
|
||||
mmcli -m any --location-set-gps-refresh-rate=2 >/dev/null 2>&1 || true
|
||||
"$GPS_MODEM" enable >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
disable_gps() {
|
||||
mmcli -m any --location-disable-gps-nmea --location-disable-gps-raw >/dev/null 2>&1 || true
|
||||
"$GPS_MODEM" disable >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
print_status() {
|
||||
enable_gps
|
||||
RAW="$(mmcli -m any -K --location-get 2>/dev/null)"
|
||||
m="$(modem_path)"
|
||||
RAW="$("$MMCLI" -m "$m" -K --location-get 2>/dev/null)"
|
||||
if [ -z "$RAW" ]; then
|
||||
echo "GPS Status: Modem not ready"
|
||||
exit 1
|
||||
echo "GPS Status: Modem not ready (modem=$m)"
|
||||
"$GPS_MODEM" status 2>/dev/null || true
|
||||
return 1
|
||||
fi
|
||||
MCC="$(printf '%s\n' "$RAW" | awk -F': ' '/location\.3gpp\.mcc/{print $2}')"
|
||||
MNC="$(printf '%s\n' "$RAW" | awk -F': ' '/location\.3gpp\.mnc/{print $2}')"
|
||||
@@ -29,6 +41,7 @@ print_status() {
|
||||
ALT="$(printf '%s\n' "$RAW" | awk -F': ' '/location\.gps\.altitude/{print $2}')"
|
||||
UTC="$(printf '%s\n' "$RAW" | awk -F': ' '/location\.gps\.utc/{print $2}')"
|
||||
echo "=== Bananarchy GNSS / GPS Reception Status ==="
|
||||
echo "Modem : $m"
|
||||
echo "3GPP Cell : MCC=${MCC:--} MNC=${MNC:--} TAC=${TAC:--} CID=${CID:--}"
|
||||
echo "GPS Fix : Lat=${LAT:---} Lon=${LON:---} Alt=${ALT:---}m UTC=${UTC:---}"
|
||||
echo "--- NMEA Sentences ---"
|
||||
@@ -38,7 +51,7 @@ print_status() {
|
||||
case "$ACTION" in
|
||||
enable)
|
||||
enable_gps
|
||||
echo "GPS enabled (2s refresh)"
|
||||
echo "GPS enabled on $(modem_path) (2s refresh)"
|
||||
;;
|
||||
disable)
|
||||
disable_gps
|
||||
@@ -46,7 +59,8 @@ case "$ACTION" in
|
||||
;;
|
||||
nmea)
|
||||
enable_gps
|
||||
mmcli -m any -K --location-get 2>/dev/null | awk -F': ' '/location\.gps\.nmea\.value/{print $2}'
|
||||
"$MMCLI" -m "$(modem_path)" -K --location-get 2>/dev/null |
|
||||
awk -F': ' '/location\.gps\.nmea\.value/{print $2}'
|
||||
;;
|
||||
watch)
|
||||
enable_gps
|
||||
@@ -59,7 +73,11 @@ case "$ACTION" in
|
||||
gui|open)
|
||||
exec xgps
|
||||
;;
|
||||
modem|status-detail)
|
||||
"$GPS_MODEM" status
|
||||
;;
|
||||
status|test|*)
|
||||
print_status
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
#!/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
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
#!/bin/sh
|
||||
# GPS-only system datetime synchronisation for Bananarchy.
|
||||
#
|
||||
# "Solely via GPS" means no NTP pool, no DNS and no chronyd step: when the GPS
|
||||
# time source is selected, chronyd is stopped and bananarchy-time-sync.sh
|
||||
# declines every network-up burst. The clock is set from a valid GNSS RMC
|
||||
# sentence (or ModemManager's GPS UTC field) with date -u -s, then written to
|
||||
# the RTC and republished on the status bar.
|
||||
set -u
|
||||
|
||||
ACTION="${1:-status}"
|
||||
SOURCE_FILE="${BANANARCHY_TIME_SOURCE_FILE:-/home/user/.config/sxmo/time_source}"
|
||||
MMCLI="${BANANARCHY_MMCLI:-/usr/bin/mmcli}"
|
||||
GPS_MODEM="${BANANARCHY_GPS_MODEM:-/usr/bin/bananarchy-gps-modem.sh}"
|
||||
DATE_CMD="${BANANARCHY_DATE_CMD:-date}"
|
||||
RC_SERVICE="${BANANARCHY_RC_SERVICE:-rc-service}"
|
||||
REFRESH_CMD="${BANANARCHY_STATUS_REFRESH:-su}"
|
||||
NET_SYNC="${BANANARCHY_NET_SYNC:-/usr/bin/bananarchy-time-sync.sh}"
|
||||
TIMEOUT="${BANANARCHY_GPS_TIME_TIMEOUT:-90}"
|
||||
RUNDIR="${BANANARCHY_GPS_TIME_RUNDIR:-/run/bananarchy-gps-time}"
|
||||
NO_ESCALATION="${BANANARCHY_GPS_TIME_NO_ESCALATION:-0}"
|
||||
|
||||
log() {
|
||||
printf 'bananarchy-gps-time-sync: %s\n' "$*" >&2
|
||||
}
|
||||
|
||||
get_source() {
|
||||
v="network"
|
||||
if [ -r "$SOURCE_FILE" ]; then
|
||||
read -r v < "$SOURCE_FILE" 2>/dev/null || v="network"
|
||||
fi
|
||||
case "$v" in gps) printf 'gps\n' ;; *) printf 'network\n' ;; esac
|
||||
}
|
||||
|
||||
refresh_statusbar() {
|
||||
"$REFRESH_CMD" user -s /bin/sh -c '
|
||||
HOME=/home/user DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/10000 \
|
||||
SXMO_WM=dwm SXMO_STATUS_NAME=default SXMO_STATE=/run/user/10000/sxmo.state \
|
||||
PATH=/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin \
|
||||
sxmo_hook_statusbar.sh refresh
|
||||
' >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
# Stop chronyd while GPS is the only time source; start it again for NTP.
|
||||
apply_source() {
|
||||
if [ "$(get_source)" = "gps" ]; then
|
||||
"$RC_SERVICE" chronyd stop >/dev/null 2>&1 || true
|
||||
log "GPS-only time source: chronyd stopped, NTP disabled"
|
||||
else
|
||||
"$RC_SERVICE" chronyd start >/dev/null 2>&1 || true
|
||||
"$NET_SYNC" force >/dev/null 2>&1 || true
|
||||
log "Network time source: chronyd started"
|
||||
fi
|
||||
}
|
||||
|
||||
apply_as_root() {
|
||||
if [ "$(id -u)" = "0" ] || [ "$NO_ESCALATION" = "1" ]; then
|
||||
apply_source
|
||||
else
|
||||
doas -n "$0" apply >/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
set_source() {
|
||||
v="${1:-}"
|
||||
case "$v" in
|
||||
network|gps) ;;
|
||||
*) printf 'Usage: %s source <network|gps>\n' "${0##*/}" >&2; exit 2 ;;
|
||||
esac
|
||||
dir="${SOURCE_FILE%/*}"
|
||||
mkdir -p "$dir" 2>/dev/null || exit 1
|
||||
printf '%s\n' "$v" > "$SOURCE_FILE" || exit 1
|
||||
apply_as_root
|
||||
printf 'Time source: %s\n' "$v"
|
||||
}
|
||||
|
||||
utc_to_stamp() {
|
||||
v="$1"
|
||||
case "$v" in *Z) v="${v%Z}" ;; esac
|
||||
v="$(printf '%s' "$v" | tr 'T' ' ' | cut -c1-19)"
|
||||
case "$v" in
|
||||
????-??-??' '??:??:??) printf '%s\n' "$v" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
rmc_to_stamp() {
|
||||
printf '%s\n' "$1" | awk -F',' '
|
||||
/^\$(GP|GN|GL|GA)RMC/ && $3 == "A" {
|
||||
split($2, t, ".");
|
||||
d = $10;
|
||||
if (length(t[1]) < 6 || length(d) < 6) next;
|
||||
printf "20%s-%s-%s %s:%s:%s\n",
|
||||
substr(d, 5, 2), substr(d, 3, 2), substr(d, 1, 2),
|
||||
substr(t[1], 1, 2), substr(t[1], 3, 2), substr(t[1], 5, 2);
|
||||
exit
|
||||
}'
|
||||
}
|
||||
|
||||
location_sample() {
|
||||
m="$("$GPS_MODEM" path 2>/dev/null | head -n1)"
|
||||
case "$m" in ''|*[!0-9a-zA-Z/_-]*) m=any ;; esac
|
||||
"$MMCLI" -m "$m" -K --location-get 2>/dev/null
|
||||
}
|
||||
|
||||
stamp_from_sample() {
|
||||
raw="$1"
|
||||
iso="$(printf '%s\n' "$raw" | awk -F': ' '
|
||||
/modem\.location\.gps\.utc/ {
|
||||
v = $2;
|
||||
gsub(/^[ \047"]+|[ \047"]+$/, "", v);
|
||||
if (v ~ /^[0-9][0-9][0-9][0-9]-/) { print v; exit }
|
||||
}')"
|
||||
if [ -n "$iso" ]; then
|
||||
utc_to_stamp "$iso" && return 0
|
||||
fi
|
||||
# NMEA sentences are emitted one per line by `mmcli -K`. A here-document
|
||||
# keeps the loop in this function instead of a subshell, so the first valid
|
||||
# RMC fix can be returned directly.
|
||||
nmea="$(printf '%s\n' "$raw" | awk -F': ' '/location\.gps\.nmea\.value/{print $2}')"
|
||||
while IFS= read -r sentence; do
|
||||
stamp="$(rmc_to_stamp "$sentence")"
|
||||
if [ -n "$stamp" ]; then
|
||||
printf '%s\n' "$stamp"
|
||||
return 0
|
||||
fi
|
||||
done <<EOF
|
||||
$nmea
|
||||
EOF
|
||||
return 1
|
||||
}
|
||||
|
||||
sync_now() {
|
||||
"$GPS_MODEM" enable >/dev/null 2>&1 || true
|
||||
end=$(( $("$DATE_CMD" +%s 2>/dev/null || printf '0') + TIMEOUT ))
|
||||
while [ "$("$DATE_CMD" +%s 2>/dev/null || printf '0')" -lt "$end" ]; do
|
||||
raw="$(location_sample)"
|
||||
stamp="$(stamp_from_sample "$raw")"
|
||||
if [ -n "$stamp" ]; then
|
||||
if "$DATE_CMD" -u -s "$stamp" >/dev/null 2>&1; then
|
||||
hwclock -w -u >/dev/null 2>&1 || true
|
||||
mkdir -p "$RUNDIR" 2>/dev/null || true
|
||||
printf '%s\n' "$stamp" > "$RUNDIR/last" 2>/dev/null || true
|
||||
refresh_statusbar
|
||||
printf 'GPS time synced: %s UTC\n' "$stamp"
|
||||
return 0
|
||||
fi
|
||||
log "date -u -s failed (root privileges required)"
|
||||
return 1
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
echo "No GPS time fix yet; retry outdoors or check ModemManager location status"
|
||||
return 1
|
||||
}
|
||||
|
||||
# The user-facing source/get/status paths need no privileges; the clock step,
|
||||
# boot sync and chronyd control do.
|
||||
case "$ACTION" in
|
||||
source|get|status) ;;
|
||||
*)
|
||||
if [ "$(id -u)" != "0" ] && [ "$NO_ESCALATION" != "1" ]; then
|
||||
exec doas -n "$0" "$@"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$ACTION" in
|
||||
get) get_source ;;
|
||||
source) set_source "${2:-}" ;;
|
||||
status)
|
||||
printf 'time_source=%s\n' "$(get_source)"
|
||||
printf 'last_gps_sync=%s\n' "$(cat "$RUNDIR/last" 2>/dev/null || printf 'never')"
|
||||
"$GPS_MODEM" status 2>/dev/null || true
|
||||
;;
|
||||
apply) apply_source ;;
|
||||
sync) sync_now; exit $? ;;
|
||||
boot)
|
||||
if [ "$(get_source)" != "gps" ]; then
|
||||
exit 0
|
||||
fi
|
||||
( "$0" sync >/dev/null 2>&1 ) &
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
printf 'Usage: %s [status|get|source <network|gps>|sync|boot|apply]\n' \
|
||||
"${0##*/}" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
# Config -> Sync GPS time: set the system clock from GNSS only and report it.
|
||||
# Runs as the X session user; the GPS helper escalates the clock step itself.
|
||||
set -u
|
||||
|
||||
HELPER="${BANANARCHY_GPS_TIME_HELPER:-/usr/bin/bananarchy-gps-time-sync.sh}"
|
||||
result="$("$HELPER" sync 2>&1)"
|
||||
[ -n "$result" ] || result="No response from the GPS time helper"
|
||||
|
||||
case "$result" in
|
||||
"GPS time synced"*) urgency=normal ;;
|
||||
*) urgency=critical ;;
|
||||
esac
|
||||
|
||||
if command -v notify-send >/dev/null 2>&1; then
|
||||
notify-send --urgency="$urgency" --expire-time=8000 \
|
||||
"Sync GPS time" "$result" >/dev/null 2>&1 || true
|
||||
else
|
||||
sxmo_notify_user.sh "Sync GPS time: $result" >/dev/null 2>&1 || true
|
||||
fi
|
||||
exit 0
|
||||
@@ -17,6 +17,11 @@ CHRONYC=/usr/bin/chronyc
|
||||
RUNDIR=/run/bananarchy-time-sync
|
||||
STAMP="$RUNDIR/last"
|
||||
LOCK="$RUNDIR/lock"
|
||||
# GPS-only mode: the user can select GNSS as the sole time source. In that
|
||||
# mode chronyd is stopped by bananarchy-gps-time-sync.sh and no NTP burst,
|
||||
# source refresh or clock step may be issued from this helper.
|
||||
TIME_SOURCE_FILE="${BANANARCHY_TIME_SOURCE_FILE:-/home/user/.config/sxmo/time_source}"
|
||||
GPS_TIME_HELPER="${BANANARCHY_GPS_TIME_HELPER:-/usr/bin/bananarchy-gps-time-sync.sh}"
|
||||
# /proc/uptime is monotonic, so a multi-decade clock step cannot invalidate
|
||||
# the stamp. Unsynchronised attempts are spaced 15 s apart; a synchronised
|
||||
# clock only needs a source refresh every 5 min after a network change.
|
||||
@@ -30,7 +35,8 @@ ATTEMPTS=8
|
||||
BOOT_ATTEMPTS=30
|
||||
SYNC_WAIT=15
|
||||
|
||||
if [ "$(id -u)" != "0" ]; then
|
||||
# Host tests (and callers that already have privileges) can skip the doas hop.
|
||||
if [ "$(id -u)" != "0" ] && [ "${BANANARCHY_TIME_SYNC_NO_ESCALATION:-0}" != "1" ]; then
|
||||
exec doas -n "$0" "$@"
|
||||
fi
|
||||
|
||||
@@ -38,6 +44,14 @@ log() {
|
||||
printf 'bananarchy-time-sync: %s\n' "$*" >&2
|
||||
}
|
||||
|
||||
time_source() {
|
||||
v="network"
|
||||
if [ -r "$TIME_SOURCE_FILE" ]; then
|
||||
read -r v < "$TIME_SOURCE_FILE" 2>/dev/null || v="network"
|
||||
fi
|
||||
case "$v" in gps) printf 'gps\n' ;; *) printf 'network\n' ;; esac
|
||||
}
|
||||
|
||||
monotonic() {
|
||||
awk '{ printf "%d\n", $1 }' /proc/uptime
|
||||
}
|
||||
@@ -106,6 +120,10 @@ wait_sources() {
|
||||
|
||||
trigger() {
|
||||
mode="${1:-network-up}"
|
||||
if [ "$(time_source)" = "gps" ]; then
|
||||
log "GPS is the sole time source; skipping NTP"
|
||||
return 1
|
||||
fi
|
||||
attempts="$ATTEMPTS"
|
||||
[ "$mode" = "boot" ] && attempts="$BOOT_ATTEMPTS"
|
||||
wait_chronyd || {
|
||||
@@ -142,6 +160,12 @@ trigger() {
|
||||
|
||||
# Config -> Sync net time: force a burst and report when the clock is set.
|
||||
sync_now() {
|
||||
if [ "$(time_source)" = "gps" ]; then
|
||||
# Config -> Sync net time must still give the user a result without
|
||||
# touching NTP when GNSS is the only configured source.
|
||||
"$GPS_TIME_HELPER" sync
|
||||
return $?
|
||||
fi
|
||||
if ! trigger sync; then
|
||||
echo "Network time unavailable: chronyd or DNS is not ready"
|
||||
return 1
|
||||
@@ -162,9 +186,17 @@ sync_now() {
|
||||
|
||||
case "${1:-status}" in
|
||||
boot|network-up)
|
||||
if [ "$(time_source)" = "gps" ]; then
|
||||
[ "${1:-}" = "boot" ] && "$GPS_TIME_HELPER" boot >/dev/null 2>&1 || true
|
||||
exit 0
|
||||
fi
|
||||
trigger "$1" && refresh_statusbar
|
||||
;;
|
||||
force)
|
||||
if [ "$(time_source)" = "gps" ]; then
|
||||
"$GPS_TIME_HELPER" sync && refresh_statusbar
|
||||
exit $?
|
||||
fi
|
||||
trigger force && refresh_statusbar
|
||||
;;
|
||||
sync)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user