Debian Codegraph @

test/test-gatt-profile

raw symbols SCIP
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from __future__ import absolute_import, print_function, unicode_literals
5
6 from optparse import OptionParser, make_option
7 import os
8 import sys
9 import uuid
10 import dbus
11 import dbus.service
12 import dbus.mainloop.glib
13 try:
14 from gi.repository import GObject
15 except ImportError:
16 import gobject as GObject
17 import bluezutils
18
19 BLUEZ_SERVICE_NAME = 'org.bluez'
20 GATT_MANAGER_IFACE = 'org.bluez.GattManager1'
21 DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager'
22 DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties'
23
24 GATT_PROFILE_IFACE = 'org.bluez.GattProfile1'
25
26
27 class InvalidArgsException(dbus.exceptions.DBusException):
28 _dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs'
29
30
31 class Application(dbus.service.Object):
32 def __init__(self, bus):
33 self.path = '/'
34 self.profiles = []
35 dbus.service.Object.__init__(self, bus, self.path)
36
37 def get_path(self):
38 return dbus.ObjectPath(self.path)
39
40 def add_profile(self, profile):
41 self.profiles.append(profile)
42
43 @dbus.service.method(DBUS_OM_IFACE, out_signature='a{oa{sa{sv}}}')
44 def GetManagedObjects(self):
45 response = {}
46 print('GetManagedObjects')
47
48 for profile in self.profiles:
49 response[profile.get_path()] = profile.get_properties()
50
51 return response
52
53
54 class Profile(dbus.service.Object):
55 PATH_BASE = '/org/bluez/example/profile'
56
57 def __init__(self, bus, uuids):
58 self.path = self.PATH_BASE
59 self.bus = bus
60 self.uuids = uuids
61 dbus.service.Object.__init__(self, bus, self.path)
62
63 def get_properties(self):
64 return {
65 GATT_PROFILE_IFACE: {
66 'UUIDs': self.uuids,
67 }
68 }
69
70 def get_path(self):
71 return dbus.ObjectPath(self.path)
72
73 @dbus.service.method(GATT_PROFILE_IFACE,
74 in_signature="",
75 out_signature="")
76 def Release(self):
77 print("Release")
78 mainloop.quit()
79
80 @dbus.service.method(DBUS_PROP_IFACE,
81 in_signature='s',
82 out_signature='a{sv}')
83 def GetAll(self, interface):
84 if interface != GATT_PROFILE_IFACE:
85 raise InvalidArgsException()
86
87 return self.get_properties[GATT_PROFILE_IFACE]
88
89
90 def register_app_cb():
91 print('GATT application registered')
92
93
94 def register_app_error_cb(error):
95 print('Failed to register application: ' + str(error))
96 mainloop.quit()
97
98 if __name__ == '__main__':
99 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
100
101 bus = dbus.SystemBus()
102
103 path = bluezutils.find_adapter().object_path
104
105 manager = dbus.Interface(bus.get_object("org.bluez", path),
106 GATT_MANAGER_IFACE)
107
108 option_list = [make_option("-u", "--uuid", action="store",
109 type="string", dest="uuid",
110 default=None),
111 ]
112
113 opts = dbus.Dictionary({}, signature='sv')
114
115 parser = OptionParser(option_list=option_list)
116
117 (options, args) = parser.parse_args()
118
119 mainloop = GObject.MainLoop()
120
121 if not options.uuid:
122 options.uuid = str(uuid.uuid4())
123
124 app = Application(bus)
125 profile = Profile(bus, [options.uuid])
126 app.add_profile(profile)
127 manager.RegisterApplication(app.get_path(), {},
128 reply_handler=register_app_cb,
129 error_handler=register_app_error_cb)
130
131 mainloop.run()