Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 <[email protected]>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include "asterisk/autochan.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/linkedlists.h"
35 #include "asterisk/options.h"
36 #include "asterisk/channel.h"
37 
39 {
40  struct ast_autochan *autochan;
41 
42  if (!chan) {
43  return NULL;
44  }
45 
46  if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
47  return NULL;
48  }
49  ast_mutex_init(&autochan->lock);
50 
51  autochan->chan = ast_channel_ref(chan);
52 
53  ast_debug(1, "Created autochan %p to hold channel %s (%p)\n",
54  autochan, ast_channel_name(chan), chan);
55 
56  /* autochan is still private, no need for ast_autochan_channel_lock() */
57  ast_channel_lock(autochan->chan);
58  AST_LIST_INSERT_TAIL(ast_channel_autochans(autochan->chan), autochan, list);
59  ast_channel_unlock(autochan->chan);
60 
61  return autochan;
62 }
63 
64 void ast_autochan_destroy(struct ast_autochan *autochan)
65 {
66  struct ast_autochan *autochan_iter;
67 
68  ast_autochan_channel_lock(autochan);
69  AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_autochans(autochan->chan), autochan_iter, list) {
70  if (autochan_iter == autochan) {
72  ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
73  break;
74  }
75  }
78 
79  autochan->chan = ast_channel_unref(autochan->chan);
80 
81  ast_mutex_destroy(&autochan->lock);
82 
83  ast_free(autochan);
84 }
85 
86 void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
87 {
88  struct ast_autochan *autochan;
89 
91 
92  /* Deadlock avoidance is not needed since the channels are already locked. */
93  AST_LIST_TRAVERSE(ast_channel_autochans(new_chan), autochan, list) {
94  ast_mutex_lock(&autochan->lock);
95  if (autochan->chan == old_chan) {
96  autochan->chan = ast_channel_ref(new_chan);
97  ast_channel_unref(old_chan);
98 
99  ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
100  autochan, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
101  }
102  ast_mutex_unlock(&autochan->lock);
103  }
104 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_autochan_channel_lock(autochan)
Lock the autochan&#39;s channel lock.
Definition: autochan.h:75
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
struct ast_channel * chan
Definition: autochan.h:33
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:614
Utility functions.
struct ast_autochan::@226 list
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
General Asterisk PBX channel definitions.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:556
A set of macros to manage forward-linked lists.
void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
Switch what channel autochans point to.
Definition: autochan.c:86
"smart" channels that update automatically if a channel is masqueraded
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
void ast_autochan_destroy(struct ast_autochan *autochan)
destroy an ast_autochan structure
Definition: autochan.c:64
#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:2946
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
struct ast_autochan_list * ast_channel_autochans(struct ast_channel *chan)
struct ast_autochan * ast_autochan_setup(struct ast_channel *chan)
set up a new ast_autochan structure
Definition: autochan.c:38
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2970
const char * ast_channel_name(const struct ast_channel *chan)
Options provided by main asterisk program.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
#define ast_autochan_channel_unlock(autochan)
Definition: autochan.h:84
#define ast_mutex_init(pmutex)
Definition: lock.h:184
#define ast_mutex_destroy(a)
Definition: lock.h:186
#define ast_mutex_unlock(a)
Definition: lock.h:188
ast_mutex_t lock
Definition: autochan.h:35
#define AST_LIST_APPEND_LIST(head, list, field)
Appends a whole list to the tail of a list.
Definition: linkedlists.h:782