1 /*
2  * Copyright (C) 2021-2022 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 "cellular_call_service.h"
17 
18 #include "cellular_call_callback.h"
19 #include "cellular_call_dump_helper.h"
20 #include "cellular_call_hisysevent.h"
21 #include "common_event.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "emergency_utils.h"
25 #include "ims_call_client.h"
26 #include "ims_video_call_control.h"
27 #include "module_service_utils.h"
28 #include "radio_event.h"
29 #include "satellite_call_client.h"
30 #include "securec.h"
31 #include "string_ex.h"
32 #include "system_ability_definition.h"
33 
34 namespace OHOS {
35 namespace Telephony {
36 const uint32_t CONNECT_MAX_TRY_COUNT = 20;
37 const uint32_t CONNECT_CORE_SERVICE_WAIT_TIME = 2000; // ms
38 const uint32_t TELEPHONY_SATELLITE_SYS_ABILITY_ID = 4012;
39 bool g_registerResult =
40     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<CellularCallService>::GetInstance().get());
41 
CellularCallService()42 CellularCallService::CellularCallService() : SystemAbility(TELEPHONY_CELLULAR_CALL_SYS_ABILITY_ID, true)
43 {
44     state_ = ServiceRunningState::STATE_STOPPED;
45 }
46 
~CellularCallService()47 CellularCallService::~CellularCallService()
48 {
49     state_ = ServiceRunningState::STATE_STOPPED;
50     if (statusChangeListener_ != nullptr) {
51         statusChangeListener_.clear();
52         statusChangeListener_ = nullptr;
53     }
54 }
55 
Init()56 bool CellularCallService::Init()
57 {
58     TELEPHONY_LOGD("CellularCallService::Init start");
59     CreateHandler();
60     SendEventRegisterHandler();
61     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62     callManagerListener_ = new (std::nothrow) SystemAbilityStatusChangeListener();
63     if (samgrProxy == nullptr || callManagerListener_ == nullptr) {
64         TELEPHONY_LOGE("samgrProxy or callManagerListener_ is nullptr");
65     } else {
66         int32_t ret = samgrProxy->SubscribeSystemAbility(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID, callManagerListener_);
67         TELEPHONY_LOGI("SubscribeSystemAbility TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID result:%{public}d", ret);
68     }
69     // connect ims_service
70     DelayedSingleton<ImsCallClient>::GetInstance()->Init();
71     TELEPHONY_LOGD("CellularCallService::Init, init success");
72     return true;
73 }
74 
OnStart()75 void CellularCallService::OnStart()
76 {
77     TELEPHONY_LOGD("CellularCallService OnStart");
78     bindTime_ =
79         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
80             .count();
81     if (state_ == ServiceRunningState::STATE_RUNNING) {
82         TELEPHONY_LOGE("CellularCallService::OnStart return, has already started.");
83         return;
84     }
85     if (!Init()) {
86         TELEPHONY_LOGE("CellularCallService::OnStart return, failed to init service.");
87         return;
88     }
89     state_ = ServiceRunningState::STATE_RUNNING;
90     bool ret = Publish(DelayedSingleton<CellularCallService>::GetInstance().get());
91     if (!ret) {
92         TELEPHONY_LOGE("CellularCallService::OnStart Publish failed!");
93     }
94     endTime_ =
95         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
96             .count();
97     TELEPHONY_LOGD("CellularCallService start success.");
98 }
99 
OnStop()100 void CellularCallService::OnStop()
101 {
102     TELEPHONY_LOGD("CellularCallService stop service");
103     DelayedSingleton<ImsCallClient>::GetInstance()->UnInit();
104     state_ = ServiceRunningState::STATE_STOPPED;
105     HandlerResetUnRegister();
106 }
107 
RegisterHandler()108 void CellularCallService::RegisterHandler()
109 {
110     TELEPHONY_LOGI("connect core service Register Handler start");
111     networkSearchCallBack_ = (std::make_unique<CellularCallCallback>()).release();
112     for (uint32_t i = 0; i < CONNECT_MAX_TRY_COUNT; i++) {
113         std::this_thread::sleep_for(std::chrono::milliseconds(CONNECT_CORE_SERVICE_WAIT_TIME));
114         if (CoreManagerInner::GetInstance().IsInitFinished()) {
115             TELEPHONY_LOGI("connect core service Register Handler start");
116             RegisterCoreServiceHandler();
117             CoreManagerInner::GetInstance().RegisterCellularCallObject(networkSearchCallBack_);
118             break;
119         }
120         TELEPHONY_LOGW("connect core service Register Handler null or not init");
121     }
122     TELEPHONY_LOGI("connect core service Register Handler end");
123 }
124 
CreateHandler()125 void CellularCallService::CreateHandler()
126 {
127     ModuleServiceUtils obtain;
128     std::vector<int32_t> slotVector = obtain.GetSlotInfo();
129     EventFwk::MatchingSkills matchingSkills;
130     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_OPERATOR_CONFIG_CHANGED);
131     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED);
132     EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
133     subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
134     for (const auto &it : slotVector) {
135         auto handler = std::make_shared<CellularCallHandler>(subscriberInfo);
136         TELEPHONY_LOGI("setSlotId:%{public}d", it);
137         handler->SetSlotId(it);
138         handler->RegisterImsCallCallbackHandler();
139         {
140             std::unique_lock<std::mutex> lock(handlerMapMutex_);
141             handlerMap_.insert(std::make_pair(it, handler));
142         }
143         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
144         statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(handler);
145         if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
146             TELEPHONY_LOGE("samgrProxy or statusChangeListener_ is nullptr");
147         } else {
148             int32_t retSubCommnetEvent =
149                 samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
150             TELEPHONY_LOGI("SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result:%{public}d", retSubCommnetEvent);
151             int32_t retSubSateEvent =
152                 samgrProxy->SubscribeSystemAbility(TELEPHONY_SATELLITE_SYS_ABILITY_ID, statusChangeListener_);
153             TELEPHONY_LOGI(
154                 "SubscribeSystemAbility TELEPHONY_SATELLITE_SYS_ABILITY_ID result:%{public}d", retSubSateEvent);
155         }
156     }
157 }
158 
HandlerResetUnRegister()159 void CellularCallService::HandlerResetUnRegister()
160 {
161     TELEPHONY_LOGI("HandlerResetUnRegister");
162     std::unique_lock<std::mutex> lock(handlerMapMutex_);
163     for (const auto &it : handlerMap_) {
164         int32_t slot = it.first;
165         auto handler = it.second;
166         if (handler != nullptr) {
167             handler.reset();
168         }
169         CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
170         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_AVAIL);
171         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_NOT_AVAIL);
172         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_SIM_RECORDS_LOADED);
173         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_SIM_ACCOUNT_LOADED);
174         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_STATUS_INFO);
175         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_USSD_NOTICE);
176         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_SS_NOTICE);
177         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_RINGBACK_VOICE);
178         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_EMERGENCY_NUMBER_REPORT);
179         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_SRVCC_STATUS);
180         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_RSRVCC_STATUS);
181         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_RESIDENT_NETWORK_CHANGE);
182         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_RIL_ADAPTER_HOST_DIED);
183         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_FACTORY_RESET);
184 #ifdef CALL_MANAGER_AUTO_START_OPTIMIZE
185         coreInner.UnRegisterCoreNotify(slot, handler, RadioEvent::RADIO_STATE_CHANGED);
186 #endif
187         if (GetCsControl(slot) != nullptr) {
188             GetCsControl(slot)->ReleaseAllConnection();
189         }
190         if (GetImsControl(slot) != nullptr) {
191             GetImsControl(slot)->ReleaseAllConnection();
192         }
193     }
194 }
195 
RegisterCoreServiceHandler()196 void CellularCallService::RegisterCoreServiceHandler()
197 {
198     TELEPHONY_LOGI("RegisterCoreServiceHandle");
199     std::unique_lock<std::mutex> lock(handlerMapMutex_, std::defer_lock);
200     lock.lock();
201     for (const auto &it : handlerMap_) {
202         int32_t slot = it.first;
203         auto handler = it.second;
204         if (handler != nullptr) {
205             CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
206             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_AVAIL, nullptr);
207             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_NOT_AVAIL, nullptr);
208             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_SIM_STATE_CHANGE, nullptr);
209             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_SIM_RECORDS_LOADED, nullptr);
210             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_SIM_ACCOUNT_LOADED, nullptr);
211             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_STATUS_INFO, nullptr);
212             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_USSD_NOTICE, nullptr);
213             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_SS_NOTICE, nullptr);
214             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_EMERGENCY_NUMBER_REPORT, nullptr);
215             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_RINGBACK_VOICE, nullptr);
216             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_SRVCC_STATUS, nullptr);
217             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_CALL_RSRVCC_STATUS, nullptr);
218             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_RESIDENT_NETWORK_CHANGE, nullptr);
219             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_RIL_ADAPTER_HOST_DIED, nullptr);
220             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_FACTORY_RESET, nullptr);
221             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_NV_REFRESH_FINISHED, nullptr);
222 #ifdef CALL_MANAGER_AUTO_START_OPTIMIZE
223             coreInner.RegisterCoreNotify(slot, handler, RadioEvent::RADIO_STATE_CHANGED, nullptr);
224             coreInner.GetRadioState(slot, RadioEvent::RADIO_GET_STATUS, handler);
225 #endif
226         }
227         CellularCallConfig config;
228         config.InitModeActive();
229         if (config.GetDomainPreferenceMode(slot) != TELEPHONY_SUCCESS) {
230             TELEPHONY_LOGW("RegisterCoreServiceHandler, GetDomainPreferenceMode request fail");
231         }
232         lock.unlock();
233         if (config.GetEmergencyCallList(it.first) != TELEPHONY_SUCCESS) {
234             TELEPHONY_LOGW("RegisterCoreServiceHandler, GetEmergencyCallList request fail");
235         }
236         lock.lock();
237     }
238 }
239 
SendEventRegisterHandler()240 void CellularCallService::SendEventRegisterHandler()
241 {
242     int64_t delayTime = 1000;
243     int32_t slot = DEFAULT_SIM_SLOT_ID;
244     std::unique_lock<std::mutex> lock(handlerMapMutex_);
245     auto handler = handlerMap_[slot];
246     if (handler == nullptr) {
247         TELEPHONY_LOGE("SendEventRegisterHandler return, handler is nullptr");
248         return;
249     }
250     handler->SendEvent(handler->REGISTER_HANDLER_ID, delayTime, CellularCallHandler::Priority::HIGH);
251 }
252 
Dump(int32_t fd, const std::vector<std::u16string> &args)253 int32_t CellularCallService::Dump(int32_t fd, const std::vector<std::u16string> &args)
254 {
255     if (fd < 0) {
256         TELEPHONY_LOGE("dump fd invalid");
257         return TELEPHONY_ERR_FAIL;
258     }
259     std::vector<std::string> argsInStr;
260     for (const auto &arg : args) {
261         argsInStr.emplace_back(Str16ToStr8(arg));
262     }
263     std::string result;
264     CellularCallDumpHelper dumpHelper;
265     if (dumpHelper.Dump(argsInStr, result)) {
266         int32_t ret = dprintf(fd, "%s", result.c_str());
267         if (ret < 0) {
268             TELEPHONY_LOGE("dprintf to dump fd failed");
269             return TELEPHONY_ERR_FAIL;
270         }
271         return TELEPHONY_SUCCESS;
272     }
273     TELEPHONY_LOGW("dumpHelper failed");
274     return TELEPHONY_ERR_FAIL;
275 }
276 
GetServiceRunningState()277 int32_t CellularCallService::GetServiceRunningState()
278 {
279     return static_cast<int32_t>(state_);
280 }
281 
GetBindTime()282 std::string CellularCallService::GetBindTime()
283 {
284     std::ostringstream oss;
285     oss << bindTime_;
286     return oss.str();
287 }
288 
GetEndTime()289 std::string CellularCallService::GetEndTime()
290 {
291     std::ostringstream oss;
292     oss << endTime_;
293     return oss.str();
294 }
295 
GetSpendTime()296 std::string CellularCallService::GetSpendTime()
297 {
298     spendTime_ = endTime_ - bindTime_;
299     std::ostringstream oss;
300     oss << spendTime_;
301     return oss.str();
302 }
303 
Dial(const CellularCallInfo &callInfo)304 int32_t CellularCallService::Dial(const CellularCallInfo &callInfo)
305 {
306     if (!IsValidSlotId(callInfo.slotId)) {
307         TELEPHONY_LOGE("CellularCallService::Dial return, invalid slot id");
308         CellularCallHiSysEvent::WriteDialCallFaultEvent(callInfo.accountId, static_cast<int32_t>(callInfo.callType),
309             callInfo.videoState, CALL_ERR_INVALID_SLOT_ID, "invalid slot id");
310         return CALL_ERR_INVALID_SLOT_ID;
311     }
312     if (srvccState_ == SrvccState::STARTED) {
313         CellularCallHiSysEvent::WriteDialCallFaultEvent(callInfo.accountId, static_cast<int32_t>(callInfo.callType),
314             callInfo.videoState, static_cast<int32_t>(CallErrorCode::CALL_ERROR_UNEXPECTED_SRVCC_STATE),
315             "srvccState_ is STARTED");
316         return TELEPHONY_ERR_FAIL;
317     }
318     bool isEcc = false;
319     IsEmergencyPhoneNumber(callInfo.slotId, callInfo.phoneNum, isEcc);
320     ModuleServiceUtils moduleServiceUtils;
321     bool satelliteStatusOn = moduleServiceUtils.GetSatelliteStatus();
322     if (satelliteStatusOn) {
323         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
324         if (satelliteControl == nullptr) {
325             TELEPHONY_LOGI("CellularCallService::Dial satelliteControl dial");
326             satelliteControl = std::make_shared<SatelliteControl>();
327             if (satelliteControl == nullptr) {
328                 TELEPHONY_LOGE("CellularCallService::Dial return, satelliteControl create fail");
329                 return TELEPHONY_ERR_LOCAL_PTR_NULL;
330             }
331             SetSatelliteControl(callInfo.slotId, satelliteControl);
332         }
333         return satelliteControl->Dial(callInfo, isEcc);
334     }
335     return DialNormalCall(callInfo, isEcc);
336 }
337 
DialNormalCall(const CellularCallInfo &callInfo, bool isEcc)338 int32_t CellularCallService::DialNormalCall(const CellularCallInfo &callInfo, bool isEcc)
339 {
340     bool useImsForEmergency = UseImsForEmergency(callInfo, isEcc);
341     if (IsNeedIms(callInfo.slotId) || useImsForEmergency) {
342         auto imsControl = GetImsControl(callInfo.slotId);
343         if (imsControl == nullptr) {
344             TELEPHONY_LOGI("CellularCallService::Dial ims dial");
345             imsControl = std::make_shared<IMSControl>();
346             if (imsControl == nullptr) {
347                 TELEPHONY_LOGE("CellularCallService::Dial return, imsControl create fail");
348                 return TELEPHONY_ERR_LOCAL_PTR_NULL;
349             }
350             SetImsControl(callInfo.slotId, imsControl);
351         }
352         return imsControl->Dial(callInfo, isEcc);
353     }
354 
355     auto csControl = GetCsControl(callInfo.slotId);
356     if (csControl == nullptr) {
357         csControl = std::make_shared<CSControl>();
358         if (csControl == nullptr) {
359             TELEPHONY_LOGE("CellularCallService::Dial return, csControl create fail");
360             return TELEPHONY_ERR_LOCAL_PTR_NULL;
361         }
362         SetCsControl(callInfo.slotId, csControl);
363     }
364     return csControl->Dial(callInfo, isEcc);
365 }
366 
HangUp(const CellularCallInfo &callInfo, CallSupplementType type)367 int32_t CellularCallService::HangUp(const CellularCallInfo &callInfo, CallSupplementType type)
368 {
369     DelayedSingleton<CellularCallHiSysEvent>::GetInstance()->SetCallParameterInfo(
370         callInfo.slotId, static_cast<int32_t>(callInfo.callType), callInfo.videoState);
371     if (!IsValidSlotId(callInfo.slotId)) {
372         TELEPHONY_LOGE("CellularCallService::HangUp return, invalid slot id");
373         CellularCallHiSysEvent::WriteHangUpFaultEvent(
374             callInfo.slotId, callInfo.callId, CALL_ERR_INVALID_SLOT_ID, "HangUp invalid slot id");
375         return CALL_ERR_INVALID_SLOT_ID;
376     }
377     if (srvccState_ == SrvccState::STARTED) {
378         CellularCallHiSysEvent::WriteHangUpFaultEvent(callInfo.slotId, callInfo.callId,
379             static_cast<int32_t>(CallErrorCode::CALL_ERROR_UNEXPECTED_SRVCC_STATE), "HangUp srvccState_ is STARTED");
380         return TELEPHONY_ERR_FAIL;
381     }
382     if (CallType::TYPE_SATELLITE == callInfo.callType) {
383         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
384         if (satelliteControl == nullptr) {
385             TELEPHONY_LOGE("CellularCallService::HangUp return, satelliteControl is nullptr");
386             CellularCallHiSysEvent::WriteHangUpFaultEvent(
387                 callInfo.slotId, callInfo.callId, TELEPHONY_ERR_LOCAL_PTR_NULL, "HangUp satelliteControl is nullptr");
388             HandleCellularControlException(callInfo);
389             return TELEPHONY_ERR_LOCAL_PTR_NULL;
390         }
391         return satelliteControl->HangUp(callInfo, type);
392     } else if (CallType::TYPE_CS == callInfo.callType) {
393         auto csControl = GetCsControl(callInfo.slotId);
394         if (csControl == nullptr) {
395             TELEPHONY_LOGE("CellularCallService::HangUp return, csControl is nullptr");
396             CellularCallHiSysEvent::WriteHangUpFaultEvent(
397                 callInfo.slotId, callInfo.callId, TELEPHONY_ERR_LOCAL_PTR_NULL, "HangUp csControl is nullptr");
398             HandleCellularControlException(callInfo);
399             return TELEPHONY_ERR_LOCAL_PTR_NULL;
400         }
401         return csControl->HangUp(callInfo, type);
402     } else if (CallType::TYPE_IMS == callInfo.callType) {
403         auto imsControl = GetImsControl(callInfo.slotId);
404         if (imsControl == nullptr) {
405             TELEPHONY_LOGE("CellularCallService::HangUp return, imsControl is nullptr");
406             CellularCallHiSysEvent::WriteHangUpFaultEvent(
407                 callInfo.slotId, callInfo.callId, TELEPHONY_ERR_LOCAL_PTR_NULL, "HangUp imsControl is nullptr");
408             HandleCellularControlException(callInfo);
409             return TELEPHONY_ERR_LOCAL_PTR_NULL;
410         }
411         return imsControl->HangUp(callInfo, type);
412     }
413     TELEPHONY_LOGE("CellularCallService::HangUp return, call type error.");
414     CellularCallHiSysEvent::WriteHangUpFaultEvent(
415         callInfo.slotId, callInfo.callId, TELEPHONY_ERR_ARGUMENT_INVALID, "HangUp call type error");
416     return TELEPHONY_ERR_ARGUMENT_INVALID;
417 }
418 
Reject(const CellularCallInfo &callInfo)419 int32_t CellularCallService::Reject(const CellularCallInfo &callInfo)
420 {
421     DelayedSingleton<CellularCallHiSysEvent>::GetInstance()->SetCallParameterInfo(
422         callInfo.slotId, static_cast<int32_t>(callInfo.callType), callInfo.videoState);
423     if (!IsValidSlotId(callInfo.slotId)) {
424         TELEPHONY_LOGE("CellularCallService::Reject return, invalid slot id");
425         CellularCallHiSysEvent::WriteHangUpFaultEvent(
426             callInfo.slotId, callInfo.callId, TELEPHONY_ERR_ARGUMENT_INVALID, "Reject call type error");
427         return CALL_ERR_INVALID_SLOT_ID;
428     }
429     if (srvccState_ == SrvccState::STARTED) {
430         CellularCallHiSysEvent::WriteHangUpFaultEvent(callInfo.slotId, callInfo.callId,
431             static_cast<int32_t>(CallErrorCode::CALL_ERROR_UNEXPECTED_SRVCC_STATE), "Reject srvccState_ is STARTED");
432         return TELEPHONY_ERR_FAIL;
433     }
434     if (CallType::TYPE_SATELLITE == callInfo.callType) {
435         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
436         if (satelliteControl == nullptr) {
437             TELEPHONY_LOGE("CellularCallService::Reject return, satelliteControl is nullptr");
438             CellularCallHiSysEvent::WriteHangUpFaultEvent(
439                 callInfo.slotId, callInfo.callId, TELEPHONY_ERR_LOCAL_PTR_NULL, "Reject satelliteControl is nullptr");
440             HandleCellularControlException(callInfo);
441             return TELEPHONY_ERR_LOCAL_PTR_NULL;
442         }
443         return satelliteControl->Reject(callInfo);
444     } else if (CallType::TYPE_CS == callInfo.callType) {
445         auto csControl = GetCsControl(callInfo.slotId);
446         if (csControl == nullptr) {
447             TELEPHONY_LOGE("CellularCallService::Reject return, csControl is nullptr");
448             CellularCallHiSysEvent::WriteHangUpFaultEvent(
449                 callInfo.slotId, callInfo.callId, TELEPHONY_ERR_LOCAL_PTR_NULL, "Reject csControl is nullptr");
450             HandleCellularControlException(callInfo);
451             return TELEPHONY_ERR_LOCAL_PTR_NULL;
452         }
453         return csControl->Reject(callInfo);
454     } else if (CallType::TYPE_IMS == callInfo.callType) {
455         auto imsControl = GetImsControl(callInfo.slotId);
456         if (imsControl == nullptr) {
457             TELEPHONY_LOGE("CellularCallService::Reject return, imsControl is nullptr");
458             CellularCallHiSysEvent::WriteHangUpFaultEvent(
459                 callInfo.slotId, callInfo.callId, TELEPHONY_ERR_LOCAL_PTR_NULL, "Reject imsControl is nullptr");
460             HandleCellularControlException(callInfo);
461             return TELEPHONY_ERR_LOCAL_PTR_NULL;
462         }
463         return imsControl->Reject(callInfo);
464     }
465     TELEPHONY_LOGE("CellularCallService::Reject return, call type error.");
466     CellularCallHiSysEvent::WriteHangUpFaultEvent(
467         callInfo.slotId, callInfo.callId, TELEPHONY_ERR_ARGUMENT_INVALID, "Reject call type error");
468     return TELEPHONY_ERR_ARGUMENT_INVALID;
469 }
470 
Answer(const CellularCallInfo &callInfo)471 int32_t CellularCallService::Answer(const CellularCallInfo &callInfo)
472 {
473     DelayedSingleton<CellularCallHiSysEvent>::GetInstance()->SetCallParameterInfo(
474         callInfo.slotId, static_cast<int32_t>(callInfo.callType), callInfo.videoState);
475 
476     if (!IsValidSlotId(callInfo.slotId)) {
477         TELEPHONY_LOGE("CellularCallService::Answer return, invalid slot id");
478         CellularCallHiSysEvent::WriteAnswerCallFaultEvent(
479             callInfo.slotId, callInfo.callId, callInfo.videoState, CALL_ERR_INVALID_SLOT_ID, "invalid slot id");
480         return CALL_ERR_INVALID_SLOT_ID;
481     }
482     if (srvccState_ == SrvccState::STARTED) {
483         CellularCallHiSysEvent::WriteAnswerCallFaultEvent(callInfo.slotId, callInfo.callId, callInfo.videoState,
484             static_cast<int32_t>(CallErrorCode::CALL_ERROR_UNEXPECTED_SRVCC_STATE), "srvccState_ is STARTED");
485         return TELEPHONY_ERR_FAIL;
486     }
487     if (CallType::TYPE_SATELLITE == callInfo.callType) {
488         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
489         if (satelliteControl == nullptr) {
490             TELEPHONY_LOGE("CellularCallService::Answer return, satelliteControl is nullptr");
491             CellularCallHiSysEvent::WriteAnswerCallFaultEvent(callInfo.slotId, callInfo.callId, callInfo.videoState,
492                 TELEPHONY_ERR_LOCAL_PTR_NULL, "satelliteControl is nullptr");
493             HandleCellularControlException(callInfo);
494             return TELEPHONY_ERR_LOCAL_PTR_NULL;
495         }
496         return satelliteControl->Answer(callInfo);
497     } else if (CallType::TYPE_CS == callInfo.callType) {
498         auto csControl = GetCsControl(callInfo.slotId);
499         if (csControl == nullptr) {
500             TELEPHONY_LOGE("CellularCallService::Answer return, csControl is nullptr");
501             CellularCallHiSysEvent::WriteAnswerCallFaultEvent(callInfo.slotId, callInfo.callId, callInfo.videoState,
502                 TELEPHONY_ERR_LOCAL_PTR_NULL, "csControl is nullptr");
503             HandleCellularControlException(callInfo);
504             return TELEPHONY_ERR_LOCAL_PTR_NULL;
505         }
506         return csControl->Answer(callInfo);
507     } else if (CallType::TYPE_IMS == callInfo.callType) {
508         auto imsControl = GetImsControl(callInfo.slotId);
509         if (imsControl == nullptr) {
510             TELEPHONY_LOGE("CellularCallService::Answer return, imsControl is nullptr");
511             CellularCallHiSysEvent::WriteAnswerCallFaultEvent(callInfo.slotId, callInfo.callId, callInfo.videoState,
512                 TELEPHONY_ERR_LOCAL_PTR_NULL, "imsControl is nullptr");
513             HandleCellularControlException(callInfo);
514             return TELEPHONY_ERR_LOCAL_PTR_NULL;
515         }
516         return imsControl->Answer(callInfo);
517     }
518     TELEPHONY_LOGE("CellularCallService::Answer return, call type error.");
519     CellularCallHiSysEvent::WriteAnswerCallFaultEvent(
520         callInfo.slotId, callInfo.callId, callInfo.videoState, TELEPHONY_ERR_LOCAL_PTR_NULL, "call type error");
521     return TELEPHONY_ERR_ARGUMENT_INVALID;
522 }
523 
RegisterCallManagerCallBack(const sptr<ICallStatusCallback> &callback)524 int32_t CellularCallService::RegisterCallManagerCallBack(const sptr<ICallStatusCallback> &callback)
525 {
526     if (DelayedSingleton<CellularCallRegister>::GetInstance() == nullptr) {
527         TELEPHONY_LOGE("CellularCallService::RegisterCallManagerCallBack return, instance is nullptr.");
528         return TELEPHONY_ERR_LOCAL_PTR_NULL;
529     }
530     TELEPHONY_LOGI("CellularCallService::RegisterCallManagerCallBack");
531     return DelayedSingleton<CellularCallRegister>::GetInstance()->RegisterCallManagerCallBack(callback);
532 }
533 
UnRegisterCallManagerCallBack()534 int32_t CellularCallService::UnRegisterCallManagerCallBack()
535 {
536     if (DelayedSingleton<CellularCallRegister>::GetInstance() == nullptr) {
537         TELEPHONY_LOGE("CellularCallService::UnRegisterCallManagerCallBack return, instance is nullptr.");
538         return TELEPHONY_ERR_LOCAL_PTR_NULL;
539     }
540     TELEPHONY_LOGI("CellularCallService::UnRegisterCallManagerCallBack");
541     return DelayedSingleton<CellularCallRegister>::GetInstance()->UnRegisterCallManagerCallBack();
542 }
543 
HoldCall(const CellularCallInfo &callInfo)544 int32_t CellularCallService::HoldCall(const CellularCallInfo &callInfo)
545 {
546     if (!IsValidSlotId(callInfo.slotId)) {
547         TELEPHONY_LOGE("CellularCallService::HoldCall return, invalid slot id");
548         return CALL_ERR_INVALID_SLOT_ID;
549     }
550     if (srvccState_ == SrvccState::STARTED) {
551         return TELEPHONY_ERR_FAIL;
552     }
553     if (CallType::TYPE_SATELLITE == callInfo.callType) {
554         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
555         if (satelliteControl == nullptr) {
556             TELEPHONY_LOGE("CellularCallService::HoldCall return, satelliteControl is nullptr");
557             return TELEPHONY_ERR_LOCAL_PTR_NULL;
558         }
559         return satelliteControl->HoldCall(callInfo.slotId);
560     } else if (CallType::TYPE_IMS == callInfo.callType) {
561         auto imsControl = GetImsControl(callInfo.slotId);
562         if (imsControl == nullptr) {
563             TELEPHONY_LOGE("CellularCallService::HoldCall return, imsControl is nullptr");
564             return TELEPHONY_ERR_LOCAL_PTR_NULL;
565         }
566         return imsControl->HoldCall(callInfo.slotId);
567     } else if (CallType::TYPE_CS == callInfo.callType) {
568         auto csControl = GetCsControl(callInfo.slotId);
569         if (csControl == nullptr) {
570             TELEPHONY_LOGE("CellularCallService::HoldCall return, csControl is nullptr");
571             return TELEPHONY_ERR_LOCAL_PTR_NULL;
572         }
573         return csControl->HoldCall(callInfo.slotId);
574     }
575     TELEPHONY_LOGE("CellularCallService::HoldCall return, call type error.");
576     return TELEPHONY_ERR_ARGUMENT_INVALID;
577 }
578 
UnHoldCall(const CellularCallInfo &callInfo)579 int32_t CellularCallService::UnHoldCall(const CellularCallInfo &callInfo)
580 {
581     if (!IsValidSlotId(callInfo.slotId)) {
582         TELEPHONY_LOGE("CellularCallService::UnHoldCall return, invalid slot id");
583         return CALL_ERR_INVALID_SLOT_ID;
584     }
585     if (srvccState_ == SrvccState::STARTED) {
586         return TELEPHONY_ERR_FAIL;
587     }
588     if (CallType::TYPE_SATELLITE == callInfo.callType) {
589         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
590         if (satelliteControl == nullptr) {
591             TELEPHONY_LOGE("CellularCallService::HoldCall return, satelliteControl is nullptr");
592             return TELEPHONY_ERR_LOCAL_PTR_NULL;
593         }
594         return satelliteControl->UnHoldCall(callInfo.slotId);
595     } else if (CallType::TYPE_IMS == callInfo.callType) {
596         auto imsControl = GetImsControl(callInfo.slotId);
597         if (imsControl == nullptr) {
598             TELEPHONY_LOGE("CellularCallService::UnHoldCall return, imsControl is nullptr");
599             return TELEPHONY_ERR_LOCAL_PTR_NULL;
600         }
601         return imsControl->UnHoldCall(callInfo.slotId);
602     } else if (CallType::TYPE_CS == callInfo.callType) {
603         auto csControl = GetCsControl(callInfo.slotId);
604         if (csControl == nullptr) {
605             TELEPHONY_LOGE("CellularCallService::UnHoldCall return, csControl is nullptr");
606             return TELEPHONY_ERR_LOCAL_PTR_NULL;
607         }
608         return csControl->UnHoldCall(callInfo.slotId);
609     }
610     TELEPHONY_LOGE("CellularCallService::UnHoldCall return, call type error.");
611     return TELEPHONY_ERR_ARGUMENT_INVALID;
612 }
613 
SwitchCall(const CellularCallInfo &callInfo)614 int32_t CellularCallService::SwitchCall(const CellularCallInfo &callInfo)
615 {
616     if (!IsValidSlotId(callInfo.slotId)) {
617         TELEPHONY_LOGE("CellularCallService::SwitchCall return, invalid slot id");
618         return CALL_ERR_INVALID_SLOT_ID;
619     }
620     if (srvccState_ == SrvccState::STARTED) {
621         return TELEPHONY_ERR_FAIL;
622     }
623     if (CallType::TYPE_SATELLITE == callInfo.callType) {
624         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
625         if (satelliteControl == nullptr) {
626             TELEPHONY_LOGE("CellularCallService::HoldCall return, satelliteControl is nullptr");
627             return TELEPHONY_ERR_LOCAL_PTR_NULL;
628         }
629         return satelliteControl->UnHoldCall(callInfo.slotId);
630     } else if (CallType::TYPE_IMS == callInfo.callType) {
631         auto imsControl = GetImsControl(callInfo.slotId);
632         if (imsControl == nullptr) {
633             TELEPHONY_LOGE("CellularCallService::SwitchCall return, imsControl is nullptr");
634             return TELEPHONY_ERR_LOCAL_PTR_NULL;
635         }
636         return imsControl->SwitchCall(callInfo.slotId);
637     } else if (CallType::TYPE_CS == callInfo.callType) {
638         auto csControl = GetCsControl(callInfo.slotId);
639         if (csControl == nullptr) {
640             TELEPHONY_LOGE("CellularCallService::SwitchCall return, csControl is nullptr");
641             return TELEPHONY_ERR_LOCAL_PTR_NULL;
642         }
643         return csControl->SwitchCall(callInfo.slotId);
644     }
645     TELEPHONY_LOGE("CellularCallService::SwitchCall return, call type error.");
646     return TELEPHONY_ERR_ARGUMENT_INVALID;
647 }
648 
CombineConference(const CellularCallInfo &callInfo)649 int32_t CellularCallService::CombineConference(const CellularCallInfo &callInfo)
650 {
651     if (!IsValidSlotId(callInfo.slotId)) {
652         TELEPHONY_LOGE("CellularCallService::CombineConference return, invalid slot id");
653         return CALL_ERR_INVALID_SLOT_ID;
654     }
655     if (srvccState_ == SrvccState::STARTED) {
656         return TELEPHONY_ERR_FAIL;
657     }
658     if (CallType::TYPE_IMS == callInfo.callType) {
659         auto imsControl = GetImsControl(callInfo.slotId);
660         if (imsControl == nullptr) {
661             TELEPHONY_LOGE("CellularCallService::CombineConference return, imsControl is nullptr");
662             return TELEPHONY_ERR_LOCAL_PTR_NULL;
663         }
664         return imsControl->CombineConference(callInfo.slotId);
665     } else if (CallType::TYPE_CS == callInfo.callType) {
666         auto csControl = GetCsControl(callInfo.slotId);
667         if (csControl == nullptr) {
668             TELEPHONY_LOGE("CellularCallService::CombineConference return, csControl is nullptr");
669             return TELEPHONY_ERR_LOCAL_PTR_NULL;
670         }
671         return csControl->CombineConference(callInfo.slotId);
672     }
673     TELEPHONY_LOGE("CellularCallService::CombineConference return, call type error.");
674     return TELEPHONY_ERR_ARGUMENT_INVALID;
675 }
676 
SeparateConference(const CellularCallInfo &callInfo)677 int32_t CellularCallService::SeparateConference(const CellularCallInfo &callInfo)
678 {
679     if (!IsValidSlotId(callInfo.slotId)) {
680         TELEPHONY_LOGE("CellularCallService::SeparateConference return, invalid slot id");
681         return CALL_ERR_INVALID_SLOT_ID;
682     }
683     if (CallType::TYPE_CS == callInfo.callType) {
684         auto csControl = GetCsControl(callInfo.slotId);
685         if (csControl == nullptr) {
686             TELEPHONY_LOGE("CellularCallService::SeparateConference return, csControl is nullptr");
687             return TELEPHONY_ERR_LOCAL_PTR_NULL;
688         }
689         return csControl->SeparateConference(callInfo.slotId, callInfo.phoneNum, callInfo.index);
690     }
691     TELEPHONY_LOGE("CellularCallService::SeparateConference return, call type error.");
692     return TELEPHONY_ERR_ARGUMENT_INVALID;
693 }
694 
InviteToConference(int32_t slotId, const std::vector<std::string> &numberList)695 int32_t CellularCallService::InviteToConference(int32_t slotId, const std::vector<std::string> &numberList)
696 {
697     auto control = GetImsControl(slotId);
698     if (control == nullptr) {
699         TELEPHONY_LOGE("CellularCallService::InviteToConference return, control is nullptr");
700         return TELEPHONY_ERR_LOCAL_PTR_NULL;
701     }
702     return control->InviteToConference(slotId, numberList);
703 }
704 
KickOutFromConference(const CellularCallInfo &callInfo)705 int32_t CellularCallService::KickOutFromConference(const CellularCallInfo &callInfo)
706 {
707     if (!IsValidSlotId(callInfo.slotId)) {
708         TELEPHONY_LOGE("CellularCallService::KickOutFromConference return, invalid slot id");
709         return CALL_ERR_INVALID_SLOT_ID;
710     }
711     if (CallType::TYPE_IMS == callInfo.callType) {
712         auto imsControl = GetImsControl(callInfo.slotId);
713         if (imsControl == nullptr) {
714             TELEPHONY_LOGE("CellularCallService::KickOutFromConference return, imsControl is nullptr");
715             return TELEPHONY_ERR_LOCAL_PTR_NULL;
716         }
717         return imsControl->KickOutFromConference(callInfo.slotId, callInfo.phoneNum, callInfo.index);
718     } else if (CallType::TYPE_CS == callInfo.callType) {
719         auto csControl = GetCsControl(callInfo.slotId);
720         if (csControl == nullptr) {
721             TELEPHONY_LOGE("CellularCallService::KickOutFromConference return, csControl is nullptr");
722             return TELEPHONY_ERR_LOCAL_PTR_NULL;
723         }
724         return csControl->HangUp(callInfo, CallSupplementType::TYPE_DEFAULT);
725     }
726     TELEPHONY_LOGE("CellularCallService::KickOutFromConference return, call type error.");
727     return TELEPHONY_ERR_ARGUMENT_INVALID;
728 }
729 
HangUpAllConnection()730 int32_t CellularCallService::HangUpAllConnection()
731 {
732     ModuleServiceUtils obtain;
733     std::vector<int32_t> slotVector = obtain.GetSlotInfo();
734     for (const auto &it : slotVector) {
735         if (GetCsControl(it)) {
736             GetCsControl(it)->HangUpAllConnection(it);
737         }
738         if (GetImsControl(it)) {
739             GetImsControl(it)->HangUpAllConnection(it);
740         }
741     }
742     return TELEPHONY_SUCCESS;
743 }
744 
SetReadyToCall(int32_t slotId, int32_t callType, bool isReadyToCall)745 int32_t CellularCallService::SetReadyToCall(int32_t slotId, int32_t callType, bool isReadyToCall)
746 {
747     TELEPHONY_LOGI("slotId = %{public}d, callType = %{public}d, isReadyToCall = %{public}d",
748         slotId, callType, isReadyToCall);
749     if (!IsValidSlotId(slotId)) {
750         TELEPHONY_LOGE("CellularCallService::SetReadyToCall return, invalid slot id");
751         return CALL_ERR_INVALID_SLOT_ID;
752     }
753     if (GetCsControl(slotId) != nullptr) {
754         GetCsControl(slotId)->SetReadyToCall(slotId, isReadyToCall);
755     }
756     if (GetImsControl(slotId) != nullptr) {
757         GetImsControl(slotId)->SetReadyToCall(slotId, isReadyToCall);
758     }
759     if (GetSatelliteControl(slotId) != nullptr) {
760         GetSatelliteControl(slotId)->SetReadyToCall(slotId, isReadyToCall);
761     }
762     return TELEPHONY_SUCCESS;
763 }
764 
HangUpAllConnection(int32_t slotId)765 int32_t CellularCallService::HangUpAllConnection(int32_t slotId)
766 {
767     if (GetCsControl(slotId)) {
768         GetCsControl(slotId)->HangUpAllConnection(slotId);
769     }
770     if (GetImsControl(slotId)) {
771         GetImsControl(slotId)->HangUpAllConnection(slotId);
772     }
773     if (GetSatelliteControl(slotId)) {
774         GetSatelliteControl(slotId)->HangUpAllConnection(slotId);
775     }
776     return TELEPHONY_SUCCESS;
777 }
778 
SendUpdateCallMediaModeRequest(const CellularCallInfo &callInfo, ImsCallMode mode)779 int32_t CellularCallService::SendUpdateCallMediaModeRequest(const CellularCallInfo &callInfo, ImsCallMode mode)
780 {
781     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
782     if (videoCallControl == nullptr) {
783         TELEPHONY_LOGE("videoCallControl is nullptr");
784         return TELEPHONY_ERR_LOCAL_PTR_NULL;
785     }
786     return videoCallControl->SendUpdateCallMediaModeRequest(callInfo, mode);
787 }
788 
SendUpdateCallMediaModeResponse(const CellularCallInfo &callInfo, ImsCallMode mode)789 int32_t CellularCallService::SendUpdateCallMediaModeResponse(const CellularCallInfo &callInfo, ImsCallMode mode)
790 {
791     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
792     if (videoCallControl == nullptr) {
793         TELEPHONY_LOGE("videoCallControl is nullptr");
794         return TELEPHONY_ERR_LOCAL_PTR_NULL;
795     }
796     return videoCallControl->SendUpdateCallMediaModeResponse(callInfo, mode);
797 }
798 
CancelCallUpgrade(int32_t slotId, int32_t callIndex)799 int32_t CellularCallService::CancelCallUpgrade(int32_t slotId, int32_t callIndex)
800 {
801     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
802     if (videoCallControl == nullptr) {
803         TELEPHONY_LOGE("videoCallControl is nullptr");
804         return TELEPHONY_ERR_LOCAL_PTR_NULL;
805     }
806     return videoCallControl->CancelCallUpgrade(slotId, callIndex);
807 }
808 
RequestCameraCapabilities(int32_t slotId, int32_t callIndex)809 int32_t CellularCallService::RequestCameraCapabilities(int32_t slotId, int32_t callIndex)
810 {
811     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
812     if (videoCallControl == nullptr) {
813         TELEPHONY_LOGE("videoCallControl is nullptr");
814         return TELEPHONY_ERR_LOCAL_PTR_NULL;
815     }
816     return videoCallControl->RequestCameraCapabilities(slotId, callIndex);
817 }
818 
StartDtmf(char cDtmfCode, const CellularCallInfo &callInfo)819 int32_t CellularCallService::StartDtmf(char cDtmfCode, const CellularCallInfo &callInfo)
820 {
821     if (!IsValidSlotId(callInfo.slotId)) {
822         TELEPHONY_LOGE("CellularCallService::StartDtmf return, invalid slot id");
823         return CALL_ERR_INVALID_SLOT_ID;
824     }
825     if (srvccState_ == SrvccState::STARTED) {
826         return TELEPHONY_ERR_FAIL;
827     }
828     if (CallType::TYPE_IMS == callInfo.callType) {
829         auto imsControl = GetImsControl(callInfo.slotId);
830         if (imsControl == nullptr) {
831             TELEPHONY_LOGE("CellularCallService::StartDtmf return, imsControl is nullptr");
832             return TELEPHONY_ERR_LOCAL_PTR_NULL;
833         }
834         return imsControl->StartDtmf(imsControl->GetConnectionMap(), cDtmfCode, callInfo);
835     } else if (CallType::TYPE_CS == callInfo.callType) {
836         auto csControl = GetCsControl(callInfo.slotId);
837         if (csControl == nullptr) {
838             TELEPHONY_LOGE("CellularCallService::StartDtmf return, csControl is nullptr");
839             return TELEPHONY_ERR_LOCAL_PTR_NULL;
840         }
841         return csControl->StartDtmf(csControl->GetConnectionMap(), cDtmfCode, callInfo);
842     } else if (CallType::TYPE_SATELLITE == callInfo.callType) {
843         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
844         if (satelliteControl == nullptr) {
845             TELEPHONY_LOGE("CellularCallService::StartDtmf return, satelliteControl is nullptr");
846             return TELEPHONY_ERR_LOCAL_PTR_NULL;
847         }
848         return satelliteControl->StartDtmf(satelliteControl->GetConnectionMap(), cDtmfCode, callInfo);
849     }
850     TELEPHONY_LOGE("CellularCallService::StartDtmf return, call type error.");
851     return TELEPHONY_ERR_ARGUMENT_INVALID;
852 }
853 
StopDtmf(const CellularCallInfo &callInfo)854 int32_t CellularCallService::StopDtmf(const CellularCallInfo &callInfo)
855 {
856     if (!IsValidSlotId(callInfo.slotId)) {
857         TELEPHONY_LOGE("CellularCallService::StopDtmf return, invalid slot id");
858         return CALL_ERR_INVALID_SLOT_ID;
859     }
860     if (srvccState_ == SrvccState::STARTED) {
861         return TELEPHONY_ERR_FAIL;
862     }
863     if (CallType::TYPE_IMS == callInfo.callType) {
864         auto imsControl = GetImsControl(callInfo.slotId);
865         if (imsControl == nullptr) {
866             TELEPHONY_LOGE("CellularCallService::StopDtmf return, imsControl is nullptr");
867             return TELEPHONY_ERR_LOCAL_PTR_NULL;
868         }
869         return imsControl->StopDtmf(imsControl->GetConnectionMap(), callInfo);
870     } else if (CallType::TYPE_CS == callInfo.callType) {
871         auto csControl = GetCsControl(callInfo.slotId);
872         if (csControl == nullptr) {
873             TELEPHONY_LOGE("CellularCallService::StopDtmf return, csControl is nullptr");
874             return TELEPHONY_ERR_LOCAL_PTR_NULL;
875         }
876         return csControl->StopDtmf(csControl->GetConnectionMap(), callInfo);
877     } else if (CallType::TYPE_SATELLITE == callInfo.callType) {
878         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
879         if (satelliteControl == nullptr) {
880             TELEPHONY_LOGE("CellularCallService::StopDtmf return, satelliteControl is nullptr");
881             return TELEPHONY_ERR_LOCAL_PTR_NULL;
882         }
883         return satelliteControl->StopDtmf(satelliteControl->GetConnectionMap(), callInfo);
884     }
885     TELEPHONY_LOGE("CellularCallService::StopDtmf return, call type error.");
886     return TELEPHONY_ERR_ARGUMENT_INVALID;
887 }
888 
PostDialProceed(const CellularCallInfo &callInfo, const bool proceed)889 int32_t CellularCallService::PostDialProceed(const CellularCallInfo &callInfo, const bool proceed)
890 {
891     if (!IsValidSlotId(callInfo.slotId)) {
892         TELEPHONY_LOGE("CellularCallService::PostDialProceed return, invalid slot id");
893         return CALL_ERR_INVALID_SLOT_ID;
894     }
895     if (srvccState_ == SrvccState::STARTED) {
896         TELEPHONY_LOGE("CellularCallService::PostDialProceed srvccState_ is started");
897         return TELEPHONY_ERR_FAIL;
898     }
899     if (callInfo.callType == CallType::TYPE_IMS) {
900         auto imsControl = GetImsControl(callInfo.slotId);
901         if (imsControl == nullptr) {
902             TELEPHONY_LOGE("CellularCallService::PostDialProceed return, imsControl is nullptr");
903             return TELEPHONY_ERR_LOCAL_PTR_NULL;
904         }
905         return imsControl->PostDialProceed(callInfo, proceed);
906     } else if (callInfo.callType == CallType::TYPE_CS) {
907         auto csControl = GetCsControl(callInfo.slotId);
908         if (csControl == nullptr) {
909             TELEPHONY_LOGE("CellularCallService::PostDialProceed return, csControl is nullptr");
910             return TELEPHONY_ERR_LOCAL_PTR_NULL;
911         }
912         return csControl->PostDialProceed(callInfo, proceed);
913     } else if (CallType::TYPE_SATELLITE == callInfo.callType) {
914         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
915         if (satelliteControl == nullptr) {
916             TELEPHONY_LOGE("CellularCallService::PostDialProceed return, satelliteControl is nullptr");
917             return TELEPHONY_ERR_LOCAL_PTR_NULL;
918         }
919         return satelliteControl->PostDialProceed(callInfo, proceed);
920     }
921     TELEPHONY_LOGE("CellularCallService::PostDialProceed return, call type error.");
922     return TELEPHONY_ERR_ARGUMENT_INVALID;
923 }
924 
SendDtmf(char cDtmfCode, const CellularCallInfo &callInfo)925 int32_t CellularCallService::SendDtmf(char cDtmfCode, const CellularCallInfo &callInfo)
926 {
927     if (!IsValidSlotId(callInfo.slotId)) {
928         TELEPHONY_LOGE("CellularCallService::SendDtmf return, invalid slot id");
929         return CALL_ERR_INVALID_SLOT_ID;
930     }
931     if (srvccState_ == SrvccState::STARTED) {
932         return TELEPHONY_ERR_FAIL;
933     }
934     if (CallType::TYPE_IMS == callInfo.callType) {
935         auto imsControl = GetImsControl(callInfo.slotId);
936         if (imsControl == nullptr) {
937             TELEPHONY_LOGE("CellularCallService::SendDtmf return, imsControl is nullptr");
938             return TELEPHONY_ERR_LOCAL_PTR_NULL;
939         }
940         return imsControl->SendDtmf(imsControl->GetConnectionMap(), cDtmfCode, callInfo);
941     } else if (CallType::TYPE_CS == callInfo.callType) {
942         auto csControl = GetCsControl(callInfo.slotId);
943         if (csControl == nullptr) {
944             TELEPHONY_LOGE("CellularCallService::SendDtmf return, csControl is nullptr");
945             return TELEPHONY_ERR_LOCAL_PTR_NULL;
946         }
947         return csControl->SendDtmf(csControl->GetConnectionMap(), cDtmfCode, callInfo);
948     } else if (CallType::TYPE_SATELLITE == callInfo.callType) {
949         auto satelliteControl = GetSatelliteControl(callInfo.slotId);
950         if (satelliteControl == nullptr) {
951             TELEPHONY_LOGE("CellularCallService::SendDtmf return, satelliteControl is nullptr");
952             return TELEPHONY_ERR_LOCAL_PTR_NULL;
953         }
954         return satelliteControl->SendDtmf(satelliteControl->GetConnectionMap(), cDtmfCode, callInfo);
955     }
956     TELEPHONY_LOGE("CellularCallService::SendDtmf return, call type error.");
957     return TELEPHONY_ERR_ARGUMENT_INVALID;
958 }
959 
StartRtt(int32_t slotId, const std::string &msg)960 int32_t CellularCallService::StartRtt(int32_t slotId, const std::string &msg)
961 {
962     auto control = GetImsControl(slotId);
963     if (control == nullptr) {
964         TELEPHONY_LOGE("CellularCallService::StartRtt return, control is nullptr");
965         return TELEPHONY_ERR_LOCAL_PTR_NULL;
966     }
967     return control->StartRtt(slotId, msg);
968 }
969 
StopRtt(int32_t slotId)970 int32_t CellularCallService::StopRtt(int32_t slotId)
971 {
972     auto control = GetImsControl(slotId);
973     if (control == nullptr) {
974         TELEPHONY_LOGE("CellularCallService::StopRtt return, control is nullptr");
975         return TELEPHONY_ERR_LOCAL_PTR_NULL;
976     }
977     return control->StopRtt(slotId);
978 }
979 
SetCallTransferInfo(int32_t slotId, const CallTransferInfo &cTInfo)980 int32_t CellularCallService::SetCallTransferInfo(int32_t slotId, const CallTransferInfo &cTInfo)
981 {
982     if (!IsValidSlotId(slotId)) {
983         TELEPHONY_LOGE("CellularCallService::SetCallTransferInfo return, invalid slot id");
984         return CALL_ERR_INVALID_SLOT_ID;
985     }
986     CellularCallSupplement cellularCallSupplement;
987     if (cTInfo.settingType == CallTransferSettingType::CALL_TRANSFER_DISABLE) {
988         DelayedSingleton<CellularCallHiSysEvent>::GetInstance()->SetCallForwardingInfo(
989             slotId, false, cTInfo.transferNum);
990     } else if (cTInfo.settingType == CallTransferSettingType::CALL_TRANSFER_ENABLE) {
991         DelayedSingleton<CellularCallHiSysEvent>::GetInstance()->SetCallForwardingInfo(
992             slotId, true, cTInfo.transferNum);
993     }
994     return cellularCallSupplement.SetCallTransferInfo(slotId, cTInfo);
995 }
996 
CanSetCallTransferTime(int32_t slotId, bool &result)997 int32_t CellularCallService::CanSetCallTransferTime(int32_t slotId, bool &result)
998 {
999     if (!IsValidSlotId(slotId)) {
1000         TELEPHONY_LOGE("invalid slot id");
1001         return CALL_ERR_INVALID_SLOT_ID;
1002     }
1003     CellularCallSupplement cellularCallSupplement;
1004     return cellularCallSupplement.CanSetCallTransferTime(slotId, result);
1005 }
1006 
GetCallTransferInfo(int32_t slotId, CallTransferType type)1007 int32_t CellularCallService::GetCallTransferInfo(int32_t slotId, CallTransferType type)
1008 {
1009     TELEPHONY_LOGD("CellularCallService::GetCallTransferInfo");
1010     if (!IsValidSlotId(slotId)) {
1011         TELEPHONY_LOGE("CellularCallService::GetCallTransferInfo return, invalid slot id");
1012         return CALL_ERR_INVALID_SLOT_ID;
1013     }
1014     CellularCallSupplement cellularCallSupplement;
1015     return cellularCallSupplement.GetCallTransferInfo(slotId, type);
1016 }
1017 
GetCsControl(int32_t slotId)1018 std::shared_ptr<CSControl> CellularCallService::GetCsControl(int32_t slotId)
1019 {
1020     std::lock_guard<std::mutex> lock(mutex_);
1021     if (!IsValidSlotId(slotId)) {
1022         TELEPHONY_LOGE("return nullptr, invalid slot id");
1023         return nullptr;
1024     }
1025     return csControlMap_[slotId];
1026 }
1027 
GetImsControl(int32_t slotId)1028 std::shared_ptr<IMSControl> CellularCallService::GetImsControl(int32_t slotId)
1029 {
1030     std::lock_guard<std::mutex> lock(mutex_);
1031     if (!IsValidSlotId(slotId)) {
1032         TELEPHONY_LOGE("return nullptr, invalid slot id");
1033         return nullptr;
1034     }
1035     return imsControlMap_[slotId];
1036 }
1037 
GetSatelliteControl(int32_t slotId)1038 std::shared_ptr<SatelliteControl> CellularCallService::GetSatelliteControl(int32_t slotId)
1039 {
1040     std::lock_guard<std::mutex> lock(mutex_);
1041     return satelliteControlMap_[slotId];
1042 }
1043 
SetCsControl(int32_t slotId, const std::shared_ptr<CSControl> &csControl)1044 void CellularCallService::SetCsControl(int32_t slotId, const std::shared_ptr<CSControl> &csControl)
1045 {
1046     std::lock_guard<std::mutex> lock(mutex_);
1047     if (!IsValidSlotId(slotId)) {
1048         TELEPHONY_LOGE("invalid slot id, return");
1049         return;
1050     }
1051     csControlMap_[slotId] = csControl;
1052 }
1053 
SetImsControl(int32_t slotId, const std::shared_ptr<IMSControl> &imsControl)1054 void CellularCallService::SetImsControl(int32_t slotId, const std::shared_ptr<IMSControl> &imsControl)
1055 {
1056     std::lock_guard<std::mutex> lock(mutex_);
1057     if (!IsValidSlotId(slotId)) {
1058         TELEPHONY_LOGE("invalid slot id, return");
1059         return;
1060     }
1061     imsControlMap_[slotId] = imsControl;
1062 }
1063 
SetSatelliteControl(int32_t slotId, const std::shared_ptr<SatelliteControl> &satelliteControl)1064 void CellularCallService::SetSatelliteControl(int32_t slotId, const std::shared_ptr<SatelliteControl> &satelliteControl)
1065 {
1066     std::lock_guard<std::mutex> lock(mutex_);
1067     if (!IsValidSlotId(slotId)) {
1068         TELEPHONY_LOGE("invalid slot id, return");
1069         return;
1070     }
1071     satelliteControlMap_[slotId] = satelliteControl;
1072 }
1073 
SetCallWaiting(int32_t slotId, bool activate)1074 int32_t CellularCallService::SetCallWaiting(int32_t slotId, bool activate)
1075 {
1076     if (!IsValidSlotId(slotId)) {
1077         TELEPHONY_LOGE("CellularCallService::SetCallWaiting return, invalid slot id");
1078         return CALL_ERR_INVALID_SLOT_ID;
1079     }
1080     CellularCallSupplement cellularCallSupplement;
1081     return cellularCallSupplement.SetCallWaiting(slotId, activate);
1082 }
1083 
GetCallWaiting(int32_t slotId)1084 int32_t CellularCallService::GetCallWaiting(int32_t slotId)
1085 {
1086     TELEPHONY_LOGD("CellularCallService::GetCallWaiting");
1087     if (!IsValidSlotId(slotId)) {
1088         TELEPHONY_LOGE("CellularCallService::GetCallWaiting return, invalid slot id");
1089         return CALL_ERR_INVALID_SLOT_ID;
1090     }
1091     CellularCallSupplement cellularCallSupplement;
1092     return cellularCallSupplement.GetCallWaiting(slotId);
1093 }
1094 
SetCallRestriction(int32_t slotId, const CallRestrictionInfo &crInfo)1095 int32_t CellularCallService::SetCallRestriction(int32_t slotId, const CallRestrictionInfo &crInfo)
1096 {
1097     TELEPHONY_LOGD("CellularCallService::SetCallRestriction");
1098     if (!IsValidSlotId(slotId)) {
1099         TELEPHONY_LOGE("CellularCallService::SetCallRestriction return, invalid slot id");
1100         return CALL_ERR_INVALID_SLOT_ID;
1101     }
1102     CellularCallSupplement cellularCallSupplement;
1103     return cellularCallSupplement.SetCallRestriction(slotId, crInfo);
1104 }
1105 
GetCallRestriction(int32_t slotId, CallRestrictionType facType)1106 int32_t CellularCallService::GetCallRestriction(int32_t slotId, CallRestrictionType facType)
1107 {
1108     TELEPHONY_LOGD("CellularCallService::GetCallRestriction");
1109     if (!IsValidSlotId(slotId)) {
1110         TELEPHONY_LOGE("CellularCallService::GetCallRestriction return, invalid slot id");
1111         return CALL_ERR_INVALID_SLOT_ID;
1112     }
1113     CellularCallSupplement cellularCallSupplement;
1114     return cellularCallSupplement.GetCallRestriction(slotId, facType);
1115 }
1116 
SetCallRestrictionPassword( int32_t slotId, CallRestrictionType facType, const char *oldPassword, const char *newPassword)1117 int32_t CellularCallService::SetCallRestrictionPassword(
1118     int32_t slotId, CallRestrictionType facType, const char *oldPassword, const char *newPassword)
1119 {
1120     if (!IsValidSlotId(slotId)) {
1121         TELEPHONY_LOGE("invalid slot id");
1122         return CALL_ERR_INVALID_SLOT_ID;
1123     }
1124     CellularCallSupplement cellularCallSupplement;
1125     return cellularCallSupplement.SetBarringPassword(slotId, facType, oldPassword, newPassword);
1126 }
1127 
IsEmergencyPhoneNumber(int32_t slotId, const std::string &phoneNum, bool &enabled)1128 int32_t CellularCallService::IsEmergencyPhoneNumber(int32_t slotId, const std::string &phoneNum, bool &enabled)
1129 {
1130     if (!IsValidSlotId(slotId)) {
1131         TELEPHONY_LOGE("CellularCallService::IsEmergencyPhoneNumber return, invalid slot id");
1132         return CALL_ERR_INVALID_SLOT_ID;
1133     }
1134     EmergencyUtils emergencyUtils;
1135     return emergencyUtils.IsEmergencyCall(slotId, phoneNum, enabled);
1136 }
1137 
SetEmergencyCallList(int32_t slotId, std::vector<EmergencyCall> &eccVec)1138 int32_t CellularCallService::SetEmergencyCallList(int32_t slotId, std::vector<EmergencyCall> &eccVec)
1139 {
1140     TELEPHONY_LOGD("CellularCallService::SetEmergencyCallList start");
1141     if (!IsValidSlotId(slotId)) {
1142         TELEPHONY_LOGE("CellularCallService::SetMute return, invalid slot id");
1143         return CALL_ERR_INVALID_SLOT_ID;
1144     }
1145     CellularCallConfig config;
1146     return config.SetEmergencyCallList(slotId, eccVec);
1147 }
1148 
SetDomainPreferenceMode(int32_t slotId, int32_t mode)1149 int32_t CellularCallService::SetDomainPreferenceMode(int32_t slotId, int32_t mode)
1150 {
1151     if (!IsValidSlotId(slotId)) {
1152         TELEPHONY_LOGE("CellularCallService::SetDomainPreferenceMode return, invalid slot id");
1153         return CALL_ERR_INVALID_SLOT_ID;
1154     }
1155     CellularCallConfig config;
1156     return config.SetDomainPreferenceMode(slotId, mode);
1157 }
1158 
GetDomainPreferenceMode(int32_t slotId)1159 int32_t CellularCallService::GetDomainPreferenceMode(int32_t slotId)
1160 {
1161     if (!IsValidSlotId(slotId)) {
1162         TELEPHONY_LOGE("CellularCallService::GetDomainPreferenceMode return, invalid slot id");
1163         return CALL_ERR_INVALID_SLOT_ID;
1164     }
1165     CellularCallConfig config;
1166     return config.GetDomainPreferenceMode(slotId);
1167 }
1168 
SetImsSwitchStatus(int32_t slotId, bool active)1169 int32_t CellularCallService::SetImsSwitchStatus(int32_t slotId, bool active)
1170 {
1171     if (!IsValidSlotId(slotId)) {
1172         TELEPHONY_LOGE("CellularCallService::SetImsSwitchStatus return, invalid slot id");
1173         return CALL_ERR_INVALID_SLOT_ID;
1174     }
1175     CellularCallConfig config;
1176     return config.SetImsSwitchStatus(slotId, active);
1177 }
1178 
GetImsSwitchStatus(int32_t slotId, bool &enabled)1179 int32_t CellularCallService::GetImsSwitchStatus(int32_t slotId, bool &enabled)
1180 {
1181     if (!IsValidSlotId(slotId)) {
1182         TELEPHONY_LOGE("CellularCallService::GetImsSwitchStatus return, invalid slot id");
1183         return CALL_ERR_INVALID_SLOT_ID;
1184     }
1185     CellularCallConfig config;
1186     return config.GetImsSwitchStatus(slotId, enabled);
1187 }
1188 
SetVoNRState(int32_t slotId, int32_t state)1189 int32_t CellularCallService::SetVoNRState(int32_t slotId, int32_t state)
1190 {
1191     if (!IsValidSlotId(slotId)) {
1192         TELEPHONY_LOGE("CellularCallService::SetVoNRState return, invalid slot id");
1193         return CALL_ERR_INVALID_SLOT_ID;
1194     }
1195     CellularCallConfig config;
1196     return config.SetVoNRSwitchStatus(slotId, state);
1197 }
1198 
GetVoNRState(int32_t slotId, int32_t &state)1199 int32_t CellularCallService::GetVoNRState(int32_t slotId, int32_t &state)
1200 {
1201     if (!IsValidSlotId(slotId)) {
1202         TELEPHONY_LOGE("CellularCallService::GetVoNRState return, invalid slot id");
1203         return CALL_ERR_INVALID_SLOT_ID;
1204     }
1205     CellularCallConfig config;
1206     return config.GetVoNRSwitchStatus(slotId, state);
1207 }
1208 
SetImsConfig(int32_t slotId, ImsConfigItem item, const std::string &value)1209 int32_t CellularCallService::SetImsConfig(int32_t slotId, ImsConfigItem item, const std::string &value)
1210 {
1211     if (!IsValidSlotId(slotId)) {
1212         TELEPHONY_LOGE("CellularCallService::SetImsConfig return, invalid slot id");
1213         return CALL_ERR_INVALID_SLOT_ID;
1214     }
1215     CellularCallConfig config;
1216     return config.SetImsConfig(item, value);
1217 }
1218 
SetImsConfig(int32_t slotId, ImsConfigItem item, int32_t value)1219 int32_t CellularCallService::SetImsConfig(int32_t slotId, ImsConfigItem item, int32_t value)
1220 {
1221     if (!IsValidSlotId(slotId)) {
1222         TELEPHONY_LOGE("CellularCallService::SetImsConfig return, invalid slot id");
1223         return CALL_ERR_INVALID_SLOT_ID;
1224     }
1225     CellularCallConfig config;
1226     return config.SetImsConfig(item, value);
1227 }
1228 
GetImsConfig(int32_t slotId, ImsConfigItem item)1229 int32_t CellularCallService::GetImsConfig(int32_t slotId, ImsConfigItem item)
1230 {
1231     if (!IsValidSlotId(slotId)) {
1232         TELEPHONY_LOGE("CellularCallService::GetImsConfig return, invalid slot id");
1233         return CALL_ERR_INVALID_SLOT_ID;
1234     }
1235     CellularCallConfig config;
1236     return config.GetImsConfig(item);
1237 }
1238 
SetImsFeatureValue(int32_t slotId, FeatureType type, int32_t value)1239 int32_t CellularCallService::SetImsFeatureValue(int32_t slotId, FeatureType type, int32_t value)
1240 {
1241     if (!IsValidSlotId(slotId)) {
1242         TELEPHONY_LOGE("CellularCallService::SetImsFeatureValue return, invalid slot id");
1243         return CALL_ERR_INVALID_SLOT_ID;
1244     }
1245     CellularCallConfig config;
1246     return config.SetImsFeatureValue(type, value);
1247 }
1248 
GetImsFeatureValue(int32_t slotId, FeatureType type)1249 int32_t CellularCallService::GetImsFeatureValue(int32_t slotId, FeatureType type)
1250 {
1251     if (!IsValidSlotId(slotId)) {
1252         TELEPHONY_LOGE("CellularCallService::GetImsFeatureValue return, invalid slot id");
1253         return CALL_ERR_INVALID_SLOT_ID;
1254     }
1255     CellularCallConfig config;
1256     return config.GetImsFeatureValue(type);
1257 }
1258 
IsValidSlotId(int32_t slotId) const1259 bool CellularCallService::IsValidSlotId(int32_t slotId) const
1260 {
1261     const int32_t slotSingle = 1;
1262     const int32_t slotDouble = 2;
1263     if (SIM_SLOT_COUNT == slotSingle) {
1264         return slotId == DEFAULT_SIM_SLOT_ID;
1265     } else if (SIM_SLOT_COUNT == slotDouble) {
1266         return slotId == SIM_SLOT_0 || slotId == SIM_SLOT_1;
1267     }
1268     return false;
1269 }
1270 
IsNeedIms(int32_t slotId) const1271 bool CellularCallService::IsNeedIms(int32_t slotId) const
1272 {
1273     ModuleServiceUtils moduleUtils;
1274     CellularCallConfig config;
1275     bool imsRegState = moduleUtils.GetImsRegistrationState(slotId);
1276     bool imsServiceConnected = moduleUtils.NeedCallImsService();
1277     int32_t preferenceMode = config.GetPreferenceMode(slotId);
1278     bool imsSwitchStatus = false;
1279     config.GetImsSwitchStatus(slotId, imsSwitchStatus);
1280     TELEPHONY_LOGI("IsNeedIms state:%{public}d, mode:%{public}d, status:%{public}d, connected:%{public}d", imsRegState,
1281         preferenceMode, imsSwitchStatus, imsServiceConnected);
1282     if (imsRegState && preferenceMode != DomainPreferenceMode::CS_VOICE_ONLY && imsSwitchStatus &&
1283         imsServiceConnected) {
1284         return true;
1285     }
1286     return false;
1287 }
1288 
GetHandler(int32_t slotId)1289 std::shared_ptr<CellularCallHandler> CellularCallService::GetHandler(int32_t slotId)
1290 {
1291     std::unique_lock<std::mutex> lock(handlerMapMutex_);
1292     return handlerMap_[slotId];
1293 }
1294 
ControlCamera(int32_t slotId, int32_t index, const std::string &cameraId)1295 int32_t CellularCallService::ControlCamera(int32_t slotId, int32_t index, const std::string &cameraId)
1296 {
1297     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
1298     if (videoCallControl == nullptr) {
1299         TELEPHONY_LOGE("videoCallControl is nullptr");
1300         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1301     }
1302     return videoCallControl->ControlCamera(slotId, index, cameraId);
1303 }
1304 
SetPreviewWindow( int32_t slotId, int32_t index, const std::string &surfaceId, sptr<Surface> surface)1305 int32_t CellularCallService::SetPreviewWindow(
1306     int32_t slotId, int32_t index, const std::string &surfaceId, sptr<Surface> surface)
1307 {
1308     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
1309     if (videoCallControl == nullptr) {
1310         TELEPHONY_LOGE("videoCallControl is nullptr");
1311         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1312     }
1313     return videoCallControl->SetPreviewWindow(slotId, index, surfaceId, surface);
1314 }
1315 
SetDisplayWindow( int32_t slotId, int32_t index, const std::string &surfaceId, sptr<Surface> surface)1316 int32_t CellularCallService::SetDisplayWindow(
1317     int32_t slotId, int32_t index, const std::string &surfaceId, sptr<Surface> surface)
1318 {
1319     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
1320     if (videoCallControl == nullptr) {
1321         TELEPHONY_LOGE("videoCallControl is nullptr");
1322         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1323     }
1324     return videoCallControl->SetDisplayWindow(slotId, index, surfaceId, surface);
1325 }
1326 
SetCameraZoom(float zoomRatio)1327 int32_t CellularCallService::SetCameraZoom(float zoomRatio)
1328 {
1329     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
1330     if (videoCallControl == nullptr) {
1331         TELEPHONY_LOGE("videoCallControl is nullptr");
1332         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1333     }
1334     return videoCallControl->SetCameraZoom(zoomRatio);
1335 }
1336 
SetPausePicture(int32_t slotId, int32_t index, const std::string &path)1337 int32_t CellularCallService::SetPausePicture(int32_t slotId, int32_t index, const std::string &path)
1338 {
1339     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
1340     if (videoCallControl == nullptr) {
1341         TELEPHONY_LOGE("videoCallControl is nullptr");
1342         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1343     }
1344     return videoCallControl->SetPausePicture(slotId, index, path);
1345 }
1346 
SetDeviceDirection(int32_t slotId, int32_t callIndex, int32_t rotation)1347 int32_t CellularCallService::SetDeviceDirection(int32_t slotId, int32_t callIndex, int32_t rotation)
1348 {
1349     auto videoCallControl = DelayedSingleton<ImsVideoCallControl>::GetInstance();
1350     if (videoCallControl == nullptr) {
1351         TELEPHONY_LOGE("videoCallControl is nullptr");
1352         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1353     }
1354     return videoCallControl->SetDeviceDirection(slotId, callIndex, rotation);
1355 }
1356 
SetMute(int32_t slotId, int32_t mute)1357 int32_t CellularCallService::SetMute(int32_t slotId, int32_t mute)
1358 {
1359     if (!IsValidSlotId(slotId)) {
1360         TELEPHONY_LOGE("CellularCallService::SetMute return, invalid slot id");
1361         return CALL_ERR_INVALID_SLOT_ID;
1362     }
1363     CellularCallConfig config;
1364     return config.SetMute(slotId, mute);
1365 }
1366 
GetMute(int32_t slotId)1367 int32_t CellularCallService::GetMute(int32_t slotId)
1368 {
1369     if (!IsValidSlotId(slotId)) {
1370         TELEPHONY_LOGE("CellularCallService::GetMute return, invalid slot id");
1371         return CALL_ERR_INVALID_SLOT_ID;
1372     }
1373     CellularCallConfig config;
1374     return config.GetMute(slotId);
1375 }
1376 
CloseUnFinishedUssd(int32_t slotId)1377 int32_t CellularCallService::CloseUnFinishedUssd(int32_t slotId)
1378 {
1379     if (!IsValidSlotId(slotId)) {
1380         TELEPHONY_LOGE("CellularCallService::CloseUnFinishedUssd return, invalid slot id");
1381         return CALL_ERR_INVALID_SLOT_ID;
1382     }
1383     CellularCallSupplement cellularCallSupplement;
1384     return cellularCallSupplement.CloseUnFinishedUssd(slotId);
1385 }
1386 
SetControl(const CellularCallInfo &info)1387 int32_t CellularCallService::SetControl(const CellularCallInfo &info)
1388 {
1389     if (info.callType == CallType::TYPE_CS) {
1390         auto csControl = GetCsControl(info.slotId);
1391         if (csControl == nullptr) {
1392             TELEPHONY_LOGI("GetCsControl csControl is nullptr");
1393             csControl = std::make_shared<CSControl>();
1394             if (csControl == nullptr) {
1395                 TELEPHONY_LOGE("csControl is nullptr");
1396                 return TELEPHONY_ERR_LOCAL_PTR_NULL;
1397             }
1398             SetCsControl(info.slotId, csControl);
1399         }
1400     }
1401     if (info.callType == CallType::TYPE_IMS) {
1402         auto imsControl = GetImsControl(info.slotId);
1403         if (imsControl == nullptr) {
1404             TELEPHONY_LOGI("GetImsControl imsControl is nullptr");
1405             imsControl = std::make_shared<IMSControl>();
1406             if (imsControl == nullptr) {
1407                 TELEPHONY_LOGE("imsControl is nullptr");
1408                 return TELEPHONY_ERR_LOCAL_PTR_NULL;
1409             }
1410             SetImsControl(info.slotId, imsControl);
1411         }
1412     }
1413     return TELEPHONY_SUCCESS;
1414 }
1415 
ClearAllCalls(const std::vector<CellularCallInfo> &infos)1416 int32_t CellularCallService::ClearAllCalls(const std::vector<CellularCallInfo> &infos)
1417 {
1418     if (infos.empty()) {
1419         TELEPHONY_LOGE("CellularCallService::ClearAllCalls infos is empty");
1420         return TELEPHONY_ERR_ARGUMENT_INVALID;
1421     }
1422     for (auto &info : infos) {
1423         if (SetControl(info) != TELEPHONY_SUCCESS) {
1424             return TELEPHONY_ERR_LOCAL_PTR_NULL;
1425         }
1426     }
1427     HangUpWithCellularCallRestart(infos);
1428     return TELEPHONY_SUCCESS;
1429 }
1430 
SetSrvccState(int32_t srvccState)1431 void CellularCallService::SetSrvccState(int32_t srvccState)
1432 {
1433     srvccState_ = srvccState;
1434 }
1435 
GetSrvccState()1436 int32_t CellularCallService::GetSrvccState()
1437 {
1438     return srvccState_;
1439 }
1440 
UseImsForEmergency(const CellularCallInfo &callInfo, bool isEcc)1441 bool CellularCallService::UseImsForEmergency(const CellularCallInfo &callInfo, bool isEcc)
1442 {
1443     ModuleServiceUtils moduleUtils;
1444     CellularCallConfig config;
1445     if (isEcc && moduleUtils.NeedCallImsService() && config.GetImsPreferForEmergencyConfig(callInfo.slotId)) {
1446         return true;
1447     }
1448     return false;
1449 }
1450 
HandleCallManagerException()1451 void CellularCallService::HandleCallManagerException()
1452 {
1453     ModuleServiceUtils obtain;
1454     std::vector<int32_t> slotVector = obtain.GetSlotInfo();
1455     for (const auto &it : slotVector) {
1456         auto csControl = GetCsControl(it);
1457         if (csControl != nullptr) {
1458             csControl->HangUpAllConnection(it);
1459         }
1460         auto imsControl = GetImsControl(it);
1461         if (imsControl != nullptr) {
1462             imsControl->HangUpAllConnection(it);
1463         }
1464     }
1465 }
1466 
HangUpWithCellularCallRestart(const std::vector<CellularCallInfo> &infos)1467 void CellularCallService::HangUpWithCellularCallRestart(const std::vector<CellularCallInfo> &infos)
1468 {
1469     ModuleServiceUtils obtain;
1470     std::vector<int32_t> slotVector = obtain.GetSlotInfo();
1471     for (const auto &it : slotVector) {
1472         auto csControl = GetCsControl(it);
1473         if (csControl != nullptr) {
1474             csControl->ReportHangUp(infos, it);
1475             csControl->HangUpAllConnection(it);
1476         }
1477         auto imsControl = GetImsControl(it);
1478         if (imsControl != nullptr) {
1479             imsControl->ReportHangUp(infos, it);
1480             imsControl->RestoreConnection(infos, it);
1481             imsControl->HangUpAllConnection(it);
1482             imsControl->ReleaseAllConnection();
1483         }
1484     }
1485 }
1486 
HandleCellularControlException(const CellularCallInfo &callInfo)1487 void CellularCallService::HandleCellularControlException(const CellularCallInfo &callInfo)
1488 {
1489     TELEPHONY_LOGI("HandleCellularControlException entry");
1490     CallsReportInfo callsReportInfo;
1491     CallReportInfo reportInfo = EncapsulationCallReportInfo(callInfo);
1492     callsReportInfo.callVec.push_back(reportInfo);
1493     if (DelayedSingleton<CellularCallRegister>::GetInstance() == nullptr) {
1494         TELEPHONY_LOGE("HandleCellularControlException return, GetInstance() is nullptr.");
1495         return;
1496     }
1497     callsReportInfo.slotId = callInfo.slotId;
1498     DelayedSingleton<CellularCallRegister>::GetInstance()->ReportCallsInfo(callsReportInfo);
1499 }
1500 
EncapsulationCallReportInfo(const CellularCallInfo &callInfo)1501 CallReportInfo CellularCallService::EncapsulationCallReportInfo(const CellularCallInfo &callInfo)
1502 {
1503     TELEPHONY_LOGD("EncapsulationCallReportInfo entry");
1504     CallReportInfo callReportInfo;
1505     if (memset_s(&callReportInfo, sizeof(callReportInfo), 0, sizeof(callReportInfo)) != EOK) {
1506         TELEPHONY_LOGE("EncapsulationCallReportInfo return, memset_s fail.");
1507         return callReportInfo;
1508     }
1509 
1510     size_t cpyLen = strlen(callInfo.phoneNum) + 1;
1511     if (cpyLen > static_cast<size_t>(kMaxNumberLen + 1)) {
1512         TELEPHONY_LOGE("EncapsulationCallReportInfo return, strcpy_s fail.");
1513         return callReportInfo;
1514     }
1515     if (strcpy_s(callReportInfo.accountNum, cpyLen, callInfo.phoneNum) != EOK) {
1516         TELEPHONY_LOGE("EncapsulationCallReportInfo return, strcpy_s fail.");
1517         return callReportInfo;
1518     }
1519     callReportInfo.index = callInfo.index;
1520     callReportInfo.accountId = callInfo.slotId;
1521     callReportInfo.state = TelCallState::CALL_STATUS_DISCONNECTED;
1522     callReportInfo.callType = callInfo.callType;
1523     return callReportInfo;
1524 }
1525 
SystemAbilityStatusChangeListener( std::shared_ptr<CellularCallHandler> &cellularCallHandler)1526 CellularCallService::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
1527     std::shared_ptr<CellularCallHandler> &cellularCallHandler)
1528     : cellularCallHandler_(cellularCallHandler)
1529 {}
1530 
OnAddSystemAbility( int32_t systemAbilityId, const std::string &deviceId)1531 void CellularCallService::SystemAbilityStatusChangeListener::OnAddSystemAbility(
1532     int32_t systemAbilityId, const std::string &deviceId)
1533 {
1534     if (systemAbilityId != COMMON_EVENT_SERVICE_ID && systemAbilityId != TELEPHONY_SATELLITE_SYS_ABILITY_ID) {
1535         TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID or TELEPHONY_SATELLITE_SYS_ABILITY_ID");
1536         return;
1537     }
1538     if (cellularCallHandler_ == nullptr) {
1539         TELEPHONY_LOGE("COMMON_EVENT_SERVICE_ID cellularCallHandler_ is nullptr");
1540         return;
1541     }
1542     if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
1543         bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(cellularCallHandler_);
1544         TELEPHONY_LOGI("subscribeResult = %{public}d", subscribeResult);
1545     } else if (systemAbilityId == TELEPHONY_SATELLITE_SYS_ABILITY_ID) {
1546         DelayedSingleton<SatelliteCallClient>::GetInstance()->Init();
1547         cellularCallHandler_->RegisterSatelliteCallCallbackHandler();
1548     }
1549 }
1550 
OnRemoveSystemAbility( int32_t systemAbilityId, const std::string &deviceId)1551 void CellularCallService::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
1552     int32_t systemAbilityId, const std::string &deviceId)
1553 {
1554     switch (systemAbilityId) {
1555         case TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID: {
1556             auto cellularCallService = DelayedSingleton<CellularCallService>::GetInstance();
1557             if (cellularCallService == nullptr) {
1558                 TELEPHONY_LOGE("cellularCallService is nullptr");
1559                 return;
1560             }
1561             cellularCallService->HandleCallManagerException();
1562             count_++;
1563             CellularCallHiSysEvent::WriteFoundationRestartFaultEvent(count_);
1564             break;
1565         }
1566         case COMMON_EVENT_SERVICE_ID: {
1567             if (cellularCallHandler_ == nullptr) {
1568                 TELEPHONY_LOGE("cellularCallHandler_ is nullptr");
1569                 return;
1570             }
1571             bool unSubscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(cellularCallHandler_);
1572             TELEPHONY_LOGI("unSubscribeResult = %{public}d", unSubscribeResult);
1573             break;
1574         }
1575         case TELEPHONY_SATELLITE_SYS_ABILITY_ID: {
1576             DelayedSingleton<SatelliteCallClient>::GetInstance()->UnInit();
1577             break;
1578         }
1579         default:
1580             TELEPHONY_LOGE("systemAbilityId is invalid");
1581             break;
1582     }
1583 }
1584 
1585 #ifdef CALL_MANAGER_AUTO_START_OPTIMIZE
StartCallManagerService()1586 void CellularCallService::StartCallManagerService()
1587 {
1588     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1589     if (managerPtr == nullptr) {
1590         TELEPHONY_LOGE("GetSystemAbilityManager failed!");
1591         return;
1592     }
1593 
1594     sptr<IRemoteObject> iRemoteObjectPtr = managerPtr->GetSystemAbility(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
1595     if (iRemoteObjectPtr == nullptr) {
1596         TELEPHONY_LOGE("GetSystemAbility failed!");
1597     }
1598 }
1599 #endif
1600 } // namespace Telephony
1601 } // namespace OHOS
1602