• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2017 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 different scan filters with different screen states.
18"""
19
20import time
21
22from queue import Empty
23from acts import utils
24from acts.test_decorators import test_tracker_info
25from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
26from acts_contrib.test_utils.bt.bt_constants import adv_succ
27from acts_contrib.test_utils.bt.bt_constants import ble_advertise_settings_modes
28from acts_contrib.test_utils.bt.bt_constants import ble_scan_settings_modes
29from acts_contrib.test_utils.bt.bt_constants import bt_default_timeout
30from acts_contrib.test_utils.bt.bt_constants import scan_result
31from acts_contrib.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
32from acts_contrib.test_utils.bt.bt_test_utils import generate_ble_scan_objects
33from acts_contrib.test_utils.bt.bt_test_utils import reset_bluetooth
34
35
36class BleScanScreenStateTest(BluetoothBaseTest):
37    advertise_callback = -1
38    max_concurrent_scans = 27
39    scan_callback = -1
40    shorter_scan_timeout = 4
41
42    def setup_class(self):
43        super(BluetoothBaseTest, self).setup_class()
44        self.scn_ad = self.android_devices[0]
45        self.adv_ad = self.android_devices[1]
46
47        utils.set_location_service(self.scn_ad, True)
48        utils.set_location_service(self.adv_ad, True)
49        return True
50
51    def _setup_generic_advertisement(self):
52        self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode(
53            ble_advertise_settings_modes['low_latency'])
54        self.advertise_callback, advertise_data, advertise_settings = (
55            generate_ble_advertise_objects(self.adv_ad.droid))
56        self.adv_ad.droid.bleStartBleAdvertising(self.advertise_callback,
57                                                 advertise_data,
58                                                 advertise_settings)
59        try:
60            self.adv_ad.ed.pop_event(adv_succ.format(self.advertise_callback))
61        except Empty:
62            self.log.error("Failed to start advertisement.")
63            return False
64        return True
65
66    def _setup_scan_with_no_filters(self):
67        filter_list, scan_settings, self.scan_callback = \
68            generate_ble_scan_objects(self.scn_ad.droid)
69        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
70                                          self.scan_callback)
71
72    def _scan_found_results(self):
73        try:
74            self.scn_ad.ed.pop_event(scan_result.format(self.scan_callback),
75                                     bt_default_timeout)
76            self.log.info("Found an advertisement.")
77        except Empty:
78            self.log.info("Did not find an advertisement.")
79            return False
80        return True
81
82    @BluetoothBaseTest.bt_test_wrap
83    @test_tracker_info(uuid='9b695819-e5a8-48b3-87a0-f90422998bf9')
84    def test_scan_no_filters_screen_on(self):
85        """Test LE scanning is successful with no filters and screen on.
86
87        Test LE scanning is successful with no filters and screen on. Scan
88        results should be found.
89
90        Steps:
91        1. Setup advertisement
92        2. Turn on screen
93        3. Start scanner without filters
94        4. Verify scan results are found
95        5. Teardown advertisement and scanner
96
97        Expected Result:
98        Scan results should be found.
99
100        Returns:
101          Pass if True
102          Fail if False
103
104        TAGS: LE, Advertising, Filtering, Scanning, Screen
105        Priority: 2
106        """
107        # Step 1
108        if not self._setup_generic_advertisement():
109            return False
110
111        # Step 2
112        self.scn_ad.droid.wakeUpNow()
113
114        # Step 3
115        self._setup_scan_with_no_filters()
116
117        # Step 4
118        if not self._scan_found_results():
119            return False
120
121        # Step 5
122        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
123        self.scn_ad.droid.bleStopBleScan(self.scan_callback)
124        return True
125
126    @BluetoothBaseTest.bt_test_wrap
127    @test_tracker_info(uuid='38fb6959-f07b-4501-814b-81a498e3efc4')
128    def test_scan_no_filters_screen_off(self):
129        """Test LE scanning is successful with no filters and screen off.
130
131        Test LE scanning is successful with no filters and screen off. No scan
132        results should be found.
133
134        Steps:
135        1. Setup advertisement
136        2. Turn off screen
137        3. Start scanner without filters
138        4. Verify no scan results are found
139        5. Teardown advertisement and scanner
140
141        Expected Result:
142        No scan results should be found.
143
144        Returns:
145          Pass if True
146          Fail if False
147
148        TAGS: LE, Advertising, Filtering, Scanning, Screen
149        Priority: 1
150        """
151        # Step 1
152        if not self._setup_generic_advertisement():
153            return False
154
155        # Step 2
156        self.scn_ad.droid.goToSleepNow()
157        # Give the device time to go to sleep
158        time.sleep(2)
159
160        # Step 3
161        self._setup_scan_with_no_filters()
162
163        # Step 4
164        if self._scan_found_results():
165            return False
166
167        # Step 5
168        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
169        self.scn_ad.droid.bleStopBleScan(self.scan_callback)
170        return True
171
172    @BluetoothBaseTest.bt_test_wrap
173    @test_tracker_info(uuid='7186ef2f-096a-462e-afde-b0e3d4ecdd83')
174    def test_scan_filters_works_with_screen_off(self):
175        """Test LE scanning is successful with filters and screen off.
176
177        Test LE scanning is successful with no filters and screen off. No scan
178        results should be found.
179
180        Steps:
181        1. Setup advertisement
182        2. Turn off screen
183        3. Start scanner with filters
184        4. Verify scan results are found
185        5. Teardown advertisement and scanner
186
187        Expected Result:
188        Scan results should be found.
189
190        Returns:
191          Pass if True
192          Fail if False
193
194        TAGS: LE, Advertising, Filtering, Scanning, Screen
195        Priority: 1
196        """
197        # Step 1
198        adv_device_name = self.adv_ad.droid.bluetoothGetLocalName()
199        print(adv_device_name)
200        self.adv_ad.droid.bleSetAdvertiseDataIncludeDeviceName(True)
201        if not self._setup_generic_advertisement():
202            return False
203
204        # Step 2
205        self.scn_ad.droid.goToSleepNow()
206
207        # Step 3
208        self.scn_ad.droid.bleSetScanFilterDeviceName(adv_device_name)
209        filter_list, scan_settings, self.scan_callback = generate_ble_scan_objects(
210            self.scn_ad.droid)
211        self.scn_ad.droid.bleBuildScanFilter(filter_list)
212        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
213                                          self.scan_callback)
214
215        # Step 4
216        if not self._scan_found_results():
217            return False
218
219        # Step 5
220        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
221        self.scn_ad.droid.bleStopBleScan(self.scan_callback)
222        return True
223
224    @BluetoothBaseTest.bt_test_wrap
225    @test_tracker_info(uuid='02cd6dca-149e-439b-8427-a2edc7864265')
226    def test_scan_no_filters_screen_off_then_turn_on(self):
227        """Test start LE scan with no filters while screen is off then turn on.
228
229        Test that a scan without filters will not return results while the
230        screen is off but will return results when the screen turns on.
231
232        Steps:
233        1. Setup advertisement
234        2. Turn off screen
235        3. Start scanner without filters
236        4. Verify no scan results are found
237        5. Turn screen on
238        6. Verify scan results are found
239        7. Teardown advertisement and scanner
240
241        Expected Result:
242        Scan results should only come in when the screen is on.
243
244        Returns:
245          Pass if True
246          Fail if False
247
248        TAGS: LE, Advertising, Filtering, Scanning, Screen
249        Priority: 2
250        """
251        # Step 1
252        if not self._setup_generic_advertisement():
253            return False
254
255        # Step 2
256        self.scn_ad.droid.goToSleepNow()
257        # Give the device time to go to sleep
258        time.sleep(2)
259
260        # Step 3
261        self._setup_scan_with_no_filters()
262
263        # Step 4
264        if self._scan_found_results():
265            return False
266
267        # Step 5
268        self.scn_ad.droid.wakeUpNow()
269
270        # Step 6
271        if not self._scan_found_results():
272            return False
273
274        # Step 7
275        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
276        self.scn_ad.droid.bleStopBleScan(self.scan_callback)
277        return True
278
279    @BluetoothBaseTest.bt_test_wrap
280    @test_tracker_info(uuid='eb9fc373-f5e8-4a55-9750-02b7a11893d1')
281    def test_scan_no_filters_screen_on_then_turn_off(self):
282        """Test start LE scan with no filters while screen is on then turn off.
283
284        Test that a scan without filters will not return results while the
285        screen is off but will return results when the screen turns on.
286
287        Steps:
288        1. Setup advertisement
289        2. Turn off screen
290        3. Start scanner without filters
291        4. Verify no scan results are found
292        5. Turn screen on
293        6. Verify scan results are found
294        7. Teardown advertisement and scanner
295
296        Expected Result:
297        Scan results should only come in when the screen is on.
298
299        Returns:
300          Pass if True
301          Fail if False
302
303        TAGS: LE, Advertising, Filtering, Scanning, Screen
304        Priority: 2
305        """
306        # Step 1
307        if not self._setup_generic_advertisement():
308            return False
309
310        # Step 2
311        self.scn_ad.droid.wakeUpNow()
312
313        # Step 3
314        self._setup_scan_with_no_filters()
315
316        # Step 4
317        if not self._scan_found_results():
318            return False
319
320        # Step 5
321        self.scn_ad.droid.goToSleepNow()
322        # Give the device time to go to sleep
323        time.sleep(2)
324        self.scn_ad.ed.clear_all_events()
325
326        # Step 6
327        if self._scan_found_results():
328            return False
329
330        # Step 7
331        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
332        self.scn_ad.droid.bleStopBleScan(self.scan_callback)
333        return True
334
335    @BluetoothBaseTest.bt_test_wrap
336    @test_tracker_info(uuid='41d90e11-b0a8-4eed-bff1-c19678920762')
337    def test_scan_no_filters_screen_toggling(self):
338        """Test start LE scan with no filters and test screen toggling.
339
340        Test that a scan without filters will not return results while the
341        screen is off and return results while the screen is on.
342
343        Steps:
344        1. Setup advertisement
345        2. Turn off screen
346        3. Start scanner without filters
347        4. Verify no scan results are found
348        5. Turn screen on
349        6. Verify scan results are found
350        7. Repeat steps 1-6 10 times
351        7. Teardown advertisement and scanner
352
353        Expected Result:
354        Scan results should only come in when the screen is on.
355
356        Returns:
357          Pass if True
358          Fail if False
359
360        TAGS: LE, Advertising, Filtering, Scanning, Screen
361        Priority: 3
362        """
363        iterations = 10
364        # Step 1
365        if not self._setup_generic_advertisement():
366            return False
367
368        for i in range(iterations):
369            self.log.info("Starting iteration {}".format(i + 1))
370            # Step 2
371            self.scn_ad.droid.goToSleepNow()
372            # Give the device time to go to sleep
373            time.sleep(2)
374            self.scn_ad.ed.clear_all_events()
375
376            # Step 3
377            self._setup_scan_with_no_filters()
378
379            # Step 4
380            if self._scan_found_results():
381                return False
382
383            # Step 5
384            self.scn_ad.droid.wakeUpNow()
385
386            # Step 6
387            if not self._scan_found_results():
388                return False
389
390        # Step 7
391        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
392        self.scn_ad.droid.bleStopBleScan(self.scan_callback)
393        return True
394
395    @BluetoothBaseTest.bt_test_wrap
396    @test_tracker_info(uuid='7a2fe7ef-b15f-4e93-a2f0-40e2f7d9cbcb')
397    def test_opportunistic_scan_no_filters_screen_off_then_on(self):
398        """Test opportunistic scanning does not find results with screen off.
399
400        Test LE scanning is successful with no filters and screen off. No scan
401        results should be found.
402
403        Steps:
404        1. Setup advertisement
405        2. Turn off screen
406        3. Start opportunistic scan without filters
407        4. Start scan without filters
408        5. Verify no scan results are found on either scan instance
409        6. Wake up phone
410        7. Verify scan results on each scan instance
411        8. Teardown advertisement and scanner
412
413        Expected Result:
414        No scan results should be found.
415
416        Returns:
417          Pass if True
418          Fail if False
419
420        TAGS: LE, Advertising, Filtering, Scanning, Screen
421        Priority: 1
422        """
423        # Step 1
424        if not self._setup_generic_advertisement():
425            return False
426
427        # Step 2
428        self.scn_ad.droid.goToSleepNow()
429        # Give the device time to go to sleep
430        time.sleep(2)
431
432        # Step 3
433        self.scn_ad.droid.bleSetScanSettingsScanMode(
434            ble_scan_settings_modes['opportunistic'])
435        filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
436            self.scn_ad.droid)
437        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
438                                          scan_callback)
439
440        # Step 4
441        filter_list2, scan_settings2, scan_callback2 = generate_ble_scan_objects(
442            self.scn_ad.droid)
443        self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
444                                          scan_callback2)
445
446        # Step 5
447        try:
448            self.scn_ad.ed.pop_event(scan_result.format(scan_callback),
449                                     self.shorter_scan_timeout)
450            self.log.error("Found an advertisement on opportunistic scan.")
451            return False
452        except Empty:
453            self.log.info("Did not find an advertisement.")
454        try:
455            self.scn_ad.ed.pop_event(scan_result.format(scan_callback2),
456                                     self.shorter_scan_timeout)
457            self.log.error("Found an advertisement on scan instance.")
458            return False
459        except Empty:
460            self.log.info("Did not find an advertisement.")
461
462        # Step 6
463        self.scn_ad.droid.wakeUpNow()
464
465        # Step 7
466        try:
467            self.scn_ad.ed.pop_event(scan_result.format(scan_callback),
468                                     self.shorter_scan_timeout)
469            self.log.info("Found an advertisement on opportunistic scan.")
470        except Empty:
471            self.log.error(
472                "Did not find an advertisement on opportunistic scan.")
473            return False
474        try:
475            self.scn_ad.ed.pop_event(scan_result.format(scan_callback2),
476                                     self.shorter_scan_timeout)
477            self.log.info("Found an advertisement on scan instance.")
478        except Empty:
479            self.log.info("Did not find an advertisement.")
480            return False
481
482        # Step 8
483        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
484        self.scn_ad.droid.bleStopBleScan(scan_callback)
485        self.scn_ad.droid.bleStopBleScan(scan_callback2)
486        return True
487
488    @BluetoothBaseTest.bt_test_wrap
489    @test_tracker_info(uuid='406f1a2e-160f-4fb2-8a87-6403996df36e')
490    def test_max_scan_no_filters_screen_off_then_turn_on(self):
491        """Test start max scans with no filters while screen is off then turn on
492
493        Test that max LE scan without filters will not return results while the
494        screen is off but will return results when the screen turns on.
495
496        Steps:
497        1. Setup advertisement
498        2. Turn off screen
499        3. Start scanner without filters and verify no scan results
500        4. Turn screen on
501        5. Verify scan results are found on each scan callback
502        6. Teardown advertisement and all scanner
503
504        Expected Result:
505        Scan results should only come in when the screen is on.
506
507        Returns:
508          Pass if True
509          Fail if False
510
511        TAGS: LE, Advertising, Filtering, Scanning, Screen
512        Priority: 2
513        """
514        # Step 1
515        if not self._setup_generic_advertisement():
516            return False
517
518        # Step 2
519        self.scn_ad.droid.goToSleepNow()
520        # Give the device time to go to sleep
521        time.sleep(2)
522
523        # Step 3
524        scan_callback_list = []
525        for _ in range(self.max_concurrent_scans):
526            filter_list, scan_settings, scan_callback = \
527                generate_ble_scan_objects(self.scn_ad.droid)
528            self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
529                                              scan_callback)
530            scan_callback_list.append(scan_callback)
531            try:
532                self.scn_ad.ed.pop_event(
533                    scan_result.format(self.scan_callback),
534                    self.shorter_scan_timeout)
535                self.log.info("Found an advertisement.")
536                return False
537            except Empty:
538                self.log.info("Did not find an advertisement.")
539
540        # Step 4
541        self.scn_ad.droid.wakeUpNow()
542
543        # Step 5
544        for callback in scan_callback_list:
545            try:
546                self.scn_ad.ed.pop_event(scan_result.format(callback),
547                                         self.shorter_scan_timeout)
548                self.log.info("Found an advertisement.")
549            except Empty:
550                self.log.info("Did not find an advertisement.")
551                return False
552
553        # Step 7
554        self.adv_ad.droid.bleStopBleAdvertising(self.advertise_callback)
555        for callback in scan_callback_list:
556            self.scn_ad.droid.bleStopBleScan(callback)
557        return True
558