Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


autochan.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \brief "smart" channels
22  *
23  * \author Mark Michelson <mmichelson@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
33 
34 #include "asterisk/autochan.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/linkedlists.h"
37 #include "asterisk/options.h"
38 #include "asterisk/channel.h"
39 
41 {
42  struct ast_autochan *autochan;
43 
44  if (!chan) {
45  return NULL;
46  }
47 
48  if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
49  return NULL;
50  }
51 
52  autochan->chan = ast_channel_ref(chan);
53 
54  ast_channel_lock(autochan->chan);
55  AST_LIST_INSERT_TAIL(&autochan->chan->autochans, autochan, list);
56  ast_channel_unlock(autochan->chan);
57 
58  ast_debug(1, "Created autochan %p to hold channel %s (%p)\n", autochan, chan->name, chan);
59 
60  return autochan;
61 }
62 
63 void ast_autochan_destroy(struct ast_autochan *autochan)
64 {
65  struct ast_autochan *autochan_iter;
66 
67  ast_channel_lock(autochan->chan);
68  AST_LIST_TRAVERSE_SAFE_BEGIN(&autochan->chan->autochans, autochan_iter, list) {
69  if (autochan_iter == autochan) {
71  ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
72  break;
73  }
74  }
76  ast_channel_unlock(autochan->chan);
77 
78  autochan->chan = ast_channel_unref(autochan->chan);
79 
80  ast_free(autochan);
81 }
82 
83 void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
84 {
85  struct ast_autochan *autochan;
86 
87  AST_LIST_APPEND_LIST(&new_chan->autochans, &old_chan->autochans, list);
88 
89  AST_LIST_TRAVERSE(&new_chan->autochans, autochan, list) {
90  if (autochan->chan == old_chan) {
91  autochan->chan = ast_channel_unref(old_chan);
92  autochan->chan = ast_channel_ref(new_chan);
93 
94  ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
95  autochan, old_chan->name, old_chan, new_chan->name, new_chan);
96  }
97  }
98 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
Main Channel structure associated with a channel.
Definition: channel.h:742
Asterisk main include file. File version handling, generic pbx functions.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
struct ast_channel * chan
Definition: autochan.h:33
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:600
Utility functions.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
General Asterisk PBX channel definitions.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:554
A set of macros to manage forward-linked lists.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
const ast_string_field name
Definition: channel.h:787
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define ast_channel_unlock(chan)
Definition: channel.h:2467
#define ast_free(a)
Definition: astmm.h:97
void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
Switch what channel autochans point to.
Definition: autochan.c:83
void ast_autochan_destroy(struct ast_autochan *autochan)
destroy an ast_autochan structure
Definition: autochan.c:63
struct ast_autochan * ast_autochan_setup(struct ast_channel *chan)
set up a new ast_autochan structure
Definition: autochan.c:40
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2491
#define ast_calloc(a, b)
Definition: astmm.h:82
Options provided by main asterisk program.
struct ast_channel::autochans autochans
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define AST_LIST_APPEND_LIST(head, list, field)
Appends a whole list to the tail of a list.
Definition: linkedlists.h:768
struct ast_autochan::@144 list