checks if device supports ad-hoc before starting ad-hoc

Partly added AP support
This commit is contained in:
Pranav Jerry 2021-06-14 11:02:27 +05:30
parent 0fb8b6a8ab
commit 006cb1095c
Signed by: pranav
GPG Key ID: F1DCDC4FED0A0C5B
1 changed files with 49 additions and 21 deletions

View File

@ -57,30 +57,58 @@ try:
# Get list of all devices
print("Finding connected devices")
objects = iwd.GetManagedObjects()
devices = []
for name, obj in objects.items():
# devices that support ad-hoc
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
print("Found device:", obj["net.connman.iwd.Device"]["Name"])
devices.append(name)
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)
print(objects)
print(adhoc_devices)
print(ap_devices)
# Start ad-hoc on first device
devpath = devices.pop()
print("Working on first device", devpath)
dev1 = bus.get_proxy("net.connman.iwd", devpath)
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", devpath)
print("Starting ad-hoc network")
dev1.StartOpen(ADHOC_SSID)
# TODO: If there is a second device, start AP
# in it
if len(adhoc_devices) != 0:
# Start ad-hoc on first device
dev1path = adhoc_devices.pop()
# Remove device from ap_devices if it exists there
if dev1path in ap_devices:
ap_devices.remove(dev1path)
print("Working on AP")
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)
print("Starting ad-hoc network")
dev1.StartOpen(ADHOC_SSID)
# Start Access point
if len(ap_devices) != 0:
print("Working on AP")
dev2path = ap_devices.pop()
dev2 = bus.get_proxy("net.connman.iwd", dev2path)
print("Starting AP on", dev2.Name)
if not dev1.Powered:
print("Device is off. Turning on")
dev1.Powered = True
# TODO: Start AP on dev2
except DBusError:
sys.exit("An error occured while communicating with iwd")