Merged revisions 163448 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         /*! 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;
64 };
65
66 static AST_LIST_HEAD_STATIC(aslist, asent);
67 static ast_cond_t as_cond;
68
69 static pthread_t asthread = AST_PTHREADT_NULL;
70
71 static int as_chan_list_state;
72
73 static void *autoservice_run(void *ign)
74 {
75         for (;;) {
76                 struct ast_channel *mons[MAX_AUTOMONS];
77                 struct asent *ents[MAX_AUTOMONS];
78                 struct ast_channel *chan;
79                 struct asent *as;
80                 int i, x = 0, ms = 50;
81                 struct ast_frame *f = NULL;
82                 struct ast_frame *defer_frame = NULL;
83
84                 AST_LIST_LOCK(&aslist);
85
86                 /* At this point, we know that no channels that have been removed are going
87                  * to get used again. */
88                 as_chan_list_state++;
89
90                 if (AST_LIST_EMPTY(&aslist)) {
91                         ast_cond_wait(&as_cond, &aslist.lock);
92                 }
93
94                 AST_LIST_TRAVERSE(&aslist, as, list) {
95                         if (!ast_check_hangup(as->chan)) {
96                                 if (x < MAX_AUTOMONS) {
97                                         ents[x] = as;
98                                         mons[x++] = as->chan;
99                                 } else {
100                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
101                                 }
102                         }
103                 }
104
105                 AST_LIST_UNLOCK(&aslist);
106
107                 if (!x) {
108                         continue;
109                 }
110
111                 chan = ast_waitfor_n(mons, x, &ms);
112                 if (!chan) {
113                         continue;
114                 }
115
116                 f = ast_read(chan);
117         
118                 if (!f) {
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. */
125
126                         hangup_frame.frametype = AST_FRAME_CONTROL;
127                         hangup_frame.subclass = AST_CONTROL_HANGUP;
128
129                         defer_frame = &hangup_frame;
130                 } else {
131
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. */
135
136                         switch (f->frametype) {
137                         /* Save these frames */
138                         case AST_FRAME_DTMF_END:
139                         case AST_FRAME_CONTROL:
140                         case AST_FRAME_TEXT:
141                         case AST_FRAME_IMAGE:
142                         case AST_FRAME_HTML:
143                                 defer_frame = f;
144                                 break;
145
146                         /* Throw these frames away */
147                         case AST_FRAME_DTMF_BEGIN:
148                         case AST_FRAME_VOICE:
149                         case AST_FRAME_VIDEO:
150                         case AST_FRAME_NULL:
151                         case AST_FRAME_IAX:
152                         case AST_FRAME_CNG:
153                         case AST_FRAME_MODEM:
154                                 break;
155                         }
156                 }
157
158                 if (defer_frame) {
159                         for (i = 0; i < x; i++) {
160                                 struct ast_frame *dup_f;
161                                 
162                                 if (mons[i] != chan) {
163                                         continue;
164                                 }
165                                 
166                                 if ((dup_f = ast_frdup(defer_frame))) {
167                                         AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
168                                 }
169                                 
170                                 break;
171                         }
172                 }
173
174                 if (f) {
175                         ast_frfree(f);
176                 }
177         }
178
179         asthread = AST_PTHREADT_NULL;
180
181         return NULL;
182 }
183
184 int ast_autoservice_start(struct ast_channel *chan)
185 {
186         int res = 0;
187         struct asent *as;
188
189         AST_LIST_LOCK(&aslist);
190         AST_LIST_TRAVERSE(&aslist, as, list) {
191                 if (as->chan == chan) {
192                         as->use_count++;
193                         break;
194                 }
195         }
196         AST_LIST_UNLOCK(&aslist);
197
198         if (as) {
199                 /* Entry exists, autoservice is already handling this channel */
200                 return 0;
201         }
202
203         if (!(as = ast_calloc(1, sizeof(*as))))
204                 return -1;
205         
206         /* New entry created */
207         as->chan = chan;
208         as->use_count = 1;
209
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);
215
216         AST_LIST_LOCK(&aslist);
217
218         if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
219                 ast_cond_signal(&as_cond);
220         }
221
222         AST_LIST_INSERT_HEAD(&aslist, as, list);
223
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);
230                         free(as);
231                         asthread = AST_PTHREADT_NULL;
232                         res = -1;
233                 } else {
234                         pthread_kill(asthread, SIGURG);
235                 }
236         }
237
238         AST_LIST_UNLOCK(&aslist);
239
240         return res;
241 }
242
243 int ast_autoservice_stop(struct ast_channel *chan)
244 {
245         int res = -1;
246         struct asent *as, *removed = NULL;
247         struct ast_frame *f;
248         int chan_list_state;
249
250         AST_LIST_LOCK(&aslist);
251
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;
257
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) {
262                         as->use_count--;
263                         if (as->use_count < 1) {
264                                 AST_LIST_REMOVE_CURRENT(list);
265                                 removed = as;
266                         }
267                         break;
268                 }
269         }
270         AST_LIST_TRAVERSE_SAFE_END;
271
272         if (removed && asthread != AST_PTHREADT_NULL) {
273                 pthread_kill(asthread, SIGURG);
274         }
275
276         AST_LIST_UNLOCK(&aslist);
277
278         if (!removed) {
279                 return 0;
280         }
281
282         /* Wait while autoservice thread rebuilds its list. */
283         while (chan_list_state == as_chan_list_state) {
284                 usleep(1000);
285         }
286
287         /* Now autoservice thread should have no references to our entry
288            and we can safely destroy it */
289
290         if (!chan->_softhangup) {
291                 res = 0;
292         }
293
294         if (!as->orig_end_dtmf_flag) {
295                 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
296         }
297
298         ast_channel_lock(chan);
299         while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
300                 ast_queue_frame_head(chan, f);
301                 ast_frfree(f);
302         }
303         ast_channel_unlock(chan);
304
305         free(as);
306
307         return res;
308 }
309
310 void ast_autoservice_init(void)
311 {
312         ast_cond_init(&as_cond, NULL);
313 }