From 1475f5a6650cd581cbd1792e290fe2dfc86a041d Mon Sep 17 00:00:00 2001 From: t00605578 Date: Tue, 26 Mar 2024 16:43:05 +0800 Subject: [PATCH] networkId check before start remote Signed-off-by: t00605578 --- .../include/dtbschedmgr_device_info_storage.h | 6 +- .../src/adapter/dnetwork_adapter.cpp | 11 +--- .../src/dtbschedmgr_device_info_storage.cpp | 62 +++++++++++-------- .../dtbschedmgr_device_info_storage_test.cpp | 21 +------ 4 files changed, 41 insertions(+), 59 deletions(-) diff --git a/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h b/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h index fdba9718..86ca25b0 100644 --- a/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h +++ b/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h @@ -18,6 +18,7 @@ #include #include +#include #include "adapter/dnetwork_adapter.h" #include "deviceManager/dms_device_info.h" @@ -86,14 +87,13 @@ public: /** * UpdateDeviceInfoStorage update device Info cache - * - * @param dmDeviceInfoList */ - void UpdateDeviceInfoStorage(const std::vector& dmDeviceInfoList); + bool UpdateDeviceInfoStorage(); private: bool InitNetworkIdManager(std::shared_ptr dnetworkAdapter); bool ConnectSoftbus(); + std::shared_ptr FindDeviceInfoInStorage(const std::string& networkId); void ClearAllDevices(); bool WaitForDnetworkReady(); bool GetLocalDeviceFromDnet(std::string& networkId); diff --git a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp b/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp index 9230e89d..2495bb02 100644 --- a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp +++ b/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp @@ -156,15 +156,8 @@ bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr dmDeviceInfoList; - int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmDeviceInfoList); - if (errCode != ERR_OK) { - HILOGE("GetTrustedDeviceList failed, errCode = %{public}d", errCode); - return false; - } - DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage(dmDeviceInfoList); - HILOGI("UpdateDeviceInfoStorage success"); - return true; + HILOGI("UpdateDeviceInfoStorage start."); + return DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage(); } void DnetworkAdapter::RemoveDeviceChangeListener(const std::shared_ptr& listener) diff --git a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp b/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp index e09c42a7..e8384271 100644 --- a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp +++ b/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp @@ -175,20 +175,27 @@ void DtbschedmgrDeviceInfoStorage::GetDeviceIdSet(std::set& deviceI } } -void DtbschedmgrDeviceInfoStorage::UpdateDeviceInfoStorage( - const std::vector& dmDeviceInfoList) +bool DtbschedmgrDeviceInfoStorage::UpdateDeviceInfoStorage() { + std::vector dmDeviceInfoList; + int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmDeviceInfoList); + if (errCode != ERR_OK) { + HILOGE("Get device manager trusted device list fail, errCode %{public}d", errCode); + return false; + } for (const auto& dmDeviceInfo : dmDeviceInfoList) { auto deviceInfo = std::make_shared( dmDeviceInfo.deviceName, dmDeviceInfo.deviceTypeId, dmDeviceInfo.networkId); std::string networkId = deviceInfo->GetNetworkId(); RegisterUuidNetworkIdMap(networkId); { - HILOGI("remoteDevices networkId = %{public}s", DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); + HILOGI("Add remote device networkId %{public}s", DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); lock_guard autoLock(deviceLock_); remoteDevices_[networkId] = deviceInfo; } } + HILOGI("Update remote devices info storage success."); + return true; } bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceId(std::string& networkId) @@ -242,38 +249,39 @@ void DtbschedmgrDeviceInfoStorage::ClearAllDevices() remoteDevices_.clear(); } -std::shared_ptr DtbschedmgrDeviceInfoStorage::GetDeviceInfoById(const string& networkId) +std::shared_ptr DtbschedmgrDeviceInfoStorage::FindDeviceInfoInStorage(const std::string& networkId) { - HILOGI("GetDeviceInfoById start, networkId = %{public}s.", DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); lock_guard autoLock(deviceLock_); auto iter = remoteDevices_.find(networkId); if (iter == remoteDevices_.end()) { - HILOGE("DeviceInfo not in cache, obtained from DM."); - std::vector dmDeviceInfoList; - int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmDeviceInfoList); - if (errCode != ERR_OK) { - HILOGD("GetTrustedDeviceList failed, errCode = %{public}d.", errCode); - return nullptr; - } - std::map> remoteDevices; - for (const auto& dmDeviceInfo : dmDeviceInfoList) { - auto deviceInfo = std::make_shared( - dmDeviceInfo.deviceName, dmDeviceInfo.deviceTypeId, dmDeviceInfo.networkId); - RegisterUuidNetworkIdMap(networkId); - remoteDevices[networkId] = deviceInfo; - } - auto it = remoteDevices.find(networkId); - if (it == remoteDevices.end()) { - HILOGD("DeviceInfo not in DM."); - return nullptr; - } - HILOGD("Get deviceInfo from DM success."); - return it->second; + HILOGE("Get remote device info from storage fail, networkId %{public}s.", + DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); + return nullptr; } - HILOGI("Get deviceInfo from DMS local cache success."); + HILOGI("Get remote device info from storage success, networkId %{public}s.", + DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); return iter->second; } +std::shared_ptr DtbschedmgrDeviceInfoStorage::GetDeviceInfoById(const std::string& networkId) +{ + HILOGI("Get device info by networkId %{public}s start.", DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); + auto devInfo = FindDeviceInfoInStorage(networkId); + if (devInfo != nullptr) { + return devInfo; + } + + HILOGI("NetworkId %{public}s not in storage, update devices info from device manager.", + DnetworkAdapter::AnonymizeNetworkId(networkId).c_str()); + if (!UpdateDeviceInfoStorage()) { + HILOGE("Update device info storage from device manager trusted device list fail."); + return nullptr; + } + + devInfo = FindDeviceInfoInStorage(networkId); + return devInfo; +} + std::string DtbschedmgrDeviceInfoStorage::GetUuidByNetworkId(const std::string& networkId) { if (networkId.empty()) { diff --git a/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp b/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp index bf7a03e0..e25dede7 100644 --- a/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp +++ b/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp @@ -368,29 +368,10 @@ HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetNetworkIdByUuidTest_001, TestSize. HWTEST_F(DtbschedmgrDeviceInfoStorageTest, UpdateDeviceInfoStorageTest_001, TestSize.Level3) { DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest UpdateDeviceInfoStorageTest_001 start" << std::endl; - std::vector dmDeviceInfoList; - dmDeviceInfoList.clear(); - DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage(dmDeviceInfoList); - EXPECT_EQ(dmDeviceInfoList.empty(), true); + EXPECT_EQ(DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage(), true); DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest UpdateDeviceInfoStorageTest_001 end" << std::endl; } -/** - * @tc.name: UpdateDeviceInfoStorageTest_002 - * @tc.desc: test UpdateDeviceInfoStorage - * @tc.type: FUNC - */ -HWTEST_F(DtbschedmgrDeviceInfoStorageTest, UpdateDeviceInfoStorageTest_002, TestSize.Level3) -{ - DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest UpdateDeviceInfoStorageTest_002 start" << std::endl; - std::vector dmDeviceInfoList; - DistributedHardware::DmDeviceInfo dmDeviceInfo; - dmDeviceInfoList.emplace_back(dmDeviceInfo); - DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage(dmDeviceInfoList); - EXPECT_EQ(!dmDeviceInfoList.empty(), true); - DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest UpdateDeviceInfoStorageTest_002 end" << std::endl; -} - /** * @tc.name: GetLocalDeviceUdidTest_001 * @tc.desc: test GetLocalDeviceUdid