1 /*
2 * Copyright 2015 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 "dual_mode_controller.h"
18
19 #include <memory>
20
21 #include <base/files/file_util.h>
22 #include <base/json/json_reader.h>
23 #include <base/values.h>
24
25 #include "os/log.h"
26 #include "packet/raw_builder.h"
27
28 namespace gd_hci = ::bluetooth::hci;
29 using gd_hci::ErrorCode;
30 using gd_hci::LoopbackMode;
31 using gd_hci::OpCode;
32 using std::vector;
33
34 namespace test_vendor_lib {
35 constexpr char DualModeController::kControllerPropertiesFile[];
36 constexpr uint16_t DualModeController::kSecurityManagerNumKeys;
37 constexpr uint16_t kNumCommandPackets = 0x01;
38 constexpr uint16_t kLeMaximumAdvertisingDataLength = 256;
39 constexpr uint16_t kLeMaximumDataLength = 64;
40 constexpr uint16_t kLeMaximumDataTime = 0x148;
41
42 // Device methods.
Initialize(const std::vector<std::string> & args)43 void DualModeController::Initialize(const std::vector<std::string>& args) {
44 if (args.size() < 2) return;
45
46 Address addr{};
47 if (Address::FromString(args[1], addr)) {
48 properties_.SetAddress(addr);
49 } else {
50 LOG_ALWAYS_FATAL("Invalid address: %s", args[1].c_str());
51 }
52 };
53
GetTypeString() const54 std::string DualModeController::GetTypeString() const {
55 return "Simulated Bluetooth Controller";
56 }
57
IncomingPacket(model::packets::LinkLayerPacketView incoming)58 void DualModeController::IncomingPacket(
59 model::packets::LinkLayerPacketView incoming) {
60 link_layer_controller_.IncomingPacket(incoming);
61 }
62
TimerTick()63 void DualModeController::TimerTick() {
64 link_layer_controller_.TimerTick();
65 }
66
SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const67 void DualModeController::SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const {
68 std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
69 std::make_unique<bluetooth::packet::RawBuilder>();
70 raw_builder_ptr->AddOctets1(kNumCommandPackets);
71 raw_builder_ptr->AddOctets2(command_opcode);
72 raw_builder_ptr->AddOctets1(
73 static_cast<uint8_t>(ErrorCode::UNKNOWN_HCI_COMMAND));
74
75 auto packet = gd_hci::EventPacketBuilder::Create(
76 gd_hci::EventCode::COMMAND_COMPLETE, std::move(raw_builder_ptr));
77 send_event_(std::move(packet));
78 }
79
DualModeController(const std::string & properties_filename,uint16_t num_keys)80 DualModeController::DualModeController(const std::string& properties_filename, uint16_t num_keys)
81 : Device(properties_filename), security_manager_(num_keys) {
82 loopback_mode_ = LoopbackMode::NO_LOOPBACK;
83
84 Address public_address{};
85 ASSERT(Address::FromString("3C:5A:B4:04:05:06", public_address));
86 properties_.SetAddress(public_address);
87
88 link_layer_controller_.RegisterRemoteChannel(
89 [this](std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
90 Phy::Type phy_type) {
91 DualModeController::SendLinkLayerPacket(packet, phy_type);
92 });
93
94 #define SET_HANDLER(opcode, method) \
95 active_hci_commands_[opcode] = [this](CommandPacketView param) { \
96 method(param); \
97 };
98 SET_HANDLER(OpCode::RESET, Reset);
99 SET_HANDLER(OpCode::READ_BUFFER_SIZE, ReadBufferSize);
100 SET_HANDLER(OpCode::HOST_BUFFER_SIZE, HostBufferSize);
101 SET_HANDLER(OpCode::SNIFF_SUBRATING, SniffSubrating);
102 SET_HANDLER(OpCode::READ_ENCRYPTION_KEY_SIZE, ReadEncryptionKeySize);
103 SET_HANDLER(OpCode::READ_LOCAL_VERSION_INFORMATION,
104 ReadLocalVersionInformation);
105 SET_HANDLER(OpCode::READ_BD_ADDR, ReadBdAddr);
106 SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_COMMANDS,
107 ReadLocalSupportedCommands);
108 SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_FEATURES,
109 ReadLocalSupportedFeatures);
110 SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_CODECS_V1, ReadLocalSupportedCodecs);
111 SET_HANDLER(OpCode::READ_LOCAL_EXTENDED_FEATURES, ReadLocalExtendedFeatures);
112 SET_HANDLER(OpCode::READ_REMOTE_EXTENDED_FEATURES,
113 ReadRemoteExtendedFeatures);
114 SET_HANDLER(OpCode::SWITCH_ROLE, SwitchRole);
115 SET_HANDLER(OpCode::READ_REMOTE_SUPPORTED_FEATURES,
116 ReadRemoteSupportedFeatures);
117 SET_HANDLER(OpCode::READ_CLOCK_OFFSET, ReadClockOffset);
118 SET_HANDLER(OpCode::IO_CAPABILITY_REQUEST_REPLY, IoCapabilityRequestReply);
119 SET_HANDLER(OpCode::USER_CONFIRMATION_REQUEST_REPLY,
120 UserConfirmationRequestReply);
121 SET_HANDLER(OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY,
122 UserConfirmationRequestNegativeReply);
123 SET_HANDLER(OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY,
124 IoCapabilityRequestNegativeReply);
125 SET_HANDLER(OpCode::READ_INQUIRY_RESPONSE_TRANSMIT_POWER_LEVEL,
126 ReadInquiryResponseTransmitPowerLevel);
127 SET_HANDLER(OpCode::WRITE_SIMPLE_PAIRING_MODE, WriteSimplePairingMode);
128 SET_HANDLER(OpCode::WRITE_LE_HOST_SUPPORT, WriteLeHostSupport);
129 SET_HANDLER(OpCode::WRITE_SECURE_CONNECTIONS_HOST_SUPPORT,
130 WriteSecureConnectionsHostSupport);
131 SET_HANDLER(OpCode::SET_EVENT_MASK, SetEventMask);
132 SET_HANDLER(OpCode::READ_INQUIRY_MODE, ReadInquiryMode);
133 SET_HANDLER(OpCode::WRITE_INQUIRY_MODE, WriteInquiryMode);
134 SET_HANDLER(OpCode::READ_PAGE_SCAN_TYPE, ReadPageScanType);
135 SET_HANDLER(OpCode::WRITE_PAGE_SCAN_TYPE, WritePageScanType);
136 SET_HANDLER(OpCode::WRITE_INQUIRY_SCAN_TYPE, WriteInquiryScanType);
137 SET_HANDLER(OpCode::READ_INQUIRY_SCAN_TYPE, ReadInquiryScanType);
138 SET_HANDLER(OpCode::AUTHENTICATION_REQUESTED, AuthenticationRequested);
139 SET_HANDLER(OpCode::SET_CONNECTION_ENCRYPTION, SetConnectionEncryption);
140 SET_HANDLER(OpCode::CHANGE_CONNECTION_LINK_KEY, ChangeConnectionLinkKey);
141 SET_HANDLER(OpCode::MASTER_LINK_KEY, MasterLinkKey);
142 SET_HANDLER(OpCode::WRITE_AUTHENTICATION_ENABLE, WriteAuthenticationEnable);
143 SET_HANDLER(OpCode::READ_AUTHENTICATION_ENABLE, ReadAuthenticationEnable);
144 SET_HANDLER(OpCode::WRITE_CLASS_OF_DEVICE, WriteClassOfDevice);
145 SET_HANDLER(OpCode::READ_PAGE_TIMEOUT, ReadPageTimeout);
146 SET_HANDLER(OpCode::WRITE_PAGE_TIMEOUT, WritePageTimeout);
147 SET_HANDLER(OpCode::WRITE_LINK_SUPERVISION_TIMEOUT,
148 WriteLinkSupervisionTimeout);
149 SET_HANDLER(OpCode::HOLD_MODE, HoldMode);
150 SET_HANDLER(OpCode::SNIFF_MODE, SniffMode);
151 SET_HANDLER(OpCode::EXIT_SNIFF_MODE, ExitSniffMode);
152 SET_HANDLER(OpCode::QOS_SETUP, QosSetup);
153 SET_HANDLER(OpCode::READ_DEFAULT_LINK_POLICY_SETTINGS,
154 ReadDefaultLinkPolicySettings);
155 SET_HANDLER(OpCode::WRITE_DEFAULT_LINK_POLICY_SETTINGS,
156 WriteDefaultLinkPolicySettings);
157 SET_HANDLER(OpCode::FLOW_SPECIFICATION, FlowSpecification);
158 SET_HANDLER(OpCode::WRITE_LINK_POLICY_SETTINGS, WriteLinkPolicySettings);
159 SET_HANDLER(OpCode::CHANGE_CONNECTION_PACKET_TYPE,
160 ChangeConnectionPacketType);
161 SET_HANDLER(OpCode::WRITE_LOCAL_NAME, WriteLocalName);
162 SET_HANDLER(OpCode::READ_LOCAL_NAME, ReadLocalName);
163 SET_HANDLER(OpCode::WRITE_EXTENDED_INQUIRY_RESPONSE,
164 WriteExtendedInquiryResponse);
165 SET_HANDLER(OpCode::REFRESH_ENCRYPTION_KEY, RefreshEncryptionKey);
166 SET_HANDLER(OpCode::WRITE_VOICE_SETTING, WriteVoiceSetting);
167 SET_HANDLER(OpCode::READ_NUMBER_OF_SUPPORTED_IAC, ReadNumberOfSupportedIac);
168 SET_HANDLER(OpCode::READ_CURRENT_IAC_LAP, ReadCurrentIacLap);
169 SET_HANDLER(OpCode::WRITE_CURRENT_IAC_LAP, WriteCurrentIacLap);
170 SET_HANDLER(OpCode::READ_PAGE_SCAN_ACTIVITY, ReadPageScanActivity);
171 SET_HANDLER(OpCode::WRITE_PAGE_SCAN_ACTIVITY, WritePageScanActivity);
172 SET_HANDLER(OpCode::READ_INQUIRY_SCAN_ACTIVITY, ReadInquiryScanActivity);
173 SET_HANDLER(OpCode::WRITE_INQUIRY_SCAN_ACTIVITY, WriteInquiryScanActivity);
174 SET_HANDLER(OpCode::READ_SCAN_ENABLE, ReadScanEnable);
175 SET_HANDLER(OpCode::WRITE_SCAN_ENABLE, WriteScanEnable);
176 SET_HANDLER(OpCode::SET_EVENT_FILTER, SetEventFilter);
177 SET_HANDLER(OpCode::INQUIRY, Inquiry);
178 SET_HANDLER(OpCode::INQUIRY_CANCEL, InquiryCancel);
179 SET_HANDLER(OpCode::ACCEPT_CONNECTION_REQUEST, AcceptConnectionRequest);
180 SET_HANDLER(OpCode::REJECT_CONNECTION_REQUEST, RejectConnectionRequest);
181 SET_HANDLER(OpCode::LINK_KEY_REQUEST_REPLY, LinkKeyRequestReply);
182 SET_HANDLER(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY,
183 LinkKeyRequestNegativeReply);
184 SET_HANDLER(OpCode::DELETE_STORED_LINK_KEY, DeleteStoredLinkKey);
185 SET_HANDLER(OpCode::REMOTE_NAME_REQUEST, RemoteNameRequest);
186 SET_HANDLER(OpCode::LE_SET_EVENT_MASK, LeSetEventMask);
187 SET_HANDLER(OpCode::LE_READ_BUFFER_SIZE_V1, LeReadBufferSize);
188 SET_HANDLER(OpCode::LE_READ_LOCAL_SUPPORTED_FEATURES,
189 LeReadLocalSupportedFeatures);
190 SET_HANDLER(OpCode::LE_SET_RANDOM_ADDRESS, LeSetRandomAddress);
191 SET_HANDLER(OpCode::LE_SET_ADVERTISING_PARAMETERS,
192 LeSetAdvertisingParameters);
193 SET_HANDLER(OpCode::LE_SET_ADVERTISING_DATA, LeSetAdvertisingData);
194 SET_HANDLER(OpCode::LE_SET_SCAN_RESPONSE_DATA, LeSetScanResponseData);
195 SET_HANDLER(OpCode::LE_SET_ADVERTISING_ENABLE, LeSetAdvertisingEnable);
196 SET_HANDLER(OpCode::LE_SET_SCAN_PARAMETERS, LeSetScanParameters);
197 SET_HANDLER(OpCode::LE_SET_SCAN_ENABLE, LeSetScanEnable);
198 SET_HANDLER(OpCode::LE_CREATE_CONNECTION, LeCreateConnection);
199 SET_HANDLER(OpCode::CREATE_CONNECTION, CreateConnection);
200 SET_HANDLER(OpCode::DISCONNECT, Disconnect);
201 SET_HANDLER(OpCode::LE_CREATE_CONNECTION_CANCEL, LeConnectionCancel);
202 SET_HANDLER(OpCode::LE_READ_CONNECT_LIST_SIZE, LeReadConnectListSize);
203 SET_HANDLER(OpCode::LE_CLEAR_CONNECT_LIST, LeClearConnectList);
204 SET_HANDLER(OpCode::LE_ADD_DEVICE_TO_CONNECT_LIST, LeAddDeviceToConnectList);
205 SET_HANDLER(OpCode::LE_REMOVE_DEVICE_FROM_CONNECT_LIST,
206 LeRemoveDeviceFromConnectList);
207 SET_HANDLER(OpCode::LE_RAND, LeRand);
208 SET_HANDLER(OpCode::LE_READ_SUPPORTED_STATES, LeReadSupportedStates);
209 SET_HANDLER(OpCode::LE_GET_VENDOR_CAPABILITIES, LeVendorCap);
210 SET_HANDLER(OpCode::LE_MULTI_ADVT, LeVendorMultiAdv);
211 SET_HANDLER(OpCode::LE_ADV_FILTER, LeAdvertisingFilter);
212 SET_HANDLER(OpCode::LE_ENERGY_INFO, LeEnergyInfo);
213 SET_HANDLER(OpCode::LE_SET_EXTENDED_ADVERTISING_RANDOM_ADDRESS,
214 LeSetExtendedAdvertisingRandomAddress);
215 SET_HANDLER(OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
216 LeSetExtendedAdvertisingParameters);
217 SET_HANDLER(OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
218 LeSetExtendedAdvertisingData);
219 SET_HANDLER(OpCode::LE_SET_EXTENDED_ADVERTISING_SCAN_RESPONSE,
220 LeSetExtendedAdvertisingScanResponse);
221 SET_HANDLER(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
222 LeSetExtendedAdvertisingEnable);
223 SET_HANDLER(OpCode::LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH,
224 LeReadMaximumAdvertisingDataLength);
225 SET_HANDLER(OpCode::LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS,
226 LeReadNumberOfSupportedAdvertisingSets);
227 SET_HANDLER(OpCode::LE_REMOVE_ADVERTISING_SET, LeRemoveAdvertisingSet);
228 SET_HANDLER(OpCode::LE_CLEAR_ADVERTISING_SETS, LeClearAdvertisingSets);
229 SET_HANDLER(OpCode::LE_READ_REMOTE_FEATURES, LeReadRemoteFeatures);
230 SET_HANDLER(OpCode::READ_REMOTE_VERSION_INFORMATION,
231 ReadRemoteVersionInformation);
232 SET_HANDLER(OpCode::LE_CONNECTION_UPDATE, LeConnectionUpdate);
233 SET_HANDLER(OpCode::LE_START_ENCRYPTION, LeStartEncryption);
234 SET_HANDLER(OpCode::LE_ADD_DEVICE_TO_RESOLVING_LIST,
235 LeAddDeviceToResolvingList);
236 SET_HANDLER(OpCode::LE_REMOVE_DEVICE_FROM_RESOLVING_LIST,
237 LeRemoveDeviceFromResolvingList);
238 SET_HANDLER(OpCode::LE_CLEAR_RESOLVING_LIST, LeClearResolvingList);
239 SET_HANDLER(OpCode::LE_READ_RESOLVING_LIST_SIZE, LeReadResolvingListSize);
240 SET_HANDLER(OpCode::LE_READ_MAXIMUM_DATA_LENGTH, LeReadMaximumDataLength);
241 SET_HANDLER(OpCode::LE_SET_EXTENDED_SCAN_PARAMETERS,
242 LeSetExtendedScanParameters);
243 SET_HANDLER(OpCode::LE_SET_EXTENDED_SCAN_ENABLE, LeSetExtendedScanEnable);
244 SET_HANDLER(OpCode::LE_EXTENDED_CREATE_CONNECTION,
245 LeExtendedCreateConnection);
246 SET_HANDLER(OpCode::LE_SET_PRIVACY_MODE, LeSetPrivacyMode);
247 // Testing Commands
248 SET_HANDLER(OpCode::READ_LOOPBACK_MODE, ReadLoopbackMode);
249 SET_HANDLER(OpCode::WRITE_LOOPBACK_MODE, WriteLoopbackMode);
250 #undef SET_HANDLER
251 }
252
SniffSubrating(CommandPacketView command)253 void DualModeController::SniffSubrating(CommandPacketView command) {
254 auto command_view = gd_hci::SniffSubratingView::Create(
255 gd_hci::ConnectionManagementCommandView::Create(command));
256 ASSERT(command_view.IsValid());
257
258 send_event_(gd_hci::SniffSubratingCompleteBuilder::Create(
259 kNumCommandPackets, ErrorCode::SUCCESS,
260 command_view.GetConnectionHandle()));
261 }
262
RegisterTaskScheduler(std::function<AsyncTaskId (std::chrono::milliseconds,const TaskCallback &)> oneshot_scheduler)263 void DualModeController::RegisterTaskScheduler(
264 std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> oneshot_scheduler) {
265 link_layer_controller_.RegisterTaskScheduler(oneshot_scheduler);
266 }
267
RegisterPeriodicTaskScheduler(std::function<AsyncTaskId (std::chrono::milliseconds,std::chrono::milliseconds,const TaskCallback &)> periodic_scheduler)268 void DualModeController::RegisterPeriodicTaskScheduler(
269 std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
270 periodic_scheduler) {
271 link_layer_controller_.RegisterPeriodicTaskScheduler(periodic_scheduler);
272 }
273
RegisterTaskCancel(std::function<void (AsyncTaskId)> task_cancel)274 void DualModeController::RegisterTaskCancel(std::function<void(AsyncTaskId)> task_cancel) {
275 link_layer_controller_.RegisterTaskCancel(task_cancel);
276 }
277
HandleAcl(std::shared_ptr<std::vector<uint8_t>> packet)278 void DualModeController::HandleAcl(std::shared_ptr<std::vector<uint8_t>> packet) {
279 bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet(packet);
280 auto acl_packet = bluetooth::hci::AclPacketView::Create(raw_packet);
281 ASSERT(acl_packet.IsValid());
282 if (loopback_mode_ == LoopbackMode::ENABLE_LOCAL) {
283 uint16_t handle = acl_packet.GetHandle();
284
285 std::vector<bluetooth::hci::CompletedPackets> completed_packets;
286 bluetooth::hci::CompletedPackets cp;
287 cp.connection_handle_ = handle;
288 cp.host_num_of_completed_packets_ = kNumCommandPackets;
289 completed_packets.push_back(cp);
290 send_event_(bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
291 completed_packets));
292 return;
293 }
294
295 link_layer_controller_.SendAclToRemote(acl_packet);
296 }
297
HandleSco(std::shared_ptr<std::vector<uint8_t>> packet)298 void DualModeController::HandleSco(std::shared_ptr<std::vector<uint8_t>> packet) {
299 bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet(packet);
300 auto sco_packet = bluetooth::hci::ScoPacketView::Create(raw_packet);
301 if (loopback_mode_ == LoopbackMode::ENABLE_LOCAL) {
302 uint16_t handle = sco_packet.GetHandle();
303 send_sco_(packet);
304 std::vector<bluetooth::hci::CompletedPackets> completed_packets;
305 bluetooth::hci::CompletedPackets cp;
306 cp.connection_handle_ = handle;
307 cp.host_num_of_completed_packets_ = kNumCommandPackets;
308 completed_packets.push_back(cp);
309 send_event_(bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
310 completed_packets));
311 return;
312 }
313 }
314
HandleIso(std::shared_ptr<std::vector<uint8_t>>)315 void DualModeController::HandleIso(
316 std::shared_ptr<std::vector<uint8_t>> /* packet */) {
317 // TODO: implement handling similar to HandleSco
318 }
319
HandleCommand(std::shared_ptr<std::vector<uint8_t>> packet)320 void DualModeController::HandleCommand(std::shared_ptr<std::vector<uint8_t>> packet) {
321 bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet(packet);
322 auto command_packet = bluetooth::hci::CommandPacketView::Create(raw_packet);
323 ASSERT(command_packet.IsValid());
324 auto op = command_packet.GetOpCode();
325
326 if (loopback_mode_ == LoopbackMode::ENABLE_LOCAL &&
327 // Loopback exceptions.
328 op != OpCode::RESET &&
329 op != OpCode::SET_CONTROLLER_TO_HOST_FLOW_CONTROL &&
330 op != OpCode::HOST_BUFFER_SIZE &&
331 op != OpCode::HOST_NUM_COMPLETED_PACKETS &&
332 op != OpCode::READ_BUFFER_SIZE && op != OpCode::READ_LOOPBACK_MODE &&
333 op != OpCode::WRITE_LOOPBACK_MODE) {
334 std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
335 std::make_unique<bluetooth::packet::RawBuilder>(255);
336 raw_builder_ptr->AddOctets(*packet);
337 send_event_(bluetooth::hci::LoopbackCommandBuilder::Create(
338 std::move(raw_builder_ptr)));
339 } else if (active_hci_commands_.count(op) > 0) {
340 active_hci_commands_[op](command_packet);
341 } else {
342 uint16_t opcode = static_cast<uint16_t>(op);
343 SendCommandCompleteUnknownOpCodeEvent(opcode);
344 LOG_INFO("Unknown command, opcode: 0x%04X, OGF: 0x%04X, OCF: 0x%04X",
345 opcode, (opcode & 0xFC00) >> 10, opcode & 0x03FF);
346 }
347 }
348
RegisterEventChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)349 void DualModeController::RegisterEventChannel(
350 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
351 send_event_ =
352 [callback](std::shared_ptr<bluetooth::hci::EventPacketBuilder> event) {
353 auto bytes = std::make_shared<std::vector<uint8_t>>();
354 bluetooth::packet::BitInserter bit_inserter(*bytes);
355 bytes->reserve(event->size());
356 event->Serialize(bit_inserter);
357 callback(std::move(bytes));
358 };
359 link_layer_controller_.RegisterEventChannel(send_event_);
360 }
361
RegisterAclChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)362 void DualModeController::RegisterAclChannel(
363 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
364 send_acl_ =
365 [callback](std::shared_ptr<bluetooth::hci::AclPacketBuilder> acl_data) {
366 auto bytes = std::make_shared<std::vector<uint8_t>>();
367 bluetooth::packet::BitInserter bit_inserter(*bytes);
368 bytes->reserve(acl_data->size());
369 acl_data->Serialize(bit_inserter);
370 callback(std::move(bytes));
371 };
372 link_layer_controller_.RegisterAclChannel(send_acl_);
373 }
374
RegisterScoChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)375 void DualModeController::RegisterScoChannel(
376 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
377 link_layer_controller_.RegisterScoChannel(callback);
378 send_sco_ = callback;
379 }
380
RegisterIsoChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)381 void DualModeController::RegisterIsoChannel(
382 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
383 callback) {
384 link_layer_controller_.RegisterIsoChannel(callback);
385 send_iso_ = callback;
386 }
387
Reset(CommandPacketView command)388 void DualModeController::Reset(CommandPacketView command) {
389 auto command_view = gd_hci::ResetView::Create(command);
390 ASSERT(command_view.IsValid());
391 link_layer_controller_.Reset();
392 if (loopback_mode_ == LoopbackMode::ENABLE_LOCAL) {
393 loopback_mode_ = LoopbackMode::NO_LOOPBACK;
394 }
395
396 send_event_(bluetooth::hci::ResetCompleteBuilder::Create(kNumCommandPackets,
397 ErrorCode::SUCCESS));
398 }
399
ReadBufferSize(CommandPacketView command)400 void DualModeController::ReadBufferSize(CommandPacketView command) {
401 auto command_view = gd_hci::ReadBufferSizeView::Create(command);
402 ASSERT(command_view.IsValid());
403
404 auto packet = bluetooth::hci::ReadBufferSizeCompleteBuilder::Create(
405 kNumCommandPackets, ErrorCode::SUCCESS,
406 properties_.GetAclDataPacketSize(),
407 properties_.GetSynchronousDataPacketSize(),
408 properties_.GetTotalNumAclDataPackets(),
409 properties_.GetTotalNumSynchronousDataPackets());
410 send_event_(std::move(packet));
411 }
412
ReadEncryptionKeySize(CommandPacketView command)413 void DualModeController::ReadEncryptionKeySize(CommandPacketView command) {
414 auto command_view = gd_hci::ReadEncryptionKeySizeView::Create(
415 gd_hci::SecurityCommandView::Create(command));
416 ASSERT(command_view.IsValid());
417
418 auto packet = bluetooth::hci::ReadEncryptionKeySizeCompleteBuilder::Create(
419 kNumCommandPackets, ErrorCode::SUCCESS,
420 command_view.GetConnectionHandle(), properties_.GetEncryptionKeySize());
421 send_event_(std::move(packet));
422 }
423
HostBufferSize(CommandPacketView command)424 void DualModeController::HostBufferSize(CommandPacketView command) {
425 auto command_view = gd_hci::HostBufferSizeView::Create(command);
426 ASSERT(command_view.IsValid());
427 auto packet = bluetooth::hci::HostBufferSizeCompleteBuilder::Create(
428 kNumCommandPackets, ErrorCode::SUCCESS);
429 send_event_(std::move(packet));
430 }
431
ReadLocalVersionInformation(CommandPacketView command)432 void DualModeController::ReadLocalVersionInformation(
433 CommandPacketView command) {
434 auto command_view = gd_hci::ReadLocalVersionInformationView::Create(command);
435 ASSERT(command_view.IsValid());
436
437 bluetooth::hci::LocalVersionInformation local_version_information;
438 local_version_information.hci_version_ =
439 static_cast<bluetooth::hci::HciVersion>(properties_.GetVersion());
440 local_version_information.hci_revision_ = properties_.GetRevision();
441 local_version_information.lmp_version_ =
442 static_cast<bluetooth::hci::LmpVersion>(properties_.GetLmpPalVersion());
443 local_version_information.manufacturer_name_ =
444 properties_.GetManufacturerName();
445 local_version_information.lmp_subversion_ = properties_.GetLmpPalSubversion();
446 auto packet =
447 bluetooth::hci::ReadLocalVersionInformationCompleteBuilder::Create(
448 kNumCommandPackets, ErrorCode::SUCCESS, local_version_information);
449 send_event_(std::move(packet));
450 }
451
ReadRemoteVersionInformation(CommandPacketView command)452 void DualModeController::ReadRemoteVersionInformation(
453 CommandPacketView command) {
454 auto command_view = gd_hci::ReadRemoteVersionInformationView::Create(
455 gd_hci::ConnectionManagementCommandView::Create(command));
456 ASSERT(command_view.IsValid());
457
458 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
459 OpCode::READ_REMOTE_VERSION_INFORMATION, command.GetPayload(),
460 command_view.GetConnectionHandle());
461
462 auto packet =
463 bluetooth::hci::ReadRemoteVersionInformationStatusBuilder::Create(
464 status, kNumCommandPackets);
465 send_event_(std::move(packet));
466 }
467
ReadBdAddr(CommandPacketView command)468 void DualModeController::ReadBdAddr(CommandPacketView command) {
469 auto command_view = gd_hci::ReadBdAddrView::Create(command);
470 ASSERT(command_view.IsValid());
471 auto packet = bluetooth::hci::ReadBdAddrCompleteBuilder::Create(
472 kNumCommandPackets, ErrorCode::SUCCESS, properties_.GetAddress());
473 send_event_(std::move(packet));
474 }
475
ReadLocalSupportedCommands(CommandPacketView command)476 void DualModeController::ReadLocalSupportedCommands(CommandPacketView command) {
477 auto command_view = gd_hci::ReadLocalSupportedCommandsView::Create(command);
478 ASSERT(command_view.IsValid());
479
480 std::array<uint8_t, 64> supported_commands{};
481 supported_commands.fill(0x00);
482 size_t len = properties_.GetSupportedCommands().size();
483 if (len > 64) {
484 len = 64;
485 }
486 std::copy_n(properties_.GetSupportedCommands().begin(), len,
487 supported_commands.begin());
488
489 auto packet =
490 bluetooth::hci::ReadLocalSupportedCommandsCompleteBuilder::Create(
491 kNumCommandPackets, ErrorCode::SUCCESS, supported_commands);
492 send_event_(std::move(packet));
493 }
494
ReadLocalSupportedFeatures(CommandPacketView command)495 void DualModeController::ReadLocalSupportedFeatures(CommandPacketView command) {
496 auto command_view = gd_hci::ReadLocalSupportedFeaturesView::Create(command);
497 ASSERT(command_view.IsValid());
498 auto packet =
499 bluetooth::hci::ReadLocalSupportedFeaturesCompleteBuilder::Create(
500 kNumCommandPackets, ErrorCode::SUCCESS,
501 properties_.GetSupportedFeatures());
502 send_event_(std::move(packet));
503 }
504
ReadLocalSupportedCodecs(CommandPacketView command)505 void DualModeController::ReadLocalSupportedCodecs(CommandPacketView command) {
506 auto command_view = gd_hci::ReadLocalSupportedCodecsV1View::Create(command);
507 ASSERT(command_view.IsValid());
508 auto packet =
509 bluetooth::hci::ReadLocalSupportedCodecsV1CompleteBuilder::Create(
510 kNumCommandPackets, ErrorCode::SUCCESS,
511 properties_.GetSupportedCodecs(),
512 properties_.GetVendorSpecificCodecs());
513 send_event_(std::move(packet));
514 }
515
ReadLocalExtendedFeatures(CommandPacketView command)516 void DualModeController::ReadLocalExtendedFeatures(CommandPacketView command) {
517 auto command_view = gd_hci::ReadLocalExtendedFeaturesView::Create(command);
518 ASSERT(command_view.IsValid());
519 uint8_t page_number = command_view.GetPageNumber();
520
521 auto pakcet =
522 bluetooth::hci::ReadLocalExtendedFeaturesCompleteBuilder::Create(
523 kNumCommandPackets, ErrorCode::SUCCESS, page_number,
524 properties_.GetExtendedFeaturesMaximumPageNumber(),
525 properties_.GetExtendedFeatures(page_number));
526 send_event_(std::move(pakcet));
527 }
528
ReadRemoteExtendedFeatures(CommandPacketView command)529 void DualModeController::ReadRemoteExtendedFeatures(CommandPacketView command) {
530 auto command_view = gd_hci::ReadRemoteExtendedFeaturesView::Create(
531 gd_hci::ConnectionManagementCommandView::Create(command));
532 ASSERT(command_view.IsValid());
533
534 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
535 OpCode::READ_REMOTE_EXTENDED_FEATURES, command_view.GetPayload(),
536 command_view.GetConnectionHandle());
537
538 auto packet = bluetooth::hci::ReadRemoteExtendedFeaturesStatusBuilder::Create(
539 status, kNumCommandPackets);
540 send_event_(std::move(packet));
541 }
542
SwitchRole(CommandPacketView command)543 void DualModeController::SwitchRole(CommandPacketView command) {
544 auto command_view = gd_hci::SwitchRoleView::Create(
545 gd_hci::ConnectionManagementCommandView::Create(command));
546 ASSERT(command_view.IsValid());
547
548 auto status = link_layer_controller_.SwitchRole(
549 command_view.GetBdAddr(), static_cast<uint8_t>(command_view.GetRole()));
550
551 auto packet = bluetooth::hci::SwitchRoleStatusBuilder::Create(
552 status, kNumCommandPackets);
553 send_event_(std::move(packet));
554 }
555
ReadRemoteSupportedFeatures(CommandPacketView command)556 void DualModeController::ReadRemoteSupportedFeatures(
557 CommandPacketView command) {
558 auto command_view = gd_hci::ReadRemoteSupportedFeaturesView::Create(
559 gd_hci::ConnectionManagementCommandView::Create(command));
560 ASSERT(command_view.IsValid());
561
562 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
563 OpCode::READ_REMOTE_SUPPORTED_FEATURES, command_view.GetPayload(),
564 command_view.GetConnectionHandle());
565
566 auto packet =
567 bluetooth::hci::ReadRemoteSupportedFeaturesStatusBuilder::Create(
568 status, kNumCommandPackets);
569 send_event_(std::move(packet));
570 }
571
ReadClockOffset(CommandPacketView command)572 void DualModeController::ReadClockOffset(CommandPacketView command) {
573 auto command_view = gd_hci::ReadClockOffsetView::Create(
574 gd_hci::ConnectionManagementCommandView::Create(command));
575 ASSERT(command_view.IsValid());
576
577 uint16_t handle = command_view.GetConnectionHandle();
578
579 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
580 OpCode::READ_CLOCK_OFFSET, command_view.GetPayload(), handle);
581
582 auto packet = bluetooth::hci::ReadClockOffsetStatusBuilder::Create(
583 status, kNumCommandPackets);
584 send_event_(std::move(packet));
585 }
586
IoCapabilityRequestReply(CommandPacketView command)587 void DualModeController::IoCapabilityRequestReply(CommandPacketView command) {
588 auto command_view = gd_hci::IoCapabilityRequestReplyView::Create(
589 gd_hci::SecurityCommandView::Create(command));
590 ASSERT(command_view.IsValid());
591
592 Address peer = command_view.GetBdAddr();
593 uint8_t io_capability = static_cast<uint8_t>(command_view.GetIoCapability());
594 uint8_t oob_data_present_flag =
595 static_cast<uint8_t>(command_view.GetOobPresent());
596 uint8_t authentication_requirements =
597 static_cast<uint8_t>(command_view.GetAuthenticationRequirements());
598
599 auto status = link_layer_controller_.IoCapabilityRequestReply(
600 peer, io_capability, oob_data_present_flag, authentication_requirements);
601 auto packet = bluetooth::hci::IoCapabilityRequestReplyCompleteBuilder::Create(
602 kNumCommandPackets, status, peer);
603
604 send_event_(std::move(packet));
605 }
606
UserConfirmationRequestReply(CommandPacketView command)607 void DualModeController::UserConfirmationRequestReply(
608 CommandPacketView command) {
609 auto command_view = gd_hci::UserConfirmationRequestReplyView::Create(
610 gd_hci::SecurityCommandView::Create(command));
611 ASSERT(command_view.IsValid());
612
613 Address peer = command_view.GetBdAddr();
614
615 auto status = link_layer_controller_.UserConfirmationRequestReply(peer);
616 auto packet =
617 bluetooth::hci::UserConfirmationRequestReplyCompleteBuilder::Create(
618 kNumCommandPackets, status, peer);
619
620 send_event_(std::move(packet));
621 }
622
UserConfirmationRequestNegativeReply(CommandPacketView command)623 void DualModeController::UserConfirmationRequestNegativeReply(
624 CommandPacketView command) {
625 auto command_view = gd_hci::UserConfirmationRequestNegativeReplyView::Create(
626 gd_hci::SecurityCommandView::Create(command));
627 ASSERT(command_view.IsValid());
628
629 Address peer = command_view.GetBdAddr();
630
631 auto status =
632 link_layer_controller_.UserConfirmationRequestNegativeReply(peer);
633 auto packet =
634 bluetooth::hci::UserConfirmationRequestNegativeReplyCompleteBuilder::
635 Create(kNumCommandPackets, status, peer);
636
637 send_event_(std::move(packet));
638 }
639
UserPasskeyRequestReply(CommandPacketView command)640 void DualModeController::UserPasskeyRequestReply(CommandPacketView command) {
641 auto command_view = gd_hci::UserPasskeyRequestReplyView::Create(
642 gd_hci::SecurityCommandView::Create(command));
643 ASSERT(command_view.IsValid());
644
645 Address peer = command_view.GetBdAddr();
646 uint32_t numeric_value = command_view.GetNumericValue();
647
648 auto status =
649 link_layer_controller_.UserPasskeyRequestReply(peer, numeric_value);
650 auto packet = bluetooth::hci::UserPasskeyRequestReplyCompleteBuilder::Create(
651 kNumCommandPackets, status, peer);
652
653 send_event_(std::move(packet));
654 }
655
UserPasskeyRequestNegativeReply(CommandPacketView command)656 void DualModeController::UserPasskeyRequestNegativeReply(
657 CommandPacketView command) {
658 auto command_view = gd_hci::UserPasskeyRequestNegativeReplyView::Create(
659 gd_hci::SecurityCommandView::Create(command));
660 ASSERT(command_view.IsValid());
661
662 Address peer = command_view.GetBdAddr();
663
664 auto status = link_layer_controller_.UserPasskeyRequestNegativeReply(peer);
665 auto packet =
666 bluetooth::hci::UserPasskeyRequestNegativeReplyCompleteBuilder::Create(
667 kNumCommandPackets, status, peer);
668
669 send_event_(std::move(packet));
670 }
671
RemoteOobDataRequestReply(CommandPacketView command)672 void DualModeController::RemoteOobDataRequestReply(CommandPacketView command) {
673 auto command_view = gd_hci::RemoteOobDataRequestReplyView::Create(
674 gd_hci::SecurityCommandView::Create(command));
675 ASSERT(command_view.IsValid());
676
677 Address peer = command_view.GetBdAddr();
678 std::array<uint8_t, 16> c = command_view.GetC();
679 std::array<uint8_t, 16> r = command_view.GetR();
680
681 auto status = link_layer_controller_.RemoteOobDataRequestReply(
682 peer, std::vector<uint8_t>(c.begin(), c.end()),
683 std::vector<uint8_t>(r.begin(), r.end()));
684 auto packet =
685 bluetooth::hci::RemoteOobDataRequestReplyCompleteBuilder::Create(
686 kNumCommandPackets, status, peer);
687
688 send_event_(std::move(packet));
689 }
690
RemoteOobDataRequestNegativeReply(CommandPacketView command)691 void DualModeController::RemoteOobDataRequestNegativeReply(
692 CommandPacketView command) {
693 auto command_view = gd_hci::RemoteOobDataRequestNegativeReplyView::Create(
694 gd_hci::SecurityCommandView::Create(command));
695 ASSERT(command_view.IsValid());
696
697 Address peer = command_view.GetBdAddr();
698
699 auto status = link_layer_controller_.RemoteOobDataRequestNegativeReply(peer);
700 auto packet =
701 bluetooth::hci::RemoteOobDataRequestNegativeReplyCompleteBuilder::Create(
702 kNumCommandPackets, status, peer);
703
704 send_event_(std::move(packet));
705 }
706
IoCapabilityRequestNegativeReply(CommandPacketView command)707 void DualModeController::IoCapabilityRequestNegativeReply(
708 CommandPacketView command) {
709 auto command_view = gd_hci::IoCapabilityRequestNegativeReplyView::Create(
710 gd_hci::SecurityCommandView::Create(command));
711 ASSERT(command_view.IsValid());
712
713 Address peer = command_view.GetBdAddr();
714 ErrorCode reason = command_view.GetReason();
715
716 auto status =
717 link_layer_controller_.IoCapabilityRequestNegativeReply(peer, reason);
718 auto packet =
719 bluetooth::hci::IoCapabilityRequestNegativeReplyCompleteBuilder::Create(
720 kNumCommandPackets, status, peer);
721
722 send_event_(std::move(packet));
723 }
724
ReadInquiryResponseTransmitPowerLevel(CommandPacketView command)725 void DualModeController::ReadInquiryResponseTransmitPowerLevel(
726 CommandPacketView command) {
727 auto command_view = gd_hci::ReadInquiryResponseTransmitPowerLevelView::Create(
728 gd_hci::DiscoveryCommandView::Create(command));
729 ASSERT(command_view.IsValid());
730
731 uint8_t tx_power = 20; // maximum
732 auto packet =
733 bluetooth::hci::ReadInquiryResponseTransmitPowerLevelCompleteBuilder::
734 Create(kNumCommandPackets, ErrorCode::SUCCESS, tx_power);
735 send_event_(std::move(packet));
736 }
737
WriteSimplePairingMode(CommandPacketView command)738 void DualModeController::WriteSimplePairingMode(CommandPacketView command) {
739 auto command_view = gd_hci::WriteSimplePairingModeView::Create(
740 gd_hci::SecurityCommandView::Create(command));
741 ASSERT(command_view.IsValid());
742
743 link_layer_controller_.WriteSimplePairingMode(
744 command_view.GetSimplePairingMode() == gd_hci::Enable::ENABLED);
745 auto packet = bluetooth::hci::WriteSimplePairingModeCompleteBuilder::Create(
746 kNumCommandPackets, ErrorCode::SUCCESS);
747 send_event_(std::move(packet));
748 }
749
ChangeConnectionPacketType(CommandPacketView command)750 void DualModeController::ChangeConnectionPacketType(CommandPacketView command) {
751 auto command_view = gd_hci::ChangeConnectionPacketTypeView::Create(
752 gd_hci::ConnectionManagementCommandView::Create(command));
753 ASSERT(command_view.IsValid());
754
755 uint16_t handle = command_view.GetConnectionHandle();
756 uint16_t packet_type = static_cast<uint16_t>(command_view.GetPacketType());
757
758 auto status =
759 link_layer_controller_.ChangeConnectionPacketType(handle, packet_type);
760
761 auto packet = bluetooth::hci::ChangeConnectionPacketTypeStatusBuilder::Create(
762 status, kNumCommandPackets);
763 send_event_(std::move(packet));
764 }
765
WriteLeHostSupport(CommandPacketView command)766 void DualModeController::WriteLeHostSupport(CommandPacketView command) {
767 auto command_view = gd_hci::WriteLeHostSupportView::Create(command);
768 ASSERT(command_view.IsValid());
769 auto packet = bluetooth::hci::WriteLeHostSupportCompleteBuilder::Create(
770 kNumCommandPackets, ErrorCode::SUCCESS);
771 send_event_(std::move(packet));
772 }
773
WriteSecureConnectionsHostSupport(CommandPacketView command)774 void DualModeController::WriteSecureConnectionsHostSupport(
775 CommandPacketView command) {
776 auto command_view = gd_hci::WriteSecureConnectionsHostSupportView::Create(
777 gd_hci::SecurityCommandView::Create(command));
778 properties_.SetExtendedFeatures(properties_.GetExtendedFeatures(1) | 0x8, 1);
779 auto packet =
780 bluetooth::hci::WriteSecureConnectionsHostSupportCompleteBuilder::Create(
781 kNumCommandPackets, ErrorCode::SUCCESS);
782 send_event_(std::move(packet));
783 }
784
SetEventMask(CommandPacketView command)785 void DualModeController::SetEventMask(CommandPacketView command) {
786 auto command_view = gd_hci::SetEventMaskView::Create(command);
787 ASSERT(command_view.IsValid());
788 auto packet = bluetooth::hci::SetEventMaskCompleteBuilder::Create(
789 kNumCommandPackets, ErrorCode::SUCCESS);
790 send_event_(std::move(packet));
791 }
792
ReadInquiryMode(CommandPacketView command)793 void DualModeController::ReadInquiryMode(CommandPacketView command) {
794 auto command_view = gd_hci::ReadInquiryModeView::Create(
795 gd_hci::DiscoveryCommandView::Create(command));
796 ASSERT(command_view.IsValid());
797 gd_hci::InquiryMode inquiry_mode = gd_hci::InquiryMode::STANDARD;
798 auto packet = bluetooth::hci::ReadInquiryModeCompleteBuilder::Create(
799 kNumCommandPackets, ErrorCode::SUCCESS, inquiry_mode);
800 send_event_(std::move(packet));
801 }
802
WriteInquiryMode(CommandPacketView command)803 void DualModeController::WriteInquiryMode(CommandPacketView command) {
804 auto command_view = gd_hci::WriteInquiryModeView::Create(
805 gd_hci::DiscoveryCommandView::Create(command));
806 ASSERT(command_view.IsValid());
807 link_layer_controller_.SetInquiryMode(
808 static_cast<uint8_t>(command_view.GetInquiryMode()));
809 auto packet = bluetooth::hci::WriteInquiryModeCompleteBuilder::Create(
810 kNumCommandPackets, ErrorCode::SUCCESS);
811 send_event_(std::move(packet));
812 }
813
ReadPageScanType(CommandPacketView command)814 void DualModeController::ReadPageScanType(CommandPacketView command) {
815 auto command_view = gd_hci::ReadPageScanTypeView::Create(
816 gd_hci::DiscoveryCommandView::Create(command));
817 ASSERT(command_view.IsValid());
818 gd_hci::PageScanType page_scan_type = gd_hci::PageScanType::STANDARD;
819 auto packet = bluetooth::hci::ReadPageScanTypeCompleteBuilder::Create(
820 kNumCommandPackets, ErrorCode::SUCCESS, page_scan_type);
821 send_event_(std::move(packet));
822 }
823
WritePageScanType(CommandPacketView command)824 void DualModeController::WritePageScanType(CommandPacketView command) {
825 auto command_view = gd_hci::WritePageScanTypeView::Create(
826 gd_hci::DiscoveryCommandView::Create(command));
827 ASSERT(command_view.IsValid());
828 auto packet = bluetooth::hci::WritePageScanTypeCompleteBuilder::Create(
829 kNumCommandPackets, ErrorCode::SUCCESS);
830 send_event_(std::move(packet));
831 }
832
ReadInquiryScanType(CommandPacketView command)833 void DualModeController::ReadInquiryScanType(CommandPacketView command) {
834 auto command_view = gd_hci::ReadInquiryScanTypeView::Create(
835 gd_hci::DiscoveryCommandView::Create(command));
836 ASSERT(command_view.IsValid());
837 gd_hci::InquiryScanType inquiry_scan_type = gd_hci::InquiryScanType::STANDARD;
838 auto packet = bluetooth::hci::ReadInquiryScanTypeCompleteBuilder::Create(
839 kNumCommandPackets, ErrorCode::SUCCESS, inquiry_scan_type);
840 send_event_(std::move(packet));
841 }
842
WriteInquiryScanType(CommandPacketView command)843 void DualModeController::WriteInquiryScanType(CommandPacketView command) {
844 auto command_view = gd_hci::WriteInquiryScanTypeView::Create(
845 gd_hci::DiscoveryCommandView::Create(command));
846 ASSERT(command_view.IsValid());
847 auto packet = bluetooth::hci::WriteInquiryScanTypeCompleteBuilder::Create(
848 kNumCommandPackets, ErrorCode::SUCCESS);
849 send_event_(std::move(packet));
850 }
851
AuthenticationRequested(CommandPacketView command)852 void DualModeController::AuthenticationRequested(CommandPacketView command) {
853 auto command_view = gd_hci::AuthenticationRequestedView::Create(
854 gd_hci::ConnectionManagementCommandView::Create(command));
855 ASSERT(command_view.IsValid());
856 uint16_t handle = command_view.GetConnectionHandle();
857 auto status = link_layer_controller_.AuthenticationRequested(handle);
858
859 auto packet = bluetooth::hci::AuthenticationRequestedStatusBuilder::Create(
860 status, kNumCommandPackets);
861 send_event_(std::move(packet));
862 }
863
SetConnectionEncryption(CommandPacketView command)864 void DualModeController::SetConnectionEncryption(CommandPacketView command) {
865 auto command_view = gd_hci::SetConnectionEncryptionView::Create(
866 gd_hci::ConnectionManagementCommandView::Create(command));
867 ASSERT(command_view.IsValid());
868 uint16_t handle = command_view.GetConnectionHandle();
869 uint8_t encryption_enable =
870 static_cast<uint8_t>(command_view.GetEncryptionEnable());
871 auto status =
872 link_layer_controller_.SetConnectionEncryption(handle, encryption_enable);
873
874 auto packet = bluetooth::hci::SetConnectionEncryptionStatusBuilder::Create(
875 status, kNumCommandPackets);
876 send_event_(std::move(packet));
877 }
878
ChangeConnectionLinkKey(CommandPacketView command)879 void DualModeController::ChangeConnectionLinkKey(CommandPacketView command) {
880 auto command_view = gd_hci::ChangeConnectionLinkKeyView::Create(
881 gd_hci::ConnectionManagementCommandView::Create(command));
882 ASSERT(command_view.IsValid());
883 uint16_t handle = command_view.GetConnectionHandle();
884
885 auto status = link_layer_controller_.ChangeConnectionLinkKey(handle);
886
887 auto packet = bluetooth::hci::ChangeConnectionLinkKeyStatusBuilder::Create(
888 status, kNumCommandPackets);
889 send_event_(std::move(packet));
890 }
891
MasterLinkKey(CommandPacketView command)892 void DualModeController::MasterLinkKey(CommandPacketView command) {
893 auto command_view = gd_hci::MasterLinkKeyView::Create(
894 gd_hci::ConnectionManagementCommandView::Create(command));
895 ASSERT(command_view.IsValid());
896 uint8_t key_flag = static_cast<uint8_t>(command_view.GetKeyFlag());
897
898 auto status = link_layer_controller_.MasterLinkKey(key_flag);
899
900 auto packet = bluetooth::hci::MasterLinkKeyStatusBuilder::Create(
901 status, kNumCommandPackets);
902 send_event_(std::move(packet));
903 }
904
WriteAuthenticationEnable(CommandPacketView command)905 void DualModeController::WriteAuthenticationEnable(CommandPacketView command) {
906 auto command_view = gd_hci::WriteAuthenticationEnableView::Create(
907 gd_hci::SecurityCommandView::Create(command));
908 ASSERT(command_view.IsValid());
909 properties_.SetAuthenticationEnable(
910 static_cast<uint8_t>(command_view.GetAuthenticationEnable()));
911 auto packet =
912 bluetooth::hci::WriteAuthenticationEnableCompleteBuilder::Create(
913 kNumCommandPackets, ErrorCode::SUCCESS);
914 send_event_(std::move(packet));
915 }
916
ReadAuthenticationEnable(CommandPacketView command)917 void DualModeController::ReadAuthenticationEnable(CommandPacketView command) {
918 auto command_view = gd_hci::ReadAuthenticationEnableView::Create(command);
919 ASSERT(command_view.IsValid());
920 auto packet = bluetooth::hci::ReadAuthenticationEnableCompleteBuilder::Create(
921 kNumCommandPackets, ErrorCode::SUCCESS,
922 static_cast<bluetooth::hci::AuthenticationEnable>(
923 properties_.GetAuthenticationEnable()));
924 send_event_(std::move(packet));
925 }
926
WriteClassOfDevice(CommandPacketView command)927 void DualModeController::WriteClassOfDevice(CommandPacketView command) {
928 auto command_view = gd_hci::WriteClassOfDeviceView::Create(
929 gd_hci::DiscoveryCommandView::Create(command));
930 ASSERT(command_view.IsValid());
931 ClassOfDevice class_of_device = command_view.GetClassOfDevice();
932 properties_.SetClassOfDevice(class_of_device.cod[0], class_of_device.cod[1],
933 class_of_device.cod[2]);
934 auto packet = bluetooth::hci::WriteClassOfDeviceCompleteBuilder::Create(
935 kNumCommandPackets, ErrorCode::SUCCESS);
936 send_event_(std::move(packet));
937 }
938
ReadPageTimeout(CommandPacketView command)939 void DualModeController::ReadPageTimeout(CommandPacketView command) {
940 auto command_view = gd_hci::ReadPageTimeoutView::Create(
941 gd_hci::DiscoveryCommandView::Create(command));
942 ASSERT(command_view.IsValid());
943 uint16_t page_timeout = 0x2000;
944 auto packet = bluetooth::hci::ReadPageTimeoutCompleteBuilder::Create(
945 kNumCommandPackets, ErrorCode::SUCCESS, page_timeout);
946 send_event_(std::move(packet));
947 }
948
WritePageTimeout(CommandPacketView command)949 void DualModeController::WritePageTimeout(CommandPacketView command) {
950 auto command_view = gd_hci::WritePageTimeoutView::Create(
951 gd_hci::DiscoveryCommandView::Create(command));
952 ASSERT(command_view.IsValid());
953 auto packet = bluetooth::hci::WritePageTimeoutCompleteBuilder::Create(
954 kNumCommandPackets, ErrorCode::SUCCESS);
955 send_event_(std::move(packet));
956 }
957
HoldMode(CommandPacketView command)958 void DualModeController::HoldMode(CommandPacketView command) {
959 auto command_view = gd_hci::HoldModeView::Create(
960 gd_hci::ConnectionManagementCommandView::Create(command));
961 ASSERT(command_view.IsValid());
962 uint16_t handle = command_view.GetConnectionHandle();
963 uint16_t hold_mode_max_interval = command_view.GetHoldModeMaxInterval();
964 uint16_t hold_mode_min_interval = command_view.GetHoldModeMinInterval();
965
966 auto status = link_layer_controller_.HoldMode(handle, hold_mode_max_interval,
967 hold_mode_min_interval);
968
969 auto packet =
970 bluetooth::hci::HoldModeStatusBuilder::Create(status, kNumCommandPackets);
971 send_event_(std::move(packet));
972 }
973
SniffMode(CommandPacketView command)974 void DualModeController::SniffMode(CommandPacketView command) {
975 auto command_view = gd_hci::SniffModeView::Create(
976 gd_hci::ConnectionManagementCommandView::Create(command));
977 ASSERT(command_view.IsValid());
978 uint16_t handle = command_view.GetConnectionHandle();
979 uint16_t sniff_max_interval = command_view.GetSniffMaxInterval();
980 uint16_t sniff_min_interval = command_view.GetSniffMinInterval();
981 uint16_t sniff_attempt = command_view.GetSniffAttempt();
982 uint16_t sniff_timeout = command_view.GetSniffTimeout();
983
984 auto status = link_layer_controller_.SniffMode(handle, sniff_max_interval,
985 sniff_min_interval,
986 sniff_attempt, sniff_timeout);
987
988 auto packet = bluetooth::hci::SniffModeStatusBuilder::Create(
989 status, kNumCommandPackets);
990 send_event_(std::move(packet));
991 }
992
ExitSniffMode(CommandPacketView command)993 void DualModeController::ExitSniffMode(CommandPacketView command) {
994 auto command_view = gd_hci::ExitSniffModeView::Create(
995 gd_hci::ConnectionManagementCommandView::Create(command));
996 ASSERT(command_view.IsValid());
997
998 auto status =
999 link_layer_controller_.ExitSniffMode(command_view.GetConnectionHandle());
1000
1001 auto packet = bluetooth::hci::ExitSniffModeStatusBuilder::Create(
1002 status, kNumCommandPackets);
1003 send_event_(std::move(packet));
1004 }
1005
QosSetup(CommandPacketView command)1006 void DualModeController::QosSetup(CommandPacketView command) {
1007 auto command_view = gd_hci::QosSetupView::Create(
1008 gd_hci::ConnectionManagementCommandView::Create(command));
1009 ASSERT(command_view.IsValid());
1010 uint16_t handle = command_view.GetConnectionHandle();
1011 uint8_t service_type = static_cast<uint8_t>(command_view.GetServiceType());
1012 uint32_t token_rate = command_view.GetTokenRate();
1013 uint32_t peak_bandwidth = command_view.GetPeakBandwidth();
1014 uint32_t latency = command_view.GetLatency();
1015 uint32_t delay_variation = command_view.GetDelayVariation();
1016
1017 auto status =
1018 link_layer_controller_.QosSetup(handle, service_type, token_rate,
1019 peak_bandwidth, latency, delay_variation);
1020
1021 auto packet =
1022 bluetooth::hci::QosSetupStatusBuilder::Create(status, kNumCommandPackets);
1023 send_event_(std::move(packet));
1024 }
1025
ReadDefaultLinkPolicySettings(CommandPacketView command)1026 void DualModeController::ReadDefaultLinkPolicySettings(
1027 CommandPacketView command) {
1028 auto command_view = gd_hci::ReadDefaultLinkPolicySettingsView::Create(
1029 gd_hci::ConnectionManagementCommandView::Create(command));
1030 ASSERT(command_view.IsValid());
1031 uint16_t settings = link_layer_controller_.ReadDefaultLinkPolicySettings();
1032 auto packet =
1033 bluetooth::hci::ReadDefaultLinkPolicySettingsCompleteBuilder::Create(
1034 kNumCommandPackets, ErrorCode::SUCCESS, settings);
1035 send_event_(std::move(packet));
1036 }
1037
WriteDefaultLinkPolicySettings(CommandPacketView command)1038 void DualModeController::WriteDefaultLinkPolicySettings(
1039 CommandPacketView command) {
1040 auto command_view = gd_hci::WriteDefaultLinkPolicySettingsView::Create(
1041 gd_hci::ConnectionManagementCommandView::Create(command));
1042 ASSERT(command_view.IsValid());
1043 ErrorCode status = link_layer_controller_.WriteDefaultLinkPolicySettings(
1044 command_view.GetDefaultLinkPolicySettings());
1045 auto packet =
1046 bluetooth::hci::WriteDefaultLinkPolicySettingsCompleteBuilder::Create(
1047 kNumCommandPackets, status);
1048 send_event_(std::move(packet));
1049 }
1050
FlowSpecification(CommandPacketView command)1051 void DualModeController::FlowSpecification(CommandPacketView command) {
1052 auto command_view = gd_hci::FlowSpecificationView::Create(
1053 gd_hci::ConnectionManagementCommandView::Create(command));
1054 ASSERT(command_view.IsValid());
1055 uint16_t handle = command_view.GetConnectionHandle();
1056 uint8_t flow_direction =
1057 static_cast<uint8_t>(command_view.GetFlowDirection());
1058 uint8_t service_type = static_cast<uint8_t>(command_view.GetServiceType());
1059 uint32_t token_rate = command_view.GetTokenRate();
1060 uint32_t token_bucket_size = command_view.GetTokenBucketSize();
1061 uint32_t peak_bandwidth = command_view.GetPeakBandwidth();
1062 uint32_t access_latency = command_view.GetAccessLatency();
1063
1064 auto status = link_layer_controller_.FlowSpecification(
1065 handle, flow_direction, service_type, token_rate, token_bucket_size,
1066 peak_bandwidth, access_latency);
1067
1068 auto packet = bluetooth::hci::FlowSpecificationStatusBuilder::Create(
1069 status, kNumCommandPackets);
1070 send_event_(std::move(packet));
1071 }
1072
WriteLinkPolicySettings(CommandPacketView command)1073 void DualModeController::WriteLinkPolicySettings(CommandPacketView command) {
1074 auto command_view = gd_hci::WriteLinkPolicySettingsView::Create(
1075 gd_hci::ConnectionManagementCommandView::Create(command));
1076 ASSERT(command_view.IsValid());
1077
1078 uint16_t handle = command_view.GetConnectionHandle();
1079 uint16_t settings = command_view.GetLinkPolicySettings();
1080
1081 auto status =
1082 link_layer_controller_.WriteLinkPolicySettings(handle, settings);
1083
1084 auto packet = bluetooth::hci::WriteLinkPolicySettingsCompleteBuilder::Create(
1085 kNumCommandPackets, status, handle);
1086 send_event_(std::move(packet));
1087 }
1088
WriteLinkSupervisionTimeout(CommandPacketView command)1089 void DualModeController::WriteLinkSupervisionTimeout(
1090 CommandPacketView command) {
1091 auto command_view = gd_hci::WriteLinkSupervisionTimeoutView::Create(
1092 gd_hci::ConnectionManagementCommandView::Create(command));
1093 ASSERT(command_view.IsValid());
1094
1095 uint16_t handle = command_view.GetConnectionHandle();
1096 uint16_t timeout = command_view.GetLinkSupervisionTimeout();
1097
1098 auto status =
1099 link_layer_controller_.WriteLinkSupervisionTimeout(handle, timeout);
1100 auto packet =
1101 bluetooth::hci::WriteLinkSupervisionTimeoutCompleteBuilder::Create(
1102 kNumCommandPackets, status, handle);
1103 send_event_(std::move(packet));
1104 }
1105
ReadLocalName(CommandPacketView command)1106 void DualModeController::ReadLocalName(CommandPacketView command) {
1107 auto command_view = gd_hci::ReadLocalNameView::Create(command);
1108 ASSERT(command_view.IsValid());
1109
1110 std::array<uint8_t, 248> local_name{};
1111 local_name.fill(0x00);
1112 size_t len = properties_.GetName().size();
1113 if (len > 247) {
1114 len = 247; // one byte for NULL octet (0x00)
1115 }
1116 std::copy_n(properties_.GetName().begin(), len, local_name.begin());
1117
1118 auto packet = bluetooth::hci::ReadLocalNameCompleteBuilder::Create(
1119 kNumCommandPackets, ErrorCode::SUCCESS, local_name);
1120 send_event_(std::move(packet));
1121 }
1122
WriteLocalName(CommandPacketView command)1123 void DualModeController::WriteLocalName(CommandPacketView command) {
1124 auto command_view = gd_hci::WriteLocalNameView::Create(command);
1125 ASSERT(command_view.IsValid());
1126 const auto local_name = command_view.GetLocalName();
1127 std::vector<uint8_t> name_vec(248);
1128 for (size_t i = 0; i < 248; i++) {
1129 name_vec[i] = local_name[i];
1130 }
1131 properties_.SetName(name_vec);
1132 auto packet = bluetooth::hci::WriteLocalNameCompleteBuilder::Create(
1133 kNumCommandPackets, ErrorCode::SUCCESS);
1134 send_event_(std::move(packet));
1135 }
1136
WriteExtendedInquiryResponse(CommandPacketView command)1137 void DualModeController::WriteExtendedInquiryResponse(
1138 CommandPacketView command) {
1139 auto command_view = gd_hci::WriteExtendedInquiryResponseView::Create(command);
1140 ASSERT(command_view.IsValid());
1141 properties_.SetExtendedInquiryData(std::vector<uint8_t>(
1142 command_view.GetPayload().begin() + 1, command_view.GetPayload().end()));
1143 auto packet =
1144 bluetooth::hci::WriteExtendedInquiryResponseCompleteBuilder::Create(
1145 kNumCommandPackets, ErrorCode::SUCCESS);
1146 send_event_(std::move(packet));
1147 }
1148
RefreshEncryptionKey(CommandPacketView command)1149 void DualModeController::RefreshEncryptionKey(CommandPacketView command) {
1150 auto command_view = gd_hci::RefreshEncryptionKeyView::Create(
1151 gd_hci::SecurityCommandView::Create(command));
1152 ASSERT(command_view.IsValid());
1153 uint16_t handle = command_view.GetConnectionHandle();
1154 auto status_packet =
1155 bluetooth::hci::RefreshEncryptionKeyStatusBuilder::Create(
1156 ErrorCode::SUCCESS, kNumCommandPackets);
1157 send_event_(std::move(status_packet));
1158 // TODO: Support this in the link layer
1159 auto complete_packet =
1160 bluetooth::hci::EncryptionKeyRefreshCompleteBuilder::Create(
1161 ErrorCode::SUCCESS, handle);
1162 send_event_(std::move(complete_packet));
1163 }
1164
WriteVoiceSetting(CommandPacketView command)1165 void DualModeController::WriteVoiceSetting(CommandPacketView command) {
1166 auto command_view = gd_hci::WriteVoiceSettingView::Create(command);
1167 ASSERT(command_view.IsValid());
1168 auto packet = bluetooth::hci::WriteVoiceSettingCompleteBuilder::Create(
1169 kNumCommandPackets, ErrorCode::SUCCESS);
1170 send_event_(std::move(packet));
1171 }
1172
ReadNumberOfSupportedIac(CommandPacketView command)1173 void DualModeController::ReadNumberOfSupportedIac(CommandPacketView command) {
1174 auto command_view = gd_hci::ReadNumberOfSupportedIacView::Create(
1175 gd_hci::DiscoveryCommandView::Create(command));
1176 ASSERT(command_view.IsValid());
1177 uint8_t num_support_iac = 0x1;
1178 auto packet = bluetooth::hci::ReadNumberOfSupportedIacCompleteBuilder::Create(
1179 kNumCommandPackets, ErrorCode::SUCCESS, num_support_iac);
1180 send_event_(std::move(packet));
1181 }
1182
ReadCurrentIacLap(CommandPacketView command)1183 void DualModeController::ReadCurrentIacLap(CommandPacketView command) {
1184 auto command_view = gd_hci::ReadCurrentIacLapView::Create(
1185 gd_hci::DiscoveryCommandView::Create(command));
1186 ASSERT(command_view.IsValid());
1187 gd_hci::Lap lap;
1188 lap.lap_ = 0x30;
1189 auto packet = bluetooth::hci::ReadCurrentIacLapCompleteBuilder::Create(
1190 kNumCommandPackets, ErrorCode::SUCCESS, {lap});
1191 send_event_(std::move(packet));
1192 }
1193
WriteCurrentIacLap(CommandPacketView command)1194 void DualModeController::WriteCurrentIacLap(CommandPacketView command) {
1195 auto command_view = gd_hci::WriteCurrentIacLapView::Create(
1196 gd_hci::DiscoveryCommandView::Create(command));
1197 ASSERT(command_view.IsValid());
1198 auto packet = bluetooth::hci::WriteCurrentIacLapCompleteBuilder::Create(
1199 kNumCommandPackets, ErrorCode::SUCCESS);
1200 send_event_(std::move(packet));
1201 }
1202
ReadPageScanActivity(CommandPacketView command)1203 void DualModeController::ReadPageScanActivity(CommandPacketView command) {
1204 auto command_view = gd_hci::ReadPageScanActivityView::Create(
1205 gd_hci::DiscoveryCommandView::Create(command));
1206 ASSERT(command_view.IsValid());
1207 uint16_t interval = 0x1000;
1208 uint16_t window = 0x0012;
1209 auto packet = bluetooth::hci::ReadPageScanActivityCompleteBuilder::Create(
1210 kNumCommandPackets, ErrorCode::SUCCESS, interval, window);
1211 send_event_(std::move(packet));
1212 }
1213
WritePageScanActivity(CommandPacketView command)1214 void DualModeController::WritePageScanActivity(CommandPacketView command) {
1215 auto command_view = gd_hci::WritePageScanActivityView::Create(
1216 gd_hci::DiscoveryCommandView::Create(command));
1217 ASSERT(command_view.IsValid());
1218 auto packet = bluetooth::hci::WritePageScanActivityCompleteBuilder::Create(
1219 kNumCommandPackets, ErrorCode::SUCCESS);
1220 send_event_(std::move(packet));
1221 }
1222
ReadInquiryScanActivity(CommandPacketView command)1223 void DualModeController::ReadInquiryScanActivity(CommandPacketView command) {
1224 auto command_view = gd_hci::ReadInquiryScanActivityView::Create(
1225 gd_hci::DiscoveryCommandView::Create(command));
1226 ASSERT(command_view.IsValid());
1227 uint16_t interval = 0x1000;
1228 uint16_t window = 0x0012;
1229 auto packet = bluetooth::hci::ReadInquiryScanActivityCompleteBuilder::Create(
1230 kNumCommandPackets, ErrorCode::SUCCESS, interval, window);
1231 send_event_(std::move(packet));
1232 }
1233
WriteInquiryScanActivity(CommandPacketView command)1234 void DualModeController::WriteInquiryScanActivity(CommandPacketView command) {
1235 auto command_view = gd_hci::WriteInquiryScanActivityView::Create(
1236 gd_hci::DiscoveryCommandView::Create(command));
1237 ASSERT(command_view.IsValid());
1238 auto packet = bluetooth::hci::WriteInquiryScanActivityCompleteBuilder::Create(
1239 kNumCommandPackets, ErrorCode::SUCCESS);
1240 send_event_(std::move(packet));
1241 }
1242
ReadScanEnable(CommandPacketView command)1243 void DualModeController::ReadScanEnable(CommandPacketView command) {
1244 auto command_view = gd_hci::ReadScanEnableView::Create(
1245 gd_hci::DiscoveryCommandView::Create(command));
1246 ASSERT(command_view.IsValid());
1247 auto packet = bluetooth::hci::ReadScanEnableCompleteBuilder::Create(
1248 kNumCommandPackets, ErrorCode::SUCCESS, gd_hci::ScanEnable::NO_SCANS);
1249 send_event_(std::move(packet));
1250 }
1251
WriteScanEnable(CommandPacketView command)1252 void DualModeController::WriteScanEnable(CommandPacketView command) {
1253 auto command_view = gd_hci::WriteScanEnableView::Create(
1254 gd_hci::DiscoveryCommandView::Create(command));
1255 ASSERT(command_view.IsValid());
1256 link_layer_controller_.SetInquiryScanEnable(
1257 command_view.GetScanEnable() ==
1258 gd_hci::ScanEnable::INQUIRY_AND_PAGE_SCAN ||
1259 command_view.GetScanEnable() == gd_hci::ScanEnable::INQUIRY_SCAN_ONLY);
1260 link_layer_controller_.SetPageScanEnable(
1261 command_view.GetScanEnable() ==
1262 gd_hci::ScanEnable::INQUIRY_AND_PAGE_SCAN ||
1263 command_view.GetScanEnable() == gd_hci::ScanEnable::PAGE_SCAN_ONLY);
1264 auto packet = bluetooth::hci::WriteScanEnableCompleteBuilder::Create(
1265 kNumCommandPackets, ErrorCode::SUCCESS);
1266 send_event_(std::move(packet));
1267 }
1268
SetEventFilter(CommandPacketView command)1269 void DualModeController::SetEventFilter(CommandPacketView command) {
1270 auto command_view = gd_hci::SetEventFilterView::Create(command);
1271 ASSERT(command_view.IsValid());
1272 auto packet = bluetooth::hci::SetEventFilterCompleteBuilder::Create(
1273 kNumCommandPackets, ErrorCode::SUCCESS);
1274 send_event_(std::move(packet));
1275 }
1276
Inquiry(CommandPacketView command)1277 void DualModeController::Inquiry(CommandPacketView command) {
1278 auto command_view = gd_hci::InquiryView::Create(
1279 gd_hci::DiscoveryCommandView::Create(command));
1280 ASSERT(command_view.IsValid());
1281 link_layer_controller_.SetInquiryLAP(command_view.GetLap().lap_);
1282 link_layer_controller_.SetInquiryMaxResponses(command_view.GetNumResponses());
1283 link_layer_controller_.StartInquiry(
1284 std::chrono::milliseconds(command_view.GetInquiryLength() * 1280));
1285
1286 auto packet = bluetooth::hci::InquiryStatusBuilder::Create(
1287 ErrorCode::SUCCESS, kNumCommandPackets);
1288 send_event_(std::move(packet));
1289 }
1290
InquiryCancel(CommandPacketView command)1291 void DualModeController::InquiryCancel(CommandPacketView command) {
1292 auto command_view = gd_hci::InquiryCancelView::Create(
1293 gd_hci::DiscoveryCommandView::Create(command));
1294 ASSERT(command_view.IsValid());
1295 link_layer_controller_.InquiryCancel();
1296 auto packet = bluetooth::hci::InquiryCancelCompleteBuilder::Create(
1297 kNumCommandPackets, ErrorCode::SUCCESS);
1298 send_event_(std::move(packet));
1299 }
1300
AcceptConnectionRequest(CommandPacketView command)1301 void DualModeController::AcceptConnectionRequest(CommandPacketView command) {
1302 auto command_view = gd_hci::AcceptConnectionRequestView::Create(
1303 gd_hci::ConnectionManagementCommandView::Create(command));
1304 ASSERT(command_view.IsValid());
1305 Address addr = command_view.GetBdAddr();
1306 bool try_role_switch = command_view.GetRole() ==
1307 gd_hci::AcceptConnectionRequestRole::BECOME_MASTER;
1308 auto status =
1309 link_layer_controller_.AcceptConnectionRequest(addr, try_role_switch);
1310 auto packet = bluetooth::hci::AcceptConnectionRequestStatusBuilder::Create(
1311 status, kNumCommandPackets);
1312 send_event_(std::move(packet));
1313 }
1314
RejectConnectionRequest(CommandPacketView command)1315 void DualModeController::RejectConnectionRequest(CommandPacketView command) {
1316 auto command_view = gd_hci::RejectConnectionRequestView::Create(
1317 gd_hci::ConnectionManagementCommandView::Create(command));
1318 ASSERT(command_view.IsValid());
1319 Address addr = command_view.GetBdAddr();
1320 uint8_t reason = static_cast<uint8_t>(command_view.GetReason());
1321 auto status = link_layer_controller_.RejectConnectionRequest(addr, reason);
1322 auto packet = bluetooth::hci::RejectConnectionRequestStatusBuilder::Create(
1323 status, kNumCommandPackets);
1324 send_event_(std::move(packet));
1325 }
1326
LinkKeyRequestReply(CommandPacketView command)1327 void DualModeController::LinkKeyRequestReply(CommandPacketView command) {
1328 auto command_view = gd_hci::LinkKeyRequestReplyView::Create(
1329 gd_hci::SecurityCommandView::Create(command));
1330 ASSERT(command_view.IsValid());
1331 Address addr = command_view.GetBdAddr();
1332 auto key = command_view.GetLinkKey();
1333 auto status = link_layer_controller_.LinkKeyRequestReply(addr, key);
1334 auto packet = bluetooth::hci::LinkKeyRequestReplyCompleteBuilder::Create(
1335 kNumCommandPackets, status);
1336 send_event_(std::move(packet));
1337 }
1338
LinkKeyRequestNegativeReply(CommandPacketView command)1339 void DualModeController::LinkKeyRequestNegativeReply(
1340 CommandPacketView command) {
1341 auto command_view = gd_hci::LinkKeyRequestNegativeReplyView::Create(
1342 gd_hci::SecurityCommandView::Create(command));
1343 ASSERT(command_view.IsValid());
1344 Address addr = command_view.GetBdAddr();
1345 auto status = link_layer_controller_.LinkKeyRequestNegativeReply(addr);
1346 auto packet =
1347 bluetooth::hci::LinkKeyRequestNegativeReplyCompleteBuilder::Create(
1348 kNumCommandPackets, status, addr);
1349 send_event_(std::move(packet));
1350 }
1351
DeleteStoredLinkKey(CommandPacketView command)1352 void DualModeController::DeleteStoredLinkKey(CommandPacketView command) {
1353 auto command_view = gd_hci::DeleteStoredLinkKeyView::Create(
1354 gd_hci::SecurityCommandView::Create(command));
1355 ASSERT(command_view.IsValid());
1356
1357 uint16_t deleted_keys = 0;
1358
1359 auto flag = command_view.GetDeleteAllFlag();
1360 if (flag == gd_hci::DeleteStoredLinkKeyDeleteAllFlag::SPECIFIED_BD_ADDR) {
1361 Address addr = command_view.GetBdAddr();
1362 deleted_keys = security_manager_.DeleteKey(addr);
1363 }
1364
1365 if (flag == gd_hci::DeleteStoredLinkKeyDeleteAllFlag::ALL) {
1366 security_manager_.DeleteAllKeys();
1367 }
1368
1369 auto packet = bluetooth::hci::DeleteStoredLinkKeyCompleteBuilder::Create(
1370 kNumCommandPackets, ErrorCode::SUCCESS, deleted_keys);
1371
1372 send_event_(std::move(packet));
1373 }
1374
RemoteNameRequest(CommandPacketView command)1375 void DualModeController::RemoteNameRequest(CommandPacketView command) {
1376 auto command_view = gd_hci::RemoteNameRequestView::Create(
1377 gd_hci::DiscoveryCommandView::Create(command));
1378 ASSERT(command_view.IsValid());
1379
1380 Address remote_addr = command_view.GetBdAddr();
1381
1382 auto status = link_layer_controller_.SendCommandToRemoteByAddress(
1383 OpCode::REMOTE_NAME_REQUEST, command_view.GetPayload(), remote_addr);
1384
1385 auto packet = bluetooth::hci::RemoteNameRequestStatusBuilder::Create(
1386 status, kNumCommandPackets);
1387 send_event_(std::move(packet));
1388 }
1389
LeSetEventMask(CommandPacketView command)1390 void DualModeController::LeSetEventMask(CommandPacketView command) {
1391 auto command_view = gd_hci::LeSetEventMaskView::Create(command);
1392 ASSERT(command_view.IsValid());
1393 /*
1394 uint64_t mask = args.begin().extract<uint64_t>();
1395 link_layer_controller_.SetLeEventMask(mask);
1396 */
1397 auto packet = bluetooth::hci::LeSetEventMaskCompleteBuilder::Create(
1398 kNumCommandPackets, ErrorCode::SUCCESS);
1399 send_event_(std::move(packet));
1400 }
1401
LeReadBufferSize(CommandPacketView command)1402 void DualModeController::LeReadBufferSize(CommandPacketView command) {
1403 auto command_view = gd_hci::LeReadBufferSizeV1View::Create(command);
1404 ASSERT(command_view.IsValid());
1405
1406 bluetooth::hci::LeBufferSize le_buffer_size;
1407 le_buffer_size.le_data_packet_length_ = properties_.GetLeDataPacketLength();
1408 le_buffer_size.total_num_le_packets_ = properties_.GetTotalNumLeDataPackets();
1409
1410 auto packet = bluetooth::hci::LeReadBufferSizeV1CompleteBuilder::Create(
1411 kNumCommandPackets, ErrorCode::SUCCESS, le_buffer_size);
1412 send_event_(std::move(packet));
1413 }
1414
LeReadLocalSupportedFeatures(CommandPacketView command)1415 void DualModeController::LeReadLocalSupportedFeatures(
1416 CommandPacketView command) {
1417 auto command_view = gd_hci::LeReadLocalSupportedFeaturesView::Create(command);
1418 ASSERT(command_view.IsValid());
1419 auto packet =
1420 bluetooth::hci::LeReadLocalSupportedFeaturesCompleteBuilder::Create(
1421 kNumCommandPackets, ErrorCode::SUCCESS,
1422 properties_.GetLeSupportedFeatures());
1423 send_event_(std::move(packet));
1424 }
1425
LeSetRandomAddress(CommandPacketView command)1426 void DualModeController::LeSetRandomAddress(CommandPacketView command) {
1427 auto command_view = gd_hci::LeSetRandomAddressView::Create(
1428 gd_hci::LeAdvertisingCommandView::Create(command));
1429 ASSERT(command_view.IsValid());
1430 properties_.SetLeAddress(command_view.GetRandomAddress());
1431 auto packet = bluetooth::hci::LeSetRandomAddressCompleteBuilder::Create(
1432 kNumCommandPackets, ErrorCode::SUCCESS);
1433 send_event_(std::move(packet));
1434 }
1435
LeSetAdvertisingParameters(CommandPacketView command)1436 void DualModeController::LeSetAdvertisingParameters(CommandPacketView command) {
1437 auto command_view = gd_hci::LeSetAdvertisingParametersView::Create(
1438 gd_hci::LeAdvertisingCommandView::Create(command));
1439 ASSERT(command_view.IsValid());
1440 auto peer_address = command_view.GetPeerAddress();
1441 auto type = command_view.GetType();
1442 if (type != bluetooth::hci::AdvertisingType::ADV_DIRECT_IND &&
1443 type != bluetooth::hci::AdvertisingType::ADV_DIRECT_IND_LOW) {
1444 peer_address = Address::kEmpty;
1445 }
1446 properties_.SetLeAdvertisingParameters(
1447 command_view.GetIntervalMin(), command_view.GetIntervalMax(),
1448 static_cast<uint8_t>(type),
1449 static_cast<uint8_t>(command_view.GetOwnAddressType()),
1450 static_cast<uint8_t>(command_view.GetPeerAddressType()), peer_address,
1451 command_view.GetChannelMap(),
1452 static_cast<uint8_t>(command_view.GetFilterPolicy()));
1453
1454 auto packet =
1455 bluetooth::hci::LeSetAdvertisingParametersCompleteBuilder::Create(
1456 kNumCommandPackets, ErrorCode::SUCCESS);
1457 send_event_(std::move(packet));
1458 }
1459
LeSetAdvertisingData(CommandPacketView command)1460 void DualModeController::LeSetAdvertisingData(CommandPacketView command) {
1461 auto command_view = gd_hci::LeSetAdvertisingDataView::Create(
1462 gd_hci::LeAdvertisingCommandView::Create(command));
1463 auto payload = command.GetPayload();
1464 auto data_size = *payload.begin();
1465 auto first_data = payload.begin() + 1;
1466 std::vector<uint8_t> payload_bytes{first_data, first_data + data_size};
1467 ASSERT_LOG(command_view.IsValid(), "%s command.size() = %zu",
1468 gd_hci::OpCodeText(command.GetOpCode()).c_str(), command.size());
1469 ASSERT(command_view.GetPayload().size() == 32);
1470 properties_.SetLeAdvertisement(payload_bytes);
1471 auto packet = bluetooth::hci::LeSetAdvertisingDataCompleteBuilder::Create(
1472 kNumCommandPackets, ErrorCode::SUCCESS);
1473 send_event_(std::move(packet));
1474 }
1475
LeSetScanResponseData(CommandPacketView command)1476 void DualModeController::LeSetScanResponseData(CommandPacketView command) {
1477 auto command_view = gd_hci::LeSetScanResponseDataView::Create(
1478 gd_hci::LeAdvertisingCommandView::Create(command));
1479 ASSERT(command_view.IsValid());
1480 ASSERT(command_view.GetPayload().size() == 32);
1481 properties_.SetLeScanResponse(std::vector<uint8_t>(
1482 command_view.GetPayload().begin() + 1, command_view.GetPayload().end()));
1483 auto packet = bluetooth::hci::LeSetScanResponseDataCompleteBuilder::Create(
1484 kNumCommandPackets, ErrorCode::SUCCESS);
1485 send_event_(std::move(packet));
1486 }
1487
LeSetAdvertisingEnable(CommandPacketView command)1488 void DualModeController::LeSetAdvertisingEnable(CommandPacketView command) {
1489 auto command_view = gd_hci::LeSetAdvertisingEnableView::Create(
1490 gd_hci::LeAdvertisingCommandView::Create(command));
1491 ASSERT(command_view.IsValid());
1492 auto status = link_layer_controller_.SetLeAdvertisingEnable(
1493 command_view.GetAdvertisingEnable() == gd_hci::Enable::ENABLED);
1494 send_event_(bluetooth::hci::LeSetAdvertisingEnableCompleteBuilder::Create(
1495 kNumCommandPackets, status));
1496 }
1497
LeSetScanParameters(CommandPacketView command)1498 void DualModeController::LeSetScanParameters(CommandPacketView command) {
1499 auto command_view = gd_hci::LeSetScanParametersView::Create(
1500 gd_hci::LeScanningCommandView::Create(command));
1501 ASSERT(command_view.IsValid());
1502 link_layer_controller_.SetLeScanType(
1503 static_cast<uint8_t>(command_view.GetLeScanType()));
1504 link_layer_controller_.SetLeScanInterval(command_view.GetLeScanInterval());
1505 link_layer_controller_.SetLeScanWindow(command_view.GetLeScanWindow());
1506 link_layer_controller_.SetLeAddressType(
1507 static_cast<uint8_t>(command_view.GetOwnAddressType()));
1508 link_layer_controller_.SetLeScanFilterPolicy(
1509 static_cast<uint8_t>(command_view.GetScanningFilterPolicy()));
1510 auto packet = bluetooth::hci::LeSetScanParametersCompleteBuilder::Create(
1511 kNumCommandPackets, ErrorCode::SUCCESS);
1512 send_event_(std::move(packet));
1513 }
1514
LeSetScanEnable(CommandPacketView command)1515 void DualModeController::LeSetScanEnable(CommandPacketView command) {
1516 auto command_view = gd_hci::LeSetScanEnableView::Create(
1517 gd_hci::LeScanningCommandView::Create(command));
1518 ASSERT(command_view.IsValid());
1519 if (command_view.GetLeScanEnable() == gd_hci::Enable::ENABLED) {
1520 link_layer_controller_.SetLeScanEnable(gd_hci::OpCode::LE_SET_SCAN_ENABLE);
1521 } else {
1522 link_layer_controller_.SetLeScanEnable(gd_hci::OpCode::NONE);
1523 }
1524 link_layer_controller_.SetLeFilterDuplicates(
1525 command_view.GetFilterDuplicates() == gd_hci::Enable::ENABLED);
1526 auto packet = bluetooth::hci::LeSetScanEnableCompleteBuilder::Create(
1527 kNumCommandPackets, ErrorCode::SUCCESS);
1528 send_event_(std::move(packet));
1529 }
1530
LeCreateConnection(CommandPacketView command)1531 void DualModeController::LeCreateConnection(CommandPacketView command) {
1532 auto command_view = gd_hci::LeCreateConnectionView::Create(
1533 gd_hci::LeConnectionManagementCommandView::Create(command));
1534 ASSERT(command_view.IsValid());
1535 link_layer_controller_.SetLeScanInterval(command_view.GetLeScanInterval());
1536 link_layer_controller_.SetLeScanWindow(command_view.GetLeScanWindow());
1537 uint8_t initiator_filter_policy =
1538 static_cast<uint8_t>(command_view.GetInitiatorFilterPolicy());
1539 link_layer_controller_.SetLeInitiatorFilterPolicy(initiator_filter_policy);
1540
1541 if (initiator_filter_policy == 0) { // Connect list not used
1542 uint8_t peer_address_type =
1543 static_cast<uint8_t>(command_view.GetPeerAddressType());
1544 Address peer_address = command_view.GetPeerAddress();
1545 link_layer_controller_.SetLePeerAddressType(peer_address_type);
1546 link_layer_controller_.SetLePeerAddress(peer_address);
1547 }
1548 link_layer_controller_.SetLeAddressType(
1549 static_cast<uint8_t>(command_view.GetOwnAddressType()));
1550 link_layer_controller_.SetLeConnectionIntervalMin(
1551 command_view.GetConnIntervalMin());
1552 link_layer_controller_.SetLeConnectionIntervalMax(
1553 command_view.GetConnIntervalMax());
1554 link_layer_controller_.SetLeConnectionLatency(command_view.GetConnLatency());
1555 link_layer_controller_.SetLeSupervisionTimeout(
1556 command_view.GetSupervisionTimeout());
1557 link_layer_controller_.SetLeMinimumCeLength(
1558 command_view.GetMinimumCeLength());
1559 link_layer_controller_.SetLeMaximumCeLength(
1560 command_view.GetMaximumCeLength());
1561
1562 auto status = link_layer_controller_.SetLeConnect(true);
1563
1564 auto packet = bluetooth::hci::LeCreateConnectionStatusBuilder::Create(
1565 status, kNumCommandPackets);
1566 send_event_(std::move(packet));
1567 }
1568
LeConnectionUpdate(CommandPacketView command)1569 void DualModeController::LeConnectionUpdate(CommandPacketView command) {
1570 auto command_view = gd_hci::LeConnectionUpdateView::Create(
1571 gd_hci::LeConnectionManagementCommandView::Create(command));
1572 ASSERT(command_view.IsValid());
1573 ErrorCode status = link_layer_controller_.LeConnectionUpdate(command_view);
1574
1575 auto status_packet = bluetooth::hci::LeConnectionUpdateStatusBuilder::Create(
1576 status, kNumCommandPackets);
1577 send_event_(std::move(status_packet));
1578 }
1579
CreateConnection(CommandPacketView command)1580 void DualModeController::CreateConnection(CommandPacketView command) {
1581 auto command_view = gd_hci::CreateConnectionView::Create(
1582 gd_hci::ConnectionManagementCommandView::Create(command));
1583 ASSERT(command_view.IsValid());
1584
1585 Address address = command_view.GetBdAddr();
1586 uint16_t packet_type = command_view.GetPacketType();
1587 uint8_t page_scan_mode =
1588 static_cast<uint8_t>(command_view.GetPageScanRepetitionMode());
1589 uint16_t clock_offset =
1590 (command_view.GetClockOffsetValid() == gd_hci::ClockOffsetValid::VALID
1591 ? command_view.GetClockOffset()
1592 : 0);
1593 uint8_t allow_role_switch =
1594 static_cast<uint8_t>(command_view.GetAllowRoleSwitch());
1595
1596 auto status = link_layer_controller_.CreateConnection(
1597 address, packet_type, page_scan_mode, clock_offset, allow_role_switch);
1598
1599 auto packet = bluetooth::hci::CreateConnectionStatusBuilder::Create(
1600 status, kNumCommandPackets);
1601 send_event_(std::move(packet));
1602 }
1603
Disconnect(CommandPacketView command)1604 void DualModeController::Disconnect(CommandPacketView command) {
1605 auto command_view = gd_hci::DisconnectView::Create(
1606 gd_hci::ConnectionManagementCommandView::Create(command));
1607 ASSERT(command_view.IsValid());
1608
1609 uint16_t handle = command_view.GetConnectionHandle();
1610 uint8_t reason = static_cast<uint8_t>(command_view.GetReason());
1611
1612 auto status = link_layer_controller_.Disconnect(handle, reason);
1613
1614 auto packet = bluetooth::hci::DisconnectStatusBuilder::Create(
1615 status, kNumCommandPackets);
1616 send_event_(std::move(packet));
1617 }
1618
LeConnectionCancel(CommandPacketView command)1619 void DualModeController::LeConnectionCancel(CommandPacketView command) {
1620 auto command_view = gd_hci::LeCreateConnectionCancelView::Create(
1621 gd_hci::LeConnectionManagementCommandView::Create(command));
1622 ASSERT(command_view.IsValid());
1623 link_layer_controller_.SetLeConnect(false);
1624 auto packet = bluetooth::hci::LeCreateConnectionCancelCompleteBuilder::Create(
1625 kNumCommandPackets, ErrorCode::SUCCESS);
1626 send_event_(std::move(packet));
1627 /* For testing Jakub's patch: Figure out a neat way to call this without
1628 recompiling. I'm thinking about a bad device. */
1629 /*
1630 SendCommandCompleteOnlyStatus(OpCode::LE_CREATE_CONNECTION_CANCEL,
1631 ErrorCode::COMMAND_DISALLOWED);
1632 */
1633 }
1634
LeReadConnectListSize(CommandPacketView command)1635 void DualModeController::LeReadConnectListSize(CommandPacketView command) {
1636 auto command_view = gd_hci::LeReadConnectListSizeView::Create(
1637 gd_hci::LeConnectionManagementCommandView::Create(command));
1638 ASSERT(command_view.IsValid());
1639 auto packet = bluetooth::hci::LeReadConnectListSizeCompleteBuilder::Create(
1640 kNumCommandPackets, ErrorCode::SUCCESS,
1641 properties_.GetLeConnectListSize());
1642 send_event_(std::move(packet));
1643 }
1644
LeClearConnectList(CommandPacketView command)1645 void DualModeController::LeClearConnectList(CommandPacketView command) {
1646 auto command_view = gd_hci::LeClearConnectListView::Create(
1647 gd_hci::LeConnectionManagementCommandView::Create(command));
1648 ASSERT(command_view.IsValid());
1649 link_layer_controller_.LeConnectListClear();
1650 auto packet = bluetooth::hci::LeClearConnectListCompleteBuilder::Create(
1651 kNumCommandPackets, ErrorCode::SUCCESS);
1652 send_event_(std::move(packet));
1653 }
1654
LeAddDeviceToConnectList(CommandPacketView command)1655 void DualModeController::LeAddDeviceToConnectList(CommandPacketView command) {
1656 auto command_view = gd_hci::LeAddDeviceToConnectListView::Create(
1657 gd_hci::LeConnectionManagementCommandView::Create(command));
1658 ASSERT(command_view.IsValid());
1659
1660 if (link_layer_controller_.LeConnectListFull()) {
1661 auto packet =
1662 bluetooth::hci::LeAddDeviceToConnectListCompleteBuilder::Create(
1663 kNumCommandPackets, ErrorCode::MEMORY_CAPACITY_EXCEEDED);
1664 send_event_(std::move(packet));
1665 return;
1666 }
1667 uint8_t addr_type = static_cast<uint8_t>(command_view.GetAddressType());
1668 Address address = command_view.GetAddress();
1669 link_layer_controller_.LeConnectListAddDevice(address, addr_type);
1670 auto packet = bluetooth::hci::LeAddDeviceToConnectListCompleteBuilder::Create(
1671 kNumCommandPackets, ErrorCode::SUCCESS);
1672 send_event_(std::move(packet));
1673 }
1674
LeRemoveDeviceFromConnectList(CommandPacketView command)1675 void DualModeController::LeRemoveDeviceFromConnectList(
1676 CommandPacketView command) {
1677 auto command_view = gd_hci::LeRemoveDeviceFromConnectListView::Create(
1678 gd_hci::LeConnectionManagementCommandView::Create(command));
1679 ASSERT(command_view.IsValid());
1680
1681 uint8_t addr_type = static_cast<uint8_t>(command_view.GetAddressType());
1682 Address address = command_view.GetAddress();
1683 link_layer_controller_.LeConnectListRemoveDevice(address, addr_type);
1684 auto packet =
1685 bluetooth::hci::LeRemoveDeviceFromConnectListCompleteBuilder::Create(
1686 kNumCommandPackets, ErrorCode::SUCCESS);
1687 send_event_(std::move(packet));
1688 }
1689
LeClearResolvingList(CommandPacketView command)1690 void DualModeController::LeClearResolvingList(CommandPacketView command) {
1691 auto command_view = gd_hci::LeClearResolvingListView::Create(
1692 gd_hci::LeSecurityCommandView::Create(command));
1693 ASSERT(command_view.IsValid());
1694 link_layer_controller_.LeResolvingListClear();
1695 auto packet = bluetooth::hci::LeClearResolvingListCompleteBuilder::Create(
1696 kNumCommandPackets, ErrorCode::SUCCESS);
1697 send_event_(std::move(packet));
1698 }
1699
LeReadResolvingListSize(CommandPacketView command)1700 void DualModeController::LeReadResolvingListSize(CommandPacketView command) {
1701 auto command_view = gd_hci::LeReadResolvingListSizeView::Create(
1702 gd_hci::LeSecurityCommandView::Create(command));
1703 ASSERT(command_view.IsValid());
1704 auto packet = bluetooth::hci::LeReadResolvingListSizeCompleteBuilder::Create(
1705 kNumCommandPackets, ErrorCode::SUCCESS,
1706 properties_.GetLeResolvingListSize());
1707 send_event_(std::move(packet));
1708 }
1709
LeReadMaximumDataLength(CommandPacketView command)1710 void DualModeController::LeReadMaximumDataLength(CommandPacketView command) {
1711 auto command_view = gd_hci::LeReadMaximumDataLengthView::Create(
1712 gd_hci::LeSecurityCommandView::Create(command));
1713 ASSERT(command_view.IsValid());
1714 bluetooth::hci::LeMaximumDataLength data_length;
1715 data_length.supported_max_rx_octets_ = kLeMaximumDataLength;
1716 data_length.supported_max_rx_time_ = kLeMaximumDataTime;
1717 data_length.supported_max_tx_octets_ = kLeMaximumDataLength + 10;
1718 data_length.supported_max_tx_time_ = kLeMaximumDataTime + 10;
1719 send_event_(bluetooth::hci::LeReadMaximumDataLengthCompleteBuilder::Create(
1720 kNumCommandPackets, ErrorCode::SUCCESS, data_length));
1721 }
1722
LeAddDeviceToResolvingList(CommandPacketView command)1723 void DualModeController::LeAddDeviceToResolvingList(CommandPacketView command) {
1724 auto command_view = gd_hci::LeAddDeviceToResolvingListView::Create(
1725 gd_hci::LeSecurityCommandView::Create(command));
1726 ASSERT(command_view.IsValid());
1727
1728 if (link_layer_controller_.LeResolvingListFull()) {
1729 auto packet =
1730 bluetooth::hci::LeAddDeviceToResolvingListCompleteBuilder::Create(
1731 kNumCommandPackets, ErrorCode::MEMORY_CAPACITY_EXCEEDED);
1732 send_event_(std::move(packet));
1733 return;
1734 }
1735 uint8_t addr_type =
1736 static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
1737 Address address = command_view.GetPeerIdentityAddress();
1738 std::array<uint8_t, LinkLayerController::kIrk_size> peerIrk =
1739 command_view.GetPeerIrk();
1740 std::array<uint8_t, LinkLayerController::kIrk_size> localIrk =
1741 command_view.GetLocalIrk();
1742
1743 link_layer_controller_.LeResolvingListAddDevice(address, addr_type, peerIrk,
1744 localIrk);
1745 auto packet =
1746 bluetooth::hci::LeAddDeviceToResolvingListCompleteBuilder::Create(
1747 kNumCommandPackets, ErrorCode::SUCCESS);
1748 send_event_(std::move(packet));
1749 }
1750
LeRemoveDeviceFromResolvingList(CommandPacketView command)1751 void DualModeController::LeRemoveDeviceFromResolvingList(
1752 CommandPacketView command) {
1753 auto command_view = gd_hci::LeRemoveDeviceFromResolvingListView::Create(
1754 gd_hci::LeSecurityCommandView::Create(command));
1755 ASSERT(command_view.IsValid());
1756
1757 uint8_t addr_type =
1758 static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
1759 Address address = command_view.GetPeerIdentityAddress();
1760 link_layer_controller_.LeResolvingListRemoveDevice(address, addr_type);
1761 auto packet =
1762 bluetooth::hci::LeRemoveDeviceFromResolvingListCompleteBuilder::Create(
1763 kNumCommandPackets, ErrorCode::SUCCESS);
1764 send_event_(std::move(packet));
1765 }
1766
LeSetExtendedScanParameters(CommandPacketView command)1767 void DualModeController::LeSetExtendedScanParameters(
1768 CommandPacketView command) {
1769 auto command_view = gd_hci::LeSetExtendedScanParametersView::Create(
1770 gd_hci::LeScanningCommandView::Create(command));
1771 ASSERT(command_view.IsValid());
1772 auto parameters = command_view.GetParameters();
1773 // Multiple phys are not supported.
1774 ASSERT(command_view.GetScanningPhys() == 1);
1775 ASSERT(parameters.size() == 1);
1776
1777 link_layer_controller_.SetLeScanType(
1778 static_cast<uint8_t>(parameters[0].le_scan_type_));
1779 link_layer_controller_.SetLeScanInterval(parameters[0].le_scan_interval_);
1780 link_layer_controller_.SetLeScanWindow(parameters[0].le_scan_window_);
1781 link_layer_controller_.SetLeAddressType(
1782 static_cast<uint8_t>(command_view.GetOwnAddressType()));
1783 link_layer_controller_.SetLeScanFilterPolicy(
1784 static_cast<uint8_t>(command_view.GetScanningFilterPolicy()));
1785 auto packet =
1786 bluetooth::hci::LeSetExtendedScanParametersCompleteBuilder::Create(
1787 kNumCommandPackets, ErrorCode::SUCCESS);
1788 send_event_(std::move(packet));
1789 }
1790
LeSetExtendedScanEnable(CommandPacketView command)1791 void DualModeController::LeSetExtendedScanEnable(CommandPacketView command) {
1792 auto command_view = gd_hci::LeSetExtendedScanEnableView::Create(
1793 gd_hci::LeScanningCommandView::Create(command));
1794 ASSERT(command_view.IsValid());
1795 if (command_view.GetEnable() == gd_hci::Enable::ENABLED) {
1796 link_layer_controller_.SetLeScanEnable(
1797 gd_hci::OpCode::LE_SET_EXTENDED_SCAN_ENABLE);
1798 } else {
1799 link_layer_controller_.SetLeScanEnable(gd_hci::OpCode::NONE);
1800 }
1801 link_layer_controller_.SetLeFilterDuplicates(
1802 command_view.GetFilterDuplicates() == gd_hci::FilterDuplicates::ENABLED);
1803 auto packet = bluetooth::hci::LeSetExtendedScanEnableCompleteBuilder::Create(
1804 kNumCommandPackets, ErrorCode::SUCCESS);
1805 send_event_(std::move(packet));
1806 }
1807
LeExtendedCreateConnection(CommandPacketView command)1808 void DualModeController::LeExtendedCreateConnection(CommandPacketView command) {
1809 auto command_view = gd_hci::LeExtendedCreateConnectionView::Create(
1810 gd_hci::LeConnectionManagementCommandView::Create(command));
1811 ASSERT(command_view.IsValid());
1812 ASSERT_LOG(command_view.GetInitiatingPhys() == 1, "Only LE_1M is supported");
1813 auto params = command_view.GetPhyScanParameters();
1814 link_layer_controller_.SetLeScanInterval(params[0].scan_interval_);
1815 link_layer_controller_.SetLeScanWindow(params[0].scan_window_);
1816 auto initiator_filter_policy = command_view.GetInitiatorFilterPolicy();
1817 link_layer_controller_.SetLeInitiatorFilterPolicy(
1818 static_cast<uint8_t>(initiator_filter_policy));
1819
1820 if (initiator_filter_policy ==
1821 gd_hci::InitiatorFilterPolicy::USE_PEER_ADDRESS) {
1822 link_layer_controller_.SetLePeerAddressType(
1823 static_cast<uint8_t>(command_view.GetPeerAddressType()));
1824 link_layer_controller_.SetLePeerAddress(command_view.GetPeerAddress());
1825 }
1826 link_layer_controller_.SetLeAddressType(
1827 static_cast<uint8_t>(command_view.GetOwnAddressType()));
1828 link_layer_controller_.SetLeConnectionIntervalMin(
1829 params[0].conn_interval_min_);
1830 link_layer_controller_.SetLeConnectionIntervalMax(
1831 params[0].conn_interval_max_);
1832 link_layer_controller_.SetLeConnectionLatency(params[0].conn_latency_);
1833 link_layer_controller_.SetLeSupervisionTimeout(
1834 params[0].supervision_timeout_);
1835 link_layer_controller_.SetLeMinimumCeLength(params[0].min_ce_length_);
1836 link_layer_controller_.SetLeMaximumCeLength(params[0].max_ce_length_);
1837
1838 auto status = link_layer_controller_.SetLeConnect(true);
1839
1840 send_event_(bluetooth::hci::LeExtendedCreateConnectionStatusBuilder::Create(
1841 status, kNumCommandPackets));
1842 }
1843
LeSetPrivacyMode(CommandPacketView command)1844 void DualModeController::LeSetPrivacyMode(CommandPacketView command) {
1845 auto command_view = gd_hci::LeSetPrivacyModeView::Create(
1846 gd_hci::LeSecurityCommandView::Create(command));
1847 ASSERT(command_view.IsValid());
1848
1849 uint8_t peer_identity_address_type =
1850 static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
1851 Address peer_identity_address = command_view.GetPeerIdentityAddress();
1852 uint8_t privacy_mode = static_cast<uint8_t>(command_view.GetPrivacyMode());
1853
1854 if (link_layer_controller_.LeResolvingListContainsDevice(
1855 peer_identity_address, peer_identity_address_type)) {
1856 link_layer_controller_.LeSetPrivacyMode(
1857 peer_identity_address_type, peer_identity_address, privacy_mode);
1858 }
1859
1860 auto packet = bluetooth::hci::LeSetPrivacyModeCompleteBuilder::Create(
1861 kNumCommandPackets, ErrorCode::SUCCESS);
1862 send_event_(std::move(packet));
1863 }
1864
LeReadRemoteFeatures(CommandPacketView command)1865 void DualModeController::LeReadRemoteFeatures(CommandPacketView command) {
1866 auto command_view = gd_hci::LeReadRemoteFeaturesView::Create(
1867 gd_hci::LeConnectionManagementCommandView::Create(command));
1868 ASSERT(command_view.IsValid());
1869
1870 uint16_t handle = command_view.GetConnectionHandle();
1871
1872 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
1873 OpCode::LE_READ_REMOTE_FEATURES, command_view.GetPayload(), handle);
1874
1875 auto packet = bluetooth::hci::LeConnectionUpdateStatusBuilder::Create(
1876 status, kNumCommandPackets);
1877 send_event_(std::move(packet));
1878 }
1879
LeRand(CommandPacketView command)1880 void DualModeController::LeRand(CommandPacketView command) {
1881 auto command_view = gd_hci::LeRandView::Create(
1882 gd_hci::LeSecurityCommandView::Create(command));
1883 ASSERT(command_view.IsValid());
1884 uint64_t random_val = 0;
1885 for (size_t rand_bytes = 0; rand_bytes < sizeof(uint64_t); rand_bytes += sizeof(RAND_MAX)) {
1886 random_val = (random_val << (8 * sizeof(RAND_MAX))) | random();
1887 }
1888
1889 auto packet = bluetooth::hci::LeRandCompleteBuilder::Create(
1890 kNumCommandPackets, ErrorCode::SUCCESS, random_val);
1891 send_event_(std::move(packet));
1892 }
1893
LeReadSupportedStates(CommandPacketView command)1894 void DualModeController::LeReadSupportedStates(CommandPacketView command) {
1895 auto command_view = gd_hci::LeReadSupportedStatesView::Create(command);
1896 ASSERT(command_view.IsValid());
1897 auto packet = bluetooth::hci::LeReadSupportedStatesCompleteBuilder::Create(
1898 kNumCommandPackets, ErrorCode::SUCCESS,
1899 properties_.GetLeSupportedStates());
1900 send_event_(std::move(packet));
1901 }
1902
LeVendorCap(CommandPacketView command)1903 void DualModeController::LeVendorCap(CommandPacketView command) {
1904 auto command_view = gd_hci::LeGetVendorCapabilitiesView::Create(
1905 gd_hci::VendorCommandView::Create(command));
1906 ASSERT(command_view.IsValid());
1907 vector<uint8_t> caps = properties_.GetLeVendorCap();
1908 if (caps.size() == 0) {
1909 SendCommandCompleteUnknownOpCodeEvent(
1910 static_cast<uint16_t>(OpCode::LE_GET_VENDOR_CAPABILITIES));
1911 return;
1912 }
1913
1914 std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
1915 std::make_unique<bluetooth::packet::RawBuilder>();
1916 raw_builder_ptr->AddOctets1(static_cast<uint8_t>(ErrorCode::SUCCESS));
1917 raw_builder_ptr->AddOctets(properties_.GetLeVendorCap());
1918
1919 auto packet = bluetooth::hci::CommandCompleteBuilder::Create(
1920 kNumCommandPackets, OpCode::LE_GET_VENDOR_CAPABILITIES,
1921 std::move(raw_builder_ptr));
1922 send_event_(std::move(packet));
1923 }
1924
LeVendorMultiAdv(CommandPacketView command)1925 void DualModeController::LeVendorMultiAdv(CommandPacketView command) {
1926 auto command_view = gd_hci::LeMultiAdvtView::Create(
1927 gd_hci::LeAdvertisingCommandView::Create(command));
1928 ASSERT(command_view.IsValid());
1929 SendCommandCompleteUnknownOpCodeEvent(
1930 static_cast<uint16_t>(OpCode::LE_MULTI_ADVT));
1931 }
1932
LeAdvertisingFilter(CommandPacketView command)1933 void DualModeController::LeAdvertisingFilter(CommandPacketView command) {
1934 auto command_view = gd_hci::LeAdvFilterView::Create(
1935 gd_hci::LeScanningCommandView::Create(command));
1936 ASSERT(command_view.IsValid());
1937 SendCommandCompleteUnknownOpCodeEvent(
1938 static_cast<uint16_t>(OpCode::LE_ADV_FILTER));
1939 }
1940
LeEnergyInfo(CommandPacketView command)1941 void DualModeController::LeEnergyInfo(CommandPacketView command) {
1942 auto command_view = gd_hci::LeEnergyInfoView::Create(
1943 gd_hci::VendorCommandView::Create(command));
1944 ASSERT(command_view.IsValid());
1945 SendCommandCompleteUnknownOpCodeEvent(
1946 static_cast<uint16_t>(OpCode::LE_ENERGY_INFO));
1947 }
1948
LeSetExtendedAdvertisingRandomAddress(CommandPacketView command)1949 void DualModeController::LeSetExtendedAdvertisingRandomAddress(
1950 CommandPacketView command) {
1951 auto command_view = gd_hci::LeSetExtendedAdvertisingRandomAddressView::Create(
1952 gd_hci::LeAdvertisingCommandView::Create(command));
1953 ASSERT(command_view.IsValid());
1954 link_layer_controller_.SetLeExtendedAddress(
1955 command_view.GetAdvertisingHandle(),
1956 command_view.GetAdvertisingRandomAddress());
1957 send_event_(
1958 bluetooth::hci::LeSetExtendedAdvertisingRandomAddressCompleteBuilder::
1959 Create(kNumCommandPackets, ErrorCode::SUCCESS));
1960 }
1961
LeSetExtendedAdvertisingParameters(CommandPacketView command)1962 void DualModeController::LeSetExtendedAdvertisingParameters(
1963 CommandPacketView command) {
1964 auto command_view =
1965 gd_hci::LeSetExtendedAdvertisingLegacyParametersView::Create(
1966 gd_hci::LeAdvertisingCommandView::Create(command));
1967 // TODO: Support non-legacy parameters
1968 ASSERT(command_view.IsValid());
1969 link_layer_controller_.SetLeExtendedAdvertisingParameters(
1970 command_view.GetAdvertisingHandle(),
1971 command_view.GetPrimaryAdvertisingIntervalMin(),
1972 command_view.GetPrimaryAdvertisingIntervalMax(),
1973 command_view.GetAdvertisingEventLegacyProperties(),
1974 command_view.GetOwnAddressType(), command_view.GetPeerAddressType(),
1975 command_view.GetPeerAddress(), command_view.GetAdvertisingFilterPolicy());
1976
1977 send_event_(
1978 bluetooth::hci::LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
1979 kNumCommandPackets, ErrorCode::SUCCESS, 0xa5));
1980 }
1981
LeSetExtendedAdvertisingData(CommandPacketView command)1982 void DualModeController::LeSetExtendedAdvertisingData(
1983 CommandPacketView command) {
1984 auto command_view = gd_hci::LeSetExtendedAdvertisingDataView::Create(
1985 gd_hci::LeAdvertisingCommandView::Create(command));
1986 ASSERT(command_view.IsValid());
1987 auto raw_command_view = gd_hci::LeSetExtendedAdvertisingDataRawView::Create(
1988 gd_hci::LeAdvertisingCommandView::Create(command));
1989 ASSERT(raw_command_view.IsValid());
1990 link_layer_controller_.SetLeExtendedAdvertisingData(
1991 command_view.GetAdvertisingHandle(),
1992 raw_command_view.GetAdvertisingData());
1993 auto packet =
1994 bluetooth::hci::LeSetExtendedAdvertisingDataCompleteBuilder::Create(
1995 kNumCommandPackets, ErrorCode::SUCCESS);
1996 send_event_(std::move(packet));
1997 }
1998
LeSetExtendedAdvertisingScanResponse(CommandPacketView command)1999 void DualModeController::LeSetExtendedAdvertisingScanResponse(
2000 CommandPacketView command) {
2001 auto command_view = gd_hci::LeSetExtendedAdvertisingScanResponseView::Create(
2002 gd_hci::LeAdvertisingCommandView::Create(command));
2003 ASSERT(command_view.IsValid());
2004 properties_.SetLeScanResponse(std::vector<uint8_t>(
2005 command_view.GetPayload().begin() + 1, command_view.GetPayload().end()));
2006 send_event_(
2007 bluetooth::hci::LeSetExtendedAdvertisingScanResponseCompleteBuilder::
2008 Create(kNumCommandPackets, ErrorCode::SUCCESS));
2009 }
2010
LeSetExtendedAdvertisingEnable(CommandPacketView command)2011 void DualModeController::LeSetExtendedAdvertisingEnable(
2012 CommandPacketView command) {
2013 auto command_view = gd_hci::LeSetExtendedAdvertisingEnableView::Create(
2014 gd_hci::LeAdvertisingCommandView::Create(command));
2015 ASSERT(command_view.IsValid());
2016 auto enabled_sets = command_view.GetEnabledSets();
2017 ErrorCode status = ErrorCode::SUCCESS;
2018 if (enabled_sets.size() == 0) {
2019 link_layer_controller_.LeDisableAdvertisingSets();
2020 } else {
2021 status = link_layer_controller_.SetLeExtendedAdvertisingEnable(
2022 command_view.GetEnable(), command_view.GetEnabledSets());
2023 }
2024 send_event_(
2025 bluetooth::hci::LeSetExtendedAdvertisingEnableCompleteBuilder::Create(
2026 kNumCommandPackets, status));
2027 }
2028
LeReadMaximumAdvertisingDataLength(CommandPacketView command)2029 void DualModeController::LeReadMaximumAdvertisingDataLength(
2030 CommandPacketView command) {
2031 auto command_view = gd_hci::LeReadMaximumAdvertisingDataLengthView::Create(
2032 gd_hci::LeAdvertisingCommandView::Create(command));
2033 ASSERT(command_view.IsValid());
2034 send_event_(
2035 bluetooth::hci::LeReadMaximumAdvertisingDataLengthCompleteBuilder::Create(
2036 kNumCommandPackets, ErrorCode::SUCCESS,
2037 kLeMaximumAdvertisingDataLength));
2038 }
2039
LeReadNumberOfSupportedAdvertisingSets(CommandPacketView command)2040 void DualModeController::LeReadNumberOfSupportedAdvertisingSets(
2041 CommandPacketView command) {
2042 auto command_view =
2043 gd_hci::LeReadNumberOfSupportedAdvertisingSetsView::Create(
2044 gd_hci::LeAdvertisingCommandView::Create(command));
2045 ASSERT(command_view.IsValid());
2046 send_event_(
2047 bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteBuilder::
2048 Create(
2049 kNumCommandPackets, ErrorCode::SUCCESS,
2050 link_layer_controller_.LeReadNumberOfSupportedAdvertisingSets()));
2051 }
2052
LeRemoveAdvertisingSet(CommandPacketView command)2053 void DualModeController::LeRemoveAdvertisingSet(CommandPacketView command) {
2054 auto command_view = gd_hci::LeRemoveAdvertisingSetView::Create(
2055 gd_hci::LeAdvertisingCommandView::Create(command));
2056 ASSERT(command_view.IsValid());
2057 auto status = link_layer_controller_.LeRemoveAdvertisingSet(
2058 command_view.GetAdvertisingHandle());
2059 send_event_(bluetooth::hci::LeRemoveAdvertisingSetCompleteBuilder::Create(
2060 kNumCommandPackets, status));
2061 }
2062
LeClearAdvertisingSets(CommandPacketView command)2063 void DualModeController::LeClearAdvertisingSets(CommandPacketView command) {
2064 auto command_view = gd_hci::LeClearAdvertisingSetsView::Create(
2065 gd_hci::LeAdvertisingCommandView::Create(command));
2066 ASSERT(command_view.IsValid());
2067 auto status = link_layer_controller_.LeClearAdvertisingSets();
2068 send_event_(bluetooth::hci::LeClearAdvertisingSetsCompleteBuilder::Create(
2069 kNumCommandPackets, status));
2070 }
2071
LeExtendedScanParams(CommandPacketView command)2072 void DualModeController::LeExtendedScanParams(CommandPacketView command) {
2073 auto command_view = gd_hci::LeExtendedScanParamsView::Create(
2074 gd_hci::LeScanningCommandView::Create(command));
2075 ASSERT(command_view.IsValid());
2076 SendCommandCompleteUnknownOpCodeEvent(
2077 static_cast<uint16_t>(OpCode::LE_EXTENDED_SCAN_PARAMS));
2078 }
2079
LeStartEncryption(CommandPacketView command)2080 void DualModeController::LeStartEncryption(CommandPacketView command) {
2081 auto command_view = gd_hci::LeStartEncryptionView::Create(
2082 gd_hci::LeSecurityCommandView::Create(command));
2083 ASSERT(command_view.IsValid());
2084
2085 ErrorCode status = link_layer_controller_.LeEnableEncryption(
2086 command_view.GetConnectionHandle(), command_view.GetRand(),
2087 command_view.GetEdiv(), command_view.GetLtk());
2088
2089 send_event_(bluetooth::hci::LeStartEncryptionStatusBuilder::Create(
2090 status, kNumCommandPackets));
2091 }
2092
ReadLoopbackMode(CommandPacketView command)2093 void DualModeController::ReadLoopbackMode(CommandPacketView command) {
2094 auto command_view = gd_hci::ReadLoopbackModeView::Create(command);
2095 ASSERT(command_view.IsValid());
2096 auto packet = bluetooth::hci::ReadLoopbackModeCompleteBuilder::Create(
2097 kNumCommandPackets, ErrorCode::SUCCESS,
2098 static_cast<LoopbackMode>(loopback_mode_));
2099 send_event_(std::move(packet));
2100 }
2101
WriteLoopbackMode(CommandPacketView command)2102 void DualModeController::WriteLoopbackMode(CommandPacketView command) {
2103 auto command_view = gd_hci::WriteLoopbackModeView::Create(command);
2104 ASSERT(command_view.IsValid());
2105 loopback_mode_ = command_view.GetLoopbackMode();
2106 // ACL channel
2107 uint16_t acl_handle = 0x123;
2108 auto packet_acl = bluetooth::hci::ConnectionCompleteBuilder::Create(
2109 ErrorCode::SUCCESS, acl_handle, properties_.GetAddress(),
2110 bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED);
2111 send_event_(std::move(packet_acl));
2112 // SCO channel
2113 uint16_t sco_handle = 0x345;
2114 auto packet_sco = bluetooth::hci::ConnectionCompleteBuilder::Create(
2115 ErrorCode::SUCCESS, sco_handle, properties_.GetAddress(),
2116 bluetooth::hci::LinkType::SCO, bluetooth::hci::Enable::DISABLED);
2117 send_event_(std::move(packet_sco));
2118 auto packet = bluetooth::hci::WriteLoopbackModeCompleteBuilder::Create(
2119 kNumCommandPackets, ErrorCode::SUCCESS);
2120 send_event_(std::move(packet));
2121 }
2122
SetAddress(Address address)2123 void DualModeController::SetAddress(Address address) {
2124 properties_.SetAddress(address);
2125 }
2126
2127 } // namespace test_vendor_lib
2128