2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Russell Bryant <russell@digium.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Automatic channel service routines
24 * \author Mark Spencer <markster@digium.com>
25 * \author Russell Bryant <russell@digium.com>
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
37 #include "asterisk/pbx.h"
38 #include "asterisk/frame.h"
39 #include "asterisk/sched.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/file.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/manager.h"
44 #include "asterisk/chanvars.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/indications.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/utils.h"
50 #define MAX_AUTOMONS 1500
53 struct ast_channel *chan;
54 /*! This gets incremented each time autoservice gets started on the same
55 * channel. It will ensure that it doesn't actually get stopped until
56 * it gets stopped for the last time. */
57 unsigned int use_count;
58 unsigned int orig_end_dtmf_flag:1;
59 /*! Frames go on at the head of deferred_frames, so we have the frames
60 * from newest to oldest. As we put them at the head of the readq, we'll
61 * end up with them in the right order for the channel's readq. */
62 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
63 AST_LIST_ENTRY(asent) list;
66 static AST_LIST_HEAD_STATIC(aslist, asent);
67 static ast_cond_t as_cond;
69 static pthread_t asthread = AST_PTHREADT_NULL;
71 static int as_chan_list_state;
73 static void *autoservice_run(void *ign)
76 struct ast_channel *mons[MAX_AUTOMONS];
77 struct asent *ents[MAX_AUTOMONS];
78 struct ast_channel *chan;
80 int i, x = 0, ms = 50;
81 struct ast_frame *f = NULL;
82 struct ast_frame *defer_frame = NULL;
84 AST_LIST_LOCK(&aslist);
86 /* At this point, we know that no channels that have been removed are going
87 * to get used again. */
90 if (AST_LIST_EMPTY(&aslist)) {
91 ast_cond_wait(&as_cond, &aslist.lock);
94 AST_LIST_TRAVERSE(&aslist, as, list) {
95 if (!ast_check_hangup(as->chan)) {
96 if (x < MAX_AUTOMONS) {
100 ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events. Fix autoservice.c\n");
105 AST_LIST_UNLOCK(&aslist);
111 chan = ast_waitfor_n(mons, x, &ms);
119 struct ast_frame hangup_frame = { 0, };
120 /* No frame means the channel has been hung up.
121 * A hangup frame needs to be queued here as ast_waitfor() may
122 * never return again for the condition to be detected outside
123 * of autoservice. So, we'll leave a HANGUP queued up so the
124 * thread in charge of this channel will know. */
126 hangup_frame.frametype = AST_FRAME_CONTROL;
127 hangup_frame.subclass = AST_CONTROL_HANGUP;
129 defer_frame = &hangup_frame;
132 /* Do not add a default entry in this switch statement. Each new
133 * frame type should be addressed directly as to whether it should
134 * be queued up or not. */
136 switch (f->frametype) {
137 /* Save these frames */
138 case AST_FRAME_DTMF_END:
139 case AST_FRAME_CONTROL:
141 case AST_FRAME_IMAGE:
146 /* Throw these frames away */
147 case AST_FRAME_DTMF_BEGIN:
148 case AST_FRAME_VOICE:
149 case AST_FRAME_VIDEO:
153 case AST_FRAME_MODEM:
159 for (i = 0; i < x; i++) {
160 struct ast_frame *dup_f;
162 if (mons[i] != chan) {
166 if ((dup_f = ast_frdup(defer_frame))) {
167 AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
179 asthread = AST_PTHREADT_NULL;
184 int ast_autoservice_start(struct ast_channel *chan)
189 AST_LIST_LOCK(&aslist);
190 AST_LIST_TRAVERSE(&aslist, as, list) {
191 if (as->chan == chan) {
196 AST_LIST_UNLOCK(&aslist);
199 /* Entry exists, autoservice is already handling this channel */
203 if (!(as = ast_calloc(1, sizeof(*as))))
206 /* New entry created */
210 ast_channel_lock(chan);
211 as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
212 if (!as->orig_end_dtmf_flag)
213 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
214 ast_channel_unlock(chan);
216 AST_LIST_LOCK(&aslist);
218 if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
219 ast_cond_signal(&as_cond);
222 AST_LIST_INSERT_HEAD(&aslist, as, list);
224 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
225 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
226 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
227 /* There will only be a single member in the list at this point,
228 the one we just added. */
229 AST_LIST_REMOVE(&aslist, as, list);
231 asthread = AST_PTHREADT_NULL;
234 pthread_kill(asthread, SIGURG);
238 AST_LIST_UNLOCK(&aslist);
243 int ast_autoservice_stop(struct ast_channel *chan)
246 struct asent *as, *removed = NULL;
250 AST_LIST_LOCK(&aslist);
252 /* Save the autoservice channel list state. We _must_ verify that the channel
253 * list has been rebuilt before we return. Because, after we return, the channel
254 * could get destroyed and we don't want our poor autoservice thread to step on
255 * it after its gone! */
256 chan_list_state = as_chan_list_state;
258 /* Find the entry, but do not free it because it still can be in the
259 autoservice thread array */
260 AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {
261 if (as->chan == chan) {
263 if (as->use_count < 1) {
264 AST_LIST_REMOVE_CURRENT(list);
270 AST_LIST_TRAVERSE_SAFE_END;
272 if (removed && asthread != AST_PTHREADT_NULL) {
273 pthread_kill(asthread, SIGURG);
276 AST_LIST_UNLOCK(&aslist);
282 /* Wait while autoservice thread rebuilds its list. */
283 while (chan_list_state == as_chan_list_state) {
287 /* Now autoservice thread should have no references to our entry
288 and we can safely destroy it */
290 if (!chan->_softhangup) {
294 if (!as->orig_end_dtmf_flag) {
295 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
298 ast_channel_lock(chan);
299 while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
300 ast_queue_frame_head(chan, f);
303 ast_channel_unlock(chan);
310 void ast_autoservice_init(void)
312 ast_cond_init(&as_cond, NULL);