|
20 | 20 | NetworkManagerSettings, |
21 | 21 | ) |
22 | 22 | from sdbus_async.networkmanager.enums import AccessPointCapabilities, WpaSecurityFlags |
23 | | -from typedefs import SavedWifiNetwork, ScannedWifiNetwork, WifiCredentials, WifiStatus |
| 23 | +from typedefs import SavedWifiNetwork, ScannedWifiNetwork, WifiCredentials, WifiInterface, WifiStatus |
24 | 24 | from wifi_handlers.AbstractWifiHandler import AbstractWifiManager |
25 | 25 |
|
26 | 26 |
|
@@ -107,19 +107,46 @@ async def start(self) -> None: |
107 | 107 | for sig in (signal.SIGTERM, signal.SIGINT): |
108 | 108 | loop.add_signal_handler(sig, lambda s=sig: asyncio.create_task(self.handle_shutdown(s))) |
109 | 109 |
|
110 | | - # Find WiFi device |
| 110 | + # Find WiFi device (use configured interface if set) |
| 111 | + await self._select_wifi_device() |
| 112 | + |
| 113 | + # Create virtual AP interface if needed |
| 114 | + await self._create_virtual_interface() |
| 115 | + self._tasks.append(asyncio.get_event_loop().create_task(self._autoscan())) |
| 116 | + self._tasks.append(asyncio.get_event_loop().create_task(self.hotspot_watchdog())) |
| 117 | + |
| 118 | + async def _get_wifi_devices(self) -> List[tuple[str, str]]: |
| 119 | + """Get all WiFi devices as (device_path, interface_name) tuples.""" |
111 | 120 | assert self._nm is not None |
| 121 | + wifi_devices: List[tuple[str, str]] = [] |
112 | 122 | devices = await self._nm.get_devices() |
113 | 123 | for device_path in devices: |
114 | 124 | device = NetworkDeviceWireless(device_path, self._bus) |
115 | 125 | if await device.device_type == DeviceType.WIFI: |
116 | | - self._device_path = device_path |
117 | | - break |
| 126 | + interface_name = await device.interface |
| 127 | + wifi_devices.append((device_path, interface_name)) |
| 128 | + return wifi_devices |
| 129 | + |
| 130 | + async def _select_wifi_device(self) -> None: |
| 131 | + """Select the WiFi device based on settings or first available.""" |
| 132 | + wifi_devices = await self._get_wifi_devices() |
| 133 | + if not wifi_devices: |
| 134 | + logger.warning("No WiFi devices found") |
| 135 | + return |
118 | 136 |
|
119 | | - # Create virtual AP interface if needed |
120 | | - await self._create_virtual_interface() |
121 | | - self._tasks.append(asyncio.get_event_loop().create_task(self._autoscan())) |
122 | | - self._tasks.append(asyncio.get_event_loop().create_task(self.hotspot_watchdog())) |
| 137 | + configured_interface = self._settings_manager.settings.primary_interface |
| 138 | + if configured_interface: |
| 139 | + for device_path, interface_name in wifi_devices: |
| 140 | + if interface_name == configured_interface: |
| 141 | + self._device_path = device_path |
| 142 | + logger.info(f"Using configured WiFi interface: {configured_interface}") |
| 143 | + return |
| 144 | + |
| 145 | + logger.warning(f"Configured interface {configured_interface} not found, using first available") |
| 146 | + |
| 147 | + # Use first available device |
| 148 | + self._device_path = wifi_devices[0][0] |
| 149 | + logger.info(f"Using WiFi interface: {wifi_devices[0][1]}") |
123 | 150 |
|
124 | 151 | async def _autoscan(self) -> None: |
125 | 152 |
|
@@ -615,3 +642,31 @@ async def hotspot_watchdog(self) -> None: |
615 | 642 | if not await self.hotspot_is_running(): |
616 | 643 | logger.info("No network connection detected, enabling hotspot") |
617 | 644 | await self.enable_hotspot() |
| 645 | + |
| 646 | + async def get_interfaces(self) -> List[WifiInterface]: |
| 647 | + """Get available WiFi interfaces.""" |
| 648 | + wifi_devices = await self._get_wifi_devices() |
| 649 | + current_interface = None |
| 650 | + if self._device_path: |
| 651 | + device = NetworkDeviceWireless(self._device_path, self._bus) |
| 652 | + current_interface = await device.interface |
| 653 | + |
| 654 | + return [ |
| 655 | + WifiInterface(name=interface_name, is_primary=interface_name == current_interface) |
| 656 | + for _, interface_name in wifi_devices |
| 657 | + ] |
| 658 | + |
| 659 | + async def set_primary_interface(self, interface_name: str) -> None: |
| 660 | + """Set the primary WiFi interface.""" |
| 661 | + wifi_devices = await self._get_wifi_devices() |
| 662 | + available_interfaces = [name for _, name in wifi_devices] |
| 663 | + |
| 664 | + if interface_name not in available_interfaces: |
| 665 | + raise ValueError(f"Interface {interface_name} not found. Available: {available_interfaces}") |
| 666 | + |
| 667 | + self._settings_manager.settings.primary_interface = interface_name |
| 668 | + self._settings_manager.save() |
| 669 | + |
| 670 | + # Switch to the new interface |
| 671 | + await self._select_wifi_device() |
| 672 | + logger.info(f"Primary interface set to: {interface_name}") |
0 commit comments