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