• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 "sim_state_handle.h"
17 
18 #include "common_event.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "core_service_hisysevent.h"
22 #include "enum_convert.h"
23 #ifdef CORE_SERVICE_SUPPORT_ESIM
24 #include "esim_service_client.h"
25 #endif
26 #include "hilog/log.h"
27 #include "if_system_ability_manager.h"
28 #include "inner_event.h"
29 #include "iservice_registry.h"
30 #include "radio_event.h"
31 #include "satellite_service_client.h"
32 #include "sim_constant.h"
33 #include "sim_state_manager.h"
34 #ifdef CORE_SERVICE_SUPPORT_ESIM
35 #include "start_osu_result_callback.h"
36 #endif
37 #include "system_ability_definition.h"
38 #include "tel_event_handler.h"
39 #include "tel_ril_sim_parcel.h"
40 #include "telephony_log_wrapper.h"
41 #include "telephony_state_registry_client.h"
42 #include "telephony_types.h"
43 #include "core_manager_inner.h"
44 
45 using namespace OHOS::EventFwk;
46 namespace OHOS {
47 namespace Telephony {
48 const std::map<uint32_t, SimStateHandle::Func> SimStateHandle::memberFuncMap_ = {
49     { MSG_SIM_UNLOCK_PIN_DONE,
__anonc574a2290102() 50         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
51             handle->GetUnlockResult(slotId, event);
52         } },
53     { MSG_SIM_UNLOCK_PUK_DONE,
__anonc574a2290202() 54         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
55             handle->GetUnlockResult(slotId, event);
56         } },
57     { MSG_SIM_CHANGE_PIN_DONE,
__anonc574a2290302() 58         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
59             handle->GetUnlockResult(slotId, event);
60         } },
61     { MSG_SIM_UNLOCK_PIN2_DONE,
__anonc574a2290402() 62         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
63             handle->GetUnlockResult(slotId, event);
64         } },
65     { MSG_SIM_UNLOCK_PUK2_DONE,
__anonc574a2290502() 66         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
67             handle->GetUnlockResult(slotId, event);
68         } },
69     { MSG_SIM_CHANGE_PIN2_DONE,
__anonc574a2290602() 70         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
71             handle->GetUnlockResult(slotId, event);
72         } },
73     { MSG_SIM_UNLOCK_SIMLOCK_DONE,
__anonc574a2290702() 74         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
75             handle->GetUnlockSimLockResult(slotId, event);
76         } },
77     { MSG_SIM_ENABLE_PIN_DONE,
__anonc574a2290802() 78         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
79             handle->GetSetLockResult(slotId, event);
80         } },
81     { MSG_SIM_CHECK_PIN_DONE,
__anonc574a2290902() 82         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
83             handle->GetSimLockState(slotId, event);
84         } },
85     { MSG_SIM_GET_REALTIME_ICC_STATUS_DONE,
__anonc574a2290a02() 86         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
87             handle->GetSimCardData(slotId, event);
88         } },
89     { MSG_SIM_AUTHENTICATION_DONE,
__anonc574a2290b02() 90         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
91             handle->GetSimAuthenticationResult(slotId, event);
92         } },
93     { MSG_SIM_SEND_NCFG_OPER_INFO_DONE,
__anonc574a2290c02() 94         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
95             handle->GetSendSimMatchedOperatorInfoResult(slotId, event);
96         } },
97     { MSG_SIM_GET_SIM_IO_DONE,
__anonc574a2290d02() 98         [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
99             handle->GetSimIOResult(slotId, event);
100         } },
101 };
102 
SimStateHandle(const std::weak_ptr<SimStateManager> & simStateManager)103 SimStateHandle::SimStateHandle(const std::weak_ptr<SimStateManager> &simStateManager)
104     : TelEventHandler("SimStateHandle"), simStateManager_(simStateManager)
105 {
106     TELEPHONY_LOGI("SimStateHandle::SimStateHandle()");
107 }
108 
Init(int32_t slotId)109 void SimStateHandle::Init(int32_t slotId)
110 {
111     slotId_ = slotId;
112     TELEPHONY_LOGI("SimStateHandle::HasSimCard(), slotId_ = %{public}d", slotId_);
113     ConnectService();
114     if (IsSatelliteSupported() == static_cast<int32_t>(SatelliteValue::SATELLITE_SUPPORTED)) {
115         std::shared_ptr<SatelliteServiceClient> satelliteClient =
116             DelayedSingleton<SatelliteServiceClient>::GetInstance();
117         satelliteClient->AddSimHandler(slotId_, std::static_pointer_cast<TelEventHandler>(shared_from_this()));
118     }
119     auto telRilManager = telRilManager_.lock();
120     if (telRilManager != nullptr) {
121         TELEPHONY_LOGI("SimStateHandle::SimStateHandle RegisterEvent start");
122         telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE, nullptr);
123         telRilManager->RegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED, nullptr);
124     } else {
125         TELEPHONY_LOGE("SimStateHandle::SimStateHandle get ril_Manager fail");
126         return;
127     }
128     observerHandler_ = std::make_unique<ObserverHandler>();
129     if (observerHandler_ == nullptr) {
130         TELEPHONY_LOGE("SimStateHandle::failed to create new ObserverHandler");
131         return;
132     }
133     externalState_ = SimState::SIM_STATE_UNKNOWN;
134     CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
135     externalType_ = CardType::UNKNOWN_CARD;
136 }
137 
IsSatelliteSupported()138 int32_t SimStateHandle::IsSatelliteSupported()
139 {
140     char satelliteSupported[SYSPARA_SIZE] = { 0 };
141     GetParameter(TEL_SATELLITE_SUPPORTED, SATELLITE_DEFAULT_VALUE, satelliteSupported, SYSPARA_SIZE);
142     TELEPHONY_LOGI("satelliteSupported is %{public}s", satelliteSupported);
143     return std::atoi(satelliteSupported);
144 }
145 
HasSimCard()146 bool SimStateHandle::HasSimCard()
147 {
148     bool has = false;
149     if (iccState_.simStatus_ != ICC_CARD_ABSENT) {
150         has = true;
151     }
152     TELEPHONY_LOGD("SimStateHandle::HasSimCard(), has = %{public}d", has);
153     return has;
154 }
155 
GetSimState()156 SimState SimStateHandle::GetSimState()
157 {
158     return externalState_;
159 }
160 
GetSimIccStatus()161 IccSimStatus SimStateHandle::GetSimIccStatus()
162 {
163     return static_cast<IccSimStatus>(oldSimStatus_);
164 }
165 
SetSimState(SimState simState)166 void SimStateHandle::SetSimState(SimState simState)
167 {
168     externalState_ = simState;
169 }
170 
GetCardType()171 CardType SimStateHandle::GetCardType()
172 {
173     TELEPHONY_LOGD("SimStateHandle::GetCardType() externalType_=%{public}d", static_cast<int32_t>(externalType_));
174     return externalType_;
175 }
176 
GetIccid()177 std::string SimStateHandle::GetIccid()
178 {
179     return iccid_;
180 }
181 
UnlockPin(int32_t slotId,const std::string & pin)182 void SimStateHandle::UnlockPin(int32_t slotId, const std::string &pin)
183 {
184     TELEPHONY_LOGI("SimStateHandle::UnlockPin1() slotId = %{public}d", slotId);
185     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_UNLOCK_PIN_DONE);
186     if (event == nullptr) {
187         TELEPHONY_LOGE("event is nullptr!");
188         return;
189     }
190     event->SetOwner(shared_from_this());
191     auto telRilManager = telRilManager_.lock();
192     if (telRilManager == nullptr) {
193         TELEPHONY_LOGE("telRilManager is nullptr!");
194         return;
195     }
196     telRilManager->UnlockPin(slotId, pin, event);
197 }
198 
UnlockPuk(int32_t slotId,const std::string & newPin,const std::string & puk)199 void SimStateHandle::UnlockPuk(int32_t slotId, const std::string &newPin, const std::string &puk)
200 {
201     TELEPHONY_LOGI("SimStateHandle::UnlockPuk1() slotId = %{public}d", slotId);
202     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_UNLOCK_PUK_DONE);
203     if (event == nullptr) {
204         TELEPHONY_LOGE("event is nullptr!");
205         return;
206     }
207     event->SetOwner(shared_from_this());
208     auto telRilManager = telRilManager_.lock();
209     if (telRilManager == nullptr) {
210         TELEPHONY_LOGE("telRilManager is nullptr!");
211         return;
212     }
213     telRilManager->UnlockPuk(slotId, puk, newPin, event);
214 }
215 
AlterPin(int32_t slotId,const std::string & newPin,const std::string & oldPin)216 void SimStateHandle::AlterPin(int32_t slotId, const std::string &newPin, const std::string &oldPin)
217 {
218     TELEPHONY_LOGI("SimStateHandle::AlterPin() slotId = %{public}d", slotId);
219     int32_t length = (int32_t)newPin.size();
220     SimPasswordParam simPinPassword;
221     simPinPassword.passwordLength = length;
222     simPinPassword.fac = FAC_PIN_LOCK;
223     simPinPassword.oldPassword = oldPin;
224     simPinPassword.newPassword = newPin;
225     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_CHANGE_PIN_DONE);
226     if (event == nullptr) {
227         TELEPHONY_LOGE("event is nullptr!");
228         return;
229     }
230     event->SetOwner(shared_from_this());
231     auto telRilManager = telRilManager_.lock();
232     if (telRilManager == nullptr) {
233         TELEPHONY_LOGE("telRilManager is nullptr!");
234         return;
235     }
236     telRilManager->ChangeSimPassword(slotId, simPinPassword, event);
237 }
238 
UnlockPin2(int32_t slotId,const std::string & pin2)239 void SimStateHandle::UnlockPin2(int32_t slotId, const std::string &pin2)
240 {
241     TELEPHONY_LOGI("SimStateHandle::UnlockPin2() slotId = %{public}d", slotId);
242     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_UNLOCK_PIN2_DONE);
243     if (event == nullptr) {
244         TELEPHONY_LOGE("event is nullptr!");
245         return;
246     }
247     event->SetOwner(shared_from_this());
248     auto telRilManager = telRilManager_.lock();
249     if (telRilManager == nullptr) {
250         TELEPHONY_LOGE("telRilManager is nullptr!");
251         return;
252     }
253     telRilManager->UnlockPin2(slotId, pin2, event);
254 }
255 
UnlockPuk2(int32_t slotId,const std::string & newPin2,const std::string & puk2)256 void SimStateHandle::UnlockPuk2(int32_t slotId, const std::string &newPin2, const std::string &puk2)
257 {
258     TELEPHONY_LOGI("SimStateHandle::UnlockPuk2() slotId = %{public}d", slotId);
259     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_UNLOCK_PUK2_DONE);
260     if (event == nullptr) {
261         TELEPHONY_LOGE("event is nullptr!");
262         return;
263     }
264     event->SetOwner(shared_from_this());
265     auto telRilManager = telRilManager_.lock();
266     if (telRilManager == nullptr) {
267         TELEPHONY_LOGE("telRilManager is nullptr!");
268         return;
269     }
270     telRilManager->UnlockPuk2(slotId, puk2, newPin2, event);
271 }
272 
AlterPin2(int32_t slotId,const std::string & newPin2,const std::string & oldPin2)273 void SimStateHandle::AlterPin2(int32_t slotId, const std::string &newPin2, const std::string &oldPin2)
274 {
275     TELEPHONY_LOGI("SimStateHandle::AlterPin2() slotId = %{public}d", slotId);
276     int32_t length = (int32_t)newPin2.size();
277     SimPasswordParam simPin2Password;
278     simPin2Password.passwordLength = length;
279     simPin2Password.fac = FDN_PIN_LOCK;
280     simPin2Password.oldPassword = oldPin2;
281     simPin2Password.newPassword = newPin2;
282     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_CHANGE_PIN2_DONE);
283     if (event == nullptr) {
284         TELEPHONY_LOGE("event is nullptr!");
285         return;
286     }
287     event->SetOwner(shared_from_this());
288     auto telRilManager = telRilManager_.lock();
289     if (telRilManager == nullptr) {
290         TELEPHONY_LOGE("telRilManager is nullptr!");
291         return;
292     }
293     telRilManager->ChangeSimPassword(slotId, simPin2Password, event);
294 }
295 
SetLockState(int32_t slotId,const LockInfo & options)296 void SimStateHandle::SetLockState(int32_t slotId, const LockInfo &options)
297 {
298     TELEPHONY_LOGI("SimStateHandle::SetLockState() slotId = %{public}d", slotId);
299     SimLockParam simLock;
300     simLock.mode = static_cast<int32_t>(options.lockState);
301     simLock.passwd = Str16ToStr8(options.password);
302     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_ENABLE_PIN_DONE);
303     if (event == nullptr) {
304         TELEPHONY_LOGE("event is nullptr!");
305         return;
306     }
307     event->SetOwner(shared_from_this());
308     if (LockType::PIN_LOCK == options.lockType) {
309         simLock.fac = FAC_PIN_LOCK;
310     } else {
311         simLock.fac = FDN_PIN2_LOCK;
312     }
313     auto telRilManager = telRilManager_.lock();
314     if (telRilManager == nullptr) {
315         TELEPHONY_LOGE("telRilManager is nullptr!");
316         return;
317     }
318     telRilManager->SetSimLock(slotId, simLock, event);
319 }
320 
UnlockSimLock(int32_t slotId,const PersoLockInfo & lockInfo)321 void SimStateHandle::UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo)
322 {
323     TELEPHONY_LOGI("SimStateHandle::UnlockSimLock() slotId = %{public}d", slotId);
324     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_UNLOCK_SIMLOCK_DONE);
325     if (event == nullptr) {
326         TELEPHONY_LOGE("event is nullptr!");
327         return;
328     }
329     event->SetOwner(shared_from_this());
330     auto telRilManager = telRilManager_.lock();
331     if (telRilManager == nullptr) {
332         TELEPHONY_LOGE("SimStateHandle telRilManager is nullptr!!");
333         return;
334     }
335     int32_t lockType = static_cast<int32_t>(lockInfo.lockType);
336     telRilManager->UnlockSimLock(slotId, lockType, Str16ToStr8(lockInfo.password), event);
337 }
338 
SetRilManager(std::weak_ptr<Telephony::ITelRilManager> telRilManager)339 void SimStateHandle::SetRilManager(std::weak_ptr<Telephony::ITelRilManager> telRilManager)
340 {
341     telRilManager_ = telRilManager;
342     auto telRilManagerNew = telRilManager_.lock();
343     if (telRilManagerNew == nullptr) {
344         TELEPHONY_LOGE("SimStateHandle set NULL TelRilManager!!");
345     }
346 }
347 
GetLockState(int32_t slotId,LockType lockType)348 void SimStateHandle::GetLockState(int32_t slotId, LockType lockType)
349 {
350     TELEPHONY_LOGI("SimStateHandle::GetLockState() slotId = %{public}d", slotId);
351     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_CHECK_PIN_DONE);
352     if (event == nullptr) {
353         TELEPHONY_LOGE("event is nullptr!");
354         return;
355     }
356     event->SetOwner(shared_from_this());
357     auto telRilManager = telRilManager_.lock();
358     if (telRilManager == nullptr) {
359         TELEPHONY_LOGE("telRilManager is nullptr!");
360         return;
361     }
362     if (LockType::PIN_LOCK == lockType) {
363         telRilManager->GetSimLockStatus(slotId, FAC_PIN_LOCK, event);
364     } else {
365         telRilManager->GetSimLockStatus(slotId, FDN_PIN2_LOCK, event);
366     }
367 }
368 
ProcessIccCardState(IccState & ar,int32_t slotId)369 void SimStateHandle::ProcessIccCardState(IccState &ar, int32_t slotId)
370 {
371     TELEPHONY_LOGI("SimStateHandle::ProcessIccCardState slotId = %{public}d", slotId);
372     LockReason reason = LockReason::SIM_NONE;
373     const int32_t newSimType = ar.simType_;
374     const int32_t newSimStatus = ar.simStatus_;
375     iccState_ = ar;
376     auto iter = simIccStatusMap_.find(newSimStatus);
377     TELEPHONY_LOGI("SimStateHandle::ProcessIccCardState newSimType[%{public}d], newSimStatus[%{public}s](%{public}d), "
378         "oldSimStatus_%{public}d", newSimType, iter->second.c_str(), newSimStatus, oldSimStatus_);
379     if (oldSimType_ != newSimType) {
380         CardTypeEscape(newSimType, slotId);
381         oldSimType_ = newSimType;
382     }
383     if (oldSimStatus_ != newSimStatus) {
384         iccid_ = ar.iccid_;
385         SimStateEscape(newSimStatus, slotId, reason);
386         oldSimStatus_ = newSimStatus;
387         iter = simIccStatusMap_.find(newSimStatus);
388         TELEPHONY_LOGI("will to NotifyIccStateChanged at newSimStatus[%{public}s]"
389             "(%{public}d) observerHandler_ is nullptr[%{public}d] ",
390             iter->second.c_str(), newSimStatus, (observerHandler_ == nullptr));
391         if (newSimStatus == ICC_CARD_ABSENT) {
392             TELEPHONY_LOGI("SimStateHandle::ProcessIccCardState slotId: %{public}d ICC_CARD_ABSENT", slotId);
393             CoreManagerInner::GetInstance().ResetSimLoadAccount(slotId);
394         }
395         if (observerHandler_ != nullptr) {
396             observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_CHANGE, slotId);
397             observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_ICCID_LOADED, slotId);
398         }
399         DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateSimState(
400             slotId, externalType_, externalState_, reason);
401         if (TELEPHONY_EXT_WRAPPER.updateHotPlugCardState_ != nullptr) {
402             TELEPHONY_EXT_WRAPPER.updateHotPlugCardState_(slotId, externalState_);
403         }
404     }
405 }
406 
UnInit()407 void SimStateHandle::UnInit()
408 {
409     auto telRilManager = telRilManager_.lock();
410     if (telRilManager != nullptr) {
411         telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
412         telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED);
413     }
414     if (IsSatelliteSupported() == static_cast<int32_t>(SatelliteValue::SATELLITE_SUPPORTED) &&
415         satelliteCallback_ != nullptr) {
416         std::shared_ptr<SatelliteServiceClient> satelliteClient =
417             DelayedSingleton<SatelliteServiceClient>::GetInstance();
418         satelliteClient->UnRegisterCoreNotify(slotId_, RadioEvent::RADIO_SIM_STATE_CHANGE);
419     }
420 }
421 
RegisterSatelliteCallback()422 void SimStateHandle::RegisterSatelliteCallback()
423 {
424     satelliteCallback_ =
425         std::make_unique<SatelliteCoreCallback>(std::static_pointer_cast<TelEventHandler>(shared_from_this()))
426             .release();
427     std::shared_ptr<SatelliteServiceClient> satelliteClient = DelayedSingleton<SatelliteServiceClient>::GetInstance();
428     satelliteClient->RegisterCoreNotify(slotId_, RadioEvent::RADIO_SIM_STATE_CHANGE, satelliteCallback_);
429 }
430 
UnregisterSatelliteCallback()431 void SimStateHandle::UnregisterSatelliteCallback()
432 {
433     if (IsSatelliteSupported() == static_cast<int32_t>(SatelliteValue::SATELLITE_SUPPORTED)) {
434         satelliteCallback_ = nullptr;
435     }
436 }
437 
ObtainIccStatus(int32_t slotId)438 void SimStateHandle::ObtainIccStatus(int32_t slotId)
439 {
440     TELEPHONY_LOGI("SimStateHandle::ObtainIccStatus() slotId = %{public}d", slotId);
441     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_GET_ICC_STATUS_DONE);
442     if (event == nullptr) {
443         TELEPHONY_LOGE("event is nullptr!");
444         return;
445     }
446     event->SetOwner(shared_from_this());
447     auto telRilManager = telRilManager_.lock();
448     if (telRilManager == nullptr) {
449         TELEPHONY_LOGE("telRilManager is nullptr!");
450         return;
451     }
452     telRilManager->GetSimStatus(slotId, event); // get sim card state
453 }
454 
ObtainRealtimeIccStatus(int32_t slotId)455 void SimStateHandle::ObtainRealtimeIccStatus(int32_t slotId)
456 {
457     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_GET_REALTIME_ICC_STATUS_DONE);
458     if (event == nullptr) {
459         TELEPHONY_LOGE("event is nullptr!");
460         return;
461     }
462     event->SetOwner(shared_from_this());
463     auto telRilManager = telRilManager_.lock();
464     if (telRilManager == nullptr) {
465         TELEPHONY_LOGE("telRilManager is nullptr!");
466         return;
467     }
468     telRilManager->GetSimStatus(slotId, event); // get sim card state
469 }
470 
SimAuthentication(int32_t slotId,AuthType authType,const std::string & authData)471 int32_t SimStateHandle::SimAuthentication(int32_t slotId, AuthType authType, const std::string &authData)
472 {
473     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_AUTHENTICATION_DONE);
474     if (event == nullptr) {
475         TELEPHONY_LOGE("event is nullptr!");
476         return SIM_AUTH_FAIL;
477     }
478     event->SetOwner(shared_from_this());
479     SimAuthenticationRequestInfo requireInfo;
480     requireInfo.serial = static_cast<int32_t>(authType);
481     requireInfo.aid = GetAidByCardType(externalType_);
482     requireInfo.authData = authData;
483     auto telRilManager = telRilManager_.lock();
484     if (telRilManager == nullptr) {
485         TELEPHONY_LOGE("SimStateHandle::SimAuthentication() telRilManager is nullptr!!");
486         return SIM_AUTH_FAIL;
487     }
488     return telRilManager->SimAuthentication(slotId, requireInfo, event);
489 }
490 
SendSimMatchedOperatorInfo(int32_t slotId,int32_t state,const std::string & operName,const std::string & operKey)491 void SimStateHandle::SendSimMatchedOperatorInfo(
492     int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey)
493 {
494     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_SEND_NCFG_OPER_INFO_DONE);
495     if (event == nullptr) {
496         TELEPHONY_LOGE("event is nullptr!");
497         return;
498     }
499     event->SetOwner(shared_from_this());
500     NcfgOperatorInfo requireInfo;
501     requireInfo.state = state;
502     requireInfo.operName = operName;
503     requireInfo.operKey = operKey;
504     auto telRilManager = telRilManager_.lock();
505     if (telRilManager == nullptr) {
506         TELEPHONY_LOGE("SimStateHandle::SendSimMatchedOperatorInfo() telRilManager is nullptr!!");
507         return;
508     }
509     telRilManager->SendSimMatchedOperatorInfo(slotId, requireInfo, event);
510 }
511 
GetAidByCardType(CardType type)512 std::string SimStateHandle::GetAidByCardType(CardType type)
513 {
514     switch (type) {
515         case CardType::SINGLE_MODE_RUIM_CARD:
516             return CDMA_FAKE_AID;
517         case CardType::SINGLE_MODE_SIM_CARD:
518             [[fallthrough]]; // fall_through
519         case CardType::DUAL_MODE_CG_CARD:
520             [[fallthrough]]; // fall_through
521         case CardType::CT_NATIONAL_ROAMING_CARD:
522             [[fallthrough]]; // fall_through
523         case CardType::CU_DUAL_MODE_CARD:
524             [[fallthrough]]; // fall_through
525         case CardType::DUAL_MODE_TELECOM_LTE_CARD:
526             [[fallthrough]]; // fall_through
527         case CardType::DUAL_MODE_UG_CARD:
528             return GSM_FAKE_AID;
529         default:
530             break;
531     }
532     return USIM_AID;
533 }
534 
GetSimCardData(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)535 void SimStateHandle::GetSimCardData(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
536 {
537     TELEPHONY_LOGD("SimStateHandle::GetSimCardData slotId = %{public}d", slotId);
538     int32_t error = 0;
539     IccState iccState;
540     std::shared_ptr<SimCardStatusInfo> param = event->GetSharedObject<SimCardStatusInfo>();
541     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
542     if ((param == nullptr) && (response == nullptr)) {
543         TELEPHONY_LOGE("SimStateHandle::GetSimCardData() fail");
544         return;
545     }
546     if (param != nullptr) {
547         iccState.simType_ = param->simType;
548         iccState.simStatus_ = param->simState;
549         iccState.iccid_ = param->iccid;
550         modemInitDone_ = true;
551         TELEPHONY_LOGI("SimStateHandle::GetSimCardData(), slot%{public}d, type = %{public}d, status = %{public}d",
552             slotId, iccState.simType_, iccState.simStatus_);
553     } else {
554         error = static_cast<int32_t>(response->error);
555         TELEPHONY_LOGI("SimStateHandle::GetSimCardData(), slot%{public}d, error = %{public}d", slotId, error);
556 
557         /* In the dual-modem scenario, when SkyTone is enabled,
558          * the card slot may not be on any modem due to the card slot switchover command such as SCICHG.
559          * As a result, the card status is incorrectly obtained.
560          * Therefore, if the card status is incorrectly obtained, the card cannot be detected.
561          * Otherwise, the card status cannot be updated because the card slot is not in any modem.
562          */
563         if (VSIM_MODEM_COUNT != DUAL_SLOT_COUNT) {
564             return;
565         }
566         if (TELEPHONY_EXT_WRAPPER.isVSimEnabled_ &&
567             TELEPHONY_EXT_WRAPPER.isVSimEnabled_() &&
568             slotId != static_cast<int32_t>(SimSlotType::VSIM_SLOT_ID)) {
569             iccState.simType_ = oldSimType_;
570             iccState.simStatus_ = ICC_CARD_ABSENT;
571             iccState.iccid_ = iccid_;
572         } else {
573             return;
574         }
575     }
576     ProcessIccCardState(iccState, slotId);
577 }
578 
GetSimLockState(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)579 void SimStateHandle::GetSimLockState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
580 {
581     TELEPHONY_LOGI("SimStateHandle::GetSimLockState slotId = %{public}d", slotId);
582     int32_t error = 0;
583     std::shared_ptr<int32_t> param = event->GetSharedObject<int32_t>();
584     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
585     if ((param == nullptr) && (response == nullptr)) {
586         TELEPHONY_LOGE("SimStateHandle::GetSimLockState() fail");
587         return;
588     }
589     if (param != nullptr) {
590         TELEPHONY_LOGI("SimStateHandle::GetSimLockState(), param = %{public}d", *param);
591         unlockRespon_.lockState = *param;
592     } else {
593         unlockRespon_.lockState = static_cast<int32_t>(LockState::LOCK_ERROR);
594         error = static_cast<int32_t>(response->error);
595         TELEPHONY_LOGI("SimStateHandle::GetSimLockState(), error = %{public}d", error);
596     }
597     TELEPHONY_LOGI("SimStateHandle::GetSimLockState(), lockState = %{public}d", unlockRespon_.lockState);
598 }
599 
GetSetLockResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)600 void SimStateHandle::GetSetLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
601 {
602     TELEPHONY_LOGI("SimStateHandle::GetSetLockResult slotId = %{public}d", slotId);
603     std::shared_ptr<LockStatusResp> param = event->GetSharedObject<LockStatusResp>();
604     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
605     if ((param == nullptr) && (response == nullptr)) {
606         TELEPHONY_LOGE("SimStateHandle::GetSetLockResult() fail");
607         return;
608     }
609     if (param != nullptr) {
610         unlockRespon_.result = param->result;
611         unlockRespon_.remain = param->remain;
612         TELEPHONY_LOGI("SimStateHandle::GetSetLockResult result = %{public}d, remain = %{public}d",
613             unlockRespon_.result, unlockRespon_.remain);
614     } else {
615         unlockRespon_.result = static_cast<int32_t>(response->error);
616     }
617     TELEPHONY_LOGI("SimStateHandle::GetSetLockResult(), iccUnlockResponse = %{public}d", unlockRespon_.result);
618 }
619 
GetUnlockResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)620 void SimStateHandle::GetUnlockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
621 {
622     TELEPHONY_LOGI("SimStateHandle::GetUnlockResult slotId = %{public}d", slotId);
623     std::shared_ptr<LockStatusResp> param = event->GetSharedObject<LockStatusResp>();
624     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
625     if ((param == nullptr) && (response == nullptr)) {
626         TELEPHONY_LOGE("SimStateHandle::GetSimUnlockResult() fail");
627         return;
628     }
629     if (param != nullptr) {
630         unlockRespon_.result = param->result;
631         unlockRespon_.remain = param->remain;
632         TELEPHONY_LOGE("SimStateHandle::GetUnlockReult result:%{public}d, remain:%{public}d",
633             param->result, param->remain);
634     } else {
635         TELEPHONY_LOGE("SimStateHandle::GetUnlockResult param is null");
636         unlockRespon_.result = static_cast<int32_t>(response->error);
637     }
638     TELEPHONY_LOGI("SimStateHandle::GetSimUnlockResponse(), iccUnlockResponse = %{public}d", unlockRespon_.result);
639 }
640 
GetUnlockSimLockResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)641 void SimStateHandle::GetUnlockSimLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
642 {
643     TELEPHONY_LOGI("SimStateHandle::GetUnlockSimLockResult slotId = %{public}d", slotId);
644     std::shared_ptr<LockStatusResp> param = event->GetSharedObject<LockStatusResp>();
645     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
646     if ((param == nullptr) && (response == nullptr)) {
647         TELEPHONY_LOGE("SimStateHandle::GetUnlockSimLockResult() fail");
648         return;
649     }
650     if (param != nullptr) {
651         TELEPHONY_LOGI("SimStateHandle::GetUnlockSimLockResult param is true");
652         simlockRespon_.result = param->result;
653         simlockRespon_.remain = param->remain;
654     } else {
655         TELEPHONY_LOGE("SimStateHandle::GetUnlockSimLockResult param is null");
656         simlockRespon_.result = static_cast<int32_t>(response->error);
657     }
658 }
659 
GetSimAuthenticationResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)660 void SimStateHandle::GetSimAuthenticationResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
661 {
662     TELEPHONY_LOGI("SimStateHandle::GetSimAuthenticationResult slotId = %{public}d", slotId);
663     auto response = event->GetSharedObject<IccIoResultInfo>();
664     if (response == nullptr) {
665         TELEPHONY_LOGE("SimStateHandle::GetSimAuthenticationResult() fail");
666         return;
667     }
668     simAuthRespon_.sw1 = response->sw1;
669     simAuthRespon_.sw2 = response->sw2;
670     simAuthRespon_.response = response->response;
671 }
672 
GetSendSimMatchedOperatorInfoResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)673 void SimStateHandle::GetSendSimMatchedOperatorInfoResult(
674     int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
675 {
676     TELEPHONY_LOGI("SimStateHandle::GetSendSimMatchedOperatorInfoResult slotId = %{public}d", slotId);
677     std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
678     if (response == nullptr) {
679         TELEPHONY_LOGE("SimStateHandle::GetSendSimMatchedOperatorInfoResult() fail");
680         return;
681     }
682     sendSimMatchedOperatorInfoResult_ = static_cast<int32_t>(response->error);
683 }
684 
GetSimAuthenticationResponse()685 SimAuthenticationResponse SimStateHandle::GetSimAuthenticationResponse()
686 {
687     return simAuthRespon_;
688 }
689 
GetSendSimMatchedOperatorInfoResponse()690 int32_t SimStateHandle::GetSendSimMatchedOperatorInfoResponse()
691 {
692     return sendSimMatchedOperatorInfoResult_;
693 }
694 
GetUnlockData()695 UnlockData SimStateHandle::GetUnlockData()
696 {
697     return unlockRespon_;
698 }
699 
GetSimlockResponse()700 LockStatusResponse SimStateHandle::GetSimlockResponse()
701 {
702     return simlockRespon_;
703 }
704 
PublishSimStateEvent(std::string event,int32_t eventCode,std::string eventData)705 bool SimStateHandle::PublishSimStateEvent(std::string event, int32_t eventCode, std::string eventData)
706 {
707     AAFwk::Want want;
708     want.SetAction(event);
709     EventFwk::CommonEventData data;
710     data.SetWant(want);
711     data.SetCode(eventCode);
712     data.SetData(eventData);
713     EventFwk::CommonEventPublishInfo publishInfo;
714     publishInfo.SetOrdered(false);
715     publishInfo.SetSticky(true);
716     bool publishResult = CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
717     TELEPHONY_LOGI("SimStateHandle::PublishSimStateEvent result : %{public}d", publishResult);
718     return publishResult;
719 }
720 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)721 void SimStateHandle::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
722 {
723     if (event == nullptr) {
724         TELEPHONY_LOGE("event is nullptr!");
725         return;
726     }
727     uint32_t eventId = event->GetInnerEventId();
728     TELEPHONY_LOGD("SimStateHandle::ProcessEvent(), eventId = %{public}d, slotId_ = %{public}d", eventId, slotId_);
729     auto simStateManager = simStateManager_.lock();
730     if (simStateManager == nullptr) {
731         TELEPHONY_LOGE("simStateManager nullptr");
732         return;
733     }
734     auto itFunc = memberFuncMap_.find(eventId);
735     if (itFunc != memberFuncMap_.end()) {
736         auto memberFunc = itFunc->second;
737         if (memberFunc != nullptr) {
738             memberFunc(this, slotId_, event);
739         }
740         if (eventId == MSG_SIM_SEND_NCFG_OPER_INFO_DONE) {
741             simStateManager->SyncSimMatchResponse();
742         } else if (eventId == MSG_SIM_UNLOCK_PIN_DONE) {
743             simStateManager->SyncUnlockPinResponse();
744         } else {
745             simStateManager->SyncCmdResponse();
746         }
747         return;
748     }
749 
750     switch (eventId) {
751         case RadioEvent::RADIO_STATE_CHANGED:
752             if (IsRadioStateUnavailable(event)) {
753                 break;
754             }
755 #ifdef CORE_SERVICE_SUPPORT_ESIM
756             UpdateEsimOSVersion(slotId_);
757 #endif
758             [[fallthrough]]; // fall_through
759         case RadioEvent::RADIO_SIM_STATE_CHANGE:
760             ObtainIccStatus(slotId_);
761             break;
762         case MSG_SIM_GET_ICC_STATUS_DONE:
763             simStateManager->SyncSimStateResponse();
764             GetSimCardData(slotId_, event);
765             break;
766         default:
767             TELEPHONY_LOGI("SimStateHandle::ProcessEvent(), unknown event");
768             break;
769     }
770 }
771 
ConnectService()772 bool SimStateHandle::ConnectService()
773 {
774     auto systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
775     if (systemManager == nullptr) {
776         TELEPHONY_LOGE("SimStateHandle::ConnectService() GetSystemAbilityManager null");
777         return false;
778     }
779     sptr<IRemoteObject> object = systemManager->GetSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
780     if (object == nullptr) {
781         TELEPHONY_LOGE("SimStateHandle::ConnectService faild");
782         return false;
783     }
784     TELEPHONY_LOGI("SimStateHandle::ConnectService success");
785     return true;
786 }
787 
IsIccReady()788 bool SimStateHandle::IsIccReady()
789 {
790     return externalState_ == SimState::SIM_STATE_READY;
791 }
792 
IsIccLocked()793 bool SimStateHandle::IsIccLocked()
794 {
795     return externalState_ == SimState::SIM_STATE_LOCKED;
796 }
797 
SimStateEscape(int32_t simState,int slotId,LockReason & reason)798 void SimStateHandle::SimStateEscape(
799     int32_t simState, int slotId, LockReason &reason)
800 {
801     switch (simState) {
802         case ICC_CARD_ABSENT:
803             externalState_ = SimState::SIM_STATE_NOT_PRESENT;
804             PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_ABSENT, "");
805             break;
806         case ICC_CONTENT_READY:
807             externalState_ = SimState::SIM_STATE_READY;
808             observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_READY, slotId);
809             PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_READY, "");
810             break;
811         case ICC_CONTENT_PIN:
812             externalState_ = SimState::SIM_STATE_LOCKED;
813             reason = LockReason::SIM_PIN;
814             observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_LOCKED, slotId);
815             PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_PIN, "");
816             break;
817         case ICC_CONTENT_PUK:
818             externalState_ = SimState::SIM_STATE_LOCKED;
819             reason = LockReason::SIM_PUK;
820             observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_LOCKED, slotId);
821             PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_PUK, "");
822             break;
823         default:
824             SimLockStateEscape(simState, slotId, reason);
825             break;
826     }
827     CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
828 }
829 
SimLockStateEscape(int32_t simState,int slotId,LockReason & reason)830 void SimStateHandle::SimLockStateEscape(
831     int32_t simState, int slotId, LockReason &reason)
832 {
833     bool isSimLockState = true;
834     switch (simState) {
835         case ICC_CONTENT_PH_NET_PIN:
836             reason = LockReason::SIM_PN_PIN;
837             break;
838         case ICC_CONTENT_PH_NET_PUK:
839             reason = LockReason::SIM_PN_PUK;
840             break;
841         case ICC_CONTENT_PH_NET_SUB_PIN:
842             reason = LockReason::SIM_PU_PIN;
843             break;
844         case ICC_CONTENT_PH_NET_SUB_PUK:
845             reason = LockReason::SIM_PU_PUK;
846             break;
847         case ICC_CONTENT_PH_SP_PIN:
848             reason = LockReason::SIM_PP_PIN;
849             break;
850         case ICC_CONTENT_PH_SP_PUK:
851             reason = LockReason::SIM_PP_PUK;
852             break;
853         case ICC_CONTENT_UNKNOWN:
854             [[fallthrough]]; // fall_through
855         default:
856             isSimLockState = false;
857             externalState_ = SimState::SIM_STATE_UNKNOWN;
858             CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
859             PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED,
860                 ICC_STATE_NOT_READY, "");
861             break;
862     }
863     if (isSimLockState) {
864         NotifySimLock(slotId);
865     }
866 }
867 
NotifySimLock(int slotId)868 void SimStateHandle::NotifySimLock(int slotId)
869 {
870     externalState_ = SimState::SIM_STATE_LOCKED;
871     CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
872     observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_SIMLOCK);
873     PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_SIMLOCK, "");
874 }
875 
CardTypeEscape(int32_t simType,int slotId)876 void SimStateHandle::CardTypeEscape(int32_t simType, int slotId)
877 {
878     CardType cardTypeStorage = externalType_;
879     TELEPHONY_LOGI("SimStateHandle::CardTypeEscape() simType=%{public}d, slotId = %{public}d", simType, slotId);
880     switch (simType) {
881         case ICC_UNKNOWN_TYPE:
882             externalType_ = CardType::UNKNOWN_CARD;
883             break;
884         case ICC_SIM_TYPE:
885             externalType_ = CardType::SINGLE_MODE_SIM_CARD;
886             break;
887         case ICC_USIM_TYPE:
888             externalType_ = CardType::SINGLE_MODE_USIM_CARD;
889             break;
890         case ICC_RUIM_TYPE:
891             externalType_ = CardType::SINGLE_MODE_RUIM_CARD;
892             break;
893         case ICC_CG_TYPE:
894             externalType_ = CardType::DUAL_MODE_CG_CARD;
895             break;
896         case ICC_DUAL_MODE_ROAMING_TYPE:
897             externalType_ = CardType::CT_NATIONAL_ROAMING_CARD;
898             break;
899         case ICC_UNICOM_DUAL_MODE_TYPE:
900             externalType_ = CardType::CU_DUAL_MODE_CARD;
901             break;
902         case ICC_4G_LTE_TYPE:
903             externalType_ = CardType::DUAL_MODE_TELECOM_LTE_CARD;
904             break;
905         case ICC_UG_TYPE:
906             externalType_ = CardType::DUAL_MODE_UG_CARD;
907             break;
908         case ICC_IMS_TYPE:
909             externalType_ = CardType::SINGLE_MODE_ISIM_CARD;
910             break;
911         default:
912             externalType_ = CardType::UNKNOWN_CARD;
913             break;
914     }
915     if (externalType_ != cardTypeStorage) {
916         TELEPHONY_LOGI("will to NotifyIccCardTypeChange at oldSimType[%{public}d] != newSimType[%{public}d]",
917             cardTypeStorage, externalType_);
918         observerHandler_->NotifyObserver(RadioEvent::RADIO_CARD_TYPE_CHANGE);
919     } else {
920         TELEPHONY_LOGI("do not NotifyIccCardTypeChange at oldSimType[%{public}d] == newSimType[%{public}d]",
921             cardTypeStorage, externalType_);
922     }
923 }
924 
RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)925 void SimStateHandle::RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
926 {
927     switch (what) {
928         case RadioEvent::RADIO_SIM_STATE_CHANGE:
929             TELEPHONY_LOGI("SimStateHandle::RegisterIccStateChanged()");
930             observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_CHANGE, handler);
931             break;
932         case RadioEvent::RADIO_SIM_STATE_READY:
933             TELEPHONY_LOGI("SimStateHandle::RegisterIccReady()");
934             observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_READY, handler);
935             if (IsIccReady() && handler != nullptr) {
936                 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_STATE_READY, slotId_, 0);
937                 TELEPHONY_LOGI("SimStateHandle::RegisterIccReady() OK send, slotId = %{public}d", slotId_);
938             }
939             break;
940         case RadioEvent::RADIO_SIM_STATE_LOCKED:
941             TELEPHONY_LOGI("SimStateHandle::RegisterIccLocked()");
942             observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_LOCKED, handler);
943             if (IsIccLocked() && handler != nullptr) {
944                 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_STATE_LOCKED, slotId_, 0);
945                 TELEPHONY_LOGI("SimStateHandle::RegisterIccLocked() OK send, slotId = %{public}d", slotId_);
946             }
947             break;
948         case RadioEvent::RADIO_SIM_STATE_SIMLOCK:
949             TELEPHONY_LOGI("SimStateHandle::RegisterIccSimLock()");
950             observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_SIMLOCK, handler);
951             break;
952         case RadioEvent::RADIO_CARD_TYPE_CHANGE:
953             TELEPHONY_LOGI("SimStateHandle::RegisterCardTypeChange()");
954             observerHandler_->RegObserver(RadioEvent::RADIO_CARD_TYPE_CHANGE, handler);
955             break;
956         case RadioEvent::RADIO_SIM_ICCID_LOADED:
957             TELEPHONY_LOGI("SimStateHandle::RegisterIccidLoaded()");
958             observerHandler_->RegObserver(RadioEvent::RADIO_SIM_ICCID_LOADED, handler);
959             if (!iccid_.empty() && handler != nullptr) {
960                 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_ICCID_LOADED, slotId_, 0);
961                 TELEPHONY_LOGI("SimStateHandle::RegisterIccidLoaded() OK send, slotId = %{public}d", slotId_);
962             }
963             break;
964         default:
965             TELEPHONY_LOGI("SimStateHandle RegisterCoreNotify do default");
966             break;
967     }
968 }
969 
UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)970 void SimStateHandle::UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
971 {
972     switch (what) {
973         case RadioEvent::RADIO_SIM_STATE_CHANGE:
974             TELEPHONY_LOGI("SimStateHandle::UnregisterIccStateChanged()");
975             observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_CHANGE, handler);
976             break;
977         case RadioEvent::RADIO_SIM_STATE_READY:
978             TELEPHONY_LOGI("SimStateHandle::UnregisterIccReady()");
979             observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_READY, handler);
980             break;
981         case RadioEvent::RADIO_SIM_STATE_LOCKED:
982             TELEPHONY_LOGI("SimStateHandle::UnregisterIccLocked()");
983             observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_LOCKED, handler);
984             break;
985         case RadioEvent::RADIO_SIM_STATE_SIMLOCK:
986             TELEPHONY_LOGI("SimStateHandle::UnregisterIccSimLock()");
987             observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_SIMLOCK, handler);
988             break;
989         case RadioEvent::RADIO_CARD_TYPE_CHANGE:
990             TELEPHONY_LOGI("SimStateHandle::RegisterCardTypeChange()");
991             observerHandler_->Remove(RadioEvent::RADIO_CARD_TYPE_CHANGE, handler);
992             break;
993         default:
994             TELEPHONY_LOGI("SimStateHandle UnRegisterCoreNotify do default");
995             break;
996     }
997 }
998 
IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer & event)999 bool SimStateHandle::IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer &event)
1000 {
1001     std::shared_ptr<Int32Parcel> object = event->GetSharedObject<Int32Parcel>();
1002     if (object == nullptr) {
1003         TELEPHONY_LOGE("object is nullptr!");
1004         return false;
1005     }
1006     int32_t radioState = object->data;
1007     if (radioState == ModemPowerState::CORE_SERVICE_POWER_NOT_AVAILABLE) {
1008         TELEPHONY_LOGI("received radio unavailable");
1009         IccState iccState;
1010         iccState.simType_ = ICC_UNKNOWN_TYPE;
1011         iccState.simStatus_ = ICC_CONTENT_UNKNOWN;
1012         modemInitDone_ = false;
1013         ProcessIccCardState(iccState, slotId_);
1014         return true;
1015     }
1016     return false;
1017 }
1018 
GetSimIO(int32_t slotId,SimIoRequestInfo requestInfo)1019 int32_t SimStateHandle::GetSimIO(int32_t slotId, SimIoRequestInfo requestInfo)
1020 {
1021     auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_GET_SIM_IO_DONE);
1022     if (event == nullptr) {
1023         TELEPHONY_LOGE("event is nullptr!");
1024         return SIM_AUTH_FAIL;
1025     }
1026     event->SetOwner(shared_from_this());
1027     auto telRilManager = telRilManager_.lock();
1028     if (telRilManager == nullptr) {
1029         TELEPHONY_LOGE("SimStateHandle::GetSimIO() telRilManager is nullptr!!");
1030         return SIM_AUTH_FAIL;
1031     }
1032     return telRilManager->GetSimIO(slotId, requestInfo, event);
1033 }
1034 
GetSimIOResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)1035 void SimStateHandle::GetSimIOResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
1036 {
1037     TELEPHONY_LOGI("SimStateHandle::GetSimIOResult slotId = %{public}d", slotId);
1038     auto response = event->GetSharedObject<IccIoResultInfo>();
1039     if (response == nullptr) {
1040         TELEPHONY_LOGE("SimStateHandle::GetSimIOResult() fail");
1041         return;
1042     }
1043     simIORespon_.sw1 = response->sw1;
1044     simIORespon_.sw2 = response->sw2;
1045     simIORespon_.response = response->response;
1046 }
1047 
GetSimIOResponse()1048 SimAuthenticationResponse SimStateHandle::GetSimIOResponse()
1049 {
1050     return simIORespon_;
1051 }
1052 
1053 #ifdef CORE_SERVICE_SUPPORT_ESIM
UpdateEsimOSVersion(int32_t slotId)1054 void SimStateHandle::UpdateEsimOSVersion(int32_t slotId)
1055 {
1056     bool result = DelayedRefSingleton<EsimServiceClient>::GetInstance().IsSupported(slotId_);
1057     if (result == TELEPHONY_ERR_SUCCESS) {
1058         std::unique_ptr<StartOsuResultCallback> callback = std::make_unique<StartOsuResultCallback>(0);
1059         int32_t updateResult =
1060             DelayedRefSingleton<EsimServiceClient>::GetInstance().StartOsu(slotId_, callback.release());
1061         if (updateResult == TELEPHONY_ERR_SUCCESS) {
1062             TELEPHONY_LOGI("StartOsu success");
1063         } else {
1064             TELEPHONY_LOGE("StartOsu fail, updateResult: %{public}d", updateResult);
1065         }
1066     }
1067 }
1068 #endif
1069 } // namespace Telephony
1070 } // namespace OHOS
1071