From f0689016ce891918271528ccec978b4364c6a45d Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Mon, 19 Jul 2021 14:11:43 +0530 Subject: [PATCH] ad-hoc part done Check out naxalnet/naxalnet.py --- naxalnet/iwd.py | 44 ++++++++++++++++++++--- naxalnet/main.py | 16 --------- naxalnet/naxalnet.py | 86 ++++++++++++-------------------------------- 3 files changed, 62 insertions(+), 84 deletions(-) delete mode 100644 naxalnet/main.py diff --git a/naxalnet/iwd.py b/naxalnet/iwd.py index 3feb57e..1578572 100644 --- a/naxalnet/iwd.py +++ b/naxalnet/iwd.py @@ -27,6 +27,7 @@ IWD_ADAPTER_INTERFACE = "net.connman.iwd.Adapter" # If you are new to D-Bus, you might want to use a program # such as D-Feet (https://wiki.gnome.org/Apps/DFeet) for reference. +# And try out iwctl to understand iwd's bus objects class IWD: @@ -128,9 +129,6 @@ class Device: self._bus = self._iwd._bus self._path = self._iwd.get_device_path_from_name(name) self.reload() - self.name = self._proxy.Name - adapter_path = self._proxy.Adapter - self.adapter = self._iwd.get_name_from_path(adapter_path) def __str__(self): return self.name @@ -152,6 +150,10 @@ class Device: def reload(self): """reload the proxy after changing mode""" self._proxy = self._bus.get_proxy(IWD_BUS, self._path) + self.name = self._proxy.Name + adapter_path = self._proxy.Adapter + # name of adapter ('phy0' for example) + self.adapter = self._iwd.get_name_from_path(adapter_path) def is_adhoc_started(self): """ @@ -194,10 +196,18 @@ class Device: if not self.is_powered_on(): self.power_on() + if self.is_adhoc_started(): + self.stop_adhoc() + self._proxy.StartOpen(name) + def stop_adhoc(self): + """stop adhoc if adhoc is started""" + if self.is_adhoc_started(): + self._proxy.Stop() + self.reload() + -# TODO: add power_on() and power_off() class Adapter: """represents an adapter as a python object""" @@ -208,6 +218,32 @@ class Adapter: # Initialise self._proxy self.reload() + def __str__(self): + return self.name + def reload(self): """reload the proxy after changing mode""" self._proxy = self._bus.get_proxy(IWD_BUS, self._path) + self.name = self._proxy.Name + self.supported_modes = self._proxy.SupportedModes + self.model = self._proxy.Model + self.vendor = self._proxy.Vendor + + def is_powered_on(self) -> bool: + """returns True if adapter is powered on, False otherwise""" + return self._proxy.Powered + + def power_on(self): + """power on the adapter""" + self._proxy.Powered = True + + def power_off(self): + """power off the adapter""" + self._proxy.Powered = False + + def supports_mode(self, mode: str) -> bool: + """ + Returns True if the adapter supports the mode + mode can be "ad-hoc", "ap" or "station" + """ + return mode in self.supported_modes diff --git a/naxalnet/main.py b/naxalnet/main.py deleted file mode 100644 index 0051bf2..0000000 --- a/naxalnet/main.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python3 - -# This file should import libraries and do -# what /usr/bin/naxalnet used to do - -# TODO: change to naxalnet.iwd -from iwd import IWD, Device - -iwd = IWD() -devices = iwd.get_devices() - -print("Devices:", devices) - -for i in devices: - dev = Device(i) - print(dev) diff --git a/naxalnet/naxalnet.py b/naxalnet/naxalnet.py index 379b630..25045f0 100755 --- a/naxalnet/naxalnet.py +++ b/naxalnet/naxalnet.py @@ -23,8 +23,10 @@ with systemd-networkd and iwd import sys from pathlib import Path from shutil import copy -from dasbus.connection import SystemMessageBus + +# from dasbus.connection import SystemMessageBus from dasbus.error import DBusError +from iwd import IWD, Device, Adapter NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd" NETWORKD_VOLATILE_DIR = "/run/systemd/network" @@ -35,6 +37,7 @@ AP_PASSWD = "naxalnet256" # The D-Bus API does not support creating new interfaces # or linking to bridges. So we use config files. # See man:systemd.network(5) + try: print("Copying network config files") dest = Path(NETWORKD_VOLATILE_DIR) @@ -52,82 +55,37 @@ except PermissionError as error: # Now, the iwd part try: - # connect to the System bus - bus = SystemMessageBus() - # iwd proxy - iwd = bus.get_proxy("net.connman.iwd", "/") - # Get list of all devices - print("Finding connected devices") - objects = iwd.GetManagedObjects() - # devices that support ad-hoc + iwd = IWD() + devices = iwd.get_devices() adhoc_devices = [] - # devices that support ap ap_devices = [] - for path, obj in objects.items(): - if "net.connman.iwd.Device" in obj: - # add all devices to the list - name = obj["net.connman.iwd.Device"]["Name"] - print("Found device", name) - adapter_path = obj["net.connman.iwd.Device"]["Adapter"].get_string() - adapter = objects[adapter_path]["net.connman.iwd.Adapter"] - if "ad-hoc" in adapter["SupportedModes"]: - print(name, "supports ad-hoc") - adhoc_devices.append(path) - if "ap" in adapter["SupportedModes"]: - print(name, "supports ap") - ap_devices.append(path) + + for i in devices: + d = Device(i) + a = Adapter(d.adapter) + if a.supports_mode("ad-hoc"): + adhoc_devices.append(i) + if a.supports_mode("ap"): + ap_devices.append(i) if len(adhoc_devices) != 0: # Start ad-hoc on first device supporting ad-hoc - dev1path = adhoc_devices.pop() + adhoc_device = Device(adhoc_devices.pop()) # The same device is likely to have ap support too. # But we can't start ad-hoc and ap on the same interface. # Remove dev1 from ap_devices if it exists there - if dev1path in ap_devices: - ap_devices.remove(dev1path) + if adhoc_device.name in ap_devices: + ap_devices.remove(adhoc_device.name) print("Working on ad-hoc") - dev1 = bus.get_proxy("net.connman.iwd", dev1path) - print("Starting ad-hoc on", dev1.Name) - if not dev1.Powered: - print("Device is off. Turning on") - dev1.Powered = True - if dev1.Mode != "ad-hoc": - print("Device is in", dev1.Mode) - print("Switching to ad-hoc") - dev1.Mode = "ad-hoc" - # Changing Mode needs connecting to the proxy again - dev1 = bus.get_proxy("net.connman.iwd", dev1path) - # If already connected to ad-hoc, stop it - if dev1.Started is True: - print("Already connected to ad-hoc. Stopping") - dev1.Stop() - # Reconnect to proxy or StartOpen won't work - dev1 = bus.get_proxy("net.connman.iwd", dev1path) - print("Starting ad-hoc network") - dev1.StartOpen(ADHOC_SSID) - + adhoc_device.start_adhoc_open(ADHOC_SSID) # Start Access point if ap_device is not empty, # ie, we have more devices if len(ap_devices) != 0: print("Working on AP") - dev2path = ap_devices.pop() - dev2 = bus.get_proxy("net.connman.iwd", dev2path) - if not dev1.Powered: - print("Device is off. Turning on") - dev1.Powered = True - if dev2.Mode != "ap": - print(dev2.Name, "is in", dev2.Mode) - print("Switching to ap") - dev2.Mode = "ap" - dev2 = bus.get_proxy("net.connman.iwd", dev2path) - if dev2.Started is True: - print("An AP is already started on", dev2.Name) - print("Stopping") - dev2.Stop() - dev2 = bus.get_proxy("net.connman.iwd", dev2path) - print("Starting AP on", dev2.Name) - dev2.Start(AP_SSID, AP_PASSWD) -except DBusError: + ap_device = ap_devices.pop() + # _TODO: start ap after implementing in in iwd.py +except DBusError as e: + print(e) sys.exit("An error occured while communicating with iwd") print("Bye")