added new functions

This commit is contained in:
Pranav Jerry 2021-07-12 20:17:33 +05:30
parent 0e7a69be53
commit 8bf05ba334
Signed by: pranav
GPG Key ID: F1DCDC4FED0A0C5B
2 changed files with 49 additions and 7 deletions

View File

@ -26,7 +26,9 @@ class IWD:
self._proxy = self._bus.get_proxy(IWD_BUS, IWD_ROOT_PATH)
def get_name_from_path(self, path: str) -> str:
"""returns device or adapter 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
@ -37,7 +39,7 @@ class IWD:
proxy = self._bus.get_proxy(IWD_BUS, i)
if proxy.Name == name:
return i
# If no devices were found, return None
# If no devices were found, return None
return None
def get_adapter_path_from_name(self, name: str) -> str:
@ -47,7 +49,7 @@ class IWD:
proxy = self._bus.get_proxy(IWD_BUS, i)
if proxy.Name == name:
return i
# If no adapters were found
# If no adapters were found
return None
def get_all_device_paths(self) -> list:
@ -101,7 +103,7 @@ class IWD:
class Device:
"""
control devices with iwd
name: name of device
name: name of device (str)
adapter: name of adapter (str)
"""
@ -114,10 +116,42 @@ class Device:
adapter_path = self._proxy.Adapter
self.adapter = self._iwd.get_name_from_path(adapter_path)
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()
def reload(self):
"""reload the proxy after changing mode"""
self._proxy = self._bus.get_proxy(IWD_BUS, self._path)
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
def get_mode(self) -> str:
"""
returns the mode in which the device is in
@ -130,7 +164,15 @@ class Device:
self._proxy.Mode = mode
self.reload()
def start_adhoc_open(self, name):
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
"""
if self.get_mode() != "ad-hoc":
self.set_mode("ad-hoc")
# TODO: connect to network
if not self.is_powered_on():
self.power_on()
self._proxy.StartOpen(name)

View File

@ -5,7 +5,7 @@ Setup a working BATMAN Advanced network
with systemd-networkd and iwd
"""
# Copyright (C) 2021 The Authors
# Copyright (C) 2021 The naxalnet Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by