d0566a8984ba12d67c2f62ff579ac5ba498dc90f
[asterisk/asterisk.git] / res / parking / parking_bridge.c
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 Parking Bridge Class
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  */
25
26 #include "asterisk.h"
27 #include "asterisk/logger.h"
28 #include "res_parking.h"
29 #include "asterisk/astobj2.h"
30 #include "asterisk/features.h"
31 #include "asterisk/say.h"
32 #include "asterisk/term.h"
33
34 struct ast_bridge_parking
35 {
36         struct ast_bridge base;
37
38         /* private stuff for parking */
39         struct parking_lot *lot;
40 };
41
42 /*!
43  * \internal
44  * \brief ast_bridge parking class destructor
45  * \since 12.0.0
46  *
47  * \param self Bridge to operate upon.
48  *
49  * \note XXX Stub... and it might go unused.
50  *
51  * \return Nothing
52  */
53 static void bridge_parking_destroy(struct ast_bridge_parking *self)
54 {
55         ast_bridge_base_v_table.destroy(&self->base);
56 }
57
58 static void bridge_parking_dissolving(struct ast_bridge_parking *self)
59 {
60         self->lot = NULL;
61         ast_bridge_base_v_table.dissolving(&self->base);
62 }
63
64 static void destroy_parked_user(void *obj)
65 {
66         struct parked_user *pu = obj;
67
68         ao2_cleanup(pu->lot);
69         ao2_cleanup(pu->retriever);
70         ast_free(pu->parker_dial_string);
71 }
72
73 /* Only call this on a parked user that hasn't had its parker_dial_string set already */
74 static int parked_user_set_parker_dial_string(struct parked_user *pu, struct ast_channel *parker)
75 {
76         char *dial_string = ast_strdupa(ast_channel_name(parker));
77
78         ast_channel_name_to_dial_string(dial_string);
79         pu->parker_dial_string = ast_strdup(dial_string);
80
81         if (!pu->parker_dial_string) {
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 /*!
89  * \internal
90  * \since 12
91  * \brief Construct a parked_user struct assigned to the specified parking lot
92  *
93  * \param lot The parking lot we are assigning the user to
94  * \param parkee The channel being parked
95  * \param parker The channel performing the park operation (may be the same channel)
96  * \param parker_dial_string Takes priority over parker for setting the parker dial string if included
97  * \param use_random_space if true, prioritize using a random parking space instead
98  *        of ${PARKINGEXTEN} and/or automatic assignment from the parking lot
99  * \param time_limit If using a custom timeout, this should be supplied so that the
100  *        parked_user struct can provide this information for manager events. If <0,
101  *        use the parking lot limit instead.
102  *
103  * \retval NULL on failure
104  * \retval reference to the parked user
105  *
106  * \note ao2_cleanup this reference when you are done using it or you'll cause leaks.
107  */
108 static struct parked_user *generate_parked_user(struct parking_lot *lot, struct ast_channel *chan, struct ast_channel *parker, const char *parker_dial_string, int use_random_space, int time_limit)
109 {
110         struct parked_user *new_parked_user;
111         int preferred_space = -1; /* Initialize to use parking lot defaults */
112         int parking_space;
113         const char *parkingexten;
114
115         if (lot->mode == PARKINGLOT_DISABLED) {
116                 ast_log(LOG_NOTICE, "Tried to park in a parking lot that is no longer able to be parked to.\n");
117                 return NULL;
118         }
119
120         new_parked_user = ao2_alloc(sizeof(*new_parked_user), destroy_parked_user);
121         if (!new_parked_user) {
122                 return NULL;
123         }
124
125         if (use_random_space) {
126                 preferred_space = ast_random() % (lot->cfg->parking_stop - lot->cfg->parking_start + 1);
127                 preferred_space += lot->cfg->parking_start;
128         } else {
129                 ast_channel_lock(chan);
130                 if ((parkingexten = pbx_builtin_getvar_helper(chan, "PARKINGEXTEN"))) {
131                         parkingexten = ast_strdupa(parkingexten);
132                 }
133                 ast_channel_unlock(chan);
134
135                 if (!ast_strlen_zero(parkingexten)) {
136                         if (sscanf(parkingexten, "%30d", &preferred_space) != 1 || preferred_space <= 0) {
137                                 ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n", parkingexten);
138                                 ao2_ref(new_parked_user, -1);
139                                 return NULL;
140                         }
141                 }
142         }
143
144         /* We need to keep the lot locked between parking_lot_get_space and actually placing it in the lot. Or until we decide not to. */
145         ao2_lock(lot);
146
147         parking_space = parking_lot_get_space(lot, preferred_space);
148         if (parking_space == -1) {
149                 ast_log(LOG_NOTICE, "Failed to get parking space in lot '%s'. All full.\n", lot->name);
150                 ao2_ref(new_parked_user, -1);
151                 ao2_unlock(lot);
152                 return NULL;
153         }
154
155         lot->next_space = ((parking_space + 1) - lot->cfg->parking_start) % (lot->cfg->parking_stop - lot->cfg->parking_start + 1) + lot->cfg->parking_start;
156         new_parked_user->chan = chan;
157         new_parked_user->parking_space = parking_space;
158
159         /* Have the parked user take a reference to the parking lot. This reference should be immutable and released at destruction */
160         new_parked_user->lot = lot;
161         ao2_ref(lot, +1);
162
163         new_parked_user->start = ast_tvnow();
164         new_parked_user->time_limit = (time_limit >= 0) ? time_limit : lot->cfg->parkingtime;
165
166         if (parker_dial_string) {
167                 new_parked_user->parker_dial_string = ast_strdup(parker_dial_string);
168         } else {
169                 if (parked_user_set_parker_dial_string(new_parked_user, parker)) {
170                         ao2_ref(new_parked_user, -1);
171                         ao2_unlock(lot);
172                         return NULL;
173                 }
174         }
175
176         if (!new_parked_user->parker_dial_string) {
177                 ao2_ref(new_parked_user, -1);
178                 ao2_unlock(lot);
179                 return NULL;
180         }
181
182         /* Insert into the parking lot's parked user list. We can unlock the lot now. */
183         ao2_link(lot->parked_users, new_parked_user);
184         ao2_unlock(lot);
185
186         return new_parked_user;
187 }
188
189 /* TODO CEL events for parking */
190
191 /*!
192  * \internal
193  * \brief ast_bridge parking push method.
194  * \since 12.0.0
195  *
196  * \param self Bridge to operate upon
197  * \param bridge_channel Bridge channel to push
198  * \param swap Bridge channel to swap places with if not NULL
199  *
200  * \note On entry, self is already locked
201  *
202  * \retval 0 on success
203  * \retval -1 on failure
204  */
205 static int bridge_parking_push(struct ast_bridge_parking *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
206 {
207         struct parked_user *pu;
208         const char *blind_transfer;
209         RAII_VAR(struct ast_channel *, parker, NULL, ao2_cleanup); /* XXX replace with ast_channel_cleanup when available */
210         RAII_VAR(struct park_common_datastore *, park_datastore, NULL, park_common_datastore_free);
211
212         ast_bridge_base_v_table.push(&self->base, bridge_channel, swap);
213
214         ast_assert(self->lot != NULL);
215
216         /* Answer the channel if needed */
217         if (ast_channel_state(bridge_channel->chan) != AST_STATE_UP) {
218                 ast_answer(bridge_channel->chan);
219         }
220
221         if (swap) {
222                 ao2_lock(swap);
223                 pu = swap->bridge_pvt;
224                 if (!pu) {
225                         /* This should be impossible since the only way a channel can enter in the first place
226                          * is if it has a parked user associated with it */
227                         publish_parked_call_failure(bridge_channel->chan);
228                         ao2_unlock(swap);
229                         return -1;
230                 }
231
232                 /* Give the swap channel's parked user reference to the incoming channel */
233                 pu->chan = bridge_channel->chan;
234                 bridge_channel->bridge_pvt = pu;
235                 swap->bridge_pvt = NULL;
236
237                 /* TODO Add a parked call swap message type to relay information about parked channel swaps */
238
239                 ao2_unlock(swap);
240
241                 parking_set_duration(bridge_channel->features, pu);
242
243                 /* BUGBUG Adding back local channel swapping made us not hear music on hold for the channel that got swapped
244                  * into the parking lot. Setting the roels back up gets around that, but we still need to deal with the ringing option
245                  * to the park application here somehow.
246                  */
247                 parking_channel_set_roles(bridge_channel->chan, self->lot, 0);
248
249                 return 0;
250         }
251
252         if (!(park_datastore = get_park_common_datastore_copy(bridge_channel->chan))) {
253                 /* There was either a failure to apply the datastore when performing park common setup or else we had alloc failures while cloning. Abort. */
254                 return -1;
255         }
256         parker = ast_channel_get_by_name(park_datastore->parker_uuid);
257
258         /* If the parker and the parkee are the same channel pointer, then the channel entered using
259          * the park application. It's possible that the channel that transferred it is still alive (particularly
260          * when a multichannel bridge is parked), so try to get the real parker if possible. */
261         ast_channel_lock(bridge_channel->chan);
262         blind_transfer = S_OR(pbx_builtin_getvar_helper(bridge_channel->chan, "BLINDTRANSFER"),
263                 ast_channel_name(bridge_channel->chan));
264         if (blind_transfer) {
265                 blind_transfer = ast_strdupa(blind_transfer);
266         }
267         ast_channel_unlock(bridge_channel->chan);
268
269         if (parker == bridge_channel->chan) {
270                 struct ast_channel *real_parker = ast_channel_get_by_name(blind_transfer);
271                 if (real_parker) {
272                         ao2_cleanup(parker);
273                         parker = real_parker;
274                 }
275         }
276
277         pu = generate_parked_user(self->lot, bridge_channel->chan, parker,
278                 park_datastore->parker_dial_string, park_datastore->randomize, park_datastore->time_limit);
279
280         if (!pu) {
281                 publish_parked_call_failure(bridge_channel->chan);
282                 return -1;
283         }
284
285         /* If a comeback_override was provided, set it for the parked user's comeback string. */
286         if (park_datastore->comeback_override) {
287                 strncpy(pu->comeback, park_datastore->comeback_override, sizeof(pu->comeback));
288                 pu->comeback[sizeof(pu->comeback) - 1] = '\0';
289         }
290
291         /* Generate ParkedCall Stasis Message */
292         publish_parked_call(pu, PARKED_CALL);
293
294         /* If the parkee and the parker are the same and silence_announce isn't set, play the announcement to the parkee */
295         if (!strcmp(blind_transfer, ast_channel_name(bridge_channel->chan)) && !park_datastore->silence_announce) {
296                 char saynum_buf[16];
297                 snprintf(saynum_buf, sizeof(saynum_buf), "%u %u", 0, pu->parking_space);
298                 ast_bridge_channel_queue_playfile(bridge_channel, say_parking_space, saynum_buf, NULL);
299         }
300
301         /* Apply parking duration limits */
302         parking_set_duration(bridge_channel->features, pu);
303
304         /* Set this to the bridge pvt so that we don't have to refind the parked user associated with this bridge channel again. */
305         bridge_channel->bridge_pvt = pu;
306
307         ast_verb(3, "Parking '" COLORIZE_FMT "' in '" COLORIZE_FMT "' at space %d\n",
308                 COLORIZE(COLOR_BRMAGENTA, 0, ast_channel_name(bridge_channel->chan)),
309                 COLORIZE(COLOR_BRMAGENTA, 0, self->lot->name),
310                 pu->parking_space);
311
312         parking_notify_metermaids(pu->parking_space, self->lot->cfg->parking_con, AST_DEVICE_INUSE);
313
314         return 0;
315 }
316
317 /*!
318  * \internal
319  * \brief ast_bridge parking pull method.
320  * \since 12.0.0
321  *
322  * \param self Bridge to operate upon.
323  * \param bridge_channel Bridge channel to pull.
324  *
325  * \note On entry, self is already locked.
326  *
327  * \return Nothing
328  */
329 static void bridge_parking_pull(struct ast_bridge_parking *self, struct ast_bridge_channel *bridge_channel)
330 {
331         RAII_VAR(struct parked_user *, pu, NULL, ao2_cleanup);
332         ast_bridge_base_v_table.pull(&self->base, bridge_channel);
333
334         /* Take over the bridge channel's pu reference. It will be released when we are done. */
335         pu = bridge_channel->bridge_pvt;
336         bridge_channel->bridge_pvt = NULL;
337
338         /* This should only happen if the exiting channel was swapped out */
339         if (!pu) {
340                 return;
341         }
342
343         /* If we got here without the resolution being set, that's because the call was hung up for some reason without
344          * timing out or being picked up. There may be some forcible park removals later, but the resolution should be
345          * handled in those cases */
346         ao2_lock(pu);
347         if (pu->resolution == PARK_UNSET) {
348                 pu->resolution = PARK_ABANDON;
349         }
350         ao2_unlock(pu);
351
352         parking_notify_metermaids(pu->parking_space, self->lot->cfg->parking_con, AST_DEVICE_NOT_INUSE);
353
354         switch (pu->resolution) {
355         case PARK_UNSET:
356                 /* This should be impossible now since the resolution is forcibly set to abandon if it was unset at this point. Resolution
357                    isn't allowed to be changed when it isn't currently PARK_UNSET. */
358                 return;
359         case PARK_ABANDON:
360                 /* Since the call was abandoned without additional handling, we need to issue the give up event and unpark the user. */
361                 publish_parked_call(pu, PARKED_CALL_GIVEUP);
362                 unpark_parked_user(pu);
363                 return;
364         case PARK_FORCED:
365                 /* PARK_FORCED is currently unused, but it is expected that it would be handled similar to PARK_ANSWERED.
366                  * There is currently no event related to forced parked calls either */
367                 return;
368         case PARK_ANSWERED:
369                 /* If answered or forced, the channel should be pulled from the bridge as part of that process and unlinked from
370                  * the parking lot afterwards. We do need to apply bridge features though and play the courtesy tone if set. */
371                 publish_parked_call(pu, PARKED_CALL_UNPARKED);
372                 parked_call_retrieve_enable_features(bridge_channel->chan, pu->lot, AST_FEATURE_FLAG_BYCALLEE);
373
374                 if (pu->lot->cfg->parkedplay & AST_FEATURE_FLAG_BYCALLEE) {
375                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, pu->lot->cfg->courtesytone, NULL);
376                 }
377
378                 return;
379         case PARK_TIMEOUT:
380                 /* Timeout is similar to abandon because it simply sets the bridge state to end and doesn't
381                  * actually pull the channel. Because of that, unpark should happen in here. */
382                 publish_parked_call(pu, PARKED_CALL_TIMEOUT);
383                 unpark_parked_user(pu);
384                 return;
385         }
386 }
387
388 /*!
389  * \internal
390  * \brief ast_bridge parking notify_masquerade method.
391  * \since 12.0.0
392  *
393  * \param self Bridge to operate upon.
394  * \param bridge_channel Bridge channel that was masqueraded.
395  *
396  * \note On entry, self is already locked.
397  * \note XXX Stub... and it will probably go unused.
398  *
399  * \return Nothing
400  */
401 static void bridge_parking_notify_masquerade(struct ast_bridge_parking *self, struct ast_bridge_channel *bridge_channel)
402 {
403         ast_bridge_base_v_table.notify_masquerade(&self->base, bridge_channel);
404 }
405
406 static void bridge_parking_get_merge_priority(struct ast_bridge_parking *self)
407 {
408         ast_bridge_base_v_table.get_merge_priority(&self->base);
409 }
410
411 struct ast_bridge_methods ast_bridge_parking_v_table = {
412         .name = "parking",
413         .destroy = (ast_bridge_destructor_fn) bridge_parking_destroy,
414         .dissolving = (ast_bridge_dissolving_fn) bridge_parking_dissolving,
415         .push = (ast_bridge_push_channel_fn) bridge_parking_push,
416         .pull = (ast_bridge_pull_channel_fn) bridge_parking_pull,
417         .notify_masquerade = (ast_bridge_notify_masquerade_fn) bridge_parking_notify_masquerade,
418         .get_merge_priority = (ast_bridge_merge_priority_fn) bridge_parking_get_merge_priority,
419 };
420
421 static struct ast_bridge *ast_bridge_parking_init(struct ast_bridge_parking *self, struct parking_lot *bridge_lot)
422 {
423         if (!self) {
424                 return NULL;
425         }
426
427         /* If no lot is defined for the bridge, then we aren't allowing the bridge to be initialized. */
428         if (!bridge_lot) {
429                 ao2_ref(self, -1);
430                 return NULL;
431         }
432
433         /* It doesn't need to be a reference since the bridge only lives as long as the parking lot lives. */
434         self->lot = bridge_lot;
435
436         return &self->base;
437 }
438
439 struct ast_bridge *bridge_parking_new(struct parking_lot *bridge_lot)
440 {
441         void *bridge;
442
443         bridge = ast_bridge_alloc(sizeof(struct ast_bridge_parking), &ast_bridge_parking_v_table);
444         bridge = ast_bridge_base_init(bridge, AST_BRIDGE_CAPABILITY_HOLDING,
445                 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
446                 | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM);
447         bridge = ast_bridge_parking_init(bridge, bridge_lot);
448         bridge = ast_bridge_register(bridge);
449         return bridge;
450 }