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 #include "telephony_ext_wrapper.h"
22
23 namespace OHOS {
24 namespace Telephony {
25 std::mutex SimStateManager::mtx_;
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<ffrt::mutex> lck(unlockPinCtx_);
185 responseUnlockPinReady_ = true;
186 TELEPHONY_LOGI("SimStateManager::SyncUnlockPinResponse(), responseUnlockPinReady = %{public}d",
187 responseUnlockPinReady_);
188 unlockPinCv_.notify_one();
189 }
190
UnlockPin(int32_t slotId,const std::string & pin,LockStatusResponse & response)191 int32_t SimStateManager::UnlockPin(int32_t slotId, const std::string &pin, LockStatusResponse &response)
192 {
193 if (simStateHandle_ == nullptr) {
194 TELEPHONY_LOGE("simStateHandle_ is nullptr");
195 return TELEPHONY_ERR_LOCAL_PTR_NULL;
196 }
197 std::unique_lock<ffrt::mutex> lck(unlockPinCtx_);
198 TELEPHONY_LOGD("SimStateManager::UnlockPin slotId = %{public}d", slotId);
199 responseUnlockPinReady_ = false;
200 simStateHandle_->UnlockPin(slotId, pin);
201 while (!responseUnlockPinReady_) {
202 TELEPHONY_LOGI("UnlockPin::wait(), response = false");
203 if (unlockPinCv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_LONG_SECOND)) ==
204 ffrt::cv_status::timeout) {
205 break;
206 }
207 }
208 if (!responseUnlockPinReady_) {
209 TELEPHONY_LOGE("unlock pin sim update failed");
210 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
211 }
212 int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
213 if (unlockResult == UNLOCK_SUCCESS) {
214 response.result = UNLOCK_OK;
215 if (GetSimState() == SimState::SIM_STATE_LOCKED) {
216 SetSimState(SimState::SIM_STATE_NOT_READY);
217 }
218 if (TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_ != nullptr) {
219 TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_(
220 slotId, simStateHandle_->GetIccid(), PinOperationType::PIN_ENABLE, pin);
221 }
222 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
223 response.result = UNLOCK_INCORRECT;
224 } else {
225 response.result = UNLOCK_FAIL;
226 }
227 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
228 TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
229 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
230 }
231
UnlockPuk(int32_t slotId,const std::string & newPin,const std::string & puk,LockStatusResponse & response)232 int32_t SimStateManager::UnlockPuk(
233 int32_t slotId, const std::string &newPin, const std::string &puk, LockStatusResponse &response)
234 {
235 if (simStateHandle_ == nullptr) {
236 TELEPHONY_LOGE("simStateHandle_ is nullptr");
237 return TELEPHONY_ERR_LOCAL_PTR_NULL;
238 }
239 std::unique_lock<std::mutex> lck(ctx_);
240 TELEPHONY_LOGD("SimStateManager::UnlockPuk slotId = %{public}d", slotId);
241 responseReady_ = false;
242 simStateHandle_->UnlockPuk(slotId, newPin, puk);
243 while (!responseReady_) {
244 TELEPHONY_LOGI("UnlockPuk::wait(), response = false");
245 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_LONG_SECOND)) == std::cv_status::timeout) {
246 break;
247 }
248 }
249 if (!responseReady_) {
250 TELEPHONY_LOGE("unlock puk sim update failed");
251 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
252 }
253 int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
254 if (unlockResult == UNLOCK_SUCCESS) {
255 response.result = UNLOCK_OK;
256 if (GetSimState() == SimState::SIM_STATE_LOCKED) {
257 SetSimState(SimState::SIM_STATE_NOT_READY);
258 }
259 if (TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_ != nullptr) {
260 TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_(
261 slotId, simStateHandle_->GetIccid(), PinOperationType::PIN_ALTER, newPin);
262 }
263 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
264 response.result = UNLOCK_INCORRECT;
265 } else {
266 response.result = UNLOCK_FAIL;
267 }
268 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
269 TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
270 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
271 }
272
AlterPin(int32_t slotId,const std::string & newPin,const std::string & oldPin,LockStatusResponse & response)273 int32_t SimStateManager::AlterPin(
274 int32_t slotId, const std::string &newPin, const std::string &oldPin, LockStatusResponse &response)
275 {
276 if (simStateHandle_ == nullptr) {
277 TELEPHONY_LOGE("simStateHandle_ is nullptr");
278 return TELEPHONY_ERR_LOCAL_PTR_NULL;
279 }
280 std::unique_lock<std::mutex> lck(ctx_);
281 TELEPHONY_LOGD("SimStateManager::AlterPin slotId = %{public}d", slotId);
282 responseReady_ = false;
283 simStateHandle_->AlterPin(slotId, newPin, oldPin);
284 while (!responseReady_) {
285 TELEPHONY_LOGI("AlterPin::wait(), response = false");
286 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
287 break;
288 }
289 }
290 if (!responseReady_) {
291 TELEPHONY_LOGE("alter pin sim update failed");
292 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
293 }
294 int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
295 if (unlockResult == UNLOCK_SUCCESS) {
296 response.result = UNLOCK_OK;
297 if (TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_ != nullptr) {
298 TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_(
299 slotId, simStateHandle_->GetIccid(), PinOperationType::PIN_ALTER, newPin);
300 }
301 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
302 response.result = UNLOCK_INCORRECT;
303 } else {
304 response.result = UNLOCK_FAIL;
305 }
306 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
307 TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
308 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
309 }
310
SetLockState(int32_t slotId,const LockInfo & options,LockStatusResponse & response)311 int32_t SimStateManager::SetLockState(int32_t slotId, const LockInfo &options, LockStatusResponse &response)
312 {
313 if (options.lockType != LockType::PIN_LOCK && options.lockType != LockType::FDN_LOCK) {
314 TELEPHONY_LOGE("SetLockState lockType is error");
315 response.result = UNLOCK_FAIL;
316 return TELEPHONY_ERR_ARGUMENT_INVALID;
317 }
318 if (options.lockState != LockState::LOCK_OFF && options.lockState != LockState::LOCK_ON) {
319 TELEPHONY_LOGE("SetLockState lockState is error");
320 response.result = UNLOCK_FAIL;
321 return TELEPHONY_ERR_ARGUMENT_INVALID;
322 }
323 if (simStateHandle_ == nullptr) {
324 TELEPHONY_LOGE("simStateHandle_ is nullptr");
325 return TELEPHONY_ERR_LOCAL_PTR_NULL;
326 }
327 std::unique_lock<std::mutex> lck(ctx_);
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 if (options.lockType == LockType::PIN_LOCK && TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_ != nullptr) {
344 if (options.lockState == LockState::LOCK_OFF) {
345 TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_(
346 slotId, simStateHandle_->GetIccid(), PinOperationType::PIN_DISABLE, Str16ToStr8(options.password));
347 } else if (options.lockState == LockState::LOCK_ON) {
348 TELEPHONY_EXT_WRAPPER.cacheAssetPinForUpgrade_(
349 slotId, simStateHandle_->GetIccid(), PinOperationType::PIN_ENABLE, Str16ToStr8(options.password));
350 }
351 }
352 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
353 response.result = UNLOCK_INCORRECT;
354 } else {
355 response.result = UNLOCK_FAIL;
356 }
357 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
358 TELEPHONY_LOGI(
359 "SetLockState response.result:%{public}d,response.remain:%{public}d", response.result, response.remain);
360 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
361 }
362
GetLockState(int32_t slotId,LockType lockType,LockState & lockState)363 int32_t SimStateManager::GetLockState(int32_t slotId, LockType lockType, LockState &lockState)
364 {
365 if (lockType != LockType::PIN_LOCK && lockType != LockType::FDN_LOCK) {
366 TELEPHONY_LOGE("GetLockState lockType is error");
367 lockState = LockState::LOCK_ERROR;
368 return TELEPHONY_ERR_ARGUMENT_INVALID;
369 }
370 if (simStateHandle_ == nullptr) {
371 TELEPHONY_LOGE("simStateHandle_ is nullptr");
372 return TELEPHONY_ERR_LOCAL_PTR_NULL;
373 }
374 std::unique_lock<std::mutex> lck(ctx_);
375 TELEPHONY_LOGD("SimStateManager::GetLockState slotId = %{public}d", slotId);
376 responseReady_ = false;
377 simStateHandle_->GetLockState(slotId, lockType);
378 while (!responseReady_) {
379 TELEPHONY_LOGI("GetLockState::wait, response = false");
380 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
381 break;
382 }
383 }
384 if (!responseReady_) {
385 TELEPHONY_LOGE("get lock state sim load failed");
386 return CORE_ERR_SIM_CARD_LOAD_FAILED;
387 }
388 switch (simStateHandle_->GetUnlockData().lockState) {
389 case static_cast<int32_t>(LockState::LOCK_OFF):
390 lockState = LockState::LOCK_OFF;
391 break;
392 case static_cast<int32_t>(LockState::LOCK_ON):
393 lockState = LockState::LOCK_ON;
394 break;
395 default:
396 lockState = LockState::LOCK_ERROR;
397 break;
398 }
399 TELEPHONY_LOGI("SimStateManager::GetLockState(), %{public}d", lockState);
400 return TELEPHONY_SUCCESS;
401 }
402
UnlockPin2(int32_t slotId,const std::string & pin2,LockStatusResponse & response)403 int32_t SimStateManager::UnlockPin2(int32_t slotId, const std::string &pin2, LockStatusResponse &response)
404 {
405 if (simStateHandle_ == nullptr) {
406 TELEPHONY_LOGE("simStateHandle_ is nullptr");
407 return TELEPHONY_ERR_LOCAL_PTR_NULL;
408 }
409 std::unique_lock<std::mutex> lck(ctx_);
410 TELEPHONY_LOGD("SimStateManager::UnlockPin2 slotId = %{public}d", slotId);
411 responseReady_ = false;
412 simStateHandle_->UnlockPin2(slotId, pin2);
413 while (!responseReady_) {
414 TELEPHONY_LOGI("UnlockPin2::wait(), response = false");
415 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
416 break;
417 }
418 }
419 if (!responseReady_) {
420 TELEPHONY_LOGE("unlock pin2 sim update failed");
421 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
422 }
423 int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
424 if (unlockResult == UNLOCK_SUCCESS) {
425 response.result = UNLOCK_OK;
426 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
427 response.result = UNLOCK_INCORRECT;
428 } else {
429 response.result = UNLOCK_FAIL;
430 }
431 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
432 TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
433 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
434 }
435
UnlockPuk2(int32_t slotId,const std::string & newPin2,const std::string & puk2,LockStatusResponse & response)436 int32_t SimStateManager::UnlockPuk2(
437 int32_t slotId, const std::string &newPin2, const std::string &puk2, LockStatusResponse &response)
438 {
439 if (simStateHandle_ == nullptr) {
440 TELEPHONY_LOGE("simStateHandle_ is nullptr");
441 return TELEPHONY_ERR_LOCAL_PTR_NULL;
442 }
443 std::unique_lock<std::mutex> lck(ctx_);
444 TELEPHONY_LOGD("SimStateManager::UnlockPuk2 slotId = %{public}d", slotId);
445 responseReady_ = false;
446 simStateHandle_->UnlockPuk2(slotId, newPin2, puk2);
447 while (!responseReady_) {
448 TELEPHONY_LOGI("UnlockPuk2::wait(), response = false");
449 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
450 break;
451 }
452 }
453 if (!responseReady_) {
454 TELEPHONY_LOGE("unlock puk2 sim update failed");
455 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
456 }
457 int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
458 if (unlockResult == UNLOCK_SUCCESS) {
459 response.result = UNLOCK_OK;
460 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
461 response.result = UNLOCK_INCORRECT;
462 } else {
463 response.result = UNLOCK_FAIL;
464 }
465 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
466 TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
467 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
468 }
469
AlterPin2(int32_t slotId,const std::string & newPin2,const std::string & oldPin2,LockStatusResponse & response)470 int32_t SimStateManager::AlterPin2(
471 int32_t slotId, const std::string &newPin2, const std::string &oldPin2, LockStatusResponse &response)
472 {
473 if (simStateHandle_ == nullptr) {
474 TELEPHONY_LOGE("simStateHandle_ is nullptr");
475 return TELEPHONY_ERR_LOCAL_PTR_NULL;
476 }
477 std::unique_lock<std::mutex> lck(ctx_);
478 TELEPHONY_LOGD("SimStateManager::AlterPin2 slotId = %{public}d", slotId);
479 responseReady_ = false;
480 simStateHandle_->AlterPin2(slotId, newPin2, oldPin2);
481 while (!responseReady_) {
482 TELEPHONY_LOGI("AlterPin2::wait(), response = false");
483 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
484 break;
485 }
486 }
487 if (!responseReady_) {
488 TELEPHONY_LOGE("alter pin2 sim update failed");
489 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
490 }
491 int32_t unlockResult = static_cast<int32_t>(simStateHandle_->GetUnlockData().result);
492 if (unlockResult == UNLOCK_SUCCESS) {
493 response.result = UNLOCK_OK;
494 } else if (unlockResult == UNLOCK_PASSWORD_ERR) {
495 response.result = UNLOCK_INCORRECT;
496 } else {
497 response.result = UNLOCK_FAIL;
498 }
499 response.remain = static_cast<int32_t>(simStateHandle_->GetUnlockData().remain);
500 TELEPHONY_LOGI("response.result :%{public}d, remain :%{public}d", response.result, response.remain);
501 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
502 }
503
RefreshSimState(int32_t slotId)504 int32_t SimStateManager::RefreshSimState(int32_t slotId)
505 {
506 int32_t ret = 0;
507 if (simStateHandle_ != nullptr) {
508 std::unique_lock<std::mutex> lck(ctx_);
509 TELEPHONY_LOGD("SimStateManager::RefreshSimState slotId = %{public}d", slotId);
510 responseReady_ = false;
511 simStateHandle_->ObtainRealtimeIccStatus(slotId);
512 while (!responseReady_) {
513 TELEPHONY_LOGI("RefreshSimState::wait(), response = false");
514 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
515 break;
516 }
517 }
518 ret = static_cast<int32_t>(simStateHandle_->GetSimState());
519 }
520 TELEPHONY_LOGI("SimStateManager::RefreshSimState(), %{public}d", ret);
521 return ret;
522 }
523
UnlockSimLock(int32_t slotId,const PersoLockInfo & lockInfo,LockStatusResponse & response)524 int32_t SimStateManager::UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo, LockStatusResponse &response)
525 {
526 if (simStateHandle_ == nullptr) {
527 TELEPHONY_LOGE("UnlockSimLock(), simStateHandle_ is nullptr!!!");
528 return TELEPHONY_ERR_LOCAL_PTR_NULL;
529 }
530 std::unique_lock<std::mutex> lck(ctx_);
531 TELEPHONY_LOGD("SimStateManager::UnlockSimLock slotId = %{public}d", slotId);
532 responseReady_ = false;
533 simStateHandle_->UnlockSimLock(slotId, lockInfo);
534 while (!responseReady_) {
535 TELEPHONY_LOGI("UnlockSimLock::wait(), response = false");
536 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
537 break;
538 }
539 }
540 if (!responseReady_) {
541 TELEPHONY_LOGE("sim update failed");
542 return CORE_ERR_SIM_CARD_UPDATE_FAILED;
543 }
544 int32_t ret = simStateHandle_->GetSimlockResponse().result;
545 TELEPHONY_LOGI("SimStateManager::UnlockSimLock(), remain: %{public}d", response.remain);
546 response.remain = simStateHandle_->GetSimlockResponse().remain;
547 if (ret == UNLOCK_PIN_PUK_INCORRECT) {
548 TELEPHONY_LOGI("SimStateManager::UnlockSimLock(), pin or puk incorrect");
549 response.result = UNLOCK_INCORRECT;
550 } else {
551 TELEPHONY_LOGI("SimStateManager::UnlockSimLock(), %{public}d", ret);
552 if (ret) {
553 response.result = UNLOCK_FAIL;
554 } else {
555 response.result = UNLOCK_OK;
556 }
557 }
558 return (response.result == UNLOCK_FAIL) ? TELEPHONY_ERR_RIL_CMD_FAIL : TELEPHONY_SUCCESS;
559 }
560
SimAuthentication(int32_t slotId,AuthType authType,const std::string & authData,SimAuthenticationResponse & response)561 int32_t SimStateManager::SimAuthentication(
562 int32_t slotId, AuthType authType, const std::string &authData, SimAuthenticationResponse &response)
563 {
564 if (simStateHandle_ == nullptr) {
565 TELEPHONY_LOGE("SimAuthentication(), simStateHandle_ is nullptr!!!");
566 return SIM_AUTH_FAIL;
567 }
568 std::unique_lock<std::mutex> lck(ctx_);
569 responseReady_ = false;
570 int32_t ret = SIM_AUTH_FAIL;
571 ret = simStateHandle_->SimAuthentication(slotId, authType, authData);
572 while (!responseReady_) {
573 TELEPHONY_LOGI("SimAuthentication::wait(), response = false");
574 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
575 break;
576 }
577 }
578 response.sw1 = simStateHandle_->GetSimAuthenticationResponse().sw1;
579 response.sw2 = simStateHandle_->GetSimAuthenticationResponse().sw2;
580 response.response = simStateHandle_->GetSimAuthenticationResponse().response;
581 TELEPHONY_LOGI("SimStateManager::SimAuthentication(), sw1: %{public}d, sw2: %{public}d", response.sw1,
582 response.sw2);
583 return ret;
584 }
585
SendSimMatchedOperatorInfo(int32_t slotId,int32_t state,const std::string & operName,const std::string & operKey)586 int32_t SimStateManager::SendSimMatchedOperatorInfo(
587 int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey)
588 {
589 if (simStateHandle_ == nullptr) {
590 TELEPHONY_LOGE("SendSimMatchedOperatorInfo(), simStateHandle_ is nullptr!!!");
591 return TELEPHONY_ERR_LOCAL_PTR_NULL;
592 }
593 std::unique_lock<std::mutex> lck(stx_);
594 responseSimMatchReady_ = false;
595 simStateHandle_->SendSimMatchedOperatorInfo(slotId, state, operName, operKey);
596 while (!responseSimMatchReady_) {
597 TELEPHONY_LOGI("SendSimMatchedOperatorInfo::wait(), response = false");
598 if (sv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
599 break;
600 }
601 }
602 int32_t response = simStateHandle_->GetSendSimMatchedOperatorInfoResponse();
603 TELEPHONY_LOGI("SimStateManager::SendSimMatchedOperatorInfo(), response: %{public}d", response);
604 return response;
605 }
606
GetSimIO(int32_t slotId,SimIoRequestInfo requestInfo,SimAuthenticationResponse & response)607 int32_t SimStateManager::GetSimIO(
608 int32_t slotId, SimIoRequestInfo requestInfo, SimAuthenticationResponse &response)
609 {
610 if (simStateHandle_ == nullptr) {
611 TELEPHONY_LOGE("GetSimIO(), simStateHandle_ is nullptr!!!");
612 return SIM_AUTH_FAIL;
613 }
614 std::unique_lock<std::mutex> lck(ctx_);
615 responseReady_ = false;
616 int32_t ret = SIM_AUTH_FAIL;
617 ret = simStateHandle_->GetSimIO(slotId, requestInfo);
618 while (!responseReady_) {
619 TELEPHONY_LOGI("GetSimIO::wait(), response = false");
620 if (cv_.wait_for(lck, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
621 break;
622 }
623 }
624 SimAuthenticationResponse retResponse = simStateHandle_->GetSimIOResponse();
625 response.sw1 = retResponse.sw1;
626 response.sw2 = retResponse.sw2;
627 response.response = retResponse.response;
628 TELEPHONY_LOGI("SimStateManager::GetSimIO(), sw1: %{public}d, sw2: %{public}d", response.sw1, response.sw2);
629 return ret;
630 }
631
NotifySimSlotsMapping(int32_t slotId)632 int32_t SimStateManager::NotifySimSlotsMapping(int32_t slotId)
633 {
634 if (simStateHandle_ == nullptr) {
635 TELEPHONY_LOGI("NotifySimSlotsMapping(), simStateHandle_ is nullptr!!!");
636 return TELEPHONY_ERR_LOCAL_PTR_NULL;
637 }
638 return simStateHandle_->NotifySimSlotsMapping(slotId);
639 }
640
~SimStateManager()641 SimStateManager::~SimStateManager()
642 {
643 if (simStateHandle_ != nullptr) {
644 simStateHandle_->UnInit();
645 }
646 }
647 } // namespace Telephony
648 } // namespace OHOS
649