Make ADPCM frames standard
[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 <pthread.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <signal.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <math.h>                       /* For PI */
23 #include <asterisk/pbx.h>
24 #include <asterisk/frame.h>
25 #include <asterisk/sched.h>
26 #include <asterisk/options.h>
27 #include <asterisk/channel.h>
28 #include <asterisk/channel_pvt.h>
29 #include <asterisk/logger.h>
30 #include <asterisk/file.h>
31 #include <asterisk/translate.h>
32 #include <asterisk/manager.h>
33 #include <asterisk/chanvars.h>
34 #include <asterisk/linkedlists.h>
35 #include <asterisk/indications.h>
36
37 #define MAX_AUTOMONS 256
38
39 static ast_mutex_t autolock = AST_MUTEX_INITIALIZER;
40
41 struct asent {
42         struct ast_channel *chan;
43         struct asent *next;
44 };
45
46 static struct asent *aslist = NULL;
47 static pthread_t asthread = (pthread_t) -1;
48
49 static void *autoservice_run(void *ign)
50 {
51         struct ast_channel *mons[MAX_AUTOMONS];
52         int x;
53         int ms;
54         struct ast_channel *chan;
55         struct asent *as;
56         struct ast_frame *f;
57         for(;;) {
58                 x = 0;
59                 ast_mutex_lock(&autolock);
60                 as = aslist;
61                 while(as) {
62                         if (!as->chan->_softhangup) {
63                                 if (x < MAX_AUTOMONS)
64                                         mons[x++] = as->chan;
65                                 else
66                                         ast_log(LOG_WARNING, "Exceeded maximum number of automatic monitoring events.  Fix autoservice.c\n");
67                         }
68                         as = as->next;
69                 }
70                 ast_mutex_unlock(&autolock);
71
72 /*              if (!aslist)
73                         break; */
74                 ms = 500;
75                 chan = ast_waitfor_n(mons, x, &ms);
76                 if (chan) {
77                         /* Read and ignore anything that occurs */
78                         f = ast_read(chan);
79                         if (f)
80                                 ast_frfree(f);
81                 }
82         }
83         asthread = (pthread_t) -1;
84         return NULL;
85 }
86
87 int ast_autoservice_start(struct ast_channel *chan)
88 {
89         int res = -1;
90         struct asent *as;
91         int needstart;
92         ast_mutex_lock(&autolock);
93         needstart = (asthread == (pthread_t) -1) ? 1 : 0 /* aslist ? 0 : 1 */;
94         as = aslist;
95         while(as) {
96                 if (as->chan == chan)
97                         break;
98                 as = as->next;
99         }
100         if (!as) {
101                 as = malloc(sizeof(struct asent));
102                 if (as) {
103                         memset(as, 0, sizeof(struct asent));
104                         as->chan = chan;
105                         as->next = aslist;
106                         aslist = as;
107                         res = 0;
108                         if (needstart) {
109                                 if (pthread_create(&asthread, NULL, autoservice_run, NULL)) {
110                                         ast_log(LOG_WARNING, "Unable to create autoservice thread :(\n");
111                                         free(aslist);
112                                         aslist = NULL;
113                                         res = -1;
114                                 } else
115                                         pthread_kill(asthread, SIGURG);
116                         }
117                 }
118         }
119         ast_mutex_unlock(&autolock);
120         return res;
121 }
122
123 int ast_autoservice_stop(struct ast_channel *chan)
124 {
125         int res = -1;
126         struct asent *as, *prev;
127         ast_mutex_lock(&autolock);
128         as = aslist;
129         prev = NULL;
130         while(as) {
131                 if (as->chan == chan)
132                         break;
133                 prev = as;
134                 as = as->next;
135         }
136         if (as) {
137                 if (prev)
138                         prev->next = as->next;
139                 else
140                         aslist = as->next;
141                 free(as);
142                 if (!chan->_softhangup)
143                         res = 0;
144         }
145         if (asthread != (pthread_t) -1) 
146                 pthread_kill(asthread, SIGURG);
147         ast_mutex_unlock(&autolock);
148         /* Wait for it to un-block */
149         while(chan->blocking)
150                 usleep(1000);
151         return res;
152 }