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
17 #include <mutex>
18
19 #include "gd/common/metric_id_manager.h"
20 #include "gd/hci/address.h"
21 #include "main/shim/helpers.h"
22 #include "main/shim/metric_id_api.h"
23 #include "main/shim/shim.h"
24 #include "types/raw_address.h"
25
26 using bluetooth::common::MetricIdManager;
27 using bluetooth::hci::Address;
28
29 namespace bluetooth {
30 namespace shim {
31 using CallbackGd = std::function<bool(const Address& address, const int id)>;
32
InitMetricIdAllocator(const std::unordered_map<RawAddress,int> & paired_device_map,CallbackLegacy save_id_callback,CallbackLegacy forget_device_callback)33 bool InitMetricIdAllocator(
34 const std::unordered_map<RawAddress, int>& paired_device_map,
35 CallbackLegacy save_id_callback, CallbackLegacy forget_device_callback) {
36 std::unordered_map<Address, int> paired_device_map_gd;
37 for (const auto& device : paired_device_map) {
38 Address address = bluetooth::ToGdAddress(device.first);
39 paired_device_map_gd[address] = device.second;
40 }
41
42 CallbackGd save_id_callback_gd = [save_id_callback](const Address& address,
43 const int id) {
44 return save_id_callback(bluetooth::ToRawAddress(address), id);
45 };
46 CallbackGd forget_device_callback_gd =
47 [forget_device_callback](const Address& address, const int id) {
48 return forget_device_callback(bluetooth::ToRawAddress(address), id);
49 };
50 return MetricIdManager::GetInstance().Init(
51 paired_device_map_gd, save_id_callback_gd, forget_device_callback_gd);
52 }
53
CloseMetricIdAllocator()54 bool CloseMetricIdAllocator() { return MetricIdManager::GetInstance().Close(); }
55
IsEmptyMetricIdAllocator()56 bool IsEmptyMetricIdAllocator() {
57 return MetricIdManager::GetInstance().IsEmpty();
58 }
59
AllocateIdFromMetricIdAllocator(const RawAddress & raw_address)60 int AllocateIdFromMetricIdAllocator(const RawAddress& raw_address) {
61 Address address = bluetooth::ToGdAddress(raw_address);
62 return MetricIdManager::GetInstance().AllocateId(address);
63 }
64
SaveDeviceOnMetricIdAllocator(const RawAddress & raw_address)65 bool SaveDeviceOnMetricIdAllocator(const RawAddress& raw_address) {
66 Address address = bluetooth::ToGdAddress(raw_address);
67 return MetricIdManager::GetInstance().SaveDevice(address);
68 }
69
ForgetDeviceFromMetricIdAllocator(const RawAddress & raw_address)70 void ForgetDeviceFromMetricIdAllocator(const RawAddress& raw_address) {
71 Address address = bluetooth::ToGdAddress(raw_address);
72 return MetricIdManager::GetInstance().ForgetDevice(address);
73 }
74
IsValidIdFromMetricIdAllocator(const int id)75 bool IsValidIdFromMetricIdAllocator(const int id) {
76 return MetricIdManager::GetInstance().IsValidId(id);
77 }
78 } // namespace shim
79 } // namespace bluetooth
80