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 #include "nfc_timer.h"
16 #include "loghelper.h"
17 
18 namespace OHOS {
19 namespace NFC {
GetInstance()20 NfcTimer *NfcTimer::GetInstance()
21 {
22     static NfcTimer instance;
23     return &instance;
24 }
25 
NfcTimer()26 NfcTimer::NfcTimer() : timer_(std::make_unique<Utils::Timer>("NfcManagerTimer"))
27 {
28     timer_->Setup();
29 }
30 
~NfcTimer()31 NfcTimer::~NfcTimer()
32 {
33     if (timer_) {
34         timer_->Shutdown(true);
35         timer_ = nullptr;
36     }
37 }
38 
Register(const TimerCallback & callback,uint32_t & outTimerId,uint32_t interval)39 void NfcTimer::Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval)
40 {
41     if (timer_ == nullptr) {
42         ErrorLog("timer_ is nullptr");
43         return;
44     }
45 
46     uint32_t ret = timer_->Register(callback, interval, true);
47     if (ret == Utils::TIMER_ERR_DEAL_FAILED) {
48         ErrorLog("Register timer failed");
49         return;
50     }
51 
52     outTimerId = ret;
53     InfoLog("outTimerId is %{public}u, interval is %{public}u", outTimerId, interval);
54 }
55 
UnRegister(uint32_t timerId)56 void NfcTimer::UnRegister(uint32_t timerId)
57 {
58     InfoLog("timer shutting down, timerId = %{public}u", timerId);
59     if (timerId == 0) {
60         ErrorLog("timerId is 0, no register timer");
61         return;
62     }
63 
64     if (timer_ == nullptr) {
65         ErrorLog("timer_ is nullptr");
66         return;
67     }
68 
69     timer_->Unregister(timerId);
70 }
71 }  // namespace NFC
72 }  // namespace OHOS