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 0e0e2096511b6906811ddf74362fba523e32e8a0..0d73ff77a101b4d78f058158be7897524f5e96d6 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 02781031fbf30e1c296d147899f0fb739d4a8494..ed71148367aba8ad7be25927a1446e8eae1550ab 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 92ab841fa49c1f58009565764507bd68e7d1015e..3a3fe46c6d7506eeae64655d41222496cd6af0d3 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 37f9557c127255960df8c1951f8fc68e9dcd8423..65779f7b3ffe14b0f8a83d9a9023961f01ab496d 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 86722fb50845ddc9a60ea0351de88531463ce997..1ec1620477021fac4db06bc781fa600b6bf4dc26 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 114326d9d88add61490987b4dc582d0d5dbe0af1..30fff0c47414d8f1dd6de96d83a7002e1fd61c8e 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 fea4121847bb63a0245eee0c5ca6d1c5c0744134..8d3ea5d3207e3c452766c4d845dd9c2e65c14fc8 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::seconds(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 d456a7da91c6a4f676f528cea9b1fbd12046cf48..2766f95a0a95dceb436b82855a9d3c22f5fe4148 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/dependency/softbus/softbus_session.cpp b/services/implementation/src/dependency/softbus/softbus_session.cpp index c4aea1706aa9a7cf7c5fb3a2bec3773f500b54d2..056eb40442acffaca75ff0bc67d5ff22945fa10f 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); diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 51f5d07267bc969c5e5f297cf8301de7f480caa8..c0028663f09a8123a555dfe03aa9a6480243b60f 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);