diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 8883eeb3e78dcceec849b895725ad8acc9854b8b..5e8b12431053b5ee8aea2d5e3941b63ed1c82d36 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, @@ -186,6 +187,18 @@ const int32_t ACE_X = 50; const int32_t ACE_Y = 300; const int32_t ACE_WIDTH = 600; const int32_t ACE_HEIGHT = 400; +const std::string EVENT_CONFIRM = "EVENT_CONFIRM"; +const std::string EVENT_CANCEL = "EVENT_CANCEL"; +const std::string EVENT_INIT = "EVENT_INIT"; +const std::string EVENT_CONFIRM_CODE = "0"; +const std::string EVENT_CANCEL_CODE = "1"; +const std::string EVENT_INIT_CODE = "2"; + +//timer +const std::string TIMER_PREFIX = "deviceManagerTimer:"; +const std::string STATE_TIMER_PREFIX = "stateTimer_"; +const int32_t TIMER_PREFIX_LENGTH = 19; +const int32_t TIMER_DEFAULT = 0; } // namespace DistributedHardware } // namespace OHOS #endif 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 13a4384c54373870c4ca3b1e17d2daf7567c1239..1850093f0f9060dde2ce28411b680c5eaba63fa6 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 @@ -1,12 +1,19 @@ import router from '@ohos.router' var timel = null; +var EVENT_CONFIRM = "EVENT_CONFIRM"; +var EVENT_CANCEL = "EVENT_CANCEL"; +var EVENT_INIT = "EVENT_INIT"; +var EVENT_CONFIRM_CODE = "0"; +var EVENT_CANCEL_CODE = "1"; +var EVENT_INIT_CODE = "2"; export default { data: { seconds:60, }, onInit() { console.info('getParams: ' + router.getParams()); + callNativeHandler(EVENT_INIT, EVENT_INIT_CODE); }, onShow() { timel = setInterval(this.run,1000) @@ -17,15 +24,15 @@ export default { clearInterval(timel); timel = null; console.info('click cancel'); - callNativeHandler("EVENT_CONFIRM", "1"); + callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); } }, onConfirm() { console.info('click confirm'); - callNativeHandler("EVENT_CONFIRM", "0"); + callNativeHandler(EVENT_CONFIRM, EVENT_CONFIRM_CODE); }, onCancel() { console.info('click cancel'); - callNativeHandler("EVENT_CANCEL", "1"); + callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); } } \ No newline at end of file diff --git a/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js b/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js index 488972d500f7250991051ef8511d5c24f22ce86d..b0405866e1fa6d464fb316930f6688999a28663e 100644 --- a/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js +++ b/ext/pin_auth/input_pin_dialog/dialog_ui/js/pages/index/index.js @@ -3,6 +3,12 @@ import router from '@ohos.router'; var numbs = 0; var code = 0; var inputVal = 0; +var EVENT_CONFIRM = "EVENT_CONFIRM"; +var EVENT_CANCEL = "EVENT_CANCEL"; +var EVENT_INIT = "EVENT_INIT"; +var EVENT_CONFIRM_CODE = "0"; +var EVENT_CANCEL_CODE = "1"; +var EVENT_INIT_CODE = "2"; export default { data: { pincode: router.getParams().pinCode, @@ -11,7 +17,7 @@ export default { }, onInit() { code = router.getParams().pinCode; - callNativeHandler("EVENT_CONFIRM", "2"); + callNativeHandler(EVENT_INIT, EVENT_INIT_CODE); }, onChange(e){ inputVal = e.value; @@ -22,11 +28,11 @@ export default { console.info('click confirm numbs < 3 '); if(code == inputVal){ console.info('click confirm code == inputVal'); - callNativeHandler("EVENT_INPUT", "0"); + callNativeHandler(EVENT_CONFIRM, EVENT_CONFIRM_CODE); }else{ if(numbs == 3){ console.info('click confirm code != inputVal and numbs == 3'); - callNativeHandler("EVENT_CONFIRM", "1"); + callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); } console.info('click confirm code != inputVal'); this.isShow = true; @@ -34,11 +40,11 @@ export default { } }else{ console.info('click confirm numbs > 3 '); - callNativeHandler("EVENT_CONFIRM", "1"); + callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); } }, onCancel() { console.info('click cancel'); - callNativeHandler("EVENT_CONFIRM", "1"); + callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); } } \ No newline at end of file diff --git a/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.hml b/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.hml index 845c99e570c9e56592ea375fa53410773b4c743c..715ceda514b37e784da387dac2e99d674181b4be 100644 --- a/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.hml +++ b/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.hml @@ -8,7 +8,7 @@ {{ pincode }} - \ No newline at end of file diff --git a/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.js b/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.js index 598deca1f5d8075b1e73fbdc3cd4fbdcd5781635..daef5ab44dd70c124f45c4db236a62c3b37cae8f 100644 --- a/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.js +++ b/ext/pin_auth/show_pin_dialog/dialog_ui/js/pages/index/index.js @@ -1,14 +1,20 @@ import router from '@ohos.router' +var EVENT_CONFIRM = "EVENT_CONFIRM"; +var EVENT_CANCEL = "EVENT_CANCEL"; +var EVENT_INIT = "EVENT_INIT"; +var EVENT_CONFIRM_CODE = "0"; +var EVENT_CANCEL_CODE = "1"; +var EVENT_INIT_CODE = "2"; export default { data: { pincode: router.getParams().pinCode, }, onInit() { - callNativeHandler("EVENT_CONFIRM", "0"); + callNativeHandler(EVENT_INIT, EVENT_INIT_CODE); }, - onConfirm() { - console.info('click confirm'); - callNativeHandler("EVENT_CONFIRM", "1"); + onCancel() { + console.info('click onCancel'); + callNativeHandler(EVENT_CANCEL, EVENT_CANCEL_CODE); } } \ No newline at end of file diff --git a/ext/pin_auth/src/pin_auth.cpp b/ext/pin_auth/src/pin_auth.cpp index 4c3e3040964568eca1a2df2b93d81a3ec2ce05a8..1bc7838b47a8324c06c90ad831a3018c8806c093 100644 --- a/ext/pin_auth/src/pin_auth.cpp +++ b/ext/pin_auth/src/pin_auth.cpp @@ -67,7 +67,7 @@ int32_t PinAuth::VerifyAuthentication(std::string &authToken, const std::string { times_ += 1; if (authParam.length() == 1) { - if (authParam == "0") { + if (authParam == EVENT_CONFIRM_CODE) { return DM_OK; } LOGE("Peer rejection"); @@ -89,9 +89,9 @@ int32_t PinAuth::VerifyAuthentication(std::string &authToken, const std::string return DM_FAILED; } int32_t code = authTokenJson[PIN_CODE_KEY]; - int32_t pinToken = authTokenJson[PIN_TOKEN]; + std::string pinToken = authTokenJson[PIN_TOKEN]; int32_t inputPinCode = authParamJson[PIN_CODE_KEY]; - int32_t inputPinToken = authParamJson[PIN_TOKEN]; + std::string inputPinToken = authParamJson[PIN_TOKEN]; if (code == inputPinCode && pinToken == inputPinToken) { return DM_OK; } else if (code != inputPinCode && times_ < MAX_VERIFY_TIMES) { diff --git a/ext/pin_auth/src/pin_auth_ui.cpp b/ext/pin_auth/src/pin_auth_ui.cpp index eb976a67898c97ed963f8228bf400323baa920f8..dcd283df7c76d5e039438793c46df37189f5f532 100644 --- a/ext/pin_auth/src/pin_auth_ui.cpp +++ b/ext/pin_auth/src/pin_auth_ui.cpp @@ -42,10 +42,10 @@ int32_t PinAuthUi::ShowPinDialog(int32_t code, std::shared_ptr au OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ACE_X, ACE_Y, ACE_WIDTH, ACE_HEIGHT, [authManager](int32_t id, const std::string& event, const std::string& params) { - if (strcmp(params.c_str(), "0") == 0) { + if (params == EVENT_INIT_CODE) { authManager->SetPageId(id); } - if (strcmp(params.c_str(), "1") == 0) { + if (params == EVENT_CANCEL_CODE) { LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); } @@ -68,10 +68,10 @@ int32_t PinAuthUi::InputPinDialog(int32_t code, std::shared_ptr a OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, ACE_X, ACE_Y, ACE_WIDTH, ACE_HEIGHT, [authManager](int32_t id, const std::string& event, const std::string& params) { - if (strcmp(params.c_str(), "2") == 0) { + if (params == EVENT_INIT_CODE) { authManager->SetPageId(id); } - if (strcmp(params.c_str(), "0") == 0 || strcmp(params.c_str(), "1") == 0) { + if (params == EVENT_CANCEL_CODE || params == EVENT_CONFIRM_CODE) { Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); LOGI("CancelDialog start id:%d,event:%s,parms:%s", id, event.c_str(), params.c_str()); authManager->VerifyAuthentication(params.c_str()); diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index a4ffec0fdddc84600365578547bde4c5f67e169c..b15a95770402f131c0f7f6f9419f3c9d54d42d65 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 mTimerLock_; DmTimerStatus mStatus_; uint32_t mTimeOutSec_; TimeoutHandle mHandle_; diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h index 7817313fc7a3af962550f2886f9560afa8ee4cbc..32a7dacd7b86fc261a85f1b5694a71736efdff21 100755 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -29,6 +29,14 @@ namespace OHOS { namespace DistributedHardware { #define OFFLINE_TIMEOUT 300 +struct StateTimerInfo +{ + std::string timerName; + std::string netWorkId; + std::string deviceId; + std::shared_ptr timer; +}; + class DmDeviceStateManager final : public ISoftbusStateCallback, public std::enable_shared_from_this { public: @@ -48,7 +56,7 @@ public: int32_t RegisterSoftbusStateCallback(); void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo); void StartOffLineTimer(const DmDeviceInfo &deviceInfo); - void DeleteTimeOutGroup(std::string deviceId); + void DeleteTimeOutGroup(std::string stateTimer); void RegisterDevStateCallback(const std::string &pkgName, const std::string &extra); void UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra); @@ -60,8 +68,7 @@ private: std::shared_ptr listener_; std::map remoteDeviceInfos_; std::map decisionInfos_; - std::map> timerMap_; - std::map deviceinfoMap_; + std::map stateTimerInfoMap_; std::shared_ptr hiChainConnector_; std::string decisionSoName_; }; diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index bb075c5b98ab08045d520c72ccf794811e03fc19..38dcdab95f81a225d8a694a89b303a2b0190daa5 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 = TIMER_PREFIX + "authenticate"; +const std::string NEGOTIATE_TIMEOUT_TASK = TIMER_PREFIX + "negotiate"; +const std::string CONFIRM_TIMEOUT_TASK = TIMER_PREFIX + "confirm"; +const std::string SHOW_TIMEOUT_TASK = TIMER_PREFIX + "show"; +const std::string INPUT_TIMEOUT_TASK = TIMER_PREFIX + "input"; +const std::string ADD_TIMEOUT_TASK = TIMER_PREFIX + "add"; +const std::string WAIT_NEGOTIATE_TIMEOUT_TASK = TIMER_PREFIX + "waitNegotiate"; +const std::string WAIT_REQUEST_TIMEOUT_TASK = TIMER_PREFIX + "waitRequest"; const int32_t SESSION_CANCEL_TIMEOUT = 0; const int32_t AUTHENTICATE_TIMEOUT = 120; @@ -53,12 +55,18 @@ const int32_t DEVICE_ID_HALF = 2; static void TimeOut(void *data, DmTimer& timer) { - LOGE("time out "); + LOGI("time out %s", timer.GetTimerName().c_str()); + if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("time out is not our timer"); + return; + } + DmAuthManager *authMgr = (DmAuthManager *)data; if (authMgr == nullptr) { - LOGE("time out error"); + LOGE("authMgr is nullptr"); return; } + authMgr->HandleAuthenticateTimeout(); } @@ -145,7 +153,7 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au int32_t DmAuthManager::UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId) { if (pkgName.empty()) { - LOGI(" DmAuthManager::UnAuthenticateDevice failed pkgName is null"); + LOGE(" DmAuthManager::UnAuthenticateDevice failed pkgName is null"); return DM_FAILED; } std::string deviceUdid; @@ -175,7 +183,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() @@ -333,7 +341,7 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId { LOGI("DmAuthManager::OnGroupCreated start"); if (authResponseState_ == nullptr) { - LOGI("DmAuthManager::AuthenticateDevice end"); + LOGE("DmAuthManager::AuthenticateDevice end"); return; } if (groupId == "{}") { @@ -387,6 +395,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"); @@ -490,6 +500,11 @@ void DmAuthManager::SendAuthRequest(const int32_t &sessionId) int32_t DmAuthManager::StartAuthProcess(const int32_t &action) { LOGI("DmAuthManager:: StartAuthProcess"); + if (authResponseContext_ == nullptr) { + LOGE("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 +734,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 == EVENT_INIT_CODE) { + 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"); } @@ -754,13 +774,13 @@ void DmAuthManager::ShowStartAuthDialog() int32_t DmAuthManager::GetAuthenticationParam(DmAuthParam &authParam) { if (dmAbilityMgr_ == nullptr) { - LOGI("dmAbilityMgr_ is nullptr"); + LOGE("dmAbilityMgr_ is nullptr"); return DM_POINT_NULL; } if (authResponseContext_ == nullptr) { - LOGI("authResponseContext_ is not init"); - return DM_FAILED; + LOGE("authResponseContext_ is not init"); + return DM_AUTH_NOT_START; } dmAbilityMgr_->StartAbilityDone(); @@ -783,8 +803,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; + LOGE("authResponseContext_ is not init"); + return DM_AUTH_NOT_START; } switch (action) { @@ -845,12 +865,22 @@ void DmAuthManager::UserSwitchEventCallback (int32_t userId) int32_t DmAuthManager::SetPageId(int32_t pageId) { + if (authResponseContext_ == nullptr) { + LOGE("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) { + LOGE("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 0dadbdf98216742db8862d53e9589e3fae6427cc..0835f9b5e4f36582f283da52a037c179e1fc10c9 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -17,6 +17,7 @@ #include +#include "dm_constants.h" #include "securec.h" namespace OHOS { @@ -26,10 +27,11 @@ const int32_t MILL_SECONDS_PER_SECOND = 1000; } DmTimer::DmTimer(const std::string &name) { - if (name.empty()) { - LOGI("DmTimer name is null"); + if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer name is null"); return; } + mStatus_ = DmTimerStatus::DM_STATUS_INIT; mTimeOutSec_ = 0; mHandle_ = nullptr; @@ -43,20 +45,23 @@ DmTimer::DmTimer(const std::string &name) DmTimer::~DmTimer() { - if (mTimerName_.empty()) { - LOGI("DmTimer is not init"); + if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer is not init"); return; } + LOGI("DmTimer %s Destroy in", mTimerName_.c_str()); Release(); } DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) { - if (mTimerName_.empty() || handle == nullptr || data == nullptr) { - LOGI("DmTimer is not init or param empty"); + if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT + || handle == nullptr || data == nullptr) { + LOGE("DmTimer is not init or param empty"); return DmTimerStatus::DM_STATUS_FINISH; } + LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut); if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) { return DmTimerStatus::DM_STATUS_BUSY; @@ -78,10 +83,12 @@ DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) void DmTimer::Stop(int32_t code) { - if (mTimerName_.empty()) { - LOGI("DmTimer is not init"); + if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT + || mHandleData_ == nullptr) { + LOGE("DmTimer is not init"); return; } + if (mTimeFd_[1]) { char event = 'S'; if (write(mTimeFd_[1], &event, 1) < 0) { @@ -92,41 +99,47 @@ void DmTimer::Stop(int32_t code) void DmTimer::WaitForTimeout() { - if (mTimerName_.empty()) { - LOGI("DmTimer is not init"); + if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("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); - if (nfds < 0) { - LOGE("DmTimer %s epoll_wait returned n=%d, error: %d", mTimerName_.c_str(), nfds, errno); - } else if (nfds > 0) { - char event = 0; - if (mEvents_[0].events & EPOLLIN) { - int num = read(mTimeFd_[0], &event, 1); - if (num > 0) { - LOGI("DmTimer %s exit with event %d", mTimerName_.c_str(), event); - } else { - LOGE("DmTimer %s exit with errno %d", mTimerName_.c_str(), errno); + int32_t nfds = -1; + { + std::lock_guard lock(mTimerLock_); + nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND); + LOGI("DmTimer is triggering"); + if (nfds > 0) { + char event = 0; + if (mEvents_[0].events & EPOLLIN) { + int num = read(mTimeFd_[0], &event, 1); + if (num > 0) { + LOGI("DmTimer %s exit with event %d", mTimerName_.c_str(), event); + } else { + LOGE("DmTimer %s exit with errno %d", mTimerName_.c_str(), errno); + } } - } - Release(); - } else { + } + } + + if (nfds == 0 && mHandleData_ != nullptr) { mHandle_(mHandleData_, *this); - Release(); - LOGE("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); + LOGI("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); + } else { + LOGE("DmTimer %s epoll_wait returned n=%d, error: %d", mTimerName_.c_str(), nfds, errno); } + Release(); } int32_t DmTimer::CreateTimeFd() { - if (mTimerName_.empty()) { - LOGI("DmTimer is not init"); + if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer is not init"); return DM_STATUS_FINISH; } - LOGI("DmTimer %s creatTimeFd", mTimerName_.c_str()); + LOGI("DmTimer %s creatTimeFd", mTimerName_.c_str()); int ret = pipe(mTimeFd_); if (ret < 0) { LOGE("DmTimer %s CreateTimeFd fail:(%d) errno(%d)", mTimerName_.c_str(), ret, errno); @@ -146,21 +159,27 @@ int32_t DmTimer::CreateTimeFd() void DmTimer::Release() { - if (mTimerName_.empty()) { - LOGI("DmTimer is not init"); + if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer is not init"); return; } + + std::lock_guard lock(mTimerLock_); LOGI("DmTimer %s Release in", mTimerName_.c_str()); if (mStatus_ == DmTimerStatus::DM_STATUS_INIT) { - LOGI("DmTimer %s already Release", mTimerName_.c_str()); + LOGE("DmTimer %s already Release", mTimerName_.c_str()); return; } + mStatus_ = DmTimerStatus::DM_STATUS_INIT; close(mTimeFd_[0]); close(mTimeFd_[1]); 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 1cef6db5644e8e04a502580e21820ae27e4a98a5..ff935f6d6d2ca9c9655b0dfc9137ad93e65a9b0b 100755 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -26,12 +26,18 @@ const int32_t SESSION_CANCEL_TIMEOUT = 0; static void TimeOut(void *data, DmTimer& timer) { - LOGE("time out "); + LOGI("time out %s",timer.GetTimerName().c_str()); + if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("time out is not our timer"); + return; + } + DmDeviceStateManager *deviceStateMgr = (DmDeviceStateManager*)data; if (deviceStateMgr == nullptr) { - LOGE("OnDeviceOfflineTimeOut deviceStateMgr = nullptr"); + LOGE("deviceStateMgr is nullptr"); return; } + deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName()); } @@ -244,41 +250,48 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) LOGI("Register OffLine Timer with device: %s", GetAnonyString(deviceId).c_str()); std::lock_guard mutexLock(timerMapMutex_); - deviceinfoMap_[deviceInfo.deviceId] = deviceId; - auto iter = timerMap_.find(deviceId); - if (iter != timerMap_.end()) { - iter->second->Stop(SESSION_CANCEL_TIMEOUT); - return; + for (auto &iter : stateTimerInfoMap_) { + if (iter.second.netWorkId == deviceInfo.deviceId) { + iter.second.timer->Stop(SESSION_CANCEL_TIMEOUT); + return; + } } - std::shared_ptr offLineTimer = std::make_shared(deviceId); + + std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(stateTimerInfoMap_.size()); + std::shared_ptr offLineTimer = std::make_shared(timerName); if (offLineTimer != nullptr) { - timerMap_[deviceId] = offLineTimer; + StateTimerInfo stateTimer = { + .timerName = timerName, + .netWorkId = deviceInfo.deviceId, + .deviceId = deviceId, + .timer = offLineTimer + }; + stateTimerInfoMap_[timerName] = stateTimer; } } void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) { - if (deviceinfoMap_.find(deviceInfo.deviceId) == deviceinfoMap_.end()) { - LOGE("fail to get udid by networkId"); - return; - } - - LOGI("start offline timer with device: %s", GetAnonyString(deviceinfoMap_[deviceInfo.deviceId]).c_str()); - std::lock_guard mutexLock(timerMapMutex_); - for (auto &iter : timerMap_) { - if (iter.first == deviceinfoMap_[deviceInfo.deviceId]) { - iter.second->Start(OFFLINE_TIMEOUT, TimeOut, this); + LOGI("start offline timer"); + for (auto &iter : stateTimerInfoMap_) { + if (iter.second.netWorkId == deviceInfo.deviceId) { + iter.second.timer->Start(OFFLINE_TIMEOUT, TimeOut, this); } } - deviceinfoMap_.erase(deviceInfo.deviceId); } -void DmDeviceStateManager::DeleteTimeOutGroup(std::string deviceId) +void DmDeviceStateManager::DeleteTimeOutGroup(std::string stateTimer) { - LOGI("remove hichain group with device: %s", GetAnonyString(deviceId).c_str()); + std::lock_guard mutexLock(timerMapMutex_); if (hiChainConnector_ != nullptr) { - hiChainConnector_->DeleteTimeOutGroup(deviceId.c_str()); + auto iter = stateTimerInfoMap_.find(stateTimer); + if (iter != stateTimerInfoMap_.end()) { + LOGI("remove hichain group with device: %s", + GetAnonyString(stateTimerInfoMap_[stateTimer].deviceId).c_str()); + hiChainConnector_->DeleteTimeOutGroup(stateTimerInfoMap_[stateTimer].deviceId.c_str()); + } } + stateTimerInfoMap_.erase(stateTimer); } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp index 4942143390f1bdc5e34e76b397bdd4ad77ae5950..d653e2b18d7e931d75c294e12c0ced595658d0c1 100644 --- a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp +++ b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp @@ -21,18 +21,24 @@ namespace OHOS { namespace DistributedHardware { -const std::string DISCOVERY_TIMEOUT_TASK = "discoveryTimeout"; +const std::string DISCOVERY_TIMEOUT_TASK = TIMER_PREFIX + "discovery"; const int32_t DISCOVERY_TIMEOUT = 120; const int32_t SESSION_CANCEL_TIMEOUT = 0; static void TimeOut(void *data, DmTimer& timer) { - LOGE("time out"); + LOGI("time out %s",timer.GetTimerName().c_str()); + if (data == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) { + LOGE("time out is not our timer"); + return; + } + DmDiscoveryManager *discoveryMgr = (DmDiscoveryManager *)data; if (discoveryMgr == nullptr) { - LOGE("time out error"); + LOGE("discoveryMgr is nullptr"); return; } + discoveryMgr->HandleDiscoveryTimeout(); }