summaryrefslogtreecommitdiff
path: root/system/gd/neighbor/name.cc
blob: 18acf2eb693882b57afc277616944f4f39384f50 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright 2019 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.h"

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

#include "common/bind.h"
#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
#include "module.h"
#include "os/handler.h"
#include "os/log.h"

namespace bluetooth {
namespace neighbor {

struct ReadCallbackHandler {
  ReadRemoteNameCallback callback;
  os::Handler* handler;
};

struct CancelCallbackHandler {
  CancelRemoteNameCallback callback;
  os::Handler* handler;
};

constexpr RemoteName kEmptyName{};

struct NameModule::impl {
  void ReadRemoteNameRequest(
      hci::Address address,
      hci::PageScanRepetitionMode page_scan_repetition_mode,
      uint16_t clock_offset,
      hci::ClockOffsetValid clock_offset_valid,
      ReadRemoteNameCallback callback,
      os::Handler* handler);
  void CancelRemoteNameRequest(hci::Address address, CancelRemoteNameCallback, os::Handler* handler);

  void Start();
  void Stop();

  impl(const NameModule& name_module);

 private:
  const NameModule& module_;

  void EnqueueCommandComplete(std::unique_ptr<hci::CommandBuilder> command);
  void EnqueueCommandStatus(std::unique_ptr<hci::CommandBuilder> command);

  void OnCommandComplete(hci::CommandCompleteView view);
  void OnCommandStatus(hci::CommandStatusView status);
  void OnEvent(hci::EventView view);

  std::unordered_map<hci::Address, std::unique_ptr<ReadCallbackHandler>> read_callback_handler_map_;
  std::unordered_map<hci::Address, std::unique_ptr<CancelCallbackHandler>> cancel_callback_handler_map_;

