tturney | 1bdf77d | 2015-12-28 17:46:13 -0800 | [diff] [blame] | 1 | #/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright (C) 2016 The Android Open Source Project |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 6 | # use this file except in compliance with the License. You may obtain a copy of |
| 7 | # the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations under |
| 15 | # the License. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 16 | """ |
| 17 | Test script to execute Bluetooth basic functionality test cases. |
| 18 | This test was designed to be run in a shield box. |
| 19 | """ |
| 20 | |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 21 | from contextlib import suppress |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 22 | import threading |
| 23 | import time |
| 24 | |
| 25 | from queue import Empty |
| 26 | from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 27 | from acts.test_utils.bt.bt_test_utils import clear_bonded_devices |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 28 | from acts.test_utils.bt.bt_test_utils import log_energy_info |
tturney | e3170f0 | 2016-05-19 14:37:00 -0700 | [diff] [blame] | 29 | from acts.test_utils.bt.bt_test_utils import kill_bluetooth_process |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 30 | from acts.test_utils.bt.bt_test_utils import orchestrate_rfcomm_connection |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 31 | from acts.test_utils.bt.bt_test_utils import reset_bluetooth |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 32 | from acts.test_utils.bt.bt_test_utils import setup_multiple_devices_for_bt_test |
| 33 | from acts.test_utils.bt.bt_test_utils import take_btsnoop_logs |
tturney | 2621d7c | 2016-05-20 16:19:59 -0700 | [diff] [blame] | 34 | from acts.test_utils.bt.bt_test_utils import write_read_verify_data |
tturney | 96970fd | 2016-08-31 17:36:09 -0700 | [diff] [blame^] | 35 | from acts.test_utils.bt.bt_test_utils import verify_server_and_client_connected |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 36 | |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 37 | from acts.test_utils.bt.BtEnum import RfcommUuid |
| 38 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 39 | |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 40 | class RfcommTest(BluetoothBaseTest): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 41 | default_timeout = 10 |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 42 | rf_client_th = 0 |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 43 | scan_discovery_time = 5 |
tturney | c12b6ec | 2016-03-30 13:39:40 -0700 | [diff] [blame] | 44 | message = ( |
| 45 | "Space: the final frontier. These are the voyages of " |
| 46 | "the starship Enterprise. Its continuing mission: to explore " |
| 47 | "strange new worlds, to seek out new life and new civilizations," |
| 48 | " to boldly go where no man has gone before.") |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 49 | |
| 50 | def __init__(self, controllers): |
| 51 | BluetoothBaseTest.__init__(self, controllers) |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 52 | self.client_ad = self.android_devices[0] |
| 53 | self.server_ad = self.android_devices[1] |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 54 | |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 55 | def setup_class(self): |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 56 | return setup_multiple_devices_for_bt_test(self.android_devices) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 57 | |
| 58 | def setup_test(self): |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 59 | for a in self.android_devices: |
| 60 | if not clear_bonded_devices(a): |
| 61 | return False |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 62 | self.log.debug(log_energy_info(self.android_devices, "Start")) |
| 63 | for a in self.android_devices: |
| 64 | a.ed.clear_all_events() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 65 | return True |
| 66 | |
| 67 | def teardown_test(self): |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 68 | self.client_ad.droid.bluetoothRfcommCloseClientSocket() |
| 69 | self.server_ad.droid.bluetoothRfcommCloseServerSocket() |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 70 | self.log.debug(log_energy_info(self.android_devices, "End")) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 71 | return True |
| 72 | |
| 73 | def on_fail(self, test_name, begin_time): |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 74 | take_btsnoop_logs(self.android_devices, self, test_name) |
| 75 | reset_bluetooth(self.android_devices) |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 76 | |
| 77 | def teardown_test(self): |
tturney | 96970fd | 2016-08-31 17:36:09 -0700 | [diff] [blame^] | 78 | if verify_server_and_client_connected(self.client_ad, self.server_ad): |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 79 | self.client_ad.droid.bluetoothRfcommStop() |
| 80 | self.server_ad.droid.bluetoothRfcommStop() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 81 | |
| 82 | @BluetoothBaseTest.bt_test_wrap |
tturney | 17e6f86 | 2016-06-23 10:52:40 -0700 | [diff] [blame] | 83 | def test_rfcomm_connection(self): |
| 84 | """Test bluetooth RFCOMM connection |
| 85 | |
| 86 | Test RFCOMM though establishing a basic connection. |
| 87 | |
| 88 | Steps: |
| 89 | 1. Get the mac address of the server device. |
| 90 | 2. Establish an RFCOMM connection from the client to the server AD. |
| 91 | 3. Verify that the RFCOMM connection is active from both the client and |
| 92 | server. |
| 93 | |
| 94 | Expected Result: |
| 95 | RFCOMM connection is established then disconnected succcessfully. |
| 96 | |
| 97 | Returns: |
| 98 | Pass if True |
| 99 | Fail if False |
| 100 | |
| 101 | TAGS: Classic, RFCOMM |
| 102 | Priority: 1 |
| 103 | """ |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 104 | if not orchestrate_rfcomm_connection(self.client_ad, self.server_ad): |
tturney | 17e6f86 | 2016-06-23 10:52:40 -0700 | [diff] [blame] | 105 | return False |
| 106 | |
| 107 | self.client_ad.droid.bluetoothRfcommStop() |
| 108 | self.server_ad.droid.bluetoothRfcommStop() |
| 109 | return True |
| 110 | |
tturney | 17e6f86 | 2016-06-23 10:52:40 -0700 | [diff] [blame] | 111 | @BluetoothBaseTest.bt_test_wrap |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 112 | def test_rfcomm_connection_write_ascii(self): |
| 113 | """Test bluetooth RFCOMM writing and reading ascii data |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 114 | |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 115 | Test RFCOMM though establishing a connection. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 116 | |
| 117 | Steps: |
| 118 | 1. Get the mac address of the server device. |
| 119 | 2. Establish an RFCOMM connection from the client to the server AD. |
| 120 | 3. Verify that the RFCOMM connection is active from both the client and |
| 121 | server. |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 122 | 4. Write data from the client and read received data from the server. |
| 123 | 5. Verify data matches from client and server |
| 124 | 6. Disconnect the RFCOMM connection. |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 125 | |
| 126 | Expected Result: |
| 127 | RFCOMM connection is established then disconnected succcessfully. |
| 128 | |
| 129 | Returns: |
| 130 | Pass if True |
| 131 | Fail if False |
| 132 | |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 133 | TAGS: Classic, RFCOMM |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 134 | Priority: 1 |
| 135 | """ |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 136 | if not orchestrate_rfcomm_connection(self.client_ad, self.server_ad): |
tturney | e3170f0 | 2016-05-19 14:37:00 -0700 | [diff] [blame] | 137 | return False |
| 138 | if not write_read_verify_data(self.client_ad, self.server_ad, |
| 139 | self.message, False): |
| 140 | return False |
tturney | 96970fd | 2016-08-31 17:36:09 -0700 | [diff] [blame^] | 141 | if not verify_server_and_client_connected(self.client_ad, |
| 142 | self.server_ad): |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 143 | return False |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 144 | |
| 145 | self.client_ad.droid.bluetoothRfcommStop() |
| 146 | self.server_ad.droid.bluetoothRfcommStop() |
| 147 | return True |
| 148 | |
| 149 | @BluetoothBaseTest.bt_test_wrap |
| 150 | def test_rfcomm_write_binary(self): |
| 151 | """Test bluetooth RFCOMM writing and reading binary data |
| 152 | |
| 153 | Test profile though establishing an RFCOMM connection. |
| 154 | |
| 155 | Steps: |
| 156 | 1. Get the mac address of the server device. |
| 157 | 2. Establish an RFCOMM connection from the client to the server AD. |
| 158 | 3. Verify that the RFCOMM connection is active from both the client and |
| 159 | server. |
| 160 | 4. Write data from the client and read received data from the server. |
| 161 | 5. Verify data matches from client and server |
| 162 | 6. Disconnect the RFCOMM connection. |
| 163 | |
| 164 | Expected Result: |
| 165 | RFCOMM connection is established then disconnected succcessfully. |
| 166 | |
| 167 | Returns: |
| 168 | Pass if True |
| 169 | Fail if False |
| 170 | |
| 171 | TAGS: Classic, RFCOMM |
| 172 | Priority: 1 |
| 173 | """ |
tturney | 2450694 | 2016-08-23 10:53:17 -0700 | [diff] [blame] | 174 | if not orchestrate_rfcomm_connection(self.client_ad, self.server_ad): |
tturney | e3170f0 | 2016-05-19 14:37:00 -0700 | [diff] [blame] | 175 | return False |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 176 | binary_message = "11010101" |
tturney | e3170f0 | 2016-05-19 14:37:00 -0700 | [diff] [blame] | 177 | if not write_read_verify_data(self.client_ad, self.server_ad, |
| 178 | binary_message, True): |
| 179 | return False |
tturney | 96970fd | 2016-08-31 17:36:09 -0700 | [diff] [blame^] | 180 | |
| 181 | if not verify_server_and_client_connected(self.client_ad, |
| 182 | self.server_ad): |
tturney | 0ab6f01 | 2016-05-09 13:59:27 -0700 | [diff] [blame] | 183 | return False |
| 184 | |
tturney | 1ce8dc6 | 2016-02-18 09:55:53 -0800 | [diff] [blame] | 185 | self.client_ad.droid.bluetoothRfcommStop() |
| 186 | self.server_ad.droid.bluetoothRfcommStop() |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 187 | return True |
tturney | 96970fd | 2016-08-31 17:36:09 -0700 | [diff] [blame^] | 188 | |
| 189 | @BluetoothBaseTest.bt_test_wrap |
| 190 | def test_rfcomm_accept_timeout(self): |
| 191 | """Test bluetooth RFCOMM accept socket timeout |
| 192 | |
| 193 | Verify that RFCOMM connections are unsuccessful if |
| 194 | the socket timeout is exceeded. |
| 195 | |
| 196 | Steps: |
| 197 | 1. Get the mac address of the server device. |
| 198 | 2. Establish an RFCOMM connection from the client to the server AD. |
| 199 | 3. Verify that the RFCOMM connection is active from both the client and |
| 200 | server. |
| 201 | |
| 202 | Expected Result: |
| 203 | RFCOMM connection is established then disconnected succcessfully. |
| 204 | |
| 205 | Returns: |
| 206 | Pass if True |
| 207 | Fail if False |
| 208 | |
| 209 | TAGS: Classic, RFCOMM |
| 210 | Priority: 1 |
| 211 | """ |
| 212 | # Socket timeout set to 999ms |
| 213 | short_socket_timeout = 999 |
| 214 | # Wait time in seconds before attempting a connection |
| 215 | wait_time_before_connect_attempt = 1 |
| 216 | self.server_ad.droid.bluetoothStartPairingHelper() |
| 217 | self.client_ad.droid.bluetoothStartPairingHelper() |
| 218 | self.server_ad.droid.bluetoothRfcommBeginAcceptThread( |
| 219 | RfcommUuid.DEFAULT_UUID.value, short_socket_timeout) |
| 220 | time.sleep(wait_time_before_connect_attempt) |
| 221 | |
| 222 | # Try to connect |
| 223 | self.client_ad.droid.bluetoothRfcommBeginConnectThread( |
| 224 | self.server_ad.droid.bluetoothGetLocalAddress()) |
| 225 | # Give the connection time to fail |
| 226 | #time.sleep(self.default_timeout) |
| 227 | time.sleep(2) |
| 228 | if verify_server_and_client_connected(self.client_ad, self.server_ad): |
| 229 | return False |
| 230 | self.log.info("No active connections found as expected") |
| 231 | # AcceptThread has finished, kill hanging ConnectThread |
| 232 | self.client_ad.droid.bluetoothRfcommKillConnThread() |
| 233 | reset_bluetooth(self.android_devices) |
| 234 | return True |