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