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