2 * Asterisk -- A telephony toolkit for Linux.
4 * Automatic channel service routines
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
21 #include <math.h> /* For PI */
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include "asterisk/pbx.h"
28 #include "asterisk/frame.h"
29 #include "asterisk/sched.h"
30 #include "asterisk/options.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/logger.h"
33 #include "asterisk/file.h"
34 #include "asterisk/translate.h"
35 #include "asterisk/manager.h"
36 #include "asterisk/chanvars.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/indications.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/utils.h"
42 #define MAX_AUTOMONS 256
44 AST_MUTEX_DEFINE_STATIC(autolock);
47 struct ast_channel *chan;
51 static struct asent *aslist = NULL;
52 static pthread_t asthread = AST_PTHREADT_NULL;
54 static void *autoservice_run(void *ign)
56 struct ast_channel *mons[MAX_AUTOMONS];
59 struct ast_channel *chan;
64 ast_mutex_lock(&autolock);
67 if (!as->chan->_softhangup) {
71 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
75 ast_mutex_unlock(&autolock);
80 chan = ast_waitfor_n(mons, x, &ms);
82 /* Read and ignore anything that occurs */
88 asthread = AST_PTHREADT_NULL;
92 int ast_autoservice_start(struct ast_channel *chan)
97 ast_mutex_lock(&autolock);
98 needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
101 if (as->chan == chan)
106 as = malloc(sizeof(struct asent));
108 memset(as, 0, sizeof(struct asent));
114 if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
115 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
120 pthread_kill(asthread, SIGURG);
124 ast_mutex_unlock(&autolock);
128 int ast_autoservice_stop(struct ast_channel *chan)
131 struct asent *as, *prev;
132 ast_mutex_lock(&autolock);
136 if (as->chan == chan)
143 prev->next = as->next;
147 if (!chan->_softhangup)
150 if (asthread != AST_PTHREADT_NULL)
151 pthread_kill(asthread, SIGURG);
152 ast_mutex_unlock(&autolock);
153 /* Wait for it to un-block */
154 while(ast_test_flag(chan, AST_FLAG_BLOCKING))