1f8af9c48Sopenharmony_ci/*
2f8af9c48Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3f8af9c48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f8af9c48Sopenharmony_ci * you may not use this file except in compliance with the License.
5f8af9c48Sopenharmony_ci * You may obtain a copy of the License at
6f8af9c48Sopenharmony_ci *
7f8af9c48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f8af9c48Sopenharmony_ci *
9f8af9c48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f8af9c48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f8af9c48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f8af9c48Sopenharmony_ci * See the License for the specific language governing permissions and
13f8af9c48Sopenharmony_ci * limitations under the License.
14f8af9c48Sopenharmony_ci */
15f8af9c48Sopenharmony_ci
16f8af9c48Sopenharmony_ci#include "profile_utils.h"
17f8af9c48Sopenharmony_ci
18f8af9c48Sopenharmony_ci#include <codecvt>
19f8af9c48Sopenharmony_ci#include <locale>
20f8af9c48Sopenharmony_ci#include <regex>
21f8af9c48Sopenharmony_ci#include <unordered_set>
22f8af9c48Sopenharmony_ci
23f8af9c48Sopenharmony_ci#include "cJSON.h"
24f8af9c48Sopenharmony_ci#include "device_manager.h"
25f8af9c48Sopenharmony_ci#include "dm_constants.h"
26f8af9c48Sopenharmony_ci#include "rdb_errno.h"
27f8af9c48Sopenharmony_ci
28f8af9c48Sopenharmony_ci#include "content_sensor_manager_utils.h"
29f8af9c48Sopenharmony_ci#include "distributed_device_profile_constants.h"
30f8af9c48Sopenharmony_ci#include "distributed_device_profile_enums.h"
31f8af9c48Sopenharmony_ci#include "distributed_device_profile_errors.h"
32f8af9c48Sopenharmony_ci#include "distributed_device_profile_log.h"
33f8af9c48Sopenharmony_ci
34f8af9c48Sopenharmony_cinamespace OHOS {
35f8af9c48Sopenharmony_cinamespace DistributedDeviceProfile {
36f8af9c48Sopenharmony_ciusing namespace OHOS::DistributedHardware;
37f8af9c48Sopenharmony_ci
38f8af9c48Sopenharmony_cinamespace {
39f8af9c48Sopenharmony_ci    const std::string TAG = "ProfileUtils";
40f8af9c48Sopenharmony_ci    const std::unordered_set<std::string> NEED_ADD_OH_SUFFIX_DEV_PROFILES { OS_TYPE, OS_VERSION };
41f8af9c48Sopenharmony_ci    const std::unordered_set<std::string> NEED_ADD_OH_SUFFIX_SVR_NAMES { "DistributeModemService",
42f8af9c48Sopenharmony_ci        "multiScreenAssistantService", "appInfo" };
43f8af9c48Sopenharmony_ci}
44f8af9c48Sopenharmony_ci
45f8af9c48Sopenharmony_cistd::string ProfileUtils::GetDbKeyAnonyString(const std::string& dbKey)
46f8af9c48Sopenharmony_ci{
47f8af9c48Sopenharmony_ci    constexpr size_t DEVICE_ID_INDEX = 1;
48f8af9c48Sopenharmony_ci    std::vector<std::string> splitKeys;
49f8af9c48Sopenharmony_ci    if (ProfileUtils::SplitString(dbKey, SEPARATOR, splitKeys) != DP_SUCCESS ||
50f8af9c48Sopenharmony_ci        splitKeys.size() <= DEVICE_ID_INDEX) {
51f8af9c48Sopenharmony_ci        return GetAnonyString(dbKey);
52f8af9c48Sopenharmony_ci    }
53f8af9c48Sopenharmony_ci    for (size_t i = DEVICE_ID_INDEX; i < splitKeys.size(); i++) {
54f8af9c48Sopenharmony_ci        splitKeys[i] = GetAnonyString(splitKeys[i]);
55f8af9c48Sopenharmony_ci    }
56f8af9c48Sopenharmony_ci    return JoinString(splitKeys, SEPARATOR);
57f8af9c48Sopenharmony_ci}
58f8af9c48Sopenharmony_ci
59f8af9c48Sopenharmony_cistd::string ProfileUtils::GetAnonyString(const std::string& value)
60f8af9c48Sopenharmony_ci{
61f8af9c48Sopenharmony_ci    constexpr size_t INT32_SHORT_ID_LENGTH = 10;
62f8af9c48Sopenharmony_ci    constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
63f8af9c48Sopenharmony_ci    constexpr size_t INT32_MIN_ID_LENGTH = 3;
64f8af9c48Sopenharmony_ci    std::string res;
65f8af9c48Sopenharmony_ci    std::string tmpStr("***");
66f8af9c48Sopenharmony_ci    size_t strLen = value.length();
67f8af9c48Sopenharmony_ci    if (strLen < INT32_MIN_ID_LENGTH) {
68f8af9c48Sopenharmony_ci        return tmpStr;
69f8af9c48Sopenharmony_ci    }
70f8af9c48Sopenharmony_ci
71f8af9c48Sopenharmony_ci    if (strLen <= INT32_SHORT_ID_LENGTH) {
72f8af9c48Sopenharmony_ci        res += value[0];
73f8af9c48Sopenharmony_ci        res += tmpStr;
74f8af9c48Sopenharmony_ci        res += value[strLen - 1];
75f8af9c48Sopenharmony_ci    } else {
76f8af9c48Sopenharmony_ci        res.append(value, 0, INT32_PLAINTEXT_LENGTH);
77f8af9c48Sopenharmony_ci        res += tmpStr;
78f8af9c48Sopenharmony_ci        res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
79f8af9c48Sopenharmony_ci    }
80f8af9c48Sopenharmony_ci    return res;
81f8af9c48Sopenharmony_ci}
82f8af9c48Sopenharmony_ci
83f8af9c48Sopenharmony_cistd::vector<std::string> ProfileUtils::GetOnlineDevices()
84f8af9c48Sopenharmony_ci{
85f8af9c48Sopenharmony_ci    std::vector<std::string> targetDevices;
86f8af9c48Sopenharmony_ci    std::vector<DmDeviceInfo> allOnlineDeviceInfos;
87f8af9c48Sopenharmony_ci    int32_t result = DeviceManager::GetInstance().GetTrustedDeviceList(DP_PKG_NAME, "", allOnlineDeviceInfos);
88f8af9c48Sopenharmony_ci    if (result != DP_SUCCESS || allOnlineDeviceInfos.empty()) {
89f8af9c48Sopenharmony_ci        HILOGE("GetTrustedDeviceList Failed!");
90f8af9c48Sopenharmony_ci        return {};
91f8af9c48Sopenharmony_ci    }
92f8af9c48Sopenharmony_ci    for (const DmDeviceInfo& dmDeviceInfo : allOnlineDeviceInfos) {
93f8af9c48Sopenharmony_ci        targetDevices.push_back(dmDeviceInfo.networkId);
94f8af9c48Sopenharmony_ci    }
95f8af9c48Sopenharmony_ci    return targetDevices;
96f8af9c48Sopenharmony_ci}
97f8af9c48Sopenharmony_ci
98f8af9c48Sopenharmony_cistd::string ProfileUtils::GetLocalUdidFromDM()
99f8af9c48Sopenharmony_ci{
100f8af9c48Sopenharmony_ci    std::string udid = "";
101f8af9c48Sopenharmony_ci    if (DeviceManager::GetInstance().GetLocalDeviceId(DP_PKG_NAME, udid) != DP_SUCCESS) {
102f8af9c48Sopenharmony_ci        HILOGE("Get local udid fail from DM!");
103f8af9c48Sopenharmony_ci        return "";
104f8af9c48Sopenharmony_ci    }
105f8af9c48Sopenharmony_ci    return udid;
106f8af9c48Sopenharmony_ci}
107f8af9c48Sopenharmony_ci
108f8af9c48Sopenharmony_cibool ProfileUtils::FilterAndGroupOnlineDevices(const std::vector<std::string>& deviceList,
109f8af9c48Sopenharmony_ci    std::vector<std::string>& ohBasedDevices, std::vector<std::string>& notOHBasedDevices)
110f8af9c48Sopenharmony_ci{
111f8af9c48Sopenharmony_ci    if (deviceList.size() == 0 || deviceList.size() > MAX_DEVICE_SIZE) {
112f8af9c48Sopenharmony_ci        HILOGE("This deviceList size is invalid, size: %{public}zu!", deviceList.size());
113f8af9c48Sopenharmony_ci        return false;
114f8af9c48Sopenharmony_ci    }
115f8af9c48Sopenharmony_ci    std::vector<DmDeviceInfo> allOnlineDeviceInfos;
116f8af9c48Sopenharmony_ci    int32_t result = DeviceManager::GetInstance().GetTrustedDeviceList(DP_PKG_NAME, "", allOnlineDeviceInfos);
117f8af9c48Sopenharmony_ci    if (result != DP_SUCCESS || allOnlineDeviceInfos.empty()) {
118f8af9c48Sopenharmony_ci        HILOGE("GetTrustedDeviceList Failed!");
119f8af9c48Sopenharmony_ci        return false;
120f8af9c48Sopenharmony_ci    }
121f8af9c48Sopenharmony_ci    for (const DmDeviceInfo& dmDeviceInfo : allOnlineDeviceInfos) {
122f8af9c48Sopenharmony_ci        if (std::find(deviceList.begin(), deviceList.end(), dmDeviceInfo.networkId) == deviceList.end()) {
123f8af9c48Sopenharmony_ci            continue;
124f8af9c48Sopenharmony_ci        }
125f8af9c48Sopenharmony_ci        if (dmDeviceInfo.extraData.empty()) {
126f8af9c48Sopenharmony_ci            HILOGW("extraData is empty! networkId:%{public}s", GetAnonyString(dmDeviceInfo.networkId).c_str());
127f8af9c48Sopenharmony_ci            continue;
128f8af9c48Sopenharmony_ci        }
129f8af9c48Sopenharmony_ci        if (IsOHBasedDevice(dmDeviceInfo.extraData)) {
130f8af9c48Sopenharmony_ci            ohBasedDevices.push_back(dmDeviceInfo.networkId);
131f8af9c48Sopenharmony_ci        } else {
132f8af9c48Sopenharmony_ci            notOHBasedDevices.push_back(dmDeviceInfo.networkId);
133f8af9c48Sopenharmony_ci        }
134f8af9c48Sopenharmony_ci    }
135f8af9c48Sopenharmony_ci    return true;
136f8af9c48Sopenharmony_ci}
137f8af9c48Sopenharmony_ci
138f8af9c48Sopenharmony_cibool ProfileUtils::IsOHBasedDevice(const std::string& extraData)
139f8af9c48Sopenharmony_ci{
140f8af9c48Sopenharmony_ci    if (extraData.empty()) {
141f8af9c48Sopenharmony_ci        HILOGE("extraData is empty!");
142f8af9c48Sopenharmony_ci        return false;
143f8af9c48Sopenharmony_ci    }
144f8af9c48Sopenharmony_ci    cJSON* extraDataJson = cJSON_Parse(extraData.c_str());
145f8af9c48Sopenharmony_ci    if (extraDataJson == NULL) {
146f8af9c48Sopenharmony_ci        HILOGE("extraData parse failed");
147f8af9c48Sopenharmony_ci        return false;
148f8af9c48Sopenharmony_ci    }
149f8af9c48Sopenharmony_ci    int32_t osType = OHOS_TYPE_UNKNOWN;
150f8af9c48Sopenharmony_ci    cJSON* osTypeJson = cJSON_GetObjectItem(extraDataJson, DistributedHardware::PARAM_KEY_OS_TYPE);
151f8af9c48Sopenharmony_ci    if (cJSON_IsNumber(osTypeJson)) {
152f8af9c48Sopenharmony_ci        osType = static_cast<int32_t>(osTypeJson->valueint);
153f8af9c48Sopenharmony_ci    }
154f8af9c48Sopenharmony_ci    cJSON_Delete(extraDataJson);
155f8af9c48Sopenharmony_ci    return osType == OHOS_TYPE;
156f8af9c48Sopenharmony_ci}
157f8af9c48Sopenharmony_ci
158f8af9c48Sopenharmony_cibool ProfileUtils::IsP2p(const int32_t authForm)
159f8af9c48Sopenharmony_ci{
160f8af9c48Sopenharmony_ci    if (authForm == static_cast<int32_t>(DistributedHardware::DmAuthForm::INVALID_TYPE) ||
161f8af9c48Sopenharmony_ci        authForm == static_cast<int32_t>(DistributedHardware::DmAuthForm::IDENTICAL_ACCOUNT)) {
162f8af9c48Sopenharmony_ci        HILOGD("authForm: %{public}d!", authForm);
163f8af9c48Sopenharmony_ci        return false;
164f8af9c48Sopenharmony_ci    }
165f8af9c48Sopenharmony_ci    return true;
166f8af9c48Sopenharmony_ci}
167f8af9c48Sopenharmony_ci
168f8af9c48Sopenharmony_ciProfileType ProfileUtils::GetProfileType(const std::string& key)
169f8af9c48Sopenharmony_ci{
170f8af9c48Sopenharmony_ci    if (key.length() == 0 || key.length() > MAX_STRING_LEN) {
171f8af9c48Sopenharmony_ci        HILOGE("This key is invalid, value: %{public}s!", GetAnonyString(key).c_str());
172f8af9c48Sopenharmony_ci        return ProfileType::PROFILE_TYPE_MIN;
173f8af9c48Sopenharmony_ci    }
174f8af9c48Sopenharmony_ci    ProfileType profileType = ProfileType::PROFILE_TYPE_MIN;
175f8af9c48Sopenharmony_ci    if (StartsWith(key, DEV_PREFIX)) {
176f8af9c48Sopenharmony_ci        profileType = ProfileType::DEVICE_PROFILE;
177f8af9c48Sopenharmony_ci    }
178f8af9c48Sopenharmony_ci    if (StartsWith(key, SVR_PREFIX)) {
179f8af9c48Sopenharmony_ci        profileType = ProfileType::SERVICE_PROFILE;
180f8af9c48Sopenharmony_ci    }
181f8af9c48Sopenharmony_ci    if (StartsWith(key, CHAR_PREFIX)) {
182f8af9c48Sopenharmony_ci        profileType = ProfileType::CHAR_PROFILE;
183f8af9c48Sopenharmony_ci    }
184f8af9c48Sopenharmony_ci    return profileType;
185f8af9c48Sopenharmony_ci}
186f8af9c48Sopenharmony_ci
187f8af9c48Sopenharmony_cibool ProfileUtils::StartsWith(const std::string& str, const std::string& prefix)
188f8af9c48Sopenharmony_ci{
189f8af9c48Sopenharmony_ci    return (str.find(prefix, 0) == 0);
190f8af9c48Sopenharmony_ci}
191f8af9c48Sopenharmony_ci
192f8af9c48Sopenharmony_cibool ProfileUtils::EndsWith(const std::string& str, const std::string& suffix)
193f8af9c48Sopenharmony_ci{
194f8af9c48Sopenharmony_ci    if (str.length() < suffix.length()) {
195f8af9c48Sopenharmony_ci        return false;
196f8af9c48Sopenharmony_ci    }
197f8af9c48Sopenharmony_ci    return str.substr(str.length() - suffix.length()).compare(suffix) == 0;
198f8af9c48Sopenharmony_ci}
199f8af9c48Sopenharmony_ci
200f8af9c48Sopenharmony_cistd::string ProfileUtils::GetProfileKey(const std::string& dbKey)
201f8af9c48Sopenharmony_ci{
202f8af9c48Sopenharmony_ci    if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
203f8af9c48Sopenharmony_ci        return "";
204f8af9c48Sopenharmony_ci    }
205f8af9c48Sopenharmony_ci    std::size_t pos = dbKey.find_last_of("#");
206f8af9c48Sopenharmony_ci    return dbKey.substr(0, pos);
207f8af9c48Sopenharmony_ci}
208f8af9c48Sopenharmony_ci
209f8af9c48Sopenharmony_cistd::string ProfileUtils::GetDeviceIdByDBKey(const std::string& dbKey)
210f8af9c48Sopenharmony_ci{
211f8af9c48Sopenharmony_ci    if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
212f8af9c48Sopenharmony_ci        return "";
213f8af9c48Sopenharmony_ci    }
214f8af9c48Sopenharmony_ci    std::vector<std::string> res;
215f8af9c48Sopenharmony_ci    if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
216f8af9c48Sopenharmony_ci        return "";
217f8af9c48Sopenharmony_ci    }
218f8af9c48Sopenharmony_ci    if (res.size() < NUM_1) {
219f8af9c48Sopenharmony_ci        return "";
220f8af9c48Sopenharmony_ci    }
221f8af9c48Sopenharmony_ci    return res[NUM_1];
222f8af9c48Sopenharmony_ci}
223f8af9c48Sopenharmony_ci
224f8af9c48Sopenharmony_cistd::string ProfileUtils::GetServiceNameByDBKey(const std::string& dbKey)
225f8af9c48Sopenharmony_ci{
226f8af9c48Sopenharmony_ci    if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
227f8af9c48Sopenharmony_ci        return "";
228f8af9c48Sopenharmony_ci    }
229f8af9c48Sopenharmony_ci    std::vector<std::string> res;
230f8af9c48Sopenharmony_ci    if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
231f8af9c48Sopenharmony_ci        return "";
232f8af9c48Sopenharmony_ci    }
233f8af9c48Sopenharmony_ci    if (res.size() < NUM_2) {
234f8af9c48Sopenharmony_ci        return "";
235f8af9c48Sopenharmony_ci    }
236f8af9c48Sopenharmony_ci    return res[NUM_2];
237f8af9c48Sopenharmony_ci}
238f8af9c48Sopenharmony_ci
239f8af9c48Sopenharmony_cistd::string ProfileUtils::GetNonOhSuffixServiceNameByDBKey(const std::string& dbKey)
240f8af9c48Sopenharmony_ci{
241f8af9c48Sopenharmony_ci    std::string serviceName = GetServiceNameByDBKey(dbKey);
242f8af9c48Sopenharmony_ci    if (serviceName.empty()) {
243f8af9c48Sopenharmony_ci        return "";
244f8af9c48Sopenharmony_ci    }
245f8af9c48Sopenharmony_ci    return CheckAndRemoveOhSuffix(serviceName);
246f8af9c48Sopenharmony_ci}
247f8af9c48Sopenharmony_ci
248f8af9c48Sopenharmony_cibool ProfileUtils::IsNeedAddOhSuffix(const std::string& profileName, bool isSvr)
249f8af9c48Sopenharmony_ci{
250f8af9c48Sopenharmony_ci    if (profileName.length() == 0 || profileName.length() > MAX_STRING_LEN) {
251f8af9c48Sopenharmony_ci        return false;
252f8af9c48Sopenharmony_ci    }
253f8af9c48Sopenharmony_ci    if (isSvr && NEED_ADD_OH_SUFFIX_SVR_NAMES.count(profileName) > 0) {
254f8af9c48Sopenharmony_ci        return true;
255f8af9c48Sopenharmony_ci    }
256f8af9c48Sopenharmony_ci    if (!isSvr && NEED_ADD_OH_SUFFIX_DEV_PROFILES.count(profileName) > 0) {
257f8af9c48Sopenharmony_ci        return true;
258f8af9c48Sopenharmony_ci    }
259f8af9c48Sopenharmony_ci    return false;
260f8af9c48Sopenharmony_ci}
261f8af9c48Sopenharmony_ci
262f8af9c48Sopenharmony_cistd::string ProfileUtils::CheckAndAddOhSuffix(const std::string& profileName, bool isSvr)
263f8af9c48Sopenharmony_ci{
264f8af9c48Sopenharmony_ci    std::string str = profileName;
265f8af9c48Sopenharmony_ci    if (IsNeedAddOhSuffix(str, isSvr)) {
266f8af9c48Sopenharmony_ci        str = str + OH_PROFILE_SUFFIX;
267f8af9c48Sopenharmony_ci    }
268f8af9c48Sopenharmony_ci    return str;
269f8af9c48Sopenharmony_ci}
270f8af9c48Sopenharmony_ci
271f8af9c48Sopenharmony_cistd::string ProfileUtils::CheckAndRemoveOhSuffix(const std::string& profileName)
272f8af9c48Sopenharmony_ci{
273f8af9c48Sopenharmony_ci    std::string str = profileName;
274f8af9c48Sopenharmony_ci    if (EndsWith(str, OH_PROFILE_SUFFIX)) {
275f8af9c48Sopenharmony_ci        str = str.erase(str.length() - OH_PROFILE_SUFFIX.length());
276f8af9c48Sopenharmony_ci    }
277f8af9c48Sopenharmony_ci    return str;
278f8af9c48Sopenharmony_ci}
279f8af9c48Sopenharmony_ci
280f8af9c48Sopenharmony_cistd::string ProfileUtils::GetCharKeyByDBKey(const std::string& dbKey)
281f8af9c48Sopenharmony_ci{
282f8af9c48Sopenharmony_ci    if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
283f8af9c48Sopenharmony_ci        return "";
284f8af9c48Sopenharmony_ci    }
285f8af9c48Sopenharmony_ci    std::vector<std::string> res;
286f8af9c48Sopenharmony_ci    if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
287f8af9c48Sopenharmony_ci        return "";
288f8af9c48Sopenharmony_ci    }
289f8af9c48Sopenharmony_ci    if (res.size() < NUM_3) {
290f8af9c48Sopenharmony_ci        return "";
291f8af9c48Sopenharmony_ci    }
292f8af9c48Sopenharmony_ci    return res[NUM_3];
293f8af9c48Sopenharmony_ci}
294f8af9c48Sopenharmony_ci
295f8af9c48Sopenharmony_ciint32_t ProfileUtils::SplitString(const std::string& str, const std::string& splits, std::vector<std::string>& res)
296f8af9c48Sopenharmony_ci{
297f8af9c48Sopenharmony_ci    if (str == "") {
298f8af9c48Sopenharmony_ci        return DP_INVALID_PARAMS;
299f8af9c48Sopenharmony_ci    }
300f8af9c48Sopenharmony_ci    std::string strs = str + splits;
301f8af9c48Sopenharmony_ci    size_t pos = strs.find(splits);
302f8af9c48Sopenharmony_ci    int32_t step = splits.size();
303f8af9c48Sopenharmony_ci
304f8af9c48Sopenharmony_ci    while (pos != strs.npos) {
305f8af9c48Sopenharmony_ci        std::string temp = strs.substr(0, pos);
306f8af9c48Sopenharmony_ci        res.push_back(temp);
307f8af9c48Sopenharmony_ci        strs = strs.substr(pos + step, strs.size());
308f8af9c48Sopenharmony_ci        pos = strs.find(splits);
309f8af9c48Sopenharmony_ci    }
310f8af9c48Sopenharmony_ci    return DP_SUCCESS;
311f8af9c48Sopenharmony_ci}
312f8af9c48Sopenharmony_ci
313f8af9c48Sopenharmony_cistd::string ProfileUtils::JoinString(const std::vector<std::string>& strs, const std::string& delimiter)
314f8af9c48Sopenharmony_ci{
315f8af9c48Sopenharmony_ci    std::string res = EMPTY_STRING;
316f8af9c48Sopenharmony_ci    if (strs.empty()) {
317f8af9c48Sopenharmony_ci        return res;
318f8af9c48Sopenharmony_ci    }
319f8af9c48Sopenharmony_ci    for (size_t i = 0; i < strs.size(); i++) {
320f8af9c48Sopenharmony_ci        res += strs[i];
321f8af9c48Sopenharmony_ci        if (i != strs.size() - 1) {
322f8af9c48Sopenharmony_ci            res += delimiter;
323f8af9c48Sopenharmony_ci        }
324f8af9c48Sopenharmony_ci    }
325f8af9c48Sopenharmony_ci    return res;
326f8af9c48Sopenharmony_ci}
327f8af9c48Sopenharmony_ci
328f8af9c48Sopenharmony_cibool ProfileUtils::IsKeyValid(const std::string& key)
329f8af9c48Sopenharmony_ci{
330f8af9c48Sopenharmony_ci    if (key.length() == 0 || key.length() > MAX_STRING_LEN) {
331f8af9c48Sopenharmony_ci        return false;
332f8af9c48Sopenharmony_ci    }
333f8af9c48Sopenharmony_ci    size_t found = key.find(SEPARATOR);
334f8af9c48Sopenharmony_ci    if (found != std::string::npos) {
335f8af9c48Sopenharmony_ci        return false;
336f8af9c48Sopenharmony_ci    }
337f8af9c48Sopenharmony_ci    return true;
338f8af9c48Sopenharmony_ci}
339f8af9c48Sopenharmony_ci
340f8af9c48Sopenharmony_cibool ProfileUtils::IsLocalUdid(const std::string& udid)
341f8af9c48Sopenharmony_ci{
342f8af9c48Sopenharmony_ci    if (udid.length() == 0 || udid.length() > MAX_STRING_LEN) {
343f8af9c48Sopenharmony_ci        return false;
344f8af9c48Sopenharmony_ci    }
345f8af9c48Sopenharmony_ci    std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid();
346f8af9c48Sopenharmony_ci    return localUdid == udid;
347f8af9c48Sopenharmony_ci}
348f8af9c48Sopenharmony_ci
349f8af9c48Sopenharmony_cibool ProfileUtils::IsDevProfileValid(const DeviceProfile& devProfile)
350f8af9c48Sopenharmony_ci{
351f8af9c48Sopenharmony_ci    return IsKeyValid(devProfile.GetDeviceId()) && IsLocalUdid(devProfile.GetDeviceId());
352f8af9c48Sopenharmony_ci}
353f8af9c48Sopenharmony_ci
354f8af9c48Sopenharmony_cibool ProfileUtils::IsSvrProfileValid(const ServiceProfile& svrProfile)
355f8af9c48Sopenharmony_ci{
356f8af9c48Sopenharmony_ci    return IsKeyValid(svrProfile.GetDeviceId()) && IsLocalUdid(svrProfile.GetDeviceId()) &&
357f8af9c48Sopenharmony_ci        IsKeyValid(svrProfile.GetServiceName());
358f8af9c48Sopenharmony_ci}
359f8af9c48Sopenharmony_ci
360f8af9c48Sopenharmony_cibool ProfileUtils::IsCharProfileValid(const CharacteristicProfile& charProfile)
361f8af9c48Sopenharmony_ci{
362f8af9c48Sopenharmony_ci    return IsKeyValid(charProfile.GetDeviceId()) && IsLocalUdid(charProfile.GetDeviceId()) &&
363f8af9c48Sopenharmony_ci        IsKeyValid(charProfile.GetServiceName()) && IsKeyValid(charProfile.GetCharacteristicKey());
364f8af9c48Sopenharmony_ci}
365f8af9c48Sopenharmony_ci
366f8af9c48Sopenharmony_cibool ProfileUtils::IsDeviceProfileValid(const DeviceProfile& devProfile)
367f8af9c48Sopenharmony_ci{
368f8af9c48Sopenharmony_ci    if (devProfile.GetOsVersion().empty() || devProfile.GetOsType() == MIN_OS_TYPE) {
369f8af9c48Sopenharmony_ci        return false;
370f8af9c48Sopenharmony_ci    }
371f8af9c48Sopenharmony_ci    return true;
372f8af9c48Sopenharmony_ci}
373f8af9c48Sopenharmony_ci
374f8af9c48Sopenharmony_cibool ProfileUtils::IsServiceProfileValid(const ServiceProfile& svrProfile)
375f8af9c48Sopenharmony_ci{
376f8af9c48Sopenharmony_ci    if (svrProfile.GetServiceName().empty() || svrProfile.GetServiceType().empty()) {
377f8af9c48Sopenharmony_ci        return false;
378f8af9c48Sopenharmony_ci    }
379f8af9c48Sopenharmony_ci    return true;
380f8af9c48Sopenharmony_ci}
381f8af9c48Sopenharmony_ci
382f8af9c48Sopenharmony_cibool ProfileUtils::IsCharacteristicProfileValid(const CharacteristicProfile& charProfile)
383f8af9c48Sopenharmony_ci{
384f8af9c48Sopenharmony_ci    if (charProfile.GetCharacteristicKey().empty() || charProfile.GetCharacteristicValue().empty()) {
385f8af9c48Sopenharmony_ci        return false;
386f8af9c48Sopenharmony_ci    }
387f8af9c48Sopenharmony_ci    return true;
388f8af9c48Sopenharmony_ci}
389f8af9c48Sopenharmony_ci
390f8af9c48Sopenharmony_cistd::string ProfileUtils::GenerateDeviceProfileKey(const std::string& deviceId)
391f8af9c48Sopenharmony_ci{
392f8af9c48Sopenharmony_ci    return DEV_PREFIX + SEPARATOR + deviceId;
393f8af9c48Sopenharmony_ci}
394f8af9c48Sopenharmony_ci
395f8af9c48Sopenharmony_cistd::string ProfileUtils::GenerateServiceProfileKey(const std::string& deviceId, const std::string& serviceName)
396f8af9c48Sopenharmony_ci{
397f8af9c48Sopenharmony_ci    return SVR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName;
398f8af9c48Sopenharmony_ci}
399f8af9c48Sopenharmony_ci
400f8af9c48Sopenharmony_cistd::string ProfileUtils::GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName,
401f8af9c48Sopenharmony_ci    const std::string& charKey)
402f8af9c48Sopenharmony_ci{
403f8af9c48Sopenharmony_ci    return CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + charKey;
404f8af9c48Sopenharmony_ci}
405f8af9c48Sopenharmony_ci
406f8af9c48Sopenharmony_ciint32_t ProfileUtils::TrustDeviceProfileToEntries(const TrustDeviceProfile& profile, ValuesBucket& values)
407f8af9c48Sopenharmony_ci{
408f8af9c48Sopenharmony_ci    values.PutString(DEVICE_ID, profile.GetDeviceId());
409f8af9c48Sopenharmony_ci    values.PutInt(DEVICE_ID_TYPE, profile.GetDeviceIdType());
410f8af9c48Sopenharmony_ci    values.PutString(DEVICE_ID_HASH, profile.GetDeviceIdHash());
411f8af9c48Sopenharmony_ci    values.PutInt(STATUS, profile.GetStatus());
412f8af9c48Sopenharmony_ci    return DP_SUCCESS;
413f8af9c48Sopenharmony_ci}
414f8af9c48Sopenharmony_ci
415f8af9c48Sopenharmony_ciint32_t ProfileUtils::AccessControlProfileToEntries(const AccessControlProfile& profile, ValuesBucket& values)
416f8af9c48Sopenharmony_ci{
417f8af9c48Sopenharmony_ci    values.PutLong(ACCESS_CONTROL_ID, profile.GetAccessControlId());
418f8af9c48Sopenharmony_ci    values.PutLong(ACCESSER_ID, profile.GetAccesserId());
419f8af9c48Sopenharmony_ci    values.PutLong(ACCESSEE_ID, profile.GetAccesseeId());
420f8af9c48Sopenharmony_ci    values.PutString(TRUST_DEVICE_ID, profile.GetTrustDeviceId());
421f8af9c48Sopenharmony_ci    values.PutInt(AUTHENTICATION_TYPE, profile.GetAuthenticationType());
422f8af9c48Sopenharmony_ci    values.PutInt(DEVICE_ID_TYPE, profile.GetDeviceIdType());
423f8af9c48Sopenharmony_ci    values.PutString(DEVICE_ID_HASH, profile.GetDeviceIdHash());
424f8af9c48Sopenharmony_ci    values.PutInt(BIND_TYPE, profile.GetBindType());
425f8af9c48Sopenharmony_ci    values.PutString(SESSION_KEY, profile.GetSessionKey());
426f8af9c48Sopenharmony_ci    values.PutInt(LAST_AUTH_TIME, profile.GetLastAuthTime());
427f8af9c48Sopenharmony_ci    values.PutInt(VALID_PERIOD, profile.GetValidPeriod());
428f8af9c48Sopenharmony_ci    values.PutInt(STATUS, profile.GetStatus());
429f8af9c48Sopenharmony_ci    values.PutInt(BIND_LEVEL, profile.GetBindLevel());
430f8af9c48Sopenharmony_ci    return DP_SUCCESS;
431f8af9c48Sopenharmony_ci}
432f8af9c48Sopenharmony_ci
433f8af9c48Sopenharmony_ciint32_t ProfileUtils::AccesserToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values)
434f8af9c48Sopenharmony_ci{
435f8af9c48Sopenharmony_ci    Accesser accesser = aclProfile.GetAccesser();
436f8af9c48Sopenharmony_ci    values.PutLong(ACCESSER_ID, accesser.GetAccesserId());
437f8af9c48Sopenharmony_ci    values.PutString(ACCESSER_DEVICE_ID, accesser.GetAccesserDeviceId());
438f8af9c48Sopenharmony_ci    values.PutInt(ACCESSER_USER_ID, accesser.GetAccesserUserId());
439f8af9c48Sopenharmony_ci    values.PutString(ACCESSER_ACCOUNT_ID, accesser.GetAccesserAccountId());
440f8af9c48Sopenharmony_ci    values.PutLong(ACCESSER_TOKEN_ID, accesser.GetAccesserTokenId());
441f8af9c48Sopenharmony_ci    values.PutString(ACCESSER_BUNDLE_NAME, accesser.GetAccesserBundleName());
442f8af9c48Sopenharmony_ci    values.PutString(ACCESSER_HAP_SIGNATURE, accesser.GetAccesserHapSignature());
443f8af9c48Sopenharmony_ci    values.PutInt(ACCESSER_BIND_LEVEL, accesser.GetAccesserBindLevel());
444f8af9c48Sopenharmony_ci    return DP_SUCCESS;
445f8af9c48Sopenharmony_ci}
446f8af9c48Sopenharmony_ci
447f8af9c48Sopenharmony_ciint32_t ProfileUtils::AccesseeToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values)
448f8af9c48Sopenharmony_ci{
449f8af9c48Sopenharmony_ci    Accessee accessee = aclProfile.GetAccessee();
450f8af9c48Sopenharmony_ci    values.PutLong(ACCESSEE_ID, accessee.GetAccesseeId());
451f8af9c48Sopenharmony_ci    values.PutString(ACCESSEE_DEVICE_ID, accessee.GetAccesseeDeviceId());
452f8af9c48Sopenharmony_ci    values.PutInt(ACCESSEE_USER_ID, accessee.GetAccesseeUserId());
453f8af9c48Sopenharmony_ci    values.PutString(ACCESSEE_ACCOUNT_ID, accessee.GetAccesseeAccountId());
454f8af9c48Sopenharmony_ci    values.PutLong(ACCESSEE_TOKEN_ID, accessee.GetAccesseeTokenId());
455f8af9c48Sopenharmony_ci    values.PutString(ACCESSEE_BUNDLE_NAME, accessee.GetAccesseeBundleName());
456f8af9c48Sopenharmony_ci    values.PutString(ACCESSEE_HAP_SIGNATURE, accessee.GetAccesseeHapSignature());
457f8af9c48Sopenharmony_ci    values.PutInt(ACCESSEE_BIND_LEVEL, accessee.GetAccesseeBindLevel());
458f8af9c48Sopenharmony_ci    return DP_SUCCESS;
459f8af9c48Sopenharmony_ci}
460f8af9c48Sopenharmony_ci
461f8af9c48Sopenharmony_ciint32_t ProfileUtils::DeviceProfileToEntries(const DeviceProfile& profile, std::map<std::string, std::string>& values)
462f8af9c48Sopenharmony_ci{
463f8af9c48Sopenharmony_ci    std::string deviceProfileKey = GenerateDeviceProfileKey(profile.GetDeviceId());
464f8af9c48Sopenharmony_ci    values[GenerateDBKey(deviceProfileKey, OS_SYS_CAPACITY)] = profile.GetOsSysCap();
465f8af9c48Sopenharmony_ci    values[GenerateDBKey(deviceProfileKey, OS_VERSION)] = profile.GetOsVersion();
466f8af9c48Sopenharmony_ci    values[GenerateDBKey(deviceProfileKey, OS_TYPE)] = std::to_string(profile.GetOsType());
467f8af9c48Sopenharmony_ci    values[GenerateDBKey(deviceProfileKey, OS_VERSION + OH_PROFILE_SUFFIX)] = profile.GetOsVersion();
468f8af9c48Sopenharmony_ci    values[GenerateDBKey(deviceProfileKey, OS_TYPE + OH_PROFILE_SUFFIX)] = std::to_string(profile.GetOsType());
469f8af9c48Sopenharmony_ci    return DP_SUCCESS;
470f8af9c48Sopenharmony_ci}
471f8af9c48Sopenharmony_ci
472f8af9c48Sopenharmony_ciint32_t ProfileUtils::ServiceProfileToEntries(const ServiceProfile& profile, std::map<std::string, std::string>& values)
473f8af9c48Sopenharmony_ci{
474f8af9c48Sopenharmony_ci    std::string serviceName = CheckAndAddOhSuffix(profile.GetServiceName(), true);
475f8af9c48Sopenharmony_ci    std::string serviceProfileKey = GenerateServiceProfileKey(profile.GetDeviceId(), serviceName);
476f8af9c48Sopenharmony_ci    // value not need add OH suffix
477f8af9c48Sopenharmony_ci    values[GenerateDBKey(serviceProfileKey, SERVICE_NAME)] = profile.GetServiceName();
478f8af9c48Sopenharmony_ci    values[GenerateDBKey(serviceProfileKey, SERVICE_TYPE)] = profile.GetServiceType();
479f8af9c48Sopenharmony_ci    return DP_SUCCESS;
480f8af9c48Sopenharmony_ci}
481f8af9c48Sopenharmony_ci
482f8af9c48Sopenharmony_ciint32_t ProfileUtils::CharacteristicProfileToEntries(const CharacteristicProfile& profile,
483f8af9c48Sopenharmony_ci                                                     std::map<std::string, std::string>& values)
484f8af9c48Sopenharmony_ci{
485f8af9c48Sopenharmony_ci    std::string serviceName = CheckAndAddOhSuffix(profile.GetServiceName(), true);
486f8af9c48Sopenharmony_ci    std::string charProfileKey = GenerateCharProfileKey(profile.GetDeviceId(), serviceName,
487f8af9c48Sopenharmony_ci        profile.GetCharacteristicKey());
488f8af9c48Sopenharmony_ci    values[GenerateDBKey(charProfileKey, CHARACTERISTIC_KEY)] = profile.GetCharacteristicKey();
489f8af9c48Sopenharmony_ci    values[GenerateDBKey(charProfileKey, CHARACTERISTIC_VALUE)] = profile.GetCharacteristicValue();
490f8af9c48Sopenharmony_ci    return DP_SUCCESS;
491f8af9c48Sopenharmony_ci}
492f8af9c48Sopenharmony_ci
493f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToTrustDeviceProfile(const ValuesBucket& values, TrustDeviceProfile& profile)
494f8af9c48Sopenharmony_ci{
495f8af9c48Sopenharmony_ci    ValueObject valueObject;
496f8af9c48Sopenharmony_ci    std::string strValue = "";
497f8af9c48Sopenharmony_ci    int32_t intValue = 0;
498f8af9c48Sopenharmony_ci    if (values.GetObject(DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
499f8af9c48Sopenharmony_ci        profile.SetDeviceId(strValue);
500f8af9c48Sopenharmony_ci    }
501f8af9c48Sopenharmony_ci    if (values.GetObject(DEVICE_ID_HASH, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
502f8af9c48Sopenharmony_ci        profile.SetDeviceIdHash(strValue);
503f8af9c48Sopenharmony_ci    }
504f8af9c48Sopenharmony_ci    if (values.GetObject(DEVICE_TYPE_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
505f8af9c48Sopenharmony_ci        profile.SetDeviceIdType(intValue);
506f8af9c48Sopenharmony_ci    }
507f8af9c48Sopenharmony_ci    if (values.GetObject(STATUS, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
508f8af9c48Sopenharmony_ci        profile.SetStatus(intValue);
509f8af9c48Sopenharmony_ci    }
510f8af9c48Sopenharmony_ci    return DP_SUCCESS;
511f8af9c48Sopenharmony_ci}
512f8af9c48Sopenharmony_ci
513f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToAccessControlProfile(const ValuesBucket& values, AccessControlProfile& profile)
514f8af9c48Sopenharmony_ci{
515f8af9c48Sopenharmony_ci    std::string strValue = "";
516f8af9c48Sopenharmony_ci    int32_t intValue = 0;
517f8af9c48Sopenharmony_ci    int64_t int64Value = 0;
518f8af9c48Sopenharmony_ci    if (GetLongValue(values, ACCESS_CONTROL_ID, int64Value)) {
519f8af9c48Sopenharmony_ci        profile.SetAccessControlId(int64Value);
520f8af9c48Sopenharmony_ci    }
521f8af9c48Sopenharmony_ci    if (GetLongValue(values, ACCESSER_ID, int64Value)) {
522f8af9c48Sopenharmony_ci        profile.SetAccesserId(int64Value);
523f8af9c48Sopenharmony_ci    }
524f8af9c48Sopenharmony_ci    if (GetLongValue(values, ACCESSEE_ID, int64Value)) {
525f8af9c48Sopenharmony_ci        profile.SetAccesseeId(int64Value);
526f8af9c48Sopenharmony_ci    }
527f8af9c48Sopenharmony_ci    if (GetStringValue(values, SESSION_KEY, strValue)) {
528f8af9c48Sopenharmony_ci        profile.SetSessionKey(strValue);
529f8af9c48Sopenharmony_ci    }
530f8af9c48Sopenharmony_ci    if (GetIntValue(values, BIND_TYPE, intValue)) {
531f8af9c48Sopenharmony_ci        profile.SetBindType(intValue);
532f8af9c48Sopenharmony_ci    }
533f8af9c48Sopenharmony_ci    if (GetIntValue(values, AUTHENTICATION_TYPE, intValue)) {
534f8af9c48Sopenharmony_ci        profile.SetAuthenticationType(intValue);
535f8af9c48Sopenharmony_ci    }
536f8af9c48Sopenharmony_ci    if (GetIntValue(values, BIND_LEVEL, intValue)) {
537f8af9c48Sopenharmony_ci        profile.SetBindLevel(intValue);
538f8af9c48Sopenharmony_ci    }
539f8af9c48Sopenharmony_ci    if (GetIntValue(values, STATUS, intValue)) {
540f8af9c48Sopenharmony_ci        profile.SetStatus(intValue);
541f8af9c48Sopenharmony_ci    }
542f8af9c48Sopenharmony_ci    if (GetIntValue(values, VALID_PERIOD, intValue)) {
543f8af9c48Sopenharmony_ci        profile.SetValidPeriod(intValue);
544f8af9c48Sopenharmony_ci    }
545f8af9c48Sopenharmony_ci    if (GetIntValue(values, LAST_AUTH_TIME, intValue)) {
546f8af9c48Sopenharmony_ci        profile.SetLastAuthTime(intValue);
547f8af9c48Sopenharmony_ci    }
548f8af9c48Sopenharmony_ci    if (GetStringValue(values, TRUST_DEVICE_ID, strValue)) {
549f8af9c48Sopenharmony_ci        profile.SetTrustDeviceId(strValue);
550f8af9c48Sopenharmony_ci    }
551f8af9c48Sopenharmony_ci    if (GetIntValue(values, DEVICE_TYPE_ID, intValue)) {
552f8af9c48Sopenharmony_ci        profile.SetDeviceIdType(intValue);
553f8af9c48Sopenharmony_ci    }
554f8af9c48Sopenharmony_ci    if (GetStringValue(values, DEVICE_ID_HASH, strValue)) {
555f8af9c48Sopenharmony_ci        profile.SetDeviceIdHash(strValue);
556f8af9c48Sopenharmony_ci    }
557f8af9c48Sopenharmony_ci    return DP_SUCCESS;
558f8af9c48Sopenharmony_ci}
559f8af9c48Sopenharmony_ci
560f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToAccesser(const ValuesBucket& values, Accesser& accesser)
561f8af9c48Sopenharmony_ci{
562f8af9c48Sopenharmony_ci    ValueObject valueObject;
563f8af9c48Sopenharmony_ci    std::string strValue = "";
564f8af9c48Sopenharmony_ci    int32_t intValue = 0;
565f8af9c48Sopenharmony_ci    int64_t int64Value = 0;
566f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
567f8af9c48Sopenharmony_ci        accesser.SetAccesserId(int64Value);
568f8af9c48Sopenharmony_ci    }
569f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
570f8af9c48Sopenharmony_ci        accesser.SetAccesserDeviceId(strValue);
571f8af9c48Sopenharmony_ci    }
572f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_USER_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
573f8af9c48Sopenharmony_ci        accesser.SetAccesserUserId(intValue);
574f8af9c48Sopenharmony_ci    }
575f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_ACCOUNT_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
576f8af9c48Sopenharmony_ci        accesser.SetAccesserAccountId(strValue);
577f8af9c48Sopenharmony_ci    }
578f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_TOKEN_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
579f8af9c48Sopenharmony_ci        accesser.SetAccesserTokenId(int64Value);
580f8af9c48Sopenharmony_ci    }
581f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_BUNDLE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
582f8af9c48Sopenharmony_ci        accesser.SetAccesserBundleName(strValue);
583f8af9c48Sopenharmony_ci    }
584f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSER_BIND_LEVEL, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
585f8af9c48Sopenharmony_ci        accesser.SetAccesserBindLevel(intValue);
586f8af9c48Sopenharmony_ci    }
587f8af9c48Sopenharmony_ci    return DP_SUCCESS;
588f8af9c48Sopenharmony_ci}
589f8af9c48Sopenharmony_ci
590f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToAccessee(const ValuesBucket& values, Accessee& accessee)
591f8af9c48Sopenharmony_ci{
592f8af9c48Sopenharmony_ci    ValueObject valueObject;
593f8af9c48Sopenharmony_ci    std::string strValue = "";
594f8af9c48Sopenharmony_ci    int32_t intValue = 0;
595f8af9c48Sopenharmony_ci    int64_t int64Value = 0;
596f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
597f8af9c48Sopenharmony_ci        accessee.SetAccesseeId(int64Value);
598f8af9c48Sopenharmony_ci    }
599f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
600f8af9c48Sopenharmony_ci        accessee.SetAccesseeDeviceId(strValue);
601f8af9c48Sopenharmony_ci    }
602f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_USER_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
603f8af9c48Sopenharmony_ci        accessee.SetAccesseeUserId(intValue);
604f8af9c48Sopenharmony_ci    }
605f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_ACCOUNT_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
606f8af9c48Sopenharmony_ci        accessee.SetAccesseeAccountId(strValue);
607f8af9c48Sopenharmony_ci    }
608f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_TOKEN_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
609f8af9c48Sopenharmony_ci        accessee.SetAccesseeTokenId(int64Value);
610f8af9c48Sopenharmony_ci    }
611f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_BUNDLE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
612f8af9c48Sopenharmony_ci        accessee.SetAccesseeBundleName(strValue);
613f8af9c48Sopenharmony_ci    }
614f8af9c48Sopenharmony_ci    if (values.GetObject(ACCESSEE_BIND_LEVEL, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
615f8af9c48Sopenharmony_ci        accessee.SetAccesseeBindLevel(intValue);
616f8af9c48Sopenharmony_ci    }
617f8af9c48Sopenharmony_ci    return DP_SUCCESS;
618f8af9c48Sopenharmony_ci}
619f8af9c48Sopenharmony_ci
620f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToDeviceProfile(std::map<std::string, std::string> values, DeviceProfile& profile)
621f8af9c48Sopenharmony_ci{
622f8af9c48Sopenharmony_ci    if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
623f8af9c48Sopenharmony_ci        HILOGI("Entries size is invalid!size: %{public}zu!", values.size());
624f8af9c48Sopenharmony_ci        return DP_INVALID_PARAMS;
625f8af9c48Sopenharmony_ci    }
626f8af9c48Sopenharmony_ci    auto propertiesMap = GetProfilePropertiesMap(values);
627f8af9c48Sopenharmony_ci    if (IsPropertyValid(propertiesMap, OS_SYS_CAPACITY, MAX_STRING_LEN)) {
628f8af9c48Sopenharmony_ci        profile.SetOsSysCap(propertiesMap[OS_SYS_CAPACITY]);
629f8af9c48Sopenharmony_ci    }
630f8af9c48Sopenharmony_ci    if (IsPropertyValid(propertiesMap, OS_VERSION, MAX_STRING_LEN)) {
631f8af9c48Sopenharmony_ci        profile.SetOsVersion(propertiesMap[OS_VERSION]);
632f8af9c48Sopenharmony_ci    }
633f8af9c48Sopenharmony_ci    if (IsPropertyValid(propertiesMap, OS_VERSION + OH_PROFILE_SUFFIX, MAX_STRING_LEN)) {
634f8af9c48Sopenharmony_ci        profile.SetOsVersion(propertiesMap[OS_VERSION + OH_PROFILE_SUFFIX]);
635f8af9c48Sopenharmony_ci    }
636f8af9c48Sopenharmony_ci    if (IsPropertyValid(propertiesMap, OS_TYPE, MIN_OS_TYPE, MAX_OS_TYPE)) {
637f8af9c48Sopenharmony_ci        int32_t osType = std::atoi(propertiesMap[OS_TYPE].c_str());
638f8af9c48Sopenharmony_ci        profile.SetOsType(osType);
639f8af9c48Sopenharmony_ci    }
640f8af9c48Sopenharmony_ci    if (IsPropertyValid(propertiesMap, OS_TYPE + OH_PROFILE_SUFFIX, MIN_OS_TYPE, MAX_OS_TYPE)) {
641f8af9c48Sopenharmony_ci        int32_t osType = std::atoi(propertiesMap[OS_TYPE + OH_PROFILE_SUFFIX].c_str());
642f8af9c48Sopenharmony_ci        profile.SetOsType(osType);
643f8af9c48Sopenharmony_ci    }
644f8af9c48Sopenharmony_ci    return DP_SUCCESS;
645f8af9c48Sopenharmony_ci}
646f8af9c48Sopenharmony_ci
647f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToServiceProfile(std::map<std::string, std::string> values, ServiceProfile& profile)
648f8af9c48Sopenharmony_ci{
649f8af9c48Sopenharmony_ci    if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
650f8af9c48Sopenharmony_ci        HILOGI("Entries size is invalid!size: %{public}zu!", values.size());
651f8af9c48Sopenharmony_ci        return DP_INVALID_PARAMS;
652f8af9c48Sopenharmony_ci    }
653f8af9c48Sopenharmony_ci    auto iter = values.begin();
654f8af9c48Sopenharmony_ci    profile.SetDeviceId(GetDeviceIdByDBKey(iter->first));
655f8af9c48Sopenharmony_ci    auto propertiesMap = GetProfilePropertiesMap(values);
656f8af9c48Sopenharmony_ci    if (propertiesMap.count(SERVICE_NAME) != 0 && 0 < propertiesMap[SERVICE_NAME].length() &&
657f8af9c48Sopenharmony_ci        propertiesMap[SERVICE_NAME].length() < MAX_STRING_LEN) {
658f8af9c48Sopenharmony_ci        profile.SetServiceName(CheckAndRemoveOhSuffix(propertiesMap[SERVICE_NAME]));
659f8af9c48Sopenharmony_ci    }
660f8af9c48Sopenharmony_ci    if (propertiesMap.count(SERVICE_TYPE) != 0 && 0 < propertiesMap[SERVICE_TYPE].length() &&
661f8af9c48Sopenharmony_ci        propertiesMap[SERVICE_TYPE].length() < MAX_STRING_LEN) {
662f8af9c48Sopenharmony_ci        profile.SetServiceType(propertiesMap[SERVICE_TYPE]);
663f8af9c48Sopenharmony_ci    }
664f8af9c48Sopenharmony_ci    return DP_SUCCESS;
665f8af9c48Sopenharmony_ci}
666f8af9c48Sopenharmony_ci
667f8af9c48Sopenharmony_ciint32_t ProfileUtils::EntriesToCharProfile(std::map<std::string, std::string> values, CharacteristicProfile& profile)
668f8af9c48Sopenharmony_ci{
669f8af9c48Sopenharmony_ci    if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
670f8af9c48Sopenharmony_ci        HILOGI("Entries size is invalid!size : %{public}zu", values.size());
671f8af9c48Sopenharmony_ci        return DP_INVALID_PARAMS;
672f8af9c48Sopenharmony_ci    }
673f8af9c48Sopenharmony_ci    auto iter = values.begin();
674f8af9c48Sopenharmony_ci    profile.SetDeviceId(GetDeviceIdByDBKey(iter->first));
675f8af9c48Sopenharmony_ci    profile.SetServiceName(GetNonOhSuffixServiceNameByDBKey(iter->first));
676f8af9c48Sopenharmony_ci    auto propertiesMap = GetProfilePropertiesMap(values);
677f8af9c48Sopenharmony_ci    if (propertiesMap.count(CHARACTERISTIC_KEY) != 0 && 0 < propertiesMap[CHARACTERISTIC_KEY].length() &&
678f8af9c48Sopenharmony_ci        propertiesMap[CHARACTERISTIC_KEY].length() < MAX_STRING_LEN) {
679f8af9c48Sopenharmony_ci        profile.SetCharacteristicKey(propertiesMap[CHARACTERISTIC_KEY]);
680f8af9c48Sopenharmony_ci    }
681f8af9c48Sopenharmony_ci    if (propertiesMap.count(CHARACTERISTIC_VALUE) != 0 && 0 < propertiesMap[CHARACTERISTIC_VALUE].length() &&
682f8af9c48Sopenharmony_ci        propertiesMap[CHARACTERISTIC_VALUE].length() < MAX_STRING_LEN) {
683f8af9c48Sopenharmony_ci        profile.SetCharacteristicValue(propertiesMap[CHARACTERISTIC_VALUE]);
684f8af9c48Sopenharmony_ci    }
685f8af9c48Sopenharmony_ci    return DP_SUCCESS;
686f8af9c48Sopenharmony_ci}
687f8af9c48Sopenharmony_ci
688f8af9c48Sopenharmony_cistd::string ProfileUtils::GenerateDBKey(const std::string& profileKey, const std::string& profileProperty)
689f8af9c48Sopenharmony_ci{
690f8af9c48Sopenharmony_ci    return profileKey + SEPARATOR + profileProperty;
691f8af9c48Sopenharmony_ci}
692f8af9c48Sopenharmony_ci
693f8af9c48Sopenharmony_cistd::string ProfileUtils::GetProfileProperty(const std::string& dbKey)
694f8af9c48Sopenharmony_ci{
695f8af9c48Sopenharmony_ci    if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
696f8af9c48Sopenharmony_ci        return "";
697f8af9c48Sopenharmony_ci    }
698f8af9c48Sopenharmony_ci    std::size_t pos = dbKey.find_last_of("#");
699f8af9c48Sopenharmony_ci    if (pos == std::string::npos) {
700f8af9c48Sopenharmony_ci        return "";
701f8af9c48Sopenharmony_ci    }
702f8af9c48Sopenharmony_ci    return dbKey.substr(pos + 1);
703f8af9c48Sopenharmony_ci}
704f8af9c48Sopenharmony_ci
705f8af9c48Sopenharmony_cistd::map<std::string, std::string> ProfileUtils::GetProfilePropertiesMap(std::map<std::string, std::string> dbEntries)
706f8af9c48Sopenharmony_ci{
707f8af9c48Sopenharmony_ci    std::map<std::string, std::string> propertiesMap;
708f8af9c48Sopenharmony_ci    for (const auto& item : dbEntries) {
709f8af9c48Sopenharmony_ci        std::string profileProperty = GetProfileProperty(item.first);
710f8af9c48Sopenharmony_ci        if (profileProperty.empty()) {
711f8af9c48Sopenharmony_ci            HILOGE("GetProfileProperty fail, %{public}s!", GetDbKeyAnonyString(item.first).c_str());
712f8af9c48Sopenharmony_ci            continue;
713f8af9c48Sopenharmony_ci        }
714f8af9c48Sopenharmony_ci        propertiesMap[profileProperty] = item.second;
715f8af9c48Sopenharmony_ci    }
716f8af9c48Sopenharmony_ci    return propertiesMap;
717f8af9c48Sopenharmony_ci}
718f8af9c48Sopenharmony_ci
719f8af9c48Sopenharmony_cistd::string ProfileUtils::toString(const std::u16string& str16)
720f8af9c48Sopenharmony_ci{
721f8af9c48Sopenharmony_ci    return std::wstring_convert< std::codecvt_utf8_utf16<char16_t>, char16_t >{}.to_bytes(str16);
722f8af9c48Sopenharmony_ci}
723f8af9c48Sopenharmony_ci
724f8af9c48Sopenharmony_cibool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
725f8af9c48Sopenharmony_ci    int32_t maxValue)
726f8af9c48Sopenharmony_ci{
727f8af9c48Sopenharmony_ci    if (propertyMap.count(property) != 0 && 0 < propertyMap.at(property).length() &&
728f8af9c48Sopenharmony_ci        (static_cast<int32_t>(propertyMap.at(property).length())) < maxValue) {
729f8af9c48Sopenharmony_ci        return true;
730f8af9c48Sopenharmony_ci    }
731f8af9c48Sopenharmony_ci    HILOGE("property is valid, property : %{public}s", property.c_str());
732f8af9c48Sopenharmony_ci    return false;
733f8af9c48Sopenharmony_ci}
734f8af9c48Sopenharmony_ci
735f8af9c48Sopenharmony_cibool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
736f8af9c48Sopenharmony_ci    int32_t minValue, int32_t maxValue)
737f8af9c48Sopenharmony_ci{
738f8af9c48Sopenharmony_ci    if (property.empty() || propertyMap.find(property) == propertyMap.end()) {
739f8af9c48Sopenharmony_ci        HILOGE("property is valid, property : %{public}s", property.c_str());
740f8af9c48Sopenharmony_ci        return false;
741f8af9c48Sopenharmony_ci    }
742f8af9c48Sopenharmony_ci    std::string propertyValue = propertyMap.at(property);
743f8af9c48Sopenharmony_ci    if (!IsNumStr(propertyValue)) {
744f8af9c48Sopenharmony_ci        HILOGE("%{public}s is not numeric string", GetAnonyString(propertyValue).c_str());
745f8af9c48Sopenharmony_ci        return false;
746f8af9c48Sopenharmony_ci    }
747f8af9c48Sopenharmony_ci    if (minValue < std::atoi(propertyValue.c_str()) && std::atoi(propertyValue.c_str()) < maxValue) {
748f8af9c48Sopenharmony_ci        return true;
749f8af9c48Sopenharmony_ci    }
750f8af9c48Sopenharmony_ci    return false;
751f8af9c48Sopenharmony_ci}
752f8af9c48Sopenharmony_ci
753f8af9c48Sopenharmony_cibool ProfileUtils::IsNumStr(const std::string& inString)
754f8af9c48Sopenharmony_ci{
755f8af9c48Sopenharmony_ci    if (inString.empty()) {
756f8af9c48Sopenharmony_ci        HILOGE("inString is empty");
757f8af9c48Sopenharmony_ci        return false;
758f8af9c48Sopenharmony_ci    }
759f8af9c48Sopenharmony_ci    std::regex isNumStrRule(IS_NUMSTRING_RULES);
760f8af9c48Sopenharmony_ci    return std::regex_match(inString, isNumStrRule);
761f8af9c48Sopenharmony_ci}
762f8af9c48Sopenharmony_ci
763f8af9c48Sopenharmony_cibool ProfileUtils::GetIntValue(const ValuesBucket& values, const std::string& property, int32_t& value)
764f8af9c48Sopenharmony_ci{
765f8af9c48Sopenharmony_ci    ValueObject valueObject;
766f8af9c48Sopenharmony_ci    if (values.GetObject(property, valueObject) && valueObject.GetInt(value) == NativeRdb::E_OK) {
767f8af9c48Sopenharmony_ci        return true;
768f8af9c48Sopenharmony_ci    }
769f8af9c48Sopenharmony_ci    return false;
770f8af9c48Sopenharmony_ci}
771f8af9c48Sopenharmony_ci
772f8af9c48Sopenharmony_cibool ProfileUtils::GetStringValue(const ValuesBucket& values, const std::string& property, std::string& value)
773f8af9c48Sopenharmony_ci{
774f8af9c48Sopenharmony_ci    ValueObject valueObject;
775f8af9c48Sopenharmony_ci    if (values.GetObject(property, valueObject) && valueObject.GetString(value) == NativeRdb::E_OK) {
776f8af9c48Sopenharmony_ci        return true;
777f8af9c48Sopenharmony_ci    }
778f8af9c48Sopenharmony_ci    return false;
779f8af9c48Sopenharmony_ci}
780f8af9c48Sopenharmony_ci
781f8af9c48Sopenharmony_cibool ProfileUtils::GetLongValue(const ValuesBucket& values, const std::string& property, int64_t& value)
782f8af9c48Sopenharmony_ci{
783f8af9c48Sopenharmony_ci    ValueObject valueObject;
784f8af9c48Sopenharmony_ci    if (values.GetObject(property, valueObject) && valueObject.GetLong(value) == NativeRdb::E_OK) {
785f8af9c48Sopenharmony_ci        return true;
786f8af9c48Sopenharmony_ci    }
787f8af9c48Sopenharmony_ci    return false;
788f8af9c48Sopenharmony_ci}
789f8af9c48Sopenharmony_ci
790f8af9c48Sopenharmony_cibool ProfileUtils::GetUdidByNetworkId(const std::string& networkId, std::string& udid)
791f8af9c48Sopenharmony_ci{
792f8af9c48Sopenharmony_ci    return ((DeviceManager::GetInstance().GetUdidByNetworkId(DP_PKG_NAME, networkId, udid) == 0) ? true : false);
793f8af9c48Sopenharmony_ci}
794f8af9c48Sopenharmony_ci
795f8af9c48Sopenharmony_cibool ProfileUtils::GetUuidByNetworkId(const std::string& networkId, std::string& uuid)
796f8af9c48Sopenharmony_ci{
797f8af9c48Sopenharmony_ci    return ((DeviceManager::GetInstance().GetUuidByNetworkId(DP_PKG_NAME, networkId, uuid) == 0) ? true : false);
798f8af9c48Sopenharmony_ci}
799f8af9c48Sopenharmony_ci
800f8af9c48Sopenharmony_cistd::string ProfileUtils::GetDbKeyByProfile(const CharacteristicProfile& profile)
801f8af9c48Sopenharmony_ci{
802f8af9c48Sopenharmony_ci    if (profile.GetDeviceId().empty() ||
803f8af9c48Sopenharmony_ci        profile.GetServiceName().empty() ||
804f8af9c48Sopenharmony_ci        profile.GetCharacteristicKey().empty()) {
805f8af9c48Sopenharmony_ci        HILOGE("GetDbKeyByProfile fail");
806f8af9c48Sopenharmony_ci        return "";
807f8af9c48Sopenharmony_ci    }
808f8af9c48Sopenharmony_ci    std::string serviceName = CheckAndAddOhSuffix(profile.GetServiceName(), true);
809f8af9c48Sopenharmony_ci    std::string dbKey = CHAR_PREFIX + SEPARATOR + profile.GetDeviceId() + SEPARATOR + serviceName +
810f8af9c48Sopenharmony_ci        SEPARATOR + profile.GetCharacteristicKey() + SEPARATOR + CHARACTERISTIC_VALUE;
811f8af9c48Sopenharmony_ci    return dbKey;
812f8af9c48Sopenharmony_ci}
813f8af9c48Sopenharmony_ci} // namespace DistributedDeviceProfile
814f8af9c48Sopenharmony_ci} // namespace OHOS
815