test-gatt-profile 3.4 KB

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