test-profile 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. class Profile(dbus.service.Object):
  16. fd = -1
  17. @dbus.service.method("org.bluez.Profile1",
  18. in_signature="", out_signature="")
  19. def Release(self):
  20. print("Release")
  21. mainloop.quit()
  22. @dbus.service.method("org.bluez.Profile1",
  23. in_signature="", out_signature="")
  24. def Cancel(self):
  25. print("Cancel")
  26. @dbus.service.method("org.bluez.Profile1",
  27. in_signature="oha{sv}", out_signature="")
  28. def NewConnection(self, path, fd, properties):
  29. self.fd = fd.take()
  30. print("NewConnection(%s, %d)" % (path, self.fd))
  31. for key in properties.keys():
  32. if key == "Version" or key == "Features":
  33. print(" %s = 0x%04x" % (key, properties[key]))
  34. else:
  35. print(" %s = %s" % (key, properties[key]))
  36. @dbus.service.method("org.bluez.Profile1",
  37. in_signature="o", out_signature="")
  38. def RequestDisconnection(self, path):
  39. print("RequestDisconnection(%s)" % (path))
  40. if (self.fd > 0):
  41. os.close(self.fd)
  42. self.fd = -1
  43. if __name__ == '__main__':
  44. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  45. bus = dbus.SystemBus()
  46. manager = dbus.Interface(bus.get_object("org.bluez",
  47. "/org/bluez"), "org.bluez.ProfileManager1")
  48. option_list = [
  49. make_option("-u", "--uuid", action="store",
  50. type="string", dest="uuid",
  51. default=None),
  52. make_option("-p", "--path", action="store",
  53. type="string", dest="path",
  54. default="/foo/bar/profile"),
  55. make_option("-n", "--name", action="store",
  56. type="string", dest="name",
  57. default=None),
  58. make_option("-s", "--server",
  59. action="store_const",
  60. const="server", dest="role"),
  61. make_option("-c", "--client",
  62. action="store_const",
  63. const="client", dest="role"),
  64. make_option("-a", "--auto-connect",
  65. action="store_true",
  66. dest="auto_connect", default=False),
  67. make_option("-P", "--PSM", action="store",
  68. type="int", dest="psm",
  69. default=None),
  70. make_option("-C", "--channel", action="store",
  71. type="int", dest="channel",
  72. default=None),
  73. make_option("-r", "--record", action="store",
  74. type="string", dest="record",
  75. default=None),
  76. make_option("-S", "--service", action="store",
  77. type="string", dest="service",
  78. default=None),
  79. ]
  80. parser = OptionParser(option_list=option_list)
  81. (options, args) = parser.parse_args()
  82. profile = Profile(bus, options.path)
  83. mainloop = GObject.MainLoop()
  84. opts = {
  85. "AutoConnect" : options.auto_connect,
  86. }
  87. if (options.name):
  88. opts["Name"] = options.name
  89. if (options.role):
  90. opts["Role"] = options.role
  91. if (options.psm is not None):
  92. opts["PSM"] = dbus.UInt16(options.psm)
  93. if (options.channel is not None):
  94. opts["Channel"] = dbus.UInt16(options.channel)
  95. if (options.record):
  96. opts["ServiceRecord"] = options.record
  97. if (options.service):
  98. opts["Service"] = options.service
  99. if not options.uuid:
  100. options.uuid = str(uuid.uuid4())
  101. manager.RegisterProfile(options.path, options.uuid, opts)
  102. mainloop.run()