Files
StarPilot/openpilot/starpilot/system/starport/network.py
T

766 lines
33 KiB
Python

"""NetworkManager lease for the head unit's projection Wi-Fi.
The lease creates one volatile, never-default connection profile named
``starpilot-starport``, activates it on the Wi-Fi device, and on release
deactivates and deletes only that profile and reactivates whatever Wi-Fi
connection was active before -- unless the user switched networks meanwhile.
Credentials travel over D-Bus only; they never appear in argv or logs. The
cellular default route and DNS are left alone (``never-default``,
``ignore-auto-dns``) so the car's local network cannot take over Internet traffic.
"""
from __future__ import annotations
import ipaddress
import json
import threading
import time
import uuid
from collections.abc import Callable
from dataclasses import replace
from pathlib import Path
from jeepney import DBusAddress, new_method_call
from jeepney.io.threading import DBusRouter, open_dbus_connection
from jeepney.low_level import MessageType
from jeepney.wrappers import Properties
from openpilot.starpilot.system.starport.bootstrap import WifiCredentials
from openpilot.starpilot.system.starport.identity import DATA_DIR, boot_id as _boot_id
NM = "org.freedesktop.NetworkManager"
NM_PATH = "/org/freedesktop/NetworkManager"
NM_IFACE = "org.freedesktop.NetworkManager"
NM_DEVICE_IFACE = "org.freedesktop.NetworkManager.Device"
NM_ACTIVE_IFACE = "org.freedesktop.NetworkManager.Connection.Active"
NM_SETTINGS_CONNECTION_IFACE = "org.freedesktop.NetworkManager.Settings.Connection"
NM_IP4_CONFIG_IFACE = "org.freedesktop.NetworkManager.IP4Config"
NM_WIRELESS_IFACE = "org.freedesktop.NetworkManager.Device.Wireless"
NM_AP_IFACE = "org.freedesktop.NetworkManager.AccessPoint"
DEVICE_TYPE_WIFI = 2
DEVICE_STATE_DISCONNECTED = 30
DEVICE_STATE_PREPARE = 40
DEVICE_STATE_IP_CONFIG = 70
DEVICE_STATE_ACTIVATED = 100
DHCP_RESTARTS = 3
DEVICE_STATES_NOT_MANAGED = (10, 20) # NM_DEVICE_STATE_UNMANAGED, NM_DEVICE_STATE_UNAVAILABLE
DEVICE_READY_TIMEOUT_S = 5.0
# A shared interface can be briefly unmanaged (for example while the Wi-Fi mode is being switched). Bugsink showed 19 joins
# refused with "device is strictly unmanaged"; waiting a few seconds turns a transient state into a normal join, and a
# device that stays unmanaged is reported as such instead of as an opaque D-Bus error.
UNMANAGED_WAIT_S = 8.0
ACTIVE_STATE_ACTIVATED = 2
ACTIVE_STATE_DEACTIVATED = 4
# The car sends its Wi-Fi details ~4 s after ignition, but a Honda's access point took ~55 s from ignition to appear
# (2026-09-30 drives). After a reported failed join that car stopped auto-launching projection until the driver tapped
# Android Auto, so wait for a slow access point rather than give up. The car's reported BSSID gets most of the time;
# the SSID-only retry covers head units that report the wrong radio.
JOIN_TIMEOUT_S = 120.0
BSSID_SHARE = 0.75
CONNECTION_ID = "starpilot-starport"
SECURITY_WPA3 = (32,)
AUTOCONNECT_MARKER = DATA_DIR / "wifi_autoconnect_paused.json"
REJOIN_MARKER = DATA_DIR / "wifi_rejoin.json"
REJOIN_ATTEMPTS = 2
DRIVE_JOIN_TIMEOUT_S = 20.0
DRIVE_SCAN_WAIT_S = 5.0
DRIVE_SCAN_EVERY_S = 45.0 # a forced scan takes the shared radio off the car's channel, so not on every check
AP_FRESH_S = 20.0 # a network not heard in the latest scan has been driven away from
FAILED_SKIP_S = 120.0 # a network that just refused a join is passed over for this long
DEVICE_STATE_FAILED = 120
class NetworkError(RuntimeError):
pass
class DedicatedUnavailable(NetworkError):
"""The dedicated car interface cannot be used this session; projection falls back to wlan0."""
class JoinTimeout(NetworkError):
"""The car's network never came up in time."""
def _paused_interface(marker: Path) -> str:
"""The interface a session paused this boot and never restored, or ""; NetworkManager resets autoconnect on reboot."""
try:
state = json.loads(marker.read_text())
except (OSError, ValueError):
return ""
return state.get("interface", "") if isinstance(state, dict) and state.get("boot") == _boot_id() else ""
def recover_autoconnect(log: Callable[..., None], marker: Path = AUTOCONNECT_MARKER) -> bool:
"""Turn wlan0 autoconnect back on after a session that paused it ended without restoring it. True when done."""
if not marker.exists():
return True
interface = _paused_interface(marker)
if interface:
lease = NetworkLease(log, interface)
try:
path = next((path for path, name in lease._wifi_devices() if name == interface), "")
if not path:
return False
lease._set(path, NM_DEVICE_IFACE, "Autoconnect", "b", True)
log("wifi_autoconnect_recovered", interface=interface)
except NetworkError as error:
log("wifi_autoconnect_recover_failed", error=str(error))
return False
finally:
if lease.router is not None:
lease.router.close()
marker.unlink(missing_ok=True)
return True
def _rejoin_state(marker: Path) -> dict:
try:
state = json.loads(marker.read_text())
except (OSError, ValueError):
return {}
return state if isinstance(state, dict) and state.get("boot") == _boot_id() else {}
def _remember_interrupted(marker: Path, interface: str, connection: str) -> None:
state = _rejoin_state(marker)
connections = [path for path in state.get("connections", []) if path != connection] + [connection]
marker.parent.mkdir(parents=True, exist_ok=True)
marker.write_text(json.dumps({"interface": interface, "boot": _boot_id(), "connections": connections}))
def rejoin_interrupted(log: Callable[..., None], marker: Path = REJOIN_MARKER) -> bool:
"""Rejoin a saved network a hold stopped mid-join; NetworkManager blocks its autoconnect until then. True when done."""
if not marker.exists():
return True
state = _rejoin_state(marker)
connections = [path for path in state.get("connections", []) if isinstance(path, str)]
if not connections:
marker.unlink(missing_ok=True) # a reboot clears NetworkManager's block
return True
saved = state.get("failures")
failures: dict[str, int] = saved if isinstance(saved, dict) else {}
lease = NetworkLease(log, state.get("interface", "wlan0"))
try:
path = next((path for path, name in lease._wifi_devices() if name == lease.interface), "")
if not path or not lease._get(path, NM_DEVICE_IFACE, "Autoconnect"):
return False
if int(lease._get(path, NM_DEVICE_IFACE, "State")) != DEVICE_STATE_DISCONNECTED:
return False # busy or on another network; wait until it is idle
available = set(lease._get(path, NM_DEVICE_IFACE, "AvailableConnections"))
connection = next((connection for connection in reversed(connections) if connection in available), "") # newest first
if not connection:
return False
try:
lease._call(NM_PATH, NM_IFACE, "ActivateConnection", "ooo", (connection, path, "/"))
log("wifi_rejoined", interface=lease.interface)
except NetworkError as error:
failures[connection] = failures.get(connection, 0) + 1
log("wifi_rejoin_failed", error=str(error), attempt=failures[connection])
if failures[connection] < REJOIN_ATTEMPTS:
marker.write_text(json.dumps({**state, "failures": failures}))
return False
except NetworkError as error:
log("wifi_rejoin_failed", error=str(error))
return False
finally:
if lease.router is not None:
lease.router.close()
connections.remove(connection)
failures.pop(connection, None)
if connections:
marker.write_text(json.dumps({**state, "connections": connections, "failures": failures}))
return False
marker.unlink(missing_ok=True)
return True
def static_address(endpoint_ip: str) -> str:
"""An unused address beside the dongle's own, for dongles whose DHCP server never answers."""
try:
address = ipaddress.ip_address(endpoint_ip)
except ValueError:
return ""
if address.version != 4 or not address.is_private:
return ""
octets = str(address).split(".")
octets[3] = "77" if octets[3] != "77" else "78"
return ".".join(octets)
def connection_settings(credentials: WifiCredentials, interface: str, static_ip: str = "") -> dict:
wireless = {
"ssid": ("ay", credentials.ssid.encode("utf-8")),
"mode": ("s", "infrastructure"),
"hidden": ("b", True), # projection networks are often hidden; harmless when broadcast
}
if credentials.bssid:
wireless["bssid"] = ("ay", bytes.fromhex(credentials.bssid.replace(":", "")))
settings: dict[str, dict] = {
"connection": {
"type": ("s", "802-11-wireless"),
"uuid": ("s", str(uuid.uuid4())),
"id": ("s", CONNECTION_ID),
"interface-name": ("s", interface),
"autoconnect": ("b", False),
"autoconnect-retries": ("i", 1),
},
"802-11-wireless": wireless,
"ipv4": {
"method": ("s", "auto"),
"never-default": ("b", True),
"ignore-auto-dns": ("b", True),
"route-metric": ("x", 3000),
},
"ipv6": {"method": ("s", "ignore")},
}
if static_ip:
settings["ipv4"]["method"] = ("s", "manual")
settings["ipv4"]["address-data"] = ("aa{sv}", [{"address": ("s", static_ip), "prefix": ("u", 24)}])
if not credentials.open:
settings["802-11-wireless-security"] = {
"key-mgmt": ("s", "sae" if credentials.security in SECURITY_WPA3 else "wpa-psk"),
"psk": ("s", credentials.key),
}
return settings
class NetworkLease:
def __init__(self, log: Callable[..., None], interface: str = "wlan0", dedicated: bool = False):
"""``dedicated``: the interface exists only for the car, so never fall back to another one or touch its previous network."""
self.log = log
self.interface = interface
self.dedicated = dedicated
self.router: DBusRouter | None = None
self.device_path = ""
self.connection_path = ""
self.active_path = ""
self.previous_connection = ""
self.local_ip = ""
self.device_ready = False
self.autoconnect_paused = False
self.dhcp_patience = 0.0
self.static_ip = ""
self.endpoint_ip: Callable[[], str] = lambda: ""
self.marker = AUTOCONNECT_MARKER
self.rejoin_marker = REJOIN_MARKER
self._failed_at: dict[str, float] = {}
self._scanned_at = -DRIVE_SCAN_EVERY_S
self._lock = threading.Lock()
def _open(self) -> DBusRouter:
if self.router is None:
self.router = DBusRouter(open_dbus_connection(bus="SYSTEM"))
return self.router
def _call(self, path: str, interface: str, member: str, signature: str | None = None, body: tuple = (), timeout: float = 10.0):
address = DBusAddress(path, bus_name=NM, interface=interface)
message = new_method_call(address, member, signature, body) if signature else new_method_call(address, member)
reply = self._open().send_and_get_reply(message, timeout=timeout)
if reply.header.message_type == MessageType.error:
raise NetworkError(f"{member} failed: {reply.body[0] if reply.body else reply.header}")
return reply.body
def _get(self, path: str, interface: str, name: str):
address = DBusAddress(path, bus_name=NM, interface=interface)
reply = self._open().send_and_get_reply(Properties(address).get(name), timeout=5.0)
if reply.header.message_type == MessageType.error:
raise NetworkError(f"Cannot read {name}: {reply.body[0] if reply.body else ''}")
return reply.body[0][1]
def _set(self, path: str, interface: str, name: str, signature: str, value) -> None:
address = DBusAddress(path, bus_name=NM, interface=interface)
reply = self._open().send_and_get_reply(Properties(address).set(name, signature, value), timeout=5.0)
if reply.header.message_type == MessageType.error:
raise NetworkError(f"Cannot set {name}: {reply.body[0] if reply.body else ''}")
def _wifi_devices(self) -> list[tuple[str, str]]:
devices = []
for path in self._call(NM_PATH, NM_IFACE, "GetDevices")[0]:
try:
if self._get(path, NM_DEVICE_IFACE, "DeviceType") == DEVICE_TYPE_WIFI:
devices.append((path, self._get(path, NM_DEVICE_IFACE, "Interface")))
except NetworkError:
continue
return devices
def _wifi_device(self) -> str:
devices = self._wifi_devices()
for path, name in devices:
if name == self.interface:
return path
if not devices:
raise NetworkError("No Wi-Fi device found")
self.interface = devices[0][1]
return devices[0][0]
def prepare_device(self, cancelled: Callable[[], bool] = lambda: False) -> None:
"""Dedicated mode, ahead of the join: wait for NetworkManager to take the new interface and keep saved networks off it."""
with self._lock:
self._prepare_device(cancelled)
def acquire(self, credentials: WifiCredentials, timeout: float = JOIN_TIMEOUT_S, cancelled: Callable[[], bool] = lambda: False,
dhcp_patience: float = 0.0, endpoint_ip: Callable[[], str] = lambda: "", static_first: bool = False) -> str:
"""Join the projection network and return the comma's IPv4 address on it.
The BSSID the car reports is tried first (fast, unambiguous); if that
attempt fails, one retry matches the SSID alone in case the reported BSSID
belongs to a different radio of the head unit.
"""
self.dhcp_patience = dhcp_patience
self.endpoint_ip = endpoint_ip
self.static_ip = static_address(endpoint_ip()) if static_first else ""
attempts = [credentials]
if credentials.bssid:
attempts.append(replace(credentials, bssid=""))
last_error: NetworkError | None = None
for index, attempt in enumerate(attempts):
try:
share = 1.0 if len(attempts) == 1 else (BSSID_SHARE if index == 0 else 1.0 - BSSID_SHARE)
return self._acquire_once(attempt, timeout * share, cancelled)
except DedicatedUnavailable:
raise
except NetworkError as error:
last_error = error
if cancelled() or index == len(attempts) - 1:
raise
self.log("wifi_retry_without_bssid", error=str(error))
self._drop_attempt()
raise last_error or NetworkError("Could not join the projection network")
def _drop_attempt(self) -> None:
for path, interface, member in ((self.active_path, NM_IFACE, "DeactivateConnection"),
(self.connection_path, NM_SETTINGS_CONNECTION_IFACE, "Delete")):
if not path:
continue
try:
if member == "DeactivateConnection":
self._call(NM_PATH, NM_IFACE, member, "o", (path,))
else:
self._call(path, interface, member)
except NetworkError:
pass
self.active_path = self.connection_path = ""
def _acquire_once(self, credentials: WifiCredentials, timeout: float, cancelled: Callable[[], bool]) -> str:
deadline = time.monotonic() + timeout
restarts = 0
while True:
self._activate(credentials, cancelled)
if self._wait_activated(deadline, timeout, cancelled, restarts < DHCP_RESTARTS):
break
restarts += 1
self.static_ip = static_address(self.endpoint_ip())
self.log("wifi_dhcp_restart", interface=self.interface, patience_s=self.dhcp_patience, restarts=restarts, static_ip=self.static_ip)
self._drop_attempt()
self.local_ip = self._ipv4()
if self.dedicated:
self._check_overlap()
self.log("wifi_joined", interface=self.interface, local_ip=self.local_ip, **self._radio())
return self.local_ip
def _activate(self, credentials: WifiCredentials, cancelled: Callable[[], bool]) -> None:
with self._lock:
if not self._get(NM_PATH, NM_IFACE, "WirelessEnabled"):
raise NetworkError("Wi-Fi is turned off on the comma")
if self.dedicated:
self._prepare_device(cancelled)
else:
self.device_path = self._wifi_device()
self._wait_managed(cancelled)
self._pause_autoconnect()
active = self._get(self.device_path, NM_DEVICE_IFACE, "ActiveConnection")
if active and active != "/" and not self.previous_connection and not self.dedicated:
try:
previous = self._get(active, NM_ACTIVE_IFACE, "Connection")
if self._connection_id(previous) != CONNECTION_ID:
self.previous_connection = previous
except NetworkError:
pass
self._delete_stale_profiles()
self.log("wifi_joining", interface=self.interface, **{k: v for k, v in credentials.describe().items() if k != "key_length"},
had_previous=bool(self.previous_connection), static_ip=self.static_ip)
body = self._call(NM_PATH, NM_IFACE, "AddAndActivateConnection2", "a{sa{sv}}ooa{sv}",
(connection_settings(credentials, self.interface, self.static_ip), self.device_path, "/",
{"persist": ("s", "volatile")}), timeout=15.0)
self.connection_path, self.active_path = body[0], body[1]
def _wait_activated(self, deadline: float, timeout: float, cancelled: Callable[[], bool], may_restart: bool) -> bool:
"""True once active; False when DHCP sat unanswered past dhcp_patience and the join should start over."""
ip_config_since = 0.0
while True:
if cancelled():
raise NetworkError("cancelled")
try:
state = self._get(self.active_path, NM_ACTIVE_IFACE, "State")
except NetworkError as error:
raise NetworkError("The projection network connection was removed (wrong key or out of range?)") from error
if state == ACTIVE_STATE_ACTIVATED:
return True
if state == ACTIVE_STATE_DEACTIVATED:
raise NetworkError("NetworkManager could not join the projection network")
now = time.monotonic()
if now >= deadline:
raise JoinTimeout(f"Timed out joining the projection network after {timeout:.0f} s")
if may_restart and self.dhcp_patience > 0:
if self._get(self.device_path, NM_DEVICE_IFACE, "State") == DEVICE_STATE_IP_CONFIG:
ip_config_since = ip_config_since or now
if now - ip_config_since >= self.dhcp_patience:
return False
else:
ip_config_since = 0.0
time.sleep(0.25)
def _frequency(self, path: str) -> int:
access_point = self._get(path, NM_WIRELESS_IFACE, "ActiveAccessPoint")
return int(self._get(access_point, NM_AP_IFACE, "Frequency")) if access_point and access_point != "/" else 0
def _radio(self) -> dict:
"""Channels at join time; a car and home Wi-Fi on different channels share the radio's airtime."""
radio = {}
try:
radio["freq"] = self._frequency(self.device_path)
if self.dedicated:
for path, name in self._wifi_devices():
if name == "wlan0":
radio["wlan0_state"] = int(self._get(path, NM_DEVICE_IFACE, "State"))
radio["wlan0_freq"] = self._frequency(path)
except NetworkError:
pass
return radio
def _wait_step(self, deadline: float, cancelled: Callable[[], bool], failure: str) -> None:
if cancelled():
raise NetworkError("cancelled")
if time.monotonic() >= deadline:
raise DedicatedUnavailable(failure)
time.sleep(0.25)
def _wait_managed(self, cancelled: Callable[[], bool], timeout: float = UNMANAGED_WAIT_S) -> None:
"""Give NetworkManager a few seconds to manage the shared interface before joining on it."""
deadline = time.monotonic() + timeout
waited = False
while (state := self._get(self.device_path, NM_DEVICE_IFACE, "State")) in DEVICE_STATES_NOT_MANAGED:
if cancelled():
raise NetworkError("cancelled")
if time.monotonic() >= deadline:
raise NetworkError(f"{self.interface} is unmanaged by NetworkManager (state {state}) after waiting {timeout:.0f} s")
if not waited:
waited = True
self.log("wifi_waiting_managed", interface=self.interface, state=state)
time.sleep(0.25)
if waited:
self.log("wifi_became_managed", interface=self.interface)
def _prepare_device(self, cancelled: Callable[[], bool]) -> None:
if self.device_ready:
return
deadline = time.monotonic() + DEVICE_READY_TIMEOUT_S
while not (path := next((path for path, name in self._wifi_devices() if name == self.interface), "")):
self._wait_step(deadline, cancelled, f"{self.interface} is not a NetworkManager Wi-Fi device")
self.device_path = path
# Before the device is ready, so a saved network can never autoconnect on it.
if self._get(path, NM_DEVICE_IFACE, "Autoconnect"):
try:
self._set(path, NM_DEVICE_IFACE, "Autoconnect", "b", False)
except NetworkError as error:
raise DedicatedUnavailable(f"Cannot stop saved networks joining {self.interface}") from error
while self._get(path, NM_DEVICE_IFACE, "State") < DEVICE_STATE_DISCONNECTED:
self._wait_step(deadline, cancelled, f"NetworkManager did not take {self.interface}")
self.device_ready = True
# A new interface has never scanned, so the car's BSSID would be rejected until a scan finds it.
try:
self._call(path, NM_WIRELESS_IFACE, "RequestScan", "a{sv}", ({},))
self.log("wifi_scan_requested", interface=self.interface)
except NetworkError as error:
self.log("wifi_scan_request_failed", error=str(error))
def hold_autoconnect(self) -> bool:
"""Stop wlan0 hunting for a network until release(restore=True). True when held."""
with self._lock:
self.device_path = next((path for path, name in self._wifi_devices() if name == self.interface), "")
if not self.device_path:
return False
state = int(self._get(self.device_path, NM_DEVICE_IFACE, "State"))
if state == DEVICE_STATE_ACTIVATED:
if self._carrier():
return False # connected again; nothing to hold
# NetworkManager reports a lost network as connected for 15 s, then joins the next one by itself.
self._pause_autoconnect()
return self.autoconnect_paused
self._pause_autoconnect()
if DEVICE_STATE_PREPARE <= state < DEVICE_STATE_ACTIVATED:
self._remember_join()
try:
self._call(self.device_path, NM_DEVICE_IFACE, "Disconnect") # a join already under way
except NetworkError as error:
self.log("wifi_hold_disconnect_failed", error=str(error))
return self.autoconnect_paused
def _carrier(self) -> bool:
try:
return Path(f"/sys/class/net/{self.interface}/carrier").read_text().strip() == "1"
except OSError:
return True
def drive_join(self, allowed: Callable[[str], bool], before: Callable[[], bool] = lambda: True,
after: Callable[[], None] = lambda: None, timeout: float = DRIVE_JOIN_TIMEOUT_S,
cancelled: Callable[[], bool] = lambda: False) -> str:
"""Join the best allowed saved network in range by hand while auto-join stays paused.
Returns "connected" (already on a network), "none" (nothing to join), "joined" or "failed".
"""
with self._lock:
if cancelled():
return "none"
path = self.device_path or next((path for path, name in self._wifi_devices() if name == self.interface), "")
if not path:
return "none"
state = int(self._get(path, NM_DEVICE_IFACE, "State"))
if state == DEVICE_STATE_ACTIVATED:
return "connected" if self._carrier() else "none"
if state != DEVICE_STATE_DISCONNECTED:
return "none"
self._scan(path, cancelled)
with self._lock:
if cancelled() or int(self._get(path, NM_DEVICE_IFACE, "State")) != DEVICE_STATE_DISCONNECTED:
return "none"
heard = self._heard(path)
now = time.monotonic()
self._failed_at = {key: at for key, at in self._failed_at.items() if now - at < FAILED_SKIP_S}
candidates = []
for connection in self._get(path, NM_DEVICE_IFACE, "AvailableConnections"):
settings = self._call(connection, NM_SETTINGS_CONNECTION_IFACE, "GetSettings")[0]
values = {key: entry[1] for key, entry in settings.get("connection", {}).items()}
if values.get("id") == CONNECTION_ID or not values.get("autoconnect", True) or not allowed(values.get("uuid", "")):
continue
wireless = settings.get("802-11-wireless", {})
strength = heard.get(bytes(wireless["ssid"][1]), -1) if "ssid" in wireless else -1
if strength < 0 or connection in self._failed_at:
continue
candidates.append((int(values.get("autoconnect-priority", 0)), strength, connection))
if not candidates:
return "none"
target = max(candidates)[2]
if not before():
after()
return "none"
try:
with self._lock:
if cancelled() or int(self._get(path, NM_DEVICE_IFACE, "State")) != DEVICE_STATE_DISCONNECTED:
return "none"
self._call(NM_PATH, NM_IFACE, "ActivateConnection", "ooo", (target, path, "/"))
deadline, started = time.monotonic() + timeout, False
while time.monotonic() < deadline:
time.sleep(0.25)
with self._lock:
state = int(self._get(path, NM_DEVICE_IFACE, "State"))
if state == DEVICE_STATE_ACTIVATED:
return "joined"
if state == DEVICE_STATE_FAILED or (started and state == DEVICE_STATE_DISCONNECTED):
self._failed_at[target] = time.monotonic()
return "failed" # refused, or stopped because the car link stalled
if state >= DEVICE_STATE_PREPARE:
started = True
self._failed_at[target] = time.monotonic()
return "failed"
except NetworkError as error:
self.log("wifi_drive_join_failed", error=str(error))
self._failed_at[target] = time.monotonic()
return "failed"
finally:
after()
if self.autoconnect_paused:
try:
with self._lock:
self._set(path, NM_DEVICE_IFACE, "Autoconnect", "b", False) # joining by hand turns auto-join back on
except NetworkError as error:
self.log("wifi_autoconnect_pause_failed", error=str(error))
def _scan(self, path: str, cancelled: Callable[[], bool] = lambda: False) -> None:
"""Request a scan while auto-join is paused and wait briefly for completion."""
if time.monotonic() - self._scanned_at < DRIVE_SCAN_EVERY_S:
return
self._scanned_at = time.monotonic()
try:
with self._lock:
if cancelled():
return
before = int(self._get(path, NM_WIRELESS_IFACE, "LastScan"))
self._call(path, NM_WIRELESS_IFACE, "RequestScan", "a{sv}", ({},))
except NetworkError:
return # already scanning, or scans refused; the access points heard so far still count
deadline = time.monotonic() + DRIVE_SCAN_WAIT_S
while time.monotonic() < deadline:
time.sleep(0.25)
try:
with self._lock:
if cancelled():
return
if int(self._get(path, NM_WIRELESS_IFACE, "LastScan")) != before:
return
except NetworkError:
return
def _heard(self, path: str) -> dict[bytes, int]:
"""Strongest signal per network name among access points heard in the latest scan."""
try:
last_scan = int(self._get(path, NM_WIRELESS_IFACE, "LastScan")) / 1000
except NetworkError:
last_scan = -1
now = time.clock_gettime(getattr(time, "CLOCK_BOOTTIME", time.CLOCK_MONOTONIC))
since = now - AP_FRESH_S
if 0 < now - last_scan <= DRIVE_SCAN_EVERY_S + DRIVE_SCAN_WAIT_S + 5:
since = min(since, last_scan - 5) # between forced scans, what the last one heard still counts
heard: dict[bytes, int] = {}
for access_point in self._get(path, NM_WIRELESS_IFACE, "AccessPoints"):
try:
seen = int(self._get(access_point, NM_AP_IFACE, "LastSeen"))
if seen < 0 or seen < since:
continue
ssid = bytes(self._get(access_point, NM_AP_IFACE, "Ssid"))
heard[ssid] = max(heard.get(ssid, 0), int(self._get(access_point, NM_AP_IFACE, "Strength")))
except NetworkError:
continue
return heard
def _remember_join(self) -> None:
"""Disconnect blocks the network being joined from autoconnecting; note it so it can be rejoined after the session."""
try:
active = self._get(self.device_path, NM_DEVICE_IFACE, "ActiveConnection")
connection = self._get(active, NM_ACTIVE_IFACE, "Connection") if active and active != "/" else ""
if connection and connection != "/":
_remember_interrupted(self.rejoin_marker, self.interface, connection)
except (NetworkError, OSError) as error:
self.log("wifi_rejoin_note_failed", error=str(error))
def _pause_autoconnect(self) -> None:
"""Keep NetworkManager from rejoining home Wi-Fi between attempts; release(restore=True) turns it back on."""
if self.autoconnect_paused:
return
try:
if self._get(self.device_path, NM_DEVICE_IFACE, "Autoconnect"):
if not self._write_marker():
return
self._set(self.device_path, NM_DEVICE_IFACE, "Autoconnect", "b", False)
self.autoconnect_paused = True
elif _paused_interface(self.marker) == self.interface:
self.autoconnect_paused = True
except NetworkError as error:
self.log("wifi_autoconnect_pause_failed", error=str(error))
def _write_marker(self) -> bool:
try:
self.marker.parent.mkdir(parents=True, exist_ok=True)
self.marker.write_text(json.dumps({"interface": self.interface, "boot": _boot_id()}))
return True
except OSError as error:
self.log("wifi_autoconnect_marker_failed", error=str(error))
return False
def _networks(self, path: str) -> list:
config = self._get(path, NM_DEVICE_IFACE, "Ip4Config")
if not config or config == "/":
return []
return [ipaddress.ip_network(f"{entry['address'][1]}/{entry['prefix'][1]}", strict=False)
for entry in self._get(config, NM_IP4_CONFIG_IFACE, "AddressData") if "address" in entry and "prefix" in entry]
def _check_overlap(self) -> None:
"""The car's subnet must not also be reachable through home Wi-Fi, or replies to the head unit leave on wlan0."""
ours = self._networks(self.device_path)
for path in self._call(NM_PATH, NM_IFACE, "GetDevices")[0]:
if path == self.device_path:
continue
try:
others = self._networks(path)
except NetworkError:
continue
for other in others:
if any(network.overlaps(other) for network in ours):
self.log("wifi_subnet_overlap", car=str(ours[0]), other=str(other))
raise DedicatedUnavailable(f"The car's network {ours[0]} overlaps {other}")
def _connection_id(self, path: str) -> str:
try:
return self._call(path, NM_SETTINGS_CONNECTION_IFACE, "GetSettings")[0].get("connection", {}).get("id", ("s", ""))[1]
except NetworkError:
return ""
def _ipv4(self) -> str:
config = self._get(self.active_path, NM_ACTIVE_IFACE, "Ip4Config")
if config and config != "/":
for entry in self._get(config, NM_IP4_CONFIG_IFACE, "AddressData"):
address = entry.get("address", ("s", ""))[1]
if address:
return address
raise NetworkError("Joined the projection network but received no IPv4 address")
def _delete_stale_profiles(self) -> None:
"""Remove profiles left by a crashed earlier session (volatile ones vanish on their own)."""
settings = DBusAddress("/org/freedesktop/NetworkManager/Settings", bus_name=NM, interface="org.freedesktop.NetworkManager.Settings")
reply = self._open().send_and_get_reply(new_method_call(settings, "ListConnections"), timeout=5.0)
if reply.header.message_type == MessageType.error:
return
for path in reply.body[0]:
try:
body = self._call(path, NM_SETTINGS_CONNECTION_IFACE, "GetSettings")
if body[0].get("connection", {}).get("id", ("s", ""))[1] == CONNECTION_ID:
self._call(path, NM_SETTINGS_CONNECTION_IFACE, "Delete")
self.log("wifi_stale_profile_removed")
except NetworkError:
continue
def still_connected(self) -> bool:
with self._lock:
if not self.active_path:
return False
return self._get(self.active_path, NM_ACTIVE_IFACE, "State") == ACTIVE_STATE_ACTIVATED
def release(self, restore: bool = True) -> None:
"""Undo only what this lease owns. Safe to call repeatedly.
``restore=False`` is for a retry: the earlier network is remembered and
reactivated only when the session finally stops, avoiding churn between attempts.
"""
with self._lock:
if self.router is None and not (restore and (self.previous_connection or self.autoconnect_paused)):
return
ours_active = False
try:
if self.device_path:
current = self._get(self.device_path, NM_DEVICE_IFACE, "ActiveConnection")
ours_active = current == self.active_path or current in ("", "/")
except NetworkError:
ours_active = True
if self.active_path:
try:
self._call(NM_PATH, NM_IFACE, "DeactivateConnection", "o", (self.active_path,))
except NetworkError:
pass
if self.connection_path:
try:
self._call(self.connection_path, NM_SETTINGS_CONNECTION_IFACE, "Delete")
except NetworkError:
pass # volatile profiles delete themselves on deactivation
if restore and self.autoconnect_paused:
try:
self._set(self.device_path, NM_DEVICE_IFACE, "Autoconnect", "b", True)
self.marker.unlink(missing_ok=True)
except (NetworkError, OSError) as error:
self.log("wifi_autoconnect_restore_failed", error=str(error))
self.autoconnect_paused = False
if restore and ours_active and self.previous_connection and self.device_path:
try:
self._call(NM_PATH, NM_IFACE, "ActivateConnection", "ooo", (self.previous_connection, self.device_path, "/"))
self.log("wifi_restored_previous")
except NetworkError as error:
self.log("wifi_restore_failed", error=str(error))
self.connection_path = self.active_path = self.local_ip = ""
if restore:
self.previous_connection = ""
try:
if self.router is not None:
self.router.close()
finally:
self.router = None