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