parking_tests: Fix assertions and possibly crashes in res_parking unit tests
[asterisk/asterisk.git] / res / parking / parking_controller.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 Entry, Exit, and other assorted controls.
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  */
25 #include "asterisk.h"
26
27 #include "asterisk/logger.h"
28 #include "res_parking.h"
29 #include "asterisk/astobj2.h"
30 #include "asterisk/utils.h"
31 #include "asterisk/manager.h"
32 #include "asterisk/test.h"
33 #include "asterisk/features.h"
34 #include "asterisk/bridge_basic.h"
35
36 struct ast_bridge *parking_lot_get_bridge(struct parking_lot *lot)
37 {
38         struct ast_bridge *lot_bridge;
39
40         if (lot->parking_bridge) {
41                 ao2_ref(lot->parking_bridge, +1);
42                 return lot->parking_bridge;
43         }
44
45         lot_bridge = bridge_parking_new(lot);
46         if (!lot_bridge) {
47                 return NULL;
48         }
49
50         /* The parking lot needs a reference to the bridge as well. */
51         lot->parking_bridge = lot_bridge;
52         ao2_ref(lot->parking_bridge, +1);
53
54         return lot_bridge;
55 }
56
57 int parking_channel_set_roles(struct ast_channel *chan, struct parking_lot *lot, int force_ringing)
58 {
59         if (ast_channel_add_bridge_role(chan, "holding_participant")) {
60                 return -1;
61         }
62
63         if (force_ringing) {
64                 if (ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "ringing")) {
65                         return -1;
66                 }
67         } else {
68                 if (ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "musiconhold")) {
69                         return -1;
70                 }
71                 if (!ast_strlen_zero(lot->cfg->mohclass)) {
72                         if (ast_channel_set_bridge_role_option(chan, "holding_participant", "moh_class", lot->cfg->mohclass)) {
73                                 return -1;
74                         }
75                 }
76         }
77
78         return 0;
79 }
80
81 struct parking_limits_pvt {
82         struct parked_user *user;
83 };
84
85 int unpark_parked_user(struct parked_user *pu)
86 {
87         if (pu->lot) {
88                 ao2_unlink(pu->lot->parked_users, pu);
89                 parking_lot_remove_if_unused(pu->lot);
90                 return 0;
91         }
92
93         return -1;
94 }
95
96 int parking_lot_get_space(struct parking_lot *lot, int target_override)
97 {
98         int original_target;
99         int current_target;
100         struct ao2_iterator i;
101         struct parked_user *user;
102         int wrap;
103
104         if (lot->cfg->parkfindnext) {
105                 /* Use next_space if the lot already has next_space set; otherwise use lot start. */
106                 original_target = lot->next_space ? lot->next_space : lot->cfg->parking_start;
107         } else {
108                 original_target = lot->cfg->parking_start;
109         }
110
111         if (target_override >= lot->cfg->parking_start && target_override <= lot->cfg->parking_stop) {
112                 original_target = target_override;
113         }
114
115         current_target = original_target;
116
117         wrap = lot->cfg->parking_start;
118
119         i = ao2_iterator_init(lot->parked_users, 0);
120         while ((user = ao2_iterator_next(&i))) {
121                 /* Increment the wrap on each pass until we find an empty space */
122                 if (wrap == user->parking_space) {
123                         wrap += 1;
124                 }
125
126                 if (user->parking_space < current_target) {
127                         /* It's lower than the anticipated target, so we haven't reached the target yet. */
128                         ao2_ref(user, -1);
129                         continue;
130                 }
131
132                 if (user->parking_space > current_target) {
133                         /* The current target is usable because all items below have been read and the next target is higher than the one we want. */
134                         ao2_ref(user, -1);
135                         break;
136                 }
137
138                 /* We found one already parked here. */
139                 current_target += 1;
140                 ao2_ref(user, -1);
141         }
142         ao2_iterator_destroy(&i);
143
144         if (current_target <= lot->cfg->parking_stop) {
145                 return current_target;
146         }
147
148         if (wrap <= lot->cfg->parking_stop) {
149                 return wrap;
150         }
151
152         return -1;
153 }
154
155 static int retrieve_parked_user_targeted(void *obj, void *arg, int flags)
156 {
157         int *target = arg;
158         struct parked_user *user = obj;
159         if (user->parking_space == *target) {
160                 return CMP_MATCH;
161         }
162
163         return 0;
164 }
165
166 struct parked_user *parking_lot_retrieve_parked_user(struct parking_lot *lot, int target)
167 {
168         RAII_VAR(struct parked_user *, user, NULL, ao2_cleanup);
169
170         if (target < 0) {
171                 user = ao2_callback(lot->parked_users, 0, NULL, NULL);
172         } else {
173                 user = ao2_callback(lot->parked_users, 0, retrieve_parked_user_targeted, &target);
174         }
175
176         if (!user) {
177                 return NULL;
178         }
179
180         ao2_lock(user);
181         if (user->resolution != PARK_UNSET) {
182                 /* Abandon. Something else has resolved the parked user before we got to it. */
183                 ao2_unlock(user);
184                 return NULL;
185         }
186
187         ao2_unlink(lot->parked_users, user);
188         user->resolution = PARK_ANSWERED;
189         ao2_unlock(user);
190
191         parking_lot_remove_if_unused(user->lot);
192
193         /* Bump the ref count by 1 since the RAII_VAR will eat the reference otherwise */
194         ao2_ref(user, +1);
195         return user;
196 }
197
198 void parked_call_retrieve_enable_features(struct ast_channel *chan, struct parking_lot *lot, int recipient_mode)
199 {
200         /* Enabling features here should be additive to features that are already on the channel. */
201         struct ast_flags feature_flags = { 0 };
202         struct ast_flags *existing_features;
203
204         ast_channel_lock(chan);
205         existing_features = ast_bridge_features_ds_get(chan);
206         if (existing_features) {
207                 feature_flags = *existing_features;
208         }
209
210         if (lot->cfg->parkedcalltransfers & recipient_mode) {
211                 ast_set_flag(&feature_flags, AST_FEATURE_REDIRECT);
212         }
213
214         if (lot->cfg->parkedcallreparking & recipient_mode) {
215                 ast_set_flag(&feature_flags, AST_FEATURE_PARKCALL);
216         }
217
218         if (lot->cfg->parkedcallhangup & recipient_mode) {
219                 ast_set_flag(&feature_flags, AST_FEATURE_DISCONNECT);
220         }
221
222         if (lot->cfg->parkedcallrecording & recipient_mode) {
223                 ast_set_flag(&feature_flags, AST_FEATURE_AUTOMIXMON);
224         }
225
226         ast_bridge_features_ds_set(chan, &feature_flags);
227         ast_channel_unlock(chan);
228
229         return;
230 }
231
232 void flatten_dial_string(char *dialstring)
233 {
234         int i;
235
236         for (i = 0; dialstring[i]; i++) {
237                 if (dialstring[i] == '/') {
238                         /* The underscore is the flattest character of all. */
239                         dialstring[i] = '_';
240                 }
241         }
242 }
243
244 int comeback_goto(struct parked_user *pu, struct parking_lot *lot)
245 {
246         struct ast_channel *chan = pu->chan;
247         char *peername_flat = ast_strdupa(pu->parker_dial_string);
248
249         /* Flatten the peername so that it can be used for performing the timeout PBX operations */
250         flatten_dial_string(peername_flat);
251
252         if (lot->cfg->comebacktoorigin) {
253                 if (ast_exists_extension(chan, PARK_DIAL_CONTEXT, peername_flat, 1, NULL)) {
254                         ast_async_goto(chan, PARK_DIAL_CONTEXT, peername_flat, 1);
255                         return 0;
256                 } else {
257                         ast_log(LOG_ERROR, "Can not start %s at %s,%s,1 because extension does not exist. Terminating call.\n",
258                                 ast_channel_name(chan), PARK_DIAL_CONTEXT, peername_flat);
259                         return -1;
260                 }
261         }
262
263         if (ast_exists_extension(chan, lot->cfg->comebackcontext, peername_flat, 1, NULL)) {
264                 ast_async_goto(chan, lot->cfg->comebackcontext, peername_flat, 1);
265                 return 0;
266         }
267
268         if (ast_exists_extension(chan, lot->cfg->comebackcontext, "s", 1, NULL)) {
269                 ast_verb(2, "Could not start %s at %s,%s,1. Using 's@%s' instead.\n", ast_channel_name(chan),
270                         lot->cfg->comebackcontext, peername_flat, lot->cfg->comebackcontext);
271                 ast_async_goto(chan, lot->cfg->comebackcontext, "s", 1);
272                 return 0;
273         }
274
275         ast_verb(2, "Can not start %s at %s,%s,1 and exten 's@%s' does not exist. Using 's@default'\n",
276                 ast_channel_name(chan),
277                 lot->cfg->comebackcontext, peername_flat, lot->cfg->comebackcontext);
278         ast_async_goto(chan, "default", "s", 1);
279
280         return 0;
281 }