Blame test/test-gatt-profile

Packit 34410b
#!/usr/bin/python
Packit 34410b
Packit 34410b
from __future__ import absolute_import, print_function, unicode_literals
Packit 34410b
Packit 34410b
from optparse import OptionParser, make_option
Packit 34410b
import os
Packit 34410b
import sys
Packit 34410b
import uuid
Packit 34410b
import dbus
Packit 34410b
import dbus.service
Packit 34410b
import dbus.mainloop.glib
Packit 34410b
try:
Packit 34410b
  from gi.repository import GObject
Packit 34410b
except ImportError:
Packit 34410b
  import gobject as GObject
Packit 34410b
import bluezutils
Packit 34410b
Packit 34410b
BLUEZ_SERVICE_NAME = 'org.bluez'
Packit 34410b
GATT_MANAGER_IFACE = 'org.bluez.GattManager1'
Packit 34410b
DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager'
Packit 34410b
DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
Packit 34410b
Packit 34410b
GATT_PROFILE_IFACE = 'org.bluez.GattProfile1'
Packit 34410b
Packit 34410b
Packit 34410b
class InvalidArgsException(dbus.exceptions.DBusException):
Packit 34410b
    _dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs'
Packit 34410b
Packit 34410b
Packit 34410b
class Application(dbus.service.Object):
Packit 34410b
    def __init__(self, bus):
Packit 34410b
        self.path = '/'
Packit 34410b
        self.profiles = []
Packit 34410b
        dbus.service.Object.__init__(self, bus, self.path)
Packit 34410b
Packit 34410b
    def get_path(self):
Packit 34410b
        return dbus.ObjectPath(self.path)
Packit 34410b
Packit 34410b
    def add_profile(self, profile):
Packit 34410b
        self.profiles.append(profile)
Packit 34410b
Packit 34410b
    @dbus.service.method(DBUS_OM_IFACE, out_signature='a{oa{sa{sv}}}')
Packit 34410b
    def GetManagedObjects(self):
Packit 34410b
        response = {}
Packit 34410b
        print('GetManagedObjects')
Packit 34410b
Packit 34410b
        for profile in self.profiles:
Packit 34410b
            response[profile.get_path()] = profile.get_properties()
Packit 34410b
Packit 34410b
        return response
Packit 34410b
Packit 34410b
Packit 34410b
class Profile(dbus.service.Object):
Packit 34410b
    PATH_BASE = '/org/bluez/example/profile'
Packit 34410b
Packit 34410b
    def __init__(self, bus, uuids):
Packit 34410b
        self.path = self.PATH_BASE
Packit 34410b
        self.bus = bus
Packit 34410b
        self.uuids = uuids
Packit 34410b
        dbus.service.Object.__init__(self, bus, self.path)
Packit 34410b
Packit 34410b
    def get_properties(self):
Packit 34410b
        return {
Packit 34410b
            GATT_PROFILE_IFACE: {
Packit 34410b
                'UUIDs': self.uuids,
Packit 34410b
            }
Packit 34410b
        }
Packit 34410b
Packit 34410b
    def get_path(self):
Packit 34410b
        return dbus.ObjectPath(self.path)
Packit 34410b
Packit 34410b
    @dbus.service.method(GATT_PROFILE_IFACE,
Packit 34410b
                        in_signature="",
Packit 34410b
                        out_signature="")
Packit 34410b
    def Release(self):
Packit 34410b
        print("Release")
Packit 34410b
        mainloop.quit()
Packit 34410b
Packit 34410b
    @dbus.service.method(DBUS_PROP_IFACE,
Packit 34410b
                         in_signature='s',
Packit 34410b
                         out_signature='a{sv}')
Packit 34410b
    def GetAll(self, interface):
Packit 34410b
        if interface != GATT_PROFILE_IFACE:
Packit 34410b
            raise InvalidArgsException()
Packit 34410b
Packit 34410b
        return self.get_properties[GATT_PROFILE_IFACE]
Packit 34410b
Packit 34410b
Packit 34410b
def register_app_cb():
Packit 34410b
    print('GATT application registered')
Packit 34410b
Packit 34410b
Packit 34410b
def register_app_error_cb(error):
Packit 34410b
    print('Failed to register application: ' + str(error))
Packit 34410b
    mainloop.quit()
Packit 34410b
Packit 34410b
if __name__ == '__main__':
Packit 34410b
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Packit 34410b
Packit 34410b
    bus = dbus.SystemBus()
Packit 34410b
Packit 34410b
    path = bluezutils.find_adapter().object_path
Packit 34410b
Packit 34410b
    manager = dbus.Interface(bus.get_object("org.bluez", path),
Packit 34410b
                            GATT_MANAGER_IFACE)
Packit 34410b
Packit 34410b
    option_list = [make_option("-u", "--uuid", action="store",
Packit 34410b
                                type="string", dest="uuid",
Packit 34410b
                                default=None),
Packit 34410b
    ]
Packit 34410b
Packit 34410b
    opts = dbus.Dictionary({}, signature='sv')
Packit 34410b
Packit 34410b
    parser = OptionParser(option_list=option_list)
Packit 34410b
Packit 34410b
    (options, args) = parser.parse_args()
Packit 34410b
Packit 34410b
    mainloop = GObject.MainLoop()
Packit 34410b
Packit 34410b
    if not options.uuid:
Packit 34410b
        options.uuid = str(uuid.uuid4())
Packit 34410b
Packit 34410b
    app = Application(bus)
Packit 34410b
    profile = Profile(bus, [options.uuid])
Packit 34410b
    app.add_profile(profile)
Packit 34410b
    manager.RegisterApplication(app.get_path(), {},
Packit 34410b
                                reply_handler=register_app_cb,
Packit 34410b
                                error_handler=register_app_error_cb)
Packit 34410b
Packit 34410b
    mainloop.run()