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