2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Jonathan Rose <jrose@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Call Parking Resource Internal API
23 * \author Jonathan Rose <jrose@digium.com>
26 #include "asterisk/pbx.h"
27 #include "asterisk/bridging.h"
28 #include "asterisk/parking.h"
29 #include "asterisk/stasis_channels.h"
31 #define DEFAULT_PARKING_LOT "default"
32 #define DEFAULT_PARKING_EXTEN "700"
33 #define BASE_REGISTRAR "res_parking"
34 #define PARK_DIAL_CONTEXT "park-dial"
36 enum park_call_resolution {
37 PARK_UNSET = 0, /*! Nothing set a resolution. This should never be observed in practice. */
38 PARK_ABANDON, /*! The channel for the parked call hung up */
39 PARK_TIMEOUT, /*! The parked call stayed parked until the parking lot timeout was reached and was removed */
40 PARK_FORCED, /*! The parked call was forcibly terminated by an unusual means in Asterisk */
41 PARK_ANSWERED, /*! The parked call was retrieved successfully */
44 enum parked_call_feature_options {
52 enum parking_lot_modes {
53 PARKINGLOT_NORMAL = 0, /*! The parking lot is configured normally and can accept new calls. Disable on reload if the config isn't replaced.
54 * valid transitions: PARKINGLOT_DISABLED */
55 PARKINGLOT_DYNAMIC, /*! The parking lot is a dynamically created parking lot. It can be parked to at any time. Disabled on last parked call leaving.
56 * valid transitions: PARKINGLOT_DISABLED */
57 PARKINGLOT_DISABLED, /*! The parking lot is no longer linked to a parking lot in configuration. It can no longer be parked to.
58 * and it can not be parked to. This mode has no transitions. */
61 struct parking_lot_cfg {
62 int parking_start; /*!< First space in the parking lot */
63 int parking_stop; /*!< Last space in the parking lot */
65 unsigned int parkingtime; /*!< Analogous to parkingtime config option */
66 unsigned int comebackdialtime; /*!< Analogous to comebackdialtime config option */
67 unsigned int parkfindnext; /*!< Analogous to parkfindnext config option */
68 unsigned int parkext_exclusive; /*!< Analogous to parkext_exclusive config option */
69 unsigned int parkaddhints; /*!< Analogous to parkaddhints config option */
70 unsigned int comebacktoorigin; /*!< Analogous to comebacktoorigin config option */
71 int parkedplay; /*!< Analogous to parkedplay config option */
72 int parkedcalltransfers; /*!< Analogous to parkedcalltransfers config option */
73 int parkedcallreparking; /*!< Analogous to parkedcallreparking config option */
74 int parkedcallhangup; /*!< Analogous to parkedcallhangup config option */
75 int parkedcallrecording; /*!< Analogous to parkedcallrecording config option */
77 AST_DECLARE_STRING_FIELDS(
78 AST_STRING_FIELD(name); /*!< Name of the parking lot configuration object */
79 AST_STRING_FIELD(registrar); /*!< Which registrar the lot uses if it isn't the default registrar */
80 AST_STRING_FIELD(mohclass); /*!< Analogous to mohclass config option */
81 AST_STRING_FIELD(parkext); /*!< Analogous to parkext config option */
82 AST_STRING_FIELD(parking_con); /*!< Analogous to context config option */
83 AST_STRING_FIELD(comebackcontext); /*!< Analogous to comebackcontext config option */
84 AST_STRING_FIELD(courtesytone); /*!< Analogous to courtesytone config option */
89 int next_space; /*!< When using parkfindnext, which space we should start searching from next time we park */
90 struct ast_bridge *parking_bridge; /*!< Bridged where parked calls will rest until they are answered or otherwise leave */
91 struct ao2_container *parked_users; /*!< List of parked users rigidly ordered by their parking space */
92 struct parking_lot_cfg *cfg; /*!< Reference to configuration object for the parking lot */
93 enum parking_lot_modes mode; /*!< Whether a parking lot is operational, being reconfigured, primed for deletion, or dynamically created. */
94 int disable_mark; /*!< On reload, disable this parking lot if it doesn't receive a new configuration. */
96 AST_DECLARE_STRING_FIELDS(
97 AST_STRING_FIELD(name); /*!< Name of the parking lot object */
102 struct ast_channel *chan; /*!< Parked channel */
103 struct ast_channel_snapshot *parker; /*!< Snapshot of the channel that parked the call at the time of parking */
104 struct ast_channel_snapshot *retriever; /*!< Snapshot of the channel that retrieves a parked call */
105 struct timeval start; /*!< When the call was parked */
106 int parking_space; /*!< Which parking space is used */
107 char comeback[AST_MAX_CONTEXT]; /*!< Where to go on parking timeout */
108 char blindtransfer[AST_CHANNEL_NAME]; /*!< What the BLINDTRANSFER variable was at the time of entry */
109 unsigned int time_limit; /*!< How long this specific channel may remain in the parking lot before timing out */
110 struct parking_lot *lot; /*!< Which parking lot the user is parked to */
111 enum park_call_resolution resolution; /*!< How did the parking session end? If the call is in a bridge, lock parked_user before checking/setting */
116 * \brief If a parking lot exists in the parking lot list already, update its status to match the provided
117 * configuration and return a reference return a reference to it. Otherwise, create a parking lot
118 * struct based on a parking lot configuration and return a reference to the new one.
120 * \param cfg The configuration being used as a reference to build the parking lot from.
122 * \retval A reference to the new parking lot
123 * \retval NULL if it was not found and could not be be allocated
125 * \note The parking lot will need to be unreffed if it ever falls out of scope
126 * \note The parking lot will automatically be added to the parking lot container if needed as part of this process
128 struct parking_lot *parking_lot_build_or_update(struct parking_lot_cfg *cfg);
132 * \brief Remove a parking lot from the usable lists if it is no longer involved in any calls and no configuration currently claims it
134 * \param lot Which parking lot is being checked for elimination
136 * \note This should generally be called when something is happening that could cause a parking lot to die such as a call being unparked or
137 * a parking lot no longer existing in configurations.
139 void parking_lot_remove_if_unused(struct parking_lot *lot);
143 * \brief Create a new parking bridge
145 * \param bridge_lot Parking lot which the new bridge should be based on
147 * \retval NULL if the bridge can not be created
148 * \retval Newly created parking bridge
150 struct ast_bridge *bridge_parking_new(struct parking_lot *bridge_lot);
154 * \brief Get a reference to a parking lot's bridge. If it doesn't exist, create it and get a reference.
156 * \param lot Which parking lot we need the bridge from. This parking lot must be locked before calling this function.
158 * \retval A reference to the ast_bridge associated with the parking lot
159 * \retval NULL if it didn't already have a bridge and one couldn't be created
161 * \note This bridge will need to be unreffed if it ever falls out of scope.
163 struct ast_bridge *parking_lot_get_bridge(struct parking_lot *lot);
167 * \brief Get an available parking space within a parking lot.
169 * \param lot Which parking lot we are getting a space from
170 * \param target_override If there is a specific slot we want, provide it here and we'll start from that position
172 * \retval -1 if No slot can be found
173 * \retval integer value of parking space selected
175 * \note lot should be locked before this is called and unlocked only after a parked_user with the space
176 * returned has been added to the parking lot.
178 int parking_lot_get_space(struct parking_lot *lot, int target_override);
182 * \brief Determine if there is a parked user in a parking space and pull it from the parking lot if there is.
184 * \param lot Parking lot being pulled from
185 * \param target If < 0 search for the first occupied space in the parking lot
186 * If >= 0 Only pull from the indicated target
188 * \retval NULL if no parked user could be pulled from the requested parking lot at the requested parking space
189 * \retval reference to the requested parked user
191 * \note The parked user will be removed from parking lot as part of this process
192 * \note Remove this reference with ao2_cleanup once it falls out of scope.
194 struct parked_user *parking_lot_retrieve_parked_user(struct parking_lot *lot, int target);
198 * \brief Apply features based on the parking lot feature options
200 * \param chan Which channel's feature set is being modified
201 * \param lot parking lot which establishes the features used
202 * \param recipient_mode AST_FEATURE_FLAG_BYCALLER if the user is the retriever
203 * AST_FEATURE_FLAG_BYCALLEE if the user is the parkee
205 void parked_call_retrieve_enable_features(struct ast_channel *chan, struct parking_lot *lot, int recipient_mode);
209 * \brief Set necessary bridge roles on a channel that is about to enter a parking lot
211 * \param chan Entering channel
212 * \param lot The parking lot the channel will be entering
213 * \param force_ringing Use ringing instead of music on hold
215 void parking_channel_set_roles(struct ast_channel *chan, struct parking_lot *lot, int force_ringing);
219 * \brief custom callback function for ast_bridge_channel_queue_playfile which plays a parking space
220 * and optionally hangs up the call afterwards based on the payload in playfile.
222 void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload);
226 * \brief Setup timeout interval feature on an ast_bridge_features for parking
228 * \param features The ast_bridge_features we are establishing the interval hook on
229 * \param user The parked_user receiving the timeout duration limits
231 void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user);
235 * \brief Get a pointer to the parking lot container for purposes such as iteration
237 * \retval pointer to the parking lot container.
239 struct ao2_container *get_parking_lot_container(void);
243 * \brief Find a parking lot based on its name
245 * \param lot_name Name of the parking lot sought
247 * \retval The parking lot if found
248 * \retval NULL if no parking lot with the name specified exists
250 * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
252 struct parking_lot *parking_lot_find_by_name(const char *lot_name);
256 * \brief Find parking lot name from channel
258 * \param chan The channel we want the parking lot name for
260 * \retval name of the channel's assigned parking lot if it is defined by the channel in some way
261 * \retval name of the default parking lot if it is not
263 * \note Channel needs to be locked while the returned string is in use.
265 const char *find_channel_parking_lot_name(struct ast_channel *chan);
269 * \brief Flattens a peer name so that it can be written to/found from PBX extensions
271 * \param peername unflattened peer name. This will be flattened in place, so expect it to change.
273 void flatten_peername(char *peername);
277 * \brief Set a channel's position in the PBX after timeout using the parking lot settings
279 * \param pu Parked user who is entering/reentering the PBX
280 * \param lot Parking lot the user was removed from.
282 * \retval 0 Position set successfully
283 * \retval -1 Failed to set the position
285 int comeback_goto(struct parked_user *pu, struct parking_lot *lot);
289 * \brief Add extensions for a parking lot configuration
291 * \param lot_cfg parking lot configuration to generate extensions for
293 * \retval 0 on success
294 * \retval non-zero on failure
296 int parking_lot_cfg_create_extensions(struct parking_lot_cfg *lot_cfg);
300 * \brief Remove extensions belonging to a parking lot configuration
302 * \param lot_cfg parking lot configuratin to remove extensions from
304 * \note This will not remove extensions registered non-exclusively even
305 * if those extensions were registered by lot_cfg. Those are only
306 * purged on a res_parking module reload.
308 void parking_lot_cfg_remove_extensions(struct parking_lot_cfg *lot_cfg);
312 * \brief Pull a parked user out of its parking lot. Use this when you don't want to use the parked user afterwards.
313 * \param user The parked user being pulled.
315 * \retval 0 on success
316 * \retval -1 if the user didn't have its parking lot set
318 int unpark_parked_user(struct parked_user *user);
322 * \brief Publish a stasis parked call message for the channel indicating failure to park.
324 * \param parkee channel belonging to the failed parkee
326 void publish_parked_call_failure(struct ast_channel *parkee);
330 * \brief Publish a stasis parked call message for a given parked user
332 * \param pu pointer to a parked_user that we are generating the message for
333 * \param event_type What parked call event type is provoking this message
335 void publish_parked_call(struct parked_user *pu, enum ast_parked_call_event_type event_type);
339 * \brief Setup a parked call on a parking bridge without needing to parse appdata
342 struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
343 const char *lot_name, const char *comeback_override,
344 int use_ringing, int randomize, int time_limit, int silence_announcements);
348 * \brief Function to prepare a channel for parking by determining which parking bridge should
349 * be used, setting up a park common datastore so that the parking bridge will have access
350 * to necessary parking information when joining, and applying various bridge roles to the
353 * \param parkee The channel being preparred for parking
354 * \param parker The channel initiating the park; may be the parkee as well
355 * \param app_data arguments supplied to the Park application. May be NULL.
356 * \param silence_announcements optional pointer to an integer where we want to store the silence option flag
357 * this value should be initialized to 0 prior to calling park_common_setup.
359 * \retval reference to a parking bridge if successful
360 * \retval NULL on failure
362 * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
364 struct ast_bridge *park_application_setup(struct ast_channel *parkee, struct ast_channel *parker,
365 const char *app_data, int *silence_announcements);
367 struct park_common_datastore {
368 char *parker_uuid; /*!< Unique ID of the channel parking the call. */
369 char *comeback_override; /*!< Optional goto string for where to send the call after we are done */
370 int randomize; /*!< Pick a parking space to enter on at random */
371 int time_limit; /*!< time limit override. -1 values don't override, 0 for unlimited time, >0 for custom time limit in seconds */
372 int silence_announce; /*!< Used when a call parks itself to keep it from hearing the parked call announcement */
377 * \brief Function that pulls data from the park common datastore on a channel in order to apply it to
378 * the parked user struct upon bridging.
380 * \param parkee The channel entering parking with the datastore we are checking
381 * \param parker_uuid pointer to a string pointer for placing the name of the channel that parked parkee
382 * \param comeback_override pointer to a string pointer for placing the comeback_override option
383 * \param randomize integer pointer to an integer for placing the randomize option
384 * \param time_limit integer pointer to an integer for placing the time limit option
385 * \param silence_announce pointer to an integer for placing the silence_announcements option
387 void get_park_common_datastore_data(struct ast_channel *parkee,
388 char **parker_uuid, char **comeback_override,
389 int *randomize, int *time_limit, int *silence_announce);
393 * \brief Notify metermaids that we've changed an extension
395 * \param exten Extension of the call parked/unparked
396 * \param context Context of the call parked/unparked
397 * \param state new device state
399 void parking_notify_metermaids(int exten, const char *context, enum ast_device_state state);
403 * \brief Execution function for the parking application
405 * \param chan ast_channel entering the application
406 * \param data arguments to the application
408 * \retval 0 the application executed in such a way that the channel should proceed in the dial plan
409 * \retval -1 the channel should no longer proceed through the dial plan
411 * \note this function should only be used to register the parking application and not generally to park calls.
413 int park_app_exec(struct ast_channel *chan, const char *data);
417 * \brief Execution function for the parked call application
419 * \param chan ast_channel entering the application
420 * \param data arguments to the application
422 * \retval 0 the application executed in such a way that the channel should proceed in the dial plan
423 * \retval -1 the channel should no longer proceed through the dial plan
425 int parked_call_app_exec(struct ast_channel *chan, const char *data);
429 * \brief Execution function for the park and retrieve application
431 * \param chan ast_channel entering the application
432 * \param data arguments to the application
434 * \retval 0 the application executed in such a way that the channel should proceed in the dial plan
435 * \retval -1 the channel should no longer proceed through the dial plan
437 * \note this function should only be used to register the park and announce application and not generally to park and announce.
439 int park_and_announce_app_exec(struct ast_channel *chan, const char *data);
443 * \brief Register CLI commands
445 * \retval 0 if successful
446 * \retval -1 on failure
448 int load_parking_ui(void);
452 * \brief Unregister CLI commands
454 void unload_parking_ui(void);
458 * \brief Register manager actions and setup subscriptions for stasis events
460 int load_parking_manager(void);
464 * \brief Unregister manager actions and remove subscriptions for stasis events
466 void unload_parking_manager(void);
470 * \brief Register bridge features for parking
472 * \retval 0 on success
473 * \retval -1 on failure
475 int load_parking_bridge_features(void);
479 * \brief Unregister features registered by load_parking_bridge_features
481 void unload_parking_bridge_features(void);
485 * \brief Register Parking devstate handler
487 int load_parking_devstate(void);
491 * \brief Unregister Parking devstate handler
493 void unload_parking_devstate(void);