From 53c321d1db172b8e539b831076d9eddf5ae09463 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Wed, 23 Jul 2025 20:27:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=A1=AC=E4=BB=B6AI?= =?UTF-8?q?=E7=9C=BC=E9=95=9C=E9=9C=80=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../componentmanager/component_manager.h | 7 ++ .../resourcemanager/capability_info_manager.h | 6 - .../local_capability_info_manager.h | 1 + .../include/utils/dh_context.h | 1 + .../componentmanager/component_manager.cpp | 61 ++++++++++ .../src/distributed_hardware_service.cpp | 24 ++-- .../capability_info_manager.cpp | 54 --------- .../local_capability_info_manager.cpp | 15 +++ .../src/transport/dh_comm_tool.cpp | 9 ++ .../src/utils/dh_context.cpp | 16 +++ .../capabilityinfomanager_fuzzer.cpp | 26 ----- .../component_manager/BUILD.gn | 2 + .../include/component_manager_test.h | 15 ++- .../src/component_manager_test.cpp | 106 ++++++++++++++++++ .../local_capability_info_manager_test.cpp | 34 ++++++ .../resourcemanager/resource_manager_test.cpp | 13 --- .../unittest/common/utils/dh_context_test.cpp | 15 +++ 17 files changed, 293 insertions(+), 112 deletions(-) diff --git a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h index 624c00e1..924323c7 100644 --- a/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkservice/include/componentmanager/component_manager.h @@ -102,6 +102,10 @@ public: std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); int32_t DisableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor, std::shared_ptr dhModemExt, IDistributedHardwareSource *&sourcePtr); + void SyncRemoteDeviceInfoBySoftbus(const std::string &realNetworkId, EnableStep enableStep, + const sptr callback); + void OnGetDescriptors(const std::string &realNetworkId, const std::vector &descriptors); + class ComponentManagerEventHandler : public AppExecFwk::EventHandler { public: ComponentManagerEventHandler(const std::shared_ptr runner); @@ -262,6 +266,7 @@ private: void RecoverAutoEnableSource(DHType dhType); void RecoverActiveEnableSink(DHType dhType); void RecoverActiveEnableSource(DHType dhType); + void OnGetDescriptorsError(); private: std::map compSource_; std::shared_mutex compSourceMutex_; @@ -292,6 +297,8 @@ private: std::mutex dhSinkStatusMtx_; std::map dhSourceStatus_; std::mutex dhSourceStatusMtx_; + std::map>> syncDeviceInfoMap_; + std::mutex syncDeviceInfoMapMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h b/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h index 4979c5f0..9ef23460 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h @@ -86,10 +86,6 @@ public: std::shared_ptr GetEventHandler(); void DumpCapabilityInfos(std::vector &capInfos); - void AsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep, - const sptr callback); - void DoAsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep, - const sptr callback); private: void HandleCapabilityAddChange(const std::vector &insertRecords); @@ -103,8 +99,6 @@ private: CapabilityInfoMap globalCapInfoMap_; std::shared_ptr eventHandler_; - std::mutex syncDataMutex_; - std::condition_variable syncDataCondVar_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/local_capability_info_manager.h b/services/distributedhardwarefwkservice/include/resourcemanager/local_capability_info_manager.h index e7b48f6b..0c08b0b8 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/local_capability_info_manager.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/local_capability_info_manager.h @@ -59,6 +59,7 @@ public: int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap); int32_t RemoveLocalInfoInMemByUuid(const std::string &peeruuid); int32_t ClearRemoteDeviceLocalInfoData(const std::string &peeruuid); + std::string GetDhSubtype(const std::string &deviceId, const std::string &dhId); private: mutable std::mutex capInfoMgrMutex_; diff --git a/services/distributedhardwarefwkservice/include/utils/dh_context.h b/services/distributedhardwarefwkservice/include/utils/dh_context.h index 7e20de30..5a336415 100644 --- a/services/distributedhardwarefwkservice/include/utils/dh_context.h +++ b/services/distributedhardwarefwkservice/include/utils/dh_context.h @@ -86,6 +86,7 @@ public: void AddOnlineDeviceType(const std::string &networkId, uint16_t deviceType); void DeleteOnlineDeviceType(const std::string &networkId); uint16_t GetDeviceTypeByNetworkId(const std::string &networkId); + std::string GetDeviceIdByNetworkId(const std::string &networkId); /* DeviceId is which is hashed by sha256 */ std::string GetUUIDByDeviceId(const std::string &deviceId); /** diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 76814600..e235d54f 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -16,6 +16,7 @@ #include "component_manager.h" #include +#include #include #include #include @@ -65,6 +66,7 @@ namespace { constexpr int32_t ENABLE_PARAM_RETRY_TIME = 500 * 1000; constexpr int32_t INVALID_SA_ID = -1; constexpr int32_t UNINIT_COMPONENT_TIMEOUT_SECONDS = 2; + constexpr int32_t SYNC_DATA_TIMEOUT_MS = 1000; } ComponentManager::ComponentManager() : compSource_({}), compSink_({}), compSrcSaId_({}), @@ -2056,5 +2058,64 @@ int32_t ComponentManager::EnableMetaSourceInternal(const std::string &networkId, status.refLoad = 1; return ret; } + +void ComponentManager::SyncRemoteDeviceInfoBySoftbus(const std::string &realNetworkId, EnableStep enableStep, + const sptr callback) +{ + if (callback == nullptr) { + DHLOGE("Param callback is null."); + return; + } + std::lock_guard lock(syncDeviceInfoMapMutex_); + syncDeviceInfoMap_[realNetworkId] = {enableStep, callback}; + std::shared_ptr networkIdPtr = std::make_shared(realNetworkId); + AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(EVENT_DATA_SYNC_MANUAL, networkIdPtr); + if (GetEventHandler() == nullptr) { + DHLOGE("Can not get eventHandler"); + callback->OnError(realNetworkId, ERR_DH_FWK_GETDISTRIBUTEDHARDWARE_TIMEOUT); + syncDeviceInfoMap_.erase(realNetworkId); + return; + } + GetEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + + std::thread([this]() { + this->OnGetDescriptorsError(); + }).detach(); +} + +void ComponentManager::OnGetDescriptors(const std::string &realNetworkId, const std::vector &descriptors) +{ + DHLOGI("OnGetDescriptors enter, networkId = %{public}s, descriptors.size = %{public}zu", + GetAnonyString(realNetworkId).c_str(), descriptors.size()); + if (descriptors.size() == 0) { + DHLOGE("Get dh descriptor faild"); + return; + } + std::lock_guard lock(syncDeviceInfoMapMutex_); + auto iter = syncDeviceInfoMap_.find(realNetworkId); + if (iter != syncDeviceInfoMap_.end()) { + if (iter->second.second != nullptr) { + iter->second.second->OnSuccess(realNetworkId, descriptors, iter->second.first); + syncDeviceInfoMap_.erase(iter); + DHLOGI("Notify get dh descriptor success."); + } + } +} + +void ComponentManager::OnGetDescriptorsError() +{ + DHLOGI("OnGetDescriptorsError enter"); + std::this_thread::sleep_for(std::chrono::milliseconds(SYNC_DATA_TIMEOUT_MS)); + std::lock_guard lock(syncDeviceInfoMapMutex_); + for (auto iter = syncDeviceInfoMap_.begin(); iter != syncDeviceInfoMap_.end();) { + if (iter->second.second != nullptr) { + DHLOGI("OnGetDescriptorsError networkId = %{public}s", GetAnonyString(iter->first).c_str()); + iter->second.second->OnError(iter->first, ERR_DH_FWK_GETDISTRIBUTEDHARDWARE_TIMEOUT); + syncDeviceInfoMap_.erase(iter); + } else { + ++iter; + } + } +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 6d174079..b57ea200 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -17,7 +17,6 @@ #include -#include "constants.h" #include "if_system_ability_manager.h" #include "ipc_skeleton.h" #include "ipc_types.h" @@ -31,9 +30,8 @@ #include "anonymous_string.h" #include "av_trans_control_center.h" #include "capability_info_manager.h" -#include "meta_info_manager.h" #include "component_manager.h" -#include "hdf_operate.h" +#include "constants.h" #include "dh_context.h" #include "dh_utils_tool.h" #include "dh_utils_hisysevent.h" @@ -41,6 +39,9 @@ #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "distributed_hardware_manager_factory.h" +#include "hdf_operate.h" +#include "local_capability_info_manager.h" +#include "meta_info_manager.h" #include "publisher.h" #include "task_executor.h" #include "task_factory.h" @@ -422,22 +423,21 @@ int32_t DistributedHardwareService::GetDistributedHardware(const std::string &ne return DH_FWK_SUCCESS; } - std::vector> capabilities; - CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities); - if (!capabilities.empty()) { - for (const auto &capabilitie : capabilities) { + std::vector> capaInfos; + LocalCapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capaInfos); + if (!capaInfos.empty()) { + for (const auto &capsInfo : capaInfos) { DHDescriptor descriptor; - descriptor.id = capabilitie->GetDHId(); - descriptor.dhType = capabilitie->GetDHType(); + descriptor.id = capsInfo->GetDHId(); + descriptor.dhType = capsInfo->GetDHType(); descriptors.push_back(descriptor); } DHLOGI("Get CapabilitieInfo Success, deviceId: %{public}s.", GetAnonyString(deviceId).c_str()); callback->OnSuccess(realNetworkId, descriptors, enableStep); return DH_FWK_SUCCESS; } - VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId); - CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId); - CapabilityInfoManager::GetInstance()->DoAsyncGetDistributedHardware(realNetworkId, enableStep, callback); + + ComponentManager::GetInstance().SyncRemoteDeviceInfoBySoftbus(realNetworkId, enableStep, callback); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index 9af32875..5543fcaa 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -33,7 +33,6 @@ namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "CapabilityInfoManager" -constexpr int32_t SYNC_DATA_TIMEOUT_MS = 1000 * 10; constexpr const char *GLOBAL_CAPABILITY_INFO_KEY = "global_capability_info"; CapabilityInfoManager::CapabilityInfoManager() : dbAdapterPtr_(nullptr) @@ -399,8 +398,6 @@ void CapabilityInfoManager::HandleCapabilityAddChange(const std::vector condition(syncDataMutex_); - syncDataCondVar_.notify_all(); } void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector &updateRecords) @@ -446,8 +443,6 @@ void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector condition(syncDataMutex_); - syncDataCondVar_.notify_all(); } void CapabilityInfoManager::HandleCapabilityDeleteChange(const std::vector &deleteRecords) @@ -600,55 +595,6 @@ void CapabilityInfoManager::DumpCapabilityInfos(std::vector &cap } } -void CapabilityInfoManager::AsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep, - const sptr callback) -{ - DHLOGI("AsyncGetDistributedHardware networkId: %{public}s.", GetAnonyString(networkId).c_str()); - if (callback == nullptr) { - DHLOGE("callback ptr is null"); - return; - } - int32_t waitTimeMill = SYNC_DATA_TIMEOUT_MS; - while (waitTimeMill > 0) { - auto beginTime = std::chrono::steady_clock::now(); - std::unique_lock locker(syncDataMutex_); - syncDataCondVar_.wait_for(locker, std::chrono::milliseconds(waitTimeMill)); - - std::vector descriptors; - std::vector> capabilities; - std::string deviceId = GetDeviceIdByUUID(DHContext::GetInstance().GetUUIDByNetworkId(networkId)); - CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities); - for (const auto &capabilitie : capabilities) { - DHDescriptor descriptor; - descriptor.id = capabilitie->GetDHId(); - descriptor.dhType = capabilitie->GetDHType(); - descriptors.push_back(descriptor); - } - if (descriptors.size()) { - DHLOGI("AsyncGetDistributedHardware call OnSuccess, networkId: %{public}s.", - GetAnonyString(networkId).c_str()); - callback->OnSuccess(networkId, descriptors, enableStep); - return; - } - - auto endTime = std::chrono::steady_clock::now(); - auto costTime = std::chrono::duration_cast(endTime - beginTime); - waitTimeMill -= costTime.count(); - DHLOGI("AsyncGetDistributedHardware do retry, networkId: %{public}s.", GetAnonyString(networkId).c_str()); - } - callback->OnError(networkId, ERR_DH_FWK_GETDISTRIBUTEDHARDWARE_TIMEOUT); - DHLOGI("AsyncGetDistributedHardware call OnError, networkId: %{public}s.", GetAnonyString(networkId).c_str()); -} - -void CapabilityInfoManager::DoAsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep, - const sptr callback) -{ - DHLOGI("DoAsyncGetDistributedHardware networkId: %{public}s.", GetAnonyString(networkId).c_str()); - std::thread([this, networkId, enableStep, callback]() { - this->AsyncGetDistributedHardware(networkId, enableStep, callback); - }).detach(); -} - std::vector CapabilityInfoManager::GetEntriesByKeys(const std::vector &keys) { if (!IsArrayLengthValid(keys)) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp index e9004549..733c1238 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp @@ -18,6 +18,7 @@ #include "anonymous_string.h" #include "capability_utils.h" #include "constants.h" +#include "capability_info.h" #include "dh_context.h" #include "dh_utils_tool.h" #include "distributed_hardware_errno.h" @@ -287,5 +288,19 @@ int32_t LocalCapabilityInfoManager::ClearRemoteDeviceLocalInfoData(const std::st RemoveLocalInfoInMemByUuid(peeruuid); return DH_FWK_SUCCESS; } + +std::string LocalCapabilityInfoManager::GetDhSubtype(const std::string &deviceId, const std::string &dhId) +{ + if (!IsIdLengthValid(deviceId) || !IsIdLengthValid(dhId)) { + return ""; + } + std::lock_guard lock(capInfoMgrMutex_); + std::string key = GetCapabilityKey(deviceId, dhId); + if (globalCapInfoMap_.find(key) == globalCapInfoMap_.end()) { + DHLOGE("Can not find capability In globalCapInfoMap_: %{public}s", GetAnonyString(deviceId).c_str()); + return ""; + } + return globalCapInfoMap_[key]->GetDHSubtype(); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp index 07463e64..bf1a57e7 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp @@ -290,6 +290,15 @@ void DHCommTool::DHCommToolEventHandler::ProcessFullCapsRsp(const FullCapsRsp &c return; } + std::vector descriptors; + for (auto const &cap : capsRsp.caps) { + DHDescriptor descriptor; + descriptor.id = cap->GetDHId(); + descriptor.dhType = cap->GetDHType(); + descriptors.push_back(descriptor); + } + ComponentManager::GetInstance().OnGetDescriptors(capsRsp.networkId, descriptors); + for (auto const &cap : capsRsp.caps) { if (cap == nullptr) { continue; diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 35c7b7c3..fe2e8426 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -425,5 +425,21 @@ uint16_t DHContext::GetDeviceTypeByNetworkId(const std::string &networkId) } return onlineDevTypeMap_[networkId]; } + +std::string DHContext::GetDeviceIdByNetworkId(const std::string &networkId) +{ + if (!IsIdLengthValid(networkId)) { + return ""; + } + std::unique_lock lock(onlineDevMutex_); + std::string deviceId = ""; + for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { + if (iter->networkId == networkId) { + deviceId = iter->deviceId; + break; + } + } + return deviceId; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp index eaf84fe0..ca0b9d1d 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp @@ -370,30 +370,6 @@ void GetDataByDHTypeFuzzTest(const uint8_t* data, size_t size) CapabilityInfoManager::GetInstance()->GetDataByDHType(dhType, capabilityMap); } -void AsyncGetDistributedHardwareFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - - std::string networkId(reinterpret_cast(data), size); - EnableStep enableStep = static_cast(data[0] % 4); - sptr callback(new TestGetDistributedHardwareCallback()); - CapabilityInfoManager::GetInstance()->AsyncGetDistributedHardware(networkId, enableStep, callback); -} - -void DoAsyncGetDistributedHardwareFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - - std::string networkId(reinterpret_cast(data), size); - EnableStep enableStep = static_cast(data[0] % 4); - sptr callback(new TestGetDistributedHardwareCallback()); - CapabilityInfoManager::GetInstance()->DoAsyncGetDistributedHardware(networkId, enableStep, callback); -} - void GetEntriesByKeysFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size == 0)) { @@ -426,8 +402,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::GetEventHandlerFuzzTest(data, size); OHOS::DistributedHardware::OnChangeFuzzTest(data, size); OHOS::DistributedHardware::GetDataByDHTypeFuzzTest(data, size); - OHOS::DistributedHardware::AsyncGetDistributedHardwareFuzzTest(data, size); - OHOS::DistributedHardware::DoAsyncGetDistributedHardwareFuzzTest(data, size); OHOS::DistributedHardware::GetEntriesByKeysFuzzTest(data, size); return 0; } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn index 5c8f76ca..15eb356a 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/BUILD.gn @@ -23,6 +23,7 @@ config("module_private_config") { "${av_trans_path}/common/include", "include", "${innerkits_path}/include", + "${innerkits_path}/include/ipc", "${utils_path}/include", "${services_path}/distributedhardwarefwkservice/include", "${services_path}/distributedhardwarefwkservice/include/componentloader", @@ -56,6 +57,7 @@ ohos_unittest("ComponentManagerTest") { configs = [ ":module_private_config" ] deps = [ + "${innerkits_path}:libdhfwk_sdk", "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", "${utils_path}:distributedhardwareutils", ] diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test.h index 6b85f502..d8379f63 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/include/component_manager_test.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 @@ -18,6 +18,8 @@ #include "gtest/gtest.h" +#include "get_dh_descriptors_callback_stub.h" + namespace OHOS { namespace DistributedHardware { class ComponentManagerTest : public testing::Test { @@ -26,6 +28,17 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + +public: + class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub { + public: + TestGetDistributedHardwareCallback() = default; + virtual ~TestGetDistributedHardwareCallback() = default; + protected: + void OnSuccess(const std::string &networkId, const std::vector &descriptors, + EnableStep enableStep) override; + void OnError(const std::string &networkId, int32_t error) override; + }; }; } // namespace DistributedHardware } // namespace OHOS 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 b5c11d47..c49e1ed9 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 @@ -164,6 +164,21 @@ void SetDownComponentLoaderConfig() } } +void ComponentManagerTest::TestGetDistributedHardwareCallback::OnSuccess( + const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) +{ + (void)networkId; + (void)descriptors; + (void)enableStep; +} + +void ComponentManagerTest::TestGetDistributedHardwareCallback::OnError( + const std::string &networkId, int32_t error) +{ + (void)networkId; + (void)error; +} + /** * @tc.name: init_test_001 * @tc.desc: Verify the Init function @@ -1257,5 +1272,96 @@ HWTEST_F(ComponentManagerTest, InitAndUnInit_DHCommTool_001, TestSize.Level0) EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().InitDHCommTool()); EXPECT_NO_FATAL_FAILURE(ComponentManager::GetInstance().UnInitDHCommTool()); } + +HWTEST_F(ComponentManagerTest, SyncRemoteDeviceInfoBySoftbus_001, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + sptr callback = nullptr; + EXPECT_NO_FATAL_FAILURE( + ComponentManager::GetInstance().SyncRemoteDeviceInfoBySoftbus(realNetworkId, enableStep, callback)); +} + +HWTEST_F(ComponentManagerTest, SyncRemoteDeviceInfoBySoftbus_002, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + sptr callback(new TestGetDistributedHardwareCallback()); + ComponentManager::GetInstance().eventHandler_ = nullptr; + EXPECT_NO_FATAL_FAILURE( + ComponentManager::GetInstance().SyncRemoteDeviceInfoBySoftbus(realNetworkId, enableStep, callback)); +} + +HWTEST_F(ComponentManagerTest, SyncRemoteDeviceInfoBySoftbus_003, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + sptr callback(new TestGetDistributedHardwareCallback()); + std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); + ComponentManager::GetInstance().eventHandler_ = + std::make_shared(runner); + EXPECT_NO_FATAL_FAILURE( + ComponentManager::GetInstance().SyncRemoteDeviceInfoBySoftbus(realNetworkId, enableStep, callback)); +} + +HWTEST_F(ComponentManagerTest, OnGetDescriptors_001, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + std::vector descriptors; + ComponentManager::GetInstance().OnGetDescriptors(realNetworkId, descriptors); + EXPECT_TRUE(ComponentManager::GetInstance().syncDeviceInfoMap_.empty()); +} + +HWTEST_F(ComponentManagerTest, OnGetDescriptors_002, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + std::vector descriptors; + DHDescriptor descriptor; + descriptor.id = "camera_1"; + descriptor.dhType = DHType::CAMERA; + descriptors.push_back(descriptor); + ComponentManager::GetInstance().syncDeviceInfoMap_[realNetworkId] = {enableStep, nullptr}; + ComponentManager::GetInstance().OnGetDescriptors("realNetworkId_test", descriptors); + EXPECT_FALSE(ComponentManager::GetInstance().syncDeviceInfoMap_.empty()); +} + +HWTEST_F(ComponentManagerTest, OnGetDescriptors_003, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + std::vector descriptors; + DHDescriptor descriptor; + descriptor.id = "camera_1"; + descriptor.dhType = DHType::CAMERA; + descriptors.push_back(descriptor); + ComponentManager::GetInstance().syncDeviceInfoMap_[realNetworkId] = {enableStep, nullptr}; + ComponentManager::GetInstance().OnGetDescriptors(realNetworkId, descriptors); + EXPECT_FALSE(ComponentManager::GetInstance().syncDeviceInfoMap_.empty()); + + sptr callback(new TestGetDistributedHardwareCallback()); + ComponentManager::GetInstance().syncDeviceInfoMap_[realNetworkId] = {enableStep, callback}; + ComponentManager::GetInstance().OnGetDescriptors(realNetworkId, descriptors); + EXPECT_TRUE(ComponentManager::GetInstance().syncDeviceInfoMap_.empty()); +} + +HWTEST_F(ComponentManagerTest, OnGetDescriptorsError_001, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + ComponentManager::GetInstance().syncDeviceInfoMap_[realNetworkId] = {enableStep, nullptr}; + ComponentManager::GetInstance().OnGetDescriptorsError(); + EXPECT_FALSE(ComponentManager::GetInstance().syncDeviceInfoMap_.empty()); +} + +HWTEST_F(ComponentManagerTest, OnGetDescriptorsError_002, TestSize.Level0) +{ + std::string realNetworkId = "realNetworkId_1"; + EnableStep enableStep = EnableStep::ENABLE_SOURCE; + sptr callback(new TestGetDistributedHardwareCallback()); + ComponentManager::GetInstance().syncDeviceInfoMap_[realNetworkId] = {enableStep, callback}; + ComponentManager::GetInstance().OnGetDescriptorsError(); + EXPECT_TRUE(ComponentManager::GetInstance().syncDeviceInfoMap_.empty()); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp index 1dd53aac..f8f4fbfd 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/local_capability_info_manager_test.cpp @@ -291,5 +291,39 @@ HWTEST_F(LocalCapInfoMgrTest, RemoveLocalInfoInMemByUuid_001, TestSize.Level1) ret = LocalCapabilityInfoManager::GetInstance()->RemoveLocalInfoInMemByUuid(peeruuid); EXPECT_EQ(DH_FWK_SUCCESS, ret); } + +HWTEST_F(LocalCapInfoMgrTest, GetDhSubtype_001, TestSize.Level1) +{ + std::string deviceId = ""; + std::string dhId = ""; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + EXPECT_EQ("", ret); + + deviceId = "deviceId_1"; + ret = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + EXPECT_EQ("", ret); + + deviceId = ""; + dhId = "dhId_1"; + ret = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + EXPECT_EQ("", ret); +} + +HWTEST_F(LocalCapInfoMgrTest, GetDhSubtype_002, TestSize.Level1) +{ + std::string peeruuid = "123456789"; + std::string dhid = "audio_132"; + std::string deviceId = Sha256(peeruuid); + + std::shared_ptr capInfo = std::make_shared( + dhid, deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs", "subtype"); + std::string key = deviceId + "###" + dhid; + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo; + auto ret = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype("deviceId_1", dhid); + EXPECT_EQ("", ret); + + ret = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhid); + EXPECT_EQ("subtype", ret); +} } } \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp index 2c541ab6..a8bd3471 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp @@ -925,18 +925,5 @@ HWTEST_F(ResourceManagerTest, GetDataByDHType_001, TestSize.Level1) EXPECT_EQ(DH_FWK_SUCCESS, ret); CapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); } - -HWTEST_F(ResourceManagerTest, AsyncGetDistributedHardware_001, TestSize.Level1) -{ - std::string networkId = "networkId_test"; - EnableStep enableStep = EnableStep::ENABLE_SOURCE; - sptr callback = nullptr; - ASSERT_NO_FATAL_FAILURE(CapabilityInfoManager::GetInstance()->AsyncGetDistributedHardware(networkId, - enableStep, callback)); - - sptr callback1(new (std::nothrow) TestGetDistributedHardwareCallback()); - ASSERT_NO_FATAL_FAILURE(CapabilityInfoManager::GetInstance()->AsyncGetDistributedHardware(networkId, - enableStep, callback1)); -} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/utils/dh_context_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/utils/dh_context_test.cpp index 0cc8386f..1e9e04f0 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/utils/dh_context_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/utils/dh_context_test.cpp @@ -431,5 +431,20 @@ HWTEST_F(DhContextTest, OnMessage_008, TestSize.Level1) cJSON_Delete(json); EXPECT_TRUE(DHContext::GetInstance().connectedDevIds_.empty()); } + +HWTEST_F(DhContextTest, GetDeviceIdByNetworkId_001, TestSize.Level1) +{ + std::string networkId = ""; + auto ret = DHContext::GetInstance().GetDeviceIdByNetworkId(networkId); + EXPECT_EQ("", ret); + + networkId = "networkId_1"; + DHContext::GetInstance().AddOnlineDevice(TEST_UDID, TEST_UUID, TEST_NETWORKID); + ret = DHContext::GetInstance().GetDeviceIdByNetworkId(networkId); + EXPECT_EQ("", ret); + + ret = DHContext::GetInstance().GetDeviceIdByNetworkId(TEST_NETWORKID); + EXPECT_EQ(Sha256(TEST_UUID), ret); +} } } \ No newline at end of file -- Gitee