1 /*
2 * Copyright (c) 2024 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 "device_profile_dao.h"
17 #include "content_sensor_manager_utils.h"
18 #include "distributed_device_profile_constants.h"
19 #include "distributed_device_profile_errors.h"
20 #include "distributed_device_profile_log.h"
21 #include "device_profile_manager.h"
22 #include "dp_services_constants.h"
23 #include "profile_utils.h"
24 #include "subscribe_profile_manager.h"
25
26 namespace OHOS {
27 namespace DistributedDeviceProfile {
28 IMPLEMENT_SINGLE_INSTANCE(DeviceProfileDao);
29 namespace {
30 const std::string TAG = "DeviceProfileDao";
31 }
32
Init()33 int32_t DeviceProfileDao::Init()
34 {
35 std::lock_guard<std::mutex> lock(rdbMutex_);
36 int32_t ret = RET_INIT;
37 if (!ProfileDataRdbAdapter::GetInstance().IsInit()) {
38 ret = ProfileDataRdbAdapter::GetInstance().Init();
39 if (ret != DP_SUCCESS) {
40 HILOGE("ProfileDataRdbAdapter Init failed");
41 return DP_INIT_DB_FAILED;
42 }
43 }
44 CreateTable();
45 CreateIndex();
46 HILOGI("end!");
47 return DP_SUCCESS;
48 }
49
UnInit()50 int32_t DeviceProfileDao::UnInit()
51 {
52 std::lock_guard<std::mutex> lock(rdbMutex_);
53 if (!ProfileDataRdbAdapter::GetInstance().IsInit()) {
54 HILOGE("ProfileDataRdbAdapter is UnInit");
55 return DP_SUCCESS;
56 }
57 int32_t ret = ProfileDataRdbAdapter::GetInstance().UnInit();
58 if (ret != DP_SUCCESS) {
59 HILOGE("ProfileDataRdbAdapter UnInit failed");
60 return DP_UNINIT_FAIL;
61 }
62 HILOGI("end!");
63 return DP_SUCCESS;
64 }
65
PutDeviceProfile(const DeviceProfile & deviceProfile)66 int32_t DeviceProfileDao::PutDeviceProfile(const DeviceProfile& deviceProfile)
67 {
68 ValuesBucket values;
69 DeviceProfileToEntries(deviceProfile, values);
70 int64_t rowId = ROWID_INIT;
71 int32_t ret = RET_INIT;
72 {
73 std::lock_guard<std::mutex> lock(rdbMutex_);
74 ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, DEVICE_PROFILE_TABLE, values);
75 if (ret != DP_SUCCESS) {
76 HILOGE("deviceprofile insert failed");
77 return DP_PUT_TRUST_DEVICE_PROFILE_FAIL;
78 }
79 }
80 std::shared_ptr<ResultSet> resultSet = ProfileDataRdbAdapter::GetInstance().Get(
81 SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID,
82 std::vector<ValueObject>{ ValueObject(deviceProfile.GetDeviceId()),
83 ValueObject(deviceProfile.GetUserId())});
84 if (resultSet == nullptr) {
85 HILOGE("resultSet is nullptr");
86 return DP_GET_RESULTSET_FAIL;
87 }
88 DeviceProfile profile(deviceProfile);
89 while (resultSet->GoToNextRow() == DP_SUCCESS) {
90 int32_t columnIndex = COLUMNINDEX_INIT;
91 int32_t id = DEVICE_PROFILE_ID_INIT;
92 resultSet->GetColumnIndex(ID, columnIndex);
93 resultSet->GetInt(columnIndex, id);
94 profile.SetId(id);
95 }
96 resultSet->Close();
97 HILOGI("end!");
98 return DP_SUCCESS;
99 }
100
GetDeviceProfiles(const DeviceProfileFilterOptions & filterOptions,std::vector<DeviceProfile> & deviceProfiles)101 int32_t DeviceProfileDao::GetDeviceProfiles(const DeviceProfileFilterOptions &filterOptions,
102 std::vector<DeviceProfile> &deviceProfiles)
103 {
104 std::string sql;
105 std::vector<ValueObject> condition;
106 CreateQuerySqlAndCondition(filterOptions, sql, condition);
107 std::shared_ptr<ResultSet> resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition);
108 if (resultSet == nullptr) {
109 HILOGE("resultSet is nullptr");
110 return DP_GET_RESULTSET_FAIL;
111 }
112 int32_t rowCount = ROWCOUNT_INIT;
113 resultSet->GetRowCount(rowCount);
114 if (rowCount == 0) {
115 HILOGE("by condition not find data");
116 resultSet->Close();
117 return DP_NOT_FIND_DATA;
118 }
119 while (resultSet->GoToNextRow() == DP_SUCCESS) {
120 DeviceProfile deviceProfile;
121 ConvertToDeviceProfile(resultSet, deviceProfile);
122 deviceProfiles.emplace_back(deviceProfile);
123 }
124 resultSet->Close();
125 if (deviceProfiles.empty()) {
126 return DP_NOT_FIND_DATA;
127 }
128 HILOGI("end!");
129 return DP_SUCCESS;
130 }
131
DeleteDeviceProfile(const DeviceProfile & deviceProfile)132 int32_t DeviceProfileDao::DeleteDeviceProfile(const DeviceProfile &deviceProfile)
133 {
134 {
135 std::lock_guard<std::mutex> lock(rdbMutex_);
136 int32_t deleteRows = DELETEROWS_INIT;
137 int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, DEVICE_PROFILE_TABLE, ID_EQUAL_CONDITION,
138 std::vector<ValueObject>{ ValueObject(deviceProfile.GetId()) });
139 if (ret != DP_SUCCESS) {
140 HILOGE("delete deviceprofile data failed, ret=%{public}d", ret);
141 return DP_DELETE_TRUST_DEVICE_PROFILE_FAIL;
142 }
143 }
144 HILOGI("end!");
145 return DP_SUCCESS;
146 }
147
UpdateDeviceProfile(const DeviceProfile & newProfile)148 int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &newProfile)
149 {
150 ValuesBucket values;
151 DeviceProfileToEntries(newProfile, values);
152 int32_t changeRowCnt = CHANGEROWCNT_INIT;
153 {
154 std::lock_guard<std::mutex> lock(rdbMutex_);
155 int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(
156 changeRowCnt, DEVICE_PROFILE_TABLE, values, ID_EQUAL_CONDITION,
157 std::vector<ValueObject>{ ValueObject(newProfile.GetId()) });
158 if (ret != DP_SUCCESS) {
159 HILOGE("Update deviceprofile table failed");
160 return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL;
161 }
162 }
163 HILOGI("end!");
164 return DP_SUCCESS;
165 }
166
CreateTable()167 int32_t DeviceProfileDao::CreateTable()
168 {
169 int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_PROFILE_TABLE_SQL);
170 if (ret != DP_SUCCESS) {
171 HILOGE("deviceprofile create failed");
172 return DP_CREATE_TABLE_FAIL;
173 }
174 return DP_SUCCESS;
175 }
176
CreateIndex()177 int32_t DeviceProfileDao::CreateIndex()
178 {
179 int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL);
180 if (ret != DP_SUCCESS) {
181 HILOGE("deviceprofile unique index create failed");
182 return DP_CREATE_UNIQUE_INDEX_FAIL;
183 }
184 ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_PROFILE_WISE_DEVICEID_INDEX_SQL);
185 if (ret != DP_SUCCESS) {
186 HILOGE("deviceprofile wiseDeviceId index create failed");
187 return DP_CREATE_INDEX_FAIL;
188 }
189 return DP_SUCCESS;
190 }
191
CreateQuerySqlAndCondition(const DeviceProfileFilterOptions & filterOptions,std::string & sql,std::vector<ValueObject> & condition)192 void DeviceProfileDao::CreateQuerySqlAndCondition(const DeviceProfileFilterOptions &filterOptions,
193 std::string &sql, std::vector<ValueObject> &condition)
194 {
195 sql = SELECT_DEVICE_PROFILE_TABLE;
196 bool matchCondition = false;
197 if (!filterOptions.GetDeviceProfileIds().empty()) {
198 sql += "id IN(";
199 GenerateSqlAndCondition(filterOptions.GetDeviceProfileIds(), sql, condition);
200 matchCondition = true;
201 }
202 if (!filterOptions.GetDeviceIds().empty()) {
203 sql += "deviceId IN(";
204 GenerateSqlAndCondition(filterOptions.GetDeviceIds(), sql, condition);
205 matchCondition = true;
206 }
207 if (filterOptions.GetUserId() != DEFAULT_USER_ID) {
208 sql += "userId = ? AND ";
209 condition.emplace_back(ValueObject(filterOptions.GetUserId()));
210 matchCondition = true;
211 }
212 if (!filterOptions.GetAccountId().empty()) {
213 sql += "accountId = ? AND ";
214 condition.emplace_back(ValueObject(filterOptions.GetAccountId()));
215 matchCondition = true;
216 }
217 if (!filterOptions.GetWiseDeviceIds().empty()) {
218 sql += "wiseDeviceId IN(";
219 GenerateSqlAndCondition(filterOptions.GetWiseDeviceIds(), sql, condition);
220 matchCondition = true;
221 }
222 if (matchCondition) {
223 sql.erase(sql.end() - AND_LENGTH, sql.end());
224 return;
225 }
226 sql.erase(sql.end() - WHERE_LENGTH, sql.end());
227 return;
228 }
229
DeviceProfileToEntries(const DeviceProfile & deviceProfile,ValuesBucket & values)230 int32_t DeviceProfileDao::DeviceProfileToEntries(const DeviceProfile &deviceProfile, ValuesBucket &values)
231 {
232 values.PutString(DEVICE_ID, deviceProfile.GetDeviceId());
233 values.PutString(DEVICE_MODEL, deviceProfile.GetDeviceModel());
234 values.PutString(DEV_TYPE, deviceProfile.GetDevType());
235 values.PutString(MANU, deviceProfile.GetManu());
236 values.PutString(SN, deviceProfile.GetSn());
237 values.PutString(PRODUCT_ID, deviceProfile.GetProductId());
238 values.PutString(PRODUCT_NAME, deviceProfile.GetProductName());
239 values.PutString(SUB_PRODUCT_ID, deviceProfile.GetSubProductId());
240 values.PutString(HIV, deviceProfile.GetHiv());
241 values.PutString(MAC, deviceProfile.GetMac());
242 values.PutString(FWV, deviceProfile.GetFwv());
243 values.PutString(HWV, deviceProfile.GetHwv());
244 values.PutString(SWV, deviceProfile.GetSwv());
245 values.PutInt(PROT_TYPE, deviceProfile.GetProtType());
246 values.PutString(DEVICE_NAME, deviceProfile.GetDeviceName());
247 values.PutString(WISE_USER_ID, deviceProfile.GetWiseUserId());
248 values.PutString(WISE_DEVICE_ID, deviceProfile.GetWiseDeviceId());
249 values.PutString(REGISTER_TIME, deviceProfile.GetRegisterTime());
250 values.PutString(INTERNAL_MODEL, deviceProfile.GetInternalModel());
251 values.PutString(MODIFY_TIME, deviceProfile.GetModifyTime());
252 values.PutString(SHARE_TIME, deviceProfile.GetShareTime());
253 values.PutInt(USERID, deviceProfile.GetUserId());
254 values.PutString(ACCOUNTID, deviceProfile.GetAccountId());
255 values.PutString(BLE_MAC, deviceProfile.GetBleMac());
256 values.PutString(BR_MAC, deviceProfile.GetBrMac());
257 values.PutString(SLE_MAC, deviceProfile.GetSleMac());
258 values.PutInt(SETUP_TYPE, deviceProfile.GetSetupType());
259 return DP_SUCCESS;
260 }
261
ConvertToDeviceProfile(std::shared_ptr<ResultSet> resultSet,DeviceProfile & deviceProfile)262 int32_t DeviceProfileDao::ConvertToDeviceProfile(
263 std::shared_ptr<ResultSet> resultSet, DeviceProfile& deviceProfile)
264 {
265 if (resultSet == nullptr) {
266 HILOGE("resultSet is nullptr");
267 return DP_GET_RESULTSET_FAIL;
268 }
269 RowEntity rowEntity;
270 if (resultSet->GetRow(rowEntity) != DP_SUCCESS) {
271 HILOGE("get resultSet failed");
272 return DP_GET_RESULTSET_FAIL;
273 }
274 deviceProfile.SetId(rowEntity.Get(ID));
275 deviceProfile.SetDeviceId(rowEntity.Get(DEVICE_ID));
276 deviceProfile.SetDeviceModel(rowEntity.Get(DEVICE_MODEL));
277 deviceProfile.SetDevType(rowEntity.Get(DEV_TYPE));
278 deviceProfile.SetManu(rowEntity.Get(MANU));
279 deviceProfile.SetSn(rowEntity.Get(SN));
280 deviceProfile.SetProductId(rowEntity.Get(PRODUCT_ID));
281 deviceProfile.SetProductName(rowEntity.Get(PRODUCT_NAME));
282 deviceProfile.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID));
283 deviceProfile.SetHiv(rowEntity.Get(HIV));
284 deviceProfile.SetMac(rowEntity.Get(MAC));
285 deviceProfile.SetFwv(rowEntity.Get(FWV));
286 deviceProfile.SetHwv(rowEntity.Get(HWV));
287 deviceProfile.SetSwv(rowEntity.Get(SWV));
288 deviceProfile.SetProtType(rowEntity.Get(PROT_TYPE));
289 deviceProfile.SetDeviceName(rowEntity.Get(DEVICE_NAME));
290 deviceProfile.SetWiseUserId(rowEntity.Get(WISE_USER_ID));
291 deviceProfile.SetWiseDeviceId(rowEntity.Get(WISE_DEVICE_ID));
292 deviceProfile.SetRegisterTime(rowEntity.Get(REGISTER_TIME));
293 deviceProfile.SetInternalModel(rowEntity.Get(INTERNAL_MODEL));
294 deviceProfile.SetModifyTime(rowEntity.Get(MODIFY_TIME));
295 deviceProfile.SetShareTime(rowEntity.Get(SHARE_TIME));
296 deviceProfile.SetUserId(rowEntity.Get(USERID));
297 deviceProfile.SetAccountId(rowEntity.Get(ACCOUNTID));
298 deviceProfile.SetBleMac(rowEntity.Get(BLE_MAC));
299 deviceProfile.SetBrMac(rowEntity.Get(BR_MAC));
300 deviceProfile.SetSleMac(rowEntity.Get(SLE_MAC));
301 deviceProfile.SetSetupType(rowEntity.Get(SETUP_TYPE));
302 return DP_SUCCESS;
303 }
304
GenerateSqlAndCondition(const std::vector<int32_t> & params,std::string & sql,std::vector<ValueObject> & condition)305 void DeviceProfileDao::GenerateSqlAndCondition(const std::vector<int32_t> ¶ms,
306 std::string &sql, std::vector<ValueObject> &condition)
307 {
308 for (auto param : params) {
309 sql += "?,";
310 condition.emplace_back(ValueObject(param));
311 }
312 sql.erase(sql.end() - 1, sql.end());
313 sql += ") AND ";
314 return;
315 }
316
GenerateSqlAndCondition(const std::vector<std::string> & params,std::string & sql,std::vector<ValueObject> & condition)317 void DeviceProfileDao::GenerateSqlAndCondition(const std::vector<std::string> ¶ms,
318 std::string &sql, std::vector<ValueObject> &condition)
319 {
320 for (auto param : params) {
321 sql += "?,";
322 condition.emplace_back(ValueObject(param));
323 }
324 sql.erase(sql.end() - 1, sql.end());
325 sql += ") AND ";
326 return;
327 }
328 } // namespace DistributedDeviceProfile
329 } // namespace OHOS
330