Asterisk - The Open Source Telephony Project GIT-master-7e7a603
timing.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2008 - 2009, Digium, Inc.
5 *
6 * Kevin P. Fleming <kpfleming@digium.com>
7 * Russell Bryant <russell@digium.com>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20/*!
21 \file
22 \brief Timing source management
23 \author Kevin P. Fleming <kpfleming@digium.com>
24 \author Russell Bryant <russell@digium.com>
25
26 Portions of Asterisk require a timing source, a periodic trigger
27 for media handling activities. The functions in this file allow
28 a loadable module to provide a timing source for Asterisk and its
29 modules, so that those modules can request a 'timing handle' when
30 they require one. These handles are file descriptors, which can be
31 used with select() or poll().
32
33 The timing source used by Asterisk must provide the following
34 features:
35
36 1) Periodic triggers, with a configurable interval (specified as
37 number of triggers per second).
38
39 2) Multiple outstanding triggers, each of which must be 'acked'
40 to clear it. Triggers must also be 'ackable' in quantity.
41
42 3) Continuous trigger mode, which when enabled causes every call
43 to poll() on the timer handle to immediately return.
44
45 4) Multiple 'event types', so that the code using the timer can
46 know whether the wakeup it received was due to a periodic trigger
47 or a continuous trigger.
48 */
49
50#ifndef _ASTERISK_TIMING_H
51#define _ASTERISK_TIMING_H
52
53#if defined(__cplusplus) || defined(c_plusplus)
54extern "C" {
55#endif
56
60};
61
62/*!
63 * \brief Timing module interface
64 *
65 * The public API calls for the timing API directly map to this interface.
66 * So, the behavior of these calls should match the documentation of the
67 * public API calls.
68 */
70 const char *name;
71 /*! This handles the case where multiple timing modules are loaded.
72 * The highest priority timing interface available will be used. */
73 unsigned int priority;
74 void *(*timer_open)(void);
75 void (*timer_close)(void *data);
76 int (*timer_set_rate)(void *data, unsigned int rate);
77 int (*timer_ack)(void *data, unsigned int quantity);
78 int (*timer_enable_continuous)(void *data);
79 int (*timer_disable_continuous)(void *data);
80 enum ast_timer_event (*timer_get_event)(void *data);
81 unsigned int (*timer_get_max_rate)(void *data);
82 int (*timer_fd)(void *data);
83};
84
85/*!
86 * \brief Register a set of timing functions.
87 *
88 * \param i An instance of the \c ast_timing_interfaces structure with pointers
89 * to the functions provided by the timing implementation.
90 *
91 * \retval NULL failure
92 * \retval non-Null handle to be passed to ast_unregister_timing_interface() on success
93 * \since 1.6.1
94 */
95#define ast_register_timing_interface(i) _ast_register_timing_interface(i, AST_MODULE_SELF)
97 struct ast_module *mod);
98
99/*!
100 * \brief Unregister a previously registered timing interface.
101 *
102 * \param handle The handle returned from a prior successful call to
103 * ast_register_timing_interface().
104 *
105 * \retval 0 success
106 * \retval non-zero failure
107 * \since 1.6.1
108 */
109int ast_unregister_timing_interface(void *handle);
110
111struct ast_timer;
112
113/*!
114 * \brief Open a timer
115 *
116 * \retval NULL on error, with errno set
117 * \retval non-NULL timer handle on success
118 * \since 1.6.1
119 */
120struct ast_timer *ast_timer_open(void);
121
122/*!
123 * \brief Close an opened timing handle
124 *
125 * \param handle timer handle returned from timer_open()
126 *
127 * \since 1.6.1
128 */
129void ast_timer_close(struct ast_timer *handle);
130
131/*!
132 * \brief Get a poll()-able file descriptor for a timer
133 *
134 * \param handle timer handle returned from timer_open()
135 *
136 * \return file descriptor which can be used with poll() to wait for events
137 * \since 1.6.1
138 */
139int ast_timer_fd(const struct ast_timer *handle);
140
141/*!
142 * \brief Set the timing tick rate
143 *
144 * \param handle timer handle returned from timer_open()
145 * \param rate ticks per second, 0 turns the ticks off if needed
146 *
147 * Use this function if you want the timer to show input at a certain
148 * rate. The other alternative use of a timer is the continuous
149 * mode.
150 *
151 * \retval -1 error, with errno set
152 * \retval 0 success
153 * \since 1.6.1
154 */
155int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate);
156
157/*!
158 * \brief Acknowledge a timer event
159 *
160 * \param handle timer handle returned from timer_open()
161 * \param quantity number of timer events to acknowledge
162 *
163 * \note This function should only be called if timer_get_event()
164 * returned AST_TIMING_EVENT_EXPIRED.
165 *
166 * \retval -1 failure, with errno set
167 * \retval 0 success
168 * \since 10.5.2
169 */
170int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity);
171
172/*!
173 * \brief Enable continuous mode
174 *
175 * \param handle timer handle returned from timer_open()
176 *
177 * Continuous mode causes poll() on the timer's fd to immediately return
178 * always until continuous mode is disabled.
179 *
180 * \retval -1 failure, with errno set
181 * \retval 0 success
182 * \since 1.6.1
183 */
184int ast_timer_enable_continuous(const struct ast_timer *handle);
185
186/*!
187 * \brief Disable continuous mode
188 *
189 * \param handle timer handle returned from timer_close()
190 *
191 * \retval -1 failure, with errno set
192 * \retval 0 success
193 * \since 1.6.1
194 */
195int ast_timer_disable_continuous(const struct ast_timer *handle);
196
197/*!
198 * \brief Retrieve timing event
199 *
200 * \param handle timer handle returned by timer_open()
201 *
202 * After poll() indicates that there is input on the timer's fd, this will
203 * be called to find out what triggered it.
204 *
205 * \return which event triggered the timer
206 * \since 1.6.1
207 */
208enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle);
209
210/*!
211 * \brief Get maximum rate supported for a timer
212 *
213 * \param handle timer handle returned by timer_open()
214 *
215 * \return maximum rate supported by timer
216 * \since 1.6.1
217 */
218unsigned int ast_timer_get_max_rate(const struct ast_timer *handle);
219
220/*!
221 * \brief Get name of timer in use
222 *
223 * \param handle timer handle returned by timer_open()
224 *
225 * \return name of timer
226 * \since 1.6.2
227 */
228const char *ast_timer_get_name(const struct ast_timer *handle);
229
230#if defined(__cplusplus) || defined(c_plusplus)
231}
232#endif
233
234#endif /* _ASTERISK_TIMING_H */
Timing module interface.
Definition: timing.h:69
unsigned int priority
Definition: timing.h:73
int(* timer_set_rate)(void *data, unsigned int rate)
Definition: timing.h:76
enum ast_timer_event(* timer_get_event)(void *data)
Definition: timing.h:80
int(* timer_disable_continuous)(void *data)
Definition: timing.h:79
int(* timer_enable_continuous)(void *data)
Definition: timing.h:78
void(* timer_close)(void *data)
Definition: timing.h:75
const char * name
Definition: timing.h:70
int(* timer_fd)(void *data)
Definition: timing.h:82
int(* timer_ack)(void *data, unsigned int quantity)
Definition: timing.h:77
unsigned int(* timer_get_max_rate)(void *data)
Definition: timing.h:81
unsigned int ast_timer_get_max_rate(const struct ast_timer *handle)
Get maximum rate supported for a timer.
Definition: timing.c:191
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition: timing.c:154
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
Acknowledge a timer event.
Definition: timing.c:171
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition: timing.c:166
int ast_unregister_timing_interface(void *handle)
Unregister a previously registered timing interface.
Definition: timing.c:104
enum ast_timer_event ast_timer_get_event(const struct ast_timer *handle)
Retrieve timing event.
Definition: timing.c:186
int ast_timer_enable_continuous(const struct ast_timer *handle)
Enable continuous mode.
Definition: timing.c:176
const char * ast_timer_get_name(const struct ast_timer *handle)
Get name of timer in use.
Definition: timing.c:196
int ast_timer_disable_continuous(const struct ast_timer *handle)
Disable continuous mode.
Definition: timing.c:181
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition: timing.c:122
ast_timer_event
Definition: timing.h:57
@ AST_TIMING_EVENT_CONTINUOUS
Definition: timing.h:59
@ AST_TIMING_EVENT_EXPIRED
Definition: timing.h:58
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition: timing.c:161
void * _ast_register_timing_interface(struct ast_timing_interface *funcs, struct ast_module *mod)
Definition: timing.c:73