ast_framehook_attach() must be called with the channel locked.
[asterisk/asterisk.git] / main / bridge_basic.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013 Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief Basic bridge class.  It is a subclass of struct ast_bridge.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *
25  * See Also:
26  * \arg \ref AstCREDITS
27  */
28
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include "asterisk/channel.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/linkedlists.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/bridge_internal.h"
39 #include "asterisk/bridge_basic.h"
40 #include "asterisk/bridge_after.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/features_config.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/file.h"
45 #include "asterisk/app.h"
46 #include "asterisk/dial.h"
47 #include "asterisk/stasis_bridges.h"
48 #include "asterisk/stasis_channels.h"
49 #include "asterisk/features.h"
50 #include "asterisk/format_cache.h"
51 #include "asterisk/test.h"
52
53 #define NORMAL_FLAGS    (AST_BRIDGE_FLAG_DISSOLVE_HANGUP | AST_BRIDGE_FLAG_DISSOLVE_EMPTY \
54                         | AST_BRIDGE_FLAG_SMART)
55
56 #define TRANSFER_FLAGS AST_BRIDGE_FLAG_SMART
57
58 struct attended_transfer_properties;
59
60 enum bridge_basic_personality_type {
61         /*! Index for "normal" basic bridge personality */
62         BRIDGE_BASIC_PERSONALITY_NORMAL,
63         /*! Index for attended transfer basic bridge personality */
64         BRIDGE_BASIC_PERSONALITY_ATXFER,
65         /*! Indicates end of enum. Must always remain the last element */
66         BRIDGE_BASIC_PERSONALITY_END,
67 };
68
69 /*!
70  * \brief Change basic bridge personality
71  *
72  * Changing personalities allows for the bridge to remain in use but have
73  * properties such as its v_table and its flags change.
74  *
75  * \param bridge The bridge
76  * \param type The personality to change the bridge to
77  * \user_data Private data to attach to the personality.
78  */
79 static void bridge_basic_change_personality(struct ast_bridge *bridge,
80                 enum bridge_basic_personality_type type, void *user_data);
81
82 /* ------------------------------------------------------------------- */
83
84 static const struct ast_datastore_info dtmf_features_info = {
85         .type = "bridge-dtmf-features",
86         .destroy = ast_free_ptr,
87 };
88
89 /*!
90  * \internal
91  * \since 12.0.0
92  * \brief read a feature code character and set it on for the give feature_flags struct
93  *
94  * \param feature_flags flags being modifed
95  * \param feature feature code provided - should be an uppercase letter
96  *
97  * \retval 0 if the feature was set successfully
98  * \retval -1 failure because the requested feature code isn't handled by this function
99  */
100 static int set_feature_flag_from_char(struct ast_flags *feature_flags, char feature)
101 {
102         switch (feature) {
103         case 'T':
104                 ast_set_flag(feature_flags, AST_FEATURE_REDIRECT);
105                 return 0;
106         case 'K':
107                 ast_set_flag(feature_flags, AST_FEATURE_PARKCALL);
108                 return 0;
109         case 'H':
110                 ast_set_flag(feature_flags, AST_FEATURE_DISCONNECT);
111                 return 0;
112         case 'W':
113                 ast_set_flag(feature_flags, AST_FEATURE_AUTOMON);
114                 return 0;
115         case 'X':
116                 ast_set_flag(feature_flags, AST_FEATURE_AUTOMIXMON);
117                 return 0;
118         default:
119                 return -1;
120         }
121 }
122
123 /*!
124  * \internal
125  * \since 12.0.0
126  * \brief Write a features string to a string buffer based on the feature flags provided
127  *
128  * \param feature_flags pointer to the feature flags to write from.
129  * \param buffer pointer to a string buffer to write the features
130  * \param buffer_size size of the buffer provided (should be able to fit all feature codes)
131  *
132  * \retval 0 on successful write
133  * \retval -1 failure due to running out of buffer space
134  */
135 static int dtmf_features_flags_to_string(struct ast_flags *feature_flags, char *buffer, size_t buffer_size)
136 {
137         size_t buffer_expended = 0;
138         unsigned int cur_feature;
139         static const struct {
140                 char letter;
141                 unsigned int flag;
142         } associations[] = {
143                 { 'T', AST_FEATURE_REDIRECT },
144                 { 'K', AST_FEATURE_PARKCALL },
145                 { 'H', AST_FEATURE_DISCONNECT },
146                 { 'W', AST_FEATURE_AUTOMON },
147                 { 'X', AST_FEATURE_AUTOMIXMON },
148         };
149
150         for (cur_feature = 0; cur_feature < ARRAY_LEN(associations); cur_feature++) {
151                 if (ast_test_flag(feature_flags, associations[cur_feature].flag)) {
152                         if (buffer_expended == buffer_size - 1) {
153                                 buffer[buffer_expended] = '\0';
154                                 return -1;
155                         }
156                         buffer[buffer_expended++] = associations[cur_feature].letter;
157                 }
158         }
159
160         buffer[buffer_expended] = '\0';
161         return 0;
162 }
163
164 static int build_dtmf_features(struct ast_flags *flags, const char *features)
165 {
166         const char *feature;
167
168         char missing_features[strlen(features) + 1];
169         size_t number_of_missing_features = 0;
170
171         for (feature = features; *feature; feature++) {
172                 if (!isupper(*feature)) {
173                         ast_log(LOG_ERROR, "Features string '%s' rejected because it contains non-uppercase feature.\n", features);
174                         return -1;
175                 }
176
177                 if (set_feature_flag_from_char(flags, *feature)) {
178                         missing_features[number_of_missing_features++] = *feature;
179                 }
180         }
181
182         missing_features[number_of_missing_features] = '\0';
183
184         if (number_of_missing_features) {
185                 ast_log(LOG_WARNING, "Features '%s' from features string '%s' can not be applied.\n", missing_features, features);
186         }
187
188         return 0;
189 }
190
191 int ast_bridge_features_ds_set_string(struct ast_channel *chan, const char *features)
192 {
193         struct ast_flags flags = {0};
194
195         if (build_dtmf_features(&flags, features)) {
196                 return -1;
197         }
198
199         ast_channel_lock(chan);
200         if (ast_bridge_features_ds_set(chan, &flags)) {
201                 ast_channel_unlock(chan);
202                 ast_log(LOG_ERROR, "Failed to apply features datastore for '%s' to channel '%s'\n", features, ast_channel_name(chan));
203                 return -1;
204         }
205         ast_channel_unlock(chan);
206
207         return 0;
208 }
209
210 int ast_bridge_features_ds_get_string(struct ast_channel *chan, char *buffer, size_t buf_size)
211 {
212         struct ast_flags *channel_flags;
213         struct ast_flags held_copy;
214
215         ast_channel_lock(chan);
216         if (!(channel_flags = ast_bridge_features_ds_get(chan))) {
217                 ast_channel_unlock(chan);
218                 return -1;
219         }
220         held_copy = *channel_flags;
221         ast_channel_unlock(chan);
222
223         return dtmf_features_flags_to_string(&held_copy, buffer, buf_size);
224 }
225
226 static int bridge_features_ds_set_full(struct ast_channel *chan, struct ast_flags *flags, int replace)
227 {
228         struct ast_datastore *datastore;
229         struct ast_flags *ds_flags;
230
231         datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
232         if (datastore) {
233                 ds_flags = datastore->data;
234                 if (replace) {
235                         *ds_flags = *flags;
236                 } else {
237                         flags->flags = flags->flags | ds_flags->flags;
238                         *ds_flags = *flags;
239                 }
240                 return 0;
241         }
242
243         datastore = ast_datastore_alloc(&dtmf_features_info, NULL);
244         if (!datastore) {
245                 return -1;
246         }
247
248         ds_flags = ast_malloc(sizeof(*ds_flags));
249         if (!ds_flags) {
250                 ast_datastore_free(datastore);
251                 return -1;
252         }
253
254         *ds_flags = *flags;
255         datastore->data = ds_flags;
256         ast_channel_datastore_add(chan, datastore);
257         return 0;
258 }
259
260 int ast_bridge_features_ds_set(struct ast_channel *chan, struct ast_flags *flags)
261 {
262         return bridge_features_ds_set_full(chan, flags, 1);
263 }
264
265 int ast_bridge_features_ds_append(struct ast_channel *chan, struct ast_flags *flags)
266 {
267         return bridge_features_ds_set_full(chan, flags, 0);
268 }
269
270 struct ast_flags *ast_bridge_features_ds_get(struct ast_channel *chan)
271 {
272         struct ast_datastore *datastore;
273
274         datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
275         if (!datastore) {
276                 return NULL;
277         }
278         return datastore->data;
279 }
280
281 /*!
282  * \internal
283  * \brief Determine if we should dissolve the bridge from a hangup.
284  * \since 12.0.0
285  *
286  * \param bridge_channel Channel executing the feature
287  * \param hook_pvt Private data passed in when the hook was created
288  *
289  * \retval 0 Keep the callback hook.
290  * \retval -1 Remove the callback hook.
291  */
292 static int basic_hangup_hook(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
293 {
294         int bridge_count = 0;
295         struct ast_bridge_channel *iter;
296
297         ast_bridge_channel_lock_bridge(bridge_channel);
298         AST_LIST_TRAVERSE(&bridge_channel->bridge->channels, iter, entry) {
299                 if (iter != bridge_channel && iter->state == BRIDGE_CHANNEL_STATE_WAIT) {
300                         ++bridge_count;
301                 }
302         }
303         if (2 <= bridge_count) {
304                 /* Just allow this channel to leave the multi-party bridge. */
305                 ast_bridge_channel_leave_bridge(bridge_channel,
306                         BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
307         }
308         ast_bridge_unlock(bridge_channel->bridge);
309         return 0;
310 }
311
312 /*!
313  * \brief Details for specific basic bridge personalities
314  */
315 struct personality_details {
316         /*! The v_table to use for this personality */
317         struct ast_bridge_methods *v_table;
318         /*! Flags to set on this type of bridge */
319         unsigned int bridge_flags;
320         /*! User data for this personality. If used, must be an ao2 object */
321         void *pvt;
322         /*! Callback to be called when changing to the personality */
323         void (*on_personality_change)(struct ast_bridge *bridge);
324 };
325
326 /*!
327  * \brief structure that organizes different personalities for basic bridges.
328  */
329 struct bridge_basic_personality {
330         /*! The current bridge personality in use */
331         enum bridge_basic_personality_type current;
332         /*! Array of details for the types of bridge personalities supported */
333         struct personality_details details[BRIDGE_BASIC_PERSONALITY_END];
334 };
335
336 /*
337  * \internal
338  * \brief Get the extension for a given builtin feature.
339  *
340  * \param chan Get the feature extension for this channel.
341  * \param feature_name features.conf name of feature.
342  * \param buf Where to put the extension.
343  * \param len Length of the given extension buffer.
344  *
345  * \retval 0 success
346  * \retval non-zero failiure
347  */
348 static int builtin_feature_get_exten(struct ast_channel *chan, const char *feature_name, char *buf, size_t len)
349 {
350         SCOPED_CHANNELLOCK(lock, chan);
351
352         return ast_get_builtin_feature(chan, feature_name, buf, len);
353 }
354
355 /*!
356  * \internal
357  * \brief Helper to add a builtin DTMF feature hook to the features struct.
358  * \since 12.0.0
359  *
360  * \param features Bridge features to setup.
361  * \param chan Get features from this channel.
362  * \param flags Feature flags on the channel.
363  * \param feature_flag Feature flag to test.
364  * \param feature_name features.conf name of feature.
365  * \param feature_bridge Bridge feature enum to get hook callback.
366  *
367  * \retval 0 on success.
368  * \retval -1 on error.
369  */
370 static int builtin_features_helper(struct ast_bridge_features *features, struct ast_channel *chan,
371         struct ast_flags *flags, unsigned int feature_flag, const char *feature_name, enum ast_bridge_builtin_feature feature_bridge)
372 {
373         char dtmf[AST_FEATURE_MAX_LEN];
374         int res;
375
376         res = 0;
377         if (ast_test_flag(flags, feature_flag)
378                 && !builtin_feature_get_exten(chan, feature_name, dtmf, sizeof(dtmf))
379                 && !ast_strlen_zero(dtmf)) {
380                 res = ast_bridge_features_enable(features, feature_bridge, dtmf, NULL, NULL,
381                         AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
382                 if (res) {
383                         ast_log(LOG_ERROR, "Channel %s: Requested DTMF feature %s not available.\n",
384                                 ast_channel_name(chan), feature_name);
385                 }
386         }
387
388         return res;
389 }
390
391 /*!
392  * \internal
393  * \brief Setup bridge builtin features.
394  * \since 12.0.0
395  *
396  * \param features Bridge features to setup.
397  * \param chan Get features from this channel.
398  *
399  * \retval 0 on success.
400  * \retval -1 on error.
401  */
402 static int setup_bridge_features_builtin(struct ast_bridge_features *features, struct ast_channel *chan)
403 {
404         struct ast_flags *flags;
405         int res;
406
407         ast_channel_lock(chan);
408         flags = ast_bridge_features_ds_get(chan);
409         ast_channel_unlock(chan);
410         if (!flags) {
411                 return 0;
412         }
413
414         res = 0;
415         res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "blindxfer", AST_BRIDGE_BUILTIN_BLINDTRANSFER);
416         res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "atxfer", AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER);
417         res |= builtin_features_helper(features, chan, flags, AST_FEATURE_DISCONNECT, "disconnect", AST_BRIDGE_BUILTIN_HANGUP);
418         res |= builtin_features_helper(features, chan, flags, AST_FEATURE_PARKCALL, "parkcall", AST_BRIDGE_BUILTIN_PARKCALL);
419         res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMON, "automon", AST_BRIDGE_BUILTIN_AUTOMON);
420         res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMIXMON, "automixmon", AST_BRIDGE_BUILTIN_AUTOMIXMON);
421
422         return res ? -1 : 0;
423 }
424
425 struct dynamic_dtmf_hook_run {
426         /*! Offset into app_name[] where the channel name that activated the hook starts. */
427         int activated_offset;
428         /*! Offset into app_name[] where the dynamic feature name starts. */
429         int feature_offset;
430         /*! Offset into app_name[] where the MOH class name starts.  (zero if no MOH) */
431         int moh_offset;
432         /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
433         int app_args_offset;
434         /*! Application name to run. */
435         char app_name[0];
436 };
437
438 static void dynamic_dtmf_hook_callback(struct ast_bridge_channel *bridge_channel,
439         const void *payload, size_t payload_size)
440 {
441         struct ast_channel *chan = bridge_channel->chan;
442         const struct dynamic_dtmf_hook_run *run_data = payload;
443
444         pbx_builtin_setvar_helper(chan, "DYNAMIC_FEATURENAME",
445                 &run_data->app_name[run_data->feature_offset]);
446         pbx_builtin_setvar_helper(chan, "DYNAMIC_WHO_ACTIVATED",
447                 &run_data->app_name[run_data->activated_offset]);
448
449         ast_bridge_channel_run_app(bridge_channel, run_data->app_name,
450                 run_data->app_args_offset ? &run_data->app_name[run_data->app_args_offset] : NULL,
451                 run_data->moh_offset ? &run_data->app_name[run_data->moh_offset] : NULL);
452 }
453
454 struct dynamic_dtmf_hook_data {
455         /*! Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER) */
456         unsigned int flags;
457         /*! Offset into app_name[] where the dynamic feature name starts. */
458         int feature_offset;
459         /*! Offset into app_name[] where the MOH class name starts.  (zero if no MOH) */
460         int moh_offset;
461         /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
462         int app_args_offset;
463         /*! Application name to run. */
464         char app_name[0];
465 };
466
467 /*!
468  * \internal
469  * \brief Activated dynamic DTMF feature hook.
470  * \since 12.0.0
471  *
472  * \param bridge_channel Channel executing the feature
473  * \param hook_pvt Private data passed in when the hook was created
474  *
475  * \retval 0 Keep the callback hook.
476  * \retval -1 Remove the callback hook.
477  */
478 static int dynamic_dtmf_hook_trip(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
479 {
480         struct dynamic_dtmf_hook_data *pvt = hook_pvt;
481         struct dynamic_dtmf_hook_run *run_data;
482         const char *activated_name;
483         size_t len_name;
484         size_t len_args;
485         size_t len_moh;
486         size_t len_feature;
487         size_t len_activated;
488         size_t len_data;
489
490         /* Determine lengths of things. */
491         len_name = strlen(pvt->app_name) + 1;
492         len_args = pvt->app_args_offset ? strlen(&pvt->app_name[pvt->app_args_offset]) + 1 : 0;
493         len_moh = pvt->moh_offset ? strlen(&pvt->app_name[pvt->moh_offset]) + 1 : 0;
494         len_feature = strlen(&pvt->app_name[pvt->feature_offset]) + 1;
495         ast_channel_lock(bridge_channel->chan);
496         activated_name = ast_strdupa(ast_channel_name(bridge_channel->chan));
497         ast_channel_unlock(bridge_channel->chan);
498         len_activated = strlen(activated_name) + 1;
499         len_data = sizeof(*run_data) + len_name + len_args + len_moh + len_feature + len_activated;
500
501         /* Fill in dynamic feature run hook data. */
502         run_data = ast_alloca(len_data);
503         run_data->app_args_offset = len_args ? len_name : 0;
504         run_data->moh_offset = len_moh ? len_name + len_args : 0;
505         run_data->feature_offset = len_name + len_args + len_moh;
506         run_data->activated_offset = len_name + len_args + len_moh + len_feature;
507         strcpy(run_data->app_name, pvt->app_name);/* Safe */
508         if (len_args) {
509                 strcpy(&run_data->app_name[run_data->app_args_offset],
510                         &pvt->app_name[pvt->app_args_offset]);/* Safe */
511         }
512         if (len_moh) {
513                 strcpy(&run_data->app_name[run_data->moh_offset],
514                         &pvt->app_name[pvt->moh_offset]);/* Safe */
515         }
516         strcpy(&run_data->app_name[run_data->feature_offset],
517                 &pvt->app_name[pvt->feature_offset]);/* Safe */
518         strcpy(&run_data->app_name[run_data->activated_offset], activated_name);/* Safe */
519
520         if (ast_test_flag(pvt, AST_FEATURE_FLAG_ONPEER)) {
521                 ast_bridge_channel_write_callback(bridge_channel,
522                         AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA,
523                         dynamic_dtmf_hook_callback, run_data, len_data);
524         } else {
525                 dynamic_dtmf_hook_callback(bridge_channel, run_data, len_data);
526         }
527         return 0;
528 }
529
530 /*!
531  * \internal
532  * \brief Add a dynamic DTMF feature hook to the bridge features.
533  * \since 12.0.0
534  *
535  * \param features Bridge features to setup.
536  * \param flags Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER).
537  * \param dtmf DTMF trigger sequence.
538  * \param feature_name Name of the dynamic feature.
539  * \param app_name Dialplan application name to run.
540  * \param app_args Dialplan application arguments. (Empty or NULL if no arguments)
541  * \param moh_class MOH class to play to peer. (Empty or NULL if no MOH played)
542  *
543  * \retval 0 on success.
544  * \retval -1 on error.
545  */
546 static int dynamic_dtmf_hook_add(struct ast_bridge_features *features, unsigned int flags, const char *dtmf, const char *feature_name, const char *app_name, const char *app_args, const char *moh_class)
547 {
548         struct dynamic_dtmf_hook_data *hook_data;
549         size_t len_name = strlen(app_name) + 1;
550         size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
551         size_t len_moh = ast_strlen_zero(moh_class) ? 0 : strlen(moh_class) + 1;
552         size_t len_feature = strlen(feature_name) + 1;
553         size_t len_data = sizeof(*hook_data) + len_name + len_args + len_moh + len_feature;
554         int res;
555
556         /* Fill in application run hook data. */
557         hook_data = ast_malloc(len_data);
558         if (!hook_data) {
559                 return -1;
560         }
561         hook_data->flags = flags;
562         hook_data->app_args_offset = len_args ? len_name : 0;
563         hook_data->moh_offset = len_moh ? len_name + len_args : 0;
564         hook_data->feature_offset = len_name + len_args + len_moh;
565         strcpy(hook_data->app_name, app_name);/* Safe */
566         if (len_args) {
567                 strcpy(&hook_data->app_name[hook_data->app_args_offset], app_args);/* Safe */
568         }
569         if (len_moh) {
570                 strcpy(&hook_data->app_name[hook_data->moh_offset], moh_class);/* Safe */
571         }
572         strcpy(&hook_data->app_name[hook_data->feature_offset], feature_name);/* Safe */
573
574         res = ast_bridge_dtmf_hook(features, dtmf, dynamic_dtmf_hook_trip, hook_data,
575                 ast_free_ptr,
576                 AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
577         if (res) {
578                 ast_free(hook_data);
579         }
580         return res;
581 }
582
583 static int setup_dynamic_feature(void *obj, void *arg, void *data, int flags)
584 {
585         struct ast_applicationmap_item *item = obj;
586         struct ast_bridge_features *features = arg;
587         int *res = data;
588
589         *res |= dynamic_dtmf_hook_add(features,
590                 item->activate_on_self ? AST_FEATURE_FLAG_ONSELF : AST_FEATURE_FLAG_ONPEER,
591                 item->dtmf, item->name, item->app, item->app_data, item->moh_class);
592
593         return 0;
594 }
595
596 /*!
597  * \internal
598  * \brief Setup bridge dynamic features.
599  * \since 12.0.0
600  *
601  * \param features Bridge features to setup.
602  * \param chan Get features from this channel.
603  *
604  * \retval 0 on success.
605  * \retval -1 on error.
606  */
607 static int setup_bridge_features_dynamic(struct ast_bridge_features *features, struct ast_channel *chan)
608 {
609         struct ao2_container *applicationmap;
610         int res = 0;
611
612         ast_channel_lock(chan);
613         applicationmap = ast_get_chan_applicationmap(chan);
614         ast_channel_unlock(chan);
615         if (applicationmap) {
616                 ao2_callback_data(applicationmap, 0, setup_dynamic_feature, features, &res);
617                 ao2_ref(applicationmap, -1);
618         }
619
620         return res;
621 }
622
623 /*!
624  * \internal
625  * \brief Setup DTMF feature hooks using the channel features datastore property.
626  * \since 12.0.0
627  *
628  * \param bridge_channel What to setup DTMF features on.
629  *
630  * \retval 0 on success.
631  * \retval -1 on error.
632  */
633 static int bridge_basic_setup_features(struct ast_bridge_channel *bridge_channel)
634 {
635         int res = 0;
636
637         res |= setup_bridge_features_builtin(bridge_channel->features, bridge_channel->chan);
638         res |= setup_bridge_features_dynamic(bridge_channel->features, bridge_channel->chan);
639
640         return res;
641 }
642
643 static int add_normal_hooks(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
644 {
645         return ast_bridge_hangup_hook(bridge_channel->features, basic_hangup_hook,
646                         NULL, NULL, AST_BRIDGE_HOOK_REMOVE_ON_PULL)
647                 || bridge_basic_setup_features(bridge_channel);
648 }
649
650 /*!
651  * \internal
652  * \brief ast_bridge basic push method.
653  * \since 12.0.0
654  *
655  * \param self Bridge to operate upon.
656  * \param bridge_channel Bridge channel to push.
657  * \param swap Bridge channel to swap places with if not NULL.
658  *
659  * \note On entry, self is already locked.
660  *
661  * \retval 0 on success
662  * \retval -1 on failure
663  */
664 static int bridge_personality_normal_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
665 {
666         if (add_normal_hooks(self, bridge_channel)) {
667                 return -1;
668         }
669
670         return 0;
671 }
672
673 static int bridge_basic_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
674 {
675         struct bridge_basic_personality *personality = self->personality;
676
677         ast_assert(personality != NULL);
678
679         if (personality->details[personality->current].v_table->push
680                 && personality->details[personality->current].v_table->push(self, bridge_channel, swap)) {
681                 return -1;
682         }
683
684         ast_bridge_channel_update_linkedids(bridge_channel, swap);
685         ast_bridge_channel_update_accountcodes(bridge_channel, swap);
686
687         return ast_bridge_base_v_table.push(self, bridge_channel, swap);
688 }
689
690 static void bridge_basic_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
691 {
692         struct bridge_basic_personality *personality = self->personality;
693
694         ast_assert(personality != NULL);
695
696         if (personality->details[personality->current].v_table->pull) {
697                 personality->details[personality->current].v_table->pull(self, bridge_channel);
698         }
699
700         ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
701
702         ast_bridge_base_v_table.pull(self, bridge_channel);
703 }
704
705 static void bridge_basic_destroy(struct ast_bridge *self)
706 {
707         struct bridge_basic_personality *personality = self->personality;
708
709         ao2_cleanup(personality);
710
711         ast_bridge_base_v_table.destroy(self);
712 }
713
714 /*!
715  * \brief Remove appropriate hooks when basic bridge personality changes
716  *
717  * Hooks that have the AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE flag
718  * set will be removed from all bridge channels in the bridge.
719  *
720  * \param bridge Basic bridge undergoing personality change
721  */
722 static void remove_hooks_on_personality_change(struct ast_bridge *bridge)
723 {
724         struct ast_bridge_channel *iter;
725
726         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
727                 SCOPED_LOCK(lock, iter, ast_bridge_channel_lock, ast_bridge_channel_unlock);
728                 ast_bridge_features_remove(iter->features, AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
729         }
730 }
731
732 /*!
733  * \brief Attended transfer superstates.
734  *
735  * An attended transfer's progress is facilitated by a state machine.
736  * The individual states of the state machine fall into the realm of
737  * one of two superstates.
738  */
739 enum attended_transfer_superstate {
740         /*!
741          * \brief Transfer superstate
742          *
743          * The attended transfer state machine begins in this superstate. The
744          * goal of this state is for a transferer channel to facilitate a
745          * transfer from a transferee to a transfer target.
746          *
747          * There are two bridges used in this superstate. The transferee bridge is
748          * the bridge that the transferer and transferee channels originally
749          * communicate in, and the target bridge is the bridge where the transfer
750          * target is being dialed.
751          *
752          * The transferer channel is capable of moving between the bridges using
753          * the DTMF swap sequence.
754          */
755         SUPERSTATE_TRANSFER,
756         /*!
757          * \brief Recall superstate
758          *
759          * The attended transfer state machine moves to this superstate if
760          * atxferdropcall is set to "no" and the transferer channel hangs up
761          * during a transfer. The goal in this superstate is to call back either
762          * the transfer target or transferer and rebridge with the transferee
763          * channel(s).
764          *
765          * In this superstate, there is only a single bridge used, the original
766          * transferee bridge. Rather than distinguishing between a transferer
767          * and transfer target, all outbound calls are toward a "recall_target"
768          * channel.
769          */
770         SUPERSTATE_RECALL,
771 };
772
773 /*!
774  * The states in the attended transfer state machine.
775  */
776 enum attended_transfer_state {
777         /*!
778          * \brief Calling Target state
779          *
780          * This state describes the initial state of a transfer. The transferer
781          * waits in the transfer target's bridge for the transfer target to answer.
782          *
783          * Superstate: Transfer
784          *
785          * Preconditions:
786          * 1) Transfer target is RINGING
787          * 2) Transferer is in transferee bridge
788          * 3) Transferee is on hold
789          *
790          * Transitions to TRANSFER_CALLING_TARGET:
791          * 1) This is the initial state for an attended transfer.
792          * 2) TRANSFER_HESITANT: Transferer presses DTMF swap sequence
793          *
794          * State operation:
795          * The transferer is moved from the transferee bridge into the transfer
796          * target bridge.
797          *
798          * Transitions from TRANSFER_CALLING_TARGET:
799          * 1) TRANSFER_FAIL: Transferee hangs up.
800          * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
801          * and configured atxferdropcall setting is yes.
802          * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
803          * sequence and configured atxferdroppcall setting is no.
804          * 4) TRANSFER_CONSULTING: Transfer target answers the call.
805          * 5) TRANSFER_REBRIDGE: Transfer target hangs up, call to transfer target
806          * times out, or transferer presses DTMF abort sequence.
807          * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
808          * 7) TRANSFER_HESITANT: Transferer presses DTMF swap sequence.
809          */
810         TRANSFER_CALLING_TARGET,
811         /*!
812          * \brief Hesitant state
813          *
814          * This state only arises if when waiting for the transfer target to
815          * answer, the transferer presses the DTMF swap sequence. This will
816          * cause the transferer to be rebridged with the transferee temporarily.
817          *
818          * Superstate: Transfer
819          *
820          * Preconditions:
821          * 1) Transfer target is in ringing state
822          * 2) Transferer is in transfer target bridge
823          * 3) Transferee is on hold
824          *
825          * Transitions to TRANSFER_HESITANT:
826          * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
827          *
828          * State operation:
829          * The transferer is moved from the transfer target bridge into the
830          * transferee bridge, and the transferee is taken off hold.
831          *
832          * Transitions from TRANSFER_HESITANT:
833          * 1) TRANSFER_FAIL: Transferee hangs up
834          * 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
835          * and configured atxferdropcall setting is yes.
836          * 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
837          * sequence and configured atxferdroppcall setting is no.
838          * 4) TRANSFER_DOUBLECHECKING: Transfer target answers the call
839          * 5) TRANSFER_RESUME: Transfer target hangs up, call to transfer target
840          * times out, or transferer presses DTMF abort sequence.
841          * 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
842          * 7) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
843          */
844         TRANSFER_HESITANT,
845         /*!
846          * \brief Rebridge state
847          *
848          * This is a terminal state that indicates that the transferer needs
849          * to move back to the transferee's bridge. This is a failed attended
850          * transfer result.
851          *
852          * Superstate: Transfer
853          *
854          * Preconditions:
855          * 1) Transferer is in transfer target bridge
856          * 2) Transferee is on hold
857          *
858          * Transitions to TRANSFER_REBRIDGE:
859          * 1) TRANSFER_CALLING_TARGET: Transfer target hangs up, call to transfer target
860          * times out, or transferer presses DTMF abort sequence.
861          * 2) TRANSFER_STATE_CONSULTING: Transfer target hangs up, or transferer presses
862          * DTMF abort sequence.
863          *
864          * State operation:
865          * The transferer channel is moved from the transfer target bridge to the
866          * transferee bridge. The transferee is taken off hold. A stasis transfer
867          * message is published indicating a failed attended transfer.
868          *
869          * Transitions from TRANSFER_REBRIDGE:
870          * None
871          */
872         TRANSFER_REBRIDGE,
873         /*!
874          * \brief Resume state
875          *
876          * This is a terminal state that indicates that the party bridged with the
877          * transferee is the final party to be bridged with that transferee. This state
878          * may come about due to a successful recall or due to a failed transfer.
879          *
880          * Superstate: Transfer or Recall
881          *
882          * Preconditions:
883          * In Transfer Superstate:
884          * 1) Transferer is in transferee bridge
885          * 2) Transferee is not on hold
886          * In Recall Superstate:
887          * 1) The recall target is in the transferee bridge
888          * 2) Transferee is not on hold
889          *
890          * Transitions to TRANSFER_RESUME:
891          * TRANSFER_HESITANT: Transfer target hangs up, call to transfer target times out,
892          * or transferer presses DTMF abort sequence.
893          * TRANSFER_DOUBLECHECKING: Transfer target hangs up or transferer presses DTMF
894          * abort sequence.
895          * TRANSFER_BLOND_NONFINAL: Recall target answers
896          * TRANSFER_RECALLING: Recall target answers
897          * TRANSFER_RETRANSFER: Recall target answers
898          *
899          * State operations:
900          * None
901          *
902          * Transitions from TRANSFER_RESUME:
903          * None
904          */
905         TRANSFER_RESUME,
906         /*!
907          * \brief Threeway state
908          *
909          * This state results when the transferer wishes to have all parties involved
910          * in a transfer to be in the same bridge together.
911          *
912          * Superstate: Transfer
913          *
914          * Preconditions:
915          * 1) Transfer target state is either RINGING or UP
916          * 2) Transferer is in either bridge
917          * 3) Transferee is not on hold
918          *
919          * Transitions to TRANSFER_THREEWAY:
920          * 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF threeway sequence.
921          * 2) TRANSFER_HESITANT: Transferer presses DTMF threeway sequence.
922          * 3) TRANSFER_CONSULTING: Transferer presses DTMF threeway sequence.
923          * 4) TRANSFER_DOUBLECHECKING: Transferer presses DTMF threeway sequence.
924          *
925          * State operation:
926          * The transfer target bridge is merged into the transferee bridge.
927          *
928          * Transitions from TRANSFER_THREEWAY:
929          * None.
930          */
931         TRANSFER_THREEWAY,
932         /*!
933          * \brief Consulting state
934          *
935          * This state describes the case where the transferer and transfer target
936          * are able to converse in the transfer target's bridge prior to completing
937          * the transfer.
938          *
939          * Superstate: Transfer
940          *
941          * Preconditions:
942          * 1) Transfer target is UP
943          * 2) Transferer is in target bridge
944          * 3) Transferee is on hold
945          *
946          * Transitions to TRANSFER_CONSULTING:
947          * 1) TRANSFER_CALLING_TARGET: Transfer target answers.
948          * 2) TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
949          *
950          * State operations:
951          * None.
952          *
953          * Transitions from TRANSFER_CONSULTING:
954          * TRANSFER_COMPLETE: Transferer hangs up or transferer presses DTMF complete sequence.
955          * TRANSFER_REBRIDGE: Transfer target hangs up or transferer presses DTMF abort sequence.
956          * TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
957          * TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
958          */
959         TRANSFER_CONSULTING,
960         /*!
961          * \brief Double-checking state
962          *
963          * This state describes the case where the transferer and transferee are
964          * able to converse in the transferee's bridge prior to completing the transfer. The
965          * difference between this and TRANSFER_HESITANT is that the transfer target is
966          * UP in this case.
967          *
968          * Superstate: Transfer
969          *
970          * Preconditions:
971          * 1) Transfer target is UP and on hold
972          * 2) Transferer is in transferee bridge
973          * 3) Transferee is off hold
974          *
975          * Transitions to TRANSFER_DOUBLECHECKING:
976          * 1) TRANSFER_HESITANT: Transfer target answers.
977          * 2) TRANSFER_CONSULTING: Transferer presses DTMF swap sequence.
978          *
979          * State operations:
980          * None.
981          *
982          * Transitions from TRANSFER_DOUBLECHECKING:
983          * 1) TRANSFER_FAIL: Transferee hangs up.
984          * 2) TRANSFER_COMPLETE: Transferer hangs up or presses DTMF complete sequence.
985          * 3) TRANSFER_RESUME: Transfer target hangs up or transferer presses DTMF abort sequence.
986          * 4) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
987          * 5) TRANSFER_CONSULTING: Transferer presses the DTMF swap sequence.
988          */
989         TRANSFER_DOUBLECHECKING,
990         /*!
991          * \brief Complete state
992          *
993          * This is a terminal state where a transferer has successfully completed an attended
994          * transfer. This state's goal is to get the transfer target and transferee into
995          * the same bridge and the transferer off the call.
996          *
997          * Superstate: Transfer
998          *
999          * Preconditions:
1000          * 1) Transfer target is UP and off hold.
1001          * 2) Transferer is in either bridge.
1002          * 3) Transferee is off hold.
1003          *
1004          * Transitions to TRANSFER_COMPLETE:
1005          * 1) TRANSFER_CONSULTING: transferer hangs up or presses the DTMF complete sequence.
1006          * 2) TRANSFER_DOUBLECHECKING: transferer hangs up or presses the DTMF complete sequence.
1007          *
1008          * State operation:
1009          * The transfer target bridge is merged into the transferee bridge. The transferer
1010          * channel is kicked out of the bridges as part of the merge.
1011          *
1012          * State operations:
1013          * 1) Merge the transfer target bridge into the transferee bridge,
1014          * excluding the transferer channel from the merge.
1015          * 2) Publish a stasis transfer message.
1016          *
1017          * Exit operations:
1018          * This is a terminal state, so there are no exit operations.
1019          */
1020         TRANSFER_COMPLETE,
1021         /*!
1022          * \brief Blond state
1023          *
1024          * This is a terminal state where a transferer has completed an attended transfer prior
1025          * to the transfer target answering. This state is only entered if atxferdropcall
1026          * is set to 'yes'. This is considered to be a successful attended transfer.
1027          *
1028          * Superstate: Transfer
1029          *
1030          * Preconditions:
1031          * 1) Transfer target is RINGING.
1032          * 2) Transferer is in either bridge.
1033          * 3) Transferee is off hold.
1034          *
1035          * Transitions to TRANSFER_BLOND:
1036          * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
1037          *    atxferdropcall is set to 'yes'.
1038          * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
1039          *    atxferdropcall is set to 'yes'.
1040          *
1041          * State operations:
1042          * The transfer target bridge is merged into the transferee bridge. The transferer
1043          * channel is kicked out of the bridges as part of the merge. A stasis transfer
1044          * publication is sent indicating a successful transfer.
1045          *
1046          * Transitions from TRANSFER_BLOND:
1047          * None
1048          */
1049         TRANSFER_BLOND,
1050         /*!
1051          * \brief Blond non-final state
1052          *
1053          * This state is very similar to the TRANSFER_BLOND state, except that
1054          * this state is entered when atxferdropcall is set to 'no'. This is the
1055          * initial state of the Recall superstate, so state operations mainly involve
1056          * moving to the Recall superstate. This means that the transfer target, that
1057          * is currently ringing is now known as the recall target.
1058          *
1059          * Superstate: Recall
1060          *
1061          * Preconditions:
1062          * 1) Recall target is RINGING.
1063          * 2) Transferee is off hold.
1064          *
1065          * Transitions to TRANSFER_BLOND_NONFINAL:
1066          * 1) TRANSFER_CALLING_TARGET: Transferer hangs up or presses the DTMF complete sequence.
1067          *    atxferdropcall is set to 'no'.
1068          * 2) TRANSFER_HESITANT: Transferer hangs up or presses the DTMF complete sequence.
1069          *    atxferdropcall is set to 'no'.
1070          *
1071          * State operation:
1072          * The superstate of the attended transfer is changed from Transfer to Recall.
1073          * The transfer target bridge is merged into the transferee bridge. The transferer
1074          * channel is kicked out of the bridges as part of the merge.
1075          *
1076          * Transitions from TRANSFER_BLOND_NONFINAL:
1077          * 1) TRANSFER_FAIL: Transferee hangs up
1078          * 2) TRANSFER_RESUME: Recall target answers
1079          * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
1080          */
1081         TRANSFER_BLOND_NONFINAL,
1082         /*!
1083          * \brief Recalling state
1084          *
1085          * This state is entered if the recall target from the TRANSFER_BLOND_NONFINAL
1086          * or TRANSFER_RETRANSFER states hangs up or does not answer. The goal of this
1087          * state is to call back the original transferer in an attempt to recover the
1088          * original call.
1089          *
1090          * Superstate: Recall
1091          *
1092          * Preconditions:
1093          * 1) Recall target is down.
1094          * 2) Transferee is off hold.
1095          *
1096          * Transitions to TRANSFER_RECALLING:
1097          * 1) TRANSFER_BLOND_NONFINAL: Recall target hangs up or time expires.
1098          * 2) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
1099          *    atxferloopdelay is non-zero.
1100          * 3) TRANSFER_WAIT_TO_RECALL: Time expires.
1101          *
1102          * State operation:
1103          * The original transferer becomes the recall target and is called using the Dialing API.
1104          * Ringing is indicated to the transferee.
1105          *
1106          * Transitions from TRANSFER_RECALLING:
1107          * 1) TRANSFER_FAIL:
1108          *    a) Transferee hangs up.
1109          *    b) Recall target hangs up or time expires, and number of recall attempts exceeds atxfercallbackretries
1110          * 2) TRANSFER_WAIT_TO_RETRANSFER: Recall target hangs up or time expires.
1111          *    atxferloopdelay is non-zero.
1112          * 3) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
1113          *    atxferloopdelay is zero.
1114          * 4) TRANSFER_RESUME: Recall target answers.
1115          */
1116         TRANSFER_RECALLING,
1117         /*!
1118          * \brief Wait to Retransfer state
1119          *
1120          * This state is used simply to give a bit of breathing room between attempting
1121          * to call back the original transferer and attempting to call back the original
1122          * transfer target. The transferee hears music on hold during this state as an
1123          * auditory clue that no one is currently being dialed.
1124          *
1125          * Superstate: Recall
1126          *
1127          * Preconditions:
1128          * 1) Recall target is down.
1129          * 2) Transferee is off hold.
1130          *
1131          * Transitions to TRANSFER_WAIT_TO_RETRANSFER:
1132          * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
1133          *    atxferloopdelay is non-zero.
1134          *
1135          * State operation:
1136          * The transferee is placed on hold.
1137          *
1138          * Transitions from TRANSFER_WAIT_TO_RETRANSFER:
1139          * 1) TRANSFER_FAIL: Transferee hangs up.
1140          * 2) TRANSFER_RETRANSFER: Time expires.
1141          */
1142         TRANSFER_WAIT_TO_RETRANSFER,
1143         /*!
1144          * \brief Retransfer state
1145          *
1146          * This state is used in order to attempt to call back the original
1147          * transfer target channel from the transfer. The transferee hears
1148          * ringing during this state as an auditory cue that a party is being
1149          * dialed.
1150          *
1151          * Superstate: Recall
1152          *
1153          * Preconditions:
1154          * 1) Recall target is down.
1155          * 2) Transferee is off hold.
1156          *
1157          * Transitions to TRANSFER_RETRANSFER:
1158          * 1) TRANSFER_RECALLING: Recall target hangs up or time expires.
1159          *    atxferloopdelay is zero.
1160          * 2) TRANSFER_WAIT_TO_RETRANSFER: Time expires.
1161          *
1162          * State operation:
1163          * The original transfer target is requested and is set as the recall target.
1164          * The recall target is called and placed into the transferee bridge.
1165          *
1166          * Transitions from TRANSFER_RETRANSFER:
1167          * 1) TRANSFER_FAIL: Transferee hangs up.
1168          * 2) TRANSFER_WAIT_TO_RECALL: Recall target hangs up or time expires.
1169          *    atxferloopdelay is non-zero.
1170          * 3) TRANSFER_RECALLING: Recall target hangs up or time expires.
1171          *    atxferloopdelay is zero.
1172          */
1173         TRANSFER_RETRANSFER,
1174         /*!
1175          * \brief Wait to recall state
1176          *
1177          * This state is used simply to give a bit of breathing room between attempting
1178          * to call back the original transfer target and attempting to call back the
1179          * original transferer. The transferee hears music on hold during this state as an
1180          * auditory clue that no one is currently being dialed.
1181          *
1182          * Superstate: Recall
1183          *
1184          * Preconditions:
1185          * 1) Recall target is down.
1186          * 2) Transferee is off hold.
1187          *
1188          * Transitions to TRANSFER_WAIT_TO_RECALL:
1189          * 1) TRANSFER_RETRANSFER: Recall target hangs up or time expires.
1190          *    atxferloopdelay is non-zero.
1191          *
1192          * State operation:
1193          * Transferee is placed on hold.
1194          *
1195          * Transitions from TRANSFER_WAIT_TO_RECALL:
1196          * 1) TRANSFER_FAIL: Transferee hangs up
1197          * 2) TRANSFER_RECALLING: Time expires
1198          */
1199         TRANSFER_WAIT_TO_RECALL,
1200         /*!
1201          * \brief Fail state
1202          *
1203          * This state indicates that something occurred during the transfer that
1204          * makes a graceful completion impossible. The most common stimulus for this
1205          * state is when the transferee hangs up.
1206          *
1207          * Superstate: Transfer and Recall
1208          *
1209          * Preconditions:
1210          * None
1211          *
1212          * Transitions to TRANSFER_FAIL:
1213          * 1) TRANSFER_CALLING_TARGET: Transferee hangs up.
1214          * 2) TRANSFER_HESITANT: Transferee hangs up.
1215          * 3) TRANSFER_DOUBLECHECKING: Transferee hangs up.
1216          * 4) TRANSFER_BLOND_NONFINAL: Transferee hangs up.
1217          * 5) TRANSFER_RECALLING:
1218          *    a) Transferee hangs up.
1219          *    b) Recall target hangs up or time expires, and number of
1220          *       recall attempts exceeds atxfercallbackretries.
1221          * 6) TRANSFER_WAIT_TO_RETRANSFER: Transferee hangs up.
1222          * 7) TRANSFER_RETRANSFER: Transferee hangs up.
1223          * 8) TRANSFER_WAIT_TO_RECALL: Transferee hangs up.
1224          *
1225          * State operation:
1226          * A transfer stasis publication is made indicating a failed transfer.
1227          * The transferee bridge is destroyed.
1228          *
1229          * Transitions from TRANSFER_FAIL:
1230          * None.
1231          */
1232         TRANSFER_FAIL,
1233 };
1234
1235 /*!
1236  * \brief Stimuli that can cause transfer state changes
1237  */
1238 enum attended_transfer_stimulus {
1239         /*! No stimulus. This literally can never happen. */
1240         STIMULUS_NONE,
1241         /*! All of the transferee channels have been hung up. */
1242         STIMULUS_TRANSFEREE_HANGUP,
1243         /*! The transferer has hung up. */
1244         STIMULUS_TRANSFERER_HANGUP,
1245         /*! The transfer target channel has hung up. */
1246         STIMULUS_TRANSFER_TARGET_HANGUP,
1247         /*! The transfer target channel has answered. */
1248         STIMULUS_TRANSFER_TARGET_ANSWER,
1249         /*! The recall target channel has hung up. */
1250         STIMULUS_RECALL_TARGET_HANGUP,
1251         /*! The recall target channel has answered. */
1252         STIMULUS_RECALL_TARGET_ANSWER,
1253         /*! The current state's timer has expired. */
1254         STIMULUS_TIMEOUT,
1255         /*! The transferer pressed the abort DTMF sequence. */
1256         STIMULUS_DTMF_ATXFER_ABORT,
1257         /*! The transferer pressed the complete DTMF sequence. */
1258         STIMULUS_DTMF_ATXFER_COMPLETE,
1259         /*! The transferer pressed the three-way DTMF sequence. */
1260         STIMULUS_DTMF_ATXFER_THREEWAY,
1261         /*! The transferer pressed the swap DTMF sequence. */
1262         STIMULUS_DTMF_ATXFER_SWAP,
1263 };
1264
1265 /*!
1266  * \brief String representations of the various stimuli
1267  *
1268  * Used for debugging purposes
1269  */
1270 const char *stimulus_strs[] = {
1271         [STIMULUS_NONE] = "None",
1272         [STIMULUS_TRANSFEREE_HANGUP] = "Transferee Hangup",
1273         [STIMULUS_TRANSFERER_HANGUP] = "Transferer Hangup",
1274         [STIMULUS_TRANSFER_TARGET_HANGUP] = "Transfer Target Hangup",
1275         [STIMULUS_TRANSFER_TARGET_ANSWER] = "Transfer Target Answer",
1276         [STIMULUS_RECALL_TARGET_HANGUP] = "Recall Target Hangup",
1277         [STIMULUS_RECALL_TARGET_ANSWER] = "Recall Target Answer",
1278         [STIMULUS_TIMEOUT] = "Timeout",
1279         [STIMULUS_DTMF_ATXFER_ABORT] = "DTMF Abort",
1280         [STIMULUS_DTMF_ATXFER_COMPLETE] = "DTMF Complete",
1281         [STIMULUS_DTMF_ATXFER_THREEWAY] = "DTMF Threeway",
1282         [STIMULUS_DTMF_ATXFER_SWAP] = "DTMF Swap",
1283 };
1284
1285 struct stimulus_list {
1286         enum attended_transfer_stimulus stimulus;
1287         AST_LIST_ENTRY(stimulus_list) next;
1288 };
1289
1290 /*!
1291  * \brief Collection of data related to an attended transfer attempt
1292  */
1293 struct attended_transfer_properties {
1294         AST_DECLARE_STRING_FIELDS (
1295                 /*! Extension of transfer target */
1296                 AST_STRING_FIELD(exten);
1297                 /*! Context of transfer target */
1298                 AST_STRING_FIELD(context);
1299                 /*! Sound to play when transfer completes */
1300                 AST_STRING_FIELD(xfersound);
1301                 /*! The channel technology of the transferer channel */
1302                 AST_STRING_FIELD(transferer_type);
1303                 /*! The transferer channel address */
1304                 AST_STRING_FIELD(transferer_addr);
1305         );
1306         /*! Condition used to synchronize when stimuli are reported to the monitor thread */
1307         ast_cond_t cond;
1308         /*! The bridge where the transferee resides. This bridge is also the bridge that
1309          * survives a successful attended transfer.
1310          */
1311         struct ast_bridge *transferee_bridge;
1312         /*! The bridge used to place an outbound call to the transfer target. This
1313          * bridge is merged with the transferee_bridge on a successful transfer.
1314          */
1315         struct ast_bridge *target_bridge;
1316         /*! The party that performs the attended transfer. */
1317         struct ast_channel *transferer;
1318         /*! The local channel dialed to reach the transfer target. */
1319         struct ast_channel *transfer_target;
1320         /*! The party that is currently being recalled. Depending on
1321          * the current state, this may be either the party that originally
1322          * was the transferer or the original transfer target.  This is
1323          * set with reference when entering the BLOND_NONFINAL, RECALLING,
1324          * and RETRANSFER states, and the reference released on state exit
1325          * if continuing with recall or retransfer to avoid leak.
1326          */
1327         struct ast_channel *recall_target;
1328         /*! The absolute starting time for running timers */
1329         struct timeval start;
1330         AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
1331         /*! The current state of the attended transfer */
1332         enum attended_transfer_state state;
1333         /*! The current superstate of the attended transfer */
1334         enum attended_transfer_superstate superstate;
1335         /*! Configured atxferdropcall from features.conf */
1336         int atxferdropcall;
1337         /*! Configured atxfercallbackretries from features.conf */
1338         int atxfercallbackretries;
1339         /*! Configured atxferloopdelay from features.conf */
1340         int atxferloopdelay;
1341         /*! Configured atxfernoanswertimeout from features.conf */
1342         int atxfernoanswertimeout;
1343         /*! Count of the number of times that recalls have been attempted */
1344         int retry_attempts;
1345         /*! Framehook ID for outbounc call to transfer target or recall target */
1346         int target_framehook_id;
1347         /*! Dial structure used when recalling transferer channel */
1348         struct ast_dial *dial;
1349         /*! The bridging features the transferer has available */
1350         struct ast_flags transferer_features;
1351         /*! Saved transferer connected line data for recalling the transferer. */
1352         struct ast_party_connected_line original_transferer_colp;
1353 };
1354
1355 static void attended_transfer_properties_destructor(void *obj)
1356 {
1357         struct attended_transfer_properties *props = obj;
1358
1359         ast_debug(1, "Destroy attended transfer properties %p\n", props);
1360
1361         ao2_cleanup(props->target_bridge);
1362         ao2_cleanup(props->transferee_bridge);
1363         /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
1364         ast_channel_cleanup(props->transferer);
1365         ast_channel_cleanup(props->transfer_target);
1366         ast_channel_cleanup(props->recall_target);
1367         ast_party_connected_line_free(&props->original_transferer_colp);
1368         ast_string_field_free_memory(props);
1369         ast_cond_destroy(&props->cond);
1370 }
1371
1372 /*!
1373  * \internal
1374  * \brief Determine the transfer context to use.
1375  * \since 12.0.0
1376  *
1377  * \param transferer Channel initiating the transfer.
1378  * \param context User supplied context if available.  May be NULL.
1379  *
1380  * \return The context to use for the transfer.
1381  */
1382 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
1383 {
1384         if (!ast_strlen_zero(context)) {
1385                 return context;
1386         }
1387         context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
1388         if (!ast_strlen_zero(context)) {
1389                 return context;
1390         }
1391         context = ast_channel_macrocontext(transferer);
1392         if (!ast_strlen_zero(context)) {
1393                 return context;
1394         }
1395         context = ast_channel_context(transferer);
1396         if (!ast_strlen_zero(context)) {
1397                 return context;
1398         }
1399         return "default";
1400 }
1401
1402 /*!
1403  * \brief Allocate and initialize attended transfer properties
1404  *
1405  * \param transferer The channel performing the attended transfer
1406  * \param context Suggestion for what context the transfer target extension can be found in
1407  *
1408  * \retval NULL Failure to allocate or initialize
1409  * \retval non-NULL Newly allocated properties
1410  */
1411 static struct attended_transfer_properties *attended_transfer_properties_alloc(
1412         struct ast_channel *transferer, const char *context)
1413 {
1414         struct attended_transfer_properties *props;
1415         char *tech;
1416         char *addr;
1417         char *serial;
1418         struct ast_features_xfer_config *xfer_cfg;
1419         struct ast_flags *transferer_features;
1420
1421         props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
1422         if (!props) {
1423                 ast_log(LOG_ERROR, "Unable to create props - channel %s, context %s\n",
1424                         ast_channel_name(transferer), context);
1425                 return NULL;
1426         }
1427
1428         ast_cond_init(&props->cond, NULL);
1429
1430         if (ast_string_field_init(props, 64)) {
1431                 ast_log(LOG_ERROR, "Unable to initialize prop fields - channel %s, context %s\n",
1432                         ast_channel_name(transferer), context);
1433                 ao2_ref(props, -1);
1434                 return NULL;
1435         }
1436
1437         props->target_framehook_id = -1;
1438         props->transferer = ast_channel_ref(transferer);
1439
1440         ast_channel_lock(props->transferer);
1441         xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
1442         if (!xfer_cfg) {
1443                 ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
1444                 ast_channel_unlock(props->transferer);
1445                 ao2_ref(props, -1);
1446                 return NULL;
1447         }
1448         transferer_features = ast_bridge_features_ds_get(props->transferer);
1449         if (transferer_features) {
1450                 props->transferer_features = *transferer_features;
1451         }
1452         props->atxferdropcall = xfer_cfg->atxferdropcall;
1453         props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
1454         props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
1455         props->atxferloopdelay = xfer_cfg->atxferloopdelay;
1456         ast_string_field_set(props, context, get_transfer_context(transferer, context));
1457         ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
1458         ao2_ref(xfer_cfg, -1);
1459
1460         /*
1461          * Save the transferee's party information for any recall calls.
1462          * This is the only piece of information needed that gets overwritten
1463          * on the transferer channel by the inital call to the transfer target.
1464          */
1465         ast_party_connected_line_copy(&props->original_transferer_colp,
1466                 ast_channel_connected(props->transferer));
1467
1468         tech = ast_strdupa(ast_channel_name(props->transferer));
1469         addr = strchr(tech, '/');
1470         if (!addr) {
1471                 ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
1472                 ast_channel_unlock(props->transferer);
1473                 ao2_ref(props, -1);
1474                 return NULL;
1475         }
1476         *addr++ = '\0';
1477         serial = strrchr(addr, '-');
1478         if (serial) {
1479                 *serial = '\0';
1480         }
1481         ast_string_field_set(props, transferer_type, tech);
1482         ast_string_field_set(props, transferer_addr, addr);
1483
1484         ast_channel_unlock(props->transferer);
1485
1486         ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
1487                         props, ast_channel_name(props->transferer));
1488         return props;
1489 }
1490
1491 /*!
1492  * \brief Free backlog of stimuli in the queue
1493  */
1494 static void clear_stimulus_queue(struct attended_transfer_properties *props)
1495 {
1496         struct stimulus_list *list;
1497         SCOPED_AO2LOCK(lock, props);
1498
1499         while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
1500                 ast_free(list);
1501         }
1502 }
1503
1504 /*!
1505  * \brief Initiate shutdown of attended transfer properties
1506  *
1507  * Calling this indicates that the attended transfer properties are no longer needed
1508  * because the transfer operation has concluded.
1509  */
1510 static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
1511 {
1512         ast_debug(1, "Shutting down attended transfer %p\n", props);
1513
1514         if (props->transferee_bridge) {
1515                 bridge_basic_change_personality(props->transferee_bridge,
1516                                 BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
1517                 ast_bridge_merge_inhibit(props->transferee_bridge, -1);
1518         }
1519
1520         if (props->target_bridge) {
1521                 ast_bridge_destroy(props->target_bridge, 0);
1522                 props->target_bridge = NULL;
1523         }
1524
1525         if (props->transferer) {
1526                 ast_channel_remove_bridge_role(props->transferer, AST_TRANSFERER_ROLE_NAME);
1527         }
1528
1529         clear_stimulus_queue(props);
1530
1531         ao2_cleanup(props);
1532 }
1533
1534 static void stimulate_attended_transfer(struct attended_transfer_properties *props,
1535                 enum attended_transfer_stimulus stimulus)
1536 {
1537         struct stimulus_list *list;
1538
1539         list = ast_calloc(1, sizeof(*list));
1540         if (!list) {
1541                 ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
1542                 return;
1543         }
1544
1545         list->stimulus = stimulus;
1546         ao2_lock(props);
1547         AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
1548         ast_cond_signal(&props->cond);
1549         ao2_unlock(props);
1550 }
1551
1552 /*!
1553  * \brief Get a desired transfer party for a bridge the transferer is not in.
1554  *
1555  * \param bridge The bridge to get the party from. May be NULL.
1556  * \param[out] party The lone channel in the bridge. Will be set NULL if bridge is NULL or multiple parties are present.
1557  */
1558 static void get_transfer_party_non_transferer_bridge(struct ast_bridge *bridge,
1559                 struct ast_channel **party)
1560 {
1561         if (bridge && bridge->num_channels == 1) {
1562                 *party = ast_channel_ref(AST_LIST_FIRST(&bridge->channels)->chan);
1563         } else {
1564                 *party = NULL;
1565         }
1566 }
1567
1568 /*!
1569  * \brief Get the transferee and transfer target when the transferer is in a bridge with
1570  * one of the desired parties.
1571  *
1572  * \param transferer_bridge The bridge the transferer is in
1573  * \param other_bridge The bridge the transferer is not in. May be NULL.
1574  * \param transferer The transferer party
1575  * \param[out] transferer_peer The party that is in the bridge with the transferer
1576  * \param[out] other_party The party that is in the other_bridge
1577  */
1578 static void get_transfer_parties_transferer_bridge(struct ast_bridge *transferer_bridge,
1579                 struct ast_bridge *other_bridge, struct ast_channel *transferer,
1580                 struct ast_channel **transferer_peer, struct ast_channel **other_party)
1581 {
1582         *transferer_peer = ast_bridge_peer(transferer_bridge, transferer);
1583         get_transfer_party_non_transferer_bridge(other_bridge, other_party);
1584 }
1585
1586 /*!
1587  * \brief determine transferee and transfer target for an attended transfer
1588  *
1589  * In builtin attended transfers, there is a single transferer channel that jumps between
1590  * the two bridges involved. At the time the attended transfer occurs, the transferer could
1591  * be in either bridge, so determining the parties is a bit more complex than normal.
1592  *
1593  * The method used here is to determine which of the two bridges the transferer is in, and
1594  * grabbing the peer from that bridge. The other bridge, if it only has a single channel in it,
1595  * has the other desired channel.
1596  *
1597  * \param transferer The channel performing the transfer
1598  * \param transferee_bridge The bridge that the transferee is in
1599  * \param target_bridge The bridge that the transfer target is in
1600  * \param[out] transferee The transferee channel
1601  * \param[out] transfer_target The transfer target channel
1602  */
1603 static void get_transfer_parties(struct ast_channel *transferer, struct ast_bridge *transferee_bridge,
1604                 struct ast_bridge *target_bridge, struct ast_channel **transferee,
1605                 struct ast_channel **transfer_target)
1606 {
1607         struct ast_bridge *transferer_bridge;
1608
1609         ast_channel_lock(transferer);
1610         transferer_bridge = ast_channel_get_bridge(transferer);
1611         ast_channel_unlock(transferer);
1612
1613         if (transferer_bridge == transferee_bridge) {
1614                 get_transfer_parties_transferer_bridge(transferee_bridge, target_bridge,
1615                                 transferer, transferee, transfer_target);
1616         } else if (transferer_bridge == target_bridge) {
1617                 get_transfer_parties_transferer_bridge(target_bridge, transferee_bridge,
1618                                 transferer, transfer_target, transferee);
1619         } else {
1620                 get_transfer_party_non_transferer_bridge(transferee_bridge, transferee);
1621                 get_transfer_party_non_transferer_bridge(target_bridge, transfer_target);
1622         }
1623
1624         ao2_cleanup(transferer_bridge);
1625 }
1626
1627 /*!
1628  * \brief Send a stasis publication for a successful attended transfer
1629  */
1630 static void publish_transfer_success(struct attended_transfer_properties *props,
1631                 struct ast_channel *transferee_channel, struct ast_channel *target_channel)
1632 {
1633         struct ast_attended_transfer_message *transfer_msg;
1634
1635         transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
1636                         props->transferee_bridge, props->transferer, props->target_bridge,
1637                         transferee_channel, target_channel);
1638
1639         if (!transfer_msg) {
1640                 ast_log(LOG_ERROR, "Unable to publish successful attended transfer from %s\n",
1641                                 ast_channel_name(props->transferer));
1642                 return;
1643         }
1644
1645         ast_attended_transfer_message_add_merge(transfer_msg, props->transferee_bridge);
1646         ast_bridge_publish_attended_transfer(transfer_msg);
1647         ao2_cleanup(transfer_msg);
1648 }
1649
1650 /*!
1651  * \brief Send a stasis publication for an attended transfer that ends in a threeway call
1652  */
1653 static void publish_transfer_threeway(struct attended_transfer_properties *props,
1654                 struct ast_channel *transferee_channel, struct ast_channel *target_channel)
1655 {
1656         struct ast_attended_transfer_message *transfer_msg;
1657
1658         transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
1659                         props->transferee_bridge, props->transferer, props->target_bridge,
1660                         transferee_channel, target_channel);
1661
1662         if (!transfer_msg) {
1663                 ast_log(LOG_ERROR, "Unable to publish successful three-way transfer from %s\n",
1664                                 ast_channel_name(props->transferer));
1665                 return;
1666         }
1667
1668         ast_attended_transfer_message_add_threeway(transfer_msg, props->transferer,
1669                         props->transferee_bridge);
1670         ast_bridge_publish_attended_transfer(transfer_msg);
1671         ao2_cleanup(transfer_msg);
1672 }
1673
1674 /*!
1675  * \brief Send a stasis publication for a failed attended transfer
1676  */
1677 static void publish_transfer_fail(struct attended_transfer_properties *props)
1678 {
1679         struct ast_attended_transfer_message *transfer_msg;
1680
1681         transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
1682                         props->transferee_bridge, props->transferer, props->target_bridge,
1683                         NULL, NULL);
1684
1685         if (!transfer_msg) {
1686                 ast_log(LOG_ERROR, "Unable to publish failed transfer from %s\n",
1687                                 ast_channel_name(props->transferer));
1688                 return;
1689         }
1690
1691         transfer_msg->result = AST_BRIDGE_TRANSFER_FAIL;
1692         ast_bridge_publish_attended_transfer(transfer_msg);
1693         ao2_cleanup(transfer_msg);
1694 }
1695
1696 /*!
1697  * \brief Helper method to play a sound on a channel in a bridge
1698  *
1699  * \param chan The channel to play the sound to
1700  * \param sound The sound to play
1701  */
1702 static void play_sound(struct ast_channel *chan, const char *sound)
1703 {
1704         struct ast_bridge_channel *bridge_channel;
1705
1706         ast_channel_lock(chan);
1707         bridge_channel = ast_channel_get_bridge_channel(chan);
1708         ast_channel_unlock(chan);
1709
1710         if (bridge_channel) {
1711                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
1712                 ao2_ref(bridge_channel, -1);
1713         }
1714 }
1715
1716 /*!
1717  * \brief Helper method to play a fail sound on a channel in a bridge
1718  *
1719  * \param chan The channel to play the fail sound to
1720  */
1721 static void play_failsound(struct ast_channel *chan)
1722 {
1723         char *sound;
1724
1725         ast_channel_lock(chan);
1726         sound = ast_get_chan_features_xferfailsound(chan);
1727         ast_channel_unlock(chan);
1728
1729         if (sound) {
1730                 play_sound(chan, sound);
1731                 ast_free(sound);
1732         }
1733 }
1734
1735 /*!
1736  * \brief Helper method to stream a fail sound on a channel
1737  *
1738  * \param chan The channel to stream the fail sound to
1739  */
1740 static void stream_failsound(struct ast_channel *chan)
1741 {
1742         char *sound;
1743
1744         ast_channel_lock(chan);
1745         sound = ast_get_chan_features_xferfailsound(chan);
1746         ast_channel_unlock(chan);
1747
1748         if (sound) {
1749                 ast_stream_and_wait(chan, sound, AST_DIGIT_NONE);
1750                 ast_free(sound);
1751         }
1752 }
1753
1754 /*!
1755  * \brief Helper method to place a channel in a bridge on hold
1756  */
1757 static void hold(struct ast_channel *chan)
1758 {
1759         struct ast_bridge_channel *bridge_channel;
1760
1761         if (!chan) {
1762                 return;
1763         }
1764
1765         ast_channel_lock(chan);
1766         bridge_channel = ast_channel_get_bridge_channel(chan);
1767         ast_channel_unlock(chan);
1768
1769         if (bridge_channel) {
1770                 ast_bridge_channel_write_hold(bridge_channel, NULL);
1771                 ao2_ref(bridge_channel, -1);
1772         }
1773 }
1774
1775 /*!
1776  * \brief Helper method to take a channel in a bridge off hold
1777  */
1778 static void unhold(struct ast_channel *chan)
1779 {
1780         struct ast_bridge_channel *bridge_channel;
1781
1782         if (!chan) {
1783                 return;
1784         }
1785
1786         ast_channel_lock(chan);
1787         bridge_channel = ast_channel_get_bridge_channel(chan);
1788         ast_channel_unlock(chan);
1789
1790         if (bridge_channel) {
1791                 ast_bridge_channel_write_unhold(bridge_channel);
1792                 ao2_ref(bridge_channel, -1);
1793         }
1794 }
1795
1796 /*!
1797  * \brief Helper method to send a ringing indication to a channel in a bridge
1798  */
1799 static void ringing(struct ast_channel *chan)
1800 {
1801         struct ast_bridge_channel *bridge_channel;
1802
1803         ast_channel_lock(chan);
1804         bridge_channel = ast_channel_get_bridge_channel(chan);
1805         ast_channel_unlock(chan);
1806
1807         if (bridge_channel) {
1808                 ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
1809                 ao2_ref(bridge_channel, -1);
1810         }
1811 }
1812
1813 /*!
1814  * \brief Helper method to send a ringing indication to all channels in a bridge
1815  */
1816 static void bridge_ringing(struct ast_bridge *bridge)
1817 {
1818         struct ast_frame ringing = {
1819                 .frametype = AST_FRAME_CONTROL,
1820                 .subclass.integer = AST_CONTROL_RINGING,
1821         };
1822
1823         ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
1824 }
1825
1826 /*!
1827  * \brief Helper method to send a hold frame to all channels in a bridge
1828  */
1829 static void bridge_hold(struct ast_bridge *bridge)
1830 {
1831         struct ast_frame hold = {
1832                 .frametype = AST_FRAME_CONTROL,
1833                 .subclass.integer = AST_CONTROL_HOLD,
1834         };
1835
1836         ast_bridge_queue_everyone_else(bridge, NULL, &hold);
1837 }
1838
1839 /*!
1840  * \brief Helper method to send an unhold frame to all channels in a bridge
1841  */
1842 static void bridge_unhold(struct ast_bridge *bridge)
1843 {
1844         struct ast_frame unhold = {
1845                 .frametype = AST_FRAME_CONTROL,
1846                 .subclass.integer = AST_CONTROL_UNHOLD,
1847         };
1848
1849         ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
1850 }
1851
1852 /*!
1853  * \brief Wrapper for \ref bridge_do_move
1854  */
1855 static void bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
1856 {
1857         struct ast_bridge_channel *bridge_channel;
1858
1859         ast_bridge_lock_both(src, dest);
1860
1861         ast_channel_lock(channel);
1862         bridge_channel = ast_channel_get_bridge_channel(channel);
1863         ast_channel_unlock(channel);
1864
1865         if (bridge_channel) {
1866                 ao2_lock(bridge_channel);
1867                 bridge_channel->swap = swap;
1868                 ao2_unlock(bridge_channel);
1869
1870                 bridge_do_move(dest, bridge_channel, 1, 0);
1871         }
1872
1873         ast_bridge_unlock(dest);
1874         ast_bridge_unlock(src);
1875
1876         ao2_cleanup(bridge_channel);
1877 }
1878
1879 /*!
1880  * \brief Wrapper for \ref bridge_do_merge
1881  */
1882 static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
1883 {
1884         struct ast_bridge_channel **kick_bridge_channels = num_channels ?
1885                 ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
1886         int i;
1887         int num_bridge_channels = 0;
1888
1889         ast_bridge_lock_both(dest, src);
1890
1891         for (i = 0; i < num_channels; ++i) {
1892                 struct ast_bridge_channel *kick_bridge_channel;
1893
1894                 kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
1895                 if (!kick_bridge_channel) {
1896                         kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
1897                 }
1898
1899                 /* It's possible (and fine) for the bridge channel to be NULL at this point if the
1900                  * channel has hung up already. If that happens, we can just remove it from the list
1901                  * of bridge channels to kick from the bridge
1902                  */
1903                 if (!kick_bridge_channel) {
1904                         continue;
1905                 }
1906
1907                 kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
1908         }
1909
1910         bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
1911         ast_bridge_unlock(dest);
1912         ast_bridge_unlock(src);
1913 }
1914
1915 /*!
1916  * \brief Flags that indicate properties of attended transfer states
1917  */
1918 enum attended_transfer_state_flags {
1919         /*! This state requires that the timer be reset when entering the state */
1920         TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
1921         /*! This state's timer uses atxferloopdelay */
1922         TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
1923         /*! This state's timer uses atxfernoanswertimeout */
1924         TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
1925         /*! This state has a time limit associated with it */
1926         TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
1927                         TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
1928         /*! This state does not transition to any other states */
1929         TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
1930 };
1931
1932 static int calling_target_enter(struct attended_transfer_properties *props);
1933 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
1934                 enum attended_transfer_stimulus stimulus);
1935
1936 static int hesitant_enter(struct attended_transfer_properties *props);
1937 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
1938                 enum attended_transfer_stimulus stimulus);
1939
1940 static int rebridge_enter(struct attended_transfer_properties *props);
1941
1942 static int resume_enter(struct attended_transfer_properties *props);
1943
1944 static int threeway_enter(struct attended_transfer_properties *props);
1945
1946 static int consulting_enter(struct attended_transfer_properties *props);
1947 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
1948                 enum attended_transfer_stimulus stimulus);
1949
1950 static int double_checking_enter(struct attended_transfer_properties *props);
1951 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
1952                 enum attended_transfer_stimulus stimulus);
1953
1954 static int complete_enter(struct attended_transfer_properties *props);
1955
1956 static int blond_enter(struct attended_transfer_properties *props);
1957
1958 static int blond_nonfinal_enter(struct attended_transfer_properties *props);
1959 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
1960                 enum attended_transfer_stimulus stimulus);
1961
1962 static int recalling_enter(struct attended_transfer_properties *props);
1963 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
1964                 enum attended_transfer_stimulus stimulus);
1965
1966 static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
1967 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
1968                 enum attended_transfer_stimulus stimulus);
1969
1970 static int retransfer_enter(struct attended_transfer_properties *props);
1971 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
1972                 enum attended_transfer_stimulus stimulus);
1973
1974 static int wait_to_recall_enter(struct attended_transfer_properties *props);
1975 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
1976                 enum attended_transfer_stimulus stimulus);
1977
1978 static int fail_enter(struct attended_transfer_properties *props);
1979
1980 /*!
1981  * \brief Properties of an attended transfer state
1982  */
1983 struct attended_transfer_state_properties {
1984         /*! The name of the state. Used for debugging */
1985         const char *state_name;
1986         /*! Function used to enter a state */
1987         int (*enter)(struct attended_transfer_properties *props);
1988         /*!
1989          * Function used to exit a state
1990          * This is used both to determine what the next state
1991          * to transition to will be and to perform any cleanup
1992          * necessary before exiting the current state.
1993          */
1994         enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
1995                         enum attended_transfer_stimulus stimulus);
1996         /*! Flags associated with this state */
1997         enum attended_transfer_state_flags flags;
1998 };
1999
2000 static const struct attended_transfer_state_properties state_properties[] = {
2001         [TRANSFER_CALLING_TARGET] = {
2002                 .state_name = "Calling Target",
2003                 .enter = calling_target_enter,
2004                 .exit = calling_target_exit,
2005                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
2006         },
2007         [TRANSFER_HESITANT] = {
2008                 .state_name = "Hesitant",
2009                 .enter = hesitant_enter,
2010                 .exit = hesitant_exit,
2011                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
2012         },
2013         [TRANSFER_REBRIDGE] = {
2014                 .state_name = "Rebridge",
2015                 .enter = rebridge_enter,
2016                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2017         },
2018         [TRANSFER_RESUME] = {
2019                 .state_name = "Resume",
2020                 .enter = resume_enter,
2021                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2022         },
2023         [TRANSFER_THREEWAY] = {
2024                 .state_name = "Threeway",
2025                 .enter = threeway_enter,
2026                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2027         },
2028         [TRANSFER_CONSULTING] = {
2029                 .state_name = "Consulting",
2030                 .enter = consulting_enter,
2031                 .exit = consulting_exit,
2032         },
2033         [TRANSFER_DOUBLECHECKING] = {
2034                 .state_name = "Double Checking",
2035                 .enter = double_checking_enter,
2036                 .exit = double_checking_exit,
2037         },
2038         [TRANSFER_COMPLETE] = {
2039                 .state_name = "Complete",
2040                 .enter = complete_enter,
2041                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2042         },
2043         [TRANSFER_BLOND] = {
2044                 .state_name = "Blond",
2045                 .enter = blond_enter,
2046                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2047         },
2048         [TRANSFER_BLOND_NONFINAL] = {
2049                 .state_name = "Blond Non-Final",
2050                 .enter = blond_nonfinal_enter,
2051                 .exit = blond_nonfinal_exit,
2052                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
2053         },
2054         [TRANSFER_RECALLING] = {
2055                 .state_name = "Recalling",
2056                 .enter = recalling_enter,
2057                 .exit = recalling_exit,
2058                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
2059         },
2060         [TRANSFER_WAIT_TO_RETRANSFER] = {
2061                 .state_name = "Wait to Retransfer",
2062                 .enter = wait_to_retransfer_enter,
2063                 .exit = wait_to_retransfer_exit,
2064                 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
2065         },
2066         [TRANSFER_RETRANSFER] = {
2067                 .state_name = "Retransfer",
2068                 .enter = retransfer_enter,
2069                 .exit = retransfer_exit,
2070                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
2071         },
2072         [TRANSFER_WAIT_TO_RECALL] = {
2073                 .state_name = "Wait to Recall",
2074                 .enter = wait_to_recall_enter,
2075                 .exit = wait_to_recall_exit,
2076                 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
2077         },
2078         [TRANSFER_FAIL] = {
2079                 .state_name = "Fail",
2080                 .enter = fail_enter,
2081                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2082         },
2083 };
2084
2085 static int calling_target_enter(struct attended_transfer_properties *props)
2086 {
2087         bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
2088         return 0;
2089 }
2090
2091 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
2092                 enum attended_transfer_stimulus stimulus)
2093 {
2094         switch (stimulus) {
2095         case STIMULUS_TRANSFEREE_HANGUP:
2096                 play_failsound(props->transferer);
2097                 publish_transfer_fail(props);
2098                 return TRANSFER_FAIL;
2099         case STIMULUS_DTMF_ATXFER_COMPLETE:
2100         case STIMULUS_TRANSFERER_HANGUP:
2101                 bridge_unhold(props->transferee_bridge);
2102                 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
2103         case STIMULUS_TRANSFER_TARGET_ANSWER:
2104                 return TRANSFER_CONSULTING;
2105         case STIMULUS_TRANSFER_TARGET_HANGUP:
2106         case STIMULUS_TIMEOUT:
2107         case STIMULUS_DTMF_ATXFER_ABORT:
2108                 play_failsound(props->transferer);
2109                 return TRANSFER_REBRIDGE;
2110         case STIMULUS_DTMF_ATXFER_THREEWAY:
2111                 bridge_unhold(props->transferee_bridge);
2112                 return TRANSFER_THREEWAY;
2113         case STIMULUS_DTMF_ATXFER_SWAP:
2114                 return TRANSFER_HESITANT;
2115         case STIMULUS_NONE:
2116         case STIMULUS_RECALL_TARGET_ANSWER:
2117         case STIMULUS_RECALL_TARGET_HANGUP:
2118         default:
2119                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2120                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2121                 return props->state;
2122         }
2123 }
2124
2125 static int hesitant_enter(struct attended_transfer_properties *props)
2126 {
2127         bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
2128         unhold(props->transferer);
2129         return 0;
2130 }
2131
2132 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
2133                 enum attended_transfer_stimulus stimulus)
2134 {
2135         switch (stimulus) {
2136         case STIMULUS_TRANSFEREE_HANGUP:
2137                 play_failsound(props->transferer);
2138                 publish_transfer_fail(props);
2139                 return TRANSFER_FAIL;
2140         case STIMULUS_DTMF_ATXFER_COMPLETE:
2141         case STIMULUS_TRANSFERER_HANGUP:
2142                 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
2143         case STIMULUS_TRANSFER_TARGET_ANSWER:
2144                 return TRANSFER_DOUBLECHECKING;
2145         case STIMULUS_TRANSFER_TARGET_HANGUP:
2146         case STIMULUS_TIMEOUT:
2147         case STIMULUS_DTMF_ATXFER_ABORT:
2148                 play_failsound(props->transferer);
2149                 return TRANSFER_RESUME;
2150         case STIMULUS_DTMF_ATXFER_THREEWAY:
2151                 return TRANSFER_THREEWAY;
2152         case STIMULUS_DTMF_ATXFER_SWAP:
2153                 hold(props->transferer);
2154                 return TRANSFER_CALLING_TARGET;
2155         case STIMULUS_NONE:
2156         case STIMULUS_RECALL_TARGET_HANGUP:
2157         case STIMULUS_RECALL_TARGET_ANSWER:
2158         default:
2159                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2160                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2161                 return props->state;
2162         }
2163 }
2164
2165 static int rebridge_enter(struct attended_transfer_properties *props)
2166 {
2167         bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
2168         unhold(props->transferer);
2169         return 0;
2170 }
2171
2172 static int resume_enter(struct attended_transfer_properties *props)
2173 {
2174         return 0;
2175 }
2176
2177 static int threeway_enter(struct attended_transfer_properties *props)
2178 {
2179         struct ast_channel *transferee_channel;
2180         struct ast_channel *target_channel;
2181
2182         get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
2183                         &transferee_channel, &target_channel);
2184         bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
2185         play_sound(props->transfer_target, props->xfersound);
2186         play_sound(props->transferer, props->xfersound);
2187         publish_transfer_threeway(props, transferee_channel, target_channel);
2188
2189         ast_channel_cleanup(transferee_channel);
2190         ast_channel_cleanup(target_channel);
2191         return 0;
2192 }
2193
2194 static int consulting_enter(struct attended_transfer_properties *props)
2195 {
2196         return 0;
2197 }
2198
2199 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
2200                 enum attended_transfer_stimulus stimulus)
2201 {
2202         switch (stimulus) {
2203         case STIMULUS_TRANSFEREE_HANGUP:
2204                 /* This is a one-of-a-kind event. The transferer and transfer target are talking in
2205                  * one bridge, and the transferee has hung up in a separate bridge. In this case, we
2206                  * will change the personality of the transfer target bridge back to normal, and play
2207                  * a sound to the transferer to indicate the transferee is gone.
2208                  */
2209                 bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
2210                 play_failsound(props->transferer);
2211                 ast_bridge_merge_inhibit(props->target_bridge, -1);
2212                 /* These next two lines are here to ensure that our reference to the target bridge
2213                  * is cleaned up properly and that the target bridge is not destroyed when the
2214                  * monitor thread exits
2215                  */
2216                 ao2_ref(props->target_bridge, -1);
2217                 props->target_bridge = NULL;
2218                 return TRANSFER_FAIL;
2219         case STIMULUS_TRANSFERER_HANGUP:
2220         case STIMULUS_DTMF_ATXFER_COMPLETE:
2221                 /* We know the transferer is in the target_bridge, so take the other bridge off hold */
2222                 bridge_unhold(props->transferee_bridge);
2223                 return TRANSFER_COMPLETE;
2224         case STIMULUS_TRANSFER_TARGET_HANGUP:
2225                 return TRANSFER_REBRIDGE;
2226         case STIMULUS_DTMF_ATXFER_ABORT:
2227                 play_failsound(props->transferer);
2228                 return TRANSFER_REBRIDGE;
2229         case STIMULUS_DTMF_ATXFER_THREEWAY:
2230                 bridge_unhold(props->transferee_bridge);
2231                 return TRANSFER_THREEWAY;
2232         case STIMULUS_DTMF_ATXFER_SWAP:
2233                 hold(props->transferer);
2234                 bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
2235                 unhold(props->transferer);
2236                 return TRANSFER_DOUBLECHECKING;
2237         case STIMULUS_NONE:
2238         case STIMULUS_TIMEOUT:
2239         case STIMULUS_TRANSFER_TARGET_ANSWER:
2240         case STIMULUS_RECALL_TARGET_HANGUP:
2241         case STIMULUS_RECALL_TARGET_ANSWER:
2242         default:
2243                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2244                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2245                 return props->state;
2246         }
2247 }
2248
2249 static int double_checking_enter(struct attended_transfer_properties *props)
2250 {
2251         return 0;
2252 }
2253
2254 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
2255                 enum attended_transfer_stimulus stimulus)
2256 {
2257         switch (stimulus) {
2258         case STIMULUS_TRANSFEREE_HANGUP:
2259                 play_failsound(props->transferer);
2260                 publish_transfer_fail(props);
2261                 return TRANSFER_FAIL;
2262         case STIMULUS_TRANSFERER_HANGUP:
2263         case STIMULUS_DTMF_ATXFER_COMPLETE:
2264                 /* We know the transferer is in the transferee, so take the other bridge off hold */
2265                 bridge_unhold(props->target_bridge);
2266                 return TRANSFER_COMPLETE;
2267         case STIMULUS_TRANSFER_TARGET_HANGUP:
2268         case STIMULUS_DTMF_ATXFER_ABORT:
2269                 play_failsound(props->transferer);
2270                 return TRANSFER_RESUME;
2271         case STIMULUS_DTMF_ATXFER_THREEWAY:
2272                 bridge_unhold(props->target_bridge);
2273                 return TRANSFER_THREEWAY;
2274         case STIMULUS_DTMF_ATXFER_SWAP:
2275                 hold(props->transferer);
2276                 bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
2277                 unhold(props->transferer);
2278                 return TRANSFER_CONSULTING;
2279         case STIMULUS_NONE:
2280         case STIMULUS_TIMEOUT:
2281         case STIMULUS_TRANSFER_TARGET_ANSWER:
2282         case STIMULUS_RECALL_TARGET_HANGUP:
2283         case STIMULUS_RECALL_TARGET_ANSWER:
2284         default:
2285                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2286                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2287                 return props->state;
2288         }
2289 }
2290
2291 static int complete_enter(struct attended_transfer_properties *props)
2292 {
2293         struct ast_channel *transferee_channel;
2294         struct ast_channel *target_channel;
2295
2296         get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
2297                         &transferee_channel, &target_channel);
2298         bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
2299         play_sound(props->transfer_target, props->xfersound);
2300         publish_transfer_success(props, transferee_channel, target_channel);
2301
2302         ast_channel_cleanup(transferee_channel);
2303         ast_channel_cleanup(target_channel);
2304         return 0;
2305 }
2306
2307 static int blond_enter(struct attended_transfer_properties *props)
2308 {
2309         struct ast_channel *transferee_channel;
2310         struct ast_channel *target_channel;
2311
2312         get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
2313                         &transferee_channel, &target_channel);
2314         bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
2315         ringing(props->transfer_target);
2316         publish_transfer_success(props, transferee_channel, target_channel);
2317
2318         ast_channel_cleanup(transferee_channel);
2319         ast_channel_cleanup(target_channel);
2320         return 0;
2321 }
2322
2323 static int blond_nonfinal_enter(struct attended_transfer_properties *props)
2324 {
2325         int res;
2326         props->superstate = SUPERSTATE_RECALL;
2327         /* move the transfer target to the recall target along with its reference */
2328         props->recall_target = ast_channel_ref(props->transfer_target);
2329         res = blond_enter(props);
2330         props->transfer_target = ast_channel_unref(props->transfer_target);
2331         return res;
2332 }
2333
2334 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
2335                 enum attended_transfer_stimulus stimulus)
2336 {
2337         switch (stimulus) {
2338         case STIMULUS_TRANSFEREE_HANGUP:
2339                 return TRANSFER_FAIL;
2340         case STIMULUS_RECALL_TARGET_ANSWER:
2341                 return TRANSFER_RESUME;
2342         case STIMULUS_TIMEOUT:
2343                 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
2344         case STIMULUS_RECALL_TARGET_HANGUP:
2345                 props->recall_target = ast_channel_unref(props->recall_target);
2346                 return TRANSFER_RECALLING;
2347         case STIMULUS_NONE:
2348         case STIMULUS_DTMF_ATXFER_ABORT:
2349         case STIMULUS_DTMF_ATXFER_COMPLETE:
2350         case STIMULUS_DTMF_ATXFER_THREEWAY:
2351         case STIMULUS_DTMF_ATXFER_SWAP:
2352         case STIMULUS_TRANSFERER_HANGUP:
2353         case STIMULUS_TRANSFER_TARGET_HANGUP:
2354         case STIMULUS_TRANSFER_TARGET_ANSWER:
2355         default:
2356                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2357                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2358                 return props->state;
2359         }
2360 }
2361
2362 /*!
2363  * \brief Dial callback when attempting to recall the original transferer channel
2364  *
2365  * This is how we can monitor if the recall target has answered or has hung up.
2366  * If one of the two is detected, then an appropriate stimulus is sent to the
2367  * attended transfer monitor thread.
2368  */
2369 static void recall_callback(struct ast_dial *dial)
2370 {
2371         struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
2372
2373         switch (ast_dial_state(dial)) {
2374         default:
2375         case AST_DIAL_RESULT_INVALID:
2376         case AST_DIAL_RESULT_FAILED:
2377         case AST_DIAL_RESULT_TIMEOUT:
2378         case AST_DIAL_RESULT_HANGUP:
2379         case AST_DIAL_RESULT_UNANSWERED:
2380                 /* Failure cases */
2381                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
2382                 break;
2383         case AST_DIAL_RESULT_RINGING:
2384         case AST_DIAL_RESULT_PROGRESS:
2385         case AST_DIAL_RESULT_PROCEEDING:
2386         case AST_DIAL_RESULT_TRYING:
2387                 /* Don't care about these cases */
2388                 break;
2389         case AST_DIAL_RESULT_ANSWERED:
2390                 /* We struck gold! */
2391                 props->recall_target = ast_dial_answered_steal(dial);
2392                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
2393                 break;
2394         }
2395 }
2396
2397 /*!
2398  * \internal
2399  * \brief Setup common things to transferrer and transfer_target recall channels.
2400  *
2401  * \param recall Channel for recalling a party.
2402  * \param transferer Channel supplying recall information.
2403  *
2404  * \details
2405  * Setup callid, variables, datastores, accountcode, and peeraccount.
2406  *
2407  * \pre Both channels are locked on entry.
2408  *
2409  * \pre COLP and CLID on the recall channel are setup by the caller but not
2410  * explicitly published yet.
2411  *
2412  * \return Nothing
2413  */
2414 static void common_recall_channel_setup(struct ast_channel *recall, struct ast_channel *transferer)
2415 {
2416         ast_callid callid;
2417
2418         callid = ast_read_threadstorage_callid();
2419         if (callid) {
2420                 ast_channel_callid_set(recall, callid);
2421         }
2422
2423         ast_channel_inherit_variables(transferer, recall);
2424         ast_channel_datastore_inherit(transferer, recall);
2425
2426         /*
2427          * Stage a snapshot to ensure that a snapshot is always done
2428          * on the recall channel so earler COLP and CLID setup will
2429          * get published.
2430          */
2431         ast_channel_stage_snapshot(recall);
2432         ast_channel_req_accountcodes(recall, transferer, AST_CHANNEL_REQUESTOR_REPLACEMENT);
2433         ast_channel_stage_snapshot_done(recall);
2434 }
2435
2436 static int recalling_enter(struct attended_transfer_properties *props)
2437 {
2438         RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
2439         struct ast_channel *recall;
2440
2441         if (!cap) {
2442                 return -1;
2443         }
2444
2445         ast_format_cap_append(cap, ast_format_slin, 0);
2446
2447         /* When we dial the transfer target, since we are communicating
2448          * with a local channel, we can place the local channel in a bridge
2449          * and then call out to it. When recalling the transferer, though, we
2450          * have to use the dialing API because the channel is not local.
2451          */
2452         props->dial = ast_dial_create();
2453         if (!props->dial) {
2454                 return -1;
2455         }
2456
2457         if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr, NULL)) {
2458                 return -1;
2459         }
2460
2461         if (ast_dial_prerun(props->dial, NULL, cap)) {
2462                 return -1;
2463         }
2464
2465         /*
2466          * Setup callid, variables, datastores, accountcode, peeraccount,
2467          * COLP, and CLID on the recalled transferrer.
2468          */
2469         recall = ast_dial_get_channel(props->dial, 0);
2470         if (!recall) {
2471                 return -1;
2472         }
2473         ast_channel_lock_both(recall, props->transferer);
2474
2475         ast_party_caller_copy(ast_channel_caller(recall),
2476                 ast_channel_caller(props->transferer));
2477         ast_party_connected_line_copy(ast_channel_connected(recall),
2478                 &props->original_transferer_colp);
2479
2480         common_recall_channel_setup(recall, props->transferer);
2481         ast_channel_unlock(recall);
2482         ast_channel_unlock(props->transferer);
2483
2484         ast_dial_set_state_callback(props->dial, recall_callback);
2485
2486         ao2_ref(props, +1);
2487         ast_dial_set_user_data(props->dial, props);
2488
2489         if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
2490                 ao2_ref(props, -1);
2491                 return -1;
2492         }
2493
2494         bridge_ringing(props->transferee_bridge);
2495         return 0;
2496 }
2497
2498 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
2499                 enum attended_transfer_stimulus stimulus)
2500 {
2501         /* No matter what the outcome was, we need to kill off the dial */
2502         ast_dial_join(props->dial);
2503         ast_dial_destroy(props->dial);
2504         props->dial = NULL;
2505         /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
2506         ao2_ref(props, -1);
2507
2508         switch (stimulus) {
2509         case STIMULUS_TRANSFEREE_HANGUP:
2510                 return TRANSFER_FAIL;
2511         case STIMULUS_TIMEOUT:
2512         case STIMULUS_RECALL_TARGET_HANGUP:
2513                 ++props->retry_attempts;
2514                 if (props->retry_attempts >= props->atxfercallbackretries) {
2515                         return TRANSFER_FAIL;
2516                 }
2517                 if (props->atxferloopdelay) {
2518                         return TRANSFER_WAIT_TO_RETRANSFER;
2519                 }
2520                 return TRANSFER_RETRANSFER;
2521         case STIMULUS_RECALL_TARGET_ANSWER:
2522                 /* Setting this datastore up will allow the transferer to have all of his
2523                  * call features set up automatically when the bridge changes back to a
2524                  * normal personality
2525                  */
2526                 ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
2527                 ast_channel_ref(props->recall_target);
2528                 if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
2529                         AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
2530                         ast_hangup(props->recall_target);
2531                         ast_channel_unref(props->recall_target);
2532                         return TRANSFER_FAIL;
2533                 }
2534                 return TRANSFER_RESUME;
2535         case STIMULUS_NONE:
2536         case STIMULUS_DTMF_ATXFER_ABORT:
2537         case STIMULUS_DTMF_ATXFER_COMPLETE:
2538         case STIMULUS_DTMF_ATXFER_THREEWAY:
2539         case STIMULUS_DTMF_ATXFER_SWAP:
2540         case STIMULUS_TRANSFER_TARGET_HANGUP:
2541         case STIMULUS_TRANSFER_TARGET_ANSWER:
2542         case STIMULUS_TRANSFERER_HANGUP:
2543         default:
2544                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2545                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2546                 return props->state;
2547         }
2548 }
2549
2550 static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
2551 {
2552         bridge_hold(props->transferee_bridge);
2553         return 0;
2554 }
2555
2556 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
2557                 enum attended_transfer_stimulus stimulus)
2558 {
2559         bridge_unhold(props->transferee_bridge);
2560         switch (stimulus) {
2561         case STIMULUS_TRANSFEREE_HANGUP:
2562                 return TRANSFER_FAIL;
2563         case STIMULUS_TIMEOUT:
2564                 return TRANSFER_RETRANSFER;
2565         case STIMULUS_NONE:
2566         case STIMULUS_DTMF_ATXFER_ABORT:
2567         case STIMULUS_DTMF_ATXFER_COMPLETE:
2568         case STIMULUS_DTMF_ATXFER_THREEWAY:
2569         case STIMULUS_DTMF_ATXFER_SWAP:
2570         case STIMULUS_TRANSFER_TARGET_HANGUP:
2571         case STIMULUS_TRANSFER_TARGET_ANSWER:
2572         case STIMULUS_TRANSFERER_HANGUP:
2573         case STIMULUS_RECALL_TARGET_HANGUP:
2574         case STIMULUS_RECALL_TARGET_ANSWER:
2575         default:
2576                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2577                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2578                 return props->state;
2579         }
2580 }
2581
2582 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
2583
2584 static int retransfer_enter(struct attended_transfer_properties *props)
2585 {
2586         RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
2587         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
2588         int cause;
2589
2590         if (!cap) {
2591                 return -1;
2592         }
2593
2594         snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
2595
2596         ast_format_cap_append(cap, ast_format_slin, 0);
2597
2598         /* Get a channel that is the destination we wish to call */
2599         props->recall_target = ast_request("Local", cap, NULL, NULL, destination, &cause);
2600         if (!props->recall_target) {
2601                 ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
2602                 return -1;
2603         }
2604
2605         if (attach_framehook(props, props->recall_target)) {
2606                 ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
2607                 ast_hangup(props->recall_target);
2608                 props->recall_target = NULL;
2609                 return -1;
2610         }
2611
2612         /*
2613          * Setup callid, variables, datastores, accountcode, peeraccount,
2614          * and COLP on the recalled transfer target.
2615          */
2616         ast_channel_lock_both(props->recall_target, props->transferer);
2617
2618         ast_party_connected_line_copy(ast_channel_connected(props->recall_target),
2619                 &props->original_transferer_colp);
2620         ast_party_id_reset(&ast_channel_connected(props->recall_target)->priv);
2621
2622         common_recall_channel_setup(props->recall_target, props->recall_target);
2623         ast_channel_unlock(props->recall_target);
2624         ast_channel_unlock(props->transferer);
2625
2626         if (ast_call(props->recall_target, destination, 0)) {
2627                 ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
2628                 ast_hangup(props->recall_target);
2629                 props->recall_target = NULL;
2630                 return -1;
2631         }
2632
2633         ast_channel_ref(props->recall_target);
2634         if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
2635                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
2636                 ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
2637                 ast_hangup(props->recall_target);
2638                 ast_channel_unref(props->recall_target);
2639                 return -1;
2640         }
2641
2642         return 0;
2643 }
2644
2645 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
2646                 enum attended_transfer_stimulus stimulus)
2647 {
2648         switch (stimulus) {
2649         case STIMULUS_TRANSFEREE_HANGUP:
2650                 return TRANSFER_FAIL;
2651         case STIMULUS_TIMEOUT:
2652                 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
2653         case STIMULUS_RECALL_TARGET_HANGUP:
2654                 props->recall_target = ast_channel_unref(props->recall_target);
2655                 if (props->atxferloopdelay) {
2656                         return TRANSFER_WAIT_TO_RECALL;
2657                 }
2658                 return TRANSFER_RECALLING;
2659         case STIMULUS_RECALL_TARGET_ANSWER:
2660                 return TRANSFER_RESUME;
2661         case STIMULUS_NONE:
2662         case STIMULUS_DTMF_ATXFER_ABORT:
2663         case STIMULUS_DTMF_ATXFER_COMPLETE:
2664         case STIMULUS_DTMF_ATXFER_THREEWAY:
2665         case STIMULUS_DTMF_ATXFER_SWAP:
2666         case STIMULUS_TRANSFER_TARGET_HANGUP:
2667         case STIMULUS_TRANSFER_TARGET_ANSWER:
2668         case STIMULUS_TRANSFERER_HANGUP:
2669         default:
2670                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2671                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2672                 return props->state;
2673         }
2674 }
2675
2676 static int wait_to_recall_enter(struct attended_transfer_properties *props)
2677 {
2678         bridge_hold(props->transferee_bridge);
2679         return 0;
2680 }
2681
2682 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
2683                 enum attended_transfer_stimulus stimulus)
2684 {
2685         bridge_unhold(props->transferee_bridge);
2686         switch (stimulus) {
2687         case STIMULUS_TRANSFEREE_HANGUP:
2688                 return TRANSFER_FAIL;
2689         case STIMULUS_TIMEOUT:
2690                 return TRANSFER_RECALLING;
2691         case STIMULUS_NONE:
2692         case STIMULUS_DTMF_ATXFER_ABORT:
2693         case STIMULUS_DTMF_ATXFER_COMPLETE:
2694         case STIMULUS_DTMF_ATXFER_THREEWAY:
2695         case STIMULUS_DTMF_ATXFER_SWAP:
2696         case STIMULUS_TRANSFER_TARGET_HANGUP:
2697         case STIMULUS_TRANSFER_TARGET_ANSWER:
2698         case STIMULUS_TRANSFERER_HANGUP:
2699         case STIMULUS_RECALL_TARGET_HANGUP:
2700         case STIMULUS_RECALL_TARGET_ANSWER:
2701         default:
2702                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2703                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2704                 return props->state;
2705         }
2706 }
2707
2708 static int fail_enter(struct attended_transfer_properties *props)
2709 {
2710         if (props->transferee_bridge) {
2711                 ast_bridge_destroy(props->transferee_bridge, 0);
2712                 props->transferee_bridge = NULL;
2713         }
2714         return 0;
2715 }
2716
2717 /*!
2718  * \brief DTMF hook when transferer presses abort sequence.
2719  *
2720  * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
2721  */
2722 static int atxfer_abort(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2723 {
2724         struct attended_transfer_properties *props = hook_pvt;
2725
2726         ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
2727         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
2728         return 0;
2729 }
2730
2731 /*!
2732  * \brief DTMF hook when transferer presses complete sequence.
2733  *
2734  * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
2735  */
2736 static int atxfer_complete(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2737 {
2738         struct attended_transfer_properties *props = hook_pvt;
2739
2740         ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
2741         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
2742         return 0;
2743 }
2744
2745 /*!
2746  * \brief DTMF hook when transferer presses threeway sequence.
2747  *
2748  * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
2749  */
2750 static int atxfer_threeway(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2751 {
2752         struct attended_transfer_properties *props = hook_pvt;
2753
2754         ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
2755         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
2756         return 0;
2757 }
2758
2759 /*!
2760  * \brief DTMF hook when transferer presses swap sequence.
2761  *
2762  * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
2763  */
2764 static int atxfer_swap(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2765 {
2766         struct attended_transfer_properties *props = hook_pvt;
2767
2768         ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
2769         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
2770         return 0;
2771 }
2772
2773 /*!
2774  * \brief Hangup hook for transferer channel.
2775  *
2776  * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
2777  */
2778 static int atxfer_transferer_hangup(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2779 {
2780         struct attended_transfer_properties *props = hook_pvt;
2781
2782         ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
2783         stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
2784         return 0;
2785 }
2786
2787 /*!
2788  * \brief Frame hook for transfer target channel
2789  *
2790  * This is used to determine if the transfer target or recall target has answered
2791  * the outgoing call.
2792  *
2793  * When an answer is detected, a stimulus is sent to the attended transfer monitor
2794  * thread to indicate that the transfer target or recall target has answered.
2795  *
2796  * \param chan The channel the framehook is attached to.
2797  * \param frame The frame being read or written.
2798  * \param event What is being done with the frame.
2799  * \param data The attended transfer properties.
2800  */
2801 static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
2802                 struct ast_frame *frame, enum ast_framehook_event event, void *data)
2803 {
2804         struct attended_transfer_properties *props = data;
2805
2806         if (event == AST_FRAMEHOOK_EVENT_READ &&
2807                         frame && frame->frametype == AST_FRAME_CONTROL &&
2808                         frame->subclass.integer == AST_CONTROL_ANSWER) {
2809
2810                 ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
2811                 if (props->superstate == SUPERSTATE_TRANSFER) {
2812                         stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
2813                 } else {
2814                         stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
2815                 }
2816                 ast_framehook_detach(chan, props->target_framehook_id);
2817                 props->target_framehook_id = -1;
2818         }
2819
2820         return frame;
2821 }
2822
2823 /*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
2824 static int transfer_target_framehook_consume(void *data, enum ast_frame_type type)
2825 {
2826         return (type == AST_FRAME_CONTROL ? 1 : 0);
2827 }
2828
2829 static void transfer_target_framehook_destroy_cb(void *data)
2830 {
2831         struct attended_transfer_properties *props = data;
2832         ao2_cleanup(props);
2833 }
2834
2835 static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
2836 {
2837         const char *abort_dtmf;
2838         const char *complete_dtmf;
2839         const char *threeway_dtmf;
2840         const char *swap_dtmf;
2841         struct bridge_basic_personality *personality = self->personality;
2842
2843         if (!ast_channel_has_role(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME)) {
2844                 return 0;
2845         }
2846
2847         abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "abort");
2848         complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "complete");
2849         threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "threeway");
2850         swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "swap");
2851
2852         if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2853                         abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
2854                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2855                 return -1;
2856         }
2857         if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2858                         complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
2859                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2860                 return -1;
2861         }
2862         if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2863                         threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
2864                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2865                 return -1;
2866         }
2867         if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2868                         swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
2869                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2870                 return -1;
2871         }
2872         if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
2873                         personality->details[personality->current].pvt, NULL,
2874                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2875                 return -1;
2876         }
2877
2878         return 0;
2879 }
2880
2881 static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2882 {
2883         if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2884                 return;
2885         }
2886
2887         if (self->num_channels == 1) {
2888                 struct ast_bridge_channel *transferer_bridge_channel;
2889                 int not_transferer;
2890
2891                 ast_channel_lock(props->transferer);
2892                 transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
2893                 ast_channel_unlock(props->transferer);
2894
2895                 if (!transferer_bridge_channel) {
2896                         return;
2897                 }
2898
2899                 not_transferer = AST_LIST_FIRST(&self->channels) != transferer_bridge_channel;
2900                 ao2_ref(transferer_bridge_channel, -1);
2901                 if (not_transferer) {
2902                         return;
2903                 }
2904         }
2905
2906         /* Reaching this point means that either
2907          * 1) The bridge has no channels in it
2908          * 2) The bridge has one channel, and it's the transferer
2909          * In either case, it indicates that the non-transferer parties
2910          * are no longer in the bridge.
2911          */
2912         if (self == props->transferee_bridge) {
2913                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2914         } else {
2915                 stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
2916         }
2917 }
2918
2919 static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2920 {
2921         if (self == props->target_bridge) {
2922                 /* Once we're in the recall superstate, we no longer care about this bridge */
2923                 return;
2924         }
2925
2926         if (bridge_channel->chan == props->recall_target) {
2927                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
2928                 return;
2929         }
2930
2931         if (self->num_channels == 0) {
2932                 /* Empty bridge means all transferees are gone for sure */
2933                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2934                 return;
2935         }
2936
2937         if (self->num_channels == 1) {
2938                 struct ast_bridge_channel *target_bridge_channel;
2939
2940                 if (!props->recall_target) {
2941                         /* No recall target means that the pull happened on a transferee. If there's still
2942                          * a channel left in the bridge, we don't need to send a stimulus
2943                          */
2944                         return;
2945                 }
2946
2947                 ast_channel_lock(props->recall_target);
2948                 target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
2949                 ast_channel_unlock(props->recall_target);
2950
2951                 if (target_bridge_channel) {
2952                         if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
2953                                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2954                         }
2955                         ao2_ref(target_bridge_channel, -1);
2956                 }
2957         }
2958 }
2959
2960 static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
2961 {
2962         struct bridge_basic_personality *personality = self->personality;
2963         struct attended_transfer_properties *props = personality->details[personality->current].pvt;
2964
2965         switch (props->superstate) {
2966         case SUPERSTATE_TRANSFER:
2967                 transfer_pull(self, bridge_channel, props);
2968                 break;
2969         case SUPERSTATE_RECALL:
2970                 recall_pull(self, bridge_channel, props);
2971                 break;
2972         }
2973 }
2974
2975 static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
2976 {
2977         enum attended_transfer_stimulus stimulus;
2978         struct stimulus_list *list;
2979         SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
2980
2981         while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
2982                 if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
2983                         ast_cond_wait(&props->cond, lock);
2984                 } else {
2985                         struct timeval relative_timeout = { 0, };
2986                         struct timeval absolute_timeout;
2987                         struct timespec timeout_arg;
2988
2989                         if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
2990                                 props->start = ast_tvnow();
2991                         }
2992
2993                         if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
2994                                 relative_timeout.tv_sec = props->atxferloopdelay;
2995                         } else {
2996                                 /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
2997                                 relative_timeout.tv_sec = props->atxfernoanswertimeout;
2998                         }
2999
3000                         absolute_timeout = ast_tvadd(props->start, relative_timeout);
3001                         timeout_arg.tv_sec = absolute_timeout.tv_sec;
3002                         timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
3003
3004                         if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
3005                                 return STIMULUS_TIMEOUT;
3006                         }
3007                 }
3008         }
3009         stimulus = list->stimulus;
3010         ast_free(list);
3011         return stimulus;
3012 }
3013
3014 /*!
3015  * \brief The main loop for the attended transfer monitor thread.
3016  *
3017  * This loop runs continuously until the attended transfer reaches
3018  * a terminal state. Stimuli for changes in the attended transfer
3019  * state are handled in this thread so that all factors in an
3020  * attended transfer can be handled in an orderly fashion.
3021  *
3022  * \param data The attended transfer properties
3023  */
3024 static void *attended_transfer_monitor_thread(void *data)
3025 {
3026         struct attended_transfer_properties *props = data;
3027         ast_callid callid;
3028
3029         /*
3030          * Set thread callid to the transferer's callid because we
3031          * are doing all this on that channel's behalf.
3032          */
3033         ast_channel_lock(props->transferer);
3034         callid = ast_channel_callid(props->transferer);
3035         ast_channel_unlock(props->transferer);
3036         if (callid) {
3037                 ast_callid_threadassoc_add(callid);
3038         }
3039
3040         for (;;) {
3041                 enum attended_transfer_stimulus stimulus;
3042
3043                 ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
3044
3045                 if (state_properties[props->state].enter &&
3046                                 state_properties[props->state].enter(props)) {
3047                         ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
3048                                         state_properties[props->state].state_name, props);
3049                         break;
3050                 }
3051
3052                 if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
3053                         ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
3054                                         state_properties[props->state].state_name, props);
3055                         break;
3056                 }
3057
3058                 stimulus = wait_for_stimulus(props);
3059
3060                 ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
3061
3062                 ast_assert(state_properties[props->state].exit != NULL);
3063
3064                 props->state = state_properties[props->state].exit(props, stimulus);
3065
3066                 ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
3067         }
3068
3069         attended_transfer_properties_shutdown(props);
3070
3071         if (callid) {
3072                 ast_callid_threadassoc_remove();
3073         }
3074
3075         return NULL;
3076 }
3077
3078 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
3079 {
3080         struct ast_framehook_interface target_interface = {
3081                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
3082                 .event_cb = transfer_target_framehook_cb,
3083                 .destroy_cb = transfer_target_framehook_destroy_cb,
3084                 .consume_cb = transfer_target_framehook_consume,
3085                 .disable_inheritance = 1,
3086         };
3087
3088         ao2_ref(props, +1);
3089         target_interface.data = props;
3090
3091         ast_channel_lock(channel);
3092         props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
3093         ast_channel_unlock(channel);
3094         if (props->target_framehook_id == -1) {
3095                 ao2_ref(props, -1);
3096                 return -1;
3097         }
3098         return 0;
3099 }
3100
3101 static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
3102 {
3103         const char *atxfer_abort;
3104         const char *atxfer_threeway;
3105         const char *atxfer_complete;
3106         const char *atxfer_swap;
3107         struct ast_features_xfer_config *xfer_cfg;
3108         SCOPED_CHANNELLOCK(lock, chan);
3109
3110         xfer_cfg = ast_get_chan_features_xfer_config(chan);
3111         if (!xfer_cfg) {
3112                 return -1;
3113         }
3114         if (attended_transfer) {
3115                 atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
3116                 atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
3117                 atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
3118                 atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
3119         } else {
3120                 atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
3121                 atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
3122                 atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
3123                 atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
3124         }
3125         ao2_ref(xfer_cfg, -1);
3126
3127         return ast_channel_add_bridge_role(chan, AST_TRANSFERER_ROLE_NAME) ||
3128                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
3129                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
3130                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
3131                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
3132 }
3133
3134 /*!
3135  * \brief Helper function that presents dialtone and grabs extension
3136  *
3137  * \retval 0 on success
3138  * \retval -1 on failure
3139  */
3140 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
3141 {
3142         int res;
3143         int digit_timeout;
3144         int attempts = 0;
3145         int max_attempts;
3146         struct ast_features_xfer_config *xfer_cfg;
3147         char *retry_sound;
3148         char *invalid_sound;
3149
3150         ast_channel_lock(chan);
3151         xfer_cfg = ast_get_chan_features_xfer_config(chan);
3152         if (!xfer_cfg) {
3153                 ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
3154                 ast_channel_unlock(chan);
3155                 return -1;
3156         }
3157         digit_timeout = xfer_cfg->transferdigittimeout * 1000;
3158         max_attempts = xfer_cfg->transferdialattempts;
3159         retry_sound = ast_strdupa(xfer_cfg->transferretrysound);
3160         invalid_sound = ast_strdupa(xfer_cfg->transferinvalidsound);
3161         ao2_ref(xfer_cfg, -1);
3162         ast_channel_unlock(chan);
3163
3164         /* Play the simple "transfer" prompt out and wait */
3165         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
3166         ast_stopstream(chan);
3167         if (res < 0) {
3168                 /* Hangup or error */
3169                 return -1;
3170         }
3171         if (res) {
3172                 /* Store the DTMF digit that interrupted playback of the file. */
3173                 exten[0] = res;
3174         }
3175
3176         /* Drop to dialtone so they can enter the extension they want to transfer to */
3177         do {
3178                 ++attempts;
3179
3180                 ast_test_suite_event_notify("TRANSFER_BEGIN_DIAL",
3181                                 "Channel: %s\r\n"
3182                                 "Attempt: %d",
3183                                 ast_channel_name(chan), attempts);
3184                 res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
3185                 ast_test_suite_event_notify("TRANSFER_DIALLED",
3186                                 "Channel: %s\r\n"
3187                                 "Attempt: %d\r\n"
3188                                 "Dialled: %s\r\n"
3189                                 "Result: %s",
3190                                 ast_channel_name(chan), attempts, exten, res > 0 ? "Success" : "Failure");
3191                 if (res < 0) {
3192                         /* Hangup or error */
3193                         res = -1;
3194                 } else if (!res) {
3195                         /* 0 for invalid extension dialed. */
3196                         if (ast_strlen_zero(exten)) {
3197                                 ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
3198                         } else {
3199                                 ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
3200                                         ast_channel_name(chan), exten, context);
3201                         }
3202                         if (attempts < max_attempts) {
3203                                 ast_stream_and_wait(chan, retry_sound, AST_DIGIT_NONE);
3204                         } else {
3205                                 ast_stream_and_wait(chan, invalid_sound, AST_DIGIT_NONE);
3206                         }
3207                         memset(exten, 0, exten_len);
3208                         res = 1;
3209                 } else {
3210                         /* Dialed extension is valid. */
3211                         res = 0;
3212                 }
3213         } while (res > 0 && attempts < max_attempts);
3214
3215         ast_test_suite_event_notify("TRANSFER_DIAL_FINAL",
3216                         "Channel: %s\r\n"
3217                         "Result: %s",
3218                         ast_channel_name(chan), res == 0 ? "Success" : "Failure");
3219
3220         return res ? -1 : 0;
3221 }
3222
3223 static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
3224 {
3225         ast_channel_lock_both(caller, dest);
3226         ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
3227         ast_channel_inherit_variables(caller, dest);
3228         ast_channel_datastore_inherit(caller, dest);
3229         ast_channel_unlock(dest);
3230         ast_channel_unlock(caller);
3231 }
3232
3233 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
3234 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
3235 {
3236         struct ast_channel *chan;
3237         int cause;
3238
3239         /* Now we request a local channel to prepare to call the destination */
3240         chan = ast_request("Local", ast_channel_nativeformats(caller), NULL, caller, destination,
3241                 &cause);
3242         if (!chan) {
3243                 return NULL;
3244         }
3245
3246         ast_channel_lock_both(chan, caller);
3247
3248         ast_channel_req_accountcodes(chan, caller, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
3249
3250         /* Who is transferring the call. */
3251         pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
3252
3253         ast_bridge_set_transfer_variables(chan, ast_channel_name(caller), 1);
3254
3255         ast_channel_unlock(chan);
3256         ast_channel_unlock(caller);
3257
3258         /* Before we actually dial out let's inherit appropriate information. */
3259         copy_caller_data(chan, caller);
3260
3261         return chan;
3262 }
3263
3264 /*!
3265  * \brief Internal built in feature for attended transfers
3266  *
3267  * This hook will set up a thread for monitoring the progress of
3268  * an attended transfer. For more information about attended transfer
3269  * progress, see documentation on the transfer state machine.
3270  *
3271  * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
3272  * \param hook_pvt Structure with further information about the attended transfer
3273  */
3274 static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
3275 {
3276         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
3277         struct attended_transfer_properties *props;
3278         struct ast_bridge *bridge;
3279         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
3280         char exten[AST_MAX_EXTENSION] = "";
3281         pthread_t thread;
3282
3283         /* Inhibit the bridge before we do anything else. */
3284         bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
3285
3286         if (strcmp(bridge->v_table->name, "basic")) {
3287                 ast_log(LOG_ERROR, "Attended transfer attempted on unsupported bridge type '%s'.\n",
3288                         bridge->v_table->name);
3289                 ast_bridge_merge_inhibit(bridge, -1);
3290                 ao2_ref(bridge, -1);
3291                 return 0;
3292         }
3293
3294         /* Was the bridge inhibited before we inhibited it? */
3295         if (1 < bridge->inhibit_merge) {
3296                 /*
3297                  * The peer likely initiated attended transfer at the same time
3298                  * and we lost the race.
3299                  */
3300                 ast_verb(3, "Channel %s: Bridge '%s' does not permit merging at this time.\n",
3301                         ast_channel_name(bridge_channel->chan), bridge->uniqueid);
3302                 ast_bridge_merge_inhibit(bridge, -1);
3303                 ao2_ref(bridge, -1);
3304                 return 0;
3305         }
3306
3307         props = attended_transfer_properties_alloc(bridge_channel->chan,
3308                 attended_transfer ? attended_transfer->context : NULL);
3309         if (!props) {
3310                 ast_log(LOG_ERROR, "Unable to allocate control structure for performing attended transfer.\n");
3311                 ast_bridge_merge_inhibit(bridge, -1);
3312                 ao2_ref(bridge, -1);
3313                 return 0;
3314         }
3315
3316         props->transferee_bridge = bridge;
3317
3318         if (add_transferer_role(props->transferer, attended_transfer)) {
3319                 ast_log(LOG_ERROR, "Unable to set transferrer bridge role.\n");
3320                 attended_transfer_properties_shutdown(props);
3321                 return 0;
3322         }
3323
3324         ast_bridge_channel_write_hold(bridge_channel, NULL);
3325
3326         /* Grab the extension to transfer to */
3327         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
3328                 ast_log(LOG_WARNING, "Unable to acquire target extension for attended transfer.\n");
3329                 ast_bridge_channel_write_unhold(bridge_channel);
3330                 attended_transfer_properties_shutdown(props);
3331                 return 0;
3332         }
3333
3334         ast_string_field_set(props, exten, exten);
3335
3336         /* Fill the variable with the extension and context we want to call */
3337         snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
3338
3339         ast_debug(1, "Attended transfer to '%s'\n", destination);
3340
3341         /* Get a channel that is the destination we wish to call */
3342         props->transfer_target = dial_transfer(bridge_channel->chan, destination);
3343         if (!props->transfer_target) {
3344                 ast_log(LOG_ERROR, "Unable to request outbound channel for attended transfer target.\n");
3345                 stream_failsound(props->transferer);
3346                 ast_bridge_channel_write_unhold(bridge_channel);
3347                 attended_transfer_properties_shutdown(props);
3348                 return 0;
3349         }
3350
3351
3352         /* Create a bridge to use to talk to the person we are calling */
3353         props->target_bridge = ast_bridge_basic_new();
3354         if (!props->target_bridge) {
3355                 ast_log(LOG_ERROR, "Unable to create bridge for attended transfer target.\n");
3356                 stream_failsound(props->transferer);
3357                 ast_bridge_channel_write_unhold(bridge_channel);
3358                 ast_hangup(props->transfer_target);
3359                 props->transfer_target = NULL;
3360                 attended_transfer_properties_shutdown(props);
3361                 return 0;
3362         }
3363         ast_bridge_merge_inhibit(props->target_bridge, +1);
3364
3365         if (attach_framehook(props, props->transfer_target)) {
3366                 ast_log(LOG_ERROR, "Unable to attach framehook to transfer target.\n");
3367                 stream_failsound(props->transferer);
3368                 ast_bridge_channel_write_unhold(bridge_channel);
3369                 ast_hangup(props->transfer_target);
3370                 props->transfer_target = NULL;
3371                 attended_transfer_properties_shutdown(props);
3372                 return 0;
3373         }
3374
3375         bridge_basic_change_personality(props->target_bridge,
3376                         BRIDGE_BASIC_PERSONALITY_ATXFER, props);
3377         bridge_basic_change_personality(bridge,
3378                         BRIDGE_BASIC_PERSONALITY_ATXFER, props);
3379
3380         if (ast_call(props->transfer_target, destination, 0)) {
3381                 ast_log(LOG_ERROR, "Unable to place outbound call to transfer target.\n");
3382                 stream_failsound(props->transferer);
3383                 ast_bridge_channel_write_unhold(bridge_channel);
3384                 ast_hangup(props->transfer_target);
3385                 props->transfer_target = NULL;
3386                 attended_transfer_properties_shutdown(props);
3387                 return 0;
3388         }
3389
3390         /* We increase the refcount of the transfer target because ast_bridge_impart() will
3391          * steal the reference we already have. We need to keep a reference, so the only
3392          * choice is to give it a bump
3393          */
3394         ast_channel_ref(props->transfer_target);
3395         if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL,
3396                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
3397                 ast_log(LOG_ERROR, "Unable to place transfer target into bridge.\n");
3398                 stream_failsound(props->transferer);
3399                 ast_bridge_channel_write_unhold(bridge_channel);
3400                 ast_hangup(props->transfer_target);
3401                 props->transfer_target = NULL;
3402                 attended_transfer_properties_shutdown(props);
3403                 return 0;
3404         }
3405
3406         if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
3407                 ast_log(LOG_ERROR, "Unable to create monitoring thread for attended transfer.\n");
3408                 stream_failsound(props->transferer);
3409                 ast_bridge_channel_write_unhold(bridge_channel);
3410                 attended_transfer_properties_shutdown(props);
3411                 return 0;
3412         }
3413
3414         /* Once the monitoring thread has been created, it is responsible for destroying all
3415          * of the necessary components.
3416          */
3417         return 0;
3418 }
3419
3420 static void blind_transfer_cb(struct ast_channel *new_channel, struct transfer_channel_data *user_data_wrapper,
3421                 enum ast_transfer_type transfer_type)
3422 {
3423         struct ast_channel *transferer_channel = user_data_wrapper->data;
3424
3425         if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
3426                 copy_caller_data(new_channel, transferer_channel);
3427         }
3428 }
3429
3430 /*! \brief Internal built in feature for blind transfers */
3431 static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
3432 {
3433         char xfer_exten[AST_MAX_EXTENSION] = "";
3434         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
3435         const char *xfer_context;
3436         char *goto_on_blindxfr;
3437
3438         ast_bridge_channel_write_hold(bridge_channel, NULL);
3439
3440         ast_channel_lock(bridge_channel->chan);
3441         xfer_context = ast_strdupa(get_transfer_context(bridge_channel->chan,
3442                 blind_transfer ? blind_transfer->context : NULL));
3443         goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
3444                 "GOTO_ON_BLINDXFR"), ""));
3445         ast_channel_unlock(bridge_channel->chan);
3446
3447         /* Grab the extension to transfer to */
3448         if (grab_transfer(bridge_channel->chan, xfer_exten, sizeof(xfer_exten), xfer_context)) {
3449                 ast_bridge_channel_write_unhold(bridge_channel);
3450                 return 0;
3451         }
3452
3453         if (!ast_strlen_zero(goto_on_blindxfr)) {
3454                 const char *chan_context;
3455                 const char *chan_exten;
3456                 int chan_priority;
3457
3458                 ast_debug(1, "After transfer, transferer %s goes to %s\n",
3459                                 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
3460
3461                 ast_channel_lock(bridge_channel->chan);
3462                 chan_context = ast_strdupa(ast_channel_context(bridge_channel->chan));
3463                 chan_exten = ast_strdupa(ast_channel_exten(bridge_channel->chan));
3464                 chan_priority = ast_channel_priority(bridge_channel->chan);
3465                 ast_channel_unlock(bridge_channel->chan);
3466                 ast_bridge_set_after_go_on(bridge_channel->chan,
3467                         chan_context, chan_exten, chan_priority, goto_on_blindxfr);
3468         }
3469
3470         if (ast_bridge_transfer_blind(0, bridge_channel->chan, xfer_exten, xfer_context,
3471                 blind_transfer_cb, bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS
3472                 && !ast_strlen_zero(goto_on_blindxfr)) {
3473                 ast_bridge_discard_after_goto(bridge_channel->chan);
3474         }
3475
3476         return 0;
3477 }
3478
3479 struct ast_bridge_methods ast_bridge_basic_v_table;
3480 struct ast_bridge_methods personality_normal_v_table;
3481 struct ast_bridge_methods personality_atxfer_v_table;
3482
3483 static void bridge_basic_change_personality(struct ast_bridge *bridge,
3484                 enum bridge_basic_personality_type type, void *user_data)
3485 {
3486         struct bridge_basic_personality *personality = bridge->personality;
3487         SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
3488
3489         remove_hooks_on_personality_change(bridge);
3490
3491         ao2_cleanup(personality->details[personality->current].pvt);
3492         personality->details[personality->current].pvt = NULL;
3493         ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
3494
3495         personality->current = type;
3496         if (user_data) {
3497                 ao2_ref(user_data, +1);
3498         }
3499         personality->details[personality->current].pvt = user_data;
3500         ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
3501         if (personality->details[personality->current].on_personality_change) {
3502                 personality->details[personality->current].on_personality_change(bridge);
3503         }
3504 }
3505
3506 static void personality_destructor(void *obj)
3507 {
3508         struct bridge_basic_personality *personality = obj;
3509         int i;
3510
3511         for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
3512                 ao2_cleanup(personality->details[i].pvt);
3513         }
3514 }
3515
3516 static void on_personality_change_normal(struct ast_bridge *bridge)
3517 {
3518         struct ast_bridge_channel *iter;
3519
3520         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
3521                 if (add_normal_hooks(bridge, iter)) {
3522                         ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
3523                                         ast_channel_name(iter->chan));
3524                 }
3525         }
3526 }
3527
3528 static void init_details(struct personality_details *details,
3529                 enum bridge_basic_personality_type type)
3530 {
3531         switch (type) {
3532         case BRIDGE_BASIC_PERSONALITY_NORMAL:
3533                 details->v_table = &personality_normal_v_table;
3534                 details->bridge_flags = NORMAL_FLAGS;
3535                 details->on_personality_change = on_personality_change_normal;
3536                 break;
3537         case BRIDGE_BASIC_PERSONALITY_ATXFER:
3538                 details->v_table = &personality_atxfer_v_table;
3539                 details->bridge_flags = TRANSFER_FLAGS;
3540                 break;
3541         default:
3542                 ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
3543                 break;
3544         }
3545 }
3546
3547 static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
3548 {
3549         struct bridge_basic_personality *personality;
3550         int i;
3551
3552         if (!bridge) {
3553                 return NULL;
3554         }
3555
3556         personality = ao2_alloc(sizeof(*personality), personality_destructor);
3557         if (!personality) {
3558                 ao2_ref(bridge, -1);
3559                 return NULL;
3560         }
3561         for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
3562                 init_details(&personality->details[i], i);
3563         }
3564         personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
3565         bridge->personality = personality;
3566
3567         return bridge;
3568 }
3569
3570 struct ast_bridge *ast_bridge_basic_new(void)
3571 {
3572         struct ast_bridge *bridge;
3573
3574         bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
3575         bridge = bridge_base_init(bridge,
3576                 AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
3577                         | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS, NULL, NULL, NULL);
3578         bridge = bridge_basic_personality_alloc(bridge);
3579         bridge = bridge_register(bridge);
3580         return bridge;
3581 }
3582
3583 void ast_bridge_basic_set_flags(struct ast_bridge *bridge, unsigned int flags)
3584 {
3585         SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
3586         struct bridge_basic_personality *personality = bridge->personality;
3587
3588         personality->details[personality->current].bridge_flags |= flags;
3589         ast_set_flag(&bridge->feature_flags, flags);
3590 }
3591
3592 void ast_bridging_init_basic(void)
3593 {
3594         /* Setup bridge basic subclass v_table. */
3595         ast_bridge_basic_v_table = ast_bridge_base_v_table;
3596         ast_bridge_basic_v_table.name = "basic";
3597         ast_bridge_basic_v_table.push = bridge_basic_push;
3598         ast_bridge_basic_v_table.pull = bridge_basic_pull;
3599         ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
3600
3601         /*
3602          * Personality vtables don't have the same rules as
3603          * normal bridge vtables.  These vtable functions are
3604          * used as alterations to the ast_bridge_basic_v_table
3605          * method functionality and are checked for NULL before
3606          * calling.
3607          */
3608         personality_normal_v_table.name = "normal";
3609         personality_normal_v_table.push = bridge_personality_normal_push;
3610
3611         personality_atxfer_v_table.name = "attended transfer";
3612         personality_atxfer_v_table.push = bridge_personality_atxfer_push;
3613         personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
3614
3615         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
3616         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
3617 }
3618