Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_periodic_hook.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Russell Bryant
5 *
6 * Russell Bryant <russell@russellbryant.net>
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 Periodic dialplan hooks.
22 *
23 * \author Russell Bryant <russell@russellbryant.net>
24 *
25 * \ingroup functions
26 */
27
28/*** MODULEINFO
29 <depend>app_chanspy</depend>
30 <depend>func_cut</depend>
31 <depend>func_groupcount</depend>
32 <depend>func_uri</depend>
33 <support_level>core</support_level>
34 ***/
35
36#include "asterisk.h"
37
38#include "asterisk/module.h"
39#include "asterisk/channel.h"
40#include "asterisk/pbx.h"
41#include "asterisk/app.h"
42#include "asterisk/audiohook.h"
43#include "asterisk/test.h"
44#define AST_API_MODULE
45#include "asterisk/beep.h"
46
47/*** DOCUMENTATION
48 <function name="PERIODIC_HOOK" language="en_US">
49 <since>
50 <version>13.0.0</version>
51 </since>
52 <synopsis>
53 Execute a periodic dialplan hook into the audio of a call.
54 </synopsis>
55 <syntax>
56 <parameter name="context" required="true">
57 <para>(On Read Only) Context for the hook extension.</para>
58 </parameter>
59 <parameter name="extension" required="true">
60 <para>(On Read Only) The hook extension.</para>
61 </parameter>
62 <parameter name="interval" required="true">
63 <para>(On Read Only) Number of seconds in between hook runs.
64 Whole seconds only.</para>
65 </parameter>
66 <parameter name="hook_id" required="true">
67 <para>(On Write Only) The hook ID.</para>
68 </parameter>
69 </syntax>
70 <description>
71 <para>For example, you could use this function to enable playing
72 a periodic <literal>beep</literal> sound in a call.</para>
73 <para/>
74 <example title="To turn on">
75 same => n,Set(BEEPID=${PERIODIC_HOOK(hooks,beep,180)})
76 </example>
77 <example title="To turn off">
78 same => n,Set(PERIODIC_HOOK(${BEEPID})=off)
79 </example>
80 <example title="To turn back on again later">
81 same => n,Set(PERIODIC_HOOK(${BEEPID})=on)
82 </example>
83 <para>It is important to note that the hook does not actually
84 run on the channel itself. It runs asynchronously on a new channel.
85 Any audio generated by the hook gets injected into the call for
86 the channel PERIODIC_HOOK() was set on.</para>
87 <para/>
88 <para>The hook dialplan will have two variables available.
89 <variable>HOOK_CHANNEL</variable> is the channel the hook is
90 enabled on. <variable>HOOK_ID</variable> is the hook ID for
91 enabling or disabling the hook.</para>
92 </description>
93 </function>
94 ***/
95
96static const char context_name[] = "__func_periodic_hook_context__";
97static const char exten_name[] = "hook";
98static const char full_exten_name[] = "hook@__func_periodic_hook_context__";
99
100static const char beep_exten[] = "beep";
101
102/*!
103 * \brief Last used hook ID
104 *
105 * This is incremented each time a hook is created to give each hook a unique
106 * ID.
107 */
108static unsigned int global_hook_id;
109
110/*! State put in a datastore to track the state of the hook */
112 /*!
113 * \brief audiohook used as a callback into this module
114 *
115 * \note The code assumes this is the first element in the struct
116 */
118 /*! Seconds between each hook run */
119 unsigned int interval;
120 /*! The last time the hook ran */
121 struct timeval last_hook;
122 /*! Dialplan context for the hook */
123 char *context;
124 /*! Dialplan extension for the hook */
125 char *exten;
126 /*! Hook ID */
127 unsigned int hook_id;
128 /*! Non-zero if the hook is currently disabled */
129 unsigned char disabled;
130};
131
132static void hook_datastore_destroy_callback(void *data)
133{
134 struct hook_state *state = data;
135
136 ast_audiohook_lock(&state->audiohook);
137 ast_audiohook_detach(&state->audiohook);
138 ast_audiohook_unlock(&state->audiohook);
139 ast_audiohook_destroy(&state->audiohook);
140
141 ast_free(state->context);
142 ast_free(state->exten);
144}
145
146static const struct ast_datastore_info hook_datastore = {
147 .type = AST_MODULE,
149};
150
151/*! Arguments to the thread that launches the hook */
153 /*! Hook ID */
154 char *hook_id;
155 /*! Name of the channel the hook was set on */
157 /*! Dialplan context for the hook */
158 char *context;
159 /*! Dialplan extension for the hook */
160 char *exten;
161};
162
164{
165 ast_free(arg->hook_id);
166 ast_free(arg->chan_name);
167 ast_free(arg->context);
168 ast_free(arg->exten);
169 ast_free(arg);
170}
171
172static void *hook_launch_thread(void *data)
173{
174 struct hook_thread_arg *arg = data;
175 struct ast_variable hook_id = {
176 .name = "HOOK_ID",
177 .value = arg->hook_id,
178 };
179 struct ast_variable chan_name_var = {
180 .name = "HOOK_CHANNEL",
181 .value = arg->chan_name,
182 .next = &hook_id,
183 };
184
186 arg->context, arg->exten, 1, NULL, AST_OUTGOING_NO_WAIT,
187 NULL, NULL, &chan_name_var, NULL, NULL, 1, NULL);
188
190
191 return NULL;
192}
193
195 struct hook_state *state)
196{
197 struct hook_thread_arg *arg;
198
199 if (!(arg = ast_calloc(1, sizeof(*arg)))) {
200 return NULL;
201 }
202
203 ast_channel_lock(chan);
205 ast_channel_unlock(chan);
206 if (!arg->chan_name) {
208 return NULL;
209 }
210
211 if (ast_asprintf(&arg->hook_id, "%u", state->hook_id) == -1) {
213 return NULL;
214 }
215
216 if (!(arg->context = ast_strdup(state->context))) {
218 return NULL;
219 }
220
221 if (!(arg->exten = ast_strdup(state->exten))) {
223 return NULL;
224 }
225
226 return arg;
227}
228
229static int do_hook(struct ast_channel *chan, struct hook_state *state)
230{
231 pthread_t t;
232 struct hook_thread_arg *arg;
233 int res;
234
235 if (!(arg = hook_thread_arg_alloc(chan, state))) {
236 return -1;
237 }
238
239 /*
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.
242 */
244 if (res != 0) {
246 }
247
248 return res;
249}
250
251static int hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan,
252 struct ast_frame *frame, enum ast_audiohook_direction direction)
253{
254 struct hook_state *state = (struct hook_state *) audiohook; /* trust me. */
255 struct timeval now;
256 int res = 0;
257
258 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || state->disabled) {
259 return 0;
260 }
261
262 now = ast_tvnow();
263 if (ast_tvdiff_ms(now, state->last_hook) > state->interval * 1000) {
264 if ((res = do_hook(chan, state))) {
265 const char *name;
266 ast_channel_lock(chan);
268 ast_channel_unlock(chan);
269 ast_log(LOG_WARNING, "Failed to run hook on '%s'\n", name);
270 }
271 state->last_hook = now;
272 }
273
274 return res;
275}
276
277static struct hook_state *hook_state_alloc(const char *context, const char *exten,
278 unsigned int interval, unsigned int hook_id)
279{
280 struct hook_state *state;
281
282 if (!(state = ast_calloc(1, sizeof(*state)))) {
283 return NULL;
284 }
285
286 state->context = ast_strdup(context);
287 state->exten = ast_strdup(exten);
288 state->interval = interval;
289 state->hook_id = hook_id;
290
293 state->audiohook.manipulate_callback = hook_callback;
294
295 return state;
296}
297
298static int init_hook(struct ast_channel *chan, const char *context, const char *exten,
299 unsigned int interval, unsigned int hook_id)
300{
301 struct hook_state *state;
302 struct ast_datastore *datastore;
303 char uid[32];
304
305 snprintf(uid, sizeof(uid), "%u", hook_id);
306
307 if (!(datastore = ast_datastore_alloc(&hook_datastore, uid))) {
308 return -1;
309 }
310
311 if (!(state = hook_state_alloc(context, exten, interval, hook_id))) {
312 ast_datastore_free(datastore);
313 return -1;
314 }
315 datastore->data = state;
316
317 ast_channel_lock(chan);
318 ast_channel_datastore_add(chan, datastore);
319 ast_audiohook_attach(chan, &state->audiohook);
320 ast_channel_unlock(chan);
321
322 return 0;
323}
324
325static int hook_on(struct ast_channel *chan, const char *data, unsigned int hook_id)
326{
327 char *parse = ast_strdupa(S_OR(data, ""));
330 AST_APP_ARG(exten);
331 AST_APP_ARG(interval);
332 );
333 unsigned int interval;
334
336
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, ""));
340 return -1;
341 }
342
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");
345 return -1;
346 }
347
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);
350 ast_test_suite_event_notify("PERIODIC_HOOK_ENABLED", "Exten: %s\r\nChannel: %s\r\nInterval: %u\r\n",
351 args.exten, ast_channel_name(chan), interval);
352
353 return init_hook(chan, args.context, args.exten, interval, hook_id);
354}
355
356static int hook_off(struct ast_channel *chan, const char *hook_id)
357{
358 struct ast_datastore *datastore;
359 struct hook_state *state;
360
362 return -1;
363 }
364
365 ast_channel_lock(chan);
366
367 if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, hook_id))) {
368 ast_log(LOG_WARNING, "Hook with ID '%s' not found on channel '%s'\n", hook_id,
369 ast_channel_name(chan));
370 ast_channel_unlock(chan);
371 return -1;
372 }
373
374 state = datastore->data;
375 state->disabled = 1;
376
377 ast_channel_unlock(chan);
378
379 return 0;
380}
381
382static int hook_read(struct ast_channel *chan, const char *cmd, char *data,
383 char *buf, size_t len)
384{
385 unsigned int hook_id;
386
387 if (!chan) {
388 return -1;
389 }
390
391 hook_id = (unsigned int) ast_atomic_fetchadd_int((int *) &global_hook_id, 1);
392
393 snprintf(buf, len, "%u", hook_id);
394
395 return hook_on(chan, data, hook_id);
396}
397
398static int hook_re_enable(struct ast_channel *chan, const char *uid)
399{
400 struct ast_datastore *datastore;
401 struct hook_state *state;
402
403 if (ast_strlen_zero(uid)) {
404 return -1;
405 }
406
407 ast_channel_lock(chan);
408
409 if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, uid))) {
410 ast_log(LOG_WARNING, "Hook with ID '%s' not found on '%s'\n",
411 uid, ast_channel_name(chan));
412 ast_channel_unlock(chan);
413 return -1;
414 }
415
416 state = datastore->data;
417 state->disabled = 0;
418
419 ast_channel_unlock(chan);
420
421 return 0;
422}
423
424static int hook_write(struct ast_channel *chan, const char *cmd, char *data,
425 const char *value)
426{
427 int res;
428
429 if (!chan) {
430 return -1;
431 }
432
433 if (ast_false(value)) {
434 res = hook_off(chan, data);
435 } else if (ast_true(value)) {
436 res = hook_re_enable(chan, data);
437 } else {
438 ast_log(LOG_WARNING, "Invalid value for PERIODIC_HOOK function: '%s'\n", value);
439 res = -1;
440 }
441
442 return res;
443}
444
446 .name = "PERIODIC_HOOK",
447 .read = hook_read,
448 .write = hook_write,
449};
450
451static int unload_module(void)
452{
454
456 return 0;
457}
458
459static int load_module(void)
460{
461 int res;
462
464 ast_log(LOG_ERROR, "Failed to create %s dialplan context.\n", context_name);
466 }
467
468 /*
469 * Based on a handy recipe from the Asterisk Cookbook.
470 */
471 res = ast_add_extension(context_name, 1, exten_name, 1, "", "",
472 "Set", "EncodedChannel=${HOOK_CHANNEL}",
474 res |= ast_add_extension(context_name, 1, exten_name, 2, "", "",
475 "Set", "GROUP_NAME=${EncodedChannel}${HOOK_ID}",
477 res |= ast_add_extension(context_name, 1, exten_name, 3, "", "",
478 "Set", "GROUP(periodic-hook)=${GROUP_NAME}",
480 res |= ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf",
481 "$[${GROUP_COUNT(${GROUP_NAME}@periodic-hook)} > 1]?Hangup()",
483 res |= ast_add_extension(context_name, 1, exten_name, 5, "", "",
484 "Set", "ChannelToSpy=${URIDECODE(${EncodedChannel})}",
486 res |= ast_add_extension(context_name, 1, exten_name, 6, "", "",
487 "ChanSpy", "${ChannelToSpy},qEB", NULL, AST_MODULE);
488
489 res |= ast_add_extension(context_name, 1, beep_exten, 1, "", "",
490 "Answer", "", NULL, AST_MODULE);
491 res |= ast_add_extension(context_name, 1, beep_exten, 2, "", "",
492 "Playback", "beep", NULL, AST_MODULE);
493 res |= ast_add_extension(context_name, 1, beep_exten, 3, "", "",
494 "Hangup", "", NULL, AST_MODULE);
495
497
498 if (res) {
501 }
503}
504
506 unsigned int interval, char *beep_id, size_t len)
507{
509
510 snprintf(args, sizeof(args), "%s,%s,%u",
511 context_name, beep_exten, interval);
512
513 if (hook_read(chan, NULL, args, beep_id, len)) {
514 ast_log(LOG_WARNING, "Failed to enable periodic beep.\n");
515 return -1;
516 }
517
518 return 0;
519}
520
521int AST_OPTIONAL_API_NAME(ast_beep_stop)(struct ast_channel *chan, const char *beep_id)
522{
523 return hook_write(chan, NULL, (char *) beep_id, "off");
524}
525
527 .support_level = AST_MODULE_SUPPORT_CORE,
528 .load = load_module,
529 .unload = unload_module,
530 .requires = "app_chanspy,func_cut,func_groupcount,func_uri",
#define AST_MODULE
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
Audiohooks Architecture.
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
Definition: audiohook.h:75
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
Definition: audiohook.c:100
ast_audiohook_direction
Definition: audiohook.h:48
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:313
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:578
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:512
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:124
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
Definition: audiohook.h:318
@ AST_AUDIOHOOK_TYPE_MANIPULATE
Definition: audiohook.h:38
@ AST_AUDIOHOOK_STATUS_DONE
Definition: audiohook.h:45
Periodic beeps into the audio of a call.
enum cc_state state
Definition: ccss.c:399
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2414
#define ast_channel_lock(chan)
Definition: channel.h:2970
#define AST_MAX_CONTEXT
Definition: channel.h:135
#define ast_channel_unlock(chan)
Definition: channel.h:2971
#define AST_MAX_EXTENSION
Definition: channel.h:134
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2428
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static const char name[]
Definition: format_mp3.c:68
direction
static void * hook_launch_thread(void *data)
static void hook_datastore_destroy_callback(void *data)
static int hook_re_enable(struct ast_channel *chan, const char *uid)
static void hook_thread_arg_destroy(struct hook_thread_arg *arg)
static const char exten_name[]
static int hook_off(struct ast_channel *chan, const char *hook_id)
static struct hook_state * hook_state_alloc(const char *context, const char *exten, unsigned int interval, unsigned int hook_id)
static unsigned int global_hook_id
Last used hook ID.
static int hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
static int hook_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int hook_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static struct ast_custom_function hook_function
int AST_OPTIONAL_API_NAME() ast_beep_start(struct ast_channel *chan, unsigned int interval, char *beep_id, size_t len)
static const char full_exten_name[]
static int do_hook(struct ast_channel *chan, struct hook_state *state)
static const char context_name[]
static int load_module(void)
static int hook_on(struct ast_channel *chan, const char *data, unsigned int hook_id)
static struct hook_thread_arg * hook_thread_arg_alloc(struct ast_channel *chan, struct hook_state *state)
static int unload_module(void)
int AST_OPTIONAL_API_NAME() ast_beep_stop(struct ast_channel *chan, const char *beep_id)
static const struct ast_datastore_info hook_datastore
static const char beep_exten[]
static int init_hook(struct ast_channel *chan, const char *context, const char *exten, unsigned int interval, unsigned int hook_id)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:761
Asterisk module definitions.
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:330
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define AST_OPTIONAL_API_NAME(name)
Expands to the name of the implementation function.
Definition: optional_api.h:228
Core PBX routines and definitions.
int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid, const char *application, void *data, void(*datad)(void *), const char *registrar)
Add and extension to an extension context.
Definition: pbx.c:6943
@ AST_OUTGOING_NO_WAIT
Definition: pbx.h:1141
struct ast_context * ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
Register a new context or find an existing one.
Definition: pbx.c:6164
int ast_pbx_outgoing_exten(const char *type, struct ast_format_cap *cap, const char *addr, int timeout, const char *context, const char *exten, int priority, int *reason, int synchronous, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel, int early_media, const struct ast_assigned_ids *assignedids)
Synchronously or asynchronously make an outbound call and send it to a particular extension.
Definition: pbx.c:7931
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1568
void ast_context_destroy(struct ast_context *con, const char *registrar)
Destroy a context (matches the specified context or ANY context if NULL)
Definition: pbx.c:8236
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
@ AST_CFE_BOTH
Definition: pbx.h:1553
#define NULL
Definition: resample.c:96
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
Definition: utils.c:2216
enum ast_audiohook_status status
Definition: audiohook.h:108
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Structure for a data store type.
Definition: datastore.h:31
const char * type
Definition: datastore.h:32
Structure for a data store object.
Definition: datastore.h:64
const char * uid
Definition: datastore.h:65
void * data
Definition: datastore.h:66
Data structure associated with a single frame of data.
Structure for variables, used for configurations and for channel variables.
unsigned int hook_id
unsigned char disabled
unsigned int interval
struct timeval last_hook
struct ast_audiohook audiohook
audiohook used as a callback into this module
int value
Definition: syslog.c:37
Test Framework API.
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:189
const char * args
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
#define ast_pthread_create_detached_background(a, b, c, d)
Definition: utils.h:597