diff --git a/deploy/yaris-netwatch.service b/deploy/yaris-netwatch.service index 65cb664..eb96acf 100644 --- a/deploy/yaris-netwatch.service +++ b/deploy/yaris-netwatch.service @@ -5,4 +5,7 @@ Wants=NetworkManager.service [Service] Type=oneshot +# Retry loop (down AP, rescan, retry connect up to 2x) can take ~80s worst +# case - default 90s systemd start timeout was too tight a margin. +TimeoutStartSec=180 ExecStart=/usr/local/bin/yaris-netwatch.sh diff --git a/deploy/yaris-netwatch.sh b/deploy/yaris-netwatch.sh index 9eb3ca8..a068d99 100755 --- a/deploy/yaris-netwatch.sh +++ b/deploy/yaris-netwatch.sh @@ -39,9 +39,32 @@ fi logger -t "$LOG_TAG" "wlan0 is on hotspot, checking for home WiFi ($HOME_CON)..." -if nmcli --wait 20 con up "$HOME_CON" >"$LAST_LOG" 2>&1; then +# Real-hardware finding: a plain "nmcli con up home-wifi" straight out of AP +# mode reliably reports "not found" even when actually in range, repeatedly +# (confirmed: 4 failures in a row over 13min while sitting right next to the +# router). NM's scan cache for wlan0 is stale/empty coming out of AP mode +# (the radio literally can't scan while acting as an AP - see comment above), +# and a single con-up attempt doesn't wait for a fresh scan to land before +# giving up. Fix: explicitly drop the AP connection first to free the radio, +# force a fresh scan and give it a few seconds to populate, then retry the +# connect attempt with a longer timeout up to 2 times before conceding and +# restoring the hotspot. +nmcli con down "$AP_CON" >"$LAST_LOG" 2>&1 + +connected=false +for attempt in 1 2; do + nmcli device wifi rescan ifname "$IFACE" >>"$LAST_LOG" 2>&1 + sleep 5 + if nmcli --wait 30 con up "$HOME_CON" >>"$LAST_LOG" 2>&1; then + connected=true + break + fi + logger -t "$LOG_TAG" "home WiFi attempt $attempt failed, retrying..." +done + +if [ "$connected" = true ]; then logger -t "$LOG_TAG" "home WiFi found, switched to '$HOME_CON'" else - logger -t "$LOG_TAG" "home WiFi not found, restoring hotspot" + logger -t "$LOG_TAG" "home WiFi not found after retries, restoring hotspot" nmcli con up "$AP_CON" >>"$LAST_LOG" 2>&1 fi