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 <utility>
22 
23 #include "hci/hci_layer.h"
24 
25 namespace bluetooth {
26 namespace hci {
27 
28 using os::Handler;
29 
30 struct Controller::impl {
implbluetooth::hci::Controller::impl31   impl(Controller& module) : module_(module) {}
32 
Startbluetooth::hci::Controller::impl33   void Start(hci::HciLayer* hci) {
34     hci_ = hci;
35     Handler* handler = module_.GetHandler();
36     hci_->RegisterEventHandler(EventCode::NUMBER_OF_COMPLETED_PACKETS,
37                                handler->BindOn(this, &Controller::impl::NumberOfCompletedPackets));
38 
39     set_event_mask(kDefaultEventMask);
40     write_simple_pairing_mode(Enable::ENABLED);
41     // TODO(b/159927452): Legacy stack set SimultaneousLeHost = 1. Revisit if this causes problem.
42     write_le_host_support(Enable::ENABLED, SimultaneousLeHost::DISABLED);
43     hci_->EnqueueCommand(ReadLocalNameBuilder::Create(),
44                          handler->BindOnceOn(this, &Controller::impl::read_local_name_complete_handler));
45     hci_->EnqueueCommand(ReadLocalVersionInformationBuilder::Create(),
46                          handler->BindOnceOn(this, &Controller::impl::read_local_version_information_complete_handler));
47     hci_->EnqueueCommand(ReadLocalSupportedCommandsBuilder::Create(),
48                          handler->BindOnceOn(this, &Controller::impl::read_local_supported_commands_complete_handler));
49     hci_->EnqueueCommand(ReadLocalSupportedFeaturesBuilder::Create(),
50                          handler->BindOnceOn(this, &Controller::impl::read_local_supported_features_complete_handler));
51 
52     // Wait for all extended features read
53     std::promise<void> features_promise;
54     auto features_future = features_promise.get_future();
55     hci_->EnqueueCommand(ReadLocalExtendedFeaturesBuilder::Create(0x00),
56                          handler->BindOnceOn(this, &Controller::impl::read_local_extended_features_complete_handler,
57                                              std::move(features_promise)));
58     features_future.wait();
59 
60     hci_->EnqueueCommand(ReadBufferSizeBuilder::Create(),
61                          handler->BindOnceOn(this, &Controller::impl::read_buffer_size_complete_handler));
62 
63     hci_->EnqueueCommand(LeReadBufferSizeV1Builder::Create(),
64                          handler->BindOnceOn(this, &Controller::impl::le_read_buffer_size_handler));
65 
66     hci_->EnqueueCommand(LeReadLocalSupportedFeaturesBuilder::Create(),
67                          handler->BindOnceOn(this, &Controller::impl::le_read_local_supported_features_handler));
68 
69     hci_->EnqueueCommand(LeReadSupportedStatesBuilder::Create(),
70                          handler->BindOnceOn(this, &Controller::impl::le_read_supported_states_handler));
71 
72     hci_->EnqueueCommand(
73         LeReadConnectListSizeBuilder::Create(),
74         handler->BindOnceOn(this, &Controller::impl::le_read_connect_list_size_handler));
75 
76     hci_->EnqueueCommand(
77         LeReadResolvingListSizeBuilder::Create(),
78         handler->BindOnceOn(this, &Controller::impl::le_read_resolving_list_size_handler));
79 
80     if (is_supported(OpCode::LE_READ_MAXIMUM_DATA_LENGTH)) {
81       hci_->EnqueueCommand(LeReadMaximumDataLengthBuilder::Create(),
82                            handler->BindOnceOn(this, &Controller::impl::le_read_maximum_data_length_handler));
83     } else {
84       le_maximum_data_length_.supported_max_rx_octets_ = 0;
85       le_maximum_data_length_.supported_max_rx_time_ = 0;
86       le_maximum_data_length_.supported_max_tx_octets_ = 0;
87       le_maximum_data_length_.supported_max_tx_time_ = 0;
88     }
89     if (is_supported(OpCode::LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH)) {
90       hci_->EnqueueCommand(
91           LeReadMaximumAdvertisingDataLengthBuilder::Create(),
92           handler->BindOnceOn(this, &Controller::impl::le_read_maximum_advertising_data_length_handler));
93     } else {
94       le_maximum_advertising_data_length_ = 31;
95     }
96     if (is_supported(OpCode::LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS)) {
97       hci_->EnqueueCommand(
98           LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
99           handler->BindOnceOn(this, &Controller::impl::le_read_number_of_supported_advertising_sets_handler));
100     } else {
101       le_number_supported_advertising_sets_ = 1;
102     }
103 
104     hci_->EnqueueCommand(LeGetVendorCapabilitiesBuilder::Create(),
105                          handler->BindOnceOn(this, &Controller::impl::le_get_vendor_capabilities_handler));
106 
107     // We only need to synchronize the last read. Make BD_ADDR to be the last one.
108     std::promise<void> promise;
109     auto future = promise.get_future();
110     hci_->EnqueueCommand(
111         ReadBdAddrBuilder::Create(),
112         handler->BindOnceOn(this, &Controller::impl::read_controller_mac_address_handler, std::move(promise)));
113     future.wait();
114   }
115 
Stopbluetooth::hci::Controller::impl116   void Stop() {
117     hci_->UnregisterEventHandler(EventCode::NUMBER_OF_COMPLETED_PACKETS);
118     hci_ = nullptr;
119   }
120 
NumberOfCompletedPacketsbluetooth::hci::Controller::impl121   void NumberOfCompletedPackets(EventPacketView event) {
122     if (acl_credits_callback_.IsEmpty()) {
123       LOG_WARN("Received event when AclManager is not listening");
124       return;
125     }
126     auto complete_view = NumberOfCompletedPacketsView::Create(event);
127     ASSERT(complete_view.IsValid());
128     for (auto completed_packets : complete_view.GetCompletedPackets()) {
129       uint16_t handle = completed_packets.connection_handle_;
130       uint16_t credits = completed_packets.host_num_of_completed_packets_;
131       acl_credits_callback_.Invoke(handle, credits);
132     }
133   }
134 
register_completed_acl_packets_callbackbluetooth::hci::Controller::impl135   void register_completed_acl_packets_callback(CompletedAclPacketsCallback callback) {
136     ASSERT(acl_credits_callback_.IsEmpty());
137     acl_credits_callback_ = callback;
138   }
139 
unregister_completed_acl_packets_callbackbluetooth::hci::Controller::impl140   void unregister_completed_acl_packets_callback() {
141     ASSERT(!acl_credits_callback_.IsEmpty());
142     acl_credits_callback_ = {};
143   }
144 
read_local_name_complete_handlerbluetooth::hci::Controller::impl145   void read_local_name_complete_handler(CommandCompleteView view) {
146     auto complete_view = ReadLocalNameCompleteView::Create(view);
147     ASSERT(complete_view.IsValid());
148     ErrorCode status = complete_view.GetStatus();
149     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
150     std::array<uint8_t, 248> local_name_array = complete_view.GetLocalName();
151 
152     local_name_ = std::string(local_name_array.begin(), local_name_array.end());
153     // erase \0
154     local_name_.erase(std::find(local_name_.begin(), local_name_.end(), '\0'), local_name_.end());
155   }
156 
read_local_version_information_complete_handlerbluetooth::hci::Controller::impl157   void read_local_version_information_complete_handler(CommandCompleteView view) {
158     auto complete_view = ReadLocalVersionInformationCompleteView::Create(view);
159     ASSERT(complete_view.IsValid());
160     ErrorCode status = complete_view.GetStatus();
161     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
162 
163     local_version_information_ = complete_view.GetLocalVersionInformation();
164   }
165 
read_local_supported_commands_complete_handlerbluetooth::hci::Controller::impl166   void read_local_supported_commands_complete_handler(CommandCompleteView view) {
167     auto complete_view = ReadLocalSupportedCommandsCompleteView::Create(view);
168     ASSERT(complete_view.IsValid());
169     ErrorCode status = complete_view.GetStatus();
170     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
171     local_supported_commands_ = complete_view.GetSupportedCommands();
172   }
173 
read_local_supported_features_complete_handlerbluetooth::hci::Controller::impl174   void read_local_supported_features_complete_handler(CommandCompleteView view) {
175     auto complete_view = ReadLocalSupportedFeaturesCompleteView::Create(view);
176     ASSERT(complete_view.IsValid());
177     ErrorCode status = complete_view.GetStatus();
178     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
179     local_supported_features_ = complete_view.GetLmpFeatures();
180   }
181 
read_local_extended_features_complete_handlerbluetooth::hci::Controller::impl182   void read_local_extended_features_complete_handler(std::promise<void> promise, CommandCompleteView view) {
183     auto complete_view = ReadLocalExtendedFeaturesCompleteView::Create(view);
184     ASSERT(complete_view.IsValid());
185     ErrorCode status = complete_view.GetStatus();
186     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
187     uint8_t page_number = complete_view.GetPageNumber();
188     maximum_page_number_ = complete_view.GetMaximumPageNumber();
189     extended_lmp_features_array_.push_back(complete_view.GetExtendedLmpFeatures());
190 
191     // Query all extended features
192     if (page_number < maximum_page_number_) {
193       page_number++;
194       hci_->EnqueueCommand(
195           ReadLocalExtendedFeaturesBuilder::Create(page_number),
196           module_.GetHandler()->BindOnceOn(this, &Controller::impl::read_local_extended_features_complete_handler,
197                                            std::move(promise)));
198     } else {
199       promise.set_value();
200     }
201   }
202 
read_buffer_size_complete_handlerbluetooth::hci::Controller::impl203   void read_buffer_size_complete_handler(CommandCompleteView view) {
204     auto complete_view = ReadBufferSizeCompleteView::Create(view);
205     ASSERT(complete_view.IsValid());
206     ErrorCode status = complete_view.GetStatus();
207     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
208     acl_buffer_length_ = complete_view.GetAclDataPacketLength();
209     acl_buffers_ = complete_view.GetTotalNumAclDataPackets();
210 
211     sco_buffer_length_ = complete_view.GetSynchronousDataPacketLength();
212     sco_buffers_ = complete_view.GetTotalNumSynchronousDataPackets();
213   }
214 
read_controller_mac_address_handlerbluetooth::hci::Controller::impl215   void read_controller_mac_address_handler(std::promise<void> promise, CommandCompleteView view) {
216     auto complete_view = ReadBdAddrCompleteView::Create(view);
217     ASSERT(complete_view.IsValid());
218     ErrorCode status = complete_view.GetStatus();
219     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
220     mac_address_ = complete_view.GetBdAddr();
221     promise.set_value();
222   }
223 
le_read_buffer_size_handlerbluetooth::hci::Controller::impl224   void le_read_buffer_size_handler(CommandCompleteView view) {
225     auto complete_view = LeReadBufferSizeV1CompleteView::Create(view);
226     ASSERT(complete_view.IsValid());
227     ErrorCode status = complete_view.GetStatus();
228     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
229     le_buffer_size_ = complete_view.GetLeBufferSize();
230 
231     // If LE buffer size is zero, then buffers returned by Read_Buffer_Size are shared between BR/EDR and LE.
232     if (le_buffer_size_.total_num_le_packets_ == 0) {
233       ASSERT(acl_buffers_ != 0);
234       le_buffer_size_.total_num_le_packets_ = acl_buffers_ / 2;
235       acl_buffers_ -= le_buffer_size_.total_num_le_packets_;
236       le_buffer_size_.le_data_packet_length_ = acl_buffer_length_;
237     }
238   }
239 
le_read_local_supported_features_handlerbluetooth::hci::Controller::impl240   void le_read_local_supported_features_handler(CommandCompleteView view) {
241     auto complete_view = LeReadLocalSupportedFeaturesCompleteView::Create(view);
242     ASSERT(complete_view.IsValid());
243     ErrorCode status = complete_view.GetStatus();
244     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
245     le_local_supported_features_ = complete_view.GetLeFeatures();
246   }
247 
le_read_supported_states_handlerbluetooth::hci::Controller::impl248   void le_read_supported_states_handler(CommandCompleteView view) {
249     auto complete_view = LeReadSupportedStatesCompleteView::Create(view);
250     ASSERT(complete_view.IsValid());
251     ErrorCode status = complete_view.GetStatus();
252     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
253     le_supported_states_ = complete_view.GetLeStates();
254   }
255 
le_read_connect_list_size_handlerbluetooth::hci::Controller::impl256   void le_read_connect_list_size_handler(CommandCompleteView view) {
257     auto complete_view = LeReadConnectListSizeCompleteView::Create(view);
258     ASSERT(complete_view.IsValid());
259     ErrorCode status = complete_view.GetStatus();
260     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
261     le_connect_list_size_ = complete_view.GetConnectListSize();
262   }
263 
le_read_resolving_list_size_handlerbluetooth::hci::Controller::impl264   void le_read_resolving_list_size_handler(CommandCompleteView view) {
265     auto complete_view = LeReadResolvingListSizeCompleteView::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     le_resolving_list_size_ = complete_view.GetResolvingListSize();
270   }
271 
le_read_maximum_data_length_handlerbluetooth::hci::Controller::impl272   void le_read_maximum_data_length_handler(CommandCompleteView view) {
273     auto complete_view = LeReadMaximumDataLengthCompleteView::Create(view);
274     ASSERT(complete_view.IsValid());
275     ErrorCode status = complete_view.GetStatus();
276     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
277     le_maximum_data_length_ = complete_view.GetLeMaximumDataLength();
278   }
279 
le_read_maximum_advertising_data_length_handlerbluetooth::hci::Controller::impl280   void le_read_maximum_advertising_data_length_handler(CommandCompleteView view) {
281     auto complete_view = LeReadMaximumAdvertisingDataLengthCompleteView::Create(view);
282     ASSERT(complete_view.IsValid());
283     ErrorCode status = complete_view.GetStatus();
284     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
285     le_maximum_advertising_data_length_ = complete_view.GetMaximumAdvertisingDataLength();
286   }
287 
le_read_number_of_supported_advertising_sets_handlerbluetooth::hci::Controller::impl288   void le_read_number_of_supported_advertising_sets_handler(CommandCompleteView view) {
289     auto complete_view = LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(view);
290     ASSERT(complete_view.IsValid());
291     ErrorCode status = complete_view.GetStatus();
292     ASSERT_LOG(status == ErrorCode::SUCCESS, "Status 0x%02hhx, %s", status, ErrorCodeText(status).c_str());
293     le_number_supported_advertising_sets_ = complete_view.GetNumberSupportedAdvertisingSets();
294   }
295 
le_get_vendor_capabilities_handlerbluetooth::hci::Controller::impl296   void le_get_vendor_capabilities_handler(CommandCompleteView view) {
297     auto complete_view = LeGetVendorCapabilitiesCompleteView::Create(view);
298 
299     vendor_capabilities_.is_supported_ = 0x00;
300     vendor_capabilities_.max_advt_instances_ = 0x00;
301     vendor_capabilities_.offloaded_resolution_of_private_address_ = 0x00;
302     vendor_capabilities_.total_scan_results_storage_ = 0x00;
303     vendor_capabilities_.max_irk_list_sz_ = 0x00;
304     vendor_capabilities_.filtering_support_ = 0x00;
305     vendor_capabilities_.max_filter_ = 0x00;
306     vendor_capabilities_.activity_energy_info_support_ = 0x00;
307     vendor_capabilities_.version_supported_ = 0x00;
308     vendor_capabilities_.version_supported_ = 0x00;
309     vendor_capabilities_.total_num_of_advt_tracked_ = 0x00;
310     vendor_capabilities_.extended_scan_support_ = 0x00;
311     vendor_capabilities_.debug_logging_supported_ = 0x00;
312     vendor_capabilities_.le_address_generation_offloading_support_ = 0x00;
313     vendor_capabilities_.a2dp_source_offload_capability_mask_ = 0x00;
314     vendor_capabilities_.bluetooth_quality_report_support_ = 0x00;
315 
316     if (complete_view.IsValid()) {
317       vendor_capabilities_.is_supported_ = 0x01;
318 
319       // v0.55
320       BaseVendorCapabilities base_vendor_capabilities = complete_view.GetBaseVendorCapabilities();
321       vendor_capabilities_.max_advt_instances_ = base_vendor_capabilities.max_advt_instances_;
322       vendor_capabilities_.offloaded_resolution_of_private_address_ =
323           base_vendor_capabilities.offloaded_resolution_of_private_address_;
324       vendor_capabilities_.total_scan_results_storage_ = base_vendor_capabilities.total_scan_results_storage_;
325       vendor_capabilities_.max_irk_list_sz_ = base_vendor_capabilities.max_irk_list_sz_;
326       vendor_capabilities_.filtering_support_ = base_vendor_capabilities.filtering_support_;
327       vendor_capabilities_.max_filter_ = base_vendor_capabilities.max_filter_;
328       vendor_capabilities_.activity_energy_info_support_ = base_vendor_capabilities.activity_energy_info_support_;
329       if (complete_view.GetPayload().size() == 0) {
330         vendor_capabilities_.version_supported_ = 55;
331         return;
332       }
333 
334       // v0.95
335       auto v95 = LeGetVendorCapabilitiesComplete095View::Create(complete_view);
336       if (!v95.IsValid()) {
337         LOG_ERROR("invalid data for hci requirements v0.95");
338         return;
339       }
340       vendor_capabilities_.version_supported_ = v95.GetVersionSupported();
341       vendor_capabilities_.total_num_of_advt_tracked_ = v95.GetTotalNumOfAdvtTracked();
342       vendor_capabilities_.extended_scan_support_ = v95.GetExtendedScanSupport();
343       vendor_capabilities_.debug_logging_supported_ = v95.GetDebugLoggingSupported();
344       if (vendor_capabilities_.version_supported_ <= 95 || complete_view.GetPayload().size() == 0) {
345         return;
346       }
347 
348       // v0.96
349       auto v96 = LeGetVendorCapabilitiesComplete096View::Create(v95);
350       if (!v96.IsValid()) {
351         LOG_ERROR("invalid data for hci requirements v0.96");
352         return;
353       }
354       vendor_capabilities_.le_address_generation_offloading_support_ = v96.GetLeAddressGenerationOffloadingSupport();
355       if (vendor_capabilities_.version_supported_ <= 96 || complete_view.GetPayload().size() == 0) {
356         return;
357       }
358 
359       // v0.98
360       auto v98 = LeGetVendorCapabilitiesComplete098View::Create(v96);
361       if (!v98.IsValid()) {
362         LOG_ERROR("invalid data for hci requirements v0.98");
363         return;
364       }
365       vendor_capabilities_.a2dp_source_offload_capability_mask_ = v98.GetA2dpSourceOffloadCapabilityMask();
366       vendor_capabilities_.bluetooth_quality_report_support_ = v98.GetBluetoothQualityReportSupport();
367     }
368   }
369 
set_event_maskbluetooth::hci::Controller::impl370   void set_event_mask(uint64_t event_mask) {
371     std::unique_ptr<SetEventMaskBuilder> packet = SetEventMaskBuilder::Create(event_mask);
372     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
373                                                 this, &Controller::impl::check_status<SetEventMaskCompleteView>));
374   }
375 
write_simple_pairing_modebluetooth::hci::Controller::impl376   void write_simple_pairing_mode(Enable enable) {
377     std::unique_ptr<WriteSimplePairingModeBuilder> packet = WriteSimplePairingModeBuilder::Create(enable);
378     hci_->EnqueueCommand(
379         std::move(packet),
380         module_.GetHandler()->BindOnceOn(this, &Controller::impl::check_status<WriteSimplePairingModeCompleteView>));
381   }
382 
write_le_host_supportbluetooth::hci::Controller::impl383   void write_le_host_support(Enable enable, SimultaneousLeHost simultaneous_le_host) {
384     std::unique_ptr<WriteLeHostSupportBuilder> packet = WriteLeHostSupportBuilder::Create(enable, simultaneous_le_host);
385     hci_->EnqueueCommand(
386         std::move(packet),
387         module_.GetHandler()->BindOnceOn(this, &Controller::impl::check_status<WriteLeHostSupportCompleteView>));
388   }
389 
resetbluetooth::hci::Controller::impl390   void reset() {
391     std::unique_ptr<ResetBuilder> packet = ResetBuilder::Create();
392     hci_->EnqueueCommand(std::move(packet),
393                          module_.GetHandler()->BindOnceOn(this, &Controller::impl::check_status<ResetCompleteView>));
394   }
395 
set_event_filterbluetooth::hci::Controller::impl396   void set_event_filter(std::unique_ptr<SetEventFilterBuilder> packet) {
397     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
398                                                 this, &Controller::impl::check_status<SetEventFilterCompleteView>));
399   }
400 
write_local_namebluetooth::hci::Controller::impl401   void write_local_name(std::string local_name) {
402     ASSERT(local_name.length() <= 248);
403     // Fill remaining char with 0
404     local_name.append(std::string(248 - local_name.length(), '\0'));
405     std::array<uint8_t, 248> local_name_array;
406     std::copy(std::begin(local_name), std::end(local_name), std::begin(local_name_array));
407 
408     std::unique_ptr<WriteLocalNameBuilder> packet = WriteLocalNameBuilder::Create(local_name_array);
409     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
410                                                 this, &Controller::impl::check_status<WriteLocalNameCompleteView>));
411   }
412 
host_buffer_sizebluetooth::hci::Controller::impl413   void host_buffer_size(uint16_t host_acl_data_packet_length, uint8_t host_synchronous_data_packet_length,
414                         uint16_t host_total_num_acl_data_packets, uint16_t host_total_num_synchronous_data_packets) {
415     std::unique_ptr<HostBufferSizeBuilder> packet =
416         HostBufferSizeBuilder::Create(host_acl_data_packet_length, host_synchronous_data_packet_length,
417                                       host_total_num_acl_data_packets, host_total_num_synchronous_data_packets);
418     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
419                                                 this, &Controller::impl::check_status<HostBufferSizeCompleteView>));
420   }
421 
le_set_event_maskbluetooth::hci::Controller::impl422   void le_set_event_mask(uint64_t le_event_mask) {
423     std::unique_ptr<LeSetEventMaskBuilder> packet = LeSetEventMaskBuilder::Create(le_event_mask);
424     hci_->EnqueueCommand(std::move(packet), module_.GetHandler()->BindOnceOn(
425                                                 this, &Controller::impl::check_status<LeSetEventMaskCompleteView>));
426   }
427 
428   template <class T>
check_statusbluetooth::hci::Controller::impl429   void check_status(CommandCompleteView view) {
430     ASSERT(view.IsValid());
431     auto status_view = T::Create(view);
432     ASSERT(status_view.IsValid());
433     ASSERT(status_view.GetStatus() == ErrorCode::SUCCESS);
434   }
435 
436 #define OP_CODE_MAPPING(name)                                                  \
437   case OpCode::name: {                                                         \
438     uint16_t index = (uint16_t)OpCodeIndex::name;                              \
439     uint16_t byte_index = index / 10;                                          \
440     uint16_t bit_index = index % 10;                                           \
441     bool supported = local_supported_commands_[byte_index] & (1 << bit_index); \
442     if (!supported) {                                                          \
443       LOG_WARN("unsupported command opcode: 0x%04x", (uint16_t)OpCode::name);  \
444     }                                                                          \
445     return supported;                                                          \
446   }
447 
is_supportedbluetooth::hci::Controller::impl448   bool is_supported(OpCode op_code) {
449     switch (op_code) {
450       OP_CODE_MAPPING(INQUIRY)
451       OP_CODE_MAPPING(INQUIRY_CANCEL)
452       OP_CODE_MAPPING(PERIODIC_INQUIRY_MODE)
453       OP_CODE_MAPPING(EXIT_PERIODIC_INQUIRY_MODE)
454       OP_CODE_MAPPING(CREATE_CONNECTION)
455       OP_CODE_MAPPING(DISCONNECT)
456       OP_CODE_MAPPING(CREATE_CONNECTION_CANCEL)
457       OP_CODE_MAPPING(ACCEPT_CONNECTION_REQUEST)
458       OP_CODE_MAPPING(REJECT_CONNECTION_REQUEST)
459       OP_CODE_MAPPING(LINK_KEY_REQUEST_REPLY)
460       OP_CODE_MAPPING(LINK_KEY_REQUEST_NEGATIVE_REPLY)
461       OP_CODE_MAPPING(PIN_CODE_REQUEST_REPLY)
462       OP_CODE_MAPPING(PIN_CODE_REQUEST_NEGATIVE_REPLY)
463       OP_CODE_MAPPING(CHANGE_CONNECTION_PACKET_TYPE)
464       OP_CODE_MAPPING(AUTHENTICATION_REQUESTED)
465       OP_CODE_MAPPING(SET_CONNECTION_ENCRYPTION)
466       OP_CODE_MAPPING(CHANGE_CONNECTION_LINK_KEY)
467       OP_CODE_MAPPING(MASTER_LINK_KEY)
468       OP_CODE_MAPPING(REMOTE_NAME_REQUEST)
469       OP_CODE_MAPPING(REMOTE_NAME_REQUEST_CANCEL)
470       OP_CODE_MAPPING(READ_REMOTE_SUPPORTED_FEATURES)
471       OP_CODE_MAPPING(READ_REMOTE_EXTENDED_FEATURES)
472       OP_CODE_MAPPING(READ_REMOTE_VERSION_INFORMATION)
473       OP_CODE_MAPPING(READ_CLOCK_OFFSET)
474       OP_CODE_MAPPING(READ_LMP_HANDLE)
475       OP_CODE_MAPPING(HOLD_MODE)
476       OP_CODE_MAPPING(SNIFF_MODE)
477       OP_CODE_MAPPING(EXIT_SNIFF_MODE)
478       OP_CODE_MAPPING(QOS_SETUP)
479       OP_CODE_MAPPING(ROLE_DISCOVERY)
480       OP_CODE_MAPPING(SWITCH_ROLE)
481       OP_CODE_MAPPING(READ_LINK_POLICY_SETTINGS)
482       OP_CODE_MAPPING(WRITE_LINK_POLICY_SETTINGS)
483       OP_CODE_MAPPING(READ_DEFAULT_LINK_POLICY_SETTINGS)
484       OP_CODE_MAPPING(WRITE_DEFAULT_LINK_POLICY_SETTINGS)
485       OP_CODE_MAPPING(FLOW_SPECIFICATION)
486       OP_CODE_MAPPING(SET_EVENT_MASK)
487       OP_CODE_MAPPING(RESET)
488       OP_CODE_MAPPING(SET_EVENT_FILTER)
489       OP_CODE_MAPPING(FLUSH)
490       OP_CODE_MAPPING(READ_PIN_TYPE)
491       OP_CODE_MAPPING(WRITE_PIN_TYPE)
492       OP_CODE_MAPPING(READ_STORED_LINK_KEY)
493       OP_CODE_MAPPING(WRITE_STORED_LINK_KEY)
494       OP_CODE_MAPPING(DELETE_STORED_LINK_KEY)
495       OP_CODE_MAPPING(WRITE_LOCAL_NAME)
496       OP_CODE_MAPPING(READ_LOCAL_NAME)
497       OP_CODE_MAPPING(READ_CONNECTION_ACCEPT_TIMEOUT)
498       OP_CODE_MAPPING(WRITE_CONNECTION_ACCEPT_TIMEOUT)
499       OP_CODE_MAPPING(READ_PAGE_TIMEOUT)
500       OP_CODE_MAPPING(WRITE_PAGE_TIMEOUT)
501       OP_CODE_MAPPING(READ_SCAN_ENABLE)
502       OP_CODE_MAPPING(WRITE_SCAN_ENABLE)
503       OP_CODE_MAPPING(READ_PAGE_SCAN_ACTIVITY)
504       OP_CODE_MAPPING(WRITE_PAGE_SCAN_ACTIVITY)
505       OP_CODE_MAPPING(READ_INQUIRY_SCAN_ACTIVITY)
506       OP_CODE_MAPPING(WRITE_INQUIRY_SCAN_ACTIVITY)
507       OP_CODE_MAPPING(READ_AUTHENTICATION_ENABLE)
508       OP_CODE_MAPPING(WRITE_AUTHENTICATION_ENABLE)
509       OP_CODE_MAPPING(READ_CLASS_OF_DEVICE)
510       OP_CODE_MAPPING(WRITE_CLASS_OF_DEVICE)
511       OP_CODE_MAPPING(READ_VOICE_SETTING)
512       OP_CODE_MAPPING(WRITE_VOICE_SETTING)
513       OP_CODE_MAPPING(READ_AUTOMATIC_FLUSH_TIMEOUT)
514       OP_CODE_MAPPING(WRITE_AUTOMATIC_FLUSH_TIMEOUT)
515       OP_CODE_MAPPING(READ_NUM_BROADCAST_RETRANSMITS)
516       OP_CODE_MAPPING(WRITE_NUM_BROADCAST_RETRANSMITS)
517       OP_CODE_MAPPING(READ_HOLD_MODE_ACTIVITY)
518       OP_CODE_MAPPING(WRITE_HOLD_MODE_ACTIVITY)
519       OP_CODE_MAPPING(READ_TRANSMIT_POWER_LEVEL)
520       OP_CODE_MAPPING(READ_SYNCHRONOUS_FLOW_CONTROL_ENABLE)
521       OP_CODE_MAPPING(WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)
522       OP_CODE_MAPPING(SET_CONTROLLER_TO_HOST_FLOW_CONTROL)
523       OP_CODE_MAPPING(HOST_BUFFER_SIZE)
524       OP_CODE_MAPPING(HOST_NUM_COMPLETED_PACKETS)
525       OP_CODE_MAPPING(READ_LINK_SUPERVISION_TIMEOUT)
526       OP_CODE_MAPPING(WRITE_LINK_SUPERVISION_TIMEOUT)
527       OP_CODE_MAPPING(READ_NUMBER_OF_SUPPORTED_IAC)
528       OP_CODE_MAPPING(READ_CURRENT_IAC_LAP)
529       OP_CODE_MAPPING(WRITE_CURRENT_IAC_LAP)
530       OP_CODE_MAPPING(SET_AFH_HOST_CHANNEL_CLASSIFICATION)
531       OP_CODE_MAPPING(READ_INQUIRY_SCAN_TYPE)
532       OP_CODE_MAPPING(WRITE_INQUIRY_SCAN_TYPE)
533       OP_CODE_MAPPING(READ_INQUIRY_MODE)
534       OP_CODE_MAPPING(WRITE_INQUIRY_MODE)
535       OP_CODE_MAPPING(READ_PAGE_SCAN_TYPE)
536       OP_CODE_MAPPING(WRITE_PAGE_SCAN_TYPE)
537       OP_CODE_MAPPING(READ_AFH_CHANNEL_ASSESSMENT_MODE)
538       OP_CODE_MAPPING(WRITE_AFH_CHANNEL_ASSESSMENT_MODE)
539       OP_CODE_MAPPING(READ_LOCAL_VERSION_INFORMATION)
540       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_FEATURES)
541       OP_CODE_MAPPING(READ_LOCAL_EXTENDED_FEATURES)
542       OP_CODE_MAPPING(READ_BUFFER_SIZE)
543       OP_CODE_MAPPING(READ_BD_ADDR)
544       OP_CODE_MAPPING(READ_FAILED_CONTACT_COUNTER)
545       OP_CODE_MAPPING(RESET_FAILED_CONTACT_COUNTER)
546       OP_CODE_MAPPING(READ_LINK_QUALITY)
547       OP_CODE_MAPPING(READ_RSSI)
548       OP_CODE_MAPPING(READ_AFH_CHANNEL_MAP)
549       OP_CODE_MAPPING(READ_CLOCK)
550       OP_CODE_MAPPING(READ_LOOPBACK_MODE)
551       OP_CODE_MAPPING(WRITE_LOOPBACK_MODE)
552       OP_CODE_MAPPING(ENABLE_DEVICE_UNDER_TEST_MODE)
553       OP_CODE_MAPPING(SETUP_SYNCHRONOUS_CONNECTION)
554       OP_CODE_MAPPING(ACCEPT_SYNCHRONOUS_CONNECTION)
555       OP_CODE_MAPPING(REJECT_SYNCHRONOUS_CONNECTION)
556       OP_CODE_MAPPING(READ_EXTENDED_INQUIRY_RESPONSE)
557       OP_CODE_MAPPING(WRITE_EXTENDED_INQUIRY_RESPONSE)
558       OP_CODE_MAPPING(REFRESH_ENCRYPTION_KEY)
559       OP_CODE_MAPPING(SNIFF_SUBRATING)
560       OP_CODE_MAPPING(READ_SIMPLE_PAIRING_MODE)
561       OP_CODE_MAPPING(WRITE_SIMPLE_PAIRING_MODE)
562       OP_CODE_MAPPING(READ_LOCAL_OOB_DATA)
563       OP_CODE_MAPPING(READ_INQUIRY_RESPONSE_TRANSMIT_POWER_LEVEL)
564       OP_CODE_MAPPING(WRITE_INQUIRY_TRANSMIT_POWER_LEVEL)
565       OP_CODE_MAPPING(IO_CAPABILITY_REQUEST_REPLY)
566       OP_CODE_MAPPING(USER_CONFIRMATION_REQUEST_REPLY)
567       OP_CODE_MAPPING(USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY)
568       OP_CODE_MAPPING(USER_PASSKEY_REQUEST_REPLY)
569       OP_CODE_MAPPING(USER_PASSKEY_REQUEST_NEGATIVE_REPLY)
570       OP_CODE_MAPPING(REMOTE_OOB_DATA_REQUEST_REPLY)
571       OP_CODE_MAPPING(WRITE_SIMPLE_PAIRING_DEBUG_MODE)
572       OP_CODE_MAPPING(REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY)
573       OP_CODE_MAPPING(SEND_KEYPRESS_NOTIFICATION)
574       OP_CODE_MAPPING(IO_CAPABILITY_REQUEST_NEGATIVE_REPLY)
575       OP_CODE_MAPPING(READ_ENCRYPTION_KEY_SIZE)
576       OP_CODE_MAPPING(READ_DATA_BLOCK_SIZE)
577       OP_CODE_MAPPING(READ_LE_HOST_SUPPORT)
578       OP_CODE_MAPPING(WRITE_LE_HOST_SUPPORT)
579       OP_CODE_MAPPING(LE_SET_EVENT_MASK)
580       OP_CODE_MAPPING(LE_READ_BUFFER_SIZE_V1)
581       OP_CODE_MAPPING(LE_READ_LOCAL_SUPPORTED_FEATURES)
582       OP_CODE_MAPPING(LE_SET_RANDOM_ADDRESS)
583       OP_CODE_MAPPING(LE_SET_ADVERTISING_PARAMETERS)
584       OP_CODE_MAPPING(LE_READ_ADVERTISING_CHANNEL_TX_POWER)
585       OP_CODE_MAPPING(LE_SET_ADVERTISING_DATA)
586       OP_CODE_MAPPING(LE_SET_SCAN_RESPONSE_DATA)
587       OP_CODE_MAPPING(LE_SET_ADVERTISING_ENABLE)
588       OP_CODE_MAPPING(LE_SET_SCAN_PARAMETERS)
589       OP_CODE_MAPPING(LE_SET_SCAN_ENABLE)
590       OP_CODE_MAPPING(LE_CREATE_CONNECTION)
591       OP_CODE_MAPPING(LE_CREATE_CONNECTION_CANCEL)
592       OP_CODE_MAPPING(LE_READ_CONNECT_LIST_SIZE)
593       OP_CODE_MAPPING(LE_CLEAR_CONNECT_LIST)
594       OP_CODE_MAPPING(LE_ADD_DEVICE_TO_CONNECT_LIST)
595       OP_CODE_MAPPING(LE_REMOVE_DEVICE_FROM_CONNECT_LIST)
596       OP_CODE_MAPPING(LE_CONNECTION_UPDATE)
597       OP_CODE_MAPPING(LE_SET_HOST_CHANNEL_CLASSIFICATION)
598       OP_CODE_MAPPING(LE_READ_CHANNEL_MAP)
599       OP_CODE_MAPPING(LE_READ_REMOTE_FEATURES)
600       OP_CODE_MAPPING(LE_ENCRYPT)
601       OP_CODE_MAPPING(LE_RAND)
602       OP_CODE_MAPPING(LE_START_ENCRYPTION)
603       OP_CODE_MAPPING(LE_LONG_TERM_KEY_REQUEST_REPLY)
604       OP_CODE_MAPPING(LE_LONG_TERM_KEY_REQUEST_NEGATIVE_REPLY)
605       OP_CODE_MAPPING(LE_READ_SUPPORTED_STATES)
606       OP_CODE_MAPPING(LE_RECEIVER_TEST)
607       OP_CODE_MAPPING(LE_TRANSMITTER_TEST)
608       OP_CODE_MAPPING(LE_TEST_END)
609       OP_CODE_MAPPING(ENHANCED_SETUP_SYNCHRONOUS_CONNECTION)
610       OP_CODE_MAPPING(ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION)
611       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CODECS_V1)
612       OP_CODE_MAPPING(READ_SECURE_CONNECTIONS_HOST_SUPPORT)
613       OP_CODE_MAPPING(WRITE_SECURE_CONNECTIONS_HOST_SUPPORT)
614       OP_CODE_MAPPING(READ_LOCAL_OOB_EXTENDED_DATA)
615       OP_CODE_MAPPING(WRITE_SECURE_CONNECTIONS_TEST_MODE)
616       OP_CODE_MAPPING(LE_REMOTE_CONNECTION_PARAMETER_REQUEST_REPLY)
617       OP_CODE_MAPPING(LE_REMOTE_CONNECTION_PARAMETER_REQUEST_NEGATIVE_REPLY)
618       OP_CODE_MAPPING(LE_SET_DATA_LENGTH)
619       OP_CODE_MAPPING(LE_READ_SUGGESTED_DEFAULT_DATA_LENGTH)
620       OP_CODE_MAPPING(LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)
621       OP_CODE_MAPPING(LE_READ_LOCAL_P_256_PUBLIC_KEY_COMMAND)
622       OP_CODE_MAPPING(LE_GENERATE_DHKEY_COMMAND_V1)
623       OP_CODE_MAPPING(LE_ADD_DEVICE_TO_RESOLVING_LIST)
624       OP_CODE_MAPPING(LE_REMOVE_DEVICE_FROM_RESOLVING_LIST)
625       OP_CODE_MAPPING(LE_CLEAR_RESOLVING_LIST)
626       OP_CODE_MAPPING(LE_READ_RESOLVING_LIST_SIZE)
627       OP_CODE_MAPPING(LE_READ_PEER_RESOLVABLE_ADDRESS)
628       OP_CODE_MAPPING(LE_READ_LOCAL_RESOLVABLE_ADDRESS)
629       OP_CODE_MAPPING(LE_SET_ADDRESS_RESOLUTION_ENABLE)
630       OP_CODE_MAPPING(LE_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT)
631       OP_CODE_MAPPING(LE_READ_MAXIMUM_DATA_LENGTH)
632       OP_CODE_MAPPING(LE_READ_PHY)
633       OP_CODE_MAPPING(LE_SET_DEFAULT_PHY)
634       OP_CODE_MAPPING(LE_SET_PHY)
635       OP_CODE_MAPPING(LE_ENHANCED_RECEIVER_TEST)
636       OP_CODE_MAPPING(LE_ENHANCED_TRANSMITTER_TEST)
637       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_RANDOM_ADDRESS)
638       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_PARAMETERS)
639       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_DATA)
640       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_SCAN_RESPONSE)
641       OP_CODE_MAPPING(LE_SET_EXTENDED_ADVERTISING_ENABLE)
642       OP_CODE_MAPPING(LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH)
643       OP_CODE_MAPPING(LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS)
644       OP_CODE_MAPPING(LE_REMOVE_ADVERTISING_SET)
645       OP_CODE_MAPPING(LE_CLEAR_ADVERTISING_SETS)
646       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_PARAM)
647       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_DATA)
648       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_ENABLE)
649       OP_CODE_MAPPING(LE_SET_EXTENDED_SCAN_PARAMETERS)
650       OP_CODE_MAPPING(LE_SET_EXTENDED_SCAN_ENABLE)
651       OP_CODE_MAPPING(LE_EXTENDED_CREATE_CONNECTION)
652       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_CREATE_SYNC)
653       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_CREATE_SYNC_CANCEL)
654       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_TERMINATE_SYNC)
655       OP_CODE_MAPPING(LE_ADD_DEVICE_TO_PERIODIC_ADVERTISING_LIST)
656       OP_CODE_MAPPING(LE_REMOVE_DEVICE_FROM_PERIODIC_ADVERTISING_LIST)
657       OP_CODE_MAPPING(LE_CLEAR_PERIODIC_ADVERTISING_LIST)
658       OP_CODE_MAPPING(LE_READ_PERIODIC_ADVERTISING_LIST_SIZE)
659       OP_CODE_MAPPING(LE_READ_TRANSMIT_POWER)
660       OP_CODE_MAPPING(LE_READ_RF_PATH_COMPENSATION_POWER)
661       OP_CODE_MAPPING(LE_WRITE_RF_PATH_COMPENSATION_POWER)
662       OP_CODE_MAPPING(LE_SET_PRIVACY_MODE)
663       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_RECEIVE_ENABLE)
664       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_SYNC_TRANSFER)
665       OP_CODE_MAPPING(LE_PERIODIC_ADVERTISING_SET_INFO_TRANSFER)
666       OP_CODE_MAPPING(LE_SET_PERIODIC_ADVERTISING_SYNC_TRANSFER_PARAMETERS)
667       OP_CODE_MAPPING(LE_SET_DEFAULT_PERIODIC_ADVERTISING_SYNC_TRANSFER_PARAMETERS)
668       OP_CODE_MAPPING(LE_GENERATE_DHKEY_COMMAND)
669       OP_CODE_MAPPING(LE_MODIFY_SLEEP_CLOCK_ACCURACY)
670       OP_CODE_MAPPING(LE_READ_BUFFER_SIZE_V2)
671       OP_CODE_MAPPING(LE_READ_ISO_TX_SYNC)
672       OP_CODE_MAPPING(LE_SET_CIG_PARAMETERS)
673       OP_CODE_MAPPING(LE_CREATE_CIS)
674       OP_CODE_MAPPING(LE_REMOVE_CIG)
675       OP_CODE_MAPPING(LE_ACCEPT_CIS_REQUEST)
676       OP_CODE_MAPPING(LE_REJECT_CIS_REQUEST)
677       OP_CODE_MAPPING(LE_CREATE_BIG)
678       OP_CODE_MAPPING(LE_TERMINATE_BIG)
679       OP_CODE_MAPPING(LE_BIG_CREATE_SYNC)
680       OP_CODE_MAPPING(LE_BIG_TERMINATE_SYNC)
681       OP_CODE_MAPPING(LE_REQUEST_PEER_SCA)
682       OP_CODE_MAPPING(LE_SETUP_ISO_DATA_PATH)
683       OP_CODE_MAPPING(LE_REMOVE_ISO_DATA_PATH)
684       OP_CODE_MAPPING(LE_SET_HOST_FEATURE)
685       OP_CODE_MAPPING(LE_READ_ISO_LINK_QUALITY)
686       OP_CODE_MAPPING(LE_ENHANCED_READ_TRANSMIT_POWER_LEVEL)
687       OP_CODE_MAPPING(LE_READ_REMOTE_TRANSMIT_POWER_LEVEL)
688       OP_CODE_MAPPING(LE_SET_PATH_LOSS_REPORTING_PARAMETERS)
689       OP_CODE_MAPPING(LE_SET_PATH_LOSS_REPORTING_ENABLE)
690       OP_CODE_MAPPING(LE_SET_TRANSMIT_POWER_REPORTING_ENABLE)
691       OP_CODE_MAPPING(SET_ECOSYSTEM_BASE_INTERVAL)
692       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CODECS_V2)
693       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CODEC_CAPABILITIES)
694       OP_CODE_MAPPING(READ_LOCAL_SUPPORTED_CONTROLLER_DELAY)
695       OP_CODE_MAPPING(CONFIGURE_DATA_PATH)
696 
697       // vendor specific
698       case OpCode::LE_GET_VENDOR_CAPABILITIES:
699         return vendor_capabilities_.is_supported_ == 0x01;
700       case OpCode::LE_MULTI_ADVT:
701         return vendor_capabilities_.max_advt_instances_ != 0x00;
702       case OpCode::LE_BATCH_SCAN:
703         return vendor_capabilities_.total_scan_results_storage_ != 0x00;
704       case OpCode::LE_ADV_FILTER:
705         return vendor_capabilities_.filtering_support_ == 0x01;
706       case OpCode::LE_TRACK_ADV:
707         return vendor_capabilities_.total_num_of_advt_tracked_ > 0;
708       case OpCode::LE_ENERGY_INFO:
709         return vendor_capabilities_.activity_energy_info_support_ == 0x01;
710       case OpCode::LE_EXTENDED_SCAN_PARAMS:
711         return vendor_capabilities_.extended_scan_support_ == 0x01;
712       case OpCode::CONTROLLER_DEBUG_INFO:
713         return vendor_capabilities_.debug_logging_supported_ == 0x01;
714       case OpCode::CONTROLLER_A2DP_OPCODE:
715         return vendor_capabilities_.a2dp_source_offload_capability_mask_ != 0x00;
716       case OpCode::CONTROLLER_BQR:
717         return vendor_capabilities_.bluetooth_quality_report_support_ == 0x01;
718       // undefined in local_supported_commands_
719       case OpCode::CREATE_NEW_UNIT_KEY:
720       case OpCode::READ_LOCAL_SUPPORTED_COMMANDS:
721         return true;
722       case OpCode::NONE:
723         return false;
724     }
725     return false;
726   }
727 #undef OP_CODE_MAPPING
728 
729   Controller& module_;
730 
731   HciLayer* hci_;
732 
733   CompletedAclPacketsCallback acl_credits_callback_{};
734   LocalVersionInformation local_version_information_;
735   std::array<uint8_t, 64> local_supported_commands_;
736   uint64_t local_supported_features_;
737   uint8_t maximum_page_number_;
738   std::vector<uint64_t> extended_lmp_features_array_;
739   uint16_t acl_buffer_length_ = 0;
740   uint16_t acl_buffers_ = 0;
741   uint8_t sco_buffer_length_ = 0;
742   uint16_t sco_buffers_ = 0;
743   Address mac_address_;
744   std::string local_name_;
745   LeBufferSize le_buffer_size_;
746   uint64_t le_local_supported_features_;
747   uint64_t le_supported_states_;
748   uint8_t le_connect_list_size_;
749   uint8_t le_resolving_list_size_;
750   LeMaximumDataLength le_maximum_data_length_;
751   uint16_t le_maximum_advertising_data_length_;
752   uint8_t le_number_supported_advertising_sets_;
753   VendorCapabilities vendor_capabilities_;
754 };  // namespace hci
755 
Controller()756 Controller::Controller() : impl_(std::make_unique<impl>(*this)) {}
757 
758 Controller::~Controller() = default;
759 
RegisterCompletedAclPacketsCallback(CompletedAclPacketsCallback cb)760 void Controller::RegisterCompletedAclPacketsCallback(CompletedAclPacketsCallback cb) {
761   CallOn(impl_.get(), &impl::register_completed_acl_packets_callback, cb);
762 }
763 
UnregisterCompletedAclPacketsCallback()764 void Controller::UnregisterCompletedAclPacketsCallback() {
765   CallOn(impl_.get(), &impl::unregister_completed_acl_packets_callback);
766 }
767 
GetControllerLocalName() const768 std::string Controller::GetControllerLocalName() const {
769   return impl_->local_name_;
770 }
771 
GetControllerLocalVersionInformation() const772 LocalVersionInformation Controller::GetControllerLocalVersionInformation() const {
773   return impl_->local_version_information_;
774 }
775 
GetControllerLocalSupportedCommands() const776 std::array<uint8_t, 64> Controller::GetControllerLocalSupportedCommands() const {
777   return impl_->local_supported_commands_;
778 }
779 
GetControllerLocalExtendedFeaturesMaxPageNumber() const780 uint8_t Controller::GetControllerLocalExtendedFeaturesMaxPageNumber() const {
781   return impl_->maximum_page_number_;
782 }
783 
GetControllerLocalSupportedFeatures() const784 uint64_t Controller::GetControllerLocalSupportedFeatures() const {
785   return impl_->local_supported_features_;
786 }
787 
GetControllerLocalExtendedFeatures(uint8_t page_number) const788 uint64_t Controller::GetControllerLocalExtendedFeatures(uint8_t page_number) const {
789   if (page_number <= impl_->maximum_page_number_) {
790     return impl_->extended_lmp_features_array_[page_number];
791   }
792   return 0x00;
793 }
794 
GetControllerAclPacketLength() const795 uint16_t Controller::GetControllerAclPacketLength() const {
796   return impl_->acl_buffer_length_;
797 }
798 
GetControllerNumAclPacketBuffers() const799 uint16_t Controller::GetControllerNumAclPacketBuffers() const {
800   return impl_->acl_buffers_;
801 }
802 
GetControllerScoPacketLength() const803 uint8_t Controller::GetControllerScoPacketLength() const {
804   return impl_->sco_buffer_length_;
805 }
806 
GetControllerNumScoPacketBuffers() const807 uint16_t Controller::GetControllerNumScoPacketBuffers() const {
808   return impl_->sco_buffers_;
809 }
810 
GetControllerMacAddress() const811 Address Controller::GetControllerMacAddress() const {
812   return impl_->mac_address_;
813 }
814 
SetEventMask(uint64_t event_mask)815 void Controller::SetEventMask(uint64_t event_mask) {
816   CallOn(impl_.get(), &impl::set_event_mask, event_mask);
817 }
818 
Reset()819 void Controller::Reset() {
820   CallOn(impl_.get(), &impl::reset);
821 }
822 
SetEventFilterClearAll()823 void Controller::SetEventFilterClearAll() {
824   std::unique_ptr<SetEventFilterClearAllBuilder> packet = SetEventFilterClearAllBuilder::Create();
825   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
826 }
827 
SetEventFilterInquiryResultAllDevices()828 void Controller::SetEventFilterInquiryResultAllDevices() {
829   std::unique_ptr<SetEventFilterInquiryResultAllDevicesBuilder> packet =
830       SetEventFilterInquiryResultAllDevicesBuilder::Create();
831   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
832 }
833 
SetEventFilterInquiryResultClassOfDevice(ClassOfDevice class_of_device,ClassOfDevice class_of_device_mask)834 void Controller::SetEventFilterInquiryResultClassOfDevice(ClassOfDevice class_of_device,
835                                                           ClassOfDevice class_of_device_mask) {
836   std::unique_ptr<SetEventFilterInquiryResultClassOfDeviceBuilder> packet =
837       SetEventFilterInquiryResultClassOfDeviceBuilder::Create(class_of_device, class_of_device_mask);
838   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
839 }
840 
SetEventFilterInquiryResultAddress(Address address)841 void Controller::SetEventFilterInquiryResultAddress(Address address) {
842   std::unique_ptr<SetEventFilterInquiryResultAddressBuilder> packet =
843       SetEventFilterInquiryResultAddressBuilder::Create(address);
844   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
845 }
846 
SetEventFilterConnectionSetupAllDevices(AutoAcceptFlag auto_accept_flag)847 void Controller::SetEventFilterConnectionSetupAllDevices(AutoAcceptFlag auto_accept_flag) {
848   std::unique_ptr<SetEventFilterConnectionSetupAllDevicesBuilder> packet =
849       SetEventFilterConnectionSetupAllDevicesBuilder::Create(auto_accept_flag);
850   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
851 }
852 
SetEventFilterConnectionSetupClassOfDevice(ClassOfDevice class_of_device,ClassOfDevice class_of_device_mask,AutoAcceptFlag auto_accept_flag)853 void Controller::SetEventFilterConnectionSetupClassOfDevice(ClassOfDevice class_of_device,
854                                                             ClassOfDevice class_of_device_mask,
855                                                             AutoAcceptFlag auto_accept_flag) {
856   std::unique_ptr<SetEventFilterConnectionSetupClassOfDeviceBuilder> packet =
857       SetEventFilterConnectionSetupClassOfDeviceBuilder::Create(class_of_device, class_of_device_mask,
858                                                                 auto_accept_flag);
859   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
860 }
861 
SetEventFilterConnectionSetupAddress(Address address,AutoAcceptFlag auto_accept_flag)862 void Controller::SetEventFilterConnectionSetupAddress(Address address, AutoAcceptFlag auto_accept_flag) {
863   std::unique_ptr<SetEventFilterConnectionSetupAddressBuilder> packet =
864       SetEventFilterConnectionSetupAddressBuilder::Create(address, auto_accept_flag);
865   CallOn(impl_.get(), &impl::set_event_filter, std::move(packet));
866 }
867 
WriteLocalName(std::string local_name)868 void Controller::WriteLocalName(std::string local_name) {
869   impl_->local_name_ = local_name;
870   CallOn(impl_.get(), &impl::write_local_name, local_name);
871 }
872 
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)873 void Controller::HostBufferSize(uint16_t host_acl_data_packet_length, uint8_t host_synchronous_data_packet_length,
874                                 uint16_t host_total_num_acl_data_packets,
875                                 uint16_t host_total_num_synchronous_data_packets) {
876   CallOn(
877       impl_.get(),
878       &impl::host_buffer_size,
879       host_acl_data_packet_length,
880       host_synchronous_data_packet_length,
881       host_total_num_acl_data_packets,
882       host_total_num_synchronous_data_packets);
883 }
884 
LeSetEventMask(uint64_t le_event_mask)885 void Controller::LeSetEventMask(uint64_t le_event_mask) {
886   CallOn(impl_.get(), &impl::le_set_event_mask, le_event_mask);
887 }
888 
GetControllerLeBufferSize() const889 LeBufferSize Controller::GetControllerLeBufferSize() const {
890   return impl_->le_buffer_size_;
891 }
892 
GetControllerLeLocalSupportedFeatures() const893 uint64_t Controller::GetControllerLeLocalSupportedFeatures() const {
894   return impl_->le_local_supported_features_;
895 }
896 
GetControllerLeSupportedStates() const897 uint64_t Controller::GetControllerLeSupportedStates() const {
898   return impl_->le_supported_states_;
899 }
900 
GetControllerLeConnectListSize() const901 uint8_t Controller::GetControllerLeConnectListSize() const {
902   return impl_->le_connect_list_size_;
903 }
904 
GetControllerLeResolvingListSize() const905 uint8_t Controller::GetControllerLeResolvingListSize() const {
906   return impl_->le_resolving_list_size_;
907 }
908 
GetControllerLeMaximumDataLength() const909 LeMaximumDataLength Controller::GetControllerLeMaximumDataLength() const {
910   return impl_->le_maximum_data_length_;
911 }
912 
GetControllerLeMaximumAdvertisingDataLength() const913 uint16_t Controller::GetControllerLeMaximumAdvertisingDataLength() const {
914   return impl_->le_maximum_advertising_data_length_;
915 }
916 
GetControllerLeNumberOfSupportedAdverisingSets() const917 uint8_t Controller::GetControllerLeNumberOfSupportedAdverisingSets() const {
918   return impl_->le_number_supported_advertising_sets_;
919 }
920 
GetControllerVendorCapabilities() const921 VendorCapabilities Controller::GetControllerVendorCapabilities() const {
922   return impl_->vendor_capabilities_;
923 }
924 
IsSupported(bluetooth::hci::OpCode op_code) const925 bool Controller::IsSupported(bluetooth::hci::OpCode op_code) const {
926   return impl_->is_supported(op_code);
927 }
928 
__anonbb2f65cd0102() 929 const ModuleFactory Controller::Factory = ModuleFactory([]() { return new Controller(); });
930 
ListDependencies(ModuleList * list)931 void Controller::ListDependencies(ModuleList* list) {
932   list->add<hci::HciLayer>();
933 }
934 
Start()935 void Controller::Start() {
936   impl_->Start(GetDependency<hci::HciLayer>());
937 }
938 
Stop()939 void Controller::Stop() {
940   impl_->Stop();
941 }
942 
ToString() const943 std::string Controller::ToString() const {
944   return "Controller";
945 }
946 }  // namespace hci
947 }  // namespace bluetooth
948