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 |
| 7 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 8 | from autotest_lib.client.cros import constants |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 9 | from autotest_lib.server import autotest, hosts |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class BluetoothTester(object): |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 13 | """BluetoothTester is a thin layer of logic over a remote tester. |
| 14 | |
| 15 | The Autotest host object representing the remote tester, passed to this |
| 16 | class on initialization, can be accessed from its host property. |
| 17 | |
| 18 | """ |
| 19 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 20 | |
| 21 | XMLRPC_BRINGUP_TIMEOUT_SECONDS = 60 |
| 22 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 23 | def __init__(self, tester_host): |
| 24 | """Construct a BluetoothTester. |
| 25 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 26 | @param tester_host: host object representing a remote host. |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 27 | |
| 28 | """ |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 29 | self.host = tester_host |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 30 | # Make sure the client library is on the device so that the proxy code |
| 31 | # is there when we try to call it. |
| 32 | client_at = autotest.Autotest(self.host) |
| 33 | client_at.install() |
| 34 | # Start up the XML-RPC proxy on the tester. |
| 35 | self._proxy = self.host.xmlrpc_connect( |
| 36 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_COMMAND, |
| 37 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_PORT, |
| 38 | command_name= |
| 39 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_CLEANUP_PATTERN, |
| 40 | ready_test_name= |
| 41 | constants.BLUETOOTH_TESTER_XMLRPC_SERVER_READY_METHOD, |
| 42 | timeout_seconds=self.XMLRPC_BRINGUP_TIMEOUT_SECONDS) |
| 43 | |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 44 | |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 45 | def setup(self, profile): |
| 46 | """Set up the tester with the given profile. |
| 47 | |
| 48 | @param profile: Profile to use for this test, valid values are: |
| 49 | computer - a standard computer profile |
| 50 | |
| 51 | @return True on success, False otherwise. |
| 52 | |
| 53 | """ |
| 54 | return self._proxy.setup(profile) |
| 55 | |
| 56 | |
Scott James Remnant | aec4edd | 2013-08-26 18:47:11 -0700 | [diff] [blame] | 57 | def set_discoverable(self, discoverable, timeout=0): |
| 58 | """Set the discoverable state of the controller. |
| 59 | |
| 60 | @param discoverable: Whether controller should be discoverable. |
| 61 | @param timeout: Timeout in seconds before disabling discovery again, |
| 62 | ignored when discoverable is False, must not be zero when |
| 63 | discoverable is True. |
| 64 | |
| 65 | @return True on success, False otherwise. |
| 66 | |
| 67 | """ |
| 68 | return self._proxy.set_discoverable(discoverable, timeout) |
| 69 | |
| 70 | |
| 71 | def read_info(self): |
| 72 | """Read the adapter information from the Kernel. |
| 73 | |
| 74 | @return the information as a JSON-encoded tuple of: |
| 75 | ( address, bluetooth_version, manufacturer_id, |
| 76 | supported_settings, current_settings, class_of_device, |
| 77 | name, short_name ) |
| 78 | |
| 79 | """ |
| 80 | return json.loads(self._proxy.read_info()) |
| 81 | |
| 82 | |
Scott James Remnant | da9f43c | 2013-08-07 17:53:14 -0700 | [diff] [blame] | 83 | def discover_devices(self, br_edr=True, le_public=True, le_random=True): |
| 84 | """Discover remote devices. |
| 85 | |
| 86 | Activates device discovery and collects the set of devices found, |
| 87 | returning them as a list. |
| 88 | |
| 89 | @param br_edr: Whether to detect BR/EDR devices. |
| 90 | @param le_public: Whether to detect LE Public Address devices. |
| 91 | @param le_random: Whether to detect LE Random Address devices. |
| 92 | |
| 93 | @return List of devices found as tuples with the format |
| 94 | (address, address_type, rssi, flags, base64-encoded eirdata), |
| 95 | or False if discovery could not be started. |
| 96 | |
| 97 | """ |
| 98 | devices = self._proxy.discover_devices(br_edr, le_public, le_random) |
| 99 | if devices == False: |
| 100 | return False |
| 101 | |
| 102 | return ( |
| 103 | (address, address_type, rssi, flags, |
| 104 | base64.decodestring(eirdata)) |
| 105 | for address, address_type, rssi, flags, eirdata |
| 106 | in json.loads(devices) |
| 107 | ) |
| 108 | |
| 109 | |
Scott James Remnant | 4dcd73f | 2013-07-22 15:00:24 -0700 | [diff] [blame] | 110 | def close(self): |
| 111 | """Tear down state associated with the client.""" |
| 112 | # This kills the RPC server. |
Scott James Remnant | 1c72d7a | 2013-07-29 15:00:04 -0700 | [diff] [blame] | 113 | self.host.close() |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 114 | |
| 115 | |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 116 | def connect(self, address): |
| 117 | """Connect to device with the given address |
| 118 | |
| 119 | @param address: Bluetooth address. |
| 120 | |
| 121 | """ |
| 122 | self._proxy.connect(address) |
| 123 | |
| 124 | |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 125 | def service_search_request(self, uuids, max_rec_cnt, preferred_size=32, |
Artem Rakhov | f42dc83 | 2014-03-03 12:19:13 -0800 | [diff] [blame^] | 126 | forced_pdu_size=None, invalid_request=False): |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 127 | """Send a Service Search Request |
| 128 | |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 129 | @param uuids: List of UUIDs (as integers) to look for. |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 130 | @param max_rec_cnt: Maximum count of returned service records. |
Artem Rakhov | ca443b5 | 2014-01-10 17:18:24 -0800 | [diff] [blame] | 131 | @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] | 132 | @param forced_pdu_size: Use certain PDU size parameter instead of |
| 133 | calculating actual length of sequence. |
Artem Rakhov | f42dc83 | 2014-03-03 12:19:13 -0800 | [diff] [blame^] | 134 | @param invalid_request: Whether to send request with intentionally |
| 135 | invalid syntax for testing purposes (bool flag). |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 136 | |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 137 | @return list of found services' service record handles or Error Code |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 138 | |
| 139 | """ |
Artem Rakhov | ca443b5 | 2014-01-10 17:18:24 -0800 | [diff] [blame] | 140 | return self._proxy.service_search_request(uuids, max_rec_cnt, |
Artem Rakhov | ba13038 | 2014-02-26 17:39:57 -0800 | [diff] [blame] | 141 | preferred_size, |
Artem Rakhov | f42dc83 | 2014-03-03 12:19:13 -0800 | [diff] [blame^] | 142 | forced_pdu_size, |
| 143 | invalid_request) |
Artem Rakhov | 448d52c | 2013-12-03 16:38:36 -0800 | [diff] [blame] | 144 | |
| 145 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 146 | def create_host_from(device_host): |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 147 | """Creates a host object for the Tester associated with a DUT. |
| 148 | |
| 149 | Will raise an exception if there isn't a tester for the DUT. |
| 150 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 151 | @param device_host: Autotest host object for the DUT. |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 152 | |
| 153 | @return Autotest host object for the Tester. |
| 154 | |
| 155 | """ |
| 156 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 157 | device_hostname = device_host.hostname |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 158 | |
Scott James Remnant | 8d2cbf3 | 2013-11-12 11:00:25 -0800 | [diff] [blame] | 159 | parts = device_hostname.split('.') |
Scott James Remnant | 8559307 | 2014-02-25 17:43:16 -0800 | [diff] [blame] | 160 | parts[0] = parts[0] + '-router' |
Scott James Remnant | 1ca2e0e | 2013-07-31 16:49:07 -0700 | [diff] [blame] | 161 | tester_hostname = '.'.join(parts) |
| 162 | |
| 163 | return hosts.create_host(tester_hostname) |