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 unsigned int time_limit; /*!< How long this specific channel may remain in the parking lot before timing out */
109 struct parking_lot *lot; /*!< Which parking lot the user is parked to */
110 enum park_call_resolution resolution; /*!< How did the parking session end? If the call is in a bridge, lock parked_user before checking/setting */
115 * \brief If a parking lot exists in the parking lot list already, update its status to match the provided
116 * configuration and return a reference return a reference to it. Otherwise, create a parking lot
117 * struct based on a parking lot configuration and return a reference to the new one.
119 * \param cfg The configuration being used as a reference to build the parking lot from.
121 * \retval A reference to the new parking lot
122 * \retval NULL if it was not found and could not be be allocated
124 * \note The parking lot will need to be unreffed if it ever falls out of scope
125 * \note The parking lot will automatically be added to the parking lot container if needed as part of this process
127 struct parking_lot *parking_lot_build_or_update(struct parking_lot_cfg *cfg);
131 * \brief Remove a parking lot from the usable lists if it is no longer involved in any calls and no configuration currently claims it
133 * \param lot Which parking lot is being checked for elimination
135 * \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
136 * a parking lot no longer existing in configurations.
138 void parking_lot_remove_if_unused(struct parking_lot *lot);
142 * \brief Create a new parking bridge
144 * \param bridge_lot Parking lot which the new bridge should be based on
146 * \retval NULL if the bridge can not be created
147 * \retval Newly created parking bridge
149 struct ast_bridge *bridge_parking_new(struct parking_lot *bridge_lot);
153 * \brief Get a reference to a parking lot's bridge. If it doesn't exist, create it and get a reference.
155 * \param lot Which parking lot we need the bridge from. This parking lot must be locked before calling this function.
157 * \retval A reference to the ast_bridge associated with the parking lot
158 * \retval NULL if it didn't already have a bridge and one couldn't be created
160 * \note This bridge will need to be unreffed if it ever falls out of scope.
162 struct ast_bridge *parking_lot_get_bridge(struct parking_lot *lot);
166 * \brief Get an available parking space within a parking lot.
168 * \param lot Which parking lot we are getting a space from
169 * \param target_override If there is a specific slot we want, provide it here and we'll start from that position
171 * \retval -1 if No slot can be found
172 * \retval integer value of parking space selected
174 * \note lot should be locked before this is called and unlocked only after a parked_user with the space
175 * returned has been added to the parking lot.
177 int parking_lot_get_space(struct parking_lot *lot, int target_override);
181 * \brief Determine if there is a parked user in a parking space and pull it from the parking lot if there is.
183 * \param lot Parking lot being pulled from
184 * \param target If < 0 search for the first occupied space in the parking lot
185 * If >= 0 Only pull from the indicated target
187 * \retval NULL if no parked user could be pulled from the requested parking lot at the requested parking space
188 * \retval reference to the requested parked user
190 * \note The parked user will be removed from parking lot as part of this process
191 * \note Remove this reference with ao2_cleanup once it falls out of scope.
193 struct parked_user *parking_lot_retrieve_parked_user(struct parking_lot *lot, int target);
197 * \brief Apply features based on the parking lot feature options
199 * \param chan Which channel's feature set is being modified
200 * \param lot parking lot which establishes the features used
201 * \param recipient_mode AST_FEATURE_FLAG_BYCALLER if the user is the retriever
202 * AST_FEATURE_FLAG_BYCALLEE if the user is the parkee
204 void parked_call_retrieve_enable_features(struct ast_channel *chan, struct parking_lot *lot, int recipient_mode);
208 * \brief Set necessary bridge roles on a channel that is about to enter a parking lot
210 * \param chan Entering channel
211 * \param lot The parking lot the channel will be entering
212 * \param force_ringing Use ringing instead of music on hold
214 void parking_channel_set_roles(struct ast_channel *chan, struct parking_lot *lot, int force_ringing);
218 * \brief custom callback function for ast_bridge_channel_queue_playfile which plays a parking space
219 * and optionally hangs up the call afterwards based on the payload in playfile.
221 void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload);
225 * \brief Setup timeout interval feature on an ast_bridge_features for parking
227 * \param features The ast_bridge_features we are establishing the interval hook on
228 * \param user The parked_user receiving the timeout duration limits
230 void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user);
234 * \brief Get a pointer to the parking lot container for purposes such as iteration
236 * \retval pointer to the parking lot container.
238 struct ao2_container *get_parking_lot_container(void);
242 * \brief Find a parking lot based on its name
244 * \param lot_name Name of the parking lot sought
246 * \retval The parking lot if found
247 * \retval NULL if no parking lot with the name specified exists
249 * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
251 struct parking_lot *parking_lot_find_by_name(const char *lot_name);
255 * \brief Find parking lot name from channel
257 * \param chan The channel we want the parking lot name for
259 * \retval name of the channel's assigned parking lot if it is defined by the channel in some way
260 * \retval name of the default parking lot if it is not
262 * \note Channel needs to be locked while the returned string is in use.
264 const char *find_channel_parking_lot_name(struct ast_channel *chan);
268 * \brief Flattens a peer name so that it can be written to/found from PBX extensions
270 * \param peername unflattened peer name. This will be flattened in place, so expect it to change.
272 void flatten_peername(char *peername);
276 * \brief Set a channel's position in the PBX after timeout using the parking lot settings
278 * \param pu Parked user who is entering/reentering the PBX
279 * \param lot Parking lot the user was removed from.
281 * \retval 0 Position set successfully
282 * \retval -1 Failed to set the position
284 int comeback_goto(struct parked_user *pu, struct parking_lot *lot);
288 * \brief Add extensions for a parking lot configuration
290 * \param lot_cfg parking lot configuration to generate extensions for
292 * \retval 0 on success
293 * \retval non-zero on failure
295 int parking_lot_cfg_create_extensions(struct parking_lot_cfg *lot_cfg);
299 * \brief Remove extensions belonging to a parking lot configuration
301 * \param lot_cfg parking lot configuratin to remove extensions from
303 * \note This will not remove extensions registered non-exclusively even
304 * if those extensions were registered by lot_cfg. Those are only
305 * purged on a res_parking module reload.
307 void parking_lot_cfg_remove_extensions(struct parking_lot_cfg *lot_cfg);
311 * \brief Pull a parked user out of its parking lot. Use this when you don't want to use the parked user afterwards.
312 * \param user The parked user being pulled.
314 * \retval 0 on success
315 * \retval -1 if the user didn't have its parking lot set
317 int unpark_parked_user(struct parked_user *user);
321 * \brief Publish a stasis parked call message for the channel indicating failure to park.
323 * \param parkee channel belonging to the failed parkee
325 void publish_parked_call_failure(struct ast_channel *parkee);
329 * \brief Publish a stasis parked call message for a given parked user
331 * \param pu pointer to a parked_user that we are generating the message for
332 * \param event_type What parked call event type is provoking this message
334 void publish_parked_call(struct parked_user *pu, enum ast_parked_call_event_type event_type);
338 * \brief Function to prepare a channel for parking by determining which parking bridge should
339 * be used, setting up a park common datastore so that the parking bridge will have access
340 * to necessary parking information when joining, and applying various bridge roles to the
343 * \param parkee The channel being preparred for parking
344 * \param parker The channel initiating the park; may be the parkee as well
345 * \param app_data arguments supplied to the Park application. May be NULL.
346 * \param silence_announcements optional pointer to an integer where we want to store the silence option flag
347 * this value should be initialized to 0 prior to calling park_common_setup.
349 * \retval reference to a parking bridge if successful
350 * \retval NULL on failure
352 * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
354 struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
355 const char *app_data, int *silence_announcements);
357 struct park_common_datastore {
358 char *parker_uuid; /*!< Unique ID of the channel parking the call. */
359 char *comeback_override; /*!< Optional goto string for where to send the call after we are done */
360 int randomize; /*!< Pick a parking space to enter on at random */
361 int time_limit; /*!< time limit override. -1 values don't override, 0 for unlimited time, >0 for custom time limit in seconds */
362 int silence_announce; /*!< Used when a call parks itself to keep it from hearing the parked call announcement */
367 * \brief Function that pulls data from the park common datastore on a channel in order to apply it to
368 * the parked user struct upon bridging.
370 * \param parkee The channel entering parking with the datastore we are checking
371 * \param parker_uuid pointer to a string pointer for placing the name of the channel that parked parkee
372 * \param comeback_override pointer to a string pointer for placing the comeback_override option
373 * \param randomize integer pointer to an integer for placing the randomize option
374 * \param time_limit integer pointer to an integer for placing the time limit option
375 * \param silence_announce pointer to an integer for placing the silence_announcements option
377 void get_park_common_datastore_data(struct ast_channel *parkee,
378 char **parker_uuid, char **comeback_override,
379 int *randomize, int *time_limit, int *silence_announce);
383 * \brief Notify metermaids that we've changed an extension
385 * \param exten Extension of the call parked/unparked
386 * \param context Context of the call parked/unparked
387 * \param state new device state
389 void parking_notify_metermaids(int exten, const char *context, enum ast_device_state state);
393 * \brief Execution function for the parking application
395 * \param chan ast_channel entering the application
396 * \param data arguments to the application
398 * \retval 0 the application executed in such a way that the channel should proceed in the dial plan
399 * \retval -1 the channel should no longer proceed through the dial plan
401 * \note this function should only be used to register the parking application and not generally to park calls.
403 int park_app_exec(struct ast_channel *chan, const char *data);
407 * \brief Execution function for the parked call application
409 * \param chan ast_channel entering the application
410 * \param data arguments to the application
412 * \retval 0 the application executed in such a way that the channel should proceed in the dial plan
413 * \retval -1 the channel should no longer proceed through the dial plan
415 int parked_call_app_exec(struct ast_channel *chan, const char *data);
419 * \brief Execution function for the park and retrieve application
421 * \param chan ast_channel entering the application
422 * \param data arguments to the application
424 * \retval 0 the application executed in such a way that the channel should proceed in the dial plan
425 * \retval -1 the channel should no longer proceed through the dial plan
427 * \note this function should only be used to register the park and announce application and not generally to park and announce.
429 int park_and_announce_app_exec(struct ast_channel *chan, const char *data);
433 * \brief Register CLI commands
435 * \retval 0 if successful
436 * \retval -1 on failure
438 int load_parking_ui(void);
442 * \brief Unregister CLI commands
444 void unload_parking_ui(void);
448 * \brief Register manager actions and setup subscriptions for stasis events
450 int load_parking_manager(void);
454 * \brief Unregister manager actions and remove subscriptions for stasis events
456 void unload_parking_manager(void);
460 * \brief Register bridge features for parking
462 * \retval 0 on success
463 * \retval -1 on failure
465 int load_parking_bridge_features(void);
469 * \brief Unregister features registered by load_parking_bridge_features
471 void unload_parking_bridge_features(void);
475 * \brief Register Parking devstate handler
477 int load_parking_devstate(void);
481 * \brief Unregister Parking devstate handler
483 void unload_parking_devstate(void);