• 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 "icc_file.h"
17 
18 #include "core_manager_inner.h"
19 #include "if_system_ability_manager.h"
20 #include "inner_event.h"
21 #include "iservice_registry.h"
22 #include "radio_event.h"
23 #include "system_ability_definition.h"
24 #include "telephony_ext_wrapper.h"
25 #include "tel_event_handler.h"
26 #include "telephony_state_registry_client.h"
27 
28 using namespace std;
29 using namespace OHOS::AppExecFwk;
30 using namespace OHOS::EventFwk;
31 
32 namespace OHOS {
33 namespace Telephony {
34 constexpr int32_t OPKEY_VMSG_LENTH = 3;
35 constexpr int32_t VMSG_SLOTID_INDEX = 0;
36 constexpr int32_t VMSG_OPKEY_INDEX = 1;
37 constexpr int32_t VMSG_OPNAME_INDEX = 2;
38 std::unique_ptr<ObserverHandler> IccFile::filesFetchedObser_ = nullptr;
39 constexpr const char *IS_UPDATE_OPERATORCONFIG = "telephony.is_update_operatorconfig";
40 constexpr const char *IS_BLOCK_LOAD_OPERATORCONFIG = "telephony.is_block_load_operatorconfig";
IccFile(const std::string & name,std::shared_ptr<SimStateManager> simStateManager)41 IccFile::IccFile(const std::string &name, std::shared_ptr<SimStateManager> simStateManager)
42     : TelEventHandler(name), stateManager_(simStateManager)
43 {
44     if (stateManager_ == nullptr) {
45         TELEPHONY_LOGE("IccFile::IccFile set NULL SIMStateManager!!");
46     }
47     if (filesFetchedObser_ == nullptr) {
48         filesFetchedObser_ = std::make_unique<ObserverHandler>();
49     }
50     if (filesFetchedObser_ == nullptr) {
51         TELEPHONY_LOGE("IccFile::IccFile filesFetchedObser_ create nullptr.");
52         return;
53     }
54     lockedFilesFetchedObser_ = std::make_unique<ObserverHandler>();
55     if (lockedFilesFetchedObser_ == nullptr) {
56         TELEPHONY_LOGE("IccFile::IccFile lockedFilesFetchedObser_ create nullptr.");
57         return;
58     }
59     networkLockedFilesFetchedObser_ = std::make_unique<ObserverHandler>();
60     if (networkLockedFilesFetchedObser_ == nullptr) {
61         TELEPHONY_LOGE("IccFile::IccFile networkLockedFilesFetchedObser_ create nullptr.");
62         return;
63     }
64     recordsEventsObser_ = std::make_unique<ObserverHandler>();
65     if (recordsEventsObser_ == nullptr) {
66         TELEPHONY_LOGE("IccFile::IccFile recordsEventsObser_ create nullptr.");
67         return;
68     }
69     networkSelectionModeAutomaticObser_ = std::make_unique<ObserverHandler>();
70     if (networkSelectionModeAutomaticObser_ == nullptr) {
71         TELEPHONY_LOGE("IccFile::IccFile networkSelectionModeAutomaticObser_ create nullptr.");
72         return;
73     }
74     spnUpdatedObser_ = std::make_unique<ObserverHandler>();
75     if (spnUpdatedObser_ == nullptr) {
76         TELEPHONY_LOGE("IccFile::IccFile spnUpdatedObser_ create nullptr.");
77         return;
78     }
79     AddRecordsOverrideObser();
80     TELEPHONY_LOGI("simmgr IccFile::IccFile finish");
81 }
82 
Init()83 void IccFile::Init()
84 {
85     if (stateManager_ != nullptr) {
86         stateManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
87         stateManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
88         stateManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_SIMLOCK);
89     }
90 }
91 
StartLoad()92 void IccFile::StartLoad()
93 {
94     TELEPHONY_LOGI("IccFile::StarLoad start");
95 }
96 
SetId(int id)97 void IccFile::SetId(int id)
98 {
99     slotId_ = id;
100     TELEPHONY_LOGI("IccFile::SetId, slotId %{public}d.", id);
101     voiceMailConfig_ = std::make_shared<VoiceMailConstants>(id);
102 }
103 
GetIsVoiceMailFixed()104 bool IccFile::GetIsVoiceMailFixed()
105 {
106     return isVoiceMailFixed_;
107 }
108 
SetVoiceMailByOperator(std::string spn)109 void IccFile::SetVoiceMailByOperator(std::string spn)
110 {
111     if (voiceMailConfig_ == nullptr) {
112         TELEPHONY_LOGE("IccFile::SetVoiceMailByOperator, voiceMailConfig_ is null.");
113         return;
114     }
115     if (voiceMailConfig_->ContainsCarrier(spn)) {
116         std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
117         isVoiceMailFixed_ = voiceMailConfig_->GetVoiceMailFixed(spn);
118         voiceMailNum_ = voiceMailConfig_->GetVoiceMailNumber(spn);
119         voiceMailTag_ = voiceMailConfig_->GetVoiceMailTag(spn);
120     } else {
121         TELEPHONY_LOGI("IccFile::SetVoiceMailByOperator, ContainsCarrier fail.");
122         std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
123         isVoiceMailFixed_ = false;
124     }
125 }
126 
ObtainIMSI()127 std::string IccFile::ObtainIMSI()
128 {
129     if (imsi_.empty()) {
130         TELEPHONY_LOGD("IccFile::ObtainIMSI is null:");
131     }
132     return imsi_;
133 }
134 
ObtainMCC()135 std::string IccFile::ObtainMCC()
136 {
137     if (imsi_.empty()) {
138         TELEPHONY_LOGI("IccFile::ObtainMCC is null:");
139     }
140     return mcc_;
141 }
142 
ObtainMNC()143 std::string IccFile::ObtainMNC()
144 {
145     if (imsi_.empty()) {
146         TELEPHONY_LOGI("IccFile::ObtainMNC is null:");
147     }
148     return mnc_;
149 }
150 
UpdateImsi(std::string imsi)151 void IccFile::UpdateImsi(std::string imsi)
152 {
153     imsi_ = imsi;
154 }
155 
UpdateIccId(std::string iccid)156 void IccFile::UpdateIccId(std::string iccid)
157 {
158     iccId_ = iccid;
159 }
160 
ObtainIccId()161 std::string IccFile::ObtainIccId()
162 {
163     return iccId_;
164 }
165 
ObtainDecIccId()166 std::string IccFile::ObtainDecIccId()
167 {
168     return decIccId_;
169 }
170 
ObtainGid1()171 std::string IccFile::ObtainGid1()
172 {
173     return gid1_;
174 }
175 
ObtainGid2()176 std::string IccFile::ObtainGid2()
177 {
178     return gid2_;
179 }
180 
ObtainMsisdnNumber()181 std::string IccFile::ObtainMsisdnNumber()
182 {
183     return msisdn_;
184 }
185 
LoadedOrNot()186 bool IccFile::LoadedOrNot()
187 {
188     return loaded_;
189 }
190 
UpdateLoaded(bool loaded)191 void IccFile::UpdateLoaded(bool loaded)
192 {
193     loaded_ = loaded;
194 }
195 
ObtainSimOperator()196 std::string IccFile::ObtainSimOperator()
197 {
198     return operatorNumeric_;
199 }
200 
ObtainIsoCountryCode()201 std::string IccFile::ObtainIsoCountryCode()
202 {
203     return "";
204 }
205 
ObtainCallForwardStatus()206 int IccFile::ObtainCallForwardStatus()
207 {
208     return ICC_CALL_FORWARD_TYPE_UNKNOWN;
209 }
210 
UpdateMsisdnNumber(const std::string & alphaTag,const std::string & number)211 bool IccFile::UpdateMsisdnNumber(const std::string &alphaTag, const std::string &number)
212 {
213     return false;
214 }
215 
ObtainFilesFetched()216 bool IccFile::ObtainFilesFetched()
217 {
218     return (fileToGet_ == 0) && fileQueried_;
219 }
220 
LockQueriedOrNot()221 bool IccFile::LockQueriedOrNot()
222 {
223     return (fileToGet_ == 0) && lockQueried_;
224 }
225 
ObtainDiallingNumberInfo()226 std::string IccFile::ObtainDiallingNumberInfo()
227 {
228     return "";
229 }
230 
ObtainNAI()231 std::string IccFile::ObtainNAI()
232 {
233     return "";
234 }
235 
ObtainHomeNameOfPnn()236 std::string IccFile::ObtainHomeNameOfPnn()
237 {
238     return pnnHomeName_;
239 }
240 
ObtainMsisdnAlphaStatus()241 std::string IccFile::ObtainMsisdnAlphaStatus()
242 {
243     return msisdnTag_;
244 }
245 
ObtainVoiceMailCount()246 int32_t IccFile::ObtainVoiceMailCount()
247 {
248     return voiceMailCount_;
249 }
250 
ObtainSPN()251 std::string IccFile::ObtainSPN()
252 {
253     return spn_;
254 }
255 
ObtainEons(const std::string & plmn,int32_t lac,bool longNameRequired)256 std::string IccFile::ObtainEons(const std::string &plmn, int32_t lac, bool longNameRequired)
257 {
258     std::vector<std::shared_ptr<OperatorPlmnInfo>> oplFiles = oplFiles_;
259     sptr<NetworkState> networkState = nullptr;
260     CoreManagerInner::GetInstance().GetNetworkStatus(slotId_, networkState);
261     if (networkState != nullptr && !(opl5gFiles_.empty())) {
262         NrState nrState = networkState->GetNrState();
263         if (nrState == NrState::NR_NSA_STATE_SA_ATTACHED) {
264             oplFiles = opl5gFiles_;
265         }
266     }
267     bool roaming = (plmn.compare(operatorNumeric_) == 0 ? false : true);
268     TELEPHONY_LOGI("ObtainEons roaming:%{public}d", roaming);
269     if (plmn.empty() || pnnFiles_.empty() || (oplFiles.empty() && roaming)) {
270         TELEPHONY_LOGE("ObtainEons is empty");
271         return "";
272     }
273     int pnnIndex = 1;
274     for (std::shared_ptr<OperatorPlmnInfo> opl : oplFiles) {
275         if (opl == nullptr) {
276             continue;
277         }
278         pnnIndex = -1;
279         TELEPHONY_LOGD("ObtainEons plmn:%{public}s, opl->plmnNumeric:%{public}s, lac:%{public}d, "
280                        "opl->lacStart:%{public}d, opl->lacEnd:%{public}d, opl->pnnRecordId:%{public}d",
281             plmn.c_str(), opl->plmnNumeric.c_str(), lac, opl->lacStart, opl->lacEnd, opl->pnnRecordId);
282         if (plmn.compare(opl->plmnNumeric) == 0 &&
283             ((opl->lacStart == 0 && opl->lacEnd == 0xfffe) || (opl->lacStart <= lac && opl->lacEnd >= lac))) {
284             pnnIndex = opl->pnnRecordId;
285             break;
286         }
287     }
288     std::string eons = "";
289     if (pnnIndex >= 1 && pnnIndex <= static_cast<int>(pnnFiles_.size())) {
290         TELEPHONY_LOGI("ObtainEons longNameRequired:%{public}d, longName:%{public}s, shortName:%{public}s,",
291             longNameRequired, pnnFiles_.at(pnnIndex - 1)->longName.c_str(),
292             pnnFiles_.at(pnnIndex - 1)->shortName.c_str());
293         if (longNameRequired) {
294             eons = pnnFiles_.at(pnnIndex - 1)->longName;
295         } else {
296             eons = pnnFiles_.at(pnnIndex - 1)->shortName;
297         }
298     }
299     return eons;
300 }
301 
ObtainVoiceMailInfo()302 std::string IccFile::ObtainVoiceMailInfo()
303 {
304     return voiceMailTag_;
305 }
306 
ObtainIccLanguage()307 std::string IccFile::ObtainIccLanguage()
308 {
309     return iccLanguage_;
310 }
311 
ObtainUsimFunctionHandle()312 std::shared_ptr<UsimFunctionHandle> IccFile::ObtainUsimFunctionHandle()
313 {
314     return std::make_shared<UsimFunctionHandle>(nullptr, 0);
315 }
316 
ObtainSpNameFromEfSpn()317 std::string IccFile::ObtainSpNameFromEfSpn()
318 {
319     return "";
320 }
321 
ObtainLengthOfMnc()322 int IccFile::ObtainLengthOfMnc()
323 {
324     return lengthOfMnc_;
325 }
326 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)327 void IccFile::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
328 {
329     if (event == nullptr) {
330         TELEPHONY_LOGE("event is nullptr!");
331         return;
332     }
333     auto id = event->GetInnerEventId();
334     bool result = false;
335     TELEPHONY_LOGD("IccFile::ProcessEvent id %{public}d", id);
336     switch (id) {
337         case MSG_SIM_OBTAIN_ICC_FILE_DONE:
338             result = ProcessIccFileObtained(event);
339             ProcessFileLoaded(result);
340             break;
341         case MSG_ICC_REFRESH:
342             ProcessIccRefresh(MSG_ID_DEFAULT);
343             break;
344         default:
345             break;
346     }
347 }
348 
LoadVoiceMail()349 void IccFile::LoadVoiceMail()
350 {
351     if (voiceMailConfig_ == nullptr) {
352         TELEPHONY_LOGE("IccFile::LoadVoiceMail, voiceMailConfig_ is null.");
353         return;
354     }
355     voiceMailConfig_->ResetVoiceMailLoadedFlag();
356     std::string operatorNumeric = ObtainSimOperator();
357     SetVoiceMailByOperator(operatorNumeric);
358 }
359 
RegisterImsiLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)360 void IccFile::RegisterImsiLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
361 {
362     int eventCode = RadioEvent::RADIO_IMSI_LOADED_READY;
363     if (filesFetchedObser_ != nullptr) {
364         filesFetchedObser_->RegObserver(eventCode, eventHandler);
365     }
366     if (!ObtainIMSI().empty()) {
367         if (eventHandler != nullptr) {
368             TelEventHandler::SendTelEvent(eventHandler, RadioEvent::RADIO_IMSI_LOADED_READY);
369         }
370     }
371 }
372 
UnregisterImsiLoaded(const std::shared_ptr<AppExecFwk::EventHandler> & handler)373 void IccFile::UnregisterImsiLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
374 {
375     if (filesFetchedObser_ != nullptr) {
376         filesFetchedObser_->Remove(RadioEvent::RADIO_IMSI_LOADED_READY, handler);
377     }
378 }
379 
RegisterAllFilesLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)380 void IccFile::RegisterAllFilesLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
381 {
382     int eventCode = RadioEvent::RADIO_SIM_RECORDS_LOADED;
383     if (filesFetchedObser_ != nullptr) {
384         filesFetchedObser_->RegObserver(eventCode, eventHandler);
385         TELEPHONY_LOGD("IccFile::RegisterAllFilesLoaded: registerd, slotId:%{public}d", slotId_);
386     }
387     if (ObtainFilesFetched()) {
388         TELEPHONY_LOGI("IccFile::RegisterAllFilesLoaded: notify, slotId:%{public}d", slotId_);
389         if (eventHandler != nullptr) {
390             TelEventHandler::SendTelEvent(eventHandler, RadioEvent::RADIO_SIM_RECORDS_LOADED, slotId_, 0);
391         }
392         PublishSimFileEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED,
393             static_cast<int32_t>(SimState::SIM_STATE_LOADED), "");
394     }
395 }
396 
UnregisterAllFilesLoaded(const std::shared_ptr<AppExecFwk::EventHandler> & handler)397 void IccFile::UnregisterAllFilesLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
398 {
399     if (filesFetchedObser_ != nullptr) {
400         filesFetchedObser_->Remove(RadioEvent::RADIO_SIM_RECORDS_LOADED, handler);
401     }
402 }
403 
RegisterOpkeyLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)404 void IccFile::RegisterOpkeyLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
405 {
406     int eventCode = RadioEvent::RADIO_SIM_OPKEY_LOADED;
407     if (filesFetchedObser_ != nullptr) {
408         filesFetchedObser_->RegObserver(eventCode, eventHandler);
409     }
410     TELEPHONY_LOGD("IccFile::RegisterOpkeyLoaded: registered");
411 }
412 
RegisterOperatorCacheDel(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)413 void IccFile::RegisterOperatorCacheDel(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
414 {
415     int eventCode = RadioEvent::RADIO_OPERATOR_CACHE_DELETE;
416     if (filesFetchedObser_ != nullptr) {
417         filesFetchedObser_->RegObserver(eventCode, eventHandler);
418     }
419     TELEPHONY_LOGD("IccFile::RegisterOperatorCacheDel: registered");
420 }
421 
RegisterIccidLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)422 void IccFile::RegisterIccidLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
423 {
424     int eventCode = RadioEvent::RADIO_QUERY_ICCID_DONE;
425     if (filesFetchedObser_ != nullptr) {
426         filesFetchedObser_->RegObserver(eventCode, eventHandler);
427         TELEPHONY_LOGI("IccFile::RegisterIccidLoaded: registered, slotId:%{public}d", slotId_);
428     }
429     if (!iccId_.empty()) {
430         TELEPHONY_LOGI("IccFile::RegisterIccidLoaded: notify, slotId:%{public}d", slotId_);
431         if (eventHandler != nullptr) {
432             TelEventHandler::SendTelEvent(eventHandler, RadioEvent::RADIO_QUERY_ICCID_DONE, slotId_, 0);
433         }
434     }
435 }
436 
RegisterOperatorConfigUpdate(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)437 void IccFile::RegisterOperatorConfigUpdate(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
438 {
439     int eventCode = RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE;
440     if (filesFetchedObser_ != nullptr) {
441         filesFetchedObser_->RegObserver(eventCode, eventHandler);
442     }
443     TELEPHONY_LOGD("IccFile::RegisterOperatorConfigUpdate: registered");
444 }
445 
UnregisterOpkeyLoaded(const std::shared_ptr<AppExecFwk::EventHandler> & handler)446 void IccFile::UnregisterOpkeyLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
447 {
448     if (filesFetchedObser_ != nullptr) {
449         filesFetchedObser_->Remove(RadioEvent::RADIO_SIM_OPKEY_LOADED, handler);
450     }
451 }
452 
UnregisterOperatorCacheDel(const std::shared_ptr<AppExecFwk::EventHandler> & handler)453 void IccFile::UnregisterOperatorCacheDel(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
454 {
455     if (filesFetchedObser_ != nullptr) {
456         filesFetchedObser_->Remove(RadioEvent::RADIO_OPERATOR_CACHE_DELETE, handler);
457     }
458 }
459 
UnregisterIccidLoaded(const std::shared_ptr<AppExecFwk::EventHandler> & handler)460 void IccFile::UnregisterIccidLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
461 {
462     if (filesFetchedObser_ != nullptr) {
463         filesFetchedObser_->Remove(RadioEvent::RADIO_QUERY_ICCID_DONE, handler);
464     }
465 }
466 
UnregisterOperatorConfigUpdate(const std::shared_ptr<AppExecFwk::EventHandler> & handler)467 void IccFile::UnregisterOperatorConfigUpdate(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
468 {
469     if (filesFetchedObser_ != nullptr) {
470         filesFetchedObser_->Remove(RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE, handler);
471     }
472 }
473 
RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)474 void IccFile::RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
475 {
476     switch (what) {
477         case RadioEvent::RADIO_SIM_RECORDS_LOADED:
478             RegisterAllFilesLoaded(handler);
479             break;
480         case RadioEvent::RADIO_IMSI_LOADED_READY:
481             RegisterImsiLoaded(handler);
482             break;
483         case RadioEvent::RADIO_SIM_OPKEY_LOADED:
484             RegisterOpkeyLoaded(handler);
485             break;
486         case RadioEvent::RADIO_OPERATOR_CACHE_DELETE:
487             RegisterOperatorCacheDel(handler);
488             break;
489         case RadioEvent::RADIO_QUERY_ICCID_DONE:
490             RegisterIccidLoaded(handler);
491             break;
492         case RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE:
493             RegisterOperatorConfigUpdate(handler);
494             break;
495         default:
496             TELEPHONY_LOGI("RegisterCoreNotify default");
497     }
498 }
499 
UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)500 void IccFile::UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
501 {
502     switch (what) {
503         case RadioEvent::RADIO_SIM_RECORDS_LOADED:
504             UnregisterAllFilesLoaded(handler);
505             break;
506         case RadioEvent::RADIO_IMSI_LOADED_READY:
507             UnregisterImsiLoaded(handler);
508             break;
509         case RadioEvent::RADIO_SIM_OPKEY_LOADED:
510             UnregisterOpkeyLoaded(handler);
511             break;
512         case RadioEvent::RADIO_OPERATOR_CACHE_DELETE:
513             UnregisterOperatorCacheDel(handler);
514             break;
515         case RadioEvent::RADIO_QUERY_ICCID_DONE:
516             UnregisterIccidLoaded(handler);
517             break;
518         case RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE:
519             UnregisterOperatorConfigUpdate(handler);
520             break;
521         default:
522             TELEPHONY_LOGI("UnregisterCoreNotify default");
523     }
524 }
525 
UpdateSPN(const std::string spn)526 void IccFile::UpdateSPN(const std::string spn)
527 {
528     if (spn_ != spn) {
529         spnUpdatedObser_->NotifyObserver(MSG_SIM_SPN_UPDATED);
530         spn_ = spn;
531     }
532 }
533 
BuildCallerInfo(int eventId)534 AppExecFwk::InnerEvent::Pointer IccFile::BuildCallerInfo(int eventId)
535 {
536     std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
537     int eventParam = 0;
538     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
539     if (event == nullptr) {
540         TELEPHONY_LOGE("event is nullptr!");
541         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
542     }
543     event->SetOwner(shared_from_this());
544     return event;
545 }
546 
BuildCallerInfo(int eventId,int arg1,int arg2)547 AppExecFwk::InnerEvent::Pointer IccFile::BuildCallerInfo(int eventId, int arg1, int arg2)
548 {
549     std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
550     object->arg1 = arg1;
551     object->arg2 = arg2;
552     int eventParam = 0;
553     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
554     if (event == nullptr) {
555         TELEPHONY_LOGE("event is nullptr!");
556         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
557     }
558     event->SetOwner(shared_from_this());
559     return event;
560 }
561 
BuildCallerInfo(int eventId,std::shared_ptr<void> loader)562 AppExecFwk::InnerEvent::Pointer IccFile::BuildCallerInfo(int eventId, std::shared_ptr<void> loader)
563 {
564     std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
565     object->iccLoader = loader;
566     int eventParam = 0;
567     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
568     if (event == nullptr) {
569         TELEPHONY_LOGE("event is nullptr!");
570         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
571     }
572     event->SetOwner(shared_from_this());
573     return event;
574 }
575 
PublishSimFileEvent(const std::string & event,int eventCode,const std::string & eventData)576 bool IccFile::PublishSimFileEvent(const std::string &event, int eventCode, const std::string &eventData)
577 {
578     Want want;
579     want.SetAction(event);
580     CommonEventData data;
581     data.SetWant(want);
582     data.SetCode(eventCode);
583     data.SetData(eventData);
584     CommonEventPublishInfo publishInfo;
585     publishInfo.SetOrdered(false);
586     bool publishResult = CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
587     TELEPHONY_LOGI("IccFile::PublishSimEvent result : %{public}d", publishResult);
588     return publishResult;
589 }
590 
ProcessIccFileObtained(const AppExecFwk::InnerEvent::Pointer & event)591 bool IccFile::ProcessIccFileObtained(const AppExecFwk::InnerEvent::Pointer &event)
592 {
593     bool isFileProcessResponse = true;
594     std::shared_ptr<ControllerToFileMsg> fd = event->GetSharedObject<ControllerToFileMsg>();
595     if (fd == nullptr) {
596         TELEPHONY_LOGE("fd is nullptr!");
597         return isFileProcessResponse;
598     }
599     std::shared_ptr<void> baseLoad = fd->iccLoader;
600     if (baseLoad != nullptr) {
601         std::shared_ptr<IccFileLoaded> destLoad = std::static_pointer_cast<IccFileLoaded>(baseLoad);
602         destLoad->ProcessParseFile(event);
603         TELEPHONY_LOGI("ProcessIccFileObtained item %{public}s", destLoad->ObtainElementaryFileName().c_str());
604     } else {
605         isFileProcessResponse = false;
606         TELEPHONY_LOGE("IccFile::ProcessIccFileObtained null base pointer");
607     }
608     return isFileProcessResponse;
609 }
610 
UpdateIccLanguage(const std::string & langLi,const std::string & langPl)611 void IccFile::UpdateIccLanguage(const std::string &langLi, const std::string &langPl)
612 {
613     iccLanguage_ = ObtainValidLanguage(langLi);
614     if (iccLanguage_.empty()) {
615         iccLanguage_ = ObtainValidLanguage(langPl);
616     }
617     TELEPHONY_LOGI("IccFile::UpdateIccLanguage end is %{public}s", iccLanguage_.c_str());
618 }
619 
ObtainValidLanguage(const std::string & langData)620 std::string IccFile::ObtainValidLanguage(const std::string &langData)
621 {
622     if (langData.empty()) {
623         TELEPHONY_LOGE("langData null data!!");
624         return "";
625     }
626     int langDataLen = 0;
627     std::shared_ptr<unsigned char> ucc = SIMUtils::HexStringConvertToBytes(langData, langDataLen);
628     if (ucc == nullptr) {
629         TELEPHONY_LOGE("ucc is nullptr!!");
630         return "";
631     }
632     unsigned char *data = ucc.get();
633 
634     if (data == nullptr) {
635         TELEPHONY_LOGE("data is nullptr!!");
636         return "";
637     }
638 
639     int dataLen = static_cast<int>(strlen(reinterpret_cast<char *>(data)));
640     TELEPHONY_LOGI("ObtainValidLanguage all is %{public}s---%{public}d, dataLen:%{public}d",
641         data, langDataLen, dataLen);
642     if (langDataLen > dataLen) {
643         langDataLen = dataLen;
644     }
645     for (int i = 0; (i + 1) < langDataLen; i += DATA_STEP) {
646         std::string langName((char *)data, i, DATA_STEP);
647         TELEPHONY_LOGI("ObtainValidLanguage item is %{public}d--%{public}s", i, langName.c_str());
648         if (!langName.empty()) {
649             return langName;
650         }
651     }
652     return "";
653 }
654 
SwapPairsForIccId(std::string & iccId)655 void IccFile::SwapPairsForIccId(std::string &iccId)
656 {
657     if (iccId.empty() || iccId.length() < LENGTH_TWO) {
658         return;
659     }
660     std::string result = "";
661     for (size_t i = 0; i < iccId.length() - 1; i += DATA_STEP) {
662         if (iccId[i + 1] > '9') {
663             break;
664         }
665         result += iccId[i + 1];
666         if (iccId[i] == 'F') {
667             continue;
668         }
669         if (iccId[i] > '9') {
670             break;
671         }
672         result += iccId[i];
673     }
674     iccId = result;
675 }
676 
GetFullIccid(std::string & iccId)677 void IccFile::GetFullIccid(std::string &iccId)
678 {
679     if (iccId.empty() || iccId.length() < LENGTH_TWO) {
680         return;
681     }
682     std::string result = "";
683     for (size_t i = 0; i < iccId.length() - 1; i += DATA_STEP) {
684         result += iccId[i + 1];
685         result += iccId[i];
686     }
687     iccId = result;
688 }
689 
~IccFile()690 IccFile::~IccFile() {}
691 
SetRilAndFileController(const std::shared_ptr<Telephony::ITelRilManager> & ril,const std::shared_ptr<IccFileController> & file,const std::shared_ptr<IccDiallingNumbersHandler> & handler)692 void IccFile::SetRilAndFileController(const std::shared_ptr<Telephony::ITelRilManager> &ril,
693     const std::shared_ptr<IccFileController> &file, const std::shared_ptr<IccDiallingNumbersHandler> &handler)
694 {
695     telRilManager_ = ril;
696     if (telRilManager_ == nullptr) {
697         TELEPHONY_LOGE("IccFile set NULL TelRilManager!!");
698     }
699 
700     fileController_ = file;
701     if (fileController_ == nullptr) {
702         TELEPHONY_LOGE("IccFile set NULL File Controller!!");
703     }
704     diallingNumberHandler_ = handler;
705     if (fileController_ == nullptr) {
706         TELEPHONY_LOGE("IccFile set NULL File Controller!!");
707     }
708 }
709 
CreateDiallingNumberPointer(int eventid,int efId,int index,std::shared_ptr<void> pobj)710 AppExecFwk::InnerEvent::Pointer IccFile::CreateDiallingNumberPointer(
711     int eventid, int efId, int index, std::shared_ptr<void> pobj)
712 {
713     std::unique_ptr<DiallingNumbersHandleHolder> holder = std::make_unique<DiallingNumbersHandleHolder>();
714     holder->fileID = efId;
715     holder->index = index;
716     holder->diallingNumber = pobj;
717     int eventParam = 0;
718     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventid, holder, eventParam);
719     if (event == nullptr) {
720         TELEPHONY_LOGE("event is nullptr!");
721         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
722     }
723     event->SetOwner(shared_from_this());
724     return event;
725 }
726 
727 
NotifyRegistrySimState(CardType type,SimState state,LockReason reason)728 void IccFile::NotifyRegistrySimState(CardType type, SimState state, LockReason reason)
729 {
730     if (stateManager_ != nullptr) {
731         stateManager_->SetSimState(state);
732     }
733     int32_t result =
734         DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateSimState(slotId_, type, state, reason);
735     TELEPHONY_LOGI("NotifyRegistrySimState slotId: %{public}d, simState: %{public}d, ret: %{public}d", slotId_, state,
736         result);
737 }
738 
HasSimCard()739 bool IccFile::HasSimCard()
740 {
741     return (stateManager_ != nullptr) ? stateManager_->HasSimCard() : false;
742 }
743 
ResetVoiceMailVariable()744 void IccFile::ResetVoiceMailVariable()
745 {
746     std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
747     isVoiceMailFixed_ = false;
748     voiceMailNum_ = "";
749     voiceMailTag_ = "";
750     if (TELEPHONY_EXT_WRAPPER.resetVoiceMailManagerExt_ != nullptr) {
751         TELEPHONY_EXT_WRAPPER.resetVoiceMailManagerExt_(slotId_);
752     }
753 }
754 
ClearData()755 void IccFile::ClearData()
756 {
757     TELEPHONY_LOGI("IccFile ClearData");
758     imsi_ = "";
759     iccId_ = "";
760     decIccId_ = "";
761     UpdateSPN("");
762     UpdateLoaded(false);
763     operatorNumeric_ = "";
764     mcc_ = "";
765     mnc_ = "";
766     lengthOfMnc_ = UNINITIALIZED_MNC;
767     indexOfMailbox_ = 1;
768     msisdn_ = "";
769     gid1_ = "";
770     gid2_ = "";
771     msisdnTag_ = "";
772     fileQueried_ = false;
773     pnnFiles_.clear();
774     oplFiles_.clear();
775     opl5gFiles_.clear();
776     ResetVoiceMailVariable();
777     auto iccFileExt = iccFile_.lock();
778     if (TELEPHONY_EXT_WRAPPER.createIccFileExt_ != nullptr && iccFileExt) {
779         iccFileExt->ClearData();
780     }
781 }
782 
ProcessIccLocked()783 void IccFile::ProcessIccLocked()
784 {
785     TELEPHONY_LOGI("IccFile ProcessIccLocked");
786     fileQueried_ = false;
787     UpdateLoaded(false);
788 }
789 
UnInit()790 void IccFile::UnInit()
791 {
792     if (stateManager_ != nullptr) {
793         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_READY);
794         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_LOCKED);
795         stateManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_SIMLOCK);
796     }
797     ClearData();
798 }
799 
SaveCountryCode()800 void IccFile::SaveCountryCode()
801 {
802     std::string countryCode = ObtainIsoCountryCode();
803     std::string key = COUNTRY_CODE_KEY + std::to_string(slotId_);
804     SetParameter(key.c_str(), countryCode.c_str());
805 }
806 
ProcessExtGetFileResponse()807 void IccFile::ProcessExtGetFileResponse()
808 {
809     bool response = true;
810     ProcessFileLoaded(response);
811 }
812 
ProcessExtGetFileDone(const AppExecFwk::InnerEvent::Pointer & event)813 void IccFile::ProcessExtGetFileDone(const AppExecFwk::InnerEvent::Pointer &event)
814 {
815     ProcessEvent(event);
816 }
817 
SetIccFile(std::shared_ptr<OHOS::Telephony::IIccFileExt> & iccFileExt)818 void IccFile::SetIccFile(std::shared_ptr<OHOS::Telephony::IIccFileExt>& iccFileExt)
819 {
820     iccFile_ = iccFileExt;
821 }
822 
OnOpkeyLoad(const std::string opkey,const std::string opName)823 void IccFile::OnOpkeyLoad(const std::string opkey, const std::string opName)
824 {
825     TELEPHONY_LOGI("OnOpkeyLoad slotId: %{public}d opkey: %{public}s opName: %{public}s",
826         slotId_, opkey.data(), opName.data());
827     if (filesFetchedObser_ != nullptr) {
828         std::vector<std::string> vMsg(OPKEY_VMSG_LENTH, "");
829         vMsg[VMSG_SLOTID_INDEX] = std::to_string(slotId_);
830         vMsg[VMSG_OPKEY_INDEX] = opkey;
831         vMsg[VMSG_OPNAME_INDEX] = opName;
832         auto obj = std::make_shared<std::vector<std::string>>(vMsg);
833         filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_SIM_OPKEY_LOADED, obj);
834     }
835 }
836 
ExecutOriginalSimIoRequest(int32_t fileId,int fileIdDone)837 bool IccFile::ExecutOriginalSimIoRequest(int32_t fileId, int fileIdDone)
838 {
839     TELEPHONY_LOGD("ExecutOriginalSimIoRequest simfile: %{public}x doneId: %{public}x", fileId, fileIdDone);
840     AppExecFwk::InnerEvent::Pointer event = BuildCallerInfo(fileIdDone);
841     fileController_->ObtainBinaryFile(fileId, event);
842     return true;
843 }
844 
AddRecordsOverrideObser()845 void IccFile::AddRecordsOverrideObser()
846 {
847     recordsOverrideObser_ = std::make_unique<ObserverHandler>();
848     if (recordsOverrideObser_ == nullptr) {
849         TELEPHONY_LOGE("IccFile::IccFile recordsOverrideObser_ create nullptr.");
850         return;
851     }
852 }
853 
FileChangeToExt(const std::string fileName,const FileChangeType fileLoad)854 void IccFile::FileChangeToExt(const std::string fileName, const FileChangeType fileLoad)
855 {
856     auto iccFileExt = iccFile_.lock();
857     if (TELEPHONY_EXT_WRAPPER.createIccFileExt_ != nullptr && iccFileExt) {
858         iccFileExt->FileChange(fileName, fileLoad);
859     }
860 }
861 
AddRecordsToLoadNum()862 void IccFile::AddRecordsToLoadNum()
863 {
864     fileToGet_++;
865 }
866 
DeleteOperatorCache()867 void IccFile::DeleteOperatorCache()
868 {
869     if (filesFetchedObser_  != nullptr) {
870         filesFetchedObser_ ->NotifyObserver(RadioEvent::RADIO_OPERATOR_CACHE_DELETE, slotId_);
871     }
872 }
873 
UpdateOpkeyConfig()874 void IccFile::UpdateOpkeyConfig()
875 {
876     if (filesFetchedObser_ == nullptr) {
877         TELEPHONY_LOGE("operatorConfigUpdateObser nullptr.");
878         return;
879     }
880     if (ObtainFilesFetched()) {
881         filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE, slotId_);
882     } else {
883         std::string key = "";
884         SetParameter(key.append(IS_UPDATE_OPERATORCONFIG).append(std::to_string(slotId_)).c_str(), "true");
885         SetParameter(IS_BLOCK_LOAD_OPERATORCONFIG, "false");
886         CoreManagerInner::GetInstance().ResetDataShareError();
887     }
888 }
889 } // namespace Telephony
890 } // namespace OHOS
891