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