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