  hci::HciLayer* hci_layer_;
  os::Handler* handler_;
};

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

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

void neighbor::NameModule::impl::EnqueueCommandComplete(std::unique_ptr<hci::CommandBuilder> command) {
  hci_layer_->EnqueueCommand(std::move(command), handler_->BindOnceOn(this, &impl::OnCommandComplete));
}

void neighbor::NameModule::impl::EnqueueCommandStatus(std::unique_ptr<hci::CommandBuilder> command) {
  hci_layer_->EnqueueCommand(std::move(command), handler_->BindOnceOn(this, &impl::OnCommandStatus));
}

void neighbor::NameModule::impl::OnCommandComplete(hci::CommandCompleteView view) {
  switch (view.GetCommandOpCode()) {
    case hci::OpCode::REMOTE_NAME_REQUEST_CANCEL: {
      auto packet = hci::RemoteNameRequestCancelCompleteView::Create(view);
      ASSERT(packet.IsValid());
      ASSERT(packet.GetStatus() == hci::ErrorCode::SUCCESS);
      hci::Address address = packet.GetBdAddr();
      ASSERT(cancel_callback_handler_map_.find(address) != cancel_callback_handler_map_.end());
      cancel_callback_handler_map_.erase(address);
    } break;
    default:
      LOG_WARN("Unhandled command:%s", hci::OpCodeText(view.GetCommandOpCode()).c_str());
      break;
  }
}

void neighbor::NameModule::impl::OnCommandStatus(hci::CommandStatusView status) {
  ASSERT(status.GetStatus() == hci::ErrorCode::SUCCESS);

  switch (status.GetCommandOpCode()) {
    case hci::OpCode::REMOTE_NAME_REQUEST: {
      auto packet = hci::RemoteNameRequestStatusView::Create(status);
      ASSERT(packet.IsValid());
    } break;

    default:
      LOG_WARN("Unhandled command:%s", hci::OpCodeText(status.GetCommandOpCode()).c_str());
      break;
  }
}

void neighbor::NameModule::impl::OnEvent(hci::EventView view) {
  switch (view.GetEventCode()) {
    case hci::EventCode::REMOTE_NAME_REQUEST_COMPLETE: {
      auto packet = hci::RemoteNameRequestCompleteView::Create(view);
      ASSERT(packet.IsValid());
      hci::Address address = packet.GetBdAddr();
      ASSERT(read_callback_handler_map_.find(address) != read_callback_handler_map_.end());
      auto read_callback_handler = std::move(read_callback_handler_map_[address]);
      read_callback_handler->handler->Post(common::BindOnce(
          std::move(read_callback_handler->callback), packet.GetStatus(), address, packet.GetRemoteName()));
      read_callback_handler_map_.erase(address);
    } break;
    default:
      LOG_ERROR("Unhandled event:%s", hci::EventCodeText(view.GetEventCode()).c_str());
      break;
  }
}

void neighbor::NameModule::impl::Start() {
  hci_layer_ = module_.GetDependency<hci::HciLayer>();
  handler_ = module_.GetHandler();

  hci_layer_->RegisterEventHandler(
      hci::EventCode::REMOTE_NAME_REQUEST_COMPLETE, handler_->BindOn(this, &NameModule::impl::OnEvent));
}

void neighbor::NameModule::impl::Stop() {
  hci_layer_->UnregisterEventHandler(hci::EventCode::REMOTE_NAME_REQUEST_COMPLETE);
}

void neighbor::NameModule::impl::ReadRemoteNameRequest(
    hci::Address address,
    hci::PageScanRepetitionMode page_scan_repetition_mode,
    uint16_t clock_offset,
    hci::ClockOffsetValid clock_offset_valid,
    ReadRemoteNameCallback callback,
    os::Handler* handler) {
  LOG_INFO("%s Start read remote name request for %s", __func__, address.ToString().c_str());

  if (read_callback_handler_map_.find(address) != read_callback_handler_map_.end()) {
    LOG_WARN("Ignoring duplicate read remote name request to:%s", address.ToString().c_str());
    handler->Post(common::BindOnce(std::move(callback), hci::ErrorCode::UNSPECIFIED_ERROR, address, kEmptyName));
    return;
  }
  read_callback_handler_map_[address] = std::unique_ptr<ReadCallbackHandler>(new ReadCallbackHandler{
      .callback = std::move(callback),
      .handler = handler,
  });

  EnqueueCommandStatus(
      hci::RemoteNameRequestBuilder::Create(address, page_scan_repetition_mode, clock_offset, clock_offset_valid));
}

void neighbor::NameModule::impl::CancelRemoteNameRequest(
    hci::Address address, CancelRemoteNameCallback callback, os::Handler* handler) {
  LOG_INFO("%s Cancel remote name request for %s", __func__, address.ToString().c_str());

  if (cancel_callback_handler_map_.find(address) != cancel_callback_handler_map_.end()) {
    LOG_WARN("Ignoring duplicate cancel remote name request to:%s", address.ToString().c_str());
    handler->Post(common::BindOnce(std::move(callback), hci::ErrorCode::UNSPECIFIED_ERROR, address));
    return;
  }
  cancel_callback_handler_map_[address] = std::unique_ptr<CancelCallbackHandler>(new CancelCallbackHandler{
      .callback = std::move(callback),
      .handler = handler,
  });
  EnqueueCommandComplete(hci::RemoteNameRequestCancelBuilder::Create(address));
}

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

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

void neighbor::NameModule::ReadRemoteNameRequest(
    hci::Address address,
    hci::PageScanRepetitionMode page_scan_repetition_mode,
    uint16_t clock_offset,
    hci::ClockOffsetValid clock_offset_valid,
    ReadRemoteNameCallback callback,
    os::Handler* handler) {
  ASSERT(callback);
  ASSERT(handler != nullptr);
  GetHandler()->Post(common::BindOnce(
      &NameModule::impl::ReadRemoteNameRequest,
      common::Unretained(pimpl_.get()),
      address,
      page_scan_repetition_mode,
      clock_offset,
      clock_offset_valid,
      std::move(callback),
      handler));
}

void neighbor::NameModule::CancelRemoteNameRequest(
    hci::Address address, CancelRemoteNameCallback callback, os::Handler* handler) {
  ASSERT(callback);
  ASSERT(handler != nullptr);
  GetHandler()->Post(common::BindOnce(
      &NameModule::impl::CancelRemoteNameRequest,
      common::Unretained(pimpl_.get()),
      address,
      std::move(callback),
      handler));
}

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

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

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

}  // namespace neighbor
}  // namespace bluetooth