From 88fcd2de1e404fb00ebc0f3e2d1d8ee9d7503b87 Mon Sep 17 00:00:00 2001 From: liuzhongming60 Date: Mon, 1 Sep 2025 21:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=90=AF=E6=B7=BB=E5=8A=A0=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=B8=B4=E6=97=B6=E5=8F=AF=E4=BF=A1=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=20Signed-off-by:=20liuzhongming=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/deviceprofile_connector.h | 3 +- .../src/deviceprofile_connector.cpp | 22 +++++++++ .../include/device_manager_service_impl.h | 1 + .../device_manager_service_impl_lite.h | 1 + .../devicestate/dm_device_state_manager.h | 1 + .../src/device_manager_service_impl.cpp | 7 +++ .../src/device_manager_service_impl_lite.cpp | 7 +++ .../devicestate/dm_device_state_manager.cpp | 46 +++++++++++++++++-- .../service/include/device_manager_service.h | 2 +- .../include/idevice_manager_service_impl.h | 2 + .../service/src/device_manager_service.cpp | 33 +++++++++++++ 11 files changed, 120 insertions(+), 5 deletions(-) diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 5bd29fa0c..1cd88b8d1 100644 --- a/commondependency/include/deviceprofile_connector.h +++ b/commondependency/include/deviceprofile_connector.h @@ -147,6 +147,7 @@ public: const std::vector &acceptEventUdids) = 0; virtual int32_t HandleAccountCommonEvent(const std::string &localUdid, const std::vector &deviceVec, const std::vector &foregroundUserIds, const std::vector &backgroundUserIds) = 0; + virtual int32_t GetAuthOnceUdids(std::unordered_set &udidSet) = 0; }; class DeviceProfileConnector : public IDeviceProfileConnector { @@ -331,7 +332,7 @@ public: const std::vector &backGroundUserIds); DM_EXPORT bool IsAllowAuthAlways(const std::string &localUdid, int32_t userId, const std::string &peerUdid, const std::string &pkgName, int64_t tokenId); - + int32_t GetAuthOnceUdids(std::unordered_set &udidSet); private: int32_t HandleDmAuthForm(DistributedDeviceProfile::AccessControlProfile profiles, DmDiscoveryInfo discoveryInfo); void GetParamBindTypeVec(DistributedDeviceProfile::AccessControlProfile profiles, std::string requestDeviceId, diff --git a/commondependency/src/deviceprofile_connector.cpp b/commondependency/src/deviceprofile_connector.cpp index adc893627..16c8db8d3 100644 --- a/commondependency/src/deviceprofile_connector.cpp +++ b/commondependency/src/deviceprofile_connector.cpp @@ -3515,6 +3515,28 @@ bool DeviceProfileConnector::IsAllowAuthAlways(const std::string &localUdid, int return false; } +int32_t DeviceProfileConnector::GetAuthOnceUdids(std::unordered_set &udidSet) +{ + std::vector profiles = GetAllAclIncludeLnnAcl(); + int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID(); + std::string localUdid = GetLocalDeviceId(); + for (auto &item : profiles) { + std::string accesserUdid = item.GetAccesser().GetAccesserDeviceId(); + std::string accesseeUdid = item.GetAccessee().GetAccesseeDeviceId(); + int32_t accesserUserId = item.GetAccesser().GetAccesserUserId(); + int32_t accesseeUserId = item.GetAccessee().GetAccesseeUserId(); + if (item.GetAuthenticationType() == ALLOW_AUTH_ONCE) { + if (accesserUserId == currentUserId && accesserUdid == localUdid) { + udidSet.insert(accesseeUdid); + } + if (accesseeUserId == currentUserId && accesseeUdid == localUdid) { + udidSet.insert(accesserUdid); + } + } + } + return DM_OK; +} + IDeviceProfileConnector *CreateDpConnectorInstance() { return &DeviceProfileConnector::GetInstance(); diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 60726df8f..13759a00d 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -205,6 +205,7 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); + void InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, const std::string &deviceUdidHash); private: int32_t PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject); std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/include/device_manager_service_impl_lite.h b/services/implementation/include/device_manager_service_impl_lite.h index 8ec4d49a2..3fecede6b 100644 --- a/services/implementation/include/device_manager_service_impl_lite.h +++ b/services/implementation/include/device_manager_service_impl_lite.h @@ -182,6 +182,7 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid); void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); + void InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, const std::string &deviceUdidHash); private: std::string GetUdidHashByNetworkId(const std::string &networkId); diff --git a/services/implementation/include/devicestate/dm_device_state_manager.h b/services/implementation/include/devicestate/dm_device_state_manager.h index a7ea1c6a4..d2a47129b 100644 --- a/services/implementation/include/devicestate/dm_device_state_manager.h +++ b/services/implementation/include/devicestate/dm_device_state_manager.h @@ -103,6 +103,7 @@ public: #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) int32_t DeleteSkCredAndAcl(const std::vector &acls); #endif + void StartDelTimerByDP(const std::string &deviceUdid, const std::string &deviceUdidHash); private: void StartEventThread(); void StopEventThread(); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 23051eb11..b72c528e7 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -3201,6 +3201,13 @@ void DeviceManagerServiceImpl::DeleteSessionKey(int32_t userId, DeviceProfileConnector::GetInstance().DeleteSessionKey(userId, skId); } +void DeviceManagerServiceImpl::InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, + const std::string &deviceUdidHash) +{ + CHECK_NULL_VOID(deviceStateMgr_); + deviceStateMgr_->StartDelTimerByDP(deviceUdid, deviceUdidHash); +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/implementation/src/device_manager_service_impl_lite.cpp b/services/implementation/src/device_manager_service_impl_lite.cpp index 9ba5f9ca1..b59957cc0 100644 --- a/services/implementation/src/device_manager_service_impl_lite.cpp +++ b/services/implementation/src/device_manager_service_impl_lite.cpp @@ -747,6 +747,13 @@ void DeviceManagerServiceImpl::DeleteHoDevice(const std::string &peerUdid, return; } +void DeviceManagerServiceImpl::InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, + const std::string &deviceUdidHash) +{ + (void)deviceUdid; + (void)deviceUdidHash; +} + extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void) { return new DeviceManagerServiceImpl; diff --git a/services/implementation/src/devicestate/dm_device_state_manager.cpp b/services/implementation/src/devicestate/dm_device_state_manager.cpp index e77e579b7..7e0db378b 100644 --- a/services/implementation/src/devicestate/dm_device_state_manager.cpp +++ b/services/implementation/src/devicestate/dm_device_state_manager.cpp @@ -256,13 +256,13 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) { std::lock_guard mutexLock(timerMapMutex_); - std::string networkId = deviceInfo.networkId; - LOGI("Start offline timer for networkId: %{public}s", GetAnonyString(networkId).c_str()); + std::string deviceId = deviceInfo.deviceId; + LOGI("Start offline timer for deviceId: %{public}s", GetAnonyString(deviceId).c_str()); if (timer_ == nullptr) { timer_ = std::make_shared(); } for (auto &iter : stateTimerInfoMap_) { - if ((iter.second.networkId == networkId) && !iter.second.isStart) { + if ((iter.first == deviceId) && !iter.second.isStart) { timer_->StartTimer(iter.second.timerName, OFFLINE_TIMEOUT, [this] (std::string name) { DmDeviceStateManager::DeleteTimeOutGroup(name); @@ -302,6 +302,12 @@ void DmDeviceStateManager::DeleteTimeOutGroup(std::string name) LOGE("remove hichain group find deviceId: %{public}s failed.", GetAnonyString(iter->first).c_str()); break; } + if (softbusConnector_ == nullptr || softbusConnector_->CheckIsOnline(idIter->second)) { + LOGI("device is online or status is unknown udidHash : %{public}s", + GetAnonyString(idIter->first).c_str()); + iter->second.isStart = false; + break; + } LOGI("remove hichain group with deviceId: %{public}s", GetAnonyString(idIter->second).c_str()); hiChainConnector_->DeleteTimeOutGroup((idIter->second).c_str()); #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -566,5 +572,39 @@ void DmDeviceStateManager::HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo, listener_->OnDeviceScreenStateChange(item, devInfo); } } + +void DmDeviceStateManager::StartDelTimerByDP(const std::string &deviceUdid, const std::string &deviceUdidHash) +{ + LOGI("StartDelTimerByDP for udidHash: %{public}s", GetAnonyString(std::string(deviceUdidHash)).c_str()); + std::lock_guard mutexLock(timerMapMutex_); + auto iter = stateTimerInfoMap_.find(deviceUdidHash); + if ((iter != stateTimerInfoMap_.end()) && (timer_ != nullptr)) { + timer_->DeleteTimer(iter->second.timerName); + stateTimerInfoMap_.erase(iter); + auto idIter = udidhash2udidMap_.find(deviceUdidHash); + if (idIter != udidhash2udidMap_.end()) { + udidhash2udidMap_.erase(idIter); + } + } + std::string sha256UdidHash = Crypto::Sha256(std::string(deviceUdidHash)); + std::string timerName = std::string(STATE_TIMER_PREFIX) + sha256UdidHash.substr(0, sha256UdidHash.size() / 2); + if (stateTimerInfoMap_.find(std::string(deviceUdidHash)) == stateTimerInfoMap_.end()) { + StateTimerInfo stateTimer = { + .timerName = timerName, + .isStart = true, + }; + stateTimerInfoMap_[std::string(deviceUdidHash)] = stateTimer; + } + if (udidhash2udidMap_.find(std::string(deviceUdidHash)) == udidhash2udidMap_.end()) { + udidhash2udidMap_[std::string(deviceUdidHash)] = deviceUdid; + } + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } + timer_->StartTimer(timerName, OFFLINE_TIMEOUT, + [this] (std::string name) { + DmDeviceStateManager::DeleteTimeOutGroup(name); + }); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 709e7301f..ca856c285 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -399,7 +399,7 @@ private: void DeleteHoDevice(const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds); void HandleAccountLogoutEventCallback(const std::string &commonEventType, int32_t currentUserId, int32_t beforeUserId); - + void InitTaskOfDelTimeOutAcl(); #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI) void SubscribePublishCommonEvent(); void QueryDependsSwitchState(); diff --git a/services/service/include/idevice_manager_service_impl.h b/services/service/include/idevice_manager_service_impl.h index 949542bed..08830f86f 100644 --- a/services/service/include/idevice_manager_service_impl.h +++ b/services/service/include/idevice_manager_service_impl.h @@ -286,6 +286,8 @@ public: const DmAccessCallee &callee, const std::string &sinkUdid) = 0; virtual void DeleteHoDevice(const std::string &peerUdid, const std::vector &foreGroundUserIds, const std::vector &backGroundUserIds) = 0; + virtual void InitTaskOfDelTimeOutAcl(const std::string &deviceUdid, + const std::string &deviceUdidHash) = 0; }; using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 45925abff..c5c37c728 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -354,6 +354,7 @@ int32_t DeviceManagerService::InitDMServiceListener() if (IsPC() && !MultipleUserConnector::IsUserUnlocked(currentUserId)) { HandleUserStopEvent(currentUserId); } + InitTaskOfDelTimeOutAcl(); #endif LOGI("Init success."); return DM_OK; @@ -4363,6 +4364,38 @@ void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &c MultipleUserConnector::SetAccountInfo(MultipleUserConnector::GetCurrentAccountUserID(), MultipleUserConnector::GetCurrentDMAccountInfo()); } + +void DeviceManagerService::InitTaskOfDelTimeOutAcl() +{ + CHECK_NULL_VOID(discoveryMgr_); + if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) { + LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr."); + return; + } + std::unordered_set udidSet; + discoveryMgr_->GetCommonDependencyObj()->GetAuthOnceUdids(udidSet); + if (udidSet.empty()) { + LOGI("no auth once data."); + return; + } + if (!IsDMServiceImplReady()) { + LOGE("instance not init or init failed."); + return; + } + + for (const std::string &udid : udidSet) { + char udidHash[DM_MAX_DEVICE_ID_LEN] = {0}; + if (Crypto::GetUdidHash(udid, reinterpret_cast(udidHash)) != DM_OK) { + LOGE("get udidhash failed."); + return; + } + if (SoftbusCache::GetInstance().CheckIsOnline(udidHash)) { + LOGE("device is online udidhash %{public}s.", GetAnonyString(udidHash).c_str()); + continue; + } + dmServiceImpl_->InitTaskOfDelTimeOutAcl(udid, udidHash); + } +} #endif } // namespace DistributedHardware } // namespace OHOS -- Gitee