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 | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 5 | import base64 |
| 6 | import json |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 7 | import socket |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 8 | |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import error |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 10 | from autotest_lib.client.cros import constants |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 11 | from autotest_lib.server import autotest, hosts |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 12 | |
| 13 | |
| 14 | class BluetoothTester(object): |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 15 | """BluetoothTester is a thin layer of logic over a remote tester. |
| 16 | |
| 17 | The Autotest host object representing the remote tester, passed to this |
| 18 | class on initialization, can be accessed from its host property. |
| 19 | |
| 20 | """ |
| 21 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 22 | |
| 23 | XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60 |
| 24 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 25 | def __init__(self, tester_host): |
| 26 | """Construct a BluetoothTester. |
| 27 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 28 | @param tester_host: host object representing a remote host. |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 29 | |
| 30 | """ |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 31 | self.host = tester_host |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 32 | # Make sure the client library is on the device so that the proxy code |
| 33 | # is there when we try to call it. |
| 34 | client_at = autotest.Autotest(self.host) |
| 35 | client_at.install() |
| 36 | # Start up the XML-RPC proxy on the tester. |
| 37 | self._proxy = self.host.xmlrpc_connect( |
| 38 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_COMMAND, |
| 39 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_PORT, |
| 40 | command_name= |
| 41 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_CLEANUP_PATTERN, |
| 42 | ready_test_name= |
| 43 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_READY_METHOD, |
| 44 | timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS) |
| 45 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 46 | |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 47 | def setup(self, profile): |
| 48 | """Set up the tester with the given profile. |
| 49 | |
| 50 | @param profile: Profile to use for this test, valid values are: |
| 51 | computer - a standard computer profile |
| 52 | |
| 53 | @return True on success, False otherwise. |
| 54 | |
| 55 | """ |
| 56 | return self._proxy.setup(profile) |
| 57 | |
| 58 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame] | 59 | def set_discoverable(self, discoverable, timeout=0): |
| 60 | """Set the discoverable state of the controller. |
| 61 | |
| 62 | @param discoverable: Whether controller should be discoverable. |
| 63 | @param timeout: Timeout in seconds before disabling discovery again, |
| 64 | ignored when discoverable is False, must not be zero when |
| 65 | discoverable is True. |
| 66 | |
| 67 | @return True on success, False otherwise. |
| 68 | |
| 69 | """ |
| 70 | return self._proxy.set_discoverable(discoverable, timeout) |
| 71 | |
| 72 | |
| 73 | def read_info(self): |
| 74 | """Read the adapter information from the Kernel. |
| 75 | |
| 76 | @return the information as a JSON-encoded tuple of: |
| 77 | ( address, bluetooth_version, manufacturer_id, |
| 78 | supported_settings, current_settings, class_of_device, |
| 79 | name, short_name ) |
| 80 | |
| 81 | """ |
| 82 | return json.loads(self._proxy.read_info()) |
| 83 | |
| 84 | |
Scott James Remnant | e686dcc | 2014-12-05 17:14:08 -0800 | [diff] [blame^] | 85 | def set_advertising(self, advertising): |
| 86 | """Set the whether the controller is advertising via LE. |
| 87 | |
| 88 | @param advertising: Whether controller should advertise via LE. |
| 89 | |
| 90 | @return True on success, False otherwise. |
| 91 | |
| 92 | """ |
| 93 | return self._proxy.set_advertising(advertising) |
| 94 | |
| 95 | |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 96 | def discover_devices(self, br_edr=True, le_public=True, le_random=True): |
| 97 | """Discover remote devices. |
| 98 | |
| 99 | Activates device discovery and collects the set of devices found, |
| 100 | returning them as a list. |
| 101 | |
| 102 | @param br_edr: Whether to detect BR/EDR devices. |
| 103 | @param le_public: Whether to detect LE Public Address devices. |
| 104 | @param le_random: Whether to detect LE Random Address devices. |
| 105 | |
| 106 | @return List of devices found as tuples with the format |
| 107 | (address, address_type, rssi, flags, base64-encoded eirdata), |
| 108 | or False if discovery could not be started. |
| 109 | |
| 110 | """ |
| 111 | devices = self._proxy.discover_devices(br_edr, le_public, le_random) |
| 112 | if devices == False: |
| 113 | return False |
| 114 | |
| 115 | return ( |
| 116 | (address, address_type, rssi, flags, |
| 117 | base64.decodestring(eirdata)) |
| 118 | for address, address_type, rssi, flags, eirdata |
| 119 | in json.loads(devices) |
| 120 | ) |
| 121 | |
| 122 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 123 | def close(self): |
| 124 | """Tear down state associated with the client.""" |
| 125 | # This kills the RPC server. |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 126 | self.host.close() |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 127 | |
| 128 | |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 129 | def connect(self, address): |
| 130 | """Connect to device with the given address |
| 131 | |
| 132 | @param address: Bluetooth address. |
| 133 | |
| 134 | """ |
| 135 | self._proxy.connect(address) |
| 136 | |
| 137 | |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 138 | def service_search_request(self, uuids, max_rec_cnt, preferred_size=32, |
Artem Rakhov | f42dc83 | 2014-03-03 12:19:13 -0800 | [diff] [blame] | 139 | forced_pdu_size=None, invalid_request=False): |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 140 | """Send a Service Search Request |
| 141 | |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 142 | @param uuids: List of UUIDs (as integers) to look for. |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 143 | @param max_rec_cnt: Maximum count of returned service records. |
Artem Rakhov | ca443b5 | 2014-01-10 17:18:24 -0800 | [diff] [blame] | 144 | @param preferred_size: Preffered size of UUIDs in bits (16, 32, or 128). |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 145 | @param forced_pdu_size: Use certain PDU size parameter instead of |
| 146 | calculating actual length of sequence. |
Artem Rakhov | f42dc83 | 2014-03-03 12:19:13 -0800 | [diff] [blame] | 147 | @param invalid_request: Whether to send request with intentionally |
| 148 | invalid syntax for testing purposes (bool flag). |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 149 | |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 150 | @return list of found services' service record handles or Error Code |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 151 | |
| 152 | """ |
Artem Rakhov | ca443b5 | 2014-01-10 17:18:24 -0800 | [diff] [blame] | 153 | return self._proxy.service_search_request(uuids, max_rec_cnt, |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 154 | preferred_size, |
Artem Rakhov | f42dc83 | 2014-03-03 12:19:13 -0800 | [diff] [blame] | 155 | forced_pdu_size, |
| 156 | invalid_request) |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 157 | |
| 158 | |
Artem Rakhov | af4e4a6 | 2014-06-11 20:33:50 -0700 | [diff] [blame] | 159 | def service_attribute_request(self, handle, max_attr_byte_count, attr_ids, |
| 160 | forced_pdu_size=None, invalid_request=None): |
Artem Rakhov | ebf3df3 | 2014-04-01 14:52:25 -0700 | [diff] [blame] | 161 | """Send a Service Attribute Request |
| 162 | |
| 163 | @param handle: service record from which attribute values are to be |
| 164 | retrieved. |
| 165 | @param max_attr_byte_count: maximum number of bytes of attribute data to |
| 166 | be returned in the response to this request. |
| 167 | @param attr_ids: a list, where each element is either an attribute ID |
| 168 | or a range of attribute IDs. |
Artem Rakhov | af4e4a6 | 2014-06-11 20:33:50 -0700 | [diff] [blame] | 169 | @param forced_pdu_size: Use certain PDU size parameter instead of |
| 170 | calculating actual length of sequence. |
| 171 | @param invalid_request: Whether to send request with intentionally |
| 172 | invalid syntax for testing purposes (string with raw request). |
Artem Rakhov | ebf3df3 | 2014-04-01 14:52:25 -0700 | [diff] [blame] | 173 | |
| 174 | @return list of found attributes IDs and their values or Error Code |
| 175 | |
| 176 | """ |
| 177 | return self._proxy.service_attribute_request(handle, |
| 178 | max_attr_byte_count, |
Artem Rakhov | af4e4a6 | 2014-06-11 20:33:50 -0700 | [diff] [blame] | 179 | attr_ids, |
| 180 | forced_pdu_size, |
| 181 | invalid_request) |
Artem Rakhov | ebf3df3 | 2014-04-01 14:52:25 -0700 | [diff] [blame] | 182 | |
| 183 | |
Artem Rakhov | f53eeec | 2014-09-24 21:15:58 -0700 | [diff] [blame] | 184 | def service_search_attribute_request(self, uuids, max_attr_byte_count, |
Artem Rakhov | ef5a458 | 2014-10-29 16:02:55 -0700 | [diff] [blame] | 185 | attr_ids, preferred_size=32, |
| 186 | forced_pdu_size=None, |
| 187 | invalid_request=None): |
Artem Rakhov | f53eeec | 2014-09-24 21:15:58 -0700 | [diff] [blame] | 188 | """Send a Service Search Attribute Request |
| 189 | |
| 190 | @param uuids: list of UUIDs (as integers) to look for. |
| 191 | @param max_attr_byte_count: maximum number of bytes of attribute data to |
| 192 | be returned in the response to this request. |
| 193 | @param attr_ids: a list, where each element is either an attribute ID |
| 194 | or a range of attribute IDs. |
| 195 | @param preferred_size: Preffered size of UUIDs in bits (16, 32, or 128). |
Artem Rakhov | ef5a458 | 2014-10-29 16:02:55 -0700 | [diff] [blame] | 196 | @param forced_pdu_size: Use certain PDU size parameter instead of |
| 197 | calculating actual length of sequence. |
| 198 | @param invalid_request: Whether to send request with intentionally |
| 199 | invalid syntax for testing purposes (string to be prepended |
| 200 | to correct request). |
Artem Rakhov | f53eeec | 2014-09-24 21:15:58 -0700 | [diff] [blame] | 201 | |
| 202 | @return list of found attributes IDs and their values or Error Code |
| 203 | |
| 204 | """ |
| 205 | return self._proxy.service_search_attribute_request(uuids, |
| 206 | max_attr_byte_count, |
| 207 | attr_ids, |
Artem Rakhov | ef5a458 | 2014-10-29 16:02:55 -0700 | [diff] [blame] | 208 | preferred_size, |
| 209 | forced_pdu_size, |
| 210 | invalid_request) |
Artem Rakhov | f53eeec | 2014-09-24 21:15:58 -0700 | [diff] [blame] | 211 | |
| 212 | |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 213 | def create_host_from(device_host, args=None): |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 214 | """Creates a host object for the Tester associated with a DUT. |
| 215 | |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 216 | The IP address or the hostname can be specified in the 'tester' member of |
| 217 | the argument dictionary. When not present it is derived from the hostname |
| 218 | of the DUT by appending '-router' to the first part. |
| 219 | |
| 220 | Will raise an exception if there isn't a tester for the DUT, or if the DUT |
| 221 | is specified as an IP address and thus the hostname cannot be derived. |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 222 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 223 | @param device_host: Autotest host object for the DUT. |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 224 | @param args: Dictionary of arguments passed to the test. |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 225 | |
| 226 | @return Autotest host object for the Tester. |
| 227 | |
| 228 | """ |
| 229 | |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 230 | def is_ip_address(hostname): |
| 231 | try: |
| 232 | socket.inet_aton(hostname) |
| 233 | return True |
| 234 | except socket.error: |
| 235 | return False |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 236 | |
Scott James Remnant | 2562204 | 2014-12-05 11:20:37 -0800 | [diff] [blame] | 237 | try: |
| 238 | tester_hostname = args['tester'] |
| 239 | except KeyError: |
| 240 | device_hostname = device_host.hostname |
| 241 | if is_ip_address(device_hostname): |
| 242 | raise error.TestError("Remote host cannot be an IP address unless " |
| 243 | "tester specified with --args tester=IP") |
| 244 | |
| 245 | parts = device_hostname.split('.') |
| 246 | parts[0] = parts[0] + '-router' |
| 247 | tester_hostname = '.'.join(parts) |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 248 | |
| 249 | return hosts.create_host(tester_hostname) |