Example #1
0
 def test_incomplete_conf(self):
     f = mock.mock_open(read_data=yaml.dump({'Foo': 'Bar'}))
     conf = etc_io.get_conf(open=f)
     self.assertIsInstance(conf, dict)
     expected = etc_io.default_conf
     expected.update({'Foo': 'Bar'})
     self.assertEqual(conf, expected)
Example #2
0
 def test_good_conf(self):
     f = mock.mock_open(
         read_data=yaml.dump({'delete_existing_ap_connections': True}))
     conf = etc_io.get_conf(open=f)
     self.assertIsInstance(conf, dict)
     expected = etc_io.default_conf
     expected.update({'delete_existing_ap_connections': True})
     self.assertEqual(conf, expected)
Example #3
0
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)
Example #4
0
File: pifi.py Project: ihelal/pifi
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)
Example #5
0
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)
Example #6
0
 def test_bad_conf(self):
     f = mock.mock_open(read_data=': fd')
     conf = etc_io.get_conf(open=f)
     self.assertIsNot(conf, None)
     self.assertIsInstance(conf, dict)
     self.assertEqual(conf, etc_io.default_conf)
Example #7
0
 def test_nonexistant_conf(self):
     f = mock.Mock(side_effect=FileNotFoundError('foo'))
     conf = etc_io.get_conf(open=f)
     self.assertIsInstance(conf, dict)