1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DM_TIMER_H 17 #define DM_TIMER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <condition_variable> 22 #include <functional> 23 #include <map> 24 #include <mutex> 25 #include <queue> 26 #include <vector> 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 constexpr const char* AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate"; 31 constexpr const char* NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate"; 32 constexpr const char* CONFIRM_TIMEOUT_TASK = "deviceManagerTimer:confirm"; 33 constexpr const char* INPUT_TIMEOUT_TASK = "deviceManagerTimer:input"; 34 constexpr const char* ADD_TIMEOUT_TASK = "deviceManagerTimer:add"; 35 constexpr const char* WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate"; 36 constexpr const char* WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest"; 37 constexpr const char* STATE_TIMER_PREFIX = "deviceManagerTimer:stateTimer_"; 38 constexpr const char* AUTH_DEVICE_TIMEOUT_TASK = "deviceManagerTimer:authDevice_"; 39 constexpr const char* SYNC_DELETE_TIMEOUT_TASK = "deviceManagerTimer:syncDelete_"; 40 constexpr const char* SESSION_HEARTBEAT_TIMEOUT_TASK = "deviceManagerTimer:sessionHeartbeat"; 41 42 43 constexpr int32_t DELAY_TICK_MILLSECONDS = 1000; 44 typedef std::chrono::steady_clock::time_point timerPoint; 45 typedef std::chrono::steady_clock steadyClock; 46 typedef std::chrono::duration<int64_t, std::ratio<1, 1000>> timerDuration; 47 using TimerCallback = std::function<void (std::string name)>; 48 const int32_t MILLISECOND_TO_SECOND = 1000; 49 const int32_t MIN_TIME_OUT = 0; 50 const int32_t MAX_TIME_OUT = 300; 51 52 class Timer { 53 public: 54 Timer(std::string name, int32_t time, TimerCallback callback); ~Timer()55 ~Timer() {}; 56 public: 57 std::string timerName_; 58 timerPoint expire_; 59 bool state_; 60 int32_t timeOut_; 61 TimerCallback callback_; 62 }; 63 64 struct TimerCmpare { 65 public: operatorTimerCmpare66 bool operator () (std::shared_ptr<Timer> frontTimer, std::shared_ptr<Timer> timer) 67 { 68 int32_t frontTimerOut = frontTimer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now() 69 - frontTimer->expire_).count() / MILLISECOND_TO_SECOND); 70 int32_t timerOut = timer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now() 71 - timer->expire_).count() / MILLISECOND_TO_SECOND); 72 return frontTimerOut > timerOut; 73 } 74 }; 75 76 class DmTimer { 77 public: 78 DmTimer(); 79 ~DmTimer(); 80 81 /** 82 * @tc.name: DmTimer::StartTimer 83 * @tc.desc: start timer running 84 * @tc.type: FUNC 85 */ 86 int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback); 87 88 /** 89 * @tc.name: DmTimer::DeleteTimer 90 * @tc.desc: delete timer 91 * @tc.type: FUNC 92 */ 93 int32_t DeleteTimer(std::string timerName); 94 95 /** 96 * @tc.name: DmTimer::DeleteAll 97 * @tc.desc: delete all timer 98 * @tc.type: FUNC 99 */ 100 int32_t DeleteAll(); 101 102 /** 103 * @tc.name: DmTimer::TimerRunning 104 * @tc.desc: timer running 105 * @tc.type: FUNC 106 */ 107 int32_t TimerRunning(); 108 109 private: 110 void DeleteVector(std::string name); 111 112 private: 113 mutable std::mutex timerMutex_; 114 mutable std::mutex timerStateMutex_; 115 std::priority_queue<std::shared_ptr<Timer>, std::vector<std::shared_ptr<Timer>>, TimerCmpare> timerQueue_; 116 std::vector<std::shared_ptr<Timer>> timerVec_; 117 std::atomic<bool> timerState_ {false}; 118 std::condition_variable runTimerCondition_; 119 std::condition_variable stopTimerCondition_; 120 }; 121 } 122 } 123 #endif