1 /*
2 * Copyright 2020 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 #define LOG_TAG "bt_gd_neigh"
17
18 #include "neighbor/name_db.h"
19
20 #include <memory>
21 #include <unordered_map>
22 #include <utility>
23
24 #include "common/bind.h"
25 #include "module.h"
26 #include "neighbor/name.h"
27 #include "os/handler.h"
28 #include "os/log.h"
29
30 namespace bluetooth {
31 namespace neighbor {
32
33 namespace {
34 struct PendingRemoteNameRead {
35 ReadRemoteNameDbCallback callback_;
36 os::Handler* handler_;
37 };
38 } // namespace
39
40 struct NameDbModule::impl {
41 void ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler);
42
43 bool IsNameCached(hci::Address address) const;
44 RemoteName ReadCachedRemoteName(hci::Address address) const;
45
46 impl(const NameDbModule& module);
47
48 void Start();
49 void Stop();
50
51 private:
52 std::unordered_map<hci::Address, PendingRemoteNameRead> address_to_pending_read_map_;
53 std::unordered_map<hci::Address, RemoteName> address_to_name_map_;
54
55 void OnRemoteNameResponse(hci::ErrorCode status, hci::Address address, RemoteName name);
56
57 neighbor::NameModule* name_module_;
58
59 const NameDbModule& module_;
60 os::Handler* handler_;
61 };
62
__anone43e45a90202() 63 const ModuleFactory neighbor::NameDbModule::Factory = ModuleFactory([]() { return new neighbor::NameDbModule(); });
64
impl(const neighbor::NameDbModule & module)65 neighbor::NameDbModule::impl::impl(const neighbor::NameDbModule& module) : module_(module) {}
66
ReadRemoteNameRequest(hci::Address address,ReadRemoteNameDbCallback callback,os::Handler * handler)67 void neighbor::NameDbModule::impl::ReadRemoteNameRequest(
68 hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler) {
69 if (address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end()) {
70 LOG_WARN("Already have remote read db in progress and currently can only have one outstanding");
71 return;
72 }
73
74 address_to_pending_read_map_[address] = {std::move(callback), std::move(handler)};
75
76 // TODO(cmanton) Use remote name request defaults for now
77 hci::PageScanRepetitionMode page_scan_repetition_mode = hci::PageScanRepetitionMode::R1;
78 uint16_t clock_offset = 0;
79 hci::ClockOffsetValid clock_offset_valid = hci::ClockOffsetValid::INVALID;
80 name_module_->ReadRemoteNameRequest(
81 address,
82 page_scan_repetition_mode,
83 clock_offset,
84 clock_offset_valid,
85 common::BindOnce(&NameDbModule::impl::OnRemoteNameResponse, common::Unretained(this)),
86 handler_);
87 }
88
OnRemoteNameResponse(hci::ErrorCode status,hci::Address address,RemoteName name)89 void neighbor::NameDbModule::impl::OnRemoteNameResponse(hci::ErrorCode status, hci::Address address, RemoteName name) {
90 ASSERT(address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end());
91 PendingRemoteNameRead callback_handler = std::move(address_to_pending_read_map_.at(address));
92
93 if (status == hci::ErrorCode::SUCCESS) {
94 address_to_name_map_[address] = name;
95 }
96 callback_handler.handler_->Post(
97 common::BindOnce(std::move(callback_handler.callback_), address, status == hci::ErrorCode::SUCCESS));
98 }
99
IsNameCached(hci::Address address) const100 bool neighbor::NameDbModule::impl::IsNameCached(hci::Address address) const {
101 return address_to_name_map_.count(address) == 1;
102 }
103
ReadCachedRemoteName(hci::Address address) const104 RemoteName neighbor::NameDbModule::impl::ReadCachedRemoteName(hci::Address address) const {
105 ASSERT(IsNameCached(address));
106 return address_to_name_map_.at(address);
107 }
108
109 /**
110 * General API here
111 */
NameDbModule()112 neighbor::NameDbModule::NameDbModule() : pimpl_(std::make_unique<impl>(*this)) {}
113
~NameDbModule()114 neighbor::NameDbModule::~NameDbModule() {
115 pimpl_.reset();
116 }
117
ReadRemoteNameRequest(hci::Address address,ReadRemoteNameDbCallback callback,os::Handler * handler)118 void neighbor::NameDbModule::ReadRemoteNameRequest(
119 hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler) {
120 GetHandler()->Post(common::BindOnce(
121 &NameDbModule::impl::ReadRemoteNameRequest,
122 common::Unretained(pimpl_.get()),
123 address,
124 std::move(callback),
125 handler));
126 }
127
IsNameCached(hci::Address address) const128 bool neighbor::NameDbModule::IsNameCached(hci::Address address) const {
129 return pimpl_->IsNameCached(address);
130 }
131
ReadCachedRemoteName(hci::Address address) const132 RemoteName neighbor::NameDbModule::ReadCachedRemoteName(hci::Address address) const {
133 return pimpl_->ReadCachedRemoteName(address);
134 }
135
Start()136 void neighbor::NameDbModule::impl::Start() {
137 name_module_ = module_.GetDependency<neighbor::NameModule>();
138 handler_ = module_.GetHandler();
139 }
140
Stop()141 void neighbor::NameDbModule::impl::Stop() {}
142
143 /**
144 * Module methods here
145 */
ListDependencies(ModuleList * list)146 void neighbor::NameDbModule::ListDependencies(ModuleList* list) {
147 list->add<neighbor::NameModule>();
148 }
149
Start()150 void neighbor::NameDbModule::Start() {
151 pimpl_->Start();
152 }
153
Stop()154 void neighbor::NameDbModule::Stop() {
155 pimpl_->Stop();
156 }
157
158 } // namespace neighbor
159 } // namespace bluetooth
160