• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "dual_mode_controller"
18 
19 #include "dual_mode_controller.h"
20 
21 #include <memory>
22 
23 #include <base/files/file_util.h>
24 #include <base/json/json_reader.h>
25 #include <base/logging.h>
26 #include <base/values.h>
27 
28 #include "osi/include/log.h"
29 #include "osi/include/osi.h"
30 
31 #include "hci.h"
32 #include "packets/hci/acl_packet_view.h"
33 #include "packets/hci/command_packet_view.h"
34 #include "packets/hci/event_packet_builder.h"
35 #include "packets/hci/sco_packet_view.h"
36 
37 using std::vector;
38 using test_vendor_lib::hci::EventCode;
39 using test_vendor_lib::hci::OpCode;
40 
41 namespace {
42 
LastNonZero(test_vendor_lib::packets::PacketView<true> view)43 size_t LastNonZero(test_vendor_lib::packets::PacketView<true> view) {
44   for (size_t i = view.size() - 1; i > 0; i--) {
45     if (view[i] != 0) {
46       return i;
47     }
48   }
49   return 0;
50 }
51 
52 }  // namespace
53 
54 namespace test_vendor_lib {
55 constexpr char DualModeController::kControllerPropertiesFile[];
56 constexpr uint16_t DualModeController::kSecurityManagerNumKeys;
57 
58 // Device methods.
Initialize(const std::vector<std::string> & args)59 void DualModeController::Initialize(const std::vector<std::string>& args) {
60   if (args.size() < 2) return;
61 
62   Address addr;
63   if (Address::FromString(args[1], addr)) properties_.SetAddress(addr);
64 };
65 
GetTypeString() const66 std::string DualModeController::GetTypeString() const {
67   return "Simulated Bluetooth Controller";
68 }
69 
IncomingPacket(packets::LinkLayerPacketView incoming)70 void DualModeController::IncomingPacket(packets::LinkLayerPacketView incoming) {
71   link_layer_controller_.IncomingPacket(incoming);
72 }
73 
TimerTick()74 void DualModeController::TimerTick() {
75   link_layer_controller_.TimerTick();
76 }
77 
SendLinkLayerPacket(std::shared_ptr<packets::LinkLayerPacketBuilder> to_send,Phy::Type phy_type)78 void DualModeController::SendLinkLayerPacket(std::shared_ptr<packets::LinkLayerPacketBuilder> to_send,
79                                              Phy::Type phy_type) {
80   for (auto phy_pair : phy_layers_) {
81     auto phy_list = std::get<1>(phy_pair);
82     if (phy_type != std::get<0>(phy_pair)) {
83       continue;
84     }
85     for (auto phy : phy_list) {
86       phy->Send(to_send);
87     }
88   }
89 }
90 
91 /*
92 void DualModeController::AddConnectionAction(const TaskCallback& task,
93                                              uint16_t handle) {
94   for (size_t i = 0; i < connections_.size(); i++)
95     if (connections_[i]->GetHandle() == handle)
96 connections_[i]->AddAction(task);
97 }
98 */
99 
SendCommandCompleteSuccess(OpCode command_opcode) const100 void DualModeController::SendCommandCompleteSuccess(OpCode command_opcode) const {
101   send_event_(packets::EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(command_opcode, hci::Status::SUCCESS)
102                   ->ToVector());
103 }
104 
SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const105 void DualModeController::SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const {
106   send_event_(packets::EventPacketBuilder::CreateCommandCompleteUnknownOpCodeEvent(command_opcode)->ToVector());
107 }
108 
SendCommandCompleteOnlyStatus(OpCode command_opcode,hci::Status status) const109 void DualModeController::SendCommandCompleteOnlyStatus(OpCode command_opcode, hci::Status status) const {
110   send_event_(packets::EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(command_opcode, status)->ToVector());
111 }
112 
SendCommandCompleteStatusAndAddress(OpCode command_opcode,hci::Status status,const Address & address) const113 void DualModeController::SendCommandCompleteStatusAndAddress(OpCode command_opcode, hci::Status status,
114                                                              const Address& address) const {
115   send_event_(packets::EventPacketBuilder::CreateCommandCompleteStatusAndAddressEvent(command_opcode, status, address)
116                   ->ToVector());
117 }
118 
SendCommandStatus(hci::Status status,OpCode command_opcode) const119 void DualModeController::SendCommandStatus(hci::Status status, OpCode command_opcode) const {
120   send_event_(packets::EventPacketBuilder::CreateCommandStatusEvent(status, command_opcode)->ToVector());
121 }
122 
SendCommandStatusSuccess(OpCode command_opcode) const123 void DualModeController::SendCommandStatusSuccess(OpCode command_opcode) const {
124   SendCommandStatus(hci::Status::SUCCESS, command_opcode);
125 }
126 
DualModeController(const std::string & properties_filename,uint16_t num_keys)127 DualModeController::DualModeController(const std::string& properties_filename, uint16_t num_keys)
128     : Device(properties_filename), security_manager_(num_keys) {
129   loopback_mode_ = hci::LoopbackMode::NO;
130 
131   Address public_address;
132   CHECK(Address::FromString("3C:5A:B4:04:05:06", public_address));
133   properties_.SetAddress(public_address);
134 
135   link_layer_controller_.RegisterRemoteChannel(
136       [this](std::shared_ptr<packets::LinkLayerPacketBuilder> packet, Phy::Type phy_type) {
137         DualModeController::SendLinkLayerPacket(packet, phy_type);
138       });
139 
140 #define SET_HANDLER(opcode, method) \
141   active_hci_commands_[static_cast<uint16_t>(opcode)] = [this](packets::PacketView<true> param) { method(param); };
142   SET_HANDLER(OpCode::RESET, HciReset);
143   SET_HANDLER(OpCode::READ_BUFFER_SIZE, HciReadBufferSize);
144   SET_HANDLER(OpCode::HOST_BUFFER_SIZE, HciHostBufferSize);
145   SET_HANDLER(OpCode::SNIFF_SUBRATING, HciSniffSubrating);
146   SET_HANDLER(OpCode::READ_LOCAL_VERSION_INFORMATION, HciReadLocalVersionInformation);
147   SET_HANDLER(OpCode::READ_BD_ADDR, HciReadBdAddr);
148   SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_COMMANDS, HciReadLocalSupportedCommands);
149   SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_CODECS, HciReadLocalSupportedCodecs);
150   SET_HANDLER(OpCode::READ_LOCAL_EXTENDED_FEATURES, HciReadLocalExtendedFeatures);
151   SET_HANDLER(OpCode::READ_REMOTE_EXTENDED_FEATURES, HciReadRemoteExtendedFeatures);
152   SET_HANDLER(OpCode::READ_REMOTE_SUPPORTED_FEATURES, HciReadRemoteSupportedFeatures);
153   SET_HANDLER(OpCode::READ_CLOCK_OFFSET, HciReadClockOffset);
154   SET_HANDLER(OpCode::IO_CAPABILITY_REQUEST_REPLY, HciIoCapabilityRequestReply);
155   SET_HANDLER(OpCode::USER_CONFIRMATION_REQUEST_REPLY, HciUserConfirmationRequestReply);
156   SET_HANDLER(OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY, HciUserConfirmationRequestNegativeReply);
157   SET_HANDLER(OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY, HciIoCapabilityRequestNegativeReply);
158   SET_HANDLER(OpCode::WRITE_SIMPLE_PAIRING_MODE, HciWriteSimplePairingMode);
159   SET_HANDLER(OpCode::WRITE_LE_HOST_SUPPORT, HciWriteLeHostSupport);
160   SET_HANDLER(OpCode::SET_EVENT_MASK, HciSetEventMask);
161   SET_HANDLER(OpCode::WRITE_INQUIRY_MODE, HciWriteInquiryMode);
162   SET_HANDLER(OpCode::WRITE_PAGE_SCAN_TYPE, HciWritePageScanType);
163   SET_HANDLER(OpCode::WRITE_INQUIRY_SCAN_TYPE, HciWriteInquiryScanType);
164   SET_HANDLER(OpCode::AUTHENTICATION_REQUESTED, HciAuthenticationRequested);
165   SET_HANDLER(OpCode::SET_CONNECTION_ENCRYPTION, HciSetConnectionEncryption);
166   SET_HANDLER(OpCode::WRITE_AUTHENTICATION_ENABLE, HciWriteAuthenticationEnable);
167   SET_HANDLER(OpCode::READ_AUTHENTICATION_ENABLE, HciReadAuthenticationEnable);
168   SET_HANDLER(OpCode::WRITE_CLASS_OF_DEVICE, HciWriteClassOfDevice);
169   SET_HANDLER(OpCode::WRITE_PAGE_TIMEOUT, HciWritePageTimeout);
170   SET_HANDLER(OpCode::WRITE_LINK_SUPERVISION_TIMEOUT, HciWriteLinkSupervisionTimeout);
171   SET_HANDLER(OpCode::WRITE_DEFAULT_LINK_POLICY_SETTINGS, HciWriteDefaultLinkPolicySettings);
172   SET_HANDLER(OpCode::WRITE_LINK_POLICY_SETTINGS, HciWriteLinkPolicySettings);
173   SET_HANDLER(OpCode::CHANGE_CONNECTION_PACKET_TYPE, HciChangeConnectionPacketType);
174   SET_HANDLER(OpCode::WRITE_LOCAL_NAME, HciWriteLocalName);
175   SET_HANDLER(OpCode::READ_LOCAL_NAME, HciReadLocalName);
176   SET_HANDLER(OpCode::WRITE_EXTENDED_INQUIRY_RESPONSE, HciWriteExtendedInquiryResponse);
177   SET_HANDLER(OpCode::WRITE_VOICE_SETTING, HciWriteVoiceSetting);
178   SET_HANDLER(OpCode::WRITE_CURRENT_IAC_LAP, HciWriteCurrentIacLap);
179   SET_HANDLER(OpCode::WRITE_INQUIRY_SCAN_ACTIVITY, HciWriteInquiryScanActivity);
180   SET_HANDLER(OpCode::WRITE_SCAN_ENABLE, HciWriteScanEnable);
181   SET_HANDLER(OpCode::SET_EVENT_FILTER, HciSetEventFilter);
182   SET_HANDLER(OpCode::INQUIRY, HciInquiry);
183   SET_HANDLER(OpCode::INQUIRY_CANCEL, HciInquiryCancel);
184   SET_HANDLER(OpCode::ACCEPT_CONNECTION_REQUEST, HciAcceptConnectionRequest);
185   SET_HANDLER(OpCode::REJECT_CONNECTION_REQUEST, HciRejectConnectionRequest);
186   SET_HANDLER(OpCode::LINK_KEY_REQUEST_REPLY, HciLinkKeyRequestReply);
187   SET_HANDLER(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, HciLinkKeyRequestNegativeReply);
188   SET_HANDLER(OpCode::DELETE_STORED_LINK_KEY, HciDeleteStoredLinkKey);
189   SET_HANDLER(OpCode::REMOTE_NAME_REQUEST, HciRemoteNameRequest);
190   SET_HANDLER(OpCode::LE_SET_EVENT_MASK, HciLeSetEventMask);
191   SET_HANDLER(OpCode::LE_READ_BUFFER_SIZE, HciLeReadBufferSize);
192   SET_HANDLER(OpCode::LE_READ_LOCAL_SUPPORTED_FEATURES, HciLeReadLocalSupportedFeatures);
193   SET_HANDLER(OpCode::LE_SET_RANDOM_ADDRESS, HciLeSetRandomAddress);
194   SET_HANDLER(OpCode::LE_SET_ADVERTISING_DATA, HciLeSetAdvertisingData);
195   SET_HANDLER(OpCode::LE_SET_ADVERTISING_PARAMETERS, HciLeSetAdvertisingParameters);
196   SET_HANDLER(OpCode::LE_SET_SCAN_PARAMETERS, HciLeSetScanParameters);
197   SET_HANDLER(OpCode::LE_SET_SCAN_ENABLE, HciLeSetScanEnable);
198   SET_HANDLER(OpCode::LE_CREATE_CONNECTION, HciLeCreateConnection);
199   SET_HANDLER(OpCode::CREATE_CONNECTION, HciCreateConnection);
200   SET_HANDLER(OpCode::DISCONNECT, HciDisconnect);
201   SET_HANDLER(OpCode::LE_CREATE_CONNECTION_CANCEL, HciLeConnectionCancel);
202   SET_HANDLER(OpCode::LE_READ_WHITE_LIST_SIZE, HciLeReadWhiteListSize);
203   SET_HANDLER(OpCode::LE_CLEAR_WHITE_LIST, HciLeClearWhiteList);
204   SET_HANDLER(OpCode::LE_ADD_DEVICE_TO_WHITE_LIST, HciLeAddDeviceToWhiteList);
205   SET_HANDLER(OpCode::LE_REMOVE_DEVICE_FROM_WHITE_LIST, HciLeRemoveDeviceFromWhiteList);
206   SET_HANDLER(OpCode::LE_RAND, HciLeRand);
207   SET_HANDLER(OpCode::LE_READ_SUPPORTED_STATES, HciLeReadSupportedStates);
208   SET_HANDLER(OpCode::LE_GET_VENDOR_CAPABILITIES, HciLeVendorCap);
209   SET_HANDLER(OpCode::LE_MULTI_ADVT, HciLeVendorMultiAdv);
210   SET_HANDLER(OpCode::LE_ADV_FILTER, HciLeAdvertisingFilter);
211   SET_HANDLER(OpCode::LE_ENERGY_INFO, HciLeEnergyInfo);
212   SET_HANDLER(OpCode::LE_EXTENDED_SCAN_PARAMS, HciLeExtendedScanParams);
213   SET_HANDLER(OpCode::LE_READ_REMOTE_FEATURES, HciLeReadRemoteFeatures);
214   SET_HANDLER(OpCode::READ_REMOTE_VERSION_INFORMATION, HciReadRemoteVersionInformation);
215   SET_HANDLER(OpCode::LE_CONNECTION_UPDATE, HciLeConnectionUpdate);
216   SET_HANDLER(OpCode::LE_START_ENCRYPTION, HciLeStartEncryption);
217   // Testing Commands
218   SET_HANDLER(OpCode::READ_LOOPBACK_MODE, HciReadLoopbackMode);
219   SET_HANDLER(OpCode::WRITE_LOOPBACK_MODE, HciWriteLoopbackMode);
220 #undef SET_HANDLER
221 }
222 
HciSniffSubrating(packets::PacketView<true> args)223 void DualModeController::HciSniffSubrating(packets::PacketView<true> args) {
224   CHECK(args.size() == 8) << __func__ << " size=" << args.size();
225 
226   uint16_t handle = args.begin().extract<uint16_t>();
227 
228   send_event_(packets::EventPacketBuilder::CreateSniffSubratingEvent(hci::Status::SUCCESS, handle)->ToVector());
229 }
230 
RegisterTaskScheduler(std::function<AsyncTaskId (std::chrono::milliseconds,const TaskCallback &)> oneshot_scheduler)231 void DualModeController::RegisterTaskScheduler(
232     std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> oneshot_scheduler) {
233   link_layer_controller_.RegisterTaskScheduler(oneshot_scheduler);
234 }
235 
RegisterPeriodicTaskScheduler(std::function<AsyncTaskId (std::chrono::milliseconds,std::chrono::milliseconds,const TaskCallback &)> periodic_scheduler)236 void DualModeController::RegisterPeriodicTaskScheduler(
237     std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
238         periodic_scheduler) {
239   link_layer_controller_.RegisterPeriodicTaskScheduler(periodic_scheduler);
240 }
241 
RegisterTaskCancel(std::function<void (AsyncTaskId)> task_cancel)242 void DualModeController::RegisterTaskCancel(std::function<void(AsyncTaskId)> task_cancel) {
243   link_layer_controller_.RegisterTaskCancel(task_cancel);
244 }
245 
HandleAcl(std::shared_ptr<std::vector<uint8_t>> packet)246 void DualModeController::HandleAcl(std::shared_ptr<std::vector<uint8_t>> packet) {
247   auto acl_packet = packets::AclPacketView::Create(packet);
248   if (loopback_mode_ == hci::LoopbackMode::LOCAL) {
249     uint16_t handle = acl_packet.GetHandle();
250     send_acl_(packet);
251     send_event_(packets::EventPacketBuilder::CreateNumberOfCompletedPacketsEvent(handle, 1)->ToVector());
252     return;
253   }
254 
255   link_layer_controller_.SendAclToRemote(acl_packet);
256 }
257 
HandleSco(std::shared_ptr<std::vector<uint8_t>> packet)258 void DualModeController::HandleSco(std::shared_ptr<std::vector<uint8_t>> packet) {
259   auto sco_packet = packets::ScoPacketView::Create(packet);
260   if (loopback_mode_ == hci::LoopbackMode::LOCAL) {
261     uint16_t handle = sco_packet.GetHandle();
262     send_sco_(packet);
263     send_event_(packets::EventPacketBuilder::CreateNumberOfCompletedPacketsEvent(handle, 1)->ToVector());
264     return;
265   }
266 }
267 
HandleCommand(std::shared_ptr<std::vector<uint8_t>> packet)268 void DualModeController::HandleCommand(std::shared_ptr<std::vector<uint8_t>> packet) {
269   auto command_packet = packets::CommandPacketView::Create(packet);
270   uint16_t opcode = command_packet.GetOpcode();
271   hci::OpCode op = static_cast<hci::OpCode>(opcode);
272 
273   if (loopback_mode_ == hci::LoopbackMode::LOCAL &&
274       // Loopback exceptions.
275       op != OpCode::RESET && op != OpCode::SET_CONTROLLER_TO_HOST_FLOW_CONTROL && op != OpCode::HOST_BUFFER_SIZE &&
276       op != OpCode::HOST_NUM_COMPLETED_PACKETS && op != OpCode::READ_BUFFER_SIZE && op != OpCode::READ_LOOPBACK_MODE &&
277       op != OpCode::WRITE_LOOPBACK_MODE) {
278     send_event_(packets::EventPacketBuilder::CreateLoopbackCommandEvent(op, command_packet.GetPayload())->ToVector());
279   } else if (active_hci_commands_.count(opcode) > 0) {
280     active_hci_commands_[opcode](command_packet.GetPayload());
281   } else {
282     SendCommandCompleteUnknownOpCodeEvent(opcode);
283     LOG_INFO(LOG_TAG, "Command opcode: 0x%04X, OGF: 0x%04X, OCF: 0x%04X", opcode, opcode & 0xFC00, opcode & 0x03FF);
284   }
285 }
286 
RegisterEventChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)287 void DualModeController::RegisterEventChannel(
288     const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
289   link_layer_controller_.RegisterEventChannel(callback);
290   send_event_ = callback;
291 }
292 
RegisterAclChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)293 void DualModeController::RegisterAclChannel(
294     const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
295   link_layer_controller_.RegisterAclChannel(callback);
296   send_acl_ = callback;
297 }
298 
RegisterScoChannel(const std::function<void (std::shared_ptr<std::vector<uint8_t>>)> & callback)299 void DualModeController::RegisterScoChannel(
300     const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
301   link_layer_controller_.RegisterScoChannel(callback);
302   send_sco_ = callback;
303 }
304 
HciReset(packets::PacketView<true> args)305 void DualModeController::HciReset(packets::PacketView<true> args) {
306   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
307   link_layer_controller_.Reset();
308 
309   SendCommandCompleteSuccess(OpCode::RESET);
310 }
311 
HciReadBufferSize(packets::PacketView<true> args)312 void DualModeController::HciReadBufferSize(packets::PacketView<true> args) {
313   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
314   std::shared_ptr<packets::EventPacketBuilder> command_complete =
315       packets::EventPacketBuilder::CreateCommandCompleteReadBufferSize(
316           hci::Status::SUCCESS, properties_.GetAclDataPacketSize(), properties_.GetSynchronousDataPacketSize(),
317           properties_.GetTotalNumAclDataPackets(), properties_.GetTotalNumSynchronousDataPackets());
318 
319   send_event_(command_complete->ToVector());
320 }
321 
HciHostBufferSize(packets::PacketView<true> args)322 void DualModeController::HciHostBufferSize(packets::PacketView<true> args) {
323   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
324   SendCommandCompleteSuccess(OpCode::HOST_BUFFER_SIZE);
325 }
326 
HciReadLocalVersionInformation(packets::PacketView<true> args)327 void DualModeController::HciReadLocalVersionInformation(packets::PacketView<true> args) {
328   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
329   std::shared_ptr<packets::EventPacketBuilder> command_complete =
330       packets::EventPacketBuilder::CreateCommandCompleteReadLocalVersionInformation(
331           hci::Status::SUCCESS, properties_.GetVersion(), properties_.GetRevision(), properties_.GetLmpPalVersion(),
332           properties_.GetManufacturerName(), properties_.GetLmpPalSubversion());
333   send_event_(command_complete->ToVector());
334 }
335 
HciReadRemoteVersionInformation(packets::PacketView<true> args)336 void DualModeController::HciReadRemoteVersionInformation(packets::PacketView<true> args) {
337   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
338 
339   uint16_t handle = args.begin().extract<uint16_t>();
340 
341   hci::Status status =
342       link_layer_controller_.SendCommandToRemoteByHandle(OpCode::READ_REMOTE_VERSION_INFORMATION, args, handle);
343 
344   SendCommandStatus(status, OpCode::READ_REMOTE_VERSION_INFORMATION);
345 }
346 
HciReadBdAddr(packets::PacketView<true> args)347 void DualModeController::HciReadBdAddr(packets::PacketView<true> args) {
348   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
349   std::shared_ptr<packets::EventPacketBuilder> command_complete =
350       packets::EventPacketBuilder::CreateCommandCompleteReadBdAddr(hci::Status::SUCCESS, properties_.GetAddress());
351   send_event_(command_complete->ToVector());
352 }
353 
HciReadLocalSupportedCommands(packets::PacketView<true> args)354 void DualModeController::HciReadLocalSupportedCommands(packets::PacketView<true> args) {
355   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
356   std::shared_ptr<packets::EventPacketBuilder> command_complete =
357       packets::EventPacketBuilder::CreateCommandCompleteReadLocalSupportedCommands(hci::Status::SUCCESS,
358                                                                                    properties_.GetSupportedCommands());
359   send_event_(command_complete->ToVector());
360 }
361 
HciReadLocalSupportedCodecs(packets::PacketView<true> args)362 void DualModeController::HciReadLocalSupportedCodecs(packets::PacketView<true> args) {
363   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
364   std::shared_ptr<packets::EventPacketBuilder> command_complete =
365       packets::EventPacketBuilder::CreateCommandCompleteReadLocalSupportedCodecs(
366           hci::Status::SUCCESS, properties_.GetSupportedCodecs(), properties_.GetVendorSpecificCodecs());
367   send_event_(command_complete->ToVector());
368 }
369 
HciReadLocalExtendedFeatures(packets::PacketView<true> args)370 void DualModeController::HciReadLocalExtendedFeatures(packets::PacketView<true> args) {
371   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
372   uint8_t page_number = args.begin().extract<uint8_t>();
373   send_event_(packets::EventPacketBuilder::CreateCommandCompleteReadLocalExtendedFeatures(
374                   hci::Status::SUCCESS, page_number, properties_.GetExtendedFeaturesMaximumPageNumber(),
375                   properties_.GetExtendedFeatures(page_number))
376                   ->ToVector());
377 }
378 
HciReadRemoteExtendedFeatures(packets::PacketView<true> args)379 void DualModeController::HciReadRemoteExtendedFeatures(packets::PacketView<true> args) {
380   CHECK(args.size() == 3) << __func__ << " size=" << args.size();
381 
382   uint16_t handle = args.begin().extract<uint16_t>();
383 
384   hci::Status status =
385       link_layer_controller_.SendCommandToRemoteByHandle(OpCode::READ_REMOTE_EXTENDED_FEATURES, args, handle);
386 
387   SendCommandStatus(status, OpCode::READ_REMOTE_EXTENDED_FEATURES);
388 }
389 
HciReadRemoteSupportedFeatures(packets::PacketView<true> args)390 void DualModeController::HciReadRemoteSupportedFeatures(packets::PacketView<true> args) {
391   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
392 
393   uint16_t handle = args.begin().extract<uint16_t>();
394 
395   hci::Status status =
396       link_layer_controller_.SendCommandToRemoteByHandle(OpCode::READ_REMOTE_SUPPORTED_FEATURES, args, handle);
397 
398   SendCommandStatus(status, OpCode::READ_REMOTE_SUPPORTED_FEATURES);
399 }
400 
HciReadClockOffset(packets::PacketView<true> args)401 void DualModeController::HciReadClockOffset(packets::PacketView<true> args) {
402   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
403 
404   uint16_t handle = args.begin().extract<uint16_t>();
405 
406   hci::Status status = link_layer_controller_.SendCommandToRemoteByHandle(OpCode::READ_CLOCK_OFFSET, args, handle);
407 
408   SendCommandStatus(status, OpCode::READ_CLOCK_OFFSET);
409 }
410 
HciIoCapabilityRequestReply(packets::PacketView<true> args)411 void DualModeController::HciIoCapabilityRequestReply(packets::PacketView<true> args) {
412   CHECK(args.size() == 9) << __func__ << " size=" << args.size();
413 
414   auto args_itr = args.begin();
415   Address peer = args_itr.extract<Address>();
416   uint8_t io_capability = args_itr.extract<uint8_t>();
417   uint8_t oob_data_present_flag = args_itr.extract<uint8_t>();
418   uint8_t authentication_requirements = args_itr.extract<uint8_t>();
419 
420   hci::Status status = link_layer_controller_.IoCapabilityRequestReply(peer, io_capability, oob_data_present_flag,
421                                                                        authentication_requirements);
422 
423   SendCommandCompleteStatusAndAddress(OpCode::IO_CAPABILITY_REQUEST_REPLY, status, peer);
424 }
425 
HciUserConfirmationRequestReply(packets::PacketView<true> args)426 void DualModeController::HciUserConfirmationRequestReply(packets::PacketView<true> args) {
427   CHECK(args.size() == 6) << __func__ << " size=" << args.size();
428 
429   Address peer = args.begin().extract<Address>();
430 
431   hci::Status status = link_layer_controller_.UserConfirmationRequestReply(peer);
432 
433   SendCommandCompleteStatusAndAddress(OpCode::USER_CONFIRMATION_REQUEST_REPLY, status, peer);
434 }
435 
HciUserConfirmationRequestNegativeReply(packets::PacketView<true> args)436 void DualModeController::HciUserConfirmationRequestNegativeReply(packets::PacketView<true> args) {
437   CHECK(args.size() == 6) << __func__ << " size=" << args.size();
438 
439   Address peer = args.begin().extract<Address>();
440 
441   hci::Status status = link_layer_controller_.UserConfirmationRequestNegativeReply(peer);
442 
443   SendCommandCompleteStatusAndAddress(OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY, status, peer);
444 }
445 
HciUserPasskeyRequestReply(packets::PacketView<true> args)446 void DualModeController::HciUserPasskeyRequestReply(packets::PacketView<true> args) {
447   CHECK(args.size() == 10) << __func__ << " size=" << args.size();
448 
449   auto args_itr = args.begin();
450   Address peer = args_itr.extract<Address>();
451   uint32_t numeric_value = args_itr.extract<uint32_t>();
452 
453   hci::Status status = link_layer_controller_.UserPasskeyRequestReply(peer, numeric_value);
454 
455   SendCommandCompleteStatusAndAddress(OpCode::USER_PASSKEY_REQUEST_REPLY, status, peer);
456 }
457 
HciUserPasskeyRequestNegativeReply(packets::PacketView<true> args)458 void DualModeController::HciUserPasskeyRequestNegativeReply(packets::PacketView<true> args) {
459   CHECK(args.size() == 6) << __func__ << " size=" << args.size();
460 
461   Address peer = args.begin().extract<Address>();
462 
463   hci::Status status = link_layer_controller_.UserPasskeyRequestNegativeReply(peer);
464 
465   SendCommandCompleteStatusAndAddress(OpCode::USER_PASSKEY_REQUEST_NEGATIVE_REPLY, status, peer);
466 }
467 
HciRemoteOobDataRequestReply(packets::PacketView<true> args)468 void DualModeController::HciRemoteOobDataRequestReply(packets::PacketView<true> args) {
469   CHECK(args.size() == 38) << __func__ << " size=" << args.size();
470 
471   auto args_itr = args.begin();
472   Address peer = args_itr.extract<Address>();
473   std::vector<uint8_t> c;
474   std::vector<uint8_t> r;
475   for (size_t i = 0; i < 16; i++) {
476     c.push_back(args_itr.extract<uint8_t>());
477   }
478   for (size_t i = 0; i < 16; i++) {
479     r.push_back(args_itr.extract<uint8_t>());
480   }
481   hci::Status status = link_layer_controller_.RemoteOobDataRequestReply(peer, c, r);
482 
483   SendCommandCompleteStatusAndAddress(OpCode::REMOTE_OOB_DATA_REQUEST_REPLY, status, peer);
484 }
485 
HciRemoteOobDataRequestNegativeReply(packets::PacketView<true> args)486 void DualModeController::HciRemoteOobDataRequestNegativeReply(packets::PacketView<true> args) {
487   CHECK(args.size() == 6) << __func__ << " size=" << args.size();
488 
489   Address peer = args.begin().extract<Address>();
490 
491   hci::Status status = link_layer_controller_.RemoteOobDataRequestNegativeReply(peer);
492 
493   SendCommandCompleteStatusAndAddress(OpCode::REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY, status, peer);
494 }
495 
HciIoCapabilityRequestNegativeReply(packets::PacketView<true> args)496 void DualModeController::HciIoCapabilityRequestNegativeReply(packets::PacketView<true> args) {
497   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
498 
499   auto args_itr = args.begin();
500   Address peer = args_itr.extract<Address>();
501   hci::Status reason = args_itr.extract<hci::Status>();
502 
503   hci::Status status = link_layer_controller_.IoCapabilityRequestNegativeReply(peer, reason);
504 
505   SendCommandCompleteStatusAndAddress(OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY, status, peer);
506 }
507 
HciWriteSimplePairingMode(packets::PacketView<true> args)508 void DualModeController::HciWriteSimplePairingMode(packets::PacketView<true> args) {
509   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
510   CHECK(args[0] == 1 || args[0] == 0);
511   link_layer_controller_.WriteSimplePairingMode(args[0] == 1);
512   SendCommandCompleteSuccess(OpCode::WRITE_SIMPLE_PAIRING_MODE);
513 }
514 
HciChangeConnectionPacketType(packets::PacketView<true> args)515 void DualModeController::HciChangeConnectionPacketType(packets::PacketView<true> args) {
516   CHECK(args.size() == 4) << __func__ << " size=" << args.size();
517   auto args_itr = args.begin();
518   uint16_t handle = args_itr.extract<uint16_t>();
519   uint16_t packet_type = args_itr.extract<uint16_t>();
520 
521   hci::Status status = link_layer_controller_.ChangeConnectionPacketType(handle, packet_type);
522 
523   SendCommandStatus(status, OpCode::CHANGE_CONNECTION_PACKET_TYPE);
524 }
525 
HciWriteLeHostSupport(packets::PacketView<true> args)526 void DualModeController::HciWriteLeHostSupport(packets::PacketView<true> args) {
527   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
528   SendCommandCompleteSuccess(OpCode::WRITE_LE_HOST_SUPPORT);
529 }
530 
HciSetEventMask(packets::PacketView<true> args)531 void DualModeController::HciSetEventMask(packets::PacketView<true> args) {
532   CHECK(args.size() == 8) << __func__ << " size=" << args.size();
533   SendCommandCompleteSuccess(OpCode::SET_EVENT_MASK);
534 }
535 
HciWriteInquiryMode(packets::PacketView<true> args)536 void DualModeController::HciWriteInquiryMode(packets::PacketView<true> args) {
537   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
538   link_layer_controller_.SetInquiryMode(args[0]);
539   SendCommandCompleteSuccess(OpCode::WRITE_INQUIRY_MODE);
540 }
541 
HciWritePageScanType(packets::PacketView<true> args)542 void DualModeController::HciWritePageScanType(packets::PacketView<true> args) {
543   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
544   SendCommandCompleteSuccess(OpCode::WRITE_PAGE_SCAN_TYPE);
545 }
546 
HciWriteInquiryScanType(packets::PacketView<true> args)547 void DualModeController::HciWriteInquiryScanType(packets::PacketView<true> args) {
548   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
549   SendCommandCompleteSuccess(OpCode::WRITE_INQUIRY_SCAN_TYPE);
550 }
551 
HciAuthenticationRequested(packets::PacketView<true> args)552 void DualModeController::HciAuthenticationRequested(packets::PacketView<true> args) {
553   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
554   uint16_t handle = args.begin().extract<uint16_t>();
555   hci::Status status = link_layer_controller_.AuthenticationRequested(handle);
556 
557   SendCommandStatus(status, OpCode::AUTHENTICATION_REQUESTED);
558 }
559 
HciSetConnectionEncryption(packets::PacketView<true> args)560 void DualModeController::HciSetConnectionEncryption(packets::PacketView<true> args) {
561   CHECK(args.size() == 3) << __func__ << " size=" << args.size();
562   auto args_itr = args.begin();
563   uint16_t handle = args_itr.extract<uint16_t>();
564   uint8_t encryption_enable = args_itr.extract<uint8_t>();
565   hci::Status status = link_layer_controller_.SetConnectionEncryption(handle, encryption_enable);
566 
567   SendCommandStatus(status, OpCode::SET_CONNECTION_ENCRYPTION);
568 }
569 
HciWriteAuthenticationEnable(packets::PacketView<true> args)570 void DualModeController::HciWriteAuthenticationEnable(packets::PacketView<true> args) {
571   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
572   properties_.SetAuthenticationEnable(args[0]);
573   SendCommandCompleteSuccess(OpCode::WRITE_AUTHENTICATION_ENABLE);
574 }
575 
HciReadAuthenticationEnable(packets::PacketView<true> args)576 void DualModeController::HciReadAuthenticationEnable(packets::PacketView<true> args) {
577   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
578   std::shared_ptr<packets::EventPacketBuilder> command_complete =
579       packets::EventPacketBuilder::CreateCommandCompleteReadAuthenticationEnable(hci::Status::SUCCESS,
580                                                                                  properties_.GetAuthenticationEnable());
581   send_event_(command_complete->ToVector());
582 }
583 
HciWriteClassOfDevice(packets::PacketView<true> args)584 void DualModeController::HciWriteClassOfDevice(packets::PacketView<true> args) {
585   CHECK(args.size() == 3) << __func__ << " size=" << args.size();
586   properties_.SetClassOfDevice(args[0], args[1], args[2]);
587   SendCommandCompleteSuccess(OpCode::WRITE_CLASS_OF_DEVICE);
588 }
589 
HciWritePageTimeout(packets::PacketView<true> args)590 void DualModeController::HciWritePageTimeout(packets::PacketView<true> args) {
591   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
592   SendCommandCompleteSuccess(OpCode::WRITE_PAGE_TIMEOUT);
593 }
594 
HciWriteDefaultLinkPolicySettings(packets::PacketView<true> args)595 void DualModeController::HciWriteDefaultLinkPolicySettings(packets::PacketView<true> args) {
596   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
597   SendCommandCompleteSuccess(OpCode::WRITE_DEFAULT_LINK_POLICY_SETTINGS);
598 }
599 
HciWriteLinkPolicySettings(packets::PacketView<true> args)600 void DualModeController::HciWriteLinkPolicySettings(packets::PacketView<true> args) {
601   CHECK(args.size() == 4) << __func__ << " size=" << args.size();
602 
603   auto args_itr = args.begin();
604   uint16_t handle = args_itr.extract<uint16_t>();
605   uint16_t settings = args_itr.extract<uint16_t>();
606 
607   hci::Status status = link_layer_controller_.WriteLinkPolicySettings(handle, settings);
608 
609   send_event_(packets::EventPacketBuilder::CreateCommandCompleteWriteLinkPolicySettings(status, handle)->ToVector());
610 }
611 
HciWriteLinkSupervisionTimeout(packets::PacketView<true> args)612 void DualModeController::HciWriteLinkSupervisionTimeout(packets::PacketView<true> args) {
613   CHECK(args.size() == 4) << __func__ << " size=" << args.size();
614 
615   auto args_itr = args.begin();
616   uint16_t handle = args_itr.extract<uint16_t>();
617   uint16_t timeout = args_itr.extract<uint16_t>();
618 
619   hci::Status status = link_layer_controller_.WriteLinkSupervisionTimeout(handle, timeout);
620 
621   send_event_(
622       packets::EventPacketBuilder::CreateCommandCompleteWriteLinkSupervisionTimeout(status, handle)->ToVector());
623 }
624 
HciReadLocalName(packets::PacketView<true> args)625 void DualModeController::HciReadLocalName(packets::PacketView<true> args) {
626   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
627   std::shared_ptr<packets::EventPacketBuilder> command_complete =
628       packets::EventPacketBuilder::CreateCommandCompleteReadLocalName(hci::Status::SUCCESS, properties_.GetName());
629   send_event_(command_complete->ToVector());
630 }
631 
HciWriteLocalName(packets::PacketView<true> args)632 void DualModeController::HciWriteLocalName(packets::PacketView<true> args) {
633   CHECK(args.size() == 248) << __func__ << " size=" << args.size();
634   std::vector<uint8_t> clipped(args.begin(), args.begin() + LastNonZero(args) + 1);
635   properties_.SetName(clipped);
636   SendCommandCompleteSuccess(OpCode::WRITE_LOCAL_NAME);
637 }
638 
HciWriteExtendedInquiryResponse(packets::PacketView<true> args)639 void DualModeController::HciWriteExtendedInquiryResponse(packets::PacketView<true> args) {
640   CHECK(args.size() == 241) << __func__ << " size=" << args.size();
641   // Strip FEC byte and trailing zeros
642   std::vector<uint8_t> clipped(args.begin() + 1, args.begin() + LastNonZero(args) + 1);
643   properties_.SetExtendedInquiryData(clipped);
644   LOG_WARN(LOG_TAG, "Write EIR Inquiry - Size = %d (%d)", static_cast<int>(properties_.GetExtendedInquiryData().size()),
645            static_cast<int>(clipped.size()));
646   SendCommandCompleteSuccess(OpCode::WRITE_EXTENDED_INQUIRY_RESPONSE);
647 }
648 
HciWriteVoiceSetting(packets::PacketView<true> args)649 void DualModeController::HciWriteVoiceSetting(packets::PacketView<true> args) {
650   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
651   SendCommandCompleteSuccess(OpCode::WRITE_VOICE_SETTING);
652 }
653 
HciWriteCurrentIacLap(packets::PacketView<true> args)654 void DualModeController::HciWriteCurrentIacLap(packets::PacketView<true> args) {
655   CHECK(args.size() > 0);
656   CHECK(args.size() == 1 + (3 * args[0]));  // count + 3-byte IACs
657 
658   SendCommandCompleteSuccess(OpCode::WRITE_CURRENT_IAC_LAP);
659 }
660 
HciWriteInquiryScanActivity(packets::PacketView<true> args)661 void DualModeController::HciWriteInquiryScanActivity(packets::PacketView<true> args) {
662   CHECK(args.size() == 4) << __func__ << " size=" << args.size();
663   SendCommandCompleteSuccess(OpCode::WRITE_INQUIRY_SCAN_ACTIVITY);
664 }
665 
HciWriteScanEnable(packets::PacketView<true> args)666 void DualModeController::HciWriteScanEnable(packets::PacketView<true> args) {
667   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
668   link_layer_controller_.SetInquiryScanEnable(args[0] & 0x1);
669   link_layer_controller_.SetPageScanEnable(args[0] & 0x2);
670   SendCommandCompleteSuccess(OpCode::WRITE_SCAN_ENABLE);
671 }
672 
HciSetEventFilter(packets::PacketView<true> args)673 void DualModeController::HciSetEventFilter(packets::PacketView<true> args) {
674   CHECK(args.size() > 0);
675   SendCommandCompleteSuccess(OpCode::SET_EVENT_FILTER);
676 }
677 
HciInquiry(packets::PacketView<true> args)678 void DualModeController::HciInquiry(packets::PacketView<true> args) {
679   CHECK(args.size() == 5) << __func__ << " size=" << args.size();
680   link_layer_controller_.SetInquiryLAP(args[0] | (args[1] << 8) | (args[2] << 16));
681   link_layer_controller_.SetInquiryMaxResponses(args[4]);
682   link_layer_controller_.StartInquiry(std::chrono::milliseconds(args[3] * 1280));
683 
684   SendCommandStatusSuccess(OpCode::INQUIRY);
685 }
686 
HciInquiryCancel(packets::PacketView<true> args)687 void DualModeController::HciInquiryCancel(packets::PacketView<true> args) {
688   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
689   link_layer_controller_.InquiryCancel();
690   SendCommandCompleteSuccess(OpCode::INQUIRY_CANCEL);
691 }
692 
HciAcceptConnectionRequest(packets::PacketView<true> args)693 void DualModeController::HciAcceptConnectionRequest(packets::PacketView<true> args) {
694   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
695   Address addr = args.begin().extract<Address>();
696   bool try_role_switch = args[6] == 0;
697   hci::Status status = link_layer_controller_.AcceptConnectionRequest(addr, try_role_switch);
698   SendCommandStatus(status, OpCode::ACCEPT_CONNECTION_REQUEST);
699 }
700 
HciRejectConnectionRequest(packets::PacketView<true> args)701 void DualModeController::HciRejectConnectionRequest(packets::PacketView<true> args) {
702   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
703   auto args_itr = args.begin();
704   Address addr = args_itr.extract<Address>();
705   uint8_t reason = args_itr.extract<uint8_t>();
706   hci::Status status = link_layer_controller_.RejectConnectionRequest(addr, reason);
707   SendCommandStatus(status, OpCode::REJECT_CONNECTION_REQUEST);
708 }
709 
HciLinkKeyRequestReply(packets::PacketView<true> args)710 void DualModeController::HciLinkKeyRequestReply(packets::PacketView<true> args) {
711   CHECK(args.size() == 22) << __func__ << " size=" << args.size();
712   Address addr = args.begin().extract<Address>();
713   packets::PacketView<true> key = args.SubViewLittleEndian(6, 22);
714   hci::Status status = link_layer_controller_.LinkKeyRequestReply(addr, key);
715   send_event_(packets::EventPacketBuilder::CreateCommandCompleteLinkKeyRequestReply(status, addr)->ToVector());
716 }
717 
HciLinkKeyRequestNegativeReply(packets::PacketView<true> args)718 void DualModeController::HciLinkKeyRequestNegativeReply(packets::PacketView<true> args) {
719   CHECK(args.size() == 6) << __func__ << " size=" << args.size();
720   Address addr = args.begin().extract<Address>();
721   hci::Status status = link_layer_controller_.LinkKeyRequestNegativeReply(addr);
722   send_event_(packets::EventPacketBuilder::CreateCommandCompleteLinkKeyRequestNegativeReply(status, addr)->ToVector());
723 }
724 
HciDeleteStoredLinkKey(packets::PacketView<true> args)725 void DualModeController::HciDeleteStoredLinkKey(packets::PacketView<true> args) {
726   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
727 
728   uint16_t deleted_keys = 0;
729 
730   if (args[6] == 0) {
731     Address addr = args.begin().extract<Address>();
732     deleted_keys = security_manager_.DeleteKey(addr);
733   }
734 
735   if (args[6] == 1) {
736     security_manager_.DeleteAllKeys();
737   }
738 
739   send_event_(packets::EventPacketBuilder::CreateCommandCompleteDeleteStoredLinkKey(hci::Status::SUCCESS, deleted_keys)
740                   ->ToVector());
741 }
742 
HciRemoteNameRequest(packets::PacketView<true> args)743 void DualModeController::HciRemoteNameRequest(packets::PacketView<true> args) {
744   CHECK(args.size() == 10) << __func__ << " size=" << args.size();
745 
746   Address remote_addr = args.begin().extract<Address>();
747 
748   hci::Status status =
749       link_layer_controller_.SendCommandToRemoteByAddress(OpCode::REMOTE_NAME_REQUEST, args, remote_addr, false);
750 
751   SendCommandStatus(status, OpCode::REMOTE_NAME_REQUEST);
752 }
753 
HciLeSetEventMask(packets::PacketView<true> args)754 void DualModeController::HciLeSetEventMask(packets::PacketView<true> args) {
755   CHECK(args.size() == 8) << __func__ << " size=" << args.size();
756   /*
757     uint64_t mask = args.begin().extract<uint64_t>();
758     link_layer_controller_.SetLeEventMask(mask);
759   */
760   SendCommandCompleteSuccess(OpCode::LE_SET_EVENT_MASK);
761 }
762 
HciLeReadBufferSize(packets::PacketView<true> args)763 void DualModeController::HciLeReadBufferSize(packets::PacketView<true> args) {
764   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
765   std::shared_ptr<packets::EventPacketBuilder> command_complete =
766       packets::EventPacketBuilder::CreateCommandCompleteLeReadBufferSize(
767           hci::Status::SUCCESS, properties_.GetLeDataPacketLength(), properties_.GetTotalNumLeDataPackets());
768   send_event_(command_complete->ToVector());
769 }
770 
HciLeReadLocalSupportedFeatures(packets::PacketView<true> args)771 void DualModeController::HciLeReadLocalSupportedFeatures(packets::PacketView<true> args) {
772   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
773   std::shared_ptr<packets::EventPacketBuilder> command_complete =
774       packets::EventPacketBuilder::CreateCommandCompleteLeReadLocalSupportedFeatures(
775           hci::Status::SUCCESS, properties_.GetLeSupportedFeatures());
776   send_event_(command_complete->ToVector());
777 }
778 
HciLeSetRandomAddress(packets::PacketView<true> args)779 void DualModeController::HciLeSetRandomAddress(packets::PacketView<true> args) {
780   CHECK(args.size() == 6) << __func__ << " size=" << args.size();
781   properties_.SetLeAddress(args.begin().extract<Address>());
782   SendCommandCompleteSuccess(OpCode::LE_SET_RANDOM_ADDRESS);
783 }
784 
HciLeSetAdvertisingParameters(packets::PacketView<true> args)785 void DualModeController::HciLeSetAdvertisingParameters(packets::PacketView<true> args) {
786   CHECK(args.size() == 15) << __func__ << " size=" << args.size();
787 
788   SendCommandCompleteSuccess(OpCode::LE_SET_ADVERTISING_PARAMETERS);
789 }
790 
HciLeSetAdvertisingData(packets::PacketView<true> args)791 void DualModeController::HciLeSetAdvertisingData(packets::PacketView<true> args) {
792   CHECK(args.size() > 0);
793   SendCommandCompleteSuccess(OpCode::LE_SET_ADVERTISING_DATA);
794 }
795 
HciLeSetScanParameters(packets::PacketView<true> args)796 void DualModeController::HciLeSetScanParameters(packets::PacketView<true> args) {
797   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
798   link_layer_controller_.SetLeScanType(args[0]);
799   link_layer_controller_.SetLeScanInterval(args[1] | (args[2] << 8));
800   link_layer_controller_.SetLeScanWindow(args[3] | (args[4] << 8));
801   link_layer_controller_.SetLeAddressType(args[5]);
802   link_layer_controller_.SetLeScanFilterPolicy(args[6]);
803   SendCommandCompleteSuccess(OpCode::LE_SET_SCAN_PARAMETERS);
804 }
805 
HciLeSetScanEnable(packets::PacketView<true> args)806 void DualModeController::HciLeSetScanEnable(packets::PacketView<true> args) {
807   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
808   LOG_INFO(LOG_TAG, "SetScanEnable: %d %d", args[0], args[1]);
809   link_layer_controller_.SetLeScanEnable(args[0]);
810   link_layer_controller_.SetLeFilterDuplicates(args[1]);
811   SendCommandCompleteSuccess(OpCode::LE_SET_SCAN_ENABLE);
812 }
813 
HciLeCreateConnection(packets::PacketView<true> args)814 void DualModeController::HciLeCreateConnection(packets::PacketView<true> args) {
815   CHECK(args.size() == 25) << __func__ << " size=" << args.size();
816   auto args_itr = args.begin();
817   link_layer_controller_.SetLeScanInterval(args_itr.extract<uint16_t>());
818   link_layer_controller_.SetLeScanWindow(args_itr.extract<uint16_t>());
819   uint8_t initiator_filter_policy = args_itr.extract<uint8_t>();
820   link_layer_controller_.SetLeInitiatorFilterPolicy(initiator_filter_policy);
821 
822   if (initiator_filter_policy == 0) {  // White list not used
823     uint8_t peer_address_type = args_itr.extract<uint8_t>();
824     Address peer_address = args_itr.extract<Address>();
825     link_layer_controller_.SetLePeerAddressType(peer_address_type);
826     link_layer_controller_.SetLePeerAddress(peer_address);
827   }
828   link_layer_controller_.SetLeAddressType(args_itr.extract<uint8_t>());
829   link_layer_controller_.SetLeConnectionIntervalMin(args_itr.extract<uint16_t>());
830   link_layer_controller_.SetLeConnectionIntervalMax(args_itr.extract<uint16_t>());
831   link_layer_controller_.SetLeConnectionLatency(args_itr.extract<uint16_t>());
832   link_layer_controller_.SetLeSupervisionTimeout(args_itr.extract<uint16_t>());
833   link_layer_controller_.SetLeMinimumCeLength(args_itr.extract<uint16_t>());
834   link_layer_controller_.SetLeMaximumCeLength(args_itr.extract<uint16_t>());
835 
836   hci::Status status = link_layer_controller_.SetLeConnect(true);
837 
838   SendCommandStatus(status, OpCode::LE_CREATE_CONNECTION);
839 }
840 
HciLeConnectionUpdate(packets::PacketView<true> args)841 void DualModeController::HciLeConnectionUpdate(packets::PacketView<true> args) {
842   CHECK(args.size() == 14) << __func__ << " size=" << args.size();
843 
844   SendCommandStatus(hci::Status::CONNECTION_REJECTED_UNACCEPTABLE_BD_ADDR, OpCode::LE_CONNECTION_UPDATE);
845 
846   send_event_(packets::EventPacketBuilder::CreateLeConnectionUpdateCompleteEvent(hci::Status::SUCCESS, 0x0002, 0x0006,
847                                                                                  0x0000, 0x01f4)
848                   ->ToVector());
849 }
850 
HciCreateConnection(packets::PacketView<true> args)851 void DualModeController::HciCreateConnection(packets::PacketView<true> args) {
852   CHECK(args.size() == 13) << __func__ << " size=" << args.size();
853 
854   auto args_itr = args.begin();
855   Address address = args_itr.extract<Address>();
856   uint16_t packet_type = args_itr.extract<uint16_t>();
857   uint8_t page_scan_mode = args_itr.extract<uint8_t>();
858   uint16_t clock_offset = args_itr.extract<uint16_t>();
859   uint8_t allow_role_switch = args_itr.extract<uint8_t>();
860 
861   hci::Status status =
862       link_layer_controller_.CreateConnection(address, packet_type, page_scan_mode, clock_offset, allow_role_switch);
863 
864   SendCommandStatus(status, OpCode::CREATE_CONNECTION);
865 }
866 
HciDisconnect(packets::PacketView<true> args)867 void DualModeController::HciDisconnect(packets::PacketView<true> args) {
868   CHECK(args.size() == 3) << __func__ << " size=" << args.size();
869 
870   auto args_itr = args.begin();
871   uint16_t handle = args_itr.extract<uint16_t>();
872   uint8_t reason = args_itr.extract<uint8_t>();
873 
874   hci::Status status = link_layer_controller_.Disconnect(handle, reason);
875 
876   SendCommandStatus(status, OpCode::DISCONNECT);
877 }
878 
HciLeConnectionCancel(packets::PacketView<true> args)879 void DualModeController::HciLeConnectionCancel(packets::PacketView<true> args) {
880   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
881   link_layer_controller_.SetLeConnect(false);
882   SendCommandStatusSuccess(OpCode::LE_CREATE_CONNECTION_CANCEL);
883   /* For testing Jakub's patch:  Figure out a neat way to call this without
884      recompiling.  I'm thinking about a bad device. */
885   /*
886   SendCommandCompleteOnlyStatus(OpCode::LE_CREATE_CONNECTION_CANCEL,
887                                 Status::ERR_COMMAND_DISALLOWED);
888   */
889 }
890 
HciLeReadWhiteListSize(packets::PacketView<true> args)891 void DualModeController::HciLeReadWhiteListSize(packets::PacketView<true> args) {
892   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
893   std::shared_ptr<packets::EventPacketBuilder> command_complete =
894       packets::EventPacketBuilder::CreateCommandCompleteLeReadWhiteListSize(hci::Status::SUCCESS,
895                                                                             properties_.GetLeWhiteListSize());
896   send_event_(command_complete->ToVector());
897 }
898 
HciLeClearWhiteList(packets::PacketView<true> args)899 void DualModeController::HciLeClearWhiteList(packets::PacketView<true> args) {
900   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
901   link_layer_controller_.LeWhiteListClear();
902   SendCommandCompleteSuccess(OpCode::LE_CLEAR_WHITE_LIST);
903 }
904 
HciLeAddDeviceToWhiteList(packets::PacketView<true> args)905 void DualModeController::HciLeAddDeviceToWhiteList(packets::PacketView<true> args) {
906   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
907 
908   if (link_layer_controller_.LeWhiteListFull()) {
909     SendCommandCompleteOnlyStatus(OpCode::LE_ADD_DEVICE_TO_WHITE_LIST, hci::Status::MEMORY_CAPACITY_EXCEEDED);
910     return;
911   }
912   auto args_itr = args.begin();
913   uint8_t addr_type = args_itr.extract<uint8_t>();
914   Address address = args_itr.extract<Address>();
915   link_layer_controller_.LeWhiteListAddDevice(address, addr_type);
916   SendCommandCompleteSuccess(OpCode::LE_ADD_DEVICE_TO_WHITE_LIST);
917 }
918 
HciLeRemoveDeviceFromWhiteList(packets::PacketView<true> args)919 void DualModeController::HciLeRemoveDeviceFromWhiteList(packets::PacketView<true> args) {
920   CHECK(args.size() == 7) << __func__ << " size=" << args.size();
921 
922   auto args_itr = args.begin();
923   uint8_t addr_type = args_itr.extract<uint8_t>();
924   Address address = args_itr.extract<Address>();
925   link_layer_controller_.LeWhiteListRemoveDevice(address, addr_type);
926   SendCommandCompleteSuccess(OpCode::LE_REMOVE_DEVICE_FROM_WHITE_LIST);
927 }
928 
929 /*
930 void DualModeController::HciLeReadRemoteUsedFeaturesRsp(uint16_t handle,
931                                                         uint64_t features) {
932   std::shared_ptr<packets::EventPacketBuilder> event =
933       packets::EventPacketBuilder::CreateLeRemoteUsedFeaturesEvent(
934           hci::Status::SUCCESS, handle, features);
935   send_event_(event->ToVector());
936 }
937 */
938 
HciLeReadRemoteFeatures(packets::PacketView<true> args)939 void DualModeController::HciLeReadRemoteFeatures(packets::PacketView<true> args) {
940   CHECK(args.size() == 2) << __func__ << " size=" << args.size();
941 
942   uint16_t handle = args.begin().extract<uint16_t>();
943 
944   hci::Status status =
945       link_layer_controller_.SendCommandToRemoteByHandle(OpCode::LE_READ_REMOTE_FEATURES, args, handle);
946 
947   SendCommandStatus(status, OpCode::LE_READ_REMOTE_FEATURES);
948 }
949 
HciLeRand(packets::PacketView<true> args)950 void DualModeController::HciLeRand(packets::PacketView<true> args) {
951   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
952   uint64_t random_val = 0;
953   for (size_t rand_bytes = 0; rand_bytes < sizeof(uint64_t); rand_bytes += sizeof(RAND_MAX)) {
954     random_val = (random_val << (8 * sizeof(RAND_MAX))) | random();
955   }
956   std::shared_ptr<packets::EventPacketBuilder> command_complete =
957       packets::EventPacketBuilder::CreateCommandCompleteLeRand(hci::Status::SUCCESS, random_val);
958   send_event_(command_complete->ToVector());
959 }
960 
HciLeReadSupportedStates(packets::PacketView<true> args)961 void DualModeController::HciLeReadSupportedStates(packets::PacketView<true> args) {
962   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
963   std::shared_ptr<packets::EventPacketBuilder> command_complete =
964       packets::EventPacketBuilder::CreateCommandCompleteLeReadSupportedStates(hci::Status::SUCCESS,
965                                                                               properties_.GetLeSupportedStates());
966   send_event_(command_complete->ToVector());
967 }
968 
HciLeVendorCap(packets::PacketView<true> args)969 void DualModeController::HciLeVendorCap(packets::PacketView<true> args) {
970   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
971   vector<uint8_t> caps = properties_.GetLeVendorCap();
972   if (caps.size() == 0) {
973     SendCommandCompleteOnlyStatus(OpCode::LE_GET_VENDOR_CAPABILITIES, hci::Status::UNKNOWN_COMMAND);
974     return;
975   }
976 
977   std::shared_ptr<packets::EventPacketBuilder> command_complete =
978       packets::EventPacketBuilder::CreateCommandCompleteLeGetVendorCapabilities(hci::Status::SUCCESS,
979                                                                                 properties_.GetLeVendorCap());
980   send_event_(command_complete->ToVector());
981 }
982 
HciLeVendorMultiAdv(packets::PacketView<true> args)983 void DualModeController::HciLeVendorMultiAdv(packets::PacketView<true> args) {
984   CHECK(args.size() > 0);
985   SendCommandCompleteOnlyStatus(OpCode::LE_MULTI_ADVT, hci::Status::UNKNOWN_COMMAND);
986 }
987 
HciLeAdvertisingFilter(packets::PacketView<true> args)988 void DualModeController::HciLeAdvertisingFilter(packets::PacketView<true> args) {
989   CHECK(args.size() > 0);
990   SendCommandCompleteOnlyStatus(OpCode::LE_ADV_FILTER, hci::Status::UNKNOWN_COMMAND);
991 }
992 
HciLeEnergyInfo(packets::PacketView<true> args)993 void DualModeController::HciLeEnergyInfo(packets::PacketView<true> args) {
994   CHECK(args.size() > 0);
995   SendCommandCompleteOnlyStatus(OpCode::LE_ENERGY_INFO, hci::Status::UNKNOWN_COMMAND);
996 }
997 
HciLeExtendedScanParams(packets::PacketView<true> args)998 void DualModeController::HciLeExtendedScanParams(packets::PacketView<true> args) {
999   CHECK(args.size() > 0);
1000   SendCommandCompleteOnlyStatus(OpCode::LE_EXTENDED_SCAN_PARAMS, hci::Status::UNKNOWN_COMMAND);
1001 }
1002 
HciLeStartEncryption(packets::PacketView<true> args)1003 void DualModeController::HciLeStartEncryption(packets::PacketView<true> args) {
1004   CHECK(args.size() == 28) << __func__ << " size=" << args.size();
1005 
1006   auto args_itr = args.begin();
1007   uint16_t handle = args_itr.extract<uint16_t>();
1008   // uint64_t random_number = args_itr.extract<uint64_t>();
1009   // uint16_t encrypted_diversifier = args_itr.extract<uint16_t>();
1010   // std::vector<uint8_t> long_term_key;
1011   // for (size_t i = 0; i < 16; i++) {
1012   //   long_term_key.push_back(args_itr.extract<uint18_t>();
1013   // }
1014   SendCommandStatus(hci::Status::SUCCESS, OpCode::LE_START_ENCRYPTION);
1015 
1016   send_event_(packets::EventPacketBuilder::CreateEncryptionChange(hci::Status::SUCCESS, handle, 0x01)->ToVector());
1017 #if 0
1018 
1019   std::shared_ptr<packets::AclPacketBuilder> encryption_information =
1020       std::make_shared<packets::AclPacketBuilder>(
1021           0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1022           std::vector<uint8_t>({}));
1023 
1024   encryption_information->AddPayloadOctets2(0x0011);
1025   encryption_information->AddPayloadOctets2(0x0006);
1026   encryption_information->AddPayloadOctets1(0x06);
1027   encryption_information->AddPayloadOctets8(0x0706050403020100);
1028   encryption_information->AddPayloadOctets8(0x0F0E0D0C0B0A0908);
1029 
1030   send_acl_(encryption_information);
1031 
1032   encryption_information = std::make_shared<packets::AclPacketBuilder>(
1033       0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1034       std::vector<uint8_t>({}));
1035 
1036   encryption_information->AddPayloadOctets2(0x000B);
1037   encryption_information->AddPayloadOctets2(0x0006);
1038   encryption_information->AddPayloadOctets1(0x07);
1039   encryption_information->AddPayloadOctets2(0xBEEF);
1040   encryption_information->AddPayloadOctets8(0x0706050403020100);
1041 
1042   send_acl_(encryption_information);
1043 
1044   encryption_information = std::make_shared<packets::AclPacketBuilder>(
1045       0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1046       std::vector<uint8_t>({}));
1047 
1048   encryption_information->AddPayloadOctets2(0x0011);
1049   encryption_information->AddPayloadOctets2(0x0006);
1050   encryption_information->AddPayloadOctets1(0x08);
1051   encryption_information->AddPayloadOctets8(0x0F0E0D0C0B0A0908);
1052   encryption_information->AddPayloadOctets8(0x0706050403020100);
1053 
1054   send_acl_(encryption_information);
1055 
1056   encryption_information = std::make_shared<packets::AclPacketBuilder>(
1057       0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1058       std::vector<uint8_t>({}));
1059 
1060   encryption_information->AddPayloadOctets2(0x0008);
1061   encryption_information->AddPayloadOctets2(0x0006);
1062   encryption_information->AddPayloadOctets1(0x09);
1063   encryption_information->AddPayloadOctets1(0x01);
1064   encryption_information->AddPayloadOctets6(0xDEADBEEFF00D);
1065   send_acl_(encryption_information);
1066   // send_event_(packets::EventPacketBuilder::CreateLeStartEncryption()->ToVector());
1067 
1068 #endif
1069 }
1070 
HciReadLoopbackMode(packets::PacketView<true> args)1071 void DualModeController::HciReadLoopbackMode(packets::PacketView<true> args) {
1072   CHECK(args.size() == 0) << __func__ << " size=" << args.size();
1073   send_event_(packets::EventPacketBuilder::CreateCommandCompleteReadLoopbackMode(hci::Status::SUCCESS, loopback_mode_)
1074                   ->ToVector());
1075 }
1076 
HciWriteLoopbackMode(packets::PacketView<true> args)1077 void DualModeController::HciWriteLoopbackMode(packets::PacketView<true> args) {
1078   CHECK(args.size() == 1) << __func__ << " size=" << args.size();
1079   loopback_mode_ = static_cast<hci::LoopbackMode>(args[0]);
1080   // ACL channel
1081   uint16_t acl_handle = 0x123;
1082   send_event_(packets::EventPacketBuilder::CreateConnectionCompleteEvent(
1083                   hci::Status::SUCCESS, acl_handle, properties_.GetAddress(), hci::LinkType::ACL, false)
1084                   ->ToVector());
1085   // SCO channel
1086   uint16_t sco_handle = 0x345;
1087   send_event_(packets::EventPacketBuilder::CreateConnectionCompleteEvent(
1088                   hci::Status::SUCCESS, sco_handle, properties_.GetAddress(), hci::LinkType::SCO, false)
1089                   ->ToVector());
1090   SendCommandCompleteSuccess(OpCode::WRITE_LOOPBACK_MODE);
1091 }
1092 
1093 }  // namespace test_vendor_lib
1094