summaryrefslogtreecommitdiff
path: root/system/main/shim/metric_id_api.cc
blob: fa912bc328b21ca62f1bf0e2b22e7fc5735e476e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <mutex>

#include "gd/common/metric_id_manager.h"
#include "gd/hci/address.h"
#include "main/shim/helpers.h"
#include "main/shim/metric_id_api.h"
#include "main/shim/shim.h"
#include "types/raw_address.h"

using bluetooth::common::MetricIdManager;
using bluetooth::hci::Address;

namespace bluetooth {
namespace shim {
using CallbackGd = std::function<bool(const Address& address, const int id)>;

bool InitMetricIdAllocator(
    const std::unordered_map<RawAddress, int>& paired_device_map,
    CallbackLegacy save_id_callback, CallbackLegacy forget_device_callback) {
  std::unordered_map<Address, int> paired_device_map_gd;
  for (const auto& device : paired_device_map) {
    Address address = bluetooth::ToGdAddress(device.first);
    paired_device_map_gd[address] = device.second;
  }

  CallbackGd save_id_callback_gd = [save_id_callback](const Address& address,
                                                      const int id) {
    return save_id_callback(bluetooth::ToRawAddress(address), id);
  };
  CallbackGd forget_device_callback_gd =
      [forget_device_callback](const Address& address, const int id) {
        return forget_device_callback(bluetooth::ToRawAddress(address), id);
      };
  return MetricIdManager::GetInstance().Init(
      paired_device_map_gd, save_id_callback_gd, forget_device_callback_gd);
}

bool CloseMetricIdAllocator() { return MetricIdManager::GetInstance().Close(); }

bool IsEmptyMetricIdAllocator() {
  return MetricIdManager::GetInstance().IsEmpty();
}

int AllocateIdFromMetricIdAllocator(const RawAddress& raw_address) {
  Address address = bluetooth::ToGdAddress(raw_address);
  return MetricIdManager::GetInstance().AllocateId(address);
}

bool SaveDeviceOnMetricIdAllocator(const RawAddress& raw_address) {
  Address address = bluetooth::ToGdAddress(raw_address);
  return MetricIdManager::GetInstance().SaveDevice(address);
}

void ForgetDeviceFromMetricIdAllocator(const RawAddress& raw_address) {
  Address address = bluetooth::ToGdAddress(raw_address);
  return MetricIdManager::GetInstance().ForgetDevice(address);
}

bool IsValidIdFromMetricIdAllocator(const int id) {
  return MetricIdManager::GetInstance().IsValidId(id);
}
}  // namespace shim
}  // namespace bluetooth