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 c196aada9dec3b38f4aa12027a38f22058b293bf..2ceff22d20a5c9dc8f805652e4d53202bea1c077 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 ed71148367aba8ad7be25927a1446e8eae1550ab..03a0d83dd1b808e5f8878c60f3665d0225bcbd54 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 32347ab91c9f22d57e8ce6965d43310e2038ee4a..328a61a1a1b76539766d8f2c9ac46a164aa471c5 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 b57e00daa894d89d525a7c21185ae328343605ae..49eb28e1781bf68134cc0407b4bae2ae8c0d4130 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 8265cbe402385e5039829a55813fca898be60b37..fea4121847bb63a0245eee0c5ca6d1c5c0744134 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 ff7e0ef1761fbbae9ac0c926100a2861e031e1fe..88731386dc6e4f61deb573a22da7cb952a175da6 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)