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 #ifndef OHOS_ICC_FILE_CONTROLLER_H
17 #define OHOS_ICC_FILE_CONTROLLER_H
18 
19 #include <cstring>
20 #include <string>
21 
22 #include "i_tel_ril_manager.h"
23 #include "sim_constant.h"
24 #include "sim_data_type.h"
25 #include "sim_utils.h"
26 #include "tel_event_handler.h"
27 #include "telephony_log_wrapper.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 class IccFileController : public TelEventHandler {
32 public:
33     explicit IccFileController(const std::string &name, int slotId);
34     virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
35     virtual ~IccFileController();
36     void ObtainBinaryFile(int fileId, const AppExecFwk::InnerEvent::Pointer &event);
37     void ObtainBinaryFile(int fileId, int size, const AppExecFwk::InnerEvent::Pointer &event);
38 
39     void ObtainLinearFixedFile(
40         int fileId, const std::string &path, int fileNum, const AppExecFwk::InnerEvent::Pointer &event);
41     void ObtainLinearFixedFile(int fileId, int fileNum, const AppExecFwk::InnerEvent::Pointer &event);
42 
43     void ObtainAllLinearFixedFile(
44         int fileId, const std::string &path, const AppExecFwk::InnerEvent::Pointer &event);
45     void ObtainAllLinearFixedFile(int fileId, const AppExecFwk::InnerEvent::Pointer &event);
46     void ObtainLinearFileSize(int fileId, const std::string &path, const AppExecFwk::InnerEvent::Pointer &event);
47     void ObtainLinearFileSize(int fileId, const AppExecFwk::InnerEvent::Pointer &event);
48 
49     void UpdateLinearFixedFile(int fileId, const std::string &path, int fileNum, const std::string data,
50         int dataLength, const std::string pin2, const AppExecFwk::InnerEvent::Pointer &onComplete);
51     void UpdateLinearFixedFile(int fileId, int fileNum, const std::string data, int dataLength,
52         const std::string pin2, const AppExecFwk::InnerEvent::Pointer &onComplete);
53     void UpdateBinaryFile(
54         int fileId, const std::string data, int dataLength, const AppExecFwk::InnerEvent::Pointer &onComplete);
55     void SetRilManager(std::shared_ptr<ITelRilManager> ril);
56     inline const static std::string NULLSTR = "";
57 
58 protected:
59     // 3GPP TS 51.011 V4.1.0 section 10.7 files of gsm
60     const std::string MASTER_FILE_SIM = "3F00";
61     const std::string DEDICATED_FILE_TELECOM = "7F10";
62     const std::string DEDICATED_FILE_GSM = "7F20";
63     const std::string DEDICATED_FILE_GRAPHICS = "5F50";
64     // ETSI TS 102 221 V3.3.0 section 8.6 reservation of file IDs
65     const std::string DEDICATED_FILE_DIALLING_NUMBERS = "5F3A";
66     const std::string DEDICATED_FILE_ADF = "7FFF";
67     std::shared_ptr<Telephony::ITelRilManager> telRilManager_ = nullptr;
68     int slotId_ = 0;
69     virtual std::string ObtainElementFilePath(int efId) = 0;
70     std::string ObtainElementFileForPublic(int efId);
71     void SendResponse(std::shared_ptr<IccControllerHolder> holder, const IccFileData *fd);
72     void SendEfLinearResult(const AppExecFwk::InnerEvent::Pointer &response, const int val[], int len);
73     void SendMultiRecordResult(const AppExecFwk::InnerEvent::Pointer &response, std::vector<std::string> &strValue);
74     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, std::shared_ptr<IccControllerHolder> &holderObject);
75     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(
76         int eventId, int arg1, int arg2, std::shared_ptr<IccControllerHolder> &holderObject);
77     AppExecFwk::InnerEvent::Pointer BuildCallerInfo(
78         int eventId, int arg1, int arg2, const AppExecFwk::InnerEvent::Pointer &msg);
79     void ProcessBinarySize(const AppExecFwk::InnerEvent::Pointer &event);
80     void ProcessRecordSize(const AppExecFwk::InnerEvent::Pointer &event);
81     void ProcessLinearRecordSize(const AppExecFwk::InnerEvent::Pointer &event);
82     void ProcessReadRecord(const AppExecFwk::InnerEvent::Pointer &event);
83     void ProcessReadBinary(const AppExecFwk::InnerEvent::Pointer &event);
84     void SendEvent(std::shared_ptr<AppExecFwk::EventHandler> handler, uint32_t id, bool needShare,
85         std::shared_ptr<ControllerToFileMsg> objectShare, std::unique_ptr<ControllerToFileMsg> &objectUnique);
86 
87 private:
88     const int RECORD_NUM = 3;
89     const uint32_t OFFSET = 8;
90     const uint8_t BYTE_NUM = 0xff;
91     const int MAX_FILE_INDEX = 2;
92     const int ICC_FILE_CURRENT_MODE = 4;
93     void ParseFileSize(int val[], int len, const unsigned char *data);
94     bool IsValidRecordSizeData(const unsigned char *data);
95     bool IsValidBinarySizeData(const unsigned char *data);
96     void GetFileAndDataSize(const unsigned char *data, int &dataSize, int &fileSize);
97     void GetDataSize(const unsigned char *data, int &dataSize);
98     std::string CheckRightPath(const std::string &path, int fileId);
99     bool ProcessErrorResponse(const AppExecFwk::InnerEvent::Pointer &event);
100     bool IsFixedNumberType(int);
101 };
102 } // namespace Telephony
103 } // namespace OHOS
104 
105 #endif
106