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