Remove ability to control T.38 FAX error correction from udptl.conf.
[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 (defer_frame != f) {
167                                         if ((dup_f = ast_frdup(defer_frame))) {
168                                                 AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
169                                         }
170                                 } else {
171                                         if ((dup_f = ast_frisolate(defer_frame))) {
172                                                 if (dup_f != defer_frame) {
173                                                         ast_frfree(defer_frame);
174                                                 }
175                                                 AST_LIST_INSERT_HEAD(&ents[i]->deferred_frames, dup_f, frame_list);
176                                         }
177                                 }
178                                 
179                                 break;
180                         }
181                 } else if (f) {
182                         ast_frfree(f);
183                 }
184         }
185
186         asthread = AST_PTHREADT_NULL;
187
188         return NULL;
189 }
190
191 int ast_autoservice_start(struct ast_channel *chan)
192 {
193         int res = 0;
194         struct asent *as;
195
196         AST_LIST_LOCK(&aslist);
197         AST_LIST_TRAVERSE(&aslist, as, list) {
198                 if (as->chan == chan) {
199                         as->use_count++;
200                         break;
201                 }
202         }
203         AST_LIST_UNLOCK(&aslist);
204
205         if (as) {
206                 /* Entry exists, autoservice is already handling this channel */
207                 return 0;
208         }
209
210         if (!(as = ast_calloc(1, sizeof(*as))))
211                 return -1;
212         
213         /* New entry created */
214         as->chan = chan;
215         as->use_count = 1;
216
217         ast_channel_lock(chan);
218         as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
219         if (!as->orig_end_dtmf_flag)
220                 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
221         ast_channel_unlock(chan);
222
223         AST_LIST_LOCK(&aslist);
224
225         if (AST_LIST_EMPTY(&aslist) && asthread != AST_PTHREADT_NULL) {
226                 ast_cond_signal(&as_cond);
227         }
228
229         AST_LIST_INSERT_HEAD(&aslist, as, list);
230
231         if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
232                 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
233                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
234                         /* There will only be a single member in the list at this point,
235                            the one we just added. */
236                         AST_LIST_REMOVE(&aslist, as, list);
237                         free(as);
238                         asthread = AST_PTHREADT_NULL;
239                         res = -1;
240                 } else {
241                         pthread_kill(asthread, SIGURG);
242                 }
243         }
244
245         AST_LIST_UNLOCK(&aslist);
246
247         return res;
248 }
249
250 int ast_autoservice_stop(struct ast_channel *chan)
251 {
252         int res = -1;
253         struct asent *as, *removed = NULL;
254         struct ast_frame *f;
255         int chan_list_state;
256
257         AST_LIST_LOCK(&aslist);
258
259         /* Save the autoservice channel list state.  We _must_ verify that the channel
260          * list has been rebuilt before we return.  Because, after we return, the channel
261          * could get destroyed and we don't want our poor autoservice thread to step on
262          * it after its gone! */
263         chan_list_state = as_chan_list_state;
264
265         /* Find the entry, but do not free it because it still can be in the
266            autoservice thread array */
267         AST_LIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {       
268                 if (as->chan == chan) {
269                         as->use_count--;
270                         if (as->use_count < 1) {
271                                 AST_LIST_REMOVE_CURRENT(list);
272                                 removed = as;
273                         }
274                         break;
275                 }
276         }
277         AST_LIST_TRAVERSE_SAFE_END;
278
279         if (removed && asthread != AST_PTHREADT_NULL) {
280                 pthread_kill(asthread, SIGURG);
281         }
282
283         AST_LIST_UNLOCK(&aslist);
284
285         if (!removed) {
286                 return 0;
287         }
288
289         /* Wait while autoservice thread rebuilds its list. */
290         while (chan_list_state == as_chan_list_state) {
291                 usleep(1000);
292         }
293
294         /* Now autoservice thread should have no references to our entry
295            and we can safely destroy it */
296
297         if (!chan->_softhangup) {
298                 res = 0;
299         }
300
301         if (!as->orig_end_dtmf_flag) {
302                 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
303         }
304
305         ast_channel_lock(chan);
306         while ((f = AST_LIST_REMOVE_HEAD(&as->deferred_frames, frame_list))) {
307                 ast_queue_frame_head(chan, f);
308                 ast_frfree(f);
309         }
310         ast_channel_unlock(chan);
311
312         free(as);
313
314         return res;
315 }
316
317 void ast_autoservice_init(void)
318 {
319         ast_cond_init(&as_cond, NULL);
320 }