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 "ott_call.h"
17
18#include "call_manager_errors.h"
19#include "call_manager_hisysevent.h"
20#include "telephony_log_wrapper.h"
21
22#include "ott_call_connection.h"
23#include "ott_conference.h"
24
25namespace OHOS {
26namespace Telephony {
27OTTCall::OTTCall(DialParaInfo &info) : CallBase(info), ottCallConnectionPtr_(std::make_unique<OTTCallConnection>())
28{}
29
30OTTCall::OTTCall(DialParaInfo &info, AppExecFwk::PacMap &extras)
31    : CallBase(info, extras), ottCallConnectionPtr_(std::make_unique<OTTCallConnection>())
32{}
33
34OTTCall::~OTTCall() {}
35
36int32_t OTTCall::DialingProcess()
37{
38    int32_t ret = DialCallBase();
39    if (ret != TELEPHONY_SUCCESS) {
40        HangUpCall();
41    }
42    return ret;
43}
44
45int32_t OTTCall::AnswerCall(int32_t videoState)
46{
47    int32_t ret = AnswerCallBase();
48    if (ret != TELEPHONY_SUCCESS) {
49        TELEPHONY_LOGE("answer call failed!");
50        CallManagerHisysevent::WriteAnswerCallFaultEvent(
51            INVALID_PARAMETER, INVALID_PARAMETER, videoState, ret, "the device is currently not ringing");
52        return CALL_ERR_ANSWER_FAILED;
53    }
54    OttCallRequestInfo requestInfo;
55    ret = PackOttCallRequestInfo(requestInfo);
56    if (ret != TELEPHONY_SUCCESS) {
57        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
58        CallManagerHisysevent::WriteAnswerCallFaultEvent(
59            INVALID_PARAMETER, INVALID_PARAMETER, videoState, ret, "PackOttCallRequestInfo failed");
60        return CALL_ERR_ANSWER_FAILED;
61    }
62    if (ottCallConnectionPtr_ == nullptr) {
63        TELEPHONY_LOGE("ottCallConnectionPtr_ is nullptr!");
64        return TELEPHONY_ERR_LOCAL_PTR_NULL;
65    }
66    ret = ottCallConnectionPtr_->Answer(requestInfo);
67    if (ret != TELEPHONY_SUCCESS) {
68        TELEPHONY_LOGE("answer call failed!");
69        return CALL_ERR_ANSWER_FAILED;
70    }
71    return TELEPHONY_SUCCESS;
72}
73
74int32_t OTTCall::RejectCall()
75{
76    int32_t ret = RejectCallBase();
77    if (ret != TELEPHONY_SUCCESS) {
78        return ret;
79    }
80    OttCallRequestInfo requestInfo;
81    ret = PackOttCallRequestInfo(requestInfo);
82    if (ret != TELEPHONY_SUCCESS) {
83        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
84        CallManagerHisysevent::WriteHangUpFaultEvent(
85            INVALID_PARAMETER, INVALID_PARAMETER, ret, "Reject PackOttCallRequestInfo failed");
86        return CALL_ERR_REJECT_FAILED;
87    }
88    ret = ottCallConnectionPtr_->Reject(requestInfo);
89    if (ret != TELEPHONY_SUCCESS) {
90        TELEPHONY_LOGE("reject call failed!");
91        return CALL_ERR_REJECT_FAILED;
92    }
93    return TELEPHONY_SUCCESS;
94}
95
96int32_t OTTCall::HangUpCall()
97{
98    OttCallRequestInfo requestInfo;
99    int32_t ret = PackOttCallRequestInfo(requestInfo);
100    if (ret != TELEPHONY_SUCCESS) {
101        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
102        CallManagerHisysevent::WriteHangUpFaultEvent(
103            INVALID_PARAMETER, INVALID_PARAMETER, ret, "HangUp PackOttCallRequestInfo failed");
104        return CALL_ERR_HANGUP_FAILED;
105    }
106    ret = ottCallConnectionPtr_->HangUp(requestInfo);
107    if (ret != TELEPHONY_SUCCESS) {
108        TELEPHONY_LOGE("hangUp call failed!");
109        return CALL_ERR_HANGUP_FAILED;
110    }
111    return TELEPHONY_SUCCESS;
112}
113
114int32_t OTTCall::HoldCall()
115{
116    OttCallRequestInfo requestInfo;
117    int32_t ret = PackOttCallRequestInfo(requestInfo);
118    if (ret != TELEPHONY_SUCCESS) {
119        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
120        return CALL_ERR_HOLD_FAILED;
121    }
122    ret = ottCallConnectionPtr_->HoldCall(requestInfo);
123    if (ret != TELEPHONY_SUCCESS) {
124        TELEPHONY_LOGE("holdCall call failed!");
125        return CALL_ERR_HOLD_FAILED;
126    }
127    return TELEPHONY_SUCCESS;
128}
129
130int32_t OTTCall::UnHoldCall()
131{
132    OttCallRequestInfo requestInfo;
133    int32_t ret = PackOttCallRequestInfo(requestInfo);
134    if (ret != TELEPHONY_SUCCESS) {
135        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
136        return CALL_ERR_UNHOLD_FAILED;
137    }
138    ret = ottCallConnectionPtr_->UnHoldCall(requestInfo);
139    if (ret != TELEPHONY_SUCCESS) {
140        TELEPHONY_LOGE("unHoldCall call failed!");
141        return CALL_ERR_UNHOLD_FAILED;
142    }
143    return TELEPHONY_SUCCESS;
144}
145
146int32_t OTTCall::SwitchCall()
147{
148    OttCallRequestInfo requestInfo;
149    int32_t ret = PackOttCallRequestInfo(requestInfo);
150    if (ret != TELEPHONY_SUCCESS) {
151        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
152        return CALL_ERR_UNHOLD_FAILED;
153    }
154    ret = ottCallConnectionPtr_->SwitchCall(requestInfo);
155    if (ret != TELEPHONY_SUCCESS) {
156        TELEPHONY_LOGE("switchCall call failed!");
157        return CALL_ERR_UNHOLD_FAILED;
158    }
159    return TELEPHONY_SUCCESS;
160}
161
162void OTTCall::GetCallAttributeInfo(CallAttributeInfo &info)
163{
164    GetCallAttributeBaseInfo(info);
165}
166
167bool OTTCall::GetEmergencyState()
168{
169    return false;
170}
171
172int32_t OTTCall::StartDtmf(char str)
173{
174    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
175}
176
177int32_t OTTCall::StopDtmf()
178{
179    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
180}
181
182int32_t OTTCall::PostDialProceed(bool proceed)
183{
184    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
185}
186
187int32_t OTTCall::GetSlotId()
188{
189    return CALL_ERR_ILLEGAL_CALL_OPERATION;
190}
191
192int32_t OTTCall::CombineConference()
193{
194    int32_t ret = DelayedSingleton<OttConference>::GetInstance()->SetMainCall(GetCallID());
195    if (ret != TELEPHONY_SUCCESS) {
196        TELEPHONY_LOGE("SetMainCall failed,  error%{public}d", ret);
197        return ret;
198    }
199    OttCallRequestInfo requestInfo;
200    ret = PackOttCallRequestInfo(requestInfo);
201    if (ret != TELEPHONY_SUCCESS) {
202        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
203        return ret;
204    }
205    if (ottCallConnectionPtr_ == nullptr) {
206        TELEPHONY_LOGE("ottCallConnectionPtr_ is nullptr!");
207        return TELEPHONY_ERR_LOCAL_PTR_NULL;
208    }
209    ConferenceState currentState = DelayedSingleton<OttConference>::GetInstance()->GetConferenceState();
210    if (currentState == ConferenceState::CONFERENCE_STATE_CREATING) {
211        TELEPHONY_LOGE("skip combine, a process of combine already exsists");
212        return TELEPHONY_SUCCESS;
213    }
214    DelayedSingleton<OttConference>::GetInstance()->SetConferenceState(ConferenceState::CONFERENCE_STATE_CREATING);
215    return ottCallConnectionPtr_->CombineConference(requestInfo);
216}
217
218void OTTCall::HandleCombineConferenceFailEvent()
219{
220    std::set<std::int32_t> subCallIdList = DelayedSingleton<OttConference>::GetInstance()->GetSubCallIdList();
221    if (subCallIdList.empty()) {
222        DelayedSingleton<OttConference>::GetInstance()->SetMainCall(ERR_ID);
223    } else {
224        DelayedSingleton<OttConference>::GetInstance()->SetMainCall(*subCallIdList.begin());
225    }
226    ConferenceState oldState = DelayedSingleton<OttConference>::GetInstance()->GetOldConferenceState();
227    DelayedSingleton<OttConference>::GetInstance()->SetConferenceState(oldState);
228}
229
230int32_t OTTCall::CanCombineConference()
231{
232    int32_t ret = IsSupportConferenceable();
233    if (ret != TELEPHONY_SUCCESS) {
234        TELEPHONY_LOGE("call unsupported conference, error%{public}d", ret);
235        return ret;
236    }
237    return DelayedSingleton<OttConference>::GetInstance()->CanCombineConference();
238}
239
240int32_t OTTCall::SeparateConference()
241{
242    OttCallRequestInfo requestInfo;
243    int32_t ret = PackOttCallRequestInfo(requestInfo);
244    if (ret != TELEPHONY_SUCCESS) {
245        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
246        return ret;
247    }
248    if (ottCallConnectionPtr_ == nullptr) {
249        TELEPHONY_LOGE("ottCallConnectionPtr_ is nullptr!");
250        return TELEPHONY_ERR_LOCAL_PTR_NULL;
251    }
252    return ottCallConnectionPtr_->SeparateConference(requestInfo);
253}
254
255int32_t OTTCall::KickOutFromConference()
256{
257    OttCallRequestInfo requestInfo;
258    int32_t ret = PackOttCallRequestInfo(requestInfo);
259    if (ret != TELEPHONY_SUCCESS) {
260        TELEPHONY_LOGE("PackOttCallRequestInfo failed, error%{public}d", ret);
261        return ret;
262    }
263    if (ottCallConnectionPtr_ == nullptr) {
264        TELEPHONY_LOGE("ottCallConnectionPtr_ is nullptr!");
265        return TELEPHONY_ERR_LOCAL_PTR_NULL;
266    }
267    return ottCallConnectionPtr_->KickOutFromConference(requestInfo);
268}
269
270int32_t OTTCall::CanSeparateConference()
271{
272    return DelayedSingleton<OttConference>::GetInstance()->CanSeparateConference();
273}
274
275int32_t OTTCall::CanKickOutFromConference()
276{
277    return DelayedSingleton<OttConference>::GetInstance()->CanKickOutFromConference();
278}
279
280int32_t OTTCall::LaunchConference()
281{
282    int32_t ret = DelayedSingleton<OttConference>::GetInstance()->JoinToConference(GetCallID());
283    if (ret == TELEPHONY_SUCCESS) {
284        SetTelConferenceState(TelConferenceState::TEL_CONFERENCE_ACTIVE);
285    }
286    return ret;
287}
288
289int32_t OTTCall::ExitConference()
290{
291    int32_t ret = DelayedSingleton<OttConference>::GetInstance()->LeaveFromConference(GetCallID());
292    if (ret == TELEPHONY_SUCCESS) {
293        SetTelConferenceState(TelConferenceState::TEL_CONFERENCE_IDLE);
294    }
295    return ret;
296}
297
298int32_t OTTCall::HoldConference()
299{
300    int32_t ret = DelayedSingleton<OttConference>::GetInstance()->HoldConference(GetCallID());
301    if (ret == TELEPHONY_SUCCESS) {
302        SetTelConferenceState(TelConferenceState::TEL_CONFERENCE_HOLDING);
303    }
304    return ret;
305}
306
307int32_t OTTCall::GetMainCallId(int32_t &mainCallId)
308{
309    mainCallId = DelayedSingleton<OttConference>::GetInstance()->GetMainCall();
310    return TELEPHONY_SUCCESS;
311}
312
313int32_t OTTCall::GetSubCallIdList(std::vector<std::u16string> &callIdList)
314{
315    return DelayedSingleton<OttConference>::GetInstance()->GetSubCallIdList(GetCallID(), callIdList);
316}
317
318int32_t OTTCall::GetCallIdListForConference(std::vector<std::u16string> &callIdList)
319{
320    return DelayedSingleton<OttConference>::GetInstance()->GetCallIdListForConference(GetCallID(), callIdList);
321}
322
323int32_t OTTCall::IsSupportConferenceable()
324{
325#ifdef ABILIT_CONFIG_SUPPORT
326    bool ottSupport = GetOttConfig(OTT_SUPPORT_CONFERENCE);
327    if (!ottSupport) {
328        return TELEPHONY_CONFERENCE_OTT_NOT_SUPPORT;
329    }
330    if (isVideoCall()) {
331        ottSupport = GetOTTConfig(OTT_VIDEO_SUPPORT_CONFERENCE)
332    }
333    if (!ottSupport) {
334        return TELEPHONY_CONFERENCE_VIDEO_CALL_NOT_SUPPORT;
335    }
336#endif
337    return TELEPHONY_SUCCESS;
338}
339
340int32_t OTTCall::SendUpdateCallMediaModeRequest(ImsCallMode mode)
341{
342    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
343}
344
345int32_t OTTCall::InitVideoCall()
346{
347    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
348}
349
350int32_t OTTCall::RecieveUpdateCallMediaModeRequest(CallModeReportInfo &imsCallModeInfo)
351{
352    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
353}
354
355int32_t OTTCall::SendUpdateCallMediaModeResponse(ImsCallMode mode)
356{
357    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
358}
359
360int32_t OTTCall::ReceiveUpdateCallMediaModeResponse(CallModeReportInfo &response)
361{
362    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
363}
364
365int32_t OTTCall::UpdateImsCallMode(ImsCallMode mode)
366{
367    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
368}
369
370int32_t OTTCall::ReportImsCallModeInfo(CallMediaModeInfo &response)
371{
372    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
373}
374
375int32_t OTTCall::ControlCamera(std::string &cameraId, int32_t callingUid, int32_t callingPid)
376{
377    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
378}
379
380int32_t OTTCall::SetPreviewWindow(std::string &surfaceId, sptr<Surface> surface)
381{
382    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
383}
384
385int32_t OTTCall::SetDisplayWindow(std::string &surfaceId, sptr<Surface> surface)
386{
387    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
388}
389
390int32_t OTTCall::SetPausePicture(std::string &path)
391{
392    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
393}
394
395int32_t OTTCall::SetDeviceDirection(int32_t rotation)
396{
397    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
398}
399
400int32_t OTTCall::CancelCallUpgrade()
401{
402    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
403}
404
405int32_t OTTCall::RequestCameraCapabilities()
406{
407    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
408}
409
410int32_t OTTCall::SetMute(int32_t mute, int32_t slotId)
411{
412    return CALL_ERR_FUNCTION_NOT_SUPPORTED;
413}
414
415int32_t OTTCall::PackOttCallRequestInfo(OttCallRequestInfo &requestInfo)
416{
417    if (accountNumber_.length() > static_cast<size_t>(kMaxNumberLen)) {
418        TELEPHONY_LOGE("Number out of limit!");
419        return CALL_ERR_NUMBER_OUT_OF_RANGE;
420    }
421    if (memcpy_s(requestInfo.phoneNum, kMaxNumberLen, accountNumber_.c_str(), accountNumber_.length()) != EOK) {
422        TELEPHONY_LOGW("memset_s failed!");
423        return TELEPHONY_ERR_MEMSET_FAIL;
424    }
425    if (bundleName_.length() > static_cast<size_t>(kMaxBundleNameLen)) {
426        TELEPHONY_LOGE("Number out of limit!");
427        return CALL_ERR_NUMBER_OUT_OF_RANGE;
428    }
429    if (memcpy_s(requestInfo.bundleName, kMaxBundleNameLen, bundleName_.c_str(), bundleName_.length()) != EOK) {
430        TELEPHONY_LOGW("memset_s failed!");
431        return TELEPHONY_ERR_MEMSET_FAIL;
432    }
433    requestInfo.videoState = videoState_;
434    return TELEPHONY_SUCCESS;
435}
436} // namespace Telephony
437} // namespace OHOS
438