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