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_hf_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_HF_SERVICE_H 35 #define HFP_HF_SERVICE_H 36 37 #include <optional> 38 39 #include "context.h" 40 #include "hfp_hf_message.h" 41 #include "hfp_hf_statemachine.h" 42 #include "interface_profile_hfp_hf.h" 43 #include "message.h" 44 #include "raw_address.h" 45 46 namespace OHOS { 47 namespace bluetooth { 48 /** 49 * @brief This HFP class provides a set of methods that is called by Framework API, and manage 50 * the state machine and the message queue. 51 * 52 * @since 6 53 */ 54 class HfpHfService : public IProfileHfpHf, public utility::Context { 55 public: 56 /** 57 * @brief Get the instance of the HfpHfService object. 58 * 59 * @return Returns the instance of the HfpHfService object. 60 * @since 6 61 */ 62 static HfpHfService *GetService(); 63 64 /** 65 * @brief Construct a new HfpHfService object. 66 * 67 * @since 6 68 */ 69 HfpHfService(); 70 71 /** 72 * @brief Destroy the HfpHfService object. 73 * 74 * @since 6 75 */ 76 ~HfpHfService(); 77 78 /** 79 * @brief Get the instance of the HfpAgService object. 80 * 81 * @return Returns the instance of the HfpAgService object. 82 * @since 6 83 */ 84 utility::Context *GetContext() override; 85 86 /** 87 * @brief Enable the target service. 88 * 89 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 90 * @since 6 91 */ 92 void Enable() override; 93 94 /** 95 * @brief Disable the target service. 96 * 97 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 98 * @since 6 99 */ 100 void Disable() override; 101 102 /** 103 * @brief Finish the shut down service. 104 * 105 * @since 6 106 */ 107 void ShutDownDone(bool isAllDisconnected); 108 109 /** 110 * @brief Establish the service level connection. 111 * 112 * @param device The remote device. 113 * @return Returns the connect result code. 114 * @since 6 115 */ 116 int Connect(const RawAddress &device) override; 117 118 /** 119 * @brief Release the service level connection. 120 * 121 * @param device The remote device. 122 * @return Returns the disconnect result code. 123 * @since 6 124 */ 125 int Disconnect(const RawAddress &device) override; 126 127 /** 128 * @brief Get the connected devices list. 129 * 130 * @return Returns the connected devices list. 131 * @since 6 132 */ 133 std::list<RawAddress> GetConnectDevices() override; 134 135 /** 136 * @brief Get the connection state. 137 * 138 * @return Returns the connection state. 139 * @since 6 140 */ 141 int GetConnectState() override; 142 143 /** 144 * @brief Get the maximum number of connected devices. 145 * 146 * @return Returns the max connected devices number. 147 * @since 6 148 */ 149 int GetMaxConnectNum() override; 150 151 /** 152 * @brief Establish the audio connection. 153 * 154 * @param device The remote device. 155 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 156 * @since 6 157 */ 158 bool ConnectSco(const RawAddress &device) override; 159 160 /** 161 * @brief Release the audio connection. 162 * 163 * @param device The remote device. 164 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 165 * @since 6 166 */ 167 bool DisconnectSco(const RawAddress &device) override; 168 169 /** 170 * @brief Get the devices list of the specified states. 171 * 172 * @param states The specified states. 173 * @return Returns the remote devices list of the specified states. 174 * @since 6 175 */ 176 std::vector<RawAddress> GetDevicesByStates(std::vector<int> states) override; 177 178 /** 179 * @brief Get the state by the device. 180 * 181 * @param device: The remote device. 182 * @return Returns the connection state. 183 * @since 6 184 */ 185 int GetDeviceState(const RawAddress &device) override; 186 187 /** 188 * @brief Get the audio state of the specified device. 189 * 190 * @param device The remote device. 191 * @return Returns the audio state. 192 * @since 6 193 */ 194 int GetScoState(const RawAddress &device) override; 195 196 /** 197 * @brief Send DTMF tone code to remote AG device. 198 * 199 * @param device Remote device object. 200 * @param code DTMF tone code. 201 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 202 * @since 6 203 */ 204 bool SendDTMFTone(const RawAddress &device, uint8_t code) override; 205 206 /** 207 * @brief Get a list of current all existing calls. 208 * 209 * @param device Remote device object. 210 * @return The list of all existing calls. 211 * @since 6 212 */ 213 std::vector<HandsFreeUnitCalls> GetCurrentCallList(const RawAddress &device) override; 214 215 /** 216 * @brief Accept an incoming call. 217 * 218 * @param device Remote device object. 219 * @param flag Types of calls accepted. 220 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 221 * @since 6 222 */ 223 bool AcceptIncomingCall(const RawAddress &device, int flag) override; 224 225 /** 226 * @brief Hold an active call. 227 * 228 * @param device Remote device object. 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 HoldActiveCall(const RawAddress &device) override; 233 234 /** 235 * @brief Reject an incoming call. 236 * 237 * @param device Remote device object. 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 RejectIncomingCall(const RawAddress &device) override; 242 243 /** 244 * @brief Send key pressed event. 245 * 246 * @param device Remote device object. 247 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 248 * @since 9 249 */ 250 bool SendKeyPressed(const RawAddress &device) override; 251 252 /** 253 * @brief handle an incoming call. 254 * 255 * @param device Remote device object. 256 * @param flag Types of incoming calls handle. 257 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 258 * @since 9 259 */ 260 bool HandleIncomingCall(const RawAddress &device, int flag) override; 261 262 /** 263 * @brief handle multi call. 264 * 265 * @param device Remote device object. 266 * @param flag Types of incoming calls handle. 267 * @param index call index 268 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 269 * @since 9 270 */ 271 bool HandleMultiCall(const RawAddress &device, int flag, int index) override; 272 273 /** 274 * @brief Dial last number. 275 * 276 * @param device Remote device object. 277 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 278 * @since 9 279 */ 280 bool DialLastNumber(const RawAddress &device) override; 281 282 /** 283 * @brief Dial memory. 284 * 285 * @param device Remote device object. 286 * @param index Memory index. 287 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 288 * @since 9 289 */ 290 bool DialMemory(const RawAddress &device, int index) override; 291 292 /** 293 * @brief Send voice tag. 294 * 295 * @param device Remote device object. 296 * @param index voice tag index. 297 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 298 * @since 9 299 */ 300 bool SendVoiceTag(const RawAddress &device, int index) override; 301 302 /** 303 * @brief Finish a specified active call. 304 * 305 * @param device Remote device object. 306 * @param call Call object. 307 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 308 * @since 6 309 */ 310 bool FinishActiveCall(const RawAddress &device, const HandsFreeUnitCalls &call) override; 311 312 /** 313 * @brief Start dial with specified call number. 314 * 315 * @param device Remote device object. 316 * @param number Call's number to dial. 317 * @return The object of dial out call or nullopt. 318 * @since 6 319 */ 320 std::optional<HandsFreeUnitCalls> StartDial(const RawAddress &device, const std::string &number) override; 321 322 /** 323 * @brief Register HfpHfServiceObserver instance. 324 * 325 * @param observer HfpHfServiceObserver instance. 326 * @since 6 327 */ 328 void RegisterObserver(HfpHfServiceObserver &observer) override; 329 330 /** 331 * @brief Deregister HfpHfServiceObserver instance. 332 * 333 * @param observer HfpHfServiceObserver instance. 334 * @since 6 335 */ 336 void DeregisterObserver(HfpHfServiceObserver &observer) override; 337 338 /** 339 * @brief Set the Hf Volume. 340 * 341 * @param volume The value of the volume. 342 * @param type The type of the volume. 343 * @since 6 344 */ 345 void SetHfVolume(int volume, int type); 346 347 /** 348 * @brief Open voice recognition. 349 * 350 * @param device The remote device. 351 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 352 * @since 6 353 */ 354 bool OpenVoiceRecognition(const RawAddress &device) override; 355 356 /** 357 * @brief Close voice recognition. 358 * 359 * @param device The remote device. 360 * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails. 361 * @since 6 362 */ 363 bool CloseVoiceRecognition(const RawAddress &device) override; 364 365 /** 366 * @brief Send the event of the HFP HF role. 367 * 368 * @param event The event of the HFP HF role. 369 * @since 6 370 */ 371 void PostEvent(const HfpHfMessage &event); 372 373 /** 374 * @brief Process the event of the HFP HF role. 375 * 376 * @param event The event of the HFP HF role. 377 * @since 6 378 */ 379 void ProcessEvent(const HfpHfMessage &event); 380 381 /** 382 * @brief Notify the HF battery level change. 383 * 384 * @param batteryLevel The value of the battery level. 385 * @since 6 386 */ 387 void BatteryLevelChanged(int batteryLevel); 388 389 /** 390 * @brief Notify the HF enhanced driver safety change. 391 * 392 * @param state The state of the enhanced driver safety 393 * @since 6 394 */ 395 void EnhancedDriverSafety(int state); 396 397 /** 398 * @brief Notify the connection state change. 399 * 400 * @param device The remote device. 401 * @param state The state of the connection. 402 * @since 6 403 */ 404 void NotifyStateChanged(const RawAddress &device, int state); 405 406 /** 407 * @brief Notify the audio connection state change. 408 * 409 * @param device The remote device. 410 * @param state The state of the audio connection. 411 * @since 6 412 */ 413 void NotifyScoStateChanged(const RawAddress &device, int state); 414 415 /** 416 * @brief Notify the call change. 417 * 418 * @param device The remote device. 419 * @param call The changed call. 420 * @since 6 421 */ 422 void NotifyCallChanged(const RawAddress &device, const HandsFreeUnitCalls &call); 423 424 /** 425 * @brief Notify the battery level change. 426 * 427 * @param device The remote device. 428 * @param batteryLevel The battery level. 429 * @since 6 430 */ 431 void NotifyBatteryLevelChanged(const RawAddress &device, int batteryLevel); 432 433 /** 434 * @brief Notify the signal strength change. 435 * 436 * @param device The remote device. 437 * @param signal The signal strength. 438 * @since 6 439 */ 440 void NotifySignalStrengthChanged(const RawAddress &device, int signal); 441 442 /** 443 * @brief Notify the registration status change. 444 * 445 * @param device The remote device. 446 * @param status The registration status. 447 * @since 6 448 */ 449 void NotifyRegistrationStatusChanged(const RawAddress &device, int status); 450 451 /** 452 * @brief Notify the roam status change. 453 * 454 * @param device The remote device. 455 * @param status The roam state. 456 * @since 6 457 */ 458 void NotifyRoamingStatusChanged(const RawAddress &device, int status); 459 460 /** 461 * @brief Notify the network operator name change. 462 * 463 * @param device The remote device. 464 * @param name The network operator name. 465 * @since 6 466 */ 467 void NotifyOperatorSelectionChanged(const RawAddress &device, const std::string &name); 468 469 /** 470 * @brief Notify the subscriber number change. 471 * 472 * @param device : The remote device. 473 * @param number The subscriber number. 474 * @since 6 475 */ 476 void NotifySubscriberNumberChanged(const RawAddress &device, const std::string &number); 477 478 /** 479 * @brief Notify the voice recognition state change. 480 * 481 * @param device : The remote device. 482 * @param status : The voice recognition state. 483 * @since 6 484 */ 485 void NotifyVoiceRecognitionStatusChanged(const RawAddress &device, int status); 486 487 /** 488 * @brief Notify the inBand ring tone. 489 * 490 * @param device : The remote device. 491 * @param status : The status of the inBand ring. 492 * @since 6 493 */ 494 void NotifyInBandRingTone(const RawAddress &device, int status); 495 496 /** 497 * @brief Send the AT command. 498 * @param device : The remote device. 499 * @param cmdId : The id of the at command 500 * @param arg : The string of the at command 501 * @since 6 502 */ 503 void SendAtCommand(const RawAddress &device, int cmdId, const std::string &arg); 504 505 private: 506 /** 507 * @brief Service startup. 508 * 509 * @since 6 510 */ 511 void StartUp(); 512 513 /** 514 * @brief Service shutdown. 515 * 516 * @since 6 517 */ 518 void ShutDown(); 519 520 /** 521 * @brief Get the connected and connecting device number. 522 * 523 * @return Returns the number of the Connected Device. 524 * @since 6 525 */ 526 int GetConnectionsDeviceNum() const; 527 528 /** 529 * @brief Get the max connection devices number. 530 * 531 * @return Returns the max connection devices number. 532 * @since 6 533 */ 534 int GetMaxConnectionsDeviceNum() const; 535 536 /** 537 * @brief Check whether device is connected. 538 * 539 * @param address: The remote device address. 540 * @return Returns <b>true</b> if the is connected; returns <b>false</b> if not connected. 541 * @since 6 542 */ 543 bool IsConnected(const std::string &address) const; 544 545 /** 546 * @brief Is the sco connected. 547 * 548 * @return Returns if the audio is connected or not. 549 * @since 6 550 */ 551 bool IsScoConnected() const; 552 553 /** 554 * @brief Send the event to each state machine. 555 * 556 * @param event The event of the HFP. 557 * @since 6 558 */ 559 void SendEventToEachStateMachine(const HfpHfMessage &event) const; 560 561 /** 562 * @brief Process connect or connect request event of the HFP HF role. 563 * 564 * @param event The connect or connect request event. 565 * @since 6 566 */ 567 void ProcessConnectEvent(const HfpHfMessage &event); 568 569 /** 570 * @brief Process remove state machine event of the HFP HF role. 571 * 572 * @param address The remote device address. 573 * @since 6 574 */ 575 void ProcessRemoveStateMachine(const std::string &address); 576 577 /** 578 * @brief Process default event of the HFP HF role. 579 * 580 * @param event The default event. 581 * @since 6 582 */ 583 void ProcessDefaultEvent(const HfpHfMessage &event) const; 584 585 // service status 586 bool isStarted_ {false}; 587 588 // service status 589 bool isShuttingDown_ {false}; 590 591 // the maximum number of connection devices. 592 static inline constexpr int MAX_CONNECTIONS_NUM {6}; 593 594 // the maximum number of connection devices. 595 int maxConnectionsNum_ {MAX_CONNECTIONS_NUM}; 596 597 // the map of the device and sate machine 598 std::map<const std::string, std::unique_ptr<HfpHfStateMachine>> stateMachines_ {}; 599 600 // the mutex variable 601 std::recursive_mutex mutex_ {}; 602 603 // The observer list. 604 std::list<HfpHfServiceObserver *> observers_ {}; 605 606 // const state map 607 const std::map<const int, const int> stateMap_ = { 608 {HFP_HF_STATE_DISCONNECTED, static_cast<int>(BTConnectState::DISCONNECTED)}, 609 {HFP_HF_STATE_CONNECTING, static_cast<int>(BTConnectState::CONNECTING)}, 610 {HFP_HF_STATE_DISCONNECTING, static_cast<int>(BTConnectState::DISCONNECTING)}, 611 {HFP_HF_STATE_CONNECTED, static_cast<int>(BTConnectState::CONNECTED)} 612 }; 613 614 BT_DISALLOW_COPY_AND_ASSIGN(HfpHfService); 615 DECLARE_IMPL(); 616 }; 617 } // namespace bluetooth 618 } // namespace OHOS 619 #endif // HFP_HF_SERVICE_H