| 21 | |
| 22 | |
| 23 | class WifiHandler(InstanceRunnable[bool]): |
| 24 | def __init__(self) -> None: |
| 25 | self._wpa_config: WpaSupplicantConfig = WpaSupplicantConfig() |
| 26 | |
| 27 | @override |
| 28 | async def run(self) -> bool | None: |
| 29 | """ |
| 30 | This is the entry point that is called by components.TApp |
| 31 | """ |
| 32 | wifi_iface = self._find_wifi_interface() |
| 33 | |
| 34 | if not wifi_iface: |
| 35 | debug('No wifi interface found') |
| 36 | return False |
| 37 | |
| 38 | prompt = tr('No network connection found') + '\n\n' |
| 39 | prompt += tr('Would you like to connect to a Wifi?') + '\n' |
| 40 | |
| 41 | result = await ConfirmationScreen[bool]( |
| 42 | MenuItemGroup.yes_no(), |
| 43 | header=prompt, |
| 44 | allow_skip=True, |
| 45 | allow_reset=True, |
| 46 | ).run() |
| 47 | |
| 48 | match result.type_: |
| 49 | case ResultType.Selection: |
| 50 | if result.get_value() is False: |
| 51 | return False |
| 52 | case ResultType.Skip | ResultType.Reset: |
| 53 | return False |
| 54 | |
| 55 | setup_result = await self._setup_wifi(wifi_iface) |
| 56 | return setup_result |
| 57 | |
| 58 | async def _enable_supplicant(self, wifi_iface: str) -> bool: |
| 59 | self._wpa_config.load_config() |
| 60 | |
| 61 | result = self._wpa_cli('status') # if it it's running it will blow up |
| 62 | |
| 63 | if result.success: |
| 64 | debug('wpa_supplicant already running') |
| 65 | return True |
| 66 | |
| 67 | if result.error and 'failed to connect to non-global ctrl_ifname'.lower() not in result.error.lower(): |
| 68 | debug('Unexpected wpa_cli failure') |
| 69 | return False |
| 70 | |
| 71 | debug('wpa_supplicant not running, trying to enable') |
| 72 | |
| 73 | try: |
| 74 | SysCommand(f'wpa_supplicant -B -i {wifi_iface} -c {self._wpa_config.config_file}') |
| 75 | result = self._wpa_cli('status') # if it it's running it will blow up |
| 76 | |
| 77 | if result.success: |
| 78 | debug('successfully enabled wpa_supplicant') |
| 79 | return True |
| 80 | else: |