2008-06-13 12:45:50 +00:00
/*
* Asterisk - - An open source telephony toolkit .
*
* Copyright ( C ) 2008 , Digium , Inc .
*
* Russell Bryant < russell @ digium . com >
*
* See http : //www.asterisk.org for more information about
* the Asterisk project . Please do not directly contact
* any of the maintainers of this project for assistance ;
* the project provides a web site , mailing lists and IRC
* channels for your use .
*
* This program is free software , distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree .
*/
/*!
* \ file
* \ author Russell Bryant < russell @ digium . com >
*
* \ brief DAHDI timing interface
*/
/*** MODULEINFO
< depend > dahdi < / depend >
2011-07-14 20:28:54 +00:00
< support_level > core < / support_level >
2008-06-13 12:45:50 +00:00
* * */
# include "asterisk.h"
ASTERISK_FILE_VERSION ( __FILE__ , " $Revision$ " ) ;
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <math.h>
2008-06-25 23:05:28 +00:00
# include <dahdi/user.h>
2008-06-13 12:45:50 +00:00
# include "asterisk/module.h"
# include "asterisk/timing.h"
# include "asterisk/utils.h"
static void * timing_funcs_handle ;
2014-02-07 20:01:45 +00:00
static void * dahdi_timer_open ( void ) ;
static void dahdi_timer_close ( void * data ) ;
static int dahdi_timer_set_rate ( void * data , unsigned int rate ) ;
static int dahdi_timer_ack ( void * data , unsigned int quantity ) ;
static int dahdi_timer_enable_continuous ( void * data ) ;
static int dahdi_timer_disable_continuous ( void * data ) ;
static enum ast_timer_event dahdi_timer_get_event ( void * data ) ;
static unsigned int dahdi_timer_get_max_rate ( void * data ) ;
static int dahdi_timer_fd ( void * data ) ;
2008-06-13 12:45:50 +00:00
2009-02-17 21:22:40 +00:00
static struct ast_timing_interface dahdi_timing = {
. name = " DAHDI " ,
. priority = 100 ,
2008-06-13 12:45:50 +00:00
. timer_open = dahdi_timer_open ,
. timer_close = dahdi_timer_close ,
. timer_set_rate = dahdi_timer_set_rate ,
. timer_ack = dahdi_timer_ack ,
. timer_enable_continuous = dahdi_timer_enable_continuous ,
. timer_disable_continuous = dahdi_timer_disable_continuous ,
. timer_get_event = dahdi_timer_get_event ,
2008-06-26 15:37:01 +00:00
. timer_get_max_rate = dahdi_timer_get_max_rate ,
2014-02-07 20:01:45 +00:00
. timer_fd = dahdi_timer_fd ,
2008-06-13 12:45:50 +00:00
} ;
2014-02-07 20:01:45 +00:00
struct dahdi_timer {
int fd ;
} ;
static void * dahdi_timer_open ( void )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer ;
if ( ! ( timer = ast_calloc ( 1 , sizeof ( * timer ) ) ) ) {
return NULL ;
}
if ( ( timer - > fd = open ( " /dev/dahdi/timer " , O_RDWR ) ) < 0 ) {
ast_log ( LOG_ERROR , " Failed to create dahdi timer: %s \n " , strerror ( errno ) ) ;
ast_free ( timer ) ;
return NULL ;
}
return timer ;
2008-06-13 12:45:50 +00:00
}
2014-02-07 20:01:45 +00:00
static void dahdi_timer_close ( void * data )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer = data ;
close ( timer - > fd ) ;
ast_free ( timer ) ;
2008-06-13 12:45:50 +00:00
}
2014-02-07 20:01:45 +00:00
static int dahdi_timer_set_rate ( void * data , unsigned int rate )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer = data ;
2008-06-13 12:45:50 +00:00
int samples ;
/* DAHDI timers are configured using a number of samples,
* based on an 8 kHz sample rate . */
samples = ( unsigned int ) roundf ( ( 8000.0 / ( ( float ) rate ) ) ) ;
2014-02-07 20:01:45 +00:00
if ( ioctl ( timer - > fd , DAHDI_TIMERCONFIG , & samples ) ) {
2014-05-09 22:49:26 +00:00
ast_log ( LOG_ERROR , " Failed to configure DAHDI timing fd for %d sample timer ticks \n " ,
2008-06-13 12:45:50 +00:00
samples ) ;
return - 1 ;
}
return 0 ;
}
2014-02-07 20:01:45 +00:00
static int dahdi_timer_ack ( void * data , unsigned int quantity )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer = data ;
return ioctl ( timer - > fd , DAHDI_TIMERACK , & quantity ) ? - 1 : 0 ;
2008-06-13 12:45:50 +00:00
}
2014-02-07 20:01:45 +00:00
static int dahdi_timer_enable_continuous ( void * data )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer = data ;
2008-06-13 12:45:50 +00:00
int flags = 1 ;
2014-02-07 20:01:45 +00:00
return ioctl ( timer - > fd , DAHDI_TIMERPING , & flags ) ? - 1 : 0 ;
2008-06-13 12:45:50 +00:00
}
2014-02-07 20:01:45 +00:00
static int dahdi_timer_disable_continuous ( void * data )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer = data ;
2008-06-13 12:45:50 +00:00
int flags = - 1 ;
2014-02-07 20:01:45 +00:00
return ioctl ( timer - > fd , DAHDI_TIMERPONG , & flags ) ? - 1 : 0 ;
2008-06-13 12:45:50 +00:00
}
2014-02-07 20:01:45 +00:00
static enum ast_timer_event dahdi_timer_get_event ( void * data )
2008-06-13 12:45:50 +00:00
{
2014-02-07 20:01:45 +00:00
struct dahdi_timer * timer = data ;
2008-06-13 12:45:50 +00:00
int res ;
int event ;
2014-02-07 20:01:45 +00:00
res = ioctl ( timer - > fd , DAHDI_GETEVENT , & event ) ;
2008-06-13 12:45:50 +00:00
if ( res ) {
event = DAHDI_EVENT_TIMER_EXPIRED ;
}
switch ( event ) {
case DAHDI_EVENT_TIMER_PING :
return AST_TIMING_EVENT_CONTINUOUS ;
case DAHDI_EVENT_TIMER_EXPIRED :
default :
return AST_TIMING_EVENT_EXPIRED ;
}
}
2014-02-07 20:01:45 +00:00
static unsigned int dahdi_timer_get_max_rate ( void * data )
2008-06-26 15:37:01 +00:00
{
return 1000 ;
}
2014-02-07 20:01:45 +00:00
static int dahdi_timer_fd ( void * data )
{
struct dahdi_timer * timer = data ;
return timer - > fd ;
}
2011-02-02 18:59:29 +00:00
# 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"
2009-03-03 20:59:16 +00:00
2008-06-13 12:45:50 +00:00
static int dahdi_test_timer ( void )
{
int fd ;
int x = 160 ;
fd = open ( " /dev/dahdi/timer " , O_RDWR ) ;
if ( fd < 0 ) {
return - 1 ;
}
if ( ioctl ( fd , DAHDI_TIMERCONFIG , & x ) ) {
2009-03-03 20:59:16 +00:00
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 ) ;
2008-06-13 12:45:50 +00:00
close ( fd ) ;
return - 1 ;
}
if ( ( x = ast_wait_for_input ( fd , 300 ) ) < 0 ) {
2009-03-03 20:59:16 +00:00
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 ) ;
2008-06-13 12:45:50 +00:00
close ( fd ) ;
return - 1 ;
}
if ( ! x ) {
const char dahdi_timer_error [ ] = {
" Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection. You have options: "
" \n \t 1. You only have to compile DAHDI support into Asterisk if you need it. One option is to recompile without DAHDI support. "
" \n \t 2. 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. "
" \n \t 3. If you need DAHDI services, you must correctly configure DAHDI. "
} ;
2009-03-03 20:59:16 +00:00
ast_log ( LOG_ERROR , " %s \n " SEE_TIMING , dahdi_timer_error ) ;
2008-06-13 12:45:50 +00:00
usleep ( 100 ) ;
close ( fd ) ;
return - 1 ;
}
close ( fd ) ;
return 0 ;
}
static int load_module ( void )
{
if ( dahdi_test_timer ( ) ) {
return AST_MODULE_LOAD_DECLINE ;
}
2009-02-17 21:22:40 +00:00
return ( timing_funcs_handle = ast_register_timing_interface ( & dahdi_timing ) ) ?
2008-06-13 12:45:50 +00:00
AST_MODULE_LOAD_SUCCESS : AST_MODULE_LOAD_DECLINE ;
}
static int unload_module ( void )
{
2009-05-31 01:22:15 +00:00
if ( timing_funcs_handle ) {
return ast_unregister_timing_interface ( timing_funcs_handle ) ;
}
return 0 ;
2008-06-13 12:45:50 +00:00
}
2009-06-09 16:22:04 +00:00
AST_MODULE_INFO ( ASTERISK_GPL_KEY , AST_MODFLAG_LOAD_ORDER , " DAHDI Timing Interface " ,
2014-07-25 16:47:17 +00:00
. support_level = AST_MODULE_SUPPORT_CORE ,
2009-06-09 16:22:04 +00:00
. load = load_module ,
. unload = unload_module ,
Fix timing source dependency issues with MOH
Prior to this patch, res_musiconhold existed at the same module priority level
as the timing sources that it depends on. This would cause a problem when
music on hold was reloaded, as the timing source could be changed after
res_musiconhold was processed. This patch adds a new module priority level,
AST_MODPRI_TIMING, that the various timing modules are now loaded at. This
now occurs before loading other resource modules, such that the timing source
is guaranteed to be set prior to resolving the timing source dependencies.
(closes issue ASTERISK-17474)
Reporter: Luke H
Tested by: Luke H, Vladimir Mikhelson, zzsurf, Wes Van Tlghem, elguero, Thomas Arimont
Patches:
asterisk-17474-dahdi_timing-infinite-wait-fix_v3_branch-1.8.diff uploaded by elguero (License #5026)
asterisk-17474-dahdi_timing-infinite-wait-fix_v3_branch-10.diff uploaded by elguero (License #5026)
asterisk-17474-dahdi_timing-infinite-wait-fix_v3.diff uploaded by elguero (License #5026)
Review: https://reviewboard.asterisk.org/r/1578/
........
Merged revisions 349194 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 349195 from http://svn.asterisk.org/svn/asterisk/branches/10
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@349196 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2011-12-27 20:55:15 +00:00
. load_pri = AST_MODPRI_TIMING ,
2009-06-09 16:22:04 +00:00
) ;