• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "operator_config_cache.h"
16 
17 #include <json/json.h>
18 #include <openssl/sha.h>
19 #include <string_ex.h>
20 #include <telephony_types.h>
21 
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "core_manager_inner.h"
25 #include "radio_event.h"
26 
27 namespace OHOS {
28 namespace Telephony {
OperatorConfigCache(const std::shared_ptr<AppExecFwk::EventRunner> & runner,std::shared_ptr<SimFileManager> simFileManager,int32_t slotId)29 OperatorConfigCache::OperatorConfigCache(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
30     std::shared_ptr<SimFileManager> simFileManager, int32_t slotId)
31     : AppExecFwk::EventHandler(runner), simFileManager_(simFileManager), slotId_(slotId)
32 {
33     if (runner != nullptr) {
34         runner->Run();
35     }
36     TELEPHONY_LOGI("OperatorConfigCache create");
37 }
38 
ClearAllCache(int32_t slotId)39 void OperatorConfigCache::ClearAllCache(int32_t slotId)
40 {
41     ClearOperatorValue(slotId);
42     ClearMemoryCache(slotId);
43     parser_.ClearFilesCache();
44 }
45 
ClearOperatorValue(int32_t slotId)46 void OperatorConfigCache::ClearOperatorValue(int32_t slotId)
47 {
48     if (simFileManager_ == nullptr) {
49         TELEPHONY_LOGE("simFileManager_ is nullptr");
50         return;
51     }
52     std::string key;
53     std::string initialOpkey = INITIAL_OPKEY;
54     SetParameter(key.append(OPKEY_PROP_PREFIX).append(std::to_string(slotId)).c_str(), initialOpkey.c_str());
55     simFileManager_->SetOpKey("");
56     simFileManager_->SetOpName("");
57     simFileManager_->SetOpKeyExt("");
58 }
59 
ClearMemoryCache(int32_t slotId)60 void OperatorConfigCache::ClearMemoryCache(int32_t slotId)
61 {
62     opc_.stringValue.clear();
63     opc_.stringArrayValue.clear();
64     opc_.intValue.clear();
65     opc_.intArrayValue.clear();
66     opc_.longValue.clear();
67     opc_.longArrayValue.clear();
68     opc_.boolValue.clear();
69     opc_.configValue.clear();
70 }
71 
LoadOperatorConfig(int32_t slotId,OperatorConfig & poc)72 int32_t OperatorConfigCache::LoadOperatorConfig(int32_t slotId, OperatorConfig &poc)
73 {
74     if (simFileManager_ == nullptr) {
75         TELEPHONY_LOGE("simFileManager_ is nullptr");
76         return TELEPHONY_ERR_LOCAL_PTR_NULL;
77     }
78     std::lock_guard<std::mutex> lock(mutex_);
79     std::string iccid = Str16ToStr8(simFileManager_->GetSimIccId());
80     std::string filename = EncryptIccId(iccid) + ".json";
81     std::string opkey = GetOpKey(slotId);
82     if (opkey == std::string(INITIAL_OPKEY)) {
83         TELEPHONY_LOGI("load default operator config");
84         filename = DEFAULT_OPERATOR_CONFIG;
85     }
86     SimState simState = SimState::SIM_STATE_UNKNOWN;
87     CoreManagerInner::GetInstance().GetSimState(slotId, simState);
88     TELEPHONY_LOGI("LoadOperatorConfig simState = %{public}d", simState);
89     bool canAnnounceChanged = (simState == SimState::SIM_STATE_NOT_PRESENT || simState == SimState::SIM_STATE_READY);
90     Json::Value opcJson;
91     if (parser_.ParseOperatorConfigFromFile(poc, parser_.GetOperatorConfigFilePath(filename), opcJson)) {
92         TELEPHONY_LOGI("load from file success opc size %{public}zu", poc.configValue.size());
93         if (poc.configValue.size() > 0) {
94             CopyOperatorConfig(poc, opc_);
95             if (canAnnounceChanged) {
96                 AnnounceOperatorConfigChanged(slotId);
97             }
98             return TELEPHONY_ERR_SUCCESS;
99         }
100     }
101     if (parser_.ParseFromCustomSystem(slotId, poc, opcJson)) {
102         TELEPHONY_LOGI("load from custom system success");
103         parser_.WriteOperatorConfigJson(filename, opcJson);
104 
105         if (poc.configValue.size() > 0) {
106             CopyOperatorConfig(poc, opc_);
107             if (canAnnounceChanged) {
108                 AnnounceOperatorConfigChanged(slotId);
109             }
110             return TELEPHONY_ERR_SUCCESS;
111         }
112     }
113     return CORE_ERR_OPERATOR_CONF_NOT_EXIT;
114 }
115 
GetOperatorConfigs(int32_t slotId,OperatorConfig & poc)116 int32_t OperatorConfigCache::GetOperatorConfigs(int32_t slotId, OperatorConfig &poc)
117 {
118     if (opc_.configValue.size() > 0) {
119         TELEPHONY_LOGI("get from memory");
120         CopyOperatorConfig(opc_, poc);
121         return TELEPHONY_ERR_SUCCESS;
122     }
123     TELEPHONY_LOGI("reload operator config");
124     return LoadOperatorConfig(slotId, poc);
125 }
126 
CopyOperatorConfig(const OperatorConfig & from,OperatorConfig & to)127 void OperatorConfigCache::CopyOperatorConfig(const OperatorConfig &from, OperatorConfig &to)
128 {
129     for (auto it : from.configValue) {
130         to.configValue[it.first] = it.second;
131     }
132     for (auto it : from.boolValue) {
133         to.boolValue[it.first] = it.second;
134     }
135     for (auto it : from.intValue) {
136         to.intValue[it.first] = it.second;
137     }
138     for (auto it : from.longValue) {
139         to.longValue[it.first] = it.second;
140     }
141     for (auto it : from.stringValue) {
142         to.stringValue[it.first] = it.second;
143     }
144     for (auto it : from.intArrayValue) {
145         to.intArrayValue[it.first] = std::vector<int32_t>(it.second);
146     }
147     for (auto it : from.longArrayValue) {
148         to.longArrayValue[it.first] = std::vector<int64_t>(it.second);
149     }
150     for (auto it : from.stringArrayValue) {
151         to.stringArrayValue[it.first] = std::vector<std::string>(it.second);
152     }
153 }
154 
GetOpKey(int32_t slotId)155 std::string OperatorConfigCache::GetOpKey(int32_t slotId)
156 {
157     char simOpKey[SYSPARA_SIZE] = { 0 };
158     std::string key;
159     GetParameter(key.append(OPKEY_PROP_PREFIX).append(std::to_string(slotId)).c_str(), DEFAULT_OPERATOR_KEY,
160         simOpKey, SYSPARA_SIZE);
161     key.shrink_to_fit();
162     return simOpKey;
163 }
164 
EncryptIccId(const std::string iccid)165 std::string OperatorConfigCache::EncryptIccId(const std::string iccid)
166 {
167     unsigned char hash[SHA256_DIGEST_LENGTH];
168     SHA256_CTX sha256;
169     SHA256_Init(&sha256);
170     SHA256_Update(&sha256, iccid.c_str(), iccid.size());
171     SHA256_Final(hash, &sha256);
172     std::string encryptIccId = SIMUtils::BytesConvertToHexString(hash, SHA256_DIGEST_LENGTH);
173     return encryptIccId;
174 }
175 
RegisterForIccChange()176 bool OperatorConfigCache::RegisterForIccChange()
177 {
178     TELEPHONY_LOGI("OperatorConfigCache::RegisterForIccLoaded");
179     if (simFileManager_ == nullptr) {
180         TELEPHONY_LOGE("OperatorConfigCache::can not get SimFileManager");
181         return false;
182     }
183     simFileManager_->RegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
184     return true;
185 }
186 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)187 void OperatorConfigCache::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
188 {
189     if (event == nullptr) {
190         TELEPHONY_LOGE("start ProcessEvent but event is null!");
191         return;
192     }
193     SimState simState = SimState::SIM_STATE_UNKNOWN;
194     CoreManagerInner::GetInstance().GetSimState(slotId_, simState);
195     if (event->GetInnerEventId() == RadioEvent::RADIO_SIM_STATE_CHANGE) {
196         TELEPHONY_LOGI("OperatorConfigCache::Sim state change");
197         if (simState == SimState::SIM_STATE_NOT_PRESENT || simState == SimState::SIM_STATE_LOCKED) {
198             ClearOperatorValue(slotId_);
199             ClearMemoryCache(slotId_);
200             OperatorConfig opc;
201             LoadOperatorConfig(slotId_, opc);
202         }
203     }
204 }
205 
UnRegisterForIccChange()206 bool OperatorConfigCache::UnRegisterForIccChange()
207 {
208     TELEPHONY_LOGI("OperatorConfigCache::UnRegisterForIccLoaded");
209     if (simFileManager_ == nullptr) {
210         TELEPHONY_LOGE("OperatorConfigCache::can not get SimFileManager");
211         return false;
212     }
213     simFileManager_->UnRegisterCoreNotify(shared_from_this(), RadioEvent::RADIO_SIM_STATE_CHANGE);
214     return true;
215 }
216 
AnnounceOperatorConfigChanged(int32_t slotId)217 bool OperatorConfigCache::AnnounceOperatorConfigChanged(int32_t slotId)
218 {
219     AAFwk::Want want;
220     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_OPERATOR_CONFIG_CHANGED);
221     want.SetParam(KEY_SLOTID, slotId);
222     std::string eventData(OPERATOR_CONFIG_CHANGED);
223     EventFwk::CommonEventData data;
224     data.SetWant(want);
225     data.SetData(eventData);
226     EventFwk::CommonEventPublishInfo publishInfo;
227     publishInfo.SetOrdered(true);
228     bool publishResult = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
229     TELEPHONY_LOGI("OperatorConfigCache:AnnounceOperatorConfigChanged end###result = %{public}d", publishResult);
230     return publishResult;
231 }
232 } // namespace Telephony
233 } // namespace OHOS
234