Merged revisions 91777 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         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
55         AST_LIST_ENTRY(asent) list;
56 };
57
58 static AST_RWLIST_HEAD_STATIC(aslist, asent);
59
60 static pthread_t asthread = AST_PTHREADT_NULL;
61
62 static void defer_frame(struct ast_channel *chan, struct ast_frame *f)
63 {
64         struct ast_frame *dup_f;
65         struct asent *as;
66
67         AST_RWLIST_WRLOCK(&aslist);
68         AST_RWLIST_TRAVERSE(&aslist, as, list) {
69                 if (as->chan != chan)
70                         continue;
71                 if ((dup_f = ast_frdup(f)))
72                         AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
73         }
74         AST_RWLIST_UNLOCK(&aslist);
75 }
76
77 static void *autoservice_run(void *ign)
78 {
79         for (;;) {
80                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
81                 struct asent *as;
82                 int x = 0, ms = 500;
83
84                 AST_RWLIST_RDLOCK(&aslist);
85                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
86                         if (!ast_check_hangup(as->chan)) {
87                                 if (x < MAX_AUTOMONS)
88                                         mons[x++] = as->chan;
89                                 else
90                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
91                         }
92                 }
93                 AST_RWLIST_UNLOCK(&aslist);
94
95                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
96                         struct ast_frame *f = ast_read(chan);
97         
98                         if (!f) {
99                                 struct ast_frame hangup_frame = { 0, };
100                                 /* No frame means the channel has been hung up.
101                                  * A hangup frame needs to be queued here as ast_waitfor() may
102                                  * never return again for the condition to be detected outside
103                                  * of autoservice.  So, we'll leave a HANGUP queued up so the
104                                  * thread in charge of this channel will know. */
105
106                                 hangup_frame.frametype = AST_FRAME_CONTROL;
107                                 hangup_frame.subclass = AST_CONTROL_HANGUP;
108
109                                 defer_frame(chan, &hangup_frame);
110
111                                 continue;
112                         }
113                         
114                         /* Do not add a default entry in this switch statement.  Each new
115                          * frame type should be addressed directly as to whether it should
116                          * be queued up or not. */
117                         switch (f->frametype) {
118                         /* Save these frames */
119                         case AST_FRAME_DTMF_BEGIN:
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_VOICE:
130                         case AST_FRAME_VIDEO:
131                         case AST_FRAME_NULL:
132                         case AST_FRAME_IAX:
133                         case AST_FRAME_CNG:
134                         case AST_FRAME_MODEM:
135                                 break;
136                         }
137
138                         if (f)
139                                 ast_frfree(f);
140                 }
141         }
142
143         asthread = AST_PTHREADT_NULL;
144
145         return NULL;
146 }
147
148 int ast_autoservice_start(struct ast_channel *chan)
149 {
150         int res = 0;
151         struct asent *as;
152
153         AST_RWLIST_WRLOCK(&aslist);
154
155         /* Check if the channel already has autoservice */
156         AST_RWLIST_TRAVERSE(&aslist, as, list) {
157                 if (as->chan == chan) {
158                         as->use_count++;
159                         break;
160                 }
161         }
162
163         /* If not, start autoservice on channel */
164         if (as) {
165                 /* Entry extist, autoservice is already handling this channel */
166         } else if ((as = ast_calloc(1, sizeof(*as))) == NULL) {
167                 /* Memory allocation failed */
168                 res = -1;
169         } else {
170                 /* New entry created */
171                 as->chan = chan;
172                 as->use_count = 1;
173                 AST_RWLIST_INSERT_HEAD(&aslist, as, list);
174                 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
175                         if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
176                                 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
177                                 /* There will only be a single member in the list at this point,
178                                    the one we just added. */
179                                 AST_RWLIST_REMOVE(&aslist, as, list);
180                                 ast_free(as);
181                                 res = -1;
182                         } else
183                                 pthread_kill(asthread, SIGURG);
184                 }
185         }
186
187         AST_RWLIST_UNLOCK(&aslist);
188
189         return res;
190 }
191
192 int ast_autoservice_stop(struct ast_channel *chan)
193 {
194         int res = -1;
195         struct asent *as;
196         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
197         struct ast_frame *f;
198         int removed = 0;
199
200         AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
201
202         AST_RWLIST_WRLOCK(&aslist);
203         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {     
204                 if (as->chan == chan) {
205                         AST_RWLIST_REMOVE_CURRENT(list);
206                         as->use_count--;
207                         if (as->use_count)
208                                 break;
209                         AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
210                         ast_free(as);
211                         removed = 1;
212                         if (!ast_check_hangup(chan))
213                                 res = 0;
214                         break;
215                 }
216         }
217         AST_RWLIST_TRAVERSE_SAFE_END;
218
219         if (removed && asthread != AST_PTHREADT_NULL) 
220                 pthread_kill(asthread, SIGURG);
221         
222         AST_RWLIST_UNLOCK(&aslist);
223
224         if (!removed)
225                 return 0;
226
227         /* Wait for it to un-block */
228         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
229                 usleep(1000);
230
231         while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
232                 ast_queue_frame(chan, f);
233                 ast_frfree(f);
234         }
235
236         return res;
237 }