#!/bin/sh # Mazda 55.00.753: connect, save, auto-reconnect, and keep Wi-Fi power save off. set +x SSID='YOUR_HOTSPOT_NAME' PASSWORD='YOUR_HOTSPOT_PASSWORD' # 8-63 bytes; for ' use '\'' inside the value. IFACE=wlan0 POWER_SAVE_POLL=2 # Seconds between silent checks, including after wake. DIR=$(CDPATH= cd -P "$(dirname "$0")" && pwd) || exit 1 IW_BIN=$(command -v iw 2>/dev/null) # Run independently of connection setup, without retaining a USB script or log. # This helper remains alive after the finite connection worker below exits. if [ -n "$IW_BIN" ]; then nohup /bin/sh -c ' trap "" HUP export LC_ALL=C iface=$1; iw_bin=$2; poll=$3 cd / || exit 1 umask 077 lock="/tmp/mazda-wifi-power-save-$iface" if ! mkdir "$lock" 2>/dev/null; then # A missing PID may mean another copy is still starting. read oldpid < "$lock/pid" || exit 0 case "$oldpid" in ""|*[!0-9]*) exit 0 ;; esac if kill -0 "$oldpid" 2>/dev/null && tr "\000" " " < "/proc/$oldpid/cmdline" 2>/dev/null | grep -q "mazda-wifi-power-save"; then exit 0 fi rm -f "$lock/pid" rmdir "$lock" 2>/dev/null || exit 0 mkdir "$lock" 2>/dev/null || exit 0 fi echo "$$" > "$lock/pid" trap "rm -f \"$lock/pid\"; rmdir \"$lock\"" 0 trap "exit 0" INT TERM while :; do if [ -d "/sys/class/net/$iface" ]; then case "$("$iw_bin" dev "$iface" get power_save 2>/dev/null)" in *"Power save: off"*) ;; *) "$iw_bin" dev "$iface" set power_save off ;; esac fi sleep "$poll" done ' mazda-wifi-power-save "$IFACE" "$IW_BIN" "$POWER_SAVE_POLL" \ /dev/null 2>&1 & fi ( trap '' HUP RESTORE_AUTOCONNECT=0 finish() { if [ "$RESTORE_AUTOCONNECT" = 1 ]; then if dbus SetWiFiAutoconnect_Async boolean:true; then echo "AUTOCONNECT: enable request accepted." else echo "WARNING: automatic reconnect could not be re-enabled." fi fi sync } trap 'finish' 0 export LC_ALL=C now() { cut -d. -f1 /proc/uptime; } fail() { echo "FAILED: $*"; exit 1; } wpa() { /usr/bin/timeout -t 5 -s 9 /usr/sbin/wpa_cli \ -p /var/run/wpa_supplicant-wlan0 -i "$IFACE" "$@" } ok() { [ "$(wpa "$@" 2>&1)" = OK ] || fail "wpa_cli $1 failed."; } dbus() { method=$1; shift reply=$(DBUS_SESSION_BUS_ADDRESS=unix:path=/tmp/dbus_hmi_socket \ /usr/bin/dbus-send --session --print-reply --reply-timeout=10000 \ --dest=com.jci.blm_nms /com/jci/blm_nms \ "com.jci.blm_nms.$method" "$@" 2>&1) printf '%s\n' "$method: $reply" [ "$(printf '%s\n' "$reply" | awk '$1=="int32" {print $2; exit}')" = 0 ] } [ "$SSID" != YOUR_HOTSPOT_NAME ] && [ "$PASSWORD" != YOUR_HOTSPOT_PASSWORD ] || fail "Edit SSID and PASSWORD first." [ "${#SSID}" -ge 1 ] && [ "${#SSID}" -le 32 ] || fail "SSID must be 1-32 bytes." [ "${#PASSWORD}" -ge 8 ] && [ "${#PASSWORD}" -le 63 ] || fail "Password must be 8-63 bytes." if [ -n "$IW_BIN" ]; then echo "POWER_SAVE: silent background guard requested; interval=${POWER_SAVE_POLL}s." else echo "WARNING: iw is unavailable; cannot disable Wi-Fi power saving." fi echo "Waiting for $IFACE; start uptime=$(now)" deadline=$(( $(now) + 300 )) ready=0 while [ "$(now)" -lt "$deadline" ]; do if [ -d "/sys/class/net/$IFACE" ] && dbus WIFI_SetMode_Async int32:100 int32:1; then ready=1; break fi sleep 2 done [ "$ready" = 1 ] || fail "Interface/network service did not become ready." until [ "$(wpa ping)" = PONG ]; do [ "$(now)" -lt "$deadline" ] || fail "Supplicant did not become ready." sleep 2 done RESTORE_AUTOCONNECT=1 dbus SetWiFiAutoconnect_Async boolean:false || fail "Cannot pause automatic reconnect." ok remove_network all # Rebuild runtime profiles; save after success. NETWORK_ID=$(wpa add_network 2>&1) case "$NETWORK_ID" in ''|*[!0-9]*) fail "Cannot create new profile." ;; esac ok set_network "$NETWORK_ID" ssid "\"$SSID\"" ok set_network "$NETWORK_ID" psk "\"$PASSWORD\"" ok set_network "$NETWORK_ID" key_mgmt WPA-PSK ok set_network "$NETWORK_ID" proto RSN ok select_network "$NETWORK_ID" dbus Connect int32:100 || fail "Connection request rejected." wpa list_networks deadline=$(( $(now) + 90 )) while [ "$(now)" -lt "$deadline" ]; do status=$(wpa status) printf '\nUptime=%s\n%s\n' "$(now)" "$status" if printf '%s\n' "$status" | grep -q '^wpa_state=COMPLETED$' && ifconfig "$IFACE" | grep 'inet addr:[1-9]' | grep -qvE 'inet addr:(127\.|169\.254\.)'; then ifconfig "$IFACE" echo "CONNECTED: authentication completed and IPv4 address present." if [ -n "$IW_BIN" ]; then "$IW_BIN" dev "$IFACE" set power_save off "$IW_BIN" dev "$IFACE" get power_save fi if dbus WIFI_SaveConfiguredNetworks_Async int32:100; then echo "NATIVE_SAVE: Mazda reported success; verify retention after sleep/wake." else echo "WARNING: connected, but Mazda's save request failed." fi exit 0 fi sleep 3 done wpa list_networks ifconfig "$IFACE" fail "No completed connection with IPv4 address within 90 seconds." ) "$DIR/wifi-connect.log" 2>&1 & exit 0