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

Tone detection module. More...

#include "asterisk.h"
#include <math.h>
#include "asterisk/module.h"
#include "asterisk/frame.h"
#include "asterisk/format_cache.h"
#include "asterisk/channel.h"
#include "asterisk/dsp.h"
#include "asterisk/pbx.h"
#include "asterisk/audiohook.h"
#include "asterisk/app.h"
#include "asterisk/indications.h"
#include "asterisk/conversions.h"
Include dependency graph for app_tonedetect.c:

Go to the source code of this file.

Data Structures

struct  detect_information
 

Enumerations

enum  { OPT_ARG_DECIBEL, OPT_ARG_GOTO_RX, OPT_ARG_GOTO_TX, OPT_ARG_ARRAY_SIZE }
 
enum  { OPT_APP_DECIBEL = (1 << 0), OPT_APP_SQUELCH = (1 << 1) }
 
enum  { OPT_APP_ARG_DECIBEL, OPT_APP_ARG_ARRAY_SIZE }
 
enum  td_opts {
  OPT_TX = (1 << 1), OPT_RX = (1 << 2), OPT_END_FILTER = (1 << 3), OPT_GOTO_RX = (1 << 4),
  OPT_GOTO_TX = (1 << 5), OPT_DECIBEL = (1 << 6), OPT_SQUELCH = (1 << 7)
}
 

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Tone detection module")
 
static void destroy_callback (void *data)
 
static int detect_callback (struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
 
static int detect_read (struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen)
 
static int detect_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int freq_parser (char *freqs, int *freq1, int *freq2)
 
static char * goto_parser (struct ast_channel *chan, char *loc)
 
static int load_module (void)
 
static int remove_detect (struct ast_channel *chan)
 
static int unload_module (void)
 
static int wait_exec (struct ast_channel *chan, const char *data)
 

Variables

static const struct ast_datastore_info detect_datastore
 
static struct ast_custom_function detect_function
 
static const struct ast_app_option td_opts [128] = { [ 'd' ] = { .flag = OPT_DECIBEL , .arg_index = OPT_ARG_DECIBEL + 1 }, [ 'g' ] = { .flag = OPT_GOTO_RX , .arg_index = OPT_ARG_GOTO_RX + 1 }, [ 'h' ] = { .flag = OPT_GOTO_TX , .arg_index = OPT_ARG_GOTO_TX + 1 }, [ 's' ] = { .flag = OPT_SQUELCH }, [ 't' ] = { .flag = OPT_TX }, [ 'r' ] = { .flag = OPT_RX }, [ 'x' ] = { .flag = OPT_END_FILTER }, }
 
static const struct ast_app_option wait_exec_options [128] = { [ 'd' ] = { .flag = OPT_APP_DECIBEL , .arg_index = OPT_APP_ARG_DECIBEL + 1 }, [ 's' ] = { .flag = OPT_APP_SQUELCH }, }
 
static char * waitapp = "WaitForTone"
 

Detailed Description

Tone detection module.

Author
Naveen Albert aster.nosp@m.isk@.nosp@m.phrea.nosp@m.knet.nosp@m..org

Definition in file app_tonedetect.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
OPT_ARG_DECIBEL 
OPT_ARG_GOTO_RX 
OPT_ARG_GOTO_TX 
OPT_ARG_ARRAY_SIZE 

Definition at line 183 of file app_tonedetect.c.

183  {
187  /* note: this entry _MUST_ be the last one in the enum */
189 };

◆ anonymous enum

anonymous enum
Enumerator
OPT_APP_DECIBEL 
OPT_APP_SQUELCH 

Definition at line 508 of file app_tonedetect.c.

508  {
509  OPT_APP_DECIBEL = (1 << 0),
510  OPT_APP_SQUELCH = (1 << 1),
511 };

◆ anonymous enum

anonymous enum
Enumerator
OPT_APP_ARG_DECIBEL 
OPT_APP_ARG_ARRAY_SIZE 

Definition at line 513 of file app_tonedetect.c.

513  {
515  /* note: this entry _MUST_ be the last one in the enum */
517 };

◆ td_opts

enum td_opts
Enumerator
OPT_TX 
OPT_RX 
OPT_END_FILTER 
OPT_GOTO_RX 
OPT_GOTO_TX 
OPT_DECIBEL 
OPT_SQUELCH 

Definition at line 173 of file app_tonedetect.c.

173  {
174  OPT_TX = (1 << 1),
175  OPT_RX = (1 << 2),
176  OPT_END_FILTER = (1 << 3),
177  OPT_GOTO_RX = (1 << 4),
178  OPT_GOTO_TX = (1 << 5),
179  OPT_DECIBEL = (1 << 6),
180  OPT_SQUELCH = (1 << 7),
181 };

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Tone detection module"   
)

