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 #pragma once 18 19 #include <unistd.h> 20 #include <cstdint> 21 #include <memory> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "base/time/time.h" 27 #include "hci/address.h" 28 #include "hci/hci_packets.h" 29 #include "link_layer_controller.h" 30 #include "model/devices/device.h" 31 #include "model/setup/async_manager.h" 32 #include "security_manager.h" 33 34 namespace test_vendor_lib { 35 36 using ::bluetooth::hci::Address; 37 using ::bluetooth::hci::CommandView; 38 39 // Emulates a dual mode BR/EDR + LE controller by maintaining the link layer 40 // state machine detailed in the Bluetooth Core Specification Version 4.2, 41 // Volume 6, Part B, Section 1.1 (page 30). Provides methods corresponding to 42 // commands sent by the HCI. These methods will be registered as callbacks from 43 // a controller instance with the HciHandler. To implement a new Bluetooth 44 // command, simply add the method declaration below, with return type void and a 45 // single const std::vector<uint8_t>& argument. After implementing the 46 // method, simply register it with the HciHandler using the SET_HANDLER macro in 47 // the controller's default constructor. Be sure to name your method after the 48 // corresponding Bluetooth command in the Core Specification with the prefix 49 // "Hci" to distinguish it as a controller command. 50 class DualModeController : public Device { 51 // The location of the config file loaded to populate controller attributes. 52 static constexpr char kControllerPropertiesFile[] = "/etc/bluetooth/controller_properties.json"; 53 static constexpr uint16_t kSecurityManagerNumKeys = 15; 54 55 public: 56 // Sets all of the methods to be used as callbacks in the HciHandler. 57 DualModeController(const std::string& properties_filename = std::string(kControllerPropertiesFile), 58 uint16_t num_keys = kSecurityManagerNumKeys); 59 60 ~DualModeController() = default; 61 62 // Device methods. 63 virtual void Initialize(const std::vector<std::string>& args) override; 64 65 virtual std::string GetTypeString() const override; 66 67 virtual void IncomingPacket( 68 model::packets::LinkLayerPacketView incoming) override; 69 70 virtual void TimerTick() override; 71 72 // Route commands and data from the stack. 73 void HandleAcl(std::shared_ptr<std::vector<uint8_t>> acl_packet); 74 void HandleCommand(std::shared_ptr<std::vector<uint8_t>> command_packet); 75 void HandleSco(std::shared_ptr<std::vector<uint8_t>> sco_packet); 76 void HandleIso(std::shared_ptr<std::vector<uint8_t>> iso_packet); 77 78 // Set the callbacks for scheduling tasks. 79 void RegisterTaskScheduler(std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> evtScheduler); 80 81 void RegisterPeriodicTaskScheduler( 82 std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)> 83 periodicEvtScheduler); 84 85 void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel); 86 87 // Set the callbacks for sending packets to the HCI. 88 void RegisterEventChannel( 89 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& 90 send_event); 91 92 void RegisterAclChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_acl); 93 94 void RegisterScoChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_sco); 95 96 void RegisterIsoChannel( 97 const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& 98 send_iso); 99 100 // Set the device's address. 101 void SetAddress(Address address) override; 102 103 // Controller commands. For error codes, see the Bluetooth Core Specification, 104 // Version 4.2, Volume 2, Part D (page 370). 105 106 // Link Control Commands 107 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1 108 109 // 7.1.1 110 void Inquiry(CommandView args); 111 112 // 7.1.2 113 void InquiryCancel(CommandView args); 114 115 // 7.1.5 116 void CreateConnection(CommandView args); 117 118 // 7.1.6 119 void Disconnect(CommandView args); 120 121 // 7.1.7 122 void CreateConnectionCancel(CommandView args); 123 124 // 7.1.8 125 void AcceptConnectionRequest(CommandView args); 126 127 // 7.1.9 128 void RejectConnectionRequest(CommandView args); 129 130 // 7.1.10 131 void LinkKeyRequestReply(CommandView args); 132 133 // 7.1.11 134 void LinkKeyRequestNegativeReply(CommandView args); 135 136 // 7.1.12 137 void PinCodeRequestReply(CommandView args); 138 139 // 7.1.13 140 void PinCodeRequestNegativeReply(CommandView args); 141 142 // 7.1.14 143 void ChangeConnectionPacketType(CommandView args); 144 145 // 7.1.15 146 void AuthenticationRequested(CommandView args); 147 148 // 7.1.16 149 void SetConnectionEncryption(CommandView args); 150 151 // 7.1.17 152 void ChangeConnectionLinkKey(CommandView args); 153 154 // 7.1.18 155 void CentralLinkKey(CommandView args); 156 157 // 7.1.19 158 void RemoteNameRequest(CommandView args); 159 160 // 7.2.8 161 void SwitchRole(CommandView args); 162 163 // 7.1.21 164 void ReadRemoteSupportedFeatures(CommandView args); 165 166 // 7.1.22 167 void ReadRemoteExtendedFeatures(CommandView args); 168 169 // 7.1.23 170 void ReadRemoteVersionInformation(CommandView args); 171 172 // 7.1.24 173 void ReadClockOffset(CommandView args); 174 175 // 7.1.29 176 void IoCapabilityRequestReply(CommandView args); 177 178 // 7.1.30 179 void UserConfirmationRequestReply(CommandView args); 180 181 // 7.1.31 182 void UserConfirmationRequestNegativeReply(CommandView args); 183 184 // 7.1.32 185 void UserPasskeyRequestReply(CommandView args); 186 187 // 7.1.33 188 void UserPasskeyRequestNegativeReply(CommandView args); 189 190 // 7.1.34 191 void RemoteOobDataRequestReply(CommandView args); 192 193 // 7.1.35 194 void RemoteOobDataRequestNegativeReply(CommandView args); 195 196 // 7.1.36 197 void IoCapabilityRequestNegativeReply(CommandView args); 198 199 // 7.1.53 200 void RemoteOobExtendedDataRequestReply(CommandView args); 201 202 // Link Policy Commands 203 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2 204 205 // 7.2.1 206 void HoldMode(CommandView args); 207 208 // 7.2.2 209 void SniffMode(CommandView args); 210 211 // 7.2.3 212 void ExitSniffMode(CommandView args); 213 214 // 7.2.6 215 void QosSetup(CommandView args); 216 217 // 7.2.10 218 void WriteLinkPolicySettings(CommandView args); 219 220 // 7.2.11 221 void ReadDefaultLinkPolicySettings(CommandView args); 222 223 // 7.2.12 224 void WriteDefaultLinkPolicySettings(CommandView args); 225 226 // 7.2.13 227 void FlowSpecification(CommandView args); 228 229 // 7.2.14 230 void SniffSubrating(CommandView args); 231 232 // Link Controller Commands 233 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3 234 235 // 7.3.1 236 void SetEventMask(CommandView args); 237 238 // 7.3.2 239 void Reset(CommandView args); 240 241 // 7.3.3 242 void SetEventFilter(CommandView args); 243 244 // 7.3.10 245 void DeleteStoredLinkKey(CommandView args); 246 247 // 7.3.11 248 void WriteLocalName(CommandView args); 249 250 // 7.3.12 251 void ReadLocalName(CommandView args); 252 253 // 7.3.15 254 void ReadPageTimeout(CommandView args); 255 256 // 7.3.16 257 void WritePageTimeout(CommandView args); 258 259 // 7.3.17 260 void ReadScanEnable(CommandView args); 261 262 // 7.3.18 263 void WriteScanEnable(CommandView args); 264 265 // 7.3.19 266 void ReadPageScanActivity(CommandView args); 267 268 // 7.3.20 269 void WritePageScanActivity(CommandView args); 270 271 // 7.3.21 272 void ReadInquiryScanActivity(CommandView args); 273 274 // 7.3.22 275 void WriteInquiryScanActivity(CommandView args); 276 277 // 7.3.23 278 void ReadAuthenticationEnable(CommandView args); 279 280 // 7.3.24 281 void WriteAuthenticationEnable(CommandView args); 282 283 // 7.3.26 284 void WriteClassOfDevice(CommandView args); 285 286 // 7.3.28 287 void WriteVoiceSetting(CommandView args); 288 289 // 7.3.39 290 void HostBufferSize(CommandView args); 291 292 // 7.3.42 293 void WriteLinkSupervisionTimeout(CommandView args); 294 295 // 7.3.43 296 void ReadNumberOfSupportedIac(CommandView args); 297 298 // 7.3.44 299 void ReadCurrentIacLap(CommandView args); 300 301 // 7.3.45 302 void WriteCurrentIacLap(CommandView args); 303 304 // 7.3.47 305 void ReadInquiryScanType(CommandView args); 306 307 // 7.3.48 308 void WriteInquiryScanType(CommandView args); 309 310 // 7.3.49 311 void ReadInquiryMode(CommandView args); 312 313 // 7.3.50 314 void WriteInquiryMode(CommandView args); 315 316 // 7.3.52 317 void ReadPageScanType(CommandView args); 318 319 // 7.3.52 320 void WritePageScanType(CommandView args); 321 322 // 7.3.56 323 void WriteExtendedInquiryResponse(CommandView args); 324 325 // 7.3.57 326 void RefreshEncryptionKey(CommandView args); 327 328 // 7.3.59 329 void WriteSimplePairingMode(CommandView args); 330 331 // 7.3.60 332 void ReadLocalOobData(CommandView args); 333 334 // 7.3.61 335 void ReadInquiryResponseTransmitPowerLevel(CommandView args); 336 337 // 7.3.63 338 void SendKeypressNotification(CommandView args); 339 340 // 7.3.69 341 void SetEventMaskPage2(CommandView args); 342 343 // 7.3.79 344 void WriteLeHostSupport(CommandView args); 345 346 // 7.3.92 347 void WriteSecureConnectionsHostSupport(CommandView args); 348 349 // 7.3.95 350 void ReadLocalOobExtendedData(CommandView args); 351 352 // Informational Parameters Commands 353 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4 354 355 // 7.4.5 356 void ReadBufferSize(CommandView args); 357 358 // 7.4.1 359 void ReadLocalVersionInformation(CommandView args); 360 361 // 7.4.6 362 void ReadBdAddr(CommandView args); 363 364 // 7.4.2 365 void ReadLocalSupportedCommands(CommandView args); 366 367 // 7.4.3 368 void ReadLocalSupportedFeatures(CommandView args); 369 370 // 7.4.4 371 void ReadLocalExtendedFeatures(CommandView args); 372 373 // 7.4.8 374 void ReadLocalSupportedCodecs(CommandView args); 375 376 // Status Parameters Commands 377 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.5 378 379 // 7.5.7 380 void ReadEncryptionKeySize(CommandView args); 381 382 // Test Commands 383 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.7 384 385 // 7.7.1 386 void ReadLoopbackMode(CommandView args); 387 388 // 7.7.2 389 void WriteLoopbackMode(CommandView args); 390 391 // LE Controller Commands 392 // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8 393 394 // 7.8.1 395 void LeSetEventMask(CommandView args); 396 397 // 7.8.2 398 void LeReadBufferSize(CommandView args); 399 400 // 7.8.3 401 void LeReadLocalSupportedFeatures(CommandView args); 402 403 // 7.8.4 404 void LeSetRandomAddress(CommandView args); 405 406 // 7.8.5 407 void LeSetAdvertisingParameters(CommandView args); 408 409 // 7.8.6 410 void LeReadAdvertisingPhysicalChannelTxPower(CommandView args); 411 412 // 7.8.7 413 void LeSetAdvertisingData(CommandView args); 414 415 // 7.8.8 416 void LeSetScanResponseData(CommandView args); 417 418 // 7.8.9 419 void LeSetAdvertisingEnable(CommandView args); 420 421 // 7.8.10 422 void LeSetScanParameters(CommandView args); 423 424 // 7.8.11 425 void LeSetScanEnable(CommandView args); 426 427 // 7.8.12 428 void LeCreateConnection(CommandView args); 429 430 // 7.8.18 431 void LeConnectionUpdate(CommandView args); 432 433 // 7.8.13 434 void LeConnectionCancel(CommandView args); 435 436 // 7.8.14 437 void LeReadConnectListSize(CommandView args); 438 439 // 7.8.15 440 void LeClearConnectList(CommandView args); 441 442 // 7.8.16 443 void LeAddDeviceToConnectList(CommandView args); 444 445 // 7.8.17 446 void LeRemoveDeviceFromConnectList(CommandView args); 447 448 // 7.8.21 449 void LeReadRemoteFeatures(CommandView args); 450 451 // 7.8.23 452 void LeRand(CommandView args); 453 454 // 7.8.24 455 void LeStartEncryption(CommandView args); 456 457 // 7.8.25 458 void LeLongTermKeyRequestReply(CommandView args); 459 460 // 7.8.26 461 void LeLongTermKeyRequestNegativeReply(CommandView args); 462 463 // 7.8.27 464 void LeReadSupportedStates(CommandView args); 465 466 // 7.8.34 467 void LeReadSuggestedDefaultDataLength(CommandView args); 468 469 // 7.8.38 470 void LeAddDeviceToResolvingList(CommandView args); 471 472 // 7.8.39 473 void LeRemoveDeviceFromResolvingList(CommandView args); 474 475 // 7.8.40 476 void LeClearResolvingList(CommandView args); 477 478 // 7.8.41 479 void LeReadResolvingListSize(CommandView args); 480 481 // 7.8.44 482 void LeSetAddressResolutionEnable(CommandView args); 483 484 // 7.8.45 485 void LeSetResovalablePrivateAddressTimeout(CommandView args); 486 487 // 7.8.46 488 void LeReadMaximumDataLength(CommandView args); 489 490 // 7.8.52 491 void LeSetExtendedAdvertisingRandomAddress(CommandView args); 492 493 // 7.8.53 494 void LeSetExtendedAdvertisingParameters(CommandView args); 495 496 // 7.8.54 497 void LeSetExtendedAdvertisingData(CommandView args); 498 499 // 7.8.55 500 void LeSetExtendedAdvertisingScanResponse(CommandView args); 501 502 // 7.8.56 503 void LeSetExtendedAdvertisingEnable(CommandView args); 504 505 // 7.8.57 506 void LeReadMaximumAdvertisingDataLength(CommandView args); 507 508 // 7.8.58 509 void LeReadNumberOfSupportedAdvertisingSets(CommandView args); 510 511 // 7.8.59 512 void LeRemoveAdvertisingSet(CommandView args); 513 514 // 7.8.60 515 void LeClearAdvertisingSets(CommandView args); 516 517 // 7.8.64 518 void LeSetExtendedScanParameters(CommandView args); 519 520 // 7.8.65 521 void LeSetExtendedScanEnable(CommandView args); 522 523 // 7.8.66 524 void LeExtendedCreateConnection(CommandView args); 525 526 // 7.8.77 527 void LeSetPrivacyMode(CommandView args); 528 529 // 7.8.96 - 7.8.110 530 void LeReadIsoTxSync(CommandView packet_view); 531 void LeSetCigParameters(CommandView packet_view); 532 void LeCreateCis(CommandView packet_view); 533 void LeRemoveCig(CommandView packet_view); 534 void LeAcceptCisRequest(CommandView packet_view); 535 void LeRejectCisRequest(CommandView packet_view); 536 void LeCreateBig(CommandView packet_view); 537 void LeTerminateBig(CommandView packet_view); 538 void LeBigCreateSync(CommandView packet_view); 539 void LeBigTerminateSync(CommandView packet_view); 540 void LeRequestPeerSca(CommandView packet_view); 541 void LeSetupIsoDataPath(CommandView packet_view); 542 void LeRemoveIsoDataPath(CommandView packet_view); 543 544 // Vendor-specific Commands 545 546 void LeVendorSleepMode(CommandView args); 547 void LeVendorCap(CommandView args); 548 void LeVendorMultiAdv(CommandView args); 549 void LeVendor155(CommandView args); 550 void LeVendor157(CommandView args); 551 void LeEnergyInfo(CommandView args); 552 void LeAdvertisingFilter(CommandView args); 553 void LeExtendedScanParams(CommandView args); 554 555 // Required commands for handshaking with hci driver 556 void ReadClassOfDevice(CommandView args); 557 void ReadVoiceSetting(CommandView args); 558 void ReadConnectionAcceptTimeout(CommandView args); 559 void WriteConnectionAcceptTimeout(CommandView args); 560 561 void SetTimerPeriod(std::chrono::milliseconds new_period); 562 void StartTimer(); 563 void StopTimer(); 564 565 protected: 566 LinkLayerController link_layer_controller_{properties_}; 567 568 private: 569 // Set a timer for a future action 570 void AddControllerEvent(std::chrono::milliseconds, const TaskCallback& callback); 571 572 void AddConnectionAction(const TaskCallback& callback, uint16_t handle); 573 574 void SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const; 575 576 // Callbacks to send packets back to the HCI. 577 std::function<void(std::shared_ptr<bluetooth::hci::AclBuilder>)> send_acl_; 578 std::function<void(std::shared_ptr<bluetooth::hci::EventBuilder>)> 579 send_event_; 580 std::function<void(std::shared_ptr<bluetooth::hci::ScoBuilder>)> send_sco_; 581 std::function<void(std::shared_ptr<bluetooth::hci::IsoBuilder>)> send_iso_; 582 583 // Maintains the commands to be registered and used in the HciHandler object. 584 // Keys are command opcodes and values are the callbacks to handle each 585 // command. 586 std::unordered_map<bluetooth::hci::OpCode, 587 std::function<void(bluetooth::hci::CommandView)>> 588 active_hci_commands_; 589 590 bluetooth::hci::LoopbackMode loopback_mode_; 591 592 SecurityManager security_manager_; 593 594 DualModeController(const DualModeController& cmdPckt) = delete; 595 DualModeController& operator=(const DualModeController& cmdPckt) = delete; 596 }; 597 598 } // namespace test_vendor_lib 599