a59edc9af1cac56484c67d73eeeb889045b19847
[asterisk/asterisk.git] / apps / app_dahdibarge.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  * Special thanks to comphealth.com for sponsoring this
9  * GPL application.
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21
22 /*! \file
23  *
24  * \brief DAHDI Barge support
25  *
26  * \author Mark Spencer <markster@digium.com>
27  *
28  * \note Special thanks to comphealth.com for sponsoring this
29  * GPL application.
30  * 
31  * \ingroup applications
32  */
33
34 /*** MODULEINFO
35         <depend>dahdi</depend>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include <dahdi/user.h>
43
44 #include "asterisk/lock.h"
45 #include "asterisk/file.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/module.h"
49 #include "asterisk/config.h"
50 #include "asterisk/app.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/say.h"
53 #include "asterisk/utils.h"
54
55 static char *app = "DAHDIBarge";
56
57 static char *synopsis = "Barge in (monitor) DAHDI channel";
58
59 static char *descrip = 
60 "  DAHDIBarge([channel]): Barges in on a specified DAHDI\n"
61 "channel or prompts if one is not specified.  Returns\n"
62 "-1 when caller user hangs up and is independent of the\n"
63 "state of the channel being monitored.";
64
65
66 #define CONF_SIZE 160
67
68 static int careful_write(int fd, unsigned char *data, int len)
69 {
70         int res;
71         while(len) {
72                 res = write(fd, data, len);
73                 if (res < 1) {
74                         if (errno != EAGAIN) {
75                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
76                                 return -1;
77                         } else
78                                 return 0;
79                 }
80                 len -= res;
81                 data += res;
82         }
83         return 0;
84 }
85
86 static int conf_run(struct ast_channel *chan, int confno, int confflags)
87 {
88         int fd;
89         struct dahdi_confinfo dahdic;
90         struct ast_frame *f;
91         struct ast_channel *c;
92         struct ast_frame fr;
93         int outfd;
94         int ms;
95         int nfds;
96         int res;
97         int flags;
98         int retrydahdi;
99         int origfd;
100         int ret = -1;
101
102         DAHDI_BUFFERINFO bi;
103         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
104         char *buf = __buf + AST_FRIENDLY_OFFSET;
105
106         /* Set it into U-law mode (write) */
107         if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
108                 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
109                 goto outrun;
110         }
111
112         /* Set it into U-law mode (read) */
113         if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
114                 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
115                 goto outrun;
116         }
117         ast_indicate(chan, -1);
118         retrydahdi = strcasecmp(chan->tech->type, "DAHDI");
119 dahdiretry:
120         origfd = chan->fds[0];
121         if (retrydahdi) {
122                 fd = open("/dev/dahdi/pseudo", O_RDWR);
123                 if (fd < 0) {
124                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
125                         goto outrun;
126                 }
127                 /* Make non-blocking */
128                 flags = fcntl(fd, F_GETFL);
129                 if (flags < 0) {
130                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
131                         close(fd);
132                         goto outrun;
133                 }
134                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
135                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
136                         close(fd);
137                         goto outrun;
138                 }
139                 /* Setup buffering information */
140                 memset(&bi, 0, sizeof(bi));
141                 bi.bufsize = CONF_SIZE;
142                 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
143                 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
144                 bi.numbufs = 4;
145                 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
146                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
147                         close(fd);
148                         goto outrun;
149                 }
150                 nfds = 1;
151         } else {
152                 /* XXX Make sure we're not running on a pseudo channel XXX */
153                 fd = chan->fds[0];
154                 nfds = 0;
155         }
156         memset(&dahdic, 0, sizeof(dahdic));
157         /* Check to see if we're in a conference... */
158         dahdic.chan = 0;        
159         if (ioctl(fd, DAHDI_GETCONF, &dahdic)) {
160                 ast_log(LOG_WARNING, "Error getting conference\n");
161                 close(fd);
162                 goto outrun;
163         }
164         if (dahdic.confmode) {
165                 /* Whoa, already in a conference...  Retry... */
166                 if (!retrydahdi) {
167                         ast_debug(1, "DAHDI channel is in a conference already, retrying with pseudo\n");
168                         retrydahdi = 1;
169                         goto dahdiretry;
170                 }
171         }
172         memset(&dahdic, 0, sizeof(dahdic));
173         /* Add us to the conference */
174         dahdic.chan = 0;        
175         dahdic.confno = confno;
176         dahdic.confmode = DAHDI_CONF_MONITORBOTH;
177
178         if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
179                 ast_log(LOG_WARNING, "Error setting conference\n");
180                 close(fd);
181                 goto outrun;
182         }
183         ast_debug(1, "Placed channel %s in DAHDI channel %d monitor\n", chan->name, confno);
184
185         for(;;) {
186                 outfd = -1;
187                 ms = -1;
188                 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
189                 if (c) {
190                         if (c->fds[0] != origfd) {
191                                 if (retrydahdi) {
192                                         /* Kill old pseudo */
193                                         close(fd);
194                                 }
195                                 ast_debug(1, "Ooh, something swapped out under us, starting over\n");
196                                 retrydahdi = 0;
197                                 goto dahdiretry;
198                         }
199                         f = ast_read(c);
200                         if (!f) 
201                                 break;
202                         if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
203                                 ret = 0;
204                                 ast_frfree(f);
205                                 break;
206                         } else if (fd != chan->fds[0]) {
207                                 if (f->frametype == AST_FRAME_VOICE) {
208                                         if (f->subclass == AST_FORMAT_ULAW) {
209                                                 /* Carefully write */
210                                                 careful_write(fd, f->data.ptr, f->datalen);
211                                         } else
212                                                 ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
213                                 }
214                         }
215                         ast_frfree(f);
216                 } else if (outfd > -1) {
217                         res = read(outfd, buf, CONF_SIZE);
218                         if (res > 0) {
219                                 memset(&fr, 0, sizeof(fr));
220                                 fr.frametype = AST_FRAME_VOICE;
221                                 fr.subclass = AST_FORMAT_ULAW;
222                                 fr.datalen = res;
223                                 fr.samples = res;
224                                 fr.data.ptr = buf;
225                                 fr.offset = AST_FRIENDLY_OFFSET;
226                                 if (ast_write(chan, &fr) < 0) {
227                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
228                                         /* break; */
229                                 }
230                         } else 
231                                 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
232                 }
233         }
234         if (fd != chan->fds[0])
235                 close(fd);
236         else {
237                 /* Take out of conference */
238                 /* Add us to the conference */
239                 dahdic.chan = 0;        
240                 dahdic.confno = 0;
241                 dahdic.confmode = 0;
242                 if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
243                         ast_log(LOG_WARNING, "Error setting conference\n");
244                 }
245         }
246
247 outrun:
248
249         return ret;
250 }
251
252 static int conf_exec(struct ast_channel *chan, void *data)
253 {
254         int res=-1;
255         int retrycnt = 0;
256         int confflags = 0;
257         int confno = 0;
258         char confstr[80] = "";
259         
260         if (!ast_strlen_zero(data)) {
261                 if ((sscanf(data, "DAHDI/%d", &confno) != 1) &&
262                     (sscanf(data, "%d", &confno) != 1)) {
263                         ast_log(LOG_WARNING, "DAHDIBarge Argument (if specified) must be a channel number, not '%s'\n", (char *)data);
264                         return 0;
265                 }
266         }
267         
268         if (chan->_state != AST_STATE_UP)
269                 ast_answer(chan);
270
271         while(!confno && (++retrycnt < 4)) {
272                 /* Prompt user for conference number */
273                 confstr[0] = '\0';
274                 res = ast_app_getdata(chan, "conf-getchannel",confstr, sizeof(confstr) - 1, 0);
275                 if (res <0) goto out;
276                 if (sscanf(confstr, "%d", &confno) != 1)
277                         confno = 0;
278         }
279         if (confno) {
280                 /* XXX Should prompt user for pin if pin is required XXX */
281                 /* Run the conference */
282                 res = conf_run(chan, confno, confflags);
283         }
284 out:
285         /* Do the conference */
286         return res;
287 }
288
289 static int unload_module(void)
290 {
291         return ast_unregister_application(app);
292 }
293
294 static int load_module(void)
295 {
296         return ((ast_register_application(app, conf_exec, synopsis, descrip)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS);
297 }
298
299 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Barge in on DAHDI channel application");