res_timing_dahdi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \author Russell Bryant <russell@digium.com>
  21. *
  22. * \brief DAHDI timing interface
  23. */
  24. /*** MODULEINFO
  25. <depend>dahdi</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <math.h>
  34. #include <dahdi/user.h>
  35. #include "asterisk/module.h"
  36. #include "asterisk/timing.h"
  37. #include "asterisk/utils.h"
  38. static void *timing_funcs_handle;
  39. static void *dahdi_timer_open(void);
  40. static void dahdi_timer_close(void *data);
  41. static int dahdi_timer_set_rate(void *data, unsigned int rate);
  42. static int dahdi_timer_ack(void *data, unsigned int quantity);
  43. static int dahdi_timer_enable_continuous(void *data);
  44. static int dahdi_timer_disable_continuous(void *data);
  45. static enum ast_timer_event dahdi_timer_get_event(void *data);
  46. static unsigned int dahdi_timer_get_max_rate(void *data);
  47. static int dahdi_timer_fd(void *data);
  48. static struct ast_timing_interface dahdi_timing = {
  49. .name = "DAHDI",
  50. .priority = 100,
  51. .timer_open = dahdi_timer_open,
  52. .timer_close = dahdi_timer_close,
  53. .timer_set_rate = dahdi_timer_set_rate,
  54. .timer_ack = dahdi_timer_ack,
  55. .timer_enable_continuous = dahdi_timer_enable_continuous,
  56. .timer_disable_continuous = dahdi_timer_disable_continuous,
  57. .timer_get_event = dahdi_timer_get_event,
  58. .timer_get_max_rate = dahdi_timer_get_max_rate,
  59. .timer_fd = dahdi_timer_fd,
  60. };
  61. struct dahdi_timer {
  62. int fd;
  63. };
  64. static void *dahdi_timer_open(void)
  65. {
  66. struct dahdi_timer *timer;
  67. if (!(timer = ast_calloc(1, sizeof(*timer)))) {
  68. return NULL;
  69. }
  70. if ((timer->fd = open("/dev/dahdi/timer", O_RDWR)) < 0) {
  71. ast_log(LOG_ERROR, "Failed to create dahdi timer: %s\n", strerror(errno));
  72. ast_free(timer);
  73. return NULL;
  74. }
  75. return timer;
  76. }
  77. static void dahdi_timer_close(void *data)
  78. {
  79. struct dahdi_timer *timer = data;
  80. close(timer->fd);
  81. ast_free(timer);
  82. }
  83. static int dahdi_timer_set_rate(void *data, unsigned int rate)
  84. {
  85. struct dahdi_timer *timer = data;
  86. int samples;
  87. /* DAHDI timers are configured using a number of samples,
  88. * based on an 8 kHz sample rate. */
  89. samples = (unsigned int) roundf((8000.0 / ((float) rate)));
  90. if (ioctl(timer->fd, DAHDI_TIMERCONFIG, &samples)) {
  91. ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %d sample timer ticks\n",
  92. samples);
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. static int dahdi_timer_ack(void *data, unsigned int quantity)
  98. {
  99. struct dahdi_timer *timer = data;
  100. return ioctl(timer->fd, DAHDI_TIMERACK, &quantity) ? -1 : 0;
  101. }
  102. static int dahdi_timer_enable_continuous(void *data)
  103. {
  104. struct dahdi_timer *timer = data;
  105. int flags = 1;
  106. return ioctl(timer->fd, DAHDI_TIMERPING, &flags) ? -1 : 0;
  107. }
  108. static int dahdi_timer_disable_continuous(void *data)
  109. {
  110. struct dahdi_timer *timer = data;
  111. int flags = -1;
  112. return ioctl(timer->fd, DAHDI_TIMERPONG, &flags) ? -1 : 0;
  113. }
  114. static enum ast_timer_event dahdi_timer_get_event(void *data)
  115. {
  116. struct dahdi_timer *timer = data;
  117. int res;
  118. int event;
  119. res = ioctl(timer->fd, DAHDI_GETEVENT, &event);
  120. if (res) {
  121. event = DAHDI_EVENT_TIMER_EXPIRED;
  122. }
  123. switch (event) {
  124. case DAHDI_EVENT_TIMER_PING:
  125. return AST_TIMING_EVENT_CONTINUOUS;
  126. case DAHDI_EVENT_TIMER_EXPIRED:
  127. default:
  128. return AST_TIMING_EVENT_EXPIRED;
  129. }
  130. }
  131. static unsigned int dahdi_timer_get_max_rate(void *data)
  132. {
  133. return 1000;
  134. }
  135. static int dahdi_timer_fd(void *data)
  136. {
  137. struct dahdi_timer *timer = data;
  138. return timer->fd;
  139. }
  140. #define SEE_TIMING "For more information on Asterisk timing modules, including ways to potentially fix this problem, please see https://wiki.asterisk.org/wiki/display/AST/Timing+Interfaces\n"
  141. static int dahdi_test_timer(void)
  142. {
  143. int fd;
  144. int x = 160;
  145. fd = open("/dev/dahdi/timer", O_RDWR);
  146. if (fd < 0) {
  147. return -1;
  148. }
  149. if (ioctl(fd, DAHDI_TIMERCONFIG, &x)) {
  150. ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer test failed to set DAHDI_TIMERCONFIG to %d.\n" SEE_TIMING, x);
  151. close(fd);
  152. return -1;
  153. }
  154. if ((x = ast_wait_for_input(fd, 300)) < 0) {
  155. ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer could not be polled during the DAHDI timer test.\n" SEE_TIMING);
  156. close(fd);
  157. return -1;
  158. }
  159. if (!x) {
  160. const char dahdi_timer_error[] = {
  161. "Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection. You have options:"
  162. "\n\t1. You only have to compile DAHDI support into Asterisk if you need it. One option is to recompile without DAHDI support."
  163. "\n\t2. You only have to load DAHDI drivers if you want to take advantage of DAHDI services. One option is to unload DAHDI modules if you don't need them."
  164. "\n\t3. If you need DAHDI services, you must correctly configure DAHDI."
  165. };
  166. ast_log(LOG_ERROR, "%s\n" SEE_TIMING, dahdi_timer_error);
  167. usleep(100);
  168. close(fd);
  169. return -1;
  170. }
  171. close(fd);
  172. return 0;
  173. }
  174. static int load_module(void)
  175. {
  176. if (dahdi_test_timer()) {
  177. return AST_MODULE_LOAD_DECLINE;
  178. }
  179. return (timing_funcs_handle = ast_register_timing_interface(&dahdi_timing)) ?
  180. AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
  181. }
  182. static int unload_module(void)
  183. {
  184. if (timing_funcs_handle) {
  185. return ast_unregister_timing_interface(timing_funcs_handle);
  186. }
  187. return 0;
  188. }
  189. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "DAHDI Timing Interface",
  190. .support_level = AST_MODULE_SUPPORT_CORE,
  191. .load = load_module,
  192. .unload = unload_module,
  193. .load_pri = AST_MODPRI_TIMING,
  194. );