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