Minor chan_features tweak
[asterisk/asterisk.git] / channels / chan_features.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * feature Proxy Channel
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <asterisk/lock.h>
25 #include <asterisk/sched.h>
26 #include <asterisk/io.h>
27 #include <asterisk/rtp.h>
28 #include <asterisk/acl.h>
29 #include <asterisk/callerid.h>
30 #include <asterisk/file.h>
31 #include <asterisk/cli.h>
32 #include <asterisk/app.h>
33 #include <asterisk/musiconhold.h>
34 #include <asterisk/manager.h>
35 #include <sys/socket.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <netdb.h>
41 #include <arpa/inet.h>
42 #include <sys/signal.h>
43
44
45 static char *desc = "Feature Proxy Channel";
46 static char *type = "Feature";
47 static char *tdesc = "Feature Proxy Channel Driver";
48
49 static int capability = -1;
50
51 static int usecnt =0;
52 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
53
54 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
55
56 /* Protect the interface list (of feature_pvt's) */
57 AST_MUTEX_DEFINE_STATIC(featurelock);
58
59 struct feature_sub {
60         struct ast_channel *owner;
61         int inthreeway;
62         int pfd;
63 };
64
65 static struct feature_pvt {
66         ast_mutex_t lock;                               /* Channel private lock */
67         char tech[AST_MAX_EXTENSION];   /* Technology to abstract */
68         char dest[AST_MAX_EXTENSION];   /* Destination to abstract */
69         struct ast_channel *subchan;
70         struct feature_sub subs[3];             /* Subs */
71         struct ast_channel *owner;                      /* Current Master Channel */
72         struct feature_pvt *next;                               /* Next entity */
73 } *features = NULL;
74
75 #define SUB_REAL                0                       /* Active call */
76 #define SUB_CALLWAIT    1                       /* Call-Waiting call on hold */
77 #define SUB_THREEWAY    2                       /* Three-way call */
78
79 static inline void init_sub(struct feature_sub *sub)
80 {
81         sub->inthreeway = 0;
82         sub->pfd = -1;
83 }
84
85 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
86 {
87         int x;
88         if (!owner) {
89                 ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
90                 return -1;
91         }
92         for (x=0;x<3;x++) {
93                 if (owner == p->subs[x].owner)
94                         return x;
95         }
96         return -1;
97 }
98
99 static void wakeup_sub(struct feature_pvt *p, int a)
100 {
101         struct ast_frame null = { AST_FRAME_NULL, };
102         for (;;) {
103                 if (p->subs[a].owner) {
104                         if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
105                                 ast_mutex_unlock(&p->lock);
106                                 usleep(1);
107                                 ast_mutex_lock(&p->lock);
108                         } else {
109                                 ast_queue_frame(p->subs[a].owner, &null);
110                                 ast_mutex_unlock(&p->subs[a].owner->lock);
111                                 break;
112                         }
113                 } else
114                         break;
115         }
116 }
117
118 static void swap_subs(struct feature_pvt *p, int a, int b)
119 {
120         int x;
121         int tinthreeway;
122         struct ast_channel *towner;
123
124         ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
125
126         towner = p->subs[a].owner;
127         tinthreeway = p->subs[a].inthreeway;
128
129         p->subs[a].owner = p->subs[b].owner;
130         p->subs[a].inthreeway = p->subs[b].inthreeway;
131
132         p->subs[b].owner = towner;
133         p->subs[b].inthreeway = tinthreeway;
134
135         if (p->subs[a].owner) {
136                 for (x=0;x<AST_MAX_FDS;x++) {
137                         if (a) 
138                                 p->subs[a].owner->fds[x] = -1;
139                         else
140                                 p->subs[a].owner->fds[x] = p->subchan->fds[x];
141                 }
142         }
143         if (p->subs[b].owner) {
144                 for (x=0;x<AST_MAX_FDS;x++)
145                         if (b)
146                                 p->subs[b].owner->fds[x] = -1;
147                         else
148                                 p->subs[b].owner->fds[x] = p->subchan->fds[x];
149         }
150         wakeup_sub(p, a);
151         wakeup_sub(p, b);
152 }
153
154 static int features_answer(struct ast_channel *ast)
155 {
156         struct feature_pvt *p = ast->pvt->pvt;
157         int res = -1;
158         int x;
159         ast_mutex_lock(&p->lock);
160         x = indexof(p, ast, 0);
161         if (!x && p->subchan)
162                 res = ast_answer(p->subchan);
163         ast_mutex_unlock(&p->lock);
164         return res;
165 }
166
167 static struct ast_frame  *features_read(struct ast_channel *ast)
168 {
169         static struct ast_frame null_frame = { AST_FRAME_NULL, };
170         struct feature_pvt *p = ast->pvt->pvt;
171         struct ast_frame *f;
172         int x;
173         
174         f = &null_frame;
175         ast_mutex_lock(&p->lock);
176         x = indexof(p, ast, 0);
177         if (!x && p->subchan)
178                 f = ast_read(p->subchan);
179         ast_mutex_unlock(&p->lock);
180         return f;
181 }
182
183 static int features_write(struct ast_channel *ast, struct ast_frame *f)
184 {
185         struct feature_pvt *p = ast->pvt->pvt;
186         int res = -1;
187         int x;
188         ast_mutex_lock(&p->lock);
189         x = indexof(p, ast, 0);
190         if (!x && p->subchan)
191                 res = ast_write(p->subchan, f);
192         ast_mutex_unlock(&p->lock);
193         return res;
194 }
195
196 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
197 {
198         struct feature_pvt *p = newchan->pvt->pvt;
199         int x;
200         ast_mutex_lock(&p->lock);
201         if (p->owner == oldchan)
202                 p->owner = newchan;
203         for (x=0;x<3;x++) {
204                 if (p->subs[x].owner == oldchan)
205                         p->subs[x].owner = newchan;
206         }
207         ast_mutex_unlock(&p->lock);
208         return 0;
209 }
210
211 static int features_indicate(struct ast_channel *ast, int condition)
212 {
213         struct feature_pvt *p = ast->pvt->pvt;
214         int res = -1;
215         int x;
216         /* Queue up a frame representing the indication as a control frame */
217         ast_mutex_lock(&p->lock);
218         x = indexof(p, ast, 0);
219         if (!x && p->subchan)
220                 res = ast_indicate(p->subchan, condition);
221         ast_mutex_unlock(&p->lock);
222         return res;
223 }
224
225 static int features_digit(struct ast_channel *ast, char digit)
226 {
227         struct feature_pvt *p = ast->pvt->pvt;
228         int res = -1;
229         int x;
230         /* Queue up a frame representing the indication as a control frame */
231         ast_mutex_lock(&p->lock);
232         x = indexof(p, ast, 0);
233         if (!x && p->subchan)
234                 res = ast_senddigit(p->subchan, digit);
235         ast_mutex_unlock(&p->lock);
236         return res;
237 }
238
239 static int features_call(struct ast_channel *ast, char *dest, int timeout)
240 {
241         struct feature_pvt *p = ast->pvt->pvt;
242         int res = -1;
243         int x;
244                 
245         ast_mutex_lock(&p->lock);
246         x = indexof(p, ast, 0);
247         if (!x && p->subchan) {
248                 if (p->owner->cid.cid_num)
249                         p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
250                 else 
251                         p->subchan->cid.cid_num = NULL;
252         
253                 if (p->owner->cid.cid_name)
254                         p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
255                 else 
256                         p->subchan->cid.cid_name = NULL;
257         
258                 if (p->owner->cid.cid_rdnis)
259                         p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
260                 else
261                         p->subchan->cid.cid_rdnis = NULL;
262         
263                 if (p->owner->cid.cid_ani)
264                         p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
265                 else
266                         p->subchan->cid.cid_ani = NULL;
267         
268                 strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
269                 strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
270                 p->subchan->cdrflags = p->owner->cdrflags;
271         } else
272                 ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
273         ast_mutex_unlock(&p->lock);
274         return res;
275 }
276
277 static int features_hangup(struct ast_channel *ast)
278 {
279         struct feature_pvt *p = ast->pvt->pvt;
280         struct feature_pvt *cur, *prev=NULL;
281         int x;
282
283         ast_mutex_lock(&p->lock);
284         x = indexof(p, ast, 0);
285         if (x > -1) {
286                 p->subs[x].owner = NULL;
287                 /* XXX Re-arrange, unconference, etc XXX */
288         }
289         ast->pvt->pvt = NULL;
290         
291         
292         if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
293                 ast_mutex_unlock(&p->lock);
294                 /* Remove from list */
295                 ast_mutex_lock(&featurelock);
296                 cur = features;
297                 while(cur) {
298                         if (cur == p) {
299                                 if (prev)
300                                         prev->next = cur->next;
301                                 else
302                                         features = cur->next;
303                                 break;
304                         }
305                         prev = cur;
306                         cur = cur->next;
307                 }
308                 ast_mutex_unlock(&featurelock);
309                 ast_mutex_lock(&p->lock);
310                 /* And destroy */
311                 if (p->subchan)
312                         ast_hangup(p->subchan);
313                 ast_mutex_unlock(&p->lock);
314                 ast_mutex_destroy(&p->lock);
315                 free(p);
316                 return 0;
317         }
318         ast_mutex_unlock(&p->lock);
319         return 0;
320 }
321
322 static struct feature_pvt *features_alloc(char *data, int format)
323 {
324         struct feature_pvt *tmp;
325         char *dest=NULL;
326         char *tech;
327         int x;
328         int status;
329         struct ast_channel *chan;
330         
331         tech = ast_strdupa(data);
332         if (tech) {
333                 dest = strchr(tech, '/');
334                 if (dest) {
335                         *dest = '\0';
336                         dest++;
337                 }
338         }
339         if (!tech || !dest) {
340                 ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
341                         data);
342                 return NULL;
343         }
344         ast_mutex_lock(&featurelock);
345         tmp = features;
346         while(tmp) {
347                 if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
348                         break;
349                 tmp = tmp->next;
350         }
351         ast_mutex_unlock(&featurelock);
352         if (!tmp) {
353                 chan = ast_request(tech, format, dest, &status);
354                 if (!chan) {
355                         ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
356                         return NULL;
357                 }
358                 tmp = malloc(sizeof(struct feature_pvt));
359                 if (tmp) {
360                         memset(tmp, 0, sizeof(struct feature_pvt));
361                         for (x=0;x<3;x++)
362                                 init_sub(tmp->subs + x);
363                         ast_mutex_init(&tmp->lock);
364                         strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
365                         strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
366                         tmp->subchan = chan;
367                         ast_mutex_lock(&featurelock);
368                         tmp->next = features;
369                         features = tmp;
370                         ast_mutex_unlock(&featurelock);
371                 }
372         }
373         return tmp;
374 }
375
376 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
377 {
378         struct ast_channel *tmp;
379         int x,y;
380         if (!p->subchan) {
381                 ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
382                 return NULL;
383         }
384         if (!p->subs[index].owner) {
385                 ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
386                 return NULL;
387         }
388         tmp = ast_channel_alloc(1);
389         if (!tmp)
390                 return NULL;
391         if (tmp) {
392                 for (x=1;x<4;x++) {
393                         snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
394                         for (y=0;y<3;y++) {
395                                 if (p->subs[x].owner && !strcasecmp(p->subs[x].owner->name, tmp->name))
396                                         break;
397                         }
398                         if (y < 3)
399                                 break;
400                 }
401                 tmp->type = type;
402                 ast_setstate(tmp, state);
403                 tmp->writeformat = p->subchan->writeformat;;
404                 tmp->pvt->rawwriteformat = p->subchan->pvt->rawwriteformat;
405                 tmp->readformat = p->subchan->readformat;
406                 tmp->pvt->rawreadformat = p->subchan->pvt->rawreadformat;
407                 tmp->pvt->pvt = p;
408                 tmp->pvt->send_digit = features_digit;
409                 tmp->pvt->call = features_call;
410                 tmp->pvt->hangup = features_hangup;
411                 tmp->pvt->answer = features_answer;
412                 tmp->pvt->read = features_read;
413                 tmp->pvt->write = features_write;
414                 tmp->pvt->exception = features_read;
415                 tmp->pvt->indicate = features_indicate;
416                 tmp->pvt->fixup = features_fixup;
417                 p->subs[index].owner = tmp;
418                 ast_mutex_lock(&usecnt_lock);
419                 usecnt++;
420                 ast_mutex_unlock(&usecnt_lock);
421                 ast_update_use_count();
422         } else
423                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
424         return tmp;
425 }
426
427
428 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
429 {
430         struct feature_pvt *p;
431         struct ast_channel *chan = NULL;
432         p = features_alloc(data, format);
433         if (p && !p->subs[SUB_REAL].owner)
434                 chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
435         return chan;
436 }
437
438 static int features_show(int fd, int argc, char **argv)
439 {
440         struct feature_pvt *p;
441
442         if (argc != 3)
443                 return RESULT_SHOWUSAGE;
444         ast_mutex_lock(&featurelock);
445         p = features;
446         while(p) {
447                 ast_mutex_lock(&p->lock);
448                 ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
449                 ast_mutex_unlock(&p->lock);
450                 p = p->next;
451         }
452         if (!features)
453                 ast_cli(fd, "No feature channels in use\n");
454         ast_mutex_unlock(&featurelock);
455         return RESULT_SUCCESS;
456 }
457
458 static char show_features_usage[] = 
459 "Usage: feature show channels\n"
460 "       Provides summary information on feature channels.\n";
461
462 static struct ast_cli_entry cli_show_features = {
463         { "feature", "show", "channels", NULL }, features_show, 
464         "Show status of feature channels", show_features_usage, NULL };
465
466 int load_module()
467 {
468         /* Make sure we can register our sip channel type */
469         if (ast_channel_register(type, tdesc, capability, features_request)) {
470                 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
471                 return -1;
472         }
473         ast_cli_register(&cli_show_features);
474         return 0;
475 }
476
477 int reload()
478 {
479         return 0;
480 }
481
482 int unload_module()
483 {
484         struct feature_pvt *p;
485         /* First, take us out of the channel loop */
486         ast_cli_unregister(&cli_show_features);
487         ast_channel_unregister(type);
488         if (!ast_mutex_lock(&featurelock)) {
489                 /* Hangup all interfaces if they have an owner */
490                 p = features;
491                 while(p) {
492                         if (p->owner)
493                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
494                         p = p->next;
495                 }
496                 features = NULL;
497                 ast_mutex_unlock(&featurelock);
498         } else {
499                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
500                 return -1;
501         }               
502         return 0;
503 }
504
505 int usecount()
506 {
507         int res;
508         ast_mutex_lock(&usecnt_lock);
509         res = usecnt;
510         ast_mutex_unlock(&usecnt_lock);
511         return res;
512 }
513
514 char *key()
515 {
516         return ASTERISK_GPL_KEY;
517 }
518
519 char *description()
520 {
521         return desc;
522 }
523