1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License.
5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at
6bc2ed2b3Sopenharmony_ci *
7bc2ed2b3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc2ed2b3Sopenharmony_ci *
9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and
13bc2ed2b3Sopenharmony_ci * limitations under the License.
14bc2ed2b3Sopenharmony_ci */
15bc2ed2b3Sopenharmony_ci#include "nfc_timer.h"
16bc2ed2b3Sopenharmony_ci#include "loghelper.h"
17bc2ed2b3Sopenharmony_ci
18bc2ed2b3Sopenharmony_cinamespace OHOS {
19bc2ed2b3Sopenharmony_cinamespace NFC {
20bc2ed2b3Sopenharmony_ciNfcTimer *NfcTimer::GetInstance()
21bc2ed2b3Sopenharmony_ci{
22bc2ed2b3Sopenharmony_ci    static NfcTimer instance;
23bc2ed2b3Sopenharmony_ci    return &instance;
24bc2ed2b3Sopenharmony_ci}
25bc2ed2b3Sopenharmony_ci
26bc2ed2b3Sopenharmony_ciNfcTimer::NfcTimer() : timer_(std::make_unique<Utils::Timer>("NfcManagerTimer"))
27bc2ed2b3Sopenharmony_ci{
28bc2ed2b3Sopenharmony_ci    timer_->Setup();
29bc2ed2b3Sopenharmony_ci}
30bc2ed2b3Sopenharmony_ci
31bc2ed2b3Sopenharmony_ciNfcTimer::~NfcTimer()
32bc2ed2b3Sopenharmony_ci{
33bc2ed2b3Sopenharmony_ci    if (timer_) {
34bc2ed2b3Sopenharmony_ci        timer_->Shutdown(true);
35bc2ed2b3Sopenharmony_ci        timer_ = nullptr;
36bc2ed2b3Sopenharmony_ci    }
37bc2ed2b3Sopenharmony_ci}
38bc2ed2b3Sopenharmony_ci
39bc2ed2b3Sopenharmony_civoid NfcTimer::Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval)
40bc2ed2b3Sopenharmony_ci{
41bc2ed2b3Sopenharmony_ci    if (timer_ == nullptr) {
42bc2ed2b3Sopenharmony_ci        ErrorLog("timer_ is nullptr");
43bc2ed2b3Sopenharmony_ci        return;
44bc2ed2b3Sopenharmony_ci    }
45bc2ed2b3Sopenharmony_ci
46bc2ed2b3Sopenharmony_ci    uint32_t ret = timer_->Register(callback, interval, true);
47bc2ed2b3Sopenharmony_ci    if (ret == Utils::TIMER_ERR_DEAL_FAILED) {
48bc2ed2b3Sopenharmony_ci        ErrorLog("Register timer failed");
49bc2ed2b3Sopenharmony_ci        return;
50bc2ed2b3Sopenharmony_ci    }
51bc2ed2b3Sopenharmony_ci
52bc2ed2b3Sopenharmony_ci    outTimerId = ret;
53bc2ed2b3Sopenharmony_ci    InfoLog("outTimerId is %{public}u, interval is %{public}u", outTimerId, interval);
54bc2ed2b3Sopenharmony_ci}
55bc2ed2b3Sopenharmony_ci
56bc2ed2b3Sopenharmony_civoid NfcTimer::UnRegister(uint32_t timerId)
57bc2ed2b3Sopenharmony_ci{
58bc2ed2b3Sopenharmony_ci    InfoLog("timer shutting down, timerId = %{public}u", timerId);
59bc2ed2b3Sopenharmony_ci    if (timerId == 0) {
60bc2ed2b3Sopenharmony_ci        ErrorLog("timerId is 0, no register timer");
61bc2ed2b3Sopenharmony_ci        return;
62bc2ed2b3Sopenharmony_ci    }
63bc2ed2b3Sopenharmony_ci
64bc2ed2b3Sopenharmony_ci    if (timer_ == nullptr) {
65bc2ed2b3Sopenharmony_ci        ErrorLog("timer_ is nullptr");
66bc2ed2b3Sopenharmony_ci        return;
67bc2ed2b3Sopenharmony_ci    }
68bc2ed2b3Sopenharmony_ci
69bc2ed2b3Sopenharmony_ci    timer_->Unregister(timerId);
70bc2ed2b3Sopenharmony_ci}
71bc2ed2b3Sopenharmony_ci}  // namespace NFC
72bc2ed2b3Sopenharmony_ci}  // namespace OHOS