From 00138a0d7b6e7936022e1759268d66e4973a5787 Mon Sep 17 00:00:00 2001 From: chenshuo Date: Sat, 18 May 2024 02:47:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=88=86=E7=BA=A7=E9=A2=84?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenshuo --- bundle.json | 1 + services/core/BUILD.gn | 1 + .../switch_status_collector.h | 36 ++ .../dynamic_profile_manager.h | 75 +++ .../static_profile_manager.h | 47 ++ .../switch_profile_manager.h | 60 +++ .../kvadapter/switch_adapter.h | 49 ++ .../switch_status_collector.cpp | 158 +++++++ .../dynamic_profile_manager.cpp | 434 ++++++++++++++++++ .../static_profile_manager.cpp | 182 ++++++++ .../switch_profile_manager.cpp | 135 ++++++ .../kvadapter/switch_adapter.cpp | 118 +++++ 12 files changed, 1296 insertions(+) create mode 100644 services/core/include/contentsensormanager/switch_status_collector.h create mode 100644 services/core/include/deviceprofilemanager/dynamic_profile_manager.h create mode 100644 services/core/include/deviceprofilemanager/static_profile_manager.h create mode 100644 services/core/include/deviceprofilemanager/switch_profile_manager.h create mode 100644 services/core/include/persistenceadapter/kvadapter/switch_adapter.h create mode 100644 services/core/src/contentsensormanager/switch_status_collector.cpp create mode 100644 services/core/src/deviceprofilemanager/dynamic_profile_manager.cpp create mode 100644 services/core/src/deviceprofilemanager/static_profile_manager.cpp create mode 100644 services/core/src/deviceprofilemanager/switch_profile_manager.cpp create mode 100644 services/core/src/persistenceadapter/kvadapter/switch_adapter.cpp diff --git a/bundle.json b/bundle.json index c58a953b..cc0858d3 100644 --- a/bundle.json +++ b/bundle.json @@ -23,6 +23,7 @@ "components": [ "cJSON", "c_utils", + "config_policy", "hisysevent", "hilog", "ipc", diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index 8ab4d41a..92e2ec5e 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -87,6 +87,7 @@ ohos_shared_library("distributed_device_profile_svr") { "access_token:libaccesstoken_sdk", "cJSON:cjson", "c_utils:utils", + "config_policy:configpolicy_util", "device_auth:deviceauth_sdk", "device_manager:devicemanagersdk", "dmsfwk:common_sdk", diff --git a/services/core/include/contentsensormanager/switch_status_collector.h b/services/core/include/contentsensormanager/switch_status_collector.h new file mode 100644 index 00000000..99eb4665 --- /dev/null +++ b/services/core/include/contentsensormanager/switch_status_collector.h @@ -0,0 +1,36 @@ +/* + * 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_SWITCH_STATUS_TASK_H +#define OHOS_DP_PROFILE_SWITCH_STATUS_TASK_H + +#include +#include "cJSON.h" +#include "collector.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class SwitchStatusCollector : public Collector { +public: + bool ConvertToProfile(std::vector& charProfileList) override; +private: + int32_t LoadJsonFile(const std::string& filePath, std::string& fileContent); + int32_t GenerateSwitchProfiles(const cJSON* const staticInfoJson, + std::vector &charProfileList); + void AddSwitchStatusToDB(std::vector& charProfileList); +}; +} // namespace DeviceProfile +} // namespace OHOS +#endif // OHOS_DP_PROFILE_SWITCH_STATUS_TASK_H \ No newline at end of file diff --git a/services/core/include/deviceprofilemanager/dynamic_profile_manager.h b/services/core/include/deviceprofilemanager/dynamic_profile_manager.h new file mode 100644 index 00000000..a076d265 --- /dev/null +++ b/services/core/include/deviceprofilemanager/dynamic_profile_manager.h @@ -0,0 +1,75 @@ +/* + * 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_DYNAMIC_PROFILE_MANAGER_H +#define OHOS_DP_DYNAMIC_PROFILE_MANAGER_H + +#include + +#include "single_instance.h" +#include "device_profile.h" +#include "service_profile.h" +#include "characteristic_profile.h" +#include "dp_sync_options.h" +#include "iremote_object.h" +#include "i_sync_completed_callback.h" +#include "ikv_adapter.h" +#include "kv_data_change_listener.h" +#include "kv_sync_completed_listener.h" +#include "kv_store_death_recipient.h" +#include "i_dp_sync_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DynamicProfileManager { +DECLARE_SINGLE_INSTANCE(DynamicProfileManager); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t ReInit(); + int32_t PutDeviceProfile(const DeviceProfile& deviceProfile); + int32_t PutServiceProfile(const ServiceProfile& serviceProfile); + int32_t PutServiceProfileBatch(const std::vector& serviceProfiles); + int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile); + int32_t PutCharacteristicProfileBatch(const std::vector& charProfiles); + int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); + int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, + ServiceProfile& serviceProfile); + int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey, CharacteristicProfile& charProfile); + int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName); + int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey); + int32_t GetAllDeviceProfile(std::vector& deviceProfiles); + int32_t GetAllServiceProfile(std::vector& serviceProfiles); + int32_t GetAllCharacteristicProfile(std::vector& charProfiles); + int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, + sptr syncCompletedCallback); + int32_t DeviceOnlineAutoSync(const std::string& peerNetworkId); + +private: + bool LoadDpSyncAdapter(); + void UnloadDpSyncAdapter(); + int32_t RunloadedFunction(std::string deviceId, sptr syncCompletedCallback); + bool isAdapterSoLoaded_ = false; + std::mutex isAdapterLoadLock_; + std::mutex dynamicStoreMutex_; + std::shared_ptr dynamicProfileStore_ = nullptr; + std::shared_ptr dpSyncAdapter_; +}; +} // namespace DeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DYNAMIC_PROFILE_MANAGER_H diff --git a/services/core/include/deviceprofilemanager/static_profile_manager.h b/services/core/include/deviceprofilemanager/static_profile_manager.h new file mode 100644 index 00000000..b6e4b2cd --- /dev/null +++ b/services/core/include/deviceprofilemanager/static_profile_manager.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_STATIC_PROFILE_MANAGER_H +#define OHOS_DP_STATIC_PROFILE_MANAGER_H + +#include "characteristic_profile.h" +#include "kv_adapter.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class StaticProfileManager { +DECLARE_SINGLE_INSTANCE(StaticProfileManager); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t ReInit(); + int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile); + int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey, CharacteristicProfile& charProfile); + int32_t GetAllCharacteristicProfile(std::vector& staticCapabilityProfiles); + +private: + int32_t GenerateStaticInfoProfile(const CharacteristicProfile& staticCapabilityProfile, + std::unordered_map& staticInfoProfiles); + +private: + std::mutex staticStoreMutex_; + std::shared_ptr staticProfileStore_ = nullptr; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_STATIC_PROFILE_MANAGER_H diff --git a/services/core/include/deviceprofilemanager/switch_profile_manager.h b/services/core/include/deviceprofilemanager/switch_profile_manager.h new file mode 100644 index 00000000..92ddc335 --- /dev/null +++ b/services/core/include/deviceprofilemanager/switch_profile_manager.h @@ -0,0 +1,60 @@ +/* + * 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_SWITCH_PROFILE_MANAGER_H +#define OHOS_DP_SWITCH_PROFILE_MANAGER_H + +#include "single_instance.h" + +#include +#include +#include + +#include "single_instance.h" +#include "device_profile.h" +#include "service_profile.h" +#include "characteristic_profile.h" +#include "distributed_kv_data_manager.h" +#include "dp_sync_options.h" +#include "iremote_object.h" +#include "i_sync_completed_callback.h" +#include "ikv_adapter.h" +#include "kv_data_change_listener.h" +#include "i_dp_sync_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class SwitchProfileManager { +DECLARE_SINGLE_INSTANCE(SwitchProfileManager); + +public: + int32_t Init(); + int32_t UnInit(); + int32_t ReInit(); + int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile); + int32_t PutCharacteristicProfileBatch(const std::vector& charProfiles); + int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey, CharacteristicProfile& charProfile); + int32_t RefreshLocalSwitchProfile(); + +private: + int32_t GenerateSwitchInfoProfile(const CharacteristicProfile& switchProfile, + std::unordered_map switchProfileMap); + std::mutex switchProfileMutex_; + std::shared_ptr switchProfileStore_ = nullptr; +}; +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_SWITCH_PROFILE_MANAGER_H diff --git a/services/core/include/persistenceadapter/kvadapter/switch_adapter.h b/services/core/include/persistenceadapter/kvadapter/switch_adapter.h new file mode 100644 index 00000000..d2e2ffaa --- /dev/null +++ b/services/core/include/persistenceadapter/kvadapter/switch_adapter.h @@ -0,0 +1,49 @@ +/* + * 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_SWITCH_ADAPTER_H +#define OHOS_DP_SWITCH_ADAPTER_H + +#include +#include +#include + +#include "distributed_kv_data_manager.h" +#include "kv_adapter.h" +#include "kvstore_observer.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class SwitchAdapter { +DECLARE_SINGLE_INSTANCE(SwitchAdapter); + +public: + void Init(); + void Uninit(); + int32_t PutSwitch(const std::string& appId, uint32_t value, uint16_t length); + int32_t GetSwitch(const std::string& appId, const std::string& networkId, uint32_t& value); + int32_t SubscribeSwitchData(const std::string& appId); + int32_t UnsubscribeSwitchData(const std::string& appId); + +private: + std::shared_ptr observer_; + DistributedKv::DistributedKvDataManager kvDataMgr_; + std::mutex switchAdapterMutex_; +}; + +} // namespace DistributedDeviceProfile +} // namespace OHOS +#endif // OHOS_DP_SWITCH_ADAPTER_H diff --git a/services/core/src/contentsensormanager/switch_status_collector.cpp b/services/core/src/contentsensormanager/switch_status_collector.cpp new file mode 100644 index 00000000..9c722151 --- /dev/null +++ b/services/core/src/contentsensormanager/switch_status_collector.cpp @@ -0,0 +1,158 @@ +/* + * 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 "switch_status_collector.h" +#include +#include "cJSON.h" +#include "config_policy_utils.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" +#include "profile_cache.h" +#include "profile_utils.h" +#include "switch_profile_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { +const std::string TAG = "SwitchStatusCollector"; +const std::string APP_ID = "distributed_device_profile_service"; +} + +bool SwitchStatusCollector::ConvertToProfile(std::vector& charProfileList) +{ + HILOGI("call!"); + std::string fileContent = ""; + int32_t loadJsonResult = LoadJsonFile(SWITCH_CAPABILITY_PATH, fileContent); + if (loadJsonResult != DP_SUCCESS) { + HILOGE("Load json failed, result: %{public}d!", loadJsonResult); + return false; + } + HILOGI("fileContent: %{public}s", fileContent.c_str()); + cJSON* switchCapabilityJson = cJSON_Parse(fileContent.c_str()); + if (!cJSON_IsObject(switchCapabilityJson)) { + HILOGE("Switch capability json parse failed!"); + cJSON_Delete(switchCapabilityJson); + return false; + } + // the charProfileList will be write to dynamic kv_store by ContentSensorManager + // so, create anther vector of ChararcteristicProfile + std::vector switchCharProfileList; + int32_t generateProfilesRes = GenerateSwitchProfiles(switchCapabilityJson, switchCharProfileList); + if (generateProfilesRes != DP_SUCCESS) { + HILOGE("Generate switch profiles result %{public}d!", generateProfilesRes); + cJSON_Delete(switchCapabilityJson); + return false; + } + HILOGI("Convert to profile success!"); + cJSON_Delete(switchCapabilityJson); + AddSwitchStatusToDB(switchCharProfileList); + return true; +} + +int32_t SwitchStatusCollector::LoadJsonFile(const std::string& filePath, std::string& fileContent) +{ + HILOGI("call!"); + if (filePath.empty() || filePath.size() > MAX_STRING_LEN) { + HILOGE("filePath is invalid!"); + return DP_INVALID_PARAM; + } + char buf[MAX_PATH_LEN] = {0}; + char targetPath[PATH_MAX + 1] = {0x00}; + char *srcPath = GetOneCfgFile(filePath.c_str(), buf, MAX_PATH_LEN); + if (srcPath == nullptr) { + HILOGE("srcPath is invalid!"); + return DP_LOAD_JSON_FILE_FAIL; + } + if (strlen(srcPath) == 0 || strlen(srcPath) > PATH_MAX || realpath(srcPath, targetPath) == nullptr) { + HILOGE("File canonicalization failed!"); + return DP_LOAD_JSON_FILE_FAIL; + } + std::ifstream ifs(targetPath); + if (!ifs.is_open()) { + HILOGE("load json file failed"); + return DP_LOAD_JSON_FILE_FAIL; + } + fileContent = std::string(std::istreambuf_iterator{ifs}, std::istreambuf_iterator{}); + ifs.close(); + return DP_SUCCESS; +} + +int32_t SwitchStatusCollector::GenerateSwitchProfiles(const cJSON* const staticInfoJson, + std::vector& charProfileList) +{ + if (!cJSON_IsObject(staticInfoJson)) { + HILOGE("staticInfoJson is not object!"); + return DP_GET_SWITCH_INFO_FAIL; + } + cJSON* abilitiesJson = cJSON_GetObjectItemCaseSensitive(staticInfoJson, SWITCH_CALLERS.c_str()); + if (!cJSON_IsArray(abilitiesJson)) { + HILOGE("abilitiesJson is not array!"); + return DP_GET_SWITCH_INFO_FAIL; + } + cJSON* abilityItem = NULL; + cJSON_ArrayForEach(abilityItem, abilitiesJson) { + if (!cJSON_IsObject(abilityItem)) { + HILOGE("abilityItem is not object!"); + continue; + } + cJSON* abilityKeyItem = cJSON_GetObjectItemCaseSensitive(abilityItem, SWITCH_SERVICE_NAMES.c_str()); + if (!cJSON_IsString(abilityKeyItem) || abilityKeyItem->valuestring == NULL) { + HILOGE("get abilityKeyItem fail!"); + continue; + } + cJSON* abilityValueItem = cJSON_GetObjectItemCaseSensitive(abilityItem, SWITCH_STATUS.c_str()); + if (!cJSON_IsString(abilityValueItem)) { + HILOGE("get abilityValueItem fail!"); + continue; + } + std::string deviceId = ContentSensorManagerUtils::GetInstance().ObtainLocalUdid(); + std::string serviceId = abilityKeyItem->valuestring; + std::string charKey = ProfileUtils::GenerateCharProfileKey(deviceId, serviceId, SWITCH_STATUS); + if (ProfileCache::GetInstance().IsCharProfileKeyExist(charKey)) { + HILOGE("switch already exists in profileCache!"); + charProfileList.clear(); + break; + } + std::string charValue = abilityValueItem->valuestring; + if (charValue != SWITCH_ON && charValue != SWITCH_OFF) { + HILOGE("switch status invaild"); + continue; + } + CharacteristicProfile characteristicProfile(deviceId, serviceId, SWITCH_STATUS, charValue); + charProfileList.push_back(characteristicProfile); + } + return DP_SUCCESS; +} + +void SwitchStatusCollector::AddSwitchStatusToDB(std::vector& charProfileList) +{ + if (charProfileList.empty()) { + return; + } + // get switch from db + uint32_t switchFromDB; + int32_t ret = SwitchProfileManager::GetInstance().GetLocalSwitchFromDB(switchFromDB); + if (ret == DP_SUCCESS) { + return; + } + ret = SwitchProfileManager::GetInstance().PutCharacteristicProfileBatch(charProfileList); + if (ret != DP_SUCCESS) { + HILOGE("collect switch status error"); + } +} +} // namespace DeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/deviceprofilemanager/dynamic_profile_manager.cpp b/services/core/src/deviceprofilemanager/dynamic_profile_manager.cpp new file mode 100644 index 00000000..62e91b4c --- /dev/null +++ b/services/core/src/deviceprofilemanager/dynamic_profile_manager.cpp @@ -0,0 +1,434 @@ +/* + * 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 +#include +#include +#include + +#include "kv_adapter.h" +#include "content_sensor_manager_utils.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_utils.h" +#include "profile_cache.h" +#include "profile_control_utils.h" +#include "permission_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +constexpr const char *LIB_DP_ADAPTER_NAME = "libdeviceprofileadapter.z.so"; +IMPLEMENT_SINGLE_INSTANCE(DynamicProfileManager); +namespace { + const std::string APP_ID = "distributed_device_profile_service"; + const std::string STORE_ID = "dp_kv_dynamic_store"; + const std::string TAG = "DynamicProfileManager"; + const std::string DP_MANAGER_HANDLER = "dp_manager_handler"; +} + +int32_t DynamicProfileManager::Init() +{ + HILOGI("call!"); + int32_t initResult = DP_MANAGER_INIT_FAIL; + { + std::lock_guard lock(dynamicStoreMutex_); + dynamicProfileStore_ = std::make_shared(APP_ID, STORE_ID, std::make_shared(), + std::make_shared(), std::make_shared(), + DistributedKv::TYPE_DYNAMICAL); + initResult = dynamicProfileStore_->Init(); + } + HILOGI("Init finish, res: %d", initResult); + return initResult; +} + +int32_t DynamicProfileManager::UnInit() +{ + HILOGI("call!"); + { + std::lock_guard lock(dynamicStoreMutex_); + dynamicProfileStore_->UnInit(); + dynamicProfileStore_ = nullptr; + } + UnloadDpSyncAdapter(); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::ReInit() +{ + HILOGI("call!"); + UnInit(); + return Init(); +} + +int32_t DynamicProfileManager::PutDeviceProfile(const DeviceProfile& deviceProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::PutDeviceProfile(dynamicProfileStore_, deviceProfile); + } + if (res != DP_SUCCESS) { + HILOGE("PutDeviceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutDeviceProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::PutServiceProfile(const ServiceProfile& serviceProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::PutServiceProfile(dynamicProfileStore_, serviceProfile); + } + if (res != DP_SUCCESS) { + HILOGE("PutServiceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutServiceProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::PutServiceProfileBatch(const std::vector& serviceProfiles) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::PutServiceProfileBatch(dynamicProfileStore_, serviceProfiles); + } + if (res != DP_SUCCESS) { + HILOGE("PutServiceProfileBatch fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutServiceProfileBatch success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::PutCharacteristicProfile(const CharacteristicProfile& charProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::PutCharacteristicProfile(dynamicProfileStore_, charProfile); + } + if (res != DP_SUCCESS) { + HILOGE("PutCharacteristicProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutCharacteristicProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::PutCharacteristicProfileBatch(const std::vector& charProfiles) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::PutCharacteristicProfileBatch(dynamicProfileStore_, charProfiles); + } + if (res != DP_SUCCESS) { + HILOGE("PutCharacteristicProfileBatch fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutCharacteristicProfileBatch success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::GetDeviceProfile(dynamicProfileStore_, deviceId, deviceProfile); + } + if (res != DP_SUCCESS) { + HILOGE("GetDeviceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetDeviceProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::GetServiceProfile(const std::string& deviceId, const std::string& serviceName, + ServiceProfile& serviceProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::GetServiceProfile(dynamicProfileStore_, deviceId, serviceName, + serviceProfile); + } + if (res != DP_SUCCESS) { + HILOGE("GetServiceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetServiceProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey, CharacteristicProfile& charProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::GetCharacteristicProfile(dynamicProfileStore_, deviceId, serviceName, + characteristicKey, charProfile); + } + if (res != DP_SUCCESS) { + HILOGE("GetCharacteristicProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetCharacteristicProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::DeleteServiceProfile(dynamicProfileStore_, deviceId, serviceName); + } + if (res != DP_SUCCESS) { + HILOGE("DeleteServiceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("DeleteServiceProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::DeleteCharacteristicProfile(dynamicProfileStore_, deviceId, serviceName, + characteristicKey); + } + if (res != DP_SUCCESS) { + HILOGE("DeleteCharacteristicProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("DeleteCharacteristicProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::GetAllDeviceProfile(std::vector& deviceProfiles) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::GetAllDeviceProfile(dynamicProfileStore_, deviceProfiles); + } + if (res != DP_SUCCESS) { + HILOGE("GetAllDeviceProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetAllDeviceProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::GetAllServiceProfile(std::vector& serviceProfiles) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::GetAllServiceProfile(dynamicProfileStore_, serviceProfiles); + } + if (res != DP_SUCCESS) { + HILOGE("serviceProfiles fail, reason: %{public}d!", res); + return res; + } + HILOGI("serviceProfiles success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::GetAllCharacteristicProfile(std::vector& charProfiles) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(dynamicStoreMutex_); + res = ProfileControlUtils::GetAllCharacteristicProfile(dynamicProfileStore_, charProfiles); + } + if (res != DP_SUCCESS) { + HILOGE("GetAllCharacteristicProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetAllCharacteristicProfile success"); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions &syncOptions, + sptr syncCompletedCallback) +{ + HILOGI("call!"); + if (syncCompletedCallback == nullptr) { + HILOGE("Params is invalid!"); + return DP_INVALID_PARAMS; + } + HILOGI("SyncDeviceProfile start!"); + std::vector onlineDevices = ProfileUtils::FilterOnlineDevices(syncOptions.GetDeviceList()); + if (onlineDevices.empty()) { + HILOGE("Params is invalid!"); + return DP_INVALID_PARAMS; + } + std::vector openHarmonyDevices; + for (auto it = onlineDevices.begin(); it != onlineDevices.end(); it++) { + std::string deviceId = *it; + if (RunloadedFunction(deviceId, syncCompletedCallback) != DP_SUCCESS) { + openHarmonyDevices.push_back(deviceId); + } + } + if (openHarmonyDevices.empty()) { + return DP_SUCCESS; + } + std::string callerDescriptor = PermissionManager::GetInstance().GetCallerProcName(); + ProfileCache::GetInstance().AddSyncListener(callerDescriptor, syncCompletedCallback); + { + std::lock_guard lock(dynamicStoreMutex_); + int32_t syncResult = dynamicProfileStore_->Sync(openHarmonyDevices, syncOptions.GetSyncMode()); + if (syncResult != DP_SUCCESS) { + HILOGI("SyncDeviceProfile fail, res: %d!", syncResult); + return DP_SYNC_DEVICE_FAIL; + } + } + HILOGI("SyncDeviceProfile success, caller: %s!", callerDescriptor.c_str()); + return DP_SUCCESS; +} + +bool DynamicProfileManager::LoadDpSyncAdapter() +{ + HILOGI("DynamicProfileManager::LoadDpSyncAdapter start."); + std::lock_guard lock(isAdapterLoadLock_); + if (isAdapterSoLoaded_ && (dpSyncAdapter_ != nullptr)) { + return true; + } + char path[PATH_MAX + 1] = {0x00}; + std::string soName = std::string(LIB_LOAD_PATH) + std::string(LIB_DP_ADAPTER_NAME); + if ((soName.length() == 0) || (soName.length() > PATH_MAX) || (realpath(soName.c_str(), path) == nullptr)) { + HILOGI("File %s canonicalization failed", soName.c_str()); + return false; + } + void *so_handle = dlopen(path, RTLD_NOW); + if (so_handle == nullptr) { + HILOGI("load dp sync adapter so %s failed", soName.c_str()); + return false; + } + dlerror(); + auto func = (CreateDPSyncAdapterFuncPtr)dlsym(so_handle, "CreateDPSyncAdaptertObject"); + if (dlerror() != nullptr || func == nullptr) { + dlclose(so_handle); + HILOGI("Create object function is not exist."); + return false; + } + dpSyncAdapter_ = std::shared_ptr(func()); + if (dpSyncAdapter_->Initialize() != DP_SUCCESS) { + dlclose(so_handle); + dpSyncAdapter_ = nullptr; + isAdapterSoLoaded_ = false; + HILOGI("dp sync adapter init failed"); + return false; + } + isAdapterSoLoaded_ = true; + HILOGI("DynamicProfileManager::LoadDpSyncAdapter sucess"); + return true; +} + +void DynamicProfileManager::UnloadDpSyncAdapter() +{ + HILOGI("DynamicProfileManager::UnloadDpSyncAdapter start."); + std::lock_guard lock(isAdapterLoadLock_); + if (dpSyncAdapter_ != nullptr) { + dpSyncAdapter_->Release(); + } + dpSyncAdapter_ = nullptr; + char path[PATH_MAX + 1] = {0x00}; + std::string soPathName = std::string(LIB_LOAD_PATH) + std::string(LIB_DP_ADAPTER_NAME); + if ((soPathName.length() == 0) || (soPathName.length() > PATH_MAX) || + (realpath(soPathName.c_str(), path) == nullptr)) { + HILOGI("File %s canonicalization failed", soPathName.c_str()); + return; + } + void *so_handle = dlopen(path, RTLD_NOW | RTLD_NOLOAD); + if (so_handle != nullptr) { + HILOGI("dp sync adapter so_handle is not nullptr."); + dlclose(so_handle); + isAdapterSoLoaded_ = false; + } +} + +int32_t DynamicProfileManager::RunloadedFunction(std::string deviceId, sptr syncCompletedCallback) +{ + if (!LoadDpSyncAdapter()) { + HILOGE("dp service adapter load failed."); + return DP_LOAD_SYNC_ADAPTER_FAILED; + } + if (dpSyncAdapter_->Initialize() != DP_SUCCESS) { + HILOGE("dp service adapter initialize failed."); + return DP_LOAD_SYNC_ADAPTER_FAILED; + } + if (dpSyncAdapter_->DetectRemoteDPVersion(deviceId) != DP_SUCCESS) { + HILOGE("dp service adapter detect remote version failed."); + return DP_LOAD_SYNC_ADAPTER_FAILED; + } + const std::list deviceIdList = { deviceId }; + if (dpSyncAdapter_->SyncProfile(deviceIdList, syncCompletedCallback) != DP_SUCCESS) { + HILOGE("dp service adapter sync profile failed."); + return DP_LOAD_SYNC_ADAPTER_FAILED; + } + HILOGD("dp service adapter sync profile success."); + return DP_SUCCESS; +} + +int32_t DynamicProfileManager::DeviceOnlineAutoSync(const std::string& peerNetworkId) +{ + HILOGI("call! peerNetworkId=%{public}s", ProfileUtils::GetAnonyString(peerNetworkId).c_str()); + std::vector deviceList{peerNetworkId}; + std::vector onlineDevices = ProfileUtils::FilterOnlineDevices(deviceList); + if (onlineDevices.empty() + || std::find(onlineDevices.begin(), onlineDevices.end(), peerNetworkId) == onlineDevices.end()) { + HILOGE("Params is invalid! peerNetworkId=%{public}s", ProfileUtils::GetAnonyString(peerNetworkId).c_str()); + return DP_INVALID_PARAMS; + } + int32_t errCode = RunloadedFunction(peerNetworkId, nullptr); + if (errCode != DP_SUCCESS) { + HILOGE("sync profile failed.errCode=%{public}d,peerNetworkId=%{public}s", errCode, + ProfileUtils::GetAnonyString(peerNetworkId).c_str()); + } + return errCode; +} + +} // namespace DeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/deviceprofilemanager/static_profile_manager.cpp b/services/core/src/deviceprofilemanager/static_profile_manager.cpp new file mode 100644 index 00000000..a5ec6f13 --- /dev/null +++ b/services/core/src/deviceprofilemanager/static_profile_manager.cpp @@ -0,0 +1,182 @@ +/* + * 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 +#include + +#include "static_profile_manager.h" + +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "kv_adapter.h" +#include "profile_cache.h" +#include "profile_control_utils.h" +#include "profile_utils.h" +#include "static_capability_loader.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(StaticProfileManager); +namespace { + const std::string TAG = "StaticProfileManager"; + const std::string APP_ID = "distributed_device_profile_service"; + const std::string STORE_ID = "dp_kv_static_store"; +} + +int32_t StaticProfileManager::Init() +{ + HILOGI("call!"); + int32_t initResult = DP_MANAGER_INIT_FAIL; + { + std::lock_guard lock(staticStoreMutex_); + staticProfileStore_ = std::make_shared(APP_ID, STORE_ID, std::make_shared(), + std::make_shared(), std::make_shared(), + DistributedKv::TYPE_STATICS); + initResult = staticProfileStore_->Init(); + } + HILOGI("Init finish, res: %{public}d", initResult); + return initResult; +} + +int32_t StaticProfileManager::UnInit() +{ + HILOGI("call!"); + { + std::lock_guard lock(staticStoreMutex_); + staticProfileStore_->UnInit(); + staticProfileStore_ = nullptr; + } + return DP_SUCCESS; +} + +int32_t StaticProfileManager::ReInit() +{ + HILOGI("call!"); + UnInit(); + return Init(); +} + +int32_t StaticProfileManager::PutCharacteristicProfile(const CharacteristicProfile& charProfile) +{ + HILOGI("call!"); + int32_t putResult = DP_PUT_CHARACTERISTIC_CACHE_ERR; + { + std::lock_guard lock(staticStoreMutex_); + putResult = ProfileControlUtils::PutCharacteristicProfile(staticProfileStore_, charProfile); + } + HILOGI("PutCharacteristicProfile fail, reason: %{public}d!", putResult); + return putResult; +} + +int32_t StaticProfileManager::GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey, CharacteristicProfile& charProfile) +{ + HILOGI("call!"); + if (!ProfileUtils::IsKeyValid(deviceId) || !ProfileUtils::IsKeyValid(serviceName) || + !ProfileUtils::IsKeyValid(characteristicKey)) { + HILOGE("Params are invalid!"); + return DP_INVALID_PARAMS; + } + HILOGI("GetCharacteristicProfile, deviceId: %s, serviceName: %s, charKey: %s!", + ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str()); + if (ProfileCache::GetInstance().GetStaticCharacteristicProfile(deviceId, serviceName, characteristicKey, + charProfile) == DP_SUCCESS) { + HILOGI("GetCharProfile in cache, profile: %{public}s!", charProfile.dump().c_str()); + return DP_SUCCESS; + } + CharacteristicProfile staticCapabilityProfile; + int32_t getResult = DP_GET_KV_DB_FAIL; + { + std::lock_guard lock(staticStoreMutex_); + getResult = ProfileControlUtils::GetCharacteristicProfile(staticProfileStore_, deviceId, + STATIC_CAPABILITY_SVR_ID, STATIC_CAPABILITY_CHAR_ID, staticCapabilityProfile); + } + if (getResult != DP_SUCCESS) { + HILOGE("GetCharacteristicProfile fail, reason: %{public}d!", getResult); + return getResult; + } + std::unordered_map staticInfoProfiles; + int generateProfileResult = GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles); + if (generateProfileResult != DP_SUCCESS) { + HILOGE("GenerateStaticInfoProfile fail, reason: %{public}d!", generateProfileResult); + return generateProfileResult; + } + std::string charProfileKey = ProfileUtils::GenerateCharProfileKey(deviceId, serviceName, characteristicKey); + if (staticInfoProfiles.find(charProfileKey) == staticInfoProfiles.end()) { + HILOGE("charProfileKey is not exist, deviceId: %s, serviceName: %s, characteristicKey: %s", + ProfileUtils::GetAnonyString(deviceId).c_str(), serviceName.c_str(), characteristicKey.c_str()); + return DP_NOT_FOUND_FAIL; + } + charProfile = staticInfoProfiles[charProfileKey]; + ProfileCache::GetInstance().AddStaticCharProfileBatch(staticInfoProfiles); + return DP_SUCCESS; +} + +int32_t StaticProfileManager::GetAllCharacteristicProfile( + std::vector& staticCapabilityProfiles) +{ + HILOGI("call!"); + int32_t getAllResult = DP_GET_KV_DB_FAIL; + { + std::lock_guard lock(staticStoreMutex_); + getAllResult = ProfileControlUtils::GetAllCharacteristicProfile(staticProfileStore_, staticCapabilityProfiles); + } + if (getAllResult != DP_SUCCESS) { + HILOGE("StaticProfileManager GetAllCharacteristicProfile fail, reason: %{public}d!", getAllResult); + return getAllResult; + } + for (const CharacteristicProfile& staticCapabilityProfile : staticCapabilityProfiles) { + HILOGI("staticCapabilityProfile %{public}s!", staticCapabilityProfile.dump().c_str()); + GenerateStaticInfoProfile(staticCapabilityProfile, staticInfoProfiles); + } + return DP_SUCCESS; +} + +int32_t StaticProfileManager::GenerateStaticInfoProfile(const CharacteristicProfile& staticCapabilityProfile, + std::unordered_map& staticInfoProfiles) +{ + HILOGI("call!"); + std::string charValue = staticCapabilityProfile.GetCharacteristicValue(); + cJSON* charValueJson = cJSON_Parse(charValue.c_str()); + if (!cJSON_IsObject(charValueJson)) { + HILOGE("charValueJson parse fail!"); + cJSON_Delete(charValueJson); + return DP_PARSE_STATIC_CAP_FAIL; + } + cJSON* staticCapabilityVersionItem = cJSON_GetObjectItemCaseSensitive(charValueJson, + STATIC_CAPABILITY_VERSION.c_str()); + if (!cJSON_IsString(staticCapabilityVersionItem) || (staticCapabilityVersionItem->valuestring == NULL)) { + HILOGE("staticCapabilityVersion is invalid!"); + cJSON_Delete(charValueJson); + return DP_PARSE_STATIC_CAP_FAIL; + } + std::string staticCapabilityVersion = staticCapabilityVersionItem->valuestring; + cJSON* staticCapabilityValueItem = cJSON_GetObjectItemCaseSensitive(charValueJson, STATIC_CAPABILITY_VALUE.c_str()); + if (!cJSON_IsString(staticCapabilityValueItem) || (staticCapabilityValueItem->valuestring == NULL)) { + HILOGE("staticCapabilityValue is invalid!"); + cJSON_Delete(charValueJson); + return DP_PARSE_STATIC_CAP_FAIL; + } + std::string staticCapabilityValue = staticCapabilityValueItem->valuestring; + cJSON_Delete(charValueJson); + StaticCapabilityLoader::GetInstance().LoadStaticProfiles(staticCapabilityProfile.GetDeviceId(), + staticCapabilityVersion, staticCapabilityValue, staticInfoProfiles); + return DP_SUCCESS; +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/deviceprofilemanager/switch_profile_manager.cpp b/services/core/src/deviceprofilemanager/switch_profile_manager.cpp new file mode 100644 index 00000000..2a8e943a --- /dev/null +++ b/services/core/src/deviceprofilemanager/switch_profile_manager.cpp @@ -0,0 +1,135 @@ +/* + * 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 "switch_profile_manager.h" + +#include +#include +#include +#include +#include + +#include "switch_adapter.h" +#include "content_sensor_manager_utils.h" +#include "device_manager.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "profile_utils.h" +#include "profile_cache.h" +#include "profile_control_utils.h" +#include "permission_manager.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(SwitchProfileManager); +namespace { + const std::string TAG = "SwitchProfileManager"; + const std::string APP_ID = "distributed_device_profile_service"; +} + +int32_t SwitchProfileManager::Init() +{ + HILOGI("call!"); + SwitchAdapter::GetInstance().Init(); + int32_t res = SwitchAdapter::GetInstance().SubscribeSwitchData(APP_ID); + if (res != DP_SUCCESS) { + HILOGE("SubscribeSwitchData failed, res: %d", res); + return DP_INIT_SWITCH_PROFILE_MANAGER_FAIL; + } + return DP_SUCCESS; +} + +int32_t SwitchProfileManager::UnInit() +{ + HILOGI("call!"); + int32_t res = SwitchAdapter::GetInstance().UnsubscribeSwitchData(APP_ID); + if (res != DP_SUCCESS) { + HILOGE("UnsubscribeSwitchData failed, res: %d", res); + return DP_UNSUBSCRIBE_FAILED; + } + return DP_SUCCESS; +} + +int32_t SwitchProfileManager::ReInit() +{ + HILOGI("call!"); + UnInit(); + return Init(); +} + +int32_t SwitchProfileManager::PutCharacteristicProfile(const CharacteristicProfile& charProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(switchProfileMutex_); + res = ProfileControlUtils::PutSwitchCharacteristicProfile(APP_ID, charProfile); + } + if (res != DP_SUCCESS) { + HILOGE("PutCharacteristicProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutCharacteristicProfile success"); + return DP_SUCCESS; +} + +int32_t SwitchProfileManager::PutCharacteristicProfileBatch(const std::vector& charProfiles) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(switchProfileMutex_); + res = ProfileControlUtils::PutSwitchCharacteristicProfileBatch(APP_ID, charProfiles); + } + if (res != DP_SUCCESS) { + HILOGE("PutCharacteristicProfileBatch fail, reason: %{public}d!", res); + return res; + } + HILOGI("PutCharacteristicProfileBatch success"); + return DP_SUCCESS; +} + +int32_t SwitchProfileManager::GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey, CharacteristicProfile& charProfile) +{ + HILOGI("call!"); + int32_t res; + { + std::lock_guard lock(switchProfileMutex_); + res = ProfileControlUtils::GetSwitchCharacteristicProfile(APP_ID, deviceId, serviceName, characteristicKey, + charProfile); + } + if (res != DP_SUCCESS) { + HILOGE("GetSwitchCharacteristicProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetSwitchCharacteristicProfile success"); + return DP_SUCCESS; +} + +int32_t SwitchProfileManager::RefreshLocalSwitchProfile() +{ + HILOGI("call!"); + int32_t res = ProfileControlUtils::RefreshLocalSwitchProfile(APP_ID); + if (res != DP_SUCCESS) { + HILOGE("RefreshLocalSwitchProfile fail, reason: %{public}d!", res); + return res; + } + HILOGI("GetSwitchCharacteristicProfile success"); + return DP_SUCCESS; +} + +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/src/persistenceadapter/kvadapter/switch_adapter.cpp b/services/core/src/persistenceadapter/kvadapter/switch_adapter.cpp new file mode 100644 index 00000000..d955514f --- /dev/null +++ b/services/core/src/persistenceadapter/kvadapter/switch_adapter.cpp @@ -0,0 +1,118 @@ +/* + * 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 "switch_adapter.h" + +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "kv_data_change_listener.h" +#include "types.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(SwitchAdapter); + +namespace { + const std::string TAG = "SwitchAdapter"; +} + +void SwitchAdapter::Init() +{ + HILOGI("called"); + std::lock_guard lock(switchAdapterMutex_); + observer_ = std::make_shared(); +} + +void SwitchAdapter::Uninit() {} + +int32_t SwitchAdapter::PutSwitch(const std::string& appId, uint32_t value, uint16_t length) +{ + HILOGI("called"); + if (appId.empty()) { + HILOGE("params are invalid"); + return DP_INVALID_PARAMS; + } + DistributedKv::AppId appID; + appID.appId = appId; + const DistributedKv::SwitchData switchData = {value, length}; + + std::lock_guard lock(switchAdapterMutex_); + DistributedKv::Status res = kvDataMgr_.PutSwitch(appID, switchData); + if (res != DistributedKv::Status::SUCCESS) { + HILOGE("PutSwitch kv to db failed, ret: %d", res); + return DP_PUT_KV_DB_FAIL; + } + return DP_SUCCESS; +} + +int32_t SwitchAdapter::GetSwitch(const std::string& appId, const std::string& networkId, uint32_t& value) +{ + HILOGI("called"); + if (appId.empty() || networkId.empty()) { + HILOGE("params are invalid"); + return DP_INVALID_PARAMS; + } + DistributedKv::AppId appID; + appID.appId = appId; + + std::lock_guard lock(switchAdapterMutex_); + auto res = kvDataMgr_.GetSwitch(appID, networkId); + if (res.first != DistributedKv::Status::SUCCESS) { + HILOGE("kv from db failed, ret: %d", res.first); + return DP_PUT_KV_DB_FAIL; + } + value = res.second.value; + return DP_SUCCESS; +} + +int32_t SwitchAdapter::SubscribeSwitchData(const std::string& appId) +{ + HILOGI("called"); + if (appId.empty()) { + HILOGE("appid is empty"); + return DP_INVALID_PARAMS; + } + DistributedKv::AppId appID; + appID.appId = appId; + + std::lock_guard lock(switchAdapterMutex_); + auto res = kvDataMgr_.SubscribeSwitchData(appID, observer_); + if (res != DistributedKv::Status::SUCCESS) { + HILOGE("SubscribeSwitchData failed, ret: %d", res); + return DP_SUBSCRIBE_FAILED; + } + return DP_SUCCESS; +} + +int32_t SwitchAdapter::UnsubscribeSwitchData(const std::string& appId) +{ + HILOGI("called"); + if (appId.empty()) { + HILOGE("appid is empty"); + return DP_INVALID_PARAMS; + } + + DistributedKv::AppId appID; + appID.appId = appId; + std::lock_guard lock(switchAdapterMutex_); + auto res = kvDataMgr_.UnsubscribeSwitchData(appID, observer_); + if (res != DistributedKv::Status::SUCCESS) { + HILOGE("SubscribeSwitchData failed, ret: %d", res); + return DP_SUBSCRIBE_FAILED; + } + return DP_SUCCESS; +} +} // namespace DeviceProfile +} // namespace OHOS -- Gitee