点对点设备上线优化

Signed-off-by: guoyi <guoyi39@huawei.com>
This commit is contained in:
guoyi 2024-07-19 17:41:46 +08:00
parent dfb5cb0edd
commit fdb8a777c2
8 changed files with 80 additions and 42 deletions

View File

@ -42,6 +42,7 @@ public:
static bool FilterAndGroupOnlineDevices(const std::vector<std::string>& deviceList,
std::vector<std::string>& ohBasedDevices, std::vector<std::string>& notOHBasedDevices);
static bool IsOHBasedDevice(const std::string& extraData);
static bool IsP2p(const int32_t authForm);
static ProfileType GetProfileType(const std::string& key);
static bool StartsWith(const std::string& str, const std::string prefix);
static bool IsKeyValid(const std::string& key);

View File

@ -149,6 +149,15 @@ bool ProfileUtils::IsOHBasedDevice(const std::string& extraData)
return osType == OHOS_TYPE;
}
bool ProfileUtils::IsP2p(const int32_t authForm)
{
if (authForm == static_cast<int32_t>(DistributedHardware::DmAuthForm::INVALID_TYPE) ||
authForm == static_cast<int32_t>(DistributedHardware::DmAuthForm::IDENTICAL_ACCOUNT)) {
return false;
}
return true;
}
ProfileType ProfileUtils::GetProfileType(const std::string& key)
{
if (key.length() == 0 || key.length() > MAX_STRING_LEN) {

View File

@ -100,7 +100,7 @@ void DpDeviceManager::DpDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo &
DistributedDeviceProfileService::GetInstance().DeviceOnline();
std::string networkId = deviceInfo.networkId;
DistributedDeviceProfile::ProfileCache::GetInstance().OnNodeOnline(networkId);
DistributedDeviceProfile::DeviceProfileManager::GetInstance().FixDataOnDeviceOnline(deviceInfo);
DistributedDeviceProfile::DeviceProfileManager::GetInstance().OnDeviceOnline(deviceInfo);
}
void DpDeviceManager::DpDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo)

View File

@ -68,7 +68,7 @@ public:
int32_t SavePutTempCache(std::map<std::string, std::string>& entries);
bool IsFirstInitDB();
void ResetFirst();
void FixDataOnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo);
void OnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo);
private:
bool LoadDpSyncAdapter();
@ -79,7 +79,8 @@ private:
void SyncWithNotOHBasedDeviceFailed(const std::vector<std::string>& notOHBasedDevices,
sptr<IRemoteObject> syncCompletedCallback);
void AddToPutTempCache(const std::map<std::string, std::string>& values);
void FixRemoteData(const std::string& remoteUdid, const std::string& localUuid);
void FixDataOnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo);
void NotifyNotOHBaseP2pOnline(const DistributedHardware::DmDeviceInfo deviceInfo);
bool isAdapterSoLoaded_ = false;
std::mutex isAdapterLoadLock_;
std::mutex dynamicStoreMutex_;

View File

@ -20,11 +20,6 @@
namespace OHOS {
namespace DistributedDeviceProfile {
class IDPSyncListener {
public:
virtual ~IDPSyncListener() = default;
virtual void SyncCompleted(const SyncResult& syncResults) = 0;
};
class IDPSyncAdapter {
public:
virtual ~IDPSyncAdapter() = default;
@ -33,6 +28,10 @@ public:
virtual int32_t DetectRemoteDPVersion(const std::string& peerDevId) = 0;
virtual int32_t SyncProfile(const std::list<std::string>& deviceIdList,
const sptr<IRemoteObject> syncCompletedCallback) = 0;
virtual int32_t NotOHBaseDeviceOnline(const std::string& peerUdid, const std::string& peerNetworkId,
bool isP2p) = 0;
virtual int32_t SyncProfile(const std::string& peerUdid, const std::string& peerNetworkId,
const sptr<IRemoteObject> syncCb, bool isP2p) = 0;
};
using CreateDPSyncAdapterFuncPtr = IDPSyncAdapter *(*)(void);
} // namespace DeviceProfile

View File

@ -36,7 +36,7 @@ public:
virtual int32_t Get(const std::string& key, std::string& value) = 0;
virtual int32_t GetByPrefix(const std::string& keyPrefix, std::map<std::string, std::string>& values) = 0;
virtual int32_t Sync(const std::vector<std::string>& deviceList, SyncMode syncMode) = 0;
virtual int32_t GetDeviceEntries(const std::string& uuid, std::map<std::string, std::string>& values) = 0;
virtual int32_t GetDeviceEntries(const std::string& udid, std::map<std::string, std::string>& values) = 0;
virtual int32_t DeleteBatch(const std::vector<std::string>& keys) = 0;
};
} // namespace DeviceProfile

View File

@ -50,7 +50,7 @@ public:
int32_t Get(const std::string& key, std::string& value) override;
int32_t GetByPrefix(const std::string& keyPrefix, std::map<std::string, std::string>& values) override;
int32_t Sync(const std::vector<std::string>& deviceList, SyncMode syncMode) override;
int32_t GetDeviceEntries(const std::string& uuid, std::map<std::string, std::string>& values) override;
int32_t GetDeviceEntries(const std::string& udid, std::map<std::string, std::string>& values) override;
int32_t DeleteBatch(const std::vector<std::string>& keys) override;
int32_t DeleteKvStore();

