add new application 'zapscan' Bug #250
[asterisk/asterisk.git] / apps / app_zapscan.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Zap Scanner
5  *
6  * Copyright (C) 2003, Digium
7  *
8  * Modified from app_zapbarge by David Troy <dave@toad.net>
9  *
10  * Mark Spencer <markster@digium.com>
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License
14  *
15  * Special thanks to comphealth.com for sponsoring this
16  * GPL application.
17  */
18
19 #include <asterisk/lock.h>
20 #include <asterisk/file.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/channel.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/module.h>
25 #include <asterisk/config.h>
26 #include <asterisk/app.h>
27 #include <asterisk/options.h>
28 #include <asterisk/cli.h>
29 #include <asterisk/say.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/ioctl.h>
36
37 #include <pthread.h>
38 #include <linux/zaptel.h>
39 static char *tdesc = "Scan Zap channels application";
40
41 static char *app = "ZapScan";
42
43 static char *synopsis = "Scan Zap channels to monitor calls";
44
45 static char *descrip =
46 "  ZapScan allows a call center manager to monitor\n"
47 "phone conversations in a convenient way.";
48
49
50 STANDARD_LOCAL_USER;
51
52 LOCAL_USER_DECL;
53
54
55 #define CONF_SIZE 160
56
57 static int careful_write(int fd, unsigned char *data, int len)
58 {
59         int res;
60         while(len) {
61                 res = write(fd, data, len);
62                 if (res < 1) {
63                         if (errno != EAGAIN) {
64                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
65                                 return -1;
66                         } else
67                                 return 0;
68                 }
69                 len -= res;
70                 data += res;
71         }
72         return 0;
73 }
74
75 static int conf_run(struct ast_channel *chan, int confno, int confflags)
76 {
77         int fd;
78         struct zt_confinfo ztc;
79         struct ast_frame *f;
80         struct ast_channel *c;
81         struct ast_frame fr;
82         int outfd;
83         int ms;
84         int nfds;
85         int res;
86         int flags;
87         int retryzap;
88         int origfd;
89         int ret = -1;
90
91         ZT_BUFFERINFO bi;
92         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
93         char *buf = __buf + AST_FRIENDLY_OFFSET;
94
95         /* Set it into U-law mode (write) */
96         if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
97                 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
98                 goto outrun;
99         }
100
101         /* Set it into U-law mode (read) */
102         if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
103                 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
104                 goto outrun;
105         }
106         ast_indicate(chan, -1);
107         retryzap = strcasecmp(chan->type, "Zap");
108 zapretry:
109         origfd = chan->fds[0];
110         if (retryzap) {
111                 fd = open("/dev/zap/pseudo", O_RDWR);
112                 if (fd < 0) {
113                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
114                         goto outrun;
115                 }
116                 /* Make non-blocking */
117                 flags = fcntl(fd, F_GETFL);
118                 if (flags < 0) {
119                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
120                         close(fd);
121                         goto outrun;
122                 }
123                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
124                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
125                         close(fd);
126                         goto outrun;
127                 }
128                 /* Setup buffering information */
129                 memset(&bi, 0, sizeof(bi));
130                 bi.bufsize = CONF_SIZE;
131                 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
132                 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
133                 bi.numbufs = 4;
134                 if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
135                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
136                         close(fd);
137                         goto outrun;
138                 }
139                 nfds = 1;
140         } else {
141                 /* XXX Make sure we're not running on a pseudo channel XXX */
142                 fd = chan->fds[0];
143                 nfds = 0;
144         }
145         memset(&ztc, 0, sizeof(ztc));
146         /* Check to see if we're in a conference... */
147         ztc.chan = 0;
148         if (ioctl(fd, ZT_GETCONF, &ztc)) {
149                 ast_log(LOG_WARNING, "Error getting conference\n");
150                 close(fd);
151                 goto outrun;
152         }
153         if (ztc.confmode) {
154                 /* Whoa, already in a conference...  Retry... */
155                 if (!retryzap) {
156                         ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
157                         retryzap = 1;
158                         goto zapretry;
159                 }
160         }
161         memset(&ztc, 0, sizeof(ztc));
162         /* Add us to the conference */
163         ztc.chan = 0;
164         ztc.confno = confno;
165         ztc.confmode = ZT_CONF_MONITORBOTH;
166
167         if (ioctl(fd, ZT_SETCONF, &ztc)) {
168                 ast_log(LOG_WARNING, "Error setting conference\n");
169                 close(fd);
170                 goto outrun;
171         }
172         ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
173
174         for(;;) {
175                 outfd = -1;
176                 ms = -1;
177                 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
178                 if (c) {
179                         if (c->fds[0] != origfd) {
180                                 if (retryzap) {
181                                         /* Kill old pseudo */
182                                         close(fd);
183                                 }
184                                 ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
185                                 retryzap = 0;
186                                 goto zapretry;
187                         }
188                         f = ast_read(c);
189                         if (!f)
190                                 break;
191                         if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
192                                 ret = 0;
193                                 break;
194                         } else if (fd != chan->fds[0]) {
195                                 if (f->frametype == AST_FRAME_VOICE) {
196                                         if (f->subclass == AST_FORMAT_ULAW) {
197                                                 /* Carefully write */
198                                                 careful_write(fd, f->data, f->datalen);
199                                         } else
200                                                 ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
201                                 }
202                         }
203                         ast_frfree(f);
204                 } else if (outfd > -1) {
205                         res = read(outfd, buf, CONF_SIZE);
206                         if (res > 0) {
207                                 memset(&fr, 0, sizeof(fr));
208                                 fr.frametype = AST_FRAME_VOICE;
209                                 fr.subclass = AST_FORMAT_ULAW;
210                                 fr.datalen = res;
211                                 fr.samples = res;
212                                 fr.data = buf;
213                                 fr.offset = AST_FRIENDLY_OFFSET;
214                                 if (ast_write(chan, &fr) < 0) {
215                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
216                                         /* break; */
217                                 }
218                         } else
219                                 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
220                 }
221         }
222         if (fd != chan->fds[0])
223                 close(fd);
224         else {
225                 /* Take out of conference */
226                 /* Add us to the conference */
227                 ztc.chan = 0;
228                 ztc.confno = 0;
229                 ztc.confmode = 0;
230                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
231                         ast_log(LOG_WARNING, "Error setting conference\n");
232                 }
233         }
234
235 outrun:
236
237         return ret;
238 }
239
240 static int conf_exec(struct ast_channel *chan, void *data)
241 {
242         int res=-1;
243         struct localuser *u;
244         int confflags = 0;
245         int confno = 0;
246         char confstr[80], *tmp;
247         struct ast_channel *tempchan = NULL, *lastchan = NULL;
248
249         LOCAL_USER_ADD(u);
250
251         if (chan->_state != AST_STATE_UP)
252                 ast_answer(chan);
253
254         for (;;) {
255                 tempchan = ast_channel_walk(tempchan);
256                 if ( !tempchan && !lastchan )
257                         break;
258                 if ( tempchan && (!strcmp(tempchan->type, "Zap")) && (tempchan != chan) ) {
259                         ast_verbose(VERBOSE_PREFIX_3 "Zap channel %s is in-use, monitoring...\n", tempchan->name);
260                         strcpy(confstr, tempchan->name);
261                         if ((tmp = strchr(confstr,'-'))) {
262                                 *tmp = '\0';
263                         }
264                         confno = atoi(strchr(confstr,'/') + 1);
265                         ast_stopstream(chan);
266                         ast_say_number(chan, confno, AST_DIGIT_ANY, chan->language);
267                         res = conf_run(chan, confno, confflags);
268                         if (res<0) break;
269         }
270                 lastchan = tempchan;
271         }
272
273         LOCAL_USER_REMOVE(u);
274         return res;
275 }
276
277 int unload_module(void)
278 {
279         STANDARD_HANGUP_LOCALUSERS;
280         return ast_unregister_application(app);
281 }
282
283 int load_module(void)
284 {
285         return ast_register_application(app, conf_exec, synopsis, descrip);
286 }
287
288 char *description(void)
289 {
290         return tdesc;
291 }
292
293 int usecount(void)
294 {
295         int res;
296         STANDARD_USECOUNT(res);
297         return res;
298 }
299
300 char *key()
301 {
302         return ASTERISK_GPL_KEY;
303 }
304