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 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions, 21 * including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc. 22 * 23 * @since 6 24 */ 25 26 /** 27 * @file hfp_ag_service.h 28 * 29 * @brief Declares HFP AG role service functions, including basic and observer functions. 30 * 31 * @since 6 32 */ 33 34 #ifndef HFP_AG_SERVICE_H 35 #define HFP_AG_SERVICE_H 36 37 #include <cstdint> 38 #include <map> 39 #include <memory> 40 #include <mutex> 41 42 #include "base_def.h" 43 #include "context.h" 44 #include "bluetooth_phone_state.h" 45 #include "hfp_ag_message.h" 46 #include "hfp_ag_statemachine.h" 47 #include "interface_profile_hfp_ag.h" 48 #include "message.h" 49 #include "raw_address.h" 50 #include "timer.h" 51 52 namespace OHOS { 53 namespace bluetooth { 54 /** 55 * @brief This HFP class provides a set of methods that is called by Framework API, and manage 56 * the state machine and the message queue. 57 * 58 * @since 6 59 */ 60 class HfpAgService : public IProfileHfpAg, public utility::Context { 61 public: 62 /** 63 * @brief Construct a new HfpAgService object. 64 * 65 * @since 6 66 */ 67 HfpAgService(); 68 69 /** 70 * @brief Destroy the HfpAgService object. 71 * 72 * @since 6 73 */ 74 ~HfpAgService(); 75 76 /** 77 * @brief Get the instance of the HfpAgService object. 78 * 79 * @return Returns the instance of the HfpAgService object. 80 * @since 6 81 */ 82 static HfpAgService *GetService(); 83 84 /** 85 * @brief Get the Context object. 86 * 87 * @return Returns the Context object. 88 * @since 6 89 */ 90 utility::Context *GetContext() override; 91 92 /** 93 * @brief Enable the target service. 94 * 95 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 96 * @since 6 97 */ 98 void Enable() override; 99 100 /** 101 * @brief Disable the target service. 102 * 103 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 104 * @since 6 105 */ 106 void Disable() override; 107 108 /** 109 * @brief Finish the shut down service. 110 * 111 * @since 6 112 */ 113 void ShutDownDone(bool isAllDisconnected); 114 115 /** 116 * @brief Establish the service level connection. 117 * 118 * @param device The remote device 119 * @return Returns the connect result code. 120 * @since 6 121 */ 122 int Connect(const RawAddress &device) override; 123 124 /** 125 * @brief Release the service level connection. 126 * 127 * @param device The remote device 128 * @return Returns the disconnect result code. 129 * @since 6 130 */ 131 int Disconnect(const RawAddress &device) override; 132 133 /** 134 * @brief Get the connected devices list. 135 * 136 * @return Returns the connected devices list. 137 * @since 6 138 */ 139 std::list<RawAddress> GetConnectDevices() override; 140 141 /** 142 * @brief Get the connection state. 143 * 144 * @return Returns the connection state. 145 * @since 6 146 */ 147 int GetConnectState() override; 148 149 /** 150 * @brief Get the state by the device. 151 * 152 * @param device The remote device. 153 * @return Returns the connection state. 154 * @since 6 155 */ 156 int GetDeviceState(const RawAddress &device) override; 157 158 /** 159 * @brief Get the maximum number of connected devices. 160 * 161 * @return Returns the max connected devices number. 162 * @since 6 163 */ 164 int GetMaxConnectNum() override; 165 166 /** 167 * @brief Get the devices list of the specified states. 168 * 169 * @param states The specified states. 170 * @return Returns the remote devices list of the specified states. 171 * @since 6 172 */ 173 std::vector<RawAddress> GetDevicesByStates(std::vector<int> states) override; 174 175 /** 176 * @brief Establish the audio connection. 177 * 178 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 179 * @since 6 180 */ 181 bool ConnectSco() override; 182 183 /** 184 * @brief Release the audio connection. 185 * 186 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 187 * @since 6 188 */ 189 bool DisconnectSco() override; 190 191 /** 192 * @brief Get the audio state of the specified device. 193 * 194 * @param device The remote device. 195 * @return Returns the audio state. 196 * @since 6 197 */ 198 int GetScoState(const RawAddress &device) override; 199 200 void UpdateMockCallList(int callState, const std::string &number, int type); 201 202 /** 203 * @brief This function used to Update changed phone call information. 204 * 205 * @param phoneState Bluetooth phone state. 206 * @since 6 207 */ 208 void PhoneStateChanged(Bluetooth::BluetoothPhoneState &phoneState) override; 209 210 /** 211 * @brief Send response for querying standard list current calls by remote Hf device. 212 * 213 * @param index Index of the current call. 214 * @param direction Direction of the current call. 215 * @param status Status of the current call. 216 * @param mode Source Mode of the current call. 217 * @param mpty Is this call a member of a conference call. 218 * @param number Phone call number. 219 * @param type Type of phone call number. 220 * @since 6 221 */ 222 void ClccResponse( 223 int index, int direction, int status, int mode, bool mpty, const std::string &number, int type) override; 224 225 /** 226 * @brief Open the voice recognition. 227 * 228 * @param device The Remote device. 229 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 230 * @since 6 231 */ 232 bool OpenVoiceRecognition(const RawAddress &device) override; 233 234 /** 235 * @brief Close the voice recognition. 236 * 237 * @param device The Remote device. 238 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 239 * @since 6 240 */ 241 bool CloseVoiceRecognition(const RawAddress &device) override; 242 243 /** 244 * @brief Set the active device for audio connection. 245 * 246 * @param device The remote device. 247 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 248 * @since 6 249 */ 250 bool SetActiveDevice(const RawAddress &device) override; 251 252 bool IntoMock(int state) override; 253 bool SendNoCarrier(const RawAddress &device) override; 254 int GetMockState(); 255 std::vector<MockCall> GetCallList(); 256 257 /** 258 * @brief Get the active device. 259 * 260 * @param device The remote active device. 261 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 262 * @since 6 263 */ 264 std::string GetActiveDevice() override; 265 266 /** 267 * @brief Notify the slc state change. 268 * 269 * @param device The remote device. 270 * @param toState The current state. 271 * @since 6 272 */ 273 void NotifySlcStateChanged(const RawAddress &device, int toState); 274 275 /** 276 * @brief Notify the audio state change. 277 * 278 * @param device The remote device. 279 * @param toState The current state. 280 * @since 6 281 */ 282 void NotifyAudioStateChanged(const RawAddress &device, int toState, int reason = 0); 283 284 /** 285 * @brief Notify the active device change. 286 * 287 * @param device The active device. 288 * @since 6 289 */ 290 void NotifyCurrentActiveDevice(const RawAddress &device); 291 292 /** 293 * @brief Notify the value of the HF enhanced driver safety. 294 * 295 * @param device The bluetooth device address. 296 * @param indValue The value of the HF enhanced driver safety. 297 * @since 6 298 */ 299 void NotifyHfEnhancedDriverSafety(const RawAddress &device, int indValue); 300 301 /** 302 * @brief Notify the value of the HF battery level. 303 * 304 * @param device The bluetooth device address. 305 * @param indValue The value of the HF battery level. 306 * @since 6 307 */ 308 void NotifyHfBatteryLevel(const RawAddress &device, int indValue); 309 310 /** 311 * @brief Register HfpAgServiceObserver instance. 312 * 313 * @param observer HfpAgServiceObserver instance. 314 * @since 6 315 */ 316 void RegisterObserver(HfpAgServiceObserver &observer) override; 317 318 /** 319 * @brief Deregister HfpAgServiceObserver instance. 320 * 321 * @param observer HfpAgServiceObserver instance. 322 * @since 6 323 */ 324 void DeregisterObserver(HfpAgServiceObserver &observer) override; 325 326 /** 327 * @brief Send the event of the HFP AG role. 328 * 329 * @param event The event of the HFP AG role. 330 * @since 6 331 */ 332 void PostEvent(const HfpAgMessage &event); 333 334 /** 335 * @brief Process the event of the HFP AG role. 336 * 337 * @param event The event of the HFP AG role. 338 * @since 6 339 */ 340 void ProcessEvent(const HfpAgMessage &event); 341 342 /** 343 * @brief Notify the ag indicator state changed. 344 * 345 * @param what The name of the event. 346 * @param state the service state of the ag indicator 347 * @since 6 348 */ 349 void NotifyAgIndicatorStateChanged(int what, int state); 350 351 /** 352 * @brief Notify the ag incoming state changed. 353 * 354 * @param what The name of the event. 355 * @param number incoming number. 356 * @param type call type 357 * @since 9 358 */ 359 void NotifyAgIncomingStateChanged(int what, std::string number, int type); 360 361 void SendMockCmd(const HfpAgMessage &event); 362 363 /** 364 * @brief Notify the ag call response hold state changed. 365 * 366 * @param what The name of the event. 367 * @param state responsw hold state 368 * @since 9 369 */ 370 void NotifyAgResponseHoldStateChanged(int what, int state); 371 372 /** 373 * @brief Send binp bumber. 374 * 375 * @param number voice number. 376 * @since 9 377 */ 378 void SendBinpNumber(std::string number); 379 380 /** 381 * @brief Dial out call by HF. 382 * 383 * @param address The remote device address. 384 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 385 * @since 6 386 */ 387 bool DialOutCallByHf(const std::string &address); 388 389 /** 390 * @brief Dialing out call time out. 391 * 392 * @since 6 393 */ 394 void DialOutCallTimeOut(); 395 396 /** 397 * @brief Open the voice recognition by HF. 398 * 399 * @param address The remote device address. 400 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 401 * @since 6 402 */ 403 bool OpenVoiceRecognitionByHf(const std::string &address); 404 405 /** 406 * @brief Close the voice recognition by HF. 407 * 408 * @param address The remote device address. 409 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 410 * @since 6 411 */ 412 bool CloseVoiceRecognitionByHf(const std::string &address); 413 414 /** 415 * @brief Voice recognition time out. 416 * 417 * @since 6 418 */ 419 void VoiceRecognitionTimeOut(); 420 421 /** 422 * @brief Set the response CLCC timer. 423 * 424 * @param address The remote device address. 425 * @since 6 426 */ 427 void SetResponseClccTimer(const std::string &address); 428 429 /** 430 * @brief Responsing CLCC time out. 431 * 432 * @since 6 433 */ 434 void ResponseClccTimeOut(); 435 436 /** 437 * @brief Service level connection status change. 438 * 439 * @param address The remote device address. 440 * @param toState The current state. 441 * @since 6 442 */ 443 void SlcStateChanged(const std::string &address, int toState); 444 445 /** 446 * @brief Audio State change. 447 * 448 * @param address The remote device address. 449 * @param fromState The previous state. 450 * @param toState The current state. 451 * @since 6 452 */ 453 void ScoStateChanged(const std::string &address, int fromState, int toState); 454 455 /** 456 * @brief Check if it is inbang ringing. 457 * 458 * @return Returns if is inBanding ring or not. 459 * @since 6 460 */ 461 bool IsInbandRinging(); 462 463 /** 464 * @brief Notify the status of the registration. 465 * 466 * @param status the status of the registration. 467 * @since 6 468 */ 469 void NotifyRegistrationStatusChanged(int status); 470 471 /** 472 * @brief Notify the signal strength. 473 * 474 * @param signalStrength The signal strength. 475 * @since 6 476 */ 477 void NotifySignalStrengthChanged(int signalStrength); 478 479 /** 480 * @brief Notify the roam status. 481 * 482 * @param status The roam status. 483 * @since 6 484 */ 485 void NotifyRoamingStatusChanged(int status); 486 487 /** 488 * @brief Notify the battery level. 489 * 490 * @param batteryLevel The battery level. 491 * @since 6 492 */ 493 void NotifyBatteryLevelChanged(int batteryLevel); 494 495 /** 496 * @brief Notify the volume change. 497 * 498 * @param type The type of the volume 499 * @param volume The value of the volume. 500 * @since 6 501 */ 502 void NotifyVolumeChanged(int type, int volume); 503 504 /** 505 * @brief Set in-band ring. 506 * @param action If <b>true</b> set in-band ring enabled, else disabled. 507 * @since 6 508 */ 509 void SetInbandRing(bool action); 510 511 /** 512 * @brief respones ok to hf. 513 * @since 9 514 */ 515 void ResponesOK(const std::string &address); 516 517 private: 518 /** 519 * @brief Service startup. 520 * 521 * @since 6 522 */ 523 void StartUp(); 524 525 /** 526 * @brief Service shutdown. 527 * 528 * @since 6 529 */ 530 void ShutDown(); 531 532 /** 533 * @brief Release single audio connection. 534 * 535 * @param device The remote device address. 536 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 537 * @since 6 538 */ 539 bool DisconnectSingleSco(const std::string &address); 540 541 /** 542 * @brief Get the connected and connecting device number. 543 * 544 * @return Returns the number of the Connected Device. 545 * @since 6 546 */ 547 int GetConnectedDeviceNum() const; 548 549 /** 550 * @brief Get the max connection devices number. 551 * 552 * @return Returns the max connection devices number. 553 * @since 6 554 */ 555 int GetMaxConnectionDevicesNum() const; 556 557 /** 558 * @brief Check whether device is connected. 559 * 560 * @param address: The remote device address. 561 * @return Returns <b>true</b> if the is connected; returns <b>false</b> if not connected. 562 * @since 6 563 */ 564 bool IsConnected(const std::string &address) const; 565 566 /** 567 * @brief Clear active device. 568 * 569 * @since 6 570 */ 571 void ClearActiveDevice(); 572 573 /** 574 * @brief Check whether device is active device. 575 * 576 * @param address: The remote device address. 577 * @return Returns <b>true</b> if device is active device; returns <b>false</b> if not. 578 * @since 6 579 */ 580 bool IsActiveDevice(const std::string &address) const; 581 582 /** 583 * @brief Modify active device. 584 * 585 * @param address: new active device address. 586 * @since 6 587 */ 588 void ModifyActiveDevice(const std::string &newAddress); 589 590 /** 591 * @brief Check whether audio is connected. 592 * 593 * @return Returns true if the audio is connected else return false. 594 * @return Returns <b>true</b> if the audio is connected; returns <b>false</b> if audio is not connected. 595 * @since 6 596 */ 597 bool IsAudioConnected() const; 598 599 /** 600 * @brief Check if audio is idle. 601 * 602 * @return Returns <b>true</b> if the audio is idle; returns <b>false</b> if audio is not idle. 603 * @since 6 604 */ 605 bool IsAudioIdle() const; 606 607 /** 608 * @brief Check if sco is used by call. 609 * 610 * @return Returns <b>true</b> if sco used by call; returns <b>false</b> if not. 611 * @since 6 612 */ 613 bool IsIncall(); 614 615 /** 616 * @brief Send the event to each device's state machine. 617 * 618 * @param event The event of the HFP AG role. 619 * @since 6 620 */ 621 void SendEventToEachStateMachine(const HfpAgMessage &event) const; 622 623 /** 624 * @brief Update ag indicators. 625 * 626 * @param event The event of the HFP AG role. 627 * @since 6 628 */ 629 void UpdateAgIndicators() const; 630 631 /** 632 * @brief Process connect or connect request event of the HFP AG role. 633 * 634 * @param event The connect or connect request event. 635 * @since 6 636 */ 637 void ProcessConnectEvent(const HfpAgMessage &event); 638 639 /** 640 * @brief Process remove state machine event of the HFP AG role. 641 * 642 * @param address The remote device address. 643 * @since 6 644 */ 645 void ProcessRemoveStateMachine(const std::string &address); 646 647 /** 648 * @brief Process default event of the HFP AG role. 649 * 650 * @param event The default event. 651 * @since 6 652 */ 653 void ProcessDefaultEvent(const HfpAgMessage &event) const; 654 655 /** 656 * @brief Process voice recognition time out event of the HFP AG role. 657 * 658 * @param event The voice recognition time out event. 659 * @since 6 660 */ 661 void ProcessVoiceRecognitionTimeOut(const HfpAgMessage &event); 662 663 /** 664 * @brief Process response CLCC time out event of the HFP AG role. 665 * 666 * @param event The response CLCC time out event. 667 * @since 6 668 */ 669 void ProcessResponseClccTimeOut(const HfpAgMessage &event); 670 671 /** 672 * @brief Check if voice recognition available. 673 * 674 * @param address Open voice recognition device address. 675 * @return Returns if voice recognition should to be open. 676 * @since 6 677 */ 678 bool IsVoiceRecognitionAvailable(const std::string &address) const; 679 680 /** 681 * @brief Process response dial time out event of the HFP AG role. 682 * 683 * @since 6 684 */ 685 void ProcessDialOutTimeOut(); 686 687 /** 688 * @brief Open the voice recognition internal 689 * 690 * @param device The Remote device address. 691 * @param isRequestByHf true if VR request by hf, else false. 692 * @since 6 693 */ 694 void SendOpenVoiceEvent(const std::string &address, bool isRequestByHf); 695 696 /** 697 * @brief Close the voice recognition internal 698 * 699 * @param device The Remote device address. 700 * @since 6 701 */ 702 void SendCloseVoiceEvent(const std::string &address); 703 704 // service status 705 bool isStarted_ {false}; 706 707 // service status 708 bool isShuttingDown_ {false}; 709 710 // The maximum default number of connection devices. 711 inline static constexpr int MAX_DEFAULT_CONNECTIONS_NUM {6}; 712 713 // The maximum number of connected devices. 714 int maxConnectedNum_ {MAX_DEFAULT_CONNECTIONS_NUM}; 715 716 // The time of dialing out timeout 717 inline static constexpr int DIALING_OUT_TIMEOUT_TIME {10000}; 718 719 // The time of voice recognition timeout 720 inline static constexpr int VOICE_RECOGNITION_TIMEOUT_TIME {5000}; 721 722 // The time of response clcc timeout 723 inline static constexpr int RESPONSE_CLCC_TIMEOUT_TIME {5000}; 724 725 // The empty address 726 inline static const std::string NULL_ADDRESS {""}; 727 728 // The mutex variable 729 std::recursive_mutex mutex_ {}; 730 731 // The flag for Voice recognition opened. 732 bool isVrOpened_ {false}; 733 734 // The map of the device and sate machine. 735 std::map<const std::string, std::unique_ptr<HfpAgStateMachine>> stateMachines_ {}; 736 737 // The list of the observer pointer. 738 std::list<HfpAgServiceObserver *> observers_ {}; 739 740 // dialing out time out. 741 std::unique_ptr<utility::Timer> dialingOutTimeout_ {nullptr}; 742 743 // voice recognition time out. 744 std::unique_ptr<utility::Timer> voiceRecognitionTimeout_ {nullptr}; 745 746 // Response Clcc time out. 747 std::unique_ptr<utility::Timer> ResponseClccTimeout_ {nullptr}; 748 749 // The status of the inband ringing. 750 bool isInbandRinging_ {false}; 751 752 // The device address of the querying clcc. 753 std::string queryClccAddress_ {NULL_ADDRESS}; 754 755 // const state map 756 const std::map<const int, const int> stateMap_ = { 757 {HFP_AG_STATE_DISCONNECTED, static_cast<int>(BTConnectState::DISCONNECTED)}, 758 {HFP_AG_STATE_CONNECTING, static_cast<int>(BTConnectState::CONNECTING)}, 759 {HFP_AG_STATE_DISCONNECTING, static_cast<int>(BTConnectState::DISCONNECTING)}, 760 {HFP_AG_STATE_CONNECTED, static_cast<int>(BTConnectState::CONNECTED)} 761 }; 762 763 int mockState_ {0}; 764 std::vector<MockCall> callList_; 765 766 BT_DISALLOW_COPY_AND_ASSIGN(HfpAgService); 767 }; 768 } // namespace bluetooth 769 } // namespace OHOS 770 #endif // HFP_AG_SERVICE_H