Mon Jun 27 16:50:56 2011

Asterisk developer's documentation


res_timing_dahdi.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! 
00020  * \file
00021  * \author Russell Bryant <russell@digium.com>
00022  *
00023  * \brief DAHDI timing interface 
00024  */
00025 
00026 /*** MODULEINFO
00027    <depend>dahdi</depend>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 305798 $");
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <fcntl.h>
00037 #include <math.h>
00038 
00039 #include <dahdi/user.h>
00040 
00041 #include "asterisk/module.h"
00042 #include "asterisk/timing.h"
00043 #include "asterisk/utils.h"
00044 
00045 static void *timing_funcs_handle;
00046 
00047 static int dahdi_timer_open(void);
00048 static void dahdi_timer_close(int handle);
00049 static int dahdi_timer_set_rate(int handle, unsigned int rate);
00050 static void dahdi_timer_ack(int handle, unsigned int quantity);
00051 static int dahdi_timer_enable_continuous(int handle);
00052 static int dahdi_timer_disable_continuous(int handle);
00053 static enum ast_timer_event dahdi_timer_get_event(int handle);
00054 static unsigned int dahdi_timer_get_max_rate(int handle);
00055 
00056 static struct ast_timing_interface dahdi_timing = {
00057    .name = "DAHDI",
00058    .priority = 100,
00059    .timer_open = dahdi_timer_open,
00060    .timer_close = dahdi_timer_close,
00061    .timer_set_rate = dahdi_timer_set_rate,
00062    .timer_ack = dahdi_timer_ack,
00063    .timer_enable_continuous = dahdi_timer_enable_continuous,
00064    .timer_disable_continuous = dahdi_timer_disable_continuous,
00065    .timer_get_event = dahdi_timer_get_event,
00066    .timer_get_max_rate = dahdi_timer_get_max_rate,
00067 };
00068 
00069 static int dahdi_timer_open(void)
00070 {
00071    return open("/dev/dahdi/timer", O_RDWR);
00072 }
00073 
00074 static void dahdi_timer_close(int handle)
00075 {
00076    close(handle);
00077 }
00078 
00079 static int dahdi_timer_set_rate(int handle, unsigned int rate)
00080 {
00081    int samples;
00082 
00083    /* DAHDI timers are configured using a number of samples,
00084     * based on an 8 kHz sample rate. */
00085    samples = (unsigned int) roundf((8000.0 / ((float) rate)));
00086 
00087    if (ioctl(handle, DAHDI_TIMERCONFIG, &samples)) {
00088       ast_log(LOG_ERROR, "Failed to configure DAHDI timing fd for %u sample timer ticks\n",
00089          samples);
00090       return -1;
00091    }
00092 
00093    return 0;
00094 }
00095 
00096 static void dahdi_timer_ack(int handle, unsigned int quantity)
00097 {
00098    ioctl(handle, DAHDI_TIMERACK, &quantity);
00099 }
00100 
00101 static int dahdi_timer_enable_continuous(int handle)
00102 {
00103    int flags = 1;
00104 
00105    return ioctl(handle, DAHDI_TIMERPING, &flags) ? -1 : 0;
00106 }
00107 
00108 static int dahdi_timer_disable_continuous(int handle)
00109 {
00110    int flags = -1;
00111 
00112    return ioctl(handle, DAHDI_TIMERPONG, &flags) ? -1 : 0;
00113 }
00114 
00115 static enum ast_timer_event dahdi_timer_get_event(int handle)
00116 {
00117    int res;
00118    int event;
00119 
00120    res = ioctl(handle, DAHDI_GETEVENT, &event);
00121 
00122    if (res) {
00123       event = DAHDI_EVENT_TIMER_EXPIRED;
00124    }
00125 
00126    switch (event) {
00127    case DAHDI_EVENT_TIMER_PING:
00128       return AST_TIMING_EVENT_CONTINUOUS;
00129    case DAHDI_EVENT_TIMER_EXPIRED:
00130    default:
00131       return AST_TIMING_EVENT_EXPIRED; 
00132    }
00133 }
00134 
00135 static unsigned int dahdi_timer_get_max_rate(int handle)
00136 {
00137    return 1000;
00138 }
00139 
00140 #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"
00141 
00142 static int dahdi_test_timer(void)
00143 {
00144    int fd;
00145    int x = 160;
00146    
00147    fd = open("/dev/dahdi/timer", O_RDWR);
00148 
00149    if (fd < 0) {
00150       return -1;
00151    }
00152 
00153    if (ioctl(fd, DAHDI_TIMERCONFIG, &x)) {
00154       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);
00155       close(fd);
00156       return -1;
00157    }
00158 
00159    if ((x = ast_wait_for_input(fd, 300)) < 0) {
00160       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);
00161       close(fd);
00162       return -1;
00163    }
00164 
00165    if (!x) {
00166       const char dahdi_timer_error[] = {
00167          "Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection.  You have options:"
00168          "\n\t1. You only have to compile DAHDI support into Asterisk if you need it.  One option is to recompile without DAHDI support."
00169          "\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."
00170          "\n\t3. If you need DAHDI services, you must correctly configure DAHDI."
00171       };
00172       ast_log(LOG_ERROR, "%s\n" SEE_TIMING, dahdi_timer_error);
00173       usleep(100);
00174       close(fd);
00175       return -1;
00176    }
00177 
00178    close(fd);
00179 
00180    return 0;
00181 }
00182 
00183 static int load_module(void)
00184 {
00185    if (dahdi_test_timer()) {
00186       return AST_MODULE_LOAD_DECLINE;
00187    }
00188 
00189    return (timing_funcs_handle = ast_register_timing_interface(&dahdi_timing)) ?
00190       AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE;
00191 }
00192 
00193 static int unload_module(void)
00194 {
00195    if (timing_funcs_handle) {
00196       return ast_unregister_timing_interface(timing_funcs_handle);
00197    }
00198 
00199    return 0;
00200 }
00201 
00202 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "DAHDI Timing Interface",
00203       .load = load_module,
00204       .unload = unload_module,
00205       .load_pri = AST_MODPRI_CHANNEL_DEPEND,
00206       );

Generated on Mon Jun 27 16:50:56 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7