Referenced by load_module().

◆ destroy_callback()

static void destroy_callback ( void *  data)
static

Definition at line 201 of file app_tonedetect.c.

References ast_audiohook_destroy(), ast_audiohook_detach(), ast_audiohook_lock, ast_audiohook_unlock, ast_dsp_free(), ast_free, detect_information::audiohook, di, detect_information::dsp, detect_information::gotorx, and detect_information::gototx.

202 {
203  struct detect_information *di = data;
204  ast_dsp_free(di->dsp);
205  if (di->gotorx) {
206  ast_free(di->gotorx);
207  }
208  if (di->gototx) {
209  ast_free(di->gototx);
210  }
215  ast_free(di);
216  return;
217 }
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1770
struct ast_audiohook audiohook
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:133
struct ast_dsp * dsp
#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
static float di[4]
Definition: tdd.c:58
#define ast_free(a)
Definition: astmm.h:182
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:295

◆ detect_callback()

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

Definition at line 224 of file app_tonedetect.c.

References ast_async_parseable_goto(), AST_AUDIOHOOK_DIRECTION_READ, AST_AUDIOHOOK_STATUS_DONE, ast_channel_datastore_find(), ast_debug, ast_dsp_process(), AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frfree, ast_frisolate, ast_datastore::data, di, detect_information::dsp, ast_frame::frametype, detect_information::gotorx, detect_information::gototx, ast_frame_subclass::integer, NULL, result, detect_information::rx, detect_information::rxcount, ast_audiohook::status, ast_frame::subclass, detect_information::tx, and detect_information::txcount.

Referenced by detect_write().

225 {
226  struct ast_datastore *datastore = NULL;
227  struct detect_information *di = NULL;
228 
229  /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
230  if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
231  return 0;
232 
233  /* Grab datastore which contains our gain information */
234  if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL)))
235  return 0;
236 
237  di = datastore->data;
238 
239  if (!frame || frame->frametype != AST_FRAME_VOICE)
240  return 0;
241 
242  if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? &di->rx : &di->tx))
243  return 0;
244 
245  /* we don't want to modify the original frame */
246  if (!(frame = ast_frisolate(frame))) {
247  return 0;
248  }
249  frame = ast_dsp_process(chan, di->dsp, frame);
250  if (frame->frametype == AST_FRAME_DTMF) {
251  char result = frame->subclass.integer;
252  if (result == 'q') {
253  ast_debug(1, "TONE_DETECT just got a hit\n");
255  di->rxcount = di->rxcount + 1;
256  else
257  di->txcount = di->txcount + 1;
259  ast_async_parseable_goto(chan, di->gotorx);
260  } else if (di->gototx) {
261  ast_async_parseable_goto(chan, di->gototx);
262  }
263  }
264  }
265  ast_frfree(frame);
266 
267  return 0;
268 }
unsigned short int rx
struct ast_frame * ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *inf)
Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress...
Definition: dsp.c:1494
Structure for a data store object.
Definition: datastore.h:68
static const struct ast_datastore_info detect_datastore
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 NULL
Definition: resample.c:96
#define AST_FRAME_DTMF
struct ast_frame_subclass subclass
struct ast_dsp * dsp
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static float di[4]
Definition: tdd.c:58
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
int ast_async_parseable_goto(struct ast_channel *chan, const char *goto_string)
Definition: pbx.c:8864
void * data
Definition: datastore.h:70
#define ast_frfree(fr)
static PGresult * result
Definition: cel_pgsql.c:88
enum ast_audiohook_status status
Definition: audiohook.h:107
enum ast_frame_type frametype
direction
unsigned short int tx

◆ detect_read()

