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