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