1/*
2 * Copyright (c) 2023 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#include "dfsu_timer.h"
17
18#include "common_timer_errors.h"
19#include "utils_log.h"
20
21namespace OHOS::FileManagement {
22using namespace std;
23
24DfsuTimer &DfsuTimer::GetInstance()
25{
26    static DfsuTimer instance;
27    return instance;
28}
29
30DfsuTimer::DfsuTimer() : timer_(std::make_unique<Utils::Timer>("OH_DfsTimer"))
31{
32    timer_->Setup();
33}
34
35DfsuTimer::~DfsuTimer()
36{
37    if (timer_) {
38        timer_->Shutdown(true);
39        timer_ = nullptr;
40    }
41}
42
43void DfsuTimer::Register(const Utils::Timer::TimerCallback &callback, uint32_t &outTimerId, uint32_t interval)
44{
45    if (timer_ == nullptr) {
46        LOGE("timer is nullptr");
47        return;
48    }
49
50    uint32_t ret = timer_->Register(callback, interval);
51    if (ret == Utils::TIMER_ERR_DEAL_FAILED) {
52        LOGE("Register timer failed");
53        return;
54    }
55    outTimerId = ret;
56}
57
58void DfsuTimer::Unregister(uint32_t timerId)
59{
60    if ((timerId == 0) || (!timer_)) {
61        LOGE("timerId is 0 or timer_ is nullptr");
62        return;
63    }
64    timer_->Unregister(timerId);
65}
66} // namespace OHOS::FileManagement::CloudSync