1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef A2DP_PROFILE_PEER_H 17 #define A2DP_PROFILE_PEER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <memory> 22 #include <string> 23 24 #include "a2dp_codec/include/a2dp_codec_wrapper.h" 25 #include "a2dp_codec/include/a2dp_codec_constant.h" 26 #include "a2dp_codec/include/a2dp_codec_factory.h" 27 #include "a2dp_def.h" 28 #include "a2dp_sdp.h" 29 #include "a2dp_state_machine.h" 30 #include "avdtp.h" 31 #include "base_def.h" 32 #include "btstack.h" 33 #include "interface_profile_a2dp_src.h" 34 #include "sdp.h" 35 #include "timer.h" 36 #include "../../../stack/platform/include/queue.h" 37 38 namespace OHOS { 39 namespace bluetooth { 40 /** 41 * @brief The stream information associated with the a2dp profile. 42 * 43 * @since 6.0 44 */ 45 class A2dpStream { 46 public: 47 /** 48 * @brief A constructor used to create an <b>A2dpStream</b> instance. 49 * 50 * @since 6.0 51 */ 52 A2dpStream(); 53 54 /** 55 * @brief A desconstructor used to destroy an <b>A2dpStream</b> instance. 56 * 57 * @since 6.0 58 */ 59 ~A2dpStream(); 60 61 /** 62 * @brief A function to set the local handle of stream. 63 * 64 * @param[in] The local handle of stream 65 * @since 6.0 66 */ 67 void SetHandle(uint16_t hdl); 68 69 /** 70 * @brief A function to get the local handle of stream. 71 * 72 * @return The local handle of stream 73 * @since 6.0 74 */ 75 uint16_t GetHandle() const; 76 77 /** 78 * @brief A function to get the peer capability of stream. 79 * 80 * @return The detail of peer capability 81 * @since 6.0 82 */ 83 AvdtSepConfig GetPeerSEPInformation() const; 84 85 /** 86 * @brief A function to save the peer capability of stream. 87 * 88 * @param[in] The peer capability of stream 89 * @since 6.0 90 */ 91 void SetPeerCapability(AvdtSepConfig cap); 92 93 /** 94 * @brief A function to save the local codec information of stream. 95 * 96 * @param[in] The local codec information of stream 97 * @since 6.0 98 */ 99 void SetCodecInfo(CodecInfo data); 100 101 /** 102 * @brief A function to get the local codec information of stream. 103 * 104 * @return The local codec information of stream 105 * @since 6.0 106 */ 107 CodecInfo GetCodecInfo() const; 108 109 /** 110 * @brief A function to save the peer seid of stream. 111 * 112 * @param[in] The peer seid of stream 113 * @since 6.0 114 */ 115 void SetAcpSeid(uint8_t seid); 116 117 /** 118 * @brief A function to get the peer seid of stream. 119 * 120 * @param[in] The peer seid of stream 121 * @since 6.0 122 */ 123 uint8_t GetAcpSeid() const; 124 125 /** 126 * @brief A function to get the peer capability. 127 * 128 * @since 6.0 129 */ 130 bool GetPeerCap() const; 131 132 /** 133 * @brief A function to get the peer capability. 134 * 135 * @since 6.0 136 */ 137 void SetPeerCap(bool value); 138 139 private: 140 uint16_t handle_ = 0; 141 uint8_t acpSeid_ = 0; 142 AvdtSepConfig peerCapability_ {}; 143 CodecInfo codecInfo_ {}; 144 bool peerCap_ = false; 145 }; 146 147 class A2dpCodecDecoderObserver : public A2dpDecoderObserver { 148 public: 149 ~A2dpCodecDecoderObserver() override = default; 150 void DataAvailable(uint8_t *buf, uint32_t size) override; 151 }; 152 153 /** 154 * @brief This class provides a set of methods to manager the peer device information 155 * 156 * @since 6.0 157 */ 158 class A2dpProfilePeer { 159 public: 160 /** 161 * @brief A constructor used to create an <b>A2dpProfilePeer</b> instance. 162 * 163 * @since 6.0 164 */ 165 A2dpProfilePeer(const BtAddr &addr, uint8_t localRole); 166 167 /** 168 * @brief A destructor used to delete an <b>A2dpProfilePeer</b> instance. 169 * 170 * @since 6.0 171 */ 172 ~A2dpProfilePeer(); 173 174 /** 175 * @brief A function used to get the information of peer SEP. 176 * 177 * @return The information of peer SEP 178 * @since 6.0 179 */ 180 AvdtSepInfo GetPeerSEPInformation() const; 181 182 /** 183 * @brief A function used to update the index of peer stream endpoint. 184 * sepIndex++ 185 * @since 6.0 186 */ 187 void UpdateSepIndex(); 188 189 /** 190 * @brief A function used to save the sep information of peer. 191 * 192 * @param[in] The information of peer SEP 193 * @since 6.0 194 */ 195 void SetPeerSepInfo(AvdtDiscover seps); 196 197 /** 198 * @brief A function used to save the capability information of peer and other information 199 * related into local. 200 * 201 * @param[in] intSeid The handle of initiator 202 * @param[in] acpSeid The handle of acceptor/peer 203 * @param[in] cap The capability of peer stream endpoint 204 * @param[in] role The role of local profile 205 * @return true Save and matched config success 206 * @return false Capability is invalid or configure is not matched 207 * @since 6.0 208 */ 209 bool SetPeerCapInfo(uint8_t intSeid, uint8_t acpSeid, AvdtSepConfig cap, uint8_t role); 210 211 /** 212 * @brief A function used to save the handle of stream for local profile. 213 * 214 * @param[in] The handle of stream for local profile 215 * @since 6.0 216 */ 217 void SetStreamHandle(uint16_t handle); 218 219 /** 220 * @brief A function used to save the seid of peer stream endpoint 221 * 222 * @param[in] The seid of peer stream endpoint 223 * @since 6.0 224 */ 225 void SetAcpSeid(uint8_t seid); 226 227 /** 228 * @brief A function used to save the seid of local stream endpoint 229 * 230 * @param[in] The seid of local stream endpoint 231 * @since 6.0 232 */ 233 void SetIntSeid(uint8_t seid); 234 235 /** 236 * @brief A function used to get the current seid of peer stream endpoint 237 * 238 * @return The current seid of peer stream endpoint 239 * @since 6.0 240 */ 241 uint8_t GetAcpSeid() const; 242 243 /** 244 * @brief A function used to get the current seid of local stream endpoint 245 * 246 * @return The current seid of local stream endpoint 247 * @since 6.0 248 */ 249 uint8_t GetIntSeid() const; 250 251 /** 252 * @brief A function used to get the number of peer stream endpoint 253 * 254 * @return The number of peer stream endpoint 255 * @since 6.0 256 */ 257 uint8_t GetPeerSepNum() const; 258 259 /** 260 * @brief A function used to get the handle of stream 261 * 262 * @return The handle of stream 263 * @since 6.0 264 */ 265 uint16_t GetStreamHandle() const; 266 267 /** 268 * @brief A function used to get the address of peer device 269 * 270 * @return The address of peer device 271 * @since 6.0 272 */ 273 BtAddr GetPeerAddress() const; 274 275 /** 276 * @brief A function used to get the capability of peer 277 * 278 * @return The the capability of peer 279 * @since 6.0 280 */ 281 AvdtSepConfig GetPeerCapabilityInfo() const; 282 283 /** 284 * @brief A function used to get state machine of profile 285 * 286 * @return The capability of peer 287 * @since 6.0 288 */ 289 A2dpStateMachine *GetStateMachine(); 290 291 /** 292 * @brief A function used to get the service information of peer device 293 * 294 * @param[in] The address of peer 295 * @param[in] The service attributes of peer 296 * @param[in] The number of services 297 * @param[in] The content of other information related with this class 298 * @since 6.0 299 */ 300 static void SDPServiceCallback( 301 const BtAddr *addr, const SdpService *serviceArray, const uint16_t serviceNum, void *context); 302 303 /** 304 * @brief A function used to set the services class of peer is matched or not 305 * 306 * @param[in] true The service class of peer is matched 307 * @param[in] false The service class of peer is not matched 308 * @since 6.0 309 */ 310 void SetSDPServiceCapability(bool value); 311 312 /** 313 * @brief A function used to get the services class of peer is matched or not 314 * 315 * @return true The service class of peer is matched 316 * @return false The service class of peer is not matched 317 * @since 6.0 318 */ 319 bool GetSDPServiceCapability() const; 320 321 /** 322 * @brief A function used to set the avdtp version of peer 323 * 324 * @param[in] true The avdtp version of peer is ver1.3.2 325 * @param[in] false The avdtp version of peer is not ver1.3.2 326 * @since 6.0 327 */ 328 void SetAvdtpVersion(bool value); 329 330 /** 331 * @brief A function used to get the avdtp version of peer 332 * 333 * @return true The avdtp version of peer is ver1.3.2 334 * @return false The avdtp version of peer is not ver1.3.2 335 * @since 6.0 336 */ 337 bool GetAvdtpVersion() const; 338 339 /** 340 * @brief A function used to set the a2dp version of peer 341 * 342 * @param[in] true The a2dp version of peer is ver1.3.2 343 * @param[in] false The a2dp version of peer is not ver1.3.2 344 * @since 6.0 345 */ 346 void SetA2dpVersion(bool value); 347 348 /** 349 * @brief A function used to get the a2dp version of peer 350 * 351 * @return true The a2dp version of peer is ver1.3.2 352 * @return false The a2dp version of peer is not ver1.3.2 353 * @since 6.0 354 */ 355 bool GetA2dpVersion() const; 356 357 /** 358 * @brief A function used to get the codec configure of peer 359 * 360 * @return The configure of codec peer 361 * @since 6.0 362 */ 363 A2dpCodecFactory *GetCodecConfigure() const; 364 365 /** 366 * @brief A function used to get the configure matched 367 * 368 * @param[in] role The role of local profile 369 * @return true Find the matched configure 370 * @return false Not fine the matched configure 371 * @since 6.0 372 */ 373 bool JudgeCapabilityMatched(uint8_t role); 374 375 /** 376 * @brief A function to stop timeout 377 * 378 * @since 6.0 379 */ 380 void StopSignalingTimer() const; 381 382 /** 383 * @brief A function to set timeout 384 * 385 * @param[in] ms The timer 386 * @param[in] isPeriodic TRUE: period; FALSE: once only 387 * @since 6.0 388 */ 389 void SetSignalingTimer(int ms, bool isPeriodic) const; 390 391 /** 392 * @brief A function to process timeout switched thread 393 * 394 * @param[in] The role of profile(AVDT_ROLE_SRC/AVDT_ROLE_SNK) 395 * @since 6.0 396 */ 397 void SignalingTimeoutCallback(uint8_t role) const; 398 399 /** 400 * @brief A function to process timeout this class 401 * 402 * @since 6.0 403 */ 404 void ProcessSignalingTimeoutCallback(); 405 406 /** 407 * @brief A function to save the command sent just 408 * 409 * @param[in] The command sent just 410 * @since 6.0 411 */ 412 void SetCurrentCmd(uint8_t cmd); 413 414 /** 415 * @brief A function to get the command sent just 416 * 417 * @return The command sent just 418 * @since 6.0 419 */ 420 uint8_t GetCurrentCmd() const; 421 422 /** 423 * @brief A function to process timeout this class 424 * 425 * @param[in] info: User configure information 426 * @since 6.0 427 */ 428 int SetUserCodecConfigure(const A2dpSrcCodecInfo &info); 429 430 /** 431 * @brief A function to enable/disable the active device 432 * 433 * @param[in] value: True is enable optional codec; False is disable optional codec. 434 * @since 6.0 435 */ 436 bool EnableOptionalCodec(bool value); 437 438 /** 439 * @brief Notify to encoder pcm data. 440 * @param[in] addr: The address of peer device 441 * @since 6.0 442 */ 443 void NotifyEncoder(); 444 445 /** 446 * @brief Notify to decode frame data. 447 * @since 6.0 448 */ 449 void NotifyDecoder(); 450 451 /** 452 * @brief Notify to update audio reconfigure. 453 * @since 6.0 454 */ 455 void NotifyAudioConfigChanged() const; 456 457 /** 458 * @brief Update the configure by user configure triggered. 459 * @since 6.0 460 */ 461 void UpdateConfigure(); 462 463 /** 464 * @brief Update the configure by user configure triggered. 465 * @since 6.0 466 */ 467 AvdtSepConfig GetReconfig() const; 468 469 /** 470 * @brief Get the restart status. 471 * @since 6.0 472 */ 473 bool GetRestart() const; 474 475 /** 476 * @brief Set the restart status. 477 * @since 6.0 478 */ 479 void SetRestart(bool value); 480 481 /** 482 * @brief Get the reconfig tag status. 483 * @since 6.0 484 */ 485 bool GetReconfigTag() const; 486 487 /** 488 * @brief Set the reconfig tag status. 489 * @since 6.0 490 */ 491 void SetReconfigTag(bool value); 492 493 /** 494 * @brief A function to get the codec status. 495 * 496 * @since 6.0 497 */ 498 A2dpSrcCodecStatus GetCodecStatus() const; 499 500 /** 501 * @brief A function to set the optional support config. 502 * 503 * @param A2DP_OPTIONAL_NOT_SUPPORT: Not support optional codec; A2DP_OPTIONAL_SUPPORT: support optional codec 504 * @since 6.0 505 */ 506 void SetOptionalCodecsSupportState(int state) const; 507 508 /** 509 * @brief A function to get the configure of active stream 510 * 511 * @param[in] The address of active device 512 * @return The configure of active stream 513 * @since 6.0 514 */ 515 const ConfigureStream &GetConfigure() const; 516 517 /** 518 * @brief A function to set the configure of active stream 519 * 520 * @param[in] The address of active device 521 * @since 6.0 522 */ 523 void SetConfigure(); 524 525 /** 526 * @brief A function to set the init role is source or not 527 * 528 * @param[in] The value of init is source or not 529 * @since 6.0 530 */ 531 void SetInitSide(bool value); 532 533 /** 534 * @brief A function to get the init role is source or not 535 * 536 * @return The value of init is source or not 537 * @since 6.0 538 */ 539 bool GetInitSide() const; 540 541 /** 542 * @brief A function to set indication status 543 * 544 * @param[in] The value of indication is disconnect or not 545 * @since 6.0 546 */ 547 void SetDisconnectIndication(bool value); 548 549 /** 550 * @brief A function to get indication status 551 * 552 * @return The value of indication is disconnect or not 553 * @since 6.0 554 */ 555 bool GetDisconnectIndication() const; 556 557 /** 558 * @brief A function to get the number of capability is got 559 * 560 * @return The result of capability is got complete 561 * @since 6.0 562 */ 563 bool GetPeerCapComplete(uint8_t role) const; 564 565 /** 566 * @brief A function to set the number of capability is got 567 * 568 * @return The number of capability is got 569 * @since 6.0 570 */ 571 void SetNumberOfPeerCap(bool value); 572 573 /** 574 * @brief A function to set peer mtu size 575 * 576 * @param[in] The value of mtu size 577 * @return - 578 * @since 6.0 579 */ 580 void UpdatePeerMtu(uint16_t mtu); 581 582 /** 583 * @brief A function to set peer edr 584 * 585 * @param[in] The value of edr 586 * @return - 587 * @since 6.0 588 */ 589 void UpdatePeerEdr(uint8_t edr); 590 591 bool SendPacket(const Packet *packet, size_t frames, uint32_t bytes, uint32_t pktTimeStamp) const; 592 593 private: 594 BT_DISALLOW_COPY_AND_ASSIGN(A2dpProfilePeer); 595 /** 596 * @brief Reconfigure 597 * @param[in] The information of reconfigure 598 * @since 6.0 599 */ 600 void Reconfigure(bool close); 601 602 /** 603 * @brief A function used to register the local stream endpoint to avdtp 604 * 605 * @param[in] addr The address of peer device 606 * @param[in] role The role of local profile 607 * @since 6.0 608 */ 609 void RegisterSEPConfigureInfo(const BtAddr &addr, uint8_t role); 610 611 /** 612 * @brief A function used to update the peer capability information 613 * 614 * @param[in] acpSeid The seid of peer device 615 * @param[in] cap The capability information 616 * @param[in] role The role of local profile 617 * @since 6.0 618 */ 619 void UpdatePeerCapabilityInfo(uint8_t acpSeid, AvdtSepConfig cap, uint8_t role); 620 621 /** 622 * @brief A function to find peer capability 623 * 624 * @param[in] The capability of peer device 625 * @param[out] The property of selectable capability 626 * @since 6.0 627 */ 628 static void FindPeerSelectableCapabilityMatched(A2dpSrcCodecInfo &selectableCap, AvdtSepConfig cap); 629 630 /** 631 * @brief A function to find capability matched 632 * 633 * @param[in] config The configure information 634 * @param[in] role The role of local profile 635 * @since 6.0 636 */ 637 bool FindCapabilityMatched(const A2dpCodecConfig &config, const uint8_t role); 638 639 /** 640 * @brief Change the information of user configure 641 * 642 * @param[in] THe information of user set 643 * @param[out] The configure information to set 644 * @since 6.0 645 */ 646 void ChangeUserConfigureInfo( 647 A2dpCodecCapability &userConfig, uint8_t *codecInfo, const A2dpSrcCodecInfo info); 648 649 /** 650 * @brief Select the information of codec 651 * 652 * @param[in] THe index of user set 653 * @param[out] userConfig The configure information to set 654 * @param[out] codecInfo The information of codec selected 655 * @since 6.0 656 */ 657 void SelectCodecInfo(A2dpCodecCapability &userConfig, uint8_t *codecInfo, A2dpCodecIndex index); 658 659 /** 660 * @brief A function used to parse the service information of peer 661 * 662 * @param[in] addr The address of peer device 663 * @param[in] serviceArray The service information of peer profile 664 * @param[in] serviceNum The number of service 665 * @param[in] role The role of the peer 666 * @since 6.0 667 */ 668 static void ParseSDPInformation( 669 const BtAddr &addr, const SdpService *serviceArray, uint16_t serviceNum, uint8_t role); 670 671 /** 672 * @brief A function to create codecindex list with priority 673 * 674 * @since 6.0 675 */ 676 void CreateDefaultCodecPriority(); 677 678 /** 679 * @brief Init 680 * @param[in] addr The address of peer device 681 * @param[in] localRole The role of peer is source or sink 682 * @since 6.0 683 */ 684 void A2dpProfilePeerInit(const BtAddr &addr, const uint8_t localRole); 685 686 bool avdtpVer132_ = false; 687 bool a2dpVer132_ = false; 688 bool isService_ = false; 689 bool restart_ = false; 690 uint8_t peerSepIndex_ = 0; 691 uint8_t selectbleStreamIndex_ = 0; 692 uint8_t localRole_ = 0; // SEP type of local profile 693 uint8_t acpId_ = 0; 694 uint8_t intId_ = 0; 695 uint8_t peerNumSeps_ = 0; 696 uint8_t currentCmd_ = EVT_SDP_DISC; 697 uint8_t sourceStreamNum_ = (A2DP_SOURCE_CODEC_INDEX_MAX - A2DP_SOURCE_CODEC_INDEX_SBC); 698 uint8_t sinkStreamNum_ = (A2DP_SINK_CODEC_INDEX_MAX - A2DP_SINK_CODEC_INDEX_MIN); 699 uint16_t streamHandle_ = 0; 700 BtAddr peerAddress_ {}; 701 A2dpStream streamCtrl_[A2DP_CODEC_INDEX_MAX - A2DP_SOURCE_CODEC_INDEX_SBC]; 702 A2dpCodecFactory *codecConfig_ = nullptr; 703 std::map<A2dpCodecIndex, A2dpCodecPriority> defaultCodecPriorities_ {}; 704 ConfigureStream configureStream_ = {}; 705 A2dpStateMachine stateMachine_ {}; 706 AvdtSepInfo peerSepInfo_[AVDT_NUM_SEPS] {}; 707 AvdtSepConfig codecReconfig_ {}; 708 std::unique_ptr<utility::Timer> signalingTimer_ = nullptr; 709 uint16_t mtu_ = 0; 710 bool isInitSide_ = false; 711 bool disconnectInd_ = false; 712 std::unique_ptr<A2dpDecoderObserver> decoderObserver_ = nullptr; 713 uint8_t capNumber_ = 0; 714 uint8_t edr_ = 0; 715 bool codecAACConfig = false; 716 bool reconfigTag_ = false; 717 }; 718 } // namespace bluetooth 719 } // namespace OHOS 720 #endif // A2DP_PROFILE_PEER_H