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 // pin lock type 88 constexpr const char *FAC_PIN_LOCK = "SC"; 89 // change pin2 type 90 constexpr const char *FDN_PIN_LOCK = "P2"; 91 // FDN lock type 92 constexpr const char *FDN_PIN2_LOCK = "FD"; 93 94 constexpr const char *USIM_AID = "USIM_AID"; 95 constexpr const char *CDMA_FAKE_AID = "CDMA_FAKE_AID"; 96 constexpr const char *GSM_FAKE_AID = "GSM_FAKE_AID"; 97 98 struct UnlockData { 99 int32_t result = 0; 100 int32_t remain = 0; 101 int32_t lockState = 0; 102 }; 103 104 class SimStateHandle : public TelEventHandler { 105 public: 106 using Func = void (SimStateHandle::*)(int32_t, const AppExecFwk::InnerEvent::Pointer &); 107 explicit SimStateHandle(const std::weak_ptr<SimStateManager> &simStateManager); 108 ~SimStateHandle() = default; 109 void Init(int32_t slotId); 110 void UnInit(); 111 SimState GetSimState(); 112 CardType GetCardType(); 113 bool HasSimCard(); 114 void ObtainRealtimeIccStatus(int32_t slotId); 115 void UnlockPin(int32_t slotId, const std::string &pin); 116 void UnlockPuk(int32_t slotId, const std::string &newPin, const std::string &puk); 117 void AlterPin(int32_t slotId, const std::string &newPin, const std::string &oldPin); 118 void SetLockState(int32_t slotId, const LockInfo &options); 119 void GetLockState(int32_t slotId, LockType lockType); 120 UnlockData GetUnlockData(); 121 LockStatusResponse GetSimlockResponse(); 122 bool ConnectService(); 123 void UnlockPin2(int32_t slotId, const std::string &pin2); 124 void UnlockPuk2(int32_t slotId, const std::string &newPin2, const std::string &puk2); 125 void AlterPin2(int32_t slotId, const std::string &newPin2, const std::string &oldPin2); 126 void UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo); 127 void SetRilManager(std::weak_ptr<Telephony::ITelRilManager> telRilManager); 128 bool IsIccReady(); 129 void RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what); 130 void UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int what); 131 void RegisterSatelliteCallback(); 132 void UnregisterSatelliteCallback(); 133 int32_t SimAuthentication(int32_t slotId, AuthType authType, const std::string &authData); 134 SimAuthenticationResponse GetSimAuthenticationResponse(); 135 void SendSimMatchedOperatorInfo( 136 int32_t slotId, int32_t state, const std::string &operName, const std::string &operKey); 137 int32_t GetSendSimMatchedOperatorInfoResponse(); 138 139 public: 140 bool modemInitDone_ = false; 141 142 private: 143 void SyncCmdResponse(); 144 void ObtainIccStatus(int32_t slotId); 145 void GetSimCardData(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 146 void GetSimLockState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 147 void GetSetLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 148 void GetUnlockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 149 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event); 150 void ProcessIccCardState(IccState &ar, int32_t slotId); 151 void UpdateAppInfo(IccState &ar, int32_t slotId); 152 bool PublishSimStateEvent(std::string event, int32_t eventCode, std::string eventData); 153 void SimStateEscape(int32_t simState, int32_t slotId, LockReason &reason); 154 void CardTypeEscape(int32_t simType, int32_t slotId); 155 void SimLockStateEscape(int32_t simState, int32_t slotId, LockReason &reason); 156 void NotifySimLock(int slotId); 157 void GetUnlockSimLockResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 158 void GetSimAuthenticationResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 159 void GetSendSimMatchedOperatorInfoResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event); 160 std::string GetAidByCardType(CardType type); 161 bool IsRadioStateUnavailable(const AppExecFwk::InnerEvent::Pointer &event); 162 int32_t IsSatelliteSupported(); 163 164 private: 165 static const std::map<uint32_t, Func> memberFuncMap_; 166 int32_t oldSimType_ = ICC_UNKNOWN_TYPE; 167 int32_t oldSimStatus_ = ICC_CONTENT_UNKNOWN; 168 int32_t slotId_ = DEFAULT_SIM_SLOT_ID; 169 UnlockData unlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR, static_cast<int32_t>(LockState::LOCK_ERROR) }; 170 SimAuthenticationResponse simAuthRespon_ = { 0 }; 171 int32_t sendSimMatchedOperatorInfoResult_ = static_cast<int32_t>(HRilErrType::NONE); 172 LockStatusResponse simlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR }; 173 IccState iccState_; // icc card states 174 SimState externalState_; // need to broadcast sim state; 175 CardType externalType_ = CardType::UNKNOWN_CARD; // need to broadcast card type; 176 std::weak_ptr<SimStateManager> simStateManager_; 177 std::weak_ptr<Telephony::ITelRilManager> telRilManager_; // ril manager 178 std::unique_ptr<ObserverHandler> observerHandler_ = nullptr; 179 sptr<ISatelliteCoreCallback> satelliteCallback_ = nullptr; 180 }; 181 } // namespace Telephony 182 } // namespace OHOS 183 184 #endif // OHOS_SIM_STATE_HANDLE_H 185