aboutsummaryrefslogtreecommitdiff
path: root/parrot_zik/bluezutils.py
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-05-05 12:54:03 +0200
committerneodarz <neodarz@neodarz.net>2019-05-05 12:57:20 +0200
commit2cfb8fac238222d3b67349539846905ec812962c (patch)
treec93133875832183d18fd2b7042d7dc06f211a5c4 /parrot_zik/bluezutils.py
parentcce51d8a961cb5b6af21cd44b357defc39e1b008 (diff)
downloadpyParrotZikTCP-2cfb8fac238222d3b67349539846905ec812962c.tar.xz
pyParrotZikTCP-2cfb8fac238222d3b67349539846905ec812962c.zip
Implement bluez python code management directly in project
It's directly implemented because this scipt isn't in Archlinux distribution. Because it's python code I prefere to have it here.
Diffstat (limited to 'parrot_zik/bluezutils.py')
-rw-r--r--parrot_zik/bluezutils.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/parrot_zik/bluezutils.py b/parrot_zik/bluezutils.py
new file mode 100644
index 0000000..6f49f2b
--- /dev/null
+++ b/parrot_zik/bluezutils.py
@@ -0,0 +1,48 @@
+import dbus
+
+SERVICE_NAME = "org.bluez"
+ADAPTER_INTERFACE = SERVICE_NAME + ".Adapter1"
+DEVICE_INTERFACE = SERVICE_NAME + ".Device1"
+
+def get_managed_objects():
+ bus = dbus.SystemBus()
+ manager = dbus.Interface(bus.get_object("org.bluez", "/"),
+ "org.freedesktop.DBus.ObjectManager")
+ return manager.GetManagedObjects()
+
+def find_adapter(pattern=None):
+ return find_adapter_in_objects(get_managed_objects(), pattern)
+
+def find_adapter_in_objects(objects, pattern=None):
+ bus = dbus.SystemBus()
+ for path, ifaces in objects.items():
+ adapter = ifaces.get(ADAPTER_INTERFACE)
+ if adapter is None:
+ continue
+ if not pattern or pattern == adapter["Address"] or \
+ path.endswith(pattern):
+ obj = bus.get_object(SERVICE_NAME, path)
+ return dbus.Interface(obj, ADAPTER_INTERFACE)
+ raise Exception("Bluetooth adapter not found")
+
+def find_device(device_address, adapter_pattern=None):
+ return find_device_in_objects(get_managed_objects(), device_address,
+ adapter_pattern)
+
+def find_device_in_objects(objects, device_address, adapter_pattern=None):
+ bus = dbus.SystemBus()
+ path_prefix = ""
+ if adapter_pattern:
+ adapter = find_adapter_in_objects(objects, adapter_pattern)
+ path_prefix = adapter.object_path
+ for path, ifaces in objects.items():
+ device = ifaces.get(DEVICE_INTERFACE)
+ if device is None:
+ continue
+ if (device["Address"] == device_address and
+ path.startswith(path_prefix)):
+ obj = bus.get_object(SERVICE_NAME, path)
+ return dbus.Interface(obj, DEVICE_INTERFACE)
+
+ raise Exception("Bluetooth device not found")
+