Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
func_periodic_hook.c File Reference

Periodic dialplan hooks. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/audiohook.h"
#include "asterisk/beep.h"
Include dependency graph for func_periodic_hook.c:

Go to the source code of this file.

Data Structures

struct  hook_state
 
struct  hook_thread_arg
 

Macros

#define AST_API_MODULE
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
int AST_OPTIONAL_API_NAME() ast_beep_start (struct ast_channel *chan, unsigned int interval, char *beep_id, size_t len)
 
int AST_OPTIONAL_API_NAME() ast_beep_stop (struct ast_channel *chan, const char *beep_id)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int do_hook (struct ast_channel *chan, struct hook_state *state)
 
static int hook_callback (struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
 
static void hook_datastore_destroy_callback (void *data)
 
static void * hook_launch_thread (void *data)
 
static int hook_off (struct ast_channel *chan, const char *hook_id)
 
static int hook_on (struct ast_channel *chan, const char *data, unsigned int hook_id)
 
static int hook_re_enable (struct ast_channel *chan, const char *uid)
 
static int hook_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static struct hook_statehook_state_alloc (const char *context, const char *exten, unsigned int interval, unsigned int hook_id)
 
static struct hook_thread_arghook_thread_arg_alloc (struct ast_channel *chan, struct hook_state *state)
 
static void hook_thread_arg_destroy (struct hook_thread_arg *arg)
 
static int hook_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int init_hook (struct ast_channel *chan, const char *context, const char *exten, unsigned int interval, unsigned int hook_id)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Periodic dialplan hooks." , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "app_chanspy,func_cut,func_groupcount,func_uri", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const char beep_exten [] = "beep"
 
static const char context_name [] = "__func_periodic_hook_context__"
 
static const char exten_name [] = "hook"
 
static const char full_exten_name [] = "hook@__func_periodic_hook_context__"
 
static unsigned int global_hook_id
 Last used hook ID. More...
 
static const struct ast_datastore_info hook_datastore
 
static struct ast_custom_function hook_function
 

Detailed Description

Periodic dialplan hooks.

Author
Russell Bryant russe.nosp@m.ll@r.nosp@m.ussel.nosp@m.lbry.nosp@m.ant.n.nosp@m.et

Definition in file func_periodic_hook.c.

Macro Definition Documentation

◆ AST_API_MODULE

#define AST_API_MODULE

Definition at line 43 of file func_periodic_hook.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 523 of file func_periodic_hook.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 523 of file func_periodic_hook.c.

◆ ast_beep_start()

int AST_OPTIONAL_API_NAME() ast_beep_start ( struct ast_channel chan,
unsigned int  interval,
char *  beep_id,
size_t  len 
)

Definition at line 497 of file func_periodic_hook.c.

References args, ast_log, AST_MAX_CONTEXT, AST_MAX_EXTENSION, beep_exten, context_name, hook_read(), hook_state::interval, len(), LOG_WARNING, and NULL.

Referenced by mixmonitor_exec(), and start_monitor_exec().

499 {
501 
502  snprintf(args, sizeof(args), "%s,%s,%u",
503  context_name, beep_exten, interval);
504 
505  if (hook_read(chan, NULL, args, beep_id, len)) {
506  ast_log(LOG_WARNING, "Failed to enable periodic beep.\n");
507  return -1;
508  }
509 
510  return 0;
511 }
#define LOG_WARNING
Definition: logger.h:274
static int hook_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
const char * args
#define NULL
Definition: resample.c:96
static const char context_name[]
#define ast_log
Definition: astobj2.c:42
#define AST_MAX_EXTENSION
Definition: channel.h:135
static const char beep_exten[]
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define AST_MAX_CONTEXT
Definition: channel.h:136

◆ ast_beep_stop()

int AST_OPTIONAL_API_NAME() ast_beep_stop ( struct ast_channel chan,
const char *  beep_id 
)

Definition at line 513 of file func_periodic_hook.c.

References AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, hook_write(), load_module(), NULL, and unload_module().

Referenced by __stub__ast_beep_start(), ast_monitor_stop(), and stop_mixmonitor_full().

514 {
515  return hook_write(chan, NULL, (char *) beep_id, "off");
516 }
#define NULL
Definition: resample.c:96
static int hook_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 523 of file func_periodic_hook.c.

◆ do_hook()

static int do_hook ( struct ast_channel chan,
struct hook_state state 
)
static

Definition at line 225 of file func_periodic_hook.c.

References ast_pthread_create_detached_background, hook_launch_thread(), hook_thread_arg_alloc(), hook_thread_arg_destroy(), and NULL.

Referenced by hook_callback().

226 {
227  pthread_t t;
228  struct hook_thread_arg *arg;
229  int res;
230 
231  if (!(arg = hook_thread_arg_alloc(chan, state))) {
232  return -1;
233  }
234 
235  /*
236  * We don't want to block normal frame processing *at all* while we kick
237  * this off, so do it in a new thread.
238  */
240  if (res != 0) {
242  }
243 
244  return res;
245 }
static void hook_thread_arg_destroy(struct hook_thread_arg *arg)
#define NULL
Definition: resample.c:96
#define ast_pthread_create_detached_background(a, b, c, d)
Definition: utils.h:572
static struct hook_thread_arg * hook_thread_arg_alloc(struct ast_channel *chan, struct hook_state *state)
static void * hook_launch_thread(void *data)

◆ hook_callback()

static int hook_callback ( struct ast_audiohook audiohook,
struct ast_channel chan,
struct ast_frame frame,
enum ast_audiohook_direction  direction 
)
static

Definition at line 247 of file func_periodic_hook.c.

References AST_AUDIOHOOK_STATUS_DONE, ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_log, ast_strdupa, ast_tvdiff_ms(), ast_tvnow(), hook_state::disabled, do_hook(), hook_state::interval, hook_state::last_hook, LOG_WARNING, name, and ast_audiohook::status.

Referenced by hook_state_alloc().

249 {
250  struct hook_state *state = (struct hook_state *) audiohook; /* trust me. */
251  struct timeval now;
252  int res = 0;
253 
254  if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || state->disabled) {
255  return 0;
256  }
257 
258  now = ast_tvnow();
259  if (ast_tvdiff_ms(now, state->last_hook) > state->interval * 1000) {
260  if ((res = do_hook(chan, state))) {
261  const char *name;
262  ast_channel_lock(chan);
263  name = ast_strdupa(ast_channel_name(chan));
264  ast_channel_unlock(chan);
265  ast_log(LOG_WARNING, "Failed to run hook on '%s'\n", name);
266  }
267  state->last_hook = now;
268  }
269 
270  return res;
271 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
#define LOG_WARNING
Definition: logger.h:274
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
static int do_hook(struct ast_channel *chan, struct hook_state *state)
unsigned int interval
unsigned char disabled
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
struct timeval last_hook
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static const char name[]
Definition: cdr_mysql.c:74
const char * ast_channel_name(const struct ast_channel *chan)
enum ast_audiohook_status status
Definition: audiohook.h:107

◆ hook_datastore_destroy_callback()

static void hook_datastore_destroy_callback ( void *  data)
static

Definition at line 128 of file func_periodic_hook.c.

References ast_audiohook_destroy(), ast_audiohook_detach(), ast_audiohook_lock, ast_audiohook_unlock, ast_free, hook_state::audiohook, hook_state::context, and hook_state::exten.

129 {
130  struct hook_state *state = data;
131 
132  ast_audiohook_lock(&state->audiohook);
136 
137  ast_free(state->context);
138  ast_free(state->exten);
139  ast_free(state);
140 }
struct ast_audiohook audiohook
audiohook used as a callback into this module
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:133
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
Definition: audiohook.h:300
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:579
#define ast_free(a)
Definition: astmm.h:182
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:295

◆ hook_launch_thread()

static void* hook_launch_thread ( void *  data)
static

Definition at line 168 of file func_periodic_hook.c.

References AST_OUTGOING_NO_WAIT, ast_pbx_outgoing_exten(), hook_thread_arg::chan_name, hook_thread_arg::context, hook_thread_arg::exten, full_exten_name, hook_state::hook_id, hook_thread_arg::hook_id, hook_thread_arg_destroy(), ast_variable::name, and NULL.

Referenced by do_hook().

169 {
170  struct hook_thread_arg *arg = data;
171  struct ast_variable hook_id = {
172  .name = "HOOK_ID",
173  .value = arg->hook_id,
174  };
175  struct ast_variable chan_name_var = {
176  .name = "HOOK_CHANNEL",
177  .value = arg->chan_name,
178  .next = &hook_id,
179  };
180 
182  arg->context, arg->exten, 1, NULL, AST_OUTGOING_NO_WAIT,
183  NULL, NULL, &chan_name_var, NULL, NULL, 1, NULL);
184 
186 
187  return NULL;
188 }
Structure for variables, used for configurations and for channel variables.
static void hook_thread_arg_destroy(struct hook_thread_arg *arg)
#define NULL
Definition: resample.c:96
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:7951
static const char full_exten_name[]