static int detect_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buffer,
size_t  buflen 
)
static

Definition at line 369 of file app_tonedetect.c.

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_log, ast_datastore::data, di, LOG_WARNING, NULL, detect_information::rxcount, and detect_information::txcount.

370 {
371  struct ast_datastore *datastore = NULL;
372  struct detect_information *di = NULL;
373 
374  if (!chan) {
375  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
376  return -1;
377  }
378 
379  ast_channel_lock(chan);
380  if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
381  ast_channel_unlock(chan);
382  return -1; /* function not initiated yet, so nothing to read */
383  } else {
384  ast_channel_unlock(chan);
385  di = datastore->data;
386  }
387 
388  if (strchr(data, 't')) {
389  snprintf(buffer, buflen, "%d", di->txcount);
390  } else if (strchr(data, 'r')) {
391  snprintf(buffer, buflen, "%d", di->rxcount);
392  } else {
393  ast_log(LOG_WARNING, "Invalid direction: %s\n", data);
394  }
395 
396  return 0;
397 }
#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
static const struct ast_datastore_info detect_datastore
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 NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
static float di[4]
Definition: tdd.c:58
#define ast_channel_unlock(chan)
Definition: channel.h:2946
void * data
Definition: datastore.h:70

◆ detect_write()

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

Definition at line 399 of file app_tonedetect.c.

References args, AST_APP_ARG, ast_app_parse_options(), ast_audiohook_attach(), ast_audiohook_init(), AST_AUDIOHOOK_MANIPULATE_ALL_RATES, AST_AUDIOHOOK_TYPE_MANIPULATE, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc, ast_datastore_free(), ast_debug, AST_DECLARE_APP_ARGS, ast_dsp_new(), ast_dsp_set_features(), ast_dsp_set_freqmode(), ast_log, AST_STANDARD_APP_ARGS, ast_str_to_int(), ast_strdupa, ast_strlen_zero, ast_test_flag, detect_information::audiohook, ast_datastore::data, detect_information::db, detect_callback(), di, detect_information::dsp, DSP_FEATURE_FREQ_DETECT, detect_information::duration, detect_information::freq1, detect_information::freq2, freq_parser(), ast_dsp::freqs, goto_parser(), detect_information::gotorx, detect_information::gototx, LOG_WARNING, ast_audiohook::manipulate_callback, NULL, OPT_ARG_ARRAY_SIZE, OPT_ARG_DECIBEL, OPT_ARG_GOTO_RX, OPT_ARG_GOTO_TX, OPT_DECIBEL, OPT_END_FILTER, OPT_GOTO_RX, OPT_GOTO_TX, OPT_RX, OPT_SQUELCH, OPT_TX, options, parse(), remove_detect(), detect_information::rx, detect_information::rxcount, detect_information::squelch, detect_information::tx, and detect_information::txcount.

