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