1/* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "dual_mode_controller" 18 19#include "dual_mode_controller.h" 20#include "device_factory.h" 21 22#include <memory> 23 24#include <base/logging.h> 25#include "base/files/file_util.h" 26#include "base/json/json_reader.h" 27#include "base/values.h" 28 29#include "osi/include/log.h" 30#include "osi/include/osi.h" 31#include "stack/include/hcidefs.h" 32 33using std::vector; 34 35namespace { 36 37// Included in certain events to indicate success (specific to the event 38// context). 39const uint8_t kSuccessStatus = 0; 40 41const uint8_t kUnknownHciCommand = 1; 42 43// The location of the config file loaded to populate controller attributes. 44const std::string kControllerPropertiesFile = 45 "/etc/bluetooth/controller_properties.json"; 46 47void LogCommand(const char* command) { 48 LOG_INFO(LOG_TAG, "Controller performing command: %s", command); 49} 50 51} // namespace 52 53namespace test_vendor_lib { 54 55void DualModeController::AddControllerEvent(std::chrono::milliseconds delay, 56 const TaskCallback& task) { 57 controller_events_.push_back(schedule_task_(delay, task)); 58} 59 60void DualModeController::AddConnectionAction(const TaskCallback& task, 61 uint16_t handle) { 62 for (size_t i = 0; i < connections_.size(); i++) 63 if (*connections_[i] == handle) connections_[i]->AddAction(task); 64} 65 66void DualModeController::SendCommandCompleteSuccess( 67 uint16_t command_opcode) const { 68 send_event_(EventPacket::CreateCommandCompleteOnlyStatusEvent( 69 command_opcode, kSuccessStatus)); 70} 71 72void DualModeController::SendCommandCompleteOnlyStatus(uint16_t command_opcode, 73 uint8_t status) const { 74 send_event_(EventPacket::CreateCommandCompleteOnlyStatusEvent(command_opcode, 75 status)); 76} 77 78void DualModeController::SendCommandStatus(uint8_t status, 79 uint16_t command_opcode) const { 80 send_event_(EventPacket::CreateCommandStatusEvent(status, command_opcode)); 81} 82 83void DualModeController::SendCommandStatusSuccess( 84 uint16_t command_opcode) const { 85 SendCommandStatus(kSuccessStatus, command_opcode); 86} 87 88DualModeController::DualModeController() 89 : state_(kStandby), properties_(kControllerPropertiesFile) { 90 devices_ = {}; 91 92 vector<std::string> beacon = {"beacon", "be:ac:10:00:00:01", "1000"}; 93 TestChannelAdd(beacon); 94 95 vector<std::string> classic = {std::string("classic"), 96 std::string("c1:a5:51:c0:00:01")}; 97 TestChannelAdd(classic); 98 99 vector<std::string> keyboard = {std::string("keyboard"), 100 std::string("cc:1c:eb:0a:12:d1"), 101 std::string("500")}; 102 TestChannelAdd(keyboard); 103 104 le_scan_enable_ = 0; 105 le_connect_ = false; 106 107 loopback_mode_ = 0; 108 109#define SET_HANDLER(opcode, method) \ 110 active_hci_commands_[opcode] = [this](const vector<uint8_t>& param) { \ 111 method(param); \ 112 }; 113 SET_HANDLER(HCI_RESET, HciReset); 114 SET_HANDLER(HCI_READ_BUFFER_SIZE, HciReadBufferSize); 115 SET_HANDLER(HCI_HOST_BUFFER_SIZE, HciHostBufferSize); 116 SET_HANDLER(HCI_READ_LOCAL_VERSION_INFO, HciReadLocalVersionInformation); 117 SET_HANDLER(HCI_READ_BD_ADDR, HciReadBdAddr); 118 SET_HANDLER(HCI_READ_LOCAL_SUPPORTED_CMDS, HciReadLocalSupportedCommands); 119 SET_HANDLER(HCI_READ_LOCAL_SUPPORTED_CODECS, HciReadLocalSupportedCodecs); 120 SET_HANDLER(HCI_READ_LOCAL_EXT_FEATURES, HciReadLocalExtendedFeatures); 121 SET_HANDLER(HCI_WRITE_SIMPLE_PAIRING_MODE, HciWriteSimplePairingMode); 122 SET_HANDLER(HCI_WRITE_LE_HOST_SUPPORT, HciWriteLeHostSupport); 123 SET_HANDLER(HCI_SET_EVENT_MASK, HciSetEventMask); 124 SET_HANDLER(HCI_WRITE_INQUIRY_MODE, HciWriteInquiryMode); 125 SET_HANDLER(HCI_WRITE_PAGESCAN_TYPE, HciWritePageScanType); 126 SET_HANDLER(HCI_WRITE_INQSCAN_TYPE, HciWriteInquiryScanType); 127 SET_HANDLER(HCI_WRITE_CLASS_OF_DEVICE, HciWriteClassOfDevice); 128 SET_HANDLER(HCI_WRITE_PAGE_TOUT, HciWritePageTimeout); 129 SET_HANDLER(HCI_WRITE_DEF_POLICY_SETTINGS, HciWriteDefaultLinkPolicySettings); 130 SET_HANDLER(HCI_READ_LOCAL_NAME, HciReadLocalName); 131 SET_HANDLER(HCI_CHANGE_LOCAL_NAME, HciWriteLocalName); 132 SET_HANDLER(HCI_WRITE_EXT_INQ_RESPONSE, HciWriteExtendedInquiryResponse); 133 SET_HANDLER(HCI_WRITE_VOICE_SETTINGS, HciWriteVoiceSetting); 134 SET_HANDLER(HCI_WRITE_CURRENT_IAC_LAP, HciWriteCurrentIacLap); 135 SET_HANDLER(HCI_WRITE_INQUIRYSCAN_CFG, HciWriteInquiryScanActivity); 136 SET_HANDLER(HCI_WRITE_SCAN_ENABLE, HciWriteScanEnable); 137 SET_HANDLER(HCI_SET_EVENT_FILTER, HciSetEventFilter); 138 SET_HANDLER(HCI_INQUIRY, HciInquiry); 139 SET_HANDLER(HCI_INQUIRY_CANCEL, HciInquiryCancel); 140 SET_HANDLER(HCI_DELETE_STORED_LINK_KEY, HciDeleteStoredLinkKey); 141 SET_HANDLER(HCI_RMT_NAME_REQUEST, HciRemoteNameRequest); 142 SET_HANDLER(HCI_BLE_SET_EVENT_MASK, HciLeSetEventMask); 143 SET_HANDLER(HCI_BLE_READ_BUFFER_SIZE, HciLeReadBufferSize); 144 SET_HANDLER(HCI_BLE_READ_LOCAL_SPT_FEAT, HciLeReadLocalSupportedFeatures); 145 SET_HANDLER(HCI_BLE_WRITE_RANDOM_ADDR, HciLeSetRandomAddress); 146 SET_HANDLER(HCI_BLE_WRITE_ADV_DATA, HciLeSetAdvertisingData); 147 SET_HANDLER(HCI_BLE_WRITE_ADV_PARAMS, HciLeSetAdvertisingParameters); 148 SET_HANDLER(HCI_BLE_WRITE_SCAN_PARAMS, HciLeSetScanParameters); 149 SET_HANDLER(HCI_BLE_WRITE_SCAN_ENABLE, HciLeSetScanEnable); 150 SET_HANDLER(HCI_BLE_CREATE_LL_CONN, HciLeCreateConnection); 151 SET_HANDLER(HCI_BLE_CREATE_CONN_CANCEL, HciLeConnectionCancel); 152 SET_HANDLER(HCI_BLE_READ_WHITE_LIST_SIZE, HciLeReadWhiteListSize); 153 SET_HANDLER(HCI_BLE_RAND, HciLeRand); 154 SET_HANDLER(HCI_BLE_READ_SUPPORTED_STATES, HciLeReadSupportedStates); 155 SET_HANDLER((HCI_GRP_VENDOR_SPECIFIC | 0x27), HciBleVendorSleepMode); 156 SET_HANDLER(HCI_BLE_VENDOR_CAP_OCF, HciBleVendorCap); 157 SET_HANDLER(HCI_BLE_MULTI_ADV_OCF, HciBleVendorMultiAdv); 158 SET_HANDLER((HCI_GRP_VENDOR_SPECIFIC | 0x155), HciBleVendor155); 159 SET_HANDLER(HCI_BLE_ADV_FILTER_OCF, HciBleAdvertisingFilter); 160 SET_HANDLER(HCI_BLE_ENERGY_INFO_OCF, HciBleEnergyInfo); 161 SET_HANDLER(HCI_BLE_EXTENDED_SCAN_PARAMS_OCF, HciBleExtendedScanParams); 162 // Testing Commands 163 SET_HANDLER(HCI_READ_LOOPBACK_MODE, HciReadLoopbackMode); 164 SET_HANDLER(HCI_WRITE_LOOPBACK_MODE, HciWriteLoopbackMode); 165#undef SET_HANDLER 166 167#define SET_TEST_HANDLER(command_name, method) \ 168 active_test_channel_commands_[command_name] = \ 169 [this](const vector<std::string>& param) { method(param); }; 170 SET_TEST_HANDLER("add", TestChannelAdd); 171 SET_TEST_HANDLER("del", TestChannelDel); 172 SET_TEST_HANDLER("list", TestChannelList); 173#undef SET_TEST_HANDLER 174} 175 176void DualModeController::RegisterTaskScheduler( 177 std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> 178 oneshotScheduler) { 179 schedule_task_ = oneshotScheduler; 180} 181 182void DualModeController::RegisterPeriodicTaskScheduler( 183 std::function<AsyncTaskId(std::chrono::milliseconds, 184 std::chrono::milliseconds, const TaskCallback&)> 185 periodicScheduler) { 186 schedule_periodic_task_ = periodicScheduler; 187} 188 189void DualModeController::RegisterTaskCancel( 190 std::function<void(AsyncTaskId)> task_cancel) { 191 cancel_task_ = task_cancel; 192} 193 194void DualModeController::HandleTestChannelCommand( 195 const std::string& name, const vector<std::string>& args) { 196 if (active_test_channel_commands_.count(name) == 0) return; 197 active_test_channel_commands_[name](args); 198} 199 200void DualModeController::HandleAcl(std::unique_ptr<AclPacket> acl_packet) { 201 if (loopback_mode_ == HCI_LOOPBACK_MODE_LOCAL) { 202 uint16_t channel = acl_packet->GetChannel(); 203 send_acl_(std::move(acl_packet)); 204 send_event_(EventPacket::CreateNumberOfCompletedPacketsEvent(channel, 1)); 205 return; 206 } 207} 208 209void DualModeController::HandleSco(std::unique_ptr<ScoPacket> sco_packet) { 210 if (loopback_mode_ == HCI_LOOPBACK_MODE_LOCAL) { 211 uint16_t channel = sco_packet->GetChannel(); 212 send_sco_(std::move(sco_packet)); 213 send_event_(EventPacket::CreateNumberOfCompletedPacketsEvent(channel, 1)); 214 return; 215 } 216} 217 218void DualModeController::HandleCommand( 219 std::unique_ptr<CommandPacket> command_packet) { 220 uint16_t opcode = command_packet->GetOpcode(); 221 LOG_INFO(LOG_TAG, "Command opcode: 0x%04X, OGF: 0x%04X, OCF: 0x%04X", opcode, 222 command_packet->GetOGF(), command_packet->GetOCF()); 223 224 if (loopback_mode_ == HCI_LOOPBACK_MODE_LOCAL && 225 // Loopback exceptions. 226 opcode != HCI_RESET && opcode != HCI_SET_HC_TO_HOST_FLOW_CTRL && 227 opcode != HCI_HOST_BUFFER_SIZE && opcode != HCI_HOST_NUM_PACKETS_DONE && 228 opcode != HCI_READ_BUFFER_SIZE && opcode != HCI_READ_LOOPBACK_MODE && 229 opcode != HCI_WRITE_LOOPBACK_MODE) { 230 send_event_(EventPacket::CreateLoopbackCommandEvent( 231 opcode, command_packet->GetPayload())); 232 } else if (active_hci_commands_.count(opcode) > 0) { 233 active_hci_commands_[opcode](command_packet->GetPayload()); 234 } else { 235 SendCommandCompleteOnlyStatus(opcode, kUnknownHciCommand); 236 } 237} 238 239void DualModeController::RegisterEventChannel( 240 const std::function<void(std::unique_ptr<EventPacket>)>& callback) { 241 send_event_ = callback; 242} 243 244void DualModeController::RegisterAclChannel( 245 const std::function<void(std::unique_ptr<AclPacket>)>& callback) { 246 send_acl_ = callback; 247} 248 249void DualModeController::RegisterScoChannel( 250 const std::function<void(std::unique_ptr<ScoPacket>)>& callback) { 251 send_sco_ = callback; 252} 253 254void DualModeController::HandleTimerTick() { 255 if (state_ == kInquiry) PageScan(); 256 if (le_scan_enable_ || le_connect_) LeScan(); 257 Connections(); 258 for (size_t dev = 0; dev < devices_.size(); dev++) devices_[dev]->TimerTick(); 259} 260 261void DualModeController::SetTimerPeriod(std::chrono::milliseconds new_period) { 262 timer_period_ = new_period; 263 264 if (timer_tick_task_ == kInvalidTaskId) return; 265 266 // Restart the timer with the new period 267 StopTimer(); 268 StartTimer(); 269} 270 271static uint8_t GetRssi(size_t dev) { 272 // TODO: Model rssi somehow 273 return -((dev * 16) % 127); 274} 275 276static uint8_t LeGetHandle() { 277 static int handle = 0; 278 return handle++; 279} 280 281static uint8_t LeGetConnInterval() { return 1; } 282 283static uint8_t LeGetConnLatency() { return 2; } 284 285static uint8_t LeGetSupervisionTimeout() { return 3; } 286 287void DualModeController::Connections() { 288 for (size_t i = 0; i < connections_.size(); i++) { 289 if (connections_[i]->Connected()) { 290 connections_[i]->SendToDevice(); 291 vector<uint8_t> data; 292 connections_[i]->ReceiveFromDevice(data); 293 // HandleConnectionData(data); 294 } 295 } 296} 297 298void DualModeController::LeScan() { 299 std::unique_ptr<EventPacket> le_adverts = 300 EventPacket::CreateLeAdvertisingReportEvent(); 301 vector<uint8_t> ad; 302 for (size_t dev = 0; dev < devices_.size(); dev++) { 303 uint8_t adv_type; 304 const BtAddress addr = devices_[dev]->GetBtAddress(); 305 uint8_t addr_type = devices_[dev]->GetAddressType(); 306 ad.clear(); 307 308 // Listen for Advertisements 309 if (devices_[dev]->IsAdvertisementAvailable( 310 std::chrono::milliseconds(le_scan_window_))) { 311 ad = devices_[dev]->GetAdvertisement(); 312 adv_type = devices_[dev]->GetAdvertisementType(); 313 if (le_scan_enable_ && !le_adverts->AddLeAdvertisingReport( 314 adv_type, addr_type, addr, ad, GetRssi(dev))) { 315 send_event_(std::move(le_adverts)); 316 le_adverts = EventPacket::CreateLeAdvertisingReportEvent(); 317 CHECK(le_adverts->AddLeAdvertisingReport(adv_type, addr_type, addr, ad, 318 GetRssi(dev))); 319 } 320 321 // Connect 322 if (le_connect_ && (adv_type == BTM_BLE_CONNECT_EVT || 323 adv_type == BTM_BLE_CONNECT_DIR_EVT)) { 324 LOG_INFO(LOG_TAG, "Connecting to device %d", static_cast<int>(dev)); 325 if (peer_address_ == addr && peer_address_type_ == addr_type && 326 devices_[dev]->LeConnect()) { 327 uint16_t handle = LeGetHandle(); 328 std::unique_ptr<EventPacket> event = 329 EventPacket::CreateLeConnectionCompleteEvent( 330 kSuccessStatus, handle, HCI_ROLE_MASTER, addr_type, addr, 331 LeGetConnInterval(), LeGetConnLatency(), 332 LeGetSupervisionTimeout()); 333 send_event_(std::move(event)); 334 le_connect_ = false; 335 336 connections_.push_back( 337 std::make_shared<Connection>(devices_[dev], handle)); 338 } 339 340 // TODO: Handle the white list (if (InWhiteList(dev))) 341 } 342 343 // Active scanning 344 if (le_scan_enable_ && le_scan_type_ == 1) { 345 ad.clear(); 346 if (devices_[dev]->HasScanResponse()) { 347 ad = devices_[dev]->GetScanResponse(); 348 if (!le_adverts->AddLeAdvertisingReport( 349 BTM_BLE_SCAN_RSP_EVT, addr_type, addr, ad, GetRssi(dev))) { 350 send_event_(std::move(le_adverts)); 351 le_adverts = EventPacket::CreateLeAdvertisingReportEvent(); 352 CHECK(le_adverts->AddLeAdvertisingReport( 353 BTM_BLE_SCAN_RSP_EVT, addr_type, addr, ad, GetRssi(dev))); 354 } 355 } 356 } 357 } 358 } 359 360 if (le_scan_enable_) send_event_(std::move(le_adverts)); 361} 362 363void DualModeController::PageScan() { 364 // Inquiry modes for specifiying inquiry result formats. 365 static const uint8_t kStandardInquiry = 0x00; 366 static const uint8_t kRssiInquiry = 0x01; 367 static const uint8_t kExtendedOrRssiInquiry = 0x02; 368 369 switch (inquiry_mode_) { 370 case (kStandardInquiry): { 371 std::unique_ptr<EventPacket> inquiry_result = 372 EventPacket::CreateInquiryResultEvent(); 373 for (size_t dev = 0; dev < devices_.size(); dev++) 374 // Scan for devices 375 if (devices_[dev]->IsPageScanAvailable()) { 376 bool result_added = inquiry_result->AddInquiryResult( 377 devices_[dev]->GetBtAddress(), 378 devices_[dev]->GetPageScanRepetitionMode(), 379 devices_[dev]->GetDeviceClass(), devices_[dev]->GetClockOffset()); 380 if (!result_added) { 381 send_event_(std::move(inquiry_result)); 382 inquiry_result = EventPacket::CreateInquiryResultEvent(); 383 result_added = inquiry_result->AddInquiryResult( 384 devices_[dev]->GetBtAddress(), 385 devices_[dev]->GetPageScanRepetitionMode(), 386 devices_[dev]->GetDeviceClass(), 387 devices_[dev]->GetClockOffset()); 388 CHECK(result_added); 389 } 390 } 391 } break; 392 393 case (kRssiInquiry): 394 LOG_INFO(LOG_TAG, "RSSI Inquiry Mode currently not supported."); 395 break; 396 397 case (kExtendedOrRssiInquiry): 398 for (size_t dev = 0; dev < devices_.size(); dev++) 399 if (devices_[dev]->IsPageScanAvailable()) { 400 send_event_(EventPacket::CreateExtendedInquiryResultEvent( 401 devices_[dev]->GetBtAddress(), 402 devices_[dev]->GetPageScanRepetitionMode(), 403 devices_[dev]->GetDeviceClass(), devices_[dev]->GetClockOffset(), 404 GetRssi(dev), devices_[dev]->GetExtendedInquiryData())); 405 } 406 break; 407 } 408} 409 410void DualModeController::StartTimer() { 411 LOG_ERROR(LOG_TAG, "StartTimer"); 412 timer_tick_task_ = schedule_periodic_task_( 413 std::chrono::milliseconds(0), timer_period_, 414 [this]() { DualModeController::HandleTimerTick(); }); 415} 416 417void DualModeController::StopTimer() { 418 LOG_ERROR(LOG_TAG, "StopTimer"); 419 cancel_task_(timer_tick_task_); 420 timer_tick_task_ = kInvalidTaskId; 421} 422 423void DualModeController::SetEventDelay(int64_t delay) { 424 if (delay < 0) delay = 0; 425} 426 427void DualModeController::TestChannelAdd(const vector<std::string>& args) { 428 LogCommand("TestChannel 'add'"); 429 430 std::shared_ptr<Device> new_dev = DeviceFactory::Create(args); 431 432 if (new_dev == NULL) { 433 LOG_ERROR(LOG_TAG, "TestChannel 'add' failed!"); 434 return; 435 } 436 437 devices_.push_back(new_dev); 438} 439 440void DualModeController::TestChannelDel(const vector<std::string>& args) { 441 LogCommand("TestChannel 'del'"); 442 443 size_t dev_index = std::stoi(args[0]); 444 445 if (dev_index >= devices_.size()) { 446 LOG_INFO(LOG_TAG, "TestChannel 'del': index %d out of range!", 447 static_cast<int>(dev_index)); 448 } else { 449 devices_.erase(devices_.begin() + dev_index); 450 } 451} 452 453void DualModeController::TestChannelList( 454 UNUSED_ATTR const vector<std::string>& args) const { 455 LogCommand("TestChannel 'list'"); 456 LOG_INFO(LOG_TAG, "Devices:"); 457 for (size_t dev = 0; dev < devices_.size(); dev++) { 458 LOG_INFO(LOG_TAG, "%d:", static_cast<int>(dev)); 459 devices_[dev]->ToString(); 460 } 461} 462 463void DualModeController::HciReset(const vector<uint8_t>& args) { 464 LogCommand("Reset"); 465 CHECK(args[0] == 0); // No arguments 466 state_ = kStandby; 467 if (timer_tick_task_ != kInvalidTaskId) { 468 LOG_INFO(LOG_TAG, "The timer was already running!"); 469 StopTimer(); 470 } 471 LOG_INFO(LOG_TAG, "Starting timer."); 472 StartTimer(); 473 474 SendCommandCompleteSuccess(HCI_RESET); 475} 476 477void DualModeController::HciReadBufferSize(const vector<uint8_t>& args) { 478 LogCommand("Read Buffer Size"); 479 CHECK(args[0] == 0); // No arguments 480 std::unique_ptr<EventPacket> command_complete = 481 EventPacket::CreateCommandCompleteReadBufferSize( 482 kSuccessStatus, properties_.GetAclDataPacketSize(), 483 properties_.GetSynchronousDataPacketSize(), 484 properties_.GetTotalNumAclDataPackets(), 485 properties_.GetTotalNumSynchronousDataPackets()); 486 487 send_event_(std::move(command_complete)); 488} 489 490void DualModeController::HciHostBufferSize(const vector<uint8_t>& args) { 491 LogCommand("Host Buffer Size"); 492 CHECK(args[0] == 7); // No arguments 493 SendCommandCompleteSuccess(HCI_HOST_BUFFER_SIZE); 494} 495 496void DualModeController::HciReadLocalVersionInformation( 497 const vector<uint8_t>& args) { 498 LogCommand("Read Local Version Information"); 499 CHECK(args[0] == 0); // No arguments 500 std::unique_ptr<EventPacket> command_complete = 501 EventPacket::CreateCommandCompleteReadLocalVersionInformation( 502 kSuccessStatus, properties_.GetVersion(), properties_.GetRevision(), 503 properties_.GetLmpPalVersion(), properties_.GetManufacturerName(), 504 properties_.GetLmpPalSubversion()); 505 send_event_(std::move(command_complete)); 506} 507 508void DualModeController::HciReadBdAddr(const vector<uint8_t>& args) { 509 CHECK(args[0] == 0); // No arguments 510 std::unique_ptr<EventPacket> command_complete = 511 EventPacket::CreateCommandCompleteReadBdAddr(kSuccessStatus, 512 properties_.GetAddress()); 513 send_event_(std::move(command_complete)); 514} 515 516void DualModeController::HciReadLocalSupportedCommands( 517 const vector<uint8_t>& args) { 518 LogCommand("Read Local Supported Commands"); 519 CHECK(args[0] == 0); // No arguments 520 std::unique_ptr<EventPacket> command_complete = 521 EventPacket::CreateCommandCompleteReadLocalSupportedCommands( 522 kSuccessStatus, properties_.GetLocalSupportedCommands()); 523 send_event_(std::move(command_complete)); 524} 525 526void DualModeController::HciReadLocalSupportedCodecs( 527 const vector<uint8_t>& args) { 528 LogCommand("Read Local Supported Codecs"); 529 CHECK(args[0] == 0); // No arguments 530 std::unique_ptr<EventPacket> command_complete = 531 EventPacket::CreateCommandCompleteReadLocalSupportedCodecs( 532 kSuccessStatus, properties_.GetSupportedCodecs(), 533 properties_.GetVendorSpecificCodecs()); 534 send_event_(std::move(command_complete)); 535} 536 537void DualModeController::HciReadLocalExtendedFeatures( 538 const vector<uint8_t>& args) { 539 LogCommand("Read Local Extended Features"); 540 CHECK(args.size() == 2); 541 std::unique_ptr<EventPacket> command_complete = 542 EventPacket::CreateCommandCompleteReadLocalExtendedFeatures( 543 kSuccessStatus, args[1], 544 properties_.GetLocalExtendedFeaturesMaximumPageNumber(), 545 properties_.GetLocalExtendedFeatures(args[1])); 546 send_event_(std::move(command_complete)); 547} 548 549void DualModeController::HciWriteSimplePairingMode( 550 UNUSED_ATTR const vector<uint8_t>& args) { 551 LogCommand("Write Simple Pairing Mode"); 552 SendCommandCompleteSuccess(HCI_WRITE_SIMPLE_PAIRING_MODE); 553} 554 555void DualModeController::HciWriteLeHostSupport( 556 UNUSED_ATTR const vector<uint8_t>& args) { 557 LogCommand("Write Le Host Support"); 558 SendCommandCompleteSuccess(HCI_WRITE_LE_HOST_SUPPORT); 559} 560 561void DualModeController::HciSetEventMask( 562 UNUSED_ATTR const vector<uint8_t>& args) { 563 LogCommand("Set Event Mask"); 564 SendCommandCompleteSuccess(HCI_SET_EVENT_MASK); 565} 566 567void DualModeController::HciWriteInquiryMode(const vector<uint8_t>& args) { 568 LogCommand("Write Inquiry Mode"); 569 CHECK(args.size() == 2); 570 inquiry_mode_ = args[1]; 571 SendCommandCompleteSuccess(HCI_WRITE_INQUIRY_MODE); 572} 573 574void DualModeController::HciWritePageScanType( 575 UNUSED_ATTR const vector<uint8_t>& args) { 576 LogCommand("Write Page Scan Type"); 577 SendCommandCompleteSuccess(HCI_WRITE_PAGESCAN_TYPE); 578} 579 580void DualModeController::HciWriteInquiryScanType( 581 UNUSED_ATTR const vector<uint8_t>& args) { 582 LogCommand("Write Inquiry Scan Type"); 583 SendCommandCompleteSuccess(HCI_WRITE_INQSCAN_TYPE); 584} 585 586void DualModeController::HciWriteClassOfDevice( 587 UNUSED_ATTR const vector<uint8_t>& args) { 588 LogCommand("Write Class Of Device"); 589 SendCommandCompleteSuccess(HCI_WRITE_CLASS_OF_DEVICE); 590} 591 592void DualModeController::HciWritePageTimeout( 593 UNUSED_ATTR const vector<uint8_t>& args) { 594 LogCommand("Write Page Timeout"); 595 SendCommandCompleteSuccess(HCI_WRITE_PAGE_TOUT); 596} 597 598void DualModeController::HciWriteDefaultLinkPolicySettings( 599 UNUSED_ATTR const vector<uint8_t>& args) { 600 LogCommand("Write Default Link Policy Settings"); 601 SendCommandCompleteSuccess(HCI_WRITE_DEF_POLICY_SETTINGS); 602} 603 604void DualModeController::HciReadLocalName( 605 UNUSED_ATTR const vector<uint8_t>& args) { 606 LogCommand("Get Local Name"); 607 std::unique_ptr<EventPacket> command_complete = 608 EventPacket::CreateCommandCompleteReadLocalName( 609 kSuccessStatus, properties_.GetLocalName()); 610 send_event_(std::move(command_complete)); 611} 612 613void DualModeController::HciWriteLocalName( 614 UNUSED_ATTR const vector<uint8_t>& args) { 615 LogCommand("Write Local Name"); 616 SendCommandCompleteSuccess(HCI_CHANGE_LOCAL_NAME); 617} 618 619void DualModeController::HciWriteExtendedInquiryResponse( 620 UNUSED_ATTR const vector<uint8_t>& args) { 621 LogCommand("Write Extended Inquiry Response"); 622 SendCommandCompleteSuccess(HCI_WRITE_EXT_INQ_RESPONSE); 623} 624 625void DualModeController::HciWriteVoiceSetting( 626 UNUSED_ATTR const vector<uint8_t>& args) { 627 LogCommand("Write Voice Setting"); 628 SendCommandCompleteSuccess(HCI_WRITE_VOICE_SETTINGS); 629} 630 631void DualModeController::HciWriteCurrentIacLap( 632 UNUSED_ATTR const vector<uint8_t>& args) { 633 LogCommand("Write Current IAC LAP"); 634 SendCommandCompleteSuccess(HCI_WRITE_CURRENT_IAC_LAP); 635} 636 637void DualModeController::HciWriteInquiryScanActivity( 638 UNUSED_ATTR const vector<uint8_t>& args) { 639 LogCommand("Write Inquiry Scan Activity"); 640 SendCommandCompleteSuccess(HCI_WRITE_INQUIRYSCAN_CFG); 641} 642 643void DualModeController::HciWriteScanEnable( 644 UNUSED_ATTR const vector<uint8_t>& args) { 645 LogCommand("Write Scan Enable"); 646 SendCommandCompleteSuccess(HCI_WRITE_SCAN_ENABLE); 647} 648 649void DualModeController::HciSetEventFilter( 650 UNUSED_ATTR const vector<uint8_t>& args) { 651 LogCommand("Set Event Filter"); 652 SendCommandCompleteSuccess(HCI_SET_EVENT_FILTER); 653} 654 655void DualModeController::HciInquiry(const vector<uint8_t>& args) { 656 LogCommand("Inquiry"); 657 CHECK(args.size() == 6); 658 state_ = kInquiry; 659 SendCommandStatusSuccess(HCI_INQUIRY); 660 inquiry_lap_[0] = args[1]; 661 inquiry_lap_[1] = args[2]; 662 inquiry_lap_[2] = args[3]; 663 664 AddControllerEvent(std::chrono::milliseconds(args[4] * 1280), 665 [this]() { DualModeController::InquiryTimeout(); }); 666 667 if (args[5] > 0) { 668 inquiry_responses_limited_ = true; 669 inquiry_num_responses_ = args[5]; 670 } 671} 672 673void DualModeController::HciInquiryCancel( 674 UNUSED_ATTR const vector<uint8_t>& args) { 675 LogCommand("Inquiry Cancel"); 676 CHECK(state_ == kInquiry); 677 state_ = kStandby; 678 SendCommandCompleteSuccess(HCI_INQUIRY_CANCEL); 679} 680 681void DualModeController::InquiryTimeout() { 682 LOG_INFO(LOG_TAG, "InquiryTimer fired"); 683 if (state_ == kInquiry) { 684 state_ = kStandby; 685 inquiry_responses_limited_ = false; 686 send_event_(EventPacket::CreateInquiryCompleteEvent(kSuccessStatus)); 687 } 688} 689 690void DualModeController::HciDeleteStoredLinkKey( 691 UNUSED_ATTR const vector<uint8_t>& args) { 692 LogCommand("Delete Stored Link Key"); 693 /* Check the last octect in |args|. If it is 0, delete only the link key for 694 * the given BD_ADDR. If is is 1, delete all stored link keys. */ 695 uint16_t deleted_keys = 1; 696 697 send_event_(EventPacket::CreateCommandCompleteDeleteStoredLinkKey( 698 kSuccessStatus, deleted_keys)); 699} 700 701void DualModeController::HciRemoteNameRequest( 702 UNUSED_ATTR const vector<uint8_t>& args) { 703 LogCommand("Remote Name Request"); 704 SendCommandStatusSuccess(HCI_RMT_NAME_REQUEST); 705} 706 707void DualModeController::HciLeSetEventMask(const vector<uint8_t>& args) { 708 LogCommand("LE SetEventMask"); 709 le_event_mask_ = args; 710 SendCommandCompleteSuccess(HCI_BLE_SET_EVENT_MASK); 711} 712 713void DualModeController::HciLeReadBufferSize( 714 UNUSED_ATTR const vector<uint8_t>& args) { 715 std::unique_ptr<EventPacket> command_complete = 716 EventPacket::CreateCommandCompleteLeReadBufferSize( 717 kSuccessStatus, properties_.GetLeDataPacketLength(), 718 properties_.GetTotalNumLeDataPackets()); 719 send_event_(std::move(command_complete)); 720} 721 722void DualModeController::HciLeReadLocalSupportedFeatures( 723 UNUSED_ATTR const vector<uint8_t>& args) { 724 std::unique_ptr<EventPacket> command_complete = 725 EventPacket::CreateCommandCompleteLeReadLocalSupportedFeatures( 726 kSuccessStatus, properties_.GetLeLocalSupportedFeatures()); 727 send_event_(std::move(command_complete)); 728} 729 730void DualModeController::HciLeSetRandomAddress(const vector<uint8_t>& args) { 731 LogCommand("LE SetRandomAddress"); 732 CHECK(args.size() == 7); 733 vector<uint8_t> new_addr = {args[1], args[2], args[3], 734 args[4], args[5], args[6]}; 735 CHECK(le_random_address_.FromVector(new_addr)); 736 SendCommandCompleteSuccess(HCI_BLE_WRITE_RANDOM_ADDR); 737} 738 739void DualModeController::HciLeSetAdvertisingParameters( 740 UNUSED_ATTR const vector<uint8_t>& args) { 741 LogCommand("LE SetAdvertisingParameters"); 742 SendCommandCompleteSuccess(HCI_BLE_WRITE_ADV_PARAMS); 743} 744 745void DualModeController::HciLeSetAdvertisingData( 746 UNUSED_ATTR const vector<uint8_t>& args) { 747 LogCommand("LE SetAdvertisingData"); 748 SendCommandCompleteSuccess(HCI_BLE_WRITE_ADV_DATA); 749} 750 751void DualModeController::HciLeSetScanParameters(const vector<uint8_t>& args) { 752 LogCommand("LE SetScanParameters"); 753 CHECK(args.size() == 8); 754 le_scan_type_ = args[1]; 755 le_scan_interval_ = args[2] | (args[3] << 8); 756 le_scan_window_ = args[4] | (args[5] << 8); 757 own_address_type_ = args[6]; 758 scanning_filter_policy_ = args[7]; 759 SendCommandCompleteSuccess(HCI_BLE_WRITE_SCAN_PARAMS); 760} 761 762void DualModeController::HciLeSetScanEnable(const vector<uint8_t>& args) { 763 LogCommand("LE SetScanEnable"); 764 CHECK(args.size() == 3); 765 le_scan_enable_ = args[1]; 766 filter_duplicates_ = args[2]; 767 SendCommandCompleteSuccess(HCI_BLE_WRITE_SCAN_ENABLE); 768} 769 770void DualModeController::HciLeCreateConnection(const vector<uint8_t>& args) { 771 LogCommand("LE CreateConnection"); 772 le_connect_ = true; 773 le_scan_interval_ = args[1] | (args[2] << 8); 774 le_scan_window_ = args[3] | (args[4] << 8); 775 initiator_filter_policy_ = args[5]; 776 777 if (initiator_filter_policy_ == 0) { // White list not used 778 peer_address_type_ = args[6]; 779 vector<uint8_t> peer_addr = {args[7], args[8], args[9], 780 args[10], args[11], args[12]}; 781 peer_address_.FromVector(peer_addr); 782 } 783 784 SendCommandStatusSuccess(HCI_BLE_CREATE_LL_CONN); 785} 786 787void DualModeController::HciLeConnectionCancel(const vector<uint8_t>& args) { 788 LogCommand("LE ConnectionCancel"); 789 CHECK(args[0] == 0); // No arguments 790 le_connect_ = false; 791 SendCommandStatusSuccess(HCI_BLE_CREATE_CONN_CANCEL); 792} 793 794void DualModeController::HciLeReadWhiteListSize(const vector<uint8_t>& args) { 795 LogCommand("LE ReadWhiteListSize"); 796 CHECK(args[0] == 0); // No arguments 797 std::unique_ptr<EventPacket> command_complete = 798 EventPacket::CreateCommandCompleteLeReadWhiteListSize( 799 kSuccessStatus, properties_.GetLeWhiteListSize()); 800 send_event_(std::move(command_complete)); 801} 802 803void DualModeController::HciLeReadRemoteUsedFeaturesB(uint16_t handle) { 804 uint64_t features; 805 LogCommand("LE ReadRemoteUsedFeatures Bottom half"); 806 807 for (size_t i = 0; i < connections_.size(); i++) 808 if (*connections_[i] == handle) 809 // TODO: 810 // features = connections_[i]->GetDevice()->GetUsedFeatures(); 811 features = 0xffffffffffffffff; 812 813 std::unique_ptr<EventPacket> event = 814 EventPacket::CreateLeRemoteUsedFeaturesEvent(kSuccessStatus, handle, 815 features); 816 send_event_(std::move(event)); 817} 818 819void DualModeController::HciLeReadRemoteUsedFeatures( 820 const vector<uint8_t>& args) { 821 LogCommand("LE ReadRemoteUsedFeatures"); 822 CHECK(args.size() == 3); 823 824 uint16_t handle = args[1] | (args[2] << 8); 825 826 AddConnectionAction( 827 [this, handle]() { 828 DualModeController::HciLeReadRemoteUsedFeaturesB(handle); 829 }, 830 handle); 831 832 SendCommandStatusSuccess(HCI_BLE_READ_REMOTE_FEAT); 833} 834 835void DualModeController::HciLeRand(UNUSED_ATTR const vector<uint8_t>& args) { 836 uint64_t random_val = rand(); 837 std::unique_ptr<EventPacket> command_complete = 838 EventPacket::CreateCommandCompleteLeRand(kSuccessStatus, random_val); 839 send_event_(std::move(command_complete)); 840} 841 842void DualModeController::HciLeReadSupportedStates( 843 UNUSED_ATTR const vector<uint8_t>& args) { 844 std::unique_ptr<EventPacket> command_complete = 845 EventPacket::CreateCommandCompleteLeReadSupportedStates( 846 kSuccessStatus, properties_.GetLeSupportedStates()); 847 send_event_(std::move(command_complete)); 848} 849 850void DualModeController::HciBleVendorSleepMode( 851 UNUSED_ATTR const vector<uint8_t>& args) { 852 SendCommandCompleteOnlyStatus(HCI_GRP_VENDOR_SPECIFIC | 0x27, 853 kUnknownHciCommand); 854} 855 856void DualModeController::HciBleVendorCap( 857 UNUSED_ATTR const vector<uint8_t>& args) { 858 vector<uint8_t> caps = properties_.GetLeVendorCap(); 859 if (caps.size() == 0) { 860 SendCommandCompleteOnlyStatus(HCI_BLE_VENDOR_CAP_OCF, kUnknownHciCommand); 861 return; 862 } 863 864 std::unique_ptr<EventPacket> command_complete = 865 EventPacket::CreateCommandCompleteLeVendorCap( 866 kSuccessStatus, properties_.GetLeVendorCap()); 867 send_event_(std::move(command_complete)); 868} 869 870void DualModeController::HciBleVendorMultiAdv( 871 UNUSED_ATTR const vector<uint8_t>& args) { 872 SendCommandCompleteOnlyStatus(HCI_BLE_MULTI_ADV_OCF, kUnknownHciCommand); 873} 874 875void DualModeController::HciBleVendor155( 876 UNUSED_ATTR const vector<uint8_t>& args) { 877 SendCommandCompleteOnlyStatus(HCI_GRP_VENDOR_SPECIFIC | 0x155, 878 kUnknownHciCommand); 879} 880 881void DualModeController::HciBleAdvertisingFilter( 882 UNUSED_ATTR const vector<uint8_t>& args) { 883 SendCommandCompleteOnlyStatus(HCI_BLE_ADV_FILTER_OCF, kUnknownHciCommand); 884} 885 886void DualModeController::HciBleEnergyInfo( 887 UNUSED_ATTR const vector<uint8_t>& args) { 888 SendCommandCompleteOnlyStatus(HCI_BLE_ENERGY_INFO_OCF, kUnknownHciCommand); 889} 890 891void DualModeController::HciBleExtendedScanParams( 892 UNUSED_ATTR const vector<uint8_t>& args) { 893 SendCommandCompleteOnlyStatus(HCI_BLE_EXTENDED_SCAN_PARAMS_OCF, 894 kUnknownHciCommand); 895} 896 897void DualModeController::HciReadLoopbackMode(const vector<uint8_t>& args) { 898 CHECK(args[0] == 0); // No arguments 899 std::unique_ptr<EventPacket> command_complete = 900 EventPacket::CreateCommandCompleteReadLoopbackMode(kSuccessStatus, 901 loopback_mode_); 902 send_event_(std::move(command_complete)); 903} 904 905void DualModeController::HciWriteLoopbackMode(const vector<uint8_t>& args) { 906 CHECK(args[0] == 1); 907 loopback_mode_ = args[1]; 908 // ACL channel 909 uint16_t acl_handle = 0x123; 910 send_event_(EventPacket::CreateConnectionCompleteEvent( 911 kSuccessStatus, acl_handle, properties_.GetAddress(), HCI_LINK_TYPE_ACL, 912 false)); 913 // SCO channel 914 uint16_t sco_handle = 0x345; 915 send_event_(EventPacket::CreateConnectionCompleteEvent( 916 kSuccessStatus, sco_handle, properties_.GetAddress(), HCI_LINK_TYPE_SCO, 917 false)); 918 SendCommandCompleteSuccess(HCI_WRITE_LOOPBACK_MODE); 919} 920 921} // namespace test_vendor_lib 922