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