• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/controller.h"
18 
19 #include <future>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "common/init_flags.h"
25 #include "hci/hci_layer.h"
26 
27 namespace bluetooth {
28 namespace hci {
29 
30 using os::Handler;
31 
32 struct Controller::impl {
implbluetooth::hci::Controller::impl33   impl(Controller& module) : module_(module) {}
34 
Startbluetooth::hci::Controller::impl35   void Start(hci::HciLayer* hci) {
36     hci_ = hci;
37     Handler* handler = module_.GetHandler();
38     if (common::init_flags::gd_acl_is_enabled() || common::init_flags::gd_l2cap_is_enabled()) {
39       hci_->RegisterEventHandler(
40           EventCode::NUMBER_OF_COMPLETED_PACKETS, handler->BindOn(this, &Controller::impl::NumberOfCompletedPackets));
41     }
42 
43     le_set_event_mask(kDefaultLeEventMask);
44     set_event_mask(kDefaultEventMask);
45     write_le_host_support(Enable::ENABLED);
46     // SSP is managed by security layer once enabled
47     if (!common::init_flags::gd_security_is_enabled()) {
48       write_simple_pairing_mode(Enable::ENABLED);
49       hci_->EnqueueCommand(
50           WriteSecureConnectionsHostSupportBuilder::Create(Enable::ENABLED),
51           handler->BindOnceOn(this, &Controller::impl::write_secure_connections_host_support_complete_handler));
52     }
53     hci_->EnqueueCommand(ReadLocalNameBuilder::Create(),
54                          handler->BindOnceOn(this, &Controller::impl::read_local_name_complete_handler));
55     hci_->EnqueueCommand(ReadLocalVersionInformationBuilder::Create(),
56                          handler->BindOnceOn(this, &Controller::impl::read_local_version_information_complete_handler));
57     hci_->EnqueueCommand(ReadLocalSupportedCommandsBuilder::Create(),
58                          handler->BindOnceOn(this, &Controller::impl::read_local_supported_commands_complete_handler));
59 
60     // Wait for all extended features read
61     std::promise<void> features_promise;
62     auto features_future = features_promise.get_future();
63     hci_->EnqueueCommand(ReadLocalExtendedFeaturesBuilder::Create(0x00),
64                          handler->BindOnceOn(this, &Controller::impl::read_local_extended_features_complete_handler,
65                                              std::move(features_promise)));
66     features_future.wait();
67 
68     hci_->EnqueueCommand(ReadBufferSizeBuilder::Create(),
69                          handler->BindOnceOn(this, &Controller::impl::read_buffer_size_complete_handler));
70 
71     if (is_supported(OpCode::LE_READ_BUFFER_SIZE_V2)) {
72       hci_->EnqueueCommand(
73           LeReadBufferSizeV2Builder::Create(),
74           handler->BindOnceOn(this, &Controller::impl::le_read_buffer_size_v2_handler));
75     } else {
76       hci_->EnqueueCommand(
77           LeReadBufferSizeV1Builder::Create(),
78           handler->BindOnceOn(this, &Controller::impl::le_read_buffer_size_handler));
79     }
80 
81     hci_->EnqueueCommand(LeReadLocalSupportedFeaturesBuilder::Create(),
82                          handler->BindOnceOn(this, &Controller::impl::le_read_local_supported_features_handler));
83 
84     hci_->EnqueueCommand(LeReadSupportedStatesBuilder::Create(),
85                          handler->BindOnceOn(this, &Controller::impl::le_read_supported_states_handler));
86 
87     hci_->EnqueueCommand(
88         LeReadConnectListSizeBuilder::Create(),
89         handler->BindOnceOn(this, &Controller::impl::le_read_connect_list_size_handler));
90 
91     hci_->EnqueueCommand(
92         LeReadResolvingListSizeBuilder::Create(),
93         handler->BindOnceOn(this, &Controller::impl::le_read_resolving_list_size_handler));
94 
95     if (is_supported(OpCode::LE_READ_MAXIMUM_DATA_LENGTH)) {
96       hci_->EnqueueCommand(LeReadMaximumDataLengthBuilder::Create(),
97                            handler->BindOnceOn(this, &Controller::impl::le_read_maximum_data_length_handler));
98     } else {
99       le_maximum_data_length_.supported_max_rx_octets_ = 0;
100       le_maximum_data_length_.supported_max_rx_time_ = 0;
101       le_maximum_data_length_.supported_max_tx_octets_ = 0;
102       le_maximum_data_length_.supported_max_tx_time_ = 0;
103     }
104     if (is_supported(OpCode::LE_READ_SUGGESTED_DEFAULT_DATA_LENGTH)) {
105       hci_->EnqueueCommand(
106           LeReadSuggestedDefaultDataLengthBuilder::Create(),
107           handler->BindOnceOn(this, &Controller::impl::le_read_suggested_default_data_length_handler));
108     }
109     if (is_supported(OpCode::LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH)) {
110       hci_->EnqueueCommand(
111           LeReadMaximumAdvertisingDataLengthBuilder::Create(),
112           handler->BindOnceOn(this, &Controller::impl::le_read_maximum_advertising_data_length_handler));
113     } else {
114       le_maximum_advertising_data_length_ = 31;
115     }
116     if (is_supported(OpCode::LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS)) {
117       hci_->EnqueueCommand(
118           LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
119           handler->BindOnceOn(this, &Controller::impl::le_read_number_of_supported_advertising_sets_handler));
120     } else {
121       le_number_supported_advertising_sets_ = 1;
122     }
123     if (is_supported(OpCode::LE_READ_PERIODIC_ADVERTISING_LIST_SIZE)) {
124       hci_->EnqueueCommand(
125           LeReadPeriodicAdvertiserListSizeBuilder::Create(),
126           handler->BindOnceOn(this, &Controller::impl::le_read_periodic_advertiser_list_size_handler));
127     }
128 
129     hci_->EnqueueCommand(LeGetVendorCapabilitiesBuilder::Create(),
130                          handler->BindOnceOn(this, &Controller::impl::le_get_vendor_capabilities_handler));
131 
132     // We only need to synchronize the last read. Make BD_ADDR to be the last one.
133     std::promise<void> promise;
134     auto future = promise.get_future();
135     hci_->EnqueueCommand(
136         ReadBdAddrBuilder::Create(),
137         handler->BindOnceOn(this, &Controller::impl::read_controller_mac_address_handler, std::move(promise)));
138     future.wait();
139   }
140 
Stopbluetooth::hci::Controller::impl141   void Stop() {
142     if (bluetooth::common::init_flags::gd_core_is_enabled()) {
143       hci_->UnregisterEventHandler(EventCode::NUMBER_OF_COMPLETED_PACKETS);
144     }
145     hci_ = nullptr;
146   }
147 
NumberOfCompletedPacketsbluetooth::hci::Controller::impl148   void NumberOfCompletedPackets(EventView event) {
149     if (acl_credits_callback_.IsEmpty()) {
150       LOG_WARN("Received event when AclManager is not listening");
151       return;
152     }
153     auto complete_view = NumberOfCompletedPacketsView::Create(event);
154     ASSERT(complete_view.IsValid());
155     for (auto completed_packets : complete_view.GetCompletedPackets()) {
156       uint16_t handle = completed_packets.connection_handle_;
157       uint16_t credits = completed_packets.host_num_of_completed_packets_;
158       acl_credits_callback_.Invoke(handle, credits);
159       if (!acl_monitor_credits_callback_.IsEmpty()) {
160         acl_monitor_credits_callback_.Invoke(handle, credits);
161       }
162     }
163   }
164 
register_completed_acl_packets_callbackbluetooth::hci::Controller::impl165   void register_completed_acl_packets_callback(CompletedAclPacketsCallback callback) {
166     ASSERT(acl_credits_callback_.IsEmpty());
167     acl_credits_callback_ = callback;
168   }
169 
unregister_completed_acl_packets_callbackbluetooth::hci::Controller::impl170   void unregister_completed_acl_packets_callback() {
171     ASSERT(!acl_credits_callback_.IsEmpty());
172     acl_credits_callback_ = {};
173   }
174 
register_completed_monitor_acl_packets_callbackbluetooth::hci::Controller::impl175   void register_completed_monitor_acl_packets_callback(CompletedAclPacketsCallback callback) {
176     ASSERT(acl_monitor_credits_callback_.IsEmpty());
177     acl_monitor_credits_callback_ = callback;
178   }
179 
unregister_completed_monitor_acl_packets_callbackbluetooth::hci::Controller::impl180   void unregister_completed_monitor_acl_packets_callback() {
181     ASSERT(!acl_monitor_credits_callback_.IsEmpty());
182     acl_monitor_credits_callback_ = {};
183   }
184 
register_monitor_completed_acl_packets_callbackbluetooth::hci::Controller::impl185   void register_monitor_completed_acl_packets_callback(CompletedAclPacketsCallback callback) {
186     ASSERT(acl_monitor_credits_callback_.IsEmpty());
187     acl_monitor_credits_callback_ = callback;
188   }
189 
unregister_monitor_completed_acl_packets_callbackbluetooth::hci::Controller::impl190   void unregister_monitor_completed_acl_packets_callback() {
191     ASSERT(!acl_monitor_credits_callback_.IsEmpty());
192     acl_monitor_credits_callback_ = {};
193   }
194 
write_secure_connections_host_support_complete_handlerbluetooth::hci::Controller::impl195   void write_secure_connections_host_support_complete_handler(CommandCompleteView view) {
196     auto complete_view = WriteSecureConnectionsHostSupportCompleteView::Create(view);
197     ASSERT(complete_view.IsValid());
198     ErrorCode status = complete_view.GetStatus();
199     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
200   }
201 
read_local_name_complete_handlerbluetooth::hci::Controller::impl202   void read_local_name_complete_handler(CommandCompleteView view) {
203     auto complete_view = ReadLocalNameCompleteView::Create(view);
204     ASSERT(complete_view.IsValid());
205     ErrorCode status = complete_view.GetStatus();
206     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
207     std::array<uint8_t, 248> local_name_array = complete_view.GetLocalName();
208 
209     local_name_ = std::string(local_name_array.begin(), local_name_array.end());
210     // erase \0
211     local_name_.erase(std::find(local_name_.begin(), local_name_.end(), '\0'), local_name_.end());
212   }
213 
read_local_version_information_complete_handlerbluetooth::hci::Controller::impl214   void read_local_version_information_complete_handler(CommandCompleteView view) {
215     auto complete_view = ReadLocalVersionInformationCompleteView::Create(view);
216     ASSERT(complete_view.IsValid());
217     ErrorCode status = complete_view.GetStatus();
218     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
219 
220     local_version_information_ = complete_view.GetLocalVersionInformation();
221   }
222 
read_local_supported_commands_complete_handlerbluetooth::hci::Controller::impl223   void read_local_supported_commands_complete_handler(CommandCompleteView view) {
224     auto complete_view = ReadLocalSupportedCommandsCompleteView::Create(view);
225     ASSERT(complete_view.IsValid());
226     ErrorCode status = complete_view.GetStatus();
227     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
228     local_supported_commands_ = complete_view.GetSupportedCommands();
229   }
230 
read_local_extended_features_complete_handlerbluetooth::hci::Controller::impl231   void read_local_extended_features_complete_handler(std::promise<void> promise, CommandCompleteView view) {
232     auto complete_view = ReadLocalExtendedFeaturesCompleteView::Create(view);
233     ASSERT(complete_view.IsValid());
234     ErrorCode status = complete_view.GetStatus();
235     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
236     uint8_t page_number = complete_view.GetPageNumber();
237     maximum_page_number_ = complete_view.GetMaximumPageNumber();
238     extended_lmp_features_array_.push_back(complete_view.GetExtendedLmpFeatures());
239 
240     // Query all extended features
241     if (page_number < maximum_page_number_) {
242       page_number++;
243       hci_->EnqueueCommand(
244           ReadLocalExtendedFeaturesBuilder::Create(page_number),
245           module_.GetHandler()->BindOnceOn(this, &Controller::impl::read_local_extended_features_complete_handler,
246                                            std::move(promise)));
247     } else {
248       promise.set_value();
249     }
250   }
251 
read_buffer_size_complete_handlerbluetooth::hci::Controller::impl252   void read_buffer_size_complete_handler(CommandCompleteView view) {
253     auto complete_view = ReadBufferSizeCompleteView::Create(view);
254     ASSERT(complete_view.IsValid());
255     ErrorCode status = complete_view.GetStatus();
256     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
257     acl_buffer_length_ = complete_view.GetAclDataPacketLength();
258     acl_buffers_ = complete_view.GetTotalNumAclDataPackets();
259 
260     sco_buffer_length_ = complete_view.GetSynchronousDataPacketLength();
261     sco_buffers_ = complete_view.GetTotalNumSynchronousDataPackets();
262   }
263 
read_controller_mac_address_handlerbluetooth::hci::Controller::impl264   void read_controller_mac_address_handler(std::promise<void> promise, CommandCompleteView view) {
265     auto complete_view = ReadBdAddrCompleteView::Create(view);
266     ASSERT(complete_view.IsValid());
267     ErrorCode status = complete_view.GetStatus();
268     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
269     mac_address_ = complete_view.GetBdAddr();
270     promise.set_value();
271   }
272 
le_read_buffer_size_handlerbluetooth::hci::Controller::impl273   void le_read_buffer_size_handler(CommandCompleteView view) {
274     auto complete_view = LeReadBufferSizeV1CompleteView::Create(view);
275     ASSERT(complete_view.IsValid());
276     ErrorCode status = complete_view.GetStatus();
277     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
278     le_buffer_size_ = complete_view.GetLeBufferSize();
279 
280     // If LE buffer size is zero, then buffers returned by Read_Buffer_Size are shared between BR/EDR and LE.
281     if (le_buffer_size_.total_num_le_packets_ == 0) {
282       ASSERT(acl_buffers_ != 0);
283       le_buffer_size_.total_num_le_packets_ = acl_buffers_ / 2;
284       acl_buffers_ -= le_buffer_size_.total_num_le_packets_;
285       le_buffer_size_.le_data_packet_length_ = acl_buffer_length_;
286     }
287   }
288 
le_read_buffer_size_v2_handlerbluetooth::hci::Controller::impl289   void le_read_buffer_size_v2_handler(CommandCompleteView view) {
290     auto complete_view = LeReadBufferSizeV2CompleteView::Create(view);
291     ASSERT(complete_view.IsValid());
292     ErrorCode status = complete_view.GetStatus();
293     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
294     le_buffer_size_ = complete_view.GetLeBufferSize();
295     iso_buffer_size_ = complete_view.GetIsoBufferSize();
296 
297     // If LE buffer size is zero, then buffers returned by Read_Buffer_Size are shared between BR/EDR and LE.
298     if (le_buffer_size_.total_num_le_packets_ == 0) {
299       ASSERT(acl_buffers_ != 0);
300       le_buffer_size_.total_num_le_packets_ = acl_buffers_ / 2;
301       acl_buffers_ -= le_buffer_size_.total_num_le_packets_;
302       le_buffer_size_.le_data_packet_length_ = acl_buffer_length_;
303     }
304   }
305 
le_read_local_supported_features_handlerbluetooth::hci::Controller::impl306   void le_read_local_supported_features_handler(CommandCompleteView view) {
307     auto complete_view = LeReadLocalSupportedFeaturesCompleteView::Create(view);
308     ASSERT(complete_view.IsValid());
309     ErrorCode status = complete_view.GetStatus();
310     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
311     le_local_supported_features_ = complete_view.GetLeFeatures();
312   }
313 
le_read_supported_states_handlerbluetooth::hci::Controller::impl314   void le_read_supported_states_handler(CommandCompleteView view) {
315     auto complete_view = LeReadSupportedStatesCompleteView::Create(view);
316     ASSERT(complete_view.IsValid());
317     ErrorCode status = complete_view.GetStatus();
318     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
319     le_supported_states_ = complete_view.GetLeStates();
320   }
321 
le_read_connect_list_size_handlerbluetooth::hci::Controller::impl322   void le_read_connect_list_size_handler(CommandCompleteView view) {
323     auto complete_view = LeReadConnectListSizeCompleteView::Create(view);
324     ASSERT(complete_view.IsValid());
325     ErrorCode status = complete_view.GetStatus();
326     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
327     le_connect_list_size_ = complete_view.GetConnectListSize();
328   }
329 
le_read_resolving_list_size_handlerbluetooth::hci::Controller::impl330   void le_read_resolving_list_size_handler(CommandCompleteView view) {
331     auto complete_view = LeReadResolvingListSizeCompleteView::Create(view);
332     ASSERT(complete_view.IsValid());
333     ErrorCode status = complete_view.GetStatus();
334     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
335     le_resolving_list_size_ = complete_view.GetResolvingListSize();
336   }
337 
le_read_maximum_data_length_handlerbluetooth::hci::Controller::impl338   void le_read_maximum_data_length_handler(CommandCompleteView view) {
339     auto complete_view = LeReadMaximumDataLengthCompleteView::Create(view);
340     ASSERT(complete_view.IsValid());
341     ErrorCode status = complete_view.GetStatus();
342     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
343     le_maximum_data_length_ = complete_view.GetLeMaximumDataLength();
344   }
345 
le_read_suggested_default_data_length_handlerbluetooth::hci::Controller::impl346   void le_read_suggested_default_data_length_handler(CommandCompleteView view) {
347     auto complete_view = LeReadSuggestedDefaultDataLengthCompleteView::Create(view);
348     ASSERT(complete_view.IsValid());
349     ErrorCode status = complete_view.GetStatus();
350     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
351     le_suggested_default_data_length_ = complete_view.GetTxOctets();
352   }
353 
le_read_maximum_advertising_data_length_handlerbluetooth::hci::Controller::impl354   void le_read_maximum_advertising_data_length_handler(CommandCompleteView view) {
355     auto complete_view = LeReadMaximumAdvertisingDataLengthCompleteView::Create(view);
356     ASSERT(complete_view.IsValid());
357     ErrorCode status = complete_view.GetStatus();
358     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
359     le_maximum_advertising_data_length_ = complete_view.GetMaximumAdvertisingDataLength();
360   }
361 
le_read_number_of_supported_advertising_sets_handlerbluetooth::hci::Controller::impl362   void le_read_number_of_supported_advertising_sets_handler(CommandCompleteView view) {
363     auto complete_view = LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(view);
364     ASSERT(complete_view.IsValid());
365     ErrorCode status = complete_view.GetStatus();
366     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
367     le_number_supported_advertising_sets_ = complete_view.GetNumberSupportedAdvertisingSets();
368   }
369 
le_read_periodic_advertiser_list_size_handlerbluetooth::hci::Controller::impl370   void le_read_periodic_advertiser_list_size_handler(CommandCompleteView view) {
371     auto complete_view = LeReadPeriodicAdvertiserListSizeCompleteView::Create(view);
372     ASSERT(complete_view.IsValid());
373     ErrorCode status = complete_view.GetStatus();
374     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
375     le_periodic_advertiser_list_size_ = complete_view.GetPeriodicAdvertiserListSize();
376   }
377 
le_get_vendor_capabilities_handlerbluetooth::hci::Controller::impl378   void le_get_vendor_capabilities_handler(CommandCompleteView view) {
379     auto complete_view = LeGetVendorCapabilitiesCompleteView::Create(view);
380 
381     vendor_capabilities_.is_supported_ = 0x00;
382     vendor_capabilities_.max_advt_instances_ = 0x00;
383     vendor_capabilities_.offloaded_resolution_of_private_address_ = 0x00;
384     vendor_capabilities_.total_scan_results_storage_ = 0x00;
385     vendor_capabilities_.max_irk_list_sz_ = 0x00;
386     vendor_capabilities_.filtering_support_ = 0x00;
387     vendor_capabilities_.max_filter_ = 0x00;
388     vendor_capabilities_.activity_energy_info_support_ = 0x00;
389     vendor_capabilities_.version_supported_ = 0x00;
390     vendor_capabilities_.version_supported_ = 0x00;
391     vendor_capabilities_.total_num_of_advt_tracked_ = 0x00;
392     vendor_capabilities_.extended_scan_support_ = 0x00;
393     vendor_capabilities_.debug_logging_supported_ = 0x00;
394     vendor_capabilities_.le_address_generation_offloading_support_ = 0x00;
395     vendor_capabilities_.a2dp_source_offload_capability_mask_ = 0x00;
396     vendor_capabilities_.bluetooth_quality_report_support_ = 0x00;
397 
398     if (complete_view.IsValid()) {
399       vendor_capabilities_.is_supported_ = 0x01;
400 
401       // v0.55
402       BaseVendorCapabilities base_vendor_capabilities = complete_view.GetBaseVendorCapabilities();
403       vendor_capabilities_.max_advt_instances_ = base_vendor_capabilities.max_advt_instances_;
404       vendor_capabilities_.offloaded_resolution_of_private_address_ =
405           base_vendor_capabilities.offloaded_resolution_of_private_address_;
406       vendor_capabilities_.total_scan_results_storage_ = base_vendor_capabilities.total_scan_results_storage_;
407       vendor_capabilities_.max_irk_list_sz_ = base_vendor_capabilities.max_irk_list_sz_;
408       vendor_capabilities_.filtering_support_ = base_vendor_capabilities.filtering_support_;
409       vendor_capabilities_.max_filter_ = base_vendor_capabilities.max_filter_;
410       vendor_capabilities_.activity_energy_info_support_ = base_vendor_capabilities.activity_energy_info_support_;
411       if (complete_view.GetPayload().size() == 0) {
412         vendor_capabilities_.version_supported_ = 55;
413         return;
414       }
415 
416       // v0.95
417       auto v95 = LeGetVendorCapabilitiesComplete095View::Create(complete_view);
418       if (!v95.IsValid()) {
419         LOG_ERROR("invalid data for hci requirements v0.95");
420         return;
421       }
422       vendor_capabilities_.version_supported_ = v95.GetVersionSupported();
423       vendor_capabilities_.total_num_of_advt_tracked_ = v95.GetTotalNumOfAdvtTracked();
424       vendor_capabilities_.extended_scan_support_ = v95.GetExtendedScanSupport();
425       vendor_capabilities_.debug_logging_supported_ = v95.GetDebugLoggingSupported();
426       if (vendor_capabilities_.version_supported_ <= 95 || complete_view.GetPayload().size() == 0) {
427         return;
428       }
429 
430       // v0.96
431       auto v96 = LeGetVendorCapabilitiesComplete096View::Create(v95);
432       if (!v96.IsValid()) {
433         LOG_ERROR("invalid data for hci requirements v0.96");
434         return;
435       }
436       vendor_capabilities_.le_address_generation_offloading_support_ = v96.GetLeAddressGenerationOffloadingSupport();
437       if (vendor_capabilities_.version_supported_ <= 96 || complete_view.GetPayload().size() == 0) {
438         return;
439       }
440 
441       // v0.98
442       auto v98 = LeGetVendorCapabilitiesComplete098View::Create(v96);
443       if (!v98.IsValid()) {
444         LOG_ERROR("invalid data for hci requirements v0.98");
445         return;
446       }
447       vendor_capabilities_.a2dp_source_offload_capability_mask_ = v98.GetA2dpSourceOffloadCapabilityMask();
448       vendor_capabilities_.bluetooth_quality_report_support_ = v98.GetBluetoothQualityReportSupport();
449     }
450   }
451 
set_event_maskbluetooth::hci::Controller::impl452   void set_event_mask(uint64_t event_mask) {
453     std::unique_ptr<SetEventMaskBuilder> packet = SetEventMaskBuilder::Create(event_mask);
454     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
455                                                 this, &Controller::impl::check_status<SetEventMaskCompleteView>));
456   }
457 
write_le_host_supportbluetooth::hci::Controller::impl458   void write_le_host_support(Enable enable) {
459     // Since Bluetooth Core Spec 4.1, this bit should be 0, but some controllers still require it
460     Enable simultaneous_le_host = Enable::ENABLED;
461     std::unique_ptr<WriteLeHostSupportBuilder> packet = WriteLeHostSupportBuilder::Create(enable, simultaneous_le_host);
462     hci_->EnqueueCommand(
463         std::move(packet),
464         module_.GetHandler()->BindOnceOn(this, &Controller::impl::check_status<WriteLeHostSupportCompleteView>));
465   }
466 
write_simple_pairing_modebluetooth::hci::Controller::impl467   void write_simple_pairing_mode(Enable enable) {
468     std::unique_ptr<WriteSimplePairingModeBuilder> packet = WriteSimplePairingModeBuilder::Create(enable);
469     hci_->EnqueueCommand(
470         std::move(packet),
471         module_.GetHandler()->BindOnceOn(this, &Controller::impl::check_status<WriteSimplePairingModeCompleteView>));
472   }
473 
resetbluetooth::hci::Controller::impl474   void reset() {
475     std::unique_ptr<ResetBuilder> packet = ResetBuilder::Create();
476     hci_->EnqueueCommand(std::move(packet),
477                          module_.GetHandler()->BindOnceOn(this, &Controller::impl::check_status<ResetCompleteView>));
478   }
479 
set_event_filterbluetooth::hci::Controller::impl480   void set_event_filter(std::unique_ptr<SetEventFilterBuilder> packet) {
481     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
482                                                 this, &Controller::impl::check_status<SetEventFilterCompleteView>));
483   }
484 
write_local_namebluetooth::hci::Controller::impl485   void write_local_name(std::string local_name) {
486     ASSERT(local_name.length() <= 248);
487     // Fill remaining char with 0
488     local_name.append(std::string(248 - local_name.length(), '\0'));
489     std::array<uint8_t, 248> local_name_array;
490     std::copy(std::begin(local_name), std::end(local_name), std::begin(local_name_array));
491 
492     std::unique_ptr<WriteLocalNameBuilder> packet = WriteLocalNameBuilder::Create(local_name_array);
493     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
494                                                 this, &Controller::impl::check_status<WriteLocalNameCompleteView>));
495   }
496 
host_buffer_sizebluetooth::hci::Controller::impl497   void host_buffer_size(uint16_t host_acl_data_packet_length, uint8_t host_synchronous_data_packet_length,
498                         uint16_t host_total_num_acl_data_packets, uint16_t host_total_num_synchronous_data_packets) {
499     std::unique_ptr<HostBufferSizeBuilder> packet =
500         HostBufferSizeBuilder::Create(host_acl_data_packet_length, host_synchronous_data_packet_length,
501                                       host_total_num_acl_data_packets, host_total_num_synchronous_data_packets);
502     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
503                                                 this, &Controller::impl::check_status<HostBufferSizeCompleteView>));
504   }
505 
le_set_event_maskbluetooth::hci::Controller::impl506   void le_set_event_mask(uint64_t le_event_mask) {
507     std::unique_ptr<LeSetEventMaskBuilder> packet = LeSetEventMaskBuilder::Create(le_event_mask);
508     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
509                                                 this, &Controller::impl::check_status<LeSetEventMaskCompleteView>));
510   }
511 
512   template <class T>
check_statusbluetooth::hci::Controller::impl513   void check_status(CommandCompleteView view) {
514     ASSERT(view.IsValid());
515     auto status_view = T::Create(view);
516     ASSERT(status_view.IsValid());
517     ASSERT(status_view.GetStatus() == ErrorCode::SUCCESS);
518   }
519 
520 #define OP_CODE_MAPPING(name)                                                  \
521   case OpCode::name: {                                                         \
522     uint16_t index = (uint16_t)OpCodeIndex::name;                              \
523     uint16_t byte_index = index / 10;                                          \
524     uint16_t bit_index = index % 10;                                           \
525     bool supported = local_supported_commands_[byte_index] & (1 << bit_index); \
526     if (!supported) {                                                          \
527       LOG_DEBUG("unsupported command opcode: 0x%04x", (uint16_t)OpCode::name); \
528     }                                                                          \
529     return supported;                                                          \
530   }
531 
is_supportedbluetooth::hci::Controller::impl532   bool is_supported(OpCode op_code) {
533     switch (op_code) {
534       OP_CODE_MAPPING(INQUIRY)
535       OP_CODE_MAPPING(INQUIRY_CANCEL)
536       OP_CODE_MAPPING(PERIODIC_INQUIRY_MODE)
537       OP_CODE_MAPPING(EXIT_PERIODIC_INQUIRY_MODE)
538       OP_CODE_MAPPING(CREATE_CONNECTION)
539       OP_CODE_MAPPING(DISCONNECT)
540       OP_CODE_MAPPING(CREATE_CONNECTION_CANCEL)
541       OP_CODE_MAPPING(ACCEPT_CONNECTION_REQUEST)
542       OP_CODE_MAPPING(REJECT_CONNECTION_REQUEST)
543       OP_CODE_MAPPING(LINK_KEY_REQUEST_REPLY)
544       OP_CODE_MAPPING(LINK_KEY_REQUEST_NEGATIVE_REPLY)
545       OP_CODE_MAPPING(PIN_CODE_REQUEST_REPLY)
546       OP_CODE_MAPPING(PIN_CODE_REQUEST_NEGATIVE_REPLY)
547       OP_CODE_MAPPING(CHANGE_CONNECTION_PACKET_TYPE)
548       OP_CODE_MAPPING(AUTHENTICATION_REQUESTED)
549       OP_CODE_MAPPING(SET_CONNECTION_ENCRYPTION)
550       OP_CODE_MAPPING(CHANGE_CONNECTION_LINK_KEY)
551       OP_CODE_MAPPING(CENTRAL_LINK_KEY)
552       OP_CODE_MAPPING(REMOTE_NAME_REQUEST)
553       OP_CODE_MAPPING(REMOTE_NAME_REQUEST_CANCEL)
554       OP_CODE_MAPPING(READ_REMOTE_SUPPORTED_FEATURES)
555       OP_CODE_MAPPING(READ_REMOTE_EXTENDED_FEATURES)
556       OP_CODE_MAPPING(READ_REMOTE_VERSION_INFORMATION)
557       OP_CODE_MAPPING(READ_CLOCK_OFFSET)
558       OP_CODE_MAPPING(READ_LMP_HANDLE)
559       OP_CODE_MAPPING(HOLD_MODE)
560       OP_CODE_MAPPING(SNIFF_MODE)
561       OP_CODE_MAPPING(EXIT_SNIFF_MODE)
562       OP_CODE_MAPPING(QOS_SETUP)
563       OP_CODE_MAPPING(ROLE_DISCOVERY)
564       OP_CODE_MAPPING(SWITCH_ROLE)
565       OP_CODE_MAPPING(READ_LINK_POLICY_SETTINGS)
566       OP_CODE_MAPPING(WRITE_LINK_POLICY_SETTINGS)
567       OP_CODE_MAPPING(READ_DEFAULT_LINK_POLICY_SETTINGS)
568       OP_CODE_MAPPING(WRITE_DEFAULT_LINK_POLICY_SETTINGS)
569       OP_CODE_MAPPING(FLOW_SPECIFICATION)
570       OP_CODE_MAPPING(SET_EVENT_MASK)
571       OP_CODE_MAPPING(RESET)
572       OP_CODE_MAPPING(SET_EVENT_FILTER)
573       OP_CODE_MAPPING(FLUSH)
574       OP_CODE_MAPPING(READ_PIN_TYPE)
575       OP_CODE_MAPPING(WRITE_PIN_TYPE)
576       OP_CODE_MAPPING(READ_STORED_LINK_KEY)
577       OP_CODE_MAPPING(WRITE_STORED_LINK_KEY)
578       OP_CODE_MAPPING(DELETE_STORED_LINK_KEY)
579       OP_CODE_MAPPING(WRITE_LOCAL_NAME)
580       OP_CODE_MAPPING(READ_LOCAL_NAME)
581       OP_CODE_MAPPING(READ_CONNECTION_ACCEPT_TIMEOUT)
582       OP_CODE_MAPPING(WRITE_CONNECTION_ACCEPT_TIMEOUT)
583       OP_CODE_MAPPING(READ_PAGE_TIMEOUT)
584       OP_CODE_MAPPING(WRITE_PAGE_TIMEOUT)
585       OP_CODE_MAPPING(READ_SCAN_ENABLE)
586       OP_CODE_MAPPING(WRITE_SCAN_ENABLE)
587       OP_CODE_MAPPING(READ_PAGE_SCAN_ACTIVITY)
588       OP_CODE_MAPPING(WRITE_PAGE_SCAN_ACTIVITY)
589       OP_CODE_MAPPING(READ_INQUIRY_SCAN_ACTIVITY)
590       OP_CODE_MAPPING(WRITE_INQUIRY_SCAN_ACTIVITY)
591       OP_CODE_MAPPING(READ_AUTHENTICATION_ENABLE)
592       OP_CODE_MAPPING(WRITE_AUTHENTICATION_ENABLE)
593       OP_CODE_MAPPING(READ_CLASS_OF_DEVICE)
594       OP_CODE_MAPPING(WRITE_CLASS_OF_DEVICE)
595       OP_CODE_MAPPING(READ_VOICE_SETTING)
596       OP_CODE_MAPPING(WRITE_VOICE_SETTING)
597       OP_CODE_MAPPING(READ_AUTOMATIC_FLUSH_TIMEOUT)
598       OP_CODE_MAPPING(WRITE_AUTOMATIC_FLUSH_TIMEOUT)
599       OP_CODE_MAPPING(READ_NUM_BROADCAST_RETRANSMITS)
600       OP_CODE_MAPPING(WRITE_NUM_BROADCAST_RETRANSMITS)
601       OP_CODE_MAPPING(READ_HOLD_MODE_ACTIVITY)
602       OP_CODE_MAPPING(WRITE_HOLD_MODE_ACTIVITY)
603       OP_CODE_MAPPING(READ_TRANSMIT_POWER_LEVEL)
604       OP_CODE_MAPPING(READ_SYNCHRONOUS_FLOW_CONTROL_ENABLE)
605       OP_CODE_MAPPING(WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)
606       OP_CODE_MAPPING(SET_CONTROLLER_TO_HOST_FLOW_CONTROL)
607       OP_CODE_MAPPING(HOST_BUFFER_SIZE)
608       OP_CODE_MAPPING(HOST_NUM_COMPLETED_PACKETS)
609       OP_CODE_MAPPING(READ_LINK_SUPERVISION_TIMEOUT)
610       OP_CODE_MAPPING(WRITE_LINK_SUPERVISION_TIMEOUT)
611       OP_CODE_MAPPING(READ_NUMBER_OF_SUPPORTED_IAC)
612       OP_CODE_MAPPING(READ_CURRENT_IAC_LAP)
613       OP_CODE_MAPPING(WRITE_CURRENT_IAC_LAP)
614       OP_CODE_MAPPING(SET_AFH_HOST_CHANNEL_CLASSIFICATION)
615       OP_CODE_MAPPING(READ_INQUIRY_SCAN_TYPE)
616       OP_CODE_MAPPING(WRITE_INQUIRY_SCAN_TYPE)
617       OP_CODE_MAPPING(READ_INQUIRY_MODE)
618       OP_CODE_MAPPING(WRITE_INQUIRY_MODE)
619       OP_CODE_MAPPING(READ_PAGE_SCAN_TYPE)
620       OP_CODE_MAPPING(WRITE_PAGE_SCAN_TYPE)
621       OP_CODE_MAPPING(READ_AFH_CHANNEL_ASSESSMENT_MODE)
622       OP_CODE_MAPPING(WRITE_AFH_CHANNEL_ASSESSMENT_MODE)
623       OP_CODE_MAPPING(READ_LOCAL_VERSION_INFORMATION)
624       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_FEATURES)
625       OP_CODE_MAPPING(READ_LOCAL_EXTENDED_FEATURES)
626       OP_CODE_MAPPING(READ_BUFFER_SIZE)
627       OP_CODE_MAPPING(READ_BD_ADDR)
628       OP_CODE_MAPPING(READ_FAILED_CONTACT_COUNTER)
629       OP_CODE_MAPPING(RESET_FAILED_CONTACT_COUNTER)
630       OP_CODE_MAPPING(READ_LINK_QUALITY)
631       OP_CODE_MAPPING(READ_RSSI)
632       OP_CODE_MAPPING(READ_AFH_CHANNEL_MAP)
633       OP_CODE_MAPPING(READ_CLOCK)
634       OP_CODE_MAPPING(READ_LOOPBACK_MODE)
635       OP_CODE_MAPPING(WRITE_LOOPBACK_MODE)
636       OP_CODE_MAPPING(ENABLE_DEVICE_UNDER_TEST_MODE)
637       OP_CODE_MAPPING(SETUP_SYNCHRONOUS_CONNECTION)
638       OP_CODE_MAPPING(ACCEPT_SYNCHRONOUS_CONNECTION)
639       OP_CODE_MAPPING(REJECT_SYNCHRONOUS_CONNECTION)
640       OP_CODE_MAPPING(READ_EXTENDED_INQUIRY_RESPONSE)
641       OP_CODE_MAPPING(WRITE_EXTENDED_INQUIRY_RESPONSE)
642       OP_CODE_MAPPING(REFRESH_ENCRYPTION_KEY)
643       OP_CODE_MAPPING(SNIFF_SUBRATING)
644       OP_CODE_MAPPING(READ_SIMPLE_PAIRING_MODE)
645       OP_CODE_MAPPING(WRITE_SIMPLE_PAIRING_MODE)
646       OP_CODE_MAPPING(READ_LOCAL_OOB_DATA)
647       OP_CODE_MAPPING(READ_INQUIRY_RESPONSE_TRANSMIT_POWER_LEVEL)
648       OP_CODE_MAPPING(WRITE_INQUIRY_TRANSMIT_POWER_LEVEL)
649       OP_CODE_MAPPING(IO_CAPABILITY_REQUEST_REPLY)
650       OP_CODE_MAPPING(USER_CONFIRMATION_REQUEST_REPLY)
651       OP_CODE_MAPPING(USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY)
652       OP_CODE_MAPPING(USER_PASSKEY_REQUEST_REPLY)
653       OP_CODE_MAPPING(USER_PASSKEY_REQUEST_NEGATIVE_REPLY)
654       OP_CODE_MAPPING(REMOTE_OOB_DATA_REQUEST_REPLY)
655       OP_CODE_MAPPING(WRITE_SIMPLE_PAIRING_DEBUG_MODE)
656       OP_CODE_MAPPING(REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY)
657       OP_CODE_MAPPING(SEND_KEYPRESS_NOTIFICATION)
658       OP_CODE_MAPPING(SET_EVENT_MASK_PAGE_2)
659       OP_CODE_MAPPING(IO_CAPABILITY_REQUEST_NEGATIVE_REPLY)
660       OP_CODE_MAPPING(REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)
661       OP_CODE_MAPPING(READ_ENCRYPTION_KEY_SIZE)
662       OP_CODE_MAPPING(READ_DATA_BLOCK_SIZE)
663       OP_CODE_MAPPING(READ_LE_HOST_SUPPORT)
664       OP_CODE_MAPPING(WRITE_LE_HOST_SUPPORT)
665       OP_CODE_MAPPING(LE_SET_EVENT_MASK)
666       OP_CODE_MAPPING(LE_READ_BUFFER_SIZE_V1)
667       OP_CODE_MAPPING(LE_READ_LOCAL_SUPPORTED_FEATURES)
668       OP_CODE_MAPPING(LE_SET_RANDOM_ADDRESS)
669       OP_CODE_MAPPING(LE_SET_ADVERTISING_PARAMETERS)
670       OP_CODE_MAPPING(LE_READ_ADVERTISING_PHYSICAL_CHANNEL_TX_POWER)
671       OP_CODE_MAPPING(LE_SET_ADVERTISING_DATA)
672       OP_CODE_MAPPING(LE_SET_SCAN_RESPONSE_DATA)
673       OP_CODE_MAPPING(LE_SET_ADVERTISING_ENABLE)
674       OP_CODE_MAPPING(LE_SET_SCAN_PARAMETERS)
675       OP_CODE_MAPPING(LE_SET_SCAN_ENABLE)
676       OP_CODE_MAPPING(LE_CREATE_CONNECTION)
677       OP_CODE_MAPPING(LE_CREATE_CONNECTION_CANCEL)
678       OP_CODE_MAPPING(LE_READ_CONNECT_LIST_SIZE)
679       OP_CODE_MAPPING(LE_CLEAR_CONNECT_LIST)
680       OP_CODE_MAPPING(LE_ADD_DEVICE_TO_CONNECT_LIST)
681       OP_CODE_MAPPING(LE_REMOVE_DEVICE_FROM_CONNECT_LIST)
682       OP_CODE_MAPPING(LE_CONNECTION_UPDATE)
683       OP_CODE_MAPPING(LE_SET_HOST_CHANNEL_CLASSIFICATION)
684       OP_CODE_MAPPING(LE_READ_CHANNEL_MAP)
685       OP_CODE_MAPPING(LE_READ_REMOTE_FEATURES)
686       OP_CODE_MAPPING(LE_ENCRYPT)
687       OP_CODE_MAPPING(LE_RAND)
688       OP_CODE_MAPPING(LE_START_ENCRYPTION)
689       OP_CODE_MAPPING(LE_LONG_TERM_KEY_REQUEST_REPLY)
690       OP_CODE_MAPPING(LE_LONG_TERM_KEY_REQUEST_NEGATIVE_REPLY)
691       OP_CODE_MAPPING(LE_READ_SUPPORTED_STATES)
692       OP_CODE_MAPPING(LE_RECEIVER_TEST)
693       OP_CODE_MAPPING(LE_TRANSMITTER_TEST)
694       OP_CODE_MAPPING(LE_TEST_END)
695       OP_CODE_MAPPING(ENHANCED_SETUP_SYNCHRONOUS_CONNECTION)
696       OP_CODE_MAPPING(ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION)
697       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CODECS_V1)
698       OP_CODE_MAPPING(READ_SECURE_CONNECTIONS_HOST_SUPPORT)
699       OP_CODE_MAPPING(WRITE_SECURE_CONNECTIONS_HOST_SUPPORT)
700       OP_CODE_MAPPING(READ_LOCAL_OOB_EXTENDED_DATA)
701       OP_CODE_MAPPING(WRITE_SECURE_CONNECTIONS_TEST_MODE)
702       OP_CODE_MAPPING(LE_REMOTE_CONNECTION_PARAMETER_REQUEST_REPLY)
703       OP_CODE_MAPPING(LE_REMOTE_CONNECTION_PARAMETER_REQUEST_NEGATIVE_REPLY)
704       OP_CODE_MAPPING(LE_SET_DATA_LENGTH)
705       OP_CODE_MAPPING(LE_READ_SUGGESTED_DEFAULT_DATA_LENGTH)
706       OP_CODE_MAPPING(LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)
707       OP_CODE_MAPPING(LE_READ_LOCAL_P_256_PUBLIC_KEY_COMMAND)
708       OP_CODE_MAPPING(LE_GENERATE_DHKEY_COMMAND_V1)
709       OP_CODE_MAPPING(LE_ADD_DEVICE_TO_RESOLVING_LIST)
710       OP_CODE_MAPPING(LE_REMOVE_DEVICE_FROM_RESOLVING_LIST)
711       OP_CODE_MAPPING(LE_CLEAR_RESOLVING_LIST)
712       OP_CODE_MAPPING(LE_READ_RESOLVING_LIST_SIZE)
713       OP_CODE_MAPPING(LE_READ_PEER_RESOLVABLE_ADDRESS)
714       OP_CODE_MAPPING(LE_READ_LOCAL_RESOLVABLE_ADDRESS)
715       OP_CODE_MAPPING(LE_SET_ADDRESS_RESOLUTION_ENABLE)
716       OP_CODE_MAPPING(LE_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT)
717       OP_CODE_MAPPING(LE_READ_MAXIMUM_DATA_LENGTH)
718       OP_CODE_MAPPING(LE_READ_PHY)
719       OP_CODE_MAPPING(LE_SET_DEFAULT_PHY)
720       OP_CODE_MAPPING(LE_SET_PHY)
721       OP_CODE_MAPPING(LE_ENHANCED_RECEIVER_TEST)
722       OP_CODE_MAPPING(LE_ENHANCED_TRANSMITTER_TEST)
723       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_RANDOM_ADDRESS)
724       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_PARAMETERS)
725       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_DATA)
726       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_SCAN_RESPONSE)
727       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_ENABLE)
728       OP_CODE_MAPPING(LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH)
729       OP_CODE_MAPPING(LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS)
730       OP_CODE_MAPPING(LE_REMOVE_ADVERTISING_SET)
731       OP_CODE_MAPPING(LE_CLEAR_ADVERTISING_SETS)
732       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_PARAM)
733       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_DATA)
734       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_ENABLE)
735       OP_CODE_MAPPING(LE_SET_EXTENDED_SCAN_PARAMETERS)
736       OP_CODE_MAPPING(LE_SET_EXTENDED_SCAN_ENABLE)
737       OP_CODE_MAPPING(LE_EXTENDED_CREATE_CONNECTION)
738       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_CREATE_SYNC)
739       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_CREATE_SYNC_CANCEL)
740       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_TERMINATE_SYNC)
741       OP_CODE_MAPPING(LE_ADD_DEVICE_TO_PERIODIC_ADVERTISING_LIST)
742       OP_CODE_MAPPING(LE_REMOVE_DEVICE_FROM_PERIODIC_ADVERTISING_LIST)
743       OP_CODE_MAPPING(LE_CLEAR_PERIODIC_ADVERTISING_LIST)
744       OP_CODE_MAPPING(LE_READ_PERIODIC_ADVERTISING_LIST_SIZE)
745       OP_CODE_MAPPING(LE_READ_TRANSMIT_POWER)
746       OP_CODE_MAPPING(LE_READ_RF_PATH_COMPENSATION_POWER)
747       OP_CODE_MAPPING(LE_WRITE_RF_PATH_COMPENSATION_POWER)
748       OP_CODE_MAPPING(LE_SET_PRIVACY_MODE)
749       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_RECEIVE_ENABLE)
750       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_SYNC_TRANSFER)
751       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_SET_INFO_TRANSFER)
752       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_SYNC_TRANSFER_PARAMETERS)
753       OP_CODE_MAPPING(LE_SET_DEFAULT_PERIODIC_ADVERTISING_SYNC_TRANSFER_PARAMETERS)
754       OP_CODE_MAPPING(LE_GENERATE_DHKEY_COMMAND)
755       OP_CODE_MAPPING(LE_MODIFY_SLEEP_CLOCK_ACCURACY)
756       OP_CODE_MAPPING(LE_READ_BUFFER_SIZE_V2)
757       OP_CODE_MAPPING(LE_READ_ISO_TX_SYNC)
758       OP_CODE_MAPPING(LE_SET_CIG_PARAMETERS)
759       OP_CODE_MAPPING(LE_SET_CIG_PARAMETERS_TEST)
760       OP_CODE_MAPPING(LE_CREATE_CIS)
761       OP_CODE_MAPPING(LE_REMOVE_CIG)
762       OP_CODE_MAPPING(LE_ACCEPT_CIS_REQUEST)
763       OP_CODE_MAPPING(LE_REJECT_CIS_REQUEST)
764       OP_CODE_MAPPING(LE_CREATE_BIG)
765       OP_CODE_MAPPING(LE_TERMINATE_BIG)
766       OP_CODE_MAPPING(LE_BIG_CREATE_SYNC)
767       OP_CODE_MAPPING(LE_BIG_TERMINATE_SYNC)
768       OP_CODE_MAPPING(LE_REQUEST_PEER_SCA)
769       OP_CODE_MAPPING(LE_SETUP_ISO_DATA_PATH)
770       OP_CODE_MAPPING(LE_REMOVE_ISO_DATA_PATH)
771       OP_CODE_MAPPING(LE_SET_HOST_FEATURE)
772       OP_CODE_MAPPING(LE_READ_ISO_LINK_QUALITY)
773       OP_CODE_MAPPING(LE_ENHANCED_READ_TRANSMIT_POWER_LEVEL)
774       OP_CODE_MAPPING(LE_READ_REMOTE_TRANSMIT_POWER_LEVEL)
775       OP_CODE_MAPPING(LE_SET_PATH_LOSS_REPORTING_PARAMETERS)
776       OP_CODE_MAPPING(LE_SET_PATH_LOSS_REPORTING_ENABLE)
777       OP_CODE_MAPPING(LE_SET_TRANSMIT_POWER_REPORTING_ENABLE)
778       OP_CODE_MAPPING(SET_ECOSYSTEM_BASE_INTERVAL)
779       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CODECS_V2)
780       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CODEC_CAPABILITIES)
781       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CONTROLLER_DELAY)
782       OP_CODE_MAPPING(CONFIGURE_DATA_PATH)
783       OP_CODE_MAPPING(ENHANCED_FLUSH)
784 
785       // vendor specific
786       case OpCode::LE_GET_VENDOR_CAPABILITIES:
787         return vendor_capabilities_.is_supported_ == 0x01;
788       case OpCode::LE_MULTI_ADVT:
789         return vendor_capabilities_.max_advt_instances_ != 0x00;
790       case OpCode::LE_BATCH_SCAN:
791         return vendor_capabilities_.total_scan_results_storage_ != 0x00;
792       case OpCode::LE_ADV_FILTER:
793         return vendor_capabilities_.filtering_support_ == 0x01;
794       case OpCode::LE_ENERGY_INFO:
795         return vendor_capabilities_.activity_energy_info_support_ == 0x01;
796       case OpCode::LE_EXTENDED_SCAN_PARAMS:
797         return vendor_capabilities_.extended_scan_support_ == 0x01;
798       case OpCode::CONTROLLER_DEBUG_INFO:
799         return vendor_capabilities_.debug_logging_supported_ == 0x01;
800       case OpCode::CONTROLLER_A2DP_OPCODE:
801         return vendor_capabilities_.a2dp_source_offload_capability_mask_ != 0x00;
802       case OpCode::CONTROLLER_BQR:
803         return vendor_capabilities_.bluetooth_quality_report_support_ == 0x01;
804       // undefined in local_supported_commands_
805       case OpCode::READ_LOCAL_SUPPORTED_COMMANDS:
806         return true;
807       case OpCode::NONE:
808         return false;
809     }
810     return false;
811   }
812 #undef OP_CODE_MAPPING
813 
814   Controller& module_;
815 
816   HciLayer* hci_;
817 
818   CompletedAclPacketsCallback acl_credits_callback_{};
819   CompletedAclPacketsCallback acl_monitor_credits_callback_{};
820   LocalVersionInformation local_version_information_;
821   std::array<uint8_t, 64> local_supported_commands_;
822   uint8_t maximum_page_number_;
823   std::vector<uint64_t> extended_lmp_features_array_;
824   uint16_t acl_buffer_length_ = 0;
825   uint16_t acl_buffers_ = 0;
826   uint8_t sco_buffer_length_ = 0;
827   uint16_t sco_buffers_ = 0;
828   Address mac_address_;
829   std::string local_name_;
830   LeBufferSize le_buffer_size_;
831   LeBufferSize iso_buffer_size_;
832   uint64_t le_local_supported_features_;
833   uint64_t le_supported_states_;
834   uint8_t le_connect_list_size_;
835   uint8_t le_resolving_list_size_;
836   LeMaximumDataLength le_maximum_data_length_;
837   uint16_t le_maximum_advertising_data_length_;
838   uint16_t le_suggested_default_data_length_;
839   uint8_t le_number_supported_advertising_sets_;
840   uint8_t le_periodic_advertiser_list_size_;
841   VendorCapabilities vendor_capabilities_;
842 };  // namespace hci
843 
Controller()844 Controller::Controller() : impl_(std::make_unique<impl>(*this)) {}
845 
846 Controller::~Controller() = default;
847 
RegisterCompletedAclPacketsCallback(CompletedAclPacketsCallback cb)848 void Controller::RegisterCompletedAclPacketsCallback(CompletedAclPacketsCallback cb) {
849   CallOn(impl_.get(), &impl::register_completed_acl_packets_callback, cb);
850 }
851 
UnregisterCompletedAclPacketsCallback()852 void Controller::UnregisterCompletedAclPacketsCallback() {
853   CallOn(impl_.get(), &impl::unregister_completed_acl_packets_callback);
854 }
855 
RegisterCompletedMonitorAclPacketsCallback(CompletedAclPacketsCallback cb)856 void Controller::RegisterCompletedMonitorAclPacketsCallback(CompletedAclPacketsCallback cb) {
857   CallOn(impl_.get(), &impl::register_completed_monitor_acl_packets_callback, cb);
858 }
859 
UnregisterCompletedMonitorAclPacketsCallback()860 void Controller::UnregisterCompletedMonitorAclPacketsCallback() {
861   CallOn(impl_.get(), &impl::unregister_completed_monitor_acl_packets_callback);
862 }
863 
GetLocalName() const864 std::string Controller::GetLocalName() const {
865   return impl_->local_name_;
866 }
867 
GetLocalVersionInformation() const868 LocalVersionInformation Controller::GetLocalVersionInformation() const {
869   return impl_->local_version_information_;
870 }
871 
872 #define BIT(x) (0x1ULL << (x))
873 
874 #define LOCAL_FEATURE_ACCESSOR(name, page, bit) \
875   bool Controller::name() const {               \
876     return GetLocalFeatures(page) & BIT(bit);   \
877   }
878 
879 LOCAL_FEATURE_ACCESSOR(SupportsSimplePairing, 0, 51)
880 LOCAL_FEATURE_ACCESSOR(SupportsSecureConnections, 2, 8)
881 LOCAL_FEATURE_ACCESSOR(SupportsSimultaneousLeBrEdr, 0, 49)
882 LOCAL_FEATURE_ACCESSOR(SupportsInterlacedInquiryScan, 0, 28)
883 LOCAL_FEATURE_ACCESSOR(SupportsRssiWithInquiryResults, 0, 30)
884 LOCAL_FEATURE_ACCESSOR(SupportsExtendedInquiryResponse, 0, 48)
885 LOCAL_FEATURE_ACCESSOR(SupportsRoleSwitch, 0, 5)
886 LOCAL_FEATURE_ACCESSOR(Supports3SlotPackets, 0, 0)
887 LOCAL_FEATURE_ACCESSOR(Supports5SlotPackets, 0, 1)
888 LOCAL_FEATURE_ACCESSOR(SupportsClassic2mPhy, 0, 25)
889 LOCAL_FEATURE_ACCESSOR(SupportsClassic3mPhy, 0, 26)
890 LOCAL_FEATURE_ACCESSOR(Supports3SlotEdrPackets, 0, 39)
891 LOCAL_FEATURE_ACCESSOR(Supports5SlotEdrPackets, 0, 40)
892 LOCAL_FEATURE_ACCESSOR(SupportsSco, 0, 11)
893 LOCAL_FEATURE_ACCESSOR(SupportsHv2Packets, 0, 12)
894 LOCAL_FEATURE_ACCESSOR(SupportsHv3Packets, 0, 13)
895 LOCAL_FEATURE_ACCESSOR(SupportsEv3Packets, 0, 31)
896 LOCAL_FEATURE_ACCESSOR(SupportsEv4Packets, 0, 32)
897 LOCAL_FEATURE_ACCESSOR(SupportsEv5Packets, 0, 33)
898 LOCAL_FEATURE_ACCESSOR(SupportsEsco2mPhy, 0, 45)
899 LOCAL_FEATURE_ACCESSOR(SupportsEsco3mPhy, 0, 46)
900 LOCAL_FEATURE_ACCESSOR(Supports3SlotEscoEdrPackets, 0, 47)
901 LOCAL_FEATURE_ACCESSOR(SupportsHoldMode, 0, 6)
902 LOCAL_FEATURE_ACCESSOR(SupportsSniffMode, 0, 7)
903 LOCAL_FEATURE_ACCESSOR(SupportsParkMode, 0, 8)
904 LOCAL_FEATURE_ACCESSOR(SupportsNonFlushablePb, 0, 54)
905 LOCAL_FEATURE_ACCESSOR(SupportsSniffSubrating, 0, 41)
906 LOCAL_FEATURE_ACCESSOR(SupportsEncryptionPause, 0, 42)
907 LOCAL_FEATURE_ACCESSOR(SupportsBle, 0, 38)
908 
909 #define LOCAL_LE_FEATURE_ACCESSOR(name, bit) \
910   bool Controller::name() const {            \
911     return GetLocalLeFeatures() & BIT(bit);  \
912   }
913 
914 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleConnectionParameterRequest, 1)
915 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleConnectionParametersRequest, 2)
916 LOCAL_LE_FEATURE_ACCESSOR(SupportsBlePeripheralInitiatedFeatureExchange, 3)
917 LOCAL_LE_FEATURE_ACCESSOR(SupportsBlePacketExtension, 5)
918 LOCAL_LE_FEATURE_ACCESSOR(SupportsBlePrivacy, 6)
919 LOCAL_LE_FEATURE_ACCESSOR(SupportsBle2mPhy, 8)
920 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleCodedPhy, 11)
921 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleExtendedAdvertising, 12)
922 LOCAL_LE_FEATURE_ACCESSOR(SupportsBlePeriodicAdvertising, 13)
923 LOCAL_LE_FEATURE_ACCESSOR(SupportsBlePeriodicAdvertisingSyncTransferSender, 24)
924 LOCAL_LE_FEATURE_ACCESSOR(SupportsBlePeriodicAdvertisingSyncTransferRecipient, 25)
925 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleConnectedIsochronousStreamCentral, 28)
926 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleConnectedIsochronousStreamPeripheral, 29)
927 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleIsochronousBroadcaster, 30)
928 LOCAL_LE_FEATURE_ACCESSOR(SupportsBleSynchronizedReceiver, 31)
929 
GetLocalFeatures(uint8_t page_number) const930 uint64_t Controller::GetLocalFeatures(uint8_t page_number) const {
931   if (page_number <= impl_->maximum_page_number_) {
932     return impl_->extended_lmp_features_array_[page_number];
933   }
934   return 0x00;
935 }
936 
GetAclPacketLength() const937 uint16_t Controller::GetAclPacketLength() const {
938   return impl_->acl_buffer_length_;
939 }
940 
GetNumAclPacketBuffers() const941 uint16_t Controller::GetNumAclPacketBuffers() const {
942   return impl_->acl_buffers_;
943 }
944 
GetScoPacketLength() const945 uint8_t Controller::GetScoPacketLength() const {
946   return impl_->sco_buffer_length_;
947 }
948 
GetNumScoPacketBuffers() const949 uint16_t Controller::GetNumScoPacketBuffers() const {
950   return impl_->sco_buffers_;
951 }
952 
GetMacAddress() const953 Address Controller::GetMacAddress() const {
954   return impl_->mac_address_;
955 }
956 
SetEventMask(uint64_t event_mask)957 void Controller::SetEventMask(uint64_t event_mask) {
958   CallOn(impl_.get(), &impl::set_event_mask, event_mask);
959 }
960 
Reset()961 void Controller::Reset() {
962   CallOn(impl_.get(), &impl::reset);
963 }
964 
SetEventFilterClearAll()965 void Controller::SetEventFilterClearAll() {
966   std::unique_ptr<SetEventFilterClearAllBuilder> packet = SetEventFilterClearAllBuilder::Create();
967   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
968 }
969 
SetEventFilterInquiryResultAllDevices()970 void Controller::SetEventFilterInquiryResultAllDevices() {
971   std::unique_ptr<SetEventFilterInquiryResultAllDevicesBuilder> packet =
972       SetEventFilterInquiryResultAllDevicesBuilder::Create();
973   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
974 }
975 
SetEventFilterInquiryResultClassOfDevice(ClassOfDevice class_of_device,ClassOfDevice class_of_device_mask)976 void Controller::SetEventFilterInquiryResultClassOfDevice(ClassOfDevice class_of_device,
977                                                           ClassOfDevice class_of_device_mask) {
978   std::unique_ptr<SetEventFilterInquiryResultClassOfDeviceBuilder> packet =
979       SetEventFilterInquiryResultClassOfDeviceBuilder::Create(class_of_device, class_of_device_mask);
980   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
981 }
982 
SetEventFilterInquiryResultAddress(Address address)983 void Controller::SetEventFilterInquiryResultAddress(Address address) {
984   std::unique_ptr<SetEventFilterInquiryResultAddressBuilder> packet =
985       SetEventFilterInquiryResultAddressBuilder::Create(address);
986   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
987 }
988 
SetEventFilterConnectionSetupAllDevices(AutoAcceptFlag auto_accept_flag)989 void Controller::SetEventFilterConnectionSetupAllDevices(AutoAcceptFlag auto_accept_flag) {
990   std::unique_ptr<SetEventFilterConnectionSetupAllDevicesBuilder> packet =
991       SetEventFilterConnectionSetupAllDevicesBuilder::Create(auto_accept_flag);
992   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
993 }
994 
SetEventFilterConnectionSetupClassOfDevice(ClassOfDevice class_of_device,ClassOfDevice class_of_device_mask,AutoAcceptFlag auto_accept_flag)995 void Controller::SetEventFilterConnectionSetupClassOfDevice(ClassOfDevice class_of_device,
996                                                             ClassOfDevice class_of_device_mask,
997                                                             AutoAcceptFlag auto_accept_flag) {
998   std::unique_ptr<SetEventFilterConnectionSetupClassOfDeviceBuilder> packet =
999       SetEventFilterConnectionSetupClassOfDeviceBuilder::Create(class_of_device, class_of_device_mask,
1000                                                                 auto_accept_flag);
1001   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
1002 }
1003 
SetEventFilterConnectionSetupAddress(Address address,AutoAcceptFlag auto_accept_flag)1004 void Controller::SetEventFilterConnectionSetupAddress(Address address, AutoAcceptFlag auto_accept_flag) {
1005   std::unique_ptr<SetEventFilterConnectionSetupAddressBuilder> packet =
1006       SetEventFilterConnectionSetupAddressBuilder::Create(address, auto_accept_flag);
1007   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
1008 }
1009 
WriteLocalName(std::string local_name)1010 void Controller::WriteLocalName(std::string local_name) {
1011   impl_->local_name_ = local_name;
1012   CallOn(impl_.get(), &impl::write_local_name, local_name);
1013 }
1014 
HostBufferSize(uint16_t host_acl_data_packet_length,uint8_t host_synchronous_data_packet_length,uint16_t host_total_num_acl_data_packets,uint16_t host_total_num_synchronous_data_packets)1015 void Controller::HostBufferSize(uint16_t host_acl_data_packet_length, uint8_t host_synchronous_data_packet_length,
1016                                 uint16_t host_total_num_acl_data_packets,
1017                                 uint16_t host_total_num_synchronous_data_packets) {
1018   CallOn(
1019       impl_.get(),
1020       &impl::host_buffer_size,
1021       host_acl_data_packet_length,
1022       host_synchronous_data_packet_length,
1023       host_total_num_acl_data_packets,
1024       host_total_num_synchronous_data_packets);
1025 }
1026 
LeSetEventMask(uint64_t le_event_mask)1027 void Controller::LeSetEventMask(uint64_t le_event_mask) {
1028   CallOn(impl_.get(), &impl::le_set_event_mask, le_event_mask);
1029 }
1030 
GetLeBufferSize() const1031 LeBufferSize Controller::GetLeBufferSize() const {
1032   return impl_->le_buffer_size_;
1033 }
1034 
GetLocalLeFeatures() const1035 uint64_t Controller::GetLocalLeFeatures() const {
1036   return impl_->le_local_supported_features_;
1037 }
1038 
GetControllerIsoBufferSize() const1039 LeBufferSize Controller::GetControllerIsoBufferSize() const {
1040   return impl_->iso_buffer_size_;
1041 }
1042 
GetControllerLeLocalSupportedFeatures() const1043 uint64_t Controller::GetControllerLeLocalSupportedFeatures() const {
1044   return impl_->le_local_supported_features_;
1045 }
1046 
GetLeSupportedStates() const1047 uint64_t Controller::GetLeSupportedStates() const {
1048   return impl_->le_supported_states_;
1049 }
1050 
GetLeConnectListSize() const1051 uint8_t Controller::GetLeConnectListSize() const {
1052   return impl_->le_connect_list_size_;
1053 }
1054 
GetLeResolvingListSize() const1055 uint8_t Controller::GetLeResolvingListSize() const {
1056   return impl_->le_resolving_list_size_;
1057 }
1058 
GetLeMaximumDataLength() const1059 LeMaximumDataLength Controller::GetLeMaximumDataLength() const {
1060   return impl_->le_maximum_data_length_;
1061 }
1062 
GetLeMaximumAdvertisingDataLength() const1063 uint16_t Controller::GetLeMaximumAdvertisingDataLength() const {
1064   return impl_->le_maximum_advertising_data_length_;
1065 }
1066 
GetLeSuggestedDefaultDataLength() const1067 uint16_t Controller::GetLeSuggestedDefaultDataLength() const {
1068   return impl_->le_suggested_default_data_length_;
1069 }
1070 
GetLeNumberOfSupportedAdverisingSets() const1071 uint8_t Controller::GetLeNumberOfSupportedAdverisingSets() const {
1072   return impl_->le_number_supported_advertising_sets_;
1073 }
1074 
GetVendorCapabilities() const1075 VendorCapabilities Controller::GetVendorCapabilities() const {
1076   return impl_->vendor_capabilities_;
1077 }
1078 
GetLePeriodicAdvertiserListSize() const1079 uint8_t Controller::GetLePeriodicAdvertiserListSize() const {
1080   return impl_->le_periodic_advertiser_list_size_;
1081 }
1082 
IsSupported(bluetooth::hci::OpCode op_code) const1083 bool Controller::IsSupported(bluetooth::hci::OpCode op_code) const {
1084   return impl_->is_supported(op_code);
1085 }
1086 
__anon82be10f40102() 1087 const ModuleFactory Controller::Factory = ModuleFactory([]() { return new Controller(); });
1088 
ListDependencies(ModuleList * list)1089 void Controller::ListDependencies(ModuleList* list) {
1090   list->add<hci::HciLayer>();
1091 }
1092 
Start()1093 void Controller::Start() {
1094   impl_->Start(GetDependency<hci::HciLayer>());
1095 }
1096 
Stop()1097 void Controller::Stop() {
1098   impl_->Stop();
1099 }
1100 
ToString() const1101 std::string Controller::ToString() const {
1102   return "Controller";
1103 }
1104 }  // namespace hci
1105 }  // namespace bluetooth
1106