Merged revisions 110395 via svnmerge from
[asterisk/asterisk.git] / main / autoservice.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * Russell Bryant <russell@digium.com>
8  *
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.
14  *
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.
18  */
19
20 /*! \file
21  *
22  * \brief Automatic channel service routines
23  *
24  * \author Mark Spencer <markster@digium.com> 
25  * \author Russell Bryant <russell@digium.com>
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <sys/time.h>
33 #include <signal.h>
34
35 #include "asterisk/_private.h" /* prototype for ast_autoservice_init() */
36
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"
49
50 #define MAX_AUTOMONS 1500
51
52 struct asent {
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         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
60         AST_LIST_ENTRY(asent) list;
61 };
62
63 static AST_LIST_HEAD_STATIC(aslist, asent);
64 static ast_cond_t as_cond;
65
66 static pthread_t asthread = AST_PTHREADT_NULL;
67
68 static int as_chan_list_state;
69
70 static void defer_frame(struct ast_channel *chan, struct ast_frame *f)
71 {
72         struct ast_frame *dup_f;
73         struct asent *as;
74
75         AST_LIST_LOCK(&aslist);
76         AST_LIST_TRAVERSE(&aslist, as, list) {
77                 if (as->chan != chan)
78                         continue;
79                 if ((dup_f = ast_frdup(f)))
80                         AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
81         }
82         AST_LIST_UNLOCK(&aslist);
83 }
84
85 static void *autoservice_run(void *ign)
86 {
87         for (;;) {
88                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
89                 struct asent *as;
90                 int x = 0, ms = 50;
91
92                 AST_LIST_LOCK(&aslist);
93
94                 /* At this point, we know that no channels that have been removed are going
95                  * to get used again. */
96                 as_chan_list_state++;
97
98                 if (AST_LIST_EMPTY(&aslist))
99                         ast_cond_wait(&as_cond, &aslist.lock);
100
101                 AST_LIST_TRAVERSE(&aslist, as, list) {
102                         if (!ast_check_hangup(as->chan)) {
103                                 if (x < MAX_AUTOMONS)
104                                         mons[x++] = as->chan;
105                                 else
106                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
107                         }
108                 }
109
110                 AST_LIST_UNLOCK(&aslist);
111
112                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
113                         struct ast_frame *f = ast_read(chan);
114         
115                         if (!f) {
116                                 struct ast_frame hangup_frame = { 0, };
117                                 /* No frame means the channel has been hung up.
118                                  * A hangup frame needs to be queued here as ast_waitfor() may
119                                  * never return again for the condition to be detected outside
120                                  * of autoservice.  So, we'll leave a HANGUP queued up so the
121                                  * thread in charge of this channel will know. */
122
123                                 hangup_frame.frametype = AST_FRAME_CONTROL;
124                                 hangup_frame.subclass = AST_CONTROL_HANGUP;
125
126                                 defer_frame(chan, &hangup_frame);
127
128                                 continue;
129                         }
130                         
131                         /* Do not add a default entry in this switch statement.  Each new
132                          * frame type should be addressed directly as to whether it should
133                          * be queued up or not. */
134                         switch (f->frametype) {
135                         /* Save these frames */
136                         case AST_FRAME_DTMF_END:
137                         case AST_FRAME_CONTROL:
138                         case AST_FRAME_TEXT:
139                         case AST_FRAME_IMAGE:
140                         case AST_FRAME_HTML:
141                                 defer_frame(chan, f);
142                                 break;
143
144                         /* Throw these frames away */
145                         case AST_FRAME_DTMF_BEGIN:
146                         case AST_FRAME_VOICE:
147                         case AST_FRAME_VIDEO:
148                         case AST_FRAME_NULL:
149                         case AST_FRAME_IAX:
150                         case AST_FRAME_CNG:
151                         case AST_FRAME_MODEM:
152                                 break;
153                         }
154
155                         if (f)
156                                 ast_frfree(f);
157                 }
158         }
159
160         asthread = AST_PTHREADT_NULL;
161
162         return NULL;
163 }
164
165 int ast_autoservice_start(struct ast_channel *chan)
166 {
167         int res = 0;
168         struct asent *as;
169
170         AST_LIST_LOCK(&aslist);
171         AST_LIST_TRAVERSE(&aslist, as, list) {
172                 if (as->chan == chan) {
173                         as->use_count++;
174                         break;
175                 }
176         }
177         AST_LIST_UNLOCK(&aslist);
178
179         if (as) {
180                 /* Entry exists, autoservice is already handling this channel */
181                 return 0;
182         }
183
184         if (!(as = ast_calloc(1, sizeof(*as))))
185                 return -1;
186         
187         /* New entry created */
188         as->chan = chan;
189         as->use_count = 1;
190
191         ast_channel_lock(chan);
192         as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
193         if (!as->orig_end_dtmf_flag)
194                 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
195         ast_channel_unlock(chan);
196
197         AST_LIST_LOCK(&aslist);
198         if (AST_LIST_EMPTY(&aslist))
199                 ast_cond_signal(&as_cond);
200         AST_LIST_INSERT_HEAD(&aslist, as, list);
201         AST_LIST_UNLOCK(&aslist);
202
203         if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
204                 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
205                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
206                         /* There will only be a single member in the list at this point,
207                            the one we just added. */
208                         AST_LIST_LOCK(&aslist);
209                         AST_LIST_REMOVE(&aslist, as, list);
210                         AST_LIST_UNLOCK(&aslist);
211                         free(as);
212                         res = -1;
213                 } else
214                         pthread_kill(asthread, SIGURG);
215         }
216
217         return res;
218 }
219
220 int ast_autoservice_stop(struct ast_channel *chan)
221 {
222         int res = -1;
223         struct asent *as;
224         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
225         struct ast_frame *f;
226         int removed = 0;
227         int orig_end_dtmf_flag = 0;
228         int chan_list_state;
229
230         AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
231
232         AST_LIST_LOCK(&aslist);
233
234         /* Save the autoservice channel list state.  We _must_ verify that the channel
235          * list has been rebuilt before we return.  Because, after we return, the channel
236          * could get destroyed and we don't want our poor autoservice thread to step on
237          * it after its gone! */
238         chan_list_state = as_chan_list_state;
239
240         AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {       
241                 if (as->chan == chan) {
242                         AST_LIST_REMOVE_CURRENT(list);
243                         as->use_count--;
244                         if (as->use_count)
245                                 break;
246                         AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
247                         orig_end_dtmf_flag = as->orig_end_dtmf_flag;
248                         ast_free(as);
249                         removed = 1;
250                         if (!ast_check_hangup(chan))
251                                 res = 0;
252                         break;
253                 }
254         }
255         AST_LIST_TRAVERSE_SAFE_END;
256
257         if (removed && asthread != AST_PTHREADT_NULL) 
258                 pthread_kill(asthread, SIGURG);
259         
260         AST_LIST_UNLOCK(&aslist);
261
262         if (!removed)
263                 return 0;
264
265         if (!orig_end_dtmf_flag)
266                 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
267
268         /* Wait for it to un-block */
269         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
270                 usleep(1000);
271
272         while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
273                 ast_queue_frame(chan, f);
274                 ast_frfree(f);
275         }
276
277         while (chan_list_state == as_chan_list_state)
278                 usleep(1000);
279
280         return res;
281 }
282
283 void ast_autoservice_init(void)
284 {
285         ast_cond_init(&as_cond, NULL);
286 }