1 /*
2  * Copyright (C) Huawei Device Co., Ltd. 2023-2023. All rights reserved.
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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_timer"
17 #endif
18 
19 #include "bluetooth_timer.h"
20 #include "common_timer_errors.h"
21 #include "bluetooth_log.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
GetInstance()25 BluetoothTimer *BluetoothTimer::GetInstance()
26 {
27     static BluetoothTimer instance;
28     return &instance;
29 }
30 
BluetoothTimer()31 BluetoothTimer::BluetoothTimer() : timer_(std::make_unique<OHOS::Utils::Timer>("BluetoothTimer"))
32 {
33     timer_->Setup();
34 }
35 
~BluetoothTimer()36 BluetoothTimer::~BluetoothTimer()
37 {
38     if (timer_) {
39         timer_->Shutdown(true);
40         timer_ = nullptr;
41     }
42 }
43 
Register(const TimerCallback & callback,uint32_t & outTimerId,uint32_t interval)44 void BluetoothTimer::Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval)
45 {
46     if (timer_ == nullptr) {
47         HILOGE("timer_ is nullptr");
48         return;
49     }
50 
51     uint32_t ret = timer_->Register(callback, interval, true); // one shot timer
52     if (ret == OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
53         HILOGE("Register timer failed");
54         return;
55     }
56 
57     outTimerId = ret;
58 }
59 
UnRegister(uint32_t timerId)60 void BluetoothTimer::UnRegister(uint32_t timerId)
61 {
62     if (timerId == 0) {
63         HILOGE("timerId is 0, no register timer");
64         return;
65     }
66 
67     if (timer_ == nullptr) {
68         HILOGE("timer_ is nullptr");
69         return;
70     }
71 
72     timer_->Unregister(timerId);
73 }
74 }  // namespace NFC
75 }  // namespace OHOS