• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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_file.h"
17 
18 #include <unistd.h>
19 
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "core_manager_inner.h"
23 #include "radio_event.h"
24 #include "sim_file_init.h"
25 #include "sim_number_decode.h"
26 #include "telephony_common_utils.h"
27 #include "telephony_ext_wrapper.h"
28 #include "telephony_state_registry_client.h"
29 #include "configuration.h"
30 #include "app_mgr_client.h"
31 
32 using namespace std;
33 using namespace OHOS::AppExecFwk;
34 using namespace OHOS::EventFwk;
35 
36 namespace OHOS {
37 namespace Telephony {
38 constexpr static const int32_t WAIT_TIME_SECOND = 1;
39 const int64_t DELAY_TIME = 500;
40 const int64_t GET_IMSI_DELAY_TIME = 2000;
41 const int BYTE_TO_BIT_LEN = 8;
42 const int HEX_TO_BYTE_LEN = 2;
43 const int HEX_TYPE = 16;
44 const int CFIS_BCD_NUMBER_LENGTH_OFFSET = 2;
45 const int CFIS_TON_NPI_OFFSET = 3;
46 const int SST_SPN_OFFSET = 4;
47 const int SST_PNN_OFFSET = 12;
48 const int CFIS_ADN_CAPABILITY_ID_OFFSET = 14;
49 const int CFIS_ADN_EXTENSION_ID_OFFSET = 15;
50 std::mutex IccFile::mtx_;
51 std::vector<std::string> SimFile::indiaMcc_;
SimFile(std::shared_ptr<SimStateManager> simStateManager)52 SimFile::SimFile(std::shared_ptr<SimStateManager> simStateManager) : IccFile("SimFile", simStateManager)
53 {
54     fileQueried_ = false;
55     displayConditionOfSpn_ = SPN_INVALID;
56     simFileInit_ = std::make_shared<SimFileInit>();
57     simFileInit_->InitMemberFunc(*this);
58 }
59 
StartLoad()60 void SimFile::StartLoad()
61 {
62     TELEPHONY_LOGI("SimFile::StartLoad() start");
63     LoadSimFiles();
64 }
65 
ObtainMCC()66 std::string SimFile::ObtainMCC()
67 {
68     if (mcc_.empty()) {
69         std::string imsi = ObtainIMSI();
70         if (imsi.empty()) {
71             TELEPHONY_LOGE("SimFile ObtainMCC: IMSI is null");
72             return "";
73         }
74         if ((lengthOfMnc_ == UNINITIALIZED_MNC) || (lengthOfMnc_ == UNKNOWN_MNC)) {
75             TELEPHONY_LOGE("SimFile ObtainMCC:  mncLength invalid");
76             return "";
77         }
78         int length = MCC_LEN + lengthOfMnc_;
79         int imsiLen = static_cast<int>(imsi.size());
80         mcc_ = ((imsiLen >= length) ? imsi.substr(0, MCC_LEN) : "");
81     }
82     return mcc_;
83 }
84 
ObtainMNC()85 std::string SimFile::ObtainMNC()
86 {
87     if (mnc_.empty()) {
88         std::string imsi = ObtainIMSI();
89         if (imsi.empty()) {
90             TELEPHONY_LOGE("SimFile ObtainMNC: IMSI is null");
91             return "";
92         }
93         if ((lengthOfMnc_ == UNINITIALIZED_MNC) || (lengthOfMnc_ == UNKNOWN_MNC)) {
94             TELEPHONY_LOGE("SimFile ObtainMNC:  mncLength invalid");
95             return "";
96         }
97         int length = MCC_LEN + lengthOfMnc_;
98         int imsiLen = static_cast<int>(imsi.size());
99         mnc_ = ((imsiLen >= length) ? imsi.substr(MCC_LEN, lengthOfMnc_) : "");
100     }
101     return mnc_;
102 }
103 
ObtainSimOperator()104 std::string SimFile::ObtainSimOperator()
105 {
106     if (operatorNumeric_.empty()) {
107         std::string imsi = ObtainIMSI();
108         if (imsi.empty()) {
109             TELEPHONY_LOGE("SimFile ObtainSimOperator: IMSI is null");
110             return "";
111         }
112         if ((lengthOfMnc_ == UNINITIALIZED_MNC) || (lengthOfMnc_ == UNKNOWN_MNC)) {
113             TELEPHONY_LOGE("SimFile ObtainSimOperator:  mncLength invalid");
114             return "";
115         }
116         int length = MCC_LEN + lengthOfMnc_;
117         int imsiLen = static_cast<int>(imsi.size());
118         operatorNumeric_ = ((imsiLen >= length) ? imsi.substr(0, MCC_LEN + lengthOfMnc_) : "");
119     }
120     return operatorNumeric_;
121 }
122 
ObtainIsoCountryCode()123 std::string SimFile::ObtainIsoCountryCode()
124 {
125     std::string numeric = ObtainSimOperator();
126     if (numeric.empty()) {
127         TELEPHONY_LOGE("SimFile ObtainIsoCountryCode: numeric is null");
128         return "";
129     }
130     int len = static_cast<int>(numeric.length());
131     std::string mcc = numeric.substr(0, MCC_LEN);
132     if (len >= MCC_LEN && IsValidDecValue(mcc)) {
133         std::string iso = MccPool::MccCountryCode(std::stoi(mcc));
134         return iso;
135     } else {
136         return "";
137     }
138 }
139 
ObtainCallForwardStatus()140 int SimFile::ObtainCallForwardStatus()
141 {
142     return callForwardStatus_;
143 }
144 
UpdateMsisdnNumber(const std::string & alphaTag,const std::string & number)145 bool SimFile::UpdateMsisdnNumber(const std::string &alphaTag, const std::string &number)
146 {
147     lastMsisdn_ = number;
148     lastMsisdnTag_ = alphaTag;
149     waitResult_ = false;
150     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::make_shared<DiallingNumbersInfo>();
151     diallingNumber->name_ = Str8ToStr16(alphaTag);
152     diallingNumber->number_ = Str8ToStr16(number);
153     std::unique_lock<std::mutex> lock(IccFile::mtx_);
154     AppExecFwk::InnerEvent::Pointer phoneNumberEvent =
155             CreateDiallingNumberPointer(MSG_SIM_SET_MSISDN_DONE, 0, 0, nullptr);
156     DiallingNumberUpdateInfor infor;
157     infor.diallingNumber = diallingNumber;
158     infor.fileId = ELEMENTARY_FILE_MSISDN;
159     infor.extFile = ObtainExtensionElementaryFile(ELEMENTARY_FILE_MSISDN);
160     infor.index = 1;
161     diallingNumberHandler_->UpdateDiallingNumbers(infor, phoneNumberEvent);
162     while (!waitResult_) {
163         TELEPHONY_LOGI("update msisdn number wait, response = false");
164         if (processWait_.wait_for(lock, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
165             break;
166         }
167     }
168     TELEPHONY_LOGI("UpdateMsisdnNumber finished %{public}d", waitResult_);
169     return waitResult_;
170 }
171 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)172 void SimFile::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
173 {
174     if (event == nullptr) {
175         TELEPHONY_LOGE("event is nullptr!");
176         return;
177     }
178     auto id = event->GetInnerEventId();
179     TELEPHONY_LOGD("SimFile::ProcessEvent id %{public}d, slotId_ = %{public}d", id, slotId_);
180     auto itFunc = memberFuncMap_.find(id);
181     if (itFunc != memberFuncMap_.end()) {
182         auto memberFunc = itFunc->second;
183         if (memberFunc != nullptr) {
184             bool isFileProcessResponse = memberFunc(event);
185             ProcessFileLoaded(isFileProcessResponse);
186         }
187     } else {
188         IccFile::ProcessEvent(event);
189     }
190 }
191 
ProcessIccRefresh(int msgId)192 void SimFile::ProcessIccRefresh(int msgId)
193 {
194     TELEPHONY_LOGI("SimFile::ProcessIccRefresh msgId %{public}d, slotId_ = %{public}d", msgId, slotId_);
195     switch (msgId) {
196         case ELEMENTARY_FILE_MBDN:
197             fileToGet_++;
198             break;
199         case ELEMENTARY_FILE_MAILBOX_CPHS:
200             fileToGet_++;
201             break;
202         case ELEMENTARY_FILE_CSP_CPHS:
203             fileToGet_++;
204             break;
205         case ELEMENTARY_FILE_FDN:
206             break;
207         case ELEMENTARY_FILE_MSISDN:
208             fileToGet_++;
209             break;
210         case ELEMENTARY_FILE_CFIS:
211         case ELEMENTARY_FILE_CFF_CPHS:
212             break;
213         default:
214             ClearData();
215             DeleteOperatorCache();
216             LoadSimFiles();
217             break;
218     }
219 }
220 
ProcessFileLoaded(bool response)221 void SimFile::ProcessFileLoaded(bool response)
222 {
223     if (!response) {
224         return;
225     }
226     fileToGet_ -= LOAD_STEP;
227     TELEPHONY_LOGD("SimFile ProcessFileLoaded Done: %{public}d requested: %{public}d slotId = %{public}d", fileToGet_,
228         fileQueried_, slotId_);
229     if (ObtainFilesFetched()) {
230         OnAllFilesFetched();
231     } else if (LockQueriedOrNot()) {
232         UpdateSimLanguage();
233     } else if (fileToGet_ < 0) {
234         fileToGet_ = 0;
235     }
236 }
237 
OnAllFilesFetched()238 void SimFile::OnAllFilesFetched()
239 {
240     UpdateSimLanguage();
241     UpdateLoaded(true);
242     TELEPHONY_LOGI("SimFile::OnAllFilesFetched: start notify slotId = %{public}d", slotId_);
243     if (filesFetchedObser_ != nullptr) {
244         filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_SIM_RECORDS_LOADED, slotId_);
245     }
246     if (stateManager_ != nullptr) {
247         CardType cardType = stateManager_->GetCardType();
248         NotifyRegistrySimState(cardType, SimState::SIM_STATE_LOADED, LockReason::SIM_NONE);
249     }
250     PublishSimFileEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_LOADED, "");
251     LoadVoiceMail();
252     if (TELEPHONY_EXT_WRAPPER.onAllFilesFetchedExt_) {
253         TELEPHONY_EXT_WRAPPER.onAllFilesFetchedExt_(slotId_);
254     }
255 }
256 
ProcessIccReady(const AppExecFwk::InnerEvent::Pointer & event)257 bool SimFile::ProcessIccReady(const AppExecFwk::InnerEvent::Pointer &event)
258 {
259     TELEPHONY_LOGI("SimFile::SIM_STATE_READY received slotId = %{public}d", slotId_);
260     CardType cardType = stateManager_->GetCardType();
261     if (cardType == CardType::SINGLE_MODE_USIM_CARD || cardType == CardType::SINGLE_MODE_SIM_CARD) {
262         LoadSimFiles();
263     } else {
264         TELEPHONY_LOGI("invalid SimFile::SIM_STATE_READY received %{public}d", cardType);
265     }
266     return false;
267 }
268 
ProcessIccLocked(const AppExecFwk::InnerEvent::Pointer & event)269 bool SimFile::ProcessIccLocked(const AppExecFwk::InnerEvent::Pointer &event)
270 {
271     TELEPHONY_LOGI("only fetch ELEMENTARY_FILE_LI, ELEMENTARY_FILE_PL and ELEMENTARY_FILE_ICCID in locked state");
272     IccFile::ProcessIccLocked();
273     lockQueried_ = true;
274     AppExecFwk::InnerEvent::Pointer eventIccId = BuildCallerInfo(MSG_SIM_OBTAIN_ICCID_DONE);
275     if (fileController_ == nullptr) {
276         TELEPHONY_LOGE("fileController_ is nullptr!");
277         return false;
278     }
279     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_ICCID, eventIccId);
280     fileToGet_++;
281     return false;
282 }
283 
ObtainCallForwardFiles()284 void SimFile::ObtainCallForwardFiles()
285 {
286     fileQueried_ = true;
287 
288     AppExecFwk::InnerEvent::Pointer eventCFIS = BuildCallerInfo(MSG_SIM_OBTAIN_CFIS_DONE);
289     fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_CFIS, 1, eventCFIS);
290     fileToGet_++;
291 
292     AppExecFwk::InnerEvent::Pointer eventCFF = BuildCallerInfo(MSG_SIM_OBTAIN_CFF_DONE);
293     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_CFF_CPHS, eventCFF);
294     fileToGet_++;
295 }
296 
LoadSimOtherFile()297 void SimFile::LoadSimOtherFile()
298 {
299     if (serviceTable_.empty()) {
300         TELEPHONY_LOGE("get sst fail, serviceTable_ is null");
301         return;
302     }
303     if (IsServiceAvailable(UsimService::USIM_PLMN_NETWORK_NAME)) {
304         AppExecFwk::InnerEvent::Pointer eventPnn = BuildCallerInfo(MSG_SIM_OBTAIN_PNN_DONE);
305         fileController_->ObtainAllLinearFixedFile(ELEMENTARY_FILE_PNN, eventPnn);
306         fileToGet_++;
307     }
308     if (IsServiceAvailable(UsimService::USIM_OPERATOR_PLMN_LIST)) {
309         AppExecFwk::InnerEvent::Pointer eventOpl = BuildCallerInfo(MSG_SIM_OBTAIN_OPL_DONE);
310         fileController_->ObtainAllLinearFixedFile(ELEMENTARY_FILE_OPL, eventOpl);
311         fileToGet_++;
312     }
313     if (IsServiceAvailable(UsimService::USIM_FOR_5GS_OPERATOR_PLMN_LIST)) {
314         AppExecFwk::InnerEvent::Pointer eventOpl5g = BuildCallerInfo(MSG_SIM_OBTAIN_OPL5G_DONE);
315         fileController_->ObtainAllLinearFixedFile(ELEMENTARY_FILE_OPL5G, eventOpl5g);
316         fileToGet_++;
317     }
318     if (IsServiceAvailable(UsimService::USIM_MSISDN)) {
319         AppExecFwk::InnerEvent::Pointer phoneNumberEvent =
320             CreateDiallingNumberPointer(MSG_SIM_OBTAIN_MSISDN_DONE, 0, 0, nullptr);
321         diallingNumberHandler_->GetDiallingNumbers(
322             ELEMENTARY_FILE_MSISDN, ObtainExtensionElementaryFile(ELEMENTARY_FILE_MSISDN), 1, phoneNumberEvent);
323         fileToGet_++;
324     }
325     if (IsServiceAvailable(UsimService::USIM_MBDN)) {
326         AppExecFwk::InnerEvent::Pointer eventMBI = BuildCallerInfo(MSG_SIM_OBTAIN_MBI_DONE);
327         fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_MBI, 1, eventMBI);
328         fileToGet_++;
329     }
330     if (IsServiceAvailable(UsimService::USIM_MWI_STATUS)) {
331         AppExecFwk::InnerEvent::Pointer eventMWIS = BuildCallerInfo(MSG_SIM_OBTAIN_MWIS_DONE);
332         fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_MWIS, 1, eventMWIS);
333         fileToGet_++;
334     }
335     AppExecFwk::InnerEvent::Pointer eventCPHS = BuildCallerInfo(MSG_SIM_OBTAIN_VOICE_MAIL_INDICATOR_CPHS_DONE);
336     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_VOICE_MAIL_INDICATOR_CPHS, eventCPHS);
337     fileToGet_++;
338 }
339 
LoadSimFiles()340 void SimFile::LoadSimFiles()
341 {
342     TELEPHONY_LOGI("SimFile LoadSimFiles started");
343     fileQueried_ = true;
344     fileToGet_ = 0;
345 
346     AppExecFwk::InnerEvent::Pointer eventIMSI = BuildCallerInfo(MSG_SIM_OBTAIN_IMSI_DONE);
347     telRilManager_->GetImsi(slotId_, eventIMSI);
348     fileToGet_++;
349 
350     auto iccFileExt = iccFile_.lock();
351     if (TELEPHONY_EXT_WRAPPER.createIccFileExt_ != nullptr && iccFileExt) {
352         iccFileExt->LoadSimMatchedFileFromRilCache();
353     } else {
354         AppExecFwk::InnerEvent::Pointer eventIccId = BuildCallerInfo(MSG_SIM_OBTAIN_ICCID_DONE);
355         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_ICCID, eventIccId);
356         fileToGet_++;
357         AppExecFwk::InnerEvent::Pointer eventAD = BuildCallerInfo(MSG_SIM_OBTAIN_AD_DONE);
358         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_AD, eventAD);
359         fileToGet_++;
360         AppExecFwk::InnerEvent::Pointer eventSST = BuildCallerInfo(MSG_SIM_OBTAIN_SST_DONE);
361         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SST, eventSST);
362         fileToGet_++;
363         AppExecFwk::InnerEvent::Pointer eventGid1 = BuildCallerInfo(MSG_SIM_OBTAIN_GID1_DONE);
364         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_GID1, eventGid1);
365         fileToGet_++;
366         AppExecFwk::InnerEvent::Pointer eventGid2 = BuildCallerInfo(MSG_SIM_OBTAIN_GID2_DONE);
367         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_GID2, eventGid2);
368         fileToGet_++;
369     }
370     ObtainCallForwardFiles();
371     AppExecFwk::InnerEvent::Pointer eventCPHS = BuildCallerInfo(MSG_SIM_OBTAIN_VOICE_MAIL_INDICATOR_CPHS_DONE);
372     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_VOICE_MAIL_INDICATOR_CPHS, eventCPHS);
373     fileToGet_++;
374 }
375 
ObtainSpnPhase(bool start,const AppExecFwk::InnerEvent::Pointer & event)376 void SimFile::ObtainSpnPhase(bool start, const AppExecFwk::InnerEvent::Pointer &event)
377 {
378     SpnStatus curStatus = spnStatus_;
379     if (!IsContinueGetSpn(start, curStatus, spnStatus_)) {
380         return;
381     }
382 
383     TELEPHONY_LOGI("SimFile::ObtainSpnPhase state is %{public}d slotId is %{public}d", spnStatus_, slotId_);
384     if (spnStatus_ == OBTAIN_SPN_START) {
385         StartObtainSpn();
386     } else if (spnStatus_ == OBTAIN_SPN_GENERAL) {
387         ProcessSpnGeneral(event);
388     } else if (spnStatus_ == OBTAIN_OPERATOR_NAMESTRING) {
389         ProcessSpnCphs(event);
390     } else if (spnStatus_ == OBTAIN_OPERATOR_NAME_SHORTFORM) {
391         ProcessSpnShortCphs(event);
392     } else {
393         spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
394     }
395 }
396 
StartObtainSpn()397 void SimFile::StartObtainSpn()
398 {
399     UpdateSPN(IccFileController::NULLSTR);
400     auto iccFileExt = iccFile_.lock();
401     if (TELEPHONY_EXT_WRAPPER.createIccFileExt_ != nullptr && iccFileExt) {
402         iccFileExt->LoadSimMatchedFileFromRilCacheByEfid(ELEMENTARY_FILE_SPN);
403     } else {
404         AppExecFwk::InnerEvent::Pointer eventSPN = BuildCallerInfo(MSG_SIM_OBTAIN_SPN_DONE);
405         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SPN, eventSPN);
406     }
407     fileToGet_++;
408     spnStatus_ = SpnStatus::OBTAIN_SPN_GENERAL;
409 }
410 
ProcessSpnGeneral(const AppExecFwk::InnerEvent::Pointer & event)411 void SimFile::ProcessSpnGeneral(const AppExecFwk::InnerEvent::Pointer &event)
412 {
413     if (event == nullptr) {
414         TELEPHONY_LOGE("event is nullptr!");
415         return;
416     }
417     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
418     if (fd != nullptr && fd->exception == nullptr) {
419         std::string iccData = fd->resultData;
420         int length = 0;
421         std::shared_ptr<unsigned char> hexData = SIMUtils::HexStringConvertToBytes(iccData, length);
422         if (hexData != nullptr) {
423             unsigned char *byteData = hexData.get();
424             unsigned char value = byteData[0];
425             displayConditionOfSpn_ = (BYTE_NUM & value);
426         }
427         std::string str = ParseSpn(iccData, spnStatus_);
428         UpdateSPN(str);
429         std::string spn = ObtainSPN();
430         if (spn.empty() || !spn.size()) {
431             spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAMESTRING;
432         } else {
433             TELEPHONY_LOGI("SimFile Load Spn3Gpp done");
434             FileChangeToExt(spn, FileChangeType::SPN_FILE_LOAD);
435             spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
436         }
437     } else {
438         spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAMESTRING;
439     }
440 
441     if (spnStatus_ == SpnStatus::OBTAIN_OPERATOR_NAMESTRING) {
442         auto iccFileExt = iccFile_.lock();
443         if (TELEPHONY_EXT_WRAPPER.createIccFileExt_ != nullptr && iccFileExt) {
444             iccFileExt->LoadSimMatchedFileFromRilCacheByEfid(ELEMENTARY_FILE_SPN_CPHS);
445         } else {
446             AppExecFwk::InnerEvent::Pointer eventCphs = BuildCallerInfo(MSG_SIM_OBTAIN_SPN_DONE);
447             fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SPN_CPHS, eventCphs);
448         }
449         fileToGet_++;
450         displayConditionOfSpn_ = 0;
451     }
452 }
453 
ProcessSpnCphs(const AppExecFwk::InnerEvent::Pointer & event)454 void SimFile::ProcessSpnCphs(const AppExecFwk::InnerEvent::Pointer &event)
455 {
456     if (event == nullptr) {
457         TELEPHONY_LOGE("event is nullptr!");
458         return;
459     }
460     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
461     if (fd != nullptr && fd->exception == nullptr) {
462         std::string iccData = fd->resultData;
463         UpdateSPN(ParseSpn(iccData, spnStatus_));
464         std::string spn = ObtainSPN();
465         if (spn.empty() || !spn.size()) {
466             spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAME_SHORTFORM;
467         } else {
468             displayConditionOfSpn_ = 0;
469             TELEPHONY_LOGI("SimFile Load ELEMENTARY_FILE_SPN_CPHS done: %{public}s", spn.c_str());
470             FileChangeToExt(spn, FileChangeType::SPN_FILE_LOAD);
471             spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
472         }
473     } else {
474         spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAME_SHORTFORM;
475     }
476 
477     if (spnStatus_ == SpnStatus::OBTAIN_OPERATOR_NAME_SHORTFORM) {
478         auto iccFileExt = iccFile_.lock();
479         if (TELEPHONY_EXT_WRAPPER.createIccFileExt_ != nullptr && iccFileExt) {
480             iccFileExt->LoadSimMatchedFileFromRilCacheByEfid(ELEMENTARY_FILE_SPN_SHORT_CPHS);
481         } else {
482             AppExecFwk::InnerEvent::Pointer eventShortCphs = BuildCallerInfo(MSG_SIM_OBTAIN_SPN_DONE);
483             fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SPN_SHORT_CPHS, eventShortCphs);
484         }
485         fileToGet_++;
486     }
487 }
488 
ProcessSpnShortCphs(const AppExecFwk::InnerEvent::Pointer & event)489 void SimFile::ProcessSpnShortCphs(const AppExecFwk::InnerEvent::Pointer &event)
490 {
491     if (event == nullptr) {
492         TELEPHONY_LOGE("event is nullptr!");
493         return;
494     }
495     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
496     if (fd != nullptr && fd->exception == nullptr) {
497         std::string iccData = fd->resultData;
498         UpdateSPN(ParseSpn(iccData, spnStatus_));
499         std::string spn = ObtainSPN();
500         if (spn.empty() || !spn.size()) {
501             TELEPHONY_LOGI("SimFile No SPN loaded");
502         } else {
503             displayConditionOfSpn_ = 0;
504             TELEPHONY_LOGI("SimFile Load ELEMENTARY_FILE_SPN_SHORT_CPHS");
505         }
506     } else {
507         UpdateSPN(IccFileController::NULLSTR);
508         TELEPHONY_LOGI("SimFile No SPN get in either CHPS or 3GPP");
509     }
510     FileChangeToExt(spn_, FileChangeType::SPN_FILE_LOAD);
511     spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
512 }
513 
ParseSpn(const std::string & rawData,int spnStatus)514 std::string SimFile::ParseSpn(const std::string &rawData, int spnStatus)
515 {
516     int offset = 0;
517     int length = 0;
518     std::shared_ptr<unsigned char> bytesRaw = SIMUtils::HexStringConvertToBytes(rawData, length);
519     std::shared_ptr<unsigned char> bytesNew = nullptr;
520     if (bytesRaw == nullptr) {
521         TELEPHONY_LOGE("ParseSpn invalid data: %{public}s", rawData.c_str());
522         return "";
523     }
524     TELEPHONY_LOGI("ParseSpn rawData: %{public}s, %{public}d, %{public}d", rawData.c_str(), spnStatus, length);
525     if (spnStatus == OBTAIN_SPN_GENERAL) {
526         offset = 0;
527         length -= INVALID_BYTES_NUM;
528         bytesNew = std::shared_ptr<unsigned char>(
529             bytesRaw.get() + INVALID_BYTES_NUM, [bytesRaw](unsigned char *) {}); // first is 0, +1
530     } else if ((spnStatus == OBTAIN_OPERATOR_NAMESTRING) || (spnStatus == OBTAIN_OPERATOR_NAME_SHORTFORM)) {
531         offset = 0;
532         bytesNew = bytesRaw;
533     } else {
534         return "";
535     }
536     std::string ret = SIMUtils::DiallingNumberStringFieldConvertToString(bytesNew, offset, length, SPN_CHAR_POS);
537     TELEPHONY_LOGI("SimFile::ParseSpn spn: %{public}s", ret.c_str());
538     return ret;
539 }
540 
ParsePnn(const std::vector<std::string> & records)541 void SimFile::ParsePnn(const std::vector<std::string> &records)
542 {
543     pnnFiles_.clear();
544     if (records.empty()) {
545         TELEPHONY_LOGI("ParsePnn records is empty");
546         return;
547     }
548     for (const auto &dataPnn : records) {
549         TELEPHONY_LOGI("ParsePnn: %{public}s", dataPnn.c_str());
550         int recordLen = 0;
551         std::shared_ptr<unsigned char> data = SIMUtils::HexStringConvertToBytes(dataPnn, recordLen);
552         if (data == nullptr) {
553             TELEPHONY_LOGD("ParsePnn data is nullptr");
554             continue;
555         }
556         unsigned char *tlv = data.get();
557         std::shared_ptr<PlmnNetworkName> file = std::make_shared<PlmnNetworkName>();
558         int tagAndLength = NETWORK_NAME_LENGTH + 1;
559         if (recordLen <= tagAndLength) {
560             TELEPHONY_LOGD("recordLen <= tagAndLength");
561             continue;
562         }
563         if (recordLen >= (tagAndLength + static_cast<int>(tlv[NETWORK_NAME_LENGTH])) &&
564             tlv[NETWORK_NAME_IEI] == (unsigned char)LONG_NAME_FLAG) {
565             file->longName =
566                 SIMUtils::Gsm7bitConvertToString(tlv + NETWORK_NAME_TEXT_STRING, tlv[NETWORK_NAME_LENGTH] - 1);
567         }
568         int shortNameOffset = tagAndLength + tlv[NETWORK_NAME_LENGTH];
569         if (recordLen > (shortNameOffset + tagAndLength) &&
570             recordLen >=
571             (shortNameOffset + tagAndLength + static_cast<int>(tlv[shortNameOffset + NETWORK_NAME_LENGTH])) &&
572             tlv[shortNameOffset + NETWORK_NAME_IEI] == (unsigned char)SHORT_NAME_FLAG) {
573             file->shortName = SIMUtils::Gsm7bitConvertToString(
574                 tlv + (shortNameOffset + NETWORK_NAME_TEXT_STRING), tlv[shortNameOffset + NETWORK_NAME_LENGTH] - 1);
575         }
576         TELEPHONY_LOGI("longName: %{public}s, shortName: %{public}s", file->longName.c_str(), file->shortName.c_str());
577         pnnFiles_.push_back(file);
578     }
579 }
580 
ParseOpl(const std::vector<std::string> & records)581 void SimFile::ParseOpl(const std::vector<std::string> &records)
582 {
583     oplFiles_.clear();
584     if (records.empty()) {
585         TELEPHONY_LOGI("ParseOpl records is empty");
586         return;
587     }
588     for (const auto &dataOpl : records) {
589         TELEPHONY_LOGD("ParseOpl: %{public}s", dataOpl.c_str());
590         if (dataOpl.size() != (BYTE_LENGTH + BYTE_LENGTH)) {
591             continue;
592         }
593         std::string plmn = SIMUtils::BcdPlmnConvertToString(dataOpl, 0);
594         if (plmn.empty()) {
595             continue;
596         }
597         std::shared_ptr<OperatorPlmnInfo> file = std::make_shared<OperatorPlmnInfo>();
598         file->plmnNumeric = plmn;
599         if (!regex_match(dataOpl, std::regex("[0-9a-fA-F]+"))) {
600             TELEPHONY_LOGI("InputValue is not a hexadecimal number");
601             continue;
602         }
603         file->lacStart = stoi(dataOpl.substr(MCCMNC_LEN, HALF_BYTE_LEN), 0, HEXADECIMAL);
604         file->lacEnd = stoi(dataOpl.substr(MCCMNC_LEN + HALF_BYTE_LEN, HALF_BYTE_LEN), 0, HEXADECIMAL);
605         file->pnnRecordId = stoi(dataOpl.substr(MCCMNC_LEN + BYTE_LENGTH, HALF_LEN), 0, HEXADECIMAL);
606         TELEPHONY_LOGI("plmnNumeric: %{public}s, lacStart: %{public}d, lacEnd: %{public}d, pnnRecordId: %{public}d",
607             file->plmnNumeric.c_str(), file->lacStart, file->lacEnd, file->pnnRecordId);
608         oplFiles_.push_back(file);
609     }
610 }
611 
ParseOpl5g(const std::vector<std::string> & records)612 void SimFile::ParseOpl5g(const std::vector<std::string> &records)
613 {
614     opl5gFiles_.clear();
615     if (records.empty()) {
616         TELEPHONY_LOGE("ParseOpl5g records is empty");
617         return;
618     }
619     for (const auto &dataOpl : records) {
620         TELEPHONY_LOGD("ParseOpl5g: %{public}s", dataOpl.c_str());
621         if (dataOpl.size() != (OPL_5G_LENGTH + OPL_5G_LENGTH)) {
622             continue;
623         }
624         std::string plmn = SIMUtils::BcdPlmnConvertToString(dataOpl, 0);
625         if (plmn.empty()) {
626             continue;
627         }
628         std::shared_ptr<OperatorPlmnInfo> file = std::make_shared<OperatorPlmnInfo>();
629         file->plmnNumeric = plmn;
630         if (!regex_match(dataOpl, std::regex("[0-9a-fA-F]+"))) {
631             TELEPHONY_LOGI("InputValue is not a hexadecimal number");
632             continue;
633         }
634         file->lacStart = stoi(dataOpl.substr(MCCMNC_LEN, LAC_RANGE_LEN), 0, HEXADECIMAL);
635         file->lacEnd = stoi(dataOpl.substr(MCCMNC_LEN + LAC_RANGE_LEN, LAC_RANGE_LEN), 0, HEXADECIMAL);
636         file->pnnRecordId = stoi(dataOpl.substr(MCCMNC_LEN + LAC_RANGE_LEN + LAC_RANGE_LEN, HALF_LEN), 0, HEXADECIMAL);
637         TELEPHONY_LOGD("plmnNumeric: %{public}s, lacStart: %{public}d, lacEnd: %{public}d, pnnRecordId: %{public}d",
638             file->plmnNumeric.c_str(), file->lacStart, file->lacEnd, file->pnnRecordId);
639         opl5gFiles_.push_back(file);
640     }
641 }
642 
ObtainUsimFunctionHandle()643 std::shared_ptr<UsimFunctionHandle> SimFile::ObtainUsimFunctionHandle()
644 {
645     return UsimFunctionHandle_;
646 }
647 
UpdateSimLanguage()648 void SimFile::UpdateSimLanguage()
649 {
650     AppExecFwk::InnerEvent::Pointer eventLILAN = BuildCallerInfo(MSG_SIM_OBTAIN_LI_LANGUAGE_DONE);
651     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_LI, eventLILAN);
652 }
653 
ProcessObtainLiLanguage(const AppExecFwk::InnerEvent::Pointer & event)654 bool SimFile::ProcessObtainLiLanguage(const AppExecFwk::InnerEvent::Pointer &event)
655 {
656     if (event == nullptr) {
657         TELEPHONY_LOGE("event is nullptr!");
658         return true;
659     }
660     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
661     if (fd != nullptr && fd->exception == nullptr) {
662         efLi_ = fd->resultData;
663         if (efLi_.empty() || !efLi_.size()) {
664             TELEPHONY_LOGI("efLi_ No language loaded");
665             AppExecFwk::InnerEvent::Pointer eventPLLAN = BuildCallerInfo(MSG_SIM_OBTAIN_PL_LANGUAGE_DONE);
666             fileController_->ObtainBinaryFile(ELEMENTARY_FILE_PL, eventPLLAN);
667         } else {
668             TELEPHONY_LOGI("efLi_  language loaded");
669             UpdateIccLanguage(efLi_, efPl_);
670         }
671     }
672     return true;
673 }
674 
ProcessObtainPlLanguage(const AppExecFwk::InnerEvent::Pointer & event)675 bool SimFile::ProcessObtainPlLanguage(const AppExecFwk::InnerEvent::Pointer &event)
676 {
677     if (event == nullptr) {
678         TELEPHONY_LOGE("event is nullptr!");
679         return true;
680     }
681     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
682     if (fd != nullptr && fd->exception == nullptr) {
683         efPl_ = fd->resultData;
684         if (efPl_.empty() || !efPl_.size()) {
685             TELEPHONY_LOGI("efPl_ No language loaded");
686         } else {
687             TELEPHONY_LOGI("efPl_  language loaded");
688             UpdateIccLanguage(efLi_, efPl_);
689         }
690     }
691     return true;
692 }
693 
AnalysisBcdPlmn(std::string data,std::string description)694 std::string SimFile::AnalysisBcdPlmn(std::string data, std::string description)
695 {
696     return "";
697 }
698 
ProcessElementaryFileCsp(std::string data)699 void SimFile::ProcessElementaryFileCsp(std::string data) {}
700 
AnalysisElementaryFileSpdi(std::string data)701 void SimFile::AnalysisElementaryFileSpdi(std::string data) {}
702 
ProcessSmses(std::string messages)703 void SimFile::ProcessSmses(std::string messages) {}
704 
ProcessSms(std::string data)705 void SimFile::ProcessSms(std::string data) {}
706 
ProcessObtainGid1Done(const AppExecFwk::InnerEvent::Pointer & event)707 bool SimFile::ProcessObtainGid1Done(const AppExecFwk::InnerEvent::Pointer &event)
708 {
709     bool isFileProcessResponse = true;
710     if (event == nullptr) {
711         TELEPHONY_LOGE("get GID1 event is nullptr!");
712         return isFileProcessResponse;
713     }
714     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
715     if (fd == nullptr) {
716         TELEPHONY_LOGE("get GID1 fd is nullptr!");
717         return isFileProcessResponse;
718     }
719     std::string iccData = fd->resultData;
720     char *rawData = const_cast<char *>(iccData.c_str());
721     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
722 
723     if (fd->exception != nullptr) {
724         TELEPHONY_LOGE("SimFile failed in get GID1");
725         gid1_ = "";
726         return isFileProcessResponse;
727     }
728 
729     gid1_ = iccData;
730     FileChangeToExt(gid1_, FileChangeType::GID1_FILE_LOAD);
731     TELEPHONY_LOGI("SimFile GID1: %{public}s", fileData);
732     return isFileProcessResponse;
733 }
734 
ProcessObtainGid2Done(const AppExecFwk::InnerEvent::Pointer & event)735 bool SimFile::ProcessObtainGid2Done(const AppExecFwk::InnerEvent::Pointer &event)
736 {
737     bool isFileProcessResponse = true;
738     if (event == nullptr) {
739         TELEPHONY_LOGE("get GID2 event is nullptr!");
740         return isFileProcessResponse;
741     }
742     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
743     if (fd == nullptr) {
744         TELEPHONY_LOGE("get GID2 fd is nullptr!");
745         return isFileProcessResponse;
746     }
747     std::string iccData = fd->resultData;
748     char *rawData = const_cast<char *>(iccData.c_str());
749     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
750 
751     if (fd->exception != nullptr) {
752         TELEPHONY_LOGE("SimFile failed in get GID2");
753         gid2_ = "";
754         return isFileProcessResponse;
755     }
756 
757     gid2_ = iccData;
758     FileChangeToExt(gid2_, FileChangeType::GID2_FILE_LOAD);
759     TELEPHONY_LOGI("SimFile GID2: %{public}s", fileData);
760     return isFileProcessResponse;
761 }
762 
ProcessGetMsisdnDone(const AppExecFwk::InnerEvent::Pointer & event)763 bool SimFile::ProcessGetMsisdnDone(const AppExecFwk::InnerEvent::Pointer &event)
764 {
765     bool isFileProcessResponse = true;
766     if (event == nullptr) {
767         TELEPHONY_LOGE("event is nullptr!");
768         return isFileProcessResponse;
769     }
770     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
771     if (fd == nullptr) {
772         TELEPHONY_LOGE("fd is nullptr!");
773         return isFileProcessResponse;
774     }
775     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
776     if (fd->exception != nullptr) {
777         TELEPHONY_LOGE("SimFile Invalid or missing EF[MSISDN]");
778         return isFileProcessResponse;
779     }
780     msisdn_ = Str16ToStr8(diallingNumber->GetNumber());
781     msisdnTag_ = Str16ToStr8(diallingNumber->GetName());
782     return isFileProcessResponse;
783 }
784 
ProcessSetMsisdnDone(const AppExecFwk::InnerEvent::Pointer & event)785 bool SimFile::ProcessSetMsisdnDone(const AppExecFwk::InnerEvent::Pointer &event)
786 {
787     bool isFileProcessResponse = true;
788     if (event == nullptr) {
789         TELEPHONY_LOGE("update Msisdn event is nullptr!");
790         return isFileProcessResponse;
791     }
792     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
793     if (fd == nullptr) {
794         TELEPHONY_LOGE("update Msisdn fd is nullptr!");
795         return isFileProcessResponse;
796     }
797     std::string iccData = fd->resultData;
798     if (fd->exception == nullptr) {
799         msisdn_ = lastMsisdn_;
800         msisdnTag_ = lastMsisdnTag_;
801         waitResult_ = true;
802         processWait_.notify_all();
803         TELEPHONY_LOGI("SimFile Success to update EF[MSISDN]");
804     } else {
805         processWait_.notify_all();
806         TELEPHONY_LOGE("SimFile Fail to update EF[MSISDN]");
807     }
808     return isFileProcessResponse;
809 }
810 
ProcessGetSpdiDone(const AppExecFwk::InnerEvent::Pointer & event)811 bool SimFile::ProcessGetSpdiDone(const AppExecFwk::InnerEvent::Pointer &event)
812 {
813     bool isFileProcessResponse = true;
814     if (event == nullptr) {
815         TELEPHONY_LOGE("get Spdi event is nullptr!");
816         return isFileProcessResponse;
817     }
818     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
819     if (fd == nullptr) {
820         TELEPHONY_LOGE("get Spdi fd is nullptr!");
821         return isFileProcessResponse;
822     }
823     std::string iccData = fd->resultData;
824     char *rawData = const_cast<char *>(iccData.c_str());
825     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
826 
827     if (fd->exception != nullptr) {
828         return isFileProcessResponse;
829     }
830     TELEPHONY_LOGI("SimFile MSG_SIM_OBTAIN_SPDI_DONE data:%{public}s", fileData);
831     AnalysisElementaryFileSpdi(iccData);
832     return isFileProcessResponse;
833 }
834 
ProcessGetCfisDone(const AppExecFwk::InnerEvent::Pointer & event)835 bool SimFile::ProcessGetCfisDone(const AppExecFwk::InnerEvent::Pointer &event)
836 {
837     bool isFileProcessResponse = true;
838     if (event == nullptr) {
839         TELEPHONY_LOGE("get Cfis event is nullptr!");
840         return isFileProcessResponse;
841     }
842     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
843     if (fd == nullptr) {
844         TELEPHONY_LOGE("get Cfis fd is nullptr!");
845         return isFileProcessResponse;
846     }
847     efCfisStr_ = fd->resultData;
848     std::shared_ptr<unsigned char> rawData = SIMUtils::HexStringConvertToBytes(efCfisStr_, efCfisSize_);
849     if (rawData == nullptr) {
850         TELEPHONY_LOGE("rawData is nullptr");
851         return isFileProcessResponse;
852     }
853     if (fd->exception != nullptr) {
854         efCfis_ = nullptr;
855     } else {
856         unsigned char *fileData = rawData.get();
857         TELEPHONY_LOGI("ELEMENTARY_FILE_CFIS: %{public}s", fileData);
858         efCfis_ = fileData;
859         if (EfCfisAvailable(efCfisSize_)) {
860             // Refer TS 51.011 Section 10.3.46 for the content description
861             callForwardingStatus = (efCfis_[1] & BYTE_NUM2);
862             DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCfuIndicator(
863                 slotId_, callForwardingStatus == CALL_FORWARDING_STATUS_ENABLED);
864         }
865     }
866     return isFileProcessResponse;
867 }
868 
ProcessGetMbiDone(const AppExecFwk::InnerEvent::Pointer & event)869 bool SimFile::ProcessGetMbiDone(const AppExecFwk::InnerEvent::Pointer &event)
870 {
871     bool isFileProcessResponse = true;
872     if (event == nullptr) {
873         TELEPHONY_LOGE("get Mbi event is nullptr!");
874         return isFileProcessResponse;
875     }
876     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
877     if (fd == nullptr) {
878         TELEPHONY_LOGE("get Mbi fd is nullptr!");
879         return isFileProcessResponse;
880     }
881     std::string iccData = fd->resultData;
882     char *rawData = const_cast<char *>(iccData.c_str());
883     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
884     if (fd->exception == nullptr) {
885         int dataLen = 0;
886         std::shared_ptr<unsigned char> dataByte = SIMUtils::HexStringConvertToBytes(iccData, dataLen);
887         int index = (dataByte != nullptr) ? (dataByte.get()[0] & BYTE_NUM) : 0;
888         if (index != 0 && index != BYTE_NUM) {
889             indexOfMailbox_ = index;
890             TELEPHONY_LOGI("fetch valid mailbox number for MBDN");
891         }
892     }
893     TELEPHONY_LOGI("ELEMENTARY_FILE_MBI data is:%{public}s id: %{public}d", fileData, indexOfMailbox_);
894     fileToGet_ += LOAD_STEP;
895     AppExecFwk::InnerEvent::Pointer mbdnEvent = CreateDiallingNumberPointer(MSG_SIM_OBTAIN_MBDN_DONE, 0, 0, nullptr);
896     diallingNumberHandler_->GetDiallingNumbers(ELEMENTARY_FILE_MBDN, ELEMENTARY_FILE_EXT6, indexOfMailbox_, mbdnEvent);
897     return isFileProcessResponse;
898 }
899 
ProcessGetMbdnDone(const AppExecFwk::InnerEvent::Pointer & event)900 bool SimFile::ProcessGetMbdnDone(const AppExecFwk::InnerEvent::Pointer &event)
901 {
902     if (TELEPHONY_EXT_WRAPPER.setVoiceMailOnSimExt_ != nullptr) {
903         std::string nullStr = IccFileController::NULLSTR;
904         TELEPHONY_EXT_WRAPPER.setVoiceMailOnSimExt_(slotId_, nullStr.c_str(), nullStr.c_str());
905     }
906     bool isFileProcessResponse = true;
907     if (event == nullptr) {
908         TELEPHONY_LOGE("get Mbdn event is nullptr!");
909         return isFileProcessResponse;
910     }
911     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
912     if (fd == nullptr) {
913         TELEPHONY_LOGE("get Mbdn fd is nullptr!");
914         return isFileProcessResponse;
915     }
916     bool hasException = fd->exception == nullptr;
917     TELEPHONY_LOGI("ProcessGetMbdnDone start %{public}d", hasException);
918     std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
919     voiceMailNum_ = IccFileController::NULLSTR;
920     voiceMailTag_ = IccFileController::NULLSTR;
921     if (fd->exception != nullptr) {
922         TELEPHONY_LOGE("SimFile failed missing EF MBDN");
923         GetCphsMailBox();
924         return isFileProcessResponse;
925     }
926     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
927     if (diallingNumber == nullptr) {
928         TELEPHONY_LOGE("ProcessGetMbdnDone get null diallingNumber!!");
929         return isFileProcessResponse;
930     }
931 
932     if (diallingNumber->IsEmpty()) {
933         GetCphsMailBox();
934         return isFileProcessResponse;
935     }
936     voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
937     voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
938     if (TELEPHONY_EXT_WRAPPER.setVoiceMailOnSimExt_ != nullptr) {
939         TELEPHONY_EXT_WRAPPER.setVoiceMailOnSimExt_(slotId_, voiceMailNum_.c_str(), voiceMailTag_.c_str());
940     }
941     TELEPHONY_LOGI("ProcessGetMbdnDone success");
942     return isFileProcessResponse;
943 }
944 
ProcessGetCphsMailBoxDone(const AppExecFwk::InnerEvent::Pointer & event)945 bool SimFile::ProcessGetCphsMailBoxDone(const AppExecFwk::InnerEvent::Pointer &event)
946 {
947     bool isFileProcessResponse = true;
948     if (event == nullptr) {
949         TELEPHONY_LOGE("get Cphs Mailbox event is nullptr!");
950         return isFileProcessResponse;
951     }
952     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
953     if (fd == nullptr) {
954         TELEPHONY_LOGE("get Cphs Mailbox fd is nullptr!");
955         return isFileProcessResponse;
956     }
957     bool hasException = fd->exception == nullptr;
958     TELEPHONY_LOGI("ProcessGetCphsMailBoxDone start %{public}d", hasException);
959     std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
960     voiceMailNum_ = IccFileController::NULLSTR;
961     voiceMailTag_ = IccFileController::NULLSTR;
962     if (fd->exception != nullptr) {
963         TELEPHONY_LOGE("SimFile failed missing CPHS MAILBOX");
964         return isFileProcessResponse;
965     }
966     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
967     if (diallingNumber == nullptr) {
968         TELEPHONY_LOGE("GetCphsMailBoxDone get null diallingNumber!!");
969         return isFileProcessResponse;
970     }
971 
972     voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
973     voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
974     TELEPHONY_LOGI("GetCphsMailBoxDone success");
975     return isFileProcessResponse;
976 }
977 
GetCphsMailBox()978 void SimFile::GetCphsMailBox()
979 {
980     fileToGet_ += LOAD_STEP;
981     AppExecFwk::InnerEvent::Pointer cphsEvent =
982         CreateDiallingNumberPointer(MSG_SIM_OBTAIN_CPHS_MAILBOX_DONE, 0, 0, nullptr);
983     diallingNumberHandler_->GetDiallingNumbers(ELEMENTARY_FILE_MAILBOX_CPHS, ELEMENTARY_FILE_EXT1, 1, cphsEvent);
984 }
985 
ProcessGetMwisDone(const AppExecFwk::InnerEvent::Pointer & event)986 bool SimFile::ProcessGetMwisDone(const AppExecFwk::InnerEvent::Pointer &event)
987 {
988     bool isFileProcessResponse = true;
989     if (event == nullptr) {
990         TELEPHONY_LOGE("get Mwis event is nullptr!");
991         return isFileProcessResponse;
992     }
993     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
994     if (fd == nullptr) {
995         TELEPHONY_LOGE("get Mwis fd is nullptr!");
996         return isFileProcessResponse;
997     }
998     efMWISStr_ = fd->resultData;
999     std::shared_ptr<unsigned char> rawData = SIMUtils::HexStringConvertToBytes(efMWISStr_, efMWISSize_);
1000     if (rawData == nullptr) {
1001         TELEPHONY_LOGE("rawData is nullptr");
1002         return isFileProcessResponse;
1003     }
1004     if (fd->exception != nullptr) {
1005         TELEPHONY_LOGE("MSG_SIM_OBTAIN_MWIS_DONE exception is nullptr");
1006         return isFileProcessResponse;
1007     }
1008     unsigned char *fileData = rawData.get();
1009     TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_MWIS : %{public}s", fileData);
1010     unsigned char value = fileData[0];
1011     if ((value & BYTE_NUM) == BYTE_NUM) {
1012         TELEPHONY_LOGI("SimFiles: Uninitialized record MWIS");
1013         return isFileProcessResponse;
1014     }
1015     efMWIS_ = fileData;
1016     if (efMWIS_ != nullptr && efMWISSize_ > 1) {
1017         // Refer TS 51.011 Section 10.3.45 for the content description
1018         voiceMailWaiting_ = ((efMWIS_[0] & BYTE_NUM2) != 0);
1019         voiceMailCount_ = efMWIS_[1] & BYTE_NUM;
1020         if (voiceMailWaiting_ && (voiceMailCount_ == 0 || voiceMailCount_ == BYTE_NUM)) {
1021             voiceMailCount_ = UNKNOWN_VOICE_MAIL_COUNT;
1022         }
1023         DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateVoiceMailMsgIndicator(
1024             slotId_, voiceMailCount_ > 0);
1025     }
1026     return isFileProcessResponse;
1027 }
1028 
ProcessVoiceMailCphs(const AppExecFwk::InnerEvent::Pointer & event)1029 bool SimFile::ProcessVoiceMailCphs(const AppExecFwk::InnerEvent::Pointer &event)
1030 {
1031     bool isFileProcessResponse = true;
1032     if (event == nullptr) {
1033         TELEPHONY_LOGE("process voice mail event is nullptr!");
1034         return isFileProcessResponse;
1035     }
1036     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1037     if (fd == nullptr) {
1038         TELEPHONY_LOGE("process voice mail fd is nullptr!");
1039         return isFileProcessResponse;
1040     }
1041     efCphsMwisStr_ = fd->resultData;
1042     std::shared_ptr<unsigned char> rawData = SIMUtils::HexStringConvertToBytes(efCphsMwisStr_, efCphsMwiSize_);
1043     if (rawData == nullptr) {
1044         TELEPHONY_LOGE("rawData is nullptr");
1045         return isFileProcessResponse;
1046     }
1047     if (fd->exception != nullptr) {
1048         TELEPHONY_LOGE("MSG_SIM_OBTAIN_VOICE_MAIL_INDICATOR_CPHS_DONE exception is nullptr");
1049         return isFileProcessResponse;
1050     }
1051     unsigned char *fileData = rawData.get();
1052     TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_VOICE_MAIL_INDICATOR_CPHS: %{public}s", fileData);
1053     efCphsMwi_ = fileData;
1054     if (efCphsMwi_ != nullptr && efCphsMwiSize_ > 0 && voiceMailCount_ == DEFAULT_VOICE_MAIL_COUNT) {
1055         // Refer TS 51.011 Section 10.3.45 for the content description
1056         int indicator = static_cast<int>(efCphsMwi_[0] & BYTE_NUM3);
1057         if (indicator == BYTE_NUM4) {
1058             voiceMailCount_ = UNKNOWN_VOICE_MAIL_COUNT;
1059         } else if (indicator == BYTE_NUM5) {
1060             voiceMailCount_ = 0;
1061         }
1062     }
1063     DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateVoiceMailMsgIndicator(
1064         slotId_, voiceMailCount_ > 0);
1065     return isFileProcessResponse;
1066 }
1067 
ProcessGetIccIdDone(const AppExecFwk::InnerEvent::Pointer & event)1068 bool SimFile::ProcessGetIccIdDone(const AppExecFwk::InnerEvent::Pointer &event)
1069 {
1070     bool isFileProcessResponse = true;
1071     if (event == nullptr) {
1072         TELEPHONY_LOGE("get IccId event is nullptr!");
1073         return isFileProcessResponse;
1074     }
1075     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1076     if (fd == nullptr) {
1077         TELEPHONY_LOGE("get IccId fd is nullptr!");
1078         return isFileProcessResponse;
1079     }
1080     if (fd->exception == nullptr) {
1081         std::string iccData = fd->resultData;
1082         TELEPHONY_LOGI("ICCID length is %{public}zu, slotId:%{public}d", iccData.length(), slotId_);
1083         std::string fullIccData = iccData;
1084         GetFullIccid(fullIccData);
1085         SwapPairsForIccId(iccData);
1086         TELEPHONY_LOGI("SwapPairsForIccId ICCID length is %{public}zu", fullIccData.length());
1087         decIccId_ = iccData;
1088         iccId_ = fullIccData;
1089         FileChangeToExt(iccId_, FileChangeType::ICCID_FILE_LOAD);
1090         if (filesFetchedObser_ != nullptr) {
1091             TELEPHONY_LOGI("slotId:%{public}d iccid loaded", slotId_);
1092             filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_QUERY_ICCID_DONE, slotId_);
1093         }
1094     } else {
1095         if (iccId_.empty() && reloadIccidCount_ > 0) {
1096             fileToGet_++;
1097             SendEvent(SimFile::RELOAD_ICCID_EVENT, 0, DELAY_TIME);
1098             TELEPHONY_LOGI("retry reload iccid %{public}d, slotId is %{public}d",
1099                 static_cast<int32_t>(reloadIccidCount_), slotId_);
1100             reloadIccidCount_--;
1101         }
1102     }
1103     return isFileProcessResponse;
1104 }
1105 
ProcessReloadIccid(const AppExecFwk::InnerEvent::Pointer & event)1106 bool SimFile::ProcessReloadIccid(const AppExecFwk::InnerEvent::Pointer &event)
1107 {
1108     bool isFileProcessResponse = false;
1109     if (event == nullptr) {
1110         TELEPHONY_LOGE("ProcessReloadIccid event is nullptr");
1111         return isFileProcessResponse;
1112     }
1113     AppExecFwk::InnerEvent::Pointer eventIccId = BuildCallerInfo(MSG_SIM_OBTAIN_ICCID_DONE);
1114     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_ICCID, eventIccId);
1115     return isFileProcessResponse;
1116 }
1117 
ProcessReloadImsi(const AppExecFwk::InnerEvent::Pointer & event)1118 bool SimFile::ProcessReloadImsi(const AppExecFwk::InnerEvent::Pointer &event)
1119 {
1120     bool isFileProcessResponse = false;
1121     if (event == nullptr) {
1122         TELEPHONY_LOGE("ProcessReloadImsi event is nullptr");
1123         return isFileProcessResponse;
1124     }
1125     AppExecFwk::InnerEvent::Pointer eventIMSI = BuildCallerInfo(MSG_SIM_OBTAIN_IMSI_DONE);
1126     telRilManager_->GetImsi(slotId_, eventIMSI);
1127     hasRetryGetImsi_ = true;
1128     return isFileProcessResponse;
1129 }
1130 
ProcessObtainIMSIDone(const AppExecFwk::InnerEvent::Pointer & event)1131 bool SimFile::ProcessObtainIMSIDone(const AppExecFwk::InnerEvent::Pointer &event)
1132 {
1133     bool isFileProcessResponse = true;
1134     if (event == nullptr) {
1135         TELEPHONY_LOGE("get IMSI event is nullptr!");
1136         return isFileProcessResponse;
1137     }
1138     std::shared_ptr<std::string> sharedObject = event->GetSharedObject<std::string>();
1139     if (sharedObject == nullptr) {
1140         TELEPHONY_LOGE("get IMSI fd is nullptr!");
1141         return isFileProcessResponse;
1142     }
1143     if (sharedObject != nullptr) {
1144         imsi_ = *sharedObject;
1145         TELEPHONY_LOGI("SimFile::ProcessEvent IMSI received success");
1146         SaveCountryCode();
1147         TELEPHONY_LOGI("SimFile::ObtainIsoCountryCode result success");
1148         if (!imsi_.empty() && filesFetchedObser_ != nullptr) {
1149             CheckMncLengthForImsiDone();
1150             filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_IMSI_LOADED_READY);
1151             FileChangeToExt(imsi_, FileChangeType::G_IMSI_FILE_LOAD);
1152         } else {
1153             DelayGetImsi();
1154         }
1155     }
1156     return isFileProcessResponse;
1157 }
1158 
DelayGetImsi()1159 void SimFile::DelayGetImsi()
1160 {
1161     if (hasRetryGetImsi_) {
1162         TELEPHONY_LOGI("DelayGetImsi has retried, slotId = %{public}d", slotId_);
1163         return;
1164     }
1165     fileToGet_++;
1166     SendEvent(SimFile::RELOAD_IMSI_EVENT, 0, GET_IMSI_DELAY_TIME);
1167 }
1168 
ProcessGetCffDone(const AppExecFwk::InnerEvent::Pointer & event)1169 bool SimFile::ProcessGetCffDone(const AppExecFwk::InnerEvent::Pointer &event)
1170 {
1171     bool isFileProcessResponse = true;
1172     if (event == nullptr) {
1173         TELEPHONY_LOGE("get Cff event is nullptr!");
1174         return isFileProcessResponse;
1175     }
1176     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1177     if (fd == nullptr) {
1178         TELEPHONY_LOGE("get Cff fd is nullptr!");
1179         return isFileProcessResponse;
1180     }
1181     efCffStr_ = fd->resultData;
1182     std::shared_ptr<unsigned char> rawData = SIMUtils::HexStringConvertToBytes(efCffStr_, efCffSize_);
1183     if (rawData == nullptr) {
1184         TELEPHONY_LOGE("rawData is nullptr");
1185         return isFileProcessResponse;
1186     }
1187     if (fd->exception != nullptr) {
1188         efCff_ = nullptr;
1189     } else {
1190         unsigned char *fileData = rawData.get();
1191         TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_CFF_CPHS: %{public}s", fileData);
1192         efCff_ = fileData;
1193         if (efCff_ != nullptr && efCffSize_ > 0 && callForwardingStatus == CALL_FORWARDING_STATUS_UNKNOWN) {
1194             // Refer TS 51.011 Section 10.3.46 for the content description
1195             callForwardingStatus = ((efCff_[0] & BYTE_NUM3) == BYTE_NUM4) ? CALL_FORWARDING_STATUS_ENABLED
1196                                                                           : CALL_FORWARDING_STATUS_DISABLED;
1197         }
1198         DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCfuIndicator(
1199             slotId_, callForwardingStatus == CALL_FORWARDING_STATUS_ENABLED);
1200     }
1201     return isFileProcessResponse;
1202 }
1203 
ProcessGetAdDone(const AppExecFwk::InnerEvent::Pointer & event)1204 bool SimFile::ProcessGetAdDone(const AppExecFwk::InnerEvent::Pointer &event)
1205 {
1206     bool isFileProcessResponse = true;
1207     lengthOfMnc_ = UNKNOWN_MNC;
1208     if (event == nullptr) {
1209         TELEPHONY_LOGE("get Ad event is nullptr!");
1210         return isFileProcessResponse;
1211     }
1212     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1213     if (fd == nullptr) {
1214         TELEPHONY_LOGE("get Ad fd is nullptr!");
1215         return isFileProcessResponse;
1216     }
1217     std::string iccData = fd->resultData;
1218     bool doneData = true;
1219     char *rawData = const_cast<char *>(iccData.c_str());
1220     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
1221     if (fd->exception != nullptr) {
1222         doneData = false;
1223     }
1224     TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_AD: %{public}s", rawData);
1225     int dataSize = static_cast<int>(iccData.size());
1226     if (dataSize <= MNC_INDEX) {
1227         TELEPHONY_LOGI("SimFile MNC length dataSize = %{public}d", dataSize);
1228         doneData = false;
1229     }
1230     if (doneData) {
1231         lengthOfMnc_ = fileData[MNC_INDEX] & 0xf;
1232         TELEPHONY_LOGI("setting4 lengthOfMnc_= %{public}d", lengthOfMnc_);
1233     }
1234     if (doneData && (lengthOfMnc_ == 0xf)) {
1235         lengthOfMnc_ = UNKNOWN_MNC;
1236     } else if (doneData && (lengthOfMnc_ != MNC_LEN) && (lengthOfMnc_ != MCC_LEN)) {
1237         lengthOfMnc_ = UNINITIALIZED_MNC;
1238     }
1239     TELEPHONY_LOGI("update5 length Mnc_= %{public}d", lengthOfMnc_);
1240     CheckMncLengthForAdDone();
1241     return isFileProcessResponse;
1242 }
1243 
CheckMncLen(std::string imsi,int imsiSize,int mncLen,int mccmncLen,bool isCheckUninitMnc)1244 bool SimFile::CheckMncLen(std::string imsi, int imsiSize, int mncLen, int mccmncLen, bool isCheckUninitMnc)
1245 {
1246     if (isCheckUninitMnc) {
1247         return ((lengthOfMnc_ == UNINITIALIZED_MNC) || (lengthOfMnc_ == UNKNOWN_MNC) || (lengthOfMnc_ == mncLen)) &&
1248             ((!imsi.empty()) && (imsiSize >= mccmncLen));
1249     }
1250     return ((lengthOfMnc_ == UNKNOWN_MNC) || (lengthOfMnc_ == mncLen)) && ((!imsi.empty()) && (imsiSize >= mccmncLen));
1251 }
1252 
IsIndiaMcc(std::string mccCode)1253 bool SimFile::IsIndiaMcc(std::string mccCode)
1254 {
1255     if (indiaMcc_.size() == 0) {
1256         indiaMcc_ = {"404", "405"};
1257     }
1258     std::vector<std::string>::iterator obj = std::find(indiaMcc_.begin(), indiaMcc_.end(), mccCode);
1259     return (obj == indiaMcc_.end()) ? false : true;
1260 }
1261 
CheckMncLengthForAdDone()1262 void SimFile::CheckMncLengthForAdDone()
1263 {
1264     std::string imsi = ObtainIMSI();
1265     int imsiSize = static_cast<int>(imsi.size());
1266     if (CheckMncLen(imsi, imsiSize, MNC_LEN, MCCMNC_LEN, true)) {
1267         std::string mccMncCode = imsi.substr(0, MCCMNC_LEN);
1268         TELEPHONY_LOGI("SimFile mccMncCode= %{public}s", mccMncCode.c_str());
1269         if (MccPool::LengthIsThreeMnc(mccMncCode)) {
1270             lengthOfMnc_ = MNC_LONG_LEN;
1271             TELEPHONY_LOGI("SimFile AdDone update1 lengthOfMnc_= %{public}d", lengthOfMnc_);
1272         }
1273     }
1274 
1275     if (CheckMncLen(imsi, imsiSize, MNC_LONG_LEN, MCCMNC_SHORT_LEN, true)) {
1276         std::string mccCode = imsi.substr(0, MCC_LEN);
1277         if (IsIndiaMcc(mccCode)) {
1278             std::string mccMncCode = imsi.substr(0, MCCMNC_SHORT_LEN);
1279             if (MccPool::LengthIsTwoMnc(mccMncCode)) {
1280                 lengthOfMnc_ = MNC_LEN;
1281                 TELEPHONY_LOGI("SimFile AdDone update2 lengthOfMnc_= %{public}d", lengthOfMnc_);
1282             }
1283         }
1284     }
1285 
1286     if (lengthOfMnc_ == UNKNOWN_MNC || lengthOfMnc_ == UNINITIALIZED_MNC) {
1287         if (!imsi.empty()) {
1288             std::string mccCode = imsi.substr(0, MCC_LEN);
1289             if (IsIndiaMcc(mccCode)) {
1290                 lengthOfMnc_ = MNC_LONG_LEN;
1291                 TELEPHONY_LOGI("SimFile AdDone update3 lengthOfMnc_= %{public}d", lengthOfMnc_);
1292             } else {
1293                 int mcc = atoi(mccCode.c_str());
1294                 lengthOfMnc_ = MccPool::ShortestMncLengthFromMcc(mcc);
1295                 TELEPHONY_LOGI("SimFile AdDone update4 lengthOfMnc_= %{public}d", lengthOfMnc_);
1296             }
1297         } else {
1298             lengthOfMnc_ = UNKNOWN_MNC;
1299             TELEPHONY_LOGI(
1300                 "MNC length not present in ELEMENTARY_FILE_AD setting9 lengthOfMnc_= %{public}d", lengthOfMnc_);
1301         }
1302     }
1303     OnMccMncLoaded(imsi);
1304 }
1305 
CheckMncLengthForImsiDone()1306 void SimFile::CheckMncLengthForImsiDone()
1307 {
1308     std::string imsi = ObtainIMSI();
1309     int imsiSize = static_cast<int>(imsi.size());
1310     if (CheckMncLen(imsi, imsiSize, MNC_LEN, MCCMNC_LEN, false)) {
1311         std::string mccMncCode = imsi.substr(0, MCCMNC_LEN);
1312         TELEPHONY_LOGI("SimFile imsidone mccMncCode= %{public}s", mccMncCode.c_str());
1313         if (MccPool::LengthIsThreeMnc(mccMncCode)) {
1314             lengthOfMnc_ = MNC_LONG_LEN;
1315             TELEPHONY_LOGI("SimFile imsidone update1 lengthOfMnc_= %{public}d", lengthOfMnc_);
1316         }
1317     }
1318 
1319     if (CheckMncLen(imsi, imsiSize, MNC_LONG_LEN, MCCMNC_SHORT_LEN, false)) {
1320         std::string mccCode = imsi.substr(0, MCC_LEN);
1321         if (IsIndiaMcc(mccCode)) {
1322             std::string mccMncCode = imsi.substr(0, MCCMNC_SHORT_LEN);
1323             if (MccPool::LengthIsTwoMnc(mccMncCode)) {
1324                 lengthOfMnc_ = MNC_LEN;
1325                 TELEPHONY_LOGI("SimFile imsidone update2 lengthOfMnc_= %{public}d", lengthOfMnc_);
1326             }
1327         }
1328     }
1329 
1330     if (lengthOfMnc_ == UNKNOWN_MNC) {
1331         if (!imsi.empty()) {
1332             std::string mccCode = imsi.substr(0, MCC_LEN);
1333             if (IsIndiaMcc(mccCode)) {
1334                 lengthOfMnc_ = MNC_LONG_LEN;
1335                 TELEPHONY_LOGI("SimFile imsidone update3 lengthOfMnc_= %{public}d", lengthOfMnc_);
1336             } else {
1337                 int mcc = atoi(mccCode.c_str());
1338                 lengthOfMnc_ = MccPool::ShortestMncLengthFromMcc(mcc);
1339                 TELEPHONY_LOGI("SimFile imsidone update4 lengthOfMnc_= %{public}d", lengthOfMnc_);
1340             }
1341         } else {
1342             lengthOfMnc_ = UNKNOWN_MNC;
1343             TELEPHONY_LOGI(
1344                 "MNC length not present in ELEMENTARY_FILE_AD setting9 lengthOfMnc_= %{public}d", lengthOfMnc_);
1345         }
1346     }
1347     OnMccMncLoaded(imsi);
1348 }
1349 
OnMccMncLoaded(std::string imsi)1350 void SimFile::OnMccMncLoaded(std::string imsi)
1351 {
1352     int lenNum = MCC_LEN + lengthOfMnc_;
1353     int sz = static_cast<int>(imsi.size());
1354     bool cond = sz >= lenNum;
1355     if ((!imsi.empty()) && (lengthOfMnc_ != UNKNOWN_MNC && lengthOfMnc_ != UNINITIALIZED_MNC) && cond) {
1356         operatorNumeric_ = imsi.substr(0, lenNum);
1357         TELEPHONY_LOGI("SimFile OnMccMncLoaded MCCMNC:%{public}s", operatorNumeric_.c_str());
1358         FileChangeToExt(operatorNumeric_, FileChangeType::G_MCCMNC_FILE_LOAD);
1359         std::string mcc = imsi.substr(0, MCC_LEN);
1360         std::string mnc = imsi.substr(MCC_LEN, lengthOfMnc_);
1361         AppExecFwk::Configuration configuration;
1362         configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC, mcc);
1363         configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC, mnc);
1364         auto appMgrClient = std::make_unique<AppExecFwk::AppMgrClient>();
1365         appMgrClient->UpdateConfiguration(configuration);
1366     }
1367 }
1368 
ProcessSmsOnSim(const AppExecFwk::InnerEvent::Pointer & event)1369 bool SimFile::ProcessSmsOnSim(const AppExecFwk::InnerEvent::Pointer &event)
1370 {
1371     bool isFileProcessResponse = false;
1372     if (event == nullptr) {
1373         TELEPHONY_LOGE("process sms event is nullptr!");
1374         return isFileProcessResponse;
1375     }
1376     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1377     if (fd == nullptr) {
1378         TELEPHONY_LOGE("process sms fd is nullptr!");
1379         return isFileProcessResponse;
1380     }
1381     std::string iccData = fd->resultData;
1382     int index = atoi(iccData.c_str());
1383     if (fd->exception != nullptr || index == INVALID_VALUE) {
1384         TELEPHONY_LOGE("exception on SMS_ON_SIM with index: %{public}d", index);
1385     } else {
1386         TELEPHONY_LOGI("READ ELEMENTARY_FILE_SMS RECORD index= %{public}d", index);
1387         AppExecFwk::InnerEvent::Pointer eventSMS = BuildCallerInfo(MSG_SIM_OBTAIN_SMS_DONE);
1388         fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_SMS, index, eventSMS);
1389     }
1390     return isFileProcessResponse;
1391 }
1392 
ProcessGetAllSmsDone(const AppExecFwk::InnerEvent::Pointer & event)1393 bool SimFile::ProcessGetAllSmsDone(const AppExecFwk::InnerEvent::Pointer &event)
1394 {
1395     bool isFileProcessResponse = true;
1396     if (event == nullptr) {
1397         TELEPHONY_LOGE("get all sms event is nullptr!");
1398         return isFileProcessResponse;
1399     }
1400     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1401     if (fd == nullptr) {
1402         TELEPHONY_LOGE("get all sms fd is nullptr!");
1403         return isFileProcessResponse;
1404     }
1405     std::string iccData = fd->resultData;
1406     if (fd->exception != nullptr) {
1407         return isFileProcessResponse;
1408     }
1409     ProcessSmses(iccData);
1410     return isFileProcessResponse;
1411 }
1412 
ProcessGetSmsDone(const AppExecFwk::InnerEvent::Pointer & event)1413 bool SimFile::ProcessGetSmsDone(const AppExecFwk::InnerEvent::Pointer &event)
1414 {
1415     bool isFileProcessResponse = false;
1416     if (event == nullptr) {
1417         TELEPHONY_LOGE("get sms event is nullptr!");
1418         return isFileProcessResponse;
1419     }
1420     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1421     if (fd == nullptr) {
1422         TELEPHONY_LOGE("get sms fd is nullptr!");
1423         return isFileProcessResponse;
1424     }
1425     std::string iccData = fd->resultData;
1426     if (fd->exception == nullptr) {
1427         ProcessSms(iccData);
1428     } else {
1429         TELEPHONY_LOGI("SimFile exception on GET_SMS ");
1430     }
1431     return isFileProcessResponse;
1432 }
1433 
ProcessGetPlmnActDone(const AppExecFwk::InnerEvent::Pointer & event)1434 bool SimFile::ProcessGetPlmnActDone(const AppExecFwk::InnerEvent::Pointer &event)
1435 {
1436     bool isFileProcessResponse = true;
1437     if (event == nullptr) {
1438         TELEPHONY_LOGE("get Plmn event is nullptr!");
1439         return isFileProcessResponse;
1440     }
1441     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1442     if (fd == nullptr) {
1443         TELEPHONY_LOGE("get Plmn fd is nullptr!");
1444         return isFileProcessResponse;
1445     }
1446     std::string iccData = fd->resultData;
1447     char *rawData = const_cast<char *>(iccData.c_str());
1448     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
1449 
1450     if (fd->exception != nullptr || iccData.empty()) {
1451         TELEPHONY_LOGE("Failed fetch User PLMN ");
1452     } else {
1453         TELEPHONY_LOGI("fetch a PlmnRAT, iccData= %{public}s", fileData);
1454     }
1455     return isFileProcessResponse;
1456 }
1457 
1458 // Process operator plmn
ProcessGetOplmnActDone(const AppExecFwk::InnerEvent::Pointer & event)1459 bool SimFile::ProcessGetOplmnActDone(const AppExecFwk::InnerEvent::Pointer &event)
1460 {
1461     bool isFileProcessResponse = true;
1462     if (event == nullptr) {
1463         TELEPHONY_LOGE("operator plmn event is nullptr!");
1464         return isFileProcessResponse;
1465     }
1466     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1467     if (fd == nullptr) {
1468         TELEPHONY_LOGE("operator plmn fd is nullptr!");
1469         return isFileProcessResponse;
1470     }
1471     std::string iccData = fd->resultData;
1472     char *rawData = const_cast<char *>(iccData.c_str());
1473     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
1474 
1475     if (fd->exception != nullptr || iccData.empty()) {
1476         TELEPHONY_LOGE("Failed fetch Operator PLMN");
1477     } else {
1478         TELEPHONY_LOGI("fetch a OPlmnRAT, iccData= %{public}s", fileData);
1479     }
1480     return isFileProcessResponse;
1481 }
1482 
ProcessGetCspCphs(const AppExecFwk::InnerEvent::Pointer & event)1483 bool SimFile::ProcessGetCspCphs(const AppExecFwk::InnerEvent::Pointer &event)
1484 {
1485     bool isFileProcessResponse = true;
1486     if (event == nullptr) {
1487         TELEPHONY_LOGE("get Csp Cphs event is nullptr!");
1488         return isFileProcessResponse;
1489     }
1490     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1491     if (fd == nullptr) {
1492         TELEPHONY_LOGE("get Csp Cphs fd is nullptr!");
1493         return isFileProcessResponse;
1494     }
1495     std::string iccData = fd->resultData;
1496     char *rawData = const_cast<char *>(iccData.c_str());
1497     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
1498 
1499     if (fd->exception != nullptr) {
1500         TELEPHONY_LOGE("Exception to get ELEMENTARY_FILE_CSP data ");
1501         return isFileProcessResponse;
1502     }
1503     TELEPHONY_LOGI("SimFile MSG_SIM_OBTAIN_CSP_CPHS_DONE data:%{public}s", fileData);
1504     TELEPHONY_LOGI("ELEMENTARY_FILE_CSP: %{public}s", fileData);
1505     ProcessElementaryFileCsp(iccData);
1506     return isFileProcessResponse;
1507 }
1508 
ProcessGetInfoCphs(const AppExecFwk::InnerEvent::Pointer & event)1509 bool SimFile::ProcessGetInfoCphs(const AppExecFwk::InnerEvent::Pointer &event)
1510 {
1511     bool isFileProcessResponse = true;
1512     if (event == nullptr) {
1513         TELEPHONY_LOGE("get Cphs event is nullptr!");
1514         return isFileProcessResponse;
1515     }
1516     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1517     if (fd == nullptr) {
1518         TELEPHONY_LOGE("get Cphs fd is nullptr!");
1519         return isFileProcessResponse;
1520     }
1521     if (fd->exception != nullptr) {
1522         return isFileProcessResponse;
1523     }
1524     cphsInfo_ = fd->resultData;
1525     TELEPHONY_LOGI("SimFile::ProcessGetInfoCphs success");
1526     return isFileProcessResponse;
1527 }
1528 
ProcessGetSstDone(const AppExecFwk::InnerEvent::Pointer & event)1529 bool SimFile::ProcessGetSstDone(const AppExecFwk::InnerEvent::Pointer &event)
1530 {
1531     bool isFileProcessResponse = true;
1532     std::string tempStr;
1533     if (event == nullptr) {
1534         TELEPHONY_LOGE("get Sst event is nullptr!");
1535         return isFileProcessResponse;
1536     }
1537     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1538     if (fd == nullptr) {
1539         TELEPHONY_LOGE("get Sst fd is nullptr!");
1540         return isFileProcessResponse;
1541     }
1542     std::string iccData = fd->resultData;
1543 
1544     if (fd->exception != nullptr) {
1545         FileChangeToExt(tempStr, FileChangeType::SPN_FILE_LOAD);
1546         return isFileProcessResponse;
1547     }
1548     if (iccData.empty()) {
1549         TELEPHONY_LOGE("sst data is nullptr");
1550         return false;
1551     }
1552     serviceTable_ = iccData;
1553     if (IsServiceAvailable(UsimService::USIM_SPN)) {
1554         AppExecFwk::InnerEvent::Pointer eventSpn = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
1555         ObtainSpnPhase(true, eventSpn);
1556     } else {
1557         FileChangeToExt(tempStr, FileChangeType::SPN_FILE_LOAD);
1558     }
1559     TELEPHONY_LOGI("SimFile MSG_SIM_OBTAIN_SST_DONE data:%{public}s", serviceTable_.c_str());
1560     LoadSimOtherFile();
1561     return isFileProcessResponse;
1562 }
1563 
IsServiceAvailable(UsimService service)1564 bool SimFile::IsServiceAvailable(UsimService service)
1565 {
1566     if (serviceTable_.empty()) {
1567         TELEPHONY_LOGE("serviceTable_ is unavailable");
1568         return false;
1569     }
1570     CardType cardType = CardType::UNKNOWN_CARD;
1571     if (stateManager_ != nullptr) {
1572         cardType = stateManager_->GetCardType();
1573     } else {
1574         TELEPHONY_LOGE("stateManager_ is unavailable");
1575         return false;
1576     }
1577     if (cardType == CardType::SINGLE_MODE_SIM_CARD) {
1578         return true;
1579     }
1580     if (service < UsimService::USIM_PHONEBOOK || service > UsimService::USIM_FOR_5G_SECURITY_PARAMETERS_EXTENDED) {
1581         TELEPHONY_LOGE("service %{public}d is unavailable", service);
1582         return false;
1583     }
1584     uint32_t offset = static_cast<uint32_t>(service) / BYTE_TO_BIT_LEN;
1585     uint32_t mask = static_cast<uint32_t>(service) % BYTE_TO_BIT_LEN;
1586     if (mask == 0 && offset != 0) {
1587         offset--;
1588         mask = 7; // 7 means max index in one byte
1589     } else {
1590         mask--;
1591     }
1592     if (offset * HEX_TO_BYTE_LEN >= serviceTable_.length()) {
1593         TELEPHONY_LOGE("offset exceeds serviceTable_ length");
1594         return false;
1595     }
1596     std::istringstream istr(serviceTable_.substr(offset * HEX_TO_BYTE_LEN, HEX_TO_BYTE_LEN));
1597     uint32_t num = 0;
1598     istr >> std::hex >> num;
1599     return (num & (1 << mask)) != 0;
1600 }
1601 
ProcessGetPnnDone(const AppExecFwk::InnerEvent::Pointer & event)1602 bool SimFile::ProcessGetPnnDone(const AppExecFwk::InnerEvent::Pointer &event)
1603 {
1604     TELEPHONY_LOGI("ProcessGetPnnDone: start");
1605     bool isFileProcessResponse = true;
1606     if (event == nullptr) {
1607         TELEPHONY_LOGE("event is nullptr!");
1608         return isFileProcessResponse;
1609     }
1610     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1611     if (fd != nullptr) {
1612         if (fd->exception != nullptr) {
1613             TELEPHONY_LOGE("ProcessGetPnnDone: get error result");
1614             return isFileProcessResponse;
1615         }
1616     } else {
1617         std::shared_ptr<MultiRecordResult> object = event->GetSharedObject<MultiRecordResult>();
1618         if (object != nullptr) {
1619             TELEPHONY_LOGI("ProcessGetPnnDone: %{public}d", object->resultLength);
1620             if (object->exception == nullptr) {
1621                 ParsePnn(object->fileResults);
1622             }
1623             for (std::string str : object->fileResults) {
1624                 TELEPHONY_LOGI("ProcessGetPnnDone: %{public}s", str.c_str());
1625             }
1626         } else {
1627             TELEPHONY_LOGE("ProcessGetPnnDone: get null pointer!!!");
1628         }
1629     }
1630     return isFileProcessResponse;
1631 }
1632 
ProcessGetOplDone(const AppExecFwk::InnerEvent::Pointer & event)1633 bool SimFile::ProcessGetOplDone(const AppExecFwk::InnerEvent::Pointer &event)
1634 {
1635     TELEPHONY_LOGI("ProcessGetOplDone: start");
1636     bool isFileProcessResponse = true;
1637     if (event == nullptr) {
1638         TELEPHONY_LOGE("ProcessGetOplDone event is nullptr!");
1639         return isFileProcessResponse;
1640     }
1641     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1642     if (fd != nullptr) {
1643         if (fd->exception != nullptr) {
1644             TELEPHONY_LOGE("ProcessGetOplDone: get error result");
1645             return isFileProcessResponse;
1646         }
1647     } else {
1648         std::shared_ptr<MultiRecordResult> object = event->GetSharedObject<MultiRecordResult>();
1649         if (object != nullptr) {
1650             TELEPHONY_LOGI("ProcessGetOplDone: %{public}d", object->resultLength);
1651             if (object->exception == nullptr) {
1652                 ParseOpl(object->fileResults);
1653             }
1654         } else {
1655             TELEPHONY_LOGE("ProcessGetOplDone: get null pointer!!!");
1656         }
1657     }
1658     return isFileProcessResponse;
1659 }
1660 
ProcessGetOpl5gDone(const AppExecFwk::InnerEvent::Pointer & event)1661 bool SimFile::ProcessGetOpl5gDone(const AppExecFwk::InnerEvent::Pointer &event)
1662 {
1663     TELEPHONY_LOGD("ProcessGetOpl5gDone: start");
1664     bool isFileProcessResponse = true;
1665     if (event == nullptr) {
1666         TELEPHONY_LOGE("ProcessGetOpl5gDone: event is nullptr!");
1667         return isFileProcessResponse;
1668     }
1669     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1670     if (fd != nullptr) {
1671         if (fd->exception != nullptr) {
1672             TELEPHONY_LOGE("ProcessGetOpl5gDone: get error result");
1673         }
1674         return isFileProcessResponse;
1675     }
1676     std::shared_ptr<MultiRecordResult> object = event->GetSharedObject<MultiRecordResult>();
1677     if (object == nullptr) {
1678         TELEPHONY_LOGE("ProcessGetOpl5gDone: get null pointer!!!");
1679         return isFileProcessResponse;
1680     }
1681     if (object->exception == nullptr) {
1682         ParseOpl5g(object->fileResults);
1683     }
1684     return isFileProcessResponse;
1685 }
1686 
ProcessUpdateDone(const AppExecFwk::InnerEvent::Pointer & event)1687 bool SimFile::ProcessUpdateDone(const AppExecFwk::InnerEvent::Pointer &event)
1688 {
1689     bool isFileProcessResponse = false;
1690     if (event == nullptr) {
1691         TELEPHONY_LOGE("update event is nullptr!");
1692         return isFileProcessResponse;
1693     }
1694     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1695     if (fd == nullptr) {
1696         TELEPHONY_LOGE("update fd is nullptr!");
1697         return isFileProcessResponse;
1698     }
1699     std::string iccData = fd->resultData;
1700     if (fd->exception != nullptr) {
1701         TELEPHONY_LOGE("SimFile failed to update");
1702     }
1703     return isFileProcessResponse;
1704 }
1705 
ProcessSetCphsMailbox(const AppExecFwk::InnerEvent::Pointer & event)1706 bool SimFile::ProcessSetCphsMailbox(const AppExecFwk::InnerEvent::Pointer &event)
1707 {
1708     bool isFileProcessResponse = true;
1709     if (event == nullptr) {
1710         TELEPHONY_LOGE("event is nullptr!");
1711         return isFileProcessResponse;
1712     }
1713     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
1714     if (fd == nullptr) {
1715         TELEPHONY_LOGE("fd is nullptr!");
1716         return isFileProcessResponse;
1717     }
1718     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
1719     if (fd->exception == nullptr) {
1720         std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
1721         voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
1722         voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
1723         waitResult_ = true;
1724         processWait_.notify_all();
1725         TELEPHONY_LOGI("set cphs voicemail success");
1726     } else {
1727         processWait_.notify_all();
1728         TELEPHONY_LOGE("set cphs voicemail failed with exception!!");
1729     }
1730     return isFileProcessResponse;
1731 }
1732 
1733 // Process forbidden PLMNs
ProcessGetHplmActDone(const AppExecFwk::InnerEvent::Pointer & event)1734 bool SimFile::ProcessGetHplmActDone(const AppExecFwk::InnerEvent::Pointer &event)
1735 {
1736     bool isFileProcessResponse = true;
1737     if (event == nullptr) {
1738         TELEPHONY_LOGE("Process forbidden PLMNs event is nullptr!");
1739         return isFileProcessResponse;
1740     }
1741     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1742     if (fd == nullptr) {
1743         TELEPHONY_LOGE("Process forbidden PLMNs fd is nullptr!");
1744         return isFileProcessResponse;
1745     }
1746     std::string iccData = fd->resultData;
1747     char *rawData = const_cast<char *>(iccData.c_str());
1748     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
1749 
1750     if (fd->exception != nullptr || iccData.empty()) {
1751         TELEPHONY_LOGE("Failed to fetch forbidden PLMN");
1752         return isFileProcessResponse;
1753     } else {
1754         TELEPHONY_LOGI("fetch a FPlmnRAT, iccData=%{public}s", fileData);
1755     }
1756     return isFileProcessResponse;
1757 }
1758 
1759 // Process Equivalent Home PLMNs
ProcessGetEhplmnDone(const AppExecFwk::InnerEvent::Pointer & event)1760 bool SimFile::ProcessGetEhplmnDone(const AppExecFwk::InnerEvent::Pointer &event)
1761 {
1762     bool isFileProcessResponse = true;
1763     if (event == nullptr) {
1764         TELEPHONY_LOGE("Process Equivalent Home PLMNs event is nullptr!");
1765         return isFileProcessResponse;
1766     }
1767     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1768     if (fd == nullptr) {
1769         TELEPHONY_LOGE("Process Equivalent Home PLMNs fd is nullptr!");
1770         return isFileProcessResponse;
1771     }
1772     std::string iccData = fd->resultData;
1773     if (fd->exception != nullptr || iccData.empty()) {
1774         TELEPHONY_LOGE("Failed fetch Equivalent Home PLMNs");
1775         return isFileProcessResponse;
1776     }
1777     return isFileProcessResponse;
1778 }
1779 
1780 // Process forbidden PLMNs
ProcessGetFplmnDone(const AppExecFwk::InnerEvent::Pointer & event)1781 bool SimFile::ProcessGetFplmnDone(const AppExecFwk::InnerEvent::Pointer &event)
1782 {
1783     bool loadResponse = true;
1784     if (event == nullptr) {
1785         TELEPHONY_LOGE("Process forbidden PLMNs event is nullptr!");
1786         return loadResponse;
1787     }
1788     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
1789     if (fd == nullptr) {
1790         TELEPHONY_LOGE("Process forbidden PLMNs fd is nullptr!");
1791         return loadResponse;
1792     }
1793     std::string iccData = fd->resultData;
1794     if (fd->exception != nullptr || iccData.empty()) {
1795         TELEPHONY_LOGE("Failed to get forbidden PLMNs");
1796         return loadResponse;
1797     }
1798     if (fd->arg1 == ICC_CONTROLLER_REQ_SEND_RESPONSE) {
1799         TELEPHONY_LOGI("getForbiddenPlmns and send result");
1800         loadResponse = false;
1801     }
1802     return loadResponse;
1803 }
1804 
ProcessSetMbdn(const AppExecFwk::InnerEvent::Pointer & event)1805 bool SimFile::ProcessSetMbdn(const AppExecFwk::InnerEvent::Pointer &event)
1806 {
1807     bool isFileProcessResponse = true;
1808     if (event == nullptr) {
1809         TELEPHONY_LOGE("event is nullptr!");
1810         return isFileProcessResponse;
1811     }
1812     bool hasNotify = false;
1813     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
1814     if (fd == nullptr) {
1815         TELEPHONY_LOGE("fd is nullptr!");
1816         return isFileProcessResponse;
1817     }
1818     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
1819     if (fd->exception == nullptr) {
1820         std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
1821         voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
1822         voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
1823         waitResult_ = true;
1824         processWait_.notify_all();
1825         hasNotify = true;
1826         TELEPHONY_LOGI("set voicemail name success");
1827     }
1828 
1829     if (CphsVoiceMailAvailable()) {
1830         std::shared_ptr<DiallingNumbersInfo> diallingNumberCphs = std::make_shared<DiallingNumbersInfo>();
1831         std::shared_lock<std::shared_mutex> lock(voiceMailMutex_);
1832         diallingNumberCphs->name_ = Str8ToStr16(voiceMailNum_);
1833         diallingNumberCphs->number_ = Str8ToStr16(voiceMailTag_);
1834         AppExecFwk::InnerEvent::Pointer eventCphs =
1835             CreateDiallingNumberPointer(MSG_SIM_SET_CPHS_MAILBOX_DONE, 0, 0, nullptr);
1836         DiallingNumberUpdateInfor infor;
1837         infor.diallingNumber = diallingNumberCphs;
1838         infor.fileId = ELEMENTARY_FILE_MAILBOX_CPHS;
1839         infor.extFile = ELEMENTARY_FILE_EXT1;
1840         infor.index = 1;
1841         diallingNumberHandler_->UpdateDiallingNumbers(infor, eventCphs);
1842         TELEPHONY_LOGI("set cphs voicemail number as it is available");
1843     } else {
1844         if (!hasNotify) {
1845             processWait_.notify_all();
1846         }
1847         TELEPHONY_LOGI("set voicemail number finished");
1848     }
1849     return isFileProcessResponse;
1850 }
1851 
ProcessMarkSms(const AppExecFwk::InnerEvent::Pointer & event)1852 bool SimFile::ProcessMarkSms(const AppExecFwk::InnerEvent::Pointer &event)
1853 {
1854     (void)event;
1855     return false;
1856 }
1857 
ProcessObtainSpnPhase(const AppExecFwk::InnerEvent::Pointer & event)1858 bool SimFile::ProcessObtainSpnPhase(const AppExecFwk::InnerEvent::Pointer &event)
1859 {
1860     bool loadResponse = true;
1861     if (event == nullptr) {
1862         TELEPHONY_LOGE("event is nullptr!");
1863         return loadResponse;
1864     }
1865     ObtainSpnPhase(false, event);
1866     return loadResponse;
1867 }
1868 
IsContinueGetSpn(bool start,SpnStatus curStatus,SpnStatus & newStatus)1869 bool SimFile::IsContinueGetSpn(bool start, SpnStatus curStatus, SpnStatus &newStatus)
1870 {
1871     if (start) {
1872         switch (curStatus) {
1873             case OBTAIN_SPN_GENERAL:
1874             case OBTAIN_OPERATOR_NAMESTRING:
1875             case OBTAIN_OPERATOR_NAME_SHORTFORM:
1876             case OBTAIN_SPN_START:
1877                 newStatus = SpnStatus::OBTAIN_SPN_START;
1878                 return false;
1879             default:
1880                 newStatus = SpnStatus::OBTAIN_SPN_START;
1881                 return true;
1882         }
1883     } else {
1884         return true;
1885     }
1886 }
1887 
ObtainSpnCondition(bool roaming,const std::string & operatorNum)1888 int SimFile::ObtainSpnCondition(bool roaming, const std::string &operatorNum)
1889 {
1890     unsigned int cond = 0;
1891     if (displayConditionOfSpn_ <= SPN_INVALID) {
1892         return cond;
1893     }
1894     if (roaming) {
1895         cond = SPN_CONDITION_DISPLAY_PLMN;
1896         if ((static_cast<unsigned int>(displayConditionOfSpn_) & static_cast<unsigned int>(SPN_COND)) == 0) {
1897             cond |= static_cast<unsigned int>(SPN_CONDITION_DISPLAY_SPN);
1898         }
1899     } else {
1900         cond = SPN_CONDITION_DISPLAY_SPN;
1901         if ((static_cast<unsigned int>(displayConditionOfSpn_) & static_cast<unsigned int>(SPN_COND_PLMN)) ==
1902             SPN_COND_PLMN) {
1903             cond |= static_cast<unsigned int>(SPN_CONDITION_DISPLAY_PLMN);
1904         }
1905     }
1906     TELEPHONY_LOGI("displayConditionOfSpn_:%{public}d, roaming:%{public}d, cond:%{public}d",
1907         displayConditionOfSpn_, roaming, cond);
1908     return cond;
1909 }
1910 
ObtainExtensionElementaryFile(int ef)1911 int SimFile::ObtainExtensionElementaryFile(int ef)
1912 {
1913     int ext = 0;
1914     if (ef == ELEMENTARY_FILE_MSISDN) {
1915         ext = ELEMENTARY_FILE_EXT5; // ELEMENTARY_FILE_EXT1
1916     } else {
1917         ext = ELEMENTARY_FILE_EXT1;
1918     }
1919     return ext;
1920 }
1921 
GetVoiceMailNumber()1922 std::string SimFile::GetVoiceMailNumber()
1923 {
1924     std::shared_lock<std::shared_mutex> lock(voiceMailMutex_);
1925     return voiceMailNum_;
1926 }
1927 
SetVoiceMailNumber(const std::string mailNumber)1928 void SimFile::SetVoiceMailNumber(const std::string mailNumber)
1929 {
1930     std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
1931     voiceMailNum_ = mailNumber;
1932 }
1933 
VoiceMailNotEditToSim()1934 bool SimFile::VoiceMailNotEditToSim()
1935 {
1936     OperatorConfig operatorConfig;
1937     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, operatorConfig);
1938     bool custVmNotToSim = false;
1939     std::map<std::string, bool>::iterator it = operatorConfig.boolValue.find(KEY_VOICE_MAIL_EDIT_NOT_TO_SIM_BOOL);
1940     if (it != operatorConfig.boolValue.end()) {
1941         custVmNotToSim = it->second;
1942     }
1943     return custVmNotToSim;
1944 }
1945 
UpdateVoiceMail(const std::string & mailName,const std::string & mailNumber)1946 bool SimFile::UpdateVoiceMail(const std::string &mailName, const std::string &mailNumber)
1947 {
1948     if (VoiceMailNotEditToSim()) {
1949         TELEPHONY_LOGI("SimFile::UpdateVoiceMail, no need to edit voice mail info to sim");
1950         return true;
1951     }
1952     waitResult_ = false;
1953     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::make_shared<DiallingNumbersInfo>();
1954     diallingNumber->name_ = Str8ToStr16(mailName);
1955     diallingNumber->number_ = Str8ToStr16(mailNumber);
1956 
1957     if ((indexOfMailbox_) && (indexOfMailbox_ != BYTE_NUM)) {
1958         std::unique_lock<std::mutex> lock(IccFile::mtx_);
1959         TELEPHONY_LOGI("UpdateVoiceMail start MBDN");
1960         AppExecFwk::InnerEvent::Pointer event = CreateDiallingNumberPointer(MSG_SIM_SET_MBDN_DONE, 0, 0, nullptr);
1961         DiallingNumberUpdateInfor infor;
1962         infor.diallingNumber = diallingNumber;
1963         infor.fileId = ELEMENTARY_FILE_MBDN;
1964         infor.extFile = ELEMENTARY_FILE_EXT6;
1965         infor.index = indexOfMailbox_;
1966         diallingNumberHandler_->UpdateDiallingNumbers(infor, event);
1967         while (!waitResult_) {
1968             TELEPHONY_LOGI("update voicemail wait, response = false");
1969             if (processWait_.wait_for(lock, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
1970                 break;
1971             }
1972         }
1973     } else if (CphsVoiceMailAvailable()) {
1974         std::unique_lock<std::mutex> lock(IccFile::mtx_);
1975         AppExecFwk::InnerEvent::Pointer event =
1976             CreateDiallingNumberPointer(MSG_SIM_SET_CPHS_MAILBOX_DONE, 0, 0, nullptr);
1977         DiallingNumberUpdateInfor infor;
1978         infor.diallingNumber = diallingNumber;
1979         infor.fileId = ELEMENTARY_FILE_MAILBOX_CPHS;
1980         infor.extFile = ELEMENTARY_FILE_EXT1;
1981         infor.index = 1;
1982         diallingNumberHandler_->UpdateDiallingNumbers(infor, event);
1983         while (!waitResult_) {
1984             TELEPHONY_LOGI("update voicemail wait, response = false");
1985             if (processWait_.wait_for(lock, std::chrono::seconds(WAIT_TIME_SECOND)) == std::cv_status::timeout) {
1986                 break;
1987             }
1988         }
1989     } else {
1990         TELEPHONY_LOGE("UpdateVoiceMail indexOfMailbox_ %{public}d is invalid!!", indexOfMailbox_);
1991     }
1992     TELEPHONY_LOGI("UpdateVoiceMail finished %{public}d", waitResult_);
1993     return waitResult_;
1994 }
1995 
SetVoiceMailCount(int32_t voiceMailCount)1996 bool SimFile::SetVoiceMailCount(int32_t voiceMailCount)
1997 {
1998     bool setDone = false;
1999     AppExecFwk::InnerEvent::Pointer eventUpdate = BuildCallerInfo(MSG_SIM_UPDATE_DONE);
2000     std::shared_ptr<unsigned char> efMWISData = SIMUtils::HexStringConvertToBytes(efMWISStr_, efMWISSize_);
2001     efMWIS_ = efMWISData != nullptr ? efMWISData.get() : nullptr;
2002     if (efMWIS_ != nullptr && efMWISSize_ > 1) {
2003         // TS 51.011 10.3.45
2004         efMWIS_[0] = static_cast<unsigned char>((efMWIS_[0] & BYTE_NUM6) | (voiceMailCount == 0 ? 0 : BYTE_NUM2));
2005         efMWIS_[1] = voiceMailCount < 0 ? 0 : static_cast<unsigned char>(voiceMailCount);
2006         fileController_->UpdateLinearFixedFile(ELEMENTARY_FILE_MWIS, 1, efMWISStr_, efMWISSize_, "", eventUpdate);
2007         setDone = true;
2008     }
2009     std::shared_ptr<unsigned char> efCphsMwiData = SIMUtils::HexStringConvertToBytes(efCphsMwisStr_, efCphsMwiSize_);
2010     efCphsMwi_ = efCphsMwiData != nullptr ? efCphsMwiData.get() : nullptr;
2011     if (efCphsMwi_ != nullptr && efCphsMwiSize_ > 1) {
2012         efCphsMwi_[0] =
2013             static_cast<unsigned char>((efCphsMwi_[0] & BYTE_NUM7) | (voiceMailCount == 0 ? BYTE_NUM5 : BYTE_NUM4));
2014         fileController_->UpdateBinaryFile(
2015             ELEMENTARY_FILE_VOICE_MAIL_INDICATOR_CPHS, efCphsMwisStr_, efCphsMwiSize_, eventUpdate);
2016         setDone = true;
2017     }
2018     if (setDone) {
2019         voiceMailCount_ = voiceMailCount;
2020         DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateVoiceMailMsgIndicator(
2021             slotId_, voiceMailCount_ > 0);
2022         return true;
2023     }
2024     TELEPHONY_LOGE("SetVoiceMailCount efMWIS_ and efCphsMwi_ is nullptr");
2025     return false;
2026 }
2027 
SetVoiceCallForwarding(bool enable,const std::string & number)2028 bool SimFile::SetVoiceCallForwarding(bool enable, const std::string &number)
2029 {
2030     bool setDone = false;
2031     AppExecFwk::InnerEvent::Pointer eventUpdate = BuildCallerInfo(MSG_SIM_UPDATE_DONE);
2032     std::shared_ptr<unsigned char> efCfisData = SIMUtils::HexStringConvertToBytes(efCfisStr_, efCfisSize_);
2033     efCfis_ = efCfisData != nullptr ? efCfisData.get() : nullptr;
2034     if (EfCfisAvailable(efCfisSize_)) {
2035         if (enable) {
2036             efCfis_[1] |= BYTE_NUM2;
2037         } else {
2038             efCfis_[1] &= BYTE_NUM6;
2039         }
2040         // Spec reference for EF_CFIS contents, TS 51.011 section 10.3.46.
2041         if (enable && !number.empty()) {
2042             if (!FillNumber(efCfisData, efCfisSize_, number)) {
2043                 TELEPHONY_LOGE("fill number failed");
2044                 return false;
2045             }
2046         }
2047         fileController_->UpdateLinearFixedFile(ELEMENTARY_FILE_CFIS, 1, efCfisStr_, efCfisSize_, "", eventUpdate);
2048         setDone = true;
2049     }
2050     std::shared_ptr<unsigned char> efCffData = SIMUtils::HexStringConvertToBytes(efCffStr_, efCffSize_);
2051     efCff_ = efCffData != nullptr ? efCffData.get() : nullptr;
2052     if (efCff_ != nullptr && efCffSize_ > 0) {
2053         if (enable) {
2054             efCff_[0] = static_cast<unsigned char>((efCff_[0] & BYTE_NUM7) | BYTE_NUM4);
2055         } else {
2056             efCff_[0] = static_cast<unsigned char>((efCff_[0] & BYTE_NUM7) | BYTE_NUM5);
2057         }
2058         fileController_->UpdateBinaryFile(ELEMENTARY_FILE_CFF_CPHS, efCffStr_, efCffSize_, eventUpdate);
2059         setDone = true;
2060     }
2061     if (setDone) {
2062         callForwardingStatus = enable ? CALL_FORWARDING_STATUS_ENABLED : CALL_FORWARDING_STATUS_DISABLED;
2063         DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateCfuIndicator(
2064             slotId_, callForwardingStatus == CALL_FORWARDING_STATUS_ENABLED);
2065         return true;
2066     }
2067     TELEPHONY_LOGE("SetVoiceCallForwarding efCfis_ and efCff_ is nullptr");
2068     return false;
2069 }
2070 
FillNumber(std::shared_ptr<unsigned char> efCfisData,int32_t efCfisSize,const std::string & number)2071 bool SimFile::FillNumber(std::shared_ptr<unsigned char> efCfisData, int32_t efCfisSize, const std::string &number)
2072 {
2073     std::vector<uint8_t> bcdCodes;
2074     SimNumberDecode::NumberConvertToBCD(number, bcdCodes, false, SimNumberDecode::BCD_TYPE_ADN);
2075     unsigned int dataLength = bcdCodes.size();
2076     unsigned char numberData[dataLength];
2077     for (unsigned int i = 0; i < dataLength; ++i) {
2078         numberData[i] = bcdCodes.at(i);
2079     }
2080     if (CFIS_TON_NPI_OFFSET + static_cast<int32_t>(dataLength) >= efCfisSize) {
2081         TELEPHONY_LOGE("data is invalid");
2082         return false;
2083     }
2084     auto efCfis = efCfisData.get();
2085     SIMUtils::ArrayCopy(numberData, 0, efCfis, CFIS_TON_NPI_OFFSET, dataLength);
2086     if (CFIS_ADN_EXTENSION_ID_OFFSET >= efCfisSize) {
2087         TELEPHONY_LOGE("data is invalid");
2088         return false;
2089     }
2090     efCfis[CFIS_BCD_NUMBER_LENGTH_OFFSET] = static_cast<unsigned char>(dataLength);
2091     efCfis[CFIS_ADN_CAPABILITY_ID_OFFSET] = static_cast<unsigned char>(BYTE_NUM);
2092     efCfis[CFIS_ADN_EXTENSION_ID_OFFSET] = static_cast<unsigned char>(BYTE_NUM);
2093     return true;
2094 }
2095 
CphsVoiceMailAvailable()2096 bool SimFile::CphsVoiceMailAvailable()
2097 {
2098     bool available = false;
2099     if (!cphsInfo_.empty()) {
2100         int dataLen = 0;
2101         std::shared_ptr<unsigned char> dataByte = SIMUtils::HexStringConvertToBytes(cphsInfo_, dataLen);
2102         available = (dataByte != nullptr) ? (dataByte.get()[1] & CPHS_VOICE_MAIL_MASK) == CPHS_VOICE_MAIL_EXSIT : false;
2103     }
2104     return available;
2105 }
2106 
EfCfisAvailable(int32_t size)2107 bool SimFile::EfCfisAvailable(int32_t size)
2108 {
2109     if (efCfis_ != nullptr && size > 1) {
2110         for (int32_t i = 0; i < size; ++i) {
2111             if (efCfis_[i] != BYTE_NUM) {
2112                 return true;
2113             }
2114         }
2115     }
2116     return false;
2117 }
2118 
ClearData()2119 void SimFile::ClearData()
2120 {
2121     spnStatus_ = OBTAIN_SPN_NONE;
2122     reloadIccidCount_ = RELOAD_ICCID_COUNT;
2123     hasRetryGetImsi_ = false;
2124     IccFile::ClearData();
2125 }
2126 } // namespace Telephony
2127 } // namespace OHOS
2128