12ee81decSopenharmony_ci/*
22ee81decSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
32ee81decSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42ee81decSopenharmony_ci * you may not use this file except in compliance with the License.
52ee81decSopenharmony_ci * You may obtain a copy of the License at
62ee81decSopenharmony_ci *
72ee81decSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
82ee81decSopenharmony_ci *
92ee81decSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102ee81decSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112ee81decSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122ee81decSopenharmony_ci * See the License for the specific language governing permissions and
132ee81decSopenharmony_ci * limitations under the License.
142ee81decSopenharmony_ci */
152ee81decSopenharmony_ci
162ee81decSopenharmony_ci#include "utils_timer.h"
172ee81decSopenharmony_ci
182ee81decSopenharmony_ci#include <functional>
192ee81decSopenharmony_ci#include <new>
202ee81decSopenharmony_ci
212ee81decSopenharmony_ci#include "nocopyable.h"
222ee81decSopenharmony_ci#include "singleton.h"
232ee81decSopenharmony_ci#include "timer.h"
242ee81decSopenharmony_ci
252ee81decSopenharmony_ci#ifdef __cplusplus
262ee81decSopenharmony_ciextern "C" {
272ee81decSopenharmony_ci#endif
282ee81decSopenharmony_ci
292ee81decSopenharmony_cinamespace {
302ee81decSopenharmony_ciusing namespace OHOS;
312ee81decSopenharmony_ciclass UtilsTimer final : public OHOS::Utils::Timer, public DelayedRefSingleton<UtilsTimer> {
322ee81decSopenharmony_cipublic:
332ee81decSopenharmony_ci    UtilsTimer();
342ee81decSopenharmony_ci    ~UtilsTimer() override;
352ee81decSopenharmony_ci};
362ee81decSopenharmony_ci
372ee81decSopenharmony_ciUtilsTimer::~UtilsTimer()
382ee81decSopenharmony_ci{
392ee81decSopenharmony_ci    this->Shutdown();
402ee81decSopenharmony_ci}
412ee81decSopenharmony_ci
422ee81decSopenharmony_ciUtilsTimer::UtilsTimer() : Timer("timer_process")
432ee81decSopenharmony_ci{
442ee81decSopenharmony_ci    this->Setup();
452ee81decSopenharmony_ci}
462ee81decSopenharmony_ci} // namespace
472ee81decSopenharmony_ci
482ee81decSopenharmony_civoid DoTimerProcess(TimerProc callback, const void *context)
492ee81decSopenharmony_ci{
502ee81decSopenharmony_ci    if (callback != nullptr) {
512ee81decSopenharmony_ci        callback(context);
522ee81decSopenharmony_ci    }
532ee81decSopenharmony_ci}
542ee81decSopenharmony_ci
552ee81decSopenharmony_ciTimerHandle DslmUtilsStartPeriodicTimerTask(uint32_t interval, TimerProc callback, const void *context)
562ee81decSopenharmony_ci{
572ee81decSopenharmony_ci    UtilsTimer &st = DelayedRefSingleton<UtilsTimer>::GetInstance();
582ee81decSopenharmony_ci    uint32_t timerId = st.Register([callback, context]() { DoTimerProcess(callback, context); }, interval, false);
592ee81decSopenharmony_ci    return static_cast<TimerHandle>(timerId);
602ee81decSopenharmony_ci}
612ee81decSopenharmony_ci
622ee81decSopenharmony_ciTimerHandle DslmUtilsStartOnceTimerTask(uint32_t interval, TimerProc callback, const void *context)
632ee81decSopenharmony_ci{
642ee81decSopenharmony_ci    UtilsTimer &st = DelayedRefSingleton<UtilsTimer>::GetInstance();
652ee81decSopenharmony_ci    uint32_t timerId = st.Register([callback, context]() { DoTimerProcess(callback, context); }, interval, true);
662ee81decSopenharmony_ci    return static_cast<TimerHandle>(timerId);
672ee81decSopenharmony_ci}
682ee81decSopenharmony_ci
692ee81decSopenharmony_civoid DslmUtilsStopTimerTask(TimerHandle handle)
702ee81decSopenharmony_ci{
712ee81decSopenharmony_ci    UtilsTimer &st = DelayedRefSingleton<UtilsTimer>::GetInstance();
722ee81decSopenharmony_ci    st.Unregister(static_cast<uint32_t>(handle));
732ee81decSopenharmony_ci}
742ee81decSopenharmony_ci#ifdef __cplusplus
752ee81decSopenharmony_ci}
762ee81decSopenharmony_ci#endif
77