1 /*
2  * Copyright (C) 2021-2024 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_H
17 #define OHOS_ICC_FILE_H
18 #include <shared_mutex>
19 
20 #include "common_event.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "i_tel_ril_manager.h"
24 #include "icc_dialling_numbers_handler.h"
25 #include "icc_file_controller.h"
26 #include "mcc_pool.h"
27 #include "observer_handler.h"
28 #include "plmn_file.h"
29 #include "sim_constant.h"
30 #include "sim_state_manager.h"
31 #include "tel_event_handler.h"
32 #include "telephony_log_wrapper.h"
33 #include "usim_function_handle.h"
34 #include "voice_mail_constants.h"
35 #include "want.h"
36 #include "i_icc_file.h"
37 
38 namespace OHOS {
39 namespace Telephony {
40 class IccFile : public IIccFileExt, public TelEventHandler {
41 public:
42     explicit IccFile(const std::string &name, std::shared_ptr<SimStateManager> simStateManager);
43     void Init();
44     virtual void StartLoad();
45     std::string ObtainIMSI();
46     virtual std::string ObtainMCC();
47     virtual std::string ObtainMNC();
48     void UpdateImsi(std::string imsi);
49     void UpdateIccId(std::string iccid);
50     std::string ObtainIccId();
51     std::string ObtainDecIccId();
52     std::string ObtainGid1();
53     std::string ObtainGid2();
54     std::string ObtainMsisdnNumber();
55     virtual std::string ObtainSimOperator();
56     virtual std::string ObtainIsoCountryCode();
57     virtual int ObtainCallForwardStatus();
58     virtual int ObtainSpnCondition(bool roaming, const std::string &operatorNum) = 0;
59     bool LoadedOrNot();
60     void UpdateLoaded(bool loaded);
61     virtual bool UpdateMsisdnNumber(
62         const std::string &alphaTag, const std::string &number);
63     virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
64     virtual ~IccFile();
65     virtual bool ProcessIccReady(const AppExecFwk::InnerEvent::Pointer &event) = 0;
66     std::string ObtainDiallingNumberInfo();
67     std::string ObtainNAI();
68     std::string ObtainHomeNameOfPnn();
69     std::string ObtainMsisdnAlphaStatus();
70     int32_t ObtainVoiceMailCount();
71     std::string ObtainSPN();
72     std::string ObtainEons(const std::string &plmn, int32_t lac, bool longNameRequired);
73     std::string ObtainVoiceMailInfo();
74     bool ObtainFilesFetched();
75     std::string ObtainIccLanguage();
76     virtual std::shared_ptr<UsimFunctionHandle> ObtainUsimFunctionHandle();
77     std::string ObtainSpNameFromEfSpn();
78     int ObtainLengthOfMnc();
79     virtual void RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what);
80     virtual void UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int what);
81     void SetRilAndFileController(const std::shared_ptr<Telephony::ITelRilManager> &ril,
82         const std::shared_ptr<IccFileController> &file, const std::shared_ptr<IccDiallingNumbersHandler> &handler);
83     struct IccFileLoaded {
84         virtual std::string ObtainElementaryFileName() = 0;
85         virtual void ProcessParseFile(const AppExecFwk::InnerEvent::Pointer &event) = 0;
86     };
87     virtual bool UpdateVoiceMail(const std::string &mailName, const std::string &mailNumber) = 0;
88     virtual bool SetVoiceMailCount(int32_t voiceMailCount) = 0;
89     virtual bool SetVoiceCallForwarding(bool enable, const std::string &number) = 0;
90     virtual std::string GetVoiceMailNumber() = 0;
91     virtual void SetVoiceMailNumber(const std::string mailNumber) = 0;
92     virtual void ProcessIccRefresh(int msgId) = 0;
93     bool HasSimCard();
94     void UnInit();
95     virtual void ClearData();
96     void ProcessIccLocked();
97     void SetId(int id);
98     bool GetIsVoiceMailFixed();
99     void LoadVoiceMail();
100     bool ExecutOriginalSimIoRequest(int32_t filedId, int fileIdDone);
101     void OnOpkeyLoad(const std::string opKey, const std::string opName);
102     void FileChangeToExt(const std::string fileName, const FileChangeType fileLoad);
103     void ProcessExtGetFileDone(const AppExecFwk::InnerEvent::Pointer &event);
104     void SetIccFile(std::shared_ptr<OHOS::Telephony::IIccFileExt> &iccFile);
105     void AddRecordsToLoadNum();
106     void DeleteOperatorCache();
107     void UpdateOpkeyConfig();
108     virtual void ProcessExtGetFileResponse();
109 
110 protected:
111     virtual void ProcessFileLoaded(bool response) = 0;
112     virtual void OnAllFilesFetched() = 0;
113     bool LockQueriedOrNot();
114     void UpdateSPN(const std::string spn);
115     void SaveCountryCode();
116     std::shared_ptr<Telephony::ITelRilManager> telRilManager_ = nullptr;
117     std::shared_ptr<IccFileController> fileController_ = nullptr;
118     std::shared_ptr<SimStateManager> stateManager_ = nullptr;
119     std::string imsi_ = "";
120     std::string iccId_ = "";
121     std::string decIccId_ = "";
122     std::string spn_ = "";
123     std::string gid1_ = "";
124     std::string gid2_ = "";
125     std::string msisdn_ = "";
126     std::string msisdnTag_ = "";
127     std::string lastMsisdn_ = "";
128     std::string lastMsisdnTag_ = "";
129     std::string voiceMailNum_ = "";
130     int32_t efMWISSize_ = 0;
131     int32_t efCphsMwiSize_ = 0;
132     int32_t efCfisSize_ = 0;
133     int32_t efCffSize_ = 0;
134     int32_t callForwardingStatus = CALL_FORWARDING_STATUS_UNKNOWN;
135     bool voiceMailWaiting_ = false;
136     int32_t voiceMailCount_ = DEFAULT_VOICE_MAIL_COUNT;
137     std::string voiceMailTag_ = "";
138     std::string lastVoiceMailNum_ = "";
139     std::string lastVoiceMailTag_ = "";
140     std::string operatorNumeric_ = "";
141     std::string mcc_ = "";
142     std::string mnc_ = "";
143     bool voiceMailFixedOrNot_ = false;
144     std::string pnnHomeName_ = "";
145     std::string iccLanguage_ = "";
146     PlmnFile *hplmnRAT_ = nullptr;
147     PlmnFile *oplmnRAT_ = nullptr;
148     PlmnFile *plmnRAT_ = nullptr;
149     std::string ehplmns_ = "";
150     std::string fplmns_ = "";
151     std::vector<std::shared_ptr<PlmnNetworkName>> pnnFiles_;
152     std::vector<std::shared_ptr<OperatorPlmnInfo>> oplFiles_;
153     std::vector<std::shared_ptr<OperatorPlmnInfo>> opl5gFiles_;
154     int lengthOfMnc_ = UNINITIALIZED_MNC;
155     int indexOfMailbox_ = 1;
156     int fileToGet_ = 0;
157     bool loaded_ = false;
158     bool fileQueried_ = false;
159     bool lockQueried_ = false;
160     bool waitResult_ = false;
161     static std::mutex mtx_;
162     std::condition_variable processWait_;
163     const int HEXADECIMAL = 16;
164     const size_t OPL_5G_LENGTH = 10;
165     const uint8_t BYTE_NUM = 0xFF;
166     const uint8_t BYTE_NUM2 = 0x01;
167     const uint8_t BYTE_NUM3 = 0x0F;
168     const uint8_t BYTE_NUM4 = 0x0A;
169     const uint8_t BYTE_NUM5 = 0x05;
170     const uint8_t BYTE_NUM6 = 0xFE;
171     const uint8_t BYTE_NUM7 = 0xF0;
172     const int DATA_STEP = 2;
173     const unsigned long LENGTH_TWO = 2;
174     static std::unique_ptr<ObserverHandler> filesFetchedObser_;
175     std::unique_ptr<ObserverHandler> lockedFilesFetchedObser_ = nullptr;
176     std::unique_ptr<ObserverHandler> networkLockedFilesFetchedObser_ = nullptr;
177     std::unique_ptr<ObserverHandler> imsiReadyObser_ = nullptr;
178     std::unique_ptr<ObserverHandler> recordsEventsObser_ = nullptr;
179     std::unique_ptr<ObserverHandler> networkSelectionModeAutomaticObser_ = nullptr;
180     std::unique_ptr<ObserverHandler> spnUpdatedObser_ = nullptr;
181     std::unique_ptr<ObserverHandler> recordsOverrideObser_ = nullptr;
182     std::unique_ptr<ObserverHandler> opkeyLoadObser_ = nullptr;
183     std::unique_ptr<ObserverHandler> iccidLoadObser_ = nullptr;
184     std::unique_ptr<ObserverHandler> operatorCacheDelObser_ = nullptr;
185     virtual AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId);
186     virtual AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, int arg1, int arg2);
187     virtual AppExecFwk::InnerEvent::Pointer BuildCallerInfo(int eventId, std::shared_ptr<void> loader);
188     bool PublishSimFileEvent(const std::string &event, int eventCode, const std::string &eventData);
189     void UpdateIccLanguage(const std::string &langLi, const std::string &langPl);
190     std::string ObtainValidLanguage(const std::string &langData);
191     void SwapPairsForIccId(std::string &iccId);
192     void GetFullIccid(std::string &iccId);
193     std::shared_ptr<IccDiallingNumbersHandler> diallingNumberHandler_ = nullptr;
194     AppExecFwk::InnerEvent::Pointer CreateDiallingNumberPointer(
195         int eventid, int efId, int index, std::shared_ptr<void> pobj);
196     void NotifyRegistrySimState(CardType type, SimState state, LockReason reason);
197     int slotId_ = 0;
198     void SetVoiceMailByOperator(std::string spn);
199     std::shared_ptr<VoiceMailConstants> voiceMailConfig_ = nullptr;
200     std::shared_mutex voiceMailMutex_;
201 
202 private:
203     bool isVoiceMailFixed_ = false;
204     void ResetVoiceMailVariable();
205     bool ProcessIccFileObtained(const AppExecFwk::InnerEvent::Pointer &event);
206     void RegisterImsiLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
207     void UnregisterImsiLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler);
208     void RegisterAllFilesLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
209     void UnregisterAllFilesLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler);
210     void RegisterOpkeyLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
211     void UnregisterOpkeyLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler);
212     void RegisterOperatorCacheDel(std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
213     void UnregisterOperatorCacheDel(const std::shared_ptr<AppExecFwk::EventHandler> &handler);
214     void RegisterIccidLoaded(std::shared_ptr<AppExecFwk::EventHandler> eventHandler);
215     void UnregisterIccidLoaded(const std::shared_ptr<AppExecFwk::EventHandler> &handler);
216     void AddRecordsOverrideObser();
217     void AddOpkeyLoadObser();
218     void AddOperatorCacheDelObser();
219     void AddIccidLoadObser();
220 };
221 } // namespace Telephony
222 } // namespace OHOS
223 
224 #endif // OHOS_ICC_FILE_H
225