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