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