diff --git a/commondependency/include/deviceprofile_connector.h b/commondependency/include/deviceprofile_connector.h index 5bd29fa0c9bca351d6d4764bf172df55b2e71323..1cd88b8d18450b361fb14c73bd5aa10d7078d5c5 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 adc8936278caa8bc4f30272eccf7ebd18a27b00b..16c8db8d3aea840c03819166c78c652111771e4a 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 60726df8f88a2e6bb78e05898265789b5455a390..13759a00d3000ce20f2c6c065eac5f6ddbe2e3de 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 8ec4d49a25f0968d43a1658e3728e7f48ea24dbe..3fecede6b016de3f21413b8b090c206d0466e9ec 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 a7ea1c6a4c205d3f5bd5ca3c216ac5d5f8585cdb..d2a47129baf497d1d0b68ab7a579ae86b8b82ab3 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 23051eb11af75f28434c1a114bab08dc4f14aaba..b72c528e7a5e0a42201bdb1655717d33079e4439 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 9ba5f9ca1f88aa8f754e5f49515f50444554ea01..b59957cc02cb3a3d6707e5a77f79b59a5a7fee8e 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 e77e579b7fd319f9819489bac22c05e69606b21f..7e0db378bd1aae0d6b219f10dd5cb5cee7397034 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 709e7301f82ebded3a8b48bc725e3199ce8e06b4..ca856c2855e0a064536c10330c4afb6fd9090c0c 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 949542bedeb80a1f942cc5aecc0edaf66183fd77..08830f86f96369739066fecdcbad7e238870ba7f 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 45925abffa3eadb13348f4b1052ef81e64597065..c5c37c7280329c53e014ff25ccd78aa7598b931d 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