remove a bunch of useless #include "options.h"
[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         AST_LIST_ENTRY(asent) list;
51 };
52
53 static AST_RWLIST_HEAD_STATIC(aslist, asent);
54
55 static pthread_t asthread = AST_PTHREADT_NULL;
56
57 static void *autoservice_run(void *ign)
58 {
59
60         for (;;) {
61                 struct ast_channel *mons[MAX_AUTOMONS], *chan;
62                 struct asent *as;
63                 int x = 0, ms = 500;
64
65                 AST_RWLIST_RDLOCK(&aslist);
66                 AST_RWLIST_TRAVERSE(&aslist, as, list) {
67                         if (!ast_check_hangup(as->chan)) {
68                                 if (x < MAX_AUTOMONS)
69                                         mons[x++] = as->chan;
70                                 else
71                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
72                         }
73                 }
74                 AST_RWLIST_UNLOCK(&aslist);
75
76                 if ((chan = ast_waitfor_n(mons, x, &ms))) {
77                         /* Read and ignore anything that occurs */
78                         struct ast_frame *f = ast_read(chan);
79                         if (f)
80                                 ast_frfree(f);
81                 }
82         }
83
84         asthread = AST_PTHREADT_NULL;
85
86         return NULL;
87 }
88
89 int ast_autoservice_start(struct ast_channel *chan)
90 {
91         int res = -1;
92         struct asent *as;
93
94         AST_RWLIST_WRLOCK(&aslist);
95
96         /* Check if the channel already has autoservice */
97         AST_RWLIST_TRAVERSE(&aslist, as, list) {
98                 if (as->chan == chan)
99                         break;
100         }
101
102         /* If not, start autoservice on channel */
103         if (!as && (as = ast_calloc(1, sizeof(*as)))) {
104                 as->chan = chan;
105                 AST_RWLIST_INSERT_HEAD(&aslist, as, list);
106                 res = 0;
107                 if (asthread == AST_PTHREADT_NULL) { /* need start the thread */
108                         if (ast_pthread_create_background(&asthread, NULL, autoservice_run, NULL)) {
109                                 ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
110                                 /* There will only be a single member in the list at this point,
111                                    the one we just added. */
112                                 AST_RWLIST_REMOVE(&aslist, as, list);
113                                 ast_free(as);
114                                 res = -1;
115                         } else
116                                 pthread_kill(asthread, SIGURG);
117                 }
118         }
119         AST_RWLIST_UNLOCK(&aslist);
120         return res;
121 }
122
123 int ast_autoservice_stop(struct ast_channel *chan)
124 {
125         int res = -1;
126         struct asent *as;
127
128         AST_RWLIST_WRLOCK(&aslist);
129         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&aslist, as, list) {     
130                 if (as->chan == chan) {
131                         AST_RWLIST_REMOVE_CURRENT(list);
132                         ast_free(as);
133                         if (!ast_check_hangup(chan))
134                                 res = 0;
135                         break;
136                 }
137         }
138         AST_RWLIST_TRAVERSE_SAFE_END;
139
140         if (asthread != AST_PTHREADT_NULL) 
141                 pthread_kill(asthread, SIGURG);
142         AST_RWLIST_UNLOCK(&aslist);
143
144         /* Wait for it to un-block */
145         while (ast_test_flag(chan, AST_FLAG_BLOCKING))
146                 usleep(1000);
147
148         return res;
149 }