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* SESSION_HEARTBEAT_TIMEOUT_TASK = "deviceManagerTimer:sessionHeartbeat"; 41 42 using TimerCallback = std::function<void (std::string name)>; 43 44 class DmTimer { 45 public: 46 DmTimer(); 47 ~DmTimer(); 48 49 /** 50 * @tc.name: DmTimer::StartTimer 51 * @tc.desc: start timer running 52 * @tc.type: FUNC 53 */ 54 int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback); 55 56 /** 57 * @tc.name: DmTimer::DeleteTimer 58 * @tc.desc: delete timer 59 * @tc.type: FUNC 60 */ 61 int32_t DeleteTimer(std::string timerName); 62 63 /** 64 * @tc.name: DmTimer::DeleteAll 65 * @tc.desc: delete all timer 66 * @tc.type: FUNC 67 */ 68 int32_t DeleteAll(); 69 70 private: 71 mutable std::mutex timerMutex_; 72 std::unordered_map<std::string, ffrt::task_handle> timerVec_ = {}; 73 std::shared_ptr<ffrt::queue> queue_; 74 }; 75 } 76 } 77 #endif