aboutsummaryrefslogtreecommitdiff
path: root/bluez-test-scripts/test-adapter
blob: 959a43703c9340a018c60276207de963abd5677d (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
#!/usr/bin/python

from __future__ import absolute_import, print_function, unicode_literals

from optparse import OptionParser, make_option
import sys
import time
import dbus
import bluezutils

bus = dbus.SystemBus()

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

(options, args) = parser.parse_args()

adapter_path = bluezutils.find_adapter(options.dev_id).object_path
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path),
					"org.freedesktop.DBus.Properties")

if (len(args) < 1):
	print("Usage: %s <command>" % (sys.argv[0]))
	print("")
	print("  address")
	print("  list")
	print("  name")
	print("  alias [alias]")
	print("  powered [on/off]")
	print("  pairable [on/off]")
	print("  pairabletimeout [timeout]")
	print("  discoverable [on/off]")
	print("  discoverabletimeout [timeout]")
	print("  discovering")
	sys.exit(1)

if (args[0] == "address"):
	addr = adapter.Get("org.bluez.Adapter1", "Address")
	print(addr)
	sys.exit(0)

if (args[0] == "name"):
	name = adapter.Get("org.bluez.Adapter1", "Name")
	print(name)
	sys.exit(0)

if (args[0] == "alias"):
	if (len(args) < 2):
		alias = adapter.Get("org.bluez.Adapter1", "Alias")
		print(alias)
	else:
		adapter.Set("org.bluez.Adapter1", "Alias", args[1])
	sys.exit(0)

if (args[0] == "list"):
	if (len(args) < 2):
		om = dbus.Interface(bus.get_object("org.bluez", "/"),
					"org.freedesktop.DBus.ObjectManager")
		objects = om.GetManagedObjects()
		for path, interfaces in objects.iteritems():
			if "org.bluez.Adapter1" not in interfaces:
				continue

			print(" [ %s ]" % (path))

			props = interfaces["org.bluez.Adapter1"]

			for (key, value) in props.items():
				if (key == "Class"):
					print("    %s = 0x%06x" % (key, value))
				else:
					print("    %s = %s" % (key, value))
			print()
	sys.exit(0)

if (args[0] == "powered"):
	if (len(args) < 2):
		powered = adapter.Get("org.bluez.Adapter1", "Powered")
		print(powered)
	else:
		if (args[1] == "on"):
			value = dbus.Boolean(1)
		elif (args[1] == "off"):
			value = dbus.Boolean(0)
		else:
			value = dbus.Boolean(args[1])
		adapter.Set("org.bluez.Adapter1", "Powered", value)
	sys.exit(0)

if (args[0] == "pairable"):
	if (len(args) < 2):
		pairable = adapter.Get("org.bluez.Adapter1", "Pairable")
		print(pairable)
	else:
		if (args[1] == "on"):
			value = dbus.Boolean(1)
		elif (args[1] == "off"):
			value = dbus.Boolean(0)
		else:
			value = dbus.Boolean(args[1])
		adapter.Set("org.bluez.Adapter1", "Pairable", value)
	sys.exit(0)

if (args[0] == "pairabletimeout"):
	if (len(args) < 2):
		pt = adapter.Get("org.bluez.Adapter1", "PairableTimeout")
		print(pt)
	else:
		timeout = dbus.UInt32(args[1])
		adapter.Set("org.bluez.Adapter1", "PairableTimeout", timeout)
	sys.exit(0)

if (args[0] == "discoverable"):
	if (len(args) < 2):
		discoverable = adapter.Get("org.bluez.Adapter1", "Discoverable")
		print(discoverable)
	else:
		if (args[1] == "on"):
			value = dbus.Boolean(1)
		elif (args[1] == "off"):
			value = dbus.Boolean(0)
		else:
			value = dbus.Boolean(args[1])
		adapter.Set("org.bluez.Adapter1", "Discoverable", value)
	sys.exit(0)

if (args[0] == "discoverabletimeout"):
	if (len(args) < 2):
		dt = adapter.Get("org.bluez.Adapter1", "DiscoverableTimeout")
		print(dt)
	else:
		to = dbus.UInt32(args[1])
		adapter.Set("org.bluez.Adapter1", "DiscoverableTimeout", to)
	sys.exit(0)

if (args[0] == "discovering"):
	discovering = adapter.Get("org.bluez.Adapter1", "Discovering")
	print(discovering)
	sys.exit(0)

print("Unknown command")
sys.exit(1)