1 /*
2  * Copyright (C) 2023 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 #include "voip_call_connection.h"
17 
18 #include "i_voip_call_manager_callback.h"
19 #include "i_voip_call_manager_service.h"
20 #include "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23 #include "telephony_log_wrapper.h"
24 #include "voip_call_manager_proxy.h"
25 
26 namespace OHOS {
27 namespace Telephony {
VoipCallConnection()28 VoipCallConnection::VoipCallConnection()
29     : systemAbilityId_(TELEPHONY_VOIP_CALL_MANAGER_SYS_ABILITY_ID), connectCallManagerState_(false)
30 {}
31 
~VoipCallConnection()32 VoipCallConnection::~VoipCallConnection()
33 {
34     UnInit();
35 }
36 
Init(int32_t systemAbilityId)37 void VoipCallConnection::Init(int32_t systemAbilityId)
38 {
39     std::lock_guard<std::mutex> lock(mutex_);
40     if (connectCallManagerState_) {
41         TELEPHONY_LOGE("Init, connectState is true");
42         return;
43     }
44     systemAbilityId_ = systemAbilityId;
45     TELEPHONY_LOGI("systemAbilityId_ = %{public}d", systemAbilityId);
46     GetCallManagerProxy();
47     statusChangeListener_ = new (std::nothrow) SystemAbilityListener();
48     if (statusChangeListener_ == nullptr) {
49         TELEPHONY_LOGE("Init, failed to create statusChangeListener.");
50         return;
51     }
52     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53     if (managerPtr == nullptr) {
54         TELEPHONY_LOGE("voipconnect managerPtr is null");
55         return;
56     }
57     int32_t ret = managerPtr->SubscribeSystemAbility(systemAbilityId_, statusChangeListener_);
58     if (ret != TELEPHONY_SUCCESS) {
59         TELEPHONY_LOGE("Init, failed to subscribe sa:%{public}d", systemAbilityId_);
60         return;
61     }
62     TELEPHONY_LOGI("subscribe voip call manager successfully!");
63 }
64 
UnInit()65 void VoipCallConnection::UnInit()
66 {
67     std::lock_guard<std::mutex> lock(mutex_);
68     voipCallManagerInterfacePtr_ = nullptr;
69     connectCallManagerState_ = false;
70     if (statusChangeListener_ != nullptr) {
71         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
72         if (samgrProxy != nullptr) {
73             samgrProxy->UnSubscribeSystemAbility(systemAbilityId_, statusChangeListener_);
74             statusChangeListener_ = nullptr;
75         }
76     }
77     TELEPHONY_LOGI("voip call connection uninit");
78 }
79 
GetCallManagerProxy()80 int32_t VoipCallConnection::GetCallManagerProxy()
81 {
82     TELEPHONY_LOGI("Voipconnect GetCallManagerProxy start");
83     if (voipCallManagerInterfacePtr_ != nullptr) {
84         return TELEPHONY_SUCCESS;
85     }
86     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87     if (managerPtr == nullptr) {
88         TELEPHONY_LOGI("Voipconnect managerPtr is null");
89         return TELEPHONY_ERR_LOCAL_PTR_NULL;
90     }
91     sptr<IVoipCallManagerService> voipCallManagerInterfacePtr = nullptr;
92     sptr<IRemoteObject> iRemoteObjectPtr = managerPtr->GetSystemAbility(systemAbilityId_);
93     if (iRemoteObjectPtr == nullptr) {
94         TELEPHONY_LOGI("Voipconnect iRemoteObjectPtr is null");
95         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
96     }
97     voipCallManagerInterfacePtr = iface_cast<IVoipCallManagerService>(iRemoteObjectPtr);
98     if (!voipCallManagerInterfacePtr) {
99         TELEPHONY_LOGI("Voipconnect GetCallManagerProxy voipCallManagerInterfacePtr is null");
100         return TELEPHONY_ERR_LOCAL_PTR_NULL;
101     }
102 
103     voipCallManagerInterfacePtr_ = voipCallManagerInterfacePtr;
104     connectCallManagerState_ = true;
105     return TELEPHONY_SUCCESS;
106 }
107 
AnswerCall(const VoipCallEventInfo & events,int32_t videoState)108 int32_t VoipCallConnection::AnswerCall(const VoipCallEventInfo &events, int32_t videoState)
109 {
110     GetCallManagerProxy();
111     if (voipCallManagerInterfacePtr_ == nullptr) {
112         TELEPHONY_LOGI("Voipconnect AnswerCall voipCallManagerInterfacePtr_ is null");
113         return TELEPHONY_ERROR;
114     }
115     return voipCallManagerInterfacePtr_->Answer(events, videoState);
116 }
117 
RejectCall(const VoipCallEventInfo & events)118 int32_t VoipCallConnection::RejectCall(const VoipCallEventInfo &events)
119 {
120     GetCallManagerProxy();
121     if (voipCallManagerInterfacePtr_ == nullptr) {
122         TELEPHONY_LOGI("Voipconnect RejectCall voipCallManagerInterfacePtr_ is null");
123         return TELEPHONY_ERROR;
124     }
125     return voipCallManagerInterfacePtr_->Reject(events);
126 }
127 
HangUpCall(const VoipCallEventInfo & events)128 int32_t VoipCallConnection::HangUpCall(const VoipCallEventInfo &events)
129 {
130     GetCallManagerProxy();
131     if (voipCallManagerInterfacePtr_ == nullptr) {
132         TELEPHONY_LOGI("Voipconnect HangUpCall voipCallManagerInterfacePtr_ is null");
133         return TELEPHONY_ERROR;
134     }
135     return voipCallManagerInterfacePtr_->HangUp(events);
136 }
137 
RegisterCallManagerCallBack(const sptr<ICallStatusCallback> & callback)138 int32_t VoipCallConnection::RegisterCallManagerCallBack(const sptr<ICallStatusCallback> &callback)
139 {
140     GetCallManagerProxy();
141     if (voipCallManagerInterfacePtr_ == nullptr) {
142         TELEPHONY_LOGI("Voipconnect RegisterCallManagerCallBack voipCallManagerInterfacePtr_ is null");
143         return TELEPHONY_ERROR;
144     }
145     return voipCallManagerInterfacePtr_->RegisterCallManagerCallBack(callback);
146 }
147 
UnRegisterCallManagerCallBack()148 int32_t VoipCallConnection::UnRegisterCallManagerCallBack()
149 {
150     GetCallManagerProxy();
151     if (voipCallManagerInterfacePtr_ == nullptr) {
152         TELEPHONY_LOGI("Voipconnect UnRegisterCallManagerCallBack voipCallManagerInterfacePtr_ is null");
153         return TELEPHONY_ERROR;
154     }
155     int32_t ret = voipCallManagerInterfacePtr_->UnRegisterCallManagerCallBack();
156     UnInit();
157     return ret;
158 }
159 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)160 void VoipCallConnection::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
161 {
162     auto voipCallConnection = DelayedSingleton<VoipCallConnection>::GetInstance();
163     if (voipCallConnection == nullptr) {
164         TELEPHONY_LOGE("voipCallConnection is nullptr");
165         return;
166     }
167     voipCallConnection->Init(systemAbilityId);
168 }
169 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)170 void VoipCallConnection::SystemAbilityListener::OnRemoveSystemAbility(
171     int32_t systemAbilityId, const std::string &deviceId)
172 {
173     auto voipCallConnection = DelayedSingleton<VoipCallConnection>::GetInstance();
174     if (voipCallConnection == nullptr) {
175         TELEPHONY_LOGE("voipCallConnection is nullptr");
176         return;
177     }
178     voipCallConnection->ClearVoipCall();
179     voipCallConnection->UnInit();
180 }
181 
ClearVoipCall()182 void VoipCallConnection::ClearVoipCall()
183 {
184     if (!CallObjectManager::HasVoipCallExist()) {
185         TELEPHONY_LOGI("no voip call exist, no need to clear");
186         return;
187     }
188     std::list<sptr<CallBase>> allCallList = CallObjectManager::GetAllCallList();
189     for (auto call : allCallList) {
190         if (call != nullptr && call->GetCallType() == CallType::TYPE_VOIP) {
191             TELEPHONY_LOGI("clearVoipCall callId %{public}d", call->GetCallID());
192             CallObjectManager::DeleteOneCallObject(call);
193         }
194     }
195 }
196 
SendCallUiEvent(std::string voipCallId,const CallAudioEvent & callAudioEvent)197 int32_t VoipCallConnection::SendCallUiEvent(std::string voipCallId, const CallAudioEvent &callAudioEvent)
198 {
199     GetCallManagerProxy();
200     if (voipCallManagerInterfacePtr_ == nullptr) {
201         TELEPHONY_LOGE("voipCallManagerInterfacePtr_ is nullptr");
202         return TELEPHONY_ERROR;
203     }
204     return voipCallManagerInterfacePtr_->SendCallUiEvent(voipCallId, callAudioEvent);
205 }
206 } // namespace Telephony
207 } // namespace OHOS
208