From 5cbd41d6ad96d3cd4b34d5c2f83786240d8c1ca2 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 31 Dec 2024 02:44:18 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E4=BA=91characteristic?= =?UTF-8?q?=20dao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: torrizo --- .../distributed_device_profile_constants.h | 2 + .../distributed_device_profile_errors.h | 5 + .../interfaces/characteristic_profile.h | 33 +- .../characteristic_profile_filter_option.h | 77 +++++ .../distributed_device_profile_constants.cpp | 2 + .../src/interfaces/characteristic_profile.cpp | 54 +++ .../characteristic_profile_filter_option.cpp | 144 ++++++++ .../characteristic_profile_dao.h | 59 ++++ .../characteristic_profile_dao.cpp | 315 ++++++++++++++++++ 9 files changed, 690 insertions(+), 1 deletion(-) create mode 100644 common/include/interfaces/characteristic_profile_filter_option.h create mode 100644 common/src/interfaces/characteristic_profile_filter_option.cpp create mode 100644 services/core/include/profiledatamanager/characteristic_profile_dao.h create mode 100644 services/core/src/profiledatamanager/characteristic_profile_dao.cpp diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 289ebc55..5f1f329c 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -47,6 +47,8 @@ extern const std::string OH_PROFILE_SUFFIX; extern const std::string SERVICE_NAME; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ +extern const std::string SERVICE_PROFILE_ID; +extern const std::string CHARACTERISTIC_PROFILE_TABLE; extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; /* TrustDeviceProfile Attribute */ diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 86fa4d08..d853a907 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -160,6 +160,11 @@ constexpr int32_t DP_GET_USER_ID_IS_NOT_TRUSTED = 98566280; constexpr int32_t DP_MULTI_USER_MANAGER_INIT_FAIL = 98566281; constexpr int32_t DP_MULTI_USER_MANAGER_UNINIT_FAIL = 98566282; constexpr int32_t DP_DM_ADAPTER_UNINIT_FAIL = 98566283; + +constexpr int32_t DP_PUT_CHARACTERISTIC_PROFILE_FAIL = 98566298; +constexpr int32_t DP_GET_CHARACTERISTIC_PROFILE_FAIL = 98566299; +constexpr int32_t DP_DELETE_CHARACTERISTIC_PROFILE_FAIL = 98566300; +constexpr int32_t DP_UPDATE_CHARACTERISTIC_PROFILE_FAIL = 98566301; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 57523ba0..474bd55b 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 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 @@ -42,6 +42,27 @@ public: isMultiUser_(isMultiUser), userId_(userId) {} + CharacteristicProfile(const std::string& deviceId, + const std::string& characteristicKey, const std::string& characteristicValue, + const int32_t id, const int32_t serviceProfileId, const std::string& serviceId) + : deviceId_(deviceId), + characteristicKey_(characteristicKey), + characteristicValue_(characteristicValue), + id_(id), + serviceProfileId_(serviceProfileId), + serviceId_(serviceId) + {} + CharacteristicProfile(const CharacteristicProfile& other) + : deviceId_(other.deviceId_), + serviceName_(other.serviceName_), + characteristicKey_(other.characteristicKey_), + characteristicValue_(other.characteristicValue_), + isMultiUser_(other.isMultiUser_), + userId_(other.userId_), + id_(other.id_), + serviceProfileId_(other.serviceProfileId_), + serviceId_(other.serviceId_) + {} CharacteristicProfile() = default; ~CharacteristicProfile() = default; @@ -57,9 +78,16 @@ public: void SetIsMultiUser(bool isMultiUser); int32_t GetUserId() const; void SetUserId(int32_t userId); + int32_t GetId() const; + void SetId(int32_t id); + int32_t GetServiceProfileId() const; + void SetServiceProfileId(int32_t serviceProfileId); + std::string GetServiceId() const; + void SetServiceId(const std::string& serviceId); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const CharacteristicProfile& charProfile) const; + CharacteristicProfile& operator=(const CharacteristicProfile& other); std::string dump() const override; private: @@ -69,6 +97,9 @@ private: std::string characteristicValue_ = ""; bool isMultiUser_ = false; int32_t userId_ = DEFAULT_USER_ID; + int32_t id_ = CHANGEROWCNT_INIT_ID; + int32_t serviceProfileId_ = DEFAULT_SERVICE_PROFILE_ID; + std::string serviceId_ = ""; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/characteristic_profile_filter_option.h b/common/include/interfaces/characteristic_profile_filter_option.h new file mode 100644 index 00000000..54a62bad --- /dev/null +++ b/common/include/interfaces/characteristic_profile_filter_option.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 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_CHARACTERISTIC_PROFILE_FILTER_OPTION_H +#define OHOS_DP_CHARACTERISTIC_PROFILE_FILTER_OPTION_H + +#include +#include +#include +#include "distributed_device_profile_constants.h" +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class CharacteristicProfileFilterOption : public DpParcel { +public: + CharacteristicProfileFilterOption(const int32_t userId, const std::string& accountld, + const std::string& deviceId, const std::string& wiseDeviceId, + const std::string& serviceId, const std::string& characteristicKey, + const std::vector& characteristicProfileIds, const int32_t serviceProfileId) + : userId_(userId), + accountld_(accountld), + deviceId_(deviceId), + wiseDeviceId_(wiseDeviceId), + serviceId_(serviceId), + characteristicKey_(characteristicKey), + characteristicProfileIds_(characteristicProfileIds), + serviceProfileId_(serviceProfileId) + {} + CharacteristicProfileFilterOption() = default; + ~CharacteristicProfileFilterOption() = default; + + std::string GetAccountld() const; + void SetAccountld(const std::string& accountld); + std::string GetServiceId() const; + void SetServiceId(const std::string& serviceId); + std::string GetWiseDeviceId() const; + void SetWiseDeviceId(const std::string& wiseDeviceId); + int32_t GetUserId() const; + void SetUserId(int32_t userId); + std::vector GetCharacteristicProfileIds() const; + void SetCharacteristicProfileIds(std::vector& characteristicProfileIds); + std::string GetCharacteristicKey() const; + void SetCharacteristicKey(std::string& characteristicKey); + std::string GetDeviceId() const; + void SetDeviceId(std::string& deviceId); + int32_t GetServiceProfileId() const; + void SetServiceProfileId(int32_t serviceProfileId); + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + int32_t userId_ = DEFAULT_USER_ID; + std::string accountld_ = ""; + std::string deviceId_ = ""; + std::string wiseDeviceId_ = ""; + std::string serviceId_ = ""; + std::string characteristicKey_ = ""; + std::vector characteristicProfileIds_; + int32_t serviceProfileId_ = DEFAULT_SERVICE_PROFILE_ID; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_CHARACTERISTIC_PROFILE_FILTER_OPTION_H diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index 7414d4bb..b3a9996d 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -37,6 +37,8 @@ const std::string OH_PROFILE_SUFFIX = "_OH"; const std::string SERVICE_NAME = "serviceName"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ +const std::string SERVICE_PROFILE_ID = "serviceProfileId"; +const std::string CHARACTERISTIC_PROFILE_TABLE = "characteristic_profile"; const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; /* TrustDeviceProfile Attribute */ diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index 13123602..b3ec27f9 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -64,6 +64,16 @@ void CharacteristicProfile::SetCharacteristicValue(const std::string& characteri characteristicValue_ = characteristicValue; } +std::string CharacteristicProfile::GetServiceId() const +{ + return serviceId_; +} + +void CharacteristicProfile::SetServiceId(const std::string& serviceId) +{ + serviceId_ = serviceId; +} + bool CharacteristicProfile::IsMultiUser() const { return isMultiUser_; @@ -84,6 +94,26 @@ void CharacteristicProfile::SetUserId(int32_t userId) userId_ = userId; } +int32_t CharacteristicProfile::GetId() const +{ + return id_; +} + +void CharacteristicProfile::SetId(int32_t id) +{ + id_ = id; +} + +int32_t CharacteristicProfile::GetServiceProfileId() const +{ + return serviceProfileId_; +} + +void CharacteristicProfile::SetServiceProfileId(int32_t serviceProfileId) +{ + serviceProfileId_ = serviceProfileId; +} + bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, deviceId_, false); @@ -92,6 +122,9 @@ bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, characteristicValue_, false); WRITE_HELPER_RET(parcel, Bool, isMultiUser_, false); WRITE_HELPER_RET(parcel, Int32, userId_, false); + WRITE_HELPER_RET(parcel, Int32, id_, false); + WRITE_HELPER_RET(parcel, Int32, serviceProfileId_, false); + WRITE_HELPER_RET(parcel, String, serviceId_, false); return true; } @@ -103,6 +136,9 @@ bool CharacteristicProfile::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, characteristicValue_, false); READ_HELPER_RET(parcel, Bool, isMultiUser_, false); READ_HELPER_RET(parcel, Int32, userId_, false); + READ_HELPER_RET(parcel, Int32, id_, false); + READ_HELPER_RET(parcel, Int32, serviceProfileId_, false); + READ_HELPER_RET(parcel, String, serviceId_, false); return true; } @@ -119,6 +155,24 @@ bool CharacteristicProfile::operator!=(const CharacteristicProfile& charProfile) } } +CharacteristicProfile& CharacteristicProfile::operator!=(const CharacteristicProfile& other) +{ + if (this == &other) { + return *this; + } + + deviceId_ = other.deviceId_; + serviceName_ = other.serviceName_; + characteristicKey_ = other.characteristicKey_; + characteristicValue_ = other.characteristicValue_; + isMultiUser_ = other.isMultiUser_; + userId_ = other.userId_; + id_ = other.id_; + serviceProfileId_ = other.serviceProfileId_; + serviceId_ = other.serviceId_; + return *this; +} + std::string CharacteristicProfile::dump() const { cJSON* json = cJSON_CreateObject(); diff --git a/common/src/interfaces/characteristic_profile_filter_option.cpp b/common/src/interfaces/characteristic_profile_filter_option.cpp new file mode 100644 index 00000000..c6daf6a5 --- /dev/null +++ b/common/src/interfaces/characteristic_profile_filter_option.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2023-2024 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 "characteristic_profile_filter_option.h" +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "ipc_utils.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "CharacteristicProfile"; +} +std::string CharacteristicProfileFilterOption::GetAccountld() const +{ + return accountld_; +} + +void CharacteristicProfileFilterOption::SetAccountld(const std::string& accountld) +{ + accountld_ = accountld; +} + +std::string CharacteristicProfileFilterOption::GetServiceId() const +{ + return serviceId_; +} + +void CharacteristicProfileFilterOption::SetServiceId(const std::string& serviceId) +{ + serviceId_ = serviceId; +} + +std::string CharacteristicProfileFilterOption::GetWiseDeviceId() const +{ + return wiseDeviceId_; +} + +void CharacteristicProfileFilterOption::SetWiseDeviceId(const std::string& wiseDeviceId) +{ + wiseDeviceId_ = wiseDeviceId; +} + +int32_t CharacteristicProfileFilterOption::GetUserId() const +{ + return userId_; +} + +void CharacteristicProfileFilterOption::SetUserId(int32_t userId) +{ + userId_ = userId; +} + +std::vector CharacteristicProfileFilterOption::GetCharacteristicProfileIds() const +{ + return characteristicProfileIds_; +} + +void CharacteristicProfileFilterOption::SetCharacteristicProfileIds(std::vector& characteristicProfileIds) +{ + characteristicProfileIds_ = characteristicProfileIds; +} + +std::string CharacteristicProfileFilterOption::GetCharacteristicKey() const +{ + return characteristicKey_; +} + +void CharacteristicProfileFilterOption::SetCharacteristicKey(std::string& characteristicKey) +{ + characteristicKey_ = characteristicKey; +} + +std::string CharacteristicProfileFilterOption::GetDeviceId() const +{ + return deviceId_; +} + +void CharacteristicProfileFilterOption::SetDeviceId(std::string& deviceId) +{ + deviceId_ = deviceId; +} + +int32_t CharacteristicProfileFilterOption::GetServiceProfileId() const +{ + return serviceProfileId_; +} + +void CharacteristicProfileFilterOption::SetServiceProfileId(int32_t serviceProfileId) +{ + serviceProfileId_ = serviceProfileId; +} + +bool CharacteristicProfileFilterOption::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER_RET(parcel, Int32, userId_, false); + WRITE_HELPER_RET(parcel, String, accountld_, false); + WRITE_HELPER_RET(parcel, String, deviceId_, false); + WRITE_HELPER_RET(parcel, String, wiseDeviceId_, false); + WRITE_HELPER_RET(parcel, String, serviceId_, false); + WRITE_HELPER_RET(parcel, String, characteristicKey_, false); + WRITE_HELPER_RET(parcel, Int32, serviceProfileId_, false); + if (!IpcUtils::Marshalling(parcel, characteristicProfileIds_)) { + HILOGE("read parcel fail!"); + return false; + } + return true; +} + +bool CharacteristicProfileFilterOption::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, Int32, userId_, false); + READ_HELPER_RET(parcel, String, accountld_, false); + READ_HELPER_RET(parcel, String, deviceId_, false); + READ_HELPER_RET(parcel, String, wiseDeviceId_, false); + READ_HELPER_RET(parcel, String, serviceId_, false); + READ_HELPER_RET(parcel, String, characteristicKey_, false); + READ_HELPER_RET(parcel, Int32, serviceProfileId_, false); + if (!IpcUtils::UnMarshalling(parcel, characteristicProfileIds_)) { + HILOGE("read parcel fail!"); + return false; + } + return true; +} + +std::string CharacteristicProfileFilterOption::dump() const +{ + return ""; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/include/profiledatamanager/characteristic_profile_dao.h b/services/core/include/profiledatamanager/characteristic_profile_dao.h new file mode 100644 index 00000000..491cca76 --- /dev/null +++ b/services/core/include/profiledatamanager/characteristic_profile_dao.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024 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_CHARACTERISTIC_PROFILE_DAO_H +#define OHOS_DP_CHARACTERISTIC_PROFILE_DAO_H + + +#include +#include +#include +#include +#include + +#include "characteristic_profile.h" +#include "characteristic_profile_filter_option.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class CharacteristicProfileDao { + DECLARE_SINGLE_INSTANCE(CharacteristicProfileDao); +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutCharacteristicProfile(CharacteristicProfile& charProfile); + int32_t GetCharacteristicProfiles(const CharacteristicProfileFilterOption& filterOptions, + std::vector& deviceProfiles); + int32_t DeleteCharacteristicProfile(const CharacteristicProfile& charProfile); + int32_t UpdateCharacteristicProfile(const CharacteristicProfile& oldProfile, + const CharacteristicProfile& newProfile); + +private: + void CreateQuerySqlAndCondition(const CharacteristicProfileFilterOption& filterOptions, + std::string& sql, std::vector& condition); + int32_t CharProfileToEntries(const CharacteristicProfile& charProfile, ValuesBucket& values); + int32_t ConvertToCharProfile(std::shared_ptr resultSet, CharacteristicProfile& charProfile); + int32_t CreateTable(); + int32_t CreateIndex(); + int32_t SetCharacteristicProfileId(CharacteristicProfile& charProfile); +}; +} +} +#endif // OHOS_DP_CHARACTERISTIC_PROFILE_DAO_H diff --git a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp new file mode 100644 index 00000000..17fc4c0f --- /dev/null +++ b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2024 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 "dp_services_constants.h" +#include "content_sensor_manager_utils.h" +#include "characteristic_profile.h" +#include "characteristic_profile_filter_option.h" +#include "characteristic_profile_dao.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "device_profile_manager.h" +#include "profile_utils.h" +#include "profile_cache.h" +#include "subscribe_profile_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(CharacteristicProfileDao); +namespace { + const std::string TAG = "CharacteristicProfileDao"; +} + +int32_t CharacteristicProfileDao::Init() +{ + int32_t ret; + if (!ProfileDataRdbAdapter::GetInstance().IsInit()) { + ret = ProfileDataRdbAdapter::GetInstance().Init(); + if (ret != DP_SUCCESS) { + HILOGE("ProfileDataRdbAdapter Init failed"); + return DP_INIT_DB_FAILED; + } + } + CreateTable(); + CreateIndex(); + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::UnInit() +{ + if (!ProfileDataRdbAdapter::GetInstance().IsInit()) { + HILOGE("ProfileDataRdbAdapter is UnInit"); + return DP_SUCCESS; + } + int32_t ret = ProfileDataRdbAdapter::GetInstance().UnInit(); + if (ret != DP_SUCCESS) { + HILOGE("ProfileDataRdbAdapter UnInit failed"); + return DP_UNINIT_FAIL; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::PutCharacteristicProfile(CharacteristicProfile& charProfile) +{ + ValuesBucket values; + int32_t result = CharProfileToEntries(charProfile, values); + if (result != DP_SUCCESS) { + HILOGE("charProfile invalid params"); + return DP_INVALID_PARAMS; + } + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, CHARACTERISTIC_PROFILE_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("characteristic_profile insert failed"); + return DP_PUT_CHARACTERISTIC_PROFILE_FAIL; + } + if (SetCharacteristicProfileId(charProfile) != DP_SUCCESS) { + HILOGE("SetCharacteristicProfileId fail"); + return DP_PUT_CHARACTERISTIC_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyCharProfileAdd(charProfile); + std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); + if (localUdid == charProfile.GetDeviceId()) { + DeviceProfileManager::GetInstance().PutCharacteristicProfile(charProfile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::GetCharacteristicProfiles(const CharacteristicProfileFilterOption &filterOptions, + std::vector &charProfiles) +{ + std::string sql; + std::vector condition; + if (filterOptions.GetUserId() <= 0 && filterOptions.GetAccountld().empty() && + filterOptions.GetDeviceIds().empty() && filterOptions.GetWiseDeviceId() .empty() && + filterOptions.GetServiceId().empty() && filterOptions.GetCharacteristicKey().empty() && + filterOptions.GetCharacteristicProfileIds().empty() && filterOptions.GetServiceProfileId() <= 0) { + HILOGE("filterOptions is empty"); + return DP_INVALID_PARAMS; + } + CreateQuerySqlAndCondition(filterOptions, sql, condition); + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_CHARACTERISTIC_PROFILE_FAIL; + } + int32_t rowCount = ROWCOUNT_INIT; + resultSet->GetRowCount(rowCount); + if (rowCount == 0) { + HILOGE("by condition not find data"); + resultSet->Close(); + return DP_NOT_FIND_DATA; + } + while (resultSet->GoToNextRow() == DP_SUCCESS) { + CharacteristicProfile charProfile; + ConvertToCharProfile(resultSet, charProfile); + charProfiles.push_back(charProfile); + } + resultSet->Close(); + if (charProfiles.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::DeleteCharacteristicProfile(const CharacteristicProfile &charProfiles) +{ + if (charProfiles.GetDeviceId().empty() || charProfiles.GetServiceProfileId() <= 0 || + charProfiles.GetServiceId().empty() || charProfiles.GetCharacteristicKey().empty() || + charProfiles.GetCharacteristicValue().empty()) { + HILOGE("charProfile invalid params"); + return DP_INVALID_PARAMS; + } + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, CHARACTERISTIC_PROFILE_TABLE, + ID_EQUAL_CONDITION, std::vector{ ValueObject(charProfiles.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete characteristicprofile_profile data failed"); + return DP_DELETE_CHARACTERISTIC_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyCharProfileDelete(charProfiles); + std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); + if (localUdid == charProfiles.GetDeviceId()) { + DeviceProfileManager::GetInstance().DeleteCharacteristicProfile(charProfiles.GetDeviceId(), + charProfiles.GetServiceName(), charProfiles.GetCharacteristicKey(), true, charProfiles.GetUserId()); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::UpdateCharacteristicProfile(const CharacteristicProfile &oldProfile, + const CharacteristicProfile &newProfile) +{ + ValuesBucket values; + int32_t result = CharProfileToEntries(newProfile, values); + if (result != DP_SUCCESS) { + HILOGE("charProfile invalid params"); + return DP_INVALID_PARAMS; + } + int32_t changeRowCnt = CHANGEROWCNT_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update( + changeRowCnt, CHARACTERISTIC_PROFILE_TABLE, values, ID_EQUAL_CONDITION, + std::vector{ ValueObject(newProfile.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update characteristicprofile_profile table failed"); + return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyCharProfileUpdate(oldProfile, newProfile); + std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); + if (localUdid == newProfile.GetDeviceId()) { + DeviceProfileManager::GetInstance().PutCharacteristicProfile(newProfile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::CreateTable() +{ + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("characteristicprofile_profile create failed"); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::CreateIndex() +{ + int32_t ret = ProfileDataRdbAdapter::GetInstance(). + CreateTable(CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_CHARACTERISTIC_KEY); + if (ret != DP_SUCCESS) { + HILOGE("characteristicprofile_profile unique index create failed"); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + ret = ProfileDataRdbAdapter::GetInstance(). + CreateTable(CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_SERVICE_PROFILE_ID); + if (ret != DP_SUCCESS) { + HILOGE("characteristicprofile_profile unique index create failed"); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + +void CharacteristicProfileDao::CreateQuerySqlAndCondition(const CharacteristicProfileFilterOption& filterOptions, + std::string& sql, std::vector& condition) +{ + sql = SELECT_CHARACTERISTIC_PROFILE_TABLE; + if (!filterOptions.GetCharacteristicProfileIds().empty()) { + sql += "cp.id IN("; + std::vector characteristicProfileIds = filterOptions.GetCharacteristicProfileIds(); + for (auto deviceProfileId : characteristicProfileIds) { + sql += "?,"; + condition.emplace_back(ValueObject(deviceProfileId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + } + if (!filterOptions.GetCharacteristicKey().empty()) { + sql += "cp.characteristicKey = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetCharacteristicKey())); + } + if (filterOptions.GetServiceProfileId() != 0) { + sql += "cp.serviceProfileId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetServiceProfileId())); + } + if (!filterOptions.GetServiceId().empty()) { + sql += "sp.serviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetServiceId())); + } + if (!filterOptions.GetDeviceIds().empty()) { + sql += "sp.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetDeviceIds())); + } + if (filterOptions.GetUserId() != 0) { + sql += "dp.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + } + if (!filterOptions.GetAccountld().empty()) { + sql += "dp.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountld())); + } + if (!filterOptions.GetWiseDeviceId().empty()) { + sql += "dp.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetWiseDeviceId())); + } + if (sql.empty()) { + return; + } + sql.erase(sql.end() - AND_LENGTH); + return; +} + +int32_t CharacteristicProfileDao::CharProfileToEntries(const CharacteristicProfile& charProfile, ValuesBucket& values) +{ + if (charProfile.GetDeviceId().empty() || charProfile.GetServiceProfileId() <= 0 || + charProfile.GetServiceId().empty() || charProfile.GetCharacteristicKey().empty() || + charProfile.GetCharacteristicValue().empty()) { + return DP_INVALID_PARAMS; + } + values.PutInt(SERVICE_PROFILE_ID, charProfile.GetServiceProfileId()); + values.PutString(CHARACTERISTIC_KEY, charProfile.GetCharacteristicKey()); + values.PutString(CHARACTERISTIC_VALUE, charProfile.GetCharacteristicValue()); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::ConvertToCharProfile( + std::shared_ptr resultSet, CharacteristicProfile& charProfile) +{ + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_FAIL; + } + RowEntity rowEntity; + if (resultSet->GetRow(rowEntity) != DP_SUCCESS) { + HILOGE("get resultSet failed"); + return DP_GET_RESULTSET_FAIL; + } + charProfile.SetId(rowEntity.Get(ID)); + charProfile.SetServiceProfileId(rowEntity.Get(SERVICE_PROFILE_ID)); + charProfile.SetDeviceId(rowEntity.Get(DEVICE_ID)); + charProfile.SetServiceId(rowEntity.Get(SERVICE_PROFILE_SERVICE_ID)); + charProfile.SetCharacteristicKey(rowEntity.Get(CHARACTERISTIC_KEY)); + charProfile.SetCharacteristicValue(rowEntity.Get(CHARACTERISTIC_VALUE)); + return DP_SUCCESS; +} + +int32_t CharacteristicProfileDao::SetCharacteristicProfileId(CharacteristicProfile& charProfile) +{ + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get( + SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID, std::vector { + charProfile.GetServiceProfileId(), charProfile.GetCharacteristicKey(), + charProfile.GetCharacteristicValue()}); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_FAIL; + } + while (resultSet->GoToNextRow() == DP_SUCCESS) { + int32_t columnIndex = COLUMNINDEX_INIT; + int32_t id = DEVICE_PROFILE_ID_INIT; + resultSet->GetColumnIndex(ID, columnIndex); + resultSet->GetInt(columnIndex, id); + charProfile.SetId(id); + } + resultSet->Close(); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS -- Gitee From f6f80dfa3a3eedea9f1ef6e9edf10f458f105d34 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 31 Dec 2024 02:50:17 +0000 Subject: [PATCH 2/5] update common/src/interfaces/characteristic_profile.cpp. Signed-off-by: torrizo --- common/src/interfaces/characteristic_profile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index b3ec27f9..c16c0a17 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -155,7 +155,7 @@ bool CharacteristicProfile::operator!=(const CharacteristicProfile& charProfile) } } -CharacteristicProfile& CharacteristicProfile::operator!=(const CharacteristicProfile& other) +CharacteristicProfile& CharacteristicProfile::operator=(const CharacteristicProfile& other) { if (this == &other) { return *this; -- Gitee From fc02e8e4cc26f778da0614c796b7849cec0bcce8 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 31 Dec 2024 03:28:05 +0000 Subject: [PATCH 3/5] update common/include/interfaces/characteristic_profile.h. Signed-off-by: torrizo --- common/include/interfaces/characteristic_profile.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 474bd55b..62cd35c6 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -42,7 +42,7 @@ public: isMultiUser_(isMultiUser), userId_(userId) {} - CharacteristicProfile(const std::string& deviceId, + CharacteristicProfile(const std::string& deviceId, const std::string& characteristicKey, const std::string& characteristicValue, const int32_t id, const int32_t serviceProfileId, const std::string& serviceId) : deviceId_(deviceId), -- Gitee From 3bb28d05a93c0d7cc81bbcfad2f1dd78b2e8d79a Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 31 Dec 2024 03:41:46 +0000 Subject: [PATCH 4/5] CharacteristicProfile Signed-off-by: torrizo --- common/include/constants/distributed_device_profile_constants.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 5f1f329c..289ebc55 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -47,8 +47,6 @@ extern const std::string OH_PROFILE_SUFFIX; extern const std::string SERVICE_NAME; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ -extern const std::string SERVICE_PROFILE_ID; -extern const std::string CHARACTERISTIC_PROFILE_TABLE; extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; /* TrustDeviceProfile Attribute */ -- Gitee From 90ce51e4aabcc6ff78bec756b1d8786e3500c513 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 31 Dec 2024 03:42:40 +0000 Subject: [PATCH 5/5] rename common/src/constants Signed-off-by: torrizo --- ..._constants.cpp => sdistributed_device_profile_constants.cpp} | 2 -- 1 file changed, 2 deletions(-) rename common/src/constants/{distributed_device_profile_constants.cpp => sdistributed_device_profile_constants.cpp} (99%) diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/sdistributed_device_profile_constants.cpp similarity index 99% rename from common/src/constants/distributed_device_profile_constants.cpp rename to common/src/constants/sdistributed_device_profile_constants.cpp index b3a9996d..7414d4bb 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/sdistributed_device_profile_constants.cpp @@ -37,8 +37,6 @@ const std::string OH_PROFILE_SUFFIX = "_OH"; const std::string SERVICE_NAME = "serviceName"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ -const std::string SERVICE_PROFILE_ID = "serviceProfileId"; -const std::string CHARACTERISTIC_PROFILE_TABLE = "characteristic_profile"; const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; /* TrustDeviceProfile Attribute */ -- Gitee