Merged revisions 89790 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 *autoservice_run(void *ign)
63 {
64
65         for (;;) {
66                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
67                 struct asent *as;
68                 int x = 0, ms = 500;
69
70                 AST_RWLIST_RDLOCK(&aslist);
71                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
72                         if (!ast_check_hangup(as->chan)) {
73                                 if (x < MAX_AUTOMONS)
74                                         mons[x++] = as->chan;
75                                 else
76                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
77                         }
78                 }
79                 AST_RWLIST_UNLOCK(&aslist);
80
81                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
82                         struct ast_frame *f = ast_read(chan);
83                         
84                         /* Do not add a default entry in this switch statement.  Each new
85                          * frame type should be addressed directly as to whether it should
86                          * be queued up or not. */
87                         switch (f->frametype) {
88                         /* Save these frames */
89                         case AST_FRAME_DTMF_BEGIN:
90                         case AST_FRAME_DTMF_END:
91                         case AST_FRAME_CONTROL:
92                         case AST_FRAME_TEXT:
93                         case AST_FRAME_IMAGE:
94                         case AST_FRAME_HTML:
95                         {
96                                 struct ast_frame *dup_f;
97
98                                 AST_RWLIST_WRLOCK(&aslist);
99                                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
100                                         if (as->chan != chan)
101                                                 continue;
102                                         if ((dup_f = ast_frdup(f)))
103                                                 AST_LIST_INSERT_TAIL(&as->dtmf_frames, dup_f, frame_list);
104                                 }
105                                 AST_RWLIST_UNLOCK(&aslist);
106                         }
107
108                         /* Throw these frames away */
109                         case AST_FRAME_VOICE:
110                         case AST_FRAME_VIDEO:
111                         case AST_FRAME_NULL:
112                         case AST_FRAME_IAX:
113                         case AST_FRAME_CNG:
114                         case AST_FRAME_MODEM:
115                                 break;
116                         }
117
118                         if (f)
119                                 ast_frfree(f);
120                 }
121         }
122
123         asthread = AST_PTHREADT_NULL;
124
125         return NULL;
126 }
127
128 int ast_autoservice_start(struct ast_channel *chan)
129 {
130         int res = -1;
131         struct asent *as;
132
133         AST_RWLIST_WRLOCK(&aslist);
134
135         /* Check if the channel already has autoservice */
136         AST_RWLIST_TRAVERSE(&aslist, as, list) {
137                 if (as->chan == chan) {
138                         as->use_count++;
139                         break;
140                 }
141         }
142
143         /* If not, start autoservice on channel */
144         if (!as && (as = ast_calloc(1, sizeof(*as)))) {
145                 as->chan = chan;
146                 as->use_count = 1;
147                 AST_RWLIST_INSERT_HEAD(&aslist, as, list);
148                 res = 0;
149                 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
150                         if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
151                                 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
152                                 /* There will only be a single member in the list at this point,
153                                    the one we just added. */
154                                 AST_RWLIST_REMOVE(&aslist, as, list);
155                                 ast_free(as);
156                                 res = -1;
157                         } else
158                                 pthread_kill(asthread, SIGURG);
159                 }
160         }
161
162         AST_RWLIST_UNLOCK(&aslist);
163
164         return res;
165 }
166
167 int ast_autoservice_stop(struct ast_channel *chan)
168 {
169         int res = -1;
170         struct asent *as;
171         AST_LIST_HEAD_NOLOCK(, ast_frame) dtmf_frames;
172         struct ast_frame *f;
173         int removed = 1;
174
175         AST_LIST_HEAD_INIT_NOLOCK(&dtmf_frames);
176
177         AST_RWLIST_WRLOCK(&aslist);
178         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {     
179                 if (as->chan == chan) {
180                         AST_RWLIST_REMOVE_CURRENT(list);
181                         as->use_count--;
182                         if (as->use_count) {
183                                 removed = 0;
184                                 break;
185                         }
186                         AST_LIST_APPEND_LIST(&dtmf_frames, &as->dtmf_frames, frame_list);
187                         ast_free(as);
188                         if (!ast_check_hangup(chan))
189                                 res = 0;
190                         break;
191                 }
192         }
193         AST_RWLIST_TRAVERSE_SAFE_END;
194
195         if (removed && asthread != AST_PTHREADT_NULL) 
196                 pthread_kill(asthread, SIGURG);
197         
198         AST_RWLIST_UNLOCK(&aslist);
199
200         if (!removed)
201                 return 0;
202
203         /* Wait for it to un-block */
204         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
205                 usleep(1000);
206
207         while ((f = AST_LIST_REMOVE_HEAD(&dtmf_frames, frame_list))) {
208                 ast_queue_frame(chan, f);
209                 ast_frfree(f);
210         }
211
212         return res;
213 }