res_parking: Automatically generate extensions, hints, etc.
[asterisk/asterisk.git] / res / parking / res_parking.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jonathan Rose <jrose@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Call Parking Resource Internal API
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  */
25
26 #include "asterisk/pbx.h"
27 #include "asterisk/bridging.h"
28 #include "asterisk/parking.h"
29 #include "asterisk/stasis_channels.h"
30
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"
35
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 */
42 };
43
44 enum parked_call_feature_options {
45         OPT_PARKEDPLAY = 0,
46         OPT_PARKEDTRANSFERS,
47         OPT_PARKEDREPARKING,
48         OPT_PARKEDHANGUP,
49         OPT_PARKEDRECORDING,
50 };
51
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. */
59 };
60
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 */
64
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 */
76
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 */
85         );
86 };
87
88 struct parking_lot {
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. */
95
96         AST_DECLARE_STRING_FIELDS(
97                 AST_STRING_FIELD(name);               /*!< Name of the parking lot object */
98         );
99 };
100
101 struct parked_user {
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 */
111 };
112
113 /*!
114  * \since 12.0.0
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.
118  *
119  * \param cfg The configuration being used as a reference to build the parking lot from.
120  *
121  * \retval A reference to the new parking lot
122  * \retval NULL if it was not found and could not be be allocated
123  *
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
126  */
127 struct parking_lot *parking_lot_build_or_update(struct parking_lot_cfg *cfg);
128
129 /*!
130  * \since 12.0.0
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
132  *
133  * \param lot Which parking lot is being checked for elimination
134  *
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.
137  */
138 void parking_lot_remove_if_unused(struct parking_lot *lot);
139
140 /*!
141  * \since 12.0.0
142  * \brief Create a new parking bridge
143  *
144  * \param bridge_lot Parking lot which the new bridge should be based on
145  *
146  * \retval NULL if the bridge can not be created
147  * \retval Newly created parking bridge
148  */
149 struct ast_bridge *bridge_parking_new(struct parking_lot *bridge_lot);
150
151 /*!
152  * \since 12.0.0
153  * \brief Get a reference to a parking lot's bridge. If it doesn't exist, create it and get a reference.
154  *
155  * \param lot Which parking lot we need the bridge from. This parking lot must be locked before calling this function.
156  *
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
159  *
160  * \note This bridge will need to be unreffed if it ever falls out of scope.
161  */
162 struct ast_bridge *parking_lot_get_bridge(struct parking_lot *lot);
163
164 /*!
165  * \since 12.0.0
166  * \brief Get an available parking space within a parking lot.
167  *
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
170  *
171  * \retval -1 if No slot can be found
172  * \retval integer value of parking space selected
173  *
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.
176  */
177 int parking_lot_get_space(struct parking_lot *lot, int target_override);
178
179 /*!
180  * \since 12.0.0
181  * \brief Determine if there is a parked user in a parking space and pull it from the parking lot if there is.
182  *
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
186  *
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
189  *
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.
192  */
193 struct parked_user *parking_lot_retrieve_parked_user(struct parking_lot *lot, int target);
194
195 /*!
196  * \since 12.0.0
197  * \brief Apply features based on the parking lot feature options
198  *
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
203  */
204 void parked_call_retrieve_enable_features(struct ast_channel *chan, struct parking_lot *lot, int recipient_mode);
205
206 /*!
207  * \since 12.0.0
208  * \brief Set necessary bridge roles on a channel that is about to enter a parking lot
209  *
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
213  */
214 void parking_channel_set_roles(struct ast_channel *chan, struct parking_lot *lot, int force_ringing);
215
216 /*!
217  * \since 12.0.0
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.
220  */
221 void say_parking_space(struct ast_bridge_channel *bridge_channel, const char *payload);
222
223 /*!
224  * \since 12.0.0
225  * \brief Setup timeout interval feature on an ast_bridge_features for parking
226  *
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
229  */
230 void parking_set_duration(struct ast_bridge_features *features, struct parked_user *user);
231
232 /*!
233  * \since 12.0.0
234  * \brief Get a pointer to the parking lot container for purposes such as iteration
235  *
236  * \retval pointer to the parking lot container.
237  */
238 struct ao2_container *get_parking_lot_container(void);
239
240 /*!
241  * \since 12.0.0
242  * \brief Find a parking lot based on its name
243  *
244  * \param lot_name Name of the parking lot sought
245  *
246  * \retval The parking lot if found
247  * \retval NULL if no parking lot with the name specified exists
248  *
249  * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
250  */
251 struct parking_lot *parking_lot_find_by_name(const char *lot_name);
252
253 /*!
254  * \since 12.0.0
255  * \brief Find parking lot name from channel
256  *
257  * \param chan The channel we want the parking lot name for
258  *
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
261  *
262  * \note Channel needs to be locked while the returned string is in use.
263  */
264 const char *find_channel_parking_lot_name(struct ast_channel *chan);
265
266 /*!
267  * \since 12.0.0
268  * \brief Flattens a peer name so that it can be written to/found from PBX extensions
269  *
270  * \param peername unflattened peer name. This will be flattened in place, so expect it to change.
271  */
272 void flatten_peername(char *peername);
273
274 /*!
275  * \since 12.0.0
276  * \brief Set a channel's position in the PBX after timeout using the parking lot settings
277  *
278  * \param pu Parked user who is entering/reentering the PBX
279  * \param lot Parking lot the user was removed from.
280  *
281  * \retval 0 Position set successfully
282  * \retval -1 Failed to set the position
283  */
284 int comeback_goto(struct parked_user *pu, struct parking_lot *lot);
285
286 /*!
287  * \since 12.0.0
288  * \brief Add extensions for a parking lot configuration
289  *
290  * \param lot_cfg parking lot configuration to generate extensions for
291  *
292  * \retval 0 on success
293  * \retval non-zero on failure
294  */
295 int parking_lot_cfg_create_extensions(struct parking_lot_cfg *lot_cfg);
296
297 /*!
298  * \since 12.0.0
299  * \brief Remove extensions belonging to a parking lot configuration
300  *
301  * \param lot_cfg parking lot configuratin to remove extensions from
302  *
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.
306  */
307 void parking_lot_cfg_remove_extensions(struct parking_lot_cfg *lot_cfg);
308
309 /*!
310  * \since 12.0.0
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.
313  *
314  * \retval 0 on success
315  * \retval -1 if the user didn't have its parking lot set
316  */
317 int unpark_parked_user(struct parked_user *user);
318
319 /*!
320  * \since 12.0.0
321  * \brief Publish a stasis parked call message for the channel indicating failure to park.
322  *
323  * \param parkee channel belonging to the failed parkee
324  */
325 void publish_parked_call_failure(struct ast_channel *parkee);
326
327 /*!
328  * \since 12.0.0
329  * \brief Publish a stasis parked call message for a given parked user
330  *
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
333  */
334 void publish_parked_call(struct parked_user *pu, enum ast_parked_call_event_type event_type);
335
336 /*!
337  * \since 12.0.0
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
341  *        channel.
342  *
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.
348  *
349  * \retval reference to a parking bridge if successful
350  * \retval NULL on failure
351  *
352  * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
353  */
354 struct ast_bridge *park_common_setup(struct ast_channel *parkee, struct ast_channel *parker,
355         const char *app_data, int *silence_announcements);
356
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 */
363 };
364
365 /*!
366  * \since 12.0.0
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.
369  *
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
376  */
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);
380
381 /*!
382  * \since 12.0.0
383  * \brief Notify metermaids that we've changed an extension
384  *
385  * \param exten Extension of the call parked/unparked
386  * \param context Context of the call parked/unparked
387  * \param state new device state
388  */
389 void parking_notify_metermaids(int exten, const char *context, enum ast_device_state state);
390
391 /*!
392  * \since 12.0.0
393  * \brief Execution function for the parking application
394  *
395  * \param chan ast_channel entering the application
396  * \param data arguments to the application
397  *
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
400  *
401  * \note this function should only be used to register the parking application and not generally to park calls.
402  */
403 int park_app_exec(struct ast_channel *chan, const char *data);
404
405 /*!
406  * \since 12.0.0
407  * \brief Execution function for the parked call application
408  *
409  * \param chan ast_channel entering the application
410  * \param data arguments to the application
411  *
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
414  */
415 int parked_call_app_exec(struct ast_channel *chan, const char *data);
416
417 /*!
418  * \since 12.0.0
419  * \brief Execution function for the park and retrieve application
420  *
421  * \param chan ast_channel entering the application
422  * \param data arguments to the application
423  *
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
426  *
427  * \note this function should only be used to register the park and announce application and not generally to park and announce.
428  */
429 int park_and_announce_app_exec(struct ast_channel *chan, const char *data);
430
431 /*!
432  * \since 12.0.0
433  * \brief Register CLI commands
434  *
435  * \retval 0 if successful
436  * \retval -1 on failure
437  */
438 int load_parking_ui(void);
439
440 /*!
441  * \since 12.0.0
442  * \brief Unregister CLI commands
443  */
444 void unload_parking_ui(void);
445
446 /*!
447  * \since 12.0.0
448  * \brief Register manager actions and setup subscriptions for stasis events
449  */
450 int load_parking_manager(void);
451
452 /*!
453  * \since 12.0.0
454  * \brief Unregister manager actions and remove subscriptions for stasis events
455  */
456 void unload_parking_manager(void);
457
458 /*!
459  * \since 12.0.0
460  * \brief Register bridge features for parking
461  *
462  * \retval 0 on success
463  * \retval -1 on failure
464  */
465 int load_parking_bridge_features(void);
466
467 /*!
468  * \since 12.0.0
469  * \brief Unregister features registered by load_parking_bridge_features
470  */
471 void unload_parking_bridge_features(void);
472
473 /*!
474  * \since 12.0.0
475  * \brief Register Parking devstate handler
476  */
477 int load_parking_devstate(void);
478
479 /*!
480  * \since 12.0.0
481  * \brief Unregister Parking devstate handler
482  */
483 void unload_parking_devstate(void);