Fri May 26 01:45:29 2006

Asterisk developer's documentation


chan_features.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief feature Proxy Channel
00022  * 
00023  * \note *** Experimental code ****
00024  * 
00025  * \ingroup channel_drivers
00026  */
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 #include <sys/socket.h>
00032 #include <errno.h>
00033 #include <stdlib.h>
00034 #include <fcntl.h>
00035 #include <netdb.h>
00036 #include <netinet/in.h>
00037 #include <arpa/inet.h>
00038 #include <sys/signal.h>
00039 
00040 #include "asterisk.h"
00041 
00042 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 8666 $")
00043 
00044 #include "asterisk/lock.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/config.h"
00047 #include "asterisk/logger.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/pbx.h"
00050 #include "asterisk/options.h"
00051 #include "asterisk/lock.h"
00052 #include "asterisk/sched.h"
00053 #include "asterisk/io.h"
00054 #include "asterisk/rtp.h"
00055 #include "asterisk/acl.h"
00056 #include "asterisk/callerid.h"
00057 #include "asterisk/file.h"
00058 #include "asterisk/cli.h"
00059 #include "asterisk/app.h"
00060 #include "asterisk/musiconhold.h"
00061 #include "asterisk/manager.h"
00062 
00063 static const char desc[] = "Feature Proxy Channel";
00064 static const char type[] = "Feature";
00065 static const char tdesc[] = "Feature Proxy Channel Driver";
00066 
00067 static int usecnt =0;
00068 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00069 
00070 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
00071 
00072 /* Protect the interface list (of feature_pvt's) */
00073 AST_MUTEX_DEFINE_STATIC(featurelock);
00074 
00075 struct feature_sub {
00076    struct ast_channel *owner;
00077    int inthreeway;
00078    int pfd;
00079    int timingfdbackup;
00080    int alertpipebackup[2];
00081 };
00082 
00083 static struct feature_pvt {
00084    ast_mutex_t lock;       /* Channel private lock */
00085    char tech[AST_MAX_EXTENSION];    /* Technology to abstract */
00086    char dest[AST_MAX_EXTENSION];    /* Destination to abstract */
00087    struct ast_channel *subchan;
00088    struct feature_sub subs[3];      /* Subs */
00089    struct ast_channel *owner;    /* Current Master Channel */
00090    struct feature_pvt *next;     /* Next entity */
00091 } *features = NULL;
00092 
00093 #define SUB_REAL  0        /* Active call */
00094 #define SUB_CALLWAIT 1        /* Call-Waiting call on hold */
00095 #define SUB_THREEWAY 2        /* Three-way call */
00096 
00097 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause);
00098 static int features_digit(struct ast_channel *ast, char digit);
00099 static int features_call(struct ast_channel *ast, char *dest, int timeout);
00100 static int features_hangup(struct ast_channel *ast);
00101 static int features_answer(struct ast_channel *ast);
00102 static struct ast_frame *features_read(struct ast_channel *ast);
00103 static int features_write(struct ast_channel *ast, struct ast_frame *f);
00104 static int features_indicate(struct ast_channel *ast, int condition);
00105 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00106 
00107 static const struct ast_channel_tech features_tech = {
00108    .type = type,
00109    .description = tdesc,
00110    .capabilities = -1,
00111    .requester = features_request,
00112    .send_digit = features_digit,
00113    .call = features_call,
00114    .hangup = features_hangup,
00115    .answer = features_answer,
00116    .read = features_read,
00117    .write = features_write,
00118    .exception = features_read,
00119    .indicate = features_indicate,
00120    .fixup = features_fixup,
00121 };
00122 
00123 static inline void init_sub(struct feature_sub *sub)
00124 {
00125    sub->inthreeway = 0;
00126    sub->pfd = -1;
00127    sub->timingfdbackup = -1;
00128    sub->alertpipebackup[0] = sub->alertpipebackup[1] = -1;
00129 }
00130 
00131 static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int nullok)
00132 {
00133    int x;
00134    if (!owner) {
00135       ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
00136       return -1;
00137    }
00138    for (x=0; x<3; x++) {
00139       if (owner == p->subs[x].owner)
00140          return x;
00141    }
00142    return -1;
00143 }
00144 
00145 #if 0
00146 static void wakeup_sub(struct feature_pvt *p, int a)
00147 {
00148    struct ast_frame null = { AST_FRAME_NULL, };
00149    for (;;) {
00150       if (p->subs[a].owner) {
00151          if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00152             ast_mutex_unlock(&p->lock);
00153             usleep(1);
00154             ast_mutex_lock(&p->lock);
00155          } else {
00156             ast_queue_frame(p->subs[a].owner, &null);
00157             ast_mutex_unlock(&p->subs[a].owner->lock);
00158             break;
00159          }
00160       } else
00161          break;
00162    }
00163 }
00164 #endif
00165 
00166 static void restore_channel(struct feature_pvt *p, int index)
00167 {
00168    /* Restore timing/alertpipe */
00169    p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
00170    p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
00171    p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
00172    p->subs[index].owner->fds[AST_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
00173    p->subs[index].owner->fds[AST_MAX_FDS-2] = p->subs[index].timingfdbackup;
00174 }
00175 
00176 static void update_features(struct feature_pvt *p, int index)
00177 {
00178    int x;
00179    if (p->subs[index].owner) {
00180       for (x=0; x<AST_MAX_FDS; x++) {
00181          if (index) 
00182             p->subs[index].owner->fds[x] = -1;
00183          else
00184             p->subs[index].owner->fds[x] = p->subchan->fds[x];
00185       }
00186       if (!index) {
00187          /* Copy timings from master channel */
00188          p->subs[index].owner->timingfd = p->subchan->timingfd;
00189          p->subs[index].owner->alertpipe[0] = p->subchan->alertpipe[0];
00190          p->subs[index].owner->alertpipe[1] = p->subchan->alertpipe[1];
00191          if (p->subs[index].owner->nativeformats != p->subchan->readformat) {
00192             p->subs[index].owner->nativeformats = p->subchan->readformat;
00193             if (p->subs[index].owner->readformat)
00194                ast_set_read_format(p->subs[index].owner, p->subs[index].owner->readformat);
00195             if (p->subs[index].owner->writeformat)
00196                ast_set_write_format(p->subs[index].owner, p->subs[index].owner->writeformat);
00197          }
00198       } else{
00199          restore_channel(p, index);
00200       }
00201    }
00202 }
00203 
00204 #if 0
00205 static void swap_subs(struct feature_pvt *p, int a, int b)
00206 {
00207    int tinthreeway;
00208    struct ast_channel *towner;
00209 
00210    ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00211 
00212    towner = p->subs[a].owner;
00213    tinthreeway = p->subs[a].inthreeway;
00214 
00215    p->subs[a].owner = p->subs[b].owner;
00216    p->subs[a].inthreeway = p->subs[b].inthreeway;
00217 
00218    p->subs[b].owner = towner;
00219    p->subs[b].inthreeway = tinthreeway;
00220    update_features(p,a);
00221    update_features(p,b);
00222    wakeup_sub(p, a);
00223    wakeup_sub(p, b);
00224 }
00225 #endif
00226 
00227 static int features_answer(struct ast_channel *ast)
00228 {
00229    struct feature_pvt *p = ast->tech_pvt;
00230    int res = -1;
00231    int x;
00232 
00233    ast_mutex_lock(&p->lock);
00234    x = indexof(p, ast, 0);
00235    if (!x && p->subchan)
00236       res = ast_answer(p->subchan);
00237    ast_mutex_unlock(&p->lock);
00238    return res;
00239 }
00240 
00241 static struct ast_frame  *features_read(struct ast_channel *ast)
00242 {
00243    static struct ast_frame null_frame = { AST_FRAME_NULL, };
00244    struct feature_pvt *p = ast->tech_pvt;
00245    struct ast_frame *f;
00246    int x;
00247    
00248    f = &null_frame;
00249    ast_mutex_lock(&p->lock);
00250    x = indexof(p, ast, 0);
00251    if (!x && p->subchan) {
00252       update_features(p, x);
00253       f = ast_read(p->subchan);
00254    }
00255    ast_mutex_unlock(&p->lock);
00256    return f;
00257 }
00258 
00259 static int features_write(struct ast_channel *ast, struct ast_frame *f)
00260 {
00261    struct feature_pvt *p = ast->tech_pvt;
00262    int res = -1;
00263    int x;
00264 
00265    ast_mutex_lock(&p->lock);
00266    x = indexof(p, ast, 0);
00267    if (!x && p->subchan)
00268       res = ast_write(p->subchan, f);
00269    ast_mutex_unlock(&p->lock);
00270    return res;
00271 }
00272 
00273 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00274 {
00275    struct feature_pvt *p = newchan->tech_pvt;
00276    int x;
00277 
00278    ast_mutex_lock(&p->lock);
00279    if (p->owner == oldchan)
00280       p->owner = newchan;
00281    for (x = 0; x < 3; x++) {
00282       if (p->subs[x].owner == oldchan)
00283          p->subs[x].owner = newchan;
00284    }
00285    ast_mutex_unlock(&p->lock);
00286    return 0;
00287 }
00288 
00289 static int features_indicate(struct ast_channel *ast, int condition)
00290 {
00291    struct feature_pvt *p = ast->tech_pvt;
00292    int res = -1;
00293    int x;
00294 
00295    /* Queue up a frame representing the indication as a control frame */
00296    ast_mutex_lock(&p->lock);
00297    x = indexof(p, ast, 0);
00298    if (!x && p->subchan)
00299       res = ast_indicate(p->subchan, condition);
00300    ast_mutex_unlock(&p->lock);
00301    return res;
00302 }
00303 
00304 static int features_digit(struct ast_channel *ast, char digit)
00305 {
00306    struct feature_pvt *p = ast->tech_pvt;
00307    int res = -1;
00308    int x;
00309 
00310    /* Queue up a frame representing the indication as a control frame */
00311    ast_mutex_lock(&p->lock);
00312    x = indexof(p, ast, 0);
00313    if (!x && p->subchan)
00314       res = ast_senddigit(p->subchan, digit);
00315    ast_mutex_unlock(&p->lock);
00316    return res;
00317 }
00318 
00319 static int features_call(struct ast_channel *ast, char *dest, int timeout)
00320 {
00321    struct feature_pvt *p = ast->tech_pvt;
00322    int res = -1;
00323    int x;
00324    char *dest2;
00325       
00326    dest2 = strchr(dest, '/');
00327    if (dest2) {
00328       ast_mutex_lock(&p->lock);
00329       x = indexof(p, ast, 0);
00330       if (!x && p->subchan) {
00331          if (p->owner->cid.cid_num)
00332             p->subchan->cid.cid_num = strdup(p->owner->cid.cid_num);
00333          else 
00334             p->subchan->cid.cid_num = NULL;
00335       
00336          if (p->owner->cid.cid_name)
00337             p->subchan->cid.cid_name = strdup(p->owner->cid.cid_name);
00338          else 
00339             p->subchan->cid.cid_name = NULL;
00340       
00341          if (p->owner->cid.cid_rdnis)
00342             p->subchan->cid.cid_rdnis = strdup(p->owner->cid.cid_rdnis);
00343          else
00344             p->subchan->cid.cid_rdnis = NULL;
00345       
00346          if (p->owner->cid.cid_ani)
00347             p->subchan->cid.cid_ani = strdup(p->owner->cid.cid_ani);
00348          else
00349             p->subchan->cid.cid_ani = NULL;
00350       
00351          p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
00352          strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
00353          strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
00354          p->subchan->cdrflags = p->owner->cdrflags;
00355          res = ast_call(p->subchan, dest2, timeout);
00356          update_features(p, x);
00357       } else
00358          ast_log(LOG_NOTICE, "Uhm yah, not quite there with the call waiting...\n");
00359       ast_mutex_unlock(&p->lock);
00360    }
00361    return res;
00362 }
00363 
00364 static int features_hangup(struct ast_channel *ast)
00365 {
00366    struct feature_pvt *p = ast->tech_pvt;
00367    struct feature_pvt *cur, *prev=NULL;
00368    int x;
00369 
00370    ast_mutex_lock(&p->lock);
00371    x = indexof(p, ast, 0);
00372    if (x > -1) {
00373       restore_channel(p, x);
00374       p->subs[x].owner = NULL;
00375       /* XXX Re-arrange, unconference, etc XXX */
00376    }
00377    ast->tech_pvt = NULL;
00378    
00379    
00380    if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
00381       ast_mutex_unlock(&p->lock);
00382       /* Remove from list */
00383       ast_mutex_lock(&featurelock);
00384       cur = features;
00385       while(cur) {
00386          if (cur == p) {
00387             if (prev)
00388                prev->next = cur->next;
00389             else
00390                features = cur->next;
00391             break;
00392          }
00393          prev = cur;
00394          cur = cur->next;
00395       }
00396       ast_mutex_unlock(&featurelock);
00397       ast_mutex_lock(&p->lock);
00398       /* And destroy */
00399       if (p->subchan)
00400          ast_hangup(p->subchan);
00401       ast_mutex_unlock(&p->lock);
00402       ast_mutex_destroy(&p->lock);
00403       free(p);
00404       return 0;
00405    }
00406    ast_mutex_unlock(&p->lock);
00407    return 0;
00408 }
00409 
00410 static struct feature_pvt *features_alloc(char *data, int format)
00411 {
00412    struct feature_pvt *tmp;
00413    char *dest=NULL;
00414    char *tech;
00415    int x;
00416    int status;
00417    struct ast_channel *chan;
00418    
00419    tech = ast_strdupa(data);
00420    if (tech) {
00421       dest = strchr(tech, '/');
00422       if (dest) {
00423          *dest = '\0';
00424          dest++;
00425       }
00426    }
00427    if (!tech || !dest) {
00428       ast_log(LOG_NOTICE, "Format for feature channel is Feature/Tech/Dest ('%s' not valid)!\n", 
00429          data);
00430       return NULL;
00431    }
00432    ast_mutex_lock(&featurelock);
00433    tmp = features;
00434    while(tmp) {
00435       if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
00436          break;
00437       tmp = tmp->next;
00438    }
00439    ast_mutex_unlock(&featurelock);
00440    if (!tmp) {
00441       chan = ast_request(tech, format, dest, &status);
00442       if (!chan) {
00443          ast_log(LOG_NOTICE, "Unable to allocate subchannel '%s/%s'\n", tech, dest);
00444          return NULL;
00445       }
00446       tmp = malloc(sizeof(struct feature_pvt));
00447       if (tmp) {
00448          memset(tmp, 0, sizeof(struct feature_pvt));
00449          for (x=0;x<3;x++)
00450             init_sub(tmp->subs + x);
00451          ast_mutex_init(&tmp->lock);
00452          strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
00453          strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
00454          tmp->subchan = chan;
00455          ast_mutex_lock(&featurelock);
00456          tmp->next = features;
00457          features = tmp;
00458          ast_mutex_unlock(&featurelock);
00459       }
00460    }
00461    return tmp;
00462 }
00463 
00464 static struct ast_channel *features_new(struct feature_pvt *p, int state, int index)
00465 {
00466    struct ast_channel *tmp;
00467    int x,y;
00468    if (!p->subchan) {
00469       ast_log(LOG_WARNING, "Called upon channel with no subchan:(\n");
00470       return NULL;
00471    }
00472    if (p->subs[index].owner) {
00473       ast_log(LOG_WARNING, "Called to put index %d already there!\n", index);
00474       return NULL;
00475    }
00476    tmp = ast_channel_alloc(0);
00477    if (!tmp) {
00478       ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00479       return NULL;
00480    }
00481    tmp->tech = &features_tech;
00482    for (x=1;x<4;x++) {
00483       snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
00484       for (y=0;y<3;y++) {
00485          if (y == index)
00486             continue;
00487          if (p->subs[y].owner && !strcasecmp(p->subs[y].owner->name, tmp->name))
00488             break;
00489       }
00490       if (y >= 3)
00491          break;
00492    }
00493    tmp->type = type;
00494    ast_setstate(tmp, state);
00495    tmp->writeformat = p->subchan->writeformat;
00496    tmp->rawwriteformat = p->subchan->rawwriteformat;
00497    tmp->readformat = p->subchan->readformat;
00498    tmp->rawreadformat = p->subchan->rawreadformat;
00499    tmp->nativeformats = p->subchan->readformat;
00500    tmp->tech_pvt = p;
00501    p->subs[index].owner = tmp;
00502    if (!p->owner)
00503       p->owner = tmp;
00504    ast_mutex_lock(&usecnt_lock);
00505    usecnt++;
00506    ast_mutex_unlock(&usecnt_lock);
00507    ast_update_use_count();
00508    return tmp;
00509 }
00510 
00511 
00512 static struct ast_channel *features_request(const char *type, int format, void *data, int *cause)
00513 {
00514    struct feature_pvt *p;
00515    struct ast_channel *chan = NULL;
00516 
00517    p = features_alloc(data, format);
00518    if (p && !p->subs[SUB_REAL].owner)
00519       chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
00520    if (chan)
00521       update_features(p,SUB_REAL);
00522    return chan;
00523 }
00524 
00525 static int features_show(int fd, int argc, char **argv)
00526 {
00527    struct feature_pvt *p;
00528 
00529    if (argc != 3)
00530       return RESULT_SHOWUSAGE;
00531    ast_mutex_lock(&featurelock);
00532    p = features;
00533    while(p) {
00534       ast_mutex_lock(&p->lock);
00535       ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
00536       ast_mutex_unlock(&p->lock);
00537       p = p->next;
00538    }
00539    if (!features)
00540       ast_cli(fd, "No feature channels in use\n");
00541    ast_mutex_unlock(&featurelock);
00542    return RESULT_SUCCESS;
00543 }
00544 
00545 static char show_features_usage[] = 
00546 "Usage: feature show channels\n"
00547 "       Provides summary information on feature channels.\n";
00548 
00549 static struct ast_cli_entry cli_show_features = {
00550    { "feature", "show", "channels", NULL }, features_show, 
00551    "Show status of feature channels", show_features_usage, NULL };
00552 
00553 int load_module()
00554 {
00555    /* Make sure we can register our sip channel type */
00556    if (ast_channel_register(&features_tech)) {
00557       ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
00558       return -1;
00559    }
00560    ast_cli_register(&cli_show_features);
00561    return 0;
00562 }
00563 
00564 int reload()
00565 {
00566    return 0;
00567 }
00568 
00569 int unload_module()
00570 {
00571    struct feature_pvt *p, *prev;
00572    /* First, take us out of the channel loop */
00573    ast_cli_unregister(&cli_show_features);
00574    ast_channel_unregister(&features_tech);
00575    if (!ast_mutex_lock(&featurelock)) {
00576       /* Hangup all interfaces if they have an owner */
00577       for (p = features; p; p = p->next) {
00578          prev = p;
00579          if (p->owner)
00580             ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
00581          free(prev);
00582       }
00583       features = NULL;
00584       ast_mutex_unlock(&featurelock);
00585    } else {
00586       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
00587       return -1;
00588    }     
00589    return 0;
00590 }
00591 
00592 int usecount()
00593 {
00594    return usecnt;
00595 }
00596 
00597 char *key()
00598 {
00599    return ASTERISK_GPL_KEY;
00600 }
00601 
00602 char *description()
00603 {
00604    return (char *) desc;
00605 }
00606 

Generated on Fri May 26 01:45:29 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.6