timing.h 6.5 KB

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