diff --git a/system/btif/src/bluetooth.cc b/system/btif/src/bluetooth.cc index a537dd11aa51479a874751b7b05b6afb10bdd930..1cf5dd9aefaf238759ded6299bc57eaa7de76024 100644 --- a/system/btif/src/bluetooth.cc +++ b/system/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/system/common/metric_id_allocator.cc b/system/common/metric_id_allocator.cc index 2667fa656f597f24b55aac59de3b6f520f925025..92a582250d3eb51bbcd582ac9c63fb79b8097b1f 100644 --- a/system/common/metric_id_allocator.cc +++ b/system/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<RawAddress, int>& 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/system/common/metric_id_allocator.h b/system/common/metric_id_allocator.h index f941fd0386f902a7fb1d815d823d6e1fc5d9a8e6..bbef02e671eefcea3c310088e8c14d51bdee8f94 100644 --- a/system/common/metric_id_allocator.h +++ b/system/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<RawAddress, int> paired_device_cache_; diff --git a/system/include/hardware/bluetooth.h b/system/include/hardware/bluetooth.h index 063abf9a3659096cd056edbe78758dfe19966bfd..1e3a391c2f6ac650ecc2922dc47713c5acf4d6dc 100644 --- a/system/include/hardware/bluetooth.h +++ b/system/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/system/service/hal/fake_bluetooth_interface.cc b/system/service/hal/fake_bluetooth_interface.cc index f1d03adc3294e46defe194c7f1d795c91e181661..4af8bc60d732770c8b197aa24de40afee4371538 100644 --- a/system/service/hal/fake_bluetooth_interface.cc +++ b/system/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