Asterisk - The Open Source Telephony Project  18.5.0
app_bridgeaddchan.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Alec Davis <[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 /*! \file
20  *
21  * \brief Application to place the channel into an existing Bridge
22  *
23  * \author Alec Davis
24  *
25  * \ingroup applications
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include "asterisk/file.h"
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/features.h"
39 
40 /*** DOCUMENTATION
41  <application name="BridgeAdd" language="en_US">
42  <synopsis>
43  Join a bridge that contains the specified channel.
44  </synopsis>
45  <syntax>
46  <parameter name="channel" required="true">
47  <para>The current channel joins the bridge containing the channel
48  identified by the channel name, channel name prefix, or channel
49  uniqueid.</para>
50  </parameter>
51  </syntax>
52  <description>
53  <para>This application places the incoming channel into
54  the bridge containing the specified channel. The specified
55  channel only needs to be the prefix of a full channel name
56  IE. 'PJSIP/cisco0001'.
57  </para>
58  <para>This application sets the following channel variable upon completion:</para>
59  <variablelist>
60  <variable name="BRIDGERESULT">
61  <para>The result of the bridge attempt as a text string.</para>
62  <value name="SUCCESS" />
63  <value name="FAILURE" />
64  <value name="LOOP" />
65  <value name="NONEXISTENT" />
66  </variable>
67  </variablelist>
68  </description>
69  </application>
70  ***/
71 
72 static const char app[] = "BridgeAdd";
73 
74 static int bridgeadd_exec(struct ast_channel *chan, const char *data)
75 {
76  struct ast_channel *c_ref;
77  struct ast_bridge_features chan_features;
78  struct ast_bridge *bridge;
79  char *c_name;
80  int failed;
81 
82  /* Answer the channel if needed */
83  if (ast_channel_state(chan) != AST_STATE_UP) {
84  ast_answer(chan);
85  }
86 
87  if (ast_strlen_zero(data)) {
88  data = "";
89  c_ref = NULL;
90  } else {
91  c_ref = ast_channel_get_by_name_prefix(data, strlen(data));
92  }
93  if (!c_ref) {
94  ast_verb(4, "Channel '%s' not found\n", data);
95  pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
96  return 0;
97  }
98  if (chan == c_ref) {
99  ast_channel_unref(c_ref);
100  pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
101  return 0;
102  }
103 
104  c_name = ast_strdupa(ast_channel_name(c_ref));
105 
106  ast_channel_lock(c_ref);
107  bridge = ast_channel_get_bridge(c_ref);
108  ast_channel_unlock(c_ref);
109 
110  ast_channel_unref(c_ref);
111 
112  if (!bridge) {
113  ast_verb(4, "Channel '%s' is not in a bridge\n", c_name);
114  pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
115  return 0;
116  }
117 
118  ast_verb(4, "%s is joining %s in bridge %s\n",
119  ast_channel_name(chan), c_name, bridge->uniqueid);
120 
121  failed = ast_bridge_features_init(&chan_features)
122  || ast_bridge_join(bridge, chan, NULL, &chan_features, NULL, 0);
123  if (failed) {
124  ast_verb(4, "%s failed to join %s in bridge %s\n",
125  ast_channel_name(chan), c_name, bridge->uniqueid);
126  }
127 
128  ast_bridge_features_cleanup(&chan_features);
129  ao2_cleanup(bridge);
130  pbx_builtin_setvar_helper(chan, "BRIDGERESULT", failed ? "FAILURE" : "SUCCESS");
131  return 0;
132 }
133 
134 static int unload_module(void)
135 {
137 }
138 
139 static int load_module(void)
140 {
142 }
143 
144 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bridge Add Channel Application");
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
void ast_bridge_features_cleanup(struct ast_bridge_features *features)
Clean up the contents of a bridge features structure.
Definition: bridge.c:3720
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
const ast_string_field uniqueid
Definition: bridge.h:409
Structure that contains features information.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2981
int ast_bridge_features_init(struct ast_bridge_features *features)
Initialize bridge features structure.
Definition: bridge.c:3687
ast_channel_state
ast_channel states
Definition: channelstate.h:35
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
#define NULL
Definition: resample.c:96
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_verb(level,...)
Definition: logger.h:463
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1434
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ast_bridge * ast_channel_get_bridge(const struct ast_channel *chan)
Get the bridge associated with a channel.
Definition: channel.c:10735
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static const char app[]
Structure that contains information about a bridge.
Definition: bridge.h:357
static int unload_module(void)
static int bridgeadd_exec(struct ast_channel *chan, const char *data)
static int load_module(void)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
int ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, struct ast_bridge_tech_optimizations *tech_args, enum ast_bridge_join_flags flags)
Join a channel to a bridge (blocking)
Definition: bridge.c:1667
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
const char * ast_channel_name(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2814
Call Parking and Pickup API Includes code and algorithms from the Zapata library. ...
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Bridging API.
Asterisk module definitions.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626