1/*
2 * Copyright (c) 2023-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#ifndef OHOS_DP_PROFILE_UTILS_H
17#define OHOS_DP_PROFILE_UTILS_H
18
19#include <map>
20#include <mutex>
21#include <string>
22#include <vector>
23
24#include "values_bucket.h"
25
26#include "access_control_profile.h"
27#include "characteristic_profile.h"
28#include "device_profile.h"
29#include "service_profile.h"
30#include "trust_device_profile.h"
31
32namespace OHOS {
33namespace DistributedDeviceProfile {
34using namespace OHOS::NativeRdb;
35
36class ProfileUtils {
37public:
38    static std::string GetDbKeyAnonyString(const std::string& value);
39    static std::string GetAnonyString(const std::string& value);
40    static std::vector<std::string> GetOnlineDevices();
41    static std::string GetLocalUdidFromDM();
42    static bool FilterAndGroupOnlineDevices(const std::vector<std::string>& deviceList,
43        std::vector<std::string>& ohBasedDevices, std::vector<std::string>& notOHBasedDevices);
44    static bool IsOHBasedDevice(const std::string& extraData);
45    static bool IsP2p(const int32_t authForm);
46    static ProfileType GetProfileType(const std::string& key);
47    static bool StartsWith(const std::string& str, const std::string& prefix);
48    static bool EndsWith(const std::string& str, const std::string& suffix);
49    static bool IsKeyValid(const std::string& key);
50    static bool IsLocalUdid(const std::string& udid);
51    // This mothed can be invoked only when put or delete profile.
52    static bool IsDevProfileValid(const DeviceProfile& devProfile);
53    // This mothed can be invoked only when put or delete profile.
54    static bool IsSvrProfileValid(const ServiceProfile& svrProfile);
55    // This mothed can be invoked only when put or delete profile.
56    static bool IsCharProfileValid(const CharacteristicProfile& charProfile);
57    // This mothed can be invoked only when get profile.
58    static bool IsDeviceProfileValid(const DeviceProfile& devProfile);
59    // This mothed can be invoked only when get profile.
60    static bool IsServiceProfileValid(const ServiceProfile& svrProfile);
61    // This mothed can be invoked only when get profile.
62    static bool IsCharacteristicProfileValid(const CharacteristicProfile& charProfile);
63    static std::string GenerateDeviceProfileKey(const std::string& deviceId);
64    static std::string GenerateServiceProfileKey(const std::string& deviceId, const std::string& serviceName);
65    static std::string GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName,
66        const std::string& charKey);
67    static int32_t TrustDeviceProfileToEntries(const TrustDeviceProfile& profile, ValuesBucket& values);
68    static int32_t AccessControlProfileToEntries(const AccessControlProfile& profile, ValuesBucket& values);
69    static int32_t AccesserToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values);
70    static int32_t AccesseeToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values);
71    static int32_t DeviceProfileToEntries(const DeviceProfile& profile, std::map<std::string, std::string>& values);
72    static int32_t ServiceProfileToEntries(const ServiceProfile& profile, std::map<std::string, std::string>& values);
73    static int32_t CharacteristicProfileToEntries(const CharacteristicProfile& profile,
74        std::map<std::string, std::string>& values);
75    static int32_t EntriesToTrustDeviceProfile(const ValuesBucket& values, TrustDeviceProfile& profile);
76    static int32_t EntriesToAccessControlProfile(const ValuesBucket& values, AccessControlProfile& profile);
77    static int32_t EntriesToAccesser(const ValuesBucket& values, Accesser& accesser);
78    static int32_t EntriesToAccessee(const ValuesBucket& values, Accessee& accessee);
79    static int32_t EntriesToDeviceProfile(std::map<std::string, std::string> values, DeviceProfile& profile);
80    static int32_t EntriesToServiceProfile(std::map<std::string, std::string> values, ServiceProfile& profile);
81    static int32_t EntriesToCharProfile(std::map<std::string, std::string> values, CharacteristicProfile& profile);
82    static std::string GenerateDBKey(const std::string& profileKey, const std::string& profileProperty);
83    static std::string GetProfileKey(const std::string& dbKey);
84    static std::string GetDeviceIdByDBKey(const std::string& dbKey);
85    static std::string GetServiceNameByDBKey(const std::string& dbKey);
86    static std::string GetNonOhSuffixServiceNameByDBKey(const std::string& dbKey);
87    static bool IsNeedAddOhSuffix(const std::string& profileName, bool isSvr);
88    static std::string CheckAndAddOhSuffix(const std::string& profileName, bool isSvr);
89    static std::string CheckAndRemoveOhSuffix(const std::string& profileName);
90    static std::string GetCharKeyByDBKey(const std::string& dbKey);
91    static std::string GetDbKeyByProfile(const CharacteristicProfile& profile);
92    static int32_t SplitString(const std::string& str, const std::string& splits, std::vector<std::string>& res);
93    static std::string JoinString(const std::vector<std::string>& strs, const std::string& delimiter);
94    static std::string GetProfileProperty(const std::string& dbKey);
95    static std::map<std::string, std::string> GetProfilePropertiesMap(std::map<std::string, std::string> dbEntries);
96    static std::string toString(const std::u16string& str16);
97    static bool IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
98        int32_t maxValue);
99    static bool IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
100        int32_t minValue, int32_t maxValue);
101    static bool GetIntValue(const ValuesBucket& values, const std::string& property, int32_t& value);
102    static bool GetStringValue(const ValuesBucket& values, const std::string& property, std::string& value);
103    static bool GetLongValue(const ValuesBucket& values, const std::string& property, int64_t& value);
104    static bool GetUdidByNetworkId(const std::string& networkId, std::string& udid);
105    static bool GetUuidByNetworkId(const std::string& networkId, std::string& uuid);
106    static bool IsNumStr(const std::string& inString);
107};
108} // namespace DistributedDeviceProfile
109} // namespace OHOS
110#endif // OHOS_DP_PROFILE_UTILS_H
111