2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
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.
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.
21 * \author Russell Bryant <russell@digium.com>
23 * \brief DAHDI timing interface
27 <depend>dahdi</depend>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
34 #include <sys/types.h>
39 #include <dahdi/user.h>
41 #include "asterisk/module.h"
42 #include "asterisk/timing.h"
43 #include "asterisk/utils.h"
45 static void *timing_funcs_handle;
47 static int dahdi_timer_open(void);
48 static void dahdi_timer_close(int handle);
49 static int dahdi_timer_set_rate(int handle, unsigned int rate);
50 static void dahdi_timer_ack(int handle, unsigned int quantity);
51 static int dahdi_timer_enable_continuous(int handle);
52 static int dahdi_timer_disable_continuous(int handle);
53 static enum ast_timer_event dahdi_timer_get_event(int handle);
54 static unsigned int dahdi_timer_get_max_rate(int handle);
56 static struct ast_timing_interface dahdi_timing = {
59 .timer_open = dahdi_timer_open,
60 .timer_close = dahdi_timer_close,
61 .timer_set_rate = dahdi_timer_set_rate,
62 .timer_ack = dahdi_timer_ack,
63 .timer_enable_continuous = dahdi_timer_enable_continuous,
64 .timer_disable_continuous = dahdi_timer_disable_continuous,
65 .timer_get_event = dahdi_timer_get_event,
66 .timer_get_max_rate = dahdi_timer_get_max_rate,
69 static int dahdi_timer_open(void)
71 return open("/dev/dahdi/timer", O_RDWR);
74 static void dahdi_timer_close(int handle)
79 static int dahdi_timer_set_rate(int handle, unsigned int rate)
83 /* DAHDI timers are configured using a number of samples,
84 * based on an 8 kHz sample rate. */
85 samples = (unsigned int) roundf((8000.0 / ((float) rate)));
87 if (ioctl(handle, DAHDI_TIMERCONFIG, &samples)) {
88 ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %u sample timer ticks\n",
96 static void dahdi_timer_ack(int handle, unsigned int quantity)
98 ioctl(handle, DAHDI_TIMERACK, &quantity);
101 static int dahdi_timer_enable_continuous(int handle)
105 return ioctl(handle, DAHDI_TIMERPING, &flags) ? -1 : 0;
108 static int dahdi_timer_disable_continuous(int handle)
112 return ioctl(handle, DAHDI_TIMERPONG, &flags) ? -1 : 0;
115 static enum ast_timer_event dahdi_timer_get_event(int handle)
120 res = ioctl(handle, DAHDI_GETEVENT, &event);
123 event = DAHDI_EVENT_TIMER_EXPIRED;
127 case DAHDI_EVENT_TIMER_PING:
128 return AST_TIMING_EVENT_CONTINUOUS;
129 case DAHDI_EVENT_TIMER_EXPIRED:
131 return AST_TIMING_EVENT_EXPIRED;
135 static unsigned int dahdi_timer_get_max_rate(int handle)
140 #define SEE_TIMING "For more information on Asterisk timing modules, including ways to potentially fix this problem, please see doc/timing.txt\n"
142 static int dahdi_test_timer(void)
147 fd = open("/dev/dahdi/timer", O_RDWR);
153 if (ioctl(fd, DAHDI_TIMERCONFIG, &x)) {
154 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);
159 if ((x = ast_wait_for_input(fd, 300)) < 0) {
160 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);
166 const char dahdi_timer_error[] = {
167 "Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection. You have options:"
168 "\n\t1. You only have to compile DAHDI support into Asterisk if you need it. One option is to recompile without DAHDI support."
169 "\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."
170 "\n\t3. If you need DAHDI services, you must correctly configure DAHDI."
172 ast_log(LOG_ERROR, "%s\n" SEE_TIMING, dahdi_timer_error);
183 static int load_module(void)
185 if (dahdi_test_timer()) {
186 return AST_MODULE_LOAD_DECLINE;
189 return (timing_funcs_handle = ast_register_timing_interface(&dahdi_timing)) ?
190 AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
193 static int unload_module(void)
195 return ast_unregister_timing_interface(timing_funcs_handle);
198 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DAHDI Timing Interface");