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 "profile_control_utils.h"
17
18 #include <map>
19
20 #include "distributed_device_profile_errors.h"
21 #include "distributed_device_profile_enums.h"
22 #include "distributed_device_profile_log.h"
23 #include "multi_user_manager.h"
24 #include "profile_cache.h"
25 #include "profile_utils.h"
26 #include "switch_adapter.h"
27
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 namespace {
31 const std::string TAG = "ProfileControlUtils";
32 }
PutDeviceProfile(std::shared_ptr<IKVAdapter> kvStore,const DeviceProfile & deviceProfile)33 int32_t ProfileControlUtils::PutDeviceProfile(std::shared_ptr<IKVAdapter> kvStore, const DeviceProfile& deviceProfile)
34 {
35 HILOGI("DeviceProfile : %{public}s!", deviceProfile.AnnoymizeDump().c_str());
36 if (kvStore == nullptr) {
37 HILOGE("kvStore is nullptr!");
38 return DP_INVALID_PARAMS;
39 }
40 if (!ProfileUtils::IsDevProfileValid(deviceProfile)) {
41 HILOGE("the profile is invalid!");
42 return DP_INVALID_PARAMS;
43 }
44 if (ProfileCache::GetInstance().IsDeviceProfileExist(deviceProfile)) {
45 HILOGI("the profile is exist!");
46 return DP_CACHE_EXIST;
47 }
48 std::map<std::string, std::string> entries;
49 ProfileUtils::DeviceProfileToEntries(deviceProfile, entries);
50 if (kvStore->PutBatch(entries) != DP_SUCCESS) {
51 HILOGE("PutDeviceProfile fail!");
52 return DP_PUT_KV_DB_FAIL;
53 }
54 ProfileCache::GetInstance().AddDeviceProfile(deviceProfile);
55 return DP_SUCCESS;
56 }
57
PutServiceProfile(std::shared_ptr<IKVAdapter> kvStore,const ServiceProfile & serviceProfile)58 int32_t ProfileControlUtils::PutServiceProfile(std::shared_ptr<IKVAdapter> kvStore,
59 const ServiceProfile& serviceProfile)
60 {
61 HILOGI("ServiceProfile : %{public}s!", serviceProfile.dump().c_str());
62 if (kvStore == nullptr) {
63 HILOGE("kvStore is nullptr!");
64 return DP_INVALID_PARAMS;
65 }
66 if (!ProfileUtils::IsSvrProfileValid(serviceProfile)) {
67 HILOGE("the profile is invalid!");
68 return DP_INVALID_PARAMS;
69 }
70 if (ProfileCache::GetInstance().IsServiceProfileExist(serviceProfile)) {
71 HILOGW("the profile is exist!");
72 return DP_CACHE_EXIST;
73 }
74 std::map<std::string, std::string> entries;
75 ProfileUtils::ServiceProfileToEntries(serviceProfile, entries);
76 if (kvStore->PutBatch(entries) != DP_SUCCESS) {
77 HILOGE("PutServiceProfile fail!");
78 return DP_PUT_KV_DB_FAIL;
79 }
80 ProfileCache::GetInstance().AddServiceProfile(serviceProfile);
81 return DP_SUCCESS;
82 }
83
PutServiceProfileBatch(std::shared_ptr<IKVAdapter> kvStore,const std::vector<ServiceProfile> & serviceProfiles)84 int32_t ProfileControlUtils::PutServiceProfileBatch(std::shared_ptr<IKVAdapter> kvStore,
85 const std::vector<ServiceProfile>& serviceProfiles)
86 {
87 HILOGD("PutServiceProfileBatch call!");
88 if (serviceProfiles.size() > MAX_SERVICE_SIZE) {
89 HILOGE("serviceProfiles size is too large!");
90 return DP_INVALID_PARAMS;
91 }
92 for (const auto& serviceProfile : serviceProfiles) {
93 int32_t putServiceResult = PutServiceProfile(kvStore, serviceProfile);
94 if (putServiceResult != DP_SUCCESS) {
95 HILOGE("PutServiceProfile fail, serviceProfile: %{public}s, errcode: %{public}d!",
96 serviceProfile.dump().c_str(), putServiceResult);
97 continue;
98 }
99 }
100 return DP_SUCCESS;
101 }
102
PutCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,const CharacteristicProfile & charProfile)103 int32_t ProfileControlUtils::PutCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,
104 const CharacteristicProfile& charProfile)
105 {
106 HILOGI("Profile : %{public}s!", charProfile.dump().c_str());
107 if (kvStore == nullptr) {
108 HILOGE("kvStore is nullptr!");
109 return DP_INVALID_PARAMS;
110 }
111 if (!ProfileUtils::IsCharProfileValid(charProfile)) {
112 HILOGE("the profile is invalid!");
113 return DP_INVALID_PARAMS;
114 }
115 if (ProfileCache::GetInstance().IsCharProfileExist(charProfile)) {
116 HILOGW("the profile is exist!");
117 return DP_CACHE_EXIST;
118 }
119
120 std::map<std::string, std::string> entries;
121 ProfileUtils::CharacteristicProfileToEntries(charProfile, entries);
122 if (kvStore->PutBatch(entries) != DP_SUCCESS) {
123 HILOGE("PutCharacteristicProfile fail!");
124 return DP_PUT_KV_DB_FAIL;
125 }
126 ProfileCache::GetInstance().AddCharProfile(charProfile);
127 return DP_SUCCESS;
128 }
129
PutSwitchCharacteristicProfile(const std::string & appId,const CharacteristicProfile & charProfile)130 int32_t ProfileControlUtils::PutSwitchCharacteristicProfile(const std::string& appId,
131 const CharacteristicProfile& charProfile)
132 {
133 HILOGI("Profile : %{public}s!", charProfile.dump().c_str());
134 if (!ProfileUtils::IsCharProfileValid(charProfile)) {
135 HILOGE("the profile is invalid!");
136 return DP_INVALID_PARAMS;
137 }
138 if (ProfileCache::GetInstance().IsCharProfileExist(charProfile)) {
139 HILOGW("the profile is exist!");
140 return DP_CACHE_EXIST;
141 }
142 uint32_t curSwitch = ProfileCache::GetInstance().GetSwitch();
143 HILOGD("PutSwitchCharacteristicProfile curSwitch:%{public}d", curSwitch);
144 uint32_t newSwitch = curSwitch;
145 int32_t res = ProfileCache::GetInstance().SetSwitchByProfile(charProfile, SWITCH_SERVICE_MAP, newSwitch);
146 if (res != DP_SUCCESS) {
147 HILOGE("set switch profile failed: %{public}d", res);
148 return res;
149 }
150 HILOGD("PutSwitchCharacteristicProfile newSwitch:%{public}d", newSwitch);
151 res = SwitchAdapter::GetInstance().PutSwitch(appId, newSwitch, CUR_SWITCH_LEN);
152 if (res != DP_SUCCESS) {
153 HILOGE("put switch failed: %{public}d", res);
154 return res;
155 }
156 ProfileCache::GetInstance().SetCurSwitch(newSwitch);
157 ProfileCache::GetInstance().AddCharProfile(charProfile);
158 return DP_SUCCESS;
159 }
160
PutSwitchCharacteristicProfileBatch(const std::string & appId,const std::vector<CharacteristicProfile> & charProfiles)161 int32_t ProfileControlUtils::PutSwitchCharacteristicProfileBatch(const std::string& appId,
162 const std::vector<CharacteristicProfile>& charProfiles)
163 {
164 HILOGI("charProfiles.size:%{public}zu", charProfiles.size());
165 if (charProfiles.empty() || appId.empty()) {
166 HILOGE("charProfiles or appId are empty");
167 return DP_INVALID_PARAMS;
168 }
169 if (charProfiles.size() > MAX_CHAR_SIZE) {
170 HILOGE("charProfiles size is too large!");
171 return DP_INVALID_PARAMS;
172 }
173 int32_t res = 0;
174 uint32_t curSwitch = ProfileCache::GetInstance().GetSwitch();
175 HILOGD("curSwitch:%{public}d", curSwitch);
176 uint32_t newSwitch = curSwitch;
177 for (auto item : charProfiles) {
178 if (!ProfileUtils::IsCharProfileValid(item)) {
179 HILOGE("a profile is invalid! charProfile: %{public}s", item.dump().c_str());
180 return DP_INVALID_PARAMS;
181 }
182 if (ProfileCache::GetInstance().IsCharProfileExist(item)) {
183 HILOGW("this profile is exist! charProfile: %{public}s", item.dump().c_str());
184 continue;
185 }
186 HILOGI("charProfile: %{public}s!", item.dump().c_str());
187 res = ProfileCache::GetInstance().SetSwitchByProfile(item, SWITCH_SERVICE_MAP, newSwitch);
188 if (res != DP_SUCCESS) {
189 HILOGW("set switch profile failed: %{public}d", res);
190 }
191 }
192 HILOGD("newSwitch:%{public}d", newSwitch);
193 res = SwitchAdapter::GetInstance().PutSwitch(appId, newSwitch, CUR_SWITCH_LEN);
194 if (res != DP_SUCCESS) {
195 HILOGE("put switch Batch failed: %{public}d", res);
196 return res;
197 }
198 ProfileCache::GetInstance().SetCurSwitch(newSwitch);
199 for (auto item : charProfiles) {
200 ProfileCache::GetInstance().AddCharProfile(item);
201 }
202 return DP_SUCCESS;
203 }
204
PutCharacteristicProfileBatch(std::shared_ptr<IKVAdapter> kvStore,const std::vector<CharacteristicProfile> & charProfiles)205 int32_t ProfileControlUtils::PutCharacteristicProfileBatch(std::shared_ptr<IKVAdapter> kvStore,
206 const std::vector<CharacteristicProfile>& charProfiles)
207 {
208 HILOGD("call!");
209 if (charProfiles.size() > MAX_CHAR_SIZE) {
210 HILOGE("charProfiles size is too large!");
211 return DP_INVALID_PARAMS;
212 }
213 for (const auto& charProfile : charProfiles) {
214 int32_t putCharacteristicResult = PutCharacteristicProfile(kvStore, charProfile);
215 if (putCharacteristicResult != DP_SUCCESS) {
216 HILOGE("PutCharacteristic fail, charProfile: %{public}s, errcode: %{public}d!", charProfile.dump().c_str(),
217 putCharacteristicResult);
218 continue;
219 }
220 }
221 return DP_SUCCESS;
222 }
223
GetDeviceProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,DeviceProfile & deviceProfile)224 int32_t ProfileControlUtils::GetDeviceProfile(std::shared_ptr<IKVAdapter> kvStore, const std::string& deviceId,
225 DeviceProfile& deviceProfile)
226 {
227 HILOGD("call!");
228 if (kvStore == nullptr) {
229 HILOGE("kvStore is nullptr!");
230 return DP_INVALID_PARAMS;
231 }
232 if (!ProfileUtils::IsKeyValid(deviceId)) {
233 HILOGE("the profile is invalid!");
234 return DP_INVALID_PARAMS;
235 }
236 HILOGI("GetDeviceProfile, deviceId: %{public}s!", ProfileUtils::GetAnonyString(deviceId).c_str());
237 if (ProfileCache::GetInstance().GetDeviceProfile(deviceId, deviceProfile) == DP_SUCCESS) {
238 HILOGI("GetDeviceProfile in cache!");
239 return DP_SUCCESS;
240 }
241 std::string dbKeyPrefix = ProfileUtils::GenerateDeviceProfileKey(deviceId);
242 std::map<std::string, std::string> values;
243 if (kvStore->GetByPrefix(dbKeyPrefix, values) != DP_SUCCESS) {
244 HILOGE("Get data fail!");
245 return DP_GET_KV_DB_FAIL;
246 }
247 ProfileUtils::EntriesToDeviceProfile(values, deviceProfile);
248 if (!ProfileUtils::IsDeviceProfileValid(deviceProfile)) {
249 HILOGE("deviceProfile is invalid!");
250 return DP_GET_KV_DB_FAIL;
251 }
252 HILOGD("GetDeviceProfile in db : %{public}s!", deviceProfile.AnnoymizeDump().c_str());
253 return DP_SUCCESS;
254 }
255
GetServiceProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,const std::string & serviceName,ServiceProfile & serviceProfile)256 int32_t ProfileControlUtils::GetServiceProfile(std::shared_ptr<IKVAdapter> kvStore, const std::string& deviceId,
257 const std::string& serviceName, ServiceProfile& serviceProfile)
258 {
259 HILOGD("call!");
260 if (kvStore == nullptr) {
261 HILOGE("kvStore is nullptr!");
262 return DP_INVALID_PARAMS;
263 }
264 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsKeyValid(serviceName)) {
265 HILOGE("the profile is invalid!");
266 return DP_INVALID_PARAMS;
267 }
268 HILOGI("deviceId: %{public}s, serviceName: %{public}s!",
269 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str());
270 if (ProfileCache::GetInstance().GetServiceProfile(deviceId, serviceName, serviceProfile) == DP_SUCCESS) {
271 HILOGI("GetServiceProfile in cache!");
272 return DP_SUCCESS;
273 }
274 std::string dbKeyPrefix = ProfileUtils::GenerateServiceProfileKey(deviceId, serviceName);
275 std::map<std::string, std::string> values;
276 if (kvStore->GetByPrefix(dbKeyPrefix, values) != DP_SUCCESS) {
277 HILOGE("Get data fail!");
278 return DP_GET_KV_DB_FAIL;
279 }
280 ProfileUtils::EntriesToServiceProfile(values, serviceProfile);
281 if (!ProfileUtils::IsServiceProfileValid(serviceProfile)) {
282 HILOGE("serviceProfile is invalid!");
283 return DP_GET_KV_DB_FAIL;
284 }
285 HILOGD("GetServiceProfile in db : %{public}s!", serviceProfile.dump().c_str());
286 return DP_SUCCESS;
287 }
288
GetCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,CharacteristicProfile & charProfile)289 int32_t ProfileControlUtils::GetCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore, const std::string& deviceId,
290 const std::string& serviceName, const std::string& characteristicKey, CharacteristicProfile& charProfile)
291 {
292 HILOGI("deviceId: %{public}s, serviceName: %{public}s, charKey: %{public}s!",
293 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
294 if (kvStore == nullptr) {
295 HILOGE("kvStore is nullptr! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
296 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
297 return DP_INVALID_PARAMS;
298 }
299 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsKeyValid(serviceName) ||
300 !ProfileUtils::IsKeyValid(characteristicKey)) {
301 HILOGE("the profile is invalid! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
302 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
303 return DP_INVALID_PARAMS;
304 }
305 if (ProfileCache::GetInstance().GetCharacteristicProfile(deviceId, serviceName, characteristicKey, charProfile)
306 == DP_SUCCESS) {
307 HILOGI("GetCharProfile in cache! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
308 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
309 return DP_SUCCESS;
310 }
311 std::map<std::string, std::string> values;
312 if (ProfileUtils::IsNeedAddOhSuffix(serviceName, true)) {
313 std::string profileKeyPrefix = ProfileUtils::GenerateCharProfileKey(deviceId,
314 ProfileUtils::CheckAndAddOhSuffix(serviceName, true), characteristicKey);
315 if (kvStore->GetByPrefix(profileKeyPrefix, values) != DP_SUCCESS) {
316 HILOGE("Get data by oh suffix fail! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
317 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
318 values.clear();
319 }
320 }
321 if (values.empty()) {
322 std::string profileKeyPrefix = ProfileUtils::GenerateCharProfileKey(deviceId, serviceName, characteristicKey);
323 if (kvStore->GetByPrefix(profileKeyPrefix, values) != DP_SUCCESS) {
324 HILOGE("Get data fail! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
325 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
326 return DP_GET_KV_DB_FAIL;
327 }
328 }
329 ProfileUtils::EntriesToCharProfile(values, charProfile);
330 if (!ProfileUtils::IsCharacteristicProfileValid(charProfile)) {
331 HILOGE("charProfile is invalid!");
332 return DP_GET_KV_DB_FAIL;
333 }
334 HILOGD("GetCharacteristicProfile in db : %{public}s!", charProfile.dump().c_str());
335 return DP_SUCCESS;
336 }
337
RefreshLocalSwitchProfile(const std::string & appId)338 int32_t ProfileControlUtils::RefreshLocalSwitchProfile(const std::string& appId)
339 {
340 HILOGD("call!");
341 std::string localNetwork = ProfileCache::GetInstance().GetLocalNetworkId();
342 std::string localUdid = ProfileCache::GetInstance().GetLocalUdid();
343 if (localNetwork.empty() || localUdid.empty() || appId.empty()) {
344 HILOGE("params are empty");
345 return DP_INVALID_PARAMS;
346 }
347 uint32_t newSwitch = 0;
348 uint32_t switchLength = 0;
349 int32_t res = SwitchAdapter::GetInstance().GetSwitch(appId, localNetwork, newSwitch, switchLength);
350 if (res != DP_SUCCESS) {
351 HILOGE("GetSwitch failed, res: %{public}d", res);
352 return DP_GET_KV_DB_FAIL;
353 }
354 HILOGI("GetSwitch, newSwitch: %{public}d", newSwitch);
355 for (int32_t i = static_cast<int32_t>(SwitchFlag::SWITCH_FLAG_MIN) + NUM_1;
356 i < static_cast<int32_t>(SwitchFlag::SWITCH_FLAG_MAX); ++i) {
357 HILOGD("Find Switch, idx: %{public}d", i);
358 std::string itemSwitchValue = std::to_string((newSwitch >> i) & NUM_1);
359 std::string serviceName;
360 res = ProfileCache::GetInstance().GetServiceNameByPos(i, SWITCH_SERVICE_MAP, serviceName);
361 if (res != DP_SUCCESS || serviceName.empty()) {
362 HILOGE("GetServiceNameByPos failed, serviceName:%{public}s", serviceName.c_str());
363 continue;
364 }
365 const CharacteristicProfile newSwitchProfile = {localUdid, serviceName, SWITCH_STATUS, itemSwitchValue};
366 ProfileCache::GetInstance().AddCharProfile(newSwitchProfile);
367 }
368 ProfileCache::GetInstance().SetCurSwitch(newSwitch);
369 HILOGI("update curLocalSwitch: %{public}d", ProfileCache::GetInstance().GetSwitch());
370 return DP_SUCCESS;
371 }
372
GetSwitchCharacteristicProfile(const std::string & appId,const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,CharacteristicProfile & charProfile)373 int32_t ProfileControlUtils::GetSwitchCharacteristicProfile(const std::string& appId, const std::string& deviceId,
374 const std::string& serviceName, const std::string& characteristicKey, CharacteristicProfile& charProfile)
375 {
376 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsKeyValid(serviceName) ||
377 !ProfileUtils::IsKeyValid(characteristicKey) || appId.empty()) {
378 HILOGE("params are invalid!");
379 return DP_INVALID_PARAMS;
380 }
381 HILOGI("deviceId: %{public}s, serviceName: %{public}s, charKey: %{public}s!",
382 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
383 if (ProfileCache::GetInstance().GetCharacteristicProfile(deviceId, serviceName, characteristicKey, charProfile)
384 == DP_SUCCESS) {
385 HILOGI("GetCharProfile in cache: %{public}s!", charProfile.dump().c_str());
386 return DP_SUCCESS;
387 }
388 const CharacteristicProfile profile(deviceId, serviceName, characteristicKey, "");
389 if (!ProfileCache::GetInstance().IsSwitchValid(profile, SWITCH_SERVICE_MAP, SWITCH_OPERATE_GET)) {
390 HILOGE("params invalid");
391 return DP_INVALID_PARAMS;
392 }
393 std::string netWorkId;
394 int32_t res = ProfileCache::GetInstance().GetNetWorkIdByUdid(deviceId, netWorkId);
395 if (res != DP_SUCCESS) {
396 HILOGE("GetNetWorkIdByUdid failed, res: %{public}d", res);
397 return DP_GET_KV_DB_FAIL;
398 }
399 uint32_t switchValue;
400 uint32_t switchLength;
401 res = SwitchAdapter::GetInstance().GetSwitch(appId, netWorkId, switchValue, switchLength);
402 if (res != DP_SUCCESS) {
403 HILOGE("GetSwitch failed, res: %{public}d", res);
404 return DP_GET_KV_DB_FAIL;
405 }
406 HILOGD("GetSwitch success, switchValue: %{public}d", switchValue);
407 charProfile.SetDeviceId(deviceId);
408 charProfile.SetServiceName(serviceName);
409 charProfile.SetCharacteristicKey(characteristicKey);
410 res = ProfileCache::GetInstance().SetSwitchProfile(charProfile, switchValue);
411 if (res != DP_SUCCESS) {
412 HILOGE("SetSwitchProfile failed, res: %{public}d", res);
413 return DP_GET_KV_DB_FAIL;
414 }
415 HILOGI("success : %{public}s!", charProfile.dump().c_str());
416 ProfileCache::GetInstance().AddCharProfile(charProfile);
417 return DP_SUCCESS;
418 }
419
DeleteServiceProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,const std::string & serviceName,bool isMultiUser,int32_t userId)420 int32_t ProfileControlUtils::DeleteServiceProfile(std::shared_ptr<IKVAdapter> kvStore, const std::string& deviceId,
421 const std::string& serviceName, bool isMultiUser, int32_t userId)
422 {
423 HILOGD("call!");
424 if (kvStore == nullptr) {
425 HILOGE("kvStore is nullptr!");
426 return DP_INVALID_PARAMS;
427 }
428 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsLocalUdid(deviceId) ||
429 !ProfileUtils::IsKeyValid(serviceName)) {
430 HILOGE("the profile is invalid!");
431 return DP_INVALID_PARAMS;
432 }
433 HILOGI("deviceId: %{public}s, serviceName: %{public}s!",
434 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str());
435 std::vector<std::string> keys;
436 if (isMultiUser) {
437 ProfileUtils::GenerateServiceDBkeys(deviceId, serviceName, keys, true, userId);
438 if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == userId) {
439 ProfileUtils::GenerateServiceDBkeys(deviceId, serviceName, keys);
440 } else {
441 HILOGI("the profile does not belong to the current user.");
442 }
443 } else {
444 ProfileUtils::GenerateServiceDBkeys(deviceId, serviceName, keys);
445 }
446 if (kvStore->DeleteBatch(keys) != DP_SUCCESS) {
447 HILOGE("DeleteServiceProfile fail!");
448 return DP_DEL_KV_DB_FAIL;
449 }
450 ProfileCache::GetInstance().DeleteServiceProfile(deviceId, serviceName);
451 return DP_SUCCESS;
452 }
453
DeleteCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,bool isMultiUser,int32_t userId)454 int32_t ProfileControlUtils::DeleteCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,
455 const std::string& deviceId, const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser,
456 int32_t userId)
457 {
458 HILOGI("call!");
459 if (kvStore == nullptr) {
460 HILOGE("kvStore is nullptr!");
461 return DP_INVALID_PARAMS;
462 }
463 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsLocalUdid(deviceId) ||
464 !ProfileUtils::IsKeyValid(serviceName) || !ProfileUtils::IsKeyValid(characteristicKey)) {
465 HILOGE("the profile is invalid!");
466 return DP_INVALID_PARAMS;
467 }
468 HILOGI("deviceId: %{public}s, serviceName: %{public}s, charKey: %{public}s!",
469 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
470 std::vector<std::string> keys;
471 if (isMultiUser) {
472 ProfileUtils::GenerateCharacteristicDBkeys(deviceId, serviceName, characteristicKey, keys, true, userId);
473 if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == userId) {
474 ProfileUtils::GenerateCharacteristicDBkeys(deviceId, serviceName, characteristicKey, keys);
475 } else {
476 HILOGI("the profile does not belong to the current user.");
477 }
478 } else {
479 ProfileUtils::GenerateCharacteristicDBkeys(deviceId, serviceName, characteristicKey, keys);
480 }
481 if (kvStore->DeleteBatch(keys) != DP_SUCCESS) {
482 HILOGE("DeleteCharacteristicProfile fail!");
483 return DP_DEL_KV_DB_FAIL;
484 }
485 ProfileCache::GetInstance().DeleteCharProfile(deviceId, serviceName, characteristicKey);
486 return DP_SUCCESS;
487 }
488
GetAllDeviceProfile(std::shared_ptr<IKVAdapter> kvStore,std::vector<DeviceProfile> & deviceProfiles)489 int32_t ProfileControlUtils::GetAllDeviceProfile(std::shared_ptr<IKVAdapter> kvStore,
490 std::vector<DeviceProfile>& deviceProfiles)
491 {
492 HILOGD("call!");
493 std::map<std::string, std::string> values;
494 if (kvStore == nullptr) {
495 HILOGE("kvStore is nullptr!");
496 return DP_INVALID_PARAMS;
497 }
498 if (kvStore->GetByPrefix(DEV_PREFIX, values) != DP_SUCCESS) {
499 HILOGE("Get data fail!");
500 return DP_GET_KV_DB_FAIL;
501 }
502 std::map<std::string, std::map<std::string, std::string>> profileEntries;
503 for (const auto& item : values) {
504 std::string dbKey = item.first;
505 std::string dbValue = item.second;
506 std::string profileKey = ProfileUtils::GetProfileKey(dbKey);
507 profileEntries[profileKey].emplace(dbKey, dbValue);
508 }
509 for (const auto& item : profileEntries) {
510 DeviceProfile deviceProfile;
511 ProfileUtils::EntriesToDeviceProfile(item.second, deviceProfile);
512 deviceProfiles.push_back(deviceProfile);
513 }
514 return DP_SUCCESS;
515 }
516
GetAllServiceProfile(std::shared_ptr<IKVAdapter> kvStore,std::vector<ServiceProfile> & serviceProfiles)517 int32_t ProfileControlUtils::GetAllServiceProfile(std::shared_ptr<IKVAdapter> kvStore,
518 std::vector<ServiceProfile>& serviceProfiles)
519 {
520 HILOGD("call!");
521 std::map<std::string, std::string> values;
522 if (kvStore == nullptr) {
523 HILOGE("kvStore is nullptr!");
524 return DP_INVALID_PARAMS;
525 }
526 if (kvStore->GetByPrefix(SVR_PREFIX, values) != DP_SUCCESS) {
527 HILOGE("Get data fail!");
528 return DP_GET_KV_DB_FAIL;
529 }
530 std::map<std::string, std::map<std::string, std::string>> profileEntries;
531 for (const auto& item : values) {
532 std::string dbKey = item.first;
533 std::string dbValue = item.second;
534 std::string profileKey = ProfileUtils::GetProfileKey(dbKey);
535 profileEntries[profileKey].emplace(dbKey, dbValue);
536 }
537 for (const auto &item : profileEntries) {
538 ServiceProfile serviceProfile;
539 ProfileUtils::EntriesToServiceProfile(item.second, serviceProfile);
540 serviceProfiles.push_back(serviceProfile);
541 }
542 return DP_SUCCESS;
543 }
544
GetAllCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,std::vector<CharacteristicProfile> & charProfiles)545 int32_t ProfileControlUtils::GetAllCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,
546 std::vector<CharacteristicProfile>& charProfiles)
547 {
548 HILOGD("call!");
549 std::map<std::string, std::string> values;
550 if (kvStore == nullptr) {
551 HILOGE("kvStore is nullptr!");
552 return DP_INVALID_PARAMS;
553 }
554 if (kvStore->GetByPrefix(CHAR_PREFIX, values) != DP_SUCCESS) {
555 HILOGE("Get data fail!");
556 return DP_GET_KV_DB_FAIL;
557 }
558 std::map<std::string, std::map<std::string, std::string>> profileEntries;
559 for (auto item : values) {
560 std::string dbKey = item.first;
561 std::string dbValue = item.second;
562 std::string profileKey = ProfileUtils::GetProfileKey(dbKey);
563 profileEntries[profileKey].emplace(dbKey, dbValue);
564 }
565 for (const auto& item : profileEntries) {
566 CharacteristicProfile charProfile;
567 ProfileUtils::EntriesToCharProfile(item.second, charProfile);
568 charProfiles.push_back(charProfile);
569 }
570 return DP_SUCCESS;
571 }
572 } // namespace DeviceProfile
573 } // namespace OHOS
574