From 123c8d79495bc33cc143e367fc20a7909912ba0f Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Thu, 24 Apr 2025 00:02:27 +0800 Subject: [PATCH 1/3] fix: new protocol bug Signed-off-by: ZHANGHE24 --- .../authentication_v2/dm_auth_manager_base.h | 1 + .../authentication_v2/dm_auth_state_machine.h | 2 + .../hichain/hichain_auth_connector.h | 2 + .../include/device_manager_service_impl.h | 1 + .../src/authentication_v2/auth_manager.cpp | 2 + .../dm_auth_manager_base.cpp | 1 + .../dm_auth_state_machine.cpp | 16 +++- .../hichain/hichain_auth_connector.cpp | 14 +++ .../src/device_manager_service_impl.cpp | 90 +++++++++++-------- 9 files changed, 89 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 0e0e20965..0d73ff77a 100644 --- a/services/implementation/include/authentication_v2/dm_auth_manager_base.h +++ b/services/implementation/include/authentication_v2/dm_auth_manager_base.h @@ -96,6 +96,7 @@ 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 02781031f..ed7114836 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state_machine.h +++ b/services/implementation/include/authentication_v2/dm_auth_state_machine.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "dm_auth_state.h" @@ -113,6 +114,7 @@ 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/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h index 92ab841fa..3a3fe46c6 100644 --- a/services/implementation/include/dependency/hichain/hichain_auth_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_auth_connector.h @@ -60,7 +60,9 @@ public: int32_t ImportCredential(int32_t osAccountId, int32_t peerOsAccountId, std::string deviceId, std::string publicKey); int32_t DeleteCredential(const std::string &deviceId, int32_t userId, int32_t peerUserId); int32_t RegisterHiChainAuthCallback(std::shared_ptr callback); + int32_t UnRegisterHiChainAuthCallback(); int32_t RegisterHiChainAuthCallbackById(int64_t id, std::shared_ptr callback); + int32_t UnRegisterHiChainAuthCallbackById(int64_t id); int32_t GetCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey); int32_t ProcessCredData(int64_t authReqId, const std::string &data); diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 37f9557c1..65779f7b3 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -265,6 +265,7 @@ private: std::mutex mapMutex_; // sessionsMap_的锁 std::map sessionEnableCvMap_; // Condition variable corresponding to the session std::map sessionEnableMutexMap_; // Lock corresponding to the session + std::map sessionEnableCvReadyMap_; // Condition variable ready flag std::map logicalSessionId2TokenIdMap_; // The relationship between logicalSessionId and tokenId std::map logicalSessionId2SessionIdMap_; // The relationship logicalSessionId and physical sessionId std::map> configsMap_; // Import when authMgr is not initialized diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index 86722fb50..1ec162047 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -149,8 +149,10 @@ AuthManager::~AuthManager() context_->successFinished = true; context_->authStateMachine->Stop(); // Stop statemMachine thread context_->timer->DeleteAll(); + LOGI("AuthManager context variables destroy successful."); } bindParam_.clear(); + LOGI("DmAuthManager destructor"); } void AuthManager::RegisterCleanNotifyCallback(CleanNotifyCallback cleanNotifyCallback) 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 114326d9d..30fff0c47 100644 --- a/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_manager_base.cpp @@ -93,6 +93,7 @@ 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 fea412184..92f1a9c09 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp @@ -262,9 +262,18 @@ DmEventType DmAuthStateMachine::WaitExpectEvent(DmEventType eventType) std::unique_lock lock(eventMutex_); auto startTime = std::chrono::high_resolution_clock::now(); while (running_.load()) { - eventCv_.wait(lock, [&] { - return !running_.load() || !eventQueue_.empty(); - }); + if (eventCv_.wait_for(lock, std::chrono::second(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; + } if (!running_.load()) { return DmEventType::ON_FAIL; } @@ -296,6 +305,7 @@ 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/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index d456a7da9..2766f95a0 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -65,6 +65,13 @@ int32_t HiChainAuthConnector::RegisterHiChainAuthCallback(std::shared_ptr lock(dmDeviceAuthCallbackMutex_); + dmDeviceAuthCallback_ = nullptr; + return DM_OK; +} + // 当前id为tokenId对应生成的requestId int32_t HiChainAuthConnector::RegisterHiChainAuthCallbackById(int64_t id, std::shared_ptr callback) @@ -74,6 +81,13 @@ int32_t HiChainAuthConnector::RegisterHiChainAuthCallbackById(int64_t id, return DM_OK; } +int32_t HiChainAuthConnector::UnRegisterHiChainAuthCallbackById(int64_t id) +{ + std::lock_guard lock(dmDeviceAuthCallbackMutex_); + dmDeviceAuthCallbackMap_[id] = nullptr; + return DM_OK; +} + std::shared_ptr HiChainAuthConnector::GetDeviceAuthCallback(int64_t id) { if (dmDeviceAuthCallbackMap_.find(id) != dmDeviceAuthCallbackMap_.end()) { diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 51f5d0726..c0028663f 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -345,7 +345,7 @@ void DeviceManagerServiceImpl::CleanAuthMgrByLogicalSessionId(uint64_t logicalSe authMgr_->SetTransferReady(true); authMgr_->ClearSoftbusSessionCallback(); } - + hiChainAuthConnector_->UnRegisterHiChainAuthCallbackById(logicalSessionId); if (authMgrMap_.find(tokenId) != authMgrMap_.end()) { authMgrMap_[tokenId] = nullptr; authMgrMap_.erase(tokenId); @@ -455,6 +455,7 @@ void DeviceManagerServiceImpl::Release() softbusConnector_->UnRegisterSoftbusStateCallback(); softbusConnector_->GetSoftbusSession()->UnRegisterSessionCallback(); hiChainConnector_->UnRegisterHiChainCallback(); + hiChainAuthConnector_->UnRegisterHiChainAuthCallback(); authMgr_ = nullptr; for (auto& pair : authMgrMap_) { pair.second = nullptr; @@ -711,6 +712,12 @@ 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); + } sessionEnableCvMap_[sessionId].notify_all(); } std::string peerUdid = ""; @@ -777,38 +784,34 @@ std::shared_ptr DeviceManagerServiceImpl::GetAuthMgrByMessage(i { uint64_t tokenId = 0; if (msgType == MSG_TYPE_REQ_ACL_NEGOTIATE) { - if (logicalSessionId != 0) { - curSession->logicalSessionSet_.insert(logicalSessionId); - std::string bundleName; - int32_t displayId = 0; - if (jsonObject[TAG_PEER_BUNDLE_NAME_V2].IsString()) { - bundleName = jsonObject[TAG_PEER_BUNDLE_NAME_V2].Get(); - } - if (jsonObject[DM_TAG_PEER_DISPLAY_ID].IsNumberInteger()) { - displayId = jsonObject[DM_TAG_PEER_DISPLAY_ID].Get(); - } - tokenId = GetTokenId(false, displayId, bundleName); - if (tokenId == 0) { - LOGE("GetAuthMgrByMessage, Get tokenId failed."); - return nullptr; - } - if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { - LOGE("GetAuthMgrByMessage, logicalSessionId exists in logicalSessionId2TokenIdMap_."); - return nullptr; - } - logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; + std::string bundleName; + int32_t displayId = 0; + if (jsonObject[TAG_PEER_BUNDLE_NAME_V2].IsString()) { + bundleName = jsonObject[TAG_PEER_BUNDLE_NAME_V2].Get(); + } + if (jsonObject[DM_TAG_PEER_DISPLAY_ID].IsNumberInteger()) { + displayId = jsonObject[DM_TAG_PEER_DISPLAY_ID].Get(); + } + tokenId = GetTokenId(false, displayId, bundleName); + if (tokenId == 0) { + LOGE("GetAuthMgrByMessage, Get tokenId failed."); + return nullptr; } if (InitAndRegisterAuthMgr(false, tokenId, curSession, logicalSessionId) != DM_OK) { return nullptr; } + curSession->logicalSessionSet_.insert(logicalSessionId); + if (logicalSessionId2TokenIdMap_.find(logicalSessionId) != logicalSessionId2TokenIdMap_.end()) { + LOGE("GetAuthMgrByMessage, logicalSessionId exists in logicalSessionId2TokenIdMap_."); + return nullptr; + } + logicalSessionId2TokenIdMap_[logicalSessionId] = tokenId; } else { - if (logicalSessionId != 0) { - if (curSession->logicalSessionSet_.find(logicalSessionId) == curSession->logicalSessionSet_.end()) { - LOGE("GetAuthMgrByMessage, The logical session ID does not exist in the physical session."); - return nullptr; - } - tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; + if (curSession->logicalSessionSet_.find(logicalSessionId) == curSession->logicalSessionSet_.end()) { + LOGE("GetAuthMgrByMessage, The logical session ID does not exist in the physical session."); + return nullptr; } + tokenId = logicalSessionId2TokenIdMap_[logicalSessionId]; } return GetAuthMgrByTokenId(tokenId); @@ -876,6 +879,7 @@ int32_t DeviceManagerServiceImpl::TransferByAuthType(int32_t authType, authMgr_->EnableInsensibleSwitching(); curSession->logicalSessionSet_.insert(0); curSession->logicalSessionCnt_.fetch_add(1); + logicalSessionId2SessionIdMap_[0] = sessionId; authMgr->OnSessionDisable(); } else { authMgr_->DisableInsensibleSwitching(); @@ -998,9 +1002,13 @@ void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data,         2. Old-to-new: When the sink side receives an 80 message and detects a version mismatch, it receives the 80 message, directly creates a new old protocol authMgr, and re-OnSessionOpened and OnBytesReceived.         */ - if (TransferOldAuthMgr(msgType, jsonObject, curSession) != DM_OK) { - LOGE("DeviceManagerServiceImpl::OnBytesReceived TransferOldAuthMgr failed"); - return; + if (curSession->version_ == "" || CompareVersion(curSession->version_, DM_VERSION_5_0_OLD_MAX)) { + if (TransferOldAuthMgr(msgType, jsonObject, curSession) != DM_OK) { + LOGE("DeviceManagerServiceImpl::OnBytesReceived TransferOldAuthMgr failed"); + return; + } + } else { + LOGI("DeviceManagerServiceImpl::OnBytesReceived Reuse Old AuthMgr, sessionId: %{public}d.", sessionId); } authMgr = authMgr_; } @@ -1400,8 +1408,14 @@ std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std: } std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); - sessionEnableCvMap_[sessionId].wait(cvLock); - + sessionEnableCvReadyMap_[sessionId] = false; + if (sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::seconds(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); + goto error; + } instance = std::make_shared(sessionId, deviceId); deviceId2SessionIdMap_[deviceId] = sessionId; sessionsMap_[sessionId] = instance; @@ -1533,10 +1547,13 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P // Logical session random number int sessionId = curSession->sessionId_; - uint64_t 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; + uint64_t logicalSessionId = 0; + if (curSession->version_ == "" || CompareVersion(curSession->version_, DM_VERSION_5_0_OLD_MAX)) { + 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; + } } // Create on the src end. @@ -1553,7 +1570,6 @@ int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const P auto authMgr = GetAuthMgrByTokenId(tokenId); if (authMgr == nullptr) { - LOGE("authMgr is nullptr"); return ERR_DM_POINT_NULL; } authMgr->SetBindTargetParams(targetId); -- Gitee From e434cf3380a30ff84428baa98f94e8deb5380c6f Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Thu, 24 Apr 2025 17:49:50 +0800 Subject: [PATCH 2/3] fix: add onsessionopened result log Signed-off-by: ZHANGHE24 --- .../implementation/src/dependency/softbus/softbus_session.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/implementation/src/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index c4aea1706..056eb4044 100644 --- a/services/implementation/src/dependency/softbus/softbus_session.cpp +++ b/services/implementation/src/dependency/softbus/softbus_session.cpp @@ -142,9 +142,9 @@ int32_t SoftbusSession::SendHeartbeatData(int32_t sessionId, std::string &messag int SoftbusSession::OnSessionOpened(int sessionId, int result) { - LOGD("OnSessionOpened, success, sessionId: %{public}d.", sessionId); + LOGI("OnSessionOpened, success, sessionId: %{public}d, result: %{public}d.", sessionId, result); if (sessionCallback_ == nullptr) { - LOGD("Session callback is not registered."); + LOGI("Session callback is not registered."); return DM_OK; } int32_t sessionSide = GetSessionSide(sessionId); -- Gitee From e170f28388dd10cab6a9acc0ea530642becdb26f Mon Sep 17 00:00:00 2001 From: ZHANGHE24 Date: Thu, 24 Apr 2025 17:54:34 +0800 Subject: [PATCH 3/3] fix: compile error Signed-off-by: ZHANGHE24 --- .../src/authentication_v2/dm_auth_state_machine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 92f1a9c09..8d3ea5d32 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state_machine.cpp @@ -262,7 +262,7 @@ 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::second(WAIT_TIMEOUT), [&] { + if (eventCv_.wait_for(lock, std::chrono::seconds(WAIT_TIMEOUT), [&] { return !running_.load() || !eventQueue_.empty() || eventCvReady_; })) { eventCvReady_ = false; -- Gitee