<p>N A has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/17547">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_tonedetect: Add call progress tone detection<br><br>Makes basic call progress tone detection available<br>in a tech-agnostic manner with the addition of the<br>ToneScan application. This can determine if the channel<br>has encountered a busy signal, SIT tones, dial tone,<br>modem, fax machine, etc. A few basic async progress<br>tone detect options are also added to the TONE_DETECT<br>function.<br><br>ASTERISK-29720 #close<br><br>Change-Id: Ia02437e0450473031e294798b8cb421fb8f24e90<br>---<br>A doc/CHANGES-staging/tonescan.txt<br>M res/res_tonedetect.c<br>2 files changed, 364 insertions(+), 7 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/47/17547/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/doc/CHANGES-staging/tonescan.txt b/doc/CHANGES-staging/tonescan.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..cbed34f</span><br><span>--- /dev/null</span><br><span>+++ b/doc/CHANGES-staging/tonescan.txt</span><br><span>@@ -0,0 +1,6 @@</span><br><span style="color: hsl(120, 100%, 40%);">+Subject: ToneScan application</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+A new application, ToneScan, allows for</span><br><span style="color: hsl(120, 100%, 40%);">+synchronous detection of call progress</span><br><span style="color: hsl(120, 100%, 40%);">+signals such as dial tone, busy tone,</span><br><span style="color: hsl(120, 100%, 40%);">+Special Information Tones, and modems.</span><br><span>diff --git a/res/res_tonedetect.c b/res/res_tonedetect.c</span><br><span>index 1d5db83..b65a69b 100644</span><br><span>--- a/res/res_tonedetect.c</span><br><span>+++ b/res/res_tonedetect.c</span><br><span>@@ -93,13 +93,90 @@</span><br><span> <ref type="application">PlayTones</ref></span><br><span> </see-also></span><br><span> </application></span><br><span style="color: hsl(120, 100%, 40%);">+ <application name="ToneScan" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ Wait for period of time while scanning for call progress tones</span><br><span style="color: hsl(120, 100%, 40%);">+ </synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="zone" required="false"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Call progress zone. Default is the system default.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="timeout" required="false"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Maximum amount of time, in seconds, to wait for call progress</span><br><span style="color: hsl(120, 100%, 40%);">+ or signal tones. Default is forever.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="threshold" required="false"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>DSP threshold required for a match. A higher number will</span><br><span style="color: hsl(120, 100%, 40%);">+ require a longer match and may reduce false positives, at the</span><br><span style="color: hsl(120, 100%, 40%);">+ expense of false negatives. Default is 1.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="options" required="false"></span><br><span style="color: hsl(120, 100%, 40%);">+ <optionlist></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="f"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Enable fax machine detection. By default, this is disabled.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="v"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Enable voice detection. By default, this is disabled.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span style="color: hsl(120, 100%, 40%);">+ </optionlist></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ </syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <description></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Waits for a a distinguishable call progress tone and then exits.</span><br><span style="color: hsl(120, 100%, 40%);">+ Unlike a conventional scanner, this is not currently capable of</span><br><span style="color: hsl(120, 100%, 40%);">+ scanning for modem carriers.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <variablelist></span><br><span style="color: hsl(120, 100%, 40%);">+ <variable name="TONESCANSTATUS"></span><br><span style="color: hsl(120, 100%, 40%);">+ This indicates the result of the scan.</span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="RINGING"></span><br><span style="color: hsl(120, 100%, 40%);">+ Audible ringback tone</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="BUSY"></span><br><span style="color: hsl(120, 100%, 40%);">+ Busy tone</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="SIT"></span><br><span style="color: hsl(120, 100%, 40%);">+ Special Information Tones</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="VOICE"></span><br><span style="color: hsl(120, 100%, 40%);">+ Human voice detected</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="DTMF"></span><br><span style="color: hsl(120, 100%, 40%);">+ DTMF digit</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="FAX"></span><br><span style="color: hsl(120, 100%, 40%);">+ Fax (answering)</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="MODEM"></span><br><span style="color: hsl(120, 100%, 40%);">+ Modem (answering)</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="DIALTONE"></span><br><span style="color: hsl(120, 100%, 40%);">+ Dial tone</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="NUT"></span><br><span style="color: hsl(120, 100%, 40%);">+ UK Number Unobtainable tone</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="TIMEOUT"></span><br><span style="color: hsl(120, 100%, 40%);">+ Timeout reached before any positive detection</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ <value name="HANGUP"></span><br><span style="color: hsl(120, 100%, 40%);">+ Caller hung up before any positive detection</span><br><span style="color: hsl(120, 100%, 40%);">+ </value></span><br><span style="color: hsl(120, 100%, 40%);">+ </variable></span><br><span style="color: hsl(120, 100%, 40%);">+ </variablelist></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ <see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ <ref type="application">WaitForTone</ref></span><br><span style="color: hsl(120, 100%, 40%);">+ </see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ </application></span><br><span> <function name="TONE_DETECT" language="en_US"></span><br><span> <synopsis></span><br><span> Asynchronously detects a tone</span><br><span> </synopsis></span><br><span> <syntax></span><br><span> <parameter name="freq" required="true"></span><br><span style="color: hsl(0, 100%, 40%);">- <para>Frequency of the tone to detect.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Frequency of the tone to detect. To disable frequency</span><br><span style="color: hsl(120, 100%, 40%);">+ detection completely (e.g. for signal detection only),</span><br><span style="color: hsl(120, 100%, 40%);">+ specify 0 for the frequency.</para></span><br><span> </parameter></span><br><span> <parameter name="duration_ms" required="false"></span><br><span> <para>Minimum duration of tone, in ms. Default is 500ms.</span><br><span>@@ -108,6 +185,18 @@</span><br><span> </parameter></span><br><span> <parameter name="options"></span><br><span> <optionlist></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="a"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Match immediately on Special Information Tones, instead of or in addition</span><br><span style="color: hsl(120, 100%, 40%);">+ to a particular frequency.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="b"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Match immediately on a busy signal, instead of or in addition to</span><br><span style="color: hsl(120, 100%, 40%);">+ a particular frequency.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span style="color: hsl(120, 100%, 40%);">+ <option name="c"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Match immediately on a dial tone, instead of or in addition to</span><br><span style="color: hsl(120, 100%, 40%);">+ a particular frequency.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </option></span><br><span> <option name="d"></span><br><span> <para>Custom decibel threshold to use. Default is 16.</para></span><br><span> </option></span><br><span>@@ -147,10 +236,18 @@</span><br><span> <literal>rx</literal> to get the number of times a tone has been detected in the</span><br><span> RX direction.</para></span><br><span> <example title="intercept2600"></span><br><span style="color: hsl(0, 100%, 40%);">- same => n,Set(TONE_DETECT(2600,1000,g(got-2600,s,1))=)</span><br><span style="color: hsl(120, 100%, 40%);">+ same => n,Set(TONE_DETECT(2600,1000,g(got-2600,s,1))=) ; detect 2600 Hz</span><br><span> same => n,Wait(15)</span><br><span> same => n,NoOp(${TONE_DETECT(rx)})</span><br><span> </example></span><br><span style="color: hsl(120, 100%, 40%);">+ <example title="dropondialtone"></span><br><span style="color: hsl(120, 100%, 40%);">+ same => n,Set(TONE_DETECT(0,,bg(my-hangup,s,1))=) ; disconnect a call if we hear a busy signal</span><br><span style="color: hsl(120, 100%, 40%);">+ same => n,Goto(somewhere-else)</span><br><span style="color: hsl(120, 100%, 40%);">+ same => n(myhangup),Hangup()</span><br><span style="color: hsl(120, 100%, 40%);">+ </example></span><br><span style="color: hsl(120, 100%, 40%);">+ <example title="removedetector"></span><br><span style="color: hsl(120, 100%, 40%);">+ same => n,Set(TONE_DETECT(0,,x)=) ; remove the detector from the channel</span><br><span style="color: hsl(120, 100%, 40%);">+ </example></span><br><span> </description></span><br><span> </function></span><br><span> ***/</span><br><span>@@ -170,6 +267,7 @@</span><br><span> int txcount;</span><br><span> int rxcount;</span><br><span> int hitsrequired;</span><br><span style="color: hsl(120, 100%, 40%);">+ int signalfeatures;</span><br><span> };</span><br><span> </span><br><span> enum td_opts {</span><br><span>@@ -181,6 +279,9 @@</span><br><span> OPT_DECIBEL = (1 << 6),</span><br><span> OPT_SQUELCH = (1 << 7),</span><br><span> OPT_HITS_REQ = (1 << 8),</span><br><span style="color: hsl(120, 100%, 40%);">+ OPT_SIT = (1 << 9),</span><br><span style="color: hsl(120, 100%, 40%);">+ OPT_BUSY = (1 << 10),</span><br><span style="color: hsl(120, 100%, 40%);">+ OPT_DIALTONE = (1 << 11),</span><br><span> };</span><br><span> </span><br><span> enum {</span><br><span>@@ -193,6 +294,9 @@</span><br><span> };</span><br><span> </span><br><span> AST_APP_OPTIONS(td_opts, {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_OPTION('a', OPT_SIT),</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_OPTION('b', OPT_BUSY),</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_OPTION('c', OPT_DIALTONE),</span><br><span> AST_APP_OPTION_ARG('d', OPT_DECIBEL, OPT_ARG_DECIBEL),</span><br><span> AST_APP_OPTION_ARG('g', OPT_GOTO_RX, OPT_ARG_GOTO_RX),</span><br><span> AST_APP_OPTION_ARG('h', OPT_GOTO_TX, OPT_ARG_GOTO_TX),</span><br><span>@@ -230,6 +334,7 @@</span><br><span> {</span><br><span> struct ast_datastore *datastore = NULL;</span><br><span> struct detect_information *di = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ int match = 0;</span><br><span> </span><br><span> /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */</span><br><span> if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {</span><br><span>@@ -258,6 +363,7 @@</span><br><span> char result = frame->subclass.integer;</span><br><span> if (result == 'q') {</span><br><span> int now;</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span> if (direction == AST_AUDIOHOOK_DIRECTION_READ) {</span><br><span> di->rxcount = di->rxcount + 1;</span><br><span> now = di->rxcount;</span><br><span>@@ -275,6 +381,42 @@</span><br><span> }</span><br><span> }</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (di->signalfeatures && !match) { /* skip unless there are call progress/signal options */</span><br><span style="color: hsl(120, 100%, 40%);">+ int tstate, tcount;</span><br><span style="color: hsl(120, 100%, 40%);">+ tcount = ast_dsp_get_tcount(di->dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ tstate = ast_dsp_get_tstate(di->dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tstate > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate);</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (tstate) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_DIALTONE:</span><br><span style="color: hsl(120, 100%, 40%);">+ if (di->signalfeatures & DSP_FEATURE_WAITDIALTONE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_BUSY:</span><br><span style="color: hsl(120, 100%, 40%);">+ if (di->signalfeatures & DSP_PROGRESS_BUSY) {</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_SPECIAL3:</span><br><span style="color: hsl(120, 100%, 40%);">+ if (di->signalfeatures & DSP_PROGRESS_CONGESTION) {</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ default: /* ignore */</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (match) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_async_parseable_goto(chan, di->gotorx);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (di->gototx) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_async_parseable_goto(chan, di->gototx);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "Detected call progress signal, but don't know where to go\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> /* this could be the duplicated frame or a new one, doesn't matter */</span><br><span> ast_frfree(frame);</span><br><span> return 0;</span><br><span>@@ -326,8 +468,8 @@</span><br><span> ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f1);</span><br><span> return -1;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- if (*freq1 < 1) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_log(LOG_WARNING, "Sorry, positive frequencies only: %d\n", *freq1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (*freq1 < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Sorry, no negative frequencies: %d\n", *freq1);</span><br><span> return -1;</span><br><span> }</span><br><span> if (!ast_strlen_zero(f2)) {</span><br><span>@@ -413,6 +555,23 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static int parse_signal_features(struct ast_flags *flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int features = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(flags, OPT_SIT)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_PROGRESS_CONGESTION;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(flags, OPT_BUSY)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_PROGRESS_BUSY;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_test_flag(flags, OPT_DIALTONE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_FEATURE_WAITDIALTONE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return features;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int detect_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)</span><br><span> {</span><br><span> char *parse;</span><br><span>@@ -422,6 +581,7 @@</span><br><span> char *opt_args[OPT_ARG_ARRAY_SIZE];</span><br><span> struct ast_dsp *dsp;</span><br><span> int freq1 = 0, freq2 = 0, duration = 500, db = 16, squelch = 0, hitsrequired = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ int signalfeatures = 0;</span><br><span> </span><br><span> AST_DECLARE_APP_ARGS(args,</span><br><span> AST_APP_ARG(freqs);</span><br><span>@@ -461,6 +621,7 @@</span><br><span> return -1;</span><br><span> }</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+ signalfeatures = parse_signal_features(&flags);</span><br><span> </span><br><span> ast_channel_lock(chan);</span><br><span> if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) {</span><br><span>@@ -481,8 +642,12 @@</span><br><span> ast_log(LOG_WARNING, "Unable to allocate DSP!\n");</span><br><span> return -1;</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);</span><br><span style="color: hsl(120, 100%, 40%);">+ di->signalfeatures = signalfeatures; /* we're not including freq detect */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (freq1 > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ signalfeatures |= DSP_FEATURE_FREQ_DETECT;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_features(dsp, signalfeatures);</span><br><span> di->dsp = dsp;</span><br><span> di->txcount = 0;</span><br><span> di->rxcount = 0;</span><br><span>@@ -493,7 +658,12 @@</span><br><span> } else {</span><br><span> di = datastore->data;</span><br><span> dsp = di->dsp;</span><br><span style="color: hsl(0, 100%, 40%);">- ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);</span><br><span style="color: hsl(120, 100%, 40%);">+ di->signalfeatures = signalfeatures; /* we're not including freq detect */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (freq1 > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ signalfeatures |= DSP_FEATURE_FREQ_DETECT;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_features(dsp, signalfeatures);</span><br><span> }</span><br><span> di->duration = duration;</span><br><span> di->gotorx = NULL;</span><br><span>@@ -641,6 +811,185 @@</span><br><span> }</span><br><span> </span><br><span> static char *waitapp = "WaitForTone";</span><br><span style="color: hsl(120, 100%, 40%);">+static char *scanapp = "ToneScan";</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int scan_exec(struct ast_channel *chan, const char *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char *appdata;</span><br><span style="color: hsl(120, 100%, 40%);">+ double timeoutf = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int timeout = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_frame *frame = NULL, *frame2 = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_dsp *dsp = NULL, *dsp2 = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timeval start;</span><br><span style="color: hsl(120, 100%, 40%);">+ int remaining_time = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ int features, match = 0, fax = 0, voice = 0, threshold = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_DECLARE_APP_ARGS(args,</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(zone);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(threshold);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(options);</span><br><span style="color: hsl(120, 100%, 40%);">+ );</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ appdata = ast_strdupa(data);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STANDARD_APP_ARGS(args, appdata);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeout < 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.threshold) && (ast_str_to_int(args.threshold, &threshold) || threshold < 1)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid threshold: %s\n", args.threshold);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ timeout = 1000 * timeoutf;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.options) && strchr(args.options, 'f')) {</span><br><span style="color: hsl(120, 100%, 40%);">+ fax = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.options) && strchr(args.options, 'v')) {</span><br><span style="color: hsl(120, 100%, 40%);">+ voice = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(dsp = ast_dsp_new())) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Unable to allocate DSP!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_strlen_zero(args.zone)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_dsp_set_call_progress_zone(dsp, args.zone)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Invalid call progress zone: %s\n", args.zone);</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_free(dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fax) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(dsp2 = ast_dsp_new())) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_free(dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Unable to allocate DSP!\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ features = DSP_PROGRESS_RINGING; /* audible ringback tone */</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_PROGRESS_BUSY; /* busy signal */</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_PROGRESS_CONGESTION; /* SIT tones (not reorder!) */</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_PROGRESS_TALK; /* voice. */</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_FEATURE_WAITDIALTONE; /* dial tone */</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_FEATURE_FREQ_DETECT; /* modem answer */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (voice) {</span><br><span style="color: hsl(120, 100%, 40%);">+ features |= DSP_TONE_STATE_TALKING; /* voice */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_features(dsp, features);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* all modems begin negotiating with Bell 103. An answering modem just sends mark tone, or 2225 Hz */</span><br><span style="color: hsl(120, 100%, 40%);">+ 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 */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ 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. */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_features(dsp2, DSP_FEATURE_FAX_DETECT); /* fax tone */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_set_faxmode(dsp2, DSP_FAXMODE_DETECT_CED); /* we only care about the answering side (CED), not originating (CNG) */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Starting tone scan, timeout: %d ms, threshold: %d\n", timeout, threshold);</span><br><span style="color: hsl(120, 100%, 40%);">+ start = ast_tvnow();</span><br><span style="color: hsl(120, 100%, 40%);">+ do {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (timeout > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ remaining_time = ast_remaining_ms(start, timeout);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (remaining_time <= 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "TIMEOUT");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_waitfor(chan, 1000) > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!(frame = ast_read(chan))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (frame->frametype == AST_FRAME_VOICE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (fax) {</span><br><span style="color: hsl(120, 100%, 40%);">+ frame2 = ast_frdup(frame);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ frame = ast_dsp_process(chan, dsp, frame);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (frame->frametype == AST_FRAME_DTMF) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char result = frame->subclass.integer;</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (result == 'q') {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "MODEM");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DTMF");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (fax) {</span><br><span style="color: hsl(120, 100%, 40%);">+ char result;</span><br><span style="color: hsl(120, 100%, 40%);">+ frame2 = ast_dsp_process(chan, dsp2, frame2);</span><br><span style="color: hsl(120, 100%, 40%);">+ result = frame->subclass.integer;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (result == AST_FRAME_DTMF) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (result == 'e') {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "FAX");</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Ignoring inactionable event\n"); /* shouldn't happen */</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_frfree(frame2);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!match) {</span><br><span style="color: hsl(120, 100%, 40%);">+ int tstate, tcount;</span><br><span style="color: hsl(120, 100%, 40%);">+ tcount = ast_dsp_get_tcount(dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ tstate = ast_dsp_get_tstate(dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tstate > 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (tcount >= threshold) {</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (tstate) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_RINGING:</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "RINGING");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_DIALTONE:</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DIALTONE");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_TALKING:</span><br><span style="color: hsl(120, 100%, 40%);">+ /* even if we don't specify this feature, it's still checked, so we always need to handle it.</span><br><span style="color: hsl(120, 100%, 40%);">+ Even if we are looking for it, we need to wait a while or tones will be interpreted</span><br><span style="color: hsl(120, 100%, 40%);">+ as voice, because this will match first (and this should match last). */</span><br><span style="color: hsl(120, 100%, 40%);">+ if (voice && tcount > 15 && tcount >= threshold) {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "VOICE");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_BUSY:</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "BUSY");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_SPECIAL3:</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "SIT");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ case DSP_TONE_STATE_HUNGUP: /* UK only */</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "NUT");</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ default:</span><br><span style="color: hsl(120, 100%, 40%);">+ match = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "Something else we weren't expecting? tstate: %d, #%d\n", tstate, tcount);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_frfree(frame);</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP");</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } while (!match && (timeout == 0 || remaining_time > 0));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_free(dsp);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (dsp2) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_dsp_free(dsp2);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span> </span><br><span> static struct ast_custom_function detect_function = {</span><br><span> .name = "TONE_DETECT",</span><br><span>@@ -653,6 +1002,7 @@</span><br><span> int res;</span><br><span> </span><br><span> res = ast_unregister_application(waitapp);</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_unregister_application(scanapp);</span><br><span> res |= ast_custom_function_unregister(&detect_function);</span><br><span> </span><br><span> return res;</span><br><span>@@ -663,6 +1013,7 @@</span><br><span> int res;</span><br><span> </span><br><span> res = ast_register_application_xml(waitapp, wait_exec);</span><br><span style="color: hsl(120, 100%, 40%);">+ res |= ast_register_application_xml(scanapp, scan_exec);</span><br><span> res |= ast_custom_function_register(&detect_function);</span><br><span> </span><br><span> return res;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/17547">change 17547</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/17547"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ia02437e0450473031e294798b8cb421fb8f24e90 </div>
<div style="display:none"> Gerrit-Change-Number: 17547 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>