• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_timer.h"
17 
18 #include "bluetooth_log.h"
19 #include "bluetooth_errorcode.h"
20 #include "common_timer_errors.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 
GetInstance(void)25 NapiTimer *NapiTimer::GetInstance(void)
26 {
27     static NapiTimer instance;
28     return &instance;
29 }
30 
NapiTimer()31 NapiTimer::NapiTimer() : timer_(std::make_unique<OHOS::Utils::Timer>("NapiBtTimer"))
32 {
33     timer_->Setup();
34 }
35 
~NapiTimer()36 NapiTimer::~NapiTimer()
37 {
38     if (timer_) {
39         timer_->Shutdown();
40     }
41 }
42 
Register(const TimerCallback & callback,uint32_t & outTimerId,uint32_t interval)43 int NapiTimer::Register(const TimerCallback& callback, uint32_t &outTimerId, uint32_t interval)
44 {
45     if (timer_ == nullptr) {
46         HILOGE("timer_ is nulptr");
47         return BT_ERR_INTERNAL_ERROR;
48     }
49 
50     uint32_t ret = timer_->Register(callback, interval, true); // only support once timer now
51     if (ret == OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
52         HILOGE("Register timer failed");
53         return BT_ERR_INTERNAL_ERROR;
54     }
55 
56     outTimerId = ret;
57     HILOGI("timerId: %{public}u", outTimerId);
58     return BT_NO_ERROR;
59 }
60 
Unregister(uint32_t timerId)61 void NapiTimer::Unregister(uint32_t timerId)
62 {
63     if (timerId == 0) {
64         HILOGE("timerId is 0, no registered timer");
65         return;
66     }
67 
68     HILOGI("timerId: %{public}d", timerId);
69     if (timer_ == nullptr) {
70         HILOGE("timer_ is nulptr");
71         return;
72     }
73 
74     timer_->Unregister(timerId);
75 }
76 
77 }  // namespace Bluetooth
78 }  // namespace OHOS