• 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 
16 #include "operator_config_loader.h"
17 
18 #include <string_ex.h>
19 
20 #include "core_manager_inner.h"
21 #include "operator_matching_rule.h"
22 #include "sim_state_type.h"
23 #include "telephony_types.h"
24 
25 namespace OHOS {
26 namespace Telephony {
OperatorConfigLoader(std::shared_ptr<SimFileManager> simFileManager,std::shared_ptr<OperatorConfigCache> operatorConfigCache)27 OperatorConfigLoader::OperatorConfigLoader(
28     std::shared_ptr<SimFileManager> simFileManager, std::shared_ptr<OperatorConfigCache> operatorConfigCache)
29     : simFileManager_(simFileManager), operatorConfigCache_(operatorConfigCache)
30 {
31     TELEPHONY_LOGI("OperatorConfigLoader construct");
32 }
33 
~OperatorConfigLoader()34 OperatorConfigLoader::~OperatorConfigLoader() {}
35 
LoadOperatorConfig(int32_t slotId)36 OperatorConfig OperatorConfigLoader::LoadOperatorConfig(int32_t slotId)
37 {
38     OperatorConfig opc;
39     if (operatorConfigCache_ == nullptr) {
40         TELEPHONY_LOGE("operatorConfigCache_ is nullptr");
41         return opc;
42     }
43     TELEPHONY_LOGI("LoadOperatorConfig slotId %{public}d", slotId);
44     std::string opkey = LoadOpKeyOnMccMnc(slotId);
45     operatorConfigCache_->LoadOperatorConfig(slotId, opc);
46     TELEPHONY_LOGI("LoadOperatorConfig %{public}zu", opc.configValue.size());
47     return opc;
48 }
49 
LoadOpKeyOnMccMnc(int32_t slotId)50 std::string OperatorConfigLoader::LoadOpKeyOnMccMnc(int32_t slotId)
51 {
52     SimState simState = SimState::SIM_STATE_UNKNOWN;
53     CoreManagerInner::GetInstance().GetSimState(slotId, simState);
54     if (simState != SimState::SIM_STATE_READY) {
55         TELEPHONY_LOGE("LoadOpKeyOnMccMnc simState not ready");
56         return DEFAULT_OPERATOR_KEY;
57     }
58     Uri uri(OPKEY_INFO_URI);
59     std::vector<std::string> colume;
60     NativeRdb::DataAbilityPredicates predicates;
61     std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet;
62     std::shared_ptr<AppExecFwk::DataAbilityHelper> helper = CreateOpKeyHelper();
63     if (helper == nullptr || simFileManager_ == nullptr) {
64         TELEPHONY_LOGE("helper or simFileManager_ is nullptr");
65         return DEFAULT_OPERATOR_KEY;
66     }
67     std::string mccmncFromSim = Str16ToStr8(simFileManager_->GetSimOperatorNumeric());
68     predicates.EqualTo(MCCMNC, mccmncFromSim);
69     resultSet = helper->Query(uri, colume, predicates);
70     if (resultSet != nullptr) {
71         return GetOpKey(resultSet, slotId);
72     }
73     return DEFAULT_OPERATOR_KEY;
74 }
75 
GetOpKey(std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet,int32_t slotId)76 std::string OperatorConfigLoader::GetOpKey(std::shared_ptr<NativeRdb::AbsSharedResultSet> resultSet, int32_t slotId)
77 {
78     if (resultSet == nullptr) {
79         TELEPHONY_LOGE("GetOpKey resultSet is nullptr");
80         return DEFAULT_OPERATOR_KEY;
81     }
82     if (simFileManager_ == nullptr) {
83         TELEPHONY_LOGE("GetOpKey simFileManager_ is nullptr");
84         return DEFAULT_OPERATOR_KEY;
85     }
86     iccidFromSim_ = Str16ToStr8(simFileManager_->GetSimIccId());
87     imsiFromSim_ = Str16ToStr8(simFileManager_->GetIMSI());
88     spnFromSim_ = Str16ToStr8(simFileManager_->GetSimSpn());
89     gid1FromSim_ = Str16ToStr8(simFileManager_->GetSimGid1());
90     gid2FromSim_ = Str16ToStr8(simFileManager_->GetSimGid2());
91     int count;
92     resultSet->GetRowCount(count);
93     TELEPHONY_LOGI("GetOpKey count: %{public}d", count);
94     if (count <= 0) {
95         TELEPHONY_LOGE("GetOpKey count: %{public}d null return", count);
96         return DEFAULT_OPERATOR_KEY;
97     }
98     int columnIndex;
99     std::string opKeyVal = DEFAULT_OPERATOR_KEY;
100     std::string opNameVal;
101     std::string opKeyExtVal;
102     for (int row = 0; row < count; row++) {
103         if (MatchOperatorRule(resultSet, row)) {
104             resultSet->GetColumnIndex(OPKEY, columnIndex);
105             resultSet->GetString(columnIndex, opKeyVal);
106             resultSet->GetColumnIndex(OPNAME, columnIndex);
107             resultSet->GetString(columnIndex, opNameVal);
108             resultSet->GetColumnIndex(OPKEY_EXT, columnIndex);
109             resultSet->GetString(columnIndex, opKeyExtVal);
110         }
111     }
112     resultSet->Close();
113     std::string key;
114     SetParameter(key.append(OPKEY_PROP_PREFIX).append(std::to_string(slotId)).c_str(), opKeyVal.c_str());
115     key.shrink_to_fit();
116     simFileManager_->SetOpKey(opKeyVal);
117     simFileManager_->SetOpName(opNameVal);
118     simFileManager_->SetOpKeyExt(opKeyExtVal);
119     return opKeyVal;
120 }
121 
MatchOperatorRule(std::shared_ptr<NativeRdb::AbsSharedResultSet> & resultSet,int row)122 bool OperatorConfigLoader::MatchOperatorRule(std::shared_ptr<NativeRdb::AbsSharedResultSet> &resultSet, int row)
123 {
124     if (resultSet == nullptr) {
125         TELEPHONY_LOGE("resultSet is nullptr");
126         return false;
127     }
128     bool isAllRuleMatch = true;
129     int columnIndex;
130     std::string strVal;
131     resultSet->GoToRow(row);
132     resultSet->GetColumnIndex(ICCID, columnIndex);
133     resultSet->GetString(columnIndex, strVal);
134     if (!strVal.empty()) {
135         isAllRuleMatch = OperatorMatchingRule::IccidRegexMatch(iccidFromSim_, strVal);
136     }
137     if (!isAllRuleMatch) {
138         return false;
139     }
140     resultSet->GetColumnIndex(IMSI, columnIndex);
141     resultSet->GetString(columnIndex, strVal);
142     if (!strVal.empty()) {
143         isAllRuleMatch = OperatorMatchingRule::ImsiRegexMatch(imsiFromSim_, strVal);
144     }
145     if (!isAllRuleMatch) {
146         return false;
147     }
148     resultSet->GetColumnIndex(SPN, columnIndex);
149     resultSet->GetString(columnIndex, strVal);
150     if (!strVal.empty()) {
151         isAllRuleMatch = OperatorMatchingRule::SpnRegexMatch(spnFromSim_, strVal);
152     }
153     if (!isAllRuleMatch) {
154         return false;
155     }
156     resultSet->GetColumnIndex(GID1, columnIndex);
157     resultSet->GetString(columnIndex, strVal);
158     if (!strVal.empty()) {
159         isAllRuleMatch = OperatorMatchingRule::PrefixMatch(gid1FromSim_, strVal);
160     }
161     if (!isAllRuleMatch) {
162         return false;
163     }
164     resultSet->GetColumnIndex(GID2, columnIndex);
165     resultSet->GetString(columnIndex, strVal);
166     if (!strVal.empty()) {
167         isAllRuleMatch = OperatorMatchingRule::PrefixMatch(gid2FromSim_, strVal);
168     }
169     return isAllRuleMatch;
170 }
171 
CreateOpKeyHelper()172 std::shared_ptr<AppExecFwk::DataAbilityHelper> OperatorConfigLoader::CreateOpKeyHelper()
173 {
174     if (opKeyDataAbilityHelper_ == nullptr) {
175         std::shared_ptr<Uri> dataAbilityUri = std::make_shared<Uri>(OPKEY_URI);
176         if (dataAbilityUri == nullptr) {
177             TELEPHONY_LOGE("CreateOpKeyHelper dataAbilityUri is nullptr");
178             return nullptr;
179         }
180         opKeyDataAbilityHelper_ = CreateDataAHelper(TELEPHONY_CORE_SERVICE_SYS_ABILITY_ID, dataAbilityUri);
181     }
182     return opKeyDataAbilityHelper_;
183 }
184 
CreateDataAHelper(int32_t systemAbilityId,std::shared_ptr<Uri> dataAbilityUri) const185 std::shared_ptr<AppExecFwk::DataAbilityHelper> OperatorConfigLoader::CreateDataAHelper(
186     int32_t systemAbilityId, std::shared_ptr<Uri> dataAbilityUri) const
187 {
188     TELEPHONY_LOGI("OperatorConfigLoader::CreateDataAHelper");
189     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
190     if (saManager == nullptr) {
191         TELEPHONY_LOGE("OperatorConfigLoader Get system ability mgr failed");
192         return nullptr;
193     }
194     auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
195     if (remoteObj == nullptr) {
196         TELEPHONY_LOGE("OperatorConfigLoader GetSystemAbility Service Failed");
197         return nullptr;
198     }
199     return AppExecFwk::DataAbilityHelper::Creator(remoteObj, dataAbilityUri);
200 }
201 } // namespace Telephony
202 } // namespace OHOS
203