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
28namespace OHOS {
29namespace DistributedHardware {
30constexpr const char* AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate";
31constexpr const char* NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate";
32constexpr const char* CONFIRM_TIMEOUT_TASK = "deviceManagerTimer:confirm";
33constexpr const char* INPUT_TIMEOUT_TASK = "deviceManagerTimer:input";
34constexpr const char* ADD_TIMEOUT_TASK = "deviceManagerTimer:add";
35constexpr const char* WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate";
36constexpr const char* WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest";
37constexpr const char* STATE_TIMER_PREFIX = "deviceManagerTimer:stateTimer_";
38constexpr const char* AUTH_DEVICE_TIMEOUT_TASK = "deviceManagerTimer:authDevice_";
39constexpr const char* SESSION_HEARTBEAT_TIMEOUT_TASK = "deviceManagerTimer:sessionHeartbeat";
40
41
42constexpr int32_t DELAY_TICK_MILLSECONDS = 1000;
43typedef std::chrono::steady_clock::time_point timerPoint;
44typedef std::chrono::steady_clock steadyClock;
45typedef std::chrono::duration<int64_t, std::ratio<1, 1000>> timerDuration;
46using TimerCallback = std::function<void (std::string name)>;
47const int32_t MILLISECOND_TO_SECOND = 1000;
48const int32_t MIN_TIME_OUT = 0;
49const int32_t MAX_TIME_OUT = 300;
50
51class Timer {
52public:
53    Timer(std::string name, int32_t time, TimerCallback callback);
54    ~Timer() {};
55public:
56    std::string timerName_;
57    timerPoint expire_;
58    bool state_;
59    int32_t timeOut_;
60    TimerCallback callback_;
61};
62
63struct TimerCmpare {
64public:
65    bool operator () (std::shared_ptr<Timer> frontTimer, std::shared_ptr<Timer> timer)
66    {
67        int32_t frontTimerOut = frontTimer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now()
68            - frontTimer->expire_).count() / MILLISECOND_TO_SECOND);
69        int32_t timerOut = timer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now()
70            - timer->expire_).count() / MILLISECOND_TO_SECOND);
71        return frontTimerOut > timerOut;
72    }
73};
74
75class DmTimer {
76public:
77    DmTimer();
78    ~DmTimer();
79
80    /**
81     * @tc.name: DmTimer::StartTimer
82     * @tc.desc: start timer running
83     * @tc.type: FUNC
84     */
85    int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback);
86
87    /**
88     * @tc.name: DmTimer::DeleteTimer
89     * @tc.desc: delete timer
90     * @tc.type: FUNC
91     */
92    int32_t DeleteTimer(std::string timerName);
93
94    /**
95     * @tc.name: DmTimer::DeleteAll
96     * @tc.desc: delete all timer
97     * @tc.type: FUNC
98     */
99    int32_t DeleteAll();
100
101    /**
102     * @tc.name: DmTimer::TimerRunning
103     * @tc.desc: timer running
104     * @tc.type: FUNC
105     */
106    int32_t TimerRunning();
107
108private:
109    void DeleteVector(std::string name);
110
111private:
112    mutable std::mutex timerMutex_;
113    mutable std::mutex timerStateMutex_;
114    std::priority_queue<std::shared_ptr<Timer>, std::vector<std::shared_ptr<Timer>>, TimerCmpare> timerQueue_;
115    std::vector<std::shared_ptr<Timer>> timerVec_;
116    std::atomic<bool> timerState_ {false};
117    std::condition_variable runTimerCondition_;
118    std::condition_variable stopTimerCondition_;
119};
120}
121}
122#endif