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 #include "hci/acl_manager/classic_acl_connection.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include "hci/address.h"
22 #include "hci/event_checkers.h"
23 #include "os/metrics.h"
24 
25 using bluetooth::hci::Address;
26 
27 namespace bluetooth {
28 namespace hci {
29 namespace acl_manager {
30 
31 class AclConnectionTracker : public ConnectionManagementCallbacks {
32  public:
AclConnectionTracker(AclConnectionInterface * acl_connection_interface,const Address & address,uint16_t connection_handle)33   AclConnectionTracker(
34       AclConnectionInterface* acl_connection_interface, const Address& address, uint16_t connection_handle)
35       : acl_connection_interface_(acl_connection_interface), address_(address), connection_handle_(connection_handle) {}
~AclConnectionTracker()36   ~AclConnectionTracker() {
37     // If callbacks were registered, they should have been delivered.
38     log::assert_that(
39         client_callbacks_ == nullptr || queued_callbacks_.empty(),
40         "assert failed: client_callbacks_ == nullptr || queued_callbacks_.empty()");
41   }
RegisterCallbacks(ConnectionManagementCallbacks * callbacks,os::Handler * handler)42   void RegisterCallbacks(ConnectionManagementCallbacks* callbacks, os::Handler* handler) {
43     client_handler_ = handler;
44     client_callbacks_ = callbacks;
45     while (!queued_callbacks_.empty()) {
46       auto iter = queued_callbacks_.begin();
47       handler->Post(std::move(*iter));
48       queued_callbacks_.erase(iter);
49     }
50   }
51 
52 #define SAVE_OR_CALL(f, ...)                                                                                        \
53   if (client_handler_ == nullptr) {                                                                                 \
54     queued_callbacks_.emplace_back(                                                                                 \
55         common::BindOnce(&ConnectionManagementCallbacks::f, common::Unretained(this), ##__VA_ARGS__));              \
56   } else {                                                                                                          \
57     client_handler_->Post(                                                                                          \
58         common::BindOnce(&ConnectionManagementCallbacks::f, common::Unretained(client_callbacks_), ##__VA_ARGS__)); \
59   }
60 
OnConnectionPacketTypeChanged(uint16_t packet_type)61   void OnConnectionPacketTypeChanged(uint16_t packet_type) override {
62     SAVE_OR_CALL(OnConnectionPacketTypeChanged, packet_type)
63   }
OnAuthenticationComplete(hci::ErrorCode hci_status)64   void OnAuthenticationComplete(hci::ErrorCode hci_status) override {
65     SAVE_OR_CALL(OnAuthenticationComplete, hci_status)
66   }
OnEncryptionChange(EncryptionEnabled enabled)67   void OnEncryptionChange(EncryptionEnabled enabled) override {
68     SAVE_OR_CALL(OnEncryptionChange, enabled)
69   }
OnChangeConnectionLinkKeyComplete()70   void OnChangeConnectionLinkKeyComplete() override {
71     SAVE_OR_CALL(OnChangeConnectionLinkKeyComplete)
72   }
OnReadClockOffsetComplete(uint16_t clock_offset)73   void OnReadClockOffsetComplete(uint16_t clock_offset) override {
74     SAVE_OR_CALL(OnReadClockOffsetComplete, clock_offset)
75   }
OnModeChange(ErrorCode status,Mode current_mode,uint16_t interval)76   void OnModeChange(ErrorCode status, Mode current_mode, uint16_t interval) override {
77     SAVE_OR_CALL(OnModeChange, status, current_mode, interval)
78   }
OnSniffSubrating(hci::ErrorCode hci_status,uint16_t maximum_transmit_latency,uint16_t maximum_receive_latency,uint16_t minimum_remote_timeout,uint16_t minimum_local_timeout)79   void OnSniffSubrating(
80       hci::ErrorCode hci_status,
81       uint16_t maximum_transmit_latency,
82       uint16_t maximum_receive_latency,
83       uint16_t minimum_remote_timeout,
84       uint16_t minimum_local_timeout) override {
85     SAVE_OR_CALL(
86         OnSniffSubrating,
87         hci_status,
88         maximum_transmit_latency,
89         maximum_receive_latency,
90         minimum_remote_timeout,
91         minimum_local_timeout);
92   }
OnQosSetupComplete(ServiceType service_type,uint32_t token_rate,uint32_t peak_bandwidth,uint32_t latency,uint32_t delay_variation)93   void OnQosSetupComplete(ServiceType service_type, uint32_t token_rate, uint32_t peak_bandwidth, uint32_t latency,
94                           uint32_t delay_variation) override {
95     SAVE_OR_CALL(OnQosSetupComplete, service_type, token_rate, peak_bandwidth, latency, delay_variation)
96   }
OnFlowSpecificationComplete(FlowDirection flow_direction,ServiceType service_type,uint32_t token_rate,uint32_t token_bucket_size,uint32_t peak_bandwidth,uint32_t access_latency)97   void OnFlowSpecificationComplete(FlowDirection flow_direction, ServiceType service_type, uint32_t token_rate,
98                                    uint32_t token_bucket_size, uint32_t peak_bandwidth,
99                                    uint32_t access_latency) override {
100     SAVE_OR_CALL(OnFlowSpecificationComplete, flow_direction, service_type, token_rate, token_bucket_size,
101                  peak_bandwidth, access_latency)
102   }
OnFlushOccurred()103   void OnFlushOccurred() override {
104     SAVE_OR_CALL(OnFlushOccurred)
105   }
OnRoleDiscoveryComplete(Role current_role)106   void OnRoleDiscoveryComplete(Role current_role) override {
107     SAVE_OR_CALL(OnRoleDiscoveryComplete, current_role)
108   }
OnReadLinkPolicySettingsComplete(uint16_t link_policy_settings)109   void OnReadLinkPolicySettingsComplete(uint16_t link_policy_settings) override {
110     SAVE_OR_CALL(OnReadLinkPolicySettingsComplete, link_policy_settings)
111   }
OnReadAutomaticFlushTimeoutComplete(uint16_t flush_timeout)112   void OnReadAutomaticFlushTimeoutComplete(uint16_t flush_timeout) override {
113     SAVE_OR_CALL(OnReadAutomaticFlushTimeoutComplete, flush_timeout)
114   }
OnReadTransmitPowerLevelComplete(uint8_t transmit_power_level)115   void OnReadTransmitPowerLevelComplete(uint8_t transmit_power_level) override {
116     bluetooth::os::LogMetricReadTxPowerLevelResult(
117         address_, connection_handle_, static_cast<uint8_t>(ErrorCode::SUCCESS), transmit_power_level);
118     SAVE_OR_CALL(OnReadTransmitPowerLevelComplete, transmit_power_level)
119   }
OnReadLinkSupervisionTimeoutComplete(uint16_t link_supervision_timeout)120   void OnReadLinkSupervisionTimeoutComplete(uint16_t link_supervision_timeout) override {
121     SAVE_OR_CALL(OnReadLinkSupervisionTimeoutComplete, link_supervision_timeout)
122   }
OnReadFailedContactCounterComplete(uint16_t failed_contact_counter)123   void OnReadFailedContactCounterComplete(uint16_t failed_contact_counter) override {
124     bluetooth::os::LogMetricReadFailedContactCounterResult(
125         address_, connection_handle_, static_cast<uint8_t>(ErrorCode::SUCCESS), failed_contact_counter);
126     SAVE_OR_CALL(OnReadFailedContactCounterComplete, failed_contact_counter);
127   }
OnReadLinkQualityComplete(uint8_t link_quality)128   void OnReadLinkQualityComplete(uint8_t link_quality) override {
129     SAVE_OR_CALL(OnReadLinkQualityComplete, link_quality)
130   }
OnReadAfhChannelMapComplete(AfhMode afh_mode,std::array<uint8_t,10> afh_channel_map)131   void OnReadAfhChannelMapComplete(AfhMode afh_mode, std::array<uint8_t, 10> afh_channel_map) override {
132     SAVE_OR_CALL(OnReadAfhChannelMapComplete, afh_mode, afh_channel_map)
133   }
OnReadRssiComplete(uint8_t rssi)134   void OnReadRssiComplete(uint8_t rssi) override {
135     bluetooth::os::LogMetricReadRssiResult(
136         address_, connection_handle_, static_cast<uint8_t>(ErrorCode::SUCCESS), rssi);
137     SAVE_OR_CALL(OnReadRssiComplete, rssi);
138   }
OnReadClockComplete(uint32_t clock,uint16_t accuracy)139   void OnReadClockComplete(uint32_t clock, uint16_t accuracy) override {
140     SAVE_OR_CALL(OnReadClockComplete, clock, accuracy)
141   }
OnCentralLinkKeyComplete(KeyFlag key_flag)142   void OnCentralLinkKeyComplete(KeyFlag key_flag) override {
143     SAVE_OR_CALL(OnCentralLinkKeyComplete, key_flag)
144   }
OnRoleChange(hci::ErrorCode hci_status,Role new_role)145   void OnRoleChange(hci::ErrorCode hci_status, Role new_role) override {
146     SAVE_OR_CALL(OnRoleChange, hci_status, new_role)
147   }
OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,uint8_t lmp_version,uint16_t manufacturer_name,uint16_t sub_version)148   void OnReadRemoteVersionInformationComplete(
149       hci::ErrorCode hci_status, uint8_t lmp_version, uint16_t manufacturer_name, uint16_t sub_version) override {
150     bluetooth::os::LogMetricRemoteVersionInfo(
151         connection_handle_, static_cast<uint8_t>(hci_status), lmp_version, manufacturer_name, sub_version);
152     SAVE_OR_CALL(OnReadRemoteVersionInformationComplete, hci_status, lmp_version, manufacturer_name, sub_version);
153   }
OnReadRemoteSupportedFeaturesComplete(uint64_t features)154   void OnReadRemoteSupportedFeaturesComplete(uint64_t features) override {
155     SAVE_OR_CALL(OnReadRemoteSupportedFeaturesComplete, features);
156   }
OnReadRemoteExtendedFeaturesComplete(uint8_t page_number,uint8_t max_page_number,uint64_t features)157   void OnReadRemoteExtendedFeaturesComplete(uint8_t page_number, uint8_t max_page_number, uint64_t features) override {
158     SAVE_OR_CALL(OnReadRemoteExtendedFeaturesComplete, page_number, max_page_number, features);
159   }
OnDisconnection(ErrorCode reason)160   void OnDisconnection(ErrorCode reason) {
161     SAVE_OR_CALL(OnDisconnection, reason);
162   }
163 
164 #undef SAVE_OR_CALL
165 
on_role_discovery_complete(CommandCompleteView view)166   void on_role_discovery_complete(CommandCompleteView view) {
167     auto complete_view = RoleDiscoveryCompleteView::Create(view);
168     if (!complete_view.IsValid()) {
169       log::error("Received on_role_discovery_complete with invalid packet");
170       return;
171     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
172       auto status = complete_view.GetStatus();
173       std::string error_code = ErrorCodeText(status);
174       log::error("Received on_role_discovery_complete with error code {}", error_code);
175       return;
176     }
177     OnRoleDiscoveryComplete(complete_view.GetCurrentRole());
178   }
179 
on_read_link_policy_settings_complete(CommandCompleteView view)180   void on_read_link_policy_settings_complete(CommandCompleteView view) {
181     auto complete_view = ReadLinkPolicySettingsCompleteView::Create(view);
182     if (!complete_view.IsValid()) {
183       log::error("Received on_read_link_policy_settings_complete with invalid packet");
184       return;
185     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
186       auto status = complete_view.GetStatus();
187       std::string error_code = ErrorCodeText(status);
188       log::error("Received on_read_link_policy_settings_complete with error code {}", error_code);
189       return;
190     }
191     OnReadLinkPolicySettingsComplete(complete_view.GetLinkPolicySettings());
192   }
193 
on_read_automatic_flush_timeout_complete(CommandCompleteView view)194   void on_read_automatic_flush_timeout_complete(CommandCompleteView view) {
195     auto complete_view = ReadAutomaticFlushTimeoutCompleteView::Create(view);
196     if (!complete_view.IsValid()) {
197       log::error("Received on_read_automatic_flush_timeout_complete with invalid packet");
198       return;
199     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
200       auto status = complete_view.GetStatus();
201       std::string error_code = ErrorCodeText(status);
202       log::error(
203           "Received on_read_automatic_flush_timeout_complete with error code {}", error_code);
204       return;
205     }
206     OnReadAutomaticFlushTimeoutComplete(complete_view.GetFlushTimeout());
207   }
208 
on_read_transmit_power_level_complete(CommandCompleteView view)209   void on_read_transmit_power_level_complete(CommandCompleteView view) {
210     auto complete_view = ReadTransmitPowerLevelCompleteView::Create(view);
211     if (!complete_view.IsValid()) {
212       log::error("Received on_read_transmit_power_level_complete with invalid packet");
213       return;
214     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
215       auto status = complete_view.GetStatus();
216       std::string error_code = ErrorCodeText(status);
217       log::error("Received on_read_transmit_power_level_complete with error code {}", error_code);
218       return;
219     }
220     OnReadTransmitPowerLevelComplete(complete_view.GetTransmitPowerLevel());
221   }
222 
on_read_link_supervision_timeout_complete(CommandCompleteView view)223   void on_read_link_supervision_timeout_complete(CommandCompleteView view) {
224     auto complete_view = ReadLinkSupervisionTimeoutCompleteView::Create(view);
225     if (!complete_view.IsValid()) {
226       log::error("Received on_read_link_supervision_timeout_complete with invalid packet");
227       return;
228     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
229       auto status = complete_view.GetStatus();
230       std::string error_code = ErrorCodeText(status);
231       log::error(
232           "Received on_read_link_supervision_timeout_complete with error code {}", error_code);
233       return;
234     }
235     OnReadLinkSupervisionTimeoutComplete(complete_view.GetLinkSupervisionTimeout());
236   }
237 
on_read_failed_contact_counter_complete(CommandCompleteView view)238   void on_read_failed_contact_counter_complete(CommandCompleteView view) {
239     auto complete_view = ReadFailedContactCounterCompleteView::Create(view);
240     if (!complete_view.IsValid()) {
241       log::error("Received on_read_failed_contact_counter_complete with invalid packet");
242       return;
243     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
244       auto status = complete_view.GetStatus();
245       std::string error_code = ErrorCodeText(status);
246       log::error("Received on_read_failed_contact_counter_complete with error code {}", error_code);
247       return;
248     }
249     OnReadFailedContactCounterComplete(complete_view.GetFailedContactCounter());
250   }
251 
on_read_link_quality_complete(CommandCompleteView view)252   void on_read_link_quality_complete(CommandCompleteView view) {
253     auto complete_view = ReadLinkQualityCompleteView::Create(view);
254     if (!complete_view.IsValid()) {
255       log::error("Received on_read_link_quality_complete with invalid packet");
256       return;
257     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
258       auto status = complete_view.GetStatus();
259       std::string error_code = ErrorCodeText(status);
260       log::error("Received on_read_link_quality_complete with error code {}", error_code);
261       return;
262     }
263     OnReadLinkQualityComplete(complete_view.GetLinkQuality());
264   }
265 
on_read_afh_channel_map_complete(CommandCompleteView view)266   void on_read_afh_channel_map_complete(CommandCompleteView view) {
267     auto complete_view = ReadAfhChannelMapCompleteView::Create(view);
268     if (!complete_view.IsValid()) {
269       log::error("Received on_read_afh_channel_map_complete with invalid packet");
270       return;
271     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
272       auto status = complete_view.GetStatus();
273       std::string error_code = ErrorCodeText(status);
274       log::error("Received on_read_afh_channel_map_complete with error code {}", error_code);
275       return;
276     }
277     OnReadAfhChannelMapComplete(complete_view.GetAfhMode(), complete_view.GetAfhChannelMap());
278   }
279 
on_read_rssi_complete(CommandCompleteView view)280   void on_read_rssi_complete(CommandCompleteView view) {
281     auto complete_view = ReadRssiCompleteView::Create(view);
282     if (!complete_view.IsValid()) {
283       log::error("Received on_read_rssi_complete with invalid packet");
284       return;
285     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
286       auto status = complete_view.GetStatus();
287       std::string error_code = ErrorCodeText(status);
288       log::error("Received on_read_rssi_complete with error code {}", error_code);
289       return;
290     }
291     OnReadRssiComplete(complete_view.GetRssi());
292   }
293 
on_read_remote_version_information_status(CommandStatusView view)294   void on_read_remote_version_information_status(CommandStatusView view) {
295     log::assert_that(view.IsValid(), "Bad status packet!");
296   }
297 
on_read_remote_supported_features_status(CommandStatusView view)298   void on_read_remote_supported_features_status(CommandStatusView view) {
299     log::assert_that(view.IsValid(), "Bad status packet!");
300   }
301 
on_read_remote_extended_features_status(CommandStatusView view)302   void on_read_remote_extended_features_status(CommandStatusView view) {
303     log::assert_that(view.IsValid(), "Bad status packet!");
304   }
305 
on_read_clock_complete(CommandCompleteView view)306   void on_read_clock_complete(CommandCompleteView view) {
307     auto complete_view = ReadClockCompleteView::Create(view);
308     if (!complete_view.IsValid()) {
309       log::error("Received on_read_clock_complete with invalid packet");
310       return;
311     } else if (complete_view.GetStatus() != ErrorCode::SUCCESS) {
312       auto status = complete_view.GetStatus();
313       std::string error_code = ErrorCodeText(status);
314       log::error("Received on_read_clock_complete with error code {}", error_code);
315       return;
316     }
317     uint32_t clock = complete_view.GetClock();
318     uint16_t accuracy = complete_view.GetAccuracy();
319     OnReadClockComplete(clock, accuracy);
320   }
321 
322   AclConnectionInterface* acl_connection_interface_;
323   os::Handler* client_handler_ = nullptr;
324   ConnectionManagementCallbacks* client_callbacks_ = nullptr;
325   std::list<common::OnceClosure> queued_callbacks_;
326   Address address_;
327   uint16_t connection_handle_;
328 };
329 
330 struct ClassicAclConnection::impl {
implbluetooth::hci::acl_manager::ClassicAclConnection::impl331   impl(
332       AclConnectionInterface* acl_connection_interface,
333       std::shared_ptr<Queue> queue,
334       const Address& address,
335       uint16_t connection_handle)
336       : tracker(acl_connection_interface, address, connection_handle), queue_(std::move(queue)) {}
GetEventCallbacksbluetooth::hci::acl_manager::ClassicAclConnection::impl337   ConnectionManagementCallbacks* GetEventCallbacks(std::function<void(uint16_t)> invalidate_callbacks) {
338     log::assert_that(
339         !invalidate_callbacks_, "Already returned event callbacks for this connection");
340     invalidate_callbacks_ = std::move(invalidate_callbacks);
341     return &tracker;
342   }
PutEventCallbacksbluetooth::hci::acl_manager::ClassicAclConnection::impl343   void PutEventCallbacks() {
344     if (invalidate_callbacks_) invalidate_callbacks_(tracker.connection_handle_);
345     invalidate_callbacks_ = {};
346   }
347 
348   AclConnectionTracker tracker;
349   std::shared_ptr<Queue> queue_;
350   std::function<void(uint16_t)> invalidate_callbacks_;
351 };
352 
ClassicAclConnection()353 ClassicAclConnection::ClassicAclConnection()
354     : AclConnection(), acl_connection_interface_(nullptr), address_(Address::kEmpty) {}
355 
ClassicAclConnection(std::shared_ptr<Queue> queue,AclConnectionInterface * acl_connection_interface,uint16_t handle,Address address)356 ClassicAclConnection::ClassicAclConnection(std::shared_ptr<Queue> queue,
357                                            AclConnectionInterface* acl_connection_interface, uint16_t handle,
358                                            Address address)
359     : AclConnection(queue->GetUpEnd(), handle), acl_connection_interface_(acl_connection_interface), address_(address) {
360   pimpl_ = new ClassicAclConnection::impl(acl_connection_interface, std::move(queue), address, handle);
361 }
362 
~ClassicAclConnection()363 ClassicAclConnection::~ClassicAclConnection() {
364   if (pimpl_) pimpl_->PutEventCallbacks();
365   delete pimpl_;
366 }
367 
GetEventCallbacks(std::function<void (uint16_t)> invalidate_callbacks)368 ConnectionManagementCallbacks* ClassicAclConnection::GetEventCallbacks(
369     std::function<void(uint16_t)> invalidate_callbacks) {
370   return pimpl_->GetEventCallbacks(std::move(invalidate_callbacks));
371 }
372 
RegisterCallbacks(ConnectionManagementCallbacks * callbacks,os::Handler * handler)373 void ClassicAclConnection::RegisterCallbacks(ConnectionManagementCallbacks* callbacks, os::Handler* handler) {
374   return pimpl_->tracker.RegisterCallbacks(callbacks, handler);
375 }
376 
Disconnect(DisconnectReason reason)377 bool ClassicAclConnection::Disconnect(DisconnectReason reason) {
378   acl_connection_interface_->EnqueueCommand(
379       DisconnectBuilder::Create(handle_, reason),
380       pimpl_->tracker.client_handler_->BindOnce(check_status<DisconnectStatusView>));
381   return true;
382 }
383 
ChangeConnectionPacketType(uint16_t packet_type)384 bool ClassicAclConnection::ChangeConnectionPacketType(uint16_t packet_type) {
385   acl_connection_interface_->EnqueueCommand(
386       ChangeConnectionPacketTypeBuilder::Create(handle_, packet_type),
387       pimpl_->tracker.client_handler_->BindOnce(
388           check_status<ChangeConnectionPacketTypeStatusView>));
389   return true;
390 }
391 
AuthenticationRequested()392 bool ClassicAclConnection::AuthenticationRequested() {
393   acl_connection_interface_->EnqueueCommand(
394       AuthenticationRequestedBuilder::Create(handle_),
395       pimpl_->tracker.client_handler_->BindOnce(check_status<AuthenticationRequestedStatusView>));
396   return true;
397 }
398 
SetConnectionEncryption(Enable enable)399 bool ClassicAclConnection::SetConnectionEncryption(Enable enable) {
400   acl_connection_interface_->EnqueueCommand(
401       SetConnectionEncryptionBuilder::Create(handle_, enable),
402       pimpl_->tracker.client_handler_->BindOnce(check_status<SetConnectionEncryptionStatusView>));
403   return true;
404 }
405 
ChangeConnectionLinkKey()406 bool ClassicAclConnection::ChangeConnectionLinkKey() {
407   acl_connection_interface_->EnqueueCommand(
408       ChangeConnectionLinkKeyBuilder::Create(handle_),
409       pimpl_->tracker.client_handler_->BindOnce(check_status<ChangeConnectionLinkKeyStatusView>));
410   return true;
411 }
412 
ReadClockOffset()413 bool ClassicAclConnection::ReadClockOffset() {
414   acl_connection_interface_->EnqueueCommand(
415       ReadClockOffsetBuilder::Create(handle_),
416       pimpl_->tracker.client_handler_->BindOnce(check_status<ReadClockOffsetStatusView>));
417   return true;
418 }
419 
HoldMode(uint16_t max_interval,uint16_t min_interval)420 bool ClassicAclConnection::HoldMode(uint16_t max_interval, uint16_t min_interval) {
421   acl_connection_interface_->EnqueueCommand(
422       HoldModeBuilder::Create(handle_, max_interval, min_interval),
423       pimpl_->tracker.client_handler_->BindOnce(check_status<HoldModeStatusView>));
424   return true;
425 }
426 
SniffMode(uint16_t max_interval,uint16_t min_interval,uint16_t attempt,uint16_t timeout)427 bool ClassicAclConnection::SniffMode(uint16_t max_interval, uint16_t min_interval, uint16_t attempt, uint16_t timeout) {
428   acl_connection_interface_->EnqueueCommand(
429       SniffModeBuilder::Create(handle_, max_interval, min_interval, attempt, timeout),
430       pimpl_->tracker.client_handler_->BindOnce(check_status<SniffModeStatusView>));
431   return true;
432 }
433 
ExitSniffMode()434 bool ClassicAclConnection::ExitSniffMode() {
435   acl_connection_interface_->EnqueueCommand(
436       ExitSniffModeBuilder::Create(handle_),
437       pimpl_->tracker.client_handler_->BindOnce(check_status<ExitSniffModeStatusView>));
438   return true;
439 }
440 
QosSetup(ServiceType service_type,uint32_t token_rate,uint32_t peak_bandwidth,uint32_t latency,uint32_t delay_variation)441 bool ClassicAclConnection::QosSetup(ServiceType service_type, uint32_t token_rate, uint32_t peak_bandwidth,
442                                     uint32_t latency, uint32_t delay_variation) {
443   acl_connection_interface_->EnqueueCommand(
444       QosSetupBuilder::Create(
445           handle_, service_type, token_rate, peak_bandwidth, latency, delay_variation),
446       pimpl_->tracker.client_handler_->BindOnce(check_status<QosSetupStatusView>));
447   return true;
448 }
449 
RoleDiscovery()450 bool ClassicAclConnection::RoleDiscovery() {
451   acl_connection_interface_->EnqueueCommand(
452       RoleDiscoveryBuilder::Create(handle_),
453       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker, &AclConnectionTracker::on_role_discovery_complete));
454   return true;
455 }
456 
ReadLinkPolicySettings()457 bool ClassicAclConnection::ReadLinkPolicySettings() {
458   acl_connection_interface_->EnqueueCommand(
459       ReadLinkPolicySettingsBuilder::Create(handle_),
460       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
461                                                   &AclConnectionTracker::on_read_link_policy_settings_complete));
462   return true;
463 }
464 
WriteLinkPolicySettings(uint16_t link_policy_settings)465 bool ClassicAclConnection::WriteLinkPolicySettings(uint16_t link_policy_settings) {
466   acl_connection_interface_->EnqueueCommand(
467       WriteLinkPolicySettingsBuilder::Create(handle_, link_policy_settings),
468       pimpl_->tracker.client_handler_->BindOnce(
469           check_complete<WriteLinkPolicySettingsCompleteView>));
470   return true;
471 }
472 
FlowSpecification(FlowDirection flow_direction,ServiceType service_type,uint32_t token_rate,uint32_t token_bucket_size,uint32_t peak_bandwidth,uint32_t access_latency)473 bool ClassicAclConnection::FlowSpecification(FlowDirection flow_direction, ServiceType service_type,
474                                              uint32_t token_rate, uint32_t token_bucket_size, uint32_t peak_bandwidth,
475                                              uint32_t access_latency) {
476   acl_connection_interface_->EnqueueCommand(
477       FlowSpecificationBuilder::Create(
478           handle_,
479           flow_direction,
480           service_type,
481           token_rate,
482           token_bucket_size,
483           peak_bandwidth,
484           access_latency),
485       pimpl_->tracker.client_handler_->BindOnce(check_status<FlowSpecificationStatusView>));
486   return true;
487 }
488 
SniffSubrating(uint16_t maximum_latency,uint16_t minimum_remote_timeout,uint16_t minimum_local_timeout)489 bool ClassicAclConnection::SniffSubrating(uint16_t maximum_latency, uint16_t minimum_remote_timeout,
490                                           uint16_t minimum_local_timeout) {
491   acl_connection_interface_->EnqueueCommand(
492       SniffSubratingBuilder::Create(
493           handle_, maximum_latency, minimum_remote_timeout, minimum_local_timeout),
494       pimpl_->tracker.client_handler_->BindOnce(check_complete<SniffSubratingCompleteView>));
495   return true;
496 }
497 
Flush()498 bool ClassicAclConnection::Flush() {
499   acl_connection_interface_->EnqueueCommand(
500       EnhancedFlushBuilder::Create(handle_),
501       pimpl_->tracker.client_handler_->BindOnce(check_status<EnhancedFlushStatusView>));
502   return true;
503 }
504 
ReadAutomaticFlushTimeout()505 bool ClassicAclConnection::ReadAutomaticFlushTimeout() {
506   acl_connection_interface_->EnqueueCommand(
507       ReadAutomaticFlushTimeoutBuilder::Create(handle_),
508       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
509                                                   &AclConnectionTracker::on_read_automatic_flush_timeout_complete));
510   return true;
511 }
512 
WriteAutomaticFlushTimeout(uint16_t flush_timeout)513 bool ClassicAclConnection::WriteAutomaticFlushTimeout(uint16_t flush_timeout) {
514   acl_connection_interface_->EnqueueCommand(
515       WriteAutomaticFlushTimeoutBuilder::Create(handle_, flush_timeout),
516       pimpl_->tracker.client_handler_->BindOnce(
517           check_complete<WriteAutomaticFlushTimeoutCompleteView>));
518   return true;
519 }
520 
ReadTransmitPowerLevel(TransmitPowerLevelType type)521 bool ClassicAclConnection::ReadTransmitPowerLevel(TransmitPowerLevelType type) {
522   acl_connection_interface_->EnqueueCommand(
523       ReadTransmitPowerLevelBuilder::Create(handle_, type),
524       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
525                                                   &AclConnectionTracker::on_read_transmit_power_level_complete));
526   return true;
527 }
528 
ReadLinkSupervisionTimeout()529 bool ClassicAclConnection::ReadLinkSupervisionTimeout() {
530   acl_connection_interface_->EnqueueCommand(
531       ReadLinkSupervisionTimeoutBuilder::Create(handle_),
532       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
533                                                   &AclConnectionTracker::on_read_link_supervision_timeout_complete));
534   return true;
535 }
536 
WriteLinkSupervisionTimeout(uint16_t link_supervision_timeout)537 bool ClassicAclConnection::WriteLinkSupervisionTimeout(uint16_t link_supervision_timeout) {
538   acl_connection_interface_->EnqueueCommand(
539       WriteLinkSupervisionTimeoutBuilder::Create(handle_, link_supervision_timeout),
540       pimpl_->tracker.client_handler_->BindOnce(
541           check_complete<WriteLinkSupervisionTimeoutCompleteView>));
542   return true;
543 }
544 
ReadFailedContactCounter()545 bool ClassicAclConnection::ReadFailedContactCounter() {
546   acl_connection_interface_->EnqueueCommand(
547       ReadFailedContactCounterBuilder::Create(handle_),
548       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
549                                                   &AclConnectionTracker::on_read_failed_contact_counter_complete));
550   return true;
551 }
552 
ResetFailedContactCounter()553 bool ClassicAclConnection::ResetFailedContactCounter() {
554   acl_connection_interface_->EnqueueCommand(
555       ResetFailedContactCounterBuilder::Create(handle_),
556       pimpl_->tracker.client_handler_->BindOnce(
557           check_complete<ResetFailedContactCounterCompleteView>));
558   return true;
559 }
560 
ReadLinkQuality()561 bool ClassicAclConnection::ReadLinkQuality() {
562   acl_connection_interface_->EnqueueCommand(
563       ReadLinkQualityBuilder::Create(handle_),
564       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
565                                                   &AclConnectionTracker::on_read_link_quality_complete));
566   return true;
567 }
568 
ReadAfhChannelMap()569 bool ClassicAclConnection::ReadAfhChannelMap() {
570   acl_connection_interface_->EnqueueCommand(
571       ReadAfhChannelMapBuilder::Create(handle_),
572       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
573                                                   &AclConnectionTracker::on_read_afh_channel_map_complete));
574   return true;
575 }
576 
ReadRssi()577 bool ClassicAclConnection::ReadRssi() {
578   acl_connection_interface_->EnqueueCommand(
579       ReadRssiBuilder::Create(handle_),
580       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker, &AclConnectionTracker::on_read_rssi_complete));
581   return true;
582 }
583 
ReadRemoteVersionInformation()584 bool ClassicAclConnection::ReadRemoteVersionInformation() {
585   acl_connection_interface_->EnqueueCommand(
586       ReadRemoteVersionInformationBuilder::Create(handle_),
587       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
588                                                   &AclConnectionTracker::on_read_remote_version_information_status));
589   return true;
590 }
591 
ReadRemoteSupportedFeatures()592 bool ClassicAclConnection::ReadRemoteSupportedFeatures() {
593   acl_connection_interface_->EnqueueCommand(
594       ReadRemoteSupportedFeaturesBuilder::Create(handle_),
595       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker,
596                                                   &AclConnectionTracker::on_read_remote_supported_features_status));
597   return true;
598 }
599 
ReadRemoteExtendedFeatures(uint8_t page_number)600 bool ClassicAclConnection::ReadRemoteExtendedFeatures(uint8_t page_number) {
601   acl_connection_interface_->EnqueueCommand(
602       ReadRemoteExtendedFeaturesBuilder::Create(handle_, page_number),
603       pimpl_->tracker.client_handler_->BindOnceOn(
604           &pimpl_->tracker, &AclConnectionTracker::on_read_remote_extended_features_status));
605   return true;
606 }
607 
ReadClock(WhichClock which_clock)608 bool ClassicAclConnection::ReadClock(WhichClock which_clock) {
609   pimpl_->tracker.acl_connection_interface_->EnqueueCommand(
610       ReadClockBuilder::Create(handle_, which_clock),
611       pimpl_->tracker.client_handler_->BindOnceOn(&pimpl_->tracker, &AclConnectionTracker::on_read_clock_complete));
612   return true;
613 }
614 
615 }  // namespace acl_manager
616 }  // namespace hci
617 }  // namespace bluetooth
618