blob: faefabc9f972f392ddf20938764676eeb0a90925 [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.
16
17"""
18This test script exercises background scan test scenarios.
19"""
20
21from queue import Empty
22
23from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
24from acts.test_utils.bt.BleEnum import BluetoothAdapterState
25from acts.test_utils.bt.bt_test_utils import bluetooth_off
26from acts.test_utils.bt.bt_test_utils import bluetooth_on
27from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers
28from acts.test_utils.bt.bt_test_utils import log_energy_info
29from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
30from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
31from acts.test_utils.bt.bt_test_utils import get_advanced_droid_list
32from acts.test_utils.bt.bt_test_utils import scan_result
33
34
35class BleBackgroundScanTest(BluetoothBaseTest):
36 tests = None
37 default_timeout = 10
38 max_scan_instances = 28
39 report_delay = 2000
40 scan_callbacks = []
41 adv_callbacks = []
42 active_scan_callback_list = []
43 active_adv_callback_list = []
44
45 def __init__(self, controllers):
46 BluetoothBaseTest.__init__(self, controllers)
tturney1ce8dc62016-02-18 09:55:53 -080047 self.droid_list = get_advanced_droid_list(self.android_devices)
48 self.scn_ad = self.android_devices[0]
49 self.adv_ad = self.android_devices[1]
Ang Li73697b32015-12-03 00:41:53 +000050 if self.droid_list[1]['max_advertisements'] == 0:
51 self.tests = ()
52 return
53 self.tests = (
54 "test_background_scan",
55 "test_background_scan_ble_disabled",
56 )
57
58 def setup_test(self):
tturney1ce8dc62016-02-18 09:55:53 -080059 self.log.debug(log_energy_info(self.android_devices, "Start"))
60 if (self.scn_ad.droid.bluetoothGetLeState() ==
Ang Li73697b32015-12-03 00:41:53 +000061 BluetoothAdapterState.STATE_OFF.value):
tturney1ce8dc62016-02-18 09:55:53 -080062 self.scn_ad.droid.bluetoothEnableBLE()
63 self.scn_ad.ed.pop_event("BleStateChangedOn")
tturney653b2732016-02-24 10:40:50 -080064 for a in self.android_devices:
65 a.ed.clear_all_events()
Ang Li73697b32015-12-03 00:41:53 +000066 return True
67
68 def teardown_test(self):
tturney1ce8dc62016-02-18 09:55:53 -080069 self.log.debug(log_energy_info(self.android_devices, "End"))
Ang Li73697b32015-12-03 00:41:53 +000070 cleanup_scanners_and_advertisers(
tturney1ce8dc62016-02-18 09:55:53 -080071 self.scn_ad, self.active_adv_callback_list,
72 self.adv_ad, self.active_adv_callback_list)
Ang Li73697b32015-12-03 00:41:53 +000073 self.active_adv_callback_list = []
74 self.active_scan_callback_list = []
75
76 def _setup_generic_advertisement(self):
77 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
tturney1ce8dc62016-02-18 09:55:53 -080078 self.adv_ad.droid)
79 self.adv_ad.droid.bleStartBleAdvertising(
Ang Li73697b32015-12-03 00:41:53 +000080 adv_callback, adv_data, adv_settings)
81 self.active_adv_callback_list.append(adv_callback)
82
83 def _verify_no_events_found(self, event_name):
84 try:
tturney1ce8dc62016-02-18 09:55:53 -080085 self.scn_ad.ed.pop_event(event_name, self.default_timeout)
Ang Li73697b32015-12-03 00:41:53 +000086 self.log.error("Found an event when none was expected.")
87 return False
88 except Empty:
89 self.log.info("No scan result found as expected.")
90 return True
91
92 def _delete_me(self):
93 import time
94 time.sleep(5)
95
96 @BluetoothBaseTest.bt_test_wrap
97 def test_background_scan(self):
98 """Test generic background scan.
99
100 Tests LE background scan. The goal is to find scan results even though
101 Bluetooth is turned off.
102
103 Steps:
104 1. Setup an advertisement on dut1
105 2. Enable LE on the Bluetooth Adapter on dut0
106 3. Toggle BT off on dut1
107 4. Start a LE scan on dut0
108 5. Find the advertisement from dut1
109
110 Expected Result:
111 Find a advertisement from the scan instance.
112
113 Returns:
114 Pass if True
115 Fail if False
116
117 TAGS: LE, Advertising, Scanning, Background Scanning
118 Priority: 0
119 """
120 import time
121 self._setup_generic_advertisement()
tturney1ce8dc62016-02-18 09:55:53 -0800122 self.scn_ad.droid.bluetoothToggleState(False)
123 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
124 self.scn_ad.droid.bluetoothDisableBLE()
125 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
126 self.scn_ad.droid.bluetoothEnableBLE()
Ang Li73697b32015-12-03 00:41:53 +0000127 self._delete_me()
tturney1ce8dc62016-02-18 09:55:53 -0800128 self.scn_ad.ed.pop_event(bluetooth_on, self.default_timeout * 2)
Ang Li73697b32015-12-03 00:41:53 +0000129 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800130 self.scn_ad.droid)
131 self.scn_ad.droid.bleStartBleScan(
Ang Li73697b32015-12-03 00:41:53 +0000132 filter_list, scan_settings, scan_callback)
tturney1ce8dc62016-02-18 09:55:53 -0800133 self.scn_ad.ed.pop_event(
Ang Li73697b32015-12-03 00:41:53 +0000134 scan_result.format(scan_callback), self.default_timeout)
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()
tturney1ce8dc62016-02-18 09:55:53 -0800162 self.scn_ad.droid.bluetoothEnableBLE()
163 self.scn_ad.droid.bluetoothToggleState(False)
164 self.scn_ad.ed.pop_event(bluetooth_off, self.default_timeout)
Ang Li73697b32015-12-03 00:41:53 +0000165 self._delete_me()
166 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800167 self.scn_ad.droid)
Ang Li73697b32015-12-03 00:41:53 +0000168 try:
tturney1ce8dc62016-02-18 09:55:53 -0800169 self.scn_ad.droid.bleStartBleScan(
Ang Li73697b32015-12-03 00:41:53 +0000170 filter_list, scan_settings, scan_callback)
tturney1ce8dc62016-02-18 09:55:53 -0800171 self.scn_ad.ed.pop_event(scan_result.format(scan_callback))
Ang Li73697b32015-12-03 00:41:53 +0000172 self.log.info("Was able to start background scan even though ble "
173 "was disabled.")
174 return False
175 except Exception:
176 self.log.info(
177 "Was not able to start a background scan as expected.")
178 return True