400 {
401  char *parse;
402  struct ast_datastore *datastore = NULL;
403  struct detect_information *di = NULL;
404  int is_new = 0;
405  struct ast_flags flags = { 0 };
406  char *opt_args[OPT_ARG_ARRAY_SIZE];
407  struct ast_dsp *dsp;
408  int freq1 = 0, freq2 = 0, duration = 500, db = 16, squelch = 0;
409 
412  AST_APP_ARG(duration);
414  );
415 
416  if (!chan) {
417  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
418  return -1;
419  }
420  parse = ast_strdupa(data);
421  AST_STANDARD_APP_ARGS(args, parse);
422 
423  ast_channel_lock(chan);
424  if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
425  ast_channel_unlock(chan);
426  if (!(datastore = ast_datastore_alloc(&detect_datastore, NULL)))
427  return 0;
428  if (!(di = ast_calloc(1, sizeof(*di)))) {
429  ast_datastore_free(datastore);
430  return 0;
431  }
434  is_new = 1;
435  } else {
436  ast_channel_unlock(chan);
437  di = datastore->data;
438  }
439 
440  if (!ast_strlen_zero(args.options)) {
441  ast_app_parse_options(td_opts, &flags, opt_args, args.options);
442  }
443 
444  if (ast_test_flag(&flags, OPT_END_FILTER)) {
445  return remove_detect(chan);
446  }
447  if (freq_parser(args.freqs, &freq1, &freq2)) {
448  return -1;
449  }
450  if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) {
451  ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration);
452  return -1;
453  }
454  di->duration = duration;
455  if (ast_test_flag(&flags, OPT_DECIBEL) && !ast_strlen_zero(opt_args[OPT_ARG_DECIBEL])) {
456  if ((ast_str_to_int(opt_args[OPT_ARG_DECIBEL], &db) || db < 1)) {
457  ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_ARG_DECIBEL]);
458  return -1;
459  }
460  }
461  di->gotorx = NULL;
462  di->gototx = NULL;
463  /* resolve gotos now, in case a full context,exten,pri wasn't specified */
464  if (ast_test_flag(&flags, OPT_GOTO_RX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_RX])) {
465  di->gotorx = goto_parser(chan, opt_args[OPT_ARG_GOTO_RX]);
466  }
467  if (ast_test_flag(&flags, OPT_GOTO_TX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_TX])) {
468  di->gototx = goto_parser(chan, opt_args[OPT_ARG_GOTO_TX]);
469  }
470  di->db = db;
471  di->squelch = ast_test_flag(&flags, OPT_SQUELCH);
472 
473  di->tx = 1;
474  di->rx = 1;
475  if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_TX)) {
476  di->tx = 1;
477  di->rx = 0;
478  }
479  if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_RX)) {
480  di->rx = 1;
481  di->tx = 0;
482  }
483 
484  if (is_new) {
485  if (!(dsp = ast_dsp_new())) {
486  ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
487  return -1;
488  }
490  ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
491  di->dsp = dsp;
492  di->txcount = 0;
493  di->rxcount = 0;
494  ast_debug(1, "Keeping our ears open for %s Hz, %d db\n", args.freqs, db);
495  datastore->data = di;
496  ast_channel_lock(chan);
497  ast_channel_datastore_add(chan, datastore);
498  ast_channel_unlock(chan);
499  ast_audiohook_attach(chan, &di->audiohook);
500  } else {
501  dsp = di->dsp;
502  ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
503  }
504 
505  return 0;
506 }
unsigned short int rx
#define ast_channel_lock(chan)
Definition: channel.h:2945
#define ast_test_flag(p, flag)
Definition: utils.h:63
goertzel_state_t freqs[FREQ_ARRAY_SIZE]
Definition: dsp.c:419
static char * goto_parser(struct ast_channel *chan, char *loc)
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
static sqlite3 * db
#define LOG_WARNING
Definition: logger.h:274
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1745
unsigned int flags
Definition: utils.h:200
struct ast_audiohook audiohook
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:501
static int detect_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
Structure for a data store object.
Definition: datastore.h:68
static const struct ast_datastore_info detect_datastore
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
const char * args
#define NULL
Definition: resample.c:96
static int remove_detect(struct ast_channel *chan)
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ast_dsp * dsp
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
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
unsigned short int squelch
Definition: dsp.c:405
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:2906
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
static float di[4]
Definition: tdd.c:58
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1755
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
Structure used to handle boolean flags.
Definition: utils.h:199
void * data
Definition: datastore.h:70
td_opts
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq1, int dur, int db, int squelch)
Set arbitrary frequency detection mode.
Definition: dsp.c:1859
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
static struct test_options options
#define DSP_FEATURE_FREQ_DETECT
Definition: dsp.h:45
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
static int freq_parser(char *freqs, int *freq1, int *freq2)
unsigned short int tx
#define AST_APP_ARG(name)
Define an application argument.

◆ freq_parser()

static int freq_parser ( char *  freqs,
int *  freq1,
int *  freq2 
)
static

Definition at line 299 of file app_tonedetect.c.

References ast_log, ast_str_to_int(), ast_strdupa, ast_strlen_zero, LOG_ERROR, LOG_WARNING, and strsep().

Referenced by detect_write(), and wait_exec().

