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
22 #include <math.h> /* For PI */
23 #include <asterisk/pbx.h>
24 #include <asterisk/frame.h>
25 #include <asterisk/sched.h>
26 #include <asterisk/options.h>
27 #include <asterisk/channel.h>
28 #include <asterisk/channel_pvt.h>
29 #include <asterisk/logger.h>
30 #include <asterisk/file.h>
31 #include <asterisk/translate.h>
32 #include <asterisk/manager.h>
33 #include <asterisk/chanvars.h>
34 #include <asterisk/linkedlists.h>
35 #include <asterisk/indications.h>
37 #define MAX_AUTOMONS 256
39 static pthread_mutex_t autolock = AST_MUTEX_INITIALIZER;
42 struct ast_channel *chan;
46 static struct asent *aslist = NULL;
47 static pthread_t asthread = -1;
49 static void *autoservice_run(void *ign)
51 struct ast_channel *mons[MAX_AUTOMONS];
54 struct ast_channel *chan;
59 ast_pthread_mutex_lock(&autolock);
62 if (!as->chan->_softhangup) {
66 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
70 ast_pthread_mutex_unlock(&autolock);
75 chan = ast_waitfor_n(mons, x, &ms);
77 /* Read and ignore anything that occurs */
87 int ast_autoservice_start(struct ast_channel *chan)
92 ast_pthread_mutex_lock(&autolock);
93 needstart = (asthread == -1) ? 1 : 0 /* aslist ? 0 : 1 */;
101 as = malloc(sizeof(struct asent));
103 memset(as, 0, sizeof(struct asent));
109 if (pthread_create(&asthread, NULL, autoservice_run, NULL)) {
110 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
115 pthread_kill(asthread, SIGURG);
119 ast_pthread_mutex_unlock(&autolock);
123 int ast_autoservice_stop(struct ast_channel *chan)
126 struct asent *as, *prev;
127 ast_pthread_mutex_lock(&autolock);
131 if (as->chan == chan)
138 prev->next = as->next;
142 if (!chan->_softhangup)
146 pthread_kill(asthread, SIGURG);
147 ast_pthread_mutex_unlock(&autolock);
148 /* Wait for it to un-block */
149 while(chan->blocking)