Debian Codegraph @

apps/app_bridgewait.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 *
8 * Author: Jonathan Rose <[email protected]>
9 *
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
19 */
20
21 /*! \file
22 *
23 * \brief Application to place the channel into a holding Bridge
24 *
25 * \author Jonathan Rose <[email protected]>
26 *
27 * \ingroup applications
28 */
29
30 /*** MODULEINFO
31 <depend>bridge_holding</depend>
32 <support_level>core</support_level>
33 ***/
34
35 #include "asterisk.h"
36
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/features.h"
42 #include "asterisk/say.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/app.h"
46 #include "asterisk/bridge.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/astobj2.h"
49 #include "asterisk/causes.h"
50
51 /*** DOCUMENTATION
52 <application name="BridgeWait" language="en_US">
53 <since>
54 <version>12.0.0</version>
55 </since>
56 <synopsis>
57 Put a call into the holding bridge.
58 </synopsis>
59 <syntax>
60 <parameter name="name">
61 <para>Name of the holding bridge to join. This is a handle for <literal>BridgeWait</literal>
62 only and does not affect the actual bridges that are created. If not provided,
63 the reserved name <literal>default</literal> will be used.
64 </para>
65 </parameter>
66 <parameter name="role" required="false">
67 <para>Defines the channel's purpose for entering the holding bridge. Values are case sensitive.
68 </para>
69 <enumlist>
70 <enum name="participant">
71 <para>The channel will enter the holding bridge to be placed on hold
72 until it is removed from the bridge for some reason. (default)</para>
73 </enum>
74 <enum name="announcer">
75 <para>The channel will enter the holding bridge to make announcements
76 to channels that are currently in the holding bridge. While an
77 announcer is present, holding for the participants will be
78 suspended.</para>
79 </enum>
80 </enumlist>
81 </parameter>
82 <parameter name="options">
83 <optionlist>
84 <option name="m">
85 <argument name="class" required="true" />
86 <para>The specified MOH class will be used/suggested for
87 music on hold operations. This option will only be useful for
88 entertainment modes that use it (m and h).</para>
89 </option>
90 <option name="e">
91 <para>Which entertainment mechanism should be used while on hold
92 in the holding bridge. Only the first letter is read.</para>
93 <enumlist>
94 <enum name="m"><para>Play music on hold (default)</para></enum>
95 <enum name="r"><para>Ring without pause</para></enum>
96 <enum name="s"><para>Generate silent audio</para></enum>
97 <enum name="h"><para>Put the channel on hold</para></enum>
98 <enum name="n"><para>No entertainment</para></enum>
99 </enumlist>
100 </option>
101 <option name="S">
102 <argument name="duration" required="true" />
103 <para>Automatically exit the bridge and return to the PBX after
104 <emphasis>duration</emphasis> seconds.</para>
105 </option>
106 <option name="n">
107 <para>Do not automatically answer the channel.</para>
108 </option>
109 </optionlist>
110 </parameter>
111 </syntax>
112 <description>
113 <para>This application places the incoming channel into a holding bridge.
114 The channel will then wait in the holding bridge until some event occurs
115 which removes it from the holding bridge.</para>
116 <note><para>This application will answer calls which haven't already
117 been answered, unless the n option is provided.</para></note>
118 </description>
119 </application>
120 ***/
121
122 #define APP_NAME "BridgeWait"
123 #define DEFAULT_BRIDGE_NAME "default"
124
125 static struct ao2_container *wait_bridge_wrappers;
126
127 struct wait_bridge_wrapper {
128 struct ast_bridge *bridge; /*!< Bridge being wrapped by this wrapper */
129 char name[0]; /*!< Name of the holding bridge wrapper */
130 };
131
132 static void wait_bridge_wrapper_destructor(void *obj)
133 {
134 struct wait_bridge_wrapper *wrapper = obj;
135
136 if (wrapper->bridge) {
137 ast_bridge_destroy(wrapper->bridge, 0);
138 }
139 }
140
141 static struct wait_bridge_wrapper *wait_bridge_wrapper_find_by_name(const char *bridge_name)
142 {
143 return ao2_find(wait_bridge_wrappers, bridge_name, OBJ_KEY);
144 }
145
146 static int wait_bridge_hash_fn(const void *obj, const int flags)
147 {
148 const struct wait_bridge_wrapper *entry;
149 const char *key;
150
151 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
152 case OBJ_KEY:
153 key = obj;
154 return ast_str_hash(key);
155 case OBJ_POINTER:
156 entry = obj;
157 return ast_str_hash(entry->name);
158 default:
159 /* Hash can only work on something with a full key. */
160 ast_assert(0);
161 return 0;
162 }
163 }
164
165 static int wait_bridge_sort_fn(const void *obj_left, const void *obj_right, const int flags)
166 {
167 const struct wait_bridge_wrapper *left = obj_left;
168 const struct wait_bridge_wrapper *right = obj_right;
169 const char *right_key = obj_right;
170 int cmp;
171
172 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
173 case OBJ_POINTER:
174 right_key = right->name;
175 /* Fall through */
176 case OBJ_KEY:
177 cmp = strcmp(left->name, right_key);
178 break;
179 case OBJ_PARTIAL_KEY:
180 cmp = strncmp(left->name, right_key, strlen(right_key));
181 break;
182 default:
183 /* Sort can only work on something with a full or partial key. */
184 ast_assert(0);
185 cmp = 0;
186 break;
187 }
188 return cmp;
189 }
190
191 enum bridgewait_flags {
192 MUXFLAG_MOHCLASS = (1 << 0),
193 MUXFLAG_ENTERTAINMENT = (1 << 1),
194 MUXFLAG_TIMEOUT = (1 << 2),
195 MUXFLAG_NOANSWER = (1 << 3),
196 };
197
198 enum bridgewait_args {
199 OPT_ARG_ENTERTAINMENT,
200 OPT_ARG_MOHCLASS,
201 OPT_ARG_TIMEOUT,
202 OPT_ARG_ARRAY_SIZE, /* Always the last element of the enum */
203 };
204
205 AST_APP_OPTIONS(bridgewait_opts, {
206 AST_APP_OPTION_ARG('e', MUXFLAG_ENTERTAINMENT, OPT_ARG_ENTERTAINMENT),
207 AST_APP_OPTION_ARG('m', MUXFLAG_MOHCLASS, OPT_ARG_MOHCLASS),
208 AST_APP_OPTION_ARG('S', MUXFLAG_TIMEOUT, OPT_ARG_TIMEOUT),
209 AST_APP_OPTION('n', MUXFLAG_NOANSWER),
210 });
211
212 static int bridgewait_timeout_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
213 {
214 ast_verb(3, "Channel %s timed out.\n", ast_channel_name(bridge_channel->chan));
215 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
216 AST_CAUSE_NORMAL_CLEARING);
217 return -1;
218 }
219
220 static int apply_option_timeout(struct ast_bridge_features *features, char *duration_arg)
221 {
222 unsigned int duration;
223
224 if (ast_strlen_zero(duration_arg)) {
225 ast_log(LOG_ERROR, "Timeout option 'S': No value provided.\n");
226 return -1;
227 }
228 if (sscanf(duration_arg, "%u", &duration) != 1 || duration == 0) {
229 ast_log(LOG_ERROR, "Timeout option 'S': Invalid value provided '%s'.\n",
230 duration_arg);
231 return -1;
232 }
233
234 duration *= 1000;
235 if (ast_bridge_interval_hook(features, 0, duration, bridgewait_timeout_callback,
236 NULL, NULL, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
237 ast_log(LOG_ERROR, "Timeout option 'S': Could not create timer.\n");
238 return -1;
239 }
240
241 return 0;
242 }
243
244 static int apply_option_moh(struct ast_channel *chan, const char *class_arg)
245 {
246 return ast_channel_set_bridge_role_option(chan, "holding_participant", "moh_class", class_arg);
247 }
248
249 static int apply_option_entertainment(struct ast_channel *chan, const char *entertainment_arg)
250 {
251 char entertainment = entertainment_arg[0];
252
253 switch (entertainment) {
254 case 'm':
255 return ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "musiconhold");
256 case 'r':
257 return ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "ringing");
258 case 's':
259 return ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "silence");
260 case 'h':
261 return ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "hold");
262 case 'n':
263 return ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "none");
264 default:
265 ast_log(LOG_ERROR, "Invalid argument for BridgeWait entertainment '%s'\n", entertainment_arg);
266 return -1;
267 }
268 }
269
270 enum wait_bridge_roles {
271 ROLE_PARTICIPANT = 0,
272 ROLE_ANNOUNCER,
273 ROLE_INVALID,
274 };
275
276 static int process_options(struct ast_channel *chan, struct ast_flags *flags, char **opts, struct ast_bridge_features *features, enum wait_bridge_roles role)
277 {
278 if (ast_test_flag(flags, MUXFLAG_TIMEOUT)) {
279 if (apply_option_timeout(features, opts[OPT_ARG_TIMEOUT])) {
280 return -1;
281 }
282 }
283
284 switch (role) {
285 case ROLE_PARTICIPANT:
286 if (ast_channel_add_bridge_role(chan, "holding_participant")) {
287 return -1;
288 }
289
290 if (ast_test_flag(flags, MUXFLAG_MOHCLASS)) {
291 if (apply_option_moh(chan, opts[OPT_ARG_MOHCLASS])) {
292 return -1;
293 }
294 }
295
296 if (ast_test_flag(flags, MUXFLAG_ENTERTAINMENT)) {
297 if (apply_option_entertainment(chan, opts[OPT_ARG_ENTERTAINMENT])) {
298 return -1;
299 }
300 }
301
302 break;
303 case ROLE_ANNOUNCER:
304 if (ast_channel_add_bridge_role(chan, "announcer")) {
305 return -1;
306 }
307 break;
308 case ROLE_INVALID:
309 ast_assert(0);
310 return -1;
311 }
312
313 return 0;
314 }
315
316 /*!
317 * \internal
318 * \since 12.0.0
319 * \brief Allocate a new holding bridge wrapper with the given bridge name and bridge ID.
320 *
321 * \param bridge_name name of the bridge wrapper
322 * \param bridge the bridge being wrapped
323 *
324 * \retval Pointer to the newly allocated holding bridge wrapper
325 * \retval NULL if allocation failed. The bridge will be destroyed if this function fails.
326 */
327 static struct wait_bridge_wrapper *wait_bridge_wrapper_alloc(const char *bridge_name, struct ast_bridge *bridge)
328 {
329 struct wait_bridge_wrapper *bridge_wrapper;
330
331 bridge_wrapper = ao2_alloc_options(sizeof(*bridge_wrapper) + strlen(bridge_name) + 1,
332 wait_bridge_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
333 if (!bridge_wrapper) {
334 ast_bridge_destroy(bridge, 0);
335 return NULL;
336 }
337
338 strcpy(bridge_wrapper->name, bridge_name);
339 bridge_wrapper->bridge = bridge;
340
341 if (!ao2_link(wait_bridge_wrappers, bridge_wrapper)) {
342 ao2_cleanup(bridge_wrapper);
343 return NULL;
344 }
345
346 return bridge_wrapper;
347 }
348
349 static struct wait_bridge_wrapper *get_wait_bridge_wrapper(const char *bridge_name)
350 {
351 struct wait_bridge_wrapper * wrapper;
352 struct ast_bridge *bridge = NULL;
353
354 SCOPED_AO2LOCK(lock, wait_bridge_wrappers);
355
356 if ((wrapper = wait_bridge_wrapper_find_by_name(bridge_name))) {
357 return wrapper;
358 }
359
360 /*
361 * Holding bridges can allow local channel move/swap
362 * optimization to the bridge. However, we cannot allow it for
363 * this holding bridge because the call will lose the channel
364 * roles and dialplan location as a result.
365 */
366 bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_HOLDING,
367 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
368 | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
369 | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED, APP_NAME, bridge_name, NULL);
370
371 if (!bridge) {
372 return NULL;
373 }
374
375 /* The bridge reference is unconditionally passed. */
376 return wait_bridge_wrapper_alloc(bridge_name, bridge);
377 }
378
379 /*!
380 * \internal
381 * \since 12.0.0
382 * \brief If we are down to the last reference of a wrapper and it's still contained within the list, remove it from the list.
383 *
384 * \param wrapper reference to wait bridge wrapper being checked for list culling - will be cleared on exit
385 */
386 static void wait_wrapper_removal(struct wait_bridge_wrapper *wrapper)
387 {
388 if (!wrapper) {
389 return;
390 }
391
392 ao2_lock(wait_bridge_wrappers);
393 if (ao2_ref(wrapper, 0) == 2) {
394 /* Either we have the only real reference or else wrapper isn't in the container anyway. */
395 ao2_unlink(wait_bridge_wrappers, wrapper);
396 }
397 ao2_unlock(wait_bridge_wrappers);
398
399 ao2_cleanup(wrapper);
400 }
401
402 static enum wait_bridge_roles validate_role(const char *role)
403 {
404 if (!strcmp(role, "participant")) {
405 return ROLE_PARTICIPANT;
406 } else if (!strcmp(role, "announcer")) {
407 return ROLE_ANNOUNCER;
408 } else {
409 return ROLE_INVALID;
410 }
411 }
412
413 /*!
414 * \internal
415 * \since 12.0.0
416 * \brief Application callback for the bridgewait application
417 *
418 * \param chan channel running the application
419 * \param data Arguments to the application
420 *
421 * \retval 0 Ran successfully and the call didn't hang up
422 * \retval -1 Failed or the call was hung up by the time the channel exited the holding bridge
423 */
424 static int bridgewait_exec(struct ast_channel *chan, const char *data)
425 {
426 char *bridge_name = DEFAULT_BRIDGE_NAME;
427 struct ast_bridge_features chan_features;
428 struct ast_flags flags = { 0 };
429 char *parse;
430 enum wait_bridge_roles role = ROLE_PARTICIPANT;
431 char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
432 struct wait_bridge_wrapper *bridge_wrapper;
433 int res;
434
435 AST_DECLARE_APP_ARGS(args,
436 AST_APP_ARG(name);
437 AST_APP_ARG(role);
438 AST_APP_ARG(options);
439 AST_APP_ARG(other); /* Any remaining unused arguments */
440 );
441
442 parse = ast_strdupa(data);
443 AST_STANDARD_APP_ARGS(args, parse);
444
445 if (!ast_strlen_zero(args.name)) {
446 bridge_name = args.name;
447 }
448
449 if (!ast_strlen_zero(args.role)) {
450 role = validate_role(args.role);
451 if (role == ROLE_INVALID) {
452 ast_log(LOG_ERROR, "Requested waiting bridge role '%s' is invalid.\n", args.role);
453 return -1;
454 }
455 }
456
457 if (ast_bridge_features_init(&chan_features)) {
458 ast_bridge_features_cleanup(&chan_features);
459 ast_log(LOG_ERROR, "'%s' failed to enter the waiting bridge - could not set up channel features\n",
460 ast_channel_name(chan));
461 return -1;
462 }
463
464 if (args.options) {
465 ast_app_parse_options(bridgewait_opts, &flags, opts, args.options);
466 }
467
468 /* Answer the channel if needed */
469 if (ast_channel_state(chan) != AST_STATE_UP && !ast_test_flag(&flags, MUXFLAG_NOANSWER)) {
470 ast_answer(chan);
471 }
472
473 if (process_options(chan, &flags, opts, &chan_features, role)) {
474 ast_bridge_features_cleanup(&chan_features);
475 return -1;
476 }
477
478 bridge_wrapper = get_wait_bridge_wrapper(bridge_name);
479 if (!bridge_wrapper) {
480 ast_log(LOG_WARNING, "Failed to find or create waiting bridge '%s' for '%s'.\n", bridge_name, ast_channel_name(chan));
481 ast_bridge_features_cleanup(&chan_features);
482 return -1;
483 }
484
485 ast_verb(3, "%s is entering waiting bridge %s:%s\n", ast_channel_name(chan), bridge_name, bridge_wrapper->bridge->uniqueid);
486 res = ast_bridge_join(bridge_wrapper->bridge, chan, NULL, &chan_features, NULL, 0);
487 wait_wrapper_removal(bridge_wrapper);
488 ast_bridge_features_cleanup(&chan_features);
489
490 if (res) {
491 /* For the lifetime of the bridge wrapper the bridge itself will be valid, if an error occurs it is because
492 * of extreme situations.
493 */
494 ast_log(LOG_WARNING, "Failed to join waiting bridge '%s' for '%s'.\n", bridge_name, ast_channel_name(chan));
495 }
496
497 return (res || ast_check_hangup_locked(chan)) ? -1 : 0;
498 }
499
500 static int unload_module(void)
501 {
502 ao2_cleanup(wait_bridge_wrappers);
503
504 return ast_unregister_application(APP_NAME);
505 }
506
507 static int load_module(void)
508 {
509 wait_bridge_wrappers = ao2_container_alloc_hash(
510 AO2_ALLOC_OPT_LOCK_MUTEX, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
511 37, wait_bridge_hash_fn, wait_bridge_sort_fn, NULL);
512
513 if (!wait_bridge_wrappers) {
514 return -1;
515 }
516
517 return ast_register_application_xml(APP_NAME, bridgewait_exec);
518 }
519
520 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Place the channel into a holding bridge application");