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 base64 15from acts_contrib.test_utils.bt.protos import bluetooth_pb2 16 17 18def get_bluetooth_metrics(ad): 19 """ 20 Get metric proto from the Bluetooth stack 21 22 Parameter: 23 ad - Android device 24 25 Return: 26 a protobuf object representing Bluetooth metric 27 28 """ 29 bluetooth_log = bluetooth_pb2.BluetoothLog() 30 proto_native_str_64 = \ 31 ad.adb.shell("/system/bin/dumpsys bluetooth_manager --proto-bin") 32 proto_native_str = base64.b64decode(proto_native_str_64) 33 bluetooth_log.MergeFromString(proto_native_str) 34 return bluetooth_log 35 36def get_bluetooth_profile_connection_stats_map(bluetooth_log): 37 return project_pairs_list_to_map(bluetooth_log.profile_connection_stats, 38 lambda stats : stats.profile_id, 39 lambda stats : stats.num_times_connected, 40 lambda a, b : a + b) 41 42def get_bluetooth_headset_profile_connection_stats_map(bluetooth_log): 43 return project_pairs_list_to_map(bluetooth_log.headset_profile_connection_stats, 44 lambda stats : stats.profile_id, 45 lambda stats : stats.num_times_connected, 46 lambda a, b : a + b) 47 48def project_pairs_list_to_map(pairs_list, get_key, get_value, merge_value): 49 """ 50 Project a list of pairs (A, B) into a map of [A] --> B 51 :param pairs_list: list of pairs (A, B) 52 :param get_key: function used to get key from pair (A, B) 53 :param get_value: function used to get value from pair (A, B) 54 :param merge_value: function used to merge values of B 55 :return: a map of [A] --> B 56 """ 57 result = {} 58 for item in pairs_list: 59 my_key = get_key(item) 60 if my_key in result: 61 result[my_key] = merge_value(result[my_key], get_value(item)) 62 else: 63 result[my_key] = get_value(item) 64 return result 65