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 <queue> 26 #include <vector> 27 #include <unordered_set> 28 29 #include "event_handler.h" 30 31 namespace OHOS { 32 namespace DistributedHardware { 33 constexpr const char* AUTHENTICATE_TIMEOUT_TASK = "deviceManagerTimer:authenticate"; 34 constexpr const char* NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:negotiate"; 35 constexpr const char* CONFIRM_TIMEOUT_TASK = "deviceManagerTimer:confirm"; 36 constexpr const char* INPUT_TIMEOUT_TASK = "deviceManagerTimer:input"; 37 constexpr const char* ADD_TIMEOUT_TASK = "deviceManagerTimer:add"; 38 constexpr const char* WAIT_NEGOTIATE_TIMEOUT_TASK = "deviceManagerTimer:waitNegotiate"; 39 constexpr const char* WAIT_REQUEST_TIMEOUT_TASK = "deviceManagerTimer:waitRequest"; 40 constexpr const char* STATE_TIMER_PREFIX = "deviceManagerTimer:stateTimer_"; 41 constexpr const char* AUTH_DEVICE_TIMEOUT_TASK = "deviceManagerTimer:authDevice_"; 42 constexpr const char* SYNC_DELETE_TIMEOUT_TASK = "deviceManagerTimer:syncDelete_"; 43 constexpr const char* SESSION_HEARTBEAT_TIMEOUT_TASK = "deviceManagerTimer:sessionHeartbeat"; 44 45 using TimerCallback = std::function<void (std::string name)>; 46 47 class CommonEventHandler : public AppExecFwk::EventHandler { 48 public: 49 CommonEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner); 50 ~CommonEventHandler() override = default; 51 52 bool PostTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0); 53 void RemoveTask(const std::string &name); 54 }; 55 56 class DmTimer { 57 public: 58 DmTimer(); 59 ~DmTimer(); 60 61 /** 62 * @tc.name: DmTimer::StartTimer 63 * @tc.desc: start timer running 64 * @tc.type: FUNC 65 */ 66 int32_t StartTimer(std::string name, int32_t timeOut, TimerCallback callback); 67 68 /** 69 * @tc.name: DmTimer::DeleteTimer 70 * @tc.desc: delete timer 71 * @tc.type: FUNC 72 */ 73 int32_t DeleteTimer(std::string timerName); 74 75 /** 76 * @tc.name: DmTimer::DeleteAll 77 * @tc.desc: delete all timer 78 * @tc.type: FUNC 79 */ 80 int32_t DeleteAll(); 81 82 private: 83 mutable std::mutex timerMutex_; 84 std::unordered_set<std::string> timerVec_ = {}; 85 std::shared_ptr<CommonEventHandler> eventHandler_; 86 }; 87 } 88 } 89 #endif