From 830a9ebe98045e5cbda17783a40dd3b5e447cc26 Mon Sep 17 00:00:00 2001 From: tangfan <1824525929@qq.com> Date: Tue, 22 Mar 2022 17:39:57 +0800 Subject: [PATCH] fix bug Signed-off-by: tangfan <1824525929@qq.com> --- .../src/accessmanager/access_manager.cpp | 6 +++--- .../distributed_hardware_manager_factory.cpp | 5 ----- .../include/utils/dh_context.h | 1 + .../src/distributed_hardware_manager.cpp | 20 ++++++++++++------- .../src/utils/dh_context.cpp | 13 ++++++++++++ utils/include/dh_utils_tool.h | 2 +- utils/src/dh_utils_tool.cpp | 4 ++-- 7 files changed, 33 insertions(+), 18 deletions(-) diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index 26a2e012..a1c54ab6 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -117,7 +117,7 @@ void AccessManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo) deviceInfo.deviceName, deviceInfo.deviceTypeId); auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId - auto uuid = GetUUIDByNetworkId(networkId); + auto uuid = GetUUIDBySoftBus(networkId); auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId); DHLOGI("online result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(), @@ -131,7 +131,7 @@ void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo) deviceInfo.deviceName, deviceInfo.deviceTypeId); auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId - auto uuid = GetUUIDByNetworkId(networkId); + auto uuid = GetUUIDBySoftBus(networkId); auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(networkId, uuid, deviceInfo.deviceTypeId); DHLOGI("offline result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(), @@ -156,7 +156,7 @@ void AccessManager::SendTrustedDeviceOnline() DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList); for (const auto &deviceInfo : deviceList) { const auto networkId = std::string(deviceInfo.deviceId); - const auto uuid = GetUUIDByNetworkId(networkId); + const auto uuid = GetUUIDBySoftBus(networkId); DHLOGI("Send trusted device online, networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str()); DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp index 0a4dda09..42bf836a 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -82,11 +82,6 @@ int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &ne return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; } - if (uuid.empty()) { - DHLOGE("uuid is empty"); - return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY; - } - std::lock_guard lock(mutex_); if (distributedHardwareMgrPtr_ == nullptr && !Init()) { DHLOGE("distributedHardwareMgr is null"); diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/dh_context.h b/services/distributedhardwarefwkserviceimpl/include/utils/dh_context.h index 42b1c825..1a4fe238 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/dh_context.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/dh_context.h @@ -41,6 +41,7 @@ public: bool IsDeviceOnline(const std::string &uuid); size_t GetOnlineCount(); std::string GetNetworkIdByUUID(const std::string &uuid); + std::string GetUUIDByNetworkId(const std::string &networkId); /* DeviceId is which is hashed by sha256 */ std::string GetUUIDByDeviceId(const std::string &deviceId); diff --git a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp index cce91398..8ad46ced 100644 --- a/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/distributed_hardware_manager.cpp @@ -119,23 +119,29 @@ int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkI DHLOGE("networkId is empty"); return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY; } - if (uuid.empty()) { + + // when other device restart, the device receives online and offline messages in sequence + // So, make the cache device handle offline event when other device restart + std::string cacheUUID = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + if (uuid.empty() && cacheUUID.empty()) { DHLOGE("uuid is empty, networkId = %s", GetAnonyString(networkId).c_str()); return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY; } - DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str()); + std::string realUUID = uuid.empty() ? cacheUUID : uuid; + + DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(realUUID).c_str()); - if (!DHContext::GetInstance().IsDeviceOnline(uuid)) { - DHLOGW("device is already offline, uuid = %s", GetAnonyString(uuid).c_str()); + if (!DHContext::GetInstance().IsDeviceOnline(realUUID)) { + DHLOGW("device is already offline, uuid = %s", GetAnonyString(realUUID).c_str()); return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE; } - auto task = TaskFactory::GetInstance().CreateTask(TaskType::OFF_LINE, networkId, uuid, "", nullptr); + auto task = TaskFactory::GetInstance().CreateTask(TaskType::OFF_LINE, networkId, realUUID, "", nullptr); TaskExecutor::GetInstance().PushTask(task); - DHContext::GetInstance().RemoveOnlineDevice(uuid); - CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(uuid)); + DHContext::GetInstance().RemoveOnlineDevice(realUUID); + CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID)); return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkserviceimpl/src/utils/dh_context.cpp b/services/distributedhardwarefwkserviceimpl/src/utils/dh_context.cpp index 06e71cd1..ea997a6e 100644 --- a/services/distributedhardwarefwkserviceimpl/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/utils/dh_context.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include + #include "dh_context.h" #include "dh_utils_tool.h" @@ -88,6 +90,17 @@ std::string DHContext::GetNetworkIdByUUID(const std::string &uuid) return onlineDeviceMap_[uuid]; } +std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) { + std::unique_lock lock(onlineDevMutex_); + auto iter = std::find_if(onlineDeviceMap_.begin(), onlineDeviceMap_.end(), + [networkId](const auto &item) {return networkId.compare(item.second) == 0; }); + if (iter == onlineDeviceMap_.end()) { + DHLOGE("Can not find uuid, networkId: %s", GetAnonyString(networkId).c_str()); + return ""; + } + return iter->first; +} + std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId) { std::unique_lock lock(onlineDevMutex_); diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 0fdf5b9e..000c845c 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -33,7 +33,7 @@ int64_t GetCurrentTime(); */ std::string GetRandomID(); -std::string GetUUIDByNetworkId(const std::string &networkId); +std::string GetUUIDBySoftBus(const std::string &networkId); DeviceInfo GetLocalDeviceInfo(); diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index ab5c3aec..86827dd6 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -67,7 +67,7 @@ std::string GetRandomID() return ss.str(); } -std::string GetUUIDByNetworkId(const std::string &networkId) +std::string GetUUIDBySoftBus(const std::string &networkId) { if (networkId.empty()) { return ""; @@ -109,7 +109,7 @@ DeviceInfo GetLocalDeviceInfo() DHLOGE("GetLocalNodeDeviceInfo failed, errCode = %d", ret); return devInfo; } - devInfo.uuid = GetUUIDByNetworkId(info->networkId); + devInfo.uuid = GetUUIDBySoftBus(info->networkId); devInfo.deviceId = GetDeviceIdByUUID(devInfo.uuid); devInfo.deviceName = info->deviceName; devInfo.deviceType = info->deviceTypeId; -- Gitee