• 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 <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 #include "telephony_errors.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 // pin lock type
86 constexpr const char *FAC_PIN_LOCK = "SC";
87 // change pin2 type
88 constexpr const char *FDN_PIN_LOCK = "P2";
89 // FDN lock type
90 constexpr const char *FDN_PIN2_LOCK = "FD";
91 
92 struct UnlockData {
93     int32_t result;
94     int32_t remain;
95     int32_t lockState;
96 };
97 
98 class SimStateHandle : public AppExecFwk::EventHandler {
99 public:
100     SimStateHandle(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
101         const std::weak_ptr<SimStateManager> &simStateManager);
102     ~SimStateHandle() = default;
103     void Init(int32_t slotId);
104     void UnInit();
105     SimState GetSimState();
106     CardType GetCardType();
107     bool HasSimCard();
108     void ObtainRealtimeIccStatus(int32_t slotId);
109     void UnlockPin(int32_t slotId, const std::string &pin);
110     void UnlockPuk(int32_t slotId, const std::string &newPin, const std::string &puk);
111     void AlterPin(int32_t slotId, const std::string &newPin, const std::string &oldPin);
112     void SetLockState(int32_t slotId, const LockInfo &options);
113     void GetLockState(int32_t slotId, LockType lockType);
114     UnlockData GetUnlockData();
115     LockStatusResponse GetSimlockResponse();
116     bool ConnectService();
117     void UnlockPin2(int32_t slotId, const std::string &pin2);
118     void UnlockPuk2(int32_t slotId, const std::string &newPin2, const std::string &puk2);
119     void AlterPin2(int32_t slotId, const std::string &newPin2, const std::string &oldPin2);
120     void UnlockSimLock(int32_t slotId, const PersoLockInfo &lockInfo);
121     void SetRilManager(std::shared_ptr<Telephony::ITelRilManager> telRilManager);
122     bool IsIccReady();
123     void RegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &handler, int what);
124     void UnRegisterCoreNotify(const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int what);
125     int32_t SimAuthentication(int32_t slotId, const std::string &aid, const std::string &authData);
126     SimAuthenticationResponse GetSimAuthenticationResponse();
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 GetUnlockResult(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     void GetSimAuthenticationResult(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event);
146 
147 private:
148     int32_t oldSimType_ = ICC_UNKNOWN_TYPE;
149     int32_t oldSimStatus_ = ICC_CONTENT_UNKNOWN;
150     int32_t slotId_ = DEFAULT_SIM_SLOT_ID;
151     UnlockData unlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR, static_cast<int32_t>(LockState::LOCK_ERROR) };
152     SimAuthenticationResponse simAuthRespon_ = { 0 };
153     LockStatusResponse simlockRespon_ = { UNLOCK_FAIL, TELEPHONY_ERROR };
154     IccState iccState_; // icc card states
155     SimState externalState_; // need to broadcast sim state;
156     CardType externalType_ = CardType::UNKNOWN_CARD; // need to broadcast card type;
157     std::weak_ptr<SimStateManager> simStateManager_;
158     std::shared_ptr<Telephony::ITelRilManager> telRilManager_ = nullptr; // ril manager
159     std::unique_ptr<ObserverHandler> observerHandler_ = nullptr;
160 };
161 } // namespace Telephony
162 } // namespace OHOS
163 
164 #endif // OHOS_SIM_STATE_HANDLE_H
165