From ec03032b0c4aa1f7d2bc3a9de7a53626b9f4de81 Mon Sep 17 00:00:00 2001 From: lsq Date: Sat, 23 Jul 2022 20:47:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lsq Change-Id: Ia945d60fab37359d746d87571b4ffca2beeace7b Signed-off-by: lsq --- .../soft_bus_device_connection_listener.h | 4 ++ .../soft_bus_device_connection_listener.cpp | 71 +++++++++++++------ 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h b/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h index 7d0086efb..c2e13cb18 100644 --- a/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h +++ b/services/tokensyncmanager/include/remote/soft_bus_device_connection_listener.h @@ -18,6 +18,7 @@ #include #include +#include #include #include "accesstoken_log.h" @@ -64,6 +65,9 @@ public: * @param deviceInfo node info */ void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; + +private: + std::mutex exitMutex_; }; } // namespace AccessToken } // namespace Security diff --git a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp index 1f3e1ea26..f6b97a2c0 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_device_connection_listener.cpp @@ -14,6 +14,9 @@ */ #include "soft_bus_device_connection_listener.h" + +#include + #include "remote_command_manager.h" #include "soft_bus_manager.h" #include "device_info_manager.h" @@ -21,6 +24,8 @@ #include "constant_common.h" #include "device_manager.h" #include "dm_device_info.h" +#include "token_sync_event_handler.h" +#include "token_sync_manager_service.h" namespace OHOS { namespace Security { @@ -73,36 +78,58 @@ void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info) std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId); std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId); - ACCESSTOKEN_LOG_INFO(LABEL, - "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", - networkId.c_str(), - uuid.c_str(), - ConstantCommon::EncryptDevId(udid).c_str()); + ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s, uuid: %{public}s, udid: %{public}s", networkId.c_str(), + uuid.c_str(), ConstantCommon::EncryptDevId(udid).c_str()); - if (uuid != "" && udid != "") { - RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); - RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); - DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); + if (uuid == "" || udid == "") { + ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); + return; + } - std::string packageName = ACCESSTOKEN_PACKAGE_NAME; - std::string extra = ""; - std::vector deviceList; + RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid); + RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid); + DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID); - int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, - extra, deviceList); - if (ret != Constant::SUCCESS) { - ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret); - return; - } + std::function delayed = ([=]() { + ACCESSTOKEN_LOG_INFO(LABEL, "delayed task entry."); + bool sleeped = false; + std::vector deviceList; + std::unique_lock lock(exitMutex_); + + do { + std::string packageName = ACCESSTOKEN_PACKAGE_NAME; + std::string extra = ""; + + int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName, + extra, deviceList); + if (ret != Constant::SUCCESS) { + ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret); + return; + } + if (deviceList.empty() && sleeped == false) { + ACCESSTOKEN_LOG_INFO(LABEL, "delayed sleep."); + sleep(10000); + sleeped = true; + } else { + sleeped = false; + } + } while (sleeped == true); if (deviceList.empty()) { - ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote decice online, exit tokensync process"); + + ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote device online, exit tokensync process"); + SoftBusManager::GetInstance().Destroy(); + std::shared_ptr handler = + DelayedSingleton::GetInstance()->GetSendEventHandler(); + while ((handler != nullptr) && (!handler->IsIdle())) { + sleep(10); + } exit(0); } - } else { - ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed."); - } + }); + + std::thread threadObj(delayed); } void SoftBusDeviceConnectionListener::OnDeviceReady(const DmDeviceInfo &info) -- Gitee