◆ hook_off()

static int hook_off ( struct ast_channel chan,
const char *  hook_id 
)
static

Definition at line 350 of file func_periodic_hook.c.

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_log, ast_strlen_zero, ast_datastore::data, hook_state::disabled, LOG_WARNING, and state.

Referenced by hook_write().

351 {
352  struct ast_datastore *datastore;
353  struct hook_state *state;
354 
355  if (ast_strlen_zero(hook_id)) {
356  return -1;
357  }
358 
359  ast_channel_lock(chan);
360 
361  if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, hook_id))) {
362  ast_log(LOG_WARNING, "Hook with ID '%s' not found on channel '%s'\n", hook_id,
363  ast_channel_name(chan));
364  ast_channel_unlock(chan);
365  return -1;
366  }
367 
368  state = datastore->data;
369  state->disabled = 1;
370 
371  ast_channel_unlock(chan);
372 
373  return 0;
374 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define ast_channel_lock(chan)
Definition: channel.h:2945
unsigned int hook_id
#define LOG_WARNING
Definition: logger.h:274
Structure for a data store object.
Definition: datastore.h:68
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:2404
#define ast_strlen_zero(foo)
Definition: strings.h:52
unsigned char disabled
#define ast_log
Definition: astobj2.c:42
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void * data
Definition: datastore.h:70
const char * ast_channel_name(const struct ast_channel *chan)
static const struct ast_datastore_info hook_datastore

