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