1 /*
2 * Copyright 2019 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 #include "hci/hci_layer.h"
18
19 #include "common/bind.h"
20 #include "common/callback.h"
21 #include "os/alarm.h"
22 #include "os/queue.h"
23 #include "packet/packet_builder.h"
24
25 namespace {
26 using bluetooth::common::Bind;
27 using bluetooth::common::BindOnce;
28 using bluetooth::common::Callback;
29 using bluetooth::common::Closure;
30 using bluetooth::common::OnceCallback;
31 using bluetooth::common::OnceClosure;
32 using bluetooth::hci::CommandCompleteView;
33 using bluetooth::hci::CommandPacketBuilder;
34 using bluetooth::hci::CommandStatusView;
35 using bluetooth::hci::EventPacketView;
36 using bluetooth::hci::LeMetaEventView;
37 using bluetooth::os::Handler;
38
39 class EventHandler {
40 public:
EventHandler()41 EventHandler() : event_handler(), handler(nullptr) {}
EventHandler(Callback<void (EventPacketView)> on_event,Handler * on_event_handler)42 EventHandler(Callback<void(EventPacketView)> on_event, Handler* on_event_handler)
43 : event_handler(std::move(on_event)), handler(on_event_handler) {}
44 Callback<void(EventPacketView)> event_handler;
45 Handler* handler;
46 };
47
48 class SubeventHandler {
49 public:
SubeventHandler()50 SubeventHandler() : subevent_handler(), handler(nullptr) {}
SubeventHandler(Callback<void (LeMetaEventView)> on_event,Handler * on_event_handler)51 SubeventHandler(Callback<void(LeMetaEventView)> on_event, Handler* on_event_handler)
52 : subevent_handler(std::move(on_event)), handler(on_event_handler) {}
53 Callback<void(LeMetaEventView)> subevent_handler;
54 Handler* handler;
55 };
56
57 class CommandQueueEntry {
58 public:
CommandQueueEntry(std::unique_ptr<CommandPacketBuilder> command_packet,OnceCallback<void (CommandCompleteView)> on_complete_function,Handler * handler)59 CommandQueueEntry(std::unique_ptr<CommandPacketBuilder> command_packet,
60 OnceCallback<void(CommandCompleteView)> on_complete_function, Handler* handler)
61 : command(std::move(command_packet)), waiting_for_status_(false), on_complete(std::move(on_complete_function)),
62 caller_handler(handler) {}
63
CommandQueueEntry(std::unique_ptr<CommandPacketBuilder> command_packet,OnceCallback<void (CommandStatusView)> on_status_function,Handler * handler)64 CommandQueueEntry(std::unique_ptr<CommandPacketBuilder> command_packet,
65 OnceCallback<void(CommandStatusView)> on_status_function, Handler* handler)
66 : command(std::move(command_packet)), waiting_for_status_(true), on_status(std::move(on_status_function)),
67 caller_handler(handler) {}
68
69 std::unique_ptr<CommandPacketBuilder> command;
70 bool waiting_for_status_;
71 OnceCallback<void(CommandStatusView)> on_status;
72 OnceCallback<void(CommandCompleteView)> on_complete;
73 Handler* caller_handler;
74 };
75 } // namespace
76
77 namespace bluetooth {
78 namespace hci {
79
80 using common::BidiQueue;
81 using common::BidiQueueEnd;
82 using os::Alarm;
83 using os::Handler;
84
85 namespace {
86 using hci::OpCode;
87 using hci::ResetCompleteView;
88
fail_if_reset_complete_not_success(CommandCompleteView complete)89 void fail_if_reset_complete_not_success(CommandCompleteView complete) {
90 auto reset_complete = ResetCompleteView::Create(complete);
91 ASSERT(reset_complete.IsValid());
92 ASSERT(reset_complete.GetStatus() == ErrorCode::SUCCESS);
93 }
94
on_hci_timeout(OpCode op_code)95 void on_hci_timeout(OpCode op_code) {
96 ASSERT_LOG(false, "Timed out waiting for 0x%02hx (%s)", op_code, OpCodeText(op_code).c_str());
97 }
98 } // namespace
99
100 class SecurityInterfaceImpl : public SecurityInterface {
101 public:
SecurityInterfaceImpl(HciLayer & hci)102 SecurityInterfaceImpl(HciLayer& hci) : hci_(hci) {}
103 virtual ~SecurityInterfaceImpl() = default;
104
EnqueueCommand(std::unique_ptr<SecurityCommandBuilder> command,common::OnceCallback<void (CommandCompleteView)> on_complete,os::Handler * handler)105 virtual void EnqueueCommand(std::unique_ptr<SecurityCommandBuilder> command,
106 common::OnceCallback<void(CommandCompleteView)> on_complete,
107 os::Handler* handler) override {
108 hci_.EnqueueCommand(std::move(command), std::move(on_complete), handler);
109 }
110
EnqueueCommand(std::unique_ptr<SecurityCommandBuilder> command,common::OnceCallback<void (CommandStatusView)> on_status,os::Handler * handler)111 virtual void EnqueueCommand(std::unique_ptr<SecurityCommandBuilder> command,
112 common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) override {
113 hci_.EnqueueCommand(std::move(command), std::move(on_status), handler);
114 }
115 HciLayer& hci_;
116 };
117
118 class LeSecurityInterfaceImpl : public LeSecurityInterface {
119 public:
LeSecurityInterfaceImpl(HciLayer & hci)120 LeSecurityInterfaceImpl(HciLayer& hci) : hci_(hci) {}
121 virtual ~LeSecurityInterfaceImpl() = default;
122
EnqueueCommand(std::unique_ptr<LeSecurityCommandBuilder> command,common::OnceCallback<void (CommandCompleteView)> on_complete,os::Handler * handler)123 virtual void EnqueueCommand(std::unique_ptr<LeSecurityCommandBuilder> command,
124 common::OnceCallback<void(CommandCompleteView)> on_complete,
125 os::Handler* handler) override {
126 hci_.EnqueueCommand(std::move(command), std::move(on_complete), handler);
127 }
128
EnqueueCommand(std::unique_ptr<LeSecurityCommandBuilder> command,common::OnceCallback<void (CommandStatusView)> on_status,os::Handler * handler)129 virtual void EnqueueCommand(std::unique_ptr<LeSecurityCommandBuilder> command,
130 common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) override {
131 hci_.EnqueueCommand(std::move(command), std::move(on_status), handler);
132 }
133 HciLayer& hci_;
134 };
135
136 class LeAdvertisingInterfaceImpl : public LeAdvertisingInterface {
137 public:
LeAdvertisingInterfaceImpl(HciLayer & hci)138 LeAdvertisingInterfaceImpl(HciLayer& hci) : hci_(hci) {}
139 virtual ~LeAdvertisingInterfaceImpl() = default;
140
EnqueueCommand(std::unique_ptr<LeAdvertisingCommandBuilder> command,common::OnceCallback<void (CommandCompleteView)> on_complete,os::Handler * handler)141 virtual void EnqueueCommand(std::unique_ptr<LeAdvertisingCommandBuilder> command,
142 common::OnceCallback<void(CommandCompleteView)> on_complete,
143 os::Handler* handler) override {
144 hci_.EnqueueCommand(std::move(command), std::move(on_complete), handler);
145 }
146
EnqueueCommand(std::unique_ptr<LeAdvertisingCommandBuilder> command,common::OnceCallback<void (CommandStatusView)> on_status,os::Handler * handler)147 virtual void EnqueueCommand(std::unique_ptr<LeAdvertisingCommandBuilder> command,
148 common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) override {
149 hci_.EnqueueCommand(std::move(command), std::move(on_status), handler);
150 }
151 HciLayer& hci_;
152 };
153
154 class LeScanningInterfaceImpl : public LeScanningInterface {
155 public:
LeScanningInterfaceImpl(HciLayer & hci)156 LeScanningInterfaceImpl(HciLayer& hci) : hci_(hci) {}
157 virtual ~LeScanningInterfaceImpl() = default;
158
EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,common::OnceCallback<void (CommandCompleteView)> on_complete,os::Handler * handler)159 virtual void EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,
160 common::OnceCallback<void(CommandCompleteView)> on_complete,
161 os::Handler* handler) override {
162 hci_.EnqueueCommand(std::move(command), std::move(on_complete), handler);
163 }
164
EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,common::OnceCallback<void (CommandStatusView)> on_status,os::Handler * handler)165 virtual void EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,
166 common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) override {
167 hci_.EnqueueCommand(std::move(command), std::move(on_status), handler);
168 }
169 HciLayer& hci_;
170 };
171
172 struct HciLayer::impl : public hal::HciHalCallbacks {
implbluetooth::hci::HciLayer::impl173 impl(HciLayer& module) : hal_(nullptr), module_(module) {}
174
~implbluetooth::hci::HciLayer::impl175 ~impl() {}
176
Startbluetooth::hci::HciLayer::impl177 void Start(hal::HciHal* hal) {
178 hal_ = hal;
179 hci_timeout_alarm_ = new Alarm(module_.GetHandler());
180
181 auto queue_end = acl_queue_.GetDownEnd();
182 Handler* handler = module_.GetHandler();
183 queue_end->RegisterDequeue(handler, Bind(&impl::dequeue_and_send_acl, common::Unretained(this)));
184 RegisterEventHandler(EventCode::COMMAND_COMPLETE, Bind(&impl::command_complete_callback, common::Unretained(this)),
185 handler);
186 RegisterEventHandler(EventCode::COMMAND_STATUS, Bind(&impl::command_status_callback, common::Unretained(this)),
187 handler);
188 RegisterEventHandler(EventCode::LE_META_EVENT, Bind(&impl::le_meta_event_callback, common::Unretained(this)),
189 handler);
190 // TODO find the right place
191 RegisterEventHandler(EventCode::PAGE_SCAN_REPETITION_MODE_CHANGE, Bind(&impl::drop, common::Unretained(this)),
192 handler);
193 RegisterEventHandler(EventCode::MAX_SLOTS_CHANGE, Bind(&impl::drop, common::Unretained(this)), handler);
194 RegisterEventHandler(EventCode::VENDOR_SPECIFIC, Bind(&impl::drop, common::Unretained(this)), handler);
195
196 EnqueueCommand(ResetBuilder::Create(), BindOnce(&fail_if_reset_complete_not_success), handler);
197 hal_->registerIncomingPacketCallback(this);
198 }
199
dropbluetooth::hci::HciLayer::impl200 void drop(EventPacketView) {}
201
dequeue_and_send_aclbluetooth::hci::HciLayer::impl202 void dequeue_and_send_acl() {
203 auto packet = acl_queue_.GetDownEnd()->TryDequeue();
204 send_acl(std::move(packet));
205 }
206
Stopbluetooth::hci::HciLayer::impl207 void Stop() {
208 hal_->unregisterIncomingPacketCallback();
209 UnregisterEventHandler(EventCode::COMMAND_COMPLETE);
210 UnregisterEventHandler(EventCode::COMMAND_STATUS);
211 UnregisterEventHandler(EventCode::LE_META_EVENT);
212 UnregisterEventHandler(EventCode::PAGE_SCAN_REPETITION_MODE_CHANGE);
213 UnregisterEventHandler(EventCode::MAX_SLOTS_CHANGE);
214 UnregisterEventHandler(EventCode::VENDOR_SPECIFIC);
215
216 acl_queue_.GetDownEnd()->UnregisterDequeue();
217 incoming_acl_packet_buffer_.Clear();
218 delete hci_timeout_alarm_;
219 command_queue_.clear();
220 hal_ = nullptr;
221 }
222
send_aclbluetooth::hci::HciLayer::impl223 void send_acl(std::unique_ptr<hci::BasePacketBuilder> packet) {
224 std::vector<uint8_t> bytes;
225 BitInserter bi(bytes);
226 packet->Serialize(bi);
227 hal_->sendAclData(bytes);
228 }
229
send_scobluetooth::hci::HciLayer::impl230 void send_sco(std::unique_ptr<hci::BasePacketBuilder> packet) {
231 std::vector<uint8_t> bytes;
232 BitInserter bi(bytes);
233 packet->Serialize(bi);
234 hal_->sendScoData(bytes);
235 }
236
command_status_callbackbluetooth::hci::HciLayer::impl237 void command_status_callback(EventPacketView event) {
238 CommandStatusView status_view = CommandStatusView::Create(event);
239 ASSERT(status_view.IsValid());
240 command_credits_ = status_view.GetNumHciCommandPackets();
241 OpCode op_code = status_view.GetCommandOpCode();
242 if (op_code == OpCode::NONE) {
243 send_next_command();
244 return;
245 }
246 ASSERT_LOG(!command_queue_.empty(), "Unexpected status event with OpCode 0x%02hx (%s)", op_code,
247 OpCodeText(op_code).c_str());
248 ASSERT_LOG(waiting_command_ == op_code, "Waiting for 0x%02hx (%s), got 0x%02hx (%s)", waiting_command_,
249 OpCodeText(waiting_command_).c_str(), op_code, OpCodeText(op_code).c_str());
250 ASSERT_LOG(command_queue_.front().waiting_for_status_,
251 "Waiting for command complete 0x%02hx (%s), got command status for 0x%02hx (%s)", waiting_command_,
252 OpCodeText(waiting_command_).c_str(), op_code, OpCodeText(op_code).c_str());
253 auto caller_handler = command_queue_.front().caller_handler;
254 caller_handler->Post(BindOnce(std::move(command_queue_.front().on_status), std::move(status_view)));
255 command_queue_.pop_front();
256 waiting_command_ = OpCode::NONE;
257 hci_timeout_alarm_->Cancel();
258 send_next_command();
259 }
260
command_complete_callbackbluetooth::hci::HciLayer::impl261 void command_complete_callback(EventPacketView event) {
262 CommandCompleteView complete_view = CommandCompleteView::Create(event);
263 ASSERT(complete_view.IsValid());
264 command_credits_ = complete_view.GetNumHciCommandPackets();
265 OpCode op_code = complete_view.GetCommandOpCode();
266 if (op_code == OpCode::NONE) {
267 send_next_command();
268 return;
269 }
270 ASSERT_LOG(command_queue_.size() > 0, "Unexpected command complete with OpCode 0x%02hx (%s)", op_code,
271 OpCodeText(op_code).c_str());
272 ASSERT_LOG(waiting_command_ == op_code, "Waiting for 0x%02hx (%s), got 0x%02hx (%s)", waiting_command_,
273 OpCodeText(waiting_command_).c_str(), op_code, OpCodeText(op_code).c_str());
274 ASSERT_LOG(!command_queue_.front().waiting_for_status_,
275 "Waiting for command status 0x%02hx (%s), got command complete for 0x%02hx (%s)", waiting_command_,
276 OpCodeText(waiting_command_).c_str(), op_code, OpCodeText(op_code).c_str());
277 auto caller_handler = command_queue_.front().caller_handler;
278 caller_handler->Post(BindOnce(std::move(command_queue_.front().on_complete), complete_view));
279 command_queue_.pop_front();
280 waiting_command_ = OpCode::NONE;
281 hci_timeout_alarm_->Cancel();
282 send_next_command();
283 }
284
le_meta_event_callbackbluetooth::hci::HciLayer::impl285 void le_meta_event_callback(EventPacketView event) {
286 LeMetaEventView meta_event_view = LeMetaEventView::Create(event);
287 ASSERT(meta_event_view.IsValid());
288 SubeventCode subevent_code = meta_event_view.GetSubeventCode();
289 ASSERT_LOG(subevent_handlers_.find(subevent_code) != subevent_handlers_.end(),
290 "Unhandled le event of type 0x%02hhx (%s)", subevent_code, SubeventCodeText(subevent_code).c_str());
291 auto& registered_handler = subevent_handlers_[subevent_code].subevent_handler;
292 subevent_handlers_[subevent_code].handler->Post(BindOnce(registered_handler, meta_event_view));
293 }
294
hciEventReceivedbluetooth::hci::HciLayer::impl295 void hciEventReceived(hal::HciPacket event_bytes) override {
296 auto packet = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(event_bytes));
297 EventPacketView event = EventPacketView::Create(packet);
298 ASSERT(event.IsValid());
299 module_.GetHandler()->Post(
300 BindOnce(&HciLayer::impl::hci_event_received_handler, common::Unretained(this), std::move(event)));
301 }
302
hci_event_received_handlerbluetooth::hci::HciLayer::impl303 void hci_event_received_handler(EventPacketView event) {
304 EventCode event_code = event.GetEventCode();
305 ASSERT_LOG(event_handlers_.find(event_code) != event_handlers_.end(), "Unhandled event of type 0x%02hhx (%s)",
306 event_code, EventCodeText(event_code).c_str());
307 auto& registered_handler = event_handlers_[event_code].event_handler;
308 event_handlers_[event_code].handler->Post(BindOnce(registered_handler, std::move(event)));
309 }
310
aclDataReceivedbluetooth::hci::HciLayer::impl311 void aclDataReceived(hal::HciPacket data_bytes) override {
312 auto packet =
313 packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(std::move(data_bytes)));
314 AclPacketView acl = AclPacketView::Create(packet);
315 incoming_acl_packet_buffer_.Enqueue(std::make_unique<AclPacketView>(acl), module_.GetHandler());
316 }
317
scoDataReceivedbluetooth::hci::HciLayer::impl318 void scoDataReceived(hal::HciPacket data_bytes) override {
319 auto packet = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(data_bytes));
320 ScoPacketView sco = ScoPacketView::Create(packet);
321 }
322
EnqueueCommandbluetooth::hci::HciLayer::impl323 void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
324 OnceCallback<void(CommandCompleteView)> on_complete, os::Handler* handler) {
325 module_.GetHandler()->Post(common::BindOnce(&impl::handle_enqueue_command_with_complete, common::Unretained(this),
326 std::move(command), std::move(on_complete),
327 common::Unretained(handler)));
328 }
329
EnqueueCommandbluetooth::hci::HciLayer::impl330 void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command, OnceCallback<void(CommandStatusView)> on_status,
331 os::Handler* handler) {
332 module_.GetHandler()->Post(common::BindOnce(&impl::handle_enqueue_command_with_status, common::Unretained(this),
333 std::move(command), std::move(on_status), common::Unretained(handler)));
334 }
335
handle_enqueue_command_with_completebluetooth::hci::HciLayer::impl336 void handle_enqueue_command_with_complete(std::unique_ptr<CommandPacketBuilder> command,
337 OnceCallback<void(CommandCompleteView)> on_complete, os::Handler* handler) {
338 command_queue_.emplace_back(std::move(command), std::move(on_complete), handler);
339
340 send_next_command();
341 }
342
handle_enqueue_command_with_statusbluetooth::hci::HciLayer::impl343 void handle_enqueue_command_with_status(std::unique_ptr<CommandPacketBuilder> command,
344 OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) {
345 command_queue_.emplace_back(std::move(command), std::move(on_status), handler);
346
347 send_next_command();
348 }
349
send_next_commandbluetooth::hci::HciLayer::impl350 void send_next_command() {
351 if (command_credits_ == 0) {
352 return;
353 }
354 if (waiting_command_ != OpCode::NONE) {
355 return;
356 }
357 if (command_queue_.size() == 0) {
358 return;
359 }
360 std::shared_ptr<std::vector<uint8_t>> bytes = std::make_shared<std::vector<uint8_t>>();
361 BitInserter bi(*bytes);
362 command_queue_.front().command->Serialize(bi);
363 hal_->sendHciCommand(*bytes);
364 auto cmd_view = CommandPacketView::Create(bytes);
365 ASSERT(cmd_view.IsValid());
366 OpCode op_code = cmd_view.GetOpCode();
367 waiting_command_ = op_code;
368 command_credits_ = 0; // Only allow one outstanding command
369 hci_timeout_alarm_->Schedule(BindOnce(&on_hci_timeout, op_code), kHciTimeoutMs);
370 }
371
GetAclQueueEndbluetooth::hci::HciLayer::impl372 BidiQueueEnd<AclPacketBuilder, AclPacketView>* GetAclQueueEnd() {
373 return acl_queue_.GetUpEnd();
374 }
375
RegisterEventHandlerbluetooth::hci::HciLayer::impl376 void RegisterEventHandler(EventCode event_code, Callback<void(EventPacketView)> event_handler, os::Handler* handler) {
377 module_.GetHandler()->Post(common::BindOnce(&impl::handle_register_event_handler, common::Unretained(this),
378 event_code, event_handler, common::Unretained(handler)));
379 }
380
handle_register_event_handlerbluetooth::hci::HciLayer::impl381 void handle_register_event_handler(EventCode event_code, Callback<void(EventPacketView)> event_handler,
382 os::Handler* handler) {
383 ASSERT_LOG(event_handlers_.count(event_code) == 0, "Can not register a second handler for event_code %02hhx (%s)",
384 event_code, EventCodeText(event_code).c_str());
385 EventHandler to_save(event_handler, handler);
386 event_handlers_[event_code] = to_save;
387 }
388
UnregisterEventHandlerbluetooth::hci::HciLayer::impl389 void UnregisterEventHandler(EventCode event_code) {
390 module_.GetHandler()->Post(
391 common::BindOnce(&impl::handle_unregister_event_handler, common::Unretained(this), event_code));
392 }
393
handle_unregister_event_handlerbluetooth::hci::HciLayer::impl394 void handle_unregister_event_handler(EventCode event_code) {
395 event_handlers_.erase(event_code);
396 }
397
RegisterLeEventHandlerbluetooth::hci::HciLayer::impl398 void RegisterLeEventHandler(SubeventCode subevent_code, Callback<void(LeMetaEventView)> event_handler,
399 os::Handler* handler) {
400 module_.GetHandler()->Post(common::BindOnce(&impl::handle_register_le_event_handler, common::Unretained(this),
401 subevent_code, event_handler, common::Unretained(handler)));
402 }
403
handle_register_le_event_handlerbluetooth::hci::HciLayer::impl404 void handle_register_le_event_handler(SubeventCode subevent_code, Callback<void(LeMetaEventView)> subevent_handler,
405 os::Handler* handler) {
406 ASSERT_LOG(subevent_handlers_.count(subevent_code) == 0,
407 "Can not register a second handler for subevent_code %02hhx (%s)", subevent_code,
408 SubeventCodeText(subevent_code).c_str());
409 SubeventHandler to_save(subevent_handler, handler);
410 subevent_handlers_[subevent_code] = to_save;
411 }
412
UnregisterLeEventHandlerbluetooth::hci::HciLayer::impl413 void UnregisterLeEventHandler(SubeventCode subevent_code) {
414 module_.GetHandler()->Post(
415 common::BindOnce(&impl::handle_unregister_le_event_handler, common::Unretained(this), subevent_code));
416 }
417
handle_unregister_le_event_handlerbluetooth::hci::HciLayer::impl418 void handle_unregister_le_event_handler(SubeventCode subevent_code) {
419 subevent_handlers_.erase(subevent_code);
420 }
421
422 // The HAL
423 hal::HciHal* hal_;
424
425 // A reference to the HciLayer module
426 HciLayer& module_;
427
428 // Interfaces
429 SecurityInterfaceImpl security_interface{module_};
430 LeSecurityInterfaceImpl le_security_interface{module_};
431 LeAdvertisingInterfaceImpl le_advertising_interface{module_};
432 LeScanningInterfaceImpl le_scanning_interface{module_};
433
434 // Command Handling
435 std::list<CommandQueueEntry> command_queue_;
436
437 std::map<EventCode, EventHandler> event_handlers_;
438 std::map<SubeventCode, SubeventHandler> subevent_handlers_;
439 OpCode waiting_command_{OpCode::NONE};
440 uint8_t command_credits_{1}; // Send reset first
441 Alarm* hci_timeout_alarm_{nullptr};
442
443 // Acl packets
444 BidiQueue<AclPacketView, AclPacketBuilder> acl_queue_{3 /* TODO: Set queue depth */};
445 os::EnqueueBuffer<AclPacketView> incoming_acl_packet_buffer_{acl_queue_.GetDownEnd()};
446 };
447
HciLayer()448 HciLayer::HciLayer() : impl_(std::make_unique<impl>(*this)) {}
449
~HciLayer()450 HciLayer::~HciLayer() {
451 impl_.reset();
452 }
453
EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,common::OnceCallback<void (CommandCompleteView)> on_complete,os::Handler * handler)454 void HciLayer::EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
455 common::OnceCallback<void(CommandCompleteView)> on_complete, os::Handler* handler) {
456 impl_->EnqueueCommand(std::move(command), std::move(on_complete), handler);
457 }
458
EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,common::OnceCallback<void (CommandStatusView)> on_status,os::Handler * handler)459 void HciLayer::EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
460 common::OnceCallback<void(CommandStatusView)> on_status, os::Handler* handler) {
461 impl_->EnqueueCommand(std::move(command), std::move(on_status), handler);
462 }
463
GetAclQueueEnd()464 common::BidiQueueEnd<AclPacketBuilder, AclPacketView>* HciLayer::GetAclQueueEnd() {
465 return impl_->GetAclQueueEnd();
466 }
467
RegisterEventHandler(EventCode event_code,common::Callback<void (EventPacketView)> event_handler,os::Handler * handler)468 void HciLayer::RegisterEventHandler(EventCode event_code, common::Callback<void(EventPacketView)> event_handler,
469 os::Handler* handler) {
470 impl_->RegisterEventHandler(event_code, std::move(event_handler), handler);
471 }
472
UnregisterEventHandler(EventCode event_code)473 void HciLayer::UnregisterEventHandler(EventCode event_code) {
474 impl_->UnregisterEventHandler(event_code);
475 }
476
RegisterLeEventHandler(SubeventCode subevent_code,common::Callback<void (LeMetaEventView)> event_handler,os::Handler * handler)477 void HciLayer::RegisterLeEventHandler(SubeventCode subevent_code, common::Callback<void(LeMetaEventView)> event_handler,
478 os::Handler* handler) {
479 impl_->RegisterLeEventHandler(subevent_code, std::move(event_handler), handler);
480 }
481
UnregisterLeEventHandler(SubeventCode subevent_code)482 void HciLayer::UnregisterLeEventHandler(SubeventCode subevent_code) {
483 impl_->UnregisterLeEventHandler(subevent_code);
484 }
485
GetSecurityInterface(common::Callback<void (EventPacketView)> event_handler,os::Handler * handler)486 SecurityInterface* HciLayer::GetSecurityInterface(common::Callback<void(EventPacketView)> event_handler,
487 os::Handler* handler) {
488 for (const auto event : SecurityInterface::SecurityEvents) {
489 RegisterEventHandler(event, event_handler, handler);
490 }
491 return &impl_->security_interface;
492 }
493
GetLeSecurityInterface(common::Callback<void (LeMetaEventView)> event_handler,os::Handler * handler)494 LeSecurityInterface* HciLayer::GetLeSecurityInterface(common::Callback<void(LeMetaEventView)> event_handler,
495 os::Handler* handler) {
496 for (const auto subevent : LeSecurityInterface::LeSecurityEvents) {
497 RegisterLeEventHandler(subevent, event_handler, handler);
498 }
499 return &impl_->le_security_interface;
500 }
501
GetLeAdvertisingInterface(common::Callback<void (LeMetaEventView)> event_handler,os::Handler * handler)502 LeAdvertisingInterface* HciLayer::GetLeAdvertisingInterface(common::Callback<void(LeMetaEventView)> event_handler,
503 os::Handler* handler) {
504 for (const auto subevent : LeAdvertisingInterface::LeAdvertisingEvents) {
505 RegisterLeEventHandler(subevent, event_handler, handler);
506 }
507 return &impl_->le_advertising_interface;
508 }
509
GetLeScanningInterface(common::Callback<void (LeMetaEventView)> event_handler,os::Handler * handler)510 LeScanningInterface* HciLayer::GetLeScanningInterface(common::Callback<void(LeMetaEventView)> event_handler,
511 os::Handler* handler) {
512 for (const auto subevent : LeScanningInterface::LeScanningEvents) {
513 RegisterLeEventHandler(subevent, event_handler, handler);
514 }
515 return &impl_->le_scanning_interface;
516 }
517
__anon89211d040302() 518 const ModuleFactory HciLayer::Factory = ModuleFactory([]() { return new HciLayer(); });
519
ListDependencies(ModuleList * list)520 void HciLayer::ListDependencies(ModuleList* list) {
521 list->add<hal::HciHal>();
522 }
523
Start()524 void HciLayer::Start() {
525 impl_->Start(GetDependency<hal::HciHal>());
526 }
527
Stop()528 void HciLayer::Stop() {
529 impl_->Stop();
530 }
531
ToString() const532 std::string HciLayer::ToString() const {
533 return "Hci Layer";
534 }
535 } // namespace hci
536 } // namespace bluetooth
537