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 isSimRecordLoaded_ = true;
92 }
93 if (stateManager_ != nullptr) {
94 CardType cardType = stateManager_->GetCardType();
95 NotifyRegistrySimState(cardType, SimState::SIM_STATE_LOADED, LockReason::SIM_NONE);
96 }
97 PublishSimFileEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED, ICC_STATE_LOADED, "");
98 LoadVoiceMail();
99 if (TELEPHONY_EXT_WRAPPER.onAllFilesFetchedExt_) {
100 TELEPHONY_EXT_WRAPPER.onAllFilesFetchedExt_(slotId_);
101 }
102 }
103
ProcessIccReady(const AppExecFwk::InnerEvent::Pointer & event)104 bool IsimFile::ProcessIccReady(const AppExecFwk::InnerEvent::Pointer &event)
105 {
106 TELEPHONY_LOGI("IsimFile::SIM_STATE_READY --received");
107 if (stateManager_ == nullptr) {
108 TELEPHONY_LOGE("stateManager_ is nullptr!");
109 return false;
110 }
111 if (stateManager_->GetCardType() != CardType::SINGLE_MODE_ISIM_CARD) {
112 TELEPHONY_LOGI("invalid IsimFile::SIM_STATE_READY received");
113 return false;
114 }
115 LoadIsimFiles();
116 return false;
117 }
118
ProcessIsimRefresh(const AppExecFwk::InnerEvent::Pointer & event)119 bool IsimFile::ProcessIsimRefresh(const AppExecFwk::InnerEvent::Pointer &event)
120 {
121 return false;
122 }
123
LoadIsimFiles()124 void IsimFile::LoadIsimFiles()
125 {
126 TELEPHONY_LOGI("LoadIsimFiles started");
127 fileQueried_ = true;
128 AppExecFwk::InnerEvent::Pointer eventImpi = BuildCallerInfo(MSG_SIM_OBTAIN_IMPI_DONE);
129 fileController_->ObtainBinaryFile(ELEMENTARY_FILE_IMPI, eventImpi);
130 fileToGet_++;
131
132 AppExecFwk::InnerEvent::Pointer eventIst = BuildCallerInfo(MSG_SIM_OBTAIN_IST_DONE);
133 fileController_->ObtainBinaryFile(ELEMENTARY_FILE_IST, eventIst);
134 fileToGet_++;
135 }
136
137
ProcessGetIccidDone(const AppExecFwk::InnerEvent::Pointer & event)138 bool IsimFile::ProcessGetIccidDone(const AppExecFwk::InnerEvent::Pointer &event)
139 {
140 bool isFileProcessResponse = true;
141 if (event == nullptr) {
142 TELEPHONY_LOGE("event is nullptr!");
143 return isFileProcessResponse;
144 }
145 std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
146 if (fd == nullptr) {
147 TELEPHONY_LOGE("fd is nullptr!");
148 return isFileProcessResponse;
149 }
150 if (fd->exception == nullptr) {
151 iccId_ = fd->resultData;
152 TELEPHONY_LOGI("IsimFile::ProcessEvent MSG_SIM_OBTAIN_ICCID_DONE result success, slotId:%{public}d", slotId_);
153 if (filesFetchedObser_ != nullptr) {
154 TELEPHONY_LOGI("slotId:%{public}d iccid loaded", slotId_);
155 filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_QUERY_ICCID_DONE, slotId_);
156 }
157 }
158 return isFileProcessResponse;
159 }
160
ProcessGetImsiDone(const AppExecFwk::InnerEvent::Pointer & event)161 bool IsimFile::ProcessGetImsiDone(const AppExecFwk::InnerEvent::Pointer &event)
162 {
163 bool isFileHandleResponse = true;
164 if (event == nullptr) {
165 TELEPHONY_LOGE("event is nullptr!");
166 return isFileHandleResponse;
167 }
168 std::shared_ptr<std::string> sharedObject = event->GetSharedObject<std::string>();
169 if (sharedObject == nullptr) {
170 TELEPHONY_LOGE("fd is nullptr!");
171 return isFileHandleResponse;
172 }
173 if (sharedObject != nullptr) {
174 imsi_ = *sharedObject;
175 TELEPHONY_LOGI("IsimFile::ProcessEvent MSG_SIM_OBTAIN_IMSI_DONE");
176 SaveCountryCode();
177 if (!imsi_.empty() && filesFetchedObser_ != nullptr) {
178 filesFetchedObser_->NotifyObserver(RadioEvent::RADIO_IMSI_LOADED_READY);
179 }
180 }
181 return isFileHandleResponse;
182 }
183
InitMemberFunc()184 void IsimFile::InitMemberFunc()
185 {
186 memberFuncMap_[RadioEvent::RADIO_SIM_STATE_READY] =
187 [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessIccReady(event); };
188 memberFuncMap_[MSG_ICC_REFRESH] =
189 [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessIsimRefresh(event); };
190 memberFuncMap_[MSG_SIM_OBTAIN_IMSI_DONE] =
191 [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetImsiDone(event); };
192 memberFuncMap_[MSG_SIM_OBTAIN_ICCID_DONE] =
193 [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetIccidDone(event); };
194 memberFuncMap_[MSG_SIM_OBTAIN_IMPI_DONE] =
195 [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetImpiDone(event); };
196 memberFuncMap_[MSG_SIM_OBTAIN_IST_DONE] =
197 [this](const AppExecFwk::InnerEvent::Pointer &event) { return ProcessGetIstDone(event);};
198 }
199
ProcessGetImpiDone(const AppExecFwk::InnerEvent::Pointer & event)200 bool IsimFile::ProcessGetImpiDone(const AppExecFwk::InnerEvent::Pointer &event)
201 {
202 bool isFileProcessResponse = true;
203 if (event == nullptr) {
204 TELEPHONY_LOGE("event is nullptr!");
205 return isFileProcessResponse;
206 }
207 std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
208 if (fd == nullptr) {
209 TELEPHONY_LOGE("fd is nullptr!");
210 return isFileProcessResponse;
211 }
212 if (fd->exception != nullptr) {
213 TELEPHONY_LOGE("ProcessGetImpiDone get exception");
214 return isFileProcessResponse;
215 }
216 imsi_ = fd->resultData;
217 TELEPHONY_LOGI("IsimFile::ProcessGetImpiDone success");
218 return isFileProcessResponse;
219 }
220
ProcessGetIstDone(const AppExecFwk::InnerEvent::Pointer & event)221 bool IsimFile::ProcessGetIstDone(const AppExecFwk::InnerEvent::Pointer &event)
222 {
223 bool isFileProcessResponse = true;
224 if (event == nullptr) {
225 TELEPHONY_LOGE("event is nullptr!");
226 return isFileProcessResponse;
227 }
228 std::unique_ptr<ControllerToFileMsg> fd = event->GetUniqueObject<ControllerToFileMsg>();
229 if (fd == nullptr) {
230 TELEPHONY_LOGE("fd is nullptr!");
231 return isFileProcessResponse;
232 }
233 if (fd->exception != nullptr) {
234 TELEPHONY_LOGE("ProcessGetIstDone get exception");
235 return isFileProcessResponse;
236 }
237 ist_ = fd->resultData;
238 TELEPHONY_LOGI("IsimFile::ProcessGetIstDone success");
239 return isFileProcessResponse;
240 }
241
ObtainIsimImpi()242 std::string IsimFile::ObtainIsimImpi()
243 {
244 return impi_;
245 }
ObtainIsimDomain()246 std::string IsimFile::ObtainIsimDomain()
247 {
248 return domain_;
249 }
ObtainIsimImpu()250 std::string* IsimFile::ObtainIsimImpu()
251 {
252 return impu_;
253 }
ObtainIsimIst()254 std::string IsimFile::ObtainIsimIst()
255 {
256 return ist_;
257 }
ObtainIsimPcscf()258 std::string* IsimFile::ObtainIsimPcscf()
259 {
260 return pcscf_;
261 }
262
UpdateVoiceMail(const std::string & mailName,const std::string & mailNumber)263 bool IsimFile::UpdateVoiceMail(const std::string &mailName, const std::string &mailNumber)
264 {
265 // cdma not support
266 return false;
267 }
268
SetVoiceMailCount(int32_t voiceMailCount)269 bool IsimFile::SetVoiceMailCount(int32_t voiceMailCount)
270 {
271 // cdma not support
272 return false;
273 }
274
SetVoiceCallForwarding(bool enable,const std::string & number)275 bool IsimFile::SetVoiceCallForwarding(bool enable, const std::string &number)
276 {
277 // cdma not support
278 return false;
279 }
280
ObtainSpnCondition(bool roaming,const std::string & operatorNum)281 int IsimFile::ObtainSpnCondition(bool roaming, const std::string &operatorNum)
282 {
283 return 0;
284 }
285
ObtainIsoCountryCode()286 std::string IsimFile::ObtainIsoCountryCode()
287 {
288 return "";
289 }
290
GetVoiceMailNumber()291 std::string IsimFile::GetVoiceMailNumber()
292 {
293 std::shared_lock<std::shared_mutex> lock(voiceMailMutex_);
294 return voiceMailNum_;
295 }
296
SetVoiceMailNumber(const std::string mailNumber)297 void IsimFile::SetVoiceMailNumber(const std::string mailNumber)
298 {
299 std::unique_lock<std::shared_mutex> lock(voiceMailMutex_);
300 voiceMailNum_ = mailNumber;
301 }
302
~IsimFile()303 IsimFile::~IsimFile() {}
304 } // namespace Telephony
305 } // namespace OHOS
306