Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / channels / chan_features.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  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief feature Proxy Channel
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \note *** Experimental code ****
26  * 
27  * \ingroup channel_drivers
28  */
29 /*** MODULEINFO
30         <defaultenabled>no</defaultenabled>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/socket.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <netdb.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/signal.h>
48
49 #include "asterisk/lock.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/config.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/module.h"
54 #include "asterisk/pbx.h"
55 #include "asterisk/options.h"
56 #include "asterisk/lock.h"
57 #include "asterisk/sched.h"
58 #include "asterisk/io.h"
59 #include "asterisk/rtp.h"
60 #include "asterisk/acl.h"
61 #include "asterisk/callerid.h"
62 #include "asterisk/file.h"
63 #include "asterisk/cli.h"
64 #include "asterisk/app.h"
65 #include "asterisk/musiconhold.h"
66 #include "asterisk/manager.h"
67 #include "asterisk/stringfields.h"
68
69 static const char tdesc[] = "Feature Proxy Channel Driver";
70
71 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
72
73 struct feature_sub {
74         struct ast_channel *owner;
75         int inthreeway;
76         int pfd;
77         int timingfdbackup;
78         int alertpipebackup[2];
79 };
80
81 struct feature_pvt {
82         ast_mutex_t lock;                       /* Channel private lock */
83         char tech[AST_MAX_EXTENSION];           /* Technology to abstract */
84         char dest[AST_MAX_EXTENSION];           /* Destination to abstract */
85         struct ast_channel *subchan;
86         struct feature_sub subs[3];             /* Subs */
87         struct ast_channel *owner;              /* Current Master Channel */
88         AST_LIST_ENTRY(feature_pvt) list;       /* Next entity */
89 };
90
91 static AST_LIST_HEAD_STATIC(features, feature_pvt);
92
93 #define SUB_REAL        0                       /* Active call */
94 #define SUB_CALLWAIT    1                       /* Call-Waiting call on hold */
95 #define SUB_THREEWAY    2                       /* Three-way call */
96
97 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
98 static int features_digit_begin(struct ast_channel *ast, char digit);
99 static int features_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
100 static int features_call(struct ast_channel *ast, char *dest, int timeout);
101 static int features_hangup(struct ast_channel *ast);
102 static int features_answer(struct ast_channel *ast);
103 static struct ast_frame *features_read(struct ast_channel *ast);
104 static int features_write(struct ast_channel *ast, struct ast_frame *f);
105 static int features_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
106 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
107
108 static const struct ast_channel_tech features_tech = {
109         .type = "Feature",
110         .description = tdesc,
111         .capabilities = -1,
112         .requester = features_request,
113         .send_digit_begin = features_digit_begin,
114         .send_digit_end = features_digit_end,
115         .call = features_call,
116         .hangup = features_hangup,
117         .answer = features_answer,
118         .read = features_read,
119         .write = features_write,
120         .exception = features_read,
121         .indicate = features_indicate,
122         .fixup = features_fixup,
123 };
124
125 static inline void init_sub(struct feature_sub *sub)
126 {
127         sub->inthreeway = 0;
128         sub->pfd = -1;
129         sub->timingfdbackup = -1;
130         sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;
131 }
132
133 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
134 {
135         int x;
136         if (!owner) {
137                 ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
138                 return -1;
139         }
140         for (x=0; x<3; x++) {
141                 if (owner == p->subs[x].owner)
142                         return x;
143         }
144         return -1;
145 }
146
147 #if 0
148 static void wakeup_sub(struct feature_pvt *p, int a)
149 {
150         struct ast_frame null = { AST_FRAME_NULL, };
151         for (;;) {
152                 if (p->subs[a].owner) {
153                         if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
154                                 ast_mutex_unlock(&p->lock);
155                                 usleep(1);
156                                 ast_mutex_lock(&p->lock);
157                         } else {
158                                 ast_queue_frame(p->subs[a].owner, &null);
159                                 ast_mutex_unlock(&p->subs[a].owner->lock);
160                                 break;
161                         }
162                 } else
163                         break;
164         }
165 }
166 #endif
167
168 static void restore_channel(struct feature_pvt *p, int index)
169 {
170         /* Restore timing/alertpipe */
171         p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
172         p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
173         p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
174         p->subs[index].owner->fds[AST_ALERT_FD] = p->subs[index].alertpipebackup[0];
175         p->subs[index].owner->fds[AST_TIMING_FD] = p->subs[index].timingfdbackup;
176 }
177
178 static void update_features(struct feature_pvt *p, int index)
179 {
180         int x;
181         if (p->subs[index].owner) {
182                 for (x=0; x<AST_MAX_FDS; x++) {
183                         if (index) 
184                                 p->subs[index].owner->fds[x] = -1;
185                         else
186                                 p->subs[index].owner->fds[x] = p->subchan->fds[x];
187                 }
188                 if (!index) {
189                         /* Copy timings from master channel */
190                         p->subs[index].owner->timingfd = p->subchan->timingfd;
191                         p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0];
192                         p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1];
193                         if (p->subs[index].owner->nativeformats != p->subchan->readformat) {
194                                 p->subs[index].owner->nativeformats = p->subchan->readformat;
195                                 if (p->subs[index].owner->readformat)
196                                         ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat);
197                                 if (p->subs[index].owner->writeformat)
198                                         ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat);
199                         }
200                 } else{
201                         restore_channel(p, index);
202                 }
203         }
204 }
205
206 #if 0
207 static void swap_subs(struct feature_pvt *p, int a, int b)
208 {
209         int tinthreeway;
210         struct ast_channel *towner;
211
212         ast_debug(1, "Swapping %d and %d\n", a, b);
213
214         towner = p->subs[a].owner;
215         tinthreeway = p->subs[a].inthreeway;
216
217         p->subs[a].owner = p->subs[b].owner;
218         p->subs[a].inthreeway = p->subs[b].inthreeway;
219
220         p->subs[b].owner = towner;
221         p->subs[b].inthreeway = tinthreeway;
222         update_features(p,a);
223         update_features(p,b);
224         wakeup_sub(p, a);
225         wakeup_sub(p, b);
226 }
227 #endif
228
229 static int features_answer(struct ast_channel *ast)
230 {
231         struct feature_pvt *p = ast->tech_pvt;
232         int res = -1;
233         int x;
234
235         ast_mutex_lock(&p->lock);
236         x = indexof(p, ast, 0);
237         if (!x && p->subchan)
238                 res = ast_answer(p->subchan);
239         ast_mutex_unlock(&p->lock);
240         return res;
241 }
242
243 static struct ast_frame  *features_read(struct ast_channel *ast)
244 {
245         struct feature_pvt *p = ast->tech_pvt;
246         struct ast_frame *f;
247         int x;
248         
249         f = &ast_null_frame;
250         ast_mutex_lock(&p->lock);
251         x = indexof(p, ast, 0);
252         if (!x && p->subchan) {
253                 update_features(p, x);
254                 f = ast_read(p->subchan);
255         }
256         ast_mutex_unlock(&p->lock);
257         return f;
258 }
259
260 static int features_write(struct ast_channel *ast, struct ast_frame *f)
261 {
262         struct feature_pvt *p = ast->tech_pvt;
263         int res = -1;
264         int x;
265
266         ast_mutex_lock(&p->lock);
267         x = indexof(p, ast, 0);
268         if (!x && p->subchan)
269                 res = ast_write(p->subchan, f);
270         ast_mutex_unlock(&p->lock);
271         return res;
272 }
273
274 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
275 {
276         struct feature_pvt *p = newchan->tech_pvt;
277         int x;
278
279         ast_mutex_lock(&p->lock);
280         if (p->owner == oldchan)
281                 p->owner = newchan;
282         for (x = 0; x < 3; x++) {
283                 if (p->subs[x].owner == oldchan)
284                         p->subs[x].owner = newchan;
285         }
286         ast_mutex_unlock(&p->lock);
287         return 0;
288 }
289
290 static int features_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
291 {
292         struct feature_pvt *p = ast->tech_pvt;
293         int res = -1;
294         int x;
295
296         /* Queue up a frame representing the indication as a control frame */
297         ast_mutex_lock(&p->lock);
298         x = indexof(p, ast, 0);
299         if (!x && p->subchan)
300                 res = ast_indicate(p->subchan, condition);
301         ast_mutex_unlock(&p->lock);
302         return res;
303 }
304
305 static int features_digit_begin(struct ast_channel *ast, char digit)
306 {
307         struct feature_pvt *p = ast->tech_pvt;
308         int res = -1;
309         int x;
310
311         /* Queue up a frame representing the indication as a control frame */
312         ast_mutex_lock(&p->lock);
313         x = indexof(p, ast, 0);
314         if (!x && p->subchan)
315                 res = ast_senddigit_begin(p->subchan, digit);
316         ast_mutex_unlock(&p->lock);
317
318         return res;
319 }
320
321 static int features_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
322 {
323         struct feature_pvt *p = ast->tech_pvt;
324         int res = -1;
325         int x;
326
327         /* Queue up a frame representing the indication as a control frame */
328         ast_mutex_lock(&p->lock);
329         x = indexof(p, ast, 0);
330         if (!x && p->subchan)
331                 res = ast_senddigit_end(p->subchan, digit, duration);
332         ast_mutex_unlock(&p->lock);
333         return res;
334 }
335
336 static int features_call(struct ast_channel *ast, char *dest, int timeout)
337 {
338         struct feature_pvt *p = ast->tech_pvt;
339         int res = -1;
340         int x;
341         char *dest2;
342                 
343         dest2 = strchr(dest, '/');
344         if (dest2) {
345                 ast_mutex_lock(&p->lock);
346                 x = indexof(p, ast, 0);
347                 if (!x && p->subchan) {
348                         p->subchan->cid.cid_num = ast_strdup(p->owner->cid.cid_num);
349                         p->subchan->cid.cid_name = ast_strdup(p->owner->cid.cid_name);
350                         p->subchan->cid.cid_rdnis = ast_strdup(p->owner->cid.cid_rdnis);
351                         p->subchan->cid.cid_ani = ast_strdup(p->owner->cid.cid_ani);
352                 
353                         p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
354                         ast_string_field_set(p->subchan, language, p->owner->language);
355                         ast_string_field_set(p->subchan, accountcode, p->owner->accountcode);
356                         p->subchan->cdrflags = p->owner->cdrflags;
357                         res = ast_call(p->subchan, dest2, timeout);
358                         update_features(p, x);
359                 } else
360                         ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
361                 ast_mutex_unlock(&p->lock);
362         }
363         return res;
364 }
365
366 static int features_hangup(struct ast_channel *ast)
367 {
368         struct feature_pvt *p = ast->tech_pvt;
369         int x;
370
371         ast_mutex_lock(&p->lock);
372         x = indexof(p, ast, 0);
373         if (x > -1) {
374                 restore_channel(p, x);
375                 p->subs[x].owner = NULL;
376                 /* XXX Re-arrange, unconference, etc XXX */
377         }
378         ast->tech_pvt = NULL;
379         
380         if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
381                 ast_mutex_unlock(&p->lock);
382                 /* Remove from list */
383                 AST_LIST_LOCK(&features);
384                 AST_LIST_REMOVE(&features, p, list);
385                 AST_LIST_UNLOCK(&features);
386                 ast_mutex_lock(&p->lock);
387                 /* And destroy */
388                 if (p->subchan)
389                         ast_hangup(p->subchan);
390                 ast_mutex_unlock(&p->lock);
391                 ast_mutex_destroy(&p->lock);
392                 ast_free(p);
393                 return 0;
394         }
395         ast_mutex_unlock(&p->lock);
396         return 0;
397 }
398
399 static struct feature_pvt *features_alloc(char *data, int format)
400 {
401         struct feature_pvt *tmp;
402         char *dest=NULL;
403         char *tech;
404         int x;
405         int status;
406         struct ast_channel *chan;
407         
408         tech = ast_strdupa(data);
409         if (tech) {
410                 dest = strchr(tech, '/');
411                 if (dest) {
412                         *dest = '\0';
413                         dest++;
414                 }
415         }
416         if (!tech || !dest) {
417                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
418                         data);
419                 return NULL;
420         }
421         AST_LIST_LOCK(&features);
422         AST_LIST_TRAVERSE(&features, tmp, list) {
423                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
424                         break;
425         }
426         AST_LIST_UNLOCK(&features);
427         if (!tmp) {
428                 chan = ast_request(tech, format, dest, &status);
429                 if (!chan) {
430                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
431                         return NULL;
432                 }
433                 tmp = ast_calloc(1, sizeof(*tmp));
434                 if (tmp) {
435                         for (x=0;x<3;x++)
436                                 init_sub(tmp->subs + x);
437                         ast_mutex_init(&tmp->lock);
438                         ast_copy_string(tmp->tech, tech, sizeof(tmp->tech));
439                         ast_copy_string(tmp->dest, dest, sizeof(tmp->dest));
440                         tmp->subchan = chan;
441                         AST_LIST_LOCK(&features);
442                         AST_LIST_INSERT_HEAD(&features, tmp, list);
443                         AST_LIST_UNLOCK(&features);
444                 }
445         }
446         return tmp;
447 }
448
449 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
450 {
451         struct ast_channel *tmp;
452         int x,y;
453         char *b2 = 0;
454         if (!p->subchan) {
455                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
456                 return NULL;
457         }
458         if (p->subs[index].owner) {
459                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
460                 return NULL;
461         }
462         /* figure out what you want the name to be */
463         for (x=1;x<4;x++) {
464                 if (b2)
465                         ast_free(b2);
466                 asprintf(&b2, "%s/%s-%d", p->tech, p->dest, x);
467                 for (y=0;y<3;y++) {
468                         if (y == index)
469                                 continue;
470                         if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, b2))
471                                 break;
472                 }
473                 if (y >= 3)
474                         break;
475         }
476         tmp = ast_channel_alloc(0, state, 0,0, "", "", "", 0, "Feature/%s", b2);
477         /* free up the name, it was copied into the channel name */
478         if (b2)
479                 ast_free(b2);
480         if (!tmp) {
481                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
482                 return NULL;
483         }
484         tmp->tech = &features_tech;
485         tmp->writeformat = p->subchan->writeformat;
486         tmp->rawwriteformat = p->subchan->rawwriteformat;
487         tmp->readformat = p->subchan->readformat;
488         tmp->rawreadformat = p->subchan->rawreadformat;
489         tmp->nativeformats = p->subchan->readformat;
490         tmp->tech_pvt = p;
491         p->subs[index].owner = tmp;
492         if (!p->owner)
493                 p->owner = tmp;
494         ast_module_ref(ast_module_info->self);
495         return tmp;
496 }
497
498
499 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
500 {
501         struct feature_pvt *p;
502         struct ast_channel *chan = NULL;
503
504         p = features_alloc(data, format);
505         if (p && !p->subs[SUB_REAL].owner)
506                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
507         if (chan)
508                 update_features(p,SUB_REAL);
509         return chan;
510 }
511
512 static int features_show(int fd, int argc, char **argv)
513 {
514         struct feature_pvt *p;
515
516         if (argc != 3)
517                 return RESULT_SHOWUSAGE;
518
519         if (AST_LIST_EMPTY(&features)) {
520                 ast_cli(fd, "No feature channels in use\n");
521                 return RESULT_SUCCESS;
522         }
523
524         AST_LIST_LOCK(&features);
525         AST_LIST_TRAVERSE(&features, p, list) {
526                 ast_mutex_lock(&p->lock);
527                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
528                 ast_mutex_unlock(&p->lock);
529         }
530         AST_LIST_UNLOCK(&features);
531         return RESULT_SUCCESS;
532 }
533
534 static const char show_features_usage[] = 
535 "Usage: feature show channels\n"
536 "       Provides summary information on feature channels.\n";
537
538 static struct ast_cli_entry cli_features[] = {
539         { { "feature", "show", "channels", NULL },
540         features_show, "List status of feature channels",
541         show_features_usage },
542 };
543
544 static int load_module(void)
545 {
546         /* Make sure we can register our sip channel type */
547         if (ast_channel_register(&features_tech)) {
548                 ast_log(LOG_ERROR, "Unable to register channel class 'Feature'\n");
549                 return -1;
550         }
551         ast_cli_register_multiple(cli_features, sizeof(cli_features) / sizeof(struct ast_cli_entry));
552         return 0;
553 }
554
555 static int unload_module(void)
556 {
557         struct feature_pvt *p;
558         
559         /* First, take us out of the channel loop */
560         ast_cli_unregister_multiple(cli_features, sizeof(cli_features) / sizeof(struct ast_cli_entry));
561         ast_channel_unregister(&features_tech);
562         
563         if (!AST_LIST_LOCK(&features))
564                 return -1;
565         /* Hangup all interfaces if they have an owner */
566         AST_LIST_TRAVERSE_SAFE_BEGIN(&features, p, list) {
567                 if (p->owner)
568                         ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
569                 AST_LIST_REMOVE_CURRENT(&features, list);
570                 ast_free(p);
571         }
572         AST_LIST_TRAVERSE_SAFE_END
573         AST_LIST_UNLOCK(&features);
574         
575         return 0;
576 }
577
578 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Feature Proxy Channel");
579