• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "radio_event.h"
21 
22 using namespace std;
23 using namespace OHOS::AppExecFwk;
24 
25 namespace OHOS {
26 namespace Telephony {
27 std::mutex IccFile::mtx_;
SimFile(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::shared_ptr<SimStateManager> simStateManager)28 SimFile::SimFile(
29     const std::shared_ptr<AppExecFwk::EventRunner> &runner, std::shared_ptr<SimStateManager> simStateManager)
30     : IccFile(runner, simStateManager)
31 {
32     fileQueried_ = false;
33     displayConditionOfSpn_ = SPN_INVALID;
34     InitMemberFunc();
35 }
36 
Init()37 void SimFile::Init()
38 {
39     TELEPHONY_LOGI("SimFile:::Init():start");
40     IccFile::Init();
41     if (stateManager_ != nullptr) {
42         stateManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
43         stateManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
44         stateManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_SIMLOCK);
45     }
46 }
47 
StartLoad()48 void SimFile::StartLoad()
49 {
50     TELEPHONY_LOGI("SimFile::StartLoad() start");
51     LoadSimFiles();
52 }
53 
ObtainSimOperator()54 std::string SimFile::ObtainSimOperator()
55 {
56     if (operatorNumeric_.empty()) {
57         std::string imsi = ObtainIMSI();
58         if (imsi.empty()) {
59             TELEPHONY_LOGE("SimFile ObtainSimOperator: IMSI is null");
60             return "";
61         }
62         if ((lengthOfMnc_ == UNINITIALIZED_MNC) || (lengthOfMnc_ == UNKNOWN_MNC)) {
63             TELEPHONY_LOGE("SimFile ObtainSimOperator:  mncLength invalid");
64             return "";
65         }
66         int length = MCC_LEN + lengthOfMnc_;
67         int imsiLen = imsi.size();
68         operatorNumeric_ = ((imsiLen >= length) ? imsi.substr(0, MCC_LEN + lengthOfMnc_) : "");
69     }
70     return operatorNumeric_;
71 }
72 
ObtainIsoCountryCode()73 std::string SimFile::ObtainIsoCountryCode()
74 {
75     std::string imsi = ObtainSimOperator();
76     if (imsi.empty()) {
77         TELEPHONY_LOGE("SimFile ObtainIsoCountryCode: IMSI is null");
78         return "";
79     }
80     int len = imsi.length();
81     if (len >= MCC_LEN) {
82         std::string mnc = imsi.substr(0, MCC_LEN);
83         std::string iso = MccPool::MccCountryCode(std::stoi(mnc));
84         return iso;
85     } else {
86         return "";
87     }
88 }
89 
ObtainCallForwardStatus()90 int SimFile::ObtainCallForwardStatus()
91 {
92     return callForwardStatus_;
93 }
94 
UpdateMsisdnNumber(const std::string & alphaTag,const std::string & number,const AppExecFwk::InnerEvent::Pointer & onComplete)95 void SimFile::UpdateMsisdnNumber(
96     const std::string &alphaTag, const std::string &number, const AppExecFwk::InnerEvent::Pointer &onComplete)
97 {}
98 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)99 void SimFile::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
100 {
101     auto id = event->GetInnerEventId();
102     TELEPHONY_LOGI("SimFile::ProcessEvent id %{public}d", id);
103     auto itFunc = memberFuncMap_.find(id);
104     if (itFunc != memberFuncMap_.end()) {
105         auto memberFunc = itFunc->second;
106         if (memberFunc != nullptr) {
107             bool isFileProcessResponse = (this->*memberFunc)(event);
108             ProcessFileLoaded(isFileProcessResponse);
109         }
110     } else {
111         IccFile::ProcessEvent(event);
112     }
113 }
114 
ProcessIccRefresh(int msgId)115 void SimFile::ProcessIccRefresh(int msgId)
116 {
117     switch (msgId) {
118         case ELEMENTARY_FILE_MBDN:
119             fileToGet_++;
120             break;
121         case ELEMENTARY_FILE_MAILBOX_CPHS:
122             fileToGet_++;
123             break;
124         case ELEMENTARY_FILE_CSP_CPHS:
125             fileToGet_++;
126             break;
127         case ELEMENTARY_FILE_FDN:
128             break;
129         case ELEMENTARY_FILE_MSISDN:
130             fileToGet_++;
131             break;
132         case ELEMENTARY_FILE_CFIS:
133         case ELEMENTARY_FILE_CFF_CPHS:
134             break;
135         default:
136             LoadSimFiles();
137             break;
138     }
139 }
140 
ProcessFileLoaded(bool response)141 void SimFile::ProcessFileLoaded(bool response)
142 {
143     if (!response) {
144         return;
145     }
146     fileToGet_ -= LOAD_STEP;
147     TELEPHONY_LOGI("SimFile ProcessFileLoaded Done: %{public}d requested: %{public}d", fileToGet_, fileQueried_);
148     if (ObtainFilesFetched()) {
149         OnAllFilesFetched();
150     } else if (LockQueriedOrNot()) {
151         UpdateSimLanguage();
152     } else if (fileToGet_ < 0) {
153         fileToGet_ = 0;
154     }
155 }
156 
OnAllFilesFetched()157 void SimFile::OnAllFilesFetched()
158 {
159     UpdateSimLanguage();
160     UpdateLoaded(true);
161     TELEPHONY_LOGI("SimFile SimFile::OnAllFilesFetched: start notify");
162     if (filesFetchedObser_ != nullptr) {
163         filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_SIM_RECORDS_LOADED, slotId_);
164     }
165     PublishSimFileEvent(SIM_STATE_ACTION, ICC_STATE_LOADED, "");
166     NotifyRegistrySimState(CardType::SINGLE_MODE_USIM_CARD, SimState::SIM_STATE_LOADED, LockReason::SIM_NONE);
167 }
168 
ProcessIccReady(const AppExecFwk::InnerEvent::Pointer & event)169 bool SimFile::ProcessIccReady(const AppExecFwk::InnerEvent::Pointer &event)
170 {
171     TELEPHONY_LOGI("SimFile::SIM_STATE_READY received");
172     CardType cardType = stateManager_->GetCardType();
173     if (cardType == CardType::SINGLE_MODE_USIM_CARD || cardType == CardType::SINGLE_MODE_SIM_CARD) {
174         LoadSimFiles();
175     } else {
176         TELEPHONY_LOGI("invalid SimFile::SIM_STATE_READY received %{public}d", cardType);
177     }
178     return false;
179 }
180 
ProcessIccLocked(const AppExecFwk::InnerEvent::Pointer & event)181 bool SimFile::ProcessIccLocked(const AppExecFwk::InnerEvent::Pointer &event)
182 {
183     TELEPHONY_LOGI("only fetch ELEMENTARY_FILE_LI, ELEMENTARY_FILE_PL and ELEMENTARY_FILE_ICCID in locked state");
184     lockQueried_ = true;
185     AppExecFwk::InnerEvent::Pointer eventIccId = BuildCallerInfo(MSG_SIM_OBTAIN_ICCID_DONE);
186     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_ICCID, eventIccId);
187     fileToGet_++;
188     return false;
189 }
190 
ObtainCallForwardFiles()191 void SimFile::ObtainCallForwardFiles()
192 {
193     fileQueried_ = true;
194 
195     AppExecFwk::InnerEvent::Pointer eventCFIS = BuildCallerInfo(MSG_SIM_OBTAIN_CFIS_DONE);
196     fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_CFIS, 1, eventCFIS);
197     fileToGet_++;
198 
199     AppExecFwk::InnerEvent::Pointer eventCFF = BuildCallerInfo(MSG_SIM_OBTAIN_CFF_DONE);
200     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_CFF_CPHS, eventCFF);
201     fileToGet_++;
202 }
203 
LoadSimFiles()204 void SimFile::LoadSimFiles()
205 {
206     TELEPHONY_LOGI("SimFile LoadSimFiles started");
207     fileQueried_ = true;
208 
209     AppExecFwk::InnerEvent::Pointer eventIMSI = BuildCallerInfo(MSG_SIM_OBTAIN_IMSI_DONE);
210     telRilManager_->GetImsi(slotId_, eventIMSI);
211     fileToGet_++;
212 
213     AppExecFwk::InnerEvent::Pointer eventIccId = BuildCallerInfo(MSG_SIM_OBTAIN_ICCID_DONE);
214     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_ICCID, eventIccId);
215     fileToGet_++;
216 
217     AppExecFwk::InnerEvent::Pointer eventSpn = AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
218     ObtainSpnPhase(true, eventSpn);
219 
220     AppExecFwk::InnerEvent::Pointer eventGid1 = BuildCallerInfo(MSG_SIM_OBTAIN_GID1_DONE);
221     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_GID1, eventGid1);
222     fileToGet_++;
223 
224     AppExecFwk::InnerEvent::Pointer phoneNumberEvent =
225         CreateDiallingNumberPointer(MSG_SIM_OBTAIN_MSISDN_DONE, 0, 0, nullptr);
226     diallingNumberHandler_->GetDiallingNumbers(
227         ELEMENTARY_FILE_MSISDN, ObtainExtensionElementaryFile(ELEMENTARY_FILE_MSISDN), 1, phoneNumberEvent);
228     fileToGet_++;
229 
230     AppExecFwk::InnerEvent::Pointer eventMBI = BuildCallerInfo(MSG_SIM_OBTAIN_MBI_DONE);
231     fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_MBI, 1, eventMBI);
232     fileToGet_++;
233 }
234 
ObtainSpnPhase(bool start,const AppExecFwk::InnerEvent::Pointer & event)235 void SimFile::ObtainSpnPhase(bool start, const AppExecFwk::InnerEvent::Pointer &event)
236 {
237     SpnStatus curStatus = spnStatus_;
238     if (!IsContinueGetSpn(start, curStatus, spnStatus_)) {
239         return;
240     }
241 
242     TELEPHONY_LOGI("SimFile::ObtainSpnPhase state is %{public}d", spnStatus_);
243     if (spnStatus_ == OBTAIN_SPN_START) {
244         StartObtainSpn();
245     } else if (spnStatus_ == OBTAIN_SPN_GENERAL) {
246         ProcessSpnGeneral(event);
247     } else if (spnStatus_ == OBTAIN_OPERATOR_NAMESTRING) {
248         ProcessSpnCphs(event);
249     } else if (spnStatus_ == OBTAIN_OPERATOR_NAME_SHORTFORM) {
250         ProcessSpnShortCphs(event);
251     } else {
252         spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
253     }
254 }
255 
StartObtainSpn()256 void SimFile::StartObtainSpn()
257 {
258     UpdateSPN(IccFileController::NULLSTR);
259     AppExecFwk::InnerEvent::Pointer eventSPN = BuildCallerInfo(MSG_SIM_OBTAIN_SPN_DONE);
260     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SPN, eventSPN);
261     fileToGet_++;
262     spnStatus_ = SpnStatus::OBTAIN_SPN_GENERAL;
263 }
264 
ProcessSpnGeneral(const AppExecFwk::InnerEvent::Pointer & event)265 void SimFile::ProcessSpnGeneral(const AppExecFwk::InnerEvent::Pointer &event)
266 {
267     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
268     if (fd != nullptr && fd->exception == nullptr) {
269         std::string iccData = fd->resultData;
270         int length = 0;
271         std::shared_ptr<unsigned char> hexData = SIMUtils::HexStringConvertToBytes(iccData, length);
272         if (hexData != nullptr) {
273             unsigned char *byteData = hexData.get();
274             unsigned char value = byteData[0];
275             displayConditionOfSpn_ = (BYTE_NUM & value);
276         }
277         std::string str = ParseSpn(iccData, spnStatus_);
278         UpdateSPN(str);
279         std::string spn = ObtainSPN();
280         if (spn.empty() || spn.size() == 0) {
281             spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAMESTRING;
282         } else {
283             TELEPHONY_LOGI("SimFile Load Spn3Gpp done");
284             spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
285         }
286     } else {
287         spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAMESTRING;
288     }
289 
290     if (spnStatus_ == SpnStatus::OBTAIN_OPERATOR_NAMESTRING) {
291         AppExecFwk::InnerEvent::Pointer eventCphs = BuildCallerInfo(MSG_SIM_OBTAIN_SPN_DONE);
292         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SPN_CPHS, eventCphs);
293         fileToGet_++;
294         displayConditionOfSpn_ = SPN_INVALID;
295     }
296 }
297 
ProcessSpnCphs(const AppExecFwk::InnerEvent::Pointer & event)298 void SimFile::ProcessSpnCphs(const AppExecFwk::InnerEvent::Pointer &event)
299 {
300     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
301     if (fd != nullptr && fd->exception == nullptr) {
302         std::string iccData = fd->resultData;
303         UpdateSPN(ParseSpn(iccData, spnStatus_));
304         std::string spn = ObtainSPN();
305         if (spn.empty() || spn.size() == 0) {
306             spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAME_SHORTFORM;
307         } else {
308             displayConditionOfSpn_ = SPN_COND;
309             TELEPHONY_LOGI("SimFile Load ELEMENTARY_FILE_SPN_CPHS done: %{public}s", spn.c_str());
310             spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
311         }
312     } else {
313         spnStatus_ = SpnStatus::OBTAIN_OPERATOR_NAME_SHORTFORM;
314     }
315 
316     if (spnStatus_ == SpnStatus::OBTAIN_OPERATOR_NAME_SHORTFORM) {
317         AppExecFwk::InnerEvent::Pointer eventShortCphs = BuildCallerInfo(MSG_SIM_OBTAIN_SPN_DONE);
318         fileController_->ObtainBinaryFile(ELEMENTARY_FILE_SPN_SHORT_CPHS, eventShortCphs);
319         fileToGet_++;
320     }
321 }
322 
ProcessSpnShortCphs(const AppExecFwk::InnerEvent::Pointer & event)323 void SimFile::ProcessSpnShortCphs(const AppExecFwk::InnerEvent::Pointer &event)
324 {
325     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
326     if (fd != nullptr && fd->exception == nullptr) {
327         std::string iccData = fd->resultData;
328         UpdateSPN(ParseSpn(iccData, spnStatus_));
329         std::string spn = ObtainSPN();
330         if (spn.empty() || spn.size() == 0) {
331             TELEPHONY_LOGI("SimFile No SPN loaded");
332         } else {
333             displayConditionOfSpn_ = SPN_COND;
334             TELEPHONY_LOGI("SimFile Load ELEMENTARY_FILE_SPN_SHORT_CPHS");
335         }
336     } else {
337         UpdateSPN(IccFileController::NULLSTR);
338         TELEPHONY_LOGI("SimFile No SPN get in either CHPS or 3GPP");
339     }
340     spnStatus_ = SpnStatus::OBTAIN_SPN_NONE;
341 }
342 
ParseSpn(const std::string & rawData,int spnStatus)343 std::string SimFile::ParseSpn(const std::string &rawData, int spnStatus)
344 {
345     int offset = 0;
346     int length = 0;
347     std::shared_ptr<unsigned char> bytesRaw = SIMUtils::HexStringConvertToBytes(rawData, length);
348     std::shared_ptr<unsigned char> bytesNew = nullptr;
349     if (bytesRaw == nullptr) {
350         TELEPHONY_LOGI("ParseSpn invalid data: %{public}s", rawData.c_str());
351         return "";
352     }
353     if (spnStatus == OBTAIN_SPN_GENERAL) {
354         offset = 0;
355         length -= INVALID_BYTES_NUM;
356         bytesNew = std::shared_ptr<unsigned char>(bytesRaw.get() + INVALID_BYTES_NUM,
357             [bytesRaw](unsigned char *) {}); // first is 0, +1
358     } else if ((spnStatus == OBTAIN_OPERATOR_NAMESTRING) || (spnStatus == OBTAIN_OPERATOR_NAME_SHORTFORM)) {
359         offset = 0;
360         bytesNew = bytesRaw;
361     } else {
362         return "";
363     }
364     std::string ret = SIMUtils::DiallingNumberStringFieldConvertToString(bytesNew, offset, length, SPN_CHAR_POS);
365     TELEPHONY_LOGI("SimFile::ParseSpn success");
366     return ret;
367 }
368 
ObtainUsimFunctionHandle()369 std::shared_ptr<UsimFunctionHandle> SimFile::ObtainUsimFunctionHandle()
370 {
371     return UsimFunctionHandle_;
372 }
373 
UpdateSimLanguage()374 void SimFile::UpdateSimLanguage()
375 {
376     UpdateIccLanguage(efLi_, efPl_);
377 }
378 
AnalysisBcdPlmn(std::string data,std::string description)379 std::string SimFile::AnalysisBcdPlmn(std::string data, std::string description)
380 {
381     return "";
382 }
383 
ProcessElementaryFileCsp(std::string data)384 void SimFile::ProcessElementaryFileCsp(std::string data) {}
385 
AnalysisElementaryFileSpdi(std::string data)386 void SimFile::AnalysisElementaryFileSpdi(std::string data) {}
387 
ProcessSmses(std::string messages)388 void SimFile::ProcessSmses(std::string messages) {}
389 
ProcessSms(std::string data)390 void SimFile::ProcessSms(std::string data) {}
391 
ProcessObtainGid1Done(const AppExecFwk::InnerEvent::Pointer & event)392 bool SimFile::ProcessObtainGid1Done(const AppExecFwk::InnerEvent::Pointer &event)
393 {
394     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
395     std::string iccData = fd->resultData;
396     bool isFileProcessResponse = true;
397     char *rawData = const_cast<char *>(iccData.c_str());
398     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
399 
400     if (fd->exception != nullptr) {
401         TELEPHONY_LOGE("SimFile failed in get GID1 ");
402         gid1_ = "";
403         return isFileProcessResponse;
404     }
405 
406     gid1_ = iccData;
407     TELEPHONY_LOGI("SimFile GID1: %{public}s", fileData);
408     return isFileProcessResponse;
409 }
410 
ProcessObtainGid2Done(const AppExecFwk::InnerEvent::Pointer & event)411 bool SimFile::ProcessObtainGid2Done(const AppExecFwk::InnerEvent::Pointer &event)
412 {
413     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
414     std::string iccData = fd->resultData;
415     bool isFileProcessResponse = true;
416     char *rawData = const_cast<char *>(iccData.c_str());
417     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
418 
419     if (fd->exception != nullptr) {
420         TELEPHONY_LOGE("SimFile failed in get GID2 ");
421         gid2_ = "";
422         return isFileProcessResponse;
423     }
424 
425     gid2_ = iccData;
426     TELEPHONY_LOGI("SimFile GID2: %{public}s", fileData);
427     return isFileProcessResponse;
428 }
429 
ProcessGetMsisdnDone(const AppExecFwk::InnerEvent::Pointer & event)430 bool SimFile::ProcessGetMsisdnDone(const AppExecFwk::InnerEvent::Pointer &event)
431 {
432     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
433     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
434     bool isFileProcessResponse = true;
435     if (fd->exception != nullptr) {
436         TELEPHONY_LOGE("SimFile Invalid or missing EF[MSISDN]");
437         return isFileProcessResponse;
438     }
439     msisdn_ = Str16ToStr8(diallingNumber->GetNumber());
440     msisdnTag_ = Str16ToStr8(diallingNumber->GetName());
441     return isFileProcessResponse;
442 }
443 
ProcessSetMsisdnDone(const AppExecFwk::InnerEvent::Pointer & event)444 bool SimFile::ProcessSetMsisdnDone(const AppExecFwk::InnerEvent::Pointer &event)
445 {
446     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
447     std::string iccData = fd->resultData;
448     bool isFileProcessResponse = false;
449     if (fd->exception == nullptr) {
450         msisdn_ = lastMsisdn_;
451         msisdnTag_ = lastMsisdnTag_;
452         TELEPHONY_LOGI("SimFile Success to update EF[MSISDN]");
453     }
454     return isFileProcessResponse;
455 }
456 
ProcessGetSpdiDone(const AppExecFwk::InnerEvent::Pointer & event)457 bool SimFile::ProcessGetSpdiDone(const AppExecFwk::InnerEvent::Pointer &event)
458 {
459     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
460     std::string iccData = fd->resultData;
461     bool isFileProcessResponse = true;
462     char *rawData = const_cast<char *>(iccData.c_str());
463     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
464 
465     if (fd->exception != nullptr) {
466         return isFileProcessResponse;
467     }
468     TELEPHONY_LOGI("SimFile MSG_SIM_OBTAIN_SPDI_DONE data:%{public}s", fileData);
469     AnalysisElementaryFileSpdi(iccData);
470     return isFileProcessResponse;
471 }
472 
ProcessGetCfisDone(const AppExecFwk::InnerEvent::Pointer & event)473 bool SimFile::ProcessGetCfisDone(const AppExecFwk::InnerEvent::Pointer &event)
474 {
475     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
476     std::string iccData = fd->resultData;
477     bool isFileProcessResponse = true;
478     char *rawData = const_cast<char *>(iccData.c_str());
479     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
480 
481     if (fd->exception != nullptr) {
482         efCfis_ = nullptr;
483     } else {
484         TELEPHONY_LOGI("ELEMENTARY_FILE_CFIS: %{public}s", fileData);
485         efCfis_ = fileData;
486     }
487     return isFileProcessResponse;
488 }
489 
ProcessGetMbiDone(const AppExecFwk::InnerEvent::Pointer & event)490 bool SimFile::ProcessGetMbiDone(const AppExecFwk::InnerEvent::Pointer &event)
491 {
492     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
493     std::string iccData = fd->resultData;
494     bool isFileProcessResponse = true;
495     bool isValidMbdn = false;
496     char *rawData = const_cast<char *>(iccData.c_str());
497     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
498     if (fd->exception == nullptr) {
499         int dataLen = 0;
500         std::shared_ptr<unsigned char> dataByte = SIMUtils::HexStringConvertToBytes(iccData, dataLen);
501         int index = (dataByte != nullptr) ? (dataByte.get()[0] & BYTE_NUM) : 0;
502         if (index != 0 && index != BYTE_NUM) {
503             indexOfMailbox_ = index;
504             TELEPHONY_LOGI("fetch valid mailbox number for MBDN");
505             isValidMbdn = true;
506         } else {
507             isValidMbdn = true;
508         }
509     }
510     TELEPHONY_LOGI("ELEMENTARY_FILE_MBI data is:%{public}s id: %{public}d", fileData, indexOfMailbox_);
511     fileToGet_ += LOAD_STEP;
512     AppExecFwk::InnerEvent::Pointer mbdnEvent =
513         CreateDiallingNumberPointer(MSG_SIM_OBTAIN_MBDN_DONE, 0, 0, nullptr);
514     diallingNumberHandler_->GetDiallingNumbers(ELEMENTARY_FILE_MBDN, ELEMENTARY_FILE_EXT6, indexOfMailbox_, mbdnEvent);
515     return isFileProcessResponse;
516 }
517 
ProcessGetMbdnDone(const AppExecFwk::InnerEvent::Pointer & event)518 bool SimFile::ProcessGetMbdnDone(const AppExecFwk::InnerEvent::Pointer &event)
519 {
520     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
521     bool isFileProcessResponse = true;
522     bool hasException = fd->exception == nullptr;
523     TELEPHONY_LOGI("ProcessGetMbdnDone start %{public}d", hasException);
524     voiceMailNum_ = IccFileController::NULLSTR;
525     voiceMailTag_ = IccFileController::NULLSTR;
526     if (fd->exception != nullptr) {
527         TELEPHONY_LOGE("SimFile failed missing EF MBDN");
528         GetCphsMailBox();
529         return isFileProcessResponse;
530     }
531     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
532     if (diallingNumber == nullptr) {
533         TELEPHONY_LOGE("ProcessGetMbdnDone get null diallingNumber!!");
534         return isFileProcessResponse;
535     }
536 
537     if (diallingNumber->IsEmpty()) {
538         GetCphsMailBox();
539         return isFileProcessResponse;
540     }
541     voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
542     voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
543     TELEPHONY_LOGI("ProcessGetMbdnDone success");
544     return isFileProcessResponse;
545 }
546 
ProcessGetCphsMailBoxDone(const AppExecFwk::InnerEvent::Pointer & event)547 bool SimFile::ProcessGetCphsMailBoxDone(const AppExecFwk::InnerEvent::Pointer &event)
548 {
549     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
550     bool isFileProcessResponse = true;
551     bool hasException = fd->exception == nullptr;
552     TELEPHONY_LOGI("ProcessGetCphsMailBoxDone start %{public}d", hasException);
553     voiceMailNum_ = IccFileController::NULLSTR;
554     voiceMailTag_ = IccFileController::NULLSTR;
555     if (fd->exception != nullptr) {
556         TELEPHONY_LOGE("SimFile failed missing CPHS MAILBOX");
557         return isFileProcessResponse;
558     }
559     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
560     if (diallingNumber == nullptr) {
561         TELEPHONY_LOGE("GetCphsMailBoxDone get null diallingNumber!!");
562         return isFileProcessResponse;
563     }
564 
565     voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
566     voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
567     TELEPHONY_LOGI("GetCphsMailBoxDone success");
568     return isFileProcessResponse;
569 }
570 
GetCphsMailBox()571 void SimFile::GetCphsMailBox()
572 {
573     fileToGet_ += LOAD_STEP;
574     AppExecFwk::InnerEvent::Pointer cphsEvent =
575         CreateDiallingNumberPointer(MSG_SIM_OBTAIN_CPHS_MAILBOX_DONE, 0, 0, nullptr);
576     diallingNumberHandler_->GetDiallingNumbers(ELEMENTARY_FILE_MAILBOX_CPHS, ELEMENTARY_FILE_EXT1, 1, cphsEvent);
577 }
578 
ProcessGetMwisDone(const AppExecFwk::InnerEvent::Pointer & event)579 bool SimFile::ProcessGetMwisDone(const AppExecFwk::InnerEvent::Pointer &event)
580 {
581     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
582     std::string iccData = fd->resultData;
583     bool isFileProcessResponse = true;
584     char *rawData = const_cast<char *>(iccData.c_str());
585     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
586     TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_MWIS : %{public}s", rawData);
587     if (fd->exception != nullptr) {
588         TELEPHONY_LOGE("MSG_SIM_OBTAIN_MWIS_DONE exception = ");
589         return isFileProcessResponse;
590     }
591     unsigned char value = fileData[0];
592     if ((value & BYTE_NUM) == BYTE_NUM) {
593         TELEPHONY_LOGI("SimFiles: Uninitialized record MWIS");
594         return isFileProcessResponse;
595     }
596     efMWIS_ = fileData;
597     return isFileProcessResponse;
598 }
599 
ProcessVoiceMailCphs(const AppExecFwk::InnerEvent::Pointer & event)600 bool SimFile::ProcessVoiceMailCphs(const AppExecFwk::InnerEvent::Pointer &event)
601 {
602     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
603     std::string iccData = fd->resultData;
604     bool isFileProcessResponse = true;
605     char *rawData = const_cast<char *>(iccData.c_str());
606     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
607     TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_CPHS_MWI: %{public}s", rawData);
608     if (fd->exception != nullptr) {
609         TELEPHONY_LOGE("MSG_SIM_OBTAIN_VOICE_MAIL_INDICATOR_CPHS_DONE exception = ");
610         return isFileProcessResponse;
611     }
612     efCphsMwi_ = fileData;
613     return isFileProcessResponse;
614 }
615 
ProcessGetIccIdDone(const AppExecFwk::InnerEvent::Pointer & event)616 bool SimFile::ProcessGetIccIdDone(const AppExecFwk::InnerEvent::Pointer &event)
617 {
618     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
619     bool isFileProcessResponse = true;
620     if (fd->exception == nullptr) {
621         std::string iccData = fd->resultData;
622         TELEPHONY_LOGI("SimFile::ProcessEvent ICCID result success");
623         iccId_ = iccData;
624     }
625     return isFileProcessResponse;
626 }
627 
ProcessObtainIMSIDone(const AppExecFwk::InnerEvent::Pointer & event)628 bool SimFile::ProcessObtainIMSIDone(const AppExecFwk::InnerEvent::Pointer &event)
629 {
630     std::shared_ptr<std::string> sharedObject = event->GetSharedObject<std::string>();
631     bool isFileProcessResponse = true;
632     if (sharedObject != nullptr) {
633         imsi_ = *sharedObject;
634         TELEPHONY_LOGI("SimFile::ProcessEvent IMSI received success");
635         std::string iso = ObtainIsoCountryCode();
636         TELEPHONY_LOGI("SimFile::ObtainIsoCountryCode result success");
637         if (!imsi_.empty()) {
638             imsiReadyObser_->NotifyObserver(RadioEvent::RADIO_IMSI_LOADED_READY);
639             PublishSimFileEvent(SIM_STATE_ACTION, ICC_STATE_IMSI, imsi_);
640         }
641     }
642     return isFileProcessResponse;
643 }
644 
ProcessGetCffDone(const AppExecFwk::InnerEvent::Pointer & event)645 bool SimFile::ProcessGetCffDone(const AppExecFwk::InnerEvent::Pointer &event)
646 {
647     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
648     std::string iccData = fd->resultData;
649     bool isFileProcessResponse = true;
650     char *rawData = const_cast<char *>(iccData.c_str());
651     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
652     if (fd->exception != nullptr) {
653         efCff_ = nullptr;
654     } else {
655         TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_CFF_CPHS: %{public}s", rawData);
656         efCff_ = fileData;
657     }
658     return isFileProcessResponse;
659 }
660 
ProcessGetAdDone(const AppExecFwk::InnerEvent::Pointer & event)661 bool SimFile::ProcessGetAdDone(const AppExecFwk::InnerEvent::Pointer &event)
662 {
663     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
664     std::string iccData = fd->resultData;
665     bool isFileProcessResponse = true;
666     bool doneData = true;
667     if (!ObtainIMSI().empty()) {
668         std::string imsi = ObtainIMSI();
669         int mcc = atoi(imsi.substr(0, MCC_LEN).c_str());
670         lengthOfMnc_ = MccPool::ShortestMncLengthFromMcc(mcc);
671         TELEPHONY_LOGI("SimFile [TestMode] lengthOfMnc_= %{public}d", lengthOfMnc_);
672     } else {
673         char *rawData = const_cast<char *>(iccData.c_str());
674         unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
675         if (fd->exception != nullptr) {
676             doneData = false;
677         }
678         TELEPHONY_LOGI("SimFile ELEMENTARY_FILE_AD: %{public}s", rawData);
679         int dataSize = iccData.size();
680         if (dataSize <= MCC_LEN) {
681             TELEPHONY_LOGI("SimFile MNC length dataSize = %{public}d", dataSize);
682             doneData = false;
683         }
684         if (doneData) {
685             lengthOfMnc_ = fileData[MCC_LEN] & 0xf;
686             TELEPHONY_LOGI("setting4 lengthOfMnc_= %{public}d", lengthOfMnc_);
687         }
688     }
689 
690     if (doneData && (lengthOfMnc_ == 0xf)) {
691         lengthOfMnc_ = UNKNOWN_MNC;
692     } else if (doneData && (lengthOfMnc_ != MNC_LEN) && (lengthOfMnc_ != MCC_LEN)) {
693         lengthOfMnc_ = UNINITIALIZED_MNC;
694     }
695     TELEPHONY_LOGI("update5 length Mnc_= %{public}d", lengthOfMnc_);
696     CheckMncLength();
697     return isFileProcessResponse;
698 }
699 
CheckMncLength()700 void SimFile::CheckMncLength()
701 {
702     std::string imsi = ObtainIMSI();
703     int imsiSize = imsi.size();
704     if (((lengthOfMnc_ == UNINITIALIZED_MNC) || (lengthOfMnc_ == UNKNOWN_MNC) || (lengthOfMnc_ == MNC_LEN)) &&
705         ((!imsi.empty()) && (imsiSize >= MCCMNC_LEN))) {
706         std::string mccMncCode = imsi.substr(0, MCCMNC_LEN);
707         TELEPHONY_LOGI("SimFile mccMncCode= %{public}s", mccMncCode.c_str());
708         if (MccPool::LengthIsThreeMnc(mccMncCode)) {
709             lengthOfMnc_ = MCC_LEN;
710             TELEPHONY_LOGI("SimFile update6 lengthOfMnc_= %{public}d", lengthOfMnc_);
711         }
712     }
713 
714     if (lengthOfMnc_ == UNKNOWN_MNC || lengthOfMnc_ == UNINITIALIZED_MNC) {
715         if (!imsi.empty()) {
716             int mcc = atoi(imsi.substr(0, MCC_LEN).c_str());
717             lengthOfMnc_ = MccPool::ShortestMncLengthFromMcc(mcc);
718             TELEPHONY_LOGI("SimFile update7 lengthOfMnc_= %{public}d", lengthOfMnc_);
719         } else {
720             lengthOfMnc_ = UNKNOWN_MNC;
721             TELEPHONY_LOGI(
722                 "MNC length not present in ELEMENTARY_FILE_AD setting9 lengthOfMnc_= %{public}d", lengthOfMnc_);
723         }
724     }
725     int lenNum = MCC_LEN + lengthOfMnc_;
726     int sz = imsi.size();
727     bool cond = sz >= lenNum;
728     if ((!imsi.empty()) && (lengthOfMnc_ != UNKNOWN_MNC) && cond) {
729     }
730 }
731 
ProcessSmsOnSim(const AppExecFwk::InnerEvent::Pointer & event)732 bool SimFile::ProcessSmsOnSim(const AppExecFwk::InnerEvent::Pointer &event)
733 {
734     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
735     std::string iccData = fd->resultData;
736     int index = atoi(iccData.c_str());
737     bool isFileProcessResponse = false;
738     if (fd->exception != nullptr || index == INVALID_VALUE) {
739         TELEPHONY_LOGE("exception on SMS_ON_SIM with index: %{public}d", index);
740     } else {
741         TELEPHONY_LOGI("READ ELEMENTARY_FILE_SMS RECORD index= %{public}d", index);
742         AppExecFwk::InnerEvent::Pointer eventSMS = BuildCallerInfo(MSG_SIM_OBTAIN_SMS_DONE);
743         fileController_->ObtainLinearFixedFile(ELEMENTARY_FILE_SMS, index, eventSMS);
744     }
745     return isFileProcessResponse;
746 }
747 
ProcessGetAllSmsDone(const AppExecFwk::InnerEvent::Pointer & event)748 bool SimFile::ProcessGetAllSmsDone(const AppExecFwk::InnerEvent::Pointer &event)
749 {
750     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
751     std::string iccData = fd->resultData;
752     bool isFileProcessResponse = true;
753     if (fd->exception != nullptr) {
754         return isFileProcessResponse;
755     }
756     ProcessSmses(iccData);
757     return isFileProcessResponse;
758 }
759 
ProcessGetSmsDone(const AppExecFwk::InnerEvent::Pointer & event)760 bool SimFile::ProcessGetSmsDone(const AppExecFwk::InnerEvent::Pointer &event)
761 {
762     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
763     std::string iccData = fd->resultData;
764     bool isFileProcessResponse = false;
765     if (fd->exception == nullptr) {
766         ProcessSms(iccData);
767     } else {
768         TELEPHONY_LOGI("SimFile exception on GET_SMS ");
769     }
770     return isFileProcessResponse;
771 }
772 
ProcessGetPlmnActDone(const AppExecFwk::InnerEvent::Pointer & event)773 bool SimFile::ProcessGetPlmnActDone(const AppExecFwk::InnerEvent::Pointer &event)
774 {
775     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
776     std::string iccData = fd->resultData;
777     bool isFileProcessResponse = true;
778     char *rawData = const_cast<char *>(iccData.c_str());
779     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
780 
781     if (fd->exception != nullptr || iccData.empty()) {
782         TELEPHONY_LOGE("Failed fetch User PLMN ");
783     } else {
784         TELEPHONY_LOGI("fetch a PlmnRAT, iccData= %{public}s", fileData);
785     }
786     return isFileProcessResponse;
787 }
788 
789 // Process operator plmn
ProcessGetOplmnActDone(const AppExecFwk::InnerEvent::Pointer & event)790 bool SimFile::ProcessGetOplmnActDone(const AppExecFwk::InnerEvent::Pointer &event)
791 {
792     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
793     std::string iccData = fd->resultData;
794     bool isFileProcessResponse = true;
795     char *rawData = const_cast<char *>(iccData.c_str());
796     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
797 
798     if (fd->exception != nullptr || iccData.empty()) {
799         TELEPHONY_LOGE("Failed fetch Operator PLMN");
800     } else {
801         TELEPHONY_LOGI("fetch a OPlmnRAT, iccData= %{public}s", fileData);
802     }
803     return isFileProcessResponse;
804 }
805 
ProcessGetCspCphs(const AppExecFwk::InnerEvent::Pointer & event)806 bool SimFile::ProcessGetCspCphs(const AppExecFwk::InnerEvent::Pointer &event)
807 {
808     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
809     std::string iccData = fd->resultData;
810     bool isFileProcessResponse = true;
811     char *rawData = const_cast<char *>(iccData.c_str());
812     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
813 
814     if (fd->exception != nullptr) {
815         TELEPHONY_LOGE("Exception to get ELEMENTARY_FILE_CSP data ");
816         return isFileProcessResponse;
817     }
818     TELEPHONY_LOGI("SimFile MSG_SIM_OBTAIN_CSP_CPHS_DONE data:%{public}s", fileData);
819     TELEPHONY_LOGI("ELEMENTARY_FILE_CSP: %{public}s", fileData);
820     ProcessElementaryFileCsp(iccData);
821     return isFileProcessResponse;
822 }
823 
ProcessGetInfoCphs(const AppExecFwk::InnerEvent::Pointer & event)824 bool SimFile::ProcessGetInfoCphs(const AppExecFwk::InnerEvent::Pointer &event)
825 {
826     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
827     bool isFileProcessResponse = true;
828     if (fd->exception != nullptr) {
829         return isFileProcessResponse;
830     }
831     cphsInfo_ = fd->resultData;
832     TELEPHONY_LOGI("SimFile::ProcessGetInfoCphs success");
833     return isFileProcessResponse;
834 }
835 
ProcessGetSstDone(const AppExecFwk::InnerEvent::Pointer & event)836 bool SimFile::ProcessGetSstDone(const AppExecFwk::InnerEvent::Pointer &event)
837 {
838     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
839     std::string iccData = fd->resultData;
840     bool isFileProcessResponse = true;
841     char *rawData = const_cast<char *>(iccData.c_str());
842     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
843 
844     if (fd->exception != nullptr) {
845         return isFileProcessResponse;
846     }
847     TELEPHONY_LOGI("SimFile MSG_SIM_OBTAIN_SST_DONE data:%{public}s", fileData);
848     return isFileProcessResponse;
849 }
850 
ProcessGetPnnDone(const AppExecFwk::InnerEvent::Pointer & event)851 bool SimFile::ProcessGetPnnDone(const AppExecFwk::InnerEvent::Pointer &event)
852 {
853     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
854     std::string iccData = fd->resultData;
855     bool isFileProcessResponse = true;
856     if (fd->exception != nullptr) {
857         return isFileProcessResponse;
858     }
859     return isFileProcessResponse;
860 }
861 
ProcessUpdateDone(const AppExecFwk::InnerEvent::Pointer & event)862 bool SimFile::ProcessUpdateDone(const AppExecFwk::InnerEvent::Pointer &event)
863 {
864     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
865     std::string iccData = fd->resultData;
866     bool isFileProcessResponse = false;
867     if (fd->exception != nullptr) {
868         TELEPHONY_LOGE("SimFile failed to update");
869     }
870     return isFileProcessResponse;
871 }
872 
ProcessSetCphsMailbox(const AppExecFwk::InnerEvent::Pointer & event)873 bool SimFile::ProcessSetCphsMailbox(const AppExecFwk::InnerEvent::Pointer &event)
874 {
875     bool isFileProcessResponse = true;
876     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
877     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
878     if (fd->exception == nullptr) {
879         voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
880         voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
881         waitResult_ = true;
882         processWait_.notify_all();
883         TELEPHONY_LOGI("set cphs voicemail success");
884     } else {
885         processWait_.notify_all();
886         TELEPHONY_LOGE("set cphs voicemail failed with exception!!");
887     }
888     return isFileProcessResponse;
889 }
890 
891 // Process forbidden PLMNs
ProcessGetHplmActDone(const AppExecFwk::InnerEvent::Pointer & event)892 bool SimFile::ProcessGetHplmActDone(const AppExecFwk::InnerEvent::Pointer &event)
893 {
894     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
895     std::string iccData = fd->resultData;
896     bool isFileProcessResponse = true;
897     char *rawData = const_cast<char *>(iccData.c_str());
898     unsigned char *fileData = reinterpret_cast<unsigned char *>(rawData);
899 
900     if (fd->exception != nullptr || iccData.empty()) {
901         TELEPHONY_LOGE("Failed to fetch forbidden PLMN");
902         return isFileProcessResponse;
903     } else {
904         TELEPHONY_LOGI("fetch a FPlmnRAT, iccData=%{public}s", fileData);
905     }
906     return isFileProcessResponse;
907 }
908 
909 // Process Equivalent Home PLMNs
ProcessGetEhplmnDone(const AppExecFwk::InnerEvent::Pointer & event)910 bool SimFile::ProcessGetEhplmnDone(const AppExecFwk::InnerEvent::Pointer &event)
911 {
912     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
913     std::string iccData = fd->resultData;
914     bool isFileProcessResponse = true;
915     if (fd->exception != nullptr || iccData.empty()) {
916         TELEPHONY_LOGE("Failed fetch Equivalent Home PLMNs");
917         return isFileProcessResponse;
918     } else {
919     }
920     return isFileProcessResponse;
921 }
922 
923 // Process forbidden PLMNs
ProcessGetFplmnDone(const AppExecFwk::InnerEvent::Pointer & event)924 bool SimFile::ProcessGetFplmnDone(const AppExecFwk::InnerEvent::Pointer &event)
925 {
926     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
927     std::string iccData = fd->resultData;
928     bool loadResponse = true;
929     if (fd->exception != nullptr || iccData.empty()) {
930         TELEPHONY_LOGE("Failed to get forbidden PLMNs");
931         return loadResponse;
932     } else {
933     }
934     if (fd->arg1 == ICC_CONTROLLER_REQ_SEND_RESPONSE) {
935         TELEPHONY_LOGI("getForbiddenPlmns and send result");
936         loadResponse = false;
937     }
938     return loadResponse;
939 }
940 
ProcessSetMbdn(const AppExecFwk::InnerEvent::Pointer & event)941 bool SimFile::ProcessSetMbdn(const AppExecFwk::InnerEvent::Pointer &event)
942 {
943     bool isFileProcessResponse = true;
944     bool hasNotify = false;
945     std::unique_ptr<DiallingNumbersHandlerResult> fd = event->GetUniqueObject<DiallingNumbersHandlerResult>();
946     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::static_pointer_cast<DiallingNumbersInfo>(fd->result);
947     if (fd->exception == nullptr) {
948         voiceMailNum_ = Str16ToStr8(diallingNumber->GetNumber());
949         voiceMailTag_ = Str16ToStr8(diallingNumber->GetName());
950         waitResult_ = true;
951         processWait_.notify_all();
952         hasNotify = true;
953         TELEPHONY_LOGI("set voicemail name success");
954     }
955 
956     if (CphsVoiceMailAvailable()) {
957         std::shared_ptr<DiallingNumbersInfo> diallingNumberCphs = std::make_shared<DiallingNumbersInfo>();
958         diallingNumberCphs->name_ = Str8ToStr16(voiceMailNum_);
959         diallingNumberCphs->number_ = Str8ToStr16(voiceMailTag_);
960         AppExecFwk::InnerEvent::Pointer eventCphs =
961             CreateDiallingNumberPointer(MSG_SIM_SET_CPHS_MAILBOX_DONE, 0, 0, nullptr);
962         DiallingNumberUpdateInfor infor;
963         infor.diallingNumber = diallingNumberCphs;
964         infor.fileId = ELEMENTARY_FILE_MAILBOX_CPHS;
965         infor.extFile = ELEMENTARY_FILE_EXT1;
966         infor.index = 1;
967         diallingNumberHandler_->UpdateDiallingNumbers(infor, eventCphs);
968         TELEPHONY_LOGI("set cphs voicemail number as it is available");
969     } else {
970         if (!hasNotify) {
971             processWait_.notify_all();
972         }
973         TELEPHONY_LOGI("set voicemail number finished");
974     }
975     return isFileProcessResponse;
976 }
977 
ProcessMarkSms(const AppExecFwk::InnerEvent::Pointer & event)978 bool SimFile::ProcessMarkSms(const AppExecFwk::InnerEvent::Pointer &event)
979 {
980     (void)event;
981     return false;
982 }
983 
ProcessObtainSpnPhase(const AppExecFwk::InnerEvent::Pointer & event)984 bool SimFile::ProcessObtainSpnPhase(const AppExecFwk::InnerEvent::Pointer &event)
985 {
986     bool loadResponse = true;
987     ObtainSpnPhase(false, event);
988     return loadResponse;
989 }
IsContinueGetSpn(bool start,SpnStatus curStatus,SpnStatus & newStatus)990 bool SimFile::IsContinueGetSpn(bool start, SpnStatus curStatus, SpnStatus &newStatus)
991 {
992     if (start) {
993         switch (curStatus) {
994             case OBTAIN_SPN_GENERAL:
995             case OBTAIN_OPERATOR_NAMESTRING:
996             case OBTAIN_OPERATOR_NAME_SHORTFORM:
997             case OBTAIN_SPN_START:
998                 newStatus = SpnStatus::OBTAIN_SPN_START;
999                 return false;
1000             default:
1001                 newStatus = SpnStatus::OBTAIN_SPN_START;
1002                 return true;
1003         }
1004     } else {
1005         return true;
1006     }
1007 }
InitMemberFunc()1008 void SimFile::InitMemberFunc()
1009 {
1010     memberFuncMap_[RadioEvent::RADIO_SIM_STATE_READY] = &SimFile::ProcessIccReady;
1011     memberFuncMap_[RadioEvent::RADIO_SIM_STATE_LOCKED] = &SimFile::ProcessIccLocked;
1012     memberFuncMap_[RadioEvent::RADIO_SIM_STATE_SIMLOCK] = &SimFile::ProcessIccLocked;
1013     memberFuncMap_[MSG_SIM_OBTAIN_IMSI_DONE] = &SimFile::ProcessObtainIMSIDone;
1014     memberFuncMap_[MSG_SIM_OBTAIN_ICCID_DONE] = &SimFile::ProcessGetIccIdDone;
1015     memberFuncMap_[MSG_SIM_OBTAIN_MBI_DONE] = &SimFile::ProcessGetMbiDone;
1016     memberFuncMap_[MSG_SIM_OBTAIN_CPHS_MAILBOX_DONE] = &SimFile::ProcessGetCphsMailBoxDone;
1017     memberFuncMap_[MSG_SIM_OBTAIN_MBDN_DONE] = &SimFile::ProcessGetMbdnDone;
1018     memberFuncMap_[MSG_SIM_OBTAIN_MSISDN_DONE] = &SimFile::ProcessGetMsisdnDone;
1019     memberFuncMap_[MSG_SIM_SET_MSISDN_DONE] = &SimFile::ProcessSetMsisdnDone;
1020     memberFuncMap_[MSG_SIM_OBTAIN_MWIS_DONE] = &SimFile::ProcessGetMwisDone;
1021     memberFuncMap_[MSG_SIM_OBTAIN_VOICE_MAIL_INDICATOR_CPHS_DONE] = &SimFile::ProcessVoiceMailCphs;
1022     memberFuncMap_[MSG_SIM_OBTAIN_AD_DONE] = &SimFile::ProcessGetAdDone;
1023     memberFuncMap_[MSG_SIM_OBTAIN_SPN_DONE] = &SimFile::ProcessObtainSpnPhase;
1024     memberFuncMap_[MSG_SIM_OBTAIN_CFF_DONE] = &SimFile::ProcessGetCffDone;
1025     memberFuncMap_[MSG_SIM_OBTAIN_SPDI_DONE] = &SimFile::ProcessGetSpdiDone;
1026     memberFuncMap_[MSG_SIM_UPDATE_DONE] = &SimFile::ProcessUpdateDone;
1027     memberFuncMap_[MSG_SIM_OBTAIN_PNN_DONE] = &SimFile::ProcessGetPnnDone;
1028     memberFuncMap_[MSG_SIM_OBTAIN_ALL_SMS_DONE] = &SimFile::ProcessGetAllSmsDone;
1029     memberFuncMap_[MSG_SIM_MARK_SMS_READ_DONE] = &SimFile::ProcessMarkSms;
1030     memberFuncMap_[MSG_SIM_SMS_ON_SIM] = &SimFile::ProcessSmsOnSim;
1031     memberFuncMap_[MSG_SIM_OBTAIN_SMS_DONE] = &SimFile::ProcessGetSmsDone;
1032     memberFuncMap_[MSG_SIM_OBTAIN_SST_DONE] = &SimFile::ProcessGetSstDone;
1033     memberFuncMap_[MSG_SIM_OBTAIN_INFO_CPHS_DONE] = &SimFile::ProcessGetInfoCphs;
1034     memberFuncMap_[MSG_SIM_SET_MBDN_DONE] = &SimFile::ProcessSetMbdn;
1035     memberFuncMap_[MSG_SIM_SET_CPHS_MAILBOX_DONE] = &SimFile::ProcessSetCphsMailbox;
1036     memberFuncMap_[MSG_SIM_OBTAIN_CFIS_DONE] = &SimFile::ProcessGetCfisDone;
1037     memberFuncMap_[MSG_SIM_OBTAIN_CSP_CPHS_DONE] = &SimFile::ProcessGetCspCphs;
1038     memberFuncMap_[MSG_SIM_OBTAIN_GID1_DONE] = &SimFile::ProcessObtainGid1Done;
1039     memberFuncMap_[MSG_SIM_OBTAIN_GID2_DONE] = &SimFile::ProcessObtainGid2Done;
1040     memberFuncMap_[MSG_SIM_OBTAIN_PLMN_W_ACT_DONE] = &SimFile::ProcessGetPlmnActDone;
1041     memberFuncMap_[MSG_SIM_OBTAIN_OPLMN_W_ACT_DONE] = &SimFile::ProcessGetOplmnActDone;
1042     memberFuncMap_[MSG_SIM_OBTAIN_HPLMN_W_ACT_DONE] = &SimFile::ProcessGetHplmActDone;
1043     memberFuncMap_[MSG_SIM_OBTAIN_EHPLMN_DONE] = &SimFile::ProcessGetEhplmnDone;
1044     memberFuncMap_[MSG_SIM_OBTAIN_FPLMN_DONE] = &SimFile::ProcessGetFplmnDone;
1045 }
1046 
~SimFile()1047 SimFile::~SimFile()
1048 {
1049     if (stateManager_ != nullptr) {
1050         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
1051         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
1052         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_SIMLOCK);
1053     }
1054 }
1055 
ObtainSpnCondition(bool roaming,const std::string & operatorNum)1056 int SimFile::ObtainSpnCondition(bool roaming, const std::string &operatorNum)
1057 {
1058     int cond = 0;
1059     if (ObtainSPN().empty() || (displayConditionOfSpn_ == SPN_INVALID)) {
1060         cond = SPN_CONDITION_DISPLAY_PLMN;
1061     } else if (!roaming || !operatorNum.empty()) {
1062         cond = SPN_CONDITION_DISPLAY_SPN;
1063         if ((displayConditionOfSpn_ & SPN_COND_PLMN) == SPN_COND_PLMN) {
1064             cond |= SPN_CONDITION_DISPLAY_PLMN;
1065         }
1066     } else {
1067         cond = SPN_CONDITION_DISPLAY_SPN;
1068     }
1069     return cond;
1070 }
1071 
ObtainExtensionElementaryFile(int ef)1072 int SimFile::ObtainExtensionElementaryFile(int ef)
1073 {
1074     int ext = 0;
1075     if (ef == ELEMENTARY_FILE_MSISDN) {
1076         ext = ELEMENTARY_FILE_EXT5; // ELEMENTARY_FILE_EXT1
1077     } else {
1078         ext = ELEMENTARY_FILE_EXT1;
1079     }
1080     return ext;
1081 }
1082 
UpdateVoiceMail(const std::string & mailName,const std::string & mailNumber)1083 bool SimFile::UpdateVoiceMail(const std::string &mailName, const std::string &mailNumber)
1084 {
1085     waitResult_ = false;
1086     std::shared_ptr<DiallingNumbersInfo> diallingNumber = std::make_shared<DiallingNumbersInfo>();
1087     diallingNumber->name_ = Str8ToStr16(mailName);
1088     diallingNumber->number_ = Str8ToStr16(mailNumber);
1089 
1090     if ((indexOfMailbox_ != 0) && (indexOfMailbox_ != BYTE_NUM)) {
1091         std::unique_lock<std::mutex> lock(IccFile::mtx_);
1092         TELEPHONY_LOGI("UpdateVoiceMail start MBDN");
1093         AppExecFwk::InnerEvent::Pointer event =
1094             CreateDiallingNumberPointer(MSG_SIM_SET_MBDN_DONE, 0, 0, nullptr);
1095         DiallingNumberUpdateInfor infor;
1096         infor.diallingNumber = diallingNumber;
1097         infor.fileId = ELEMENTARY_FILE_MBDN;
1098         infor.extFile = ELEMENTARY_FILE_EXT6;
1099         infor.index = indexOfMailbox_;
1100         diallingNumberHandler_->UpdateDiallingNumbers(infor, event);
1101         processWait_.wait(lock);
1102     } else if (CphsVoiceMailAvailable()) {
1103         std::unique_lock<std::mutex> lock(IccFile::mtx_);
1104         AppExecFwk::InnerEvent::Pointer event =
1105             CreateDiallingNumberPointer(MSG_SIM_SET_CPHS_MAILBOX_DONE, 0, 0, nullptr);
1106         DiallingNumberUpdateInfor infor;
1107         infor.diallingNumber = diallingNumber;
1108         infor.fileId = ELEMENTARY_FILE_MAILBOX_CPHS;
1109         infor.extFile = ELEMENTARY_FILE_EXT1;
1110         infor.index = 1;
1111         diallingNumberHandler_->UpdateDiallingNumbers(infor, event);
1112         processWait_.wait(lock);
1113     } else {
1114         TELEPHONY_LOGE("UpdateVoiceMail indexOfMailbox_ %{public}d is invalid!!", indexOfMailbox_);
1115     }
1116     TELEPHONY_LOGI("UpdateVoiceMail finished %{public}d", waitResult_);
1117     return waitResult_;
1118 }
1119 
CphsVoiceMailAvailable()1120 bool SimFile::CphsVoiceMailAvailable()
1121 {
1122     bool available = false;
1123     if (!cphsInfo_.empty()) {
1124         int dataLen = 0;
1125         std::shared_ptr<unsigned char> dataByte = SIMUtils::HexStringConvertToBytes(cphsInfo_, dataLen);
1126         available = (dataByte != nullptr) ? (dataByte.get()[1] & CPHS_VOICE_MAIL_MASK) ==
1127             CPHS_VOICE_MAIL_EXSIT : false;
1128     }
1129     return available;
1130 }
1131 
UnInit()1132 void SimFile::UnInit()
1133 {
1134     if (stateManager_ != nullptr) {
1135         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
1136         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
1137         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_SIMLOCK);
1138     }
1139     IccFile::UnInit();
1140 }
1141 } // namespace Telephony
1142 } // namespace OHOS
1143