From 3c39ec9bff72ae26d4a488d35a233e88694b3e13 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Wed, 12 Feb 2025 09:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=A1=AC=E4=BB=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=99=9A=E6=8B=9F=E7=A1=AC=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=8C=89=E9=9C=80=E5=90=AF=E5=8A=A8=EF=BC=8C=E5=88=86?= =?UTF-8?q?=E5=B8=83=E5=BC=8F=E7=A1=AC=E4=BB=B6=E6=94=AF=E6=8C=81=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E4=B8=BB=E5=8A=A8=E4=BD=BF=E8=83=BD/=E5=8E=BB?= =?UTF-8?q?=E4=BD=BF=E8=83=BD=EF=BC=8C=E4=BB=A3=E7=A0=81=E5=88=86=E8=A7=A3?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=BA=8C=E7=AC=94=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuzhihui7 --- common/utils/include/constants.h | 4 +- .../include/distributed_hardware_errno.h | 14 +- common/utils/include/ihardware_handler.h | 2 +- .../componentloader/component_loader.h | 38 +- .../componentmanager/component_manager.h | 2 +- .../include/resourcemanager/version_info.h | 3 +- .../include/task/disable_task.h | 23 +- .../include/task/enable_task.h | 23 +- .../include/task/task_factory.h | 5 +- .../include/utils/impl_utils.h | 12 +- .../src/componentloader/component_loader.cpp | 115 +++- .../componentmanager/component_manager.cpp | 550 +++++++++++++++++- .../src/resourcemanager/version_info.cpp | 63 +- .../src/task/disable_task.cpp | 97 ++- .../src/task/enable_task.cpp | 97 ++- .../src/task/online_task.cpp | 11 +- .../src/task/task_factory.cpp | 28 +- 17 files changed, 1043 insertions(+), 44 deletions(-) diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index c3719d62..238be88f 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -65,6 +65,8 @@ namespace DistributedHardware { const std::string HANDLER = "handler"; const std::string SOURCE_VER = "source_ver"; const std::string SINK_VER = "sink_ver"; + const std::string SOURCE_FEATURE_FILTER = "source_feature_filter"; + const std::string SINK_SUPPORTED_FEATURE = "sink_supported_feature"; const std::string DH_TASK_NAME_PREFIX = "Task_"; const std::string DH_FWK_PKG_NAME = "ohos.dhardware"; const std::string DH_FWK_SESSION_NAME = "ohos.dhardware.session_"; diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index c3d4069a..09068db9 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -81,12 +81,12 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL = -10602; constexpr int32_t ERR_DH_FWK_LOADER_DLCLOSE_FAIL = -10603; constexpr int32_t ERR_DH_FWK_LOADER_PROFILE_PATH_IS_NULL = -10604; - constexpr int32_t ERR_DH_FWK_LOADER_SINK_IS_LOADED = -10605; - constexpr int32_t ERR_DH_FWK_LOADER_SOURCE_IS_LOADED = -10606; - constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_IS_LOADED = -10607; - constexpr int32_t ERR_DH_FWK_LOADER_SINK_NOT_LOADED = -10608; - constexpr int32_t ERR_DH_FWK_LOADER_SOURCE_NOT_LOADED = -10609; - constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_NOT_LOADED = -10610; + constexpr int32_t ERR_DH_FWK_LOADER_SINK_LOAD = -10605; + constexpr int32_t ERR_DH_FWK_LOADER_SOURCE_LOAD = -10606; + constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_LOAD = -10607; + constexpr int32_t ERR_DH_FWK_LOADER_SINK_UNLOAD = -10608; + constexpr int32_t ERR_DH_FWK_LOADER_SOURCE_UNLOAD = -10609; + constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_UNLOAD = -10610; /* Task errno, range: [-10700, -10799] */ constexpr int32_t ERR_DH_FWK_TASK_TIMEOUT = -10700; diff --git a/common/utils/include/ihardware_handler.h b/common/utils/include/ihardware_handler.h index 10e5cd2a..a80cd368 100644 --- a/common/utils/include/ihardware_handler.h +++ b/common/utils/include/ihardware_handler.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 diff --git a/services/distributedhardwarefwkservice/include/componentloader/component_loader.h b/services/distributedhardwarefwkservice/include/componentloader/component_loader.h index 6009f2a8..88548103 100644 --- a/services/distributedhardwarefwkservice/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkservice/include/componentloader/component_loader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -38,17 +38,6 @@ struct ResourceDesc { }; namespace { -struct CompHandler { - DHType type; - void *sourceHandler; - int32_t sourceSaId; - void *sinkHandler; - int32_t sinkSaId; - void *hardwareHandler; - std::vector resourceDesc; -}; -} - struct CompConfig { std::string name; DHType type; @@ -61,8 +50,23 @@ struct CompConfig { std::string compSinkVersion; int32_t compSinkSaId; std::vector compResourceDesc; + bool haveFeature; + std::vector sourceFeatureFilters; + std::vector sinkSupportedFeatures; }; +struct CompHandler { + DHType type; + void *sourceHandler; + int32_t sourceSaId; + void *sinkHandler; + int32_t sinkSaId; + void *hardwareHandler; + std::vector resourceDesc; + CompConfig compConfig; +}; +} + class ComponentLoader { DECLARE_SINGLE_INSTANCE_BASE(ComponentLoader); @@ -84,6 +88,10 @@ public: int32_t GetSourceSaId(const DHType dhType); DHType GetDHTypeBySrcSaId(const int32_t saId); std::map GetCompResourceDesc(); + + int32_t GetSource(const DHType dhType); + int32_t GetSink(const DHType dhType); + int32_t GetHardwareHandler(const DHType dhType); bool IsDHTypeSupport(DHType dhType); private: @@ -95,9 +103,15 @@ private: int32_t ParseConfig(); void StoreLocalDHVersionInDB(); bool IsDHTypeExist(DHType dhType); + bool IsDHTypeSinkLoaded(DHType dhType); + bool IsDHTypeSourceLoaded(DHType dhType); + bool IsDHTypeHandlerLoaded(DHType dhType); std::string Readfile(const std::string &filePath); void ParseCompConfigFromJson(cJSON *component, CompConfig &config); void ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config); + void ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilters, CompConfig &config); + void ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFeatures, CompConfig &config); + void CheckAndParseFeatures(cJSON *component, CompConfig &config); bool CheckComponentEnable(const CompConfig &config); private: diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 6af28617..55bee59a 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -173,7 +173,7 @@ private: struct DHSinkStatus { int32_t refLoad; - std::map enableInfos; + std::map enableInfos; // key is dhid std::map> listeners; DHSinkStatus() { diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h b/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h index 0acd872d..eb97edc6 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/version_info.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -31,6 +31,7 @@ struct VersionInfo { std::string ToJsonString() const; }; +void ToJson(cJSON *jsonObject, const CompVersion &compVer); void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo); void FromJson(const cJSON *jsonObject, CompVersion &compVer); void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo); diff --git a/services/distributedhardwarefwkservice/include/task/disable_task.h b/services/distributedhardwarefwkservice/include/task/disable_task.h index 1904d4a5..346c32ae 100644 --- a/services/distributedhardwarefwkservice/include/task/disable_task.h +++ b/services/distributedhardwarefwkservice/include/task/disable_task.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -28,10 +28,31 @@ public: virtual ~DisableTask(); virtual void DoTask(); + void SetEffectSink(bool isEffect); + bool GetEffectSink(); + void SetEffectSource(bool isEffect); + bool GetEffectSource(); + void SetCallingUid(int32_t callingUid); + int32_t GetCallingUid(); + void SetCallingPid(int32_t callingPid); + int32_t GetCallingPid(); + private: /* synchronous function for unregister distributed hardware, return on asynchronous unregister finish */ int32_t UnRegisterHardware(); void DoTaskInner(); + int32_t DoAutoDisable(); + int32_t DoActiveDisable(); + +private: + // effect sink + bool effectSink_{ false }; + // effect source + bool effectSource_{ false }; + // enable or disable calling uid + int32_t callingUid_{ 0 }; + // enable or disable calling pid + int32_t callingPid_{ 0 }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/task/enable_task.h b/services/distributedhardwarefwkservice/include/task/enable_task.h index 36323aa3..f6a26ae4 100644 --- a/services/distributedhardwarefwkservice/include/task/enable_task.h +++ b/services/distributedhardwarefwkservice/include/task/enable_task.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -28,10 +28,31 @@ public: virtual ~EnableTask(); virtual void DoTask(); + void SetEffectSink(bool isEffect); + bool GetEffectSink(); + void SetEffectSource(bool isEffect); + bool GetEffectSource(); + void SetCallingUid(int32_t callingUid); + int32_t GetCallingUid(); + void SetCallingPid(int32_t callingPid); + int32_t GetCallingPid(); + private: /* synchronous function for register distributed hardware, return on asynchronous register finish */ int32_t RegisterHardware(); void DoTaskInner(); + int32_t DoAutoEnable(); + int32_t DoActiveEnable(); + +private: + // effect sink + bool effectSink_{ false }; + // effect source + bool effectSource_{ false }; + // enable or disable calling uid + int32_t callingUid_{ 0 }; + // enable or disable calling pid + int32_t callingPid_{ 0 }; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/task/task_factory.h b/services/distributedhardwarefwkservice/include/task/task_factory.h index c52c5b3f..c733bd8d 100644 --- a/services/distributedhardwarefwkservice/include/task/task_factory.h +++ b/services/distributedhardwarefwkservice/include/task/task_factory.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -27,6 +27,9 @@ class TaskFactory { DECLARE_SINGLE_INSTANCE(TaskFactory); public: std::shared_ptr CreateTask(TaskType taskType, TaskParam taskParam, std::shared_ptr fatherTask); +private: + std::shared_ptr CreateEnableTask(const TaskParam &taskParam); + std::shared_ptr CreateDisableTask(const TaskParam &taskParam); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/utils/impl_utils.h b/services/distributedhardwarefwkservice/include/utils/impl_utils.h index 51bd6ce1..389c093e 100644 --- a/services/distributedhardwarefwkservice/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkservice/include/utils/impl_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -81,7 +81,15 @@ struct TaskParam { // remote device dhid std::string dhId; // remote device dh type - DHType dhType; + DHType dhType{ DHType::UNKNOWN }; + // effect sink + bool effectSink{ false }; + // effect source + bool effectSource{ false }; + // enable or disable calling uid + int32_t callingUid{ 0 }; + // enable or disable calling pid + int32_t callingPid{ 0 }; }; struct TaskDump { diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index c8e686bc..8026aec1 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -380,6 +380,41 @@ void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig } } +void ComponentLoader::ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilters, CompConfig &config) +{ + cJSON *filter = nullptr; + config.sourceFeatureFilters.clear(); + cJSON_ArrayForEach(filter, sourceFeatureFilters) { + config.sourceFeatureFilters.push_back(filter->valuestring); + } +} + +void ComponentLoader::ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFeatures, CompConfig &config) +{ + cJSON *feature = nullptr; + config.sinkSupportedFeatures.clear(); + cJSON_ArrayForEach(feature, sinkSupportedFeatures) { + config.sinkSupportedFeatures.push_back(feature->valuestring); + } +} + +void ComponentLoader::CheckAndParseFeatures(cJSON *component, CompConfig &config) +{ + cJSON *sourceFeatureFilters = cJSON_GetObjectItem(component, SOURCE_FEATURE_FILTER.c_str()); + cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(component, SINK_SUPPORTED_FEATURE.c_str()); + if (sourceFeatureFilters || sinkSupportedFeatures) { + config.haveFeature = true; + if (IsArray(sourceFeatureFilters)) { + ParseSourceFeatureFiltersFromJson(sourceFeatureFilters, config); + } + if (IsArray(sinkSupportedFeatures)) { + ParseSinkSupportedFeaturesFromJson(sinkSupportedFeatures, config); + } + } else { + config.haveFeature = false; + } +} + int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion) { if (!isLocalVersionInit_.load()) { @@ -648,6 +683,33 @@ bool ComponentLoader::IsDHTypeExist(DHType dhType) return true; } +bool ComponentLoader::IsDHTypeSinkLoaded(DHType dhType) +{ + if (compHandlerMap_[dhType].sinkHandler == nullptr) { + DHLOGE("fail, dhType: %{public}#X sink not loaded", dhType); + return false; + } + return true; +} + +bool ComponentLoader::IsDHTypeSourceLoaded(DHType dhType) +{ + if (compHandlerMap_[dhType].sourceHandler == nullptr) { + DHLOGE("fail, dhType: %{public}#X source not loaded", dhType); + return false; + } + return true; +} + +bool ComponentLoader::IsDHTypeHandlerLoaded(DHType dhType) +{ + if (compHandlerMap_[dhType].hardwareHandler == nullptr) { + DHLOGE("fail, dhType: %{public}#X handler not loaded", dhType); + return false; + } + return true; +} + int32_t ComponentLoader::GetSourceSaId(const DHType dhType) { std::lock_guard lock(compHandlerMapMutex_); @@ -676,6 +738,57 @@ std::map ComponentLoader::GetCompResourceDesc() return resDescMap_; } +int32_t ComponentLoader::GetSource(const DHType dhType) +{ + std::lock_guard lock(compHandlerMapMutex_); + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { + DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + CompHandler &compHandler = iter->second; + if (compHandler.sourceHandler != nullptr) { + DHLOGE("sourceHandler is loaded."); + return ERR_DH_FWK_LOADER_SOURCE_LOAD; + } + compHandler.sourceHandler = GetHandler(compHandler.compConfig.compSourceLoc); + return DH_FWK_SUCCESS; +} + +int32_t ComponentLoader::GetSink(const DHType dhType) +{ + std::lock_guard lock(compHandlerMapMutex_); + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { + DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + CompHandler &compHandler = iter->second; + if (compHandler.sinkHandler != nullptr) { + DHLOGE("sinkHandler is loaded."); + return ERR_DH_FWK_LOADER_SINK_LOAD; + } + compHandler.sinkHandler = GetHandler(compHandler.compConfig.compSinkLoc); + return DH_FWK_SUCCESS; +} + +int32_t ComponentLoader::GetHardwareHandler(const DHType dhType) +{ + std::lock_guard lock(compHandlerMapMutex_); + auto iter = compHandlerMap_.find(dhType); + if (iter == compHandlerMap_.end()) { + DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + CompHandler &compHandler = iter->second; + if (compHandler.hardwareHandler != nullptr) { + DHLOGE("hardwareHandler is loaded."); + return ERR_DH_FWK_LOADER_HANDLER_LOAD; + } + compHandler.hardwareHandler = GetHandler(compHandler.compConfig.compHandlerLoc); + return DH_FWK_SUCCESS; +} + bool ComponentLoader::IsDHTypeSupport(DHType dhType) { return IsDHTypeExist(dhType); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 6a4c8807..22a76b96 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -1140,6 +1140,548 @@ std::shared_ptr ComponentManager return this->eventHandler_; } +int32_t ComponentManager::CheckDemandStart(const std::string &uuid, + const DHType dhType, bool &enableSink, bool &enableSource) +{ + // Initialize output parameters + enableSink = false; + enableSource = false; + + // Get remote config + VersionInfo versionInfo; + auto ret = GetRemoteVerInfo(versionInfo, uuid, dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetRemoteVerInfo fail, errCode = %{public}d!", ret); + return ret; + } + + // Get local config + DHVersion dhVersion; + ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetLocalDHVersion fail, errCode = %{public}d!", ret); + return ret; + } + + auto iterLocal = dhVersion.compVersions.find(dhType); + if (iterLocal == dhVersion.compVersions.end()) { + DHLOGE("Not find dhType in local: %{public}#X!", dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto iterRemote = versionInfo.compVersions.find(dhType); + if (iterRemote == versionInfo.compVersions.end()) { + DHLOGE("Not find dhType in remote: %{public}#X!", dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + // Check local config + if (!iterLocal->second.haveFeature) { + enableSink = true; + enableSource = true; + return DH_FWK_SUCCESS; + } + + if (iterLocal->second.sinkSupportedFeatures.size()) { + enableSink = true; + } + + if (iterLocal->second.sourceFeatureFilters.size() == 0) { + return DH_FWK_SUCCESS; + } + + // Check remote config + if (!iterRemote->second.haveFeature) { // Remote config is null, need enable source + enableSource = true; + return DH_FWK_SUCCESS; + } + + if (iterRemote->second.sinkSupportedFeatures.size() == 0) { // Remote sink config is empty, not enable source + return DH_FWK_SUCCESS; + } + + // Check if the configurations on both ends match + enableSource = IsFeatureMatched(iterLocal->second.sourceFeatureFilters, iterRemote->second.sinkSupportedFeatures); + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::RegisterDHStatusListener( + sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSinkStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + if (listeners.find(ctrlKey) != listeners.end()) { + DHLOGE("Repeat call RegisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners[ctrlKey] = listener; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::UnregisterDHStatusListener( + sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSinkStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + auto it = listeners.find(ctrlKey); + if (it == listeners.end()) { + DHLOGE("Repeat call UnregisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners.erase(it); + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::RegisterDHStatusListener( + const std::string &networkId, sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSourceStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSourceStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + if (listeners.find(ctrlKey) != listeners.end()) { + DHLOGE("Repeat call RegisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners[ctrlKey] = listener; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::UnregisterDHStatusListener( + const std::string &networkId, sptr listener, int32_t callingUid, int32_t callingPid) +{ + std::lock_guard lock(dhSourceStatusMtx_); + + auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); + for (const auto &type : compTypes) { + auto &status = dhSourceStatus_[type]; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &listeners = status.listeners; + auto it = listeners.find(ctrlKey); + if (it == listeners.end()) { + DHLOGE("Repeat call UnregisterDHStatusListener, uid = %{public}d, pid = %{public}d.", + ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + listeners.erase(it); + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = EnableSinkInternal(dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnEnable(dhDescriptor); + } + } + return ret; +} + +int32_t ComponentManager::DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = DisableSinkInternal(dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnDisable(dhDescriptor); + } + } + return ret; +} + +int32_t ComponentManager::EnableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = EnableSourceInternal(networkId, dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnEnable(networkId, dhDescriptor); + } + } + return ret; +} + +int32_t ComponentManager::DisableSource(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid) +{ + sptr listener; + int32_t ret = DisableSourceInternal(networkId, dhDescriptor, callingUid, callingPid, listener); + if (ret == DH_FWK_SUCCESS) { + if (listener) { + listener->OnDisable(networkId, dhDescriptor); + } + } + return ret; +} + +int32_t ComponentManager::ForceDisableSink(const DHDescriptor &dhDescriptor) +{ + std::vector> listeners; + int32_t ret = ForceDisableSinkInternal(dhDescriptor, listeners); + if (ret == DH_FWK_SUCCESS) { + for (auto listener : listeners) { + listener->OnDisable(dhDescriptor); + } + } + return ret; +} + +int32_t ComponentManager::ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor) +{ + std::vector> listeners; + int32_t ret = ForceDisableSourceInternal(networkId, dhDescriptor, listeners); + if (ret == DH_FWK_SUCCESS) { + for (auto listener : listeners) { + listener->OnDisable(networkId, dhDescriptor); + } + } + return ret; +} + +int32_t ComponentManager::GetRemoteVerInfo(VersionInfo &versionInfo, const std::string &uuid, DHType dhType) +{ + auto deviceId = GetDeviceIdByUUID(uuid); + auto ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Get Version info Manager failed, uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + for (int32_t retryCount = 0; + retryCount < SYNC_DEVICE_INFO_TIMEOUT_MILLISECONDS/SYNC_DEVICE_INFO_INTERVAL_MILLISECONDS; + ++retryCount) { + std::this_thread::sleep_for(std::chrono::milliseconds(SYNC_DEVICE_INFO_INTERVAL_MILLISECONDS)); + ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); + if (ret == DH_FWK_SUCCESS) { + break; + } else { + DHLOGE("Attempted to retrieve VersionInfo again but still failed, " + "uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + } + } + if (ret != DH_FWK_SUCCESS) { + DHLOGE("After trying again, it ultimately failed, " + "uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + return ret; + } else { + DHLOGI("After multiple attempts, obtaining VersionInfo was successful, " + "uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d.", + GetAnonyString(uuid).c_str(), dhType, ret); + } + } + return ret; +} + +bool ComponentManager::IsFeatureMatched(const std::vector &sourceFeatureFilters, + const std::vector &sinkSupportedFeatures) +{ + for (const auto &filter : sourceFeatureFilters) { + for (const auto &feature : sinkSupportedFeatures) { + if (feature == filter) { + return true; + } + } + } + return false; +} + +int32_t ComponentManager::EnableSinkInternal(const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto &status = dhSinkStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[dhDescriptor.id]; + + // Check if the business is being called repeatedly + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::ENABLED) { + DHLOGE("Repeat call EnableSink, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check reference count + if (enableInfo.refEnable || status.refLoad) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + // Start enabling hardware sink + auto ret = InitCompSink(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("InitCompSink failed, ret = %{public}d.", ret); + return ret; + } + auto sinkResult = StartSink(dhDescriptor.dhType); + if (!WaitForResult(Action::START_SINK, sinkResult)) { + DHLOGE("StartSink failed, some virtual components maybe cannot work, but want to continue!"); + HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, + "dhfwk start sink failed."); + UninitCompSink(dhDescriptor.dhType); + return ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable = 1; + status.refLoad = 1; + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::DisableSinkInternal(const DHDescriptor &dhDescriptor, + int32_t callingUid, int32_t callingPid, sptr &listener) +{ + std::lock_guard lock(dhSinkStatusMtx_); + + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + auto &status = dhSinkStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[dhDescriptor.id]; + + // Check if the business is being called repeatedly + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::DISABLED) { + DHLOGE("Repeat call DisableSink, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check reference count + if (enableInfo.refEnable > 1 || status.refLoad > 1) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + // Start disabling hardware sink + auto sinkResult = StopSink(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SINK, sinkResult)) { + DHLOGE("StopSource failed, but want to continue!"); + return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; + } + auto ret = UninitCompSink(dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("UninitCompSink failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable = 0; + status.refLoad = 0; + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::EnableSourceInternal(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid, sptr &listener) +{ + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + DHStatusSourceEnableInfoKey enableInfoKey { + .networkId = networkId, + .dhId = dhDescriptor.id + }; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + + std::lock_guard lock(dhSourceStatusMtx_); + + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[enableInfoKey]; + + // Check if the business is being called repeatedly + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::ENABLED) { + DHLOGE("Repeat call EnableSource, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check enable reference count + if (enableInfo.refEnable) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + // Check load reference count + if (status.refLoad) { + auto ret = Enable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Enable failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::ENABLED; + enableInfo.refEnable++; + status.refLoad++; + return DH_FWK_SUCCESS; + } + + auto ret = RealEnableSource(networkId, uuid, dhDescriptor, statusCtrl, enableInfo, status); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("RealEnableSource failed, ret = %{public}d.", ret); + return ret; + } + + return DH_FWK_SUCCESS; +} + +int32_t ComponentManager::DisableSourceInternal(const std::string &networkId, + const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid, sptr &listener) +{ + // Check if the input parameters and device type support it + if (!ComponentLoader::GetInstance().IsDHTypeSupport(dhDescriptor.dhType)) { + DHLOGE("Not support dhType: %{public}#X!", dhDescriptor.dhType); + return ERR_DH_FWK_TYPE_NOT_EXIST; + } + + DHStatusSourceEnableInfoKey enableInfoKey { + .networkId = networkId, + .dhId = dhDescriptor.id + }; + DHStatusCtrlKey ctrlKey { + .uid = callingUid, + .pid = callingPid + }; + auto uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + + std::lock_guard lock(dhSourceStatusMtx_); + + auto &status = dhSourceStatus_[dhDescriptor.dhType]; + auto &enableInfo = status.enableInfos[enableInfoKey]; + + // Check if the business is being called repeatedly + auto &statusCtrl = enableInfo.dhStatusCtrl[ctrlKey]; + if (statusCtrl.enableState == EnableState::DISABLED) { + DHLOGE("Repeat call DisableSource, uid = %{public}d, pid = %{public}d.", ctrlKey.uid, ctrlKey.pid); + return ERR_DH_FWK_COMPONENT_REPEAT_CALL; + } + + // Get business enable status listener + auto itrListener = status.listeners.find(ctrlKey); + if (itrListener != status.listeners.end()) { + listener = itrListener->second; + } + + // Check enable reference count + if (enableInfo.refEnable > 1) { + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + // Check load reference count + if (status.refLoad > 1) { + auto ret = Disable(networkId, uuid, dhDescriptor.id, dhDescriptor.dhType); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Disable failed, ret = %{public}d.", ret); + return ret; + } + // Change status, we won't call back directly here because there is a lock + statusCtrl.enableState = EnableState::DISABLED; + enableInfo.refEnable--; + status.refLoad--; + return DH_FWK_SUCCESS; + } + + auto ret = RealDisableSource(networkId, uuid, dhDescriptor, statusCtrl, enableInfo, status); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("RealDisableSource failed, ret = %{public}d.", ret); + return ret; + } + + return DH_FWK_SUCCESS; +} + int32_t ComponentManager::ForceDisableSinkInternal( const DHDescriptor &dhDescriptor, std::vector> &listeners) { @@ -1239,8 +1781,8 @@ int32_t ComponentManager::ForceDisableSourceInternal(const std::string &networkI } // Unload component - auto sinkResult = StopSource(dhDescriptor.dhType); - if (!WaitForResult(Action::STOP_SINK, sinkResult)) { + auto sourceResult = StopSource(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { DHLOGE("StopSource timeout!"); return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; } @@ -1292,8 +1834,8 @@ int32_t ComponentManager::RealDisableSource(const std::string &networkId, const DHLOGE("Disable failed, ret = %{public}d.", ret); return ret; } - auto sinkResult = StopSource(dhDescriptor.dhType); - if (!WaitForResult(Action::STOP_SINK, sinkResult)) { + auto sourceResult = StopSource(dhDescriptor.dhType); + if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { DHLOGE("StopSource timeout!"); return ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT; } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index 7c8fdace..a4f7d7d2 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -64,6 +64,40 @@ std::string VersionInfo::ToJsonString() const return result; } +void ToJson(cJSON *jsonObject, const CompVersion &compVer) +{ + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } + cJSON_AddStringToObject(jsonObject, NAME.c_str(), compVer.name.c_str()); + cJSON_AddNumberToObject(jsonObject, TYPE.c_str(), (double)compVer.dhType); + cJSON_AddStringToObject(jsonObject, HANDLER.c_str(), compVer.handlerVersion.c_str()); + cJSON_AddStringToObject(jsonObject, SOURCE_VER.c_str(), compVer.sourceVersion.c_str()); + cJSON_AddStringToObject(jsonObject, SINK_VER.c_str(), compVer.sinkVersion.c_str()); + if (compVer.haveFeature) { + cJSON *sourceFeatureFilters = cJSON_CreateArray(); + if (sourceFeatureFilters == NULL) { + DHLOGE("Failed to create cJSON object."); + return; + } + cJSON *sinkSupportedFeatures = cJSON_CreateArray(); + if (sinkSupportedFeatures == NULL) { + cJSON_Delete(sourceFeatureFilters); + DHLOGE("Failed to create cJSON object."); + return; + } + for (const auto &filter : compVer.sourceFeatureFilters) { + cJSON_AddItemToArray(sourceFeatureFilters, cJSON_CreateString(filter.c_str())); + } + cJSON_AddItemToObject(jsonObject, SOURCE_FEATURE_FILTER.c_str(), sourceFeatureFilters); + for (const auto &feature : compVer.sinkSupportedFeatures) { + cJSON_AddItemToArray(sinkSupportedFeatures, cJSON_CreateString(feature.c_str())); + } + cJSON_AddItemToObject(jsonObject, SINK_SUPPORTED_FEATURE.c_str(), sinkSupportedFeatures); + } +} + void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo) { if (jsonObject == nullptr) { @@ -85,11 +119,7 @@ void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo) DHLOGE("Failed to create cJSON object."); return; } - cJSON_AddStringToObject(compVer, NAME.c_str(), compVersion.second.name.c_str()); - cJSON_AddNumberToObject(compVer, TYPE.c_str(), (double)compVersion.second.dhType); - cJSON_AddStringToObject(compVer, HANDLER.c_str(), compVersion.second.handlerVersion.c_str()); - cJSON_AddStringToObject(compVer, SOURCE_VER.c_str(), compVersion.second.sourceVersion.c_str()); - cJSON_AddStringToObject(compVer, SINK_VER.c_str(), compVersion.second.sinkVersion.c_str()); + ToJson(compVer, compVersion.second); cJSON_AddItemToArray(compVers, compVer); } cJSON_AddItemToObject(jsonObject, COMP_VER.c_str(), compVers); @@ -121,6 +151,27 @@ void FromJson(const cJSON *jsonObject, CompVersion &compVer) if (IsString(sinkVerJson)) { compVer.sinkVersion = sinkVerJson->valuestring; } + cJSON *sourceFeatureFilters = cJSON_GetObjectItem(jsonObject, SOURCE_FEATURE_FILTER.c_str()); + cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(jsonObject, SINK_SUPPORTED_FEATURE.c_str()); + if (sourceFeatureFilters || sinkSupportedFeatures) { + compVer.haveFeature = true; + if (sourceFeatureFilters) { + cJSON *filterObj = nullptr; + compVer.sourceFeatureFilters.clear(); + cJSON_ArrayForEach(filterObj, sourceFeatureFilters) { + compVer.sourceFeatureFilters.push_back(filterObj->valuestring); + } + } + if (sinkSupportedFeatures) { + cJSON *featureObj = nullptr; + compVer.sinkSupportedFeatures.clear(); + cJSON_ArrayForEach(featureObj, sinkSupportedFeatures) { + compVer.sinkSupportedFeatures.push_back(featureObj->valuestring); + } + } + } else { + compVer.haveFeature = false; + } } void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo) diff --git a/services/distributedhardwarefwkservice/src/task/disable_task.cpp b/services/distributedhardwarefwkservice/src/task/disable_task.cpp index 0314c571..2b30632c 100644 --- a/services/distributedhardwarefwkservice/src/task/disable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/disable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -23,6 +23,7 @@ #include "capability_utils.h" #include "component_manager.h" #include "constants.h" +#include "dh_context.h" #include "dh_utils_hitrace.h" #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" @@ -94,5 +95,99 @@ int32_t DisableTask::UnRegisterHardware() DHTraceEnd(); return result; } + +void DisableTask::SetEffectSink(bool isEffect) +{ + effectSink_ = isEffect; +} + +bool DisableTask::GetEffectSink() +{ + return effectSink_; +} + +void DisableTask::SetEffectSource(bool isEffect) +{ + effectSource_ = isEffect; +} + +bool DisableTask::GetEffectSource() +{ + return effectSource_; +} + +void DisableTask::SetCallingUid(int32_t callingUid) +{ + callingUid_ = callingUid; +} + +int32_t DisableTask::GetCallingUid() +{ + return callingUid_; +} + +void DisableTask::SetCallingPid(int32_t callingPid) +{ + callingPid_ = callingPid; +} + +int32_t DisableTask::GetCallingPid() +{ + return callingPid_; +} + +int32_t DisableTask::DoAutoDisable() +{ + bool disableSink = false; + bool disableSource = false; + int32_t ret = ComponentManager::GetInstance().CheckDemandStart(GetUUID(), GetDhType(), disableSink, disableSource); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("CheckDemandStart failed!"); + return ret; + } + if (DHContext::GetInstance().GetRealTimeOnlineDeviceCount() == 0 && + DHContext::GetInstance().GetIsomerismConnectCount() == 0) { + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (disableSink) { + ret = ComponentManager::GetInstance().ForceDisableSink(dhDescriptor); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSink failed!"); + } + } + if (disableSource) { + ret = ComponentManager::GetInstance().ForceDisableSource(GetNetworkId(), dhDescriptor); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSource failed!"); + } + } + } + return ret; +} + +int32_t DisableTask::DoActiveDisable() +{ + int32_t ret = DH_FWK_SUCCESS; + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (GetEffectSink()) { + ret = ComponentManager::GetInstance().DisableSink(dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSink failed!"); + } + } + if (GetEffectSource()) { + ret = ComponentManager::GetInstance().DisableSource( + GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("DisableSource failed!"); + } + } + return ret; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/enable_task.cpp b/services/distributedhardwarefwkservice/src/task/enable_task.cpp index 1287e69f..e3515166 100644 --- a/services/distributedhardwarefwkservice/src/task/enable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/enable_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -90,5 +90,100 @@ int32_t EnableTask::RegisterHardware() DHTraceEnd(); return result; } + +void EnableTask::SetEffectSink(bool isEffect) +{ + effectSink_ = isEffect; +} + +bool EnableTask::GetEffectSink() +{ + return effectSink_; +} + +void EnableTask::SetEffectSource(bool isEffect) +{ + effectSource_ = isEffect; +} + +bool EnableTask::GetEffectSource() +{ + return effectSource_; +} + +void EnableTask::SetCallingUid(int32_t callingUid) +{ + callingUid_ = callingUid; +} + +int32_t EnableTask::GetCallingUid() +{ + return callingUid_; +} + +void EnableTask::SetCallingPid(int32_t callingPid) +{ + callingPid_ = callingPid; +} + +int32_t EnableTask::GetCallingPid() +{ + return callingPid_; +} + +int32_t EnableTask::DoAutoEnable() +{ + bool enableSink = false; + bool enableSource = false; + int32_t ret = ComponentManager::GetInstance().CheckDemandStart(GetUUID(), GetDhType(), enableSink, enableSource); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("CheckDemandStart failed!"); + return ret; + } + if (!enableSink && !enableSource) { + return ERR_DH_FWK_COMPONENT_LIMIT_DEMAND_START; + } + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (enableSink) { + ret = ComponentManager::GetInstance().EnableSink(dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSink failed!"); + } + } + if (enableSource) { + ret = ComponentManager::GetInstance().EnableSource( + GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSource failed!"); + } + } + return ret; +} + +int32_t EnableTask::DoActiveEnable() +{ + int32_t ret = DH_FWK_SUCCESS; + DHDescriptor dhDescriptor { + .dhType = GetDhType(), + .id = GetDhId() + }; + if (GetEffectSink()) { + ret = ComponentManager::GetInstance().EnableSink(dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSink failed!"); + } + } + if (GetEffectSource()) { + ret = ComponentManager::GetInstance().EnableSource( + GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid()); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("EnableSource failed!"); + } + } + return ret; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/task/online_task.cpp b/services/distributedhardwarefwkservice/src/task/online_task.cpp index eb747826..f5c990c2 100644 --- a/services/distributedhardwarefwkservice/src/task/online_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/online_task.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -87,7 +87,14 @@ void OnLineTask::DoSyncInfo() "udid: %{public}s, udidHash: %{public}s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(deviceId).c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetUDID()).c_str(), GetAnonyString(udidHash).c_str()); - auto ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); + + auto ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("SyncVersionInfoFromDB failed, deviceId = %{public}s, uuid = %{public}s, errCode = %{public}d", + GetAnonyString(deviceId).c_str(), GetAnonyString(GetUUID()).c_str(), ret); + } + + ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); if (ret != DH_FWK_SUCCESS) { DHLOGE("SyncLocalCapabilityInfoFromDB failed, deviceId = %{public}s, errCode = %{public}d", GetAnonyString(deviceId).c_str(), ret); diff --git a/services/distributedhardwarefwkservice/src/task/task_factory.cpp b/services/distributedhardwarefwkservice/src/task/task_factory.cpp index 36ebbfe2..ecb90ba3 100644 --- a/services/distributedhardwarefwkservice/src/task/task_factory.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_factory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -82,5 +82,31 @@ std::shared_ptr TaskFactory::CreateTask(TaskType taskType, TaskParam taskP return task; } + +std::shared_ptr TaskFactory::CreateEnableTask(const TaskParam &taskParam) +{ + auto enableTask = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, + taskParam.dhId, taskParam.dhType); + if (enableTask != nullptr) { + enableTask->SetEffectSink(taskParam.effectSink); + enableTask->SetEffectSource(taskParam.effectSource); + enableTask->SetCallingUid(taskParam.callingUid); + enableTask->SetCallingPid(taskParam.callingPid); + } + return enableTask; +} + +std::shared_ptr TaskFactory::CreateDisableTask(const TaskParam &taskParam) +{ + auto disableTask = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, + taskParam.dhId, taskParam.dhType); + if (disableTask != nullptr) { + disableTask->SetEffectSink(taskParam.effectSink); + disableTask->SetEffectSource(taskParam.effectSource); + disableTask->SetCallingUid(taskParam.callingUid); + disableTask->SetCallingPid(taskParam.callingPid); + } + return disableTask; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee