1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "main/shim/metric_id_api.h"
17
18 #include "common/metric_id_manager.h"
19 #include "hci/address.h"
20 #include "main/shim/helpers.h"
21 #include "types/raw_address.h"
22
23 using bluetooth::common::MetricIdManager;
24 using bluetooth::hci::Address;
25
26 namespace bluetooth {
27 namespace shim {
28 using CallbackGd = std::function<bool(const Address& address, const int id)>;
29
InitMetricIdAllocator(const std::unordered_map<RawAddress,int> & paired_device_map,CallbackLegacy save_id_callback,CallbackLegacy forget_device_callback)30 bool InitMetricIdAllocator(
31 const std::unordered_map<RawAddress, int>& paired_device_map,
32 CallbackLegacy save_id_callback, CallbackLegacy forget_device_callback) {
33 std::unordered_map<Address, int> paired_device_map_gd;
34 for (const auto& device : paired_device_map) {
35 Address address = bluetooth::ToGdAddress(device.first);
36 paired_device_map_gd[address] = device.second;
37 }
38
39 CallbackGd save_id_callback_gd = [save_id_callback](const Address& address,
40 const int id) {
41 return save_id_callback(bluetooth::ToRawAddress(address), id);
42 };
43 CallbackGd forget_device_callback_gd =
44 [forget_device_callback](const Address& address, const int id) {
45 return forget_device_callback(bluetooth::ToRawAddress(address), id);
46 };
47 return MetricIdManager::GetInstance().Init(
48 paired_device_map_gd, save_id_callback_gd, forget_device_callback_gd);
49 }
50
CloseMetricIdAllocator()51 bool CloseMetricIdAllocator() { return MetricIdManager::GetInstance().Close(); }
52
IsEmptyMetricIdAllocator()53 bool IsEmptyMetricIdAllocator() {
54 return MetricIdManager::GetInstance().IsEmpty();
55 }
56
AllocateIdFromMetricIdAllocator(const RawAddress & raw_address)57 int AllocateIdFromMetricIdAllocator(const RawAddress& raw_address) {
58 Address address = bluetooth::ToGdAddress(raw_address);
59 return MetricIdManager::GetInstance().AllocateId(address);
60 }
61
SaveDeviceOnMetricIdAllocator(const RawAddress & raw_address)62 bool SaveDeviceOnMetricIdAllocator(const RawAddress& raw_address) {
63 Address address = bluetooth::ToGdAddress(raw_address);
64 return MetricIdManager::GetInstance().SaveDevice(address);
65 }
66
ForgetDeviceFromMetricIdAllocator(const RawAddress & raw_address)67 void ForgetDeviceFromMetricIdAllocator(const RawAddress& raw_address) {
68 Address address = bluetooth::ToGdAddress(raw_address);
69 return MetricIdManager::GetInstance().ForgetDevice(address);
70 }
71
IsValidIdFromMetricIdAllocator(const int id)72 bool IsValidIdFromMetricIdAllocator(const int id) {
73 return MetricIdManager::GetInstance().IsValidId(id);
74 }
75 } // namespace shim
76 } // namespace bluetooth
77