a199d4e02137f60e31cbd5a3d8322bbd4423c2cd
[asterisk/asterisk.git] / apps / app_zapscan.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Modified from app_zapbarge by David Troy <dave@toad.net>
9  *
10  * Special thanks to comphealth.com for sponsoring this
11  * GPL application.
12  *
13  * See http://www.asterisk.org for more information about
14  * the Asterisk project. Please do not directly contact
15  * any of the maintainers of this project for assistance;
16  * the project provides a web site, mailing lists and IRC
17  * channels for your use.
18  *
19  * This program is free software, distributed under the terms of
20  * the GNU General Public License Version 2. See the LICENSE file
21  * at the top of the source tree.
22  */
23
24 /*! \file
25  *
26  * \brief Zap Scanner
27  *
28  * \ingroup applications
29  */
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <sys/ioctl.h>
37
38 #ifdef __linux__
39 #include <linux/zaptel.h>
40 #else
41 #include <zaptel.h>
42 #endif /* __linux__ */
43
44 #include "asterisk.h"
45
46 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47
48 #include "asterisk/lock.h"
49 #include "asterisk/file.h"
50 #include "asterisk/logger.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/pbx.h"
53 #include "asterisk/module.h"
54 #include "asterisk/config.h"
55 #include "asterisk/app.h"
56 #include "asterisk/options.h"
57 #include "asterisk/utils.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/say.h"
60
61 static char *tdesc = "Scan Zap channels application";
62
63 static char *app = "ZapScan";
64
65 static char *synopsis = "Scan Zap channels to monitor calls";
66
67 static char *descrip =
68 "  ZapScan([group]) allows a call center manager to monitor Zap channels in\n"
69 "a convenient way.  Use '#' to select the next channel and use '*' to exit\n"
70 "Limit scanning to a channel GROUP by setting the option group argument.\n";
71
72 STANDARD_LOCAL_USER;
73
74 LOCAL_USER_DECL;
75
76
77 #define CONF_SIZE 160
78
79 static struct ast_channel *get_zap_channel_locked(int num) {
80         char name[80];
81         
82         snprintf(name,sizeof(name),"Zap/%d-1",num);
83         return ast_get_channel_by_name_locked(name);
84 }
85
86 static int careful_write(int fd, unsigned char *data, int len)
87 {
88         int res;
89         while(len) {
90                 res = write(fd, data, len);
91                 if (res < 1) {
92                         if (errno != EAGAIN) {
93                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
94                                 return -1;
95                         } else
96                                 return 0;
97                 }
98                 len -= res;
99                 data += res;
100         }
101         return 0;
102 }
103
104 static int conf_run(struct ast_channel *chan, int confno, int confflags)
105 {
106         int fd;
107         struct zt_confinfo ztc;
108         struct ast_frame *f;
109         struct ast_channel *c;
110         struct ast_frame fr;
111         int outfd;
112         int ms;
113         int nfds;
114         int res;
115         int flags;
116         int retryzap;
117         int origfd;
118         int ret = -1;
119         char input[4];
120         int ic=0;
121         
122         ZT_BUFFERINFO bi;
123         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
124         char *buf = __buf + AST_FRIENDLY_OFFSET;
125         
126         /* Set it into U-law mode (write) */
127         if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
128                 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
129                 goto outrun;
130         }
131         
132         /* Set it into U-law mode (read) */
133         if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
134                 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
135                 goto outrun;
136         }
137         ast_indicate(chan, -1);
138         retryzap = strcasecmp(chan->type, "Zap");
139  zapretry:
140         origfd = chan->fds[0];
141         if (retryzap) {
142                 fd = open("/dev/zap/pseudo", O_RDWR);
143                 if (fd < 0) {
144                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
145                         goto outrun;
146                 }
147                 /* Make non-blocking */
148                 flags = fcntl(fd, F_GETFL);
149                 if (flags < 0) {
150                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
151                         close(fd);
152                         goto outrun;
153                 }
154                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
155                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
156                         close(fd);
157                         goto outrun;
158                 }
159                 /* Setup buffering information */
160                 memset(&bi, 0, sizeof(bi));
161                 bi.bufsize = CONF_SIZE;
162                 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
163                 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
164                 bi.numbufs = 4;
165                 if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
166                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
167                         close(fd);
168                         goto outrun;
169                 }
170                 nfds = 1;
171         } else {
172                 /* XXX Make sure we're not running on a pseudo channel XXX */
173                 fd = chan->fds[0];
174                 nfds = 0;
175         }
176         memset(&ztc, 0, sizeof(ztc));
177         /* Check to see if we're in a conference... */
178         ztc.chan = 0;
179         if (ioctl(fd, ZT_GETCONF, &ztc)) {
180                         ast_log(LOG_WARNING, "Error getting conference\n");
181                         close(fd);
182                         goto outrun;
183         }
184         if (ztc.confmode) {
185                         /* Whoa, already in a conference...  Retry... */
186                         if (!retryzap) {
187                                 ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
188                                 retryzap = 1;
189                                 goto zapretry;
190                         }
191         }
192         memset(&ztc, 0, sizeof(ztc));
193         /* Add us to the conference */
194         ztc.chan = 0;
195         ztc.confno = confno;
196         ztc.confmode = ZT_CONF_MONITORBOTH;
197                 
198         if (ioctl(fd, ZT_SETCONF, &ztc)) {
199                 ast_log(LOG_WARNING, "Error setting conference\n");
200                 close(fd);
201                 goto outrun;
202         }
203         ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
204                 
205         for(;;) {
206                         outfd = -1;
207                         ms = -1;
208                         c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
209                         if (c) {
210                                 if (c->fds[0] != origfd) {
211                                         if (retryzap) {
212                                                 /* Kill old pseudo */
213                                                 close(fd);
214                                         }
215                                         ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
216                                         retryzap = 0;
217                                 goto zapretry;
218                                 }
219                                 f = ast_read(c);
220                                 if (!f)
221                                         break;
222                                 if(f->frametype == AST_FRAME_DTMF) {
223                                         if(f->subclass == '#') {
224                                                 ret = 0;
225                                                 break;
226                                         }
227                                         else if (f->subclass == '*') {
228                                                 ret = -1;
229                                                 break;
230                                                 
231                                         }
232                                         else {
233                                                 input[ic++] = f->subclass;
234                                         }
235                                         if(ic == 3) {
236                                                 input[ic++] = '\0';
237                                                 ic=0;
238                                                 ret = atoi(input);
239                                                 ast_verbose(VERBOSE_PREFIX_3 "Zapscan: change channel to %d\n",ret);
240                                                 break;
241                                         }
242                                 }
243                                 
244                                 if (fd != chan->fds[0]) {
245                                         if (f->frametype == AST_FRAME_VOICE) {
246                                                 if (f->subclass == AST_FORMAT_ULAW) {
247                                                         /* Carefully write */
248                                                 careful_write(fd, f->data, f->datalen);
249                                                 } else
250                                                         ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
251                                         }
252                                 }
253                                 ast_frfree(f);
254                         } else if (outfd > -1) {
255                                 res = read(outfd, buf, CONF_SIZE);
256                                 if (res > 0) {
257                                         memset(&fr, 0, sizeof(fr));
258                                         fr.frametype = AST_FRAME_VOICE;
259                                         fr.subclass = AST_FORMAT_ULAW;
260                                         fr.datalen = res;
261                                         fr.samples = res;
262                                         fr.data = buf;
263                                         fr.offset = AST_FRIENDLY_OFFSET;
264                                         if (ast_write(chan, &fr) < 0) {
265                                                 ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
266                                                 /* break; */
267                                         }
268                                 } else
269                                         ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
270                         }
271         }
272         if (fd != chan->fds[0])
273                         close(fd);
274         else {
275                         /* Take out of conference */
276                         /* Add us to the conference */
277                         ztc.chan = 0;
278                         ztc.confno = 0;
279                         ztc.confmode = 0;
280                         if (ioctl(fd, ZT_SETCONF, &ztc)) {
281                                 ast_log(LOG_WARNING, "Error setting conference\n");
282                 }
283         }
284                 
285  outrun:
286                 
287         return ret;
288 }
289
290 static int conf_exec(struct ast_channel *chan, void *data)
291 {
292         int res=-1;
293         struct localuser *u;
294         int confflags = 0;
295         int confno = 0;
296         char confstr[80] = "", *tmp = NULL;
297         struct ast_channel *tempchan = NULL, *lastchan = NULL,*ichan = NULL;
298         struct ast_frame *f;
299         char *mygroup;
300         char *desired_group;
301         int input=0,search_group=0;
302         
303         LOCAL_USER_ADD(u);
304         
305         if (chan->_state != AST_STATE_UP)
306                 ast_answer(chan);
307         
308         desired_group = ast_strdupa((char *) data);
309         if(!ast_strlen_zero(desired_group)) {
310                 ast_verbose(VERBOSE_PREFIX_3 "Scanning for group %s\n", desired_group);
311                 search_group = 1;
312         }
313
314         for (;;) {
315                 if (ast_waitfor(chan, 100) < 0)
316                         break;
317                 
318                 f = ast_read(chan);
319                 if (!f)
320                         break;
321                 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
322                         ast_frfree(f);
323                         break;
324                 }
325                 ast_frfree(f);
326                 ichan = NULL;
327                 if(input) {
328                         ichan = get_zap_channel_locked(input);
329                         input = 0;
330                 }
331                 
332                 tempchan = ichan ? ichan : ast_channel_walk_locked(tempchan);
333                 
334                 if ( !tempchan && !lastchan )
335                         break;
336                 
337                 if (tempchan && search_group) {
338                         if((mygroup = pbx_builtin_getvar_helper(tempchan, "GROUP")) && (!strcmp(mygroup, desired_group))) {
339                                 ast_verbose(VERBOSE_PREFIX_3 "Found Matching Channel %s in group %s\n", tempchan->name, desired_group);
340                         } else {
341                                 ast_mutex_unlock(&tempchan->lock);
342                                 lastchan = tempchan;
343                                 continue;
344                         }
345                 }
346                 if ( tempchan && tempchan->type && (!strcmp(tempchan->type, "Zap")) && (tempchan != chan) ) {
347                         ast_verbose(VERBOSE_PREFIX_3 "Zap channel %s is in-use, monitoring...\n", tempchan->name);
348                         ast_copy_string(confstr, tempchan->name, sizeof(confstr));
349                         ast_mutex_unlock(&tempchan->lock);
350                         if ((tmp = strchr(confstr,'-'))) {
351                                 *tmp = '\0';
352                         }
353                         confno = atoi(strchr(confstr,'/') + 1);
354                         ast_stopstream(chan);
355                         ast_say_number(chan, confno, AST_DIGIT_ANY, chan->language, (char *) NULL);
356                         res = conf_run(chan, confno, confflags);
357                         if (res<0) break;
358                         input = res;
359                 } else if (tempchan)
360                         ast_mutex_unlock(&tempchan->lock);
361                 lastchan = tempchan;
362         }
363         LOCAL_USER_REMOVE(u);
364         return res;
365 }
366
367 int unload_module(void)
368 {
369         int res;
370
371         res = ast_unregister_application(app);
372         
373         STANDARD_HANGUP_LOCALUSERS;
374
375         return res;
376 }
377
378 int load_module(void)
379 {
380         return ast_register_application(app, conf_exec, synopsis, descrip);
381 }
382
383 char *description(void)
384 {
385         return tdesc;
386 }
387
388 int usecount(void)
389 {
390         int res;
391         STANDARD_USECOUNT(res);
392         return res;
393 }
394
395 char *key()
396 {
397         return ASTERISK_GPL_KEY;
398 }
399