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