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.bt.BleEnum as bleenum
19import acts.test_utils.bt.bt_power_test_utils as btputils
20import acts.test_utils.power.PowerBTBaseTest as PBtBT
21
22BLE_LOCATION_SCAN_ENABLE = 'settings put secure location_mode 3'
23EXTRA_ADV_TIME = 10
24MONSOON_TAIL_CUT = 5
25
26
27class PowerBLEadvertiseTest(PBtBT.PowerBTBaseTest):
28    def __init__(self, configs):
29        super().__init__(configs)
30        req_params = ['adv_modes', 'adv_power_levels', 'adv_duration']
31        self.unpack_userparams(req_params)
32        # Loop all advertise modes and power levels
33        for adv_mode in self.adv_modes:
34            for adv_power_level in self.adv_power_levels:
35                self.generate_test_case(adv_mode, adv_power_level,
36                                        self.adv_duration)
37
38    def setup_class(self):
39
40        super().setup_class()
41        self.dut.adb.shell(BLE_LOCATION_SCAN_ENABLE)
42        # Make sure during power measurement, advertisement is always on
43        self.mon_info.duration = (self.adv_duration - self.mon_offset -
44                                  EXTRA_ADV_TIME - MONSOON_TAIL_CUT)
45
46    def generate_test_case(self, adv_mode, adv_power_level, adv_duration):
47        def test_case_fn():
48
49            self.measure_ble_advertise_power(adv_mode, adv_power_level,
50                                             adv_duration)
51
52        adv_mode_str = bleenum.AdvertiseSettingsAdvertiseMode(adv_mode).name
53        adv_txpl_str = bleenum.AdvertiseSettingsAdvertiseTxPower(
54            adv_power_level).name.strip('ADVERTISE').strip('_')
55        test_case_name = ('test_BLE_{}_{}'.format(adv_mode_str, adv_txpl_str))
56        setattr(self, test_case_name, test_case_fn)
57
58    def measure_ble_advertise_power(self, adv_mode, adv_power_level,
59                                    adv_duration):
60
61        btputils.start_apk_ble_adv(self.dut, adv_mode, adv_power_level,
62                                   adv_duration)
63        time.sleep(EXTRA_ADV_TIME)
64        self.measure_power_and_validate()
65