update doxygen docs to specify authors
[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
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <netdb.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <sys/signal.h>
41
42 #include "asterisk.h"
43
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
45
46 #include "asterisk/lock.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/config.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/module.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/options.h"
53 #include "asterisk/lock.h"
54 #include "asterisk/sched.h"
55 #include "asterisk/io.h"
56 #include "asterisk/rtp.h"
57 #include "asterisk/acl.h"
58 #include "asterisk/callerid.h"
59 #include "asterisk/file.h"
60 #include "asterisk/cli.h"
61 #include "asterisk/app.h"
62 #include "asterisk/musiconhold.h"
63 #include "asterisk/manager.h"
64
65 static const char desc[] = "Feature Proxy Channel";
66 static const char type[] = "Feature";
67 static const char tdesc[] = "Feature Proxy Channel Driver";
68
69 static int usecnt =0;
70 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
71
72 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
73
74 /* Protect the interface list (of feature_pvt's) */
75 AST_MUTEX_DEFINE_STATIC(featurelock);
76
77 struct feature_sub {
78         struct ast_channel *owner;
79         int inthreeway;
80         int pfd;
81         int timingfdbackup;
82         int alertpipebackup[2];
83 };
84
85 static struct feature_pvt {
86         ast_mutex_t lock;                       /* Channel private lock */
87         char tech[AST_MAX_EXTENSION];           /* Technology to abstract */
88         char dest[AST_MAX_EXTENSION];           /* Destination to abstract */
89         struct ast_channel *subchan;
90         struct feature_sub subs[3];             /* Subs */
91         struct ast_channel *owner;              /* Current Master Channel */
92         struct feature_pvt *next;               /* Next entity */
93 } *features = NULL;
94
95 #define SUB_REAL        0                       /* Active call */
96 #define SUB_CALLWAIT    1                       /* Call-Waiting call on hold */
97 #define SUB_THREEWAY    2                       /* Three-way call */
98
99 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
100 static int features_digit(struct ast_channel *ast, char digit);
101 static int features_call(struct ast_channel *ast, char *dest, int timeout);
102 static int features_hangup(struct ast_channel *ast);
103 static int features_answer(struct ast_channel *ast);
104 static struct ast_frame *features_read(struct ast_channel *ast);
105 static int features_write(struct ast_channel *ast, struct ast_frame *f);
106 static int features_indicate(struct ast_channel *ast, int condition);
107 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
108
109 static const struct ast_channel_tech features_tech = {
110         .type = type,
111         .description = tdesc,
112         .capabilities = -1,
113         .requester = features_request,
114         .send_digit = features_digit,
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_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
175         p->subs[index].owner->fds[AST_MAX_FDS-2] = 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_log(LOG_DEBUG, "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         static struct ast_frame null_frame = { AST_FRAME_NULL, };
246         struct feature_pvt *p = ast->tech_pvt;
247         struct ast_frame *f;
248         int x;
249         
250         f = &null_frame;
251         ast_mutex_lock(&p->lock);
252         x = indexof(p, ast, 0);
253         if (!x && p->subchan) {
254                 update_features(p, x);
255                 f = ast_read(p->subchan);
256         }
257         ast_mutex_unlock(&p->lock);
258         return f;
259 }
260
261 static int features_write(struct ast_channel *ast, struct ast_frame *f)
262 {
263         struct feature_pvt *p = ast->tech_pvt;
264         int res = -1;
265         int x;
266
267         ast_mutex_lock(&p->lock);
268         x = indexof(p, ast, 0);
269         if (!x && p->subchan)
270                 res = ast_write(p->subchan, f);
271         ast_mutex_unlock(&p->lock);
272         return res;
273 }
274
275 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
276 {
277         struct feature_pvt *p = newchan->tech_pvt;
278         int x;
279
280         ast_mutex_lock(&p->lock);
281         if (p->owner == oldchan)
282                 p->owner = newchan;
283         for (x = 0; x < 3; x++) {
284                 if (p->subs[x].owner == oldchan)
285                         p->subs[x].owner = newchan;
286         }
287         ast_mutex_unlock(&p->lock);
288         return 0;
289 }
290
291 static int features_indicate(struct ast_channel *ast, int condition)
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_indicate(p->subchan, condition);
302         ast_mutex_unlock(&p->lock);
303         return res;
304 }
305
306 static int features_digit(struct ast_channel *ast, char digit)
307 {
308         struct feature_pvt *p = ast->tech_pvt;
309         int res = -1;
310         int x;
311
312         /* Queue up a frame representing the indication as a control frame */
313         ast_mutex_lock(&p->lock);
314         x = indexof(p, ast, 0);
315         if (!x && p->subchan)
316                 res = ast_senddigit(p->subchan, digit);
317         ast_mutex_unlock(&p->lock);
318         return res;
319 }
320
321 static int features_call(struct ast_channel *ast, char *dest, int timeout)
322 {
323         struct feature_pvt *p = ast->tech_pvt;
324         int res = -1;
325         int x;
326         char *dest2;
327                 
328         dest2 = strchr(dest, '/');
329         if (dest2) {
330                 ast_mutex_lock(&p->lock);
331                 x = indexof(p, ast, 0);
332                 if (!x && p->subchan) {
333                         if (p->owner->cid.cid_num)
334                                 p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
335                         else 
336                                 p->subchan->cid.cid_num = NULL;
337                 
338                         if (p->owner->cid.cid_name)
339                                 p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
340                         else 
341                                 p->subchan->cid.cid_name = NULL;
342                 
343                         if (p->owner->cid.cid_rdnis)
344                                 p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
345                         else
346                                 p->subchan->cid.cid_rdnis = NULL;
347                 
348                         if (p->owner->cid.cid_ani)
349                                 p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
350                         else
351                                 p->subchan->cid.cid_ani = NULL;
352                 
353                         p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
354                         strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
355                         strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
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         struct feature_pvt *cur, *prev=NULL;
370         int x;
371
372         ast_mutex_lock(&p->lock);
373         x = indexof(p, ast, 0);
374         if (x > -1) {
375                 restore_channel(p, x);
376                 p->subs[x].owner = NULL;
377                 /* XXX Re-arrange, unconference, etc XXX */
378         }
379         ast->tech_pvt = NULL;
380         
381         
382         if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
383                 ast_mutex_unlock(&p->lock);
384                 /* Remove from list */
385                 ast_mutex_lock(&featurelock);
386                 cur = features;
387                 while(cur) {
388                         if (cur == p) {
389                                 if (prev)
390                                         prev->next = cur->next;
391                                 else
392                                         features = cur->next;
393                                 break;
394                         }
395                         prev = cur;
396                         cur = cur->next;
397                 }
398                 ast_mutex_unlock(&featurelock);
399                 ast_mutex_lock(&p->lock);
400                 /* And destroy */
401                 if (p->subchan)
402                         ast_hangup(p->subchan);
403                 ast_mutex_unlock(&p->lock);
404                 ast_mutex_destroy(&p->lock);
405                 free(p);
406                 return 0;
407         }
408         ast_mutex_unlock(&p->lock);
409         return 0;
410 }
411
412 static struct feature_pvt *features_alloc(char *data, int format)
413 {
414         struct feature_pvt *tmp;
415         char *dest=NULL;
416         char *tech;
417         int x;
418         int status;
419         struct ast_channel *chan;
420         
421         tech = ast_strdupa(data);
422         if (tech) {
423                 dest = strchr(tech, '/');
424                 if (dest) {
425                         *dest = '\0';
426                         dest++;
427                 }
428         }
429         if (!tech || !dest) {
430                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
431                         data);
432                 return NULL;
433         }
434         ast_mutex_lock(&featurelock);
435         tmp = features;
436         while(tmp) {
437                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
438                         break;
439                 tmp = tmp->next;
440         }
441         ast_mutex_unlock(&featurelock);
442         if (!tmp) {
443                 chan = ast_request(tech, format, dest, &status);
444                 if (!chan) {
445                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
446                         return NULL;
447                 }
448                 tmp = malloc(sizeof(struct feature_pvt));
449                 if (tmp) {
450                         memset(tmp, 0, sizeof(struct feature_pvt));
451                         for (x=0;x<3;x++)
452                                 init_sub(tmp->subs + x);
453                         ast_mutex_init(&tmp->lock);
454                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
455                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
456                         tmp->subchan = chan;
457                         ast_mutex_lock(&featurelock);
458                         tmp->next = features;
459                         features = tmp;
460                         ast_mutex_unlock(&featurelock);
461                 }
462         }
463         return tmp;
464 }
465
466 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
467 {
468         struct ast_channel *tmp;
469         int x,y;
470         if (!p->subchan) {
471                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
472                 return NULL;
473         }
474         if (p->subs[index].owner) {
475                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
476                 return NULL;
477         }
478         tmp = ast_channel_alloc(0);
479         if (!tmp) {
480                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
481                 return NULL;
482         }
483         tmp->tech = &features_tech;
484         for (x=1;x<4;x++) {
485                 snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
486                 for (y=0;y<3;y++) {
487                         if (y == index)
488                                 continue;
489                         if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
490                                 break;
491                 }
492                 if (y >= 3)
493                         break;
494         }
495         tmp->type = type;
496         ast_setstate(tmp, state);
497         tmp->writeformat = p->subchan->writeformat;
498         tmp->rawwriteformat = p->subchan->rawwriteformat;
499         tmp->readformat = p->subchan->readformat;
500         tmp->rawreadformat = p->subchan->rawreadformat;
501         tmp->nativeformats = p->subchan->readformat;
502         tmp->tech_pvt = p;
503         p->subs[index].owner = tmp;
504         if (!p->owner)
505                 p->owner = tmp;
506         ast_mutex_lock(&usecnt_lock);
507         usecnt++;
508         ast_mutex_unlock(&usecnt_lock);
509         ast_update_use_count();
510         return tmp;
511 }
512
513
514 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
515 {
516         struct feature_pvt *p;
517         struct ast_channel *chan = NULL;
518
519         p = features_alloc(data, format);
520         if (p && !p->subs[SUB_REAL].owner)
521                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
522         if (chan)
523                 update_features(p,SUB_REAL);
524         return chan;
525 }
526
527 static int features_show(int fd, int argc, char **argv)
528 {
529         struct feature_pvt *p;
530
531         if (argc != 3)
532                 return RESULT_SHOWUSAGE;
533         ast_mutex_lock(&featurelock);
534         p = features;
535         while(p) {
536                 ast_mutex_lock(&p->lock);
537                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
538                 ast_mutex_unlock(&p->lock);
539                 p = p->next;
540         }
541         if (!features)
542                 ast_cli(fd, "No feature channels in use\n");
543         ast_mutex_unlock(&featurelock);
544         return RESULT_SUCCESS;
545 }
546
547 static char show_features_usage[] = 
548 "Usage: feature show channels\n"
549 "       Provides summary information on feature channels.\n";
550
551 static struct ast_cli_entry cli_show_features = {
552         { "feature", "show", "channels", NULL }, features_show, 
553         "Show status of feature channels", show_features_usage, NULL };
554
555 int load_module()
556 {
557         /* Make sure we can register our sip channel type */
558         if (ast_channel_register(&features_tech)) {
559                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
560                 return -1;
561         }
562         ast_cli_register(&cli_show_features);
563         return 0;
564 }
565
566 int reload()
567 {
568         return 0;
569 }
570
571 int unload_module()
572 {
573         struct feature_pvt *p;
574         /* First, take us out of the channel loop */
575         ast_cli_unregister(&cli_show_features);
576         ast_channel_unregister(&features_tech);
577         if (!ast_mutex_lock(&featurelock)) {
578                 /* Hangup all interfaces if they have an owner */
579                 p = features;
580                 while(p) {
581                         if (p->owner)
582                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
583                         p = p->next;
584                 }
585                 features = NULL;
586                 ast_mutex_unlock(&featurelock);
587         } else {
588                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
589                 return -1;
590         }               
591         return 0;
592 }
593
594 int usecount()
595 {
596         return usecnt;
597 }
598
599 char *key()
600 {
601         return ASTERISK_GPL_KEY;
602 }
603
604 char *description()
605 {
606         return (char *) desc;
607 }
608