diff --git a/src/wifi_connection.py b/src/wifi_connection.py index f08374e..641cd4c 100644 --- a/src/wifi_connection.py +++ b/src/wifi_connection.py @@ -4,6 +4,7 @@ import secrets import shlex import subprocess import tempfile +import time from dataclasses import dataclass from pathlib import Path @@ -63,7 +64,9 @@ class WiFiConnectionService: def __init__( self, command_runner=None, - command_timeout=30 + command_timeout=30, + scan_timeout=15, + scan_poll_interval=0.25 ): self.command_runner = ( command_runner @@ -72,6 +75,8 @@ class WiFiConnectionService: ) self.command_timeout = command_timeout + self.scan_timeout = scan_timeout + self.scan_poll_interval = scan_poll_interval @staticmethod def _validate_interface(interface): @@ -216,6 +221,79 @@ class WiFiConnectionService: f"Cannot parse D-Bus property {property_name}." ) from error + def _get_last_scan(self, device_path): + + parts = self._get_property( + device_path, + NM_DEVICE_WIRELESS, + "LastScan" + ) + + if len(parts) != 2 or parts[0] != "x": + raise WiFiConnectionError( + "Unexpected D-Bus type for LastScan." + ) + + try: + return int(parts[1]) + + except ValueError as error: + + raise WiFiConnectionError( + "Invalid LastScan value from NetworkManager." + ) from error + + def _request_fresh_scan(self, device_path): + + previous_scan = self._get_last_scan( + device_path + ) + + self._busctl( + "call", + NM_SERVICE, + device_path, + NM_DEVICE_WIRELESS, + "RequestScan", + "a{sv}", + "0" + ) + + deadline = ( + time.monotonic() + + + self.scan_timeout + ) + + while True: + + current_scan = self._get_last_scan( + device_path + ) + + if current_scan > previous_scan: + return + + remaining = ( + deadline + - + time.monotonic() + ) + + if remaining <= 0: + break + + time.sleep( + min( + self.scan_poll_interval, + remaining + ) + ) + + raise WiFiConnectionError( + "NetworkManager did not complete a fresh Wi-Fi scan in time." + ) + def _get_integer_property( self, object_path, @@ -346,6 +424,10 @@ class WiFiConnectionService: interface ) + self._request_fresh_scan( + device_path + ) + output = self._busctl( "call", NM_SERVICE, @@ -757,6 +839,7 @@ class WiFiConnectionService: profile_uuid = None success = False + operation_error = None error_message = None restore_error = None cleanup_error = None @@ -777,8 +860,9 @@ class WiFiConnectionService: success = True - except NetworkCommandError as error: + except Exception as error: + operation_error = error error_message = str( error ) @@ -837,34 +921,41 @@ class WiFiConnectionService: error ) - if restore_error and cleanup_error: - - raise NetworkRestoreError( - "The Wi-Fi verification attempt finished, but the previous connection " - f"could not be restored: {restore_error}. " - f"The temporary profile could not be deleted: {cleanup_error}" - ) + verification_error = ( + f"Password verification failed: {operation_error}. " + if operation_error else "" + ) if restore_error: raise NetworkRestoreError( - "The Wi-Fi verification attempt finished, but the previous connection " - f"could not be restored: {restore_error}" + f"{verification_error}" + "The previous Wi-Fi connection could not be restored: " + f"{restore_error}. " + + ( + f"The temporary profile could not be deleted: {cleanup_error}" + if cleanup_error else "" + ) ) if cleanup_error: - verification_error = ( - f"Password verification failed: {error_message}. " - if error_message else "" - ) - raise NetworkCleanupError( f"{verification_error}" "The temporary Wi-Fi profile could not be deleted: " f"{cleanup_error}" ) + if ( + operation_error + and + not isinstance( + operation_error, + NetworkCommandError + ) + ): + raise operation_error + return { "success": success, "interface": interface,