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 "rdb_helper.h" 25 #include "rdb_store.h" 26 #include "result_set.h" 27 #include "values_bucket.h" 28 29 #include "access_control_profile.h" 30 #include "characteristic_profile.h" 31 #include "device_profile.h" 32 #include "distributed_device_profile_constants.h" 33 #include "service_profile.h" 34 #include "trust_device_profile.h" 35 36 namespace OHOS { 37 namespace DistributedDeviceProfile { 38 using namespace OHOS::NativeRdb; 39 40 class ProfileUtils { 41 public: 42 static std::string GetDbKeyAnonyString(const std::string& value); 43 static std::string GetAnonyString(const std::string& value); 44 static std::string GetAnonyInt32(const int32_t value); 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 bool isMultiUser = false); 73 static int32_t ServiceProfileToEntries(const ServiceProfile& profile, std::map<std::string, std::string>& values, 74 bool isMultiUser = false); 75 static int32_t CharacteristicProfileToEntries(const CharacteristicProfile& profile, 76 std::map<std::string, std::string>& values, bool isMultiUser = false); 77 static int32_t EntriesToTrustDeviceProfile(const ValuesBucket& values, TrustDeviceProfile& profile); 78 static int32_t EntriesToAccessControlProfile(const ValuesBucket& values, AccessControlProfile& profile); 79 static int32_t EntriesToAccesser(const ValuesBucket& values, Accesser& accesser); 80 static int32_t EntriesToAccessee(const ValuesBucket& values, Accessee& accessee); 81 static int32_t EntriesToAccesserExt(const ValuesBucket& values, Accesser& accesser); 82 static int32_t EntriesToAccesseeExt(const ValuesBucket& values, Accessee& accessee); 83 static int32_t EntriesToDeviceProfile(std::map<std::string, std::string> values, DeviceProfile& profile); 84 static int32_t EntriesToServiceProfile(std::map<std::string, std::string> values, ServiceProfile& profile); 85 static int32_t EntriesToCharProfile(std::map<std::string, std::string> values, CharacteristicProfile& profile); 86 static std::string GenerateDBKey(const std::string& profileKey, const std::string& profileProperty, 87 int32_t userId = DEFAULT_USER_ID); 88 static std::string GetProfileKey(const std::string& dbKey); 89 static std::string GetDeviceIdByDBKey(const std::string& dbKey); 90 static std::string GetServiceNameByDBKey(const std::string& dbKey); 91 static std::string GetNonOhSuffixServiceNameByDBKey(const std::string& dbKey); 92 static bool IsNeedAddOhSuffix(const std::string& profileName, bool isSvr); 93 static std::string CheckAndAddOhSuffix(const std::string& profileName, bool isSvr); 94 static std::string CheckAndRemoveOhSuffix(const std::string& profileName); 95 static std::string GetCharKeyByDBKey(const std::string& dbKey); 96 static std::string GetDbKeyByProfile(const CharacteristicProfile& profile); 97 static int32_t SplitString(const std::string& str, const std::string& splits, std::vector<std::string>& res); 98 static std::string JoinString(const std::vector<std::string>& strs, const std::string& delimiter); 99 static std::string GetProfileProperty(const std::string& dbKey, int32_t userId = DEFAULT_USER_ID); 100 static std::map<std::string, std::string> GetProfilePropertiesMap(std::map<std::string, std::string> dbEntries, 101 int32_t userId = DEFAULT_USER_ID); 102 static std::string toString(const std::u16string& str16); 103 static bool IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property, 104 int32_t maxValue); 105 static bool IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property, 106 int32_t minValue, int32_t maxValue); 107 static bool IsPropertyValidInt64(const std::map<std::string, 108 std::string>& propertyMap, const std::string& property); 109 static bool GetIntValue(const ValuesBucket& values, const std::string& property, int32_t& value); 110 static bool GetStringValue(const ValuesBucket& values, const std::string& property, std::string& value); 111 static bool GetLongValue(const ValuesBucket& values, const std::string& property, int64_t& value); 112 static bool IsNumStr(const std::string& inString); 113 static int32_t GetUserIdFromDbKey(const std::string& dbKey); 114 static std::string RemoveUserIdFromDbKey(const std::string& dbKey); 115 static int32_t GenerateServiceDBkeys(const std::string& deviceId, const std::string& serviceName, 116 std::vector<std::string>& dbKeys, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); 117 static int32_t GenerateCharacteristicDBkeys(const std::string& deviceId, const std::string& serviceName, 118 const std::string& characteristicKey, std::vector<std::string>& dbKeys, bool isMultiUser = false, 119 int32_t userId = DEFAULT_USER_ID); 120 static int32_t ConvertToTrustDeviceProfile(const AccessControlProfile& accessControlProfile, 121 TrustDeviceProfile& trustDeviceProfile); 122 static int32_t ConvertToAccessControlProfiles(std::shared_ptr<ResultSet> resultSet, 123 std::shared_ptr<ResultSet> accesserResultSet, std::shared_ptr<ResultSet> accesseeResultSet, 124 std::vector<AccessControlProfile>& profile); 125 static int32_t ConvertToTrustDeviceProfile(std::shared_ptr<ResultSet> trustResultSet, 126 TrustDeviceProfile& trustDeviceProfile); 127 static int32_t ConvertToAccesser(std::shared_ptr<ResultSet> accesserResultSet, Accesser& accesser); 128 static int32_t ConvertToAccessee(std::shared_ptr<ResultSet> accesseeResultSet, Accessee& accessee); 129 static int32_t ConvertToAccessControlProfile(std::shared_ptr<ResultSet> accessControlResultSet, 130 AccessControlProfile& accessControlProfile); 131 static bool IsExistColumn(RdbStore& store, const std::string &tabName, const std::string &colName, 132 const std::string &colType, int32_t &errCode); 133 }; 134 } // namespace DistributedDeviceProfile 135 } // namespace OHOS 136 #endif // OHOS_DP_PROFILE_UTILS_H 137