From 49deb42b2d4fd7cfd3431377a5a37a455e1f7392 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Wed, 11 Dec 2024 10:47:39 +0800 Subject: [PATCH 01/55] =?UTF-8?q?IpcUtils=E5=A2=9E=E5=8A=A0std::vector=E7=9A=84Marshalling=E5=92=8CUnMarshalling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangzhaohao --- .../distributed_device_profile_constants.h | 1 + common/include/utils/ipc_utils.h | 2 ++ common/src/utils/ipc_utils.cpp | 29 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 289ebc55..bee61c75 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -135,6 +135,7 @@ constexpr int32_t MAX_INTERFACE_SIZE = 20; constexpr int32_t MAX_SUBSCRIBE_INFO_SIZE = 500; constexpr int32_t MAX_SYNC_RESULTS_SIZE = 50; constexpr int32_t MAX_STATIC_CAPABILITY_SIZE = 100; +constexpr int32_t MAX_ID_SIZE = 1000; constexpr int32_t MIN_USER_ID = 0; constexpr int32_t MAX_USER_ID = 100000; constexpr uint32_t MAX_TRUSTED_DEVICE_SIZE = 1000; diff --git a/common/include/utils/ipc_utils.h b/common/include/utils/ipc_utils.h index 551cc356..e5dc5939 100644 --- a/common/include/utils/ipc_utils.h +++ b/common/include/utils/ipc_utils.h @@ -44,6 +44,7 @@ public: static bool Marshalling(MessageParcel& parcel, const std::vector& aclProfiles); static bool Marshalling(MessageParcel& parcel, const std::vector& serviceProfiles); static bool Marshalling(MessageParcel& parcel, const std::vector& charProfiles); + static bool Marshalling(MessageParcel& parcel, const std::vector& strings); static bool Marshalling(MessageParcel& parcel, const std::map& params); static bool Marshalling(MessageParcel& parcel, const std::map& listenerMap); @@ -53,6 +54,7 @@ public: static bool UnMarshalling(MessageParcel& parcel, std::vector& aclProfiles); static bool UnMarshalling(MessageParcel& parcel, std::vector& serviceProfiles); static bool UnMarshalling(MessageParcel& parcel, std::vector& charProfiles); + static bool UnMarshalling(MessageParcel& parcel, std::vector& strings); static bool UnMarshalling(MessageParcel& parcel, std::map& params); static bool UnMarshalling(MessageParcel& parcel, std::map& listenerMap); diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index 882875d4..00a9b979 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -87,6 +87,20 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& strings) +{ + if (strings.empty() || strings.size() > MAX_ID_SIZE) { + HILOGE("strings size is invalid!size : %{public}zu", strings.size()); + return false; + } + size_t size = strings.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + for (const auto& item : strings) { + WRITE_HELPER_RET(parcel, String, item, false); + } + return true; +} + bool IpcUtils::Marshalling(MessageParcel& parcel, const std::map& params) { if (params.size() == 0 || params.size() > MAX_PARAM_SIZE) { @@ -220,6 +234,21 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& strings) +{ + size_t size = parcel.ReadUint32(); + if (size == 0 || size > MAX_ID_SIZE) { + HILOGE("strings size is invalid!size : %{public}u", size); + return false; + } + for (uint32_t i = 0; i < size; i++) { + std::string item = ""; + READ_HELPER_RET(parcel, String, item, false); + strings.emplace_back(item); + } + return true; +} + bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::map& params) { uint32_t size = parcel.ReadUint32(); -- Gitee From 99578de19de5fb6e5bd665d11e8a2708499134fb Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Wed, 11 Dec 2024 11:26:32 +0800 Subject: [PATCH 02/55] =?UTF-8?q?IpcUtils=E5=A2=9E=E5=8A=A0std::vector=E7=9A=84Marshalling=E5=92=8CUnMarshalling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangzhaohao --- common/include/utils/ipc_utils.h | 2 ++ common/src/utils/ipc_utils.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/common/include/utils/ipc_utils.h b/common/include/utils/ipc_utils.h index e5dc5939..02ee50c8 100644 --- a/common/include/utils/ipc_utils.h +++ b/common/include/utils/ipc_utils.h @@ -45,6 +45,7 @@ public: static bool Marshalling(MessageParcel& parcel, const std::vector& serviceProfiles); static bool Marshalling(MessageParcel& parcel, const std::vector& charProfiles); static bool Marshalling(MessageParcel& parcel, const std::vector& strings); + static bool Marshalling(MessageParcel& parcel, const std::vector& params); static bool Marshalling(MessageParcel& parcel, const std::map& params); static bool Marshalling(MessageParcel& parcel, const std::map& listenerMap); @@ -55,6 +56,7 @@ public: static bool UnMarshalling(MessageParcel& parcel, std::vector& serviceProfiles); static bool UnMarshalling(MessageParcel& parcel, std::vector& charProfiles); static bool UnMarshalling(MessageParcel& parcel, std::vector& strings); + static bool UnMarshalling(MessageParcel& parcel, std::vector& params); static bool UnMarshalling(MessageParcel& parcel, std::map& params); static bool UnMarshalling(MessageParcel& parcel, std::map& listenerMap); diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index 00a9b979..d805ade1 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -101,6 +101,20 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector return true; } +bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) +{ + if (params.empty() || params.size() > MAX_ID_SIZE) { + HILOGE("params size is invalid!size : %{public}zu", params.size()); + return false; + } + size_t size = params.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + for (const auto& item : params) { + WRITE_HELPER_RET(parcel, Int32, item, false); + } + return true; +} + bool IpcUtils::Marshalling(MessageParcel& parcel, const std::map& params) { if (params.size() == 0 || params.size() > MAX_PARAM_SIZE) { @@ -249,6 +263,21 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& st return true; } +bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params) +{ + size_t size = parcel.ReadUint32(); + if (size == 0 || size > MAX_ID_SIZE) { + HILOGE("params size is invalid!size : %{public}u", size); + return false; + } + for (uint32_t i = 0; i < size; i++) { + int32_t item = 0; + READ_HELPER_RET(parcel, Int32, item, false); + params.emplace_back(item); + } + return true; +} + bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::map& params) { uint32_t size = parcel.ReadUint32(); -- Gitee From de5b66a48cbc0d64e9b80e357e2609725a4a1f07 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Sat, 14 Dec 2024 18:50:43 +0800 Subject: [PATCH 03/55] add profile data Signed-off-by: zhanglei --- common/BUILD.gn | 1 + .../distributed_device_profile_constants.h | 21 ++ common/include/interfaces/device_profile.h | 90 ++++- .../device_profile_filter_options.h | 56 +++ .../distributed_device_profile_constants.cpp | 20 ++ common/src/interfaces/device_profile.cpp | 281 +++++++++++++-- .../device_profile_filter_options.cpp | 138 ++++++++ common/src/utils/ipc_utils.cpp | 4 +- services/core/BUILD.gn | 4 + .../include/common/dp_services_constants.h | 35 ++ .../rdbadapter/profile_data_rdb_adapter.h | 54 +++ .../profiledatamanager/device_profile_dao.h | 59 ++++ .../subscribe_profile_manager.h | 4 + .../core/src/common/dp_services_constants.cpp | 63 ++++ .../rdbadapter/profile_data_rdb_adapter.cpp | 257 ++++++++++++++ .../profiledatamanager/device_profile_dao.cpp | 324 ++++++++++++++++++ .../subscribe_profile_manager.cpp | 63 +++- .../unittest/device_profile_manager_test.cpp | 30 -- ...tributed_device_profile_client_kv_test.cpp | 4 - .../core/test/unittest/profile_cache_test.cpp | 2 - .../unittest/profile_control_utils_test.cpp | 2 - 21 files changed, 1427 insertions(+), 85 deletions(-) create mode 100644 common/include/interfaces/device_profile_filter_options.h create mode 100644 common/src/interfaces/device_profile_filter_options.cpp create mode 100644 services/core/include/common/dp_services_constants.h create mode 100644 services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h create mode 100644 services/core/include/profiledatamanager/device_profile_dao.h create mode 100644 services/core/src/common/dp_services_constants.cpp create mode 100644 services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp create mode 100644 services/core/src/profiledatamanager/device_profile_dao.cpp diff --git a/common/BUILD.gn b/common/BUILD.gn index 2f7ec0e2..5cb74819 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -51,6 +51,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accesser.cpp", "src/interfaces/characteristic_profile.cpp", "src/interfaces/device_profile.cpp", + "src/interfaces/device_profile_filter_options.cpp", "src/interfaces/dp_inited_callback_proxy.cpp", "src/interfaces/dp_inited_callback_stub.cpp", "src/interfaces/dp_subscribe_info.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index bee61c75..f67e082d 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -43,6 +43,26 @@ extern const std::string OS_API_LEVEL; extern const std::string OS_VERSION; extern const std::string OS_TYPE; extern const std::string OH_PROFILE_SUFFIX; +extern const std::string ID; +extern const std::string DEV_TYPE; +extern const std::string MANU; +extern const std::string SN; +extern const std::string PRODUCT_ID; +extern const std::string SUB_PRODUCT_ID; +extern const std::string HIV; +extern const std::string MAC; +extern const std::string FWV; +extern const std::string HWV; +extern const std::string SWV; +extern const std::string PROT_TYPE; +extern const std::string WISE_USER_ID; +extern const std::string WISE_DEVICE_ID; +extern const std::string ROOM_NAME; +extern const std::string REGISTER_TIME; +extern const std::string MODIFY_TIME; +extern const std::string SHARE_TIME; +extern const std::string PRODUCTOR_INFO_VERSION; +extern const std::string DEVICE_PROFILE_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; extern const std::string SERVICE_TYPE; @@ -192,6 +212,7 @@ constexpr int32_t DELETE_ACCESSEE_CONDITION = 1; constexpr int32_t DELETE_TRUST_CONDITION = 0; constexpr int32_t MAX_SAID = 16777215; constexpr int64_t ROWID_INIT = -1; +constexpr int64_t DEVICE_PROFILE_ID_INIT = -1; constexpr int64_t ACCESSERID_INIT = -1; constexpr int64_t ACCESSEEID_INIT = -1; constexpr int64_t ACCESSCONTROLID_INIT = -1; diff --git a/common/include/interfaces/device_profile.h b/common/include/interfaces/device_profile.h index c0a2f207..1443d22c 100644 --- a/common/include/interfaces/device_profile.h +++ b/common/include/interfaces/device_profile.h @@ -27,8 +27,6 @@ class DeviceProfile : public DpParcel { public: DeviceProfile() : deviceId_(""), - deviceTypeName_(""), - deviceTypeId_(0), deviceName_(""), manufactureName_(""), deviceModel_(""), @@ -38,16 +36,32 @@ public: osVersion_(""), osType_(0), isMultiUser_(false), - userId_(DEFAULT_USER_ID) + userId_(DEFAULT_USER_ID), + id_(0), + devType_(""), + manu_(""), + sn_(""), + productId_(""), + subProductId_(""), + hiv_(""), + mac_(""), + fwv_(""), + hwv_(""), + swv_(""), + protType_(0), + wiseUserId_(""), + wiseDeviceId_(""), + roomName_(""), + registerTime_(""), + modifyTime_(0), + shareTime_(""), + productorInfoVersion_(""), + accountId_("") {} ~DeviceProfile() = default; std::string GetDeviceId() const; void SetDeviceId(const std::string& deviceId); - std::string GetDeviceTypeName() const; - void SetDeviceTypeName(const std::string& deviceTypeName); - int32_t GetDeviceTypeId() const; - void SetDeviceTypeId(int32_t deviceTypeId); std::string GetDeviceName() const; void SetDeviceName(const std::string& deviceName); std::string GetManufactureName() const; @@ -68,6 +82,46 @@ public: void SetIsMultiUser(bool isMultiUser); int32_t GetUserId() const; void SetUserId(int32_t userId); + int32_t GetId() const; + void SetId(int32_t id); + std::string GetDevType() const; + void SetDevType(const std::string& devType); + std::string GetManu() const; + void SetManu(const std::string& manu); + std::string GetSn() const; + void SetSn(const std::string& sn); + std::string GetProductId() const; + void SetProductId(const std::string& productId); + std::string GetSubProductId() const; + void SetSubProductId(const std::string& subProductId); + std::string GetHiv() const; + void SetHiv(const std::string& hiv); + std::string GetMac() const; + void SetMac(const std::string& mac); + std::string GetFwv() const; + void SetFwv(const std::string& fwv); + std::string GetHwv() const; + void SetHwv(const std::string& hwv); + std::string GetSwv() const; + void SetSwv(const std::string& swv); + int32_t GetProtType() const; + void SetProtType(int32_t protType); + std::string GetWiseUserId() const; + void SetWiseUserId(const std::string& wiseUserId); + std::string GetWiseDeviceId() const; + void SetWiseDeviceId(const std::string& wiseDeviceId); + std::string GetRoomName() const; + void SetRoomName(const std::string& roomName); + std::string GetRegisterTime() const; + void SetRegisterTime(const std::string& registerTime); + int32_t GetModifyTime() const; + void SetModifyTime(int32_t modifyTime); + std::string GetShareTime() const; + void SetShareTime(const std::string& shareTime); + std::string GetProductorInfoVersion() const; + void SetProductorInfoVersion(const std::string& productorInfoVersion); + std::string GetAccountId() const; + void SetAccountId(const std::string& accountId); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const DeviceProfile& deviceProfile) const; @@ -76,8 +130,6 @@ public: private: std::string deviceId_; - std::string deviceTypeName_; - int32_t deviceTypeId_; std::string deviceName_; std::string manufactureName_; std::string deviceModel_; @@ -88,6 +140,26 @@ private: int32_t osType_; bool isMultiUser_; int32_t userId_; + int32_t id_; + std::string devType_; + std::string manu_; + std::string sn_; + std::string productId_; + std::string subProductId_; + std::string hiv_; + std::string mac_; + std::string fwv_; + std::string hwv_; + std::string swv_; + int32_t protType_; + std::string wiseUserId_; + std::string wiseDeviceId_; + std::string roomName_; + std::string registerTime_; + int32_t modifyTime_; + std::string shareTime_; + std::string productorInfoVersion_; + std::string accountId_; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/device_profile_filter_options.h b/common/include/interfaces/device_profile_filter_options.h new file mode 100644 index 00000000..60b341b8 --- /dev/null +++ b/common/include/interfaces/device_profile_filter_options.h @@ -0,0 +1,56 @@ + /* + * 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. + */ + +#ifndef OHOS_DP_DEVICE_PROFILE_FILTER_OPTIONS_H +#define OHOS_DP_DEVICE_PROFILE_FILTER_OPTIONS_H + +#include +#include "distributed_device_profile_enums.h" +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceProfileFilterOptions : public DpParcel { +public: + DeviceProfileFilterOptions() + : userId_(-1), + accountId_("") + {} + ~DeviceProfileFilterOptions() = default; + + int32_t GetUserId() const; + void SetUserId(int32_t userId); + std::string GetAccountId() const; + void SetAccountId(std::string accountId); + std::vector GetDeviceIds() const; + void SetDeviceIds(std::vector deviceIds); + std::vector GetWiseDeviceIds() const; + void SetWiseDeviceIds(std::vector wiseDeviceIds); + std::vector GetDeviceProfileIds() const; + void SetDeviceProfileIds(std::vector deviceProfileIds); + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + int32_t userId_; + std::string accountId_; + std::vector deviceIds_; + std::vector wiseDeviceIds_; + std::vector deviceProfileIds_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DEVICE_PROFILE_FILTER_OPTIONS_H diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index 7414d4bb..ae381843 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -33,6 +33,26 @@ const std::string OS_API_LEVEL = "osApiLevel"; const std::string OS_VERSION = "osVersion"; const std::string OS_TYPE = "osType"; const std::string OH_PROFILE_SUFFIX = "_OH"; +const std::string ID = "id"; +const std::string DEV_TYPE = "devType"; +const std::string MANU = "manu"; +const std::string SN = "sn"; +const std::string PRODUCT_ID = "productId"; +const std::string SUB_PRODUCT_ID = "subProductId"; +const std::string HIV = "hiv"; +const std::string MAC = "mac"; +const std::string FWV = "fwv"; +const std::string HWV = "hwv"; +const std::string SWV = "swv"; +const std::string PROT_TYPE = "protType"; +const std::string WISE_USER_ID = "wiseUserId"; +const std::string WISE_DEVICE_ID = "wiseDeviceId"; +const std::string ROOM_NAME = "roomName"; +const std::string REGISTER_TIME = "registerTime"; +const std::string MODIFY_TIME = "modifyTime"; +const std::string SHARE_TIME = "shareTime"; +const std::string PRODUCTOR_INFO_VERSION = "productorInfoVersion"; +const std::string DEVICE_PROFILE_TABLE = "device_profile"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; const std::string SERVICE_TYPE = "serviceType"; diff --git a/common/src/interfaces/device_profile.cpp b/common/src/interfaces/device_profile.cpp index b51fd2b8..0af044ee 100644 --- a/common/src/interfaces/device_profile.cpp +++ b/common/src/interfaces/device_profile.cpp @@ -34,26 +34,6 @@ void DeviceProfile::SetDeviceId(const std::string& deviceId) deviceId_ = deviceId; } -std::string DeviceProfile::GetDeviceTypeName() const -{ - return deviceTypeName_; -} - -void DeviceProfile::SetDeviceTypeName(const std::string &deviceTypeName) -{ - deviceTypeName_ = deviceTypeName; -} - -int32_t DeviceProfile::GetDeviceTypeId() const -{ - return deviceTypeId_; -} - -void DeviceProfile::SetDeviceTypeId(int32_t deviceTypeId) -{ - deviceTypeId_ = deviceTypeId; -} - std::string DeviceProfile::GetDeviceName() const { return deviceName_; @@ -154,11 +134,209 @@ void DeviceProfile::SetUserId(int32_t userId) userId_ = userId; } +int32_t DeviceProfile::GetId() const +{ + return id_; +} + +void DeviceProfile::SetId(int32_t id) +{ + id_ = id; +} + +std::string DeviceProfile::GetDevType() const +{ + return devType_; +} + +void DeviceProfile::SetDevType(const std::string &devType) +{ + devType_ = devType; +} + +std::string DeviceProfile::GetManu() const +{ + return manu_; +} + +void DeviceProfile::SetManu(const std::string &manu) +{ + manu_ = manu; +} + +std::string DeviceProfile::GetSn() const +{ + return sn_; +} + +void DeviceProfile::SetSn(const std::string &sn) +{ + sn_ = sn; +} + +std::string DeviceProfile::GetProductId() const +{ + return productId_; +} + +void DeviceProfile::SetProductId(const std::string &productId) +{ + productId_ = productId; +} + +std::string DeviceProfile::GetSubProductId() const +{ + return subProductId_; +} + +void DeviceProfile::SetSubProductId(const std::string &subProductId) +{ + subProductId_ = subProductId; +} + +std::string DeviceProfile::GetHiv() const +{ + return hiv_; +} + +void DeviceProfile::SetHiv(const std::string &hiv) +{ + hiv_ = hiv; +} + +std::string DeviceProfile::GetMac() const +{ + return mac_; +} + +void DeviceProfile::SetMac(const std::string &mac) +{ + mac_ = mac; +} + +std::string DeviceProfile::GetFwv() const +{ + return fwv_; +} + +void DeviceProfile::SetFwv(const std::string &fwv) +{ + fwv_ = fwv; +} + +std::string DeviceProfile::GetHwv() const +{ + return hwv_; +} + +void DeviceProfile::SetHwv(const std::string &hwv) +{ + hwv_ = hwv; +} + +std::string DeviceProfile::GetSwv() const +{ + return swv_; +} + +void DeviceProfile::SetSwv(const std::string &swv) +{ + swv_ = swv; +} + +int32_t DeviceProfile::GetProtType() const +{ + return protType_; +} + +void DeviceProfile::SetProtType(int32_t protType) +{ + protType_ = protType; +} + +std::string DeviceProfile::GetWiseUserId() const +{ + return wiseUserId_; +} + +void DeviceProfile::SetWiseUserId(const std::string &wiseUserId) +{ + wiseUserId_ = wiseUserId; +} + +std::string DeviceProfile::GetWiseDeviceId() const +{ + return wiseDeviceId_; +} + +void DeviceProfile::SetWiseDeviceId(const std::string &wiseDeviceId) +{ + wiseDeviceId_ = wiseDeviceId; +} + +std::string DeviceProfile::GetRoomName() const +{ + return roomName_; +} + +void DeviceProfile::SetRoomName(const std::string &roomName) +{ + roomName_ = roomName; +} + +std::string DeviceProfile::GetRegisterTime() const +{ + return registerTime_; +} + +void DeviceProfile::SetRegisterTime(const std::string ®isterTime) +{ + registerTime_ = registerTime; +} + +int32_t DeviceProfile::GetModifyTime() const +{ + return modifyTime_; +} + +void DeviceProfile::SetModifyTime(int32_t modifyTime) +{ + modifyTime_ = modifyTime; +} + +std::string DeviceProfile::GetShareTime() const +{ + return shareTime_; +} + +void DeviceProfile::SetShareTime(const std::string &shareTime) +{ + shareTime_ = shareTime; +} + +std::string DeviceProfile::GetProductorInfoVersion() const +{ + return productorInfoVersion_; +} + +void DeviceProfile::SetProductorInfoVersion(const std::string &productorInfoVersion) +{ + productorInfoVersion_ = productorInfoVersion; +} + +std::string DeviceProfile::GetAccountId() const +{ + return accountId_; +} + +void DeviceProfile::SetAccountId(const std::string &accountId) +{ + accountId_ = accountId; +} + bool DeviceProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, deviceId_, false); - WRITE_HELPER_RET(parcel, String, deviceTypeName_, false); - WRITE_HELPER_RET(parcel, Int32, deviceTypeId_, false); WRITE_HELPER_RET(parcel, String, deviceName_, false); WRITE_HELPER_RET(parcel, String, manufactureName_, false); WRITE_HELPER_RET(parcel, String, deviceModel_, false); @@ -169,14 +347,32 @@ bool DeviceProfile::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, Int32, osType_, 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, String, devType_, false); + WRITE_HELPER_RET(parcel, String, manu_, false); + WRITE_HELPER_RET(parcel, String, sn_, false); + WRITE_HELPER_RET(parcel, String, productId_, false); + WRITE_HELPER_RET(parcel, String, subProductId_, false); + WRITE_HELPER_RET(parcel, String, hiv_, false); + WRITE_HELPER_RET(parcel, String, mac_, false); + WRITE_HELPER_RET(parcel, String, fwv_, false); + WRITE_HELPER_RET(parcel, String, hwv_, false); + WRITE_HELPER_RET(parcel, String, swv_, false); + WRITE_HELPER_RET(parcel, Int32, protType_, false); + WRITE_HELPER_RET(parcel, String, wiseUserId_, false); + WRITE_HELPER_RET(parcel, String, wiseDeviceId_, false); + WRITE_HELPER_RET(parcel, String, roomName_, false); + WRITE_HELPER_RET(parcel, String, registerTime_, false); + WRITE_HELPER_RET(parcel, Int32, modifyTime_, false); + WRITE_HELPER_RET(parcel, String, shareTime_, false); + WRITE_HELPER_RET(parcel, String, productorInfoVersion_, false); + WRITE_HELPER_RET(parcel, String, accountId_, false); return true; } bool DeviceProfile::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, String, deviceId_, false); - READ_HELPER_RET(parcel, String, deviceTypeName_, false); - READ_HELPER_RET(parcel, Int32, deviceTypeId_, false); READ_HELPER_RET(parcel, String, deviceName_, false); READ_HELPER_RET(parcel, String, manufactureName_, false); READ_HELPER_RET(parcel, String, deviceModel_, false); @@ -187,18 +383,37 @@ bool DeviceProfile::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, Int32, osType_, 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, String, devType_, false); + READ_HELPER_RET(parcel, String, manu_, false); + READ_HELPER_RET(parcel, String, sn_, false); + READ_HELPER_RET(parcel, String, productId_, false); + READ_HELPER_RET(parcel, String, subProductId_, false); + READ_HELPER_RET(parcel, String, hiv_, false); + READ_HELPER_RET(parcel, String, mac_, false); + READ_HELPER_RET(parcel, String, fwv_, false); + READ_HELPER_RET(parcel, String, hwv_, false); + READ_HELPER_RET(parcel, String, swv_, false); + READ_HELPER_RET(parcel, Int32, protType_, false); + READ_HELPER_RET(parcel, String, wiseUserId_, false); + READ_HELPER_RET(parcel, String, wiseDeviceId_, false); + READ_HELPER_RET(parcel, String, roomName_, false); + READ_HELPER_RET(parcel, String, registerTime_, false); + READ_HELPER_RET(parcel, Int32, modifyTime_, false); + READ_HELPER_RET(parcel, String, shareTime_, false); + READ_HELPER_RET(parcel, String, productorInfoVersion_, false); + READ_HELPER_RET(parcel, String, accountId_, false); return true; } bool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const { - bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || deviceTypeName_ != deviceProfile.GetDeviceTypeName() - || deviceTypeId_ != deviceProfile.GetDeviceTypeId() || deviceName_ != deviceProfile.GetDeviceName() || - manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() || - storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() || - osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || osType_ != - deviceProfile.GetOsType() || isMultiUser_ != deviceProfile.IsMultiUser() || userId_ != - deviceProfile.GetUserId()); + bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || + deviceName_ != deviceProfile.GetDeviceName() || manufactureName_ != deviceProfile.GetManufactureName() || + deviceModel_ != deviceProfile.GetDeviceModel() || storageCapability_ != deviceProfile.GetStorageCapability() || + osSysCap_ != deviceProfile.GetOsSysCap() || osApiLevel_ != deviceProfile.GetOsApiLevel() || + osVersion_ != deviceProfile.GetOsVersion() || osType_ != deviceProfile.GetOsType() || + isMultiUser_ != deviceProfile.IsMultiUser() || userId_ != deviceProfile.GetUserId()); if (isNotEqual) { return true; } else { @@ -214,8 +429,6 @@ std::string DeviceProfile::dump() const return EMPTY_STRING; } cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str()); - cJSON_AddStringToObject(json, DEVICE_TYPE_NAME.c_str(), deviceTypeName_.c_str()); - cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_); cJSON_AddStringToObject(json, DEVICE_NAME.c_str(), deviceName_.c_str()); cJSON_AddStringToObject(json, MANUFACTURE_NAME.c_str(), manufactureName_.c_str()); cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), deviceModel_.c_str()); @@ -246,8 +459,6 @@ std::string DeviceProfile::AnnoymizeDump() const return EMPTY_STRING; } cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str()); - cJSON_AddStringToObject(json, DEVICE_TYPE_NAME.c_str(), deviceTypeName_.c_str()); - cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_); cJSON_AddStringToObject(json, DEVICE_NAME.c_str(), deviceName_.c_str()); cJSON_AddStringToObject(json, MANUFACTURE_NAME.c_str(), manufactureName_.c_str()); cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), deviceModel_.c_str()); diff --git a/common/src/interfaces/device_profile_filter_options.cpp b/common/src/interfaces/device_profile_filter_options.cpp new file mode 100644 index 00000000..b0c1f124 --- /dev/null +++ b/common/src/interfaces/device_profile_filter_options.cpp @@ -0,0 +1,138 @@ +/* + * 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 "device_profile_filter_options.h" +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "macro_utils.h" +#include "profile_utils.h" +#include "ipc_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DeviceProfileFilterOptions"; +} +int32_t DeviceProfileFilterOptions::GetUserId() const +{ + return userId_; +} + +void DeviceProfileFilterOptions::SetUserId(int32_t userId) +{ + userId_ = userId; +} + +std::string DeviceProfileFilterOptions::GetAccountId() const +{ + return accountId_; +} + +void DeviceProfileFilterOptions::SetAccountId(std::string accountId) +{ + accountId_ = accountId; +} + +std::vector DeviceProfileFilterOptions::GetDeviceIds() const +{ + return deviceIds_; +} + +void DeviceProfileFilterOptions::SetDeviceIds(std::vector deviceIds) +{ + deviceIds_ = deviceIds; +} + +std::vector DeviceProfileFilterOptions::GetWiseDeviceIds() const +{ + return wiseDeviceIds_; +} + +void DeviceProfileFilterOptions::SetWiseDeviceIds(std::vector wiseDeviceIds) +{ + wiseDeviceIds_ = wiseDeviceIds; +} + +std::vector DeviceProfileFilterOptions::GetDeviceProfileIds() const +{ + return deviceProfileIds_; +} + +void DeviceProfileFilterOptions::SetDeviceProfileIds(std::vector deviceProfileIds) +{ + deviceProfileIds_ = deviceProfileIds; +} + +bool DeviceProfileFilterOptions::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER_RET(parcel, Int32, userId_, false); + WRITE_HELPER_RET(parcel, String, accountId_, false); + IpcUtils::Marshalling(parcel, deviceIds_); + IpcUtils::Marshalling(parcel, wiseDeviceIds_); + IpcUtils::Marshalling(parcel, deviceProfileIds_); + return true; +} + +bool DeviceProfileFilterOptions::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, Int32, userId_, false); + READ_HELPER_RET(parcel, String, accountId_, false); + IpcUtils::UnMarshalling(parcel, deviceIds_); + IpcUtils::UnMarshalling(parcel, wiseDeviceIds_); + IpcUtils::UnMarshalling(parcel, deviceProfileIds_); + return true; +} + +std::string DeviceProfileFilterOptions::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (!cJSON_IsObject(json)) { + cJSON_Delete(json); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, USERID.c_str(), ProfileUtils::GetAnonyInt32(userId_).c_str()); + cJSON_AddStringToObject(json, ACCOUNTID.c_str(), ProfileUtils::GetAnonyString(accountId_).c_str()); + cJSON* jsonArr = cJSON_CreateArray(); + if (!cJSON_IsArray(jsonArr)) { + cJSON_Delete(jsonArr); + } else { + for (const auto &deviceId : deviceIds_) { + cJSON* jsonArrItem = cJSON_CreateString(ProfileUtils::GetAnonyString(deviceId).c_str()); + if (jsonArrItem == NULL) { + continue; + } + if (!cJSON_AddItemToArray(jsonArr, jsonArrItem)) { + cJSON_Delete(jsonArrItem); + continue; + } + } + if (!cJSON_AddItemToObject(json, DEVICE_ID.c_str(), jsonArr)) { + HILOGE("cJSON formatted to string failed!"); + cJSON_Delete(jsonArr); + } + } + 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/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index d805ade1..b51fc826 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -234,7 +234,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector MAX_PROFILE_SIZE) { - HILOGE("Profile size is invalid!size : %{public}u", size); + HILOGE("Profile size is invalid!size : %{public}zu", size); return false; } for (uint32_t i = 0; i < size; i++) { @@ -252,7 +252,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& st { size_t size = parcel.ReadUint32(); if (size == 0 || size > MAX_ID_SIZE) { - HILOGE("strings size is invalid!size : %{public}u", size); + HILOGE("strings size is invalid!size : %{public}zu", size); return false; } for (uint32_t i = 0; i < size; i++) { diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 0a79847c..81b60a24 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -31,6 +31,7 @@ config("device_info_manager_config") { "include/persistenceadapter", "include/persistenceadapter/kvadapter", "include/persistenceadapter/rdbadapter", + "include/profiledatamanager", "include/publishcommonevent", "include/subscribeprofilemanager", "include/staticcapabilitycollector", @@ -64,6 +65,7 @@ ohos_shared_library("distributed_device_profile_svr") { install_enable = true sources = [ + "src/common/dp_services_constants.cpp", "src/contentsensormanager/collaboration_info_collector.cpp", "src/contentsensormanager/collector.cpp", "src/contentsensormanager/content_sensor_manager.cpp", @@ -87,7 +89,9 @@ ohos_shared_library("distributed_device_profile_svr") { "src/permissionmanager/permission_manager.cpp", "src/persistenceadapter/kvadapter/kv_adapter.cpp", "src/persistenceadapter/kvadapter/switch_adapter.cpp", + "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", + "src/profiledatamanager/device_profile_dao.cpp", "src/publishcommonevent/dp_account_common_event.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 new file mode 100644 index 00000000..265d990d --- /dev/null +++ b/services/core/include/common/dp_services_constants.h @@ -0,0 +1,35 @@ +/* + * 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_SERVICES_CONSTANTS_H +#define OHOS_DP_SERVICES_CONSTANTS_H + +#include +#include +#include + +namespace OHOS { +namespace DistributedDeviceProfile { +/* DeviceProfileDao */ +extern const std::string PROFILE_DATA_RDB_PATH; +extern const std::string PROFILE_DATA_DATABASE_NAME; +extern const std::string ID_EQUAL_CONDITION; +extern const std::string SELECT_DEVICE_PROFILE_TABLE; +extern const std::string SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID_ACCOUNTID; +extern const std::string CREATE_DEVICE_PROFILE_TABLE_SQL; +extern const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_SERVICES_CONSTANTS_H \ No newline at end of file diff --git a/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h b/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h new file mode 100644 index 00000000..9696849d --- /dev/null +++ b/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h @@ -0,0 +1,54 @@ +/* + * 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_PROFILE_DATA_RDB_ADAPTER_H +#define OHOS_DP_PROFILE_DATA_RDB_ADAPTER_H + + +#include +#include "irdb_adapter.h" +#include "rdb_open_callback.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ProfileDataRdbAdapter : public IRdbAdapter { + DECLARE_SINGLE_INSTANCE(ProfileDataRdbAdapter); +public: + int32_t Init() override; + int32_t UnInit() override; + int32_t Put(int64_t& outRowId, const std::string& table, const ValuesBucket& Values) override; + int32_t Delete(int32_t& deleteRows, const std::string& table, const std::string& whereClause, + const std::vector& bindArgs = {}) override; + int32_t Update(int32_t& changedRows, const std::string& table, const ValuesBucket& values, + const std::string& whereClause, const std::vector& bindArgs = {}) override; + int32_t CreateTable(const std::string& sql) override; + std::shared_ptr Get(const std::string& sql, const std::vector& args = {}) override; + int32_t GetRDBPtr(); + bool IsInit(); + +private: + std::shared_ptr store_ = nullptr; + std::mutex ProfileDataRdbAdapterMtx_; +}; + +class ProfileDataOpenCallback : public NativeRdb::RdbOpenCallback { +public: + int32_t OnCreate(RdbStore& store) override; + int32_t OnUpgrade(RdbStore& store, int oldVersion, int newVersion) override; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_PROFILE_DATA_RDB_ADAPTER_H diff --git a/services/core/include/profiledatamanager/device_profile_dao.h b/services/core/include/profiledatamanager/device_profile_dao.h new file mode 100644 index 00000000..2b030e5c --- /dev/null +++ b/services/core/include/profiledatamanager/device_profile_dao.h @@ -0,0 +1,59 @@ +/* + * 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. + */ + +#ifndef OHOS_DP_DEVICE_PROFILE_DAO_H +#define OHOS_DP_DEVICE_PROFILE_DAO_H + + +#include +#include +#include +#include +#include + +#include "device_profile.h" +#include "device_profile_filter_options.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class DeviceProfileDao { + DECLARE_SINGLE_INSTANCE(DeviceProfileDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutDeviceProfile(const DeviceProfile& deviceProfile); + int32_t GetDeviceProfiles(const DeviceProfileFilterOptions& filterOptions, + std::vector& deviceProfiles); + int32_t DeleteDeviceProfile(const DeviceProfile& deviceProfile); + int32_t UpdateDeviceProfile(const DeviceProfile& oldProfile, const DeviceProfile& newProfile); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const DeviceProfileFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t DeviceProfileToEntries(const DeviceProfile& deviceProfile, ValuesBucket& values); + int32_t ConvertToDeviceProfile(std::shared_ptr resultSet, DeviceProfile& deviceProfile); +private: + std::mutex rdbMutex_; +}; +} +} +#endif // OHOS_DP_DEVICE_PROFILE_DAO_H \ No newline at end of file diff --git a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h index 9fe77633..4f5aacfa 100644 --- a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h +++ b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h @@ -44,6 +44,10 @@ public: int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); int32_t SubscribeDeviceProfile(std::map subscribeInfos); int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); + int32_t NotifyDeviceProfileAdd(const DeviceProfile& deviceProfile); + int32_t NotifyDeviceProfileUpdate(const DeviceProfile& oldProfile, + const DeviceProfile& newProfile); + int32_t NotifyDeviceProfileDelete(const DeviceProfile& deviceProfile); private: int32_t NotifyDeviceProfileAdd(const std::string& dbKey, const std::string& dbValue); diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp new file mode 100644 index 00000000..dfa4bbee --- /dev/null +++ b/services/core/src/common/dp_services_constants.cpp @@ -0,0 +1,63 @@ +/* + * 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" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DpServicesConstants"; +} +/* DeviceProfileDao */ +const std::string PROFILE_DATA_RDB_PATH = "/data/service/el2/public/database/distributed_device_profile_service/"; +const std::string PROFILE_DATA_DATABASE_NAME = "profile_data.db"; +const std::string ID_EQUAL_CONDITION = "id = ?"; +const std::string SELECT_DEVICE_PROFILE_TABLE = "SELECT * FROM device_profile WHERE "; +const std::string SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID_ACCOUNTID = + "SELECT * FROM device_profile WHERE deviceId = ? AND udid = ? AND accountId = ?"; +const std::string CREATE_DEVICE_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_profile\ +(\ + id INTEGER PRIMARY KEY AUTOINCREMENT,\ + deviceId TEXT,\ + model TEXT,\ + devType TEXT,\ + manu TEXT,\ + sn TEXT,\ + productId TEXT,\ + subProductId TEXT,\ + hiv TEXT,\ + mac TEXT,\ + fwv TEXT,\ + hwv TEXT,\ + swv TEXT,\ + protType INTEGER,\ + userId INTEGER,\ + accountId TEXT,\ + wiseUserId TEXT,\ + deviceName TEXT,\ + wiseDeviceId TEXT,\ + roomName TEXT,\ + registerTime TEXT,\ + modifyTime INTEGER,\ + shareTime TEXT,\ + productInfoVersion TEXT,);"; +const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = +"CREATE UNIQUE INDEX if not exists unique_device_profile ON device_profile\ +(\ + deviceId,\ + userId,\ + accountId);"; +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp b/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp new file mode 100644 index 00000000..1bbcb056 --- /dev/null +++ b/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp @@ -0,0 +1,257 @@ +/* + * 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 +#include +#include +#include "profile_data_rdb_adapter.h" +#include "rdb_errno.h" +#include "distributed_device_profile_constants.h" +#include "dp_services_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProfileDataRdbAdapter); +using namespace std::chrono_literals; +namespace { + const std::set TABLES = { + "device_profile", + "service_profile", + "characteristic_profile", + "device_icon" + }; + const std::string TAG = "ProfileDatardbAdapter"; +} + +int32_t ProfileDataRdbAdapter::Init() +{ + int32_t retryTimes = RDB_INIT_MAX_TIMES; + while (retryTimes > 0) { + if (GetRDBPtr() == DP_SUCCESS) { + HILOGI("ProfileDatardbAdapter init success"); + return DP_SUCCESS; + } + usleep(RDB_INIT_INTERVAL_TIME); + retryTimes--; + } + HILOGE("ProfileDatardbAdapter init failed"); + return DP_RDBADAPTER_INIT_FAIL; +} + +int32_t ProfileDataRdbAdapter::UnInit() +{ + HILOGI("ProfileDatardbAdapter unInit"); + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + store_ = nullptr; + } + return DP_SUCCESS; +} + +int32_t ProfileDataRdbAdapter::Put(int64_t& outRowId, const std::string& table, const ValuesBucket& values) +{ + if (TABLES.find(table) == TABLES.end()) { + HILOGE("table does not exist"); + return DP_RDBADAPTER_TABLE_NOT_EXIST; + } + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + if (store_ == nullptr) { + HILOGE("RDBStore_ is null"); + return DP_RDB_DB_PTR_NULL; + } + int32_t ret = store_->Insert(outRowId, table, values); + if (ret == E_SQLITE_CORRUPT) { + HILOGE("database corrupt ret:%{public}d", ret); + int32_t restoreRet = store_->Restore(""); + if (restoreRet != E_OK) { + HILOGE("Restore failed restoreRet:%{public}d", restoreRet); + return DP_RDB_DATABASE_RESTORE_FAIL; + } + ret = store_->Insert(outRowId, table, values); + } + if (ret != E_OK) { + HILOGE("ProfileDatardbAdapter put failed ret:%{public}d", ret); + return DP_RDBADAPTER_PUT_FAIL; + } + } + return DP_SUCCESS; +} + +int32_t ProfileDataRdbAdapter::Delete(int32_t& deleteRows, const std::string& table, const std::string& whereClause, + const std::vector& bindArgs) +{ + if (TABLES.find(table) == TABLES.end()) { + HILOGE("table does not exist"); + return DP_RDBADAPTER_TABLE_NOT_EXIST; + } + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + if (store_ == nullptr) { + HILOGE("RDBStore_ is null"); + return DP_RDB_DB_PTR_NULL; + } + int32_t ret = store_->Delete(deleteRows, table, whereClause, bindArgs); + if (ret == E_SQLITE_CORRUPT) { + HILOGE("database corrupt ret:%{public}d", ret); + int32_t restoreRet = store_->Restore(""); + if (restoreRet != E_OK) { + HILOGE("Restore failed restoreRet:%{public}d", restoreRet); + return DP_RDB_DATABASE_RESTORE_FAIL; + } + ret = store_->Delete(deleteRows, table, whereClause, bindArgs); + } + if (ret != E_OK) { + HILOGE("ProfileDatardbAdapter delete failed ret:%{public}d", ret); + return DP_RDBADAPTER_DELETE_FAIL; + } + } + return DP_SUCCESS; +} + +int32_t ProfileDataRdbAdapter::Update(int32_t& changedRows, const std::string& table, const ValuesBucket& values, + const std::string& whereClause, const std::vector& bindArgs) +{ + if (TABLES.find(table) == TABLES.end()) { + HILOGE("table does not exist"); + return DP_RDBADAPTER_TABLE_NOT_EXIST; + } + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + if (store_ == nullptr) { + HILOGE("RDBStore_ is null"); + return DP_RDB_DB_PTR_NULL; + } + int32_t ret = store_->Update(changedRows, table, values, whereClause, bindArgs); + if (ret == E_SQLITE_CORRUPT) { + HILOGE("database corrupt ret:%{public}d", ret); + int32_t restoreRet = store_->Restore(""); + if (restoreRet != E_OK) { + HILOGE("Restore failed restoreRet:%{public}d", restoreRet); + return DP_RDB_DATABASE_RESTORE_FAIL; + } + ret = store_->Update(changedRows, table, values, whereClause, bindArgs); + } + if (ret != E_OK) { + HILOGE("ProfileDatardbAdapter update failed ret:%{public}d", ret); + return DP_RDBADAPTER_UPDATE_FAIL; + } + } + return DP_SUCCESS; +} + +std::shared_ptr ProfileDataRdbAdapter::Get(const std::string& sql, const std::vector& args) +{ + std::shared_ptr resultSet = nullptr; + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + if (store_ == nullptr) { + HILOGE("RDBStore_ is null"); + return nullptr; + } + resultSet = store_->QueryByStep(sql, args); + if (resultSet == nullptr) { + HILOGE("resultSet is null"); + return nullptr; + } + int32_t rowCount = ROWCOUNT_INIT; + int32_t ret = resultSet->GetRowCount(rowCount); + if (ret == E_SQLITE_CORRUPT) { + HILOGE("database corrupt ret:%{public}d", ret); + resultSet->Close(); + ret = store_->Restore(""); + if (ret != E_OK) { + HILOGE("Restore failed ret:%{public}d", ret); + return nullptr; + } + resultSet = store_->QueryByStep(sql, args); + } + } + return resultSet; +} + +int32_t ProfileDataRdbAdapter::GetRDBPtr() +{ + int32_t version = RDB_VERSION; + ProfileDataOpenCallback helper; + RdbStoreConfig config(PROFILE_DATA_RDB_PATH + PROFILE_DATA_DATABASE_NAME); + config.SetSecurityLevel(SecurityLevel::S2); + config.SetHaMode(HAMode::MAIN_REPLICA); + config.SetAllowRebuild(true); + int32_t errCode = E_OK; + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + store_ = RdbHelper::GetRdbStore(config, version, helper, errCode); + if (store_ == nullptr) { + HILOGE("RDBStore_ is null"); + return DP_RDB_DB_PTR_NULL; + } + NativeRdb::RebuiltType rebuiltType = NativeRdb::RebuiltType::NONE; + errCode = store_->GetRebuilt(rebuiltType); + if (errCode != E_OK) { + HILOGE("getRDBPtr failed errCode:%{public}d", errCode); + return DP_GET_RDBSTORE_FAIL; + } + if (rebuiltType == NativeRdb::RebuiltType::REBUILT) { + HILOGE("database corrupt"); + int32_t restoreRet = store_->Restore(""); + if (restoreRet != E_OK) { + HILOGE("Restore failed restoreRet:%{public}d", restoreRet); + return DP_RDB_DATABASE_RESTORE_FAIL; + } + } + } + return DP_SUCCESS; +} + +bool ProfileDataRdbAdapter::IsInit() +{ + if (store_ == nullptr) { + return false; + } + return true; +} + +int32_t ProfileDataRdbAdapter::CreateTable(const std::string& sql) +{ + { + std::lock_guard lock(ProfileDataRdbAdapterMtx_); + if (store_ == nullptr) { + HILOGE("RDBStore_ is null"); + return DP_RDB_DB_PTR_NULL; + } + if (store_->ExecuteSql(sql) != E_OK) { + HILOGE("ProfileDatardbAdapter create table failed"); + return DP_RDBADAPTER_TABLE_NOT_EXIST; + } + } + return DP_SUCCESS; +} + +int32_t ProfileDataOpenCallback::OnCreate(RdbStore& store) +{ + HILOGI("rdbStore create"); + return NativeRdb::E_OK; +} + +int32_t ProfileDataOpenCallback::OnUpgrade(RdbStore& store, int oldVersion, int newVersion) +{ + HILOGI("rdbStore upgrade"); + return NativeRdb::E_OK; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/src/profiledatamanager/device_profile_dao.cpp b/services/core/src/profiledatamanager/device_profile_dao.cpp new file mode 100644 index 00000000..d60383c2 --- /dev/null +++ b/services/core/src/profiledatamanager/device_profile_dao.cpp @@ -0,0 +1,324 @@ +/* + * 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 "content_sensor_manager_utils.h" +#include "device_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 "dp_services_constants.h" +#include "profile_utils.h" +#include "subscribe_profile_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(DeviceProfileDao); +namespace { + const std::string TAG = "DeviceProfileDao"; +} + +int32_t DeviceProfileDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceProfileDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceProfileDao::PutDeviceProfile(const DeviceProfile& deviceProfile) +{ + ValuesBucket values; + DeviceProfileToEntries(deviceProfile, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, DEVICE_PROFILE_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("device_profile insert failed"); + return DP_PUT_TRUST_DEVICE_PROFILE_FAIL; + } + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get( + SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID_ACCOUNTID, + std::vector{ ValueObject(deviceProfile.GetDeviceId()), + ValueObject(deviceProfile.GetUserId()), ValueObject(deviceProfile.GetAccountId())}); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_FAIL; + } + DeviceProfile profile(deviceProfile); + 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); + profile.SetId(id); + } + resultSet->Close(); + SubscribeProfileManager::GetInstance().NotifyDeviceProfileAdd(profile); + std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); + if (localUdid == deviceProfile.GetDeviceId() ) { + DeviceProfileManager::GetInstance().PutDeviceProfile(profile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceProfileDao::GetDeviceProfiles(const DeviceProfileFilterOptions &filterOptions, + std::vector &deviceProfiles) +{ + std::string sql; + std::vector condition; + CreateQuerySqlAndCondition(filterOptions, sql, condition); + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + DeviceProfile deviceProfile; + ConvertToDeviceProfile(resultSet, deviceProfile); + deviceProfiles.emplace_back(deviceProfile); + } + resultSet->Close(); + if (deviceProfiles.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceProfileDao::DeleteDeviceProfile(const DeviceProfile &deviceProfile) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, DEVICE_PROFILE_TABLE, ID_EQUAL_CONDITION, + std::vector{ ValueObject(deviceProfile.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete device_profile data failed"); + return DP_DELETE_TRUST_DEVICE_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyDeviceProfileDelete(deviceProfile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &oldProfile, const DeviceProfile &newProfile) +{ + ValuesBucket values; + DeviceProfileToEntries(newProfile, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update( + changeRowCnt, DEVICE_PROFILE_TABLE, values, ID_EQUAL_CONDITION, + std::vector{ ValueObject(newProfile.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update device_profile table failed"); + return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(oldProfile, newProfile); + } + std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); + if (localUdid == newProfile.GetDeviceId()) { + DeviceProfileManager::GetInstance().PutDeviceProfile(newProfile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceProfileDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_PROFILE_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("device_profile create failed"); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t DeviceProfileDao::CreateIndex() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("device_profile unique index create failed"); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + +void DeviceProfileDao::CreateQuerySqlAndCondition(const DeviceProfileFilterOptions &filterOptions, + std::string &sql, std::vector &condition) +{ + sql = SELECT_DEVICE_PROFILE_TABLE; + bool flag = false; + if (!filterOptions.GetDeviceProfileIds().empty()) { + sql += "deviceProfileId IN("; + std::vector deviceProfileIds = filterOptions.GetDeviceProfileIds(); + for (auto deviceProfileId : deviceProfileIds) { + sql += "?,"; + condition.emplace_back(ValueObject(deviceProfileId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (!filterOptions.GetDeviceIds().empty()) { + sql += "deviceId IN("; + std::vector deviceIds = filterOptions.GetDeviceIds(); + for (auto deviceId : deviceIds) { + sql += "?,"; + condition.emplace_back(ValueObject(deviceId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (filterOptions.GetUserId() != DEFAULT_USER_ID) { + sql += "userId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + flag = true; + } + if (!filterOptions.GetAccountId().empty()) { + sql += "accountId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (!filterOptions.GetWiseDeviceIds().empty()) { + sql += "wiseDeviceId IN("; + std::vector wiseDeviceIds = filterOptions.GetWiseDeviceIds(); + for (auto wiseDeviceId : wiseDeviceIds) { + sql += "?,"; + condition.emplace_back(ValueObject(wiseDeviceId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (flag) { + sql.erase(sql.end() - 4); + return; + } + sql.erase(sql.end() - 6); + return; +} + +int32_t DeviceProfileDao::DeviceProfileToEntries(const DeviceProfile &deviceProfile, ValuesBucket &values) +{ + // values.PutInt(ID, deviceProfile.GetId()); + values.PutString(DEVICE_ID, deviceProfile.GetDeviceId()); + values.PutString(DEVICE_MODEL, deviceProfile.GetDeviceModel()); + values.PutString(DEV_TYPE, deviceProfile.GetDevType()); + values.PutString(MANU, deviceProfile.GetManu()); + values.PutString(SN, deviceProfile.GetSn()); + values.PutString(PRODUCT_ID, deviceProfile.GetProductId()); + values.PutString(SUB_PRODUCT_ID, deviceProfile.GetSubProductId()); + values.PutString(HIV, deviceProfile.GetHiv()); + values.PutString(MAC, deviceProfile.GetMac()); + values.PutString(FWV, deviceProfile.GetFwv()); + values.PutString(HWV, deviceProfile.GetHwv()); + values.PutString(SWV, deviceProfile.GetSwv()); + values.PutInt(PROT_TYPE, deviceProfile.GetProtType()); + values.PutString(DEVICE_NAME, deviceProfile.GetDeviceName()); + values.PutString(WISE_USER_ID, deviceProfile.GetWiseUserId()); + values.PutString(WISE_DEVICE_ID, deviceProfile.GetWiseDeviceId()); + values.PutString(ROOM_NAME, deviceProfile.GetRoomName()); + values.PutString(REGISTER_TIME, deviceProfile.GetRegisterTime()); + values.PutInt(MODIFY_TIME, deviceProfile.GetModifyTime()); + values.PutString(SHARE_TIME, deviceProfile.GetShareTime()); + values.PutString(PRODUCTOR_INFO_VERSION, deviceProfile.GetProductorInfoVersion()); + values.PutInt(USERID, deviceProfile.GetUserId()); + values.PutString(ACCOUNTID, deviceProfile.GetAccountId()); + return DP_SUCCESS; +} + +int32_t DeviceProfileDao::ConvertToDeviceProfile( + std::shared_ptr resultSet, DeviceProfile& deviceProfile) +{ + 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; + } + deviceProfile.SetId(rowEntity.Get(ID)); + deviceProfile.SetDeviceId(rowEntity.Get(DEVICE_ID)); + deviceProfile.SetDeviceModel(rowEntity.Get(DEVICE_MODEL)); + deviceProfile.SetDevType(rowEntity.Get(DEV_TYPE)); + deviceProfile.SetManu(rowEntity.Get(MANU)); + deviceProfile.SetSn(rowEntity.Get(SN)); + deviceProfile.SetProductId(rowEntity.Get(PRODUCT_ID)); + deviceProfile.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID)); + deviceProfile.SetHiv(rowEntity.Get(HIV)); + deviceProfile.SetMac(rowEntity.Get(MAC)); + deviceProfile.SetFwv(rowEntity.Get(FWV)); + deviceProfile.SetHwv(rowEntity.Get(HWV)); + deviceProfile.SetSwv(rowEntity.Get(SWV)); + deviceProfile.SetProtType(rowEntity.Get(PROT_TYPE)); + deviceProfile.SetDeviceName(rowEntity.Get(DEVICE_NAME)); + deviceProfile.SetWiseUserId(rowEntity.Get(WISE_USER_ID)); + deviceProfile.SetWiseDeviceId(rowEntity.Get(WISE_DEVICE_ID)); + deviceProfile.SetRoomName(rowEntity.Get(ROOM_NAME)); + deviceProfile.SetRegisterTime(rowEntity.Get(REGISTER_TIME)); + deviceProfile.SetModifyTime(rowEntity.Get(MODIFY_TIME)); + deviceProfile.SetShareTime(rowEntity.Get(SHARE_TIME)); + deviceProfile.SetProductorInfoVersion(rowEntity.Get(PRODUCTOR_INFO_VERSION)); + deviceProfile.SetUserId(rowEntity.Get(USERID)); + deviceProfile.SetAccountId(rowEntity.Get(ACCOUNTID)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp index 06cebceb..6528fd4a 100644 --- a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp +++ b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp @@ -514,8 +514,69 @@ int32_t SubscribeProfileManager::NotifyCharProfileDelete(const std::string& dbKe } return DP_SUCCESS; } + +int32_t SubscribeProfileManager::NotifyDeviceProfileAdd(const DeviceProfile &deviceProfile) +{ + auto subscriberInfos = GetSubscribeInfos(DEVICE_PROFILE_TABLE); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", deviceProfile.AnnoymizeDump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::DEVICE_PROFILE_ADD) != 0) { + listenerProxy->OnDeviceProfileAdd(deviceProfile); + } + } + return DP_SUCCESS; +} + +int32_t SubscribeProfileManager::NotifyDeviceProfileUpdate(const DeviceProfile &oldProfile, const DeviceProfile &newProfile) +{ + auto subscriberInfos = GetSubscribeInfos(DEVICE_PROFILE_TABLE); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", newProfile.AnnoymizeDump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::DEVICE_PROFILE_UPDATE) != 0) { + listenerProxy->OnDeviceProfileUpdate(oldProfile, newProfile); + } + } + return DP_SUCCESS; +} + +int32_t SubscribeProfileManager::NotifyDeviceProfileDelete(const DeviceProfile &deviceProfile) +{ + auto subscriberInfos = GetSubscribeInfos(DEVICE_PROFILE_TABLE); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", deviceProfile.AnnoymizeDump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE) != 0) { + listenerProxy->OnDeviceProfileDelete(deviceProfile); + } + } + return DP_SUCCESS; +} + std::unordered_set SubscribeProfileManager::GetSubscribeInfos( - const std::string& dbKey) + const std::string &dbKey) { { std::lock_guard lock(subscribeMutex_); diff --git a/services/core/test/unittest/device_profile_manager_test.cpp b/services/core/test/unittest/device_profile_manager_test.cpp index 29c3185c..4de5faa6 100644 --- a/services/core/test/unittest/device_profile_manager_test.cpp +++ b/services/core/test/unittest/device_profile_manager_test.cpp @@ -162,8 +162,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile001, TestSize.Level1) DeviceProfile deviceProfile; ContentSensorManagerUtils::GetInstance().localUdid_ = "anything"; deviceProfile.SetDeviceId("anything"); - deviceProfile.SetDeviceTypeName("anything"); - deviceProfile.SetDeviceTypeId(0); deviceProfile.SetDeviceName("anything"); deviceProfile.SetManufactureName("anything"); deviceProfile.SetDeviceModel("anything"); @@ -187,8 +185,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile002, TestSize.Level1) { DeviceProfile deviceProfile; deviceProfile.SetDeviceId(""); - deviceProfile.SetDeviceTypeName("anything"); - deviceProfile.SetDeviceTypeId(0); deviceProfile.SetDeviceName("anything"); deviceProfile.SetManufactureName("anything"); deviceProfile.SetDeviceModel("anything"); @@ -213,8 +209,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile003, TestSize.Level1) DeviceProfile deviceProfile1; ContentSensorManagerUtils::GetInstance().localUdid_ = "anything1"; deviceProfile1.SetDeviceId("anything1"); - deviceProfile1.SetDeviceTypeName("anything"); - deviceProfile1.SetDeviceTypeId(0); deviceProfile1.SetDeviceName("anything"); deviceProfile1.SetManufactureName("anything"); deviceProfile1.SetDeviceModel("anything"); @@ -226,8 +220,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile003, TestSize.Level1) DeviceProfile deviceProfile2; deviceProfile2.SetDeviceId("anything1"); - deviceProfile2.SetDeviceTypeName("anything"); - deviceProfile2.SetDeviceTypeId(0); deviceProfile2.SetDeviceName("anything"); deviceProfile2.SetManufactureName("anything"); deviceProfile2.SetDeviceModel("anything"); @@ -252,8 +244,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile004, TestSize.Level1) { DeviceProfile deviceProfile10; deviceProfile10.SetDeviceId("anything10"); - deviceProfile10.SetDeviceTypeName("anything"); - deviceProfile10.SetDeviceTypeId(0); deviceProfile10.SetDeviceName("anything"); deviceProfile10.SetManufactureName("anything"); deviceProfile10.SetDeviceModel("anything"); @@ -279,8 +269,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile005, TestSize.Level1) DeviceProfile deviceProfile11; ContentSensorManagerUtils::GetInstance().localUdid_ = "anything11"; deviceProfile11.SetDeviceId("anything11"); - deviceProfile11.SetDeviceTypeName("anything"); - deviceProfile11.SetDeviceTypeId(0); deviceProfile11.SetDeviceName("anything"); deviceProfile11.SetManufactureName("anything"); deviceProfile11.SetDeviceModel("anything"); @@ -309,8 +297,6 @@ HWTEST_F(DeviceProfileManagerTest, PutDeviceProfile006, TestSize.Level1) DeviceProfile deviceProfile; ContentSensorManagerUtils::GetInstance().localUdid_ = "anything111"; deviceProfile.SetDeviceId("anything111"); - deviceProfile.SetDeviceTypeName("anything"); - deviceProfile.SetDeviceTypeId(0); deviceProfile.SetDeviceName("anything"); deviceProfile.SetManufactureName("anything"); deviceProfile.SetDeviceModel("anything"); @@ -715,8 +701,6 @@ HWTEST_F(DeviceProfileManagerTest, GetDeviceProfile001, TestSize.Level1) ContentSensorManagerUtils::GetInstance().localUdid_ = "deviceId"; string deviceId = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); deviceProfile2.SetDeviceId(deviceId); - deviceProfile2.SetDeviceTypeName("anything"); - deviceProfile2.SetDeviceTypeId(0); deviceProfile2.SetDeviceName("anything"); deviceProfile2.SetManufactureName("anything"); deviceProfile2.SetDeviceModel("anything"); @@ -733,8 +717,6 @@ HWTEST_F(DeviceProfileManagerTest, GetDeviceProfile001, TestSize.Level1) EXPECT_EQ(ret, DP_SUCCESS); outDeviceProfile.SetDeviceId(deviceId); string outDeviceId = outDeviceProfile.GetDeviceId(); - outDeviceProfile.GetDeviceTypeName(); - outDeviceProfile.GetDeviceTypeId(); outDeviceProfile.GetDeviceName(); outDeviceProfile.GetManufactureName(); outDeviceProfile.GetDeviceModel(); @@ -1373,8 +1355,6 @@ HWTEST_F(DeviceProfileManagerTest, DeviceProfileMarshalling001, TestSize.Level1) OHOS::MessageParcel data; DeviceProfile deviceProfile; deviceProfile.SetDeviceId("anything"); - deviceProfile.SetDeviceTypeName("anything"); - deviceProfile.SetDeviceTypeId(0); deviceProfile.SetDeviceName("anything"); deviceProfile.SetManufactureName("anything"); deviceProfile.SetDeviceModel("anything"); @@ -1401,8 +1381,6 @@ HWTEST_F(DeviceProfileManagerTest, DeviceProfileOperator001, TestSize.Level1) { DeviceProfile deviceProfile1; deviceProfile1.SetDeviceId("anything1"); - deviceProfile1.SetDeviceTypeName("anything1"); - deviceProfile1.SetDeviceTypeId(0); deviceProfile1.SetDeviceName("anything1"); deviceProfile1.SetManufactureName("anything1"); deviceProfile1.SetDeviceModel("anything1"); @@ -1414,8 +1392,6 @@ HWTEST_F(DeviceProfileManagerTest, DeviceProfileOperator001, TestSize.Level1) DeviceProfile deviceProfile2; deviceProfile2.SetDeviceId("anything2"); - deviceProfile2.SetDeviceTypeName("anything2"); - deviceProfile2.SetDeviceTypeId(0); deviceProfile2.SetDeviceName("anything2"); deviceProfile2.SetManufactureName("anything2"); deviceProfile2.SetDeviceModel("anything2"); @@ -1439,8 +1415,6 @@ HWTEST_F(DeviceProfileManagerTest, DeviceProfileDump001, TestSize.Level1) { DeviceProfile deviceProfile; deviceProfile.SetDeviceId("anything"); - deviceProfile.SetDeviceTypeName("anything"); - deviceProfile.SetDeviceTypeId(0); deviceProfile.SetDeviceName("anything"); deviceProfile.SetManufactureName("anything"); deviceProfile.SetDeviceModel("anything"); @@ -1500,8 +1474,6 @@ HWTEST_F(DeviceProfileManagerTest, GetInKvDB001, TestSize.Level1) string deviceId = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); DeviceProfile deviceProfile2; deviceProfile2.SetDeviceId(deviceId); - deviceProfile2.SetDeviceTypeName("GetInKvDB001_DeviceTypeName"); - deviceProfile2.SetDeviceTypeId(0); deviceProfile2.SetDeviceName("GetInKvDB001_DeviceName"); deviceProfile2.SetManufactureName("GetInKvDB001_ManufactureName"); deviceProfile2.SetDeviceModel("GetInKvDB001_DeviceModel"); @@ -1579,8 +1551,6 @@ HWTEST_F(DeviceProfileManagerTest, GetInKvDB002, TestSize.Level1) string deviceId = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); DeviceProfile deviceProfile2; deviceProfile2.SetDeviceId(deviceId); - deviceProfile2.SetDeviceTypeName("GetInKvDB001_DeviceTypeName2"); - deviceProfile2.SetDeviceTypeId(0); deviceProfile2.SetDeviceName("GetInKvDB001_DeviceName2"); deviceProfile2.SetManufactureName("GetInKvDB001_ManufactureName2"); deviceProfile2.SetDeviceModel("GetInKvDB001_DeviceModel2"); diff --git a/services/core/test/unittest/distributed_device_profile_client_kv_test.cpp b/services/core/test/unittest/distributed_device_profile_client_kv_test.cpp index 55c33d4f..b70e955d 100644 --- a/services/core/test/unittest/distributed_device_profile_client_kv_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_client_kv_test.cpp @@ -272,8 +272,6 @@ HWTEST_F(DistributedDeviceProfileClientKvTest, GetDeviceProfile001, TestSize.Lev DeviceProfile deviceProfile1; deviceProfile1.SetDeviceId("anything"); - deviceProfile1.SetDeviceTypeName("anything"); - deviceProfile1.SetDeviceTypeId(0); deviceProfile1.SetDeviceName("anything"); deviceProfile1.SetManufactureName("anything"); deviceProfile1.SetDeviceModel("anything"); @@ -284,8 +282,6 @@ HWTEST_F(DistributedDeviceProfileClientKvTest, GetDeviceProfile001, TestSize.Lev deviceProfile1.SetOsType(1); deviceProfile1.GetDeviceId(); - deviceProfile1.GetDeviceTypeName(); - deviceProfile1.GetDeviceTypeId(); deviceProfile1.GetDeviceName(); deviceProfile1.GetManufactureName(); deviceProfile1.GetDeviceModel(); diff --git a/services/core/test/unittest/profile_cache_test.cpp b/services/core/test/unittest/profile_cache_test.cpp index a01acbdf..9cb605a3 100644 --- a/services/core/test/unittest/profile_cache_test.cpp +++ b/services/core/test/unittest/profile_cache_test.cpp @@ -310,8 +310,6 @@ HWTEST_F(ProfileCacheTest, IsDeviceProfileExist_001, TestSize.Level2) DeviceProfile deviceProfile1; deviceProfile1.SetDeviceId("anything1"); - deviceProfile1.SetDeviceTypeName("anything"); - deviceProfile1.SetDeviceTypeId(0); deviceProfile1.SetDeviceName("anything"); deviceProfile1.SetManufactureName("anything"); deviceProfile1.SetDeviceModel("anything"); diff --git a/services/core/test/unittest/profile_control_utils_test.cpp b/services/core/test/unittest/profile_control_utils_test.cpp index 0ed7a522..90144ed5 100644 --- a/services/core/test/unittest/profile_control_utils_test.cpp +++ b/services/core/test/unittest/profile_control_utils_test.cpp @@ -137,8 +137,6 @@ HWTEST_F(ProfileControlUtilsTest, PutDeviceProfile004, TestSize.Level1) DeviceProfile deviceProfile1; ContentSensorManagerUtils::GetInstance().localUdid_ = "anything1"; deviceProfile1.SetDeviceId("anything1"); - deviceProfile1.SetDeviceTypeName("anything"); - deviceProfile1.SetDeviceTypeId(0); deviceProfile1.SetDeviceName("anything"); deviceProfile1.SetManufactureName("anything"); deviceProfile1.SetDeviceModel("anything"); -- Gitee From 105fc484dc7fde4bb6b305aa6e97384f7b3df5d3 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Sat, 14 Dec 2024 19:01:31 +0800 Subject: [PATCH 04/55] update Signed-off-by: zhanglei --- common/include/interfaces/device_profile_filter_options.h | 2 +- .../persistenceadapter/rdbadapter/profile_data_rdb_adapter.h | 2 +- services/core/include/profiledatamanager/device_profile_dao.h | 2 +- services/core/src/profiledatamanager/device_profile_dao.cpp | 2 +- .../src/subscribeprofilemanager/subscribe_profile_manager.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common/include/interfaces/device_profile_filter_options.h b/common/include/interfaces/device_profile_filter_options.h index 60b341b8..1e5328c0 100644 --- a/common/include/interfaces/device_profile_filter_options.h +++ b/common/include/interfaces/device_profile_filter_options.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * 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 diff --git a/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h b/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h index 9696849d..ebabbf68 100644 --- a/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h +++ b/services/core/include/persistenceadapter/rdbadapter/profile_data_rdb_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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 diff --git a/services/core/include/profiledatamanager/device_profile_dao.h b/services/core/include/profiledatamanager/device_profile_dao.h index 2b030e5c..c215722f 100644 --- a/services/core/include/profiledatamanager/device_profile_dao.h +++ b/services/core/include/profiledatamanager/device_profile_dao.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * 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 diff --git a/services/core/src/profiledatamanager/device_profile_dao.cpp b/services/core/src/profiledatamanager/device_profile_dao.cpp index d60383c2..57f689db 100644 --- a/services/core/src/profiledatamanager/device_profile_dao.cpp +++ b/services/core/src/profiledatamanager/device_profile_dao.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * 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 diff --git a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp index 6528fd4a..0d86642c 100644 --- a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp +++ b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp @@ -568,7 +568,7 @@ int32_t SubscribeProfileManager::NotifyDeviceProfileDelete(const DeviceProfile & HILOGE("Cast to IProfileChangeListener failed!"); continue; } - if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE) != 0) { + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::DEVICE_PROFILE_DELETE) != 0) { listenerProxy->OnDeviceProfileDelete(deviceProfile); } } -- Gitee From 299d4b821e3962b6746a1cda1f9c3cfacd7f53a6 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Wed, 11 Dec 2024 14:59:12 +0800 Subject: [PATCH 05/55] add serviceProfile_Dao Signed-off-by: wangzhaohao --- common/BUILD.gn | 1 + .../distributed_device_profile_constants.h | 15 + .../distributed_device_profile_errors.h | 6 + common/include/interfaces/service_profile.h | 6 + .../service_profile_filter_opotions.h | 68 +++++ .../distributed_device_profile_constants.cpp | 15 + common/src/interfaces/service_profile.cpp | 29 +- .../service_profile_filter_opotions.cpp | 177 ++++++++++++ services/core/BUILD.gn | 1 + .../include/profiledao/service_profile_dao.h | 47 ++++ .../subscribe_profile_manager.h | 1 + .../src/profiledao/service_profile_dao.cpp | 257 ++++++++++++++++++ .../subscribe_profile_manager.cpp | 20 ++ 13 files changed, 642 insertions(+), 1 deletion(-) create mode 100644 common/include/interfaces/service_profile_filter_opotions.h create mode 100644 common/src/interfaces/service_profile_filter_opotions.cpp create mode 100644 services/core/include/profiledao/service_profile_dao.h create mode 100644 services/core/src/profiledao/service_profile_dao.cpp diff --git a/common/BUILD.gn b/common/BUILD.gn index 2f7ec0e2..42ce4d46 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -59,6 +59,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/profile_change_listener_proxy.cpp", "src/interfaces/profile_change_listener_stub.cpp", "src/interfaces/service_profile.cpp", + "src/interfaces/service_profile_filter_opotions.cpp", "src/interfaces/sync_completed_callback_proxy.cpp", "src/interfaces/sync_completed_callback_stub.cpp", "src/interfaces/trust_device_profile.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index bee61c75..a20c9098 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -45,7 +45,10 @@ extern const std::string OS_TYPE; extern const std::string OH_PROFILE_SUFFIX; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; +extern const std::string SERVICE_ID; extern const std::string SERVICE_TYPE; +extern const std::string SERVICE_PROFILE_TABLE; +extern const std::string RDB_USER_ID; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; @@ -83,6 +86,11 @@ extern const std::string ACCESSEE_TOKEN_ID; extern const std::string ACCESSEE_BUNDLE_NAME; extern const std::string ACCESSEE_HAP_SIGNATURE; extern const std::string ACCESSEE_BIND_LEVEL; +/* ServiceProfile Attribute */ +extern const std::string SERVICE_PROFILE_ID; +extern const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID; +extern const std::string SERVICE_PROFILE_SERVICE_ID; +extern const std::string SERVICE_PROFILE_SERVICE_TYPE; /* subscribe info */ extern const std::string SA_ID; extern const std::string SUBSCRIBE_KEY; @@ -146,6 +154,8 @@ extern const std::string SVR_PREFIX; extern const std::string CHAR_PREFIX; extern const std::string USER_ID; extern const std::string TOKEN_ID; +extern const std::string ID; +extern const std::string DEVICE_PROFILE_ID; extern const std::string ALL_PROC; constexpr int32_t NUM_1 = 1; constexpr int32_t NUM_2 = 2; @@ -154,6 +164,8 @@ constexpr int32_t NUM_4 = 4; constexpr int32_t NUM_5 = 5; constexpr int32_t NUM_6 = 6; constexpr int32_t DEFAULT_USER_ID = -1; +constexpr int32_t DEFAULT_SERVICE_PROFILE_ID = -1; +constexpr int32_t DEFAULT_DEVICE_PROFILE_ID = -1; constexpr uint32_t NUM_1U = 1; constexpr uint32_t NUM_8U = 8; constexpr uint16_t CUR_SWITCH_LEN = 3; @@ -245,6 +257,9 @@ extern const std::string SELECT_ACCESSEE_TABLE_WHERE_ALL; extern const std::string SELECT_ACCESSER_TABLE_WHERE_ACCESSERDEVICEID_AND_ACCESSERUSERID; extern const std::string SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEDEVICEID_AND_ACCESSEEUSERID; extern const std::string SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSERID_AND_ACCESSEEID; +/* profileData */ +extern const std::string SELECT_SERVICE_PROGILES; +extern const std::string ID_EQUAL_CONDITION; /* SubscribeTrustInfoManager */ extern const std::string SUBSCRIBE_TRUST_INFO_TABLE; extern const std::string CREATE_SUBSCRIBE_TRUST_INFO_TABLE_SQL; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 86fa4d08..ee27a0ce 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -160,6 +160,12 @@ 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; + +// profile_data +constexpr int32_t DP_RDB_PUT_SERVICE_PROFILE_FAIL = 98566284; +constexpr int32_t DP_RDB_DELETE_SERVICE_PROFILE_FAIL = 98566285; +constexpr int32_t DP_RDB_UPDATE_SERVICE_PROFILE_FAIL = 98566286; +constexpr int32_t DP_RDB_GET_SERVICE_PROFILE_FAIL = 98566287; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/service_profile.h b/common/include/interfaces/service_profile.h index b8ff73bd..cc71ff81 100644 --- a/common/include/interfaces/service_profile.h +++ b/common/include/interfaces/service_profile.h @@ -41,6 +41,10 @@ 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 GetDeviceProfileId() const; + void SetDeviceProfileId(int32_t deviceProfileId); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const ServiceProfile& serviceProfile) const; @@ -52,6 +56,8 @@ private: std::string serviceType_ = ""; bool isMultiUser_ = false; int32_t userId_ = DEFAULT_USER_ID; + int32_t id_ = DEFAULT_SERVICE_PROFILE_ID; + int32_t deviceProfileId_ = DEFAULT_DEVICE_PROFILE_ID; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/service_profile_filter_opotions.h b/common/include/interfaces/service_profile_filter_opotions.h new file mode 100644 index 00000000..5a124934 --- /dev/null +++ b/common/include/interfaces/service_profile_filter_opotions.h @@ -0,0 +1,68 @@ +/* + * 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_SERVICE_PROFILE_FILTER_OPTIONS_H +#define OHOS_DP_SERVICE_PROFILE_FILTER_OPTIONS_H + +#include +#include +#include + +#include "distributed_device_profile_constants.h" +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceProfileFilterOptions : public DpParcel { +public: + ServiceProfileFilterOptions(int32_t userId, std::string& accountId, const std::vector& deviceIds, + const std::vector& wiseDeviceIds, const std::vector& serviceIds, + const std::vector& serviceProfileIds, const std::vector& deviceProfileIds); + ServiceProfileFilterOptions(); + ~ServiceProfileFilterOptions(); + + int32_t GetUserId() const; + void SetUserId(int32_t userId); + const std::string& GetAccountId() const; + void SetAccountId(const std::string& accountId); + const std::vector& GetDeviceIds() const; + void SetDeviceIds(const std::vector& deviceIds); + const std::vector& GetWiseDeviceIds() const; + void SetWiseDeviceIds(const std::vector& wiseDeviceIds); + const std::vector& GetServiceIds() const; + void SetServiceIds(const std::vector& serviceIds); + const std::vector& GetServiceProfileIds() const; + void SetServiceProfileIds(const std::vector& serviceProfileIds); + const std::vector& GetDeviceProfileIds() const; + void SetDeviceProfileIds(const std::vector& deviceProfileIds); + bool IsEmpty() const; + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + + bool operator==(const ServiceProfileFilterOptions& rhs) const; + +private: + int32_t userId_ = DEFAULT_USER_ID; + std::string accountId_ = ""; + std::vector deviceIds_; + std::vector wiseDeviceIds_; + std::vector serviceIds_; + std::vector serviceProfileIds_; + std::vector deviceProfileIds_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif //OHOS_DP_SERVICE_PROFILE_FILTER_OPTIONS_H diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index 7414d4bb..f77d27a7 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -35,7 +35,10 @@ const std::string OS_TYPE = "osType"; const std::string OH_PROFILE_SUFFIX = "_OH"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; +const std::string SERVICE_ID = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; +const std::string SERVICE_PROFILE_TABLE = "service_profile"; +const std::string RDB_USER_ID = "userId"; /* CharacteristicProfile Attribute */ const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; @@ -73,6 +76,11 @@ const std::string ACCESSEE_TOKEN_ID = "accesseeTokenId"; const std::string ACCESSEE_BUNDLE_NAME = "accesseeBundleName"; const std::string ACCESSEE_HAP_SIGNATURE = "accesseeHapSignature"; const std::string ACCESSEE_BIND_LEVEL = "accesseeBindLevel"; +/* ServiceProfile Attribute */ +const std::string SERVICE_PROFILE_ID = "id"; +const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID = "deviceProfileId"; +const std::string SERVICE_PROFILE_SERVICE_ID = "serviceId"; +const std::string SERVICE_PROFILE_SERVICE_TYPE = "serviceType"; /* subscribe info */ const std::string SA_ID = "saId"; const std::string SUBSCRIBE_KEY = "subscribeKey"; @@ -108,6 +116,8 @@ const std::string DEV_PREFIX = "dev"; const std::string SVR_PREFIX = "svr"; const std::string CHAR_PREFIX = "char"; const std::string USER_ID = "user_id"; +const std::string ID = "id"; +const std::string DEVICE_PROFILE_ID = "deviceProfile_id"; const std::string TOKEN_ID = "token_id"; const std::string ALL_PROC = "all"; const std::string DP_PKG_NAME = "ohos.deviceprofile"; @@ -286,6 +296,11 @@ const std::string SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEDEVICEID_AND_ACCESSEEUSERI "SELECT * FROM accessee_table WHERE accesseeDeviceId = ? and accesseeUserId = ? "; const std::string SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSERID_AND_ACCESSEEID = "SELECT * FROM access_control_table WHERE accesserId = ? and accesseeId = ? "; +/* profileData */ +const std::string SELECT_SERVICE_PROGILES = + "SELECT sp.*, dp.udid, dp.userId FROM service_profile sp \ + LEFT JOIN device_profile dp ON dp.id=sp.deviceProfileId WHERE "; +const std::string ID_EQUAL_CONDITION = "id = ?"; /* SubscribeTrustInfoManager */ const std::string SUBSCRIBE_TRUST_INFO_TABLE = "subscribe_trust_info_table"; const std::string CREATE_SUBSCRIBE_TRUST_INFO_TABLE_SQL = diff --git a/common/src/interfaces/service_profile.cpp b/common/src/interfaces/service_profile.cpp index d4bc2272..95a103bd 100644 --- a/common/src/interfaces/service_profile.cpp +++ b/common/src/interfaces/service_profile.cpp @@ -93,6 +93,26 @@ void ServiceProfile::SetUserId(int32_t userId) userId_ = userId; } +int32_t ServiceProfile::GetId() const +{ + return id_; +} + +void ServiceProfile::SetId(int32_t id) +{ + id_ = id; +} + +int32_t ServiceProfile::GetDeviceProfileId() const +{ + return deviceProfileId_; +} + +void ServiceProfile::SetDeviceProfileId(int32_t deviceProfileId) +{ + deviceProfileId_ = deviceProfileId; +} + bool ServiceProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, deviceId_, false); @@ -100,6 +120,8 @@ bool ServiceProfile::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, serviceType_, 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, deviceProfileId_, false); return true; } @@ -110,6 +132,8 @@ bool ServiceProfile::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, serviceType_, 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, deviceProfileId_, false); return true; } @@ -117,7 +141,8 @@ bool ServiceProfile::operator!=(const ServiceProfile& serviceProfile) const { bool isNotEqual = (deviceId_ != serviceProfile.GetDeviceId() || serviceName_ != serviceProfile.GetServiceName() || serviceType_ != serviceProfile.GetServiceType() || isMultiUser_ != serviceProfile.IsMultiUser() || - userId_ != serviceProfile.GetUserId()); + userId_ != serviceProfile.GetUserId() || id_ != serviceProfile.GetId() || + deviceProfileId_ != serviceProfile.GetDeviceProfileId()); if (isNotEqual) { return true; } else { @@ -137,6 +162,8 @@ std::string ServiceProfile::dump() const cJSON_AddStringToObject(json, SERVICE_TYPE.c_str(), serviceType_.c_str()); cJSON_AddBoolToObject(json, IS_MULTI_USER.c_str(), isMultiUser_); cJSON_AddNumberToObject(json, USER_ID.c_str(), userId_); + cJSON_AddNumberToObject(json, ID.c_str(), id_); + cJSON_AddNumberToObject(json, DEVICE_PROFILE_ID.c_str(), deviceProfileId_); char* jsonChars = cJSON_PrintUnformatted(json); if (jsonChars == NULL) { cJSON_Delete(json); diff --git a/common/src/interfaces/service_profile_filter_opotions.cpp b/common/src/interfaces/service_profile_filter_opotions.cpp new file mode 100644 index 00000000..b52621ef --- /dev/null +++ b/common/src/interfaces/service_profile_filter_opotions.cpp @@ -0,0 +1,177 @@ +/* + * 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 "service_profile_filter_opotions.h" + +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "ipc_utils.h" +#include "macro_utils.h" +#include "profile_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "ServiceProfileFilterOptions"; +} +ServiceProfileFilterOptions::ServiceProfileFilterOptions(int32_t userId, std::string& accountId, + const std::vector& deviceIds,const std::vector& wiseDeviceIds, + const std::vector& serviceIds,const std::vector& serviceProfileIds, + const std::vector& deviceProfileIds) + : userId_(userId), accountId_(accountId), deviceIds_(deviceIds), wiseDeviceIds_(wiseDeviceIds), + serviceIds_(serviceIds), serviceProfileIds_(serviceProfileIds), deviceProfileIds_(deviceProfileIds) +{ +} + +ServiceProfileFilterOptions::ServiceProfileFilterOptions() +{ +} + +ServiceProfileFilterOptions::~ServiceProfileFilterOptions() +{ +} + +int32_t ServiceProfileFilterOptions::GetUserId() const +{ + return userId_; +} + +void ServiceProfileFilterOptions::SetUserId(int32_t userId) +{ + userId_ = userId; +} + +const std::string& ServiceProfileFilterOptions::GetAccountId() const +{ + return accountId_; +} + +void ServiceProfileFilterOptions::SetAccountId(const std::string& accountId) +{ + accountId_ = accountId; +} + +const std::vector& ServiceProfileFilterOptions::GetDeviceIds() const +{ + return deviceIds_; +} + +void ServiceProfileFilterOptions::SetDeviceIds(const std::vector& deviceIds) +{ + deviceIds_ = deviceIds; +} + +const std::vector& ServiceProfileFilterOptions::GetWiseDeviceIds() const +{ + return wiseDeviceIds_; +} + +void ServiceProfileFilterOptions::SetWiseDeviceIds(const std::vector& wiseDeviceIds) +{ + wiseDeviceIds_ = wiseDeviceIds; +} + +const std::vector& ServiceProfileFilterOptions::GetServiceIds() const +{ + return serviceIds_; +} + +void ServiceProfileFilterOptions::SetServiceIds(const std::vector& serviceIds) +{ + serviceIds_ = serviceIds; +} + +const std::vector& ServiceProfileFilterOptions::GetServiceProfileIds() const +{ + return serviceProfileIds_; +} + +void ServiceProfileFilterOptions::SetServiceProfileIds(const std::vector& serviceProfileIds) +{ + serviceProfileIds_ = serviceProfileIds; +} + +const std::vector& ServiceProfileFilterOptions::GetDeviceProfileIds() const +{ + return deviceProfileIds_; +} + +void ServiceProfileFilterOptions::SetDeviceProfileIds(const std::vector& deviceProfileIds) +{ + deviceProfileIds_ = deviceProfileIds; +} + +bool ServiceProfileFilterOptions::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER_RET(parcel, Int32, userId_, false); + WRITE_HELPER_RET(parcel, String, accountId_, false); + if (!IpcUtils::Marshalling(parcel, deviceIds_)) { + HILOGE("dp write parcel fail"); + return false; + } + if (!IpcUtils::Marshalling(parcel, wiseDeviceIds_)) { + HILOGE("dp write parcel fail"); + return false; + } + if (!IpcUtils::Marshalling(parcel, serviceIds_)) { + HILOGE("dp write parcel fail"); + return false; + } + if (!IpcUtils::Marshalling(parcel, serviceProfileIds_)) { + HILOGE("dp write parcel fail"); + return false; + } + return true; +} + +bool ServiceProfileFilterOptions::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, Int32, userId_, false); + READ_HELPER_RET(parcel, String, accountId_, false); + if (!IpcUtils::UnMarshalling(parcel, deviceIds_)) { + HILOGE("dp read parcel fail"); + return false; + } + if (!IpcUtils::UnMarshalling(parcel, wiseDeviceIds_)) { + HILOGE("dp read parcel fail"); + return false; + } + if (!IpcUtils::UnMarshalling(parcel, serviceIds_)) { + HILOGE("dp read parcel fail"); + return false; + } + if (!IpcUtils::UnMarshalling(parcel, serviceProfileIds_)) { + HILOGE("dp read parcel fail"); + return false; + } + return true; +} + +bool ServiceProfileFilterOptions::IsEmpty() const +{ + return deviceIds_.empty() && wiseDeviceIds_.empty() && serviceIds_.empty() && userId_ == DEFAULT_USER_ID && + serviceProfileIds_.empty() && accountId_.empty() && deviceProfileIds_.empty(); +} + +bool ServiceProfileFilterOptions::operator==(const ServiceProfileFilterOptions& rhs) const +{ + return userId_ == rhs.userId_ && accountId_ == rhs.accountId_ && deviceIds_ == rhs.deviceIds_ && + wiseDeviceIds_ == rhs.wiseDeviceIds_ && serviceIds_ == rhs.serviceIds_ && + serviceProfileIds_ == rhs.serviceProfileIds_ && deviceProfileIds_ == rhs.deviceProfileIds_; +} + + +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 0a79847c..9e0ae45d 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -88,6 +88,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/kvadapter/kv_adapter.cpp", "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", + "src/profiledao/service_profile_dao.cpp", "src/publishcommonevent/dp_account_common_event.cpp", "src/staticcapabilitycollector/static_capability_collector.cpp", "src/staticcapabilityloader/static_capability_loader.cpp", diff --git a/services/core/include/profiledao/service_profile_dao.h b/services/core/include/profiledao/service_profile_dao.h new file mode 100644 index 00000000..693f04a7 --- /dev/null +++ b/services/core/include/profiledao/service_profile_dao.h @@ -0,0 +1,47 @@ +/* + * 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_SERVICE_PROFILE_DAO_H +#define OHOS_DP_SERVICE_PROFILE_DAO_H + +#include "service_profile.h" +#include "service_profile_filter_opotions.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ServiceProfileDao { +public: + ServiceProfileDao() = default; + ~ServiceProfileDao() = default; + int32_t AddServiceProfile(ServiceProfile& serviceProfile); + int32_t DeleteServiceProfile(ServiceProfile& serviceProfile); + int32_t UpdateServiceProfile(ServiceProfile& serviceProfile); + int32_t GetServiceProfiles(ServiceProfileFilterOptions& filterOptions, + std::vector& serviceProfiles); +private: + int32_t ConvertToServiceProfile(std::shared_ptr resultSet, ServiceProfile& serviceProfile); + int32_t CreateQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, std::string& sql, + std::vector& condition); + void CreateBaseQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, std::string& whereSql, + std::vector& condition); + void CreateComplexQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, std::string& whereSql, + std::vector& condition); + void ServiceProfileToEntries(const ServiceProfile& serviceProfile, ValuesBucket& values); +}; + + +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif //OHOS_DP_SERVICE_PROFILE_DAO_H diff --git a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h index 9fe77633..f3d55722 100644 --- a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h +++ b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h @@ -44,6 +44,7 @@ public: int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); int32_t SubscribeDeviceProfile(std::map subscribeInfos); int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); + int32_t NotifyServiceProfileAdd(const ServiceProfile& serviceProfile); private: int32_t NotifyDeviceProfileAdd(const std::string& dbKey, const std::string& dbValue); diff --git a/services/core/src/profiledao/service_profile_dao.cpp b/services/core/src/profiledao/service_profile_dao.cpp new file mode 100644 index 00000000..6e2c972b --- /dev/null +++ b/services/core/src/profiledao/service_profile_dao.cpp @@ -0,0 +1,257 @@ +/* + * 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 +#include "content_sensor_manager_utils.h" +#include "device_profile_manager.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "service_profile_dao.h" +#include "subscribe_profile_manager.h" +#include "profile_cache.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { +const std::string TAG = "DistributedDeviceProfile"; +} + +int32_t ServiceProfileDao::AddServiceProfile(ServiceProfile& serviceProfile) +{ + if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || + serviceProfile.GetServiceType().empty() || serviceProfile.GetDeviceProfileId() == DEFAULT_DEVICE_PROFILE_ID) { + HILOGE("serviceProfile params is invalid!"); + return DP_INVALID_PARAMS; + } + ValuesBucket values; + ServiceProfileToEntries(serviceProfile, values); + int64_t rowId = ROWID_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, SERVICE_PROFILE_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("service_profile insert failed"); + return DP_RDB_PUT_SERVICE_PROFILE_FAIL; + } + //todo: set id + SubscribeProfileManager::GetInstance().NotifyServiceProfileAdd(serviceProfile); + std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); + if (localUdid == serviceProfile.GetDeviceId() ) { + DeviceProfileManager::GetInstance().PutServiceProfile(serviceProfile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::DeleteServiceProfile(ServiceProfile& serviceProfile) +{ + if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || + serviceProfile.GetServiceType().empty() || serviceProfile.GetDeviceProfileId() == DEFAULT_DEVICE_PROFILE_ID) { + HILOGE("serviceProfile params is invalid!"); + return DP_INVALID_PARAMS; + } + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, SERVICE_PROFILE_TABLE, ID_EQUAL_CONDITION, + std::vector{ ValueObject(serviceProfile.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete service_profile data failed"); + return DP_RDB_DELETE_SERVICE_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyDeviceProfileDelete(deviceProfile); + std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); + if (localUdid == serviceProfile.GetDeviceId() ) { + DeviceProfileManager::GetInstance().DeleteServiceProfile(serviceProfile.); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::UpdateServiceProfile(ServiceProfile& serviceProfile) +{ + if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || + serviceProfile.GetServiceType().empty() || serviceProfile.GetDeviceProfileId() == DEFAULT_DEVICE_PROFILE_ID) { + HILOGE("serviceProfile params is invalid!"); + return DP_INVALID_PARAMS; + } + ValuesBucket values; + ServiceProfileToEntries(serviceProfile, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update( + changeRowCnt, SERVICE_PROFILE_TABLE, values, ID_EQUAL_CONDITION, + std::vector{ ValueObject(serviceProfile.GetId()) }); + if (ret != DP_SUCCESS) {fc + HILOGE("Update service_profile table failed"); + return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL; + } + SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(oldProfile, newProfile); + std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); + if (localUdid == serviceProfile.GetDeviceId() ) { + DeviceProfileManager::GetInstance().PutServiceProfile(serviceProfile); + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfile::ServiceProfileDao::GetServiceProfiles(ServiceProfileFilterOptions& filterOptions, + std::vector& serviceProfiles) +{ + if (filterOptions.IsEmpty()) { + HILOGE("filterOptions is empty!"); + return DP_INVALID_PARAMS; + } + std::vector condition; + std::string sql = ""; + if (CreateQuerySqlAndCondition(filterOptions, sql, condition) != DP_SUCCESS) { + HILOGE("CreateQuerySqlAndCondition failed!"); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + ServiceProfile serviceProfile; + ConvertToServiceProfile(resultSet, serviceProfile); + serviceProfiles.emplace_back(serviceProfile); + } + resultSet->Close(); + if (serviceProfiles.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::ConvertToServiceProfile(std::shared_ptr resultSet, + ServiceProfile& serviceProfile) +{ + 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; + } + serviceProfile.SetId(rowEntity.Get(ID)); + serviceProfile.SetDeviceProfileId(rowEntity.Get(SERVICE_PROFILE_DEVICE_PROFILE_ID)); + serviceProfile.SetDeviceId(rowEntity.Get(DEVICE_ID)); + serviceProfile.SetServiceName(rowEntity.Get(SERVICE_ID)); + serviceProfile.SetServiceType(rowEntity.Get(SERVICE_TYPE)); + serviceProfile.SetUserId(rowEntity.Get(RDB_USER_ID)); + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::CreateQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, std::string& sql, + std::vector& condition) +{ + std::string whereSql = ""; + CreateComplexQuerySqlAndCondition(filterOptions, whereSql, condition); + CreateBaseQuerySqlAndCondition(filterOptions, whereSql, condition); + if (whereSql.empty()) { + HILOGE("whereSql is empty!"); + return DP_INVALID_PARAMS; + } + whereSql.erase(whereSql.end() - 3, whereSql.end()); + sql = SELECT_SERVICE_PROGILES + whereSql; + return DP_SUCCESS; +} + +void ServiceProfileDao::CreateComplexQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, + std::string& whereSql, std::vector& condition) +{ + // whereSql append service_profile.id + if (!filterOptions.GetServiceProfileIds().empty()) { + whereSql += "service_profile.id IN ("; + for (auto& id : filterOptions.GetServiceProfileIds()) { + whereSql += "?,"; + condition.emplace_back(ValueObject(id)); + } + whereSql.erase(whereSql.end() - 1, whereSql.end()); + whereSql += ") AND"; + } + // whereSql append service_profile.serviceId + if (!filterOptions.GetServiceIds().empty()) { + whereSql += "service_profile.serviceId IN ("; + for (auto& serviceId : filterOptions.GetServiceIds()) { + whereSql += "?,"; + condition.emplace_back(ValueObject(serviceId)); + } + whereSql.erase(whereSql.end() - 1, whereSql.end()); + whereSql += ") AND"; + } + // whereSql append service_profile.deviceProfileId + if (!filterOptions.GetDeviceProfileIds().empty()) { + whereSql += "service_profile.deviceProfileId IN ("; + for (auto& deviceProfileId : filterOptions.GetDeviceProfileIds()) { + whereSql += "?,"; + condition.emplace_back(ValueObject(deviceProfileId)); + } + whereSql.erase(whereSql.end() - 1, whereSql.end()); + whereSql += ") AND"; + } + // whereSql append device_profile.deviceId + if (!filterOptions.GetDeviceIds().empty()) { + whereSql += "device_profile.udid IN ("; + for (auto& deviceId : filterOptions.GetDeviceIds()) { + whereSql += "?,"; + condition.emplace_back(ValueObject(deviceId)); + } + whereSql.erase(whereSql.end() - 1, whereSql.end()); + whereSql += ") AND"; + } +} + +void ServiceProfileDao::CreateBaseQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, + std::string& whereSql, std::vector& condition) +{ + // whereSql append device_profile.userId + if (filterOptions.GetUserId() != DEFAULT_USER_ID) { + whereSql += "device_profile.userId=? AND"; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + } + // whereSql append device_profile.accountId + if (!filterOptions.GetAccountId().empty()) { + whereSql += "device_profile.accountId=? AND"; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + } + // whereSql append device_profile.wiseDeviceId + if (!filterOptions.GetWiseDeviceIds().empty()) { + whereSql += "device_profile.wiseDeviceId IN ("; + for (auto& wiseDeviceId : filterOptions.GetWiseDeviceIds()) { + whereSql += "?,"; + condition.emplace_back(ValueObject(wiseDeviceId)); + } + whereSql.erase(whereSql.end() - 1, whereSql.end()); + whereSql += ") AND"; + } +} + +void ServiceProfileDao::ServiceProfileToEntries(const ServiceProfile& serviceProfile, ValuesBucket& values) +{ + values.PutInt(SERVICE_PROFILE_DEVICE_PROFILE_ID, serviceProfile.GetDeviceProfileId()); + values.PutString(SERVICE_PROFILE_SERVICE_ID, serviceProfile.GetServiceName()); + values.PutString(SERVICE_PROFILE_SERVICE_TYPE, serviceProfile.GetServiceType()); +} +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp index 06cebceb..f83564f2 100644 --- a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp +++ b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp @@ -540,5 +540,25 @@ std::string SubscribeProfileManager::DBKeyToSubcribeKey(const std::string& dbkey } return subscribeKey; } + +int32_t SubscribeProfileManager::NotifyServiceProfileAdd(const ServiceProfile& serviceProfile) +{ + auto subscriberInfos = GetSubscribeInfos(SERVICE_PROFILE_TABLE); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", serviceProfile.dump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_ADD) != 0) { + listenerProxy->OnDeviceProfileAdd(serviceProfile); + } + } + return DP_SUCCESS; +} } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 960e8a57ce1cacb36e0ee0a0acf9ce0e4b1ef4e6 Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 11:34:07 +0800 Subject: [PATCH 06/55] =?UTF-8?q?=E6=96=B0=E5=A2=9EProductInfo=E3=80=81Dev?= =?UTF-8?q?iceIconInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/include/interfaces/device_icon_info.h | 63 ++++++++ common/include/interfaces/product_info.h | 59 +++++++ .../include/interfaces/trusted_device_info.h | 12 +- common/src/interfaces/device_icon_info.cpp | 149 ++++++++++++++++++ common/src/interfaces/product_info.cpp | 137 ++++++++++++++++ common/src/interfaces/trusted_device_info.cpp | 6 +- 6 files changed, 417 insertions(+), 9 deletions(-) create mode 100644 common/include/interfaces/device_icon_info.h create mode 100644 common/include/interfaces/product_info.h create mode 100644 common/src/interfaces/device_icon_info.cpp create mode 100644 common/src/interfaces/product_info.cpp diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h new file mode 100644 index 00000000..4d0a98d6 --- /dev/null +++ b/common/include/interfaces/device_icon_info.h @@ -0,0 +1,63 @@ +/* + * 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_DEVICE_ICON_INFO_H +#define OHOS_DP_DEVICE_ICON_INFO_H + +#include +#include +#include +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceIconInfo : public DpParcel { +public: + DeviceIconInfo() + : id_(0), + productId_(""), + subProductId_(nullptr), + imageType(""), + specName(""), + icon_(nullptr), + {} + ~DeviceIconInfo() = default; + int32_t GetId() const; + void SetId(const int32_t id); + std::string GetProuctId() const; + void SetProuctId(const std::string& productId); + std::string GetSubProuctId() const; + void SetSubProuctId(const std::string& subProductId); + std::string GetImageType() const; + void SetImageType(const std::string& imageType); + std::string GetSpecName() const; + void SetSpecName(const std::string& specName); + std::vector GetIcon() const; + void SetIcon(const std::vector& icon); + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + bool operator!=(const DeviceIconInfo& other) const; + +private: + int32_t id_; + std::string productId_; + std::string subProductId_; + std::string imageType_; + std::string specName_; + std::vector icon_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif //OHOS_DP_DEVICE_ICON_INFO_H diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h new file mode 100644 index 00000000..c1eb94fe --- /dev/null +++ b/common/include/interfaces/product_info.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_PRODUCT_INFO_H +#define OHOS_DP_PRODUCT_INFO_H + +#include +#include +#include +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ProductInfo : public DpParcel { +public: + ProductInfo() : productId_(""), + model_(nullptr), + productName_(""), + productShortName_(""), + imageVersion_(nullptr), + {} + ~ProductInfo() = default; + + std::string GetProuctId() const; + void SetProuctId(const std::string& productId); + std::string GetModel() const; + void SetModelGetModel(const std::string& model); + std::string GetProductName() const; + void SetProductName(const std::string& productName); + std::string GetProductShortName() const; + void SetProductShortName(const std::string& productShortName); + int32_t GetImageVersion() const; + void SetImageVersion(const int32_t specName); + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + bool operator!=(const DeviceIconInfo& other) const; + +private: + std::string productId_; + std::string model_; + std::string productName_; + std::string productShortName_; + int32_t imageVersion_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif //OHOS_DP_PRODUCT_INFO_H diff --git a/common/include/interfaces/trusted_device_info.h b/common/include/interfaces/trusted_device_info.h index 136689d4..4a339d4b 100644 --- a/common/include/interfaces/trusted_device_info.h +++ b/common/include/interfaces/trusted_device_info.h @@ -32,13 +32,13 @@ public: std::string GetNetworkId() const; void SetNetworkId(const std::string& networkId); int32_t GetAuthForm() const; - void SetAuthForm(int32_t authForm); + void SetAuthForm(const int32_t authForm); uint16_t GetDeviceTypeId() const; - void SetDeviceTypeId(uint16_t deviceTypeId); + void SetDeviceTypeId(const uint16_t deviceTypeId); std::string GetOsVersion() const; void SetOsVersion(const std::string& osVersion); int32_t GetOsType() const; - void SetOsType(int32_t osType); + void SetOsType(const int32_t osType); std::string GetUdid() const; void SetUdid(const std::string& udid); std::string GetUuid() const; @@ -52,10 +52,10 @@ public: private: std::string networkId_; - std::int32_t authForm_; - std::uint16_t deviceTypeId_; + int32_t authForm_; + uint16_t deviceTypeId_; std::string osVersion_; - std::int32_t osType_; + int32_t osType_; std::string udid_; std::string uuid_; }; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp new file mode 100644 index 00000000..09204b9f --- /dev/null +++ b/common/src/interfaces/device_icon_info.cpp @@ -0,0 +1,149 @@ +/* + * 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 "device_icon_info.h" +#include "cJSON.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DeviceIconInfo"; + const std::string PRODUCT_INFO_ID = "id"; + const std::string PRODUCT_ID = "productId"; + const std::string SUB_PRODUCT_ID = "subProductId"; + const std::string IMAGE_TYPE = "imageType"; + const std::string SPEC_NAME = "specName"; +} + +int32_t DeviceIconInfo::GetId() const +{ + return id_; +} + +void DeviceIconInfo::SetId(const int32_t id) +{ + this->id_ = id; +} + +std::string DeviceIconInfo::GetProuctId() const +{ + return productId_; +} + +void DeviceIconInfo::SetProuctId(const std::string& productId) +{ + this->productId_ = productId; +} + +std::string DeviceIconInfo::GetSubProuctId() const +{ + return subProductId_; +} + +void DeviceIconInfo::SetSubProuctId(const std::string& subProductId) +{ + this->subProductId_ = subProductId; +} + +std::string DeviceIconInfo::GetImageType() const +{ + return imageType_; +} + +void DeviceIconInfo::SetImageType(const std::string& imageType) +{ + this->imageType_ = imageType; +} + +std::string DeviceIconInfo::GetSpecName() const +{ + return specName_; +} + +void DeviceIconInfo::SetSpecName(const std::string& specName) +{ + this->specName_ = specName; +} + +std::vector DeviceIconInfo::GetIcon() const +{ + return icon_; +} + +void DeviceIconInfo::SetIcon(const std::vector& icon) +{ + this->icon_ = icon; +} + + +bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER_RET(parcel, Int32, id_, false); + WRITE_HELPER_RET(parcel, String, productId_, false); + WRITE_HELPER_RET(parcel, String, subProductId_, false); + WRITE_HELPER_RET(parcel, String, imageType_, false); + WRITE_HELPER_RET(parcel, String, specName_, false); + WRITE_HELPER_RET(parcel, Int64, icon_, false); + return true; +} + +bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, Int32, id_, false); + READ_HELPER_RET(parcel, String, productId_, false); + READ_HELPER_RET(parcel, String, subProductId_, false); + READ_HELPER_RET(parcel, String, imageType_, false); + READ_HELPER_RET(parcel, String, specName_, false); + READ_HELPER_RET(parcel, Int64, icon_, false); + return true; +} + +bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const +{ + bool isNotEqual = (id_ != other.GetId() || productId_ != other.GetProuctId() + || subProductId_ != other.GetSubProuctId() || imageType_ != other.GetImageType() || + specName_ != other.GetSpecName() || icon_ != other.GetIcon()); + if (isNotEqual) { + return true; + } else { + return false; + } +} + +std::string DeviceProfile::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + return EMPTY_STRING; + } + cJSON_AddNumberToObject(json, PRODUCT_INFO_ID.c_str(), id_); + cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + 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 diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp new file mode 100644 index 00000000..82eaf476 --- /dev/null +++ b/common/src/interfaces/product_info.cpp @@ -0,0 +1,137 @@ +/* + * 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 "product_info.h" +#include "cJSON.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "ProductInfo"; + const std::string MODEL = "model"; + const std::string PRODUCT_ID = "productId"; + const std::string PRODUCT_NAME = "productName"; + const std::string PRODUCT_SHORT_NAME = "productShortName"; + const std::string IMAGE_VERSION = "imageVersion"; +} + +std::string DeviceIconInfo::GetProuctId() const +{ + return productId_; +} + +void DeviceIconInfo::SetProuctId(const std::string& productId) +{ + this->productId_ = productId; +} + +std::string DeviceIconInfo::GetModel() const +{ + return model_; +} + +void DeviceIconInfo::SetModel(const std::string& model) +{ + this->model_ = model; +} + +std::string DeviceIconInfo::GetProductName() const +{ + return productName_; +} + +void DeviceIconInfo::SetProductName(const std::string& productName) +{ + this->productName_ = productName; +} + +std::string DeviceIconInfo::GetProductShortName() const +{ + return productShortName_; +} + +void DeviceIconInfo::SetProductShortName(const std::string& productShortName) +{ + this->productShortName_ = productShortName; +} + +int32_t DeviceIconInfo::GetImageVersion() const +{ + return imageVersion_; +} + +void DeviceIconInfo::SetImageVersion(const int32_t imageVersion) +{ + this->ImageVersion_ = imageVersion; +} + + +bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const +{ + WRITE_HELPER_RET(parcel, String, productId_, false); + WRITE_HELPER_RET(parcel, String, model_, false); + WRITE_HELPER_RET(parcel, String, productName_, false); + WRITE_HELPER_RET(parcel, String, productShortName_, false); + WRITE_HELPER_RET(parcel, Int32, imageVersion_, false); + return true; +} + +bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) +{ + READ_HELPER_RET(parcel, String, productId_, false); + READ_HELPER_RET(parcel, String, model_, false); + READ_HELPER_RET(parcel, String, productName_, false); + READ_HELPER_RET(parcel, String, productShortName_, false); + READ_HELPER_RET(parcel, Int32, imageVersion_, false); + return true; +} + +bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const +{ + bool isNotEqual = (productId_ != other.GetProuctId() || model_ != other.GetModel() || + productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() || + imageVersion_ != other.GetImageVersion()); + if (isNotEqual) { + return true; + } else { + return false; + } +} + +std::string DeviceProfile::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); + cJSON_AddStringToObject(json, MODEL.c_str(), model_); + cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_); + cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_); + cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); + 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 diff --git a/common/src/interfaces/trusted_device_info.cpp b/common/src/interfaces/trusted_device_info.cpp index cfb9d6b5..4791f29e 100644 --- a/common/src/interfaces/trusted_device_info.cpp +++ b/common/src/interfaces/trusted_device_info.cpp @@ -45,7 +45,7 @@ int32_t TrustedDeviceInfo::GetAuthForm() const return authForm_; } -void TrustedDeviceInfo::SetAuthForm(int32_t authForm) +void TrustedDeviceInfo::SetAuthForm(const int32_t authForm) { authForm_ = authForm; } @@ -55,7 +55,7 @@ uint16_t TrustedDeviceInfo::GetDeviceTypeId() const return deviceTypeId_; } -void TrustedDeviceInfo::SetDeviceTypeId(uint16_t deviceTypeId) +void TrustedDeviceInfo::SetDeviceTypeId(const uint16_t deviceTypeId) { deviceTypeId_ = deviceTypeId; } @@ -75,7 +75,7 @@ int32_t TrustedDeviceInfo::GetOsType() const return osType_; } -void TrustedDeviceInfo::SetOsType(int32_t osType) +void TrustedDeviceInfo::SetOsType(const int32_t osType) { osType_ = osType; } -- Gitee From 6033339a8f3385c0eae6ac16b0708c56adf564dc Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 17:16:08 +0800 Subject: [PATCH 07/55] =?UTF-8?q?=E6=96=B0=E5=A2=9EProductInfoDao=E3=80=81?= =?UTF-8?q?DeviceIconInfoDao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- .../distributed_device_profile_constants.h | 10 + .../distributed_device_profile_errors.h | 7 + common/include/interfaces/device_icon_info.h | 6 +- .../device_icon_info_filter_options.h | 53 ++++ common/include/interfaces/product_info.h | 6 +- .../distributed_device_profile_constants.cpp | 13 +- common/src/interfaces/device_icon_info.cpp | 16 +- .../device_icon_info_filter_options.cpp | 132 ++++++++++ common/src/interfaces/product_info.cpp | 35 ++- common/src/utils/ipc_utils.cpp | 14 + .../profiledatamanager/device_icon_info_dao.h | 56 ++++ .../profiledatamanager/product_info_dao.h | 55 ++++ .../core/src/common/dp_services_constants.cpp | 18 ++ .../device_icon_info_dao.cpp | 240 ++++++++++++++++++ .../profiledatamanager/product_info_dao.cpp | 212 ++++++++++++++++ 15 files changed, 837 insertions(+), 36 deletions(-) create mode 100644 common/include/interfaces/device_icon_info_filter_options.h create mode 100644 common/src/interfaces/device_icon_info_filter_options.cpp create mode 100644 services/core/include/profiledatamanager/device_icon_info_dao.h create mode 100644 services/core/include/profiledatamanager/product_info_dao.h create mode 100644 services/core/src/profiledatamanager/device_icon_info_dao.cpp create mode 100644 services/core/src/profiledatamanager/product_info_dao.cpp diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index f67e082d..549e99fa 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -63,12 +63,22 @@ extern const std::string MODIFY_TIME; extern const std::string SHARE_TIME; extern const std::string PRODUCTOR_INFO_VERSION; extern const std::string DEVICE_PROFILE_TABLE; +extern const std::string DEVICE_ICON_INFO_TABLE; +extern const std::string PRODUCT_INFO_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; +/* ProductInfo Attribute */ +extern const std::string PRODUCT_NAME; +extern const std::string PRODUCT_SHORT_NAME; +extern const std::string IMAGE_VERSION; +/* DeviceIconInfo Attribute */ +extern const std::string IMAGE_TYPE; +extern const std::string SPEC_NAME; +extern const std::string ICON; /* TrustDeviceProfile Attribute */ extern const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE; extern const std::string DEVICE_ID_TYPE; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 86fa4d08..8aad772f 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -160,6 +160,13 @@ 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_DEVICE_ICON_INFO_FAIL = 98566284; +constexpr int32_t DP_DEL_DEVICE_ICON_INFO_FAIL = 98566286; +constexpr int32_t DP_UPDATE_DEVICE_ICON_INFO_FAIL = 98566287; +constexpr int32_t DP_PUT_PRODUCT_INFO_FAIL = 98566288; +constexpr int32_t DP_DEL_PRODUCT_INFO_FAIL = 98566280; +constexpr int32_t DP_UPDATE_PRODUCT_INFO_FAIL = 98566290; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h index 4d0a98d6..c75d93dc 100644 --- a/common/include/interfaces/device_icon_info.h +++ b/common/include/interfaces/device_icon_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_DEVICE_ICON_INFO_H #define OHOS_DP_DEVICE_ICON_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -28,10 +28,10 @@ public: DeviceIconInfo() : id_(0), productId_(""), - subProductId_(nullptr), + subProductId_(""), imageType(""), specName(""), - icon_(nullptr), + icon_(""), {} ~DeviceIconInfo() = default; int32_t GetId() const; diff --git a/common/include/interfaces/device_icon_info_filter_options.h b/common/include/interfaces/device_icon_info_filter_options.h new file mode 100644 index 00000000..fc4f5e40 --- /dev/null +++ b/common/include/interfaces/device_icon_info_filter_options.h @@ -0,0 +1,53 @@ + /* + * 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_DEVICE_ICON_INFO_FILTER_OPTIONS_H +#define OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H + +#include +#include + +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceIconInfoFilterOptions : public DpParcel { +public: + DeviceIconInfoFilterOptions() :subProductId_(""), imageType_(""),specName_("") + {} + ~DeviceIconInfoFilterOptions() = default; + + std::vector GetProductIds() const; + void SetProductIds(const std::vector& productIds); + std::string GetSubProductId() const; + void SetSubProductId(const std::string& subProductId); + std::string GetImageType() const; + void SetImageType(const std::string& imageType); + std::string GetSpecName() const; + void SetSpecName(const std::string& specName); + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + std::vector productIds_; + std::string subProductId_; + std::string imageType_; + std::string specName_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h index c1eb94fe..121fe31e 100644 --- a/common/include/interfaces/product_info.h +++ b/common/include/interfaces/product_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_PRODUCT_INFO_H #define OHOS_DP_PRODUCT_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -26,10 +26,10 @@ namespace DistributedDeviceProfile { class ProductInfo : public DpParcel { public: ProductInfo() : productId_(""), - model_(nullptr), + model_(""), productName_(""), productShortName_(""), - imageVersion_(nullptr), + imageVersion_(""), {} ~ProductInfo() = default; diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index ae381843..5c1406ff 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -26,7 +26,7 @@ const std::string DEVICE_TYPE_ID = "deviceTypeId"; const std::string DEVICE_TYPE_NAME = "deviceTypeName"; const std::string DEVICE_NAME = "deviceIdName"; const std::string MANUFACTURE_NAME = "manufactureName"; -const std::string DEVICE_MODEL = "deviceModel"; +const std::string DEVICE_MODEL = "model"; const std::string STORAGE_CAPACITY = "storageCapacity"; const std::string OS_SYS_CAPACITY = "osSysCapacity"; const std::string OS_API_LEVEL = "osApiLevel"; @@ -53,12 +53,23 @@ const std::string MODIFY_TIME = "modifyTime"; const std::string SHARE_TIME = "shareTime"; const std::string PRODUCTOR_INFO_VERSION = "productorInfoVersion"; const std::string DEVICE_PROFILE_TABLE = "device_profile"; +const std::string DEVICE_ICON_INFO_TABLE = "device_icon_info"; +const std::string PRODUCT_INFO_TABLE = "product_info"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; +const std::string SERVICE_NAME = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; +/* ProductInfo Attribute */ +const std::string PRODUCT_NAME = "productName"; +const std::string PRODUCT_SHORT_NAME = "productShortName"; +const std::string IMAGE_VERSION = "imageVersion"; +/* DeviceIconInfo Attribute */ +const std::string IMAGE_TYPE = "imageType"; +const std::string SPEC_NAME = "specName"; +const std::string ICON = "icon"; /* TrustDeviceProfile Attribute */ const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE = "trust_device_profile"; const std::string DEVICE_ID_TYPE = "deviceIdType"; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 09204b9f..2857a7c7 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -14,18 +14,15 @@ */ #include "device_icon_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "DeviceIconInfo"; - const std::string PRODUCT_INFO_ID = "id"; - const std::string PRODUCT_ID = "productId"; - const std::string SUB_PRODUCT_ID = "subProductId"; - const std::string IMAGE_TYPE = "imageType"; - const std::string SPEC_NAME = "specName"; } int32_t DeviceIconInfo::GetId() const @@ -88,7 +85,6 @@ void DeviceIconInfo::SetIcon(const std::vector& icon) this->icon_ = icon; } - bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, Int32, id_, false); @@ -96,7 +92,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); - WRITE_HELPER_RET(parcel, Int64, icon_, false); + WRITE_HELPER_RET(parcel, UInt8Vector, icon_); return true; } @@ -107,7 +103,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); - READ_HELPER_RET(parcel, Int64, icon_, false); + READ_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -123,13 +119,13 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } - cJSON_AddNumberToObject(json, PRODUCT_INFO_ID.c_str(), id_); + cJSON_AddNumberToObject(json, ID.c_str(), id_); cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); diff --git a/common/src/interfaces/device_icon_info_filter_options.cpp b/common/src/interfaces/device_icon_info_filter_options.cpp new file mode 100644 index 00000000..a6cf7233 --- /dev/null +++ b/common/src/interfaces/device_icon_info_filter_options.cpp @@ -0,0 +1,132 @@ +/* + * 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 "device_icon_info_filter_options.h" + +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_log.h" +#include "ipc_utils.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DeviceIconInfoFilterOptions"; +} + +std::vector DistributedDeviceProfile::GetProductIds() const +{ + return productIds_; +} + +void DistributedDeviceProfile::SetProductIds(const std::vector& productIds) +{ + specName_ = specName; +} + +std::string DistributedDeviceProfile::GetSubProductId() const +{ + return subProductId_; +} + +void DistributedDeviceProfile::SetSubProductId(const std::string& subProductId) +{ + subProductId_ = subProductId; +} + +std::string DistributedDeviceProfile::GetImageType() const +{ + return imageType_; +} + +void DistributedDeviceProfile::SetImageType(const std::string& imageType) +{ + imageType_ = imageType; +} + +std::string DistributedDeviceProfile::GetSpecName() const +{ + return specName_; +} + +void DistributedDeviceProfile::SetSpecName(const std::string& specName) +{ + specName_ = specName; +} + +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const +{ + IpcUtils::Marshalling(parcel, productIds_); + WRITE_HELPER_RET(parcel, String, subProductId_, false); + WRITE_HELPER_RET(parcel, String, imageType_, false); + WRITE_HELPER_RET(parcel, String, specName_, false); + return true; +} + +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) +{ + IpcUtils::UnMarshalling(parcel, productIds_); + READ_HELPER_RET(parcel, String, subProductId_, false); + READ_HELPER_RET(parcel, String, imageType_, false); + READ_HELPER_RET(parcel, String, specName_, false); + return true; +} + +std::string DistributedDeviceProfile::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + HILOGE("cJSON CreateObject failed!"); + return EMPTY_STRING; + } + cJSON* prodIdsJson = cJSON_CreateArray(); + if (prodIdsJson == NULL) { + HILOGE("cJSON CreateArray failed!"); + return EMPTY_STRING; + } + for (const auto& value : productIds_) { + cJSON* arrItem = cJSON_CreateString(value.c_str()); + if (arrItem == NULL) { + HILOGW("cJSON CreateString failed!"); + continue; + } + if (!cJSON_AddItemToArray(prodIdsJson, arrItem)) { + cJSON_Delete(arrItem); + HILOGW("Add item to array failed!"); + continue; + } + } + if (!cJSON_AddItemToObject(json, PRODUCT_IDS.c_str(), prodIdsJson)) { + cJSON_Delete(prodIdsJson); + HILOGE("Add json array to Object failed!"); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + 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 diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp index 82eaf476..04d8f1e3 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -14,18 +14,15 @@ */ #include "product_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "ProductInfo"; - const std::string MODEL = "model"; - const std::string PRODUCT_ID = "productId"; - const std::string PRODUCT_NAME = "productName"; - const std::string PRODUCT_SHORT_NAME = "productShortName"; - const std::string IMAGE_VERSION = "imageVersion"; } std::string DeviceIconInfo::GetProuctId() const @@ -33,53 +30,53 @@ std::string DeviceIconInfo::GetProuctId() const return productId_; } -void DeviceIconInfo::SetProuctId(const std::string& productId) +void DistributedDeviceProfile::SetProuctId(const std::string& productId) { this->productId_ = productId; } -std::string DeviceIconInfo::GetModel() const +std::string DistributedDeviceProfile::GetModel() const { return model_; } -void DeviceIconInfo::SetModel(const std::string& model) +void DistributedDeviceProfile::SetModel(const std::string& model) { this->model_ = model; } -std::string DeviceIconInfo::GetProductName() const +std::string DistributedDeviceProfile::GetProductName() const { return productName_; } -void DeviceIconInfo::SetProductName(const std::string& productName) +void DistributedDeviceProfile::SetProductName(const std::string& productName) { this->productName_ = productName; } -std::string DeviceIconInfo::GetProductShortName() const +std::string DistributedDeviceProfile::GetProductShortName() const { return productShortName_; } -void DeviceIconInfo::SetProductShortName(const std::string& productShortName) +void DistributedDeviceProfile::SetProductShortName(const std::string& productShortName) { this->productShortName_ = productShortName; } -int32_t DeviceIconInfo::GetImageVersion() const +int32_t DistributedDeviceProfile::GetImageVersion() const { return imageVersion_; } -void DeviceIconInfo::SetImageVersion(const int32_t imageVersion) +void DistributedDeviceProfile::SetImageVersion(const int32_t imageVersion) { this->ImageVersion_ = imageVersion; } -bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, productId_, false); WRITE_HELPER_RET(parcel, String, model_, false); @@ -89,7 +86,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const return true; } -bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, String, productId_, false); READ_HELPER_RET(parcel, String, model_, false); @@ -99,7 +96,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) return true; } -bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const +bool DistributedDeviceProfile::operator!=(const DistributedDeviceProfile& other) const { bool isNotEqual = (productId_ != other.GetProuctId() || model_ != other.GetModel() || productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() || @@ -111,14 +108,14 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); - cJSON_AddStringToObject(json, MODEL.c_str(), model_); + cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_); cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_); cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_); cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index b51fc826..8b937463 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -115,6 +115,20 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& pa return true; } +bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) +{ + if (params.empty() || params.size() > MAX_ID_SIZE) { + HILOGE("params size is invalid!size : %{public}zu", params.size()); + return false; + } + size_t size = params.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + for (const auto& item : params) { + WRITE_HELPER_RET(parcel, Uint8, item, false); + } + return true; +} + bool IpcUtils::Marshalling(MessageParcel& parcel, const std::map& params) { if (params.size() == 0 || params.size() > MAX_PARAM_SIZE) { diff --git a/services/core/include/profiledatamanager/device_icon_info_dao.h b/services/core/include/profiledatamanager/device_icon_info_dao.h new file mode 100644 index 00000000..97e4371c --- /dev/null +++ b/services/core/include/profiledatamanager/device_icon_info_dao.h @@ -0,0 +1,56 @@ +/* + * 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_DEVICE_ICON_INFO_DAO_H +#define OHOS_DP_DEVICE_ICON_INFO_DAO_H + +#include +#include +#include +#include + +#include "device_icon_info.h" +#include "device_icon_info_filter_options.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class DeviceIconInfoDao { + DECLARE_SINGLE_INSTANCE(DeviceIconInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutDeviceIconInfo(DeviceIconInfo& deviceIconInfo); + int32_t GetDeviceIconInfos(const std::vector& productIds, + std::vector& deviceIconInfos); + int32_t DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values); + int32_t ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_DAO_H \ No newline at end of file diff --git a/services/core/include/profiledatamanager/product_info_dao.h b/services/core/include/profiledatamanager/product_info_dao.h new file mode 100644 index 00000000..ceddac7b --- /dev/null +++ b/services/core/include/profiledatamanager/product_info_dao.h @@ -0,0 +1,55 @@ +/* + * 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_PRODUCT_INFO_DAO_H +#define OHOS_DP_PRODUCT_INFO_DAO_H + +#include +#include +#include +#include + +#include "product_info.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class ProductInfoDao { + DECLARE_SINGLE_INSTANCE(ProductInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutProductInfo(ProductInfo& productInfo); + int32_t GetProductInfos(const std::vector& productIds, + std::vector& productInfos); + int32_t DeleteProductInfo(const ProductInfo& productInfo); + int32_t UpdateProductInfo(const ProductInfo& productInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const ProductInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t ProductInfoToEntries(const ProductInfo& ProductInfo, ValuesBucket& values); + int32_t ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_PRODUCT_INFO_DAO_H \ No newline at end of file diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index dfa4bbee..4563d3a4 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -59,5 +59,23 @@ const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = deviceId,\ userId,\ accountId);"; +// DeviceIconInfoDao +const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_icon_info\ +(id INTEGER PRIMARY KEY AUTOINCREMENT,\ +productId TEXT,\ +subProductId TEXT,\ +imageType TEXT,\ +specName TEXT,\ +icon blob);"; +const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL = "CREATE UNIQUE INDEX if not exists \ +unique_device_icon_info ON device_icon_info (productId,subProductId,imageType,specName);"; +const std::string SELECT_DEVICE_ICON_INFO_TABLE = "SELECT * FROM device_profile WHERE "; +// ProductInfoDao +const std::string CREATE_PRODUCT_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS product_info \ +(productId TEXT PRIMARY KEY,\ +model TEXT,\ +productName TEXT,\ +productShortName TEXT,\ +imageVersion INTEGER);"; } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp new file mode 100644 index 00000000..a116af07 --- /dev/null +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -0,0 +1,240 @@ +/* + * 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 "device_icon_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(DeviceIconInfoDao); +namespace { + const std::string TAG = "DeviceIconInfoDao"; +} + +int32_t DeviceIconInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, DEVICE_ICON_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_PUT_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::GetDeviceIconInfos(const DeviceIconInfoOptions& filterOptions, + std::vector& deviceIconInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(filterOptions, sql, condition)) { + HILOGE("invalid params:%{public}s", filterOptions.dump().c_str()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + DeviceIconInfo deviceIconInfo; + ConvertToDeviceIconInfo(resultSet, deviceIconInfo); + deviceIconInfos.emplace_back(deviceIconInfo); + } + resultSet->Close(); + if (deviceIconInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, DEVICE_ICON_INFO_TABLE, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_DEL_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, DEVICE_ICON_INFO_TABLE, values, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_UPDATE_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateIndex() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s unique index create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + +bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition) +{ + sql = SELECT_DEVICE_ICON_INFO_TABLE; + bool flag = false; + if (!filterOptions.GetProductIds().empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : filterOptions.GetProductIds()) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (!filterOptions.GetSubProductId().empty()) { + sql += SUB_PRODUCT_ID + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + flag = true; + } + if (!filterOptions.GetImageType().empty()) { + sql += IMAGE_TYPE + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (!filterOptions.GetSpecName().empty()) { + sql += SPEC_NAME + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (flag) { + sql.erase(sql.end() - 4); + return flag; + } + return flag; +} + +int32_t DeviceIconInfoDao::DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, deviceIconInfo.GetProductId()); + values.PutString(SUB_PRODUCT_ID, deviceIconInfo.GetSubProductId()); + values.PutString(IMAGE_TYPE, deviceIconInfo.GetImageType()); + values.PutString(SPEC_NAME, deviceIconInfo.GetSpecName()); + values.PutBlob(ICON, deviceIconInfo.GetIcon()); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo) +{ + 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; + } + deviceIconInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + deviceIconInfo.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID)); + deviceIconInfo.SetImageType(rowEntity.Get(IMAGE_TYPE)); + deviceIconInfo.SetSpecName(rowEntity.Get(SPEC_NAME)); + // std::vector icon; + // rowEntity.Get(ICON).GetBlob(icon); + // deviceIconInfo.SetIcon(icon); + deviceIconInfo.SetIcon(rowEntity.Get(ICON)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp new file mode 100644 index 00000000..43955592 --- /dev/null +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -0,0 +1,212 @@ +/* + * 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 "product_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProductInfoDao); +namespace { + const std::string TAG = "ProductInfoDao"; +} + +int32_t ProductInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::PutProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, PRODUCT_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", PRODUCT_INFO_TABLE.c_str()); + return DP_PUT_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::GetProductInfos(const std::vector& productIds, + std::vector& productInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(productIds, sql, condition)) { + HILOGE("invalid params: productIds.size=%{public}zu", productIds.size()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + ProductInfo productInfo; + ConvertToProductInfo(resultSet, productInfo); + productInfos.emplace_back(productInfo); + } + resultSet->Close(); + if (productInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::DeleteProductInfo(const ProductInfo& productInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, PRODUCT_INFO_TABLE, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", PRODUCT_INFO_TABLE.c_str()); + return DP_DEL_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::UpdateProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, PRODUCT_INFO_TABLE, values, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", PRODUCT_INFO_TABLE.c_str()); + return DP_UPDATE_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_PRODUCT_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateIndex() +{ + return DP_SUCCESS; +} + +bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& productIds, + std::string& sql, std::vector& condition) +{ + sql = SELECT_PRODUCT_INFO_TABLE; + bool flag = false; + if (!productIds.empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : productIds) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ")"; + flag = true; + } + return flag; +} + +int32_t ProductInfoDao::ProductInfoToEntries(const ProductInfo& productInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, productInfo.GetProductId()); + values.PutString(DEVICE_MODEL, productInfo.GetModel()); + values.PutString(PRODUCT_NAME, productInfo.GetProductName()); + values.PutString(PRODUCT_SHORT_NAME, productInfo.GetProductShortName()); + values.PutInt(IMAGE_VERSION, productInfo.GetImageVersion()); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo) +{ + 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; + } + productInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + productInfo.SetModel(rowEntity.Get(DEVICE_MODEL)); + productInfo.SetProductName(rowEntity.Get(PRODUCT_NAME)); + productInfo.SetProductShortName(rowEntity.Get(PRODUCT_SHORT_NAME)); + productInfo.SetImageVersion(rowEntity.Get(IMAGE_VERSION)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file -- Gitee From da303d859ebb77561f50f8c798cbed7ccfc445c4 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Mon, 16 Dec 2024 11:40:09 +0800 Subject: [PATCH 08/55] ServiceProfileDao Signed-off-by: wangzhaohao --- .../distributed_device_profile_constants.h | 3 - .../distributed_device_profile_errors.h | 1 + common/include/interfaces/service_profile.h | 2 + .../distributed_device_profile_constants.cpp | 5 - common/src/interfaces/service_profile.cpp | 7 ++ services/core/BUILD.gn | 2 +- .../include/common/dp_services_constants.h | 7 +- .../service_profile_dao.h | 7 +- .../subscribe_profile_manager.h | 2 + .../core/src/common/dp_services_constants.cpp | 20 +++- .../service_profile_dao.cpp | 93 +++++++++++++++++-- .../subscribe_profile_manager.cpp | 40 ++++++++ .../core/test/unittest/profile_cache_test.cpp | 10 +- 13 files changed, 172 insertions(+), 27 deletions(-) rename services/core/include/{profiledao => profiledatamanager}/service_profile_dao.h (89%) rename services/core/src/{profiledao => profiledatamanager}/service_profile_dao.cpp (77%) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 3c7b0d04..ed5b90c0 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -278,9 +278,6 @@ extern const std::string SELECT_ACCESSEE_TABLE_WHERE_ALL; extern const std::string SELECT_ACCESSER_TABLE_WHERE_ACCESSERDEVICEID_AND_ACCESSERUSERID; extern const std::string SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEDEVICEID_AND_ACCESSEEUSERID; extern const std::string SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSERID_AND_ACCESSEEID; -/* profileData */ -extern const std::string SELECT_SERVICE_PROGILES; -extern const std::string ID_EQUAL_CONDITION; /* SubscribeTrustInfoManager */ extern const std::string SUBSCRIBE_TRUST_INFO_TABLE; extern const std::string CREATE_SUBSCRIBE_TRUST_INFO_TABLE_SQL; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index ee27a0ce..29d1c874 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -166,6 +166,7 @@ constexpr int32_t DP_RDB_PUT_SERVICE_PROFILE_FAIL = 98566284; constexpr int32_t DP_RDB_DELETE_SERVICE_PROFILE_FAIL = 98566285; constexpr int32_t DP_RDB_UPDATE_SERVICE_PROFILE_FAIL = 98566286; constexpr int32_t DP_RDB_GET_SERVICE_PROFILE_FAIL = 98566287; +constexpr int32_t DP_RDB_SET_SERVICE_PROFILE_ID_FAIL = 98566288; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/service_profile.h b/common/include/interfaces/service_profile.h index cc71ff81..6b4d9791 100644 --- a/common/include/interfaces/service_profile.h +++ b/common/include/interfaces/service_profile.h @@ -28,6 +28,8 @@ public: ServiceProfile(const std::string& deviceId, const std::string& serviceName, const std::string& serviceType); ServiceProfile(const std::string& deviceId, const std::string& serviceName, const std::string& serviceType, const bool isMultiUser, const int32_t userId); + ServiceProfile(const std::string& deviceId, const std::string& serviceName, const std::string& serviceType, + bool isMultiUser, int32_t userId, int32_t id, int32_t deviceProfileId); ServiceProfile(); ~ServiceProfile(); diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index e50b2e4f..8691fd0b 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -316,11 +316,6 @@ const std::string SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEDEVICEID_AND_ACCESSEEUSERI "SELECT * FROM accessee_table WHERE accesseeDeviceId = ? and accesseeUserId = ? "; const std::string SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSERID_AND_ACCESSEEID = "SELECT * FROM access_control_table WHERE accesserId = ? and accesseeId = ? "; -/* profileData */ -const std::string SELECT_SERVICE_PROGILES = - "SELECT sp.*, dp.udid, dp.userId FROM service_profile sp \ - LEFT JOIN device_profile dp ON dp.id=sp.deviceProfileId WHERE "; -const std::string ID_EQUAL_CONDITION = "id = ?"; /* SubscribeTrustInfoManager */ const std::string SUBSCRIBE_TRUST_INFO_TABLE = "subscribe_trust_info_table"; const std::string CREATE_SUBSCRIBE_TRUST_INFO_TABLE_SQL = diff --git a/common/src/interfaces/service_profile.cpp b/common/src/interfaces/service_profile.cpp index 95a103bd..a8dcfbb1 100644 --- a/common/src/interfaces/service_profile.cpp +++ b/common/src/interfaces/service_profile.cpp @@ -35,6 +35,13 @@ ServiceProfile::ServiceProfile(const std::string& deviceId, const std::string& s userId_(userId) { } +ServiceProfile::ServiceProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& serviceType, bool isMultiUser, int32_t userId, int32_t id, int32_t deviceProfileId) + : deviceId_(deviceId), serviceName_(serviceName), serviceType_(serviceType), isMultiUser_(isMultiUser), + userId_(userId), id_(id), deviceProfileId_(deviceProfileId) +{ +} + ServiceProfile::ServiceProfile() { } diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index eae8ad6c..e9fbe8e8 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -92,7 +92,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", "src/profiledatamanager/device_profile_dao.cpp", - "src/profiledao/service_profile_dao.cpp", + "src/profiledatamanager/service_profile_dao.cpp", "src/publishcommonevent/dp_account_common_event.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 265d990d..1227e4a8 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -30,6 +30,11 @@ extern const std::string SELECT_DEVICE_PROFILE_TABLE; extern const std::string SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID_ACCOUNTID; extern const std::string CREATE_DEVICE_PROFILE_TABLE_SQL; extern const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL; +/* ServiceProfileDao */ +extern const std::string SELECT_SERVICE_PROGILES; +extern const std::string SELECT_SERVICE_PROFILE_TABLE_MAX_ID; +extern const std::string CREATE_SERVICE_PROFILE_TABLE_SQL; +extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL; } // namespace DistributedDeviceProfile } // namespace OHOS -#endif // OHOS_DP_SERVICES_CONSTANTS_H \ No newline at end of file +#endif // OHOS_DP_SERVICES_CONSTANTS_H diff --git a/services/core/include/profiledao/service_profile_dao.h b/services/core/include/profiledatamanager/service_profile_dao.h similarity index 89% rename from services/core/include/profiledao/service_profile_dao.h rename to services/core/include/profiledatamanager/service_profile_dao.h index 693f04a7..69b407df 100644 --- a/services/core/include/profiledao/service_profile_dao.h +++ b/services/core/include/profiledatamanager/service_profile_dao.h @@ -25,12 +25,16 @@ class ServiceProfileDao { public: ServiceProfileDao() = default; ~ServiceProfileDao() = default; - int32_t AddServiceProfile(ServiceProfile& serviceProfile); + int32_t Init(); + int32_t UnInit(); + int32_t PutServiceProfile(ServiceProfile& serviceProfile); int32_t DeleteServiceProfile(ServiceProfile& serviceProfile); int32_t UpdateServiceProfile(ServiceProfile& serviceProfile); int32_t GetServiceProfiles(ServiceProfileFilterOptions& filterOptions, std::vector& serviceProfiles); private: + int32_t CreateTable(); + int32_t CreateIndex(); int32_t ConvertToServiceProfile(std::shared_ptr resultSet, ServiceProfile& serviceProfile); int32_t CreateQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, std::string& sql, std::vector& condition); @@ -39,6 +43,7 @@ private: void CreateComplexQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, std::string& whereSql, std::vector& condition); void ServiceProfileToEntries(const ServiceProfile& serviceProfile, ValuesBucket& values); + int32_t SetServiceProfileId(ServiceProfile& serviceProfile); }; diff --git a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h index 3e169876..ecdbd0bc 100644 --- a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h +++ b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h @@ -49,6 +49,8 @@ public: const DeviceProfile& newProfile); int32_t NotifyDeviceProfileDelete(const DeviceProfile& deviceProfile); int32_t NotifyServiceProfileAdd(const ServiceProfile& serviceProfile); + int32_t NotifyServiceProfileUpdate(const ServiceProfile& serviceProfile); + int32_t NotifyServiceProfileDelete(const ServiceProfile& serviceProfile); private: int32_t NotifyDeviceProfileAdd(const std::string& dbKey, const std::string& dbValue); diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index dfa4bbee..a109c3fd 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -59,5 +59,23 @@ const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = deviceId,\ userId,\ accountId);"; +/* ServiceProfileDao */ +const std::string SELECT_SERVICE_PROGILES = + "SELECT sp.*, dp.udid, dp.userId FROM service_profile sp \ + LEFT JOIN device_profile dp ON dp.id=sp.deviceProfileId WHERE "; +const std::string SELECT_SERVICE_PROFILE_TABLE_MAX_ID = "SELECT MAX(id) FROM service_profile"; +const std::string CREATE_SERVICE_PROFILE_TABLE_SQL = +"CREATE TABLE IF NOT EXISTS service_profile (\n" +" id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +" deviceProfileId INTEGER NOT NULL,\n" +" serviceId TEXT NOT NULL,\n" +" serviceType TEXT,\n" +" FOREIGN KEY (\n" +" deviceProfileId\n" +" )\n" +" REFERENCES device_profile (id)\n" +")" +const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL = +"CREATE INDEX if not exists serviceId_idx ON service_profile (serviceId)"; } // namespace DistributedDeviceProfile -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/core/src/profiledao/service_profile_dao.cpp b/services/core/src/profiledatamanager/service_profile_dao.cpp similarity index 77% rename from services/core/src/profiledao/service_profile_dao.cpp rename to services/core/src/profiledatamanager/service_profile_dao.cpp index 6e2c972b..d1f91dba 100644 --- a/services/core/src/profiledao/service_profile_dao.cpp +++ b/services/core/src/profiledatamanager/service_profile_dao.cpp @@ -13,15 +13,18 @@ * limitations under the License. */ +#include "service_profile_dao.h" #include + #include "content_sensor_manager_utils.h" #include "device_profile_manager.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" -#include "service_profile_dao.h" +#include "dp_services_constants.h" #include "subscribe_profile_manager.h" #include "profile_cache.h" +#include "profile_data_rdb_adapter.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -29,7 +32,35 @@ namespace { const std::string TAG = "DistributedDeviceProfile"; } -int32_t ServiceProfileDao::AddServiceProfile(ServiceProfile& serviceProfile) +int32_t ServiceProfileDao::Init() +{ + if (!ProfileDataRdbAdapter::GetInstance().IsInit()) { + if (ProfileDataRdbAdapter::GetInstance().Init() != DP_SUCCESS) { + HILOGE("ProfileDataRdbAdapter Init failed"); + return DP_INIT_DB_FAILED; + } + } + CreateTable(); + CreateIndex(); + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::UnInit() +{ + if (!ProfileDataRdbAdapter::GetInstance().IsInit()) { + HILOGE("ProfileDataRdbAdapter is UnInit"); + return DP_SUCCESS; + } + if (ProfileDataRdbAdapter::GetInstance().UnInit() != DP_SUCCESS) { + HILOGE("ProfileDataRdbAdapter UnInit failed"); + return DP_UNINIT_FAIL; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::PutServiceProfile(ServiceProfile& serviceProfile) { if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || serviceProfile.GetServiceType().empty() || serviceProfile.GetDeviceProfileId() == DEFAULT_DEVICE_PROFILE_ID) { @@ -39,12 +70,14 @@ int32_t ServiceProfileDao::AddServiceProfile(ServiceProfile& serviceProfile) ValuesBucket values; ServiceProfileToEntries(serviceProfile, values); int64_t rowId = ROWID_INIT; - int32_t ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, SERVICE_PROFILE_TABLE, values); - if (ret != DP_SUCCESS) { + if (ProfileDataRdbAdapter::GetInstance().Put(rowId, SERVICE_PROFILE_TABLE, values) != DP_SUCCESS) { HILOGE("service_profile insert failed"); return DP_RDB_PUT_SERVICE_PROFILE_FAIL; } - //todo: set id + if (SetServiceProfileId(serviceProfile) != DP_SUCCESS) { + HILOGE("SetServiceProfileId fail"); + return DP_RDB_SET_SERVICE_PROFILE_ID_FAIL; + } SubscribeProfileManager::GetInstance().NotifyServiceProfileAdd(serviceProfile); std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); if (localUdid == serviceProfile.GetDeviceId() ) { @@ -68,10 +101,11 @@ int32_t ServiceProfileDao::DeleteServiceProfile(ServiceProfile& serviceProfile) HILOGE("delete service_profile data failed"); return DP_RDB_DELETE_SERVICE_PROFILE_FAIL; } - SubscribeProfileManager::GetInstance().NotifyDeviceProfileDelete(deviceProfile); + SubscribeProfileManager::GetInstance().NotifyServiceProfileDelete(serviceProfile); std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); if (localUdid == serviceProfile.GetDeviceId() ) { - DeviceProfileManager::GetInstance().DeleteServiceProfile(serviceProfile.); + DeviceProfileManager::GetInstance().DeleteServiceProfile(serviceProfile.GetDeviceId(), + serviceProfile.GetServiceName(), true, serviceProfile.GetUserId()); } HILOGI("end!"); return DP_SUCCESS; @@ -90,11 +124,11 @@ int32_t ServiceProfileDao::UpdateServiceProfile(ServiceProfile& serviceProfile) int32_t ret = ProfileDataRdbAdapter::GetInstance().Update( changeRowCnt, SERVICE_PROFILE_TABLE, values, ID_EQUAL_CONDITION, std::vector{ ValueObject(serviceProfile.GetId()) }); - if (ret != DP_SUCCESS) {fc + if (ret != DP_SUCCESS) { HILOGE("Update service_profile table failed"); return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL; } - SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(oldProfile, newProfile); + SubscribeProfileManager::GetInstance().NotifyServiceProfileUpdate(serviceProfile); std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); if (localUdid == serviceProfile.GetDeviceId() ) { DeviceProfileManager::GetInstance().PutServiceProfile(serviceProfile); @@ -116,7 +150,7 @@ int32_t DistributedDeviceProfile::ServiceProfileDao::GetServiceProfiles(ServiceP HILOGE("CreateQuerySqlAndCondition failed!"); return DP_INVALID_PARAMS; } - std::shared_ptr resultSet = ProfileDataAdapter::GetInstance().Get(sql, condition); + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); if (resultSet == nullptr) { HILOGE("resultSet is nullptr"); return DP_GET_RESULTSET_FAIL; @@ -141,6 +175,26 @@ int32_t DistributedDeviceProfile::ServiceProfileDao::GetServiceProfiles(ServiceP return DP_SUCCESS; } +int32_t ServiceProfileDao::CreateTable() +{ + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_SERVICE_PROFILE_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("service_profile create failed"); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t ServiceProfileDao::CreateIndex() +{ + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("service_profile unique index create failed"); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + int32_t ServiceProfileDao::ConvertToServiceProfile(std::shared_ptr resultSet, ServiceProfile& serviceProfile) { @@ -253,5 +307,24 @@ void ServiceProfileDao::ServiceProfileToEntries(const ServiceProfile& servicePro values.PutString(SERVICE_PROFILE_SERVICE_ID, serviceProfile.GetServiceName()); values.PutString(SERVICE_PROFILE_SERVICE_TYPE, serviceProfile.GetServiceType()); } + +int32_t ServiceProfileDao::SetServiceProfileId(ServiceProfile& serviceProfile) +{ + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get( + SELECT_SERVICE_PROFILE_TABLE_MAX_ID, std::vector{}); + 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); + serviceProfile.SetId(id); + } + resultSet->Close(); + return DP_SUCCESS; +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp index 95fcd514..1e8811df 100644 --- a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp +++ b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp @@ -621,5 +621,45 @@ int32_t SubscribeProfileManager::NotifyServiceProfileAdd(const ServiceProfile& s } return DP_SUCCESS; } + +int32_t SubscribeProfileManager::NotifyServiceProfileUpdate(const ServiceProfile& serviceProfile) +{ + auto subscriberInfos = GetSubscribeInfos(SERVICE_PROFILE_TABLE); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", serviceProfile.dump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_UPDATE) != 0) { + listenerProxy->OnServiceProfileUpdate(ServiceProfile oldProfile, serviceProfile); + } + } + return DP_SUCCESS; +} + +int32_t SubscribeProfileManager::NotifyServiceProfileDelete(const ServiceProfile& serviceProfile) +{ + auto subscriberInfos = GetSubscribeInfos(SERVICE_PROFILE_TABLE); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", serviceProfile.dump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_DELETE) != 0) { + listenerProxy->OnDeviceProfileDelete(serviceProfile); + } + } + return DP_SUCCESS; +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/test/unittest/profile_cache_test.cpp b/services/core/test/unittest/profile_cache_test.cpp index 9cb605a3..356ddf5c 100644 --- a/services/core/test/unittest/profile_cache_test.cpp +++ b/services/core/test/unittest/profile_cache_test.cpp @@ -78,22 +78,22 @@ HWTEST_F(ProfileCacheTest, AddDeviceProfile_001, TestSize.Level2) HWTEST_F(ProfileCacheTest, AddServiceProfile_001, TestSize.Level2) { ServiceProfile serviceProfile; - int32_t ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); + int32_t ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); EXPECT_EQ(DP_INVALID_PARAMS, ret); std::string devId = "dp_devId"; serviceProfile.SetDeviceId(devId); - ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); EXPECT_EQ(DP_INVALID_PARAMS, ret); std::string serName = "dp_serName"; serviceProfile.SetServiceName(serName); - ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); EXPECT_EQ(DP_SUCCESS, ret); devId = ""; serviceProfile.SetDeviceId(devId); - ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); EXPECT_EQ(DP_INVALID_PARAMS, ret); devId = "dp_devId"; @@ -101,7 +101,7 @@ HWTEST_F(ProfileCacheTest, AddServiceProfile_001, TestSize.Level2) for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) { ProfileCache::GetInstance().serviceProfileMap_[std::to_string(i)] = serviceProfile; } - ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret); } -- Gitee From 08fd05f7bd9900277f1ebe226ba213539fb65fa3 Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 17:16:08 +0800 Subject: [PATCH 09/55] =?UTF-8?q?=E6=96=B0=E5=A2=9EProductInfoDao=E3=80=81?= =?UTF-8?q?DeviceIconInfoDao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/BUILD.gn | 3 + .../distributed_device_profile_constants.h | 10 + .../distributed_device_profile_errors.h | 7 + common/include/interfaces/device_icon_info.h | 6 +- .../device_icon_info_filter_options.h | 53 ++++ common/include/interfaces/product_info.h | 6 +- .../distributed_device_profile_constants.cpp | 13 +- common/src/interfaces/device_icon_info.cpp | 16 +- .../device_icon_info_filter_options.cpp | 132 ++++++++++ common/src/interfaces/product_info.cpp | 35 ++- common/src/utils/ipc_utils.cpp | 14 + services/core/BUILD.gn | 2 + .../profiledatamanager/device_icon_info_dao.h | 56 ++++ .../profiledatamanager/product_info_dao.h | 55 ++++ .../core/src/common/dp_services_constants.cpp | 18 ++ .../device_icon_info_dao.cpp | 240 ++++++++++++++++++ .../profiledatamanager/product_info_dao.cpp | 212 ++++++++++++++++ 17 files changed, 842 insertions(+), 36 deletions(-) create mode 100644 common/include/interfaces/device_icon_info_filter_options.h create mode 100644 common/src/interfaces/device_icon_info_filter_options.cpp create mode 100644 services/core/include/profiledatamanager/device_icon_info_dao.h create mode 100644 services/core/include/profiledatamanager/product_info_dao.h create mode 100644 services/core/src/profiledatamanager/device_icon_info_dao.cpp create mode 100644 services/core/src/profiledatamanager/product_info_dao.cpp diff --git a/common/BUILD.gn b/common/BUILD.gn index 5cb74819..dcf9f2a3 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -50,6 +50,8 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accessee.cpp", "src/interfaces/accesser.cpp", "src/interfaces/characteristic_profile.cpp", + "src/interfaces/device_icon_info_filter_options.cpp", + "src/interfaces/device_icon_info.cpp", "src/interfaces/device_profile.cpp", "src/interfaces/device_profile_filter_options.cpp", "src/interfaces/dp_inited_callback_proxy.cpp", @@ -57,6 +59,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/dp_subscribe_info.cpp", "src/interfaces/dp_sync_options.cpp", "src/interfaces/i_profile_change_listener.cpp", + "src/interfaces/product_info.cpp", "src/interfaces/profile_change_listener_proxy.cpp", "src/interfaces/profile_change_listener_stub.cpp", "src/interfaces/service_profile.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index f67e082d..549e99fa 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -63,12 +63,22 @@ extern const std::string MODIFY_TIME; extern const std::string SHARE_TIME; extern const std::string PRODUCTOR_INFO_VERSION; extern const std::string DEVICE_PROFILE_TABLE; +extern const std::string DEVICE_ICON_INFO_TABLE; +extern const std::string PRODUCT_INFO_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; +/* ProductInfo Attribute */ +extern const std::string PRODUCT_NAME; +extern const std::string PRODUCT_SHORT_NAME; +extern const std::string IMAGE_VERSION; +/* DeviceIconInfo Attribute */ +extern const std::string IMAGE_TYPE; +extern const std::string SPEC_NAME; +extern const std::string ICON; /* TrustDeviceProfile Attribute */ extern const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE; extern const std::string DEVICE_ID_TYPE; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 86fa4d08..8aad772f 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -160,6 +160,13 @@ 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_DEVICE_ICON_INFO_FAIL = 98566284; +constexpr int32_t DP_DEL_DEVICE_ICON_INFO_FAIL = 98566286; +constexpr int32_t DP_UPDATE_DEVICE_ICON_INFO_FAIL = 98566287; +constexpr int32_t DP_PUT_PRODUCT_INFO_FAIL = 98566288; +constexpr int32_t DP_DEL_PRODUCT_INFO_FAIL = 98566280; +constexpr int32_t DP_UPDATE_PRODUCT_INFO_FAIL = 98566290; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h index 4d0a98d6..c75d93dc 100644 --- a/common/include/interfaces/device_icon_info.h +++ b/common/include/interfaces/device_icon_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_DEVICE_ICON_INFO_H #define OHOS_DP_DEVICE_ICON_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -28,10 +28,10 @@ public: DeviceIconInfo() : id_(0), productId_(""), - subProductId_(nullptr), + subProductId_(""), imageType(""), specName(""), - icon_(nullptr), + icon_(""), {} ~DeviceIconInfo() = default; int32_t GetId() const; diff --git a/common/include/interfaces/device_icon_info_filter_options.h b/common/include/interfaces/device_icon_info_filter_options.h new file mode 100644 index 00000000..fc4f5e40 --- /dev/null +++ b/common/include/interfaces/device_icon_info_filter_options.h @@ -0,0 +1,53 @@ + /* + * 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_DEVICE_ICON_INFO_FILTER_OPTIONS_H +#define OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H + +#include +#include + +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceIconInfoFilterOptions : public DpParcel { +public: + DeviceIconInfoFilterOptions() :subProductId_(""), imageType_(""),specName_("") + {} + ~DeviceIconInfoFilterOptions() = default; + + std::vector GetProductIds() const; + void SetProductIds(const std::vector& productIds); + std::string GetSubProductId() const; + void SetSubProductId(const std::string& subProductId); + std::string GetImageType() const; + void SetImageType(const std::string& imageType); + std::string GetSpecName() const; + void SetSpecName(const std::string& specName); + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + std::vector productIds_; + std::string subProductId_; + std::string imageType_; + std::string specName_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h index c1eb94fe..121fe31e 100644 --- a/common/include/interfaces/product_info.h +++ b/common/include/interfaces/product_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_PRODUCT_INFO_H #define OHOS_DP_PRODUCT_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -26,10 +26,10 @@ namespace DistributedDeviceProfile { class ProductInfo : public DpParcel { public: ProductInfo() : productId_(""), - model_(nullptr), + model_(""), productName_(""), productShortName_(""), - imageVersion_(nullptr), + imageVersion_(""), {} ~ProductInfo() = default; diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index ae381843..5c1406ff 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -26,7 +26,7 @@ const std::string DEVICE_TYPE_ID = "deviceTypeId"; const std::string DEVICE_TYPE_NAME = "deviceTypeName"; const std::string DEVICE_NAME = "deviceIdName"; const std::string MANUFACTURE_NAME = "manufactureName"; -const std::string DEVICE_MODEL = "deviceModel"; +const std::string DEVICE_MODEL = "model"; const std::string STORAGE_CAPACITY = "storageCapacity"; const std::string OS_SYS_CAPACITY = "osSysCapacity"; const std::string OS_API_LEVEL = "osApiLevel"; @@ -53,12 +53,23 @@ const std::string MODIFY_TIME = "modifyTime"; const std::string SHARE_TIME = "shareTime"; const std::string PRODUCTOR_INFO_VERSION = "productorInfoVersion"; const std::string DEVICE_PROFILE_TABLE = "device_profile"; +const std::string DEVICE_ICON_INFO_TABLE = "device_icon_info"; +const std::string PRODUCT_INFO_TABLE = "product_info"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; +const std::string SERVICE_NAME = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; +/* ProductInfo Attribute */ +const std::string PRODUCT_NAME = "productName"; +const std::string PRODUCT_SHORT_NAME = "productShortName"; +const std::string IMAGE_VERSION = "imageVersion"; +/* DeviceIconInfo Attribute */ +const std::string IMAGE_TYPE = "imageType"; +const std::string SPEC_NAME = "specName"; +const std::string ICON = "icon"; /* TrustDeviceProfile Attribute */ const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE = "trust_device_profile"; const std::string DEVICE_ID_TYPE = "deviceIdType"; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 09204b9f..2857a7c7 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -14,18 +14,15 @@ */ #include "device_icon_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "DeviceIconInfo"; - const std::string PRODUCT_INFO_ID = "id"; - const std::string PRODUCT_ID = "productId"; - const std::string SUB_PRODUCT_ID = "subProductId"; - const std::string IMAGE_TYPE = "imageType"; - const std::string SPEC_NAME = "specName"; } int32_t DeviceIconInfo::GetId() const @@ -88,7 +85,6 @@ void DeviceIconInfo::SetIcon(const std::vector& icon) this->icon_ = icon; } - bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, Int32, id_, false); @@ -96,7 +92,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); - WRITE_HELPER_RET(parcel, Int64, icon_, false); + WRITE_HELPER_RET(parcel, UInt8Vector, icon_); return true; } @@ -107,7 +103,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); - READ_HELPER_RET(parcel, Int64, icon_, false); + READ_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -123,13 +119,13 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } - cJSON_AddNumberToObject(json, PRODUCT_INFO_ID.c_str(), id_); + cJSON_AddNumberToObject(json, ID.c_str(), id_); cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); diff --git a/common/src/interfaces/device_icon_info_filter_options.cpp b/common/src/interfaces/device_icon_info_filter_options.cpp new file mode 100644 index 00000000..a6cf7233 --- /dev/null +++ b/common/src/interfaces/device_icon_info_filter_options.cpp @@ -0,0 +1,132 @@ +/* + * 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 "device_icon_info_filter_options.h" + +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_log.h" +#include "ipc_utils.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DeviceIconInfoFilterOptions"; +} + +std::vector DistributedDeviceProfile::GetProductIds() const +{ + return productIds_; +} + +void DistributedDeviceProfile::SetProductIds(const std::vector& productIds) +{ + specName_ = specName; +} + +std::string DistributedDeviceProfile::GetSubProductId() const +{ + return subProductId_; +} + +void DistributedDeviceProfile::SetSubProductId(const std::string& subProductId) +{ + subProductId_ = subProductId; +} + +std::string DistributedDeviceProfile::GetImageType() const +{ + return imageType_; +} + +void DistributedDeviceProfile::SetImageType(const std::string& imageType) +{ + imageType_ = imageType; +} + +std::string DistributedDeviceProfile::GetSpecName() const +{ + return specName_; +} + +void DistributedDeviceProfile::SetSpecName(const std::string& specName) +{ + specName_ = specName; +} + +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const +{ + IpcUtils::Marshalling(parcel, productIds_); + WRITE_HELPER_RET(parcel, String, subProductId_, false); + WRITE_HELPER_RET(parcel, String, imageType_, false); + WRITE_HELPER_RET(parcel, String, specName_, false); + return true; +} + +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) +{ + IpcUtils::UnMarshalling(parcel, productIds_); + READ_HELPER_RET(parcel, String, subProductId_, false); + READ_HELPER_RET(parcel, String, imageType_, false); + READ_HELPER_RET(parcel, String, specName_, false); + return true; +} + +std::string DistributedDeviceProfile::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + HILOGE("cJSON CreateObject failed!"); + return EMPTY_STRING; + } + cJSON* prodIdsJson = cJSON_CreateArray(); + if (prodIdsJson == NULL) { + HILOGE("cJSON CreateArray failed!"); + return EMPTY_STRING; + } + for (const auto& value : productIds_) { + cJSON* arrItem = cJSON_CreateString(value.c_str()); + if (arrItem == NULL) { + HILOGW("cJSON CreateString failed!"); + continue; + } + if (!cJSON_AddItemToArray(prodIdsJson, arrItem)) { + cJSON_Delete(arrItem); + HILOGW("Add item to array failed!"); + continue; + } + } + if (!cJSON_AddItemToObject(json, PRODUCT_IDS.c_str(), prodIdsJson)) { + cJSON_Delete(prodIdsJson); + HILOGE("Add json array to Object failed!"); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + 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 diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp index 82eaf476..04d8f1e3 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -14,18 +14,15 @@ */ #include "product_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "ProductInfo"; - const std::string MODEL = "model"; - const std::string PRODUCT_ID = "productId"; - const std::string PRODUCT_NAME = "productName"; - const std::string PRODUCT_SHORT_NAME = "productShortName"; - const std::string IMAGE_VERSION = "imageVersion"; } std::string DeviceIconInfo::GetProuctId() const @@ -33,53 +30,53 @@ std::string DeviceIconInfo::GetProuctId() const return productId_; } -void DeviceIconInfo::SetProuctId(const std::string& productId) +void DistributedDeviceProfile::SetProuctId(const std::string& productId) { this->productId_ = productId; } -std::string DeviceIconInfo::GetModel() const +std::string DistributedDeviceProfile::GetModel() const { return model_; } -void DeviceIconInfo::SetModel(const std::string& model) +void DistributedDeviceProfile::SetModel(const std::string& model) { this->model_ = model; } -std::string DeviceIconInfo::GetProductName() const +std::string DistributedDeviceProfile::GetProductName() const { return productName_; } -void DeviceIconInfo::SetProductName(const std::string& productName) +void DistributedDeviceProfile::SetProductName(const std::string& productName) { this->productName_ = productName; } -std::string DeviceIconInfo::GetProductShortName() const +std::string DistributedDeviceProfile::GetProductShortName() const { return productShortName_; } -void DeviceIconInfo::SetProductShortName(const std::string& productShortName) +void DistributedDeviceProfile::SetProductShortName(const std::string& productShortName) { this->productShortName_ = productShortName; } -int32_t DeviceIconInfo::GetImageVersion() const +int32_t DistributedDeviceProfile::GetImageVersion() const { return imageVersion_; } -void DeviceIconInfo::SetImageVersion(const int32_t imageVersion) +void DistributedDeviceProfile::SetImageVersion(const int32_t imageVersion) { this->ImageVersion_ = imageVersion; } -bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, productId_, false); WRITE_HELPER_RET(parcel, String, model_, false); @@ -89,7 +86,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const return true; } -bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, String, productId_, false); READ_HELPER_RET(parcel, String, model_, false); @@ -99,7 +96,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) return true; } -bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const +bool DistributedDeviceProfile::operator!=(const DistributedDeviceProfile& other) const { bool isNotEqual = (productId_ != other.GetProuctId() || model_ != other.GetModel() || productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() || @@ -111,14 +108,14 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); - cJSON_AddStringToObject(json, MODEL.c_str(), model_); + cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_); cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_); cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_); cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index b51fc826..8b937463 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -115,6 +115,20 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& pa return true; } +bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) +{ + if (params.empty() || params.size() > MAX_ID_SIZE) { + HILOGE("params size is invalid!size : %{public}zu", params.size()); + return false; + } + size_t size = params.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + for (const auto& item : params) { + WRITE_HELPER_RET(parcel, Uint8, item, false); + } + return true; +} + bool IpcUtils::Marshalling(MessageParcel& parcel, const std::map& params) { if (params.size() == 0 || params.size() > MAX_PARAM_SIZE) { diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 81b60a24..746d9b26 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -91,7 +91,9 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", + "src/profiledatamanager/device_icon_info_dao.cpp", "src/profiledatamanager/device_profile_dao.cpp", + "src/profiledatamanager/product_info_dao.cpp", "src/publishcommonevent/dp_account_common_event.cpp", "src/staticcapabilitycollector/static_capability_collector.cpp", "src/staticcapabilityloader/static_capability_loader.cpp", diff --git a/services/core/include/profiledatamanager/device_icon_info_dao.h b/services/core/include/profiledatamanager/device_icon_info_dao.h new file mode 100644 index 00000000..97e4371c --- /dev/null +++ b/services/core/include/profiledatamanager/device_icon_info_dao.h @@ -0,0 +1,56 @@ +/* + * 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_DEVICE_ICON_INFO_DAO_H +#define OHOS_DP_DEVICE_ICON_INFO_DAO_H + +#include +#include +#include +#include + +#include "device_icon_info.h" +#include "device_icon_info_filter_options.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class DeviceIconInfoDao { + DECLARE_SINGLE_INSTANCE(DeviceIconInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutDeviceIconInfo(DeviceIconInfo& deviceIconInfo); + int32_t GetDeviceIconInfos(const std::vector& productIds, + std::vector& deviceIconInfos); + int32_t DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values); + int32_t ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_DAO_H \ No newline at end of file diff --git a/services/core/include/profiledatamanager/product_info_dao.h b/services/core/include/profiledatamanager/product_info_dao.h new file mode 100644 index 00000000..ceddac7b --- /dev/null +++ b/services/core/include/profiledatamanager/product_info_dao.h @@ -0,0 +1,55 @@ +/* + * 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_PRODUCT_INFO_DAO_H +#define OHOS_DP_PRODUCT_INFO_DAO_H + +#include +#include +#include +#include + +#include "product_info.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class ProductInfoDao { + DECLARE_SINGLE_INSTANCE(ProductInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutProductInfo(ProductInfo& productInfo); + int32_t GetProductInfos(const std::vector& productIds, + std::vector& productInfos); + int32_t DeleteProductInfo(const ProductInfo& productInfo); + int32_t UpdateProductInfo(const ProductInfo& productInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const ProductInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t ProductInfoToEntries(const ProductInfo& ProductInfo, ValuesBucket& values); + int32_t ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_PRODUCT_INFO_DAO_H \ No newline at end of file diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index dfa4bbee..4563d3a4 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -59,5 +59,23 @@ const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = deviceId,\ userId,\ accountId);"; +// DeviceIconInfoDao +const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_icon_info\ +(id INTEGER PRIMARY KEY AUTOINCREMENT,\ +productId TEXT,\ +subProductId TEXT,\ +imageType TEXT,\ +specName TEXT,\ +icon blob);"; +const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL = "CREATE UNIQUE INDEX if not exists \ +unique_device_icon_info ON device_icon_info (productId,subProductId,imageType,specName);"; +const std::string SELECT_DEVICE_ICON_INFO_TABLE = "SELECT * FROM device_profile WHERE "; +// ProductInfoDao +const std::string CREATE_PRODUCT_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS product_info \ +(productId TEXT PRIMARY KEY,\ +model TEXT,\ +productName TEXT,\ +productShortName TEXT,\ +imageVersion INTEGER);"; } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp new file mode 100644 index 00000000..a116af07 --- /dev/null +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -0,0 +1,240 @@ +/* + * 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 "device_icon_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(DeviceIconInfoDao); +namespace { + const std::string TAG = "DeviceIconInfoDao"; +} + +int32_t DeviceIconInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, DEVICE_ICON_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_PUT_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::GetDeviceIconInfos(const DeviceIconInfoOptions& filterOptions, + std::vector& deviceIconInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(filterOptions, sql, condition)) { + HILOGE("invalid params:%{public}s", filterOptions.dump().c_str()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + DeviceIconInfo deviceIconInfo; + ConvertToDeviceIconInfo(resultSet, deviceIconInfo); + deviceIconInfos.emplace_back(deviceIconInfo); + } + resultSet->Close(); + if (deviceIconInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, DEVICE_ICON_INFO_TABLE, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_DEL_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, DEVICE_ICON_INFO_TABLE, values, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_UPDATE_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateIndex() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s unique index create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + +bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition) +{ + sql = SELECT_DEVICE_ICON_INFO_TABLE; + bool flag = false; + if (!filterOptions.GetProductIds().empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : filterOptions.GetProductIds()) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (!filterOptions.GetSubProductId().empty()) { + sql += SUB_PRODUCT_ID + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + flag = true; + } + if (!filterOptions.GetImageType().empty()) { + sql += IMAGE_TYPE + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (!filterOptions.GetSpecName().empty()) { + sql += SPEC_NAME + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (flag) { + sql.erase(sql.end() - 4); + return flag; + } + return flag; +} + +int32_t DeviceIconInfoDao::DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, deviceIconInfo.GetProductId()); + values.PutString(SUB_PRODUCT_ID, deviceIconInfo.GetSubProductId()); + values.PutString(IMAGE_TYPE, deviceIconInfo.GetImageType()); + values.PutString(SPEC_NAME, deviceIconInfo.GetSpecName()); + values.PutBlob(ICON, deviceIconInfo.GetIcon()); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo) +{ + 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; + } + deviceIconInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + deviceIconInfo.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID)); + deviceIconInfo.SetImageType(rowEntity.Get(IMAGE_TYPE)); + deviceIconInfo.SetSpecName(rowEntity.Get(SPEC_NAME)); + // std::vector icon; + // rowEntity.Get(ICON).GetBlob(icon); + // deviceIconInfo.SetIcon(icon); + deviceIconInfo.SetIcon(rowEntity.Get(ICON)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp new file mode 100644 index 00000000..43955592 --- /dev/null +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -0,0 +1,212 @@ +/* + * 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 "product_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProductInfoDao); +namespace { + const std::string TAG = "ProductInfoDao"; +} + +int32_t ProductInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::PutProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, PRODUCT_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", PRODUCT_INFO_TABLE.c_str()); + return DP_PUT_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::GetProductInfos(const std::vector& productIds, + std::vector& productInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(productIds, sql, condition)) { + HILOGE("invalid params: productIds.size=%{public}zu", productIds.size()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + ProductInfo productInfo; + ConvertToProductInfo(resultSet, productInfo); + productInfos.emplace_back(productInfo); + } + resultSet->Close(); + if (productInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::DeleteProductInfo(const ProductInfo& productInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, PRODUCT_INFO_TABLE, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", PRODUCT_INFO_TABLE.c_str()); + return DP_DEL_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::UpdateProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, PRODUCT_INFO_TABLE, values, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", PRODUCT_INFO_TABLE.c_str()); + return DP_UPDATE_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_PRODUCT_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateIndex() +{ + return DP_SUCCESS; +} + +bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& productIds, + std::string& sql, std::vector& condition) +{ + sql = SELECT_PRODUCT_INFO_TABLE; + bool flag = false; + if (!productIds.empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : productIds) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ")"; + flag = true; + } + return flag; +} + +int32_t ProductInfoDao::ProductInfoToEntries(const ProductInfo& productInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, productInfo.GetProductId()); + values.PutString(DEVICE_MODEL, productInfo.GetModel()); + values.PutString(PRODUCT_NAME, productInfo.GetProductName()); + values.PutString(PRODUCT_SHORT_NAME, productInfo.GetProductShortName()); + values.PutInt(IMAGE_VERSION, productInfo.GetImageVersion()); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo) +{ + 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; + } + productInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + productInfo.SetModel(rowEntity.Get(DEVICE_MODEL)); + productInfo.SetProductName(rowEntity.Get(PRODUCT_NAME)); + productInfo.SetProductShortName(rowEntity.Get(PRODUCT_SHORT_NAME)); + productInfo.SetImageVersion(rowEntity.Get(IMAGE_VERSION)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file -- Gitee From 6b97b769b5a1bc79c03f021a8aa00a0eef374052 Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 17:16:08 +0800 Subject: [PATCH 10/55] =?UTF-8?q?=E6=96=B0=E5=A2=9EProductInfoDao=E3=80=81?= =?UTF-8?q?DeviceIconInfoDao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/BUILD.gn | 3 + .../distributed_device_profile_constants.h | 10 + .../distributed_device_profile_errors.h | 7 + common/include/interfaces/device_icon_info.h | 6 +- .../device_icon_info_filter_options.h | 53 ++++ common/include/interfaces/product_info.h | 6 +- .../distributed_device_profile_constants.cpp | 13 +- common/src/interfaces/device_icon_info.cpp | 16 +- .../device_icon_info_filter_options.cpp | 132 ++++++++++ common/src/interfaces/device_profile.cpp | 4 +- common/src/interfaces/product_info.cpp | 35 ++- common/src/utils/ipc_utils.cpp | 14 + services/core/BUILD.gn | 2 + .../profiledatamanager/device_icon_info_dao.h | 56 ++++ .../profiledatamanager/product_info_dao.h | 55 ++++ .../core/src/common/dp_services_constants.cpp | 18 ++ .../device_icon_info_dao.cpp | 240 ++++++++++++++++++ .../profiledatamanager/product_info_dao.cpp | 212 ++++++++++++++++ 18 files changed, 844 insertions(+), 38 deletions(-) create mode 100644 common/include/interfaces/device_icon_info_filter_options.h create mode 100644 common/src/interfaces/device_icon_info_filter_options.cpp create mode 100644 services/core/include/profiledatamanager/device_icon_info_dao.h create mode 100644 services/core/include/profiledatamanager/product_info_dao.h create mode 100644 services/core/src/profiledatamanager/device_icon_info_dao.cpp create mode 100644 services/core/src/profiledatamanager/product_info_dao.cpp diff --git a/common/BUILD.gn b/common/BUILD.gn index 5cb74819..dcf9f2a3 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -50,6 +50,8 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accessee.cpp", "src/interfaces/accesser.cpp", "src/interfaces/characteristic_profile.cpp", + "src/interfaces/device_icon_info_filter_options.cpp", + "src/interfaces/device_icon_info.cpp", "src/interfaces/device_profile.cpp", "src/interfaces/device_profile_filter_options.cpp", "src/interfaces/dp_inited_callback_proxy.cpp", @@ -57,6 +59,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/dp_subscribe_info.cpp", "src/interfaces/dp_sync_options.cpp", "src/interfaces/i_profile_change_listener.cpp", + "src/interfaces/product_info.cpp", "src/interfaces/profile_change_listener_proxy.cpp", "src/interfaces/profile_change_listener_stub.cpp", "src/interfaces/service_profile.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index f67e082d..549e99fa 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -63,12 +63,22 @@ extern const std::string MODIFY_TIME; extern const std::string SHARE_TIME; extern const std::string PRODUCTOR_INFO_VERSION; extern const std::string DEVICE_PROFILE_TABLE; +extern const std::string DEVICE_ICON_INFO_TABLE; +extern const std::string PRODUCT_INFO_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; +/* ProductInfo Attribute */ +extern const std::string PRODUCT_NAME; +extern const std::string PRODUCT_SHORT_NAME; +extern const std::string IMAGE_VERSION; +/* DeviceIconInfo Attribute */ +extern const std::string IMAGE_TYPE; +extern const std::string SPEC_NAME; +extern const std::string ICON; /* TrustDeviceProfile Attribute */ extern const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE; extern const std::string DEVICE_ID_TYPE; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 86fa4d08..8aad772f 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -160,6 +160,13 @@ 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_DEVICE_ICON_INFO_FAIL = 98566284; +constexpr int32_t DP_DEL_DEVICE_ICON_INFO_FAIL = 98566286; +constexpr int32_t DP_UPDATE_DEVICE_ICON_INFO_FAIL = 98566287; +constexpr int32_t DP_PUT_PRODUCT_INFO_FAIL = 98566288; +constexpr int32_t DP_DEL_PRODUCT_INFO_FAIL = 98566280; +constexpr int32_t DP_UPDATE_PRODUCT_INFO_FAIL = 98566290; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h index 4d0a98d6..c75d93dc 100644 --- a/common/include/interfaces/device_icon_info.h +++ b/common/include/interfaces/device_icon_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_DEVICE_ICON_INFO_H #define OHOS_DP_DEVICE_ICON_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -28,10 +28,10 @@ public: DeviceIconInfo() : id_(0), productId_(""), - subProductId_(nullptr), + subProductId_(""), imageType(""), specName(""), - icon_(nullptr), + icon_(""), {} ~DeviceIconInfo() = default; int32_t GetId() const; diff --git a/common/include/interfaces/device_icon_info_filter_options.h b/common/include/interfaces/device_icon_info_filter_options.h new file mode 100644 index 00000000..fc4f5e40 --- /dev/null +++ b/common/include/interfaces/device_icon_info_filter_options.h @@ -0,0 +1,53 @@ + /* + * 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_DEVICE_ICON_INFO_FILTER_OPTIONS_H +#define OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H + +#include +#include + +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceIconInfoFilterOptions : public DpParcel { +public: + DeviceIconInfoFilterOptions() :subProductId_(""), imageType_(""),specName_("") + {} + ~DeviceIconInfoFilterOptions() = default; + + std::vector GetProductIds() const; + void SetProductIds(const std::vector& productIds); + std::string GetSubProductId() const; + void SetSubProductId(const std::string& subProductId); + std::string GetImageType() const; + void SetImageType(const std::string& imageType); + std::string GetSpecName() const; + void SetSpecName(const std::string& specName); + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + std::vector productIds_; + std::string subProductId_; + std::string imageType_; + std::string specName_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h index c1eb94fe..121fe31e 100644 --- a/common/include/interfaces/product_info.h +++ b/common/include/interfaces/product_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_PRODUCT_INFO_H #define OHOS_DP_PRODUCT_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -26,10 +26,10 @@ namespace DistributedDeviceProfile { class ProductInfo : public DpParcel { public: ProductInfo() : productId_(""), - model_(nullptr), + model_(""), productName_(""), productShortName_(""), - imageVersion_(nullptr), + imageVersion_(""), {} ~ProductInfo() = default; diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index ae381843..5c1406ff 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -26,7 +26,7 @@ const std::string DEVICE_TYPE_ID = "deviceTypeId"; const std::string DEVICE_TYPE_NAME = "deviceTypeName"; const std::string DEVICE_NAME = "deviceIdName"; const std::string MANUFACTURE_NAME = "manufactureName"; -const std::string DEVICE_MODEL = "deviceModel"; +const std::string DEVICE_MODEL = "model"; const std::string STORAGE_CAPACITY = "storageCapacity"; const std::string OS_SYS_CAPACITY = "osSysCapacity"; const std::string OS_API_LEVEL = "osApiLevel"; @@ -53,12 +53,23 @@ const std::string MODIFY_TIME = "modifyTime"; const std::string SHARE_TIME = "shareTime"; const std::string PRODUCTOR_INFO_VERSION = "productorInfoVersion"; const std::string DEVICE_PROFILE_TABLE = "device_profile"; +const std::string DEVICE_ICON_INFO_TABLE = "device_icon_info"; +const std::string PRODUCT_INFO_TABLE = "product_info"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; +const std::string SERVICE_NAME = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; +/* ProductInfo Attribute */ +const std::string PRODUCT_NAME = "productName"; +const std::string PRODUCT_SHORT_NAME = "productShortName"; +const std::string IMAGE_VERSION = "imageVersion"; +/* DeviceIconInfo Attribute */ +const std::string IMAGE_TYPE = "imageType"; +const std::string SPEC_NAME = "specName"; +const std::string ICON = "icon"; /* TrustDeviceProfile Attribute */ const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE = "trust_device_profile"; const std::string DEVICE_ID_TYPE = "deviceIdType"; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 09204b9f..2857a7c7 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -14,18 +14,15 @@ */ #include "device_icon_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "DeviceIconInfo"; - const std::string PRODUCT_INFO_ID = "id"; - const std::string PRODUCT_ID = "productId"; - const std::string SUB_PRODUCT_ID = "subProductId"; - const std::string IMAGE_TYPE = "imageType"; - const std::string SPEC_NAME = "specName"; } int32_t DeviceIconInfo::GetId() const @@ -88,7 +85,6 @@ void DeviceIconInfo::SetIcon(const std::vector& icon) this->icon_ = icon; } - bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, Int32, id_, false); @@ -96,7 +92,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); - WRITE_HELPER_RET(parcel, Int64, icon_, false); + WRITE_HELPER_RET(parcel, UInt8Vector, icon_); return true; } @@ -107,7 +103,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); - READ_HELPER_RET(parcel, Int64, icon_, false); + READ_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -123,13 +119,13 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } - cJSON_AddNumberToObject(json, PRODUCT_INFO_ID.c_str(), id_); + cJSON_AddNumberToObject(json, ID.c_str(), id_); cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); diff --git a/common/src/interfaces/device_icon_info_filter_options.cpp b/common/src/interfaces/device_icon_info_filter_options.cpp new file mode 100644 index 00000000..a6cf7233 --- /dev/null +++ b/common/src/interfaces/device_icon_info_filter_options.cpp @@ -0,0 +1,132 @@ +/* + * 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 "device_icon_info_filter_options.h" + +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_log.h" +#include "ipc_utils.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DeviceIconInfoFilterOptions"; +} + +std::vector DistributedDeviceProfile::GetProductIds() const +{ + return productIds_; +} + +void DistributedDeviceProfile::SetProductIds(const std::vector& productIds) +{ + specName_ = specName; +} + +std::string DistributedDeviceProfile::GetSubProductId() const +{ + return subProductId_; +} + +void DistributedDeviceProfile::SetSubProductId(const std::string& subProductId) +{ + subProductId_ = subProductId; +} + +std::string DistributedDeviceProfile::GetImageType() const +{ + return imageType_; +} + +void DistributedDeviceProfile::SetImageType(const std::string& imageType) +{ + imageType_ = imageType; +} + +std::string DistributedDeviceProfile::GetSpecName() const +{ + return specName_; +} + +void DistributedDeviceProfile::SetSpecName(const std::string& specName) +{ + specName_ = specName; +} + +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const +{ + IpcUtils::Marshalling(parcel, productIds_); + WRITE_HELPER_RET(parcel, String, subProductId_, false); + WRITE_HELPER_RET(parcel, String, imageType_, false); + WRITE_HELPER_RET(parcel, String, specName_, false); + return true; +} + +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) +{ + IpcUtils::UnMarshalling(parcel, productIds_); + READ_HELPER_RET(parcel, String, subProductId_, false); + READ_HELPER_RET(parcel, String, imageType_, false); + READ_HELPER_RET(parcel, String, specName_, false); + return true; +} + +std::string DistributedDeviceProfile::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + HILOGE("cJSON CreateObject failed!"); + return EMPTY_STRING; + } + cJSON* prodIdsJson = cJSON_CreateArray(); + if (prodIdsJson == NULL) { + HILOGE("cJSON CreateArray failed!"); + return EMPTY_STRING; + } + for (const auto& value : productIds_) { + cJSON* arrItem = cJSON_CreateString(value.c_str()); + if (arrItem == NULL) { + HILOGW("cJSON CreateString failed!"); + continue; + } + if (!cJSON_AddItemToArray(prodIdsJson, arrItem)) { + cJSON_Delete(arrItem); + HILOGW("Add item to array failed!"); + continue; + } + } + if (!cJSON_AddItemToObject(json, PRODUCT_IDS.c_str(), prodIdsJson)) { + cJSON_Delete(prodIdsJson); + HILOGE("Add json array to Object failed!"); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + 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 diff --git a/common/src/interfaces/device_profile.cpp b/common/src/interfaces/device_profile.cpp index 0af044ee..1b8c571c 100644 --- a/common/src/interfaces/device_profile.cpp +++ b/common/src/interfaces/device_profile.cpp @@ -408,8 +408,8 @@ bool DeviceProfile::UnMarshalling(MessageParcel& parcel) bool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const { - bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || - deviceName_ != deviceProfile.GetDeviceName() || manufactureName_ != deviceProfile.GetManufactureName() || + bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || + deviceName_ != deviceProfile.GetDeviceName() || manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() || storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() || osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || osType_ != deviceProfile.GetOsType() || diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp index 82eaf476..04d8f1e3 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -14,18 +14,15 @@ */ #include "product_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "ProductInfo"; - const std::string MODEL = "model"; - const std::string PRODUCT_ID = "productId"; - const std::string PRODUCT_NAME = "productName"; - const std::string PRODUCT_SHORT_NAME = "productShortName"; - const std::string IMAGE_VERSION = "imageVersion"; } std::string DeviceIconInfo::GetProuctId() const @@ -33,53 +30,53 @@ std::string DeviceIconInfo::GetProuctId() const return productId_; } -void DeviceIconInfo::SetProuctId(const std::string& productId) +void DistributedDeviceProfile::SetProuctId(const std::string& productId) { this->productId_ = productId; } -std::string DeviceIconInfo::GetModel() const +std::string DistributedDeviceProfile::GetModel() const { return model_; } -void DeviceIconInfo::SetModel(const std::string& model) +void DistributedDeviceProfile::SetModel(const std::string& model) { this->model_ = model; } -std::string DeviceIconInfo::GetProductName() const +std::string DistributedDeviceProfile::GetProductName() const { return productName_; } -void DeviceIconInfo::SetProductName(const std::string& productName) +void DistributedDeviceProfile::SetProductName(const std::string& productName) { this->productName_ = productName; } -std::string DeviceIconInfo::GetProductShortName() const +std::string DistributedDeviceProfile::GetProductShortName() const { return productShortName_; } -void DeviceIconInfo::SetProductShortName(const std::string& productShortName) +void DistributedDeviceProfile::SetProductShortName(const std::string& productShortName) { this->productShortName_ = productShortName; } -int32_t DeviceIconInfo::GetImageVersion() const +int32_t DistributedDeviceProfile::GetImageVersion() const { return imageVersion_; } -void DeviceIconInfo::SetImageVersion(const int32_t imageVersion) +void DistributedDeviceProfile::SetImageVersion(const int32_t imageVersion) { this->ImageVersion_ = imageVersion; } -bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, productId_, false); WRITE_HELPER_RET(parcel, String, model_, false); @@ -89,7 +86,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const return true; } -bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, String, productId_, false); READ_HELPER_RET(parcel, String, model_, false); @@ -99,7 +96,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) return true; } -bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const +bool DistributedDeviceProfile::operator!=(const DistributedDeviceProfile& other) const { bool isNotEqual = (productId_ != other.GetProuctId() || model_ != other.GetModel() || productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() || @@ -111,14 +108,14 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); - cJSON_AddStringToObject(json, MODEL.c_str(), model_); + cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_); cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_); cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_); cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index b51fc826..8b937463 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -115,6 +115,20 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& pa return true; } +bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) +{ + if (params.empty() || params.size() > MAX_ID_SIZE) { + HILOGE("params size is invalid!size : %{public}zu", params.size()); + return false; + } + size_t size = params.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + for (const auto& item : params) { + WRITE_HELPER_RET(parcel, Uint8, item, false); + } + return true; +} + bool IpcUtils::Marshalling(MessageParcel& parcel, const std::map& params) { if (params.size() == 0 || params.size() > MAX_PARAM_SIZE) { diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 81b60a24..746d9b26 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -91,7 +91,9 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", + "src/profiledatamanager/device_icon_info_dao.cpp", "src/profiledatamanager/device_profile_dao.cpp", + "src/profiledatamanager/product_info_dao.cpp", "src/publishcommonevent/dp_account_common_event.cpp", "src/staticcapabilitycollector/static_capability_collector.cpp", "src/staticcapabilityloader/static_capability_loader.cpp", diff --git a/services/core/include/profiledatamanager/device_icon_info_dao.h b/services/core/include/profiledatamanager/device_icon_info_dao.h new file mode 100644 index 00000000..97e4371c --- /dev/null +++ b/services/core/include/profiledatamanager/device_icon_info_dao.h @@ -0,0 +1,56 @@ +/* + * 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_DEVICE_ICON_INFO_DAO_H +#define OHOS_DP_DEVICE_ICON_INFO_DAO_H + +#include +#include +#include +#include + +#include "device_icon_info.h" +#include "device_icon_info_filter_options.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class DeviceIconInfoDao { + DECLARE_SINGLE_INSTANCE(DeviceIconInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutDeviceIconInfo(DeviceIconInfo& deviceIconInfo); + int32_t GetDeviceIconInfos(const std::vector& productIds, + std::vector& deviceIconInfos); + int32_t DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values); + int32_t ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_DAO_H \ No newline at end of file diff --git a/services/core/include/profiledatamanager/product_info_dao.h b/services/core/include/profiledatamanager/product_info_dao.h new file mode 100644 index 00000000..ceddac7b --- /dev/null +++ b/services/core/include/profiledatamanager/product_info_dao.h @@ -0,0 +1,55 @@ +/* + * 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_PRODUCT_INFO_DAO_H +#define OHOS_DP_PRODUCT_INFO_DAO_H + +#include +#include +#include +#include + +#include "product_info.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class ProductInfoDao { + DECLARE_SINGLE_INSTANCE(ProductInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutProductInfo(ProductInfo& productInfo); + int32_t GetProductInfos(const std::vector& productIds, + std::vector& productInfos); + int32_t DeleteProductInfo(const ProductInfo& productInfo); + int32_t UpdateProductInfo(const ProductInfo& productInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const ProductInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t ProductInfoToEntries(const ProductInfo& ProductInfo, ValuesBucket& values); + int32_t ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_PRODUCT_INFO_DAO_H \ No newline at end of file diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index dfa4bbee..4563d3a4 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -59,5 +59,23 @@ const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = deviceId,\ userId,\ accountId);"; +// DeviceIconInfoDao +const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_icon_info\ +(id INTEGER PRIMARY KEY AUTOINCREMENT,\ +productId TEXT,\ +subProductId TEXT,\ +imageType TEXT,\ +specName TEXT,\ +icon blob);"; +const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL = "CREATE UNIQUE INDEX if not exists \ +unique_device_icon_info ON device_icon_info (productId,subProductId,imageType,specName);"; +const std::string SELECT_DEVICE_ICON_INFO_TABLE = "SELECT * FROM device_profile WHERE "; +// ProductInfoDao +const std::string CREATE_PRODUCT_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS product_info \ +(productId TEXT PRIMARY KEY,\ +model TEXT,\ +productName TEXT,\ +productShortName TEXT,\ +imageVersion INTEGER);"; } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp new file mode 100644 index 00000000..df3a282b --- /dev/null +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -0,0 +1,240 @@ +/* + * 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 "device_icon_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(DeviceIconInfoDao); +namespace { + const std::string TAG = "DeviceIconInfoDao"; +} + +int32_t DeviceIconInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, DEVICE_ICON_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_PUT_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::GetDeviceIconInfos(const DeviceIconInfoOptions& filterOptions, + std::vector& deviceIconInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(filterOptions, sql, condition)) { + HILOGE("invalid params:%{public}s", filterOptions.dump().c_str()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + DeviceIconInfo deviceIconInfo; + ConvertToDeviceIconInfo(resultSet, deviceIconInfo); + deviceIconInfos.emplace_back(deviceIconInfo); + } + resultSet->Close(); + if (deviceIconInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, DEVICE_ICON_INFO_TABLE, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_DEL_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, DEVICE_ICON_INFO_TABLE, values, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_UPDATE_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateIndex() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s unique index create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + +bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition) +{ + sql = SELECT_DEVICE_ICON_INFO_TABLE; + bool flag = false; + if (!filterOptions.GetProductIds().empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : filterOptions.GetProductIds()) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (!filterOptions.GetSubProductId().empty()) { + sql += SUB_PRODUCT_ID + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + flag = true; + } + if (!filterOptions.GetImageType().empty()) { + sql += IMAGE_TYPE + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (!filterOptions.GetSpecName().empty()) { + sql += SPEC_NAME + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (flag) { + sql.erase(sql.end() - 4); + return flag; + } + return flag; +} + +int32_t DeviceIconInfoDao::DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, deviceIconInfo.GetProductId()); + values.PutString(SUB_PRODUCT_ID, deviceIconInfo.GetSubProductId()); + values.PutString(IMAGE_TYPE, deviceIconInfo.GetImageType()); + values.PutString(SPEC_NAME, deviceIconInfo.GetSpecName()); + values.PutBlob(ICON, deviceIconInfo.GetIcon()); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo) +{ + 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; + } + deviceIconInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + deviceIconInfo.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID)); + deviceIconInfo.SetImageType(rowEntity.Get(IMAGE_TYPE)); + deviceIconInfo.SetSpecName(rowEntity.Get(SPEC_NAME)); + // std::vector icon; + // rowEntity.Get(ICON).GetBlob(icon); + // deviceIconInfo.SetIcon(icon); + deviceIconInfo.SetIcon(rowEntity.Get(ICON)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp new file mode 100644 index 00000000..e03d2cad --- /dev/null +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -0,0 +1,212 @@ +/* + * 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 "product_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProductInfoDao); +namespace { + const std::string TAG = "ProductInfoDao"; +} + +int32_t ProductInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::PutProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, PRODUCT_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", PRODUCT_INFO_TABLE.c_str()); + return DP_PUT_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::GetProductInfos(const std::vector& productIds, + std::vector& productInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(productIds, sql, condition)) { + HILOGE("invalid params: productIds.size=%{public}zu", productIds.size()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + ProductInfo productInfo; + ConvertToProductInfo(resultSet, productInfo); + productInfos.emplace_back(productInfo); + } + resultSet->Close(); + if (productInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::DeleteProductInfo(const ProductInfo& productInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, PRODUCT_INFO_TABLE, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", PRODUCT_INFO_TABLE.c_str()); + return DP_DEL_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::UpdateProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, PRODUCT_INFO_TABLE, values, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", PRODUCT_INFO_TABLE.c_str()); + return DP_UPDATE_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_PRODUCT_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateIndex() +{ + return DP_SUCCESS; +} + +bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& productIds, + std::string& sql, std::vector& condition) +{ + sql = SELECT_PRODUCT_INFO_TABLE; + bool flag = false; + if (!productIds.empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : productIds) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ")"; + flag = true; + } + return flag; +} + +int32_t ProductInfoDao::ProductInfoToEntries(const ProductInfo& productInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, productInfo.GetProductId()); + values.PutString(DEVICE_MODEL, productInfo.GetModel()); + values.PutString(PRODUCT_NAME, productInfo.GetProductName()); + values.PutString(PRODUCT_SHORT_NAME, productInfo.GetProductShortName()); + values.PutInt(IMAGE_VERSION, productInfo.GetImageVersion()); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo) +{ + 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; + } + productInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + productInfo.SetModel(rowEntity.Get(DEVICE_MODEL)); + productInfo.SetProductName(rowEntity.Get(PRODUCT_NAME)); + productInfo.SetProductShortName(rowEntity.Get(PRODUCT_SHORT_NAME)); + productInfo.SetImageVersion(rowEntity.Get(IMAGE_VERSION)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file -- Gitee From 5d1978e31214f209797230b6cab5fb2aab5b3a22 Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 17:16:08 +0800 Subject: [PATCH 11/55] =?UTF-8?q?=E6=96=B0=E5=A2=9EProductInfoDao=E3=80=81?= =?UTF-8?q?DeviceIconInfoDao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/BUILD.gn | 3 + .../distributed_device_profile_constants.h | 11 + .../distributed_device_profile_errors.h | 7 + common/include/interfaces/device_icon_info.h | 7 +- .../device_icon_info_filter_options.h | 53 ++++ common/include/interfaces/product_info.h | 8 +- common/include/utils/ipc_utils.h | 1 + .../distributed_device_profile_constants.cpp | 13 +- common/src/interfaces/device_icon_info.cpp | 16 +- .../device_icon_info_filter_options.cpp | 132 ++++++++++ common/src/interfaces/device_profile.cpp | 4 +- common/src/interfaces/product_info.cpp | 37 ++- common/src/utils/ipc_utils.cpp | 14 + services/core/BUILD.gn | 2 + .../profiledatamanager/device_icon_info_dao.h | 56 ++++ .../profiledatamanager/product_info_dao.h | 55 ++++ .../core/src/common/dp_services_constants.cpp | 18 ++ .../device_icon_info_dao.cpp | 240 ++++++++++++++++++ .../profiledatamanager/product_info_dao.cpp | 212 ++++++++++++++++ 19 files changed, 848 insertions(+), 41 deletions(-) create mode 100644 common/include/interfaces/device_icon_info_filter_options.h create mode 100644 common/src/interfaces/device_icon_info_filter_options.cpp create mode 100644 services/core/include/profiledatamanager/device_icon_info_dao.h create mode 100644 services/core/include/profiledatamanager/product_info_dao.h create mode 100644 services/core/src/profiledatamanager/device_icon_info_dao.cpp create mode 100644 services/core/src/profiledatamanager/product_info_dao.cpp diff --git a/common/BUILD.gn b/common/BUILD.gn index 5cb74819..dcf9f2a3 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -50,6 +50,8 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accessee.cpp", "src/interfaces/accesser.cpp", "src/interfaces/characteristic_profile.cpp", + "src/interfaces/device_icon_info_filter_options.cpp", + "src/interfaces/device_icon_info.cpp", "src/interfaces/device_profile.cpp", "src/interfaces/device_profile_filter_options.cpp", "src/interfaces/dp_inited_callback_proxy.cpp", @@ -57,6 +59,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/dp_subscribe_info.cpp", "src/interfaces/dp_sync_options.cpp", "src/interfaces/i_profile_change_listener.cpp", + "src/interfaces/product_info.cpp", "src/interfaces/profile_change_listener_proxy.cpp", "src/interfaces/profile_change_listener_stub.cpp", "src/interfaces/service_profile.cpp", diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index f67e082d..f19b2ad2 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -63,12 +63,23 @@ extern const std::string MODIFY_TIME; extern const std::string SHARE_TIME; extern const std::string PRODUCTOR_INFO_VERSION; extern const std::string DEVICE_PROFILE_TABLE; +extern const std::string DEVICE_ICON_INFO_TABLE; +extern const std::string PRODUCT_INFO_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; +extern const std::string SERVICE_ID; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; +/* ProductInfo Attribute */ +extern const std::string PRODUCT_NAME; +extern const std::string PRODUCT_SHORT_NAME; +extern const std::string IMAGE_VERSION; +/* DeviceIconInfo Attribute */ +extern const std::string IMAGE_TYPE; +extern const std::string SPEC_NAME; +extern const std::string ICON; /* TrustDeviceProfile Attribute */ extern const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE; extern const std::string DEVICE_ID_TYPE; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 86fa4d08..8aad772f 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -160,6 +160,13 @@ 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_DEVICE_ICON_INFO_FAIL = 98566284; +constexpr int32_t DP_DEL_DEVICE_ICON_INFO_FAIL = 98566286; +constexpr int32_t DP_UPDATE_DEVICE_ICON_INFO_FAIL = 98566287; +constexpr int32_t DP_PUT_PRODUCT_INFO_FAIL = 98566288; +constexpr int32_t DP_DEL_PRODUCT_INFO_FAIL = 98566280; +constexpr int32_t DP_UPDATE_PRODUCT_INFO_FAIL = 98566290; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h index 4d0a98d6..35ae2f84 100644 --- a/common/include/interfaces/device_icon_info.h +++ b/common/include/interfaces/device_icon_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_DEVICE_ICON_INFO_H #define OHOS_DP_DEVICE_ICON_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -28,10 +28,9 @@ public: DeviceIconInfo() : id_(0), productId_(""), - subProductId_(nullptr), + subProductId_(""), imageType(""), - specName(""), - icon_(nullptr), + specName("") {} ~DeviceIconInfo() = default; int32_t GetId() const; diff --git a/common/include/interfaces/device_icon_info_filter_options.h b/common/include/interfaces/device_icon_info_filter_options.h new file mode 100644 index 00000000..fc4f5e40 --- /dev/null +++ b/common/include/interfaces/device_icon_info_filter_options.h @@ -0,0 +1,53 @@ + /* + * 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_DEVICE_ICON_INFO_FILTER_OPTIONS_H +#define OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H + +#include +#include + +#include "dp_parcel.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceIconInfoFilterOptions : public DpParcel { +public: + DeviceIconInfoFilterOptions() :subProductId_(""), imageType_(""),specName_("") + {} + ~DeviceIconInfoFilterOptions() = default; + + std::vector GetProductIds() const; + void SetProductIds(const std::vector& productIds); + std::string GetSubProductId() const; + void SetSubProductId(const std::string& subProductId); + std::string GetImageType() const; + void SetImageType(const std::string& imageType); + std::string GetSpecName() const; + void SetSpecName(const std::string& specName); + + bool Marshalling(MessageParcel& parcel) const override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; + +private: + std::vector productIds_; + std::string subProductId_; + std::string imageType_; + std::string specName_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_FILTER_OPTIONS_H diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h index c1eb94fe..265f017a 100644 --- a/common/include/interfaces/product_info.h +++ b/common/include/interfaces/product_info.h @@ -16,9 +16,9 @@ #ifndef OHOS_DP_PRODUCT_INFO_H #define OHOS_DP_PRODUCT_INFO_H -#include #include #include + #include "dp_parcel.h" namespace OHOS { @@ -26,10 +26,10 @@ namespace DistributedDeviceProfile { class ProductInfo : public DpParcel { public: ProductInfo() : productId_(""), - model_(nullptr), + model_(""), productName_(""), productShortName_(""), - imageVersion_(nullptr), + imageVersion_(""), {} ~ProductInfo() = default; @@ -45,7 +45,7 @@ public: void SetImageVersion(const int32_t specName); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; - bool operator!=(const DeviceIconInfo& other) const; + bool operator!=(const ProductInfo& other) const; private: std::string productId_; diff --git a/common/include/utils/ipc_utils.h b/common/include/utils/ipc_utils.h index 02ee50c8..0222f385 100644 --- a/common/include/utils/ipc_utils.h +++ b/common/include/utils/ipc_utils.h @@ -46,6 +46,7 @@ public: static bool Marshalling(MessageParcel& parcel, const std::vector& charProfiles); static bool Marshalling(MessageParcel& parcel, const std::vector& strings); static bool Marshalling(MessageParcel& parcel, const std::vector& params); + static bool Marshalling(MessageParcel& parcel, const std::vector& params); static bool Marshalling(MessageParcel& parcel, const std::map& params); static bool Marshalling(MessageParcel& parcel, const std::map& listenerMap); diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index ae381843..3b1c13a7 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -26,7 +26,7 @@ const std::string DEVICE_TYPE_ID = "deviceTypeId"; const std::string DEVICE_TYPE_NAME = "deviceTypeName"; const std::string DEVICE_NAME = "deviceIdName"; const std::string MANUFACTURE_NAME = "manufactureName"; -const std::string DEVICE_MODEL = "deviceModel"; +const std::string DEVICE_MODEL = "model"; const std::string STORAGE_CAPACITY = "storageCapacity"; const std::string OS_SYS_CAPACITY = "osSysCapacity"; const std::string OS_API_LEVEL = "osApiLevel"; @@ -53,12 +53,23 @@ const std::string MODIFY_TIME = "modifyTime"; const std::string SHARE_TIME = "shareTime"; const std::string PRODUCTOR_INFO_VERSION = "productorInfoVersion"; const std::string DEVICE_PROFILE_TABLE = "device_profile"; +const std::string DEVICE_ICON_INFO_TABLE = "device_icon_info"; +const std::string PRODUCT_INFO_TABLE = "product_info"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; +const std::string SERVICE_ID = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; +/* ProductInfo Attribute */ +const std::string PRODUCT_NAME = "productName"; +const std::string PRODUCT_SHORT_NAME = "productShortName"; +const std::string IMAGE_VERSION = "imageVersion"; +/* DeviceIconInfo Attribute */ +const std::string IMAGE_TYPE = "imageType"; +const std::string SPEC_NAME = "specName"; +const std::string ICON = "icon"; /* TrustDeviceProfile Attribute */ const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE = "trust_device_profile"; const std::string DEVICE_ID_TYPE = "deviceIdType"; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 09204b9f..2857a7c7 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -14,18 +14,15 @@ */ #include "device_icon_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "DeviceIconInfo"; - const std::string PRODUCT_INFO_ID = "id"; - const std::string PRODUCT_ID = "productId"; - const std::string SUB_PRODUCT_ID = "subProductId"; - const std::string IMAGE_TYPE = "imageType"; - const std::string SPEC_NAME = "specName"; } int32_t DeviceIconInfo::GetId() const @@ -88,7 +85,6 @@ void DeviceIconInfo::SetIcon(const std::vector& icon) this->icon_ = icon; } - bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, Int32, id_, false); @@ -96,7 +92,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); - WRITE_HELPER_RET(parcel, Int64, icon_, false); + WRITE_HELPER_RET(parcel, UInt8Vector, icon_); return true; } @@ -107,7 +103,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); - READ_HELPER_RET(parcel, Int64, icon_, false); + READ_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -123,13 +119,13 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string DistributedDeviceProfile::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } - cJSON_AddNumberToObject(json, PRODUCT_INFO_ID.c_str(), id_); + cJSON_AddNumberToObject(json, ID.c_str(), id_); cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); diff --git a/common/src/interfaces/device_icon_info_filter_options.cpp b/common/src/interfaces/device_icon_info_filter_options.cpp new file mode 100644 index 00000000..a6cf7233 --- /dev/null +++ b/common/src/interfaces/device_icon_info_filter_options.cpp @@ -0,0 +1,132 @@ +/* + * 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 "device_icon_info_filter_options.h" + +#include "cJSON.h" +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_log.h" +#include "ipc_utils.h" +#include "macro_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DeviceIconInfoFilterOptions"; +} + +std::vector DistributedDeviceProfile::GetProductIds() const +{ + return productIds_; +} + +void DistributedDeviceProfile::SetProductIds(const std::vector& productIds) +{ + specName_ = specName; +} + +std::string DistributedDeviceProfile::GetSubProductId() const +{ + return subProductId_; +} + +void DistributedDeviceProfile::SetSubProductId(const std::string& subProductId) +{ + subProductId_ = subProductId; +} + +std::string DistributedDeviceProfile::GetImageType() const +{ + return imageType_; +} + +void DistributedDeviceProfile::SetImageType(const std::string& imageType) +{ + imageType_ = imageType; +} + +std::string DistributedDeviceProfile::GetSpecName() const +{ + return specName_; +} + +void DistributedDeviceProfile::SetSpecName(const std::string& specName) +{ + specName_ = specName; +} + +bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const +{ + IpcUtils::Marshalling(parcel, productIds_); + WRITE_HELPER_RET(parcel, String, subProductId_, false); + WRITE_HELPER_RET(parcel, String, imageType_, false); + WRITE_HELPER_RET(parcel, String, specName_, false); + return true; +} + +bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) +{ + IpcUtils::UnMarshalling(parcel, productIds_); + READ_HELPER_RET(parcel, String, subProductId_, false); + READ_HELPER_RET(parcel, String, imageType_, false); + READ_HELPER_RET(parcel, String, specName_, false); + return true; +} + +std::string DistributedDeviceProfile::dump() const +{ + cJSON* json = cJSON_CreateObject(); + if (json == NULL) { + HILOGE("cJSON CreateObject failed!"); + return EMPTY_STRING; + } + cJSON* prodIdsJson = cJSON_CreateArray(); + if (prodIdsJson == NULL) { + HILOGE("cJSON CreateArray failed!"); + return EMPTY_STRING; + } + for (const auto& value : productIds_) { + cJSON* arrItem = cJSON_CreateString(value.c_str()); + if (arrItem == NULL) { + HILOGW("cJSON CreateString failed!"); + continue; + } + if (!cJSON_AddItemToArray(prodIdsJson, arrItem)) { + cJSON_Delete(arrItem); + HILOGW("Add item to array failed!"); + continue; + } + } + if (!cJSON_AddItemToObject(json, PRODUCT_IDS.c_str(), prodIdsJson)) { + cJSON_Delete(prodIdsJson); + HILOGE("Add json array to Object failed!"); + return EMPTY_STRING; + } + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + 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 diff --git a/common/src/interfaces/device_profile.cpp b/common/src/interfaces/device_profile.cpp index 0af044ee..1b8c571c 100644 --- a/common/src/interfaces/device_profile.cpp +++ b/common/src/interfaces/device_profile.cpp @@ -408,8 +408,8 @@ bool DeviceProfile::UnMarshalling(MessageParcel& parcel) bool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const { - bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || - deviceName_ != deviceProfile.GetDeviceName() || manufactureName_ != deviceProfile.GetManufactureName() || + bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || + deviceName_ != deviceProfile.GetDeviceName() || manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() || storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() || osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || osType_ != deviceProfile.GetOsType() || diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp index 82eaf476..b42b5d67 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -14,72 +14,69 @@ */ #include "product_info.h" + #include "cJSON.h" +#include "distributed_device_profile_constants.h" #include "macro_utils.h" namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "ProductInfo"; - const std::string MODEL = "model"; - const std::string PRODUCT_ID = "productId"; - const std::string PRODUCT_NAME = "productName"; - const std::string PRODUCT_SHORT_NAME = "productShortName"; - const std::string IMAGE_VERSION = "imageVersion"; } -std::string DeviceIconInfo::GetProuctId() const +std::string ProductInfo::GetProuctId() const { return productId_; } -void DeviceIconInfo::SetProuctId(const std::string& productId) +void ProductInfo::SetProuctId(const std::string& productId) { this->productId_ = productId; } -std::string DeviceIconInfo::GetModel() const +std::string ProductInfo::GetModel() const { return model_; } -void DeviceIconInfo::SetModel(const std::string& model) +void ProductInfo::SetModel(const std::string& model) { this->model_ = model; } -std::string DeviceIconInfo::GetProductName() const +std::string ProductInfo::GetProductName() const { return productName_; } -void DeviceIconInfo::SetProductName(const std::string& productName) +void ProductInfo::SetProductName(const std::string& productName) { this->productName_ = productName; } -std::string DeviceIconInfo::GetProductShortName() const +std::string ProductInfo::GetProductShortName() const { return productShortName_; } -void DeviceIconInfo::SetProductShortName(const std::string& productShortName) +void ProductInfo::SetProductShortName(const std::string& productShortName) { this->productShortName_ = productShortName; } -int32_t DeviceIconInfo::GetImageVersion() const +int32_t ProductInfo::GetImageVersion() const { return imageVersion_; } -void DeviceIconInfo::SetImageVersion(const int32_t imageVersion) +void ProductInfo::SetImageVersion(const int32_t imageVersion) { this->ImageVersion_ = imageVersion; } -bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const +bool ProductInfo::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, productId_, false); WRITE_HELPER_RET(parcel, String, model_, false); @@ -89,7 +86,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const return true; } -bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) +bool ProductInfo::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, String, productId_, false); READ_HELPER_RET(parcel, String, model_, false); @@ -99,7 +96,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) return true; } -bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const +bool ProductInfo::operator!=(const ProductInfo& other) const { bool isNotEqual = (productId_ != other.GetProuctId() || model_ != other.GetModel() || productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() || @@ -111,14 +108,14 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DeviceProfile::dump() const +std::string ProductInfo::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); - cJSON_AddStringToObject(json, MODEL.c_str(), model_); + cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_); cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_); cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_); cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index b51fc826..8b937463 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -115,6 +115,20 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& pa return true; } +bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) +{ + if (params.empty() || params.size() > MAX_ID_SIZE) { + HILOGE("params size is invalid!size : %{public}zu", params.size()); + return false; + } + size_t size = params.size(); + WRITE_HELPER_RET(parcel, Uint32, size, false); + for (const auto& item : params) { + WRITE_HELPER_RET(parcel, Uint8, item, false); + } + return true; +} + bool IpcUtils::Marshalling(MessageParcel& parcel, const std::map& params) { if (params.size() == 0 || params.size() > MAX_PARAM_SIZE) { diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 81b60a24..746d9b26 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -91,7 +91,9 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", + "src/profiledatamanager/device_icon_info_dao.cpp", "src/profiledatamanager/device_profile_dao.cpp", + "src/profiledatamanager/product_info_dao.cpp", "src/publishcommonevent/dp_account_common_event.cpp", "src/staticcapabilitycollector/static_capability_collector.cpp", "src/staticcapabilityloader/static_capability_loader.cpp", diff --git a/services/core/include/profiledatamanager/device_icon_info_dao.h b/services/core/include/profiledatamanager/device_icon_info_dao.h new file mode 100644 index 00000000..97e4371c --- /dev/null +++ b/services/core/include/profiledatamanager/device_icon_info_dao.h @@ -0,0 +1,56 @@ +/* + * 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_DEVICE_ICON_INFO_DAO_H +#define OHOS_DP_DEVICE_ICON_INFO_DAO_H + +#include +#include +#include +#include + +#include "device_icon_info.h" +#include "device_icon_info_filter_options.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class DeviceIconInfoDao { + DECLARE_SINGLE_INSTANCE(DeviceIconInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutDeviceIconInfo(DeviceIconInfo& deviceIconInfo); + int32_t GetDeviceIconInfos(const std::vector& productIds, + std::vector& deviceIconInfos); + int32_t DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values); + int32_t ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_DEVICE_ICON_INFO_DAO_H \ No newline at end of file diff --git a/services/core/include/profiledatamanager/product_info_dao.h b/services/core/include/profiledatamanager/product_info_dao.h new file mode 100644 index 00000000..ceddac7b --- /dev/null +++ b/services/core/include/profiledatamanager/product_info_dao.h @@ -0,0 +1,55 @@ +/* + * 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_PRODUCT_INFO_DAO_H +#define OHOS_DP_PRODUCT_INFO_DAO_H + +#include +#include +#include +#include + +#include "product_info.h" +#include "profile_data_rdb_adapter.h" +#include "single_instance.h" +#include "values_bucket.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace OHOS::NativeRdb; + +class ProductInfoDao { + DECLARE_SINGLE_INSTANCE(ProductInfoDao); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t PutProductInfo(ProductInfo& productInfo); + int32_t GetProductInfos(const std::vector& productIds, + std::vector& productInfos); + int32_t DeleteProductInfo(const ProductInfo& productInfo); + int32_t UpdateProductInfo(const ProductInfo& productInfo); + int32_t CreateTable(); + int32_t CreateIndex(); + void CreateQuerySqlAndCondition(const ProductInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition); + int32_t ProductInfoToEntries(const ProductInfo& ProductInfo, ValuesBucket& values); + int32_t ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo); +private: + std::mutex rdbMutex_; +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_PRODUCT_INFO_DAO_H \ No newline at end of file diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index dfa4bbee..4563d3a4 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -59,5 +59,23 @@ const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = deviceId,\ userId,\ accountId);"; +// DeviceIconInfoDao +const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_icon_info\ +(id INTEGER PRIMARY KEY AUTOINCREMENT,\ +productId TEXT,\ +subProductId TEXT,\ +imageType TEXT,\ +specName TEXT,\ +icon blob);"; +const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL = "CREATE UNIQUE INDEX if not exists \ +unique_device_icon_info ON device_icon_info (productId,subProductId,imageType,specName);"; +const std::string SELECT_DEVICE_ICON_INFO_TABLE = "SELECT * FROM device_profile WHERE "; +// ProductInfoDao +const std::string CREATE_PRODUCT_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS product_info \ +(productId TEXT PRIMARY KEY,\ +model TEXT,\ +productName TEXT,\ +productShortName TEXT,\ +imageVersion INTEGER);"; } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp new file mode 100644 index 00000000..df3a282b --- /dev/null +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -0,0 +1,240 @@ +/* + * 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 "device_icon_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(DeviceIconInfoDao); +namespace { + const std::string TAG = "DeviceIconInfoDao"; +} + +int32_t DeviceIconInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 DeviceIconInfoDao::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, DEVICE_ICON_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_PUT_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::GetDeviceIconInfos(const DeviceIconInfoOptions& filterOptions, + std::vector& deviceIconInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(filterOptions, sql, condition)) { + HILOGE("invalid params:%{public}s", filterOptions.dump().c_str()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + DeviceIconInfo deviceIconInfo; + ConvertToDeviceIconInfo(resultSet, deviceIconInfo); + deviceIconInfos.emplace_back(deviceIconInfo); + } + resultSet->Close(); + if (deviceIconInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, DEVICE_ICON_INFO_TABLE, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_DEL_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + ValuesBucket values; + DeviceIconInfoToEntries(deviceIconInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, DEVICE_ICON_INFO_TABLE, values, + ID_EQUAL_CONDITION, std::vector{ ValueObject(deviceIconInfo.GetId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", DEVICE_ICON_INFO_TABLE.c_str()); + return DP_UPDATE_DEVICE_ICON_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::CreateIndex() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s unique index create failed", DEVICE_ICON_INFO_TABLE); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + return DP_SUCCESS; +} + +bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + std::string& sql, std::vector& condition) +{ + sql = SELECT_DEVICE_ICON_INFO_TABLE; + bool flag = false; + if (!filterOptions.GetProductIds().empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : filterOptions.GetProductIds()) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + flag = true; + } + if (!filterOptions.GetSubProductId().empty()) { + sql += SUB_PRODUCT_ID + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + flag = true; + } + if (!filterOptions.GetImageType().empty()) { + sql += IMAGE_TYPE + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (!filterOptions.GetSpecName().empty()) { + sql += SPEC_NAME + " = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + flag = true; + } + if (flag) { + sql.erase(sql.end() - 4); + return flag; + } + return flag; +} + +int32_t DeviceIconInfoDao::DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, deviceIconInfo.GetProductId()); + values.PutString(SUB_PRODUCT_ID, deviceIconInfo.GetSubProductId()); + values.PutString(IMAGE_TYPE, deviceIconInfo.GetImageType()); + values.PutString(SPEC_NAME, deviceIconInfo.GetSpecName()); + values.PutBlob(ICON, deviceIconInfo.GetIcon()); + return DP_SUCCESS; +} + +int32_t DeviceIconInfoDao::ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo) +{ + 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; + } + deviceIconInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + deviceIconInfo.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID)); + deviceIconInfo.SetImageType(rowEntity.Get(IMAGE_TYPE)); + deviceIconInfo.SetSpecName(rowEntity.Get(SPEC_NAME)); + // std::vector icon; + // rowEntity.Get(ICON).GetBlob(icon); + // deviceIconInfo.SetIcon(icon); + deviceIconInfo.SetIcon(rowEntity.Get(ICON)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp new file mode 100644 index 00000000..e03d2cad --- /dev/null +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -0,0 +1,212 @@ +/* + * 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 "product_info_dao.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_data_rdb_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProductInfoDao); +namespace { + const std::string TAG = "ProductInfoDao"; +} + +int32_t ProductInfoDao::Init() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::UnInit() +{ + std::lock_guard lock(rdbMutex_); + 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 ProductInfoDao::PutProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int64_t rowId = ROWID_INIT; + int32_t ret = RET_INIT; + { + std::lock_guard lock(rdbMutex_); + ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, PRODUCT_INFO_TABLE, values); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s insert failed", PRODUCT_INFO_TABLE.c_str()); + return DP_PUT_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::GetProductInfos(const std::vector& productIds, + std::vector& productInfos) +{ + std::string sql; + std::vector condition; + if (!CreateQuerySqlAndCondition(productIds, sql, condition)) { + HILOGE("invalid params: productIds.size=%{public}zu", productIds.size()); + return DP_INVALID_PARAMS; + } + std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition); + if (resultSet == nullptr) { + HILOGE("resultSet is nullptr"); + return DP_GET_RESULTSET_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) { + ProductInfo productInfo; + ConvertToProductInfo(resultSet, productInfo); + productInfos.emplace_back(productInfo); + } + resultSet->Close(); + if (productInfos.empty()) { + return DP_NOT_FIND_DATA; + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::DeleteProductInfo(const ProductInfo& productInfo) +{ + { + std::lock_guard lock(rdbMutex_); + int32_t deleteRows = DELETEROWS_INIT; + int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, PRODUCT_INFO_TABLE, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("delete %{public}s data failed", PRODUCT_INFO_TABLE.c_str()); + return DP_DEL_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::UpdateProductInfo(const ProductInfo& productInfo) +{ + ValuesBucket values; + ProductInfoToEntries(productInfo, values); + int32_t changeRowCnt = CHANGEROWCNT_INIT; + { + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, PRODUCT_INFO_TABLE, values, + PRODUCT_ID + " = ?", std::vector{ ValueObject(productInfo.GetProductId()) }); + if (ret != DP_SUCCESS) { + HILOGE("Update %{public}s table failed", PRODUCT_INFO_TABLE.c_str()); + return DP_UPDATE_PRODUCT_INFO_FAIL; + } + } + HILOGI("end!"); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateTable() +{ + std::lock_guard lock(rdbMutex_); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_PRODUCT_INFO_TABLE_SQL); + if (ret != DP_SUCCESS) { + HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE); + return DP_CREATE_TABLE_FAIL; + } + return DP_SUCCESS; +} + +int32_t ProductInfoDao::CreateIndex() +{ + return DP_SUCCESS; +} + +bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& productIds, + std::string& sql, std::vector& condition) +{ + sql = SELECT_PRODUCT_INFO_TABLE; + bool flag = false; + if (!productIds.empty()) { + sql += PRODUCT_ID + "IN("; + for (cosnt auto& prodId : productIds) { + sql += "?,"; + condition.emplace_back(ValueObject(prodId)); + } + sql.erase(sql.end() - 1); + sql += ")"; + flag = true; + } + return flag; +} + +int32_t ProductInfoDao::ProductInfoToEntries(const ProductInfo& productInfo, ValuesBucket& values) +{ + values.PutString(PRODUCT_ID, productInfo.GetProductId()); + values.PutString(DEVICE_MODEL, productInfo.GetModel()); + values.PutString(PRODUCT_NAME, productInfo.GetProductName()); + values.PutString(PRODUCT_SHORT_NAME, productInfo.GetProductShortName()); + values.PutInt(IMAGE_VERSION, productInfo.GetImageVersion()); + return DP_SUCCESS; +} + +int32_t ProductInfoDao::ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo) +{ + 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; + } + productInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); + productInfo.SetModel(rowEntity.Get(DEVICE_MODEL)); + productInfo.SetProductName(rowEntity.Get(PRODUCT_NAME)); + productInfo.SetProductShortName(rowEntity.Get(PRODUCT_SHORT_NAME)); + productInfo.SetImageVersion(rowEntity.Get(IMAGE_VERSION)); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file -- Gitee From d543838affd136cae78e69277adace7606874c4c Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 19:14:56 +0800 Subject: [PATCH 12/55] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- .../distributed_device_profile_constants.h | 2 +- common/include/interfaces/device_icon_info.h | 13 +++---- common/include/interfaces/product_info.h | 11 +++--- .../distributed_device_profile_constants.cpp | 2 +- common/src/interfaces/device_icon_info.cpp | 26 +++++++------- .../device_icon_info_filter_options.cpp | 34 +++++++++---------- common/src/interfaces/product_info.cpp | 16 ++++----- .../include/common/dp_services_constants.h | 7 ++++ .../profiledatamanager/device_icon_info_dao.h | 6 ++-- .../profiledatamanager/product_info_dao.h | 6 ++-- .../core/src/common/dp_services_constants.cpp | 1 + .../device_icon_info_dao.cpp | 15 ++++---- .../profiledatamanager/product_info_dao.cpp | 5 +-- 13 files changed, 78 insertions(+), 66 deletions(-) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index f19b2ad2..c826dc79 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -67,7 +67,7 @@ extern const std::string DEVICE_ICON_INFO_TABLE; extern const std::string PRODUCT_INFO_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; -extern const std::string SERVICE_ID; +// extern const std::string SERVICE_ID; extern const std::string SERVICE_TYPE; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h index 35ae2f84..4b60384b 100644 --- a/common/include/interfaces/device_icon_info.h +++ b/common/include/interfaces/device_icon_info.h @@ -29,16 +29,16 @@ public: : id_(0), productId_(""), subProductId_(""), - imageType(""), - specName("") + imageType_(""), + specName_("") {} ~DeviceIconInfo() = default; int32_t GetId() const; void SetId(const int32_t id); - std::string GetProuctId() const; - void SetProuctId(const std::string& productId); - std::string GetSubProuctId() const; - void SetSubProuctId(const std::string& subProductId); + std::string GetProductId() const; + void SetProductId(const std::string& productId); + std::string GetSubProductId() const; + void SetSubProductId(const std::string& subProductId); std::string GetImageType() const; void SetImageType(const std::string& imageType); std::string GetSpecName() const; @@ -48,6 +48,7 @@ public: bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const DeviceIconInfo& other) const; + std::string dump() const override; private: int32_t id_; diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h index 265f017a..c930fd4e 100644 --- a/common/include/interfaces/product_info.h +++ b/common/include/interfaces/product_info.h @@ -29,23 +29,24 @@ public: model_(""), productName_(""), productShortName_(""), - imageVersion_(""), + imageVersion_(0) {} ~ProductInfo() = default; - std::string GetProuctId() const; - void SetProuctId(const std::string& productId); + std::string GetProductId() const; + void SetProductId(const std::string& productId); std::string GetModel() const; - void SetModelGetModel(const std::string& model); + void SetModel(const std::string& model); std::string GetProductName() const; void SetProductName(const std::string& productName); std::string GetProductShortName() const; void SetProductShortName(const std::string& productShortName); int32_t GetImageVersion() const; - void SetImageVersion(const int32_t specName); + void SetImageVersion(const int32_t imageVersion); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const ProductInfo& other) const; + std::string dump() const override; private: std::string productId_; diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index 3b1c13a7..f76d3790 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -57,7 +57,7 @@ const std::string DEVICE_ICON_INFO_TABLE = "device_icon_info"; const std::string PRODUCT_INFO_TABLE = "product_info"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; -const std::string SERVICE_ID = "serviceId"; +// const std::string SERVICE_ID = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; /* CharacteristicProfile Attribute */ const std::string CHARACTERISTIC_KEY = "characteristicKey"; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 2857a7c7..731f18d4 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -35,22 +35,22 @@ void DeviceIconInfo::SetId(const int32_t id) this->id_ = id; } -std::string DeviceIconInfo::GetProuctId() const +std::string DeviceIconInfo::GetProductId() const { return productId_; } -void DeviceIconInfo::SetProuctId(const std::string& productId) +void DeviceIconInfo::SetProductId(const std::string& productId) { this->productId_ = productId; } -std::string DeviceIconInfo::GetSubProuctId() const +std::string DeviceIconInfo::GetSubProductId() const { return subProductId_; } -void DeviceIconInfo::SetSubProuctId(const std::string& subProductId) +void DeviceIconInfo::SetSubProductId(const std::string& subProductId) { this->subProductId_ = subProductId; } @@ -92,7 +92,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); - WRITE_HELPER_RET(parcel, UInt8Vector, icon_); + // WRITE_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -103,14 +103,14 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); - READ_HELPER_RET(parcel, UInt8Vector, icon_, false); + // READ_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const { - bool isNotEqual = (id_ != other.GetId() || productId_ != other.GetProuctId() - || subProductId_ != other.GetSubProuctId() || imageType_ != other.GetImageType() || + bool isNotEqual = (id_ != other.GetId() || productId_ != other.GetProductId() + || subProductId_ != other.GetSubProductId() || imageType_ != other.GetImageType() || specName_ != other.GetSpecName() || icon_ != other.GetIcon()); if (isNotEqual) { return true; @@ -119,17 +119,17 @@ bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const } } -std::string DistributedDeviceProfile::dump() const +std::string DeviceIconInfo::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { return EMPTY_STRING; } cJSON_AddNumberToObject(json, ID.c_str(), id_); - cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); - cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); - cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); - cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_.c_str()); + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_.c_str()); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_.c_str()); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_.c_str()); char* jsonChars = cJSON_PrintUnformatted(json); if (jsonChars == NULL) { cJSON_Delete(json); diff --git a/common/src/interfaces/device_icon_info_filter_options.cpp b/common/src/interfaces/device_icon_info_filter_options.cpp index a6cf7233..dd924d13 100644 --- a/common/src/interfaces/device_icon_info_filter_options.cpp +++ b/common/src/interfaces/device_icon_info_filter_options.cpp @@ -25,49 +25,50 @@ namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "DeviceIconInfoFilterOptions"; + const std::string PRODUCT_IDS = "productIds"; } -std::vector DistributedDeviceProfile::GetProductIds() const +std::vector DeviceIconInfoFilterOptions::GetProductIds() const { return productIds_; } -void DistributedDeviceProfile::SetProductIds(const std::vector& productIds) +void DeviceIconInfoFilterOptions::SetProductIds(const std::vector& productIds) { - specName_ = specName; + productIds_ = productIds; } -std::string DistributedDeviceProfile::GetSubProductId() const +std::string DeviceIconInfoFilterOptions::GetSubProductId() const { return subProductId_; } -void DistributedDeviceProfile::SetSubProductId(const std::string& subProductId) +void DeviceIconInfoFilterOptions::SetSubProductId(const std::string& subProductId) { subProductId_ = subProductId; } -std::string DistributedDeviceProfile::GetImageType() const +std::string DeviceIconInfoFilterOptions::GetImageType() const { return imageType_; } -void DistributedDeviceProfile::SetImageType(const std::string& imageType) +void DeviceIconInfoFilterOptions::SetImageType(const std::string& imageType) { imageType_ = imageType; } -std::string DistributedDeviceProfile::GetSpecName() const +std::string DeviceIconInfoFilterOptions::GetSpecName() const { return specName_; } -void DistributedDeviceProfile::SetSpecName(const std::string& specName) +void DeviceIconInfoFilterOptions::SetSpecName(const std::string& specName) { specName_ = specName; } -bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const +bool DeviceIconInfoFilterOptions::Marshalling(MessageParcel& parcel) const { IpcUtils::Marshalling(parcel, productIds_); WRITE_HELPER_RET(parcel, String, subProductId_, false); @@ -76,7 +77,7 @@ bool DistributedDeviceProfile::Marshalling(MessageParcel& parcel) const return true; } -bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) +bool DeviceIconInfoFilterOptions::UnMarshalling(MessageParcel& parcel) { IpcUtils::UnMarshalling(parcel, productIds_); READ_HELPER_RET(parcel, String, subProductId_, false); @@ -85,7 +86,7 @@ bool DistributedDeviceProfile::UnMarshalling(MessageParcel& parcel) return true; } -std::string DistributedDeviceProfile::dump() const +std::string DeviceIconInfoFilterOptions::dump() const { cJSON* json = cJSON_CreateObject(); if (json == NULL) { @@ -111,12 +112,11 @@ std::string DistributedDeviceProfile::dump() const } if (!cJSON_AddItemToObject(json, PRODUCT_IDS.c_str(), prodIdsJson)) { cJSON_Delete(prodIdsJson); - HILOGE("Add json array to Object failed!"); - return EMPTY_STRING; + HILOGW("Add json array to Object failed!"); } - cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_); - cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_); - cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_); + cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_.c_str()); + cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_.c_str()); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_.c_str()); char* jsonChars = cJSON_PrintUnformatted(json); if (jsonChars == NULL) { cJSON_Delete(json); diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp index b42b5d67..c7b0a99f 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -25,12 +25,12 @@ namespace { const std::string TAG = "ProductInfo"; } -std::string ProductInfo::GetProuctId() const +std::string ProductInfo::GetProductId() const { return productId_; } -void ProductInfo::SetProuctId(const std::string& productId) +void ProductInfo::SetProductId(const std::string& productId) { this->productId_ = productId; } @@ -72,7 +72,7 @@ int32_t ProductInfo::GetImageVersion() const void ProductInfo::SetImageVersion(const int32_t imageVersion) { - this->ImageVersion_ = imageVersion; + this->imageVersion_ = imageVersion; } @@ -98,7 +98,7 @@ bool ProductInfo::UnMarshalling(MessageParcel& parcel) bool ProductInfo::operator!=(const ProductInfo& other) const { - bool isNotEqual = (productId_ != other.GetProuctId() || model_ != other.GetModel() || + bool isNotEqual = (productId_ != other.GetProductId() || model_ != other.GetModel() || productName_ != other.GetProductName() || productShortName_ != other.GetProductShortName() || imageVersion_ != other.GetImageVersion()); if (isNotEqual) { @@ -114,10 +114,10 @@ std::string ProductInfo::dump() const if (json == NULL) { return EMPTY_STRING; } - cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_); - cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_); - cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_); - cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_); + cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_.c_str()); + cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_.c_str()); + cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_.c_str()); + cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_.c_str()); cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); char* jsonChars = cJSON_PrintUnformatted(json); if (jsonChars == NULL) { diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index 265d990d..14bbbbfd 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -30,6 +30,13 @@ extern const std::string SELECT_DEVICE_PROFILE_TABLE; extern const std::string SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID_ACCOUNTID; extern const std::string CREATE_DEVICE_PROFILE_TABLE_SQL; extern const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL; +// DeviceIconInfoDao +extern const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL ; +extern const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL; +extern const std::string SELECT_DEVICE_ICON_INFO_TABLE; +// ProductInfoDao +extern const std::string CREATE_PRODUCT_INFO_TABLE_SQL; +extern const std::string SELECT_PRODUCT_INFO_TABLE; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_SERVICES_CONSTANTS_H \ No newline at end of file diff --git a/services/core/include/profiledatamanager/device_icon_info_dao.h b/services/core/include/profiledatamanager/device_icon_info_dao.h index 97e4371c..35c62d38 100644 --- a/services/core/include/profiledatamanager/device_icon_info_dao.h +++ b/services/core/include/profiledatamanager/device_icon_info_dao.h @@ -37,14 +37,14 @@ class DeviceIconInfoDao { public: int32_t Init(); int32_t UnInit(); - int32_t PutDeviceIconInfo(DeviceIconInfo& deviceIconInfo); - int32_t GetDeviceIconInfos(const std::vector& productIds, + int32_t PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions, std::vector& deviceIconInfos); int32_t DeleteDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); int32_t UpdateDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); int32_t CreateTable(); int32_t CreateIndex(); - void CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, + bool CreateQuerySqlAndCondition(const DeviceIconInfoFilterOptions& filterOptions, std::string& sql, std::vector& condition); int32_t DeviceIconInfoToEntries(const DeviceIconInfo& deviceIconInfo, ValuesBucket& values); int32_t ConvertToDeviceIconInfo(std::shared_ptr resultSet, DeviceIconInfo& deviceIconInfo); diff --git a/services/core/include/profiledatamanager/product_info_dao.h b/services/core/include/profiledatamanager/product_info_dao.h index ceddac7b..b3bfb25e 100644 --- a/services/core/include/profiledatamanager/product_info_dao.h +++ b/services/core/include/profiledatamanager/product_info_dao.h @@ -36,16 +36,16 @@ class ProductInfoDao { public: int32_t Init(); int32_t UnInit(); - int32_t PutProductInfo(ProductInfo& productInfo); + int32_t PutProductInfo(const ProductInfo& productInfo); int32_t GetProductInfos(const std::vector& productIds, std::vector& productInfos); int32_t DeleteProductInfo(const ProductInfo& productInfo); int32_t UpdateProductInfo(const ProductInfo& productInfo); int32_t CreateTable(); int32_t CreateIndex(); - void CreateQuerySqlAndCondition(const ProductInfoFilterOptions& filterOptions, + bool CreateQuerySqlAndCondition(const std::vector& productIds, std::string& sql, std::vector& condition); - int32_t ProductInfoToEntries(const ProductInfo& ProductInfo, ValuesBucket& values); + int32_t ProductInfoToEntries(const ProductInfo& productInfo, ValuesBucket& values); int32_t ConvertToProductInfo(std::shared_ptr resultSet, ProductInfo& productInfo); private: std::mutex rdbMutex_; diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 4563d3a4..7341c286 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -77,5 +77,6 @@ model TEXT,\ productName TEXT,\ productShortName TEXT,\ imageVersion INTEGER);"; +const std::string SELECT_PRODUCT_INFO_TABLE = "SELECT * FROM product_info WHERE "; } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp index df3a282b..0fb9f363 100644 --- a/services/core/src/profiledatamanager/device_icon_info_dao.cpp +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -18,6 +18,7 @@ #include "distributed_device_profile_constants.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" +#include "dp_services_constants.h" #include "profile_data_rdb_adapter.h" namespace OHOS { @@ -78,7 +79,7 @@ int32_t DeviceIconInfoDao::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInf return DP_SUCCESS; } -int32_t DeviceIconInfoDao::GetDeviceIconInfos(const DeviceIconInfoOptions& filterOptions, +int32_t DeviceIconInfoDao::GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions, std::vector& deviceIconInfos) { std::string sql; @@ -151,7 +152,7 @@ int32_t DeviceIconInfoDao::CreateTable() std::lock_guard lock(rdbMutex_); int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_SQL); if (ret != DP_SUCCESS) { - HILOGE("%{public}s create failed", DEVICE_ICON_INFO_TABLE); + HILOGE("%{public}s create failed", DEVICE_ICON_INFO_TABLE.c_str()); return DP_CREATE_TABLE_FAIL; } return DP_SUCCESS; @@ -162,7 +163,7 @@ int32_t DeviceIconInfoDao::CreateIndex() std::lock_guard lock(rdbMutex_); int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL); if (ret != DP_SUCCESS) { - HILOGE("%{public}s unique index create failed", DEVICE_ICON_INFO_TABLE); + HILOGE("%{public}s unique index create failed", DEVICE_ICON_INFO_TABLE.c_str()); return DP_CREATE_UNIQUE_INDEX_FAIL; } return DP_SUCCESS; @@ -175,7 +176,7 @@ bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOpt bool flag = false; if (!filterOptions.GetProductIds().empty()) { sql += PRODUCT_ID + "IN("; - for (cosnt auto& prodId : filterOptions.GetProductIds()) { + for (const auto& prodId : filterOptions.GetProductIds()) { sql += "?,"; condition.emplace_back(ValueObject(prodId)); } @@ -185,17 +186,17 @@ bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOpt } if (!filterOptions.GetSubProductId().empty()) { sql += SUB_PRODUCT_ID + " = ? AND "; - condition.emplace_back(ValueObject(filterOptions.GetUserId())); + condition.emplace_back(ValueObject(filterOptions.GetSubProductId())); flag = true; } if (!filterOptions.GetImageType().empty()) { sql += IMAGE_TYPE + " = ? AND "; - condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + condition.emplace_back(ValueObject(filterOptions.GetImageType())); flag = true; } if (!filterOptions.GetSpecName().empty()) { sql += SPEC_NAME + " = ? AND "; - condition.emplace_back(ValueObject(filterOptions.GetAccountId())); + condition.emplace_back(ValueObject(filterOptions.GetSpecName())); flag = true; } if (flag) { diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp index e03d2cad..f9b48ce4 100644 --- a/services/core/src/profiledatamanager/product_info_dao.cpp +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -18,6 +18,7 @@ #include "distributed_device_profile_constants.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" +#include "dp_services_constants.h" #include "profile_data_rdb_adapter.h" namespace OHOS { @@ -151,7 +152,7 @@ int32_t ProductInfoDao::CreateTable() std::lock_guard lock(rdbMutex_); int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_PRODUCT_INFO_TABLE_SQL); if (ret != DP_SUCCESS) { - HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE); + HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE.c_str()); return DP_CREATE_TABLE_FAIL; } return DP_SUCCESS; @@ -169,7 +170,7 @@ bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& bool flag = false; if (!productIds.empty()) { sql += PRODUCT_ID + "IN("; - for (cosnt auto& prodId : productIds) { + for (const auto& prodId : productIds) { sql += "?,"; condition.emplace_back(ValueObject(prodId)); } -- Gitee From 63713f6950df6b7ea88660406928a430d728763a Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Mon, 16 Dec 2024 20:14:01 +0800 Subject: [PATCH 13/55] ServiceProfileDao compile pass Signed-off-by: wangzhaohao --- .../constants/distributed_device_profile_constants.h | 10 +++------- .../constants/distributed_device_profile_constants.cpp | 10 +++------- common/src/utils/ipc_utils.cpp | 4 ++-- .../include/profiledatamanager/service_profile_dao.h | 3 +++ services/core/src/common/dp_services_constants.cpp | 2 +- .../src/profiledatamanager/service_profile_dao.cpp | 2 +- .../subscribe_profile_manager.cpp | 6 +++--- services/core/test/unittest/profile_cache_test.cpp | 10 +++++----- 8 files changed, 21 insertions(+), 26 deletions(-) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index ed5b90c0..f9ffd221 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -65,10 +65,12 @@ extern const std::string PRODUCTOR_INFO_VERSION; extern const std::string DEVICE_PROFILE_TABLE; /* ServiceProfile Attribute */ extern const std::string SERVICE_NAME; -extern const std::string SERVICE_ID; +extern const std::string SERVICE_PROFILE_SERVICE_ID; extern const std::string SERVICE_TYPE; extern const std::string SERVICE_PROFILE_TABLE; extern const std::string RDB_USER_ID; +extern const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID; +extern const std::string SERVICE_PROFILE_SERVICE_TYPE; /* CharacteristicProfile Attribute */ extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; @@ -106,11 +108,6 @@ extern const std::string ACCESSEE_TOKEN_ID; extern const std::string ACCESSEE_BUNDLE_NAME; extern const std::string ACCESSEE_HAP_SIGNATURE; extern const std::string ACCESSEE_BIND_LEVEL; -/* ServiceProfile Attribute */ -extern const std::string SERVICE_PROFILE_ID; -extern const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID; -extern const std::string SERVICE_PROFILE_SERVICE_ID; -extern const std::string SERVICE_PROFILE_SERVICE_TYPE; /* subscribe info */ extern const std::string SA_ID; extern const std::string SUBSCRIBE_KEY; @@ -174,7 +171,6 @@ extern const std::string SVR_PREFIX; extern const std::string CHAR_PREFIX; extern const std::string USER_ID; extern const std::string TOKEN_ID; -extern const std::string ID; extern const std::string DEVICE_PROFILE_ID; extern const std::string ALL_PROC; constexpr int32_t NUM_1 = 1; diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index 8691fd0b..40da4cc1 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -55,10 +55,12 @@ const std::string PRODUCTOR_INFO_VERSION = "productorInfoVersion"; const std::string DEVICE_PROFILE_TABLE = "device_profile"; /* ServiceProfile Attribute */ const std::string SERVICE_NAME = "serviceName"; -const std::string SERVICE_ID = "serviceId"; +const std::string SERVICE_PROFILE_SERVICE_ID = "serviceId"; const std::string SERVICE_TYPE = "serviceType"; 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 CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; @@ -96,11 +98,6 @@ const std::string ACCESSEE_TOKEN_ID = "accesseeTokenId"; const std::string ACCESSEE_BUNDLE_NAME = "accesseeBundleName"; const std::string ACCESSEE_HAP_SIGNATURE = "accesseeHapSignature"; const std::string ACCESSEE_BIND_LEVEL = "accesseeBindLevel"; -/* ServiceProfile Attribute */ -const std::string SERVICE_PROFILE_ID = "id"; -const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID = "deviceProfileId"; -const std::string SERVICE_PROFILE_SERVICE_ID = "serviceId"; -const std::string SERVICE_PROFILE_SERVICE_TYPE = "serviceType"; /* subscribe info */ const std::string SA_ID = "saId"; const std::string SUBSCRIBE_KEY = "subscribeKey"; @@ -136,7 +133,6 @@ const std::string DEV_PREFIX = "dev"; const std::string SVR_PREFIX = "svr"; const std::string CHAR_PREFIX = "char"; const std::string USER_ID = "user_id"; -const std::string ID = "id"; const std::string DEVICE_PROFILE_ID = "deviceProfile_id"; const std::string TOKEN_ID = "token_id"; const std::string ALL_PROC = "all"; diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index b51fc826..25b21915 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -234,7 +234,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector MAX_PROFILE_SIZE) { - HILOGE("Profile size is invalid!size : %{public}zu", size); + HILOGE("Profile size is invalid!size : %{public}u", size); return false; } for (uint32_t i = 0; i < size; i++) { @@ -267,7 +267,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params { size_t size = parcel.ReadUint32(); if (size == 0 || size > MAX_ID_SIZE) { - HILOGE("params size is invalid!size : %{public}u", size); + HILOGE("params size is invalid!size : %{public}zu", size); return false; } for (uint32_t i = 0; i < size; i++) { diff --git a/services/core/include/profiledatamanager/service_profile_dao.h b/services/core/include/profiledatamanager/service_profile_dao.h index 69b407df..391f1eb2 100644 --- a/services/core/include/profiledatamanager/service_profile_dao.h +++ b/services/core/include/profiledatamanager/service_profile_dao.h @@ -16,6 +16,9 @@ #ifndef OHOS_DP_SERVICE_PROFILE_DAO_H #define OHOS_DP_SERVICE_PROFILE_DAO_H +#include "values_bucket.h" + +#include "profile_data_rdb_adapter.h" #include "service_profile.h" #include "service_profile_filter_opotions.h" diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index a109c3fd..6c6696b1 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -74,7 +74,7 @@ const std::string CREATE_SERVICE_PROFILE_TABLE_SQL = " deviceProfileId\n" " )\n" " REFERENCES device_profile (id)\n" -")" +")"; const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL = "CREATE INDEX if not exists serviceId_idx ON service_profile (serviceId)"; } // namespace DistributedDeviceProfile diff --git a/services/core/src/profiledatamanager/service_profile_dao.cpp b/services/core/src/profiledatamanager/service_profile_dao.cpp index d1f91dba..5901ea0c 100644 --- a/services/core/src/profiledatamanager/service_profile_dao.cpp +++ b/services/core/src/profiledatamanager/service_profile_dao.cpp @@ -210,7 +210,7 @@ int32_t ServiceProfileDao::ConvertToServiceProfile(std::shared_ptr re serviceProfile.SetId(rowEntity.Get(ID)); serviceProfile.SetDeviceProfileId(rowEntity.Get(SERVICE_PROFILE_DEVICE_PROFILE_ID)); serviceProfile.SetDeviceId(rowEntity.Get(DEVICE_ID)); - serviceProfile.SetServiceName(rowEntity.Get(SERVICE_ID)); + serviceProfile.SetServiceName(rowEntity.Get(SERVICE_PROFILE_SERVICE_ID)); serviceProfile.SetServiceType(rowEntity.Get(SERVICE_TYPE)); serviceProfile.SetUserId(rowEntity.Get(RDB_USER_ID)); return DP_SUCCESS; diff --git a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp index 1e8811df..008c2fd0 100644 --- a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp +++ b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp @@ -616,7 +616,7 @@ int32_t SubscribeProfileManager::NotifyServiceProfileAdd(const ServiceProfile& s continue; } if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_ADD) != 0) { - listenerProxy->OnDeviceProfileAdd(serviceProfile); + listenerProxy->OnServiceProfileAdd(serviceProfile); } } return DP_SUCCESS; @@ -636,7 +636,7 @@ int32_t SubscribeProfileManager::NotifyServiceProfileUpdate(const ServiceProfile continue; } if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_UPDATE) != 0) { - listenerProxy->OnServiceProfileUpdate(ServiceProfile oldProfile, serviceProfile); + listenerProxy->OnServiceProfileUpdate(ServiceProfile(), serviceProfile); } } return DP_SUCCESS; @@ -656,7 +656,7 @@ int32_t SubscribeProfileManager::NotifyServiceProfileDelete(const ServiceProfile continue; } if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_DELETE) != 0) { - listenerProxy->OnDeviceProfileDelete(serviceProfile); + listenerProxy->OnServiceProfileDelete(serviceProfile); } } return DP_SUCCESS; diff --git a/services/core/test/unittest/profile_cache_test.cpp b/services/core/test/unittest/profile_cache_test.cpp index 356ddf5c..9cb605a3 100644 --- a/services/core/test/unittest/profile_cache_test.cpp +++ b/services/core/test/unittest/profile_cache_test.cpp @@ -78,22 +78,22 @@ HWTEST_F(ProfileCacheTest, AddDeviceProfile_001, TestSize.Level2) HWTEST_F(ProfileCacheTest, AddServiceProfile_001, TestSize.Level2) { ServiceProfile serviceProfile; - int32_t ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); + int32_t ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); EXPECT_EQ(DP_INVALID_PARAMS, ret); std::string devId = "dp_devId"; serviceProfile.SetDeviceId(devId); - ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); EXPECT_EQ(DP_INVALID_PARAMS, ret); std::string serName = "dp_serName"; serviceProfile.SetServiceName(serName); - ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); EXPECT_EQ(DP_SUCCESS, ret); devId = ""; serviceProfile.SetDeviceId(devId); - ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); EXPECT_EQ(DP_INVALID_PARAMS, ret); devId = "dp_devId"; @@ -101,7 +101,7 @@ HWTEST_F(ProfileCacheTest, AddServiceProfile_001, TestSize.Level2) for (int i = 1; i < MAX_DEVICE_SIZE + 1; ++i) { ProfileCache::GetInstance().serviceProfileMap_[std::to_string(i)] = serviceProfile; } - ret = ProfileCache::GetInstance().PutServiceProfile(serviceProfile); + ret = ProfileCache::GetInstance().AddServiceProfile(serviceProfile); EXPECT_EQ(DP_EXCEED_MAX_SIZE_FAIL, ret); } -- Gitee From f4a0ec6a10865ae12d6e2c732380eb7f989a6406 Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 16 Dec 2024 21:06:52 +0800 Subject: [PATCH 14/55] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ipc=E5=AF=B9unint8?= =?UTF-8?q?=E7=9A=84=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/include/utils/ipc_utils.h | 1 + common/src/interfaces/device_icon_info.cpp | 4 ++-- common/src/utils/ipc_utils.cpp | 27 +++++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/common/include/utils/ipc_utils.h b/common/include/utils/ipc_utils.h index 0222f385..ee1e782f 100644 --- a/common/include/utils/ipc_utils.h +++ b/common/include/utils/ipc_utils.h @@ -58,6 +58,7 @@ public: static bool UnMarshalling(MessageParcel& parcel, std::vector& charProfiles); static bool UnMarshalling(MessageParcel& parcel, std::vector& strings); static bool UnMarshalling(MessageParcel& parcel, std::vector& params); + static bool UnMarshalling(MessageParcel& parcel, std::vector& params); static bool UnMarshalling(MessageParcel& parcel, std::map& params); static bool UnMarshalling(MessageParcel& parcel, std::map& listenerMap); diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 731f18d4..9776af25 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -92,7 +92,7 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); - // WRITE_HELPER_RET(parcel, UInt8Vector, icon_, false); + WRITE_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -103,7 +103,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); - // READ_HELPER_RET(parcel, UInt8Vector, icon_, false); + READ_HELPER_RET(parcel, UInt8Vector, &icon_, false); return true; } diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index 8b937463..6812959b 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -90,7 +90,7 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& strings) { if (strings.empty() || strings.size() > MAX_ID_SIZE) { - HILOGE("strings size is invalid!size : %{public}zu", strings.size()); + HILOGE("string vector, strings size is invalid! size : %{public}zu", strings.size()); return false; } size_t size = strings.size(); @@ -104,7 +104,7 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) { if (params.empty() || params.size() > MAX_ID_SIZE) { - HILOGE("params size is invalid!size : %{public}zu", params.size()); + HILOGE("int32_t vector, params size is invalid! size : %{public}zu", params.size()); return false; } size_t size = params.size(); @@ -117,8 +117,8 @@ bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& pa bool IpcUtils::Marshalling(MessageParcel& parcel, const std::vector& params) { - if (params.empty() || params.size() > MAX_ID_SIZE) { - HILOGE("params size is invalid!size : %{public}zu", params.size()); + if (params.empty() || params.size() > MAX_ICON_SIZE) { + HILOGE("uint8_t vector, params size is invalid! size : %{public}zu", params.size()); return false; } size_t size = params.size(); @@ -266,7 +266,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& st { size_t size = parcel.ReadUint32(); if (size == 0 || size > MAX_ID_SIZE) { - HILOGE("strings size is invalid!size : %{public}zu", size); + HILOGE("string vector, strings size is invalid!size : %{public}zu", size); return false; } for (uint32_t i = 0; i < size; i++) { @@ -281,7 +281,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params { size_t size = parcel.ReadUint32(); if (size == 0 || size > MAX_ID_SIZE) { - HILOGE("params size is invalid!size : %{public}u", size); + HILOGE("int32_t vector, params size is invalid! size : %{public}u", size); return false; } for (uint32_t i = 0; i < size; i++) { @@ -292,6 +292,21 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params return true; } +bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params) +{ + size_t size = parcel.ReadUint32(); + if (size == 0 || size > MAX_ICON_SIZE) { + HILOGE("uint8_t vector, params size is invalid! size : %{public}u", size); + return false; + } + for (uint32_t i = 0; i < size; i++) { + uint8_t item = 0; + READ_HELPER_RET(parcel, Uint8, item, false); + params.emplace_back(item); + } + return true; +} + bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::map& params) { uint32_t size = parcel.ReadUint32(); -- Gitee From 6dfb9bca3088d0ef5b778450fb7dffe5d4eaeb39 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:31:16 +0000 Subject: [PATCH 15/55] characteristicProfileDao Signed-off-by: torrizo --- .../characteristic_profile_dao.cpp | 287 ++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 services/core/src/profiledatamanager/characteristic_profile_dao.cpp 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..b5058e94 --- /dev/null +++ b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp @@ -0,0 +1,287 @@ +/* + * 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 "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 { +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; + CharProfileToEntries(charProfile, values); + 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 -1; // DP_PUT_TRUST_DEVICE_PROFILE_FAIL; + } + if (SetCharacteristicProfileId(charProfile) != DP_SUCCESS) { + HILOGE("SetCharacteristicProfileId fail"); + return -1; // DP_PUT_TRUST_DEVICE_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_RESULTSET_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) +{ + 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_TRUST_DEVICE_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; + CharProfileToEntries(newProfile, values); + 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); + 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 += "characteristic_profile.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 += "characteristic_profile.characteristicKey = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetCharacteristicKey())); + } + if (filterOptions.GetServiceProfileId() != 0) { + sql += "characteristic_profile.serviceProfileId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetServiceProfileId())); + } + if (!filterOptions.GetServiceId().empty()) { + sql += "service_profile.serviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetServiceId())); + } + if (!filterOptions.GetDeviceIds().empty()) { + sql += "service_profile.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetDeviceIds())); + } + if (filterOptions.GetUserId() != 0) { + sql += "device_profile.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetUserId())); + } + if (!filterOptions.GetAccountld().empty()) { + sql += "device_profile.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetAccountld())); + } + if (!filterOptions.GetWiseDeviceId().empty()) { + sql += "device_profile.deviceId = ? AND "; + condition.emplace_back(ValueObject(filterOptions.GetWiseDeviceId())); + } + if (sql.empty()) { + return; + } + sql.erase(sql.end() - 4); + return; +} + +int32_t CharacteristicProfileDao::CharProfileToEntries(const CharacteristicProfile& charProfile, ValuesBucket& values) +{ + values.PutInt(ID, charProfile.GetId()); + 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{}); + 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 \ No newline at end of file -- Gitee From 02bfa38046bd62c4e3a6fb414443af71459b282a Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Tue, 17 Dec 2024 11:32:26 +0800 Subject: [PATCH 16/55] service_profile_dao update Signed-off-by: wangzhaohao --- .../interfaces/service_profile_filter_opotions.cpp | 9 +++++++++ .../include/profiledatamanager/service_profile_dao.h | 6 +++--- .../src/profiledatamanager/service_profile_dao.cpp | 12 ++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/common/src/interfaces/service_profile_filter_opotions.cpp b/common/src/interfaces/service_profile_filter_opotions.cpp index b52621ef..c88a5e26 100644 --- a/common/src/interfaces/service_profile_filter_opotions.cpp +++ b/common/src/interfaces/service_profile_filter_opotions.cpp @@ -16,6 +16,7 @@ #include "service_profile_filter_opotions.h" #include "cJSON.h" + #include "distributed_device_profile_constants.h" #include "ipc_utils.h" #include "macro_utils.h" @@ -133,6 +134,10 @@ bool ServiceProfileFilterOptions::Marshalling(MessageParcel& parcel) const HILOGE("dp write parcel fail"); return false; } + if (!IpcUtils::Marshalling(parcel, deviceProfileIds_)) { + HILOGE("dp write parcel fail"); + return false; + } return true; } @@ -156,6 +161,10 @@ bool ServiceProfileFilterOptions::UnMarshalling(MessageParcel& parcel) HILOGE("dp read parcel fail"); return false; } + if (!IpcUtils::UnMarshalling(parcel, deviceProfileIds_)) { + HILOGE("dp read parcel fail"); + return false; + } return true; } diff --git a/services/core/include/profiledatamanager/service_profile_dao.h b/services/core/include/profiledatamanager/service_profile_dao.h index 391f1eb2..ee64268d 100644 --- a/services/core/include/profiledatamanager/service_profile_dao.h +++ b/services/core/include/profiledatamanager/service_profile_dao.h @@ -31,9 +31,9 @@ public: int32_t Init(); int32_t UnInit(); int32_t PutServiceProfile(ServiceProfile& serviceProfile); - int32_t DeleteServiceProfile(ServiceProfile& serviceProfile); - int32_t UpdateServiceProfile(ServiceProfile& serviceProfile); - int32_t GetServiceProfiles(ServiceProfileFilterOptions& filterOptions, + int32_t DeleteServiceProfile(const ServiceProfile& serviceProfile); + int32_t UpdateServiceProfile(const ServiceProfile& serviceProfile); + int32_t GetServiceProfiles(const ServiceProfileFilterOptions& filterOptions, std::vector& serviceProfiles); private: int32_t CreateTable(); diff --git a/services/core/src/profiledatamanager/service_profile_dao.cpp b/services/core/src/profiledatamanager/service_profile_dao.cpp index 5901ea0c..0c7cb2d1 100644 --- a/services/core/src/profiledatamanager/service_profile_dao.cpp +++ b/services/core/src/profiledatamanager/service_profile_dao.cpp @@ -22,9 +22,9 @@ #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" #include "dp_services_constants.h" -#include "subscribe_profile_manager.h" #include "profile_cache.h" #include "profile_data_rdb_adapter.h" +#include "subscribe_profile_manager.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -87,10 +87,10 @@ int32_t ServiceProfileDao::PutServiceProfile(ServiceProfile& serviceProfile) return DP_SUCCESS; } -int32_t ServiceProfileDao::DeleteServiceProfile(ServiceProfile& serviceProfile) +int32_t ServiceProfileDao::DeleteServiceProfile(const ServiceProfile& serviceProfile) { if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || - serviceProfile.GetServiceType().empty() || serviceProfile.GetDeviceProfileId() == DEFAULT_DEVICE_PROFILE_ID) { + serviceProfile.GetServiceType().empty() || serviceProfile.GetId() == DEFAULT_SERVICE_PROFILE_ID) { HILOGE("serviceProfile params is invalid!"); return DP_INVALID_PARAMS; } @@ -111,10 +111,10 @@ int32_t ServiceProfileDao::DeleteServiceProfile(ServiceProfile& serviceProfile) return DP_SUCCESS; } -int32_t ServiceProfileDao::UpdateServiceProfile(ServiceProfile& serviceProfile) +int32_t ServiceProfileDao::UpdateServiceProfile(const ServiceProfile& serviceProfile) { if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || - serviceProfile.GetServiceType().empty() || serviceProfile.GetDeviceProfileId() == DEFAULT_DEVICE_PROFILE_ID) { + serviceProfile.GetServiceType().empty() || serviceProfile.GetId() == DEFAULT_SERVICE_PROFILE_ID) { HILOGE("serviceProfile params is invalid!"); return DP_INVALID_PARAMS; } @@ -137,7 +137,7 @@ int32_t ServiceProfileDao::UpdateServiceProfile(ServiceProfile& serviceProfile) return DP_SUCCESS; } -int32_t DistributedDeviceProfile::ServiceProfileDao::GetServiceProfiles(ServiceProfileFilterOptions& filterOptions, +int32_t DistributedDeviceProfile::ServiceProfileDao::GetServiceProfiles(const ServiceProfileFilterOptions& filterOptions, std::vector& serviceProfiles) { if (filterOptions.IsEmpty()) { -- Gitee From cba476d4ea6dee80260e2b7d60c171c73baf7fb9 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:32:34 +0000 Subject: [PATCH 17/55] characteristicProfileDao Signed-off-by: torrizo --- .../characteristic_profile_dao.h | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 services/core/include/profiledatamanager/characteristic_profile_dao.h 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..5fbf7669 --- /dev/null +++ b/services/core/include/profiledatamanager/characteristic_profile_dao.h @@ -0,0 +1,58 @@ +/* + * 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. + */ + +#ifndef OHOS_DP_DEVICE_PROFILE_DAO_H +#define OHOS_DP_DEVICE_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 { +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_DEVICE_PROFILE_DAO_H \ No newline at end of file -- Gitee From 225fa3bfd118ad57fe5f8352285c32b21fa79bbc Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:34:11 +0000 Subject: [PATCH 18/55] characteristicProfileDao Signed-off-by: torrizo --- .../characteristic_profile_filter_option.cpp | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 common/src/interfaces/characteristic_profile_filter_option.cpp 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..8919717b --- /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::GetDeviceIds() const +{ + return deviceId_; +} + +void CharacteristicProfileFilterOption::SetDeviceIds(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 -- Gitee From 574d9574338d3e8cfc1ba91d1f524add69353808 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:35:13 +0000 Subject: [PATCH 19/55] cc Signed-off-by: torrizo --- .../characteristic_profile_filter_option.h | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 common/include/interfaces/characteristic_profile_filter_option.h 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..b4b4c08a --- /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, + 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 GetDeviceIds() const; + void SetDeviceIds(std::string& deviceIds); + 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_ = -1; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_CHARACTERISTIC_PROFILE_FILTER_OPTION_H -- Gitee From c2caa6efa172f36c63761215ea4d5e3eb499b053 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:37:22 +0000 Subject: [PATCH 20/55] characteristicProfileDao c Signed-off-by: torrizo --- .../interfaces/characteristic_profile.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 57523ba0..8629b2c1 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -42,6 +42,16 @@ 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() = default; ~CharacteristicProfile() = default; @@ -57,6 +67,12 @@ 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; @@ -69,6 +85,9 @@ private: std::string characteristicValue_ = ""; bool isMultiUser_ = false; int32_t userId_ = DEFAULT_USER_ID; + int32_t id_ = -1; + int32_t serviceProfileId_ = -1; + std::string serviceId_ = ""; }; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From e6d89d2a3079df68908fa4931966f6da1a58ebef Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:38:14 +0000 Subject: [PATCH 21/55] characteristicProfileDao Signed-off-by: torrizo --- .../src/interfaces/characteristic_profile.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index 13123602..0e776db2 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -84,6 +84,36 @@ 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; +} + +std::string CharacteristicProfile::GetServiceId() const +{ + return serviceId_; +} + +void CharacteristicProfile::SetServiceId(const std::string& serviceId) +{ + serviceId_ = serviceId; +} + bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, deviceId_, false); -- Gitee From eb1860b361e868f445f63eff1ed9ae665d6285d9 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:40:22 +0000 Subject: [PATCH 22/55] characteristicProfileDao Signed-off-by: torrizo --- .../core/src/common/dp_services_constants.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 33d57ffc..c9212c4d 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -96,5 +96,20 @@ const std::string CREATE_SERVICE_PROFILE_TABLE_SQL = ")"; const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL = "CREATE INDEX if not exists serviceId_idx ON service_profile (serviceId)"; +// CharateristicProfileDao +const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID = "SELECT MAX(id) FROM characteristic_profile"; +const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE = "SELECT * FROM characteristic_profile WHERE "; +const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS characteristic_profile\ +(\ + id INTEGER PRIMARY KEY AUTOINCREMENT,\ + serviceProfileId INTEGER NOT NULL,\ + characteristicKey TEXT,\ + characteristicValue TEXT,\ + FOREIGN KEY (\ + serviceProfileId\ + )\ + REFERENCES service_profile (id));"; +const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL = + "CREATE INDEX characteristicKey_idx ON characteristic_profile (characteristicKey);"; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 19b19e422a6585a67cc94c656803a569674a63fc Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:41:57 +0000 Subject: [PATCH 23/55] update services/core/include/common/dp_services_constants.h. Signed-off-by: torrizo --- services/core/include/common/dp_services_constants.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index bd17decc..918a82c0 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -42,6 +42,11 @@ extern const std::string SELECT_SERVICE_PROGILES; extern const std::string SELECT_SERVICE_PROFILE_TABLE_MAX_ID; extern const std::string CREATE_SERVICE_PROFILE_TABLE_SQL; extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL; +// CharacteristicProfileDao +extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE; +extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL; +extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL; +extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_SERVICES_CONSTANTS_H -- Gitee From 93646c9737a89cd7a38d251e0dc8c898b450a2c1 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:55:20 +0000 Subject: [PATCH 24/55] cc Signed-off-by: torrizo --- common/include/constants/distributed_device_profile_constants.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 5a2a2114..4824c84c 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -74,6 +74,8 @@ extern const std::string RDB_USER_ID; extern const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID; extern const std::string SERVICE_PROFILE_SERVICE_TYPE; /* CharacteristicProfile Attribute */ +extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID; +extern const std::string CHARACTERISTIC_PROFILE_TABLE; extern const std::string CHARACTERISTIC_KEY; extern const std::string CHARACTERISTIC_VALUE; /* ProductInfo Attribute */ -- Gitee From 255aa27130f77cb89507f367ccbed2fce913baea Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:55:55 +0000 Subject: [PATCH 25/55] characteristicProfileDao Signed-off-by: torrizo --- common/src/constants/distributed_device_profile_constants.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index 7856bfa0..5273a6bd 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -64,6 +64,8 @@ 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"; const std::string CHARACTERISTIC_KEY = "characteristicKey"; const std::string CHARACTERISTIC_VALUE = "characteristicValue"; /* ProductInfo Attribute */ -- Gitee From 10deb208066e9b86c5c4a8b331395dfe1d4220cb Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 03:59:12 +0000 Subject: [PATCH 26/55] cc Signed-off-by: torrizo --- .../subscribe_profile_manager.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp index 008c2fd0..3d53cdc7 100644 --- a/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp +++ b/services/core/src/subscribeprofilemanager/subscribe_profile_manager.cpp @@ -419,6 +419,67 @@ int32_t SubscribeProfileManager::NotifyServiceProfileDelete(const std::string& d return DP_SUCCESS; } +int32_t SubscribeProfileManager::NotifyCharProfileAdd(const CharacteristicProfile& charProfile) +{ + auto subscriberInfos = GetSubscribeInfos("CHARACTERISTIC_PROFILE_TABLE"); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", charProfile.dump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::CHAR_PROFILE_ADD) != 0) { + listenerProxy->OnCharacteristicProfileAdd(charProfile); + } + } + return DP_SUCCESS; +} + +int32_t SubscribeProfileManager::NotifyCharProfileUpdate(const CharacteristicProfile& oldCharProfile, + const CharacteristicProfile& newCharProfile) +{ + auto subscriberInfos = GetSubscribeInfos("CHARACTERISTIC_PROFILE_TABLE"); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", newCharProfile.dump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::CHAR_PROFILE_UPDATE) != 0) { + listenerProxy->OnCharacteristicProfileUpdate(oldCharProfile, newCharProfile); + } + } + return DP_SUCCESS; +} + +int32_t SubscribeProfileManager::NotifyCharProfileDelete(const CharacteristicProfile& charProfile) +{ + auto subscriberInfos = GetSubscribeInfos("CHARACTERISTIC_PROFILE_TABLE"); + if (subscriberInfos.empty()) { + return DP_SUCCESS; + } + HILOGI("%{public}s!", charProfile.dump().c_str()); + for (const auto& subscriberInfo : subscriberInfos) { + sptr listenerProxy = iface_cast(subscriberInfo.GetListener()); + if (listenerProxy == nullptr) { + HILOGE("Cast to IProfileChangeListener failed!"); + continue; + } + if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::CHAR_PROFILE_DELETE) != 0) { + listenerProxy->OnCharacteristicProfileDelete(charProfile); + } + } + return DP_SUCCESS; +} + int32_t SubscribeProfileManager::NotifyCharProfileAdd(const std::string& dbKey, const std::string& dbValue) { std::map values; -- Gitee From 9b73c6a436641b2bd79588ef00d9ee33f0fd8392 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 04:00:34 +0000 Subject: [PATCH 27/55] cc Signed-off-by: torrizo --- .../subscribeprofilemanager/subscribe_profile_manager.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h index ecdbd0bc..0a6cfa96 100644 --- a/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h +++ b/services/core/include/subscribeprofilemanager/subscribe_profile_manager.h @@ -51,6 +51,10 @@ public: int32_t NotifyServiceProfileAdd(const ServiceProfile& serviceProfile); int32_t NotifyServiceProfileUpdate(const ServiceProfile& serviceProfile); int32_t NotifyServiceProfileDelete(const ServiceProfile& serviceProfile); + int32_t NotifyCharProfileAdd(const CharacteristicProfile& charProfile); + int32_t NotifyCharProfileUpdate(const CharacteristicProfile& oldCharProfile, + const CharacteristicProfile& newCharProfile); + int32_t NotifyCharProfileDelete(const CharacteristicProfile& charProfile); private: int32_t NotifyDeviceProfileAdd(const std::string& dbKey, const std::string& dbValue); -- Gitee From 336f0eac59d418662f4c2da36a89a9e471951430 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 04:06:23 +0000 Subject: [PATCH 28/55] cc Signed-off-by: torrizo --- common/include/constants/distributed_device_profile_constants.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 4824c84c..008e01a7 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -74,7 +74,7 @@ extern const std::string RDB_USER_ID; extern const std::string SERVICE_PROFILE_DEVICE_PROFILE_ID; extern const std::string SERVICE_PROFILE_SERVICE_TYPE; /* CharacteristicProfile Attribute */ -extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID; +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; -- Gitee From 87c479ff7f2d780c2dff37b00dd0756fe4645499 Mon Sep 17 00:00:00 2001 From: guoyi Date: Tue, 17 Dec 2024 14:27:40 +0800 Subject: [PATCH 29/55] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/include/constants/distributed_device_profile_constants.h | 1 + 1 file changed, 1 insertion(+) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index c826dc79..9a225375 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -167,6 +167,7 @@ constexpr int32_t MAX_SUBSCRIBE_INFO_SIZE = 500; constexpr int32_t MAX_SYNC_RESULTS_SIZE = 50; constexpr int32_t MAX_STATIC_CAPABILITY_SIZE = 100; constexpr int32_t MAX_ID_SIZE = 1000; +constexpr int32_t MAX_ICON_SIZE = 1048576; constexpr int32_t MIN_USER_ID = 0; constexpr int32_t MAX_USER_ID = 100000; constexpr uint32_t MAX_TRUSTED_DEVICE_SIZE = 1000; -- Gitee From 4546d49685a215d602be647c3852c7d9a6b12255 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Tue, 17 Dec 2024 15:59:34 +0800 Subject: [PATCH 30/55] update Signed-off-by: zhanglei --- .../include/common/dp_services_constants.h | 2 + .../profiledatamanager/device_profile_dao.h | 4 ++ .../profiledatamanager/device_profile_dao.cpp | 71 ++++++++++--------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index 918a82c0..642e1781 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -23,6 +23,8 @@ namespace OHOS { namespace DistributedDeviceProfile { /* DeviceProfileDao */ +constexpr int32_t AND_LENGTH = 5; +constexpr int32_t WHERE_LENGTH = 7; extern const std::string PROFILE_DATA_RDB_PATH; extern const std::string PROFILE_DATA_DATABASE_NAME; extern const std::string ID_EQUAL_CONDITION; diff --git a/services/core/include/profiledatamanager/device_profile_dao.h b/services/core/include/profiledatamanager/device_profile_dao.h index c215722f..d2376338 100644 --- a/services/core/include/profiledatamanager/device_profile_dao.h +++ b/services/core/include/profiledatamanager/device_profile_dao.h @@ -51,6 +51,10 @@ public: std::string& sql, std::vector& condition); int32_t DeviceProfileToEntries(const DeviceProfile& deviceProfile, ValuesBucket& values); int32_t ConvertToDeviceProfile(std::shared_ptr resultSet, DeviceProfile& deviceProfile); + void GenerateSqlAndCondition(const std::vector& params, + std::string& sql, std::vector& condition); + void GenerateSqlAndCondition(const std::vector& params, + std::string& sql, std::vector& condition); private: std::mutex rdbMutex_; }; diff --git a/services/core/src/profiledatamanager/device_profile_dao.cpp b/services/core/src/profiledatamanager/device_profile_dao.cpp index 57f689db..3394f127 100644 --- a/services/core/src/profiledatamanager/device_profile_dao.cpp +++ b/services/core/src/profiledatamanager/device_profile_dao.cpp @@ -145,8 +145,8 @@ int32_t DeviceProfileDao::DeleteDeviceProfile(const DeviceProfile &deviceProfile HILOGE("delete device_profile data failed"); return DP_DELETE_TRUST_DEVICE_PROFILE_FAIL; } - SubscribeProfileManager::GetInstance().NotifyDeviceProfileDelete(deviceProfile); } + SubscribeProfileManager::GetInstance().NotifyDeviceProfileDelete(deviceProfile); HILOGI("end!"); return DP_SUCCESS; } @@ -165,8 +165,8 @@ int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &oldProfile, c HILOGE("Update device_profile table failed"); return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL; } - SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(oldProfile, newProfile); } + SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(oldProfile, newProfile); std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); if (localUdid == newProfile.GetDeviceId()) { DeviceProfileManager::GetInstance().PutDeviceProfile(newProfile); @@ -201,61 +201,42 @@ void DeviceProfileDao::CreateQuerySqlAndCondition(const DeviceProfileFilterOptio std::string &sql, std::vector &condition) { sql = SELECT_DEVICE_PROFILE_TABLE; - bool flag = false; + bool matchCondition = false; if (!filterOptions.GetDeviceProfileIds().empty()) { sql += "deviceProfileId IN("; - std::vector deviceProfileIds = filterOptions.GetDeviceProfileIds(); - for (auto deviceProfileId : deviceProfileIds) { - sql += "?,"; - condition.emplace_back(ValueObject(deviceProfileId)); - } - sql.erase(sql.end() - 1); - sql += ") AND "; - flag = true; + GenerateSqlAndCondition(filterOptions.GetDeviceProfileIds(), sql, condition); + matchCondition = true; } if (!filterOptions.GetDeviceIds().empty()) { sql += "deviceId IN("; - std::vector deviceIds = filterOptions.GetDeviceIds(); - for (auto deviceId : deviceIds) { - sql += "?,"; - condition.emplace_back(ValueObject(deviceId)); - } - sql.erase(sql.end() - 1); - sql += ") AND "; - flag = true; + GenerateSqlAndCondition(filterOptions.GetDeviceIds(), sql, condition); + matchCondition = true; } if (filterOptions.GetUserId() != DEFAULT_USER_ID) { sql += "userId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetUserId())); - flag = true; + matchCondition = true; } if (!filterOptions.GetAccountId().empty()) { sql += "accountId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetAccountId())); - flag = true; + matchCondition = true; } if (!filterOptions.GetWiseDeviceIds().empty()) { sql += "wiseDeviceId IN("; - std::vector wiseDeviceIds = filterOptions.GetWiseDeviceIds(); - for (auto wiseDeviceId : wiseDeviceIds) { - sql += "?,"; - condition.emplace_back(ValueObject(wiseDeviceId)); - } - sql.erase(sql.end() - 1); - sql += ") AND "; - flag = true; + GenerateSqlAndCondition(filterOptions.GetWiseDeviceIds(), sql, condition); + matchCondition = true; } - if (flag) { - sql.erase(sql.end() - 4); + if (matchCondition) { + sql.erase(sql.end() - AND_LENGTH); return; } - sql.erase(sql.end() - 6); + sql.erase(sql.end() - WHERE_LENGTH); return; } int32_t DeviceProfileDao::DeviceProfileToEntries(const DeviceProfile &deviceProfile, ValuesBucket &values) { - // values.PutInt(ID, deviceProfile.GetId()); values.PutString(DEVICE_ID, deviceProfile.GetDeviceId()); values.PutString(DEVICE_MODEL, deviceProfile.GetDeviceModel()); values.PutString(DEV_TYPE, deviceProfile.GetDevType()); @@ -320,5 +301,29 @@ int32_t DeviceProfileDao::ConvertToDeviceProfile( deviceProfile.SetAccountId(rowEntity.Get(ACCOUNTID)); return DP_SUCCESS; } + +void DeviceProfileDao::GenerateSqlAndCondition(const std::vector ¶ms, + std::string &sql, std::vector &condition) +{ + for (auto param : params) { + sql += "?,"; + condition.emplace_back(ValueObject(param)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + return; +} + +void DeviceProfileDao::GenerateSqlAndCondition(const std::vector ¶ms, + std::string &sql, std::vector &condition) +{ + for (auto param : params) { + sql += "?,"; + condition.emplace_back(ValueObject(param)); + } + sql.erase(sql.end() - 1); + sql += ") AND "; + return; +} } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file -- Gitee From f03f3e288f5faaefc15f84eb3c2909d2317fce3b Mon Sep 17 00:00:00 2001 From: guoyi Date: Tue, 17 Dec 2024 16:19:03 +0800 Subject: [PATCH 31/55] =?UTF-8?q?=E4=BF=AE=E6=94=B9DeviceIconInfo=E5=92=8C?= =?UTF-8?q?ProductInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- .../distributed_device_profile_constants.h | 4 +- common/include/interfaces/device_icon_info.h | 9 ++++ common/include/interfaces/product_info.h | 10 ++-- .../distributed_device_profile_constants.cpp | 4 +- common/src/interfaces/device_icon_info.cpp | 50 +++++++++++++++++-- common/src/interfaces/product_info.cpp | 41 ++++++++++++--- .../core/src/common/dp_services_constants.cpp | 8 ++- .../device_icon_info_dao.cpp | 20 +++++--- .../profiledatamanager/product_info_dao.cpp | 4 +- 9 files changed, 123 insertions(+), 27 deletions(-) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 9a225375..d1212657 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -79,7 +79,9 @@ extern const std::string IMAGE_VERSION; /* DeviceIconInfo Attribute */ extern const std::string IMAGE_TYPE; extern const std::string SPEC_NAME; -extern const std::string ICON; +extern const std::string DEVICE_ICON; +extern const std::string DEVICE_ICON_VERSION; +extern const std::string DEVICE_ICON_URL; /* TrustDeviceProfile Attribute */ extern const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE; extern const std::string DEVICE_ID_TYPE; diff --git a/common/include/interfaces/device_icon_info.h b/common/include/interfaces/device_icon_info.h index 4b60384b..2f8f3bd6 100644 --- a/common/include/interfaces/device_icon_info.h +++ b/common/include/interfaces/device_icon_info.h @@ -43,6 +43,12 @@ public: void SetImageType(const std::string& imageType); std::string GetSpecName() const; void SetSpecName(const std::string& specName); + std::string GetVersion() const; + void SetVersion(const std::string& version); + std::string GetWiseVersion() const; + void SetWiseVersion(const std::string& wiseVersion); + std::string GetUrl() const; + void SetUrl(const std::string& url); std::vector GetIcon() const; void SetIcon(const std::vector& icon); bool Marshalling(MessageParcel& parcel) const override; @@ -56,6 +62,9 @@ private: std::string subProductId_; std::string imageType_; std::string specName_; + std::string version_; + std::string wiseVersion_; + std::string url_; std::vector icon_; }; } // namespace DistributedDeviceProfile diff --git a/common/include/interfaces/product_info.h b/common/include/interfaces/product_info.h index c930fd4e..3f32690a 100644 --- a/common/include/interfaces/product_info.h +++ b/common/include/interfaces/product_info.h @@ -29,7 +29,7 @@ public: model_(""), productName_(""), productShortName_(""), - imageVersion_(0) + imageVersion_("") {} ~ProductInfo() = default; @@ -41,19 +41,21 @@ public: void SetProductName(const std::string& productName); std::string GetProductShortName() const; void SetProductShortName(const std::string& productShortName); - int32_t GetImageVersion() const; - void SetImageVersion(const int32_t imageVersion); + std::string GetImageVersion() const; + void SetImageVersion(const std::string& imageVersion); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const ProductInfo& other) const; std::string dump() const override; private: + std::string GetAnonyProductName(const std::string& value) const; + std::string productId_; std::string model_; std::string productName_; std::string productShortName_; - int32_t imageVersion_; + std::string imageVersion_; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index f76d3790..dec16334 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -69,7 +69,9 @@ const std::string IMAGE_VERSION = "imageVersion"; /* DeviceIconInfo Attribute */ const std::string IMAGE_TYPE = "imageType"; const std::string SPEC_NAME = "specName"; -const std::string ICON = "icon"; +const std::string DEVICE_ICON = "icon"; +const std::string DEVICE_ICON_VERSION = "version"; +const std::string DEVICE_ICON_URL = "url"; /* TrustDeviceProfile Attribute */ const std::string SUBSCRIBE_TRUST_DEVICE_PROFILE = "trust_device_profile"; const std::string DEVICE_ID_TYPE = "deviceIdType"; diff --git a/common/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 9776af25..970ff969 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -23,6 +23,8 @@ namespace OHOS { namespace DistributedDeviceProfile { namespace { const std::string TAG = "DeviceIconInfo"; + const std::string DEVICE_ICON_WISE_VERSION = "wiseVersion"; + const std::string DEVICE_ICON_SIZE = "iconSize"; } int32_t DeviceIconInfo::GetId() const @@ -75,6 +77,36 @@ void DeviceIconInfo::SetSpecName(const std::string& specName) this->specName_ = specName; } +std::string DeviceIconInfo::GetVersion() const +{ + return version_; +} + +void DeviceIconInfo::SetVersion(const std::string& version) +{ + version_ = version; +} + +std::string DeviceIconInfo::GetWiseVersion() const +{ + return wiseVersion_; +} + +void DeviceIconInfo::SetWiseVersion(const std::string& wiseVersion) +{ + wiseVersion_ = wiseVersion; +} + +std::string DeviceIconInfo::GetUrl() const +{ + return url_; +} + +void DeviceIconInfo::SetUrl(const std::string& url) +{ + url_ = url; +} + std::vector DeviceIconInfo::GetIcon() const { return icon_; @@ -92,6 +124,9 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, subProductId_, false); WRITE_HELPER_RET(parcel, String, imageType_, false); WRITE_HELPER_RET(parcel, String, specName_, false); + WRITE_HELPER_RET(parcel, String, version_, false); + WRITE_HELPER_RET(parcel, String, wiseVersion_, false); + WRITE_HELPER_RET(parcel, String, url_, false); WRITE_HELPER_RET(parcel, UInt8Vector, icon_, false); return true; } @@ -103,15 +138,19 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, subProductId_, false); READ_HELPER_RET(parcel, String, imageType_, false); READ_HELPER_RET(parcel, String, specName_, false); + READ_HELPER_RET(parcel, String, version_, false); + READ_HELPER_RET(parcel, String, wiseVersion_, false); + READ_HELPER_RET(parcel, String, url_, false); READ_HELPER_RET(parcel, UInt8Vector, &icon_, false); return true; } bool DeviceIconInfo::operator!=(const DeviceIconInfo& other) const { - bool isNotEqual = (id_ != other.GetId() || productId_ != other.GetProductId() - || subProductId_ != other.GetSubProductId() || imageType_ != other.GetImageType() || - specName_ != other.GetSpecName() || icon_ != other.GetIcon()); + bool isNotEqual = (id_ != other.GetId() || productId_ != other.GetProductId() || + subProductId_ != other.GetSubProductId() || imageType_ != other.GetImageType() || + specName_ != other.GetSpecName() || version_ != other.GetVersion() || wiseVersion_ != other.GetWiseVersion() || + url_ != other.GetUrl() || icon_ != other.GetIcon()); if (isNotEqual) { return true; } else { @@ -130,6 +169,11 @@ std::string DeviceIconInfo::dump() const cJSON_AddStringToObject(json, SUB_PRODUCT_ID.c_str(), subProductId_.c_str()); cJSON_AddStringToObject(json, IMAGE_TYPE.c_str(), imageType_.c_str()); cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_.c_str()); + cJSON_AddStringToObject(json, SPEC_NAME.c_str(), specName_.c_str()); + cJSON_AddStringToObject(json, DEVICE_ICON_VERSION.c_str(), version_.c_str()); + cJSON_AddStringToObject(json, DEVICE_ICON_WISE_VERSION.c_str(), wiseVersion_.c_str()); + cJSON_AddStringToObject(json, DEVICE_ICON_URL.c_str(), url_.c_str()); + cJSON_AddNumberToObject(json, DEVICE_ICON_SIZE.c_str(), icon_.size()); char* jsonChars = cJSON_PrintUnformatted(json); if (jsonChars == NULL) { cJSON_Delete(json); diff --git a/common/src/interfaces/product_info.cpp b/common/src/interfaces/product_info.cpp index c7b0a99f..f7024623 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -18,6 +18,7 @@ #include "cJSON.h" #include "distributed_device_profile_constants.h" #include "macro_utils.h" +#include "profile_utils.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -65,12 +66,12 @@ void ProductInfo::SetProductShortName(const std::string& productShortName) this->productShortName_ = productShortName; } -int32_t ProductInfo::GetImageVersion() const +std::string ProductInfo::GetImageVersion() const { return imageVersion_; } -void ProductInfo::SetImageVersion(const int32_t imageVersion) +void ProductInfo::SetImageVersion(const std::string& imageVersion) { this->imageVersion_ = imageVersion; } @@ -82,7 +83,7 @@ bool ProductInfo::Marshalling(MessageParcel& parcel) const WRITE_HELPER_RET(parcel, String, model_, false); WRITE_HELPER_RET(parcel, String, productName_, false); WRITE_HELPER_RET(parcel, String, productShortName_, false); - WRITE_HELPER_RET(parcel, Int32, imageVersion_, false); + WRITE_HELPER_RET(parcel, String, imageVersion_, false); return true; } @@ -92,7 +93,7 @@ bool ProductInfo::UnMarshalling(MessageParcel& parcel) READ_HELPER_RET(parcel, String, model_, false); READ_HELPER_RET(parcel, String, productName_, false); READ_HELPER_RET(parcel, String, productShortName_, false); - READ_HELPER_RET(parcel, Int32, imageVersion_, false); + READ_HELPER_RET(parcel, String, imageVersion_, false); return true; } @@ -116,9 +117,9 @@ std::string ProductInfo::dump() const } cJSON_AddStringToObject(json, PRODUCT_ID.c_str(), productId_.c_str()); cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), model_.c_str()); - cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), productName_.c_str()); - cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), productShortName_.c_str()); - cJSON_AddNumberToObject(json, IMAGE_VERSION.c_str(), imageVersion_); + cJSON_AddStringToObject(json, PRODUCT_NAME.c_str(), GetAnonyProductName(productName_).c_str()); + cJSON_AddStringToObject(json, PRODUCT_SHORT_NAME.c_str(), GetAnonyProductName(productShortName_).c_str()); + cJSON_AddStringToObject(json, IMAGE_VERSION.c_str(), imageVersion_.c_str()); char* jsonChars = cJSON_PrintUnformatted(json); if (jsonChars == NULL) { cJSON_Delete(json); @@ -130,5 +131,31 @@ std::string ProductInfo::dump() const cJSON_free(jsonChars); return jsonStr; } + +std::string ProductInfo::GetAnonyProductName(const std::string& productName) const +{ + cJSON* json = cJSON_Parse(productName.c_str()); + if (!cJSON_IsObject(json)) { + HILOGW("cJSON_Parse productName fail!"); + cJSON_Delete(json); + return EMPTY_STRING; + } + cJSON* item = json->child; + while (item != NULL) { + if (cJSON_IsString(item)) { + cJSON_SetValuestring(item, ProfileUtils::GetAnonyString(item->valuestring).c_str()); + } + item = item->next; + } + 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); + return jsonStr; +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 7341c286..3c064e26 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -66,17 +66,21 @@ productId TEXT,\ subProductId TEXT,\ imageType TEXT,\ specName TEXT,\ +version TEXT,\ +url TEXT,\ icon blob);"; const std::string CREATE_DEVICE_ICON_INFO_TABLE_UNIQUE_INDEX_SQL = "CREATE UNIQUE INDEX if not exists \ unique_device_icon_info ON device_icon_info (productId,subProductId,imageType,specName);"; -const std::string SELECT_DEVICE_ICON_INFO_TABLE = "SELECT * FROM device_profile WHERE "; +const std::string SELECT_DEVICE_ICON_INFO_TABLE = "SELECT a.*,b.imageVersion FROM device_icon_info a \ +LEFT JOIN product_info b ON a.productId = b.productId \ +WHERE "; // ProductInfoDao const std::string CREATE_PRODUCT_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS product_info \ (productId TEXT PRIMARY KEY,\ model TEXT,\ productName TEXT,\ productShortName TEXT,\ -imageVersion INTEGER);"; +imageVersion TEXT);"; const std::string SELECT_PRODUCT_INFO_TABLE = "SELECT * FROM product_info WHERE "; } // namespace DistributedDeviceProfile } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp index 0fb9f363..6439334b 100644 --- a/services/core/src/profiledatamanager/device_icon_info_dao.cpp +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -175,7 +175,7 @@ bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOpt sql = SELECT_DEVICE_ICON_INFO_TABLE; bool flag = false; if (!filterOptions.GetProductIds().empty()) { - sql += PRODUCT_ID + "IN("; + sql += "a." + PRODUCT_ID + " IN("; for (const auto& prodId : filterOptions.GetProductIds()) { sql += "?,"; condition.emplace_back(ValueObject(prodId)); @@ -185,17 +185,17 @@ bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOpt flag = true; } if (!filterOptions.GetSubProductId().empty()) { - sql += SUB_PRODUCT_ID + " = ? AND "; + sql += "a." + SUB_PRODUCT_ID + " = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetSubProductId())); flag = true; } if (!filterOptions.GetImageType().empty()) { - sql += IMAGE_TYPE + " = ? AND "; + sql += "a." + IMAGE_TYPE + " = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetImageType())); flag = true; } if (!filterOptions.GetSpecName().empty()) { - sql += SPEC_NAME + " = ? AND "; + sql += "a." + SPEC_NAME + " = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetSpecName())); flag = true; } @@ -212,7 +212,9 @@ int32_t DeviceIconInfoDao::DeviceIconInfoToEntries(const DeviceIconInfo& deviceI values.PutString(SUB_PRODUCT_ID, deviceIconInfo.GetSubProductId()); values.PutString(IMAGE_TYPE, deviceIconInfo.GetImageType()); values.PutString(SPEC_NAME, deviceIconInfo.GetSpecName()); - values.PutBlob(ICON, deviceIconInfo.GetIcon()); + values.PutString(DEVICE_ICON_VERSION, deviceIconInfo.GetVersion()); + values.PutString(DEVICE_ICON_URL, deviceIconInfo.GetUrl()); + values.PutBlob(DEVICE_ICON, deviceIconInfo.GetIcon()); return DP_SUCCESS; } @@ -227,14 +229,18 @@ int32_t DeviceIconInfoDao::ConvertToDeviceIconInfo(std::shared_ptr re HILOGE("get resultSet failed"); return DP_GET_RESULTSET_FAIL; } + deviceIconInfo.SetId(rowEntity.Get(ID)); deviceIconInfo.SetProductId(rowEntity.Get(PRODUCT_ID)); deviceIconInfo.SetSubProductId(rowEntity.Get(SUB_PRODUCT_ID)); deviceIconInfo.SetImageType(rowEntity.Get(IMAGE_TYPE)); deviceIconInfo.SetSpecName(rowEntity.Get(SPEC_NAME)); + deviceIconInfo.SetVersion(rowEntity.Get(DEVICE_ICON_VERSION)); + deviceIconInfo.SetWiseVersion(rowEntity.Get(IMAGE_VERSION)); + deviceIconInfo.SetUrl(rowEntity.Get(DEVICE_ICON_URL)); // std::vector icon; - // rowEntity.Get(ICON).GetBlob(icon); + // rowEntity.Get(DEVICE_ICON).GetBlob(icon); // deviceIconInfo.SetIcon(icon); - deviceIconInfo.SetIcon(rowEntity.Get(ICON)); + deviceIconInfo.SetIcon(rowEntity.Get(DEVICE_ICON)); return DP_SUCCESS; } } // namespace DistributedDeviceProfile diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp index f9b48ce4..6ae2c049 100644 --- a/services/core/src/profiledatamanager/product_info_dao.cpp +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -169,7 +169,7 @@ bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& sql = SELECT_PRODUCT_INFO_TABLE; bool flag = false; if (!productIds.empty()) { - sql += PRODUCT_ID + "IN("; + sql += PRODUCT_ID + " IN("; for (const auto& prodId : productIds) { sql += "?,"; condition.emplace_back(ValueObject(prodId)); @@ -187,7 +187,7 @@ int32_t ProductInfoDao::ProductInfoToEntries(const ProductInfo& productInfo, Val values.PutString(DEVICE_MODEL, productInfo.GetModel()); values.PutString(PRODUCT_NAME, productInfo.GetProductName()); values.PutString(PRODUCT_SHORT_NAME, productInfo.GetProductShortName()); - values.PutInt(IMAGE_VERSION, productInfo.GetImageVersion()); + values.PutString(IMAGE_VERSION, productInfo.GetImageVersion()); return DP_SUCCESS; } -- Gitee From 7bd2dbce2a5ed0a8a1b7038aeb2748258b458a99 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 08:19:35 +0000 Subject: [PATCH 32/55] characteristicProfileDao Signed-off-by: torrizo --- .../interfaces/characteristic_profile_filter_option.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/include/interfaces/characteristic_profile_filter_option.h b/common/include/interfaces/characteristic_profile_filter_option.h index b4b4c08a..59b42ac7 100644 --- a/common/include/interfaces/characteristic_profile_filter_option.h +++ b/common/include/interfaces/characteristic_profile_filter_option.h @@ -26,10 +26,10 @@ namespace OHOS { namespace DistributedDeviceProfile { class CharacteristicProfileFilterOption : public DpParcel { public: - CharacteristicProfileFilterOption(const int32_t& userId, const std::string& accountld, + 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, - std::vector& characteristicProfileIds, const int32_t& serviceProfileId) + const std::vector& characteristicProfileIds, const int32_t serviceProfileId) : userId_(userId), accountld_(accountld), deviceId_(deviceId), @@ -70,7 +70,7 @@ private: std::string serviceId_ = ""; std::string characteristicKey_ = ""; std::vector characteristicProfileIds_; - int32_t serviceProfileId_ = -1; + int32_t serviceProfileId_ = DEFAULT_SERVICE_PROFILE_ID; }; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 70784914362a5fbe3c69b22595b8fa2507c7997d Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 08:25:22 +0000 Subject: [PATCH 33/55] characteristicProfileDao Signed-off-by: torrizo --- .../include/interfaces/characteristic_profile.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 8629b2c1..62900043 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -42,15 +42,15 @@ public: isMultiUser_(isMultiUser), userId_(userId) {} - CharacteristicProfile(const std::string& deviceId, + CharacteristicProfile(const std::string& deviceId, cosnt std::string& serviceName, const std::string& characteristicKey, const std::string& characteristicValue, - const int32_t id, const int32_t serviceProfileId, const std::string& serviceId) + const int32_t id, const int32_t serviceProfileId) : deviceId_(deviceId), + serviceName_(serviceName), characteristicKey_(characteristicKey), characteristicValue_(characteristicValue), id_(id), - serviceProfileId_(serviceProfileId), - serviceId_(serviceId) + serviceProfileId_(serviceProfileId) {} CharacteristicProfile() = default; ~CharacteristicProfile() = default; @@ -71,8 +71,6 @@ public: 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; @@ -85,9 +83,8 @@ private: std::string characteristicValue_ = ""; bool isMultiUser_ = false; int32_t userId_ = DEFAULT_USER_ID; - int32_t id_ = -1; - int32_t serviceProfileId_ = -1; - std::string serviceId_ = ""; + int32_t id_ = CHANGEROWCNT_INIT_ID; + int32_t serviceProfileId_ = DEFAULT_SERVICE_PROFILE_ID; }; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 0c45407e89a13a71c5f5a005ccc8c5b9c1a705ad Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 08:26:11 +0000 Subject: [PATCH 34/55] characteristicProfileDao Signed-off-by: torrizo --- common/include/constants/distributed_device_profile_constants.h | 1 + 1 file changed, 1 insertion(+) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 0123c919..1e0802c2 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -223,6 +223,7 @@ constexpr int32_t ROWCNT_INIT = -1; constexpr int32_t RET_INIT = -1; constexpr int32_t ROWCOUNT_INIT = -1; constexpr int32_t CHANGEROWCNT_INIT = -1; +constexpr int32_t CHANGEROWCNT_INIT_ID = -1; constexpr int32_t COLUMNINDEX_INIT = -1; constexpr int32_t STATUS_INIT = 0; constexpr int32_t STATUS_ACTIVE = 1; -- Gitee From 10c5872fb895823a8e48899bf6b6a426990aad5c Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 08:27:24 +0000 Subject: [PATCH 35/55] characteristicProfileDao Signed-off-by: torrizo --- common/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/common/BUILD.gn b/common/BUILD.gn index 623332a7..3ae604c7 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -50,6 +50,7 @@ ohos_shared_library("distributed_device_profile_common") { "src/interfaces/accessee.cpp", "src/interfaces/accesser.cpp", "src/interfaces/characteristic_profile.cpp", + "src/interfaces/characteristic_profile_filter_option.cpp", "src/interfaces/device_icon_info_filter_options.cpp", "src/interfaces/device_icon_info.cpp", "src/interfaces/device_profile.cpp", -- Gitee From 6978fb1739be01618bba5e295dd65f13a020a0ca Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 08:28:26 +0000 Subject: [PATCH 36/55] characteristicProfileDao Signed-off-by: torrizo --- services/core/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 609dbed8..b792c597 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -91,6 +91,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/persistenceadapter/kvadapter/switch_adapter.cpp", "src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp", "src/persistenceadapter/rdbadapter/rdb_adapter.cpp", + "src/profiledatamanager/characteristic_profile_dao.cpp", "src/profiledatamanager/device_icon_info_dao.cpp", "src/profiledatamanager/device_profile_dao.cpp", "src/profiledatamanager/product_info_dao.cpp", -- Gitee From bbbc58862236995966cacd4ac894bb2e5f70eb64 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 08:43:51 +0000 Subject: [PATCH 37/55] characteristicProfileDao Signed-off-by: torrizo --- common/src/interfaces/characteristic_profile.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index 0e776db2..1e4de62a 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -104,16 +104,6 @@ void CharacteristicProfile::SetServiceProfileId(int32_t serviceProfileId) serviceProfileId_ = serviceProfileId; } -std::string CharacteristicProfile::GetServiceId() const -{ - return serviceId_; -} - -void CharacteristicProfile::SetServiceId(const std::string& serviceId) -{ - serviceId_ = serviceId; -} - bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, deviceId_, false); @@ -122,6 +112,8 @@ 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); return true; } @@ -133,6 +125,8 @@ 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); return true; } -- Gitee From 359c899ee70d744ea8260a7d55125a14b7a69c42 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Tue, 17 Dec 2024 16:50:36 +0800 Subject: [PATCH 38/55] cleancode update Signed-off-by: zhanglei --- common/include/interfaces/device_profile.h | 2 +- common/src/interfaces/device_profile.cpp | 22 ++++++++++++++----- .../rdbadapter/profile_data_rdb_adapter.cpp | 6 +++-- .../profiledatamanager/device_profile_dao.cpp | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/common/include/interfaces/device_profile.h b/common/include/interfaces/device_profile.h index 1443d22c..cdf74664 100644 --- a/common/include/interfaces/device_profile.h +++ b/common/include/interfaces/device_profile.h @@ -163,4 +163,4 @@ private: }; } // namespace DistributedDeviceProfile } // namespace OHOS -#endif //OHOS_DP_DEVICE_PROFILE_H +#endif // OHOS_DP_DEVICE_PROFILE_H diff --git a/common/src/interfaces/device_profile.cpp b/common/src/interfaces/device_profile.cpp index 1b8c571c..2fcd86c4 100644 --- a/common/src/interfaces/device_profile.cpp +++ b/common/src/interfaces/device_profile.cpp @@ -408,12 +408,22 @@ bool DeviceProfile::UnMarshalling(MessageParcel& parcel) bool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const { - bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || - deviceName_ != deviceProfile.GetDeviceName() || manufactureName_ != deviceProfile.GetManufactureName() || - deviceModel_ != deviceProfile.GetDeviceModel() || storageCapability_ != deviceProfile.GetStorageCapability() || - osSysCap_ != deviceProfile.GetOsSysCap() || osApiLevel_ != deviceProfile.GetOsApiLevel() || - osVersion_ != deviceProfile.GetOsVersion() || osType_ != deviceProfile.GetOsType() || - isMultiUser_ != deviceProfile.IsMultiUser() || userId_ != deviceProfile.GetUserId()); + bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || deviceName_ != deviceProfile.GetDeviceName() || + manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() || + storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() || + osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || + osType_ != deviceProfile.GetOsType() || isMultiUser_ != deviceProfile.IsMultiUser() || + userId_ != deviceProfile.GetUserId() || devType_ != deviceProfile.GetDevType() || + manu_ != deviceProfile.GetManu() || sn_ != deviceProfile.GetSn() || + productId_ != deviceProfile.GetProductId() || subProductId_ != deviceProfile.GetSubProductId() || + hiv_ != deviceProfile.GetHiv() || mac_ != deviceProfile.GetMac() || + fwv_ != deviceProfile.GetFwv() || hwv_ != deviceProfile.GetHwv() || + swv_ != deviceProfile.GetSwv() || protType_ != deviceProfile.GetProtType() || + wiseUserId_ != deviceProfile.GetWiseUserId() || wiseDeviceId_ != deviceProfile.GetWiseDeviceId() || + roomName_ != deviceProfile.GetRoomName() || registerTime_ != deviceProfile.GetRegisterTime() || + modifyTime_ != deviceProfile.GetModifyTime() || shareTime_ != deviceProfile.GetShareTime() || + productorInfoVersion_ != deviceProfile.GetProductorInfoVersion() || + accountId_ != deviceProfile.GetAccountId() || id_ != deviceProfile.GetId()); if (isNotEqual) { return true; } else { diff --git a/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp b/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp index 1bbcb056..33c322dc 100644 --- a/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp +++ b/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp @@ -13,15 +13,17 @@ * limitations under the License. */ +#include "profile_data_rdb_adapter.h" + #include #include #include -#include "profile_data_rdb_adapter.h" -#include "rdb_errno.h" + #include "distributed_device_profile_constants.h" #include "dp_services_constants.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" +#include "rdb_errno.h" namespace OHOS { namespace DistributedDeviceProfile { diff --git a/services/core/src/profiledatamanager/device_profile_dao.cpp b/services/core/src/profiledatamanager/device_profile_dao.cpp index 3394f127..cca83ee4 100644 --- a/services/core/src/profiledatamanager/device_profile_dao.cpp +++ b/services/core/src/profiledatamanager/device_profile_dao.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ -#include "content_sensor_manager_utils.h" #include "device_profile_dao.h" +#include "content_sensor_manager_utils.h" #include "distributed_device_profile_constants.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" @@ -96,7 +96,7 @@ int32_t DeviceProfileDao::PutDeviceProfile(const DeviceProfile& deviceProfile) resultSet->Close(); SubscribeProfileManager::GetInstance().NotifyDeviceProfileAdd(profile); std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); - if (localUdid == deviceProfile.GetDeviceId() ) { + if (localUdid == deviceProfile.GetDeviceId()) { DeviceProfileManager::GetInstance().PutDeviceProfile(profile); } HILOGI("end!"); -- Gitee From 36ab02f16019c2a1b8e594b90d41521a0314244c Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Tue, 17 Dec 2024 17:37:44 +0800 Subject: [PATCH 39/55] service_profile_dao update Signed-off-by: wangzhaohao --- .../interfaces/service_profile_filter_opotions.h | 4 ++-- services/core/include/common/dp_services_constants.h | 5 +++-- services/core/src/common/dp_services_constants.cpp | 11 +++++++---- .../src/profiledatamanager/service_profile_dao.cpp | 12 +++++++++--- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/common/include/interfaces/service_profile_filter_opotions.h b/common/include/interfaces/service_profile_filter_opotions.h index 5a124934..56d6ec2c 100644 --- a/common/include/interfaces/service_profile_filter_opotions.h +++ b/common/include/interfaces/service_profile_filter_opotions.h @@ -43,14 +43,14 @@ public: void SetWiseDeviceIds(const std::vector& wiseDeviceIds); const std::vector& GetServiceIds() const; void SetServiceIds(const std::vector& serviceIds); - const std::vector& GetServiceProfileIds() const; + const std::vector& GetServiceProfileIds() const; void SetServiceProfileIds(const std::vector& serviceProfileIds); const std::vector& GetDeviceProfileIds() const; void SetDeviceProfileIds(const std::vector& deviceProfileIds); bool IsEmpty() const; bool Marshalling(MessageParcel& parcel) const override; - bool UnMarshalling(MessageParcel& parcel) override; + bool UnMarshalling(MessageParcel& parcel) override; bool operator==(const ServiceProfileFilterOptions& rhs) const; diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index bd17decc..0823f4e8 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -39,9 +39,10 @@ extern const std::string CREATE_PRODUCT_INFO_TABLE_SQL; extern const std::string SELECT_PRODUCT_INFO_TABLE; /* ServiceProfileDao */ extern const std::string SELECT_SERVICE_PROGILES; -extern const std::string SELECT_SERVICE_PROFILE_TABLE_MAX_ID; extern const std::string CREATE_SERVICE_PROFILE_TABLE_SQL; -extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL; +extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SERVICEID_SQL; +extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_DEVICEPROFILEID_SQL; +extern const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_SERVICES_CONSTANTS_H diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 33d57ffc..093c039d 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -80,9 +80,8 @@ imageVersion INTEGER);"; const std::string SELECT_PRODUCT_INFO_TABLE = "SELECT * FROM product_info WHERE "; /* ServiceProfileDao */ const std::string SELECT_SERVICE_PROGILES = - "SELECT sp.*, dp.udid, dp.userId FROM service_profile sp \ + "SELECT sp.*, dp.deviceId, dp.userId FROM service_profile sp \ LEFT JOIN device_profile dp ON dp.id=sp.deviceProfileId WHERE "; -const std::string SELECT_SERVICE_PROFILE_TABLE_MAX_ID = "SELECT MAX(id) FROM service_profile"; const std::string CREATE_SERVICE_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS service_profile (\n" " id INTEGER PRIMARY KEY AUTOINCREMENT,\n" @@ -94,7 +93,11 @@ const std::string CREATE_SERVICE_PROFILE_TABLE_SQL = " )\n" " REFERENCES device_profile (id)\n" ")"; -const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL = -"CREATE INDEX if not exists serviceId_idx ON service_profile (serviceId)"; +const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SERVICEID_SQL = +"CREATE INDEX if not exists serviceId_idx ON service_profile (serviceId)"; +const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_DEVICEPROFILEID_SQL = +"CREATE INDEX if not exists deviceProfileId_idx ON service_profile (deviceProfileId)"; +const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE = +"SELECT * FROM service_profile WHERE deviceProfileId = ? AND serviceId = ? AND serviceType = ?"; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/src/profiledatamanager/service_profile_dao.cpp b/services/core/src/profiledatamanager/service_profile_dao.cpp index 0c7cb2d1..676cade4 100644 --- a/services/core/src/profiledatamanager/service_profile_dao.cpp +++ b/services/core/src/profiledatamanager/service_profile_dao.cpp @@ -187,9 +187,14 @@ int32_t ServiceProfileDao::CreateTable() int32_t ServiceProfileDao::CreateIndex() { - int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL); + int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_SERVICE_PROFILE_TABLE_INDEX_SERVICEID_SQL); if (ret != DP_SUCCESS) { - HILOGE("service_profile unique index create failed"); + HILOGE("service_profile index create failed"); + return DP_CREATE_UNIQUE_INDEX_FAIL; + } + ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_SERVICE_PROFILE_TABLE_INDEX_DEVICEPROFILEID_SQL); + if (ret != DP_SUCCESS) { + HILOGE("service_profile index create failed"); return DP_CREATE_UNIQUE_INDEX_FAIL; } return DP_SUCCESS; @@ -311,7 +316,8 @@ void ServiceProfileDao::ServiceProfileToEntries(const ServiceProfile& servicePro int32_t ServiceProfileDao::SetServiceProfileId(ServiceProfile& serviceProfile) { std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get( - SELECT_SERVICE_PROFILE_TABLE_MAX_ID, std::vector{}); + SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE, std::vector + {serviceProfile.GetDeviceProfileId(), serviceProfile.GetServiceName(), serviceProfile.GetServiceType()}); if (resultSet == nullptr) { HILOGE("resultSet is nullptr"); return DP_GET_RESULTSET_FAIL; -- Gitee From 811ed37137b597f869dde243c5bb459a650481d0 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Tue, 17 Dec 2024 17:42:32 +0800 Subject: [PATCH 40/55] service_profile_dao update Signed-off-by: wangzhaohao --- services/core/include/common/dp_services_constants.h | 1 - services/core/src/common/dp_services_constants.cpp | 2 -- 2 files changed, 3 deletions(-) diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index 8b87adbb..11a06b03 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -45,7 +45,6 @@ extern const std::string CREATE_SERVICE_PROFILE_TABLE_SQL; extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SERVICEID_SQL; extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_DEVICEPROFILEID_SQL; extern const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE; -extern const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL; // CharacteristicProfileDao extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE; extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL; diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index ff11b33a..a0657dca 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -103,8 +103,6 @@ const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_DEVICEPROFILEID_SQL = "CREATE INDEX if not exists deviceProfileId_idx ON service_profile (deviceProfileId)"; const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE = "SELECT * FROM service_profile WHERE deviceProfileId = ? AND serviceId = ? AND serviceType = ?"; -const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_SQL = -"CREATE INDEX if not exists serviceId_idx ON service_profile (serviceId)"; // CharateristicProfileDao const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID = "SELECT MAX(id) FROM characteristic_profile"; const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE = "SELECT * FROM characteristic_profile WHERE "; -- Gitee From 725f849ffbddd559166af2dcfa7991cc4f583fc0 Mon Sep 17 00:00:00 2001 From: zhanglei Date: Tue, 17 Dec 2024 19:11:16 +0800 Subject: [PATCH 41/55] update deviceprofile uniqueIndex Signed-off-by: zhanglei --- services/core/src/common/dp_services_constants.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index a0657dca..4ac3f6e3 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -58,7 +58,8 @@ const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL = (\ deviceId,\ userId,\ - accountId);"; + accountId,\ + productId);"; // DeviceIconInfoDao const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_icon_info\ (id INTEGER PRIMARY KEY AUTOINCREMENT,\ -- Gitee From bd0a2fa5cb3e4d6e96f0e414a509bd76ff2cd52e Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 13:02:12 +0000 Subject: [PATCH 42/55] characteristicProfileDao Signed-off-by: torrizo --- common/src/interfaces/characteristic_profile.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index 1e4de62a..a792dc28 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -64,6 +64,17 @@ void CharacteristicProfile::SetCharacteristicValue(const std::string& characteri characteristicValue_ = characteristicValue; } +std::string CharacteristicProfile::GetCharacteristicValue() const +{ + return serviceId_; +} + +void CharacteristicProfile::SetCharacteristicValue(const std::string& serviceId) +{ + serviceId_ = serviceId; + serviceName_ = serviceId; +} + bool CharacteristicProfile::IsMultiUser() const { return isMultiUser_; @@ -114,6 +125,7 @@ bool CharacteristicProfile::Marshalling(MessageParcel& parcel) const 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; } @@ -127,6 +139,7 @@ bool CharacteristicProfile::UnMarshalling(MessageParcel& parcel) 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; } -- Gitee From d397d2d9d47f6bd15e9f0e217950c32cd624526a Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 13:07:37 +0000 Subject: [PATCH 43/55] characteristicProfileDao Signed-off-by: torrizo --- common/include/interfaces/characteristic_profile.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 62900043..8d650911 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -42,15 +42,15 @@ public: isMultiUser_(isMultiUser), userId_(userId) {} - CharacteristicProfile(const std::string& deviceId, cosnt std::string& serviceName, + CharacteristicProfile(const std::string& deviceId, const std::string& characteristicKey, const std::string& characteristicValue, - const int32_t id, const int32_t serviceProfileId) + const int32_t id, const int32_t serviceProfileId, const std::string& serviceId) : deviceId_(deviceId), - serviceName_(serviceName), characteristicKey_(characteristicKey), characteristicValue_(characteristicValue), id_(id), serviceProfileId_(serviceProfileId) + serviceId_(serviceId) {} CharacteristicProfile() = default; ~CharacteristicProfile() = default; @@ -85,6 +85,7 @@ private: 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 -- Gitee From 5d293fa00ff6beee7ca5cd6355a81b68c62c947d Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 13:09:25 +0000 Subject: [PATCH 44/55] characteristicProfileDao Signed-off-by: torrizo --- common/src/interfaces/characteristic_profile.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index a792dc28..6977d4cb 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -64,15 +64,14 @@ void CharacteristicProfile::SetCharacteristicValue(const std::string& characteri characteristicValue_ = characteristicValue; } -std::string CharacteristicProfile::GetCharacteristicValue() const +std::string CharacteristicProfile::GetServiceIde() const { return serviceId_; } -void CharacteristicProfile::SetCharacteristicValue(const std::string& serviceId) +void CharacteristicProfile::SetServiceId(const std::string& serviceId) { serviceId_ = serviceId; - serviceName_ = serviceId; } bool CharacteristicProfile::IsMultiUser() const -- Gitee From b84538653ac4650c6f89acc1276ff4f95397f437 Mon Sep 17 00:00:00 2001 From: torrizo Date: Tue, 17 Dec 2024 13:10:57 +0000 Subject: [PATCH 45/55] characteristicProfileDao Signed-off-by: torrizo --- common/include/interfaces/characteristic_profile.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 8d650911..55b1b784 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 @@ -71,6 +71,8 @@ public: 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; -- Gitee From 2885f4690b37059615d606b5230f84b5d4fb5347 Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:18:45 +0000 Subject: [PATCH 46/55] characteristicProfileDao Signed-off-by: torrizo --- .../characteristic_profile_dao.cpp | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp index b5058e94..eba1972d 100644 --- a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp +++ b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * 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 @@ -66,7 +66,11 @@ int32_t CharacteristicProfileDao::UnInit() int32_t CharacteristicProfileDao::PutCharacteristicProfile(CharacteristicProfile& charProfile) { ValuesBucket values; - CharProfileToEntries(charProfile, 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); @@ -127,6 +131,12 @@ int32_t CharacteristicProfileDao::GetCharacteristicProfiles(const Characteristic int32_t CharacteristicProfileDao::DeleteCharacteristicProfile(const CharacteristicProfile &charProfiles) { + if (charProfiles.GetDeviceId().empty() || charProfiles.GetDeviceId().empty() || + charProfiles.GetServiceName().empty() || charProfiles.GetCharacteristicKey() || + charProfiles.GetCharacteristicValue()) { + 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()) }); @@ -148,7 +158,11 @@ int32_t CharacteristicProfileDao::UpdateCharacteristicProfile(const Characterist const CharacteristicProfile &newProfile) { ValuesBucket values; - CharProfileToEntries(newProfile, 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, @@ -183,6 +197,11 @@ int32_t CharacteristicProfileDao::CreateIndex() 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; } @@ -191,7 +210,7 @@ void CharacteristicProfileDao::CreateQuerySqlAndCondition(const CharacteristicPr { sql = SELECT_CHARACTERISTIC_PROFILE_TABLE; if (!filterOptions.GetCharacteristicProfileIds().empty()) { - sql += "characteristic_profile.id IN("; + sql += "cp.id IN("; std::vector characteristicProfileIds = filterOptions.GetCharacteristicProfileIds(); for (auto deviceProfileId : characteristicProfileIds) { sql += "?,"; @@ -201,31 +220,31 @@ void CharacteristicProfileDao::CreateQuerySqlAndCondition(const CharacteristicPr sql += ") AND "; } if (!filterOptions.GetCharacteristicKey().empty()) { - sql += "characteristic_profile.characteristicKey = ? AND "; + sql += "cp.characteristicKey = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetCharacteristicKey())); } if (filterOptions.GetServiceProfileId() != 0) { - sql += "characteristic_profile.serviceProfileId = ? AND "; + sql += "cp.serviceProfileId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetServiceProfileId())); } if (!filterOptions.GetServiceId().empty()) { - sql += "service_profile.serviceId = ? AND "; + sql += "sp.serviceId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetServiceId())); } if (!filterOptions.GetDeviceIds().empty()) { - sql += "service_profile.deviceId = ? AND "; + sql += "sp.deviceId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetDeviceIds())); } if (filterOptions.GetUserId() != 0) { - sql += "device_profile.deviceId = ? AND "; + sql += "dp.deviceId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetUserId())); } if (!filterOptions.GetAccountld().empty()) { - sql += "device_profile.deviceId = ? AND "; + sql += "dp.deviceId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetAccountld())); } if (!filterOptions.GetWiseDeviceId().empty()) { - sql += "device_profile.deviceId = ? AND "; + sql += "dp.deviceId = ? AND "; condition.emplace_back(ValueObject(filterOptions.GetWiseDeviceId())); } if (sql.empty()) { @@ -237,7 +256,11 @@ void CharacteristicProfileDao::CreateQuerySqlAndCondition(const CharacteristicPr int32_t CharacteristicProfileDao::CharProfileToEntries(const CharacteristicProfile& charProfile, ValuesBucket& values) { - values.PutInt(ID, charProfile.GetId()); + if (charProfile.GetDeviceId().empty() || charProfile.GetDeviceId().empty() || + charProfile.GetServiceName().empty() || charProfile.GetCharacteristicKey() || + charProfile.GetCharacteristicValue()) { + return DP_INVALID_PARAMS; + } values.PutInt(SERVICE_PROFILE_ID, charProfile.GetServiceProfileId()); values.PutString(CHARACTERISTIC_KEY, charProfile.GetCharacteristicKey()); values.PutString(CHARACTERISTIC_VALUE, charProfile.GetCharacteristicValue()); @@ -268,7 +291,8 @@ int32_t CharacteristicProfileDao::ConvertToCharProfile( int32_t CharacteristicProfileDao::SetCharacteristicProfileId(CharacteristicProfile& charProfile) { std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get( - SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID, std::vector{}); + 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; -- Gitee From 2a154635b2d7731e05a3aa9f0f74b3922de30be5 Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:19:39 +0000 Subject: [PATCH 47/55] characteristicProfileDao Signed-off-by: torrizo --- .../core/src/common/dp_services_constants.cpp | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 4ac3f6e3..7801b006 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -106,18 +106,21 @@ const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE = "SELECT * FROM service_profile WHERE deviceProfileId = ? AND serviceId = ? AND serviceType = ?"; // CharateristicProfileDao const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID = "SELECT MAX(id) FROM characteristic_profile"; -const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE = "SELECT * FROM characteristic_profile WHERE "; -const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS characteristic_profile\ -(\ - id INTEGER PRIMARY KEY AUTOINCREMENT,\ - serviceProfileId INTEGER NOT NULL,\ - characteristicKey TEXT,\ - characteristicValue TEXT,\ - FOREIGN KEY (\ - serviceProfileId\ - )\ - REFERENCES service_profile (id));"; -const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL = +const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE = "SELECT cp.*,sp.serviceId,dp.deviceId,dp.userId\ + from characteristic_profile cp\ + left join service_profile sp on cp.serviceProfileId=sp.id\ + left join device_profile dp on dp.id=sp.deviceProfileId WHERE "; +const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS characteristic_profile ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + serviceProfileId INTEGER NOT NULL,\ + characteristicKey TEXT NOT NULL,\ + characteristicValue TEXT NOT NULL,\ + FOREIGN KEY (serviceProfileId)\ + REFERENCES service_profile (id)\ +);"; +const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_CHARACTERISTIC_KEY = "CREATE INDEX characteristicKey_idx ON characteristic_profile (characteristicKey);"; +const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_SERVICE_PROFILE_ID = + "CREATE INDEX IF NOT EXISTS index_serviceProfileId ON characteristic_profile (serviceProfileId);" } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 89e8232423261bc3a73bd3abcfe9295abe5aa2a5 Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:20:35 +0000 Subject: [PATCH 48/55] characteristicProfileDao Signed-off-by: torrizo --- services/core/include/common/dp_services_constants.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index 11a06b03..6760979b 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -48,7 +48,8 @@ extern const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE; // CharacteristicProfileDao extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE; extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL; -extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL; +extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_CHARACTERISTIC_KEY; +extern const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_SERVICE_PROFILE_ID; extern const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 288319be82330478e72efb629b6701a38ffd779c Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:21:27 +0000 Subject: [PATCH 49/55] characteristicProfileDao Signed-off-by: torrizo --- services/core/src/common/dp_services_constants.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 7801b006..31dd29ce 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -110,8 +110,8 @@ const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE = "SELECT cp.*,sp.serviceI from characteristic_profile cp\ left join service_profile sp on cp.serviceProfileId=sp.id\ left join device_profile dp on dp.id=sp.deviceProfileId WHERE "; -const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS characteristic_profile ( - id INTEGER PRIMARY KEY AUTOINCREMENT, +const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS characteristic_profile (\ + id INTEGER PRIMARY KEY AUTOINCREMENT,\ serviceProfileId INTEGER NOT NULL,\ characteristicKey TEXT NOT NULL,\ characteristicValue TEXT NOT NULL,\ @@ -121,6 +121,6 @@ const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_CHARACTERISTIC_KEY = "CREATE INDEX characteristicKey_idx ON characteristic_profile (characteristicKey);"; const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_SERVICE_PROFILE_ID = - "CREATE INDEX IF NOT EXISTS index_serviceProfileId ON characteristic_profile (serviceProfileId);" + "CREATE INDEX IF NOT EXISTS index_serviceProfileId ON characteristic_profile (serviceProfileId);"; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 0a0acb54eba365f2e4d699246bb4a2518617fc32 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Wed, 18 Dec 2024 10:28:29 +0800 Subject: [PATCH 50/55] add class ProfileDataManager Signed-off-by: wangzhaohao --- services/core/BUILD.gn | 1 + .../profiledatamanager/profile_data_manager.h | 33 +++++++++++++++++++ .../profile_data_manager.cpp | 30 +++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 services/core/include/profiledatamanager/profile_data_manager.h create mode 100644 services/core/src/profiledatamanager/profile_data_manager.cpp diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index b792c597..c399b07c 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -95,6 +95,7 @@ ohos_shared_library("distributed_device_profile_svr") { "src/profiledatamanager/device_icon_info_dao.cpp", "src/profiledatamanager/device_profile_dao.cpp", "src/profiledatamanager/product_info_dao.cpp", + "src/profiledatamanager/profile_data_manager.cpp", "src/profiledatamanager/service_profile_dao.cpp", "src/publishcommonevent/dp_account_common_event.cpp", "src/staticcapabilitycollector/static_capability_collector.cpp", diff --git a/services/core/include/profiledatamanager/profile_data_manager.h b/services/core/include/profiledatamanager/profile_data_manager.h new file mode 100644 index 00000000..cbbf2eec --- /dev/null +++ b/services/core/include/profiledatamanager/profile_data_manager.h @@ -0,0 +1,33 @@ +/* + * 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_PROFILE_DATA_MANAGER_H +#define OHOS_DP_PROFILE_DATA_MANAGER_H + +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class ProfileDataManager { + DECLARE_SINGLE_INSTANCE(ProfileDataManager); + +public: + +private: + +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_PROFILE_DATA_MANAGER_H diff --git a/services/core/src/profiledatamanager/profile_data_manager.cpp b/services/core/src/profiledatamanager/profile_data_manager.cpp new file mode 100644 index 00000000..78f3be54 --- /dev/null +++ b/services/core/src/profiledatamanager/profile_data_manager.cpp @@ -0,0 +1,30 @@ +/* + * 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 "profile_data_manager.h" + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProfileDataManager); +namespace { + const std::string TAG = "ProfileDataManager"; +} + +} // namespace DistributedDeviceProfile +} // namespace OHOS -- Gitee From 935a3b15e42335ad60326c649da5825531a1693e Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:33:03 +0000 Subject: [PATCH 51/55] characteristicProfileDao Signed-off-by: torrizo --- common/include/interfaces/characteristic_profile.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/include/interfaces/characteristic_profile.h b/common/include/interfaces/characteristic_profile.h index 55b1b784..62f23383 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -49,7 +49,7 @@ public: characteristicKey_(characteristicKey), characteristicValue_(characteristicValue), id_(id), - serviceProfileId_(serviceProfileId) + serviceProfileId_(serviceProfileId), serviceId_(serviceId) {} CharacteristicProfile() = default; @@ -71,7 +71,7 @@ public: void SetId(int32_t id); int32_t GetServiceProfileId() const; void SetServiceProfileId(int32_t serviceProfileId); - std::string GetServiceId_() const; + std::string GetServiceId() const; void SetServiceId(const std::string& serviceId); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; @@ -87,7 +87,7 @@ private: int32_t userId_ = DEFAULT_USER_ID; int32_t id_ = CHANGEROWCNT_INIT_ID; int32_t serviceProfileId_ = DEFAULT_SERVICE_PROFILE_ID; - std::string serviceId_ = "" + std::string serviceId_ = ""; }; } // namespace DistributedDeviceProfile } // namespace OHOS -- Gitee From 8480d6e7d02be2c4486adc6d3680d91e5d528aed Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:34:55 +0000 Subject: [PATCH 52/55] characteristicProfileDao 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 6977d4cb..dc1fcd16 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -64,7 +64,7 @@ void CharacteristicProfile::SetCharacteristicValue(const std::string& characteri characteristicValue_ = characteristicValue; } -std::string CharacteristicProfile::GetServiceIde() const +std::string CharacteristicProfile::GetServiceId() const { return serviceId_; } -- Gitee From 3431d2b0859c445a7adb35a1d22201d33fbe5fe6 Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:52:54 +0000 Subject: [PATCH 53/55] characteristicProfileDao Signed-off-by: torrizo --- services/core/src/common/dp_services_constants.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 31dd29ce..85ee47d5 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -105,7 +105,9 @@ const std::string CREATE_SERVICE_PROFILE_TABLE_INDEX_DEVICEPROFILEID_SQL = const std::string SELECT_SERVICE_PROFILE_TABLE_WHERE_DEVID_SERID_SERTYPE = "SELECT * FROM service_profile WHERE deviceProfileId = ? AND serviceId = ? AND serviceType = ?"; // CharateristicProfileDao -const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID = "SELECT MAX(id) FROM characteristic_profile"; +const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE_MAX_ID = + "SELECT * FROM characteristic_profile WHERE serviceProfileId = ? AND characteristicKey = ? \ + AND characteristicValue = ?"; const std::string SELECT_CHARACTERISTIC_PROFILE_TABLE = "SELECT cp.*,sp.serviceId,dp.deviceId,dp.userId\ from characteristic_profile cp\ left join service_profile sp on cp.serviceProfileId=sp.id\ -- Gitee From fa87e9281c6bad5ccfb7d25fb5075b9c1a7b42d4 Mon Sep 17 00:00:00 2001 From: torrizo Date: Wed, 18 Dec 2024 02:59:51 +0000 Subject: [PATCH 54/55] characteristicProfileDao Signed-off-by: torrizo --- .../characteristic_profile_dao.cpp | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp index eba1972d..20bd7bf7 100644 --- a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp +++ b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp @@ -131,9 +131,9 @@ int32_t CharacteristicProfileDao::GetCharacteristicProfiles(const Characteristic int32_t CharacteristicProfileDao::DeleteCharacteristicProfile(const CharacteristicProfile &charProfiles) { - if (charProfiles.GetDeviceId().empty() || charProfiles.GetDeviceId().empty() || - charProfiles.GetServiceName().empty() || charProfiles.GetCharacteristicKey() || - charProfiles.GetCharacteristicValue()) { + if (charProfiles.GetDeviceId().empty() || charProfiles.GetServiceProfileId() <= 0 || + charProfiles.GetServiceId().empty() || charProfiles.GetCharacteristicKey().empty() || + charProfiles.GetCharacteristicValue().empty()) { HILOGE("charProfile invalid params"); return DP_INVALID_PARAMS; } @@ -192,12 +192,14 @@ int32_t CharacteristicProfileDao::CreateTable() int32_t CharacteristicProfileDao::CreateIndex() { - int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL); + 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); + 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; @@ -256,9 +258,9 @@ void CharacteristicProfileDao::CreateQuerySqlAndCondition(const CharacteristicPr int32_t CharacteristicProfileDao::CharProfileToEntries(const CharacteristicProfile& charProfile, ValuesBucket& values) { - if (charProfile.GetDeviceId().empty() || charProfile.GetDeviceId().empty() || - charProfile.GetServiceName().empty() || charProfile.GetCharacteristicKey() || - charProfile.GetCharacteristicValue()) { + 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()); @@ -291,8 +293,9 @@ int32_t CharacteristicProfileDao::ConvertToCharProfile( 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()}); + 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; -- Gitee From 2d33a08398b2e04bcf302e237376616982b3d180 Mon Sep 17 00:00:00 2001 From: wangzhaohao Date: Tue, 24 Dec 2024 14:08:56 +0800 Subject: [PATCH 55/55] ProfileDataManager add dp,sp curd. compile pass Signed-off-by: wangzhaohao --- .../distributed_device_profile_constants.h | 2 + .../distributed_device_profile_errors.h | 2 + .../interfaces/characteristic_profile.h | 14 +- .../device_profile_filter_options.h | 3 + common/include/interfaces/service_profile.h | 6 + .../service_profile_filter_opotions.h | 16 +- .../distributed_device_profile_constants.cpp | 2 + .../src/interfaces/characteristic_profile.cpp | 18 + .../device_profile_filter_options.cpp | 17 +- common/src/interfaces/service_profile.cpp | 10 + .../service_profile_filter_opotions.cpp | 50 ++- common/src/utils/ipc_utils.cpp | 4 +- .../characteristic_profile_dao.h | 7 +- .../profiledatamanager/device_profile_dao.h | 4 +- .../profiledatamanager/profile_data_manager.h | 23 +- .../profiledatamanager/service_profile_dao.h | 4 +- services/core/include/utils/profile_cache.h | 1 + .../characteristic_profile_dao.cpp | 3 +- .../profiledatamanager/device_profile_dao.cpp | 6 +- .../profile_data_manager.cpp | 400 +++++++++++++++++- .../service_profile_dao.cpp | 16 +- services/core/src/utils/profile_cache.cpp | 6 + 22 files changed, 563 insertions(+), 51 deletions(-) diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index 1e0802c2..3b017d6c 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -181,6 +181,7 @@ constexpr int32_t MAX_USER_ID = 100000; constexpr uint32_t MAX_TRUSTED_DEVICE_SIZE = 1000; extern const std::string IS_MULTI_USER; extern const std::string SEPARATOR; +extern const std::string SLASHES; extern const std::string DEV_PREFIX; extern const std::string SVR_PREFIX; extern const std::string CHAR_PREFIX; @@ -215,6 +216,7 @@ extern const std::string USERID; extern const std::string BUNDLENAME; extern const std::string TOKENID; extern const std::string ACCOUNTID; +extern const std::string PRODUCTID; extern const std::string DEVICEID_EQUAL_CONDITION; extern const std::string ACCESSCONTROLID_EQUAL_CONDITION; extern const std::string ACCESSERID_EQUAL_CONDITION; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 353bd240..d059a61e 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -172,6 +172,8 @@ constexpr int32_t DP_RDB_DELETE_SERVICE_PROFILE_FAIL = 98566292; constexpr int32_t DP_RDB_UPDATE_SERVICE_PROFILE_FAIL = 98566293; constexpr int32_t DP_RDB_GET_SERVICE_PROFILE_FAIL = 98566294; constexpr int32_t DP_RDB_SET_SERVICE_PROFILE_ID_FAIL = 98566295; +constexpr int32_t DP_DEVICE_PROFILE_NOT_FOUND = 98566296; +constexpr int32_t DP_SERVICE_PROFILE_NOT_FOUND = 98566297; } // 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 55b1b784..355fccab 100644 --- a/common/include/interfaces/characteristic_profile.h +++ b/common/include/interfaces/characteristic_profile.h @@ -52,6 +52,17 @@ public: 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; @@ -76,6 +87,7 @@ public: 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: @@ -87,7 +99,7 @@ private: int32_t userId_ = DEFAULT_USER_ID; int32_t id_ = CHANGEROWCNT_INIT_ID; int32_t serviceProfileId_ = DEFAULT_SERVICE_PROFILE_ID; - std::string serviceId_ = "" + std::string serviceId_ = ""; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/device_profile_filter_options.h b/common/include/interfaces/device_profile_filter_options.h index 1e5328c0..db28c64c 100644 --- a/common/include/interfaces/device_profile_filter_options.h +++ b/common/include/interfaces/device_profile_filter_options.h @@ -36,10 +36,13 @@ public: void SetAccountId(std::string accountId); std::vector GetDeviceIds() const; void SetDeviceIds(std::vector deviceIds); + void AddDeviceIds(std::string deviceId); std::vector GetWiseDeviceIds() const; void SetWiseDeviceIds(std::vector wiseDeviceIds); + void AddWiseDeviceIds(std::string wiseDeviceId); std::vector GetDeviceProfileIds() const; void SetDeviceProfileIds(std::vector deviceProfileIds); + void AddDeviceProfileIds(int32_t deviceProfileId); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; std::string dump() const override; diff --git a/common/include/interfaces/service_profile.h b/common/include/interfaces/service_profile.h index 6b4d9791..2fabbcb1 100644 --- a/common/include/interfaces/service_profile.h +++ b/common/include/interfaces/service_profile.h @@ -18,6 +18,9 @@ #include #include +#include + +#include "characteristic_profile.h" #include "distributed_device_profile_constants.h" #include "dp_parcel.h" @@ -47,6 +50,8 @@ public: void SetId(int32_t id); int32_t GetDeviceProfileId() const; void SetDeviceProfileId(int32_t deviceProfileId); + const std::vector& GetCharacteristicProfiles() const; + void SetCharacteristicProfiles(const std::vector& characteristicProfiles); bool Marshalling(MessageParcel& parcel) const override; bool UnMarshalling(MessageParcel& parcel) override; bool operator!=(const ServiceProfile& serviceProfile) const; @@ -60,6 +65,7 @@ private: int32_t userId_ = DEFAULT_USER_ID; int32_t id_ = DEFAULT_SERVICE_PROFILE_ID; int32_t deviceProfileId_ = DEFAULT_DEVICE_PROFILE_ID; + std::vector characteristicProfiles_; }; } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/include/interfaces/service_profile_filter_opotions.h b/common/include/interfaces/service_profile_filter_opotions.h index 56d6ec2c..c8c2bbc8 100644 --- a/common/include/interfaces/service_profile_filter_opotions.h +++ b/common/include/interfaces/service_profile_filter_opotions.h @@ -27,7 +27,7 @@ namespace OHOS { namespace DistributedDeviceProfile { class ServiceProfileFilterOptions : public DpParcel { public: - ServiceProfileFilterOptions(int32_t userId, std::string& accountId, const std::vector& deviceIds, + ServiceProfileFilterOptions(int32_t userId, std::string& accountId, std::string& deviceId, const std::vector& wiseDeviceIds, const std::vector& serviceIds, const std::vector& serviceProfileIds, const std::vector& deviceProfileIds); ServiceProfileFilterOptions(); @@ -37,27 +37,31 @@ public: void SetUserId(int32_t userId); const std::string& GetAccountId() const; void SetAccountId(const std::string& accountId); - const std::vector& GetDeviceIds() const; - void SetDeviceIds(const std::vector& deviceIds); + const std::string& GetDeviceId() const; + void SetDeviceId(const std::string& deviceId); const std::vector& GetWiseDeviceIds() const; void SetWiseDeviceIds(const std::vector& wiseDeviceIds); + void AddWiseDeviceIds(const std::string& wiseDeviceId); const std::vector& GetServiceIds() const; void SetServiceIds(const std::vector& serviceIds); + void AddServiceIds(const std::string& serviceId); const std::vector& GetServiceProfileIds() const; void SetServiceProfileIds(const std::vector& serviceProfileIds); + void AddServiceProfileIds(int32_t serviceProfileId); const std::vector& GetDeviceProfileIds() const; void SetDeviceProfileIds(const std::vector& deviceProfileIds); + void AddDeviceProfileIds(int32_t deviceProfileId); bool IsEmpty() const; - bool Marshalling(MessageParcel& parcel) const override; - bool UnMarshalling(MessageParcel& parcel) override; + bool UnMarshalling(MessageParcel& parcel) override; + std::string dump() const override; bool operator==(const ServiceProfileFilterOptions& rhs) const; private: int32_t userId_ = DEFAULT_USER_ID; std::string accountId_ = ""; - std::vector deviceIds_; + std::string deviceId_ = ""; std::vector wiseDeviceIds_; std::vector serviceIds_; std::vector serviceProfileIds_; diff --git a/common/src/constants/distributed_device_profile_constants.cpp b/common/src/constants/distributed_device_profile_constants.cpp index bc43c11a..ad7f55fe 100644 --- a/common/src/constants/distributed_device_profile_constants.cpp +++ b/common/src/constants/distributed_device_profile_constants.cpp @@ -143,6 +143,7 @@ const std::string PUT_ALL_TRUSTED_DEVICES = "PutAllTrustedDevices"; /* Common constants */ const std::string IS_MULTI_USER = "is_multi_user"; const std::string SEPARATOR = "#"; +const std::string SLASHES = "/"; const std::string DEV_PREFIX = "dev"; const std::string SVR_PREFIX = "svr"; const std::string CHAR_PREFIX = "char"; @@ -160,6 +161,7 @@ const std::string USERID = "userId"; const std::string BUNDLENAME = "bundleName"; const std::string TOKENID = "tokenId"; const std::string ACCOUNTID = "accountId"; +const std::string PRODUCTID = "productId"; const std::string DEVICEID_EQUAL_CONDITION = "deviceId = ?"; const std::string ACCESSCONTROLID_EQUAL_CONDITION = "accessControlId = ?"; const std::string ACCESSERID_EQUAL_CONDITION = "accesserId = ? "; diff --git a/common/src/interfaces/characteristic_profile.cpp b/common/src/interfaces/characteristic_profile.cpp index 6977d4cb..8edde887 100644 --- a/common/src/interfaces/characteristic_profile.cpp +++ b/common/src/interfaces/characteristic_profile.cpp @@ -155,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/device_profile_filter_options.cpp b/common/src/interfaces/device_profile_filter_options.cpp index b0c1f124..af1d1999 100644 --- a/common/src/interfaces/device_profile_filter_options.cpp +++ b/common/src/interfaces/device_profile_filter_options.cpp @@ -30,6 +30,21 @@ int32_t DeviceProfileFilterOptions::GetUserId() const return userId_; } +void DeviceProfileFilterOptions::AddDeviceIds(std::string deviceId) +{ + deviceIds_.emplace_back(deviceId); +} + +void DeviceProfileFilterOptions::AddWiseDeviceIds(std::string wiseDeviceId) +{ + wiseDeviceIds_.emplace_back(wiseDeviceId); +} + +void DeviceProfileFilterOptions::AddDeviceProfileIds(int32_t deviceProfileId) +{ + deviceProfileIds_.emplace_back(deviceProfileId); +} + void DeviceProfileFilterOptions::SetUserId(int32_t userId) { userId_ = userId; @@ -135,4 +150,4 @@ std::string DeviceProfileFilterOptions::dump() const return jsonStr; } } // namespace DistributedDeviceProfile -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/common/src/interfaces/service_profile.cpp b/common/src/interfaces/service_profile.cpp index a8dcfbb1..8b142ee7 100644 --- a/common/src/interfaces/service_profile.cpp +++ b/common/src/interfaces/service_profile.cpp @@ -120,6 +120,16 @@ void ServiceProfile::SetDeviceProfileId(int32_t deviceProfileId) deviceProfileId_ = deviceProfileId; } +const std::vector& ServiceProfile::GetCharacteristicProfiles() const +{ + return characteristicProfiles_; +} + +void ServiceProfile::SetCharacteristicProfiles(const std::vector& characteristicProfiles) +{ + characteristicProfiles_ = characteristicProfiles; +} + bool ServiceProfile::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, String, deviceId_, false); diff --git a/common/src/interfaces/service_profile_filter_opotions.cpp b/common/src/interfaces/service_profile_filter_opotions.cpp index c88a5e26..eec2f603 100644 --- a/common/src/interfaces/service_profile_filter_opotions.cpp +++ b/common/src/interfaces/service_profile_filter_opotions.cpp @@ -28,10 +28,10 @@ namespace { const std::string TAG = "ServiceProfileFilterOptions"; } ServiceProfileFilterOptions::ServiceProfileFilterOptions(int32_t userId, std::string& accountId, - const std::vector& deviceIds,const std::vector& wiseDeviceIds, + std::string& deviceId, const std::vector& wiseDeviceIds, const std::vector& serviceIds,const std::vector& serviceProfileIds, const std::vector& deviceProfileIds) - : userId_(userId), accountId_(accountId), deviceIds_(deviceIds), wiseDeviceIds_(wiseDeviceIds), + : userId_(userId), accountId_(accountId), deviceId_(deviceId), wiseDeviceIds_(wiseDeviceIds), serviceIds_(serviceIds), serviceProfileIds_(serviceProfileIds), deviceProfileIds_(deviceProfileIds) { } @@ -64,14 +64,14 @@ void ServiceProfileFilterOptions::SetAccountId(const std::string& accountId) accountId_ = accountId; } -const std::vector& ServiceProfileFilterOptions::GetDeviceIds() const +const std::string& ServiceProfileFilterOptions::GetDeviceId() const { - return deviceIds_; + return deviceId_; } -void ServiceProfileFilterOptions::SetDeviceIds(const std::vector& deviceIds) +void ServiceProfileFilterOptions::SetDeviceId(const std::string& deviceId) { - deviceIds_ = deviceIds; + deviceId_ = deviceId; } const std::vector& ServiceProfileFilterOptions::GetWiseDeviceIds() const @@ -118,10 +118,7 @@ bool ServiceProfileFilterOptions::Marshalling(MessageParcel& parcel) const { WRITE_HELPER_RET(parcel, Int32, userId_, false); WRITE_HELPER_RET(parcel, String, accountId_, false); - if (!IpcUtils::Marshalling(parcel, deviceIds_)) { - HILOGE("dp write parcel fail"); - return false; - } + WRITE_HELPER_RET(parcel, String, deviceId_, false); if (!IpcUtils::Marshalling(parcel, wiseDeviceIds_)) { HILOGE("dp write parcel fail"); return false; @@ -145,10 +142,7 @@ bool ServiceProfileFilterOptions::UnMarshalling(MessageParcel& parcel) { READ_HELPER_RET(parcel, Int32, userId_, false); READ_HELPER_RET(parcel, String, accountId_, false); - if (!IpcUtils::UnMarshalling(parcel, deviceIds_)) { - HILOGE("dp read parcel fail"); - return false; - } + READ_HELPER_RET(parcel, String, deviceId_, false); if (!IpcUtils::UnMarshalling(parcel, wiseDeviceIds_)) { HILOGE("dp read parcel fail"); return false; @@ -170,17 +164,41 @@ bool ServiceProfileFilterOptions::UnMarshalling(MessageParcel& parcel) bool ServiceProfileFilterOptions::IsEmpty() const { - return deviceIds_.empty() && wiseDeviceIds_.empty() && serviceIds_.empty() && userId_ == DEFAULT_USER_ID && + return deviceId_.empty() && wiseDeviceIds_.empty() && serviceIds_.empty() && userId_ == DEFAULT_USER_ID && serviceProfileIds_.empty() && accountId_.empty() && deviceProfileIds_.empty(); } bool ServiceProfileFilterOptions::operator==(const ServiceProfileFilterOptions& rhs) const { - return userId_ == rhs.userId_ && accountId_ == rhs.accountId_ && deviceIds_ == rhs.deviceIds_ && + return userId_ == rhs.userId_ && accountId_ == rhs.accountId_ && deviceId_ == rhs.deviceId_ && wiseDeviceIds_ == rhs.wiseDeviceIds_ && serviceIds_ == rhs.serviceIds_ && serviceProfileIds_ == rhs.serviceProfileIds_ && deviceProfileIds_ == rhs.deviceProfileIds_; } +void ServiceProfileFilterOptions::AddWiseDeviceIds(const std::string& wiseDeviceId) +{ + wiseDeviceIds_.emplace_back(wiseDeviceId); +} + +void ServiceProfileFilterOptions::AddServiceIds(const std::string& serviceId) +{ + serviceIds_.emplace_back(serviceId); +} + +void ServiceProfileFilterOptions::AddServiceProfileIds(int32_t serviceProfileId) +{ + serviceProfileIds_.emplace_back(serviceProfileId); +} + +void ServiceProfileFilterOptions::AddDeviceProfileIds(int32_t deviceProfileId) +{ + deviceProfileIds_.emplace_back(deviceProfileId); +} + +std::string ServiceProfileFilterOptions::dump() const +{ + return ""; +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/common/src/utils/ipc_utils.cpp b/common/src/utils/ipc_utils.cpp index 129064cc..ef291f1f 100644 --- a/common/src/utils/ipc_utils.cpp +++ b/common/src/utils/ipc_utils.cpp @@ -281,7 +281,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params { size_t size = parcel.ReadUint32(); if (size == 0 || size > MAX_ID_SIZE) { - HILOGE("int32_t vector, params size is invalid! size : %{public}u", size); + HILOGE("int32_t vector, params size is invalid! size : %{public}zu", size); return false; } for (uint32_t i = 0; i < size; i++) { @@ -296,7 +296,7 @@ bool IpcUtils::UnMarshalling(MessageParcel& parcel, std::vector& params { size_t size = parcel.ReadUint32(); if (size == 0 || size > MAX_ICON_SIZE) { - HILOGE("uint8_t vector, params size is invalid! size : %{public}u", size); + HILOGE("uint8_t vector, params size is invalid! size : %{public}zu", size); return false; } for (uint32_t i = 0; i < size; i++) { diff --git a/services/core/include/profiledatamanager/characteristic_profile_dao.h b/services/core/include/profiledatamanager/characteristic_profile_dao.h index 5fbf7669..f1ef94ba 100644 --- a/services/core/include/profiledatamanager/characteristic_profile_dao.h +++ b/services/core/include/profiledatamanager/characteristic_profile_dao.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef OHOS_DP_DEVICE_PROFILE_DAO_H -#define OHOS_DP_DEVICE_PROFILE_DAO_H +#ifndef OHOS_DP_CHARACTERISTIC_PROFILE_DAO_H +#define OHOS_DP_CHARACTERISTIC_PROFILE_DAO_H #include @@ -34,6 +34,7 @@ namespace DistributedDeviceProfile { using namespace OHOS::NativeRdb; class CharacteristicProfileDao { + DECLARE_SINGLE_INSTANCE(CharacteristicProfileDao); public: int32_t Init(); int32_t UnInit(); @@ -55,4 +56,4 @@ private: }; } } -#endif // OHOS_DP_DEVICE_PROFILE_DAO_H \ No newline at end of file +#endif // OHOS_DP_CHARACTERISTIC_PROFILE_DAO_H diff --git a/services/core/include/profiledatamanager/device_profile_dao.h b/services/core/include/profiledatamanager/device_profile_dao.h index d2376338..bb869fc8 100644 --- a/services/core/include/profiledatamanager/device_profile_dao.h +++ b/services/core/include/profiledatamanager/device_profile_dao.h @@ -44,7 +44,7 @@ public: int32_t GetDeviceProfiles(const DeviceProfileFilterOptions& filterOptions, std::vector& deviceProfiles); int32_t DeleteDeviceProfile(const DeviceProfile& deviceProfile); - int32_t UpdateDeviceProfile(const DeviceProfile& oldProfile, const DeviceProfile& newProfile); + int32_t UpdateDeviceProfile(const DeviceProfile& newProfile); int32_t CreateTable(); int32_t CreateIndex(); void CreateQuerySqlAndCondition(const DeviceProfileFilterOptions& filterOptions, @@ -60,4 +60,4 @@ private: }; } } -#endif // OHOS_DP_DEVICE_PROFILE_DAO_H \ No newline at end of file +#endif // OHOS_DP_DEVICE_PROFILE_DAO_H diff --git a/services/core/include/profiledatamanager/profile_data_manager.h b/services/core/include/profiledatamanager/profile_data_manager.h index cbbf2eec..f4fc7c85 100644 --- a/services/core/include/profiledatamanager/profile_data_manager.h +++ b/services/core/include/profiledatamanager/profile_data_manager.h @@ -16,7 +16,12 @@ #ifndef OHOS_DP_PROFILE_DATA_MANAGER_H #define OHOS_DP_PROFILE_DATA_MANAGER_H +#include + +#include "device_profile.h" +#include "device_profile_dao.h" #include "single_instance.h" +#include "service_profile_dao.h" namespace OHOS { namespace DistributedDeviceProfile { @@ -24,9 +29,23 @@ class ProfileDataManager { DECLARE_SINGLE_INSTANCE(ProfileDataManager); public: - + int32_t PutDeviceProfile(DeviceProfile deviceProfile); + int32_t PutDeviceProfileBatch(std::vector deviceProfiles); + int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); + int32_t GetDeviceProfiles(DeviceProfileFilterOptions& options, + std::vector& deviceProfiles); + int32_t DeleteDeviceProfile(const DeviceProfile& deviceProfile); + int32_t DeleteDeviceProfileBatch(const std::vector& deviceProfiles); + int32_t PutServiceProfile(ServiceProfile& serviceProfile); + int32_t PutServiceProfileBatch(std::vector& serviceProfiles); + int32_t DeleteServiceProfile(const ServiceProfile& serviceProfile); + int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceId, + ServiceProfile& serviceProfile); + int32_t GetServiceProfiles(ServiceProfileFilterOptions& options,std::vector& serviceProfiles); + private: - + bool FilterInvaildSymbol(std::string str); + int32_t DeleteServiceProfileBatch(const std::vector& serviceProfiles); }; } // DistributedDeviceProfile } // OHOS diff --git a/services/core/include/profiledatamanager/service_profile_dao.h b/services/core/include/profiledatamanager/service_profile_dao.h index ee64268d..4ac4c8f9 100644 --- a/services/core/include/profiledatamanager/service_profile_dao.h +++ b/services/core/include/profiledatamanager/service_profile_dao.h @@ -21,13 +21,13 @@ #include "profile_data_rdb_adapter.h" #include "service_profile.h" #include "service_profile_filter_opotions.h" +#include "single_instance.h" namespace OHOS { namespace DistributedDeviceProfile { class ServiceProfileDao { + DECLARE_SINGLE_INSTANCE(ServiceProfileDao); public: - ServiceProfileDao() = default; - ~ServiceProfileDao() = default; int32_t Init(); int32_t UnInit(); int32_t PutServiceProfile(ServiceProfile& serviceProfile); diff --git a/services/core/include/utils/profile_cache.h b/services/core/include/utils/profile_cache.h index 527400c1..5ca3dbe2 100644 --- a/services/core/include/utils/profile_cache.h +++ b/services/core/include/utils/profile_cache.h @@ -89,6 +89,7 @@ public: std::string GetLocalUdid(); std::string GetLocalNetworkId(); std::string GetLocalUuid(); + std::string GetLocalAccountId(); int32_t AddAllTrustedDevices(const std::vector& deviceInfos); bool FilterAndGroupOnlineDevices(const std::vector& deviceList, std::vector& ohBasedDevices, std::vector& notOHBasedDevices); diff --git a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp index eba1972d..27fd7bd2 100644 --- a/services/core/src/profiledatamanager/characteristic_profile_dao.cpp +++ b/services/core/src/profiledatamanager/characteristic_profile_dao.cpp @@ -28,6 +28,7 @@ namespace OHOS { namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(CharacteristicProfileDao); namespace { const std::string TAG = "CharacteristicProfileDao"; } @@ -308,4 +309,4 @@ int32_t CharacteristicProfileDao::SetCharacteristicProfileId(CharacteristicProfi return DP_SUCCESS; } } // namespace DistributedDeviceProfile -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/core/src/profiledatamanager/device_profile_dao.cpp b/services/core/src/profiledatamanager/device_profile_dao.cpp index cca83ee4..7f1f2ef0 100644 --- a/services/core/src/profiledatamanager/device_profile_dao.cpp +++ b/services/core/src/profiledatamanager/device_profile_dao.cpp @@ -151,7 +151,7 @@ int32_t DeviceProfileDao::DeleteDeviceProfile(const DeviceProfile &deviceProfile return DP_SUCCESS; } -int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &oldProfile, const DeviceProfile &newProfile) +int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &newProfile) { ValuesBucket values; DeviceProfileToEntries(newProfile, values); @@ -166,7 +166,7 @@ int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &oldProfile, c return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL; } } - SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(oldProfile, newProfile); + SubscribeProfileManager::GetInstance().NotifyDeviceProfileUpdate(DeviceProfile(), newProfile); std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); if (localUdid == newProfile.GetDeviceId()) { DeviceProfileManager::GetInstance().PutDeviceProfile(newProfile); @@ -326,4 +326,4 @@ void DeviceProfileDao::GenerateSqlAndCondition(const std::vector &p return; } } // namespace DistributedDeviceProfile -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/core/src/profiledatamanager/profile_data_manager.cpp b/services/core/src/profiledatamanager/profile_data_manager.cpp index 78f3be54..7213edb4 100644 --- a/services/core/src/profiledatamanager/profile_data_manager.cpp +++ b/services/core/src/profiledatamanager/profile_data_manager.cpp @@ -15,16 +15,412 @@ #include "profile_data_manager.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 "multi_user_manager.h" +#include "profile_cache.h" +#include "profile_utils.h" +#include "service_profile_filter_opotions.h" namespace OHOS { namespace DistributedDeviceProfile { -IMPLEMENT_SINGLE_INSTANCE(ProfileDataManager); +IMPLEMENT_SINGLE_INSTANCE(ProfileDataManager) + namespace { - const std::string TAG = "ProfileDataManager"; +const std::string TAG = "ProfileDataManager"; +} +int32_t ProfileDataManager::PutDeviceProfile(DeviceProfile deviceProfile) +{ + if (deviceProfile.GetDeviceId().empty() || ProfileDataManager::FilterInvaildSymbol(deviceProfile.GetDeviceId())) { + HILOGE("deviceId invaild,deviceId=%{public}s", + ProfileUtils::GetAnonyString(deviceProfile.GetDeviceId()).c_str()); + } + if (deviceProfile.GetDeviceId() == ProfileCache::GetInstance().GetLocalUdid()) { + deviceProfile.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + } else { + deviceProfile.SetUserId(0); + } + deviceProfile.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId()); + DeviceProfileFilterOptions filterOptions; + filterOptions.AddDeviceIds(deviceProfile.GetDeviceId()); + filterOptions.SetUserId(deviceProfile.GetUserId()); + filterOptions.SetAccountId(deviceProfile.GetAccountId()); + std::vector oldDeviceProfiles; + DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, oldDeviceProfiles); + DeviceProfile oldDeviceProfile = oldDeviceProfiles[0]; + deviceProfile.SetId(oldDeviceProfile.GetId()); + int32_t ret = RET_INIT; + if (deviceProfile.GetId() <= 0) { + ret = DeviceProfileDao::GetInstance().PutDeviceProfile(deviceProfile); + } else { + ret = DeviceProfileDao::GetInstance().UpdateDeviceProfile(deviceProfile); + } + if (ret != DP_SUCCESS) { + HILOGE("PutDeviceProfile failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +bool ProfileDataManager::FilterInvaildSymbol(std::string str) +{ + if (str.length() == 0 || str.length() > MAX_STRING_LEN) { + return false; + } + size_t found = str.find(SEPARATOR); + if (found == std::string::npos) { + return false; + } + size_t foundSlashes = str.find(SLASHES); + if (foundSlashes == std::string::npos) { + return false; + } + return true; +} + +int32_t ProfileDataManager::PutDeviceProfileBatch(std::vector deviceProfiles) +{ + if (deviceProfiles.empty()) { + HILOGE("deviceProfiles is empty"); + return DP_INVALID_PARAM; + } + // 循环写入deviceProfiles + int32_t ret = RET_INIT; + for (auto deviceProfile : deviceProfiles) { + int32_t putRet = PutDeviceProfile(deviceProfile); + if (ret != DP_SUCCESS) { + ret = putRet; + } + } + if (ret != DP_SUCCESS) { + HILOGE("PutDeviceProfiles failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) +{ + if (deviceId.empty() || deviceId.length() > MAX_STRING_LEN) { + HILOGE("deviceId is empty"); + return DP_INVALID_PARAM; + } + if (deviceId == ProfileCache::GetInstance().GetLocalUdid()) { + deviceProfile.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + } else { + deviceProfile.SetUserId(0); + } + DeviceProfileFilterOptions filterOptions; + filterOptions.AddDeviceIds(deviceId); + filterOptions.SetUserId(deviceProfile.GetUserId()); + std::vector deviceProfiles; + int32_t ret = + DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, deviceProfiles); + if (ret != DP_SUCCESS) { + HILOGE("GetDeviceProfile failed,ret=%{public}d", ret); + return ret; + } + deviceProfile = deviceProfiles[0]; + return DP_SUCCESS; +} + +int32_t ProfileDataManager::GetDeviceProfiles(DeviceProfileFilterOptions& options, + std::vector& deviceProfiles) +{ + std::string localAccountId = ProfileCache::GetInstance().GetLocalAccountId(); + std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); + int32_t localUserId = MultiUserManager::GetInstance().GetCurrentForegroundUserID(); + options.SetAccountId(localAccountId); + int32_t ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(options, deviceProfiles); + if (ret != DP_SUCCESS) { + HILOGE("GetDeviceProfile failed,ret=%{public}d", ret); + return ret; + } + for (auto iter = deviceProfiles.begin(); iter != deviceProfiles.end();) { + if (iter->GetDeviceId() == localUdid || iter->GetUserId() == localUserId) { + iter = deviceProfiles.erase(iter); + } else { + iter++; + } + } + if (deviceProfiles.empty()) { + HILOGE("GetDeviceProfile failed,deviceProfiles is empty"); + return DP_DEVICE_PROFILE_NOT_FOUND; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::DeleteDeviceProfile(const DeviceProfile& deviceProfile) +{ + std::string localAccountId = ProfileCache::GetInstance().GetLocalAccountId(); + std::string localUdid = ProfileCache::GetInstance().GetLocalUdid(); + if (deviceProfile.GetDeviceId().empty() || + deviceProfile.GetDeviceId() == localUdid) { + HILOGE("deviceId is invaild"); + return DP_INVALID_PARAM; + } + DeviceProfileFilterOptions deviceProfileOptions; + deviceProfileOptions.AddDeviceIds(deviceProfile.GetDeviceId()); + deviceProfileOptions.SetUserId(0); + deviceProfileOptions.SetAccountId(localAccountId); + std::vector oldDeviceProfiles; + DeviceProfileDao::GetInstance().GetDeviceProfiles(deviceProfileOptions, oldDeviceProfiles); + if (oldDeviceProfiles.empty()) { + HILOGE("oldDeviceProfiles is empty"); + return DP_DEVICE_PROFILE_NOT_FOUND; + } + if (oldDeviceProfiles[0].GetDeviceId() == localUdid || oldDeviceProfiles[0].GetAccountId() != localAccountId) { + HILOGE("deviceId or accountId is local"); + return DP_DEVICE_PROFILE_NOT_FOUND; + } + ServiceProfileFilterOptions serviceProfileOptions; + serviceProfileOptions.AddDeviceProfileIds(oldDeviceProfiles[0].GetId()); + std::vector serviceProfiles; + ServiceProfileDao::GetInstance().GetServiceProfiles(serviceProfileOptions, serviceProfiles); + + int32_t ret = DeleteServiceProfileBatch(serviceProfiles); + if (ret != DP_SUCCESS) { + HILOGE("DeleteServiceProfileBatch failed,ret=%{public}d", ret); + return ret; + } + ret = DeviceProfileDao::GetInstance().DeleteDeviceProfile(oldDeviceProfiles[0]); + if (ret != DP_SUCCESS) { + HILOGE("DeleteDeviceProfile failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::DeleteDeviceProfileBatch(const std::vector& deviceProfiles) +{ + int32_t ret = RET_INIT; + for (auto& deviceProfile : deviceProfiles) { + if (deviceProfile.GetDeviceId() == ProfileCache::GetInstance().GetLocalUdid()) { + HILOGI("deviceProfile.deviceId=localUdid,skip! deviceProfile: %{public}s", + deviceProfile.AnnoymizeDump().c_str()); + continue; + } + int32_t deleteRet = DeleteDeviceProfile(deviceProfile); + if (deleteRet != DP_SUCCESS) { + HILOGE("DeleteDeviceProfile fail, deviceProfile: %{public}s, errcode: %{public}d!", + deviceProfile.AnnoymizeDump().c_str(), deleteRet); + ret = deleteRet; + } + } + return ret; } +int32_t ProfileDataManager::PutServiceProfile(ServiceProfile& serviceProfile) +{ + if (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty() || + serviceProfile.GetServiceType().empty() || FilterInvaildSymbol(serviceProfile.GetDeviceId()) || + FilterInvaildSymbol(serviceProfile.GetServiceName())) { + HILOGE("serviceProfile is invaild"); + return DP_INVALID_PARAM; + } + std::string accountId = ""; + if (serviceProfile.GetDeviceId() == ProfileCache::GetInstance().GetLocalUdid()) { + serviceProfile.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + } else { + serviceProfile.SetUserId(0); + accountId = ProfileCache::GetInstance().GetLocalAccountId(); + } + ServiceProfileFilterOptions serOptions; + serOptions.SetDeviceId(serviceProfile.GetDeviceId()); + serOptions.SetUserId(serviceProfile.GetUserId()); + serOptions.SetAccountId(accountId); + serOptions.AddServiceIds(serviceProfile.GetServiceName()); + std::vector oldServiceProfiles; + ServiceProfileDao::GetInstance().GetServiceProfiles(serOptions, oldServiceProfiles); + if (oldServiceProfiles.empty()) { + DeviceProfile deviceProfile; + DeviceProfileFilterOptions devOptions; + devOptions.AddDeviceIds(serviceProfile.GetDeviceId()); + devOptions.SetUserId(serviceProfile.GetUserId()); + devOptions.SetAccountId(accountId); + std::vector deviceProfiles; + DeviceProfileDao::GetInstance().GetDeviceProfiles(devOptions, deviceProfiles); + if (deviceProfiles.empty()) { + HILOGE("GetDeviceProfiles is empty"); + return DP_DEVICE_PROFILE_NOT_FOUND; + } + deviceProfile = deviceProfiles[0]; + if (deviceProfile.GetDeviceId() != ProfileCache::GetInstance().GetLocalUdid() || + deviceProfile.GetAccountId() != accountId) { + HILOGE("deviceProfile is not local"); + return DP_DEVICE_PROFILE_NOT_FOUND; + } + serviceProfile.SetDeviceProfileId(deviceProfile.GetId()); + int32_t putRet = ServiceProfileDao::GetInstance().PutServiceProfile(serviceProfile); + if (putRet != DP_SUCCESS) { + HILOGE("PutServiceProfile failed,ret=%{public}d", putRet); + return putRet; + } + } else { + serviceProfile.SetId(oldServiceProfiles[0].GetId()); + serviceProfile.SetDeviceProfileId(oldServiceProfiles[0].GetDeviceProfileId()); + int32_t updateRet = ServiceProfileDao::GetInstance().UpdateServiceProfile(serviceProfile); + if (updateRet != DP_SUCCESS) { + HILOGE("UpdateServiceProfile failed,ret=%{public}d", updateRet); + return updateRet; + } + } + if (!serviceProfile.GetCharacteristicProfiles().empty()) { + for (auto& tempCharProfile : serviceProfile.GetCharacteristicProfiles()) { + CharacteristicProfile charProfile = tempCharProfile; + charProfile.SetServiceProfileId(serviceProfile.GetId()); + //todo PutCharacteristicProfile(charProfile); + } + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::PutServiceProfileBatch(std::vector& serviceProfiles) +{ + if (serviceProfiles.empty() || serviceProfiles.size() > MAX_SERVICE_SIZE) { + HILOGE("serviceProfiles is empty or size is invaild"); + return DP_INVALID_PARAM; + } + int32_t ret = RET_INIT; + for (auto& serviceProfile : serviceProfiles) { + int32_t putRet = PutServiceProfile(serviceProfile); + if (putRet != DP_SUCCESS) { + ret = putRet; + } + } + if (ret != DP_SUCCESS) { + HILOGE("PutServiceProfileBatch failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::DeleteServiceProfile(const ServiceProfile& serviceProfile) +{ + if (serviceProfile.GetId() < 0 && + (serviceProfile.GetDeviceId().empty() || serviceProfile.GetServiceName().empty())) { + HILOGE("serviceProfile is invaild"); + return DP_INVALID_PARAM; + } + ServiceProfileFilterOptions serOptions; + if (serviceProfile.GetId() > 0) { + serOptions.AddServiceProfileIds(serviceProfile.GetId()); + } else { + serOptions.SetDeviceId(serviceProfile.GetDeviceId()); + serOptions.AddServiceIds(serviceProfile.GetServiceName()); + if (serviceProfile.GetDeviceId() == ProfileCache::GetInstance().GetLocalUdid()) { + serOptions.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + } else { + serOptions.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId()); + } + } + std::vector oldServiceProfiles; + ServiceProfileDao::GetInstance().GetServiceProfiles(serOptions, oldServiceProfiles); + if (oldServiceProfiles.empty()) { + HILOGE("oldServiceProfiles is empty"); + return DP_SERVICE_PROFILE_NOT_FOUND; + } + CharacteristicProfileFilterOption charOption; + charOption.SetServiceProfileId(oldServiceProfiles[0].GetId()); + std::vector charProfiles; + CharacteristicProfileDao::GetInstance().GetCharacteristicProfiles(charOption, charProfiles); + // todo: charProfiles和oldServiceProfiles[0]要一并删除 + for (auto& CharProfile : charProfiles) { + CharacteristicProfileDao::GetInstance().DeleteCharacteristicProfile(CharProfile); + } + ServiceProfileDao::GetInstance().DeleteServiceProfile(oldServiceProfiles[0]); + return DP_SUCCESS; +} + +int32_t ProfileDataManager::GetServiceProfile(const std::string& deviceId, const std::string& serviceId, + ServiceProfile& serviceProfile) +{ + if (deviceId.empty() || serviceId.empty()) { + HILOGE("deviceId or serviceId is empty"); + return DP_INVALID_PARAM; + } + ServiceProfileFilterOptions serOptions; + serOptions.SetDeviceId(deviceId); + serOptions.AddServiceIds(serviceId); + if (deviceId == ProfileCache::GetInstance().GetLocalUdid()) { + serOptions.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + } else { + serOptions.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId()); + } + std::vector serviceProfiles; + int32_t ret = ServiceProfileDao::GetInstance().GetServiceProfiles(serOptions, serviceProfiles); + if (ret != DP_SUCCESS) { + HILOGE("GetServiceProfiles failed,ret=%{public}d", ret); + return ret; + } + serviceProfile = serviceProfiles[0]; + CharacteristicProfileFilterOption charOption; + std::vector charProfiles; + charOption.SetServiceProfileId(serviceProfile.GetId()); + ret = CharacteristicProfileDao::GetInstance().GetCharacteristicProfiles(charOption, charProfiles); + if (ret != DP_SUCCESS) { + HILOGE("GetCharacteristicProfiles failed,ret=%{public}d", ret); + return ret; + } + serviceProfile.SetCharacteristicProfiles(charProfiles); + return DP_SUCCESS; +} + +int32_t ProfileDataManager::GetServiceProfiles(ServiceProfileFilterOptions& options, + std::vector& serviceProfiles) +{ + if (options.GetDeviceId().empty()) { + HILOGE("deviceId is empty"); + return DP_INVALID_PARAM; + } + if (options.GetDeviceId() == ProfileCache::GetInstance().GetLocalUdid()) { + options.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + } else { + options.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId()); + } + std::vector serviceProfilesTemp; + int32_t ret = ServiceProfileDao::GetInstance().GetServiceProfiles(options, serviceProfilesTemp); + if (ret != DP_SUCCESS) { + HILOGE("GetServiceProfiles failed,ret=%{public}d", ret); + return ret; + } + for (auto& serviceProfile : serviceProfilesTemp) { + CharacteristicProfileFilterOption charOption; + charOption.SetServiceProfileId(serviceProfile.GetId()); + std::vector charProfiles; + ret = CharacteristicProfileDao::GetInstance().GetCharacteristicProfiles(charOption, charProfiles); + if (ret != DP_SUCCESS) { + HILOGE("GetCharacteristicProfiles failed,ret=%{public}d", ret); + return ret; + } + serviceProfile.SetCharacteristicProfiles(charProfiles); + serviceProfiles.emplace_back(serviceProfile); + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::DeleteServiceProfileBatch(const std::vector& serviceProfiles) +{ + int32_t ret = RET_INIT; + for (auto& serviceProfile : serviceProfiles) { + if (serviceProfile.GetDeviceId() == ProfileCache::GetInstance().GetLocalUdid()) { + HILOGI("serviceProfile.deviceId=localUdid,skip! serviceProfile: %{public}s", + serviceProfile.dump().c_str()); + continue; + } + int32_t deleteRet = DeleteServiceProfile(serviceProfile); + if (deleteRet != DP_SUCCESS) { + HILOGE("DeleteServiceProfile fail, serviceProfile: %{public}s, errcode: %{public}d!", + serviceProfile.dump().c_str(), deleteRet); + ret = deleteRet; + } + } + return RET_INIT; +} + + } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/src/profiledatamanager/service_profile_dao.cpp b/services/core/src/profiledatamanager/service_profile_dao.cpp index 676cade4..cba6e3db 100644 --- a/services/core/src/profiledatamanager/service_profile_dao.cpp +++ b/services/core/src/profiledatamanager/service_profile_dao.cpp @@ -28,6 +28,7 @@ namespace OHOS { namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ServiceProfileDao); namespace { const std::string TAG = "DistributedDeviceProfile"; } @@ -269,16 +270,6 @@ void ServiceProfileDao::CreateComplexQuerySqlAndCondition(const ServiceProfileFi whereSql.erase(whereSql.end() - 1, whereSql.end()); whereSql += ") AND"; } - // whereSql append device_profile.deviceId - if (!filterOptions.GetDeviceIds().empty()) { - whereSql += "device_profile.udid IN ("; - for (auto& deviceId : filterOptions.GetDeviceIds()) { - whereSql += "?,"; - condition.emplace_back(ValueObject(deviceId)); - } - whereSql.erase(whereSql.end() - 1, whereSql.end()); - whereSql += ") AND"; - } } void ServiceProfileDao::CreateBaseQuerySqlAndCondition(const ServiceProfileFilterOptions& filterOptions, @@ -294,6 +285,11 @@ void ServiceProfileDao::CreateBaseQuerySqlAndCondition(const ServiceProfileFilte whereSql += "device_profile.accountId=? AND"; condition.emplace_back(ValueObject(filterOptions.GetAccountId())); } + // whereSql append device_profile.deviceId + if (!filterOptions.GetDeviceId().empty()) { + whereSql += "device_profile.deviceId=? AND"; + condition.emplace_back(ValueObject(filterOptions.GetDeviceId())); + } // whereSql append device_profile.wiseDeviceId if (!filterOptions.GetWiseDeviceIds().empty()) { whereSql += "device_profile.wiseDeviceId IN ("; diff --git a/services/core/src/utils/profile_cache.cpp b/services/core/src/utils/profile_cache.cpp index ac43f13a..a7d95d29 100644 --- a/services/core/src/utils/profile_cache.cpp +++ b/services/core/src/utils/profile_cache.cpp @@ -805,6 +805,12 @@ std::string ProfileCache::GetLocalUuid() return localUuid; } +std::string ProfileCache::GetLocalAccountId() +{ + // todo: get local accountId + return ""; +} + int32_t ProfileCache::AddAllTrustedDevices(const std::vector& deviceInfos) { HILOGI("deviceInfos.size: %{public}zu!", deviceInfos.size()); -- Gitee