diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index a2124ae27128f8252eb9e7dc5350e57997f2b544..5e6933e48b4df0ced66ec65e23189e039558a9cd 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -91,6 +91,7 @@ enum { DM_HICHAIN_GROUP_CREATE_FAILED, DM_HICHAIN_MEMBER_ADD_FAILED, DM_HICHAIN_CREATE_CHANNEL_FAILED, + DM_AUTH_NO_TIMER, }; const std::string TARGET_PKG_NAME_KEY = "targetPkgName"; const std::string HOST_PKG_NAME_KEY = "hostPackageName"; @@ -209,6 +210,12 @@ const std::string WAIT_REQUEST_TIMEOUT_TASK = TIMER_PREFIX + "waitRequest"; const std::string STATE_TIMER_PREFIX = "stateTimer_"; const int32_t TIMER_PREFIX_LENGTH = 19; const int32_t TIMER_DEFAULT = 0; +const int32_t NO_TIMER = -1; +const int32_t INIT_SIZE = 3; +const int32_t MAX_EVENT_NUMBER = 10; +const int32_t EXPAND_TWICE = 2; +const int32_t SEC_TO_MM = 1000; +const int32_t MAX_EVENTS = 5; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H diff --git a/services/devicemanagerservice/include/authentication/dm_auth_manager.h b/services/devicemanagerservice/include/authentication/dm_auth_manager.h index a463dbf3ff3a7d8fced23f5c233c9b018c00d6aa..700c965fdbd399ff4dbe51e32aa3226be5d47cbb 100644 --- a/services/devicemanagerservice/include/authentication/dm_auth_manager.h +++ b/services/devicemanagerservice/include/authentication/dm_auth_manager.h @@ -384,7 +384,7 @@ private: std::shared_ptr authRequestContext_; std::shared_ptr authResponseContext_; std::shared_ptr authMessageProcessor_; - std::map> timerMap_; + std::shared_ptr timerHeap_; std::shared_ptr dmAbilityMgr_; bool isCryptoSupport_ = false; bool isFinishOfLocal_ = true; diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index 27def3ae2959329288bde3f5f7659f3bbbd5d68c..63822d47712c363efc273b90b50015c7e7d418b5 100644 --- a/services/devicemanagerservice/include/dependency/timer/dm_timer.h +++ b/services/devicemanagerservice/include/dependency/timer/dm_timer.h @@ -15,89 +15,84 @@ #ifndef TIMER_H #define TIMER_H + +#include #include -#include +#include +#include #include -#if !defined(__LITEOS_M__) #include -#include +#include +#include #include -#include -#endif -#include -#include - -#include "dm_log.h" - namespace OHOS { namespace DistributedHardware { -class DmTimer; -typedef void (*TimeoutHandle)(void *data, DmTimer& timer); - -#define MAX_EVENTS 255 - -enum DmTimerStatus : int32_t { - DM_STATUS_INIT = 0, - DM_STATUS_RUNNING = 1, - DM_STATUS_BUSY = 2, - DM_STATUS_CREATE_ERROR = 3, - DM_STATUS_FINISH = 6, +typedef void (*TimeoutHandle)(void *data, std::string timerName); +class DmTimer { +public: + DmTimer(std::string name, time_t expire, void *user, TimeoutHandle mHandle) + :userData_(user), timerName_(name), expire_(expire), mHandle_(mHandle) {}; +public: + void *userData_; + bool isTrigger = false; + std::string timerName_; + time_t expire_; + TimeoutHandle mHandle_;; }; -class DmTimer { +class TimeHeap { public: - explicit DmTimer(const std::string &name); - ~DmTimer(); + TimeHeap(); + ~TimeHeap(); + /** + * @tc.name: TimeHeap::AddTimer + * @tc.desc: Add timer to time heap + * @tc.type: FUNC + */ + int32_t AddTimer(std::string name, int timeout, TimeoutHandle mHandle, void *user); /** - * @tc.name: DmTimer::Start - * @tc.desc: Start of the DmTimer + * @tc.name: TimeHeap::DelTimer + * @tc.desc: Delete timer of the time heap * @tc.type: FUNC */ - DmTimerStatus Start(uint32_t timeOut, TimeoutHandle handle, void *data); + int32_t DelTimer(std::string name); /** - * @tc.name: DmTimer::Stop - * @tc.desc: Stop of the DmTimer + * @tc.name: TimeHeap::DelAll + * @tc.desc: Delete all timer of the time heap * @tc.type: FUNC */ - void Stop(int32_t code); + int32_t DelAll(); +private: /** - * @tc.name: DmTimer::WaitForTimeout - * @tc.desc: WaitFor Timeout of the DmTimer + * @tc.name: TimeHeap::Run + * @tc.desc: timer wait for timeout * @tc.type: FUNC */ - void WaitForTimeout(); + void Run(); /** - * @tc.name: DmTimer::GetTimerName - * @tc.desc: Get TimerName of the DmTimer + * @tc.name: TimeHeap::Run + * @tc.desc: timerout event triggering * @tc.type: FUNC */ - std::string GetTimerName(); -private: - int32_t CreateTimeFd(); - void Release(); + int32_t Tick(); + /** + * @tc.name: TimeHeap::Run + * @tc.desc: sort the time heap + * @tc.type: FUNC + */ + int32_t MoveUp(std::shared_ptr timer); private: - DmTimerStatus mStatus_; - uint32_t mTimeOutSec_; - TimeoutHandle mHandle_; - void *mHandleData_; - int32_t mTimeFd_[2]; -#if defined(__LITEOS_M__) - void *timerId = NULL; -#else - struct epoll_event mEv_; - struct epoll_event mEvents_[MAX_EVENTS]; - int32_t mEpFd_; + int32_t hsize_ = 0; + int32_t epollFd_; + int32_t pipefd[2]; std::thread mThread_; - std::mutex mTimerLock_; -#endif - - std::string mTimerName_; + std::vector> minHeap_; }; -} // namespace DistributedHardware -} // namespace OHOS -#endif // TIMER_H +} +} +#endif \ No newline at end of file diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h index 2cf315b82a7b71c96de61f9497b49b415a9d9b18..95a59c5116816a0701173d3b1f07ae1e60c06a81 100755 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -36,7 +36,6 @@ struct StateTimerInfo { std::string timerName; std::string netWorkId; std::string deviceId; - std::shared_ptr timer; }; class DmDeviceStateManager final : public ISoftbusStateCallback, @@ -164,6 +163,7 @@ private: std::map remoteDeviceInfos_; std::map decisionInfos_; std::map stateTimerInfoMap_; + std::shared_ptr timerHeap_; std::shared_ptr hiChainConnector_; std::string decisionSoName_; }; diff --git a/services/devicemanagerservice/include/discovery/dm_discovery_manager.h b/services/devicemanagerservice/include/discovery/dm_discovery_manager.h index db0e97d2848cbf9cd8cc7d1cc5d44162a5f6ec9f..00e756b360cbfc9d466d735e96731bd2a8dc19ec 100644 --- a/services/devicemanagerservice/include/discovery/dm_discovery_manager.h +++ b/services/devicemanagerservice/include/discovery/dm_discovery_manager.h @@ -84,7 +84,7 @@ private: std::shared_ptr listener_; std::queue discoveryQueue_; std::map discoveryContextMap_; - std::shared_ptr discoveryTimer_; + std::shared_ptr timerHeap_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp index 86d31b3e77b26b988401fe8d8474982760a9e820..79f9153a9e5923fb6011df022d5a847fe61a3c32 100644 --- a/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp +++ b/services/devicemanagerservice/src/authentication/dm_auth_manager.cpp @@ -30,7 +30,6 @@ namespace OHOS { namespace DistributedHardware { -const int32_t SESSION_CANCEL_TIMEOUT = 0; const int32_t AUTHENTICATE_TIMEOUT = 120; const int32_t CONFIRM_TIMEOUT = 60; const int32_t NEGOTIATE_TIMEOUT = 10; @@ -41,10 +40,10 @@ const int32_t WAIT_REQUEST_TIMEOUT = 10; const int32_t CANCEL_PIN_CODE_DISPLAY = 1; const int32_t DEVICE_ID_HALF = 2; -static void TimeOut(void *data, DmTimer& timer) +static void TimeOut(void *data, std::string timerName) { - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGI("time out %s", timerName.c_str()); + if (data == nullptr || timerName.find(TIMER_PREFIX) != TIMER_DEFAULT) { LOGE("time out is not our timer"); return; } @@ -131,9 +130,10 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au authRequestState_->SetAuthManager(shared_from_this()); authRequestState_->SetAuthContext(authRequestContext_); authRequestState_->Enter(); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; - authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this); + if (timerHeap_ == nullptr) { + timerHeap_ = std::make_shared(); + } + timerHeap_->AddTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, TimeOut, this); LOGI("DmAuthManager::AuthenticateDevice complete"); return DM_OK; } @@ -174,12 +174,11 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam) return DM_AUTH_NOT_START; } std::shared_ptr ptr; - if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end() - || timerMap_.find(INPUT_TIMEOUT_TASK) == timerMap_.end()) { + if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) { LOGE("DmAuthManager::authenticationMap_ is null"); return DM_FAILED; } - timerMap_[INPUT_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(INPUT_TIMEOUT_TASK); ptr = authenticationMap_[authResponseContext_->authType]; int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam); switch (ret) { @@ -210,12 +209,9 @@ void DmAuthManager::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int3 authResponseState_->SetAuthManager(shared_from_this()); authResponseState_->Enter(); authResponseContext_ = std::make_shared(); - std::shared_ptr waitStartTimer = std::make_shared(WAIT_NEGOTIATE_TIMEOUT_TASK); - timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] = waitStartTimer; - waitStartTimer->Start(WAIT_NEGOTIATE_TIMEOUT, TimeOut, this); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; - authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this); + timerHeap_ = std::make_shared(); + timerHeap_->AddTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(WAIT_NEGOTIATE_TIMEOUT_TASK, WAIT_NEGOTIATE_TIMEOUT, TimeOut, this); } else { std::shared_ptr authMessageProcessor = std::make_shared(shared_from_this()); @@ -294,18 +290,16 @@ void DmAuthManager::OnDataReceived(const int32_t sessionId, const std::string me switch (authResponseContext_->msgType) { case MSG_TYPE_NEGOTIATE: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT - && timerMap_.find(WAIT_NEGOTIATE_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT) { + timerHeap_->DelTimer(WAIT_NEGOTIATE_TIMEOUT_TASK); authResponseState_->TransitionTo(std::make_shared()); } else { LOGE("Device manager auth state error"); } break; case MSG_TYPE_REQ_AUTH: - if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE - && timerMap_.find(WAIT_REQUEST_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[WAIT_REQUEST_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE) { + timerHeap_->DelTimer(WAIT_REQUEST_TIMEOUT_TASK); authResponseState_->TransitionTo(std::make_shared()); } else { LOGE("Device manager auth state error"); @@ -359,8 +353,8 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status) { LOGI("DmAuthManager OnMemberJoin start"); - if (authRequestState_ != nullptr && timerMap_.find(ADD_TIMEOUT_TASK) != timerMap_.end()) { - timerMap_[ADD_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + if (authRequestState_ != nullptr) { + timerHeap_->DelTimer(ADD_TIMEOUT_TASK); if (status != DM_OK || authResponseContext_->requestId != requestId) { if (authRequestState_ != nullptr) { authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN; @@ -419,9 +413,7 @@ void DmAuthManager::StartNegotiate(const int32_t &sessionId) authMessageProcessor_->SetResponseContext(authResponseContext_); std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_NEGOTIATE); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); - std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); - timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; - negotiateStartTimer->Start(NEGOTIATE_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(NEGOTIATE_TIMEOUT_TASK, NEGOTIATE_TIMEOUT, TimeOut, this); } void DmAuthManager::RespNegotiate(const int32_t &sessionId) @@ -460,18 +452,13 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId) jsonObject[TAG_CRYPTO_SUPPORT] = "false"; message = jsonObject.dump(); softbusConnector_->GetSoftbusSession()->SendData(sessionId, message); - std::shared_ptr waitStartTimer = std::make_shared(WAIT_REQUEST_TIMEOUT_TASK); - timerMap_[WAIT_REQUEST_TIMEOUT_TASK] = waitStartTimer; - waitStartTimer->Start(WAIT_REQUEST_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(WAIT_REQUEST_TIMEOUT_TASK, WAIT_REQUEST_TIMEOUT, TimeOut, this); } void DmAuthManager::SendAuthRequest(const int32_t &sessionId) { LOGI("DmAuthManager::EstablishAuthChannel session id"); - if (timerMap_.find(NEGOTIATE_TIMEOUT_TASK) == timerMap_.end()) { - return; - } - timerMap_[NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(NEGOTIATE_TIMEOUT_TASK); if (authResponseContext_->cryptoSupport) { isCryptoSupport_ = true; } @@ -483,9 +470,7 @@ void DmAuthManager::SendAuthRequest(const int32_t &sessionId) for (auto msg : messageList) { softbusConnector_->GetSoftbusSession()->SendData(sessionId, msg); } - std::shared_ptr confirmStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); - timerMap_[CONFIRM_TIMEOUT_TASK] = confirmStartTimer; - confirmStartTimer->Start(CONFIRM_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(CONFIRM_TIMEOUT_TASK, CONFIRM_TIMEOUT, TimeOut, this); } int32_t DmAuthManager::StartAuthProcess(const int32_t &action) @@ -511,15 +496,9 @@ int32_t DmAuthManager::StartAuthProcess(const int32_t &action) void DmAuthManager::StartRespAuthProcess() { LOGI("DmAuthManager::StartRespAuthProcess", authResponseContext_->sessionId); - if (timerMap_.find(CONFIRM_TIMEOUT_TASK) == timerMap_.end()) { - return; - } - - timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(CONFIRM_TIMEOUT_TASK); if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH) { - std::shared_ptr inputStartTimer = std::make_shared(INPUT_TIMEOUT_TASK); - timerMap_[INPUT_TIMEOUT_TASK] = inputStartTimer; - inputStartTimer->Start(INPUT_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(INPUT_TIMEOUT_TASK, INPUT_TIMEOUT, TimeOut, this); authRequestState_->TransitionTo(std::make_shared()); } else { LOGE("do not accept"); @@ -555,9 +534,7 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId) jsonObject[TAG_REQUEST_ID] = authResponseContext_->requestId; jsonObject[TAG_DEVICE_ID] = authResponseContext_->deviceId; std::string connectInfo = jsonObject.dump(); - std::shared_ptr joinStartTimer = std::make_shared(ADD_TIMEOUT_TASK); - timerMap_[ADD_TIMEOUT_TASK] = joinStartTimer; - joinStartTimer->Start(ADD_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(ADD_TIMEOUT_TASK, ADD_TIMEOUT, TimeOut, this); int32_t ret = hiChainConnector_->AddMember(deviceId, connectInfo); if (ret != 0) { return DM_FAILED; @@ -584,10 +561,7 @@ std::string DmAuthManager::GetConnectAddr(std::string deviceId) int32_t DmAuthManager::JoinNetwork() { LOGI("DmAuthManager JoinNetwork start"); - if (timerMap_.find(AUTHENTICATE_TIMEOUT_TASK) == timerMap_.end()) { - return DM_FAILED; - } - timerMap_[AUTHENTICATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(AUTHENTICATE_TIMEOUT_TASK); authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH; authRequestContext_->reason = DM_OK; authRequestState_->TransitionTo(std::make_shared()); @@ -612,12 +586,7 @@ void DmAuthManager::AuthenticateFinish() std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_AUTH_TERMINATE); softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message); } - if (!timerMap_.empty()) { - for (auto &iter : timerMap_) { - iter.second->Stop(SESSION_CANCEL_TIMEOUT); - } - timerMap_.clear(); - } + timerHeap_->DelAll(); isFinishOfLocal_ = true; authResponseContext_ = nullptr; authResponseState_ = nullptr; @@ -642,12 +611,7 @@ void DmAuthManager::AuthenticateFinish() listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId, authRequestContext_->token, authResponseContext_->state, authRequestContext_->reason); softbusConnector_->GetSoftbusSession()->CloseAuthSession(authRequestContext_->sessionId); - if (!timerMap_.empty()) { - for (auto &iter : timerMap_) { - iter.second->Stop(SESSION_CANCEL_TIMEOUT); - } - timerMap_.clear(); - } + timerHeap_->DelAll(); isFinishOfLocal_ = true; authRequestContext_ = nullptr; authResponseContext_ = nullptr; diff --git a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp index d103e8822cd615fae270f3e3b695a81e84b96647..b8b0c8c6be4ac75fe80fba970cefd44a4af4d142 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -13,177 +13,186 @@ * limitations under the License. */ -#include "dm_timer.h" - -#include - #include "dm_constants.h" -#include "securec.h" +#include "dm_log.h" +#include "dm_timer.h" namespace OHOS { namespace DistributedHardware { -namespace { -const int32_t MILL_SECONDS_PER_SECOND = 1000; -} - -DmTimer::DmTimer(const std::string &name) +int32_t TimeHeap::Tick() { - if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer name is null"); - mTimerName_ = ""; - return; - } - - mStatus_ = DmTimerStatus::DM_STATUS_INIT; - mTimeOutSec_ = 0; - mHandle_ = nullptr; - mHandleData_ = nullptr; - (void)memset_s(mTimeFd_, sizeof(mTimeFd_), 0, sizeof(mTimeFd_)); - (void)memset_s(&mEv_, sizeof(mEv_), 0, sizeof(mEv_)); - (void)memset_s(mEvents_, sizeof(mEvents_), 0, sizeof(mEvents_)); - mEpFd_ = 0; - mTimerName_ = name; -} - -DmTimer::~DmTimer() -{ - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; + LOGI("Tick start"); + if (hsize_ == 0) { + LOGE("Timer count is 0"); + return DM_AUTH_NO_TIMER; } - LOGI("DmTimer %s destroy in", mTimerName_.c_str()); - Stop(0); - std::lock_guard lock(mTimerLock_); - Release(); + std::shared_ptr top = minHeap_.front(); + top->isTrigger = true; + do { + top->mHandle_(top->userData_, top->timerName_); + DelTimer(top->timerName_); + + if (hsize_ > 0) { + top = minHeap_.front(); + } else { + break; + } + } while (top->expire_ <= time(NULL)); + + return DM_OK; } -DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) +int32_t TimeHeap::MoveUp(std::shared_ptr timer) { - 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("MoveUp timer"); + if (timer == nullptr) { + LOGE("MoveUp timer is null"); + return DM_INVALID_VALUE; } - LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut); - if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) { - return DmTimerStatus::DM_STATUS_BUSY; + if (hsize_ == 0) { + LOGE("Add timer failed"); + return DM_INVALID_VALUE; } - mTimeOutSec_ = timeOut; - mHandle_ = handle; - mHandleData_ = data; - - if (CreateTimeFd()) { - return DmTimerStatus::DM_STATUS_CREATE_ERROR; + for (int32_t i = 1;; i++) { + LOGE("MoveUp 1 = %d, h = %d", i, hsize_); + if (i == hsize_) { + minHeap_.insert(minHeap_.begin() + (i - 1), timer); + break; + } + if (timer->expire_ < minHeap_[i - 1]->expire_) { + minHeap_.insert(minHeap_.begin() + (i -1), timer); + break; + } } - - mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; - mThread_ = std::thread(&DmTimer::WaitForTimeout, this); - mThread_.detach(); - return mStatus_; + return DM_OK; } -void DmTimer::Stop(int32_t code) +void TimeHeap::Run() { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT || mHandleData_ == nullptr) { - LOGE("DmTimer is not init"); - return; - } + epoll_event event; + event.data.fd = pipefd[0]; + event.events = EPOLLIN | EPOLLET; + epoll_ctl(epollFd_, EPOLL_CTL_ADD, pipefd[0], &event); + epoll_event events[MAX_EVENT_NUMBER]; + bool stop = false; + int timeout = NO_TIMER; + + while (!stop) { + int number = epoll_wait(epollFd_, events, MAX_EVENT_NUMBER, timeout); + + LOGI("RunTimer is doing"); + if (number < 0) { + LOGE("DmTimer %s epoll_wait error: %d", minHeap_.front()->timerName_.c_str(), errno); + DelTimer(minHeap_.front()->timerName_); + } + if (!number) { + Tick(); + } else { + int buffer = 0; + recv(pipefd[0], (char*)&buffer, sizeof(buffer), 0); + } - if (mTimeFd_[1]) { - char event = 'S'; - if (write(mTimeFd_[1], &event, 1) < 0) { - return; + if (hsize_ == 0) { + break; + } else { + timeout = (minHeap_.front()->expire_ - time(NULL)) * SEC_TO_MM; } } } -void DmTimer::WaitForTimeout() +TimeHeap::TimeHeap(): epollFd_(epoll_create(MAX_EVENTS)) { - 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_); - - std::lock_guard lock(mTimerLock_); - int32_t 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); - LOGD("DmTimer %s exit with num=%d, event=%d, errno=%d", mTimerName_.c_str(), num, event, errno); - } - } else if (nfds == 0) { - if (mHandleData_ != nullptr) { - mHandle_(mHandleData_, *this); - LOGI("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); - } - } else { - LOGI("DmTimer %s epoll_wait return nfds=%d, errno=%d", mTimerName_.c_str(), nfds, errno); + minHeap_.resize(INIT_SIZE); + + int ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd); + if (ret != 0) { + LOGE("open pipe failed"); } - Release(); + assert(ret == 0); +} + +TimeHeap::~TimeHeap() +{ + DelAll(); + close(epollFd_); } -int32_t DmTimer::CreateTimeFd() +int32_t TimeHeap::AddTimer(std::string name, int timeout, TimeoutHandle mHandle, void *user) { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return DM_STATUS_FINISH; + if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer name is not DM timer"); + return DM_INVALID_VALUE; } - 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); - return ret; + LOGI("AddTimer %s", name.c_str()); + if (timeout <= 0 || mHandle == nullptr || user == nullptr) { + LOGE("DmTimer %s invalid value", name.c_str()); + return DM_INVALID_VALUE; } - std::lock_guard lock(mTimerLock_); - mEv_.data.fd = mTimeFd_[0]; - mEv_.events = EPOLLIN | EPOLLET; - mEpFd_ = epoll_create(MAX_EVENTS); - ret = epoll_ctl(mEpFd_, EPOLL_CTL_ADD, mTimeFd_[0], &mEv_); - if (ret != 0) { - Release(); + if (hsize_ == 0) { + mThread_ = std::thread(&TimeHeap::Run, this); + mThread_.detach(); + } + if (hsize_ == (int32_t)(minHeap_.size() - 1)) { + minHeap_.resize(EXPAND_TWICE * minHeap_.size()); } - return ret; + + std::shared_ptr timer = std::make_shared(name, timeout + time(NULL), user, mHandle); + ++hsize_; + MoveUp(timer); + if (timer == minHeap_.front()) { + char msg = 1; + send(pipefd[1], (char*)&msg, sizeof(msg), 0); + } + LOGE("AddTimer %s complete", name.c_str()); + return DM_OK; } -void DmTimer::Release() +int32_t TimeHeap::DelTimer(std::string name) { - if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) { - LOGE("DmTimer is not init"); - return; - } - LOGI("DmTimer %s release in", mTimerName_.c_str()); - - if (mStatus_ == DmTimerStatus::DM_STATUS_INIT) { - 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_); - } - mTimerName_ = ""; - mTimeOutSec_ = 0; - mHandle_ = nullptr; - mHandleData_ = nullptr; - mTimeFd_[0] = 0; - mTimeFd_[1] = 0; - mEpFd_ = 0; + if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGE("DmTimer name is not DM timer"); + return DM_INVALID_VALUE; + } + + LOGI("DelTimer %s", name.c_str()); + int32_t location = 0; + bool have = false; + for (int32_t i = 0; i < hsize_; i++) { + if (minHeap_[i]->timerName_ == name) { + location = i; + have = true; + break; + } + } + + if (!have) { + LOGE("heap is not have this %s", name.c_str()); + return DM_INVALID_VALUE; + } + + if (minHeap_[location] == minHeap_.front() && minHeap_[location]->isTrigger == false) { + char msg = 1; + send(pipefd[1], &msg, sizeof(msg), 0); + } + minHeap_.erase(minHeap_.begin() + location); + hsize_--; + LOGI("DelTimer %s complete , timer count %d", name.c_str(), hsize_); + return DM_OK; } -std::string DmTimer::GetTimerName() +int32_t TimeHeap::DelAll() { - return mTimerName_; + LOGI("DelAll start"); + for (int32_t i = hsize_ ; i > 0; i--) { + DelTimer(minHeap_[i - 1]->timerName_); + } + LOGI("DelAll complete"); + return DM_OK; +} } -} // namespace DistributedHardware -} // namespace OHOS +} \ No newline at end of file diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index e8d0d95ad7cf7938b62951855ba20d76eed7e316..6701c6e9b3abb30125ae0aae667e25eade68a179 100755 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -22,12 +22,10 @@ namespace OHOS { namespace DistributedHardware { -const int32_t SESSION_CANCEL_TIMEOUT = 0; - -static void TimeOut(void *data, DmTimer& timer) +static void TimeOut(void *data, std::string timerName) { - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) { + LOGI("time out %s", timerName.c_str()); + if (data == nullptr || timerName.find(TIMER_PREFIX) != TIMER_DEFAULT) { LOGE("time out is not our timer"); return; } @@ -38,7 +36,7 @@ static void TimeOut(void *data, DmTimer& timer) return; } - deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName()); + deviceStateMgr->DeleteTimeOutGroup(timerName); } DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr softbusConnector, @@ -278,22 +276,21 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) #endif for (auto &iter : stateTimerInfoMap_) { if (iter.second.netWorkId == deviceInfo.deviceId) { - iter.second.timer->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(iter.second.timerName); return; } } - std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(cumulativeQuantity_++); - std::shared_ptr offLineTimer = std::make_shared(timerName); - if (offLineTimer != nullptr) { - StateTimerInfo stateTimer = { - .timerName = timerName, - .netWorkId = deviceInfo.deviceId, - .deviceId = deviceId, - .timer = offLineTimer - }; - stateTimerInfoMap_[timerName] = stateTimer; + if (timerHeap_ == nullptr) { + timerHeap_ = std::make_shared(); } + std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(cumulativeQuantity_++); + StateTimerInfo stateTimer = { + .timerName = timerName, + .netWorkId = deviceInfo.deviceId, + .deviceId = deviceId, + }; + stateTimerInfoMap_[timerName] = stateTimer; } void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) @@ -306,7 +303,7 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) LOGI("start offline timer"); for (auto &iter : stateTimerInfoMap_) { if (iter.second.netWorkId == deviceInfo.deviceId) { - iter.second.timer->Start(OFFLINE_TIMEOUT, TimeOut, this); + timerHeap_->AddTimer(iter.second.timerName, OFFLINE_TIMEOUT, TimeOut, this); } } } diff --git a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp index bc8f80ad314c1c91a29898b4bdfa953c0f57d536..ec0cf6383517a148b4a72637abe91c47189bf9c0 100644 --- a/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp +++ b/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp @@ -23,12 +23,11 @@ namespace OHOS { namespace DistributedHardware { 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) +static void TimeOut(void *data, std::string timerName) { - LOGI("time out %s", timer.GetTimerName().c_str()); - if (data == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) { + LOGI("time out %s", timerName.c_str()); + if (data == nullptr || timerName != DISCOVERY_TIMEOUT_TASK) { LOGE("time out is not our timer"); return; } @@ -72,8 +71,10 @@ int32_t DmDiscoveryManager::StartDeviceDiscovery(const std::string &pkgName, con discoveryContextMap_.emplace(pkgName, context); softbusConnector_->RegisterSoftbusDiscoveryCallback(pkgName, std::shared_ptr(shared_from_this())); - discoveryTimer_ = std::make_shared(DISCOVERY_TIMEOUT_TASK); - discoveryTimer_->Start(DISCOVERY_TIMEOUT, TimeOut, this); + if (timerHeap_ == nullptr) { + timerHeap_ = std::make_shared(); + } + timerHeap_->AddTimer(DISCOVERY_TIMEOUT_TASK, DISCOVERY_TIMEOUT, TimeOut, this); return softbusConnector_->StartDiscovery(subscribeInfo); } @@ -85,7 +86,7 @@ int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint if (!discoveryContextMap_.empty()) { discoveryContextMap_.erase(pkgName); softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName); - discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT); + timerHeap_->DelTimer(DISCOVERY_TIMEOUT_TASK); } return softbusConnector_->StopDiscovery(subscribeId); } diff --git a/test/unittest/UTTest_auth_request_state.cpp b/test/unittest/UTTest_auth_request_state.cpp index 8b22a6c9f73323392512133c5c79f607e85d5413..4669afacd697e230bb56346ba05d2d4b5f527e5a 100644 --- a/test/unittest/UTTest_auth_request_state.cpp +++ b/test/unittest/UTTest_auth_request_state.cpp @@ -35,9 +35,6 @@ void AuthRequestStateTest::TearDownTestCase() } namespace { -std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask"; -std::string ADD_TIMEOUT_TASK = "addTimeoutTask"; - std::shared_ptr softbusConnector = std::make_shared(); std::shared_ptr listener = std::make_shared(); std::shared_ptr hiChainConnector = std::make_shared(); @@ -184,8 +181,7 @@ HWTEST_F(AuthRequestStateTest, TransitionTo_002, testing::ext::TestSize.Level0) std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr context = std::make_shared(); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); - authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; + authManager->timerHeap_ = std::make_shared(); authManager->authRequestState_ = std::make_shared(); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -310,6 +306,7 @@ HWTEST_F(AuthRequestStateTest, Enter_004, testing::ext::TestSize.Level0) authManager->authRequestContext_->deviceId = "111"; authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_); + authManager->timerHeap_ = std::make_shared(); authRequestState->SetAuthManager(authManager); std::shared_ptr context = std::make_shared(); context->deviceId = "123456"; @@ -364,8 +361,7 @@ HWTEST_F(AuthRequestStateTest, Enter_006, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); - authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; + authManager->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -426,8 +422,7 @@ HWTEST_F(AuthRequestStateTest, Enter_008, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr inputStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); - authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer; + authManager->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -605,8 +600,7 @@ HWTEST_F(AuthRequestStateTest, Enter_014, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; + authManager->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); @@ -665,8 +659,7 @@ HWTEST_F(AuthRequestStateTest, Enter_016, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr inputStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); - authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer; + authManager->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); diff --git a/test/unittest/UTTest_auth_response_state.cpp b/test/unittest/UTTest_auth_response_state.cpp index fc28c482191cdfc5d7b9a9b3b96df917762b3853..1e85f56adce0b70ac44e0a598aa2d7731c899608 100644 --- a/test/unittest/UTTest_auth_response_state.cpp +++ b/test/unittest/UTTest_auth_response_state.cpp @@ -238,6 +238,7 @@ HWTEST_F(AuthResponseStateTest, Enter_003, testing::ext::TestSize.Level0) authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_); authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); authManager->softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authManager); + authManager->timerHeap_ = std::make_shared(); authResponseState->SetAuthManager(authManager); std::shared_ptr context = std::make_shared(); context->deviceId = "123456"; diff --git a/test/unittest/UTTest_dm_auth_manager.cpp b/test/unittest/UTTest_dm_auth_manager.cpp index 4250b6f275b0c9dbe62de7c8e1a6f6ba3496c4b6..2ef3244b358b60ddc3771d78969185d355da0fa2 100644 --- a/test/unittest/UTTest_dm_auth_manager.cpp +++ b/test/unittest/UTTest_dm_auth_manager.cpp @@ -225,8 +225,7 @@ HWTEST_F(DmAuthManagerTest, JoinNetwork_001, testing::ext::TestSize.Level0) std::shared_ptr authManager = std::make_shared(softbusConnector, listener, hiChainConnector_); std::shared_ptr authRequestState = std::make_shared(); - std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); - authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; + authManager->timerHeap_ = std::make_shared(); authManager->authMessageProcessor_ = std::make_shared(authManager); authManager->authResponseContext_ = std::make_shared(); authManager->authRequestContext_ = std::make_shared(); diff --git a/test/unittest/UTTest_dm_timer.cpp b/test/unittest/UTTest_dm_timer.cpp index affcd4655e5e30e4c4c546b198d43399dcd8e8b3..650d3f07d6a2455e60ba9c94c2f0e83aa34b6ddc 100644 --- a/test/unittest/UTTest_dm_timer.cpp +++ b/test/unittest/UTTest_dm_timer.cpp @@ -23,232 +23,316 @@ namespace OHOS { namespace DistributedHardware { -void DmTimerTest::SetUp() +void TimeHeapTest::SetUp() { } -void DmTimerTest::TearDown() +void TimeHeapTest::TearDown() { } -void DmTimerTest::SetUpTestCase() +void TimeHeapTest::SetUpTestCase() { } -void DmTimerTest::TearDownTestCase() +void TimeHeapTest::TearDownTestCase() { } namespace { +static void TimeOut(void *data, std::string timerName) {} + /** - * @tc.name: DmTimerTest::DmTimer_001 - * @tc.desc: to check when name is empty + * @tc.name: TimeHeapTest::Tick_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, DmTimer_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, Tick_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->hsize_ = 0; + int32_t ret = timerHeap->Tick(); + EXPECT_EQ(DM_AUTH_NO_TIMER, ret); } /** - * @tc.name: DmTimerTest::DmTimer_002 - * @tc.desc: to check when name is not empty + * @tc.name: TimeHeapTest::Tick_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, DmTimer_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, Tick_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "123"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - EXPECT_EQ(DmTimerStatus::DM_STATUS_INIT, Timer_->mStatus_); - EXPECT_EQ(0, Timer_->mTimeOutSec_); - EXPECT_EQ(nullptr, Timer_->mHandle_); - EXPECT_EQ(nullptr, Timer_->mHandleData_); - EXPECT_EQ(0, Timer_->mTimeFd_[1]); - EXPECT_EQ(0, Timer_->mEv_.events); - EXPECT_EQ(0, Timer_->mEvents_[0].events); - EXPECT_EQ(0, Timer_->mEpFd_); - EXPECT_EQ(DM_TIMER_TASK, Timer_->mTimerName_); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 10, TimeOut, timer); + int32_t ret = timerHeap->Tick(); + EXPECT_EQ(DM_OK, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Start_001 - * @tc.desc: to check when mTimerName_ is empty,handle is nullptr,data is nullptr + * @tc.name: TimeHeapTest::Tick_003 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Start_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, Tick_003, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - uint32_t timeOut = 10; - TimeoutHandle handle = nullptr; - void *data = nullptr; - DmTimerStatus timerStatus = Timer_->Start(timeOut, handle, data); - EXPECT_EQ(DM_STATUS_FINISH, timerStatus); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 10, TimeOut, timer); + timerHeap->AddTimer(AUTHENTICATE_TIMEOUT_TASK, 20, TimeOut, timer); + int32_t ret = timerHeap->Tick(); + EXPECT_EQ(DM_OK, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Start_002 - * @tc.desc: to check when mTimerName_ is not empty, handle is not nullptr, data is not nullptr, - * @mStatus_ != DmTimerStatus::DM_STATUS_INIT + * @tc.name: TimeHeapTest::MoveUp_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -static void TimeOutTest(void *data, DmTimer& timer) +HWTEST_F(TimeHeapTest, MoveUp_001, testing::ext::TestSize.Level0) { - LOGE("time out test"); + std::shared_ptr timerHeap = std::make_shared(); + std::shared_ptr timer = nullptr; + int32_t ret = timerHeap->MoveUp(timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } -HWTEST_F(DmTimerTest, Start_002, testing::ext::TestSize.Level0) +/** + * @tc.name: TimeHeapTest::MoveUp_002 + * @tc.desc: Timeout event trigger + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(TimeHeapTest, MoveUp_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "1234"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - uint32_t timeOut = 10; - int idx = 1; - void *data = &idx; - Timer_->mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; - DmTimerStatus timerStatus = Timer_->Start(timeOut, TimeOutTest, data); - EXPECT_EQ(DmTimerStatus::DM_STATUS_BUSY, timerStatus); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + std::shared_ptr timer = std::make_shared(name, timeout + time(NULL), nullptr, TimeOut); + timerHeap->hsize_ = 0; + int32_t ret = timerHeap->MoveUp(timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Stop_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::MoveUp_003 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Stop_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, MoveUp_003, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t code = 1; - Timer_->Stop(code); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + std::shared_ptr timer = std::make_shared(name, timeout + time(NULL), nullptr, TimeOut); + timerHeap->hsize_ = 1; + int32_t ret = timerHeap->MoveUp(timer); + EXPECT_EQ(DM_OK, ret); + + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Stop_002 - * @tc.desc: to check when mTimerName_ is not empty + * @tc.name: TimeHeapTest::AddTimer_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Stop_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t code = 1; - Timer_->Stop(code); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_OK, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::Stop_003 - * @tc.desc: to check when mTimerName_ is not empty,mTimeFd_[1] is not 0 + * @tc.name: TimeHeapTest::AddTimer_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Stop_003, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t code = 1; - Timer_->mTimeFd_[1] = 1; - Timer_->Stop(code); + std::string name = ""; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::WaitForTimeout_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::AddTimer_003 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, WaitForTimeout_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_003, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->WaitForTimeout(); + std::string name = "timer"; + int32_t timeout = 10; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::WaitForTimeout_002 - * @tc.desc: to check when mTimerName_ is not empty + * @tc.name: TimeHeapTest::AddTimer_004 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, WaitForTimeout_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_004, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->WaitForTimeout(); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = -1; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::CreateTimeFd_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::AddTimer_005 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, CreateTimeFd_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, AddTimer_005, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - int32_t result = Timer_->CreateTimeFd(); - EXPECT_EQ(DM_STATUS_FINISH, result); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = -1; + + std::shared_ptr timerHeap = std::make_shared(); + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + int32_t ret = timerHeap->AddTimer(name, timeout, TimeOut, timer); + EXPECT_EQ(DM_INVALID_VALUE, ret); + timerHeap->DelAll(); } /** - * @tc.name: DmTimerTest::CreateTimeFd_002 - * @tc.desc: to check when mTimerName_ is not empty + * @tc.name: TimeHeapTest::DelTimer_001 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, CreateTimeFd_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_001, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "123"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->CreateTimeFd(); + std::string name = ""; + + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_INVALID_VALUE, ret); } /** - * @tc.name: DmTimerTest::Release_001 - * @tc.desc: to check when mTimerName_ is empty + * @tc.name: TimeHeapTest::DelTimer_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Release_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = ""; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->Release(); + std::string name = "timer"; + + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_INVALID_VALUE, ret); } /** - * @tc.name: DmTimerTest::Release_002 - * @tc.desc: to check when mTimerName_ is not empty,mStatus_ is 0 + * @tc.name: TimeHeapTest::DelTimer_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, Release_002, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelTimer_003, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - Timer_->mStatus_ = DmTimerStatus::DM_STATUS_INIT; - Timer_->Release(); + std::string name = AUTHENTICATE_TIMEOUT_TASK; + + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_INVALID_VALUE, ret); +} + +/** + * @tc.name: TimeHeapTest::DelTimer_004 + * @tc.desc: Timeout event trigger + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(TimeHeapTest, DelTimer_004, testing::ext::TestSize.Level0) +{ + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->AddTimer(name, timeout, TimeOut, timer); + int32_t ret = timerHeap->DelTimer(name); + EXPECT_EQ(DM_OK, ret); +} + +/** + * @tc.name: TimeHeapTest::DelAll_001 + * @tc.desc: Timeout event trigger + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(TimeHeapTest, DelAll_001, testing::ext::TestSize.Level0) +{ + std::string name = AUTHENTICATE_TIMEOUT_TASK; + int32_t timeout = 10; + + DmTimer *timer = new DmTimer(name, timeout + time(NULL), nullptr, TimeOut); + std::shared_ptr timerHeap = std::make_shared(); + timerHeap->AddTimer(name, timeout, TimeOut, timer); + int32_t ret = timerHeap->DelAll(); + EXPECT_EQ(DM_OK, ret); + EXPECT_EQ(timerHeap->hsize_, 0); } /** - * @tc.name: DmTimerTest::GetTimerName_001 - * @tc.desc: to check whether the return value is the same as mTimerName_ + * @tc.name: TimeHeapTest::DelAll_002 + * @tc.desc: Timeout event trigger * @tc.type: FUNC * @tc.require: AR000GHSJK */ -HWTEST_F(DmTimerTest, GetTimerName_001, testing::ext::TestSize.Level0) +HWTEST_F(TimeHeapTest, DelAll_002, testing::ext::TestSize.Level0) { - const std::string DM_TIMER_TASK = TIMER_PREFIX + "111"; - std::shared_ptr Timer_ = std::make_shared(DM_TIMER_TASK); - std::string strTimer = Timer_->GetTimerName(); - EXPECT_EQ(DM_TIMER_TASK, strTimer); + std::shared_ptr timerHeap = std::make_shared(); + int32_t ret = timerHeap->DelAll(); + EXPECT_EQ(DM_OK, ret); } } } diff --git a/test/unittest/UTTest_dm_timer.h b/test/unittest/UTTest_dm_timer.h index c697c779f4115ecb0f6b358b2cfd5d68a2018140..6f8302e20893e5294a7c3c486a0c198eb24cdc0c 100644 --- a/test/unittest/UTTest_dm_timer.h +++ b/test/unittest/UTTest_dm_timer.h @@ -14,12 +14,12 @@ */ #ifndef OHOS_UTTEST_DM_TIMER_H #define OHOS_UTTEST_DM_TIMER_H - +#define private public #include namespace OHOS { namespace DistributedHardware { -class DmTimerTest : public testing::Test { +class TimeHeapTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase();