autochan.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Digium, Inc.
00005  *
00006  * Mark Michelson <mmichelson@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 /*!
00020  * \file
00021  * \brief "smart" channels
00022  *
00023  * \author Mark Michelson <mmichelson@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369013 $")
00033 
00034 #include "asterisk/autochan.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/linkedlists.h"
00037 #include "asterisk/options.h"
00038 #include "asterisk/channel.h"
00039 
00040 struct ast_autochan *ast_autochan_setup(struct ast_channel *chan)
00041 {
00042    struct ast_autochan *autochan;
00043 
00044    if (!chan) {
00045       return NULL;
00046    }
00047 
00048    if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
00049       return NULL;
00050    }
00051 
00052    autochan->chan = ast_channel_ref(chan);
00053 
00054    ast_channel_lock(autochan->chan);
00055    AST_LIST_INSERT_TAIL(ast_channel_autochans(autochan->chan), autochan, list);
00056    ast_channel_unlock(autochan->chan);
00057 
00058    ast_debug(1, "Created autochan %p to hold channel %s (%p)\n", autochan, ast_channel_name(chan), chan);
00059 
00060    return autochan;
00061 }
00062 
00063 void ast_autochan_destroy(struct ast_autochan *autochan)
00064 {
00065    struct ast_autochan *autochan_iter;
00066 
00067    ast_channel_lock(autochan->chan);
00068    AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_autochans(autochan->chan), autochan_iter, list) {
00069       if (autochan_iter == autochan) {
00070          AST_LIST_REMOVE_CURRENT(list);
00071          ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
00072          break;
00073       }
00074    }
00075    AST_LIST_TRAVERSE_SAFE_END;
00076    ast_channel_unlock(autochan->chan);
00077 
00078    autochan->chan = ast_channel_unref(autochan->chan);
00079 
00080    ast_free(autochan);
00081 }
00082 
00083 void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
00084 {
00085    struct ast_autochan *autochan;
00086 
00087    AST_LIST_APPEND_LIST(ast_channel_autochans(new_chan), ast_channel_autochans(old_chan), list);
00088 
00089    AST_LIST_TRAVERSE(ast_channel_autochans(new_chan), autochan, list) {
00090       if (autochan->chan == old_chan) {
00091          autochan->chan = ast_channel_unref(old_chan);
00092          autochan->chan = ast_channel_ref(new_chan);
00093 
00094          ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
00095                autochan, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
00096       }
00097    }
00098 }

Generated on Thu Apr 16 06:27:15 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6