299  {
300  char *f1, *f2, *f3;
301  if (ast_strlen_zero(freqs)) {
302  ast_log(LOG_ERROR, "No frequency specified\n");
303  return -1;
304  }
305  f3 = ast_strdupa(freqs);
306  f1 = strsep(&f3, "+");
307  f2 = strsep(&f3, "+");
308  if (!ast_strlen_zero(f3)) {
309  ast_log(LOG_WARNING, "Only up to 2 frequencies may be specified: %s\n", freqs);
310  return -1;
311  }
312  if (ast_str_to_int(f1, freq1)) {
313  ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f1);
314  return -1;
315  }
316  if (*freq1 < 1) {
317  ast_log(LOG_WARNING, "Sorry, positive frequencies only: %d\n", *freq1);
318  return -1;
319  }
320  if (!ast_strlen_zero(f2)) {
321  ast_log(LOG_WARNING, "Sorry, currently only 1 frequency is supported\n");
322  return -1;
323  /* not supported just yet, but possibly will be in the future */
324  if (ast_str_to_int(f2, freq2)) {
325  ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f2);
326  return -1;
327  }
328  if (*freq2 < 1) {
329  ast_log(LOG_WARNING, "Sorry, positive frequencies only: %d\n", *freq2);
330  return -1;
331  }
332  }
333  return 0;
334 }
goertzel_state_t freqs[FREQ_ARRAY_SIZE]
Definition: dsp.c:419
#define LOG_WARNING
Definition: logger.h:274
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
#define LOG_ERROR
Definition: logger.h:285
char * strsep(char **str, const char *delims)

◆ goto_parser()

static char* goto_parser ( struct ast_channel chan,
char *  loc 
)
static

Definition at line 336 of file app_tonedetect.c.

References ast_channel_context(), ast_channel_exten(), ast_channel_lock, ast_channel_unlock, ast_malloc, ast_strdupa, ast_strlen_zero, context, exten, NULL, parse(), and strsep().

Referenced by detect_write().

336  {
337  char *exten, *pri, *context, *parse;
338  char *dest;
339  int size;
340  parse = ast_strdupa(loc);
341  context = strsep(&parse, ",");
342  exten = strsep(&parse, ",");
343  pri = strsep(&parse, ",");
344  if (!exten) {
345  pri = context;
346  exten = NULL;
347  context = NULL;
348  } else if (!pri) {
349  pri = exten;
350  exten = context;
351  context = NULL;
352  }
353  ast_channel_lock(chan);
354  if (ast_strlen_zero(exten)) {
355  exten = ast_strdupa(ast_channel_exten(chan));
356  }
357  if (ast_strlen_zero(context)) {
358  context = ast_strdupa(ast_channel_context(chan));
359  }
360  ast_channel_unlock(chan);
361 
362  /* size + 1 for null terminator + 2 commas */
363  size = sizeof(context) + sizeof(exten) + sizeof(pri) + 2;
364  dest = ast_malloc(size + 1);
365  snprintf(dest, size, "%s,%s,%s", context, exten, pri);
366  return dest;
367 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
const char * ast_channel_exten(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
char * strsep(char **str, const char *delims)
const char * ast_channel_context(const struct ast_channel *chan)
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ load_module()

static int load_module ( void  )
static

Definition at line 641 of file app_tonedetect.c.

References ast_custom_function_register, AST_MODULE_INFO_STANDARD_EXTENDED(), ast_register_application_xml, ASTERISK_GPL_KEY, and wait_exec().

642 {
643  int res;
644 
647 
648  return res;
649 }
static char * waitapp
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
static int wait_exec(struct ast_channel *chan, const char *data)
static struct ast_custom_function detect_function

◆ remove_detect()

static int remove_detect ( struct ast_channel chan)
static

Definition at line 270 of file app_tonedetect.c.

References ast_audiohook_remove(), ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_channel_name(), ast_datastore_free(), ast_log, AST_LOG_WARNING, detect_information::audiohook, ast_datastore::data, NULL, and SCOPED_CHANNELLOCK.

Referenced by detect_write().

271 {
272  struct ast_datastore *datastore = NULL;
273  struct detect_information *data;
274  SCOPED_CHANNELLOCK(chan_lock, chan);
275 
276  datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL);
277  if (!datastore) {
278  ast_log(AST_LOG_WARNING, "Cannot remove TONE_DETECT from %s: TONE_DETECT not currently enabled\n",
279  ast_channel_name(chan));
280  return -1;
281  }
282  data = datastore->data;
283 
284  if (ast_audiohook_remove(chan, &data->audiohook)) {
285  ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT audiohook from channel %s\n", ast_channel_name(chan));
286  return -1;
287  }
288 
289  if (ast_channel_datastore_remove(chan, datastore)) {
290  ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT datastore from channel %s\n",
291  ast_channel_name(chan));
292  return -1;
293  }
294  ast_datastore_free(datastore);
295 
296  return 0;
297 }
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Definition: audiohook.c:764
#define AST_LOG_WARNING
Definition: logger.h:279
struct ast_audiohook audiohook
Structure for a data store object.
Definition: datastore.h:68
static const struct ast_datastore_info detect_datastore
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 NULL
Definition: resample.c:96
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
#define ast_log
Definition: astobj2.c:42
#define SCOPED_CHANNELLOCK(varname, chan)
scoped lock specialization for channels.
Definition: lock.h:617
void * data
Definition: datastore.h:70
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2399

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 631 of file app_tonedetect.c.

