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 unsigned int orig_end_dtmf_flag:1;
55 AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
56 AST_LIST_ENTRY(asent) list;
59 static AST_RWLIST_HEAD_STATIC(aslist, asent);
61 static pthread_t asthread = AST_PTHREADT_NULL;
63 static void defer_frame(struct ast_channel *chan, struct ast_frame *f)
65 struct ast_frame *dup_f;
68 AST_RWLIST_WRLOCK(&aslist);
69 AST_RWLIST_TRAVERSE(&aslist, as, list) {
72 if ((dup_f = ast_frdup(f)))
73 AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
75 AST_RWLIST_UNLOCK(&aslist);
78 static void *autoservice_run(void *ign)
81 struct ast_channel *mons[MAX_AUTOMONS], *chan;
85 AST_RWLIST_RDLOCK(&aslist);
86 AST_RWLIST_TRAVERSE(&aslist, as, list) {
87 if (!ast_check_hangup(as->chan)) {
91 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
94 AST_RWLIST_UNLOCK(&aslist);
96 if ((chan = ast_waitfor_n(mons, x, &ms))) {
97 struct ast_frame *f = ast_read(chan);
100 struct ast_frame hangup_frame = { 0, };
101 /* No frame means the channel has been hung up.
102 * A hangup frame needs to be queued here as ast_waitfor() may
103 * never return again for the condition to be detected outside
104 * of autoservice. So, we'll leave a HANGUP queued up so the
105 * thread in charge of this channel will know. */
107 hangup_frame.frametype = AST_FRAME_CONTROL;
108 hangup_frame.subclass = AST_CONTROL_HANGUP;
110 defer_frame(chan, &hangup_frame);
115 /* Do not add a default entry in this switch statement. Each new
116 * frame type should be addressed directly as to whether it should
117 * be queued up or not. */
118 switch (f->frametype) {
119 /* Save these frames */
120 case AST_FRAME_DTMF_END:
121 case AST_FRAME_CONTROL:
123 case AST_FRAME_IMAGE:
125 defer_frame(chan, f);
128 /* Throw these frames away */
129 case AST_FRAME_DTMF_BEGIN:
130 case AST_FRAME_VOICE:
131 case AST_FRAME_VIDEO:
135 case AST_FRAME_MODEM:
144 asthread = AST_PTHREADT_NULL;
149 int ast_autoservice_start(struct ast_channel *chan)
154 AST_RWLIST_WRLOCK(&aslist);
155 AST_RWLIST_TRAVERSE(&aslist, as, list) {
156 if (as->chan == chan) {
161 AST_RWLIST_UNLOCK(&aslist);
164 /* Entry exists, autoservice is already handling this channel */
168 if (!(as = ast_calloc(1, sizeof(*as))))
171 /* New entry created */
175 ast_channel_lock(chan);
176 as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
177 if (!as->orig_end_dtmf_flag)
178 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
179 ast_channel_unlock(chan);
181 AST_RWLIST_WRLOCK(&aslist);
182 AST_RWLIST_INSERT_HEAD(&aslist, as, list);
183 AST_RWLIST_UNLOCK(&aslist);
185 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
186 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
187 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
188 /* There will only be a single member in the list at this point,
189 the one we just added. */
190 AST_RWLIST_WRLOCK(&aslist);
191 AST_RWLIST_REMOVE(&aslist, as, list);
192 AST_RWLIST_UNLOCK(&aslist);
196 pthread_kill(asthread, SIGURG);
202 int ast_autoservice_stop(struct ast_channel *chan)
206 AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
209 int orig_end_dtmf_flag = 0;
211 AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
213 AST_RWLIST_WRLOCK(&aslist);
214 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
215 if (as->chan == chan) {
216 AST_RWLIST_REMOVE_CURRENT(list);
220 AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
221 orig_end_dtmf_flag = as->orig_end_dtmf_flag;
224 if (!ast_check_hangup(chan))
229 AST_RWLIST_TRAVERSE_SAFE_END;
231 if (removed && asthread != AST_PTHREADT_NULL)
232 pthread_kill(asthread, SIGURG);
234 AST_RWLIST_UNLOCK(&aslist);
239 if (!orig_end_dtmf_flag)
240 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
242 /* Wait for it to un-block */
243 while (ast_test_flag(chan, AST_FLAG_BLOCKING))
246 while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
247 ast_queue_frame(chan, f);