From 48a6845a149d695df37e2bd35b1b900e3bf99b6a Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Wed, 30 Apr 2025 00:54:42 +0800 Subject: [PATCH] fix: bindtarget bug Signed-off-by: ZHANGHE24 --- .../authentication_v2/dm_auth_manager_base.h | 1 - .../authentication_v2/dm_auth_state_machine.h | 1 - .../include/device_manager_service_impl.h | 4 +- .../dm_auth_manager_base.cpp | 1 - .../dm_auth_state_machine.cpp | 16 +----- .../src/device_manager_service_impl.cpp | 55 +++++++++++-------- 6 files changed, 38 insertions(+), 40 deletions(-) diff --git a/services/implementation/include/authentication_v2/dm_auth_manager_base.h b/services/implementation/include/authentication_v2/dm_auth_manager_base.h index c196aada9..2ceff22d2 100644 --- a/services/implementation/include/authentication_v2/dm_auth_manager_base.h +++ b/services/implementation/include/authentication_v2/dm_auth_manager_base.h @@ -96,7 +96,6 @@ extern const int32_t HML_SESSION_TIMEOUT; extern const int32_t SESSION_HEARTBEAT_TIMEOUT; extern const int32_t PIN_AUTH_TIMEOUT; extern const int32_t EVENT_TIMEOUT; -extern const int32_t WAIT_TIMEOUT; extern const int32_t DM_AUTH_TYPE_MAX; extern const int32_t DM_AUTH_TYPE_MIN; diff --git a/services/implementation/include/authentication_v2/dm_auth_state_machine.h b/services/implementation/include/authentication_v2/dm_auth_state_machine.h index ed7114836..03a0d83dd 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state_machine.h +++ b/services/implementation/include/authentication_v2/dm_auth_state_machine.h @@ -114,7 +114,6 @@ private: std::condition_variable stateCv_; std::mutex eventMutex_; std::condition_variable eventCv_; - bool eventCvReady_{false}; // Direction of authentication DmAuthDirection direction_; diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 32347ab91..328a61a1a 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -243,6 +243,8 @@ private: void CheckIsLastLnnAcl(DistributedDeviceProfile::AccessControlProfile profile, DistributedDeviceProfile::AccessControlProfile delProfile, DmOfflineParam &lnnAclParam, bool &isLastLnnAcl, const std::string &localUdid); + void BindTargetImpl(uint64_t tokenId, const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam); private: std::shared_ptr authMgr_; // Old protocol only std::mutex authMgrMtx_; @@ -263,7 +265,7 @@ private: std::map deviceId2SessionIdMap_; std::map> sessionsMap_; // sessionId corresponds to the session object std::map deviceIdMutexMap_; // Lock corresponding to the device ID - std::mutex mapMutex_; // sessionsMap_的锁 + std::mutex mapMutex_; // sessionsMap_ lock std::map sessionEnableCvMap_; // Condition variable corresponding to the session std::map sessionEnableMutexMap_; // Lock corresponding to the session std::map sessionEnableCvReadyMap_; // Condition variable ready flag diff --git a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp index b57e00daa..49eb28e17 100644 --- a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp @@ -97,7 +97,6 @@ const int32_t HML_SESSION_TIMEOUT = 10; const int32_t SESSION_HEARTBEAT_TIMEOUT = 50; const int32_t PIN_AUTH_TIMEOUT = 60; const int32_t EVENT_TIMEOUT = 5000; // 5000 ms -const int32_t WAIT_TIMEOUT = 2000; // 2000 ms int32_t AuthManagerBase::AuthenticateDevice(const std::string &pkgName, int32_t authType, diff --git a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp index 8265cbe40..fea412184 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp @@ -262,18 +262,9 @@ DmEventType DmAuthStateMachine::WaitExpectEvent(DmEventType eventType) std::unique_lock lock(eventMutex_); auto startTime = std::chrono::high_resolution_clock::now(); while (running_.load()) { - if (eventCv_.wait_for(lock, std::chrono::milliseconds(WAIT_TIMEOUT), [&] { - return !running_.load() || !eventQueue_.empty() || eventCvReady_; - })) { - eventCvReady_ = false; - LOGI("DmAuthStateMachine: WaitExpectEvent wait successful."); - } else { - if (!eventCvReady_) { - LOGE("DmAuthStateMachine: WaitExpectEvent wait timeout."); - return DmEventType::ON_TIMEOUT; - } - eventCvReady_ = false; - } + eventCv_.wait(lock, [&] { + return !running_.load() || !eventQueue_.empty(); + }); if (!running_.load()) { return DmEventType::ON_FAIL; } @@ -305,7 +296,6 @@ void DmAuthStateMachine::NotifyEventFinish(DmEventType eventType) { std::unique_lock lock(eventMutex_); eventQueue_.push(eventType); - eventCvReady_ = true; } eventCv_.notify_one(); if (eventType == DmEventType::ON_FAIL) { diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index ff7e0ef17..88731386d 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -717,11 +717,11 @@ int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result) { std::lock_guard lock(sessionEnableMutexMap_[sessionId]); if (result == 0) { - sessionEnableCvReadyMap_[sessionId] = true; LOGE("OnSessionOpened successful, sessionId: %{public}d", sessionId); } else { LOGE("OnSessionOpened failed, sessionId: %{public}d, res: %{public}d", sessionId, result); } + sessionEnableCvReadyMap_[sessionId] = true; sessionEnableCvMap_[sessionId].notify_all(); } std::string peerUdid = ""; @@ -1424,14 +1424,10 @@ std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std: } std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); - sessionEnableCvReadyMap_[sessionId] = false; - if (sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::milliseconds(WAIT_TIMEOUT), - [&] { return sessionEnableCvReadyMap_[sessionId]; })) { - LOGI("session enable, sessionId: %{public}d.", sessionId); - } else { - LOGE("wait session enable timeout or enable fail, sessionId: %{public}d.", sessionId); - return nullptr; - } + sessionEnableCvMap_[sessionId].wait(cvLock, [&] { + return sessionEnableCvReadyMap_[sessionId]; + }); + instance = std::make_shared(sessionId, deviceId); deviceId2SessionIdMap_[deviceId] = sessionId; sessionsMap_[sessionId] = instance; @@ -1533,14 +1529,9 @@ int32_t DeviceManagerServiceImpl::ParseConnectAddr(const PeerTargetId &targetId, return DM_OK; } -int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, - const std::map &bindParam) +void DeviceManagerServiceImpl::BindTargetImpl(uint64_t tokenId, const std::string &pkgName, + const PeerTargetId &targetId, const std::map &bindParam) { - if (pkgName.empty()) { - LOGE("BindTarget failed, pkgName is empty."); - return ERR_DM_INPUT_PARA_INVALID; - } - std::string deviceId = ""; PeerTargetId targetIdTmp = const_cast(targetId); if (ParseConnectAddr(targetId, deviceId, bindParam) == DM_OK) { @@ -1548,14 +1539,14 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P } else { if (targetId.deviceId.empty()) { LOGE("DeviceManagerServiceImpl::BindTarget failed, ParseConnectAddr failed."); - return ERR_DM_INPUT_PARA_INVALID; + return; } } // Created only at the source end. The same target device will not be created repeatedly with the new protocol. std::shared_ptr curSession = GetOrCreateSession(targetIdTmp.deviceId, bindParam); if (curSession == nullptr) { LOGE("Failed to create the session. Target deviceId: %{public}s.", targetIdTmp.deviceId.c_str()); - return ERR_DM_AUTH_OPEN_SESSION_FAILED; + return; } // Logical session random number @@ -1565,16 +1556,17 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P logicalSessionId = GenerateRandNum(sessionId); if (curSession->logicalSessionSet_.find(logicalSessionId) != curSession->logicalSessionSet_.end()) { LOGE("Failed to create the logical session."); - return ERR_DM_LOGIC_SESSION_CREATE_FAILED; + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; } } // Create on the src end. - uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); int32_t ret = InitAndRegisterAuthMgr(true, tokenId, curSession, logicalSessionId); if (ret != DM_OK) { LOGE("InitAndRegisterAuthMgr failed, ret %{public}d.", ret); - return ret; + softbusConnector_->GetSoftbusSession()->CloseAuthSession(sessionId); + return; } curSession->logicalSessionSet_.insert(logicalSessionId); curSession->logicalSessionCnt_.fetch_add(1); @@ -1583,14 +1575,31 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { - return ERR_DM_POINT_NULL; + CleanAuthMgrByLogicalSessionId(logicalSessionId); + return; } authMgr->SetBindTargetParams(targetId); if ((ret = authMgr->BindTarget(pkgName, targetIdTmp, bindParam, sessionId, logicalSessionId)) != DM_OK) { LOGE("authMgr BindTarget failed, ret %{public}d.", ret); CleanAuthMgrByLogicalSessionId(logicalSessionId); } - return ret; + LOGI("DeviceManagerServiceImpl BindTargetImpl thread end, tokenId: %{public}" PRId64 ".", tokenId); + return; +} + +int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId, + const std::map &bindParam) +{ + if (pkgName.empty()) { + LOGE("BindTarget failed, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + + uint64_t tokenId = IPCSkeleton::GetCallingTokenID(); + std::thread newThread(&DeviceManagerServiceImpl::BindTargetImpl, this, tokenId, pkgName, + targetId, bindParam); + newThread.detach(); + return DM_OK; } int32_t DeviceManagerServiceImpl::DpAclAdd(const std::string &udid) -- Gitee