test-device 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 re
  6. import sys
  7. import dbus
  8. import dbus.mainloop.glib
  9. try:
  10. from gi.repository import GObject
  11. except ImportError:
  12. import gobject as GObject
  13. import bluezutils
  14. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  15. bus = dbus.SystemBus()
  16. mainloop = GObject.MainLoop()
  17. option_list = [
  18. make_option("-i", "--device", action="store",
  19. type="string", dest="dev_id"),
  20. ]
  21. parser = OptionParser(option_list=option_list)
  22. (options, args) = parser.parse_args()
  23. if (len(args) < 1):
  24. print("Usage: %s <command>" % (sys.argv[0]))
  25. print("")
  26. print(" list")
  27. print(" create <address>")
  28. print(" remove <address|path>")
  29. print(" connect <address> [profile]")
  30. print(" disconnect <address> [profile]")
  31. print(" class <address>")
  32. print(" name <address>")
  33. print(" alias <address> [alias]")
  34. print(" trusted <address> [yes/no]")
  35. print(" blocked <address> [yes/no]")
  36. sys.exit(1)
  37. if (args[0] == "list"):
  38. adapter = bluezutils.find_adapter(options.dev_id)
  39. adapter_path = adapter.object_path
  40. om = dbus.Interface(bus.get_object("org.bluez", "/"),
  41. "org.freedesktop.DBus.ObjectManager")
  42. objects = om.GetManagedObjects()
  43. for path, interfaces in objects.iteritems():
  44. if "org.bluez.Device1" not in interfaces:
  45. continue
  46. properties = interfaces["org.bluez.Device1"]
  47. if properties["Adapter"] != adapter_path:
  48. continue;
  49. print("%s %s" % (properties["Address"], properties["Alias"]))
  50. sys.exit(0)
  51. def create_device_reply(device):
  52. print("New device (%s)" % device)
  53. mainloop.quit()
  54. sys.exit(0)
  55. def create_device_error(error):
  56. print("Creating device failed: %s" % error)
  57. mainloop.quit()
  58. sys.exit(1)
  59. if (args[0] == "create"):
  60. if (len(args) < 2):
  61. print("Need address parameter")
  62. else:
  63. adapter = bluezutils.find_adapter(options.dev_id)
  64. adapter.CreateDevice(args[1],
  65. reply_handler=create_device_reply,
  66. error_handler=create_device_error)
  67. mainloop.run()
  68. if (args[0] == "remove"):
  69. if (len(args) < 2):
  70. print("Need address or object path parameter")
  71. else:
  72. managed_objects = bluezutils.get_managed_objects()
  73. adapter = bluezutils.find_adapter_in_objects(managed_objects,
  74. options.dev_id)
  75. try:
  76. dev = bluezutils.find_device_in_objects(managed_objects,
  77. args[1],
  78. options.dev_id)
  79. path = dev.object_path
  80. except:
  81. path = args[1]
  82. adapter.RemoveDevice(path)
  83. sys.exit(0)
  84. if (args[0] == "connect"):
  85. if (len(args) < 2):
  86. print("Need address parameter")
  87. else:
  88. device = bluezutils.find_device(args[1], options.dev_id)
  89. if (len(args) > 2):
  90. device.ConnectProfile(args[2])
  91. else:
  92. device.Connect()
  93. sys.exit(0)
  94. if (args[0] == "disconnect"):
  95. if (len(args) < 2):
  96. print("Need address parameter")
  97. else:
  98. device = bluezutils.find_device(args[1], options.dev_id)
  99. if (len(args) > 2):
  100. device.DisconnectProfile(args[2])
  101. else:
  102. device.Disconnect()
  103. sys.exit(0)
  104. if (args[0] == "class"):
  105. if (len(args) < 2):
  106. print("Need address parameter")
  107. else:
  108. device = bluezutils.find_device(args[1], options.dev_id)
  109. path = device.object_path
  110. props = dbus.Interface(bus.get_object("org.bluez", path),
  111. "org.freedesktop.DBus.Properties")
  112. cls = props.Get("org.bluez.Device1", "Class")
  113. print("0x%06x" % cls)
  114. sys.exit(0)
  115. if (args[0] == "name"):
  116. if (len(args) < 2):
  117. print("Need address parameter")
  118. else:
  119. device = bluezutils.find_device(args[1], options.dev_id)
  120. path = device.object_path
  121. props = dbus.Interface(bus.get_object("org.bluez", path),
  122. "org.freedesktop.DBus.Properties")
  123. name = props.Get("org.bluez.Device1", "Name")
  124. print(name)
  125. sys.exit(0)
  126. if (args[0] == "alias"):
  127. if (len(args) < 2):
  128. print("Need address parameter")
  129. else:
  130. device = bluezutils.find_device(args[1], options.dev_id)
  131. path = device.object_path
  132. props = dbus.Interface(bus.get_object("org.bluez", path),
  133. "org.freedesktop.DBus.Properties")
  134. if (len(args) < 3):
  135. alias = props.Get("org.bluez.Device1", "Alias")
  136. print(alias)
  137. else:
  138. props.Set("org.bluez.Device1", "Alias", args[2])
  139. sys.exit(0)
  140. if (args[0] == "trusted"):
  141. if (len(args) < 2):
  142. print("Need address parameter")
  143. else:
  144. device = bluezutils.find_device(args[1], options.dev_id)
  145. path = device.object_path
  146. props = dbus.Interface(bus.get_object("org.bluez", path),
  147. "org.freedesktop.DBus.Properties")
  148. if (len(args) < 3):
  149. trusted = props.Get("org.bluez.Device1", "Trusted")
  150. print(trusted)
  151. else:
  152. if (args[2] == "yes"):
  153. value = dbus.Boolean(1)
  154. elif (args[2] == "no"):
  155. value = dbus.Boolean(0)
  156. else:
  157. value = dbus.Boolean(args[2])
  158. props.Set("org.bluez.Device1", "Trusted", value)
  159. sys.exit(0)
  160. if (args[0] == "blocked"):
  161. if (len(args) < 2):
  162. print("Need address parameter")
  163. else:
  164. device = bluezutils.find_device(args[1], options.dev_id)
  165. path = device.object_path
  166. props = dbus.Interface(bus.get_object("org.bluez", path),
  167. "org.freedesktop.DBus.Properties")
  168. if (len(args) < 3):
  169. blocked = props.Get("org.bluez.Device1", "Blocked")
  170. print(blocked)
  171. else:
  172. if (args[2] == "yes"):
  173. value = dbus.Boolean(1)
  174. elif (args[2] == "no"):
  175. value = dbus.Boolean(0)
  176. else:
  177. value = dbus.Boolean(args[2])
  178. props.Set("org.bluez.Device1", "Blocked", value)
  179. sys.exit(0)
  180. print("Unknown command")
  181. sys.exit(1)