2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 2003, Digium
8 * Modified from app_zapbarge by David Troy <dave@toad.net>
10 * Mark Spencer <markster@digium.com>
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License
15 * Special thanks to comphealth.com for sponsoring this
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>
35 #include <sys/ioctl.h>
38 #include <linux/zaptel.h>
39 static char *tdesc = "Scan Zap channels application";
41 static char *app = "ZapScan";
43 static char *synopsis = "Scan Zap channels to monitor calls";
45 static char *descrip =
46 " ZapScan allows a call center manager to monitor\n"
47 "phone conversations in a convenient way.";
57 static int careful_write(int fd, unsigned char *data, int len)
61 res = write(fd, data, len);
63 if (errno != EAGAIN) {
64 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
75 static int conf_run(struct ast_channel *chan, int confno, int confflags)
78 struct zt_confinfo ztc;
80 struct ast_channel *c;
92 char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
93 char *buf = __buf + AST_FRIENDLY_OFFSET;
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);
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);
106 ast_indicate(chan, -1);
107 retryzap = strcasecmp(chan->type, "Zap");
109 origfd = chan->fds[0];
111 fd = open("/dev/zap/pseudo", O_RDWR);
113 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
116 /* Make non-blocking */
117 flags = fcntl(fd, F_GETFL);
119 ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
123 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
124 ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
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;
134 if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
135 ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
141 /* XXX Make sure we're not running on a pseudo channel XXX */
145 memset(&ztc, 0, sizeof(ztc));
146 /* Check to see if we're in a conference... */
148 if (ioctl(fd, ZT_GETCONF, &ztc)) {
149 ast_log(LOG_WARNING, "Error getting conference\n");
154 /* Whoa, already in a conference... Retry... */
156 ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
161 memset(&ztc, 0, sizeof(ztc));
162 /* Add us to the conference */
165 ztc.confmode = ZT_CONF_MONITORBOTH;
167 if (ioctl(fd, ZT_SETCONF, &ztc)) {
168 ast_log(LOG_WARNING, "Error setting conference\n");
172 ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
177 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
179 if (c->fds[0] != origfd) {
181 /* Kill old pseudo */
184 ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
191 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
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);
200 ast_log(LOG_WARNING, "Huh? Got a non-ulaw (%d) frame in the conference\n", f->subclass);
204 } else if (outfd > -1) {
205 res = read(outfd, buf, CONF_SIZE);
207 memset(&fr, 0, sizeof(fr));
208 fr.frametype = AST_FRAME_VOICE;
209 fr.subclass = AST_FORMAT_ULAW;
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));
219 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
222 if (fd != chan->fds[0])
225 /* Take out of conference */
226 /* Add us to the conference */
230 if (ioctl(fd, ZT_SETCONF, &ztc)) {
231 ast_log(LOG_WARNING, "Error setting conference\n");
240 static int conf_exec(struct ast_channel *chan, void *data)
246 char confstr[80], *tmp;
247 struct ast_channel *tempchan = NULL, *lastchan = NULL;
251 if (chan->_state != AST_STATE_UP)
255 tempchan = ast_channel_walk(tempchan);
256 if ( !tempchan && !lastchan )
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,'-'))) {
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);
273 LOCAL_USER_REMOVE(u);
277 int unload_module(void)
279 STANDARD_HANGUP_LOCALUSERS;
280 return ast_unregister_application(app);
283 int load_module(void)
285 return ast_register_application(app, conf_exec, synopsis, descrip);
288 char *description(void)
296 STANDARD_USECOUNT(res);
302 return ASTERISK_GPL_KEY;