7eaf30eb939e888ec98915ff3b27cc552186cdb0
[asterisk/asterisk.git] / main / bridge_basic.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013 Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief Basic bridge class.  It is a subclass of struct ast_bridge.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *
25  * See Also:
26  * \arg \ref AstCREDITS
27  */
28
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
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"
48
49 #define NORMAL_FLAGS    (AST_BRIDGE_FLAG_DISSOLVE_HANGUP | AST_BRIDGE_FLAG_DISSOLVE_EMPTY \
50                         | AST_BRIDGE_FLAG_SMART)
51
52 #define TRANSFER_FLAGS AST_BRIDGE_FLAG_SMART
53 #define TRANSFERER_ROLE_NAME "transferer"
54
55 struct attended_transfer_properties;
56
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,
64 };
65
66 /*!
67  * \brief Change basic bridge personality
68  *
69  * Changing personalities allows for the bridge to remain in use but have
70  * properties such as its v_table and its flags change.
71  *
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.
75  */
76 static void bridge_basic_change_personality(struct ast_bridge *bridge,
77                 enum bridge_basic_personality_type type, void *user_data);
78
79 /* ------------------------------------------------------------------- */
80
81 static const struct ast_datastore_info dtmf_features_info = {
82         .type = "bridge-dtmf-features",
83         .destroy = ast_free_ptr,
84 };
85
86 /*!
87  * \internal
88  * \since 12.0.0
89  * \brief read a feature code character and set it on for the give feature_flags struct
90  *
91  * \param feature_flags flags being modifed
92  * \param feature feature code provided - should be an uppercase letter
93  *
94  * \retval 0 if the feature was set successfully
95  * \retval -1 failure because the requested feature code isn't handled by this function
96  */
97 static int set_feature_flag_from_char(struct ast_flags *feature_flags, char feature)
98 {
99         switch (feature) {
100         case 'T':
101                 ast_set_flag(feature_flags, AST_FEATURE_REDIRECT);
102                 return 0;
103         case 'K':
104                 ast_set_flag(feature_flags, AST_FEATURE_PARKCALL);
105                 return 0;
106         case 'H':
107                 ast_set_flag(feature_flags, AST_FEATURE_DISCONNECT);
108                 return 0;
109         case 'W':
110                 ast_set_flag(feature_flags, AST_FEATURE_AUTOMON);
111                 return 0;
112         case 'X':
113                 ast_set_flag(feature_flags, AST_FEATURE_AUTOMIXMON);
114                 return 0;
115         default:
116                 return -1;
117         }
118 }
119
120 /*!
121  * \internal
122  * \since 12.0.0
123  * \brief Write a features string to a string buffer based on the feature flags provided
124  *
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)
128  *
129  * \retval 0 on successful write
130  * \retval -1 failure due to running out of buffer space
131  */
132 static int dtmf_features_flags_to_string(struct ast_flags *feature_flags, char *buffer, size_t buffer_size)
133 {
134         size_t buffer_expended = 0;
135         unsigned int cur_feature;
136         static const struct {
137                 char letter;
138                 unsigned int flag;
139         } associations[] = {
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 },
145         };
146
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';
151                                 return -1;
152                         }
153                         buffer[buffer_expended++] = associations[cur_feature].letter;
154                 }
155         }
156
157         buffer[buffer_expended] = '\0';
158         return 0;
159 }
160
161 static int build_dtmf_features(struct ast_flags *flags, const char *features)
162 {
163         const char *feature;
164
165         char missing_features[strlen(features) + 1];
166         size_t number_of_missing_features = 0;
167
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);
171                         return -1;
172                 }
173
174                 if (set_feature_flag_from_char(flags, *feature)) {
175                         missing_features[number_of_missing_features++] = *feature;
176                 }
177         }
178
179         missing_features[number_of_missing_features] = '\0';
180
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);
183         }
184
185         return 0;
186 }
187
188 int ast_bridge_features_ds_set_string(struct ast_channel *chan, const char *features)
189 {
190         struct ast_flags flags = {0};
191
192         if (build_dtmf_features(&flags, features)) {
193                 return -1;
194         }
195
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));
200                 return -1;
201         }
202         ast_channel_unlock(chan);
203
204         return 0;
205 }
206
207 int ast_bridge_features_ds_get_string(struct ast_channel *chan, char *buffer, size_t buf_size)
208 {
209         struct ast_flags *channel_flags;
210         struct ast_flags held_copy;
211
212         ast_channel_lock(chan);
213         if (!(channel_flags = ast_bridge_features_ds_get(chan))) {
214                 ast_channel_unlock(chan);
215                 return -1;
216         }
217         held_copy = *channel_flags;
218         ast_channel_unlock(chan);
219
220         return dtmf_features_flags_to_string(&held_copy, buffer, buf_size);
221 }
222
223 int ast_bridge_features_ds_set(struct ast_channel *chan, struct ast_flags *flags)
224 {
225         struct ast_datastore *datastore;
226         struct ast_flags *ds_flags;
227
228         datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
229         if (datastore) {
230                 ds_flags = datastore->data;
231                 *ds_flags = *flags;
232                 return 0;
233         }
234
235         datastore = ast_datastore_alloc(&dtmf_features_info, NULL);
236         if (!datastore) {
237                 return -1;
238         }
239
240         ds_flags = ast_malloc(sizeof(*ds_flags));
241         if (!ds_flags) {
242                 ast_datastore_free(datastore);
243                 return -1;
244         }
245
246         *ds_flags = *flags;
247         datastore->data = ds_flags;
248         ast_channel_datastore_add(chan, datastore);
249         return 0;
250 }
251
252 struct ast_flags *ast_bridge_features_ds_get(struct ast_channel *chan)
253 {
254         struct ast_datastore *datastore;
255
256         datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
257         if (!datastore) {
258                 return NULL;
259         }
260         return datastore->data;
261 }
262
263 /*!
264  * \internal
265  * \brief Determine if we should dissolve the bridge from a hangup.
266  * \since 12.0.0
267  *
268  * \param bridge The bridge that the channel is part of
269  * \param bridge_channel Channel executing the feature
270  * \param hook_pvt Private data passed in when the hook was created
271  *
272  * \retval 0 Keep the callback hook.
273  * \retval -1 Remove the callback hook.
274  */
275 static int basic_hangup_hook(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
276 {
277         int bridge_count = 0;
278         struct ast_bridge_channel *iter;
279
280         ast_bridge_channel_lock_bridge(bridge_channel);
281         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
282                 if (iter != bridge_channel && iter->state == BRIDGE_CHANNEL_STATE_WAIT) {
283                         ++bridge_count;
284                 }
285         }
286         if (2 <= bridge_count) {
287                 /* Just allow this channel to leave the multi-party bridge. */
288                 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
289         }
290         ast_bridge_unlock(bridge_channel->bridge);
291         return 0;
292 }
293
294 /*!
295  * \brief Details for specific basic bridge personalities
296  */
297 struct personality_details {
298         /*! The v_table to use for this personality */
299         struct ast_bridge_methods *v_table;
300         /*! Flags to set on this type of bridge */
301         unsigned int bridge_flags;
302         /*! User data for this personality. If used, must be an ao2 object */
303         void *pvt;
304         /*! Callback to be called when changing to the personality */
305         void (*on_personality_change)(struct ast_bridge *bridge);
306 };
307
308 /*!
309  * \brief structure that organizes different personalities for basic bridges.
310  */
311 struct bridge_basic_personality {
312         /*! The current bridge personality in use */
313         enum bridge_basic_personality_type current;
314         /*! Array of details for the types of bridge personalities supported */
315         struct personality_details details[BRIDGE_BASIC_PERSONALITY_END];
316 };
317
318 static int add_normal_hooks(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
319 {
320         return ast_bridge_hangup_hook(bridge_channel->features, basic_hangup_hook,
321                         NULL, NULL, AST_BRIDGE_HOOK_REMOVE_ON_PULL)
322                 || ast_bridge_channel_setup_features(bridge_channel);
323 }
324
325 /*!
326  * \internal
327  * \brief ast_bridge basic push method.
328  * \since 12.0.0
329  *
330  * \param self Bridge to operate upon.
331  * \param bridge_channel Bridge channel to push.
332  * \param swap Bridge channel to swap places with if not NULL.
333  *
334  * \note On entry, self is already locked.
335  *
336  * \retval 0 on success
337  * \retval -1 on failure
338  */
339 static int bridge_personality_normal_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
340 {
341         if (add_normal_hooks(self, bridge_channel)) {
342                 return -1;
343         }
344
345         ast_bridge_channel_update_accountcodes(bridge_channel, swap);
346         ast_bridge_channel_update_linkedids(bridge_channel, swap);
347         return 0;
348 }
349
350 static int bridge_basic_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
351 {
352         struct bridge_basic_personality *personality = self->personality;
353
354         ast_assert(personality != NULL);
355
356         if (personality->details[personality->current].v_table->push(self, bridge_channel, swap)) {
357                 return -1;
358         }
359
360         return ast_bridge_base_v_table.push(self, bridge_channel, swap);
361 }
362
363 static void bridge_basic_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
364 {
365         struct bridge_basic_personality *personality = self->personality;
366
367         ast_assert(personality != NULL);
368
369         if (personality->details[personality->current].v_table->pull) {
370                 personality->details[personality->current].v_table->pull(self, bridge_channel);
371         }
372
373         ast_bridge_base_v_table.pull(self, bridge_channel);
374 }
375
376 static void bridge_basic_destroy(struct ast_bridge *self)
377 {
378         struct bridge_basic_personality *personality = self->personality;
379
380         ao2_cleanup(personality);
381
382         ast_bridge_base_v_table.destroy(self);
383 }
384
385 /*!
386  * \brief Remove appropriate hooks when basic bridge personality changes
387  *
388  * Hooks that have the AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE flag
389  * set will be removed from all bridge channels in the bridge.
390  *
391  * \param bridge Basic bridge undergoing personality change
392  */
393 static void remove_hooks_on_personality_change(struct ast_bridge *bridge)
394 {
395         struct ast_bridge_channel *iter;
396
397         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
398                 SCOPED_LOCK(lock, iter, ast_bridge_channel_lock, ast_bridge_channel_unlock);
399                 ast_bridge_features_remove(iter->features, AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
400         }
401 }
402
403 /*!
404  * \brief Attended transfer superstates.
405  *
406  * An attended transfer's progress is facilitated by a state machine.
407  * The individual states of the state machine fall into the realm of
408  * one of two superstates.
409  */
410 enum attended_transfer_superstate {
411         /*!
412          * \brief Transfer superstate
413          *
414          * The attended transfer state machine begins in this superstate. The
415          * goal of this state is for a transferer channel to facilitate a
416          * transfer from a transferee to a transfer target.
417          *
418          * There are two bridges used in this superstate. The transferee bridge is
419          * the bridge that the transferer and transferee channels originally
420          * communicate in, and the target bridge is the bridge where the transfer
421          * target is being dialed.
422          *
423          * The transferer channel is capable of moving between the bridges using
424          * the DTMF swap sequence.
425          */
426         SUPERSTATE_TRANSFER,
427         /*!
428          * \brief Recall superstate
429          *
430          * The attended transfer state machine moves to this superstate if
431          * atxferdropcall is set to "no" and the transferer channel hangs up
432          * during a transfer. The goal in this superstate is to call back either
433          * the transfer target or transferer and rebridge with the transferee
434          * channel(s).
435          *
436          * In this superstate, there is only a single bridge used, the original
437          * transferee bridge. Rather than distinguishing between a transferer
438          * and transfer target, all outbound calls are toward a "recall_target"
439          * channel.
440          */
441         SUPERSTATE_RECALL,
442 };
443
444 /*!
445  * The states in the attended transfer state machine.
446  */
447 enum attended_transfer_state {
448         /*!
449          * \brief Calling Target state
450          *
451          * This state describes the initial state of a transfer. The transferer
452          * waits in the transfer target's bridge for the transfer target to answer.
453          *
454          * Superstate: Transfer
455          *
456          * Preconditions:
457          * 1) Transfer target is RINGING
458          * 2) Transferer is in transferee bridge
459          * 3) Transferee is on hold
460          *
461          * Transitions to TRANSFER_CALLING_TARGET:
462          * 1) This is the initial state for an attended transfer.
463          * 2) TRANSFER_HESITANT: Transferer presses DTMF swap sequence
464          *
465          * State operation:
466          * The transferer is moved from the transferee bridge into the transfer
467          * target bridge.
468          *
469          * Transitions from TRANSFER_CALLING_TARGET:
470          * 1) TRANSFER_FAIL: Transferee hangs up.
471          * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
472          * and configured atxferdropcall setting is yes.
473          * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
474          * sequence and configured atxferdroppcall setting is no.
475          * 4) TRANSFER_CONSULTING: Transfer target answers the call.
476          * 5) TRANSFER_REBRIDGE: Transfer target hangs up, call to transfer target
477          * times out, or transferer presses DTMF abort sequence.
478          * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
479          * 7) TRANSFER_HESITANT: Transferer presses DTMF swap sequence.
480          */
481         TRANSFER_CALLING_TARGET,
482         /*!
483          * \brief Hesitant state
484          *
485          * This state only arises if when waiting for the transfer target to
486          * answer, the transferer presses the DTMF swap sequence. This will
487          * cause the transferer to be rebridged with the transferee temporarily.
488          *
489          * Superstate: Transfer
490          *
491          * Preconditions:
492          * 1) Transfer target is in ringing state
493          * 2) Transferer is in transfer target bridge
494          * 3) Transferee is on hold
495          *
496          * Transitions to TRANSFER_HESITANT:
497          * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
498          *
499          * State operation:
500          * The transferer is moved from the transfer target bridge into the
501          * transferee bridge, and the transferee is taken off hold.
502          *
503          * Transitions from TRANSFER_HESITANT:
504          * 1) TRANSFER_FAIL: Transferee hangs up
505          * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
506          * and configured atxferdropcall setting is yes.
507          * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
508          * sequence and configured atxferdroppcall setting is no.
509          * 4) TRANSFER_DOUBLECHECKING: Transfer target answers the call
510          * 5) TRANSFER_RESUME: Transfer target hangs up, call to transfer target
511          * times out, or transferer presses DTMF abort sequence.
512          * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
513          * 7) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
514          */
515         TRANSFER_HESITANT,
516         /*!
517          * \brief Rebridge state
518          *
519          * This is a terminal state that indicates that the transferer needs
520          * to move back to the transferee's bridge. This is a failed attended
521          * transfer result.
522          *
523          * Superstate: Transfer
524          *
525          * Preconditions:
526          * 1) Transferer is in transfer target bridge
527          * 2) Transferee is on hold
528          *
529          * Transitions to TRANSFER_REBRIDGE:
530          * 1) TRANSFER_CALLING_TARGET: Transfer target hangs up, call to transfer target
531          * times out, or transferer presses DTMF abort sequence.
532          * 2) TRANSFER_STATE_CONSULTING: Transfer target hangs up, or transferer presses
533          * DTMF abort sequence.
534          *
535          * State operation:
536          * The transferer channel is moved from the transfer target bridge to the
537          * transferee bridge. The transferee is taken off hold. A stasis transfer
538          * message is published indicating a failed attended transfer.
539          *
540          * Transitions from TRANSFER_REBRIDGE:
541          * None
542          */
543         TRANSFER_REBRIDGE,
544         /*!
545          * \brief Resume state
546          *
547          * This is a terminal state that indicates that the party bridged with the
548          * transferee is the final party to be bridged with that transferee. This state
549          * may come about due to a successful recall or due to a failed transfer.
550          *
551          * Superstate: Transfer or Recall
552          *
553          * Preconditions:
554          * In Transfer Superstate:
555          * 1) Transferer is in transferee bridge
556          * 2) Transferee is not on hold
557          * In Recall Superstate:
558          * 1) The recall target is in the transferee bridge
559          * 2) Transferee is not on hold
560          *
561          * Transitions to TRANSFER_RESUME:
562          * TRANSFER_HESITANT: Transfer target hangs up, call to transfer target times out,
563          * or transferer presses DTMF abort sequence.
564          * TRANSFER_DOUBLECHECKING: Transfer target hangs up or transferer presses DTMF
565          * abort sequence.
566          * TRANSFER_BLOND_NONFINAL: Recall target answers
567          * TRANSFER_RECALLING: Recall target answers
568          * TRANSFER_RETRANSFER: Recall target answers
569          *
570          * State operations:
571          * None
572          *
573          * Transitions from TRANSFER_RESUME:
574          * None
575          */
576         TRANSFER_RESUME,
577         /*!
578          * \brief Threeway state
579          *
580          * This state results when the transferer wishes to have all parties involved
581          * in a transfer to be in the same bridge together.
582          *
583          * Superstate: Transfer
584          *
585          * Preconditions:
586          * 1) Transfer target state is either RINGING or UP
587          * 2) Transferer is in either bridge
588          * 3) Transferee is not on hold
589          *
590          * Transitions to TRANSFER_THREEWAY:
591          * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF threeway sequence.
592          * 2) TRANSFER_HESITANT: Transferer presses DTMF threeway sequence.
593          * 3) TRANSFER_CONSULTING: Transferer presses DTMF threeway sequence.
594          * 4) TRANSFER_DOUBLECHECKING: Transferer presses DTMF threeway sequence.
595          *
596          * State operation:
597          * The transfer target bridge is merged into the transferee bridge.
598          *
599          * Transitions from TRANSFER_THREEWAY:
600          * None.
601          */
602         TRANSFER_THREEWAY,
603         /*!
604          * \brief Consulting state
605          *
606          * This state describes the case where the transferer and transfer target
607          * are able to converse in the transfer target's bridge prior to completing
608          * the transfer.
609          *
610          * Superstate: Transfer
611          *
612          * Preconditions:
613          * 1) Transfer target is UP
614          * 2) Transferer is in target bridge
615          * 3) Transferee is on hold
616          *
617          * Transitions to TRANSFER_CONSULTING:
618          * 1) TRANSFER_CALLING_TARGET: Transfer target answers.
619          * 2) TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
620          *
621          * State operations:
622          * None.
623          *
624          * Transitions from TRANSFER_CONSULTING:
625          * TRANSFER_COMPLETE: Transferer hangs up or transferer presses DTMF complete sequence.
626          * TRANSFER_REBRIDGE: Transfer target hangs up or transferer presses DTMF abort sequence.
627          * TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
628          * TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
629          */
630         TRANSFER_CONSULTING,
631         /*!
632          * \brief Double-checking state
633          *
634          * This state describes the case where the transferer and transferee are
635          * able to converse in the transferee's bridge prior to completing the transfer. The
636          * difference between this and TRANSFER_HESITANT is that the transfer target is
637          * UP in this case.
638          *
639          * Superstate: Transfer
640          *
641          * Preconditions:
642          * 1) Transfer target is UP and on hold
643          * 2) Transferer is in transferee bridge
644          * 3) Transferee is off hold
645          *
646          * Transitions to TRANSFER_DOUBLECHECKING:
647          * 1) TRANSFER_HESITANT: Transfer target answers.
648          * 2) TRANSFER_CONSULTING: Transferer presses DTMF swap sequence.
649          *
650          * State operations:
651          * None.
652          *
653          * Transitions from TRANSFER_DOUBLECHECKING:
654          * 1) TRANSFER_FAIL: Transferee hangs up.
655          * 2) TRANSFER_COMPLETE: Transferer hangs up or presses DTMF complete sequence.
656          * 3) TRANSFER_RESUME: Transfer target hangs up or transferer presses DTMF abort sequence.
657          * 4) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
658          * 5) TRANSFER_CONSULTING: Transferer presses the DTMF swap sequence.
659          */
660         TRANSFER_DOUBLECHECKING,
661         /*!
662          * \brief Complete state
663          *
664          * This is a terminal state where a transferer has successfully completed an attended
665          * transfer. This state's goal is to get the transfer target and transferee into
666          * the same bridge and the transferer off the call.
667          *
668          * Superstate: Transfer
669          *
670          * Preconditions:
671          * 1) Transfer target is UP and off hold.
672          * 2) Transferer is in either bridge.
673          * 3) Transferee is off hold.
674          *
675          * Transitions to TRANSFER_COMPLETE:
676          * 1) TRANSFER_CONSULTING: transferer hangs up or presses the DTMF complete sequence.
677          * 2) TRANSFER_DOUBLECHECKING: transferer hangs up or presses the DTMF complete sequence.
678          *
679          * State operation:
680          * The transfer target bridge is merged into the transferee bridge. The transferer
681          * channel is kicked out of the bridges as part of the merge.
682          *
683          * State operations:
684          * 1) Merge the transfer target bridge into the transferee bridge,
685          * excluding the transferer channel from the merge.
686          * 2) Publish a stasis transfer message.
687          *
688          * Exit operations:
689          * This is a terminal state, so there are no exit operations.
690          */
691         TRANSFER_COMPLETE,
692         /*!
693          * \brief Blond state
694          *
695          * This is a terminal state where a transferer has completed an attended transfer prior
696          * to the transfer target answering. This state is only entered if atxferdropcall
697          * is set to 'yes'. This is considered to be a successful attended transfer.
698          *
699          * Superstate: Transfer
700          *
701          * Preconditions:
702          * 1) Transfer target is RINGING.
703          * 2) Transferer is in either bridge.
704          * 3) Transferee is off hold.
705          *
706          * Transitions to TRANSFER_BLOND:
707          * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
708          *    atxferdropcall is set to 'yes'.
709          * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
710          *    atxferdropcall is set to 'yes'.
711          *
712          * State operations:
713          * The transfer target bridge is merged into the transferee bridge. The transferer
714          * channel is kicked out of the bridges as part of the merge. A stasis transfer
715          * publication is sent indicating a successful transfer.
716          *
717          * Transitions from TRANSFER_BLOND:
718          * None
719          */
720         TRANSFER_BLOND,
721         /*!
722          * \brief Blond non-final state
723          *
724          * This state is very similar to the TRANSFER_BLOND state, except that
725          * this state is entered when atxferdropcall is set to 'no'. This is the
726          * initial state of the Recall superstate, so state operations mainly involve
727          * moving to the Recall superstate. This means that the transfer target, that
728          * is currently ringing is now known as the recall target.
729          *
730          * Superstate: Recall
731          *
732          * Preconditions:
733          * 1) Recall target is RINGING.
734          * 2) Transferee is off hold.
735          *
736          * Transitions to TRANSFER_BLOND_NONFINAL:
737          * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
738          *    atxferdropcall is set to 'no'.
739          * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
740          *    atxferdropcall is set to 'no'.
741          *
742          * State operation:
743          * The superstate of the attended transfer is changed from Transfer to Recall.
744          * The transfer target bridge is merged into the transferee bridge. The transferer
745          * channel is kicked out of the bridges as part of the merge.
746          *
747          * Transitions from TRANSFER_BLOND_NONFINAL:
748          * 1) TRANSFER_FAIL: Transferee hangs up
749          * 2) TRANSFER_RESUME: Recall target answers
750          * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
751          */
752         TRANSFER_BLOND_NONFINAL,
753         /*!
754          * \brief Recalling state
755          *
756          * This state is entered if the recall target from the TRANSFER_BLOND_NONFINAL
757          * or TRANSFER_RETRANSFER states hangs up or does not answer. The goal of this
758          * state is to call back the original transferer in an attempt to recover the
759          * original call.
760          *
761          * Superstate: Recall
762          *
763          * Preconditions:
764          * 1) Recall target is down.
765          * 2) Transferee is off hold.
766          *
767          * Transitions to TRANSFER_RECALLING:
768          * 1) TRANSFER_BLOND_NONFINAL: Recall target hangs up or time expires.
769          * 2) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
770          *    atxferloopdelay is non-zero.
771          * 3) TRANSFER_WAIT_TO_RECALL: Time expires.
772          *
773          * State operation:
774          * The original transferer becomes the recall target and is called using the Dialing API.
775          * Ringing is indicated to the transferee.
776          *
777          * Transitions from TRANSFER_RECALLING:
778          * 1) TRANSFER_FAIL:
779          *    a) Transferee hangs up.
780          *    b) Recall target hangs up or time expires, and number of recall attempts exceeds atxfercallbackretries
781          * 2) TRANSFER_WAIT_TO_RETRANSFER: Recall target hangs up or time expires.
782          *    atxferloopdelay is non-zero.
783          * 3) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
784          *    atxferloopdelay is zero.
785          * 4) TRANSFER_RESUME: Recall target answers.
786          */
787         TRANSFER_RECALLING,
788         /*!
789          * \brief Wait to Retransfer state
790          *
791          * This state is used simply to give a bit of breathing room between attempting
792          * to call back the original transferer and attempting to call back the original
793          * transfer target. The transferee hears music on hold during this state as an
794          * auditory clue that no one is currently being dialed.
795          *
796          * Superstate: Recall
797          *
798          * Preconditions:
799          * 1) Recall target is down.
800          * 2) Transferee is off hold.
801          *
802          * Transitions to TRANSFER_WAIT_TO_RETRANSFER:
803          * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
804          *    atxferloopdelay is non-zero.
805          *
806          * State operation:
807          * The transferee is placed on hold.
808          *
809          * Transitions from TRANSFER_WAIT_TO_RETRANSFER:
810          * 1) TRANSFER_FAIL: Transferee hangs up.
811          * 2) TRANSFER_RETRANSFER: Time expires.
812          */
813         TRANSFER_WAIT_TO_RETRANSFER,
814         /*!
815          * \brief Retransfer state
816          *
817          * This state is used in order to attempt to call back the original
818          * transfer target channel from the transfer. The transferee hears
819          * ringing during this state as an auditory cue that a party is being
820          * dialed.
821          *
822          * Superstate: Recall
823          *
824          * Preconditions:
825          * 1) Recall target is down.
826          * 2) Transferee is off hold.
827          *
828          * Transitions to TRANSFER_RETRANSFER:
829          * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
830          *    atxferloopdelay is zero.
831          * 2) TRANSFER_WAIT_TO_RETRANSFER: Time expires.
832          *
833          * State operation:
834          * The original transfer target is requested and is set as the recall target.
835          * The recall target is called and placed into the transferee bridge.
836          *
837          * Transitions from TRANSFER_RETRANSFER:
838          * 1) TRANSFER_FAIL: Transferee hangs up.
839          * 2) TRANSFER_WAIT_TO_RECALL: Recall target hangs up or time expires.
840          *    atxferloopdelay is non-zero.
841          * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
842          *    atxferloopdelay is zero.
843          */
844         TRANSFER_RETRANSFER,
845         /*!
846          * \brief Wait to recall state
847          *
848          * This state is used simply to give a bit of breathing room between attempting
849          * to call back the original transfer target and attempting to call back the
850          * original transferer. The transferee hears music on hold during this state as an
851          * auditory clue that no one is currently being dialed.
852          *
853          * Superstate: Recall
854          *
855          * Preconditions:
856          * 1) Recall target is down.
857          * 2) Transferee is off hold.
858          *
859          * Transitions to TRANSFER_WAIT_TO_RECALL:
860          * 1) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
861          *    atxferloopdelay is non-zero.
862          *
863          * State operation:
864          * Transferee is placed on hold.
865          *
866          * Transitions from TRANSFER_WAIT_TO_RECALL:
867          * 1) TRANSFER_FAIL: Transferee hangs up
868          * 2) TRANSFER_RECALLING: Time expires
869          */
870         TRANSFER_WAIT_TO_RECALL,
871         /*!
872          * \brief Fail state
873          *
874          * This state indicates that something occurred during the transfer that
875          * makes a graceful completion impossible. The most common stimulus for this
876          * state is when the transferee hangs up.
877          *
878          * Superstate: Transfer and Recall
879          *
880          * Preconditions:
881          * None
882          *
883          * Transitions to TRANSFER_FAIL:
884          * 1) TRANSFER_CALLING_TARGET: Transferee hangs up.
885          * 2) TRANSFER_HESITANT: Transferee hangs up.
886          * 3) TRANSFER_DOUBLECHECKING: Transferee hangs up.
887          * 4) TRANSFER_BLOND_NONFINAL: Transferee hangs up.
888          * 5) TRANSFER_RECALLING:
889          *    a) Transferee hangs up.
890          *    b) Recall target hangs up or time expires, and number of
891          *       recall attempts exceeds atxfercallbackretries.
892          * 6) TRANSFER_WAIT_TO_RETRANSFER: Transferee hangs up.
893          * 7) TRANSFER_RETRANSFER: Transferee hangs up.
894          * 8) TRANSFER_WAIT_TO_RECALL: Transferee hangs up.
895          *
896          * State operation:
897          * A transfer stasis publication is made indicating a failed transfer.
898          * The transferee bridge is destroyed.
899          *
900          * Transitions from TRANSFER_FAIL:
901          * None.
902          */
903         TRANSFER_FAIL,
904 };
905
906 /*!
907  * \brief Stimuli that can cause transfer state changes
908  */
909 enum attended_transfer_stimulus {
910         /*! No stimulus. This literally can never happen. */
911         STIMULUS_NONE,
912         /*! All of the transferee channels have been hung up. */
913         STIMULUS_TRANSFEREE_HANGUP,
914         /*! The transferer has hung up. */
915         STIMULUS_TRANSFERER_HANGUP,
916         /*! The transfer target channel has hung up. */
917         STIMULUS_TRANSFER_TARGET_HANGUP,
918         /*! The transfer target channel has answered. */
919         STIMULUS_TRANSFER_TARGET_ANSWER,
920         /*! The recall target channel has hung up. */
921         STIMULUS_RECALL_TARGET_HANGUP,
922         /*! The recall target channel has answered. */
923         STIMULUS_RECALL_TARGET_ANSWER,
924         /*! The current state's timer has expired. */
925         STIMULUS_TIMEOUT,
926         /*! The transferer pressed the abort DTMF sequence. */
927         STIMULUS_DTMF_ATXFER_ABORT,
928         /*! The transferer pressed the complete DTMF sequence. */
929         STIMULUS_DTMF_ATXFER_COMPLETE,
930         /*! The transferer pressed the three-way DTMF sequence. */
931         STIMULUS_DTMF_ATXFER_THREEWAY,
932         /*! The transferer pressed the swap DTMF sequence. */
933         STIMULUS_DTMF_ATXFER_SWAP,
934 };
935
936 /*!
937  * \brief String representations of the various stimuli
938  *
939  * Used for debugging purposes
940  */
941 const char *stimulus_strs[] = {
942         [STIMULUS_NONE] = "None",
943         [STIMULUS_TRANSFEREE_HANGUP] = "Transferee Hangup",
944         [STIMULUS_TRANSFERER_HANGUP] = "Transferer Hangup",
945         [STIMULUS_TRANSFER_TARGET_HANGUP] = "Transfer Target Hangup",
946         [STIMULUS_TRANSFER_TARGET_ANSWER] = "Transfer Target Answer",
947         [STIMULUS_RECALL_TARGET_HANGUP] = "Recall Target Hangup",
948         [STIMULUS_RECALL_TARGET_ANSWER] = "Recall Target Answer",
949         [STIMULUS_TIMEOUT] = "Timeout",
950         [STIMULUS_DTMF_ATXFER_ABORT] = "DTMF Abort",
951         [STIMULUS_DTMF_ATXFER_COMPLETE] = "DTMF Complete",
952         [STIMULUS_DTMF_ATXFER_THREEWAY] = "DTMF Threeway",
953         [STIMULUS_DTMF_ATXFER_SWAP] = "DTMF Swap",
954 };
955
956 struct stimulus_list {
957         enum attended_transfer_stimulus stimulus;
958         AST_LIST_ENTRY(stimulus_list) next;
959 };
960
961 /*!
962  * \brief Collection of data related to an attended transfer attempt
963  */
964 struct attended_transfer_properties {
965         AST_DECLARE_STRING_FIELDS (
966                 /*! Extension of transfer target */
967                 AST_STRING_FIELD(exten);
968                 /*! Context of transfer target */
969                 AST_STRING_FIELD(context);
970                 /*! Sound to play on failure */
971                 AST_STRING_FIELD(failsound);
972                 /*! Sound to play when transfer completes */
973                 AST_STRING_FIELD(xfersound);
974                 /*! The channel technology of the transferer channel */
975                 AST_STRING_FIELD(transferer_type);
976                 /*! The transferer channel address */
977                 AST_STRING_FIELD(transferer_addr);
978         );
979         /*! Condition used to synchronize when stimuli are reported to the monitor thread */
980         ast_cond_t cond;
981         /*! The bridge where the transferee resides. This bridge is also the bridge that
982          * survives a successful attended transfer.
983          */
984         struct ast_bridge *transferee_bridge;
985         /*! The bridge used to place an outbound call to the transfer target. This
986          * bridge is merged with the transferee_bridge on a successful transfer.
987          */
988         struct ast_bridge *target_bridge;
989         /*! The party that performs the attended transfer. */
990         struct ast_channel *transferer;
991         /*! The local channel dialed to reach the transfer target. */
992         struct ast_channel *transfer_target;
993         /*! The party that is currently being recalled. Depending on
994          * the current state, this may be either the party that originally
995          * was the transferer or the original transfer target
996          */
997         struct ast_channel *recall_target;
998         /*! The absolute starting time for running timers */
999         struct timeval start;
1000         AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
1001         /*! The current state of the attended transfer */
1002         enum attended_transfer_state state;
1003         /*! The current superstate of the attended transfer */
1004         enum attended_transfer_superstate superstate;
1005         /*! Configured atxferdropcall from features.conf */
1006         int atxferdropcall;
1007         /*! Configured atxfercallbackretries from features.conf */
1008         int atxfercallbackretries;
1009         /*! Configured atxferloopdelay from features.conf */
1010         int atxferloopdelay;
1011         /*! Configured atxfernoanswertimeout from features.conf */
1012         int atxfernoanswertimeout;
1013         /*! Count of the number of times that recalls have been attempted */
1014         int retry_attempts;
1015         /*! Framehook ID for outbounc call to transfer target or recall target */
1016         int target_framehook_id;
1017         /*! Dial structure used when recalling transferer channel */
1018         struct ast_dial *dial;
1019         /*! The bridging features the transferer has available */
1020         struct ast_flags transferer_features;
1021 };
1022
1023 static void attended_transfer_properties_destructor(void *obj)
1024 {
1025         struct attended_transfer_properties *props = obj;
1026
1027         ast_debug(1, "Destroy attended transfer properties %p\n", props);
1028
1029         ao2_cleanup(props->target_bridge);
1030         ao2_cleanup(props->transferee_bridge);
1031         /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
1032         ast_channel_cleanup(props->transferer);
1033         ast_channel_cleanup(props->transfer_target);
1034         ast_channel_cleanup(props->recall_target);
1035         ast_string_field_free_memory(props);
1036         ast_cond_destroy(&props->cond);
1037 }
1038
1039 /*!
1040  * \internal
1041  * \brief Determine the transfer context to use.
1042  * \since 12.0.0
1043  *
1044  * \param transferer Channel initiating the transfer.
1045  * \param context User supplied context if available.  May be NULL.
1046  *
1047  * \return The context to use for the transfer.
1048  */
1049 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
1050 {
1051         if (!ast_strlen_zero(context)) {
1052                 return context;
1053         }
1054         context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
1055         if (!ast_strlen_zero(context)) {
1056                 return context;
1057         }
1058         context = ast_channel_macrocontext(transferer);
1059         if (!ast_strlen_zero(context)) {
1060                 return context;
1061         }
1062         context = ast_channel_context(transferer);
1063         if (!ast_strlen_zero(context)) {
1064                 return context;
1065         }
1066         return "default";
1067 }
1068
1069 /*!
1070  * \brief Allocate and initialize attended transfer properties
1071  *
1072  * \param transferee_bridge The bridge where the transfer was initiated
1073  * \param transferer The channel performing the attended transfer
1074  * \param context Suggestion for what context the transfer target extension can be found in
1075  *
1076  * \retval NULL Failure to allocate or initialize
1077  * \retval non-NULL Newly allocated properties
1078  */
1079 static struct attended_transfer_properties *attended_transfer_properties_alloc(
1080                 struct ast_bridge *transferee_bridge, struct ast_channel *transferer,
1081                 const char *context)
1082 {
1083         struct attended_transfer_properties *props;
1084         char *tech;
1085         char *addr;
1086         char *serial;
1087         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
1088         struct ast_flags *transferer_features;
1089
1090         props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
1091         if (!props || ast_string_field_init(props, 64)) {
1092                 return NULL;
1093         }
1094
1095         ast_cond_init(&props->cond, NULL);
1096
1097         props->target_framehook_id = -1;
1098         props->transferer = ast_channel_ref(transferer);
1099
1100         ast_channel_lock(props->transferer);
1101         xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
1102         if (!xfer_cfg) {
1103                 ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
1104                 ao2_ref(props, -1);
1105                 return NULL;
1106         }
1107         transferer_features = ast_bridge_features_ds_get(props->transferer);
1108         if (transferer_features) {
1109                 props->transferer_features = *transferer_features;
1110         }
1111         props->atxferdropcall = xfer_cfg->atxferdropcall;
1112         props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
1113         props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
1114         props->atxferloopdelay = xfer_cfg->atxferloopdelay;
1115         ast_string_field_set(props, context, get_transfer_context(transferer, context));
1116         ast_string_field_set(props, failsound, xfer_cfg->xferfailsound);
1117         ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
1118
1119         tech = ast_strdupa(ast_channel_name(props->transferer));
1120         addr = strchr(tech, '/');
1121         if (!addr) {
1122                 ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
1123                 ast_channel_unref(props->transferer);
1124                 return NULL;
1125         }
1126         *addr++ = '\0';
1127         serial = strrchr(addr, '-');
1128         if (serial) {
1129                 *serial = '\0';
1130         }
1131         ast_string_field_set(props, transferer_type, tech);
1132         ast_string_field_set(props, transferer_addr, addr);
1133
1134         ast_channel_unlock(props->transferer);
1135
1136         ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
1137                         props, ast_channel_name(props->transferer));
1138         return props;
1139 }
1140
1141 /*!
1142  * \brief Free backlog of stimuli in the queue
1143  */
1144 static void clear_stimulus_queue(struct attended_transfer_properties *props)
1145 {
1146         struct stimulus_list *list;
1147         SCOPED_AO2LOCK(lock, props);
1148
1149         while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
1150                 ast_free(list);
1151         }
1152 }
1153
1154 /*!
1155  * \brief Initiate shutdown of attended transfer properties
1156  *
1157  * Calling this indicates that the attended transfer properties are no longer needed
1158  * because the transfer operation has concluded.
1159  */
1160 static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
1161 {
1162         ast_debug(1, "Shutting down attended transfer %p\n", props);
1163
1164         if (props->transferee_bridge) {
1165                 ast_bridge_merge_inhibit(props->transferee_bridge, -1);
1166                 bridge_basic_change_personality(props->transferee_bridge,
1167                                 BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
1168         }
1169
1170         if (props->target_bridge) {
1171                 ast_bridge_destroy(props->target_bridge);
1172                 props->target_bridge = NULL;
1173         }
1174
1175         if (props->transferer) {
1176                 ast_channel_remove_bridge_role(props->transferer, TRANSFERER_ROLE_NAME);
1177         }
1178
1179         clear_stimulus_queue(props);
1180
1181         ao2_cleanup(props);
1182 }
1183
1184 static void stimulate_attended_transfer(struct attended_transfer_properties *props,
1185                 enum attended_transfer_stimulus stimulus)
1186 {
1187         struct stimulus_list *list;
1188
1189         list = ast_calloc(1, sizeof(*list));
1190         if (!list) {
1191                 ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
1192                 return;
1193         }
1194
1195         list->stimulus = stimulus;
1196         ao2_lock(props);
1197         AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
1198         ast_cond_signal(&props->cond);
1199         ao2_unlock(props);
1200 }
1201
1202 /*!
1203  * \brief Send a stasis publication for a successful attended transfer
1204  */
1205 static void publish_transfer_success(struct attended_transfer_properties *props)
1206 {
1207         struct ast_bridge_channel_pair transferee = {
1208                 .channel = props->transferer,
1209                 .bridge = props->transferee_bridge,
1210         };
1211         struct ast_bridge_channel_pair transfer_target = {
1212                 .channel = props->transferer,
1213                 .bridge = props->target_bridge,
1214         };
1215
1216         ast_bridge_publish_attended_transfer_bridge_merge(0, AST_BRIDGE_TRANSFER_SUCCESS,
1217                         &transferee, &transfer_target, props->transferee_bridge);
1218 }
1219
1220 /*!
1221  * \brief Send a stasis publication for an attended transfer that ends in a threeway call
1222  */
1223 static void publish_transfer_threeway(struct attended_transfer_properties *props)
1224 {
1225         struct ast_bridge_channel_pair transferee = {
1226                 .channel = props->transferer,
1227                 .bridge = props->transferee_bridge,
1228         };
1229         struct ast_bridge_channel_pair transfer_target = {
1230                 .channel = props->transferer,
1231                 .bridge = props->target_bridge,
1232         };
1233         struct ast_bridge_channel_pair threeway = {
1234                 .channel = props->transferer,
1235                 .bridge = props->transferee_bridge,
1236         };
1237
1238         ast_bridge_publish_attended_transfer_threeway(0, AST_BRIDGE_TRANSFER_SUCCESS,
1239                         &transferee, &transfer_target, &threeway);
1240 }
1241
1242 /*!
1243  * \brief Send a stasis publication for a failed attended transfer
1244  */
1245 static void publish_transfer_fail(struct attended_transfer_properties *props)
1246 {
1247         struct ast_bridge_channel_pair transferee = {
1248                 .channel = props->transferer,
1249                 .bridge = props->transferee_bridge,
1250         };
1251         struct ast_bridge_channel_pair transfer_target = {
1252                 .channel = props->transferer,
1253                 .bridge = props->target_bridge,
1254         };
1255
1256         ast_bridge_publish_attended_transfer_fail(0, AST_BRIDGE_TRANSFER_FAIL,
1257                         &transferee, &transfer_target);
1258 }
1259
1260 /*!
1261  * \brief Helper method to play a sound on a channel in a bridge
1262  *
1263  * \param chan The channel to play the sound to
1264  * \param sound The sound to play
1265  */
1266 static void play_sound(struct ast_channel *chan, const char *sound)
1267 {
1268         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1269
1270         ast_channel_lock(chan);
1271         bridge_channel = ast_channel_get_bridge_channel(chan);
1272         ast_channel_unlock(chan);
1273
1274         if (!bridge_channel) {
1275                 return;
1276         }
1277
1278         ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
1279 }
1280
1281 /*!
1282  * \brief Helper method to place a channel in a bridge on hold
1283  */
1284 static void hold(struct ast_channel *chan)
1285 {
1286         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1287
1288         if (chan) {
1289                 ast_channel_lock(chan);
1290                 bridge_channel = ast_channel_get_bridge_channel(chan);
1291                 ast_channel_unlock(chan);
1292
1293                 ast_assert(bridge_channel != NULL);
1294
1295                 ast_bridge_channel_write_hold(bridge_channel, NULL);
1296         }
1297 }
1298
1299 /*!
1300  * \brief Helper method to take a channel in a bridge off hold
1301  */
1302 static void unhold(struct ast_channel *chan)
1303 {
1304         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1305
1306         ast_channel_lock(chan);
1307         bridge_channel = ast_channel_get_bridge_channel(chan);
1308         ast_channel_unlock(chan);
1309
1310         ast_assert(bridge_channel != NULL);
1311
1312         ast_bridge_channel_write_unhold(bridge_channel);
1313 }
1314
1315 /*!
1316  * \brief Helper method to send a ringing indication to a channel in a bridge
1317  */
1318 static void ringing(struct ast_channel *chan)
1319 {
1320         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1321
1322         ast_channel_lock(chan);
1323         bridge_channel = ast_channel_get_bridge_channel(chan);
1324         ast_channel_unlock(chan);
1325
1326         ast_assert(bridge_channel != NULL);
1327
1328         ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
1329 }
1330
1331 /*!
1332  * \brief Helper method to send a ringing indication to all channels in a bridge
1333  */
1334 static void bridge_ringing(struct ast_bridge *bridge)
1335 {
1336         struct ast_frame ringing = {
1337                 .frametype = AST_FRAME_CONTROL,
1338                 .subclass.integer = AST_CONTROL_RINGING,
1339         };
1340
1341         ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
1342 }
1343
1344 /*!
1345  * \brief Helper method to send a hold frame to all channels in a bridge
1346  */
1347 static void bridge_hold(struct ast_bridge *bridge)
1348 {
1349         struct ast_frame hold = {
1350                 .frametype = AST_FRAME_CONTROL,
1351                 .subclass.integer = AST_CONTROL_HOLD,
1352         };
1353
1354         ast_bridge_queue_everyone_else(bridge, NULL, &hold);
1355 }
1356
1357 /*!
1358  * \brief Helper method to send an unhold frame to all channels in a bridge
1359  */
1360 static void bridge_unhold(struct ast_bridge *bridge)
1361 {
1362         struct ast_frame unhold = {
1363                 .frametype = AST_FRAME_CONTROL,
1364                 .subclass.integer = AST_CONTROL_UNHOLD,
1365         };
1366
1367         ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
1368 }
1369
1370 /*!
1371  * \brief Wrapper for \ref bridge_do_move
1372  */
1373 static int bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
1374 {
1375         int res;
1376         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1377
1378         ast_bridge_lock_both(src, dest);
1379
1380         ast_channel_lock(channel);
1381         bridge_channel = ast_channel_get_bridge_channel(channel);
1382         ast_channel_unlock(channel);
1383
1384         ast_assert(bridge_channel != NULL);
1385
1386         ao2_lock(bridge_channel);
1387         bridge_channel->swap = swap;
1388         ao2_unlock(bridge_channel);
1389
1390         res = bridge_do_move(dest, bridge_channel, 1, 0);
1391
1392         ast_bridge_unlock(dest);
1393         ast_bridge_unlock(src);
1394
1395         return res;
1396 }
1397
1398 /*!
1399  * \brief Wrapper for \ref bridge_do_merge
1400  */
1401 static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
1402 {
1403         struct ast_bridge_channel **kick_bridge_channels = num_channels ?
1404                 ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
1405         int i;
1406         int num_bridge_channels = 0;
1407
1408         ast_bridge_lock_both(dest, src);
1409
1410         for (i = 0; i < num_channels; ++i) {
1411                 struct ast_bridge_channel *kick_bridge_channel;
1412
1413                 kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
1414                 if (!kick_bridge_channel) {
1415                         kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
1416                 }
1417
1418                 /* It's possible (and fine) for the bridge channel to be NULL at this point if the
1419                  * channel has hung up already. If that happens, we can just remove it from the list
1420                  * of bridge channels to kick from the bridge
1421                  */
1422                 if (!kick_bridge_channel) {
1423                         continue;
1424                 }
1425
1426                 kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
1427         }
1428
1429         bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
1430         ast_bridge_unlock(dest);
1431         ast_bridge_unlock(src);
1432 }
1433
1434 /*!
1435  * \brief Flags that indicate properties of attended transfer states
1436  */
1437 enum attended_transfer_state_flags {
1438         /*! This state requires that the timer be reset when entering the state */
1439         TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
1440         /*! This state's timer uses atxferloopdelay */
1441         TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
1442         /*! This state's timer uses atxfernoanswertimeout */
1443         TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
1444         /*! This state has a time limit associated with it */
1445         TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
1446                         TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
1447         /*! This state does not transition to any other states */
1448         TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
1449 };
1450
1451 static int calling_target_enter(struct attended_transfer_properties *props);
1452 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
1453                 enum attended_transfer_stimulus stimulus);
1454
1455 static int hesitant_enter(struct attended_transfer_properties *props);
1456 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
1457                 enum attended_transfer_stimulus stimulus);
1458
1459 static int rebridge_enter(struct attended_transfer_properties *props);
1460
1461 static int resume_enter(struct attended_transfer_properties *props);
1462
1463 static int threeway_enter(struct attended_transfer_properties *props);
1464
1465 static int consulting_enter(struct attended_transfer_properties *props);
1466 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
1467                 enum attended_transfer_stimulus stimulus);
1468
1469 static int double_checking_enter(struct attended_transfer_properties *props);
1470 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
1471                 enum attended_transfer_stimulus stimulus);
1472
1473 static int complete_enter(struct attended_transfer_properties *props);
1474
1475 static int blond_enter(struct attended_transfer_properties *props);
1476
1477 static int blond_nonfinal_enter(struct attended_transfer_properties *props);
1478 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
1479                 enum attended_transfer_stimulus stimulus);
1480
1481 static int recalling_enter(struct attended_transfer_properties *props);
1482 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
1483                 enum attended_transfer_stimulus stimulus);
1484
1485 static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
1486 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
1487                 enum attended_transfer_stimulus stimulus);
1488
1489 static int retransfer_enter(struct attended_transfer_properties *props);
1490 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
1491                 enum attended_transfer_stimulus stimulus);
1492
1493 static int wait_to_recall_enter(struct attended_transfer_properties *props);
1494 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
1495                 enum attended_transfer_stimulus stimulus);
1496
1497 static int fail_enter(struct attended_transfer_properties *props);
1498
1499 /*!
1500  * \brief Properties of an attended transfer state
1501  */
1502 struct attended_transfer_state_properties {
1503         /*! The name of the state. Used for debugging */
1504         const char *state_name;
1505         /*! Function used to enter a state */
1506         int (*enter)(struct attended_transfer_properties *props);
1507         /*!
1508          * Function used to exit a state
1509          * This is used both to determine what the next state
1510          * to transition to will be and to perform any cleanup
1511          * necessary before exiting the current state.
1512          */
1513         enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
1514                         enum attended_transfer_stimulus stimulus);
1515         /*! Flags associated with this state */
1516         enum attended_transfer_state_flags flags;
1517 };
1518
1519 static const struct attended_transfer_state_properties state_properties[] = {
1520         [TRANSFER_CALLING_TARGET] = {
1521                 .state_name = "Calling Target",
1522                 .enter = calling_target_enter,
1523                 .exit = calling_target_exit,
1524                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1525         },
1526         [TRANSFER_HESITANT] = {
1527                 .state_name = "Hesitant",
1528                 .enter = hesitant_enter,
1529                 .exit = hesitant_exit,
1530                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
1531         },
1532         [TRANSFER_REBRIDGE] = {
1533                 .state_name = "Rebridge",
1534                 .enter = rebridge_enter,
1535                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1536         },
1537         [TRANSFER_RESUME] = {
1538                 .state_name = "Resume",
1539                 .enter = resume_enter,
1540                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1541         },
1542         [TRANSFER_THREEWAY] = {
1543                 .state_name = "Threeway",
1544                 .enter = threeway_enter,
1545                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1546         },
1547         [TRANSFER_CONSULTING] = {
1548                 .state_name = "Consulting",
1549                 .enter = consulting_enter,
1550                 .exit = consulting_exit,
1551         },
1552         [TRANSFER_DOUBLECHECKING] = {
1553                 .state_name = "Double Checking",
1554                 .enter = double_checking_enter,
1555                 .exit = double_checking_exit,
1556         },
1557         [TRANSFER_COMPLETE] = {
1558                 .state_name = "Complete",
1559                 .enter = complete_enter,
1560                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1561         },
1562         [TRANSFER_BLOND] = {
1563                 .state_name = "Blond",
1564                 .enter = blond_enter,
1565                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1566         },
1567         [TRANSFER_BLOND_NONFINAL] = {
1568                 .state_name = "Blond Non-Final",
1569                 .enter = blond_nonfinal_enter,
1570                 .exit = blond_nonfinal_exit,
1571                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
1572         },
1573         [TRANSFER_RECALLING] = {
1574                 .state_name = "Recalling",
1575                 .enter = recalling_enter,
1576                 .exit = recalling_exit,
1577                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1578         },
1579         [TRANSFER_WAIT_TO_RETRANSFER] = {
1580                 .state_name = "Wait to Retransfer",
1581                 .enter = wait_to_retransfer_enter,
1582                 .exit = wait_to_retransfer_exit,
1583                 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
1584         },
1585         [TRANSFER_RETRANSFER] = {
1586                 .state_name = "Retransfer",
1587                 .enter = retransfer_enter,
1588                 .exit = retransfer_exit,
1589                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1590         },
1591         [TRANSFER_WAIT_TO_RECALL] = {
1592                 .state_name = "Wait to Recall",
1593                 .enter = wait_to_recall_enter,
1594                 .exit = wait_to_recall_exit,
1595                 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
1596         },
1597         [TRANSFER_FAIL] = {
1598                 .state_name = "Fail",
1599                 .enter = fail_enter,
1600                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1601         },
1602 };
1603
1604 static int calling_target_enter(struct attended_transfer_properties *props)
1605 {
1606         return bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
1607 }
1608
1609 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
1610                 enum attended_transfer_stimulus stimulus)
1611 {
1612         switch (stimulus) {
1613         case STIMULUS_TRANSFEREE_HANGUP:
1614                 play_sound(props->transferer, props->failsound);
1615                 publish_transfer_fail(props);
1616                 return TRANSFER_FAIL;
1617         case STIMULUS_DTMF_ATXFER_COMPLETE:
1618         case STIMULUS_TRANSFERER_HANGUP:
1619                 bridge_unhold(props->transferee_bridge);
1620                 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
1621         case STIMULUS_TRANSFER_TARGET_ANSWER:
1622                 return TRANSFER_CONSULTING;
1623         case STIMULUS_TRANSFER_TARGET_HANGUP:
1624         case STIMULUS_TIMEOUT:
1625         case STIMULUS_DTMF_ATXFER_ABORT:
1626                 play_sound(props->transferer, props->failsound);
1627                 return TRANSFER_REBRIDGE;
1628         case STIMULUS_DTMF_ATXFER_THREEWAY:
1629                 bridge_unhold(props->transferee_bridge);
1630                 return TRANSFER_THREEWAY;
1631         case STIMULUS_DTMF_ATXFER_SWAP:
1632                 return TRANSFER_HESITANT;
1633         case STIMULUS_NONE:
1634         case STIMULUS_RECALL_TARGET_ANSWER:
1635         case STIMULUS_RECALL_TARGET_HANGUP:
1636         default:
1637                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1638                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
1639                 return props->state;
1640         }
1641 }
1642
1643 static int hesitant_enter(struct attended_transfer_properties *props)
1644 {
1645         if (bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL)) {
1646                 return -1;
1647         }
1648
1649         unhold(props->transferer);
1650         return 0;
1651 }
1652
1653 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
1654                 enum attended_transfer_stimulus stimulus)
1655 {
1656         switch (stimulus) {
1657         case STIMULUS_TRANSFEREE_HANGUP:
1658                 play_sound(props->transferer, props->failsound);
1659                 publish_transfer_fail(props);
1660                 return TRANSFER_FAIL;
1661         case STIMULUS_DTMF_ATXFER_COMPLETE:
1662         case STIMULUS_TRANSFERER_HANGUP:
1663                 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
1664         case STIMULUS_TRANSFER_TARGET_ANSWER:
1665                 return TRANSFER_DOUBLECHECKING;
1666         case STIMULUS_TRANSFER_TARGET_HANGUP:
1667         case STIMULUS_TIMEOUT:
1668         case STIMULUS_DTMF_ATXFER_ABORT:
1669                 play_sound(props->transferer, props->failsound);
1670                 return TRANSFER_RESUME;
1671         case STIMULUS_DTMF_ATXFER_THREEWAY:
1672                 return TRANSFER_THREEWAY;
1673         case STIMULUS_DTMF_ATXFER_SWAP:
1674                 hold(props->transferer);
1675                 return TRANSFER_CALLING_TARGET;
1676         case STIMULUS_NONE:
1677         case STIMULUS_RECALL_TARGET_HANGUP:
1678         case STIMULUS_RECALL_TARGET_ANSWER:
1679         default:
1680                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1681                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
1682                 return props->state;
1683         }
1684 }
1685
1686 static int rebridge_enter(struct attended_transfer_properties *props)
1687 {
1688         if (bridge_move(props->transferee_bridge, props->target_bridge,
1689                         props->transferer, NULL)) {
1690                 return -1;
1691         }
1692
1693         unhold(props->transferer);
1694         return 0;
1695 }
1696
1697 static int resume_enter(struct attended_transfer_properties *props)
1698 {
1699         return 0;
1700 }
1701
1702 static int threeway_enter(struct attended_transfer_properties *props)
1703 {
1704         bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
1705         play_sound(props->transfer_target, props->xfersound);
1706         play_sound(props->transferer, props->xfersound);
1707         publish_transfer_threeway(props);
1708
1709         return 0;
1710 }
1711
1712 static int consulting_enter(struct attended_transfer_properties *props)
1713 {
1714         return 0;
1715 }
1716
1717 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
1718                 enum attended_transfer_stimulus stimulus)
1719 {
1720         switch (stimulus) {
1721         case STIMULUS_TRANSFEREE_HANGUP:
1722                 /* This is a one-of-a-kind event. The transferer and transfer target are talking in
1723                  * one bridge, and the transferee has hung up in a separate bridge. In this case, we
1724                  * will change the personality of the transfer target bridge back to normal, and play
1725                  * a sound to the transferer to indicate the transferee is gone.
1726                  */
1727                 bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
1728                 play_sound(props->transferer, props->failsound);
1729                 ast_bridge_merge_inhibit(props->target_bridge, -1);
1730                 /* These next two lines are here to ensure that our reference to the target bridge
1731                  * is cleaned up properly and that the target bridge is not destroyed when the
1732                  * monitor thread exits
1733                  */
1734                 ao2_ref(props->target_bridge, -1);
1735                 props->target_bridge = NULL;
1736                 return TRANSFER_FAIL;
1737         case STIMULUS_TRANSFERER_HANGUP:
1738         case STIMULUS_DTMF_ATXFER_COMPLETE:
1739                 /* We know the transferer is in the target_bridge, so take the other bridge off hold */
1740                 bridge_unhold(props->transferee_bridge);
1741                 return TRANSFER_COMPLETE;
1742         case STIMULUS_TRANSFER_TARGET_HANGUP:
1743         case STIMULUS_DTMF_ATXFER_ABORT:
1744                 play_sound(props->transferer, props->failsound);
1745                 return TRANSFER_REBRIDGE;
1746         case STIMULUS_DTMF_ATXFER_THREEWAY:
1747                 bridge_unhold(props->transferee_bridge);
1748                 return TRANSFER_THREEWAY;
1749         case STIMULUS_DTMF_ATXFER_SWAP:
1750                 hold(props->transferer);
1751                 bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
1752                 unhold(props->transferer);
1753                 return TRANSFER_DOUBLECHECKING;
1754         case STIMULUS_NONE:
1755         case STIMULUS_TIMEOUT:
1756         case STIMULUS_TRANSFER_TARGET_ANSWER:
1757         case STIMULUS_RECALL_TARGET_HANGUP:
1758         case STIMULUS_RECALL_TARGET_ANSWER:
1759         default:
1760                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1761                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
1762                 return props->state;
1763         }
1764 }
1765
1766 static int double_checking_enter(struct attended_transfer_properties *props)
1767 {
1768         return 0;
1769 }
1770
1771 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
1772                 enum attended_transfer_stimulus stimulus)
1773 {
1774         switch (stimulus) {
1775         case STIMULUS_TRANSFEREE_HANGUP:
1776                 play_sound(props->transferer, props->failsound);
1777                 publish_transfer_fail(props);
1778                 return TRANSFER_FAIL;
1779         case STIMULUS_TRANSFERER_HANGUP:
1780         case STIMULUS_DTMF_ATXFER_COMPLETE:
1781                 /* We know the transferer is in the transferee, so take the other bridge off hold */
1782                 bridge_unhold(props->target_bridge);
1783                 return TRANSFER_COMPLETE;
1784         case STIMULUS_TRANSFER_TARGET_HANGUP:
1785         case STIMULUS_DTMF_ATXFER_ABORT:
1786                 play_sound(props->transferer, props->failsound);
1787                 return TRANSFER_RESUME;
1788         case STIMULUS_DTMF_ATXFER_THREEWAY:
1789                 bridge_unhold(props->target_bridge);
1790                 return TRANSFER_THREEWAY;
1791         case STIMULUS_DTMF_ATXFER_SWAP:
1792                 hold(props->transferer);
1793                 bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
1794                 unhold(props->transferer);
1795                 return TRANSFER_CONSULTING;
1796         case STIMULUS_NONE:
1797         case STIMULUS_TIMEOUT:
1798         case STIMULUS_TRANSFER_TARGET_ANSWER:
1799         case STIMULUS_RECALL_TARGET_HANGUP:
1800         case STIMULUS_RECALL_TARGET_ANSWER:
1801         default:
1802                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1803                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
1804                 return props->state;
1805         }
1806 }
1807
1808 static int complete_enter(struct attended_transfer_properties *props)
1809 {
1810         bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
1811         play_sound(props->transfer_target, props->xfersound);
1812         publish_transfer_success(props);
1813         return 0;
1814 }
1815
1816 static int blond_enter(struct attended_transfer_properties *props)
1817 {
1818         bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
1819         ringing(props->transfer_target);
1820         publish_transfer_success(props);
1821         return 0;
1822 }
1823
1824 static int blond_nonfinal_enter(struct attended_transfer_properties *props)
1825 {
1826         int res;
1827         props->superstate = SUPERSTATE_RECALL;
1828         props->recall_target = ast_channel_ref(props->transfer_target);
1829         res = blond_enter(props);
1830         props->transfer_target = ast_channel_unref(props->transfer_target);
1831         return res;
1832 }
1833
1834 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
1835                 enum attended_transfer_stimulus stimulus)
1836 {
1837         switch (stimulus) {
1838         case STIMULUS_TRANSFEREE_HANGUP:
1839                 return TRANSFER_FAIL;
1840         case STIMULUS_RECALL_TARGET_ANSWER:
1841                 return TRANSFER_RESUME;
1842         case STIMULUS_TIMEOUT:
1843                 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
1844                 props->recall_target = ast_channel_unref(props->recall_target);
1845         case STIMULUS_RECALL_TARGET_HANGUP:
1846                 return TRANSFER_RECALLING;
1847         case STIMULUS_NONE:
1848         case STIMULUS_DTMF_ATXFER_ABORT:
1849         case STIMULUS_DTMF_ATXFER_COMPLETE:
1850         case STIMULUS_DTMF_ATXFER_THREEWAY:
1851         case STIMULUS_DTMF_ATXFER_SWAP:
1852         case STIMULUS_TRANSFERER_HANGUP:
1853         case STIMULUS_TRANSFER_TARGET_HANGUP:
1854         case STIMULUS_TRANSFER_TARGET_ANSWER:
1855         default:
1856                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1857                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
1858                 return props->state;
1859         }
1860 }
1861
1862 /*!
1863  * \brief Dial callback when attempting to recall the original transferer channel
1864  *
1865  * This is how we can monitor if the recall target has answered or has hung up.
1866  * If one of the two is detected, then an appropriate stimulus is sent to the
1867  * attended transfer monitor thread.
1868  */
1869 static void recall_callback(struct ast_dial *dial)
1870 {
1871         struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
1872
1873         switch (ast_dial_state(dial)) {
1874         default:
1875         case AST_DIAL_RESULT_INVALID:
1876         case AST_DIAL_RESULT_FAILED:
1877         case AST_DIAL_RESULT_TIMEOUT:
1878         case AST_DIAL_RESULT_HANGUP:
1879         case AST_DIAL_RESULT_UNANSWERED:
1880                 /* Failure cases */
1881                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
1882                 break;
1883         case AST_DIAL_RESULT_RINGING:
1884         case AST_DIAL_RESULT_PROGRESS:
1885         case AST_DIAL_RESULT_PROCEEDING:
1886         case AST_DIAL_RESULT_TRYING:
1887                 /* Don't care about these cases */
1888                 break;
1889         case AST_DIAL_RESULT_ANSWERED:
1890                 /* We struck gold! */
1891                 props->recall_target = ast_dial_answered_steal(dial);
1892                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
1893                 break;
1894         }
1895 }
1896
1897
1898 static int recalling_enter(struct attended_transfer_properties *props)
1899 {
1900         RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
1901         struct ast_format fmt;
1902
1903         if (!cap) {
1904                 return -1;
1905         }
1906
1907         ast_format_cap_add(cap, ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0));
1908
1909         /* When we dial the transfer target, since we are communicating
1910          * with a local channel, we can place the local channel in a bridge
1911          * and then call out to it. When recalling the transferer, though, we
1912          * have to use the dialing API because the channel is not local.
1913          */
1914         props->dial = ast_dial_create();
1915         if (!props->dial) {
1916                 return -1;
1917         }
1918
1919         if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr)) {
1920                 return -1;
1921         }
1922
1923         if (ast_dial_prerun(props->dial, NULL, cap)) {
1924                 return -1;
1925         }
1926
1927         ast_dial_set_state_callback(props->dial, &recall_callback);
1928
1929         ao2_ref(props, +1);
1930         ast_dial_set_user_data(props->dial, props);
1931
1932         if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
1933                 ao2_ref(props, -1);
1934                 return -1;
1935         }
1936
1937         bridge_ringing(props->transferee_bridge);
1938         return 0;
1939 }
1940
1941 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
1942                 enum attended_transfer_stimulus stimulus)
1943 {
1944         /* No matter what the outcome was, we need to kill off the dial */
1945         ast_dial_join(props->dial);
1946         ast_dial_destroy(props->dial);
1947         props->dial = NULL;
1948         /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
1949         ao2_ref(props, -1);
1950
1951         switch (stimulus) {
1952         case STIMULUS_TRANSFEREE_HANGUP:
1953                 return TRANSFER_FAIL;
1954         case STIMULUS_TIMEOUT:
1955         case STIMULUS_RECALL_TARGET_HANGUP:
1956                 ++props->retry_attempts;
1957                 if (props->retry_attempts >= props->atxfercallbackretries) {
1958                         return TRANSFER_FAIL;
1959                 }
1960                 if (props->atxferloopdelay) {
1961                         return TRANSFER_WAIT_TO_RETRANSFER;
1962                 }
1963                 return TRANSFER_RETRANSFER;
1964         case STIMULUS_RECALL_TARGET_ANSWER:
1965                 /* Setting this datastore up will allow the transferer to have all of his
1966                  * call features set up automatically when the bridge changes back to a
1967                  * normal personality
1968                  */
1969                 ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
1970                 ast_channel_ref(props->recall_target);
1971                 if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL, 1)) {
1972                         ast_hangup(props->recall_target);
1973                         return TRANSFER_FAIL;
1974                 }
1975                 return TRANSFER_RESUME;
1976         case STIMULUS_NONE:
1977         case STIMULUS_DTMF_ATXFER_ABORT:
1978         case STIMULUS_DTMF_ATXFER_COMPLETE:
1979         case STIMULUS_DTMF_ATXFER_THREEWAY:
1980         case STIMULUS_DTMF_ATXFER_SWAP:
1981         case STIMULUS_TRANSFER_TARGET_HANGUP:
1982         case STIMULUS_TRANSFER_TARGET_ANSWER:
1983         case STIMULUS_TRANSFERER_HANGUP:
1984         default:
1985                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
1986                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
1987                 return props->state;
1988         }
1989 }
1990
1991 static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
1992 {
1993         bridge_hold(props->transferee_bridge);
1994         return 0;
1995 }
1996
1997 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
1998                 enum attended_transfer_stimulus stimulus)
1999 {
2000         bridge_unhold(props->transferee_bridge);
2001         switch (stimulus) {
2002         case STIMULUS_TRANSFEREE_HANGUP:
2003                 return TRANSFER_FAIL;
2004         case STIMULUS_TIMEOUT:
2005                 return TRANSFER_RETRANSFER;
2006         case STIMULUS_NONE:
2007         case STIMULUS_DTMF_ATXFER_ABORT:
2008         case STIMULUS_DTMF_ATXFER_COMPLETE:
2009         case STIMULUS_DTMF_ATXFER_THREEWAY:
2010         case STIMULUS_DTMF_ATXFER_SWAP:
2011         case STIMULUS_TRANSFER_TARGET_HANGUP:
2012         case STIMULUS_TRANSFER_TARGET_ANSWER:
2013         case STIMULUS_TRANSFERER_HANGUP:
2014         case STIMULUS_RECALL_TARGET_HANGUP:
2015         case STIMULUS_RECALL_TARGET_ANSWER:
2016         default:
2017                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2018                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2019                 return props->state;
2020         }
2021 }
2022
2023 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
2024
2025 static int retransfer_enter(struct attended_transfer_properties *props)
2026 {
2027         RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
2028         struct ast_format fmt;
2029         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
2030         int cause;
2031
2032         if (!cap) {
2033                 return -1;
2034         }
2035
2036         snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
2037
2038         ast_format_cap_add(cap, ast_format_set(&fmt, AST_FORMAT_SLINEAR, 0));
2039
2040         /* Get a channel that is the destination we wish to call */
2041         props->recall_target = ast_request("Local", cap, NULL, destination, &cause);
2042         if (!props->recall_target) {
2043                 ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
2044                 return -1;
2045         }
2046
2047         if (attach_framehook(props, props->recall_target)) {
2048                 ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
2049                 ast_hangup(props->recall_target);
2050                 props->recall_target = NULL;
2051                 return -1;
2052         }
2053
2054         if (ast_call(props->recall_target, destination, 0)) {
2055                 ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
2056                 ast_hangup(props->recall_target);
2057                 props->recall_target = NULL;
2058                 return -1;
2059         }
2060
2061         ast_channel_ref(props->recall_target);
2062         if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL, 1)) {
2063                 ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
2064                 ast_hangup(props->recall_target);
2065                 return -1;
2066         }
2067
2068         return 0;
2069 }
2070
2071 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
2072                 enum attended_transfer_stimulus stimulus)
2073 {
2074         switch (stimulus) {
2075         case STIMULUS_TRANSFEREE_HANGUP:
2076                 return TRANSFER_FAIL;
2077         case STIMULUS_TIMEOUT:
2078                 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
2079         case STIMULUS_RECALL_TARGET_HANGUP:
2080                 props->recall_target = ast_channel_unref(props->recall_target);
2081                 if (props->atxferloopdelay) {
2082                         return TRANSFER_WAIT_TO_RECALL;
2083                 }
2084                 return TRANSFER_RECALLING;
2085         case STIMULUS_RECALL_TARGET_ANSWER:
2086                 return TRANSFER_RESUME;
2087         case STIMULUS_NONE:
2088         case STIMULUS_DTMF_ATXFER_ABORT:
2089         case STIMULUS_DTMF_ATXFER_COMPLETE:
2090         case STIMULUS_DTMF_ATXFER_THREEWAY:
2091         case STIMULUS_DTMF_ATXFER_SWAP:
2092         case STIMULUS_TRANSFER_TARGET_HANGUP:
2093         case STIMULUS_TRANSFER_TARGET_ANSWER:
2094         case STIMULUS_TRANSFERER_HANGUP:
2095         default:
2096                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2097                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2098                 return props->state;
2099         }
2100 }
2101
2102 static int wait_to_recall_enter(struct attended_transfer_properties *props)
2103 {
2104         bridge_hold(props->transferee_bridge);
2105         return 0;
2106 }
2107
2108 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
2109                 enum attended_transfer_stimulus stimulus)
2110 {
2111         bridge_unhold(props->transferee_bridge);
2112         switch (stimulus) {
2113         case STIMULUS_TRANSFEREE_HANGUP:
2114                 return TRANSFER_FAIL;
2115         case STIMULUS_TIMEOUT:
2116                 return TRANSFER_RECALLING;
2117         case STIMULUS_NONE:
2118         case STIMULUS_DTMF_ATXFER_ABORT:
2119         case STIMULUS_DTMF_ATXFER_COMPLETE:
2120         case STIMULUS_DTMF_ATXFER_THREEWAY:
2121         case STIMULUS_DTMF_ATXFER_SWAP:
2122         case STIMULUS_TRANSFER_TARGET_HANGUP:
2123         case STIMULUS_TRANSFER_TARGET_ANSWER:
2124         case STIMULUS_TRANSFERER_HANGUP:
2125         case STIMULUS_RECALL_TARGET_HANGUP:
2126         case STIMULUS_RECALL_TARGET_ANSWER:
2127         default:
2128                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2129                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2130                 return props->state;
2131         }
2132 }
2133
2134 static int fail_enter(struct attended_transfer_properties *props)
2135 {
2136         if (props->transferee_bridge) {
2137                 ast_bridge_destroy(props->transferee_bridge);
2138                 props->transferee_bridge = NULL;
2139         }
2140         return 0;
2141 }
2142
2143 /*!
2144  * \brief DTMF hook when transferer presses abort sequence.
2145  *
2146  * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
2147  */
2148 static int atxfer_abort(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2149 {
2150         struct attended_transfer_properties *props = hook_pvt;
2151
2152         ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
2153         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
2154         return 0;
2155 }
2156
2157 /*!
2158  * \brief DTMF hook when transferer presses complete sequence.
2159  *
2160  * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
2161  */
2162 static int atxfer_complete(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2163 {
2164         struct attended_transfer_properties *props = hook_pvt;
2165
2166         ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
2167         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
2168         return 0;
2169 }
2170
2171 /*!
2172  * \brief DTMF hook when transferer presses threeway sequence.
2173  *
2174  * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
2175  */
2176 static int atxfer_threeway(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2177 {
2178         struct attended_transfer_properties *props = hook_pvt;
2179
2180         ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
2181         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
2182         return 0;
2183 }
2184
2185 /*!
2186  * \brief DTMF hook when transferer presses swap sequence.
2187  *
2188  * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
2189  */
2190 static int atxfer_swap(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2191 {
2192         struct attended_transfer_properties *props = hook_pvt;
2193
2194         ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
2195         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
2196         return 0;
2197 }
2198
2199 /*!
2200  * \brief Hangup hook for transferer channel.
2201  *
2202  * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
2203  */
2204 static int atxfer_transferer_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2205 {
2206         struct attended_transfer_properties *props = hook_pvt;
2207
2208         ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
2209         stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
2210         return 0;
2211 }
2212
2213 /*!
2214  * \brief Frame hook for transfer target channel
2215  *
2216  * This is used to determine if the transfer target or recall target has answered
2217  * the outgoing call.
2218  *
2219  * When an answer is detected, a stimulus is sent to the attended transfer monitor
2220  * thread to indicate that the transfer target or recall target has answered.
2221  *
2222  * \param chan The channel the framehook is attached to.
2223  * \param frame The frame being read or written.
2224  * \param event What is being done with the frame.
2225  * \param data The attended transfer properties.
2226  */
2227 static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
2228                 struct ast_frame *frame, enum ast_framehook_event event, void *data)
2229 {
2230         struct attended_transfer_properties *props = data;
2231
2232         if (event == AST_FRAMEHOOK_EVENT_READ &&
2233                         frame && frame->frametype == AST_FRAME_CONTROL &&
2234                         frame->subclass.integer == AST_CONTROL_ANSWER) {
2235
2236                 ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
2237                 if (props->superstate == SUPERSTATE_TRANSFER) {
2238                         stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
2239                 } else {
2240                         stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
2241                 }
2242                 ast_framehook_detach(chan, props->target_framehook_id);
2243                 props->target_framehook_id = -1;
2244         }
2245
2246         return frame;
2247 }
2248
2249 static void transfer_target_framehook_destroy_cb(void *data)
2250 {
2251         struct attended_transfer_properties *props = data;
2252         ao2_cleanup(props);
2253 }
2254
2255 static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
2256 {
2257         const char *abort_dtmf;
2258         const char *complete_dtmf;
2259         const char *threeway_dtmf;
2260         const char *swap_dtmf;
2261         struct bridge_basic_personality *personality = self->personality;
2262
2263         if (!ast_channel_has_role(bridge_channel->chan, TRANSFERER_ROLE_NAME)) {
2264                 return 0;
2265         }
2266
2267         abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "abort");
2268         complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "complete");
2269         threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "threeway");
2270         swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, TRANSFERER_ROLE_NAME, "swap");
2271
2272         if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2273                         abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
2274                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2275                 return -1;
2276         }
2277         if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2278                         complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
2279                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2280                 return -1;
2281         }
2282         if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2283                         threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
2284                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2285                 return -1;
2286         }
2287         if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2288                         swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
2289                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2290                 return -1;
2291         }
2292         if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
2293                         personality->details[personality->current].pvt, NULL,
2294                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2295                 return -1;
2296         }
2297
2298         return 0;
2299 }
2300
2301 static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2302 {
2303         if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2304                 return;
2305         }
2306
2307         if (self->num_channels == 1) {
2308                 RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
2309
2310                 ast_channel_lock(props->transferer);
2311                 transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
2312                 ast_channel_unlock(props->transferer);
2313
2314                 if (!transferer_bridge_channel) {
2315                         return;
2316                 }
2317
2318                 if (AST_LIST_FIRST(&self->channels) != transferer_bridge_channel) {
2319                         return;
2320                 }
2321         }
2322
2323         /* Reaching this point means that either
2324          * 1) The bridge has no channels in it
2325          * 2) The bridge has one channel, and it's the transferer
2326          * In either case, it indicates that the non-transferer parties
2327          * are no longer in the bridge.
2328          */
2329         if (self == props->transferee_bridge) {
2330                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2331         } else {
2332                 stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
2333         }
2334 }
2335
2336 static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2337 {
2338         if (self == props->target_bridge) {
2339                 /* Once we're in the recall superstate, we no longer care about this bridge */
2340                 return;
2341         }
2342
2343         if (bridge_channel->chan == props->recall_target) {
2344                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
2345                 return;
2346         }
2347
2348         if (self->num_channels == 0) {
2349                 /* Empty bridge means all transferees are gone for sure */
2350                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2351                 return;
2352         }
2353
2354         if (self->num_channels == 1) {
2355                 RAII_VAR(struct ast_bridge_channel *, target_bridge_channel, NULL, ao2_cleanup);
2356                 if (!props->recall_target) {
2357                         /* No recall target means that the pull happened on a transferee. If there's still
2358                          * a channel left in the bridge, we don't need to send a stimulus
2359                          */
2360                         return;
2361                 }
2362
2363                 ast_channel_lock(props->recall_target);
2364                 target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
2365                 ast_channel_unlock(props->recall_target);
2366
2367                 if (!target_bridge_channel) {
2368                         return;
2369                 }
2370
2371                 if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
2372                         stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2373                 }
2374         }
2375 }
2376
2377 static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
2378 {
2379         struct bridge_basic_personality *personality = self->personality;
2380         struct attended_transfer_properties *props = personality->details[personality->current].pvt;
2381
2382         switch (props->superstate) {
2383         case SUPERSTATE_TRANSFER:
2384                 transfer_pull(self, bridge_channel, props);
2385                 break;
2386         case SUPERSTATE_RECALL:
2387                 recall_pull(self, bridge_channel, props);
2388                 break;
2389         }
2390 }
2391
2392 static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
2393 {
2394         RAII_VAR(struct stimulus_list *, list, NULL, ast_free_ptr);
2395         SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
2396
2397         while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
2398                 if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
2399                         ast_cond_wait(&props->cond, lock);
2400                 } else {
2401                         struct timeval relative_timeout;
2402                         struct timeval absolute_timeout;
2403                         struct timespec timeout_arg;
2404
2405                         if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
2406                                 props->start = ast_tvnow();
2407                         }
2408
2409                         if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
2410                                 relative_timeout = ast_samp2tv(props->atxferloopdelay, 1000);
2411                         } else {
2412                                 /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
2413                                 relative_timeout = ast_samp2tv(props->atxfernoanswertimeout, 1000);
2414                         }
2415
2416                         absolute_timeout = ast_tvadd(props->start, relative_timeout);
2417                         timeout_arg.tv_sec = absolute_timeout.tv_sec;
2418                         timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
2419
2420                         if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
2421                                 return STIMULUS_TIMEOUT;
2422                         }
2423                 }
2424         }
2425         return list->stimulus;
2426 }
2427
2428 /*!
2429  * \brief The main loop for the attended transfer monitor thread.
2430  *
2431  * This loop runs continuously until the attended transfer reaches
2432  * a terminal state. Stimuli for changes in the attended transfer
2433  * state are handled in this thread so that all factors in an
2434  * attended transfer can be handled in an orderly fashion.
2435  *
2436  * \param data The attended transfer properties
2437  */
2438 static void *attended_transfer_monitor_thread(void *data)
2439 {
2440         struct attended_transfer_properties *props = data;
2441
2442         for (;;) {
2443                 enum attended_transfer_stimulus stimulus;
2444
2445                 ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
2446
2447                 if (state_properties[props->state].enter &&
2448                                 state_properties[props->state].enter(props)) {
2449                         ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
2450                                         state_properties[props->state].state_name, props);
2451                         break;
2452                 }
2453
2454                 if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
2455                         ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
2456                                         state_properties[props->state].state_name, props);
2457                         break;
2458                 }
2459
2460                 stimulus = wait_for_stimulus(props);
2461
2462                 ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
2463
2464                 ast_assert(state_properties[props->state].exit != NULL);
2465
2466                 props->state = state_properties[props->state].exit(props, stimulus);
2467
2468                 ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
2469         }
2470
2471         attended_transfer_properties_shutdown(props);
2472
2473         return NULL;
2474 }
2475
2476 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
2477 {
2478         struct ast_framehook_interface target_interface = {
2479                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
2480                 .event_cb = transfer_target_framehook_cb,
2481                 .destroy_cb = transfer_target_framehook_destroy_cb,
2482         };
2483
2484         ao2_ref(props, +1);
2485         target_interface.data = props;
2486
2487         props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
2488         if (props->target_framehook_id == -1) {
2489                 ao2_ref(props, -1);
2490                 return -1;
2491         }
2492         return 0;
2493 }
2494
2495 static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
2496 {
2497         const char *atxfer_abort;
2498         const char *atxfer_threeway;
2499         const char *atxfer_complete;
2500         const char *atxfer_swap;
2501         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
2502         SCOPED_CHANNELLOCK(lock, chan);
2503
2504         xfer_cfg = ast_get_chan_features_xfer_config(chan);
2505         if (!xfer_cfg) {
2506                 return -1;
2507         }
2508         if (attended_transfer) {
2509                 atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
2510                 atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
2511                 atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
2512                 atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
2513         } else {
2514                 atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
2515                 atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
2516                 atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
2517                 atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
2518         }
2519
2520         return ast_channel_add_bridge_role(chan, TRANSFERER_ROLE_NAME) ||
2521                 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
2522                 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
2523                 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
2524                 ast_channel_set_bridge_role_option(chan, TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
2525 }
2526
2527 /*!
2528  * \brief Helper function that presents dialtone and grabs extension
2529  *
2530  * \retval 0 on success
2531  * \retval -1 on failure
2532  */
2533 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
2534 {
2535         int res;
2536         int digit_timeout;
2537         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
2538
2539         ast_channel_lock(chan);
2540         xfer_cfg = ast_get_chan_features_xfer_config(chan);
2541         if (!xfer_cfg) {
2542                 ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
2543                 ast_channel_unlock(chan);
2544                 return -1;
2545         }
2546         digit_timeout = xfer_cfg->transferdigittimeout;
2547         ast_channel_unlock(chan);
2548
2549         /* Play the simple "transfer" prompt out and wait */
2550         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
2551         ast_stopstream(chan);
2552         if (res < 0) {
2553                 /* Hangup or error */
2554                 return -1;
2555         }
2556         if (res) {
2557                 /* Store the DTMF digit that interrupted playback of the file. */
2558                 exten[0] = res;
2559         }
2560
2561         /* Drop to dialtone so they can enter the extension they want to transfer to */
2562         res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
2563         if (res < 0) {
2564                 /* Hangup or error */
2565                 res = -1;
2566         } else if (!res) {
2567                 /* 0 for invalid extension dialed. */
2568                 if (ast_strlen_zero(exten)) {
2569                         ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
2570                 } else {
2571                         ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
2572                                 ast_channel_name(chan), exten, context);
2573                 }
2574                 ast_stream_and_wait(chan, "pbx-invalid", AST_DIGIT_NONE);
2575                 res = -1;
2576         } else {
2577                 /* Dialed extension is valid. */
2578                 res = 0;
2579         }
2580         return res;
2581 }
2582
2583 static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
2584 {
2585         ast_channel_lock_both(caller, dest);
2586         ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
2587         ast_channel_inherit_variables(caller, dest);
2588         ast_channel_datastore_inherit(caller, dest);
2589         ast_channel_unlock(dest);
2590         ast_channel_unlock(caller);
2591 }
2592
2593 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
2594 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
2595 {
2596         struct ast_channel *chan;
2597         int cause;
2598
2599         /* Now we request a local channel to prepare to call the destination */
2600         chan = ast_request("Local", ast_channel_nativeformats(caller), caller, destination,
2601                 &cause);
2602         if (!chan) {
2603                 return NULL;
2604         }
2605
2606         /* Who is transferring the call. */
2607         pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
2608
2609         /* To work as an analog to BLINDTRANSFER */
2610         pbx_builtin_setvar_helper(chan, "ATTENDEDTRANSFER", ast_channel_name(caller));
2611
2612         /* Before we actually dial out let's inherit appropriate information. */
2613         copy_caller_data(chan, caller);
2614
2615         return chan;
2616 }
2617
2618 /*!
2619  * \brief Internal built in feature for attended transfers
2620  *
2621  * This hook will set up a thread for monitoring the progress of
2622  * an attended transfer. For more information about attended transfer
2623  * progress, see documentation on the transfer state machine.
2624  *
2625  * \param bridge Bridge where attended transfer DTMF sequence was entered
2626  * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
2627  * \param hook_pvt Structure with further information about the attended transfer
2628  */
2629 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2630 {
2631         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
2632         struct attended_transfer_properties *props;
2633         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
2634         char exten[AST_MAX_EXTENSION] = "";
2635         pthread_t thread;
2636
2637         if (strcmp(bridge->v_table->name, "basic")) {
2638                 ast_log(LOG_ERROR, "Attended transfer attempted on unsupported bridge type '%s'\n", bridge->v_table->name);
2639                 return 0;
2640         }
2641
2642         if (bridge->inhibit_merge) {
2643                 ast_log(LOG_ERROR, "Unable to perform attended transfer since bridge '%s' does not permit merging.\n", bridge->uniqueid);
2644                 return 0;
2645         }
2646
2647         props = attended_transfer_properties_alloc(bridge, bridge_channel->chan,
2648                         attended_transfer ? attended_transfer->context : NULL);
2649
2650         if (!props) {
2651                 ast_log(LOG_ERROR, "Unable to allocate control structure for performing attended transfer\n");
2652                 return 0;
2653         }
2654
2655         if (add_transferer_role(props->transferer, attended_transfer)) {
2656                 ast_log(LOG_ERROR, "Unable to set transferer bridge role properly\n");
2657                 attended_transfer_properties_shutdown(props);
2658                 return 0;
2659         }
2660
2661         ast_bridge_channel_write_hold(bridge_channel, NULL);
2662         props->transferee_bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
2663
2664         /* Grab the extension to transfer to */
2665         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
2666                 ast_log(LOG_WARNING, "Unable to acquire target extension for attended transfer\n");
2667                 ast_bridge_channel_write_unhold(bridge_channel);
2668                 attended_transfer_properties_shutdown(props);
2669                 return 0;
2670         }
2671
2672         ast_string_field_set(props, exten, exten);
2673
2674         /* Fill the variable with the extension and context we want to call */
2675         snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
2676
2677         ast_debug(1, "Attended transfer to '%s'\n", destination);
2678
2679         /* Get a channel that is the destination we wish to call */
2680         props->transfer_target = dial_transfer(bridge_channel->chan, destination);
2681         if (!props->transfer_target) {
2682                 ast_log(LOG_ERROR, "Unable to request outbound channel for attended transfer target\n");
2683                 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
2684                 ast_bridge_channel_write_unhold(bridge_channel);
2685                 attended_transfer_properties_shutdown(props);
2686                 return 0;
2687         }
2688
2689
2690         /* Create a bridge to use to talk to the person we are calling */
2691         props->target_bridge = ast_bridge_basic_new();
2692         if (!props->target_bridge) {
2693                 ast_log(LOG_ERROR, "Unable to create bridge for attended transfer target\n");
2694                 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
2695                 ast_bridge_channel_write_unhold(bridge_channel);
2696                 ast_hangup(props->transfer_target);
2697                 props->transfer_target = NULL;
2698                 attended_transfer_properties_shutdown(props);
2699                 return 0;
2700         }
2701         ast_bridge_merge_inhibit(props->target_bridge, +1);
2702
2703         if (attach_framehook(props, props->transfer_target)) {
2704                 ast_log(LOG_ERROR, "Unable to attach framehook to transfer target\n");
2705                 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
2706                 ast_bridge_channel_write_unhold(bridge_channel);
2707                 ast_hangup(props->transfer_target);
2708                 props->transfer_target = NULL;
2709                 attended_transfer_properties_shutdown(props);
2710                 return 0;
2711         }
2712
2713         bridge_basic_change_personality(props->target_bridge,
2714                         BRIDGE_BASIC_PERSONALITY_ATXFER, props);
2715         bridge_basic_change_personality(bridge,
2716                         BRIDGE_BASIC_PERSONALITY_ATXFER, props);
2717
2718         if (ast_call(props->transfer_target, destination, 0)) {
2719                 ast_log(LOG_ERROR, "Unable to place outbound call to transfer target\n");
2720                 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
2721                 ast_bridge_channel_write_unhold(bridge_channel);
2722                 ast_hangup(props->transfer_target);
2723                 props->transfer_target = NULL;
2724                 attended_transfer_properties_shutdown(props);
2725                 return 0;
2726         }
2727
2728         /* We increase the refcount of the transfer target because ast_bridge_impart() will
2729          * steal the reference we already have. We need to keep a reference, so the only
2730          * choice is to give it a bump
2731          */
2732         ast_channel_ref(props->transfer_target);
2733         if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL, 1)) {
2734                 ast_log(LOG_ERROR, "Unable to place transfer target into bridge\n");
2735                 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
2736                 ast_bridge_channel_write_unhold(bridge_channel);
2737                 ast_hangup(props->transfer_target);
2738                 attended_transfer_properties_shutdown(props);
2739                 return 0;
2740         }
2741
2742         if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
2743                 ast_log(LOG_ERROR, "Unable to create monitoring thread for attended transfer\n");
2744                 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
2745                 ast_bridge_channel_write_unhold(bridge_channel);
2746                 attended_transfer_properties_shutdown(props);
2747                 return 0;
2748         }
2749
2750         /* Once the monitoring thread has been created, it is responsible for destroying all
2751          * of the necessary components.
2752          */
2753         return 0;
2754 }
2755
2756 static void blind_transfer_cb(struct ast_channel *new_channel, void *user_data,
2757                 enum ast_transfer_type transfer_type)
2758 {
2759         struct ast_channel *transferer_channel = user_data;
2760
2761         if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
2762                 copy_caller_data(new_channel, transferer_channel);
2763         }
2764 }
2765
2766 /*! \brief Internal built in feature for blind transfers */
2767 static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2768 {
2769         char exten[AST_MAX_EXTENSION] = "";
2770         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
2771         const char *context;
2772         char *goto_on_blindxfr;
2773
2774         if (strcmp(bridge->v_table->name, "basic")) {
2775                 ast_log(LOG_ERROR, "Blind transfer attempted on unsupported bridge type '%s'\n", bridge->v_table->name);
2776                 return 0;
2777         }
2778
2779         ast_bridge_channel_write_hold(bridge_channel, NULL);
2780
2781         ast_channel_lock(bridge_channel->chan);
2782         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
2783                 blind_transfer ? blind_transfer->context : NULL));
2784         goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
2785                 "GOTO_ON_BLINDXFR"), ""));
2786         ast_channel_unlock(bridge_channel->chan);
2787
2788         /* Grab the extension to transfer to */
2789         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
2790                 ast_bridge_channel_write_unhold(bridge_channel);
2791                 return 0;
2792         }
2793
2794         if (!ast_strlen_zero(goto_on_blindxfr)) {
2795                 ast_debug(1, "After transfer, transferer %s goes to %s\n",
2796                                 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
2797                 ast_bridge_set_after_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
2798         }
2799
2800         if (ast_bridge_transfer_blind(0, bridge_channel->chan, exten, context, blind_transfer_cb,
2801                         bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
2802                         !ast_strlen_zero(goto_on_blindxfr)) {
2803                 ast_bridge_discard_after_goto(bridge_channel->chan);
2804         }
2805
2806         return 0;
2807 }
2808
2809 struct ast_bridge_methods ast_bridge_basic_v_table;
2810 struct ast_bridge_methods personality_normal_v_table;
2811 struct ast_bridge_methods personality_atxfer_v_table;
2812
2813 static void bridge_basic_change_personality(struct ast_bridge *bridge,
2814                 enum bridge_basic_personality_type type, void *user_data)
2815 {
2816         struct bridge_basic_personality *personality = bridge->personality;
2817         SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
2818
2819         remove_hooks_on_personality_change(bridge);
2820
2821         ao2_cleanup(personality->details[personality->current].pvt);
2822         personality->details[personality->current].pvt = NULL;
2823         ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
2824
2825         personality->current = type;
2826         if (user_data) {
2827                 ao2_ref(user_data, +1);
2828         }
2829         personality->details[personality->current].pvt = user_data;
2830         ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
2831         if (personality->details[personality->current].on_personality_change) {
2832                 personality->details[personality->current].on_personality_change(bridge);
2833         }
2834 }
2835
2836 static void personality_destructor(void *obj)
2837 {
2838         struct bridge_basic_personality *personality = obj;
2839         int i;
2840
2841         for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
2842                 ao2_cleanup(personality->details[i].pvt);
2843         }
2844 }
2845
2846 static void on_personality_change_normal(struct ast_bridge *bridge)
2847 {
2848         struct ast_bridge_channel *iter;
2849
2850         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
2851                 if (add_normal_hooks(bridge, iter)) {
2852                         ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
2853                                         ast_channel_name(iter->chan));
2854                 }
2855         }
2856 }
2857
2858 static void init_details(struct personality_details *details,
2859                 enum bridge_basic_personality_type type)
2860 {
2861         switch (type) {
2862         case BRIDGE_BASIC_PERSONALITY_NORMAL:
2863                 details->v_table = &personality_normal_v_table;
2864                 details->bridge_flags = NORMAL_FLAGS;
2865                 details->on_personality_change = on_personality_change_normal;
2866                 break;
2867         case BRIDGE_BASIC_PERSONALITY_ATXFER:
2868                 details->v_table = &personality_atxfer_v_table;
2869                 details->bridge_flags = TRANSFER_FLAGS;
2870                 break;
2871         default:
2872                 ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
2873                 break;
2874         }
2875 }
2876
2877 static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
2878 {
2879         struct bridge_basic_personality *personality;
2880         int i;
2881
2882         if (!bridge) {
2883                 return NULL;
2884         }
2885
2886         personality = ao2_alloc(sizeof(*personality), personality_destructor);
2887         if (!personality) {
2888                 ao2_ref(bridge, -1);
2889                 return NULL;
2890         }
2891         for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
2892                 init_details(&personality->details[i], i);
2893         }
2894         personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
2895         bridge->personality = personality;
2896
2897         return bridge;
2898 }
2899
2900 struct ast_bridge *ast_bridge_basic_new(void)
2901 {
2902         struct ast_bridge *bridge;
2903
2904         bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
2905         bridge = bridge_base_init(bridge,
2906                 AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
2907                         | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS);
2908         bridge = bridge_basic_personality_alloc(bridge);
2909         bridge = bridge_register(bridge);
2910         return bridge;
2911 }
2912
2913 void ast_bridging_init_basic(void)
2914 {
2915         /* Setup bridge basic subclass v_table. */
2916         ast_bridge_basic_v_table = ast_bridge_base_v_table;
2917         ast_bridge_basic_v_table.name = "basic";
2918         ast_bridge_basic_v_table.push = bridge_basic_push;
2919         ast_bridge_basic_v_table.pull = bridge_basic_pull;
2920         ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
2921
2922         personality_normal_v_table = ast_bridge_base_v_table;
2923         personality_normal_v_table.name = "normal";
2924         personality_normal_v_table.push = bridge_personality_normal_push;
2925
2926         personality_atxfer_v_table = ast_bridge_base_v_table;
2927         personality_atxfer_v_table.name = "attended transfer";
2928         personality_atxfer_v_table.push = bridge_personality_atxfer_push;
2929         personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
2930
2931         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
2932         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
2933 }
2934