From 5c57eca3916267abe31402e1f859058f397d7eaf Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Thu, 4 Sep 2025 19:17:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8E=A5=E5=8F=A3=E8=8E=B7?= =?UTF-8?q?=E5=8F=96subtype?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../resourcemanager/capability_info_manager.h | 1 + .../componentmanager/component_manager.cpp | 52 ++++++++--- .../capability_info_manager.cpp | 18 ++++ .../src/component_manager_test.cpp | 87 +++++++++++++++---- .../resourcemanager/resource_manager_test.cpp | 41 +++++++++ 5 files changed, 168 insertions(+), 31 deletions(-) diff --git a/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h b/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h index 9ef23460..fe88c43d 100644 --- a/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h +++ b/services/distributedhardwarefwkservice/include/resourcemanager/capability_info_manager.h @@ -86,6 +86,7 @@ public: std::shared_ptr GetEventHandler(); void DumpCapabilityInfos(std::vector &capInfos); + std::string GetDhSubtype(const std::string &deviceId, const std::string &dhId); private: void HandleCapabilityAddChange(const std::vector &insertRecords); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index f27eebc7..37569a51 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -757,20 +757,32 @@ int32_t ComponentManager::GetDHIdByDHSubtype(const DHSubtype dhSubtype, std::str int32_t ComponentManager::GetDHSubtypeByDHId(DHSubtype &dhSubtype, const std::string &networkId, const std::string &dhId) { - if (LocalCapabilityInfoManager::GetInstance() == nullptr) { - DHLOGE("LocalCapabilityInfoManager instance is null."); - return ERR_DH_FWK_BAD_OPERATION; - } - std::string deviceId = DHContext::GetInstance().GetDeviceIdByNetworkId(networkId); - if (deviceId.empty()) { - DHLOGE("Get deviceId by networkId failed"); - return ERR_DH_FWK_BAD_OPERATION; - } - std::string dhSubtypeStr = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); - if (dhSubtypeStr.empty()) { - DHLOGE("Get dhSubtype by dhId failed"); - return ERR_DH_FWK_BAD_OPERATION; + std::string deviceId = ""; + std::string dhSubtypeStr = ""; + if (networkId == GetLocalNetworkId()) { + deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; + if (deviceId.empty()) { + DHLOGE("Get local deviceId failed."); + return ERR_DH_FWK_BAD_OPERATION; + } + dhSubtypeStr = CapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + if (dhSubtypeStr.empty()) { + DHLOGE("Get local dhSubtype failed."); + return ERR_DH_FWK_BAD_OPERATION; + } + } else { + deviceId = DHContext::GetInstance().GetDeviceIdByNetworkId(networkId); + if (deviceId.empty()) { + DHLOGE("Get remote deviceId failed."); + return ERR_DH_FWK_BAD_OPERATION; + } + dhSubtypeStr = LocalCapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + if (dhSubtypeStr.empty()) { + DHLOGE("Get remote dhSubtype failed."); + return ERR_DH_FWK_BAD_OPERATION; + } } + if (dhSubtypeStr == MIC) { dhSubtype = DHSubtype::AUDIO_MIC; } else if (dhSubtypeStr == CAMERA) { @@ -2072,6 +2084,7 @@ int32_t ComponentManager::InitCompSink(DHType dhType) DHLOGE("sinkPtr is null, compType = %{public}#X.", dhType); return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; } + sinkPtr->RegisterDistributedHardwareSinkStateListener(dhSinkStateListener_); compSink_.insert(std::make_pair(dhType, sinkPtr)); return DH_FWK_SUCCESS; } @@ -2079,7 +2092,18 @@ int32_t ComponentManager::InitCompSink(DHType dhType) int32_t ComponentManager::UninitCompSink(DHType dhType) { std::unique_lock lock(compSinkMutex_); - auto ret = ComponentLoader::GetInstance().ReleaseSink(dhType); + IDistributedHardwareSink *sinkPtr = nullptr; + auto ret = ComponentLoader::GetInstance().GetSink(dhType, sinkPtr); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); + return ret; + } + if (sinkPtr == nullptr) { + DHLOGE("sinkPtr is null, compType = %{public}#X.", dhType); + return ERR_DH_FWK_LOADER_HANDLER_IS_NULL; + } + sinkPtr->UnregisterDistributedHardwareSinkStateListener(); + ret = ComponentLoader::GetInstance().ReleaseSink(dhType); if (ret != DH_FWK_SUCCESS) { DHLOGE("GetSource failed, compType = %{public}#X, ret = %{public}d.", dhType, ret); return ret; diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index 5543fcaa..5463dabc 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -608,5 +608,23 @@ std::vector CapabilityInfoManager::GetEntriesByKeys(const } return dbAdapterPtr_->GetEntriesByKeys(keys); } + +std::string CapabilityInfoManager::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 ""; + } + if (globalCapInfoMap_[key] == nullptr) { + DHLOGE("Pointer is null"); + return ""; + } + return globalCapInfoMap_[key]->GetDHSubtype(); +} } // 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 a2023494..633a24c1 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 @@ -82,6 +82,8 @@ const std::shared_ptr CAP_INFO_TEST = const std::shared_ptr META_INFO_PTR_TEST = std::make_shared( DH_ID_TEST, DEV_ID_TEST, DEVICE_NAME, TEST_DEV_TYPE_PAD, DHType::CAMERA, DH_ATTR_1, DH_SUBTYPE_TEST, UDIDHASH_TEST, CompVersion{ .sinkVersion = TEST_SINK_VERSION_1 }); + +static std::string g_mocklocalNetworkId = "123456789"; } void ComponentManagerTest::SetUpTestCase(void) @@ -165,6 +167,11 @@ void SetDownComponentLoaderConfig() } } +std::string GetLocalNetworkId() +{ + return g_mocklocalNetworkId; +} + void ComponentManagerTest::TestGetDistributedHardwareCallback::OnSuccess( const std::string &networkId, const std::vector &descriptors, EnableStep enableStep) { @@ -1082,31 +1089,64 @@ HWTEST_F(ComponentManagerTest, UpdateBusinessState_003, TestSize.Level1) HWTEST_F(ComponentManagerTest, GetDHSubtypeByDHId_001, TestSize.Level1) { - std::string peeruuid = "123456789"; - std::string dhid = "audio_132"; - std::string deviceId = Sha256(peeruuid); + std::string dhid = "audio_1"; + std::string networkId = "networkId_123"; + DHSubtype dhSubtype; + auto ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, networkId, dhid); + EXPECT_EQ(ret, ERR_DH_FWK_BAD_OPERATION); + + std::string deviceId = Sha256(UUID_TEST); + DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, NETWORK_TEST); + ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, NETWORK_TEST, dhid); + EXPECT_EQ(ret, ERR_DH_FWK_BAD_OPERATION); + + std::shared_ptr capInfo = std::make_shared( + dhid, deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs", ""); + std::string key = deviceId + "###" + dhid; + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo; + ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, NETWORK_TEST, dhid); + EXPECT_EQ(ret, ERR_DH_FWK_BAD_OPERATION); + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); + DHContext::GetInstance().devIdEntrySet_.clear(); +} + +HWTEST_F(ComponentManagerTest, GetDHSubtypeByDHId_002, TestSize.Level1) +{ + std::string dhid = "audio_1"; DHSubtype dhSubtype; + std::string deviceId = Sha256(UUID_TEST); + DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, NETWORK_TEST); std::shared_ptr capInfo1 = std::make_shared( dhid, deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs", "mic"); std::string key = deviceId + "###" + dhid; LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo1; - auto ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, deviceId, dhid); - EXPECT_EQ(DHSubtype::AUDIO_MIC, dhSubtype); + auto ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, NETWORK_TEST, dhid); EXPECT_EQ(ret, DH_FWK_SUCCESS); - dhid = "camera_132"; + + dhid = "audio_2"; std::shared_ptr capInfo2 = std::make_shared( dhid, deviceId, "devName_test", DEV_TYPE_TEST, DHType::CAMERA, "attrs", "camera"); key = deviceId + "###" + dhid; LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo2; - ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, deviceId, dhid); - EXPECT_EQ(DHSubtype::CAMERA, dhSubtype); + ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, NETWORK_TEST, dhid); EXPECT_EQ(ret, DH_FWK_SUCCESS); - dhid = "unknown_132"; + + dhid = "audio_3"; std::shared_ptr capInfo3 = std::make_shared( dhid, deviceId, "devName_test", DEV_TYPE_TEST, DHType::UNKNOWN, "attrs", "unknown"); key = deviceId + "###" + dhid; LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo3; - ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, deviceId, dhid); + ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, NETWORK_TEST, dhid); + EXPECT_EQ(ret, ERR_DH_FWK_BAD_OPERATION); + LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); + DHContext::GetInstance().devIdEntrySet_.clear(); +} + +HWTEST_F(ComponentManagerTest, GetDHSubtypeByDHId_003, TestSize.Level1) +{ + std::string dhid = "audio_1"; + DHSubtype dhSubtype; + auto ret = ComponentManager::GetInstance().GetDHSubtypeByDHId(dhSubtype, g_mocklocalNetworkId, dhid); EXPECT_EQ(ret, ERR_DH_FWK_BAD_OPERATION); } @@ -1123,17 +1163,20 @@ HWTEST_F(ComponentManagerTest, DeinitAVSyncSharedMemory_001, TestSize.Level1) HWTEST_F(ComponentManagerTest, GetDHIdByDHSubtype_001, TestSize.Level1) { - std::string peeruuid = "123456789"; + std::string networkId = "networkId_123456"; std::string dhid = "audio_132"; - std::string deviceId = Sha256(peeruuid); + std::string deviceId = Sha256(UUID_TEST); std::shared_ptr capInfo1 = std::make_shared( dhid, deviceId, "devName_test", DEV_TYPE_TEST, DHType::AUDIO, "attrs", "mic"); std::string key = deviceId + "###" + dhid; BusinessState state = BusinessState::IDLE; LocalCapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo1; - ComponentManager::GetInstance().dhBizStates_.emplace(std::make_pair(deviceId, dhid), state); - auto ret = ComponentManager::GetInstance().GetDHIdByDHSubtype(DHSubtype::AUDIO_MIC, deviceId, dhid); + ComponentManager::GetInstance().dhBizStates_.emplace(std::make_pair(networkId, dhid), state); + DHContext::GetInstance().AddOnlineDevice(UDID_TEST, UUID_TEST, networkId); + auto ret = ComponentManager::GetInstance().GetDHIdByDHSubtype(DHSubtype::AUDIO_MIC, networkId, dhid); EXPECT_EQ(ret, DH_FWK_SUCCESS); + + deviceId = "deviceId_test"; ret = ComponentManager::GetInstance().GetDHIdByDHSubtype(DHSubtype::CAMERA, deviceId, dhid); EXPECT_EQ(ret, ERR_DH_FWK_BAD_OPERATION); } @@ -1315,11 +1358,14 @@ HWTEST_F(ComponentManagerTest, QueryBusinessState_001, TestSize.Level1) HWTEST_F(ComponentManagerTest, QueryBusinessState_002, TestSize.Level1) { - BusinessState ret = ComponentManager::GetInstance().QueryBusinessState(NETWORK_TEST, DH_ID_TEST); + std::string uuid = "uuid_123"; + std::string dhId = "camera_1"; + ComponentManager::GetInstance().dhBizStates_.emplace(std::make_pair(uuid, dhId), BusinessState::IDLE); + BusinessState ret = ComponentManager::GetInstance().QueryBusinessState(uuid, dhId); EXPECT_EQ(BusinessState::IDLE, ret); ComponentManager::GetInstance().dhBizStates_.clear(); - ret = ComponentManager::GetInstance().QueryBusinessState(NETWORK_TEST, DH_ID_TEST); + ret = ComponentManager::GetInstance().QueryBusinessState(uuid, dhId); EXPECT_EQ(BusinessState::UNKNOWN, ret); } @@ -1467,12 +1513,19 @@ HWTEST_F(ComponentManagerTest, UninitCompSource_001, TestSize.Level1) } HWTEST_F(ComponentManagerTest, UninitCompSink_001, TestSize.Level1) +{ + ComponentLoader::GetInstance().compHandlerMap_.clear(); + auto ret = ComponentManager::GetInstance().UninitCompSink(DHType::AUDIO); + EXPECT_EQ(ret, ERR_DH_FWK_LOADER_HANDLER_IS_NULL); +} + +HWTEST_F(ComponentManagerTest, UninitCompSink_002, TestSize.Level1) { ComponentLoader::GetInstance().compHandlerMap_.clear(); SetUpComponentLoaderConfig(); auto ret = ComponentManager::GetInstance().UninitCompSink(DHType::AUDIO); SetDownComponentLoaderConfig(); - EXPECT_EQ(ret, ERR_DH_FWK_LOADER_SINK_UNLOAD); + EXPECT_EQ(ret, DH_FWK_SUCCESS); } HWTEST_F(ComponentManagerTest, OnDataSyncTrigger_001, testing::ext::TestSize.Level1) 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 a8bd3471..85ce3c37 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/resource_manager_test.cpp @@ -925,5 +925,46 @@ HWTEST_F(ResourceManagerTest, GetDataByDHType_001, TestSize.Level1) EXPECT_EQ(DH_FWK_SUCCESS, ret); CapabilityInfoManager::GetInstance()->globalCapInfoMap_.clear(); } + +HWTEST_F(ResourceManagerTest, GetDhSubtype_001, TestSize.Level1) +{ + std::string deviceId = ""; + std::string dhId = ""; + auto ret = CapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + EXPECT_EQ("", ret); + + deviceId = "deviceId_1"; + ret = CapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + EXPECT_EQ("", ret); + + deviceId = ""; + dhId = "dhId_1"; + ret = CapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhId); + EXPECT_EQ("", ret); +} + +HWTEST_F(ResourceManagerTest, 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", TEST_DEV_TYPE_PAD, DHType::AUDIO, "attrs", "subtype"); + std::string key = deviceId + "###" + dhid; + CapabilityInfoManager::GetInstance()->globalCapInfoMap_[key] = capInfo; + auto ret = CapabilityInfoManager::GetInstance()->GetDhSubtype("deviceId_1", dhid); + EXPECT_EQ("", ret); + + ret = CapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId, dhid); + EXPECT_EQ("subtype", ret); + + std::shared_ptr capInfo1 = nullptr; + std::string deviceId1 = "deviceId_2"; + std::string key1 = deviceId1 + "###" + dhid; + CapabilityInfoManager::GetInstance()->globalCapInfoMap_[key1] = capInfo1; + ret = CapabilityInfoManager::GetInstance()->GetDhSubtype(deviceId1, dhid); + EXPECT_EQ("", ret); +} } // namespace DistributedHardware } // namespace OHOS -- Gitee