522a7d40cb323df94dadb8d022094263f0435aac
[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.  This is
1326          * set with reference when entering the BLOND_NONFINAL, RECALLING,
1327          * and RETRANSFER states, and the reference released on state exit
1328          * if continuing with recall or retransfer to avoid leak.
1329          */
1330         struct ast_channel *recall_target;
1331         /*! The absolute starting time for running timers */
1332         struct timeval start;
1333         AST_LIST_HEAD_NOLOCK(,stimulus_list) stimulus_queue;
1334         /*! The current state of the attended transfer */
1335         enum attended_transfer_state state;
1336         /*! The current superstate of the attended transfer */
1337         enum attended_transfer_superstate superstate;
1338         /*! Configured atxferdropcall from features.conf */
1339         int atxferdropcall;
1340         /*! Configured atxfercallbackretries from features.conf */
1341         int atxfercallbackretries;
1342         /*! Configured atxferloopdelay from features.conf */
1343         int atxferloopdelay;
1344         /*! Configured atxfernoanswertimeout from features.conf */
1345         int atxfernoanswertimeout;
1346         /*! Count of the number of times that recalls have been attempted */
1347         int retry_attempts;
1348         /*! Framehook ID for outbounc call to transfer target or recall target */
1349         int target_framehook_id;
1350         /*! Dial structure used when recalling transferer channel */
1351         struct ast_dial *dial;
1352         /*! The bridging features the transferer has available */
1353         struct ast_flags transferer_features;
1354         /*! Saved transferer connected line data for recalling the transferer. */
1355         struct ast_party_connected_line original_transferer_colp;
1356 };
1357
1358 static void attended_transfer_properties_destructor(void *obj)
1359 {
1360         struct attended_transfer_properties *props = obj;
1361
1362         ast_debug(1, "Destroy attended transfer properties %p\n", props);
1363
1364         ao2_cleanup(props->target_bridge);
1365         ao2_cleanup(props->transferee_bridge);
1366         /* Use ast_channel_cleanup() instead of ast_channel_unref() for channels since they may be NULL */
1367         ast_channel_cleanup(props->transferer);
1368         ast_channel_cleanup(props->transfer_target);
1369         ast_channel_cleanup(props->recall_target);
1370         ast_party_connected_line_free(&props->original_transferer_colp);
1371         ast_string_field_free_memory(props);
1372         ast_cond_destroy(&props->cond);
1373 }
1374
1375 /*!
1376  * \internal
1377  * \brief Determine the transfer context to use.
1378  * \since 12.0.0
1379  *
1380  * \param transferer Channel initiating the transfer.
1381  * \param context User supplied context if available.  May be NULL.
1382  *
1383  * \return The context to use for the transfer.
1384  */
1385 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
1386 {
1387         if (!ast_strlen_zero(context)) {
1388                 return context;
1389         }
1390         context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
1391         if (!ast_strlen_zero(context)) {
1392                 return context;
1393         }
1394         context = ast_channel_macrocontext(transferer);
1395         if (!ast_strlen_zero(context)) {
1396                 return context;
1397         }
1398         context = ast_channel_context(transferer);
1399         if (!ast_strlen_zero(context)) {
1400                 return context;
1401         }
1402         return "default";
1403 }
1404
1405 /*!
1406  * \brief Allocate and initialize attended transfer properties
1407  *
1408  * \param transferer The channel performing the attended transfer
1409  * \param context Suggestion for what context the transfer target extension can be found in
1410  *
1411  * \retval NULL Failure to allocate or initialize
1412  * \retval non-NULL Newly allocated properties
1413  */
1414 static struct attended_transfer_properties *attended_transfer_properties_alloc(
1415         struct ast_channel *transferer, const char *context)
1416 {
1417         struct attended_transfer_properties *props;
1418         char *tech;
1419         char *addr;
1420         char *serial;
1421         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
1422         struct ast_flags *transferer_features;
1423
1424         props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
1425         if (!props || ast_string_field_init(props, 64)) {
1426                 return NULL;
1427         }
1428
1429         ast_cond_init(&props->cond, NULL);
1430
1431         props->target_framehook_id = -1;
1432         props->transferer = ast_channel_ref(transferer);
1433
1434         ast_channel_lock(props->transferer);
1435         xfer_cfg = ast_get_chan_features_xfer_config(props->transferer);
1436         if (!xfer_cfg) {
1437                 ast_log(LOG_ERROR, "Unable to get transfer configuration from channel %s\n", ast_channel_name(props->transferer));
1438                 ast_channel_unlock(props->transferer);
1439                 ao2_ref(props, -1);
1440                 return NULL;
1441         }
1442         transferer_features = ast_bridge_features_ds_get(props->transferer);
1443         if (transferer_features) {
1444                 props->transferer_features = *transferer_features;
1445         }
1446         props->atxferdropcall = xfer_cfg->atxferdropcall;
1447         props->atxfercallbackretries = xfer_cfg->atxfercallbackretries;
1448         props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
1449         props->atxferloopdelay = xfer_cfg->atxferloopdelay;
1450         ast_string_field_set(props, context, get_transfer_context(transferer, context));
1451         ast_string_field_set(props, failsound, xfer_cfg->xferfailsound);
1452         ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
1453
1454         /*
1455          * Save the transferee's party information for any recall calls.
1456          * This is the only piece of information needed that gets overwritten
1457          * on the transferer channel by the inital call to the transfer target.
1458          */
1459         ast_party_connected_line_copy(&props->original_transferer_colp,
1460                 ast_channel_connected(props->transferer));
1461
1462         tech = ast_strdupa(ast_channel_name(props->transferer));
1463         addr = strchr(tech, '/');
1464         if (!addr) {
1465                 ast_log(LOG_ERROR, "Transferer channel name does not follow typical channel naming format (tech/address)\n");
1466                 ast_channel_unlock(props->transferer);
1467                 ao2_ref(props, -1);
1468                 return NULL;
1469         }
1470         *addr++ = '\0';
1471         serial = strrchr(addr, '-');
1472         if (serial) {
1473                 *serial = '\0';
1474         }
1475         ast_string_field_set(props, transferer_type, tech);
1476         ast_string_field_set(props, transferer_addr, addr);
1477
1478         ast_channel_unlock(props->transferer);
1479
1480         ast_debug(1, "Allocated attended transfer properties %p for transfer from %s\n",
1481                         props, ast_channel_name(props->transferer));
1482         return props;
1483 }
1484
1485 /*!
1486  * \brief Free backlog of stimuli in the queue
1487  */
1488 static void clear_stimulus_queue(struct attended_transfer_properties *props)
1489 {
1490         struct stimulus_list *list;
1491         SCOPED_AO2LOCK(lock, props);
1492
1493         while ((list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
1494                 ast_free(list);
1495         }
1496 }
1497
1498 /*!
1499  * \brief Initiate shutdown of attended transfer properties
1500  *
1501  * Calling this indicates that the attended transfer properties are no longer needed
1502  * because the transfer operation has concluded.
1503  */
1504 static void attended_transfer_properties_shutdown(struct attended_transfer_properties *props)
1505 {
1506         ast_debug(1, "Shutting down attended transfer %p\n", props);
1507
1508         if (props->transferee_bridge) {
1509                 bridge_basic_change_personality(props->transferee_bridge,
1510                                 BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
1511                 ast_bridge_merge_inhibit(props->transferee_bridge, -1);
1512         }
1513
1514         if (props->target_bridge) {
1515                 ast_bridge_destroy(props->target_bridge, 0);
1516                 props->target_bridge = NULL;
1517         }
1518
1519         if (props->transferer) {
1520                 ast_channel_remove_bridge_role(props->transferer, AST_TRANSFERER_ROLE_NAME);
1521         }
1522
1523         clear_stimulus_queue(props);
1524
1525         ao2_cleanup(props);
1526 }
1527
1528 static void stimulate_attended_transfer(struct attended_transfer_properties *props,
1529                 enum attended_transfer_stimulus stimulus)
1530 {
1531         struct stimulus_list *list;
1532
1533         list = ast_calloc(1, sizeof(*list));
1534         if (!list) {
1535                 ast_log(LOG_ERROR, "Unable to push event to attended transfer queue. Expect transfer to fail\n");
1536                 return;
1537         }
1538
1539         list->stimulus = stimulus;
1540         ao2_lock(props);
1541         AST_LIST_INSERT_TAIL(&props->stimulus_queue, list, next);
1542         ast_cond_signal(&props->cond);
1543         ao2_unlock(props);
1544 }
1545
1546 /*!
1547  * \brief Get a desired transfer party for a bridge the transferer is not in.
1548  *
1549  * \param bridge The bridge to get the party from. May be NULL.
1550  * \param[out] party The lone channel in the bridge. Will be set NULL if bridge is NULL or multiple parties are present.
1551  */
1552 static void get_transfer_party_non_transferer_bridge(struct ast_bridge *bridge,
1553                 struct ast_channel **party)
1554 {
1555         if (bridge && bridge->num_channels == 1) {
1556                 *party = ast_channel_ref(AST_LIST_FIRST(&bridge->channels)->chan);
1557         } else {
1558                 *party = NULL;
1559         }
1560 }
1561
1562 /*!
1563  * \brief Get the transferee and transfer target when the transferer is in a bridge with
1564  * one of the desired parties.
1565  *
1566  * \param transferer_bridge The bridge the transferer is in
1567  * \param other_bridge The bridge the transferer is not in. May be NULL.
1568  * \param transferer The transferer party
1569  * \param[out] transferer_peer The party that is in the bridge with the transferer
1570  * \param[out] other_party The party that is in the other_bridge
1571  */
1572 static void get_transfer_parties_transferer_bridge(struct ast_bridge *transferer_bridge,
1573                 struct ast_bridge *other_bridge, struct ast_channel *transferer,
1574                 struct ast_channel **transferer_peer, struct ast_channel **other_party)
1575 {
1576         *transferer_peer = ast_bridge_peer(transferer_bridge, transferer);
1577         get_transfer_party_non_transferer_bridge(other_bridge, other_party);
1578 }
1579
1580 /*!
1581  * \brief determine transferee and transfer target for an attended transfer
1582  *
1583  * In builtin attended transfers, there is a single transferer channel that jumps between
1584  * the two bridges involved. At the time the attended transfer occurs, the transferer could
1585  * be in either bridge, so determining the parties is a bit more complex than normal.
1586  *
1587  * The method used here is to determine which of the two bridges the transferer is in, and
1588  * grabbing the peer from that bridge. The other bridge, if it only has a single channel in it,
1589  * has the other desired channel.
1590  *
1591  * \param transferer The channel performing the transfer
1592  * \param transferee_bridge The bridge that the transferee is in
1593  * \param target_bridge The bridge that the transfer target is in
1594  * \param[out] transferee The transferee channel
1595  * \param[out] transfer_target The transfer target channel
1596  */
1597 static void get_transfer_parties(struct ast_channel *transferer, struct ast_bridge *transferee_bridge,
1598                 struct ast_bridge *target_bridge, struct ast_channel **transferee,
1599                 struct ast_channel **transfer_target)
1600 {
1601         struct ast_bridge *transferer_bridge;
1602
1603         ast_channel_lock(transferer);
1604         transferer_bridge = ast_channel_get_bridge(transferer);
1605         ast_channel_unlock(transferer);
1606
1607         if (transferer_bridge == transferee_bridge) {
1608                 get_transfer_parties_transferer_bridge(transferee_bridge, target_bridge,
1609                                 transferer, transferee, transfer_target);
1610         } else if (transferer_bridge == target_bridge) {
1611                 get_transfer_parties_transferer_bridge(target_bridge, transferee_bridge,
1612                                 transferer, transfer_target, transferee);
1613         } else {
1614                 get_transfer_party_non_transferer_bridge(transferee_bridge, transferee);
1615                 get_transfer_party_non_transferer_bridge(target_bridge, transfer_target);
1616         }
1617
1618         ao2_cleanup(transferer_bridge);
1619 }
1620
1621 /*!
1622  * \brief Send a stasis publication for a successful attended transfer
1623  */
1624 static void publish_transfer_success(struct attended_transfer_properties *props,
1625                 struct ast_channel *transferee_channel, struct ast_channel *target_channel)
1626 {
1627         struct ast_attended_transfer_message *transfer_msg;
1628
1629         transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
1630                         props->transferee_bridge, props->transferer, props->target_bridge,
1631                         transferee_channel, target_channel);
1632
1633         if (!transfer_msg) {
1634                 ast_log(LOG_ERROR, "Unable to publish successful attended transfer from %s\n",
1635                                 ast_channel_name(props->transferer));
1636                 return;
1637         }
1638
1639         ast_attended_transfer_message_add_merge(transfer_msg, props->transferee_bridge);
1640         ast_bridge_publish_attended_transfer(transfer_msg);
1641         ao2_cleanup(transfer_msg);
1642 }
1643
1644 /*!
1645  * \brief Send a stasis publication for an attended transfer that ends in a threeway call
1646  */
1647 static void publish_transfer_threeway(struct attended_transfer_properties *props,
1648                 struct ast_channel *transferee_channel, struct ast_channel *target_channel)
1649 {
1650         struct ast_attended_transfer_message *transfer_msg;
1651
1652         transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
1653                         props->transferee_bridge, props->transferer, props->target_bridge,
1654                         transferee_channel, target_channel);
1655
1656         if (!transfer_msg) {
1657                 ast_log(LOG_ERROR, "Unable to publish successful three-way transfer from %s\n",
1658                                 ast_channel_name(props->transferer));
1659                 return;
1660         }
1661
1662         ast_attended_transfer_message_add_threeway(transfer_msg, props->transferer,
1663                         props->transferee_bridge);
1664         ast_bridge_publish_attended_transfer(transfer_msg);
1665         ao2_cleanup(transfer_msg);
1666 }
1667
1668 /*!
1669  * \brief Send a stasis publication for a failed attended transfer
1670  */
1671 static void publish_transfer_fail(struct attended_transfer_properties *props)
1672 {
1673         struct ast_attended_transfer_message *transfer_msg;
1674
1675         transfer_msg = ast_attended_transfer_message_create(0, props->transferer,
1676                         props->transferee_bridge, props->transferer, props->target_bridge,
1677                         NULL, NULL);
1678
1679         if (!transfer_msg) {
1680                 ast_log(LOG_ERROR, "Unable to publish failed transfer from %s\n",
1681                                 ast_channel_name(props->transferer));
1682                 return;
1683         }
1684
1685         transfer_msg->result = AST_BRIDGE_TRANSFER_FAIL;
1686         ast_bridge_publish_attended_transfer(transfer_msg);
1687         ao2_cleanup(transfer_msg);
1688 }
1689
1690 /*!
1691  * \brief Helper method to play a sound on a channel in a bridge
1692  *
1693  * \param chan The channel to play the sound to
1694  * \param sound The sound to play
1695  */
1696 static void play_sound(struct ast_channel *chan, const char *sound)
1697 {
1698         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1699
1700         ast_channel_lock(chan);
1701         bridge_channel = ast_channel_get_bridge_channel(chan);
1702         ast_channel_unlock(chan);
1703
1704         if (!bridge_channel) {
1705                 return;
1706         }
1707
1708         ast_bridge_channel_queue_playfile(bridge_channel, NULL, sound, NULL);
1709 }
1710
1711 /*!
1712  * \brief Helper method to place a channel in a bridge on hold
1713  */
1714 static void hold(struct ast_channel *chan)
1715 {
1716         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1717
1718         if (chan) {
1719                 ast_channel_lock(chan);
1720                 bridge_channel = ast_channel_get_bridge_channel(chan);
1721                 ast_channel_unlock(chan);
1722
1723                 ast_assert(bridge_channel != NULL);
1724
1725                 ast_bridge_channel_write_hold(bridge_channel, NULL);
1726         }
1727 }
1728
1729 /*!
1730  * \brief Helper method to take a channel in a bridge off hold
1731  */
1732 static void unhold(struct ast_channel *chan)
1733 {
1734         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1735
1736         ast_channel_lock(chan);
1737         bridge_channel = ast_channel_get_bridge_channel(chan);
1738         ast_channel_unlock(chan);
1739
1740         ast_assert(bridge_channel != NULL);
1741
1742         ast_bridge_channel_write_unhold(bridge_channel);
1743 }
1744
1745 /*!
1746  * \brief Helper method to send a ringing indication to a channel in a bridge
1747  */
1748 static void ringing(struct ast_channel *chan)
1749 {
1750         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1751
1752         ast_channel_lock(chan);
1753         bridge_channel = ast_channel_get_bridge_channel(chan);
1754         ast_channel_unlock(chan);
1755
1756         ast_assert(bridge_channel != NULL);
1757
1758         ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_RINGING, NULL, 0);
1759 }
1760
1761 /*!
1762  * \brief Helper method to send a ringing indication to all channels in a bridge
1763  */
1764 static void bridge_ringing(struct ast_bridge *bridge)
1765 {
1766         struct ast_frame ringing = {
1767                 .frametype = AST_FRAME_CONTROL,
1768                 .subclass.integer = AST_CONTROL_RINGING,
1769         };
1770
1771         ast_bridge_queue_everyone_else(bridge, NULL, &ringing);
1772 }
1773
1774 /*!
1775  * \brief Helper method to send a hold frame to all channels in a bridge
1776  */
1777 static void bridge_hold(struct ast_bridge *bridge)
1778 {
1779         struct ast_frame hold = {
1780                 .frametype = AST_FRAME_CONTROL,
1781                 .subclass.integer = AST_CONTROL_HOLD,
1782         };
1783
1784         ast_bridge_queue_everyone_else(bridge, NULL, &hold);
1785 }
1786
1787 /*!
1788  * \brief Helper method to send an unhold frame to all channels in a bridge
1789  */
1790 static void bridge_unhold(struct ast_bridge *bridge)
1791 {
1792         struct ast_frame unhold = {
1793                 .frametype = AST_FRAME_CONTROL,
1794                 .subclass.integer = AST_CONTROL_UNHOLD,
1795         };
1796
1797         ast_bridge_queue_everyone_else(bridge, NULL, &unhold);
1798 }
1799
1800 /*!
1801  * \brief Wrapper for \ref bridge_do_move
1802  */
1803 static int bridge_move(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel *channel, struct ast_channel *swap)
1804 {
1805         int res;
1806         RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
1807
1808         ast_bridge_lock_both(src, dest);
1809
1810         ast_channel_lock(channel);
1811         bridge_channel = ast_channel_get_bridge_channel(channel);
1812         ast_channel_unlock(channel);
1813
1814         ast_assert(bridge_channel != NULL);
1815
1816         ao2_lock(bridge_channel);
1817         bridge_channel->swap = swap;
1818         ao2_unlock(bridge_channel);
1819
1820         res = bridge_do_move(dest, bridge_channel, 1, 0);
1821
1822         ast_bridge_unlock(dest);
1823         ast_bridge_unlock(src);
1824
1825         return res;
1826 }
1827
1828 /*!
1829  * \brief Wrapper for \ref bridge_do_merge
1830  */
1831 static void bridge_merge(struct ast_bridge *dest, struct ast_bridge *src, struct ast_channel **kick_channels, unsigned int num_channels)
1832 {
1833         struct ast_bridge_channel **kick_bridge_channels = num_channels ?
1834                 ast_alloca(num_channels * sizeof(*kick_bridge_channels)) : NULL;
1835         int i;
1836         int num_bridge_channels = 0;
1837
1838         ast_bridge_lock_both(dest, src);
1839
1840         for (i = 0; i < num_channels; ++i) {
1841                 struct ast_bridge_channel *kick_bridge_channel;
1842
1843                 kick_bridge_channel = bridge_find_channel(src, kick_channels[i]);
1844                 if (!kick_bridge_channel) {
1845                         kick_bridge_channel = bridge_find_channel(dest, kick_channels[i]);
1846                 }
1847
1848                 /* It's possible (and fine) for the bridge channel to be NULL at this point if the
1849                  * channel has hung up already. If that happens, we can just remove it from the list
1850                  * of bridge channels to kick from the bridge
1851                  */
1852                 if (!kick_bridge_channel) {
1853                         continue;
1854                 }
1855
1856                 kick_bridge_channels[num_bridge_channels++] = kick_bridge_channel;
1857         }
1858
1859         bridge_do_merge(dest, src, kick_bridge_channels, num_bridge_channels, 0);
1860         ast_bridge_unlock(dest);
1861         ast_bridge_unlock(src);
1862 }
1863
1864 /*!
1865  * \brief Flags that indicate properties of attended transfer states
1866  */
1867 enum attended_transfer_state_flags {
1868         /*! This state requires that the timer be reset when entering the state */
1869         TRANSFER_STATE_FLAG_TIMER_RESET = (1 << 0),
1870         /*! This state's timer uses atxferloopdelay */
1871         TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY = (1 << 1),
1872         /*! This state's timer uses atxfernoanswertimeout */
1873         TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER = (1 << 2),
1874         /*! This state has a time limit associated with it */
1875         TRANSFER_STATE_FLAG_TIMED = (TRANSFER_STATE_FLAG_TIMER_RESET |
1876                         TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY | TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER),
1877         /*! This state does not transition to any other states */
1878         TRANSFER_STATE_FLAG_TERMINAL = (1 << 3),
1879 };
1880
1881 static int calling_target_enter(struct attended_transfer_properties *props);
1882 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
1883                 enum attended_transfer_stimulus stimulus);
1884
1885 static int hesitant_enter(struct attended_transfer_properties *props);
1886 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
1887                 enum attended_transfer_stimulus stimulus);
1888
1889 static int rebridge_enter(struct attended_transfer_properties *props);
1890
1891 static int resume_enter(struct attended_transfer_properties *props);
1892
1893 static int threeway_enter(struct attended_transfer_properties *props);
1894
1895 static int consulting_enter(struct attended_transfer_properties *props);
1896 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
1897                 enum attended_transfer_stimulus stimulus);
1898
1899 static int double_checking_enter(struct attended_transfer_properties *props);
1900 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
1901                 enum attended_transfer_stimulus stimulus);
1902
1903 static int complete_enter(struct attended_transfer_properties *props);
1904
1905 static int blond_enter(struct attended_transfer_properties *props);
1906
1907 static int blond_nonfinal_enter(struct attended_transfer_properties *props);
1908 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
1909                 enum attended_transfer_stimulus stimulus);
1910
1911 static int recalling_enter(struct attended_transfer_properties *props);
1912 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
1913                 enum attended_transfer_stimulus stimulus);
1914
1915 static int wait_to_retransfer_enter(struct attended_transfer_properties *props);
1916 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
1917                 enum attended_transfer_stimulus stimulus);
1918
1919 static int retransfer_enter(struct attended_transfer_properties *props);
1920 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
1921                 enum attended_transfer_stimulus stimulus);
1922
1923 static int wait_to_recall_enter(struct attended_transfer_properties *props);
1924 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
1925                 enum attended_transfer_stimulus stimulus);
1926
1927 static int fail_enter(struct attended_transfer_properties *props);
1928
1929 /*!
1930  * \brief Properties of an attended transfer state
1931  */
1932 struct attended_transfer_state_properties {
1933         /*! The name of the state. Used for debugging */
1934         const char *state_name;
1935         /*! Function used to enter a state */
1936         int (*enter)(struct attended_transfer_properties *props);
1937         /*!
1938          * Function used to exit a state
1939          * This is used both to determine what the next state
1940          * to transition to will be and to perform any cleanup
1941          * necessary before exiting the current state.
1942          */
1943         enum attended_transfer_state (*exit)(struct attended_transfer_properties *props,
1944                         enum attended_transfer_stimulus stimulus);
1945         /*! Flags associated with this state */
1946         enum attended_transfer_state_flags flags;
1947 };
1948
1949 static const struct attended_transfer_state_properties state_properties[] = {
1950         [TRANSFER_CALLING_TARGET] = {
1951                 .state_name = "Calling Target",
1952                 .enter = calling_target_enter,
1953                 .exit = calling_target_exit,
1954                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
1955         },
1956         [TRANSFER_HESITANT] = {
1957                 .state_name = "Hesitant",
1958                 .enter = hesitant_enter,
1959                 .exit = hesitant_exit,
1960                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
1961         },
1962         [TRANSFER_REBRIDGE] = {
1963                 .state_name = "Rebridge",
1964                 .enter = rebridge_enter,
1965                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1966         },
1967         [TRANSFER_RESUME] = {
1968                 .state_name = "Resume",
1969                 .enter = resume_enter,
1970                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1971         },
1972         [TRANSFER_THREEWAY] = {
1973                 .state_name = "Threeway",
1974                 .enter = threeway_enter,
1975                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1976         },
1977         [TRANSFER_CONSULTING] = {
1978                 .state_name = "Consulting",
1979                 .enter = consulting_enter,
1980                 .exit = consulting_exit,
1981         },
1982         [TRANSFER_DOUBLECHECKING] = {
1983                 .state_name = "Double Checking",
1984                 .enter = double_checking_enter,
1985                 .exit = double_checking_exit,
1986         },
1987         [TRANSFER_COMPLETE] = {
1988                 .state_name = "Complete",
1989                 .enter = complete_enter,
1990                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1991         },
1992         [TRANSFER_BLOND] = {
1993                 .state_name = "Blond",
1994                 .enter = blond_enter,
1995                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
1996         },
1997         [TRANSFER_BLOND_NONFINAL] = {
1998                 .state_name = "Blond Non-Final",
1999                 .enter = blond_nonfinal_enter,
2000                 .exit = blond_nonfinal_exit,
2001                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER,
2002         },
2003         [TRANSFER_RECALLING] = {
2004                 .state_name = "Recalling",
2005                 .enter = recalling_enter,
2006                 .exit = recalling_exit,
2007                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
2008         },
2009         [TRANSFER_WAIT_TO_RETRANSFER] = {
2010                 .state_name = "Wait to Retransfer",
2011                 .enter = wait_to_retransfer_enter,
2012                 .exit = wait_to_retransfer_exit,
2013                 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
2014         },
2015         [TRANSFER_RETRANSFER] = {
2016                 .state_name = "Retransfer",
2017                 .enter = retransfer_enter,
2018                 .exit = retransfer_exit,
2019                 .flags = TRANSFER_STATE_FLAG_ATXFER_NO_ANSWER | TRANSFER_STATE_FLAG_TIMER_RESET,
2020         },
2021         [TRANSFER_WAIT_TO_RECALL] = {
2022                 .state_name = "Wait to Recall",
2023                 .enter = wait_to_recall_enter,
2024                 .exit = wait_to_recall_exit,
2025                 .flags = TRANSFER_STATE_FLAG_TIMER_RESET | TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY,
2026         },
2027         [TRANSFER_FAIL] = {
2028                 .state_name = "Fail",
2029                 .enter = fail_enter,
2030                 .flags = TRANSFER_STATE_FLAG_TERMINAL,
2031         },
2032 };
2033
2034 static int calling_target_enter(struct attended_transfer_properties *props)
2035 {
2036         return bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
2037 }
2038
2039 static enum attended_transfer_state calling_target_exit(struct attended_transfer_properties *props,
2040                 enum attended_transfer_stimulus stimulus)
2041 {
2042         switch (stimulus) {
2043         case STIMULUS_TRANSFEREE_HANGUP:
2044                 play_sound(props->transferer, props->failsound);
2045                 publish_transfer_fail(props);
2046                 return TRANSFER_FAIL;
2047         case STIMULUS_DTMF_ATXFER_COMPLETE:
2048         case STIMULUS_TRANSFERER_HANGUP:
2049                 bridge_unhold(props->transferee_bridge);
2050                 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
2051         case STIMULUS_TRANSFER_TARGET_ANSWER:
2052                 return TRANSFER_CONSULTING;
2053         case STIMULUS_TRANSFER_TARGET_HANGUP:
2054         case STIMULUS_TIMEOUT:
2055         case STIMULUS_DTMF_ATXFER_ABORT:
2056                 play_sound(props->transferer, props->failsound);
2057                 return TRANSFER_REBRIDGE;
2058         case STIMULUS_DTMF_ATXFER_THREEWAY:
2059                 bridge_unhold(props->transferee_bridge);
2060                 return TRANSFER_THREEWAY;
2061         case STIMULUS_DTMF_ATXFER_SWAP:
2062                 return TRANSFER_HESITANT;
2063         case STIMULUS_NONE:
2064         case STIMULUS_RECALL_TARGET_ANSWER:
2065         case STIMULUS_RECALL_TARGET_HANGUP:
2066         default:
2067                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2068                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2069                 return props->state;
2070         }
2071 }
2072
2073 static int hesitant_enter(struct attended_transfer_properties *props)
2074 {
2075         if (bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL)) {
2076                 return -1;
2077         }
2078
2079         unhold(props->transferer);
2080         return 0;
2081 }
2082
2083 static enum attended_transfer_state hesitant_exit(struct attended_transfer_properties *props,
2084                 enum attended_transfer_stimulus stimulus)
2085 {
2086         switch (stimulus) {
2087         case STIMULUS_TRANSFEREE_HANGUP:
2088                 play_sound(props->transferer, props->failsound);
2089                 publish_transfer_fail(props);
2090                 return TRANSFER_FAIL;
2091         case STIMULUS_DTMF_ATXFER_COMPLETE:
2092         case STIMULUS_TRANSFERER_HANGUP:
2093                 return props->atxferdropcall ? TRANSFER_BLOND : TRANSFER_BLOND_NONFINAL;
2094         case STIMULUS_TRANSFER_TARGET_ANSWER:
2095                 return TRANSFER_DOUBLECHECKING;
2096         case STIMULUS_TRANSFER_TARGET_HANGUP:
2097         case STIMULUS_TIMEOUT:
2098         case STIMULUS_DTMF_ATXFER_ABORT:
2099                 play_sound(props->transferer, props->failsound);
2100                 return TRANSFER_RESUME;
2101         case STIMULUS_DTMF_ATXFER_THREEWAY:
2102                 return TRANSFER_THREEWAY;
2103         case STIMULUS_DTMF_ATXFER_SWAP:
2104                 hold(props->transferer);
2105                 return TRANSFER_CALLING_TARGET;
2106         case STIMULUS_NONE:
2107         case STIMULUS_RECALL_TARGET_HANGUP:
2108         case STIMULUS_RECALL_TARGET_ANSWER:
2109         default:
2110                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2111                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2112                 return props->state;
2113         }
2114 }
2115
2116 static int rebridge_enter(struct attended_transfer_properties *props)
2117 {
2118         if (bridge_move(props->transferee_bridge, props->target_bridge,
2119                         props->transferer, NULL)) {
2120                 return -1;
2121         }
2122
2123         unhold(props->transferer);
2124         return 0;
2125 }
2126
2127 static int resume_enter(struct attended_transfer_properties *props)
2128 {
2129         return 0;
2130 }
2131
2132 static int threeway_enter(struct attended_transfer_properties *props)
2133 {
2134         struct ast_channel *transferee_channel;
2135         struct ast_channel *target_channel;
2136
2137         get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
2138                         &transferee_channel, &target_channel);
2139         bridge_merge(props->transferee_bridge, props->target_bridge, NULL, 0);
2140         play_sound(props->transfer_target, props->xfersound);
2141         play_sound(props->transferer, props->xfersound);
2142         publish_transfer_threeway(props, transferee_channel, target_channel);
2143
2144         ast_channel_cleanup(transferee_channel);
2145         ast_channel_cleanup(target_channel);
2146         return 0;
2147 }
2148
2149 static int consulting_enter(struct attended_transfer_properties *props)
2150 {
2151         return 0;
2152 }
2153
2154 static enum attended_transfer_state consulting_exit(struct attended_transfer_properties *props,
2155                 enum attended_transfer_stimulus stimulus)
2156 {
2157         switch (stimulus) {
2158         case STIMULUS_TRANSFEREE_HANGUP:
2159                 /* This is a one-of-a-kind event. The transferer and transfer target are talking in
2160                  * one bridge, and the transferee has hung up in a separate bridge. In this case, we
2161                  * will change the personality of the transfer target bridge back to normal, and play
2162                  * a sound to the transferer to indicate the transferee is gone.
2163                  */
2164                 bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
2165                 play_sound(props->transferer, props->failsound);
2166                 ast_bridge_merge_inhibit(props->target_bridge, -1);
2167                 /* These next two lines are here to ensure that our reference to the target bridge
2168                  * is cleaned up properly and that the target bridge is not destroyed when the
2169                  * monitor thread exits
2170                  */
2171                 ao2_ref(props->target_bridge, -1);
2172                 props->target_bridge = NULL;
2173                 return TRANSFER_FAIL;
2174         case STIMULUS_TRANSFERER_HANGUP:
2175         case STIMULUS_DTMF_ATXFER_COMPLETE:
2176                 /* We know the transferer is in the target_bridge, so take the other bridge off hold */
2177                 bridge_unhold(props->transferee_bridge);
2178                 return TRANSFER_COMPLETE;
2179         case STIMULUS_TRANSFER_TARGET_HANGUP:
2180         case STIMULUS_DTMF_ATXFER_ABORT:
2181                 play_sound(props->transferer, props->failsound);
2182                 return TRANSFER_REBRIDGE;
2183         case STIMULUS_DTMF_ATXFER_THREEWAY:
2184                 bridge_unhold(props->transferee_bridge);
2185                 return TRANSFER_THREEWAY;
2186         case STIMULUS_DTMF_ATXFER_SWAP:
2187                 hold(props->transferer);
2188                 bridge_move(props->transferee_bridge, props->target_bridge, props->transferer, NULL);
2189                 unhold(props->transferer);
2190                 return TRANSFER_DOUBLECHECKING;
2191         case STIMULUS_NONE:
2192         case STIMULUS_TIMEOUT:
2193         case STIMULUS_TRANSFER_TARGET_ANSWER:
2194         case STIMULUS_RECALL_TARGET_HANGUP:
2195         case STIMULUS_RECALL_TARGET_ANSWER:
2196         default:
2197                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2198                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2199                 return props->state;
2200         }
2201 }
2202
2203 static int double_checking_enter(struct attended_transfer_properties *props)
2204 {
2205         return 0;
2206 }
2207
2208 static enum attended_transfer_state double_checking_exit(struct attended_transfer_properties *props,
2209                 enum attended_transfer_stimulus stimulus)
2210 {
2211         switch (stimulus) {
2212         case STIMULUS_TRANSFEREE_HANGUP:
2213                 play_sound(props->transferer, props->failsound);
2214                 publish_transfer_fail(props);
2215                 return TRANSFER_FAIL;
2216         case STIMULUS_TRANSFERER_HANGUP:
2217         case STIMULUS_DTMF_ATXFER_COMPLETE:
2218                 /* We know the transferer is in the transferee, so take the other bridge off hold */
2219                 bridge_unhold(props->target_bridge);
2220                 return TRANSFER_COMPLETE;
2221         case STIMULUS_TRANSFER_TARGET_HANGUP:
2222         case STIMULUS_DTMF_ATXFER_ABORT:
2223                 play_sound(props->transferer, props->failsound);
2224                 return TRANSFER_RESUME;
2225         case STIMULUS_DTMF_ATXFER_THREEWAY:
2226                 bridge_unhold(props->target_bridge);
2227                 return TRANSFER_THREEWAY;
2228         case STIMULUS_DTMF_ATXFER_SWAP:
2229                 hold(props->transferer);
2230                 bridge_move(props->target_bridge, props->transferee_bridge, props->transferer, NULL);
2231                 unhold(props->transferer);
2232                 return TRANSFER_CONSULTING;
2233         case STIMULUS_NONE:
2234         case STIMULUS_TIMEOUT:
2235         case STIMULUS_TRANSFER_TARGET_ANSWER:
2236         case STIMULUS_RECALL_TARGET_HANGUP:
2237         case STIMULUS_RECALL_TARGET_ANSWER:
2238         default:
2239                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2240                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2241                 return props->state;
2242         }
2243 }
2244
2245 static int complete_enter(struct attended_transfer_properties *props)
2246 {
2247         struct ast_channel *transferee_channel;
2248         struct ast_channel *target_channel;
2249
2250         get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
2251                         &transferee_channel, &target_channel);
2252         bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
2253         play_sound(props->transfer_target, props->xfersound);
2254         publish_transfer_success(props, transferee_channel, target_channel);
2255
2256         ast_channel_cleanup(transferee_channel);
2257         ast_channel_cleanup(target_channel);
2258         return 0;
2259 }
2260
2261 static int blond_enter(struct attended_transfer_properties *props)
2262 {
2263         struct ast_channel *transferee_channel;
2264         struct ast_channel *target_channel;
2265
2266         get_transfer_parties(props->transferer, props->transferee_bridge, props->target_bridge,
2267                         &transferee_channel, &target_channel);
2268         bridge_merge(props->transferee_bridge, props->target_bridge, &props->transferer, 1);
2269         ringing(props->transfer_target);
2270         publish_transfer_success(props, transferee_channel, target_channel);
2271
2272         ast_channel_cleanup(transferee_channel);
2273         ast_channel_cleanup(target_channel);
2274         return 0;
2275 }
2276
2277 static int blond_nonfinal_enter(struct attended_transfer_properties *props)
2278 {
2279         int res;
2280         props->superstate = SUPERSTATE_RECALL;
2281         /* move the transfer target to the recall target along with its reference */
2282         props->recall_target = ast_channel_ref(props->transfer_target);
2283         res = blond_enter(props);
2284         props->transfer_target = ast_channel_unref(props->transfer_target);
2285         return res;
2286 }
2287
2288 static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer_properties *props,
2289                 enum attended_transfer_stimulus stimulus)
2290 {
2291         switch (stimulus) {
2292         case STIMULUS_TRANSFEREE_HANGUP:
2293                 return TRANSFER_FAIL;
2294         case STIMULUS_RECALL_TARGET_ANSWER:
2295                 return TRANSFER_RESUME;
2296         case STIMULUS_TIMEOUT:
2297                 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
2298         case STIMULUS_RECALL_TARGET_HANGUP:
2299                 props->recall_target = ast_channel_unref(props->recall_target);
2300                 return TRANSFER_RECALLING;
2301         case STIMULUS_NONE:
2302         case STIMULUS_DTMF_ATXFER_ABORT:
2303         case STIMULUS_DTMF_ATXFER_COMPLETE:
2304         case STIMULUS_DTMF_ATXFER_THREEWAY:
2305         case STIMULUS_DTMF_ATXFER_SWAP:
2306         case STIMULUS_TRANSFERER_HANGUP:
2307         case STIMULUS_TRANSFER_TARGET_HANGUP:
2308         case STIMULUS_TRANSFER_TARGET_ANSWER:
2309         default:
2310                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2311                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2312                 return props->state;
2313         }
2314 }
2315
2316 /*!
2317  * \brief Dial callback when attempting to recall the original transferer channel
2318  *
2319  * This is how we can monitor if the recall target has answered or has hung up.
2320  * If one of the two is detected, then an appropriate stimulus is sent to the
2321  * attended transfer monitor thread.
2322  */
2323 static void recall_callback(struct ast_dial *dial)
2324 {
2325         struct attended_transfer_properties *props = ast_dial_get_user_data(dial);
2326
2327         switch (ast_dial_state(dial)) {
2328         default:
2329         case AST_DIAL_RESULT_INVALID:
2330         case AST_DIAL_RESULT_FAILED:
2331         case AST_DIAL_RESULT_TIMEOUT:
2332         case AST_DIAL_RESULT_HANGUP:
2333         case AST_DIAL_RESULT_UNANSWERED:
2334                 /* Failure cases */
2335                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
2336                 break;
2337         case AST_DIAL_RESULT_RINGING:
2338         case AST_DIAL_RESULT_PROGRESS:
2339         case AST_DIAL_RESULT_PROCEEDING:
2340         case AST_DIAL_RESULT_TRYING:
2341                 /* Don't care about these cases */
2342                 break;
2343         case AST_DIAL_RESULT_ANSWERED:
2344                 /* We struck gold! */
2345                 props->recall_target = ast_dial_answered_steal(dial);
2346                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
2347                 break;
2348         }
2349 }
2350
2351 /*!
2352  * \internal
2353  * \brief Setup common things to transferrer and transfer_target recall channels.
2354  *
2355  * \param recall Channel for recalling a party.
2356  * \param transferer Channel supplying recall information.
2357  *
2358  * \details
2359  * Setup callid, variables, datastores, accountcode, and peeraccount.
2360  *
2361  * \pre Both channels are locked on entry.
2362  *
2363  * \pre COLP and CLID on the recall channel are setup by the caller but not
2364  * explicitly published yet.
2365  *
2366  * \return Nothing
2367  */
2368 static void common_recall_channel_setup(struct ast_channel *recall, struct ast_channel *transferer)
2369 {
2370         ast_callid callid;
2371
2372         callid = ast_read_threadstorage_callid();
2373         if (callid) {
2374                 ast_channel_callid_set(recall, callid);
2375         }
2376
2377         ast_channel_inherit_variables(transferer, recall);
2378         ast_channel_datastore_inherit(transferer, recall);
2379
2380         /*
2381          * Stage a snapshot to ensure that a snapshot is always done
2382          * on the recall channel so earler COLP and CLID setup will
2383          * get published.
2384          */
2385         ast_channel_stage_snapshot(recall);
2386         ast_channel_req_accountcodes(recall, transferer, AST_CHANNEL_REQUESTOR_REPLACEMENT);
2387         ast_channel_stage_snapshot_done(recall);
2388 }
2389
2390 static int recalling_enter(struct attended_transfer_properties *props)
2391 {
2392         RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
2393         struct ast_channel *recall;
2394
2395         if (!cap) {
2396                 return -1;
2397         }
2398
2399         ast_format_cap_append(cap, ast_format_slin, 0);
2400
2401         /* When we dial the transfer target, since we are communicating
2402          * with a local channel, we can place the local channel in a bridge
2403          * and then call out to it. When recalling the transferer, though, we
2404          * have to use the dialing API because the channel is not local.
2405          */
2406         props->dial = ast_dial_create();
2407         if (!props->dial) {
2408                 return -1;
2409         }
2410
2411         if (ast_dial_append(props->dial, props->transferer_type, props->transferer_addr, NULL)) {
2412                 return -1;
2413         }
2414
2415         if (ast_dial_prerun(props->dial, NULL, cap)) {
2416                 return -1;
2417         }
2418
2419         /*
2420          * Setup callid, variables, datastores, accountcode, peeraccount,
2421          * COLP, and CLID on the recalled transferrer.
2422          */
2423         recall = ast_dial_get_channel(props->dial, 0);
2424         if (!recall) {
2425                 return -1;
2426         }
2427         ast_channel_lock_both(recall, props->transferer);
2428
2429         ast_party_caller_copy(ast_channel_caller(recall),
2430                 ast_channel_caller(props->transferer));
2431         ast_party_connected_line_copy(ast_channel_connected(recall),
2432                 &props->original_transferer_colp);
2433
2434         common_recall_channel_setup(recall, props->transferer);
2435         ast_channel_unlock(recall);
2436         ast_channel_unlock(props->transferer);
2437
2438         ast_dial_set_state_callback(props->dial, recall_callback);
2439
2440         ao2_ref(props, +1);
2441         ast_dial_set_user_data(props->dial, props);
2442
2443         if (ast_dial_run(props->dial, NULL, 1) == AST_DIAL_RESULT_FAILED) {
2444                 ao2_ref(props, -1);
2445                 return -1;
2446         }
2447
2448         bridge_ringing(props->transferee_bridge);
2449         return 0;
2450 }
2451
2452 static enum attended_transfer_state recalling_exit(struct attended_transfer_properties *props,
2453                 enum attended_transfer_stimulus stimulus)
2454 {
2455         /* No matter what the outcome was, we need to kill off the dial */
2456         ast_dial_join(props->dial);
2457         ast_dial_destroy(props->dial);
2458         props->dial = NULL;
2459         /* This reference is the one we incremented for the dial state callback (recall_callback) to use */
2460         ao2_ref(props, -1);
2461
2462         switch (stimulus) {
2463         case STIMULUS_TRANSFEREE_HANGUP:
2464                 return TRANSFER_FAIL;
2465         case STIMULUS_TIMEOUT:
2466         case STIMULUS_RECALL_TARGET_HANGUP:
2467                 ++props->retry_attempts;
2468                 if (props->retry_attempts >= props->atxfercallbackretries) {
2469                         return TRANSFER_FAIL;
2470                 }
2471                 if (props->atxferloopdelay) {
2472                         return TRANSFER_WAIT_TO_RETRANSFER;
2473                 }
2474                 return TRANSFER_RETRANSFER;
2475         case STIMULUS_RECALL_TARGET_ANSWER:
2476                 /* Setting this datastore up will allow the transferer to have all of his
2477                  * call features set up automatically when the bridge changes back to a
2478                  * normal personality
2479                  */
2480                 ast_bridge_features_ds_set(props->recall_target, &props->transferer_features);
2481                 ast_channel_ref(props->recall_target);
2482                 if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
2483                         AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
2484                         ast_hangup(props->recall_target);
2485                         ast_channel_unref(props->recall_target);
2486                         return TRANSFER_FAIL;
2487                 }
2488                 return TRANSFER_RESUME;
2489         case STIMULUS_NONE:
2490         case STIMULUS_DTMF_ATXFER_ABORT:
2491         case STIMULUS_DTMF_ATXFER_COMPLETE:
2492         case STIMULUS_DTMF_ATXFER_THREEWAY:
2493         case STIMULUS_DTMF_ATXFER_SWAP:
2494         case STIMULUS_TRANSFER_TARGET_HANGUP:
2495         case STIMULUS_TRANSFER_TARGET_ANSWER:
2496         case STIMULUS_TRANSFERER_HANGUP:
2497         default:
2498                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2499                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2500                 return props->state;
2501         }
2502 }
2503
2504 static int wait_to_retransfer_enter(struct attended_transfer_properties *props)
2505 {
2506         bridge_hold(props->transferee_bridge);
2507         return 0;
2508 }
2509
2510 static enum attended_transfer_state wait_to_retransfer_exit(struct attended_transfer_properties *props,
2511                 enum attended_transfer_stimulus stimulus)
2512 {
2513         bridge_unhold(props->transferee_bridge);
2514         switch (stimulus) {
2515         case STIMULUS_TRANSFEREE_HANGUP:
2516                 return TRANSFER_FAIL;
2517         case STIMULUS_TIMEOUT:
2518                 return TRANSFER_RETRANSFER;
2519         case STIMULUS_NONE:
2520         case STIMULUS_DTMF_ATXFER_ABORT:
2521         case STIMULUS_DTMF_ATXFER_COMPLETE:
2522         case STIMULUS_DTMF_ATXFER_THREEWAY:
2523         case STIMULUS_DTMF_ATXFER_SWAP:
2524         case STIMULUS_TRANSFER_TARGET_HANGUP:
2525         case STIMULUS_TRANSFER_TARGET_ANSWER:
2526         case STIMULUS_TRANSFERER_HANGUP:
2527         case STIMULUS_RECALL_TARGET_HANGUP:
2528         case STIMULUS_RECALL_TARGET_ANSWER:
2529         default:
2530                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2531                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2532                 return props->state;
2533         }
2534 }
2535
2536 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel);
2537
2538 static int retransfer_enter(struct attended_transfer_properties *props)
2539 {
2540         RAII_VAR(struct ast_format_cap *, cap, ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT), ao2_cleanup);
2541         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
2542         int cause;
2543
2544         if (!cap) {
2545                 return -1;
2546         }
2547
2548         snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
2549
2550         ast_format_cap_append(cap, ast_format_slin, 0);
2551
2552         /* Get a channel that is the destination we wish to call */
2553         props->recall_target = ast_request("Local", cap, NULL, NULL, destination, &cause);
2554         if (!props->recall_target) {
2555                 ast_log(LOG_ERROR, "Unable to request outbound channel for recall target\n");
2556                 return -1;
2557         }
2558
2559         if (attach_framehook(props, props->recall_target)) {
2560                 ast_log(LOG_ERROR, "Unable to attach framehook to recall target\n");
2561                 ast_hangup(props->recall_target);
2562                 props->recall_target = NULL;
2563                 return -1;
2564         }
2565
2566         /*
2567          * Setup callid, variables, datastores, accountcode, peeraccount,
2568          * and COLP on the recalled transfer target.
2569          */
2570         ast_channel_lock_both(props->recall_target, props->transferer);
2571
2572         ast_party_connected_line_copy(ast_channel_connected(props->recall_target),
2573                 &props->original_transferer_colp);
2574         ast_party_id_reset(&ast_channel_connected(props->recall_target)->priv);
2575
2576         common_recall_channel_setup(props->recall_target, props->recall_target);
2577         ast_channel_unlock(props->recall_target);
2578         ast_channel_unlock(props->transferer);
2579
2580         if (ast_call(props->recall_target, destination, 0)) {
2581                 ast_log(LOG_ERROR, "Unable to place outbound call to recall target\n");
2582                 ast_hangup(props->recall_target);
2583                 props->recall_target = NULL;
2584                 return -1;
2585         }
2586
2587         ast_channel_ref(props->recall_target);
2588         if (ast_bridge_impart(props->transferee_bridge, props->recall_target, NULL, NULL,
2589                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
2590                 ast_log(LOG_ERROR, "Unable to place recall target into bridge\n");
2591                 ast_hangup(props->recall_target);
2592                 ast_channel_unref(props->recall_target);
2593                 return -1;
2594         }
2595
2596         return 0;
2597 }
2598
2599 static enum attended_transfer_state retransfer_exit(struct attended_transfer_properties *props,
2600                 enum attended_transfer_stimulus stimulus)
2601 {
2602         switch (stimulus) {
2603         case STIMULUS_TRANSFEREE_HANGUP:
2604                 return TRANSFER_FAIL;
2605         case STIMULUS_TIMEOUT:
2606                 ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
2607         case STIMULUS_RECALL_TARGET_HANGUP:
2608                 props->recall_target = ast_channel_unref(props->recall_target);
2609                 if (props->atxferloopdelay) {
2610                         return TRANSFER_WAIT_TO_RECALL;
2611                 }
2612                 return TRANSFER_RECALLING;
2613         case STIMULUS_RECALL_TARGET_ANSWER:
2614                 return TRANSFER_RESUME;
2615         case STIMULUS_NONE:
2616         case STIMULUS_DTMF_ATXFER_ABORT:
2617         case STIMULUS_DTMF_ATXFER_COMPLETE:
2618         case STIMULUS_DTMF_ATXFER_THREEWAY:
2619         case STIMULUS_DTMF_ATXFER_SWAP:
2620         case STIMULUS_TRANSFER_TARGET_HANGUP:
2621         case STIMULUS_TRANSFER_TARGET_ANSWER:
2622         case STIMULUS_TRANSFERER_HANGUP:
2623         default:
2624                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2625                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2626                 return props->state;
2627         }
2628 }
2629
2630 static int wait_to_recall_enter(struct attended_transfer_properties *props)
2631 {
2632         bridge_hold(props->transferee_bridge);
2633         return 0;
2634 }
2635
2636 static enum attended_transfer_state wait_to_recall_exit(struct attended_transfer_properties *props,
2637                 enum attended_transfer_stimulus stimulus)
2638 {
2639         bridge_unhold(props->transferee_bridge);
2640         switch (stimulus) {
2641         case STIMULUS_TRANSFEREE_HANGUP:
2642                 return TRANSFER_FAIL;
2643         case STIMULUS_TIMEOUT:
2644                 return TRANSFER_RECALLING;
2645         case STIMULUS_NONE:
2646         case STIMULUS_DTMF_ATXFER_ABORT:
2647         case STIMULUS_DTMF_ATXFER_COMPLETE:
2648         case STIMULUS_DTMF_ATXFER_THREEWAY:
2649         case STIMULUS_DTMF_ATXFER_SWAP:
2650         case STIMULUS_TRANSFER_TARGET_HANGUP:
2651         case STIMULUS_TRANSFER_TARGET_ANSWER:
2652         case STIMULUS_TRANSFERER_HANGUP:
2653         case STIMULUS_RECALL_TARGET_HANGUP:
2654         case STIMULUS_RECALL_TARGET_ANSWER:
2655         default:
2656                 ast_log(LOG_WARNING, "Unexpected stimulus '%s' received in attended transfer state '%s'\n",
2657                                 stimulus_strs[stimulus], state_properties[props->state].state_name);
2658                 return props->state;
2659         }
2660 }
2661
2662 static int fail_enter(struct attended_transfer_properties *props)
2663 {
2664         if (props->transferee_bridge) {
2665                 ast_bridge_destroy(props->transferee_bridge, 0);
2666                 props->transferee_bridge = NULL;
2667         }
2668         return 0;
2669 }
2670
2671 /*!
2672  * \brief DTMF hook when transferer presses abort sequence.
2673  *
2674  * Sends a stimulus to the attended transfer monitor thread that the abort sequence has been pressed
2675  */
2676 static int atxfer_abort(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2677 {
2678         struct attended_transfer_properties *props = hook_pvt;
2679
2680         ast_debug(1, "Transferer on attended transfer %p pressed abort sequence\n", props);
2681         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_ABORT);
2682         return 0;
2683 }
2684
2685 /*!
2686  * \brief DTMF hook when transferer presses complete sequence.
2687  *
2688  * Sends a stimulus to the attended transfer monitor thread that the complete sequence has been pressed
2689  */
2690 static int atxfer_complete(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2691 {
2692         struct attended_transfer_properties *props = hook_pvt;
2693
2694         ast_debug(1, "Transferer on attended transfer %p pressed complete sequence\n", props);
2695         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_COMPLETE);
2696         return 0;
2697 }
2698
2699 /*!
2700  * \brief DTMF hook when transferer presses threeway sequence.
2701  *
2702  * Sends a stimulus to the attended transfer monitor thread that the threeway sequence has been pressed
2703  */
2704 static int atxfer_threeway(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2705 {
2706         struct attended_transfer_properties *props = hook_pvt;
2707
2708         ast_debug(1, "Transferer on attended transfer %p pressed threeway sequence\n", props);
2709         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_THREEWAY);
2710         return 0;
2711 }
2712
2713 /*!
2714  * \brief DTMF hook when transferer presses swap sequence.
2715  *
2716  * Sends a stimulus to the attended transfer monitor thread that the swap sequence has been pressed
2717  */
2718 static int atxfer_swap(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2719 {
2720         struct attended_transfer_properties *props = hook_pvt;
2721
2722         ast_debug(1, "Transferer on attended transfer %p pressed swap sequence\n", props);
2723         stimulate_attended_transfer(props, STIMULUS_DTMF_ATXFER_SWAP);
2724         return 0;
2725 }
2726
2727 /*!
2728  * \brief Hangup hook for transferer channel.
2729  *
2730  * Sends a stimulus to the attended transfer monitor thread that the transferer has hung up.
2731  */
2732 static int atxfer_transferer_hangup(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2733 {
2734         struct attended_transfer_properties *props = hook_pvt;
2735
2736         ast_debug(1, "Transferer on attended transfer %p hung up\n", props);
2737         stimulate_attended_transfer(props, STIMULUS_TRANSFERER_HANGUP);
2738         return 0;
2739 }
2740
2741 /*!
2742  * \brief Frame hook for transfer target channel
2743  *
2744  * This is used to determine if the transfer target or recall target has answered
2745  * the outgoing call.
2746  *
2747  * When an answer is detected, a stimulus is sent to the attended transfer monitor
2748  * thread to indicate that the transfer target or recall target has answered.
2749  *
2750  * \param chan The channel the framehook is attached to.
2751  * \param frame The frame being read or written.
2752  * \param event What is being done with the frame.
2753  * \param data The attended transfer properties.
2754  */
2755 static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
2756                 struct ast_frame *frame, enum ast_framehook_event event, void *data)
2757 {
2758         struct attended_transfer_properties *props = data;
2759
2760         if (event == AST_FRAMEHOOK_EVENT_READ &&
2761                         frame && frame->frametype == AST_FRAME_CONTROL &&
2762                         frame->subclass.integer == AST_CONTROL_ANSWER) {
2763
2764                 ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
2765                 if (props->superstate == SUPERSTATE_TRANSFER) {
2766                         stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_ANSWER);
2767                 } else {
2768                         stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_ANSWER);
2769                 }
2770                 ast_framehook_detach(chan, props->target_framehook_id);
2771                 props->target_framehook_id = -1;
2772         }
2773
2774         return frame;
2775 }
2776
2777 /*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
2778 static int transfer_target_framehook_consume(void *data, enum ast_frame_type type)
2779 {
2780         return (type == AST_FRAME_CONTROL ? 1 : 0);
2781 }
2782
2783 static void transfer_target_framehook_destroy_cb(void *data)
2784 {
2785         struct attended_transfer_properties *props = data;
2786         ao2_cleanup(props);
2787 }
2788
2789 static int bridge_personality_atxfer_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
2790 {
2791         const char *abort_dtmf;
2792         const char *complete_dtmf;
2793         const char *threeway_dtmf;
2794         const char *swap_dtmf;
2795         struct bridge_basic_personality *personality = self->personality;
2796
2797         if (!ast_channel_has_role(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME)) {
2798                 return 0;
2799         }
2800
2801         abort_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "abort");
2802         complete_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "complete");
2803         threeway_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "threeway");
2804         swap_dtmf = ast_channel_get_role_option(bridge_channel->chan, AST_TRANSFERER_ROLE_NAME, "swap");
2805
2806         if (!ast_strlen_zero(abort_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2807                         abort_dtmf, atxfer_abort, personality->details[personality->current].pvt, NULL,
2808                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2809                 return -1;
2810         }
2811         if (!ast_strlen_zero(complete_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2812                         complete_dtmf, atxfer_complete, personality->details[personality->current].pvt, NULL,
2813                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2814                 return -1;
2815         }
2816         if (!ast_strlen_zero(threeway_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2817                         threeway_dtmf, atxfer_threeway, personality->details[personality->current].pvt, NULL,
2818                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2819                 return -1;
2820         }
2821         if (!ast_strlen_zero(swap_dtmf) && ast_bridge_dtmf_hook(bridge_channel->features,
2822                         swap_dtmf, atxfer_swap, personality->details[personality->current].pvt, NULL,
2823                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2824                 return -1;
2825         }
2826         if (ast_bridge_hangup_hook(bridge_channel->features, atxfer_transferer_hangup,
2827                         personality->details[personality->current].pvt, NULL,
2828                         AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE | AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
2829                 return -1;
2830         }
2831
2832         return 0;
2833 }
2834
2835 static void transfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2836 {
2837         if (self->num_channels > 1 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2838                 return;
2839         }
2840
2841         if (self->num_channels == 1) {
2842                 RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
2843
2844                 ast_channel_lock(props->transferer);
2845                 transferer_bridge_channel = ast_channel_get_bridge_channel(props->transferer);
2846                 ast_channel_unlock(props->transferer);
2847
2848                 if (!transferer_bridge_channel) {
2849                         return;
2850                 }
2851
2852                 if (AST_LIST_FIRST(&self->channels) != transferer_bridge_channel) {
2853                         return;
2854                 }
2855         }
2856
2857         /* Reaching this point means that either
2858          * 1) The bridge has no channels in it
2859          * 2) The bridge has one channel, and it's the transferer
2860          * In either case, it indicates that the non-transferer parties
2861          * are no longer in the bridge.
2862          */
2863         if (self == props->transferee_bridge) {
2864                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2865         } else {
2866                 stimulate_attended_transfer(props, STIMULUS_TRANSFER_TARGET_HANGUP);
2867         }
2868 }
2869
2870 static void recall_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct attended_transfer_properties *props)
2871 {
2872         if (self == props->target_bridge) {
2873                 /* Once we're in the recall superstate, we no longer care about this bridge */
2874                 return;
2875         }
2876
2877         if (bridge_channel->chan == props->recall_target) {
2878                 stimulate_attended_transfer(props, STIMULUS_RECALL_TARGET_HANGUP);
2879                 return;
2880         }
2881
2882         if (self->num_channels == 0) {
2883                 /* Empty bridge means all transferees are gone for sure */
2884                 stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2885                 return;
2886         }
2887
2888         if (self->num_channels == 1) {
2889                 RAII_VAR(struct ast_bridge_channel *, target_bridge_channel, NULL, ao2_cleanup);
2890                 if (!props->recall_target) {
2891                         /* No recall target means that the pull happened on a transferee. If there's still
2892                          * a channel left in the bridge, we don't need to send a stimulus
2893                          */
2894                         return;
2895                 }
2896
2897                 ast_channel_lock(props->recall_target);
2898                 target_bridge_channel = ast_channel_get_bridge_channel(props->recall_target);
2899                 ast_channel_unlock(props->recall_target);
2900
2901                 if (!target_bridge_channel) {
2902                         return;
2903                 }
2904
2905                 if (AST_LIST_FIRST(&self->channels) == target_bridge_channel) {
2906                         stimulate_attended_transfer(props, STIMULUS_TRANSFEREE_HANGUP);
2907                 }
2908         }
2909 }
2910
2911 static void bridge_personality_atxfer_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
2912 {
2913         struct bridge_basic_personality *personality = self->personality;
2914         struct attended_transfer_properties *props = personality->details[personality->current].pvt;
2915
2916         switch (props->superstate) {
2917         case SUPERSTATE_TRANSFER:
2918                 transfer_pull(self, bridge_channel, props);
2919                 break;
2920         case SUPERSTATE_RECALL:
2921                 recall_pull(self, bridge_channel, props);
2922                 break;
2923         }
2924 }
2925
2926 static enum attended_transfer_stimulus wait_for_stimulus(struct attended_transfer_properties *props)
2927 {
2928         RAII_VAR(struct stimulus_list *, list, NULL, ast_free_ptr);
2929         SCOPED_MUTEX(lock, ao2_object_get_lockaddr(props));
2930
2931         while (!(list = AST_LIST_REMOVE_HEAD(&props->stimulus_queue, next))) {
2932                 if (!(state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMED)) {
2933                         ast_cond_wait(&props->cond, lock);
2934                 } else {
2935                         struct timeval relative_timeout = { 0, };
2936                         struct timeval absolute_timeout;
2937                         struct timespec timeout_arg;
2938
2939                         if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_RESET) {
2940                                 props->start = ast_tvnow();
2941                         }
2942
2943                         if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TIMER_LOOP_DELAY) {
2944                                 relative_timeout.tv_sec = props->atxferloopdelay;
2945                         } else {
2946                                 /* Implied TRANSFER_STATE_FLAG_TIMER_ATXFER_NO_ANSWER */
2947                                 relative_timeout.tv_sec = props->atxfernoanswertimeout;
2948                         }
2949
2950                         absolute_timeout = ast_tvadd(props->start, relative_timeout);
2951                         timeout_arg.tv_sec = absolute_timeout.tv_sec;
2952                         timeout_arg.tv_nsec = absolute_timeout.tv_usec * 1000;
2953
2954                         if (ast_cond_timedwait(&props->cond, lock, &timeout_arg) == ETIMEDOUT) {
2955                                 return STIMULUS_TIMEOUT;
2956                         }
2957                 }
2958         }
2959         return list->stimulus;
2960 }
2961
2962 /*!
2963  * \brief The main loop for the attended transfer monitor thread.
2964  *
2965  * This loop runs continuously until the attended transfer reaches
2966  * a terminal state. Stimuli for changes in the attended transfer
2967  * state are handled in this thread so that all factors in an
2968  * attended transfer can be handled in an orderly fashion.
2969  *
2970  * \param data The attended transfer properties
2971  */
2972 static void *attended_transfer_monitor_thread(void *data)
2973 {
2974         struct attended_transfer_properties *props = data;
2975         ast_callid callid;
2976
2977         /*
2978          * Set thread callid to the transferer's callid because we
2979          * are doing all this on that channel's behalf.
2980          */
2981         ast_channel_lock(props->transferer);
2982         callid = ast_channel_callid(props->transferer);
2983         ast_channel_unlock(props->transferer);
2984         if (callid) {
2985                 ast_callid_threadassoc_add(callid);
2986         }
2987
2988         for (;;) {
2989                 enum attended_transfer_stimulus stimulus;
2990
2991                 ast_debug(1, "About to enter state %s for attended transfer %p\n", state_properties[props->state].state_name, props);
2992
2993                 if (state_properties[props->state].enter &&
2994                                 state_properties[props->state].enter(props)) {
2995                         ast_log(LOG_ERROR, "State %s enter function returned an error for attended transfer %p\n",
2996                                         state_properties[props->state].state_name, props);
2997                         break;
2998                 }
2999
3000                 if (state_properties[props->state].flags & TRANSFER_STATE_FLAG_TERMINAL) {
3001                         ast_debug(1, "State %s is a terminal state. Ending attended transfer %p\n",
3002                                         state_properties[props->state].state_name, props);
3003                         break;
3004                 }
3005
3006                 stimulus = wait_for_stimulus(props);
3007
3008                 ast_debug(1, "Received stimulus %s on attended transfer %p\n", stimulus_strs[stimulus], props);
3009
3010                 ast_assert(state_properties[props->state].exit != NULL);
3011
3012                 props->state = state_properties[props->state].exit(props, stimulus);
3013
3014                 ast_debug(1, "Told to enter state %s exit on attended transfer %p\n", state_properties[props->state].state_name, props);
3015         }
3016
3017         attended_transfer_properties_shutdown(props);
3018
3019         if (callid) {
3020                 ast_callid_threadassoc_remove();
3021         }
3022
3023         return NULL;
3024 }
3025
3026 static int attach_framehook(struct attended_transfer_properties *props, struct ast_channel *channel)
3027 {
3028         struct ast_framehook_interface target_interface = {
3029                 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
3030                 .event_cb = transfer_target_framehook_cb,
3031                 .destroy_cb = transfer_target_framehook_destroy_cb,
3032                 .consume_cb = transfer_target_framehook_consume,
3033                 .disable_inheritance = 1,
3034         };
3035
3036         ao2_ref(props, +1);
3037         target_interface.data = props;
3038
3039         props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
3040         if (props->target_framehook_id == -1) {
3041                 ao2_ref(props, -1);
3042                 return -1;
3043         }
3044         return 0;
3045 }
3046
3047 static int add_transferer_role(struct ast_channel *chan, struct ast_bridge_features_attended_transfer *attended_transfer)
3048 {
3049         const char *atxfer_abort;
3050         const char *atxfer_threeway;
3051         const char *atxfer_complete;
3052         const char *atxfer_swap;
3053         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
3054         SCOPED_CHANNELLOCK(lock, chan);
3055
3056         xfer_cfg = ast_get_chan_features_xfer_config(chan);
3057         if (!xfer_cfg) {
3058                 return -1;
3059         }
3060         if (attended_transfer) {
3061                 atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
3062                 atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
3063                 atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
3064                 atxfer_swap = ast_strdupa(S_OR(attended_transfer->swap, xfer_cfg->atxferswap));
3065         } else {
3066                 atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
3067                 atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
3068                 atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
3069                 atxfer_swap = ast_strdupa(xfer_cfg->atxferswap);
3070         }
3071
3072         return ast_channel_add_bridge_role(chan, AST_TRANSFERER_ROLE_NAME) ||
3073                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "abort", atxfer_abort) ||
3074                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "complete", atxfer_complete) ||
3075                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "threeway", atxfer_threeway) ||
3076                 ast_channel_set_bridge_role_option(chan, AST_TRANSFERER_ROLE_NAME, "swap", atxfer_swap);
3077 }
3078
3079 /*!
3080  * \brief Helper function that presents dialtone and grabs extension
3081  *
3082  * \retval 0 on success
3083  * \retval -1 on failure
3084  */
3085 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
3086 {
3087         int res;
3088         int digit_timeout;
3089         int attempts = 0;
3090         int max_attempts;
3091         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
3092         char *retry_sound;
3093         char *invalid_sound;
3094
3095         ast_channel_lock(chan);
3096         xfer_cfg = ast_get_chan_features_xfer_config(chan);
3097         if (!xfer_cfg) {
3098                 ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
3099                 ast_channel_unlock(chan);
3100                 return -1;
3101         }
3102         digit_timeout = xfer_cfg->transferdigittimeout * 1000;
3103         max_attempts = xfer_cfg->transferdialattempts;
3104         retry_sound = ast_strdupa(xfer_cfg->transferretrysound);
3105         invalid_sound = ast_strdupa(xfer_cfg->transferinvalidsound);
3106         ast_channel_unlock(chan);
3107
3108         /* Play the simple "transfer" prompt out and wait */
3109         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
3110         ast_stopstream(chan);
3111         if (res < 0) {
3112                 /* Hangup or error */
3113                 return -1;
3114         }
3115         if (res) {
3116                 /* Store the DTMF digit that interrupted playback of the file. */
3117                 exten[0] = res;
3118         }
3119
3120         /* Drop to dialtone so they can enter the extension they want to transfer to */
3121         do {
3122                 ++attempts;
3123
3124                 ast_test_suite_event_notify("TRANSFER_BEGIN_DIAL",
3125                                 "Channel: %s\r\n"
3126                                 "Attempt: %d",
3127                                 ast_channel_name(chan), attempts);
3128                 res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
3129                 ast_test_suite_event_notify("TRANSFER_DIALLED",
3130                                 "Channel: %s\r\n"
3131                                 "Attempt: %d\r\n"
3132                                 "Dialled: %s\r\n"
3133                                 "Result: %s",
3134                                 ast_channel_name(chan), attempts, exten, res > 0 ? "Success" : "Failure");
3135                 if (res < 0) {
3136                         /* Hangup or error */
3137                         res = -1;
3138                 } else if (!res) {
3139                         /* 0 for invalid extension dialed. */
3140                         if (ast_strlen_zero(exten)) {
3141                                 ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
3142                         } else {
3143                                 ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
3144                                         ast_channel_name(chan), exten, context);
3145                         }
3146                         if (attempts < max_attempts) {
3147                                 ast_stream_and_wait(chan, retry_sound, AST_DIGIT_NONE);
3148                         } else {
3149                                 ast_stream_and_wait(chan, invalid_sound, AST_DIGIT_NONE);
3150                         }
3151                         memset(exten, 0, exten_len);
3152                         res = 1;
3153                 } else {
3154                         /* Dialed extension is valid. */
3155                         res = 0;
3156                 }
3157         } while (res > 0 && attempts < max_attempts);
3158
3159         ast_test_suite_event_notify("TRANSFER_DIAL_FINAL",
3160                         "Channel: %s\r\n"
3161                         "Result: %s",
3162                         ast_channel_name(chan), res == 0 ? "Success" : "Failure");
3163
3164         return res ? -1 : 0;
3165 }
3166
3167 static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
3168 {
3169         ast_channel_lock_both(caller, dest);
3170         ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
3171         ast_channel_inherit_variables(caller, dest);
3172         ast_channel_datastore_inherit(caller, dest);
3173         ast_channel_unlock(dest);
3174         ast_channel_unlock(caller);
3175 }
3176
3177 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
3178 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *destination)
3179 {
3180         struct ast_channel *chan;
3181         int cause;
3182
3183         /* Now we request a local channel to prepare to call the destination */
3184         chan = ast_request("Local", ast_channel_nativeformats(caller), NULL, caller, destination,
3185                 &cause);
3186         if (!chan) {
3187                 return NULL;
3188         }
3189
3190         ast_channel_lock_both(chan, caller);
3191
3192         ast_channel_req_accountcodes(chan, caller, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
3193
3194         /* Who is transferring the call. */
3195         pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
3196
3197         ast_bridge_set_transfer_variables(chan, ast_channel_name(caller), 1);
3198
3199         ast_channel_unlock(chan);
3200         ast_channel_unlock(caller);
3201
3202         /* Before we actually dial out let's inherit appropriate information. */
3203         copy_caller_data(chan, caller);
3204
3205         return chan;
3206 }
3207
3208 /*!
3209  * \brief Internal built in feature for attended transfers
3210  *
3211  * This hook will set up a thread for monitoring the progress of
3212  * an attended transfer. For more information about attended transfer
3213  * progress, see documentation on the transfer state machine.
3214  *
3215  * \param bridge_channel The channel that pressed the attended transfer DTMF sequence
3216  * \param hook_pvt Structure with further information about the attended transfer
3217  */
3218 static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
3219 {
3220         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
3221         struct attended_transfer_properties *props;
3222         struct ast_bridge *bridge;
3223         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
3224         char exten[AST_MAX_EXTENSION] = "";
3225         pthread_t thread;
3226
3227         /* Inhibit the bridge before we do anything else. */
3228         bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
3229
3230         if (strcmp(bridge->v_table->name, "basic")) {
3231                 ast_log(LOG_ERROR, "Attended transfer attempted on unsupported bridge type '%s'.\n",
3232                         bridge->v_table->name);
3233                 ast_bridge_merge_inhibit(bridge, -1);
3234                 ao2_ref(bridge, -1);
3235                 return 0;
3236         }
3237
3238         /* Was the bridge inhibited before we inhibited it? */
3239         if (1 < bridge->inhibit_merge) {
3240                 /*
3241                  * The peer likely initiated attended transfer at the same time
3242                  * and we lost the race.
3243                  */
3244                 ast_verb(3, "Channel %s: Bridge '%s' does not permit merging at this time.\n",
3245                         ast_channel_name(bridge_channel->chan), bridge->uniqueid);
3246                 ast_bridge_merge_inhibit(bridge, -1);
3247                 ao2_ref(bridge, -1);
3248                 return 0;
3249         }
3250
3251         props = attended_transfer_properties_alloc(bridge_channel->chan,
3252                 attended_transfer ? attended_transfer->context : NULL);
3253         if (!props) {
3254                 ast_log(LOG_ERROR, "Unable to allocate control structure for performing attended transfer.\n");
3255                 ast_bridge_merge_inhibit(bridge, -1);
3256                 ao2_ref(bridge, -1);
3257                 return 0;
3258         }
3259
3260         props->transferee_bridge = bridge;
3261
3262         if (add_transferer_role(props->transferer, attended_transfer)) {
3263                 ast_log(LOG_ERROR, "Unable to set transferrer bridge role.\n");
3264                 attended_transfer_properties_shutdown(props);
3265                 return 0;
3266         }
3267
3268         ast_bridge_channel_write_hold(bridge_channel, NULL);
3269
3270         /* Grab the extension to transfer to */
3271         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
3272                 ast_log(LOG_WARNING, "Unable to acquire target extension for attended transfer.\n");
3273                 ast_bridge_channel_write_unhold(bridge_channel);
3274                 attended_transfer_properties_shutdown(props);
3275                 return 0;
3276         }
3277
3278         ast_string_field_set(props, exten, exten);
3279
3280         /* Fill the variable with the extension and context we want to call */
3281         snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
3282
3283         ast_debug(1, "Attended transfer to '%s'\n", destination);
3284
3285         /* Get a channel that is the destination we wish to call */
3286         props->transfer_target = dial_transfer(bridge_channel->chan, destination);
3287         if (!props->transfer_target) {
3288                 ast_log(LOG_ERROR, "Unable to request outbound channel for attended transfer target.\n");
3289                 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
3290                 ast_bridge_channel_write_unhold(bridge_channel);
3291                 attended_transfer_properties_shutdown(props);
3292                 return 0;
3293         }
3294
3295
3296         /* Create a bridge to use to talk to the person we are calling */
3297         props->target_bridge = ast_bridge_basic_new();
3298         if (!props->target_bridge) {
3299                 ast_log(LOG_ERROR, "Unable to create bridge for attended transfer target.\n");
3300                 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
3301                 ast_bridge_channel_write_unhold(bridge_channel);
3302                 ast_hangup(props->transfer_target);
3303                 props->transfer_target = NULL;
3304                 attended_transfer_properties_shutdown(props);
3305                 return 0;
3306         }
3307         ast_bridge_merge_inhibit(props->target_bridge, +1);
3308
3309         if (attach_framehook(props, props->transfer_target)) {
3310                 ast_log(LOG_ERROR, "Unable to attach framehook to transfer target.\n");
3311                 ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
3312                 ast_bridge_channel_write_unhold(bridge_channel);
3313                 ast_hangup(props->transfer_target);
3314                 props->transfer_target = NULL;
3315                 attended_transfer_properties_shutdown(props);
3316                 return 0;
3317         }
3318
3319         bridge_basic_change_personality(props->target_bridge,
3320                         BRIDGE_BASIC_PERSONALITY_ATXFER, props);
3321         bridge_basic_change_personality(bridge,
3322                         BRIDGE_BASIC_PERSONALITY_ATXFER, props);
3323
3324         if (ast_call(props->transfer_target, destination, 0)) {
3325                 ast_log(LOG_ERROR, "Unable to place outbound call to transfer target.\n");
3326                 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
3327                 ast_bridge_channel_write_unhold(bridge_channel);
3328                 ast_hangup(props->transfer_target);
3329                 props->transfer_target = NULL;
3330                 attended_transfer_properties_shutdown(props);
3331                 return 0;
3332         }
3333
3334         /* We increase the refcount of the transfer target because ast_bridge_impart() will
3335          * steal the reference we already have. We need to keep a reference, so the only
3336          * choice is to give it a bump
3337          */
3338         ast_channel_ref(props->transfer_target);
3339         if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL,
3340                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
3341                 ast_log(LOG_ERROR, "Unable to place transfer target into bridge.\n");
3342                 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
3343                 ast_bridge_channel_write_unhold(bridge_channel);
3344                 ast_hangup(props->transfer_target);
3345                 props->transfer_target = NULL;
3346                 attended_transfer_properties_shutdown(props);
3347                 return 0;
3348         }
3349
3350         if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
3351                 ast_log(LOG_ERROR, "Unable to create monitoring thread for attended transfer.\n");
3352                 ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
3353                 ast_bridge_channel_write_unhold(bridge_channel);
3354                 attended_transfer_properties_shutdown(props);
3355                 return 0;
3356         }
3357
3358         /* Once the monitoring thread has been created, it is responsible for destroying all
3359          * of the necessary components.
3360          */
3361         return 0;
3362 }
3363
3364 static void blind_transfer_cb(struct ast_channel *new_channel, struct transfer_channel_data *user_data_wrapper,
3365                 enum ast_transfer_type transfer_type)
3366 {
3367         struct ast_channel *transferer_channel = user_data_wrapper->data;
3368
3369         if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
3370                 copy_caller_data(new_channel, transferer_channel);
3371         }
3372 }
3373
3374 /*! \brief Internal built in feature for blind transfers */
3375 static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
3376 {
3377         char exten[AST_MAX_EXTENSION] = "";
3378         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
3379         const char *context;
3380         char *goto_on_blindxfr;
3381
3382         ast_bridge_channel_write_hold(bridge_channel, NULL);
3383
3384         ast_channel_lock(bridge_channel->chan);
3385         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
3386                 blind_transfer ? blind_transfer->context : NULL));
3387         goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
3388                 "GOTO_ON_BLINDXFR"), ""));
3389         ast_channel_unlock(bridge_channel->chan);
3390
3391         /* Grab the extension to transfer to */
3392         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
3393                 ast_bridge_channel_write_unhold(bridge_channel);
3394                 return 0;
3395         }
3396
3397         if (!ast_strlen_zero(goto_on_blindxfr)) {
3398                 ast_debug(1, "After transfer, transferer %s goes to %s\n",
3399                                 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
3400                 ast_bridge_set_after_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
3401         }
3402
3403         if (ast_bridge_transfer_blind(0, bridge_channel->chan, exten, context, blind_transfer_cb,
3404                         bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
3405                         !ast_strlen_zero(goto_on_blindxfr)) {
3406                 ast_bridge_discard_after_goto(bridge_channel->chan);
3407         }
3408
3409         return 0;
3410 }
3411
3412 struct ast_bridge_methods ast_bridge_basic_v_table;
3413 struct ast_bridge_methods personality_normal_v_table;
3414 struct ast_bridge_methods personality_atxfer_v_table;
3415
3416 static void bridge_basic_change_personality(struct ast_bridge *bridge,
3417                 enum bridge_basic_personality_type type, void *user_data)
3418 {
3419         struct bridge_basic_personality *personality = bridge->personality;
3420         SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
3421
3422         remove_hooks_on_personality_change(bridge);
3423
3424         ao2_cleanup(personality->details[personality->current].pvt);
3425         personality->details[personality->current].pvt = NULL;
3426         ast_clear_flag(&bridge->feature_flags, AST_FLAGS_ALL);
3427
3428         personality->current = type;
3429         if (user_data) {
3430                 ao2_ref(user_data, +1);
3431         }
3432         personality->details[personality->current].pvt = user_data;
3433         ast_set_flag(&bridge->feature_flags, personality->details[personality->current].bridge_flags);
3434         if (personality->details[personality->current].on_personality_change) {
3435                 personality->details[personality->current].on_personality_change(bridge);
3436         }
3437 }
3438
3439 static void personality_destructor(void *obj)
3440 {
3441         struct bridge_basic_personality *personality = obj;
3442         int i;
3443
3444         for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
3445                 ao2_cleanup(personality->details[i].pvt);
3446         }
3447 }
3448
3449 static void on_personality_change_normal(struct ast_bridge *bridge)
3450 {
3451         struct ast_bridge_channel *iter;
3452
3453         AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
3454                 if (add_normal_hooks(bridge, iter)) {
3455                         ast_log(LOG_WARNING, "Unable to set up bridge hooks for channel %s. Features may not work properly\n",
3456                                         ast_channel_name(iter->chan));
3457                 }
3458         }
3459 }
3460
3461 static void init_details(struct personality_details *details,
3462                 enum bridge_basic_personality_type type)
3463 {
3464         switch (type) {
3465         case BRIDGE_BASIC_PERSONALITY_NORMAL:
3466                 details->v_table = &personality_normal_v_table;
3467                 details->bridge_flags = NORMAL_FLAGS;
3468                 details->on_personality_change = on_personality_change_normal;
3469                 break;
3470         case BRIDGE_BASIC_PERSONALITY_ATXFER:
3471                 details->v_table = &personality_atxfer_v_table;
3472                 details->bridge_flags = TRANSFER_FLAGS;
3473                 break;
3474         default:
3475                 ast_log(LOG_WARNING, "Asked to initialize unexpected basic bridge personality type.\n");
3476                 break;
3477         }
3478 }
3479
3480 static struct ast_bridge *bridge_basic_personality_alloc(struct ast_bridge *bridge)
3481 {
3482         struct bridge_basic_personality *personality;
3483         int i;
3484
3485         if (!bridge) {
3486                 return NULL;
3487         }
3488
3489         personality = ao2_alloc(sizeof(*personality), personality_destructor);
3490         if (!personality) {
3491                 ao2_ref(bridge, -1);
3492                 return NULL;
3493         }
3494         for (i = 0; i < BRIDGE_BASIC_PERSONALITY_END; ++i) {
3495                 init_details(&personality->details[i], i);
3496         }
3497         personality->current = BRIDGE_BASIC_PERSONALITY_NORMAL;
3498         bridge->personality = personality;
3499
3500         return bridge;
3501 }
3502
3503 struct ast_bridge *ast_bridge_basic_new(void)
3504 {
3505         struct ast_bridge *bridge;
3506
3507         bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_basic_v_table);
3508         bridge = bridge_base_init(bridge,
3509                 AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX
3510                         | AST_BRIDGE_CAPABILITY_MULTIMIX, NORMAL_FLAGS, NULL, NULL, NULL);
3511         bridge = bridge_basic_personality_alloc(bridge);
3512         bridge = bridge_register(bridge);
3513         return bridge;
3514 }
3515
3516 void ast_bridge_basic_set_flags(struct ast_bridge *bridge, unsigned int flags)
3517 {
3518         SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
3519         struct bridge_basic_personality *personality = bridge->personality;
3520
3521         personality->details[personality->current].bridge_flags |= flags;
3522         ast_set_flag(&bridge->feature_flags, flags);
3523 }
3524
3525 void ast_bridging_init_basic(void)
3526 {
3527         /* Setup bridge basic subclass v_table. */
3528         ast_bridge_basic_v_table = ast_bridge_base_v_table;
3529         ast_bridge_basic_v_table.name = "basic";
3530         ast_bridge_basic_v_table.push = bridge_basic_push;
3531         ast_bridge_basic_v_table.pull = bridge_basic_pull;
3532         ast_bridge_basic_v_table.destroy = bridge_basic_destroy;
3533
3534         /*
3535          * Personality vtables don't have the same rules as
3536          * normal bridge vtables.  These vtable functions are
3537          * used as alterations to the ast_bridge_basic_v_table
3538          * method functionality and are checked for NULL before
3539          * calling.
3540          */
3541         personality_normal_v_table.name = "normal";
3542         personality_normal_v_table.push = bridge_personality_normal_push;
3543
3544         personality_atxfer_v_table.name = "attended transfer";
3545         personality_atxfer_v_table.push = bridge_personality_atxfer_push;
3546         personality_atxfer_v_table.pull = bridge_personality_atxfer_pull;
3547
3548         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
3549         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
3550 }
3551