Asterisk - The Open Source Telephony Project  21.4.1
res_tonedetect.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2021, Naveen Albert
5  *
6  * Naveen Albert <asterisk@phreaknet.org>
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 Tone detection module
22  *
23  * \author Naveen Albert <asterisk@phreaknet.org>
24  *
25  * \ingroup resources
26  */
27 
28 /*** MODULEINFO
29  <support_level>extended</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include <math.h>
35 
36 #include "asterisk/module.h"
37 #include "asterisk/frame.h"
38 #include "asterisk/format_cache.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/dsp.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/audiohook.h"
43 #include "asterisk/app.h"
44 #include "asterisk/indications.h"
45 #include "asterisk/conversions.h"
46 
47 /*** DOCUMENTATION
48  <application name="WaitForTone" language="en_US">
49  <since>
50  <version>16.21.0</version>
51  <version>18.7.0</version>
52  <version>19.0.0</version>
53  </since>
54  <synopsis>
55  Wait for tone
56  </synopsis>
57  <syntax>
58  <parameter name="freq" required="true">
59  <para>Frequency of the tone to wait for.</para>
60  </parameter>
61  <parameter name="duration_ms" required="false">
62  <para>Minimum duration of tone, in ms. Default is 500ms.
63  Using a minimum duration under 50ms is unlikely to produce
64  accurate results.</para>
65  </parameter>
66  <parameter name="timeout" required="false">
67  <para>Maximum amount of time, in seconds, to wait for specified tone.
68  Default is forever.</para>
69  </parameter>
70  <parameter name="times" required="false">
71  <para>Number of times the tone should be detected (subject to the
72  provided timeout) before returning. Default is 1.</para>
73  </parameter>
74  <parameter name="options" required="false">
75  <optionlist>
76  <option name="d">
77  <para>Custom decibel threshold to use. Default is 16.</para>
78  </option>
79  <option name="s">
80  <para>Squelch tone.</para>
81  </option>
82  </optionlist>
83  </parameter>
84  </syntax>
85  <description>
86  <para>Waits for a single-frequency tone to be detected before dialplan execution continues.</para>
87  <variablelist>
88  <variable name="WAITFORTONESTATUS">
89  <para>This indicates the result of the wait.</para>
90  <value name="SUCCESS"/>
91  <value name="ERROR"/>
92  <value name="TIMEOUT"/>
93  <value name="HANGUP"/>
94  </variable>
95  </variablelist>
96  </description>
97  <see-also>
98  <ref type="application">PlayTones</ref>
99  </see-also>
100  </application>
101  <application name="ToneScan" language="en_US">
102  <since>
103  <version>16.23.0</version>
104  <version>18.9.0</version>
105  <version>19.1.0</version>
106  </since>
107  <synopsis>
108  Wait for period of time while scanning for call progress tones
109  </synopsis>
110  <syntax>
111  <parameter name="zone" required="false">
112  <para>Call progress zone. Default is the system default.</para>
113  </parameter>
114  <parameter name="timeout" required="false">
115  <para>Maximum amount of time, in seconds, to wait for call progress
116  or signal tones. Default is forever.</para>
117  </parameter>
118  <parameter name="threshold" required="false">
119  <para>DSP threshold required for a match. A higher number will
120  require a longer match and may reduce false positives, at the
121  expense of false negatives. Default is 1.</para>
122  </parameter>
123  <parameter name="options" required="false">
124  <optionlist>
125  <option name="f">
126  <para>Enable fax machine detection. By default, this is disabled.</para>
127  </option>
128  <option name="v">
129  <para>Enable voice detection. By default, this is disabled.</para>
130  </option>
131  </optionlist>
132  </parameter>
133  </syntax>
134  <description>
135  <para>Waits for a a distinguishable call progress tone and then exits.
136  Unlike a conventional scanner, this is not currently capable of
137  scanning for modem carriers.</para>
138  <variablelist>
139  <variable name="TONESCANSTATUS">
140  This indicates the result of the scan.
141  <value name="RINGING">
142  Audible ringback tone
143  </value>
144  <value name="BUSY">
145  Busy tone
146  </value>
147  <value name="SIT">
148  Special Information Tones
149  </value>
150  <value name="VOICE">
151  Human voice detected
152  </value>
153  <value name="DTMF">
154  DTMF digit
155  </value>
156  <value name="FAX">
157  Fax (answering)
158  </value>
159  <value name="MODEM">
160  Modem (answering)
161  </value>
162  <value name="DIALTONE">
163  Dial tone
164  </value>
165  <value name="NUT">
166  UK Number Unobtainable tone
167  </value>
168  <value name="TIMEOUT">
169  Timeout reached before any positive detection
170  </value>
171  <value name="HANGUP">
172  Caller hung up before any positive detection
173  </value>
174  </variable>
175  </variablelist>
176  </description>
177  <see-also>
178  <ref type="application">WaitForTone</ref>
179  </see-also>
180  </application>
181  <function name="TONE_DETECT" language="en_US">
182  <since>
183  <version>16.21.0</version>
184  <version>18.7.0</version>
185  <version>19.0.0</version>
186  </since>
187  <synopsis>
188  Asynchronously detects a tone
189  </synopsis>
190  <syntax>
191  <parameter name="freq" required="true">
192  <para>Frequency of the tone to detect. To disable frequency
193  detection completely (e.g. for signal detection only),
194  specify 0 for the frequency.</para>
195  </parameter>
196  <parameter name="duration_ms" required="false">
197  <para>Minimum duration of tone, in ms. Default is 500ms.
198  Using a minimum duration under 50ms is unlikely to produce
199  accurate results.</para>
200  </parameter>
201  <parameter name="options">
202  <optionlist>
203  <option name="a">
204  <para>Match immediately on Special Information Tones, instead of or in addition
205  to a particular frequency.</para>
206  </option>
207  <option name="b">
208  <para>Match immediately on a busy signal, instead of or in addition to
209  a particular frequency.</para>
210  </option>
211  <option name="c">
212  <para>Match immediately on a dial tone, instead of or in addition to
213  a particular frequency.</para>
214  </option>
215  <option name="d">
216  <para>Custom decibel threshold to use. Default is 16.</para>
217  </option>
218  <option name="g">
219  <para>Go to the specified context,exten,priority if tone is received on this channel.
220  Detection will not end automatically.</para>
221  </option>
222  <option name="h">
223  <para>Go to the specified context,exten,priority if tone is transmitted on this channel.
224  Detection will not end automatically.</para>
225  </option>
226  <option name="n">
227  <para>Number of times the tone should be detected (subject to the
228  provided timeout) before going to the destination provided in the <literal>g</literal>
229  or <literal>h</literal> option. Default is 1.</para>
230  </option>
231  <option name="p">
232  <para>Match immediately on audible ringback tone, instead of or in addition to
233  a particular frequency.</para>
234  </option>
235  <option name="r">
236  <para>Apply to received frames only. Default is both directions.</para>
237  </option>
238  <option name="s">
239  <para>Squelch tone.</para>
240  </option>
241  <option name="t">
242  <para>Apply to transmitted frames only. Default is both directions.</para>
243  </option>
244  <option name="x">
245  <para>Destroy the detector (stop detection).</para>
246  </option>
247  </optionlist>
248  </parameter>
249  </syntax>
250  <description>
251  <para>The TONE_DETECT function detects a single-frequency tone and keeps
252  track of how many times the tone has been detected.</para>
253  <para>When reading this function (instead of writing), supply <literal>tx</literal>
254  to get the number of times a tone has been detected in the TX direction and
255  <literal>rx</literal> to get the number of times a tone has been detected in the
256  RX direction.</para>
257  <example title="intercept2600">
258  same => n,Set(TONE_DETECT(2600,1000,g(got-2600,s,1))=) ; detect 2600 Hz
259  same => n,Wait(15)
260  same => n,NoOp(${TONE_DETECT(rx)})
261  </example>
262  <example title="dropondialtone">
263  same => n,Set(TONE_DETECT(0,,bg(my-hangup,s,1))=) ; disconnect a call if we hear a busy signal
264  same => n,Goto(somewhere-else)
265  same => n(myhangup),Hangup()
266  </example>
267  <example title="removedetector">
268  same => n,Set(TONE_DETECT(0,,x)=) ; remove the detector from the channel
269  </example>
270  </description>
271  </function>
272  ***/
273 
275  struct ast_dsp *dsp;
276  struct ast_audiohook audiohook;
277  int freq1;
278  int freq2;
279  int duration;
280  int db;
281  char *gototx;
282  char *gotorx;
283  unsigned short int squelch;
284  unsigned short int tx;
285  unsigned short int rx;
286  int txcount;
287  int rxcount;
288  int hitsrequired;
289  int signalfeatures;
290 };
291 
292 enum td_opts {
293  OPT_TX = (1 << 1),
294  OPT_RX = (1 << 2),
295  OPT_END_FILTER = (1 << 3),
296  OPT_GOTO_RX = (1 << 4),
297  OPT_GOTO_TX = (1 << 5),
298  OPT_DECIBEL = (1 << 6),
299  OPT_SQUELCH = (1 << 7),
300  OPT_HITS_REQ = (1 << 8),
301  OPT_SIT = (1 << 9),
302  OPT_BUSY = (1 << 10),
303  OPT_DIALTONE = (1 << 11),
304  OPT_RINGING = (1 << 12),
305 };
306 
307 enum {
308  OPT_ARG_DECIBEL,
309  OPT_ARG_GOTO_RX,
310  OPT_ARG_GOTO_TX,
311  OPT_ARG_HITS_REQ,
312  /* note: this entry _MUST_ be the last one in the enum */
313  OPT_ARG_ARRAY_SIZE,
314 };
315 
316 AST_APP_OPTIONS(td_opts, {
317  AST_APP_OPTION('a', OPT_SIT),
318  AST_APP_OPTION('b', OPT_BUSY),
319  AST_APP_OPTION('c', OPT_DIALTONE),
320  AST_APP_OPTION_ARG('d', OPT_DECIBEL, OPT_ARG_DECIBEL),
321  AST_APP_OPTION_ARG('g', OPT_GOTO_RX, OPT_ARG_GOTO_RX),
322  AST_APP_OPTION_ARG('h', OPT_GOTO_TX, OPT_ARG_GOTO_TX),
323  AST_APP_OPTION_ARG('n', OPT_HITS_REQ, OPT_ARG_HITS_REQ),
324  AST_APP_OPTION('p', OPT_RINGING),
325  AST_APP_OPTION('s', OPT_SQUELCH),
326  AST_APP_OPTION('t', OPT_TX),
327  AST_APP_OPTION('r', OPT_RX),
328  AST_APP_OPTION('x', OPT_END_FILTER),
329 });
330 
331 static void destroy_callback(void *data)
332 {
333  struct detect_information *di = data;
334  ast_dsp_free(di->dsp);
335  if (di->gotorx) {
336  ast_free(di->gotorx);
337  }
338  if (di->gototx) {
339  ast_free(di->gototx);
340  }
341  ast_audiohook_lock(&di->audiohook);
342  ast_audiohook_detach(&di->audiohook);
343  ast_audiohook_unlock(&di->audiohook);
344  ast_audiohook_destroy(&di->audiohook);
345  ast_free(di);
346  return;
347 }
348 
349 static const struct ast_datastore_info detect_datastore = {
350  .type = "detect",
351  .destroy = destroy_callback
352 };
353 
354 static int detect_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
355 {
356  struct ast_datastore *datastore = NULL;
357  struct detect_information *di = NULL;
358  int match = 0;
359 
360  /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
361  if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
362  return 0;
363  }
364 
365  /* Grab datastore which contains our gain information */
366  if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
367  return 0;
368  }
369 
370  di = datastore->data;
371 
372  if (!frame || frame->frametype != AST_FRAME_VOICE) {
373  return 0;
374  }
375 
376  if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? di->rx : di->tx)) {
377  return 0;
378  }
379 
380  /* ast_dsp_process may free the frame and return a new one */
381  frame = ast_frdup(frame);
382  frame = ast_dsp_process(chan, di->dsp, frame);
383  if (frame->frametype == AST_FRAME_DTMF) {
384  char result = frame->subclass.integer;
385  if (result == 'q') {
386  int now;
387  match = 1;
388  if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
389  di->rxcount = di->rxcount + 1;
390  now = di->rxcount;
391  } else {
392  di->txcount = di->txcount + 1;
393  now = di->txcount;
394  }
395  ast_debug(1, "TONE_DETECT just got a hit (#%d in this direction, waiting for %d total)\n", now, di->hitsrequired);
396  if (now >= di->hitsrequired) {
397  if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) {
398  ast_async_parseable_goto(chan, di->gotorx);
399  } else if (di->gototx) {
400  ast_async_parseable_goto(chan, di->gototx);
401  }
402  }
403  }
404  }
405  if (di->signalfeatures && !match) { /* skip unless there are call progress/signal options */
406  int tstate, tcount;
407  tcount = ast_dsp_get_tcount(di->dsp);
408  tstate = ast_dsp_get_tstate(di->dsp);
409  if (tstate > 0) {
410  ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate);
411  switch (tstate) {
412  case DSP_TONE_STATE_RINGING:
413  if (di->signalfeatures & DSP_PROGRESS_RINGING) {
414  ast_debug(1, "Detected ringing on %s in %s direction\n", ast_channel_name(chan),
415  direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write");
416  match = 1;
417  }
418  break;
419  case DSP_TONE_STATE_DIALTONE:
420  if (di->signalfeatures & DSP_FEATURE_WAITDIALTONE) {
421  ast_debug(1, "Detected dial tone on %s in %s direction\n", ast_channel_name(chan),
422  direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write");
423  match = 1;
424  }
425  break;
426  case DSP_TONE_STATE_BUSY:
427  if (di->signalfeatures & DSP_PROGRESS_BUSY) {
428  ast_debug(1, "Detected busy tone on %s in %s direction\n", ast_channel_name(chan),
429  direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write");
430  match = 1;
431  }
432  break;
433  case DSP_TONE_STATE_SPECIAL3:
434  if (di->signalfeatures & DSP_PROGRESS_CONGESTION) {
435  ast_debug(1, "Detected SIT on %s in %s direction\n", ast_channel_name(chan),
436  direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write");
437  match = 1;
438  }
439  break;
440  default: /* ignore */
441  break;
442  }
443  if (match) {
444  if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) {
445  ast_async_parseable_goto(chan, di->gotorx);
446  } else if (di->gototx) {
447  ast_async_parseable_goto(chan, di->gototx);
448  } else {
449  ast_debug(3, "Detected call progress signal in %s direction, but don't know where to go\n",
450  direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write");
451  }
452  }
453  }
454  }
455  /* this could be the duplicated frame or a new one, doesn't matter */
456  ast_frfree(frame);
457  return 0;
458 }
459 
460 static int remove_detect(struct ast_channel *chan)
461 {
462  struct ast_datastore *datastore = NULL;
463  struct detect_information *data;
464  SCOPED_CHANNELLOCK(chan_lock, chan);
465 
466  datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL);
467  if (!datastore) {
468  ast_log(AST_LOG_WARNING, "Cannot remove TONE_DETECT from %s: TONE_DETECT not currently enabled\n",
469  ast_channel_name(chan));
470  return -1;
471  }
472  data = datastore->data;
473 
474  if (ast_audiohook_remove(chan, &data->audiohook)) {
475  ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT audiohook from channel %s\n", ast_channel_name(chan));
476  return -1;
477  }
478 
479  if (ast_channel_datastore_remove(chan, datastore)) {
480  ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT datastore from channel %s\n",
481  ast_channel_name(chan));
482  return -1;
483  }
484  ast_datastore_free(datastore);
485 
486  return 0;
487 }
488 
489 static int freq_parser(char *freqs, int *freq1, int *freq2) {
490  char *f1, *f2, *f3;
491  if (ast_strlen_zero(freqs)) {
492  ast_log(LOG_ERROR, "No frequency specified\n");
493  return -1;
494  }
495  f3 = ast_strdupa(freqs);
496  f1 = strsep(&f3, "+");
497  f2 = strsep(&f3, "+");
498  if (!ast_strlen_zero(f3)) {
499  ast_log(LOG_WARNING, "Only up to 2 frequencies may be specified: %s\n", freqs);
500  return -1;
501  }
502  if (ast_str_to_int(f1, freq1)) {
503  ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f1);
504  return -1;
505  }
506  if (*freq1 < 0) {
507  ast_log(LOG_WARNING, "Sorry, no negative frequencies: %d\n", *freq1);
508  return -1;
509  }
510  if (!ast_strlen_zero(f2)) {
511  ast_log(LOG_WARNING, "Sorry, currently only 1 frequency is supported\n");
512  return -1;
513  /* not supported just yet, but possibly will be in the future */
514  if (ast_str_to_int(f2, freq2)) {
515  ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f2);
516  return -1;
517  }
518  if (*freq2 < 1) {
519  ast_log(LOG_WARNING, "Sorry, positive frequencies only: %d\n", *freq2);
520  return -1;
521  }
522  }
523  return 0;
524 }
525 
526 static char* goto_parser(struct ast_channel *chan, char *loc) {
527  char *exten, *pri, *context, *parse;
528  char *dest;
529  int size;
530  parse = ast_strdupa(loc);
531  context = strsep(&parse, ",");
532  exten = strsep(&parse, ",");
533  pri = strsep(&parse, ",");
534  if (!exten) {
535  pri = context;
536  exten = NULL;
537  context = NULL;
538  } else if (!pri) {
539  pri = exten;
540  exten = context;
541  context = NULL;
542  }
543  ast_channel_lock(chan);
544  if (ast_strlen_zero(exten)) {
545  exten = ast_strdupa(ast_channel_exten(chan));
546  }
547  if (ast_strlen_zero(context)) {
548  context = ast_strdupa(ast_channel_context(chan));
549  }
550  ast_channel_unlock(chan);
551 
552  /* size + 3: for 1 null terminator + 2 commas */
553  size = strlen(context) + strlen(exten) + strlen(pri) + 3;
554  dest = ast_malloc(size + 1);
555  if (!dest) {
556  ast_log(LOG_ERROR, "Failed to parse goto: %s,%s,%s\n", context, exten, pri);
557  return NULL;
558  }
559  snprintf(dest, size, "%s,%s,%s", context, exten, pri);
560  return dest;
561 }
562 
563 static int detect_read(struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen)
564 {
565  struct ast_datastore *datastore = NULL;
566  struct detect_information *di = NULL;
567 
568  if (!chan) {
569  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
570  return -1;
571  }
572 
573  ast_channel_lock(chan);
574  if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
575  ast_channel_unlock(chan);
576  return -1; /* function not initiated yet, so nothing to read */
577  } else {
578  ast_channel_unlock(chan);
579  di = datastore->data;
580  }
581 
582  if (strchr(data, 't')) {
583  snprintf(buffer, buflen, "%d", di->txcount);
584  } else if (strchr(data, 'r')) {
585  snprintf(buffer, buflen, "%d", di->rxcount);
586  } else {
587  ast_log(LOG_WARNING, "Invalid direction: %s\n", data);
588  }
589 
590  return 0;
591 }
592 
593 static int parse_signal_features(struct ast_flags *flags)
594 {
595  int features = 0;
596 
597  if (ast_test_flag(flags, OPT_SIT)) {
598  features |= DSP_PROGRESS_CONGESTION;
599  }
600  if (ast_test_flag(flags, OPT_BUSY)) {
601  features |= DSP_PROGRESS_BUSY;
602  }
603  if (ast_test_flag(flags, OPT_DIALTONE)) {
604  features |= DSP_FEATURE_WAITDIALTONE;
605  }
606  if (ast_test_flag(flags, OPT_RINGING)) {
607  features |= DSP_PROGRESS_RINGING;
608  }
609 
610  return features;
611 }
612 
613 static int detect_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
614 {
615  char *parse;
616  struct ast_datastore *datastore = NULL;
617  struct detect_information *di = NULL;
618  struct ast_flags flags = { 0 };
619  char *opt_args[OPT_ARG_ARRAY_SIZE];
620  struct ast_dsp *dsp;
621  int freq1 = 0, freq2 = 0, duration = 500, db = 16, squelch = 0, hitsrequired = 1;
622  int signalfeatures = 0;
623 
625  AST_APP_ARG(freqs);
626  AST_APP_ARG(duration);
627  AST_APP_ARG(options);
628  );
629 
630  if (!chan) {
631  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
632  return -1;
633  }
634  parse = ast_strdupa(data);
635  AST_STANDARD_APP_ARGS(args, parse);
636 
637  if (!ast_strlen_zero(args.options)) {
638  ast_app_parse_options(td_opts, &flags, opt_args, args.options);
639  }
640  if (ast_test_flag(&flags, OPT_END_FILTER)) {
641  return remove_detect(chan);
642  }
643  if (freq_parser(args.freqs, &freq1, &freq2)) {
644  return -1;
645  }
646  if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) {
647  ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration);
648  return -1;
649  }
650  if (ast_test_flag(&flags, OPT_HITS_REQ) && !ast_strlen_zero(opt_args[OPT_ARG_HITS_REQ])) {
651  if ((ast_str_to_int(opt_args[OPT_ARG_HITS_REQ], &hitsrequired) || hitsrequired < 1)) {
652  ast_log(LOG_WARNING, "Invalid number hits required: %s\n", opt_args[OPT_ARG_HITS_REQ]);
653  return -1;
654  }
655  }
656  if (ast_test_flag(&flags, OPT_DECIBEL) && !ast_strlen_zero(opt_args[OPT_ARG_DECIBEL])) {
657  if ((ast_str_to_int(opt_args[OPT_ARG_DECIBEL], &db) || db < 1)) {
658  ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_ARG_DECIBEL]);
659  return -1;
660  }
661  }
662  signalfeatures = parse_signal_features(&flags);
663 
664  ast_channel_lock(chan);
665  if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
666  if (!(datastore = ast_datastore_alloc(&detect_datastore, NULL))) {
667  ast_channel_unlock(chan);
668  return 0;
669  }
670  if (!(di = ast_calloc(1, sizeof(*di)))) {
671  ast_datastore_free(datastore);
672  ast_channel_unlock(chan);
673  return 0;
674  }
676  di->audiohook.manipulate_callback = detect_callback;
677  if (!(dsp = ast_dsp_new())) {
678  ast_datastore_free(datastore);
679  ast_channel_unlock(chan);
680  ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
681  return -1;
682  }
683  di->signalfeatures = signalfeatures; /* we're not including freq detect */
684  if (freq1 > 0) {
685  signalfeatures |= DSP_FEATURE_FREQ_DETECT;
686  ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
687  }
688  ast_dsp_set_features(dsp, signalfeatures);
689  di->dsp = dsp;
690  di->txcount = 0;
691  di->rxcount = 0;
692  ast_debug(1, "Keeping our ears open for %s Hz, %d db\n", args.freqs, db);
693  datastore->data = di;
694  ast_channel_datastore_add(chan, datastore);
695  ast_audiohook_attach(chan, &di->audiohook);
696  } else {
697  di = datastore->data;
698  dsp = di->dsp;
699  di->signalfeatures = signalfeatures; /* we're not including freq detect */
700  if (freq1 > 0) {
701  signalfeatures |= DSP_FEATURE_FREQ_DETECT;
702  ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
703  }
704  ast_dsp_set_features(dsp, signalfeatures);
705  }
706  di->duration = duration;
707  di->gotorx = NULL;
708  di->gototx = NULL;
709  /* resolve gotos now, in case a full context,exten,pri wasn't specified */
710  if (ast_test_flag(&flags, OPT_GOTO_RX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_RX])) {
711  di->gotorx = goto_parser(chan, opt_args[OPT_ARG_GOTO_RX]);
712  }
713  if (ast_test_flag(&flags, OPT_GOTO_TX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_TX])) {
714  di->gototx = goto_parser(chan, opt_args[OPT_ARG_GOTO_TX]);
715  }
716  di->db = db;
717  di->hitsrequired = hitsrequired;
718  di->squelch = ast_test_flag(&flags, OPT_SQUELCH);
719  di->tx = 1;
720  di->rx = 1;
721  if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_TX)) {
722  di->tx = 1;
723  di->rx = 0;
724  }
725  if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_RX)) {
726  di->rx = 1;
727  di->tx = 0;
728  }
729  ast_channel_unlock(chan);
730 
731  return 0;
732 }
733 
734 enum {
735  OPT_APP_DECIBEL = (1 << 0),
736  OPT_APP_SQUELCH = (1 << 1),
737 };
738 
739 enum {
740  OPT_APP_ARG_DECIBEL,
741  /* note: this entry _MUST_ be the last one in the enum */
742  OPT_APP_ARG_ARRAY_SIZE,
743 };
744 
745 AST_APP_OPTIONS(wait_exec_options, BEGIN_OPTIONS
746  AST_APP_OPTION_ARG('d', OPT_APP_DECIBEL, OPT_APP_ARG_DECIBEL),
747  AST_APP_OPTION('s', OPT_APP_SQUELCH),
748 END_OPTIONS);
749 
750 static int wait_exec(struct ast_channel *chan, const char *data)
751 {
752  char *appdata;
753  struct ast_flags flags = {0};
754  char *opt_args[OPT_APP_ARG_ARRAY_SIZE];
755  double timeoutf = 0;
756  int freq1 = 0, freq2 = 0, timeout = 0, duration = 500, times = 1, db = 16, squelch = 0;
757  struct ast_frame *frame = NULL;
758  struct ast_dsp *dsp;
759  struct timeval start;
760  int remaining_time = 0;
761  int hits = 0;
763  AST_APP_ARG(freqs);
764  AST_APP_ARG(duration);
765  AST_APP_ARG(timeout);
766  AST_APP_ARG(times);
767  AST_APP_ARG(options);
768  );
769 
770  appdata = ast_strdupa(data);
771  AST_STANDARD_APP_ARGS(args, appdata);
772 
773  if (!ast_strlen_zero(args.options)) {
774  ast_app_parse_options(wait_exec_options, &flags, opt_args, args.options);
775  }
776  if (freq_parser(args.freqs, &freq1, &freq2)) {
777  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
778  return -1;
779  }
780  if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeoutf < 0)) {
781  ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout);
782  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
783  return -1;
784  }
785  timeout = 1000 * timeoutf;
786  if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) {
787  ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration);
788  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
789  return -1;
790  }
791  if (!ast_strlen_zero(args.times) && (ast_str_to_int(args.times, &times) || times < 1)) {
792  ast_log(LOG_WARNING, "Invalid number of times: %s\n", args.times);
793  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
794  return -1;
795  }
796  if (ast_test_flag(&flags, OPT_APP_DECIBEL) && !ast_strlen_zero(opt_args[OPT_APP_ARG_DECIBEL])) {
797  if ((ast_str_to_int(opt_args[OPT_APP_ARG_DECIBEL], &db) || db < 1)) {
798  ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_APP_ARG_DECIBEL]);
799  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
800  return -1;
801  }
802  }
803  squelch = ast_test_flag(&flags, OPT_APP_SQUELCH);
804  if (!(dsp = ast_dsp_new())) {
805  ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
806  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
807  return -1;
808  }
810  ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
811  ast_debug(1, "Waiting for %s Hz, %d time(s), timeout %d ms, %d db\n", args.freqs, times, timeout, db);
812  start = ast_tvnow();
813  do {
814  if (timeout > 0) {
815  remaining_time = ast_remaining_ms(start, timeout);
816  if (remaining_time <= 0) {
817  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "TIMEOUT");
818  break;
819  }
820  }
821  if (ast_waitfor(chan, 1000) > 0) {
822  if (!(frame = ast_read(chan))) {
823  ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
824  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP");
825  break;
826  } else if (frame->frametype == AST_FRAME_VOICE) {
827  frame = ast_dsp_process(chan, dsp, frame);
828  if (frame->frametype == AST_FRAME_DTMF) {
829  char result = frame->subclass.integer;
830  if (result == 'q') {
831  hits++;
832  ast_debug(1, "We just detected %s Hz (hit #%d)\n", args.freqs, hits);
833  if (hits >= times) {
834  ast_frfree(frame);
835  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "SUCCESS");
836  break;
837  }
838  }
839  }
840  }
841  ast_frfree(frame);
842  } else {
843  pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP");
844  }
845  } while (timeout == 0 || remaining_time > 0);
846  ast_dsp_free(dsp);
847 
848  return 0;
849 }
850 
851 static char *waitapp = "WaitForTone";
852 static char *scanapp = "ToneScan";
853 
854 static int scan_exec(struct ast_channel *chan, const char *data)
855 {
856  char *appdata;
857  double timeoutf = 0;
858  int timeout = 0;
859  struct ast_frame *frame = NULL, *frame2 = NULL;
860  struct ast_dsp *dsp = NULL, *dsp2 = NULL;
861  struct timeval start;
862  int remaining_time = 0;
863  int features, match = 0, fax = 0, voice = 0, threshold = 1;
865  AST_APP_ARG(zone);
866  AST_APP_ARG(timeout);
867  AST_APP_ARG(threshold);
868  AST_APP_ARG(options);
869  );
870 
871  appdata = ast_strdupa(data);
872  AST_STANDARD_APP_ARGS(args, appdata);
873 
874  if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeout < 0)) {
875  ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout);
876  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
877  return -1;
878  }
879  if (!ast_strlen_zero(args.threshold) && (ast_str_to_int(args.threshold, &threshold) || threshold < 1)) {
880  ast_log(LOG_WARNING, "Invalid threshold: %s\n", args.threshold);
881  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
882  return -1;
883  }
884  timeout = 1000 * timeoutf;
885 
886  if (!ast_strlen_zero(args.options) && strchr(args.options, 'f')) {
887  fax = 1;
888  }
889  if (!ast_strlen_zero(args.options) && strchr(args.options, 'v')) {
890  voice = 1;
891  }
892 
893  if (!(dsp = ast_dsp_new())) {
894  ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
895  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
896  return -1;
897  }
898 
899  if (!ast_strlen_zero(args.zone)) {
900  if (ast_dsp_set_call_progress_zone(dsp, args.zone)) {
901  ast_log(LOG_WARNING, "Invalid call progress zone: %s\n", args.zone);
902  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
903  ast_dsp_free(dsp);
904  return -1;
905  }
906  }
907 
908  if (fax) {
909  if (!(dsp2 = ast_dsp_new())) {
910  ast_dsp_free(dsp);
911  ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
912  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
913  return -1;
914  }
915  }
916 
917  features = DSP_PROGRESS_RINGING; /* audible ringback tone */
918  features |= DSP_PROGRESS_BUSY; /* busy signal */
919  features |= DSP_PROGRESS_CONGESTION; /* SIT tones (not reorder!) */
920  features |= DSP_PROGRESS_TALK; /* voice. */
921  features |= DSP_FEATURE_WAITDIALTONE; /* dial tone */
922  features |= DSP_FEATURE_FREQ_DETECT; /* modem answer */
923  if (voice) {
924  features |= DSP_TONE_STATE_TALKING; /* voice */
925  }
926  ast_dsp_set_features(dsp, features);
927  /* all modems begin negotiating with Bell 103. An answering modem just sends mark tone, or 2225 Hz */
928  ast_dsp_set_freqmode(dsp, 2225, 400, 16, 0); /* this needs to be pretty short, or the progress tones code will think this is voice */
929 
930  if (fax) { /* fax detect uses same tone detect internals as modem and causes things to not work as intended, so use a separate DSP if needed. */
931  ast_dsp_set_features(dsp2, DSP_FEATURE_FAX_DETECT); /* fax tone */
932  ast_dsp_set_faxmode(dsp2, DSP_FAXMODE_DETECT_CED); /* we only care about the answering side (CED), not originating (CNG) */
933  }
934 
935  ast_debug(1, "Starting tone scan, timeout: %d ms, threshold: %d\n", timeout, threshold);
936  start = ast_tvnow();
937  do {
938  if (timeout > 0) {
939  remaining_time = ast_remaining_ms(start, timeout);
940  if (remaining_time <= 0) {
941  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "TIMEOUT");
942  break;
943  }
944  }
945  if (ast_waitfor(chan, 1000) > 0) {
946  if (!(frame = ast_read(chan))) {
947  ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
948  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP");
949  break;
950  } else if (frame->frametype == AST_FRAME_VOICE) {
951  if (fax) {
952  frame2 = ast_frdup(frame);
953  }
954  frame = ast_dsp_process(chan, dsp, frame);
955  if (frame->frametype == AST_FRAME_DTMF) {
956  char result = frame->subclass.integer;
957  match = 1;
958  if (result == 'q') {
959  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "MODEM");
960  } else {
961  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DTMF");
962  }
963  } else if (fax) {
964  char result;
965  frame2 = ast_dsp_process(chan, dsp2, frame2);
966  result = frame2->subclass.integer;
967  if (frame2->frametype == AST_FRAME_DTMF) {
968  if (result == 'e') {
969  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "FAX");
970  match = 1;
971  } else {
972  ast_debug(1, "Ignoring inactionable event\n"); /* shouldn't happen */
973  }
974  }
975  ast_frfree(frame2);
976  }
977  if (!match) {
978  int tstate, tcount;
979  tcount = ast_dsp_get_tcount(dsp);
980  tstate = ast_dsp_get_tstate(dsp);
981  if (tstate > 0) {
982  ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate);
983  if (tcount >= threshold) {
984  match = 1;
985  switch (tstate) {
986  case DSP_TONE_STATE_RINGING:
987  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "RINGING");
988  break;
989  case DSP_TONE_STATE_DIALTONE:
990  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DIALTONE");
991  break;
992  case DSP_TONE_STATE_TALKING:
993  /* even if we don't specify this feature, it's still checked, so we always need to handle it.
994  Even if we are looking for it, we need to wait a while or tones will be interpreted
995  as voice, because this will match first (and this should match last). */
996  if (voice && tcount > 15 && tcount >= threshold) {
997  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "VOICE");
998  } else {
999  match = 0;
1000  }
1001  break;
1002  case DSP_TONE_STATE_BUSY:
1003  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "BUSY");
1004  break;
1005  case DSP_TONE_STATE_SPECIAL3:
1006  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "SIT");
1007  break;
1008  case DSP_TONE_STATE_HUNGUP: /* UK only */
1009  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "NUT");
1010  break;
1011  default:
1012  match = 0;
1013  ast_debug(1, "Something else we weren't expecting? tstate: %d, #%d\n", tstate, tcount);
1014  }
1015  }
1016  }
1017  }
1018  }
1019  ast_frfree(frame);
1020  } else {
1021  pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP");
1022  }
1023  } while (!match && (timeout == 0 || remaining_time > 0));
1024  ast_dsp_free(dsp);
1025  if (dsp2) {
1026  ast_dsp_free(dsp2);
1027  }
1028 
1029  return 0;
1030 }
1031 
1032 static struct ast_custom_function detect_function = {
1033  .name = "TONE_DETECT",
1034  .read = detect_read,
1035  .write = detect_write,
1036 };
1037 
1038 static int unload_module(void)
1039 {
1040  int res;
1041 
1042  res = ast_unregister_application(waitapp);
1043  res |= ast_unregister_application(scanapp);
1044  res |= ast_custom_function_unregister(&detect_function);
1045 
1046  return res;
1047 }
1048 
1049 static int load_module(void)
1050 {
1051  int res;
1052 
1053  res = ast_register_application_xml(waitapp, wait_exec);
1054  res |= ast_register_application_xml(scanapp, scan_exec);
1055  res |= ast_custom_function_register(&detect_function);
1056 
1057  return res;
1058 }
1059 
1060 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Tone detection module");
const char * name
Definition: pbx.h:119
const char * type
Definition: datastore.h:32
#define DSP_PROGRESS_RINGING
Definition: dsp.h:40
Tone Indication Support.
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:1499
Main Channel structure associated with a channel.
#define ast_frdup(fr)
Copies a frame.
Asterisk main include file. File version handling, generic pbx functions.
int ast_dsp_get_tcount(struct ast_dsp *dsp)
Get tcount (Threshold counter)
Definition: dsp.c:1916
Convenient Signal Processing routines.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define DSP_PROGRESS_BUSY
Definition: dsp.h:41
Audiohooks Architecture.
#define DSP_PROGRESS_TALK
Definition: dsp.h:39
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Definition: audiohook.c:721
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1758
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4257
#define DSP_PROGRESS_CONGESTION
Definition: dsp.h:42
Structure for a data store type.
Definition: datastore.h:31
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:484
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
Structure for a data store object.
Definition: datastore.h:64
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:2399
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:124
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
struct ast_frame_subclass subclass
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:118
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
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
Definition: audiohook.h:318
#define SCOPED_CHANNELLOCK(varname, chan)
scoped lock specialization for channels.
Definition: lock.h:619
General Asterisk PBX channel definitions.
Definition: dsp.c:407
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Asterisk internal frame definitions.
Conversion utility functions.
int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode)
Set fax mode.
Definition: dsp.c:1883
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_debug(level,...)
Log a DEBUG message.
int ast_dsp_get_tstate(struct ast_dsp *dsp)
Get tstate (Tone State)
Definition: dsp.c:1911
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:3066
Core PBX routines and definitions.
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:550
#define AST_APP_OPTION_ARG(option, flagno, argno)
Declares an application option that accepts an argument.
static void destroy_callback(void *data)
Helper function used by datastores to destroy the speech structure upon hangup.
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:2281
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1768
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq, int dur, int db, int squelch)
Set arbitrary frequency detection mode.
Definition: dsp.c:1872
Structure used to handle boolean flags.
Definition: utils.h:199
ast_audiohook_direction
Definition: audiohook.h:48
int ast_async_parseable_goto(struct ast_channel *chan, const char *goto_string)
Definition: pbx.c:8871
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...
void * data
Definition: datastore.h:66
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3162
Data structure associated with a single frame of data.
enum ast_audiohook_status status
Definition: audiohook.h:108
enum ast_frame_type frametype
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
#define DSP_FEATURE_FREQ_DETECT
Definition: dsp.h:45
#define DSP_FEATURE_WAITDIALTONE
Definition: dsp.h:44
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:313
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2385
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone)
Set zone for doing progress detection.
Definition: dsp.c:1892
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2394
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Media Format Cache API.
#define AST_APP_ARG(name)
Define an application argument.