2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Automatic channel service routines
23 * \author Mark Spencer <markster@digium.com>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/pbx.h"
39 #include "asterisk/frame.h"
40 #include "asterisk/sched.h"
41 #include "asterisk/options.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/file.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/manager.h"
47 #include "asterisk/chanvars.h"
48 #include "asterisk/linkedlists.h"
49 #include "asterisk/indications.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/utils.h"
53 #define MAX_AUTOMONS 256
56 struct ast_channel *chan;
57 AST_LIST_ENTRY(asent) list;
60 static AST_LIST_HEAD_STATIC(aslist, asent);
62 static pthread_t asthread = AST_PTHREADT_NULL;
64 static void *autoservice_run(void *ign)
68 struct ast_channel *mons[MAX_AUTOMONS];
69 struct ast_channel *chan;
73 AST_LIST_LOCK(&aslist);
74 AST_LIST_TRAVERSE(&aslist, as, list) {
75 if (!as->chan->_softhangup) {
79 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
82 AST_LIST_UNLOCK(&aslist);
84 chan = ast_waitfor_n(mons, x, &ms);
86 /* Read and ignore anything that occurs */
87 struct ast_frame *f = ast_read(chan);
92 asthread = AST_PTHREADT_NULL;
96 int ast_autoservice_start(struct ast_channel *chan)
100 AST_LIST_LOCK(&aslist);
102 /* Check if the channel already has autoservice */
103 AST_LIST_TRAVERSE(&aslist, as, list) {
104 if (as->chan == chan)
107 /* XXX if found, we return -1, why ??? */
109 /* If not, start autoservice on channel */
111 as = calloc(1, sizeof(struct asent));
114 AST_LIST_INSERT_HEAD(&aslist, as, list);
116 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
117 if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
118 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
119 /* There will only be a single member in the list at this point,
120 the one we just added. */
121 AST_LIST_REMOVE(&aslist, as, list);
125 pthread_kill(asthread, SIGURG);
129 AST_LIST_UNLOCK(&aslist);
133 int ast_autoservice_stop(struct ast_channel *chan)
138 AST_LIST_LOCK(&aslist);
139 AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
140 if (as->chan == chan) {
141 AST_LIST_REMOVE_CURRENT(&aslist, list);
143 if (!chan->_softhangup)
148 AST_LIST_TRAVERSE_SAFE_END
150 if (asthread != AST_PTHREADT_NULL)
151 pthread_kill(asthread, SIGURG);
152 AST_LIST_UNLOCK(&aslist);
154 /* Wait for it to un-block */
155 while(ast_test_flag(chan, AST_FLAG_BLOCKING))