1 /*
2  * Copyright (c) 2022-2024 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 <unordered_map>
26 
27 #include "ffrt.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 constexpr const char* AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate";
32 constexpr const char* NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate";
33 constexpr const char* CONFIRM_TIMEOUT_TASK = "deviceManagerTimer:confirm";
34 constexpr const char* INPUT_TIMEOUT_TASK = "deviceManagerTimer:input";
35 constexpr const char* ADD_TIMEOUT_TASK = "deviceManagerTimer:add";
36 constexpr const char* WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate";
37 constexpr const char* WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest";
38 constexpr const char* STATE_TIMER_PREFIX = "deviceManagerTimer:stateTimer_";
39 constexpr const char* AUTH_DEVICE_TIMEOUT_TASK = "deviceManagerTimer:authDevice_";
40 constexpr const char* SYNC_DELETE_TIMEOUT_TASK = "deviceManagerTimer:syncDelete_";
41 constexpr const char* SESSION_HEARTBEAT_TIMEOUT_TASK = "deviceManagerTimer:sessionHeartbeat";
42 
43 using TimerCallback = std::function<void (std::string name)>;
44 
45 class DmTimer {
46 public:
47     DmTimer();
48     ~DmTimer();
49 
50     /**
51      * @tc.name: DmTimer::StartTimer
52      * @tc.desc: start timer running
53      * @tc.type: FUNC
54      */
55     int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback);
56 
57     /**
58      * @tc.name: DmTimer::DeleteTimer
59      * @tc.desc: delete timer
60      * @tc.type: FUNC
61      */
62     int32_t DeleteTimer(std::string timerName);
63 
64     /**
65      * @tc.name: DmTimer::DeleteAll
66      * @tc.desc: delete all timer
67      * @tc.type: FUNC
68      */
69     int32_t DeleteAll();
70 
71 private:
72     mutable std::mutex timerMutex_;
73     std::unordered_map<std::string, ffrt::task_handle> timerVec_ = {};
74     std::shared_ptr<ffrt::queue> queue_;
75 };
76 }
77 }
78 #endif