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