summaryrefslogtreecommitdiff
path: root/res/parking/parking_controller.c
blob: 9e9e540cd8ab51d4c918b2e75aeaf6522301c117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Jonathan Rose <jrose@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Parking Entry, Exit, and other assorted controls.
 *
 * \author Jonathan Rose <jrose@digium.com>
 */
#include "asterisk.h"

#include "asterisk/logger.h"
#include "res_parking.h"
#include "asterisk/astobj2.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
#include "asterisk/test.h"
#include "asterisk/features.h"
#include "asterisk/bridge_basic.h"

struct ast_bridge *parking_lot_get_bridge(struct parking_lot *lot)
{
	struct ast_bridge *lot_bridge;

	if (lot->parking_bridge) {
		ao2_ref(lot->parking_bridge, +1);
		return lot->parking_bridge;
	}

	lot_bridge = bridge_parking_new(lot);
	if (!lot_bridge) {
		return NULL;
	}

	/* The parking lot needs a reference to the bridge as well. */
	lot->parking_bridge = lot_bridge;
	ao2_ref(lot->parking_bridge, +1);

	return lot_bridge;
}

int parking_channel_set_roles(struct ast_channel *chan, struct parking_lot *lot, int force_ringing)
{
	if (ast_channel_add_bridge_role(chan, "holding_participant")) {
		return -1;
	}

	if (force_ringing) {
		if (ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "ringing")) {
			return -1;
		}
	} else {
		if (ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "musiconhold")) {
			return -1;
		}
		if (!ast_strlen_zero(lot->cfg->mohclass)) {
			if (ast_channel_set_bridge_role_option(chan, "holding_participant", "moh_class", lot->cfg->mohclass)) {
				return -1;
			}
		}
	}

	return 0;
}

struct parking_limits_pvt {
	struct parked_user *user;
};

int unpark_parked_user(struct parked_user *pu)
{
	if (pu->lot) {
		ao2_unlink(pu->lot->parked_users, pu);
		parking_lot_remove_if_unused(pu->lot);
		return 0;
	}

	return -1;
}

int parking_lot_get_space(struct parking_lot *lot, int target_override)
{
	int original_target;
	int current_target;
	struct ao2_iterator i;
	struct parked_user *user;
	int wrap;

	if (lot->cfg->parkfindnext) {
		/* Use next_space if the lot already has next_space set; otherwise use lot start. */
		original_target = lot->next_space ? lot->next_space : lot->cfg->parking_start;
	} else {
		original_target = lot->cfg->parking_start;
	}

	if (target_override >= lot->cfg->parking_start && target_override <= lot->cfg->parking_stop) {
		original_target = target_override;
	}

	current_target = original_target;

	wrap = lot->cfg->parking_start;

	i = ao2_iterator_init(lot->parked_users, 0);
	while ((user = ao2_iterator_next(&i))) {
		/* Increment the wrap on each pass until we find an empty space */
		if (wrap == user->parking_space) {
			wrap += 1;
		}

		if (user->parking_space < current_target) {
			/* It's lower than the anticipated target, so we haven't reached the target yet. */
			ao2_ref(user, -1);
			continue;
		}

		if (user->parking_space > current_target) {
			/* The current target is usable because all items below have been read and the next target is higher than the one we want. */
			ao2_ref(user, -1);
			break;
		}

		/* We found one already parked here. */
		current_target += 1;
		ao2_ref(user, -1);
	}
	ao2_iterator_destroy(&i);

	if (current_target <= lot->cfg->parking_stop) {
		return current_target;
	}

	if (wrap <= lot->cfg->parking_stop) {
		return wrap;
	}

	return -1;
}

static int retrieve_parked_user_targeted(void *obj, void *arg, int flags)
{
	int *target = arg;
	struct parked_user *user = obj;
	if (user->parking_space == *target) {
		return CMP_MATCH;
	}

	return 0;
}

struct parked_user *parking_lot_retrieve_parked_user(struct parking_lot *lot, int target)
{
	RAII_VAR(struct parked_user *, user, NULL, ao2_cleanup);

