aboutsummaryrefslogtreecommitdiff
path: root/gd/neighbor/name_db.cc
blob: e82cc5e7e0773df8cbe527103f82ebb1eba5ce80 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright 2020 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.
 */
#define LOG_TAG "bt_gd_neigh"

#include "neighbor/name_db.h"

#include <memory>
#include <unordered_map>
#include <utility>

#include "common/bind.h"
#include "module.h"
#include "neighbor/name.h"
#include "os/handler.h"
#include "os/log.h"

namespace bluetooth {
namespace neighbor {

namespace {
struct PendingRemoteNameRead {
  ReadRemoteNameDbCallback callback_;
  os::Handler* handler_;
};
}  // namespace

struct NameDbModule::impl {
  void ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler);

  bool IsNameCached(hci::Address address) const;
  RemoteName ReadCachedRemoteName(hci::Address address) const;

  impl(const NameDbModule& module);

  void Start();
  void Stop();

 private:
  std::unordered_map<hci::Address, PendingRemoteNameRead> address_to_pending_read_map_;
  std::unordered_map<hci::Address, RemoteName> address_to_name_map_;

  void OnRemoteNameResponse(hci::ErrorCode status, hci::Address address, RemoteName name);

  neighbor::NameModule* name_module_;

  const NameDbModule& module_;
  os::Handler* handler_;
};

const ModuleFactory neighbor::NameDbModule::Factory = ModuleFactory([]() { return new neighbor::NameDbModule(); });

neighbor::NameDbModule::impl::impl(const neighbor::NameDbModule& module) : module_(module) {}

void neighbor::NameDbModule::impl::ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback,
                                                         os::Handler* handler) {
  if (address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end()) {
    LOG_WARN("Already have remote read db in progress and currently can only have one outstanding");
    return;
  }

  address_to_pending_read_map_[address] = {std::move(callback), std::move(handler)};

  // TODO(cmanton) Use remote name request defaults for now
  hci::PageScanRepetitionMode page_scan_repetition_mode = hci::PageScanRepetitionMode::R1;
  uint16_t clock_offset = 0;
  hci::ClockOffsetValid clock_offset_valid = hci::ClockOffsetValid::INVALID;
  name_module_->ReadRemoteNameRequest(
      address, page_scan_repetition_mode, clock_offset, clock_offset_valid,
      common::BindOnce(&NameDbModule::impl::OnRemoteNameResponse, common::Unretained(this)), handler_);
}

void neighbor::NameDbModule::impl::OnRemoteNameResponse(hci::ErrorCode status, hci::Address address, RemoteName name) {
  ASSERT(address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end());
  PendingRemoteNameRead callback_handler = std::move(address_to_pending_read_map_.at(address));

  if (status == hci::ErrorCode::SUCCESS) {
    address_to_name_map_[address] = name;
  }
  callback_handler.handler_->Post(
      common::BindOnce(std::move(callback_handler.callback_), address, status == hci::ErrorCode::SUCCESS));
}

bool neighbor::NameDbModule::impl::IsNameCached(hci::Address address) const {
  return address_to_name_map_.count(address) == 1;
}

RemoteName neighbor::NameDbModule::impl::ReadCachedRemoteName(hci::Address address) const {
  ASSERT(IsNameCached(address));
  return address_to_name_map_.at(address);
}

/**
 * General API here
 */
neighbor::NameDbModule::NameDbModule() : pimpl_(std::make_unique<impl>(*this)) {}

neighbor::NameDbModule::~NameDbModule() {
  pimpl_.reset();
}

void neighbor::NameDbModule::ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback,
                                                   os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(&NameDbModule::impl::ReadRemoteNameRequest, common::Unretained(pimpl_.get()),
                                      address, std::move(callback), handler));
}

bool neighbor::NameDbModule::IsNameCached(hci::Address address) const {
  return pimpl_->IsNameCached(address);
}

RemoteName neighbor::NameDbModule::ReadCachedRemoteName(hci::Address address) const {
  return pimpl_->ReadCachedRemoteName(address);
}

void neighbor::NameDbModule::impl::Start() {
  name_module_ = module_.GetDependency<neighbor::NameModule>();
  handler_ = module_.GetHandler();
}

void neighbor::NameDbModule::impl::Stop() {}

/**
 * Module methods here
 */
void neighbor::NameDbModule::ListDependencies(ModuleList* list) {
  list->add<neighbor::NameModule>();
}

void neighbor::NameDbModule::Start() {
  pimpl_->Start();
}

void neighbor::NameDbModule::Stop() {
  pimpl_->Stop();
}

}  // namespace neighbor
}  // namespace bluetooth