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 #include "if_system_ability_manager.h"
18 #include "inner_event.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "telephony_state_registry_client.h"
22 #include "radio_event.h"
23
24 using namespace std;
25 using namespace OHOS::AppExecFwk;
26 using namespace OHOS::EventFwk;
27
28 namespace OHOS {
29 namespace Telephony {
30 std::unique_ptr<ObserverHandler> IccFile::filesFetchedObser_ = nullptr;
IccFile(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::shared_ptr<SimStateManager> simStateManager)31 IccFile::IccFile(
32 const std::shared_ptr<AppExecFwk::EventRunner> &runner, std::shared_ptr<SimStateManager> simStateManager)
33 : AppExecFwk::EventHandler(runner), stateManager_(simStateManager)
34 {
35 if (stateManager_ == nullptr) {
36 TELEPHONY_LOGE("IccFile::IccFile set NULL SIMStateManager!!");
37 }
38 if (filesFetchedObser_ == nullptr) {
39 filesFetchedObser_ = std::make_unique<ObserverHandler>();
40 }
41 if (filesFetchedObser_ == nullptr) {
42 TELEPHONY_LOGE("IccFile::IccFile filesFetchedObser_ create nullptr.");
43 return;
44 }
45
46 lockedFilesFetchedObser_ = std::make_unique<ObserverHandler>();
47 if (lockedFilesFetchedObser_ == nullptr) {
48 TELEPHONY_LOGE("IccFile::IccFile lockedFilesFetchedObser_ create nullptr.");
49 return;
50 }
51 networkLockedFilesFetchedObser_ = std::make_unique<ObserverHandler>();
52 if (networkLockedFilesFetchedObser_ == nullptr) {
53 TELEPHONY_LOGE("IccFile::IccFile networkLockedFilesFetchedObser_ create nullptr.");
54 return;
55 }
56 imsiReadyObser_ = std::make_unique<ObserverHandler>();
57 if (imsiReadyObser_ == nullptr) {
58 TELEPHONY_LOGE("IccFile::IccFile imsiReadyObser_ create nullptr.");
59 return;
60 }
61 recordsEventsObser_ = std::make_unique<ObserverHandler>();
62 if (recordsEventsObser_ == nullptr) {
63 TELEPHONY_LOGE("IccFile::IccFile recordsEventsObser_ create nullptr.");
64 return;
65 }
66 networkSelectionModeAutomaticObser_ = std::make_unique<ObserverHandler>();
67 if (networkSelectionModeAutomaticObser_ == nullptr) {
68 TELEPHONY_LOGE("IccFile::IccFile networkSelectionModeAutomaticObser_ create nullptr.");
69 return;
70 }
71 spnUpdatedObser_ = std::make_unique<ObserverHandler>();
72 if (spnUpdatedObser_ == nullptr) {
73 TELEPHONY_LOGE("IccFile::IccFile spnUpdatedObser_ create nullptr.");
74 return;
75 }
76 recordsOverrideObser_ = std::make_unique<ObserverHandler>();
77 if (recordsOverrideObser_ == nullptr) {
78 TELEPHONY_LOGE("IccFile::IccFile recordsOverrideObser_ create nullptr.");
79 return;
80 }
81 TELEPHONY_LOGI("simmgr IccFile::IccFile finish");
82 }
83
Init()84 void IccFile::Init() {}
85
StartLoad()86 void IccFile::StartLoad()
87 {
88 TELEPHONY_LOGI("simmgr IccFile::StarLoad() start");
89 }
90
ObtainIMSI()91 std::string IccFile::ObtainIMSI()
92 {
93 if (imsi_.empty()) {
94 TELEPHONY_LOGI("IccFile::ObtainIMSI is null:");
95 }
96 return imsi_;
97 }
98
UpdateImsi(std::string imsi)99 void IccFile::UpdateImsi(std::string imsi)
100 {
101 imsi_ = imsi;
102 }
103
ObtainIccId()104 std::string IccFile::ObtainIccId()
105 {
106 return iccId_;
107 }
108
ObtainGid1()109 std::string IccFile::ObtainGid1()
110 {
111 return gid1_;
112 }
113
ObtainGid2()114 std::string IccFile::ObtainGid2()
115 {
116 return gid2_;
117 }
118
ObtainMsisdnNumber()119 std::string IccFile::ObtainMsisdnNumber()
120 {
121 return msisdn_;
122 }
123
LoadedOrNot()124 bool IccFile::LoadedOrNot()
125 {
126 return loaded_;
127 }
128
UpdateLoaded(bool loaded)129 void IccFile::UpdateLoaded(bool loaded)
130 {
131 loaded_ = loaded;
132 }
133
ObtainSimOperator()134 std::string IccFile::ObtainSimOperator()
135 {
136 return operatorNumeric_;
137 }
138
ObtainIsoCountryCode()139 std::string IccFile::ObtainIsoCountryCode()
140 {
141 return "";
142 }
143
ObtainCallForwardStatus()144 int IccFile::ObtainCallForwardStatus()
145 {
146 return ICC_CALL_FORWARD_TYPE_UNKNOWN;
147 }
148
UpdateMsisdnNumber(const std::string & alphaTag,const std::string & number,const AppExecFwk::InnerEvent::Pointer & onComplete)149 void IccFile::UpdateMsisdnNumber(
150 const std::string &alphaTag, const std::string &number, const AppExecFwk::InnerEvent::Pointer &onComplete)
151 {}
152
ObtainFilesFetched()153 bool IccFile::ObtainFilesFetched()
154 {
155 return (fileToGet_ == 0) && fileQueried_;
156 }
157
LockQueriedOrNot()158 bool IccFile::LockQueriedOrNot()
159 {
160 return (fileToGet_ == 0) && lockQueried_;
161 }
162
ObtainDiallingNumberInfo()163 std::string IccFile::ObtainDiallingNumberInfo()
164 {
165 return "";
166 }
167
ObtainNAI()168 std::string IccFile::ObtainNAI()
169 {
170 return "";
171 }
172
ObtainHomeNameOfPnn()173 std::string IccFile::ObtainHomeNameOfPnn()
174 {
175 return pnnHomeName_;
176 }
177
ObtainMsisdnAlphaStatus()178 std::string IccFile::ObtainMsisdnAlphaStatus()
179 {
180 return msisdnTag_;
181 }
182
ObtainVoiceMailNumber()183 std::string IccFile::ObtainVoiceMailNumber()
184 {
185 return voiceMailNum_;
186 }
187
ObtainSPN()188 std::string IccFile::ObtainSPN()
189 {
190 return spn_;
191 }
192
ObtainVoiceMailInfo()193 std::string IccFile::ObtainVoiceMailInfo()
194 {
195 return voiceMailTag_;
196 }
197
ObtainIccLanguage()198 std::string IccFile::ObtainIccLanguage()
199 {
200 return iccLanguage_;
201 }
202
ObtainUsimFunctionHandle()203 std::shared_ptr<UsimFunctionHandle> IccFile::ObtainUsimFunctionHandle()
204 {
205 return std::make_shared<UsimFunctionHandle>(nullptr, 0);
206 }
207
ObtainSpNameFromEfSpn()208 std::string IccFile::ObtainSpNameFromEfSpn()
209 {
210 return "";
211 }
212
ObtainLengthOfMnc()213 int IccFile::ObtainLengthOfMnc()
214 {
215 return lengthOfMnc_;
216 }
217
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)218 void IccFile::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
219 {
220 auto id = event->GetInnerEventId();
221 bool result = false;
222 TELEPHONY_LOGI("IccFile::ProcessEvent id %{public}d", id);
223 switch (id) {
224 case MSG_SIM_OBTAIN_ICC_FILE_DONE:
225 result = ProcessIccFileObtained(event);
226 ProcessFileLoaded(result);
227 break;
228 case MSG_ICC_REFRESH:
229 ProcessIccRefresh(MSG_ID_DEFAULT);
230 break;
231 default:
232 break;
233 }
234 }
235
RegisterImsiLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)236 void IccFile::RegisterImsiLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
237 {
238 int eventCode = RadioEvent::RADIO_IMSI_LOADED_READY;
239 if (imsiReadyObser_ != nullptr) {
240 imsiReadyObser_->RegObserver(eventCode, eventHandler);
241 }
242 if (!ObtainIMSI().empty()) {
243 if (imsiReadyObser_ != nullptr) {
244 imsiReadyObser_->NotifyObserver(RadioEvent::RADIO_IMSI_LOADED_READY);
245 }
246 PublishSimFileEvent(SIM_STATE_ACTION, ICC_STATE_IMSI, ObtainIMSI());
247 }
248 }
249
UnregisterImsiLoaded(const std::shared_ptr<AppExecFwk::EventHandler> & handler)250 void IccFile::UnregisterImsiLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
251 {
252 if (imsiReadyObser_ != nullptr) {
253 imsiReadyObser_->Remove(RadioEvent::RADIO_IMSI_LOADED_READY, handler);
254 }
255 }
256
RegisterAllFilesLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)257 void IccFile::RegisterAllFilesLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler)
258 {
259 int eventCode = RadioEvent::RADIO_SIM_RECORDS_LOADED;
260 if (filesFetchedObser_ != nullptr) {
261 filesFetchedObser_->RegObserver(eventCode, eventHandler);
262 }
263 TELEPHONY_LOGI("IccFile::RegisterAllFilesLoaded: registerd");
264 if (ObtainFilesFetched()) {
265 TELEPHONY_LOGI("IccFile::RegisterAllFilesLoaded: notify");
266 if (filesFetchedObser_ != nullptr) {
267 filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_SIM_RECORDS_LOADED, slotId_);
268 }
269 PublishSimFileEvent(SIM_STATE_ACTION, ICC_STATE_LOADED, "");
270 }
271 }
272
UnregisterAllFilesLoaded(const std::shared_ptr<AppExecFwk::EventHandler> & handler)273 void IccFile::UnregisterAllFilesLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
274 {
275 if (filesFetchedObser_ != nullptr) {
276 filesFetchedObser_->Remove(RadioEvent::RADIO_SIM_RECORDS_LOADED, handler);
277 }
278 }
279
RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)280 void IccFile::RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
281 {
282 switch (what) {
283 case RadioEvent::RADIO_SIM_RECORDS_LOADED:
284 RegisterAllFilesLoaded(handler);
285 break;
286 case RadioEvent::RADIO_IMSI_LOADED_READY:
287 RegisterImsiLoaded(handler);
288 break;
289 default:
290 TELEPHONY_LOGI("RegisterCoreNotify default");
291 }
292 }
293
UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> & handler,int what)294 void IccFile::UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what)
295 {
296 switch (what) {
297 case RadioEvent::RADIO_SIM_RECORDS_LOADED:
298 UnregisterAllFilesLoaded(handler);
299 break;
300 case RadioEvent::RADIO_IMSI_LOADED_READY:
301 UnregisterImsiLoaded(handler);
302 break;
303 default:
304 TELEPHONY_LOGI("RegisterCoreNotify default");
305 }
306 }
307
UpdateSPN(const std::string spn)308 void IccFile::UpdateSPN(const std::string spn)
309 {
310 if (spn_ != spn) {
311 spnUpdatedObser_->NotifyObserver(MSG_SIM_SPN_UPDATED);
312 spn_ = spn;
313 }
314 }
315
BuildCallerInfo(int eventId)316 AppExecFwk::InnerEvent::Pointer IccFile::BuildCallerInfo(int eventId)
317 {
318 std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
319 int eventParam = 0;
320 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
321 event->SetOwner(shared_from_this());
322 return event;
323 }
324
BuildCallerInfo(int eventId,int arg1,int arg2)325 AppExecFwk::InnerEvent::Pointer IccFile::BuildCallerInfo(int eventId, int arg1, int arg2)
326 {
327 std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
328 object->arg1 = arg1;
329 object->arg2 = arg2;
330 int eventParam = 0;
331 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
332 event->SetOwner(shared_from_this());
333 return event;
334 }
335
BuildCallerInfo(int eventId,std::shared_ptr<void> loader)336 AppExecFwk::InnerEvent::Pointer IccFile::BuildCallerInfo(int eventId, std::shared_ptr<void> loader)
337 {
338 std::unique_ptr<FileToControllerMsg> object = std::make_unique<FileToControllerMsg>();
339 object->iccLoader = loader;
340 int eventParam = 0;
341 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, object, eventParam);
342 event->SetOwner(shared_from_this());
343 return event;
344 }
345
PublishSimFileEvent(const std::string & event,int eventCode,const std::string & eventData)346 bool IccFile::PublishSimFileEvent(const std::string &event, int eventCode, const std::string &eventData)
347 {
348 Want want;
349 want.SetAction(event);
350 CommonEventData data;
351 data.SetWant(want);
352 data.SetCode(eventCode);
353 data.SetData(eventData);
354 CommonEventPublishInfo publishInfo;
355 publishInfo.SetOrdered(true);
356 bool publishResult = CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
357 TELEPHONY_LOGI("IccFile::PublishSimEvent result : %{public}d", publishResult);
358 return publishResult;
359 }
360
ProcessIccFileObtained(const AppExecFwk::InnerEvent::Pointer & event)361 bool IccFile::ProcessIccFileObtained(const AppExecFwk::InnerEvent::Pointer &event)
362 {
363 bool isFileProcessResponse = true;
364 std::shared_ptr<ControllerToFileMsg> fd = event->GetSharedObject<ControllerToFileMsg>();
365 std::shared_ptr<void> baseLoad = fd->iccLoader;
366 if (baseLoad != nullptr) {
367 std::shared_ptr<IccFileLoaded> destLoad = std::static_pointer_cast<IccFileLoaded>(baseLoad);
368 destLoad->ProcessParseFile(event);
369 TELEPHONY_LOGI("ProcessIccFileObtained item %{public}s", destLoad->ObtainElementaryFileName().c_str());
370 } else {
371 isFileProcessResponse = false;
372 TELEPHONY_LOGE("IccFile::ProcessIccFileObtained null base ponter");
373 }
374 return isFileProcessResponse;
375 }
376
UpdateIccLanguage(const std::string & langLi,const std::string & langPl)377 void IccFile::UpdateIccLanguage(const std::string &langLi, const std::string &langPl)
378 {
379 iccLanguage_ = ObtainValidLanguage(langLi);
380 if (iccLanguage_.empty()) {
381 iccLanguage_ = ObtainValidLanguage(langPl);
382 }
383 TELEPHONY_LOGI("IccFile::UpdateIccLanguage end is %{public}s", iccLanguage_.c_str());
384 }
385
ObtainValidLanguage(const std::string & langData)386 std::string IccFile::ObtainValidLanguage(const std::string &langData)
387 {
388 if (langData.empty()) {
389 TELEPHONY_LOGE("langData null data!!");
390 return "";
391 }
392 int langDataLen = 0;
393 std::shared_ptr<unsigned char> ucc = SIMUtils::HexStringConvertToBytes(langData, langDataLen);
394 unsigned char *data = ucc.get();
395
396 std::string spnName((char *)data);
397 TELEPHONY_LOGI("ObtainValidLanguage all is %{public}s---%{public}d", spnName.c_str(), langDataLen);
398 std::string result = "";
399 for (int i = 0; (i + 1) < langDataLen; i += DATA_STEP) {
400 std::string langName((char *)data, i, DATA_STEP);
401 TELEPHONY_LOGI("ObtainValidLanguage item is %{public}d--%{public}s", i, langName.c_str());
402 if (!langName.empty()) {
403 result = langName;
404 }
405 }
406
407 return result;
408 }
409
~IccFile()410 IccFile::~IccFile() {}
411
SetRilAndFileController(const std::shared_ptr<Telephony::ITelRilManager> & ril,const std::shared_ptr<IccFileController> & file,const std::shared_ptr<IccDiallingNumbersHandler> & handler)412 void IccFile::SetRilAndFileController(const std::shared_ptr<Telephony::ITelRilManager> &ril,
413 const std::shared_ptr<IccFileController> &file, const std::shared_ptr<IccDiallingNumbersHandler> &handler)
414 {
415 telRilManager_ = ril;
416 if (telRilManager_ == nullptr) {
417 TELEPHONY_LOGE("IccFile set NULL TelRilManager!!");
418 }
419
420 fileController_ = file;
421 if (fileController_ == nullptr) {
422 TELEPHONY_LOGE("IccFile set NULL File Controller!!");
423 }
424 diallingNumberHandler_ = handler;
425 if (fileController_ == nullptr) {
426 TELEPHONY_LOGE("IccFile set NULL File Controller!!");
427 }
428 }
429
CreateDiallingNumberPointer(int eventid,int efId,int index,std::shared_ptr<void> pobj)430 AppExecFwk::InnerEvent::Pointer IccFile::CreateDiallingNumberPointer(
431 int eventid, int efId, int index, std::shared_ptr<void> pobj)
432 {
433 std::unique_ptr<DiallingNumbersHandleHolder> holder = std::make_unique<DiallingNumbersHandleHolder>();
434 holder->fileID = efId;
435 holder->index = index;
436 holder->diallingNumber = pobj;
437 int eventParam = 0;
438 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventid, holder, eventParam);
439 event->SetOwner(shared_from_this());
440 return event;
441 }
442
443
NotifyRegistrySimState(CardType type,SimState state,LockReason reason)444 void IccFile::NotifyRegistrySimState(CardType type, SimState state, LockReason reason)
445 {
446 int32_t result =
447 DelayedRefSingleton<TelephonyStateRegistryClient>::GetInstance().UpdateSimState(slotId_, type, state, reason);
448 TELEPHONY_LOGI("NotifyRegistrySimState msgId is %{public}d ret %{public}d", state, result);
449 }
450
HasSimCard()451 bool IccFile::HasSimCard()
452 {
453 return (stateManager_ != nullptr) ? stateManager_->HasSimCard() : false;
454 }
455
UnInit()456 void IccFile::UnInit()
457 {
458 imsi_ = "";
459 iccId_ = "";
460 UpdateSPN("");
461 UpdateLoaded(false);
462 operatorNumeric_ = "";
463 voiceMailNum_ = "";
464 voiceMailTag_ = "";
465 indexOfMailbox_ = 1;
466 msisdn_ = "";
467 gid1_ = "";
468 msisdnTag_ = "";
469 }
470 } // namespace Telephony
471 } // namespace OHOS
472