• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_controller.h"
17 
18 using namespace std;
19 using namespace OHOS::AppExecFwk;
20 
21 namespace OHOS {
22 namespace Telephony {
IccFileController(const std::shared_ptr<AppExecFwk::EventRunner> & runner,int slotId)23 IccFileController::IccFileController(const std::shared_ptr<AppExecFwk::EventRunner> &runner, int slotId)
24     : AppExecFwk::EventHandler(runner), slotId_(slotId)
25 {}
26 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)27 void IccFileController::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
28 {
29     if (event == nullptr) {
30         TELEPHONY_LOGE("event is nullptr!");
31         return;
32     }
33     uint32_t id = event->GetInnerEventId();
34     TELEPHONY_LOGD("IccFileController ProcessEvent Id is %{public}d", id);
35     if (ProcessErrorResponse(event)) {
36         return;
37     }
38     switch (id) {
39         case MSG_SIM_OBTAIN_SIZE_OF_LINEAR_ELEMENTARY_FILE_DONE:
40             ProcessLinearRecordSize(event);
41             break;
42         case MSG_SIM_OBTAIN_SIZE_OF_FIXED_ELEMENTARY_FILE_DONE:
43             ProcessRecordSize(event);
44             break;
45         case MSG_SIM_OBTAIN_SIZE_OF_TRANSPARENT_ELEMENTARY_FILE_DONE:
46             ProcessBinarySize(event);
47             break;
48         case MSG_SIM_OBTAIN_FIXED_ELEMENTARY_FILE_DONE:
49             ProcessReadRecord(event);
50             break;
51         case MSG_SIM_OBTAIN_ICON_DONE:
52         case MSG_SIM_UPDATE_LINEAR_FIXED_FILE_DONE:
53         case MSG_SIM_UPDATE_TRANSPARENT_ELEMENTARY_FILE_DONE:
54         case MSG_SIM_OBTAIN_TRANSPARENT_ELEMENTARY_FILE_DONE:
55             ProcessReadBinary(event);
56             break;
57         default:
58             break;
59     }
60 }
61 
ProcessLinearRecordSize(const AppExecFwk::InnerEvent::Pointer & event)62 void IccFileController::ProcessLinearRecordSize(const AppExecFwk::InnerEvent::Pointer &event)
63 {
64     std::unique_ptr<IccFromRilMsg> rcvMsg = event->GetUniqueObject<IccFromRilMsg>();
65     if (rcvMsg == nullptr) {
66         TELEPHONY_LOGE("rcvMsg is nullptr");
67         return;
68     }
69     IccFileData *result = &(rcvMsg->fileData);
70     std::shared_ptr<IccControllerHolder> &hd = rcvMsg->controlHolder;
71     if (result == nullptr || hd == nullptr) {
72         TELEPHONY_LOGE("result or hd is nullptr");
73         return;
74     }
75     const AppExecFwk::InnerEvent::Pointer &process = hd->fileLoaded;
76     TELEPHONY_LOGI("ProcessLinearRecordSize --- resultData: --- %{public}s", result->resultData.c_str());
77     int recordLen = 0;
78     int fileSize[] = { 0, 0, 0 };
79     std::shared_ptr<unsigned char> rawData = SIMUtils::HexStringConvertToBytes(result->resultData, recordLen);
80     if (recordLen > LENGTH_OF_RECORD) {
81         unsigned char *fileData = rawData.get();
82         ParseFileSize(fileSize, RECORD_NUM, fileData);
83     }
84     SendEfLinearResult(process, fileSize, RECORD_NUM);
85 }
86 
ProcessRecordSize(const AppExecFwk::InnerEvent::Pointer & event)87 void IccFileController::ProcessRecordSize(const AppExecFwk::InnerEvent::Pointer &event)
88 {
89     int size = 0;
90     std::string str = IccFileController::NULLSTR;
91     std::string path = IccFileController::NULLSTR;
92     std::unique_ptr<IccFromRilMsg> rcvMsg = event->GetUniqueObject<IccFromRilMsg>();
93     IccFileData *result = &(rcvMsg->fileData);
94     std::shared_ptr<IccControllerHolder> &hd = rcvMsg->controlHolder;
95     if (result == nullptr || hd == nullptr) {
96         TELEPHONY_LOGE("result or hd is nullptr");
97         return;
98     }
99     TELEPHONY_LOGI("ProcessRecordSize --- resultData: --- %{public}s", result->resultData.c_str());
100     int recordLen = 0;
101     std::shared_ptr<unsigned char> rawData = SIMUtils::HexStringConvertToBytes(result->resultData, recordLen);
102     if (rawData == nullptr) {
103         TELEPHONY_LOGE("rawData is nullptr");
104         SendResponse(rcvMsg->controlHolder, &(rcvMsg->fileData));
105         return;
106     }
107     unsigned char *fileData = rawData.get();
108     path = CheckRightPath(hd->filePath, hd->fileId);
109     if (recordLen > LENGTH_OF_RECORD) {
110         if (!IsValidSizeData(fileData)) {
111             TELEPHONY_LOGE("ProcessRecordSize get error filetype");
112             SendResponse(rcvMsg->controlHolder, &(rcvMsg->fileData));
113             return;
114         }
115         GetFileAndDataSize(fileData, hd->fileSize, size);
116         if (hd->fileSize != 0) {
117             hd->countFiles = size / hd->fileSize;
118         }
119     }
120     TELEPHONY_LOGI("ProcessRecordSize %{public}d %{public}d %{public}d", size, hd->fileSize, hd->countFiles);
121     if (telRilManager_ != nullptr) {
122         SimIoRequestInfo msg;
123         msg.command = CONTROLLER_REQ_READ_RECORD;
124         msg.fileId = hd->fileId;
125         msg.p1 = hd->fileNum;
126         msg.p2 = ICC_FILE_CURRENT_MODE;
127         msg.p3 = hd->fileSize;
128         msg.data = IccFileController::NULLSTR;
129         msg.path = path;
130         msg.pin2 = "";
131         telRilManager_->GetSimIO(slotId_, msg, BuildCallerInfo(MSG_SIM_OBTAIN_FIXED_ELEMENTARY_FILE_DONE, hd));
132     }
133 }
134 
ProcessBinarySize(const AppExecFwk::InnerEvent::Pointer & event)135 void IccFileController::ProcessBinarySize(const AppExecFwk::InnerEvent::Pointer &event)
136 {
137     std::string str = IccFileController::NULLSTR;
138     std::string path = IccFileController::NULLSTR;
139     TELEPHONY_LOGD("IccFileController::ProcessBinarySize init");
140     std::unique_ptr<IccFromRilMsg> rcvMsg = event->GetUniqueObject<IccFromRilMsg>();
141 
142     if (rcvMsg->controlHolder == nullptr) {
143         TELEPHONY_LOGE("IccFileController::ProcessBinarySize controlHolder is nullptr");
144         return;
145     }
146     const AppExecFwk::InnerEvent::Pointer &evt = rcvMsg->controlHolder->fileLoaded;
147 
148     if (evt->GetOwner() == nullptr) {
149         TELEPHONY_LOGE("IccFileController::ProcessBinarySize isNull is null pointer");
150         return;
151     }
152     int fileId = rcvMsg->arg1;
153     int size = 0;
154     AppExecFwk::InnerEvent::Pointer process =
155         BuildCallerInfo(MSG_SIM_OBTAIN_TRANSPARENT_ELEMENTARY_FILE_DONE, fileId, 0, evt);
156     if (telRilManager_ != nullptr) {
157         SimIoRequestInfo msg;
158         msg.command = CONTROLLER_REQ_READ_BINARY;
159         msg.fileId = fileId;
160         msg.p1 = 0;
161         msg.p2 = 0;
162         msg.p3 = size;
163         msg.data = IccFileController::NULLSTR;
164         msg.path = ObtainElementFilePath(fileId);
165         msg.pin2 = "";
166         telRilManager_->GetSimIO(slotId_, msg, process);
167     }
168     TELEPHONY_LOGD("IccFileController::ProcessBinarySize finish %{public}d", fileId);
169 }
170 
ProcessReadRecord(const AppExecFwk::InnerEvent::Pointer & event)171 void IccFileController::ProcessReadRecord(const AppExecFwk::InnerEvent::Pointer &event)
172 {
173     std::string str = IccFileController::NULLSTR;
174     std::string path = IccFileController::NULLSTR;
175     std::unique_ptr<IccFromRilMsg> rcvMsg = event->GetUniqueObject<IccFromRilMsg>();
176     const AppExecFwk::InnerEvent::Pointer &process = rcvMsg->controlHolder->fileLoaded;
177     IccFileData *result = &(rcvMsg->fileData);
178     std::shared_ptr<IccControllerHolder> &hd = rcvMsg->controlHolder;
179     TELEPHONY_LOGI("ProcessReadRecord %{public}d %{public}d %{public}d %{public}s", hd->getAllFile, hd->fileNum,
180         hd->countFiles, result->resultData.c_str());
181     path = CheckRightPath(hd->filePath, hd->fileId);
182     if (hd->getAllFile) {
183         hd->fileResults.push_back(result->resultData);
184         hd->fileNum++;
185         if (hd->fileNum > hd->countFiles) {
186             SendMultiRecordResult(process, hd->fileResults);
187         } else {
188             SimIoRequestInfo msg;
189             msg.command = CONTROLLER_REQ_READ_RECORD;
190             msg.fileId = hd->fileId;
191             msg.p1 = hd->fileNum;
192             msg.p2 = ICC_FILE_CURRENT_MODE;
193             msg.p3 = hd->fileSize;
194             msg.data = IccFileController::NULLSTR;
195             msg.path = path;
196             msg.pin2 = "";
197             telRilManager_->GetSimIO(slotId_, msg, BuildCallerInfo(MSG_SIM_OBTAIN_FIXED_ELEMENTARY_FILE_DONE, hd));
198         }
199     } else {
200         SendResponse(rcvMsg->controlHolder, &(rcvMsg->fileData));
201     }
202 }
203 
ProcessReadBinary(const AppExecFwk::InnerEvent::Pointer & event)204 void IccFileController::ProcessReadBinary(const AppExecFwk::InnerEvent::Pointer &event)
205 {
206     TELEPHONY_LOGD("IccFileController MSG_SIM_OBTAIN_TRANSPARENT_ELEMENTARY_FILE_DONE");
207     std::unique_ptr<IccFromRilMsg> rcvMsg = event->GetUniqueObject<IccFromRilMsg>();
208     if (rcvMsg == nullptr || rcvMsg->controlHolder == nullptr) {
209         TELEPHONY_LOGE("rcvMsg or rcvMsg->controlHolder is nullptr");
210         return;
211     }
212     SendResponse(rcvMsg->controlHolder, &(rcvMsg->fileData));
213 }
214 
ObtainElementFileForPublic(int efId)215 std::string IccFileController::ObtainElementFileForPublic(int efId)
216 {
217     std::string mf = MASTER_FILE_SIM;
218     if (efId == ELEMENTARY_FILE_ICCID || efId == ELEMENTARY_FILE_PL) {
219         return mf;
220     }
221     mf.append(DEDICATED_FILE_TELECOM);
222     if (efId == ELEMENTARY_FILE_ADN || efId == ELEMENTARY_FILE_FDN || efId == ELEMENTARY_FILE_MSISDN ||
223         efId == ELEMENTARY_FILE_SDN || efId == ELEMENTARY_FILE_EXT1 || efId == ELEMENTARY_FILE_EXT2 ||
224         efId == ELEMENTARY_FILE_EXT3) {
225         return mf;
226     }
227     if (efId == ELEMENTARY_FILE_PBR) {
228         mf.append(DEDICATED_FILE_DIALLING_NUMBERS);
229         return mf;
230     }
231     if (efId == ELEMENTARY_FILE_IMG) {
232         mf.append(DEDICATED_FILE_GRAPHICS);
233         return mf;
234     }
235     return IccFileController::NULLSTR;
236 }
237 
238 // implementation ObtainBinaryFile
ObtainBinaryFile(int fileId,const AppExecFwk::InnerEvent::Pointer & event)239 void IccFileController::ObtainBinaryFile(int fileId, const AppExecFwk::InnerEvent::Pointer &event)
240 {
241     TELEPHONY_LOGD("IccFileController::ObtainBinaryFile start");
242     AppExecFwk::InnerEvent::Pointer process =
243         BuildCallerInfo(MSG_SIM_OBTAIN_SIZE_OF_TRANSPARENT_ELEMENTARY_FILE_DONE, fileId, 0, event);
244     if (telRilManager_ != nullptr) {
245         SimIoRequestInfo msg;
246         msg.command = CONTROLLER_REQ_GET_RESPONSE;
247         msg.fileId = fileId;
248         msg.p1 = 0;
249         msg.p2 = 0;
250         msg.p3 = GET_RESPONSE_ELEMENTARY_FILE_SIZE_BYTES;
251         msg.data = IccFileController::NULLSTR;
252         msg.path = ObtainElementFilePath(fileId);
253         msg.pin2 = "";
254         telRilManager_->GetSimIO(slotId_, msg, process);
255     }
256     TELEPHONY_LOGD("IccFileController::ObtainBinaryFile end");
257 }
258 
ObtainBinaryFile(int fileId,int size,const AppExecFwk::InnerEvent::Pointer & event)259 void IccFileController::ObtainBinaryFile(int fileId, int size, const AppExecFwk::InnerEvent::Pointer &event)
260 {
261     AppExecFwk::InnerEvent::Pointer process =
262         BuildCallerInfo(MSG_SIM_OBTAIN_TRANSPARENT_ELEMENTARY_FILE_DONE, fileId, 0, event);
263     if (telRilManager_ != nullptr) {
264         SimIoRequestInfo msg;
265         msg.command = CONTROLLER_REQ_READ_BINARY;
266         msg.fileId = fileId;
267         msg.p1 = 0;
268         msg.p2 = 0;
269         msg.p3 = size;
270         msg.data = IccFileController::NULLSTR;
271         msg.path = ObtainElementFilePath(fileId);
272         msg.pin2 = "";
273         telRilManager_->GetSimIO(slotId_, msg, process);
274     }
275 }
276 
277 // implementation ObtainLinearFixedFile
ObtainLinearFixedFile(int fileId,const std::string & path,int fileNum,const AppExecFwk::InnerEvent::Pointer & event)278 void IccFileController::ObtainLinearFixedFile(
279     int fileId, const std::string &path, int fileNum, const AppExecFwk::InnerEvent::Pointer &event)
280 {
281     std::string filePath = CheckRightPath(path, fileId);
282     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(fileId, fileNum, filePath);
283     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(event));
284     AppExecFwk::InnerEvent::Pointer process =
285         BuildCallerInfo(MSG_SIM_OBTAIN_SIZE_OF_FIXED_ELEMENTARY_FILE_DONE, ctrlHolder);
286     if (telRilManager_ != nullptr) {
287         SimIoRequestInfo msg;
288         msg.command = CONTROLLER_REQ_GET_RESPONSE;
289         msg.fileId = fileId;
290         msg.p1 = 0;
291         msg.p2 = 0;
292         msg.p3 = GET_RESPONSE_ELEMENTARY_FILE_SIZE_BYTES;
293         msg.data = IccFileController::NULLSTR;
294         msg.path = filePath;
295         msg.pin2 = "";
296         telRilManager_->GetSimIO(slotId_, msg, process);
297     }
298 }
299 
ObtainLinearFixedFile(int fileId,int fileNum,const AppExecFwk::InnerEvent::Pointer & event)300 void IccFileController::ObtainLinearFixedFile(int fileId, int fileNum, const AppExecFwk::InnerEvent::Pointer &event)
301 {
302     ObtainLinearFixedFile(fileId, ObtainElementFilePath(fileId), fileNum, event);
303 }
304 
305 // implementation ObtainAllLinearFixedFile
ObtainAllLinearFixedFile(int fileId,const std::string & path,const AppExecFwk::InnerEvent::Pointer & event)306 void IccFileController::ObtainAllLinearFixedFile(
307     int fileId, const std::string &path, const AppExecFwk::InnerEvent::Pointer &event)
308 {
309     std::string filePath = CheckRightPath(path, fileId);
310     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(fileId, filePath);
311     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(event));
312     AppExecFwk::InnerEvent::Pointer process =
313         BuildCallerInfo(MSG_SIM_OBTAIN_SIZE_OF_FIXED_ELEMENTARY_FILE_DONE, ctrlHolder);
314     if (telRilManager_ != nullptr) {
315         SimIoRequestInfo msg;
316         msg.command = CONTROLLER_REQ_GET_RESPONSE;
317         msg.fileId = fileId;
318         msg.p1 = 0;
319         msg.p2 = 0;
320         msg.p3 = GET_RESPONSE_ELEMENTARY_FILE_SIZE_BYTES;
321         msg.data = IccFileController::NULLSTR;
322         msg.path = filePath;
323         msg.pin2 = "";
324         telRilManager_->GetSimIO(slotId_, msg, process);
325     }
326 }
327 
ObtainAllLinearFixedFile(int fileId,const AppExecFwk::InnerEvent::Pointer & event)328 void IccFileController::ObtainAllLinearFixedFile(int fileId, const AppExecFwk::InnerEvent::Pointer &event)
329 {
330     ObtainAllLinearFixedFile(fileId, ObtainElementFilePath(fileId), event);
331 }
332 
ObtainLinearFileSize(int fileId,const std::string & path,const AppExecFwk::InnerEvent::Pointer & event)333 void IccFileController::ObtainLinearFileSize(
334     int fileId, const std::string &path, const AppExecFwk::InnerEvent::Pointer &event)
335 {
336     std::string filePath = CheckRightPath(path, fileId);
337     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(fileId, filePath);
338     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(event));
339     AppExecFwk::InnerEvent::Pointer process =
340         BuildCallerInfo(MSG_SIM_OBTAIN_SIZE_OF_LINEAR_ELEMENTARY_FILE_DONE, ctrlHolder);
341     if (telRilManager_ != nullptr) {
342         SimIoRequestInfo msg;
343         msg.command = CONTROLLER_REQ_GET_RESPONSE;
344         msg.fileId = fileId;
345         msg.p1 = 0;
346         msg.p2 = 0;
347         msg.p3 = GET_RESPONSE_ELEMENTARY_FILE_SIZE_BYTES;
348         msg.data = IccFileController::NULLSTR;
349         msg.path = filePath;
350         msg.pin2 = "";
351         telRilManager_->GetSimIO(slotId_, msg, process);
352     }
353 }
354 
ObtainLinearFileSize(int fileId,const AppExecFwk::InnerEvent::Pointer & event)355 void IccFileController::ObtainLinearFileSize(int fileId, const AppExecFwk::InnerEvent::Pointer &event)
356 {
357     ObtainLinearFileSize(fileId, ObtainElementFilePath(fileId), event);
358 }
359 
UpdateLinearFixedFile(int fileId,const std::string & path,int fileNum,std::string data,int dataLength,const std::string pin2,const AppExecFwk::InnerEvent::Pointer & onComplete)360 void IccFileController::UpdateLinearFixedFile(int fileId, const std::string &path, int fileNum, std::string data,
361     int dataLength, const std::string pin2, const AppExecFwk::InnerEvent::Pointer &onComplete)
362 {
363     std::string filePath = CheckRightPath(path, fileId);
364     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(fileId);
365     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(onComplete));
366     AppExecFwk::InnerEvent::Pointer process = BuildCallerInfo(MSG_SIM_UPDATE_LINEAR_FIXED_FILE_DONE, ctrlHolder);
367     if (telRilManager_ != nullptr) {
368         SimIoRequestInfo msg;
369         msg.command = CONTROLLER_REQ_UPDATE_RECORD;
370         msg.fileId = fileId;
371         msg.p1 = fileNum;
372         msg.p2 = ICC_FILE_CURRENT_MODE;
373         msg.p3 = dataLength;
374         msg.data = data;
375         msg.path = filePath;
376         msg.pin2 = pin2;
377         telRilManager_->GetSimIO(slotId_, msg, process);
378     }
379 }
380 
UpdateLinearFixedFile(int fileId,int fileNum,const std::string data,int dataLength,const std::string pin2,const AppExecFwk::InnerEvent::Pointer & onComplete)381 void IccFileController::UpdateLinearFixedFile(int fileId, int fileNum, const std::string data, int dataLength,
382     const std::string pin2, const AppExecFwk::InnerEvent::Pointer &onComplete)
383 {
384     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(fileId);
385     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(onComplete));
386     AppExecFwk::InnerEvent::Pointer process = BuildCallerInfo(MSG_SIM_UPDATE_LINEAR_FIXED_FILE_DONE, ctrlHolder);
387     if (telRilManager_ != nullptr) {
388         SimIoRequestInfo msg;
389         msg.command = CONTROLLER_REQ_UPDATE_RECORD;
390         msg.fileId = fileId;
391         msg.p1 = fileNum;
392         msg.p2 = ICC_FILE_CURRENT_MODE;
393         msg.p3 = dataLength;
394         msg.data = data;
395         msg.path = ObtainElementFilePath(fileId);
396         msg.pin2 = pin2;
397         telRilManager_->GetSimIO(slotId_, msg, process);
398     }
399 }
400 
UpdateBinaryFile(int fileId,const std::string data,int dataLength,const AppExecFwk::InnerEvent::Pointer & onComplete)401 void IccFileController::UpdateBinaryFile(
402     int fileId, const std::string data, int dataLength, const AppExecFwk::InnerEvent::Pointer &onComplete)
403 {
404     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(fileId);
405     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(onComplete));
406     AppExecFwk::InnerEvent::Pointer process =
407         BuildCallerInfo(MSG_SIM_UPDATE_TRANSPARENT_ELEMENTARY_FILE_DONE, ctrlHolder);
408     if (telRilManager_ != nullptr) {
409         SimIoRequestInfo msg;
410         msg.command = CONTROLLER_REQ_UPDATE_BINARY;
411         msg.fileId = fileId;
412         msg.p1 = 0;
413         msg.p2 = 0;
414         msg.p3 = dataLength;
415         msg.data = data;
416         msg.path = ObtainElementFilePath(fileId);
417         msg.pin2 = "";
418         telRilManager_->GetSimIO(slotId_, msg, process);
419     }
420 }
421 
SendResponse(std::shared_ptr<IccControllerHolder> holder,const IccFileData * fd)422 void IccFileController::SendResponse(std::shared_ptr<IccControllerHolder> holder, const IccFileData *fd)
423 {
424     if (holder == nullptr || fd == nullptr) {
425         TELEPHONY_LOGE("IccFileController::SendResponse  result is null");
426         return;
427     }
428     AppExecFwk::InnerEvent::Pointer &response = holder->fileLoaded;
429     bool isNull = (response == nullptr);
430     auto owner = response->GetOwner();
431     if (owner == nullptr) {
432         TELEPHONY_LOGE("owner is nullptr");
433         return;
434     }
435     std::unique_ptr<FileToControllerMsg> cmdData = response->GetUniqueObject<FileToControllerMsg>();
436     uint32_t id = response->GetInnerEventId();
437     bool needShare = (id == MSG_SIM_OBTAIN_ICC_FILE_DONE);
438     std::unique_ptr<ControllerToFileMsg> objectUnique = nullptr;
439     std::shared_ptr<ControllerToFileMsg> objectShare = nullptr;
440     TELEPHONY_LOGD("IccFileController::SendResponse start response %{public}d %{public}d", isNull, needShare);
441     if (needShare) {
442         objectShare = std::make_shared<ControllerToFileMsg>(cmdData.get(), fd);
443     } else {
444         objectUnique = std::make_unique<ControllerToFileMsg>(cmdData.get(), fd);
445     }
446 
447     if ((objectUnique == nullptr) && (objectShare == nullptr)) {
448         TELEPHONY_LOGE("IccFileController::SendResponse  create ControllerToFileMsg is null");
449         return;
450     }
451 
452     isNull = (owner == nullptr);
453     TELEPHONY_LOGD("IccFileController::SendResponse owner: %{public}d evtId: %{public}d", isNull, id);
454     if (needShare) {
455         owner->SendEvent(id, objectShare);
456     } else {
457         owner->SendEvent(id, objectUnique);
458     }
459     TELEPHONY_LOGD("IccFileController::SendResponse send end");
460 }
461 
SendEfLinearResult(const AppExecFwk::InnerEvent::Pointer & response,const int val[],int len)462 void IccFileController::SendEfLinearResult(const AppExecFwk::InnerEvent::Pointer &response, const int val[], int len)
463 {
464     std::shared_ptr<AppExecFwk::EventHandler> handler = response->GetOwner();
465     if (handler == nullptr) {
466         TELEPHONY_LOGE("handler is nullptr!");
467         return;
468     }
469     std::unique_ptr<FileToControllerMsg> cmdData = response->GetUniqueObject<FileToControllerMsg>();
470     std::shared_ptr<EfLinearResult> object = std::make_shared<EfLinearResult>(cmdData.get());
471     object->valueData[0] = val[0];
472     object->valueData[1] = val[1];
473     object->valueData[MAX_FILE_INDEX] = val[MAX_FILE_INDEX];
474     uint32_t id = response->GetInnerEventId();
475     int eventParam = 0;
476     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(id, object, eventParam);
477     if (event == nullptr) {
478         TELEPHONY_LOGE("event is nullptr!");
479         return;
480     }
481     handler->SendEvent(event);
482 }
483 
SendMultiRecordResult(const AppExecFwk::InnerEvent::Pointer & response,std::vector<std::string> & strValue)484 void IccFileController::SendMultiRecordResult(
485     const AppExecFwk::InnerEvent::Pointer &response, std::vector<std::string> &strValue)
486 {
487     std::shared_ptr<AppExecFwk::EventHandler> handler = response->GetOwner();
488     if (handler == nullptr) {
489         TELEPHONY_LOGE("handler is nullptr!");
490         return;
491     }
492     std::unique_ptr<FileToControllerMsg> cmdData = response->GetUniqueObject<FileToControllerMsg>();
493     std::shared_ptr<MultiRecordResult> object = std::make_shared<MultiRecordResult>(cmdData.get());
494     object->fileResults.assign(strValue.begin(), strValue.end());
495     object->resultLength = static_cast<int>(strValue.size());
496     uint32_t id = response->GetInnerEventId();
497     int eventParam = 0;
498     TELEPHONY_LOGI("IccFileController::SendMultiRecordResult send end");
499     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(id, object, eventParam);
500     if (event == nullptr) {
501         TELEPHONY_LOGE("event is nullptr!");
502         return;
503     }
504     handler->SendEvent(event);
505 }
506 
BuildCallerInfo(int eventId,std::shared_ptr<IccControllerHolder> & holderObject)507 AppExecFwk::InnerEvent::Pointer IccFileController::BuildCallerInfo(
508     int eventId, std::shared_ptr<IccControllerHolder> &holderObject)
509 {
510     std::unique_ptr<IccToRilMsg> msgTo = std::make_unique<IccToRilMsg>(holderObject);
511     if (msgTo == nullptr) {
512         TELEPHONY_LOGE("IccFileController::BuildCallerInfo1  create null pointer");
513         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
514     }
515     int64_t eventParam = 0;
516     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, msgTo, eventParam);
517     if (event == nullptr) {
518         TELEPHONY_LOGE("event is nullptr!");
519         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
520     }
521     event->SetOwner(shared_from_this());
522     return event;
523 }
524 
BuildCallerInfo(int eventId,int arg1,int arg2,std::shared_ptr<IccControllerHolder> & holderObject)525 AppExecFwk::InnerEvent::Pointer IccFileController::BuildCallerInfo(
526     int eventId, int arg1, int arg2, std::shared_ptr<IccControllerHolder> &holderObject)
527 {
528     std::unique_ptr<IccToRilMsg> msgTo = std::make_unique<IccToRilMsg>(holderObject);
529     if (msgTo == nullptr) {
530         TELEPHONY_LOGE("IccFileController::BuildCallerInfo2  create null pointer");
531         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
532     }
533     msgTo->arg1 = arg1;
534     msgTo->arg2 = arg2;
535     int64_t eventParam = 0;
536     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, msgTo, eventParam);
537     if (event == nullptr) {
538         TELEPHONY_LOGE("event is nullptr!");
539         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
540     }
541     event->SetOwner(shared_from_this());
542     return event;
543 }
544 
BuildCallerInfo(int eventId,int arg1,int arg2,const AppExecFwk::InnerEvent::Pointer & msg)545 AppExecFwk::InnerEvent::Pointer IccFileController::BuildCallerInfo(
546     int eventId, int arg1, int arg2, const AppExecFwk::InnerEvent::Pointer &msg)
547 {
548     std::shared_ptr<IccControllerHolder> ctrlHolder = std::make_shared<IccControllerHolder>(arg1);
549     ctrlHolder->fileLoaded = std::move(const_cast<AppExecFwk::InnerEvent::Pointer &>(msg));
550     bool isNull = ctrlHolder->fileLoaded->GetOwner() == nullptr;
551     TELEPHONY_LOGD("IccFileController::BuildCallerInfo stage init owner: %{public}d", isNull);
552     std::unique_ptr<IccToRilMsg> msgTo = std::make_unique<IccToRilMsg>(ctrlHolder);
553     if (msgTo == nullptr) {
554         TELEPHONY_LOGE("IccFileController::BuildCallerInfo3  create null pointer");
555         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
556     }
557     TELEPHONY_LOGD("IccFileController::BuildCallerInfo stage end");
558     msgTo->arg1 = arg1;
559     msgTo->arg2 = arg2;
560     int64_t eventParam = 0;
561     AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(eventId, msgTo, eventParam);
562     if (event == nullptr) {
563         TELEPHONY_LOGE("event is nullptr!");
564         return AppExecFwk::InnerEvent::Pointer(nullptr, nullptr);
565     }
566     event->SetOwner(shared_from_this());
567     return event;
568 }
569 
ParseFileSize(int val[],int len,const unsigned char * data)570 void IccFileController::ParseFileSize(int val[], int len, const unsigned char *data)
571 {
572     if (data == nullptr) {
573         TELEPHONY_LOGE("ParseFileSize null data");
574         return;
575     }
576     if (len > MAX_FILE_INDEX) {
577         GetFileAndDataSize(data, val[0], val[1]);
578         if (val[0] != 0) {
579             val[MAX_FILE_INDEX] = val[1] / val[0];
580         }
581     }
582     TELEPHONY_LOGD("ParseFileSize result %{public}d, %{public}d %{public}d", val[0], val[1], val[MAX_FILE_INDEX]);
583 }
IsValidSizeData(const unsigned char * data)584 bool IccFileController::IsValidSizeData(const unsigned char *data)
585 {
586     if (data == nullptr) {
587         TELEPHONY_LOGE("IccFileTypeMismatch ERROR nullptr");
588         return false;
589     }
590     if (ICC_ELEMENTARY_FILE != data[TYPE_OF_FILE]) {
591         TELEPHONY_LOGE("IccFileTypeMismatch ERROR TYPE_OF_FILE");
592         return false;
593     }
594     if (ELEMENTARY_FILE_TYPE_LINEAR_FIXED != data[STRUCTURE_OF_DATA]) {
595         TELEPHONY_LOGE("IccFileTypeMismatch ERROR STRUCTURE_OF_DATA");
596         return false;
597     }
598     return true;
599 }
GetFileAndDataSize(const unsigned char * data,int & fileSize,int & dataSize)600 void IccFileController::GetFileAndDataSize(const unsigned char *data, int &fileSize, int &dataSize)
601 {
602     if (data == nullptr) {
603         TELEPHONY_LOGE("GetFileAndDataSize null data");
604         return;
605     }
606     fileSize = data[LENGTH_OF_RECORD] & BYTE_NUM;
607     dataSize = ((data[SIZE_ONE_OF_FILE] & BYTE_NUM) << OFFSET) + (data[SIZE_TWO_OF_FILE] & BYTE_NUM);
608 }
609 
SetRilManager(std::shared_ptr<Telephony::ITelRilManager> ril)610 void IccFileController::SetRilManager(std::shared_ptr<Telephony::ITelRilManager> ril)
611 {
612     telRilManager_ = ril;
613     if (telRilManager_ == nullptr) {
614         TELEPHONY_LOGE("IccFileController set NULL TelRilManager!!");
615     }
616 }
617 
CheckRightPath(const std::string & path,int fileId)618 std::string IccFileController::CheckRightPath(const std::string &path, int fileId)
619 {
620     if (path.empty()) {
621         return ObtainElementFilePath(fileId);
622     } else {
623         return path;
624     }
625 }
626 
ProcessErrorResponse(const AppExecFwk::InnerEvent::Pointer & event)627 bool IccFileController::ProcessErrorResponse(const AppExecFwk::InnerEvent::Pointer &event)
628 {
629     std::shared_ptr<IccFromRilMsg> rcvMsg = event->GetSharedObject<IccFromRilMsg>();
630     if (rcvMsg == nullptr) {
631         return false;
632     }
633     AppExecFwk::InnerEvent::Pointer &response = rcvMsg->controlHolder->fileLoaded;
634     auto owner = response->GetOwner();
635     if (owner == nullptr) {
636         TELEPHONY_LOGE("owner is nullptr");
637         return false;
638     }
639     uint32_t id = response->GetInnerEventId();
640     std::unique_ptr<FileToControllerMsg> cmdData = response->GetUniqueObject<FileToControllerMsg>();
641     bool needShare = (id == MSG_SIM_OBTAIN_ICC_FILE_DONE);
642     std::unique_ptr<ControllerToFileMsg> objectUnique = nullptr;
643     std::shared_ptr<ControllerToFileMsg> objectShare = nullptr;
644     TELEPHONY_LOGD("ProcessErrorResponse start response %{public}d", needShare);
645     if (needShare) {
646         objectShare = std::make_shared<ControllerToFileMsg>(cmdData.get(), nullptr);
647         objectShare->exception = rcvMsg->fileData.exception;
648     } else {
649         objectUnique = std::make_unique<ControllerToFileMsg>(cmdData.get(), nullptr);
650         objectUnique->exception = rcvMsg->fileData.exception;
651     }
652 
653     if ((objectUnique == nullptr) && (objectShare == nullptr)) {
654         TELEPHONY_LOGE("ProcessErrorResponse  create ControllerToFileMsg is null");
655         return true;
656     }
657 
658     TELEPHONY_LOGI("ProcessErrorResponse owner: evtId: %{public}d", id);
659     if (needShare) {
660         owner->SendEvent(id, objectShare);
661     } else {
662         owner->SendEvent(id, objectUnique);
663     }
664     TELEPHONY_LOGD("ProcessErrorResponse send end");
665     return true;
666 }
667 
IsFixedNumberType(int efId)668 bool IccFileController::IsFixedNumberType(int efId)
669 {
670     bool fixed = false;
671     switch (efId) {
672         case ELEMENTARY_FILE_ADN:
673         case ELEMENTARY_FILE_FDN:
674         case ELEMENTARY_FILE_USIM_ADN:
675         case ELEMENTARY_FILE_USIM_IAP:
676             fixed = true;
677             return fixed;
678         default:
679             break;
680     }
681     return fixed;
682 }
683 
~IccFileController()684 IccFileController::~IccFileController() {}
685 } // namespace Telephony
686 } // namespace OHOS
687