From 396d843fde28d1a24ae387b60dfc78c4e96fccd5 Mon Sep 17 00:00:00 2001 From: zhuzhihui7 Date: Tue, 18 Feb 2025 18:05:15 +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=E5=9B=9B=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 --- .../include/distributed_hardware_fwk_kit.h | 81 ++++- .../src/distributed_hardware_fwk_kit.cpp | 131 ++++++- .../src/ipc/distributed_hardware_proxy.cpp | 28 -- .../componentloader/component_loader.h | 7 +- .../componentmanager/component_manager.h | 18 - .../src/componentloader/component_loader.cpp | 102 ++++-- .../componentmanager/component_manager.cpp | 342 +----------------- .../src/distributed_hardware_stub.cpp | 8 - .../src/task/disable_task.cpp | 18 +- .../src/task/enable_task.cpp | 18 +- .../src/task/task_factory.cpp | 6 +- .../src/component_manager_test.cpp | 87 +---- 12 files changed, 333 insertions(+), 513 deletions(-) diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h index c66ef9de..a531c680 100644 --- a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 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 @@ -24,8 +24,10 @@ #include "refbase.h" #include "distributed_hardware_fwk_kit_paras.h" +#include "dhardware_descriptor.h" #include "ipublisher_listener.h" #include "idistributed_hardware.h" +#include "ihardware_status_listener.h" #ifndef API_EXPORT #define API_EXPORT __attribute__((visibility("default"))) @@ -157,6 +159,83 @@ public: */ API_EXPORT int32_t StopDistributedHardware(DHType dhType, const std::string &networkId); + /** + * @brief Get distributed hardware. + * + * @param networkId distributed hardware networkId. + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t GetDistributedHardware(const std::string &networkId, std::vector &descriptors); + + /** + * @brief Register distributed hardware status listener. + * + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t RegisterDHStatusListener(sptr listener); + + /** + * @brief Unregister distributed hardware status listener. + * + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t UnregisterDHStatusListener(sptr listener); + + /** + * @brief Register distributed hardware status listener. + * + * @param networkId distributed hardware networkId. + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t RegisterDHStatusListener(const std::string &networkId, sptr listener); + + /** + * @brief Unregister distributed hardware status listener. + * + * @param networkId distributed hardware networkId. + * @param listener distributed hardware status listener. + * @return Returns 0 if success. + */ + API_EXPORT int32_t UnregisterDHStatusListener(const std::string &networkId, sptr listener); + + /** + * @brief Enable distributed hardware sink. + * + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t EnableSink(const std::vector &descriptors); + + /** + * @brief Disable distributed hardware sink. + * + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t DisableSink(const std::vector &descriptors); + + /** + * @brief Enable distributed hardware source. + * + * @param networkId distributed hardware networkId. + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t EnableSource(const std::string &networkId, const std::vector &descriptors); + + /** + * @brief Disable distributed hardware source. + * + * @param networkId distributed hardware networkId. + * @param descriptors distributed hardware descriptor list. + * @return Returns 0 if success. + */ + API_EXPORT int32_t DisableSource(const std::string &networkId, const std::vector &descriptors); + private: /** * @brief Determine whether the topic is valid. diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index a37929a9..2ed3502f 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.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 @@ -28,6 +28,23 @@ namespace OHOS { namespace DistributedHardware { + +std::string DumpDescriptors(const std::vector &descriptors) +{ + std::string descriptorsInfo = "["; + for (auto& descriptor : descriptors) { + descriptorsInfo += "["; + descriptorsInfo += "dhType:"; + descriptorsInfo += std::to_string((uint32_t)descriptor.dhType); + descriptorsInfo += ","; + descriptorsInfo += "id:"; + descriptorsInfo += descriptor.id; + descriptorsInfo += "]"; + } + descriptorsInfo += "]"; + return descriptorsInfo; +} + DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false) { DHLOGI("Ctor DistributedHardwareFwkKit"); @@ -251,5 +268,117 @@ int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const "user stop sink ui."); return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId); } + +int32_t DistributedHardwareFwkKit::GetDistributedHardware( + const std::string &networkId, std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Get distributed hardware networkId %{public}s.", GetAnonyString(networkId).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->GetDistributedHardware(networkId, descriptors); +} + +int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(sptr listener) +{ + DHLOGI("Register distributed hardware status sink listener."); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(listener); +} + +int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener(sptr listener) +{ + DHLOGI("Unregister distributed hardware status sink listener."); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(listener); +} + +int32_t DistributedHardwareFwkKit::RegisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Register distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(networkId, listener); +} + +int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener( + const std::string &networkId, sptr listener) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Unregister distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(networkId, listener); +} + +int32_t DistributedHardwareFwkKit::EnableSink(const std::vector &descriptors) +{ + DHLOGI("Enable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSink(descriptors); +} + +int32_t DistributedHardwareFwkKit::DisableSink(const std::vector &descriptors) +{ + DHLOGI("Disable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSink(descriptors); +} + +int32_t DistributedHardwareFwkKit::EnableSource( + const std::string &networkId, const std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Enable distributed hardware source networkId %{public}s, descriptors %{public}s.", + GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSource(networkId, descriptors); +} + +int32_t DistributedHardwareFwkKit::DisableSource( + const std::string &networkId, const std::vector &descriptors) +{ + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } + DHLOGI("Disable distributed hardware source networkId %{public}s, descriptors %{public}s.", + GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str()); + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback."); + return ERR_DH_FWK_POINTER_IS_NULL; + } + return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSource(networkId, descriptors); +} } // DistributedHardware } // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index b4f1a65d..4f89b6b5 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -543,11 +543,9 @@ int32_t DistributedHardwareProxy::GetDistributedHardware( DHLOGE("remote service is null!"); return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; } - MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { DHLOGE("WriteInterfaceToken fail!"); return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; @@ -562,10 +560,8 @@ int32_t DistributedHardwareProxy::GetDistributedHardware( DHLOGE("Send Request failed, ret: %{public}d!", ret); return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; } - descriptors.clear(); ReadDescriptors(reply, descriptors); - return reply.ReadInt32(); } @@ -577,11 +573,9 @@ int32_t DistributedHardwareProxy::RegisterDHStatusListener(sptr &de DHLOGE("remote service is null!"); return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; } - MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { DHLOGE("WriteInterfaceToken fail!"); return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; @@ -735,7 +717,6 @@ int32_t DistributedHardwareProxy::EnableSink(const std::vector &de DHLOGE("Send Request failed, ret: %{public}d!", ret); return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; } - return reply.ReadInt32(); } @@ -747,11 +728,9 @@ int32_t DistributedHardwareProxy::DisableSink(const std::vector &d DHLOGE("remote service is null!"); return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; } - MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { DHLOGE("WriteInterfaceToken fail!"); return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; @@ -765,7 +744,6 @@ int32_t DistributedHardwareProxy::DisableSink(const std::vector &d DHLOGE("Send Request failed, ret: %{public}d!", ret); return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; } - return reply.ReadInt32(); } @@ -781,11 +759,9 @@ int32_t DistributedHardwareProxy::EnableSource( DHLOGE("remote service is null!"); return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; } - MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { DHLOGE("WriteInterfaceToken fail!"); return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; @@ -803,7 +779,6 @@ int32_t DistributedHardwareProxy::EnableSource( DHLOGE("Send Request failed, ret: %{public}d!", ret); return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; } - return reply.ReadInt32(); } @@ -819,11 +794,9 @@ int32_t DistributedHardwareProxy::DisableSource( DHLOGE("remote service is null!"); return ERR_DH_AVT_SERVICE_REMOTE_IS_NULL; } - MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { DHLOGE("WriteInterfaceToken fail!"); return ERR_DH_AVT_SERVICE_WRITE_TOKEN_FAIL; @@ -841,7 +814,6 @@ int32_t DistributedHardwareProxy::DisableSource( DHLOGE("Send Request failed, ret: %{public}d!", ret); return ERR_DH_AVT_SERVICE_IPC_SEND_REQUEST_FAIL; } - return reply.ReadInt32(); } diff --git a/services/distributedhardwarefwkservice/include/componentloader/component_loader.h b/services/distributedhardwarefwkservice/include/componentloader/component_loader.h index 88548103..ff3efe86 100644 --- a/services/distributedhardwarefwkservice/include/componentloader/component_loader.h +++ b/services/distributedhardwarefwkservice/include/componentloader/component_loader.h @@ -62,7 +62,6 @@ struct CompHandler { void *sinkHandler; int32_t sinkSaId; void *hardwareHandler; - std::vector resourceDesc; CompConfig compConfig; }; } @@ -80,9 +79,6 @@ public: int32_t GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr); int32_t GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr); int32_t UnInit(); - int32_t ReleaseHardwareHandler(const DHType dhType); - int32_t ReleaseSource(const DHType dhType); - int32_t ReleaseSink(const DHType dhType); std::vector GetAllCompTypes(); int32_t GetLocalDHVersion(DHVersion &dhVersion); int32_t GetSourceSaId(const DHType dhType); @@ -90,8 +86,11 @@ public: std::map GetCompResourceDesc(); int32_t GetSource(const DHType dhType); + int32_t ReleaseSource(const DHType dhType); int32_t GetSink(const DHType dhType); + int32_t ReleaseSink(const DHType dhType); int32_t GetHardwareHandler(const DHType dhType); + int32_t ReleaseHardwareHandler(const DHType dhType); bool IsDHTypeSupport(DHType dhType); private: diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 55bee59a..e71453a3 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -192,19 +192,13 @@ private: }; DHType GetDHType(const std::string &uuid, const std::string &dhId) const; - bool InitCompSource(); int32_t InitCompSource(DHType dhType); int32_t UninitCompSource(DHType dhType); - bool InitCompSink(); int32_t InitCompSink(DHType dhType); int32_t UninitCompSink(DHType dhType); - ActionResult StartSource(); ActionResult StartSource(DHType dhType); - ActionResult StopSource(); ActionResult StopSource(DHType dhType); - ActionResult StartSink(); ActionResult StartSink(DHType dhType); - ActionResult StopSink(); ActionResult StopSink(DHType dhType); bool WaitForResult(const Action &action, ActionResult result); int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, @@ -220,18 +214,6 @@ private: bool IsIdenticalAccount(const std::string &networkId); int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType, EnableParam ¶m); - void InitComponentHandler(); - int32_t InitSAMonitor(); - void StartComponent(); - void RegisterDHStateListener(); - void RegisterDataSyncTriggerListener(); - void InitDHCommTool(); - - void UnInitSAMonitor(); - void UnregisterDHStateListener(); - void UnregisterDataSyncTriggerListener(); - void UnInitDHCommTool(); - void StopComponent(); void StopPrivacy(); int32_t GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, std::shared_ptr capability); diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 8026aec1..63225e94 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -250,6 +250,9 @@ CompVersion ComponentLoader::GetCompVersionFromComConfig(const CompConfig& cCfg) compVersions.handlerVersion = cCfg.compHandlerVersion; compVersions.sinkVersion = cCfg.compSinkVersion; compVersions.sourceVersion = cCfg.compSourceVersion; + compVersions.haveFeature = cCfg.haveFeature; + compVersions.sourceFeatureFilters = cCfg.sourceFeatureFilters; + compVersions.sinkSupportedFeatures = cCfg.sinkSupportedFeatures; return compVersions; } @@ -351,6 +354,7 @@ void ComponentLoader::ParseCompConfigFromJson(cJSON *component, CompConfig &conf if (IsArray(resourceDescs)) { ParseResourceDescFromJson(resourceDescs, config); } + CheckAndParseFeatures(component, config); } void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config) @@ -461,16 +465,13 @@ void ComponentLoader::GetAllHandler(std::map &dhtypeMap) for (itor = dhtypeMap.begin(); itor != dhtypeMap.end(); ++itor) { CompHandler comHandler; comHandler.type = itor->second.type; - comHandler.hardwareHandler = GetHandler(itor->second.compHandlerLoc); - comHandler.sourceHandler = GetHandler(itor->second.compSourceLoc); comHandler.sourceSaId = itor->second.compSourceSaId; - comHandler.sinkHandler = GetHandler(itor->second.compSinkLoc); comHandler.sinkSaId = itor->second.compSinkSaId; std::vector compResourceDesc = itor->second.compResourceDesc; for (auto it = compResourceDesc.begin(); it != compResourceDesc.end(); it++) { resDescMap_[it->subtype] = it->sensitiveValue; } - comHandler.resourceDesc = itor->second.compResourceDesc; + comHandler.compConfig = itor->second; compHandlerMap_[itor->second.type] = comHandler; } } @@ -478,22 +479,25 @@ void ComponentLoader::GetAllHandler(std::map &dhtypeMap) int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr) { std::lock_guard lock(compHandlerMapMutex_); - if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) { + 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; } - - if (compHandlerMap_[dhType].hardwareHandler == nullptr) { - DHLOGE("hardwareHandler is null."); - return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + CompHandler &compHandler = iter->second; + if (compHandler.hardwareHandler == nullptr) { + compHandler.hardwareHandler = GetHandler(compHandler.compConfig.compHandlerLoc); + if (compHandler.hardwareHandler == nullptr) { + DHLOGE("get hardware handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } } - - GetHardwareClass getHardwareClassHandler = (GetHardwareClass)dlsym(compHandlerMap_[dhType].hardwareHandler, + GetHardwareClass getHardwareClassHandler = (GetHardwareClass)dlsym(compHandler.hardwareHandler, COMPONENT_LOADER_GET_HARDWARE_HANDLER.c_str()); if (getHardwareClassHandler == nullptr) { DHLOGE("get getHardwareClassHandler is null, failed reason : %{public}s", dlerror()); - dlclose(compHandlerMap_[dhType].hardwareHandler); - compHandlerMap_[dhType].hardwareHandler = nullptr; + ReleaseHandler(compHandler.hardwareHandler); + compHandler.hardwareHandler = nullptr; return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } hardwareHandlerPtr = getHardwareClassHandler(); @@ -503,22 +507,25 @@ int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandle int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr) { std::lock_guard lock(compHandlerMapMutex_); - if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) { + 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; } - - if (compHandlerMap_[dhType].sourceHandler == nullptr) { - DHLOGE("sourceHandler is null."); - return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + CompHandler &compHandler = iter->second; + if (compHandler.sourceHandler == nullptr) { + compHandler.sourceHandler = GetHandler(compHandler.compConfig.compSourceLoc); + if (compHandler.sourceHandler == nullptr) { + DHLOGE("get source handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } } - GetSourceHardwareClass getSourceHardClassHandler = (GetSourceHardwareClass)dlsym( - compHandlerMap_[dhType].sourceHandler, COMPONENT_LOADER_GET_SOURCE_HANDLER.c_str()); + compHandler.sourceHandler, COMPONENT_LOADER_GET_SOURCE_HANDLER.c_str()); if (getSourceHardClassHandler == nullptr) { DHLOGE("get getSourceHardClassHandler is null, failed reason : %{public}s", dlerror()); - dlclose(compHandlerMap_[dhType].sourceHandler); - compHandlerMap_[dhType].sourceHandler = nullptr; + ReleaseHandler(compHandler.sourceHandler); + compHandler.sourceHandler = nullptr; return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } sourcePtr = getSourceHardClassHandler(); @@ -528,22 +535,25 @@ int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSour int32_t ComponentLoader::GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr) { std::lock_guard lock(compHandlerMapMutex_); - if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) { + 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; } - - if (compHandlerMap_[dhType].sinkHandler == nullptr) { - DHLOGE("sinkHandler is null."); - return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + CompHandler &compHandler = iter->second; + if (compHandler.sinkHandler == nullptr) { + compHandler.sinkHandler = GetHandler(compHandler.compConfig.compSinkLoc); + if (compHandler.sinkHandler == nullptr) { + DHLOGE("get sink handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } } - GetSinkHardwareClass getSinkHardwareClassHandler = - (GetSinkHardwareClass)dlsym(compHandlerMap_[dhType].sinkHandler, COMPONENT_LOADER_GET_SINK_HANDLER.c_str()); + (GetSinkHardwareClass)dlsym(compHandler.sinkHandler, COMPONENT_LOADER_GET_SINK_HANDLER.c_str()); if (getSinkHardwareClassHandler == nullptr) { DHLOGE("get getSinkHardwareClassHandler is null, failed reason : %{public}s", dlerror()); - dlclose(compHandlerMap_[dhType].sinkHandler); - compHandlerMap_[dhType].sinkHandler = nullptr; + ReleaseHandler(compHandler.sinkHandler); + compHandler.sinkHandler = nullptr; return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } sinkPtr = getSinkHardwareClassHandler(); @@ -619,17 +629,21 @@ int32_t ComponentLoader::UnInit() DHLOGI("release all handler"); DHTraceStart(COMPONENT_RELEASE_START); std::lock_guard lock(compHandlerMapMutex_); - int32_t ret = DH_FWK_SUCCESS; - for (std::map::iterator iter = compHandlerMap_.begin(); - iter != compHandlerMap_.end(); ++iter) { - ret += ReleaseHardwareHandler(iter->first); - ret += ReleaseSource(iter->first); - ret += ReleaseSink(iter->first); + for (auto iter = compHandlerMap_.begin(); iter != compHandlerMap_.end(); ++iter) { + if (iter->second.sinkHandler) { + ReleaseHandler(iter->second.sinkHandler); + } + if (iter->second.sourceHandler) { + ReleaseHandler(iter->second.sourceHandler); + } + if (iter->second.hardwareHandler) { + ReleaseHandler(iter->second.hardwareHandler); + } } compHandlerMap_.clear(); resDescMap_.clear(); DHTraceEnd(); - return ret; + return DH_FWK_SUCCESS; } int32_t ComponentLoader::ReleaseHardwareHandler(const DHType dhType) @@ -637,12 +651,16 @@ int32_t ComponentLoader::ReleaseHardwareHandler(const DHType dhType) if (!IsDHTypeExist(dhType)) { return ERR_DH_FWK_TYPE_NOT_EXIST; } + if (!IsDHTypeHandlerLoaded(dhType)) { + return ERR_DH_FWK_LOADER_HANDLER_UNLOAD; + } int32_t ret = ReleaseHandler(compHandlerMap_[dhType].hardwareHandler); if (ret) { DHLOGE("fail, dhType: %{public}#X", dhType); HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, dhType, ret, "dhfwk release hardware handler failed."); } + compHandlerMap_[dhType].hardwareHandler = nullptr; return ret; } @@ -651,12 +669,16 @@ int32_t ComponentLoader::ReleaseSource(const DHType dhType) if (!IsDHTypeExist(dhType)) { return ERR_DH_FWK_TYPE_NOT_EXIST; } + if (!IsDHTypeSourceLoaded(dhType)) { + return ERR_DH_FWK_LOADER_SOURCE_UNLOAD; + } int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sourceHandler); if (ret) { DHLOGE("fail, dhType: %{public}#X", dhType); HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, dhType, ret, "dhfwk release source failed."); } + compHandlerMap_[dhType].sourceHandler = nullptr; return ret; } @@ -665,12 +687,16 @@ int32_t ComponentLoader::ReleaseSink(const DHType dhType) if (!IsDHTypeExist(dhType)) { return ERR_DH_FWK_TYPE_NOT_EXIST; } + if (!IsDHTypeSinkLoaded(dhType)) { + return ERR_DH_FWK_LOADER_SINK_UNLOAD; + } int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sinkHandler); if (ret) { DHLOGE("fail, dhType: %{public}#X", dhType); HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, dhType, ret, "dhfwk release sink failed."); } + compHandlerMap_[dhType].sinkHandler = nullptr; return ret; } diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 22a76b96..8ddc61c8 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -91,18 +91,6 @@ int32_t ComponentManager::Init() { DHLOGI("start."); DHTraceStart(COMPONENT_INIT_START); - InitComponentHandler(); - - int32_t ret = InitSAMonitor(); - if (ret != DH_FWK_SUCCESS) { - DHLOGE("Init SA monitor failed, ret: %{public}d", ret); - return ret; - } - - StartComponent(); - RegisterDHStateListener(); - RegisterDataSyncTriggerListener(); - InitDHCommTool(); #ifdef DHARDWARE_LOW_LATENCY Publisher::GetInstance().RegisterListener(DHTopic::TOPIC_LOW_LATENCY, lowLatencyListener_); #endif @@ -111,180 +99,22 @@ int32_t ComponentManager::Init() return DH_FWK_SUCCESS; } -void ComponentManager::InitComponentHandler() -{ - DHLOGI("start."); - if (!InitCompSource()) { - DHLOGE("InitCompSource failed."); - DHTraceEnd(); - } - if (!InitCompSink()) { - DHLOGE("InitCompSink failed."); - DHTraceEnd(); - } -} - -int32_t ComponentManager::InitSAMonitor() -{ - std::unique_lock lock(compSourceMutex_); - if (compMonitorPtr_ == nullptr) { - DHLOGE("compMonitorPtr_ is null."); - return ERR_DH_FWK_COMPONENT_MONITOR_NULL; - } - for (const auto &comp : compSource_) { - if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) { - continue; - } - compMonitorPtr_->AddSAMonitor(compSrcSaId_.at(comp.first)); - } - return DH_FWK_SUCCESS; -} - -void ComponentManager::StartComponent() -{ - auto sourceResult = StartSource(); - auto sinkResult = StartSink(); - - if (!WaitForResult(Action::START_SOURCE, sourceResult)) { - DHLOGE("StartSource failed, some virtual components maybe cannot work, but want to continue"); - HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, - "dhfwk start source failed."); - } - 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."); - } -} - -void ComponentManager::RegisterDHStateListener() -{ - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Register DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->RegisterDistributedHardwareStateListener(dhStateListener_); - } -} - -void ComponentManager::RegisterDataSyncTriggerListener() -{ - std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); - eventHandler_ = std::make_shared(runner); - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Register Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->RegisterDataSyncTriggerListener(dataSyncTriggerListener_); - } -} - -void ComponentManager::InitDHCommTool() -{ - if (dhCommToolPtr_ == nullptr) { - DHLOGE("DH communication tool ptr is null"); - return; - } - DHLOGI("Init DH communication tool"); - dhCommToolPtr_->Init(); -} - int32_t ComponentManager::UnInit() { DHLOGI("start."); - UnregisterDHStateListener(); - UnregisterDataSyncTriggerListener(); - UnInitDHCommTool(); StopPrivacy(); - UnInitSAMonitor(); - StopComponent(); - #ifdef DHARDWARE_LOW_LATENCY Publisher::GetInstance().UnregisterListener(DHTopic::TOPIC_LOW_LATENCY, lowLatencyListener_); LowLatency::GetInstance().CloseLowLatency(); #endif DHLOGI("Release component success"); - if (isUnInitTimeOut_.load()) { DHLOGE("Some component stop timeout, FORCE exit!"); _Exit(0); } - return DH_FWK_SUCCESS; } -void ComponentManager::UnInitSAMonitor() -{ - // clear SA monitor - std::unique_lock lock(compSourceMutex_); - if (compMonitorPtr_ == nullptr) { - DHLOGE("compMonitorPtr_ is null."); - return; - } - for (const auto &comp : compSource_) { - if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) { - continue; - } - compMonitorPtr_->RemoveSAMonitor(compSrcSaId_.at(comp.first)); - } -} - -void ComponentManager::UnregisterDHStateListener() -{ - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Unregister DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->UnregisterDistributedHardwareStateListener(); - } -} - -void ComponentManager::UnregisterDataSyncTriggerListener() -{ - std::unique_lock lock(compSourceMutex_); - for (const auto &item : compSource_) { - DHLOGI("Unregister Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first); - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - item.second->UnregisterDataSyncTriggerListener(); - } -} - -void ComponentManager::UnInitDHCommTool() -{ - if (dhCommToolPtr_ == nullptr) { - DHLOGE("DH communication tool ptr is null"); - return; - } - DHLOGI("UnInit DH communication tool"); - dhCommToolPtr_->UnInit(); -} - -void ComponentManager::StopComponent() -{ - // stop source and sink sa - auto sourceResult = StopSource(); - auto sinkResult = StopSink(); - - if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) { - DHLOGE("StopSource failed, but want to continue"); - } - if (!WaitForResult(Action::STOP_SINK, sinkResult)) { - DHLOGE("StopSink failed, but want to continue"); - } -} - void ComponentManager::StopPrivacy() { // stop privacy @@ -299,30 +129,6 @@ void ComponentManager::StopPrivacy() } } -ActionResult ComponentManager::StartSource() -{ - DHLOGI("start."); - std::unique_lock lock(compSourceMutex_); - std::unordered_map> futures; - std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; - for (const auto &item : compSource_) { - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - CompVersion compversion; - VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); - auto params = compversion.sourceVersion; - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item, params] () mutable { - p.set_value(item.second->InitSource(params)); - }).detach(); - futures.emplace(item.first, f.share()); - } - return futures; -} - ActionResult ComponentManager::StartSource(DHType dhType) { DHLOGI("Start Source, dhType: %{public}" PRIu32, (uint32_t)dhType); @@ -350,38 +156,6 @@ ActionResult ComponentManager::StartSource(DHType dhType) return futures; } -ActionResult ComponentManager::StartSink() -{ - DHLOGI("start."); - std::unique_lock lock(compSinkMutex_); - std::unordered_map> futures; - std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid; - for (const auto &item : compSink_) { - if (item.second == nullptr) { - DHLOGE("comp sink ptr is null"); - continue; - } - CompVersion compversion; - VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion); - auto params = compversion.sinkVersion; - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item, params] () mutable { - p.set_value(item.second->InitSink(params)); - }).detach(); - futures.emplace(item.first, f.share()); - if (cameraCompPrivacy_ == nullptr && item.first == DHType::CAMERA) { - cameraCompPrivacy_ = std::make_shared(); - item.second->RegisterPrivacyResources(cameraCompPrivacy_); - } - if (audioCompPrivacy_ == nullptr && item.first == DHType::AUDIO) { - audioCompPrivacy_ = std::make_shared(); - item.second->RegisterPrivacyResources(audioCompPrivacy_); - } - } - return futures; -} - ActionResult ComponentManager::StartSink(DHType dhType) { DHLOGI("Start Sink, dhType: %{public}" PRIu32, (uint32_t)dhType); @@ -417,56 +191,6 @@ ActionResult ComponentManager::StartSink(DHType dhType) return futures; } -ActionResult ComponentManager::StopSource() -{ - DHLOGI("start."); - std::unique_lock lock(compSourceMutex_); - std::unordered_map> futures; - for (const auto &item : compSource_) { - if (item.second == nullptr) { - DHLOGE("comp source ptr is null"); - continue; - } - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item] () mutable { - p.set_value(item.second->ReleaseSource()); - }).detach(); - futures.emplace(item.first, f.share()); - } - compSource_.clear(); - return futures; -} - -ActionResult ComponentManager::StopSink() -{ - DHLOGI("start."); - std::unique_lock lock(compSinkMutex_); - std::unordered_map> futures; - for (const auto &item : compSink_) { - if (item.second == nullptr) { - DHLOGE("comp sink ptr is null"); - continue; - } - std::promise p; - std::future f = p.get_future(); - std::thread([p = std::move(p), item] () mutable { - p.set_value(item.second->ReleaseSink()); - IHardwareHandler *hardwareHandler = nullptr; - int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(item.first, hardwareHandler); - if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) { - DHLOGE("GetHardwareHandler %{public}#X failed", item.first); - return status; - } - hardwareHandler->UnRegisterPluginListener(); - return status; - }).detach(); - futures.emplace(item.first, f.share()); - } - compSink_.clear(); - return futures; -} - bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsResult) { DHLOGD("start."); @@ -498,51 +222,6 @@ bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsR return ret; } -bool ComponentManager::InitCompSource() -{ - auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); - std::unique_lock lock(compSourceMutex_); - for (const auto &type : compTypes) { - IDistributedHardwareSource *sourcePtr = nullptr; - auto ret = ComponentLoader::GetInstance().GetSource(type, sourcePtr); - if (ret != DH_FWK_SUCCESS) { - DHLOGW("GetSource failed, compType = %{public}#X, ret = %{public}d.", type, ret); - continue; - } - if (sourcePtr == nullptr) { - DHLOGW("sourcePtr is null, compType = %{public}#X.", type); - continue; - } - compSource_.insert(std::make_pair(type, sourcePtr)); - - int32_t saId = ComponentLoader::GetInstance().GetSourceSaId(type); - if (saId != INVALID_SA_ID) { - compSrcSaId_.insert(std::make_pair(type, saId)); - } - } - return !compSource_.empty(); -} - -bool ComponentManager::InitCompSink() -{ - auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes(); - std::unique_lock lock(compSinkMutex_); - for (const auto &type : compTypes) { - IDistributedHardwareSink *sinkPtr = nullptr; - auto ret = ComponentLoader::GetInstance().GetSink(type, sinkPtr); - if (ret != DH_FWK_SUCCESS) { - DHLOGW("GetSink failed, compType = %{public}#X, ret = %{public}d.", type, ret); - continue; - } - if (sinkPtr == nullptr) { - DHLOGW("sinkPtr is null, compType = %{public}#X.", type); - continue; - } - compSink_.insert(std::make_pair(type, sinkPtr)); - } - return !compSink_.empty(); -} - int32_t ComponentManager::Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { @@ -1311,6 +990,7 @@ int32_t ComponentManager::EnableSink(const DHDescriptor &dhDescriptor, int32_t c if (ret == DH_FWK_SUCCESS) { if (listener) { listener->OnEnable(dhDescriptor); + DHLOGI("Callback business sink OnEnable."); } } return ret; @@ -1323,6 +1003,7 @@ int32_t ComponentManager::DisableSink(const DHDescriptor &dhDescriptor, int32_t if (ret == DH_FWK_SUCCESS) { if (listener) { listener->OnDisable(dhDescriptor); + DHLOGI("Callback business sink OnDisable."); } } return ret; @@ -1336,6 +1017,7 @@ int32_t ComponentManager::EnableSource(const std::string &networkId, if (ret == DH_FWK_SUCCESS) { if (listener) { listener->OnEnable(networkId, dhDescriptor); + DHLOGI("Callback business source OnEnable."); } } return ret; @@ -1349,6 +1031,7 @@ int32_t ComponentManager::DisableSource(const std::string &networkId, if (ret == DH_FWK_SUCCESS) { if (listener) { listener->OnDisable(networkId, dhDescriptor); + DHLOGI("Callback business source OnDisable."); } } return ret; @@ -1361,6 +1044,7 @@ int32_t ComponentManager::ForceDisableSink(const DHDescriptor &dhDescriptor) if (ret == DH_FWK_SUCCESS) { for (auto listener : listeners) { listener->OnDisable(dhDescriptor); + DHLOGI("Callback business sink OnDisable."); } } return ret; @@ -1373,6 +1057,7 @@ int32_t ComponentManager::ForceDisableSource(const std::string &networkId, const if (ret == DH_FWK_SUCCESS) { for (auto listener : listeners) { listener->OnDisable(networkId, dhDescriptor); + DHLOGI("Callback business source OnDisable."); } } return ret; @@ -1868,6 +1553,10 @@ int32_t ComponentManager::InitCompSource(DHType dhType) auto saId = ComponentLoader::GetInstance().GetSourceSaId(dhType); if (saId != INVALID_SA_ID) { compSrcSaId_.insert(std::make_pair(dhType, saId)); + if (compMonitorPtr_ == nullptr) { + DHLOGE("compMonitorPtr_ is null."); + return ERR_DH_FWK_COMPONENT_MONITOR_NULL; + } compMonitorPtr_->AddSAMonitor(saId); } else { DHLOGE("GetSourceSaId return INVALID_SA_ID, compType = %{public}#X.", dhType); @@ -1892,13 +1581,20 @@ int32_t ComponentManager::UninitCompSource(DHType dhType) } sourcePtr->UnregisterDataSyncTriggerListener(); sourcePtr->UnregisterDistributedHardwareStateListener(); - compMonitorPtr_->RemoveSAMonitor(compSrcSaId_.at(dhType)); + auto it = compSrcSaId_.find(dhType); + if (it != compSrcSaId_.end()) { + if (compMonitorPtr_ == nullptr) { + DHLOGE("compMonitorPtr_ is null."); + return ERR_DH_FWK_COMPONENT_MONITOR_NULL; + } + compMonitorPtr_->RemoveSAMonitor(it->second); + compSrcSaId_.erase(it); + } ret = ComponentLoader::GetInstance().ReleaseSource(dhType); if (ret != DH_FWK_SUCCESS) { DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); return ret; } - compSrcSaId_.erase(dhType); compSource_.erase(dhType); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp index 3c119127..d1466d0f 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_stub.cpp @@ -395,7 +395,6 @@ int32_t DistributedHardwareStub::GetDistributedHardwareInner(MessageParcel &data DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::vector descriptors; std::string networkId = data.ReadString(); int32_t ret = GetDistributedHardware(networkId, descriptors); @@ -454,7 +453,6 @@ int32_t DistributedHardwareStub::RegisterDHStatusSourceListenerInner(MessageParc DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::string networkId = data.ReadString(); sptr listener = iface_cast(data.ReadRemoteObject()); if (listener == nullptr) { @@ -475,7 +473,6 @@ int32_t DistributedHardwareStub::UnregisterDHStatusSourceListenerInner(MessagePa DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::string networkId = data.ReadString(); sptr listener = iface_cast(data.ReadRemoteObject()); if (listener == nullptr) { @@ -496,7 +493,6 @@ int32_t DistributedHardwareStub::EnableSinkInner(MessageParcel &data, MessagePar DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::vector descriptors; ReadDescriptors(data, descriptors); int32_t ret = EnableSink(descriptors); @@ -513,7 +509,6 @@ int32_t DistributedHardwareStub::DisableSinkInner(MessageParcel &data, MessagePa DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::vector descriptors; ReadDescriptors(data, descriptors); int32_t ret = DisableSink(descriptors); @@ -530,7 +525,6 @@ int32_t DistributedHardwareStub::EnableSourceInner(MessageParcel &data, MessageP DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::string networkId = data.ReadString(); std::vector descriptors; ReadDescriptors(data, descriptors); @@ -548,7 +542,6 @@ int32_t DistributedHardwareStub::DisableSourceInner(MessageParcel &data, Message DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission."); return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL; } - std::string networkId = data.ReadString(); std::vector descriptors; ReadDescriptors(data, descriptors); @@ -599,7 +592,6 @@ int32_t DistributedHardwareStub::WriteDescriptors(MessageParcel &data, const std return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL; } } - return NO_ERROR; } diff --git a/services/distributedhardwarefwkservice/src/task/disable_task.cpp b/services/distributedhardwarefwkservice/src/task/disable_task.cpp index 2b30632c..3c875fef 100644 --- a/services/distributedhardwarefwkservice/src/task/disable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/disable_task.cpp @@ -88,12 +88,22 @@ void DisableTask::DoTaskInner() int32_t DisableTask::UnRegisterHardware() { DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_DISABLE_START); - auto result = ComponentManager::GetInstance().Disable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType()); - DHLOGI("disable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s", - (result == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), + + int32_t ret = DH_FWK_SUCCESS; + + // Determine whether it is an active disable + if (GetCallingUid() || GetCallingPid()) { + // It is an active disable + ret = DoActiveDisable(); + } else { + // It is an auto disable + ret = DoAutoDisable(); + } + DHLOGI("disable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s.", + (ret == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str()); DHTraceEnd(); - return result; + return ret; } void DisableTask::SetEffectSink(bool isEffect) diff --git a/services/distributedhardwarefwkservice/src/task/enable_task.cpp b/services/distributedhardwarefwkservice/src/task/enable_task.cpp index e3515166..90a61a4d 100644 --- a/services/distributedhardwarefwkservice/src/task/enable_task.cpp +++ b/services/distributedhardwarefwkservice/src/task/enable_task.cpp @@ -83,12 +83,22 @@ void EnableTask::DoTaskInner() int32_t EnableTask::RegisterHardware() { DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_ENABLE_START); - auto result = ComponentManager::GetInstance().Enable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType()); - DHLOGI("enable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s", - (result == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), + + int32_t ret = DH_FWK_SUCCESS; + + // Determine whether it is an active enable + if (GetCallingUid() || GetCallingPid()) { + // It is an active enable + ret = DoActiveEnable(); + } else { + // It is an auto enable + ret = DoAutoEnable(); + } + DHLOGI("enable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s.", + (ret == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str()); DHTraceEnd(); - return result; + return ret; } void EnableTask::SetEffectSink(bool isEffect) diff --git a/services/distributedhardwarefwkservice/src/task/task_factory.cpp b/services/distributedhardwarefwkservice/src/task/task_factory.cpp index ecb90ba3..27f791c7 100644 --- a/services/distributedhardwarefwkservice/src/task/task_factory.cpp +++ b/services/distributedhardwarefwkservice/src/task/task_factory.cpp @@ -39,13 +39,11 @@ std::shared_ptr TaskFactory::CreateTask(TaskType taskType, TaskParam taskP std::shared_ptr task = nullptr; switch (taskType) { case TaskType::ENABLE: { - task = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, - taskParam.dhId, taskParam.dhType); + task = CreateEnableTask(taskParam); break; } case TaskType::DISABLE: { - task = std::make_shared(taskParam.networkId, taskParam.uuid, taskParam.udid, - taskParam.dhId, taskParam.dhType); + task = CreateDisableTask(taskParam); break; } case TaskType::ON_LINE: { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp index 9bd53e90..d2bc80c6 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp @@ -174,7 +174,7 @@ HWTEST_F(ComponentManagerTest, init_compSource_test_001, TestSize.Level0) { ComponentLoader::GetInstance().Init(); ComponentManager::GetInstance().compSource_.clear(); - auto ret = ComponentManager::GetInstance().InitCompSource(); + auto ret = ComponentManager::GetInstance().InitCompSource(DHType::INPUT); EXPECT_NE(ret, ComponentManager::GetInstance().compSource_.empty()); } @@ -188,7 +188,7 @@ HWTEST_F(ComponentManagerTest, init_compSink_test_001, TestSize.Level0) { ComponentLoader::GetInstance().Init(); ComponentManager::GetInstance().compSink_.clear(); - auto ret = ComponentManager::GetInstance().InitCompSink(); + auto ret = ComponentManager::GetInstance().InitCompSink(DHType::INPUT); EXPECT_NE(ret, ComponentManager::GetInstance().compSink_.empty()); } @@ -302,7 +302,7 @@ HWTEST_F(ComponentManagerTest, StartSource_001, TestSize.Level0) DHType dhType = DHType::CAMERA; IDistributedHardwareSource *sourcePtr = nullptr; ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - auto ret = ComponentManager::GetInstance().StartSource(); + auto ret = ComponentManager::GetInstance().StartSource(dhType); EXPECT_EQ(true, ret.empty()); ComponentManager::GetInstance().compSource_.clear(); } @@ -333,22 +333,6 @@ HWTEST_F(ComponentManagerTest, StartSource_002, TestSize.Level0) * @tc.require: AR000GHSJM */ HWTEST_F(ComponentManagerTest, StartSink_001, TestSize.Level0) -{ - DHType dhType = DHType::CAMERA; - IDistributedHardwareSink *sinkPtr = nullptr; - ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); - auto ret = ComponentManager::GetInstance().StartSink(); - EXPECT_EQ(true, ret.empty()); - ComponentManager::GetInstance().compSink_.clear(); -} - -/** - * @tc.name: StartSink_002 - * @tc.desc: Verify the StartSink function - * @tc.type: FUNC - * @tc.require: AR000GHSJM - */ -HWTEST_F(ComponentManagerTest, StartSink_002, TestSize.Level0) { DHType dhType = DHType::CAMERA; IDistributedHardwareSink *sinkPtr = nullptr; @@ -369,7 +353,7 @@ HWTEST_F(ComponentManagerTest, StopSource_001, TestSize.Level0) DHType dhType = DHType::CAMERA; IDistributedHardwareSource *sourcePtr = nullptr; ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - auto ret = ComponentManager::GetInstance().StopSource(); + auto ret = ComponentManager::GetInstance().StopSource(dhType); EXPECT_EQ(true, ret.empty()); ComponentManager::GetInstance().compSource_.clear(); } @@ -385,7 +369,7 @@ HWTEST_F(ComponentManagerTest, StopSink_001, TestSize.Level0) DHType dhType = DHType::CAMERA; IDistributedHardwareSink *sinkPtr = nullptr; ComponentManager::GetInstance().compSink_.insert(std::make_pair(dhType, sinkPtr)); - auto ret = ComponentManager::GetInstance().StopSink(); + auto ret = ComponentManager::GetInstance().StopSink(dhType); EXPECT_EQ(true, ret.empty()); ComponentManager::GetInstance().compSink_.clear(); } @@ -413,7 +397,7 @@ HWTEST_F(ComponentManagerTest, WaitForResult_001, TestSize.Level0) */ HWTEST_F(ComponentManagerTest, InitCompSource_001, TestSize.Level0) { - bool ret = ComponentManager::GetInstance().InitCompSource(); + bool ret = ComponentManager::GetInstance().InitCompSource(DHType::AUDIO); EXPECT_EQ(true, ret); } #endif @@ -426,7 +410,7 @@ HWTEST_F(ComponentManagerTest, WaitForResult_001, TestSize.Level0) */ HWTEST_F(ComponentManagerTest, InitCompSink_001, TestSize.Level0) { - bool ret = ComponentManager::GetInstance().InitCompSink(); + bool ret = ComponentManager::GetInstance().InitCompSink(DHType::AUDIO); EXPECT_EQ(true, ret); } @@ -1068,62 +1052,5 @@ HWTEST_F(ComponentManagerTest, GetDHSourceInstance_002, TestSize.Level0) EXPECT_EQ(nullptr, dhSourcePtr); ComponentManager::GetInstance().compSource_.clear(); } - -HWTEST_F(ComponentManagerTest, InitSAMonitor_001, TestSize.Level0) -{ - ComponentManager::GetInstance().compMonitorPtr_ = std::make_shared(); - DHType dhType = DHType::CAMERA; - IDistributedHardwareSource *sourcePtr = nullptr; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(DHType::AUDIO, 1)); - auto ret = ComponentManager::GetInstance().InitSAMonitor(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(dhType, 1)); - ret = ComponentManager::GetInstance().InitSAMonitor(); - EXPECT_EQ(DH_FWK_SUCCESS, ret); - ComponentManager::GetInstance().compSource_.clear(); - ComponentManager::GetInstance().compSrcSaId_.clear(); -} - -HWTEST_F(ComponentManagerTest, RegisterDHStateListener_001, TestSize.Level0) -{ - ComponentManager::GetInstance().compMonitorPtr_ = std::make_shared(); - DHType dhType = DHType::CAMERA; - IDistributedHardwareSource *sourcePtr = nullptr; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RegisterDHStateListener()); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnregisterDHStateListener()); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().RegisterDataSyncTriggerListener()); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnregisterDataSyncTriggerListener()); - ComponentManager::GetInstance().compSource_.clear(); -} - -HWTEST_F(ComponentManagerTest, InitDHCommTool_001, TestSize.Level0) -{ - ComponentManager::GetInstance().dhCommToolPtr_ = nullptr; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().InitDHCommTool()); -} - -HWTEST_F(ComponentManagerTest, UnInitSAMonitor_001, TestSize.Level0) -{ - ComponentManager::GetInstance().compMonitorPtr_ = std::make_shared(); - DHType dhType = DHType::CAMERA; - IDistributedHardwareSource *sourcePtr = nullptr; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(DHType::AUDIO, 1)); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitSAMonitor()); - - ComponentManager::GetInstance().compSrcSaId_.insert(std::make_pair(dhType, 1)); - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitSAMonitor()); - ComponentManager::GetInstance().compSource_.clear(); - ComponentManager::GetInstance().compSrcSaId_.clear(); -} - -HWTEST_F(ComponentManagerTest, UnInitDHCommTool_001, TestSize.Level0) -{ - ComponentManager::GetInstance().dhCommToolPtr_ = nullptr; - EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitDHCommTool()); -} } // namespace DistributedHardware } // namespace OHOS -- Gitee