◆ hook_on()

static int hook_on ( struct ast_channel chan,
const char *  data,
unsigned int  hook_id 
)
static

Definition at line 321 of file func_periodic_hook.c.

References args, AST_APP_ARG, ast_channel_name(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero, hook_state::context, hook_state::exten, init_hook(), hook_state::interval, LOG_WARNING, parse(), and S_OR.

Referenced by hook_read().

322 {
323  char *parse = ast_strdupa(S_OR(data, ""));
328  );
329  unsigned int interval;
330 
331  AST_STANDARD_APP_ARGS(args, parse);
332 
333  if (ast_strlen_zero(args.interval) ||
334  sscanf(args.interval, "%30u", &interval) != 1 || interval == 0) {
335  ast_log(LOG_WARNING, "Invalid hook interval: '%s'\n", S_OR(args.interval, ""));
336  return -1;
337  }
338 
339  if (ast_strlen_zero(args.context) || ast_strlen_zero(args.exten)) {
340  ast_log(LOG_WARNING, "A context and extension are required for PERIODIC_HOOK().\n");
341  return -1;
342  }
343 
344  ast_debug(1, "hook to %s@%s enabled on %s with interval of %u seconds\n",
345  args.exten, args.context, ast_channel_name(chan), interval);
346 
347  return init_hook(chan, args.context, args.exten, interval, hook_id);
348 }
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
unsigned int hook_id
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
const char * args
#define ast_strlen_zero(foo)
Definition: strings.h:52
unsigned int interval
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
const char * ast_channel_name(const struct ast_channel *chan)
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
static int init_hook(struct ast_channel *chan, const char *context, const char *exten, unsigned int interval, unsigned int hook_id)
#define AST_APP_ARG(name)
Define an application argument.

◆ hook_re_enable()

static int hook_re_enable ( struct ast_channel chan,
const char *  uid 
)
static

Definition at line 392 of file func_periodic_hook.c.

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_log, ast_strlen_zero, ast_datastore::data, hook_state::disabled, LOG_WARNING, and state.

