diff --git a/common/include/interfaces/device_profile_filter_options.h b/common/include/interfaces/device_profile_filter_options.h index 1e5328c059ba42c07288490af5909ea290b09ed3..db28c64ca4a0dc3fce90b923d80fa04b6e8e85c7 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/src/interfaces/device_icon_info.cpp b/common/src/interfaces/device_icon_info.cpp index 970ff969c89677aa1b2870a079fab55fcd6b8bd6..5560395aadcd7c966cde45ba415fa25d51d2c90e 100644 --- a/common/src/interfaces/device_icon_info.cpp +++ b/common/src/interfaces/device_icon_info.cpp @@ -17,6 +17,7 @@ #include "cJSON.h" #include "distributed_device_profile_constants.h" +#include "ipc_utils.h" #include "macro_utils.h" namespace OHOS { @@ -119,7 +120,6 @@ void DeviceIconInfo::SetIcon(const std::vector& 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); @@ -127,13 +127,12 @@ bool DeviceIconInfo::Marshalling(MessageParcel& parcel) const 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); + IpcUtils::Marshalling(parcel, icon_); 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); @@ -141,7 +140,7 @@ bool DeviceIconInfo::UnMarshalling(MessageParcel& parcel) 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); + IpcUtils::UnMarshalling(parcel, icon_); return true; } diff --git a/common/src/interfaces/device_profile_filter_options.cpp b/common/src/interfaces/device_profile_filter_options.cpp index b0c1f1246b1e68f6ce146be3b42cf2f9c15a4eb1..af1d19991b49c1de3aff43462e61d2c77acaf66f 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/product_info.cpp b/common/src/interfaces/product_info.cpp index 324b94e8353a984f596de90288755ffa6a686e92..f7024623dda9fb0c3f3071a5a24dd262c5548d21 100644 --- a/common/src/interfaces/product_info.cpp +++ b/common/src/interfaces/product_info.cpp @@ -154,7 +154,6 @@ std::string ProductInfo::GetAnonyProductName(const std::string& productName) con return EMPTY_STRING; } std::string jsonStr = jsonChars; - cJSON_free(jsonChars); cJSON_Delete(json); return jsonStr; } diff --git a/services/core/include/common/dp_services_constants.h b/services/core/include/common/dp_services_constants.h index 6760979b41485756d6d11679b5b3b2d3350f4198..6535cd338bc7bf9a14d01c6a71f811796693802e 100644 --- a/services/core/include/common/dp_services_constants.h +++ b/services/core/include/common/dp_services_constants.h @@ -29,9 +29,10 @@ 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 SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID; extern const std::string CREATE_DEVICE_PROFILE_TABLE_SQL; extern const std::string CREATE_DEVICE_PROFILE_TABLE_UNIQUE_INDEX_SQL; +extern const std::string CREATE_DEVICE_PROFILE_WISE_DEVICEID_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; diff --git a/services/core/include/data_share/settings_data_manager.h b/services/core/include/data_share/settings_data_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..73953d1bf8450f28e42c3d27102d9744278fad43 --- /dev/null +++ b/services/core/include/data_share/settings_data_manager.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_DP_SETTINGS_DATA_CONNECTOR_H +#define OHOS_DP_SETTINGS_DATA_CONNECTOR_H + +#include +#include +#include +#include + +#include "datashare_helper.h" + +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class SettingsDataManager { + DECLARE_SINGLE_INSTANCE(SettingsDataManager); +public: + int32_t Init(); + int32_t UnInit(); + + int32_t GetUserDefinedDeviceName(int32_t userId, std::string &deviceName); + int32_t SetUserDefinedDeviceName(const std::string &deviceName, int32_t userId); + int32_t GetDisplayDeviceName(int32_t userId, std::string &deviceName); + int32_t SetDisplayDeviceName(const std::string &deviceName, int32_t userId); + int32_t GetDeviceName(std::string &deviceName); +private: + int32_t GetValue(const std::string &tableName, int32_t userId, const std::string &key, std::string &value); + int32_t SetValue(const std::string &tableName, int32_t userId, const std::string &key, const std::string &value); + + std::string GetProxyUriStr(const std::string &tableName, int32_t userId); + std::shared_ptr CreateDataShareHelper(const std::string &proxyUri); + Uri MakeUri(const std::string &proxyUri, const std::string &key); + bool ReleaseDataShareHelper(std::shared_ptr helper); + + sptr GetRemoteObj(); + +private: + std::mutex remoteObjMtx_; + sptr remoteObj_; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_SETTINGS_DATA_CONNECTOR_H diff --git a/services/core/include/profiledatamanager/device_icon_info_dao.h b/services/core/include/profiledatamanager/device_icon_info_dao.h index 35c62d38a558e09d1e96e8c3a9befa5829cc7b6a..22aa3a37a16929a10ed882bc4a10db2ba1526eba 100644 --- a/services/core/include/profiledatamanager/device_icon_info_dao.h +++ b/services/core/include/profiledatamanager/device_icon_info_dao.h @@ -53,4 +53,4 @@ private: }; } // DistributedDeviceProfile } // OHOS -#endif // OHOS_DP_DEVICE_ICON_INFO_DAO_H \ No newline at end of file +#endif // OHOS_DP_DEVICE_ICON_INFO_DAO_H diff --git a/services/core/include/profiledatamanager/device_profile_dao.h b/services/core/include/profiledatamanager/device_profile_dao.h index d23763387675420c6cf584f798951b73bd76195b..bb869fc88a88600929a33e4bf50cdf5b806f2652 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/product_info_dao.h b/services/core/include/profiledatamanager/product_info_dao.h index b3bfb25e7e33f88a8763016d1174fe3fab474923..2a7ec213a292d1351875871198f7f4e490168e2a 100644 --- a/services/core/include/profiledatamanager/product_info_dao.h +++ b/services/core/include/profiledatamanager/product_info_dao.h @@ -52,4 +52,4 @@ private: }; } // DistributedDeviceProfile } // OHOS -#endif // OHOS_DP_PRODUCT_INFO_DAO_H \ No newline at end of file +#endif // OHOS_DP_PRODUCT_INFO_DAO_H 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 0000000000000000000000000000000000000000..335780aa6d2bbda49a1bd74827c650774f02e2ac --- /dev/null +++ b/services/core/include/profiledatamanager/profile_data_manager.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DP_PROFILE_DATA_MANAGER_H +#define OHOS_DP_PROFILE_DATA_MANAGER_H + +#include + +#include "kvstore_observer.h" + +#include "device_icon_info.h" +#include "device_icon_info_filter_options.h" +#include "device_profile.h" +#include "device_profile_dao.h" +#include "product_info.h" +#include "single_instance.h" +namespace OHOS { +namespace DistributedDeviceProfile { +class ProfileDataManager { + DECLARE_SINGLE_INSTANCE(ProfileDataManager); +public: + int32_t Init(); + int32_t UnInit(); + + int32_t PutDeviceProfileBatch(std::vector& deviceProfiles); + int32_t GetDeviceProfiles(DeviceProfileFilterOptions& options, + std::vector& deviceProfiles); + int32_t PutProductInfoBatch(const std::vector& productInfos); + int32_t PutDeviceIconInfoBatch(const std::vector& deviceIconInfos); + int32_t GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions, + std::vector& deviceIconInfos); + + int32_t PutDeviceProfile(DeviceProfile deviceProfile); +private: + bool FilterInvaildSymbol(std::string str); + int32_t PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo); + int32_t PutProductInfo(const ProductInfo& productInfo); +}; +} // DistributedDeviceProfile +} // OHOS +#endif // OHOS_DP_PROFILE_DATA_MANAGER_H diff --git a/services/core/src/common/dp_services_constants.cpp b/services/core/src/common/dp_services_constants.cpp index 8f4e6e1d0792f85e2b988af06875d202a17e7b58..8950a86533439ac0102680000245972a2824bd49 100644 --- a/services/core/src/common/dp_services_constants.cpp +++ b/services/core/src/common/dp_services_constants.cpp @@ -25,41 +25,42 @@ const std::string PROFILE_DATA_RDB_PATH = "/data/service/el2/public/database/dis 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 SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID = + "SELECT * FROM device_profile WHERE deviceId = ? AND userId = ?"; +const std::string CREATE_DEVICE_PROFILE_TABLE_SQL = +"CREATE TABLE IF NOT EXISTS device_profile (\n" +" id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +" deviceId TEXT NOT NULL,\n" +" userId INTEGER,\n" +" accountId TEXT,\n" +" productId TEXT,\n" +" subProductId TEXT,\n" +" model TEXT,\n" +" devType TEXT,\n" +" deviceName TEXT,\n" +" manu TEXT,\n" +" sn TEXT,\n" +" hiv TEXT,\n" +" mac TEXT,\n" +" bleMac TEXT,\n" +" brMac TEXT,\n" +" sleMac TEXT,\n" +" fwv TEXT,\n" +" hwv TEXT,\n" +" swv TEXT,\n" +" protType INTEGER,\n" +" setupType INTEGER,\n" +" wiseUserId TEXT,\n" +" wiseDeviceId TEXT,\n" +" registerTime TEXT,\n" +" modifyTime TEXT,\n" +" shareTime TEXT,\n" +" innerModel TEXT\n" +")"; 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,\ - productId);"; +"CREATE UNIQUE INDEX IF NOT EXISTS unique_device_profile ON device_profile (deviceId, userId)"; +const std::string CREATE_DEVICE_PROFILE_WISE_DEVICEID_INDEX_SQL = +"CREATE INDEX IF NOT EXISTS index_wiseDeviceId ON device_profile (wiseDeviceId)"; // DeviceIconInfoDao const std::string CREATE_DEVICE_ICON_INFO_TABLE_SQL = "CREATE TABLE IF NOT EXISTS device_icon_info\ (id INTEGER PRIMARY KEY AUTOINCREMENT,\ @@ -121,8 +122,8 @@ const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_SQL = "CREATE TABLE IF NOT REFERENCES service_profile (id)\ );"; const std::string CREATE_CHARACTERISTIC_PROFILE_TABLE_INDEX_SQL_CHARACTERISTIC_KEY = - "CREATE INDEX characteristicKey_idx ON characteristic_profile (characteristicKey);"; +"CREATE INDEX IF NOT EXISTS index_characteristicKey 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 diff --git a/services/core/src/datashare/settings_data_manager.cpp b/services/core/src/datashare/settings_data_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfbc8b7f16396c04394b7fc0c35b8303790e780b --- /dev/null +++ b/services/core/src/datashare/settings_data_manager.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "settings_data_manager.h" + +#include "iservice_registry.h" +#include "system_ability_definition.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(SettingsDataManager) +namespace { +const std::string TAG = "SettingsDataManager"; +constexpr int32_t USERID_HELPER_NUMBER = 100; +const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/"; +const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; +const std::string URI_PROXY_SUFFIX = "?Proxy=true"; +const std::string SETTINGSDATA_GLOBAL = "SETTINGSDATA"; +const std::string SETTINGS_GENERAL_DEVICE_NAME = "settings.general.device_name"; +const std::string SETTINGSDATA_SYSTEM = "USER_SETTINGSDATA_"; +const std::string SETTINGSDATA_SECURE = "USER_SETTINGSDATA_SECURE_"; +const std::string SETTINGS_GENERAL_DISPLAY_DEVICE_NAME = "settings.general.display_device_name"; +const std::string SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME = "settings.general.user_defined_device_name"; + +const std::string SETTING_COLUMN_VALUE = "VALUE"; +const std::string SETTING_COLUMN_KEYWORD = "KEYWORD"; +} + +int32_t SettingsDataManager::Init() +{ + GetRemoteObj(); + return DP_SUCCESS; +} + +int32_t SettingsDataManager::UnInit() +{ + std::lock_guard lock(remoteObjMtx_); + remoteObj_ = nullptr; + return DP_SUCCESS; +} + +int32_t SettingsDataManager::GetUserDefinedDeviceName(int32_t userId, std::string &deviceName) +{ + return GetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME, deviceName); +} + +int32_t SettingsDataManager::SetUserDefinedDeviceName(const std::string &deviceName, int32_t userId) +{ + return SetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME, deviceName); +} + +int32_t SettingsDataManager::GetDisplayDeviceName(int32_t userId, std::string &deviceName) +{ + return GetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_DISPLAY_DEVICE_NAME, deviceName); +} + +int32_t SettingsDataManager::SetDisplayDeviceName(const std::string &deviceName, int32_t userId) +{ + return SetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_DISPLAY_DEVICE_NAME, deviceName); +} + +int32_t SettingsDataManager::GetDeviceName(std::string &deviceName) +{ + return GetValue(SETTINGSDATA_GLOBAL, 0, SETTINGS_GENERAL_DEVICE_NAME, deviceName); +} + +sptr SettingsDataManager::GetRemoteObj() +{ + std::lock_guard lock(remoteObjMtx_); + if (remoteObj_ != nullptr) { + return remoteObj_; + } + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + HILOGE("get sa manager return nullptr"); + return nullptr; + } + auto remoteObj = samgr->GetSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID); + if (remoteObj == nullptr) { + HILOGE("get system ability failed, id=%{public}d", DISTRIBUTED_DEVICE_PROFILE_SA_ID); + return nullptr; + } + remoteObj_ = remoteObj; + return remoteObj_; +} + +int32_t SettingsDataManager::GetValue(const std::string &tableName, int32_t userId, + const std::string &key, std::string &value) +{ + std::string proxyUri = GetProxyUriStr(tableName, userId); + auto helper = CreateDataShareHelper(proxyUri); + if (helper == nullptr) { + HILOGE("helper is nullptr"); + return DP_NULLPTR; + } + std::vector columns = { SETTING_COLUMN_VALUE }; + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); + Uri uri = MakeUri(proxyUri, key); + auto resultSet = helper->Query(uri, predicates, columns); + ReleaseDataShareHelper(helper); + if (resultSet == nullptr) { + HILOGE("Query failed key=%{public}s", key.c_str()); + return DP_NULLPTR; + } + int32_t count = ROWCOUNT_INIT; + resultSet->GetRowCount(count); + if (count == 0) { + HILOGW("no value, key=%{public}s", key.c_str()); + resultSet->Close(); + return DP_SUCCESS; + } + int32_t index = 0; + resultSet->GoToRow(index); + int32_t ret = resultSet->GetString(index, value); + if (ret != DataShare::E_OK) { + HILOGE("get value failed, ret=%{public}d", ret); + resultSet->Close(); + return ret; + } + resultSet->Close(); + return DP_SUCCESS; +} + +int32_t SettingsDataManager::SetValue(const std::string &tableName, int32_t userId, + const std::string &key, const std::string &value) +{ + std::string proxyUri = GetProxyUriStr(tableName, userId); + auto helper = CreateDataShareHelper(proxyUri); + if (helper == nullptr) { + HILOGE("helper is nullptr"); + return DP_NULLPTR; + } + + DataShare::DataShareValuesBucket val; + val.Put(SETTING_COLUMN_KEYWORD, key); + val.Put(SETTING_COLUMN_VALUE, value); + Uri uri = MakeUri(proxyUri, key); + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); + int32_t ret = helper->Update(uri, predicates, val); + if (ret <= 0) { + HILOGW("Update failed, ret=%{public}d", ret); + ret = helper->Insert(uri, val); + } + ReleaseDataShareHelper(helper); + if (ret <= 0) { + HILOGE("set value failed, ret=%{public}d", ret); + return ret; + } + return ret; +} + +std::shared_ptr SettingsDataManager::CreateDataShareHelper(const std::string &proxyUri) +{ + if (proxyUri.empty()) { + HILOGE("proxyUri is empty"); + return nullptr; + } + auto [ret, helper] = DataShare::DataShareHelper::Create(GetRemoteObj(), proxyUri, SETTINGS_DATA_EXT_URI); + if (ret != 0) { + HILOGE("create helper failed ret %{public}d", ret); + return nullptr; + } + return helper; +} + +std::string SettingsDataManager::GetProxyUriStr(const std::string &tableName, int32_t userId) +{ + if (userId < USERID_HELPER_NUMBER) { + userId = USERID_HELPER_NUMBER; + } + if (tableName == SETTINGSDATA_GLOBAL) { + return SETTING_URI_PROXY + SETTINGSDATA_GLOBAL + URI_PROXY_SUFFIX; + } else { + return SETTING_URI_PROXY + tableName + std::to_string(userId) + URI_PROXY_SUFFIX; + } +} + +Uri SettingsDataManager::MakeUri(const std::string &proxyUri, const std::string &key) +{ + if (proxyUri.empty() || key.empty()) { + HILOGE("Invalid parameter."); + } + Uri uri(proxyUri + "&key=" + key); + return uri; +} + +bool SettingsDataManager::ReleaseDataShareHelper(std::shared_ptr helper) +{ + if (helper == nullptr) { + HILOGE("helper is nullptr"); + return false; + } + if (!helper->Release()) { + HILOGE("release helper fail"); + return false; + } + return true; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS 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 33c322dccb57b73575f55503fc3ac7db7aac440d..99f7b7809d5dc9b4a0fb020011ebac2bf81c3f9a 100644 --- a/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp +++ b/services/core/src/persistenceadapter/rdbadapter/profile_data_rdb_adapter.cpp @@ -34,7 +34,8 @@ namespace { "device_profile", "service_profile", "characteristic_profile", - "device_icon" + "device_icon_info", + "product_info" }; const std::string TAG = "ProfileDatardbAdapter"; } diff --git a/services/core/src/profiledatamanager/device_icon_info_dao.cpp b/services/core/src/profiledatamanager/device_icon_info_dao.cpp index 8025d160f03f443a39f51581fb1f7eac6b42066e..ac28b0e45356dc35b694a8285cd4724d7e0b30cb 100644 --- a/services/core/src/profiledatamanager/device_icon_info_dao.cpp +++ b/services/core/src/profiledatamanager/device_icon_info_dao.cpp @@ -149,7 +149,6 @@ int32_t DeviceIconInfoDao::UpdateDeviceIconInfo(const DeviceIconInfo& deviceIcon 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.c_str()); @@ -160,7 +159,6 @@ int32_t DeviceIconInfoDao::CreateTable() 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.c_str()); @@ -180,7 +178,7 @@ bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOpt sql += "?,"; condition.emplace_back(ValueObject(prodId)); } - sql.erase(sql.end() - 1); + sql.erase(sql.end() - 1, sql.end()); sql += ") AND "; flag = true; } @@ -200,7 +198,7 @@ bool DeviceIconInfoDao::CreateQuerySqlAndCondition(const DeviceIconInfoFilterOpt flag = true; } if (flag) { - sql.erase(sql.end() - NUM_4); + sql.erase(sql.end() - NUM_4, sql.end()); return flag; } return flag; @@ -241,4 +239,4 @@ int32_t DeviceIconInfoDao::ConvertToDeviceIconInfo(std::shared_ptr re 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 cca83ee48010aef9bcacdd8f8bb2305a402e1f22..2f34521c1b5fbd2e316c894f2a536e6f2efd16df 100644 --- a/services/core/src/profiledatamanager/device_profile_dao.cpp +++ b/services/core/src/profiledatamanager/device_profile_dao.cpp @@ -78,9 +78,9 @@ int32_t DeviceProfileDao::PutDeviceProfile(const DeviceProfile& deviceProfile) } } std::shared_ptr resultSet = ProfileDataRdbAdapter::GetInstance().Get( - SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID_ACCOUNTID, + SELECT_DEVICE_PROFILE_TABLE_WHERE_DEVID_USERID, std::vector{ ValueObject(deviceProfile.GetDeviceId()), - ValueObject(deviceProfile.GetUserId()), ValueObject(deviceProfile.GetAccountId())}); + ValueObject(deviceProfile.GetUserId())}); if (resultSet == nullptr) { HILOGE("resultSet is nullptr"); return DP_GET_RESULTSET_FAIL; @@ -94,7 +94,6 @@ int32_t DeviceProfileDao::PutDeviceProfile(const DeviceProfile& deviceProfile) profile.SetId(id); } resultSet->Close(); - SubscribeProfileManager::GetInstance().NotifyDeviceProfileAdd(profile); std::string localUdid = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); if (localUdid == deviceProfile.GetDeviceId()) { DeviceProfileManager::GetInstance().PutDeviceProfile(profile); @@ -146,12 +145,11 @@ int32_t DeviceProfileDao::DeleteDeviceProfile(const DeviceProfile &deviceProfile 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) +int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &newProfile) { ValuesBucket values; DeviceProfileToEntries(newProfile, values); @@ -166,7 +164,6 @@ int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &oldProfile, c 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); @@ -177,7 +174,6 @@ int32_t DeviceProfileDao::UpdateDeviceProfile(const DeviceProfile &oldProfile, c 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"); @@ -188,12 +184,16 @@ int32_t DeviceProfileDao::CreateTable() 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; } + ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_DEVICE_PROFILE_WISE_DEVICEID_INDEX_SQL); + if (ret != DP_SUCCESS) { + HILOGE("device_profile wiseDeviceId index create failed"); + return DP_CREATE_INDEX_FAIL; + } return DP_SUCCESS; } @@ -228,10 +228,10 @@ void DeviceProfileDao::CreateQuerySqlAndCondition(const DeviceProfileFilterOptio matchCondition = true; } if (matchCondition) { - sql.erase(sql.end() - AND_LENGTH); + sql.erase(sql.end() - AND_LENGTH, sql.end()); return; } - sql.erase(sql.end() - WHERE_LENGTH); + sql.erase(sql.end() - WHERE_LENGTH, sql.end()); return; } @@ -253,13 +253,16 @@ int32_t DeviceProfileDao::DeviceProfileToEntries(const DeviceProfile &deviceProf 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(INNER_MODEL, deviceProfile.GetInnerModel()); + values.PutString(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()); + values.PutString(BLE_MAC, deviceProfile.GetBleMac()); + values.PutString(BR_MAC, deviceProfile.GetBrMac()); + values.PutString(SLE_MAC, deviceProfile.GetSleMac()); + values.PutInt(SETUP_TYPE, deviceProfile.GetSetupType()); return DP_SUCCESS; } @@ -292,13 +295,16 @@ int32_t DeviceProfileDao::ConvertToDeviceProfile( 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.SetInnerModel(rowEntity.Get(INNER_MODEL)); 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)); + deviceProfile.SetBleMac(rowEntity.Get(BLE_MAC)); + deviceProfile.SetBrMac(rowEntity.Get(BR_MAC)); + deviceProfile.SetSleMac(rowEntity.Get(SLE_MAC)); + deviceProfile.SetSetupType(rowEntity.Get(SETUP_TYPE)); return DP_SUCCESS; } @@ -309,7 +315,7 @@ void DeviceProfileDao::GenerateSqlAndCondition(const std::vector ¶m sql += "?,"; condition.emplace_back(ValueObject(param)); } - sql.erase(sql.end() - 1); + sql.erase(sql.end() - 1, sql.end()); sql += ") AND "; return; } @@ -321,9 +327,9 @@ void DeviceProfileDao::GenerateSqlAndCondition(const std::vector &p sql += "?,"; condition.emplace_back(ValueObject(param)); } - sql.erase(sql.end() - 1); + sql.erase(sql.end() - 1, sql.end()); sql += ") AND "; return; } } // namespace DistributedDeviceProfile -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/core/src/profiledatamanager/product_info_dao.cpp b/services/core/src/profiledatamanager/product_info_dao.cpp index 6ae2c0497034d1e8441ee56e22ec9f7692f42a87..2d0016927020965956e19e32b78b9d5bd164414f 100644 --- a/services/core/src/profiledatamanager/product_info_dao.cpp +++ b/services/core/src/profiledatamanager/product_info_dao.cpp @@ -149,7 +149,6 @@ int32_t ProductInfoDao::UpdateProductInfo(const ProductInfo& productInfo) 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.c_str()); @@ -174,7 +173,7 @@ bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector& sql += "?,"; condition.emplace_back(ValueObject(prodId)); } - sql.erase(sql.end() - 1); + sql.erase(sql.end() - 1, sql.end()); sql += ")"; flag = true; } @@ -210,4 +209,4 @@ int32_t ProductInfoDao::ConvertToProductInfo(std::shared_ptr resultSe return DP_SUCCESS; } } // 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 new file mode 100644 index 0000000000000000000000000000000000000000..b3f2eeadf9bce63da039225b0f96addb25a0d640 --- /dev/null +++ b/services/core/src/profiledatamanager/profile_data_manager.cpp @@ -0,0 +1,274 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "profile_data_manager.h" + +#include "device_profile_manager.h" +#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 "multi_user_manager.h" +#include "product_info_dao.h" +#include "profile_cache.h" +#include "profile_utils.h" +#include "settings_data_manager.h" +#include "content_sensor_manager_utils.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ProfileDataManager) + +namespace { +const std::string TAG = "ProfileDataManager"; +} + +int32_t ProfileDataManager::Init() +{ + if (DeviceProfileDao::GetInstance().Init() != DP_SUCCESS) { + HILOGE("DeviceProfileDao init failed"); + return DP_DEVICE_PROFILE_DAO_INIT_FAIL; + } + if (ProductInfoDao::GetInstance().Init() != DP_SUCCESS) { + HILOGE("ProductInfoDao init failed"); + return DP_PRODUCT_INFO_DAO_INIT_FAIL; + } + if (DeviceIconInfoDao::GetInstance().Init() != DP_SUCCESS) { + HILOGE("DeviceIconInfoDao init failed"); + return DP_DEVICE_ICON_INFO_DAO_INIT_FAIL; + } + if (SettingsDataManager::GetInstance().Init() != DP_SUCCESS) { + HILOGE("SettingsDataManager init failed"); + return DP_SETTINGSDATA_MANAGER_INIT_FAIL; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::UnInit() +{ + if (DeviceProfileDao::GetInstance().UnInit() != DP_SUCCESS) { + HILOGE("DeviceProfileDao unInit failed"); + return DP_DEVICE_PROFILE_DAO_UNINIT_FAIL; + } + if (ProductInfoDao::GetInstance().UnInit() != DP_SUCCESS) { + HILOGE("ProductInfoDao unInit failed"); + return DP_PRODUCT_INFO_DAO_UNINIT_FAIL; + } + if (DeviceIconInfoDao::GetInstance().UnInit() != DP_SUCCESS) { + HILOGE("DeviceIconInfoDao unInit failed"); + return DP_DEVICE_ICON_INFO_DAO_UNINIT_FAIL; + } + if (SettingsDataManager::GetInstance().UnInit() != DP_SUCCESS) { + HILOGE("SettingsDataManager unInit failed"); + return DP_SETTINGSDATA_MANAGER_UNINIT_FAIL; + } + return 0; +} + +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()); + return DP_INVALID_PARAMS; + } + deviceProfile.SetUserId(MultiUserManager::GetInstance().GetCurrentForegroundUserID()); + deviceProfile.SetAccountId(ProfileCache::GetInstance().GetLocalAccountId()); + DeviceProfileFilterOptions filterOptions; + filterOptions.AddDeviceIds(deviceProfile.GetDeviceId()); + filterOptions.SetUserId(deviceProfile.GetUserId()); + std::vector oldDeviceProfiles; + int32_t ret = RET_INIT; + ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(filterOptions, oldDeviceProfiles); + if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) { + HILOGE("GetDeviceProfiles failed,ret=%{public}d", ret); + return ret; + } + if (oldDeviceProfiles.empty()) { + HILOGI("oldDeviceProfiles is mepty"); + deviceProfile.SetId(DEFAULT_ID); + } else { + DeviceProfile oldDeviceProfile = oldDeviceProfiles[0]; + deviceProfile.SetId(oldDeviceProfile.GetId()); + } + 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; + } + 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::GetDeviceProfiles(DeviceProfileFilterOptions& options, + std::vector& deviceProfiles) +{ + int32_t localUserId = MultiUserManager::GetInstance().GetCurrentForegroundUserID(); + options.SetUserId(localUserId); + int32_t ret = DeviceProfileDao::GetInstance().GetDeviceProfiles(options, deviceProfiles); + if (ret != DP_SUCCESS) { + HILOGE("GetDeviceProfile failed,ret=%{public}d", ret); + return ret; + } + if (deviceProfiles.empty()) { + HILOGE("GetDeviceProfile failed,deviceProfiles is empty"); + return DP_DEVICE_PROFILE_NOT_FOUND; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions, + std::vector& deviceIconInfos) +{ + if (filterOptions.GetProductIds().empty() || filterOptions.GetSubProductId().empty() || + filterOptions.GetImageType().empty() || filterOptions.GetSpecName().empty()) { + HILOGE("Invalid parameter"); + return DP_INVALID_PARAM; + } + int32_t ret = DeviceIconInfoDao::GetInstance().GetDeviceIconInfos(filterOptions, deviceIconInfos); + if (ret != DP_SUCCESS) { + HILOGE("GetDeviceIconInfos failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::PutDeviceIconInfoBatch(const std::vector& deviceIconInfos) +{ + if (deviceIconInfos.empty()) { + HILOGE("ProductInfos is empty"); + return DP_INVALID_PARAM; + } + for (auto &deviceIconInfo : deviceIconInfos) { + int32_t ret = PutDeviceIconInfo(deviceIconInfo); + if (ret != DP_SUCCESS) { + HILOGE("PutDeviceIconInfo is failed,ret=%{public}d", ret); + return ret; + } + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::PutDeviceIconInfo(const DeviceIconInfo& deviceIconInfo) +{ + DeviceIconInfoFilterOptions filterOptions; + std::vector productIds; + productIds.emplace_back(deviceIconInfo.GetProductId()); + filterOptions.SetProductIds(productIds); + filterOptions.SetSubProductId(deviceIconInfo.GetSubProductId()); + filterOptions.SetImageType(deviceIconInfo.GetImageType()); + filterOptions.SetSpecName(deviceIconInfo.GetSpecName()); + std::vector deviceIconInfos; + int32_t ret = DeviceIconInfoDao::GetInstance().GetDeviceIconInfos(filterOptions, deviceIconInfos); + if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) { + HILOGE("GetDeviceIconInfos failed,ret=%{public}d", ret); + return ret; + } + if (deviceIconInfos.empty()) { + ret = DeviceIconInfoDao::GetInstance().PutDeviceIconInfo(deviceIconInfo); + } else { + DeviceIconInfo updateDeviceIconInfo = deviceIconInfo; + updateDeviceIconInfo.SetId(deviceIconInfos[0].GetId()); + ret = DeviceIconInfoDao::GetInstance().UpdateDeviceIconInfo(updateDeviceIconInfo); + } + if (ret != DP_SUCCESS) { + HILOGE("PutDeviceIconInfo failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::PutProductInfoBatch(const std::vector& productInfos) +{ + if (productInfos.empty()) { + HILOGE("ProductInfos is empty"); + return DP_INVALID_PARAM; + } + for (auto &productInfo : productInfos) { + int32_t ret = PutProductInfo(productInfo); + if (ret != DP_SUCCESS) { + HILOGE("PutProductInfo is failed,ret=%{public}d", ret); + return ret; + } + } + return DP_SUCCESS; +} + +int32_t ProfileDataManager::PutProductInfo(const ProductInfo& productInfo) +{ + if (productInfo.GetProductId().empty()) { + HILOGE("ProductId is nullptr"); + return DP_INVALID_PARAM; + } + std::vector productIds; + productIds.emplace_back(productInfo.GetProductId()); + std::vector productInfos; + int32_t ret = ProductInfoDao::GetInstance().GetProductInfos(productIds, productInfos); + if ((ret != DP_SUCCESS) && (ret != DP_NOT_FIND_DATA)) { + HILOGE("GetProductInfos failed,ret=%{public}d", ret); + return ret; + } + if (productInfos.empty()) { + ret = ProductInfoDao::GetInstance().PutProductInfo(productInfo); + } else { + ret = ProductInfoDao::GetInstance().UpdateProductInfo(productInfo); + } + if (ret != DP_SUCCESS) { + HILOGE("PutProductInfo failed,ret=%{public}d", ret); + return ret; + } + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS