blob: f84e5149e0a5fe869087ef7015daf4c1247f7f60 [file] [log] [blame]
Myles Watsone22dde22019-01-18 11:42:33 -08001/*
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
Myles Watsone22dde22019-01-18 11:42:33 -080017#include "dual_mode_controller.h"
18
19#include <memory>
20
21#include <base/files/file_util.h>
22#include <base/json/json_reader.h>
Myles Watsone22dde22019-01-18 11:42:33 -080023#include <base/values.h>
24
Myles Watson1a0a0da2019-10-17 11:36:33 -070025#include "os/log.h"
Chienyuan3d8a8032019-11-01 18:04:07 +080026#include "packet/raw_builder.h"
Myles Watsone22dde22019-01-18 11:42:33 -080027
28#include "hci.h"
Myles Watsone22dde22019-01-18 11:42:33 -080029
30using std::vector;
31using test_vendor_lib::hci::EventCode;
32using test_vendor_lib::hci::OpCode;
33
34namespace {
35
Chienyuan676cefe2019-11-22 16:46:24 +080036size_t LastNonZero(bluetooth::packet::PacketView<true> view) {
Myles Watsone22dde22019-01-18 11:42:33 -080037 for (size_t i = view.size() - 1; i > 0; i--) {
38 if (view[i] != 0) {
39 return i;
40 }
41 }
42 return 0;
43}
44
45} // namespace
46
47namespace test_vendor_lib {
48constexpr char DualModeController::kControllerPropertiesFile[];
49constexpr uint16_t DualModeController::kSecurityManagerNumKeys;
Chienyuanc27da092019-11-20 19:29:43 +080050constexpr uint16_t kNumCommandPackets = 0x01;
Myles Watsone22dde22019-01-18 11:42:33 -080051
52// Device methods.
53void DualModeController::Initialize(const std::vector<std::string>& args) {
54 if (args.size() < 2) return;
55
56 Address addr;
Hansong Zhang106fc602019-06-19 16:53:15 -070057 if (Address::FromString(args[1], addr)) {
58 properties_.SetAddress(addr);
59 } else {
Myles Watson1a0a0da2019-10-17 11:36:33 -070060 LOG_ALWAYS_FATAL("Invalid address: %s", args[1].c_str());
Hansong Zhang106fc602019-06-19 16:53:15 -070061 }
Myles Watsone22dde22019-01-18 11:42:33 -080062};
63
64std::string DualModeController::GetTypeString() const {
65 return "Simulated Bluetooth Controller";
66}
67
Chienyuandb55f312019-10-31 14:01:28 +080068void DualModeController::IncomingPacket(
69 model::packets::LinkLayerPacketView incoming) {
Myles Watsone22dde22019-01-18 11:42:33 -080070 link_layer_controller_.IncomingPacket(incoming);
71}
72
73void DualModeController::TimerTick() {
74 link_layer_controller_.TimerTick();
75}
76
Myles Watsone22dde22019-01-18 11:42:33 -080077void DualModeController::SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const {
Chienyuan3d8a8032019-11-01 18:04:07 +080078 std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
79 std::make_unique<bluetooth::packet::RawBuilder>();
Chienyuanc27da092019-11-20 19:29:43 +080080 raw_builder_ptr->AddOctets1(kNumCommandPackets);
Chienyuan3d8a8032019-11-01 18:04:07 +080081 raw_builder_ptr->AddOctets2(command_opcode);
82 raw_builder_ptr->AddOctets1(
83 static_cast<uint8_t>(bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
84
85 auto packet = bluetooth::hci::EventPacketBuilder::Create(
86 bluetooth::hci::EventCode::COMMAND_COMPLETE, std::move(raw_builder_ptr));
87 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -080088}
89
Myles Watsone22dde22019-01-18 11:42:33 -080090DualModeController::DualModeController(const std::string& properties_filename, uint16_t num_keys)
91 : Device(properties_filename), security_manager_(num_keys) {
92 loopback_mode_ = hci::LoopbackMode::NO;
93
94 Address public_address;
Myles Watson1a0a0da2019-10-17 11:36:33 -070095 ASSERT(Address::FromString("3C:5A:B4:04:05:06", public_address));
Myles Watsone22dde22019-01-18 11:42:33 -080096 properties_.SetAddress(public_address);
97
98 link_layer_controller_.RegisterRemoteChannel(
Chienyuandb55f312019-10-31 14:01:28 +080099 [this](std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
100 Phy::Type phy_type) {
Myles Watsone22dde22019-01-18 11:42:33 -0800101 DualModeController::SendLinkLayerPacket(packet, phy_type);
102 });
103
Chienyuan676cefe2019-11-22 16:46:24 +0800104#define SET_HANDLER(opcode, method) \
105 active_hci_commands_[static_cast<uint16_t>(opcode)] = \
106 [this](bluetooth::packet::PacketView<true> param) { method(param); };
Myles Watsone22dde22019-01-18 11:42:33 -0800107 SET_HANDLER(OpCode::RESET, HciReset);
108 SET_HANDLER(OpCode::READ_BUFFER_SIZE, HciReadBufferSize);
109 SET_HANDLER(OpCode::HOST_BUFFER_SIZE, HciHostBufferSize);
110 SET_HANDLER(OpCode::SNIFF_SUBRATING, HciSniffSubrating);
Calvin Huang63a08962019-10-31 19:12:25 -0700111 SET_HANDLER(OpCode::READ_ENCRYPTION_KEY_SIZE, HciReadEncryptionKeySize);
Myles Watsone22dde22019-01-18 11:42:33 -0800112 SET_HANDLER(OpCode::READ_LOCAL_VERSION_INFORMATION, HciReadLocalVersionInformation);
113 SET_HANDLER(OpCode::READ_BD_ADDR, HciReadBdAddr);
114 SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_COMMANDS, HciReadLocalSupportedCommands);
Myles Watson1a0a0da2019-10-17 11:36:33 -0700115 SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_FEATURES, HciReadLocalSupportedFeatures);
Myles Watsone22dde22019-01-18 11:42:33 -0800116 SET_HANDLER(OpCode::READ_LOCAL_SUPPORTED_CODECS, HciReadLocalSupportedCodecs);
117 SET_HANDLER(OpCode::READ_LOCAL_EXTENDED_FEATURES, HciReadLocalExtendedFeatures);
118 SET_HANDLER(OpCode::READ_REMOTE_EXTENDED_FEATURES, HciReadRemoteExtendedFeatures);
Chienyuan9145e7a2019-10-02 15:18:55 +0800119 SET_HANDLER(OpCode::SWITCH_ROLE, HciSwitchRole);
Myles Watsone22dde22019-01-18 11:42:33 -0800120 SET_HANDLER(OpCode::READ_REMOTE_SUPPORTED_FEATURES, HciReadRemoteSupportedFeatures);
121 SET_HANDLER(OpCode::READ_CLOCK_OFFSET, HciReadClockOffset);
122 SET_HANDLER(OpCode::IO_CAPABILITY_REQUEST_REPLY, HciIoCapabilityRequestReply);
123 SET_HANDLER(OpCode::USER_CONFIRMATION_REQUEST_REPLY, HciUserConfirmationRequestReply);
124 SET_HANDLER(OpCode::USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY, HciUserConfirmationRequestNegativeReply);
125 SET_HANDLER(OpCode::IO_CAPABILITY_REQUEST_NEGATIVE_REPLY, HciIoCapabilityRequestNegativeReply);
126 SET_HANDLER(OpCode::WRITE_SIMPLE_PAIRING_MODE, HciWriteSimplePairingMode);
127 SET_HANDLER(OpCode::WRITE_LE_HOST_SUPPORT, HciWriteLeHostSupport);
Calvin Huang63a08962019-10-31 19:12:25 -0700128 SET_HANDLER(OpCode::WRITE_SECURE_CONNECTIONS_HOST_SUPPORT,
Chienyuanc27da092019-11-20 19:29:43 +0800129 HciWriteSecureConnectionsHostSupport);
Myles Watsone22dde22019-01-18 11:42:33 -0800130 SET_HANDLER(OpCode::SET_EVENT_MASK, HciSetEventMask);
131 SET_HANDLER(OpCode::WRITE_INQUIRY_MODE, HciWriteInquiryMode);
132 SET_HANDLER(OpCode::WRITE_PAGE_SCAN_TYPE, HciWritePageScanType);
133 SET_HANDLER(OpCode::WRITE_INQUIRY_SCAN_TYPE, HciWriteInquiryScanType);
134 SET_HANDLER(OpCode::AUTHENTICATION_REQUESTED, HciAuthenticationRequested);
135 SET_HANDLER(OpCode::SET_CONNECTION_ENCRYPTION, HciSetConnectionEncryption);
Chienyuanad340b02019-09-25 19:23:21 +0800136 SET_HANDLER(OpCode::CHANGE_CONNECTION_LINK_KEY, HciChangeConnectionLinkKey);
Chienyuan9145e7a2019-10-02 15:18:55 +0800137 SET_HANDLER(OpCode::MASTER_LINK_KEY, HciMasterLinkKey);
Myles Watsone22dde22019-01-18 11:42:33 -0800138 SET_HANDLER(OpCode::WRITE_AUTHENTICATION_ENABLE, HciWriteAuthenticationEnable);
139 SET_HANDLER(OpCode::READ_AUTHENTICATION_ENABLE, HciReadAuthenticationEnable);
140 SET_HANDLER(OpCode::WRITE_CLASS_OF_DEVICE, HciWriteClassOfDevice);
141 SET_HANDLER(OpCode::WRITE_PAGE_TIMEOUT, HciWritePageTimeout);
142 SET_HANDLER(OpCode::WRITE_LINK_SUPERVISION_TIMEOUT, HciWriteLinkSupervisionTimeout);
Chienyuanad340b02019-09-25 19:23:21 +0800143 SET_HANDLER(OpCode::HOLD_MODE, HciHoldMode);
144 SET_HANDLER(OpCode::SNIFF_MODE, HciSniffMode);
145 SET_HANDLER(OpCode::EXIT_SNIFF_MODE, HciExitSniffMode);
146 SET_HANDLER(OpCode::QOS_SETUP, HciQosSetup);
Myles Watsone22dde22019-01-18 11:42:33 -0800147 SET_HANDLER(OpCode::WRITE_DEFAULT_LINK_POLICY_SETTINGS, HciWriteDefaultLinkPolicySettings);
Chienyuanad340b02019-09-25 19:23:21 +0800148 SET_HANDLER(OpCode::FLOW_SPECIFICATION, HciFlowSpecification);
Myles Watsone22dde22019-01-18 11:42:33 -0800149 SET_HANDLER(OpCode::WRITE_LINK_POLICY_SETTINGS, HciWriteLinkPolicySettings);
150 SET_HANDLER(OpCode::CHANGE_CONNECTION_PACKET_TYPE, HciChangeConnectionPacketType);
151 SET_HANDLER(OpCode::WRITE_LOCAL_NAME, HciWriteLocalName);
152 SET_HANDLER(OpCode::READ_LOCAL_NAME, HciReadLocalName);
153 SET_HANDLER(OpCode::WRITE_EXTENDED_INQUIRY_RESPONSE, HciWriteExtendedInquiryResponse);
Myles Watsonb293d732019-08-19 12:35:36 -0700154 SET_HANDLER(OpCode::REFRESH_ENCRYPTION_KEY, HciRefreshEncryptionKey);
Myles Watsone22dde22019-01-18 11:42:33 -0800155 SET_HANDLER(OpCode::WRITE_VOICE_SETTING, HciWriteVoiceSetting);
156 SET_HANDLER(OpCode::WRITE_CURRENT_IAC_LAP, HciWriteCurrentIacLap);
157 SET_HANDLER(OpCode::WRITE_INQUIRY_SCAN_ACTIVITY, HciWriteInquiryScanActivity);
158 SET_HANDLER(OpCode::WRITE_SCAN_ENABLE, HciWriteScanEnable);
159 SET_HANDLER(OpCode::SET_EVENT_FILTER, HciSetEventFilter);
160 SET_HANDLER(OpCode::INQUIRY, HciInquiry);
161 SET_HANDLER(OpCode::INQUIRY_CANCEL, HciInquiryCancel);
162 SET_HANDLER(OpCode::ACCEPT_CONNECTION_REQUEST, HciAcceptConnectionRequest);
163 SET_HANDLER(OpCode::REJECT_CONNECTION_REQUEST, HciRejectConnectionRequest);
164 SET_HANDLER(OpCode::LINK_KEY_REQUEST_REPLY, HciLinkKeyRequestReply);
165 SET_HANDLER(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, HciLinkKeyRequestNegativeReply);
166 SET_HANDLER(OpCode::DELETE_STORED_LINK_KEY, HciDeleteStoredLinkKey);
167 SET_HANDLER(OpCode::REMOTE_NAME_REQUEST, HciRemoteNameRequest);
168 SET_HANDLER(OpCode::LE_SET_EVENT_MASK, HciLeSetEventMask);
169 SET_HANDLER(OpCode::LE_READ_BUFFER_SIZE, HciLeReadBufferSize);
170 SET_HANDLER(OpCode::LE_READ_LOCAL_SUPPORTED_FEATURES, HciLeReadLocalSupportedFeatures);
171 SET_HANDLER(OpCode::LE_SET_RANDOM_ADDRESS, HciLeSetRandomAddress);
Myles Watsone22dde22019-01-18 11:42:33 -0800172 SET_HANDLER(OpCode::LE_SET_ADVERTISING_PARAMETERS, HciLeSetAdvertisingParameters);
Myles Watson99aea892019-09-02 15:54:08 -0700173 SET_HANDLER(OpCode::LE_SET_ADVERTISING_DATA, HciLeSetAdvertisingData);
174 SET_HANDLER(OpCode::LE_SET_SCAN_RESPONSE_DATA, HciLeSetScanResponseData);
175 SET_HANDLER(OpCode::LE_SET_ADVERTISING_ENABLE, HciLeSetAdvertisingEnable);
Myles Watsone22dde22019-01-18 11:42:33 -0800176 SET_HANDLER(OpCode::LE_SET_SCAN_PARAMETERS, HciLeSetScanParameters);
177 SET_HANDLER(OpCode::LE_SET_SCAN_ENABLE, HciLeSetScanEnable);
178 SET_HANDLER(OpCode::LE_CREATE_CONNECTION, HciLeCreateConnection);
179 SET_HANDLER(OpCode::CREATE_CONNECTION, HciCreateConnection);
180 SET_HANDLER(OpCode::DISCONNECT, HciDisconnect);
181 SET_HANDLER(OpCode::LE_CREATE_CONNECTION_CANCEL, HciLeConnectionCancel);
182 SET_HANDLER(OpCode::LE_READ_WHITE_LIST_SIZE, HciLeReadWhiteListSize);
183 SET_HANDLER(OpCode::LE_CLEAR_WHITE_LIST, HciLeClearWhiteList);
184 SET_HANDLER(OpCode::LE_ADD_DEVICE_TO_WHITE_LIST, HciLeAddDeviceToWhiteList);
185 SET_HANDLER(OpCode::LE_REMOVE_DEVICE_FROM_WHITE_LIST, HciLeRemoveDeviceFromWhiteList);
186 SET_HANDLER(OpCode::LE_RAND, HciLeRand);
187 SET_HANDLER(OpCode::LE_READ_SUPPORTED_STATES, HciLeReadSupportedStates);
188 SET_HANDLER(OpCode::LE_GET_VENDOR_CAPABILITIES, HciLeVendorCap);
189 SET_HANDLER(OpCode::LE_MULTI_ADVT, HciLeVendorMultiAdv);
190 SET_HANDLER(OpCode::LE_ADV_FILTER, HciLeAdvertisingFilter);
191 SET_HANDLER(OpCode::LE_ENERGY_INFO, HciLeEnergyInfo);
192 SET_HANDLER(OpCode::LE_EXTENDED_SCAN_PARAMS, HciLeExtendedScanParams);
193 SET_HANDLER(OpCode::LE_READ_REMOTE_FEATURES, HciLeReadRemoteFeatures);
194 SET_HANDLER(OpCode::READ_REMOTE_VERSION_INFORMATION, HciReadRemoteVersionInformation);
195 SET_HANDLER(OpCode::LE_CONNECTION_UPDATE, HciLeConnectionUpdate);
196 SET_HANDLER(OpCode::LE_START_ENCRYPTION, HciLeStartEncryption);
Calvin Huangee1980c2019-11-01 16:35:55 -0700197 SET_HANDLER(OpCode::LE_ADD_DEVICE_TO_RESOLVING_LIST,
198 HciLeAddDeviceToResolvingList);
199 SET_HANDLER(OpCode::LE_REMOVE_DEVICE_FROM_RESOLVING_LIST,
200 HciLeRemoveDeviceFromResolvingList);
201 SET_HANDLER(OpCode::LE_CLEAR_RESOLVING_LIST, HciLeClearResolvingList);
202 SET_HANDLER(OpCode::LE_SET_PRIVACY_MODE, HciLeSetPrivacyMode);
Myles Watsone22dde22019-01-18 11:42:33 -0800203 // Testing Commands
204 SET_HANDLER(OpCode::READ_LOOPBACK_MODE, HciReadLoopbackMode);
205 SET_HANDLER(OpCode::WRITE_LOOPBACK_MODE, HciWriteLoopbackMode);
206#undef SET_HANDLER
207}
208
Chienyuan676cefe2019-11-22 16:46:24 +0800209void DualModeController::HciSniffSubrating(
210 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700211 ASSERT_LOG(args.size() == 8, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800212
213 uint16_t handle = args.begin().extract<uint16_t>();
214
Chienyuan3d8a8032019-11-01 18:04:07 +0800215 auto packet = bluetooth::hci::SniffSubratingCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800216 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, handle);
Chienyuan3d8a8032019-11-01 18:04:07 +0800217 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800218}
219
220void DualModeController::RegisterTaskScheduler(
221 std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> oneshot_scheduler) {
222 link_layer_controller_.RegisterTaskScheduler(oneshot_scheduler);
223}
224
225void DualModeController::RegisterPeriodicTaskScheduler(
226 std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
227 periodic_scheduler) {
228 link_layer_controller_.RegisterPeriodicTaskScheduler(periodic_scheduler);
229}
230
231void DualModeController::RegisterTaskCancel(std::function<void(AsyncTaskId)> task_cancel) {
232 link_layer_controller_.RegisterTaskCancel(task_cancel);
233}
234
235void DualModeController::HandleAcl(std::shared_ptr<std::vector<uint8_t>> packet) {
Chienyuan85db6ee2019-11-21 22:07:09 +0800236 bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet(packet);
237 auto acl_packet = bluetooth::hci::AclPacketView::Create(raw_packet);
238 ASSERT(acl_packet.IsValid());
Myles Watsone22dde22019-01-18 11:42:33 -0800239 if (loopback_mode_ == hci::LoopbackMode::LOCAL) {
240 uint16_t handle = acl_packet.GetHandle();
Chienyuan3d8a8032019-11-01 18:04:07 +0800241
Chienyuanc27da092019-11-20 19:29:43 +0800242 std::vector<bluetooth::hci::CompletedPackets> completed_packets;
243 bluetooth::hci::CompletedPackets cp;
244 cp.connection_handle_ = handle;
245 cp.host_num_of_completed_packets_ = kNumCommandPackets;
246 completed_packets.push_back(cp);
247 auto packet = bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
248 completed_packets);
Chienyuan3d8a8032019-11-01 18:04:07 +0800249 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800250 return;
251 }
252
253 link_layer_controller_.SendAclToRemote(acl_packet);
254}
255
256void DualModeController::HandleSco(std::shared_ptr<std::vector<uint8_t>> packet) {
Chienyuan85db6ee2019-11-21 22:07:09 +0800257 bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet(packet);
258 auto sco_packet = bluetooth::hci::ScoPacketView::Create(raw_packet);
Myles Watsone22dde22019-01-18 11:42:33 -0800259 if (loopback_mode_ == hci::LoopbackMode::LOCAL) {
260 uint16_t handle = sco_packet.GetHandle();
261 send_sco_(packet);
Chienyuanc27da092019-11-20 19:29:43 +0800262 std::vector<bluetooth::hci::CompletedPackets> completed_packets;
263 bluetooth::hci::CompletedPackets cp;
264 cp.connection_handle_ = handle;
265 cp.host_num_of_completed_packets_ = kNumCommandPackets;
266 completed_packets.push_back(cp);
267 auto packet = bluetooth::hci::NumberOfCompletedPacketsBuilder::Create(
268 completed_packets);
Chienyuan3d8a8032019-11-01 18:04:07 +0800269 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800270 return;
271 }
272}
273
Jakub Pawlowskide6c0132019-11-12 16:14:32 +0100274void DualModeController::HandleIso(
275 std::shared_ptr<std::vector<uint8_t>> /* packet */) {
276 // TODO: implement handling similar to HandleSco
277}
278
Myles Watsone22dde22019-01-18 11:42:33 -0800279void DualModeController::HandleCommand(std::shared_ptr<std::vector<uint8_t>> packet) {
Chienyuan676cefe2019-11-22 16:46:24 +0800280 bluetooth::hci::PacketView<bluetooth::hci::kLittleEndian> raw_packet(packet);
281 auto command_packet = bluetooth::hci::CommandPacketView::Create(raw_packet);
282 ASSERT(command_packet.IsValid());
283 auto op = command_packet.GetOpCode();
284 uint16_t opcode = static_cast<uint16_t>(op);
Myles Watsone22dde22019-01-18 11:42:33 -0800285
286 if (loopback_mode_ == hci::LoopbackMode::LOCAL &&
287 // Loopback exceptions.
Chienyuan676cefe2019-11-22 16:46:24 +0800288 op != bluetooth::hci::OpCode::RESET &&
289 op != bluetooth::hci::OpCode::SET_CONTROLLER_TO_HOST_FLOW_CONTROL &&
290 op != bluetooth::hci::OpCode::HOST_BUFFER_SIZE &&
291 op != bluetooth::hci::OpCode::HOST_NUM_COMPLETED_PACKETS &&
292 op != bluetooth::hci::OpCode::READ_BUFFER_SIZE &&
293 op != bluetooth::hci::OpCode::READ_LOOPBACK_MODE &&
294 op != bluetooth::hci::OpCode::WRITE_LOOPBACK_MODE) {
Chienyuan3d8a8032019-11-01 18:04:07 +0800295 std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
296 std::make_unique<bluetooth::packet::RawBuilder>();
297 raw_builder_ptr->AddOctets(*packet);
298 auto packet = bluetooth::hci::LoopbackCommandBuilder::Create(
299 std::move(raw_builder_ptr));
300 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800301 } else if (active_hci_commands_.count(opcode) > 0) {
302 active_hci_commands_[opcode](command_packet.GetPayload());
303 } else {
304 SendCommandCompleteUnknownOpCodeEvent(opcode);
Chienyuan3d8a8032019-11-01 18:04:07 +0800305 LOG_INFO("Unknown command, opcode: 0x%04X, OGF: 0x%04X, OCF: 0x%04X",
306 opcode, (opcode & 0xFC00) >> 10, opcode & 0x03FF);
Myles Watsone22dde22019-01-18 11:42:33 -0800307 }
308}
309
310void DualModeController::RegisterEventChannel(
311 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
Chienyuan3d8a8032019-11-01 18:04:07 +0800312 send_event_ =
313 [callback](std::shared_ptr<bluetooth::hci::EventPacketBuilder> event) {
314 auto bytes = std::make_shared<std::vector<uint8_t>>();
315 bluetooth::packet::BitInserter bit_inserter(*bytes);
316 bytes->reserve(event->size());
317 event->Serialize(bit_inserter);
318 callback(std::move(bytes));
319 };
320 link_layer_controller_.RegisterEventChannel(send_event_);
Myles Watsone22dde22019-01-18 11:42:33 -0800321}
322
323void DualModeController::RegisterAclChannel(
324 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
Chienyuan85db6ee2019-11-21 22:07:09 +0800325 send_acl_ =
326 [callback](std::shared_ptr<bluetooth::hci::AclPacketBuilder> acl_data) {
327 auto bytes = std::make_shared<std::vector<uint8_t>>();
328 bluetooth::packet::BitInserter bit_inserter(*bytes);
329 bytes->reserve(acl_data->size());
330 acl_data->Serialize(bit_inserter);
331 callback(std::move(bytes));
332 };
333 link_layer_controller_.RegisterAclChannel(send_acl_);
Myles Watsone22dde22019-01-18 11:42:33 -0800334}
335
336void DualModeController::RegisterScoChannel(
337 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& callback) {
338 link_layer_controller_.RegisterScoChannel(callback);
339 send_sco_ = callback;
340}
341
Jakub Pawlowskide6c0132019-11-12 16:14:32 +0100342void DualModeController::RegisterIsoChannel(
343 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
344 callback) {
345 link_layer_controller_.RegisterIsoChannel(callback);
346 send_iso_ = callback;
347}
348
Chienyuan676cefe2019-11-22 16:46:24 +0800349void DualModeController::HciReset(bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700350 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800351 link_layer_controller_.Reset();
Hansong Zhang843271b2019-04-30 10:51:27 -0700352 if (loopback_mode_ == hci::LoopbackMode::LOCAL) {
353 loopback_mode_ = hci::LoopbackMode::NO;
354 }
Myles Watsone22dde22019-01-18 11:42:33 -0800355
Myles Watsone066df42019-11-13 14:39:18 -0800356 send_event_(bluetooth::hci::ResetCompleteBuilder::Create(
357 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS));
Myles Watsone22dde22019-01-18 11:42:33 -0800358}
359
Chienyuan676cefe2019-11-22 16:46:24 +0800360void DualModeController::HciReadBufferSize(
361 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700362 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800363
Chienyuan3d8a8032019-11-01 18:04:07 +0800364 auto packet = bluetooth::hci::ReadBufferSizeCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800365 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +0800366 properties_.GetAclDataPacketSize(),
367 properties_.GetSynchronousDataPacketSize(),
368 properties_.GetTotalNumAclDataPackets(),
369 properties_.GetTotalNumSynchronousDataPackets());
370 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800371}
372
Calvin Huang63a08962019-10-31 19:12:25 -0700373void DualModeController::HciReadEncryptionKeySize(
Chienyuan676cefe2019-11-22 16:46:24 +0800374 bluetooth::packet::PacketView<true> args) {
Calvin Huang63a08962019-10-31 19:12:25 -0700375 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
376
377 uint16_t handle = args.begin().extract<uint16_t>();
378
Chienyuan3d8a8032019-11-01 18:04:07 +0800379 auto packet = bluetooth::hci::ReadEncryptionKeySizeCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800380 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, handle,
Chienyuan3d8a8032019-11-01 18:04:07 +0800381 properties_.GetEncryptionKeySize());
382 send_event_(std::move(packet));
Calvin Huang63a08962019-10-31 19:12:25 -0700383}
384
Chienyuan676cefe2019-11-22 16:46:24 +0800385void DualModeController::HciHostBufferSize(
386 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700387 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800388 auto packet = bluetooth::hci::HostBufferSizeCompleteBuilder::Create(
389 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
390 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800391}
392
Chienyuan676cefe2019-11-22 16:46:24 +0800393void DualModeController::HciReadLocalVersionInformation(
394 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700395 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800396
Chienyuanc27da092019-11-20 19:29:43 +0800397 bluetooth::hci::LocalVersionInformation local_version_information;
398 local_version_information.hci_version_ =
399 static_cast<bluetooth::hci::HciVersion>(properties_.GetVersion());
400 local_version_information.hci_revision_ = properties_.GetRevision();
401 local_version_information.lmp_version_ =
402 static_cast<bluetooth::hci::LmpVersion>(properties_.GetLmpPalVersion());
403 local_version_information.manufacturer_name_ =
404 properties_.GetManufacturerName();
405 local_version_information.lmp_subversion_ = properties_.GetLmpPalSubversion();
406 auto packet =
407 bluetooth::hci::ReadLocalVersionInformationCompleteBuilder::Create(
408 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
409 local_version_information);
Chienyuan3d8a8032019-11-01 18:04:07 +0800410 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800411}
412
Chienyuan676cefe2019-11-22 16:46:24 +0800413void DualModeController::HciReadRemoteVersionInformation(
414 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700415 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800416
417 uint16_t handle = args.begin().extract<uint16_t>();
418
Chienyuan3d8a8032019-11-01 18:04:07 +0800419 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
420 bluetooth::hci::OpCode::READ_REMOTE_VERSION_INFORMATION, args, handle);
Myles Watsone22dde22019-01-18 11:42:33 -0800421
Chienyuanc27da092019-11-20 19:29:43 +0800422 auto packet =
423 bluetooth::hci::ReadRemoteVersionInformationStatusBuilder::Create(
424 status, kNumCommandPackets);
425 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800426}
427
Chienyuan676cefe2019-11-22 16:46:24 +0800428void DualModeController::HciReadBdAddr(
429 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700430 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800431 auto packet = bluetooth::hci::ReadBdAddrCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800432 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
433 properties_.GetAddress());
Chienyuan3d8a8032019-11-01 18:04:07 +0800434 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800435}
436
Chienyuan676cefe2019-11-22 16:46:24 +0800437void DualModeController::HciReadLocalSupportedCommands(
438 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700439 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800440
441 std::array<uint8_t, 64> supported_commands;
442 supported_commands.fill(0x00);
443 size_t len = properties_.GetSupportedCommands().size();
444 if (len > 64) {
445 len = 64;
446 }
447 std::copy_n(properties_.GetSupportedCommands().begin(), len,
448 supported_commands.begin());
449
450 auto packet =
451 bluetooth::hci::ReadLocalSupportedCommandsCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800452 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
453 supported_commands);
Chienyuan3d8a8032019-11-01 18:04:07 +0800454 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800455}
456
Chienyuan676cefe2019-11-22 16:46:24 +0800457void DualModeController::HciReadLocalSupportedFeatures(
458 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700459 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800460 auto packet =
461 bluetooth::hci::ReadLocalSupportedFeaturesCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800462 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +0800463 properties_.GetSupportedFeatures());
464 send_event_(std::move(packet));
Myles Watson14d56862019-08-19 13:03:46 -0700465}
466
Chienyuan676cefe2019-11-22 16:46:24 +0800467void DualModeController::HciReadLocalSupportedCodecs(
468 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700469 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800470 auto packet = bluetooth::hci::ReadLocalSupportedCodecsCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800471 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +0800472 properties_.GetSupportedCodecs(), properties_.GetVendorSpecificCodecs());
473 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800474}
475
Chienyuan676cefe2019-11-22 16:46:24 +0800476void DualModeController::HciReadLocalExtendedFeatures(
477 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700478 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800479 uint8_t page_number = args.begin().extract<uint8_t>();
Chienyuan3d8a8032019-11-01 18:04:07 +0800480
481 auto pakcet =
482 bluetooth::hci::ReadLocalExtendedFeaturesCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800483 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, page_number,
Chienyuan3d8a8032019-11-01 18:04:07 +0800484 properties_.GetExtendedFeaturesMaximumPageNumber(),
485 properties_.GetExtendedFeatures(page_number));
486 send_event_(std::move(pakcet));
Myles Watsone22dde22019-01-18 11:42:33 -0800487}
488
Chienyuan676cefe2019-11-22 16:46:24 +0800489void DualModeController::HciReadRemoteExtendedFeatures(
490 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700491 ASSERT_LOG(args.size() == 3, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800492
493 uint16_t handle = args.begin().extract<uint16_t>();
494
Chienyuan3d8a8032019-11-01 18:04:07 +0800495 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
496 bluetooth::hci::OpCode::READ_REMOTE_EXTENDED_FEATURES, args, handle);
Myles Watsone22dde22019-01-18 11:42:33 -0800497
Chienyuanc27da092019-11-20 19:29:43 +0800498 auto packet = bluetooth::hci::ReadRemoteExtendedFeaturesStatusBuilder::Create(
499 status, kNumCommandPackets);
500 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800501}
502
Chienyuan676cefe2019-11-22 16:46:24 +0800503void DualModeController::HciSwitchRole(
504 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700505 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Chienyuan9145e7a2019-10-02 15:18:55 +0800506
507 Address address = args.begin().extract<Address>();
508 uint8_t role = args.begin().extract<uint8_t>();
509
Chienyuan3d8a8032019-11-01 18:04:07 +0800510 auto status = link_layer_controller_.SwitchRole(address, role);
Chienyuan9145e7a2019-10-02 15:18:55 +0800511
Chienyuanc27da092019-11-20 19:29:43 +0800512 auto packet = bluetooth::hci::SwitchRoleStatusBuilder::Create(
513 status, kNumCommandPackets);
514 send_event_(std::move(packet));
Chienyuan9145e7a2019-10-02 15:18:55 +0800515}
516
Chienyuan676cefe2019-11-22 16:46:24 +0800517void DualModeController::HciReadRemoteSupportedFeatures(
518 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700519 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800520
521 uint16_t handle = args.begin().extract<uint16_t>();
522
Chienyuan3d8a8032019-11-01 18:04:07 +0800523 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
524 bluetooth::hci::OpCode::READ_REMOTE_SUPPORTED_FEATURES, args, handle);
Myles Watsone22dde22019-01-18 11:42:33 -0800525
Chienyuanc27da092019-11-20 19:29:43 +0800526 auto packet =
527 bluetooth::hci::ReadRemoteSupportedFeaturesStatusBuilder::Create(
528 status, kNumCommandPackets);
529 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800530}
531
Chienyuan676cefe2019-11-22 16:46:24 +0800532void DualModeController::HciReadClockOffset(
533 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700534 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800535
536 uint16_t handle = args.begin().extract<uint16_t>();
537
Chienyuan3d8a8032019-11-01 18:04:07 +0800538 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
539 bluetooth::hci::OpCode::READ_CLOCK_OFFSET, args, handle);
Myles Watsone22dde22019-01-18 11:42:33 -0800540
Chienyuanc27da092019-11-20 19:29:43 +0800541 auto packet = bluetooth::hci::ReadClockOffsetStatusBuilder::Create(
542 status, kNumCommandPackets);
543 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800544}
545
Chienyuan676cefe2019-11-22 16:46:24 +0800546void DualModeController::HciIoCapabilityRequestReply(
547 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700548 ASSERT_LOG(args.size() == 9, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800549
550 auto args_itr = args.begin();
551 Address peer = args_itr.extract<Address>();
552 uint8_t io_capability = args_itr.extract<uint8_t>();
553 uint8_t oob_data_present_flag = args_itr.extract<uint8_t>();
554 uint8_t authentication_requirements = args_itr.extract<uint8_t>();
555
Chienyuan3d8a8032019-11-01 18:04:07 +0800556 auto status = link_layer_controller_.IoCapabilityRequestReply(
557 peer, io_capability, oob_data_present_flag, authentication_requirements);
558 auto packet = bluetooth::hci::IoCapabilityRequestReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800559 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800560
Chienyuan3d8a8032019-11-01 18:04:07 +0800561 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800562}
563
Chienyuan676cefe2019-11-22 16:46:24 +0800564void DualModeController::HciUserConfirmationRequestReply(
565 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700566 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800567
568 Address peer = args.begin().extract<Address>();
569
Chienyuan3d8a8032019-11-01 18:04:07 +0800570 auto status = link_layer_controller_.UserConfirmationRequestReply(peer);
571 auto packet =
572 bluetooth::hci::UserConfirmationRequestReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800573 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800574
Chienyuan3d8a8032019-11-01 18:04:07 +0800575 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800576}
577
Chienyuan676cefe2019-11-22 16:46:24 +0800578void DualModeController::HciUserConfirmationRequestNegativeReply(
579 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700580 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800581
582 Address peer = args.begin().extract<Address>();
583
Chienyuan3d8a8032019-11-01 18:04:07 +0800584 auto status =
585 link_layer_controller_.UserConfirmationRequestNegativeReply(peer);
586 auto packet =
587 bluetooth::hci::UserConfirmationRequestNegativeReplyCompleteBuilder::
Chienyuanc27da092019-11-20 19:29:43 +0800588 Create(kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800589
Chienyuan3d8a8032019-11-01 18:04:07 +0800590 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800591}
592
Chienyuan676cefe2019-11-22 16:46:24 +0800593void DualModeController::HciUserPasskeyRequestReply(
594 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700595 ASSERT_LOG(args.size() == 10, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800596
597 auto args_itr = args.begin();
598 Address peer = args_itr.extract<Address>();
599 uint32_t numeric_value = args_itr.extract<uint32_t>();
600
Chienyuan3d8a8032019-11-01 18:04:07 +0800601 auto status =
602 link_layer_controller_.UserPasskeyRequestReply(peer, numeric_value);
603 auto packet = bluetooth::hci::UserPasskeyRequestReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800604 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800605
Chienyuan3d8a8032019-11-01 18:04:07 +0800606 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800607}
608
Chienyuan676cefe2019-11-22 16:46:24 +0800609void DualModeController::HciUserPasskeyRequestNegativeReply(
610 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700611 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800612
613 Address peer = args.begin().extract<Address>();
614
Chienyuan3d8a8032019-11-01 18:04:07 +0800615 auto status = link_layer_controller_.UserPasskeyRequestNegativeReply(peer);
616 auto packet =
617 bluetooth::hci::UserPasskeyRequestNegativeReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800618 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800619
Chienyuan3d8a8032019-11-01 18:04:07 +0800620 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800621}
622
Chienyuan676cefe2019-11-22 16:46:24 +0800623void DualModeController::HciRemoteOobDataRequestReply(
624 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700625 ASSERT_LOG(args.size() == 38, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800626
627 auto args_itr = args.begin();
628 Address peer = args_itr.extract<Address>();
629 std::vector<uint8_t> c;
630 std::vector<uint8_t> r;
631 for (size_t i = 0; i < 16; i++) {
632 c.push_back(args_itr.extract<uint8_t>());
633 }
634 for (size_t i = 0; i < 16; i++) {
635 r.push_back(args_itr.extract<uint8_t>());
636 }
Chienyuan3d8a8032019-11-01 18:04:07 +0800637 auto status = link_layer_controller_.RemoteOobDataRequestReply(peer, c, r);
638 auto packet =
639 bluetooth::hci::RemoteOobDataRequestReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800640 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800641
Chienyuan3d8a8032019-11-01 18:04:07 +0800642 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800643}
644
Chienyuan676cefe2019-11-22 16:46:24 +0800645void DualModeController::HciRemoteOobDataRequestNegativeReply(
646 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700647 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800648
649 Address peer = args.begin().extract<Address>();
650
Chienyuan3d8a8032019-11-01 18:04:07 +0800651 auto status = link_layer_controller_.RemoteOobDataRequestNegativeReply(peer);
652 auto packet =
653 bluetooth::hci::RemoteOobDataRequestNegativeReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800654 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800655
Chienyuan3d8a8032019-11-01 18:04:07 +0800656 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800657}
658
Chienyuan676cefe2019-11-22 16:46:24 +0800659void DualModeController::HciIoCapabilityRequestNegativeReply(
660 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700661 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800662
663 auto args_itr = args.begin();
664 Address peer = args_itr.extract<Address>();
665 hci::Status reason = args_itr.extract<hci::Status>();
666
Chienyuan3d8a8032019-11-01 18:04:07 +0800667 auto status =
668 link_layer_controller_.IoCapabilityRequestNegativeReply(peer, reason);
669 auto packet =
670 bluetooth::hci::IoCapabilityRequestNegativeReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800671 kNumCommandPackets, status, peer);
Myles Watsone22dde22019-01-18 11:42:33 -0800672
Chienyuan3d8a8032019-11-01 18:04:07 +0800673 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800674}
675
Chienyuan676cefe2019-11-22 16:46:24 +0800676void DualModeController::HciWriteSimplePairingMode(
677 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700678 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
679 ASSERT(args[0] == 1 || args[0] == 0);
Myles Watsone22dde22019-01-18 11:42:33 -0800680 link_layer_controller_.WriteSimplePairingMode(args[0] == 1);
Chienyuanc27da092019-11-20 19:29:43 +0800681 auto packet = bluetooth::hci::WriteSimplePairingModeCompleteBuilder::Create(
682 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
683 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800684}
685
Chienyuan676cefe2019-11-22 16:46:24 +0800686void DualModeController::HciChangeConnectionPacketType(
687 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700688 ASSERT_LOG(args.size() == 4, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800689 auto args_itr = args.begin();
690 uint16_t handle = args_itr.extract<uint16_t>();
691 uint16_t packet_type = args_itr.extract<uint16_t>();
692
Chienyuan3d8a8032019-11-01 18:04:07 +0800693 auto status =
694 link_layer_controller_.ChangeConnectionPacketType(handle, packet_type);
Myles Watsone22dde22019-01-18 11:42:33 -0800695
Chienyuanc27da092019-11-20 19:29:43 +0800696 auto packet = bluetooth::hci::ChangeConnectionPacketTypeStatusBuilder::Create(
697 status, kNumCommandPackets);
698 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800699}
700
Chienyuan676cefe2019-11-22 16:46:24 +0800701void DualModeController::HciWriteLeHostSupport(
702 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700703 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800704 auto packet = bluetooth::hci::WriteLeHostSupportCompleteBuilder::Create(
705 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
706 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800707}
708
Chienyuanc27da092019-11-20 19:29:43 +0800709void DualModeController::HciWriteSecureConnectionsHostSupport(
Chienyuan676cefe2019-11-22 16:46:24 +0800710 bluetooth::packet::PacketView<true> args) {
Calvin Huang63a08962019-10-31 19:12:25 -0700711 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Myles Watson171ccb22019-11-13 09:37:29 -0800712 properties_.SetExtendedFeatures(properties_.GetExtendedFeatures(1) | 0x8, 1);
Chienyuanc27da092019-11-20 19:29:43 +0800713 auto packet =
714 bluetooth::hci::WriteSecureConnectionsHostSupportCompleteBuilder::Create(
715 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
716 send_event_(std::move(packet));
Calvin Huang63a08962019-10-31 19:12:25 -0700717}
718
Chienyuan676cefe2019-11-22 16:46:24 +0800719void DualModeController::HciSetEventMask(
720 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700721 ASSERT_LOG(args.size() == 8, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800722 auto packet = bluetooth::hci::SetEventMaskCompleteBuilder::Create(
723 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
724 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800725}
726
Chienyuan676cefe2019-11-22 16:46:24 +0800727void DualModeController::HciWriteInquiryMode(
728 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700729 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800730 link_layer_controller_.SetInquiryMode(args[0]);
Chienyuanc27da092019-11-20 19:29:43 +0800731 auto packet = bluetooth::hci::WriteInquiryModeCompleteBuilder::Create(
732 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
733 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800734}
735
Chienyuan676cefe2019-11-22 16:46:24 +0800736void DualModeController::HciWritePageScanType(
737 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700738 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800739 auto packet = bluetooth::hci::WritePageScanTypeCompleteBuilder::Create(
740 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
741 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800742}
743
Chienyuan676cefe2019-11-22 16:46:24 +0800744void DualModeController::HciWriteInquiryScanType(
745 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700746 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800747 auto packet = bluetooth::hci::WriteInquiryScanTypeCompleteBuilder::Create(
748 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
749 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800750}
751
Chienyuan676cefe2019-11-22 16:46:24 +0800752void DualModeController::HciAuthenticationRequested(
753 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700754 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800755 uint16_t handle = args.begin().extract<uint16_t>();
Chienyuan3d8a8032019-11-01 18:04:07 +0800756 auto status = link_layer_controller_.AuthenticationRequested(handle);
Myles Watsone22dde22019-01-18 11:42:33 -0800757
Chienyuanc27da092019-11-20 19:29:43 +0800758 auto packet = bluetooth::hci::AuthenticationRequestedStatusBuilder::Create(
759 status, kNumCommandPackets);
760 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800761}
762
Chienyuan676cefe2019-11-22 16:46:24 +0800763void DualModeController::HciSetConnectionEncryption(
764 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700765 ASSERT_LOG(args.size() == 3, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800766 auto args_itr = args.begin();
767 uint16_t handle = args_itr.extract<uint16_t>();
768 uint8_t encryption_enable = args_itr.extract<uint8_t>();
Chienyuan3d8a8032019-11-01 18:04:07 +0800769 auto status =
770 link_layer_controller_.SetConnectionEncryption(handle, encryption_enable);
Myles Watsone22dde22019-01-18 11:42:33 -0800771
Chienyuanc27da092019-11-20 19:29:43 +0800772 auto packet = bluetooth::hci::SetConnectionEncryptionStatusBuilder::Create(
773 status, kNumCommandPackets);
774 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800775}
776
Chienyuan676cefe2019-11-22 16:46:24 +0800777void DualModeController::HciChangeConnectionLinkKey(
778 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700779 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Chienyuanad340b02019-09-25 19:23:21 +0800780 auto args_itr = args.begin();
781 uint16_t handle = args_itr.extract<uint16_t>();
782
Chienyuan3d8a8032019-11-01 18:04:07 +0800783 auto status = link_layer_controller_.ChangeConnectionLinkKey(handle);
Chienyuanad340b02019-09-25 19:23:21 +0800784
Chienyuanc27da092019-11-20 19:29:43 +0800785 auto packet = bluetooth::hci::ChangeConnectionLinkKeyStatusBuilder::Create(
786 status, kNumCommandPackets);
787 send_event_(std::move(packet));
Chienyuanad340b02019-09-25 19:23:21 +0800788}
789
Chienyuan676cefe2019-11-22 16:46:24 +0800790void DualModeController::HciMasterLinkKey(
791 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700792 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Chienyuan9145e7a2019-10-02 15:18:55 +0800793 auto args_itr = args.begin();
794 uint8_t key_flag = args_itr.extract<uint8_t>();
795
Chienyuan3d8a8032019-11-01 18:04:07 +0800796 auto status = link_layer_controller_.MasterLinkKey(key_flag);
Chienyuan9145e7a2019-10-02 15:18:55 +0800797
Chienyuanc27da092019-11-20 19:29:43 +0800798 auto packet = bluetooth::hci::MasterLinkKeyStatusBuilder::Create(
799 status, kNumCommandPackets);
800 send_event_(std::move(packet));
Chienyuan9145e7a2019-10-02 15:18:55 +0800801}
802
Chienyuan676cefe2019-11-22 16:46:24 +0800803void DualModeController::HciWriteAuthenticationEnable(
804 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700805 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800806 properties_.SetAuthenticationEnable(args[0]);
Chienyuanc27da092019-11-20 19:29:43 +0800807 auto packet =
808 bluetooth::hci::WriteAuthenticationEnableCompleteBuilder::Create(
809 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
810 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800811}
812
Chienyuan676cefe2019-11-22 16:46:24 +0800813void DualModeController::HciReadAuthenticationEnable(
814 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700815 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800816 auto packet = bluetooth::hci::ReadAuthenticationEnableCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800817 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +0800818 static_cast<bluetooth::hci::AuthenticationEnable>(
819 properties_.GetAuthenticationEnable()));
820 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800821}
822
Chienyuan676cefe2019-11-22 16:46:24 +0800823void DualModeController::HciWriteClassOfDevice(
824 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700825 ASSERT_LOG(args.size() == 3, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800826 properties_.SetClassOfDevice(args[0], args[1], args[2]);
Chienyuanc27da092019-11-20 19:29:43 +0800827 auto packet = bluetooth::hci::WriteClassOfDeviceCompleteBuilder::Create(
828 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
829 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800830}
831
Chienyuan676cefe2019-11-22 16:46:24 +0800832void DualModeController::HciWritePageTimeout(
833 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700834 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800835 auto packet = bluetooth::hci::WritePageTimeoutCompleteBuilder::Create(
836 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
837 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800838}
839
Chienyuan676cefe2019-11-22 16:46:24 +0800840void DualModeController::HciHoldMode(bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700841 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Chienyuanad340b02019-09-25 19:23:21 +0800842 auto args_itr = args.begin();
843 uint16_t handle = args_itr.extract<uint16_t>();
844 uint16_t hold_mode_max_interval = args_itr.extract<uint16_t>();
845 uint16_t hold_mode_min_interval = args_itr.extract<uint16_t>();
846
Chienyuan3d8a8032019-11-01 18:04:07 +0800847 auto status = link_layer_controller_.HoldMode(handle, hold_mode_max_interval,
848 hold_mode_min_interval);
Chienyuanad340b02019-09-25 19:23:21 +0800849
Chienyuanc27da092019-11-20 19:29:43 +0800850 auto packet =
851 bluetooth::hci::HoldModeStatusBuilder::Create(status, kNumCommandPackets);
852 send_event_(std::move(packet));
Chienyuanad340b02019-09-25 19:23:21 +0800853}
854
Chienyuan676cefe2019-11-22 16:46:24 +0800855void DualModeController::HciSniffMode(
856 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700857 ASSERT_LOG(args.size() == 10, "%s size=%zu", __func__, args.size());
Chienyuanad340b02019-09-25 19:23:21 +0800858 auto args_itr = args.begin();
859 uint16_t handle = args_itr.extract<uint16_t>();
860 uint16_t sniff_max_interval = args_itr.extract<uint16_t>();
861 uint16_t sniff_min_interval = args_itr.extract<uint16_t>();
862 uint16_t sniff_attempt = args_itr.extract<uint16_t>();
863 uint16_t sniff_timeout = args_itr.extract<uint16_t>();
864
Chienyuan3d8a8032019-11-01 18:04:07 +0800865 auto status = link_layer_controller_.SniffMode(handle, sniff_max_interval,
866 sniff_min_interval,
867 sniff_attempt, sniff_timeout);
Chienyuanad340b02019-09-25 19:23:21 +0800868
Chienyuanc27da092019-11-20 19:29:43 +0800869 auto packet = bluetooth::hci::SniffModeStatusBuilder::Create(
870 status, kNumCommandPackets);
871 send_event_(std::move(packet));
Chienyuanad340b02019-09-25 19:23:21 +0800872}
873
Chienyuan676cefe2019-11-22 16:46:24 +0800874void DualModeController::HciExitSniffMode(
875 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700876 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Chienyuanad340b02019-09-25 19:23:21 +0800877 auto args_itr = args.begin();
878 uint16_t handle = args_itr.extract<uint16_t>();
879
Chienyuan3d8a8032019-11-01 18:04:07 +0800880 auto status = link_layer_controller_.ExitSniffMode(handle);
Chienyuanad340b02019-09-25 19:23:21 +0800881
Chienyuanc27da092019-11-20 19:29:43 +0800882 auto packet = bluetooth::hci::ExitSniffModeStatusBuilder::Create(
883 status, kNumCommandPackets);
884 send_event_(std::move(packet));
Chienyuanad340b02019-09-25 19:23:21 +0800885}
886
Chienyuan676cefe2019-11-22 16:46:24 +0800887void DualModeController::HciQosSetup(bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700888 ASSERT_LOG(args.size() == 20, "%s size=%zu", __func__, args.size());
Chienyuanad340b02019-09-25 19:23:21 +0800889 auto args_itr = args.begin();
890 uint16_t handle = args_itr.extract<uint16_t>();
891 args_itr.extract<uint8_t>(); // unused
892 uint8_t service_type = args_itr.extract<uint8_t>();
893 uint32_t token_rate = args_itr.extract<uint32_t>();
894 uint32_t peak_bandwidth = args_itr.extract<uint32_t>();
895 uint32_t latency = args_itr.extract<uint32_t>();
896 uint32_t delay_variation = args_itr.extract<uint32_t>();
897
Chienyuan3d8a8032019-11-01 18:04:07 +0800898 auto status =
899 link_layer_controller_.QosSetup(handle, service_type, token_rate,
900 peak_bandwidth, latency, delay_variation);
Chienyuanad340b02019-09-25 19:23:21 +0800901
Chienyuanc27da092019-11-20 19:29:43 +0800902 auto packet =
903 bluetooth::hci::QosSetupStatusBuilder::Create(status, kNumCommandPackets);
904 send_event_(std::move(packet));
Chienyuanad340b02019-09-25 19:23:21 +0800905}
906
Chienyuan676cefe2019-11-22 16:46:24 +0800907void DualModeController::HciWriteDefaultLinkPolicySettings(
908 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700909 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +0800910 auto packet =
911 bluetooth::hci::WriteDefaultLinkPolicySettingsCompleteBuilder::Create(
912 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
913 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800914}
915
Chienyuan676cefe2019-11-22 16:46:24 +0800916void DualModeController::HciFlowSpecification(
917 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700918 ASSERT_LOG(args.size() == 21, "%s size=%zu", __func__, args.size());
Chienyuanad340b02019-09-25 19:23:21 +0800919 auto args_itr = args.begin();
920 uint16_t handle = args_itr.extract<uint16_t>();
921 args_itr.extract<uint8_t>(); // unused
922 uint8_t flow_direction = args_itr.extract<uint8_t>();
923 uint8_t service_type = args_itr.extract<uint8_t>();
924 uint32_t token_rate = args_itr.extract<uint32_t>();
925 uint32_t token_bucket_size = args_itr.extract<uint32_t>();
926 uint32_t peak_bandwidth = args_itr.extract<uint32_t>();
927 uint32_t access_latency = args_itr.extract<uint32_t>();
928
Chienyuan3d8a8032019-11-01 18:04:07 +0800929 auto status = link_layer_controller_.FlowSpecification(
930 handle, flow_direction, service_type, token_rate, token_bucket_size,
931 peak_bandwidth, access_latency);
Chienyuanad340b02019-09-25 19:23:21 +0800932
Chienyuanc27da092019-11-20 19:29:43 +0800933 auto packet = bluetooth::hci::FlowSpecificationStatusBuilder::Create(
934 status, kNumCommandPackets);
935 send_event_(std::move(packet));
Chienyuanad340b02019-09-25 19:23:21 +0800936}
937
Chienyuan676cefe2019-11-22 16:46:24 +0800938void DualModeController::HciWriteLinkPolicySettings(
939 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700940 ASSERT_LOG(args.size() == 4, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800941
942 auto args_itr = args.begin();
943 uint16_t handle = args_itr.extract<uint16_t>();
944 uint16_t settings = args_itr.extract<uint16_t>();
945
Chienyuan3d8a8032019-11-01 18:04:07 +0800946 auto status =
947 link_layer_controller_.WriteLinkPolicySettings(handle, settings);
Myles Watsone22dde22019-01-18 11:42:33 -0800948
Chienyuan3d8a8032019-11-01 18:04:07 +0800949 auto packet = bluetooth::hci::WriteLinkPolicySettingsCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800950 kNumCommandPackets, status, handle);
Chienyuan3d8a8032019-11-01 18:04:07 +0800951 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800952}
953
Chienyuan676cefe2019-11-22 16:46:24 +0800954void DualModeController::HciWriteLinkSupervisionTimeout(
955 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700956 ASSERT_LOG(args.size() == 4, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800957
958 auto args_itr = args.begin();
959 uint16_t handle = args_itr.extract<uint16_t>();
960 uint16_t timeout = args_itr.extract<uint16_t>();
961
Chienyuan3d8a8032019-11-01 18:04:07 +0800962 auto status =
963 link_layer_controller_.WriteLinkSupervisionTimeout(handle, timeout);
964 auto packet =
965 bluetooth::hci::WriteLinkSupervisionTimeoutCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800966 kNumCommandPackets, status, handle);
Chienyuan3d8a8032019-11-01 18:04:07 +0800967 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800968}
969
Chienyuan676cefe2019-11-22 16:46:24 +0800970void DualModeController::HciReadLocalName(
971 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700972 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +0800973
974 std::array<uint8_t, 248> local_name;
975 local_name.fill(0x00);
976 size_t len = properties_.GetName().size();
977 if (len > 247) {
978 len = 247; // one byte for NULL octet (0x00)
979 }
980 std::copy_n(properties_.GetName().begin(), len, local_name.begin());
981
982 auto packet = bluetooth::hci::ReadLocalNameCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +0800983 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, local_name);
Chienyuan3d8a8032019-11-01 18:04:07 +0800984 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800985}
986
Chienyuan676cefe2019-11-22 16:46:24 +0800987void DualModeController::HciWriteLocalName(
988 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700989 ASSERT_LOG(args.size() == 248, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -0800990 std::vector<uint8_t> clipped(args.begin(), args.begin() + LastNonZero(args) + 1);
991 properties_.SetName(clipped);
Chienyuanc27da092019-11-20 19:29:43 +0800992 auto packet = bluetooth::hci::WriteLocalNameCompleteBuilder::Create(
993 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
994 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -0800995}
996
Chienyuan676cefe2019-11-22 16:46:24 +0800997void DualModeController::HciWriteExtendedInquiryResponse(
998 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -0700999 ASSERT_LOG(args.size() == 241, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001000 // Strip FEC byte and trailing zeros
1001 std::vector<uint8_t> clipped(args.begin() + 1, args.begin() + LastNonZero(args) + 1);
1002 properties_.SetExtendedInquiryData(clipped);
Chienyuanc27da092019-11-20 19:29:43 +08001003 auto packet =
1004 bluetooth::hci::WriteExtendedInquiryResponseCompleteBuilder::Create(
1005 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1006 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001007}
1008
Chienyuan676cefe2019-11-22 16:46:24 +08001009void DualModeController::HciRefreshEncryptionKey(
1010 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001011 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsonb293d732019-08-19 12:35:36 -07001012 auto args_itr = args.begin();
1013 uint16_t handle = args_itr.extract<uint16_t>();
Chienyuanc27da092019-11-20 19:29:43 +08001014 auto status_packet =
1015 bluetooth::hci::RefreshEncryptionKeyStatusBuilder::Create(
1016 bluetooth::hci::ErrorCode::SUCCESS, kNumCommandPackets);
1017 send_event_(std::move(status_packet));
Myles Watsonb293d732019-08-19 12:35:36 -07001018 // TODO: Support this in the link layer
Chienyuanc27da092019-11-20 19:29:43 +08001019 auto complete_packet =
1020 bluetooth::hci::EncryptionKeyRefreshCompleteBuilder::Create(
1021 bluetooth::hci::ErrorCode::SUCCESS, handle);
1022 send_event_(std::move(complete_packet));
Myles Watsonb293d732019-08-19 12:35:36 -07001023}
1024
Chienyuan676cefe2019-11-22 16:46:24 +08001025void DualModeController::HciWriteVoiceSetting(
1026 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001027 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +08001028 auto packet = bluetooth::hci::WriteVoiceSettingCompleteBuilder::Create(
1029 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1030 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001031}
1032
Chienyuan676cefe2019-11-22 16:46:24 +08001033void DualModeController::HciWriteCurrentIacLap(
1034 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001035 ASSERT(args.size() > 0);
1036 ASSERT(args.size() == 1 + (3 * args[0])); // count + 3-byte IACs
Chienyuanc27da092019-11-20 19:29:43 +08001037 auto packet = bluetooth::hci::WriteCurrentIacLapCompleteBuilder::Create(
1038 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1039 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001040}
1041
Chienyuan676cefe2019-11-22 16:46:24 +08001042void DualModeController::HciWriteInquiryScanActivity(
1043 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001044 ASSERT_LOG(args.size() == 4, "%s size=%zu", __func__, args.size());
Chienyuanc27da092019-11-20 19:29:43 +08001045 auto packet = bluetooth::hci::WriteInquiryScanActivityCompleteBuilder::Create(
1046 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1047 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001048}
1049
Chienyuan676cefe2019-11-22 16:46:24 +08001050void DualModeController::HciWriteScanEnable(
1051 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001052 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001053 link_layer_controller_.SetInquiryScanEnable(args[0] & 0x1);
1054 link_layer_controller_.SetPageScanEnable(args[0] & 0x2);
Chienyuanc27da092019-11-20 19:29:43 +08001055 auto packet = bluetooth::hci::WriteScanEnableCompleteBuilder::Create(
1056 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1057 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001058}
1059
Chienyuan676cefe2019-11-22 16:46:24 +08001060void DualModeController::HciSetEventFilter(
1061 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001062 ASSERT(args.size() > 0);
Chienyuanc27da092019-11-20 19:29:43 +08001063 auto packet = bluetooth::hci::SetEventFilterCompleteBuilder::Create(
1064 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1065 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001066}
1067
Chienyuan676cefe2019-11-22 16:46:24 +08001068void DualModeController::HciInquiry(bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001069 ASSERT_LOG(args.size() == 5, "%s size=%zu", __func__, args.size());
1070 link_layer_controller_.SetInquiryLAP(args[0] | (args[1], 8) | (args[2], 16));
Myles Watsone22dde22019-01-18 11:42:33 -08001071 link_layer_controller_.SetInquiryMaxResponses(args[4]);
1072 link_layer_controller_.StartInquiry(std::chrono::milliseconds(args[3] * 1280));
1073
Chienyuanc27da092019-11-20 19:29:43 +08001074 auto packet = bluetooth::hci::InquiryStatusBuilder::Create(
1075 bluetooth::hci::ErrorCode::SUCCESS, kNumCommandPackets);
1076 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001077}
1078
Chienyuan676cefe2019-11-22 16:46:24 +08001079void DualModeController::HciInquiryCancel(
1080 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001081 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001082 link_layer_controller_.InquiryCancel();
Chienyuanc27da092019-11-20 19:29:43 +08001083 auto packet = bluetooth::hci::InquiryCancelCompleteBuilder::Create(
1084 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1085 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001086}
1087
Chienyuan676cefe2019-11-22 16:46:24 +08001088void DualModeController::HciAcceptConnectionRequest(
1089 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001090 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001091 Address addr = args.begin().extract<Address>();
1092 bool try_role_switch = args[6] == 0;
Chienyuan3d8a8032019-11-01 18:04:07 +08001093 auto status =
1094 link_layer_controller_.AcceptConnectionRequest(addr, try_role_switch);
Chienyuanc27da092019-11-20 19:29:43 +08001095 auto packet = bluetooth::hci::AcceptConnectionRequestStatusBuilder::Create(
1096 status, kNumCommandPackets);
1097 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001098}
1099
Chienyuan676cefe2019-11-22 16:46:24 +08001100void DualModeController::HciRejectConnectionRequest(
1101 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001102 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001103 auto args_itr = args.begin();
1104 Address addr = args_itr.extract<Address>();
1105 uint8_t reason = args_itr.extract<uint8_t>();
Chienyuan3d8a8032019-11-01 18:04:07 +08001106 auto status = link_layer_controller_.RejectConnectionRequest(addr, reason);
Chienyuanc27da092019-11-20 19:29:43 +08001107 auto packet = bluetooth::hci::RejectConnectionRequestStatusBuilder::Create(
1108 status, kNumCommandPackets);
1109 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001110}
1111
Chienyuan676cefe2019-11-22 16:46:24 +08001112void DualModeController::HciLinkKeyRequestReply(
1113 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001114 ASSERT_LOG(args.size() == 22, "%s size=%zu", __func__, args.size());
Myles Watsone066df42019-11-13 14:39:18 -08001115 auto args_it = args.begin();
1116 Address addr = args_it.extract<Address>();
1117 auto key = args.begin().extract<std::array<uint8_t, 16>>();
Chienyuan3d8a8032019-11-01 18:04:07 +08001118 auto status = link_layer_controller_.LinkKeyRequestReply(addr, key);
Chienyuanc27da092019-11-20 19:29:43 +08001119 auto packet = bluetooth::hci::LinkKeyRequestReplyCompleteBuilder::Create(
1120 kNumCommandPackets, status);
Chienyuan3d8a8032019-11-01 18:04:07 +08001121 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001122}
1123
Chienyuan676cefe2019-11-22 16:46:24 +08001124void DualModeController::HciLinkKeyRequestNegativeReply(
1125 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001126 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001127 Address addr = args.begin().extract<Address>();
Chienyuan3d8a8032019-11-01 18:04:07 +08001128 auto status = link_layer_controller_.LinkKeyRequestNegativeReply(addr);
1129 auto packet =
1130 bluetooth::hci::LinkKeyRequestNegativeReplyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001131 kNumCommandPackets, status, addr);
Chienyuan3d8a8032019-11-01 18:04:07 +08001132 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001133}
1134
Chienyuan676cefe2019-11-22 16:46:24 +08001135void DualModeController::HciDeleteStoredLinkKey(
1136 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001137 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001138
1139 uint16_t deleted_keys = 0;
1140
1141 if (args[6] == 0) {
1142 Address addr = args.begin().extract<Address>();
1143 deleted_keys = security_manager_.DeleteKey(addr);
1144 }
1145
1146 if (args[6] == 1) {
1147 security_manager_.DeleteAllKeys();
1148 }
1149
Chienyuan3d8a8032019-11-01 18:04:07 +08001150 auto packet = bluetooth::hci::DeleteStoredLinkKeyCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001151 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, deleted_keys);
Chienyuan3d8a8032019-11-01 18:04:07 +08001152
1153 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001154}
1155
Chienyuan676cefe2019-11-22 16:46:24 +08001156void DualModeController::HciRemoteNameRequest(
1157 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001158 ASSERT_LOG(args.size() == 10, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001159
1160 Address remote_addr = args.begin().extract<Address>();
1161
Chienyuan3d8a8032019-11-01 18:04:07 +08001162 auto status = link_layer_controller_.SendCommandToRemoteByAddress(
Myles Watsone066df42019-11-13 14:39:18 -08001163 bluetooth::hci::OpCode::REMOTE_NAME_REQUEST, args, remote_addr);
Myles Watsone22dde22019-01-18 11:42:33 -08001164
Chienyuanc27da092019-11-20 19:29:43 +08001165 auto packet = bluetooth::hci::RemoteNameRequestStatusBuilder::Create(
1166 status, kNumCommandPackets);
1167 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001168}
1169
Chienyuan676cefe2019-11-22 16:46:24 +08001170void DualModeController::HciLeSetEventMask(
1171 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001172 ASSERT_LOG(args.size() == 8, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001173 /*
1174 uint64_t mask = args.begin().extract<uint64_t>();
1175 link_layer_controller_.SetLeEventMask(mask);
1176 */
Chienyuanc27da092019-11-20 19:29:43 +08001177 auto packet = bluetooth::hci::LeSetEventMaskCompleteBuilder::Create(
1178 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1179 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001180}
1181
Chienyuan676cefe2019-11-22 16:46:24 +08001182void DualModeController::HciLeReadBufferSize(
1183 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001184 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +08001185
Chienyuanc27da092019-11-20 19:29:43 +08001186 bluetooth::hci::LeBufferSize le_buffer_size;
1187 le_buffer_size.le_data_packet_length_ = properties_.GetLeDataPacketLength();
1188 le_buffer_size.total_num_le_packets_ = properties_.GetTotalNumLeDataPackets();
Chienyuan3d8a8032019-11-01 18:04:07 +08001189
Chienyuanc27da092019-11-20 19:29:43 +08001190 auto packet = bluetooth::hci::LeReadBufferSizeCompleteBuilder::Create(
1191 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, le_buffer_size);
Chienyuan3d8a8032019-11-01 18:04:07 +08001192 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001193}
1194
Chienyuan676cefe2019-11-22 16:46:24 +08001195void DualModeController::HciLeReadLocalSupportedFeatures(
1196 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001197 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +08001198 auto packet =
1199 bluetooth::hci::LeReadLocalSupportedFeaturesCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001200 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +08001201 properties_.GetLeSupportedFeatures());
1202 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001203}
1204
Chienyuan676cefe2019-11-22 16:46:24 +08001205void DualModeController::HciLeSetRandomAddress(
1206 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001207 ASSERT_LOG(args.size() == 6, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001208 properties_.SetLeAddress(args.begin().extract<Address>());
Chienyuanc27da092019-11-20 19:29:43 +08001209 auto packet = bluetooth::hci::LeSetRandomAddressCompleteBuilder::Create(
1210 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1211 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001212}
1213
Chienyuan676cefe2019-11-22 16:46:24 +08001214void DualModeController::HciLeSetAdvertisingParameters(
1215 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001216 ASSERT_LOG(args.size() == 15, "%s size=%zu", __func__, args.size());
Myles Watson99aea892019-09-02 15:54:08 -07001217 auto args_itr = args.begin();
1218 properties_.SetLeAdvertisingParameters(
1219 args_itr.extract<uint16_t>() /* AdverisingIntervalMin */,
1220 args_itr.extract<uint16_t>() /* AdverisingIntervalMax */, args_itr.extract<uint8_t>() /* AdverisingType */,
1221 args_itr.extract<uint8_t>() /* OwnAddressType */, args_itr.extract<uint8_t>() /* PeerAddressType */,
1222 args_itr.extract<Address>() /* PeerAddress */, args_itr.extract<uint8_t>() /* AdvertisingChannelMap */,
1223 args_itr.extract<uint8_t>() /* AdvertisingFilterPolicy */
1224 );
Myles Watsone22dde22019-01-18 11:42:33 -08001225
Chienyuanc27da092019-11-20 19:29:43 +08001226 auto packet =
1227 bluetooth::hci::LeSetAdvertisingParametersCompleteBuilder::Create(
1228 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1229 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001230}
1231
Chienyuan676cefe2019-11-22 16:46:24 +08001232void DualModeController::HciLeSetAdvertisingData(
1233 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001234 ASSERT_LOG(args.size() == 32, "%s size=%zu", __func__, args.size());
Myles Watson99aea892019-09-02 15:54:08 -07001235 properties_.SetLeAdvertisement(std::vector<uint8_t>(args.begin() + 1, args.end()));
Chienyuanc27da092019-11-20 19:29:43 +08001236 auto packet = bluetooth::hci::LeSetAdvertisingDataCompleteBuilder::Create(
1237 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1238 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001239}
1240
Chienyuan676cefe2019-11-22 16:46:24 +08001241void DualModeController::HciLeSetScanResponseData(
1242 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001243 ASSERT_LOG(args.size() == 32, "%s size=%zu", __func__, args.size());
Myles Watson99aea892019-09-02 15:54:08 -07001244 properties_.SetLeScanResponse(std::vector<uint8_t>(args.begin() + 1, args.end()));
Chienyuanc27da092019-11-20 19:29:43 +08001245 auto packet = bluetooth::hci::LeSetScanResponseDataCompleteBuilder::Create(
1246 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1247 send_event_(std::move(packet));
Myles Watson99aea892019-09-02 15:54:08 -07001248}
1249
Chienyuan676cefe2019-11-22 16:46:24 +08001250void DualModeController::HciLeSetAdvertisingEnable(
1251 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001252 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +08001253 auto status = link_layer_controller_.SetLeAdvertisingEnable(
1254 args.begin().extract<uint8_t>());
Chienyuanc27da092019-11-20 19:29:43 +08001255 auto packet = bluetooth::hci::LeSetAdvertisingEnableCompleteBuilder::Create(
1256 kNumCommandPackets, status);
1257 send_event_(std::move(packet));
Myles Watson99aea892019-09-02 15:54:08 -07001258}
1259
Chienyuan676cefe2019-11-22 16:46:24 +08001260void DualModeController::HciLeSetScanParameters(
1261 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001262 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001263 link_layer_controller_.SetLeScanType(args[0]);
Myles Watson1a0a0da2019-10-17 11:36:33 -07001264 link_layer_controller_.SetLeScanInterval(args[1] | (args[2], 8));
1265 link_layer_controller_.SetLeScanWindow(args[3] | (args[4], 8));
Myles Watsone22dde22019-01-18 11:42:33 -08001266 link_layer_controller_.SetLeAddressType(args[5]);
1267 link_layer_controller_.SetLeScanFilterPolicy(args[6]);
Chienyuanc27da092019-11-20 19:29:43 +08001268 auto packet = bluetooth::hci::LeSetScanParametersCompleteBuilder::Create(
1269 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1270 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001271}
1272
Chienyuan676cefe2019-11-22 16:46:24 +08001273void DualModeController::HciLeSetScanEnable(
1274 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001275 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001276 link_layer_controller_.SetLeScanEnable(args[0]);
1277 link_layer_controller_.SetLeFilterDuplicates(args[1]);
Chienyuanc27da092019-11-20 19:29:43 +08001278 auto packet = bluetooth::hci::LeSetScanEnableCompleteBuilder::Create(
1279 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1280 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001281}
1282
Chienyuan676cefe2019-11-22 16:46:24 +08001283void DualModeController::HciLeCreateConnection(
1284 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001285 ASSERT_LOG(args.size() == 25, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001286 auto args_itr = args.begin();
1287 link_layer_controller_.SetLeScanInterval(args_itr.extract<uint16_t>());
1288 link_layer_controller_.SetLeScanWindow(args_itr.extract<uint16_t>());
1289 uint8_t initiator_filter_policy = args_itr.extract<uint8_t>();
1290 link_layer_controller_.SetLeInitiatorFilterPolicy(initiator_filter_policy);
1291
1292 if (initiator_filter_policy == 0) { // White list not used
1293 uint8_t peer_address_type = args_itr.extract<uint8_t>();
1294 Address peer_address = args_itr.extract<Address>();
1295 link_layer_controller_.SetLePeerAddressType(peer_address_type);
1296 link_layer_controller_.SetLePeerAddress(peer_address);
1297 }
1298 link_layer_controller_.SetLeAddressType(args_itr.extract<uint8_t>());
1299 link_layer_controller_.SetLeConnectionIntervalMin(args_itr.extract<uint16_t>());
1300 link_layer_controller_.SetLeConnectionIntervalMax(args_itr.extract<uint16_t>());
1301 link_layer_controller_.SetLeConnectionLatency(args_itr.extract<uint16_t>());
1302 link_layer_controller_.SetLeSupervisionTimeout(args_itr.extract<uint16_t>());
1303 link_layer_controller_.SetLeMinimumCeLength(args_itr.extract<uint16_t>());
1304 link_layer_controller_.SetLeMaximumCeLength(args_itr.extract<uint16_t>());
1305
Chienyuan3d8a8032019-11-01 18:04:07 +08001306 auto status = link_layer_controller_.SetLeConnect(true);
Myles Watsone22dde22019-01-18 11:42:33 -08001307
Chienyuanc27da092019-11-20 19:29:43 +08001308 auto packet = bluetooth::hci::LeCreateConnectionStatusBuilder::Create(
1309 status, kNumCommandPackets);
1310 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001311}
1312
Chienyuan676cefe2019-11-22 16:46:24 +08001313void DualModeController::HciLeConnectionUpdate(
1314 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001315 ASSERT_LOG(args.size() == 14, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001316
Chienyuanc27da092019-11-20 19:29:43 +08001317 auto status_packet = bluetooth::hci::LeConnectionUpdateStatusBuilder::Create(
Chienyuan3d8a8032019-11-01 18:04:07 +08001318 bluetooth::hci::ErrorCode::CONNECTION_REJECTED_UNACCEPTABLE_BD_ADDR,
Chienyuanc27da092019-11-20 19:29:43 +08001319 kNumCommandPackets);
1320 send_event_(std::move(status_packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001321
Chienyuanc27da092019-11-20 19:29:43 +08001322 auto complete_packet =
1323 bluetooth::hci::LeConnectionUpdateCompleteBuilder::Create(
1324 bluetooth::hci::ErrorCode::SUCCESS, 0x0002, 0x0006, 0x0000, 0x01f4);
1325 send_event_(std::move(complete_packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001326}
1327
Chienyuan676cefe2019-11-22 16:46:24 +08001328void DualModeController::HciCreateConnection(
1329 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001330 ASSERT_LOG(args.size() == 13, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001331
1332 auto args_itr = args.begin();
1333 Address address = args_itr.extract<Address>();
1334 uint16_t packet_type = args_itr.extract<uint16_t>();
1335 uint8_t page_scan_mode = args_itr.extract<uint8_t>();
1336 uint16_t clock_offset = args_itr.extract<uint16_t>();
1337 uint8_t allow_role_switch = args_itr.extract<uint8_t>();
1338
Chienyuan3d8a8032019-11-01 18:04:07 +08001339 auto status = link_layer_controller_.CreateConnection(
1340 address, packet_type, page_scan_mode, clock_offset, allow_role_switch);
Myles Watsone22dde22019-01-18 11:42:33 -08001341
Chienyuanc27da092019-11-20 19:29:43 +08001342 auto packet = bluetooth::hci::CreateConnectionStatusBuilder::Create(
1343 status, kNumCommandPackets);
1344 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001345}
1346
Chienyuan676cefe2019-11-22 16:46:24 +08001347void DualModeController::HciDisconnect(
1348 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001349 ASSERT_LOG(args.size() == 3, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001350
1351 auto args_itr = args.begin();
1352 uint16_t handle = args_itr.extract<uint16_t>();
1353 uint8_t reason = args_itr.extract<uint8_t>();
1354
Chienyuan3d8a8032019-11-01 18:04:07 +08001355 auto status = link_layer_controller_.Disconnect(handle, reason);
Myles Watsone22dde22019-01-18 11:42:33 -08001356
Chienyuanc27da092019-11-20 19:29:43 +08001357 auto packet = bluetooth::hci::DisconnectStatusBuilder::Create(
1358 status, kNumCommandPackets);
1359 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001360}
1361
Chienyuan676cefe2019-11-22 16:46:24 +08001362void DualModeController::HciLeConnectionCancel(
1363 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001364 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001365 link_layer_controller_.SetLeConnect(false);
Chienyuanc27da092019-11-20 19:29:43 +08001366 auto packet = bluetooth::hci::LeCreateConnectionCancelStatusBuilder::Create(
1367 bluetooth::hci::ErrorCode::SUCCESS, kNumCommandPackets);
1368 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001369 /* For testing Jakub's patch: Figure out a neat way to call this without
1370 recompiling. I'm thinking about a bad device. */
1371 /*
1372 SendCommandCompleteOnlyStatus(OpCode::LE_CREATE_CONNECTION_CANCEL,
Chienyuan3d8a8032019-11-01 18:04:07 +08001373 bluetooth::hci::ErrorCode::COMMAND_DISALLOWED);
Myles Watsone22dde22019-01-18 11:42:33 -08001374 */
1375}
1376
Chienyuan676cefe2019-11-22 16:46:24 +08001377void DualModeController::HciLeReadWhiteListSize(
1378 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001379 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +08001380 auto packet = bluetooth::hci::LeReadWhiteListSizeCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001381 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +08001382 properties_.GetLeWhiteListSize());
1383 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001384}
1385
Chienyuan676cefe2019-11-22 16:46:24 +08001386void DualModeController::HciLeClearWhiteList(
1387 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001388 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001389 link_layer_controller_.LeWhiteListClear();
Chienyuanc27da092019-11-20 19:29:43 +08001390 auto packet = bluetooth::hci::LeClearWhiteListCompleteBuilder::Create(
1391 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1392 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001393}
1394
Chienyuan676cefe2019-11-22 16:46:24 +08001395void DualModeController::HciLeAddDeviceToWhiteList(
1396 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001397 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001398
1399 if (link_layer_controller_.LeWhiteListFull()) {
Chienyuanc27da092019-11-20 19:29:43 +08001400 auto packet = bluetooth::hci::LeAddDeviceToWhiteListCompleteBuilder::Create(
1401 kNumCommandPackets,
Chienyuan3d8a8032019-11-01 18:04:07 +08001402 bluetooth::hci::ErrorCode::MEMORY_CAPACITY_EXCEEDED);
Chienyuanc27da092019-11-20 19:29:43 +08001403 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001404 return;
1405 }
1406 auto args_itr = args.begin();
1407 uint8_t addr_type = args_itr.extract<uint8_t>();
1408 Address address = args_itr.extract<Address>();
1409 link_layer_controller_.LeWhiteListAddDevice(address, addr_type);
Chienyuanc27da092019-11-20 19:29:43 +08001410 auto packet = bluetooth::hci::LeAddDeviceToWhiteListCompleteBuilder::Create(
1411 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1412 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001413}
1414
Chienyuan676cefe2019-11-22 16:46:24 +08001415void DualModeController::HciLeRemoveDeviceFromWhiteList(
1416 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001417 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001418
1419 auto args_itr = args.begin();
1420 uint8_t addr_type = args_itr.extract<uint8_t>();
1421 Address address = args_itr.extract<Address>();
1422 link_layer_controller_.LeWhiteListRemoveDevice(address, addr_type);
Chienyuanc27da092019-11-20 19:29:43 +08001423 auto packet =
1424 bluetooth::hci::LeRemoveDeviceFromWhiteListCompleteBuilder::Create(
1425 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1426 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001427}
1428
Calvin Huangee1980c2019-11-01 16:35:55 -07001429void DualModeController::HciLeClearResolvingList(
Chienyuan676cefe2019-11-22 16:46:24 +08001430 bluetooth::packet::PacketView<true> args) {
Calvin Huangee1980c2019-11-01 16:35:55 -07001431 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
1432 link_layer_controller_.LeResolvingListClear();
Chienyuanc27da092019-11-20 19:29:43 +08001433 auto packet = bluetooth::hci::LeClearResolvingListCompleteBuilder::Create(
1434 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1435 send_event_(std::move(packet));
Calvin Huangee1980c2019-11-01 16:35:55 -07001436}
1437
1438void DualModeController::HciLeAddDeviceToResolvingList(
Chienyuan676cefe2019-11-22 16:46:24 +08001439 bluetooth::packet::PacketView<true> args) {
Calvin Huangee1980c2019-11-01 16:35:55 -07001440 ASSERT_LOG(args.size() == 39, "%s size=%zu", __func__, args.size());
1441
1442 if (link_layer_controller_.LeResolvingListFull()) {
Chienyuanc27da092019-11-20 19:29:43 +08001443 auto packet =
1444 bluetooth::hci::LeAddDeviceToResolvingListCompleteBuilder::Create(
1445 kNumCommandPackets,
1446 bluetooth::hci::ErrorCode::MEMORY_CAPACITY_EXCEEDED);
1447 send_event_(std::move(packet));
Calvin Huangee1980c2019-11-01 16:35:55 -07001448 return;
1449 }
1450 auto args_itr = args.begin();
1451 uint8_t addr_type = args_itr.extract<uint8_t>();
1452 Address address = args_itr.extract<Address>();
1453 std::array<uint8_t, LinkLayerController::kIrk_size> peerIrk;
1454 std::array<uint8_t, LinkLayerController::kIrk_size> localIrk;
1455 for (size_t irk_ind = 0; irk_ind < LinkLayerController::kIrk_size;
1456 irk_ind++) {
1457 peerIrk[irk_ind] = args_itr.extract<uint8_t>();
1458 }
1459
1460 for (size_t irk_ind = 0; irk_ind < LinkLayerController::kIrk_size;
1461 irk_ind++) {
1462 localIrk[irk_ind] = args_itr.extract<uint8_t>();
1463 }
1464
1465 link_layer_controller_.LeResolvingListAddDevice(address, addr_type, peerIrk,
1466 localIrk);
Chienyuanc27da092019-11-20 19:29:43 +08001467 auto packet =
1468 bluetooth::hci::LeAddDeviceToResolvingListCompleteBuilder::Create(
1469 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1470 send_event_(std::move(packet));
Calvin Huangee1980c2019-11-01 16:35:55 -07001471}
1472
1473void DualModeController::HciLeRemoveDeviceFromResolvingList(
Chienyuan676cefe2019-11-22 16:46:24 +08001474 bluetooth::packet::PacketView<true> args) {
Calvin Huangee1980c2019-11-01 16:35:55 -07001475 ASSERT_LOG(args.size() == 7, "%s size=%zu", __func__, args.size());
1476
1477 auto args_itr = args.begin();
1478 uint8_t addr_type = args_itr.extract<uint8_t>();
1479 Address address = args_itr.extract<Address>();
1480 link_layer_controller_.LeResolvingListRemoveDevice(address, addr_type);
Chienyuanc27da092019-11-20 19:29:43 +08001481 auto packet =
1482 bluetooth::hci::LeRemoveDeviceFromResolvingListCompleteBuilder::Create(
1483 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1484 send_event_(std::move(packet));
Calvin Huangee1980c2019-11-01 16:35:55 -07001485}
1486
Chienyuan676cefe2019-11-22 16:46:24 +08001487void DualModeController::HciLeSetPrivacyMode(
1488 bluetooth::packet::PacketView<true> args) {
Calvin Huangee1980c2019-11-01 16:35:55 -07001489 ASSERT_LOG(args.size() == 8, "%s size=%zu", __func__, args.size());
1490
1491 auto args_itr = args.begin();
1492 uint8_t peer_identity_address_type = args_itr.extract<uint8_t>();
1493 Address peer_identity_address = args_itr.extract<Address>();
1494 uint8_t privacy_mode = args_itr.extract<uint8_t>();
1495
1496 if (link_layer_controller_.LeResolvingListContainsDevice(
1497 peer_identity_address, peer_identity_address_type)) {
1498 link_layer_controller_.LeSetPrivacyMode(
1499 peer_identity_address_type, peer_identity_address, privacy_mode);
1500 }
1501
Chienyuanc27da092019-11-20 19:29:43 +08001502 auto packet = bluetooth::hci::LeSetPrivacyModeCompleteBuilder::Create(
1503 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1504 send_event_(std::move(packet));
Calvin Huangee1980c2019-11-01 16:35:55 -07001505}
1506
Chienyuan676cefe2019-11-22 16:46:24 +08001507void DualModeController::HciLeReadRemoteFeatures(
1508 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001509 ASSERT_LOG(args.size() == 2, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001510
1511 uint16_t handle = args.begin().extract<uint16_t>();
1512
Chienyuan3d8a8032019-11-01 18:04:07 +08001513 auto status = link_layer_controller_.SendCommandToRemoteByHandle(
1514 bluetooth::hci::OpCode::LE_READ_REMOTE_FEATURES, args, handle);
Myles Watsone22dde22019-01-18 11:42:33 -08001515
Chienyuanc27da092019-11-20 19:29:43 +08001516 auto packet = bluetooth::hci::LeConnectionUpdateStatusBuilder::Create(
1517 status, kNumCommandPackets);
1518 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001519}
1520
Chienyuan676cefe2019-11-22 16:46:24 +08001521void DualModeController::HciLeRand(bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001522 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001523 uint64_t random_val = 0;
1524 for (size_t rand_bytes = 0; rand_bytes < sizeof(uint64_t); rand_bytes += sizeof(RAND_MAX)) {
1525 random_val = (random_val << (8 * sizeof(RAND_MAX))) | random();
1526 }
Chienyuan3d8a8032019-11-01 18:04:07 +08001527
1528 auto packet = bluetooth::hci::LeRandCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001529 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS, random_val);
Chienyuan3d8a8032019-11-01 18:04:07 +08001530 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001531}
1532
Chienyuan676cefe2019-11-22 16:46:24 +08001533void DualModeController::HciLeReadSupportedStates(
1534 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001535 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +08001536 auto packet = bluetooth::hci::LeReadSupportedStatesCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001537 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +08001538 properties_.GetLeSupportedStates());
1539 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001540}
1541
Chienyuan676cefe2019-11-22 16:46:24 +08001542void DualModeController::HciLeVendorCap(
1543 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001544 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001545 vector<uint8_t> caps = properties_.GetLeVendorCap();
1546 if (caps.size() == 0) {
Chienyuanc27da092019-11-20 19:29:43 +08001547 SendCommandCompleteUnknownOpCodeEvent(static_cast<uint16_t>(
1548 bluetooth::hci::OpCode::LE_GET_VENDOR_CAPABILITIES));
Myles Watsone22dde22019-01-18 11:42:33 -08001549 return;
1550 }
1551
Chienyuan3d8a8032019-11-01 18:04:07 +08001552 std::unique_ptr<bluetooth::packet::RawBuilder> raw_builder_ptr =
1553 std::make_unique<bluetooth::packet::RawBuilder>();
1554 raw_builder_ptr->AddOctets1(
1555 static_cast<uint8_t>(bluetooth::hci::ErrorCode::SUCCESS));
1556 raw_builder_ptr->AddOctets(properties_.GetLeVendorCap());
1557
1558 auto packet = bluetooth::hci::CommandCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001559 kNumCommandPackets, bluetooth::hci::OpCode::LE_GET_VENDOR_CAPABILITIES,
Chienyuan3d8a8032019-11-01 18:04:07 +08001560 std::move(raw_builder_ptr));
1561 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001562}
1563
Chienyuan676cefe2019-11-22 16:46:24 +08001564void DualModeController::HciLeVendorMultiAdv(
1565 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001566 ASSERT(args.size() > 0);
Chienyuanc27da092019-11-20 19:29:43 +08001567 SendCommandCompleteUnknownOpCodeEvent(
1568 static_cast<uint16_t>(bluetooth::hci::OpCode::LE_MULTI_ADVT));
Myles Watsone22dde22019-01-18 11:42:33 -08001569}
1570
Chienyuan676cefe2019-11-22 16:46:24 +08001571void DualModeController::HciLeAdvertisingFilter(
1572 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001573 ASSERT(args.size() > 0);
Chienyuanc27da092019-11-20 19:29:43 +08001574 SendCommandCompleteUnknownOpCodeEvent(
1575 static_cast<uint16_t>(bluetooth::hci::OpCode::LE_ADV_FILTER));
Myles Watsone22dde22019-01-18 11:42:33 -08001576}
1577
Chienyuan676cefe2019-11-22 16:46:24 +08001578void DualModeController::HciLeEnergyInfo(
1579 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001580 ASSERT(args.size() > 0);
Chienyuanc27da092019-11-20 19:29:43 +08001581 SendCommandCompleteUnknownOpCodeEvent(
1582 static_cast<uint16_t>(bluetooth::hci::OpCode::LE_ENERGY_INFO));
Myles Watsone22dde22019-01-18 11:42:33 -08001583}
1584
Chienyuan676cefe2019-11-22 16:46:24 +08001585void DualModeController::HciLeExtendedScanParams(
1586 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001587 ASSERT(args.size() > 0);
Chienyuanc27da092019-11-20 19:29:43 +08001588 SendCommandCompleteUnknownOpCodeEvent(
1589 static_cast<uint16_t>(bluetooth::hci::OpCode::LE_EXTENDED_SCAN_PARAMS));
Myles Watsone22dde22019-01-18 11:42:33 -08001590}
1591
Chienyuan676cefe2019-11-22 16:46:24 +08001592void DualModeController::HciLeStartEncryption(
1593 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001594 ASSERT_LOG(args.size() == 28, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001595
1596 auto args_itr = args.begin();
1597 uint16_t handle = args_itr.extract<uint16_t>();
1598 // uint64_t random_number = args_itr.extract<uint64_t>();
1599 // uint16_t encrypted_diversifier = args_itr.extract<uint16_t>();
1600 // std::vector<uint8_t> long_term_key;
1601 // for (size_t i = 0; i < 16; i++) {
1602 // long_term_key.push_back(args_itr.extract<uint18_t>();
1603 // }
Chienyuanc27da092019-11-20 19:29:43 +08001604 auto status_packet = bluetooth::hci::LeStartEncryptionStatusBuilder::Create(
1605 bluetooth::hci::ErrorCode::SUCCESS, kNumCommandPackets);
1606 send_event_(std::move(status_packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001607
Chienyuanc27da092019-11-20 19:29:43 +08001608 auto complete_packet = bluetooth::hci::EncryptionChangeBuilder::Create(
Chienyuan3d8a8032019-11-01 18:04:07 +08001609 bluetooth::hci::ErrorCode::SUCCESS, handle,
1610 bluetooth::hci::EncryptionEnabled::OFF);
Chienyuanc27da092019-11-20 19:29:43 +08001611 send_event_(std::move(complete_packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001612#if 0
1613
1614 std::shared_ptr<packets::AclPacketBuilder> encryption_information =
1615 std::make_shared<packets::AclPacketBuilder>(
1616 0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1617 std::vector<uint8_t>({}));
1618
1619 encryption_information->AddPayloadOctets2(0x0011);
1620 encryption_information->AddPayloadOctets2(0x0006);
1621 encryption_information->AddPayloadOctets1(0x06);
1622 encryption_information->AddPayloadOctets8(0x0706050403020100);
1623 encryption_information->AddPayloadOctets8(0x0F0E0D0C0B0A0908);
1624
1625 send_acl_(encryption_information);
1626
1627 encryption_information = std::make_shared<packets::AclPacketBuilder>(
1628 0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1629 std::vector<uint8_t>({}));
1630
1631 encryption_information->AddPayloadOctets2(0x000B);
1632 encryption_information->AddPayloadOctets2(0x0006);
1633 encryption_information->AddPayloadOctets1(0x07);
1634 encryption_information->AddPayloadOctets2(0xBEEF);
1635 encryption_information->AddPayloadOctets8(0x0706050403020100);
1636
1637 send_acl_(encryption_information);
1638
1639 encryption_information = std::make_shared<packets::AclPacketBuilder>(
1640 0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1641 std::vector<uint8_t>({}));
1642
1643 encryption_information->AddPayloadOctets2(0x0011);
1644 encryption_information->AddPayloadOctets2(0x0006);
1645 encryption_information->AddPayloadOctets1(0x08);
1646 encryption_information->AddPayloadOctets8(0x0F0E0D0C0B0A0908);
1647 encryption_information->AddPayloadOctets8(0x0706050403020100);
1648
1649 send_acl_(encryption_information);
1650
1651 encryption_information = std::make_shared<packets::AclPacketBuilder>(
1652 0x0002, Acl::FIRST_AUTOMATICALLY_FLUSHABLE, Acl::POINT_TO_POINT,
1653 std::vector<uint8_t>({}));
1654
1655 encryption_information->AddPayloadOctets2(0x0008);
1656 encryption_information->AddPayloadOctets2(0x0006);
1657 encryption_information->AddPayloadOctets1(0x09);
1658 encryption_information->AddPayloadOctets1(0x01);
1659 encryption_information->AddPayloadOctets6(0xDEADBEEFF00D);
1660 send_acl_(encryption_information);
1661 // send_event_(packets::EventPacketBuilder::CreateLeStartEncryption()->ToVector());
1662
1663#endif
1664}
1665
Chienyuan676cefe2019-11-22 16:46:24 +08001666void DualModeController::HciReadLoopbackMode(
1667 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001668 ASSERT_LOG(args.size() == 0, "%s size=%zu", __func__, args.size());
Chienyuan3d8a8032019-11-01 18:04:07 +08001669 auto packet = bluetooth::hci::ReadLoopbackModeCompleteBuilder::Create(
Chienyuanc27da092019-11-20 19:29:43 +08001670 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS,
Chienyuan3d8a8032019-11-01 18:04:07 +08001671 static_cast<bluetooth::hci::LoopbackMode>(loopback_mode_));
1672 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001673}
1674
Chienyuan676cefe2019-11-22 16:46:24 +08001675void DualModeController::HciWriteLoopbackMode(
1676 bluetooth::packet::PacketView<true> args) {
Myles Watson1a0a0da2019-10-17 11:36:33 -07001677 ASSERT_LOG(args.size() == 1, "%s size=%zu", __func__, args.size());
Myles Watsone22dde22019-01-18 11:42:33 -08001678 loopback_mode_ = static_cast<hci::LoopbackMode>(args[0]);
1679 // ACL channel
1680 uint16_t acl_handle = 0x123;
Chienyuan3d8a8032019-11-01 18:04:07 +08001681 auto packet_acl = bluetooth::hci::ConnectionCompleteBuilder::Create(
1682 bluetooth::hci::ErrorCode::SUCCESS, acl_handle, properties_.GetAddress(),
1683 bluetooth::hci::LinkType::ACL, bluetooth::hci::Enable::DISABLED);
1684 send_event_(std::move(packet_acl));
Myles Watsone22dde22019-01-18 11:42:33 -08001685 // SCO channel
1686 uint16_t sco_handle = 0x345;
Chienyuan3d8a8032019-11-01 18:04:07 +08001687 auto packet_sco = bluetooth::hci::ConnectionCompleteBuilder::Create(
1688 bluetooth::hci::ErrorCode::SUCCESS, sco_handle, properties_.GetAddress(),
1689 bluetooth::hci::LinkType::SCO, bluetooth::hci::Enable::DISABLED);
1690 send_event_(std::move(packet_sco));
Chienyuanc27da092019-11-20 19:29:43 +08001691 auto packet = bluetooth::hci::WriteLoopbackModeCompleteBuilder::Create(
1692 kNumCommandPackets, bluetooth::hci::ErrorCode::SUCCESS);
1693 send_event_(std::move(packet));
Myles Watsone22dde22019-01-18 11:42:33 -08001694}
1695
Myles Watson205c32e2019-11-12 13:16:32 -08001696void DualModeController::SetAddress(Address address) {
1697 properties_.SetAddress(address);
1698}
1699
Myles Watsone22dde22019-01-18 11:42:33 -08001700} // namespace test_vendor_lib