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 csv
18import os
19import time
20import acts.test_utils.bt.bt_test_utils as btutils
21import acts.test_utils.power.PowerBTBaseTest as PBtBT
22
23EXTRA_PLAY_TIME = 30
24
25
26class PowerBTcalibrationTest(PBtBT.PowerBTBaseTest):
27    def setup_test(self):
28
29        super().setup_test()
30        self.attenuator = self.attenuators[0]
31        btutils.enable_bqr(self.dut)
32        time.sleep(2)
33        btutils.disable_bluetooth(self.dut.droid)
34        time.sleep(2)
35        btutils.enable_bluetooth(self.dut.droid, self.dut.ed)
36        btutils.connect_phone_to_headset(self.dut, self.bt_device, 60)
37        vol = self.dut.droid.getMaxMediaVolume() * self.volume
38        self.dut.droid.setMediaVolume(int(vol))
39
40        self.cal_data_path = os.path.join(self.log_path, 'Calibration')
41        self.log_file = os.path.join(self.cal_data_path, 'Cal_data.csv')
42        os.makedirs(os.path.dirname(self.log_file), exist_ok=True)
43
44    def test_calibrate(self):
45        """Run calibration to get attenuation value at each power level
46
47        """
48
49        self.cal_matrix = []
50        self.media.play()
51        time.sleep(EXTRA_PLAY_TIME)
52
53        # Loop through attenuation in 1 dB step until reaching at PL10
54        self.log.info('Starting Calibration Process')
55        pl10_count = 0
56        for i in range(int(self.attenuator.get_max_atten())):
57
58            self.attenuator.set_atten(i)
59            bt_metrics_dict = btutils.get_bt_metric(self.dut)
60            pwl = bt_metrics_dict['pwlv'][self.dut.serial]
61            self.log.info('Reach PW {} at attenuation {} dB'.format(pwl, i))
62            self.cal_matrix.append([i, pwl])
63            if pwl == 10:
64                pl10_count += 1
65            if pl10_count > 5:
66                break
67
68        # Write cal results to csv
69        with open(self.log_file, 'w', newline='') as f:
70            writer = csv.writer(f)
71            writer.writerows(self.cal_matrix)
72