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