mirror of
https://git.disroot.org/pranav/pybatmesh.git
synced 2024-11-27 01:02:15 +05:30
basic implementaion of class Device and IWD
When work is finished, we can use them to start adhoc or ap
This commit is contained in:
parent
f4a490210d
commit
e4fd0ff04d
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
.DS_Store
|
||||
.idea
|
||||
*.log
|
||||
tmp/
|
||||
|
||||
*.py[cod]
|
||||
*.egg
|
||||
build
|
||||
htmlcov
|
||||
__pycache__
|
@ -6,6 +6,8 @@ from dasbus.connection import SystemMessageBus
|
||||
|
||||
IWD_BUS = "net.connman.iwd"
|
||||
IWD_ROOT_PATH = "/"
|
||||
IWD_DEVICE_INTERFACE = "net.connman.iwd.Device"
|
||||
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.
|
||||
@ -19,34 +21,74 @@ class IWD:
|
||||
self._proxy = self._bus.get_proxy(IWD_BUS, IWD_ROOT_PATH)
|
||||
|
||||
def get_name_from_path(self, path: str) -> str:
|
||||
"""returns device name when d-bus path is given as arg"""
|
||||
"""returns device or adapter name when d-bus path is given as arg"""
|
||||
proxy = self._bus.get_proxy(IWD_BUS, path)
|
||||
return proxy.Name
|
||||
|
||||
def get_device_path_from_name(self, name: str) -> str:
|
||||
"""returns path of device as str"""
|
||||
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
|
||||
return None
|
||||
|
||||
def get_adapter_path_from_name(self, name: str) -> str:
|
||||
"""returns path of adapter as str"""
|
||||
|
||||
def get_all_object_paths(self) -> list:
|
||||
"""returns a list of path names of all objects"""
|
||||
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
|
||||
return None
|
||||
|
||||
def get_all_device_paths(self) -> list:
|
||||
"""returns list of paths of all devices"""
|
||||
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
|
||||
|
||||
def get_all_adapter_paths(self) -> list:
|
||||
"""returns list of paths of all adapters"""
|
||||
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
|
||||
|
||||
def get_devices(self) -> list:
|
||||
"""
|
||||
returns list of device names as str
|
||||
example: ["wlan0", "wlan1"]
|
||||
"""
|
||||
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
|
||||
|
||||
def get_adapters(self) -> list:
|
||||
"""
|
||||
returns list of adapters
|
||||
example: ["phy0","phy1"]
|
||||
"""
|
||||
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
|
||||
|
||||
|
||||
class Device:
|
||||
@ -55,9 +97,11 @@ class Device:
|
||||
def __init__(self, name: str, bus=SystemMessageBus()):
|
||||
self._iwd = IWD(bus)
|
||||
self._bus = self._iwd._bus
|
||||
self._path = self._iwd.get_path_from_name(name)
|
||||
self._path = self._iwd.get_device_path_from_name(name)
|
||||
self._proxy = self._bus.get_proxy(IWD_BUS, self._path)
|
||||
self.name = self._proxy.Name
|
||||
adapter_path = self._proxy.Adapter
|
||||
self.adapter = self._iwd.get_name_from_path(adapter_path)
|
||||
|
||||
def get_mode(self) -> str:
|
||||
"""
|
||||
@ -65,6 +109,3 @@ class Device:
|
||||
example: "ap"
|
||||
"""
|
||||
return self._proxy.Mode
|
||||
|
||||
def get_adapter_name(self) -> str:
|
||||
"""returns the name of the adapter as str"""
|
||||
|
Loading…
Reference in New Issue
Block a user