References ast_custom_function_unregister(), and ast_unregister_application().

632 {
633  int res;
634 
637 
638  return res;
639 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static char * waitapp
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function detect_function

◆ wait_exec()

static int wait_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 524 of file app_tonedetect.c.

References args, AST_APP_ARG, ast_app_parse_options(), ast_channel_name(), ast_debug, AST_DECLARE_APP_ARGS, ast_dsp_free(), ast_dsp_new(), ast_dsp_process(), ast_dsp_set_features(), ast_dsp_set_freqmode(), AST_FRAME_DTMF, AST_FRAME_VOICE, ast_log, ast_read(), ast_remaining_ms(), AST_STANDARD_APP_ARGS, ast_str_to_int(), ast_strdupa, ast_strlen_zero, ast_test_flag, ast_tvnow(), ast_waitfor(), detect_information::db, detect_information::dsp, DSP_FEATURE_FREQ_DETECT, detect_information::duration, ast_frame::frametype, detect_information::freq1, detect_information::freq2, freq_parser(), ast_frame_subclass::integer, LOG_WARNING, NULL, OPT_APP_ARG_ARRAY_SIZE, OPT_APP_ARG_DECIBEL, OPT_APP_DECIBEL, OPT_APP_SQUELCH, options, pbx_builtin_setvar_helper(), result, detect_information::squelch, ast_frame::subclass, timeout, and wait_exec_options.

Referenced by load_module().

525 {
526  char *appdata;
527  struct ast_flags flags = {0};
528  char *opt_args[OPT_APP_ARG_ARRAY_SIZE];
529  double timeoutf = 0;
530  int freq1 = 0, freq2 = 0, timeout = 0, duration = 500, times = 1, db = 16, squelch = 0;
531  struct ast_frame *frame = NULL;
532  struct ast_dsp *dsp;
533  struct timeval start;
534  int remaining_time = 0;
535  int hits = 0;
537  AST_APP_ARG(freqs);
538  AST_APP_ARG(duration);
540  AST_APP_ARG(times);
542  );
543 
544  appdata = ast_strdupa(data);
545  AST_STANDARD_APP_ARGS(args, appdata);
546 
547  if (!ast_strlen_zero(args.options)) {
548  ast_app_parse_options(wait_exec_options, &flags, opt_args, args.options);
549  }
550  if (freq_parser(args.freqs, &freq1, &freq2)) {
551  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
552  return -1;
553  }
554  if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeout < 0)) {
555  ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout);
556  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
557  return -1;
558  }
559  timeout = 1000 * timeoutf;
560  if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) {
561  ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration);
562  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
563  return -1;
564  }
565  if (!ast_strlen_zero(args.times) && (ast_str_to_int(args.times, &times) || times < 1)) {
566  ast_log(LOG_WARNING, "Invalid number of times: %s\n", args.times);
567  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
568  return -1;
569  }
571  if ((ast_str_to_int(opt_args[OPT_APP_ARG_DECIBEL], &db) || db < 1)) {
572  ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_APP_ARG_DECIBEL]);
573  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
574  return -1;
575  }
576  }
577  squelch = ast_test_flag(&flags, OPT_APP_SQUELCH);
578  if (!(dsp = ast_dsp_new())) {
579  ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
580  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
581  return -1;
582  }
584  ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
585  ast_debug(1, "Waiting for %s Hz, %d time(s), timeout %d ms, %d db\n", args.freqs, times, timeout, db);
586  start = ast_tvnow();
587  while (timeout == 0 || remaining_time > 0) {
588  if (timeout > 0) {
589  remaining_time = ast_remaining_ms(start, timeout);
590  if (remaining_time <= 0) {
591  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "TIMEOUT");
592  break;
593  }
594  }
595  if (ast_waitfor(chan, 1000) > 0) {
596  if (!(frame = ast_read(chan))) {
597  ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
598  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP");
599  break;
600  } else if (frame->frametype == AST_FRAME_VOICE) {
601  frame = ast_dsp_process(chan, dsp, frame);
602  if (frame->frametype == AST_FRAME_DTMF) {
603  char result = frame->subclass.integer;
604  if (result == 'q') {
605  hits++;
606  ast_debug(1, "We just detected %s Hz (hit #%d)\n", args.freqs, hits);
607  if (hits >= times) {
608  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "SUCCESS");
609  break;
610  }
611  }
612  }
613  }
614  } else {
615  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP");
616  }
617  }
618  ast_dsp_free(dsp);
619 
620  return 0;
621 }
struct ast_frame * ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *inf)
Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress...
Definition: dsp.c:1494
#define ast_test_flag(p, flag)
Definition: utils.h:63
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1770
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
static sqlite3 * db
#define LOG_WARNING
Definition: logger.h:274
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1745
static int timeout
Definition: cdr_mysql.c:86
unsigned int flags
Definition: utils.h:200
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
static const struct ast_app_option wait_exec_options[128]
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
const char * args
#define NULL
Definition: resample.c:96
#define AST_FRAME_DTMF
struct ast_frame_subclass subclass
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
Definition: dsp.c:405
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:2906
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: main/utils.c:2033
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1755
Structure used to handle boolean flags.
Definition: utils.h:199
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3171
const char * ast_channel_name(const struct ast_channel *chan)
static PGresult * result
Definition: cel_pgsql.c:88
Data structure associated with a single frame of data.
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq1, int dur, int db, int squelch)
Set arbitrary frequency detection mode.
Definition: dsp.c:1859
enum ast_frame_type frametype
static struct test_options options
#define DSP_FEATURE_FREQ_DETECT
Definition: dsp.h:45
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
static int freq_parser(char *freqs, int *freq1, int *freq2)
#define AST_APP_ARG(name)
Define an application argument.