Referenced by hook_write().

393 {
394  struct ast_datastore *datastore;
395  struct hook_state *state;
396 
397  if (ast_strlen_zero(uid)) {
398  return -1;
399  }
400 
401  ast_channel_lock(chan);
402 
403  if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, uid))) {
404  ast_log(LOG_WARNING, "Hook with ID '%s' not found on '%s'\n",
405  uid, ast_channel_name(chan));
406  ast_channel_unlock(chan);
407  return -1;
408  }
409 
410  state = datastore->data;
411  state->disabled = 0;
412 
413  ast_channel_unlock(chan);
414 
415  return 0;
416 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define ast_channel_lock(chan)
Definition: channel.h:2945
#define LOG_WARNING
Definition: logger.h:274
Structure for a data store object.
Definition: datastore.h:68
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:2404
#define ast_strlen_zero(foo)
Definition: strings.h:52
unsigned char disabled
#define ast_log
Definition: astobj2.c:42
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void * data
Definition: datastore.h:70
const char * ast_channel_name(const struct ast_channel *chan)
static const struct ast_datastore_info hook_datastore

◆ hook_read()

static int hook_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 376 of file func_periodic_hook.c.

References ast_atomic_fetchadd_int(), global_hook_id, hook_state::hook_id, and hook_on().

Referenced by ast_beep_start().

378 {
379  unsigned int hook_id;
380 
381  if (!chan) {
382  return -1;
383  }
384 
385  hook_id = (unsigned int) ast_atomic_fetchadd_int((int *) &global_hook_id, 1);
386 
387  snprintf(buf, len, "%u", hook_id);
388 
389  return hook_on(chan, data, hook_id);
390 }
unsigned int hook_id
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
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:755
static unsigned int global_hook_id
Last used hook ID.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int hook_on(struct ast_channel *chan, const char *data, unsigned int hook_id)

◆ hook_state_alloc()

static struct hook_state* hook_state_alloc ( const char *  context,
const char *  exten,
unsigned int  interval,
unsigned int  hook_id 
)
static

Definition at line 273 of file func_periodic_hook.c.

References ast_audiohook_init(), AST_AUDIOHOOK_MANIPULATE_ALL_RATES, AST_AUDIOHOOK_TYPE_MANIPULATE, ast_calloc, AST_MODULE, ast_strdup, hook_state::audiohook, hook_state::context, hook_state::exten, hook_callback(), hook_state::hook_id, hook_state::interval, ast_audiohook::manipulate_callback, NULL, and state.

Referenced by init_hook().

275 {
276  struct hook_state *state;
277 
278  if (!(state = ast_calloc(1, sizeof(*state)))) {
279  return NULL;
280  }
281 
282  state->context = ast_strdup(context);
283  state->exten = ast_strdup(exten);
284  state->interval = interval;
285  state->hook_id = hook_id;
286 
290 
291  return state;
292 }
struct ast_audiohook audiohook
audiohook used as a callback into this module
enum sip_cc_notify_state state
Definition: chan_sip.c:959
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
unsigned int hook_id
static int hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:117
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:108
unsigned int interval
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define AST_MODULE
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ hook_thread_arg_alloc()

static struct hook_thread_arg* hook_thread_arg_alloc ( struct ast_channel chan,
struct hook_state state 
)
static

Definition at line 190 of file func_periodic_hook.c.

References ast_asprintf, ast_calloc, ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_strdup, hook_thread_arg::chan_name, hook_state::context, hook_thread_arg::context, hook_state::exten, hook_thread_arg::exten, hook_state::hook_id, hook_thread_arg::hook_id, hook_thread_arg_destroy(), and NULL.

Referenced by do_hook().

