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/test.h"
74 #include "asterisk/bridge.h"
75 #include "asterisk/bridge_basic.h"
76 #include "asterisk/bridge_after.h"
77 #include "asterisk/stasis.h"
78 #include "asterisk/stasis_channels.h"
79 #include "asterisk/features_config.h"
81 /* BUGBUG TEST_FRAMEWORK is disabled because parking tests no longer work. */
85 * Party A - transferee
86 * Party B - transferer
87 * Party C - target of transfer
89 * DTMF attended transfer works within the channel bridge.
90 * Unfortunately, when either party A or B in the channel bridge
91 * hangs up, that channel is not completely hung up until the
92 * transfer completes. This is a real problem depending upon
93 * the channel technology involved.
95 * For chan_dahdi, the channel is crippled until the hangup is
96 * complete. Either the channel is not useable (analog) or the
97 * protocol disconnect messages are held up (PRI/BRI/SS7) and
98 * the media is not released.
100 * For chan_sip, a call limit of one is going to block that
101 * endpoint from any further calls until the hangup is complete.
103 * For party A this is a minor problem. The party A channel
104 * will only be in this condition while party B is dialing and
105 * when party B and C are conferring. The conversation between
106 * party B and C is expected to be a short one. Party B is
107 * either asking a question of party C or announcing party A.
108 * Also party A does not have much incentive to hangup at this
111 * For party B this can be a major problem during a blonde
112 * transfer. (A blonde transfer is our term for an attended
113 * transfer that is converted into a blind transfer. :)) Party
114 * B could be the operator. When party B hangs up, he assumes
115 * that he is out of the original call entirely. The party B
116 * channel will be in this condition while party C is ringing,
117 * while attempting to recall party B, and while waiting between
121 * The ATXFER_NULL_TECH conditional is a hack to fix the
122 * problem. It will replace the party B channel technology with
123 * a NULL channel driver. The consequences of this code is that
124 * the 'h' extension will not be able to access any channel
125 * technology specific information like SIP statistics for the
128 * Uncomment the ATXFER_NULL_TECH define below to replace the
129 * party B channel technology in the channel bridge to complete
130 * hanging up the channel technology.
132 //#define ATXFER_NULL_TECH 1
135 <application name="Bridge" language="en_US">
140 <parameter name="channel" required="true">
141 <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
143 <parameter name="options">
146 <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
148 <option name="F" argsep="^">
149 <argument name="context" required="false" />
150 <argument name="exten" required="false" />
151 <argument name="priority" required="true" />
152 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
153 to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
155 <para>Any channel variables you want the called channel to inherit from the caller channel must be
156 prefixed with one or two underbars ('_').</para>
159 <para>This option will override the 'x' option</para>
163 <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
164 to the next priority of the current extension and <emphasis>start</emphasis> execution
165 at that location.</para>
167 <para>Any channel variables you want the called channel to inherit from the caller channel must be
168 prefixed with one or two underbars ('_').</para>
171 <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
174 <para>This option will override the 'x' option</para>
179 <para>Allow the called party to hang up by sending the
180 <replaceable>*</replaceable> DTMF digit.</para>
183 <para>Allow the calling party to hang up by pressing the
184 <replaceable>*</replaceable> DTMF digit.</para>
187 <para>Allow the called party to enable parking of the call by sending
188 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
191 <para>Allow the calling party to enable parking of the call by sending
192 the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
194 <option name="L(x[:y][:z])">
195 <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
196 when <replaceable>y</replaceable> ms are left. Repeat the warning every
197 <replaceable>z</replaceable> ms. The following special variables can be
198 used with this option:</para>
200 <variable name="LIMIT_PLAYAUDIO_CALLER">
201 <para>Play sounds to the caller. yes|no (default yes)</para>
203 <variable name="LIMIT_PLAYAUDIO_CALLEE">
204 <para>Play sounds to the callee. yes|no</para>
206 <variable name="LIMIT_TIMEOUT_FILE">
207 <para>File to play when time is up.</para>
209 <variable name="LIMIT_CONNECT_FILE">
210 <para>File to play when call begins.</para>
212 <variable name="LIMIT_WARNING_FILE">
213 <para>File to play as warning if <replaceable>y</replaceable> is
214 defined. The default is to say the time remaining.</para>
219 <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
222 <para>Allow the called party to transfer the calling party by sending the
223 DTMF sequence defined in <filename>features.conf</filename>.</para>
226 <para>Allow the calling party to transfer the called party by sending the
227 DTMF sequence defined in <filename>features.conf</filename>.</para>
230 <para>Allow the called party to enable recording of the call by sending
231 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
234 <para>Allow the calling party to enable recording of the call by sending
235 the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
238 <para>Cause the called party to be hung up after the bridge, instead of being
239 restarted in the dialplan.</para>
245 <para>Allows the ability to bridge two channels via the dialplan.</para>
246 <para>This application sets the following channel variable upon completion:</para>
248 <variable name="BRIDGERESULT">
249 <para>The result of the bridge attempt as a text string.</para>
250 <value name="SUCCESS" />
251 <value name="FAILURE" />
252 <value name="LOOP" />
253 <value name="NONEXISTENT" />
254 <value name="INCOMPATIBLE" />
259 <manager name="Bridge" language="en_US">
261 Bridge two channels already in the PBX.
264 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
265 <parameter name="Channel1" required="true">
266 <para>Channel to Bridge to Channel2.</para>
268 <parameter name="Channel2" required="true">
269 <para>Channel to Bridge to Channel1.</para>
271 <parameter name="Tone">
272 <para>Play courtesy tone to Channel 2.</para>
275 <enum name="Channel1" />
276 <enum name="Channel2" />
282 <para>Bridge together two channels already in the PBX.</para>
285 <managerEvent language="en_US" name="Pickup">
286 <managerEventInstance class="EVENT_FLAG_CALL">
287 <synopsis>Raised when a call pickup occurs.</synopsis>
289 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
290 <parameter name="TargetChannel"/>
291 <parameter name="TargetChannelState"><para>A numeric code for the channel's current state, related to TargetChannelStateDesc</para></parameter>
292 <parameter name="TargetChannelStateDesc">
296 <enum name="OffHook"/>
297 <enum name="Dialing"/>
299 <enum name="Ringing"/>
302 <enum name="Dialing Offhook"/>
303 <enum name="Pre-ring"/>
304 <enum name="Unknown"/>
307 <parameter name="TargetCallerIDNum"/>
308 <parameter name="TargetCallerIDName"/>
309 <parameter name="TargetConnectedLineNum"/>
310 <parameter name="TargetConnectedLineName"/>
311 <parameter name="TargetAccountCode"/>
312 <parameter name="TargetContext"/>
313 <parameter name="TargetExten"/>
314 <parameter name="TargetPriority"/>
315 <parameter name="TargetUniqueid"/>
317 </managerEventInstance>
321 #define DEFAULT_PARK_TIME 45000 /*!< ms */
322 #define DEFAULT_PARK_EXTENSION "700"
323 #define DEFAULT_COMEBACK_CONTEXT "parkedcallstimeout"
324 #define DEFAULT_COMEBACK_TO_ORIGIN 1
325 #define DEFAULT_COMEBACK_DIAL_TIME 30
327 #define AST_MAX_WATCHERS 256
328 #define MAX_DIAL_FEATURE_OPTIONS 30
330 /* TODO Scrape all of the parking stuff out of features.c */
333 FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
334 FEATURE_INTERPRET_DO, /* Used by feature_interpret */
335 FEATURE_INTERPRET_CHECK, /* Used by feature_check */
336 } feature_interpret_op;
338 /*! Parking lot access ramp dialplan usage entry. */
339 struct parking_dp_ramp {
340 /*! Next node in the parking lot spaces dialplan list. */
341 AST_LIST_ENTRY(parking_dp_ramp) node;
342 /*! TRUE if the parking lot access extension is exclusive. */
343 unsigned int exclusive:1;
344 /*! Parking lot access extension */
348 /*! Parking lot dialplan access ramp map */
349 AST_LIST_HEAD_NOLOCK(parking_dp_ramp_map, parking_dp_ramp);
351 /*! Parking lot spaces dialplan usage entry. */
352 struct parking_dp_spaces {
353 /*! Next node in the parking lot spaces dialplan list. */
354 AST_LIST_ENTRY(parking_dp_spaces) node;
355 /*! First parking space */
357 /*! Last parking space */
361 /*! Parking lot dialplan context space map */
362 AST_LIST_HEAD_NOLOCK(parking_dp_space_map, parking_dp_spaces);
364 /*! Parking lot context dialplan usage entry. */
365 struct parking_dp_context {
366 /*! Next node in the parking lot contexts dialplan list. */
367 AST_LIST_ENTRY(parking_dp_context) node;
368 /*! Parking access extensions defined in this context. */
369 struct parking_dp_ramp_map access_extens;
370 /*! Parking spaces defined in this context. */
371 struct parking_dp_space_map spaces;
372 /*! Parking hints defined in this context. */
373 struct parking_dp_space_map hints;
374 /*! Parking lot context name */
378 /*! Parking lot dialplan usage map. */
379 AST_LIST_HEAD_NOLOCK(parking_dp_map, parking_dp_context);
382 * \brief Description of one parked call, added to a list while active, then removed.
383 * The list belongs to a parkinglot.
386 struct ast_channel *chan; /*!< Parked channel */
387 struct timeval start; /*!< Time the park started */
388 int parkingnum; /*!< Parking lot space used */
389 char parkingexten[AST_MAX_EXTENSION]; /*!< If set beforehand, parking extension used for this call */
390 char context[AST_MAX_CONTEXT]; /*!< Where to go if our parking time expires */
391 char exten[AST_MAX_EXTENSION];
393 unsigned int parkingtime; /*!< Maximum length in parking lot before return */
394 /*! Method to entertain the caller when parked: AST_CONTROL_RINGING, AST_CONTROL_HOLD, or 0(none) */
395 enum ast_control_frame_type hold_method;
396 unsigned int notquiteyet:1;
397 unsigned int options_specified:1;
398 char peername[AST_CHANNEL_NAME];
399 unsigned char moh_trys;
400 /*! Parking lot this entry belongs to. Holds a parking lot reference. */
401 struct ast_parkinglot *parkinglot;
402 AST_LIST_ENTRY(parkeduser) list;
405 /*! Parking lot configuration options. */
406 struct parkinglot_cfg {
407 /*! Music class used for parking */
408 char mohclass[MAX_MUSICCLASS];
409 /*! Extension to park calls in this parking lot. */
410 char parkext[AST_MAX_EXTENSION];
411 /*! Context for which parking is made accessible */
412 char parking_con[AST_MAX_CONTEXT];
413 /*! Context that timed-out parked calls are called back on when comebacktoorigin=no */
414 char comebackcontext[AST_MAX_CONTEXT];
415 /*! First available extension for parking */
417 /*! Last available extension for parking */
419 /*! Default parking time in ms. */
420 unsigned int parkingtime;
422 * \brief Enable DTMF based transfers on bridge when picking up parked calls.
426 * AST_FEATURE_FLAG_BYCALLEE
427 * AST_FEATURE_FLAG_BYCALLER
428 * AST_FEATURE_FLAG_BYBOTH
430 int parkedcalltransfers;
432 * \brief Enable DTMF based parking on bridge when picking up parked calls.
436 * AST_FEATURE_FLAG_BYCALLEE
437 * AST_FEATURE_FLAG_BYCALLER
438 * AST_FEATURE_FLAG_BYBOTH
440 int parkedcallreparking;
442 * \brief Enable DTMF based hangup on a bridge when pickup up parked calls.
446 * AST_FEATURE_FLAG_BYCALLEE
447 * AST_FEATURE_FLAG_BYCALLER
448 * AST_FEATURE_FLAG_BYBOTH
450 int parkedcallhangup;
452 * \brief Enable DTMF based recording on a bridge when picking up parked calls.
456 * AST_FEATURE_FLAG_BYCALLEE
457 * AST_FEATURE_FLAG_BYCALLER
458 * AST_FEATURE_FLAG_BYBOTH
460 int parkedcallrecording;
462 /*! Time in seconds to dial the device that parked a timedout parked call */
463 unsigned int comebackdialtime;
464 /*! TRUE if findslot is set to next */
465 unsigned int parkfindnext:1;
466 /*! TRUE if the parking lot is exclusively accessed by parkext */
467 unsigned int parkext_exclusive:1;
468 /*! Add parking hints automatically */
469 unsigned int parkaddhints:1;
470 /*! TRUE if configuration is invalid and the parking lot should not be used. */
471 unsigned int is_invalid:1;
472 /*! TRUE if a timed out parked call goes back to the parker */
473 unsigned int comebacktoorigin:1;
476 /*! \brief Structure for parking lots which are put in a container. */
477 struct ast_parkinglot {
478 /*! Name of the parking lot. */
479 char name[AST_MAX_CONTEXT];
480 /*! Parking lot user configuration. */
481 struct parkinglot_cfg cfg;
483 /*! Parking space to start next park search. */
484 int next_parking_space;
486 /*! That which bears the_mark shall be deleted if parking lot empty! (Used during reloads.) */
487 unsigned int the_mark:1;
488 /*! TRUE if the parking lot is disabled. */
489 unsigned int disabled:1;
491 /*! List of active parkings in this parkinglot */
492 AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
495 /*! \brief The configured parking lots container. Always at least one - the default parking lot */
496 static struct ao2_container *parkinglots;
499 * \brief Default parking lot.
500 * \note Holds a parkinglot reference.
501 * \note Will not be NULL while running.
503 static struct ast_parkinglot *default_parkinglot;
505 /*! Force a config reload to reload regardless of config file timestamp. */
506 #ifdef TEST_FRAMEWORK
507 static int force_reload_load;
510 static int parkeddynamic = 0; /*!< Enable creation of parkinglots dynamically */
513 * \brief Context for parking dialback to parker.
514 * \note The need for the context is a KLUDGE.
516 * \todo Might be able to eliminate the parking_con_dial context
517 * kludge by running app_dial directly in its own thread to
520 static char parking_con_dial[] = "park-dial";
522 /*! Ensure that features.conf reloads on one thread at a time. */
523 AST_MUTEX_DEFINE_STATIC(features_reload_lock);
527 static char *registrar = "features"; /*!< Registrar for operations */
529 /*! PARK_APP_NAME application arguments */
530 AST_DEFINE_APP_ARGS_TYPE(park_app_args,
531 AST_APP_ARG(timeout); /*!< Time in ms to remain in the parking lot. */
532 AST_APP_ARG(return_con); /*!< Context to return parked call if timeout. */
533 AST_APP_ARG(return_ext); /*!< Exten to return parked call if timeout. */
534 AST_APP_ARG(return_pri); /*!< Priority to return parked call if timeout. */
535 AST_APP_ARG(options); /*!< Parking option flags. */
536 AST_APP_ARG(pl_name); /*!< Parking lot name to use if present. */
537 AST_APP_ARG(dummy); /*!< Place to put any remaining args string. */
540 /* module and CLI command definitions */
541 static const char *parkcall = "Park";
543 static pthread_t parking_thread;
544 struct ast_dial_features {
545 /*! Channel's feature flags. */
546 struct ast_flags my_features;
547 /*! Bridge peer's feature flags. */
548 struct ast_flags peer_features;
551 static struct ast_manager_event_blob *call_pickup_to_ami(struct stasis_message *message);
553 STASIS_MESSAGE_TYPE_DEFN(
554 ast_call_pickup_type,
555 .to_ami = call_pickup_to_ami);
558 #if defined(ATXFER_NULL_TECH)
561 * \brief Set the channel technology to the kill technology.
563 * \param chan Channel to change technology.
567 static void set_kill_chan_tech(struct ast_channel *chan)
571 ast_channel_lock(chan);
573 /* Hangup the channel's physical side */
574 if (ast_channel_tech(chan)->hangup) {
575 ast_channel_tech(chan)->hangup(chan);
577 if (ast_channel_tech_pvt(chan)) {
578 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
579 ast_channel_name(chan));
580 ast_free(ast_channel_tech_pvt(chan));
581 ast_channel_tech_pvt_set(chan, NULL);
584 /* Install the kill technology and wake up anyone waiting on it. */
585 ast_channel_tech_set(chan, &ast_kill_tech);
586 for (idx = 0; idx < AST_MAX_FDS; ++idx) {
590 case AST_GENERATOR_FD:
591 /* Don't clear these fd's. */
594 ast_channel_set_fd(chan, idx, -1);
598 ast_queue_frame(chan, &ast_null_frame);
600 ast_channel_unlock(chan);
602 #endif /* defined(ATXFER_NULL_TECH) */
604 #if defined(ATXFER_NULL_TECH)
607 * \brief Set the channel name to something unique.
609 * \param chan Channel to change name.
613 static void set_new_chan_name(struct ast_channel *chan)
615 static int seq_num_last;
621 /* Create the new channel name string. */
622 ast_channel_lock(chan);
623 seq_num = ast_atomic_fetchadd_int(&seq_num_last, +1);
624 len = snprintf(dummy, sizeof(dummy), "%s<XFER_%x>", ast_channel_name(chan), seq_num) + 1;
625 chan_name = ast_alloca(len);
626 snprintf(chan_name, len, "%s<XFER_%x>", ast_channel_name(chan), seq_num);
627 ast_channel_unlock(chan);
629 ast_change_name(chan, chan_name);
631 #endif /* defined(ATXFER_NULL_TECH) */
633 static void *dial_features_duplicate(void *data)
635 struct ast_dial_features *df = data, *df_copy;
637 if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
641 memcpy(df_copy, df, sizeof(*df));
646 static const struct ast_datastore_info dial_features_info = {
647 .type = "dial-features",
648 .destroy = ast_free_ptr,
649 .duplicate = dial_features_duplicate,
654 * \brief Set the features datastore if it doesn't exist.
656 * \param chan Channel to add features datastore
657 * \param my_features The channel's feature flags
658 * \param peer_features The channel's bridge peer feature flags
660 * \retval TRUE if features datastore already existed.
662 static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
664 struct ast_datastore *datastore;
665 struct ast_dial_features *dialfeatures;
667 ast_channel_lock(chan);
668 datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
669 ast_channel_unlock(chan);
671 /* Already exists. */
675 /* Create a new datastore with specified feature flags. */
676 datastore = ast_datastore_alloc(&dial_features_info, NULL);
678 ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
681 dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
683 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
684 ast_datastore_free(datastore);
687 ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
688 ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
689 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
690 datastore->data = dialfeatures;
691 ast_channel_lock(chan);
692 ast_channel_datastore_add(chan, datastore);
693 ast_channel_unlock(chan);
697 /* Forward declarations */
698 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
699 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
700 static struct ast_parkinglot *find_parkinglot(const char *name);
701 static struct ast_parkinglot *create_parkinglot(const char *name);
702 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
703 static int parkinglot_activate(struct ast_parkinglot *parkinglot);
704 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile);
708 * \brief Get the parking extension if it exists.
710 * \param exten_str Parking extension to see if exists.
711 * \param chan Channel to autoservice while looking for exten. (Could be NULL)
712 * \param context Parking context to look in for exten.
714 * \retval exten on success.
715 * \retval NULL on error or exten does not exist.
717 static struct ast_exten *get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
719 struct ast_exten *exten;
720 struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
721 const char *app_at_exten;
723 ast_debug(4, "Checking if %s@%s is a parking exten\n", exten_str, context);
724 exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
730 app_at_exten = ast_get_extension_app(exten);
731 if (!app_at_exten || strcasecmp(parkcall, app_at_exten)) {
738 int ast_parking_ext_valid(const char *exten_str, struct ast_channel *chan, const char *context)
740 return get_parking_exten(exten_str, chan, context) ? 1 : 0;
743 struct ast_bridge_thread_obj
745 struct ast_bridge_config bconfig;
746 struct ast_channel *chan;
747 struct ast_channel *peer;
748 struct ast_callid *callid; /*<! callid pointer (Only used to bind thread) */
749 unsigned int return_to_pbx:1;
752 static int parkinglot_hash_cb(const void *obj, const int flags)
754 const struct ast_parkinglot *parkinglot = obj;
756 return ast_str_case_hash(parkinglot->name);
759 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
761 struct ast_parkinglot *parkinglot = obj;
762 struct ast_parkinglot *parkinglot2 = arg;
764 return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
768 * \brief store context, extension and priority
769 * \param chan, context, ext, pri
771 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
773 ast_channel_context_set(chan, context);
774 ast_channel_exten_set(chan, ext);
775 ast_channel_priority_set(chan, pri);
778 static const struct ast_datastore_info channel_app_data_datastore = {
779 .type = "Channel appdata datastore",
780 .destroy = ast_free_ptr,
784 * \brief Announce call parking by ADSI
786 * \param parkingexten .
787 * Create message to show for ADSI, display message.
788 * \retval 0 on success.
789 * \retval -1 on failure.
791 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
794 int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
796 char *message[5] = {NULL, NULL, NULL, NULL, NULL};
798 snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
800 res = ast_adsi_load_session(chan, NULL, 0, 1);
803 return ast_adsi_print(chan, message, justify, 1);
807 * \brief Find parking lot name from channel
808 * \note Channel needs to be locked while the returned string is in use.
810 static const char *findparkinglotname(struct ast_channel *chan)
814 /* The channel variable overrides everything */
815 name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
816 if (!name && !ast_strlen_zero(ast_channel_parkinglot(chan))) {
817 /* Use the channel's parking lot. */
818 name = ast_channel_parkinglot(chan);
823 /*! \brief Notify metermaids that we've changed an extension */
824 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
826 ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
827 exten, context, ast_devstate2str(state));
829 ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "park:%s@%s", exten, context);
832 /*! \brief metermaids callback from devicestate.c */
833 static enum ast_device_state metermaidstate(const char *data)
838 context = ast_strdupa(data);
840 exten = strsep(&context, "@");
842 return AST_DEVICE_INVALID;
844 ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
846 if (!ast_exists_extension(NULL, context, exten, 1, NULL))
847 return AST_DEVICE_NOT_INUSE;
849 return AST_DEVICE_INUSE;
852 /*! Options to pass to park_call_full */
853 enum ast_park_call_options {
854 /*! Provide ringing to the parked caller instead of music on hold */
855 AST_PARK_OPT_RINGING = (1 << 0),
856 /*! Randomly choose a parking spot for the caller instead of choosing
857 * the first one that is available. */
858 AST_PARK_OPT_RANDOMIZE = (1 << 1),
859 /*! Do not announce the parking number */
860 AST_PARK_OPT_SILENCE = (1 << 2),
863 /*! Optional additional parking options when parking a call. */
864 struct ast_park_call_args {
865 /*! How long to wait in the parking lot before the call gets sent back
866 * to the specified return extension (or a best guess at where it came
867 * from if not explicitly specified). */
869 /*! An output parameter to store the parking space where the parked caller
872 const char *orig_chan_name;
873 const char *return_con;
874 const char *return_ext;
877 /*! Parked user that has already obtained a parking space */
878 struct parkeduser *pu;
879 /*! \brief Parkinglot to be parked in */
880 struct ast_parkinglot *parkinglot;
885 * \brief Create a dynamic parking lot.
887 * \param name Dynamic parking lot name to create.
888 * \param chan Channel to get dynamic parking lot parameters.
890 * \retval parkinglot on success.
891 * \retval NULL on error.
893 static struct ast_parkinglot *create_dynamic_parkinglot(const char *name, struct ast_channel *chan)
895 const char *dyn_context;
896 const char *dyn_exten;
897 const char *dyn_range;
898 const char *template_name;
899 struct ast_parkinglot *template_parkinglot = NULL;
900 struct ast_parkinglot *parkinglot;
904 ast_channel_lock(chan);
905 template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
906 dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
907 dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
908 dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
909 ast_channel_unlock(chan);
911 if (!ast_strlen_zero(template_name)) {
912 template_parkinglot = find_parkinglot(template_name);
913 if (!template_parkinglot) {
914 ast_debug(1, "PARKINGDYNAMIC lot %s does not exist.\n",
916 } else if (template_parkinglot->cfg.is_invalid) {
917 ast_debug(1, "PARKINGDYNAMIC lot %s has invalid config.\n",
919 parkinglot_unref(template_parkinglot);
920 template_parkinglot = NULL;
923 if (!template_parkinglot) {
924 template_parkinglot = parkinglot_addref(default_parkinglot);
925 ast_debug(1, "Using default parking lot for template\n");
928 parkinglot = copy_parkinglot(name, template_parkinglot);
930 ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
932 /* Configure the dynamic parking lot. */
933 if (!ast_strlen_zero(dyn_context)) {
934 ast_copy_string(parkinglot->cfg.parking_con, dyn_context,
935 sizeof(parkinglot->cfg.parking_con));
937 if (!ast_strlen_zero(dyn_exten)) {
938 ast_copy_string(parkinglot->cfg.parkext, dyn_exten,
939 sizeof(parkinglot->cfg.parkext));
941 if (!ast_strlen_zero(dyn_range)) {
942 if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
944 "Format for parking positions is a-b, where a and b are numbers\n");
945 } else if (dyn_end < dyn_start || dyn_start <= 0 || dyn_end <= 0) {
947 "Format for parking positions is a-b, where a <= b\n");
949 parkinglot->cfg.parking_start = dyn_start;
950 parkinglot->cfg.parking_stop = dyn_end;
955 * Sanity check for dynamic parking lot configuration.
957 * XXX It may be desirable to instead check if the dynamic
958 * parking lot overlaps any existing lots like what is done for
961 if (!strcmp(parkinglot->cfg.parking_con, template_parkinglot->cfg.parking_con)) {
962 if (!strcmp(parkinglot->cfg.parkext, template_parkinglot->cfg.parkext)
963 && parkinglot->cfg.parkext_exclusive) {
965 "Parking lot '%s' conflicts with template parking lot '%s'!\n"
966 "Change either PARKINGDYNCONTEXT or PARKINGDYNEXTEN.\n",
967 parkinglot->name, template_parkinglot->name);
969 if ((template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_start
970 && parkinglot->cfg.parking_start <= template_parkinglot->cfg.parking_stop)
971 || (template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_stop
972 && parkinglot->cfg.parking_stop <= template_parkinglot->cfg.parking_stop)
973 || (parkinglot->cfg.parking_start < template_parkinglot->cfg.parking_start
974 && template_parkinglot->cfg.parking_stop < parkinglot->cfg.parking_stop)) {
976 "Parking lot '%s' parking spaces overlap template parking lot '%s'!\n"
977 "Change PARKINGDYNPOS.\n",
978 parkinglot->name, template_parkinglot->name);
982 parkinglot_activate(parkinglot);
983 ao2_link(parkinglots, parkinglot);
985 parkinglot_unref(template_parkinglot);
992 * \brief Abort parking a call that has not completed parking yet.
994 * \param pu Parked user item to clean up.
996 * \note The parking lot parkings list is locked on entry.
1000 static void park_space_abort(struct parkeduser *pu)
1002 struct ast_parkinglot *parkinglot;
1004 parkinglot = pu->parkinglot;
1006 /* Put back the parking space just allocated. */
1007 --parkinglot->next_parking_space;
1009 AST_LIST_REMOVE(&parkinglot->parkings, pu, list);
1011 AST_LIST_UNLOCK(&parkinglot->parkings);
1012 parkinglot_unref(parkinglot);
1018 * \brief Reserve a parking space in a parking lot for a call being parked.
1020 * \param park_me Channel being parked.
1021 * \param parker Channel parking the call.
1022 * \param args Optional additional parking options when parking a call.
1024 * \return Parked call descriptor or NULL if failed.
1025 * \note The parking lot list is locked if successful.
1027 static struct parkeduser *park_space_reserve(struct ast_channel *park_me, struct ast_channel *parker, struct ast_park_call_args *args)
1029 struct parkeduser *pu;
1031 int parking_space = -1;
1032 const char *parkinglotname;
1033 const char *parkingexten;
1034 struct parkeduser *cur;
1035 struct ast_parkinglot *parkinglot = NULL;
1037 if (args->parkinglot) {
1038 parkinglot = parkinglot_addref(args->parkinglot);
1039 parkinglotname = parkinglot->name;
1042 parkinglotname = findparkinglotname(parker);
1043 } else { /* parker was NULL, check park_me (ParkAndAnnounce / res_agi) */
1044 parkinglotname = findparkinglotname(park_me);
1046 if (!ast_strlen_zero(parkinglotname)) {
1047 parkinglot = find_parkinglot(parkinglotname);
1049 /* Parking lot is not specified, so use the default parking lot. */
1050 ast_debug(4, "This could be an indication channel driver needs updating, using default lot.\n");
1051 parkinglot = parkinglot_addref(default_parkinglot);
1055 /* Dynamically create parkinglot */
1056 if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
1057 parkinglot = create_dynamic_parkinglot(parkinglotname, park_me);
1061 ast_log(LOG_WARNING, "Parking lot not available to park %s.\n", ast_channel_name(park_me));
1065 ast_debug(1, "Parking lot: %s\n", parkinglot->name);
1066 if (parkinglot->disabled || parkinglot->cfg.is_invalid) {
1067 ast_log(LOG_WARNING, "Parking lot %s is not in a useable state.\n",
1069 parkinglot_unref(parkinglot);
1073 /* Allocate memory for parking data */
1074 if (!(pu = ast_calloc(1, sizeof(*pu)))) {
1075 parkinglot_unref(parkinglot);
1079 /* Lock parking list */
1080 AST_LIST_LOCK(&parkinglot->parkings);
1082 /* Check for channel variable PARKINGEXTEN */
1083 parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(park_me, "PARKINGEXTEN"), ""));
1084 if (!ast_strlen_zero(parkingexten)) {
1086 * \note The API forces us to specify a numeric parking slot, even
1087 * though the architecture would tend to support non-numeric extensions
1088 * (as are possible with SIP, for example). Hence, we enforce that
1089 * limitation here. If extout was not numeric, we could permit
1090 * arbitrary non-numeric extensions.
1092 if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space <= 0) {
1093 ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n",
1095 AST_LIST_UNLOCK(&parkinglot->parkings);
1096 parkinglot_unref(parkinglot);
1101 if (parking_space < parkinglot->cfg.parking_start
1102 || parkinglot->cfg.parking_stop < parking_space) {
1104 * Cannot allow park because parking lots are not setup for
1105 * spaces outside of the lot. (Things like dialplan hints don't
1106 * exist for outside lot space.)
1108 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is not in %s (%d-%d).\n",
1109 parking_space, parkinglot->name, parkinglot->cfg.parking_start,
1110 parkinglot->cfg.parking_stop);
1111 AST_LIST_UNLOCK(&parkinglot->parkings);
1112 parkinglot_unref(parkinglot);
1117 /* Check if requested parking space is in use. */
1118 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
1119 if (cur->parkingnum == parking_space) {
1120 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is already in use in %s\n",
1121 parking_space, parkinglot->name);
1122 AST_LIST_UNLOCK(&parkinglot->parkings);
1123 parkinglot_unref(parkinglot);
1129 /* PARKINGEXTEN is empty, so find a usable extension in the lot to park the call */
1130 int start; /* The first slot we look in the parkinglot. It can be randomized. */
1131 int start_checked = 0; /* flag raised once the first slot is checked */
1133 /* If using randomize mode, set start to random position on parking range */
1134 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
1135 start = ast_random() % (parkinglot->cfg.parking_stop - parkinglot->cfg.parking_start + 1);
1136 start += parkinglot->cfg.parking_start;
1137 } else if (parkinglot->cfg.parkfindnext
1138 && parkinglot->cfg.parking_start <= parkinglot->next_parking_space
1139 && parkinglot->next_parking_space <= parkinglot->cfg.parking_stop) {
1140 /* Start looking with the next parking space in the lot. */
1141 start = parkinglot->next_parking_space;
1143 /* Otherwise, just set it to the start position. */
1144 start = parkinglot->cfg.parking_start;
1147 /* free parking extension linear search: O(n^2) */
1148 for (i = start; ; i++) {
1149 /* If we are past the end, wrap around to the first parking slot*/
1150 if (i == parkinglot->cfg.parking_stop + 1) {
1151 i = parkinglot->cfg.parking_start;
1155 /* At this point, if start_checked, we've exhausted all the possible slots. */
1156 if (start_checked) {
1163 /* Search the list of parked calls already in use for i. If we find it, it's in use. */
1164 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
1165 if (cur->parkingnum == i) {
1170 /* We found a parking space. */
1175 if (parking_space == -1) {
1176 /* We did not find a parking space. Lot is full. */
1177 ast_log(LOG_WARNING, "No more parking spaces in %s\n", parkinglot->name);
1178 AST_LIST_UNLOCK(&parkinglot->parkings);
1179 parkinglot_unref(parkinglot);
1185 /* Prepare for next parking space search. */
1186 parkinglot->next_parking_space = parking_space + 1;
1188 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
1189 pu->notquiteyet = 1;
1190 pu->parkingnum = parking_space;
1191 pu->parkinglot = parkinglot;
1192 AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
1198 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
1200 struct parkeduser *pu = args->pu;
1201 const char *event_from; /*!< Channel name that is parking the call. */
1202 char app_data[AST_MAX_EXTENSION + AST_MAX_CONTEXT];
1205 args->pu = pu = park_space_reserve(chan, peer, args);
1211 ast_channel_appl_set(chan, "Parked Call");
1212 ast_channel_data_set(chan, NULL);
1216 /* Put the parked channel on hold if we have two different channels */
1218 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
1219 pu->hold_method = AST_CONTROL_RINGING;
1220 ast_indicate(chan, AST_CONTROL_RINGING);
1222 pu->hold_method = AST_CONTROL_HOLD;
1223 ast_indicate_data(chan, AST_CONTROL_HOLD,
1224 S_OR(pu->parkinglot->cfg.mohclass, NULL),
1225 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
1229 pu->start = ast_tvnow();
1230 /* XXX This line was changed to not use get_parkingtime. This is just a placeholder message, because
1231 * likely this entire function is going away.
1233 pu->parkingtime = args->timeout;
1235 *(args->extout) = pu->parkingnum;
1238 event_from = S_OR(args->orig_chan_name, ast_channel_name(peer));
1241 * This is so ugly that it hurts, but implementing
1242 * get_base_channel() on local channels could have ugly side
1243 * effects. We could have
1244 * transferer<->local;1<->local;2<->parking and we need the
1245 * callback name to be that of transferer. Since local;1/2 have
1246 * the same name we can be tricky and just grab the bridged
1247 * channel from the other side of the local.
1249 if (!strcasecmp(ast_channel_tech(peer)->type, "Local")) {
1250 struct ast_channel *tmpchan, *base_peer;
1251 char other_side[AST_CHANNEL_NAME];
1254 ast_copy_string(other_side, event_from, sizeof(other_side));
1255 if ((c = strrchr(other_side, ';'))) {
1258 if ((tmpchan = ast_channel_get_by_name(other_side))) {
1259 ast_channel_lock(tmpchan);
1260 if ((base_peer = ast_bridged_channel(tmpchan))) {
1261 ast_copy_string(pu->peername, ast_channel_name(base_peer), sizeof(pu->peername));
1263 ast_channel_unlock(tmpchan);
1264 tmpchan = ast_channel_unref(tmpchan);
1267 ast_copy_string(pu->peername, event_from, sizeof(pu->peername));
1270 event_from = S_OR(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"),
1271 ast_channel_name(chan));
1275 * Remember what had been dialed, so that if the parking
1276 * expires, we try to come back to the same place
1278 pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
1281 * If extension has options specified, they override all other
1282 * possibilities such as the returntoorigin flag and transferred
1283 * context. Information on extension options is lost here, so
1286 ast_copy_string(pu->context,
1287 S_OR(args->return_con, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan))),
1288 sizeof(pu->context));
1289 ast_copy_string(pu->exten,
1290 S_OR(args->return_ext, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan))),
1292 pu->priority = args->return_pri ? args->return_pri :
1293 (ast_channel_macropriority(chan) ? ast_channel_macropriority(chan) : ast_channel_priority(chan));
1296 * If parking a channel directly, don't quite yet get parking
1297 * running on it. All parking lot entries are put into the
1298 * parking lot with notquiteyet on.
1301 pu->notquiteyet = 0;
1304 /* Wake up the (presumably select()ing) thread */
1305 pthread_kill(parking_thread, SIGURG);
1306 ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %u seconds\n",
1307 ast_channel_name(chan), pu->parkingnum, pu->parkinglot->name,
1308 pu->context, pu->exten, pu->priority, (pu->parkingtime / 1000));
1311 <managerEventInstance>
1312 <synopsis>Raised when a call has been parked.</synopsis>
1314 <parameter name="Exten">
1315 <para>The parking lot extension.</para>
1317 <parameter name="Parkinglot">
1318 <para>The name of the parking lot.</para>
1320 <parameter name="From">
1321 <para>The name of the channel that parked the call.</para>
1325 <ref type="application">Park</ref>
1326 <ref type="manager">Park</ref>
1327 <ref type="managerEvent">ParkedCallTimeOut</ref>
1328 <ref type="managerEvent">ParkedCallGiveUp</ref>
1330 </managerEventInstance>
1332 ast_manager_event(chan, EVENT_FLAG_CALL, "ParkedCall",
1335 "Parkinglot: %s\r\n"
1338 "CallerIDNum: %s\r\n"
1339 "CallerIDName: %s\r\n"
1340 "ConnectedLineNum: %s\r\n"
1341 "ConnectedLineName: %s\r\n"
1343 pu->parkingexten, ast_channel_name(chan), pu->parkinglot->name, event_from,
1344 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
1345 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
1346 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>"),
1347 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, "<unknown>"),
1348 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, "<unknown>"),
1349 ast_channel_uniqueid(chan)
1351 ast_debug(4, "peer: %s\n", peer ? ast_channel_name(peer) : "-No peer-");
1352 ast_debug(4, "args->orig_chan_name: %s\n", args->orig_chan_name ? args->orig_chan_name : "-none-");
1353 ast_debug(4, "pu->peername: %s\n", pu->peername);
1354 ast_debug(4, "AMI ParkedCall Channel: %s\n", ast_channel_name(chan));
1355 ast_debug(4, "AMI ParkedCall From: %s\n", event_from);
1357 if (peer && adsipark && ast_adsi_available(peer)) {
1358 adsi_announce_park(peer, pu->parkingexten); /* Only supports parking numbers */
1359 ast_adsi_unload_session(peer);
1362 snprintf(app_data, sizeof(app_data), "%s,%s", pu->parkingexten,
1363 pu->parkinglot->name);
1365 AST_LIST_UNLOCK(&pu->parkinglot->parkings);
1367 /* Only say number if it's a number and the channel hasn't been masqueraded away */
1368 if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE)
1369 && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(ast_channel_name(peer), args->orig_chan_name))) {
1371 * If a channel is masqueraded into peer while playing back the
1372 * parking space number do not continue playing it back. This
1373 * is the case if an attended transfer occurs.
1375 ast_set_flag(ast_channel_flags(peer), AST_FLAG_MASQ_NOSTREAM);
1376 /* Tell the peer channel the number of the parking space */
1377 ast_say_digits(peer, pu->parkingnum, "", ast_channel_language(peer));
1378 ast_clear_flag(ast_channel_flags(peer), AST_FLAG_MASQ_NOSTREAM);
1380 if (peer == chan) { /* pu->notquiteyet = 1 */
1381 /* Wake up parking thread if we're really done */
1382 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
1383 pu->hold_method = AST_CONTROL_RINGING;
1384 ast_indicate(chan, AST_CONTROL_RINGING);
1386 pu->hold_method = AST_CONTROL_HOLD;
1387 ast_indicate_data(chan, AST_CONTROL_HOLD,
1388 S_OR(pu->parkinglot->cfg.mohclass, NULL),
1389 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
1391 pu->notquiteyet = 0;
1392 pthread_kill(parking_thread, SIGURG);
1397 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)
1401 const char *app_data;
1402 struct ast_exten *exten;
1403 struct park_app_args app_args;
1404 struct ast_park_call_args args = {
1409 if (!park_exten || !park_context) {
1410 return park_call_full(park_me, parker, &args);
1414 * Determiine if the specified park extension has an exclusive
1415 * parking lot to use.
1417 if (parker && parker != park_me) {
1418 ast_autoservice_start(park_me);
1420 exten = get_parking_exten(park_exten, parker, park_context);
1422 app_data = ast_get_extension_app_data(exten);
1426 parse = ast_strdupa(app_data);
1427 AST_STANDARD_APP_ARGS(app_args, parse);
1429 if (!ast_strlen_zero(app_args.pl_name)) {
1430 /* Find the specified exclusive parking lot */
1431 args.parkinglot = find_parkinglot(app_args.pl_name);
1432 if (!args.parkinglot && parkeddynamic) {
1433 args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
1437 if (parker && parker != park_me) {
1438 ast_autoservice_stop(park_me);
1441 res = park_call_full(park_me, parker, &args);
1442 if (args.parkinglot) {
1443 parkinglot_unref(args.parkinglot);
1448 int ast_park_call(struct ast_channel *park_me, struct ast_channel *parker, int timeout, const char *park_exten, int *extout)
1450 struct ast_park_call_args args = {
1455 return park_call_full(park_me, parker, &args);
1459 * \brief Park call via masqueraded channel and announce parking spot on peer channel.
1461 * \param rchan the real channel to be parked
1462 * \param peer the channel to have the parking read to.
1463 * \param args Additional parking options when parking a call.
1465 * \retval 0 on success.
1466 * \retval -1 on failure.
1468 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
1470 struct ast_channel *chan;
1472 /* Make a new, channel that we'll use to masquerade in the real one */
1473 chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, ast_channel_accountcode(rchan), ast_channel_exten(rchan),
1474 ast_channel_context(rchan), ast_channel_linkedid(rchan), ast_channel_amaflags(rchan), "Parked/%s", ast_channel_name(rchan));
1476 ast_log(LOG_WARNING, "Unable to create parked channel\n");
1477 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1478 if (peer == rchan) {
1479 /* Only have one channel to worry about. */
1480 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1482 /* Have two different channels to worry about. */
1483 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1489 args->pu = park_space_reserve(rchan, peer, args);
1492 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1493 if (peer == rchan) {
1494 /* Only have one channel to worry about. */
1495 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1497 /* Have two different channels to worry about. */
1498 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1504 /* Make formats okay */
1505 ast_format_copy(ast_channel_readformat(chan), ast_channel_readformat(rchan));
1506 ast_format_copy(ast_channel_writeformat(chan), ast_channel_writeformat(rchan));
1508 if (ast_channel_masquerade(chan, rchan)) {
1509 park_space_abort(args->pu);
1512 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
1513 if (peer == rchan) {
1514 /* Only have one channel to worry about. */
1515 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
1517 /* Have two different channels to worry about. */
1518 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
1524 /* Setup the extensions and such */
1525 set_c_e_p(chan, ast_channel_context(rchan), ast_channel_exten(rchan), ast_channel_priority(rchan));
1527 /* Setup the macro extension and such */
1528 ast_channel_macrocontext_set(chan, ast_channel_macrocontext(rchan));
1529 ast_channel_macroexten_set(chan, ast_channel_macroexten(rchan));
1530 ast_channel_macropriority_set(chan, ast_channel_macropriority(rchan));
1532 /* Manually do the masquerade to make sure it is complete. */
1533 ast_do_masquerade(chan);
1535 if (peer == rchan) {
1539 /* parking space reserved, return code check unnecessary */
1540 park_call_full(chan, peer, args);
1545 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)
1549 const char *app_data;
1550 struct ast_exten *exten;
1551 struct park_app_args app_args;
1552 struct ast_park_call_args args = {
1558 args.orig_chan_name = ast_strdupa(ast_channel_name(parker));
1560 if (!park_exten || !park_context) {
1561 return masq_park_call(park_me, parker, &args);
1565 * Determiine if the specified park extension has an exclusive
1566 * parking lot to use.
1568 if (parker && parker != park_me) {
1569 ast_autoservice_start(park_me);
1571 exten = get_parking_exten(park_exten, parker, park_context);
1573 app_data = ast_get_extension_app_data(exten);
1577 parse = ast_strdupa(app_data);
1578 AST_STANDARD_APP_ARGS(app_args, parse);
1580 if (!ast_strlen_zero(app_args.pl_name)) {
1581 /* Find the specified exclusive parking lot */
1582 args.parkinglot = find_parkinglot(app_args.pl_name);
1583 if (!args.parkinglot && parkeddynamic) {
1584 args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
1588 if (parker && parker != park_me) {
1589 ast_autoservice_stop(park_me);
1592 res = masq_park_call(park_me, parker, &args);
1593 if (args.parkinglot) {
1594 parkinglot_unref(args.parkinglot);
1599 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
1601 struct ast_park_call_args args = {
1607 args.orig_chan_name = ast_strdupa(ast_channel_name(peer));
1609 return masq_park_call(rchan, peer, &args);
1614 * \brief Play file to specified channel.
1616 * \param play_to Channel to play audiofile to.
1617 * \param other Channel to put in autoservice while playing file.
1618 * \param msg Descriptive name of message type being played.
1619 * \param audiofile Audio file to play.
1621 * \retval 0 on success.
1622 * \retval -1 on error. (Couldn't play file, a channel hung up,...)
1624 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile)
1626 /* Put other channel in autoservice. */
1627 if (ast_autoservice_start(other)) {
1630 ast_autoservice_ignore(other, AST_FRAME_DTMF_BEGIN);
1631 ast_autoservice_ignore(other, AST_FRAME_DTMF_END);
1632 if (ast_stream_and_wait(play_to, audiofile, "")) {
1633 ast_log(LOG_WARNING, "Failed to play %s '%s'!\n", msg, audiofile);
1634 ast_autoservice_stop(other);
1637 if (ast_autoservice_stop(other)) {
1645 * \brief Get the extension for a given builtin feature
1647 * \pre expects features_lock to be readlocked
1650 * \retval non-zero failiure
1652 static int builtin_feature_get_exten(struct ast_channel *chan, const char *feature_name,
1653 char *buf, size_t len)
1655 SCOPED_CHANNELLOCK(lock, chan);
1657 return ast_get_builtin_feature(chan, feature_name, buf, len);
1660 static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
1662 /* BUGBUG there is code that checks AST_BRIDGE_IGNORE_SIGS but no code to set it. */
1663 /* BUGBUG there is code that checks AST_BRIDGE_REC_CHANNEL_0 but no code to set it. */
1664 /* BUGBUG there is code that checks AST_BRIDGE_REC_CHANNEL_1 but no code to set it. */
1665 ast_clear_flag(config, AST_FLAGS_ALL);
1667 if (ast_test_flag(&config->features_caller, AST_FEATURE_DTMF_MASK)) {
1668 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
1670 if (ast_test_flag(&config->features_callee, AST_FEATURE_DTMF_MASK)) {
1671 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
1674 if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
1675 RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
1677 ast_channel_lock(chan);
1678 applicationmap = ast_get_chan_applicationmap(chan);
1679 ast_channel_unlock(chan);
1681 if (!applicationmap) {
1685 /* If an applicationmap exists for this channel at all, then the channel needs the DTMF flag set */
1686 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
1690 void ast_channel_log(char *title, struct ast_channel *chan);
1692 void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
1694 ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan);
1695 ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
1696 ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan),
1697 ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
1698 ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
1699 ast_channel_accountcode(chan), ast_channel_dialcontext(chan), ast_channel_amaflags(chan),
1700 ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan));
1701 ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; uniqueID: %s; linkedID:%s\n",
1702 ast_channel_masq(chan), ast_channel_masqr(chan),
1703 ast_channel_uniqueid(chan), ast_channel_linkedid(chan));
1704 if (ast_channel_masqr(chan)) {
1705 ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
1706 ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan)));
1709 ast_log(LOG_NOTICE, "===== done ====\n");
1712 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
1714 const char *feature;
1716 if (ast_strlen_zero(features)) {
1720 for (feature = features; *feature; feature++) {
1721 struct ast_flags *party;
1723 if (isupper(*feature)) {
1724 party = &config->features_caller;
1726 party = &config->features_callee;
1729 switch (tolower(*feature)) {
1731 ast_set_flag(party, AST_FEATURE_REDIRECT);
1734 ast_set_flag(party, AST_FEATURE_PARKCALL);
1737 ast_set_flag(party, AST_FEATURE_DISCONNECT);
1740 ast_set_flag(party, AST_FEATURE_AUTOMON);
1743 ast_set_flag(party, AST_FEATURE_AUTOMIXMON);
1746 ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
1752 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
1754 if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
1756 * If we don't return here, then when we do a builtin_atxfer we
1757 * will copy the disconnect flags over from the atxfer to the
1763 add_features_datastore(callee, &config->features_callee, &config->features_caller);
1766 static void clear_dialed_interfaces(struct ast_channel *chan)
1768 struct ast_datastore *di_datastore;
1770 ast_channel_lock(chan);
1771 if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
1773 ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", ast_channel_name(chan));
1775 if (!ast_channel_datastore_remove(chan, di_datastore)) {
1776 ast_datastore_free(di_datastore);
1779 ast_channel_unlock(chan);
1784 * \brief Helper to add a builtin DTMF feature hook to the features struct.
1787 * \param features Bridge features to setup.
1788 * \param chan Get features from this channel.
1789 * \param flags Feature flags on the channel.
1790 * \param feature_flag Feature flag to test.
1791 * \param feature_name features.conf name of feature.
1792 * \param feature_bridge Bridge feature enum to get hook callback.
1794 * \retval 0 on success.
1795 * \retval -1 on error.
1797 static int builtin_features_helper(struct ast_bridge_features *features, struct ast_channel *chan,
1798 struct ast_flags *flags, unsigned int feature_flag, const char *feature_name, enum ast_bridge_builtin_feature feature_bridge)
1800 char dtmf[AST_FEATURE_MAX_LEN];
1804 if (ast_test_flag(flags, feature_flag)
1805 && !builtin_feature_get_exten(chan, feature_name, dtmf, sizeof(dtmf))
1806 && !ast_strlen_zero(dtmf)) {
1807 res = ast_bridge_features_enable(features, feature_bridge, dtmf, NULL, NULL,
1808 AST_BRIDGE_HOOK_REMOVE_ON_PULL | AST_BRIDGE_HOOK_REMOVE_ON_PERSONALITY_CHANGE);
1810 ast_log(LOG_ERROR, "Channel %s: Requested DTMF feature %s not available.\n",
1811 ast_channel_name(chan), feature_name);
1820 * \brief Setup bridge builtin features.
1823 * \param features Bridge features to setup.
1824 * \param chan Get features from this channel.
1826 * \retval 0 on success.
1827 * \retval -1 on error.
1829 static int setup_bridge_features_builtin(struct ast_bridge_features *features, struct ast_channel *chan)
1831 struct ast_flags *flags;
1834 ast_channel_lock(chan);
1835 flags = ast_bridge_features_ds_get(chan);
1836 ast_channel_unlock(chan);
1842 res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "blindxfer", AST_BRIDGE_BUILTIN_BLINDTRANSFER);
1843 res |= builtin_features_helper(features, chan, flags, AST_FEATURE_REDIRECT, "atxfer", AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER);
1844 res |= builtin_features_helper(features, chan, flags, AST_FEATURE_DISCONNECT, "disconnect", AST_BRIDGE_BUILTIN_HANGUP);
1845 res |= builtin_features_helper(features, chan, flags, AST_FEATURE_PARKCALL, "parkcall", AST_BRIDGE_BUILTIN_PARKCALL);
1846 res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMON, "automon", AST_BRIDGE_BUILTIN_AUTOMON);
1847 res |= builtin_features_helper(features, chan, flags, AST_FEATURE_AUTOMIXMON, "automixmon", AST_BRIDGE_BUILTIN_AUTOMIXMON);
1849 return res ? -1 : 0;
1852 struct dynamic_dtmf_hook_run {
1853 /*! Offset into app_name[] where the channel name that activated the hook starts. */
1854 int activated_offset;
1855 /*! Offset into app_name[] where the dynamic feature name starts. */
1857 /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
1859 /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
1860 int app_args_offset;
1861 /*! Application name to run. */
1865 static void dynamic_dtmf_hook_callback(struct ast_bridge_channel *bridge_channel,
1866 const void *payload, size_t payload_size)
1868 struct ast_channel *chan = bridge_channel->chan;
1869 const struct dynamic_dtmf_hook_run *run_data = payload;
1871 pbx_builtin_setvar_helper(chan, "DYNAMIC_FEATURENAME",
1872 &run_data->app_name[run_data->feature_offset]);
1873 pbx_builtin_setvar_helper(chan, "DYNAMIC_WHO_ACTIVATED",
1874 &run_data->app_name[run_data->activated_offset]);
1876 ast_bridge_channel_run_app(bridge_channel, run_data->app_name,
1877 run_data->app_args_offset ? &run_data->app_name[run_data->app_args_offset] : NULL,
1878 run_data->moh_offset ? &run_data->app_name[run_data->moh_offset] : NULL);
1881 static int dynamic_dtmf_hook_run_callback(struct ast_bridge_channel *bridge_channel,
1882 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1884 callback(bridge_channel, payload, payload_size);
1888 struct dynamic_dtmf_hook_data {
1889 /*! Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER) */
1891 /*! Offset into app_name[] where the dynamic feature name starts. */
1893 /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
1895 /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
1896 int app_args_offset;
1897 /*! Application name to run. */
1903 * \brief Activated dynamic DTMF feature hook.
1906 * \param bridge The bridge that the channel is part of
1907 * \param bridge_channel Channel executing the feature
1908 * \param hook_pvt Private data passed in when the hook was created
1910 * \retval 0 Keep the callback hook.
1911 * \retval -1 Remove the callback hook.
1913 static int dynamic_dtmf_hook_trip(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
1915 struct dynamic_dtmf_hook_data *pvt = hook_pvt;
1916 int (*run_it)(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size);
1917 struct dynamic_dtmf_hook_run *run_data;
1918 const char *activated_name;
1923 size_t len_activated;
1926 /* Determine lengths of things. */
1927 len_name = strlen(pvt->app_name) + 1;
1928 len_args = pvt->app_args_offset ? strlen(&pvt->app_name[pvt->app_args_offset]) + 1 : 0;
1929 len_moh = pvt->moh_offset ? strlen(&pvt->app_name[pvt->moh_offset]) + 1 : 0;
1930 len_feature = strlen(&pvt->app_name[pvt->feature_offset]) + 1;
1931 ast_channel_lock(bridge_channel->chan);
1932 activated_name = ast_strdupa(ast_channel_name(bridge_channel->chan));
1933 ast_channel_unlock(bridge_channel->chan);
1934 len_activated = strlen(activated_name) + 1;
1935 len_data = sizeof(*run_data) + len_name + len_args + len_moh + len_feature + len_activated;
1937 /* Fill in dynamic feature run hook data. */
1938 run_data = ast_alloca(len_data);
1939 run_data->app_args_offset = len_args ? len_name : 0;
1940 run_data->moh_offset = len_moh ? len_name + len_args : 0;
1941 run_data->feature_offset = len_name + len_args + len_moh;
1942 run_data->activated_offset = len_name + len_args + len_moh + len_feature;
1943 strcpy(run_data->app_name, pvt->app_name);/* Safe */
1945 strcpy(&run_data->app_name[run_data->app_args_offset],
1946 &pvt->app_name[pvt->app_args_offset]);/* Safe */
1949 strcpy(&run_data->app_name[run_data->moh_offset],
1950 &pvt->app_name[pvt->moh_offset]);/* Safe */
1952 strcpy(&run_data->app_name[run_data->feature_offset],
1953 &pvt->app_name[pvt->feature_offset]);/* Safe */
1954 strcpy(&run_data->app_name[run_data->activated_offset], activated_name);/* Safe */
1956 if (ast_test_flag(pvt, AST_FEATURE_FLAG_ONPEER)) {
1957 run_it = ast_bridge_channel_write_callback;
1959 run_it = dynamic_dtmf_hook_run_callback;
1961 run_it(bridge_channel, dynamic_dtmf_hook_callback, run_data, len_data);
1967 * \brief Add a dynamic DTMF feature hook to the bridge features.
1970 * \param features Bridge features to setup.
1971 * \param flags Which side of bridge to run app (AST_FEATURE_FLAG_ONSELF/AST_FEATURE_FLAG_ONPEER).
1972 * \param dtmf DTMF trigger sequence.
1973 * \param feature_name Name of the dynamic feature.
1974 * \param app_name Dialplan application name to run.
1975 * \param app_args Dialplan application arguments. (Empty or NULL if no arguments)
1976 * \param moh_class MOH class to play to peer. (Empty or NULL if no MOH played)
1978 * \retval 0 on success.
1979 * \retval -1 on error.
1981 static int dynamic_dtmf_hook_add(struct ast_bridge_features *features, unsigned int flags, const char *dtmf, const char *feature_name, const char *app_name, const char *app_args, const char *moh_class)
1983 struct dynamic_dtmf_hook_data *hook_data;
1984 size_t len_name = strlen(app_name) + 1;
1985 size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
1986 size_t len_moh = ast_strlen_zero(moh_class) ? 0 : strlen(moh_class) + 1;
1987 size_t len_feature = strlen(feature_name) + 1;
1988 size_t len_data = sizeof(*hook_data) + len_name + len_args + len_moh + len_feature;
1991 /* Fill in application run hook data. */
1992 hook_data = ast_malloc(len_data);
1996 hook_data->flags = flags;
1997 hook_data->app_args_offset = len_args ? len_name : 0;
1998 hook_data->moh_offset = len_moh ? len_name + len_args : 0;
1999 hook_data->feature_offset = len_name + len_args + len_moh;
2000 strcpy(hook_data->app_name, app_name);/* Safe */
2002 strcpy(&hook_data->app_name[hook_data->app_args_offset], app_args);/* Safe */
2005 strcpy(&hook_data->app_name[hook_data->moh_offset], moh_class);/* Safe */
2007 strcpy(&hook_data->app_name[hook_data->feature_offset], feature_name);/* Safe */
2009 res = ast_bridge_dtmf_hook(features, dtmf, dynamic_dtmf_hook_trip, hook_data,
2010 ast_free_ptr, AST_BRIDGE_HOOK_REMOVE_ON_PULL);
2012 ast_free(hook_data);
2017 static int setup_dynamic_feature(void *obj, void *arg, void *data, int flags)
2019 struct ast_applicationmap_item *item = obj;
2020 struct ast_bridge_features *features = arg;
2023 *res |= dynamic_dtmf_hook_add(features,
2024 item->activate_on_self ? AST_FEATURE_FLAG_ONSELF : AST_FEATURE_FLAG_ONPEER,
2025 item->dtmf, item->name, item->app, item->app_data, item->moh_class);
2032 * \brief Setup bridge dynamic features.
2035 * \param features Bridge features to setup.
2036 * \param chan Get features from this channel.
2038 * \retval 0 on success.
2039 * \retval -1 on error.
2041 static int setup_bridge_features_dynamic(struct ast_bridge_features *features, struct ast_channel *chan)
2043 RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
2046 ast_channel_lock(chan);
2047 applicationmap = ast_get_chan_applicationmap(chan);
2048 ast_channel_unlock(chan);
2049 if (!applicationmap) {
2053 ao2_callback_data(applicationmap, 0, setup_dynamic_feature, features, &res);
2058 /* BUGBUG this really should be made a private function of bridging_basic.c after struct ast_call_feature is made an ao2 object. */
2059 int ast_bridge_channel_setup_features(struct ast_bridge_channel *bridge_channel)
2063 /* Always pass through any DTMF digits. */
2064 bridge_channel->features->dtmf_passthrough = 1;
2066 res |= setup_bridge_features_builtin(bridge_channel->features, bridge_channel->chan);
2067 res |= setup_bridge_features_dynamic(bridge_channel->features, bridge_channel->chan);
2072 static void bridge_config_set_limits_warning_values(struct ast_bridge_config *config, struct ast_bridge_features_limits *limits)
2074 if (config->end_sound) {
2075 ast_string_field_set(limits, duration_sound, config->end_sound);
2078 if (config->warning_sound) {
2079 ast_string_field_set(limits, warning_sound, config->warning_sound);
2082 if (config->start_sound) {
2083 ast_string_field_set(limits, connect_sound, config->start_sound);
2086 limits->frequency = config->warning_freq;
2087 limits->warning = config->play_warning;
2091 * \internal brief Setup limit hook structures on calls that need limits
2093 * \param config ast_bridge_config which provides the limit data
2094 * \param caller_limits pointer to an ast_bridge_features_limits struct which will store the caller side limits
2095 * \param callee_limits pointer to an ast_bridge_features_limits struct which will store the callee side limits
2097 static void bridge_config_set_limits(struct ast_bridge_config *config, struct ast_bridge_features_limits *caller_limits, struct ast_bridge_features_limits *callee_limits)
2099 if (ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING)) {
2100 bridge_config_set_limits_warning_values(config, caller_limits);
2103 if (ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING)) {
2104 bridge_config_set_limits_warning_values(config, callee_limits);
2107 caller_limits->duration = config->timelimit;
2108 callee_limits->duration = config->timelimit;
2113 * \brief Check if Monitor needs to be started on a channel.
2116 * \param chan The bridge considers this channel the caller.
2117 * \param peer The bridge considers this channel the callee.
2121 static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *peer)
2124 const char *monitor_args = NULL;
2125 struct ast_channel *monitor_chan = NULL;
2127 ast_channel_lock(chan);
2128 value = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR");
2129 if (!ast_strlen_zero(value)) {
2130 monitor_args = ast_strdupa(value);
2131 monitor_chan = chan;
2133 ast_channel_unlock(chan);
2134 if (!monitor_chan) {
2135 ast_channel_lock(peer);
2136 value = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR");
2137 if (!ast_strlen_zero(value)) {
2138 monitor_args = ast_strdupa(value);
2139 monitor_chan = peer;
2141 ast_channel_unlock(peer);
2144 struct ast_app *monitor_app;
2146 monitor_app = pbx_findapp("Monitor");
2148 pbx_exec(monitor_chan, monitor_app, monitor_args);
2155 * \brief Send the peer channel on its way on bridge start failure.
2158 * \param chan Chan to put into autoservice.
2159 * \param peer Chan to send to after bridge goto or run hangup handlers and hangup.
2163 static void bridge_failed_peer_goto(struct ast_channel *chan, struct ast_channel *peer)
2165 if (ast_bridge_setup_after_goto(peer)
2166 || ast_pbx_start(peer)) {
2167 ast_autoservice_chan_hangup_peer(chan, peer);
2171 static int pre_bridge_setup(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config,
2172 struct ast_bridge_features *chan_features, struct ast_bridge_features *peer_features)
2176 set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
2177 add_features_datastores(chan, peer, config);
2180 * This is an interesting case. One example is if a ringing
2181 * channel gets redirected to an extension that picks up a
2182 * parked call. This will make sure that the call taken out of
2183 * parking gets told that the channel it just got bridged to is
2186 if (ast_channel_state(chan) == AST_STATE_RINGING
2187 && ast_channel_visible_indication(peer) != AST_CONTROL_RINGING) {
2188 ast_indicate(peer, AST_CONTROL_RINGING);
2191 bridge_check_monitor(chan, peer);
2193 set_config_flags(chan, config);
2195 /* Answer if need be */
2196 if (ast_channel_state(chan) != AST_STATE_UP) {
2197 if (ast_raw_answer(chan)) {
2203 /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
2204 ast_channel_log("Pre-bridge CHAN Channel info", chan);
2205 ast_channel_log("Pre-bridge PEER Channel info", peer);
2209 * If we are bridging a call, stop worrying about forwarding
2210 * loops. We presume that if a call is being bridged, that the
2211 * humans in charge know what they're doing. If they don't,
2212 * well, what can we do about that?
2214 clear_dialed_interfaces(chan);
2215 clear_dialed_interfaces(peer);
2218 ast_channel_lock(chan);
2219 res |= ast_bridge_features_ds_set(chan, &config->features_caller);
2220 ast_channel_unlock(chan);
2221 ast_channel_lock(peer);
2222 res |= ast_bridge_features_ds_set(peer, &config->features_callee);
2223 ast_channel_unlock(peer);
2229 if (config->timelimit) {
2230 struct ast_bridge_features_limits call_duration_limits_chan;
2231 struct ast_bridge_features_limits call_duration_limits_peer;
2232 int abandon_call = 0; /* TRUE if set limits fails so we can abandon the call. */
2234 if (ast_bridge_features_limits_construct(&call_duration_limits_chan)) {
2235 ast_log(LOG_ERROR, "Could not construct caller duration limits. Bridge canceled.\n");
2240 if (ast_bridge_features_limits_construct(&call_duration_limits_peer)) {
2241 ast_log(LOG_ERROR, "Could not construct callee duration limits. Bridge canceled.\n");
2242 ast_bridge_features_limits_destroy(&call_duration_limits_chan);
2247 bridge_config_set_limits(config, &call_duration_limits_chan, &call_duration_limits_peer);
2249 if (ast_bridge_features_set_limits(chan_features, &call_duration_limits_chan, 0)) {
2252 if (ast_bridge_features_set_limits(peer_features, &call_duration_limits_peer, 0)) {
2256 /* At this point we are done with the limits structs since they have been copied to the individual feature sets. */
2257 ast_bridge_features_limits_destroy(&call_duration_limits_chan);
2258 ast_bridge_features_limits_destroy(&call_duration_limits_peer);
2261 ast_log(LOG_ERROR, "Could not set duration limits on one or more sides of the call. Bridge canceled.\n");
2270 * \brief bridge the call and set CDR
2272 * \param chan The bridge considers this channel the caller.
2273 * \param peer The bridge considers this channel the callee.
2274 * \param config Configuration for this bridge.
2276 * Set start time, check for two channels,check if monitor on
2277 * check for feature activation, create new CDR
2278 * \retval res on success.
2279 * \retval -1 on failure to bridge.
2281 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
2284 struct ast_bridge *bridge;
2285 struct ast_bridge_features chan_features;
2286 struct ast_bridge_features *peer_features;
2288 /* Setup features. */
2289 res = ast_bridge_features_init(&chan_features);
2290 peer_features = ast_bridge_features_new();
2291 if (res || !peer_features) {
2292 ast_bridge_features_destroy(peer_features);
2293 ast_bridge_features_cleanup(&chan_features);
2294 bridge_failed_peer_goto(chan, peer);
2298 if (pre_bridge_setup(chan, peer, config, &chan_features, peer_features)) {
2299 ast_bridge_features_destroy(peer_features);
2300 ast_bridge_features_cleanup(&chan_features);
2301 bridge_failed_peer_goto(chan, peer);
2306 bridge = ast_bridge_basic_new();
2308 ast_bridge_features_destroy(peer_features);
2309 ast_bridge_features_cleanup(&chan_features);
2310 bridge_failed_peer_goto(chan, peer);
2314 /* Put peer into the bridge */
2315 if (ast_bridge_impart(bridge, peer, NULL, peer_features, 1)) {
2316 ast_bridge_destroy(bridge);
2317 ast_bridge_features_cleanup(&chan_features);
2318 bridge_failed_peer_goto(chan, peer);
2323 ast_bridge_join(bridge, chan, NULL, &chan_features, NULL, 1);
2326 * If the bridge was broken for a hangup that isn't real, then
2327 * don't run the h extension, because the channel isn't really
2328 * hung up. This should really only happen with
2329 * AST_SOFTHANGUP_ASYNCGOTO.
2332 ast_channel_lock(chan);
2333 if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
2336 ast_channel_unlock(chan);
2338 ast_bridge_features_cleanup(&chan_features);
2340 /* BUGBUG this is used by Dial and FollowMe for CDR information. By Queue for Queue stats like CDRs. */
2341 if (res && config->end_bridge_callback) {
2342 config->end_bridge_callback(config->end_bridge_callback_data);
2348 /*! \brief Output parking event to manager */
2349 static void post_manager_event(const char *s, struct parkeduser *pu)
2351 manager_event(EVENT_FLAG_CALL, s,
2354 "Parkinglot: %s\r\n"
2355 "CallerIDNum: %s\r\n"
2356 "CallerIDName: %s\r\n"
2357 "ConnectedLineNum: %s\r\n"
2358 "ConnectedLineName: %s\r\n"
2361 ast_channel_name(pu->chan),
2362 pu->parkinglot->name,
2363 S_COR(ast_channel_caller(pu->chan)->id.number.valid, ast_channel_caller(pu->chan)->id.number.str, "<unknown>"),
2364 S_COR(ast_channel_caller(pu->chan)->id.name.valid, ast_channel_caller(pu->chan)->id.name.str, "<unknown>"),
2365 S_COR(ast_channel_connected(pu->chan)->id.number.valid, ast_channel_connected(pu->chan)->id.number.str, "<unknown>"),
2366 S_COR(ast_channel_connected(pu->chan)->id.name.valid, ast_channel_connected(pu->chan)->id.name.str, "<unknown>"),
2367 ast_channel_uniqueid(pu->chan)
2371 static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_flags *features_caller, char *options, size_t len)
2375 OPT_CALLEE_REDIRECT = 't',
2376 OPT_CALLER_REDIRECT = 'T',
2377 OPT_CALLEE_AUTOMON = 'w',
2378 OPT_CALLER_AUTOMON = 'W',
2379 OPT_CALLEE_DISCONNECT = 'h',
2380 OPT_CALLER_DISCONNECT = 'H',
2381 OPT_CALLEE_PARKCALL = 'k',
2382 OPT_CALLER_PARKCALL = 'K',
2385 memset(options, 0, len);
2386 if (ast_test_flag(features_caller, AST_FEATURE_REDIRECT) && i < len) {
2387 options[i++] = OPT_CALLER_REDIRECT;
2389 if (ast_test_flag(features_caller, AST_FEATURE_AUTOMON) && i < len) {
2390 options[i++] = OPT_CALLER_AUTOMON;
2392 if (ast_test_flag(features_caller, AST_FEATURE_DISCONNECT) && i < len) {
2393 options[i++] = OPT_CALLER_DISCONNECT;
2395 if (ast_test_flag(features_caller, AST_FEATURE_PARKCALL) && i < len) {
2396 options[i++] = OPT_CALLER_PARKCALL;
2399 if (ast_test_flag(features_callee, AST_FEATURE_REDIRECT) && i < len) {
2400 options[i++] = OPT_CALLEE_REDIRECT;
2402 if (ast_test_flag(features_callee, AST_FEATURE_AUTOMON) && i < len) {
2403 options[i++] = OPT_CALLEE_AUTOMON;
2405 if (ast_test_flag(features_callee, AST_FEATURE_DISCONNECT) && i < len) {
2406 options[i++] = OPT_CALLEE_DISCONNECT;
2408 if (ast_test_flag(features_callee, AST_FEATURE_PARKCALL) && i < len) {
2409 options[i++] = OPT_CALLEE_PARKCALL;
2417 * \brief Run management on a parked call.
2419 * \note The parkinglot parkings list is locked on entry.
2421 * \retval TRUE if the parking completed.
2423 static int manage_parked_call(struct parkeduser *pu, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
2425 struct ast_channel *chan = pu->chan; /* shorthand */
2426 int tms; /* timeout for this item */
2427 int x; /* fd index in channel */
2429 tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
2430 if (tms > pu->parkingtime) {
2432 * Call has been parked too long.
2433 * Stop entertaining the caller.
2435 switch (pu->hold_method) {
2436 case AST_CONTROL_HOLD:
2437 ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
2439 case AST_CONTROL_RINGING:
2440 ast_indicate(pu->chan, -1);
2445 pu->hold_method = 0;
2447 /* Get chan, exten from derived kludge */
2448 if (pu->peername[0]) {
2451 char *peername_flat; /* using something like DAHDI/52 for an extension name is NOT a good idea */
2452 char parkingslot[AST_MAX_EXTENSION]; /* buffer for parkinglot slot number */
2455 peername = ast_strdupa(pu->peername);
2456 dash = strrchr(peername, '-');
2461 peername_flat = ast_strdupa(peername);
2462 for (i = 0; peername_flat[i]; i++) {
2463 if (peername_flat[i] == '/') {
2464 peername_flat[i] = '_';
2468 if (!ast_context_find_or_create(NULL, NULL, parking_con_dial, registrar)) {
2470 "Parking dial context '%s' does not exist and unable to create\n",
2473 char returnexten[AST_MAX_EXTENSION];
2474 char comebackdialtime[AST_MAX_EXTENSION];
2475 struct ast_datastore *features_datastore;
2476 struct ast_dial_features *dialfeatures;
2478 if (!strncmp(peername, "Parked/", 7)) {
2482 ast_channel_lock(chan);
2483 features_datastore = ast_channel_datastore_find(chan, &dial_features_info,
2485 if (features_datastore && (dialfeatures = features_datastore->data)) {
2486 char buf[MAX_DIAL_FEATURE_OPTIONS] = {0,};
2488 snprintf(returnexten, sizeof(returnexten), "%s,%u,%s", peername,
2489 pu->parkinglot->cfg.comebackdialtime,
2490 callback_dialoptions(&dialfeatures->peer_features,
2491 &dialfeatures->my_features, buf, sizeof(buf)));
2492 } else { /* Existing default */
2493 ast_log(LOG_NOTICE, "Dial features not found on %s, using default!\n",
2494 ast_channel_name(chan));
2495 snprintf(returnexten, sizeof(returnexten), "%s,%u,t", peername,
2496 pu->parkinglot->cfg.comebackdialtime);
2498 ast_channel_unlock(chan);
2500 snprintf(comebackdialtime, sizeof(comebackdialtime), "%u",
2501 pu->parkinglot->cfg.comebackdialtime);
2502 pbx_builtin_setvar_helper(chan, "COMEBACKDIALTIME", comebackdialtime);
2504 pbx_builtin_setvar_helper(chan, "PARKER", peername);
2508 snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
2509 pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parkingslot);
2510 pbx_builtin_setvar_helper(chan, "PARKEDLOT", pu->parkinglot->name);
2512 if (pu->options_specified) {
2514 * Park() was called with overriding return arguments, respect
2517 set_c_e_p(chan, pu->context, pu->exten, pu->priority);
2518 } else if (pu->parkinglot->cfg.comebacktoorigin) {
2519 set_c_e_p(chan, parking_con_dial, peername_flat, 1);
2521 /* Handle fallback when extensions don't exist here since that logic was removed from pbx */
2522 if (ast_exists_extension(chan, pu->parkinglot->cfg.comebackcontext, peername_flat, 1, NULL)) {
2523 set_c_e_p(chan, pu->parkinglot->cfg.comebackcontext, peername_flat, 1);
2524 } else if (ast_exists_extension(chan, pu->parkinglot->cfg.comebackcontext, "s", 1, NULL)) {
2525 ast_verb(2, "Can not start %s at %s,%s,1. Using 's@%s' instead.\n", ast_channel_name(chan),
2526 pu->parkinglot->cfg.comebackcontext, peername_flat, pu->parkinglot->cfg.comebackcontext);
2527 set_c_e_p(chan, pu->parkinglot->cfg.comebackcontext, "s", 1);
2529 ast_verb(2, "Can not start %s at %s,%s,1 and exten 's@%s' does not exist. Using 's@default'\n",
2530 ast_channel_name(chan),
2531 pu->parkinglot->cfg.comebackcontext, peername_flat,
2532 pu->parkinglot->cfg.comebackcontext);
2533 set_c_e_p(chan, "default", "s", 1);
2538 * They've been waiting too long, send them back to where they
2539 * came. Theoretically they should have their original
2540 * extensions and such, but we copy to be on the safe side.
2542 set_c_e_p(chan, pu->context, pu->exten, pu->priority);
2544 post_manager_event("ParkedCallTimeOut", pu);
2546 ast_verb(2, "Timeout for %s parked on %d (%s). Returning to %s,%s,%d\n",
2547 ast_channel_name(pu->chan), pu->parkingnum, pu->parkinglot->name, ast_channel_context(pu->chan),
2548 ast_channel_exten(pu->chan), ast_channel_priority(pu->chan));
2550 /* Start up the PBX, or hang them up */
2551 if (ast_pbx_start(chan)) {
2552 ast_log(LOG_WARNING,
2553 "Unable to restart the PBX for user on '%s', hanging them up...\n",
2554 ast_channel_name(pu->chan));
2558 /* And take them out of the parking lot */
2562 /* still within parking time, process descriptors */
2564 for (x = 0; x < AST_MAX_FDS; x++) {
2565 struct ast_frame *f;
2568 if (!ast_channel_fd_isset(chan, x)) {
2569 continue; /* nothing on this descriptor */
2572 for (y = 0; y < nfds; y++) {
2573 if (pfds[y].fd == ast_channel_fd(chan, x)) {
2574 /* Found poll record! */
2583 if (!(pfds[y].revents & (POLLIN | POLLERR | POLLPRI))) {
2588 if (pfds[y].revents & POLLPRI) {
2589 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
2591 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
2593 ast_channel_fdno_set(chan, x);
2595 /* See if they need servicing */
2596 f = ast_read(pu->chan);
2598 if (!f || (f->frametype == AST_FRAME_CONTROL
2599 && f->subclass.integer == AST_CONTROL_HANGUP)) {
2603 post_manager_event("ParkedCallGiveUp", pu);
2605 /* There's a problem, hang them up */
2606 ast_verb(2, "%s got tired of being parked\n", ast_channel_name(chan));
2609 /* And take them out of the parking lot */
2612 /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
2614 if (pu->hold_method == AST_CONTROL_HOLD
2616 && !ast_channel_generatordata(chan)) {
2618 "MOH on parked call stopped by outside source. Restarting on channel %s.\n",
2619 ast_channel_name(chan));
2620 ast_indicate_data(chan, AST_CONTROL_HOLD,
2621 S_OR(pu->parkinglot->cfg.mohclass, NULL),
2622 (!ast_strlen_zero(pu->parkinglot->cfg.mohclass)
2623 ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0));
2631 /* mark fds for next round */
2632 for (x = 0; x < AST_MAX_FDS; x++) {
2633 if (ast_channel_fd_isset(chan, x)) {
2634 void *tmp = ast_realloc(*new_pfds,
2635 (*new_nfds + 1) * sizeof(struct pollfd));
2641 (*new_pfds)[*new_nfds].fd = ast_channel_fd(chan, x);
2642 (*new_pfds)[*new_nfds].events = POLLIN | POLLERR | POLLPRI;
2643 (*new_pfds)[*new_nfds].revents = 0;
2647 /* Keep track of our shortest wait */
2648 if (tms < *ms || *ms < 0) {
2652 /* Stay in the parking lot. */
2656 /*! \brief Run management on parkinglots, called once per parkinglot */
2657 static void manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
2659 struct parkeduser *pu;
2660 struct ast_context *con;
2662 /* Lock parkings list */
2663 AST_LIST_LOCK(&curlot->parkings);
2664 AST_LIST_TRAVERSE_SAFE_BEGIN(&curlot->parkings, pu, list) {
2665 if (pu->notquiteyet) { /* Pretend this one isn't here yet */
2668 if (manage_parked_call(pu, pfds, nfds, new_pfds, new_nfds, ms)) {
2669 /* Parking is complete for this call so remove it from the parking lot. */
2670 con = ast_context_find(pu->parkinglot->cfg.parking_con);
2672 if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
2673 ast_log(LOG_WARNING,
2674 "Whoa, failed to remove the parking extension %s@%s!\n",
2675 pu->parkingexten, pu->parkinglot->cfg.parking_con);
2677 notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con,
2678 AST_DEVICE_NOT_INUSE);
2680 ast_log(LOG_WARNING,
2681 "Whoa, parking lot '%s' context '%s' does not exist.\n",
2682 pu->parkinglot->name, pu->parkinglot->cfg.parking_con);
2684 AST_LIST_REMOVE_CURRENT(list);
2685 parkinglot_unref(pu->parkinglot);
2689 AST_LIST_TRAVERSE_SAFE_END;
2690 AST_LIST_UNLOCK(&curlot->parkings);
2694 * \brief Take care of parked calls and unpark them if needed
2695 * \param ignore unused var.
2697 * Start inf loop, lock parking lot, check if any parked channels have gone above timeout
2698 * if so, remove channel from parking lot and return it to the extension that parked it.
2699 * Check if parked channel decided to hangup, wait until next FD via select().
2701 static void *do_parking_thread(void *ignore)
2703 struct pollfd *pfds = NULL, *new_pfds = NULL;
2704 int nfds = 0, new_nfds = 0;
2707 struct ao2_iterator iter;
2708 struct ast_parkinglot *curlot;
2709 int ms = -1; /* poll2 timeout, uninitialized */
2711 iter = ao2_iterator_init(parkinglots, 0);
2712 while ((curlot = ao2_iterator_next(&iter))) {
2713 manage_parkinglot(curlot, pfds, nfds, &new_pfds, &new_nfds, &ms);
2714 ao2_ref(curlot, -1);
2716 ao2_iterator_destroy(&iter);
2725 /* Wait for something to happen */
2726 ast_poll(pfds, nfds, ms);
2727 pthread_testcancel();
2729 /* If this WERE reached, we'd need to free(pfds) */
2730 return NULL; /* Never reached */
2733 /*! \brief Find parkinglot by name */
2734 static struct ast_parkinglot *find_parkinglot(const char *name)
2736 struct ast_parkinglot *parkinglot;
2738 if (ast_strlen_zero(name)) {
2742 parkinglot = ao2_find(parkinglots, (void *) name, 0);
2744 ast_debug(1, "Found Parking lot: %s\n", parkinglot->name);
2750 /*! \brief Copy parkinglot and store it with new name */
2751 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot)
2753 struct ast_parkinglot *copylot;
2755 if ((copylot = find_parkinglot(name))) { /* Parkinglot with that name already exists */
2756 ao2_ref(copylot, -1);
2760 copylot = create_parkinglot(name);
2765 ast_debug(1, "Building parking lot %s\n", name);
2767 /* Copy the source parking lot configuration. */
2768 copylot->cfg = parkinglot->cfg;
2773 AST_APP_OPTIONS(park_call_options, BEGIN_OPTIONS
2774 AST_APP_OPTION('r', AST_PARK_OPT_RINGING),
2775 AST_APP_OPTION('R', AST_PARK_OPT_RANDOMIZE),
2776 AST_APP_OPTION('s', AST_PARK_OPT_SILENCE),
2780 * \brief Unreference parkinglot object.
2782 static void parkinglot_unref(struct ast_parkinglot *parkinglot)
2784 ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name,
2785 ao2_ref(parkinglot, 0) - 1);
2786 ao2_ref(parkinglot, -1);
2789 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot)
2793 refcount = ao2_ref(parkinglot, +1);
2794 ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount + 1);
2798 /*! \brief Destroy a parking lot */
2799 static void parkinglot_destroy(void *obj)
2801 struct ast_parkinglot *doomed = obj;
2804 * No need to destroy parked calls here because any parked call
2805 * holds a parking lot reference. Therefore the parkings list
2808 ast_assert(AST_LIST_EMPTY(&doomed->parkings));
2809 AST_LIST_HEAD_DESTROY(&doomed->parkings);
2812 /*! \brief Allocate parking lot structure */
2813 static struct ast_parkinglot *create_parkinglot(const char *name)
2815 struct ast_parkinglot *newlot;
2817 if (ast_strlen_zero(name)) { /* No name specified */
2821 newlot = ao2_alloc(sizeof(*newlot), parkinglot_destroy);
2825 ast_copy_string(newlot->name, name, sizeof(newlot->name));
2826 newlot->cfg.is_invalid = 1;/* No config is set yet. */
2827 AST_LIST_HEAD_INIT(&newlot->parkings);
2832 /*! Default configuration for default parking lot. */
2833 static const struct parkinglot_cfg parkinglot_cfg_default_default = {
2834 .mohclass = "default",
2835 .parkext = DEFAULT_PARK_EXTENSION,
2836 .parking_con = "parkedcalls",
2837 .parking_start = 701,
2838 .parking_stop = 750,
2839 .parkingtime = DEFAULT_PARK_TIME,
2840 .comebackdialtime = DEFAULT_COMEBACK_DIAL_TIME,
2841 .comebackcontext = DEFAULT_COMEBACK_CONTEXT,
2842 .comebacktoorigin = DEFAULT_COMEBACK_TO_ORIGIN,
2845 /*! Default configuration for normal parking lots. */
2846 static const struct parkinglot_cfg parkinglot_cfg_default = {
2847 .parkext = DEFAULT_PARK_EXTENSION,
2848 .parkingtime = DEFAULT_PARK_TIME,
2849 .comebackdialtime = DEFAULT_COMEBACK_DIAL_TIME,
2850 .comebackcontext = DEFAULT_COMEBACK_CONTEXT,
2851 .comebacktoorigin = DEFAULT_COMEBACK_TO_ORIGIN,
2856 * \brief Activate the given parkinglot.
2858 * \param parkinglot Parking lot to activate.
2861 * Insert into the dialplan the context, parking lot access
2862 * extension, and optional dialplan hints.
2864 * \retval 0 on success.
2865 * \retval -1 on error.
2867 static int parkinglot_activate(struct ast_parkinglot *parkinglot)
2869 /* XXX All parking stuff is being replaced by res_parking */
2870 parkinglot->disabled = 1;
2874 int ast_features_reload(void)
2876 struct ast_context *con;
2879 ast_mutex_lock(&features_reload_lock);/* Searialize reloading features.conf */
2882 * Always destroy the parking_con_dial context to remove buildup
2883 * of recalled extensions in the context. At worst, the parked
2884 * call gets hungup attempting to run an invalid extension when
2885 * we are trying to callback the parker or the preset return
2886 * extension. This is a small window of opportunity on an
2887 * execution chain that is not expected to happen very often.
2889 con = ast_context_find(parking_con_dial);
2891 ast_context_destroy(con, registrar);
2894 res = ast_features_config_reload();
2895 ast_mutex_unlock(&features_reload_lock);
2900 static char *handle_features_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2904 e->command = "features reload";
2906 "Usage: features reload\n"
2907 " Reloads configured call features from features.conf\n";
2912 ast_features_reload();
2917 enum play_tone_action {
2919 PLAYTONE_CHANNEL1 = (1 << 0),
2920 PLAYTONE_CHANNEL2 = (1 << 1),
2921 PLAYTONE_BOTH = PLAYTONE_CHANNEL1 | PLAYTONE_CHANNEL2,
2924 static enum play_tone_action parse_playtone(const char *playtone_val)
2926 if (ast_strlen_zero(playtone_val) || ast_false(playtone_val)) {
2927 return PLAYTONE_NONE;
2928 } if (!strcasecmp(playtone_val, "channel1")) {
2929 return PLAYTONE_CHANNEL1;
2930 } else if (!strcasecmp(playtone_val, "channel2") || ast_true(playtone_val)) {
2931 return PLAYTONE_CHANNEL2;
2932 } else if (!strcasecmp(playtone_val, "both")) {
2933 return PLAYTONE_BOTH;
2935 /* Invalid input. Assume none */
2936 return PLAYTONE_NONE;
2941 * \brief Bridge channels together
2945 * Make sure valid channels were specified,
2946 * send errors if any of the channels could not be found/locked, answer channels if needed,
2947 * create the placeholder channels and grab the other channels
2948 * make the channels compatible, send error if we fail doing so
2949 * setup the bridge thread object and start the bridge.
2953 static int action_bridge(struct mansession *s, const struct message *m)
2955 const char *channela = astman_get_header(m, "Channel1");
2956 const char *channelb = astman_get_header(m, "Channel2");
2957 enum play_tone_action playtone = parse_playtone(astman_get_header(m, "Tone"));
2958 RAII_VAR(struct ast_channel *, chana, NULL, ao2_cleanup);
2959 RAII_VAR(struct ast_channel *, chanb, NULL, ao2_cleanup);
2960 const char *chana_name;
2961 const char *chana_exten;
2962 const char *chana_context;
2964 const char *chanb_name;
2965 const char *chanb_exten;
2966 const char *chanb_context;
2968 struct ast_bridge *bridge;
2970 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_a, NULL, ao2_cleanup);
2971 RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_b, NULL, ao2_cleanup);
2973 /* make sure valid channels were specified */
2974 if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
2975 astman_send_error(s, m, "Missing channel parameter in request");
2979 /* Start with chana */
2980 chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
2982 snprintf(buf, sizeof(buf), "Channel1 does not exist: %s", channela);
2983 astman_send_error(s, m, buf);
2986 xfer_cfg_a = ast_get_chan_features_xfer_config(chana);
2987 ast_channel_lock(chana);
2988 chana_name = ast_strdupa(ast_channel_name(chana));
2989 chana_exten = ast_strdupa(ast_channel_exten(chana));
2990 chana_context = ast_strdupa(ast_channel_context(chana));
2991 chana_priority = ast_channel_priority(chana);
2992 if (!ast_test_flag(ast_channel_flags(chana), AST_FLAG_IN_AUTOLOOP)) {
2995 ast_channel_unlock(chana);
2997 chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
2999 snprintf(buf, sizeof(buf), "Channel2 does not exist: %s", channelb);
3000 astman_send_error(s, m, buf);
3003 xfer_cfg_b = ast_get_chan_features_xfer_config(chanb);
3004 ast_channel_lock(chanb);
3005 chanb_name = ast_strdupa(ast_channel_name(chanb));
3006 chanb_exten = ast_strdupa(ast_channel_exten(chanb));
3007 chanb_context = ast_strdupa(ast_channel_context(chanb));
3008 chanb_priority = ast_channel_priority(chanb);
3009 if (!ast_test_flag(ast_channel_flags(chanb), AST_FLAG_IN_AUTOLOOP)) {
3012 ast_channel_unlock(chanb);
3014 bridge = ast_bridge_basic_new();
3016 astman_send_error(s, m, "Unable to create bridge\n");
3020 ast_bridge_set_after_go_on(chana, chana_context, chana_exten, chana_priority, NULL);
3021 if (ast_bridge_add_channel(bridge, chana, NULL, playtone & PLAYTONE_CHANNEL1, xfer_cfg_a ? xfer_cfg_a->xfersound : NULL)) {
3022 snprintf(buf, sizeof(buf), "Unable to add Channel1 to bridge: %s", ast_channel_name(chana));
3023 astman_send_error(s, m, buf);
3024 ast_bridge_destroy(bridge);
3028 ast_bridge_set_after_go_on(chanb, chanb_context, chanb_exten, chanb_priority, NULL);
3029 if (ast_bridge_add_channel(bridge, chanb, NULL, playtone & PLAYTONE_CHANNEL2, xfer_cfg_b ? xfer_cfg_b->xfersound : NULL)) {
3030 snprintf(buf, sizeof(buf), "Unable to add Channel2 to bridge: %s", ast_channel_name(chanb));
3031 astman_send_error(s, m, buf);
3032 ast_bridge_destroy(bridge);
3037 <managerEventInstance>
3038 <synopsis>Raised when a bridge is successfully created due to a manager action.</synopsis>
3040 <parameter name="Response">
3042 <enum name="Success"/>
3043 <enum name="Failed"/>
3048 <ref type="manager">Bridge</ref>
3050 </managerEventInstance>
3052 /* BUGBUG This event used to use ast_manager_event_multichan. Now channel variables are not included in the event */
3053 manager_event(EVENT_FLAG_CALL, "BridgeAction",
3054 "Response: Success\r\n"
3056 "Channel2: %s\r\n", chana_name, chanb_name);
3058 astman_send_ack(s, m, "Channels have been bridged");
3063 static struct ast_cli_entry cli_features[] = {
3064 AST_CLI_DEFINE(handle_features_reload, "Reloads configured features"),
3068 * The presence of this datastore on the channel indicates that
3069 * someone is attemting to pickup or has picked up the channel.
3070 * The purpose is to prevent a race between two channels
3071 * attempting to pickup the same channel.
3073 static const struct ast_datastore_info pickup_active = {
3074 .type = "pickup-active",
3077 int ast_can_pickup(struct ast_channel *chan)
3079 if (!ast_channel_pbx(chan) && !ast_channel_masq(chan) && !ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
3080 && (ast_channel_state(chan) == AST_STATE_RINGING
3081 || ast_channel_state(chan) == AST_STATE_RING
3083 * Check the down state as well because some SIP devices do not
3084 * give 180 ringing when they can just give 183 session progress
3085 * instead. Issue 14005. (Some ISDN switches as well for that
3088 || ast_channel_state(chan) == AST_STATE_DOWN)
3089 && !ast_channel_datastore_find(chan, &pickup_active, NULL)) {
3095 static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
3097 struct ast_channel *target = obj;/*!< Potential pickup target */
3098 struct ast_channel *chan = arg;/*!< Channel wanting to pickup call */
3100 if (chan == target) {
3104 ast_channel_lock(target);
3105 if (ast_can_pickup(target)) {
3106 /* Lock both channels. */
3107 while (ast_channel_trylock(chan)) {
3108 ast_channel_unlock(target);
3110 ast_channel_lock(target);
3114 * Both callgroup and namedcallgroup pickup variants are