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