Version 0.1.7 from FTP
[asterisk/asterisk.git] / channel.c
index b4359d2..f83a4eb 100755 (executable)
--- a/channel.c
+++ b/channel.c
@@ -3,7 +3,7 @@
  *
  * Channel Management
  * 
- * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
+ * Copyright (C) 1999, Mark Spencer
  *
  * Mark Spencer <markster@linux-support.net>
  *
 #include <sys/time.h>
 #include <signal.h>
 #include <errno.h>
+#include <unistd.h>
 #include <asterisk/sched.h>
 #include <asterisk/options.h>
 #include <asterisk/channel.h>
 #include <asterisk/channel_pvt.h>
 #include <asterisk/logger.h>
 #include <asterisk/file.h>
+#include <asterisk/translate.h>
+
+
+
+#ifdef DEBUG_MUTEX
+/* Convenient mutex debugging functions */
+#define PTHREAD_MUTEX_LOCK(a) __PTHREAD_MUTEX_LOCK(__FUNCTION__, a)
+#define PTHREAD_MUTEX_UNLOCK(a) __PTHREAD_MUTEX_UNLOCK(__FUNCTION__, a)
+
+static int __PTHREAD_MUTEX_LOCK(char *f, pthread_mutex_t *a) {
+       ast_log(LOG_DEBUG, "Locking %p (%s)\n", a, f); 
+       return pthread_mutex_lock(a);
+}
+
+static int __PTHREAD_MUTEX_UNLOCK(char *f, pthread_mutex_t *a) {
+       ast_log(LOG_DEBUG, "Unlocking %p (%s)\n", a, f); 
+       return pthread_mutex_unlock(a);
+}
+#else
+#define PTHREAD_MUTEX_LOCK(a) pthread_mutex_lock(a)
+#define PTHREAD_MUTEX_UNLOCK(a) pthread_mutex_unlock(a)
+#endif
 
 struct chanlist {
        char type[80];
@@ -32,6 +55,7 @@ struct chanlist {
        struct ast_channel * (*requester)(char *type, int format, void *data);
        struct chanlist *next;
 } *backends = NULL;
+struct ast_channel *channels = NULL;
 
 /* Protect the channel list (highly unlikely that two things would change
    it at the same time, but still! */
@@ -42,7 +66,7 @@ int ast_channel_register(char *type, char *description, int capabilities,
                struct ast_channel *(*requester)(char *type, int format, void *data))
 {
        struct chanlist *chan, *last=NULL;
-       if (pthread_mutex_lock(&chlock)) {
+       if (PTHREAD_MUTEX_LOCK(&chlock)) {
                ast_log(LOG_WARNING, "Unable to lock channel list\n");
                return -1;
        }
@@ -50,7 +74,7 @@ int ast_channel_register(char *type, char *description, int capabilities,
        while(chan) {
                if (!strcasecmp(type, chan->type)) {
                        ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", type);
-                       pthread_mutex_unlock(&chlock);
+                       PTHREAD_MUTEX_UNLOCK(&chlock);
                        return -1;
                }
                last = chan;
@@ -59,7 +83,7 @@ int ast_channel_register(char *type, char *description, int capabilities,
        chan = malloc(sizeof(struct chanlist));
        if (!chan) {
                ast_log(LOG_WARNING, "Out of memory\n");
-               pthread_mutex_unlock(&chlock);
+               PTHREAD_MUTEX_UNLOCK(&chlock);
                return -1;
        }
        strncpy(chan->type, type, sizeof(chan->type));
@@ -75,7 +99,7 @@ int ast_channel_register(char *type, char *description, int capabilities,
                ast_log(LOG_DEBUG, "Registered handler for '%s' (%s)\n", chan->type, chan->description);
        else if (option_verbose > 1)
                ast_verbose( VERBOSE_PREFIX_2 "Registered channel type '%s' (%s)\n", chan->type, chan->description);
-       pthread_mutex_unlock(&chlock);
+       PTHREAD_MUTEX_UNLOCK(&chlock);
        return 0;
 }
 
@@ -83,6 +107,7 @@ struct ast_channel *ast_channel_alloc(void)
 {
        struct ast_channel *tmp;
        struct ast_channel_pvt *pvt;
+       PTHREAD_MUTEX_LOCK(&chlock);
        tmp = malloc(sizeof(struct ast_channel));
        memset(tmp, 0, sizeof(struct ast_channel));
        if (tmp) {
@@ -97,9 +122,15 @@ struct ast_channel *ast_channel_alloc(void)
                                tmp->state = AST_STATE_DOWN;
                                tmp->stack = -1;
                                tmp->streamid = -1;
+                               tmp->appl = NULL;
+                               tmp->data = NULL;
+                               pthread_mutex_init(&tmp->lock, NULL);
                                strncpy(tmp->context, "default", sizeof(tmp->context));
+                               strncpy(tmp->language, defaultlanguage, sizeof(tmp->language));
                                strncpy(tmp->exten, "s", sizeof(tmp->exten));
                                tmp->priority=1;
+                               tmp->next = channels;
+                               channels= tmp;
                        } else {
                                ast_log(LOG_WARNING, "Unable to create schedule context\n");
                                free(tmp);
@@ -112,9 +143,62 @@ struct ast_channel *ast_channel_alloc(void)
                }
        } else 
                ast_log(LOG_WARNING, "Out of memory\n");
+       PTHREAD_MUTEX_UNLOCK(&chlock);
        return tmp;
 }
 
+struct ast_channel *ast_channel_walk(struct ast_channel *prev)
+{
+       struct ast_channel *l, *ret=NULL;
+       PTHREAD_MUTEX_LOCK(&chlock);
+       l = channels;
+       if (!prev) {
+               PTHREAD_MUTEX_UNLOCK(&chlock);
+               return l;
+       }
+       while(l) {
+               if (l == prev)
+                       ret = l->next;
+               l = l->next;
+       }
+       PTHREAD_MUTEX_UNLOCK(&chlock);
+       return ret;
+       
+}
+
+void ast_channel_free(struct ast_channel *chan)
+{
+       struct ast_channel *last=NULL, *cur;
+       PTHREAD_MUTEX_LOCK(&chlock);
+       cur = channels;
+       while(cur) {
+               if (cur == chan) {
+                       if (last)
+                               last->next = cur->next;
+                       else
+                               channels = cur->next;
+                       break;
+               }
+               last = cur;
+               cur = cur->next;
+       }
+       if (!cur)
+               ast_log(LOG_WARNING, "Unable to find channel in list\n");
+       if (chan->pvt->pvt)
+               ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
+       if (chan->trans)
+               ast_log(LOG_WARNING, "Hard hangup called on '%s' while a translator is in place!  Expect a failure.\n", chan->name);
+       if (chan->pbx) 
+               ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
+       if (chan->dnid)
+               free(chan->dnid);
+       if (chan->callerid)
+               free(chan->callerid);   
+       pthread_mutex_destroy(&chan->lock);
+       free(chan);
+       PTHREAD_MUTEX_UNLOCK(&chlock);
+}
+
 int ast_softhangup(struct ast_channel *chan)
 {
        int res = 0;
@@ -149,18 +233,7 @@ int ast_hangup(struct ast_channel *chan)
                ast_log(LOG_DEBUG, "Hanging up channel '%s'\n", chan->name);
        if (chan->pvt->hangup)
                res = chan->pvt->hangup(chan);
-       if (chan->pvt->pvt)
-               ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n", chan->name);
-       if (chan->trans)
-               ast_log(LOG_WARNING, "Hard hangup called on '%s' while a translator is in place!  Expect a failure.\n", chan->name);
-       if (chan->pbx) 
-               ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
-       if (chan->dnid)
-               free(chan->dnid);
-       if (chan->callerid)
-               free(chan->callerid);   
-       free(chan->pvt);
-       free(chan);
+       ast_channel_free(chan);
        return res;
 }
 
@@ -169,7 +242,7 @@ void ast_channel_unregister(char *type)
        struct chanlist *chan, *last=NULL;
        if (option_debug)
                ast_log(LOG_DEBUG, "Unregistering channel type '%s'\n", type);
-       if (pthread_mutex_lock(&chlock)) {
+       if (PTHREAD_MUTEX_LOCK(&chlock)) {
                ast_log(LOG_WARNING, "Unable to lock channel list\n");
                return;
        }
@@ -181,13 +254,13 @@ void ast_channel_unregister(char *type)
                        else
                                backends = backends->next;
                        free(chan);
-                       pthread_mutex_unlock(&chlock);
+                       PTHREAD_MUTEX_UNLOCK(&chlock);
                        return;
                }
                last = chan;
                chan = chan->next;
        }
-       pthread_mutex_unlock(&chlock);
+       PTHREAD_MUTEX_UNLOCK(&chlock);
 }
 
 int ast_answer(struct ast_channel *chan)
@@ -315,6 +388,16 @@ struct ast_frame *ast_read(struct ast_channel *chan)
        return f;
 }
 
+int ast_sendtext(struct ast_channel *chan, char *text)
+{
+       int res = 0;
+       CHECK_BLOCKING(chan);
+       if (chan->pvt->send_text)
+               res = chan->pvt->send_text(chan, text);
+       chan->blocking = 0;
+       return res;
+}
+
 int ast_write(struct ast_channel *chan, struct ast_frame *fr)
 {
        int res = -1;
@@ -341,22 +424,26 @@ struct ast_channel *ast_request(char *type, int format, void *data)
 {
        struct chanlist *chan;
        struct ast_channel *c = NULL;
-       if (pthread_mutex_lock(&chlock)) {
+       if (PTHREAD_MUTEX_LOCK(&chlock)) {
                ast_log(LOG_WARNING, "Unable to lock channel list\n");
                return NULL;
        }
        chan = backends;
        while(chan) {
                if (!strcasecmp(type, chan->type)) {
+                       if (!(chan->capabilities & format)) {
+                               format = ast_translator_best_choice(format, chan->capabilities);
+                       }
+                       PTHREAD_MUTEX_UNLOCK(&chlock);
                        if (chan->requester)
                                c = chan->requester(type, format, data);
-                       pthread_mutex_unlock(&chlock);
-                       break;
+                       return c;
                }
                chan = chan->next;
        }
        if (!chan)
                ast_log(LOG_WARNING, "No channel type registered for '%s'\n", type);
+       PTHREAD_MUTEX_UNLOCK(&chlock);
        return c;
 }
 
@@ -379,9 +466,10 @@ int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int fti
        if (!len)
                return -1;
        do {
-               if (c->streamid > -1) {
+               if ((c->streamid > -1) || (c->trans && (c->trans->streamid > -1))) {
                        d = ast_waitstream(c, AST_DIGIT_ANY);
                        ast_stopstream(c);
+                       usleep(1000);
                        if (!d)
                                d = ast_waitfordigit(c, to);
                } else {