blob: e907cbb3420ecaa7bc661866dd27bc84daeb6ec2 [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 different opportunistic scan scenarios.
18It is expected that the second AndroidDevice is able to advertise.
19
20This test script was designed with this setup in mind:
21Shield box one: Android Device, Android Device
22"""
23
24from queue import Empty
25
Hansong Zhangaf1d7892018-05-21 15:48:01 -070026from acts import utils
tturneycd486312017-01-11 14:04:55 -080027from acts.test_decorators import test_tracker_info
Ang Li73697b32015-12-03 00:41:53 +000028from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
tturney2378ac12017-07-24 12:42:39 -070029from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
30from acts.test_utils.bt.bt_constants import ble_scan_settings_modes
Ang Li73697b32015-12-03 00:41:53 +000031from acts.test_utils.bt.bt_test_utils import batch_scan_result
32from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers
33from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
34from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
Ang Li73697b32015-12-03 00:41:53 +000035from acts.test_utils.bt.bt_test_utils import reset_bluetooth
tturney2378ac12017-07-24 12:42:39 -070036from acts.test_utils.bt.bt_constants import scan_result
Ang Li73697b32015-12-03 00:41:53 +000037
38
39class BleOpportunisticScanTest(BluetoothBaseTest):
Ang Li73697b32015-12-03 00:41:53 +000040 default_timeout = 10
Jakub Pawłowskiba99e8c2018-05-04 17:19:22 +020041 max_scan_instances = 27
Ang Li73697b32015-12-03 00:41:53 +000042 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
Hansong Zhangaf1d7892018-05-21 15:48:01 -070053 def setup_class(self):
54 super(BluetoothBaseTest, self).setup_class()
55 utils.set_location_service(self.scn_ad, True)
56 utils.set_location_service(self.adv_ad, True)
57 return True
58
Ang Li73697b32015-12-03 00:41:53 +000059 def teardown_test(self):
60 cleanup_scanners_and_advertisers(
tturneyfc22e302016-09-08 17:20:36 -070061 self.scn_ad, self.active_scan_callback_list, self.adv_ad,
tturney6d448de2016-06-24 08:46:18 -070062 self.active_adv_callback_list)
Ang Li73697b32015-12-03 00:41:53 +000063 self.active_adv_callback_list = []
64 self.active_scan_callback_list = []
65
66 def on_exception(self, test_name, begin_time):
tturney1ce8dc62016-02-18 09:55:53 -080067 reset_bluetooth(self.android_devices)
Ang Li73697b32015-12-03 00:41:53 +000068
69 def _setup_generic_advertisement(self):
70 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
tturney1ce8dc62016-02-18 09:55:53 -080071 self.adv_ad.droid)
tturney6d448de2016-06-24 08:46:18 -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
76 def _verify_no_events_found(self, event_name):
77 try:
tturney1ce8dc62016-02-18 09:55:53 -080078 event = self.scn_ad.ed.pop_event(event_name, self.default_timeout)
tturney6d448de2016-06-24 08:46:18 -070079 self.log.error("Found an event when none was expected: {}".format(
80 event))
Ang Li73697b32015-12-03 00:41:53 +000081 return False
82 except Empty:
83 self.log.info("No scan result found as expected.")
84 return True
85
86 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -080087 @test_tracker_info(uuid='6bccfbea-3734-4504-8ea9-3511ad17a3e0')
Ang Li73697b32015-12-03 00:41:53 +000088 def test_scan_result_no_advertisement(self):
89 """Test opportunistic scan with no advertisement.
90
91 Tests opportunistic scan where there are no advertisements. This should
92 not find any onScanResults.
93
94 Steps:
95 1. Initialize scanner with scan mode set to opportunistic mode.
96 2. Start scanning on dut 0
97 3. Pop onScanResults event on the scanner
98
99 Expected Result:
100 Find no advertisements with the opportunistic scan instance.
101
102 Returns:
103 Pass if True
104 Fail if False
105
106 TAGS: LE, Advertising, Scanning, Opportunistic Scan
107 Priority: 1
108 """
tturney2378ac12017-07-24 12:42:39 -0700109 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
110 'opportunistic'])
Ang Li73697b32015-12-03 00:41:53 +0000111 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800112 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700113 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
114 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000115 self.active_scan_callback_list.append(scan_callback)
116 if not self._verify_no_events_found(scan_result.format(scan_callback)):
117 return False
tturney1ce8dc62016-02-18 09:55:53 -0800118 self.scn_ad.droid.bleStopBleScan(scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000119 return True
120
121 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800122 @test_tracker_info(uuid='8430bc57-925c-4b70-a62e-cd34df264ca1')
Ang Li73697b32015-12-03 00:41:53 +0000123 def test_batch_scan_result_no_advertisement(self):
124 """Test batch opportunistic scan without an advertisement.
125
126 Tests opportunistic scan where there are no advertisements. This should
127 not find any onBatchScanResult.
128
129 Steps:
130 1. Initialize scanner with scan mode set to opportunistic mode.
131 2. Set report delay seconds such that onBatchScanResult events are
132 expected
133 2. Start scanning on dut 0
134 3. Pop onBatchScanResult event on the scanner
135
136 Expected Result:
137 Find no advertisements with the opportunistic scan instance.
138
139 Returns:
140 Pass if True
141 Fail if False
142
143 TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
144 Priority: 1
145 """
tturney2378ac12017-07-24 12:42:39 -0700146 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
147 'opportunistic'])
tturney6d448de2016-06-24 08:46:18 -0700148 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
149 self.report_delay)
Ang Li73697b32015-12-03 00:41:53 +0000150 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800151 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700152 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
153 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000154 self.active_scan_callback_list.append(scan_callback)
tturneyfc22e302016-09-08 17:20:36 -0700155 if not self._verify_no_events_found(
156 batch_scan_result.format(scan_callback)):
Ang Li73697b32015-12-03 00:41:53 +0000157 return False
tturney1ce8dc62016-02-18 09:55:53 -0800158 self.scn_ad.droid.bleStopBleScan(scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000159 return True
160
161 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800162 @test_tracker_info(uuid='4613cb67-0f54-494e-8a56-2e8ce56fad41')
Ang Li73697b32015-12-03 00:41:53 +0000163 def test_scan_result(self):
164 """Test opportunistic scan with an advertisement.
165
166 Tests opportunistic scan where it will only report scan results when
167 other registered scanners find results.
168
169 Steps:
170 1. Initialize advertiser and start advertisement on dut1
171 2. Initialize scanner with scan mode set to opportunistic mode on dut0
172 and start scanning
173 3. Try to find an event, expect none.
174 4. Start a second scanner on dut0, with any other mode set
175 5. Pop onScanResults event on the second scanner
176 6. Pop onScanResults event on the first scanner
177
178 Expected Result:
179 Scan result is found on the opportunistic scan instance.
180
181 Returns:
182 Pass if True
183 Fail if False
184
185 TAGS: LE, Advertising, Scanning, Opportunistic Scan
186 Priority: 1
187 """
188 self._setup_generic_advertisement()
tturney2378ac12017-07-24 12:42:39 -0700189 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
190 'opportunistic'])
Ang Li73697b32015-12-03 00:41:53 +0000191 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800192 self.scn_ad.droid)
Ang Li73697b32015-12-03 00:41:53 +0000193
tturney6d448de2016-06-24 08:46:18 -0700194 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
195 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000196 self.active_scan_callback_list.append(scan_callback)
197 if not self._verify_no_events_found(scan_result.format(scan_callback)):
198 return False
tturney2378ac12017-07-24 12:42:39 -0700199 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
200 'low_latency'])
Ang Li73697b32015-12-03 00:41:53 +0000201 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800202 generate_ble_scan_objects(self.scn_ad.droid))
tturney6d448de2016-06-24 08:46:18 -0700203 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
204 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000205 self.active_scan_callback_list.append(scan_callback2)
tturneyfc22e302016-09-08 17:20:36 -0700206 try:
207 self.scn_ad.ed.pop_event(
208 scan_result.format(scan_callback2), self.default_timeout)
209 except Empty:
210 self.log.error("Non-Opportunistic scan found no scan results.")
211 return False
212 try:
213 self.scn_ad.ed.pop_event(
214 scan_result.format(scan_callback), self.default_timeout)
215 except Empty:
216 self.log.error("Opportunistic scan found no scan results.")
217 return False
Ang Li73697b32015-12-03 00:41:53 +0000218 return True
219
220 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800221 @test_tracker_info(uuid='5b46fefc-70ef-48a0-acf4-35077cd72202')
Ang Li73697b32015-12-03 00:41:53 +0000222 def test_batch_scan_result(self):
223 """Test batch opportunistic scan with advertisement.
224
225 Tests opportunistic scan where it will only report scan results when
226 other registered scanners find results. Set the report delay millis such
227 that an onBatchScanResult is expected.
228
229 Steps:
230 1. Initialize advertiser and start advertisement on dut1
231 2. Initialize scanner with scan mode set to opportunistic mode and
232 set scan settings report delay seconds such that a batch scan is
233 expected
234 3. Start scanning on dut 0
235 4. Try to find an event, expect none.
236 5. Start a second scanner on dut0, with any other mode set and set scan
237 settings report delay millis such that an onBatchScanResult is expected
238 6. Pop onBatchScanResult event on the second scanner
239 7. Pop onBatchScanResult event on the first scanner
240
241 Expected Result:
242 Find a batch scan result on both opportunistic scan instances.
243
244 Returns:
245 Pass if True
246 Fail if False
247
248 TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
249 Priority: 1
250 """
251 self._setup_generic_advertisement()
tturney2378ac12017-07-24 12:42:39 -0700252 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
253 'opportunistic'])
tturney6d448de2016-06-24 08:46:18 -0700254 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
255 self.report_delay)
Ang Li73697b32015-12-03 00:41:53 +0000256 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800257 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700258 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
259 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000260 self.active_scan_callback_list.append(scan_callback)
tturneyfc22e302016-09-08 17:20:36 -0700261 if not self._verify_no_events_found(
262 batch_scan_result.format(scan_callback)):
Ang Li73697b32015-12-03 00:41:53 +0000263 return False
tturney6d448de2016-06-24 08:46:18 -0700264 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
265 self.report_delay)
tturney2378ac12017-07-24 12:42:39 -0700266 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
267 'low_latency'])
tturneyfc22e302016-09-08 17:20:36 -0700268 filter_list2, scan_settings2, scan_callback2 = generate_ble_scan_objects(
269 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700270 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
271 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000272 self.active_scan_callback_list.append(scan_callback2)
tturneyfc22e302016-09-08 17:20:36 -0700273 try:
274 self.scn_ad.ed.pop_event(
275 batch_scan_result.format(scan_callback2), self.default_timeout)
276 except Empty:
277 self.log.error("Non-Opportunistic scan found no scan results.")
278 return False
279 try:
280 self.scn_ad.ed.pop_event(
281 batch_scan_result.format(scan_callback), self.default_timeout)
282 except Empty:
283 self.log.error("Opportunistic scan found no scan results.")
284 return False
Ang Li73697b32015-12-03 00:41:53 +0000285 return True
286
287 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800288 @test_tracker_info(uuid='fd85d95e-dc8c-48c1-8d8a-83c3475755ff')
Ang Li73697b32015-12-03 00:41:53 +0000289 def test_batch_scan_result_not_expected(self):
290 """Test opportunistic batch scan without expecting an event.
291
292 Tests opportunistic scan where it will only report scan results when
293 other registered scanners find results. Set the report delay millis such
294 that a batch scan is not expected.
295
296 Steps:
297 1. Initialize advertiser and start advertisement on dut1
298 2. Initialize scanner with scan mode set to opportunistic mode and
299 set scan settings report delay seconds such that a batch scan is
300 expected.
301 3. Start scanning on dut 0
302 4. Try to find an event, expect none.
303 5. Start a second scanner on dut0, with any other mode set and set scan
304 settings report delay millis to 0 such that an onBatchScanResult is not
305 expected.
306 6. Pop onScanResults event on the second scanner
307 7. Pop onBatchScanResult event on the first scanner
308
309 Expected Result:
310 Batch scan result is not expected on opportunistic scan instance.
311
312 Returns:
313 Pass if True
314 Fail if False
315
316 TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
317 Priority: 1
318 """
319 self._setup_generic_advertisement()
tturney2378ac12017-07-24 12:42:39 -0700320 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
321 'opportunistic'])
tturney6d448de2016-06-24 08:46:18 -0700322 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
323 self.report_delay)
Ang Li73697b32015-12-03 00:41:53 +0000324 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800325 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700326 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
327 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000328 self.active_scan_callback_list.append(scan_callback)
tturneyfc22e302016-09-08 17:20:36 -0700329 if not self._verify_no_events_found(
330 batch_scan_result.format(scan_callback)):
Ang Li73697b32015-12-03 00:41:53 +0000331
332 return False
tturney2378ac12017-07-24 12:42:39 -0700333 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
334 'low_latency'])
Ang Li73697b32015-12-03 00:41:53 +0000335 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800336 generate_ble_scan_objects(self.scn_ad.droid))
tturney6d448de2016-06-24 08:46:18 -0700337 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
338 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000339 self.active_scan_callback_list.append(scan_callback2)
tturneyfc22e302016-09-08 17:20:36 -0700340 try:
341 self.scn_ad.ed.pop_event(
342 scan_result.format(scan_callback2), self.default_timeout)
343 except Empty:
344 self.log.error("Non-Opportunistic scan found no scan results.")
345 return False
346 return self._verify_no_events_found(
347 batch_scan_result.format(scan_callback))
Ang Li73697b32015-12-03 00:41:53 +0000348
349 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800350 @test_tracker_info(uuid='6138592e-8fd5-444f-9a7c-25cd9695644a')
Ang Li73697b32015-12-03 00:41:53 +0000351 def test_scan_result_not_expected(self):
352 """Test opportunistic scan without expecting an event.
353
354 Tests opportunistic scan where it will only report batch scan results
355 when other registered scanners find results.
356
357 Steps:
358 1. Initialize advertiser and start advertisement on dut1
359 2. Initialize scanner with scan mode set to opportunistic mode.
360 3. Start scanning on dut 0
361 4. Try to find an event, expect none.
362 5. Start a second scanner on dut0, with any other mode set and set scan
363 settings
364 report delay millis such that an onBatchScanResult is expected
365 6. Pop onBatchScanResult event on the second scanner
366 7. Pop onScanResults event on the first scanner
367
368 Expected Result:
369 Scan result is not expected on opportunistic scan instance.
370
371 Returns:
372 Pass if True
373 Fail if False
374
375 TAGS: LE, Advertising, Scanning, Opportunistic Scan
376 Priority: 1
377 """
378 self._setup_generic_advertisement()
tturney2378ac12017-07-24 12:42:39 -0700379 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
380 'opportunistic'])
tturneyfc22e302016-09-08 17:20:36 -0700381 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
382 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700383 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
384 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000385 self.active_scan_callback_list.append(scan_callback)
386 if not self._verify_no_events_found(scan_result.format(scan_callback)):
387 return False
tturney6d448de2016-06-24 08:46:18 -0700388 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
389 self.report_delay)
tturney2378ac12017-07-24 12:42:39 -0700390 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
391 'low_latency'])
Ang Li73697b32015-12-03 00:41:53 +0000392 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800393 generate_ble_scan_objects(self.scn_ad.droid))
tturney6d448de2016-06-24 08:46:18 -0700394 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
395 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000396 self.active_scan_callback_list.append(scan_callback2)
tturneyfc22e302016-09-08 17:20:36 -0700397 try:
398 self.scn_ad.ed.pop_event(
399 batch_scan_result.format(scan_callback2), self.default_timeout)
400 except Empty:
401 self.log.error("Non-Opportunistic scan found no scan results.")
402 return False
Ang Li73697b32015-12-03 00:41:53 +0000403 return self._verify_no_events_found(scan_result.format(scan_callback))
404
405 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800406 @test_tracker_info(uuid='f7aba3d9-d3f7-4b2f-976e-441772705613')
Ang Li73697b32015-12-03 00:41:53 +0000407 def test_max_opportunistic_scan_instances(self):
408 """Test max number of opportunistic scan instances.
409
410 Tests max instances of opportunistic scans. Each instances should
411 find an onScanResults event.
412
413 Steps:
414 1. Initialize advertiser and start advertisement on dut1
415 2. Set scan settings to opportunistic scan on dut0 scan instance
416 3. Start scan scan from step 2
417 4. Repeat step two and three until there are max_scan_instances-1 scan
418 instances
419 5. Start a regular ble scan on dut0 with the last available scan
420 instance
421 6. Pop onScanResults event on all scan instances
422
423 Expected Result:
424 Each opportunistic scan instance finds a advertisement.
425
426 Returns:
427 Pass if True
428 Fail if False
429
430 TAGS: LE, Advertising, Scanning, Opportunistic Scan
431 Priority: 1
432 """
433 self._setup_generic_advertisement()
434 for _ in range(self.max_scan_instances - 1):
tturney1ce8dc62016-02-18 09:55:53 -0800435 self.scn_ad.droid.bleSetScanSettingsScanMode(
tturney2378ac12017-07-24 12:42:39 -0700436 ble_scan_settings_modes['opportunistic'])
tturneyfc22e302016-09-08 17:20:36 -0700437 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
438 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700439 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
440 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000441 self.active_scan_callback_list.append(scan_callback)
442
tturney2378ac12017-07-24 12:42:39 -0700443 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
444 'low_latency'])
Ang Li73697b32015-12-03 00:41:53 +0000445 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800446 generate_ble_scan_objects(self.scn_ad.droid))
tturney6d448de2016-06-24 08:46:18 -0700447 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
448 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000449 self.active_scan_callback_list.append(scan_callback2)
450
451 for callback in self.active_scan_callback_list:
tturneyfc22e302016-09-08 17:20:36 -0700452 try:
453 self.scn_ad.ed.pop_event(
454 scan_result.format(callback), self.default_timeout)
455 except Empty:
456 self.log.error("No scan results found for callback {}".format(
457 callback))
458 return False
Ang Li73697b32015-12-03 00:41:53 +0000459 return True
460
461 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800462 @test_tracker_info(uuid='cf971f08-4d92-4046-bba6-b86a75aa773c')
Ang Li73697b32015-12-03 00:41:53 +0000463 def test_max_opportunistic_batch_scan_instances(self):
464 """Test max opportunistic batch scan instances.
465
466 Tests max instances of opportunistic batch scans. Each instances should
467 find an onBatchScanResult event.
468
469 Steps:
470 1. Initialize advertiser and start advertisement on dut1
471 2. Set scan settings to opportunistic scan on dut0 scan instance and
472 set report delay seconds such that an onBatchScanResult is expected
473 3. Start scan scan from step 2
474 4. Repeat step two and three until there are max_scan_instances-1 scan
475 instances
476 5. Start a regular ble scan on dut0 with the last available scan
477 instance
478 6. Pop onBatchScanResult event on all scan instances
479
480 Expected Result:
481 Each opportunistic scan instance finds an advertisement.
482
483 Returns:
484 Pass if True
485 Fail if False
486
487 TAGS: LE, Advertising, Scanning, Opportunistic Scan, Batch Scanning
488 Priority: 1
489 """
490 self._setup_generic_advertisement()
491 for _ in range(self.max_scan_instances - 1):
tturney1ce8dc62016-02-18 09:55:53 -0800492 self.scn_ad.droid.bleSetScanSettingsScanMode(
tturney2378ac12017-07-24 12:42:39 -0700493 ble_scan_settings_modes['opportunistic'])
tturney1ce8dc62016-02-18 09:55:53 -0800494 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
Ang Li73697b32015-12-03 00:41:53 +0000495 self.report_delay)
tturneyfc22e302016-09-08 17:20:36 -0700496 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
497 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700498 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
499 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000500 self.active_scan_callback_list.append(scan_callback)
501
tturney2378ac12017-07-24 12:42:39 -0700502 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
503 'low_latency'])
tturney6d448de2016-06-24 08:46:18 -0700504 self.scn_ad.droid.bleSetScanSettingsReportDelayMillis(
505 self.report_delay)
Ang Li73697b32015-12-03 00:41:53 +0000506 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800507 generate_ble_scan_objects(self.scn_ad.droid))
tturney6d448de2016-06-24 08:46:18 -0700508 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
509 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000510 self.active_scan_callback_list.append(scan_callback2)
511
512 for callback in self.active_scan_callback_list:
tturneyfc22e302016-09-08 17:20:36 -0700513 try:
514 self.scn_ad.ed.pop_event(
515 batch_scan_result.format(callback), self.default_timeout)
516 except Empty:
517 self.log.error("No scan results found for callback {}".format(
518 callback))
Ang Li73697b32015-12-03 00:41:53 +0000519 return True
520
521 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800522 @test_tracker_info(uuid='965d84ef-11a7-418a-97e9-2a441c6de776')
tturney6d448de2016-06-24 08:46:18 -0700523 def test_discover_opportunistic_scan_result_off_secondary_scan_filter(
524 self):
Ang Li73697b32015-12-03 00:41:53 +0000525 """Test opportunistic scan result from secondary scan filter.
526
527 Tests opportunistic scan where the secondary scan instance does not find
528 an advertisement but the scan instance with scan mode set to
529 opportunistic scan will find an advertisement.
530
531 Steps:
532 1. Initialize advertiser and start advertisement on dut1 (make sure the
533 advertisement is not advertising the device name)
534 2. Set scan settings to opportunistic scan on dut0 scan instance
535 3. Start scan scan from step 2
536 4. Try to find an event, expect none
537 5. Start a second scanner on dut0, with any other mode set and set the
538 scan filter device name to "opp_test"
539 6. Pop onScanResults from the second scanner
540 7. Expect no events
541 8. Pop onScanResults from the first scanner
542
543 Expected Result:
544 Opportunistic scan instance finds an advertisement.
545
546 Returns:
547 Pass if True
548 Fail if False
549
550 TAGS: LE, Advertising, Scanning, Opportunistic Scan
551 Priority: 1
552 """
553 self._setup_generic_advertisement()
tturney2378ac12017-07-24 12:42:39 -0700554 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
555 'opportunistic'])
Ang Li73697b32015-12-03 00:41:53 +0000556 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800557 self.scn_ad.droid)
Ang Li73697b32015-12-03 00:41:53 +0000558
tturney6d448de2016-06-24 08:46:18 -0700559 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
560 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000561 self.active_scan_callback_list.append(scan_callback)
562 if not self._verify_no_events_found(scan_result.format(scan_callback)):
563 return False
tturney2378ac12017-07-24 12:42:39 -0700564 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
565 'low_latency'])
tturneyfc22e302016-09-08 17:20:36 -0700566 self.scn_ad.droid.bleSetScanFilterDeviceName("opp_test")
Ang Li73697b32015-12-03 00:41:53 +0000567 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800568 generate_ble_scan_objects(self.scn_ad.droid))
tturney49a23d72016-09-30 15:50:49 -0700569 self.scn_ad.droid.bleBuildScanFilter(filter_list2)
tturney6d448de2016-06-24 08:46:18 -0700570 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
571 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000572 self.active_scan_callback_list.append(scan_callback2)
tturneycd486312017-01-11 14:04:55 -0800573 if not self._verify_no_events_found(
574 scan_result.format(scan_callback2)):
Ang Li73697b32015-12-03 00:41:53 +0000575 return False
tturneyfc22e302016-09-08 17:20:36 -0700576 try:
577 self.scn_ad.ed.pop_event(
578 scan_result.format(scan_callback), self.default_timeout)
579 except Empty:
580 self.log.error("Opportunistic scan found no scan results.")
581 return False
Ang Li73697b32015-12-03 00:41:53 +0000582 return True
583
584 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800585 @test_tracker_info(uuid='13b0a83f-e96e-4d64-84ef-66351ec5054c')
tturney6d448de2016-06-24 08:46:18 -0700586 def test_negative_opportunistic_scan_filter_result_off_secondary_scan_result(
587 self):
Ang Li73697b32015-12-03 00:41:53 +0000588 """Test opportunistic scan not found scenario.
589
590 Tests opportunistic scan where the secondary scan instance does find an
591 advertisement but the scan instance with scan mode set to opportunistic
592 scan does not find an advertisement due to mismatched scan filters.
593
594 Steps:
595 1. Initialize advertiser and start advertisement on dut1 (make sure the
596 advertisement is not advertising the device name)
597 2. Set scan settings to opportunistic scan on dut0 scan instance and set
598 the scan filter device name to "opp_test"
599 3. Start scan scan from step 2
600 4. Try to find an event, expect none
601 5. Start a second scanner on dut0, with any other mode set
602 6. Pop onScanResults from the second scanner
603 7. Pop onScanResults from the first scanner
604 8. Expect no events
605
606 Expected Result:
607 Opportunistic scan instance doesn't find any advertisements.
608
609 Returns:
610 Pass if True
611 Fail if False
612
613 TAGS: LE, Advertising, Scanning, Opportunistic Scan
614 Priority: 1
615 """
616 self._setup_generic_advertisement()
tturney2378ac12017-07-24 12:42:39 -0700617 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
618 'opportunistic'])
tturneyfc22e302016-09-08 17:20:36 -0700619 self.scn_ad.droid.bleSetScanFilterDeviceName("opp_test")
Ang Li73697b32015-12-03 00:41:53 +0000620 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800621 self.scn_ad.droid)
tturney49a23d72016-09-30 15:50:49 -0700622 self.scn_ad.droid.bleBuildScanFilter(filter_list)
tturney6d448de2016-06-24 08:46:18 -0700623 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
624 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000625 self.active_scan_callback_list.append(scan_callback)
626 if not self._verify_no_events_found(scan_result.format(scan_callback)):
627 return False
tturney2378ac12017-07-24 12:42:39 -0700628 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
629 'low_latency'])
Ang Li73697b32015-12-03 00:41:53 +0000630 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800631 generate_ble_scan_objects(self.scn_ad.droid))
tturney6d448de2016-06-24 08:46:18 -0700632 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
633 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000634 self.active_scan_callback_list.append(scan_callback2)
tturneyfc22e302016-09-08 17:20:36 -0700635 try:
636 self.scn_ad.ed.pop_event(
637 scan_result.format(scan_callback2), self.default_timeout)
638 except Empty:
639 self.log.error("Non-Opportunistic scan found no scan results.")
640 return False
Ang Li73697b32015-12-03 00:41:53 +0000641 return self._verify_no_events_found(scan_result.format(scan_callback))
642
643 @BluetoothBaseTest.bt_test_wrap
tturneycd486312017-01-11 14:04:55 -0800644 @test_tracker_info(uuid='087f60b2-f6a1-4919-b4c5-cdf3debcfeff')
Ang Li73697b32015-12-03 00:41:53 +0000645 def test_opportunistic_scan_filter_result_off_secondary_scan_result(self):
646 """Test opportunistic scan from a secondary scan result.
647
648 Tests opportunistic scan where the scan filters are the same between the
649 first scan instance with opportunistic scan set and the second instance
650 with any other mode set.
651
652 Steps:
tturneyfc22e302016-09-08 17:20:36 -0700653 1. Initialize advertiser and start advertisement on dut1
654 2. On dut0, set the scan settings mode to opportunistic scan and set
Ang Li73697b32015-12-03 00:41:53 +0000655 the scan filter device name to the advertiser's device name
656 3. Start scan scan from step 2
657 4. Try to find an event, expect none
658 5. Start a second scanner on dut0, with any other mode set and set the
659 scan filter device name to the advertiser's device name
660 6. Pop onScanResults from the second scanner
661 7. Pop onScanResults from the first scanner
662
663 Expected Result:
664 Opportunistic scan instance finds a advertisement.
665
666 Returns:
667 Pass if True
668 Fail if False
669
670 TAGS: LE, Advertising, Scanning, Opportunistic Scan
671 Priority: 1
672 """
tturney1ce8dc62016-02-18 09:55:53 -0800673 self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True)
Ang Li73697b32015-12-03 00:41:53 +0000674 self._setup_generic_advertisement()
tturney1ce8dc62016-02-18 09:55:53 -0800675 adv_device_name = self.adv_ad.droid.bluetoothGetLocalName()
tturney2378ac12017-07-24 12:42:39 -0700676 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
677 'opportunistic'])
tturneyfc22e302016-09-08 17:20:36 -0700678 self.scn_ad.droid.bleSetScanFilterDeviceName(adv_device_name)
Ang Li73697b32015-12-03 00:41:53 +0000679 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
tturney1ce8dc62016-02-18 09:55:53 -0800680 self.scn_ad.droid)
tturney6d448de2016-06-24 08:46:18 -0700681 self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
682 scan_callback)
Ang Li73697b32015-12-03 00:41:53 +0000683 self.active_scan_callback_list.append(scan_callback)
684 if not self._verify_no_events_found(scan_result.format(scan_callback)):
685 return False
tturney2378ac12017-07-24 12:42:39 -0700686 self.scn_ad.droid.bleSetScanSettingsScanMode(ble_scan_settings_modes[
687 'low_latency'])
Ang Li73697b32015-12-03 00:41:53 +0000688 filter_list2, scan_settings2, scan_callback2 = (
tturney1ce8dc62016-02-18 09:55:53 -0800689 generate_ble_scan_objects(self.scn_ad.droid))
690 self.scn_ad.droid.bleSetScanFilterDeviceName(adv_device_name)
691 self.scn_ad.droid.bleBuildScanFilter(filter_list2)
tturney6d448de2016-06-24 08:46:18 -0700692 self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
693 scan_callback2)
Ang Li73697b32015-12-03 00:41:53 +0000694 self.active_scan_callback_list.append(scan_callback2)
tturneyfc22e302016-09-08 17:20:36 -0700695 try:
696 self.scn_ad.ed.pop_event(
697 scan_result.format(scan_callback2), self.default_timeout)
698 except Empty:
699 self.log.error("Opportunistic scan found no scan results.")
700 return False
701 try:
702 self.scn_ad.ed.pop_event(
703 scan_result.format(scan_callback), self.default_timeout)
704 except Empty:
705 self.log.error("Non-Opportunistic scan found no scan results.")
706 return False
Ang Li73697b32015-12-03 00:41:53 +0000707 return True