• 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_manager.h"
17 
18 #include "core_service_errors.h"
19 #include "telephony_errors.h"
20 #include "telephony_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 std::mutex SimStateManager::mtx_;
25 constexpr static const int32_t WAIT_TIME_ONE_SECOND = 1;
26 constexpr static const int32_t WAIT_TIME_SECOND = 3;
27 constexpr static const int32_t WAIT_TIME_LONG_SECOND = 20;
28 
SimStateManager(std::shared_ptr<ITelRilManager> telRilManager)29 SimStateManager::SimStateManager(std::shared_ptr<ITelRilManager> telRilManager)
30     : telRilManager_(telRilManager), simStateRun_(STATE_NOT_START)
31 {
32     TELEPHONY_LOGI("SimStateManager::SimStateManager()");
33 }
34 
Init(int32_t slotId)35 void SimStateManager::Init(int32_t slotId)
36 {
37     std::lock_guard<std::mutex> lck(mtx_);
38     TELEPHONY_LOGE("SimStateManager::Init()");
39     if (simStateRun_ == STATE_RUNNING) {
40         TELEPHONY_LOGE("simStateHandle_ is running");
41         return;
42     }
43     if (telRilManager_ == nullptr) {
44         TELEPHONY_LOGE("SimStateManager::Init telRilManager_ is null.");
45         return;
46     }
47     simStateHandle_ = std::make_shared<SimStateHandle>(shared_from_this());
48     if (simStateHandle_ == nullptr) {
49         TELEPHONY_LOGE("SimStateManager::failed to create new SimStateHandle");
50         return;
51     }
52     simStateHandle_->SetRilManager(std::weak_ptr<Telephony::ITelRilManager>(telRilManager_));
53     simStateHandle_->Init(slotId);
54 
55     TELEPHONY_LOGI("SimStateManager::eventLoop_ is running");
56     simStateRun_ = STATE_RUNNING;
57 }
58 
RegisterCoreNotify(const HANDLE & handler,int what)59 void SimStateManager::RegisterCoreNotify(const HANDLE &handler, int what)
60 {
61     if (simStateHandle_ == nullptr) {
62         TELEPHONY_LOGE("RegisterCoreNotify(), simStateHandle_ is nullptr!!!");
63         return;
64     }
65     std::lock_guard<std::mutex> lck(mtx_);
66     simStateHandle_->RegisterCoreNotify(handler, what);
67 }
68 
UnRegisterCoreNotify(const HANDLE & handler,int what)69 void SimStateManager::UnRegisterCoreNotify(const HANDLE &handler, int what)
70 {
71     if (simStateHandle_ == nullptr) {
72         TELEPHONY_LOGE("UnRegisterCoreNotify(), simStateHandle_ is nullptr!!!");
73         return;
74     }
75     std::lock_guard<std::mutex> lck(mtx_);
76     simStateHandle_->UnRegisterCoreNotify(handler, what);
77 }
78 
HasSimCard()79 bool SimStateManager::HasSimCard()
80 {
81     bool ret = false;
82     if (simStateHandle_ != nullptr) {
83         std::lock_guard<std::mutex> lck(mtx_);
84         TELEPHONY_LOGD("SimStateManager::HasSimCard");
85         ret = simStateHandle_->HasSimCard();
86     }
87     return ret;
88 }
89 
GetSimState()90 SimState SimStateManager::GetSimState()
91 {
92     SimState ret = SimState::SIM_STATE_UNKNOWN;
93     if (simStateHandle_ != nullptr) {
94         std::lock_guard<std::mutex> lck(mtx_);
95         TELEPHONY_LOGD("SimStateManager::GetSimState()");
96         ret = simStateHandle_->GetSimState();
97     }
98     return ret;
99 }
100 
GetSimIccStatus()101 IccSimStatus SimStateManager::GetSimIccStatus()
102 {
103     IccSimStatus ret = IccSimStatus::ICC_CONTENT_UNKNOWN;
104     if (simStateHandle_ != nullptr) {
105         std::lock_guard<std::mutex> lck(mtx_);
106         TELEPHONY_LOGD("SimStateManager::GetSimIccStatus()");
107         ret = simStateHandle_->GetSimIccStatus();
108     }
109     return ret;
110 }
111 
SetSimState(SimState simState)112 void SimStateManager::SetSimState(SimState simState)
113 {
114     if (simStateHandle_ == nullptr) {
115         TELEPHONY_LOGI("SimStateManager::SetSimState(), simStateHandle_ is nullptr!!!");
116         return;
117     }
118     std::lock_guard<std::mutex> lck(mtx_);
119     simStateHandle_->SetSimState(simState);
120 }
121 
IfModemInitDone()122 bool SimStateManager::IfModemInitDone()
123 {
124     if (simStateHandle_ != nullptr) {
125         return simStateHandle_->modemInitDone_;
126     }
127     return false;
128 }
129 
GetCardType()130 CardType SimStateManager::GetCardType()
131 {
132     CardType ret = CardType::UNKNOWN_CARD;
133     if (simStateHandle_ != nullptr) {
134         std::lock_guard<std::mutex> lck(mtx_);
135         TELEPHONY_LOGD("SimStateManager::GetCardType()");
136         ret = simStateHandle_->GetCardType();
137     }
138     return ret;
139 }
140 
GetIccid()141 std::string SimStateManager::GetIccid()
142 {
143     if (simStateHandle_ != nullptr) {
144         std::lock_guard<std::mutex> lck(mtx_);
145         return simStateHandle_->GetIccid();
146     }
147     return "";
148 }
149 
SetModemInit(bool state)150 int32_t SimStateManager::SetModemInit(bool state)
151 {
152     if (simStateHandle_ != nullptr) {
153         TELEPHONY_LOGI("state: %{public}d", state);
154         simStateHandle_->modemInitDone_ = state;
155         if (!state) {
156             TELEPHONY_LOGI("SetModemInit to false, it should set simstate to init state");
157             simStateHandle_->oldSimType_ = ICC_UNKNOWN_TYPE;
158             simStateHandle_->oldSimStatus_ = ICC_CONTENT_UNKNOWN;
159         }
160         return TELEPHONY_ERR_SUCCESS;
161     }
162     return TELEPHONY_ERR_LOCAL_PTR_NULL;
163 }
164 
SyncCmdResponse()165 void SimStateManager::SyncCmdResponse()
166 {
167     std::unique_lock<std::mutex> lck(ctx_);
168     responseReady_ = true;
169     TELEPHONY_LOGI("SimStateManager::SyncCmdResponse(), responseReady_ = %{public}d", responseReady_);
170     cv_.notify_one();
171 }
172 
SyncSimMatchResponse()173 void SimStateManager::SyncSimMatchResponse()
174 {
175     std::unique_lock<std::mutex> lck(stx_);
176     responseSimMatchReady_ = true;
177     TELEPHONY_LOGI("SimStateManager::SyncSimMatchResponse(), responseSimMatchReady = %{public}d",
178                    responseSimMatchReady_);
179     sv_.notify_one();
180 }
181 
SyncUnlockPinResponse()182 void SimStateManager::SyncUnlockPinResponse()
183 {
184     std::unique_lock<std::mutex> lck(unlockPinCtx_);
185     responseUnlockPinReady_ = true;
186     TELEPHONY_LOGI("SimStateManager::SyncUnlockPinResponse(), responseUnlockPinReady = %{public}d",
187                    responseUnlockPinReady_);
188     unlockPinCv_.notify_one();
189 }
190 
SyncSimStateResponse()191 void SimStateManager::SyncSimStateResponse()
192 {
193     std::unique_lock<std::mutex> lck(simStateCtx_);
194     responseSimStateReady_ = true;
195     TELEPHONY_LOGI("SimStateManager::SyncSimStateResponse(), responseSimStateReady = %{public}d",
196                    responseSimStateReady_);
197     simStateCv_.notify_one();
198 }
199 
UnlockPin(int32_t slotId,const std::string & pin,LockStatusResponse & response)200 int32_t SimStateManager::UnlockPin(int32_t slotId, const std::string &pin, LockStatusResponse &response)
201 {
202     if (simStateHandle_ == nullptr) {
203         TELEPHONY_LOGE("simStateHandle_ is nullptr");
204         return TELEPHONY_ERR_LOCAL_PTR_NULL;
205     }
206     std::unique_lock<std::mutex> unlockPinLck(unlockPinCtx_);
207     TELEPHONY_LOGD("SimStateManager::UnlockPin slotId = %{public}d", slotId);
208     responseUnlockPinReady_ = false;
209     simStateHandle_->UnlockPin(slotId, pin);
210     while (!responseUnlockPinReady_) {
211         TELEPHONY_LOGI("UnlockPin::wait(), response = false");
212         if (unlockPinCv_.wait_for(unlockPinLck, std::chrono::seconds(WAIT_TIME_LONG_SECOND)) ==
213             std::cv_status::timeout) {
214             break;
215         }
216     }
217     if (!responseUnlockPinReady_) {
218         TELEPHONY_LOGE("unlock pin sim update failed");
219         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
220     }
221     std::unique_lock<std::mutex> simStateLck(simStateCtx_);
222     responseSimStateReady_ = false;
223     while (!responseSimStateReady_) {
224         TELEPHONY_LOGI("UnlockPin::wait sim state changed, response = false");
225         if (simStateCv_.wait_for(simStateLck, std::chrono::seconds(WAIT_TIME_ONE_SECOND)) == std::cv_status::timeout) {
226             break;
227         }
228     }
229     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
230     if (unlockResult == UNLOCK_SUCCESS) {
231         response.result = UNLOCK_OK;
232     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
233         response.result = UNLOCK_INCORRECT;
234     } else {
235         response.result = UNLOCK_FAIL;
236     }
237     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
238     TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
239     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
240 }
241 
UnlockPuk(int32_t slotId,const std::string & newPin,const std::string & puk,LockStatusResponse & response)242 int32_t SimStateManager::UnlockPuk(
243     int32_t slotId, const std::string &newPin, const std::string &puk, LockStatusResponse &response)
244 {
245     if (simStateHandle_ == nullptr) {
246         TELEPHONY_LOGE("simStateHandle_ is nullptr");
247         return TELEPHONY_ERR_LOCAL_PTR_NULL;
248     }
249     std::unique_lock<std::mutex> lck(ctx_);
250     TELEPHONY_LOGD("SimStateManager::UnlockPuk slotId = %{public}d", slotId);
251     responseReady_ = false;
252     simStateHandle_->UnlockPuk(slotId, newPin, puk);
253     while (!responseReady_) {
254         TELEPHONY_LOGI("UnlockPuk::wait(), response = false");
255         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_LONG_SECOND)) == std::cv_status::timeout) {
256             break;
257         }
258     }
259     if (!responseReady_) {
260         TELEPHONY_LOGE("unlock puk sim update failed");
261         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
262     }
263     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
264     if (unlockResult == UNLOCK_SUCCESS) {
265         response.result = UNLOCK_OK;
266     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
267         response.result = UNLOCK_INCORRECT;
268     } else {
269         response.result = UNLOCK_FAIL;
270     }
271     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
272     TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
273     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
274 }
275 
AlterPin(int32_t slotId,const std::string & newPin,const std::string & oldPin,LockStatusResponse & response)276 int32_t SimStateManager::AlterPin(
277     int32_t slotId, const std::string &newPin, const std::string &oldPin, LockStatusResponse &response)
278 {
279     if (simStateHandle_ == nullptr) {
280         TELEPHONY_LOGE("simStateHandle_ is nullptr");
281         return TELEPHONY_ERR_LOCAL_PTR_NULL;
282     }
283     std::unique_lock<std::mutex> lck(ctx_);
284     TELEPHONY_LOGD("SimStateManager::AlterPin slotId = %{public}d", slotId);
285     responseReady_ = false;
286     simStateHandle_->AlterPin(slotId, newPin, oldPin);
287     while (!responseReady_) {
288         TELEPHONY_LOGI("AlterPin::wait(), response = false");
289         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
290             break;
291         }
292     }
293     if (!responseReady_) {
294         TELEPHONY_LOGE("alter pin sim update failed");
295         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
296     }
297     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
298     if (unlockResult == UNLOCK_SUCCESS) {
299         response.result = UNLOCK_OK;
300     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
301         response.result = UNLOCK_INCORRECT;
302     } else {
303         response.result = UNLOCK_FAIL;
304     }
305     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
306     TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
307     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
308 }
309 
SetLockState(int32_t slotId,const LockInfo & options,LockStatusResponse & response)310 int32_t SimStateManager::SetLockState(int32_t slotId, const LockInfo &options, LockStatusResponse &response)
311 {
312     if (options.lockType != LockType::PIN_LOCK && options.lockType != LockType::FDN_LOCK) {
313         TELEPHONY_LOGE("SetLockState lockType is error");
314         response.result = UNLOCK_FAIL;
315         return TELEPHONY_ERR_ARGUMENT_INVALID;
316     }
317     if (options.lockState != LockState::LOCK_OFF && options.lockState != LockState::LOCK_ON) {
318         TELEPHONY_LOGE("SetLockState lockState is error");
319         response.result = UNLOCK_FAIL;
320         return TELEPHONY_ERR_ARGUMENT_INVALID;
321     }
322     if (simStateHandle_ == nullptr) {
323         TELEPHONY_LOGE("simStateHandle_ is nullptr");
324         return TELEPHONY_ERR_LOCAL_PTR_NULL;
325     }
326     std::unique_lock<std::mutex> lck(ctx_);
327     TELEPHONY_LOGD("SimStateManager::SetLockState slotId = %{public}d", slotId);
328     responseReady_ = false;
329     simStateHandle_->SetLockState(slotId, options);
330     while (!responseReady_) {
331         TELEPHONY_LOGI("SetLockState::wait(), response = false");
332         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_LONG_SECOND)) == std::cv_status::timeout) {
333             break;
334         }
335     }
336     if (!responseReady_) {
337         TELEPHONY_LOGE("set lock state sim update failed");
338         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
339     }
340     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
341     if (unlockResult == UNLOCK_SUCCESS) {
342         response.result = UNLOCK_OK;
343     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
344         response.result = UNLOCK_INCORRECT;
345     } else {
346         response.result = UNLOCK_FAIL;
347     }
348     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
349     TELEPHONY_LOGI(
350         "SetLockState response.result:%{public}d,response.remain:%{public}d", response.result, response.remain);
351     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
352 }
353 
GetLockState(int32_t slotId,LockType lockType,LockState & lockState)354 int32_t SimStateManager::GetLockState(int32_t slotId, LockType lockType, LockState &lockState)
355 {
356     if (lockType != LockType::PIN_LOCK && lockType != LockType::FDN_LOCK) {
357         TELEPHONY_LOGE("GetLockState lockType is error");
358         lockState = LockState::LOCK_ERROR;
359         return TELEPHONY_ERR_ARGUMENT_INVALID;
360     }
361     if (simStateHandle_ == nullptr) {
362         TELEPHONY_LOGE("simStateHandle_ is nullptr");
363         return TELEPHONY_ERR_LOCAL_PTR_NULL;
364     }
365     std::unique_lock<std::mutex> lck(ctx_);
366     TELEPHONY_LOGD("SimStateManager::GetLockState slotId = %{public}d", slotId);
367     responseReady_ = false;
368     simStateHandle_->GetLockState(slotId, lockType);
369     while (!responseReady_) {
370         TELEPHONY_LOGI("GetLockState::wait, response = false");
371         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
372             break;
373         }
374     }
375     if (!responseReady_) {
376         TELEPHONY_LOGE("get lock state sim load failed");
377         return CORE_ERR_SIM_CARD_LOAD_FAILED;
378     }
379     switch (simStateHandle_->GetUnlockData().lockState) {
380         case static_cast<int32_t>(LockState::LOCK_OFF):
381             lockState = LockState::LOCK_OFF;
382             break;
383         case static_cast<int32_t>(LockState::LOCK_ON):
384             lockState = LockState::LOCK_ON;
385             break;
386         default:
387             lockState = LockState::LOCK_ERROR;
388             break;
389     }
390     TELEPHONY_LOGI("SimStateManager::GetLockState(), %{public}d", lockState);
391     return TELEPHONY_SUCCESS;
392 }
393 
UnlockPin2(int32_t slotId,const std::string & pin2,LockStatusResponse & response)394 int32_t SimStateManager::UnlockPin2(int32_t slotId, const std::string &pin2, LockStatusResponse &response)
395 {
396     if (simStateHandle_ == nullptr) {
397         TELEPHONY_LOGE("simStateHandle_ is nullptr");
398         return TELEPHONY_ERR_LOCAL_PTR_NULL;
399     }
400     std::unique_lock<std::mutex> lck(ctx_);
401     TELEPHONY_LOGD("SimStateManager::UnlockPin2 slotId = %{public}d", slotId);
402     responseReady_ = false;
403     simStateHandle_->UnlockPin2(slotId, pin2);
404     while (!responseReady_) {
405         TELEPHONY_LOGI("UnlockPin2::wait(), response = false");
406         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
407             break;
408         }
409     }
410     if (!responseReady_) {
411         TELEPHONY_LOGE("unlock pin2 sim update failed");
412         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
413     }
414     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
415     if (unlockResult == UNLOCK_SUCCESS) {
416         response.result = UNLOCK_OK;
417     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
418         response.result = UNLOCK_INCORRECT;
419     } else {
420         response.result = UNLOCK_FAIL;
421     }
422     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
423     TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
424     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
425 }
426 
UnlockPuk2(int32_t slotId,const std::string & newPin2,const std::string & puk2,LockStatusResponse & response)427 int32_t SimStateManager::UnlockPuk2(
428     int32_t slotId, const std::string &newPin2, const std::string &puk2, LockStatusResponse &response)
429 {
430     if (simStateHandle_ == nullptr) {
431         TELEPHONY_LOGE("simStateHandle_ is nullptr");
432         return TELEPHONY_ERR_LOCAL_PTR_NULL;
433     }
434     std::unique_lock<std::mutex> lck(ctx_);
435     TELEPHONY_LOGD("SimStateManager::UnlockPuk2 slotId = %{public}d", slotId);
436     responseReady_ = false;
437     simStateHandle_->UnlockPuk2(slotId, newPin2, puk2);
438     while (!responseReady_) {
439         TELEPHONY_LOGI("UnlockPuk2::wait(), response = false");
440         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
441             break;
442         }
443     }
444     if (!responseReady_) {
445         TELEPHONY_LOGE("unlock puk2 sim update failed");
446         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
447     }
448     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
449     if (unlockResult == UNLOCK_SUCCESS) {
450         response.result = UNLOCK_OK;
451     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
452         response.result = UNLOCK_INCORRECT;
453     } else {
454         response.result = UNLOCK_FAIL;
455     }
456     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
457     TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
458     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
459 }
460 
AlterPin2(int32_t slotId,const std::string & newPin2,const std::string & oldPin2,LockStatusResponse & response)461 int32_t SimStateManager::AlterPin2(
462     int32_t slotId, const std::string &newPin2, const std::string &oldPin2, LockStatusResponse &response)
463 {
464     if (simStateHandle_ == nullptr) {
465         TELEPHONY_LOGE("simStateHandle_ is nullptr");
466         return TELEPHONY_ERR_LOCAL_PTR_NULL;
467     }
468     std::unique_lock<std::mutex> lck(ctx_);
469     TELEPHONY_LOGD("SimStateManager::AlterPin2 slotId = %{public}d", slotId);
470     responseReady_ = false;
471     simStateHandle_->AlterPin2(slotId, newPin2, oldPin2);
472     while (!responseReady_) {
473         TELEPHONY_LOGI("AlterPin2::wait(), response = false");
474         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
475             break;
476         }
477     }
478     if (!responseReady_) {
479         TELEPHONY_LOGE("alter pin2 sim update failed");
480         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
481     }
482     int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
483     if (unlockResult == UNLOCK_SUCCESS) {
484         response.result = UNLOCK_OK;
485     } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
486         response.result = UNLOCK_INCORRECT;
487     } else {
488         response.result = UNLOCK_FAIL;
489     }
490     response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
491     TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
492     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
493 }
494 
RefreshSimState(int32_t slotId)495 int32_t SimStateManager::RefreshSimState(int32_t slotId)
496 {
497     int32_t ret = 0;
498     if (simStateHandle_ != nullptr) {
499         std::unique_lock<std::mutex> lck(ctx_);
500         TELEPHONY_LOGD("SimStateManager::RefreshSimState slotId = %{public}d", slotId);
501         responseReady_ = false;
502         simStateHandle_->ObtainRealtimeIccStatus(slotId);
503         while (!responseReady_) {
504             TELEPHONY_LOGI("RefreshSimState::wait(), response = false");
505             if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
506                 break;
507             }
508         }
509         ret = static_cast<int32_t>(simStateHandle_->GetSimState());
510     }
511     TELEPHONY_LOGI("SimStateManager::RefreshSimState(), %{public}d", ret);
512     return ret;
513 }
514 
UnlockSimLock(int32_t slotId,const PersoLockInfo & lockInfo,LockStatusResponse & response)515 int32_t SimStateManager::UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo, LockStatusResponse &response)
516 {
517     if (simStateHandle_ == nullptr) {
518         TELEPHONY_LOGE("UnlockSimLock(), simStateHandle_ is nullptr!!!");
519         return TELEPHONY_ERR_LOCAL_PTR_NULL;
520     }
521     std::unique_lock<std::mutex> lck(ctx_);
522     TELEPHONY_LOGD("SimStateManager::UnlockSimLock slotId = %{public}d", slotId);
523     responseReady_ = false;
524     simStateHandle_->UnlockSimLock(slotId, lockInfo);
525     while (!responseReady_) {
526         TELEPHONY_LOGI("UnlockSimLock::wait(), response = false");
527         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
528             break;
529         }
530     }
531     if (!responseReady_) {
532         TELEPHONY_LOGE("sim update failed");
533         return CORE_ERR_SIM_CARD_UPDATE_FAILED;
534     }
535     int32_t ret = simStateHandle_->GetSimlockResponse().result;
536     TELEPHONY_LOGI("SimStateManager::UnlockSimLock(), remain: %{public}d", response.remain);
537     response.remain = simStateHandle_->GetSimlockResponse().remain;
538     if (ret == UNLOCK_PIN_PUK_INCORRECT) {
539         TELEPHONY_LOGI("SimStateManager::UnlockSimLock(), pin or puk incorrect");
540         response.result = UNLOCK_INCORRECT;
541     } else {
542         TELEPHONY_LOGI("SimStateManager::UnlockSimLock(), %{public}d", ret);
543         if (ret) {
544             response.result = UNLOCK_FAIL;
545         } else {
546             response.result = UNLOCK_OK;
547         }
548     }
549     return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
550 }
551 
SimAuthentication(int32_t slotId,AuthType authType,const std::string & authData,SimAuthenticationResponse & response)552 int32_t SimStateManager::SimAuthentication(
553     int32_t slotId, AuthType authType, const std::string &authData, SimAuthenticationResponse &response)
554 {
555     if (simStateHandle_ == nullptr) {
556         TELEPHONY_LOGE("SimAuthentication(), simStateHandle_ is nullptr!!!");
557         return SIM_AUTH_FAIL;
558     }
559     std::unique_lock<std::mutex> lck(ctx_);
560     responseReady_ = false;
561     int32_t ret = SIM_AUTH_FAIL;
562     ret = simStateHandle_->SimAuthentication(slotId, authType, authData);
563     while (!responseReady_) {
564         TELEPHONY_LOGI("SimAuthentication::wait(), response = false");
565         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
566             break;
567         }
568     }
569     response.sw1 = simStateHandle_->GetSimAuthenticationResponse().sw1;
570     response.sw2 = simStateHandle_->GetSimAuthenticationResponse().sw2;
571     response.response = simStateHandle_->GetSimAuthenticationResponse().response;
572     TELEPHONY_LOGI("SimStateManager::SimAuthentication(), sw1: %{public}d, sw2: %{public}d", response.sw1,
573         response.sw2);
574     return ret;
575 }
576 
SendSimMatchedOperatorInfo(int32_t slotId,int32_t state,const std::string & operName,const std::string & operKey)577 int32_t SimStateManager::SendSimMatchedOperatorInfo(
578     int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey)
579 {
580     if (simStateHandle_ == nullptr) {
581         TELEPHONY_LOGE("SendSimMatchedOperatorInfo(), simStateHandle_ is nullptr!!!");
582         return TELEPHONY_ERR_LOCAL_PTR_NULL;
583     }
584     std::unique_lock<std::mutex> lck(stx_);
585     responseSimMatchReady_ = false;
586     simStateHandle_->SendSimMatchedOperatorInfo(slotId, state, operName, operKey);
587     while (!responseSimMatchReady_) {
588         TELEPHONY_LOGI("SendSimMatchedOperatorInfo::wait(), response = false");
589         if (sv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
590             break;
591         }
592     }
593     int32_t response = simStateHandle_->GetSendSimMatchedOperatorInfoResponse();
594     TELEPHONY_LOGI("SimStateManager::SendSimMatchedOperatorInfo(), response: %{public}d", response);
595     return response;
596 }
597 
GetSimIO(int32_t slotId,SimIoRequestInfo requestInfo,SimAuthenticationResponse & response)598 int32_t SimStateManager::GetSimIO(
599     int32_t slotId, SimIoRequestInfo requestInfo, SimAuthenticationResponse &response)
600 {
601     if (simStateHandle_ == nullptr) {
602         TELEPHONY_LOGE("GetSimIO(), simStateHandle_ is nullptr!!!");
603         return SIM_AUTH_FAIL;
604     }
605     std::unique_lock<std::mutex> lck(ctx_);
606     responseReady_ = false;
607     int32_t ret = SIM_AUTH_FAIL;
608     ret = simStateHandle_->GetSimIO(slotId, requestInfo);
609     while (!responseReady_) {
610         TELEPHONY_LOGI("GetSimIO::wait(), response = false");
611         if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
612             break;
613         }
614     }
615     SimAuthenticationResponse retResponse = simStateHandle_->GetSimIOResponse();
616     response.sw1 = retResponse.sw1;
617     response.sw2 = retResponse.sw2;
618     response.response = retResponse.response;
619     TELEPHONY_LOGI("SimStateManager::GetSimIO(), sw1: %{public}d, sw2: %{public}d", response.sw1, response.sw2);
620     return ret;
621 }
622 
~SimStateManager()623 SimStateManager::~SimStateManager()
624 {
625     if (simStateHandle_ != nullptr) {
626         simStateHandle_->UnInit();
627     }
628 }
629 } // namespace Telephony
630 } // namespace OHOS
631