• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "bt_shim_timer"
18 
19 #include <base/bind.h>
20 #include <cstdint>
21 #include <functional>
22 
23 #include "main/shim/shim.h"
24 #include "main/shim/timer.h"
25 #include "osi/include/alarm.h"
26 #include "osi/include/log.h"
27 
timer_timeout(void * data)28 static void timer_timeout(void* data) {
29   CHECK(data != nullptr);
30   bluetooth::shim::Timer* timeout = static_cast<bluetooth::shim::Timer*>(data);
31   bluetooth::shim::Post(
32       base::Bind(&bluetooth::shim::Timer::Pop, base::Unretained(timeout)));
33 }
34 
Set(uint64_t duration_ms,std::function<void ()> func)35 void bluetooth::shim::Timer::Set(uint64_t duration_ms,
36                                  std::function<void()> func) {
37   CHECK(duration_ms != 0);
38   callback_ = func;
39   alarm_set_on_mloop(timer_, duration_ms, timer_timeout, (void*)this);
40 }
41 
Cancel()42 void bluetooth::shim::Timer::Cancel() {
43   alarm_cancel(timer_);
44   callback_ = {};
45 }
46 
IsActive()47 bool bluetooth::shim::Timer::IsActive() { return callback_ != nullptr; }
48 
Timer(const char * name)49 bluetooth::shim::Timer::Timer(const char* name) {
50   timer_ = alarm_new(name);
51   CHECK(timer_ != nullptr);
52 }
53 
~Timer()54 bluetooth::shim::Timer::~Timer() { alarm_free(timer_); }
55 
Pop(Timer * timer)56 void bluetooth::shim::Timer::Pop(Timer* timer) {
57   timer->callback_();
58   timer->callback_ = {};
59 }
60