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