def test_select_devices_same_specified_not_found(self): wi_dev = mock.MagicMock(**{'WirelessCapabilities': 100}) dev1 = mock.MagicMock(**{ 'DeviceType': 2, 'SpecificDevice.return_value': wi_dev }) dev2 = mock.MagicMock(**{ 'DeviceType': 2, 'SpecificDevice.return_value': wi_dev }) byipiface = mock.MagicMock( side_effect=KeyError) # actual is dbus.exceptions.DBusException: nm = mock.MagicMock( **{ 'NetworkManager.GetDevices.return_value': [dev1, dev2], 'NetworkManager.GetDeviceByIpIface': byipiface, 'NM_DEVICE_TYPE_WIFI': 2, 'NM_WIFI_DEVICE_CAP_AP': 100 }) conf = { 'delete_existing_ap_connections': False, 'ap_device': 'any', 'client_device': 'foo0' } with self.assertRaises(KeyError): nm_helper.select_devices(conf, NetworkManager=nm)
def test_select_devices_same_specified_not_wireless(self): wi_dev = mock.MagicMock(**{'WirelessCapabilities': 0}) dev1 = mock.MagicMock(**{ 'DeviceType': 0, 'SpecificDevice.return_value': wi_dev }) dev2 = mock.MagicMock(**{ 'DeviceType': 2, 'SpecificDevice.return_value': wi_dev }) nm = mock.MagicMock( **{ 'NetworkManager.GetDevices.return_value': [dev1, dev2], 'NetworkManager.GetDeviceByIpIface.return_value': dev1, 'NM_DEVICE_TYPE_WIFI': 2, 'NM_WIFI_DEVICE_CAP_AP': 100 }) conf = { 'delete_existing_ap_connections': False, 'ap_device': 'foo0', 'client_device': 'foo0' } with self.assertRaisesRegex(AssertionError, 'not ap capable|not wireless'): nm_helper.select_devices(conf, NetworkManager=nm)
def test_select_devices_no_devices(self): nm = mock.MagicMock( **{'NetworkManager.GetDevices.return_value': list()}) conf = { 'delete_existing_ap_connections': False, 'ap_device': 'foo', 'client_device': 'foo' } with self.assertRaises(RuntimeError): nm_helper.select_devices(conf, NetworkManager=nm)
def test_select_devices_client_specified_device_multiple_total(self): wi_dev = mock.MagicMock(**{'WirelessCapabilities': 100}) dev1 = mock.MagicMock(**{ 'DeviceType': 2, 'SpecificDevice.return_value': wi_dev }) dev2 = mock.MagicMock(**{ 'DeviceType': 2, 'SpecificDevice.return_value': wi_dev }) byipiface = mock.MagicMock(**{'return_value': dev1}) nm = mock.MagicMock( **{ 'NetworkManager.GetDevices.return_value': [dev1, dev2], 'NetworkManager.GetDeviceByIpIface': byipiface, 'NM_DEVICE_TYPE_WIFI': 2, 'NM_WIFI_DEVICE_CAP_AP': 100 }) conf = { 'delete_existing_ap_connections': False, 'ap_device': 'any', 'client_device': 'foo0' } ap, client = nm_helper.select_devices(conf, NetworkManager=nm) self.assertIs(ap, dev2) self.assertIs(client, dev1)
def main(): pifi_conf_settings = etc_io.get_conf() ApModeDevice, ClientModeDevice = nm.select_devices(pifi_conf_settings) print("Using %s for AP mode support" % ApModeDevice.Interface) print("Using %s for wifi client mode" % ClientModeDevice.Interface) status_led = pifi_conf_settings["status_led"] leds.try_blink(status_led, delay_on=initializing_led[0], delay_off=initializing_led[1]) # Allow 30 seconds for network manager to sort itself out time.sleep(30) var_io.writeSeenSSIDs(nm.seenSSIDs([ClientModeDevice])) if ClientModeDevice.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED: print("Client Device currently connected to: %s" % ClientModeDevice.SpecificDevice().ActiveAccessPoint.Ssid) leds.try_blink(status_led, delay_on=connected_led[0], delay_off=connected_led[1]) # Run button handler, and when that is done, exit handle_button(pifi_conf_settings, ApModeDevice, ClientModeDevice) return else: print( "Device is not connected to any network, Looking for pending connections" ) pending = var_io.readPendingConnections() # Try to pick a connection to use, if none found, just continue try: # Use the best connection best_ap, best_con = nm.selectConnection( nm.availibleConnections(ClientModeDevice, pending)) print("Connecting to %s" % best_con["802-11-wireless"]["ssid"]) NetworkManager.NetworkManager.AddAndActivateConnection( best_con, ClientModeDevice, best_ap) new_pending = var_io.readPendingConnections().remove(best_con) var_io.writePendingConnections(new_pending) leds.try_blink(status_led, delay_on=connected_led[0], delay_off=connected_led[1]) # Run button handler, and when that is done, exit handle_button(pifi_conf_settings, ApModeDevice, ClientModeDevice) return except ValueError: pass # If we reach this point, we gave up on Client mode print("No SSIDs from pending connections found") start_ap_mode(pifi_conf_settings, ApModeDevice, ClientModeDevice)
def rescan(): pifi_conf_settings = etc_io.get_conf() ApModeDevice, ClientModeDevice = nm.select_devices(pifi_conf_settings) if ApModeDevice.State != 100: print("AP Device is not active") else: current_connection = ApModeDevice.GetAppliedConnection(0) if 'mode' in current_connection[0][ '802-11-wireless'] and current_connection[0][ '802-11-wireless']['mode'] == "ap": print( "Device is currently acting as an Access Point, Rescanning requires turning this off" ) print("This will disrupt any SSH connections") if query_yes_no("Continue?") == False: return ApModeDevice.Disconnect() print("Waiting for wifi rescan") time.sleep(30) try: var_io.writeSeenSSIDs(nm.seenSSIDs([ClientModeDevice])) except PermissionError: print("Error writing to /var/lib/pifi/seen_ssids, continuing") if (ClientModeDevice.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED): print("Connected to: %s" % ClientModeDevice.SpecificDevice().ActiveAccessPoint.Ssid) return print( "Device is not connected to any network, Looking for pending connections" ) pending = var_io.readPendingConnections() # Try to pick a connection to use, if none found, just continue try: # Use the best connection best_ap, best_con = nm.selectConnection( nm.availibleConnections(ClientModeDevice, pending)) print("Connecting to %s" % best_con['802-11-wireless']['ssid']) NetworkManager.NetworkManager.AddAndActivateConnection( best_con, ClientModeDevice, best_ap) new_pending = var_io.readPendingConnections().remove(best_con) var_io.writePendingConnections(new_pending) return except ValueError: pass # If we reach this point, we gave up on Client mode print("No SSIDs from pending connections found") startup.start_ap_mode(pifi_conf_settings, ApModeDevice, ClientModeDevice)
def rescan(argv): parser = argparse.ArgumentParser( description="Stop AP mode and rescan for known networks, start AP mode again if none found" ) parser.add_argument("-y", action="store_true") args = parser.parse_args(argv) skip_prompt = args.y pifi_conf_settings = etc_io.get_conf() ApModeDevice, ClientModeDevice = nm.select_devices(pifi_conf_settings) if ApModeDevice.State != 100: print("AP Device is not active") else: current_connection = ApModeDevice.GetAppliedConnection(0) if ( "mode" in current_connection[0]["802-11-wireless"] and current_connection[0]["802-11-wireless"]["mode"] == "ap" ): print( "Device is currently acting as an Access Point, Rescanning requires turning this off" ) print("This will disrupt any SSH connections") # If skip_prompt is true, short circuit the if, otherwise go into the query if not skip_prompt and not query_yes_no("Continue?"): return ApModeDevice.Disconnect() print("Waiting for wifi rescan") time.sleep(30) try: var_io.writeSeenSSIDs(nm.seenSSIDs([ClientModeDevice])) except PermissionError: print("Error writing to /var/lib/pifi/seen_ssids, continuing") if ClientModeDevice.State == NetworkManager.NM_DEVICE_STATE_ACTIVATED: print( "Connected to: %s" % ClientModeDevice.SpecificDevice().ActiveAccessPoint.Ssid ) return print("Device is not connected to any network, Looking for pending connections") pending = var_io.readPendingConnections() # Try to pick a connection to use, if none found, just continue try: # Use the best connection best_ap, best_con = nm.selectConnection( nm.availibleConnections(ClientModeDevice, pending) ) print("Connecting to %s" % best_con["802-11-wireless"]["ssid"]) NetworkManager.NetworkManager.AddAndActivateConnection( best_con, ClientModeDevice, best_ap ) new_pending = var_io.readPendingConnections().remove(best_con) var_io.writePendingConnections(new_pending) return except ValueError: pass # If we reach this point, we gave up on Client mode print("No SSIDs from pending connections found") startup.start_ap_mode(pifi_conf_settings, ApModeDevice, ClientModeDevice)