Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2021, Naveen Albert
*
* Naveen Albert <asterisk@phreaknet.org>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Tone detection module
*
* \author Naveen Albert <asterisk@phreaknet.org>
*
* \ingroup resources
*/
/*** MODULEINFO
<support_level>extended</support_level>
***/
#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"
/*** DOCUMENTATION
<application name="WaitForTone" language="en_US">
<synopsis>
Wait for tone
</synopsis>
<syntax>
<parameter name="freq" required="true">
<para>Frequency of the tone to wait for.</para>
</parameter>
<parameter name="duration_ms" required="false">
<para>Minimum duration of tone, in ms. Default is 500ms.
Using a minimum duration under 50ms is unlikely to produce
accurate results.</para>
</parameter>
<parameter name="timeout" required="false">
<para>Maximum amount of time, in seconds, to wait for specified tone.
Default is forever.</para>
</parameter>
<parameter name="times" required="false">
<para>Number of times the tone should be detected (subject to the
provided timeout) before returning. Default is 1.</para>
</parameter>
<parameter name="options" required="false">
<optionlist>
<option name="d">
<para>Custom decibel threshold to use. Default is 16.</para>
</option>
<option name="s">
<para>Squelch tone.</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>Waits for a single-frequency tone to be detected before dialplan execution continues.</para>
<variablelist>
<variable name="WAITFORTONESTATUS">
<para>This indicates the result of the wait.</para>
<value name="SUCCESS"/>
<value name="ERROR"/>
<value name="TIMEOUT"/>
<value name="HANGUP"/>
</variable>
</variablelist>
</description>
<see-also>
<ref type="application">PlayTones</ref>
</see-also>
</application>
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<application name="ToneScan" language="en_US">
<synopsis>
Wait for period of time while scanning for call progress tones
</synopsis>
<syntax>
<parameter name="zone" required="false">
<para>Call progress zone. Default is the system default.</para>
</parameter>
<parameter name="timeout" required="false">
<para>Maximum amount of time, in seconds, to wait for call progress
or signal tones. Default is forever.</para>
</parameter>
<parameter name="threshold" required="false">
<para>DSP threshold required for a match. A higher number will
require a longer match and may reduce false positives, at the
expense of false negatives. Default is 1.</para>
</parameter>
<parameter name="options" required="false">
<optionlist>
<option name="f">
<para>Enable fax machine detection. By default, this is disabled.</para>
</option>
<option name="v">
<para>Enable voice detection. By default, this is disabled.</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>Waits for a a distinguishable call progress tone and then exits.
Unlike a conventional scanner, this is not currently capable of
scanning for modem carriers.</para>
<variablelist>
<variable name="TONESCANSTATUS">
This indicates the result of the scan.
<value name="RINGING">
Audible ringback tone
</value>
<value name="BUSY">
Busy tone
</value>
<value name="SIT">
Special Information Tones
</value>
<value name="VOICE">
Human voice detected
</value>
<value name="DTMF">
DTMF digit
</value>
<value name="FAX">
Fax (answering)
</value>
<value name="MODEM">
Modem (answering)
</value>
<value name="DIALTONE">
Dial tone
</value>
<value name="NUT">
UK Number Unobtainable tone
</value>
<value name="TIMEOUT">
Timeout reached before any positive detection
</value>
<value name="HANGUP">
Caller hung up before any positive detection
</value>
</variable>
</variablelist>
</description>
<see-also>
<ref type="application">WaitForTone</ref>
</see-also>
</application>
<function name="TONE_DETECT" language="en_US">
<synopsis>
Asynchronously detects a tone
</synopsis>
<syntax>
<parameter name="freq" required="true">
<para>Frequency of the tone to detect. To disable frequency
detection completely (e.g. for signal detection only),
specify 0 for the frequency.</para>
</parameter>
<parameter name="duration_ms" required="false">
<para>Minimum duration of tone, in ms. Default is 500ms.
Using a minimum duration under 50ms is unlikely to produce
accurate results.</para>
</parameter>
<parameter name="options">
<optionlist>
<option name="a">
<para>Match immediately on Special Information Tones, instead of or in addition
to a particular frequency.</para>
</option>
<option name="b">
<para>Match immediately on a busy signal, instead of or in addition to
a particular frequency.</para>
</option>
<option name="c">
<para>Match immediately on a dial tone, instead of or in addition to
a particular frequency.</para>
</option>
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<option name="d">
<para>Custom decibel threshold to use. Default is 16.</para>
</option>
<option name="g">
<para>Go to the specified context,exten,priority if tone is received on this channel.
Detection will not end automatically.</para>
</option>
<option name="h">
<para>Go to the specified context,exten,priority if tone is transmitted on this channel.
Detection will not end automatically.</para>
</option>
<option name="n">
<para>Number of times the tone should be detected (subject to the
provided timeout) before going to the destination provided in the <literal>g</literal>
or <literal>h</literal> option. Default is 1.</para>
</option>
<option name="r">
<para>Apply to received frames only. Default is both directions.</para>
</option>
<option name="s">
<para>Squelch tone.</para>
</option>
<option name="t">
<para>Apply to transmitted frames only. Default is both directions.</para>
</option>
<option name="x">
<para>Destroy the detector (stop detection).</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>The TONE_DETECT function detects a single-frequency tone and keeps
track of how many times the tone has been detected.</para>
<para>When reading this function (instead of writing), supply <literal>tx</literal>
to get the number of times a tone has been detected in the TX direction and
<literal>rx</literal> to get the number of times a tone has been detected in the
RX direction.</para>
<example title="intercept2600">
same => n,Set(TONE_DETECT(2600,1000,g(got-2600,s,1))=) ; detect 2600 Hz
same => n,Wait(15)
same => n,NoOp(${TONE_DETECT(rx)})
</example>
<example title="dropondialtone">
same => n,Set(TONE_DETECT(0,,bg(my-hangup,s,1))=) ; disconnect a call if we hear a busy signal
same => n,Goto(somewhere-else)
same => n(myhangup),Hangup()
</example>
<example title="removedetector">
same => n,Set(TONE_DETECT(0,,x)=) ; remove the detector from the channel
</example>
</description>
</function>
***/
struct detect_information {
struct ast_dsp *dsp;
struct ast_audiohook audiohook;
int freq1;
int freq2;
int duration;
int db;
char *gototx;
char *gotorx;
unsigned short int squelch;
unsigned short int tx;
unsigned short int rx;
int txcount;
int rxcount;
int hitsrequired;
};
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),
OPT_HITS_REQ = (1 << 8),
OPT_SIT = (1 << 9),
OPT_BUSY = (1 << 10),
OPT_DIALTONE = (1 << 11),
};
enum {
OPT_ARG_DECIBEL,
OPT_ARG_GOTO_RX,
OPT_ARG_GOTO_TX,
OPT_ARG_HITS_REQ,
/* note: this entry _MUST_ be the last one in the enum */
OPT_ARG_ARRAY_SIZE,
};
AST_APP_OPTIONS(td_opts, {
AST_APP_OPTION('a', OPT_SIT),
AST_APP_OPTION('b', OPT_BUSY),
AST_APP_OPTION('c', OPT_DIALTONE),
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
AST_APP_OPTION_ARG('d', OPT_DECIBEL, OPT_ARG_DECIBEL),
AST_APP_OPTION_ARG('g', OPT_GOTO_RX, OPT_ARG_GOTO_RX),
AST_APP_OPTION_ARG('h', OPT_GOTO_TX, OPT_ARG_GOTO_TX),
AST_APP_OPTION_ARG('n', OPT_HITS_REQ, OPT_ARG_HITS_REQ),
AST_APP_OPTION('s', OPT_SQUELCH),
AST_APP_OPTION('t', OPT_TX),
AST_APP_OPTION('r', OPT_RX),
AST_APP_OPTION('x', OPT_END_FILTER),
});
static void destroy_callback(void *data)
{
struct detect_information *di = data;
ast_dsp_free(di->dsp);
if (di->gotorx) {
ast_free(di->gotorx);
}
if (di->gototx) {
ast_free(di->gototx);
}
ast_audiohook_lock(&di->audiohook);
ast_audiohook_detach(&di->audiohook);
ast_audiohook_unlock(&di->audiohook);
ast_audiohook_destroy(&di->audiohook);
ast_free(di);
return;
}
static const struct ast_datastore_info detect_datastore = {
.type = "detect",
.destroy = destroy_callback
};
static int detect_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
{
struct ast_datastore *datastore = NULL;
struct detect_information *di = NULL;
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
return 0;
}
/* Grab datastore which contains our gain information */
if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
return 0;
}
di = datastore->data;
if (!frame || frame->frametype != AST_FRAME_VOICE) {
return 0;
}
if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? &di->rx : &di->tx)) {
return 0;
}
/* ast_dsp_process may free the frame and return a new one */
frame = ast_frdup(frame);
frame = ast_dsp_process(chan, di->dsp, frame);
if (frame->frametype == AST_FRAME_DTMF) {
char result = frame->subclass.integer;
if (result == 'q') {
int now;
if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
di->rxcount = di->rxcount + 1;
now = di->rxcount;
} else {
di->txcount = di->txcount + 1;
now = di->txcount;
}
ast_debug(1, "TONE_DETECT just got a hit (#%d in this direction, waiting for %d total)\n", now, di->hitsrequired);
if (now >= di->hitsrequired) {
if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) {
ast_async_parseable_goto(chan, di->gotorx);
} else if (di->gototx) {
ast_async_parseable_goto(chan, di->gototx);
}
}
}
}
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
if (di->signalfeatures && !match) { /* skip unless there are call progress/signal options */
int tstate, tcount;
tcount = ast_dsp_get_tcount(di->dsp);
tstate = ast_dsp_get_tstate(di->dsp);
if (tstate > 0) {
ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate);
switch (tstate) {
case DSP_TONE_STATE_DIALTONE:
if (di->signalfeatures & DSP_FEATURE_WAITDIALTONE) {
match = 1;
}
break;
case DSP_TONE_STATE_BUSY:
if (di->signalfeatures & DSP_PROGRESS_BUSY) {
match = 1;
}
break;
case DSP_TONE_STATE_SPECIAL3:
if (di->signalfeatures & DSP_PROGRESS_CONGESTION) {
match = 1;
}
break;
default: /* ignore */
break;
}
if (match) {
if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) {
ast_async_parseable_goto(chan, di->gotorx);
} else if (di->gototx) {
ast_async_parseable_goto(chan, di->gototx);
} else {
ast_debug(3, "Detected call progress signal, but don't know where to go\n");
}
}
}
}
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* this could be the duplicated frame or a new one, doesn't matter */
ast_frfree(frame);
return 0;
}
static int remove_detect(struct ast_channel *chan)
{
struct ast_datastore *datastore = NULL;
struct detect_information *data;
SCOPED_CHANNELLOCK(chan_lock, chan);
datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL);
if (!datastore) {
ast_log(AST_LOG_WARNING, "Cannot remove TONE_DETECT from %s: TONE_DETECT not currently enabled\n",
ast_channel_name(chan));
return -1;
}
data = datastore->data;
if (ast_audiohook_remove(chan, &data->audiohook)) {
ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT audiohook from channel %s\n", ast_channel_name(chan));
return -1;
}
if (ast_channel_datastore_remove(chan, datastore)) {
ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT datastore from channel %s\n",
ast_channel_name(chan));
return -1;
}
ast_datastore_free(datastore);
return 0;
}
static int freq_parser(char *freqs, int *freq1, int *freq2) {
char *f1, *f2, *f3;
if (ast_strlen_zero(freqs)) {
ast_log(LOG_ERROR, "No frequency specified\n");
return -1;
}
f3 = ast_strdupa(freqs);
f1 = strsep(&f3, "+");
f2 = strsep(&f3, "+");
if (!ast_strlen_zero(f3)) {
ast_log(LOG_WARNING, "Only up to 2 frequencies may be specified: %s\n", freqs);
return -1;
}
if (ast_str_to_int(f1, freq1)) {
ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f1);
return -1;
}
if (*freq1 < 0) {
ast_log(LOG_WARNING, "Sorry, no negative frequencies: %d\n", *freq1);
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
return -1;
}
if (!ast_strlen_zero(f2)) {
ast_log(LOG_WARNING, "Sorry, currently only 1 frequency is supported\n");
return -1;
/* not supported just yet, but possibly will be in the future */
if (ast_str_to_int(f2, freq2)) {
ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f2);
return -1;
}
if (*freq2 < 1) {
ast_log(LOG_WARNING, "Sorry, positive frequencies only: %d\n", *freq2);
return -1;
}
}
return 0;
}
static char* goto_parser(struct ast_channel *chan, char *loc) {
char *exten, *pri, *context, *parse;
char *dest;
int size;
parse = ast_strdupa(loc);
context = strsep(&parse, ",");
exten = strsep(&parse, ",");
pri = strsep(&parse, ",");
if (!exten) {
pri = context;
exten = NULL;
context = NULL;
} else if (!pri) {
pri = exten;
exten = context;
context = NULL;
}
ast_channel_lock(chan);
if (ast_strlen_zero(exten)) {
exten = ast_strdupa(ast_channel_exten(chan));
}
if (ast_strlen_zero(context)) {
context = ast_strdupa(ast_channel_context(chan));
}
ast_channel_unlock(chan);
/* size + 3: for 1 null terminator + 2 commas */
size = strlen(context) + strlen(exten) + strlen(pri) + 3;
dest = ast_malloc(size + 1);
if (!dest) {
ast_log(LOG_ERROR, "Failed to parse goto: %s,%s,%s\n", context, exten, pri);
return NULL;
}
snprintf(dest, size, "%s,%s,%s", context, exten, pri);
return dest;
}
static int detect_read(struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen)
{
struct ast_datastore *datastore = NULL;
struct detect_information *di = NULL;
if (!chan) {
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
return -1;
}
ast_channel_lock(chan);
if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
ast_channel_unlock(chan);
return -1; /* function not initiated yet, so nothing to read */
} else {
ast_channel_unlock(chan);
di = datastore->data;
}
if (strchr(data, 't')) {
snprintf(buffer, buflen, "%d", di->txcount);
} else if (strchr(data, 'r')) {
snprintf(buffer, buflen, "%d", di->rxcount);
} else {
ast_log(LOG_WARNING, "Invalid direction: %s\n", data);
}
return 0;
}
static int parse_signal_features(struct ast_flags *flags)
{
int features = 0;
if (ast_test_flag(flags, OPT_SIT)) {
features |= DSP_PROGRESS_CONGESTION;
}
if (ast_test_flag(flags, OPT_BUSY)) {
features |= DSP_PROGRESS_BUSY;
}
if (ast_test_flag(flags, OPT_DIALTONE)) {
features |= DSP_FEATURE_WAITDIALTONE;
}
return features;
}
static int detect_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
{
char *parse;
struct ast_datastore *datastore = NULL;
struct detect_information *di = NULL;
struct ast_flags flags = { 0 };
char *opt_args[OPT_ARG_ARRAY_SIZE];
struct ast_dsp *dsp;
int freq1 = 0, freq2 = 0, duration = 500, db = 16, squelch = 0, hitsrequired = 1;
int signalfeatures = 0;
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(freqs);
AST_APP_ARG(duration);
AST_APP_ARG(options);
);
if (!chan) {
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
return -1;
}
parse = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, parse);
if (ast_test_flag(&flags, OPT_END_FILTER)) {
return remove_detect(chan);
}
if (!ast_strlen_zero(args.options)) {
ast_app_parse_options(td_opts, &flags, opt_args, args.options);
}
if (freq_parser(args.freqs, &freq1, &freq2)) {
return -1;
}
if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) {
ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration);
return -1;
}
if (ast_test_flag(&flags, OPT_HITS_REQ) && !ast_strlen_zero(opt_args[OPT_ARG_HITS_REQ])) {
if ((ast_str_to_int(opt_args[OPT_ARG_HITS_REQ], &hitsrequired) || hitsrequired < 1)) {
ast_log(LOG_WARNING, "Invalid number hits required: %s\n", opt_args[OPT_ARG_HITS_REQ]);
return -1;
}
}
if (ast_test_flag(&flags, OPT_DECIBEL) && !ast_strlen_zero(opt_args[OPT_ARG_DECIBEL])) {
if ((ast_str_to_int(opt_args[OPT_ARG_DECIBEL], &db) || db < 1)) {
ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_ARG_DECIBEL]);
return -1;
}
}
signalfeatures = parse_signal_features(&flags);
ast_channel_lock(chan);
if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {
if (!(datastore = ast_datastore_alloc(&detect_datastore, NULL))) {
ast_channel_unlock(chan);
return 0;
}
if (!(di = ast_calloc(1, sizeof(*di)))) {
ast_datastore_free(datastore);
ast_channel_unlock(chan);
return 0;
}
ast_audiohook_init(&di->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Tone Detector", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
di->audiohook.manipulate_callback = detect_callback;
if (!(dsp = ast_dsp_new())) {
ast_datastore_free(datastore);
ast_channel_unlock(chan);
ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
return -1;
}
di->signalfeatures = signalfeatures; /* we're not including freq detect */
if (freq1 > 0) {
signalfeatures |= DSP_FEATURE_FREQ_DETECT;
ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
}
ast_dsp_set_features(dsp, signalfeatures);
di->dsp = dsp;
di->txcount = 0;
di->rxcount = 0;
ast_debug(1, "Keeping our ears open for %s Hz, %d db\n", args.freqs, db);
datastore->data = di;
ast_channel_datastore_add(chan, datastore);
ast_audiohook_attach(chan, &di->audiohook);
} else {
di = datastore->data;
dsp = di->dsp;
di->signalfeatures = signalfeatures; /* we're not including freq detect */
if (freq1 > 0) {
signalfeatures |= DSP_FEATURE_FREQ_DETECT;
ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
}
ast_dsp_set_features(dsp, signalfeatures);
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
}
di->duration = duration;
di->gotorx = NULL;
di->gototx = NULL;
/* resolve gotos now, in case a full context,exten,pri wasn't specified */
if (ast_test_flag(&flags, OPT_GOTO_RX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_RX])) {
di->gotorx = goto_parser(chan, opt_args[OPT_ARG_GOTO_RX]);
}
if (ast_test_flag(&flags, OPT_GOTO_TX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_TX])) {
di->gototx = goto_parser(chan, opt_args[OPT_ARG_GOTO_TX]);
}
di->db = db;
di->hitsrequired = hitsrequired;
di->squelch = ast_test_flag(&flags, OPT_SQUELCH);
di->tx = 1;
di->rx = 1;
if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_TX)) {
di->tx = 1;
di->rx = 0;
}
if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_RX)) {
di->rx = 1;
di->tx = 0;
}
ast_channel_unlock(chan);
return 0;
}
enum {
OPT_APP_DECIBEL = (1 << 0),
OPT_APP_SQUELCH = (1 << 1),
};
enum {
OPT_APP_ARG_DECIBEL,
/* note: this entry _MUST_ be the last one in the enum */
OPT_APP_ARG_ARRAY_SIZE,
};
AST_APP_OPTIONS(wait_exec_options, BEGIN_OPTIONS
AST_APP_OPTION_ARG('d', OPT_APP_DECIBEL, OPT_APP_ARG_DECIBEL),
AST_APP_OPTION('s', OPT_APP_SQUELCH),
END_OPTIONS);
static int wait_exec(struct ast_channel *chan, const char *data)
{
char *appdata;
struct ast_flags flags = {0};
char *opt_args[OPT_APP_ARG_ARRAY_SIZE];
double timeoutf = 0;
int freq1 = 0, freq2 = 0, timeout = 0, duration = 500, times = 1, db = 16, squelch = 0;
struct ast_frame *frame = NULL;
struct ast_dsp *dsp;
struct timeval start;
int remaining_time = 0;
int hits = 0;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(freqs);
AST_APP_ARG(duration);
AST_APP_ARG(timeout);
AST_APP_ARG(times);
AST_APP_ARG(options);
);
appdata = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, appdata);
if (!ast_strlen_zero(args.options)) {
ast_app_parse_options(wait_exec_options, &flags, opt_args, args.options);
}
if (freq_parser(args.freqs, &freq1, &freq2)) {
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
return -1;
}
if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeout < 0)) {
ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout);
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
return -1;
}
timeout = 1000 * timeoutf;
if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) {
ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration);
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
return -1;
}
if (!ast_strlen_zero(args.times) && (ast_str_to_int(args.times, ×) || times < 1)) {
ast_log(LOG_WARNING, "Invalid number of times: %s\n", args.times);
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
return -1;
}
if (ast_test_flag(&flags, OPT_APP_DECIBEL) && !ast_strlen_zero(opt_args[OPT_APP_ARG_DECIBEL])) {
if ((ast_str_to_int(opt_args[OPT_APP_ARG_DECIBEL], &db) || db < 1)) {
ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_APP_ARG_DECIBEL]);
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
return -1;
}
}
squelch = ast_test_flag(&flags, OPT_APP_SQUELCH);
if (!(dsp = ast_dsp_new())) {
ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR");
return -1;
}
ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT);
ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);
ast_debug(1, "Waiting for %s Hz, %d time(s), timeout %d ms, %d db\n", args.freqs, times, timeout, db);
start = ast_tvnow();
do {
if (timeout > 0) {
remaining_time = ast_remaining_ms(start, timeout);
if (remaining_time <= 0) {
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "TIMEOUT");
break;
}
}
if (ast_waitfor(chan, 1000) > 0) {
if (!(frame = ast_read(chan))) {
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP");
break;
} else if (frame->frametype == AST_FRAME_VOICE) {
frame = ast_dsp_process(chan, dsp, frame);
if (frame->frametype == AST_FRAME_DTMF) {
char result = frame->subclass.integer;
if (result == 'q') {
hits++;
ast_debug(1, "We just detected %s Hz (hit #%d)\n", args.freqs, hits);
if (hits >= times) {
ast_frfree(frame);
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "SUCCESS");
break;
}
}
}
}
ast_frfree(frame);
} else {
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP");
}
} while (timeout == 0 || remaining_time > 0);
ast_dsp_free(dsp);
return 0;
}
static char *waitapp = "WaitForTone";
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
static char *scanapp = "ToneScan";
static int scan_exec(struct ast_channel *chan, const char *data)
{
char *appdata;
double timeoutf = 0;
int timeout = 0;
struct ast_frame *frame = NULL, *frame2 = NULL;
struct ast_dsp *dsp = NULL, *dsp2 = NULL;
struct timeval start;
int remaining_time = 0;
int features, match = 0, fax = 0, voice = 0, threshold = 1;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(zone);
AST_APP_ARG(timeout);
AST_APP_ARG(threshold);
AST_APP_ARG(options);
);
appdata = ast_strdupa(data);
AST_STANDARD_APP_ARGS(args, appdata);
if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeout < 0)) {
ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout);
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
return -1;
}
if (!ast_strlen_zero(args.threshold) && (ast_str_to_int(args.threshold, &threshold) || threshold < 1)) {
ast_log(LOG_WARNING, "Invalid threshold: %s\n", args.threshold);
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
return -1;
}
timeout = 1000 * timeoutf;
if (!ast_strlen_zero(args.options) && strchr(args.options, 'f')) {
fax = 1;
}
if (!ast_strlen_zero(args.options) && strchr(args.options, 'v')) {
voice = 1;
}
if (!(dsp = ast_dsp_new())) {
ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
return -1;
}
if (!ast_strlen_zero(args.zone)) {
if (ast_dsp_set_call_progress_zone(dsp, args.zone)) {
ast_log(LOG_WARNING, "Invalid call progress zone: %s\n", args.zone);
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
ast_dsp_free(dsp);
return -1;
}
}
if (fax) {
if (!(dsp2 = ast_dsp_new())) {
ast_dsp_free(dsp);
ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");
return -1;
}
}
features = DSP_PROGRESS_RINGING; /* audible ringback tone */
features |= DSP_PROGRESS_BUSY; /* busy signal */
features |= DSP_PROGRESS_CONGESTION; /* SIT tones (not reorder!) */
features |= DSP_PROGRESS_TALK; /* voice. */
features |= DSP_FEATURE_WAITDIALTONE; /* dial tone */
features |= DSP_FEATURE_FREQ_DETECT; /* modem answer */
if (voice) {
features |= DSP_TONE_STATE_TALKING; /* voice */
}
ast_dsp_set_features(dsp, features);
/* all modems begin negotiating with Bell 103. An answering modem just sends mark tone, or 2225 Hz */
ast_dsp_set_freqmode(dsp, 2225, 400, 16, 0); /* this needs to be pretty short, or the progress tones code will thing this is voice */
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. */
ast_dsp_set_features(dsp2, DSP_FEATURE_FAX_DETECT); /* fax tone */
ast_dsp_set_faxmode(dsp2, DSP_FAXMODE_DETECT_CED); /* we only care about the answering side (CED), not originating (CNG) */
}
ast_debug(1, "Starting tone scan, timeout: %d ms, threshold: %d\n", timeout, threshold);
start = ast_tvnow();
do {
if (timeout > 0) {
remaining_time = ast_remaining_ms(start, timeout);
if (remaining_time <= 0) {
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "TIMEOUT");
break;
}
}
if (ast_waitfor(chan, 1000) > 0) {
if (!(frame = ast_read(chan))) {
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP");
break;
} else if (frame->frametype == AST_FRAME_VOICE) {
if (fax) {
frame2 = ast_frdup(frame);
}
frame = ast_dsp_process(chan, dsp, frame);
if (frame->frametype == AST_FRAME_DTMF) {
char result = frame->subclass.integer;
match = 1;
if (result == 'q') {
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "MODEM");
} else {
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DTMF");
}
} else if (fax) {
char result;
frame2 = ast_dsp_process(chan, dsp2, frame2);
result = frame->subclass.integer;
if (result == AST_FRAME_DTMF) {
if (result == 'e') {
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "FAX");
match = 1;
} else {
ast_debug(1, "Ignoring inactionable event\n"); /* shouldn't happen */
}
}
ast_frfree(frame2);
}
if (!match) {
int tstate, tcount;
tcount = ast_dsp_get_tcount(dsp);
tstate = ast_dsp_get_tstate(dsp);
if (tstate > 0) {
ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate);
if (tcount >= threshold) {
match = 1;
switch (tstate) {
case DSP_TONE_STATE_RINGING:
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "RINGING");
break;
case DSP_TONE_STATE_DIALTONE:
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DIALTONE");
break;
case DSP_TONE_STATE_TALKING:
/* even if we don't specify this feature, it's still checked, so we always need to handle it.
Even if we are looking for it, we need to wait a while or tones will be interpreted
as voice, because this will match first (and this should match last). */
if (voice && tcount > 15 && tcount >= threshold) {
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "VOICE");
} else {
match = 0;
}
break;
case DSP_TONE_STATE_BUSY:
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "BUSY");
break;
case DSP_TONE_STATE_SPECIAL3:
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "SIT");
break;
case DSP_TONE_STATE_HUNGUP: /* UK only */
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "NUT");
break;
default:
match = 0;
ast_debug(1, "Something else we weren't expecting? tstate: %d, #%d\n", tstate, tcount);
}
}
}
}
}
ast_frfree(frame);
} else {
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP");
}
} while (!match && (timeout == 0 || remaining_time > 0));
ast_dsp_free(dsp);
if (dsp2) {
ast_dsp_free(dsp2);
}
return 0;
}
static struct ast_custom_function detect_function = {
.name = "TONE_DETECT",
.read = detect_read,
.write = detect_write,
};
static int unload_module(void)