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: 134254 $")
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
00049 #include "asterisk/lock.h"
00050 #include "asterisk/file.h"
00051 #include "asterisk/logger.h"
00052 #include "asterisk/channel.h"
00053 #include "asterisk/pbx.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/config.h"
00056 #include "asterisk/app.h"
00057 #include "asterisk/options.h"
00058 #include "asterisk/cli.h"
00059 #include "asterisk/say.h"
00060 #include "asterisk/utils.h"
00061
00062 #include "asterisk/dahdi_compat.h"
00063
00064 static char *dahdi_app = "DAHDIBarge";
00065 static char *zap_app = "ZapBarge";
00066
00067 static char *dahdi_synopsis = "Barge in (monitor) DAHDI channel";
00068 static char *zap_synopsis = "Barge in (monitor) Zap channel";
00069
00070 static char *dahdi_descrip =
00071 " DAHDIBarge([channel]): Barges in on a specified DAHDI\n"
00072 "channel or prompts if one is not specified. Returns\n"
00073 "-1 when caller user hangs up and is independent of the\n"
00074 "state of the channel being monitored.";
00075
00076 static char *zap_descrip =
00077 " ZapBarge([channel]): Barges in on a specified Zaptel\n"
00078 "channel or prompts if one is not specified. Returns\n"
00079 "-1 when caller user hangs up and is independent of the\n"
00080 "state of the channel being monitored.";
00081
00082 #define CONF_SIZE 160
00083
00084 static int careful_write(int fd, unsigned char *data, int len)
00085 {
00086 int res;
00087 while(len) {
00088 res = write(fd, data, len);
00089 if (res < 1) {
00090 if (errno != EAGAIN) {
00091 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
00092 return -1;
00093 } else
00094 return 0;
00095 }
00096 len -= res;
00097 data += res;
00098 }
00099 return 0;
00100 }
00101
00102 static int conf_run(struct ast_channel *chan, int confno, int confflags)
00103 {
00104 int fd;
00105 struct dahdi_confinfo ztc;
00106 struct ast_frame *f;
00107 struct ast_channel *c;
00108 struct ast_frame fr;
00109 int outfd;
00110 int ms;
00111 int nfds;
00112 int res;
00113 int flags;
00114 int retryzap;
00115 int origfd;
00116 int ret = -1;
00117 struct dahdi_bufferinfo bi;
00118 char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
00119 char *buf = __buf + AST_FRIENDLY_OFFSET;
00120
00121
00122 if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
00123 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
00124 goto outrun;
00125 }
00126
00127
00128 if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
00129 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
00130 goto outrun;
00131 }
00132 ast_indicate(chan, -1);
00133 retryzap = strcasecmp(chan->tech->type, dahdi_chan_name);
00134 zapretry:
00135 origfd = chan->fds[0];
00136 if (retryzap) {
00137 #ifdef HAVE_ZAPTEL
00138 fd = open("/dev/zap/pseudo", O_RDWR);
00139 #else
00140 fd = open("/dev/dahdi/pseudo", O_RDWR);
00141 #endif
00142 if (fd < 0) {
00143 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00144 goto outrun;
00145 }
00146
00147 flags = fcntl(fd, F_GETFL);
00148 if (flags < 0) {
00149 ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
00150 close(fd);
00151 goto outrun;
00152 }
00153 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
00154 ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
00155 close(fd);
00156 goto outrun;
00157 }
00158
00159 memset(&bi, 0, sizeof(bi));
00160 bi.bufsize = CONF_SIZE;
00161 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
00162 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
00163 bi.numbufs = 4;
00164 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
00165 ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
00166 close(fd);
00167 goto outrun;
00168 }
00169 nfds = 1;
00170 } else {
00171
00172 fd = chan->fds[0];
00173 nfds = 0;
00174 }
00175 memset(&ztc, 0, sizeof(ztc));
00176
00177 ztc.chan = 0;
00178 if (ioctl(fd, DAHDI_GETCONF, &ztc)) {
00179 ast_log(LOG_WARNING, "Error getting conference\n");
00180 close(fd);
00181 goto outrun;
00182 }
00183 if (ztc.confmode) {
00184
00185 if (!retryzap) {
00186 ast_log(LOG_DEBUG, "Channel is in a conference already, retrying with pseudo\n");
00187 retryzap = 1;
00188 goto zapretry;
00189 }
00190 }
00191 memset(&ztc, 0, sizeof(ztc));
00192
00193 ztc.chan = 0;
00194 ztc.confno = confno;
00195 ztc.confmode = DAHDI_CONF_MONITORBOTH;
00196
00197 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
00198 ast_log(LOG_WARNING, "Error setting conference\n");
00199 close(fd);
00200 goto outrun;
00201 }
00202 ast_log(LOG_DEBUG, "Placed channel %s in channel %d monitor\n", chan->name, confno);
00203
00204 for(;;) {
00205 outfd = -1;
00206 ms = -1;
00207 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
00208 if (c) {
00209 if (c->fds[0] != origfd) {
00210 if (retryzap) {
00211
00212 close(fd);
00213 }
00214 ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
00215 retryzap = 0;
00216 goto zapretry;
00217 }
00218 f = ast_read(c);
00219 if (!f)
00220 break;
00221 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
00222 ret = 0;
00223 ast_frfree(f);
00224 break;
00225 } else if (fd != chan->fds[0]) {
00226 if (f->frametype == AST_FRAME_VOICE) {
00227 if (f->subclass == AST_FORMAT_ULAW) {
00228
00229 careful_write(fd, f->data, f->datalen);
00230 } else
00231 ast_log(LOG_WARNING, "Huh? Got a non-ulaw (%d) frame in the conference\n", f->subclass);
00232 }
00233 }
00234 ast_frfree(f);
00235 } else if (outfd > -1) {
00236 res = read(outfd, buf, CONF_SIZE);
00237 if (res > 0) {
00238 memset(&fr, 0, sizeof(fr));
00239 fr.frametype = AST_FRAME_VOICE;
00240 fr.subclass = AST_FORMAT_ULAW;
00241 fr.datalen = res;
00242 fr.samples = res;
00243 fr.data = buf;
00244 fr.offset = AST_FRIENDLY_OFFSET;
00245 if (ast_write(chan, &fr) < 0) {
00246 ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
00247
00248 }
00249 } else
00250 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
00251 }
00252 }
00253 if (fd != chan->fds[0])
00254 close(fd);
00255 else {
00256
00257
00258 ztc.chan = 0;
00259 ztc.confno = 0;
00260 ztc.confmode = 0;
00261 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
00262 ast_log(LOG_WARNING, "Error setting conference\n");
00263 }
00264 }
00265
00266 outrun:
00267
00268 return ret;
00269 }
00270
00271 static int exec(struct ast_channel *chan, void *data, int dahdimode)
00272 {
00273 int res=-1;
00274 struct ast_module_user *u;
00275 int retrycnt = 0;
00276 int confflags = 0;
00277 int confno = 0;
00278 char confstr[80] = "";
00279
00280 u = ast_module_user_add(chan);
00281
00282 if (!ast_strlen_zero(data)) {
00283 if (dahdimode) {
00284 if ((sscanf(data, "DAHDI/%d", &confno) != 1) &&
00285 (sscanf(data, "%d", &confno) != 1)) {
00286 ast_log(LOG_WARNING, "Argument (if specified) must be a channel number, not '%s'\n", (char *) data);
00287 ast_module_user_remove(u);
00288 return 0;
00289 }
00290 } else {
00291 if ((sscanf(data, "Zap/%d", &confno) != 1) &&
00292 (sscanf(data, "%d", &confno) != 1)) {
00293 ast_log(LOG_WARNING, "Argument (if specified) must be a channel number, not '%s'\n", (char *) data);
00294 ast_module_user_remove(u);
00295 return 0;
00296 }
00297 }
00298 }
00299
00300 if (chan->_state != AST_STATE_UP)
00301 ast_answer(chan);
00302
00303 while(!confno && (++retrycnt < 4)) {
00304
00305 confstr[0] = '\0';
00306 res = ast_app_getdata(chan, "conf-getchannel",confstr, sizeof(confstr) - 1, 0);
00307 if (res <0) goto out;
00308 if (sscanf(confstr, "%d", &confno) != 1)
00309 confno = 0;
00310 }
00311 if (confno) {
00312
00313
00314 res = conf_run(chan, confno, confflags);
00315 }
00316 out:
00317
00318 ast_module_user_remove(u);
00319 return res;
00320 }
00321
00322 static int exec_zap(struct ast_channel *chan, void *data)
00323 {
00324 ast_log(LOG_WARNING, "Use of the command %s is deprecated, please use %s instead.\n", zap_app, dahdi_app);
00325
00326 return exec(chan, data, 0);
00327 }
00328
00329 static int exec_dahdi(struct ast_channel *chan, void *data)
00330 {
00331 return exec(chan, data, 1);
00332 }
00333
00334 static int unload_module(void)
00335 {
00336 int res = 0;
00337
00338 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
00339 res |= ast_unregister_application(dahdi_app);
00340 }
00341
00342 res |= ast_unregister_application(zap_app);
00343
00344 ast_module_user_hangup_all();
00345
00346 return res;
00347 }
00348
00349 static int load_module(void)
00350 {
00351 int res = 0;
00352
00353 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
00354 res |= ast_register_application(dahdi_app, exec_dahdi, dahdi_synopsis, dahdi_descrip);
00355 }
00356
00357 res |= ast_register_application(zap_app, exec_zap, zap_synopsis, zap_descrip);
00358
00359 return res;
00360 }
00361
00362 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Barge in on channel application");