1c29fa5a6Sopenharmony_ci/* 2c29fa5a6Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License. 5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at 6c29fa5a6Sopenharmony_ci * 7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8c29fa5a6Sopenharmony_ci * 9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and 13c29fa5a6Sopenharmony_ci * limitations under the License. 14c29fa5a6Sopenharmony_ci */ 15c29fa5a6Sopenharmony_ci 16c29fa5a6Sopenharmony_ci#ifndef TIMER_MANAGER_H 17c29fa5a6Sopenharmony_ci#define TIMER_MANAGER_H 18c29fa5a6Sopenharmony_ci 19c29fa5a6Sopenharmony_ci#include <future> 20c29fa5a6Sopenharmony_ci#include <functional> 21c29fa5a6Sopenharmony_ci#include <list> 22c29fa5a6Sopenharmony_ci#include <memory> 23c29fa5a6Sopenharmony_ci 24c29fa5a6Sopenharmony_ci#include "nocopyable.h" 25c29fa5a6Sopenharmony_ci 26c29fa5a6Sopenharmony_ci#include "i_context.h" 27c29fa5a6Sopenharmony_ci 28c29fa5a6Sopenharmony_cinamespace OHOS { 29c29fa5a6Sopenharmony_cinamespace Msdp { 30c29fa5a6Sopenharmony_cinamespace DeviceStatus { 31c29fa5a6Sopenharmony_ciclass TimerManager final : public ITimerManager { 32c29fa5a6Sopenharmony_cipublic: 33c29fa5a6Sopenharmony_ci TimerManager() = default; 34c29fa5a6Sopenharmony_ci DISALLOW_COPY_AND_MOVE(TimerManager); 35c29fa5a6Sopenharmony_ci ~TimerManager() = default; 36c29fa5a6Sopenharmony_ci 37c29fa5a6Sopenharmony_ci int32_t Init(IContext *context); 38c29fa5a6Sopenharmony_ci int32_t AddTimer(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback) override; 39c29fa5a6Sopenharmony_ci int32_t ResetTimer(int32_t timerId); 40c29fa5a6Sopenharmony_ci int32_t RemoveTimer(int32_t timerId) override; 41c29fa5a6Sopenharmony_ci bool IsExist(int32_t timerId) const; 42c29fa5a6Sopenharmony_ci void ProcessTimers(); 43c29fa5a6Sopenharmony_ci int32_t GetTimerFd() const; 44c29fa5a6Sopenharmony_ci 45c29fa5a6Sopenharmony_ciprivate: 46c29fa5a6Sopenharmony_ci struct TimerItem { 47c29fa5a6Sopenharmony_ci int32_t id { 0 }; 48c29fa5a6Sopenharmony_ci int32_t intervalMs { 0 }; 49c29fa5a6Sopenharmony_ci int32_t callbackCount { 0 }; 50c29fa5a6Sopenharmony_ci int32_t repeatCount { 0 }; 51c29fa5a6Sopenharmony_ci int64_t nextCallTime { 0 }; 52c29fa5a6Sopenharmony_ci std::function<void()> callback { nullptr }; 53c29fa5a6Sopenharmony_ci }; 54c29fa5a6Sopenharmony_ci 55c29fa5a6Sopenharmony_ci int32_t OnInit(IContext *context); 56c29fa5a6Sopenharmony_ci int32_t OnAddTimer(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback); 57c29fa5a6Sopenharmony_ci int32_t OnProcessTimers(); 58c29fa5a6Sopenharmony_ci int32_t OnResetTimer(int32_t timerId); 59c29fa5a6Sopenharmony_ci int32_t OnRemoveTimer(int32_t timerId); 60c29fa5a6Sopenharmony_ci bool OnIsExist(int32_t timerId) const; 61c29fa5a6Sopenharmony_ci int32_t RunIsExist(std::packaged_task<bool(int32_t)> &task, int32_t timerId) const; 62c29fa5a6Sopenharmony_ci int32_t TakeNextTimerId(); 63c29fa5a6Sopenharmony_ci int32_t AddTimerInternal(int32_t intervalMs, int32_t repeatCount, std::function<void()> callback); 64c29fa5a6Sopenharmony_ci int32_t ResetTimerInternal(int32_t timerId); 65c29fa5a6Sopenharmony_ci int32_t RemoveTimerInternal(int32_t timerId); 66c29fa5a6Sopenharmony_ci void InsertTimerInternal(std::unique_ptr<TimerItem> &timer); 67c29fa5a6Sopenharmony_ci void ProcessTimersInternal(); 68c29fa5a6Sopenharmony_ci int64_t CalcNextDelayInternal(); 69c29fa5a6Sopenharmony_ci int32_t ArmTimer(); 70c29fa5a6Sopenharmony_ci 71c29fa5a6Sopenharmony_ci int32_t timerFd_ { -1 }; 72c29fa5a6Sopenharmony_ci IContext *context_ { nullptr }; 73c29fa5a6Sopenharmony_ci std::list<std::unique_ptr<TimerItem>> timers_; 74c29fa5a6Sopenharmony_ci}; 75c29fa5a6Sopenharmony_ci 76c29fa5a6Sopenharmony_ciinline int32_t TimerManager::GetTimerFd() const 77c29fa5a6Sopenharmony_ci{ 78c29fa5a6Sopenharmony_ci return timerFd_; 79c29fa5a6Sopenharmony_ci} 80c29fa5a6Sopenharmony_ci} // namespace DeviceStatus 81c29fa5a6Sopenharmony_ci} // namespace Msdp 82c29fa5a6Sopenharmony_ci} // namespace OHOS 83c29fa5a6Sopenharmony_ci#endif // TIMER_MANAGER_H