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