1 /*
2 * Copyright (C) 2021 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 "carrier_call.h"
17
18 #include <securec.h>
19
20 #include "call_manager_errors.h"
21 #include "call_manager_hisysevent.h"
22 #include "telephony_log_wrapper.h"
23
24 #include "call_number_utils.h"
25
26 namespace OHOS {
27 namespace Telephony {
CarrierCall(DialParaInfo & info)28 CarrierCall::CarrierCall(DialParaInfo &info)
29 : CallBase(info), dialScene_(DialScene::CALL_NORMAL), slotId_(info.accountId), index_(info.index),
30 isEcc_(info.isEcc), cellularCallConnectionPtr_(DelayedSingleton<CellularCallConnection>::GetInstance())
31 {}
32
CarrierCall(DialParaInfo & info,AppExecFwk::PacMap & extras)33 CarrierCall::CarrierCall(DialParaInfo &info, AppExecFwk::PacMap &extras)
34 : CallBase(info, extras), dialScene_((DialScene)extras.GetIntValue("dialScene")), slotId_(info.accountId),
35 index_(info.index), isEcc_(info.isEcc),
36 cellularCallConnectionPtr_(DelayedSingleton<CellularCallConnection>::GetInstance())
37 {}
38
~CarrierCall()39 CarrierCall::~CarrierCall() {}
40
CarrierDialingProcess()41 int32_t CarrierCall::CarrierDialingProcess()
42 {
43 TELEPHONY_LOGI("CarrierDialingProcess start");
44 int32_t ret = DialCallBase();
45 if (ret != TELEPHONY_SUCCESS) {
46 CarrierHangUpCall();
47 }
48 return ret;
49 }
50
CarrierAnswerCall(int32_t videoState)51 int32_t CarrierCall::CarrierAnswerCall(int32_t videoState)
52 {
53 CellularCallInfo callInfo;
54 int32_t ret = AnswerCallBase();
55 if (ret != TELEPHONY_SUCCESS) {
56 TELEPHONY_LOGE("answer call failed!");
57 CallManagerHisysevent::WriteAnswerCallFaultEvent(
58 slotId_, INVALID_PARAMETER, videoState, ret, "the device is currently not ringing");
59 return CALL_ERR_ANSWER_FAILED;
60 }
61 ret = PackCellularCallInfo(callInfo);
62 if (ret != TELEPHONY_SUCCESS) {
63 TELEPHONY_LOGW("PackCellularCallInfo failed!");
64 CallManagerHisysevent::WriteAnswerCallFaultEvent(
65 slotId_, INVALID_PARAMETER, videoState, ret, "PackCellularCallInfo failed");
66 }
67 if (cellularCallConnectionPtr_ == nullptr) {
68 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
69 return TELEPHONY_ERR_LOCAL_PTR_NULL;
70 }
71 ret = cellularCallConnectionPtr_->Answer(callInfo);
72 if (ret != TELEPHONY_SUCCESS) {
73 TELEPHONY_LOGE("answer call failed!");
74 return CALL_ERR_ANSWER_FAILED;
75 }
76 return TELEPHONY_SUCCESS;
77 }
78
CarrierRejectCall()79 int32_t CarrierCall::CarrierRejectCall()
80 {
81 CellularCallInfo callInfo;
82 int32_t ret = RejectCallBase();
83 if (ret != TELEPHONY_SUCCESS) {
84 return ret;
85 }
86 ret = PackCellularCallInfo(callInfo);
87 if (ret != TELEPHONY_SUCCESS) {
88 TELEPHONY_LOGW("PackCellularCallInfo failed!");
89 CallManagerHisysevent::WriteHangUpFaultEvent(
90 slotId_, callInfo.callId, ret, "Reject PackCellularCallInfo failed");
91 }
92 TelCallState state = GetTelCallState();
93 if (cellularCallConnectionPtr_ == nullptr) {
94 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
95 return TELEPHONY_ERR_LOCAL_PTR_NULL;
96 }
97 if (state == TelCallState::CALL_STATUS_INCOMING) {
98 ret = cellularCallConnectionPtr_->Reject(callInfo);
99 } else {
100 ret = cellularCallConnectionPtr_->HangUp(callInfo, CallSupplementType::TYPE_DEFAULT);
101 }
102 if (ret != TELEPHONY_SUCCESS) {
103 TELEPHONY_LOGE("Reject failed!");
104 return CALL_ERR_REJECT_FAILED;
105 }
106 return TELEPHONY_SUCCESS;
107 }
108
CarrierHangUpCall()109 int32_t CarrierCall::CarrierHangUpCall()
110 {
111 int32_t ret = CALL_ERR_HANGUP_FAILED;
112 CellularCallInfo callInfo;
113 ret = PackCellularCallInfo(callInfo);
114 if (ret != TELEPHONY_SUCCESS) {
115 TELEPHONY_LOGW("PackCellularCallInfo failed!");
116 CallManagerHisysevent::WriteHangUpFaultEvent(
117 callInfo.accountId, callInfo.callId, ret, "HangUp PackCellularCallInfo failed");
118 }
119 uint64_t policyFlag = GetPolicyFlag();
120 if (cellularCallConnectionPtr_ == nullptr) {
121 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
122 return TELEPHONY_ERR_LOCAL_PTR_NULL;
123 }
124 if (policyFlag & POLICY_FLAG_HANG_UP_ACTIVE) {
125 ret = cellularCallConnectionPtr_->HangUp(callInfo, CallSupplementType::TYPE_HANG_UP_ACTIVE);
126 } else {
127 ret = cellularCallConnectionPtr_->HangUp(callInfo, CallSupplementType::TYPE_DEFAULT);
128 }
129 if (ret != TELEPHONY_SUCCESS) {
130 TELEPHONY_LOGE("End failed!");
131 return CALL_ERR_HANGUP_FAILED;
132 }
133 return TELEPHONY_SUCCESS;
134 }
135
CarrierHoldCall()136 int32_t CarrierCall::CarrierHoldCall()
137 {
138 CellularCallInfo callInfo;
139 int32_t ret = PackCellularCallInfo(callInfo);
140 if (ret != TELEPHONY_SUCCESS) {
141 TELEPHONY_LOGW("PackCellularCallInfo failed!");
142 }
143 if (cellularCallConnectionPtr_ == nullptr) {
144 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
145 return TELEPHONY_ERR_LOCAL_PTR_NULL;
146 }
147 ret = cellularCallConnectionPtr_->HoldCall(callInfo);
148 if (ret != TELEPHONY_SUCCESS) {
149 TELEPHONY_LOGE("Hold failed!");
150 return CALL_ERR_HOLD_FAILED;
151 }
152 return TELEPHONY_SUCCESS;
153 }
154
CarrierUnHoldCall()155 int32_t CarrierCall::CarrierUnHoldCall()
156 {
157 CellularCallInfo callInfo;
158 int32_t ret = PackCellularCallInfo(callInfo);
159 if (ret != TELEPHONY_SUCCESS) {
160 TELEPHONY_LOGW("PackCellularCallInfo failed!");
161 }
162 if (cellularCallConnectionPtr_ == nullptr) {
163 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
164 return TELEPHONY_ERR_LOCAL_PTR_NULL;
165 }
166 ret = cellularCallConnectionPtr_->UnHoldCall(callInfo);
167 if (ret != TELEPHONY_SUCCESS) {
168 TELEPHONY_LOGE("UnHold failed!");
169 return CALL_ERR_UNHOLD_FAILED;
170 }
171 return TELEPHONY_SUCCESS;
172 }
173
CarrierSetMute(int32_t mute,int32_t slotId)174 int32_t CarrierCall::CarrierSetMute(int32_t mute, int32_t slotId)
175 {
176 if (cellularCallConnectionPtr_ == nullptr) {
177 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
178 return TELEPHONY_ERR_LOCAL_PTR_NULL;
179 }
180 int32_t ret = cellularCallConnectionPtr_->SetMute(mute, slotId);
181 if (ret != TELEPHONY_SUCCESS) {
182 TELEPHONY_LOGE("SetMute failed!");
183 return CALL_ERR_UNHOLD_FAILED;
184 }
185 return TELEPHONY_SUCCESS;
186 }
187
GetCallAttributeCarrierInfo(CallAttributeInfo & info)188 void CarrierCall::GetCallAttributeCarrierInfo(CallAttributeInfo &info)
189 {
190 std::lock_guard<std::mutex> lock(mutex_);
191 info.isEcc = isEcc_;
192 info.accountId = slotId_;
193 GetCallAttributeBaseInfo(info);
194 }
195
GetEmergencyState()196 bool CarrierCall::GetEmergencyState()
197 {
198 std::lock_guard<std::mutex> lock(mutex_);
199 return isEcc_;
200 }
201
CarrierSwitchCall()202 int32_t CarrierCall::CarrierSwitchCall()
203 {
204 CellularCallInfo callInfo;
205 int32_t ret = PackCellularCallInfo(callInfo);
206 if (ret != TELEPHONY_SUCCESS) {
207 TELEPHONY_LOGW("PackCellularCallInfo failed!");
208 }
209 if (cellularCallConnectionPtr_ == nullptr) {
210 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
211 return TELEPHONY_ERR_LOCAL_PTR_NULL;
212 }
213 ret = cellularCallConnectionPtr_->SwitchCall(callInfo);
214 if (ret != TELEPHONY_SUCCESS) {
215 TELEPHONY_LOGE("SwitchCall failed!");
216 return CALL_ERR_UNHOLD_FAILED;
217 }
218 return TELEPHONY_SUCCESS;
219 }
220
GetSlotId()221 int32_t CarrierCall::GetSlotId()
222 {
223 return slotId_;
224 }
225
CarrierCombineConference()226 int32_t CarrierCall::CarrierCombineConference()
227 {
228 CellularCallInfo callInfo;
229 int32_t ret = PackCellularCallInfo(callInfo);
230 if (ret != TELEPHONY_SUCCESS) {
231 TELEPHONY_LOGW("PackCellularCallInfo failed!");
232 }
233 if (cellularCallConnectionPtr_ == nullptr) {
234 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
235 return TELEPHONY_ERR_LOCAL_PTR_NULL;
236 }
237 return cellularCallConnectionPtr_->CombineConference(callInfo);
238 }
239
CarrierSeparateConference()240 int32_t CarrierCall::CarrierSeparateConference()
241 {
242 CellularCallInfo callInfo;
243 int32_t ret = PackCellularCallInfo(callInfo);
244 if (ret != TELEPHONY_SUCCESS) {
245 TELEPHONY_LOGW("PackCellularCallInfo failed!");
246 }
247 if (cellularCallConnectionPtr_ == nullptr) {
248 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
249 return TELEPHONY_ERR_LOCAL_PTR_NULL;
250 }
251 return cellularCallConnectionPtr_->SeparateConference(callInfo);
252 }
253
IsSupportConferenceable()254 int32_t CarrierCall::IsSupportConferenceable()
255 {
256 // emergency call not allowed to join conference
257 return GetEmergencyState() != true ? TELEPHONY_SUCCESS : CALL_ERR_EMERGENCY_UNSUPPORT_CONFERENCEABLE;
258 }
259
PackCellularCallInfo(CellularCallInfo & callInfo)260 int32_t CarrierCall::PackCellularCallInfo(CellularCallInfo &callInfo)
261 {
262 callInfo.callId = callId_;
263 callInfo.callType = callType_;
264 callInfo.videoState = static_cast<int32_t>(videoState_);
265 callInfo.index = index_;
266 callInfo.slotId = slotId_;
267 callInfo.accountId = slotId_;
268 if (memset_s(callInfo.phoneNum, kMaxNumberLen, 0, kMaxNumberLen) != EOK) {
269 TELEPHONY_LOGW("memset_s failed!");
270 return TELEPHONY_ERR_MEMSET_FAIL;
271 }
272 if (accountNumber_.length() > static_cast<size_t>(kMaxNumberLen)) {
273 TELEPHONY_LOGE("Number out of limit!");
274 return CALL_ERR_NUMBER_OUT_OF_RANGE;
275 }
276 if (memcpy_s(callInfo.phoneNum, kMaxNumberLen, accountNumber_.c_str(), accountNumber_.length()) != EOK) {
277 TELEPHONY_LOGW("memcpy_s failed!");
278 return TELEPHONY_ERR_MEMCPY_FAIL;
279 }
280 return TELEPHONY_SUCCESS;
281 }
282
StartDtmf(char str)283 int32_t CarrierCall::StartDtmf(char str)
284 {
285 CellularCallInfo callInfo;
286 int32_t ret = PackCellularCallInfo(callInfo);
287 if (ret != TELEPHONY_SUCCESS) {
288 TELEPHONY_LOGW("PackCellularCallInfo failed!");
289 }
290 if (cellularCallConnectionPtr_ == nullptr) {
291 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
292 return TELEPHONY_ERR_LOCAL_PTR_NULL;
293 }
294 return cellularCallConnectionPtr_->StartDtmf(str, callInfo);
295 }
296
StopDtmf()297 int32_t CarrierCall::StopDtmf()
298 {
299 CellularCallInfo callInfo;
300 int32_t ret = PackCellularCallInfo(callInfo);
301 if (ret != TELEPHONY_SUCCESS) {
302 TELEPHONY_LOGW("PackCellularCallInfo failed!");
303 }
304 if (cellularCallConnectionPtr_ == nullptr) {
305 TELEPHONY_LOGE("cellularCallConnectionPtr_ is nullptr!");
306 return TELEPHONY_ERR_LOCAL_PTR_NULL;
307 }
308 return cellularCallConnectionPtr_->StopDtmf(callInfo);
309 }
310 } // namespace Telephony
311 } // namespace OHOS
312