From fdb8a777c2d467b8d9edc91a7317be403ad62371 Mon Sep 17 00:00:00 2001 From: guoyi Date: Fri, 19 Jul 2024 17:41:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E5=AF=B9=E7=82=B9=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E4=B8=8A=E7=BA=BF=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- common/include/utils/profile_utils.h | 1 + common/src/utils/profile_utils.cpp | 9 ++ .../src/devicemanager/dp_device_manager.cpp | 2 +- .../device_profile_manager.h | 5 +- services/core/include/i_dp_sync_adapter.h | 9 +- .../kvadapter/ikv_adapter.h | 2 +- .../persistenceadapter/kvadapter/kv_adapter.h | 2 +- .../device_profile_manager.cpp | 92 ++++++++++++------- 8 files changed, 80 insertions(+), 42 deletions(-) diff --git a/common/include/utils/profile_utils.h b/common/include/utils/profile_utils.h index 787e147c..d37e0813 100644 --- a/common/include/utils/profile_utils.h +++ b/common/include/utils/profile_utils.h @@ -42,6 +42,7 @@ public: static bool FilterAndGroupOnlineDevices(const std::vector& deviceList, std::vector& ohBasedDevices, std::vector& 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); diff --git a/common/src/utils/profile_utils.cpp b/common/src/utils/profile_utils.cpp index 400eea97..3d1fa3a3 100644 --- a/common/src/utils/profile_utils.cpp +++ b/common/src/utils/profile_utils.cpp @@ -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(DistributedHardware::DmAuthForm::INVALID_TYPE) || + authForm == static_cast(DistributedHardware::DmAuthForm::IDENTICAL_ACCOUNT)) { + return false; + } + return true; +} + ProfileType ProfileUtils::GetProfileType(const std::string& key) { if (key.length() == 0 || key.length() > MAX_STRING_LEN) { diff --git a/old/services/core/src/devicemanager/dp_device_manager.cpp b/old/services/core/src/devicemanager/dp_device_manager.cpp index 445b2ac3..f7f57bad 100644 --- a/old/services/core/src/devicemanager/dp_device_manager.cpp +++ b/old/services/core/src/devicemanager/dp_device_manager.cpp @@ -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) diff --git a/services/core/include/deviceprofilemanager/device_profile_manager.h b/services/core/include/deviceprofilemanager/device_profile_manager.h index 1e51a11b..8fe7726e 100644 --- a/services/core/include/deviceprofilemanager/device_profile_manager.h +++ b/services/core/include/deviceprofilemanager/device_profile_manager.h @@ -68,7 +68,7 @@ public: int32_t SavePutTempCache(std::map& 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& notOHBasedDevices, sptr syncCompletedCallback); void AddToPutTempCache(const std::map& 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_; diff --git a/services/core/include/i_dp_sync_adapter.h b/services/core/include/i_dp_sync_adapter.h index 8b626ea9..2d95d971 100644 --- a/services/core/include/i_dp_sync_adapter.h +++ b/services/core/include/i_dp_sync_adapter.h @@ -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& deviceIdList, const sptr 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 syncCb, bool isP2p) = 0; }; using CreateDPSyncAdapterFuncPtr = IDPSyncAdapter *(*)(void); } // namespace DeviceProfile diff --git a/services/core/include/persistenceadapter/kvadapter/ikv_adapter.h b/services/core/include/persistenceadapter/kvadapter/ikv_adapter.h index 384d71b3..b95daa41 100644 --- a/services/core/include/persistenceadapter/kvadapter/ikv_adapter.h +++ b/services/core/include/persistenceadapter/kvadapter/ikv_adapter.h @@ -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& values) = 0; virtual int32_t Sync(const std::vector& deviceList, SyncMode syncMode) = 0; - virtual int32_t GetDeviceEntries(const std::string& uuid, std::map& values) = 0; + virtual int32_t GetDeviceEntries(const std::string& udid, std::map& values) = 0; virtual int32_t DeleteBatch(const std::vector& keys) = 0; }; } // namespace DeviceProfile diff --git a/services/core/include/persistenceadapter/kvadapter/kv_adapter.h b/services/core/include/persistenceadapter/kvadapter/kv_adapter.h index baf59f64..845b1f66 100644 --- a/services/core/include/persistenceadapter/kvadapter/kv_adapter.h +++ b/services/core/include/persistenceadapter/kvadapter/kv_adapter.h @@ -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& values) override; int32_t Sync(const std::vector& deviceList, SyncMode syncMode) override; - int32_t GetDeviceEntries(const std::string& uuid, std::map& values) override; + int32_t GetDeviceEntries(const std::string& udid, std::map& values) override; int32_t DeleteBatch(const std::vector& keys) override; int32_t DeleteKvStore(); diff --git a/services/core/src/deviceprofilemanager/device_profile_manager.cpp b/services/core/src/deviceprofilemanager/device_profile_manager.cpp index dacb7882..29a4e8f0 100644 --- a/services/core/src/deviceprofilemanager/device_profile_manager.cpp +++ b/services/core/src/deviceprofilemanager/device_profile_manager.cpp @@ -637,66 +637,94 @@ 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; } + if (!ProfileUtils::IsOHBasedDevice(deviceInfo.extraData)) { + HILOGE("device is not ohbase. networkId=%{public}s", ProfileUtils::GetAnonyString(remoteNetworkId).c_str()); + return; + } auto task = [this, deviceInfo]() { - if (!ProfileUtils::IsOHBasedDevice(deviceInfo.extraData)) { - HILOGE("device is not ohbase. networkId=%{public}s", - ProfileUtils::GetAnonyString(deviceInfo.networkId).c_str()); - return; - } 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()); + std::map values; + std::lock_guard lock(dynamicStoreMutex_); + if (deviceProfileStore_ == nullptr) { + HILOGE("dynamicProfileStore is nullptr!"); + return; + } + if (deviceProfileStore_->GetDeviceEntries(localUdid, values) != DP_SUCCESS) { + HILOGE("GetDeviceEntries failed, localUdid=%{public}s", ProfileUtils::GetAnonyString(localUdid).c_str()); + return; + } + std::vector delKeys; + for (const auto& [key, _] : values) { + if (key.find(remoteUdid) != std::string::npos) { + delKeys.emplace_back(key); + } + } + if (delKeys.empty()) { + return; + } + if (deviceProfileStore_->DeleteBatch(delKeys) != DP_SUCCESS) { + HILOGE("DeleteBatch failed, remoteUdid=%{public}s", ProfileUtils::GetAnonyString(remoteUdid).c_str()); return; } - FixRemoteData(remoteUdid, localUuid); }; std::thread(task).detach(); } -void DeviceProfileManager::FixRemoteData(const std::string& remoteUdid, const std::string& localUuid) +void DeviceProfileManager::NotifyNotOHBaseP2pOnline(const DistributedHardware::DmDeviceInfo deviceInfo) { - std::map values; - std::lock_guard lock(dynamicStoreMutex_); - if (deviceProfileStore_ == nullptr) { - HILOGE("dynamicProfileStore is nullptr!"); + 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 (deviceProfileStore_->GetDeviceEntries(localUuid, values) != DP_SUCCESS) { - HILOGE("GetDeviceEntries failed, localUuid=%{public}s", ProfileUtils::GetAnonyString(localUuid).c_str()); + if (ProfileUtils::IsOHBasedDevice(deviceInfo.extraData)) { + HILOGI("device is ohbase. remoteNetworkId=%{public}s", ProfileUtils::GetAnonyString(remoteNetworkId).c_str()); return; } - std::vector delKeys; - for (const auto& [key, _] : values) { - if (key.find(remoteUdid) != std::string::npos) { - delKeys.emplace_back(key); + if (!ProfileUtils::IsP2p(static_cast(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; } - } - if (delKeys.empty()) { - return; - } - if (deviceProfileStore_->DeleteBatch(delKeys) != DP_SUCCESS) { - HILOGE("DeleteBatch failed, remoteUdid=%{public}s", ProfileUtils::GetAnonyString(remoteUdid).c_str()); - return; - } + std::lock_guard lock(isAdapterLoadLock_); + if (dpSyncAdapter_ == nullptr) { + HILOGE("dpSyncAdapter is nullptr."); + return; + } + dpSyncAdapter_->NotOHBaseDeviceOnline(remoteUdid, remoteNetworkId, true); + }; + std::thread(task).detach(); } } // namespace DeviceProfile } // namespace OHOS \ No newline at end of file