• 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 #pragma once
18 
19 #include <cstdint>
20 #include <functional>
21 #include "osi/include/alarm.h"
22 
23 namespace bluetooth {
24 namespace shim {
25 
26 class Timer {
27  public:
28   /**
29    * Set this timer using the osi timer alarm functionality.
30    *
31    * The alarm duration *must not* be zero.  The timer is set
32    * on the bluetooth main message loop thread.
33    *
34    * @param duration_ms Duration in milliseconds (>0) before alarm pops
35    * @param func Function to execute upon alarm pop.
36    */
37   void Set(uint64_t duration_ms, std::function<void()> func);
38 
39   /**
40    * Cancel this previously set timer.
41    *
42    * The associated function call will *not* be executed.
43    */
44   void Cancel();
45 
46   /**
47    * Determine if a given timer has been set or not.
48    *
49    * @return |true| if timer has been set, |false| otherwise.
50    */
51   bool IsActive();
52 
53   /**
54    * @param name Arbitrary name passed to the osi module.
55    */
56   Timer(const char* name);
57   ~Timer();
58 
59   /**
60    * Pop this timer.
61    *
62    * Called from an internal trampoline timeout global function registered
63    * with osi alarm.  This trampoline function will then post
64    * the execution of the callback function onto the shim thread.
65    */
66   static void Pop(Timer* timer);
67 
68  private:
69   std::function<void()> callback_{};
70   alarm_t* timer_{nullptr};
71 };
72 
73 }  // namespace shim
74 }  // namespace bluetooth
75