7fb86dad7be294521900de6016571a3511581dd7
[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 1500
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 int as_chan_list_state;
64
65 static void defer_frame(struct ast_channel *chan, struct ast_frame *f)
66 {
67         struct ast_frame *dup_f;
68         struct asent *as;
69
70         AST_RWLIST_WRLOCK(&aslist);
71         AST_RWLIST_TRAVERSE(&aslist, as, list) {
72                 if (as->chan != chan)
73                         continue;
74                 if ((dup_f = ast_frdup(f)))
75                         AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
76         }
77         AST_RWLIST_UNLOCK(&aslist);
78 }
79
80 static void *autoservice_run(void *ign)
81 {
82         for (;;) {
83                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
84                 struct asent *as;
85                 int x = 0, ms = 500;
86
87                 AST_RWLIST_RDLOCK(&aslist);
88
89                 /* At this point, we know that no channels that have been removed are going
90                  * to get used again. */
91                 as_chan_list_state++;
92
93                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
94                         if (!ast_check_hangup(as->chan)) {
95                                 if (x < MAX_AUTOMONS)
96                                         mons[x++] = as->chan;
97                                 else
98                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
99                         }
100                 }
101                 AST_RWLIST_UNLOCK(&aslist);
102
103                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
104                         struct ast_frame *f = ast_read(chan);
105         
106                         if (!f) {
107                                 struct ast_frame hangup_frame = { 0, };
108                                 /* No frame means the channel has been hung up.
109                                  * A hangup frame needs to be queued here as ast_waitfor() may
110                                  * never return again for the condition to be detected outside
111                                  * of autoservice.  So, we'll leave a HANGUP queued up so the
112                                  * thread in charge of this channel will know. */
113
114                                 hangup_frame.frametype = AST_FRAME_CONTROL;
115                                 hangup_frame.subclass = AST_CONTROL_HANGUP;
116
117                                 defer_frame(chan, &hangup_frame);
118
119                                 continue;
120                         }
121                         
122                         /* Do not add a default entry in this switch statement.  Each new
123                          * frame type should be addressed directly as to whether it should
124                          * be queued up or not. */
125                         switch (f->frametype) {
126                         /* Save these frames */
127                         case AST_FRAME_DTMF_END:
128                         case AST_FRAME_CONTROL:
129                         case AST_FRAME_TEXT:
130                         case AST_FRAME_IMAGE:
131                         case AST_FRAME_HTML:
132                                 defer_frame(chan, f);
133                                 break;
134
135                         /* Throw these frames away */
136                         case AST_FRAME_DTMF_BEGIN:
137                         case AST_FRAME_VOICE:
138                         case AST_FRAME_VIDEO:
139                         case AST_FRAME_NULL:
140                         case AST_FRAME_IAX:
141                         case AST_FRAME_CNG:
142                         case AST_FRAME_MODEM:
143                                 break;
144                         }
145
146                         if (f)
147                                 ast_frfree(f);
148                 }
149         }
150
151         asthread = AST_PTHREADT_NULL;
152
153         return NULL;
154 }
155
156 int ast_autoservice_start(struct ast_channel *chan)
157 {
158         int res = 0;
159         struct asent *as;
160
161         AST_RWLIST_WRLOCK(&aslist);
162         AST_RWLIST_TRAVERSE(&aslist, as, list) {
163                 if (as->chan == chan) {
164                         as->use_count++;
165                         break;
166                 }
167         }
168         AST_RWLIST_UNLOCK(&aslist);
169
170         if (as) {
171                 /* Entry exists, autoservice is already handling this channel */
172                 return 0;
173         }
174
175         if (!(as = ast_calloc(1, sizeof(*as))))
176                 return -1;
177         
178         /* New entry created */
179         as->chan = chan;
180         as->use_count = 1;
181
182         ast_channel_lock(chan);
183         as->orig_end_dtmf_flag = ast_test_flag(chan, AST_FLAG_END_DTMF_ONLY) ? 1 : 0;
184         if (!as->orig_end_dtmf_flag)
185                 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
186         ast_channel_unlock(chan);
187
188         AST_RWLIST_WRLOCK(&aslist);
189         AST_RWLIST_INSERT_HEAD(&aslist, as, list);
190         AST_RWLIST_UNLOCK(&aslist);
191
192         if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
193                 if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
194                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
195                         /* There will only be a single member in the list at this point,
196                            the one we just added. */
197                         AST_RWLIST_WRLOCK(&aslist);
198                         AST_RWLIST_REMOVE(&aslist, as, list);
199                         AST_RWLIST_UNLOCK(&aslist);
200                         free(as);
201                         res = -1;
202                 } else
203                         pthread_kill(asthread, SIGURG);
204         }
205
206         return res;
207 }
208
209 int ast_autoservice_stop(struct ast_channel *chan)
210 {
211         int res = -1;
212         struct asent *as;
213         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
214         struct ast_frame *f;
215         int removed = 0;
216         int orig_end_dtmf_flag = 0;
217         int chan_list_state;
218
219         AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
220
221         AST_RWLIST_WRLOCK(&aslist);
222
223         /* Save the autoservice channel list state.  We _must_ verify that the channel
224          * list has been rebuilt before we return.  Because, after we return, the channel
225          * could get destroyed and we don't want our poor autoservice thread to step on
226          * it after its gone! */
227         chan_list_state = as_chan_list_state;
228
229         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {     
230                 if (as->chan == chan) {
231                         AST_RWLIST_REMOVE_CURRENT(list);
232                         as->use_count--;
233                         if (as->use_count)
234                                 break;
235                         AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
236                         orig_end_dtmf_flag = as->orig_end_dtmf_flag;
237                         ast_free(as);
238                         removed = 1;
239                         if (!ast_check_hangup(chan))
240                                 res = 0;
241                         break;
242                 }
243         }
244         AST_RWLIST_TRAVERSE_SAFE_END;
245
246         if (removed && asthread != AST_PTHREADT_NULL) 
247                 pthread_kill(asthread, SIGURG);
248         
249         AST_RWLIST_UNLOCK(&aslist);
250
251         if (!removed)
252                 return 0;
253
254         if (!orig_end_dtmf_flag)
255                 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
256
257         /* Wait for it to un-block */
258         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
259                 usleep(1000);
260
261         while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
262                 ast_queue_frame(chan, f);
263                 ast_frfree(f);
264         }
265
266         while (chan_list_state == as_chan_list_state)
267                 usleep(1000);
268
269         return res;
270 }