def test_availible_connections_no_aps(self): wi_dev = mock.MagicMock(**{'GetAccessPoints.return_value': list()}) dev = mock.MagicMock(**{'SpecificDevice.return_value': wi_dev}) generator = nm_helper.availibleConnections(dev, list()) with self.assertRaises(StopIteration): next(generator)
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 test_availible_connections_no_matching_connections(self): ap1 = mock.MagicMock(**{'Ssid': 'Foo'}) ap2 = mock.MagicMock(**{'Ssid': 'Bar'}) wi_dev = mock.MagicMock(**{'GetAccessPoints.return_value': [ap1, ap2]}) dev = mock.MagicMock(**{'SpecificDevice.return_value': wi_dev}) cons = [{ '802-11-wireless': { 'ssid': 'Baz' } }, { '802-11-wireless': { 'ssid': 'Qux' } }] generator = nm_helper.availibleConnections(dev, cons) with self.assertRaises(StopIteration): next(generator)
def test_availible_connections_multiple_connections(self): ap1 = mock.MagicMock(**{'Ssid': 'Foo'}) ap2 = mock.MagicMock(**{'Ssid': 'Bar'}) ap3 = mock.MagicMock(**{'Ssid': 'Baz'}) wi_dev = mock.MagicMock( **{'GetAccessPoints.return_value': [ap1, ap2, ap3]}) dev = mock.MagicMock(**{'SpecificDevice.return_value': wi_dev}) cons = [{ '802-11-wireless': { 'ssid': 'Baz' } }, { '802-11-wireless': { 'ssid': 'Foo' } }] # Use list and assertIn beacuse we don't care about order output = list(nm_helper.availibleConnections(dev, cons)) self.assertIn((ap3, cons[0]), output) self.assertIn((ap1, cons[1]), output) self.assertEqual(len(output), 2)
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)