#!/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 \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 </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 |sync|boot|apply]\n' \ "${0##*/}" >&2 exit 2 ;; esac exit 0