-
-
Save nhooyr/b76525ba1d527a23e3da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from __future__ import absolute_import, print_function, unicode_literals | |
from optparse import OptionParser, make_option | |
import sys | |
import time | |
import dbus | |
import signal | |
SERVICE_NAME = "org.bluez" | |
ADAPTER_INTERFACE = SERVICE_NAME + ".Adapter1" | |
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_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 signal_handler(signal, frame): | |
print("Terminating connection") | |
sys.stdout.flush() | |
try: | |
server.Unregister(service) | |
except: | |
pass | |
sys.exit(0) | |
bus = dbus.SystemBus() | |
option_list = [ | |
make_option("-i", "--device", action="store", | |
type="string", dest="dev_id"), | |
make_option("-b", "--bridge", action="store", | |
type="string", dest="bridge"), | |
] | |
parser = OptionParser(option_list=option_list) | |
(options, args) = parser.parse_args() | |
adapter_path = find_adapter_in_objects(get_managed_objects(), options.dev_id).object_path | |
server = dbus.Interface(bus.get_object("org.bluez", adapter_path), "org.bluez.NetworkServer1") | |
service = "nap" | |
server.Register(service, options.bridge) | |
print("Server for %s registered for %s" % (service, options.bridge)) | |
sys.stdout.flush() | |
signal.signal(signal.SIGTERM, signal_handler) | |
signal.pause() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment