2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Routines implementing call features as call pickup, parking and transfer
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/_private.h"
34 #include <sys/signal.h>
35 #include <netinet/in.h>
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/causes.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/app.h"
45 #include "asterisk/say.h"
46 #include "asterisk/features.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/config.h"
49 #include "asterisk/cli.h"
50 #include "asterisk/manager.h"
51 #include "asterisk/utils.h"
52 #include "asterisk/adsi.h"
53 #include "asterisk/devicestate.h"
54 #include "asterisk/monitor.h"
55 #include "asterisk/audiohook.h"
56 #include "asterisk/global_datastores.h"
57 #include "asterisk/astobj2.h"
58 #include "asterisk/cel.h"
61 <application name="Bridge" language="en_US">
66 <parameter name="channel" required="true">
67 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
69 <parameter name="options">
72 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
75 <para>Allow the called party to hang up by sending the
76 <replaceable>*</replaceable> DTMF digit.</para>
79 <para>Allow the calling party to hang up by pressing the
80 <replaceable>*</replaceable> DTMF digit.</para>
83 <para>Allow the called party to enable parking of the call by sending
84 the DTMF sequence defined for call parking in features.conf.</para>
87 <para>Allow the calling party to enable parking of the call by sending
88 the DTMF sequence defined for call parking in features.conf.</para>
90 <option name="L(x[:y][:z])">
91 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
92 when <replaceable>y</replaceable> ms are left. Repeat the warning every
93 <replaceable>z</replaceable> ms. The following special variables can be
94 used with this option:</para>
96 <variable name="LIMIT_PLAYAUDIO_CALLER">
97 <para>Play sounds to the caller. yes|no (default yes)</para>
99 <variable name="LIMIT_PLAYAUDIO_CALLEE">
100 <para>Play sounds to the callee. yes|no</para>
102 <variable name="LIMIT_TIMEOUT_FILE">
103 <para>File to play when time is up.</para>
105 <variable name="LIMIT_CONNECT_FILE">
106 <para>File to play when call begins.</para>
108 <variable name="LIMIT_WARNING_FILE">
109 <para>File to play as warning if <replaceable>y</replaceable> is
110 defined. The default is to say the time remaining.</para>
115 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
118 <para>Allow the called party to transfer the calling party by sending the
119 DTMF sequence defined in features.conf.</para>
122 <para>Allow the calling party to transfer the called party by sending the
123 DTMF sequence defined in features.conf.</para>
126 <para>Allow the called party to enable recording of the call by sending
127 the DTMF sequence defined for one-touch recording in features.conf.</para>
130 <para>Allow the calling party to enable recording of the call by sending
131 the DTMF sequence defined for one-touch recording in features.conf.</para>
134 <para>Cause the called party to be hung up after the bridge, instead of being
135 restarted in the dialplan.</para>
141 <para>Allows the ability to bridge two channels via the dialplan.</para>
142 <para>This application sets the following channel variable upon completion:</para>
144 <variable name="BRIDGERESULT">
145 <para>The result of the bridge attempt as a text string.</para>
146 <value name="SUCCESS" />
147 <value name="FAILURE" />
148 <value name="LOOP" />
149 <value name="NONEXISTENT" />
150 <value name="INCOMPATIBLE" />
155 <application name="ParkedCall" language="en_US">
157 Answer a parked call.
160 <parameter name="exten" required="true" />
163 <para>Used to connect to a parked call. This application is always
164 registered internally and does not need to be explicitly added
165 into the dialplan, although you should include the <literal>parkedcalls</literal>
166 context. If no extension is provided, then the first available
167 parked call will be acquired.</para>
170 <ref type="application">Park</ref>
171 <ref type="application">ParkAndAnnounce</ref>
174 <application name="Park" language="en_US">
179 <parameter name="timeout">
180 <para>A custom parking timeout for this parked call.</para>
182 <parameter name="return_context">
183 <para>The context to return the call to after it times out.</para>
185 <parameter name="return_exten">
186 <para>The extension to return the call to after it times out.</para>
188 <parameter name="return_priority">
189 <para>The priority to return the call to after it times out.</para>
191 <parameter name="options">
192 <para>A list of options for this parked call.</para>
195 <para>Send ringing instead of MOH to the parked call.</para>
198 <para>Randomize the selection of a parking space.</para>
201 <para>Silence announcement of the parking space number.</para>
207 <para>Used to park yourself (typically in combination with a supervised
208 transfer to know the parking space). This application is always
209 registered internally and does not need to be explicitly added
210 into the dialplan, although you should include the <literal>parkedcalls</literal>
211 context (or the context specified in <filename>features.conf</filename>).</para>
212 <para>If you set the <variable>PARKINGEXTEN</variable> variable to an extension in your
213 parking context, Park() will park the call on that extension, unless
214 it already exists. In that case, execution will continue at next priority.</para>
217 <ref type="application">ParkAndAnnounce</ref>
218 <ref type="application">ParkedCall</ref>
221 <manager name="ParkedCalls" language="en_US">
226 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
229 <para>List parked calls.</para>
232 <manager name="Park" language="en_US">
237 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
238 <parameter name="Channel" required="true">
239 <para>Channel name to park.</para>
241 <parameter name="Channel2" required="true">
242 <para>Channel to announce park info to (and return to if timeout).</para>
244 <parameter name="Timeout">
245 <para>Number of milliseconds to wait before callback.</para>
249 <para>Park a channel.</para>
252 <manager name="Bridge" language="en_US">
254 Bridge two channels already in the PBX.
257 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
258 <parameter name="Channel1" required="true">
259 <para>Channel to Bridge to Channel2.</para>
261 <parameter name="Channel2" required="true">
262 <para>Channel to Bridge to Channel1.</para>
264 <parameter name="Tone">
265 <para>Play courtesy tone to Channel 2.</para>
273 <para>Bridge together two channels already in the PBX.</para>
278 #define DEFAULT_PARK_TIME 45000
279 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
280 #define DEFAULT_FEATURE_DIGIT_TIMEOUT 1000
281 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000
282 #define DEFAULT_PARKINGLOT "default" /*!< Default parking lot */
283 #define DEFAULT_ATXFER_DROP_CALL 0
284 #define DEFAULT_ATXFER_LOOP_DELAY 10000
285 #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
287 #define AST_MAX_WATCHERS 256
288 #define MAX_DIAL_FEATURE_OPTIONS 30
290 struct feature_group_exten {
291 AST_LIST_ENTRY(feature_group_exten) entry;
292 AST_DECLARE_STRING_FIELDS(
293 AST_STRING_FIELD(exten);
295 struct ast_call_feature *feature;
298 struct feature_group {
299 AST_LIST_ENTRY(feature_group) entry;
300 AST_DECLARE_STRING_FIELDS(
301 AST_STRING_FIELD(gname);
303 AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
306 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
308 static char *parkedcall = "ParkedCall";
310 static char pickup_ext[AST_MAX_EXTENSION]; /*!< Call pickup extension */
312 /*! \brief Description of one parked call, added to a list while active, then removed.
313 The list belongs to a parkinglot
316 struct ast_channel *chan; /*!< Parking channel */
317 struct timeval start; /*!< Time the parking started */
318 int parkingnum; /*!< Parking lot */
319 char parkingexten[AST_MAX_EXTENSION]; /*!< If set beforehand, parking extension used for this call */
320 char context[AST_MAX_CONTEXT]; /*!< Where to go if our parking time expires */
321 char exten[AST_MAX_EXTENSION];
323 int parkingtime; /*!< Maximum length in parking lot before return */
324 unsigned int notquiteyet:1;
325 unsigned int options_specified:1;
327 unsigned char moh_trys;
328 struct ast_parkinglot *parkinglot;
329 AST_LIST_ENTRY(parkeduser) list;
332 /*! \brief Structure for parking lots which are put in a container. */
333 struct ast_parkinglot {
334 char name[AST_MAX_CONTEXT];
335 char parking_con[AST_MAX_EXTENSION]; /*!< Context for which parking is made accessible */
336 char parking_con_dial[AST_MAX_EXTENSION]; /*!< Context for dialback for parking (KLUDGE) */
337 int parking_start; /*!< First available extension for parking */
338 int parking_stop; /*!< Last available extension for parking */
341 int parkingtime; /*!< Default parking time */
342 char mohclass[MAX_MUSICCLASS]; /*!< Music class used for parking */
343 int parkaddhints; /*!< Add parking hints automatically */
344 int parkedcalltransfers; /*!< Enable DTMF based transfers on bridge when picking up parked calls */
345 int parkedcallreparking; /*!< Enable DTMF based parking on bridge when picking up parked calls */
346 int parkedcallhangup; /*!< Enable DTMF based hangup on a bridge when pickup up parked calls */
347 int parkedcallrecording; /*!< Enable DTMF based recording on a bridge when picking up parked calls */
348 AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings; /*!< List of active parkings in this parkinglot */
351 /*! \brief The list of parking lots configured. Always at least one - the default parking lot */
352 static struct ao2_container *parkinglots;
354 struct ast_parkinglot *default_parkinglot;
355 char parking_ext[AST_MAX_EXTENSION]; /*!< Extension you type to park the call */
357 static char courtesytone[256]; /*!< Courtesy tone */
358 static int parkedplay = 0; /*!< Who to play the courtesy tone to */
359 static char xfersound[256]; /*!< Call transfer sound */
360 static char xferfailsound[256]; /*!< Call transfer failure sound */
361 static char pickupsound[256]; /*!< Pickup sound */
362 static char pickupfailsound[256]; /*!< Pickup failure sound */
366 static int transferdigittimeout;
367 static int featuredigittimeout;
368 static int comebacktoorigin = 1;
370 static int atxfernoanswertimeout;
371 static unsigned int atxferdropcall;
372 static unsigned int atxferloopdelay;
373 static unsigned int atxfercallbackretries;
375 static char *registrar = "features"; /*!< Registrar for operations */
377 /* module and CLI command definitions */
378 static char *parkcall = PARK_APP_NAME;
380 static struct ast_app *monitor_app = NULL;
381 static int monitor_ok = 1;
383 static struct ast_app *mixmonitor_app = NULL;
384 static int mixmonitor_ok = 1;
386 static struct ast_app *stopmixmonitor_app = NULL;
387 static int stopmixmonitor_ok = 1;
389 static pthread_t parking_thread;
390 struct ast_dial_features {
391 struct ast_flags features_caller;
392 struct ast_flags features_callee;
396 static void *dial_features_duplicate(void *data)
398 struct ast_dial_features *df = data, *df_copy;
400 if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
404 memcpy(df_copy, df, sizeof(*df));
409 static void dial_features_destroy(void *data)
411 struct ast_dial_features *df = data;
417 static const struct ast_datastore_info dial_features_info = {
418 .type = "dial-features",
419 .destroy = dial_features_destroy,
420 .duplicate = dial_features_duplicate,
423 /* Forward declarations */
424 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
425 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
426 static void parkinglot_destroy(void *obj);
427 int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, fd_set *nrfds, fd_set *nefds, int *fs, int *max);
428 struct ast_parkinglot *find_parkinglot(const char *name);
431 const char *ast_parking_ext(void)
436 const char *ast_pickup_ext(void)
441 struct ast_bridge_thread_obj
443 struct ast_bridge_config bconfig;
444 struct ast_channel *chan;
445 struct ast_channel *peer;
446 unsigned int return_to_pbx:1;
449 static int parkinglot_hash_cb(const void *obj, const int flags)
451 const struct ast_parkinglot *parkinglot = obj;
453 return ast_str_case_hash(parkinglot->name);
456 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
458 struct ast_parkinglot *parkinglot = obj, *parkinglot2 = arg;
460 return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
464 * \brief store context, extension and priority
465 * \param chan, context, ext, pri
467 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
469 ast_copy_string(chan->context, context, sizeof(chan->context));
470 ast_copy_string(chan->exten, ext, sizeof(chan->exten));
471 chan->priority = pri;
475 * \brief Check goto on transfer
478 * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
479 * When found make sure the types are compatible. Check if channel is valid
480 * if so start the new channel else hangup the call.
482 static void check_goto_on_transfer(struct ast_channel *chan)
484 struct ast_channel *xferchan;
485 const char *val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
486 char *x, *goto_on_transfer;
489 if (ast_strlen_zero(val))
492 goto_on_transfer = ast_strdupa(val);
494 if (!(xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", chan->linkedid, 0, "%s", chan->name)))
497 for (x = goto_on_transfer; x && *x; x++) {
501 /* Make formats okay */
502 xferchan->readformat = chan->readformat;
503 xferchan->writeformat = chan->writeformat;
504 ast_channel_masquerade(xferchan, chan);
505 ast_parseable_goto(xferchan, goto_on_transfer);
506 xferchan->_state = AST_STATE_UP;
507 ast_clear_flag(xferchan, AST_FLAGS_ALL);
508 xferchan->_softhangup = 0;
509 if ((f = ast_read(xferchan))) {
512 ast_pbx_start(xferchan);
514 ast_hangup(xferchan);
518 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate, const char *language);
521 * \brief bridge the call
522 * \param data thread bridge.
524 * Set Last Data for respective channels, reset cdr for channels
525 * bridge call, check if we're going back to dialplan
526 * if not hangup both legs of the call
528 static void *bridge_call_thread(void *data)
530 struct ast_bridge_thread_obj *tobj = data;
533 tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
534 tobj->chan->data = tobj->peer->name;
535 tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
536 tobj->peer->data = tobj->chan->name;
538 ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
540 if (tobj->return_to_pbx) {
541 if (!ast_check_hangup(tobj->peer)) {
542 ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
543 res = ast_pbx_start(tobj->peer);
544 if (res != AST_PBX_SUCCESS)
545 ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
547 ast_hangup(tobj->peer);
548 if (!ast_check_hangup(tobj->chan)) {
549 ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
550 res = ast_pbx_start(tobj->chan);
551 if (res != AST_PBX_SUCCESS)
552 ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
554 ast_hangup(tobj->chan);
556 ast_hangup(tobj->chan);
557 ast_hangup(tobj->peer);
566 * \brief create thread for the parked call
569 * Create thread and attributes, call bridge_call_thread
571 static void bridge_call_thread_launch(void *data)
575 struct sched_param sched;
577 pthread_attr_init(&attr);
578 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
579 ast_pthread_create(&thread, &attr, bridge_call_thread, data);
580 pthread_attr_destroy(&attr);
581 memset(&sched, 0, sizeof(sched));
582 pthread_setschedparam(thread, SCHED_RR, &sched);
586 * \brief Announce call parking by ADSI
588 * \param parkingexten .
589 * Create message to show for ADSI, display message.
590 * \retval 0 on success.
591 * \retval -1 on failure.
593 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
596 int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
598 char *message[5] = {NULL, NULL, NULL, NULL, NULL};
600 snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
602 res = ast_adsi_load_session(chan, NULL, 0, 1);
605 return ast_adsi_print(chan, message, justify, 1);
608 /*! \brief Find parking lot name from channel */
609 static const char *findparkinglotname(struct ast_channel *chan)
611 const char *temp, *parkinglot = NULL;
613 /* Check if the channel has a parking lot */
614 if (!ast_strlen_zero(chan->parkinglot))
615 parkinglot = chan->parkinglot;
617 /* Channel variables override everything */
619 if ((temp = pbx_builtin_getvar_helper(chan, "PARKINGLOT")))
625 /*! \brief Notify metermaids that we've changed an extension */
626 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
628 ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
629 exten, context, ast_devstate2str(state));
631 ast_devstate_changed(state, "park:%s@%s", exten, context);
634 /*! \brief metermaids callback from devicestate.c */
635 static enum ast_device_state metermaidstate(const char *data)
640 context = ast_strdupa(data);
642 exten = strsep(&context, "@");
644 return AST_DEVICE_INVALID;
646 ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
648 if (!ast_exists_extension(NULL, context, exten, 1, NULL))
649 return AST_DEVICE_NOT_INUSE;
651 return AST_DEVICE_INUSE;
654 /*! Options to pass to park_call_full */
655 enum ast_park_call_options {
656 /*! Provide ringing to the parked caller instead of music on hold */
657 AST_PARK_OPT_RINGING = (1 << 0),
658 /*! Randomly choose a parking spot for the caller instead of choosing
659 * the first one that is available. */
660 AST_PARK_OPT_RANDOMIZE = (1 << 1),
661 /*! Do not announce the parking number */
662 AST_PARK_OPT_SILENCE = (1 << 2),
665 struct ast_park_call_args {
666 /*! How long to wait in the parking lot before the call gets sent back
667 * to the specified return extension (or a best guess at where it came
668 * from if not explicitly specified). */
670 /*! An output parameter to store the parking space where the parked caller
673 const char *orig_chan_name;
674 const char *return_con;
675 const char *return_ext;
678 /*! Parked user that has already obtained a parking space */
679 struct parkeduser *pu;
682 static struct parkeduser *park_space_reserve(struct ast_channel *chan,
683 struct ast_channel *peer, struct ast_park_call_args *args)
685 struct parkeduser *pu;
686 int i, parking_space = -1, parking_range;
687 const char *parkinglotname = NULL;
688 const char *parkingexten;
689 struct ast_parkinglot *parkinglot = NULL;
692 parkinglotname = findparkinglotname(peer);
694 if (parkinglotname) {
696 ast_log(LOG_DEBUG, "Found chanvar Parkinglot: %s\n", parkinglotname);
697 parkinglot = find_parkinglot(parkinglotname);
700 parkinglot = parkinglot_addref(default_parkinglot);
704 ast_log(LOG_DEBUG, "Parkinglot: %s\n", parkinglot->name);
706 /* Allocate memory for parking data */
707 if (!(pu = ast_calloc(1, sizeof(*pu)))) {
708 parkinglot_unref(parkinglot);
712 /* Lock parking list */
713 AST_LIST_LOCK(&parkinglot->parkings);
714 /* Check for channel variable PARKINGEXTEN */
715 ast_channel_lock(chan);
716 parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGEXTEN"), ""));
717 ast_channel_unlock(chan);
718 if (!ast_strlen_zero(parkingexten)) {
719 /*!\note The API forces us to specify a numeric parking slot, even
720 * though the architecture would tend to support non-numeric extensions
721 * (as are possible with SIP, for example). Hence, we enforce that
722 * limitation here. If extout was not numeric, we could permit
723 * arbitrary non-numeric extensions.
725 if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space < 0) {
726 AST_LIST_UNLOCK(&parkinglot->parkings);
727 parkinglot_unref(parkinglot);
729 ast_log(LOG_WARNING, "PARKINGEXTEN does not indicate a valid parking slot: '%s'.\n", parkingexten);
732 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
734 if (ast_exists_extension(NULL, parkinglot->parking_con, pu->parkingexten, 1, NULL)) {
735 ast_log(LOG_WARNING, "Requested parking extension already exists: %s@%s\n", parkingexten, parkinglot->parking_con);
736 AST_LIST_UNLOCK(&parkinglot->parkings);
737 parkinglot_unref(parkinglot);
743 struct parkeduser *cur = NULL;
745 /* Select parking space within range */
746 parking_range = parkinglot->parking_stop - parkinglot->parking_start + 1;
748 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
749 start = ast_random() % (parkinglot->parking_stop - parkinglot->parking_start + 1);
751 start = parkinglot->parking_start;
754 for (i = start; 1; i++) {
755 if (i == parkinglot->parking_stop + 1) {
756 i = parkinglot->parking_start - 1;
760 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
761 if (cur->parkingnum == i) {
771 if (i == start - 1 && cur) {
772 ast_log(LOG_WARNING, "No more parking spaces\n");
774 AST_LIST_UNLOCK(&parkinglot->parkings);
775 parkinglot_unref(parkinglot);
778 /* Set pointer for next parking */
779 if (parkinglot->parkfindnext)
780 parkinglot->parking_offset = parking_space - parkinglot->parking_start + 1;
781 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
785 pu->parkingnum = parking_space;
786 pu->parkinglot = parkinglot_addref(parkinglot);
787 AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
788 parkinglot_unref(parkinglot);
794 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
796 struct ast_context *con;
798 struct parkeduser *pu = args->pu;
799 const char *event_from;
802 pu = park_space_reserve(chan, peer, args);
804 return 1; /* Continue execution if possible */
806 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", pu->parkingnum);
808 chan->appl = "Parked Call";
813 /* Put the parked channel on hold if we have two different channels */
815 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
816 ast_indicate(pu->chan, AST_CONTROL_RINGING);
818 ast_indicate_data(pu->chan, AST_CONTROL_HOLD,
819 S_OR(pu->parkinglot->mohclass, NULL),
820 !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
824 pu->start = ast_tvnow();
825 pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->parkingtime;
826 parkingnum_copy = pu->parkingnum;
828 *(args->extout) = pu->parkingnum;
831 /* This is so ugly that it hurts, but implementing get_base_channel() on local channels
832 could have ugly side effects. We could have transferer<->local,1<->local,2<->parking
833 and we need the callback name to be that of transferer. Since local,1/2 have the same
834 name we can be tricky and just grab the bridged channel from the other side of the local
836 if (!strcasecmp(peer->tech->type, "Local")) {
837 struct ast_channel *tmpchan, *base_peer;
838 char other_side[AST_CHANNEL_NAME];
840 ast_copy_string(other_side, S_OR(args->orig_chan_name, peer->name), sizeof(other_side));
841 if ((c = strrchr(other_side, ';'))) {
844 if ((tmpchan = ast_channel_get_by_name(other_side))) {
845 ast_channel_lock(tmpchan);
846 if ((base_peer = ast_bridged_channel(tmpchan))) {
847 ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
849 ast_channel_unlock(tmpchan);
850 tmpchan = ast_channel_unref(tmpchan);
853 ast_copy_string(pu->peername, S_OR(args->orig_chan_name, peer->name), sizeof(pu->peername));
857 /* Remember what had been dialed, so that if the parking
858 expires, we try to come back to the same place */
860 pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
862 /* If extension has options specified, they override all other possibilities
863 such as the returntoorigin flag and transferred context. Information on
864 extension options is lost here, so we set a flag */
866 ast_copy_string(pu->context,
867 S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)),
868 sizeof(pu->context));
869 ast_copy_string(pu->exten,
870 S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)),
872 pu->priority = args->return_pri ? args->return_pri :
873 (chan->macropriority ? chan->macropriority : chan->priority);
875 /* If parking a channel directly, don't quiet yet get parking running on it.
876 * All parking lot entries are put into the parking lot with notquiteyet on. */
880 /* Wake up the (presumably select()ing) thread */
881 pthread_kill(parking_thread, SIGURG);
882 ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n", pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->context, pu->exten, pu->priority, (pu->parkingtime/1000));
884 ast_cel_report_event(pu->chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
887 event_from = peer->name;
889 event_from = pbx_builtin_getvar_helper(chan, "BLINDTRANSFER");
892 ast_manager_event(pu->chan, EVENT_FLAG_CALL, "ParkedCall",
898 "CallerIDNum: %s\r\n"
899 "CallerIDName: %s\r\n"
901 pu->parkingexten, pu->chan->name, pu->parkinglot->name, event_from ? event_from : "",
902 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
903 S_OR(pu->chan->cid.cid_num, "<unknown>"),
904 S_OR(pu->chan->cid.cid_name, "<unknown>"),
908 if (peer && adsipark && ast_adsi_available(peer)) {
909 adsi_announce_park(peer, pu->parkingexten); /* Only supports parking numbers */
910 ast_adsi_unload_session(peer);
913 con = ast_context_find_or_create(NULL, NULL, pu->parkinglot->parking_con, registrar);
914 if (!con) /* Still no context? Bad */
915 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", pu->parkinglot->parking_con);
917 if (!ast_add_extension2(con, 1, pu->parkingexten, 1, NULL, NULL, parkedcall, ast_strdup(pu->parkingexten), ast_free_ptr, registrar))
918 notify_metermaids(pu->parkingexten, pu->parkinglot->parking_con, AST_DEVICE_INUSE);
921 AST_LIST_UNLOCK(&pu->parkinglot->parkings);
923 /* Only say number if it's a number and the channel hasn't been masqueraded away */
924 if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE) && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
925 /* If a channel is masqueraded into peer while playing back the parking slot number do not continue playing it back. This is the case if an attended transfer occurs. */
926 ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
927 /* Tell the peer channel the number of the parking space */
928 ast_say_digits(peer, pu->parkingnum, "", peer->language);
929 ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
931 if (peer == chan) { /* pu->notquiteyet = 1 */
932 /* Wake up parking thread if we're really done */
933 ast_indicate_data(pu->chan, AST_CONTROL_HOLD,
934 S_OR(pu->parkinglot->mohclass, NULL),
935 !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
937 pthread_kill(parking_thread, SIGURG);
942 /*! \brief Park a call */
943 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
945 struct ast_park_call_args args = {
950 return park_call_full(chan, peer, &args);
953 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout, int play_announcement, struct ast_park_call_args *args)
955 struct ast_channel *chan;
958 struct ast_park_call_args park_args = {0,};
962 args->timeout = timeout;
963 args->extout = extout;
966 if ((args->pu = park_space_reserve(rchan, peer, args)) == NULL) {
968 ast_stream_and_wait(peer, "beeperr", "");
969 return AST_FEATURE_RETURN_PARKFAILED;
972 /* Make a new, fake channel that we'll use to masquerade in the real one */
973 if (!(chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten, rchan->context, rchan->linkedid, rchan->amaflags, "Parked/%s",rchan->name))) {
974 ast_log(LOG_WARNING, "Unable to create parked channel\n");
978 /* Make formats okay */
979 chan->readformat = rchan->readformat;
980 chan->writeformat = rchan->writeformat;
981 ast_channel_masquerade(chan, rchan);
983 /* Setup the extensions and such */
984 set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
986 /* Make the masq execute */
987 if ((f = ast_read(chan)))
994 if (!play_announcement && args == &park_args) {
995 args->orig_chan_name = ast_strdupa(peer->name);
998 park_status = park_call_full(chan, peer, args);
999 if (park_status == 1) {
1000 /* would be nice to play "invalid parking extension" */
1008 /* Park call via masquraded channel */
1009 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1011 return masq_park_call(rchan, peer, timeout, extout, 0, NULL);
1014 static int masq_park_call_announce_args(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
1016 return masq_park_call(rchan, peer, 0, NULL, 1, args);
1019 static int masq_park_call_announce(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1021 return masq_park_call(rchan, peer, timeout, extout, 1, NULL);
1025 * \brief set caller and callee according to the direction
1026 * \param caller, callee, peer, chan, sense
1028 * Detect who triggered feature and set callee/caller variables accordingly
1030 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
1031 struct ast_channel *peer, struct ast_channel *chan, int sense)
1033 if (sense == FEATURE_SENSE_PEER) {
1043 * \brief support routing for one touch call parking
1044 * \param chan channel parking call
1045 * \param peer channel to be parked
1046 * \param config unsed
1047 * \param code unused
1048 * \param sense feature options
1051 * Setup channel, set return exten,priority to 's,1'
1052 * answer chan, sleep chan, park call
1054 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1056 struct ast_channel *parker;
1057 struct ast_channel *parkee;
1060 set_peers(&parker, &parkee, peer, chan, sense);
1061 /* we used to set chan's exten and priority to "s" and 1
1062 here, but this generates (in some cases) an invalid
1063 extension, and if "s" exists, could errantly
1064 cause execution of extensions you don't expect. It
1065 makes more sense to let nature take its course
1066 when chan finishes, and let the pbx do its thing
1067 and hang up when the park is over.
1069 if (chan->_state != AST_STATE_UP)
1070 res = ast_answer(chan);
1072 res = ast_safe_sleep(chan, 1000);
1074 if (!res) { /* one direction used to call park_call.... */
1075 res = masq_park_call_announce(parkee, parker, 0, NULL);
1076 /* PBX should hangup zombie channel if a masquerade actually occurred (res=0) */
1082 /*! \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
1083 other channel during the message, so please don't use this for very long messages
1085 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
1087 /* First play for caller, put other channel on auto service */
1088 if (ast_autoservice_start(callee_chan))
1090 if (ast_stream_and_wait(caller_chan, audiofile, "")) {
1091 ast_log(LOG_WARNING, "Failed to play automon message!\n");
1092 ast_autoservice_stop(callee_chan);
1095 if (ast_autoservice_stop(callee_chan))
1097 /* Then play for callee, put other channel on auto service */
1098 if (ast_autoservice_start(caller_chan))
1100 if (ast_stream_and_wait(callee_chan, audiofile, "")) {
1101 ast_log(LOG_WARNING, "Failed to play automon message !\n");
1102 ast_autoservice_stop(caller_chan);
1105 if (ast_autoservice_stop(caller_chan))
1111 * \brief Monitor a channel by DTMF
1112 * \param chan channel requesting monitor
1113 * \param peer channel to be monitored
1116 * \param sense feature options
1119 * Check monitor app enabled, setup channels, both caller/callee chans not null
1120 * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
1121 * \retval AST_FEATURE_RETURN_SUCCESS on success.
1122 * \retval -1 on error.
1124 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1126 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1129 struct ast_channel *caller_chan, *callee_chan;
1130 const char *automon_message_start = NULL;
1131 const char *automon_message_stop = NULL;
1134 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1138 if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
1140 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1144 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1145 if (caller_chan) { /* Find extra messages */
1146 automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
1147 automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
1150 if (!ast_strlen_zero(courtesytone)) { /* Play courtesy tone if configured */
1151 if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
1156 if (callee_chan->monitor) {
1157 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
1158 if (!ast_strlen_zero(automon_message_stop)) {
1159 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
1161 callee_chan->monitor->stop(callee_chan, 1);
1162 return AST_FEATURE_RETURN_SUCCESS;
1165 if (caller_chan && callee_chan) {
1166 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
1167 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
1168 const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
1171 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
1174 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
1176 if (!touch_monitor_prefix)
1177 touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
1179 if (touch_monitor) {
1180 len = strlen(touch_monitor) + 50;
1182 touch_filename = alloca(len);
1183 snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
1184 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
1186 caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
1187 callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
1188 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
1190 touch_filename = alloca(len);
1191 snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
1192 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
1195 for(x = 0; x < strlen(args); x++) {
1200 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
1202 pbx_exec(callee_chan, monitor_app, args);
1203 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
1204 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
1206 if (!ast_strlen_zero(automon_message_start)) { /* Play start message for both channels */
1207 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
1210 return AST_FEATURE_RETURN_SUCCESS;
1213 ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
1217 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1219 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1222 struct ast_channel *caller_chan, *callee_chan;
1223 const char *mixmonitor_spy_type = "MixMonitor";
1226 if (!mixmonitor_ok) {
1227 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
1231 if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
1233 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
1237 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1239 if (!ast_strlen_zero(courtesytone)) {
1240 if (ast_autoservice_start(callee_chan))
1242 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
1243 ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
1244 ast_autoservice_stop(callee_chan);
1247 if (ast_autoservice_stop(callee_chan))
1251 ast_channel_lock(callee_chan);
1252 count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
1253 ast_channel_unlock(callee_chan);
1255 /* This means a mixmonitor is attached to the channel, running or not is unknown. */
1258 ast_verb(3, "User hit '%s' to stop recording call.\n", code);
1260 /* Make sure they are running */
1261 ast_channel_lock(callee_chan);
1262 count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
1263 ast_channel_unlock(callee_chan);
1265 if (!stopmixmonitor_ok) {
1266 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
1269 if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
1270 stopmixmonitor_ok = 0;
1271 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
1274 pbx_exec(callee_chan, stopmixmonitor_app, "");
1275 return AST_FEATURE_RETURN_SUCCESS;
1279 ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");
1282 if (caller_chan && callee_chan) {
1283 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
1284 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
1287 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
1290 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
1292 if (touch_monitor) {
1293 len = strlen(touch_monitor) + 50;
1295 touch_filename = alloca(len);
1296 snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
1297 snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
1299 caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
1300 callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
1301 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
1303 touch_filename = alloca(len);
1304 snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
1305 snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
1308 for( x = 0; x < strlen(args); x++) {
1313 ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
1315 pbx_exec(callee_chan, mixmonitor_app, args);
1316 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
1317 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
1318 return AST_FEATURE_RETURN_SUCCESS;
1322 ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
1327 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1329 ast_verb(4, "User hit '%s' to disconnect call.\n", code);
1330 return AST_FEATURE_RETURN_HANGUP;
1333 static int finishup(struct ast_channel *chan)
1335 ast_indicate(chan, AST_CONTROL_UNHOLD);
1337 return ast_autoservice_stop(chan);
1341 * \brief Find the context for the transfer
1345 * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
1346 * \return a context string
1348 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
1350 const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
1351 if (ast_strlen_zero(s)) {
1352 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
1354 if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
1355 s = transferer->macrocontext;
1357 if (ast_strlen_zero(s)) {
1358 s = transferer->context;
1364 * \brief Blind transfer user to another extension
1365 * \param chan channel to be transfered
1366 * \param peer channel initiated blind transfer
1370 * \param sense feature options
1372 * Place chan on hold, check if transferred to parkinglot extension,
1373 * otherwise check extension exists and transfer caller.
1374 * \retval AST_FEATURE_RETURN_SUCCESS.
1375 * \retval -1 on failure.
1377 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1379 struct ast_channel *transferer;
1380 struct ast_channel *transferee;
1381 const char *transferer_real_context;
1383 int res, parkstatus = 0;
1385 set_peers(&transferer, &transferee, peer, chan, sense);
1386 transferer_real_context = real_ctx(transferer, transferee);
1387 /* Start autoservice on chan while we talk to the originator */
1388 ast_autoservice_start(transferee);
1389 ast_indicate(transferee, AST_CONTROL_HOLD);
1391 memset(xferto, 0, sizeof(xferto));
1394 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
1396 finishup(transferee);
1397 return -1; /* error ? */
1399 if (res > 0) /* If they've typed a digit already, handle it */
1400 xferto[0] = (char) res;
1402 ast_stopstream(transferer);
1403 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
1404 if (res < 0) { /* hangup, would be 0 for invalid and 1 for valid */
1405 finishup(transferee);
1408 if (!strcmp(xferto, ast_parking_ext())) {
1409 res = finishup(transferee);
1412 else if (!(parkstatus = masq_park_call_announce(transferee, transferer, 0, NULL))) { /* success */
1413 /* We return non-zero, but tell the PBX not to hang the channel when
1414 the thread dies -- We have to be careful now though. We are responsible for
1415 hanging up the channel, else it will never be hung up! */
1419 ast_log(LOG_WARNING, "Unable to park call %s, parkstatus = %d\n", transferee->name, parkstatus);
1421 /*! \todo XXX Maybe we should have another message here instead of invalid extension XXX */
1422 } else if (ast_exists_extension(transferee, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
1423 ast_cel_report_event(transferer, AST_CEL_BLINDTRANSFER, NULL, xferto, transferee);
1424 pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
1425 pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
1426 res=finishup(transferee);
1427 if (!transferer->cdr) { /* this code should never get called (in a perfect world) */
1428 transferer->cdr=ast_cdr_alloc();
1429 if (transferer->cdr) {
1430 ast_cdr_init(transferer->cdr, transferer); /* initialize our channel's cdr */
1431 ast_cdr_start(transferer->cdr);
1434 if (transferer->cdr) {
1435 struct ast_cdr *swap = transferer->cdr;
1436 ast_log(LOG_DEBUG,"transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
1437 transferer->name, transferee->name, transferer->cdr->lastapp, transferer->cdr->lastdata,
1438 transferer->cdr->channel, transferer->cdr->dstchannel);
1439 ast_log(LOG_DEBUG,"TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
1440 transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel, transferee->cdr->dstchannel);
1441 ast_log(LOG_DEBUG,"transferer_real_context=%s; xferto=%s\n", transferer_real_context, xferto);
1442 /* swap cdrs-- it will save us some time & work */
1443 transferer->cdr = transferee->cdr;
1444 transferee->cdr = swap;
1446 if (!transferee->pbx) {
1447 /* Doh! Use our handy async_goto functions */
1448 ast_verb(3, "Transferring %s to '%s' (context %s) priority 1\n"
1449 ,transferee->name, xferto, transferer_real_context);
1450 if (ast_async_goto(transferee, transferer_real_context, xferto, 1))
1451 ast_log(LOG_WARNING, "Async goto failed :-(\n");
1453 /* Set the channel's new extension, since it exists, using transferer context */
1454 ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT); /* don't let the after-bridge code run the h-exten */
1455 ast_log(LOG_DEBUG,"ABOUT TO AST_ASYNC_GOTO, have a pbx... set HANGUP_DONT on chan=%s\n", transferee->name);
1456 if (ast_channel_connected_line_macro(transferee, transferer, &transferer->connected, 1, 0)) {
1457 ast_channel_update_connected_line(transferee, &transferer->connected);
1459 set_c_e_p(transferee, transferer_real_context, xferto, 0);
1461 check_goto_on_transfer(transferer);
1464 ast_verb(3, "Unable to find extension '%s' in context '%s'\n", xferto, transferer_real_context);
1466 if (parkstatus != AST_FEATURE_RETURN_PARKFAILED && ast_stream_and_wait(transferer, xferfailsound, AST_DIGIT_ANY) < 0) {
1467 finishup(transferee);
1470 ast_stopstream(transferer);
1471 res = finishup(transferee);
1473 ast_verb(2, "Hungup during autoservice stop on '%s'\n", transferee->name);
1476 return AST_FEATURE_RETURN_SUCCESS;
1480 * \brief make channels compatible
1483 * \retval 0 on success.
1484 * \retval -1 on failure.
1486 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
1488 if (ast_channel_make_compatible(c, newchan) < 0) {
1489 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
1490 c->name, newchan->name);
1491 ast_hangup(newchan);
1498 * \brief Attended transfer
1499 * \param chan transfered user
1500 * \param peer person transfering call
1503 * \param sense feature options
1506 * Get extension to transfer to, if you cannot generate channel (or find extension)
1507 * return to host channel. After called channel answered wait for hangup of transferer,
1508 * bridge call between transfer peer (taking them off hold) to attended transfer channel.
1510 * \return -1 on failure
1512 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1514 struct ast_channel *transferer;
1515 struct ast_channel *transferee;
1516 const char *transferer_real_context;
1517 char xferto[256] = "";
1520 struct ast_channel *newchan;
1521 struct ast_channel *xferchan;
1522 struct ast_bridge_thread_obj *tobj;
1523 struct ast_bridge_config bconfig;
1524 struct ast_frame *f;
1526 struct ast_party_connected_line connected_line;
1527 struct ast_datastore *features_datastore;
1528 struct ast_dial_features *dialfeatures = NULL;
1530 ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
1531 set_peers(&transferer, &transferee, peer, chan, sense);
1532 transferer_real_context = real_ctx(transferer, transferee);
1533 /* Start autoservice on chan while we talk to the originator */
1534 ast_autoservice_start(transferee);
1535 ast_indicate(transferee, AST_CONTROL_HOLD);
1538 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
1540 finishup(transferee);
1543 if (res > 0) /* If they've typed a digit already, handle it */
1544 xferto[0] = (char) res;
1546 /* this is specific of atxfer */
1547 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
1548 if (res < 0) { /* hangup, would be 0 for invalid and 1 for valid */
1549 finishup(transferee);
1553 ast_log(LOG_WARNING, "Did not read data.\n");
1554 finishup(transferee);
1555 if (ast_stream_and_wait(transferer, "beeperr", ""))
1557 return AST_FEATURE_RETURN_SUCCESS;
1560 /* valid extension, res == 1 */
1561 if (!ast_exists_extension(transferer, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
1562 ast_log(LOG_WARNING, "Extension %s does not exist in context %s\n",xferto,transferer_real_context);
1563 finishup(transferee);
1564 if (ast_stream_and_wait(transferer, "beeperr", ""))
1566 return AST_FEATURE_RETURN_SUCCESS;
1569 /* If we are attended transfering to parking, just use builtin_parkcall instead of trying to track all of
1570 * the different variables for handling this properly with a builtin_atxfer */
1571 if (!strcmp(xferto, ast_parking_ext())) {
1572 finishup(transferee);
1573 return builtin_parkcall(chan, peer, config, code, sense, data);
1577 snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context); /* append context */
1579 /* If we are performing an attended transfer and we have two channels involved then
1580 copy sound file information to play upon attended transfer completion */
1582 const char *chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
1583 const char *chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
1585 if (!ast_strlen_zero(chan1_attended_sound)) {
1586 pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
1588 if (!ast_strlen_zero(chan2_attended_sound)) {
1589 pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
1593 newchan = feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
1594 xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1, transferer->language);
1596 ast_party_connected_line_init(&connected_line);
1597 if (!ast_check_hangup(transferer)) {
1598 /* Transferer is up - old behaviour */
1599 ast_indicate(transferer, -1);
1601 finishup(transferee);
1602 /* any reason besides user requested cancel and busy triggers the failed sound */
1603 if (outstate != AST_CONTROL_UNHOLD && outstate != AST_CONTROL_BUSY &&
1604 ast_stream_and_wait(transferer, xferfailsound, ""))
1606 if (ast_stream_and_wait(transferer, xfersound, ""))
1607 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1608 return AST_FEATURE_RETURN_SUCCESS;
1611 if (check_compat(transferer, newchan)) {
1612 /* we do mean transferee here, NOT transferer */
1613 finishup(transferee);
1616 memset(&bconfig,0,sizeof(struct ast_bridge_config));
1617 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
1618 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
1619 /* We need to get the transferer's connected line information copied
1620 * at this point because he is likely to hang up during the bridge with
1621 * newchan. This info will be used down below before bridging the
1622 * transferee and newchan
1624 * As a result, we need to be sure to free this data before returning
1625 * or overwriting it.
1627 ast_channel_lock(transferer);
1628 ast_party_connected_line_copy(&connected_line, &transferer->connected);
1629 ast_channel_unlock(transferer);
1630 res = ast_bridge_call(transferer, newchan, &bconfig);
1631 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
1632 ast_hangup(newchan);
1633 if (ast_stream_and_wait(transferer, xfersound, ""))
1634 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1635 finishup(transferee);
1636 transferer->_softhangup = 0;
1637 ast_party_connected_line_free(&connected_line);
1638 return AST_FEATURE_RETURN_SUCCESS;
1641 ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
1643 if (check_compat(transferee, newchan)) {
1644 finishup(transferee);
1645 ast_party_connected_line_free(&connected_line);
1648 ast_indicate(transferee, AST_CONTROL_UNHOLD);
1650 if ((ast_autoservice_stop(transferee) < 0)
1651 || (ast_waitfordigit(transferee, 100) < 0)
1652 || (ast_waitfordigit(newchan, 100) < 0)
1653 || ast_check_hangup(transferee)
1654 || ast_check_hangup(newchan)) {
1655 ast_hangup(newchan);
1656 ast_party_connected_line_free(&connected_line);
1659 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
1661 ast_hangup(newchan);
1662 ast_party_connected_line_free(&connected_line);
1665 /* Make formats okay */
1666 xferchan->visible_indication = transferer->visible_indication;
1667 xferchan->readformat = transferee->readformat;
1668 xferchan->writeformat = transferee->writeformat;
1669 ast_channel_masquerade(xferchan, transferee);
1670 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
1671 xferchan->_state = AST_STATE_UP;
1672 ast_clear_flag(xferchan, AST_FLAGS_ALL);
1673 xferchan->_softhangup = 0;
1674 if ((f = ast_read(xferchan)))
1676 newchan->_state = AST_STATE_UP;
1677 ast_clear_flag(newchan, AST_FLAGS_ALL);
1678 newchan->_softhangup = 0;
1679 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
1680 ast_hangup(xferchan);
1681 ast_hangup(newchan);
1682 ast_party_connected_line_free(&connected_line);
1686 ast_channel_lock(newchan);
1687 if ((features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL))) {
1688 dialfeatures = features_datastore->data;
1690 ast_channel_unlock(newchan);
1693 /* newchan should always be the callee and shows up as callee in dialfeatures, but for some reason
1694 I don't currently understand, the abilities of newchan seem to be stored on the caller side */
1695 ast_copy_flags(&(config->features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
1696 dialfeatures = NULL;
1699 ast_channel_lock(xferchan);
1700 if ((features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL))) {
1701 dialfeatures = features_datastore->data;
1703 ast_channel_unlock(xferchan);
1706 ast_copy_flags(&(config->features_caller), &(dialfeatures->features_caller), AST_FLAGS_ALL);
1709 tobj->chan = newchan;
1710 tobj->peer = xferchan;
1711 tobj->bconfig = *config;
1713 if (tobj->bconfig.end_bridge_callback_data_fixup) {
1714 tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
1717 /* Due to a limitation regarding when callerID is set on a Local channel,
1718 * we use the transferer's connected line information here.
1721 /* xferchan is transferee, and newchan is the transfer target
1722 * So...in a transfer, who is the caller and who is the callee?
1724 * When the call is originally made, it is clear who is caller and callee.
1725 * When a transfer occurs, it is my humble opinion that the transferee becomes
1726 * the caller, and the transfer target is the callee.
1728 * The problem is that these macros were set with the intention of the original
1729 * caller and callee taking those roles. A transfer can totally mess things up,
1730 * to be technical. What sucks even more is that you can't effectively change
1731 * the macros in the dialplan during the call from the transferer to the transfer
1732 * target because the transferee is stuck with whatever role he originally had.
1734 * I think the answer here is just to make sure that it is well documented that
1735 * during a transfer, the transferee is the "caller" and the transfer target
1738 * This means that if party A calls party B, and party A transfers party B to
1739 * party C, then B has switched roles for the call. Now party B will have the
1740 * caller macro called on his channel instead of the callee macro.
1742 * Luckily, the method by which the bridge is launched here ensures that the
1743 * transferee is the "chan" on the bridge and the transfer target is the "peer,"
1744 * so my idea for the roles post-transfer does not require extensive code changes.
1746 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1747 if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
1748 ast_channel_update_connected_line(xferchan, &connected_line);
1750 ast_channel_lock(xferchan);
1751 ast_connected_line_copy_from_caller(&connected_line, &xferchan->cid);
1752 ast_channel_unlock(xferchan);
1753 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1754 if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
1755 ast_channel_update_connected_line(newchan, &connected_line);
1757 ast_party_connected_line_free(&connected_line);
1759 if (ast_stream_and_wait(newchan, xfersound, ""))
1760 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1761 bridge_call_thread_launch(tobj);
1762 return -1; /* XXX meaning the channel is bridged ? */
1763 } else if (!ast_check_hangup(transferee)) {
1764 /* act as blind transfer */
1765 if (ast_autoservice_stop(transferee) < 0) {
1766 ast_hangup(newchan);
1771 unsigned int tries = 0;
1772 char *transferer_tech, *transferer_name = ast_strdupa(transferer->name);
1774 transferer_tech = strsep(&transferer_name, "/");
1775 transferer_name = strsep(&transferer_name, "-");
1777 if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
1778 ast_log(LOG_WARNING, "Transferer has invalid channel name: '%s'\n", transferer->name);
1779 if (ast_stream_and_wait(transferee, "beeperr", ""))
1781 return AST_FEATURE_RETURN_SUCCESS;
1784 ast_log(LOG_NOTICE, "We're trying to call %s/%s\n", transferer_tech, transferer_name);
1785 newchan = feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
1786 transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0, transferer->language);
1787 while (!newchan && !atxferdropcall && tries < atxfercallbackretries) {
1788 /* Trying to transfer again */
1789 ast_autoservice_start(transferee);
1790 ast_indicate(transferee, AST_CONTROL_HOLD);
1792 newchan = feature_request_and_dial(transferer, transferee, "Local", ast_best_codec(transferer->nativeformats),
1793 xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num, transferer->cid.cid_name, 1, transferer->language);
1794 if (ast_autoservice_stop(transferee) < 0) {
1796 ast_hangup(newchan);
1800 /* Transfer failed, sleeping */
1801 ast_debug(1, "Sleeping for %d ms before callback.\n", atxferloopdelay);
1802 ast_safe_sleep(transferee, atxferloopdelay);
1803 ast_debug(1, "Trying to callback...\n");
1804 newchan = feature_request_and_dial(transferee, NULL, transferer_tech, ast_best_codec(transferee->nativeformats),
1805 transferer_name, atxfernoanswertimeout, &outstate, transferee->cid.cid_num, transferee->cid.cid_name, 0, transferer->language);
1813 ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
1815 /* newchan is up, we should prepare transferee and bridge them */
1816 if (check_compat(transferee, newchan)) {
1817 finishup(transferee);
1820 ast_indicate(transferee, AST_CONTROL_UNHOLD);
1822 if ((ast_waitfordigit(transferee, 100) < 0)
1823 || (ast_waitfordigit(newchan, 100) < 0)
1824 || ast_check_hangup(transferee)
1825 || ast_check_hangup(newchan)) {
1826 ast_hangup(newchan);
1830 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
1832 ast_hangup(newchan);
1835 /* Make formats okay */
1836 xferchan->visible_indication = transferer->visible_indication;
1837 xferchan->readformat = transferee->readformat;
1838 xferchan->writeformat = transferee->writeformat;
1839 ast_channel_masquerade(xferchan, transferee);
1840 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
1841 xferchan->_state = AST_STATE_UP;
1842 ast_clear_flag(xferchan, AST_FLAGS_ALL);
1843 xferchan->_softhangup = 0;
1844 if ((f = ast_read(xferchan)))
1846 newchan->_state = AST_STATE_UP;
1847 ast_clear_flag(newchan, AST_FLAGS_ALL);
1848 newchan->_softhangup = 0;
1849 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
1850 ast_hangup(xferchan);
1851 ast_hangup(newchan);
1854 tobj->chan = newchan;
1855 tobj->peer = xferchan;
1856 tobj->bconfig = *config;
1858 if (tobj->bconfig.end_bridge_callback_data_fixup) {
1859 tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
1862 ast_channel_lock(newchan);
1863 ast_connected_line_copy_from_caller(&connected_line, &newchan->cid);
1864 ast_channel_unlock(newchan);
1865 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1866 if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
1867 ast_channel_update_connected_line(xferchan, &connected_line);
1869 ast_channel_lock(xferchan);
1870 ast_connected_line_copy_from_caller(&connected_line, &xferchan->cid);
1871 ast_channel_unlock(xferchan);
1872 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
1873 if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
1874 ast_channel_update_connected_line(newchan, &connected_line);
1877 ast_party_connected_line_free(&connected_line);
1879 if (ast_stream_and_wait(newchan, xfersound, ""))
1880 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
1881 bridge_call_thread_launch(tobj);
1882 return -1; /* XXX meaning the channel is bridged ? */
1884 /* Transferee hung up */
1885 finishup(transferee);
1890 /* add atxfer and automon as undefined so you can only use em if you configure them */
1891 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
1893 AST_RWLOCK_DEFINE_STATIC(features_lock);
1895 static struct ast_call_feature builtin_features[] = {
1896 { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1897 { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1898 { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1899 { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1900 { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1901 { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
1905 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
1907 /*! \brief register new feature into feature_list*/
1908 void ast_register_feature(struct ast_call_feature *feature)
1911 ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
1915 AST_RWLIST_WRLOCK(&feature_list);
1916 AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
1917 AST_RWLIST_UNLOCK(&feature_list);
1919 ast_verb(2, "Registered Feature '%s'\n",feature->sname);
1923 * \brief Add new feature group
1924 * \param fgname feature group name.
1926 * Add new feature group to the feature group list insert at head of list.
1927 * \note This function MUST be called while feature_groups is locked.
1929 static struct feature_group* register_group(const char *fgname)
1931 struct feature_group *fg;
1934 ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
1938 if (!(fg = ast_calloc(1, sizeof(*fg))))
1941 if (ast_string_field_init(fg, 128)) {
1946 ast_string_field_set(fg, gname, fgname);
1948 AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
1950 ast_verb(2, "Registered group '%s'\n", fg->gname);
1956 * \brief Add feature to group
1957 * \param fg feature group
1959 * \param feature feature to add.
1961 * Check fg and feature specified, add feature to list
1962 * \note This function MUST be called while feature_groups is locked.
1964 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
1966 struct feature_group_exten *fge;
1969 ast_log(LOG_NOTICE, "You didn't pass a group!\n");
1974 ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
1978 if (!(fge = ast_calloc(1, sizeof(*fge))))
1981 if (ast_string_field_init(fge, 128)) {
1986 ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
1988 fge->feature = feature;
1990 AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
1992 ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
1993 feature->sname, fg->gname, exten);
1996 void ast_unregister_feature(struct ast_call_feature *feature)
2002 AST_RWLIST_WRLOCK(&feature_list);
2003 AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
2004 AST_RWLIST_UNLOCK(&feature_list);
2009 /*! \brief Remove all features in the list */
2010 static void ast_unregister_features(void)
2012 struct ast_call_feature *feature;
2014 AST_RWLIST_WRLOCK(&feature_list);
2015 while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
2018 AST_RWLIST_UNLOCK(&feature_list);
2021 /*! \brief find a call feature by name */
2022 static struct ast_call_feature *find_dynamic_feature(const char *name)
2024 struct ast_call_feature *tmp;
2026 AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
2027 if (!strcasecmp(tmp->sname, name)) {
2035 /*! \brief Remove all feature groups in the list */
2036 static void ast_unregister_groups(void)
2038 struct feature_group *fg;
2039 struct feature_group_exten *fge;
2041 AST_RWLIST_WRLOCK(&feature_groups);
2042 while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
2043 while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
2044 ast_string_field_free_memory(fge);
2048 ast_string_field_free_memory(fg);
2051 AST_RWLIST_UNLOCK(&feature_groups);
2055 * \brief Find a group by name
2056 * \param name feature name
2057 * \retval feature group on success.
2058 * \retval NULL on failure.
2060 static struct feature_group *find_group(const char *name) {
2061 struct feature_group *fg = NULL;
2063 AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
2064 if (!strcasecmp(fg->gname, name))
2071 void ast_rdlock_call_features(void)
2073 ast_rwlock_rdlock(&features_lock);
2076 void ast_unlock_call_features(void)
2078 ast_rwlock_unlock(&features_lock);
2081 struct ast_call_feature *ast_find_call_feature(const char *name)
2084 for (x = 0; x < FEATURES_COUNT; x++) {
2085 if (!strcasecmp(name, builtin_features[x].sname))
2086 return &builtin_features[x];
2092 * \brief exec an app by feature
2093 * \param chan,peer,config,code,sense,data
2095 * Find a feature, determine which channel activated
2096 * \retval AST_FEATURE_RETURN_NO_HANGUP_PEER
2098 * \retval -2 when an application cannot be found.
2100 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2102 struct ast_app *app;
2103 struct ast_call_feature *feature = data;
2104 struct ast_channel *work, *idle;
2107 if (!feature) { /* shouldn't ever happen! */
2108 ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
2112 if (sense == FEATURE_SENSE_CHAN) {
2113 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
2114 return AST_FEATURE_RETURN_KEEPTRYING;
2115 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
2123 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
2124 return AST_FEATURE_RETURN_KEEPTRYING;
2125 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
2134 if (!(app = pbx_findapp(feature->app))) {
2135 ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
2139 ast_autoservice_start(idle);
2142 pbx_builtin_setvar_helper(work, "DYNAMIC_PEERNAME", idle->name);
2143 pbx_builtin_setvar_helper(idle, "DYNAMIC_PEERNAME", work->name);
2144 pbx_builtin_setvar_helper(work, "DYNAMIC_FEATURENAME", feature->sname);
2145 pbx_builtin_setvar_helper(idle, "DYNAMIC_FEATURENAME", feature->sname);
2148 if (!ast_strlen_zero(feature->moh_class))
2149 ast_moh_start(idle, feature->moh_class, NULL);
2151 res = pbx_exec(work, app, feature->app_args);
2153 if (!ast_strlen_zero(feature->moh_class))
2156 ast_autoservice_stop(idle);
2159 return AST_FEATURE_RETURN_SUCCESSBREAK;
2161 return AST_FEATURE_RETURN_SUCCESS; /*! \todo XXX should probably return res */
2164 static void unmap_features(void)
2168 ast_rwlock_wrlock(&features_lock);
2169 for (x = 0; x < FEATURES_COUNT; x++)
2170 strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
2171 ast_rwlock_unlock(&features_lock);
2174 static int remap_feature(const char *name, const char *value)
2178 ast_rwlock_wrlock(&features_lock);
2179 for (x = 0; x < FEATURES_COUNT; x++) {
2180 if (strcasecmp(builtin_features[x].sname, name))
2183 ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
2187 ast_rwlock_unlock(&features_lock);
2193 * \brief Helper function for feature_interpret and ast_feature_detect
2194 * \param chan,peer,config,code,sense,dynamic_features char buf,feature flags,operation,feature
2196 * Lock features list, browse for code, unlock list
2197 * If a feature is found and the operation variable is set, that feature's
2198 * operation is executed. The first feature found is copied to the feature parameter.
2199 * \retval res on success.
2200 * \retval -1 on failure.
2202 static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel *peer,
2203 struct ast_bridge_config *config, const char *code, int sense, char *dynamic_features_buf,
2204 struct ast_flags *features, int operation, struct ast_call_feature *feature)
2207 struct feature_group *fg = NULL;
2208 struct feature_group_exten *fge;
2209 struct ast_call_feature *tmpfeature;
2211 int res = AST_FEATURE_RETURN_PASSDIGITS;
2212 int feature_detected = 0;
2214 if (!(peer && chan && config) && operation) {
2215 return -1; /* can not run feature operation */
2218 ast_rwlock_rdlock(&features_lock);
2219 for (x = 0; x < FEATURES_COUNT; x++) {
2220 if ((ast_test_flag(features, builtin_features[x].feature_mask)) &&
2221 !ast_strlen_zero(builtin_features[x].exten)) {
2222 /* Feature is up for consideration */
2223 if (!strcmp(builtin_features[x].exten, code)) {
2224 ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
2226 res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
2228 memcpy(feature, &builtin_features[x], sizeof(feature));
2229 feature_detected = 1;
2231 } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
2232 if (res == AST_FEATURE_RETURN_PASSDIGITS)
2233 res = AST_FEATURE_RETURN_STOREDIGITS;
2237 ast_rwlock_unlock(&features_lock);
2239 if (ast_strlen_zero(dynamic_features_buf) || feature_detected) {
2243 tmp = dynamic_features_buf;
2245 while ((tok = strsep(&tmp, "#"))) {
2246 AST_RWLIST_RDLOCK(&feature_groups);
2248 fg = find_group(tok);
2251 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
2252 if (strcasecmp(fge->exten, code))
2255 res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
2257 memcpy(feature, fge->feature, sizeof(feature));
2258 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
2259 AST_RWLIST_UNLOCK(&feature_groups);
2262 res = AST_FEATURE_RETURN_PASSDIGITS;
2268 AST_RWLIST_UNLOCK(&feature_groups);
2270 AST_RWLIST_RDLOCK(&feature_list);
2272 if (!(tmpfeature = find_dynamic_feature(tok))) {
2273 AST_RWLIST_UNLOCK(&feature_list);
2277 /* Feature is up for consideration */
2278 if (!strcmp(tmpfeature->exten, code)) {
2279 ast_verb(3, " Feature Found: %s exten: %s\n",tmpfeature->sname, tok);
2281 res = tmpfeature->operation(chan, peer, config, code, sense, tmpfeature);
2283 memcpy(feature, tmpfeature, sizeof(feature));
2284 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
2285 AST_RWLIST_UNLOCK(&feature_list);
2288 res = AST_FEATURE_RETURN_PASSDIGITS;
2289 } else if (!strncmp(tmpfeature->exten, code, strlen(code)))
2290 res = AST_FEATURE_RETURN_STOREDIGITS;
2292 AST_RWLIST_UNLOCK(&feature_list);
2299 * \brief Check the dynamic features
2300 * \param chan,peer,config,code,sense
2302 * \retval res on success.
2303 * \retval -1 on failure.
2306 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense) {
2308 char dynamic_features_buf[128];
2309 const char *peer_dynamic_features, *chan_dynamic_features;
2310 struct ast_flags features;
2311 struct ast_call_feature feature;
2312 if (sense == FEATURE_SENSE_CHAN) {
2313 ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
2316 ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
2319 ast_channel_lock(peer);
2320 peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
2321 ast_channel_unlock(peer);
2323 ast_channel_lock(chan);
2324 chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
2325 ast_channel_unlock(chan);
2327 snprintf(dynamic_features_buf, sizeof(dynamic_features_buf), "%s%s%s", S_OR(chan_dynamic_features, ""), chan_dynamic_features && peer_dynamic_features ? "#" : "", S_OR(peer_dynamic_features,""));
2329 ast_debug(3, "Feature interpret: chan=%s, peer=%s, code=%s, sense=%d, features=%d, dynamic=%s\n", chan->name, peer->name, code, sense, features.flags, dynamic_features_buf);
2331 return feature_interpret_helper(chan, peer, config, code, sense, dynamic_features_buf, &features, 1, &feature);
2335 int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
2337 return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, 0, feature);
2340 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
2344 ast_clear_flag(config, AST_FLAGS_ALL);
2346 ast_rwlock_rdlock(&features_lock);
2347 for (x = 0; x < FEATURES_COUNT; x++) {
2348 if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
2351 if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
2352 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
2354 if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
2355 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
2357 ast_rwlock_unlock(&features_lock);
2359 if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
2360 const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
2362 if (dynamic_features) {
2363 char *tmp = ast_strdupa(dynamic_features);
2365 struct ast_call_feature *feature;
2367 /* while we have a feature */
2368 while ((tok = strsep(&tmp, "#"))) {
2369 AST_RWLIST_RDLOCK(&feature_list);
2370 if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
2371 if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
2372 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
2373 if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
2374 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
2376 AST_RWLIST_UNLOCK(&feature_list);
2383 * \brief Get feature and dial
2384 * \param caller,transferee,type,format,data,timeout,outstate,cid_num,cid_name,igncallerstate
2386 * Request channel, set channel variables, initiate call,check if they want to disconnect
2387 * go into loop, check if timeout has elapsed, check if person to be transfered hung up,
2388 * check for answer break loop, set cdr return channel.
2390 * \todo XXX Check - this is very similar to the code in channel.c
2391 * \return always a channel
2393 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller, struct ast_channel *transferee, const char *type, int format, void *data, int timeout, int *outstate, const char *cid_num, const char *cid_name, int igncallerstate, const char *language)
2398 struct ast_channel *chan;
2399 struct ast_channel *monitor_chans[2];
2400 struct ast_channel *active_channel;
2401 int res = 0, ready = 0;
2402 struct timeval started;
2404 char *disconnect_code = NULL, *dialed_code = NULL;
2406 if (!(chan = ast_request(type, format, caller, data, &cause))) {
2407 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
2409 case AST_CAUSE_BUSY:
2410 state = AST_CONTROL_BUSY;
2412 case AST_CAUSE_CONGESTION:
2413 state = AST_CONTROL_CONGESTION;
2419 ast_set_callerid(chan, cid_num, cid_name, cid_num);
2420 ast_string_field_set(chan, language, language);
2421 ast_channel_inherit_variables(caller, chan);
2422 pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller->name);
2424 ast_channel_lock(chan);
2425 ast_connected_line_copy_from_caller(&chan->connected, &caller->cid);
2426 ast_channel_unlock(chan);
2428 if (ast_call(chan, data, timeout)) {
2429 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
2433 ast_indicate(caller, AST_CONTROL_RINGING);
2434 /* support dialing of the featuremap disconnect code while performing an attended tranfer */
2435 ast_rwlock_rdlock(&features_lock);
2436 for (x = 0; x < FEATURES_COUNT; x++) {
2437 if (strcasecmp(builtin_features[x].sname, "disconnect"))
2440 disconnect_code = builtin_features[x].exten;
2441 len = strlen(disconnect_code) + 1;
2442 dialed_code = alloca(len);
2443 memset(dialed_code, 0, len);
2446 ast_rwlock_unlock(&features_lock);
2448 started = ast_tvnow();
2451 ast_poll_channel_add(caller, chan);
2453 while (!((transferee && ast_check_hangup(transferee)) && (!igncallerstate && ast_check_hangup(caller))) && timeout && (chan->_state != AST_STATE_UP)) {
2454 struct ast_frame *f = NULL;
2456 monitor_chans[0] = caller;
2457 monitor_chans[1] = chan;
2458 active_channel = ast_waitfor_n(monitor_chans, 2, &to);
2460 /* see if the timeout has been violated */
2461 if(ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
2462 state = AST_CONTROL_UNHOLD;
2463 ast_log(LOG_NOTICE, "We exceeded our AT-timeout\n");
2464 break; /*doh! timeout*/
2467 if (!active_channel)
2470 if (chan && (chan == active_channel)){
2471 if (!ast_strlen_zero(chan->call_forward)) {
2472 if (!(chan = ast_call_forward(caller, chan, &to, format, NULL, outstate))) {
2478 if (f == NULL) { /*doh! where'd he go?*/
2479 state = AST_CONTROL_HANGUP;
2484 if (f->frametype == AST_FRAME_CONTROL || f->frametype == AST_FRAME_DTMF || f->frametype == AST_FRAME_TEXT) {
2485 if (f->subclass.integer == AST_CONTROL_RINGING) {
2486 state = f->subclass.integer;
2487 ast_verb(3, "%s is ringing\n", chan->name);
2488 ast_indicate(caller, AST_CONTROL_RINGING);
2489 } else if ((f->subclass.integer == AST_CONTROL_BUSY) || (f->subclass.integer == AST_CONTROL_CONGESTION)) {
2490 state = f->subclass.integer;
2491 ast_verb(3, "%s is busy\n", chan->name);
2492 ast_indicate(caller, AST_CONTROL_BUSY);
2496 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
2497 /* This is what we are hoping for */
2498 state = f->subclass.integer;
2503 } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
2504 if (ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
2505 ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
2507 } else if (f->subclass.integer != -1 && f->subclass.integer != AST_CONTROL_PROGRESS) {
2508 ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
2510 /* else who cares */
2511 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
2512 ast_write(caller, f);
2515 } else if (caller && (active_channel == caller)) {
2516 f = ast_read(caller);
2517 if (f == NULL) { /*doh! where'd he go?*/
2518 if (!igncallerstate) {
2519 if (ast_check_hangup(caller) && !ast_check_hangup(chan)) {
2520 /* make this a blind transfer */
2524 state = AST_CONTROL_HANGUP;
2530 if (f->frametype == AST_FRAME_DTMF) {
2531 dialed_code[x++] = f->subclass.integer;
2532 dialed_code[x] = '\0';
2533 if (strlen(dialed_code) == len) {
2535 } else if (x && strncmp(dialed_code, disconnect_code, x)) {
2537 dialed_code[x] = '\0';
2539 if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
2540 /* Caller Canceled the call */
2541 state = AST_CONTROL_UNHOLD;
2546 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
2555 ast_poll_channel_del(caller, chan);
2558 ast_indicate(caller, -1);
2559 if (chan && ready) {
2560 if (chan->_state == AST_STATE_UP)
2561 state = AST_CONTROL_ANSWER;
2577 void ast_channel_log(char *title, struct ast_channel *chan);
2579 void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
2581 ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long)chan);
2582 ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
2583 chan->name, chan->appl, chan->data, chan->context, chan->exten, chan->priority);
2584 ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
2585 chan->accountcode, chan->dialcontext, chan->amaflags, chan->macrocontext, chan->macroexten, chan->macropriority);
2586 ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; _bridge: %p; uniqueID: %s; linkedID:%s\n",
2587 chan->masq, chan->masqr,
2588 chan->_bridge, chan->uniqueid, chan->linkedid);
2590 ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
2591 chan->masqr->name, chan->masqr->cdr);
2593 ast_log(LOG_NOTICE, "CHAN: Bridged to %s\n", chan->_bridge->name);
2595 ast_log(LOG_NOTICE, "===== done ====\n");
2599 * \brief return the first unlocked cdr in a possible chain
2601 static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
2603 struct ast_cdr *cdr_orig = cdr;
2605 if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
2609 return cdr_orig; /* everybody LOCKED or some other weirdness, like a NULL */
2612 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
2614 const char *feature;
2616 if (ast_strlen_zero(features)) {
2620 for (feature = features; *feature; feature++) {
2624 ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
2628 ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
2632 ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
2636 ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
2639 ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
2644 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
2646 struct ast_datastore *ds_callee_features = NULL, *ds_caller_features = NULL;
2647 struct ast_dial_features *callee_features = NULL, *caller_features = NULL;
2649 ast_channel_lock(caller);
2650 ds_caller_features = ast_channel_datastore_find(caller, &dial_features_info, NULL);
2651 ast_channel_unlock(caller);
2652 if (!ds_caller_features) {
2653 if (!(ds_caller_features = ast_datastore_alloc(&dial_features_info, NULL))) {
2654 ast_log(LOG_WARNING, "Unable to create channel datastore for caller features. Aborting!\n");
2657 if (!(caller_features = ast_calloc(1, sizeof(*caller_features)))) {
2658 ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
2659 ast_datastore_free(ds_caller_features);
2662 ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
2663 caller_features->is_caller = 1;
2664 ast_copy_flags(&(caller_features->features_callee), &(config->features_callee), AST_FLAGS_ALL);
2665 ast_copy_flags(&(caller_features->features_caller), &(config->features_caller), AST_FLAGS_ALL);
2666 ds_caller_features->data = caller_features;
2667 ast_channel_lock(caller);
2668 ast_channel_datastore_add(caller, ds_caller_features);
2669 ast_channel_unlock(caller);
2671 /* If we don't return here, then when we do a builtin_atxfer we will copy the disconnect
2672 * flags over from the atxfer to the caller */
2676 ast_channel_lock(callee);
2677 ds_callee_features = ast_channel_datastore_find(callee, &dial_features_info, NULL);
2678 ast_channel_unlock(callee);
2679 if (!ds_callee_features) {
2680 if (!(ds_callee_features = ast_datastore_alloc(&dial_features_info, NULL))) {
2681 ast_log(LOG_WARNING, "Unable to create channel datastore for callee features. Aborting!\n");
2684 if (!(callee_features = ast_calloc(1, sizeof(*callee_features)))) {
2685 ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
2686 ast_datastore_free(ds_callee_features);
2689 ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
2690 callee_features->is_caller = 0;
2691 ast_copy_flags(&(callee_features->features_callee), &(config->features_caller), AST_FLAGS_ALL);
2692 ast_copy_flags(&(callee_features->features_caller), &(config->features_callee), AST_FLAGS_ALL);
2693 ds_callee_features->data = callee_features;
2694 ast_channel_lock(callee);
2695 ast_channel_datastore_add(callee, ds_callee_features);
2696 ast_channel_unlock(callee);
2703 * \brief bridge the call and set CDR
2704 * \param chan,peer,config
2706 * Set start time, check for two channels,check if monitor on
2707 * check for feature activation, create new CDR
2708 * \retval res on success.
2709 * \retval -1 on failure to bridge.
2711 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
2713 /* Copy voice back and forth between the two channels. Give the peer
2714 the ability to transfer calls with '#<extension' syntax. */
2715 struct ast_frame *f;
2716 struct ast_channel *who;
2717 char chan_featurecode[FEATURE_MAX_LEN + 1]="";
2718 char peer_featurecode[FEATURE_MAX_LEN + 1]="";
2719 char orig_channame[AST_MAX_EXTENSION];
2720 char orig_peername[AST_MAX_EXTENSION];
2726 struct ast_option_header *aoh;
2727 struct ast_cdr *bridge_cdr = NULL;
2728 struct ast_cdr *orig_peer_cdr = NULL;
2729 struct ast_cdr *chan_cdr = pick_unlocked_cdr(chan->cdr); /* the proper chan cdr, if there are forked cdrs */
2730 struct ast_cdr *peer_cdr = pick_unlocked_cdr(peer->cdr); /* the proper chan cdr, if there are forked cdrs */
2731 struct ast_cdr *new_chan_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
2732 struct ast_cdr *new_peer_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
2735 pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
2736 pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
2738 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
2741 set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
2742 add_features_datastores(chan, peer, config);
2744 /* This is an interesting case. One example is if a ringing channel gets redirected to
2745 * an extension that picks up a parked call. This will make sure that the call taken
2746 * out of parking gets told that the channel it just got bridged to is still ringing. */
2747 if (chan->_state == AST_STATE_RINGING && peer->visible_indication != AST_CONTROL_RINGING) {
2748 ast_indicate(peer, AST_CONTROL_RINGING);
2752 const char *monitor_exec;
2753 struct ast_channel *src = NULL;
2755 if (!(monitor_app = pbx_findapp("Monitor")))
2758 if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR")))
2760 else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
2762 if (monitor_app && src) {
2763 char *tmp = ast_strdupa(monitor_exec);
2764 pbx_exec(src, monitor_app, tmp);
2768 set_config_flags(chan, peer, config);
2770 /* Answer if need be */
2771 if (chan->_state != AST_STATE_UP) {
2772 if (ast_raw_answer(chan, 1)) {
2778 /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
2779 ast_channel_log("Pre-bridge CHAN Channel info", chan);
2780 ast_channel_log("Pre-bridge PEER Channel info", peer);
2782 /* two channels are being marked as linked here */
2783 ast_channel_set_linkgroup(chan,peer);
2785 /* copy the userfield from the B-leg to A-leg if applicable */
2786 if (chan->cdr && peer->cdr && !ast_strlen_zero(peer->cdr->userfield)) {
2788 if (!ast_strlen_zero(chan->cdr->userfield)) {
2789 snprintf(tmp, sizeof(tmp), "%s;%s", chan->cdr->userfield, peer->cdr->userfield);
2790 ast_cdr_appenduserfield(chan, tmp);
2792 ast_cdr_setuserfield(chan, peer->cdr->userfield);
2793 /* free the peer's cdr without ast_cdr_free complaining */
2794 ast_free(peer->cdr);
2797 ast_copy_string(orig_channame,chan->name,sizeof(orig_channame));
2798 ast_copy_string(orig_peername,peer->name,sizeof(orig_peername));
2799 orig_peer_cdr = peer_cdr;
2801 if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
2804 ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
2805 ast_cdr_update(chan);
2806 bridge_cdr = ast_cdr_dup_unique_swap(chan_cdr);
2807 ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
2808 ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
2809 if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
2810 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
2814 /* better yet, in a xfer situation, find out why the chan cdr got zapped (pun unintentional) */
2815 bridge_cdr = ast_cdr_alloc(); /* this should be really, really rare/impossible? */
2816 ast_copy_string(bridge_cdr->channel, chan->name, sizeof(bridge_cdr->channel));
2817 ast_copy_string(bridge_cdr->dstchannel, peer->name, sizeof(bridge_cdr->dstchannel));
2818 ast_copy_string(bridge_cdr->uniqueid, chan->uniqueid, sizeof(bridge_cdr->uniqueid));
2819 ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
2820 ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
2821 ast_cdr_setcid(bridge_cdr, chan);
2822 bridge_cdr->disposition = (chan->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NULL;
2823 bridge_cdr->amaflags = chan->amaflags ? chan->amaflags : ast_default_amaflags;
2824 ast_copy_string(bridge_cdr->accountcode, chan->accountcode, sizeof(bridge_cdr->accountcode));
2825 /* Destination information */
2826 ast_copy_string(bridge_cdr->dst, chan->exten, sizeof(bridge_cdr->dst));
2827 ast_copy_string(bridge_cdr->dcontext, chan->context, sizeof(bridge_cdr->dcontext));
2829 bridge_cdr->start = peer_cdr->start;
2830 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
2832 ast_cdr_start(bridge_cdr);
2835 ast_debug(4,"bridge answer set, chan answer set\n");
2836 /* peer_cdr->answer will be set when a macro runs on the peer;
2837 in that case, the bridge answer will be delayed while the
2838 macro plays on the peer channel. The peer answered the call
2839 before the macro started playing. To the phone system,
2840 this is billable time for the call, even tho the caller
2841 hears nothing but ringing while the macro does its thing. */
2843 /* Another case where the peer cdr's time will be set, is when
2844 A self-parks by pickup up phone and dialing 700, then B
2845 picks up A by dialing its parking slot; there may be more
2846 practical paths that get the same result, tho... in which
2847 case you get the previous answer time from the Park... which
2848 is before the bridge's start time, so I added in the
2849 tvcmp check to the if below */
2851 if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
2852 bridge_cdr->answer = peer_cdr->answer;
2853 bridge_cdr->disposition = peer_cdr->disposition;
2855 chan_cdr->answer = peer_cdr->answer;
2856 chan_cdr->disposition = peer_cdr->disposition;
2859 ast_cdr_answer(bridge_cdr);
2861 ast_cdr_answer(chan_cdr); /* for the sake of cli status checks */
2864 if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
2866 ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
2869 ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
2873 ast_cel_report_event(chan, AST_CEL_BRIDGE_START, NULL, NULL, NULL);
2875 struct ast_channel *other; /* used later */
2877 res = ast_channel_bridge(chan, peer, config, &f, &who);
2879 /* When frame is not set, we are probably involved in a situation
2880 where we've timed out.
2881 When frame is set, we'll come this code twice; once for DTMF_BEGIN
2882 and also for DTMF_END. If we flow into the following 'if' for both, then
2883 our wait times are cut in half, as both will subtract from the
2884 feature_timer. Not good!
2886 if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
2887 /* Update feature timer for next pass */
2888 diff = ast_tvdiff_ms(ast_tvnow(), config->feature_start_time);
2889 if (res == AST_BRIDGE_RETRY) {
2890 /* The feature fully timed out but has not been updated. Skip
2891 * the potential round error from the diff calculation and
2892 * explicitly set to expired. */
2893 config->feature_timer = -1;
2895 config->feature_timer -= diff;
2899 if (config->feature_timer <= 0) {
2900 /* Not *really* out of time, just out of time for
2901 digits to come in for features. */
2902 ast_debug(1, "Timed out for feature!\n");
2903 if (!ast_strlen_zero(peer_featurecode)) {
2904 ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
2905 memset(peer_featurecode, 0, sizeof(peer_featurecode));
2907 if (!ast_strlen_zero(chan_featurecode)) {
2908 ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
2909 memset(chan_featurecode, 0, sizeof(chan_featurecode));
2913 hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
2915 /* No more digits expected - reset the timer */
2916 config->feature_timer = 0;
2918 hadfeatures = hasfeatures;
2919 /* Continue as we were */
2922 /* The bridge returned without a frame and there is a feature in progress.
2923 * However, we don't think the feature has quite yet timed out, so just
2924 * go back into the bridge. */
2928 if (config->feature_timer <=0) {
2929 /* We ran out of time */
2930 config->feature_timer = 0;
2940 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer)) {
2941 ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
2946 if (!f || (f->frametype == AST_FRAME_CONTROL &&
2947 (f->subclass.integer == AST_CONTROL_HANGUP || f->subclass.integer == AST_CONTROL_BUSY ||
2948 f->subclass.integer == AST_CONTROL_CONGESTION))) {
2952 /* many things should be sent to the 'other' channel */
2953 other = (who == chan) ? peer : chan;
2954 if (f->frametype == AST_FRAME_CONTROL) {
2955 switch (f->subclass.integer) {
2956 case AST_CONTROL_RINGING:
2957 case AST_CONTROL_FLASH:
2959 ast_indicate(other, f->subclass.integer);
2961 case AST_CONTROL_CONNECTED_LINE:
2962 if (!ast_channel_connected_line_macro(who, other, f, who != chan, 1)) {
2965 /* The implied "else" falls through purposely */
2966 case AST_CONTROL_HOLD:
2967 case AST_CONTROL_UNHOLD:
2968 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
2970 case AST_CONTROL_OPTION:
2972 /* Forward option Requests */
2973 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
2974 ast_channel_setoption(other, ntohs(aoh->option), aoh->data,
2975 f->datalen - sizeof(struct ast_option_header), 0);
2979 } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
2981 } else if (f->frametype == AST_FRAME_DTMF) {
2985 hadfeatures = hasfeatures;
2986 /* This cannot overrun because the longest feature is one shorter than our buffer */
2988 sense = FEATURE_SENSE_