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(const std::unordered_map<RawAddress, int>& paired_device_map,
31                            CallbackLegacy save_id_callback, CallbackLegacy forget_device_callback) {
32   std::unordered_map<Address, int> paired_device_map_gd;
33   for (const auto& device : paired_device_map) {
34     Address address = bluetooth::ToGdAddress(device.first);
35     paired_device_map_gd[address] = device.second;
36   }
37 
38   CallbackGd save_id_callback_gd = [save_id_callback](const Address& address, const int id) {
39     return save_id_callback(bluetooth::ToRawAddress(address), id);
40   };
41   CallbackGd forget_device_callback_gd = [forget_device_callback](const Address& address,
42                                                                   const int id) {
43     return forget_device_callback(bluetooth::ToRawAddress(address), id);
44   };
45   return MetricIdManager::GetInstance().Init(paired_device_map_gd, save_id_callback_gd,
46                                              forget_device_callback_gd);
47 }
48 
CloseMetricIdAllocator()49 bool CloseMetricIdAllocator() { return MetricIdManager::GetInstance().Close(); }
50 
IsEmptyMetricIdAllocator()51 bool IsEmptyMetricIdAllocator() { return MetricIdManager::GetInstance().IsEmpty(); }
52 
AllocateIdFromMetricIdAllocator(const RawAddress & raw_address)53 int AllocateIdFromMetricIdAllocator(const RawAddress& raw_address) {
54   Address address = bluetooth::ToGdAddress(raw_address);
55   return MetricIdManager::GetInstance().AllocateId(address);
56 }
57 
SaveDeviceOnMetricIdAllocator(const RawAddress & raw_address)58 bool SaveDeviceOnMetricIdAllocator(const RawAddress& raw_address) {
59   Address address = bluetooth::ToGdAddress(raw_address);
60   return MetricIdManager::GetInstance().SaveDevice(address);
61 }
62 
ForgetDeviceFromMetricIdAllocator(const RawAddress & raw_address)63 void ForgetDeviceFromMetricIdAllocator(const RawAddress& raw_address) {
64   Address address = bluetooth::ToGdAddress(raw_address);
65   return MetricIdManager::GetInstance().ForgetDevice(address);
66 }
67 
IsValidIdFromMetricIdAllocator(const int id)68 bool IsValidIdFromMetricIdAllocator(const int id) {
69   return MetricIdManager::GetInstance().IsValidId(id);
70 }
71 }  // namespace shim
72 }  // namespace bluetooth
73