Merged revisions 94801 via svnmerge from
[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/pbx.h"
34 #include "asterisk/frame.h"
35 #include "asterisk/sched.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/file.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/chanvars.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/indications.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
45
46 #define MAX_AUTOMONS 256
47
48 struct asent {
49         struct ast_channel *chan;
50         /*! This gets incremented each time autoservice gets started on the same
51          *  channel.  It will ensure that it doesn't actually get stopped until 
52          *  it gets stopped for the last time. */
53         unsigned int use_count;
54         unsigned int orig_end_dtmf_flag:1;
55         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
56         AST_LIST_ENTRY(asent) list;
57 };
58
59 static AST_RWLIST_HEAD_STATIC(aslist, asent);
60
61 static pthread_t asthread = AST_PTHREADT_NULL;
62
63 static void defer_frame(struct ast_channel *chan, struct ast_frame *f)
64 {
65         struct ast_frame *dup_f;
66         struct asent *as;
67
68         AST_RWLIST_WRLOCK(&aslist);
69         AST_RWLIST_TRAVERSE(&aslist, as, list) {
70                 if (as->chan != chan)
71                         continue;
72                 if ((dup_f = ast_frdup(f)))
73                         AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
74         }
75         AST_RWLIST_UNLOCK(&aslist);
76 }
77
78 static void *autoservice_run(void *ign)
79 {
80         for (;;) {
81                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
82                 struct asent *as;
83                 int x = 0, ms = 500;
84
85                 AST_RWLIST_RDLOCK(&aslist);
86                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
87                         if (!ast_check_hangup(as->chan)) {
88                                 if (x < MAX_AUTOMONS)
89                                         mons[x++] = as->chan;
90                                 else
91                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
92                         }
93                 }
94                 AST_RWLIST_UNLOCK(&aslist);
95
96                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
97                         struct ast_frame *f = ast_read(chan);
98         
99                         if (!f) {
100                                 struct ast_frame hangup_frame = { 0, };
101                                 /* No frame means the channel has been hung up.
102                                  * A hangup frame needs to be queued here as ast_waitfor() may
103                                  * never return again for the condition to be detected outside
104                                  * of autoservice.  So, we'll leave a HANGUP queued up so the
105                                  * thread in charge of this channel will know. */
106
107                                 hangup_frame.frametype = AST_FRAME_CONTROL;
108                                 hangup_frame.subclass = AST_CONTROL_HANGUP;
109
110                                 defer_frame(chan, &hangup_frame);
111
112                                 continue;
113                         }
114                         
115                         /* Do not add a default entry in this switch statement.  Each new
116                          * frame type should be addressed directly as to whether it should
117                          * be queued up or not. */
118                         switch (f->frametype) {
119                         /* Save these frames */
120                         case AST_FRAME_DTMF_END:
121                         case AST_FRAME_CONTROL:
122                         case AST_FRAME_TEXT:
123                         case AST_FRAME_IMAGE:
124                         case AST_FRAME_HTML:
125                                 defer_frame(chan, f);
126                                 break;
127
128                         /* Throw these frames away */
129                         case AST_FRAME_DTMF_BEGIN:
130                         case AST_FRAME_VOICE:
131                         case AST_FRAME_VIDEO:
132                         case AST_FRAME_NULL:
133                         case AST_FRAME_IAX:
134                         case AST_FRAME_CNG:
135                         case AST_FRAME_MODEM:
136                                 break;
137                         }
138
139                         if (f)
140                                 ast_frfree(f);
141                 }
142         }
143
144         asthread = AST_PTHREADT_NULL;
145
146         return NULL;
147 }
148
149 int ast_autoservice_start(struct ast_channel *chan)
150 {
151         int res = 0;
152         struct asent *as;
153
154         AST_RWLIST_WRLOCK(&aslist);
155         AST_RWLIST_TRAVERSE(&aslist, as, list) {
156                 if (as->chan == chan) {
157                         as->use_count++;
158                         break;
159                 }
160         }
161         AST_RWLIST_UNLOCK(&aslist);
162
163         if (as) {
164                 /* Entry exists, autoservice is already handling this channel */
165                 return 0;
166         }
167
168         if (!(as = ast_calloc(1, sizeof(*as))))
169                 return -1;
170         
171         /* New entry created */
172         as->chan = chan;
173         as->use_count = 1;
174
175         ast_channel_lock(chan);
176         as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
177         if (!as->orig_end_dtmf_flag)
178                 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
179         ast_channel_unlock(chan);
180
181         AST_RWLIST_WRLOCK(&aslist);
182         AST_RWLIST_INSERT_HEAD(&aslist, as, list);
183         AST_RWLIST_UNLOCK(&aslist);
184
185         if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
186                 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
187                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
188                         /* There will only be a single member in the list at this point,
189                            the one we just added. */
190                         AST_RWLIST_WRLOCK(&aslist);
191                         AST_RWLIST_REMOVE(&aslist, as, list);
192                         AST_RWLIST_UNLOCK(&aslist);
193                         free(as);
194                         res = -1;
195                 } else
196                         pthread_kill(asthread, SIGURG);
197         }
198
199         return res;
200 }
201
202 int ast_autoservice_stop(struct ast_channel *chan)
203 {
204         int res = -1;
205         struct asent *as;
206         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
207         struct ast_frame *f;
208         int removed = 0;
209         int orig_end_dtmf_flag = 0;
210
211         AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
212
213         AST_RWLIST_WRLOCK(&aslist);
214         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {     
215                 if (as->chan == chan) {
216                         AST_RWLIST_REMOVE_CURRENT(list);
217                         as->use_count--;
218                         if (as->use_count)
219                                 break;
220                         AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
221                         orig_end_dtmf_flag = as->orig_end_dtmf_flag;
222                         ast_free(as);
223                         removed = 1;
224                         if (!ast_check_hangup(chan))
225                                 res = 0;
226                         break;
227                 }
228         }
229         AST_RWLIST_TRAVERSE_SAFE_END;
230
231         if (removed && asthread != AST_PTHREADT_NULL) 
232                 pthread_kill(asthread, SIGURG);
233         
234         AST_RWLIST_UNLOCK(&aslist);
235
236         if (!removed)
237                 return 0;
238
239         if (!orig_end_dtmf_flag)
240                 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
241
242         /* Wait for it to un-block */
243         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
244                 usleep(1000);
245
246         while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
247                 ast_queue_frame(chan, f);
248                 ast_frfree(f);
249         }
250
251         return res;
252 }