Variable Documentation

◆ detect_datastore

const struct ast_datastore_info detect_datastore
static
Initial value:
= {
.type = "detect",
.destroy = destroy_callback
}
static void destroy_callback(void *data)

Definition at line 219 of file app_tonedetect.c.

◆ detect_function

struct ast_custom_function detect_function
static
Initial value:
= {
.name = "TONE_DETECT",
.read = detect_read,
.write = detect_write,
}
static int detect_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int detect_read(struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen)

Definition at line 625 of file app_tonedetect.c.

◆ td_opts

const struct ast_app_option td_opts[128] = { [ 'd' ] = { .flag = OPT_DECIBEL , .arg_index = OPT_ARG_DECIBEL + 1 }, [ 'g' ] = { .flag = OPT_GOTO_RX , .arg_index = OPT_ARG_GOTO_RX + 1 }, [ 'h' ] = { .flag = OPT_GOTO_TX , .arg_index = OPT_ARG_GOTO_TX + 1 }, [ 's' ] = { .flag = OPT_SQUELCH }, [ 't' ] = { .flag = OPT_TX }, [ 'r' ] = { .flag = OPT_RX }, [ 'x' ] = { .flag = OPT_END_FILTER }, }
static

Definition at line 199 of file app_tonedetect.c.

◆ wait_exec_options

const struct ast_app_option wait_exec_options[128] = { [ 'd' ] = { .flag = OPT_APP_DECIBEL , .arg_index = OPT_APP_ARG_DECIBEL + 1 }, [ 's' ] = { .flag = OPT_APP_SQUELCH }, }
static

Definition at line 522 of file app_tonedetect.c.

Referenced by wait_exec().

◆ waitapp

char* waitapp = "WaitForTone"
static

Definition at line 623 of file app_tonedetect.c.