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_timing_event dahdi_timer_get_event(int handle);
55 static struct ast_timing_functions dahdi_timing_functions = {
56 .timer_open = dahdi_timer_open,
57 .timer_close = dahdi_timer_close,
58 .timer_set_rate = dahdi_timer_set_rate,
59 .timer_ack = dahdi_timer_ack,
60 .timer_enable_continuous = dahdi_timer_enable_continuous,
61 .timer_disable_continuous = dahdi_timer_disable_continuous,
62 .timer_get_event = dahdi_timer_get_event,
65 static int dahdi_timer_open(void)
67 return open("/dev/dahdi/timer", O_RDWR);
70 static void dahdi_timer_close(int handle)
75 static int dahdi_timer_set_rate(int handle, unsigned int rate)
79 /* DAHDI timers are configured using a number of samples,
80 * based on an 8 kHz sample rate. */
81 samples = (unsigned int) roundf((8000.0 / ((float) rate)));
83 if (ioctl(handle, DAHDI_TIMERCONFIG, &samples)) {
84 ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %u sample timer ticks\n",
92 static void dahdi_timer_ack(int handle, unsigned int quantity)
94 ioctl(handle, DAHDI_TIMERACK, &quantity);
97 static int dahdi_timer_enable_continuous(int handle)
101 return ioctl(handle, DAHDI_TIMERPING, &flags) ? -1 : 0;
104 static int dahdi_timer_disable_continuous(int handle)
108 return ioctl(handle, DAHDI_TIMERPONG, &flags) ? -1 : 0;
111 static enum ast_timing_event dahdi_timer_get_event(int handle)
116 res = ioctl(handle, DAHDI_GETEVENT, &event);
119 event = DAHDI_EVENT_TIMER_EXPIRED;
123 case DAHDI_EVENT_TIMER_PING:
124 return AST_TIMING_EVENT_CONTINUOUS;
125 case DAHDI_EVENT_TIMER_EXPIRED:
127 return AST_TIMING_EVENT_EXPIRED;
131 static int dahdi_test_timer(void)
136 fd = open("/dev/dahdi/timer", O_RDWR);
142 if (ioctl(fd, DAHDI_TIMERCONFIG, &x)) {
143 ast_log(LOG_ERROR, "You have DAHDI built and drivers loaded, but the DAHDI timer test failed to set DAHDI_TIMERCONFIG to %d.\n", x);
148 if ((x = ast_wait_for_input(fd, 300)) < 0) {
149 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");
155 const char dahdi_timer_error[] = {
156 "Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection. You have options:"
157 "\n\t1. You only have to compile DAHDI support into Asterisk if you need it. One option is to recompile without DAHDI support."
158 "\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."
159 "\n\t3. If you need DAHDI services, you must correctly configure DAHDI."
161 ast_log(LOG_ERROR, "%s\n", dahdi_timer_error);
172 static int load_module(void)
174 if (dahdi_test_timer()) {
175 return AST_MODULE_LOAD_DECLINE;
178 return (timing_funcs_handle = ast_install_timing_functions(&dahdi_timing_functions)) ?
179 AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
182 static int unload_module(void)
184 /* ast_uninstall_timing_functions(timing_funcs_handle); */
186 /* This module can not currently be unloaded. No use count handling is being done. */
191 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "DAHDI Timing Interface");