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