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 25 typedef struct alarm_t osi_alarm_t; 26 typedef uint64_t period_ms_t; 27 typedef esp_timer_cb_t osi_alarm_callback_t; 28 29 typedef enum { 30 OSI_ALARM_ERR_PASS = 0, 31 OSI_ALARM_ERR_FAIL = -1, 32 OSI_ALARM_ERR_INVALID_ARG = -2, 33 OSI_ALARM_ERR_INVALID_STATE = -3, 34 } osi_alarm_err_t; 35 36 #define ALARM_CBS_NUM 50 37 #define ALARM_ID_BASE 1000 38 39 int osi_alarm_create_mux(void); 40 int osi_alarm_delete_mux(void); 41 void osi_alarm_init(void); 42 void osi_alarm_deinit(void); 43 44 // Creates a new alarm object. The returned object must be freed by calling 45 // |alarm_free|. Returns NULL on failure. 46 osi_alarm_t *osi_alarm_new(const char *alarm_name, osi_alarm_callback_t callback, void *data, period_ms_t timer_expire); 47 48 // Frees an alarm object created by |alarm_new|. |alarm| may be NULL. If the 49 // alarm is pending, it will be cancelled. It is not safe to call |alarm_free| 50 // from inside the callback of |alarm|. 51 void osi_alarm_free(osi_alarm_t *alarm); 52 53 // Sets an alarm to fire |cb| after the given |deadline|. Note that |deadline| is the 54 // number of milliseconds relative to the current time. |data| is a context variable 55 // for the callback and may be NULL. |cb| will be called back in the context of an 56 // unspecified thread (i.e. it will not be called back in the same thread as the caller). 57 // |alarm| and |cb| may not be NULL. 58 osi_alarm_err_t osi_alarm_set(osi_alarm_t *alarm, period_ms_t timeout); 59 60 // Sets an periodic alarm to fire |cb| each given |period|. 61 osi_alarm_err_t osi_alarm_set_periodic(osi_alarm_t *alarm, period_ms_t period); 62 63 // This function cancels the |alarm| if it was previously set. When this call 64 // returns, the caller has a guarantee that the callback is not in progress and 65 // will not be called if it hasn't already been called. This function is idempotent. 66 // |alarm| may not be NULL. 67 osi_alarm_err_t osi_alarm_cancel(osi_alarm_t *alarm); 68 69 // Figure out how much time until next expiration. 70 // Returns 0 if not armed. |alarm| may not be NULL. 71 // only for oneshot alarm, not for periodic alarm 72 // TODO: Remove this function once PM timers can be re-factored 73 period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm); 74 75 // Alarm-related state cleanup 76 //void alarm_cleanup(void); 77 78 uint32_t osi_time_get_os_boottime_ms(void); 79 80 // This function returns whether the given |alarm| is active or not. 81 // Return true if active, false otherwise. 82 bool osi_alarm_is_active(osi_alarm_t *alarm); 83 84 #endif /*_ALARM_H_*/ 85