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,
__anonafe3af4b0102() 50 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
51 handle->GetUnlockResult(slotId, event);
52 } },
53 { MSG_SIM_UNLOCK_PUK_DONE,
__anonafe3af4b0202() 54 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
55 handle->GetUnlockResult(slotId, event);
56 } },
57 { MSG_SIM_CHANGE_PIN_DONE,
__anonafe3af4b0302() 58 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
59 handle->GetUnlockResult(slotId, event);
60 } },
61 { MSG_SIM_UNLOCK_PIN2_DONE,
__anonafe3af4b0402() 62 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
63 handle->GetUnlockResult(slotId, event);
64 } },
65 { MSG_SIM_UNLOCK_PUK2_DONE,
__anonafe3af4b0502() 66 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
67 handle->GetUnlockResult(slotId, event);
68 } },
69 { MSG_SIM_CHANGE_PIN2_DONE,
__anonafe3af4b0602() 70 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
71 handle->GetUnlockResult(slotId, event);
72 } },
73 { MSG_SIM_UNLOCK_SIMLOCK_DONE,
__anonafe3af4b0702() 74 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
75 handle->GetUnlockSimLockResult(slotId, event);
76 } },
77 { MSG_SIM_ENABLE_PIN_DONE,
__anonafe3af4b0802() 78 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
79 handle->GetSetLockResult(slotId, event);
80 } },
81 { MSG_SIM_CHECK_PIN_DONE,
__anonafe3af4b0902() 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,
__anonafe3af4b0a02() 86 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
87 handle->GetSimCardData(slotId, event);
88 } },
89 { MSG_SIM_AUTHENTICATION_DONE,
__anonafe3af4b0b02() 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,
__anonafe3af4b0c02() 94 [](SimStateHandle *handle, int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event) {
95 handle->GetSendSimMatchedOperatorInfoResult(slotId, event);
96 } },
97 { MSG_SIM_GET_SIM_IO_DONE,
__anonafe3af4b0d02() 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 // When esim switches from one active profile to another, the card only report a ready status
384 // and we can only detect the change by comparing the iccid before and after
385 if ((oldSimStatus_ != newSimStatus) || (iccid_ != ar.iccid_)) {
386 iccid_ = ar.iccid_;
387 SimStateEscape(newSimStatus, slotId, reason);
388 oldSimStatus_ = newSimStatus;
389 iter = simIccStatusMap_.find(newSimStatus);
390 TELEPHONY_LOGI("will to NotifyIccStateChanged at newSimStatus[%{public}s]"
391 "(%{public}d) observerHandler_ is nullptr[%{public}d] ",
392 iter->second.c_str(), newSimStatus, (observerHandler_ == nullptr));
393 if (newSimStatus == ICC_CARD_ABSENT) {
394 TELEPHONY_LOGI("SimStateHandle::ProcessIccCardState slotId: %{public}d ICC_CARD_ABSENT", slotId);
395 CoreManagerInner::GetInstance().ResetSimLoadAccount(slotId);
396 if (TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_ != nullptr) {
397 TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_(
398 slotId, GetIccid(), PinOperationType::SIM_ABSENT, "");
399 }
400 }
401 if (observerHandler_ != nullptr) {
402 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_CHANGE, slotId);
403 if (!iccid_.empty()) {
404 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_ICCID_LOADED, slotId);
405 }
406 }
407 DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateSimState(
408 slotId, externalType_, externalState_, reason);
409 if (TELEPHONY_EXT_WRAPPER.updateHotPlugCardState_ != nullptr) {
410 TELEPHONY_EXT_WRAPPER.updateHotPlugCardState_(slotId, externalState_);
411 }
412 }
413 }
414
UnInit()415 void SimStateHandle::UnInit()
416 {
417 auto telRilManager = telRilManager_.lock();
418 if (telRilManager != nullptr) {
419 telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
420 telRilManager->UnRegisterCoreNotify(slotId_, shared_from_this(), RadioEvent::RADIO_STATE_CHANGED);
421 }
422 if (IsSatelliteSupported() == static_cast<int32_t>(SatelliteValue::SATELLITE_SUPPORTED) &&
423 satelliteCallback_ != nullptr) {
424 std::shared_ptr<SatelliteServiceClient> satelliteClient =
425 DelayedSingleton<SatelliteServiceClient>::GetInstance();
426 satelliteClient->UnRegisterCoreNotify(slotId_, RadioEvent::RADIO_SIM_STATE_CHANGE);
427 }
428 }
429
RegisterSatelliteCallback()430 void SimStateHandle::RegisterSatelliteCallback()
431 {
432 satelliteCallback_ =
433 std::make_unique<SatelliteCoreCallback>(std::static_pointer_cast<TelEventHandler>(shared_from_this()))
434 .release();
435 std::shared_ptr<SatelliteServiceClient> satelliteClient = DelayedSingleton<SatelliteServiceClient>::GetInstance();
436 satelliteClient->RegisterCoreNotify(slotId_, RadioEvent::RADIO_SIM_STATE_CHANGE, satelliteCallback_);
437 }
438
UnregisterSatelliteCallback()439 void SimStateHandle::UnregisterSatelliteCallback()
440 {
441 if (IsSatelliteSupported() == static_cast<int32_t>(SatelliteValue::SATELLITE_SUPPORTED)) {
442 satelliteCallback_ = nullptr;
443 }
444 }
445
ObtainIccStatus(int32_t slotId)446 void SimStateHandle::ObtainIccStatus(int32_t slotId)
447 {
448 TELEPHONY_LOGI("SimStateHandle::ObtainIccStatus() slotId = %{public}d", slotId);
449 auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_GET_ICC_STATUS_DONE);
450 if (event == nullptr) {
451 TELEPHONY_LOGE("event is nullptr!");
452 return;
453 }
454 event->SetOwner(shared_from_this());
455 auto telRilManager = telRilManager_.lock();
456 if (telRilManager == nullptr) {
457 TELEPHONY_LOGE("telRilManager is nullptr!");
458 return;
459 }
460 telRilManager->GetSimStatus(slotId, event); // get sim card state
461 }
462
ObtainRealtimeIccStatus(int32_t slotId)463 void SimStateHandle::ObtainRealtimeIccStatus(int32_t slotId)
464 {
465 auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_GET_REALTIME_ICC_STATUS_DONE);
466 if (event == nullptr) {
467 TELEPHONY_LOGE("event is nullptr!");
468 return;
469 }
470 event->SetOwner(shared_from_this());
471 auto telRilManager = telRilManager_.lock();
472 if (telRilManager == nullptr) {
473 TELEPHONY_LOGE("telRilManager is nullptr!");
474 return;
475 }
476 telRilManager->GetSimStatus(slotId, event); // get sim card state
477 }
478
SimAuthentication(int32_t slotId,AuthType authType,const std::string & authData)479 int32_t SimStateHandle::SimAuthentication(int32_t slotId, AuthType authType, const std::string &authData)
480 {
481 auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_AUTHENTICATION_DONE);
482 if (event == nullptr) {
483 TELEPHONY_LOGE("event is nullptr!");
484 return SIM_AUTH_FAIL;
485 }
486 event->SetOwner(shared_from_this());
487 SimAuthenticationRequestInfo requireInfo;
488 requireInfo.serial = static_cast<int32_t>(authType);
489 requireInfo.aid = GetAidByCardType(externalType_);
490 requireInfo.authData = authData;
491 auto telRilManager = telRilManager_.lock();
492 if (telRilManager == nullptr) {
493 TELEPHONY_LOGE("SimStateHandle::SimAuthentication() telRilManager is nullptr!!");
494 return SIM_AUTH_FAIL;
495 }
496 return telRilManager->SimAuthentication(slotId, requireInfo, event);
497 }
498
SendSimMatchedOperatorInfo(int32_t slotId,int32_t state,const std::string & operName,const std::string & operKey)499 void SimStateHandle::SendSimMatchedOperatorInfo(
500 int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey)
501 {
502 auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_SEND_NCFG_OPER_INFO_DONE);
503 if (event == nullptr) {
504 TELEPHONY_LOGE("event is nullptr!");
505 return;
506 }
507 event->SetOwner(shared_from_this());
508 NcfgOperatorInfo requireInfo;
509 requireInfo.state = state;
510 requireInfo.operName = operName;
511 requireInfo.operKey = operKey;
512 auto telRilManager = telRilManager_.lock();
513 if (telRilManager == nullptr) {
514 TELEPHONY_LOGE("SimStateHandle::SendSimMatchedOperatorInfo() telRilManager is nullptr!!");
515 return;
516 }
517 telRilManager->SendSimMatchedOperatorInfo(slotId, requireInfo, event);
518 }
519
GetAidByCardType(CardType type)520 std::string SimStateHandle::GetAidByCardType(CardType type)
521 {
522 switch (type) {
523 case CardType::SINGLE_MODE_RUIM_CARD:
524 return CDMA_FAKE_AID;
525 case CardType::SINGLE_MODE_SIM_CARD:
526 [[fallthrough]]; // fall_through
527 case CardType::DUAL_MODE_CG_CARD:
528 [[fallthrough]]; // fall_through
529 case CardType::CT_NATIONAL_ROAMING_CARD:
530 [[fallthrough]]; // fall_through
531 case CardType::CU_DUAL_MODE_CARD:
532 [[fallthrough]]; // fall_through
533 case CardType::DUAL_MODE_TELECOM_LTE_CARD:
534 [[fallthrough]]; // fall_through
535 case CardType::DUAL_MODE_UG_CARD:
536 return GSM_FAKE_AID;
537 default:
538 break;
539 }
540 return USIM_AID;
541 }
542
GetSimCardData(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)543 void SimStateHandle::GetSimCardData(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
544 {
545 TELEPHONY_LOGD("SimStateHandle::GetSimCardData slotId = %{public}d", slotId);
546 int32_t error = 0;
547 IccState iccState;
548 std::shared_ptr<SimCardStatusInfo> param = event->GetSharedObject<SimCardStatusInfo>();
549 std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
550 if ((param == nullptr) && (response == nullptr)) {
551 TELEPHONY_LOGE("SimStateHandle::GetSimCardData() fail");
552 return;
553 }
554 if (param != nullptr) {
555 iccState.simType_ = param->simType;
556 iccState.simStatus_ = param->simState;
557 iccState.iccid_ = param->iccid;
558 modemInitDone_ = true;
559 TELEPHONY_LOGI("SimStateHandle::GetSimCardData(), slot%{public}d, type = %{public}d, status = %{public}d",
560 slotId, iccState.simType_, iccState.simStatus_);
561 } else {
562 error = static_cast<int32_t>(response->error);
563 TELEPHONY_LOGI("SimStateHandle::GetSimCardData(), slot%{public}d, error = %{public}d", slotId, error);
564
565 /* In the dual-modem scenario, when SkyTone is enabled,
566 * the card slot may not be on any modem due to the card slot switchover command such as SCICHG.
567 * As a result, the card status is incorrectly obtained.
568 * Therefore, if the card status is incorrectly obtained, the card cannot be detected.
569 * Otherwise, the card status cannot be updated because the card slot is not in any modem.
570 */
571 if (VSIM_MODEM_COUNT != DUAL_SLOT_COUNT) {
572 return;
573 }
574 if (TELEPHONY_EXT_WRAPPER.isVSimEnabled_ &&
575 TELEPHONY_EXT_WRAPPER.isVSimEnabled_() &&
576 slotId != static_cast<int32_t>(SimSlotType::VSIM_SLOT_ID)) {
577 iccState.simType_ = oldSimType_;
578 iccState.simStatus_ = ICC_CARD_ABSENT;
579 iccState.iccid_ = iccid_;
580 } else {
581 return;
582 }
583 }
584 ProcessIccCardState(iccState, slotId);
585 }
586
GetSimLockState(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)587 void SimStateHandle::GetSimLockState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
588 {
589 TELEPHONY_LOGI("SimStateHandle::GetSimLockState slotId = %{public}d", slotId);
590 int32_t error = 0;
591 std::shared_ptr<int32_t> param = event->GetSharedObject<int32_t>();
592 std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
593 if ((param == nullptr) && (response == nullptr)) {
594 TELEPHONY_LOGE("SimStateHandle::GetSimLockState() fail");
595 return;
596 }
597 if (param != nullptr) {
598 TELEPHONY_LOGI("SimStateHandle::GetSimLockState(), param = %{public}d", *param);
599 unlockRespon_.lockState = *param;
600 } else {
601 unlockRespon_.lockState = static_cast<int32_t>(LockState::LOCK_ERROR);
602 error = static_cast<int32_t>(response->error);
603 TELEPHONY_LOGI("SimStateHandle::GetSimLockState(), error = %{public}d", error);
604 }
605 TELEPHONY_LOGI("SimStateHandle::GetSimLockState(), lockState = %{public}d", unlockRespon_.lockState);
606 }
607
GetSetLockResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)608 void SimStateHandle::GetSetLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
609 {
610 TELEPHONY_LOGI("SimStateHandle::GetSetLockResult slotId = %{public}d", slotId);
611 std::shared_ptr<LockStatusResp> param = event->GetSharedObject<LockStatusResp>();
612 std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
613 if ((param == nullptr) && (response == nullptr)) {
614 TELEPHONY_LOGE("SimStateHandle::GetSetLockResult() fail");
615 return;
616 }
617 if (param != nullptr) {
618 unlockRespon_.result = param->result;
619 unlockRespon_.remain = param->remain;
620 TELEPHONY_LOGI("SimStateHandle::GetSetLockResult result = %{public}d, remain = %{public}d",
621 unlockRespon_.result, unlockRespon_.remain);
622 } else {
623 unlockRespon_.result = static_cast<int32_t>(response->error);
624 }
625 TELEPHONY_LOGI("SimStateHandle::GetSetLockResult(), iccUnlockResponse = %{public}d", unlockRespon_.result);
626 }
627
GetUnlockResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)628 void SimStateHandle::GetUnlockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
629 {
630 TELEPHONY_LOGI("SimStateHandle::GetUnlockResult slotId = %{public}d", slotId);
631 std::shared_ptr<LockStatusResp> param = event->GetSharedObject<LockStatusResp>();
632 std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
633 if ((param == nullptr) && (response == nullptr)) {
634 TELEPHONY_LOGE("SimStateHandle::GetSimUnlockResult() fail");
635 return;
636 }
637 if (param != nullptr) {
638 unlockRespon_.result = param->result;
639 unlockRespon_.remain = param->remain;
640 TELEPHONY_LOGE("SimStateHandle::GetUnlockReult result:%{public}d, remain:%{public}d",
641 param->result, param->remain);
642 } else {
643 TELEPHONY_LOGE("SimStateHandle::GetUnlockResult param is null");
644 unlockRespon_.result = static_cast<int32_t>(response->error);
645 }
646 TELEPHONY_LOGI("SimStateHandle::GetSimUnlockResponse(), iccUnlockResponse = %{public}d", unlockRespon_.result);
647 }
648
GetUnlockSimLockResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)649 void SimStateHandle::GetUnlockSimLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
650 {
651 TELEPHONY_LOGI("SimStateHandle::GetUnlockSimLockResult slotId = %{public}d", slotId);
652 std::shared_ptr<LockStatusResp> param = event->GetSharedObject<LockStatusResp>();
653 std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
654 if ((param == nullptr) && (response == nullptr)) {
655 TELEPHONY_LOGE("SimStateHandle::GetUnlockSimLockResult() fail");
656 return;
657 }
658 if (param != nullptr) {
659 TELEPHONY_LOGI("SimStateHandle::GetUnlockSimLockResult param is true");
660 simlockRespon_.result = param->result;
661 simlockRespon_.remain = param->remain;
662 } else {
663 TELEPHONY_LOGE("SimStateHandle::GetUnlockSimLockResult param is null");
664 simlockRespon_.result = static_cast<int32_t>(response->error);
665 }
666 }
667
GetSimAuthenticationResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)668 void SimStateHandle::GetSimAuthenticationResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
669 {
670 TELEPHONY_LOGI("SimStateHandle::GetSimAuthenticationResult slotId = %{public}d", slotId);
671 auto response = event->GetSharedObject<IccIoResultInfo>();
672 if (response == nullptr) {
673 TELEPHONY_LOGE("SimStateHandle::GetSimAuthenticationResult() fail");
674 return;
675 }
676 simAuthRespon_.sw1 = response->sw1;
677 simAuthRespon_.sw2 = response->sw2;
678 simAuthRespon_.response = response->response;
679 }
680
GetSendSimMatchedOperatorInfoResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)681 void SimStateHandle::GetSendSimMatchedOperatorInfoResult(
682 int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
683 {
684 TELEPHONY_LOGI("SimStateHandle::GetSendSimMatchedOperatorInfoResult slotId = %{public}d", slotId);
685 std::shared_ptr<RadioResponseInfo> response = event->GetSharedObject<RadioResponseInfo>();
686 if (response == nullptr) {
687 TELEPHONY_LOGE("SimStateHandle::GetSendSimMatchedOperatorInfoResult() fail");
688 return;
689 }
690 sendSimMatchedOperatorInfoResult_ = static_cast<int32_t>(response->error);
691 }
692
GetSimAuthenticationResponse()693 SimAuthenticationResponse SimStateHandle::GetSimAuthenticationResponse()
694 {
695 return simAuthRespon_;
696 }
697
GetSendSimMatchedOperatorInfoResponse()698 int32_t SimStateHandle::GetSendSimMatchedOperatorInfoResponse()
699 {
700 return sendSimMatchedOperatorInfoResult_;
701 }
702
GetUnlockData()703 UnlockData SimStateHandle::GetUnlockData()
704 {
705 return unlockRespon_;
706 }
707
GetSimlockResponse()708 LockStatusResponse SimStateHandle::GetSimlockResponse()
709 {
710 return simlockRespon_;
711 }
712
PublishSimStateEvent(std::string event,int32_t eventCode,std::string eventData)713 bool SimStateHandle::PublishSimStateEvent(std::string event, int32_t eventCode, std::string eventData)
714 {
715 AAFwk::Want want;
716 want.SetAction(event);
717 EventFwk::CommonEventData data;
718 data.SetWant(want);
719 data.SetCode(eventCode);
720 data.SetData(eventData);
721 EventFwk::CommonEventPublishInfo publishInfo;
722 publishInfo.SetOrdered(false);
723 publishInfo.SetSticky(true);
724 bool publishResult = CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
725 TELEPHONY_LOGI("SimStateHandle::PublishSimStateEvent result : %{public}d", publishResult);
726 return publishResult;
727 }
728
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)729 void SimStateHandle::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
730 {
731 if (event == nullptr) {
732 TELEPHONY_LOGE("event is nullptr!");
733 return;
734 }
735 uint32_t eventId = event->GetInnerEventId();
736 TELEPHONY_LOGD("SimStateHandle::ProcessEvent(), eventId = %{public}d, slotId_ = %{public}d", eventId, slotId_);
737 auto itFunc = memberFuncMap_.find(eventId);
738 if (itFunc != memberFuncMap_.end()) {
739 auto memberFunc = itFunc->second;
740 if (memberFunc != nullptr) {
741 memberFunc(this, slotId_, event);
742 }
743 auto simStateManager = simStateManager_.lock();
744 if (simStateManager == nullptr) {
745 TELEPHONY_LOGE("simStateManager nullptr");
746 return;
747 }
748 if (eventId == MSG_SIM_SEND_NCFG_OPER_INFO_DONE) {
749 simStateManager->SyncSimMatchResponse();
750 } else if (eventId == MSG_SIM_UNLOCK_PIN_DONE) {
751 simStateManager->SyncUnlockPinResponse();
752 } else {
753 simStateManager->SyncCmdResponse();
754 }
755 return;
756 }
757
758 switch (eventId) {
759 case RadioEvent::RADIO_STATE_CHANGED:
760 if (IsRadioStateUnavailable(event)) {
761 break;
762 }
763 [[fallthrough]]; // fall_through
764 case RadioEvent::RADIO_SIM_STATE_CHANGE:
765 ObtainIccStatus(slotId_);
766 break;
767 case MSG_SIM_GET_ICC_STATUS_DONE:
768 GetSimCardData(slotId_, event);
769 break;
770 default:
771 TELEPHONY_LOGI("SimStateHandle::ProcessEvent(), unknown event");
772 break;
773 }
774 }
775
ConnectService()776 bool SimStateHandle::ConnectService()
777 {
778 auto systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
779 if (systemManager == nullptr) {
780 TELEPHONY_LOGE("SimStateHandle::ConnectService() GetSystemAbilityManager null");
781 return false;
782 }
783 sptr<IRemoteObject> object = systemManager->GetSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
784 if (object == nullptr) {
785 TELEPHONY_LOGE("SimStateHandle::ConnectService faild");
786 return false;
787 }
788 TELEPHONY_LOGI("SimStateHandle::ConnectService success");
789 return true;
790 }
791
IsIccReady()792 bool SimStateHandle::IsIccReady()
793 {
794 return externalState_ == SimState::SIM_STATE_READY;
795 }
796
IsIccLocked()797 bool SimStateHandle::IsIccLocked()
798 {
799 return externalState_ == SimState::SIM_STATE_LOCKED;
800 }
801
SimStateEscape(int32_t simState,int slotId,LockReason & reason)802 void SimStateHandle::SimStateEscape(
803 int32_t simState, int slotId, LockReason &reason)
804 {
805 switch (simState) {
806 case ICC_CARD_ABSENT:
807 externalState_ = SimState::SIM_STATE_NOT_PRESENT;
808 PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_ABSENT, "");
809 break;
810 case ICC_CONTENT_READY:
811 externalState_ = SimState::SIM_STATE_READY;
812 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_READY, slotId);
813 PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_READY, "");
814 break;
815 case ICC_CONTENT_PIN:
816 externalState_ = SimState::SIM_STATE_LOCKED;
817 reason = LockReason::SIM_PIN;
818 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_LOCKED, slotId);
819 PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_PIN, "");
820 break;
821 case ICC_CONTENT_PUK:
822 externalState_ = SimState::SIM_STATE_LOCKED;
823 reason = LockReason::SIM_PUK;
824 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_LOCKED, slotId);
825 PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_PUK, "");
826 break;
827 default:
828 SimLockStateEscape(simState, slotId, reason);
829 break;
830 }
831 CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
832 }
833
SimLockStateEscape(int32_t simState,int slotId,LockReason & reason)834 void SimStateHandle::SimLockStateEscape(
835 int32_t simState, int slotId, LockReason &reason)
836 {
837 bool isSimLockState = true;
838 switch (simState) {
839 case ICC_CONTENT_PH_NET_PIN:
840 reason = LockReason::SIM_PN_PIN;
841 break;
842 case ICC_CONTENT_PH_NET_PUK:
843 reason = LockReason::SIM_PN_PUK;
844 break;
845 case ICC_CONTENT_PH_NET_SUB_PIN:
846 reason = LockReason::SIM_PU_PIN;
847 break;
848 case ICC_CONTENT_PH_NET_SUB_PUK:
849 reason = LockReason::SIM_PU_PUK;
850 break;
851 case ICC_CONTENT_PH_SP_PIN:
852 reason = LockReason::SIM_PP_PIN;
853 break;
854 case ICC_CONTENT_PH_SP_PUK:
855 reason = LockReason::SIM_PP_PUK;
856 break;
857 case ICC_CONTENT_UNKNOWN:
858 [[fallthrough]]; // fall_through
859 default:
860 isSimLockState = false;
861 externalState_ = SimState::SIM_STATE_UNKNOWN;
862 CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
863 PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED,
864 ICC_STATE_NOT_READY, "");
865 break;
866 }
867 if (isSimLockState) {
868 NotifySimLock(slotId);
869 }
870 }
871
NotifySimLock(int slotId)872 void SimStateHandle::NotifySimLock(int slotId)
873 {
874 externalState_ = SimState::SIM_STATE_LOCKED;
875 CoreServiceHiSysEvent::WriteSimStateBehaviorEvent(slotId, static_cast<int32_t>(externalState_));
876 observerHandler_->NotifyObserver(RadioEvent::RADIO_SIM_STATE_SIMLOCK);
877 PublishSimStateEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_SIMLOCK, "");
878 }
879
CardTypeEscape(int32_t simType,int slotId)880 void SimStateHandle::CardTypeEscape(int32_t simType, int slotId)
881 {
882 CardType cardTypeStorage = externalType_;
883 TELEPHONY_LOGI("SimStateHandle::CardTypeEscape() simType=%{public}d, slotId = %{public}d", simType, slotId);
884 switch (simType) {
885 case ICC_UNKNOWN_TYPE:
886 externalType_ = CardType::UNKNOWN_CARD;
887 break;
888 case ICC_SIM_TYPE:
889 externalType_ = CardType::SINGLE_MODE_SIM_CARD;
890 break;
891 case ICC_USIM_TYPE:
892 externalType_ = CardType::SINGLE_MODE_USIM_CARD;
893 break;
894 case ICC_RUIM_TYPE:
895 externalType_ = CardType::SINGLE_MODE_RUIM_CARD;
896 break;
897 case ICC_CG_TYPE:
898 externalType_ = CardType::DUAL_MODE_CG_CARD;
899 break;
900 case ICC_DUAL_MODE_ROAMING_TYPE:
901 externalType_ = CardType::CT_NATIONAL_ROAMING_CARD;
902 break;
903 case ICC_UNICOM_DUAL_MODE_TYPE:
904 externalType_ = CardType::CU_DUAL_MODE_CARD;
905 break;
906 case ICC_4G_LTE_TYPE:
907 externalType_ = CardType::DUAL_MODE_TELECOM_LTE_CARD;
908 break;
909 case ICC_UG_TYPE:
910 externalType_ = CardType::DUAL_MODE_UG_CARD;
911 break;
912 case ICC_IMS_TYPE:
913 externalType_ = CardType::SINGLE_MODE_ISIM_CARD;
914 break;
915 default:
916 externalType_ = CardType::UNKNOWN_CARD;
917 break;
918 }
919 if (externalType_ != cardTypeStorage) {
920 TELEPHONY_LOGI("will to NotifyIccCardTypeChange at oldSimType[%{public}d] != newSimType[%{public}d]",
921 cardTypeStorage, externalType_);
922 observerHandler_->NotifyObserver(RadioEvent::RADIO_CARD_TYPE_CHANGE);
923 } else {
924 TELEPHONY_LOGI("do not NotifyIccCardTypeChange at oldSimType[%{public}d] == newSimType[%{public}d]",
925 cardTypeStorage, externalType_);
926 }
927 }
928
RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)929 void SimStateHandle::RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
930 {
931 switch (what) {
932 case RadioEvent::RADIO_SIM_STATE_CHANGE:
933 TELEPHONY_LOGI("SimStateHandle::RegisterIccStateChanged()");
934 observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_CHANGE, handler);
935 break;
936 case RadioEvent::RADIO_SIM_STATE_READY:
937 TELEPHONY_LOGI("SimStateHandle::RegisterIccReady()");
938 observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_READY, handler);
939 if (IsIccReady() && handler != nullptr) {
940 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_STATE_READY, slotId_, 0);
941 TELEPHONY_LOGI("SimStateHandle::RegisterIccReady() OK send, slotId = %{public}d", slotId_);
942 }
943 break;
944 case RadioEvent::RADIO_SIM_STATE_LOCKED:
945 TELEPHONY_LOGI("SimStateHandle::RegisterIccLocked()");
946 observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_LOCKED, handler);
947 if (IsIccLocked() && handler != nullptr) {
948 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_STATE_LOCKED, slotId_, 0);
949 TELEPHONY_LOGI("SimStateHandle::RegisterIccLocked() OK send, slotId = %{public}d", slotId_);
950 }
951 break;
952 case RadioEvent::RADIO_SIM_STATE_SIMLOCK:
953 TELEPHONY_LOGI("SimStateHandle::RegisterIccSimLock()");
954 observerHandler_->RegObserver(RadioEvent::RADIO_SIM_STATE_SIMLOCK, handler);
955 break;
956 case RadioEvent::RADIO_CARD_TYPE_CHANGE:
957 TELEPHONY_LOGI("SimStateHandle::RegisterCardTypeChange()");
958 observerHandler_->RegObserver(RadioEvent::RADIO_CARD_TYPE_CHANGE, handler);
959 break;
960 case RadioEvent::RADIO_SIM_ICCID_LOADED:
961 TELEPHONY_LOGI("SimStateHandle::RegisterIccidLoaded()");
962 observerHandler_->RegObserver(RadioEvent::RADIO_SIM_ICCID_LOADED, handler);
963 if (!iccid_.empty() && handler != nullptr) {
964 TelEventHandler::SendTelEvent(handler, RadioEvent::RADIO_SIM_ICCID_LOADED, slotId_, 0);
965 TELEPHONY_LOGI("SimStateHandle::RegisterIccidLoaded() OK send, slotId = %{public}d", slotId_);
966 }
967 break;
968 default:
969 TELEPHONY_LOGI("SimStateHandle RegisterCoreNotify do default");
970 break;
971 }
972 }
973
UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)974 void SimStateHandle::UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
975 {
976 switch (what) {
977 case RadioEvent::RADIO_SIM_STATE_CHANGE:
978 TELEPHONY_LOGI("SimStateHandle::UnregisterIccStateChanged()");
979 observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_CHANGE, handler);
980 break;
981 case RadioEvent::RADIO_SIM_STATE_READY:
982 TELEPHONY_LOGI("SimStateHandle::UnregisterIccReady()");
983 observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_READY, handler);
984 break;
985 case RadioEvent::RADIO_SIM_STATE_LOCKED:
986 TELEPHONY_LOGI("SimStateHandle::UnregisterIccLocked()");
987 observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_LOCKED, handler);
988 break;
989 case RadioEvent::RADIO_SIM_STATE_SIMLOCK:
990 TELEPHONY_LOGI("SimStateHandle::UnregisterIccSimLock()");
991 observerHandler_->Remove(RadioEvent::RADIO_SIM_STATE_SIMLOCK, handler);
992 break;
993 case RadioEvent::RADIO_CARD_TYPE_CHANGE:
994 TELEPHONY_LOGI("SimStateHandle::RegisterCardTypeChange()");
995 observerHandler_->Remove(RadioEvent::RADIO_CARD_TYPE_CHANGE, handler);
996 break;
997 default:
998 TELEPHONY_LOGI("SimStateHandle UnRegisterCoreNotify do default");
999 break;
1000 }
1001 }
1002
IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer & event)1003 bool SimStateHandle::IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer &event)
1004 {
1005 std::shared_ptr<Int32Parcel> object = event->GetSharedObject<Int32Parcel>();
1006 if (object == nullptr) {
1007 TELEPHONY_LOGE("object is nullptr!");
1008 return false;
1009 }
1010 int32_t radioState = object->data;
1011 if (radioState == ModemPowerState::CORE_SERVICE_POWER_NOT_AVAILABLE) {
1012 TELEPHONY_LOGI("received radio unavailable");
1013 IccState iccState;
1014 iccState.simType_ = ICC_UNKNOWN_TYPE;
1015 iccState.simStatus_ = ICC_CONTENT_UNKNOWN;
1016 modemInitDone_ = false;
1017 ProcessIccCardState(iccState, slotId_);
1018 return true;
1019 }
1020 return false;
1021 }
1022
GetSimIO(int32_t slotId,SimIoRequestInfo requestInfo)1023 int32_t SimStateHandle::GetSimIO(int32_t slotId, SimIoRequestInfo requestInfo)
1024 {
1025 auto event = AppExecFwk::InnerEvent::Get(MSG_SIM_GET_SIM_IO_DONE);
1026 if (event == nullptr) {
1027 TELEPHONY_LOGE("event is nullptr!");
1028 return SIM_AUTH_FAIL;
1029 }
1030 event->SetOwner(shared_from_this());
1031 auto telRilManager = telRilManager_.lock();
1032 if (telRilManager == nullptr) {
1033 TELEPHONY_LOGE("SimStateHandle::GetSimIO() telRilManager is nullptr!!");
1034 return SIM_AUTH_FAIL;
1035 }
1036 return telRilManager->GetSimIO(slotId, requestInfo, event);
1037 }
1038
GetSimIOResult(int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)1039 void SimStateHandle::GetSimIOResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
1040 {
1041 TELEPHONY_LOGI("SimStateHandle::GetSimIOResult slotId = %{public}d", slotId);
1042 auto response = event->GetSharedObject<IccIoResultInfo>();
1043 if (response == nullptr) {
1044 TELEPHONY_LOGE("SimStateHandle::GetSimIOResult() fail");
1045 return;
1046 }
1047 simIORespon_.sw1 = response->sw1;
1048 simIORespon_.sw2 = response->sw2;
1049 simIORespon_.response = response->response;
1050 }
1051
GetSimIOResponse()1052 SimAuthenticationResponse SimStateHandle::GetSimIOResponse()
1053 {
1054 return simIORespon_;
1055 }
1056
NotifySimSlotsMapping(int32_t slotId)1057 int32_t SimStateHandle::NotifySimSlotsMapping(int32_t slotId)
1058 {
1059 TELEPHONY_LOGI("SimStateHandle::NotifySimSlotsMapping slotId = %{public}d", slotId);
1060 IccState iccState;
1061 iccState.simType_ = ICC_UNKNOWN_TYPE;
1062 iccState.simStatus_ = ICC_CARD_ABSENT;
1063 ProcessIccCardState(iccState, slotId);
1064 return TELEPHONY_ERR_SUCCESS;
1065 }
1066
1067 } // namespace Telephony
1068 } // namespace OHOS
1069