formatting and callingpres support (bug #4787)
[asterisk/asterisk.git] / channels / chan_features.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * feature Proxy Channel
5  * 
6  * *** Experimental code ****
7  * 
8  * Copyright (C) 1999, Mark Spencer
9  *
10  * Mark Spencer <markster@linux-support.net>
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/socket.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <sys/signal.h>
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/lock.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/config.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/module.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/options.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/sched.h"
41 #include "asterisk/io.h"
42 #include "asterisk/rtp.h"
43 #include "asterisk/acl.h"
44 #include "asterisk/callerid.h"
45 #include "asterisk/file.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/app.h"
48 #include "asterisk/musiconhold.h"
49 #include "asterisk/manager.h"
50
51 static const char desc[] = "Feature Proxy Channel";
52 static const char type[] = "Feature";
53 static const char tdesc[] = "Feature Proxy Channel Driver";
54
55 static int usecnt =0;
56 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
57
58 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
59
60 /* Protect the interface list (of feature_pvt's) */
61 AST_MUTEX_DEFINE_STATIC(featurelock);
62
63 struct feature_sub {
64         struct ast_channel *owner;
65         int inthreeway;
66         int pfd;
67         int timingfdbackup;
68         int alertpipebackup[2];
69 };
70
71 static struct feature_pvt {
72         ast_mutex_t lock;                       /* Channel private lock */
73         char tech[AST_MAX_EXTENSION];           /* Technology to abstract */
74         char dest[AST_MAX_EXTENSION];           /* Destination to abstract */
75         struct ast_channel *subchan;
76         struct feature_sub subs[3];             /* Subs */
77         struct ast_channel *owner;              /* Current Master Channel */
78         struct feature_pvt *next;               /* Next entity */
79 } *features = NULL;
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(struct ast_channel *ast, char digit);
87 static int features_call(struct ast_channel *ast, char *dest, int timeout);
88 static int features_hangup(struct ast_channel *ast);
89 static int features_answer(struct ast_channel *ast);
90 static struct ast_frame *features_read(struct ast_channel *ast);
91 static int features_write(struct ast_channel *ast, struct ast_frame *f);
92 static int features_indicate(struct ast_channel *ast, int condition);
93 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
94
95 static const struct ast_channel_tech features_tech = {
96         .type = type,
97         .description = tdesc,
98         .capabilities = -1,
99         .requester = features_request,
100         .send_digit = features_digit,
101         .call = features_call,
102         .hangup = features_hangup,
103         .answer = features_answer,
104         .read = features_read,
105         .write = features_write,
106         .exception = features_read,
107         .indicate = features_indicate,
108         .fixup = features_fixup,
109 };
110
111 static inline void init_sub(struct feature_sub *sub)
112 {
113         sub->inthreeway = 0;
114         sub->pfd = -1;
115         sub->timingfdbackup = -1;
116         sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;
117 }
118
119 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
120 {
121         int x;
122         if (!owner) {
123                 ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
124                 return -1;
125         }
126         for (x=0; x<3; x++) {
127                 if (owner == p->subs[x].owner)
128                         return x;
129         }
130         return -1;
131 }
132
133 #if 0
134 static void wakeup_sub(struct feature_pvt *p, int a)
135 {
136         struct ast_frame null = { AST_FRAME_NULL, };
137         for (;;) {
138                 if (p->subs[a].owner) {
139                         if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
140                                 ast_mutex_unlock(&p->lock);
141                                 usleep(1);
142                                 ast_mutex_lock(&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 index)
155 {
156         /* Restore timing/alertpipe */
157         p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
158         p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
159         p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
160         p->subs[index].owner->fds[AST_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
161         p->subs[index].owner->fds[AST_MAX_FDS-2] = p->subs[index].timingfdbackup;
162 }
163
164 static void update_features(struct feature_pvt *p, int index)
165 {
166         int x;
167         if (p->subs[index].owner) {
168                 for (x=0; x<AST_MAX_FDS; x++) {
169                         if (index) 
170                                 p->subs[index].owner->fds[x] = -1;
171                         else
172                                 p->subs[index].owner->fds[x] = p->subchan->fds[x];
173                 }
174                 if (!index) {
175                         /* Copy timings from master channel */
176                         p->subs[index].owner->timingfd = p->subchan->timingfd;
177                         p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0];
178                         p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1];
179                         if (p->subs[index].owner->nativeformats != p->subchan->readformat) {
180                                 p->subs[index].owner->nativeformats = p->subchan->readformat;
181                                 if (p->subs[index].owner->readformat)
182                                         ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat);
183                                 if (p->subs[index].owner->writeformat)
184                                         ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat);
185                         }
186                 } else{
187                         restore_channel(p, index);
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_log(LOG_DEBUG, "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         static struct ast_frame null_frame = { AST_FRAME_NULL, };
232         struct feature_pvt *p = ast->tech_pvt;
233         struct ast_frame *f;
234         int x;
235         
236         f = &null_frame;
237         ast_mutex_lock(&p->lock);
238         x = indexof(p, ast, 0);
239         if (!x && p->subchan) {
240                 update_features(p, x);
241                 f = ast_read(p->subchan);
242         }
243         ast_mutex_unlock(&p->lock);
244         return f;
245 }
246
247 static int features_write(struct ast_channel *ast, struct ast_frame *f)
248 {
249         struct feature_pvt *p = ast->tech_pvt;
250         int res = -1;
251         int x;
252
253         ast_mutex_lock(&p->lock);
254         x = indexof(p, ast, 0);
255         if (!x && p->subchan)
256                 res = ast_write(p->subchan, f);
257         ast_mutex_unlock(&p->lock);
258         return res;
259 }
260
261 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
262 {
263         struct feature_pvt *p = newchan->tech_pvt;
264         int x;
265
266         ast_mutex_lock(&p->lock);
267         if (p->owner == oldchan)
268                 p->owner = newchan;
269         for (x = 0; x < 3; x++) {
270                 if (p->subs[x].owner == oldchan)
271                         p->subs[x].owner = newchan;
272         }
273         ast_mutex_unlock(&p->lock);
274         return 0;
275 }
276
277 static int features_indicate(struct ast_channel *ast, int condition)
278 {
279         struct feature_pvt *p = ast->tech_pvt;
280         int res = -1;
281         int x;
282
283         /* Queue up a frame representing the indication as a control frame */
284         ast_mutex_lock(&p->lock);
285         x = indexof(p, ast, 0);
286         if (!x && p->subchan)
287                 res = ast_indicate(p->subchan, condition);
288         ast_mutex_unlock(&p->lock);
289         return res;
290 }
291
292 static int features_digit(struct ast_channel *ast, char digit)
293 {
294         struct feature_pvt *p = ast->tech_pvt;
295         int res = -1;
296         int x;
297
298         /* Queue up a frame representing the indication as a control frame */
299         ast_mutex_lock(&p->lock);
300         x = indexof(p, ast, 0);
301         if (!x && p->subchan)
302                 res = ast_senddigit(p->subchan, digit);
303         ast_mutex_unlock(&p->lock);
304         return res;
305 }
306
307 static int features_call(struct ast_channel *ast, char *dest, int timeout)
308 {
309         struct feature_pvt *p = ast->tech_pvt;
310         int res = -1;
311         int x;
312         char *dest2;
313                 
314         dest2 = strchr(dest, '/');
315         if (dest2) {
316                 ast_mutex_lock(&p->lock);
317                 x = indexof(p, ast, 0);
318                 if (!x && p->subchan) {
319                         if (p->owner->cid.cid_num)
320                                 p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
321                         else 
322                                 p->subchan->cid.cid_num = NULL;
323                 
324                         if (p->owner->cid.cid_name)
325                                 p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
326                         else 
327                                 p->subchan->cid.cid_name = NULL;
328                 
329                         if (p->owner->cid.cid_rdnis)
330                                 p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
331                         else
332                                 p->subchan->cid.cid_rdnis = NULL;
333                 
334                         if (p->owner->cid.cid_ani)
335                                 p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
336                         else
337                                 p->subchan->cid.cid_ani = NULL;
338                 
339                         p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
340                         strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
341                         strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
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         struct feature_pvt *cur, *prev=NULL;
356         int x;
357
358         ast_mutex_lock(&p->lock);
359         x = indexof(p, ast, 0);
360         if (x > -1) {
361                 restore_channel(p, x);
362                 p->subs[x].owner = NULL;
363                 /* XXX Re-arrange, unconference, etc XXX */
364         }
365         ast->tech_pvt = NULL;
366         
367         
368         if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
369                 ast_mutex_unlock(&p->lock);
370                 /* Remove from list */
371                 ast_mutex_lock(&featurelock);
372                 cur = features;
373                 while(cur) {
374                         if (cur == p) {
375                                 if (prev)
376                                         prev->next = cur->next;
377                                 else
378                                         features = cur->next;
379                                 break;
380                         }
381                         prev = cur;
382                         cur = cur->next;
383                 }
384                 ast_mutex_unlock(&featurelock);
385                 ast_mutex_lock(&p->lock);
386                 /* And destroy */
387                 if (p->subchan)
388                         ast_hangup(p->subchan);
389                 ast_mutex_unlock(&p->lock);
390                 ast_mutex_destroy(&p->lock);
391                 free(p);
392                 return 0;
393         }
394         ast_mutex_unlock(&p->lock);
395         return 0;
396 }
397
398 static struct feature_pvt *features_alloc(char *data, int format)
399 {
400         struct feature_pvt *tmp;
401         char *dest=NULL;
402         char *tech;
403         int x;
404         int status;
405         struct ast_channel *chan;
406         
407         tech = ast_strdupa(data);
408         if (tech) {
409                 dest = strchr(tech, '/');
410                 if (dest) {
411                         *dest = '\0';
412                         dest++;
413                 }
414         }
415         if (!tech || !dest) {
416                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
417                         data);
418                 return NULL;
419         }
420         ast_mutex_lock(&featurelock);
421         tmp = features;
422         while(tmp) {
423                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
424                         break;
425                 tmp = tmp->next;
426         }
427         ast_mutex_unlock(&featurelock);
428         if (!tmp) {
429                 chan = ast_request(tech, format, dest, &status);
430                 if (!chan) {
431                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
432                         return NULL;
433                 }
434                 tmp = malloc(sizeof(struct feature_pvt));
435                 if (tmp) {
436                         memset(tmp, 0, sizeof(struct feature_pvt));
437                         for (x=0;x<3;x++)
438                                 init_sub(tmp->subs + x);
439                         ast_mutex_init(&tmp->lock);
440                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
441                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
442                         tmp->subchan = chan;
443                         ast_mutex_lock(&featurelock);
444                         tmp->next = features;
445                         features = tmp;
446                         ast_mutex_unlock(&featurelock);
447                 }
448         }
449         return tmp;
450 }
451
452 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
453 {
454         struct ast_channel *tmp;
455         int x,y;
456         if (!p->subchan) {
457                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
458                 return NULL;
459         }
460         if (p->subs[index].owner) {
461                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
462                 return NULL;
463         }
464         tmp = ast_channel_alloc(0);
465         if (!tmp) {
466                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
467                 return NULL;
468         }
469         tmp->tech = &features_tech;
470         for (x=1;x<4;x++) {
471                 snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
472                 for (y=0;y<3;y++) {
473                         if (y == index)
474                                 continue;
475                         if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
476                                 break;
477                 }
478                 if (y >= 3)
479                         break;
480         }
481         tmp->type = type;
482         ast_setstate(tmp, state);
483         tmp->writeformat = p->subchan->writeformat;
484         tmp->rawwriteformat = p->subchan->rawwriteformat;
485         tmp->readformat = p->subchan->readformat;
486         tmp->rawreadformat = p->subchan->rawreadformat;
487         tmp->nativeformats = p->subchan->readformat;
488         tmp->tech_pvt = p;
489         p->subs[index].owner = tmp;
490         if (!p->owner)
491                 p->owner = tmp;
492         ast_mutex_lock(&usecnt_lock);
493         usecnt++;
494         ast_mutex_unlock(&usecnt_lock);
495         ast_update_use_count();
496         return tmp;
497 }
498
499
500 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
501 {
502         struct feature_pvt *p;
503         struct ast_channel *chan = NULL;
504
505         p = features_alloc(data, format);
506         if (p && !p->subs[SUB_REAL].owner)
507                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
508         if (chan)
509                 update_features(p,SUB_REAL);
510         return chan;
511 }
512
513 static int features_show(int fd, int argc, char **argv)
514 {
515         struct feature_pvt *p;
516
517         if (argc != 3)
518                 return RESULT_SHOWUSAGE;
519         ast_mutex_lock(&featurelock);
520         p = features;
521         while(p) {
522                 ast_mutex_lock(&p->lock);
523                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
524                 ast_mutex_unlock(&p->lock);
525                 p = p->next;
526         }
527         if (!features)
528                 ast_cli(fd, "No feature channels in use\n");
529         ast_mutex_unlock(&featurelock);
530         return RESULT_SUCCESS;
531 }
532
533 static char show_features_usage[] = 
534 "Usage: feature show channels\n"
535 "       Provides summary information on feature channels.\n";
536
537 static struct ast_cli_entry cli_show_features = {
538         { "feature", "show", "channels", NULL }, features_show, 
539         "Show status of feature channels", show_features_usage, NULL };
540
541 int load_module()
542 {
543         /* Make sure we can register our sip channel type */
544         if (ast_channel_register(&features_tech)) {
545                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
546                 return -1;
547         }
548         ast_cli_register(&cli_show_features);
549         return 0;
550 }
551
552 int reload()
553 {
554         return 0;
555 }
556
557 int unload_module()
558 {
559         struct feature_pvt *p;
560         /* First, take us out of the channel loop */
561         ast_cli_unregister(&cli_show_features);
562         ast_channel_unregister(&features_tech);
563         if (!ast_mutex_lock(&featurelock)) {
564                 /* Hangup all interfaces if they have an owner */
565                 p = features;
566                 while(p) {
567                         if (p->owner)
568                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
569                         p = p->next;
570                 }
571                 features = NULL;
572                 ast_mutex_unlock(&featurelock);
573         } else {
574                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
575                 return -1;
576         }               
577         return 0;
578 }
579
580 int usecount()
581 {
582         return usecnt;
583 }
584
585 char *key()
586 {
587         return ASTERISK_GPL_KEY;
588 }
589
590 char *description()
591 {
592         return (char *) desc;
593 }
594