build against the now-typedef-free dahdi/user.h, and remove some #ifdefs for features...
[asterisk/asterisk.git] / apps / app_dahdiscan.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 DAHDI Scanner
27  *
28  * \author Mark Spencer <markster@digium.com>
29  *
30  * \ingroup applications
31  */
32
33 /*** MODULEINFO
34         <depend>dahdi</depend>
35  ***/
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include <dahdi/user.h>
42
43 #include "asterisk/lock.h"
44 #include "asterisk/file.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/config.h"
49 #include "asterisk/app.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/say.h"
53 #include "asterisk/options.h"
54
55 static char *app = "DAHDIScan";
56
57 static char *synopsis = "Scan DAHDI channels to monitor calls";
58
59 static char *descrip =
60 "  DAHDIScan([group]) allows a call center manager to monitor DAHDI channels in\n"
61 "a convenient way.  Use '#' to select the next channel and use '*' to exit\n"
62 "Limit scanning to a channel GROUP by setting the option group argument.\n";
63
64
65 #define CONF_SIZE 160
66
67 static struct ast_channel *get_dahdi_channel_locked(int num) {
68         char name[80];
69         
70         snprintf(name, sizeof(name), "DAHDI/%d-1", num);
71         return ast_get_channel_by_name_locked(name);
72 }
73
74 static int careful_write(int fd, unsigned char *data, int len)
75 {
76         int res;
77         while (len) {
78                 res = write(fd, data, len);
79                 if (res < 1) {
80                         if (errno != EAGAIN) {
81                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
82                                 return -1;
83                         } else {
84                                 return 0;
85                         }
86                 }
87                 len -= res;
88                 data += res;
89         }
90         return 0;
91 }
92
93 static int conf_run(struct ast_channel *chan, int confno, int confflags)
94 {
95         int fd;
96         struct dahdi_confinfo dahdic;
97         struct ast_frame *f;
98         struct ast_channel *c;
99         struct ast_frame fr;
100         int outfd;
101         int ms;
102         int nfds;
103         int res;
104         int flags;
105         int retrydahdi;
106         int origfd;
107         int ret = -1;
108         char input[4];
109         int ic = 0;
110         
111         struct dahdi_bufferinfo bi;
112         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
113         char *buf = __buf + AST_FRIENDLY_OFFSET;
114         
115         /* Set it into U-law mode (write) */
116         if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
117                 ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
118                 goto outrun;
119         }
120         
121         /* Set it into U-law mode (read) */
122         if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
123                 ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
124                 goto outrun;
125         }
126         ast_indicate(chan, -1);
127         retrydahdi = strcasecmp(chan->tech->type, "DAHDI");
128  dahdiretry:
129         origfd = chan->fds[0];
130         if (retrydahdi) {
131                 fd = open("/dev/dahdi/pseudo", O_RDWR);
132                 if (fd < 0) {
133                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
134                         goto outrun;
135                 }
136                 /* Make non-blocking */
137                 flags = fcntl(fd, F_GETFL);
138                 if (flags < 0) {
139                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
140                         close(fd);
141                         goto outrun;
142                 }
143                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
144                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
145                         close(fd);
146                         goto outrun;
147                 }
148                 /* Setup buffering information */
149                 memset(&bi, 0, sizeof(bi));
150                 bi.bufsize = CONF_SIZE;
151                 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
152                 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
153                 bi.numbufs = 4;
154                 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
155                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
156                         close(fd);
157                         goto outrun;
158                 }
159                 nfds = 1;
160         } else {
161                 /* XXX Make sure we're not running on a pseudo channel XXX */
162                 fd = chan->fds[0];
163                 nfds = 0;
164         }
165         memset(&dahdic, 0, sizeof(dahdic));
166         /* Check to see if we're in a conference... */
167         dahdic.chan = 0;
168         if (ioctl(fd, DAHDI_GETCONF, &dahdic)) {
169                 ast_log(LOG_WARNING, "Error getting conference\n");
170                 close(fd);
171                 goto outrun;
172         }
173         if (dahdic.confmode) {
174                 /* Whoa, already in a conference...  Retry... */
175                 if (!retrydahdi) {
176                         ast_debug(1, "DAHDI channel is in a conference already, retrying with pseudo\n");
177                         retrydahdi = 1;
178                         goto dahdiretry;
179                 }
180         }
181         memset(&dahdic, 0, sizeof(dahdic));
182         /* Add us to the conference */
183         dahdic.chan = 0;
184         dahdic.confno = confno;
185         dahdic.confmode = DAHDI_CONF_MONITORBOTH;
186
187         if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
188                 ast_log(LOG_WARNING, "Error setting conference\n");
189                 close(fd);
190                 goto outrun;
191         }
192         ast_debug(1, "Placed channel %s in DAHDI channel %d monitor\n", chan->name, confno);
193
194         for (;;) {
195                 outfd = -1;
196                 ms = -1;
197                 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
198                 if (c) {
199                         if (c->fds[0] != origfd) {
200                                 if (retrydahdi) {
201                                         /* Kill old pseudo */
202                                         close(fd);
203                                 }
204                                 ast_debug(1, "Ooh, something swapped out under us, starting over\n");
205                                 retrydahdi = 0;
206                                 goto dahdiretry;
207                         }
208                         f = ast_read(c);
209                         if (!f) {
210                                 break;
211                         }
212                         if (f->frametype == AST_FRAME_DTMF) {
213                                 if (f->subclass == '#') {
214                                         ret = 0;
215                                         break;
216                                 } else if (f->subclass == '*') {
217                                         ret = -1;
218                                         break;
219                                 } else {
220                                         input[ic++] = f->subclass;
221                                 }
222                                 if (ic == 3) {
223                                         input[ic++] = '\0';
224                                         ic = 0;
225                                         ret = atoi(input);
226                                         ast_verb(3, "DAHDIScan: change channel to %d\n", ret);
227                                         break;
228                                 }
229                         }
230
231                         if (fd != chan->fds[0]) {
232                                 if (f->frametype == AST_FRAME_VOICE) {
233                                         if (f->subclass == AST_FORMAT_ULAW) {
234                                                 /* Carefully write */
235                                                 careful_write(fd, f->data.ptr, f->datalen);
236                                         } else {
237                                                 ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
238                                         }
239                                 }
240                         }
241                         ast_frfree(f);
242                 } else if (outfd > -1) {
243                         res = read(outfd, buf, CONF_SIZE);
244                         if (res > 0) {
245                                 memset(&fr, 0, sizeof(fr));
246                                 fr.frametype = AST_FRAME_VOICE;
247                                 fr.subclass = AST_FORMAT_ULAW;
248                                 fr.datalen = res;
249                                 fr.samples = res;
250                                 fr.data.ptr = buf;
251                                 fr.offset = AST_FRIENDLY_OFFSET;
252                                 if (ast_write(chan, &fr) < 0) {
253                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
254                                         /* break; */
255                                 }
256                         } else {
257                                 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
258                         }
259                 }
260         }
261         if (f) {
262                 ast_frfree(f);
263         }
264         if (fd != chan->fds[0]) {
265                 close(fd);
266         } else {
267                 /* Take out of conference */
268                 /* Add us to the conference */
269                 dahdic.chan = 0;
270                 dahdic.confno = 0;
271                 dahdic.confmode = 0;
272                 if (ioctl(fd, DAHDI_SETCONF, &dahdic)) {
273                         ast_log(LOG_WARNING, "Error setting conference\n");
274                 }
275         }
276
277  outrun:
278
279         return ret;
280 }
281
282 static int conf_exec(struct ast_channel *chan, void *data)
283 {
284         int res=-1;
285         int confflags = 0;
286         int confno = 0;
287         char confstr[80] = "", *tmp = NULL;
288         struct ast_channel *tempchan = NULL, *lastchan = NULL, *ichan = NULL;
289         struct ast_frame *f;
290         char *desired_group;
291         int input = 0, search_group = 0;
292
293         if (chan->_state != AST_STATE_UP)
294                 ast_answer(chan);
295
296         desired_group = ast_strdupa(data);
297         if (!ast_strlen_zero(desired_group)) {
298                 ast_verb(3, "Scanning for group %s\n", desired_group);
299                 search_group = 1;
300         }
301
302         for (;;) {
303                 if (ast_waitfor(chan, 100) < 0)
304                         break;
305
306                 f = ast_read(chan);
307                 if (!f)
308                         break;
309                 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
310                         ast_frfree(f);
311                         break;
312                 }
313                 ast_frfree(f);
314                 ichan = NULL;
315                 if(input) {
316                         ichan = get_dahdi_channel_locked(input);
317                         input = 0;
318                 }
319
320                 tempchan = ichan ? ichan : ast_channel_walk_locked(tempchan);
321
322                 if (!tempchan && !lastchan) {
323                         break;
324                 }
325
326                 if (tempchan && search_group) {
327                         const char *mygroup;
328                         if ((mygroup = pbx_builtin_getvar_helper(tempchan, "GROUP")) && (!strcmp(mygroup, desired_group))) {
329                                 ast_verb(3, "Found Matching Channel %s in group %s\n", tempchan->name, desired_group);
330                         } else {
331                                 ast_channel_unlock(tempchan);
332                                 lastchan = tempchan;
333                                 continue;
334                         }
335                 }
336                 if (tempchan && (!strcmp(tempchan->tech->type, "DAHDI")) && (tempchan != chan)) {
337                         ast_verb(3, "DAHDI channel %s is in-use, monitoring...\n", tempchan->name);
338                         ast_copy_string(confstr, tempchan->name, sizeof(confstr));
339                         ast_channel_unlock(tempchan);
340                         if ((tmp = strchr(confstr, '-'))) {
341                                 *tmp = '\0';
342                         }
343                         confno = atoi(strchr(confstr, '/') + 1);
344                         ast_stopstream(chan);
345                         ast_say_number(chan, confno, AST_DIGIT_ANY, chan->language, (char *) NULL);
346                         res = conf_run(chan, confno, confflags);
347                         if (res < 0) {
348                                 break;
349                         }
350                         input = res;
351                 } else if (tempchan) {
352                         ast_channel_unlock(tempchan);
353                 }
354                 lastchan = tempchan;
355         }
356         return res;
357 }
358
359 static int unload_module(void)
360 {
361         return ast_unregister_application(app);
362 }
363
364 static int load_module(void)
365 {
366         return ((ast_register_application(app, conf_exec, synopsis, descrip)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS);
367 }
368
369 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Scan DAHDI channels application");
370