use auto-build for cdr modules
[asterisk/asterisk.git] / channels / chan_features.c
old mode 100755 (executable)
new mode 100644 (file)
index 18f7367..f8047d3
@@ -1,14 +1,30 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * feature Proxy Channel
- * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999 - 2005, Digium, Inc.
+ *
+ * Mark Spencer <markster@digium.com>
  *
- * Mark Spencer <markster@linux-support.net>
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
  *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief feature Proxy Channel
+ *
+ * \author Mark Spencer <markster@digium.com>
+ *
+ * \note *** Experimental code ****
+ * 
+ * \ingroup channel_drivers
  */
 
 #include <stdio.h>
@@ -45,9 +61,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/musiconhold.h"
 #include "asterisk/manager.h"
+#include "asterisk/stringfields.h"
 
 static const char desc[] = "Feature Proxy Channel";
-static const char type[] = "Feature";
 static const char tdesc[] = "Feature Proxy Channel Driver";
 
 static int usecnt =0;
@@ -55,9 +71,6 @@ AST_MUTEX_DEFINE_STATIC(usecnt_lock);
 
 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
 
-/* Protect the interface list (of feature_pvt's) */
-AST_MUTEX_DEFINE_STATIC(featurelock);
-
 struct feature_sub {
        struct ast_channel *owner;
        int inthreeway;
@@ -66,15 +79,17 @@ struct feature_sub {
        int alertpipebackup[2];
 };
 
-static struct feature_pvt {
+struct feature_pvt {
        ast_mutex_t lock;                       /* Channel private lock */
        char tech[AST_MAX_EXTENSION];           /* Technology to abstract */
        char dest[AST_MAX_EXTENSION];           /* Destination to abstract */
        struct ast_channel *subchan;
        struct feature_sub subs[3];             /* Subs */
        struct ast_channel *owner;              /* Current Master Channel */
-       struct feature_pvt *next;               /* Next entity */
-} *features = NULL;
+       AST_LIST_ENTRY(feature_pvt) list;       /* Next entity */
+};
+
+static AST_LIST_HEAD_STATIC(features, feature_pvt);
 
 #define SUB_REAL       0                       /* Active call */
 #define SUB_CALLWAIT   1                       /* Call-Waiting call on hold */
@@ -91,7 +106,7 @@ static int features_indicate(struct ast_channel *ast, int condition);
 static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
 
 static const struct ast_channel_tech features_tech = {
-       .type = type,
+       .type = "Feature",
        .description = tdesc,
        .capabilities = -1,
        .requester = features_request,
@@ -121,7 +136,7 @@ static inline int indexof(struct feature_pvt *p, struct ast_channel *owner, int
                ast_log(LOG_WARNING, "indexof called on NULL owner??\n");
                return -1;
        }
-       for (x=0;x<3;x++) {
+       for (x=0; x<3; x++) {
                if (owner == p->subs[x].owner)
                        return x;
        }
@@ -155,15 +170,15 @@ static void restore_channel(struct feature_pvt *p, int index)
        p->subs[index].owner->timingfd = p->subs[index].timingfdbackup;
        p->subs[index].owner->alertpipe[0] = p->subs[index].alertpipebackup[0];
        p->subs[index].owner->alertpipe[1] = p->subs[index].alertpipebackup[1];
-       p->subs[index].owner->fds[AST_MAX_FDS-1] = p->subs[index].alertpipebackup[0];
-       p->subs[index].owner->fds[AST_MAX_FDS-2] = p->subs[index].timingfdbackup;
+       p->subs[index].owner->fds[AST_ALERT_FD] = p->subs[index].alertpipebackup[0];
+       p->subs[index].owner->fds[AST_TIMING_FD] = p->subs[index].timingfdbackup;
 }
 
 static void update_features(struct feature_pvt *p, int index)
 {
        int x;
        if (p->subs[index].owner) {
-               for (x=0;x<AST_MAX_FDS;x++) {
+               for (x=0; x<AST_MAX_FDS; x++) {
                        if (index) 
                                p->subs[index].owner->fds[x] = -1;
                        else
@@ -215,6 +230,7 @@ static int features_answer(struct ast_channel *ast)
        struct feature_pvt *p = ast->tech_pvt;
        int res = -1;
        int x;
+
        ast_mutex_lock(&p->lock);
        x = indexof(p, ast, 0);
        if (!x && p->subchan)
@@ -225,12 +241,11 @@ static int features_answer(struct ast_channel *ast)
 
 static struct ast_frame  *features_read(struct ast_channel *ast)
 {
-       static struct ast_frame null_frame = { AST_FRAME_NULL, };
        struct feature_pvt *p = ast->tech_pvt;
        struct ast_frame *f;
        int x;
        
-       f = &null_frame;
+       f = &ast_null_frame;
        ast_mutex_lock(&p->lock);
        x = indexof(p, ast, 0);
        if (!x && p->subchan) {
@@ -246,6 +261,7 @@ static int features_write(struct ast_channel *ast, struct ast_frame *f)
        struct feature_pvt *p = ast->tech_pvt;
        int res = -1;
        int x;
+
        ast_mutex_lock(&p->lock);
        x = indexof(p, ast, 0);
        if (!x && p->subchan)
@@ -258,10 +274,11 @@ static int features_fixup(struct ast_channel *oldchan, struct ast_channel *newch
 {
        struct feature_pvt *p = newchan->tech_pvt;
        int x;
+
        ast_mutex_lock(&p->lock);
        if (p->owner == oldchan)
                p->owner = newchan;
-       for (x=0;x<3;x++) {
+       for (x = 0; x < 3; x++) {
                if (p->subs[x].owner == oldchan)
                        p->subs[x].owner = newchan;
        }
@@ -274,6 +291,7 @@ static int features_indicate(struct ast_channel *ast, int condition)
        struct feature_pvt *p = ast->tech_pvt;
        int res = -1;
        int x;
+
        /* Queue up a frame representing the indication as a control frame */
        ast_mutex_lock(&p->lock);
        x = indexof(p, ast, 0);
@@ -288,6 +306,7 @@ static int features_digit(struct ast_channel *ast, char digit)
        struct feature_pvt *p = ast->tech_pvt;
        int res = -1;
        int x;
+
        /* Queue up a frame representing the indication as a control frame */
        ast_mutex_lock(&p->lock);
        x = indexof(p, ast, 0);
@@ -329,8 +348,9 @@ static int features_call(struct ast_channel *ast, char *dest, int timeout)
                        else
                                p->subchan->cid.cid_ani = NULL;
                
-                       strncpy(p->subchan->language, p->owner->language, sizeof(p->subchan->language) - 1);
-                       strncpy(p->subchan->accountcode, p->owner->accountcode, sizeof(p->subchan->accountcode) - 1);
+                       p->subchan->cid.cid_pres = p->owner->cid.cid_pres;
+                       ast_string_field_set(p->subchan, language, p->owner->language);
+                       ast_string_field_set(p->subchan, accountcode, p->owner->accountcode);
                        p->subchan->cdrflags = p->owner->cdrflags;
                        res = ast_call(p->subchan, dest2, timeout);
                        update_features(p, x);
@@ -344,7 +364,6 @@ static int features_call(struct ast_channel *ast, char *dest, int timeout)
 static int features_hangup(struct ast_channel *ast)
 {
        struct feature_pvt *p = ast->tech_pvt;
-       struct feature_pvt *cur, *prev=NULL;
        int x;
 
        ast_mutex_lock(&p->lock);
@@ -356,24 +375,12 @@ static int features_hangup(struct ast_channel *ast)
        }
        ast->tech_pvt = NULL;
        
-       
        if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
                ast_mutex_unlock(&p->lock);
                /* Remove from list */
-               ast_mutex_lock(&featurelock);
-               cur = features;
-               while(cur) {
-                       if (cur == p) {
-                               if (prev)
-                                       prev->next = cur->next;
-                               else
-                                       features = cur->next;
-                               break;
-                       }
-                       prev = cur;
-                       cur = cur->next;
-               }
-               ast_mutex_unlock(&featurelock);
+               AST_LIST_LOCK(&features);
+               AST_LIST_REMOVE(&features, p, list);
+               AST_LIST_UNLOCK(&features);
                ast_mutex_lock(&p->lock);
                /* And destroy */
                if (p->subchan)
@@ -409,14 +416,12 @@ static struct feature_pvt *features_alloc(char *data, int format)
                        data);
                return NULL;
        }
-       ast_mutex_lock(&featurelock);
-       tmp = features;
-       while(tmp) {
+       AST_LIST_LOCK(&features);
+       AST_LIST_TRAVERSE(&features, tmp, list) {
                if (!strcasecmp(tmp->tech, tech) && !strcmp(tmp->dest, dest))
                        break;
-               tmp = tmp->next;
        }
-       ast_mutex_unlock(&featurelock);
+       AST_LIST_UNLOCK(&features);
        if (!tmp) {
                chan = ast_request(tech, format, dest, &status);
                if (!chan) {
@@ -432,10 +437,9 @@ static struct feature_pvt *features_alloc(char *data, int format)
                        strncpy(tmp->tech, tech, sizeof(tmp->tech) - 1);
                        strncpy(tmp->dest, dest, sizeof(tmp->dest) - 1);
                        tmp->subchan = chan;
-                       ast_mutex_lock(&featurelock);
-                       tmp->next = features;
-                       features = tmp;
-                       ast_mutex_unlock(&featurelock);
+                       AST_LIST_LOCK(&features);
+                       AST_LIST_INSERT_HEAD(&features, tmp, list);
+                       AST_LIST_UNLOCK(&features);
                }
        }
        return tmp;
@@ -460,7 +464,7 @@ static struct ast_channel *features_new(struct feature_pvt *p, int state, int in
        }
        tmp->tech = &features_tech;
        for (x=1;x<4;x++) {
-               snprintf(tmp->name, sizeof(tmp->name), "Feature/%s/%s-%d", p->tech, p->dest, x);
+               ast_string_field_build(tmp, name, "Feature/%s/%s-%d", p->tech, p->dest, x);
                for (y=0;y<3;y++) {
                        if (y == index)
                                continue;
@@ -470,7 +474,6 @@ static struct ast_channel *features_new(struct feature_pvt *p, int state, int in
                if (y >= 3)
                        break;
        }
-       tmp->type = type;
        ast_setstate(tmp, state);
        tmp->writeformat = p->subchan->writeformat;
        tmp->rawwriteformat = p->subchan->rawwriteformat;
@@ -493,6 +496,7 @@ static struct ast_channel *features_request(const char *type, int format, void *
 {
        struct feature_pvt *p;
        struct ast_channel *chan = NULL;
+
        p = features_alloc(data, format);
        if (p && !p->subs[SUB_REAL].owner)
                chan = features_new(p, AST_STATE_DOWN, SUB_REAL);
@@ -507,17 +511,19 @@ static int features_show(int fd, int argc, char **argv)
 
        if (argc != 3)
                return RESULT_SHOWUSAGE;
-       ast_mutex_lock(&featurelock);
-       p = features;
-       while(p) {
+
+       if (AST_LIST_EMPTY(&features)) {
+               ast_cli(fd, "No feature channels in use\n");
+               return RESULT_SUCCESS;
+       }
+
+       AST_LIST_LOCK(&features);
+       AST_LIST_TRAVERSE(&features, p, list) {
                ast_mutex_lock(&p->lock);
                ast_cli(fd, "%s -- %s/%s\n", p->owner ? p->owner->name : "<unowned>", p->tech, p->dest);
                ast_mutex_unlock(&p->lock);
-               p = p->next;
        }
-       if (!features)
-               ast_cli(fd, "No feature channels in use\n");
-       ast_mutex_unlock(&featurelock);
+       AST_LIST_UNLOCK(&features);
        return RESULT_SUCCESS;
 }
 
@@ -533,7 +539,7 @@ int load_module()
 {
        /* Make sure we can register our sip channel type */
        if (ast_channel_register(&features_tech)) {
-               ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+               ast_log(LOG_ERROR, "Unable to register channel class 'Feature'\n");
                return -1;
        }
        ast_cli_register(&cli_show_features);
@@ -548,33 +554,29 @@ int reload()
 int unload_module()
 {
        struct feature_pvt *p;
+       
        /* First, take us out of the channel loop */
        ast_cli_unregister(&cli_show_features);
        ast_channel_unregister(&features_tech);
-       if (!ast_mutex_lock(&featurelock)) {
-               /* Hangup all interfaces if they have an owner */
-               p = features;
-               while(p) {
-                       if (p->owner)
-                               ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
-                       p = p->next;
-               }
-               features = NULL;
-               ast_mutex_unlock(&featurelock);
-       } else {
-               ast_log(LOG_WARNING, "Unable to lock the monitor\n");
+       
+       if (!AST_LIST_LOCK(&features))
                return -1;
-       }               
+       /* Hangup all interfaces if they have an owner */
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&features, p, list) {
+               if (p->owner)
+                       ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
+               AST_LIST_REMOVE_CURRENT(&features, list);
+               free(p);
+       }
+       AST_LIST_TRAVERSE_SAFE_END
+       AST_LIST_UNLOCK(&features);
+       
        return 0;
 }
 
 int usecount()
 {
-       int res;
-       ast_mutex_lock(&usecnt_lock);
-       res = usecnt;
-       ast_mutex_unlock(&usecnt_lock);
-       return res;
+       return usecnt;
 }
 
 char *key()