libamxp  1.4.0
Patterns C Implementation
amxp_timer.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #if !defined(__AMXP_TIMER_H__)
56 #define __AMXP_TIMER_H__
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 #include <sys/time.h>
64 
124 typedef struct _amxp_timer amxp_timer_t;
125 
139 typedef void (* amxp_timer_cb_t) (amxp_timer_t* timer, void* priv);
140 
148 typedef enum _amxp_timer_state {
155 
163 struct _amxp_timer {
164  amxc_llist_it_t it;
165  struct itimerval timer;
168  void* priv;
169 };
170 
185 void amxp_timers_calculate(void);
186 
206 void amxp_timers_check(void);
207 
220 void amxp_timers_enable(bool enable);
221 
241 int amxp_timer_new(amxp_timer_t** timer, amxp_timer_cb_t cb, void* priv);
242 
256 void amxp_timer_delete(amxp_timer_t** timer);
257 
275 int amxp_timer_set_interval(amxp_timer_t* timer, unsigned int msec);
276 
290 unsigned int amxp_timer_remaining_time(amxp_timer_t* timer);
291 
311 int amxp_timer_start(amxp_timer_t* timer, unsigned int timeout_msec);
312 
327 int amxp_timer_stop(amxp_timer_t* timer);
328 
342 
343 #ifdef __cplusplus
344 }
345 #endif
346 
347 #endif // __AMXP_TIMER_H__
int amxp_timer_start(amxp_timer_t *timer, unsigned int timeout_msec)
Starts or resets a timer.
Definition: amxp_timer.c:296
unsigned int amxp_timer_remaining_time(amxp_timer_t *timer)
Get the remaining time of the timer.
Definition: amxp_timer.c:281
amxp_timer_state_t amxp_timer_get_state(amxp_timer_t *timer)
Get the timer's state.
Definition: amxp_timer.c:334
void amxp_timers_check(void)
Check all timers and call the callback function when the timer is in Timer expired state.
Definition: amxp_timer.c:183
void(* amxp_timer_cb_t)(amxp_timer_t *timer, void *priv)
Timer timeout callback function.
Definition: amxp_timer.h:139
int amxp_timer_stop(amxp_timer_t *timer)
Stops the timer.
Definition: amxp_timer.c:319
enum _amxp_timer_state amxp_timer_state_t
The timer states.
void amxp_timers_calculate(void)
Caclulates the remaining time of all timers.
Definition: amxp_timer.c:144
void amxp_timers_enable(bool enable)
Enable or disable all timers.
Definition: amxp_timer.c:225
void amxp_timer_delete(amxp_timer_t **timer)
Deletes a timer.
Definition: amxp_timer.c:247
int amxp_timer_new(amxp_timer_t **timer, amxp_timer_cb_t cb, void *priv)
Allocate and initializes a new timer.
Definition: amxp_timer.c:229
_amxp_timer_state
The timer states.
Definition: amxp_timer.h:148
int amxp_timer_set_interval(amxp_timer_t *timer, unsigned int msec)
Sets the interval of a timer in milli seconds.
Definition: amxp_timer.c:259
@ amxp_timer_off
Definition: amxp_timer.h:149
@ amxp_timer_running
Definition: amxp_timer.h:151
@ amxp_timer_expired
Definition: amxp_timer.h:152
@ amxp_timer_deleted
Definition: amxp_timer.h:153
@ amxp_timer_started
Definition: amxp_timer.h:150
The timer type.
Definition: amxp_timer.h:163
struct itimerval timer
Definition: amxp_timer.h:165
void * priv
Definition: amxp_timer.h:168
amxp_timer_state_t state
Definition: amxp_timer.h:166
amxc_llist_it_t it
Definition: amxp_timer.h:164
amxp_timer_cb_t cb
Definition: amxp_timer.h:167