1#!/usr/bin/env python3.4
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the 'License');
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of 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,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import time
18import acts.test_utils.power.PowerBaseTest as PBT
19from acts.test_utils.bt.bt_test_utils import enable_bluetooth
20from acts.test_utils.bt.bt_test_utils import disable_bluetooth
21
22BT_BASE_UUID = '00000000-0000-1000-8000-00805F9B34FB'
23BT_CLASSICAL_DATA = [1, 2, 3]
24BLE_LOCATION_SCAN_ENABLE = 'settings put global ble_scan_always_enabled 1'
25BLE_LOCATION_SCAN_DISABLE = 'settings put global ble_scan_always_enabled 0'
26START_PMC_CMD = 'am start -n com.android.pmc/com.android.pmc.PMCMainActivity'
27PMC_VERBOSE_CMD = 'setprop log.tag.PMC VERBOSE'
28PMC_BASE_SCAN = 'am broadcast -a com.android.pmc.BLESCAN --es ScanMode '
29
30
31class PowerBTBaseTest(PBT.PowerBaseTest):
32    """Base class for BT power related tests.
33
34    Inherited from the PowerBaseTest class
35    """
36
37    def setup_test(self):
38
39        super().setup_test()
40        # Reset BT to factory defaults
41        self.dut.droid.bluetoothFactoryReset()
42        time.sleep(2)
43        # Start PMC app.
44        self.log.info('Start PMC app...')
45        self.dut.adb.shell(START_PMC_CMD)
46        self.dut.adb.shell(PMC_VERBOSE_CMD)
47
48    def teardown_test(self):
49        """Tear down necessary objects after test case is finished.
50
51        Bring down the AP interface, delete the bridge interface, stop the
52        packet sender, and reset the ethernet interface for the packet sender
53        """
54        super().teardown_test()
55        self.dut.droid.bluetoothFactoryReset()
56        self.dut.adb.shell(BLE_LOCATION_SCAN_DISABLE)
57
58    def teardown_class(self):
59        """Clean up the test class after tests finish running
60
61        """
62        super().teardown_class()
63        self.dut.droid.bluetoothFactoryReset()
64
65    def phone_setup_for_BT(self, bt_on, ble_on, screen_status):
66        """Sets the phone and Bluetooth in the desired state
67
68        Args:
69            bt_on: Enable/Disable BT
70            ble_on: Enable/Disable BLE
71            screen_status: screen ON or OFF
72        """
73
74        # Check if we are enabling a background scan
75        # TODO: Turn OFF cellular wihtout having to turn ON airplane mode
76        if bt_on == 'OFF' and ble_on == 'ON':
77            self.dut.adb.shell(BLE_LOCATION_SCAN_ENABLE)
78            self.dut.droid.connectivityToggleAirplaneMode(False)
79            time.sleep(2)
80
81        # Turn ON/OFF BT
82        if bt_on == 'ON':
83            enable_bluetooth(self.dut.droid, self.dut.ed)
84            self.dut.log.info('BT is ON')
85        else:
86            disable_bluetooth(self.dut.droid)
87            self.dut.droid.bluetoothDisableBLE()
88            self.dut.log.info('BT is OFF')
89        time.sleep(2)
90
91        # Turn ON/OFF BLE
92        if ble_on == 'ON':
93            self.dut.droid.bluetoothEnableBLE()
94            self.dut.log.info('BLE is ON')
95        else:
96            self.dut.droid.bluetoothDisableBLE()
97            self.dut.log.info('BLE is OFF')
98        time.sleep(2)
99
100        # Set the desired screen status
101        if screen_status == 'OFF':
102            self.dut.droid.goToSleepNow()
103            self.dut.log.info('Screen is OFF')
104        time.sleep(2)
105
106    def start_pmc_ble_scan(self,
107                           scan_mode,
108                           offset_start,
109                           scan_time,
110                           idle_time=None,
111                           num_reps=1):
112        """Starts a generic BLE scan via the PMC app
113
114        Args:
115            dut: object of the android device under test
116            scan mode: desired BLE scan type
117            offset_start: Time delay in seconds before scan starts
118            scan_time: active scan time
119            idle_time: iddle time (i.e., no scans occuring)
120            num_reps: Number of repetions of the ative+idle scan sequence
121        """
122        scan_dur = scan_time
123        if not idle_time:
124            idle_time = 0.2 * scan_time
125            scan_dur = 0.8 * scan_time
126
127        first_part_msg = '%s%s --es StartTime %d --es ScanTime %d' % (
128            PMC_BASE_SCAN, scan_mode, offset_start, scan_dur)
129
130        msg = '%s --es NoScanTime %d --es Repetitions %d' % (first_part_msg,
131                                                             idle_time,
132                                                             num_reps)
133
134        self.dut.log.info('Sent BLE scan broadcast message: %s', msg)
135        self.dut.adb.shell(msg)
136