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