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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/pbx.h"
34 #include "asterisk/frame.h"
35 #include "asterisk/sched.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/file.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/chanvars.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/indications.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
46 #define MAX_AUTOMONS 256
49 struct ast_channel *chan;
50 /*! This gets incremented each time autoservice gets started on the same
51 * channel. It will ensure that it doesn't actually get stopped until
52 * it gets stopped for the last time. */
53 unsigned int use_count;
54 AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
55 AST_LIST_ENTRY(asent) list;
58 static AST_RWLIST_HEAD_STATIC(aslist, asent);
60 static pthread_t asthread = AST_PTHREADT_NULL;
62 static void *autoservice_run(void *ign)
66 struct ast_channel *mons[MAX_AUTOMONS], *chan;
70 AST_RWLIST_RDLOCK(&aslist);
71 AST_RWLIST_TRAVERSE(&aslist, as, list) {
72 if (!ast_check_hangup(as->chan)) {
76 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
79 AST_RWLIST_UNLOCK(&aslist);
81 if ((chan = ast_waitfor_n(mons, x, &ms))) {
82 struct ast_frame *f = ast_read(chan);
87 /* Do not add a default entry in this switch statement. Each new
88 * frame type should be addressed directly as to whether it should
89 * be queued up or not. */
90 switch (f->frametype) {
91 /* Save these frames */
92 case AST_FRAME_DTMF_BEGIN:
93 case AST_FRAME_DTMF_END:
94 case AST_FRAME_CONTROL:
99 struct ast_frame *dup_f;
101 AST_RWLIST_WRLOCK(&aslist);
102 AST_RWLIST_TRAVERSE(&aslist, as, list) {
103 if (as->chan != chan)
105 if ((dup_f = ast_frdup(f)))
106 AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
108 AST_RWLIST_UNLOCK(&aslist);
111 /* Throw these frames away */
112 case AST_FRAME_VOICE:
113 case AST_FRAME_VIDEO:
117 case AST_FRAME_MODEM:
126 asthread = AST_PTHREADT_NULL;
131 int ast_autoservice_start(struct ast_channel *chan)
136 AST_RWLIST_WRLOCK(&aslist);
138 /* Check if the channel already has autoservice */
139 AST_RWLIST_TRAVERSE(&aslist, as, list) {
140 if (as->chan == chan) {
146 /* If not, start autoservice on channel */
148 /* Entry extist, autoservice is already handling this channel */
149 } else if ((as = ast_calloc(1, sizeof(*as))) == NULL) {
150 /* Memory allocation failed */
153 /* New entry created */
156 AST_RWLIST_INSERT_HEAD(&aslist, as, list);
157 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
158 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
159 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
160 /* There will only be a single member in the list at this point,
161 the one we just added. */
162 AST_RWLIST_REMOVE(&aslist, as, list);
166 pthread_kill(asthread, SIGURG);
170 AST_RWLIST_UNLOCK(&aslist);
175 int ast_autoservice_stop(struct ast_channel *chan)
179 AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
183 AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
185 AST_RWLIST_WRLOCK(&aslist);
186 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
187 if (as->chan == chan) {
188 AST_RWLIST_REMOVE_CURRENT(list);
192 AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
195 if (!ast_check_hangup(chan))
200 AST_RWLIST_TRAVERSE_SAFE_END;
202 if (removed && asthread != AST_PTHREADT_NULL)
203 pthread_kill(asthread, SIGURG);
205 AST_RWLIST_UNLOCK(&aslist);
210 /* Wait for it to un-block */
211 while (ast_test_flag(chan, AST_FLAG_BLOCKING))
214 while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
215 ast_queue_frame(chan, f);