2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2012, Digium, Inc.
5 * Copyright (C) 2012, Russell Bryant
7 * Mark Spencer <markster@digium.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Routines implementing call features as call pickup, parking and transfer
24 * \author Mark Spencer <markster@digium.com>
27 /*! \li \ref features.c uses the configuration file \ref features.conf
28 * \addtogroup configuration_file Configuration Files
32 * \page features.conf features.conf
33 * \verbinclude features.conf.sample
37 <support_level>core</support_level>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/_private.h"
49 #include <sys/signal.h>
50 #include <netinet/in.h>
52 #include "asterisk/lock.h"
53 #include "asterisk/file.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/pbx.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/module.h"
58 #include "asterisk/translate.h"
59 #include "asterisk/app.h"
60 #include "asterisk/say.h"
61 #include "asterisk/features.h"
62 #include "asterisk/musiconhold.h"
63 #include "asterisk/config.h"
64 #include "asterisk/cli.h"
65 #include "asterisk/manager.h"
66 #include "asterisk/utils.h"
67 #include "asterisk/adsi.h"
68 #include "asterisk/devicestate.h"
69 #include "asterisk/monitor.h"
70 #include "asterisk/audiohook.h"
71 #include "asterisk/global_datastores.h"
72 #include "asterisk/astobj2.h"
73 #include "asterisk/cel.h"
74 #include "asterisk/test.h"
75 #include "asterisk/bridging.h"
76 #include "asterisk/bridging_basic.h"
77 #include "asterisk/features_config.h"
79 /* BUGBUG TEST_FRAMEWORK is disabled because parking tests no longer work. */
83 * Party A - transferee
84 * Party B - transferer
85 * Party C - target of transfer
87 * DTMF attended transfer works within the channel bridge.
88 * Unfortunately, when either party A or B in the channel bridge
89 * hangs up, that channel is not completely hung up until the
90 * transfer completes. This is a real problem depending upon
91 * the channel technology involved.
93 * For chan_dahdi, the channel is crippled until the hangup is
94 * complete. Either the channel is not useable (analog) or the
95 * protocol disconnect messages are held up (PRI/BRI/SS7) and
96 * the media is not released.
98 * For chan_sip, a call limit of one is going to block that
99 * endpoint from any further calls until the hangup is complete.
101 * For party A this is a minor problem. The party A channel
102 * will only be in this condition while party B is dialing and
103 * when party B and C are conferring. The conversation between
104 * party B and C is expected to be a short one. Party B is
105 * either asking a question of party C or announcing party A.
106 * Also party A does not have much incentive to hangup at this
109 * For party B this can be a major problem during a blonde
110 * transfer. (A blonde transfer is our term for an attended
111 * transfer that is converted into a blind transfer. :)) Party
112 * B could be the operator. When party B hangs up, he assumes
113 * that he is out of the original call entirely. The party B
114 * channel will be in this condition while party C is ringing,
115 * while attempting to recall party B, and while waiting between
119 * The ATXFER_NULL_TECH conditional is a hack to fix the
120 * problem. It will replace the party B channel technology with
121 * a NULL channel driver. The consequences of this code is that
122 * the 'h' extension will not be able to access any channel
123 * technology specific information like SIP statistics for the
126 * Uncomment the ATXFER_NULL_TECH define below to replace the
127 * party B channel technology in the channel bridge to complete
128 * hanging up the channel technology.
130 //#define ATXFER_NULL_TECH 1
133 <application name="Bridge" language="en_US">
138 <parameter name="channel" required="true">
139 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
141 <parameter name="options">
144 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
146 <option name="F" argsep="^">
147 <argument name="context" required="false" />
148 <argument name="exten" required="false" />
149 <argument name="priority" required="true" />
150 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
151 to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
153 <para>Any channel variables you want the called channel to inherit from the caller channel must be
154 prefixed with one or two underbars ('_').</para>
157 <para>This option will override the 'x' option</para>
161 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
162 to the next priority of the current extension and <emphasis>start</emphasis> execution
163 at that location.</para>
165 <para>Any channel variables you want the called channel to inherit from the caller channel must be
166 prefixed with one or two underbars ('_').</para>
169 <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
172 <para>This option will override the 'x' option</para>
177 <para>Allow the called party to hang up by sending the
178 <replaceable>*</replaceable> DTMF digit.</para>
181 <para>Allow the calling party to hang up by pressing the
182 <replaceable>*</replaceable> DTMF digit.</para>
185 <para>Allow the called party to enable parking of the call by sending
186 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
189 <para>Allow the calling party to enable parking of the call by sending
190 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
192 <option name="L(x[:y][:z])">
193 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
194 when <replaceable>y</replaceable> ms are left. Repeat the warning every
195 <replaceable>z</replaceable> ms. The following special variables can be
196 used with this option:</para>
198 <variable name="LIMIT_PLAYAUDIO_CALLER">
199 <para>Play sounds to the caller. yes|no (default yes)</para>
201 <variable name="LIMIT_PLAYAUDIO_CALLEE">
202 <para>Play sounds to the callee. yes|no</para>
204 <variable name="LIMIT_TIMEOUT_FILE">
205 <para>File to play when time is up.</para>
207 <variable name="LIMIT_CONNECT_FILE">
208 <para>File to play when call begins.</para>
210 <variable name="LIMIT_WARNING_FILE">
211 <para>File to play as warning if <replaceable>y</replaceable> is
212 defined. The default is to say the time remaining.</para>
217 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
220 <para>Allow the called party to transfer the calling party by sending the
221 DTMF sequence defined in <filename>features.conf</filename>.</para>
224 <para>Allow the calling party to transfer the called party by sending the
225 DTMF sequence defined in <filename>features.conf</filename>.</para>
228 <para>Allow the called party to enable recording of the call by sending
229 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
232 <para>Allow the calling party to enable recording of the call by sending
233 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
236 <para>Cause the called party to be hung up after the bridge, instead of being
237 restarted in the dialplan.</para>
243 <para>Allows the ability to bridge two channels via the dialplan.</para>
244 <para>This application sets the following channel variable upon completion:</para>
246 <variable name="BRIDGERESULT">
247 <para>The result of the bridge attempt as a text string.</para>
248 <value name="SUCCESS" />
249 <value name="FAILURE" />
250 <value name="LOOP" />
251 <value name="NONEXISTENT" />
252 <value name="INCOMPATIBLE" />
257 <manager name="Park" language="en_US">
262 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
263 <parameter name="Channel" required="true">
264 <para>Channel name to park.</para>
266 <parameter name="Channel2" required="true">
267 <para>Channel to return to if timeout.</para>
269 <parameter name="Timeout">
270 <para>Number of milliseconds to wait before callback.</para>
272 <parameter name="Parkinglot">
273 <para>Specify in which parking lot to park the channel.</para>
277 <para>Park a channel.</para>
280 <manager name="Bridge" language="en_US">
282 Bridge two channels already in the PBX.
285 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
286 <parameter name="Channel1" required="true">
287 <para>Channel to Bridge to Channel2.</para>
289 <parameter name="Channel2" required="true">
290 <para>Channel to Bridge to Channel1.</para>
292 <parameter name="Tone">
293 <para>Play courtesy tone to Channel 2.</para>
296 <enum name="Channel1" />
297 <enum name="Channel2" />
303 <para>Bridge together two channels already in the PBX.</para>
306 <managerEvent language="en_US" name="ParkedCallTimeOut">
307 <managerEventInstance class="EVENT_FLAG_CALL">
308 <synopsis>Raised when a parked call times out.</synopsis>
310 <parameter name="Exten">
311 <para>The parking lot extension.</para>
313 <parameter name="Channel"/>
314 <parameter name="Parkinglot">
315 <para>The name of the parking lot.</para>
317 <parameter name="CallerIDNum"/>
318 <parameter name="CallerIDName"/>
319 <parameter name="ConnectedLineNum"/>
320 <parameter name="ConnectedLineName"/>
321 <parameter name="UniqueID"/>
324 <ref type="managerEvent">ParkedCall</ref>
326 </managerEventInstance>
328 <managerEvent language="en_US" name="ParkedCallGiveUp">
329 <managerEventInstance class="EVENT_FLAG_CALL">
330 <synopsis>Raised when a parked call hangs up while in the parking lot.</synopsis>
332 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCallTimeOut']/managerEventInstance/syntax/parameter[@name='Exten'])" />
333 <parameter name="Channel"/>
334 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCallTimeOut']/managerEventInstance/syntax/parameter[@name='Parkinglot'])" />
335 <parameter name="CallerIDNum"/>
336 <parameter name="CallerIDName"/>
337 <parameter name="ConnectedLineNum"/>
338 <parameter name="ConnectedLineName"/>
339 <parameter name="UniqueID"/>
342 <ref type="managerEvent">ParkedCall</ref>
344 </managerEventInstance>
348 #define DEFAULT_PARK_TIME 45000 /*!< ms */
349 #define DEFAULT_PARK_EXTENSION "700"
350 #define DEFAULT_COMEBACK_CONTEXT "parkedcallstimeout"
351 #define DEFAULT_COMEBACK_TO_ORIGIN 1
352 #define DEFAULT_COMEBACK_DIAL_TIME 30
354 #define AST_MAX_WATCHERS 256
355 #define MAX_DIAL_FEATURE_OPTIONS 30
357 /* TODO Scrape all of the parking stuff out of features.c */
360 FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
361 FEATURE_INTERPRET_DO, /* Used by feature_interpret */
362 FEATURE_INTERPRET_CHECK, /* Used by feature_check */
363 } feature_interpret_op;
365 /*! Parking lot access ramp dialplan usage entry. */
366 struct parking_dp_ramp {
367 /*! Next node in the parking lot spaces dialplan list. */
368 AST_LIST_ENTRY(parking_dp_ramp) node;
369 /*! TRUE if the parking lot access extension is exclusive. */
370 unsigned int exclusive:1;
371 /*! Parking lot access extension */
375 /*! Parking lot dialplan access ramp map */
376 AST_LIST_HEAD_NOLOCK(parking_dp_ramp_map, parking_dp_ramp);
378 /*! Parking lot spaces dialplan usage entry. */
379 struct parking_dp_spaces {
380 /*! Next node in the parking lot spaces dialplan list. */
381 AST_LIST_ENTRY(parking_dp_spaces) node;
382 /*! First parking space */
384 /*! Last parking space */
388 /*! Parking lot dialplan context space map */
389 AST_LIST_HEAD_NOLOCK(parking_dp_space_map, parking_dp_spaces);
391 /*! Parking lot context dialplan usage entry. */
392 struct parking_dp_context {
393 /*! Next node in the parking lot contexts dialplan list. */
394 AST_LIST_ENTRY(parking_dp_context) node;
395 /*! Parking access extensions defined in this context. */
396 struct parking_dp_ramp_map access_extens;
397 /*! Parking spaces defined in this context. */
398 struct parking_dp_space_map spaces;
399 /*! Parking hints defined in this context. */
400 struct parking_dp_space_map hints;
401 /*! Parking lot context name */
405 /*! Parking lot dialplan usage map. */
406 AST_LIST_HEAD_NOLOCK(parking_dp_map, parking_dp_context);
409 * \brief Description of one parked call, added to a list while active, then removed.
410 * The list belongs to a parkinglot.
413 struct ast_channel *chan; /*!< Parked channel */
414 struct timeval start; /*!< Time the park started */
415 int parkingnum; /*!< Parking lot space used */
416 char parkingexten[AST_MAX_EXTENSION]; /*!< If set beforehand, parking extension used for this call */
417 char context[AST_MAX_CONTEXT]; /*!< Where to go if our parking time expires */
418 char exten[AST_MAX_EXTENSION];
420 unsigned int parkingtime; /*!< Maximum length in parking lot before return */
421 /*! Method to entertain the caller when parked: AST_CONTROL_RINGING, AST_CONTROL_HOLD, or 0(none) */
422 enum ast_control_frame_type hold_method;
423 unsigned int notquiteyet:1;
424 unsigned int options_specified:1;
425 char peername[AST_CHANNEL_NAME];
426 unsigned char moh_trys;
427 /*! Parking lot this entry belongs to. Holds a parking lot reference. */
428 struct ast_parkinglot *parkinglot;
429 AST_LIST_ENTRY(parkeduser) list;
432 /*! Parking lot configuration options. */
433 struct parkinglot_cfg {
434 /*! Music class used for parking */
435 char mohclass[MAX_MUSICCLASS];
436 /*! Extension to park calls in this parking lot. */
437 char parkext[AST_MAX_EXTENSION];
438 /*! Context for which parking is made accessible */
439 char parking_con[AST_MAX_CONTEXT];
440 /*! Context that timed-out parked calls are called back on when comebacktoorigin=no */
441 char comebackcontext[AST_MAX_CONTEXT];
442 /*! First available extension for parking */
444 /*! Last available extension for parking */
446 /*! Default parking time in ms. */
447 unsigned int parkingtime;
449 * \brief Enable DTMF based transfers on bridge when picking up parked calls.
453 * AST_FEATURE_FLAG_BYCALLEE
454 * AST_FEATURE_FLAG_BYCALLER
455 * AST_FEATURE_FLAG_BYBOTH
457 int parkedcalltransfers;
459 * \brief Enable DTMF based parking on bridge when picking up parked calls.
463 * AST_FEATURE_FLAG_BYCALLEE
464 * AST_FEATURE_FLAG_BYCALLER
465 * AST_FEATURE_FLAG_BYBOTH
467 int parkedcallreparking;
469 * \brief Enable DTMF based hangup on a bridge when pickup up parked calls.
473 * AST_FEATURE_FLAG_BYCALLEE
474 * AST_FEATURE_FLAG_BYCALLER
475 * AST_FEATURE_FLAG_BYBOTH
477 int parkedcallhangup;
479 * \brief Enable DTMF based recording on a bridge when picking up parked calls.
483 * AST_FEATURE_FLAG_BYCALLEE
484 * AST_FEATURE_FLAG_BYCALLER
485 * AST_FEATURE_FLAG_BYBOTH
487 int parkedcallrecording;
489 /*! Time in seconds to dial the device that parked a timedout parked call */
490 unsigned int comebackdialtime;
491 /*! TRUE if findslot is set to next */
492 unsigned int parkfindnext:1;
493 /*! TRUE if the parking lot is exclusively accessed by parkext */
494 unsigned int parkext_exclusive:1;
495 /*! Add parking hints automatically */
496 unsigned int parkaddhints:1;
497 /*! TRUE if configuration is invalid and the parking lot should not be used. */
498 unsigned int is_invalid:1;
499 /*! TRUE if a timed out parked call goes back to the parker */
500 unsigned int comebacktoorigin:1;
503 /*! \brief Structure for parking lots which are put in a container. */
504 struct ast_parkinglot {
505 /*! Name of the parking lot. */
506 char name[AST_MAX_CONTEXT];
507 /*! Parking lot user configuration. */
508 struct parkinglot_cfg cfg;
510 /*! Parking space to start next park search. */
511 int next_parking_space;
513 /*! That which bears the_mark shall be deleted if parking lot empty! (Used during reloads.) */
514 unsigned int the_mark:1;
515 /*! TRUE if the parking lot is disabled. */
516 unsigned int disabled:1;
518 /*! List of active parkings in this parkinglot */
519 AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
522 /*! \brief The configured parking lots container. Always at least one - the default parking lot */
523 static struct ao2_container *parkinglots;
526 * \brief Default parking lot.
527 * \note Holds a parkinglot reference.
528 * \note Will not be NULL while running.
530 static struct ast_parkinglot *default_parkinglot;
532 /*! Force a config reload to reload regardless of config file timestamp. */
533 #ifdef TEST_FRAMEWORK
534 static int force_reload_load;
537 static int parkeddynamic = 0; /*!< Enable creation of parkinglots dynamically */
540 * \brief Context for parking dialback to parker.
541 * \note The need for the context is a KLUDGE.
543 * \todo Might be able to eliminate the parking_con_dial context
544 * kludge by running app_dial directly in its own thread to
547 static char parking_con_dial[] = "park-dial";
549 /*! Ensure that features.conf reloads on one thread at a time. */
550 AST_MUTEX_DEFINE_STATIC(features_reload_lock);
554 static char *registrar = "features"; /*!< Registrar for operations */
556 /*! PARK_APP_NAME application arguments */
557 AST_DEFINE_APP_ARGS_TYPE(park_app_args,
558 AST_APP_ARG(timeout); /*!< Time in ms to remain in the parking lot. */
559 AST_APP_ARG(return_con); /*!< Context to return parked call if timeout. */
560 AST_APP_ARG(return_ext); /*!< Exten to return parked call if timeout. */
561 AST_APP_ARG(return_pri); /*!< Priority to return parked call if timeout. */
562 AST_APP_ARG(options); /*!< Parking option flags. */
563 AST_APP_ARG(pl_name); /*!< Parking lot name to use if present. */
564 AST_APP_ARG(dummy); /*!< Place to put any remaining args string. */
567 /* module and CLI command definitions */
568 static const char *parkcall = "Park";
570 static pthread_t parking_thread;
571 struct ast_dial_features {
572 /*! Channel's feature flags. */
573 struct ast_flags my_features;
574 /*! Bridge peer's feature flags. */
575 struct ast_flags peer_features;
578 #if defined(ATXFER_NULL_TECH)
581 * \brief Set the channel technology to the kill technology.
583 * \param chan Channel to change technology.
587 static void set_kill_chan_tech(struct ast_channel *chan)
591 ast_channel_lock(chan);
593 /* Hangup the channel's physical side */
594 if (ast_channel_tech(chan)->hangup) {
595 ast_channel_tech(chan)->hangup(chan);
597 if (ast_channel_tech_pvt(chan)) {
598 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
599 ast_channel_name(chan));
600 ast_free(ast_channel_tech_pvt(chan));
601 ast_channel_tech_pvt_set(chan, NULL);
604 /* Install the kill technology and wake up anyone waiting on it. */
605 ast_channel_tech_set(chan, &ast_kill_tech);
606 for (idx = 0; idx < AST_MAX_FDS; ++idx) {
610 case AST_GENERATOR_FD:
611 /* Don't clear these fd's. */
614 ast_channel_set_fd(chan, idx, -1);
618 ast_queue_frame(chan, &ast_null_frame);
620 ast_channel_unlock(chan);
622 #endif /* defined(ATXFER_NULL_TECH) */
624 #if defined(ATXFER_NULL_TECH)
627 * \brief Set the channel name to something unique.
629 * \param chan Channel to change name.
633 static void set_new_chan_name(struct ast_channel *chan)
635 static int seq_num_last;
641 /* Create the new channel name string. */
642 ast_channel_lock(chan);
643 seq_num = ast_atomic_fetchadd_int(&seq_num_last, +1);
644 len = snprintf(dummy, sizeof(dummy), "%s<XFER_%x>", ast_channel_name(chan), seq_num) + 1;
645 chan_name = ast_alloca(len);
646 snprintf(chan_name, len, "%s<XFER_%x>", ast_channel_name(chan), seq_num);
647 ast_channel_unlock(chan);
649 ast_change_name(chan, chan_name);
651 #endif /* defined(ATXFER_NULL_TECH) */
653 static void *dial_features_duplicate(void *data)
655 struct ast_dial_features *df = data, *df_copy;
657 if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
661 memcpy(df_copy, df, sizeof(*df));
666 static const struct ast_datastore_info dial_features_info = {
667 .type = "dial-features",
668 .destroy = ast_free_ptr,
669 .duplicate = dial_features_duplicate,
674 * \brief Set the features datastore if it doesn't exist.
676 * \param chan Channel to add features datastore
677 * \param my_features The channel's feature flags
678 * \param peer_features The channel's bridge peer feature flags
680 * \retval TRUE if features datastore already existed.
682 static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
684 struct ast_datastore *datastore;
685 struct ast_dial_features *dialfeatures;
687 ast_channel_lock(chan);
688 datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
689 ast_channel_unlock(chan);
691 /* Already exists. */
695 /* Create a new datastore with specified feature flags. */
696 datastore = ast_datastore_alloc(&dial_features_info, NULL);
698 ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
701 dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
703 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
704 ast_datastore_free(datastore);
707 ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
708 ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
709 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
710 datastore->data = dialfeatures;
711 ast_channel_lock(chan);
712 ast_channel_datastore_add(chan, datastore);
713 ast_channel_unlock(chan);
717 /* Forward declarations */
718 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
719 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
720 static struct ast_parkinglot *find_parkinglot(const char *name);
721 static struct ast_parkinglot *create_parkinglot(const char *name);
722 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
723 static int parkinglot_activate(struct ast_parkinglot *parkinglot);
724 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile);
728 * \brief Get the parking extension if it exists.
730 * \param exten_str Parking extension to see if exists.
731 * \param chan Channel to autoservice while looking for exten. (Could be NULL)
732 * \param context Parking context to look in for exten.
734 * \retval exten on success.
735 * \retval NULL on error or exten does not exist.
737 static struct ast_exten *get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
739 struct ast_exten *exten;
740 struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
741 const char *app_at_exten;
743 ast_debug(4, "Checking if %s@%s is a parking exten\n", exten_str, context);
744 exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
750 app_at_exten = ast_get_extension_app(exten);
751 if (!app_at_exten || strcasecmp(parkcall, app_at_exten)) {
758 int ast_parking_ext_valid(const char *exten_str, struct ast_channel *chan, const char *context)
760 return get_parking_exten(exten_str, chan, context) ? 1 : 0;
763 struct ast_bridge_thread_obj
765 struct ast_bridge_config bconfig;
766 struct ast_channel *chan;
767 struct ast_channel *peer;
768 struct ast_callid *callid; /*<! callid pointer (Only used to bind thread) */
769 unsigned int return_to_pbx:1;
772 static int parkinglot_hash_cb(const void *obj, const int flags)
774 const struct ast_parkinglot *parkinglot = obj;
776 return ast_str_case_hash(parkinglot->name);
779 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
781 struct ast_parkinglot *parkinglot = obj;
782 struct ast_parkinglot *parkinglot2 = arg;
784 return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
788 * \brief store context, extension and priority
789 * \param chan, context, ext, pri
791 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
793 ast_channel_context_set(chan, context);
794 ast_channel_exten_set(chan, ext);
795 ast_channel_priority_set(chan, pri);
799 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
800 const char *caller_name, struct ast_channel *requestor,
801 struct ast_channel *transferee, const char *type, struct ast_format_cap *cap, const char *addr,
802 int timeout, int *outstate, const char *language);
805 static const struct ast_datastore_info channel_app_data_datastore = {
806 .type = "Channel appdata datastore",
807 .destroy = ast_free_ptr,
811 static int set_chan_app_data(struct ast_channel *chan, const char *src_app_data)
813 struct ast_datastore *datastore;
816 datastore = ast_datastore_alloc(&channel_app_data_datastore, NULL);
821 dst_app_data = ast_malloc(strlen(src_app_data) + 1);
823 ast_datastore_free(datastore);
827 ast_channel_data_set(chan, strcpy(dst_app_data, src_app_data));
828 datastore->data = dst_app_data;
829 ast_channel_datastore_add(chan, datastore);
836 * \brief bridge the call
837 * \param data thread bridge.
839 * Set Last Data for respective channels, reset cdr for channels
840 * bridge call, check if we're going back to dialplan
841 * if not hangup both legs of the call
843 static void *bridge_call_thread(void *data)
845 struct ast_bridge_thread_obj *tobj = data;
848 ast_callid_threadassoc_add(tobj->callid);
849 /* Need to deref and set to null since ast_bridge_thread_obj has no common destructor */
850 tobj->callid = ast_callid_unref(tobj->callid);
853 ast_channel_appl_set(tobj->chan, !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge");
854 if (set_chan_app_data(tobj->chan, ast_channel_name(tobj->peer))) {
855 ast_channel_data_set(tobj->chan, "(Empty)");
857 ast_channel_appl_set(tobj->peer, !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge");
858 if (set_chan_app_data(tobj->peer, ast_channel_name(tobj->chan))) {
859 ast_channel_data_set(tobj->peer, "(Empty)");
862 if (tobj->return_to_pbx) {
863 ast_after_bridge_set_goto(tobj->chan, ast_channel_context(tobj->chan),
864 ast_channel_exten(tobj->chan), ast_channel_priority(tobj->chan));
865 ast_after_bridge_set_goto(tobj->peer, ast_channel_context(tobj->peer),
866 ast_channel_exten(tobj->peer), ast_channel_priority(tobj->peer));
869 ast_bridge_call(tobj->chan, tobj->peer, &tobj->bconfig);
871 ast_after_bridge_goto_run(tobj->chan);
881 * \brief create thread for the bridging call
884 static void bridge_call_thread_launch(struct ast_bridge_thread_obj *tobj)
888 /* This needs to be unreffed once it has been associated with the new thread. */
889 tobj->callid = ast_read_threadstorage_callid();
891 if (ast_pthread_create_detached(&thread, NULL, bridge_call_thread, tobj)) {
892 ast_log(LOG_ERROR, "Failed to create bridge_call_thread.\n");
893 ast_callid_unref(tobj->callid);
894 ast_hangup(tobj->chan);
895 ast_hangup(tobj->peer);
902 * \brief Announce call parking by ADSI
904 * \param parkingexten .
905 * Create message to show for ADSI, display message.
906 * \retval 0 on success.
907 * \retval -1 on failure.
909 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
912 int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
914 char *message[5] = {NULL, NULL, NULL, NULL, NULL};
916 snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
918 res = ast_adsi_load_session(chan, NULL, 0, 1);
921 return ast_adsi_print(chan, message, justify, 1);
925 * \brief Find parking lot name from channel
926 * \note Channel needs to be locked while the returned string is in use.
928 static const char *findparkinglotname(struct ast_channel *chan)
932 /* The channel variable overrides everything */
933 name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
934 if (!name && !ast_strlen_zero(ast_channel_parkinglot(chan))) {
935 /* Use the channel's parking lot. */
936 name = ast_channel_parkinglot(chan);
941 /*! \brief Notify metermaids that we've changed an extension */
942 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
944 ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
945 exten, context, ast_devstate2str(state));
947 ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "park:%s@%s", exten, context);
950 /*! \brief metermaids callback from devicestate.c */
951 static enum ast_device_state metermaidstate(const char *data)
956 context = ast_strdupa(data);
958 exten = strsep(&context, "@");
960 return AST_DEVICE_INVALID;
962 ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
964 if (!ast_exists_extension(NULL, context, exten, 1, NULL))
965 return AST_DEVICE_NOT_INUSE;
967 return AST_DEVICE_INUSE;
970 /*! Options to pass to park_call_full */
971 enum ast_park_call_options {
972 /*! Provide ringing to the parked caller instead of music on hold */
973 AST_PARK_OPT_RINGING = (1 << 0),
974 /*! Randomly choose a parking spot for the caller instead of choosing
975 * the first one that is available. */
976 AST_PARK_OPT_RANDOMIZE = (1 << 1),
977 /*! Do not announce the parking number */
978 AST_PARK_OPT_SILENCE = (1 << 2),
981 /*! Optional additional parking options when parking a call. */
982 struct ast_park_call_args {
983 /*! How long to wait in the parking lot before the call gets sent back
984 * to the specified return extension (or a best guess at where it came
985 * from if not explicitly specified). */
987 /*! An output parameter to store the parking space where the parked caller
990 const char *orig_chan_name;
991 const char *return_con;
992 const char *return_ext;
995 /*! Parked user that has already obtained a parking space */
996 struct parkeduser *pu;
997 /*! \brief Parkinglot to be parked in */
998 struct ast_parkinglot *parkinglot;
1003 * \brief Create a dynamic parking lot.
1005 * \param name Dynamic parking lot name to create.
1006 * \param chan Channel to get dynamic parking lot parameters.
1008 * \retval parkinglot on success.
1009 * \retval NULL on error.
1011 static struct ast_parkinglot *create_dynamic_parkinglot(const char *name, struct ast_channel *chan)
1013 const char *dyn_context;
1014 const char *dyn_exten;
1015 const char *dyn_range;
1016 const char *template_name;
1017 struct ast_parkinglot *template_parkinglot = NULL;
1018 struct ast_parkinglot *parkinglot;
1022 ast_channel_lock(chan);
1023 template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
1024 dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
1025 dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
1026 dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
1027 ast_channel_unlock(chan);
1029 if (!ast_strlen_zero(template_name)) {
1030 template_parkinglot = find_parkinglot(template_name);
1031 if (!template_parkinglot) {
1032 ast_debug(1, "PARKINGDYNAMIC lot %s does not exist.\n",
1034 } else if (template_parkinglot->cfg.is_invalid) {
1035 ast_debug(1, "PARKINGDYNAMIC lot %s has invalid config.\n",
1037 parkinglot_unref(template_parkinglot);
1038 template_parkinglot = NULL;
1041 if (!template_parkinglot) {
1042 template_parkinglot = parkinglot_addref(default_parkinglot);
1043 ast_debug(1, "Using default parking lot for template\n");
1046 parkinglot = copy_parkinglot(name, template_parkinglot);
1048 ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
1050 /* Configure the dynamic parking lot. */
1051 if (!ast_strlen_zero(dyn_context)) {
1052 ast_copy_string(parkinglot->cfg.parking_con, dyn_context,
1053 sizeof(parkinglot->cfg.parking_con));
1055 if (!ast_strlen_zero(dyn_exten)) {
1056 ast_copy_string(parkinglot->cfg.parkext, dyn_exten,
1057 sizeof(parkinglot->cfg.parkext));
1059 if (!ast_strlen_zero(dyn_range)) {
1060 if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
1061 ast_log(LOG_WARNING,
1062 "Format for parking positions is a-b, where a and b are numbers\n");
1063 } else if (dyn_end < dyn_start || dyn_start <= 0 || dyn_end <= 0) {
1064 ast_log(LOG_WARNING,
1065 "Format for parking positions is a-b, where a <= b\n");
1067 parkinglot->cfg.parking_start = dyn_start;
1068 parkinglot->cfg.parking_stop = dyn_end;
1073 * Sanity check for dynamic parking lot configuration.
1075 * XXX It may be desirable to instead check if the dynamic
1076 * parking lot overlaps any existing lots like what is done for
1079 if (!strcmp(parkinglot->cfg.parking_con, template_parkinglot->cfg.parking_con)) {
1080 if (!strcmp(parkinglot->cfg.parkext, template_parkinglot->cfg.parkext)
1081 && parkinglot->cfg.parkext_exclusive) {
1082 ast_log(LOG_WARNING,
1083 "Parking lot '%s' conflicts with template parking lot '%s'!\n"
1084 "Change either PARKINGDYNCONTEXT or PARKINGDYNEXTEN.\n",
1085 parkinglot->name, template_parkinglot->name);
1087 if ((template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_start
1088 && parkinglot->cfg.parking_start <= template_parkinglot->cfg.parking_stop)
1089 || (template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_stop
1090 && parkinglot->cfg.parking_stop <= template_parkinglot->cfg.parking_stop)
1091 || (parkinglot->cfg.parking_start < template_parkinglot->cfg.parking_start
1092 && template_parkinglot->cfg.parking_stop < parkinglot->cfg.parking_stop)) {
1093 ast_log(LOG_WARNING,
1094 "Parking lot '%s' parking spaces overlap template parking lot '%s'!\n"
1095 "Change PARKINGDYNPOS.\n",
1096 parkinglot->name, template_parkinglot->name);
1100 parkinglot_activate(parkinglot);
1101 ao2_link(parkinglots, parkinglot);
1103 parkinglot_unref(template_parkinglot);
1110 * \brief Abort parking a call that has not completed parking yet.
1112 * \param pu Parked user item to clean up.
1114 * \note The parking lot parkings list is locked on entry.
1118 static void park_space_abort(struct parkeduser *pu)
1120 struct ast_parkinglot *parkinglot;
1122 parkinglot = pu->parkinglot;
1124 /* Put back the parking space just allocated. */
1125 --parkinglot->next_parking_space;
1127 AST_LIST_REMOVE(&parkinglot->parkings, pu, list);
1129 AST_LIST_UNLOCK(&parkinglot->parkings);
1130 parkinglot_unref(parkinglot);
1136 * \brief Reserve a parking space in a parking lot for a call being parked.
1138 * \param park_me Channel being parked.
1139 * \param parker Channel parking the call.
1140 * \param args Optional additional parking options when parking a call.
1142 * \return Parked call descriptor or NULL if failed.
1143 * \note The parking lot list is locked if successful.
1145 static struct parkeduser *park_space_reserve(struct ast_channel *park_me, struct ast_channel *parker, struct ast_park_call_args *args)
1147 struct parkeduser *pu;
1149 int parking_space = -1;
1150 const char *parkinglotname;
1151 const char *parkingexten;
1152 struct parkeduser *cur;
1153 struct ast_parkinglot *parkinglot = NULL;
1155 if (args->parkinglot) {
1156 parkinglot = parkinglot_addref(args->parkinglot);
1157 parkinglotname = parkinglot->name;
1160 parkinglotname = findparkinglotname(parker);
1161 } else { /* parker was NULL, check park_me (ParkAndAnnounce / res_agi) */
1162 parkinglotname = findparkinglotname(park_me);
1164 if (!ast_strlen_zero(parkinglotname)) {
1165 parkinglot = find_parkinglot(parkinglotname);
1167 /* Parking lot is not specified, so use the default parking lot. */
1168 ast_debug(4, "This could be an indication channel driver needs updating, using default lot.\n");
1169 parkinglot = parkinglot_addref(default_parkinglot);
1173 /* Dynamically create parkinglot */
1174 if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
1175 parkinglot = create_dynamic_parkinglot(parkinglotname, park_me);
1179 ast_log(LOG_WARNING, "Parking lot not available to park %s.\n", ast_channel_name(park_me));
1183 ast_debug(1, "Parking lot: %s\n", parkinglot->name);
1184 if (parkinglot->disabled || parkinglot->cfg.is_invalid) {
1185 ast_log(LOG_WARNING, "Parking lot %s is not in a useable state.\n",
1187 parkinglot_unref(parkinglot);
1191 /* Allocate memory for parking data */
1192 if (!(pu = ast_calloc(1, sizeof(*pu)))) {
1193 parkinglot_unref(parkinglot);
1197 /* Lock parking list */
1198 AST_LIST_LOCK(&parkinglot->parkings);
1200 /* Check for channel variable PARKINGEXTEN */
1201 parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(park_me, "PARKINGEXTEN"), ""));
1202 if (!ast_strlen_zero(parkingexten)) {
1204 * \note The API forces us to specify a numeric parking slot, even
1205 * though the architecture would tend to support non-numeric extensions
1206 * (as are possible with SIP, for example). Hence, we enforce that
1207 * limitation here. If extout was not numeric, we could permit
1208 * arbitrary non-numeric extensions.
1210 if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space <= 0) {
1211 ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n",
1213 AST_LIST_UNLOCK(&parkinglot->parkings);
1214 parkinglot_unref(parkinglot);
1219 if (parking_space < parkinglot->cfg.parking_start
1220 || parkinglot->cfg.parking_stop < parking_space) {
1222 * Cannot allow park because parking lots are not setup for
1223 * spaces outside of the lot. (Things like dialplan hints don't
1224 * exist for outside lot space.)
1226 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is not in %s (%d-%d).\n",
1227 parking_space, parkinglot->name, parkinglot->cfg.parking_start,
1228 parkinglot->cfg.parking_stop);
1229 AST_LIST_UNLOCK(&parkinglot->parkings);
1230 parkinglot_unref(parkinglot);
1235 /* Check if requested parking space is in use. */
1236 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
1237 if (cur->parkingnum == parking_space) {
1238 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is already in use in %s\n",
1239 parking_space, parkinglot->name);
1240 AST_LIST_UNLOCK(&parkinglot->parkings);
1241 parkinglot_unref(parkinglot);
1247 /* PARKINGEXTEN is empty, so find a usable extension in the lot to park the call */
1248 int start; /* The first slot we look in the parkinglot. It can be randomized. */
1249 int start_checked = 0; /* flag raised once the first slot is checked */
1251 /* If using randomize mode, set start to random position on parking range */
1252 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
1253 start = ast_random() % (parkinglot->cfg.parking_stop - parkinglot->cfg.parking_start + 1);
1254 start += parkinglot->cfg.parking_start;
1255 } else if (parkinglot->cfg.parkfindnext
1256 && parkinglot->cfg.parking_start <= parkinglot->next_parking_space
1257 && parkinglot->next_parking_space <= parkinglot->cfg.parking_stop) {
1258 /* Start looking with the next parking space in the lot. */
1259 start = parkinglot->next_parking_space;
1261 /* Otherwise, just set it to the start position. */
1262 start = parkinglot->cfg.parking_start;
1265 /* free parking extension linear search: O(n^2) */
1266 for (i = start; ; i++) {
1267 /* If we are past the end, wrap around to the first parking slot*/
1268 if (i == parkinglot->cfg.parking_stop + 1) {
1269 i = parkinglot->cfg.parking_start;
1273 /* At this point, if start_checked, we've exhausted all the possible slots. */
1274 if (start_checked) {
1281 /* Search the list of parked calls already in use for i. If we find it, it's in use. */
1282 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
1283 if (cur->parkingnum == i) {
1288 /* We found a parking space. */
1293 if (parking_space == -1) {
1294 /* We did not find a parking space. Lot is full. */
1295 ast_log(LOG_WARNING, "No more parking spaces in %s\n", parkinglot->name);
1296 AST_LIST_UNLOCK(&parkinglot->parkings);
1297 parkinglot_unref(parkinglot);
1303 /* Prepare for next parking space search. */
1304 parkinglot->next_parking_space = parking_space + 1;
1306 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
1307 pu->notquiteyet = 1;
1308 pu->parkingnum = parking_space;
1309 pu->parkinglot = parkinglot;
1310 AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
1316 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
1318 struct parkeduser *pu = args->pu;
1319 const char *event_from; /*!< Channel name that is parking the call. */
1320 char app_data[AST_MAX_EXTENSION + AST_MAX_CONTEXT];
1323 args->pu = pu = park_space_reserve(chan, peer, args);
1329 ast_channel_appl_set(chan, "Parked Call");
1330 ast_channel_data_set(chan, NULL);
1334 /* Put the parked channel on hold if we have two different channels */
1336 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
1337 pu->hold_method = AST_CONTROL_RINGING;
1338 ast_indicate(chan, AST_CONTROL_RINGING);
1340 pu->hold_method = AST_CONTROL_HOLD;
1341 ast_indicate_data(chan, AST_CONTROL_HOLD,
1342 S_OR(pu->parkinglot->cfg.mohclass, NULL),
1343 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
1347 pu->start = ast_tvnow();
1348 /* XXX This line was changed to not use get_parkingtime. This is just a placeholder message, because
1349 * likely this entire function is going away.
1351 pu->parkingtime = args->timeout;
1353 *(args->extout) = pu->parkingnum;
1356 event_from = S_OR(args->orig_chan_name, ast_channel_name(peer));
1359 * This is so ugly that it hurts, but implementing
1360 * get_base_channel() on local channels could have ugly side
1361 * effects. We could have
1362 * transferer<->local;1<->local;2<->parking and we need the
1363 * callback name to be that of transferer. Since local;1/2 have
1364 * the same name we can be tricky and just grab the bridged
1365 * channel from the other side of the local.
1367 if (!strcasecmp(ast_channel_tech(peer)->type, "Local")) {
1368 struct ast_channel *tmpchan, *base_peer;
1369 char other_side[AST_CHANNEL_NAME];
1372 ast_copy_string(other_side, event_from, sizeof(other_side));
1373 if ((c = strrchr(other_side, ';'))) {
1376 if ((tmpchan = ast_channel_get_by_name(other_side))) {
1377 ast_channel_lock(tmpchan);
1378 if ((base_peer = ast_bridged_channel(tmpchan))) {
1379 ast_copy_string(pu->peername, ast_channel_name(base_peer), sizeof(pu->peername));
1381 ast_channel_unlock(tmpchan);
1382 tmpchan = ast_channel_unref(tmpchan);
1385 ast_copy_string(pu->peername, event_from, sizeof(pu->peername));
1388 event_from = S_OR(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"),
1389 ast_channel_name(chan));
1393 * Remember what had been dialed, so that if the parking
1394 * expires, we try to come back to the same place
1396 pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
1399 * If extension has options specified, they override all other
1400 * possibilities such as the returntoorigin flag and transferred
1401 * context. Information on extension options is lost here, so
1404 ast_copy_string(pu->context,
1405 S_OR(args->return_con, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan))),
1406 sizeof(pu->context));
1407 ast_copy_string(pu->exten,
1408 S_OR(args->return_ext, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan))),
1410 pu->priority = args->return_pri ? args->return_pri :
1411 (ast_channel_macropriority(chan) ? ast_channel_macropriority(chan) : ast_channel_priority(chan));
1414 * If parking a channel directly, don't quite yet get parking
1415 * running on it. All parking lot entries are put into the
1416 * parking lot with notquiteyet on.
1419 pu->notquiteyet = 0;
1422 /* Wake up the (presumably select()ing) thread */
1423 pthread_kill(parking_thread, SIGURG);
1424 ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %u seconds\n",
1425 ast_channel_name(chan), pu->parkingnum, pu->parkinglot->name,
1426 pu->context, pu->exten, pu->priority, (pu->parkingtime / 1000));
1428 ast_cel_report_event(chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
1430 <managerEventInstance>
1431 <synopsis>Raised when a call has been parked.</synopsis>
1433 <parameter name="Exten">
1434 <para>The parking lot extension.</para>
1436 <parameter name="Parkinglot">
1437 <para>The name of the parking lot.</para>
1439 <parameter name="From">
1440 <para>The name of the channel that parked the call.</para>
1444 <ref type="application">Park</ref>
1445 <ref type="manager">Park</ref>
1446 <ref type="managerEvent">ParkedCallTimeOut</ref>
1447 <ref type="managerEvent">ParkedCallGiveUp</ref>
1449 </managerEventInstance>
1451 ast_manager_event(chan, EVENT_FLAG_CALL, "ParkedCall",
1454 "Parkinglot: %s\r\n"
1457 "CallerIDNum: %s\r\n"
1458 "CallerIDName: %s\r\n"
1459 "ConnectedLineNum: %s\r\n"
1460 "ConnectedLineName: %s\r\n"
1462 pu->parkingexten, ast_channel_name(chan), pu->parkinglot->name, event_from,
1463 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
1464 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
1465 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>"),
1466 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, "<unknown>"),
1467 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, "<unknown>"),
1468 ast_channel_uniqueid(chan)
1470 ast_debug(4, "peer: %s\n", peer ? ast_channel_name(peer) : "-No peer-");
1471 ast_debug(4, "args->orig_chan_name: %s\n", args->orig_chan_name ? args->orig_chan_name : "-none-");
1472 ast_debug(4, "pu->peername: %s\n", pu->peername);
1473 ast_debug(4, "AMI ParkedCall Channel: %s\n", ast_channel_name(chan));
1474 ast_debug(4, "AMI ParkedCall From: %s\n", event_from);
1476 if (peer && adsipark && ast_adsi_available(peer)) {
1477 adsi_announce_park(peer, pu->parkingexten); /* Only supports parking numbers */
1478 ast_adsi_unload_session(peer);
1481 snprintf(app_data, sizeof(app_data), "%s,%s", pu->parkingexten,
1482 pu->parkinglot->name);
1484 AST_LIST_UNLOCK(&pu->parkinglot->parkings);
1486 /* Only say number if it's a number and the channel hasn't been masqueraded away */
1487 if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE)
1488 && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(ast_channel_name(peer), args->orig_chan_name))) {
1490 * If a channel is masqueraded into peer while playing back the
1491 * parking space number do not continue playing it back. This
1492 * is the case if an attended transfer occurs.
1494 ast_set_flag(ast_channel_flags(peer), AST_FLAG_MASQ_NOSTREAM);
1495 /* Tell the peer channel the number of the parking space */
1496 ast_say_digits(peer, pu->parkingnum, "", ast_channel_language(peer));
1497 ast_clear_flag(ast_channel_flags(peer), AST_FLAG_MASQ_NOSTREAM);
1499 if (peer == chan) { /* pu->notquiteyet = 1 */
1500 /* Wake up parking thread if we're really done */
1501 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
1502 pu->hold_method = AST_CONTROL_RINGING;
1503 ast_indicate(chan, AST_CONTROL_RINGING);
1505 pu->hold_method = AST_CONTROL_HOLD;
1506 ast_indicate_data(chan, AST_CONTROL_HOLD,
1507 S_OR(pu->parkinglot->cfg.mohclass, NULL),
1508 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
1510 pu->notquiteyet = 0;
1511 pthread_kill(parking_thread, SIGURG);
1516 int ast_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
1520 const char *app_data;
1521 struct ast_exten *exten;
1522 struct park_app_args app_args;
1523 struct ast_park_call_args args = {
1528 if (!park_exten || !park_context) {
1529 return park_call_full(park_me, parker, &args);
1533 * Determiine if the specified park extension has an exclusive
1534 * parking lot to use.
1536 if (parker && parker != park_me) {
1537 ast_autoservice_start(park_me);
1539 exten = get_parking_exten(park_exten, parker, park_context);
1541 app_data = ast_get_extension_app_data(exten);
1545 parse = ast_strdupa(app_data);
1546 AST_STANDARD_APP_ARGS(app_args, parse);
1548 if (!ast_strlen_zero(app_args.pl_name)) {
1549 /* Find the specified exclusive parking lot */
1550 args.parkinglot = find_parkinglot(app_args.pl_name);
1551 if (!args.parkinglot && parkeddynamic) {
1552 args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
1556 if (parker && parker != park_me) {
1557 ast_autoservice_stop(park_me);
1560 res = park_call_full(park_me, parker, &args);
1561 if (args.parkinglot) {
1562 parkinglot_unref(args.parkinglot);
1567 int ast_park_call(struct ast_channel *park_me, struct ast_channel *parker, int timeout, const char *park_exten, int *extout)
1569 struct ast_park_call_args args = {
1574 return park_call_full(park_me, parker, &args);
1578 * \brief Park call via masqueraded channel and announce parking spot on peer channel.
1580 * \param rchan the real channel to be parked
1581 * \param peer the channel to have the parking read to.
1582 * \param args Additional parking options when parking a call.
1584 * \retval 0 on success.
1585 * \retval -1 on failure.
1587 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
1589 struct ast_channel *chan;
1591 /* Make a new, channel that we'll use to masquerade in the real one */
1592 chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, ast_channel_accountcode(rchan), ast_channel_exten(rchan),
1593 ast_channel_context(rchan), ast_channel_linkedid(rchan), ast_channel_amaflags(rchan), "Parked/%s", ast_channel_name(rchan));
1595 ast_log(LOG_WARNING, "Unable to create parked channel\n");
1596 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1597 if (peer == rchan) {
1598 /* Only have one channel to worry about. */
1599 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1601 /* Have two different channels to worry about. */
1602 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1608 args->pu = park_space_reserve(rchan, peer, args);
1611 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1612 if (peer == rchan) {
1613 /* Only have one channel to worry about. */
1614 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1616 /* Have two different channels to worry about. */
1617 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1623 /* Make formats okay */
1624 ast_format_copy(ast_channel_readformat(chan), ast_channel_readformat(rchan));
1625 ast_format_copy(ast_channel_writeformat(chan), ast_channel_writeformat(rchan));
1627 if (ast_channel_masquerade(chan, rchan)) {
1628 park_space_abort(args->pu);
1631 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1632 if (peer == rchan) {
1633 /* Only have one channel to worry about. */
1634 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1636 /* Have two different channels to worry about. */
1637 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1643 /* Setup the extensions and such */
1644 set_c_e_p(chan, ast_channel_context(rchan), ast_channel_exten(rchan), ast_channel_priority(rchan));
1646 /* Setup the macro extension and such */
1647 ast_channel_macrocontext_set(chan, ast_channel_macrocontext(rchan));
1648 ast_channel_macroexten_set(chan, ast_channel_macroexten(rchan));
1649 ast_channel_macropriority_set(chan, ast_channel_macropriority(rchan));
1651 /* Manually do the masquerade to make sure it is complete. */
1652 ast_do_masquerade(chan);
1654 if (peer == rchan) {
1658 /* parking space reserved, return code check unnecessary */
1659 park_call_full(chan, peer, args);
1664 int ast_masq_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
1668 const char *app_data;
1669 struct ast_exten *exten;
1670 struct park_app_args app_args;
1671 struct ast_park_call_args args = {
1677 args.orig_chan_name = ast_strdupa(ast_channel_name(parker));
1679 if (!park_exten || !park_context) {
1680 return masq_park_call(park_me, parker, &args);
1684 * Determiine if the specified park extension has an exclusive
1685 * parking lot to use.
1687 if (parker && parker != park_me) {
1688 ast_autoservice_start(park_me);
1690 exten = get_parking_exten(park_exten, parker, park_context);
1692 app_data = ast_get_extension_app_data(exten);
1696 parse = ast_strdupa(app_data);
1697 AST_STANDARD_APP_ARGS(app_args, parse);
1699 if (!ast_strlen_zero(app_args.pl_name)) {
1700 /* Find the specified exclusive parking lot */
1701 args.parkinglot = find_parkinglot(app_args.pl_name);
1702 if (!args.parkinglot && parkeddynamic) {
1703 args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
1707 if (parker && parker != park_me) {
1708 ast_autoservice_stop(park_me);
1711 res = masq_park_call(park_me, parker, &args);
1712 if (args.parkinglot) {
1713 parkinglot_unref(args.parkinglot);
1718 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1720 struct ast_park_call_args args = {
1726 args.orig_chan_name = ast_strdupa(ast_channel_name(peer));
1728 return masq_park_call(rchan, peer, &args);
1732 static int finishup(struct ast_channel *chan)
1734 ast_indicate(chan, AST_CONTROL_UNHOLD);
1736 return ast_autoservice_stop(chan);
1743 * \brief Builtin transfer park call helper.
1745 * \param park_me Channel to be parked.
1746 * \param parker Channel parking the call.
1747 * \param park_exten Parking lot dialplan access ramp extension.
1749 * \note Assumes park_me is on hold and in autoservice.
1751 * \retval -1 on successful park.
1752 * \retval -1 on park_me hangup.
1753 * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
1755 static int xfer_park_call_helper(struct ast_channel *park_me, struct ast_channel *parker, struct ast_exten *park_exten)
1758 const char *app_data;
1759 const char *pl_name;
1760 struct ast_park_call_args args = { 0, };
1761 struct park_app_args app_args;
1764 app_data = ast_get_extension_app_data(park_exten);
1768 parse = ast_strdupa(app_data);
1769 AST_STANDARD_APP_ARGS(app_args, parse);
1771 /* Find the parking lot */
1772 if (!ast_strlen_zero(app_args.pl_name)) {
1773 pl_name = app_args.pl_name;
1775 pl_name = findparkinglotname(parker);
1777 if (ast_strlen_zero(pl_name)) {
1778 /* Parking lot is not specified, so use the default parking lot. */
1779 args.parkinglot = parkinglot_addref(default_parkinglot);
1781 args.parkinglot = find_parkinglot(pl_name);
1782 if (!args.parkinglot && parkeddynamic) {
1783 args.parkinglot = create_dynamic_parkinglot(pl_name, park_me);
1787 if (args.parkinglot) {
1789 res = finishup(park_me);
1791 /* park_me hungup on us. */
1792 parkinglot_unref(args.parkinglot);
1795 res = masq_park_call(park_me, parker, &args);
1796 parkinglot_unref(args.parkinglot);
1798 /* Parking failed because parking lot does not exist. */
1799 if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
1800 ast_stream_and_wait(parker, "pbx-parkingfailed", "");
1806 return res ? AST_FEATURE_RETURN_SUCCESS : -1;
1812 * \brief set caller and callee according to the direction
1813 * \param caller, callee, peer, chan, sense
1815 * Detect who triggered feature and set callee/caller variables accordingly
1817 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
1818 struct ast_channel *peer, struct ast_channel *chan, int sense)
1820 if (sense == FEATURE_SENSE_PEER) {
1832 * \brief support routing for one touch call parking
1833 * \param chan channel parking call
1834 * \param peer channel to be parked
1835 * \param config unsed
1836 * \param code unused
1837 * \param sense feature options
1838 * \param data unused
1840 * \retval -1 on successful park.
1841 * \retval -1 on chan hangup.
1842 * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
1844 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1846 struct ast_channel *parker;
1847 struct ast_channel *parkee;
1848 struct ast_park_call_args args = { 0, };
1851 * We used to set chan's exten and priority to "s" and 1 here,
1852 * but this generates (in some cases) an invalid extension, and
1853 * if "s" exists, could errantly cause execution of extensions
1854 * you don't expect. It makes more sense to let nature take its
1855 * course when chan finishes, and let the pbx do its thing and
1856 * hang up when the park is over.
1859 /* Answer if call is not up */
1860 if (ast_channel_state(chan) != AST_STATE_UP) {
1862 * XXX Why are we doing this? Both of the channels should be up
1863 * since you cannot do DTMF features unless you are bridged.
1865 if (ast_answer(chan)) {
1869 /* Sleep to allow VoIP streams to settle down */
1870 if (ast_safe_sleep(chan, 1000)) {
1875 /* one direction used to call park_call.... */
1876 set_peers(&parker, &parkee, peer, chan, sense);
1877 return masq_park_call(parkee, parker, &args) ? AST_FEATURE_RETURN_SUCCESS : -1;
1883 * \brief Play file to specified channel.
1885 * \param play_to Channel to play audiofile to.
1886 * \param other Channel to put in autoservice while playing file.
1887 * \param msg Descriptive name of message type being played.
1888 * \param audiofile Audio file to play.
1890 * \retval 0 on success.
1891 * \retval -1 on error. (Couldn't play file, a channel hung up,...)
1893 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile)
1895 /* Put other channel in autoservice. */
1896 if (ast_autoservice_start(other)) {
1899 ast_autoservice_ignore(other, AST_FRAME_DTMF_BEGIN);
1900 ast_autoservice_ignore(other, AST_FRAME_DTMF_END);
1901 if (ast_stream_and_wait(play_to, audiofile, "")) {
1902 ast_log(LOG_WARNING, "Failed to play %s '%s'!\n", msg, audiofile);
1903 ast_autoservice_stop(other);
1906 if (ast_autoservice_stop(other)) {
1915 * \brief Play file to specified channels.
1917 * \param left Channel on left to play file.
1918 * \param right Channel on right to play file.
1919 * \param which Play file on indicated channels: which < 0 play left, which == 0 play both, which > 0 play right
1920 * \param msg Descriptive name of message type being played.
1921 * \param audiofile Audio file to play to channels.
1923 * \note Plays file to the indicated channels in turn so please
1924 * don't use this for very long messages.
1926 * \retval 0 on success.
1927 * \retval -1 on error. (Couldn't play file, channel hung up,...)
1929 static int play_message_to_chans(struct ast_channel *left, struct ast_channel *right, int which, const char *msg, const char *audiofile)
1931 /* First play the file to the left channel if requested. */
1932 if (which <= 0 && play_message_on_chan(left, right, msg, audiofile)) {
1936 /* Then play the file to the right channel if requested. */
1937 if (which >= 0 && play_message_on_chan(right, left, msg, audiofile)) {
1947 * \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
1948 * other channel during the message, so please don't use this for very long messages
1950 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
1952 return play_message_to_chans(caller_chan, callee_chan, 0, "automon message",
1959 * \brief Monitor a channel by DTMF
1960 * \param chan channel requesting monitor
1961 * \param peer channel to be monitored
1964 * \param sense feature options
1967 * Check monitor app enabled, setup channels, both caller/callee chans not null
1968 * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
1969 * \retval AST_FEATURE_RETURN_SUCCESS on success.
1970 * \retval -1 on error.
1972 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
1974 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
1977 struct ast_channel *caller_chan, *callee_chan;
1978 const char *automon_message_start = NULL;
1979 const char *automon_message_stop = NULL;
1980 const char *touch_format = NULL;
1981 const char *touch_monitor = NULL;
1982 const char *touch_monitor_prefix = NULL;
1983 struct ast_app *monitor_app;
1985 monitor_app = pbx_findapp("Monitor");
1987 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
1991 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
1993 /* Find extra messages */
1994 automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
1995 automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
1997 if (!ast_strlen_zero(courtesytone)) { /* Play courtesy tone if configured */
1998 if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
2003 if (ast_channel_monitor(callee_chan)) {
2004 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
2005 if (!ast_strlen_zero(automon_message_stop)) {
2006 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
2008 ast_channel_monitor(callee_chan)->stop(callee_chan, 1);
2009 return AST_FEATURE_RETURN_SUCCESS;
2012 touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
2013 touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
2014 touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
2017 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
2020 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
2022 if (!touch_monitor_prefix)
2023 touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
2025 if (touch_monitor) {
2026 len = strlen(touch_monitor) + 50;
2027 args = ast_alloca(len);
2028 touch_filename = ast_alloca(len);
2029 snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
2030 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
2032 caller_chan_id = ast_strdupa(S_COR(ast_channel_caller(caller_chan)->id.number.valid,
2033 ast_channel_caller(caller_chan)->id.number.str, ast_channel_name(caller_chan)));
2034 callee_chan_id = ast_strdupa(S_COR(ast_channel_caller(callee_chan)->id.number.valid,
2035 ast_channel_caller(callee_chan)->id.number.str, ast_channel_name(callee_chan)));
2036 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
2037 args = ast_alloca(len);
2038 touch_filename = ast_alloca(len);
2039 snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
2040 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
2043 for(x = 0; x < strlen(args); x++) {
2048 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
2050 pbx_exec(callee_chan, monitor_app, args);
2051 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
2052 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
2054 if (!ast_strlen_zero(automon_message_start)) { /* Play start message for both channels */
2055 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
2058 return AST_FEATURE_RETURN_SUCCESS;
2063 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2065 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
2068 struct ast_channel *caller_chan, *callee_chan;
2069 const char *mixmonitor_spy_type = "MixMonitor";
2070 const char *touch_format;
2071 const char *touch_monitor;
2072 struct ast_app *mixmonitor_app;
2075 mixmonitor_app = pbx_findapp("MixMonitor");
2076 if (!mixmonitor_app) {
2077 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
2081 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
2083 if (!ast_strlen_zero(courtesytone)) {
2084 if (ast_autoservice_start(callee_chan))
2086 ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
2087 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
2088 ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
2089 ast_autoservice_stop(callee_chan);
2092 if (ast_autoservice_stop(callee_chan))
2096 ast_channel_lock(callee_chan);
2097 count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
2098 ast_channel_unlock(callee_chan);
2100 /* This means a mixmonitor is attached to the channel, running or not is unknown. */
2102 ast_verb(3, "User hit '%s' to stop recording call.\n", code);
2104 /* Make sure they are running */
2105 ast_channel_lock(callee_chan);
2106 count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
2107 ast_channel_unlock(callee_chan);
2109 struct ast_app *stopmixmonitor_app;
2111 stopmixmonitor_app = pbx_findapp("StopMixMonitor");
2112 if (!stopmixmonitor_app) {
2113 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
2116 pbx_exec(callee_chan, stopmixmonitor_app, "");
2117 return AST_FEATURE_RETURN_SUCCESS;
2120 ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");
2123 touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
2124 touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
2127 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
2130 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
2132 if (touch_monitor) {
2133 len = strlen(touch_monitor) + 50;
2134 args = ast_alloca(len);
2135 touch_filename = ast_alloca(len);
2136 snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
2137 snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
2139 caller_chan_id = ast_strdupa(S_COR(ast_channel_caller(caller_chan)->id.number.valid,
2140 ast_channel_caller(caller_chan)->id.number.str, ast_channel_name(caller_chan)));
2141 callee_chan_id = ast_strdupa(S_COR(ast_channel_caller(callee_chan)->id.number.valid,
2142 ast_channel_caller(callee_chan)->id.number.str, ast_channel_name(callee_chan)));
2143 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
2144 args = ast_alloca(len);
2145 touch_filename = ast_alloca(len);
2146 snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
2147 snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
2150 for( x = 0; x < strlen(args); x++) {
2155 ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
2157 pbx_exec(callee_chan, mixmonitor_app, args);
2158 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
2159 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
2160 return AST_FEATURE_RETURN_SUCCESS;
2165 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2167 ast_verb(4, "User hit '%s' to disconnect call.\n", code);
2168 return AST_FEATURE_RETURN_HANGUP;
2174 * \brief Find the context for the transfer
2178 * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
2179 * \return a context string
2181 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
2183 const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
2184 if (ast_strlen_zero(s)) {
2185 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
2187 if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
2188 s = ast_channel_macrocontext(transferer);
2190 if (ast_strlen_zero(s)) {
2191 s = ast_channel_context(transferer);
2199 * \brief make channels compatible
2202 * \retval 0 on success.
2203 * \retval -1 on failure.
2205 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
2207 if (ast_channel_make_compatible(c, newchan) < 0) {
2208 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
2209 ast_channel_name(c), ast_channel_name(newchan));
2210 ast_autoservice_chan_hangup_peer(c, newchan);
2220 * \brief Builtin attended transfer failed cleanup.
2223 * \param transferee Party A in the transfer.
2224 * \param transferer Party B in the transfer.
2225 * \param connected_line Saved connected line info about party A.
2227 * \note The connected_line data is freed.
2231 static void atxfer_fail_cleanup(struct ast_channel *transferee, struct ast_channel *transferer, struct ast_party_connected_line *connected_line)
2233 finishup(transferee);
2236 * Restore party B connected line info about party A.
2238 * Party B was the caller to party C and is the last known mode
2241 if (ast_channel_connected_line_sub(transferee, transferer, connected_line, 0) &&
2242 ast_channel_connected_line_macro(transferee, transferer, connected_line, 1, 0)) {
2243 ast_channel_update_connected_line(transferer, connected_line, NULL);
2245 ast_party_connected_line_free(connected_line);
2251 * \brief Attended transfer
2252 * \param chan transfered user
2253 * \param peer person transfering call
2256 * \param sense feature options
2259 * Get extension to transfer to, if you cannot generate channel (or find extension)
2260 * return to host channel. After called channel answered wait for hangup of transferer,
2261 * bridge call between transfer peer (taking them off hold) to attended transfer channel.
2263 * \return -1 on failure
2265 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
2267 struct ast_channel *transferer;/* Party B */
2268 struct ast_channel *transferee;/* Party A */
2269 struct ast_exten *park_exten;
2270 const char *chan1_attended_sound;
2271 const char *chan2_attended_sound;
2272 const char *transferer_real_context;
2273 char xferto[256] = "";
2276 struct ast_channel *newchan;
2277 struct ast_channel *xferchan;
2278 struct ast_bridge_thread_obj *tobj;
2279 struct ast_bridge_config bconfig;
2281 struct ast_party_connected_line connected_line;
2282 struct ast_datastore *features_datastore;
2283 struct ast_dial_features *dialfeatures;
2284 char *transferer_tech;
2285 char *transferer_name;
2286 char *transferer_name_orig;
2288 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
2290 ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", ast_channel_name(chan), ast_channel_name(peer), sense);
2291 set_peers(&transferer, &transferee, peer, chan, sense);
2292 transferer_real_context = real_ctx(transferer, transferee);
2294 /* Start autoservice on transferee while we talk to the transferer */
2295 ast_autoservice_start(transferee);
2296 ast_indicate(transferee, AST_CONTROL_HOLD);
2299 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
2301 finishup(transferee);
2304 if (res > 0) { /* If they've typed a digit already, handle it */
2305 xferto[0] = (char) res;
2308 ast_channel_lock(transferer);
2309 xfer_cfg = ast_get_chan_features_xfer_config(transferer);
2310 ast_channel_unlock(transferer);
2312 /* XXX All accesses to the xfer_cfg structure after this point are not thread-safe,
2313 * but I don't care because this is dead code.
2316 /* this is specific of atxfer */
2317 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, xfer_cfg->transferdigittimeout);
2318 if (res < 0) { /* hangup or error, (would be 0 for invalid and 1 for valid) */
2319 finishup(transferee);
2325 ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
2326 xferto, transferer_real_context);
2328 /* Does anyone care about this case? */
2329 ast_log(LOG_WARNING, "No digits dialed for atxfer.\n");
2331 ast_stream_and_wait(transferer, "pbx-invalid", "");
2332 finishup(transferee);
2333 return AST_FEATURE_RETURN_SUCCESS;
2336 park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
2338 /* We are transfering the transferee to a parking lot. */
2339 return xfer_park_call_helper(transferee, transferer, park_exten);
2343 * Append context to dialed transfer number.
2345 * NOTE: The local channel needs the /n flag so party C will use
2346 * the feature flags set by the dialplan when calling that
2349 snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);
2351 /* If we are performing an attended transfer and we have two channels involved then
2352 copy sound file information to play upon attended transfer completion */
2353 chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
2354 chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
2355 if (!ast_strlen_zero(chan1_attended_sound)) {
2356 pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
2358 if (!ast_strlen_zero(chan2_attended_sound)) {
2359 pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
2362 /* Extract redial transferer information from the channel name. */
2363 transferer_name_orig = ast_strdupa(ast_channel_name(transferer));
2364 transferer_name = ast_strdupa(transferer_name_orig);
2365 transferer_tech = strsep(&transferer_name, "/");
2366 dash = strrchr(transferer_name, '-');
2368 /* Trim off channel name sequence/serial number. */
2372 /* Stop autoservice so we can monitor all parties involved in the transfer. */
2373 if (ast_autoservice_stop(transferee) < 0) {
2374 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2378 /* Save connected line info for party B about party A in case transfer fails. */
2379 ast_party_connected_line_init(&connected_line);
2380 ast_channel_lock(transferer);
2381 ast_party_connected_line_copy(&connected_line, ast_channel_connected(transferer));
2382 ast_channel_unlock(transferer);
2383 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
2386 newchan = feature_request_and_dial(transferer, transferer_name_orig, transferer,
2387 transferee, "Local", ast_channel_nativeformats(transferer), xferto,
2388 xfer_cfg->atxfernoanswertimeout, &outstate, ast_channel_language(transferer));
2389 ast_debug(2, "Dial party C result: newchan:%d, outstate:%d\n", !!newchan, outstate);
2391 if (!ast_check_hangup(transferer)) {
2392 /* Transferer (party B) is up */
2393 ast_debug(1, "Actually doing an attended transfer.\n");
2395 /* Start autoservice on transferee while the transferer deals with party C. */
2396 ast_autoservice_start(transferee);
2398 ast_indicate(transferer, -1);
2400 /* any reason besides user requested cancel and busy triggers the failed sound */
2402 case AST_CONTROL_UNHOLD:/* Caller requested cancel or party C answer timeout. */
2403 case AST_CONTROL_BUSY:
2404 case AST_CONTROL_CONGESTION:
2405 if (ast_stream_and_wait(transferer, xfer_cfg->xfersound, "")) {
2406 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
2410 if (ast_stream_and_wait(transferer, xfer_cfg->xferfailsound, "")) {
2411 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
2415 atxfer_fail_cleanup(transferee, transferer, &connected_line);
2416 return AST_FEATURE_RETURN_SUCCESS;
2419 if (check_compat(transferer, newchan)) {
2420 if (ast_stream_and_wait(transferer, xfer_cfg->xferfailsound, "")) {
2421 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
2423 atxfer_fail_cleanup(transferee, transferer, &connected_line);
2424 return AST_FEATURE_RETURN_SUCCESS;
2426 memset(&bconfig,0,sizeof(struct ast_bridge_config));
2427 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
2428 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
2431 * Let party B and C talk as long as they want while party A
2432 * languishes in autoservice listening to MOH.
2434 ast_bridge_call(transferer, newchan, &bconfig);
2436 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
2437 ast_autoservice_chan_hangup_peer(transferer, newchan);
2438 if (ast_stream_and_wait(transferer, xfer_cfg->xfersound, "")) {
2439 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
2441 atxfer_fail_cleanup(transferee, transferer, &connected_line);
2442 return AST_FEATURE_RETURN_SUCCESS;
2445 /* Transferer (party B) is confirmed hung up at this point. */
2446 if (check_compat(transferee, newchan)) {
2447 finishup(transferee);
2448 ast_party_connected_line_free(&connected_line);
2452 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2453 if ((ast_autoservice_stop(transferee) < 0)
2454 || (ast_waitfordigit(transferee, 100) < 0)
2455 || (ast_waitfordigit(newchan, 100) < 0)
2456 || ast_check_hangup(transferee)
2457 || ast_check_hangup(newchan)) {
2458 ast_hangup(newchan);
2459 ast_party_connected_line_free(&connected_line);
2462 } else if (!ast_check_hangup(transferee)) {
2463 /* Transferer (party B) has hung up at this point. Doing blonde transfer. */
2464 ast_debug(1, "Actually doing a blonde transfer.\n");
2466 if (!newchan && !xfer_cfg->atxferdropcall) {
2467 /* Party C is not available, try to call party B back. */
2468 unsigned int tries = 0;
2470 if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
2471 ast_log(LOG_WARNING,
2472 "Transferer channel name: '%s' cannot be used for callback.\n",
2473 transferer_name_orig);
2474 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2475 ast_party_connected_line_free(&connected_line);
2481 /* Try to get party B back. */
2482 ast_debug(1, "We're trying to callback %s/%s\n",
2483 transferer_tech, transferer_name);
2484 newchan = feature_request_and_dial(transferer, transferer_name_orig,
2485 transferee, transferee, transferer_tech,
2486 ast_channel_nativeformats(transferee), transferer_name,
2487 xfer_cfg->atxfernoanswertimeout, &outstate, ast_channel_language(transferer));
2488 ast_debug(2, "Dial party B result: newchan:%d, outstate:%d\n",
2489 !!newchan, outstate);
2492 * We have recalled party B (newchan). We need to give this
2493 * call leg the same feature flags as the original party B call
2496 ast_channel_lock(transferer);
2497 features_datastore = ast_channel_datastore_find(transferer,
2498 &dial_features_info, NULL);
2499 if (features_datastore && (dialfeatures = features_datastore->data)) {
2500 struct ast_flags my_features = { 0 };
2501 struct ast_flags peer_features = { 0 };
2503 ast_copy_flags(&my_features, &dialfeatures->my_features,
2505 ast_copy_flags(&peer_features, &dialfeatures->peer_features,
2507 ast_channel_unlock(transferer);
2508 add_features_datastore(newchan, &my_features, &peer_features);
2510 ast_channel_unlock(transferer);
2514 if (ast_check_hangup(transferee)) {
2519 if (xfer_cfg->atxfercallbackretries <= tries) {
2520 /* No more callback tries remaining. */
2524 if (xfer_cfg->atxferloopdelay) {
2525 /* Transfer failed, sleeping */
2526 ast_debug(1, "Sleeping for %d ms before retrying atxfer.\n",
2527 xfer_cfg->atxferloopdelay);
2528 ast_safe_sleep(transferee, xfer_cfg->atxferloopdelay);
2529 if (ast_check_hangup(transferee)) {
2530 ast_party_connected_line_free(&connected_line);
2535 /* Retry dialing party C. */
2536 ast_debug(1, "We're retrying to call %s/%s\n", "Local", xferto);
2537 newchan = feature_request_and_dial(transferer, transferer_name_orig,
2538 transferer, transferee, "Local",
2539 ast_channel_nativeformats(transferee), xferto,
2540 xfer_cfg->atxfernoanswertimeout, &outstate, ast_channel_language(transferer));
2541 ast_debug(2, "Redial party C result: newchan:%d, outstate:%d\n",
2542 !!newchan, outstate);
2543 if (newchan || ast_check_hangup(transferee)) {
2548 ast_indicate(transferee, AST_CONTROL_UNHOLD);
2550 /* No party C or could not callback party B. */
2551 ast_party_connected_line_free(&connected_line);
2555 /* newchan is up, we should prepare transferee and bridge them */
2556 if (ast_check_hangup(newchan)) {
2557 ast_autoservice_chan_hangup_peer(transferee, newchan);
2558 ast_party_connected_line_free(&connected_line);
2561 if (check_compat(transferee, newchan)) {
2562 ast_party_connected_line_free(&connected_line);
2567 * Both the transferer and transferee have hungup. If newchan
2568 * is up, hang it up as it has no one to talk to.
2570 ast_debug(1, "Everyone is hungup.\n");
2572 ast_hangup(newchan);
2574 ast_party_connected_line_free(&connected_line);
2578 /* Initiate the channel transfer of party A to party C (or recalled party B). */
2579 ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
2581 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", ast_channel_linkedid(transferee), 0, "Transfered/%s", ast_channel_name(transferee));
2583 ast_autoservice_chan_hangup_peer(transferee, newchan);
2584 ast_party_connected_line_free(&connected_line);
2588 /* Give party A a momentary ringback tone during transfer. */
2589 ast_channel_visible_indication_set(xferchan, AST_CONTROL_RINGING);
2591 /* Make formats okay */
2592 ast_format_copy(ast_channel_readformat(xferchan), ast_channel_readformat(transferee));
2593 ast_format_copy(ast_channel_writeformat(xferchan), ast_channel_writeformat(transferee));
2595 if (ast_channel_masquerade(xferchan, transferee)) {
2596 ast_hangup(xferchan);
2597 ast_autoservice_chan_hangup_peer(transferee, newchan);
2598 ast_party_connected_line_free(&connected_line);
2602 dash = strrchr(xferto, '@');
2604 /* Trim off the context. */
2607 ast_explicit_goto(xferchan, transferer_real_context, xferto, 1);
2608 ast_channel_state_set(xferchan, AST_STATE_UP);
2609 ast_clear_flag(ast_channel_flags(xferchan), AST_FLAGS_ALL);
2611 /* Do the masquerade manually to make sure that is is completed. */
2612 ast_do_masquerade(xferchan);
2614 ast_channel_state_set(newchan, AST_STATE_UP);
2615 ast_clear_flag(ast_channel_flags(newchan), AST_FLAGS_ALL);
2616 tobj = ast_calloc(1, sizeof(*tobj));
2618 ast_hangup(xferchan);
2619 ast_hangup(newchan);
2620 ast_party_connected_line_free(&connected_line);
2624 tobj->chan = newchan;
2625 tobj->peer = xferchan;
2626 tobj->bconfig = *config;
2628 ast_channel_lock(newchan);
2629 features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL);
2630 if (features_datastore && (dialfeatures = features_datastore->data)) {
2631 ast_copy_flags(&tobj->bconfig.features_callee, &dialfeatures->my_features,
2634 ast_channel_unlock(newchan);
2636 ast_channel_lock(xferchan);
2637 features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL);
2638 if (features_datastore && (dialfeatures = features_datastore->data)) {
2639 ast_copy_flags(&tobj->bconfig.features_caller, &dialfeatures->my_features,
2642 ast_channel_unlock(xferchan);
2644 if (tobj->bconfig.end_bridge_callback_data_fixup) {
2645 tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
2649 * xferchan is transferee, and newchan is the transfer target
2650 * So...in a transfer, who is the caller and who is the callee?
2652 * When the call is originally made, it is clear who is caller and callee.
2653 * When a transfer occurs, it is my humble opinion that the transferee becomes
2654 * the caller, and the transfer target is the callee.
2656 * The problem is that these macros were set with the intention of the original
2657 * caller and callee taking those roles. A transfer can totally mess things up,
2658 * to be technical. What sucks even more is that you can't effectively change
2659 * the macros in the dialplan during the call from the transferer to the transfer
2660 * target because the transferee is stuck with whatever role he originally had.
2662 * I think the answer here is just to make sure that it is well documented that
2663 * during a transfer, the transferee is the "caller" and the transfer target
2666 * This means that if party B calls party A, and party B transfers party A to
2667 * party C, then A has switched roles for the call. Now party A will have the
2668 * caller macro called on his channel instead of the callee macro.
2670 * Luckily, the method by which the party B to party C bridge is
2671 * launched above ensures that the transferee is the "chan" on
2672 * the bridge and the transfer target is the "peer," so my idea
2673 * for the roles post-transfer does not require extensive code
2677 /* Transfer party C connected line to party A */
2678 ast_channel_lock(transferer);
2680 * Due to a limitation regarding when callerID is set on a Local channel,
2681 * we use the transferer's connected line information here.
2683 ast_party_connected_line_copy(&connected_line, ast_channel_connected(transferer));
2684 ast_channel_unlock(transferer);
2685 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
2686 if (ast_channel_connected_line_sub(newchan, xferchan, &connected_line, 0) &&
2687 ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
2688 ast_channel_update_connected_line(xferchan, &connected_line, NULL);
2691 /* Transfer party A connected line to party C */
2692 ast_channel_lock(xferchan);
2693 ast_connected_line_copy_from_caller(&connected_line, ast_channel_caller(xferchan));
2694 ast_channel_unlock(xferchan);
2695 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
2696 if (ast_channel_connected_line_sub(xferchan, newchan, &connected_line, 0) &&
2697 ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
2698 ast_channel_update_connected_line(newchan, &connected_line, NULL);
2701 if (ast_stream_and_wait(newchan, xfer_cfg->xfersound, ""))
2702 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
2703 bridge_call_thread_launch(tobj);
2705 ast_party_connected_line_free(&connected_line);
2706 return -1;/* The transferee is masqueraded and the original bridged channels can be hungup. */
2712 * \brief Get the extension for a given builtin feature
2714 * \pre expects features_lock to be readlocked
2717 * \retval non-zero failiure
2719 static int builtin_feature_get_exten(struct ast_channel *chan, const char *feature_name,
2720 char *buf, size_t len)
2722 SCOPED_CHANNELLOCK(lock, chan);
2724 return ast_get_builtin_feature(chan, feature_name, buf, len);
2727 static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
2729 /* BUGBUG there is code that checks AST_BRIDGE_IGNORE_SIGS but no code to set it. */
2730 /* BUGBUG there is code that checks AST_BRIDGE_REC_CHANNEL_0 but no code to set it. */
2731 /* BUGBUG there is code that checks AST_BRIDGE_REC_CHANNEL_1 but no code to set it. */
2732 ast_clear_flag(config, AST_FLAGS_ALL);
2734 if (ast_test_flag(&config->features_caller, AST_FEATURE_DTMF_MASK)) {
2735 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
2737 if (ast_test_flag(&config->features_callee, AST_FEATURE_DTMF_MASK)) {
2738 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
2741 if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
2742 RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
2744 ast_channel_lock(chan);
2745 applicationmap = ast_get_chan_applicationmap(chan);
2746 ast_channel_unlock(chan);
2748 if (!applicationmap) {
2752 /* If an applicationmap exists for this channel at all, then the channel needs the DTMF flag set */
2753 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
2760 * \brief Get feature and dial.
2762 * \param caller Channel to represent as the calling channel for the dialed channel.
2763 * \param caller_name Original caller channel name.
2764 * \param requestor Channel to say is requesting the dial (usually the caller).
2765 * \param transferee Channel that the dialed channel will be transferred to.
2766 * \param type Channel technology type to dial.
2767 * \param format Codec formats for dialed channel.
2768 * \param addr destination of the call
2769 * \param timeout Time limit for dialed channel to answer in ms. Must be greater than zero.
2770 * \param outstate Status of dialed channel if unsuccessful.
2771 * \param language Language of the caller.
2775 * 0, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION,
2776 * AST_CONTROL_ANSWER, or AST_CONTROL_UNHOLD. If
2777 * AST_CONTROL_UNHOLD then the caller channel cancelled the
2778 * transfer or the dialed channel did not answer before the
2782 * Request channel, set channel variables, initiate call,
2783 * check if they want to disconnect, go into loop, check if timeout has elapsed,
2784 * check if person to be transfered hung up, check for answer break loop,
2785 * set cdr return channel.
2787 * \retval Channel Connected channel for transfer.
2788 * \retval NULL on failure to get third party connected.
2790 * \note This is similar to __ast_request_and_dial() in channel.c
2792 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
2793 const char *caller_name, struct ast_channel *requestor,
2794 struct ast_channel *transferee, const char *type, struct ast_format_cap *cap, const char *addr,
2795 int timeout, int *outstate, const char *language)
2801 int transferee_hungup;
2802 struct ast_channel *chan;
2803 struct ast_channel *monitor_chans[3];
2804 struct ast_channel *active_channel;
2807 struct timeval started;
2809 char disconnect_code[AST_FEATURE_MAX_LEN];
2810 char *dialed_code = NULL;
2811 struct ast_format_cap *tmp_cap;
2812 struct ast_format best_audio_fmt;
2813 struct ast_frame *f;
2815 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
2817 tmp_cap = ast_format_cap_alloc_nolock();
2824 ast_best_codec(cap, &best_audio_fmt);
2825 ast_format_cap_add(tmp_cap, &best_audio_fmt);
2827 caller_hungup = ast_check_hangup(caller);
2829 if (!(chan = ast_request(type, tmp_cap, requestor, addr, &cause))) {
2830 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, addr);
2832 case AST_CAUSE_BUSY:
2833 state = AST_CONTROL_BUSY;
2835 case AST_CAUSE_CONGESTION:
2836 state = AST_CONTROL_CONGESTION;
2845 ast_channel_language_set(chan, language);
2846 ast_channel_inherit_variables(caller, chan);
2847 pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller_name);
2849 ast_channel_lock(chan);
2850 ast_connected_line_copy_from_caller(ast_channel_connected(chan), ast_channel_caller(requestor));
2851 ast_channel_unlock(chan);
2853 if (ast_call(chan, addr, timeout)) {
2854 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, addr);
2855 switch (ast_channel_hangupcause(chan)) {
2856 case AST_CAUSE_BUSY:
2857 state = AST_CONTROL_BUSY;
2859 case AST_CAUSE_CONGESTION:
2860 state = AST_CONTROL_CONGESTION;
2869 /* support dialing of the featuremap disconnect code while performing an attended tranfer */
2870 ast_channel_lock(chan);
2871 disconnect_res = ast_get_builtin_feature(chan, "disconnect",
2872 disconnect_code, sizeof(disconnect_code));
2873 ast_channel_unlock(chan);
2875 if (!disconnect_res) {
2876 len = strlen(disconnect_code) + 1;
2877 dialed_code = ast_alloca(len);
2878 memset(dialed_code, 0, len);
2882 started = ast_tvnow();
2884 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
2886 ast_poll_channel_add(caller, chan);
2888 transferee_hungup = 0;
2889 while (!ast_check_hangup(transferee) && (ast_channel_state(chan) != AST_STATE_UP)) {
2892 monitor_chans[num_chans++] = transferee;
2893 monitor_chans[num_chans++] = chan;
2894 if (!caller_hungup) {
2895 if (ast_check_hangup(caller)) {
2898 #if defined(ATXFER_NULL_TECH)
2899 /* Change caller's name to ensure that it will remain unique. */
2900 set_new_chan_name(caller);
2903 * Get rid of caller's physical technology so it is free for
2906 set_kill_chan_tech(caller);
2907 #endif /* defined(ATXFER_NULL_TECH) */
2909 /* caller is not hungup so monitor it. */
2910 monitor_chans[num_chans++] = caller;
2914 /* see if the timeout has been violated */
2915 if (ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
2916 state = AST_CONTROL_UNHOLD;
2917 ast_log(LOG_NOTICE, "We exceeded our AT-timeout for %s\n", ast_channel_name(chan));
2918 break; /*doh! timeout*/
2921 active_channel = ast_waitfor_n(monitor_chans, num_chans, &to);
2922 if (!active_channel)
2926 if (transferee == active_channel) {
2927 struct ast_frame *dup_f;
2929 f = ast_read(transferee);
2930 if (f == NULL) { /*doh! where'd he go?*/
2931 transferee_hungup = 1;
2935 if (ast_is_deferrable_frame(f)) {
2936 dup_f = ast_frisolate(f);
2941 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
2944 } else if (chan == active_channel) {
2945 if (!ast_strlen_zero(ast_channel_call_forward(chan))) {
2947 ast_autoservice_start(transferee);
2948 chan = ast_call_forward(caller, chan, NULL, tmp_cap, NULL, &state);
2949 ast_autoservice_stop(transferee);
2956 if (f == NULL) { /*doh! where'd he go?*/
2957 switch (ast_channel_hangupcause(chan)) {
2958 case AST_CAUSE_BUSY:
2959 state = AST_CONTROL_BUSY;
2961 case AST_CAUSE_CONGESTION:
2962 state = AST_CONTROL_CONGESTION;
2971 if (f->frametype == AST_FRAME_CONTROL) {
2972 if (f->subclass.integer == AST_CONTROL_RINGING) {
2973 ast_verb(3, "%s is ringing\n", ast_channel_name(chan));
2974 ast_indicate(caller, AST_CONTROL_RINGING);
2975 } else if (f->subclass.integer == AST_CONTROL_BUSY) {
2976 state = f->subclass.integer;
2977 ast_verb(3, "%s is busy\n", ast_channel_name(chan));
2978 ast_indicate(caller, AST_CONTROL_BUSY);
2981 } else if (f->subclass.integer == AST_CONTROL_INCOMPLETE) {
2982 ast_verb(3, "%s dialed incomplete extension %s; ignoring\n", ast_channel_name(chan), ast_channel_exten(chan));
2983 } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
2984 state = f->subclass.integer;
2985 ast_verb(3, "%s is congested\n", ast_channel_name(chan));
2986 ast_indicate(caller, AST_CONTROL_CONGESTION);
2989 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
2990 /* This is what we are hoping for */
2991 state = f->subclass.integer;
2995 } else if (f->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
2996 ast_indicate_data(caller, AST_CONTROL_PVT_CAUSE_CODE, f->data.ptr, f->datalen);
2997 } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
2998 if (caller_hungup) {
2999 struct ast_party_connected_line connected;
3001 /* Just save it for the transfer. */
3002 ast_party_connected_line_set_init(&connected, ast_channel_connected(caller));
3003 res = ast_connected_line_parse_data(f->data.ptr, f->datalen,
3006 ast_channel_set_connected_line(caller, &connected, NULL);
3008 ast_party_connected_line_free(&connected);
3010 ast_autoservice_start(transferee);
3011 if (ast_channel_connected_line_sub(chan, caller, f, 1) &&
3012 ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
3013 ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE,
3014 f->data.ptr, f->datalen);
3016 ast_autoservice_stop(transferee);
3018 } else if (f->subclass.integer == AST_CONTROL_REDIRECTING) {
3019 if (!caller_hungup) {
3020 ast_autoservice_start(transferee);
3021 if (ast_channel_redirecting_sub(chan, caller, f, 1) &&
3022 ast_channel_redirecting_macro(chan, caller, f, 1, 1)) {
3023 ast_indicate_data(caller, AST_CONTROL_REDIRECTING,
3024 f->data.ptr, f->datalen);
3026 ast_autoservice_stop(transferee);
3028 } else if (f->subclass.integer != -1
3029 && f->subclass.integer != AST_CONTROL_PROGRESS
3030 && f->subclass.integer != AST_CONTROL_PROCEEDING) {
3031 ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
3033 /* else who cares */
3034 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3035 ast_write(caller, f);
3037 } else if (caller == active_channel) {
3038 f = ast_read(caller);
3040 if (f->frametype == AST_FRAME_DTMF && dialed_code) {
3041 dialed_code[x++] = f->subclass.integer;
3042 dialed_code[x] = '\0';
3043 if (strlen(dialed_code) == len) {
3045 } else if (x && strncmp(dialed_code, disconnect_code, x)) {