2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013 Digium, Inc.
6 * Richard Mudgett <rmudgett@digium.com>
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.
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.
21 * \brief Basic bridge class. It is a subclass of struct ast_bridge.
23 * \author Richard Mudgett <rmudgett@digium.com>
26 * \arg \ref AstCREDITS
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/channel.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/linkedlists.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/bridge_internal.h"
39 #include "asterisk/bridge_basic.h"
40 #include "asterisk/bridge_after.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/features_config.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/file.h"
45 #include "asterisk/app.h"
46 #include "asterisk/dial.h"
47 #include "asterisk/stasis_bridges.h"
49 #define NORMAL_FLAGS (AST_BRIDGE_FLAG_DISSOLVE_HANGUP | AST_BRIDGE_FLAG_DISSOLVE_EMPTY \
50 | AST_BRIDGE_FLAG_SMART)
52 #define TRANSFER_FLAGS AST_BRIDGE_FLAG_SMART
53 #define TRANSFERER_ROLE_NAME "transferer"
55 struct attended_transfer_properties;
57 enum bridge_basic_personality_type {
58 /*! Index for "normal" basic bridge personality */
59 BRIDGE_BASIC_PERSONALITY_NORMAL,
60 /*! Index for attended transfer basic bridge personality */
61 BRIDGE_BASIC_PERSONALITY_ATXFER,
62 /*! Indicates end of enum. Must always remain the last element */
63 BRIDGE_BASIC_PERSONALITY_END,
67 * \brief Change basic bridge personality
69 * Changing personalities allows for the bridge to remain in use but have
70 * properties such as its v_table and its flags change.
72 * \param bridge The bridge
73 * \param type The personality to change the bridge to
74 * \user_data Private data to attach to the personality.
76 static void bridge_basic_change_personality(struct ast_bridge *bridge,
77 enum bridge_basic_personality_type type, void *user_data);
79 /* ------------------------------------------------------------------- */
81 static const struct ast_datastore_info dtmf_features_info = {
82 .type = "bridge-dtmf-features",
83 .destroy = ast_free_ptr,
89 * \brief read a feature code character and set it on for the give feature_flags struct
91 * \param feature_flags flags being modifed
92 * \param feature feature code provided - should be an uppercase letter
94 * \retval 0 if the feature was set successfully
95 * \retval -1 failure because the requested feature code isn't handled by this function
97 static int set_feature_flag_from_char(struct ast_flags *feature_flags, char feature)
101 ast_set_flag(feature_flags, AST_FEATURE_REDIRECT);
104 ast_set_flag(feature_flags, AST_FEATURE_PARKCALL);
107 ast_set_flag(feature_flags, AST_FEATURE_DISCONNECT);
110 ast_set_flag(feature_flags, AST_FEATURE_AUTOMON);
113 ast_set_flag(feature_flags, AST_FEATURE_AUTOMIXMON);
123 * \brief Write a features string to a string buffer based on the feature flags provided
125 * \param feature_flags pointer to the feature flags to write from.
126 * \param buffer pointer to a string buffer to write the features
127 * \param buffer_size size of the buffer provided (should be able to fit all feature codes)
129 * \retval 0 on successful write
130 * \retval -1 failure due to running out of buffer space
132 static int dtmf_features_flags_to_string(struct ast_flags *feature_flags, char *buffer, size_t buffer_size)
134 size_t buffer_expended = 0;
135 unsigned int cur_feature;
136 static const struct {
140 { 'T', AST_FEATURE_REDIRECT },
141 { 'K', AST_FEATURE_PARKCALL },
142 { 'H', AST_FEATURE_DISCONNECT },
143 { 'W', AST_FEATURE_AUTOMON },
144 { 'X', AST_FEATURE_AUTOMIXMON },
147 for (cur_feature = 0; cur_feature < ARRAY_LEN(associations); cur_feature++) {
148 if (ast_test_flag(feature_flags, associations[cur_feature].flag)) {
149 if (buffer_expended == buffer_size - 1) {
150 buffer[buffer_expended] = '\0';
153 buffer[buffer_expended++] = associations[cur_feature].letter;
157 buffer[buffer_expended] = '\0';
161 static int build_dtmf_features(struct ast_flags *flags, const char *features)
165 char missing_features[strlen(features) + 1];
166 size_t number_of_missing_features = 0;
168 for (feature = features; *feature; feature++) {
169 if (!isupper(*feature)) {
170 ast_log(LOG_ERROR, "Features string '%s' rejected because it contains non-uppercase feature.\n", features);
174 if (set_feature_flag_from_char(flags, *feature)) {
175 missing_features[number_of_missing_features++] = *feature;
179 missing_features[number_of_missing_features] = '\0';
181 if (number_of_missing_features) {
182 ast_log(LOG_WARNING, "Features '%s' from features string '%s' can not be applied.\n", missing_features, features);
188 int ast_bridge_features_ds_set_string(struct ast_channel *chan, const char *features)
190 struct ast_flags flags = {0};
192 if (build_dtmf_features(&flags, features)) {
196 ast_channel_lock(chan);
197 if (ast_bridge_features_ds_set(chan, &flags)) {
198 ast_channel_unlock(chan);
199 ast_log(LOG_ERROR, "Failed to apply features datastore for '%s' to channel '%s'\n", features, ast_channel_name(chan));
202 ast_channel_unlock(chan);
207 int ast_bridge_features_ds_get_string(struct ast_channel *chan, char *buffer, size_t buf_size)
209 struct ast_flags *channel_flags;
210 struct ast_flags held_copy;
212 ast_channel_lock(chan);
213 if (!(channel_flags = ast_bridge_features_ds_get(chan))) {
214 ast_channel_unlock(chan);
217 held_copy = *channel_flags;
218 ast_channel_unlock(chan);
220 return dtmf_features_flags_to_string(&held_copy, buffer, buf_size);
223 static int bridge_features_ds_set_full(struct ast_channel *chan, struct ast_flags *flags, int replace)
225 struct ast_datastore *datastore;
226 struct ast_flags *ds_flags;
228 datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
230 ds_flags = datastore->data;
234 flags->flags = flags->flags | ds_flags->flags;
240 datastore = ast_datastore_alloc(&dtmf_features_info, NULL);
245 ds_flags = ast_malloc(sizeof(*ds_flags));
247 ast_datastore_free(datastore);
252 datastore->data = ds_flags;
253 ast_channel_datastore_add(chan, datastore);
257 int ast_bridge_features_ds_set(struct ast_channel *chan, struct ast_flags *flags)
259 return bridge_features_ds_set_full(chan, flags, 1);
262 int ast_bridge_features_ds_append(struct ast_channel *chan, struct ast_flags *flags)
264 return bridge_features_ds_set_full(chan, flags, 0);
267 struct ast_flags *ast_bridge_features_ds_get(struct ast_channel *chan)
269 struct ast_datastore *datastore;
271 datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
275 return datastore->data;
280 * \brief Determine if we should dissolve the bridge from a hangup.
283 * \param bridge_channel Channel executing the feature
284 * \param hook_pvt Private data passed in when the hook was created
286 * \retval 0 Keep the callback hook.
287 * \retval -1 Remove the callback hook.
289 static int basic_hangup_hook(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
291 int bridge_count = 0;
292 struct ast_bridge_channel *iter;
294 ast_bridge_channel_lock_bridge(bridge_channel);
295 AST_LIST_TRAVERSE(&bridge_channel->bridge->channels, iter, entry) {
296 if (iter != bridge_channel && iter->state == BRIDGE_CHANNEL_STATE_WAIT) {
300 if (2 <= bridge_count) {
301 /* Just allow this channel to leave the multi-party bridge. */
302 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
304 ast_bridge_unlock(bridge_channel->bridge);
309 * \brief Details for specific basic bridge personalities
311 struct personality_details {
312 /*! The v_table to use for this personality */
313 struct ast_bridge_methods *v_table;
314 /*! Flags to set on this type of bridge */
315 unsigned int bridge_flags;
316 /*! User data for this personality. If used, must be an ao2 object */
318 /*! Callback to be called when changing to the personality */
319 void (*on_personality_change)(struct ast_bridge *bridge);
323 * \brief structure that organizes different personalities for basic bridges.
325 struct bridge_basic_personality {
326 /*! The current bridge personality in use */
327 enum bridge_basic_personality_type current;
328 /*! Array of details for the types of bridge personalities supported */
329 struct personality_details details[BRIDGE_BASIC_PERSONALITY_END];
332 static int add_normal_hooks(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
334 return ast_bridge_hangup_hook(bridge_channel->features, basic_hangup_hook,
335 NULL, NULL, AST_BRIDGE_HOOK_REMOVE_ON_PULL)
336 || ast_bridge_channel_setup_features(bridge_channel);
341 * \brief ast_bridge basic push method.
344 * \param self Bridge to operate upon.
345 * \param bridge_channel Bridge channel to push.
346 * \param swap Bridge channel to swap places with if not NULL.
348 * \note On entry, self is already locked.
350 * \retval 0 on success
351 * \retval -1 on failure
353 static int bridge_personality_normal_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
355 if (add_normal_hooks(self, bridge_channel)) {
359 ast_bridge_channel_update_accountcodes(bridge_channel, swap);
360 ast_bridge_channel_update_linkedids(bridge_channel, swap);
364 static int bridge_basic_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
366 struct bridge_basic_personality *personality = self->personality;
368 ast_assert(personality != NULL);
370 if (personality->details[personality->current].v_table->push(self, bridge_channel, swap)) {
374 return ast_bridge_base_v_table.push(self, bridge_channel, swap);
377 static void bridge_basic_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
379 struct bridge_basic_personality *personality = self->personality;
381 ast_assert(personality != NULL);
383 if (personality->details[personality->current].v_table->pull) {
384 personality->details[personality->current].v_table->pull(self, bridge_channel);
387 ast_bridge_base_v_table.pull(self, bridge_channel);
390 static void bridge_basic_destroy(struct ast_bridge *self)
392 struct bridge_basic_personality *personality = self->personality;
394 ao2_cleanup(personality);
396 ast_bridge_base_v_table.destroy(self);
400 * \brief Remove appropriate hooks when basic bridge personality changes
402 * Hooks that have the AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE flag
403 * set will be removed from all bridge channels in the bridge.
405 * \param bridge Basic bridge undergoing personality change
407 static void remove_hooks_on_personality_change(struct ast_bridge *bridge)
409 struct ast_bridge_channel *iter;
411 AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
412 SCOPED_LOCK(lock, iter, ast_bridge_channel_lock, ast_bridge_channel_unlock);
413 ast_bridge_features_remove(iter->features, AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
418 * \brief Attended transfer superstates.
420 * An attended transfer's progress is facilitated by a state machine.
421 * The individual states of the state machine fall into the realm of
422 * one of two superstates.
424 enum attended_transfer_superstate {
426 * \brief Transfer superstate
428 * The attended transfer state machine begins in this superstate. The
429 * goal of this state is for a transferer channel to facilitate a
430 * transfer from a transferee to a transfer target.
432 * There are two bridges used in this superstate. The transferee bridge is
433 * the bridge that the transferer and transferee channels originally
434 * communicate in, and the target bridge is the bridge where the transfer
435 * target is being dialed.
437 * The transferer channel is capable of moving between the bridges using
438 * the DTMF swap sequence.
442 * \brief Recall superstate
444 * The attended transfer state machine moves to this superstate if
445 * atxferdropcall is set to "no" and the transferer channel hangs up
446 * during a transfer. The goal in this superstate is to call back either
447 * the transfer target or transferer and rebridge with the transferee
450 * In this superstate, there is only a single bridge used, the original
451 * transferee bridge. Rather than distinguishing between a transferer
452 * and transfer target, all outbound calls are toward a "recall_target"
459 * The states in the attended transfer state machine.
461 enum attended_transfer_state {
463 * \brief Calling Target state
465 * This state describes the initial state of a transfer. The transferer
466 * waits in the transfer target's bridge for the transfer target to answer.
468 * Superstate: Transfer
471 * 1) Transfer target is RINGING
472 * 2) Transferer is in transferee bridge
473 * 3) Transferee is on hold
475 * Transitions to TRANSFER_CALLING_TARGET:
476 * 1) This is the initial state for an attended transfer.
477 * 2) TRANSFER_HESITANT: Transferer presses DTMF swap sequence
480 * The transferer is moved from the transferee bridge into the transfer
483 * Transitions from TRANSFER_CALLING_TARGET:
484 * 1) TRANSFER_FAIL: Transferee hangs up.
485 * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
486 * and configured atxferdropcall setting is yes.
487 * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
488 * sequence and configured atxferdroppcall setting is no.
489 * 4) TRANSFER_CONSULTING: Transfer target answers the call.
490 * 5) TRANSFER_REBRIDGE: Transfer target hangs up, call to transfer target
491 * times out, or transferer presses DTMF abort sequence.
492 * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
493 * 7) TRANSFER_HESITANT: Transferer presses DTMF swap sequence.
495 TRANSFER_CALLING_TARGET,
497 * \brief Hesitant state
499 * This state only arises if when waiting for the transfer target to
500 * answer, the transferer presses the DTMF swap sequence. This will
501 * cause the transferer to be rebridged with the transferee temporarily.
503 * Superstate: Transfer
506 * 1) Transfer target is in ringing state
507 * 2) Transferer is in transfer target bridge
508 * 3) Transferee is on hold
510 * Transitions to TRANSFER_HESITANT:
511 * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
514 * The transferer is moved from the transfer target bridge into the
515 * transferee bridge, and the transferee is taken off hold.
517 * Transitions from TRANSFER_HESITANT:
518 * 1) TRANSFER_FAIL: Transferee hangs up
519 * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
520 * and configured atxferdropcall setting is yes.
521 * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
522 * sequence and configured atxferdroppcall setting is no.
523 * 4) TRANSFER_DOUBLECHECKING: Transfer target answers the call
524 * 5) TRANSFER_RESUME: Transfer target hangs up, call to transfer target
525 * times out, or transferer presses DTMF abort sequence.
526 * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
527 * 7) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
531 * \brief Rebridge state
533 * This is a terminal state that indicates that the transferer needs
534 * to move back to the transferee's bridge. This is a failed attended
537 * Superstate: Transfer
540 * 1) Transferer is in transfer target bridge
541 * 2) Transferee is on hold
543 * Transitions to TRANSFER_REBRIDGE:
544 * 1) TRANSFER_CALLING_TARGET: Transfer target hangs up, call to transfer target
545 * times out, or transferer presses DTMF abort sequence.
546 * 2) TRANSFER_STATE_CONSULTING: Transfer target hangs up, or transferer presses
547 * DTMF abort sequence.
550 * The transferer channel is moved from the transfer target bridge to the
551 * transferee bridge. The transferee is taken off hold. A stasis transfer
552 * message is published indicating a failed attended transfer.
554 * Transitions from TRANSFER_REBRIDGE:
559 * \brief Resume state
561 * This is a terminal state that indicates that the party bridged with the
562 * transferee is the final party to be bridged with that transferee. This state
563 * may come about due to a successful recall or due to a failed transfer.
565 * Superstate: Transfer or Recall
568 * In Transfer Superstate:
569 * 1) Transferer is in transferee bridge
570 * 2) Transferee is not on hold
571 * In Recall Superstate:
572 * 1) The recall target is in the transferee bridge
573 * 2) Transferee is not on hold
575 * Transitions to TRANSFER_RESUME:
576 * TRANSFER_HESITANT: Transfer target hangs up, call to transfer target times out,
577 * or transferer presses DTMF abort sequence.
578 * TRANSFER_DOUBLECHECKING: Transfer target hangs up or transferer presses DTMF
580 * TRANSFER_BLOND_NONFINAL: Recall target answers
581 * TRANSFER_RECALLING: Recall target answers
582 * TRANSFER_RETRANSFER: Recall target answers
587 * Transitions from TRANSFER_RESUME:
592 * \brief Threeway state
594 * This state results when the transferer wishes to have all parties involved
595 * in a transfer to be in the same bridge together.
597 * Superstate: Transfer
600 * 1) Transfer target state is either RINGING or UP
601 * 2) Transferer is in either bridge
602 * 3) Transferee is not on hold
604 * Transitions to TRANSFER_THREEWAY:
605 * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF threeway sequence.
606 * 2) TRANSFER_HESITANT: Transferer presses DTMF threeway sequence.
607 * 3) TRANSFER_CONSULTING: Transferer presses DTMF threeway sequence.
608 * 4) TRANSFER_DOUBLECHECKING: Transferer presses DTMF threeway sequence.
611 * The transfer target bridge is merged into the transferee bridge.
613 * Transitions from TRANSFER_THREEWAY:
618 * \brief Consulting state
620 * This state describes the case where the transferer and transfer target
621 * are able to converse in the transfer target's bridge prior to completing
624 * Superstate: Transfer
627 * 1) Transfer target is UP
628 * 2) Transferer is in target bridge
629 * 3) Transferee is on hold
631 * Transitions to TRANSFER_CONSULTING:
632 * 1) TRANSFER_CALLING_TARGET: Transfer target answers.
633 * 2) TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
638 * Transitions from TRANSFER_CONSULTING:
639 * TRANSFER_COMPLETE: Transferer hangs up or transferer presses DTMF complete sequence.
640 * TRANSFER_REBRIDGE: Transfer target hangs up or transferer presses DTMF abort sequence.
641 * TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
642 * TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
646 * \brief Double-checking state
648 * This state describes the case where the transferer and transferee are
649 * able to converse in the transferee's bridge prior to completing the transfer. The
650 * difference between this and TRANSFER_HESITANT is that the transfer target is
653 * Superstate: Transfer
656 * 1) Transfer target is UP and on hold
657 * 2) Transferer is in transferee bridge
658 * 3) Transferee is off hold
660 * Transitions to TRANSFER_DOUBLECHECKING:
661 * 1) TRANSFER_HESITANT: Transfer target answers.
662 * 2) TRANSFER_CONSULTING: Transferer presses DTMF swap sequence.
667 * Transitions from TRANSFER_DOUBLECHECKING:
668 * 1) TRANSFER_FAIL: Transferee hangs up.
669 * 2) TRANSFER_COMPLETE: Transferer hangs up or presses DTMF complete sequence.
670 * 3) TRANSFER_RESUME: Transfer target hangs up or transferer presses DTMF abort sequence.
671 * 4) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
672 * 5) TRANSFER_CONSULTING: Transferer presses the DTMF swap sequence.
674 TRANSFER_DOUBLECHECKING,
676 * \brief Complete state
678 * This is a terminal state where a transferer has successfully completed an attended
679 * transfer. This state's goal is to get the transfer target and transferee into
680 * the same bridge and the transferer off the call.
682 * Superstate: Transfer
685 * 1) Transfer target is UP and off hold.
686 * 2) Transferer is in either bridge.
687 * 3) Transferee is off hold.
689 * Transitions to TRANSFER_COMPLETE:
690 * 1) TRANSFER_CONSULTING: transferer hangs up or presses the DTMF complete sequence.
691 * 2) TRANSFER_DOUBLECHECKING: transferer hangs up or presses the DTMF complete sequence.
694 * The transfer target bridge is merged into the transferee bridge. The transferer
695 * channel is kicked out of the bridges as part of the merge.
698 * 1) Merge the transfer target bridge into the transferee bridge,
699 * excluding the transferer channel from the merge.
700 * 2) Publish a stasis transfer message.
703 * This is a terminal state, so there are no exit operations.
709 * This is a terminal state where a transferer has completed an attended transfer prior
710 * to the transfer target answering. This state is only entered if atxferdropcall
711 * is set to 'yes'. This is considered to be a successful attended transfer.
713 * Superstate: Transfer
716 * 1) Transfer target is RINGING.
717 * 2) Transferer is in either bridge.
718 * 3) Transferee is off hold.
720 * Transitions to TRANSFER_BLOND:
721 * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
722 * atxferdropcall is set to 'yes'.
723 * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
724 * atxferdropcall is set to 'yes'.
727 * The transfer target bridge is merged into the transferee bridge. The transferer
728 * channel is kicked out of the bridges as part of the merge. A stasis transfer
729 * publication is sent indicating a successful transfer.
731 * Transitions from TRANSFER_BLOND:
736 * \brief Blond non-final state
738 * This state is very similar to the TRANSFER_BLOND state, except that
739 * this state is entered when atxferdropcall is set to 'no'. This is the
740 * initial state of the Recall superstate, so state operations mainly involve
741 * moving to the Recall superstate. This means that the transfer target, that
742 * is currently ringing is now known as the recall target.
747 * 1) Recall target is RINGING.
748 * 2) Transferee is off hold.
750 * Transitions to TRANSFER_BLOND_NONFINAL:
751 * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
752 * atxferdropcall is set to 'no'.
753 * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
754 * atxferdropcall is set to 'no'.
757 * The superstate of the attended transfer is changed from Transfer to Recall.
758 * The transfer target bridge is merged into the transferee bridge. The transferer
759 * channel is kicked out of the bridges as part of the merge.
761 * Transitions from TRANSFER_BLOND_NONFINAL:
762 * 1) TRANSFER_FAIL: Transferee hangs up
763 * 2) TRANSFER_RESUME: Recall target answers
764 * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
766 TRANSFER_BLOND_NONFINAL,
768 * \brief Recalling state
770 * This state is entered if the recall target from the TRANSFER_BLOND_NONFINAL
771 * or TRANSFER_RETRANSFER states hangs up or does not answer. The goal of this
772 * state is to call back the original transferer in an attempt to recover the
778 * 1) Recall target is down.
779 * 2) Transferee is off hold.
781 * Transitions to TRANSFER_RECALLING:
782 * 1) TRANSFER_BLOND_NONFINAL: Recall target hangs up or time expires.
783 * 2) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
784 * atxferloopdelay is non-zero.
785 * 3) TRANSFER_WAIT_TO_RECALL: Time expires.
788 * The original transferer becomes the recall target and is called using the Dialing API.
789 * Ringing is indicated to the transferee.
791 * Transitions from TRANSFER_RECALLING:
793 * a) Transferee hangs up.
794 * b) Recall target hangs up or time expires, and number of recall attempts exceeds atxfercallbackretries
795 * 2) TRANSFER_WAIT_TO_RETRANSFER: Recall target hangs up or time expires.
796 * atxferloopdelay is non-zero.
797 * 3) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
798 * atxferloopdelay is zero.
799 * 4) TRANSFER_RESUME: Recall target answers.
803 * \brief Wait to Retransfer state
805 * This state is used simply to give a bit of breathing room between attempting
806 * to call back the original transferer and attempting to call back the original
807 * transfer target. The transferee hears music on hold during this state as an
808 * auditory clue that no one is currently being dialed.
813 * 1) Recall target is down.
814 * 2) Transferee is off hold.
816 * Transitions to TRANSFER_WAIT_TO_RETRANSFER:
817 * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
818 * atxferloopdelay is non-zero.
821 * The transferee is placed on hold.
823 * Transitions from TRANSFER_WAIT_TO_RETRANSFER:
824 * 1) TRANSFER_FAIL: Transferee hangs up.
825 * 2) TRANSFER_RETRANSFER: Time expires.
827 TRANSFER_WAIT_TO_RETRANSFER,
829 * \brief Retransfer state
831 * This state is used in order to attempt to call back the original
832 * transfer target channel from the transfer. The transferee hears
833 * ringing during this state as an auditory cue that a party is being
839 * 1) Recall target is down.
840 * 2) Transferee is off hold.
842 * Transitions to TRANSFER_RETRANSFER:
843 * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
844 * atxferloopdelay is zero.
845 * 2) TRANSFER_WAIT_TO_RETRANSFER: Time expires.
848 * The original transfer target is requested and is set as the recall target.
849 * The recall target is called and placed into the transferee bridge.
851 * Transitions from TRANSFER_RETRANSFER:
852 * 1) TRANSFER_FAIL: Transferee hangs up.
853 * 2) TRANSFER_WAIT_TO_RECALL: Recall target hangs up or time expires.
854 * atxferloopdelay is non-zero.
855 * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
856 * atxferloopdelay is zero.
860 * \brief Wait to recall state
862 * This state is used simply to give a bit of breathing room between attempting
863 * to call back the original transfer target and attempting to call back the
864 * original transferer. The transferee hears music on hold during this state as an
865 * auditory clue that no one is currently being dialed.
870 * 1) Recall target is down.
871 * 2) Transferee is off hold.
873 * Transitions to TRANSFER_WAIT_TO_RECALL:
874 * 1) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
875 * atxferloopdelay is non-zero.
878 * Transferee is placed on hold.
880 * Transitions from TRANSFER_WAIT_TO_RECALL:
881 * 1) TRANSFER_FAIL: Transferee hangs up
882 * 2) TRANSFER_RECALLING: Time expires
884 TRANSFER_WAIT_TO_RECALL,
888 * This state indicates that something occurred during the transfer that
889 * makes a graceful completion impossible. The most common stimulus for this
890 * state is when the transferee hangs up.
892 * Superstate: Transfer and Recall
897 * Transitions to TRANSFER_FAIL:
898 * 1) TRANSFER_CALLING_TARGET: Transferee hangs up.
899 * 2) TRANSFER_HESITANT: Transferee hangs up.
900 * 3) TRANSFER_DOUBLECHECKING: Transferee hangs up.
901 * 4) TRANSFER_BLOND_NONFINAL: Transferee hangs up.
902 * 5) TRANSFER_RECALLING:
903 * a) Transferee hangs up.
904 * b) Recall target hangs up or time expires, and number of
905 * recall attempts exceeds atxfercallbackretries.
906 * 6) TRANSFER_WAIT_TO_RETRANSFER: Transferee hangs up.
907 * 7) TRANSFER_RETRANSFER: Transferee hangs up.
908 * 8) TRANSFER_WAIT_TO_RECALL: Transferee hangs up.
911 * A transfer stasis publication is made indicating a failed transfer.
912 * The transferee bridge is destroyed.
914 * Transitions from TRANSFER_FAIL:
921 * \brief Stimuli that can cause transfer state changes
923 enum attended_transfer_stimulus {
924 /*! No stimulus. This literally can never happen. */
926 /*! All of the transferee channels have been hung up. */
927 STIMULUS_TRANSFEREE_HANGUP,
928 /*! The transferer has hung up. */
929 STIMULUS_TRANSFERER_HANGUP,
930 /*! The transfer target channel has hung up. */
931 STIMULUS_TRANSFER_TARGET_HANGUP,
932 /*! The transfer target channel has answered. */
933 STIMULUS_TRANSFER_TARGET_ANSWER,
934 /*! The recall target channel has hung up. */
935 STIMULUS_RECALL_TARGET_HANGUP,
936 /*! The recall target channel has answered. */
937 STIMULUS_RECALL_TARGET_ANSWER,
938 /*! The current state's timer has expired. */
940 /*! The transferer pressed the abort DTMF sequence. */
941 STIMULUS_DTMF_ATXFER_ABORT,
942 /*! The transferer pressed the complete DTMF sequence. */
943 STIMULUS_DTMF_ATXFER_COMPLETE,
944 /*! The transferer pressed the three-way DTMF sequence. */
945 STIMULUS_DTMF_ATXFER_THREEWAY,
946 /*! The transferer pressed the swap DTMF sequence. */
947 STIMULUS_DTMF_ATXFER_SWAP,
951 * \brief String representations of the various stimuli
953 * Used for debugging purposes
955 const char *stimulus_strs[] = {
956 [STIMULUS_NONE] = "None",
957 [STIMULUS_TRANSFEREE_HANGUP] = "Transferee Hangup",
958 [STIMULUS_TRANSFERER_HANGUP] = "Transferer Hangup",
959 [STIMULUS_TRANSFER_TARGET_HANGUP] = "Transfer Target Hangup",
960 [STIMULUS_TRANSFER_TARGET_ANSWER] = "Transfer Target Answer",
961 [STIMULUS_RECALL_TARGET_HANGUP] = "Recall Target Hangup",
962 [STIMULUS_RECALL_TARGET_ANSWER] = "Recall Target Answer",
963 [STIMULUS_TIMEOUT] = "Timeout",
964 [STIMULUS_DTMF_ATXFER_ABORT] = "DTMF Abort",
965 [STIMULUS_DTMF_ATXFER_COMPLETE] = "DTMF Complete",
966 [STIMULUS_DTMF_ATXFER_THREEWAY] = "DTMF Threeway",
967 [STIMULUS_DTMF_ATXFER_SWAP] = "DTMF Swap",
970 struct stimulus_list {
971 enum attended_transfer_stimulus stimulus;
972 AST_LIST_ENTRY(stimulus_list) next;
976 * \brief Collection of data related to an attended transfer attempt
978 struct attended_transfer_properties {
979 AST_DECLARE_STRING_FIELDS (
980 /*! Extension of transfer target */
981 AST_STRING_FIELD(exten);
982 /*! Context of transfer target */
983 AST_STRING_FIELD(context);
984 /*! Sound to play on failure */
985 AST_STRING_FIELD(failsound);
986 /*! Sound to play when transfer completes */
987 AST_STRING_FIELD(xfersound);
988 /*! The channel technology of the transferer channel */
989 AST_STRING_FIELD(transferer_type);
990 /*! The transferer channel address */
991 AST_STRING_FIELD(transferer_addr);
993 /*! Condition used to synchronize when stimuli are reported to the monitor thread */
995 /*! The bridge where the transferee resides. This bridge is also the bridge that
996 * survives a successful attended transfer.
998 struct ast_bridge *transferee_bridge;
999 /*! The bridge used to place an outbound call to the transfer target. This
1000 * bridge is merged with the transferee_bridge on a successful transfer.
1002 struct ast_bridge *target_bridge;
1003 /*! The party that performs the attended transfer. */
1004 struct ast_channel *transferer;
1005 /*! The local channel dialed to reach the transfer target. */
1006 struct ast_channel *transfer_target;
1007 /*! The party that is currently being recalled. Depending on
1008 * the current state, this may be either the party that originally
1009 * was the transferer or the original transfer target
1011 struct ast_channel *recall_target;
1012 /*! The absolute starting time for running timers */
1013 struct timeval start;
1014 AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
1015 /*! The current state of the attended transfer */
1016 enum attended_transfer_state state;
1017 /*! The current superstate of the attended transfer */
1018 enum attended_transfer_superstate superstate;
1019 /*! Configured atxferdropcall from features.conf */
1021 /*! Configured atxfercallbackretries from features.conf */
1022 int atxfercallbackretries;
1023 /*! Configured atxferloopdelay from features.conf */
1024 int atxferloopdelay;
1025 /*! Configured atxfernoanswertimeout from features.conf */
1026 int atxfernoanswertimeout;
1027 /*! Count of the number of times that recalls have been attempted */
1029 /*! Framehook ID for outbounc call to transfer target or recall target */
1030 int target_framehook_id;
1031 /*! Dial structure used when recalling transferer channel */
1032 struct ast_dial *dial;
1033 /*! The bridging features the transferer has available */
1034 struct ast_flags transferer_features;
1037 static void attended_transfer_properties_destructor(void *obj)
1039 struct attended_transfer_properties *props = obj;
1041 ast_debug(1, "Destroy attended transfer properties %p\n", props);
1043 ao2_cleanup(props->target_bridge);
1044 ao2_cleanup(props->transferee_bridge);
1045 /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
1046 ast_channel_cleanup(props->transferer);
1047 ast_channel_cleanup(props->transfer_target);
1048 ast_channel_cleanup(props->recall_target);
1049 ast_string_field_free_memory(props);
1050 ast_cond_destroy(&props->cond);
1055 * \brief Determine the transfer context to use.
1058 * \param transferer Channel initiating the transfer.
1059 * \param context User supplied context if available. May be NULL.
1061 * \return The context to use for the transfer.
1063 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
1065 if (!ast_strlen_zero(context)) {
1068 context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
1069 if (!ast_strlen_zero(context)) {
1072 context = ast_channel_macrocontext(transferer);
1073 if (!ast_strlen_zero(context)) {
1076 context = ast_channel_context(transferer);
1077 if (!ast_strlen_zero(context)) {
1084 * \brief Allocate and initialize attended transfer properties
1086 * \param transferer The channel performing the attended transfer
1087 * \param context Suggestion for what context the transfer target extension can be found in
1089 * \retval NULL Failure to allocate or initialize
1090 * \retval non-NULL Newly allocated properties
1092 static struct attended_transfer_properties *attended_transfer_properties_alloc(
1093 struct ast_channel *transferer, const char *context)
1095 struct attended_transfer_properties *props;
1099 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
1100 struct ast_flags *transferer_features;
1102 props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
1103 if (!props || ast_string_field_init(props, 64)) {
1107 ast_cond_init(&props->cond, NULL);
1109 props->target_framehook_id = -1;
1110 props->transferer = ast_channel_ref(transferer);
1112 ast_channel_lock(props->transferer);
1113 xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
1115 ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
1119 transferer_features = ast_bridge_features_ds_get(props->transferer);
1120 if (transferer_features) {
1121 props->transferer_features = *transferer_features;
1123 props->atxferdropcall = xfer_cfg->atxferdropcall;
1124 props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
1125 props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
1126 props->atxferloopdelay = xfer_cfg->atxferloopdelay;
1127 ast_string_field_set(props, context, get_transfer_context(transferer, context));
1128 ast_string_field_set(props, failsound, xfer_cfg->xferfailsound);
1129 ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
1131 tech = ast_strdupa(ast_channel_name(props->transferer));
1132 addr = strchr(tech, '/');
1134 ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
1135 ast_channel_unref(props->transferer);
1139 serial = strrchr(addr, '-');
1143 ast_string_field_set(props, transferer_type, tech);
1144 ast_string_field_set(props, transferer_addr, addr);
1146 ast_channel_unlock(props->transferer);
1148 ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
1149 props, ast_channel_name(props->transferer));
1154 * \brief Free backlog of stimuli in the queue
1156 static void clear_stimulus_queue(struct attended_transfer_properties *props)
1158 struct stimulus_list *list;
1159 SCOPED_AO2LOCK(lock, props);
1161 while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
1167 * \brief Initiate shutdown of attended transfer properties
1169 * Calling this indicates that the attended transfer properties are no longer needed
1170 * because the transfer operation has concluded.
1172 static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
1174 ast_debug(1, "Shutting down attended transfer %p\n", props);
1176 if (props->transferee_bridge) {
1177 bridge_basic_change_personality(props->transferee_bridge,
1178 BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
1179 ast_bridge_merge_inhibit(props->transferee_bridge, -1);
1182 if (props->target_bridge) {
1183 ast_bridge_destroy(props->target_bridge);
1184 props->target_bridge = NULL;
1187 if (props->transferer) {
1188 ast_channel_remove_bridge_role(props->transferer, TRANSFERER_ROLE_NAME);
1191 clear_stimulus_queue(props);
1196 static void stimulate_attended_transfer(struct attended_transfer_properties *props,
1197 enum attended_transfer_stimulus stimulus)
1199 struct stimulus_list *list;
1201 list = ast_calloc(1, sizeof(*list));
1203 ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
1207 list->stimulus = stimulus;
1209 AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
1210 ast_cond_signal(&props->cond);
1215 * \brief Send a stasis publication for a successful attended transfer
1217 static void publish_transfer_success(struct attended_transfer_properties *props)
1219 struct ast_bridge_channel_pair transferee = {
1220 .channel = props->transferer,
1221 .bridge = props->transferee_bridge,
1223 struct ast_bridge_channel_pair transfer_target = {
1224 .channel = props->transferer,
1225 .bridge = props->target_bridge,
1228 ast_bridge_publish_attended_transfer_bridge_merge(0, AST_BRIDGE_TRANSFER_SUCCESS,
1229 &transferee, &transfer_target, props->transferee_bridge);
1233 * \brief Send a stasis publication for an attended transfer that ends in a threeway call
1235 static void publish_transfer_threeway(struct attended_transfer_properties *props)
1237 struct ast_bridge_channel_pair transferee = {
1238 .channel = props->transferer,
1239 .bridge = props->transferee_bridge,
1241 struct ast_bridge_channel_pair transfer_target = {
1242 .channel = props->transferer,
1243 .bridge = props->target_bridge,
1245 struct ast_bridge_channel_pair threeway = {
1246 .channel = props->transferer,
1247 .bridge = props->transferee_bridge,
1250 ast_bridge_publish_attended_transfer_threeway(0, AST_BRIDGE_TRANSFER_SUCCESS,
1251 &transferee, &transfer_target, &threeway);
1255 * \brief Send a stasis publication for a failed attended transfer
1257 static void publish_transfer_fail(struct attended_transfer_properties *props)
1259 struct ast_bridge_channel_pair transferee = {
1260 .channel = props->transferer,
1261 .bridge = props->transferee_bridge,
1263 struct ast_bridge_channel_pair transfer_target = {
1264 .channel = props->transferer,
1265 .bridge = props->target_bridge,
1268 ast_bridge_publish_attended_transfer_fail(0, AST_BRIDGE_TRANSFER_FAIL,
1269 &transferee, &transfer_target);
1273 * \brief Helper method to play a sound on a channel in a bridge
1275 * \param chan The channel to play the sound to
1276 * \param sound The sound to play
1278 static void play_sound(struct ast_channel *chan, const char *sound)
1280 RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1282 ast_channel_lock(chan);
1283 bridge_channel = ast_channel_get_bridge_channel(chan);
1284 ast_channel_unlock(chan);
1286 if (!bridge_channel) {
1290 ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
1294 * \brief Helper method to place a channel in a bridge on hold
1296 static void hold(struct ast_channel *chan)
1298 RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1301 ast_channel_lock(chan);
1302 bridge_channel = ast_channel_get_bridge_channel(chan);
1303 ast_channel_unlock(chan);
1305 ast_assert(bridge_channel != NULL);
1307 ast_bridge_channel_write_hold(bridge_channel, NULL);
1312 * \brief Helper method to take a channel in a bridge off hold
1314 static void unhold(struct ast_channel *chan)
1316 RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1318 ast_channel_lock(chan);
1319 bridge_channel = ast_channel_get_bridge_channel(chan);
1320 ast_channel_unlock(chan);
1322 ast_assert(bridge_channel != NULL);
1324 ast_bridge_channel_write_unhold(bridge_channel);
1328 * \brief Helper method to send a ringing indication to a channel in a bridge
1330 static void ringing(struct ast_channel *chan)
1332 RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1334 ast_channel_lock(chan);
1335 bridge_channel = ast_channel_get_bridge_channel(chan);
1336 ast_channel_unlock(chan);
1338 ast_assert(bridge_channel != NULL);
1340 ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
1344 * \brief Helper method to send a ringing indication to all channels in a bridge
1346 static void bridge_ringing(struct ast_bridge *bridge)
1348 struct ast_frame ringing = {
1349 .frametype = AST_FRAME_CONTROL,
1350 .subclass.integer = AST_CONTROL_RINGING,
1353 ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
1357 * \brief Helper method to send a hold frame to all channels in a bridge
1359 static void bridge_hold(struct ast_bridge *bridge)
1361 struct ast_frame hold = {
1362 .frametype = AST_FRAME_CONTROL,
1363 .subclass.integer = AST_CONTROL_HOLD,
1366 ast_bridge_queue_everyone_else(bridge, NULL, &hold);
1370 * \brief Helper method to send an unhold frame to all channels in a bridge
1372 static void bridge_unhold(struct ast_bridge *bridge)
1374 struct ast_frame unhold = {
1375 .frametype = AST_FRAME_CONTROL,
1376 .subclass.integer = AST_CONTROL_UNHOLD,
1379 ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
1383 * \brief Wrapper for \ref bridge_do_move
1385 static int bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
1388 RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1390 ast_bridge_lock_both(src, dest);
1392 ast_channel_lock(channel);
1393 bridge_channel = ast_channel_get_bridge_channel(channel);
1394 ast_channel_unlock(channel);
1396 ast_assert(bridge_channel != NULL);
1398 ao2_lock(bridge_channel);
1399 bridge_channel->swap = swap;
1400 ao2_unlock(bridge_channel);
1402 res = bridge_do_move(dest, bridge_channel, 1, 0);
1404 ast_bridge_unlock(dest);
1405 ast_bridge_unlock(src);
1411 * \brief Wrapper for \ref bridge_do_merge
1413 static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
1415 struct ast_bridge_channel **kick_bridge_channels = num_channels ?
1416 ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
1418 int num_bridge_channels = 0;
1420 ast_bridge_lock_both(dest, src);
1422 for (i = 0; i < num_channels; ++i) {
1423 struct ast_bridge_channel *kick_bridge_channel;
1425 kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
1426 if (!kick_bridge_channel) {
1427 kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
1430 /* It's possible (and fine) for the bridge channel to be NULL at this point if the
1431 * channel has hung up already. If that happens, we can just remove it from the list
1432 * of bridge channels to kick from the bridge
1434 if (!kick_bridge_channel) {
1438 kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
1441 bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
1442 ast_bridge_unlock(dest);
1443 ast_bridge_unlock(src);
1447 * \brief Flags that indicate properties of attended transfer states
1449 enum attended_transfer_state_flags {
1450 /*! This state requires that the timer be reset when entering the state */
1451 TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
1452 /*! This state's timer uses atxferloopdelay */
1453 TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
1454 /*! This state's timer uses atxfernoanswertimeout */
1455 TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
1456 /*! This state has a time limit associated with it */
1457 TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
1458 TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
1459 /*! This state does not transition to any other states */
1460 TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
1463 static int calling_target_enter(struct attended_transfer_properties *props);
1464 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
1465 enum attended_transfer_stimulus stimulus);
1467 static int hesitant_enter(struct attended_transfer_properties *props);
1468 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
1469 enum attended_transfer_stimulus stimulus);
1471 static int rebridge_enter(struct attended_transfer_properties *props);
1473 static int resume_enter(struct attended_transfer_properties *props);
1475 static int threeway_enter(struct attended_transfer_properties *props);
1477 static int consulting_enter(struct attended_transfer_properties *props);
1478 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
1479 enum attended_transfer_stimulus stimulus);
1481 static int double_checking_enter(struct attended_transfer_properties *props);
1482 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
1483 enum attended_transfer_stimulus stimulus);
1485 static int complete_enter(struct attended_transfer_properties *props);
1487 static int blond_enter(struct attended_transfer_properties *props);
1489 static int blond_nonfinal_enter(struct attended_transfer_properties *props);
1490 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
1491 enum attended_transfer_stimulus stimulus);
1493 static int recalling_enter(struct attended_transfer_properties *props);
1494 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
1495 enum attended_transfer_stimulus stimulus);
1497 static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
1498 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
1499 enum attended_transfer_stimulus stimulus);
1501 static int retransfer_enter(struct attended_transfer_properties *props);
1502 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
1503 enum attended_transfer_stimulus stimulus);
1505 static int wait_to_recall_enter(struct attended_transfer_properties *props);
1506 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
1507 enum attended_transfer_stimulus stimulus);
1509 static int fail_enter(struct attended_transfer_properties *props);
1512 * \brief Properties of an attended transfer state
1514 struct attended_transfer_state_properties {
1515 /*! The name of the state. Used for debugging */
1516 const char *state_name;
1517 /*! Function used to enter a state */
1518 int (*enter)(struct attended_transfer_properties *props);
1520 * Function used to exit a state
1521 * This is used both to determine what the next state
1522 * to transition to will be and to perform any cleanup
1523 * necessary before exiting the current state.
1525 enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
1526 enum attended_transfer_stimulus stimulus);
1527 /*! Flags associated with this state */
1528 enum attended_transfer_state_flags flags;
1531 static const struct attended_transfer_state_properties state_properties[] = {
1532 [TRANSFER_CALLING_TARGET] = {
1533 .state_name = "Calling Target",
1534 .enter = calling_target_enter,
1535 .exit = calling_target_exit,
1536 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1538 [TRANSFER_HESITANT] = {
1539 .state_name = "Hesitant",
1540 .enter = hesitant_enter,
1541 .exit = hesitant_exit,
1542 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
1544 [TRANSFER_REBRIDGE] = {
1545 .state_name = "Rebridge",
1546 .enter = rebridge_enter,
1547 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1549 [TRANSFER_RESUME] = {
1550 .state_name = "Resume",
1551 .enter = resume_enter,
1552 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1554 [TRANSFER_THREEWAY] = {
1555 .state_name = "Threeway",
1556 .enter = threeway_enter,
1557 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1559 [TRANSFER_CONSULTING] = {
1560 .state_name = "Consulting",
1561 .enter = consulting_enter,
1562 .exit = consulting_exit,
1564 [TRANSFER_DOUBLECHECKING] = {
1565 .state_name = "Double Checking",
1566 .enter = double_checking_enter,
1567 .exit = double_checking_exit,
1569 [TRANSFER_COMPLETE] = {
1570 .state_name = "Complete",
1571 .enter = complete_enter,
1572 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1574 [TRANSFER_BLOND] = {
1575 .state_name = "Blond",
1576 .enter = blond_enter,
1577 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1579 [TRANSFER_BLOND_NONFINAL] = {
1580 .state_name = "Blond Non-Final",
1581 .enter = blond_nonfinal_enter,
1582 .exit = blond_nonfinal_exit,
1583 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
1585 [TRANSFER_RECALLING] = {
1586 .state_name = "Recalling",
1587 .enter = recalling_enter,
1588 .exit = recalling_exit,
1589 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1591 [TRANSFER_WAIT_TO_RETRANSFER] = {
1592 .state_name = "Wait to Retransfer",
1593 .enter = wait_to_retransfer_enter,
1594 .exit = wait_to_retransfer_exit,
1595 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
1597 [TRANSFER_RETRANSFER] = {
1598 .state_name = "Retransfer",
1599 .enter = retransfer_enter,
1600 .exit = retransfer_exit,
1601 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1603 [TRANSFER_WAIT_TO_RECALL] = {
1604 .state_name = "Wait to Recall",
1605 .enter = wait_to_recall_enter,
1606 .exit = wait_to_recall_exit,
1607 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
1610 .state_name = "Fail",
1611 .enter = fail_enter,
1612 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1616 static int calling_target_enter(struct attended_transfer_properties *props)
1618 return bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
1621 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
1622 enum attended_transfer_stimulus stimulus)
1625 case STIMULUS_TRANSFEREE_HANGUP:
1626 play_sound(props->transferer, props->failsound);
1627 publish_transfer_fail(props);
1628 return TRANSFER_FAIL;
1629 case STIMULUS_DTMF_ATXFER_COMPLETE:
1630 case STIMULUS_TRANSFERER_HANGUP:
1631 bridge_unhold(props->transferee_bridge);
1632 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
1633 case STIMULUS_TRANSFER_TARGET_ANSWER:
1634 return TRANSFER_CONSULTING;
1635 case STIMULUS_TRANSFER_TARGET_HANGUP:
1636 case STIMULUS_TIMEOUT:
1637 case STIMULUS_DTMF_ATXFER_ABORT:
1638 play_sound(props->transferer, props->failsound);
1639 return TRANSFER_REBRIDGE;
1640 case STIMULUS_DTMF_ATXFER_THREEWAY:
1641 bridge_unhold(props->transferee_bridge);
1642 return TRANSFER_THREEWAY;
1643 case STIMULUS_DTMF_ATXFER_SWAP:
1644 return TRANSFER_HESITANT;
1646 case STIMULUS_RECALL_TARGET_ANSWER:
1647 case STIMULUS_RECALL_TARGET_HANGUP:
1649 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1650 stimulus_strs[stimulus], state_properties[props->state].state_name);
1651 return props->state;
1655 static int hesitant_enter(struct attended_transfer_properties *props)
1657 if (bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL)) {
1661 unhold(props->transferer);
1665 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
1666 enum attended_transfer_stimulus stimulus)
1669 case STIMULUS_TRANSFEREE_HANGUP:
1670 play_sound(props->transferer, props->failsound);
1671 publish_transfer_fail(props);
1672 return TRANSFER_FAIL;
1673 case STIMULUS_DTMF_ATXFER_COMPLETE:
1674 case STIMULUS_TRANSFERER_HANGUP:
1675 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
1676 case STIMULUS_TRANSFER_TARGET_ANSWER:
1677 return TRANSFER_DOUBLECHECKING;
1678 case STIMULUS_TRANSFER_TARGET_HANGUP:
1679 case STIMULUS_TIMEOUT:
1680 case STIMULUS_DTMF_ATXFER_ABORT:
1681 play_sound(props->transferer, props->failsound);
1682 return TRANSFER_RESUME;
1683 case STIMULUS_DTMF_ATXFER_THREEWAY:
1684 return TRANSFER_THREEWAY;
1685 case STIMULUS_DTMF_ATXFER_SWAP:
1686 hold(props->transferer);
1687 return TRANSFER_CALLING_TARGET;
1689 case STIMULUS_RECALL_TARGET_HANGUP:
1690 case STIMULUS_RECALL_TARGET_ANSWER:
1692 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1693 stimulus_strs[stimulus], state_properties[props->state].state_name);
1694 return props->state;
1698 static int rebridge_enter(struct attended_transfer_properties *props)
1700 if (bridge_move(props->transferee_bridge, props->target_bridge,
1701 props->transferer, NULL)) {
1705 unhold(props->transferer);
1709 static int resume_enter(struct attended_transfer_properties *props)
1714 static int threeway_enter(struct attended_transfer_properties *props)
1716 bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
1717 play_sound(props->transfer_target, props->xfersound);
1718 play_sound(props->transferer, props->xfersound);
1719 publish_transfer_threeway(props);
1724 static int consulting_enter(struct attended_transfer_properties *props)
1729 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
1730 enum attended_transfer_stimulus stimulus)
1733 case STIMULUS_TRANSFEREE_HANGUP:
1734 /* This is a one-of-a-kind event. The transferer and transfer target are talking in
1735 * one bridge, and the transferee has hung up in a separate bridge. In this case, we
1736 * will change the personality of the transfer target bridge back to normal, and play
1737 * a sound to the transferer to indicate the transferee is gone.
1739 bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
1740 play_sound(props->transferer, props->failsound);
1741 ast_bridge_merge_inhibit(props->target_bridge, -1);
1742 /* These next two lines are here to ensure that our reference to the target bridge
1743 * is cleaned up properly and that the target bridge is not destroyed when the
1744 * monitor thread exits
1746 ao2_ref(props->target_bridge, -1);
1747 props->target_bridge = NULL;
1748 return TRANSFER_FAIL;
1749 case STIMULUS_TRANSFERER_HANGUP:
1750 case STIMULUS_DTMF_ATXFER_COMPLETE:
1751 /* We know the transferer is in the target_bridge, so take the other bridge off hold */
1752 bridge_unhold(props->transferee_bridge);
1753 return TRANSFER_COMPLETE;
1754 case STIMULUS_TRANSFER_TARGET_HANGUP:
1755 case STIMULUS_DTMF_ATXFER_ABORT:
1756 play_sound(props->transferer, props->failsound);
1757 return TRANSFER_REBRIDGE;
1758 case STIMULUS_DTMF_ATXFER_THREEWAY:
1759 bridge_unhold(props->transferee_bridge);
1760 return TRANSFER_THREEWAY;
1761 case STIMULUS_DTMF_ATXFER_SWAP:
1762 hold(props->transferer);
1763 bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
1764 unhold(props->transferer);
1765 return TRANSFER_DOUBLECHECKING;
1767 case STIMULUS_TIMEOUT:
1768 case STIMULUS_TRANSFER_TARGET_ANSWER:
1769 case STIMULUS_RECALL_TARGET_HANGUP:
1770 case STIMULUS_RECALL_TARGET_ANSWER:
1772 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1773 stimulus_strs[stimulus], state_properties[props->state].state_name);
1774 return props->state;
1778 static int double_checking_enter(struct attended_transfer_properties *props)
1783 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
1784 enum attended_transfer_stimulus stimulus)
1787 case STIMULUS_TRANSFEREE_HANGUP:
1788 play_sound(props->transferer, props->failsound);
1789 publish_transfer_fail(props);
1790 return TRANSFER_FAIL;
1791 case STIMULUS_TRANSFERER_HANGUP:
1792 case STIMULUS_DTMF_ATXFER_COMPLETE:
1793 /* We know the transferer is in the transferee, so take the other bridge off hold */
1794 bridge_unhold(props->target_bridge);
1795 return TRANSFER_COMPLETE;
1796 case STIMULUS_TRANSFER_TARGET_HANGUP:
1797 case STIMULUS_DTMF_ATXFER_ABORT:
1798 play_sound(props->transferer, props->failsound);
1799 return TRANSFER_RESUME;
1800 case STIMULUS_DTMF_ATXFER_THREEWAY:
1801 bridge_unhold(props->target_bridge);
1802 return TRANSFER_THREEWAY;
1803 case STIMULUS_DTMF_ATXFER_SWAP:
1804 hold(props->transferer);
1805 bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
1806 unhold(props->transferer);
1807 return TRANSFER_CONSULTING;
1809 case STIMULUS_TIMEOUT:
1810 case STIMULUS_TRANSFER_TARGET_ANSWER:
1811 case STIMULUS_RECALL_TARGET_HANGUP:
1812 case STIMULUS_RECALL_TARGET_ANSWER:
1814 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1815 stimulus_strs[stimulus], state_properties[props->state].state_name);
1816 return props->state;
1820 static int complete_enter(struct attended_transfer_properties *props)
1822 bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
1823 play_sound(props->transfer_target, props->xfersound);
1824 publish_transfer_success(props);
1828 static int blond_enter(struct attended_transfer_properties *props)
1830 bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
1831 ringing(props->transfer_target);
1832 publish_transfer_success(props);
1836 static int blond_nonfinal_enter(struct attended_transfer_properties *props)
1839 props->superstate = SUPERSTATE_RECALL;
1840 props->recall_target = ast_channel_ref(props->transfer_target);
1841 res = blond_enter(props);
1842 props->transfer_target = ast_channel_unref(props->transfer_target);
1846 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
1847 enum attended_transfer_stimulus stimulus)
1850 case STIMULUS_TRANSFEREE_HANGUP:
1851 return TRANSFER_FAIL;
1852 case STIMULUS_RECALL_TARGET_ANSWER:
1853 return TRANSFER_RESUME;
1854 case STIMULUS_TIMEOUT:
1855 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
1856 props->recall_target = ast_channel_unref(props->recall_target);
1857 case STIMULUS_RECALL_TARGET_HANGUP:
1858 return TRANSFER_RECALLING;
1860 case STIMULUS_DTMF_ATXFER_ABORT:
1861 case STIMULUS_DTMF_ATXFER_COMPLETE:
1862 case STIMULUS_DTMF_ATXFER_THREEWAY:
1863 case STIMULUS_DTMF_ATXFER_SWAP:
1864 case STIMULUS_TRANSFERER_HANGUP:
1865 case STIMULUS_TRANSFER_TARGET_HANGUP:
1866 case STIMULUS_TRANSFER_TARGET_ANSWER:
1868 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1869 stimulus_strs[stimulus], state_properties[props->state].state_name);
1870 return props->state;
1875 * \brief Dial callback when attempting to recall the original transferer channel
1877 * This is how we can monitor if the recall target has answered or has hung up.
1878 * If one of the two is detected, then an appropriate stimulus is sent to the
1879 * attended transfer monitor thread.
1881 static void recall_callback(struct ast_dial *dial)
1883 struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
1885 switch (ast_dial_state(dial)) {
1887 case AST_DIAL_RESULT_INVALID:
1888 case AST_DIAL_RESULT_FAILED:
1889 case AST_DIAL_RESULT_TIMEOUT:
1890 case AST_DIAL_RESULT_HANGUP:
1891 case AST_DIAL_RESULT_UNANSWERED:
1893 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
1895 case AST_DIAL_RESULT_RINGING:
1896 case AST_DIAL_RESULT_PROGRESS:
1897 case AST_DIAL_RESULT_PROCEEDING:
1898 case AST_DIAL_RESULT_TRYING:
1899 /* Don't care about these cases */
1901 case AST_DIAL_RESULT_ANSWERED:
1902 /* We struck gold! */
1903 props->recall_target = ast_dial_answered_steal(dial);
1904 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
1910 static int recalling_enter(struct attended_transfer_properties *props)
1912 RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
1913 struct ast_format fmt;
1919 ast_format_cap_add(cap, ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0));
1921 /* When we dial the transfer target, since we are communicating
1922 * with a local channel, we can place the local channel in a bridge
1923 * and then call out to it. When recalling the transferer, though, we
1924 * have to use the dialing API because the channel is not local.
1926 props->dial = ast_dial_create();
1931 if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr)) {
1935 if (ast_dial_prerun(props->dial, NULL, cap)) {
1939 ast_dial_set_state_callback(props->dial, &recall_callback);
1942 ast_dial_set_user_data(props->dial, props);
1944 if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
1949 bridge_ringing(props->transferee_bridge);
1953 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
1954 enum attended_transfer_stimulus stimulus)
1956 /* No matter what the outcome was, we need to kill off the dial */
1957 ast_dial_join(props->dial);
1958 ast_dial_destroy(props->dial);
1960 /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
1964 case STIMULUS_TRANSFEREE_HANGUP:
1965 return TRANSFER_FAIL;
1966 case STIMULUS_TIMEOUT:
1967 case STIMULUS_RECALL_TARGET_HANGUP:
1968 ++props->retry_attempts;
1969 if (props->retry_attempts >= props->atxfercallbackretries) {
1970 return TRANSFER_FAIL;
1972 if (props->atxferloopdelay) {
1973 return TRANSFER_WAIT_TO_RETRANSFER;
1975 return TRANSFER_RETRANSFER;
1976 case STIMULUS_RECALL_TARGET_ANSWER:
1977 /* Setting this datastore up will allow the transferer to have all of his
1978 * call features set up automatically when the bridge changes back to a
1979 * normal personality
1981 ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
1982 ast_channel_ref(props->recall_target);
1983 if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL, 1)) {
1984 ast_hangup(props->recall_target);
1985 return TRANSFER_FAIL;
1987 return TRANSFER_RESUME;
1989 case STIMULUS_DTMF_ATXFER_ABORT:
1990 case STIMULUS_DTMF_ATXFER_COMPLETE:
1991 case STIMULUS_DTMF_ATXFER_THREEWAY:
1992 case STIMULUS_DTMF_ATXFER_SWAP:
1993 case STIMULUS_TRANSFER_TARGET_HANGUP:
1994 case STIMULUS_TRANSFER_TARGET_ANSWER:
1995 case STIMULUS_TRANSFERER_HANGUP:
1997 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1998 stimulus_strs[stimulus], state_properties[props->state].state_name);
1999 return props->state;
2003 static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
2005 bridge_hold(props->transferee_bridge);
2009 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
2010 enum attended_transfer_stimulus stimulus)
2012 bridge_unhold(props->transferee_bridge);
2014 case STIMULUS_TRANSFEREE_HANGUP:
2015 return TRANSFER_FAIL;
2016 case STIMULUS_TIMEOUT:
2017 return TRANSFER_RETRANSFER;
2019 case STIMULUS_DTMF_ATXFER_ABORT:
2020 case STIMULUS_DTMF_ATXFER_COMPLETE:
2021 case STIMULUS_DTMF_ATXFER_THREEWAY:
2022 case STIMULUS_DTMF_ATXFER_SWAP:
2023 case STIMULUS_TRANSFER_TARGET_HANGUP:
2024 case STIMULUS_TRANSFER_TARGET_ANSWER:
2025 case STIMULUS_TRANSFERER_HANGUP:
2026 case STIMULUS_RECALL_TARGET_HANGUP:
2027 case STIMULUS_RECALL_TARGET_ANSWER:
2029 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2030 stimulus_strs[stimulus], state_properties[props->state].state_name);
2031 return props->state;
2035 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
2037 static int retransfer_enter(struct attended_transfer_properties *props)
2039 RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
2040 struct ast_format fmt;
2041 char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
2048 snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
2050 ast_format_cap_add(cap, ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0));
2052 /* Get a channel that is the destination we wish to call */
2053 props->recall_target = ast_request("Local", cap, NULL, destination, &cause);
2054 if (!props->recall_target) {
2055 ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
2059 if (attach_framehook(props, props->recall_target)) {
2060 ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
2061 ast_hangup(props->recall_target);
2062 props->recall_target = NULL;
2066 if (ast_call(props->recall_target, destination, 0)) {
2067 ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
2068 ast_hangup(props->recall_target);
2069 props->recall_target = NULL;
2073 ast_channel_ref(props->recall_target);
2074 if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL, 1)) {
2075 ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
2076 ast_hangup(props->recall_target);
2083 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
2084 enum attended_transfer_stimulus stimulus)
2087 case STIMULUS_TRANSFEREE_HANGUP:
2088 return TRANSFER_FAIL;
2089 case STIMULUS_TIMEOUT:
2090 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
2091 case STIMULUS_RECALL_TARGET_HANGUP:
2092 props->recall_target = ast_channel_unref(props->recall_target);
2093 if (props->atxferloopdelay) {
2094 return TRANSFER_WAIT_TO_RECALL;
2096 return TRANSFER_RECALLING;
2097 case STIMULUS_RECALL_TARGET_ANSWER:
2098 return TRANSFER_RESUME;
2100 case STIMULUS_DTMF_ATXFER_ABORT:
2101 case STIMULUS_DTMF_ATXFER_COMPLETE:
2102 case STIMULUS_DTMF_ATXFER_THREEWAY:
2103 case STIMULUS_DTMF_ATXFER_SWAP:
2104 case STIMULUS_TRANSFER_TARGET_HANGUP:
2105 case STIMULUS_TRANSFER_TARGET_ANSWER:
2106 case STIMULUS_TRANSFERER_HANGUP:
2108 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2109 stimulus_strs[stimulus], state_properties[props->state].state_name);
2110 return props->state;
2114 static int wait_to_recall_enter(struct attended_transfer_properties *props)
2116 bridge_hold(props->transferee_bridge);
2120 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
2121 enum attended_transfer_stimulus stimulus)
2123 bridge_unhold(props->transferee_bridge);
2125 case STIMULUS_TRANSFEREE_HANGUP:
2126 return TRANSFER_FAIL;
2127 case STIMULUS_TIMEOUT:
2128 return TRANSFER_RECALLING;
2130 case STIMULUS_DTMF_ATXFER_ABORT:
2131 case STIMULUS_DTMF_ATXFER_COMPLETE:
2132 case STIMULUS_DTMF_ATXFER_THREEWAY:
2133 case STIMULUS_DTMF_ATXFER_SWAP:
2134 case STIMULUS_TRANSFER_TARGET_HANGUP:
2135 case STIMULUS_TRANSFER_TARGET_ANSWER:
2136 case STIMULUS_TRANSFERER_HANGUP:
2137 case STIMULUS_RECALL_TARGET_HANGUP:
2138 case STIMULUS_RECALL_TARGET_ANSWER:
2140 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2141 stimulus_strs[stimulus], state_properties[props->state].state_name);
2142 return props->state;
2146 static int fail_enter(struct attended_transfer_properties *props)
2148 if (props->transferee_bridge) {
2149 ast_bridge_destroy(props->transferee_bridge);
2150 props->transferee_bridge = NULL;
2156 * \brief DTMF hook when transferer presses abort sequence.
2158 * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
2160 static int atxfer_abort(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2162 struct attended_transfer_properties *props = hook_pvt;
2164 ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
2165 stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
2170 * \brief DTMF hook when transferer presses complete sequence.
2172 * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
2174 static int atxfer_complete(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2176 struct attended_transfer_properties *props = hook_pvt;
2178 ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
2179 stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
2184 * \brief DTMF hook when transferer presses threeway sequence.
2186 * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
2188 static int atxfer_threeway(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2190 struct attended_transfer_properties *props = hook_pvt;
2192 ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
2193 stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
2198 * \brief DTMF hook when transferer presses swap sequence.
2200 * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
2202 static int atxfer_swap(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2204 struct attended_transfer_properties *props = hook_pvt;
2206 ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
2207 stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
2212 * \brief Hangup hook for transferer channel.
2214 * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
2216 static int atxfer_transferer_hangup(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2218 struct attended_transfer_properties *props = hook_pvt;
2220 ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
2221 stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
2226 * \brief Frame hook for transfer target channel
2228 * This is used to determine if the transfer target or recall target has answered
2229 * the outgoing call.
2231 * When an answer is detected, a stimulus is sent to the attended transfer monitor
2232 * thread to indicate that the transfer target or recall target has answered.
2234 * \param chan The channel the framehook is attached to.
2235 * \param frame The frame being read or written.
2236 * \param event What is being done with the frame.
2237 * \param data The attended transfer properties.
2239 static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
2240 struct ast_frame *frame, enum ast_framehook_event event, void *data)
2242 struct attended_transfer_properties *props = data;
2244 if (event == AST_FRAMEHOOK_EVENT_READ &&
2245 frame && frame->frametype == AST_FRAME_CONTROL &&
2246 frame->subclass.integer == AST_CONTROL_ANSWER) {
2248 ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
2249 if (props->superstate == SUPERSTATE_TRANSFER) {
2250 stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
2252 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
2254 ast_framehook_detach(chan, props->target_framehook_id);
2255 props->target_framehook_id = -1;
2261 static void transfer_target_framehook_destroy_cb(void *data)
2263 struct attended_transfer_properties *props = data;
2267 static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
2269 const char *abort_dtmf;
2270 const char *complete_dtmf;
2271 const char *threeway_dtmf;
2272 const char *swap_dtmf;
2273 struct bridge_basic_personality *personality = self->personality;
2275 if (!ast_channel_has_role(bridge_channel->chan, TRANSFERER_ROLE_NAME)) {
2279 abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "abort");
2280 complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "complete");
2281 threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "threeway");
2282 swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "swap");
2284 if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2285 abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
2286 AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2289 if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2290 complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
2291 AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2294 if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2295 threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
2296 AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2299 if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2300 swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
2301 AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2304 if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
2305 personality->details[personality->current].pvt, NULL,
2306 AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2313 static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2315 if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2319 if (self->num_channels == 1) {
2320 RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
2322 ast_channel_lock(props->transferer);
2323 transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
2324 ast_channel_unlock(props->transferer);
2326 if (!transferer_bridge_channel) {
2330 if (AST_LIST_FIRST(&self->channels) != transferer_bridge_channel) {
2335 /* Reaching this point means that either
2336 * 1) The bridge has no channels in it
2337 * 2) The bridge has one channel, and it's the transferer
2338 * In either case, it indicates that the non-transferer parties
2339 * are no longer in the bridge.
2341 if (self == props->transferee_bridge) {
2342 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2344 stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
2348 static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2350 if (self == props->target_bridge) {
2351 /* Once we're in the recall superstate, we no longer care about this bridge */
2355 if (bridge_channel->chan == props->recall_target) {
2356 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
2360 if (self->num_channels == 0) {
2361 /* Empty bridge means all transferees are gone for sure */
2362 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2366 if (self->num_channels == 1) {
2367 RAII_VAR(struct ast_bridge_channel *, target_bridge_channel, NULL, ao2_cleanup);
2368 if (!props->recall_target) {
2369 /* No recall target means that the pull happened on a transferee. If there's still
2370 * a channel left in the bridge, we don't need to send a stimulus
2375 ast_channel_lock(props->recall_target);
2376 target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
2377 ast_channel_unlock(props->recall_target);
2379 if (!target_bridge_channel) {
2383 if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
2384 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2389 static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
2391 struct bridge_basic_personality *personality = self->personality;
2392 struct attended_transfer_properties *props = personality->details[personality->current].pvt;
2394 switch (props->superstate) {
2395 case SUPERSTATE_TRANSFER:
2396 transfer_pull(self, bridge_channel, props);
2398 case SUPERSTATE_RECALL:
2399 recall_pull(self, bridge_channel, props);
2404 static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
2406 RAII_VAR(struct stimulus_list *, list, NULL, ast_free_ptr);
2407 SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
2409 while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
2410 if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
2411 ast_cond_wait(&props->cond, lock);
2413 struct timeval relative_timeout;
2414 struct timeval absolute_timeout;
2415 struct timespec timeout_arg;
2417 if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
2418 props->start = ast_tvnow();
2421 if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
2422 relative_timeout = ast_samp2tv(props->atxferloopdelay, 1000);
2424 /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
2425 relative_timeout = ast_samp2tv(props->atxfernoanswertimeout, 1000);
2428 absolute_timeout = ast_tvadd(props->start, relative_timeout);
2429 timeout_arg.tv_sec = absolute_timeout.tv_sec;
2430 timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
2432 if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
2433 return STIMULUS_TIMEOUT;
2437 return list->stimulus;
2441 * \brief The main loop for the attended transfer monitor thread.
2443 * This loop runs continuously until the attended transfer reaches
2444 * a terminal state. Stimuli for changes in the attended transfer
2445 * state are handled in this thread so that all factors in an
2446 * attended transfer can be handled in an orderly fashion.
2448 * \param data The attended transfer properties
2450 static void *attended_transfer_monitor_thread(void *data)
2452 struct attended_transfer_properties *props = data;
2455 enum attended_transfer_stimulus stimulus;
2457 ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
2459 if (state_properties[props->state].enter &&
2460 state_properties[props->state].enter(props)) {
2461 ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
2462 state_properties[props->state].state_name, props);
2466 if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
2467 ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
2468 state_properties[props->state].state_name, props);
2472 stimulus = wait_for_stimulus(props);
2474 ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
2476 ast_assert(state_properties[props->state].exit != NULL);
2478 props->state = state_properties[props->state].exit(props, stimulus);
2480 ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
2483 attended_transfer_properties_shutdown(props);
2488 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
2490 struct ast_framehook_interface target_interface = {
2491 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
2492 .event_cb = transfer_target_framehook_cb,
2493 .destroy_cb = transfer_target_framehook_destroy_cb,
2497 target_interface.data = props;
2499 props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
2500 if (props->target_framehook_id == -1) {
2507 static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
2509 const char *atxfer_abort;
2510 const char *atxfer_threeway;
2511 const char *atxfer_complete;
2512 const char *atxfer_swap;
2513 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
2514 SCOPED_CHANNELLOCK(lock, chan);
2516 xfer_cfg = ast_get_chan_features_xfer_config(chan);
2520 if (attended_transfer) {
2521 atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
2522 atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
2523 atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
2524 atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
2526 atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
2527 atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
2528 atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
2529 atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
2532 return ast_channel_add_bridge_role(chan, TRANSFERER_ROLE_NAME) ||
2533 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
2534 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
2535 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
2536 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
2540 * \brief Helper function that presents dialtone and grabs extension
2542 * \retval 0 on success
2543 * \retval -1 on failure
2545 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
2549 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
2551 ast_channel_lock(chan);
2552 xfer_cfg = ast_get_chan_features_xfer_config(chan);
2554 ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
2555 ast_channel_unlock(chan);
2558 digit_timeout = xfer_cfg->transferdigittimeout;
2559 ast_channel_unlock(chan);
2561 /* Play the simple "transfer" prompt out and wait */
2562 res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
2563 ast_stopstream(chan);
2565 /* Hangup or error */
2569 /* Store the DTMF digit that interrupted playback of the file. */
2573 /* Drop to dialtone so they can enter the extension they want to transfer to */
2574 res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
2576 /* Hangup or error */
2579 /* 0 for invalid extension dialed. */
2580 if (ast_strlen_zero(exten)) {
2581 ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
2583 ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
2584 ast_channel_name(chan), exten, context);
2586 ast_stream_and_wait(chan, "pbx-invalid", AST_DIGIT_NONE);
2589 /* Dialed extension is valid. */
2595 static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
2597 ast_channel_lock_both(caller, dest);
2598 ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
2599 ast_channel_inherit_variables(caller, dest);
2600 ast_channel_datastore_inherit(caller, dest);
2601 ast_channel_unlock(dest);
2602 ast_channel_unlock(caller);
2605 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
2606 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
2608 struct ast_channel *chan;
2611 /* Now we request a local channel to prepare to call the destination */
2612 chan = ast_request("Local", ast_channel_nativeformats(caller), caller, destination,
2618 /* Who is transferring the call. */
2619 pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
2621 /* To work as an analog to BLINDTRANSFER */
2622 pbx_builtin_setvar_helper(chan, "ATTENDEDTRANSFER", ast_channel_name(caller));
2624 /* Before we actually dial out let's inherit appropriate information. */
2625 copy_caller_data(chan, caller);
2631 * \brief Internal built in feature for attended transfers
2633 * This hook will set up a thread for monitoring the progress of
2634 * an attended transfer. For more information about attended transfer
2635 * progress, see documentation on the transfer state machine.
2637 * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
2638 * \param hook_pvt Structure with further information about the attended transfer
2640 static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2642 struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
2643 struct attended_transfer_properties *props;
2644 struct ast_bridge *bridge;
2645 char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
2646 char exten[AST_MAX_EXTENSION] = "";
2649 /* Inhibit the bridge before we do anything else. */
2650 bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
2652 if (strcmp(bridge->v_table->name, "basic")) {
2653 ast_log(LOG_ERROR, "Attended transfer attempted on unsupported bridge type '%s'.\n",
2654 bridge->v_table->name);
2655 ast_bridge_merge_inhibit(bridge, -1);
2656 ao2_ref(bridge, -1);
2660 /* Was the bridge inhibited before we inhibited it? */
2661 if (1 < bridge->inhibit_merge) {
2663 * The peer likely initiated attended transfer at the same time
2664 * and we lost the race.
2666 ast_verb(3, "Channel %s: Bridge '%s' does not permit merging at this time.\n",
2667 ast_channel_name(bridge_channel->chan), bridge->uniqueid);
2668 ast_bridge_merge_inhibit(bridge, -1);
2669 ao2_ref(bridge, -1);
2673 props = attended_transfer_properties_alloc(bridge_channel->chan,
2674 attended_transfer ? attended_transfer->context : NULL);
2676 ast_log(LOG_ERROR, "Unable to allocate control structure for performing attended transfer.\n");
2677 ast_bridge_merge_inhibit(bridge, -1);
2678 ao2_ref(bridge, -1);
2682 props->transferee_bridge = bridge;
2684 if (add_transferer_role(props->transferer, attended_transfer)) {
2685 ast_log(LOG_ERROR, "Unable to set transferrer bridge role.\n");
2686 attended_transfer_properties_shutdown(props);
2690 ast_bridge_channel_write_hold(bridge_channel, NULL);
2692 /* Grab the extension to transfer to */
2693 if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
2694 ast_log(LOG_WARNING, "Unable to acquire target extension for attended transfer.\n");
2695 ast_bridge_channel_write_unhold(bridge_channel);
2696 attended_transfer_properties_shutdown(props);
2700 ast_string_field_set(props, exten, exten);
2702 /* Fill the variable with the extension and context we want to call */
2703 snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
2705 ast_debug(1, "Attended transfer to '%s'\n", destination);
2707 /* Get a channel that is the destination we wish to call */
2708 props->transfer_target = dial_transfer(bridge_channel->chan, destination);
2709 if (!props->transfer_target) {
2710 ast_log(LOG_ERROR, "Unable to request outbound channel for attended transfer target.\n");
2711 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
2712 ast_bridge_channel_write_unhold(bridge_channel);
2713 attended_transfer_properties_shutdown(props);
2718 /* Create a bridge to use to talk to the person we are calling */
2719 props->target_bridge = ast_bridge_basic_new();
2720 if (!props->target_bridge) {
2721 ast_log(LOG_ERROR, "Unable to create bridge for attended transfer target.\n");
2722 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
2723 ast_bridge_channel_write_unhold(bridge_channel);
2724 ast_hangup(props->transfer_target);
2725 props->transfer_target = NULL;
2726 attended_transfer_properties_shutdown(props);
2729 ast_bridge_merge_inhibit(props->target_bridge, +1);
2731 if (attach_framehook(props, props->transfer_target)) {
2732 ast_log(LOG_ERROR, "Unable to attach framehook to transfer target.\n");
2733 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
2734 ast_bridge_channel_write_unhold(bridge_channel);
2735 ast_hangup(props->transfer_target);
2736 props->transfer_target = NULL;
2737 attended_transfer_properties_shutdown(props);
2741 bridge_basic_change_personality(props->target_bridge,
2742 BRIDGE_BASIC_PERSONALITY_ATXFER, props);
2743 bridge_basic_change_personality(bridge,
2744 BRIDGE_BASIC_PERSONALITY_ATXFER, props);
2746 if (ast_call(props->transfer_target, destination, 0)) {
2747 ast_log(LOG_ERROR, "Unable to place outbound call to transfer target.\n");
2748 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
2749 ast_bridge_channel_write_unhold(bridge_channel);
2750 ast_hangup(props->transfer_target);
2751 props->transfer_target = NULL;
2752 attended_transfer_properties_shutdown(props);
2756 /* We increase the refcount of the transfer target because ast_bridge_impart() will
2757 * steal the reference we already have. We need to keep a reference, so the only
2758 * choice is to give it a bump
2760 ast_channel_ref(props->transfer_target);
2761 if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL, 1)) {
2762 ast_log(LOG_ERROR, "Unable to place transfer target into bridge.\n");
2763 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
2764 ast_bridge_channel_write_unhold(bridge_channel);
2765 ast_hangup(props->transfer_target);
2766 props->transfer_target = NULL;
2767 attended_transfer_properties_shutdown(props);
2771 if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
2772 ast_log(LOG_ERROR, "Unable to create monitoring thread for attended transfer.\n");
2773 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
2774 ast_bridge_channel_write_unhold(bridge_channel);
2775 attended_transfer_properties_shutdown(props);
2779 /* Once the monitoring thread has been created, it is responsible for destroying all
2780 * of the necessary components.
2785 static void blind_transfer_cb(struct ast_channel *new_channel, void *user_data,
2786 enum ast_transfer_type transfer_type)
2788 struct ast_channel *transferer_channel = user_data;
2790 if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
2791 copy_caller_data(new_channel, transferer_channel);
2795 /*! \brief Internal built in feature for blind transfers */
2796 static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2798 char exten[AST_MAX_EXTENSION] = "";
2799 struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
2800 const char *context;
2801 char *goto_on_blindxfr;
2803 ast_bridge_channel_write_hold(bridge_channel, NULL);
2805 ast_channel_lock(bridge_channel->chan);
2806 context = ast_strdupa(get_transfer_context(bridge_channel->chan,
2807 blind_transfer ? blind_transfer->context : NULL));
2808 goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
2809 "GOTO_ON_BLINDXFR"), ""));
2810 ast_channel_unlock(bridge_channel->chan);
2812 /* Grab the extension to transfer to */
2813 if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
2814 ast_bridge_channel_write_unhold(bridge_channel);
2818 if (!ast_strlen_zero(goto_on_blindxfr)) {
2819 ast_debug(1, "After transfer, transferer %s goes to %s\n",
2820 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
2821 ast_bridge_set_after_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
2824 if (ast_bridge_transfer_blind(0, bridge_channel->chan, exten, context, blind_transfer_cb,
2825 bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
2826 !ast_strlen_zero(goto_on_blindxfr)) {
2827 ast_bridge_discard_after_goto(bridge_channel->chan);
2833 struct ast_bridge_methods ast_bridge_basic_v_table;
2834 struct ast_bridge_methods personality_normal_v_table;
2835 struct ast_bridge_methods personality_atxfer_v_table;
2837 static void bridge_basic_change_personality(struct ast_bridge *bridge,
2838 enum bridge_basic_personality_type type, void *user_data)
2840 struct bridge_basic_personality *personality = bridge->personality;
2841 SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
2843 remove_hooks_on_personality_change(bridge);
2845 ao2_cleanup(personality->details[personality->current].pvt);
2846 personality->details[personality->current].pvt = NULL;
2847 ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
2849 personality->current = type;
2851 ao2_ref(user_data, +1);
2853 personality->details[personality->current].pvt = user_data;
2854 ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
2855 if (personality->details[personality->current].on_personality_change) {
2856 personality->details[personality->current].on_personality_change(bridge);
2860 static void personality_destructor(void *obj)
2862 struct bridge_basic_personality *personality = obj;
2865 for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
2866 ao2_cleanup(personality->details[i].pvt);
2870 static void on_personality_change_normal(struct ast_bridge *bridge)
2872 struct ast_bridge_channel *iter;
2874 AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
2875 if (add_normal_hooks(bridge, iter)) {
2876 ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
2877 ast_channel_name(iter->chan));
2882 static void init_details(struct personality_details *details,
2883 enum bridge_basic_personality_type type)
2886 case BRIDGE_BASIC_PERSONALITY_NORMAL:
2887 details->v_table = &personality_normal_v_table;
2888 details->bridge_flags = NORMAL_FLAGS;
2889 details->on_personality_change = on_personality_change_normal;
2891 case BRIDGE_BASIC_PERSONALITY_ATXFER:
2892 details->v_table = &personality_atxfer_v_table;
2893 details->bridge_flags = TRANSFER_FLAGS;
2896 ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
2901 static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
2903 struct bridge_basic_personality *personality;
2910 personality = ao2_alloc(sizeof(*personality), personality_destructor);
2912 ao2_ref(bridge, -1);
2915 for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
2916 init_details(&personality->details[i], i);
2918 personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
2919 bridge->personality = personality;
2924 struct ast_bridge *ast_bridge_basic_new(void)
2926 struct ast_bridge *bridge;
2928 bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
2929 bridge = bridge_base_init(bridge,
2930 AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
2931 | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS);
2932 bridge = bridge_basic_personality_alloc(bridge);
2933 bridge = bridge_register(bridge);
2937 void ast_bridging_init_basic(void)
2939 /* Setup bridge basic subclass v_table. */
2940 ast_bridge_basic_v_table = ast_bridge_base_v_table;
2941 ast_bridge_basic_v_table.name = "basic";
2942 ast_bridge_basic_v_table.push = bridge_basic_push;
2943 ast_bridge_basic_v_table.pull = bridge_basic_pull;
2944 ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
2946 personality_normal_v_table = ast_bridge_base_v_table;
2947 personality_normal_v_table.name = "normal";
2948 personality_normal_v_table.push = bridge_personality_normal_push;
2950 personality_atxfer_v_table = ast_bridge_base_v_table;
2951 personality_atxfer_v_table.name = "attended transfer";
2952 personality_atxfer_v_table.push = bridge_personality_atxfer_push;
2953 personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
2955 ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
2956 ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);