1# Copyright (C) 2016 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14import os
15import time
16
17from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
18from acts.libs.proto.proto_utils import compile_import_proto, parse_proto_to_ascii
19from acts.test_utils.bt.bt_metrics_utils import get_bluetooth_metrics
20from acts.utils import dump_string_to_file
21
22
23class BtMetricsBaseTest(BluetoothBaseTest):
24    """
25    Base class for tests that requires dumping and parsing Bluetooth Metrics
26    """
27
28    def __init__(self, controllers):
29        BluetoothBaseTest.__init__(self, controllers)
30        self.bluetooth_proto_path = None
31        self.ad = self.android_devices[0]
32
33    def setup_class(self):
34        """
35        This method finds bluetooth protobuf definitions from config file,
36        compile the protobuf and create a log directory for metrics dumping
37        :return: True on success, False on failure
38        """
39        super(BtMetricsBaseTest, self).setup_class()
40        self.bluetooth_proto_path = self.user_params["bluetooth_proto_path"][0]
41        if not os.path.isfile(self.bluetooth_proto_path):
42            try:
43                self.bluetooth_proto_path = "{}/bluetooth.proto".format(
44                    os.path.dirname(os.path.realpath(__file__)))
45            except Exception:
46                self.log.error("File not found.")
47            if not os.path.isfile(self.bluetooth_proto_path):
48                self.log.error("Unable to find Bluetooth proto {}.".format(
49                    self.bluetooth_proto_path))
50                return False
51        for ad in self.android_devices:
52            ad.metrics_path = os.path.join(ad.log_path, "BluetoothMetrics")
53            os.makedirs(ad.metrics_path, exist_ok=True)
54            ad.bluetooth_proto_module = \
55                compile_import_proto(ad.metrics_path, self.bluetooth_proto_path)
56            if not ad.bluetooth_proto_module:
57                self.log.error("Unable to compile bluetooth proto at " +
58                               self.bluetooth_proto_path)
59                return False
60        return True
61
62    def setup_test(self):
63        """
64        This method clears the current metrics, should be called after child
65        class setup_test()
66        :return: True
67        """
68        super(BtMetricsBaseTest, self).setup_test()
69        # Clear all metrics
70        for ad in self.android_devices:
71            get_bluetooth_metrics(ad, ad.bluetooth_proto_module)
72        return True
73
74    def collect_bluetooth_manager_metrics_logs(self, ads):
75        """
76        Collect Bluetooth metrics logs, save an ascii log to disk and return
77        both binary and ascii logs to caller
78        :param ads: list of active Android devices
79        :return: List of binary metrics logs,
80                List of ascii metrics logs
81        """
82        bluetooth_logs = []
83        bluetooth_logs_ascii = []
84        for ad in ads:
85            serial = ad.serial
86            out_name = "{}_{}".format(serial, "bluetooth_metrics.txt")
87            bluetooth_log = get_bluetooth_metrics(ad,
88                                                  ad.bluetooth_proto_module)
89            bluetooth_log_ascii = parse_proto_to_ascii(bluetooth_log)
90            dump_string_to_file(bluetooth_log_ascii,
91                                os.path.join(ad.metrics_path, out_name))
92            bluetooth_logs.append(bluetooth_log)
93            bluetooth_logs_ascii.append(bluetooth_log_ascii)
94        return bluetooth_logs, bluetooth_logs_ascii
95