	if (target < 0) {
		user = ao2_callback(lot->parked_users, 0, NULL, NULL);
	} else {
		user = ao2_callback(lot->parked_users, 0, retrieve_parked_user_targeted, &target);
	}

	if (!user) {
		return NULL;
	}

	ao2_lock(user);
	if (user->resolution != PARK_UNSET) {
		/* Abandon. Something else has resolved the parked user before we got to it. */
		ao2_unlock(user);
		return NULL;
	}

	ao2_unlink(lot->parked_users, user);
	user->resolution = PARK_ANSWERED;
	ao2_unlock(user);

	parking_lot_remove_if_unused(user->lot);

	/* Bump the ref count by 1 since the RAII_VAR will eat the reference otherwise */
	ao2_ref(user, +1);
	return user;
}

void parked_call_retrieve_enable_features(struct ast_channel *chan, struct parking_lot *lot, int recipient_mode)
{
	/* Enabling features here should be additive to features that are already on the channel. */
	struct ast_flags feature_flags = { 0 };
	struct ast_flags *existing_features;

	ast_channel_lock(chan);
	existing_features = ast_bridge_features_ds_get(chan);
	if (existing_features) {
		feature_flags = *existing_features;
	}

	if (lot->cfg->parkedcalltransfers & recipient_mode) {
		ast_set_flag(&feature_flags, AST_FEATURE_REDIRECT);
	}

	if (lot->cfg->parkedcallreparking & recipient_mode) {
		ast_set_flag(&feature_flags, AST_FEATURE_PARKCALL);
	}

	if (lot->cfg->parkedcallhangup & recipient_mode) {
		ast_set_flag(&feature_flags, AST_FEATURE_DISCONNECT);
	}

	if (lot->cfg->parkedcallrecording & recipient_mode) {
		ast_set_flag(&feature_flags, AST_FEATURE_AUTOMIXMON);
	}

	ast_bridge_features_ds_set(chan, &feature_flags);
	ast_channel_unlock(chan);

	return;
}

void flatten_dial_string(char *dialstring)
{
	int i;

	for (i = 0; dialstring[i]; i++) {
		if (dialstring[i] == '/') {
			/* The underscore is the flattest character of all. */
			dialstring[i] = '_';
		}
	}
}

int comeback_goto(struct parked_user *pu, struct parking_lot *lot)
{
	struct ast_channel *chan = pu->chan;
	char *peername_flat = ast_strdupa(pu->parker_dial_string);

	/* Flatten the peername so that it can be used for performing the timeout PBX operations */
	flatten_dial_string(peername_flat);

	if (lot->cfg->comebacktoorigin) {
		if (ast_exists_extension(chan, PARK_DIAL_CONTEXT, peername_flat, 1, NULL)) {
			ast_async_goto(chan, PARK_DIAL_CONTEXT, peername_flat, 1);
			return 0;
		} else {
			ast_log(LOG_ERROR, "Can not start %s at %s,%s,1 because extension does not exist. Terminating call.\n",
				ast_channel_name(chan), PARK_DIAL_CONTEXT, peername_flat);
			return -1;
		}
	}

	if (ast_exists_extension(chan, lot->cfg->comebackcontext, peername_flat, 1, NULL)) {
		ast_async_goto(chan, lot->cfg->comebackcontext, peername_flat, 1);
		return 0;
	}

	if (ast_exists_extension(chan, lot->cfg->comebackcontext, "s", 1, NULL)) {
		ast_verb(2, "Could not start %s at %s,%s,1. Using 's@%s' instead.\n", ast_channel_name(chan),
			lot->cfg->comebackcontext, peername_flat, lot->cfg->comebackcontext);
		ast_async_goto(chan, lot->cfg->comebackcontext, "s", 1);
		return 0;
	}

	ast_verb(2, "Can not start %s at %s,%s,1 and exten 's@%s' does not exist. Using 's@default'\n",
		ast_channel_name(chan),
		lot->cfg->comebackcontext, peername_flat, lot->cfg->comebackcontext);
	ast_async_goto(chan, "default", "s", 1);

	return 0;
}