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 "isim_file.h"
17 
18 #include "radio_event.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "telephony_ext_wrapper.h"
22 
23 using namespace std;
24 using namespace OHOS::AppExecFwk;
25 using namespace OHOS::EventFwk;
26 
27 namespace OHOS {
28 namespace Telephony {
IsimFile(std::shared_ptr<SimStateManager> simStateManager)29 IsimFile::IsimFile(std::shared_ptr<SimStateManager> simStateManager) : IccFile("IsimFile", simStateManager)
30 {
31     fileQueried_ = false;
32     InitMemberFunc();
33 }
34 
StartLoad()35 void IsimFile::StartLoad()
36 {
37     TELEPHONY_LOGI("IsimFile::StartLoad() start");
38     LoadIsimFiles();
39 }
40 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)41 void IsimFile::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
42 {
43     if (event == nullptr) {
44         TELEPHONY_LOGE("event is nullptr!");
45         return;
46     }
47     auto id = event->GetInnerEventId();
48     bool isFileHandleResponse = false;
49     TELEPHONY_LOGD("IsimFile::ProcessEvent id %{public}d", id);
50     auto itFunc = memberFuncMap_.find(id);
51     if (itFunc != memberFuncMap_.end()) {
52         auto memberFunc = itFunc->second;
53         if (memberFunc != nullptr) {
54             isFileHandleResponse = memberFunc(event);
55         }
56     } else {
57         IccFile::ProcessEvent(event);
58     }
59     ProcessFileLoaded(isFileHandleResponse);
60 }
61 
ProcessIccRefresh(int msgId)62 void IsimFile::ProcessIccRefresh(int msgId)
63 {
64     LoadIsimFiles();
65 }
66 
ProcessFileLoaded(bool response)67 void IsimFile::ProcessFileLoaded(bool response)
68 {
69     if (!response) {
70         return;
71     }
72     fileToGet_ -= LOAD_STEP;
73     TELEPHONY_LOGI("IsimFile::ProcessFileLoaded: %{public}d requested: %{public}d", fileToGet_, fileQueried_);
74     if (ObtainFilesFetched()) {
75         OnAllFilesFetched();
76     } else if (LockQueriedOrNot()) {
77         ProcessLockedAllFilesFetched();
78     } else if (fileToGet_ < 0) {
79         fileToGet_ = 0;
80     }
81 }
82 
ProcessLockedAllFilesFetched()83 void IsimFile::ProcessLockedAllFilesFetched() {}
84 
OnAllFilesFetched()85 void IsimFile::OnAllFilesFetched()
86 {
87     UpdateLoaded(true);
88     TELEPHONY_LOGI("IsimFile::OnAllFilesFetched: start notify slotId = %{public}d", slotId_);
89     if (filesFetchedObser_ != nullptr) {
90         filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_SIM_RECORDS_LOADED, slotId_);
91     }
92     if (stateManager_ != nullptr) {
93         CardType cardType = stateManager_->GetCardType();
94         NotifyRegistrySimState(cardType, SimState::SIM_STATE_LOADED, LockReason::SIM_NONE);
95     }
96     PublishSimFileEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_LOADED, "");
97     LoadVoiceMail();
98     if (TELEPHONY_EXT_WRAPPER.onAllFilesFetchedExt_) {
99         TELEPHONY_EXT_WRAPPER.onAllFilesFetchedExt_(slotId_);
100     }
101 }
102 
ProcessIccReady(const AppExecFwk::InnerEvent::Pointer &event)103 bool IsimFile::ProcessIccReady(const AppExecFwk::InnerEvent::Pointer &event)
104 {
105     TELEPHONY_LOGI("IsimFile::SIM_STATE_READY --received");
106     if (stateManager_ == nullptr) {
107         TELEPHONY_LOGE("stateManager_ is nullptr!");
108         return false;
109     }
110     if (stateManager_->GetCardType() != CardType::SINGLE_MODE_ISIM_CARD) {
111         TELEPHONY_LOGI("invalid IsimFile::SIM_STATE_READY received");
112         return false;
113     }
114     LoadIsimFiles();
115     return false;
116 }
117 
ProcessIsimRefresh(const AppExecFwk::InnerEvent::Pointer &event)118 bool IsimFile::ProcessIsimRefresh(const AppExecFwk::InnerEvent::Pointer &event)
119 {
120     return false;
121 }
122 
LoadIsimFiles()123 void IsimFile::LoadIsimFiles()
124 {
125     TELEPHONY_LOGI("LoadIsimFiles started");
126     fileQueried_ = true;
127     AppExecFwk::InnerEvent::Pointer eventImpi = BuildCallerInfo(MSG_SIM_OBTAIN_IMPI_DONE);
128     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_IMPI, eventImpi);
129     fileToGet_++;
130 
131     AppExecFwk::InnerEvent::Pointer eventIst = BuildCallerInfo(MSG_SIM_OBTAIN_IST_DONE);
132     fileController_->ObtainBinaryFile(ELEMENTARY_FILE_IST, eventIst);
133     fileToGet_++;
134 }
135 
136 
ProcessGetIccidDone(const AppExecFwk::InnerEvent::Pointer &event)137 bool IsimFile::ProcessGetIccidDone(const AppExecFwk::InnerEvent::Pointer &event)
138 {
139     bool isFileProcessResponse = true;
140     if (event == nullptr) {
141         TELEPHONY_LOGE("event is nullptr!");
142         return isFileProcessResponse;
143     }
144     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
145     if (fd == nullptr) {
146         TELEPHONY_LOGE("fd is nullptr!");
147         return isFileProcessResponse;
148     }
149     if (fd->exception == nullptr) {
150         iccId_ = fd->resultData;
151         TELEPHONY_LOGI("IsimFile::ProcessEvent MSG_SIM_OBTAIN_ICCID_DONE result success, slotId:%{public}d", slotId_);
152         if (filesFetchedObser_ != nullptr) {
153             TELEPHONY_LOGI("slotId:%{public}d iccid loaded", slotId_);
154             iccidLoadObser_->NotifyObserver(RadioEvent::RADIO_QUERY_ICCID_DONE, slotId_);
155         }
156     }
157     return isFileProcessResponse;
158 }
159 
ProcessGetImsiDone(const AppExecFwk::InnerEvent::Pointer &event)160 bool IsimFile::ProcessGetImsiDone(const AppExecFwk::InnerEvent::Pointer &event)
161 {
162     bool isFileHandleResponse = true;
163     if (event == nullptr) {
164         TELEPHONY_LOGE("event is nullptr!");
165         return isFileHandleResponse;
166     }
167     std::shared_ptr<std::string> sharedObject = event->GetSharedObject<std::string>();
168     if (sharedObject == nullptr) {
169         TELEPHONY_LOGE("fd is nullptr!");
170         return isFileHandleResponse;
171     }
172     if (sharedObject != nullptr) {
173         imsi_ = *sharedObject;
174         TELEPHONY_LOGI("IsimFile::ProcessEvent MSG_SIM_OBTAIN_IMSI_DONE");
175         SaveCountryCode();
176         if (!imsi_.empty()) {
177             imsiReadyObser_->NotifyObserver(RadioEvent::RADIO_IMSI_LOADED_READY);
178         }
179     }
180     return isFileHandleResponse;
181 }
182 
InitMemberFunc()183 void IsimFile::InitMemberFunc()
184 {
185     memberFuncMap_[RadioEvent::RADIO_SIM_STATE_READY] =
186         [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessIccReady(event); };
187     memberFuncMap_[MSG_ICC_REFRESH] =
188         [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessIsimRefresh(event); };
189     memberFuncMap_[MSG_SIM_OBTAIN_IMSI_DONE] =
190         [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetImsiDone(event); };
191     memberFuncMap_[MSG_SIM_OBTAIN_ICCID_DONE] =
192         [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetIccidDone(event); };
193     memberFuncMap_[MSG_SIM_OBTAIN_IMPI_DONE] =
194         [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetImpiDone(event); };
195     memberFuncMap_[MSG_SIM_OBTAIN_IST_DONE] =
196         [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetIstDone(event);};
197 }
198 
ProcessGetImpiDone(const AppExecFwk::InnerEvent::Pointer &event)199 bool IsimFile::ProcessGetImpiDone(const AppExecFwk::InnerEvent::Pointer &event)
200 {
201     bool isFileProcessResponse = true;
202     if (event == nullptr) {
203         TELEPHONY_LOGE("event is nullptr!");
204         return isFileProcessResponse;
205     }
206     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
207     if (fd == nullptr) {
208         TELEPHONY_LOGE("fd is nullptr!");
209         return isFileProcessResponse;
210     }
211     if (fd->exception != nullptr) {
212         TELEPHONY_LOGE("ProcessGetImpiDone get exception");
213         return isFileProcessResponse;
214     }
215     imsi_ = fd->resultData;
216     TELEPHONY_LOGI("IsimFile::ProcessGetImpiDone success");
217     return isFileProcessResponse;
218 }
219 
ProcessGetIstDone(const AppExecFwk::InnerEvent::Pointer &event)220 bool IsimFile::ProcessGetIstDone(const AppExecFwk::InnerEvent::Pointer &event)
221 {
222     bool isFileProcessResponse = true;
223     if (event == nullptr) {
224         TELEPHONY_LOGE("event is nullptr!");
225         return isFileProcessResponse;
226     }
227     std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
228     if (fd == nullptr) {
229         TELEPHONY_LOGE("fd is nullptr!");
230         return isFileProcessResponse;
231     }
232     if (fd->exception != nullptr) {
233         TELEPHONY_LOGE("ProcessGetIstDone get exception");
234         return isFileProcessResponse;
235     }
236     ist_ = fd->resultData;
237     TELEPHONY_LOGI("IsimFile::ProcessGetIstDone success");
238     return isFileProcessResponse;
239 }
240 
ObtainIsimImpi()241 std::string IsimFile::ObtainIsimImpi()
242 {
243     return impi_;
244 }
ObtainIsimDomain()245 std::string IsimFile::ObtainIsimDomain()
246 {
247     return domain_;
248 }
ObtainIsimImpu()249 std::string* IsimFile::ObtainIsimImpu()
250 {
251     return impu_;
252 }
ObtainIsimIst()253 std::string IsimFile::ObtainIsimIst()
254 {
255     return ist_;
256 }
ObtainIsimPcscf()257 std::string* IsimFile::ObtainIsimPcscf()
258 {
259     return pcscf_;
260 }
261 
UpdateVoiceMail(const std::string &mailName, const std::string &mailNumber)262 bool IsimFile::UpdateVoiceMail(const std::string &mailName, const std::string &mailNumber)
263 {
264     // cdma not support
265     return false;
266 }
267 
SetVoiceMailCount(int32_t voiceMailCount)268 bool IsimFile::SetVoiceMailCount(int32_t voiceMailCount)
269 {
270     // cdma not support
271     return false;
272 }
273 
SetVoiceCallForwarding(bool enable, const std::string &number)274 bool IsimFile::SetVoiceCallForwarding(bool enable, const std::string &number)
275 {
276     // cdma not support
277     return false;
278 }
279 
ObtainSpnCondition(bool roaming, const std::string &operatorNum)280 int IsimFile::ObtainSpnCondition(bool roaming, const std::string &operatorNum)
281 {
282     return 0;
283 }
284 
ObtainIsoCountryCode()285 std::string IsimFile::ObtainIsoCountryCode()
286 {
287     return "";
288 }
289 
GetVoiceMailNumber()290 std::string IsimFile::GetVoiceMailNumber()
291 {
292     std::shared_lock<std::shared_mutex> lock(voiceMailMutex_);
293     return voiceMailNum_;
294 }
295 
SetVoiceMailNumber(const std::string mailNumber)296 void IsimFile::SetVoiceMailNumber(const std::string mailNumber)
297 {
298     std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
299     voiceMailNum_ = mailNumber;
300 }
301 
~IsimFile()302 IsimFile::~IsimFile() {}
303 } // namespace Telephony
304 } // namespace OHOS
305