Make ZapScan wait 100ms for data
[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 Zap channels in\n"
47 "a convenient way.  Use '#' to select the next channel and use '*' to exit\n";
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 ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
195                                 ret = -1;
196                                 break;
197                         } else if (fd != chan->fds[0]) {
198                                 if (f->frametype == AST_FRAME_VOICE) {
199                                         if (f->subclass == AST_FORMAT_ULAW) {
200                                                 /* Carefully write */
201                                                 careful_write(fd, f->data, f->datalen);
202                                         } else
203                                                 ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
204                                 }
205                         }
206                         ast_frfree(f);
207                 } else if (outfd > -1) {
208                         res = read(outfd, buf, CONF_SIZE);
209                         if (res > 0) {
210                                 memset(&fr, 0, sizeof(fr));
211                                 fr.frametype = AST_FRAME_VOICE;
212                                 fr.subclass = AST_FORMAT_ULAW;
213                                 fr.datalen = res;
214                                 fr.samples = res;
215                                 fr.data = buf;
216                                 fr.offset = AST_FRIENDLY_OFFSET;
217                                 if (ast_write(chan, &fr) < 0) {
218                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
219                                         /* break; */
220                                 }
221                         } else
222                                 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
223                 }
224         }
225         if (fd != chan->fds[0])
226                 close(fd);
227         else {
228                 /* Take out of conference */
229                 /* Add us to the conference */
230                 ztc.chan = 0;
231                 ztc.confno = 0;
232                 ztc.confmode = 0;
233                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
234                         ast_log(LOG_WARNING, "Error setting conference\n");
235                 }
236         }
237
238 outrun:
239
240         return ret;
241 }
242
243 static int conf_exec(struct ast_channel *chan, void *data)
244 {
245         int res=-1;
246         struct localuser *u;
247         int confflags = 0;
248         int confno = 0;
249         char confstr[80], *tmp;
250         struct ast_channel *tempchan = NULL, *lastchan = NULL;
251         struct ast_frame *f;
252
253         LOCAL_USER_ADD(u);
254
255         if (chan->_state != AST_STATE_UP)
256                 ast_answer(chan);
257
258         for (;;) {
259                 if (ast_waitfor(chan, 100) < 0)
260                         break;
261                 f = ast_read(chan);
262                 if (!f)
263                         break;
264                 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
265                         ast_frfree(f);
266                         break;
267                 }
268                 ast_frfree(f);
269                 tempchan = ast_channel_walk(tempchan);
270                 if ( !tempchan && !lastchan )
271                         break;
272                 if ( tempchan && (!strcmp(tempchan->type, "Zap")) && (tempchan != chan) ) {
273                         ast_verbose(VERBOSE_PREFIX_3 "Zap channel %s is in-use, monitoring...\n", tempchan->name);
274                         strcpy(confstr, tempchan->name);
275                         if ((tmp = strchr(confstr,'-'))) {
276                                 *tmp = '\0';
277                         }
278                         confno = atoi(strchr(confstr,'/') + 1);
279                         ast_stopstream(chan);
280                         ast_say_number(chan, confno, AST_DIGIT_ANY, chan->language);
281                         res = conf_run(chan, confno, confflags);
282                         if (res<0) break;
283         }
284                 lastchan = tempchan;
285         }
286
287         LOCAL_USER_REMOVE(u);
288         return res;
289 }
290
291 int unload_module(void)
292 {
293         STANDARD_HANGUP_LOCALUSERS;
294         return ast_unregister_application(app);
295 }
296
297 int load_module(void)
298 {
299         return ast_register_application(app, conf_exec, synopsis, descrip);
300 }
301
302 char *description(void)
303 {
304         return tdesc;
305 }
306
307 int usecount(void)
308 {
309         int res;
310         STANDARD_USECOUNT(res);
311         return res;
312 }
313
314 char *key()
315 {
316         return ASTERISK_GPL_KEY;
317 }
318