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