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