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 
17 #pragma once
18 
19 #include "common/bind.h"
20 #include "hci/acl_manager/assembler.h"
21 #include "hci/acl_manager/disconnector_for_le.h"
22 #include "hci/acl_manager/event_checkers.h"
23 #include "hci/acl_manager/round_robin_scheduler.h"
24 #include "hci/controller.h"
25 #include "security/security_manager_listener.h"
26 #include "security/security_module.h"
27 
28 namespace bluetooth {
29 namespace hci {
30 namespace acl_manager {
31 
32 struct acl_connection {
acl_connectionacl_connection33   acl_connection(AddressWithType address_with_type, AclConnection::QueueDownEnd* queue_down_end, os::Handler* handler)
34       : assembler_(address_with_type, queue_down_end, handler), address_with_type_(address_with_type) {}
35   ~acl_connection() = default;
36   struct acl_manager::assembler assembler_;
37   AddressWithType address_with_type_;
38   ConnectionManagementCallbacks* connection_management_callbacks_ = nullptr;
39 };
40 
41 struct classic_impl : public DisconnectorForLe, public security::ISecurityManagerListener {
classic_implclassic_impl42   classic_impl(HciLayer* hci_layer, Controller* controller, os::Handler* handler,
43                RoundRobinScheduler* round_robin_scheduler)
44       : hci_layer_(hci_layer), controller_(controller), round_robin_scheduler_(round_robin_scheduler) {
45     hci_layer_ = hci_layer;
46     controller_ = controller;
47     handler_ = handler;
48     should_accept_connection_ = common::Bind([](Address, ClassOfDevice) { return true; });
49     acl_connection_interface_ =
50         hci_layer_->GetAclConnectionInterface(handler_->BindOn(this, &classic_impl::on_classic_event),
51                                               handler_->BindOn(this, &classic_impl::on_classic_disconnect));
52   }
53 
~classic_implclassic_impl54   ~classic_impl() override {
55     for (auto event_code : AclConnectionEvents) {
56       hci_layer_->UnregisterEventHandler(event_code);
57     }
58     acl_connections_.clear();
59     security_manager_.reset();
60   }
61 
on_classic_eventclassic_impl62   void on_classic_event(EventPacketView event_packet) {
63     EventCode event_code = event_packet.GetEventCode();
64     switch (event_code) {
65       case EventCode::CONNECTION_COMPLETE:
66         on_connection_complete(event_packet);
67         break;
68       case EventCode::CONNECTION_REQUEST:
69         on_incoming_connection(event_packet);
70         break;
71       case EventCode::CONNECTION_PACKET_TYPE_CHANGED:
72         on_connection_packet_type_changed(event_packet);
73         break;
74       case EventCode::AUTHENTICATION_COMPLETE:
75         on_authentication_complete(event_packet);
76         break;
77       case EventCode::READ_CLOCK_OFFSET_COMPLETE:
78         on_read_clock_offset_complete(event_packet);
79         break;
80       case EventCode::MODE_CHANGE:
81         on_mode_change(event_packet);
82         break;
83       case EventCode::QOS_SETUP_COMPLETE:
84         on_qos_setup_complete(event_packet);
85         break;
86       case EventCode::ROLE_CHANGE:
87         on_role_change(event_packet);
88         break;
89       case EventCode::FLOW_SPECIFICATION_COMPLETE:
90         on_flow_specification_complete(event_packet);
91         break;
92       case EventCode::FLUSH_OCCURRED:
93         on_flush_occurred(event_packet);
94         break;
95       case EventCode::READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
96         on_read_remote_supported_features_complete(event_packet);
97         break;
98       case EventCode::READ_REMOTE_EXTENDED_FEATURES_COMPLETE:
99         on_read_remote_extended_features_complete(event_packet);
100         break;
101       case EventCode::READ_REMOTE_VERSION_INFORMATION_COMPLETE:
102         on_read_remote_version_information_complete(event_packet);
103         break;
104       case EventCode::LINK_SUPERVISION_TIMEOUT_CHANGED:
105         on_link_supervision_timeout_changed(event_packet);
106         break;
107       default:
108         LOG_ALWAYS_FATAL("Unhandled event code %s", EventCodeText(event_code).c_str());
109     }
110   }
111 
on_classic_disconnectclassic_impl112   void on_classic_disconnect(uint16_t handle, ErrorCode reason) {
113     if (acl_connections_.count(handle) == 1) {
114       auto& connection = acl_connections_.find(handle)->second;
115       round_robin_scheduler_->Unregister(handle);
116       connection.connection_management_callbacks_->OnDisconnection(reason);
117       acl_connections_.erase(handle);
118     }
119   }
120 
handle_register_callbacksclassic_impl121   void handle_register_callbacks(ConnectionCallbacks* callbacks, os::Handler* handler) {
122     ASSERT(client_callbacks_ == nullptr);
123     ASSERT(client_handler_ == nullptr);
124     client_callbacks_ = callbacks;
125     client_handler_ = handler;
126   }
127 
handle_disconnectclassic_impl128   void handle_disconnect(uint16_t handle, DisconnectReason reason) {
129     acl_connection_interface_->EnqueueCommand(hci::DisconnectBuilder::Create(handle, reason),
130                                               handler_->BindOnce(&check_command_status<DisconnectStatusView>));
131   }
132 
on_incoming_connectionclassic_impl133   void on_incoming_connection(EventPacketView packet) {
134     ConnectionRequestView request = ConnectionRequestView::Create(packet);
135     ASSERT(request.IsValid());
136     Address address = request.GetBdAddr();
137     if (client_callbacks_ == nullptr) {
138       LOG_ERROR("No callbacks to call");
139       auto reason = RejectConnectionReason::LIMITED_RESOURCES;
140       this->reject_connection(RejectConnectionRequestBuilder::Create(address, reason));
141       return;
142     }
143     incoming_connecting_address_ = address;
144     if (is_classic_link_already_connected(address)) {
145       auto reason = RejectConnectionReason::UNACCEPTABLE_BD_ADDR;
146       this->reject_connection(RejectConnectionRequestBuilder::Create(address, reason));
147     } else if (should_accept_connection_.Run(address, request.GetClassOfDevice())) {
148       this->accept_connection(address);
149     } else {
150       auto reason = RejectConnectionReason::LIMITED_RESOURCES;  // TODO: determine reason
151       this->reject_connection(RejectConnectionRequestBuilder::Create(address, reason));
152     }
153   }
154 
is_classic_link_already_connectedclassic_impl155   bool is_classic_link_already_connected(Address address) {
156     for (const auto& connection : acl_connections_) {
157       if (connection.second.address_with_type_.GetAddress() == address) {
158         return true;
159       }
160     }
161     return false;
162   }
163 
create_connectionclassic_impl164   void create_connection(Address address) {
165     // TODO: Configure default connection parameters?
166     uint16_t packet_type = 0x4408 /* DM 1,3,5 */ | 0x8810 /*DH 1,3,5 */;
167     PageScanRepetitionMode page_scan_repetition_mode = PageScanRepetitionMode::R1;
168     uint16_t clock_offset = 0;
169     ClockOffsetValid clock_offset_valid = ClockOffsetValid::INVALID;
170     CreateConnectionRoleSwitch allow_role_switch = CreateConnectionRoleSwitch::ALLOW_ROLE_SWITCH;
171     ASSERT(client_callbacks_ != nullptr);
172     std::unique_ptr<CreateConnectionBuilder> packet = CreateConnectionBuilder::Create(
173         address, packet_type, page_scan_repetition_mode, clock_offset, clock_offset_valid, allow_role_switch);
174 
175     if (incoming_connecting_address_ == Address::kEmpty && outgoing_connecting_address_ == Address::kEmpty) {
176       if (is_classic_link_already_connected(address)) {
177         LOG_WARN("already connected: %s", address.ToString().c_str());
178         return;
179       }
180       outgoing_connecting_address_ = address;
181       acl_connection_interface_->EnqueueCommand(std::move(packet), handler_->BindOnce([](CommandStatusView status) {
182         ASSERT(status.IsValid());
183         ASSERT(status.GetCommandOpCode() == OpCode::CREATE_CONNECTION);
184       }));
185     } else {
186       pending_outgoing_connections_.emplace(address, std::move(packet));
187     }
188   }
189 
on_connection_completeclassic_impl190   void on_connection_complete(EventPacketView packet) {
191     ConnectionCompleteView connection_complete = ConnectionCompleteView::Create(packet);
192     ASSERT(connection_complete.IsValid());
193     auto status = connection_complete.GetStatus();
194     auto address = connection_complete.GetBdAddr();
195     Role current_role = Role::MASTER;
196     if (outgoing_connecting_address_ == address) {
197       outgoing_connecting_address_ = Address::kEmpty;
198     } else {
199       ASSERT_LOG(incoming_connecting_address_ == address, "No prior connection request for %s",
200                  address.ToString().c_str());
201       incoming_connecting_address_ = Address::kEmpty;
202       current_role = Role::SLAVE;
203     }
204     if (status != ErrorCode::SUCCESS) {
205       client_handler_->Post(common::BindOnce(&ConnectionCallbacks::OnConnectFail, common::Unretained(client_callbacks_),
206                                              address, status));
207       return;
208     }
209     uint16_t handle = connection_complete.GetConnectionHandle();
210     ASSERT(acl_connections_.count(handle) == 0);
211     auto queue = std::make_shared<AclConnection::Queue>(10);
212     acl_connections_.emplace(std::piecewise_construct, std::forward_as_tuple(handle),
213                              std::forward_as_tuple(AddressWithType{address, AddressType::PUBLIC_DEVICE_ADDRESS},
214                                                    queue->GetDownEnd(), handler_));
215     round_robin_scheduler_->Register(RoundRobinScheduler::ConnectionType::CLASSIC, handle, queue);
216     std::unique_ptr<ClassicAclConnection> connection(
217         new ClassicAclConnection(std::move(queue), acl_connection_interface_, handle, address));
218     auto& connection_proxy = check_and_get_connection(handle);
219     connection_proxy.connection_management_callbacks_ = connection->GetEventCallbacks();
220     connection_proxy.connection_management_callbacks_->OnRoleChange(current_role);
221     client_handler_->Post(common::BindOnce(&ConnectionCallbacks::OnConnectSuccess,
222                                            common::Unretained(client_callbacks_), std::move(connection)));
223     while (!pending_outgoing_connections_.empty()) {
224       auto create_connection_packet_and_address = std::move(pending_outgoing_connections_.front());
225       pending_outgoing_connections_.pop();
226       if (!is_classic_link_already_connected(create_connection_packet_and_address.first)) {
227         outgoing_connecting_address_ = create_connection_packet_and_address.first;
228         acl_connection_interface_->EnqueueCommand(std::move(create_connection_packet_and_address.second),
229                                                   handler_->BindOnce([](CommandStatusView status) {
230                                                     ASSERT(status.IsValid());
231                                                     ASSERT(status.GetCommandOpCode() == OpCode::CREATE_CONNECTION);
232                                                   }));
233         break;
234       }
235     }
236   }
237 
on_connection_packet_type_changedclassic_impl238   void on_connection_packet_type_changed(EventPacketView packet) {
239     ConnectionPacketTypeChangedView packet_type_changed = ConnectionPacketTypeChangedView::Create(packet);
240     if (!packet_type_changed.IsValid()) {
241       LOG_ERROR("Received on_connection_packet_type_changed with invalid packet");
242       return;
243     } else if (packet_type_changed.GetStatus() != ErrorCode::SUCCESS) {
244       auto status = packet_type_changed.GetStatus();
245       std::string error_code = ErrorCodeText(status);
246       LOG_ERROR("Received on_connection_packet_type_changed with error code %s", error_code.c_str());
247       return;
248     }
249     uint16_t handle = packet_type_changed.GetConnectionHandle();
250     auto& acl_connection = acl_connections_.find(handle)->second;
251     uint16_t packet_type = packet_type_changed.GetPacketType();
252     acl_connection.connection_management_callbacks_->OnConnectionPacketTypeChanged(packet_type);
253   }
254 
on_master_link_key_completeclassic_impl255   void on_master_link_key_complete(EventPacketView packet) {
256     MasterLinkKeyCompleteView complete_view = MasterLinkKeyCompleteView::Create(packet);
257     if (!complete_view.IsValid()) {
258       LOG_ERROR("Received on_master_link_key_complete with invalid packet");
259       return;
260     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
261       auto status = complete_view.GetStatus();
262       std::string error_code = ErrorCodeText(status);
263       LOG_ERROR("Received on_master_link_key_complete with error code %s", error_code.c_str());
264       return;
265     }
266     uint16_t handle = complete_view.GetConnectionHandle();
267     auto& acl_connection = acl_connections_.find(handle)->second;
268     KeyFlag key_flag = complete_view.GetKeyFlag();
269     acl_connection.connection_management_callbacks_->OnMasterLinkKeyComplete(key_flag);
270   }
271 
on_authentication_completeclassic_impl272   void on_authentication_complete(EventPacketView packet) {
273     AuthenticationCompleteView authentication_complete = AuthenticationCompleteView::Create(packet);
274     if (!authentication_complete.IsValid()) {
275       LOG_ERROR("Received on_authentication_complete with invalid packet");
276       return;
277     } else if (authentication_complete.GetStatus() != ErrorCode::SUCCESS) {
278       auto status = authentication_complete.GetStatus();
279       std::string error_code = ErrorCodeText(status);
280       LOG_ERROR("Received on_authentication_complete with error code %s", error_code.c_str());
281       return;
282     }
283     uint16_t handle = authentication_complete.GetConnectionHandle();
284     auto& acl_connection = acl_connections_.find(handle)->second;
285     acl_connection.connection_management_callbacks_->OnAuthenticationComplete();
286   }
287 
cancel_connectclassic_impl288   void cancel_connect(Address address) {
289     if (outgoing_connecting_address_ == address) {
290       LOG_INFO("Cannot cancel non-existent connection to %s", address.ToString().c_str());
291       return;
292     }
293     std::unique_ptr<CreateConnectionCancelBuilder> packet = CreateConnectionCancelBuilder::Create(address);
294     acl_connection_interface_->EnqueueCommand(
295         std::move(packet), handler_->BindOnce(&check_command_complete<CreateConnectionCancelCompleteView>));
296   }
297 
master_link_keyclassic_impl298   void master_link_key(KeyFlag key_flag) {
299     std::unique_ptr<MasterLinkKeyBuilder> packet = MasterLinkKeyBuilder::Create(key_flag);
300     acl_connection_interface_->EnqueueCommand(std::move(packet),
301                                               handler_->BindOnce(&check_command_status<MasterLinkKeyStatusView>));
302   }
303 
switch_roleclassic_impl304   void switch_role(Address address, Role role) {
305     std::unique_ptr<SwitchRoleBuilder> packet = SwitchRoleBuilder::Create(address, role);
306     acl_connection_interface_->EnqueueCommand(std::move(packet),
307                                               handler_->BindOnce(&check_command_status<SwitchRoleStatusView>));
308   }
309 
write_default_link_policy_settingsclassic_impl310   void write_default_link_policy_settings(uint16_t default_link_policy_settings) {
311     std::unique_ptr<WriteDefaultLinkPolicySettingsBuilder> packet =
312         WriteDefaultLinkPolicySettingsBuilder::Create(default_link_policy_settings);
313     acl_connection_interface_->EnqueueCommand(
314         std::move(packet), handler_->BindOnce(&check_command_complete<WriteDefaultLinkPolicySettingsCompleteView>));
315   }
316 
accept_connectionclassic_impl317   void accept_connection(Address address) {
318     auto role = AcceptConnectionRequestRole::BECOME_MASTER;  // We prefer to be master
319     acl_connection_interface_->EnqueueCommand(
320         AcceptConnectionRequestBuilder::Create(address, role),
321         handler_->BindOnceOn(this, &classic_impl::on_accept_connection_status, address));
322   }
323 
on_change_connection_link_key_completeclassic_impl324   void on_change_connection_link_key_complete(EventPacketView packet) {
325     ChangeConnectionLinkKeyCompleteView complete_view = ChangeConnectionLinkKeyCompleteView::Create(packet);
326     if (!complete_view.IsValid()) {
327       LOG_ERROR("Received on_change_connection_link_key_complete with invalid packet");
328       return;
329     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
330       auto status = complete_view.GetStatus();
331       std::string error_code = ErrorCodeText(status);
332       LOG_ERROR("Received on_change_connection_link_key_complete with error code %s", error_code.c_str());
333       return;
334     }
335     uint16_t handle = complete_view.GetConnectionHandle();
336     auto& acl_connection = acl_connections_.find(handle)->second;
337     acl_connection.connection_management_callbacks_->OnChangeConnectionLinkKeyComplete();
338   }
339 
on_read_clock_offset_completeclassic_impl340   void on_read_clock_offset_complete(EventPacketView packet) {
341     ReadClockOffsetCompleteView complete_view = ReadClockOffsetCompleteView::Create(packet);
342     if (!complete_view.IsValid()) {
343       LOG_ERROR("Received on_read_clock_offset_complete with invalid packet");
344       return;
345     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
346       auto status = complete_view.GetStatus();
347       std::string error_code = ErrorCodeText(status);
348       LOG_ERROR("Received on_read_clock_offset_complete with error code %s", error_code.c_str());
349       return;
350     }
351     uint16_t handle = complete_view.GetConnectionHandle();
352     auto& acl_connection = acl_connections_.find(handle)->second;
353     uint16_t clock_offset = complete_view.GetClockOffset();
354     acl_connection.connection_management_callbacks_->OnReadClockOffsetComplete(clock_offset);
355   }
356 
on_mode_changeclassic_impl357   void on_mode_change(EventPacketView packet) {
358     ModeChangeView mode_change_view = ModeChangeView::Create(packet);
359     if (!mode_change_view.IsValid()) {
360       LOG_ERROR("Received on_mode_change with invalid packet");
361       return;
362     } else if (mode_change_view.GetStatus() != ErrorCode::SUCCESS) {
363       auto status = mode_change_view.GetStatus();
364       std::string error_code = ErrorCodeText(status);
365       LOG_ERROR("Received on_mode_change with error code %s", error_code.c_str());
366       return;
367     }
368     uint16_t handle = mode_change_view.GetConnectionHandle();
369     auto& acl_connection = acl_connections_.find(handle)->second;
370     Mode current_mode = mode_change_view.GetCurrentMode();
371     uint16_t interval = mode_change_view.GetInterval();
372     acl_connection.connection_management_callbacks_->OnModeChange(current_mode, interval);
373   }
374 
on_qos_setup_completeclassic_impl375   void on_qos_setup_complete(EventPacketView packet) {
376     QosSetupCompleteView complete_view = QosSetupCompleteView::Create(packet);
377     if (!complete_view.IsValid()) {
378       LOG_ERROR("Received on_qos_setup_complete with invalid packet");
379       return;
380     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
381       auto status = complete_view.GetStatus();
382       std::string error_code = ErrorCodeText(status);
383       LOG_ERROR("Received on_qos_setup_complete with error code %s", error_code.c_str());
384       return;
385     }
386     uint16_t handle = complete_view.GetConnectionHandle();
387     auto& acl_connection = acl_connections_.find(handle)->second;
388     ServiceType service_type = complete_view.GetServiceType();
389     uint32_t token_rate = complete_view.GetTokenRate();
390     uint32_t peak_bandwidth = complete_view.GetPeakBandwidth();
391     uint32_t latency = complete_view.GetLatency();
392     uint32_t delay_variation = complete_view.GetDelayVariation();
393     acl_connection.connection_management_callbacks_->OnQosSetupComplete(service_type, token_rate, peak_bandwidth,
394                                                                         latency, delay_variation);
395   }
396 
on_role_changeclassic_impl397   void on_role_change(EventPacketView packet) {
398     RoleChangeView role_change_view = RoleChangeView::Create(packet);
399     if (!role_change_view.IsValid()) {
400       LOG_ERROR("Received on_role_change with invalid packet");
401       return;
402     } else if (role_change_view.GetStatus() != ErrorCode::SUCCESS) {
403       auto status = role_change_view.GetStatus();
404       std::string error_code = ErrorCodeText(status);
405       LOG_ERROR("Received on_role_change with error code %s", error_code.c_str());
406       return;
407     }
408     Address bd_addr = role_change_view.GetBdAddr();
409     Role new_role = role_change_view.GetNewRole();
410     for (auto& connection_pair : acl_connections_) {
411       if (connection_pair.second.address_with_type_.GetAddress() == bd_addr) {
412         connection_pair.second.connection_management_callbacks_->OnRoleChange(new_role);
413       }
414     }
415   }
416 
on_flow_specification_completeclassic_impl417   void on_flow_specification_complete(EventPacketView packet) {
418     FlowSpecificationCompleteView complete_view = FlowSpecificationCompleteView::Create(packet);
419     if (!complete_view.IsValid()) {
420       LOG_ERROR("Received on_flow_specification_complete with invalid packet");
421       return;
422     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
423       auto status = complete_view.GetStatus();
424       std::string error_code = ErrorCodeText(status);
425       LOG_ERROR("Received on_flow_specification_complete with error code %s", error_code.c_str());
426       return;
427     }
428     uint16_t handle = complete_view.GetConnectionHandle();
429     auto& acl_connection = acl_connections_.find(handle)->second;
430     FlowDirection flow_direction = complete_view.GetFlowDirection();
431     ServiceType service_type = complete_view.GetServiceType();
432     uint32_t token_rate = complete_view.GetTokenRate();
433     uint32_t token_bucket_size = complete_view.GetTokenBucketSize();
434     uint32_t peak_bandwidth = complete_view.GetPeakBandwidth();
435     uint32_t access_latency = complete_view.GetAccessLatency();
436     acl_connection.connection_management_callbacks_->OnFlowSpecificationComplete(
437         flow_direction, service_type, token_rate, token_bucket_size, peak_bandwidth, access_latency);
438   }
439 
on_flush_occurredclassic_impl440   void on_flush_occurred(EventPacketView packet) {
441     FlushOccurredView flush_occurred_view = FlushOccurredView::Create(packet);
442     if (!flush_occurred_view.IsValid()) {
443       LOG_ERROR("Received on_flush_occurred with invalid packet");
444       return;
445     }
446     uint16_t handle = flush_occurred_view.GetConnectionHandle();
447     auto& acl_connection = acl_connections_.find(handle)->second;
448     acl_connection.connection_management_callbacks_->OnFlushOccurred();
449   }
450 
on_read_remote_version_information_completeclassic_impl451   void on_read_remote_version_information_complete(EventPacketView packet) {
452     auto view = ReadRemoteVersionInformationCompleteView::Create(packet);
453     ASSERT_LOG(view.IsValid(), "Read remote version information packet invalid");
454     LOG_INFO("UNIMPLEMENTED called");
455   }
456 
on_read_remote_supported_features_completeclassic_impl457   void on_read_remote_supported_features_complete(EventPacketView packet) {
458     auto view = ReadRemoteSupportedFeaturesCompleteView::Create(packet);
459     ASSERT_LOG(view.IsValid(), "Read remote supported features packet invalid");
460     LOG_INFO("UNIMPLEMENTED called");
461   }
462 
on_read_remote_extended_features_completeclassic_impl463   void on_read_remote_extended_features_complete(EventPacketView packet) {
464     auto view = ReadRemoteExtendedFeaturesCompleteView::Create(packet);
465     ASSERT_LOG(view.IsValid(), "Read remote extended features packet invalid");
466     LOG_INFO("UNIMPLEMENTED called");
467   }
468 
on_link_supervision_timeout_changedclassic_impl469   void on_link_supervision_timeout_changed(EventPacketView packet) {
470     auto view = LinkSupervisionTimeoutChangedView::Create(packet);
471     ASSERT_LOG(view.IsValid(), "Link supervision timeout changed packet invalid");
472     LOG_INFO("UNIMPLEMENTED called");
473   }
474 
on_accept_connection_statusclassic_impl475   void on_accept_connection_status(Address address, CommandStatusView status) {
476     auto accept_status = AcceptConnectionRequestStatusView::Create(status);
477     ASSERT(accept_status.IsValid());
478     if (status.GetStatus() != ErrorCode::SUCCESS) {
479       cancel_connect(address);
480     }
481   }
482 
reject_connectionclassic_impl483   void reject_connection(std::unique_ptr<RejectConnectionRequestBuilder> builder) {
484     acl_connection_interface_->EnqueueCommand(
485         std::move(builder), handler_->BindOnce(&check_command_status<RejectConnectionRequestStatusView>));
486   }
487 
check_and_get_connectionclassic_impl488   acl_connection& check_and_get_connection(uint16_t handle) {
489     auto connection = acl_connections_.find(handle);
490     ASSERT(connection != acl_connections_.end());
491     return connection->second;
492   }
493 
OnDeviceBondedclassic_impl494   void OnDeviceBonded(bluetooth::hci::AddressWithType device) override {}
OnDeviceUnbondedclassic_impl495   void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) override {}
OnDeviceBondFailedclassic_impl496   void OnDeviceBondFailed(bluetooth::hci::AddressWithType device) override {}
497 
OnEncryptionStateChangedclassic_impl498   void OnEncryptionStateChanged(EncryptionChangeView encryption_change_view) override {
499     if (!encryption_change_view.IsValid()) {
500       LOG_ERROR("Invalid packet");
501       return;
502     } else if (encryption_change_view.GetStatus() != ErrorCode::SUCCESS) {
503       auto status = encryption_change_view.GetStatus();
504       std::string error_code = ErrorCodeText(status);
505       LOG_ERROR("error_code %s", error_code.c_str());
506       return;
507     }
508     uint16_t handle = encryption_change_view.GetConnectionHandle();
509     auto acl_connection = acl_connections_.find(handle);
510     if (acl_connection == acl_connections_.end()) {
511       LOG_INFO("Invalid handle (already closed?) %d", handle);
512       return;
513     }
514     EncryptionEnabled enabled = encryption_change_view.GetEncryptionEnabled();
515     acl_connection->second.connection_management_callbacks_->OnEncryptionChange(enabled);
516   }
517 
set_security_moduleclassic_impl518   void set_security_module(security::SecurityModule* security_module) {
519     security_manager_ = security_module->GetSecurityManager();
520     security_manager_->RegisterCallbackListener(this, handler_);
521   }
522 
HACK_get_handleclassic_impl523   uint16_t HACK_get_handle(Address address) {
524     for (auto it = acl_connections_.begin(); it != acl_connections_.end(); it++) {
525       if (it->second.address_with_type_.GetAddress() == address) {
526         return it->first;
527       }
528     }
529     return 0xFFFF;
530   }
531 
532   HciLayer* hci_layer_ = nullptr;
533   Controller* controller_ = nullptr;
534   RoundRobinScheduler* round_robin_scheduler_ = nullptr;
535   AclConnectionInterface* acl_connection_interface_ = nullptr;
536   os::Handler* handler_ = nullptr;
537   ConnectionCallbacks* client_callbacks_ = nullptr;
538   os::Handler* client_handler_ = nullptr;
539   std::map<uint16_t, acl_connection> acl_connections_;
540   Address outgoing_connecting_address_{Address::kEmpty};
541   Address incoming_connecting_address_{Address::kEmpty};
542   common::Callback<bool(Address, ClassOfDevice)> should_accept_connection_;
543   std::queue<std::pair<Address, std::unique_ptr<CreateConnectionBuilder>>> pending_outgoing_connections_;
544 
545   std::unique_ptr<security::SecurityManager> security_manager_;
546 };
547 
548 }  // namespace acl_manager
549 }  // namespace hci
550 }  // namespace bluetooth
551