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