blob: b6f6b0f72403e262dbf49c205adaf1623e8f67db [file] [log] [blame]
Scott James Remnant4dcd73f2013-07-22 15:00:24 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -07005import json
Scott James Remnant4dcd73f2013-07-22 15:00:24 -07006
Scott James Remnant4dcd73f2013-07-22 15:00:24 -07007from autotest_lib.client.cros import constants
8from autotest_lib.server import autotest
9
10
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080011class BluetoothDevice(object):
12 """BluetoothDevice is a thin layer of logic over a remote DUT.
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070013
14 The Autotest host object representing the remote DUT, passed to this
15 class on initialization, can be accessed from its host property.
16
17 """
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070018
19 XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60
20
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080021 def __init__(self, device_host):
22 """Construct a BluetoothDevice.
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070023
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080024 @param device_host: host object representing a remote host.
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070025
26 """
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080027 self.host = device_host
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070028 # Make sure the client library is on the device so that the proxy code
29 # is there when we try to call it.
30 client_at = autotest.Autotest(self.host)
31 client_at.install()
32 # Start up the XML-RPC proxy on the client.
33 self._proxy = self.host.xmlrpc_connect(
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080034 constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_COMMAND,
35 constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_PORT,
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070036 command_name=
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080037 constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_CLEANUP_PATTERN,
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070038 ready_test_name=
Scott James Remnant8d2cbf32013-11-12 11:00:25 -080039 constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_READY_METHOD,
Scott James Remnant4dcd73f2013-07-22 15:00:24 -070040 timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS)
41
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070042
Scott James Remnanta6442f52013-07-24 15:04:55 -070043 def reset_on(self):
44 """Reset the adapter and settings and power up the adapter.
45
46 @return True on success, False otherwise.
47
48 """
49 return self._proxy.reset_on()
50
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070051
Scott James Remnanta6442f52013-07-24 15:04:55 -070052 def reset_off(self):
53 """Reset the adapter and settings, leave the adapter powered off.
54
55 @return True on success, False otherwise.
56
57 """
58 return self._proxy.reset_off()
59
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070060
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -070061 def has_adapter(self):
62 """@return True if an adapter is present, False if not."""
63 return self._proxy.has_adapter()
64
65
Scott James Remnanta6442f52013-07-24 15:04:55 -070066 def set_powered(self, powered):
67 """Set the adapter power state.
68
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070069 @param powered: adapter power state to set (True or False).
Scott James Remnanta6442f52013-07-24 15:04:55 -070070
71 @return True on success, False otherwise.
72
73 """
74 return self._proxy.set_powered(powered)
75
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070076
Scott James Remnanta6442f52013-07-24 15:04:55 -070077 def set_discoverable(self, discoverable):
78 """Set the adapter discoverable state.
79
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070080 @param discoverable: adapter discoverable state to set (True or False).
Scott James Remnanta6442f52013-07-24 15:04:55 -070081
82 @return True on success, False otherwise.
83
84 """
85 return self._proxy.set_discoverable(discoverable)
86
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070087
Scott James Remnanta6442f52013-07-24 15:04:55 -070088 def set_pairable(self, pairable):
89 """Set the adapter pairable state.
90
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070091 @param pairable: adapter pairable state to set (True or False).
Scott James Remnanta6442f52013-07-24 15:04:55 -070092
93 @return True on success, False otherwise.
94
95 """
96 return self._proxy.set_pairable(pairable)
97
Scott James Remnant1c72d7a2013-07-29 15:00:04 -070098
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -070099 def get_adapter_properties(self):
100 """Read the adapter properties from the Bluetooth Daemon.
101
Scott James Remnantaec4edd2013-08-26 18:47:11 -0700102 @return the properties as a dictionary on success,
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -0700103 the value False otherwise.
104
105 """
106 return json.loads(self._proxy.get_adapter_properties())
107
108
Scott James Remnant50915ad2014-12-01 13:51:39 -0800109 def read_version(self):
110 """Read the version of the management interface from the Kernel.
111
112 @return the information as a JSON-encoded tuple of:
113 ( version, revision )
114
115 """
116 return json.loads(self._proxy.read_version())
117
118
119 def read_supported_commands(self):
120 """Read the set of supported commands from the Kernel.
121
122 @return the information as a JSON-encoded tuple of:
123 ( commands, events )
124
125 """
126 return json.loads(self._proxy.read_supported_commands())
127
128
129 def read_index_list(self):
130 """Read the list of currently known controllers from the Kernel.
131
132 @return the information as a JSON-encoded array of controller indexes.
133
134 """
135 return json.loads(self._proxy.read_index_list())
136
137
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -0700138 def read_info(self):
139 """Read the adapter information from the Kernel.
140
Scott James Remnantaec4edd2013-08-26 18:47:11 -0700141 @return the information as a tuple of:
Scott James Remnant1ca2e0e2013-07-31 16:49:07 -0700142 ( address, bluetooth_version, manufacturer_id,
143 supported_settings, current_settings, class_of_device,
144 name, short_name )
145
146 """
147 return json.loads(self._proxy.read_info())
148
149
Scott James Remnantaec4edd2013-08-26 18:47:11 -0700150 def get_devices(self):
151 """Read information about remote devices known to the adapter.
152
153 @return the properties of each device as a JSON-encoded array of
154 dictionaries on success, the value False otherwise.
155
156 """
157 return json.loads(self._proxy.get_devices())
158
159
160 def start_discovery(self):
161 """Start discovery of remote devices.
162
163 Obtain the discovered device information using get_devices(), called
164 stop_discovery() when done.
165
166 @return True on success, False otherwise.
167
168 """
169 return self._proxy.start_discovery()
170
171
172 def stop_discovery(self):
173 """Stop discovery of remote devices.
174
175 @return True on success, False otherwise.
176
177 """
178 return self._proxy.stop_discovery()
179
180
Artem Rakhovb144dce2014-02-20 21:02:09 -0800181 def register_profile(self, path, uuid, options):
182 """Register new profile (service).
183
184 @param path: Path to the profile object.
185 @param uuid: Service Class ID of the service as string.
186 @param options: Dictionary of options for the new service, compliant
187 with BlueZ D-Bus Profile API standard.
188
189 @return True on success, False otherwise.
190
191 """
192 return self._proxy.register_profile(path, uuid, options)
193
194
Scott James Remnant4dcd73f2013-07-22 15:00:24 -0700195 def close(self):
196 """Tear down state associated with the client."""
Scott James Remnantda9f43c2013-08-07 17:53:14 -0700197 # Turn off the discoverable flag since it may affect future tests.
198 self._proxy.set_discoverable(False)
Scott James Remnanta6442f52013-07-24 15:04:55 -0700199 # Leave the adapter powered off, but don't do a full reset.
200 self._proxy.set_powered(False)
Scott James Remnant4dcd73f2013-07-22 15:00:24 -0700201 # This kills the RPC server.
Scott James Remnant1c72d7a2013-07-29 15:00:04 -0700202 self.host.close()