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 "result_set.h" 25 #include "values_bucket.h" 26 27 #include "access_control_profile.h" 28 #include "characteristic_profile.h" 29 #include "device_profile.h" 30 #include "distributed_device_profile_constants.h" 31 #include "service_profile.h" 32 #include "trust_device_profile.h" 33 34 namespace OHOS { 35 namespace DistributedDeviceProfile { 36 using namespace OHOS::NativeRdb; 37 38 class ProfileUtils { 39 public: 40 static std::string GetDbKeyAnonyString(const std::string& value); 41 static std::string GetAnonyString(const std::string& value); 42 static std::string GetAnonyInt32(const int32_t value); 43 static bool IsP2p(const int32_t authForm); 44 static ProfileType GetProfileType(const std::string& key); 45 static bool StartsWith(const std::string& str, const std::string& prefix); 46 static bool EndsWith(const std::string& str, const std::string& suffix); 47 static bool IsKeyValid(const std::string& key); 48 static bool IsLocalUdid(const std::string& udid); 49 // This mothed can be invoked only when put or delete profile. 50 static bool IsDevProfileValid(const DeviceProfile& devProfile); 51 // This mothed can be invoked only when put or delete profile. 52 static bool IsSvrProfileValid(const ServiceProfile& svrProfile); 53 // This mothed can be invoked only when put or delete profile. 54 static bool IsCharProfileValid(const CharacteristicProfile& charProfile); 55 // This mothed can be invoked only when get profile. 56 static bool IsDeviceProfileValid(const DeviceProfile& devProfile); 57 // This mothed can be invoked only when get profile. 58 static bool IsServiceProfileValid(const ServiceProfile& svrProfile); 59 // This mothed can be invoked only when get profile. 60 static bool IsCharacteristicProfileValid(const CharacteristicProfile& charProfile); 61 static std::string GenerateDeviceProfileKey(const std::string& deviceId); 62 static std::string GenerateServiceProfileKey(const std::string& deviceId, const std::string& serviceName); 63 static std::string GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName, 64 const std::string& charKey); 65 static int32_t TrustDeviceProfileToEntries(const TrustDeviceProfile& profile, ValuesBucket& values); 66 static int32_t AccessControlProfileToEntries(const AccessControlProfile& profile, ValuesBucket& values); 67 static int32_t AccesserToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values); 68 static int32_t AccesseeToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values); 69 static int32_t DeviceProfileToEntries(const DeviceProfile& profile, std::map<std::string, std::string>& values, 70 bool isMultiUser = false); 71 static int32_t ServiceProfileToEntries(const ServiceProfile& profile, std::map<std::string, std::string>& values, 72 bool isMultiUser = false); 73 static int32_t CharacteristicProfileToEntries(const CharacteristicProfile& profile, 74 std::map<std::string, std::string>& values, bool isMultiUser = false); 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 EntriesToAccesserExt(const ValuesBucket& values, Accesser& accesser); 80 static int32_t EntriesToAccesseeExt(const ValuesBucket& values, Accessee& accessee); 81 static int32_t EntriesToDeviceProfile(std::map<std::string, std::string> values, DeviceProfile& profile); 82 static int32_t EntriesToServiceProfile(std::map<std::string, std::string> values, ServiceProfile& profile); 83 static int32_t EntriesToCharProfile(std::map<std::string, std::string> values, CharacteristicProfile& profile); 84 static std::string GenerateDBKey(const std::string& profileKey, const std::string& profileProperty, 85 int32_t userId = DEFAULT_USER_ID); 86 static std::string GetProfileKey(const std::string& dbKey); 87 static std::string GetDeviceIdByDBKey(const std::string& dbKey); 88 static std::string GetServiceNameByDBKey(const std::string& dbKey); 89 static std::string GetNonOhSuffixServiceNameByDBKey(const std::string& dbKey); 90 static bool IsNeedAddOhSuffix(const std::string& profileName, bool isSvr); 91 static std::string CheckAndAddOhSuffix(const std::string& profileName, bool isSvr); 92 static std::string CheckAndRemoveOhSuffix(const std::string& profileName); 93 static std::string GetCharKeyByDBKey(const std::string& dbKey); 94 static std::string GetDbKeyByProfile(const CharacteristicProfile& profile); 95 static int32_t SplitString(const std::string& str, const std::string& splits, std::vector<std::string>& res); 96 static std::string JoinString(const std::vector<std::string>& strs, const std::string& delimiter); 97 static std::string GetProfileProperty(const std::string& dbKey, int32_t userId = DEFAULT_USER_ID); 98 static std::map<std::string, std::string> GetProfilePropertiesMap(std::map<std::string, std::string> dbEntries, 99 int32_t userId = DEFAULT_USER_ID); 100 static std::string toString(const std::u16string& str16); 101 static bool IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property, 102 int32_t maxValue); 103 static bool IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property, 104 int32_t minValue, int32_t maxValue); 105 static bool IsPropertyValidInt64(const std::map<std::string, 106 std::string>& propertyMap, const std::string& property); 107 static bool GetIntValue(const ValuesBucket& values, const std::string& property, int32_t& value); 108 static bool GetStringValue(const ValuesBucket& values, const std::string& property, std::string& value); 109 static bool GetLongValue(const ValuesBucket& values, const std::string& property, int64_t& value); 110 static bool IsNumStr(const std::string& inString); 111 static int32_t GetUserIdFromDbKey(const std::string& dbKey); 112 static std::string RemoveUserIdFromDbKey(const std::string& dbKey); 113 static int32_t GenerateServiceDBkeys(const std::string& deviceId, const std::string& serviceName, 114 std::vector<std::string>& dbKeys, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); 115 static int32_t GenerateCharacteristicDBkeys(const std::string& deviceId, const std::string& serviceName, 116 const std::string& characteristicKey, std::vector<std::string>& dbKeys, bool isMultiUser = false, 117 int32_t userId = DEFAULT_USER_ID); 118 static int32_t ConvertToTrustDeviceProfile(const AccessControlProfile& accessControlProfile, 119 TrustDeviceProfile& trustDeviceProfile); 120 static int32_t ConvertToAccessControlProfiles(std::shared_ptr<ResultSet> resultSet, 121 std::shared_ptr<ResultSet> accesserResultSet, std::shared_ptr<ResultSet> accesseeResultSet, 122 std::vector<AccessControlProfile>& profile); 123 static int32_t ConvertToTrustDeviceProfile(std::shared_ptr<ResultSet> trustResultSet, 124 TrustDeviceProfile& trustDeviceProfile); 125 static int32_t ConvertToAccesser(std::shared_ptr<ResultSet> accesserResultSet, Accesser& accesser); 126 static int32_t ConvertToAccessee(std::shared_ptr<ResultSet> accesseeResultSet, Accessee& accessee); 127 static int32_t ConvertToAccessControlProfile(std::shared_ptr<ResultSet> accessControlResultSet, 128 AccessControlProfile& accessControlProfile); 129 }; 130 } // namespace DistributedDeviceProfile 131 } // namespace OHOS 132 #endif // OHOS_DP_PROFILE_UTILS_H 133