View File

@ -637,51 +637,43 @@ void DeviceProfileManager::ResetFirst()
isFirst_.store(false);
}
void DeviceProfileManager::OnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo)
{
FixDataOnDeviceOnline(deviceInfo);
NotifyNotOHBaseP2pOnline(deviceInfo);
}
void DeviceProfileManager::FixDataOnDeviceOnline(const DistributedHardware::DmDeviceInfo deviceInfo)
{
std::string remoteNetworkId = deviceInfo.networkId;
HILOGI("networkId:%{public}s", ProfileUtils::GetAnonyString(remoteNetworkId).c_str());
if (remoteNetworkId.empty() || deviceInfo.extraData.empty()) {
HILOGE("networkId or extraData is empty!");
return;
}
auto task = [this, deviceInfo]() {
if (!ProfileUtils::IsOHBasedDevice(deviceInfo.extraData)) {
HILOGE("device is not ohbase. networkId=%{public}s",
ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str());
HILOGE("device is not ohbase. networkId=%{public}s", ProfileUtils::GetAnonyString(remoteNetworkId).c_str());
return;
}
auto task = [this, deviceInfo]() {
std::string remoteUdid;
if (!ProfileUtils::GetUdidByNetworkId(deviceInfo.networkId, remoteUdid) || remoteUdid.empty()) {
HILOGE("Get remote deviceId failed. networkId=%{public}s",
ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str());
return;
}
std::string localNetworkId = ProfileCache::GetInstance().GetLocalNetworkId();
if (localNetworkId.empty()) {
HILOGE("Get local networkId failed");
std::string localUdid = ProfileCache::GetInstance().GetLocalUdid();
if (localUdid.empty()) {
HILOGE("Get local deviceId failed.");
return;
}
std::string localUuid;
if (!ProfileUtils::GetUuidByNetworkId(localNetworkId, localUuid) || localUuid.empty()) {
HILOGE("Get local udid failed. networkId=%{public}s", ProfileUtils::GetAnonyString(localNetworkId).c_str());
return;
}
FixRemoteData(remoteUdid, localUuid);
};
std::thread(task).detach();
}
void DeviceProfileManager::FixRemoteData(const std::string& remoteUdid, const std::string& localUuid)
{
std::map<std::string, std::string> values;
std::lock_guard<std::mutex> lock(dynamicStoreMutex_);
if (deviceProfileStore_ == nullptr) {
HILOGE("dynamicProfileStore is nullptr!");
return;
}
if (deviceProfileStore_->GetDeviceEntries(localUuid, values) != DP_SUCCESS) {
HILOGE("GetDeviceEntries failed, localUuid=%{public}s", ProfileUtils::GetAnonyString(localUuid).c_str());
if (deviceProfileStore_->GetDeviceEntries(localUdid, values) != DP_SUCCESS) {
HILOGE("GetDeviceEntries failed, localUdid=%{public}s", ProfileUtils::GetAnonyString(localUdid).c_str());
return;
}
std::vector<std::string> delKeys;
@ -697,6 +689,42 @@ void DeviceProfileManager::FixRemoteData(const std::string& remoteUdid, const st
HILOGE("DeleteBatch failed, remoteUdid=%{public}s", ProfileUtils::GetAnonyString(remoteUdid).c_str());
return;
}
};
std::thread(task).detach();
}
void DeviceProfileManager::NotifyNotOHBaseP2pOnline(const DistributedHardware::DmDeviceInfo deviceInfo)
{
std::string remoteNetworkId = deviceInfo.networkId;
HILOGI("networkId:%{public}s", ProfileUtils::GetAnonyString(remoteNetworkId).c_str());
if (remoteNetworkId.empty()) {
HILOGE("networkId or extraData is empty!");
return;
}
if (ProfileUtils::IsOHBasedDevice(deviceInfo.extraData)) {
HILOGI("device is ohbase. remoteNetworkId=%{public}s", ProfileUtils::GetAnonyString(remoteNetworkId).c_str());
return;
}
if (!ProfileUtils::IsP2p(static_cast<int32_t>(deviceInfo.authForm))) {
HILOGI("is not point 2 point. remoteNetworkId=%{public}s",
ProfileUtils::GetAnonyString(remoteNetworkId).c_str());
return;
}
auto task = [this, remoteNetworkId]() {
std::string remoteUdid;
if (!ProfileUtils::GetUdidByNetworkId(remoteNetworkId, remoteUdid) || remoteUdid.empty()) {
HILOGE("Get remote deviceId failed. remoteNetworkId=%{public}s",
ProfileUtils::GetAnonyString(remoteNetworkId).c_str());
return;
}
std::lock_guard<std::mutex> lock(isAdapterLoadLock_);
if (dpSyncAdapter_ == nullptr) {
HILOGE("dpSyncAdapter is nullptr.");
return;
}
dpSyncAdapter_->NotOHBaseDeviceOnline(remoteUdid, remoteNetworkId, true);
};
std::thread(task).detach();
}
} // namespace DeviceProfile
} // namespace OHOS