192 {
193  struct hook_thread_arg *arg;
194 
195  if (!(arg = ast_calloc(1, sizeof(*arg)))) {
196  return NULL;
197  }
198 
199  ast_channel_lock(chan);
200  arg->chan_name = ast_strdup(ast_channel_name(chan));
201  ast_channel_unlock(chan);
202  if (!arg->chan_name) {
204  return NULL;
205  }
206 
207  if (ast_asprintf(&arg->hook_id, "%u", state->hook_id) == -1) {
209  return NULL;
210  }
211 
212  if (!(arg->context = ast_strdup(state->context))) {
214  return NULL;
215  }
216 
217  if (!(arg->exten = ast_strdup(state->exten))) {
219  return NULL;
220  }
221 
222  return arg;
223 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
unsigned int hook_id
static void hook_thread_arg_destroy(struct hook_thread_arg *arg)
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
const char * ast_channel_name(const struct ast_channel *chan)

◆ hook_thread_arg_destroy()

static void hook_thread_arg_destroy ( struct hook_thread_arg arg)
static

Definition at line 159 of file func_periodic_hook.c.

References ast_free, hook_thread_arg::chan_name, hook_thread_arg::context, hook_thread_arg::exten, and hook_thread_arg::hook_id.

Referenced by do_hook(), hook_launch_thread(), and hook_thread_arg_alloc().

160 {
161  ast_free(arg->hook_id);
162  ast_free(arg->chan_name);
163  ast_free(arg->context);
164  ast_free(arg->exten);
165  ast_free(arg);
166 }
#define ast_free(a)
Definition: astmm.h:182

◆ hook_write()

static int hook_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 418 of file func_periodic_hook.c.

References ast_false(), ast_log, ast_true(), hook_off(), hook_re_enable(), and LOG_WARNING.

Referenced by ast_beep_stop().

420 {
421  int res;
422 
423  if (!chan) {
424  return -1;
425  }
426 
427  if (ast_false(value)) {
428  res = hook_off(chan, data);
429  } else if (ast_true(value)) {
430  res = hook_re_enable(chan, data);
431  } else {
432  ast_log(LOG_WARNING, "Invalid value for PERIODIC_HOOK function: '%s'\n", value);
433  res = -1;
434  }
435 
436  return res;
437 }
static int hook_off(struct ast_channel *chan, const char *hook_id)
#define LOG_WARNING
Definition: logger.h:274
int value
Definition: syslog.c:37
#define ast_log
Definition: astobj2.c:42
static int hook_re_enable(struct ast_channel *chan, const char *uid)
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1951
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"...
Definition: main/utils.c:1968

◆ init_hook()

static int init_hook ( struct ast_channel chan,
const char *  context,
const char *  exten,
unsigned int  interval,
unsigned int  hook_id 
)
static

Definition at line 294 of file func_periodic_hook.c.

References ast_audiohook_attach(), ast_channel_datastore_add(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc, ast_datastore_free(), hook_state::audiohook, ast_datastore::data, hook_state_alloc(), state, and ast_datastore::uid.

Referenced by hook_on().

296 {
297  struct hook_state *state;
298  struct ast_datastore *datastore;
299  char uid[32];
300 
301  snprintf(uid, sizeof(uid), "%u", hook_id);
302 
303  if (!(datastore = ast_datastore_alloc(&hook_datastore, uid))) {
304  return -1;
305  }
306 
307  if (!(state = hook_state_alloc(context, exten, interval, hook_id))) {
308  ast_datastore_free(datastore);
309  return -1;
310  }
311  datastore->data = state;
312 
313  ast_channel_lock(chan);
314  ast_channel_datastore_add(chan, datastore);
315  ast_audiohook_attach(chan, &state->audiohook);
316  ast_channel_unlock(chan);
317 
318  return 0;
319 }
struct ast_audiohook audiohook
audiohook used as a callback into this module
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define ast_channel_lock(chan)
Definition: channel.h:2945
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
static struct hook_state * hook_state_alloc(const char *context, const char *exten, unsigned int interval, unsigned int hook_id)
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:501
Structure for a data store object.
Definition: datastore.h:68
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
const char * uid
Definition: datastore.h:69
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void * data
Definition: datastore.h:70
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
static const struct ast_datastore_info hook_datastore
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390

◆ load_module()

static int load_module ( void  )
static

Definition at line 453 of file func_periodic_hook.c.

References ast_add_extension(), AST_CFE_BOTH, ast_context_find_or_create(), ast_custom_function_register_escalating, ast_log, AST_MODULE, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, beep_exten, context_name, exten_name, LOG_ERROR, NULL, and unload_module().

Referenced by ast_beep_stop().

454 {
455  int res;
456 
458  ast_log(LOG_ERROR, "Failed to create %s dialplan context.\n", context_name);
460  }
461 
462  /*
463  * Based on a handy recipe from the Asterisk Cookbook.
464  */
465  res = ast_add_extension(context_name, 1, exten_name, 1, "", "",
466  "Set", "EncodedChannel=${CUT(HOOK_CHANNEL,-,1-2)}",
467  NULL, AST_MODULE);
468  res |= ast_add_extension(context_name, 1, exten_name, 2, "", "",
469  "Set", "GROUP_NAME=${EncodedChannel}${HOOK_ID}",
470  NULL, AST_MODULE);
471  res |= ast_add_extension(context_name, 1, exten_name, 3, "", "",
472  "Set", "GROUP(periodic-hook)=${GROUP_NAME}",
473  NULL, AST_MODULE);
474  res |= ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf",
475  "$[${GROUP_COUNT(${GROUP_NAME}@periodic-hook)} > 1]?Hangup()",
476  NULL, AST_MODULE);
477  res |= ast_add_extension(context_name, 1, exten_name, 5, "", "",
478  "Set", "ChannelToSpy=${URIDECODE(${EncodedChannel})}",
479  NULL, AST_MODULE);
480  res |= ast_add_extension(context_name, 1, exten_name, 6, "", "",
481  "ChanSpy", "${ChannelToSpy},qEB", NULL, AST_MODULE);
482 
483  res |= ast_add_extension(context_name, 1, beep_exten, 1, "", "",
484  "Answer", "", NULL, AST_MODULE);
485  res |= ast_add_extension(context_name, 1, beep_exten, 2, "", "",
486  "Playback", "beep", NULL, AST_MODULE);
487 
489 
490  if (res) {
491  unload_module();
493  }
495 }
#define NULL
Definition: resample.c:96
static const char context_name[]
#define ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1517
#define ast_log
Definition: astobj2.c:42
static const char beep_exten[]
#define LOG_ERROR
Definition: logger.h:285
static int unload_module(void)
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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:6970
#define AST_MODULE
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:6198
static const char exten_name[]
static struct ast_custom_function hook_function

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 445 of file func_periodic_hook.c.

