From 0e3fe54bd93645191533cd71700fd00df1c4c6b7 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Tue, 21 Jan 2025 16:24:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E4=B8=8B=E7=BA=BF=E5=81=B6=E7=8E=B0=E4=B8=80?= =?UTF-8?q?=E8=B5=B7release=E5=9B=9E=E5=90=885.0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../distributedhardwarefwkservice/BUILD.gn | 6 +- .../componentmanager/component_manager.h | 8 ++- .../include/hidumphelper/hidump_helper.h | 5 +- .../componentmanager/component_manager.cpp | 68 ++++++++++++++----- .../src/hidumphelper/hidump_helper.cpp | 36 ++++++---- .../src/component_manager_test.cpp | 16 +++-- 6 files changed, 101 insertions(+), 38 deletions(-) diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 993406cb..675e68bf 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -1,4 +1,4 @@ -# 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 @@ -137,6 +137,10 @@ ohos_shared_library("distributedhardwarefwksvr") { defines += [ "DHARDWARE_OPEN_SOURCE" ] } + if (distributed_hardware_fwk_low_latency) { + defines += [ "DHARDWARE_CHECK_RESOURCE" ] + } + external_deps = [ "ability_base:want", "ability_runtime:ability_manager", diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index f549e4b4..4a7ee0a9 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.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 @@ -59,7 +59,8 @@ public: const DHType dhType); void UpdateBusinessState(const std::string &uuid, const std::string &dhId, BusinessState state); BusinessState QueryBusinessState(const std::string &uuid, const std::string &dhId); - void DumpLoadedComps(std::set &compSourceType, std::set &compSinkType); + void DumpLoadedCompsource(std::set &compSourceType); + void DumpLoadedCompsink(std::set &compSinkType); void Recover(DHType dhType); std::map GetDHSinkInstance(); void TriggerFullCapsSync(const std::string &networkId); @@ -137,10 +138,13 @@ private: int32_t GetCapParam(const std::string &uuid, const std::string &dhId, std::shared_ptr &capability); int32_t GetMetaParam(const std::string &uuid, const std::string &dhId, std::shared_ptr &metaCapPtr); + int32_t CheckSubtypeResource(const std::string &subtype, const std::string &networkId); private: std::map compSource_; + std::shared_mutex compSourceMutex_; std::map compSink_; + std::shared_mutex compSinkMutex_; std::map compSrcSaId_; std::shared_ptr audioCompPrivacy_ = nullptr; std::shared_ptr cameraCompPrivacy_ = nullptr; diff --git a/services/distributedhardwarefwkservice/include/hidumphelper/hidump_helper.h b/services/distributedhardwarefwkservice/include/hidumphelper/hidump_helper.h index a89a5952..8baad59b 100644 --- a/services/distributedhardwarefwkservice/include/hidumphelper/hidump_helper.h +++ b/services/distributedhardwarefwkservice/include/hidumphelper/hidump_helper.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 @@ -21,6 +21,7 @@ #include "enabled_comps_dump.h" #include "device_type.h" +#include "impl_utils.h" #include "single_instance.h" namespace OHOS { @@ -50,6 +51,8 @@ private: int32_t ShowAllCapabilityInfos(std::string &result); int32_t ShowHelp(std::string &result); int32_t ShowIllealInfomation(std::string &result); + void ShowLoadCompSource(const std::set &loadedCompSource, const DHVersion &dhVersion, std::string &result); + void ShowLoadCompSink(const std::set &loadedCompSink, const DHVersion &dhVersion, std::string &result); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 8b372c57..81c7a5c5 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.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 @@ -124,6 +124,7 @@ void ComponentManager::InitComponentHandler() int32_t ComponentManager::InitSAMonitor() { + std::unique_lock lock(compSourceMutex_); if (compMonitorPtr_ == nullptr) { DHLOGE("compMonitorPtr_ is null."); return ERR_DH_FWK_COMPONENT_MONITOR_NULL; @@ -156,6 +157,7 @@ void ComponentManager::StartComponent() 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) { @@ -170,7 +172,7 @@ 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) { @@ -218,6 +220,7 @@ int32_t ComponentManager::UnInit() void ComponentManager::UnInitSAMonitor() { // clear SA monitor + std::unique_lock lock(compSourceMutex_); if (compMonitorPtr_ == nullptr) { DHLOGE("compMonitorPtr_ is null."); return; @@ -232,6 +235,7 @@ void ComponentManager::UnInitSAMonitor() 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) { @@ -244,6 +248,7 @@ void ComponentManager::UnregisterDHStateListener() 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) { @@ -274,11 +279,8 @@ void ComponentManager::StopComponent() DHLOGE("StopSource failed, but want to continue"); } if (!WaitForResult(Action::STOP_SINK, sinkResult)) { - DHLOGE("StopSource failed, but want to continue"); + DHLOGE("StopSink failed, but want to continue"); } - - compSource_.clear(); - compSink_.clear(); } void ComponentManager::StopPrivacy() @@ -298,6 +300,7 @@ 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_) { @@ -321,6 +324,7 @@ ActionResult ComponentManager::StartSource() ActionResult ComponentManager::StartSource(DHType dhType) { DHLOGI("Start Source, dhType: %{public}" PRIu32, (uint32_t)dhType); + std::unique_lock lock(compSourceMutex_); std::unordered_map> futures; if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("Component for DHType: %{public}" PRIu32 " not init source handler", (uint32_t)dhType); @@ -347,6 +351,7 @@ ActionResult ComponentManager::StartSource(DHType dhType) 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_) { @@ -378,6 +383,7 @@ ActionResult ComponentManager::StartSink() ActionResult ComponentManager::StartSink(DHType dhType) { DHLOGI("Start Sink, dhType: %{public}" PRIu32, (uint32_t)dhType); + std::unique_lock lock(compSinkMutex_); std::unordered_map> futures; if (compSink_.find(dhType) == compSink_.end()) { DHLOGE("Component for DHType: %{public}" PRIu32 " not init sink handler", (uint32_t)dhType); @@ -412,6 +418,7 @@ ActionResult ComponentManager::StartSink(DHType dhType) ActionResult ComponentManager::StopSource() { DHLOGI("start."); + std::unique_lock lock(compSourceMutex_); std::unordered_map> futures; for (const auto &item : compSource_) { if (item.second == nullptr) { @@ -425,12 +432,14 @@ ActionResult ComponentManager::StopSource() }).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) { @@ -452,6 +461,7 @@ ActionResult ComponentManager::StopSink() }).detach(); futures.emplace(item.first, f.share()); } + compSink_.clear(); return futures; } @@ -489,6 +499,7 @@ bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsR 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); @@ -513,6 +524,7 @@ bool ComponentManager::InitCompSource() 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); @@ -532,10 +544,11 @@ bool ComponentManager::InitCompSink() int32_t ComponentManager::Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { + DHLOGI("start."); if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) { return ERR_DH_FWK_PARA_INVALID; } - DHLOGI("start."); + std::unique_lock lock(compSourceMutex_); if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str()); return ERR_DH_FWK_PARA_INVALID; @@ -549,15 +562,10 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string return ret; } } - std::string subtype = param.subtype; - std::map resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc(); - if (resourceDesc.find(subtype) == resourceDesc.end()) { - DHLOGE("GetCompResourceDesc failed, subtype: %{public}s", subtype.c_str()); - return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY; - } - if (resourceDesc[subtype] && !IsIdenticalAccount(networkId)) { - DHLOGE("Privacy resources must be logged in with the same account."); - return ERR_DH_FWK_COMPONENT_ENABLE_FAILED; + ret = CheckSubtypeResource(param.subtype, networkId); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("CheckSubtypeResource failed, ret = %{public}d.", ret); + return ret; } auto compEnable = std::make_shared(); @@ -584,6 +592,22 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string return result; } +int32_t ComponentManager::CheckSubtypeResource(const std::string &subtype, const std::string &networkId) +{ +#ifdef DHARDWARE_CHECK_RESOURCE + std::map resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc(); + if (resourceDesc.find(subtype) == resourceDesc.end()) { + DHLOGE("GetCompResourceDesc failed, subtype: %{public}s", subtype.c_str()); + return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY; + } + if (resourceDesc[subtype] && !IsIdenticalAccount(networkId)) { + DHLOGE("Privacy resources must be logged in with the same account."); + return ERR_DH_FWK_COMPONENT_ENABLE_FAILED; + } +#endif + return DH_FWK_SUCCESS; +} + int32_t ComponentManager::RetryGetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType, EnableParam ¶m) { @@ -609,9 +633,11 @@ int32_t ComponentManager::RetryGetEnableParam(const std::string &networkId, cons int32_t ComponentManager::Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { + DHLOGI("start."); if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) { return ERR_DH_FWK_PARA_INVALID; } + std::unique_lock lock(compSourceMutex_); auto find = compSource_.find(dhType); if (find == compSource_.end()) { DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str()); @@ -884,11 +910,17 @@ void ComponentManager::UpdateVersionCache(const std::string &uuid, const Version VersionManager::GetInstance().AddDHVersion(uuid, dhVersion); } -void ComponentManager::DumpLoadedComps(std::set &compSourceType, std::set &compSinkType) +void ComponentManager::DumpLoadedCompsource(std::set &compSourceType) { + std::unique_lock lock(compSourceMutex_); for (auto compSource : compSource_) { compSourceType.emplace(compSource.first); } +} + +void ComponentManager::DumpLoadedCompsink(std::set &compSinkType) +{ + std::unique_lock lock(compSinkMutex_); for (auto compSink : compSink_) { compSinkType.emplace(compSink.first); } @@ -958,6 +990,7 @@ void ComponentManager::RecoverDistributedHardware(DHType dhType) std::map ComponentManager::GetDHSinkInstance() { + std::shared_lock lock(compSinkMutex_); return compSink_; } @@ -1011,6 +1044,7 @@ void ComponentManager::UpdateBusinessState(const std::string &networkId, const s IDistributedHardwareSource* ComponentManager::GetDHSourceInstance(DHType dhType) { + std::unique_lock lock(compSourceMutex_); if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("can not find handler for dhType = %{public}d.", dhType); return nullptr; diff --git a/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp b/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp index a121ac83..3cdbf6d1 100644 --- a/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp +++ b/services/distributedhardwarefwkservice/src/hidumphelper/hidump_helper.cpp @@ -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 @@ -129,17 +129,9 @@ int32_t HidumpHelper::ProcessDump(const HidumpFlag &flag, std::string &result) return errCode; } -int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) +void HidumpHelper::ShowLoadCompSource(const std::set &loadedCompSource, const DHVersion &dhVersion, + std::string &result) { - DHLOGI("Dump all loaded compTypes."); - std::set loadedCompSource {}; - std::set loadedCompSink {}; - ComponentManager::GetInstance().DumpLoadedComps(loadedCompSource, loadedCompSink); - DHVersion dhVersion; - ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); - - result.append("Local loaded components:"); - result.append("\nSource:"); if (!loadedCompSource.empty()) { for (auto compSource : loadedCompSource) { std::string dhTypeStr = "UNKNOWN"; @@ -159,8 +151,11 @@ int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) result.replace(result.size() - 1, 1, "\n"); } } +} - result.append("\nSink:"); +void HidumpHelper::ShowLoadCompSink(const std::set &loadedCompSink, const DHVersion &dhVersion, + std::string &result) +{ if (!loadedCompSink.empty()) { for (auto compSink : loadedCompSink) { std::string dhTypeStr = "UNKNOWN"; @@ -180,6 +175,23 @@ int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) result.replace(result.size() - 1, 1, "\n"); } } +} + +int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) +{ + DHLOGI("Dump all loaded compTypes."); + std::set loadedCompSource {}; + std::set loadedCompSink {}; + ComponentManager::GetInstance().DumpLoadedCompsource(loadedCompSource); + ComponentManager::GetInstance().DumpLoadedCompsink(loadedCompSink); + DHVersion dhVersion; + ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion); + + result.append("Local loaded components:"); + result.append("\nSource:"); + ShowLoadCompSource(loadedCompSource, dhVersion, result); + result.append("\nSink:"); + ShowLoadCompSink(loadedCompSink, dhVersion, result); return DH_FWK_SUCCESS; } 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 447df1ea..1ff66ace 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 @@ -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 @@ -644,16 +644,22 @@ HWTEST_F(ComponentManagerTest, UpdateVersionCache_001, TestSize.Level0) } /** - * @tc.name: DumpLoadedComps_001 - * @tc.desc: Verify the DumpLoadedComps function + * @tc.name: DumpLoadedCompsource_001 + * @tc.desc: Verify the DumpLoadedCompsource function * @tc.type: FUNC * @tc.require: AR000GHSJM */ -HWTEST_F(ComponentManagerTest, DumpLoadedComps_001, TestSize.Level0) +HWTEST_F(ComponentManagerTest, DumpLoadedCompsource_001, TestSize.Level0) { std::set compSourceType; + ComponentManager::GetInstance().DumpLoadedCompsource(compSourceType); + EXPECT_EQ(true, ComponentManager::GetInstance().compSource_.empty()); +} + +HWTEST_F(ComponentManagerTest, DumpLoadedCompsink_001, TestSize.Level0) +{ std::set compSinkType; - ComponentManager::GetInstance().DumpLoadedComps(compSourceType, compSinkType); + ComponentManager::GetInstance().DumpLoadedCompsink(compSinkType); EXPECT_EQ(true, ComponentManager::GetInstance().compSource_.empty()); } -- Gitee From e95db0494046c4855e31d1daef101be9253ec81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=A4=A9=E5=88=9A?= Date: Wed, 22 Jan 2025 01:47:57 +0000 Subject: [PATCH 2/2] update services/distributedhardwarefwkservice/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李天刚 --- services/distributedhardwarefwkservice/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/distributedhardwarefwkservice/BUILD.gn b/services/distributedhardwarefwkservice/BUILD.gn index 675e68bf..38abfe48 100644 --- a/services/distributedhardwarefwkservice/BUILD.gn +++ b/services/distributedhardwarefwkservice/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2025 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -- Gitee