From feac0a2fbf90b4a515cf1b9790e33b7630fad44b Mon Sep 17 00:00:00 2001 From: Chen Chen Date: Thu, 5 Mar 2020 15:02:46 -0800 Subject: [PATCH] BluetoothMetricid: Add metric allocator api in java Bug: 142563377 Test: Manual atest AdapterServiceTest Change-Id: I46d1c1d5bf0c8beea1f46b1024cbd5ceb9c0b562 Merged-In: I46d1c1d5bf0c8beea1f46b1024cbd5ceb9c0b562 --- btif/src/bluetooth.cc | 7 +++++++ common/metric_id_allocator.cc | 12 ++++++------ common/metric_id_allocator.h | 2 +- include/hardware/bluetooth.h | 8 ++++++++ service/hal/fake_bluetooth_interface.cc | 1 + 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/btif/src/bluetooth.cc b/btif/src/bluetooth.cc index a537dd11a..1cf5dd9ae 100644 --- a/btif/src/bluetooth.cc +++ b/btif/src/bluetooth.cc @@ -63,6 +63,7 @@ #include "btsnoop.h" #include "btsnoop_mem.h" #include "common/address_obfuscator.h" +#include "common/metric_id_allocator.h" #include "common/metrics.h" #include "device/include/interop.h" #include "main/shim/dumpsys.h" @@ -460,6 +461,11 @@ static std::string obfuscate_address(const RawAddress& address) { address); } +static int get_metric_id(const RawAddress& address) { + return bluetooth::common::MetricIdAllocator::GetInstance().AllocateId( + address); +} + EXPORT_SYMBOL bt_interface_t bluetoothInterface = { sizeof(bluetoothInterface), init, @@ -496,4 +502,5 @@ EXPORT_SYMBOL bt_interface_t bluetoothInterface = { interop_database_add, get_avrcp_service, obfuscate_address, + get_metric_id, }; diff --git a/common/metric_id_allocator.cc b/common/metric_id_allocator.cc index 2667fa656..92a582250 100644 --- a/common/metric_id_allocator.cc +++ b/common/metric_id_allocator.cc @@ -27,7 +27,7 @@ namespace bluetooth { namespace common { -const std::string MetricIdAllocator::LOG_TAG = "BluetoothMetricIdAllocator"; +const std::string MetricIdAllocator::LOGGING_TAG = "BluetoothMetricIdAllocator"; const size_t MetricIdAllocator::kMaxNumUnpairedDevicesInMemory = 200; const size_t MetricIdAllocator::kMaxNumPairedDevicesInMemory = 400; const int MetricIdAllocator::kMinId = 1; @@ -42,11 +42,11 @@ static_assert((MetricIdAllocator::kMaxNumUnpairedDevicesInMemory + "kMaxNumPairedDevicesInMemory + MaxNumUnpairedDevicesInMemory"); MetricIdAllocator::MetricIdAllocator() - : paired_device_cache_(kMaxNumPairedDevicesInMemory, LOG_TAG, + : paired_device_cache_(kMaxNumPairedDevicesInMemory, LOGGING_TAG, [this](RawAddress dummy, int to_remove) { this->id_set_.erase(to_remove); }), - temporary_device_cache_(kMaxNumUnpairedDevicesInMemory, LOG_TAG, + temporary_device_cache_(kMaxNumUnpairedDevicesInMemory, LOGGING_TAG, [this](RawAddress dummy, int to_remove) { this->id_set_.erase(to_remove); }) {} @@ -62,7 +62,7 @@ bool MetricIdAllocator::Init( // init paired_devices_map if (paired_device_map.size() > kMaxNumPairedDevicesInMemory) { LOG(FATAL) - << LOG_TAG + << LOGGING_TAG << "Paired device map is bigger than kMaxNumPairedDevicesInMemory"; // fail loudly to let caller know return false; @@ -71,7 +71,7 @@ bool MetricIdAllocator::Init( next_id_ = kMinId; for (const std::pair& p : paired_device_map) { if (p.second < kMinId || p.second > kMaxId) { - LOG(FATAL) << LOG_TAG << "Invalid Bluetooth Metric Id in config"; + LOG(FATAL) << LOGGING_TAG << "Invalid Bluetooth Metric Id in config"; } paired_device_cache_.Put(p.first, p.second); id_set_.insert(p.second); @@ -130,7 +130,7 @@ int MetricIdAllocator::AllocateId(const RawAddress& mac_address) { next_id_++; if (next_id_ > kMaxId) { next_id_ = kMinId; - LOG(WARNING) << LOG_TAG << "Bluetooth metric id overflow."; + LOG(WARNING) << LOGGING_TAG << "Bluetooth metric id overflow."; } } id = next_id_++; diff --git a/common/metric_id_allocator.h b/common/metric_id_allocator.h index f941fd038..bbef02e67 100644 --- a/common/metric_id_allocator.h +++ b/common/metric_id_allocator.h @@ -107,7 +107,7 @@ class MetricIdAllocator { MetricIdAllocator(); private: - static const std::string LOG_TAG; + static const std::string LOGGING_TAG; mutable std::mutex id_allocator_mutex_; LruCache paired_device_cache_; diff --git a/include/hardware/bluetooth.h b/include/hardware/bluetooth.h index 063abf9a3..1e3a391c2 100644 --- a/include/hardware/bluetooth.h +++ b/include/hardware/bluetooth.h @@ -625,6 +625,14 @@ typedef struct { * @return a string of uint8_t that is unique to this MAC address */ std::string (*obfuscate_address)(const RawAddress& address); + + /** + * Get an incremental id for as primary key for Bluetooth metric and log + * + * @param address Bluetooth MAC address of Bluetooth device + * @return int incremental Bluetooth id + */ + int (*get_metric_id)(const RawAddress& address); } bt_interface_t; #define BLUETOOTH_INTERFACE_STRING "bluetoothInterface" diff --git a/service/hal/fake_bluetooth_interface.cc b/service/hal/fake_bluetooth_interface.cc index f1d03adc3..4af8bc60d 100644 --- a/service/hal/fake_bluetooth_interface.cc +++ b/service/hal/fake_bluetooth_interface.cc @@ -75,6 +75,7 @@ bt_interface_t fake_bt_iface = { nullptr, /* interop_database_add */ nullptr, /* get_avrcp_service */ nullptr, /* obfuscate_address */ + nullptr, /* get_metric_id */ }; } // namespace -- 2.11.0