blob: a445d424a8c57d6fc0180499d045e779e4ec4a25 [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
Ang Li73697b32015-12-03 00:41:53 +000027from acts.test_utils.bt.bt_test_utils import log_energy_info
tturneye3170f02016-05-19 14:37:00 -070028from acts.test_utils.bt.bt_test_utils import kill_bluetooth_process
Ang Li73697b32015-12-03 00:41:53 +000029from acts.test_utils.bt.bt_test_utils import reset_bluetooth
30from acts.test_utils.bt.bt_test_utils import rfcomm_accept
31from acts.test_utils.bt.bt_test_utils import rfcomm_connect
32from 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
Ang Li73697b32015-12-03 00:41:53 +000035
36
tturney0ab6f012016-05-09 13:59:27 -070037class RfcommTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000038 default_timeout = 10
tturney0ab6f012016-05-09 13:59:27 -070039 rf_client_th = 0
Ang Li73697b32015-12-03 00:41:53 +000040 scan_discovery_time = 5
41 thread_list = []
tturneyc12b6ec2016-03-30 13:39:40 -070042 message = (
43 "Space: the final frontier. These are the voyages of "
44 "the starship Enterprise. Its continuing mission: to explore "
45 "strange new worlds, to seek out new life and new civilizations,"
46 " to boldly go where no man has gone before.")
Ang Li73697b32015-12-03 00:41:53 +000047
48 def __init__(self, controllers):
49 BluetoothBaseTest.__init__(self, controllers)
tturney0ab6f012016-05-09 13:59:27 -070050 self.client_ad = self.android_devices[0]
51 self.server_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000052
53 def _clear_bonded_devices(self):
tturney1ce8dc62016-02-18 09:55:53 -080054 for a in self.android_devices:
55 bonded_device_list = a.droid.bluetoothGetBondedDevices()
Ang Li73697b32015-12-03 00:41:53 +000056 for device in bonded_device_list:
tturney1ce8dc62016-02-18 09:55:53 -080057 a.droid.bluetoothUnbond(device['address'])
Ang Li73697b32015-12-03 00:41:53 +000058
59 def setup_class(self):
tturney1ce8dc62016-02-18 09:55:53 -080060 return setup_multiple_devices_for_bt_test(self.android_devices)
Ang Li73697b32015-12-03 00:41:53 +000061
62 def setup_test(self):
63 self._clear_bonded_devices()
tturney1ce8dc62016-02-18 09:55:53 -080064 self.log.debug(log_energy_info(self.android_devices, "Start"))
65 for a in self.android_devices:
66 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000067 return True
68
69 def teardown_test(self):
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):
tturney0ab6f012016-05-09 13:59:27 -070078 with suppress(Exception):
tturney0ab6f012016-05-09 13:59:27 -070079 self.client_ad.droid.bluetoothRfcommStop()
80 self.server_ad.droid.bluetoothRfcommStop()
tturneye3170f02016-05-19 14:37:00 -070081 self.client_ad.droid.bluetoothRfcommCloseSocket()
82 self.server_ad.droid.bluetoothRfcommCloseSocket()
83 for thread in self.thread_list:
84 thread.join()
85 self.thread_list.clear()
Ang Li73697b32015-12-03 00:41:53 +000086
87 def orchestrate_rfcomm_connect(self, server_mac):
tturneyc12b6ec2016-03-30 13:39:40 -070088 accept_thread = threading.Thread(target=rfcomm_accept,
tturneye3170f02016-05-19 14:37:00 -070089 args=(self.server_ad, ))
Ang Li73697b32015-12-03 00:41:53 +000090 self.thread_list.append(accept_thread)
91 accept_thread.start()
tturneye3170f02016-05-19 14:37:00 -070092 connect_thread = threading.Thread(target=rfcomm_connect,
93 args=(self.client_ad, server_mac))
tturney0ab6f012016-05-09 13:59:27 -070094 self.rf_client_th = connect_thread
Ang Li73697b32015-12-03 00:41:53 +000095 self.thread_list.append(connect_thread)
96 connect_thread.start()
tturneye3170f02016-05-19 14:37:00 -070097 for thread in self.thread_list:
98 thread.join()
99 end_time = time.time() + self.default_timeout
100 result = False
101 while time.time() < end_time:
102 if len(self.client_ad.droid.bluetoothRfcommActiveConnections(
103 )) > 0:
104 self.log.info("RFCOMM Connection Active")
105 return True
106 self.log.error("Failed to establish an RFCOMM connection")
107 return False
Ang Li73697b32015-12-03 00:41:53 +0000108
109 @BluetoothBaseTest.bt_test_wrap
tturney17e6f862016-06-23 10:52:40 -0700110 def test_rfcomm_connection(self):
111 """Test bluetooth RFCOMM connection
112
113 Test RFCOMM though establishing a basic connection.
114
115 Steps:
116 1. Get the mac address of the server device.
117 2. Establish an RFCOMM connection from the client to the server AD.
118 3. Verify that the RFCOMM connection is active from both the client and
119 server.
120
121 Expected Result:
122 RFCOMM connection is established then disconnected succcessfully.
123
124 Returns:
125 Pass if True
126 Fail if False
127
128 TAGS: Classic, RFCOMM
129 Priority: 1
130 """
131 server_mac = self.server_ad.droid.bluetoothGetLocalAddress()
132 if not self.orchestrate_rfcomm_connect(server_mac):
133 return False
134
135 self.client_ad.droid.bluetoothRfcommStop()
136 self.server_ad.droid.bluetoothRfcommStop()
137 return True
138
139
140 @BluetoothBaseTest.bt_test_wrap
tturney0ab6f012016-05-09 13:59:27 -0700141 def test_rfcomm_connection_write_ascii(self):
142 """Test bluetooth RFCOMM writing and reading ascii data
Ang Li73697b32015-12-03 00:41:53 +0000143
tturney0ab6f012016-05-09 13:59:27 -0700144 Test RFCOMM though establishing a connection.
Ang Li73697b32015-12-03 00:41:53 +0000145
146 Steps:
147 1. Get the mac address of the server device.
148 2. Establish an RFCOMM connection from the client to the server AD.
149 3. Verify that the RFCOMM connection is active from both the client and
150 server.
tturney0ab6f012016-05-09 13:59:27 -0700151 4. Write data from the client and read received data from the server.
152 5. Verify data matches from client and server
153 6. Disconnect the RFCOMM connection.
Ang Li73697b32015-12-03 00:41:53 +0000154
155 Expected Result:
156 RFCOMM connection is established then disconnected succcessfully.
157
158 Returns:
159 Pass if True
160 Fail if False
161
tturney0ab6f012016-05-09 13:59:27 -0700162 TAGS: Classic, RFCOMM
Ang Li73697b32015-12-03 00:41:53 +0000163 Priority: 1
164 """
tturney0ab6f012016-05-09 13:59:27 -0700165 server_mac = self.server_ad.droid.bluetoothGetLocalAddress()
tturneye3170f02016-05-19 14:37:00 -0700166 if not self.orchestrate_rfcomm_connect(server_mac):
167 return False
168 if not write_read_verify_data(self.client_ad, self.server_ad,
169 self.message, False):
170 return False
tturney1ce8dc62016-02-18 09:55:53 -0800171 if len(self.server_ad.droid.bluetoothRfcommActiveConnections()) == 0:
Ang Li73697b32015-12-03 00:41:53 +0000172 self.log.info("No rfcomm connections found on server.")
173 return False
tturney1ce8dc62016-02-18 09:55:53 -0800174 if len(self.client_ad.droid.bluetoothRfcommActiveConnections()) == 0:
Ang Li73697b32015-12-03 00:41:53 +0000175 self.log.info("no rfcomm connections found on client.")
176 return False
tturney0ab6f012016-05-09 13:59:27 -0700177
178 self.client_ad.droid.bluetoothRfcommStop()
179 self.server_ad.droid.bluetoothRfcommStop()
180 return True
181
182 @BluetoothBaseTest.bt_test_wrap
183 def test_rfcomm_write_binary(self):
184 """Test bluetooth RFCOMM writing and reading binary data
185
186 Test profile though establishing an RFCOMM connection.
187
188 Steps:
189 1. Get the mac address of the server device.
190 2. Establish an RFCOMM connection from the client to the server AD.
191 3. Verify that the RFCOMM connection is active from both the client and
192 server.
193 4. Write data from the client and read received data from the server.
194 5. Verify data matches from client and server
195 6. Disconnect the RFCOMM connection.
196
197 Expected Result:
198 RFCOMM connection is established then disconnected succcessfully.
199
200 Returns:
201 Pass if True
202 Fail if False
203
204 TAGS: Classic, RFCOMM
205 Priority: 1
206 """
207 server_mac = self.server_ad.droid.bluetoothGetLocalAddress()
tturneye3170f02016-05-19 14:37:00 -0700208 if not self.orchestrate_rfcomm_connect(server_mac):
209 return False
tturney0ab6f012016-05-09 13:59:27 -0700210 binary_message = "11010101"
tturneye3170f02016-05-19 14:37:00 -0700211 if not write_read_verify_data(self.client_ad, self.server_ad,
212 binary_message, True):
213 return False
tturney0ab6f012016-05-09 13:59:27 -0700214 if len(self.server_ad.droid.bluetoothRfcommActiveConnections()) == 0:
215 self.log.info("No rfcomm connections found on server.")
216 return False
217 if len(self.client_ad.droid.bluetoothRfcommActiveConnections()) == 0:
218 self.log.info("no rfcomm connections found on client.")
219 return False
220
tturney1ce8dc62016-02-18 09:55:53 -0800221 self.client_ad.droid.bluetoothRfcommStop()
222 self.server_ad.droid.bluetoothRfcommStop()
Ang Li73697b32015-12-03 00:41:53 +0000223 return True