res_timing_dahdi.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 int dahdi_timer_open(void);
  40. static void dahdi_timer_close(int handle);
  41. static int dahdi_timer_set_rate(int handle, unsigned int rate);
  42. static int dahdi_timer_ack(int handle, unsigned int quantity);
  43. static int dahdi_timer_enable_continuous(int handle);
  44. static int dahdi_timer_disable_continuous(int handle);
  45. static enum ast_timer_event dahdi_timer_get_event(int handle);
  46. static unsigned int dahdi_timer_get_max_rate(int handle);
  47. static struct ast_timing_interface dahdi_timing = {
  48. .name = "DAHDI",
  49. .priority = 100,
  50. .timer_open = dahdi_timer_open,
  51. .timer_close = dahdi_timer_close,
  52. .timer_set_rate = dahdi_timer_set_rate,
  53. .timer_ack = dahdi_timer_ack,
  54. .timer_enable_continuous = dahdi_timer_enable_continuous,
  55. .timer_disable_continuous = dahdi_timer_disable_continuous,
  56. .timer_get_event = dahdi_timer_get_event,
  57. .timer_get_max_rate = dahdi_timer_get_max_rate,
  58. };
  59. static int dahdi_timer_open(void)
  60. {
  61. return open("/dev/dahdi/timer", O_RDWR);
  62. }
  63. static void dahdi_timer_close(int handle)
  64. {
  65. close(handle);
  66. }
  67. static int dahdi_timer_set_rate(int handle, unsigned int rate)
  68. {
  69. int samples;
  70. /* DAHDI timers are configured using a number of samples,
  71. * based on an 8 kHz sample rate. */
  72. samples = (unsigned int) roundf((8000.0 / ((float) rate)));
  73. if (ioctl(handle, DAHDI_TIMERCONFIG, &samples)) {
  74. ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %u sample timer ticks\n",
  75. samples);
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. static int dahdi_timer_ack(int handle, unsigned int quantity)
  81. {
  82. return ioctl(handle, DAHDI_TIMERACK, &quantity) ? -1 : 0;
  83. }
  84. static int dahdi_timer_enable_continuous(int handle)
  85. {
  86. int flags = 1;
  87. return ioctl(handle, DAHDI_TIMERPING, &flags) ? -1 : 0;
  88. }
  89. static int dahdi_timer_disable_continuous(int handle)
  90. {
  91. int flags = -1;
  92. return ioctl(handle, DAHDI_TIMERPONG, &flags) ? -1 : 0;
  93. }
  94. static enum ast_timer_event dahdi_timer_get_event(int handle)
  95. {
  96. int res;
  97. int event;
  98. res = ioctl(handle, DAHDI_GETEVENT, &event);
  99. if (res) {
  100. event = DAHDI_EVENT_TIMER_EXPIRED;
  101. }
  102. switch (event) {
  103. case DAHDI_EVENT_TIMER_PING:
  104. return AST_TIMING_EVENT_CONTINUOUS;
  105. case DAHDI_EVENT_TIMER_EXPIRED:
  106. default:
  107. return AST_TIMING_EVENT_EXPIRED;
  108. }
  109. }
  110. static unsigned int dahdi_timer_get_max_rate(int handle)
  111. {
  112. return 1000;
  113. }
  114. #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"
  115. static int dahdi_test_timer(void)
  116. {
  117. int fd;
  118. int x = 160;
  119. fd = open("/dev/dahdi/timer", O_RDWR);
  120. if (fd < 0) {
  121. return -1;
  122. }
  123. if (ioctl(fd, DAHDI_TIMERCONFIG, &x)) {
  124. 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);
  125. close(fd);
  126. return -1;
  127. }
  128. if ((x = ast_wait_for_input(fd, 300)) < 0) {
  129. 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);
  130. close(fd);
  131. return -1;
  132. }
  133. if (!x) {
  134. const char dahdi_timer_error[] = {
  135. "Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection. You have options:"
  136. "\n\t1. You only have to compile DAHDI support into Asterisk if you need it. One option is to recompile without DAHDI support."
  137. "\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."
  138. "\n\t3. If you need DAHDI services, you must correctly configure DAHDI."
  139. };
  140. ast_log(LOG_ERROR, "%s\n" SEE_TIMING, dahdi_timer_error);
  141. usleep(100);
  142. close(fd);
  143. return -1;
  144. }
  145. close(fd);
  146. return 0;
  147. }
  148. static int load_module(void)
  149. {
  150. if (dahdi_test_timer()) {
  151. return AST_MODULE_LOAD_DECLINE;
  152. }
  153. return (timing_funcs_handle = ast_register_timing_interface(&dahdi_timing)) ?
  154. AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
  155. }
  156. static int unload_module(void)
  157. {
  158. if (timing_funcs_handle) {
  159. return ast_unregister_timing_interface(timing_funcs_handle);
  160. }
  161. return 0;
  162. }
  163. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "DAHDI Timing Interface",
  164. .load = load_module,
  165. .unload = unload_module,
  166. .load_pri = AST_MODPRI_TIMING,
  167. );