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/init_flags.h"
21 #include "common/stop_watch.h"
22 #include "hci/hci_metrics_logging.h"
23 #include "os/alarm.h"
24 #include "os/metrics.h"
25 #include "os/queue.h"
26 #include "packet/packet_builder.h"
27 #include "storage/storage_module.h"
28
29 namespace bluetooth {
30 namespace hci {
31 using bluetooth::common::BindOn;
32 using bluetooth::common::BindOnce;
33 using bluetooth::common::ContextualCallback;
34 using bluetooth::common::ContextualOnceCallback;
35 using bluetooth::hci::CommandBuilder;
36 using bluetooth::hci::CommandCompleteView;
37 using bluetooth::hci::CommandStatusView;
38 using bluetooth::hci::EventView;
39 using bluetooth::hci::LeMetaEventView;
40 using bluetooth::os::Handler;
41 using common::BidiQueue;
42 using common::BidiQueueEnd;
43 using hci::OpCode;
44 using hci::ResetCompleteView;
45 using os::Alarm;
46 using os::Handler;
47 using std::move;
48 using std::unique_ptr;
49
fail_if_reset_complete_not_success(CommandCompleteView complete)50 static void fail_if_reset_complete_not_success(CommandCompleteView complete) {
51 auto reset_complete = ResetCompleteView::Create(complete);
52 ASSERT(reset_complete.IsValid());
53 ASSERT(reset_complete.GetStatus() == ErrorCode::SUCCESS);
54 }
55
abort_after_time_out(OpCode op_code)56 static void abort_after_time_out(OpCode op_code) {
57 bluetooth::os::LogMetricHciTimeoutEvent(static_cast<uint32_t>(op_code));
58 ASSERT_LOG(false, "Done waiting for debug information after HCI timeout (%s)", OpCodeText(op_code).c_str());
59 }
60
61 class CommandQueueEntry {
62 public:
CommandQueueEntry(unique_ptr<CommandBuilder> command_packet,ContextualOnceCallback<void (CommandCompleteView)> on_complete_function)63 CommandQueueEntry(
64 unique_ptr<CommandBuilder> command_packet, ContextualOnceCallback<void(CommandCompleteView)> on_complete_function)
65 : command(move(command_packet)), waiting_for_status_(false), on_complete(move(on_complete_function)) {}
66
CommandQueueEntry(unique_ptr<CommandBuilder> command_packet,ContextualOnceCallback<void (CommandStatusView)> on_status_function)67 CommandQueueEntry(
68 unique_ptr<CommandBuilder> command_packet, ContextualOnceCallback<void(CommandStatusView)> on_status_function)
69 : command(move(command_packet)), waiting_for_status_(true), on_status(move(on_status_function)) {}
70
71 unique_ptr<CommandBuilder> command;
72 unique_ptr<CommandView> command_view;
73
74 bool waiting_for_status_;
75 ContextualOnceCallback<void(CommandStatusView)> on_status;
76 ContextualOnceCallback<void(CommandCompleteView)> on_complete;
77
78 template <typename TView>
GetCallback()79 ContextualOnceCallback<void(TView)>* GetCallback() {
80 return nullptr;
81 }
82
83 template <>
GetCallback()84 ContextualOnceCallback<void(CommandStatusView)>* GetCallback<CommandStatusView>() {
85 return &on_status;
86 }
87
88 template <>
GetCallback()89 ContextualOnceCallback<void(CommandCompleteView)>* GetCallback<CommandCompleteView>() {
90 return &on_complete;
91 }
92 };
93
94 struct HciLayer::impl {
implbluetooth::hci::HciLayer::impl95 impl(hal::HciHal* hal, HciLayer& module) : hal_(hal), module_(module) {
96 hci_timeout_alarm_ = new Alarm(module.GetHandler());
97 }
98
~implbluetooth::hci::HciLayer::impl99 ~impl() {
100 incoming_acl_buffer_.Clear();
101 incoming_iso_buffer_.Clear();
102 if (hci_timeout_alarm_ != nullptr) {
103 delete hci_timeout_alarm_;
104 }
105 if (hci_abort_alarm_ != nullptr) {
106 delete hci_abort_alarm_;
107 }
108 command_queue_.clear();
109 }
110
dropbluetooth::hci::HciLayer::impl111 void drop(EventView event) {
112 LOG_INFO("Dropping event %s", EventCodeText(event.GetEventCode()).c_str());
113 }
114
on_outbound_acl_readybluetooth::hci::HciLayer::impl115 void on_outbound_acl_ready() {
116 auto packet = acl_queue_.GetDownEnd()->TryDequeue();
117 std::vector<uint8_t> bytes;
118 BitInserter bi(bytes);
119 packet->Serialize(bi);
120 hal_->sendAclData(bytes);
121 }
122
on_outbound_iso_readybluetooth::hci::HciLayer::impl123 void on_outbound_iso_ready() {
124 auto packet = iso_queue_.GetDownEnd()->TryDequeue();
125 std::vector<uint8_t> bytes;
126 BitInserter bi(bytes);
127 packet->Serialize(bi);
128 hal_->sendIsoData(bytes);
129 }
130
131 template <typename TResponse>
enqueue_commandbluetooth::hci::HciLayer::impl132 void enqueue_command(unique_ptr<CommandBuilder> command, ContextualOnceCallback<void(TResponse)> on_response) {
133 command_queue_.emplace_back(move(command), move(on_response));
134 send_next_command();
135 }
136
on_command_statusbluetooth::hci::HciLayer::impl137 void on_command_status(EventView event) {
138 CommandStatusView response_view = CommandStatusView::Create(event);
139 ASSERT(response_view.IsValid());
140 OpCode op_code = response_view.GetCommandOpCode();
141 ErrorCode status = response_view.GetStatus();
142 if (status != ErrorCode::SUCCESS) {
143 LOG_ERROR(
144 "Received UNEXPECTED command status:%s opcode:0x%02hx (%s)",
145 ErrorCodeText(status).c_str(),
146 op_code,
147 OpCodeText(op_code).c_str());
148 }
149 handle_command_response<CommandStatusView>(event, "status");
150 }
151
on_command_completebluetooth::hci::HciLayer::impl152 void on_command_complete(EventView event) {
153 handle_command_response<CommandCompleteView>(event, "complete");
154 }
155
156 template <typename TResponse>
handle_command_responsebluetooth::hci::HciLayer::impl157 void handle_command_response(EventView event, std::string logging_id) {
158 TResponse response_view = TResponse::Create(event);
159 ASSERT(response_view.IsValid());
160 command_credits_ = response_view.GetNumHciCommandPackets();
161 OpCode op_code = response_view.GetCommandOpCode();
162 if (op_code == OpCode::NONE) {
163 send_next_command();
164 return;
165 }
166 bool is_status = logging_id == "status";
167
168 ASSERT_LOG(!command_queue_.empty(), "Unexpected %s event with OpCode 0x%02hx (%s)", logging_id.c_str(), op_code,
169 OpCodeText(op_code).c_str());
170 ASSERT_LOG(waiting_command_ == op_code, "Waiting for 0x%02hx (%s), got 0x%02hx (%s)", waiting_command_,
171 OpCodeText(waiting_command_).c_str(), op_code, OpCodeText(op_code).c_str());
172 ASSERT_LOG(command_queue_.front().waiting_for_status_ == is_status, "0x%02hx (%s) was not expecting %s event",
173 op_code, OpCodeText(op_code).c_str(), logging_id.c_str());
174
175 command_queue_.front().GetCallback<TResponse>()->Invoke(move(response_view));
176 command_queue_.pop_front();
177 waiting_command_ = OpCode::NONE;
178 if (hci_timeout_alarm_ != nullptr) {
179 hci_timeout_alarm_->Cancel();
180 send_next_command();
181 }
182 }
183
on_hci_timeoutbluetooth::hci::HciLayer::impl184 void on_hci_timeout(OpCode op_code) {
185 common::StopWatch::DumpStopWatchLog();
186 LOG_ERROR("Timed out waiting for 0x%02hx (%s)", op_code, OpCodeText(op_code).c_str());
187 // TODO: LogMetricHciTimeoutEvent(static_cast<uint32_t>(op_code));
188
189 LOG_ERROR("Flushing %zd waiting commands", command_queue_.size());
190 // Clear any waiting commands (there is an abort coming anyway)
191 command_queue_.clear();
192 command_credits_ = 1;
193 waiting_command_ = OpCode::NONE;
194 enqueue_command(
195 ControllerDebugInfoBuilder::Create(), module_.GetHandler()->BindOnce(&fail_if_reset_complete_not_success));
196 // Don't time out for this one;
197 if (hci_timeout_alarm_ != nullptr) {
198 hci_timeout_alarm_->Cancel();
199 delete hci_timeout_alarm_;
200 hci_timeout_alarm_ = nullptr;
201 }
202 if (hci_abort_alarm_ == nullptr) {
203 hci_abort_alarm_ = new Alarm(module_.GetHandler());
204 hci_abort_alarm_->Schedule(BindOnce(&abort_after_time_out, op_code), kHciTimeoutRestartMs);
205 } else {
206 LOG_WARN("Unable to schedul abort timer");
207 }
208 }
209
send_next_commandbluetooth::hci::HciLayer::impl210 void send_next_command() {
211 if (command_credits_ == 0) {
212 return;
213 }
214 if (waiting_command_ != OpCode::NONE) {
215 return;
216 }
217 if (command_queue_.size() == 0) {
218 return;
219 }
220 std::shared_ptr<std::vector<uint8_t>> bytes = std::make_shared<std::vector<uint8_t>>();
221 BitInserter bi(*bytes);
222 command_queue_.front().command->Serialize(bi);
223 hal_->sendHciCommand(*bytes);
224
225 auto cmd_view = CommandView::Create(PacketView<kLittleEndian>(bytes));
226 ASSERT(cmd_view.IsValid());
227 OpCode op_code = cmd_view.GetOpCode();
228 command_queue_.front().command_view = std::make_unique<CommandView>(std::move(cmd_view));
229 log_link_layer_connection_command_status(command_queue_.front().command_view, ErrorCode::STATUS_UNKNOWN);
230 log_classic_pairing_command_status(command_queue_.front().command_view, ErrorCode::STATUS_UNKNOWN);
231 waiting_command_ = op_code;
232 command_credits_ = 0; // Only allow one outstanding command
233 if (hci_timeout_alarm_ != nullptr) {
234 hci_timeout_alarm_->Schedule(BindOnce(&impl::on_hci_timeout, common::Unretained(this), op_code), kHciTimeoutMs);
235 } else {
236 LOG_WARN("%s sent without an hci-timeout timer", OpCodeText(op_code).c_str());
237 }
238 }
239
register_eventbluetooth::hci::HciLayer::impl240 void register_event(EventCode event, ContextualCallback<void(EventView)> handler) {
241 ASSERT_LOG(
242 event != EventCode::LE_META_EVENT,
243 "Can not register handler for %02hhx (%s)",
244 EventCode::LE_META_EVENT,
245 EventCodeText(EventCode::LE_META_EVENT).c_str());
246 ASSERT_LOG(event_handlers_.count(event) == 0, "Can not register a second handler for %02hhx (%s)", event,
247 EventCodeText(event).c_str());
248 event_handlers_[event] = handler;
249 }
250
unregister_eventbluetooth::hci::HciLayer::impl251 void unregister_event(EventCode event) {
252 event_handlers_.erase(event);
253 }
254
register_le_meta_eventbluetooth::hci::HciLayer::impl255 void register_le_meta_event(ContextualCallback<void(EventView)> handler) {
256 ASSERT_LOG(
257 event_handlers_.count(EventCode::LE_META_EVENT) == 0,
258 "Can not register a second handler for %02hhx (%s)",
259 EventCode::LE_META_EVENT,
260 EventCodeText(EventCode::LE_META_EVENT).c_str());
261 event_handlers_[EventCode::LE_META_EVENT] = handler;
262 }
263
unregister_le_meta_eventbluetooth::hci::HciLayer::impl264 void unregister_le_meta_event() {
265 unregister_event(EventCode::LE_META_EVENT);
266 }
267
register_le_eventbluetooth::hci::HciLayer::impl268 void register_le_event(SubeventCode event, ContextualCallback<void(LeMetaEventView)> handler) {
269 ASSERT_LOG(subevent_handlers_.count(event) == 0, "Can not register a second handler for %02hhx (%s)", event,
270 SubeventCodeText(event).c_str());
271 subevent_handlers_[event] = handler;
272 }
273
unregister_le_eventbluetooth::hci::HciLayer::impl274 void unregister_le_event(SubeventCode event) {
275 subevent_handlers_.erase(subevent_handlers_.find(event));
276 }
277
abort_after_root_inflammationbluetooth::hci::HciLayer::impl278 static void abort_after_root_inflammation(uint8_t vse_error) {
279 ASSERT_LOG(false, "Root inflammation with reason 0x%02hhx", vse_error);
280 }
281
handle_root_inflammationbluetooth::hci::HciLayer::impl282 void handle_root_inflammation(uint8_t vse_error_reason) {
283 LOG_ERROR("Received a Root Inflammation Event vendor reason 0x%02hhx, scheduling an abort",
284 vse_error_reason);
285 bluetooth::os::LogMetricBluetoothHalCrashReason(Address::kEmpty, 0, vse_error_reason);
286 // Add Logging for crash reason
287 if (hci_timeout_alarm_ != nullptr) {
288 hci_timeout_alarm_->Cancel();
289 delete hci_timeout_alarm_;
290 hci_timeout_alarm_ = nullptr;
291 }
292 if (hci_abort_alarm_ == nullptr) {
293 hci_abort_alarm_ = new Alarm(module_.GetHandler());
294 hci_abort_alarm_->Schedule(BindOnce(&abort_after_root_inflammation, vse_error_reason), kHciTimeoutRestartMs);
295 } else {
296 LOG_WARN("Abort timer already scheduled");
297 }
298 }
299
on_hci_eventbluetooth::hci::HciLayer::impl300 void on_hci_event(EventView event) {
301 ASSERT(event.IsValid());
302 log_hci_event(command_queue_.front().command_view, event, module_.GetDependency<storage::StorageModule>());
303 EventCode event_code = event.GetEventCode();
304 // Root Inflamation is a special case, since it aborts here
305 if (event_code == EventCode::VENDOR_SPECIFIC) {
306 auto view = VendorSpecificEventView::Create(event);
307 ASSERT(view.IsValid());
308 if (view.GetSubeventCode() == VseSubeventCode::BQR_EVENT) {
309 auto bqr_quality_view = BqrLinkQualityEventView::Create(BqrEventView::Create(view));
310 auto inflammation = BqrRootInflammationEventView::Create(bqr_quality_view);
311 if (bqr_quality_view.IsValid() && inflammation.IsValid()) {
312 handle_root_inflammation(inflammation.GetVendorSpecificErrorCode());
313 return;
314 }
315 }
316 }
317 if (event_handlers_.find(event_code) == event_handlers_.end()) {
318 LOG_WARN("Unhandled event of type 0x%02hhx (%s)", event_code, EventCodeText(event_code).c_str());
319 return;
320 }
321 event_handlers_[event_code].Invoke(event);
322 }
323
on_le_meta_eventbluetooth::hci::HciLayer::impl324 void on_le_meta_event(EventView event) {
325 LeMetaEventView meta_event_view = LeMetaEventView::Create(event);
326 ASSERT(meta_event_view.IsValid());
327 SubeventCode subevent_code = meta_event_view.GetSubeventCode();
328 if (subevent_handlers_.find(subevent_code) == subevent_handlers_.end()) {
329 LOG_WARN("Unhandled le subevent of type 0x%02hhx (%s)", subevent_code, SubeventCodeText(subevent_code).c_str());
330 return;
331 }
332 subevent_handlers_[subevent_code].Invoke(meta_event_view);
333 }
334
335 hal::HciHal* hal_;
336 HciLayer& module_;
337
338 // Command Handling
339 std::list<CommandQueueEntry> command_queue_;
340
341 std::map<EventCode, ContextualCallback<void(EventView)>> event_handlers_;
342 std::map<SubeventCode, ContextualCallback<void(LeMetaEventView)>> subevent_handlers_;
343 OpCode waiting_command_{OpCode::NONE};
344 uint8_t command_credits_{1}; // Send reset first
345 Alarm* hci_timeout_alarm_{nullptr};
346 Alarm* hci_abort_alarm_{nullptr};
347
348 // Acl packets
349 BidiQueue<AclView, AclBuilder> acl_queue_{3 /* TODO: Set queue depth */};
350 os::EnqueueBuffer<AclView> incoming_acl_buffer_{acl_queue_.GetDownEnd()};
351
352 // ISO packets
353 BidiQueue<IsoView, IsoBuilder> iso_queue_{3 /* TODO: Set queue depth */};
354 os::EnqueueBuffer<IsoView> incoming_iso_buffer_{iso_queue_.GetDownEnd()};
355 };
356
357 // All functions here are running on the HAL thread
358 struct HciLayer::hal_callbacks : public hal::HciHalCallbacks {
hal_callbacksbluetooth::hci::HciLayer::hal_callbacks359 hal_callbacks(HciLayer& module) : module_(module) {}
360
hciEventReceivedbluetooth::hci::HciLayer::hal_callbacks361 void hciEventReceived(hal::HciPacket event_bytes) override {
362 auto packet = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(event_bytes));
363 EventView event = EventView::Create(packet);
364 module_.CallOn(module_.impl_, &impl::on_hci_event, move(event));
365 }
366
aclDataReceivedbluetooth::hci::HciLayer::hal_callbacks367 void aclDataReceived(hal::HciPacket data_bytes) override {
368 auto packet = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(move(data_bytes)));
369 auto acl = std::make_unique<AclView>(AclView::Create(packet));
370 module_.impl_->incoming_acl_buffer_.Enqueue(move(acl), module_.GetHandler());
371 }
372
scoDataReceivedbluetooth::hci::HciLayer::hal_callbacks373 void scoDataReceived(hal::HciPacket data_bytes) override {
374 // Not implemented yet
375 }
376
isoDataReceivedbluetooth::hci::HciLayer::hal_callbacks377 void isoDataReceived(hal::HciPacket data_bytes) override {
378 auto packet = packet::PacketView<packet::kLittleEndian>(std::make_shared<std::vector<uint8_t>>(move(data_bytes)));
379 auto iso = std::make_unique<IsoView>(IsoView::Create(packet));
380 module_.impl_->incoming_iso_buffer_.Enqueue(move(iso), module_.GetHandler());
381 }
382
383 HciLayer& module_;
384 };
385
HciLayer()386 HciLayer::HciLayer() : impl_(nullptr), hal_callbacks_(nullptr) {}
387
~HciLayer()388 HciLayer::~HciLayer() {
389 }
390
GetAclQueueEnd()391 common::BidiQueueEnd<AclBuilder, AclView>* HciLayer::GetAclQueueEnd() {
392 return impl_->acl_queue_.GetUpEnd();
393 }
394
GetIsoQueueEnd()395 common::BidiQueueEnd<IsoBuilder, IsoView>* HciLayer::GetIsoQueueEnd() {
396 return impl_->iso_queue_.GetUpEnd();
397 }
398
EnqueueCommand(unique_ptr<CommandBuilder> command,ContextualOnceCallback<void (CommandCompleteView)> on_complete)399 void HciLayer::EnqueueCommand(
400 unique_ptr<CommandBuilder> command, ContextualOnceCallback<void(CommandCompleteView)> on_complete) {
401 CallOn(impl_, &impl::enqueue_command<CommandCompleteView>, move(command), move(on_complete));
402 }
403
EnqueueCommand(unique_ptr<CommandBuilder> command,ContextualOnceCallback<void (CommandStatusView)> on_status)404 void HciLayer::EnqueueCommand(
405 unique_ptr<CommandBuilder> command, ContextualOnceCallback<void(CommandStatusView)> on_status) {
406 CallOn(impl_, &impl::enqueue_command<CommandStatusView>, move(command), move(on_status));
407 }
408
RegisterEventHandler(EventCode event,ContextualCallback<void (EventView)> handler)409 void HciLayer::RegisterEventHandler(EventCode event, ContextualCallback<void(EventView)> handler) {
410 CallOn(impl_, &impl::register_event, event, handler);
411 }
412
RegisterLeMetaEventHandler(ContextualCallback<void (EventView)> handler)413 void HciLayer::RegisterLeMetaEventHandler(ContextualCallback<void(EventView)> handler) {
414 CallOn(impl_, &impl::register_le_meta_event, handler);
415 }
416
UnregisterEventHandler(EventCode event)417 void HciLayer::UnregisterEventHandler(EventCode event) {
418 CallOn(impl_, &impl::unregister_event, event);
419 }
420
RegisterLeEventHandler(SubeventCode event,ContextualCallback<void (LeMetaEventView)> handler)421 void HciLayer::RegisterLeEventHandler(SubeventCode event, ContextualCallback<void(LeMetaEventView)> handler) {
422 CallOn(impl_, &impl::register_le_event, event, handler);
423 }
424
UnregisterLeEventHandler(SubeventCode event)425 void HciLayer::UnregisterLeEventHandler(SubeventCode event) {
426 CallOn(impl_, &impl::unregister_le_event, event);
427 }
428
on_disconnection_complete(EventView event_view)429 void HciLayer::on_disconnection_complete(EventView event_view) {
430 auto disconnection_view = DisconnectionCompleteView::Create(event_view);
431 if (!disconnection_view.IsValid()) {
432 LOG_INFO("Dropping invalid disconnection packet");
433 return;
434 }
435
436 uint16_t handle = disconnection_view.GetConnectionHandle();
437 ErrorCode reason = disconnection_view.GetReason();
438 Disconnect(handle, reason);
439 }
440
Disconnect(uint16_t handle,ErrorCode reason)441 void HciLayer::Disconnect(uint16_t handle, ErrorCode reason) {
442 for (auto callback : disconnect_handlers_) {
443 callback.Invoke(handle, reason);
444 }
445 }
446
on_read_remote_version_complete(EventView event_view)447 void HciLayer::on_read_remote_version_complete(EventView event_view) {
448 auto view = ReadRemoteVersionInformationCompleteView::Create(event_view);
449 ASSERT_LOG(view.IsValid(), "Read remote version information packet invalid");
450 ReadRemoteVersion(
451 view.GetStatus(),
452 view.GetConnectionHandle(),
453 view.GetVersion(),
454 view.GetManufacturerName(),
455 view.GetSubVersion());
456 }
457
ReadRemoteVersion(hci::ErrorCode hci_status,uint16_t handle,uint8_t version,uint16_t manufacturer_name,uint16_t sub_version)458 void HciLayer::ReadRemoteVersion(
459 hci::ErrorCode hci_status, uint16_t handle, uint8_t version, uint16_t manufacturer_name, uint16_t sub_version) {
460 for (auto callback : read_remote_version_handlers_) {
461 callback.Invoke(hci_status, handle, version, manufacturer_name, sub_version);
462 }
463 }
464
GetAclConnectionInterface(ContextualCallback<void (EventView)> event_handler,ContextualCallback<void (uint16_t,ErrorCode)> on_disconnect,ContextualCallback<void (hci::ErrorCode hci_status,uint16_t,uint8_t version,uint16_t manufacturer_name,uint16_t sub_version)> on_read_remote_version)465 AclConnectionInterface* HciLayer::GetAclConnectionInterface(
466 ContextualCallback<void(EventView)> event_handler,
467 ContextualCallback<void(uint16_t, ErrorCode)> on_disconnect,
468 ContextualCallback<
469 void(hci::ErrorCode hci_status, uint16_t, uint8_t version, uint16_t manufacturer_name, uint16_t sub_version)>
470 on_read_remote_version) {
471 for (const auto event : AclConnectionEvents) {
472 RegisterEventHandler(event, event_handler);
473 }
474 disconnect_handlers_.push_back(on_disconnect);
475 read_remote_version_handlers_.push_back(on_read_remote_version);
476 return &acl_connection_manager_interface_;
477 }
478
GetLeAclConnectionInterface(ContextualCallback<void (LeMetaEventView)> event_handler,ContextualCallback<void (uint16_t,ErrorCode)> on_disconnect,ContextualCallback<void (hci::ErrorCode hci_status,uint16_t,uint8_t version,uint16_t manufacturer_name,uint16_t sub_version)> on_read_remote_version)479 LeAclConnectionInterface* HciLayer::GetLeAclConnectionInterface(
480 ContextualCallback<void(LeMetaEventView)> event_handler,
481 ContextualCallback<void(uint16_t, ErrorCode)> on_disconnect,
482 ContextualCallback<
483 void(hci::ErrorCode hci_status, uint16_t, uint8_t version, uint16_t manufacturer_name, uint16_t sub_version)>
484 on_read_remote_version) {
485 for (const auto event : LeConnectionManagementEvents) {
486 RegisterLeEventHandler(event, event_handler);
487 }
488 disconnect_handlers_.push_back(on_disconnect);
489 read_remote_version_handlers_.push_back(on_read_remote_version);
490 return &le_acl_connection_manager_interface_;
491 }
492
GetSecurityInterface(ContextualCallback<void (EventView)> event_handler)493 SecurityInterface* HciLayer::GetSecurityInterface(ContextualCallback<void(EventView)> event_handler) {
494 for (const auto event : SecurityEvents) {
495 RegisterEventHandler(event, event_handler);
496 }
497 return &security_interface;
498 }
499
GetLeSecurityInterface(ContextualCallback<void (LeMetaEventView)> event_handler)500 LeSecurityInterface* HciLayer::GetLeSecurityInterface(ContextualCallback<void(LeMetaEventView)> event_handler) {
501 for (const auto subevent : LeSecurityEvents) {
502 RegisterLeEventHandler(subevent, event_handler);
503 }
504 return &le_security_interface;
505 }
506
GetLeAdvertisingInterface(ContextualCallback<void (LeMetaEventView)> event_handler)507 LeAdvertisingInterface* HciLayer::GetLeAdvertisingInterface(ContextualCallback<void(LeMetaEventView)> event_handler) {
508 for (const auto subevent : LeAdvertisingEvents) {
509 RegisterLeEventHandler(subevent, event_handler);
510 }
511 return &le_advertising_interface;
512 }
513
GetLeScanningInterface(ContextualCallback<void (LeMetaEventView)> event_handler)514 LeScanningInterface* HciLayer::GetLeScanningInterface(ContextualCallback<void(LeMetaEventView)> event_handler) {
515 for (const auto subevent : LeScanningEvents) {
516 RegisterLeEventHandler(subevent, event_handler);
517 }
518 return &le_scanning_interface;
519 }
520
GetLeIsoInterface(ContextualCallback<void (LeMetaEventView)> event_handler)521 LeIsoInterface* HciLayer::GetLeIsoInterface(ContextualCallback<void(LeMetaEventView)> event_handler) {
522 for (const auto subevent : LeIsoEvents) {
523 RegisterLeEventHandler(subevent, event_handler);
524 }
525 return &le_iso_interface;
526 }
527
__anond361b4990102() 528 const ModuleFactory HciLayer::Factory = ModuleFactory([]() { return new HciLayer(); });
529
ListDependencies(ModuleList * list)530 void HciLayer::ListDependencies(ModuleList* list) {
531 list->add<hal::HciHal>();
532 list->add<storage::StorageModule>();
533 }
534
Start()535 void HciLayer::Start() {
536 auto hal = GetDependency<hal::HciHal>();
537 impl_ = new impl(hal, *this);
538 hal_callbacks_ = new hal_callbacks(*this);
539
540 Handler* handler = GetHandler();
541 impl_->acl_queue_.GetDownEnd()->RegisterDequeue(handler, BindOn(impl_, &impl::on_outbound_acl_ready));
542 impl_->iso_queue_.GetDownEnd()->RegisterDequeue(handler, BindOn(impl_, &impl::on_outbound_iso_ready));
543 RegisterEventHandler(EventCode::COMMAND_COMPLETE, handler->BindOn(impl_, &impl::on_command_complete));
544 RegisterEventHandler(EventCode::COMMAND_STATUS, handler->BindOn(impl_, &impl::on_command_status));
545 RegisterLeMetaEventHandler(handler->BindOn(impl_, &impl::on_le_meta_event));
546 if (bluetooth::common::init_flags::gd_acl_is_enabled() || bluetooth::common::init_flags::gd_l2cap_is_enabled()) {
547 RegisterEventHandler(
548 EventCode::DISCONNECTION_COMPLETE, handler->BindOn(this, &HciLayer::on_disconnection_complete));
549 RegisterEventHandler(
550 EventCode::READ_REMOTE_VERSION_INFORMATION_COMPLETE,
551 handler->BindOn(this, &HciLayer::on_read_remote_version_complete));
552 }
553 auto drop_packet = handler->BindOn(impl_, &impl::drop);
554 RegisterEventHandler(EventCode::PAGE_SCAN_REPETITION_MODE_CHANGE, drop_packet);
555 RegisterEventHandler(EventCode::MAX_SLOTS_CHANGE, drop_packet);
556
557 EnqueueCommand(ResetBuilder::Create(), handler->BindOnce(&fail_if_reset_complete_not_success));
558 hal->registerIncomingPacketCallback(hal_callbacks_);
559 }
560
Stop()561 void HciLayer::Stop() {
562 auto hal = GetDependency<hal::HciHal>();
563 hal->unregisterIncomingPacketCallback();
564 delete hal_callbacks_;
565
566 impl_->acl_queue_.GetDownEnd()->UnregisterDequeue();
567 impl_->iso_queue_.GetDownEnd()->UnregisterDequeue();
568 delete impl_;
569 }
570
571 } // namespace hci
572 } // namespace bluetooth
573