2008-06-12 14:21:32 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Kevin P. Fleming <kpfleming@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
|
|
|
|
*
|
|
|
|
* \brief Timing source management
|
|
|
|
*
|
|
|
|
* \author Kevin P. Fleming <kpfleming@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
|
|
|
#include "asterisk/timing.h"
|
|
|
|
#include "asterisk/lock.h"
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
AST_RWLOCK_DEFINE_STATIC(lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
static struct ast_timing_functions timer_funcs;
|
|
|
|
|
|
|
|
void *ast_install_timing_functions(struct ast_timing_functions *funcs)
|
|
|
|
{
|
|
|
|
if (!funcs->timer_open ||
|
|
|
|
!funcs->timer_close ||
|
2008-06-13 12:45:50 +00:00
|
|
|
!funcs->timer_set_rate ||
|
2008-06-12 14:21:32 +00:00
|
|
|
!funcs->timer_ack ||
|
|
|
|
!funcs->timer_get_event ||
|
|
|
|
!funcs->timer_enable_continuous ||
|
|
|
|
!funcs->timer_disable_continuous) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_wrlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (timer_funcs.timer_open) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
|
|
|
ast_log(LOG_NOTICE, "Multiple timing modules are loaded. You should only load one.\n");
|
2008-06-12 14:21:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_funcs = *funcs;
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
return &timer_funcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_uninstall_timing_functions(void *handle)
|
|
|
|
{
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_wrlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (handle != &timer_funcs) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&timer_funcs, 0, sizeof(timer_funcs));
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
int ast_timer_open(void)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
|
|
|
int timer;
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_rdlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (!timer_funcs.timer_open) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
timer = timer_funcs.timer_open();
|
2008-06-12 14:21:32 +00:00
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_timer_close(int timer)
|
|
|
|
{
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_rdlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (!timer_funcs.timer_close) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_funcs.timer_close(timer);
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_timer_set_rate(int handle, unsigned int rate)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
ast_rwlock_rdlock(&lock);
|
|
|
|
|
|
|
|
if (!timer_funcs.timer_set_rate) {
|
|
|
|
ast_rwlock_unlock(&lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = timer_funcs.timer_set_rate(handle, rate);
|
|
|
|
|
|
|
|
ast_rwlock_unlock(&lock);
|
|
|
|
|
|
|
|
return res;
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ast_timer_ack(int handle, unsigned int quantity)
|
|
|
|
{
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_rdlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (!timer_funcs.timer_ack) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_funcs.timer_ack(handle, quantity);
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_timer_enable_continuous(int handle)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_rdlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (!timer_funcs.timer_enable_continuous) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = timer_funcs.timer_enable_continuous(handle);
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-06-16 12:48:11 +00:00
|
|
|
int ast_timer_disable_continuous(int handle)
|
2008-06-12 14:21:32 +00:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_rdlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (!timer_funcs.timer_disable_continuous) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = timer_funcs.timer_disable_continuous(handle);
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ast_timing_event ast_timer_get_event(int handle)
|
|
|
|
{
|
|
|
|
enum ast_timing_event result;
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_rdlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
if (!timer_funcs.timer_get_event) {
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = timer_funcs.timer_get_event(handle);
|
|
|
|
|
2008-06-13 12:45:50 +00:00
|
|
|
ast_rwlock_unlock(&lock);
|
2008-06-12 14:21:32 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|