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 #pragma once
18 
19 #include <functional>
20 #include <unordered_map>
21 #include "types/raw_address.h"
22 
23 namespace bluetooth {
24 namespace shim {
25 using CallbackLegacy =
26     std::function<bool(const RawAddress& address, const int id)>;
27 /**
28  * Initialize the allocator
29  *
30  * @param paired_device_map map from mac_address to id already saved
31  * in the disk before init
32  * @param save_id_callback a callback that will be called after successfully
33  * saving id for a paired device
34  * @param forget_device_callback a callback that will be called after
35  * successful id deletion for forgotten device,
36  * @return true if successfully initialized
37  */
38 bool InitMetricIdAllocator(
39     const std::unordered_map<RawAddress, int>& paired_device_map,
40     CallbackLegacy save_id_callback, CallbackLegacy forget_device_callback);
41 
42 /**
43  * Close the allocator. should be called when Bluetooth process is killed
44  *
45  * @return true if successfully close
46  */
47 bool CloseMetricIdAllocator();
48 
49 /**
50  * Check if no id saved in memory
51  *
52  * @return true if no id is saved
53  */
54 bool IsEmptyMetricIdAllocator();
55 
56 /**
57  * Allocate an id for a scanned device, or return the id if there is already
58  * one
59  *
60  * @param raw_address mac address of Bluetooth device
61  * @return the id of device
62  */
63 int AllocateIdFromMetricIdAllocator(const RawAddress& raw_address);
64 
65 /**
66  * Save the id for a paired device
67  *
68  * @param raw_address mac address of Bluetooth device
69  * @return true if save successfully
70  */
71 bool SaveDeviceOnMetricIdAllocator(const RawAddress& raw_address);
72 
73 /**
74  * Delete the id for a device to be forgotten
75  *
76  * @param raw_address mac address of Bluetooth device
77  */
78 void ForgetDeviceFromMetricIdAllocator(const RawAddress& raw_address);
79 
80 /**
81  * Check if an id is valid.
82  * The id should be less than or equal to kMaxId and bigger than or equal to
83  * kMinId
84  *
85  * @param mac_address mac address of Bluetooth device
86  * @return true if delete successfully
87  */
88 bool IsValidIdFromMetricIdAllocator(const int id);
89 
90 }  // namespace shim
91 }  // namespace bluetooth
92