2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2014, Russell Bryant
6 * Russell Bryant <russell@russellbryant.net>
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.
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.
21 * \brief Periodic dialplan hooks.
23 * \author Russell Bryant <russell@russellbryant.net>
29 <support_level>core</support_level>
30 <depend>app_chanspy</depend>
31 <depend>func_cut</depend>
32 <depend>func_groupcount</depend>
33 <depend>func_uri</depend>
38 ASTERISK_REGISTER_FILE()
40 #include "asterisk/module.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/app.h"
44 #include "asterisk/audiohook.h"
45 #define AST_API_MODULE
46 #include "asterisk/beep.h"
49 <function name="PERIODIC_HOOK" language="en_US">
51 Execute a periodic dialplan hook into the audio of a call.
54 <parameter name="context" required="true">
55 <para>(On Read Only) Context for the hook extension.</para>
57 <parameter name="extension" required="true">
58 <para>(On Read Only) The hook extension.</para>
60 <parameter name="interval" required="true">
61 <para>(On Read Only) Number of seconds in between hook runs.
62 Whole seconds only.</para>
64 <parameter name="hook_id" required="true">
65 <para>(On Write Only) The hook ID.</para>
69 <para>For example, you could use this function to enable playing
70 a periodic <literal>beep</literal> sound in a call.</para>
72 <para>To turn on:</para>
73 <para> Set(BEEPID=${PERIODIC_HOOK(hooks,beep,180)})</para>
75 <para>To turn off:</para>
76 <para> Set(PERIODIC_HOOK(${BEEPID})=off)</para>
78 <para>To turn back on again later:</para>
79 <para>Set(PERIODIC_HOOK(${BEEPID})=on)</para>
81 <para>It is important to note that the hook does not actually
82 run on the channel itself. It runs asynchronously on a new channel.
83 Any audio generated by the hook gets injected into the call for
84 the channel PERIODIC_HOOK() was set on.</para>
86 <para>The hook dialplan will have two variables available.
87 <variable>HOOK_CHANNEL</variable> is the channel the hook is
88 enabled on. <variable>HOOK_ID</variable> is the hook ID for
89 enabling or disabling the hook.</para>
94 static const char context_name[] = "__func_periodic_hook_context__";
95 static const char exten_name[] = "hook";
96 static const char full_exten_name[] = "hook@__func_periodic_hook_context__";
98 static const char beep_exten[] = "beep";
101 * \brief Last used hook ID
103 * This is incremented each time a hook is created to give each hook a unique
106 static unsigned int global_hook_id;
108 /*! State put in a datastore to track the state of the hook */
111 * \brief audiohook used as a callback into this module
113 * \note The code assumes this is the first element in the struct
115 struct ast_audiohook audiohook;
116 /*! Seconds between each hook run */
117 unsigned int interval;
118 /*! The last time the hook ran */
119 struct timeval last_hook;
120 /*! Dialplan context for the hook */
122 /*! Dialplan extension for the hook */
125 unsigned int hook_id;
126 /*! Non-zero if the hook is currently disabled */
127 unsigned char disabled;
130 static void hook_datastore_destroy_callback(void *data)
132 struct hook_state *state = data;
134 ast_audiohook_lock(&state->audiohook);
135 ast_audiohook_detach(&state->audiohook);
136 ast_audiohook_unlock(&state->audiohook);
137 ast_audiohook_destroy(&state->audiohook);
139 ast_free(state->context);
140 ast_free(state->exten);
143 ast_module_unref(ast_module_info->self);
146 static const struct ast_datastore_info hook_datastore = {
148 .destroy = hook_datastore_destroy_callback,
151 /*! Arguments to the thread that launches the hook */
152 struct hook_thread_arg {
155 /*! Name of the channel the hook was set on */
157 /*! Dialplan context for the hook */
159 /*! Dialplan extension for the hook */
163 static void hook_thread_arg_destroy(struct hook_thread_arg *arg)
165 ast_free(arg->hook_id);
166 ast_free(arg->chan_name);
167 ast_free(arg->context);
168 ast_free(arg->exten);
172 static void *hook_launch_thread(void *data)
174 struct hook_thread_arg *arg = data;
175 struct ast_variable hook_id = {
177 .value = arg->hook_id,
179 struct ast_variable chan_name_var = {
180 .name = "HOOK_CHANNEL",
181 .value = arg->chan_name,
185 ast_pbx_outgoing_exten("Local", NULL, full_exten_name, 60,
186 arg->context, arg->exten, 1, NULL, 0, NULL, NULL, &chan_name_var,
187 NULL, NULL, 1, NULL);
189 hook_thread_arg_destroy(arg);
194 static struct hook_thread_arg *hook_thread_arg_alloc(struct ast_channel *chan,
195 struct hook_state *state)
197 struct hook_thread_arg *arg;
199 if (!(arg = ast_calloc(1, sizeof(*arg)))) {
203 ast_channel_lock(chan);
204 arg->chan_name = ast_strdup(ast_channel_name(chan));
205 ast_channel_unlock(chan);
206 if (!arg->chan_name) {
207 hook_thread_arg_destroy(arg);
211 if (ast_asprintf(&arg->hook_id, "%u", state->hook_id) == -1) {
212 hook_thread_arg_destroy(arg);
216 if (!(arg->context = ast_strdup(state->context))) {
217 hook_thread_arg_destroy(arg);
221 if (!(arg->exten = ast_strdup(state->exten))) {
222 hook_thread_arg_destroy(arg);
229 static int do_hook(struct ast_channel *chan, struct hook_state *state)
232 struct hook_thread_arg *arg;
235 if (!(arg = hook_thread_arg_alloc(chan, state))) {
240 * We don't want to block normal frame processing *at all* while we kick
241 * this off, so do it in a new thread.
243 res = ast_pthread_create_detached_background(&t, NULL, hook_launch_thread, arg);
245 hook_thread_arg_destroy(arg);
251 static int hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan,
252 struct ast_frame *frame, enum ast_audiohook_direction direction)
254 struct hook_state *state = (struct hook_state *) audiohook; /* trust me. */
258 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || state->disabled) {
263 if (ast_tvdiff_ms(now, state->last_hook) > state->interval * 1000) {
264 if ((res = do_hook(chan, state))) {
266 ast_channel_lock(chan);
267 name = ast_strdupa(ast_channel_name(chan));
268 ast_channel_unlock(chan);
269 ast_log(LOG_WARNING, "Failed to run hook on '%s'\n", name);
271 state->last_hook = now;
277 static struct hook_state *hook_state_alloc(const char *context, const char *exten,
278 unsigned int interval, unsigned int hook_id)
280 struct hook_state *state;
282 if (!(state = ast_calloc(1, sizeof(*state)))) {
286 state->context = ast_strdup(context);
287 state->exten = ast_strdup(exten);
288 state->interval = interval;
289 state->hook_id = hook_id;
291 ast_audiohook_init(&state->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE,
292 AST_MODULE, AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
293 state->audiohook.manipulate_callback = hook_callback;
298 static int init_hook(struct ast_channel *chan, const char *context, const char *exten,
299 unsigned int interval, unsigned int hook_id)
301 struct hook_state *state;
302 struct ast_datastore *datastore;
305 snprintf(uid, sizeof(uid), "%u", hook_id);
307 if (!(datastore = ast_datastore_alloc(&hook_datastore, uid))) {
310 ast_module_ref(ast_module_info->self);
311 if (!(state = hook_state_alloc(context, exten, interval, hook_id))) {
312 ast_datastore_free(datastore);
315 datastore->data = state;
317 ast_channel_lock(chan);
318 ast_channel_datastore_add(chan, datastore);
319 ast_audiohook_attach(chan, &state->audiohook);
320 ast_channel_unlock(chan);
325 static int hook_on(struct ast_channel *chan, const char *data, unsigned int hook_id)
327 char *parse = ast_strdupa(S_OR(data, ""));
328 AST_DECLARE_APP_ARGS(args,
329 AST_APP_ARG(context);
331 AST_APP_ARG(interval);
333 unsigned int interval;
335 AST_STANDARD_APP_ARGS(args, parse);
337 if (ast_strlen_zero(args.interval) ||
338 sscanf(args.interval, "%30u", &interval) != 1 || interval == 0) {
339 ast_log(LOG_WARNING, "Invalid hook interval: '%s'\n", S_OR(args.interval, ""));
343 if (ast_strlen_zero(args.context) || ast_strlen_zero(args.exten)) {
344 ast_log(LOG_WARNING, "A context and extension are required for PERIODIC_HOOK().\n");
348 ast_debug(1, "hook to %s@%s enabled on %s with interval of %u seconds\n",
349 args.exten, args.context, ast_channel_name(chan), interval);
351 return init_hook(chan, args.context, args.exten, interval, hook_id);
354 static int hook_off(struct ast_channel *chan, const char *hook_id)
356 struct ast_datastore *datastore;
357 struct hook_state *state;
359 if (ast_strlen_zero(hook_id)) {
363 ast_channel_lock(chan);
365 if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, hook_id))) {
366 ast_log(LOG_WARNING, "Hook with ID '%s' not found on channel '%s'\n", hook_id,
367 ast_channel_name(chan));
368 ast_channel_unlock(chan);
372 state = datastore->data;
375 ast_channel_unlock(chan);
380 static int hook_read(struct ast_channel *chan, const char *cmd, char *data,
381 char *buf, size_t len)
383 unsigned int hook_id;
389 hook_id = (unsigned int) ast_atomic_fetchadd_int((int *) &global_hook_id, 1);
391 snprintf(buf, len, "%u", hook_id);
393 return hook_on(chan, data, hook_id);
396 static int hook_re_enable(struct ast_channel *chan, const char *uid)
398 struct ast_datastore *datastore;
399 struct hook_state *state;
401 if (ast_strlen_zero(uid)) {
405 ast_channel_lock(chan);
407 if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, uid))) {
408 ast_log(LOG_WARNING, "Hook with ID '%s' not found on '%s'\n",
409 uid, ast_channel_name(chan));
410 ast_channel_unlock(chan);
414 state = datastore->data;
417 ast_channel_unlock(chan);
422 static int hook_write(struct ast_channel *chan, const char *cmd, char *data,
431 if (ast_false(value)) {
432 res = hook_off(chan, data);
433 } else if (ast_true(value)) {
434 res = hook_re_enable(chan, data);
436 ast_log(LOG_WARNING, "Invalid value for PERIODIC_HOOK function: '%s'\n", value);
443 static struct ast_custom_function hook_function = {
444 .name = "PERIODIC_HOOK",
449 static int unload_module(void)
451 ast_context_destroy(NULL, AST_MODULE);
453 return ast_custom_function_unregister(&hook_function);
456 static int load_module(void)
460 if (!ast_context_find_or_create(NULL, NULL, context_name, AST_MODULE)) {
461 ast_log(LOG_ERROR, "Failed to create %s dialplan context.\n", context_name);
462 return AST_MODULE_LOAD_DECLINE;
466 * Based on a handy recipe from the Asterisk Cookbook.
468 ast_add_extension(context_name, 1, exten_name, 1, "", "",
469 "Set", "EncodedChannel=${CUT(HOOK_CHANNEL,-,1-2)}",
471 ast_add_extension(context_name, 1, exten_name, 2, "", "",
472 "Set", "GROUP_NAME=${EncodedChannel}${HOOK_ID}",
474 ast_add_extension(context_name, 1, exten_name, 3, "", "",
475 "Set", "GROUP(periodic-hook)=${GROUP_NAME}",
477 ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf",
478 "$[${GROUP_COUNT(${GROUP_NAME}@periodic-hook)} > 1]?Hangup()",
480 ast_add_extension(context_name, 1, exten_name, 5, "", "",
481 "Set", "ChannelToSpy=${URIDECODE(${EncodedChannel})}",
483 ast_add_extension(context_name, 1, exten_name, 6, "", "",
484 "ChanSpy", "${ChannelToSpy},qEB", NULL, AST_MODULE);
486 res = ast_add_extension(context_name, 1, beep_exten, 1, "", "",
487 "Answer", "", NULL, AST_MODULE);
488 res |= ast_add_extension(context_name, 1, beep_exten, 2, "", "",
489 "Playback", "beep", NULL, AST_MODULE);
491 res = ast_custom_function_register_escalating(&hook_function, AST_CFE_BOTH);
493 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
496 int AST_OPTIONAL_API_NAME(ast_beep_start)(struct ast_channel *chan,
497 unsigned int interval, char *beep_id, size_t len)
499 char args[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 32];
501 snprintf(args, sizeof(args), "%s,%s,%u",
502 context_name, beep_exten, interval);
504 if (hook_read(chan, NULL, args, beep_id, len)) {
505 ast_log(LOG_WARNING, "Failed to enable periodic beep.\n");
512 int AST_OPTIONAL_API_NAME(ast_beep_stop)(struct ast_channel *chan, const char *beep_id)
514 return hook_write(chan, NULL, (char *) beep_id, "off");
517 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Periodic dialplan hooks.",
518 .support_level = AST_MODULE_SUPPORT_CORE,
520 .unload = unload_module,