From aa56ae6c66e6e1f466bcef05e3d42cf660264475 Mon Sep 17 00:00:00 2001 From: "xiaocong.ran" Date: Thu, 17 Mar 2022 13:55:01 +0800 Subject: [PATCH 1/2] Timer Signed-off-by: xiaocong.ran Change-Id: I0dfa64085ee49f1cadd097901db415d7ca4a6e6c --- common/include/dm_constants.h | 1 + .../dialog_ui/js/pages/index/index.js | 1 + .../include/dependency/timer/dm_timer.h | 2 + .../src/authentication/dm_auth_manager.cpp | 56 +++++++++++++------ .../src/dependency/timer/dm_timer.cpp | 35 ++++++++---- .../devicestate/dm_device_state_manager.cpp | 13 +++-- .../src/discovery/dm_discovery_manager.cpp | 4 +- 7 files changed, 78 insertions(+), 34 deletions(-) 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/common/include/show_confirm_dialog/dialog_ui/js/pages/index/index.js b/common/include/show_confirm_dialog/dialog_ui/js/pages/index/index.js index 13a4384c5..e9730cf4a 100644 --- a/common/include/show_confirm_dialog/dialog_ui/js/pages/index/index.js +++ b/common/include/show_confirm_dialog/dialog_ui/js/pages/index/index.js @@ -7,6 +7,7 @@ export default { }, onInit() { console.info('getParams: ' + router.getParams()); + callNativeHandler("EVENT_INIT", "2"); }, onShow() { timel = setInterval(this.run,1000) diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index a4ffec0fd..d3504d2db 100644 --- a/services/devicemanagerservice/include/dependency/timer/dm_timer.h +++ b/services/devicemanagerservice/include/dependency/timer/dm_timer.h @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -55,6 +56,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 bb075c5b9..8952008a6 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" @@ -31,14 +33,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; @@ -53,9 +55,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; } @@ -175,7 +177,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() @@ -388,6 +390,8 @@ int32_t DmAuthManager::HandleAuthenticateTimeout() if (authResponseState_ != nullptr && authResponseState_->GetStateType() != AuthState::AUTH_RESPONSE_FINISH) { authResponseState_->TransitionTo(std::make_shared()); + authResponseContext_->state = authResponseState_->GetStateType(); + authResponseContext_->reply = DM_TIME_OUT; } LOGI("DmAuthManager::HandleAuthenticateTimeout start complete"); return DM_OK; @@ -490,6 +494,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) { @@ -719,9 +728,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 { + LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); + Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); + authMgr_->StartAuthProcess(atoi(params.c_str())); + } }); LOGI("ShowConfigDialog end"); } @@ -760,7 +774,7 @@ int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) if (authResponseContext_ == nullptr) { LOGI("authResponseContext_ is not init"); - return DM_FAILED; + return DM_AUTH_NOT_START; } dmAbilityMgr_->StartAbilityDone(); @@ -784,7 +798,7 @@ int32_t DmAuthManager::OnUserOperation(int32_t action) { if (authResponseContext_ == nullptr) { LOGI("authResponseContext_ is not init"); - return DM_FAILED; + return DM_AUTH_NOT_START; } switch (action) { @@ -845,12 +859,22 @@ 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..cbf486b53 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -26,7 +26,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 +43,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 +80,8 @@ 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..3467b977e 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; } } @@ -277,7 +278,7 @@ void DmDeviceStateManager::DeleteTimeOutGroup(std::string deviceId) { LOGI("remove hichain group with device: %s", GetAnonyString(deviceId).c_str()); if (hiChainConnector_ != nullptr) { - hiChainConnector_->DeleteTimeOutGroup(deviceId.c_str()); + hiChainConnector_->DeleteTimeOutGroup(deviceId.substr(19).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 353234c0dae5bd88dce644ed6e67c9562d239c6a Mon Sep 17 00:00:00 2001 From: fuchao Date: Thu, 17 Mar 2022 06:02:02 +0000 Subject: [PATCH 2/2] =?UTF-8?q?update=20services/devicemanagerservice/src/?= =?UTF-8?q?authentication/dm=5Fauth=5Fmanager.cpp.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devicemanagerservice/src/authentication/dm_auth_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 8952008a6..b696044c4 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -389,9 +389,9 @@ int32_t DmAuthManager::HandleAuthenticateTimeout() } if (authResponseState_ != nullptr && authResponseState_->GetStateType() != AuthState::AUTH_RESPONSE_FINISH) { - authResponseState_->TransitionTo(std::make_shared()); authResponseContext_->state = authResponseState_->GetStateType(); authResponseContext_->reply = DM_TIME_OUT; + authResponseState_->TransitionTo(std::make_shared()); } LOGI("DmAuthManager::HandleAuthenticateTimeout start complete"); return DM_OK; -- Gitee