blob: 26ecad415ef87b65bdd8129dd08a65bfede83528 [file] [log] [blame]
tturney1bdf77d2015-12-28 17:46:13 -08001#/usr/bin/env python3.4
2#
3# Copyright (C) 2016 The Android Open Source Project
Ang Li73697b32015-12-03 00:41:53 +00004#
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 Li73697b32015-12-03 00:41:53 +000016"""
17Test script to execute Bluetooth basic functionality test cases.
18This test was designed to be run in a shield box.
19"""
20
tturney0ab6f012016-05-09 13:59:27 -070021from contextlib import suppress
Ang Li73697b32015-12-03 00:41:53 +000022import threading
23import time
24
25from queue import Empty
26from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
tturney24506942016-08-23 10:53:17 -070027from acts.test_utils.bt.bt_test_utils import clear_bonded_devices
Ang Li73697b32015-12-03 00:41:53 +000028from acts.test_utils.bt.bt_test_utils import log_energy_info
tturneye3170f02016-05-19 14:37:00 -070029from acts.test_utils.bt.bt_test_utils import kill_bluetooth_process
tturney24506942016-08-23 10:53:17 -070030from acts.test_utils.bt.bt_test_utils import orchestrate_rfcomm_connection
Ang Li73697b32015-12-03 00:41:53 +000031from acts.test_utils.bt.bt_test_utils import reset_bluetooth
Ang Li73697b32015-12-03 00:41:53 +000032from acts.test_utils.bt.bt_test_utils import setup_multiple_devices_for_bt_test
33from acts.test_utils.bt.bt_test_utils import take_btsnoop_logs
tturney2621d7c2016-05-20 16:19:59 -070034from acts.test_utils.bt.bt_test_utils import write_read_verify_data
tturney96970fd2016-08-31 17:36:09 -070035from acts.test_utils.bt.bt_test_utils import verify_server_and_client_connected
Ang Li73697b32015-12-03 00:41:53 +000036
tturney24506942016-08-23 10:53:17 -070037from acts.test_utils.bt.BtEnum import RfcommUuid
38
Ang Li73697b32015-12-03 00:41:53 +000039
tturney0ab6f012016-05-09 13:59:27 -070040class RfcommTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000041 default_timeout = 10
tturney0ab6f012016-05-09 13:59:27 -070042 rf_client_th = 0
Ang Li73697b32015-12-03 00:41:53 +000043 scan_discovery_time = 5
tturneyc12b6ec2016-03-30 13:39:40 -070044 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 Li73697b32015-12-03 00:41:53 +000049
50 def __init__(self, controllers):
51 BluetoothBaseTest.__init__(self, controllers)
tturney0ab6f012016-05-09 13:59:27 -070052 self.client_ad = self.android_devices[0]
53 self.server_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000054
Ang Li73697b32015-12-03 00:41:53 +000055 def setup_class(self):
tturney1ce8dc62016-02-18 09:55:53 -080056 return setup_multiple_devices_for_bt_test(self.android_devices)
Ang Li73697b32015-12-03 00:41:53 +000057
58 def setup_test(self):
tturney24506942016-08-23 10:53:17 -070059 for a in self.android_devices:
60 if not clear_bonded_devices(a):
61 return False
tturney1ce8dc62016-02-18 09:55:53 -080062 self.log.debug(log_energy_info(self.android_devices, "Start"))
63 for a in self.android_devices:
64 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000065 return True
66
67 def teardown_test(self):
tturney24506942016-08-23 10:53:17 -070068 self.client_ad.droid.bluetoothRfcommCloseClientSocket()
69 self.server_ad.droid.bluetoothRfcommCloseServerSocket()
tturney1ce8dc62016-02-18 09:55:53 -080070 self.log.debug(log_energy_info(self.android_devices, "End"))
Ang Li73697b32015-12-03 00:41:53 +000071 return True
72
73 def on_fail(self, test_name, begin_time):
tturney1ce8dc62016-02-18 09:55:53 -080074 take_btsnoop_logs(self.android_devices, self, test_name)
75 reset_bluetooth(self.android_devices)
Ang Li73697b32015-12-03 00:41:53 +000076
77 def teardown_test(self):
tturney96970fd2016-08-31 17:36:09 -070078 if verify_server_and_client_connected(self.client_ad, self.server_ad):
tturney0ab6f012016-05-09 13:59:27 -070079 self.client_ad.droid.bluetoothRfcommStop()
80 self.server_ad.droid.bluetoothRfcommStop()
Ang Li73697b32015-12-03 00:41:53 +000081
82 @BluetoothBaseTest.bt_test_wrap
tturney17e6f862016-06-23 10:52:40 -070083 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 """
tturney24506942016-08-23 10:53:17 -0700104 if not orchestrate_rfcomm_connection(self.client_ad, self.server_ad):
tturney17e6f862016-06-23 10:52:40 -0700105 return False
106
107 self.client_ad.droid.bluetoothRfcommStop()
108 self.server_ad.droid.bluetoothRfcommStop()
109 return True
110
tturney17e6f862016-06-23 10:52:40 -0700111 @BluetoothBaseTest.bt_test_wrap
tturney0ab6f012016-05-09 13:59:27 -0700112 def test_rfcomm_connection_write_ascii(self):
113 """Test bluetooth RFCOMM writing and reading ascii data
Ang Li73697b32015-12-03 00:41:53 +0000114
tturney0ab6f012016-05-09 13:59:27 -0700115 Test RFCOMM though establishing a connection.
Ang Li73697b32015-12-03 00:41:53 +0000116
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.
tturney0ab6f012016-05-09 13:59:27 -0700122 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 Li73697b32015-12-03 00:41:53 +0000125
126 Expected Result:
127 RFCOMM connection is established then disconnected succcessfully.
128
129 Returns:
130 Pass if True
131 Fail if False
132
tturney0ab6f012016-05-09 13:59:27 -0700133 TAGS: Classic, RFCOMM
Ang Li73697b32015-12-03 00:41:53 +0000134 Priority: 1
135 """
tturney24506942016-08-23 10:53:17 -0700136 if not orchestrate_rfcomm_connection(self.client_ad, self.server_ad):
tturneye3170f02016-05-19 14:37:00 -0700137 return False
138 if not write_read_verify_data(self.client_ad, self.server_ad,
139 self.message, False):
140 return False
tturney96970fd2016-08-31 17:36:09 -0700141 if not verify_server_and_client_connected(self.client_ad,
142 self.server_ad):
Ang Li73697b32015-12-03 00:41:53 +0000143 return False
tturney0ab6f012016-05-09 13:59:27 -0700144
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 """
tturney24506942016-08-23 10:53:17 -0700174 if not orchestrate_rfcomm_connection(self.client_ad, self.server_ad):
tturneye3170f02016-05-19 14:37:00 -0700175 return False
tturney0ab6f012016-05-09 13:59:27 -0700176 binary_message = "11010101"
tturneye3170f02016-05-19 14:37:00 -0700177 if not write_read_verify_data(self.client_ad, self.server_ad,
178 binary_message, True):
179 return False
tturney96970fd2016-08-31 17:36:09 -0700180
181 if not verify_server_and_client_connected(self.client_ad,
182 self.server_ad):
tturney0ab6f012016-05-09 13:59:27 -0700183 return False
184
tturney1ce8dc62016-02-18 09:55:53 -0800185 self.client_ad.droid.bluetoothRfcommStop()
186 self.server_ad.droid.bluetoothRfcommStop()
Ang Li73697b32015-12-03 00:41:53 +0000187 return True
tturney96970fd2016-08-31 17:36:09 -0700188
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