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