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_SIM_STATE_HANDLE_H
17 #define OHOS_SIM_STATE_HANDLE_H
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <list>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <vector>
26 
27 #include "i_sim_manager.h"
28 #include "i_tel_ril_manager.h"
29 #include "icc_state.h"
30 #include "observer_handler.h"
31 #include "satellite_core_callback.h"
32 #include "sim_state_type.h"
33 #include "tel_event_handler.h"
34 #include "telephony_errors.h"
35 #include "want.h"
36 
37 namespace OHOS {
38 namespace Telephony {
39 class SimStateManager;
40 
41 enum UnlockType {
42     PIN_TYPE,
43     PUK_TYPE,
44 };
45 
46 enum UnlockCmd {
47     REQUEST_UNLOCK_PIN,
48     REQUEST_UNLOCK_PUK,
49     REQUEST_UNLOCK_REMAIN,
50 };
51 
52 // pin/puk password incorect
53 const int UNLOCK_PIN_PUK_INCORRECT = 16; // incorrect password
54 // Phone number
55 const int SIM_CARD_NUM = 2;
56 // The events for handleMessage
57 const int MSG_SIM_GET_ICC_STATUS_DONE = 3;
58 // Unlock pin
59 const int MSG_SIM_UNLOCK_PIN_DONE = 4;
60 // Unlock puk
61 const int MSG_SIM_UNLOCK_PUK_DONE = 5;
62 // Change pin
63 const int MSG_SIM_CHANGE_PIN_DONE = 6;
64 // Check pin state
65 const int MSG_SIM_CHECK_PIN_DONE = 7;
66 // Set pin state[0:close_lock_state], [1:open_lock_state]
67 const int MSG_SIM_ENABLE_PIN_DONE = 8;
68 // Get sim unlock pin remain
69 const int MSG_SIM_UNLOCK_REMAIN_DONE = 10;
70 // Get sim realtime icc state
71 const int MSG_SIM_GET_REALTIME_ICC_STATUS_DONE = 21;
72 // Unlock pin2
73 const int MSG_SIM_UNLOCK_PIN2_DONE = 31;
74 // Unlock puk2
75 const int MSG_SIM_UNLOCK_PUK2_DONE = 32;
76 // Change pin2
77 const int MSG_SIM_CHANGE_PIN2_DONE = 33;
78 // Get sim unlock pin2 remain
79 const int MSG_SIM_UNLOCK_PIN2_REMAIN_DONE = 34;
80 // Unlock simlock
81 const int MSG_SIM_UNLOCK_SIMLOCK_DONE = 51;
82 
83 const int MSG_SIM_AUTHENTICATION_DONE = 61;
84 
85 const int MSG_SIM_SEND_NCFG_OPER_INFO_DONE = 62;
86 
87 const int MSG_SIM_GET_SIM_IO_DONE = 71;
88 
89 // pin lock type
90 constexpr const char *FAC_PIN_LOCK = "SC";
91 // change pin2 type
92 constexpr const char *FDN_PIN_LOCK = "P2";
93 // FDN lock type
94 constexpr const char *FDN_PIN2_LOCK = "FD";
95 
96 constexpr const char *USIM_AID = "USIM_AID";
97 constexpr const char *CDMA_FAKE_AID = "CDMA_FAKE_AID";
98 constexpr const char *GSM_FAKE_AID = "GSM_FAKE_AID";
99 
100 struct UnlockData {
101     int32_t result = 0;
102     int32_t remain = 0;
103     int32_t lockState = 0;
104 };
105 
106 class SimStateHandle : public TelEventHandler {
107 public:
108     using Func = std::function<void(SimStateHandle *, int32_t, const AppExecFwk::InnerEvent::Pointer &)>;
109     explicit SimStateHandle(const std::weak_ptr<SimStateManager> &simStateManager);
110     ~SimStateHandle() = default;
111     void Init(int32_t slotId);
112     void UnInit();
113     SimState GetSimState();
114     CardType GetCardType();
115     std::string GetIccid();
116     bool HasSimCard();
117     void ObtainRealtimeIccStatus(int32_t slotId);
118     void UnlockPin(int32_t slotId, const std::string &pin);
119     void UnlockPuk(int32_t slotId, const std::string &newPin, const std::string &puk);
120     void AlterPin(int32_t slotId, const std::string &newPin, const std::string &oldPin);
121     void SetLockState(int32_t slotId, const LockInfo &options);
122     void GetLockState(int32_t slotId, LockType lockType);
123     UnlockData GetUnlockData();
124     LockStatusResponse GetSimlockResponse();
125     bool ConnectService();
126     void UnlockPin2(int32_t slotId, const std::string &pin2);
127     void UnlockPuk2(int32_t slotId, const std::string &newPin2, const std::string &puk2);
128     void AlterPin2(int32_t slotId, const std::string &newPin2, const std::string &oldPin2);
129     void UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo);
130     void SetRilManager(std::weak_ptr<Telephony::ITelRilManager> telRilManager);
131     bool IsIccReady();
132     bool IsIccLocked();
133     void RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what);
134     void UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int what);
135     void RegisterSatelliteCallback();
136     void UnregisterSatelliteCallback();
137     int32_t SimAuthentication(int32_t slotId, AuthType authType, const std::string &authData);
138     SimAuthenticationResponse GetSimAuthenticationResponse();
139     void SendSimMatchedOperatorInfo(
140         int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey);
141     int32_t GetSendSimMatchedOperatorInfoResponse();
142     int32_t GetSimIO(int32_t slotId, SimIoRequestInfo requestInfo);
143     SimAuthenticationResponse GetSimIOResponse();
144 
145 public:
146     bool modemInitDone_ = false;
147 
148 private:
149     void ObtainIccStatus(int32_t slotId);
150     void GetSimCardData(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
151     void GetSimLockState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
152     void GetSetLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
153     void GetUnlockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
154     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
155     void ProcessIccCardState(IccState &ar, int32_t slotId);
156     void UpdateAppInfo(IccState &ar, int32_t slotId);
157     bool PublishSimStateEvent(std::string event, int32_t eventCode, std::string eventData);
158     void SimStateEscape(int32_t simState, int32_t slotId, LockReason &reason);
159     void CardTypeEscape(int32_t simType, int32_t slotId);
160     void SimLockStateEscape(int32_t simState, int32_t slotId, LockReason &reason);
161     void NotifySimLock(int slotId);
162     void GetUnlockSimLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
163     void GetSimAuthenticationResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
164     void GetSendSimMatchedOperatorInfoResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
165     std::string GetAidByCardType(CardType type);
166     bool IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer &event);
167     int32_t IsSatelliteSupported();
168     void GetSimIOResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
169 
170 private:
171     static const std::map<uint32_t, Func> memberFuncMap_;
172     int32_t oldSimType_ = ICC_UNKNOWN_TYPE;
173     int32_t oldSimStatus_ = ICC_CONTENT_UNKNOWN;
174     int32_t slotId_ = DEFAULT_SIM_SLOT_ID;
175     UnlockData unlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR, static_cast<int32_t>(LockState::LOCK_ERROR) };
176     SimAuthenticationResponse simAuthRespon_ = { 0 };
177     SimAuthenticationResponse simIORespon_ = { 0 };
178     int32_t sendSimMatchedOperatorInfoResult_ = static_cast<int32_t>(ErrType::NONE);
179     LockStatusResponse simlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR };
180     IccState iccState_; // icc card states
181     SimState externalState_; // need to broadcast sim state;
182     CardType externalType_ = CardType::UNKNOWN_CARD; // need to broadcast card type;
183     std::weak_ptr<SimStateManager> simStateManager_;
184     std::weak_ptr<Telephony::ITelRilManager> telRilManager_; // ril manager
185     std::unique_ptr<ObserverHandler> observerHandler_ = nullptr;
186     sptr<ISatelliteCoreCallback> satelliteCallback_ = nullptr;
187     std::string iccid_ = "";
188 };
189 } // namespace Telephony
190 } // namespace OHOS
191 
192 #endif // OHOS_SIM_STATE_HANDLE_H
193