!823 networkId check before start remote

Merge pull request !823 from 仝月姣/master
This commit is contained in:
openharmony_ci 2024-03-27 12:29:13 +00:00 committed by Gitee
commit 9eafab15f5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 41 additions and 59 deletions

View File

@ -18,6 +18,7 @@
#include <map>
#include <set>
#include <string>
#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<DistributedHardware::DmDeviceInfo>& dmDeviceInfoList);
bool UpdateDeviceInfoStorage();
private:
bool InitNetworkIdManager(std::shared_ptr<DnetworkAdapter> dnetworkAdapter);
bool ConnectSoftbus();
std::shared_ptr<DmsDeviceInfo> FindDeviceInfoInStorage(const std::string& networkId);
void ClearAllDevices();
bool WaitForDnetworkReady();
bool GetLocalDeviceFromDnet(std::string& networkId);

View File

@ -156,15 +156,8 @@ bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr<DeviceListen
bool DnetworkAdapter::UpdateDeviceInfoStorage()
{
std::vector<DistributedHardware::DmDeviceInfo> 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<DeviceListener>& listener)

View File

@ -175,20 +175,27 @@ void DtbschedmgrDeviceInfoStorage::GetDeviceIdSet(std::set<std::string>& deviceI
}
}
void DtbschedmgrDeviceInfoStorage::UpdateDeviceInfoStorage(
const std::vector<DmDeviceInfo>& dmDeviceInfoList)
bool DtbschedmgrDeviceInfoStorage::UpdateDeviceInfoStorage()
{
std::vector<DistributedHardware::DmDeviceInfo> 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<DmsDeviceInfo>(
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<mutex> 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<DmsDeviceInfo> DtbschedmgrDeviceInfoStorage::GetDeviceInfoById(const string& networkId)
std::shared_ptr<DmsDeviceInfo> DtbschedmgrDeviceInfoStorage::FindDeviceInfoInStorage(const std::string& networkId)
{
HILOGI("GetDeviceInfoById start, networkId = %{public}s.", DnetworkAdapter::AnonymizeNetworkId(networkId).c_str());
lock_guard<mutex> autoLock(deviceLock_);
auto iter = remoteDevices_.find(networkId);
if (iter == remoteDevices_.end()) {
HILOGE("DeviceInfo not in cache, obtained from DM.");
std::vector<DistributedHardware::DmDeviceInfo> 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<std::string, std::shared_ptr<DmsDeviceInfo>> remoteDevices;
for (const auto& dmDeviceInfo : dmDeviceInfoList) {
auto deviceInfo = std::make_shared<DmsDeviceInfo>(
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<DmsDeviceInfo> 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()) {

View File

@ -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<DistributedHardware::DmDeviceInfo> 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<DistributedHardware::DmDeviceInfo> 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