Now supports AP!

Fixed error when already connected to ad-hoc or ap.
Removed some print() lines I had forgot to remove after debugging.
But we can't close #1 yet (need to update the README)
This commit is contained in:
Pranav Jerry 2021-06-15 14:00:48 +05:30
parent 5e8ff6b65e
commit 79b9a161f1
Signed by: pranav
GPG Key ID: F1DCDC4FED0A0C5B
2 changed files with 35 additions and 28 deletions

View File

@ -30,9 +30,12 @@ from dasbus.error import DBusError
NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd"
NETWORKD_VOLATILE_DIR = "/run/systemd/network"
ADHOC_SSID = "HelloWorld"
AP_SSID = "NaxalNet"
AP_PASSWD = "naxalnet256"
# Copy networkd configs to volatile dir.
# See man:systemd.networkm(5)
# 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)
@ -65,7 +68,7 @@ try:
if "net.connman.iwd.Device" in obj:
# add all devices to the list
name = obj["net.connman.iwd.Device"]["Name"]
print("Found 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"]:
@ -74,17 +77,16 @@ try:
if "ap" in adapter["SupportedModes"]:
print(name, "supports ap")
ap_devices.append(path)
print(objects)
print(adhoc_devices)
print(ap_devices)
if len(adhoc_devices) != 0:
# Start ad-hoc on first device
# Start ad-hoc on first device supporting ad-hoc
dev1path = adhoc_devices.pop()
# Remove device from ap_devices if it exists there
# 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)
print("Working on AP")
print("Working on ad-hoc")
dev1 = bus.get_proxy("net.connman.iwd", dev1path)
print("Starting ad-hoc on", dev1.Name)
if not dev1.Powered:
@ -96,36 +98,37 @@ try:
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)
# Start Access point
# 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)
print("Starting AP on", dev2.Name)
if not dev1.Powered:
print("Device is off. Turning on")
dev1.Powered = True
# TODO: Start AP on dev2
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:
sys.exit("An error occured while communicating with iwd")
# Sleep my little baby-oh
# Sleep until you waken
# When you wake you'll see the world
# If I'm not mistaken...
#
# Kiss a lover
# Dance a measure,
# Find your name
# And buried treasure...
#
# Face your life
# Its pain,
# Its pleasure,
# Leave no path untaken.
#
# -- Neil Gaiman, The Graveyard Book
print("Bye")

View File

@ -17,6 +17,10 @@ Type=oneshot
RemainAfterExit=yes
Restart=on-failure
RestartSec=5sec
# IWD takes some time to find devices.
# Without the sleep 5, naxalnet could not detect the second
# device while testing.
ExecStartPre=/usr/bin/sleep 5
ExecStart=/usr/bin/naxalnet
[Install]