blob: b490d53f4a71fe4ad461256faaaf8f9d793fe439 [file] [log] [blame]
Igor Sarkisov4cfbc132020-10-01 12:34:10 -07001#!/usr/bin/python
2
3from __future__ import absolute_import, print_function, unicode_literals
4
5from optparse import OptionParser, make_option
6import re
7import sys
8import dbus
9import dbus.mainloop.glib
10try:
11 from gi.repository import GObject
12except ImportError:
13 import gobject as GObject
14import bluezutils
15
16dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
17bus = dbus.SystemBus()
18mainloop = GObject.MainLoop()
19
20option_list = [
21 make_option("-i", "--device", action="store",
22 type="string", dest="dev_id"),
23 ]
24parser = OptionParser(option_list=option_list)
25
26(options, args) = parser.parse_args()
27
28if (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
43if (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
61def create_device_reply(device):
62 print("New device (%s)" % device)
63 mainloop.quit()
64 sys.exit(0)
65
66def create_device_error(error):
67 print("Creating device failed: %s" % error)
68 mainloop.quit()
69 sys.exit(1)
70
71if (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
81if (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
98if (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
109if (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
120if (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
132if (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
144if (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
159if (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
180if (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
201print("Unknown command")
202sys.exit(1)