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