2002-12-30 13:09:27 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2002-12-30 13:09:27 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2002-12-30 13:09:27 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* 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.
|
2002-12-30 13:09:27 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Automatic channel service routines
|
|
|
|
*
|
2002-12-30 13:09:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h> /* For PI */
|
2005-04-22 13:11:34 +00:00
|
|
|
|
2005-06-06 20:27:51 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
2005-06-06 22:12:19 +00:00
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
2005-06-06 20:27:51 +00:00
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/frame.h"
|
|
|
|
#include "asterisk/sched.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/translate.h"
|
|
|
|
#include "asterisk/manager.h"
|
|
|
|
#include "asterisk/chanvars.h"
|
|
|
|
#include "asterisk/linkedlists.h"
|
|
|
|
#include "asterisk/indications.h"
|
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/utils.h"
|
2002-12-30 13:09:27 +00:00
|
|
|
|
|
|
|
#define MAX_AUTOMONS 256
|
|
|
|
|
2004-06-09 01:45:08 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(autolock);
|
2002-12-30 13:09:27 +00:00
|
|
|
|
|
|
|
struct asent {
|
|
|
|
struct ast_channel *chan;
|
|
|
|
struct asent *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct asent *aslist = NULL;
|
2004-03-15 07:51:22 +00:00
|
|
|
static pthread_t asthread = AST_PTHREADT_NULL;
|
2002-12-30 13:09:27 +00:00
|
|
|
|
|
|
|
static void *autoservice_run(void *ign)
|
|
|
|
{
|
|
|
|
struct ast_channel *mons[MAX_AUTOMONS];
|
|
|
|
int x;
|
|
|
|
int ms;
|
|
|
|
struct ast_channel *chan;
|
|
|
|
struct asent *as;
|
|
|
|
struct ast_frame *f;
|
|
|
|
for(;;) {
|
|
|
|
x = 0;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&autolock);
|
2002-12-30 13:09:27 +00:00
|
|
|
as = aslist;
|
|
|
|
while(as) {
|
|
|
|
if (!as->chan->_softhangup) {
|
|
|
|
if (x < MAX_AUTOMONS)
|
|
|
|
mons[x++] = as->chan;
|
|
|
|
else
|
|
|
|
ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
|
|
|
|
}
|
|
|
|
as = as->next;
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&autolock);
|
2002-12-30 13:09:27 +00:00
|
|
|
|
|
|
|
/* if (!aslist)
|
|
|
|
break; */
|
|
|
|
ms = 500;
|
|
|
|
chan = ast_waitfor_n(mons, x, &ms);
|
|
|
|
if (chan) {
|
|
|
|
/* Read and ignore anything that occurs */
|
|
|
|
f = ast_read(chan);
|
|
|
|
if (f)
|
|
|
|
ast_frfree(f);
|
|
|
|
}
|
|
|
|
}
|
2004-03-15 07:51:22 +00:00
|
|
|
asthread = AST_PTHREADT_NULL;
|
2002-12-30 13:09:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_autoservice_start(struct ast_channel *chan)
|
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
struct asent *as;
|
|
|
|
int needstart;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&autolock);
|
2004-03-15 07:51:22 +00:00
|
|
|
needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
|
2002-12-30 13:09:27 +00:00
|
|
|
as = aslist;
|
|
|
|
while(as) {
|
|
|
|
if (as->chan == chan)
|
|
|
|
break;
|
|
|
|
as = as->next;
|
|
|
|
}
|
|
|
|
if (!as) {
|
|
|
|
as = malloc(sizeof(struct asent));
|
|
|
|
if (as) {
|
|
|
|
memset(as, 0, sizeof(struct asent));
|
|
|
|
as->chan = chan;
|
|
|
|
as->next = aslist;
|
|
|
|
aslist = as;
|
|
|
|
res = 0;
|
|
|
|
if (needstart) {
|
2004-08-08 17:15:02 +00:00
|
|
|
if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
|
2002-12-30 13:09:27 +00:00
|
|
|
ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
|
|
|
|
free(aslist);
|
|
|
|
aslist = NULL;
|
|
|
|
res = -1;
|
|
|
|
} else
|
|
|
|
pthread_kill(asthread, SIGURG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&autolock);
|
2002-12-30 13:09:27 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_autoservice_stop(struct ast_channel *chan)
|
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
struct asent *as, *prev;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&autolock);
|
2002-12-30 13:09:27 +00:00
|
|
|
as = aslist;
|
|
|
|
prev = NULL;
|
|
|
|
while(as) {
|
|
|
|
if (as->chan == chan)
|
|
|
|
break;
|
|
|
|
prev = as;
|
|
|
|
as = as->next;
|
|
|
|
}
|
|
|
|
if (as) {
|
|
|
|
if (prev)
|
|
|
|
prev->next = as->next;
|
|
|
|
else
|
|
|
|
aslist = as->next;
|
|
|
|
free(as);
|
|
|
|
if (!chan->_softhangup)
|
|
|
|
res = 0;
|
|
|
|
}
|
2004-03-15 07:51:22 +00:00
|
|
|
if (asthread != AST_PTHREADT_NULL)
|
2002-12-30 13:09:27 +00:00
|
|
|
pthread_kill(asthread, SIGURG);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&autolock);
|
2002-12-30 13:09:27 +00:00
|
|
|
/* Wait for it to un-block */
|
2004-12-07 20:38:43 +00:00
|
|
|
while(ast_test_flag(chan, AST_FLAG_BLOCKING))
|
2002-12-30 13:09:27 +00:00
|
|
|
usleep(1000);
|
|
|
|
return res;
|
|
|
|
}
|