2021-06-27 14:25:36 +05:30
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""Manage wifi adapter via iwd D-Bus api"""
|
|
|
|
|
2021-06-30 21:50:39 +05:30
|
|
|
from dasbus.connection import SystemMessageBus
|
|
|
|
|
|
|
|
IWD_BUS = "net.connman.iwd"
|
|
|
|
IWD_ROOT_PATH = "/"
|
2021-07-01 12:18:44 +05:30
|
|
|
IWD_DEVICE_INTERFACE = "net.connman.iwd.Device"
|
|
|
|
IWD_ADAPTER_INTERFACE = "net.connman.iwd.Adapter"
|
2021-06-30 21:50:39 +05:30
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2021-06-27 14:25:36 +05:30
|
|
|
|
|
|
|
class IWD:
|
|
|
|
"""Manage iwd via dbus"""
|
|
|
|
|
2021-06-30 21:50:39 +05:30
|
|
|
def __init__(self, bus=SystemMessageBus()):
|
2021-07-07 14:08:05 +05:30
|
|
|
# self._bus and self._proxy are meant for use only in this file
|
2021-06-30 21:50:39 +05:30
|
|
|
self._bus = bus
|
2021-07-07 14:08:05 +05:30
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
"""reload the proxy"""
|
2021-06-30 21:50:39 +05:30
|
|
|
self._proxy = self._bus.get_proxy(IWD_BUS, IWD_ROOT_PATH)
|
|
|
|
|
|
|
|
def get_name_from_path(self, path: str) -> str:
|
2021-07-12 20:17:33 +05:30
|
|
|
"""
|
|
|
|
returns device or adapter name when d-bus path is given as arg
|
|
|
|
"""
|
2021-07-01 12:18:44 +05:30
|
|
|
proxy = self._bus.get_proxy(IWD_BUS, path)
|
|
|
|
return proxy.Name
|
2021-06-30 21:50:39 +05:30
|
|
|
|
|
|
|
def get_device_path_from_name(self, name: str) -> str:
|
|
|
|
"""returns path of device as str"""
|
2021-07-01 12:18:44 +05:30
|
|
|
device_paths = self.get_all_device_paths()
|
|
|
|
for i in device_paths:
|
|
|
|
proxy = self._bus.get_proxy(IWD_BUS, i)
|
|
|
|
if proxy.Name == name:
|
|
|
|
return i
|
2021-07-12 20:17:33 +05:30
|
|
|
# If no devices were found, return None
|
2021-07-01 12:18:44 +05:30
|
|
|
return None
|
2021-06-30 21:50:39 +05:30
|
|
|
|
|
|
|
def get_adapter_path_from_name(self, name: str) -> str:
|
|
|
|
"""returns path of adapter as str"""
|
2021-07-01 12:18:44 +05:30
|
|
|
adapter_paths = self.get_all_adapter_paths()
|
|
|
|
for i in adapter_paths:
|
|
|
|
proxy = self._bus.get_proxy(IWD_BUS, i)
|
|
|
|
if proxy.Name == name:
|
|
|
|
return i
|
2021-07-12 20:17:33 +05:30
|
|
|
# If no adapters were found
|
2021-07-01 12:18:44 +05:30
|
|
|
return None
|
2021-06-27 14:25:36 +05:30
|
|
|
|
2021-06-30 21:50:39 +05:30
|
|
|
def get_all_device_paths(self) -> list:
|
|
|
|
"""returns list of paths of all devices"""
|
2021-07-01 12:18:44 +05:30
|
|
|
objects = self._proxy.GetManagedObjects()
|
|
|
|
paths = []
|
|
|
|
for key, value in objects.items():
|
|
|
|
# if value is a device, add its path to paths
|
|
|
|
if IWD_DEVICE_INTERFACE in value:
|
|
|
|
paths.append(key)
|
|
|
|
return paths
|
2021-06-30 21:50:39 +05:30
|
|
|
|
|
|
|
def get_all_adapter_paths(self) -> list:
|
|
|
|
"""returns list of paths of all adapters"""
|
2021-07-01 12:18:44 +05:30
|
|
|
objects = self._proxy.GetManagedObjects()
|
|
|
|
paths = []
|
|
|
|
|
|
|
|
for key, value in objects.items():
|
|
|
|
# if value is an adapter, add its path to paths
|
|
|
|
if IWD_ADAPTER_INTERFACE in value:
|
|
|
|
paths.append(key)
|
|
|
|
return paths
|
2021-06-30 21:50:39 +05:30
|
|
|
|
|
|
|
def get_devices(self) -> list:
|
2021-06-27 14:25:36 +05:30
|
|
|
"""
|
|
|
|
returns list of device names as str
|
|
|
|
example: ["wlan0", "wlan1"]
|
|
|
|
"""
|
2021-07-01 12:18:44 +05:30
|
|
|
devices = []
|
|
|
|
device_paths = self.get_all_device_paths()
|
|
|
|
|
|
|
|
for i in device_paths:
|
|
|
|
name = self.get_name_from_path(i)
|
|
|
|
devices.append(name)
|
|
|
|
return devices
|
2021-06-27 14:25:36 +05:30
|
|
|
|
2021-06-30 21:50:39 +05:30
|
|
|
def get_adapters(self) -> list:
|
2021-06-27 14:25:36 +05:30
|
|
|
"""
|
|
|
|
returns list of adapters
|
|
|
|
example: ["phy0","phy1"]
|
|
|
|
"""
|
2021-07-01 12:18:44 +05:30
|
|
|
adapters = []
|
|
|
|
adapter_paths = self.get_all_adapter_paths()
|
|
|
|
|
|
|
|
for i in adapter_paths:
|
|
|
|
name = self.get_name_from_path(i)
|
|
|
|
adapters.append(name)
|
|
|
|
return adapters
|
2021-06-27 14:25:36 +05:30
|
|
|
|
|
|
|
|
|
|
|
class Device:
|
2021-07-07 14:08:05 +05:30
|
|
|
"""
|
|
|
|
control devices with iwd
|
2021-07-12 20:17:33 +05:30
|
|
|
name: name of device (str)
|
2021-07-07 14:08:05 +05:30
|
|
|
adapter: name of adapter (str)
|
|
|
|
"""
|
2021-06-27 14:25:36 +05:30
|
|
|
|
2021-06-30 21:50:39 +05:30
|
|
|
def __init__(self, name: str, bus=SystemMessageBus()):
|
|
|
|
self._iwd = IWD(bus)
|
|
|
|
self._bus = self._iwd._bus
|
2021-07-01 12:18:44 +05:30
|
|
|
self._path = self._iwd.get_device_path_from_name(name)
|
2021-07-07 14:08:05 +05:30
|
|
|
self.reload()
|
2021-06-30 21:50:39 +05:30
|
|
|
self.name = self._proxy.Name
|
2021-07-01 12:18:44 +05:30
|
|
|
adapter_path = self._proxy.Adapter
|
|
|
|
self.adapter = self._iwd.get_name_from_path(adapter_path)
|
2021-06-30 21:50:39 +05:30
|
|
|
|
2021-07-12 20:17:33 +05:30
|
|
|
def is_powered_on(self) -> bool:
|
|
|
|
"""returns True if devie is powered on"""
|
|
|
|
return self._proxy.Powered
|
|
|
|
|
|
|
|
def power_on(self):
|
|
|
|
"""Turn on the device and reload the proxy"""
|
|
|
|
self._proxy.Powered = True
|
|
|
|
self.reload()
|
|
|
|
|
|
|
|
def power_off(self):
|
|
|
|
"""Turn off the device and reload the proxy"""
|
|
|
|
self._proxy.Powered = False
|
|
|
|
self.reload()
|
|
|
|
|
2021-07-07 14:08:05 +05:30
|
|
|
def reload(self):
|
|
|
|
"""reload the proxy after changing mode"""
|
|
|
|
self._proxy = self._bus.get_proxy(IWD_BUS, self._path)
|
|
|
|
|
2021-07-12 20:17:33 +05:30
|
|
|
def is_adhoc_started(self):
|
|
|
|
"""
|
|
|
|
Returns True if an adhoc network is started on this device.
|
|
|
|
Returns None if device is not powered on or not in ad-hoc mode.
|
|
|
|
"""
|
|
|
|
if self.is_powered_on() and self.get_mode() == "ad-hoc":
|
|
|
|
return self._proxy.Started
|
|
|
|
# If above condition is not true, return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
def is_ap_started(self):
|
|
|
|
"""
|
|
|
|
Same as is_adhoc_started(), but for ap
|
|
|
|
"""
|
|
|
|
if self.is_powered_on() and self.get_mode == "ap":
|
|
|
|
return self._proxy.Started
|
|
|
|
return None
|
|
|
|
|
2021-06-30 21:50:39 +05:30
|
|
|
def get_mode(self) -> str:
|
|
|
|
"""
|
|
|
|
returns the mode in which the device is in
|
|
|
|
example: "ap"
|
|
|
|
"""
|
|
|
|
return self._proxy.Mode
|
2021-07-07 14:08:05 +05:30
|
|
|
|
|
|
|
def set_mode(self, mode: str):
|
|
|
|
"""change the device mode to mode"""
|
|
|
|
self._proxy.Mode = mode
|
|
|
|
self.reload()
|
|
|
|
|
2021-07-12 20:17:33 +05:30
|
|
|
def start_adhoc_open(self, name: str):
|
|
|
|
"""
|
|
|
|
Create ad-hoc network with name, changing mode to ad-hoc
|
|
|
|
if it isn't already on ad-hoc
|
|
|
|
"""
|
2021-07-07 14:08:05 +05:30
|
|
|
if self.get_mode() != "ad-hoc":
|
|
|
|
self.set_mode("ad-hoc")
|
2021-07-12 20:17:33 +05:30
|
|
|
|
|
|
|
if not self.is_powered_on():
|
|
|
|
self.power_on()
|
|
|
|
|
|
|
|
self._proxy.StartOpen(name)
|