Debian Codegraph @

main/bridge_basic.c

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