From 7985524edae2a7f3259e3a26e465f475420c9b0e Mon Sep 17 00:00:00 2001 From: "xiaocong.ran" Date: Thu, 17 Mar 2022 10:07:12 +0800 Subject: [PATCH 1/4] Timer thread Signed-off-by: xiaocong.ran Change-Id: I9655f6b599f56fa3f4d65833b187fca25c50ede5 --- .../include/dialog_ui/js/pages/index/index.js | 1 + common/include/dm_constants.h | 1 + .../include/dependency/timer/dm_timer.h | 1 + .../src/authentication/dm_auth_manager.cpp | 61 +++++++++++++------ .../src/dependency/timer/dm_timer.cpp | 35 ++++++++--- .../devicestate/dm_device_state_manager.cpp | 15 +++-- .../src/discovery/dm_discovery_manager.cpp | 4 +- 7 files changed, 82 insertions(+), 36 deletions(-) diff --git a/common/include/dialog_ui/js/pages/index/index.js b/common/include/dialog_ui/js/pages/index/index.js index f579f6a3e..a60043193 100644 --- a/common/include/dialog_ui/js/pages/index/index.js +++ b/common/include/dialog_ui/js/pages/index/index.js @@ -8,6 +8,7 @@ export default { }, onInit() { console.info('getParams: ' + router.getParams()); + callNativeHandler("EVENT_INIT", "2"); }, onShow() { timel = setInterval(this.run,1000) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 8883eeb3e..491f126a7 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -84,6 +84,7 @@ enum { DM_AUTH_PEER_REJECT, DM_AUTH_NOT_AUTH, DM_AUTH_DONT_AUTH, + DM_AUTH_NOT_START, DM_SOFTBUS_FAILED = 3000, DM_SOFTBUS_CREATE_SESSION_SERVER_FAILED, DM_HICHAIN_FAILED = 4000, diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index a4ffec0fd..89967bfc2 100644 --- a/services/devicemanagerservice/include/dependency/timer/dm_timer.h +++ b/services/devicemanagerservice/include/dependency/timer/dm_timer.h @@ -55,6 +55,7 @@ private: void Release(); private: + std::mutex timerLock_; DmTimerStatus mStatus_; uint32_t mTimeOutSec_; TimeoutHandle mHandle_; diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 2fc6956ca..43913d6eb 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -15,6 +15,8 @@ #include "dm_auth_manager.h" +#include + #include "auth_message_processor.h" #include "auth_ui.h" #include "dm_ability_manager.h" @@ -33,14 +35,14 @@ namespace OHOS { namespace DistributedHardware { -const std::string AUTHENTICATE_TIMEOUT_TASK = "authenticateTimeoutTask"; -const std::string NEGOTIATE_TIMEOUT_TASK = "negotiateTimeoutTask"; -const std::string CONFIRM_TIMEOUT_TASK = "confirmTimeoutTask"; -const std::string SHOW_TIMEOUT_TASK = "showTimeoutTask"; -const std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask"; -const std::string ADD_TIMEOUT_TASK = "addTimeoutTask"; -const std::string WAIT_NEGOTIATE_TIMEOUT_TASK = "waitNegotiateTimeoutTask"; -const std::string WAIT_REQUEST_TIMEOUT_TASK = "waitRequestTimeoutTask"; +const std::string AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate"; +const std::string NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate"; +const std::string CONFIRM_TIMEOUT_TASK = "deviceManagerTimer:confirm"; +const std::string SHOW_TIMEOUT_TASK = "deviceManagerTimer:show"; +const std::string INPUT_TIMEOUT_TASK = "deviceManagerTimer:input"; +const std::string ADD_TIMEOUT_TASK = "deviceManagerTimer:add"; +const std::string WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate"; +const std::string WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest"; const int32_t SESSION_CANCEL_TIMEOUT = 0; const int32_t AUTHENTICATE_TIMEOUT = 120; @@ -55,9 +57,9 @@ const int32_t DEVICE_ID_HALF = 2; static void TimeOut(void *data, DmTimer& timer) { - LOGE("time out "); + LOGE("time out %s", timer.GetTimerName().c_str()); DmAuthManager *authMgr = (DmAuthManager *)data; - if (authMgr == nullptr) { + if (authMgr == nullptr || timer.GetTimerName().substr(0,19) != "deviceManagerTimer:") { LOGE("time out error"); return; } @@ -177,7 +179,7 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam) LOGI("DmAuthManager::VerifyAuthentication"); if (authResponseContext_ == nullptr) { LOGI("authResponseContext_ is not init"); - return DM_FAILED; + return DM_AUTH_NOT_START; } std::shared_ptr ptr; if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end() @@ -216,6 +218,7 @@ void DmAuthManager::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int3 authResponseState_->SetAuthManager(shared_from_this()); authResponseState_->Enter(); authResponseContext_ = std::make_shared(); + LOGI("DmAuthManager::timerMap_============================%d", timerMap_.size()); std::shared_ptr waitStartTimer = std::make_shared(WAIT_NEGOTIATE_TIMEOUT_TASK); timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] = waitStartTimer; waitStartTimer->Start(WAIT_NEGOTIATE_TIMEOUT, TimeOut, this); @@ -378,6 +381,8 @@ int32_t DmAuthManager::HandleAuthenticateTimeout() } if (authResponseState_ != nullptr && authResponseState_->GetStateType() != AuthState::AUTH_RESPONSE_FINISH) { + authResponseContext_->state = authResponseState_->GetStateType(); + authResponseContext_->reply = DM_TIME_OUT; authResponseState_->TransitionTo(std::make_shared()); } LOGI("DmAuthManager::HandleAuthenticateTimeout start complete"); @@ -481,6 +486,11 @@ void DmAuthManager::SendAuthRequest(const int32_t &sessionId) int32_t DmAuthManager::StartAuthProcess(const int32_t &action) { LOGI("DmAuthManager:: StartAuthProcess"); + if (authResponseContext_ == nullptr) { + LOGI("Authenticate is not start"); + return DM_AUTH_NOT_START; + } + authResponseContext_->reply = action; if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH && authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_CONFIRM) { @@ -499,6 +509,7 @@ void DmAuthManager::StartRespAuthProcess() if (timerMap_.find(CONFIRM_TIMEOUT_TASK) == timerMap_.end()) { return; } + timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH) { std::shared_ptr inputStartTimer = std::make_shared(INPUT_TIMEOUT_TASK); @@ -530,6 +541,7 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId) LOGE("DecodeRequestAuth jsonStr error"); return DM_FAILED; } + LOGI("DmAuthManager::AddMember start %s", authResponseContext_->authToken.c_str()); nlohmann::json jsonObject; jsonObject[TAG_GROUP_ID] = authResponseContext_->groupId; @@ -715,9 +727,14 @@ void DmAuthManager::ShowConfigDialog() OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ACE_X, ACE_Y, ACE_WIDTH, ACE_HEIGHT, [authMgr_](int32_t id, const std::string& event, const std::string& params) { - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); - LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); - authMgr_->StartAuthProcess(atoi(params.c_str())); + if (params == "2"){ + LOGI("Dialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); + authMgr_->SetPageId(id); + } else { + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); + authMgr_->StartAuthProcess(atoi(params.c_str())); + } }); LOGI("ShowConfigDialog end"); #endif @@ -756,8 +773,8 @@ int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) } if (authResponseContext_ == nullptr) { - LOGI("authResponseContext_ is not init"); - return DM_FAILED; + LOGI("Authenticate is not start"); + return DM_AUTH_NOT_START; } dmAbilityMgr_->StartAbilityDone(); @@ -780,8 +797,8 @@ int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) int32_t DmAuthManager::OnUserOperation(int32_t action) { if (authResponseContext_ == nullptr) { - LOGI("authResponseContext_ is not init"); - return DM_FAILED; + LOGI("Authenticate is not start"); + return DM_AUTH_NOT_START; } switch (action) { @@ -842,12 +859,20 @@ void DmAuthManager::UserSwitchEventCallback (int32_t userId) int32_t DmAuthManager::SetPageId(int32_t pageId) { + if (authResponseContext_ == nullptr) { + LOGI("Authenticate is not start"); + return DM_AUTH_NOT_START; + } authResponseContext_->pageId = pageId; return DM_OK; } int32_t DmAuthManager::SetReason(int32_t reason, int32_t state) { + if (authResponseContext_ == nullptr) { + LOGI("Authenticate is not start"); + return DM_AUTH_NOT_START; + } if (state < AuthState::AUTH_REQUEST_FINISH) { authRequestContext_->reason = reason; } diff --git a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp index 0dadbdf98..b62c8f4f2 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -16,6 +16,7 @@ #include "dm_timer.h" #include +#include #include "securec.h" @@ -26,7 +27,7 @@ const int32_t MILL_SECONDS_PER_SECOND = 1000; } DmTimer::DmTimer(const std::string &name) { - if (name.empty()) { + if (name.empty() || name.substr(0,19) != "deviceManagerTimer:") { LOGI("DmTimer name is null"); return; } @@ -43,17 +44,19 @@ DmTimer::DmTimer(const std::string &name) DmTimer::~DmTimer() { - if (mTimerName_.empty()) { + if (mTimerName_.empty() || mTimerName_.substr(0,19) != "deviceManagerTimer:") { LOGI("DmTimer is not init"); return; } LOGI("DmTimer %s Destroy in", mTimerName_.c_str()); + std::lock_guard lock(timerLock_); Release(); } DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) { - if (mTimerName_.empty() || handle == nullptr || data == nullptr) { + if (mTimerName_.empty() || mTimerName_.substr(0,19) != "deviceManagerTimer:" + || handle == nullptr || data == nullptr) { LOGI("DmTimer is not init or param empty"); return DmTimerStatus::DM_STATUS_FINISH; } @@ -78,7 +81,7 @@ DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) void DmTimer::Stop(int32_t code) { - if (mTimerName_.empty()) { + if (mTimerName_.empty() || mTimerName_.substr(0,19) != "deviceManagerTimer:" || mHandleData_ == nullptr) { LOGI("DmTimer is not init"); return; } @@ -92,13 +95,19 @@ void DmTimer::Stop(int32_t code) void DmTimer::WaitForTimeout() { - if (mTimerName_.empty()) { + if (mTimerName_.empty() || mTimerName_.substr(0,19) != "deviceManagerTimer:") { LOGI("DmTimer is not init"); return; } LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); - int32_t nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND); + int32_t nfds = -1; + { + std::lock_guard lock(timerLock_); + nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND); + } + + LOGI("DmTimer is triggering"); if (nfds < 0) { LOGE("DmTimer %s epoll_wait returned n=%d, error: %d", mTimerName_.c_str(), nfds, errno); } else if (nfds > 0) { @@ -111,17 +120,20 @@ void DmTimer::WaitForTimeout() LOGE("DmTimer %s exit with errno %d", mTimerName_.c_str(), errno); } } - Release(); } else { + if (mHandleData_ == nullptr || mTimerName_.substr(0,19) != "deviceManagerTimer:") { + LOGI("DmTimer out pointer is nullptr"); + return; + } mHandle_(mHandleData_, *this); - Release(); LOGE("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); } + Release(); } int32_t DmTimer::CreateTimeFd() { - if (mTimerName_.empty()) { + if (mTimerName_.empty() || mTimerName_.substr(0,19) != "deviceManagerTimer:") { LOGI("DmTimer is not init"); return DM_STATUS_FINISH; } @@ -146,7 +158,7 @@ int32_t DmTimer::CreateTimeFd() void DmTimer::Release() { - if (mTimerName_.empty()) { + if (mTimerName_.empty() || mTimerName_.substr(0,19) != "deviceManagerTimer:") { LOGI("DmTimer is not init"); return; } @@ -161,6 +173,9 @@ void DmTimer::Release() if (mEpFd_ >= 0) { close(mEpFd_); } + mTimeOutSec_ = 0; + mHandle_ = nullptr; + mHandleData_ = nullptr; mTimeFd_[0] = 0; mTimeFd_[1] = 0; mEpFd_ = 0; diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index 1cef6db56..d36e9e7d9 100755 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -28,8 +28,8 @@ static void TimeOut(void *data, DmTimer& timer) { LOGE("time out "); DmDeviceStateManager *deviceStateMgr = (DmDeviceStateManager*)data; - if (deviceStateMgr == nullptr) { - LOGE("OnDeviceOfflineTimeOut deviceStateMgr = nullptr"); + if (deviceStateMgr == nullptr || timer.GetTimerName().substr(0,19) != "deviceManagerTimer:") { + LOGE("time out error"); return; } deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName()); @@ -245,14 +245,15 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) std::lock_guard mutexLock(timerMapMutex_); deviceinfoMap_[deviceInfo.deviceId] = deviceId; - auto iter = timerMap_.find(deviceId); + std::string timerName = "deviceManagerTimer:" + deviceId; + auto iter = timerMap_.find(timerName); if (iter != timerMap_.end()) { iter->second->Stop(SESSION_CANCEL_TIMEOUT); return; } - std::shared_ptr offLineTimer = std::make_shared(deviceId); + std::shared_ptr offLineTimer = std::make_shared(timerName); if (offLineTimer != nullptr) { - timerMap_[deviceId] = offLineTimer; + timerMap_[timerName] = offLineTimer; } } @@ -276,8 +277,10 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) void DmDeviceStateManager::DeleteTimeOutGroup(std::string deviceId) { LOGI("remove hichain group with device: %s", GetAnonyString(deviceId).c_str()); + LOGI("remove hichain group with device: %s", deviceId.c_str()); + LOGI("remove hichain group with device: %s", deviceId.substr(18).c_str()); if (hiChainConnector_ != nullptr) { - hiChainConnector_->DeleteTimeOutGroup(deviceId.c_str()); + hiChainConnector_->DeleteTimeOutGroup(deviceId.substr(18).c_str()); } } } // namespace DistributedHardware diff --git a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp index 494214339..eff66a7da 100644 --- a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp +++ b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp @@ -21,7 +21,7 @@ namespace OHOS { namespace DistributedHardware { -const std::string DISCOVERY_TIMEOUT_TASK = "discoveryTimeout"; +const std::string DISCOVERY_TIMEOUT_TASK = "deviceManagerTimer:discovery"; const int32_t DISCOVERY_TIMEOUT = 120; const int32_t SESSION_CANCEL_TIMEOUT = 0; @@ -29,7 +29,7 @@ static void TimeOut(void *data, DmTimer& timer) { LOGE("time out"); DmDiscoveryManager *discoveryMgr = (DmDiscoveryManager *)data; - if (discoveryMgr == nullptr) { + if (discoveryMgr == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) { LOGE("time out error"); return; } -- Gitee From c8e6c8d000c1b287917decda15da80a1e9862479 Mon Sep 17 00:00:00 2001 From: fuchao Date: Thu, 17 Mar 2022 02:16:13 +0000 Subject: [PATCH 2/4] =?UTF-8?q?update=20services/devicemanagerservice/src/?= =?UTF-8?q?authentication/dm=5Fauth=5Fmanager.cpp.=20=E4=BF=AE=E6=94=B9LOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/authentication/dm_auth_manager.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 43913d6eb..1b5ec969d 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -218,7 +218,6 @@ void DmAuthManager::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int3 authResponseState_->SetAuthManager(shared_from_this()); authResponseState_->Enter(); authResponseContext_ = std::make_shared(); - LOGI("DmAuthManager::timerMap_============================%d", timerMap_.size()); std::shared_ptr waitStartTimer = std::make_shared(WAIT_NEGOTIATE_TIMEOUT_TASK); timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] = waitStartTimer; waitStartTimer->Start(WAIT_NEGOTIATE_TIMEOUT, TimeOut, this); @@ -768,7 +767,7 @@ void DmAuthManager::ShowStartAuthDialog() int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) { if (dmAbilityMgr_ == nullptr) { - LOGI("dmAbilityMgr_ is nullptr"); + LOGI("authResponseContext_ is not init"); return DM_POINT_NULL; } @@ -797,7 +796,7 @@ int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) int32_t DmAuthManager::OnUserOperation(int32_t action) { if (authResponseContext_ == nullptr) { - LOGI("Authenticate is not start"); + LOGI("authResponseContext_ is not init"); return DM_AUTH_NOT_START; } -- Gitee From f0c70a45d7bbe5a0b790fcbf2b8d5cd25899bee7 Mon Sep 17 00:00:00 2001 From: fuchao Date: Thu, 17 Mar 2022 02:19:21 +0000 Subject: [PATCH 3/4] update services/devicemanagerservice/src/authentication/dm_auth_manager.cpp. modify log --- .../src/authentication/dm_auth_manager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 1b5ec969d..ac1e3dd82 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -767,12 +767,12 @@ void DmAuthManager::ShowStartAuthDialog() int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) { if (dmAbilityMgr_ == nullptr) { - LOGI("authResponseContext_ is not init"); + LOGI("dmAbilityMgr_ is nullptr"); return DM_POINT_NULL; } if (authResponseContext_ == nullptr) { - LOGI("Authenticate is not start"); + LOGI("authResponseContext_ is not init"); return DM_AUTH_NOT_START; } -- Gitee From debee4f0a5b21cf4618509948abf99394bf623fa Mon Sep 17 00:00:00 2001 From: fuchao Date: Thu, 17 Mar 2022 02:21:01 +0000 Subject: [PATCH 4/4] =?UTF-8?q?update=20services/devicemanagerservice/src/?= =?UTF-8?q?devicestate/dm=5Fdevice=5Fstate=5Fmanager.cpp.=20substr=20?= =?UTF-8?q?=E6=88=AA=E5=8F=96=E9=95=BF=E5=BA=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/devicestate/dm_device_state_manager.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index d36e9e7d9..3467b977e 100755 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -277,10 +277,8 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) void DmDeviceStateManager::DeleteTimeOutGroup(std::string deviceId) { LOGI("remove hichain group with device: %s", GetAnonyString(deviceId).c_str()); - LOGI("remove hichain group with device: %s", deviceId.c_str()); - LOGI("remove hichain group with device: %s", deviceId.substr(18).c_str()); if (hiChainConnector_ != nullptr) { - hiChainConnector_->DeleteTimeOutGroup(deviceId.substr(18).c_str()); + hiChainConnector_->DeleteTimeOutGroup(deviceId.substr(19).c_str()); } } } // namespace DistributedHardware -- Gitee