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 15 16from acts.libs.proto.proto_utils import parse_proto_to_ascii 17from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 18from acts_contrib.test_utils.bt.bt_metrics_utils import get_bluetooth_metrics 19from acts.utils import dump_string_to_file 20 21 22class BtMetricsBaseTest(BluetoothBaseTest): 23 """ 24 Base class for tests that requires dumping and parsing Bluetooth Metrics 25 """ 26 27 def __init__(self, controllers): 28 BluetoothBaseTest.__init__(self, controllers) 29 self.ad = self.android_devices[0] 30 31 def setup_class(self): 32 """ 33 This method finds bluetooth protobuf definitions from config file, 34 compile the protobuf and create a log directory for metrics dumping 35 :return: True on success, False on failure 36 """ 37 super(BtMetricsBaseTest, self).setup_class() 38 for ad in self.android_devices: 39 ad.metrics_path = os.path.join(ad.log_path, "BluetoothMetrics") 40 os.makedirs(ad.metrics_path, exist_ok=True) 41 return True 42 43 def setup_test(self): 44 """ 45 This method clears the current metrics, should be called after child 46 class setup_test() 47 :return: True 48 """ 49 super(BtMetricsBaseTest, self).setup_test() 50 # Clear all metrics 51 for ad in self.android_devices: 52 get_bluetooth_metrics(ad) 53 return True 54 55 def collect_bluetooth_manager_metrics_logs(self, ads): 56 """ 57 Collect Bluetooth metrics logs, save an ascii log to disk and return 58 both binary and ascii logs to caller 59 :param ads: list of active Android devices 60 :return: List of binary metrics logs, 61 List of ascii metrics logs 62 """ 63 bluetooth_logs = [] 64 bluetooth_logs_ascii = [] 65 for ad in ads: 66 serial = ad.serial 67 out_name = "{}_{}".format(serial, "bluetooth_metrics.txt") 68 bluetooth_log = get_bluetooth_metrics(ad) 69 bluetooth_log_ascii = parse_proto_to_ascii(bluetooth_log) 70 dump_string_to_file(bluetooth_log_ascii, 71 os.path.join(ad.metrics_path, out_name)) 72 bluetooth_logs.append(bluetooth_log) 73 bluetooth_logs_ascii.append(bluetooth_log_ascii) 74 return bluetooth_logs, bluetooth_logs_ascii 75