Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 1 | # 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 Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 5 | import json |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 6 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 7 | from autotest_lib.client.cros import constants |
| 8 | from autotest_lib.server import autotest |
| 9 | |
| 10 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 11 | class BluetoothDevice(object): |
| 12 | """BluetoothDevice is a thin layer of logic over a remote DUT. |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 13 | |
| 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 Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 18 | |
| 19 | XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60 |
Katherine Threlkeld | 88aa143 | 2015-07-30 11:44:35 -0700 | [diff] [blame^] | 20 | XMLRPC_LOG_PATH = '/var/log/bluetooth_xmlrpc_device.log' |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 21 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 22 | def __init__(self, device_host): |
| 23 | """Construct a BluetoothDevice. |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 24 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 25 | @param device_host: host object representing a remote host. |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 26 | |
| 27 | """ |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 28 | self.host = device_host |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 29 | # Make sure the client library is on the device so that the proxy code |
| 30 | # is there when we try to call it. |
| 31 | client_at = autotest.Autotest(self.host) |
| 32 | client_at.install() |
| 33 | # Start up the XML-RPC proxy on the client. |
| 34 | self._proxy = self.host.xmlrpc_connect( |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 35 | constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_COMMAND, |
| 36 | constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_PORT, |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 37 | command_name= |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 38 | constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_CLEANUP_PATTERN, |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 39 | ready_test_name= |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 40 | constants.BLUETOOTH_DEVICE_XMLRPC_SERVER_READY_METHOD, |
Katherine Threlkeld | 88aa143 | 2015-07-30 11:44:35 -0700 | [diff] [blame^] | 41 | timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS, |
| 42 | logfile=self.XMLRPC_LOG_PATH) |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 43 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 44 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 45 | def reset_on(self): |
| 46 | """Reset the adapter and settings and power up the adapter. |
| 47 | |
| 48 | @return True on success, False otherwise. |
| 49 | |
| 50 | """ |
| 51 | return self._proxy.reset_on() |
| 52 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 53 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 54 | def reset_off(self): |
| 55 | """Reset the adapter and settings, leave the adapter powered off. |
| 56 | |
| 57 | @return True on success, False otherwise. |
| 58 | |
| 59 | """ |
| 60 | return self._proxy.reset_off() |
| 61 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 62 | |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 63 | def has_adapter(self): |
| 64 | """@return True if an adapter is present, False if not.""" |
| 65 | return self._proxy.has_adapter() |
| 66 | |
| 67 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 68 | def set_powered(self, powered): |
| 69 | """Set the adapter power state. |
| 70 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 71 | @param powered: adapter power state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 72 | |
| 73 | @return True on success, False otherwise. |
| 74 | |
| 75 | """ |
| 76 | return self._proxy.set_powered(powered) |
| 77 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 78 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 79 | def set_discoverable(self, discoverable): |
| 80 | """Set the adapter discoverable state. |
| 81 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 82 | @param discoverable: adapter discoverable state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 83 | |
| 84 | @return True on success, False otherwise. |
| 85 | |
| 86 | """ |
| 87 | return self._proxy.set_discoverable(discoverable) |
| 88 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 89 | |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 90 | def set_pairable(self, pairable): |
| 91 | """Set the adapter pairable state. |
| 92 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 93 | @param pairable: adapter pairable state to set (True or False). |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 94 | |
| 95 | @return True on success, False otherwise. |
| 96 | |
| 97 | """ |
| 98 | return self._proxy.set_pairable(pairable) |
| 99 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 100 | |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 101 | def get_adapter_properties(self): |
| 102 | """Read the adapter properties from the Bluetooth Daemon. |
| 103 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame] | 104 | @return the properties as a dictionary on success, |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 105 | the value False otherwise. |
| 106 | |
| 107 | """ |
| 108 | return json.loads(self._proxy.get_adapter_properties()) |
| 109 | |
| 110 | |
Scott James Remnant | 50915ad | 2014-12-01 13:51:39 -0800 | [diff] [blame] | 111 | def read_version(self): |
| 112 | """Read the version of the management interface from the Kernel. |
| 113 | |
Scott James Remnant | e59d5b9 | 2014-12-01 14:21:47 -0800 | [diff] [blame] | 114 | @return the version as a tuple of: |
Scott James Remnant | 50915ad | 2014-12-01 13:51:39 -0800 | [diff] [blame] | 115 | ( version, revision ) |
| 116 | |
| 117 | """ |
| 118 | return json.loads(self._proxy.read_version()) |
| 119 | |
| 120 | |
| 121 | def read_supported_commands(self): |
| 122 | """Read the set of supported commands from the Kernel. |
| 123 | |
Scott James Remnant | e59d5b9 | 2014-12-01 14:21:47 -0800 | [diff] [blame] | 124 | @return set of supported commands as arrays in a tuple of: |
Scott James Remnant | 50915ad | 2014-12-01 13:51:39 -0800 | [diff] [blame] | 125 | ( commands, events ) |
| 126 | |
| 127 | """ |
| 128 | return json.loads(self._proxy.read_supported_commands()) |
| 129 | |
| 130 | |
| 131 | def read_index_list(self): |
| 132 | """Read the list of currently known controllers from the Kernel. |
| 133 | |
Scott James Remnant | e59d5b9 | 2014-12-01 14:21:47 -0800 | [diff] [blame] | 134 | @return array of controller indexes. |
Scott James Remnant | 50915ad | 2014-12-01 13:51:39 -0800 | [diff] [blame] | 135 | |
| 136 | """ |
| 137 | return json.loads(self._proxy.read_index_list()) |
| 138 | |
| 139 | |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 140 | def read_info(self): |
| 141 | """Read the adapter information from the Kernel. |
| 142 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame] | 143 | @return the information as a tuple of: |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 144 | ( address, bluetooth_version, manufacturer_id, |
| 145 | supported_settings, current_settings, class_of_device, |
| 146 | name, short_name ) |
| 147 | |
| 148 | """ |
| 149 | return json.loads(self._proxy.read_info()) |
| 150 | |
| 151 | |
Scott James Remnant | abea37c | 2014-12-01 14:22:23 -0800 | [diff] [blame] | 152 | def add_device(self, address, address_type, action): |
| 153 | """Add a device to the Kernel action list. |
| 154 | |
| 155 | @param address: Address of the device to add. |
| 156 | @param address_type: Type of device in @address. |
| 157 | @param action: Action to take. |
| 158 | |
| 159 | @return tuple of ( address, address_type ) on success, |
| 160 | None on failure. |
| 161 | |
| 162 | """ |
| 163 | return json.loads(self._proxy.add_device(address, address_type, action)) |
| 164 | |
| 165 | |
| 166 | def remove_device(self, address, address_type): |
| 167 | """Remove a device from the Kernel action list. |
| 168 | |
| 169 | @param address: Address of the device to remove. |
| 170 | @param address_type: Type of device in @address. |
| 171 | |
| 172 | @return tuple of ( address, address_type ) on success, |
| 173 | None on failure. |
| 174 | |
| 175 | """ |
| 176 | return json.loads(self._proxy.remove_device(address, address_type)) |
| 177 | |
| 178 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame] | 179 | def get_devices(self): |
| 180 | """Read information about remote devices known to the adapter. |
| 181 | |
Scott James Remnant | e59d5b9 | 2014-12-01 14:21:47 -0800 | [diff] [blame] | 182 | @return the properties of each device as an array of |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame] | 183 | dictionaries on success, the value False otherwise. |
| 184 | |
| 185 | """ |
| 186 | return json.loads(self._proxy.get_devices()) |
| 187 | |
| 188 | |
| 189 | def start_discovery(self): |
| 190 | """Start discovery of remote devices. |
| 191 | |
| 192 | Obtain the discovered device information using get_devices(), called |
| 193 | stop_discovery() when done. |
| 194 | |
| 195 | @return True on success, False otherwise. |
| 196 | |
| 197 | """ |
| 198 | return self._proxy.start_discovery() |
| 199 | |
| 200 | |
| 201 | def stop_discovery(self): |
| 202 | """Stop discovery of remote devices. |
| 203 | |
| 204 | @return True on success, False otherwise. |
| 205 | |
| 206 | """ |
| 207 | return self._proxy.stop_discovery() |
| 208 | |
| 209 | |
Scott James Remnant | c7fd7a4 | 2014-12-01 16:43:38 -0800 | [diff] [blame] | 210 | def get_dev_info(self): |
| 211 | """Read raw HCI device information. |
| 212 | |
| 213 | @return tuple of (index, name, address, flags, device_type, bus_type, |
| 214 | features, pkt_type, link_policy, link_mode, |
| 215 | acl_mtu, acl_pkts, sco_mtu, sco_pkts, |
| 216 | err_rx, err_tx, cmd_tx, evt_rx, acl_tx, acl_rx, |
| 217 | sco_tx, sco_rx, byte_rx, byte_tx) on success, |
| 218 | None on failure. |
| 219 | |
| 220 | """ |
| 221 | return json.loads(self._proxy.get_dev_info()) |
| 222 | |
| 223 | |
Artem Rakhov | b144dce | 2014-02-20 21:02:09 -0800 | [diff] [blame] | 224 | def register_profile(self, path, uuid, options): |
| 225 | """Register new profile (service). |
| 226 | |
| 227 | @param path: Path to the profile object. |
| 228 | @param uuid: Service Class ID of the service as string. |
| 229 | @param options: Dictionary of options for the new service, compliant |
| 230 | with BlueZ D-Bus Profile API standard. |
| 231 | |
| 232 | @return True on success, False otherwise. |
| 233 | |
| 234 | """ |
| 235 | return self._proxy.register_profile(path, uuid, options) |
| 236 | |
| 237 | |
Cheng-Yi Chiang | dc6442b | 2015-07-08 16:07:02 +0800 | [diff] [blame] | 238 | def has_device(self, address): |
| 239 | """Checks if the device with a given address exists. |
| 240 | |
| 241 | @param address: Address of the device. |
| 242 | |
| 243 | @returns: True if there is a device with that address. |
| 244 | False otherwise. |
| 245 | |
| 246 | """ |
| 247 | return self._proxy.has_device(address) |
| 248 | |
| 249 | |
Cheng-Yi Chiang | 212d469 | 2015-05-25 22:08:14 +0800 | [diff] [blame] | 250 | def pair_legacy_device(self, address, pin, timeout): |
| 251 | """Pairs a device with a given pin code. |
| 252 | |
| 253 | Registers an agent who handles pin code request and |
| 254 | pairs a device with known pin code. |
| 255 | |
| 256 | @param address: Address of the device to pair. |
| 257 | @param pin: The pin code of the device to pair. |
| 258 | @param timeout: The timeout in seconds for pairing. |
| 259 | |
| 260 | @returns: True on success. False otherwise. |
| 261 | |
| 262 | """ |
| 263 | return self._proxy.pair_legacy_device(address, pin, timeout) |
| 264 | |
| 265 | |
| 266 | def connect_device(self, address): |
| 267 | """Connects a device. |
| 268 | |
| 269 | Connects a device if it is not connected. |
| 270 | |
| 271 | @param address: Address of the device to connect. |
| 272 | |
| 273 | @returns: True on success. False otherwise. |
| 274 | |
| 275 | """ |
| 276 | return self._proxy.connect_device(address) |
| 277 | |
| 278 | |
| 279 | def disconnect_device(self, address): |
| 280 | """Disconnects a device. |
| 281 | |
| 282 | Disconnects a device if it is connected. |
| 283 | |
| 284 | @param address: Address of the device to disconnect. |
| 285 | |
| 286 | @returns: True on success. False otherwise. |
| 287 | |
| 288 | """ |
| 289 | return self._proxy.disconnect_device(address) |
| 290 | |
| 291 | |
Katherine Threlkeld | 88aa143 | 2015-07-30 11:44:35 -0700 | [diff] [blame^] | 292 | def copy_logs(self, destination): |
| 293 | """Copy the logs generated by this device to a given location. |
| 294 | |
| 295 | @param destination: destination directory for the logs. |
| 296 | |
| 297 | """ |
| 298 | self.host.collect_logs(self.XMLRPC_LOG_PATH, destination) |
| 299 | |
| 300 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 301 | def close(self): |
| 302 | """Tear down state associated with the client.""" |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 303 | # Turn off the discoverable flag since it may affect future tests. |
| 304 | self._proxy.set_discoverable(False) |
Scott James Remnant | a6442f5 | 2013-07-24 15:04:55 -0700 | [diff] [blame] | 305 | # Leave the adapter powered off, but don't do a full reset. |
| 306 | self._proxy.set_powered(False) |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 307 | # This kills the RPC server. |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 308 | self.host.close() |