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