1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #ifndef _ALARM_H_
20 #define _ALARM_H_
21 
22 #include <stdint.h>
23 #include "esp_timer.h"
24 #include "bt_user_config.h"
25 
26 typedef struct alarm_t osi_alarm_t;
27 typedef uint64_t period_ms_t;
28 typedef esp_timer_cb_t osi_alarm_callback_t;
29 
30 typedef enum {
31     OSI_ALARM_ERR_PASS = 0,
32     OSI_ALARM_ERR_FAIL = -1,
33     OSI_ALARM_ERR_INVALID_ARG = -2,
34     OSI_ALARM_ERR_INVALID_STATE = -3,
35 } osi_alarm_err_t;
36 
37 #define ALARM_CBS_NUM   UC_ALARM_MAX_NUM
38 #define ALARM_ID_BASE   1000
39 
40 int osi_alarm_create_mux(void);
41 int osi_alarm_delete_mux(void);
42 void osi_alarm_init(void);
43 void osi_alarm_deinit(void);
44 
45 // Creates a new alarm object. The returned object must be freed by calling
46 // |alarm_free|. Returns NULL on failure.
47 osi_alarm_t *osi_alarm_new(const char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire);
48 
49 // Frees an alarm object created by |alarm_new|. |alarm| may be NULL. If the
50 // alarm is pending, it will be cancelled. It is not safe to call |alarm_free|
51 // from inside the callback of |alarm|.
52 void osi_alarm_free(osi_alarm_t *alarm);
53 
54 // Sets an alarm to fire |cb| after the given |deadline|. Note that |deadline| is the
55 // number of milliseconds relative to the current time. |data| is a context variable
56 // for the callback and may be NULL. |cb| will be called back in the context of an
57 // unspecified thread (i.e. it will not be called back in the same thread as the caller).
58 // |alarm| and |cb| may not be NULL.
59 osi_alarm_err_t osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout);
60 
61 // Sets an periodic alarm to fire |cb| each given |period|.
62 osi_alarm_err_t osi_alarm_set_periodic(osi_alarm_t *alarm, period_ms_t period);
63 
64 // This function cancels the |alarm| if it was previously set. When this call
65 // returns, the caller has a guarantee that the callback is not in progress and
66 // will not be called if it hasn't already been called. This function is idempotent.
67 // |alarm| may not be NULL.
68 osi_alarm_err_t osi_alarm_cancel(osi_alarm_t *alarm);
69 
70 // Figure out how much time until next expiration.
71 // Returns 0 if not armed. |alarm| may not be NULL.
72 // only for oneshot alarm, not for periodic alarm
73 // TODO: Remove this function once PM timers can be re-factored
74 period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm);
75 
76 // Alarm-related state cleanup
77 //void alarm_cleanup(void);
78 
79 uint32_t osi_time_get_os_boottime_ms(void);
80 
81 // This function returns whether the given |alarm| is active or not.
82 // Return true if active, false otherwise.
83 bool osi_alarm_is_active(osi_alarm_t *alarm);
84 
85 #endif /*_ALARM_H_*/
86