diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index 26a2e01209f1fd62b7cff7a926bb1a72563dae93..a1c54ab665240546e13dfb9c801ca63ac0650202 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 0a4dda09cc318de297f170377fb7272522521c62..42bf836aa561ceac449d9b2c0fb79517a1b9769b 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 42b1c825f54618cac51218bd0caedda21c8eab9d..1a4fe238dfb4e94c43f4c66890d5ee4df8527d5b 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 cce91398367c86ebbc743a28d43281dd056536ce..8ad46ced928ff3397e56a1cde85f2b974f6d23ed 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 06e71cd186513ae9f376fea6e460f50c34af7808..ea997a6ea70f5019a4fc95771843addab3a76786 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 0fdf5b9ebbe89ca81ff9f7612261a0184b280587..000c845cc10da5a89d3ca924a860489c2c416803 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 ab5c3aecadc3fa9be83821321ae98f8820922b9e..86827dd64bb3e4480f5c0dfbd5cd4330fcf367d5 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;