• 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 #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     void SetSimState(SimState simState);
115     IccSimStatus GetSimIccStatus();
116     CardType GetCardType();
117     std::string GetIccid();
118     bool HasSimCard();
119     void ObtainRealtimeIccStatus(int32_t slotId);
120     void UnlockPin(int32_t slotId, const std::string &pin);
121     void UnlockPuk(int32_t slotId, const std::string &newPin, const std::string &puk);
122     void AlterPin(int32_t slotId, const std::string &newPin, const std::string &oldPin);
123     void SetLockState(int32_t slotId, const LockInfo &options);
124     void GetLockState(int32_t slotId, LockType lockType);
125     UnlockData GetUnlockData();
126     LockStatusResponse GetSimlockResponse();
127     bool ConnectService();
128     void UnlockPin2(int32_t slotId, const std::string &pin2);
129     void UnlockPuk2(int32_t slotId, const std::string &newPin2, const std::string &puk2);
130     void AlterPin2(int32_t slotId, const std::string &newPin2, const std::string &oldPin2);
131     void UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo);
132     void SetRilManager(std::weak_ptr<Telephony::ITelRilManager> telRilManager);
133     bool IsIccReady();
134     bool IsIccLocked();
135     void RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what);
136     void UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int what);
137     void RegisterSatelliteCallback();
138     void UnregisterSatelliteCallback();
139     int32_t SimAuthentication(int32_t slotId, AuthType authType, const std::string &authData);
140     SimAuthenticationResponse GetSimAuthenticationResponse();
141     void SendSimMatchedOperatorInfo(
142         int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey);
143     int32_t GetSendSimMatchedOperatorInfoResponse();
144     int32_t GetSimIO(int32_t slotId, SimIoRequestInfo requestInfo);
145     SimAuthenticationResponse GetSimIOResponse();
146     int32_t NotifySimSlotsMapping(int32_t slotId);
147 
148 public:
149     bool modemInitDone_ = false;
150     int32_t oldSimType_ = ICC_UNKNOWN_TYPE;
151     int32_t oldSimStatus_ = ICC_CONTENT_UNKNOWN;
152 
153 private:
154     void ObtainIccStatus(int32_t slotId);
155     void GetSimCardData(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
156     void GetSimLockState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
157     void GetSetLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
158     void GetUnlockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
159     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event);
160     void ProcessIccCardState(IccState &ar, int32_t slotId);
161     void UpdateAppInfo(IccState &ar, int32_t slotId);
162     bool PublishSimStateEvent(std::string event, int32_t eventCode, std::string eventData);
163     void SimStateEscape(int32_t simState, int32_t slotId, LockReason &reason);
164     void CardTypeEscape(int32_t simType, int32_t slotId);
165     void SimLockStateEscape(int32_t simState, int32_t slotId, LockReason &reason);
166     void NotifySimLock(int slotId);
167     void GetUnlockSimLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
168     void GetSimAuthenticationResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
169     void GetSendSimMatchedOperatorInfoResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
170     std::string GetAidByCardType(CardType type);
171     bool IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer &event);
172     int32_t IsSatelliteSupported();
173     void GetSimIOResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
174 #ifdef CORE_SERVICE_SUPPORT_ESIM
175     void UpdateEsimOSVersion(int32_t slotId);
176 #endif
177 
178 private:
179     static const std::map<uint32_t, Func> memberFuncMap_;
180     int32_t slotId_ = DEFAULT_SIM_SLOT_ID;
181     UnlockData unlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR, static_cast<int32_t>(LockState::LOCK_ERROR) };
182     SimAuthenticationResponse simAuthRespon_ = { 0 };
183     SimAuthenticationResponse simIORespon_ = { 0 };
184     int32_t sendSimMatchedOperatorInfoResult_ = static_cast<int32_t>(ErrType::NONE);
185     LockStatusResponse simlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR };
186     IccState iccState_; // icc card states
187     SimState externalState_; // need to broadcast sim state;
188     CardType externalType_ = CardType::UNKNOWN_CARD; // need to broadcast card type;
189     std::weak_ptr<SimStateManager> simStateManager_;
190     std::weak_ptr<Telephony::ITelRilManager> telRilManager_; // ril manager
191     std::unique_ptr<ObserverHandler> observerHandler_ = nullptr;
192     sptr<ISatelliteCoreCallback> satelliteCallback_ = nullptr;
193     std::string iccid_ = "";
194     int32_t esimSwitchState_ = 0;
195 };
196 } // namespace Telephony
197 } // namespace OHOS
198 
199 #endif // OHOS_SIM_STATE_HANDLE_H
200