blob: 9a20ed66585684b1dfed43afa6aa3e4cb9cece77 [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"""
17This test script exercises background scan test scenarios.
18"""
19
20from queue import Empty
21
tturney11b65e42017-01-11 13:57:20 -080022from acts.test_decorators import test_tracker_info
Ang Li73697b32015-12-03 00:41:53 +000023from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
Ang Li73697b32015-12-03 00:41:53 +000024from acts.test_utils.bt.bt_test_utils import bluetooth_off
25from acts.test_utils.bt.bt_test_utils import bluetooth_on
26from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers
tturney69e3bb12017-10-26 14:15:05 -070027from acts.test_utils.bt.bt_test_utils import enable_bluetooth
Ang Li73697b32015-12-03 00:41:53 +000028from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
29from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
tturneya4ebb032017-07-24 12:14:45 -070030from acts.test_utils.bt.bt_constants import bluetooth_le_off
31from acts.test_utils.bt.bt_constants import bluetooth_le_on
32from acts.test_utils.bt.bt_constants import bt_adapter_states
tturney69e3bb12017-10-26 14:15:05 -070033from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
tturneya4ebb032017-07-24 12:14:45 -070034from acts.test_utils.bt.bt_constants import scan_result
35
36import time
Ang Li73697b32015-12-03 00:41:53 +000037
38
39class BleBackgroundScanTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000040 default_timeout = 10
41 max_scan_instances = 28
42 report_delay = 2000
43 scan_callbacks = []
44 adv_callbacks = []
45 active_scan_callback_list = []
46 active_adv_callback_list = []
47
48 def __init__(self, controllers):
49 BluetoothBaseTest.__init__(self, controllers)
tturney1ce8dc62016-02-18 09:55:53 -080050 self.scn_ad = self.android_devices[0]
51 self.adv_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000052
53 def setup_test(self):
tturney69e3bb12017-10-26 14:15:05 -070054 # Always start tests with Bluetooth enabled and BLE disabled.
55 enable_bluetooth(self.scn_ad.droid, self.scn_ad.ed)
56 self.scn_ad.droid.bluetoothDisableBLE()
tturney653b2732016-02-24 10:40:50 -080057 for a in self.android_devices:
58 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000059 return True
60
61 def teardown_test(self):
Ang Li73697b32015-12-03 00:41:53 +000062 cleanup_scanners_and_advertisers(
tturneyc12b6ec2016-03-30 13:39:40 -070063 self.scn_ad, self.active_adv_callback_list, self.adv_ad,
64 self.active_adv_callback_list)
Ang Li73697b32015-12-03 00:41:53 +000065 self.active_adv_callback_list = []
66 self.active_scan_callback_list = []
67
68 def _setup_generic_advertisement(self):
tturney69e3bb12017-10-26 14:15:05 -070069 self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True)
Ang Li73697b32015-12-03 00:41:53 +000070 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
tturney1ce8dc62016-02-18 09:55:53 -080071 self.adv_ad.droid)
tturneyc12b6ec2016-03-30 13:39:40 -070072 self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data,
73 adv_settings)
Ang Li73697b32015-12-03 00:41:53 +000074 self.active_adv_callback_list.append(adv_callback)
75
Ang Li73697b32015-12-03 00:41:53 +000076 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -080077 @test_tracker_info(uuid='4d13c3a8-1805-44ef-a92a-e385540767f1')
Ang Li73697b32015-12-03 00:41:53 +000078 def test_background_scan(self):
79 """Test generic background scan.
80
81 Tests LE background scan. The goal is to find scan results even though
82 Bluetooth is turned off.
83
84 Steps:
85 1. Setup an advertisement on dut1
86 2. Enable LE on the Bluetooth Adapter on dut0
87 3. Toggle BT off on dut1
88 4. Start a LE scan on dut0
89 5. Find the advertisement from dut1
90
91 Expected Result:
92 Find a advertisement from the scan instance.
93
94 Returns:
95 Pass if True
96 Fail if False
97
98 TAGS: LE, Advertising, Scanning, Background Scanning
99 Priority: 0
100 """
tturney69e3bb12017-10-26 14:15:05 -0700101 self.scn_ad.droid.bluetoothEnableBLE()
Ang Li73697b32015-12-03 00:41:53 +0000102 self._setup_generic_advertisement()
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700103 self.scn_ad.droid.bleSetScanSettingsScanMode(
104 ble_scan_settings_modes['low_latency'])
tturney69e3bb12017-10-26 14:15:05 -0700105 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
106 self.scn_ad.droid)
107 self.scn_ad.droid.bleSetScanFilterDeviceName(
108 self.adv_ad.droid.bluetoothGetLocalName())
109 self.scn_ad.droid.bleBuildScanFilter(filter_list)
tturney1ce8dc62016-02-18 09:55:53 -0800110 self.scn_ad.droid.bluetoothToggleState(False)
tturney0ab6f012016-05-09 13:59:27 -0700111 try:
112 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
113 except Empty:
114 self.log.error("Bluetooth Off event not found. Expected {}".format(
115 bluetooth_off))
116 return False
tturneyc12b6ec2016-03-30 13:39:40 -0700117 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
118 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700119 expected_event = scan_result.format(scan_callback)
120 try:
121 self.scn_ad.ed.pop_event(expected_event, self.default_timeout)
122 except Empty:
Marie Janssen1f333032016-10-25 16:49:35 -0700123 self.log.error("Scan Result event not found. Expected {}".format(
124 expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700125 return False
Ang Li73697b32015-12-03 00:41:53 +0000126 return True
127
128 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800129 @test_tracker_info(uuid='9c4577f8-5e06-4034-b977-285956734974')
Ang Li73697b32015-12-03 00:41:53 +0000130 def test_background_scan_ble_disabled(self):
131 """Test background LE scanning with LE disabled.
132
133 Tests LE background scan. The goal is to find scan results even though
134 Bluetooth is turned off.
135
136 Steps:
137 1. Setup an advertisement on dut1
138 2. Enable LE on the Bluetooth Adapter on dut0
139 3. Toggle BT off on dut1
140 4. Start a LE scan on dut0
141 5. Find the advertisement from dut1
142
143 Expected Result:
144 Find a advertisement from the scan instance.
145
146 Returns:
147 Pass if True
148 Fail if False
149
150 TAGS: LE, Advertising, Scanning, Background Scanning
151 Priority: 0
152 """
153 self._setup_generic_advertisement()
tturney1ce8dc62016-02-18 09:55:53 -0800154 self.scn_ad.droid.bluetoothEnableBLE()
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700155 self.scn_ad.droid.bleSetScanSettingsScanMode(
156 ble_scan_settings_modes['low_latency'])
tturney69e3bb12017-10-26 14:15:05 -0700157 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
158 self.scn_ad.droid)
159 self.scn_ad.droid.bleSetScanFilterDeviceName(
160 self.adv_ad.droid.bluetoothGetLocalName())
161 self.scn_ad.droid.bleBuildScanFilter(filter_list)
tturney1ce8dc62016-02-18 09:55:53 -0800162 self.scn_ad.droid.bluetoothToggleState(False)
tturney0ab6f012016-05-09 13:59:27 -0700163 try:
164 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
165 except Empty:
tturney69e3bb12017-10-26 14:15:05 -0700166 self.log.info(self.scn_ad.droid.bluetoothCheckState())
tturney0ab6f012016-05-09 13:59:27 -0700167 self.log.error("Bluetooth Off event not found. Expected {}".format(
168 bluetooth_off))
169 return False
Ang Li73697b32015-12-03 00:41:53 +0000170 try:
tturneyc12b6ec2016-03-30 13:39:40 -0700171 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
172 scan_callback)
tturney0ab6f012016-05-09 13:59:27 -0700173 expected_event = scan_result.format(scan_callback)
174 try:
175 self.scn_ad.ed.pop_event(expected_event, self.default_timeout)
176 except Empty:
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700177 self.log.error(
178 "Scan Result event not found. Expected {}".format(
179 expected_event))
tturney0ab6f012016-05-09 13:59:27 -0700180 return False
Ang Li73697b32015-12-03 00:41:53 +0000181 except Exception:
182 self.log.info(
183 "Was not able to start a background scan as expected.")
184 return True
Marie Janssen1f333032016-10-25 16:49:35 -0700185
186 @BluetoothBaseTest.bt_test_wrap
tturney11b65e42017-01-11 13:57:20 -0800187 @test_tracker_info(uuid='0bdd1764-3dc6-4a82-b041-76e48ed0f424')
Marie Janssen1f333032016-10-25 16:49:35 -0700188 def test_airplane_mode_disables_ble(self):
189 """Try to start LE mode in Airplane Mode.
190
191 This test will enable airplane mode, then attempt to start LE scanning
192 mode. This should result in bluetooth still being turned off, LE
193 not enabled.
194
195 Steps:
196 1. Start LE only mode.
197 2. Bluetooth should be in LE ONLY mode
198 2. Turn on airplane mode.
199 3. Bluetooth should be OFF
200 4. Try to start LE only mode.
201 5. Bluetooth should stay in OFF mode (LE only start should fail)
202 6. Turn off airplane mode.
203 7. Bluetooth should be OFF.
204
205 Expected Result:
206 No unexpected bluetooth state changes.
207
208 Returns:
209 Pass if True
210 Fail if False
211
212 TAGS: LE, Airplane
213 Priority: 1
214 """
215 ble_state_error_msg = "Bluetooth LE State not OK {}. Expected {} got {}"
216 # Enable BLE always available (effectively enabling BT in location)
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700217 self.scn_ad.shell.enable_ble_scanning()
tturney69e3bb12017-10-26 14:15:05 -0700218 self.scn_ad.droid.bluetoothEnableBLE()
Marie Janssen1f333032016-10-25 16:49:35 -0700219 self.scn_ad.droid.bluetoothToggleState(False)
220 try:
221 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
222 except Empty:
223 self.log.error("Bluetooth Off event not found. Expected {}".format(
224 bluetooth_off))
tturney69e3bb12017-10-26 14:15:05 -0700225 self.log.info(self.scn_ad.droid.bluetoothCheckState())
Marie Janssen1f333032016-10-25 16:49:35 -0700226 return False
227
228 # Sleep because LE turns off after the bluetooth off event fires
229 time.sleep(self.default_timeout)
230 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700231 if state != bt_adapter_states['ble_on']:
Marie Janssen1f333032016-10-25 16:49:35 -0700232 self.log.error(
tturney69e3bb12017-10-26 14:15:05 -0700233 ble_state_error_msg.format("after BT Disable",
tturneya4ebb032017-07-24 12:14:45 -0700234 bt_adapter_states['ble_on'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700235 return False
236
237 self.scn_ad.droid.bluetoothListenForBleStateChange()
238 self.scn_ad.droid.connectivityToggleAirplaneMode(True)
239 try:
tturneya4ebb032017-07-24 12:14:45 -0700240 self.scn_ad.ed.pop_event(bluetooth_le_off, self.default_timeout)
Marie Janssen1f333032016-10-25 16:49:35 -0700241 except Empty:
Ashutosh Singh6c71aa02018-05-14 15:51:14 -0700242 self.log.error(
243 "Bluetooth LE Off event not found. Expected {}".format(
244 bluetooth_le_off))
Marie Janssen1f333032016-10-25 16:49:35 -0700245 return False
246 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700247 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700248 self.log.error(
tturneya4ebb032017-07-24 12:14:45 -0700249 ble_state_error_msg.format("after Airplane Mode ON",
250 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700251 return False
252 result = self.scn_ad.droid.bluetoothEnableBLE()
253 if result:
254 self.log.error(
255 "Bluetooth Enable command succeded when it should have failed (in airplane mode)"
256 )
257 return False
258 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700259 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700260 self.log.error(
261 "Bluetooth LE State not OK after attempted enable. Expected {} got {}".
tturneya4ebb032017-07-24 12:14:45 -0700262 format(bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700263 return False
264 self.scn_ad.droid.connectivityToggleAirplaneMode(False)
265 # Sleep to let Airplane Mode disable propogate through the system
266 time.sleep(self.default_timeout)
267 state = self.scn_ad.droid.bluetoothGetLeState()
tturneya4ebb032017-07-24 12:14:45 -0700268 if state != bt_adapter_states['off']:
Marie Janssen1f333032016-10-25 16:49:35 -0700269 self.log.error(
tturneya4ebb032017-07-24 12:14:45 -0700270 ble_state_error_msg.format("after Airplane Mode OFF",
271 bt_adapter_states['off'], state))
Marie Janssen1f333032016-10-25 16:49:35 -0700272 return False
273 return True