diff --git a/common/BUILD.gn b/common/BUILD.gn index 9f148dde9bdc9c24fb657cdf29cc71f8bba6b210..016fc54f89e4ceecb5b8cb2f1832bad2574a5589 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -105,6 +105,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accessee.cpp", "src/interfaces/accesser.cpp", "src/interfaces/business_event.cpp", + "src/interfaces/service_info_profile_new.cpp", "src/interfaces/characteristic_profile.cpp", "src/interfaces/device_icon_info.cpp", "src/interfaces/device_icon_info_filter_options.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index c435bab7d3941b430c069b7c98d0b31b70685553..d192e01fc475a58d82eaeb0ee8dadc39dea597f1 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -121,6 +121,10 @@ extern const std::string GET_SERVICE_INFO_PROFILE_BY_UNIQUE_KEY; extern const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_TOKEN_ID; extern const std::string GET_ALL_SERVICE_INFO_PROFILE_LIST; extern const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_BUNDLE_NAME; +extern const std::string REG_SERVICE_ID; +extern const std::string PUBLISH_STATE; +extern const std::string SERVICE_INFO; +extern const std::string SERVICE_IS_MULTI_USER; /* LocalServiceInfo Attribute */ extern const std::string PUT_LOCAL_SERVICE_INFO; extern const std::string UPDATE_LOCAL_SERVICE_INFO; @@ -320,6 +324,25 @@ extern const std::string LNN_ACL_TRUE; extern const std::string BUSINESS_KEY; extern const std::string BUSINESS_VALUE; +extern const std::string SERVICE_ID_KEY; +extern const std::string SERVICE_ID_VALUE; +extern const std::string SERVICE_TYPE_KEY; +extern const std::string SERVICE_TYPE_VALUE; +extern const std::string SERVICE_NAME_KEY; +extern const std::string SERVICE_NAME_VALUE; +extern const std::string SERVICE_DISPLAY_NAME_KEY; +extern const std::string SERVICE_DISPLAY_NAME_VALUE; +extern const std::string REG_SERVICE_ID_KEY; +extern const std::string REG_SERVICE_ID_VALUE; +extern const std::string SER_PUB_STATE_KEY; +extern const std::string SER_PUB_STATE_VALUE; +extern const std::string DEVICE_ID_KEY; +extern const std::string DEVICE_ID_VALUE; +extern const std::string SER_USER_ID_KEY; +extern const std::string SER_USER_ID_VALUE; +extern const std::string SER_TOKEN_ID_KEY; +extern const std::string SER_TOKEN_ID_VALUE; + constexpr int32_t ROWCNT_INIT = -1; constexpr int32_t RET_INIT = -1; constexpr int32_t ROWCOUNT_INIT = -1; diff --git a/common/include/interfaces/distributed_device_profile_enums.h b/common/include/interfaces/distributed_device_profile_enums.h index 3ca83d933ee149843c749445407f235bd2d08d9e..bcb3c673f279df8b248dd7d08b705498dafab6a0 100644 --- a/common/include/interfaces/distributed_device_profile_enums.h +++ b/common/include/interfaces/distributed_device_profile_enums.h @@ -92,7 +92,10 @@ enum class DPInterfaceCode : uint32_t { UNREGISTER_BUSINESS_CALLBACK = 73, PUT_BUSINESS_EVENT = 74, GET_BUSINESS_EVENT = 75, - MAX = 76 + PUT_SERVICE_INFO_PROFILE_NEW = 76, + DEL_SERVICE_INFO_PROFILE_NEW = 77, + GET_SERVICE_INFO_PROFILE_NEW = 78, + MAX = 79 }; enum class DeviceIdType : uint32_t { diff --git a/common/include/interfaces/i_distributed_device_profile.h b/common/include/interfaces/i_distributed_device_profile.h index 413c643c081fd45f04fa222ef7d78d8077aad56b..b44f5431548c6410dd7591e89666ab28c474d4c7 100644 --- a/common/include/interfaces/i_distributed_device_profile.h +++ b/common/include/interfaces/i_distributed_device_profile.h @@ -37,6 +37,7 @@ #include "trusted_device_info.h" #include "local_service_info.h" #include "business_event.h" +#include "service_info_profile_new.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -110,6 +111,9 @@ public: virtual int32_t UnRegisterBusinessCallback(const std::string& saId, const std::string& businessKey) = 0; virtual int32_t PutBusinessEvent(const BusinessEvent& event) = 0; virtual int32_t GetBusinessEvent(BusinessEvent& event) = 0; + virtual int32_t PutServiceInfoProfileNew(const ServiceInfoProfileNew & serviceInfo) = 0; + virtual int32_t DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, int32_t userId) = 0; + virtual int32_t GetServiceInfo(ServiceInfoProfileNew & serviceInfo) = 0; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/service_info_profile_new.h b/common/include/interfaces/service_info_profile_new.h new file mode 100644 index 0000000000000000000000000000000000000000..aca6453098217c82c762a84fff15bda73673d75d --- /dev/null +++ b/common/include/interfaces/service_info_profile_new.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DP_SERVICE_INFO_H +#define OHOS_DP_SERVICE_INFO_H + +#include +#include + +#include "distributed_device_profile_constants.h" +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceInfoProfileNew : public DpParcel { +public: + ServiceInfoProfileNew() + : regServiceId_(0), + deviceId_(""), + isMultiUser_(false), + userId_(0), + tokenId_(0), + publishState_(0), + serviceId_(0), + serviceType_(""), + serviceName_(""), + serviceDisplayName_("") + {}; + + ~ServiceInfoProfileNew(); + + int32_t GetRegServiceId() const; + void SetRegServiceId(const int32_t ®ServiceIdValue); + + std::string GetDeviceId() const; + void SetDeviceId(const std::string &deviceIdValue); + + bool GetIsMultiUser() const; + void SetIsMultiUser(bool isMultiUser); + + int32_t GetUserId() const; + void SetUserId(const int32_t &userIdValue); + + int64_t GetTokenId() const; + void SetTokenId(const int64_t &tokenIdValue); + + int8_t GetSerPubState() const; + void SetSerPubState(const int8_t &SerPubStateValue); + + int64_t GetServiceId() const; + void SetServiceId(const int64_t serviceIdValue); + + std::string GetServiceType() const; + void SetServiceType(const std::string &serviceTypeValue); + + std::string GetServiceName() const; + void SetServiceName(const std::string &serviceNameValue); + + std::string GetServiceDisplayName() const; + void SetServiceDisplayName(const std::string &serviceDisplayNameValue); + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + int32_t regServiceId_; + std::string deviceId_; + bool isMultiUser_; + int32_t userId_; + int64_t tokenId_; + int8_t publishState_; + int64_t serviceId_; + std::string serviceType_; + std::string serviceName_; + std::string serviceDisplayName_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif //OHOS_DP_SERVICE_INFO_H + \ No newline at end of file diff --git a/common/include/utils/profile_utils.h b/common/include/utils/profile_utils.h index 870322f912d4ca85cf40f09fdc4c3acacf048b03..52cf279531f8437d3c215a7bc01f28b7381285ea 100644 --- a/common/include/utils/profile_utils.h +++ b/common/include/utils/profile_utils.h @@ -32,6 +32,7 @@ #include "distributed_device_profile_constants.h" #include "service_profile.h" #include "trust_device_profile.h" +#include "service_info_profile_new.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -53,6 +54,8 @@ public: // This mothed can be invoked only when put or delete profile. static bool IsSvrProfileValid(const ServiceProfile& svrProfile); // This mothed can be invoked only when put or delete profile. + static bool IsSerInfoProfileValid(const ServiceInfoProfileNew& serInfoProfile); + // This mothed can be invoked only when put or delete profile. static bool IsCharProfileValid(const CharacteristicProfile& charProfile); // This mothed can be invoked only when get profile. static bool IsDeviceProfileValid(const DeviceProfile& devProfile); @@ -62,6 +65,7 @@ public: static bool IsCharacteristicProfileValid(const CharacteristicProfile& charProfile); static std::string GenerateDeviceProfileKey(const std::string& deviceId); static std::string GenerateServiceProfileKey(const std::string& deviceId, const std::string& serviceName); + static std::string GenerateServiceInfoProfileKey(const std::string& regServiceId); static std::string GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName, const std::string& charKey); static int32_t TrustDeviceProfileToEntries(const TrustDeviceProfile& profile, ValuesBucket& values); @@ -72,6 +76,8 @@ public: bool isMultiUser = false); static int32_t ServiceProfileToEntries(const ServiceProfile& profile, std::map& values, bool isMultiUser = false); + static int32_t ServiceInfoProfileToEntries(const ServiceInfoProfileNew& profile, std::map& values, + bool isMultiUser = false); static int32_t CharacteristicProfileToEntries(const CharacteristicProfile& profile, std::map& values, bool isMultiUser = false); static int32_t EntriesToTrustDeviceProfile(const ValuesBucket& values, TrustDeviceProfile& profile); @@ -85,6 +91,8 @@ public: static int32_t EntriesToCharProfile(std::map values, CharacteristicProfile& profile); static std::string GenerateDBKey(const std::string& profileKey, const std::string& profileProperty, int32_t userId = DEFAULT_USER_ID); + static std::string GenerateServiceDBKey(const std::string& regSerDBId, const std::string& fieldInfo, + int32_t userId = DEFAULT_USER_ID); static std::string GetProfileKey(const std::string& dbKey); static std::string GetDeviceIdByDBKey(const std::string& dbKey); static std::string GetServiceNameByDBKey(const std::string& dbKey); @@ -114,6 +122,8 @@ public: static std::string RemoveUserIdFromDbKey(const std::string& dbKey); static int32_t GenerateServiceDBkeys(const std::string& deviceId, const std::string& serviceName, std::vector& dbKeys, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); + static int32_t GenerateServiceInfoProfilekeys(const std::string& regServiceId, std::vector& dbKeys, + bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); static int32_t GenerateCharacteristicDBkeys(const std::string& deviceId, const std::string& serviceName, const std::string& characteristicKey, std::vector& dbKeys, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index e86ea9c00a3579a34621b078cb9484ecd767b857..c663739c1b329351559467ecb74a7faaccda06de 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -72,6 +72,7 @@ const std::string SERVICE_PROFILE_TABLE = "service_profile"; const std::string RDB_USER_ID = "userId"; const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID = "deviceProfileId"; const std::string SERVICE_PROFILE_SERVICE_TYPE = "serviceType"; + /* CharacteristicProfile Attribute */ const std::string SERVICE_PROFILE_ID = "serviceProfileId"; const std::string CHARACTERISTIC_PROFILE_TABLE = "characteristic_profile"; @@ -168,6 +169,10 @@ const std::string GET_SERVICE_INFO_PROFILE_BY_UNIQUE_KEY = "GetServiceInfoProfil const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_TOKEN_ID = "GetServiceInfoProfileListByTokenId"; const std::string GET_ALL_SERVICE_INFO_PROFILE_LIST = "GetAllServiceInfoProfileList"; const std::string GET_SERVICE_INFO_PROFILE_LIST_BY_BUNDLE_NAME = "GetServiceInfoProfileListByBundleName"; +const std::string REG_SERVICE_ID = "regServiceId"; +const std::string PUBLISH_STATE = "publishState"; +const std::string SERVICE_INFO = "serviceInfo"; +const std::string SERVICE_IS_MULTI_USER = "isMultiUser"; /* LocalServiceInfo Attribute */ const std::string PUT_LOCAL_SERVICE_INFO = "PutLocalServiceInfo"; const std::string UPDATE_LOCAL_SERVICE_INFO = "UpdateLocalServiceInfo"; @@ -246,6 +251,26 @@ const std::string LNN_ACL_TRUE = "true"; const std::string BUSINESS_KEY = "businessKey"; const std::string BUSINESS_VALUE = "businessValue"; +//service info profile keys value +const std::string SERVICE_ID_KEY = "serviceIdKey"; +const std::string SERVICE_ID_VALUE = "serviceIdValue"; +const std::string SERVICE_TYPE_KEY = "serviceTypeKey"; +const std::string SERVICE_TYPE_VALUE = "serviceTypeValue"; +const std::string SERVICE_NAME_KEY = "serviceNameKey"; +const std::string SERVICE_NAME_VALUE = "serviceNameValue_"; +const std::string SERVICE_DISPLAY_NAME_KEY = "serviceDisplayNameKey"; +const std::string SERVICE_DISPLAY_NAME_VALUE = "serviceDisplayNameValue"; +const std::string REG_SERVICE_ID_KEY = "regServiceIdKey"; +const std::string REG_SERVICE_ID_VALUE = "regServiceIdValue"; +const std::string SER_PUB_STATE_KEY = "serPubStateKey"; +const std::string SER_PUB_STATE_VALUE = "serPubStateValue"; +const std::string DEVICE_ID_KEY = "deviceIdKey"; +const std::string DEVICE_ID_VALUE = "deviceIdValue"; +const std::string SER_USER_ID_KEY = "userIdKey"; +const std::string SER_USER_ID_VALUE = "userIdValue"; +const std::string SER_TOKEN_ID_KEY = "tokenIdKey"; +const std::string SER_TOKEN_ID_VALUE = "tokenIdValue"; + const std::string CREATE_TURST_DEVICE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS trust_device_table\ (\ deviceId TEXT PRIMARY KEY,\ diff --git a/common/src/interfaces/service_info_profile_new.cpp b/common/src/interfaces/service_info_profile_new.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6af1b2275d6f58ee51322dcca184e74910405501 --- /dev/null +++ b/common/src/interfaces/service_info_profile_new.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "service_info_profile_new.h" +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "macro_utils.h" +#include "profile_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "ServiceInfoProfileNew"; +} + +ServiceInfoProfileNew::~ServiceInfoProfileNew() +{ +} + +int64_t ServiceInfoProfileNew::GetServiceId() const +{ + return serviceId_; +} + +void ServiceInfoProfileNew::SetServiceId(const int64_t serviceId) +{ + serviceId_ = serviceId; +} + +std::string ServiceInfoProfileNew::GetServiceType() const +{ + return serviceType_; +} + +void ServiceInfoProfileNew::SetServiceType(const std::string& serviceType) +{ + serviceType_ = serviceType; +} + +std::string ServiceInfoProfileNew::GetServiceName() const +{ + return serviceName_; +} + +void ServiceInfoProfileNew::SetServiceName(const std::string &serviceName) +{ + serviceName_ = serviceName; +} + +std::string ServiceInfoProfileNew::GetServiceDisplayName() const +{ + return serviceDisplayName_; +} + +void ServiceInfoProfileNew::SetServiceDisplayName(const std::string &serviceDisplayName) +{ + serviceDisplayName_ = serviceDisplayName; +} + +int32_t ServiceInfoProfileNew::GetRegServiceId() const +{ + return regServiceId_; +} + +void ServiceInfoProfileNew::SetRegServiceId(const int32_t ®ServiceId) +{ + regServiceId_ = regServiceId; +} + +int8_t ServiceInfoProfileNew::GetSerPubState() const +{ + return publishState_; +} + +void ServiceInfoProfileNew::SetSerPubState(const int8_t &serPubState) +{ + publishState_ = serPubState; +} + +std::string ServiceInfoProfileNew::GetDeviceId() const +{ + return deviceId_; +} +void ServiceInfoProfileNew::SetDeviceId(const std::string &deviceId) +{ + deviceId_ = deviceId; +} + +int32_t ServiceInfoProfileNew::GetUserId() const +{ + return userId_; +} +void ServiceInfoProfileNew::SetUserId(const int32_t &userId) +{ + userId_ = userId; +} + +int64_t ServiceInfoProfileNew::GetTokenId() const +{ + return tokenId_; +} +void ServiceInfoProfileNew::SetTokenId(const int64_t &tokenId) +{ + tokenId_ = tokenId; +} + +bool ServiceInfoProfileNew::GetIsMultiUser() const +{ + return isMultiUser_; +} + +void ServiceInfoProfileNew::SetIsMultiUser(bool isMultiUser) +{ + isMultiUser_ = isMultiUser; +} + +bool ServiceInfoProfileNew::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER(parcel, Int32, regServiceId_); + WRITE_HELPER(parcel, String, deviceId_); + WRITE_HELPER(parcel, Bool, isMultiUser_); + WRITE_HELPER(parcel, Int32, userId_); + WRITE_HELPER(parcel, Int64, tokenId_); + WRITE_HELPER(parcel, Int8, publishState_); + WRITE_HELPER(parcel, Int64, serviceId_); + WRITE_HELPER(parcel, String, serviceType_); + WRITE_HELPER(parcel, String, serviceName_); + WRITE_HELPER(parcel, String, serviceDisplayName_); + + return true; +} + +bool ServiceInfoProfileNew::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, Int32, regServiceId_, false); + READ_HELPER_RET(parcel, String, deviceId_, false); + READ_HELPER_RET(parcel, Bool, isMultiUser_, false); + READ_HELPER_RET(parcel, Int32, userId_, false); + READ_HELPER_RET(parcel, Int64, tokenId_, false); + READ_HELPER_RET(parcel, Int8, publishState_, false); + READ_HELPER_RET(parcel, Int64, serviceId_, false); + READ_HELPER_RET(parcel, String, serviceType_, false); + READ_HELPER_RET(parcel, String, serviceName_, false); + READ_HELPER_RET(parcel, String, serviceDisplayName_, false); + + return true; +} + +std::string ServiceInfoProfileNew::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (!cJSON_IsObject(json)) { + cJSON_Delete(json); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, DEVICE_ID.c_str(), deviceId_.c_str()); + + cJSON_AddStringToObject(json, SERVICE_IS_MULTI_USER.c_str(), std::to_string(isMultiUser_).c_str()); + + cJSON_AddStringToObject(json, USERID.c_str(), std::to_string(userId_).c_str()); + + cJSON_AddStringToObject(json, TOKENID.c_str(), std::to_string(tokenId_).c_str()); + + cJSON_AddStringToObject(json, PUBLISH_STATE.c_str(), std::to_string(publishState_).c_str()); + + cJSON_AddStringToObject(json, SISERVICE_ID.c_str(), std::to_string(serviceId_).c_str()); + + cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str()); + + cJSON_AddStringToObject(json, SERVICE_NAME.c_str(), serviceName_.c_str()); + + cJSON_AddStringToObject(json, SERVICE_DISPLAY_NAME.c_str(), serviceDisplayName_.c_str()); + + char* jsonChars = cJSON_PrintUnformatted(json); + if (jsonChars == NULL) { + cJSON_Delete(json); + HILOGE("cJSON formatted to string failed!"); + return EMPTY_STRING; + } + std::string jsonStr = jsonChars; + cJSON_Delete(json); + cJSON_free(jsonChars); + return jsonStr; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS + \ No newline at end of file diff --git a/common/src/utils/profile_utils.cpp b/common/src/utils/profile_utils.cpp index 3ba188da503db78d94ede267dc69d1a789c831c8..e569f9c5b6dd86a812fab1ca455dd2e7e06e85cd 100644 --- a/common/src/utils/profile_utils.cpp +++ b/common/src/utils/profile_utils.cpp @@ -294,6 +294,12 @@ bool ProfileUtils::IsSvrProfileValid(const ServiceProfile& svrProfile) IsKeyValid(svrProfile.GetServiceName()); } +bool ProfileUtils::IsSerInfoProfileValid(const ServiceInfoProfileNew& serInfoProfile) +{ + return IsKeyValid(serInfoProfile.GetDeviceId()) && IsLocalUdid(serInfoProfile.GetDeviceId()) && + IsKeyValid(serInfoProfile.GetServiceName()); +} + bool ProfileUtils::IsCharProfileValid(const CharacteristicProfile& charProfile) { return IsKeyValid(charProfile.GetDeviceId()) && IsLocalUdid(charProfile.GetDeviceId()) && @@ -334,6 +340,11 @@ std::string ProfileUtils::GenerateServiceProfileKey(const std::string& deviceId, return SVR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName; } +std::string ProfileUtils::GenerateServiceInfoProfileKey(const std::string& regServiceId) +{ + return SERVICE_INFO + SEPARATOR + regServiceId; +} + std::string ProfileUtils::GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName, const std::string& charKey) { @@ -450,6 +461,53 @@ int32_t ProfileUtils::ServiceProfileToEntries(const ServiceProfile& profile, std return DP_SUCCESS; } +int32_t ProfileUtils::ServiceInfoProfileToEntries(const ServiceInfoProfileNew& profile, std::map& values, bool isMultiUser) +{ + HILOGI("ProfileUtils::ServiceInfoProfileToEntries"); + + std::string regServiceId = CheckAndAddOhSuffix(REG_SERVICE_ID, false); + + std::string deviceIdKey = CheckAndAddOhSuffix(DEVICE_ID, false); + std::string isMultiUserKey = CheckAndAddOhSuffix(SERVICE_IS_MULTI_USER, false); + std::string userIdKey = CheckAndAddOhSuffix(USERID, false); + std::string tokenIdKey = CheckAndAddOhSuffix(TOKENID, false); + std::string publishStateKey = CheckAndAddOhSuffix(PUBLISH_STATE, false); + std::string serviceIdKey = CheckAndAddOhSuffix(SISERVICE_ID, false); + std::string serviceTypeKey = CheckAndAddOhSuffix(SERVICE_TYPE, false); + std::string serviceNameKey = CheckAndAddOhSuffix(SERVICE_NAME, false); + std::string serviceDisplayNameKey = CheckAndAddOhSuffix(SERVICE_DISPLAY_NAME, false); + + int32_t userIdVlue = profile.GetUserId(); + + if (isMultiUser) { + values[GenerateServiceDBKey(regServiceId, deviceIdKey, userIdVlue)] = profile.GetDeviceId(); + values[GenerateServiceDBKey(regServiceId, isMultiUserKey, userIdVlue)] = profile.GetIsMultiUser(); + values[GenerateServiceDBKey(regServiceId, userIdKey, userIdVlue)] = std::to_string(profile.GetUserId()); + values[GenerateServiceDBKey(regServiceId, tokenIdKey, userIdVlue)] = std::to_string(profile.GetTokenId()); + values[GenerateServiceDBKey(regServiceId, publishStateKey, userIdVlue)] = + std::to_string(profile.GetSerPubState()); + values[GenerateServiceDBKey(regServiceId, serviceIdKey, userIdVlue)] = + std::to_string(profile.GetServiceId()); + values[GenerateServiceDBKey(regServiceId, serviceTypeKey, userIdVlue)] = profile.GetServiceType(); + values[GenerateServiceDBKey(regServiceId, serviceNameKey, userIdVlue)] = profile.GetServiceName(); + values[GenerateServiceDBKey(regServiceId, serviceDisplayNameKey, userIdVlue)] = + profile.GetServiceDisplayName(); + } else { + values[GenerateServiceDBKey(regServiceId, deviceIdKey)] = profile.GetDeviceId(); + values[GenerateServiceDBKey(regServiceId, isMultiUserKey)] = profile.GetIsMultiUser(); + values[GenerateServiceDBKey(regServiceId, userIdKey)] = std::to_string(profile.GetUserId()); + values[GenerateServiceDBKey(regServiceId, tokenIdKey)] = std::to_string(profile.GetTokenId()); + values[GenerateServiceDBKey(regServiceId, publishStateKey)] = std::to_string(profile.GetSerPubState()); + values[GenerateServiceDBKey(regServiceId, serviceIdKey)] = std::to_string(profile.GetServiceId()); + values[GenerateServiceDBKey(regServiceId, serviceTypeKey)] = profile.GetServiceType(); + values[GenerateServiceDBKey(regServiceId, serviceNameKey)] = profile.GetServiceName(); + values[GenerateServiceDBKey(regServiceId, serviceDisplayNameKey)] = profile.GetServiceDisplayName(); + } + + return DP_SUCCESS; +} + int32_t ProfileUtils::CharacteristicProfileToEntries(const CharacteristicProfile& profile, std::map& values, bool isMultiUser) { @@ -750,6 +808,20 @@ std::string ProfileUtils::GenerateDBKey(const std::string& profileKey, const std return DBKey; } +std::string ProfileUtils::GenerateServiceDBKey(const std::string& regServiceId, const std::string& trailInfo, + int32_t userId) +{ + std::string DBKey = ""; + + if (userId != DEFAULT_USER_ID) { + DBKey = "serviceInfo" + SEPARATOR + regServiceId + SEPARATOR + trailInfo + SEPARATOR + std::to_string(userId); + } else { + DBKey = "serviceInfo" + SEPARATOR + regServiceId + SEPARATOR + trailInfo; + } + + return DBKey; +} + std::string ProfileUtils::GetProfileProperty(const std::string& dbKey, int32_t userId) { if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) { @@ -979,6 +1051,38 @@ int32_t ProfileUtils::GenerateServiceDBkeys(const std::string& deviceId, const s return DP_SUCCESS; } +int32_t ProfileUtils::GenerateServiceInfoProfilekeys(const std::string& regServiceId, std::vector& dbKeys, + bool isMultiUser, int32_t userId) +{ + std::string regServiceIdTmp = CheckAndAddOhSuffix(regServiceId, false); + std::string serviceProfileKey = GenerateServiceInfoProfileKey(regServiceIdTmp); + // value not need add OH suffix + if (isMultiUser) { + //dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, REG_SERVICE_ID, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, DEVICE_ID, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, RDB_USER_ID, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, TOKENID, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, PUBLISH_STATE, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_PROFILE_SERVICE_ID, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_TYPE, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_NAME, userId)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_DISPLAY_NAME, userId)); + + } else { + //dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, REG_SERVICE_ID)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, DEVICE_ID)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, RDB_USER_ID)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, TOKENID)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, PUBLISH_STATE)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_PROFILE_SERVICE_ID)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_TYPE)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_NAME)); + dbKeys.emplace_back(GenerateDBKey(serviceProfileKey, SERVICE_DISPLAY_NAME)); + } + return DP_SUCCESS; +} + + int32_t ProfileUtils::GenerateCharacteristicDBkeys(const std::string& deviceId, const std::string& serviceName, const std::string& characteristicKey, std::vector& dbKeys, bool isMultiUser, int32_t userId) { diff --git a/interfaces/innerkits/core/include/distributed_device_profile_client.h b/interfaces/innerkits/core/include/distributed_device_profile_client.h index ad13bc05eeb5f818045b57d5ef4426744f81dce8..cba2628fcffcd3d863bfbbded5daab830ce15290 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_client.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_client.h @@ -108,11 +108,13 @@ public: int32_t UnRegisterBusinessCallback(const std::string& saId, const std::string& businessKey); int32_t PutBusinessEvent(const BusinessEvent& event); int32_t GetBusinessEvent(BusinessEvent& event); + int32_t PutServiceInfoProfileNew(const ServiceInfoProfileNew & serviceInfo); + int32_t DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, int32_t userId); + int32_t GetServiceInfo(ServiceInfoProfileNew & serviceInfo); void LoadSystemAbilitySuccess(const sptr &remoteObject); void LoadSystemAbilityFail(); void ReleaseResource(); - public: class SystemAbilityListener : public SystemAbilityStatusChangeStub { public: diff --git a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h index d9e96ee3268e7734972af85c4bc7b3cbac62b0f5..730a7b3917047d8299efc25b09b87fde1afacd20 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h @@ -31,6 +31,7 @@ #include "ipc_utils.h" #include "profile_change_listener_stub.h" + namespace OHOS { namespace DistributedDeviceProfile { class DistributedDeviceProfileProxy : public IRemoteProxy { @@ -103,7 +104,9 @@ public: int32_t UnRegisterBusinessCallback(const std::string& saId, const std::string& businessKey) override; int32_t PutBusinessEvent(const BusinessEvent& event) override; int32_t GetBusinessEvent(BusinessEvent& event) override; - + int32_t PutServiceInfoProfileNew(const ServiceInfoProfileNew & serviceInfoProfile) override; + int32_t DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, int32_t userId) override; + int32_t GetServiceInfo(ServiceInfoProfileNew & serviceInfoProfile) override; private: static inline BrokerDelegator delegator_; }; diff --git a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp index bb6bae509931b7dc8bdf29fcb484e652b9a97622..7c26cca60416735169915cb2ecfc72001f736411 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp @@ -1217,5 +1217,28 @@ int32_t DistributedDeviceProfileClient::GetBusinessEvent(BusinessEvent& event) } return dpService->GetBusinessEvent(event); } + +int32_t DistributedDeviceProfileClient::PutServiceInfoProfileNew(const ServiceInfoProfileNew & serviceInfoProfile) +{ + HILOGI("DistributedDeviceProfileClient::PutServiceInfoProfileNew"); + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->PutServiceInfoProfileNew(serviceInfoProfile); +} + +int32_t DistributedDeviceProfileClient::DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, + int32_t userId) +{ + HILOGI("DistributedDeviceProfileClient::DelServiceInfoProfileNew"); + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->DelServiceInfoProfileNew(regServiceId, isMultiUser, userId); +} } // namespace DeviceProfile } // namespace OHOS diff --git a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp index 9abe3f8dcc31585888f4819f4a8d64aec4a1eac0..6ce9e25260612758e0b02ccbcbce0cd6f5073232 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp @@ -848,5 +848,43 @@ int32_t DistributedDeviceProfileProxy::GetBusinessEvent(BusinessEvent& event) } return DP_SUCCESS; } + +int32_t DistributedDeviceProfileProxy::PutServiceInfoProfileNew(const ServiceInfoProfileNew& serviceInfoProfile) +{ + HILOGI("DistributedDeviceProfileProxy::PutServiceInfoProfileNew"); + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!serviceInfoProfile.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::PUT_SERVICE_INFO_PROFILE_NEW), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, + int32_t userId) +{ + HILOGI("DistributedDeviceProfileProxy::DelServiceInfoProfileNew"); + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, Int32, regServiceId); + WRITE_HELPER(data, Bool, isMultiUser); + WRITE_HELPER(data, Int32, userId); + + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::DEL_SERVICE_INFO_PROFILE_NEW), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetServiceInfo(ServiceInfoProfileNew& serviceInfoProfile) +{ + return DP_SUCCESS; +} } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 18ec16e818dc8146e88559b65ea7e50f8229d182..c8ebd3a8a4fdc7d81151ab23dfa4098c1da57726 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -38,6 +38,7 @@ config("device_info_manager_config") { "include/profiledatamanager", "include/publishcommonevent", "include/serviceinfoprofile_manager", + "include/serviceinfo_manager", "include/sessionkeymanager", "include/subscribeprofilemanager", "include/staticcapabilitycollector", @@ -112,6 +113,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/assetadapter/asset_adapter.cpp", "src/persistenceadapter/kvadapter/business_event_adapter.cpp", "src/persistenceadapter/kvadapter/kv_adapter.cpp", + "src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp", "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/local_service_info_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", @@ -123,6 +125,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/profiledatamanager/profile_data_manager.cpp", "src/publishcommonevent/dp_account_common_event.cpp", "src/serviceinfoprofile_manager/service_info_profile_manager.cpp", + "src/serviceinfo_manager/service_info_manager.cpp", "src/sessionkeymanager/session_key_manager.cpp", "src/staticcapabilitycollector/static_capability_collector.cpp", "src/staticcapabilityloader/static_capability_loader.cpp", diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index 270c2559f53730e2c2ecbc21630db13be9d33a97..7e9d13212ce1ec8c0f5e1d9e395d0fd61ea9d5f0 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -43,6 +43,9 @@ extern const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL; extern const std::string CREATE_DEVICE_PROFILE_WISE_DEVICEID_INDEX_SQL; extern const std::string ALTER_TABLE_DP_ADD_COLUMN_PRODUCT_NAME_SQL; extern const std::string ALTER_TABLE_DP_RENAME_COLUMN_INTERNAL_MODEL_SQL; +/* DeviceDao */ +extern const std::string SERVICE_INFO_RDB_PATH; +extern const std::string SERVICE_INFO_DATABASE_NAME; // DeviceIconInfoDao extern const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL ; extern const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL; @@ -72,6 +75,15 @@ extern const std::string SERVICE_INFO_PROFILE_UNIQUE_INDEX_EQUAL_CONDITION; extern const std::string SELECT_SERVICE_INFO_PROFILE_TABLE_WHERE_DEVID_USERID_TOKENID_SERVICEID; extern const std::string SELECT_SERVICE_INFO_PROFILE_TABLE_WHERE_DEVID_USERID_TOKENID; extern const std::string SELECT_SERVICE_INFO_PROFILE_TABLE_WHERE_DEVID_USERID_BUNDLENAME; +// ServiceInfo +extern const std::string SERVICE_INFO_TABLE; +extern const std::string CREATE_SERVICE_INFO_TABLE_SQL; +extern const std::string CREATE_SERVICE_INFO_TABLE_UNIQUE_INDEX_SQL; +extern const std::string SELECT_SERVICE_INFO_ALL; +extern const std::string SERVICE_INFO_UNIQUE_INDEX_EQUAL_CONDITION; +extern const std::string SELECT_SERVICE_INFO_TABLE_WHERE_DEVID_USERID_TOKENID_SERVICEID; +extern const std::string SELECT_SERVICE_INFO_TABLE_WHERE_DEVID_USERID_TOKENID; +extern const std::string SELECT_SERVICE_INFO_TABLE_WHERE_DEVID_USERID_BUNDLENAME; // LocalServiceInfo extern const std::string LOCAL_SERVICE_INFO_RDB_PATH; extern const std::string LOCAL_SERVICE_INFO_DATABASE_NAME; diff --git a/services/core/include/distributed_device_profile_service_new.h b/services/core/include/distributed_device_profile_service_new.h index 5bdd2026c391f369a763d956d294d848dbc22879..052c1aa239cca3b3f69660d3befa77135f1c4de1 100644 --- a/services/core/include/distributed_device_profile_service_new.h +++ b/services/core/include/distributed_device_profile_service_new.h @@ -35,6 +35,7 @@ #include "single_instance.h" #include "system_ability.h" #include "system_ability_definition.h" +#include "service_info_profile_new.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -124,6 +125,10 @@ public: int32_t PutBusinessEvent(const BusinessEvent& event) override; int32_t GetBusinessEvent(BusinessEvent& event) override; + int32_t PutServiceInfoProfileNew(const ServiceInfoProfileNew & serviceInfoProfile) override; + int32_t DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, int32_t userId) override; + int32_t GetServiceInfo(ServiceInfoProfileNew & serviceInfoProfile) override; + protected: void OnStart(const SystemAbilityOnDemandReason& startReason) override; void OnStop() override; diff --git a/services/core/include/distributed_device_profile_stub_new.h b/services/core/include/distributed_device_profile_stub_new.h index c24d67447bfd80e1aed77402e4f9186c56023e4f..f93b9c680c880c31f6af9d26acb46c1ce99558da 100644 --- a/services/core/include/distributed_device_profile_stub_new.h +++ b/services/core/include/distributed_device_profile_stub_new.h @@ -87,6 +87,9 @@ public: int32_t UnRegisterBusinessCallbackInner(MessageParcel& data, MessageParcel& reply); int32_t PutBusinessEventInner(MessageParcel& data, MessageParcel& reply); int32_t GetBusinessEventInner(MessageParcel& data, MessageParcel& reply); + int32_t PutServiceInfoProfileNewInner(MessageParcel& data, MessageParcel& reply); + int32_t DelServiceInfoProfileNewInner(MessageParcel& data, MessageParcel& reply); + int32_t GetServiceInfoProfileNewInner(MessageParcel& data, MessageParcel& reply); private: using Func = int32_t(DistributedDeviceProfileStubNew::*)(MessageParcel& data, MessageParcel& reply); diff --git a/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h b/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..e213b3c829d58d30e3080c5726d02a72acc14891 --- /dev/null +++ b/services/core/include/persistenceadapter/kvadapter/service_info_kv_adapter.h @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef OHOS_DM_SERVICE_KV_ADAPTER_H +#define OHOS_DM_SERVICE_KV_ADAPTER_H + +#include +#include +#include +#include +#include +#include +#include + +#include "distributed_kv_data_manager.h" +#include "ikv_adapter.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceInfoKvAdapter : public DistributedDeviceProfile::IKVAdapter { +public: + ServiceInfoKvAdapter( + const std::shared_ptr &deathListener, DistributedKv::DataType dataType); + + int32_t Init() override; + int32_t UnInit() override; + int32_t ReInit(); + int32_t Put(const std::string& key, const std::string& value) override; + int32_t Get(const std::string& key, std::string& value) override; + int32_t DeleteBatch(const std::vector& keys) override; + int32_t PutBatch(const std::map& values) override; + int32_t Delete(const std::string& key) override; + int32_t DeleteByPrefix(const std::string& keyPrefix) override; + int32_t GetByPrefix(const std::string& keyPrefix, std::map& values) override; + int32_t Sync(const std::vector& deviceList, SyncMode syncMode) override; + int32_t GetDeviceEntries(const std::string& udid, std::map& values) override; + int32_t RemoveDeviceData(const std::string& uuid) override; + void OnRemoteDied(); + +private: + DistributedKv::Status GetKvStorePtr(); + int32_t RegisterKvStoreDeathListener(); + int32_t UnregisterKvStoreDeathListener(); + +private: + DistributedKv::AppId appId_; + DistributedKv::StoreId storeId_; + DistributedKv::DistributedKvDataManager kvDataMgr_; + DistributedKv::DataType dataType_ = DistributedKv::DataType::TYPE_STATICS; + std::shared_ptr kvStorePtr_ = nullptr; + std::shared_ptr deathRecipient_ = nullptr; + + std::mutex serviceInfoAdapterMutex_; + std::atomic isInited_ = false; + std::mutex kvAdapterMutex_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_SERVICE_KV_ADAPTER_H diff --git a/services/core/include/serviceinfo_manager/service_info_manager.h b/services/core/include/serviceinfo_manager/service_info_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..fe0942559a2ecb16cee550632a9e195c0ab84533 --- /dev/null +++ b/services/core/include/serviceinfo_manager/service_info_manager.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DP_SERVICE_INFO_MANAGER_H +#define OHOS_DP_SERVICE_INFO_MANAGER_H + +#include +#include +#include + +#include "service_info_profile_new.h" +#include "service_info_kv_adapter.h" +#include "distributed_device_profile_constants.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceInfoProfileNewManage { +DECLARE_SINGLE_INSTANCE(ServiceInfoProfileNewManage); +public: + int32_t Init(); + int32_t UnInit(); + int32_t ReInit(); + int32_t PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfoProfile); + int32_t DeleteServiceInfoProfile(const int32_t regServiceId, bool isMultiUser = false, + int32_t userId = DEFAULT_USER_ID); + int32_t UpdateServiceInfoProfile(const int32_t regServiceId, const ServiceInfoProfileNew &serviceInfoProfile); + int32_t GetAllServiceInfoProfileList(std::vector& serviceInfoProfileList); + int32_t GetServiceInfoProfileByServiceId(const int64_t &serviceId, ServiceInfoProfileNew &serviceInfoProfile); + int32_t GetServiceInfoProfileByRegServiceId(const int32_t ®ServiceId, ServiceInfoProfileNew &serviceInfoProfile); + + void AddToPutTempCache(const std::map& values); +private: + bool IsValidKey(const std::string& key); + +private: + std::shared_ptr serviceInfoKvAdapter_ = nullptr; + std::mutex dynamicStoreMutex_; + std::atomic isFirst_{true}; + std::map putTempCache_; + //std::shared_ptr deviceProfileStore_ = nullptr; + //std::shared_ptr serviceInfoProfileStore_ = nullptr; + + std::mutex putTempCacheMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_SERVICE_INFO_MANAGER_H + \ No newline at end of file diff --git a/services/core/include/utils/profile_cache.h b/services/core/include/utils/profile_cache.h index 5ca3dbe27b7ee330f562011d6049715e397953e2..773a3bb507d4447705cc2a3708eef2bd8d1ad13a 100644 --- a/services/core/include/utils/profile_cache.h +++ b/services/core/include/utils/profile_cache.h @@ -111,6 +111,7 @@ private: std::mutex serviceProfileMutex_; // The key is profileKey, the value is ServiceProfile std::unordered_map serviceProfileMap_; + std::unordered_map serviceInfoProfileMap_; std::mutex charProfileMutex_; // The key is profileKey, the value is CharacteristicProfile std::unordered_map charProfileMap_; diff --git a/services/core/include/utils/profile_control_utils.h b/services/core/include/utils/profile_control_utils.h index d94d2b1cd89582e9c9bd0533cd763f8e47b304a9..85f0a4baa2c4b7433130491529870420cdf0794f 100644 --- a/services/core/include/utils/profile_control_utils.h +++ b/services/core/include/utils/profile_control_utils.h @@ -52,6 +52,8 @@ public: const std::string& serviceName, const std::string& characteristicKey, CharacteristicProfile& charProfile); static int32_t DeleteServiceProfile(std::shared_ptr kvStore, const std::string& deviceId, const std::string& serviceName, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); + static int32_t DeleteServiceInfoProfile(std::shared_ptr kvStore, const int32_t regServiceId, + bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); static int32_t DeleteCharacteristicProfile(std::shared_ptr kvStore, const std::string& deviceId, const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); diff --git a/services/core/src/deviceprofilemanager/listener/kv_store_death_recipient.cpp b/services/core/src/deviceprofilemanager/listener/kv_store_death_recipient.cpp index cfda0158e5d814790e71a8d2339e9e281054caa7..7384b210d618a1aed7eac95150bd4204a237198a 100644 --- a/services/core/src/deviceprofilemanager/listener/kv_store_death_recipient.cpp +++ b/services/core/src/deviceprofilemanager/listener/kv_store_death_recipient.cpp @@ -21,6 +21,8 @@ #include "distributed_device_profile_constants.h" #include "static_profile_manager.h" +#include "service_info_manager.h" + namespace OHOS { namespace DistributedDeviceProfile { namespace { @@ -29,6 +31,7 @@ namespace { const std::string DYNAMIC_STORE_ID = "dp_kv_store"; const std::string STATIC_STORE_ID = "dp_kv_static_store"; const std::string BUSINESS_STORE_ID = "dp_kv_store_business"; + const std::string SERVICE_INFO_STORE_ID = "dp_kv_store_service_info_profile"; } KvDeathRecipient::KvDeathRecipient(const std::string& storeId) @@ -69,6 +72,9 @@ void KvDeathRecipient::OnRemoteDied() if (storeId == BUSINESS_STORE_ID) { DeviceProfileManager::GetInstance().ReInit(); } + if (storeId == SERVICE_INFO_STORE_ID) { + ServiceInfoProfileNewManage::GetInstance().ReInit(); + } }; { std::lock_guard lock(reInitMutex_); diff --git a/services/core/src/distributed_device_profile_service_new.cpp b/services/core/src/distributed_device_profile_service_new.cpp index 532b0f055533c800b698ebf3adcbdd3ddfba2890..3cef58678af4d14a91cb0e808870cb5321896a4d 100644 --- a/services/core/src/distributed_device_profile_service_new.cpp +++ b/services/core/src/distributed_device_profile_service_new.cpp @@ -42,6 +42,7 @@ #include "profile_cache.h" #include "profile_data_manager.h" #include "service_info_profile_manager.h" +#include "service_info_manager.h" #include "settings_data_manager.h" #include "static_profile_manager.h" #include "static_capability_collector.h" @@ -161,6 +162,11 @@ int32_t DistributedDeviceProfileServiceNew::PostInitNext() HILOGE("BusinessEventManager init failed"); return DP_BUSINESS_EVENT_MANAGER_INIT_FAIL; } + if (ServiceInfoProfileNewManage::GetInstance().Init() != DP_SUCCESS) { + HILOGE("ServiceInfoManager init failed"); + return DP_SERVICE_INFO_PROFILE_MANAGER_INIT_FAIL; + } + #ifndef DEVICE_PROFILE_SWITCH_DISABLE SaveSwitchProfilesFromTempCache(); #endif // DEVICE_PROFILE_SWITCH_DISABLE @@ -289,6 +295,10 @@ int32_t DistributedDeviceProfileServiceNew::UnInitNext() HILOGE("EventHandlerFactory UnInit failed"); return DP_CACHE_UNINIT_FAIL; } + if (ServiceInfoProfileNewManage::GetInstance().UnInit() != DP_SUCCESS) { + HILOGE("ServiceInfoManager uninit failed"); + return DP_SERVICE_INFO_PROFILE_MANAGER_INIT_FAIL; + } return DP_SUCCESS; } @@ -1490,5 +1500,50 @@ int32_t DistributedDeviceProfileServiceNew::NotifyBusinessEvent(const BusinessEv HILOGI("NotifyBusinessEvent task posted successfully"); return DP_SUCCESS; } + +int32_t DistributedDeviceProfileServiceNew::PutServiceInfoProfileNew(const ServiceInfoProfileNew& serviceInfoProfile) +{ + HILOGI("DistributedDeviceProfileServiceNew::PutServiceInfoProfileNew"); + if (!PermissionManager::GetInstance().CheckCallerPermission()) { + HILOGE("the caller is permission denied!"); + return DP_PERMISSION_DENIED; + } + HILOGD("CheckCallerPermission success interface PutServiceInfoProfile"); + int32_t ret = ServiceInfoProfileNewManage::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + if (ret != DP_SUCCESS) { + HILOGE("PutServiceInfoProfile failed, ret: %{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileServiceNew::DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, + int32_t userId) +{ + HILOGI("DistributedDeviceProfileServiceNew::DelServiceInfoProfileNew"); + if (!PermissionManager::GetInstance().CheckCallerPermission()) { + HILOGE("the caller is permission denied!"); + return DP_PERMISSION_DENIED; + } + HILOGD("CheckCallerPermission success interface DelServiceInfoProfile"); + int32_t ret = ServiceInfoProfileNewManage::GetInstance().DeleteServiceInfoProfile(regServiceId, isMultiUser, userId); + if (ret != DP_SUCCESS) { + HILOGE("PutServiceInfoProfile failed, ret: %{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileServiceNew::GetServiceInfo(ServiceInfoProfileNew& serviceInfoProfile) +{ + HILOGI("DistributedDeviceProfileServiceNew::GetServiceInfo"); + // if (!PermissionManager::GetInstance().CheckCallerPermission()) { + // HILOGE("the caller is permission denied!"); + // return DP_PERMISSION_DENIED; + // } + // HILOGD("CheckCallerPermission success interface GetServiceInfo"); + //return ServiceInfoProfileNewManage::GetInstance().GetServiceInfo(event); + return DP_SUCCESS; +} } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/src/distributed_device_profile_stub_new.cpp b/services/core/src/distributed_device_profile_stub_new.cpp index eb60f577c773a58bb7e353c7255dd635e26b3585..11920b851c7db7b2c4dc5a807356097ca5ed4807 100644 --- a/services/core/src/distributed_device_profile_stub_new.cpp +++ b/services/core/src/distributed_device_profile_stub_new.cpp @@ -216,6 +216,12 @@ int32_t DistributedDeviceProfileStubNew::NotifyLocalServiceEventInner(uint32_t c return PutBusinessEventInner(data, reply); case static_cast(DPInterfaceCode::GET_BUSINESS_EVENT): return GetBusinessEventInner(data, reply); + case static_cast(DPInterfaceCode::PUT_SERVICE_INFO_PROFILE_NEW): + return PutServiceInfoProfileNewInner(data, reply); + case static_cast(DPInterfaceCode::DEL_SERVICE_INFO_PROFILE_NEW): + return DelServiceInfoProfileNewInner(data, reply); + case static_cast(DPInterfaceCode::GET_SERVICE_INFO_PROFILE_NEW): + return GetServiceInfoProfileNewInner(data, reply); default: HILOGW("unknown request code, please check, code = %{public}u", code); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -1155,5 +1161,46 @@ int32_t DistributedDeviceProfileStubNew::GetBusinessEventInner(MessageParcel& da } return DP_SUCCESS; } + +int32_t DistributedDeviceProfileStubNew::PutServiceInfoProfileNewInner(MessageParcel& data, MessageParcel& reply) +{ + HILOGI("DistributedDeviceProfileStubNew::PutServiceInfoProfileNewInner"); + ServiceInfoProfileNew serviceInfoProfile; + if (!serviceInfoProfile.UnMarshalling(data)) { + HILOGE("read parcel fail!"); + return DP_READ_PARCEL_FAIL; + } + int32_t ret = PutServiceInfoProfileNew(serviceInfoProfile); + if (!reply.WriteInt32(ret)) { + HILOGE("Write reply failed"); + return DP_WRITE_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileStubNew::DelServiceInfoProfileNewInner(MessageParcel& data, MessageParcel& reply) +{ + HILOGI("DistributedDeviceProfileStubNew::DelServiceInfoProfileNewInner"); + int32_t regServiceId = 0; + bool isMultiUser = false; + int32_t userId = 0; + READ_HELPER(data, Int32, regServiceId); + READ_HELPER(data, Bool, isMultiUser); + READ_HELPER(data, Int32, userId); + + int32_t ret = DelServiceInfoProfileNew(regServiceId, isMultiUser, userId); + if (!reply.WriteInt32(ret)) { + HILOGE("Write reply failed"); + return ERR_FLATTEN_OBJECT; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileStubNew::GetServiceInfoProfileNewInner(MessageParcel& data, MessageParcel& reply) +{ + HILOGI("DistributedDeviceProfileStubNew::GetServiceInfoProfileNewInner"); + + return DP_SUCCESS; +} } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp b/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..080aad9e8f7c8cdc888da4c644f54484761d2725 --- /dev/null +++ b/services/core/src/persistenceadapter/kvadapter/service_info_kv_adapter.cpp @@ -0,0 +1,345 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "service_info_kv_adapter.h" + +#include +#include + +#include "datetime_ex.h" +#include "string_ex.h" +#include "ffrt.h" + +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "distributed_device_profile_constants.h" +#include "profile_cache.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::DistributedKv; +namespace { + constexpr int32_t MAX_INIT_RETRY_TIMES = 30; + constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 200 * 1000; + constexpr uint32_t MAX_BATCH_SIZE = 128; + const std::string DATABASE_DIR = "/data/service/el1/public/database/distributed_device_profile_service"; + const std::string TAG = "ServiceInfoKvAdapter"; + const std::string APP_ID = "distributed_device_profile_service"; + const std::string STORE_ID = "dp_kv_store_service_info_profile"; +} + +ServiceInfoKvAdapter::ServiceInfoKvAdapter( + const std::shared_ptr &deathListener, DistributedKv::DataType dataType) +{ + this->deathRecipient_ = deathListener; + this->dataType_ = dataType; + HILOGD("ServiceInfoKvAdapter Constructor Success"); +} + +int32_t ServiceInfoKvAdapter::Init() +{ + HILOGI("ServiceInfoKvAdapter Init"); + HILOGI("Init local DB, dataType: %{public}d", static_cast(dataType_)); + if (isInited_.load()) { + HILOGI("Local DB already inited."); + return DP_SUCCESS; + } + this->appId_.appId = APP_ID; + this->storeId_.storeId = STORE_ID; + int32_t tryTimes = MAX_INIT_RETRY_TIMES; + int64_t beginTime = GetTickCount(); + while (tryTimes > 0) { + DistributedKv::Status status = GetKvStorePtr(); + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) { + HILOGI("Init KvStorePtr Success"); + RegisterKvStoreDeathListener(); + isInited_.store(true); + return DP_SUCCESS; + } + HILOGI("CheckKvStore, left times: %{public}d, status: %{public}d", tryTimes, status); + usleep(INIT_RETRY_SLEEP_INTERVAL); + tryTimes--; + } + if (kvStorePtr_ == nullptr) { + HILOGE("kvStorePtr is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + isInited_.store(true); + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::UnInit() +{ + HILOGI("ServiceInfoKvAdapter UnInit"); + if (isInited_.load()) { + if (kvStorePtr_ == nullptr) { + HILOGE("kvStorePtr is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + UnregisterKvStoreDeathListener(); + kvStorePtr_.reset(); + isInited_.store(false); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::ReInit() +{ + HILOGI("ServiceInfoKvAdapter ReInit"); + UnInit(); + return Init(); +} + +int32_t ServiceInfoKvAdapter::Put(const std::string& key, const std::string& value) +{ + HILOGI("ServiceInfoKvAdapter::Put"); + if (key.empty() || key.size() > MAX_STRING_LEN || value.empty() || value.size() > MAX_STRING_LEN) { + HILOGE("Param is invalid: key or value is empty or too long"); + return DP_INVALID_PARAMS; + } + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvDBPtr is null!"); + return DP_KV_DB_PTR_NULL; + } + + DistributedKv::Key kvKey(key); + DistributedKv::Value kvValue(value); + status = kvStorePtr_->Put(kvKey, kvValue); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Put kv to db failed, ret: %{public}d", status); + return DP_PUT_KV_DB_FAIL; + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::Get(const std::string& key, std::string& value) +{ + HILOGI("ServiceInfoKvAdapter::Get"); + HILOGI("Get data by key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str()); + DistributedKv::Key kvKey(key); + DistributedKv::Value kvValue; + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvStoragePtr_ is null"); + return DP_KV_DB_PTR_NULL; + } + status = kvStorePtr_->Get(kvKey, kvValue); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Get data from kv failed, key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str()); + return DP_GET_KV_DB_FAIL; + } + value = kvValue.ToString(); + return DP_SUCCESS; +} + +void ServiceInfoKvAdapter::OnRemoteDied() +{ + HILOGI("ServiceInfoKvAdapter::OnRemoteDied"); + HILOGI("OnRemoteDied, recover db begin"); + auto reInitTask = [this]() { + HILOGI("ReInit, storeId:%{public}s", storeId_.storeId.c_str()); + ReInit(); + }; + ffrt::submit(reInitTask); +} + +DistributedKv::Status ServiceInfoKvAdapter::GetKvStorePtr() +{ + HILOGI("ServiceInfoKvAdapter::GetKvStorePtr"); + DistributedKv::Options options = { + .createIfMissing = true, + .encrypt = false, + .autoSync = false, + .securityLevel = DistributedKv::SecurityLevel::S1, + .area = 1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = DATABASE_DIR + }; + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + status = kvDataMgr_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_); + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ == nullptr) { + status = DistributedKv::Status::ERROR; + } + } + return status; +} + +int32_t ServiceInfoKvAdapter::RegisterKvStoreDeathListener() +{ + HILOGI("ServiceInfoKvAdapter::RegisterKvStoreDeathListener"); + HILOGI("Register death listener"); + { + std::lock_guard lock(kvAdapterMutex_); + kvDataMgr_.RegisterKvStoreServiceDeathRecipient(deathRecipient_); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::UnregisterKvStoreDeathListener() +{ + HILOGI("ServiceInfoKvAdapter::UnregisterKvStoreDeathListener"); + HILOGI("UnRegister death listener"); + { + std::lock_guard lock(kvAdapterMutex_); + kvDataMgr_.UnRegisterKvStoreServiceDeathRecipient(deathRecipient_); + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::Delete(const std::string& key) +{ + HILOGI("ServiceInfoKvAdapter::Delete"); + HILOGI("key: %{public}s", ProfileUtils::GetDbKeyAnonyString(key).c_str()); + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvDBPtr is null!"); + return DP_KV_DB_PTR_NULL; + } + DistributedKv::Key kvKey(key); + status = kvStorePtr_->Delete(kvKey); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("Delete kv by key failed!"); + return DP_DEL_KV_DB_FAIL; + } + HILOGD("Delete kv by key success!"); + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::DeleteBatch(const std::vector& keys) +{ + HILOGI("ServiceInfoKvAdapter::DeleteBatch"); + if (keys.empty() || keys.size() > MAX_PROFILE_SIZE) { + HILOGE("keys size(%{public}zu) is invalid!", keys.size()); + return DP_INVALID_PARAMS; + } + + uint32_t keysSize = static_cast(keys.size()); + std::vector> delKeyBatches; + for (uint32_t i = 0; i < keysSize; i += MAX_BATCH_SIZE) { + uint32_t end = (i + MAX_BATCH_SIZE) > keysSize ? keysSize : (i + MAX_BATCH_SIZE); + auto batch = std::vector(keys.begin() + i, keys.begin() + end); + std::vector delKeys; + for (auto item : batch) { + DistributedKv::Key key(item); + delKeys.emplace_back(key); + } + delKeyBatches.emplace_back(delKeys); + } + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvStorePtr is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + for (auto delKeys : delKeyBatches) { + DistributedKv::Status status = kvStorePtr_->DeleteBatch(delKeys); + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("DeleteBatch failed!"); + return DP_DEL_KV_DB_FAIL; + } + } + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::PutBatch(const std::map& values) +{ + HILOGI("ServiceInfoKvAdapter::PutBatch"); + if (values.empty() || values.size() > MAX_PROFILE_SIZE) { + HILOGE("Param is invalid!"); + return DP_INVALID_PARAMS; + } + DistributedKv::Status status; + { + std::lock_guard lock(kvAdapterMutex_); + if (kvStorePtr_ == nullptr) { + HILOGE("kvDBPtr is null!"); + return DP_KV_DB_PTR_NULL; + } + std::vector entries; + DistributedKv::Value oldV; + DistributedKv::Key kvKey; + for (auto item : values) { + kvKey = item.first; + if (kvStorePtr_->Get(kvKey, oldV) == DistributedKv::Status::SUCCESS && oldV.ToString() == item.second) { + HILOGI("The key-value pair already exists. key=%{public}s,value=%{public}s", + ProfileUtils::GetDbKeyAnonyString(item.first).c_str(), + ProfileUtils::GetAnonyString(item.second).c_str()); + continue; + } + + Entry entry; + entry.key = kvKey; + entry.value = item.second; + entries.emplace_back(entry); + } + if (entries.empty()) { + HILOGD("All key-value pair already exists."); + return DP_SUCCESS; + } + status = kvStorePtr_->PutBatch(entries); + } + if (status != DistributedKv::Status::SUCCESS) { + HILOGE("PutBatch kv to db failed, ret: %{public}d", status); + return DP_PUT_KV_DB_FAIL; + } + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::GetByPrefix(const std::string& keyPrefix, std::map& values) +{ + (void)keyPrefix; + (void)values; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::DeleteByPrefix(const std::string& keyPrefix) +{ + (void)keyPrefix; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::Sync(const std::vector& deviceList, SyncMode syncMode) +{ + (void)deviceList; + (void)syncMode; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::GetDeviceEntries(const std::string& udid, std::map& values) +{ + (void)udid; + (void)values; + return DP_SUCCESS; +} + +int32_t ServiceInfoKvAdapter::RemoveDeviceData(const std::string& uuid) +{ + (void)uuid; + return DP_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/core/src/serviceinfo_manager/service_info_manager.cpp b/services/core/src/serviceinfo_manager/service_info_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e4dab0aea27eeeae200ae2b037ba2c01dfdb7b6 --- /dev/null +++ b/services/core/src/serviceinfo_manager/service_info_manager.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "service_info_manager.h" +#include "profile_utils.h" + +#include "service_info_profile_new.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "dp_services_constants.h" +#include "kv_data_change_listener.h" +#include "kv_store_death_recipient.h" +#include "kv_sync_completed_listener.h" +#include "profile_control_utils.h" + +#include "multi_user_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ServiceInfoProfileNewManage) + +namespace { + const std::string DP_REJECT_KEY = "business_id_cast+_reject_event"; + const std::string DP_DISTURBANCE_KEY = "business_id_cast+_disturbance_event"; + const std::string STORE_ID = "dp_kv_store_service_info_profile"; + const std::string TAG = "ServiceInfoProfileNewManage"; + const std::set validKeys_ = { DP_REJECT_KEY, DP_DISTURBANCE_KEY }; +} + +int32_t ServiceInfoProfileNewManage::Init() +{ + HILOGI("call!"); + std::lock_guard lock(dynamicStoreMutex_); + serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + int32_t ret = serviceInfoKvAdapter_->Init(); + if (ret != DP_SUCCESS) { + HILOGE("businessEventAdapter init failed, ret: %{public}d", ret); + return DP_INIT_DB_FAILED; + } + HILOGI("Init finish, ret: %{public}d", ret); + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::UnInit() +{ + HILOGI("call!"); + { + std::lock_guard lock(dynamicStoreMutex_); + if (serviceInfoKvAdapter_ == nullptr) { + HILOGE("serviceInfoKvAdapter_ is nullptr"); + return DP_KV_DB_PTR_NULL; + } + serviceInfoKvAdapter_->UnInit(); + serviceInfoKvAdapter_ = nullptr; + } + HILOGI("UnInit success"); + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::ReInit() +{ + HILOGI("call!"); + UnInit(); + return Init(); +} + +int32_t ServiceInfoProfileNewManage::PutServiceInfoProfile(const ServiceInfoProfileNew& serviceInfoProfile) +{ + HILOGI("ServiceInfoProfileNewManage::PutServiceInfoProfile"); + int32_t userId = serviceInfoProfile.GetUserId(); + HILOGI("serviceInfoProfile: %{public}s", serviceInfoProfile.dump().c_str()); + if (!ProfileUtils::IsSerInfoProfileValid(serviceInfoProfile)) { + HILOGE("the profile is invalid!"); + return DP_INVALID_PARAMS; + } + std::map entries; + if (serviceInfoProfile.GetIsMultiUser()) { + ProfileUtils::ServiceInfoProfileToEntries(serviceInfoProfile, entries, true); + if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == userId) { + ProfileUtils::ServiceInfoProfileToEntries(serviceInfoProfile, entries); + } else { + HILOGI("the profile does not belong to the current user."); + } + } else { + ProfileUtils::ServiceInfoProfileToEntries(serviceInfoProfile, entries); + } + // if (isFirst_.load()) { + // AddToPutTempCache(entries); + // return DP_SUCCESS; + // } + { + std::lock_guard lock(dynamicStoreMutex_); + if (serviceInfoKvAdapter_ == nullptr) { + HILOGE("deviceProfileStore is nullptr!"); + return DP_KV_DB_PTR_NULL; + } + if (serviceInfoKvAdapter_->PutBatch(entries) != DP_SUCCESS) { + HILOGE("PutServiceProfile fail! serviceInfoProfile: %{public}s", serviceInfoProfile.dump().c_str()); + return DP_PUT_KV_DB_FAIL; + } + } + HILOGD("PutServiceProfile success"); + + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::DeleteServiceInfoProfile(const int32_t regServiceId, bool isMultiUser, + int32_t userId) +{ + HILOGD("ServiceInfoProfileNewManage::DeleteServiceInfoProfile"); + int32_t res = 0; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::DeleteServiceInfoProfile(serviceInfoKvAdapter_, regServiceId, isMultiUser, + userId); + } + if (res != DP_SUCCESS) { + HILOGE("DeleteServiceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGD("DeleteServiceProfile success"); + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::UpdateServiceInfoProfile(const int32_t regServiceId, const ServiceInfoProfileNew &serviceInfoProfile) +{ + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::GetAllServiceInfoProfileList(std::vector& serviceInfoProfileList) +{ + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::GetServiceInfoProfileByServiceId(const int64_t &serviceId, ServiceInfoProfileNew &serviceInfoProfile) +{ + return DP_SUCCESS; +} + +int32_t ServiceInfoProfileNewManage::GetServiceInfoProfileByRegServiceId(const int32_t ®ServiceId, ServiceInfoProfileNew &serviceInfoProfile) +{ + return DP_SUCCESS; +} + +// int32_t ServiceInfoProfileNewManage::GetServiceInfo(ServiceInfoProfileNew& serviceInfo) +// { +// if (serviceInfo.GetServiceIdKey().empty()) { +// HILOGE("Invalid parameter: BusinessKey is empty"); +// return DP_INVALID_PARAM; +// } +// if (!IsValidKey(serviceInfo.GetServiceIdKey())) { +// HILOGE("Invalid BusinessKey: %{public}s", serviceInfo.GetServiceIdKey().c_str()); +// return DP_INVALID_PARAM; +// } + +// std::string key = serviceInfo.GetServiceIdKey(); +// std::string value; +// { +// std::lock_guard lock(dynamicStoreMutex_); +// if (serviceInfoKvAdapter_ == nullptr) { +// HILOGE("serviceInfoKvAdapter_ is null"); +// return DP_KV_DB_PTR_NULL; +// } +// int32_t ret = serviceInfoKvAdapter_->Get(key, value); +// if (ret != DP_SUCCESS) { +// HILOGE("Failed to get business serviceInfo with key: %{public}s", key.c_str()); +// return DP_GET_BUSINESS_EVENT_FAIL; +// } +// serviceInfo.SetServiceIdValue(value); +// } +// HILOGI("GetBusinessEvent success for key: %{public}s", key.c_str()); +// return DP_SUCCESS; +// } + +bool ServiceInfoProfileNewManage::IsValidKey(const std::string& key) +{ + return validKeys_.find(key) != validKeys_.end(); +} + +void ServiceInfoProfileNewManage::AddToPutTempCache(const std::map& values) +{ + if (values.empty()) { + HILOGW("values empty!"); + return; + } + HILOGI("values.size : %{public}zu", values.size()); + { + std::lock_guard lock(putTempCacheMutex_); + for (const auto& [key, value] : values) { + putTempCache_[key] = value; + } + } +} +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/src/utils/profile_cache.cpp b/services/core/src/utils/profile_cache.cpp index 78625bbd937ee074b7530035f66c89ce50a50653..0307a68e9e761e3aeae200b1d505b04e76e873ec 100644 --- a/services/core/src/utils/profile_cache.cpp +++ b/services/core/src/utils/profile_cache.cpp @@ -66,6 +66,7 @@ int32_t ProfileCache::UnInit() { std::lock_guard lock(serviceProfileMutex_); serviceProfileMap_.clear(); + serviceInfoProfileMap_.clear(); } { std::lock_guard lock(charProfileMutex_); diff --git a/services/core/src/utils/profile_control_utils.cpp b/services/core/src/utils/profile_control_utils.cpp index e2677544718a07058dcdb41642ff18251684e532..0e76d59815437a2e17fb37aafc5efbfcb57e0f73 100644 --- a/services/core/src/utils/profile_control_utils.cpp +++ b/services/core/src/utils/profile_control_utils.cpp @@ -453,6 +453,33 @@ int32_t ProfileControlUtils::DeleteServiceProfile(std::shared_ptr kv return DP_SUCCESS; } +int32_t ProfileControlUtils::DeleteServiceInfoProfile(std::shared_ptr kvStore, const int32_t regServiceId, + bool isMultiUser, int32_t userId) +{ + HILOGD("call!"); + if (kvStore == nullptr) { + HILOGE("kvStore is nullptr!"); + return DP_INVALID_PARAMS; + } + std::string strRegServiceId = std::to_string(regServiceId); + std::vector keys; + if (isMultiUser) { + ProfileUtils::GenerateServiceInfoProfilekeys(strRegServiceId, keys, true, userId); + if (MultiUserManager::GetInstance().GetCurrentForegroundUserID() == userId) { + ProfileUtils::GenerateServiceInfoProfilekeys(strRegServiceId, keys); + } else { + HILOGI("the profile does not belong to the current user."); + } + } else { + ProfileUtils::GenerateServiceInfoProfilekeys(strRegServiceId, keys); + } + if (kvStore->DeleteBatch(keys) != DP_SUCCESS) { + HILOGE("DeleteServiceProfile fail!"); + return DP_DEL_KV_DB_FAIL; + } + return DP_SUCCESS; +} + int32_t ProfileControlUtils::DeleteCharacteristicProfile(std::shared_ptr kvStore, const std::string& deviceId, const std::string& serviceName, const std::string& characteristicKey, bool isMultiUser, int32_t userId) diff --git a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp index 7bd48b7328c6b1f45e5f6fb843792d7a6d45a3d6..e4f2e5921d35f2b5dfbd33f01faa4afc992fc461 100644 --- a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp @@ -100,6 +100,10 @@ class MockDistributedDeviceProfileStubNew : public DistributedDeviceProfileStubN int32_t UnRegisterBusinessCallback(const std::string& saId, const std::string& businessKey) override; int32_t PutBusinessEvent(const BusinessEvent &event) override; int32_t GetBusinessEvent(BusinessEvent &event) override; + int32_t PutServiceInfoProfileNew(const ServiceInfoProfileNew & serviceInfo) override; + int32_t DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, int32_t userId) override; + int32_t GetServiceInfo(ServiceInfoProfileNew & serviceInfo) override; + void DelayUnloadTask() override; bool IsInited() override; }; @@ -480,6 +484,27 @@ int32_t MockDistributedDeviceProfileStubNew::GetBusinessEvent(BusinessEvent& eve (void)event; return 0; } + +int32_t MockDistributedDeviceProfileStubNew::PutServiceInfoProfileNew(const ServiceInfoProfileNew& serviceInfo) +{ + (void)serviceInfo; + return 0; +} + +int32_t MockDistributedDeviceProfileStubNew::DelServiceInfoProfileNew(const int32_t regServiceId, bool isMultiUser, + int32_t userId) +{ + (void)regServiceId; + (void)isMultiUser; + (void)userId; + return 0; +} + +int32_t MockDistributedDeviceProfileStubNew::GetServiceInfo(ServiceInfoProfileNew& serviceInfo) +{ + (void)serviceInfo; + return 0; +} /** * @tc.name: IsInterfaceTokenValid001 * @tc.desc: IsInterfaceTokenValid