forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge_basic.c
3664 lines (3268 loc) · 119 KB
/
bridge_basic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013 Digium, Inc.
*
* Richard Mudgett <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief Basic bridge class. It is a subclass of struct ast_bridge.
*
* \author Richard Mudgett <[email protected]>
*
* See Also:
* \arg \ref AstCREDITS
*/
#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/bridge.h"
#include "asterisk/bridge_internal.h"
#include "asterisk/bridge_basic.h"
#include "asterisk/bridge_after.h"
#include "asterisk/astobj2.h"
#include "asterisk/features_config.h"
#include "asterisk/pbx.h"
#include "asterisk/file.h"
#include "asterisk/app.h"
#include "asterisk/dial.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/features.h"
#include "asterisk/format_cache.h"
#include "asterisk/test.h"
#define NORMAL_FLAGS (AST_BRIDGE_FLAG_DISSOLVE_HANGUP | AST_BRIDGE_FLAG_DISSOLVE_EMPTY \
| AST_BRIDGE_FLAG_SMART)
#define TRANSFER_FLAGS AST_BRIDGE_FLAG_SMART
struct attended_transfer_properties;
enum bridge_basic_personality_type {
/*! Index for "normal" basic bridge personality */
BRIDGE_BASIC_PERSONALITY_NORMAL,
/*! Index for attended transfer basic bridge personality */
BRIDGE_BASIC_PERSONALITY_ATXFER,
/*! Indicates end of enum. Must always remain the last element */
BRIDGE_BASIC_PERSONALITY_END,
};
/*!
* \brief Change basic bridge personality
*
* Changing personalities allows for the bridge to remain in use but have
* properties such as its v_table and its flags change.
*
* \param bridge The bridge
* \param type The personality to change the bridge to
* \user_data Private data to attach to the personality.
*/
static void bridge_basic_change_personality(struct ast_bridge *bridge,
enum bridge_basic_personality_type type, void *user_data);
/* ------------------------------------------------------------------- */
static const struct ast_datastore_info dtmf_features_info = {
.type = "bridge-dtmf-features",
.destroy = ast_free_ptr,
};
/*!
* \internal
* \since 12.0.0
* \brief read a feature code character and set it on for the give feature_flags struct
*
* \param feature_flags flags being modifed
* \param feature feature code provided - should be an uppercase letter
*
* \retval 0 if the feature was set successfully
* \retval -1 failure because the requested feature code isn't handled by this function
*/
static int set_feature_flag_from_char(struct ast_flags *feature_flags, char feature)
{
switch (feature) {
case 'T':
ast_set_flag(feature_flags, AST_FEATURE_REDIRECT);
return 0;
case 'K':
ast_set_flag(feature_flags, AST_FEATURE_PARKCALL);
return 0;
case 'H':
ast_set_flag(feature_flags, AST_FEATURE_DISCONNECT);
return 0;
case 'W':
ast_set_flag(feature_flags, AST_FEATURE_AUTOMON);
return 0;
case 'X':
ast_set_flag(feature_flags, AST_FEATURE_AUTOMIXMON);
return 0;
default:
return -1;
}
}
/*!
* \internal
* \since 12.0.0
* \brief Write a features string to a string buffer based on the feature flags provided
*
* \param feature_flags pointer to the feature flags to write from.
* \param buffer pointer to a string buffer to write the features
* \param buffer_size size of the buffer provided (should be able to fit all feature codes)
*
* \retval 0 on successful write
* \retval -1 failure due to running out of buffer space
*/
static int dtmf_features_flags_to_string(struct ast_flags *feature_flags, char *buffer, size_t buffer_size)
{
size_t buffer_expended = 0;
unsigned int cur_feature;
static const struct {
char letter;
unsigned int flag;
} associations[] = {
{ 'T', AST_FEATURE_REDIRECT },
{ 'K', AST_FEATURE_PARKCALL },
{ 'H', AST_FEATURE_DISCONNECT },
{ 'W', AST_FEATURE_AUTOMON },
{ 'X', AST_FEATURE_AUTOMIXMON },
};
for (cur_feature = 0; cur_feature < ARRAY_LEN(associations); cur_feature++) {
if (ast_test_flag(feature_flags, associations[cur_feature].flag)) {
if (buffer_expended == buffer_size - 1) {
buffer[buffer_expended] = '\0';
return -1;
}
buffer[buffer_expended++] = associations[cur_feature].letter;
}
}
buffer[buffer_expended] = '\0';
return 0;
}
static int build_dtmf_features(struct ast_flags *flags, const char *features)
{
const char *feature;
char missing_features[strlen(features) + 1];
size_t number_of_missing_features = 0;
for (feature = features; *feature; feature++) {
if (!isupper(*feature)) {
ast_log(LOG_ERROR, "Features string '%s' rejected because it contains non-uppercase feature.\n", features);
return -1;
}
if (set_feature_flag_from_char(flags, *feature)) {
missing_features[number_of_missing_features++] = *feature;
}
}
missing_features[number_of_missing_features] = '\0';
if (number_of_missing_features) {
ast_log(LOG_WARNING, "Features '%s' from features string '%s' can not be applied.\n", missing_features, features);
}
return 0;
}
int ast_bridge_features_ds_set_string(struct ast_channel *chan, const char *features)
{
struct ast_flags flags = {0};
if (build_dtmf_features(&flags, features)) {
return -1;
}
ast_channel_lock(chan);
if (ast_bridge_features_ds_set(chan, &flags)) {
ast_channel_unlock(chan);
ast_log(LOG_ERROR, "Failed to apply features datastore for '%s' to channel '%s'\n", features, ast_channel_name(chan));
return -1;
}
ast_channel_unlock(chan);
return 0;
}
int ast_bridge_features_ds_get_string(struct ast_channel *chan, char *buffer, size_t buf_size)
{
struct ast_flags *channel_flags;
struct ast_flags held_copy;
ast_channel_lock(chan);
if (!(channel_flags = ast_bridge_features_ds_get(chan))) {
ast_channel_unlock(chan);
return -1;
}
held_copy = *channel_flags;
ast_channel_unlock(chan);
return dtmf_features_flags_to_string(&held_copy, buffer, buf_size);
}
static int bridge_features_ds_set_full(struct ast_channel *chan, struct ast_flags *flags, int replace)
{
struct ast_datastore *datastore;
struct ast_flags *ds_flags;
datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
if (datastore) {
ds_flags = datastore->data;
if (replace) {
*ds_flags = *flags;
} else {
flags->flags = flags->flags | ds_flags->flags;
*ds_flags = *flags;
}
return 0;
}
datastore = ast_datastore_alloc(&dtmf_features_info, NULL);
if (!datastore) {
return -1;
}
ds_flags = ast_malloc(sizeof(*ds_flags));
if (!ds_flags) {
ast_datastore_free(datastore);
return -1;
}
*ds_flags = *flags;
datastore->data = ds_flags;
ast_channel_datastore_add(chan, datastore);
return 0;
}
int ast_bridge_features_ds_set(struct ast_channel *chan, struct ast_flags *flags)
{
return bridge_features_ds_set_full(chan, flags, 1);
}
int ast_bridge_features_ds_append(struct ast_channel *chan, struct ast_flags *flags)
{
return bridge_features_ds_set_full(chan, flags, 0);
}
struct ast_flags *ast_bridge_features_ds_get(struct ast_channel *chan)
{
struct ast_datastore *datastore;
datastore = ast_channel_datastore_find(chan, &dtmf_features_info, NULL);
if (!datastore) {
return NULL;
}
return datastore->data;
}
/*!
* \internal
* \brief Determine if we should dissolve the bridge from a hangup.
* \since 12.0.0
*
* \param bridge_channel Channel executing the feature
* \param hook_pvt Private data passed in when the hook was created
*
* \retval 0 Keep the callback hook.
* \retval -1 Remove the callback hook.
*/
static int basic_hangup_hook(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
{
int bridge_count = 0;
struct ast_bridge_channel *iter;
ast_bridge_channel_lock_bridge(bridge_channel);
AST_LIST_TRAVERSE(&bridge_channel->bridge->channels, iter, entry) {
if (iter != bridge_channel && iter->state == BRIDGE_CHANNEL_STATE_WAIT) {
++bridge_count;
}
}
if (2 <= bridge_count) {
/* Just allow this channel to leave the multi-party bridge. */
ast_bridge_channel_leave_bridge(bridge_channel,
BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
}
ast_bridge_unlock(bridge_channel->bridge);
return 0;
}
/*!
* \brief Details for specific basic bridge personalities
*/
struct personality_details {
/*! The v_table to use for this personality */
struct ast_bridge_methods *v_table;
/*! Flags to set on this type of bridge */
unsigned int bridge_flags;
/*! User data for this personality. If used, must be an ao2 object */
void *pvt;
/*! Callback to be called when changing to the personality */
void (*on_personality_change)(struct ast_bridge *bridge);
};
/*!
* \brief structure that organizes different personalities for basic bridges.
*/
struct bridge_basic_personality {
/*! The current bridge personality in use */
enum bridge_basic_personality_type current;
/*! Array of details for the types of bridge personalities supported */
struct personality_details details[BRIDGE_BASIC_PERSONALITY_END];
};
/*
* \internal
* \brief Get the extension for a given builtin feature.
*
* \param chan Get the feature extension for this channel.
* \param feature_name features.conf name of feature.
* \param buf Where to put the extension.
* \param len Length of the given extension buffer.
*
* \retval 0 success
* \retval non-zero failiure
*/
static int builtin_feature_get_exten(struct ast_channel *chan, const char *feature_name, char *buf, size_t len)
{
SCOPED_CHANNELLOCK(lock, chan);
return ast_get_builtin_feature(chan, feature_name, buf, len);
}
/*!
* \internal
* \brief Helper to add a builtin DTMF feature hook to the features struct.
* \since 12.0.0
*
* \param features Bridge features to setup.
* \param chan Get features from this channel.
* \param flags Feature flags on the channel.
* \param feature_flag Feature flag to test.
* \param feature_name features.conf name of feature.
* \param feature_bridge Bridge feature enum to get hook callback.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int builtin_features_helper(struct ast_bridge_features *features, struct ast_channel *chan,
struct ast_flags *flags, unsigned int feature_flag, const char *feature_name, enum ast_bridge_builtin_feature feature_bridge)
{
char dtmf[AST_FEATURE_MAX_LEN];
int res;
res = 0;
if (ast_test_flag(flags, feature_flag)
&& !builtin_feature_get_exten(chan, feature_name, dtmf, sizeof(dtmf))
&& !ast_strlen_zero(dtmf)) {
res = ast_bridge_features_enable(features, feature_bridge, dtmf, NULL, NULL,
AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
if (res) {
ast_log(LOG_ERROR, "Channel %s: Requested DTMF feature %s not available.\n",
ast_channel_name(chan), feature_name);
}
}
return res;
}
/*!
* \internal
* \brief Setup bridge builtin features.
* \since 12.0.0
*
* \param features Bridge features to setup.
* \param chan Get features from this channel.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int setup_bridge_features_builtin(struct ast_bridge_features *features, struct ast_channel *chan)
{
struct ast_flags *flags;
int res;
ast_channel_lock(chan);
flags = ast_bridge_features_ds_get(chan);
ast_channel_unlock(chan);
if (!flags) {
return 0;
}
res = 0;
res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "blindxfer", AST_BRIDGE_BUILTIN_BLINDTRANSFER);
res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "atxfer", AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER);
res |= builtin_features_helper(features, chan, flags, AST_FEATURE_DISCONNECT, "disconnect", AST_BRIDGE_BUILTIN_HANGUP);
res |= builtin_features_helper(features, chan, flags, AST_FEATURE_PARKCALL, "parkcall", AST_BRIDGE_BUILTIN_PARKCALL);
res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMON, "automon", AST_BRIDGE_BUILTIN_AUTOMON);
res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMIXMON, "automixmon", AST_BRIDGE_BUILTIN_AUTOMIXMON);
return res ? -1 : 0;
}
struct dynamic_dtmf_hook_run {
/*! Offset into app_name[] where the channel name that activated the hook starts. */
int activated_offset;
/*! Offset into app_name[] where the dynamic feature name starts. */
int feature_offset;
/*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
int moh_offset;
/*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
int app_args_offset;
/*! Application name to run. */
char app_name[0];
};
static void dynamic_dtmf_hook_callback(struct ast_bridge_channel *bridge_channel,
const void *payload, size_t payload_size)
{
struct ast_channel *chan = bridge_channel->chan;
const struct dynamic_dtmf_hook_run *run_data = payload;
pbx_builtin_setvar_helper(chan, "DYNAMIC_FEATURENAME",
&run_data->app_name[run_data->feature_offset]);
pbx_builtin_setvar_helper(chan, "DYNAMIC_WHO_ACTIVATED",
&run_data->app_name[run_data->activated_offset]);
ast_bridge_channel_run_app(bridge_channel, run_data->app_name,
run_data->app_args_offset ? &run_data->app_name[run_data->app_args_offset] : NULL,
run_data->moh_offset ? &run_data->app_name[run_data->moh_offset] : NULL);
}
struct dynamic_dtmf_hook_data {
/*! Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER) */
unsigned int flags;
/*! Offset into app_name[] where the dynamic feature name starts. */
int feature_offset;
/*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
int moh_offset;
/*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
int app_args_offset;
/*! Application name to run. */
char app_name[0];
};
/*!
* \internal
* \brief Activated dynamic DTMF feature hook.
* \since 12.0.0
*
* \param bridge_channel Channel executing the feature
* \param hook_pvt Private data passed in when the hook was created
*
* \retval 0 Keep the callback hook.
* \retval -1 Remove the callback hook.
*/
static int dynamic_dtmf_hook_trip(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
{
struct dynamic_dtmf_hook_data *pvt = hook_pvt;
struct dynamic_dtmf_hook_run *run_data;
const char *activated_name;
size_t len_name;
size_t len_args;
size_t len_moh;
size_t len_feature;
size_t len_activated;
size_t len_data;
/* Determine lengths of things. */
len_name = strlen(pvt->app_name) + 1;
len_args = pvt->app_args_offset ? strlen(&pvt->app_name[pvt->app_args_offset]) + 1 : 0;
len_moh = pvt->moh_offset ? strlen(&pvt->app_name[pvt->moh_offset]) + 1 : 0;
len_feature = strlen(&pvt->app_name[pvt->feature_offset]) + 1;
ast_channel_lock(bridge_channel->chan);
activated_name = ast_strdupa(ast_channel_name(bridge_channel->chan));
ast_channel_unlock(bridge_channel->chan);
len_activated = strlen(activated_name) + 1;
len_data = sizeof(*run_data) + len_name + len_args + len_moh + len_feature + len_activated;
/* Fill in dynamic feature run hook data. */
run_data = ast_alloca(len_data);
run_data->app_args_offset = len_args ? len_name : 0;
run_data->moh_offset = len_moh ? len_name + len_args : 0;
run_data->feature_offset = len_name + len_args + len_moh;
run_data->activated_offset = len_name + len_args + len_moh + len_feature;
strcpy(run_data->app_name, pvt->app_name);/* Safe */
if (len_args) {
strcpy(&run_data->app_name[run_data->app_args_offset],
&pvt->app_name[pvt->app_args_offset]);/* Safe */
}
if (len_moh) {
strcpy(&run_data->app_name[run_data->moh_offset],
&pvt->app_name[pvt->moh_offset]);/* Safe */
}
strcpy(&run_data->app_name[run_data->feature_offset],
&pvt->app_name[pvt->feature_offset]);/* Safe */
strcpy(&run_data->app_name[run_data->activated_offset], activated_name);/* Safe */
if (ast_test_flag(pvt, AST_FEATURE_FLAG_ONPEER)) {
ast_bridge_channel_write_callback(bridge_channel,
AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA,
dynamic_dtmf_hook_callback, run_data, len_data);
} else {
dynamic_dtmf_hook_callback(bridge_channel, run_data, len_data);
}
return 0;
}
/*!
* \internal
* \brief Add a dynamic DTMF feature hook to the bridge features.
* \since 12.0.0
*
* \param features Bridge features to setup.
* \param flags Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER).
* \param dtmf DTMF trigger sequence.
* \param feature_name Name of the dynamic feature.
* \param app_name Dialplan application name to run.
* \param app_args Dialplan application arguments. (Empty or NULL if no arguments)
* \param moh_class MOH class to play to peer. (Empty or NULL if no MOH played)
*
* \retval 0 on success.
* \retval -1 on error.
*/
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)
{
struct dynamic_dtmf_hook_data *hook_data;
size_t len_name = strlen(app_name) + 1;
size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
size_t len_moh = ast_strlen_zero(moh_class) ? 0 : strlen(moh_class) + 1;
size_t len_feature = strlen(feature_name) + 1;
size_t len_data = sizeof(*hook_data) + len_name + len_args + len_moh + len_feature;
int res;
/* Fill in application run hook data. */
hook_data = ast_malloc(len_data);
if (!hook_data) {
return -1;
}
hook_data->flags = flags;
hook_data->app_args_offset = len_args ? len_name : 0;
hook_data->moh_offset = len_moh ? len_name + len_args : 0;
hook_data->feature_offset = len_name + len_args + len_moh;
strcpy(hook_data->app_name, app_name);/* Safe */
if (len_args) {
strcpy(&hook_data->app_name[hook_data->app_args_offset], app_args);/* Safe */
}
if (len_moh) {
strcpy(&hook_data->app_name[hook_data->moh_offset], moh_class);/* Safe */
}
strcpy(&hook_data->app_name[hook_data->feature_offset], feature_name);/* Safe */
res = ast_bridge_dtmf_hook(features, dtmf, dynamic_dtmf_hook_trip, hook_data,
ast_free_ptr,
AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
if (res) {
ast_free(hook_data);
}
return res;
}
static int setup_dynamic_feature(void *obj, void *arg, void *data, int flags)
{
struct ast_applicationmap_item *item = obj;
struct ast_bridge_features *features = arg;
int *res = data;
*res |= dynamic_dtmf_hook_add(features,
item->activate_on_self ? AST_FEATURE_FLAG_ONSELF : AST_FEATURE_FLAG_ONPEER,
item->dtmf, item->name, item->app, item->app_data, item->moh_class);
return 0;
}
/*!
* \internal
* \brief Setup bridge dynamic features.
* \since 12.0.0
*
* \param features Bridge features to setup.
* \param chan Get features from this channel.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int setup_bridge_features_dynamic(struct ast_bridge_features *features, struct ast_channel *chan)
{
struct ao2_container *applicationmap;
int res = 0;
ast_channel_lock(chan);
applicationmap = ast_get_chan_applicationmap(chan);
ast_channel_unlock(chan);
if (applicationmap) {
ao2_callback_data(applicationmap, 0, setup_dynamic_feature, features, &res);
ao2_ref(applicationmap, -1);
}
return res;
}
/*!
* \internal
* \brief Setup DTMF feature hooks using the channel features datastore property.
* \since 12.0.0
*
* \param bridge_channel What to setup DTMF features on.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int bridge_basic_setup_features(struct ast_bridge_channel *bridge_channel)
{
int res = 0;
res |= setup_bridge_features_builtin(bridge_channel->features, bridge_channel->chan);
res |= setup_bridge_features_dynamic(bridge_channel->features, bridge_channel->chan);
return res;
}
static int add_normal_hooks(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
return ast_bridge_hangup_hook(bridge_channel->features, basic_hangup_hook,
NULL, NULL, AST_BRIDGE_HOOK_REMOVE_ON_PULL)
|| bridge_basic_setup_features(bridge_channel);
}
/*!
* \internal
* \brief ast_bridge basic push method.
* \since 12.0.0
*
* \param self Bridge to operate upon.
* \param bridge_channel Bridge channel to push.
* \param swap Bridge channel to swap places with if not NULL.
*
* \note On entry, self is already locked.
*
* \retval 0 on success
* \retval -1 on failure
*/
static int bridge_personality_normal_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
{
if (add_normal_hooks(self, bridge_channel)) {
return -1;
}
return 0;
}
static int bridge_basic_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
{
struct bridge_basic_personality *personality = self->personality;
ast_assert(personality != NULL);
if (personality->details[personality->current].v_table->push
&& personality->details[personality->current].v_table->push(self, bridge_channel, swap)) {
return -1;
}
ast_bridge_channel_update_linkedids(bridge_channel, swap);
ast_bridge_channel_update_accountcodes(bridge_channel, swap);
return ast_bridge_base_v_table.push(self, bridge_channel, swap);
}
static void bridge_basic_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
{
struct bridge_basic_personality *personality = self->personality;
ast_assert(personality != NULL);
if (personality->details[personality->current].v_table->pull) {
personality->details[personality->current].v_table->pull(self, bridge_channel);
}
ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
ast_bridge_base_v_table.pull(self, bridge_channel);
}
static void bridge_basic_destroy(struct ast_bridge *self)
{
struct bridge_basic_personality *personality = self->personality;
ao2_cleanup(personality);
ast_bridge_base_v_table.destroy(self);
}
/*!
* \brief Remove appropriate hooks when basic bridge personality changes
*
* Hooks that have the AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE flag
* set will be removed from all bridge channels in the bridge.
*
* \param bridge Basic bridge undergoing personality change
*/
static void remove_hooks_on_personality_change(struct ast_bridge *bridge)
{
struct ast_bridge_channel *iter;
AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
SCOPED_LOCK(lock, iter, ast_bridge_channel_lock, ast_bridge_channel_unlock);
ast_bridge_features_remove(iter->features, AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
}
}
/*!
* \brief Attended transfer superstates.
*
* An attended transfer's progress is facilitated by a state machine.
* The individual states of the state machine fall into the realm of
* one of two superstates.
*/
enum attended_transfer_superstate {
/*!
* \brief Transfer superstate
*
* The attended transfer state machine begins in this superstate. The
* goal of this state is for a transferer channel to facilitate a
* transfer from a transferee to a transfer target.
*
* There are two bridges used in this superstate. The transferee bridge is
* the bridge that the transferer and transferee channels originally
* communicate in, and the target bridge is the bridge where the transfer
* target is being dialed.
*
* The transferer channel is capable of moving between the bridges using
* the DTMF swap sequence.
*/
SUPERSTATE_TRANSFER,
/*!
* \brief Recall superstate
*
* The attended transfer state machine moves to this superstate if
* atxferdropcall is set to "no" and the transferer channel hangs up
* during a transfer. The goal in this superstate is to call back either
* the transfer target or transferer and rebridge with the transferee
* channel(s).
*
* In this superstate, there is only a single bridge used, the original
* transferee bridge. Rather than distinguishing between a transferer
* and transfer target, all outbound calls are toward a "recall_target"
* channel.
*/
SUPERSTATE_RECALL,
};
/*!
* The states in the attended transfer state machine.
*/
enum attended_transfer_state {
/*!
* \brief Calling Target state
*
* This state describes the initial state of a transfer. The transferer
* waits in the transfer target's bridge for the transfer target to answer.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transfer target is RINGING
* 2) Transferer is in transferee bridge
* 3) Transferee is on hold
*
* Transitions to TRANSFER_CALLING_TARGET:
* 1) This is the initial state for an attended transfer.
* 2) TRANSFER_HESITANT: Transferer presses DTMF swap sequence
*
* State operation:
* The transferer is moved from the transferee bridge into the transfer
* target bridge.
*
* Transitions from TRANSFER_CALLING_TARGET:
* 1) TRANSFER_FAIL: Transferee hangs up.
* 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
* and configured atxferdropcall setting is yes.
* 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
* sequence and configured atxferdroppcall setting is no.
* 4) TRANSFER_CONSULTING: Transfer target answers the call.
* 5) TRANSFER_REBRIDGE: Transfer target hangs up, call to transfer target
* times out, or transferer presses DTMF abort sequence.
* 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
* 7) TRANSFER_HESITANT: Transferer presses DTMF swap sequence.
*/
TRANSFER_CALLING_TARGET,
/*!
* \brief Hesitant state
*
* This state only arises if when waiting for the transfer target to
* answer, the transferer presses the DTMF swap sequence. This will
* cause the transferer to be rebridged with the transferee temporarily.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transfer target is in ringing state
* 2) Transferer is in transfer target bridge
* 3) Transferee is on hold
*
* Transitions to TRANSFER_HESITANT:
* 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
*
* State operation:
* The transferer is moved from the transfer target bridge into the
* transferee bridge, and the transferee is taken off hold.
*
* Transitions from TRANSFER_HESITANT:
* 1) TRANSFER_FAIL: Transferee hangs up
* 2) TRANSFER_BLOND: Transferer hangs up or presses DTMF swap sequence
* and configured atxferdropcall setting is yes.
* 3) TRANSFER_BLOND_NONFINAL: Transferer hangs up or presses DTMF swap
* sequence and configured atxferdroppcall setting is no.
* 4) TRANSFER_DOUBLECHECKING: Transfer target answers the call
* 5) TRANSFER_RESUME: Transfer target hangs up, call to transfer target
* times out, or transferer presses DTMF abort sequence.
* 6) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
* 7) TRANSFER_CALLING_TARGET: Transferer presses DTMF swap sequence.
*/
TRANSFER_HESITANT,
/*!
* \brief Rebridge state
*
* This is a terminal state that indicates that the transferer needs
* to move back to the transferee's bridge. This is a failed attended
* transfer result.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transferer is in transfer target bridge
* 2) Transferee is on hold
*
* Transitions to TRANSFER_REBRIDGE:
* 1) TRANSFER_CALLING_TARGET: Transfer target hangs up, call to transfer target
* times out, or transferer presses DTMF abort sequence.
* 2) TRANSFER_STATE_CONSULTING: Transfer target hangs up, or transferer presses
* DTMF abort sequence.
*
* State operation:
* The transferer channel is moved from the transfer target bridge to the
* transferee bridge. The transferee is taken off hold. A stasis transfer
* message is published indicating a failed attended transfer.
*
* Transitions from TRANSFER_REBRIDGE:
* None
*/
TRANSFER_REBRIDGE,
/*!
* \brief Resume state
*
* This is a terminal state that indicates that the party bridged with the
* transferee is the final party to be bridged with that transferee. This state
* may come about due to a successful recall or due to a failed transfer.
*
* Superstate: Transfer or Recall
*
* Preconditions:
* In Transfer Superstate:
* 1) Transferer is in transferee bridge
* 2) Transferee is not on hold
* In Recall Superstate:
* 1) The recall target is in the transferee bridge
* 2) Transferee is not on hold
*
* Transitions to TRANSFER_RESUME:
* TRANSFER_HESITANT: Transfer target hangs up, call to transfer target times out,
* or transferer presses DTMF abort sequence.
* TRANSFER_DOUBLECHECKING: Transfer target hangs up or transferer presses DTMF
* abort sequence.
* TRANSFER_BLOND_NONFINAL: Recall target answers
* TRANSFER_RECALLING: Recall target answers
* TRANSFER_RETRANSFER: Recall target answers
*
* State operations:
* None
*
* Transitions from TRANSFER_RESUME:
* None
*/
TRANSFER_RESUME,
/*!
* \brief Threeway state
*
* This state results when the transferer wishes to have all parties involved
* in a transfer to be in the same bridge together.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transfer target state is either RINGING or UP
* 2) Transferer is in either bridge
* 3) Transferee is not on hold
*
* Transitions to TRANSFER_THREEWAY:
* 1) TRANSFER_CALLING_TARGET: Transferer presses DTMF threeway sequence.
* 2) TRANSFER_HESITANT: Transferer presses DTMF threeway sequence.
* 3) TRANSFER_CONSULTING: Transferer presses DTMF threeway sequence.
* 4) TRANSFER_DOUBLECHECKING: Transferer presses DTMF threeway sequence.
*
* State operation:
* The transfer target bridge is merged into the transferee bridge.
*
* Transitions from TRANSFER_THREEWAY:
* None.
*/
TRANSFER_THREEWAY,
/*!
* \brief Consulting state
*
* This state describes the case where the transferer and transfer target
* are able to converse in the transfer target's bridge prior to completing
* the transfer.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transfer target is UP
* 2) Transferer is in target bridge
* 3) Transferee is on hold
*
* Transitions to TRANSFER_CONSULTING:
* 1) TRANSFER_CALLING_TARGET: Transfer target answers.
* 2) TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
*
* State operations:
* None.
*
* Transitions from TRANSFER_CONSULTING:
* TRANSFER_COMPLETE: Transferer hangs up or transferer presses DTMF complete sequence.
* TRANSFER_REBRIDGE: Transfer target hangs up or transferer presses DTMF abort sequence.
* TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
* TRANSFER_DOUBLECHECKING: Transferer presses DTMF swap sequence.
*/
TRANSFER_CONSULTING,
/*!
* \brief Double-checking state
*
* This state describes the case where the transferer and transferee are
* able to converse in the transferee's bridge prior to completing the transfer. The
* difference between this and TRANSFER_HESITANT is that the transfer target is
* UP in this case.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transfer target is UP and on hold
* 2) Transferer is in transferee bridge
* 3) Transferee is off hold
*
* Transitions to TRANSFER_DOUBLECHECKING:
* 1) TRANSFER_HESITANT: Transfer target answers.
* 2) TRANSFER_CONSULTING: Transferer presses DTMF swap sequence.
*
* State operations:
* None.
*
* Transitions from TRANSFER_DOUBLECHECKING:
* 1) TRANSFER_FAIL: Transferee hangs up.
* 2) TRANSFER_COMPLETE: Transferer hangs up or presses DTMF complete sequence.
* 3) TRANSFER_RESUME: Transfer target hangs up or transferer presses DTMF abort sequence.
* 4) TRANSFER_THREEWAY: Transferer presses DTMF threeway sequence.
* 5) TRANSFER_CONSULTING: Transferer presses the DTMF swap sequence.
*/
TRANSFER_DOUBLECHECKING,
/*!
* \brief Complete state
*
* This is a terminal state where a transferer has successfully completed an attended
* transfer. This state's goal is to get the transfer target and transferee into
* the same bridge and the transferer off the call.
*
* Superstate: Transfer
*
* Preconditions:
* 1) Transfer target is UP and off hold.
* 2) Transferer is in either bridge.
* 3) Transferee is off hold.