00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "asterisk.h"
00039
00040 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00041
00042 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <unistd.h>
00046 #include <errno.h>
00047 #include <sys/ioctl.h>
00048 #include <zaptel/zaptel.h>
00049
00050 #include "asterisk/lock.h"
00051 #include "asterisk/file.h"
00052 #include "asterisk/logger.h"
00053 #include "asterisk/channel.h"
00054 #include "asterisk/pbx.h"
00055 #include "asterisk/module.h"
00056 #include "asterisk/config.h"
00057 #include "asterisk/app.h"
00058 #include "asterisk/options.h"
00059 #include "asterisk/cli.h"
00060 #include "asterisk/say.h"
00061 #include "asterisk/utils.h"
00062
00063 static char *app = "ZapBarge";
00064
00065 static char *synopsis = "Barge in (monitor) Zap channel";
00066
00067 static char *descrip =
00068 " ZapBarge([channel]): Barges in on a specified zap\n"
00069 "channel or prompts if one is not specified. Returns\n"
00070 "-1 when caller user hangs up and is independent of the\n"
00071 "state of the channel being monitored.";
00072
00073
00074 #define CONF_SIZE 160
00075
00076 static int careful_write(int fd, unsigned char *data, int len)
00077 {
00078 int res;
00079 while(len) {
00080 res = write(fd, data, len);
00081 if (res < 1) {
00082 if (errno != EAGAIN) {
00083 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
00084 return -1;
00085 } else
00086 return 0;
00087 }
00088 len -= res;
00089 data += res;
00090 }
00091 return 0;
00092 }
00093
00094 static int conf_run(struct ast_channel *chan, int confno, int confflags)
00095 {
00096 int fd;
00097 struct zt_confinfo ztc;
00098 struct ast_frame *f;
00099 struct ast_channel *c;
00100 struct ast_frame fr;
00101 int outfd;
00102 int ms;
00103 int nfds;
00104 int res;
00105 int flags;
00106 int retryzap;
00107 int origfd;
00108 int ret = -1;
00109
00110 ZT_BUFFERINFO bi;
00111 char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
00112 char *buf = __buf + AST_FRIENDLY_OFFSET;
00113
00114
00115 if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
00116 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
00117 goto outrun;
00118 }
00119
00120
00121 if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
00122 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
00123 goto outrun;
00124 }
00125 ast_indicate(chan, -1);
00126 retryzap = strcasecmp(chan->tech->type, "Zap");
00127 zapretry:
00128 origfd = chan->fds[0];
00129 if (retryzap) {
00130 fd = open("/dev/zap/pseudo", O_RDWR);
00131 if (fd < 0) {
00132 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00133 goto outrun;
00134 }
00135
00136 flags = fcntl(fd, F_GETFL);
00137 if (flags < 0) {
00138 ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
00139 close(fd);
00140 goto outrun;
00141 }
00142 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
00143 ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
00144 close(fd);
00145 goto outrun;
00146 }
00147
00148 memset(&bi, 0, sizeof(bi));
00149 bi.bufsize = CONF_SIZE;
00150 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
00151 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
00152 bi.numbufs = 4;
00153 if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
00154 ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
00155 close(fd);
00156 goto outrun;
00157 }
00158 nfds = 1;
00159 } else {
00160
00161 fd = chan->fds[0];
00162 nfds = 0;
00163 }
00164 memset(&ztc, 0, sizeof(ztc));
00165
00166 ztc.chan = 0;
00167 if (ioctl(fd, ZT_GETCONF, &ztc)) {
00168 ast_log(LOG_WARNING, "Error getting conference\n");
00169 close(fd);
00170 goto outrun;
00171 }
00172 if (ztc.confmode) {
00173
00174 if (!retryzap) {
00175 ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
00176 retryzap = 1;
00177 goto zapretry;
00178 }
00179 }
00180 memset(&ztc, 0, sizeof(ztc));
00181
00182 ztc.chan = 0;
00183 ztc.confno = confno;
00184 ztc.confmode = ZT_CONF_MONITORBOTH;
00185
00186 if (ioctl(fd, ZT_SETCONF, &ztc)) {
00187 ast_log(LOG_WARNING, "Error setting conference\n");
00188 close(fd);
00189 goto outrun;
00190 }
00191 ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
00192
00193 for(;;) {
00194 outfd = -1;
00195 ms = -1;
00196 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
00197 if (c) {
00198 if (c->fds[0] != origfd) {
00199 if (retryzap) {
00200
00201 close(fd);
00202 }
00203 ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
00204 retryzap = 0;
00205 goto zapretry;
00206 }
00207 f = ast_read(c);
00208 if (!f)
00209 break;
00210 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
00211 ret = 0;
00212 ast_frfree(f);
00213 break;
00214 } else if (fd != chan->fds[0]) {
00215 if (f->frametype == AST_FRAME_VOICE) {
00216 if (f->subclass == AST_FORMAT_ULAW) {
00217
00218 careful_write(fd, f->data, f->datalen);
00219 } else
00220 ast_log(LOG_WARNING, "Huh? Got a non-ulaw (%d) frame in the conference\n", f->subclass);
00221 }
00222 }
00223 ast_frfree(f);
00224 } else if (outfd > -1) {
00225 res = read(outfd, buf, CONF_SIZE);
00226 if (res > 0) {
00227 memset(&fr, 0, sizeof(fr));
00228 fr.frametype = AST_FRAME_VOICE;
00229 fr.subclass = AST_FORMAT_ULAW;
00230 fr.datalen = res;
00231 fr.samples = res;
00232 fr.data = buf;
00233 fr.offset = AST_FRIENDLY_OFFSET;
00234 if (ast_write(chan, &fr) < 0) {
00235 ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
00236
00237 }
00238 } else
00239 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
00240 }
00241 }
00242 if (fd != chan->fds[0])
00243 close(fd);
00244 else {
00245
00246
00247 ztc.chan = 0;
00248 ztc.confno = 0;
00249 ztc.confmode = 0;
00250 if (ioctl(fd, ZT_SETCONF, &ztc)) {
00251 ast_log(LOG_WARNING, "Error setting conference\n");
00252 }
00253 }
00254
00255 outrun:
00256
00257 return ret;
00258 }
00259
00260 static int conf_exec(struct ast_channel *chan, void *data)
00261 {
00262 int res=-1;
00263 struct ast_module_user *u;
00264 int retrycnt = 0;
00265 int confflags = 0;
00266 int confno = 0;
00267 char confstr[80] = "";
00268
00269 u = ast_module_user_add(chan);
00270
00271 if (!ast_strlen_zero(data)) {
00272 if ((sscanf(data, "Zap/%d", &confno) != 1) &&
00273 (sscanf(data, "%d", &confno) != 1)) {
00274 ast_log(LOG_WARNING, "ZapBarge Argument (if specified) must be a channel number, not '%s'\n", (char *)data);
00275 ast_module_user_remove(u);
00276 return 0;
00277 }
00278 }
00279
00280 if (chan->_state != AST_STATE_UP)
00281 ast_answer(chan);
00282
00283 while(!confno && (++retrycnt < 4)) {
00284
00285 confstr[0] = '\0';
00286 res = ast_app_getdata(chan, "conf-getchannel",confstr, sizeof(confstr) - 1, 0);
00287 if (res <0) goto out;
00288 if (sscanf(confstr, "%d", &confno) != 1)
00289 confno = 0;
00290 }
00291 if (confno) {
00292
00293
00294 res = conf_run(chan, confno, confflags);
00295 }
00296 out:
00297
00298 ast_module_user_remove(u);
00299 return res;
00300 }
00301
00302 static int unload_module(void)
00303 {
00304 int res;
00305
00306 res = ast_unregister_application(app);
00307
00308 ast_module_user_hangup_all();
00309
00310 return res;
00311 }
00312
00313 static int load_module(void)
00314 {
00315 return ast_register_application(app, conf_exec, synopsis, descrip);
00316 }
00317
00318 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Barge in on Zap channel application");