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