summaryrefslogtreecommitdiff
path: root/test/test-device
blob: 154af1904b8cd7f90e0a989a14b5bc5f37265f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python

import gobject

import sys
import dbus
import dbus.mainloop.glib
import re
from optparse import OptionParser, make_option

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
mainloop = gobject.MainLoop()

manager = dbus.Interface(bus.get_object("org.bluez", "/"), "org.bluez.Manager")

option_list = [
		make_option("-i", "--device", action="store",
				type="string", dest="dev_id"),
		]
parser = OptionParser(option_list=option_list)

(options, args) = parser.parse_args()

if options.dev_id:
	adapter_path = manager.FindAdapter(options.dev_id)
else:
	adapter_path = manager.DefaultAdapter()

adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
							"org.bluez.Adapter")

if (len(args) < 1):
	print "Usage: %s <command>" % (sys.argv[0])
	print ""
	print "  list"
	print "  services <address>"
	print "  create <address>"
	print "  remove <address|path>"
	print "  disconnect <address>"
	print "  discover <address> [pattern]"
	print "  class <address>"
	print "  name <address>"
	print "  alias <address> [alias]"
	print "  trusted <address> [yes/no]"
	print "  blocked <address> [yes/no]"
	sys.exit(1)

if (args[0] == "list"):
	for path in adapter.ListDevices():
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		print "%s %s" % (properties["Address"], properties["Alias"])

	sys.exit(0)

def create_device_reply(device):
	print "New device (%s)" % device
	mainloop.quit()
	sys.exit(0)

def create_device_error(error):
	print "Creating device failed: %s" % error
	mainloop.quit()
	sys.exit(1)

if (args[0] == "create"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		adapter.CreateDevice(args[1],
				reply_handler=create_device_reply,
				error_handler=create_device_error)
	mainloop.run()

if (args[0] == "remove"):
	if (len(args) < 2):
		print "Need address or object path parameter"
	else:
		try:
			path = adapter.FindDevice(args[1])
		except:
			path = args[1]
		adapter.RemoveDevice(path)
	sys.exit(0)

if (args[0] == "disconnect"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		device.Disconnect()
	sys.exit(0)

if (args[0] == "discover"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			pattern = ""
		else:
			pattern = args[2]
		services = device.DiscoverServices(pattern);
		for key in services.keys():
			p = re.compile(">.*?<")
			xml = p.sub("><", services[key].replace("\n", ""))
			print "[ 0x%5x ]" % (key)
			print xml
			print
	sys.exit(0)

if (args[0] == "class"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		print "0x%06x" % (properties["Class"])
	sys.exit(0)

if (args[0] == "name"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		print properties["Name"]
	sys.exit(0)

if (args[0] == "alias"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			properties = device.GetProperties()
			print properties["Alias"]
		else:
			device.SetProperty("Alias", args[2])
	sys.exit(0)

if (args[0] == "trusted"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			properties = device.GetProperties()
			print properties["Trusted"]
		else:
			if (args[2] == "yes"):
				value = dbus.Boolean(1)
			elif (args[2] == "no"):
				value = dbus.Boolean(0)
			else:
				value = dbus.Boolean(args[2])
			device.SetProperty("Trusted", value)
	sys.exit(0)

if (args[0] == "blocked"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		if (len(args) < 3):
			properties = device.GetProperties()
			print properties["Blocked"]
		else:
			if (args[2] == "yes"):
				value = dbus.Boolean(1)
			elif (args[2] == "no"):
				value = dbus.Boolean(0)
			else:
				value = dbus.Boolean(args[2])
			device.SetProperty("Blocked", value)
	sys.exit(0)

if (args[0] == "services"):
	if (len(args) < 2):
		print "Need address parameter"
	else:
		path = adapter.FindDevice(args[1])
		device = dbus.Interface(bus.get_object("org.bluez", path),
							"org.bluez.Device")
		properties = device.GetProperties()
		for path in properties["Services"]:
			print path
	sys.exit(0)

print "Unknown command"
sys.exit(1)