1#/usr/bin/env python3.4 2# 3# Copyright (C) 2016 The Android Open Source Project 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. 16""" 17This test script exercises background scan test scenarios. 18""" 19 20from queue import Empty 21 22from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 23from acts.test_utils.bt.BleEnum import BluetoothAdapterState 24from 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 27from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects 28from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects 29from acts.test_utils.bt.bt_test_utils import scan_result 30 31 32class BleBackgroundScanTest(BluetoothBaseTest): 33 default_timeout = 10 34 max_scan_instances = 28 35 report_delay = 2000 36 scan_callbacks = [] 37 adv_callbacks = [] 38 active_scan_callback_list = [] 39 active_adv_callback_list = [] 40 41 def __init__(self, controllers): 42 BluetoothBaseTest.__init__(self, controllers) 43 self.scn_ad = self.android_devices[0] 44 self.adv_ad = self.android_devices[1] 45 46 def setup_test(self): 47 if (self.scn_ad.droid.bluetoothGetLeState() == 48 BluetoothAdapterState.STATE_OFF.value): 49 self.scn_ad.droid.bluetoothEnableBLE() 50 self.scn_ad.ed.pop_event("BleStateChangedOn") 51 for a in self.android_devices: 52 a.ed.clear_all_events() 53 return True 54 55 def teardown_test(self): 56 cleanup_scanners_and_advertisers( 57 self.scn_ad, self.active_adv_callback_list, self.adv_ad, 58 self.active_adv_callback_list) 59 self.active_adv_callback_list = [] 60 self.active_scan_callback_list = [] 61 62 def _setup_generic_advertisement(self): 63 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects( 64 self.adv_ad.droid) 65 self.adv_ad.droid.bleStartBleAdvertising(adv_callback, adv_data, 66 adv_settings) 67 self.active_adv_callback_list.append(adv_callback) 68 69 def _verify_no_events_found(self, event_name): 70 try: 71 self.scn_ad.ed.pop_event(event_name, self.default_timeout) 72 self.log.error("Found an event when none was expected.") 73 return False 74 except Empty: 75 self.log.info("No scan result found as expected.") 76 return True 77 78 @BluetoothBaseTest.bt_test_wrap 79 def test_background_scan(self): 80 """Test generic background scan. 81 82 Tests LE background scan. The goal is to find scan results even though 83 Bluetooth is turned off. 84 85 Steps: 86 1. Setup an advertisement on dut1 87 2. Enable LE on the Bluetooth Adapter on dut0 88 3. Toggle BT off on dut1 89 4. Start a LE scan on dut0 90 5. Find the advertisement from dut1 91 92 Expected Result: 93 Find a advertisement from the scan instance. 94 95 Returns: 96 Pass if True 97 Fail if False 98 99 TAGS: LE, Advertising, Scanning, Background Scanning 100 Priority: 0 101 """ 102 import time 103 self._setup_generic_advertisement() 104 self.scn_ad.droid.bluetoothToggleState(False) 105 try: 106 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout) 107 except Empty: 108 self.log.error("Bluetooth Off event not found. Expected {}".format( 109 bluetooth_off)) 110 return False 111 self.scn_ad.droid.bluetoothDisableBLE() 112 try: 113 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout) 114 except Empty: 115 self.log.error("Bluetooth Off event not found. Expected {}".format( 116 bluetooth_off)) 117 return False 118 self.scn_ad.droid.bluetoothEnableBLE() 119 try: 120 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout*2) 121 except Empty: 122 self.log.error("Bluetooth On event not found. Expected {}".format( 123 bluetooth_on)) 124 return False 125 filter_list, scan_settings, scan_callback = generate_ble_scan_objects( 126 self.scn_ad.droid) 127 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, 128 scan_callback) 129 expected_event = scan_result.format(scan_callback) 130 try: 131 self.scn_ad.ed.pop_event(expected_event, self.default_timeout) 132 except Empty: 133 self.log.error("Scan Result event not found. Expected {}".format(expected_event)) 134 return False 135 return True 136 137 @BluetoothBaseTest.bt_test_wrap 138 def test_background_scan_ble_disabled(self): 139 """Test background LE scanning with LE disabled. 140 141 Tests LE background scan. The goal is to find scan results even though 142 Bluetooth is turned off. 143 144 Steps: 145 1. Setup an advertisement on dut1 146 2. Enable LE on the Bluetooth Adapter on dut0 147 3. Toggle BT off on dut1 148 4. Start a LE scan on dut0 149 5. Find the advertisement from dut1 150 151 Expected Result: 152 Find a advertisement from the scan instance. 153 154 Returns: 155 Pass if True 156 Fail if False 157 158 TAGS: LE, Advertising, Scanning, Background Scanning 159 Priority: 0 160 """ 161 self._setup_generic_advertisement() 162 self.scn_ad.droid.bluetoothEnableBLE() 163 self.scn_ad.droid.bluetoothToggleState(False) 164 try: 165 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout) 166 except Empty: 167 self.log.error("Bluetooth Off event not found. Expected {}".format( 168 bluetooth_off)) 169 return False 170 filter_list, scan_settings, scan_callback = generate_ble_scan_objects( 171 self.scn_ad.droid) 172 try: 173 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings, 174 scan_callback) 175 expected_event = scan_result.format(scan_callback) 176 try: 177 self.scn_ad.ed.pop_event(expected_event, self.default_timeout) 178 except Empty: 179 self.log.error("Scan Result event not found. Expected {}".format(expected_event)) 180 return False 181 self.log.info("Was able to start background scan even though ble " 182 "was disabled.") 183 return False 184 except Exception: 185 self.log.info( 186 "Was not able to start a background scan as expected.") 187 return True 188