2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2012, Digium, Inc.
5 * Copyright (C) 2012, Russell Bryant
7 * Mark Spencer <markster@digium.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Routines implementing call features as call pickup, parking and transfer
24 * \author Mark Spencer <markster@digium.com>
27 /*! \li \ref features.c uses the configuration file \ref features.conf
28 * \addtogroup configuration_file Configuration Files
32 * \page features.conf features.conf
33 * \verbinclude features.conf.sample
37 <support_level>core</support_level>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/_private.h"
49 #include <sys/signal.h>
50 #include <netinet/in.h>
52 #include "asterisk/lock.h"
53 #include "asterisk/file.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/pbx.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/module.h"
58 #include "asterisk/translate.h"
59 #include "asterisk/app.h"
60 #include "asterisk/say.h"
61 #include "asterisk/features.h"
62 #include "asterisk/musiconhold.h"
63 #include "asterisk/config.h"
64 #include "asterisk/cli.h"
65 #include "asterisk/manager.h"
66 #include "asterisk/utils.h"
67 #include "asterisk/adsi.h"
68 #include "asterisk/devicestate.h"
69 #include "asterisk/monitor.h"
70 #include "asterisk/audiohook.h"
71 #include "asterisk/global_datastores.h"
72 #include "asterisk/astobj2.h"
73 #include "asterisk/test.h"
74 #include "asterisk/bridge.h"
75 #include "asterisk/bridge_features.h"
76 #include "asterisk/bridge_basic.h"
77 #include "asterisk/bridge_after.h"
78 #include "asterisk/stasis.h"
79 #include "asterisk/stasis_channels.h"
80 #include "asterisk/features_config.h"
83 <application name="Bridge" language="en_US">
88 <parameter name="channel" required="true">
89 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
91 <parameter name="options">
94 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
96 <option name="F" argsep="^">
97 <argument name="context" required="false" />
98 <argument name="exten" required="false" />
99 <argument name="priority" required="true" />
100 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
101 to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
103 <para>Any channel variables you want the called channel to inherit from the caller channel must be
104 prefixed with one or two underbars ('_').</para>
107 <para>This option will override the 'x' option</para>
111 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
112 to the next priority of the current extension and <emphasis>start</emphasis> execution
113 at that location.</para>
115 <para>Any channel variables you want the called channel to inherit from the caller channel must be
116 prefixed with one or two underbars ('_').</para>
119 <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
122 <para>This option will override the 'x' option</para>
127 <para>Allow the called party to hang up by sending the
128 <replaceable>*</replaceable> DTMF digit.</para>
131 <para>Allow the calling party to hang up by pressing the
132 <replaceable>*</replaceable> DTMF digit.</para>
135 <para>Allow the called party to enable parking of the call by sending
136 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
139 <para>Allow the calling party to enable parking of the call by sending
140 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
142 <option name="L(x[:y][:z])">
143 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
144 when <replaceable>y</replaceable> ms are left. Repeat the warning every
145 <replaceable>z</replaceable> ms. The following special variables can be
146 used with this option:</para>
148 <variable name="LIMIT_PLAYAUDIO_CALLER">
149 <para>Play sounds to the caller. yes|no (default yes)</para>
151 <variable name="LIMIT_PLAYAUDIO_CALLEE">
152 <para>Play sounds to the callee. yes|no</para>
154 <variable name="LIMIT_TIMEOUT_FILE">
155 <para>File to play when time is up.</para>
157 <variable name="LIMIT_CONNECT_FILE">
158 <para>File to play when call begins.</para>
160 <variable name="LIMIT_WARNING_FILE">
161 <para>File to play as warning if <replaceable>y</replaceable> is
162 defined. The default is to say the time remaining.</para>
167 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
170 <para>Allow the called party to transfer the calling party by sending the
171 DTMF sequence defined in <filename>features.conf</filename>.</para>
174 <para>Allow the calling party to transfer the called party by sending the
175 DTMF sequence defined in <filename>features.conf</filename>.</para>
178 <para>Allow the called party to enable recording of the call by sending
179 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
182 <para>Allow the calling party to enable recording of the call by sending
183 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
186 <para>Cause the called party to be hung up after the bridge, instead of being
187 restarted in the dialplan.</para>
193 <para>Allows the ability to bridge two channels via the dialplan.</para>
194 <para>This application sets the following channel variable upon completion:</para>
196 <variable name="BRIDGERESULT">
197 <para>The result of the bridge attempt as a text string.</para>
198 <value name="SUCCESS" />
199 <value name="FAILURE" />
200 <value name="LOOP" />
201 <value name="NONEXISTENT" />
202 <value name="INCOMPATIBLE" />
207 <manager name="Bridge" language="en_US">
209 Bridge two channels already in the PBX.
212 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
213 <parameter name="Channel1" required="true">
214 <para>Channel to Bridge to Channel2.</para>
216 <parameter name="Channel2" required="true">
217 <para>Channel to Bridge to Channel1.</para>
219 <parameter name="Tone">
220 <para>Play courtesy tone to Channel 2.</para>
223 <enum name="Channel1" />
224 <enum name="Channel2" />
230 <para>Bridge together two channels already in the PBX.</para>
236 FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
237 FEATURE_INTERPRET_DO, /* Used by feature_interpret */
238 FEATURE_INTERPRET_CHECK, /* Used by feature_check */
239 } feature_interpret_op;
241 struct ast_dial_features {
242 /*! Channel's feature flags. */
243 struct ast_flags my_features;
244 /*! Bridge peer's feature flags. */
245 struct ast_flags peer_features;
248 static void *dial_features_duplicate(void *data)
250 struct ast_dial_features *df = data, *df_copy;
252 if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
256 memcpy(df_copy, df, sizeof(*df));
261 static const struct ast_datastore_info dial_features_info = {
262 .type = "dial-features",
263 .destroy = ast_free_ptr,
264 .duplicate = dial_features_duplicate,
269 * \brief Set the features datastore if it doesn't exist.
271 * \param chan Channel to add features datastore
272 * \param my_features The channel's feature flags
273 * \param peer_features The channel's bridge peer feature flags
275 * \retval TRUE if features datastore already existed.
277 static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
279 struct ast_datastore *datastore;
280 struct ast_dial_features *dialfeatures;
282 ast_channel_lock(chan);
283 datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
284 ast_channel_unlock(chan);
286 /* Already exists. */
290 /* Create a new datastore with specified feature flags. */
291 datastore = ast_datastore_alloc(&dial_features_info, NULL);
293 ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
296 dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
298 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
299 ast_datastore_free(datastore);
302 ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
303 ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
304 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
305 datastore->data = dialfeatures;
306 ast_channel_lock(chan);
307 ast_channel_datastore_add(chan, datastore);
308 ast_channel_unlock(chan);
312 struct ast_bridge_thread_obj
314 struct ast_bridge_config bconfig;
315 struct ast_channel *chan;
316 struct ast_channel *peer;
317 struct ast_callid *callid; /*<! callid pointer (Only used to bind thread) */
318 unsigned int return_to_pbx:1;
321 static const struct ast_datastore_info channel_app_data_datastore = {
322 .type = "Channel appdata datastore",
323 .destroy = ast_free_ptr,
326 static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
328 ast_clear_flag(config, AST_FLAGS_ALL);
330 if (ast_test_flag(&config->features_caller, AST_FEATURE_DTMF_MASK)) {
331 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
333 if (ast_test_flag(&config->features_callee, AST_FEATURE_DTMF_MASK)) {
334 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
337 if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
338 RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
340 ast_channel_lock(chan);
341 applicationmap = ast_get_chan_applicationmap(chan);
342 ast_channel_unlock(chan);
344 if (!applicationmap) {
348 /* If an applicationmap exists for this channel at all, then the channel needs the DTMF flag set */
349 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
353 void ast_channel_log(char *title, struct ast_channel *chan);
355 void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
357 ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan);
358 ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
359 ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan),
360 ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
361 ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
362 ast_channel_accountcode(chan), ast_channel_dialcontext(chan), ast_channel_amaflags(chan),
363 ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan));
364 ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; uniqueID: %s; linkedID:%s\n",
365 ast_channel_masq(chan), ast_channel_masqr(chan),
366 ast_channel_uniqueid(chan), ast_channel_linkedid(chan));
367 if (ast_channel_masqr(chan)) {
368 ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
369 ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan)));
372 ast_log(LOG_NOTICE, "===== done ====\n");
375 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
379 if (ast_strlen_zero(features)) {
383 for (feature = features; *feature; feature++) {
384 struct ast_flags *party;
386 if (isupper(*feature)) {
387 party = &config->features_caller;
389 party = &config->features_callee;
392 switch (tolower(*feature)) {
394 ast_set_flag(party, AST_FEATURE_REDIRECT);
397 ast_set_flag(party, AST_FEATURE_PARKCALL);
400 ast_set_flag(party, AST_FEATURE_DISCONNECT);
403 ast_set_flag(party, AST_FEATURE_AUTOMON);
406 ast_set_flag(party, AST_FEATURE_AUTOMIXMON);
409 ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
415 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
417 if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
419 * If we don't return here, then when we do a builtin_atxfer we
420 * will copy the disconnect flags over from the atxfer to the
426 add_features_datastore(callee, &config->features_callee, &config->features_caller);
429 static void clear_dialed_interfaces(struct ast_channel *chan)
431 struct ast_datastore *di_datastore;
433 ast_channel_lock(chan);
434 if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
436 ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", ast_channel_name(chan));
438 if (!ast_channel_datastore_remove(chan, di_datastore)) {
439 ast_datastore_free(di_datastore);
442 ast_channel_unlock(chan);
445 static void bridge_config_set_limits_warning_values(struct ast_bridge_config *config, struct ast_bridge_features_limits *limits)
447 if (config->end_sound) {
448 ast_string_field_set(limits, duration_sound, config->end_sound);
451 if (config->warning_sound) {
452 ast_string_field_set(limits, warning_sound, config->warning_sound);
455 if (config->start_sound) {
456 ast_string_field_set(limits, connect_sound, config->start_sound);
459 limits->frequency = config->warning_freq;
460 limits->warning = config->play_warning;
464 * \internal brief Setup limit hook structures on calls that need limits
466 * \param config ast_bridge_config which provides the limit data
467 * \param caller_limits pointer to an ast_bridge_features_limits struct which will store the caller side limits
468 * \param callee_limits pointer to an ast_bridge_features_limits struct which will store the callee side limits
470 static void bridge_config_set_limits(struct ast_bridge_config *config, struct ast_bridge_features_limits *caller_limits, struct ast_bridge_features_limits *callee_limits)
472 if (ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING)) {
473 bridge_config_set_limits_warning_values(config, caller_limits);
476 if (ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING)) {
477 bridge_config_set_limits_warning_values(config, callee_limits);
480 caller_limits->duration = config->timelimit;
481 callee_limits->duration = config->timelimit;
486 * \brief Check if Monitor needs to be started on a channel.
489 * \param chan The bridge considers this channel the caller.
490 * \param peer The bridge considers this channel the callee.
494 static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *peer)
497 const char *monitor_args = NULL;
498 struct ast_channel *monitor_chan = NULL;
500 ast_channel_lock(chan);
501 value = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR");
502 if (!ast_strlen_zero(value)) {
503 monitor_args = ast_strdupa(value);
506 ast_channel_unlock(chan);
508 ast_channel_lock(peer);
509 value = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR");
510 if (!ast_strlen_zero(value)) {
511 monitor_args = ast_strdupa(value);
514 ast_channel_unlock(peer);
517 struct ast_app *monitor_app;
519 monitor_app = pbx_findapp("Monitor");
521 pbx_exec(monitor_chan, monitor_app, monitor_args);
528 * \brief Send the peer channel on its way on bridge start failure.
531 * \param chan Chan to put into autoservice.
532 * \param peer Chan to send to after bridge goto or run hangup handlers and hangup.
536 static void bridge_failed_peer_goto(struct ast_channel *chan, struct ast_channel *peer)
538 if (ast_bridge_setup_after_goto(peer)
539 || ast_pbx_start(peer)) {
540 ast_autoservice_chan_hangup_peer(chan, peer);
544 static int pre_bridge_setup(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config,
545 struct ast_bridge_features *chan_features, struct ast_bridge_features *peer_features)
549 set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
550 add_features_datastores(chan, peer, config);
553 * This is an interesting case. One example is if a ringing
554 * channel gets redirected to an extension that picks up a
555 * parked call. This will make sure that the call taken out of
556 * parking gets told that the channel it just got bridged to is
559 if (ast_channel_state(chan) == AST_STATE_RINGING
560 && ast_channel_visible_indication(peer) != AST_CONTROL_RINGING) {
561 ast_indicate(peer, AST_CONTROL_RINGING);
564 bridge_check_monitor(chan, peer);
566 set_config_flags(chan, config);
568 /* Answer if need be */
569 if (ast_channel_state(chan) != AST_STATE_UP) {
570 if (ast_raw_answer(chan)) {
576 /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
577 ast_channel_log("Pre-bridge CHAN Channel info", chan);
578 ast_channel_log("Pre-bridge PEER Channel info", peer);
582 * If we are bridging a call, stop worrying about forwarding
583 * loops. We presume that if a call is being bridged, that the
584 * humans in charge know what they're doing. If they don't,
585 * well, what can we do about that?
587 clear_dialed_interfaces(chan);
588 clear_dialed_interfaces(peer);
591 ast_channel_lock(chan);
592 res |= ast_bridge_features_ds_append(chan, &config->features_caller);
593 ast_channel_unlock(chan);
594 ast_channel_lock(peer);
595 res |= ast_bridge_features_ds_append(peer, &config->features_callee);
596 ast_channel_unlock(peer);
602 if (config->timelimit) {
603 struct ast_bridge_features_limits call_duration_limits_chan;
604 struct ast_bridge_features_limits call_duration_limits_peer;
605 int abandon_call = 0; /* TRUE if set limits fails so we can abandon the call. */
607 if (ast_bridge_features_limits_construct(&call_duration_limits_chan)) {
608 ast_log(LOG_ERROR, "Could not construct caller duration limits. Bridge canceled.\n");
613 if (ast_bridge_features_limits_construct(&call_duration_limits_peer)) {
614 ast_log(LOG_ERROR, "Could not construct callee duration limits. Bridge canceled.\n");
615 ast_bridge_features_limits_destroy(&call_duration_limits_chan);
620 bridge_config_set_limits(config, &call_duration_limits_chan, &call_duration_limits_peer);
622 if (ast_bridge_features_set_limits(chan_features, &call_duration_limits_chan, 0)) {
625 if (ast_bridge_features_set_limits(peer_features, &call_duration_limits_peer, 0)) {
629 /* At this point we are done with the limits structs since they have been copied to the individual feature sets. */
630 ast_bridge_features_limits_destroy(&call_duration_limits_chan);
631 ast_bridge_features_limits_destroy(&call_duration_limits_peer);
634 ast_log(LOG_ERROR, "Could not set duration limits on one or more sides of the call. Bridge canceled.\n");
642 int ast_bridge_call_with_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, unsigned int flags)
645 struct ast_bridge *bridge;
646 struct ast_bridge_features chan_features;
647 struct ast_bridge_features *peer_features;
649 /* Setup features. */
650 res = ast_bridge_features_init(&chan_features);
651 peer_features = ast_bridge_features_new();
652 if (res || !peer_features) {
653 ast_bridge_features_destroy(peer_features);
654 ast_bridge_features_cleanup(&chan_features);
655 bridge_failed_peer_goto(chan, peer);
659 if (pre_bridge_setup(chan, peer, config, &chan_features, peer_features)) {
660 ast_bridge_features_destroy(peer_features);
661 ast_bridge_features_cleanup(&chan_features);
662 bridge_failed_peer_goto(chan, peer);
667 bridge = ast_bridge_basic_new();
669 ast_bridge_features_destroy(peer_features);
670 ast_bridge_features_cleanup(&chan_features);
671 bridge_failed_peer_goto(chan, peer);
675 ast_bridge_basic_set_flags(bridge, flags);
677 /* Put peer into the bridge */
678 if (ast_bridge_impart(bridge, peer, NULL, peer_features,
679 AST_BRIDGE_IMPART_CHAN_INDEPENDENT | AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP)) {
680 ast_bridge_destroy(bridge, 0);
681 ast_bridge_features_cleanup(&chan_features);
682 bridge_failed_peer_goto(chan, peer);
687 ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
688 AST_BRIDGE_JOIN_PASS_REFERENCE | AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);
691 * If the bridge was broken for a hangup that isn't real, then
692 * don't run the h extension, because the channel isn't really
693 * hung up. This should really only happen with
694 * AST_SOFTHANGUP_ASYNCGOTO.
697 ast_channel_lock(chan);
698 if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
701 ast_channel_unlock(chan);
703 ast_bridge_features_cleanup(&chan_features);
705 if (res && config->end_bridge_callback) {
706 config->end_bridge_callback(config->end_bridge_callback_data);
713 * \brief bridge the call and set CDR
715 * \param chan The bridge considers this channel the caller.
716 * \param peer The bridge considers this channel the callee.
717 * \param config Configuration for this bridge.
719 * Set start time, check for two channels,check if monitor on
720 * check for feature activation, create new CDR
721 * \retval res on success.
722 * \retval -1 on failure to bridge.
724 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
726 return ast_bridge_call_with_flags(chan, peer, config, 0);
729 enum play_tone_action {
731 PLAYTONE_CHANNEL1 = (1 << 0),
732 PLAYTONE_CHANNEL2 = (1 << 1),
733 PLAYTONE_BOTH = PLAYTONE_CHANNEL1 | PLAYTONE_CHANNEL2,
736 static enum play_tone_action parse_playtone(const char *playtone_val)
738 if (ast_strlen_zero(playtone_val) || ast_false(playtone_val)) {
739 return PLAYTONE_NONE;
740 } if (!strcasecmp(playtone_val, "channel1")) {
741 return PLAYTONE_CHANNEL1;
742 } else if (!strcasecmp(playtone_val, "channel2") || ast_true(playtone_val)) {
743 return PLAYTONE_CHANNEL2;
744 } else if (!strcasecmp(playtone_val, "both")) {
745 return PLAYTONE_BOTH;
747 /* Invalid input. Assume none */
748 return PLAYTONE_NONE;
753 * \brief Bridge channels together
757 * Make sure valid channels were specified,
758 * send errors if any of the channels could not be found/locked, answer channels if needed,
759 * create the placeholder channels and grab the other channels
760 * make the channels compatible, send error if we fail doing so
761 * setup the bridge thread object and start the bridge.
765 static int action_bridge(struct mansession *s, const struct message *m)
767 const char *channela = astman_get_header(m, "Channel1");
768 const char *channelb = astman_get_header(m, "Channel2");
769 enum play_tone_action playtone = parse_playtone(astman_get_header(m, "Tone"));
770 RAII_VAR(struct ast_channel *, chana, NULL, ao2_cleanup);
771 RAII_VAR(struct ast_channel *, chanb, NULL, ao2_cleanup);
772 const char *chana_exten;
773 const char *chana_context;
775 const char *chanb_exten;
776 const char *chanb_context;
778 struct ast_bridge *bridge;
780 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_a, NULL, ao2_cleanup);
781 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_b, NULL, ao2_cleanup);
783 /* make sure valid channels were specified */
784 if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
785 astman_send_error(s, m, "Missing channel parameter in request");
789 ast_debug(1, "Performing Bridge action on %s and %s\n", channela, channelb);
791 /* Start with chana */
792 chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
794 snprintf(buf, sizeof(buf), "Channel1 does not exist: %s", channela);
795 astman_send_error(s, m, buf);
798 xfer_cfg_a = ast_get_chan_features_xfer_config(chana);
799 ast_channel_lock(chana);
800 chana_exten = ast_strdupa(ast_channel_exten(chana));
801 chana_context = ast_strdupa(ast_channel_context(chana));
802 chana_priority = ast_channel_priority(chana);
803 if (!ast_test_flag(ast_channel_flags(chana), AST_FLAG_IN_AUTOLOOP)) {
806 ast_channel_unlock(chana);
808 chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
810 snprintf(buf, sizeof(buf), "Channel2 does not exist: %s", channelb);
811 astman_send_error(s, m, buf);
814 xfer_cfg_b = ast_get_chan_features_xfer_config(chanb);
815 ast_channel_lock(chanb);
816 chanb_exten = ast_strdupa(ast_channel_exten(chanb));
817 chanb_context = ast_strdupa(ast_channel_context(chanb));
818 chanb_priority = ast_channel_priority(chanb);
819 if (!ast_test_flag(ast_channel_flags(chanb), AST_FLAG_IN_AUTOLOOP)) {
822 ast_channel_unlock(chanb);
824 bridge = ast_bridge_basic_new();
826 astman_send_error(s, m, "Unable to create bridge\n");
830 ast_bridge_set_after_go_on(chana, chana_context, chana_exten, chana_priority, NULL);
831 if (ast_bridge_add_channel(bridge, chana, NULL, playtone & PLAYTONE_CHANNEL1, xfer_cfg_a ? xfer_cfg_a->xfersound : NULL)) {
832 snprintf(buf, sizeof(buf), "Unable to add Channel1 to bridge: %s", ast_channel_name(chana));
833 astman_send_error(s, m, buf);
834 ast_bridge_destroy(bridge, 0);
838 ast_bridge_set_after_go_on(chanb, chanb_context, chanb_exten, chanb_priority, NULL);
839 if (ast_bridge_add_channel(bridge, chanb, NULL, playtone & PLAYTONE_CHANNEL2, xfer_cfg_b ? xfer_cfg_b->xfersound : NULL)) {
840 snprintf(buf, sizeof(buf), "Unable to add Channel2 to bridge: %s", ast_channel_name(chanb));
841 astman_send_error(s, m, buf);
842 ast_bridge_destroy(bridge, 0);
846 astman_send_ack(s, m, "Channels have been bridged");
851 static char *app_bridge = "Bridge";
854 BRIDGE_OPT_PLAYTONE = (1 << 0),
855 OPT_CALLEE_HANGUP = (1 << 1),
856 OPT_CALLER_HANGUP = (1 << 2),
857 OPT_DURATION_LIMIT = (1 << 3),
858 OPT_DURATION_STOP = (1 << 4),
859 OPT_CALLEE_TRANSFER = (1 << 5),
860 OPT_CALLER_TRANSFER = (1 << 6),
861 OPT_CALLEE_MONITOR = (1 << 7),
862 OPT_CALLER_MONITOR = (1 << 8),
863 OPT_CALLEE_PARK = (1 << 9),
864 OPT_CALLER_PARK = (1 << 10),
865 OPT_CALLEE_KILL = (1 << 11),
866 OPT_CALLEE_GO_ON = (1 << 12),
870 OPT_ARG_DURATION_LIMIT = 0,
871 OPT_ARG_DURATION_STOP,
872 OPT_ARG_CALLEE_GO_ON,
873 /* note: this entry _MUST_ be the last one in the enum */
877 AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
878 AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
879 AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
880 AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
881 AST_APP_OPTION('H', OPT_CALLER_HANGUP),
882 AST_APP_OPTION('k', OPT_CALLEE_PARK),
883 AST_APP_OPTION('K', OPT_CALLER_PARK),
884 AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
885 AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
886 AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
887 AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
888 AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
889 AST_APP_OPTION('W', OPT_CALLER_MONITOR),
890 AST_APP_OPTION('x', OPT_CALLEE_KILL),
893 int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
894 char *parse, struct timeval *calldurationlimit)
896 char *stringp = ast_strdupa(parse);
897 char *limit_str, *warning_str, *warnfreq_str;
899 int play_to_caller = 0, play_to_callee = 0;
902 limit_str = strsep(&stringp, ":");
903 warning_str = strsep(&stringp, ":");
904 warnfreq_str = strsep(&stringp, ":");
906 config->timelimit = atol(limit_str);
908 config->play_warning = atol(warning_str);
910 config->warning_freq = atol(warnfreq_str);
912 if (!config->timelimit) {
913 ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
914 config->timelimit = config->play_warning = config->warning_freq = 0;
915 config->warning_sound = NULL;
916 return -1; /* error */
917 } else if ( (delta = config->play_warning - config->timelimit) > 0) {
918 int w = config->warning_freq;
921 * If the first warning is requested _after_ the entire call
922 * would end, and no warning frequency is requested, then turn
923 * off the warning. If a warning frequency is requested, reduce
924 * the 'first warning' time by that frequency until it falls
925 * within the call's total time limit.
928 * timelim->| delta |<-playwarning
929 * 0__________________|_________________|
932 * so the number of intervals to cut is 1+(delta-1)/w
935 config->play_warning = 0;
937 config->play_warning -= w * ( 1 + (delta-1)/w );
938 if (config->play_warning < 1)
939 config->play_warning = config->warning_freq = 0;
943 ast_channel_lock(chan);
945 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
946 play_to_caller = var ? ast_true(var) : 1;
948 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
949 play_to_callee = var ? ast_true(var) : 0;
951 if (!play_to_caller && !play_to_callee)
954 var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
955 config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
957 /* The code looking at config wants a NULL, not just "", to decide
958 * that the message should not be played, so we replace "" with NULL.
959 * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
963 var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
964 config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
966 var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
967 config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
969 ast_channel_unlock(chan);
971 /* undo effect of S(x) in case they are both used */
972 calldurationlimit->tv_sec = 0;
973 calldurationlimit->tv_usec = 0;
975 /* more efficient to do it like S(x) does since no advanced opts */
976 if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
977 calldurationlimit->tv_sec = config->timelimit / 1000;
978 calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
979 ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
980 calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
983 config->timelimit = 0;
984 config->play_warning = 0;
985 config->warning_freq = 0;
987 ast_verb(4, "Limit Data for this call:\n");
988 ast_verb(4, "timelimit = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
989 ast_verb(4, "play_warning = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
990 ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
991 ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
992 ast_verb(4, "warning_freq = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
993 ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
994 ast_verb(4, "warning_sound = %s\n", config->warning_sound);
995 ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
998 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
1000 ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
1006 * \brief Bridge channels
1008 * \param data channel to bridge with.
1010 * Split data, check we aren't bridging with ourself, check valid channel,
1011 * answer call if not already, check compatible channels, setup bridge config
1012 * now bridge call, if transfered party hangs up return to PBX extension.
1014 static int bridge_exec(struct ast_channel *chan, const char *data)
1016 RAII_VAR(struct ast_channel *, current_dest_chan, NULL, ao2_cleanup);
1017 char *tmp_data = NULL;
1018 struct ast_flags opts = { 0, };
1019 struct ast_bridge_config bconfig = { { 0, }, };
1020 char *opt_args[OPT_ARG_ARRAY_SIZE];
1021 struct timeval calldurationlimit = { 0, };
1022 const char *context;
1023 const char *extension;
1025 struct ast_bridge_features chan_features;
1026 struct ast_bridge_features *peer_features;
1027 struct ast_bridge *bridge;
1028 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
1030 AST_DECLARE_APP_ARGS(args,
1031 AST_APP_ARG(dest_chan);
1032 AST_APP_ARG(options);
1035 if (ast_strlen_zero(data)) {
1036 ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
1040 tmp_data = ast_strdupa(data);
1041 AST_STANDARD_APP_ARGS(args, tmp_data);
1042 if (!ast_strlen_zero(args.options))
1043 ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
1045 /* make sure we have a valid end point */
1046 if (!(current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
1047 strlen(args.dest_chan)))) {
1048 ast_log(LOG_WARNING, "Bridge failed because channel %s does not exist\n",
1053 /* avoid bridge with ourselves */
1054 if (chan == current_dest_chan) {
1055 ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", ast_channel_name(chan));
1059 if (ast_test_flag(&opts, OPT_DURATION_LIMIT)
1060 && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])
1061 && ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit)) {
1062 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
1066 if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
1067 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
1068 if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
1069 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
1070 if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
1071 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
1072 if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
1073 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
1074 if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
1075 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
1076 if (ast_test_flag(&opts, OPT_CALLER_MONITOR))
1077 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
1078 if (ast_test_flag(&opts, OPT_CALLEE_PARK))
1079 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
1080 if (ast_test_flag(&opts, OPT_CALLER_PARK))
1081 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
1083 /* Setup after bridge goto location. */
1084 if (ast_test_flag(&opts, OPT_CALLEE_GO_ON)) {
1085 ast_channel_lock(chan);
1086 context = ast_strdupa(ast_channel_context(chan));
1087 extension = ast_strdupa(ast_channel_exten(chan));
1088 priority = ast_channel_priority(chan);
1089 ast_channel_unlock(chan);
1090 ast_bridge_set_after_go_on(current_dest_chan, context, extension, priority,
1091 opt_args[OPT_ARG_CALLEE_GO_ON]);
1092 } else if (!ast_test_flag(&opts, OPT_CALLEE_KILL)) {
1093 ast_channel_lock(current_dest_chan);
1094 context = ast_strdupa(ast_channel_context(current_dest_chan));
1095 extension = ast_strdupa(ast_channel_exten(current_dest_chan));
1096 priority = ast_channel_priority(current_dest_chan);
1097 ast_channel_unlock(current_dest_chan);
1098 ast_bridge_set_after_goto(current_dest_chan, context, extension, priority);
1101 if (ast_bridge_features_init(&chan_features)) {
1102 ast_bridge_features_cleanup(&chan_features);
1106 peer_features = ast_bridge_features_new();
1107 if (!peer_features) {
1108 ast_bridge_features_cleanup(&chan_features);
1112 if (pre_bridge_setup(chan, current_dest_chan, &bconfig, &chan_features, peer_features)) {
1113 ast_bridge_features_destroy(peer_features);
1114 ast_bridge_features_cleanup(&chan_features);
1118 bridge = ast_bridge_basic_new();
1120 ast_bridge_features_destroy(peer_features);
1121 ast_bridge_features_cleanup(&chan_features);
1125 xfer_cfg = ast_get_chan_features_xfer_config(current_dest_chan);
1126 if (ast_bridge_add_channel(bridge, current_dest_chan, peer_features,
1127 ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE), xfer_cfg ? xfer_cfg->xfersound : NULL)) {
1128 ast_bridge_features_destroy(peer_features);
1129 ast_bridge_features_cleanup(&chan_features);
1130 ast_bridge_destroy(bridge, 0);
1134 ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
1135 AST_BRIDGE_JOIN_PASS_REFERENCE);
1137 ast_bridge_features_cleanup(&chan_features);
1139 /* The bridge has ended, set BRIDGERESULT to SUCCESS. */
1140 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
1142 ast_free((char *) bconfig.warning_sound);
1143 ast_free((char *) bconfig.end_sound);
1144 ast_free((char *) bconfig.start_sound);
1151 * \brief Clean up resources on Asterisk shutdown
1153 static void features_shutdown(void)
1155 ast_features_config_shutdown();
1157 ast_manager_unregister("Bridge");
1159 ast_unregister_application(app_bridge);
1163 int ast_features_init(void)
1167 res = ast_features_config_init();
1171 res |= ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
1172 res |= ast_manager_register_xml_core("Bridge", EVENT_FLAG_CALL, action_bridge);
1175 features_shutdown();
1177 ast_register_atexit(features_shutdown);