• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 "rdb_pdp_profile_helper.h"
17 
18 #include "data_storage_errors.h"
19 #include "data_storage_log_wrapper.h"
20 #include "parser_util.h"
21 #include "pdp_profile_data.h"
22 #include "preferences_util.h"
23 #include "rdb_errno.h"
24 #include "rdb_pdp_profile_callback.h"
25 #include "rdb_store_config.h"
26 #include "values_bucket.h"
27 #include "vector"
28 
29 namespace OHOS {
30 namespace Telephony {
RdbPdpProfileHelper()31 RdbPdpProfileHelper::RdbPdpProfileHelper() {}
32 
Init()33 int RdbPdpProfileHelper::Init()
34 {
35     int errCode = NativeRdb::E_OK;
36     NativeRdb::RdbStoreConfig config(dbPath_);
37     config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
38     std::string pdpProfileStr;
39     CreatePdpProfileTableStr(pdpProfileStr, TABLE_PDP_PROFILE);
40     std::string pseBaseStationStr;
41     CreatePseBaseStationTableStr(pseBaseStationStr, TABLE_PSE_BASE_STATION);
42     std::string pseBaseStationTriggerStr;
43     CreatePseBaseStationTriggerStr(pseBaseStationTriggerStr, TABLE_PSE_BASE_STATION);
44     std::vector<std::string> createTableVec;
45     createTableVec.push_back(pdpProfileStr);
46     createTableVec.push_back(pseBaseStationStr);
47     createTableVec.push_back(pseBaseStationTriggerStr);
48     RdbPdpProfileCallback callback(createTableVec);
49     CreateRdbStore(config, VERSION, callback, errCode);
50     return errCode;
51 }
52 
UpdateDbPath(const std::string & path)53 void RdbPdpProfileHelper::UpdateDbPath(const std::string &path)
54 {
55     dbPath_ = path + DB_NAME;
56 }
57 
CreatePdpProfileTableStr(std::string & createTableStr,const std::string & tableName)58 void RdbPdpProfileHelper::CreatePdpProfileTableStr(std::string &createTableStr, const std::string &tableName)
59 {
60     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append("(");
61     createTableStr.append(PdpProfileData::PROFILE_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
62     createTableStr.append(PdpProfileData::PROFILE_NAME).append(" TEXT DEFAULT '', ");
63     createTableStr.append(PdpProfileData::MCC).append(" TEXT DEFAULT '', ");
64     createTableStr.append(PdpProfileData::MNC).append(" TEXT DEFAULT '', ");
65     createTableStr.append(PdpProfileData::MCCMNC).append(" TEXT DEFAULT '', ");
66     createTableStr.append(PdpProfileData::APN).append(" TEXT DEFAULT '', ");
67     createTableStr.append(PdpProfileData::AUTH_TYPE).append(" INTEGER, ");
68     createTableStr.append(PdpProfileData::AUTH_USER).append(" TEXT DEFAULT '', ");
69     createTableStr.append(PdpProfileData::AUTH_PWD).append(" TEXT DEFAULT '', ");
70     createTableStr.append(PdpProfileData::APN_TYPES).append(" TEXT DEFAULT '', ");
71     createTableStr.append(PdpProfileData::IS_ROAMING_APN).append(" INTEGER DEFAULT 1, ");
72     createTableStr.append(PdpProfileData::APN_PROTOCOL).append(" TEXT DEFAULT '', ");
73     createTableStr.append(PdpProfileData::APN_ROAM_PROTOCOL).append(" TEXT DEFAULT '', ");
74     createTableStr.append(PdpProfileData::HOME_URL).append(" TEXT DEFAULT '', ");
75     createTableStr.append(PdpProfileData::MMS_IP_ADDRESS).append(" TEXT DEFAULT '', ");
76     createTableStr.append(PdpProfileData::PROXY_IP_ADDRESS).append(" TEXT DEFAULT '', ");
77     createTableStr.append(PdpProfileData::BEARING_SYSTEM_TYPE).append(" INTEGER DEFAULT 0, ");
78     createTableStr.append(PdpProfileData::MVNO_TYPE).append(" TEXT DEFAULT '', ");
79     createTableStr.append(PdpProfileData::MVNO_MATCH_DATA).append(" TEXT DEFAULT '', ");
80     createTableStr.append(PdpProfileData::EDITED_STATUS).append(" INTEGER DEFAULT 0, ");
81     createTableStr.append(PdpProfileData::SERVER).append(" TEXT DEFAULT '', ");
82     createTableStr.append(PdpProfileData::OPKEY).append(" TEXT DEFAULT '', ");
83     createTableStr.append("UNIQUE (").append(PdpProfileData::MCC).append(", ");
84     createTableStr.append(PdpProfileData::MNC).append(", ");
85     createTableStr.append(PdpProfileData::OPKEY).append(", ");
86     createTableStr.append(PdpProfileData::MVNO_TYPE).append(", ");
87     createTableStr.append(PdpProfileData::MVNO_MATCH_DATA).append(", ");
88     createTableStr.append(PdpProfileData::APN).append(", ");
89     createTableStr.append(PdpProfileData::APN_TYPES).append(", ");
90     createTableStr.append(PdpProfileData::IS_ROAMING_APN).append(", ");
91     createTableStr.append(PdpProfileData::APN_PROTOCOL).append(", ");
92     createTableStr.append(PdpProfileData::APN_ROAM_PROTOCOL).append(", ");
93     createTableStr.append(PdpProfileData::HOME_URL).append(", ");
94     createTableStr.append(PdpProfileData::MMS_IP_ADDRESS).append(", ");
95     createTableStr.append(PdpProfileData::PROFILE_NAME).append(", ");
96     createTableStr.append(PdpProfileData::BEARING_SYSTEM_TYPE).append(", ");
97     createTableStr.append(PdpProfileData::AUTH_USER).append(", ");
98     createTableStr.append(PdpProfileData::AUTH_PWD).append(", ");
99     createTableStr.append(PdpProfileData::EDITED_STATUS).append(", ");
100     createTableStr.append(PdpProfileData::PROXY_IP_ADDRESS).append("))");
101 }
102 
CreatePseBaseStationTableStr(std::string & createTableStr,const std::string & tableName)103 void RdbPdpProfileHelper::CreatePseBaseStationTableStr(std::string &createTableStr, const std::string &tableName)
104 {
105     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(tableName).append("( ");
106     createTableStr.append(PseBaseStationData::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
107     createTableStr.append(PseBaseStationData::DATE).append(" TEXT NOT NULL, ");
108     createTableStr.append(PseBaseStationData::COUNT).append(" INTEGER NOT NULL, ");
109     createTableStr.append("UNIQUE (").append(PseBaseStationData::DATE).append("));");
110 }
111 
CreatePseBaseStationTriggerStr(std::string & createTableStr,const std::string & tableName)112 void RdbPdpProfileHelper::CreatePseBaseStationTriggerStr(std::string &createTableStr, const std::string &tableName)
113 {
114     createTableStr.append("CREATE TRIGGER IF NOT EXISTS pse_trigger BEFORE INSERT ON ").append(tableName);
115     createTableStr.append(" WHEN (SELECT COUNT (*) FROM ").append(tableName);
116     createTableStr.append(") >= 90 BEGIN DELETE FROM ").append(tableName);
117     createTableStr.append(" WHERE ").append(PseBaseStationData::ID);
118     createTableStr.append(" = (SELECT MIN(").append(PseBaseStationData::ID);
119     createTableStr.append(") FROM ").append(tableName).append("); END;");
120 }
121 
ResetApn()122 int RdbPdpProfileHelper::ResetApn()
123 {
124     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
125     if (preferencesUtil != nullptr) {
126         preferencesUtil->DeleteProfiles();
127     }
128     int ret = BeginTransaction();
129     if (ret != NativeRdb::E_OK) {
130         DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn BeginTransaction is error!");
131         return ret;
132     }
133     std::string pdpProfileStr;
134     CreatePdpProfileTableStr(pdpProfileStr, TEMP_TABLE_PDP_PROFILE);
135     ret = ExecuteSql(pdpProfileStr);
136     if (ret != NativeRdb::E_OK) {
137         DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn create table temp_pdp_profile ret = %{public}d", ret);
138         return ret;
139     }
140     DATA_STORAGE_LOGI("RdbPdpProfileHelper::ResetApn create table success");
141     ParserUtil util;
142     std::vector<PdpProfile> vec;
143     ret = util.ParserPdpProfileJson(vec);
144     if (ret != DATA_STORAGE_SUCCESS) {
145         RollBack();
146         return ret;
147     }
148     for (size_t i = 0; i < vec.size(); i++) {
149         NativeRdb::ValuesBucket value;
150         util.ParserPdpProfileToValuesBucket(value, vec[i]);
151         int64_t id;
152         Insert(id, value, TEMP_TABLE_PDP_PROFILE);
153     }
154     ret = ExecuteSql("drop table " + std::string(TABLE_PDP_PROFILE));
155     if (ret != NativeRdb::E_OK) {
156         DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn drop table ret = %{public}d", ret);
157         RollBack();
158         return ret;
159     }
160     DATA_STORAGE_LOGI("RdbPdpProfileHelper::ResetApn success");
161     std::string sql;
162     sql.append("alter table ").append(TEMP_TABLE_PDP_PROFILE).append(" rename to ").append(TABLE_PDP_PROFILE);
163     ret = ExecuteSql(sql);
164     if (ret != NativeRdb::E_OK) {
165         DATA_STORAGE_LOGE("RdbPdpProfileHelper::ResetApn alter table ret = %{public}d", ret);
166         RollBack();
167         return ret;
168     }
169     DATA_STORAGE_LOGI("RdbPdpProfileHelper::ResetApn alter table success");
170     ret = CommitTransactionAction();
171     return ret;
172 }
173 
CommitTransactionAction()174 int RdbPdpProfileHelper::CommitTransactionAction()
175 {
176     int result = Commit();
177     if (result != NativeRdb::E_OK) {
178         RollBack();
179     }
180     return result;
181 }
182 
InitAPNDatabase(int slotId,const std::string & opKey,bool isNeedCheckFile)183 int RdbPdpProfileHelper::InitAPNDatabase(int slotId, const std::string &opKey, bool isNeedCheckFile)
184 {
185     if (store_ == nullptr || opKey.empty() || strcmp(opKey.c_str(), INVALID_OPKEY) == 0) {
186         return NativeRdb::E_ERROR;
187     }
188     DATA_STORAGE_LOGD("InitAPNDatabase start");
189     ParserUtil util;
190     std::string path;
191     util.GetPdpProfilePath(slotId, path);
192     if (path.empty()) {
193         return NativeRdb::E_ERROR;
194     }
195     std::string checksum;
196     util.GetFileChecksum(path.c_str(), checksum);
197     if (checksum.empty()) {
198         DATA_STORAGE_LOGE("InitAPNDatabase fail! checksum is null!");
199         return NativeRdb::E_ERROR;
200     }
201     if (isNeedCheckFile && !IsApnDbUpdateNeeded(opKey, checksum)) {
202         DATA_STORAGE_LOGI("The file is not changed and does not need to be loaded again.");
203         return DATA_STORAGE_SUCCESS;
204     }
205     std::vector<PdpProfile> vec;
206     int resultCode = util.ParserPdpProfileJson(vec, path.c_str());
207     if (resultCode != DATA_STORAGE_SUCCESS) {
208         DATA_STORAGE_LOGE("InitAPNDatabase fail");
209         return DATA_STORAGE_ERROR;
210     }
211     int32_t result = store_->BeginTransaction();
212     if (result != NativeRdb::E_OK) {
213         DATA_STORAGE_LOGE("BeginTransaction error!");
214         return DATA_STORAGE_ERROR;
215     }
216     ClearData(opKey);
217     DATA_STORAGE_LOGD("InitAPNDatabase size = %{public}zu", vec.size());
218     for (size_t i = 0; i < vec.size(); i++) {
219         NativeRdb::ValuesBucket value;
220         util.ParserPdpProfileToValuesBucket(value, vec[i]);
221         value.PutString(PdpProfileData::OPKEY, opKey);
222         int64_t id;
223         store_->InsertWithConflictResolution(
224             id, TABLE_PDP_PROFILE, value, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
225     }
226     result = CommitTransactionAction();
227     if (result == NativeRdb::E_OK) {
228         SetPreferApnConfChecksum(opKey, checksum);
229     }
230     DATA_STORAGE_LOGD("InitAPNDatabase end");
231     return result;
232 }
233 
ClearData(const std::string & opKey)234 int RdbPdpProfileHelper::ClearData(const std::string &opKey)
235 {
236     int delRows = 0;
237     NativeRdb::AbsRdbPredicates *absRdbPredicates = (new NativeRdb::AbsRdbPredicates(TABLE_PDP_PROFILE))
238                                                         ->EqualTo(PdpProfileData::OPKEY, opKey)
239                                                         ->And()
240                                                         ->EqualTo(PdpProfileData::EDITED_STATUS, 0);
241     int result = Delete(delRows, *absRdbPredicates);
242     delete absRdbPredicates;
243     return result;
244 }
245 
IsApnDbUpdateNeeded(const std::string & opkey,std::string & checkSum)246 bool RdbPdpProfileHelper::IsApnDbUpdateNeeded(const std::string &opkey, std::string &checkSum)
247 {
248     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
249     if (preferencesUtil != nullptr) {
250         std::string lastCheckSum = preferencesUtil->ObtainString(APN_CONF_CHECKSUM + opkey, "");
251         if (checkSum.compare(lastCheckSum) == 0) {
252             return false;
253         }
254     }
255     return true;
256 }
257 
SetPreferApnConfChecksum(const std::string & opkey,std::string & checkSum)258 int RdbPdpProfileHelper::SetPreferApnConfChecksum(const std::string &opkey, std::string &checkSum)
259 {
260     auto preferencesUtil = DelayedSingleton<PreferencesUtil>::GetInstance();
261     if (preferencesUtil == nullptr) {
262         DATA_STORAGE_LOGE("preferencesUtil is nullptr!");
263         return NativePreferences::E_ERROR;
264     }
265     return preferencesUtil->SaveString(APN_CONF_CHECKSUM + opkey, checkSum);
266 }
267 } // namespace Telephony
268 } // namespace OHOS
269