Update chanelog
[asterisk/asterisk.git] / autoservice.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Automatic channel service routines
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/time.h>
18 #include <signal.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <math.h>                       /* For PI */
22 #include <asterisk/pbx.h>
23 #include <asterisk/frame.h>
24 #include <asterisk/sched.h>
25 #include <asterisk/options.h>
26 #include <asterisk/channel.h>
27 #include <asterisk/channel_pvt.h>
28 #include <asterisk/logger.h>
29 #include <asterisk/file.h>
30 #include <asterisk/translate.h>
31 #include <asterisk/manager.h>
32 #include <asterisk/chanvars.h>
33 #include <asterisk/linkedlists.h>
34 #include <asterisk/indications.h>
35 #include <asterisk/lock.h>
36 #include <asterisk/utils.h>
37
38 #define MAX_AUTOMONS 256
39
40 AST_MUTEX_DEFINE_STATIC(autolock);
41
42 struct asent {
43         struct ast_channel *chan;
44         struct asent *next;
45 };
46
47 static struct asent *aslist = NULL;
48 static pthread_t asthread = AST_PTHREADT_NULL;
49
50 static void *autoservice_run(void *ign)
51 {
52         struct ast_channel *mons[MAX_AUTOMONS];
53         int x;
54         int ms;
55         struct ast_channel *chan;
56         struct asent *as;
57         struct ast_frame *f;
58         for(;;) {
59                 x = 0;
60                 ast_mutex_lock(&autolock);
61                 as = aslist;
62                 while(as) {
63                         if (!as->chan->_softhangup) {
64                                 if (x < MAX_AUTOMONS)
65                                         mons[x++] = as->chan;
66                                 else
67                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
68                         }
69                         as = as->next;
70                 }
71                 ast_mutex_unlock(&autolock);
72
73 /*              if (!aslist)
74                         break; */
75                 ms = 500;
76                 chan = ast_waitfor_n(mons, x, &ms);
77                 if (chan) {
78                         /* Read and ignore anything that occurs */
79                         f = ast_read(chan);
80                         if (f)
81                                 ast_frfree(f);
82                 }
83         }
84         asthread = AST_PTHREADT_NULL;
85         return NULL;
86 }
87
88 int ast_autoservice_start(struct ast_channel *chan)
89 {
90         int res = -1;
91         struct asent *as;
92         int needstart;
93         ast_mutex_lock(&autolock);
94         needstart = (asthread == AST_PTHREADT_NULL) ? 1 : 0 /* aslist ? 0 : 1 */;
95         as = aslist;
96         while(as) {
97                 if (as->chan == chan)
98                         break;
99                 as = as->next;
100         }
101         if (!as) {
102                 as = malloc(sizeof(struct asent));
103                 if (as) {
104                         memset(as, 0, sizeof(struct asent));
105                         as->chan = chan;
106                         as->next = aslist;
107                         aslist = as;
108                         res = 0;
109                         if (needstart) {
110                                 if (ast_pthread_create(&asthread, NULL, autoservice_run, NULL)) {
111                                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
112                                         free(aslist);
113                                         aslist = NULL;
114                                         res = -1;
115                                 } else
116                                         pthread_kill(asthread, SIGURG);
117                         }
118                 }
119         }
120         ast_mutex_unlock(&autolock);
121         return res;
122 }
123
124 int ast_autoservice_stop(struct ast_channel *chan)
125 {
126         int res = -1;
127         struct asent *as, *prev;
128         ast_mutex_lock(&autolock);
129         as = aslist;
130         prev = NULL;
131         while(as) {
132                 if (as->chan == chan)
133                         break;
134                 prev = as;
135                 as = as->next;
136         }
137         if (as) {
138                 if (prev)
139                         prev->next = as->next;
140                 else
141                         aslist = as->next;
142                 free(as);
143                 if (!chan->_softhangup)
144                         res = 0;
145         }
146         if (asthread != AST_PTHREADT_NULL) 
147                 pthread_kill(asthread, SIGURG);
148         ast_mutex_unlock(&autolock);
149         /* Wait for it to un-block */
150         while(chan->blocking)
151                 usleep(1000);
152         return res;
153 }