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 #include "sim_state_tracker.h"
17
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "core_manager_inner.h"
21 #include "radio_event.h"
22 #include "telephony_ext_wrapper.h"
23 #include "thread"
24
25 namespace OHOS {
26 namespace Telephony {
27 constexpr int32_t OPKEY_VMSG_LENTH = 3;
28 constexpr const char *IS_UPDATE_OPERATORCONFIG = "telephony.is_update_operatorconfig";
29 constexpr const char *IS_BLOCK_LOAD_OPERATORCONFIG = "telephony.is_block_load_operatorconfig";
SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,std::shared_ptr<OperatorConfigCache> operatorConfigCache,int32_t slotId)30 SimStateTracker::SimStateTracker(std::weak_ptr<SimFileManager> simFileManager,
31 std::shared_ptr<OperatorConfigCache> operatorConfigCache, int32_t slotId)
32 : TelEventHandler("SimStateTracker"), simFileManager_(simFileManager), operatorConfigCache_(operatorConfigCache),
33 slotId_(slotId)
34 {
35 if (simFileManager.lock() == nullptr) {
36 TELEPHONY_LOGE("can not make OperatorConfigLoader");
37 }
38 operatorConfigLoader_ = std::make_shared<OperatorConfigLoader>(simFileManager, operatorConfigCache);
39 }
40
~SimStateTracker()41 SimStateTracker::~SimStateTracker()
42 {
43 if (statusChangeListener_ != nullptr) {
44 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (samgrProxy != nullptr) {
46 samgrProxy->UnSubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
47 samgrProxy->UnSubscribeSystemAbility(OHOS::COMMON_EVENT_SERVICE_ID, statusChangeListener_);
48 statusChangeListener_ = nullptr;
49 }
50 }
51 }
52
ProcessSimRecordLoad(const AppExecFwk::InnerEvent::Pointer & event)53 void SimStateTracker::ProcessSimRecordLoad(const AppExecFwk::InnerEvent::Pointer &event)
54 {
55 auto slotId = event->GetParam();
56 if (slotId != slotId_) {
57 TELEPHONY_LOGE("is not current slotId");
58 return;
59 }
60 char isBlockLoadOperatorConfig[SYSPARA_SIZE] = { 0 };
61 GetParameter(IS_BLOCK_LOAD_OPERATORCONFIG, "false", isBlockLoadOperatorConfig, SYSPARA_SIZE);
62 if (strcmp(isBlockLoadOperatorConfig, "true") == 0) {
63 return;
64 }
65 if (operatorConfigLoader_ == nullptr) {
66 TELEPHONY_LOGE("operatorConfigLoader is null!");
67 return;
68 }
69 TELEPHONY_LOGI("slotId: %{public}d need trigger LoadOperatorConfig", slotId_);
70 if (IsNeedUpdateCarrierConfig()) {
71 operatorConfigLoader_->LoadOperatorConfig(slotId_, operatorConfigCache_->STATE_PARA_UPDATE);
72 ResetNeedUpdateCarrierConfig();
73 } else {
74 operatorConfigLoader_->LoadOperatorConfig(slotId_, operatorConfigCache_->STATE_PARA_LOADED);
75 }
76 }
77
ProcessSimOpkeyLoad(const AppExecFwk::InnerEvent::Pointer & event)78 void SimStateTracker::ProcessSimOpkeyLoad(const AppExecFwk::InnerEvent::Pointer &event)
79 {
80 std::shared_ptr<std::vector<std::string>> msgObj = event->GetSharedObject<std::vector<std::string>>();
81 if ((msgObj == nullptr) || ((*msgObj).size() != OPKEY_VMSG_LENTH)) {
82 TELEPHONY_LOGI("argument count error");
83 return;
84 }
85 int slotId;
86 if (!StrToInt((*msgObj)[0], slotId)) {
87 return;
88 }
89 if (slotId != slotId_) {
90 TELEPHONY_LOGE("is not current slotId");
91 return;
92 }
93 char isBlockLoadOperatorConfig[SYSPARA_SIZE] = { 0 };
94 GetParameter(IS_BLOCK_LOAD_OPERATORCONFIG, "false", isBlockLoadOperatorConfig, SYSPARA_SIZE);
95 if (strcmp(isBlockLoadOperatorConfig, "true") == 0) {
96 return;
97 }
98 std::string opkey = (*msgObj)[1];
99 std::string opName = (*msgObj)[2];
100 TELEPHONY_LOGI("OnOpkeyLoad slotId, %{public}d opkey: %{public}s opName: %{public}s",
101 slotId, opkey.data(), opName.data());
102 if (!opkey.empty() && !opName.empty()) {
103 auto simFileManager = simFileManager_.lock();
104 if (simFileManager != nullptr) {
105 simFileManager->SetOpKey(opkey);
106 simFileManager->SetOpName(opName);
107 }
108 ReloadOperatorConfigCache();
109 } else {
110 bool hasSimCard = false;
111 CoreManagerInner::GetInstance().HasSimCard(slotId_, hasSimCard);
112 if (!hasSimCard) {
113 TELEPHONY_LOGE("sim is not exist");
114 return;
115 }
116 ReloadOperatorConfig();
117 }
118 }
119
ProcessOperatorCacheDel(const AppExecFwk::InnerEvent::Pointer & event)120 void SimStateTracker::ProcessOperatorCacheDel(const AppExecFwk::InnerEvent::Pointer &event)
121 {
122 auto slotId = event->GetParam();
123 if (slotId != slotId_) {
124 TELEPHONY_LOGE("is not current slotId");
125 return;
126 }
127 if (operatorConfigCache_ == nullptr) {
128 TELEPHONY_LOGE("operatorConfigCache is nullptr");
129 return;
130 }
131 TELEPHONY_LOGI("need Clear memory and opkey, slotId: %{public}d", slotId_);
132 operatorConfigCache_->ClearOperatorValue(slotId);
133 }
134
ProcessOperatorConfigUpdate(const AppExecFwk::InnerEvent::Pointer & event)135 void SimStateTracker::ProcessOperatorConfigUpdate(const AppExecFwk::InnerEvent::Pointer &event)
136 {
137 auto slotId = event->GetParam();
138 if (slotId != slotId_) {
139 TELEPHONY_LOGE("is not current slotId");
140 return;
141 }
142 bool hasSimCard = false;
143 CoreManagerInner::GetInstance().HasSimCard(slotId_, hasSimCard);
144 if (operatorConfigLoader_ == nullptr || operatorConfigCache_ == nullptr || !hasSimCard) {
145 TELEPHONY_LOGE("operatorConfigLoader or operatorConfigCache is nullptr");
146 return;
147 }
148 TELEPHONY_LOGI("need Clear opkey and reload operatorconfig, slotId: %{public}d", slotId_);
149 operatorConfigCache_->ClearOperatorValue(slotId);
150 CoreManagerInner::GetInstance().ResetDataShareError();
151 SetParameter(IS_BLOCK_LOAD_OPERATORCONFIG, "false");
152 operatorConfigLoader_->LoadOperatorConfig(slotId_, operatorConfigCache_->STATE_PARA_UPDATE);
153 }
154
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)155 void SimStateTracker::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
156 {
157 if (event == nullptr) {
158 TELEPHONY_LOGE("start ProcessEvent but event is null!");
159 return;
160 }
161 auto eventId = event->GetInnerEventId();
162 switch (eventId) {
163 case RadioEvent::RADIO_SIM_RECORDS_LOADED:
164 ProcessSimRecordLoad(event);
165 break;
166 case RadioEvent::RADIO_SIM_OPKEY_LOADED:
167 ProcessSimOpkeyLoad(event);
168 break;
169 case RadioEvent::RADIO_OPERATOR_CACHE_DELETE:
170 ProcessOperatorCacheDel(event);
171 break;
172 case RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE:
173 ProcessOperatorConfigUpdate(event);
174 break;
175 default:
176 TELEPHONY_LOGI("ProcessEvent default");
177 }
178 }
179
RegisterForIccLoaded()180 bool SimStateTracker::RegisterForIccLoaded()
181 {
182 auto simFileManager = simFileManager_.lock();
183 if (simFileManager == nullptr) {
184 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
185 return false;
186 }
187 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
188 return true;
189 }
190
RegisterOpkeyLoaded()191 bool SimStateTracker::RegisterOpkeyLoaded()
192 {
193 auto simFileManager = simFileManager_.lock();
194 if (simFileManager == nullptr) {
195 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
196 return false;
197 }
198 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_OPKEY_LOADED);
199 return true;
200 }
201
RegisterOperatorCacheDel()202 bool SimStateTracker::RegisterOperatorCacheDel()
203 {
204 auto simFileManager = simFileManager_.lock();
205 if (simFileManager == nullptr) {
206 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
207 return false;
208 }
209 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_OPERATOR_CACHE_DELETE);
210 return true;
211 }
212
RegisterOperatorConfigUpdate()213 bool SimStateTracker::RegisterOperatorConfigUpdate()
214 {
215 auto simFileManager = simFileManager_.lock();
216 if (simFileManager == nullptr) {
217 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
218 return false;
219 }
220 simFileManager->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE);
221 return true;
222 }
223
UnRegisterForIccLoaded()224 bool SimStateTracker::UnRegisterForIccLoaded()
225 {
226 auto simFileManager = simFileManager_.lock();
227 if (simFileManager == nullptr) {
228 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
229 return false;
230 }
231 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_RECORDS_LOADED);
232 return true;
233 }
234
UnRegisterOpkeyLoaded()235 bool SimStateTracker::UnRegisterOpkeyLoaded()
236 {
237 auto simFileManager = simFileManager_.lock();
238 if (simFileManager == nullptr) {
239 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
240 return false;
241 }
242 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_OPKEY_LOADED);
243 return true;
244 }
245
UnregisterOperatorCacheDel()246 bool SimStateTracker::UnregisterOperatorCacheDel()
247 {
248 auto simFileManager = simFileManager_.lock();
249 if (simFileManager == nullptr) {
250 TELEPHONY_LOGE("simFileManager::can not get simFileManager");
251 return false;
252 }
253 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_OPERATOR_CACHE_DELETE);
254 return true;
255 }
256
UnRegisterOperatorConfigUpdate()257 bool SimStateTracker::UnRegisterOperatorConfigUpdate()
258 {
259 auto simFileManager = simFileManager_.lock();
260 if (simFileManager == nullptr) {
261 TELEPHONY_LOGE("SimStateTracker::can not get SimFileManager");
262 return false;
263 }
264 simFileManager->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_OPERATOR_CONFIG_UPDATE);
265 return true;
266 }
267
IsNeedUpdateCarrierConfig()268 bool SimStateTracker::IsNeedUpdateCarrierConfig()
269 {
270 char isNeedUpdateCarrierConfig[SYSPARA_SIZE] = { 0 };
271 std::string key = "";
272 GetParameter(key.append(IS_UPDATE_OPERATORCONFIG).append(std::to_string(slotId_)).c_str(),
273 "", isNeedUpdateCarrierConfig, SYSPARA_SIZE);
274 bool result = strcmp(isNeedUpdateCarrierConfig, "true") == 0;
275 return result;
276 }
277
ResetNeedUpdateCarrierConfig()278 void SimStateTracker::ResetNeedUpdateCarrierConfig()
279 {
280 std::string key = "";
281 SetParameter(key.append(IS_UPDATE_OPERATORCONFIG).append(std::to_string(slotId_)).c_str(), "false");
282 }
283
ReloadOperatorConfigCache()284 void SimStateTracker::ReloadOperatorConfigCache()
285 {
286 if (operatorConfigCache_ == nullptr) {
287 TELEPHONY_LOGE("operatorConfigCache is null!");
288 return;
289 }
290 OperatorConfig opc;
291 if (IsNeedUpdateCarrierConfig()) {
292 operatorConfigCache_->LoadOperatorConfig(slotId_, opc, operatorConfigCache_->STATE_PARA_UPDATE);
293 ResetNeedUpdateCarrierConfig();
294 } else {
295 operatorConfigCache_->LoadOperatorConfig(slotId_, opc, operatorConfigCache_->STATE_PARA_LOADED);
296 }
297 }
298
ReloadOperatorConfig()299 void SimStateTracker::ReloadOperatorConfig()
300 {
301 if (operatorConfigLoader_ == nullptr) {
302 TELEPHONY_LOGE("operatorConfigLoader is null!");
303 return;
304 }
305 if (IsNeedUpdateCarrierConfig()) {
306 operatorConfigLoader_->LoadOperatorConfig(slotId_, operatorConfigCache_->STATE_PARA_UPDATE);
307 ResetNeedUpdateCarrierConfig();
308 } else {
309 operatorConfigLoader_->LoadOperatorConfig(slotId_, operatorConfigCache_->STATE_PARA_LOADED);
310 }
311 }
312 } // namespace Telephony
313 } // namespace OHOS
314