1 /* 2 * Copyright (C) 2023-2025 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 TELEPHONY_DISTRIBUTED_CALL_MANAGER_H 17 #define TELEPHONY_DISTRIBUTED_CALL_MANAGER_H 18 19 #include <string> 20 #include <memory> 21 #include <map> 22 #include <mutex> 23 #include <atomic> 24 25 #include "singleton.h" 26 #include "idcall_device_callback.h" 27 #include "iservice_registry.h" 28 #include "system_ability_definition.h" 29 #include "system_ability_status_change_stub.h" 30 #include "call_manager_inner_type.h" 31 #include "distributed_call_proxy.h" 32 #ifdef ABILITY_BLUETOOTH_SUPPORT 33 #include "bluetooth_hfp_ag.h" 34 #endif 35 36 namespace OHOS { 37 namespace Telephony { 38 #ifdef ABILITY_BLUETOOTH_SUPPORT 39 class DCallHfpListener : public Bluetooth::HandsFreeAudioGatewayObserver { 40 public: 41 DCallHfpListener() = default; 42 ~DCallHfpListener() override = default; 43 void OnHfpStackChanged(const Bluetooth::BluetoothRemoteDevice &device, int32_t action) override; 44 45 private: 46 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(DCallHfpListener); 47 }; 48 #endif 49 class DistributedCallManager : public std::enable_shared_from_this<DistributedCallManager> { 50 DECLARE_DELAYED_SINGLETON(DistributedCallManager) 51 public: 52 void Init(); 53 int32_t AddDCallDevice(const std::string& devId); 54 int32_t RemoveDCallDevice(const std::string& devId); 55 void ClearDCallDevices(); 56 void ClearConnectedDCallDevice(); 57 std::string GetConnectedDCallDeviceAddr(); 58 AudioDeviceType GetConnectedDCallDeviceType(); 59 void SwitchOffDCallDeviceSync(); 60 bool IsDCallDeviceSwitchedOn(); 61 bool SwitchOnDCallDeviceSync(const AudioDevice& device); 62 void SwitchOnDCallDeviceAsync(const AudioDevice& device); 63 void SetCallState(bool isActive); 64 void DealDisconnectCall(); 65 bool IsDistributedCarDeviceOnline(); 66 67 int32_t OnDCallDeviceOnline(const std::string &devId); 68 int32_t OnDCallDeviceOffline(const std::string &devId); 69 void OnDCallSystemAbilityAdded(const std::string &deviceId); 70 void OnDCallSystemAbilityRemoved(const std::string &deviceId); 71 72 void GetConnectedDCallDevice(AudioDevice& device); 73 bool IsSelectVirtualModem(); 74 void ReportDistributedDeviceInfo(const AudioDevice& device); 75 void ReportDistributedDeviceInfoForSwitchOff(); 76 77 std::string GetConnectedDCallDeviceId(); 78 private: 79 class DistributedCallDeviceListener : public OHOS::DistributedHardware::IDCallDeviceCallback { 80 public: 81 DistributedCallDeviceListener() = default; 82 ~DistributedCallDeviceListener() = default; 83 84 int32_t OnDCallDeviceOnline(const std::string &devId) override; 85 int32_t OnDCallDeviceOffline(const std::string &devId) override; 86 }; 87 88 bool CreateDAudioDevice(const std::string& devId, AudioDevice& device); 89 std::string GetDevIdFromAudioDevice(const AudioDevice& device); 90 void NotifyOnlineDCallDevices(std::vector<std::string> devices); 91 bool isCeliaCall(); 92 void SetConnectedDCallDevice(const AudioDevice& device); 93 94 private: 95 std::atomic<bool> isCallActived_ = false; 96 std::atomic<bool> dCallDeviceSwitchedOn_ = false; 97 std::mutex connectedDevMtx_; 98 std::mutex onlineDeviceMtx_; 99 std::string connectedDevId_; 100 AudioDevice connectedAudioDevice_; 101 AudioDevice currentAudioDevice_; 102 std::map<std::string, AudioDevice> onlineDCallDevices_; 103 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 104 std::mutex dcallProxyMtx_; 105 std::shared_ptr<DistributedCallProxy> dcallProxy_ = nullptr; 106 std::shared_ptr<DistributedCallDeviceListener> dcallDeviceListener_ = nullptr; 107 bool isSwitching_ = false; 108 #ifdef ABILITY_BLUETOOTH_SUPPORT 109 std::mutex mutex_; 110 std::shared_ptr<DCallHfpListener> dcallHfpListener_{nullptr}; 111 #endif 112 }; 113 114 class DCallSystemAbilityListener : public SystemAbilityStatusChangeStub { 115 public: 116 DCallSystemAbilityListener() = default; 117 ~DCallSystemAbilityListener() = default; 118 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 119 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 120 }; 121 122 } // namespace Telephony 123 } // namespace OHOS 124 #endif // TELEPHONY_DISTRIBUTED_CALL_MANAGER_H 125