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 <list> 20 #include <memory> 21 #include <mutex> 22 #include <condition_variable> 23 #include <chrono> 24 #include <string> 25 #include <vector> 26 27 #include "event_handler.h" 28 #include "event_runner.h" 29 #include "want.h" 30 #include "i_tel_ril_manager.h" 31 #include "i_sim_manager.h" 32 #include "icc_state.h" 33 #include "observer_handler.h" 34 #include "sim_state_type.h" 35 36 namespace OHOS { 37 namespace Telephony { 38 class SimStateManager; 39 40 enum UnlockType { 41 PIN_TYPE, 42 PUK_TYPE, 43 }; 44 45 enum UnlockCmd { 46 REQUEST_UNLOCK_PIN, 47 REQUEST_UNLOCK_PUK, 48 REQUEST_UNLOCK_REMAIN, 49 }; 50 51 // pin/puk password incorect 52 const int UNLOCK_PIN_PUK_INCORRECT = 16; // incorrect password 53 // Phone number 54 const int SIM_CARD_NUM = 2; 55 // The events for handleMessage 56 const int MSG_SIM_GET_ICC_STATUS_DONE = 3; 57 // Unlock pin 58 const int MSG_SIM_UNLOCK_PIN_DONE = 4; 59 // Unlock puk 60 const int MSG_SIM_UNLOCK_PUK_DONE = 5; 61 // Change pin 62 const int MSG_SIM_CHANGE_PIN_DONE = 6; 63 // Check pin state 64 const int MSG_SIM_CHECK_PIN_DONE = 7; 65 // Set pin state[0:close_lock_state], [1:open_lock_state] 66 const int MSG_SIM_ENABLE_PIN_DONE = 8; 67 // Get sim unlock pin remain 68 const int MSG_SIM_UNLOCK_REMAIN_DONE = 10; 69 // Get sim realtime icc state 70 const int MSG_SIM_GET_REALTIME_ICC_STATUS_DONE = 21; 71 // Unlock pin2 72 const int MSG_SIM_UNLOCK_PIN2_DONE = 31; 73 // Unlock puk2 74 const int MSG_SIM_UNLOCK_PUK2_DONE = 32; 75 // Change pin2 76 const int MSG_SIM_CHANGE_PIN2_DONE = 33; 77 // Get sim unlock pin2 remain 78 const int MSG_SIM_UNLOCK_PIN2_REMAIN_DONE = 34; 79 // Unlock simlock 80 const int MSG_SIM_UNLOCK_SIMLOCK_DONE = 51; 81 82 // pin lock type 83 const std::string FAC_PIN_LOCK = "SC"; 84 // change pin2 type 85 const std::string FDN_PIN_LOCK = "P2"; 86 // FDN lock type 87 const std::string FDN_PIN2_LOCK = "FD"; 88 89 struct UnlockData { 90 UnlockType type; 91 int32_t lockState; 92 int32_t result; 93 int32_t remain; 94 int32_t pinRemain; 95 int32_t pin2Remain; 96 int32_t puk2Remain; 97 }; 98 99 class SimStateHandle : public AppExecFwk::EventHandler { 100 public: 101 SimStateHandle(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 102 const std::weak_ptr<SimStateManager> &simStateManager); 103 ~SimStateHandle(); 104 void Init(int32_t slotId); 105 SimState GetSimState(); 106 CardType GetCardType(); 107 bool HasSimCard(); 108 void ObtainRealtimeIccStatus(int32_t slotId); 109 void UnlockPin(int32_t slotId, std::string pin); 110 void UnlockPuk(int32_t slotId, std::string newPin, std::string puk); 111 void AlterPin(int32_t slotId, std::string newPin, std::string oldPin); 112 void UnlockRemain(int32_t slotId); 113 void SetLockState(int32_t slotId, const LockInfo &options); 114 void GetLockState(int32_t slotId, LockType lockType); 115 UnlockData GetUnlockData(); 116 LockStatusResponse GetSimlockResponse(); 117 bool ConnectService(); 118 void UnlockPin2(int32_t slotId, std::string pin2); 119 void UnlockPuk2(int32_t slotId, std::string newPin2, std::string puk2); 120 void AlterPin2(int32_t slotId, std::string newPin2, std::string oldPin2); 121 void UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo); 122 void SetRilManager(std::shared_ptr<Telephony::ITelRilManager> telRilManager); 123 const std::string SIM_STATE_ACTION = "com.hos.action.SIM_STATE_CHANGED"; 124 bool IsIccReady(); 125 void RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what); 126 void UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int what); 127 128 private: 129 void SyncCmdResponse(); 130 void ObtainIccStatus(int32_t slotId); 131 void GetSimCardData(const AppExecFwk::InnerEvent::Pointer &event, int32_t slotId); 132 void GetSimLockState(const AppExecFwk::InnerEvent::Pointer &event, int32_t slotId); 133 void GetSetLockResult(const AppExecFwk::InnerEvent::Pointer &event, int32_t slotId); 134 void GetUnlockReult(const AppExecFwk::InnerEvent::Pointer &event, int32_t slotId); 135 void GetUnlockRemain(const AppExecFwk::InnerEvent::Pointer &event, int32_t slotId); 136 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event); 137 void ProcessIccCardState(IccState &ar, int32_t slotId); 138 void UpdateAppInfo(IccState &ar, int32_t slotId); 139 bool PublishSimStateEvent(std::string event, int32_t eventCode, std::string eventData); 140 void SimStateEscape(int32_t simState, int32_t slotId, LockReason &reason); 141 void CardTypeEscape(int32_t simType, int32_t slotId); 142 void SimLockStateEscape(int32_t simState, int32_t slotId, LockReason &reason); 143 void NotifySimLock(int slotId); 144 void GetUnlockSimLockResult(const AppExecFwk::InnerEvent::Pointer &event, int32_t slotId); 145 146 private: 147 int32_t oldSimType_ = ICC_UNKNOWN_TYPE; 148 int32_t oldSimStatus_ = ICC_CONTENT_UNKNOWN; 149 int32_t slotId_ = DEFAULT_SIM_SLOT_ID; 150 UnlockData unlockRespon_; 151 LockStatusResponse simlockRespon_ = {0}; 152 IccState iccState_; // icc card states 153 SimState externalState_; // need to broadcast sim state; 154 CardType externalType_; // need to broadcast card type; 155 std::weak_ptr<SimStateManager> simStateManager_; 156 std::shared_ptr<Telephony::ITelRilManager> telRilManager_ = nullptr; // ril manager 157 std::unique_ptr<ObserverHandler> observerHandler_ = nullptr; 158 }; 159 } // namespace Telephony 160 } // namespace OHOS 161 162 #endif // OHOS_SIM_STATE_HANDLE_H 163