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 (characteristicKey == STATIC_CHARACTERISTIC_KEY || characteristicKey == SWITCH_STATUS) {
306 if (ProfileCache::GetInstance().GetCharacteristicProfile(deviceId, serviceName, characteristicKey, charProfile)
307 == DP_SUCCESS) {
308 HILOGI("GetCharProfile in cache! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
309 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
310 return DP_SUCCESS;
311 }
312 }
313 std::map<std::string, std::string> values;
314 if (ProfileUtils::IsNeedAddOhSuffix(serviceName, true)) {
315 std::string profileKeyPrefix = ProfileUtils::GenerateCharProfileKey(deviceId,
316 ProfileUtils::CheckAndAddOhSuffix(serviceName, true), characteristicKey);
317 if (kvStore->GetByPrefix(profileKeyPrefix, values) != DP_SUCCESS) {
318 HILOGE("Get data by oh suffix fail! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
319 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
320 values.clear();
321 }
322 }
323 if (values.empty()) {
324 std::string profileKeyPrefix = ProfileUtils::GenerateCharProfileKey(deviceId, serviceName, characteristicKey);
325 if (kvStore->GetByPrefix(profileKeyPrefix, values) != DP_SUCCESS) {
326 HILOGE("Get data fail! devId: %{public}s, svrName: %{public}s, charKey: %{public}s!",
327 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
328 return DP_GET_KV_DB_FAIL;
329 }
330 }
331 ProfileUtils::EntriesToCharProfile(values, charProfile);
332 if (!ProfileUtils::IsCharacteristicProfileValid(charProfile)) {
333 HILOGE("charProfile is invalid!");
334 return DP_GET_KV_DB_FAIL;
335 }
336 HILOGD("GetCharacteristicProfile in db : %{public}s!", charProfile.dump().c_str());
337 return DP_SUCCESS;
338 }
339
RefreshLocalSwitchProfile(const std::string & appId)340 int32_t ProfileControlUtils::RefreshLocalSwitchProfile(const std::string& appId)
341 {
342 HILOGD("call!");
343 std::string localNetwork = ProfileCache::GetInstance().GetLocalNetworkId();
344 std::string localUdid = ProfileCache::GetInstance().GetLocalUdid();
345 if (localNetwork.empty() || localUdid.empty() || appId.empty()) {
346 HILOGE("params are empty");
347 return DP_INVALID_PARAMS;
348 }
349 uint32_t newSwitch = 0;
350 uint32_t switchLength = 0;
351 int32_t res = SwitchAdapter::GetInstance().GetSwitch(appId, localNetwork, newSwitch, switchLength);
352 if (res != DP_SUCCESS) {
353 HILOGE("GetSwitch failed, res: %{public}d", res);
354 return DP_GET_KV_DB_FAIL;
355 }
356 HILOGI("GetSwitch, newSwitch: %{public}d", newSwitch);
357 for (int32_t i = static_cast<int32_t>(SwitchFlag::SWITCH_FLAG_MIN) + NUM_1;
358 i < static_cast<int32_t>(SwitchFlag::SWITCH_FLAG_MAX); ++i) {
359 HILOGD("Find Switch, idx: %{public}d", i);
360 std::string itemSwitchValue = std::to_string((newSwitch >> i) & NUM_1);
361 std::string serviceName;
362 res = ProfileCache::GetInstance().GetServiceNameByPos(i, SWITCH_SERVICE_MAP, serviceName);
363 if (res != DP_SUCCESS || serviceName.empty()) {
364 HILOGE("GetServiceNameByPos failed, serviceName:%{public}s", serviceName.c_str());
365 continue;
366 }
367 const CharacteristicProfile newSwitchProfile = {localUdid, serviceName, SWITCH_STATUS, itemSwitchValue};
368 ProfileCache::GetInstance().AddCharProfile(newSwitchProfile);
369 }
370 ProfileCache::GetInstance().SetCurSwitch(newSwitch);
371 HILOGI("update curLocalSwitch: %{public}d", ProfileCache::GetInstance().GetSwitch());
372 return DP_SUCCESS;
373 }
374
GetSwitchCharacteristicProfile(const std::string & appId,const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,CharacteristicProfile & charProfile)375 int32_t ProfileControlUtils::GetSwitchCharacteristicProfile(const std::string& appId, const std::string& deviceId,
376 const std::string& serviceName, const std::string& characteristicKey, CharacteristicProfile& charProfile)
377 {
378 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsKeyValid(serviceName) ||
379 !ProfileUtils::IsKeyValid(characteristicKey) || appId.empty()) {
380 HILOGE("params are invalid!");
381 return DP_INVALID_PARAMS;
382 }
383 HILOGI("deviceId: %{public}s, serviceName: %{public}s, charKey: %{public}s!",
384 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
385 if (ProfileCache::GetInstance().GetCharacteristicProfile(deviceId, serviceName, characteristicKey, charProfile)
386 == DP_SUCCESS) {
387 HILOGI("GetCharProfile in cache: %{public}s!", charProfile.dump().c_str());
388 return DP_SUCCESS;
389 }
390 const CharacteristicProfile profile(deviceId, serviceName, characteristicKey, "");
391 if (!ProfileCache::GetInstance().IsSwitchValid(profile, SWITCH_SERVICE_MAP, SWITCH_OPERATE_GET)) {
392 HILOGE("params invalid");
393 return DP_INVALID_PARAMS;
394 }
395 std::string netWorkId;
396 int32_t res = ProfileCache::GetInstance().GetNetWorkIdByUdid(deviceId, netWorkId);
397 if (res != DP_SUCCESS) {
398 HILOGE("GetNetWorkIdByUdid failed, res: %{public}d", res);
399 return DP_GET_KV_DB_FAIL;
400 }
401 uint32_t switchValue;
402 uint32_t switchLength;
403 res = SwitchAdapter::GetInstance().GetSwitch(appId, netWorkId, switchValue, switchLength);
404 if (res != DP_SUCCESS) {
405 HILOGE("GetSwitch failed, res: %{public}d", res);
406 return DP_GET_KV_DB_FAIL;
407 }
408 HILOGD("GetSwitch success, switchValue: %{public}d", switchValue);
409 charProfile.SetDeviceId(deviceId);
410 charProfile.SetServiceName(serviceName);
411 charProfile.SetCharacteristicKey(characteristicKey);
412 res = ProfileCache::GetInstance().SetSwitchProfile(charProfile, switchValue);
413 if (res != DP_SUCCESS) {
414 HILOGE("SetSwitchProfile failed, res: %{public}d", res);
415 return DP_GET_KV_DB_FAIL;
416 }
417 HILOGI("success : %{public}s!", charProfile.dump().c_str());
418 ProfileCache::GetInstance().AddCharProfile(charProfile);
419 return DP_SUCCESS;
420 }
421
DeleteServiceProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,const std::string & serviceName,bool isMultiUser,int32_t userId)422 int32_t ProfileControlUtils::DeleteServiceProfile(std::shared_ptr<IKVAdapter> kvStore, const std::string& deviceId,
423 const std::string& serviceName, bool isMultiUser, int32_t userId)
424 {
425 HILOGD("call!");
426 if (kvStore == nullptr) {
427 HILOGE("kvStore is nullptr!");
428 return DP_INVALID_PARAMS;
429 }
430 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsLocalUdid(deviceId) ||
431 !ProfileUtils::IsKeyValid(serviceName)) {
432 HILOGE("the profile is invalid!");
433 return DP_INVALID_PARAMS;
434 }
435 HILOGI("deviceId: %{public}s, serviceName: %{public}s!",
436 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str());
437 std::vector<std::string> keys;
438 if (isMultiUser) {
439 ProfileUtils::GenerateServiceDBkeys(deviceId, serviceName, keys, true, userId);
440 if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == userId) {
441 ProfileUtils::GenerateServiceDBkeys(deviceId, serviceName, keys);
442 } else {
443 HILOGI("the profile does not belong to the current user.");
444 }
445 } else {
446 ProfileUtils::GenerateServiceDBkeys(deviceId, serviceName, keys);
447 }
448 if (kvStore->DeleteBatch(keys) != DP_SUCCESS) {
449 HILOGE("DeleteServiceProfile fail!");
450 return DP_DEL_KV_DB_FAIL;
451 }
452 ProfileCache::GetInstance().DeleteServiceProfile(deviceId, serviceName);
453 return DP_SUCCESS;
454 }
455
DeleteCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,const std::string & deviceId,const std::string & serviceName,const std::string & characteristicKey,bool isMultiUser,int32_t userId)456 int32_t ProfileControlUtils::DeleteCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,
457 const std::string& deviceId, const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser,
458 int32_t userId)
459 {
460 HILOGI("call!");
461 if (kvStore == nullptr) {
462 HILOGE("kvStore is nullptr!");
463 return DP_INVALID_PARAMS;
464 }
465 if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsLocalUdid(deviceId) ||
466 !ProfileUtils::IsKeyValid(serviceName) || !ProfileUtils::IsKeyValid(characteristicKey)) {
467 HILOGE("the profile is invalid!");
468 return DP_INVALID_PARAMS;
469 }
470 HILOGI("deviceId: %{public}s, serviceName: %{public}s, charKey: %{public}s!",
471 ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str());
472 std::vector<std::string> keys;
473 if (isMultiUser) {
474 ProfileUtils::GenerateCharacteristicDBkeys(deviceId, serviceName, characteristicKey, keys, true, userId);
475 if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == userId) {
476 ProfileUtils::GenerateCharacteristicDBkeys(deviceId, serviceName, characteristicKey, keys);
477 } else {
478 HILOGI("the profile does not belong to the current user.");
479 }
480 } else {
481 ProfileUtils::GenerateCharacteristicDBkeys(deviceId, serviceName, characteristicKey, keys);
482 }
483 if (kvStore->DeleteBatch(keys) != DP_SUCCESS) {
484 HILOGE("DeleteCharacteristicProfile fail!");
485 return DP_DEL_KV_DB_FAIL;
486 }
487 ProfileCache::GetInstance().DeleteCharProfile(deviceId, serviceName, characteristicKey);
488 return DP_SUCCESS;
489 }
490
GetAllDeviceProfile(std::shared_ptr<IKVAdapter> kvStore,std::vector<DeviceProfile> & deviceProfiles)491 int32_t ProfileControlUtils::GetAllDeviceProfile(std::shared_ptr<IKVAdapter> kvStore,
492 std::vector<DeviceProfile>& deviceProfiles)
493 {
494 HILOGD("call!");
495 std::map<std::string, std::string> values;
496 if (kvStore == nullptr) {
497 HILOGE("kvStore is nullptr!");
498 return DP_INVALID_PARAMS;
499 }
500 if (kvStore->GetByPrefix(DEV_PREFIX, values) != DP_SUCCESS) {
501 HILOGE("Get data fail!");
502 return DP_GET_KV_DB_FAIL;
503 }
504 std::map<std::string, std::map<std::string, std::string>> profileEntries;
505 for (const auto& item : values) {
506 std::string dbKey = item.first;
507 std::string dbValue = item.second;
508 std::string profileKey = ProfileUtils::GetProfileKey(dbKey);
509 profileEntries[profileKey].emplace(dbKey, dbValue);
510 }
511 for (const auto& item : profileEntries) {
512 DeviceProfile deviceProfile;
513 ProfileUtils::EntriesToDeviceProfile(item.second, deviceProfile);
514 deviceProfiles.push_back(deviceProfile);
515 }
516 return DP_SUCCESS;
517 }
518
GetAllServiceProfile(std::shared_ptr<IKVAdapter> kvStore,std::vector<ServiceProfile> & serviceProfiles)519 int32_t ProfileControlUtils::GetAllServiceProfile(std::shared_ptr<IKVAdapter> kvStore,
520 std::vector<ServiceProfile>& serviceProfiles)
521 {
522 HILOGD("call!");
523 std::map<std::string, std::string> values;
524 if (kvStore == nullptr) {
525 HILOGE("kvStore is nullptr!");
526 return DP_INVALID_PARAMS;
527 }
528 if (kvStore->GetByPrefix(SVR_PREFIX, values) != DP_SUCCESS) {
529 HILOGE("Get data fail!");
530 return DP_GET_KV_DB_FAIL;
531 }
532 std::map<std::string, std::map<std::string, std::string>> profileEntries;
533 for (const auto& item : values) {
534 std::string dbKey = item.first;
535 std::string dbValue = item.second;
536 std::string profileKey = ProfileUtils::GetProfileKey(dbKey);
537 profileEntries[profileKey].emplace(dbKey, dbValue);
538 }
539 for (const auto &item : profileEntries) {
540 ServiceProfile serviceProfile;
541 ProfileUtils::EntriesToServiceProfile(item.second, serviceProfile);
542 serviceProfiles.push_back(serviceProfile);
543 }
544 return DP_SUCCESS;
545 }
546
GetAllCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,std::vector<CharacteristicProfile> & charProfiles)547 int32_t ProfileControlUtils::GetAllCharacteristicProfile(std::shared_ptr<IKVAdapter> kvStore,
548 std::vector<CharacteristicProfile>& charProfiles)
549 {
550 HILOGD("call!");
551 std::map<std::string, std::string> values;
552 if (kvStore == nullptr) {
553 HILOGE("kvStore is nullptr!");
554 return DP_INVALID_PARAMS;
555 }
556 if (kvStore->GetByPrefix(CHAR_PREFIX, values) != DP_SUCCESS) {
557 HILOGE("Get data fail!");
558 return DP_GET_KV_DB_FAIL;
559 }
560 std::map<std::string, std::map<std::string, std::string>> profileEntries;
561 for (auto item : values) {
562 std::string dbKey = item.first;
563 std::string dbValue = item.second;
564 std::string profileKey = ProfileUtils::GetProfileKey(dbKey);
565 profileEntries[profileKey].emplace(dbKey, dbValue);
566 }
567 for (const auto& item : profileEntries) {
568 CharacteristicProfile charProfile;
569 ProfileUtils::EntriesToCharProfile(item.second, charProfile);
570 charProfiles.push_back(charProfile);
571 }
572 return DP_SUCCESS;
573 }
574 } // namespace DeviceProfile
575 } // namespace OHOS
576