References ast_context_destroy(), ast_custom_function_unregister(), AST_MODULE, and NULL.

Referenced by ast_beep_stop(), and load_module().

446 {
448 
450  return 0;
451 }
#define NULL
Definition: resample.c:96
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define AST_MODULE
void ast_context_destroy(struct ast_context *con, const char *registrar)
Destroy a context (matches the specified context or ANY context if NULL)
Definition: conf2ael.c:625
static struct ast_custom_function hook_function

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Periodic dialplan hooks." , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "app_chanspy,func_cut,func_groupcount,func_uri", }
static

Definition at line 523 of file func_periodic_hook.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 523 of file func_periodic_hook.c.

◆ beep_exten

const char beep_exten[] = "beep"
static

Definition at line 96 of file func_periodic_hook.c.

Referenced by ast_beep_start(), and load_module().

◆ context_name

const char context_name[] = "__func_periodic_hook_context__"
static

◆ exten_name

const char exten_name[] = "hook"
static

◆ full_exten_name

const char full_exten_name[] = "hook@__func_periodic_hook_context__"
static

Definition at line 94 of file func_periodic_hook.c.

Referenced by hook_launch_thread().

◆ global_hook_id

unsigned int global_hook_id
static

Last used hook ID.

This is incremented each time a hook is created to give each hook a unique ID.

Definition at line 104 of file func_periodic_hook.c.

Referenced by hook_read().

◆ hook_datastore

const struct ast_datastore_info hook_datastore
static
Initial value:
= {
.type = AST_MODULE,
}
static void hook_datastore_destroy_callback(void *data)
#define AST_MODULE

Definition at line 142 of file func_periodic_hook.c.

◆ hook_function

struct ast_custom_function hook_function
static
Initial value:
= {
.name = "PERIODIC_HOOK",
.read = hook_read,
.write = hook_write,
}
static int hook_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int hook_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)

Definition at line 439 of file func_periodic_hook.c.