[Asterisk-code-review] app_sf: Add full tech-agnostic SF support (asterisk[master])

N A asteriskteam at digium.com
Mon Sep 13 14:07:16 CDT 2021


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16484 )


Change subject: app_sf: Add full tech-agnostic SF support
......................................................................

app_sf: Add full tech-agnostic SF support

Adds tech-agnostic support for the SF signaling protocol.
This includes SF sender and receiver applications and
Dial application integration.

ASTERISK-29496-p3 #do-not-close

Change-Id: I311bbdf596eff79d50dffdc9175fb3f25c99c9ac
---
M apps/app_dial.c
A apps/app_sf.c
A doc/CHANGES-staging/app_sf.txt
M include/asterisk/app.h
M main/app.c
5 files changed, 649 insertions(+), 8 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/84/16484/1

diff --git a/apps/app_dial.c b/apps/app_dial.c
index 7d55ad5..a61cfa0 100644
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -158,6 +158,8 @@
 					<argument name="progress" />
 					<argument name="mfprogress" />
 					<argument name="mfwink" />
+					<argument name="sfprogress" />
+					<argument name="sfwink" />
 					<para>Send the specified DTMF strings <emphasis>after</emphasis> the called
 					party has answered, but before the call gets bridged.  The
 					<replaceable>called</replaceable> DTMF string is sent to the called party, and the
@@ -170,6 +172,11 @@
 					If <replaceable>mfwink</replaceable> is specified, its MF is sent
 					to the called party immediately after receiving a <literal>WINK</literal> message.</para>
 					<para>See <literal>SendMF</literal> for valid digits.</para>
+					<para>If <replaceable>sfprogress</replaceable> is specified, its SF is sent
+					to the called party immediately after receiving a <literal>PROGRESS</literal> message.
+					If <replaceable>sfwink</replaceable> is specified, its SF is sent
+					to the called party immediately after receiving a <literal>WINK</literal> message.</para>
+					<para>See <literal>SendSF</literal> for valid digits.</para>
 				</option>
 				<option name="E">
 					<para>Enable echoing of sent MF or SF digits back to caller (e.g. "hearpulsing").
@@ -1214,7 +1221,7 @@
 	char *opt_args[],
 	struct privacy_args *pa,
 	const struct cause_args *num_in, int *result, char *dtmf_progress,
-	char *mf_progress, char *mf_wink,
+	char *mf_progress, char *mf_wink, char *sf_progress, char *sf_wink,
 	const int hearpulsing,
 	const int ignore_cc,
 	struct ast_party_id *forced_clid, struct ast_party_id *stored_clid,
@@ -1581,6 +1588,14 @@
 							ast_mf_stream(c, (hearpulsing ? NULL : in),
 							(hearpulsing ? in : NULL), mf_progress, 50, 55, 120, 65, 0);
 						}
+						if (!ast_strlen_zero(sf_progress)) {
+							ast_verb(3,
+								"Sending SF '%s' to %s as result of "
+								"receiving a PROGRESS message.\n",
+								sf_progress, (hearpulsing ? "parties" : "called party"));
+							ast_sf_stream(c, (hearpulsing ? NULL : in),
+							(hearpulsing ? in : NULL), sf_progress, 0, 0);
+						}
 						if (!ast_strlen_zero(dtmf_progress)) {
 							ast_verb(3,
 								"Sending DTMF '%s' to the called party as result of "
@@ -1603,6 +1618,14 @@
 							ast_mf_stream(c, (hearpulsing ? NULL : in),
 							(hearpulsing ? in : NULL), mf_wink, 50, 55, 120, 65, 0);
 						}
+						if (!ast_strlen_zero(sf_wink)) {
+							ast_verb(3,
+								"Sending SF '%s' to %s as result of "
+								"receiving a WINK message.\n",
+								sf_wink, (hearpulsing ? "parties" : "called party"));
+							ast_sf_stream(c, (hearpulsing ? NULL : in),
+							(hearpulsing ? in : NULL), sf_wink, 0, 0);
+						}
 					}
 					break;
 				case AST_CONTROL_VIDUPDATE:
@@ -2277,7 +2300,7 @@
 	struct ast_bridge_config config = { { 0, } };
 	struct timeval calldurationlimit = { 0, };
 	char *dtmfcalled = NULL, *dtmfcalling = NULL, *dtmf_progress = NULL;
-	char *mf_progress = NULL, *mf_wink = NULL;
+	char *mf_progress = NULL, *mf_wink = NULL, *sf_progress = NULL, *sf_wink = NULL;
 	struct privacy_args pa = {
 		.sentringing = 0,
 		.privdb_val = 0,
@@ -2412,11 +2435,13 @@
 	}
 
 	if (ast_test_flag64(&opts, OPT_SENDDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_SENDDTMF])) {
-		mf_wink = opt_args[OPT_ARG_SENDDTMF];
-		dtmfcalled = strsep(&mf_wink, ":");
-		dtmfcalling = strsep(&mf_wink, ":");
-		dtmf_progress = strsep(&mf_wink, ":");
-		mf_progress = strsep(&mf_wink, ":");
+		sf_wink = opt_args[OPT_ARG_SENDDTMF];
+		dtmfcalled = strsep(&sf_wink, ":");
+		dtmfcalling = strsep(&sf_wink, ":");
+		dtmf_progress = strsep(&sf_wink, ":");
+		mf_progress = strsep(&sf_wink, ":");
+		mf_wink = strsep(&sf_wink, ":");
+		sf_progress = strsep(&sf_wink, ":");
 	}
 
 	if (ast_test_flag64(&opts, OPT_DURATION_LIMIT) && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])) {
@@ -2893,7 +2918,7 @@
 	}
 
 	peer = wait_for_answer(chan, &out_chans, &to, peerflags, opt_args, &pa, &num, &result,
-		dtmf_progress, mf_progress, mf_wink, (ast_test_flag64(&opts, OPT_HEARPULSING) ? 1 : 0),
+		dtmf_progress, mf_progress, mf_wink, sf_progress, sf_wink, (ast_test_flag64(&opts, OPT_HEARPULSING) ? 1 : 0),
 		ignore_cc, &forced_clid, &stored_clid, &config);
 
 	if (!peer) {
diff --git a/apps/app_sf.c b/apps/app_sf.c
new file mode 100644
index 0000000..0c0b44f
--- /dev/null
+++ b/apps/app_sf.c
@@ -0,0 +1,428 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Naveen Albert
+ *
+ * Naveen Albert <asterisk at 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 SF sender and receiver applications
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup applications
+ */
+
+/*** MODULEINFO
+	<support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/file.h"
+#include "asterisk/pbx.h"
+#include "asterisk/channel.h"
+#include "asterisk/dsp.h"
+#include "asterisk/app.h"
+#include "asterisk/module.h"
+#include "asterisk/indications.h"
+#include "asterisk/conversions.h"
+
+/*** DOCUMENTATION
+	<application name="ReceiveSF" language="en_US">
+		<synopsis>
+			Detects SF digits on a channel and saves them to a variable.
+		</synopsis>
+		<syntax>
+			<parameter name="variable" required="true">
+				<para>The input digits will be stored in the given
+				<replaceable>variable</replaceable> name.</para>
+			</parameter>
+			<parameter name="digits" required="false">
+				<para>Maximum number of digits to read. Default is unlimited.</para>
+			</parameter>
+			<parameter name="timeout">
+				<para>The number of seconds to wait for all digits, if greater
+				than <literal>0</literal>. Can be floating point. Default
+				is no timeout.</para>
+			</parameter>
+			<parameter name="frequency">
+				<para>The frequency for which to detect pulsed digits.
+				Default is 2600 Hz.</para>
+			</parameter>
+			<parameter name="options">
+				<optionlist>
+					<option name="d">
+						<para>Delay audio by a frame to try to extra quelch.</para>
+					</option>
+					<option name="e">
+						<para>Allow receiving extra pulses 11 through 16.</para>
+					</option>
+					<option name="m">
+						<para>Mute conference.</para>
+					</option>
+					<option name="q">
+						<para>Quelch SF from in-band.</para>
+					</option>
+					<option name="r">
+						<para>"Radio" mode (relaxed SF).</para>
+					</option>
+				</optionlist>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Reads SF digits from the user in to the given
+			<replaceable>variable</replaceable>.</para>
+			<para>This application does not automatically answer the channel and
+			should be preceded with <literal>Answer</literal> or
+			<literal>Progress</literal> as needed.</para>
+			<variablelist>
+				<variable name="RECEIVESFSTATUS">
+					<para>This is the status of the read operation.</para>
+					<value name="START" />
+					<value name="ERROR" />
+					<value name="HANGUP" />
+					<value name="TIMEOUT" />
+				</variable>
+			</variablelist>
+		</description>
+		<see-also>
+			<ref type="application">ReceiveMF</ref>
+			<ref type="application">SendMF</ref>
+			<ref type="application">Read</ref>
+		</see-also>
+	</application>
+	<application name="SendSF" language="en_US">
+		<synopsis>
+			Sends arbitrary SF digits on the current or specified channel.
+		</synopsis>
+		<syntax>
+			<parameter name="digits" required="true">
+				<para>List of digits 0-9 to send; w for a half-second pause,
+				also f or F for a flash-hook if the channel supports flash-hook,
+				h or H for 250 ms of 2600 Hz,
+				and W for a wink if the channel supports wink.</para>
+			</parameter>
+			<parameter name="frequency" required="false">
+				<para>Frequency to use. (defaults to 2600 Hz).</para>
+			</parameter>
+			<parameter name="channel" required="false">
+				<para>Channel where digits will be played</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para>It will send all digits or terminate if it encounters an error.</para>
+		</description>
+		<see-also>
+			<ref type="application">SendDTMF</ref>
+			<ref type="application">SendMF</ref>
+			<ref type="application">ReceiveMF</ref>
+			<ref type="application">ReceiveSF</ref>
+		</see-also>
+	</application>
+ ***/
+
+enum read_option_flags {
+	OPT_DELAY = (1 << 0),
+	OPT_MUTE = (1 << 1),
+	OPT_QUELCH = (1 << 2),
+	OPT_RELAXED = (1 << 3),
+	OPT_EXTRAPULSES = (1 << 4),
+};
+
+AST_APP_OPTIONS(read_app_options, {
+	AST_APP_OPTION('d', OPT_DELAY),
+	AST_APP_OPTION('e', OPT_EXTRAPULSES),
+	AST_APP_OPTION('m', OPT_MUTE),
+	AST_APP_OPTION('q', OPT_QUELCH),
+	AST_APP_OPTION('r', OPT_RELAXED),
+});
+
+static const char *readsf_name = "ReceiveSF";
+static const char sendsf_name[] = "SendSF";
+
+static int read_sf_digits(struct ast_channel *chan, char *buf, int timeout, int maxdigits, int freq, int features, int extrapulses) {
+	/* Bell System Technical Journal 39 (Nov. 1960) */
+	#define SF_MIN_OFF 25
+	#define SF_ON 67
+	#define SF_BETWEEN 600
+	#define SF_MIN_DETECT 50
+
+	struct ast_dsp *dsp = NULL;
+	struct ast_frame *frame = NULL;
+	struct timeval start, pulsetimer, digittimer;
+	int remaining_time = timeout;
+	char *str = buf;
+	int hits = 0, digits_read = 0;
+	unsigned short int sf_on = 0;
+
+	if (!(dsp = ast_dsp_new())) {
+		ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
+		pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "ERROR");
+		return -1;
+	}
+	ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT);
+	/* tolerance is 46 to 76% make break at 8 to 12 pps */
+	ast_dsp_set_freqmode(dsp, freq, SF_MIN_DETECT, 16, 0);
+
+	start = ast_tvnow();
+	*str = 0; /* start with empty output buffer */
+
+	while (timeout == 0 || remaining_time > 0) {
+		if (timeout > 0) {
+			remaining_time = ast_remaining_ms(start, timeout);
+			if (remaining_time <= 0) {
+				pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "TIMEOUT");
+				break;
+			}
+		}
+		if (ast_waitfor(chan, 1000) > 0) {
+			frame = ast_read(chan);
+			if (!frame) {
+				ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan));
+				pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "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') {
+						sf_on = 1;
+						pulsetimer = ast_tvnow(); /* reset the pulse timer */
+						/* now, we need at least a 33ms pause to register the pulse */
+					}
+				} else {
+					if (sf_on) {
+						int timeleft = ast_remaining_ms(pulsetimer, SF_MIN_OFF);
+						if (timeleft <= 0) {
+							sf_on = 0;
+							/* The pulse needs to end no more than 30ms after we detected it */
+							if (timeleft > -30) {
+								hits++;
+								digittimer = ast_tvnow(); /* reset the digit timer */
+								ast_debug(5, "Detected SF pulse (pulse #%d)\n", hits);
+								if (!(dsp = ast_dsp_new())) {
+									ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
+									pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "ERROR");
+									return -1;
+								}
+								ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT);
+								ast_dsp_set_freqmode(dsp, freq, SF_MIN_DETECT, 16, 0);
+							} else {
+								ast_debug(5, "SF noise, ignoring, time elapsed was %d ms\n", timeleft);
+							}
+						}
+					} else if (hits > 0 && ast_remaining_ms(digittimer, SF_BETWEEN) <= 0) {
+						/* has the digit finished? */
+						ast_debug(2, "Received SF digit: %d\n", hits);
+						digits_read++;
+						if (hits > 10) {
+							if (extrapulses) {
+								/* dahdi-base.c translates 11 to * and 12 to # */
+								if (hits == 11) {
+									hits = '*';
+								} else if (hits == 12) {
+									hits = '#';
+								} else if (hits == 13) {
+									hits = 'D';
+								} else if (hits == 14) {
+									hits = 'C';
+								} else if (hits == 15) {
+									hits = 'B';
+								} else if (hits == 16) {
+									hits = 'A';
+								} else {
+									ast_debug(3, "Got %d SF pulses, is someone playing with the phone?\n", hits);
+									hits = 'A';
+								}
+								*str++ = hits;
+							} else {
+								ast_debug(2, "Got more than 10 pulses, truncating to 10\n");
+								hits = 0; /* 10 dial pulses = digit 0 */
+								*str++ = hits + '0';
+							}
+						} else {
+							if (hits == 10) {
+								hits = 0; /* 10 dial pulses = digit 0 */
+							}
+							*str++ = hits + '0';
+						}
+						*str = 0;
+						hits = 0;
+						if (maxdigits > 0 && digits_read >= maxdigits) {
+							pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "START");
+							break;
+						}
+					}
+				}
+			}
+		} else {
+			pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "HANGUP");
+		}
+	}
+	if (dsp) {
+		ast_dsp_free(dsp);
+	}
+	ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, remaining_time: %d }\n", ast_channel_name(chan), timeout, remaining_time);
+	return 0;
+}
+
+static int read_sf_exec(struct ast_channel *chan, const char *data)
+{
+	char tmp[256] = "";
+	double tosec;
+	struct ast_flags flags = {0};
+	char *argcopy = NULL;
+	int features = 0, digits = 0, to = 0, freq = 2600;
+
+	AST_DECLARE_APP_ARGS(arglist,
+		AST_APP_ARG(variable);
+		AST_APP_ARG(digits);
+		AST_APP_ARG(timeout);
+		AST_APP_ARG(freq);
+		AST_APP_ARG(options);
+	);
+
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "ReceiveSF requires an argument (variable)\n");
+		return -1;
+	}
+
+	argcopy = ast_strdupa(data);
+
+	AST_STANDARD_APP_ARGS(arglist, argcopy);
+
+	if (!ast_strlen_zero(arglist.options)) {
+		ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
+	}
+
+	if (!ast_strlen_zero(arglist.timeout)) {
+		tosec = atof(arglist.timeout);
+		if (tosec <= 0) {
+			to = 0;
+		} else {
+			to = tosec * 1000.0;
+		}
+	}
+
+	if (!ast_strlen_zero(arglist.digits) && (ast_str_to_int(arglist.digits, &digits) || digits <= 0)) {
+		ast_log(LOG_WARNING, "Invalid number of digits: %s\n", arglist.digits);
+		return -1;
+	}
+
+	if (!ast_strlen_zero(arglist.freq) && (ast_str_to_int(arglist.freq, &freq) || freq <= 0)) {
+		ast_log(LOG_WARNING, "Invalid freq: %s\n", arglist.freq);
+		return -1;
+	}
+
+	if (ast_strlen_zero(arglist.variable)) {
+		ast_log(LOG_WARNING, "Invalid! Usage: ReceiveSF(variable[,timeout][,option])\n");
+		return -1;
+	}
+
+	if (ast_test_flag(&flags, OPT_DELAY)) {
+		features |= DSP_DIGITMODE_MUTEMAX;
+	}
+
+	if (ast_test_flag(&flags, OPT_MUTE)) {
+		features |= DSP_DIGITMODE_MUTECONF;
+	}
+
+	if (!ast_test_flag(&flags, OPT_QUELCH)) {
+		features |= DSP_DIGITMODE_NOQUELCH;
+	}
+
+	if (ast_test_flag(&flags, OPT_RELAXED)) {
+		features |= DSP_DIGITMODE_RELAXDTMF;
+	}
+
+	read_sf_digits(chan, tmp, to, digits, freq, features, ast_test_flag(&flags, OPT_EXTRAPULSES));
+	pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
+	if (!ast_strlen_zero(tmp)) {
+		ast_verb(3, "MF digits received: '%s'\n", tmp);
+	} else {
+		ast_verb(3, "No MF digits received.\n");
+	}
+	return 0;
+}
+
+static int sendsf_exec(struct ast_channel *chan, const char *vdata)
+{
+	int res;
+	char *data;
+	int frequency = 2600;
+	struct ast_channel *chan_found = NULL;
+	struct ast_channel *chan_dest = chan;
+	struct ast_channel *chan_autoservice = NULL;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(digits);
+		AST_APP_ARG(frequency);
+		AST_APP_ARG(channel);
+	);
+
+	if (ast_strlen_zero(vdata)) {
+		ast_log(LOG_WARNING, "SendSF requires an argument\n");
+		return 0;
+	}
+
+	data = ast_strdupa(vdata);
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (ast_strlen_zero(args.digits)) {
+		ast_log(LOG_WARNING, "The digits argument is required (0-9,wf)\n");
+		return 0;
+	}
+	if (!ast_strlen_zero(args.frequency) && (ast_str_to_int(args.frequency, &frequency) || frequency < 1)) {
+		ast_log(LOG_WARNING, "Invalid duration: %s\n", args.frequency);
+		return -1;
+	}
+	if (!ast_strlen_zero(args.channel)) {
+		chan_found = ast_channel_get_by_name(args.channel);
+		if (!chan_found) {
+			ast_log(LOG_WARNING, "No such channel: %s\n", args.channel);
+			return 0;
+		}
+		chan_dest = chan_found;
+		if (chan_found != chan) {
+			chan_autoservice = chan;
+		}
+	}
+	res = ast_sf_stream(chan_dest, chan_autoservice, NULL, args.digits, frequency, 0);
+	ast_channel_cleanup(chan_found);
+
+	return chan_autoservice ? 0 : res;
+}
+
+static int unload_module(void)
+{
+	int res;
+
+	res = ast_unregister_application(readsf_name);
+	res |= ast_unregister_application(sendsf_name);
+
+	return res;
+}
+
+static int load_module(void)
+{
+	int res;
+
+	res = ast_register_application_xml(readsf_name, read_sf_exec);
+	res |= ast_register_application_xml(sendsf_name, sendsf_exec);
+
+	return res;
+}
+
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "SF Sender and Receiver Applications");
diff --git a/doc/CHANGES-staging/app_sf.txt b/doc/CHANGES-staging/app_sf.txt
new file mode 100644
index 0000000..347ba5d
--- /dev/null
+++ b/doc/CHANGES-staging/app_sf.txt
@@ -0,0 +1,5 @@
+Subject: app_sf
+
+Adds tech-agnostic support for SF signaling through
+SF sender and receiver applications, along with Dial
+integration.
diff --git a/include/asterisk/app.h b/include/asterisk/app.h
index ec34f28..5a8ca86 100644
--- a/include/asterisk/app.h
+++ b/include/asterisk/app.h
@@ -923,6 +923,34 @@
 void ast_unreplace_sigchld(void);
 
 /*!
+ * \brief Send a string of SF digits to a channel
+ *
+ * \param chan    The channel that will receive the SF digits
+ * \param peer    (optional) Peer channel that will be autoserviced while the
+ *                primary channel is receiving SF
+ * \param chan2   A second channel that will simultaneously receive SF digits.
+ *                This option may only be used if is_external is 0.
+ * \param digits  This is a string of characters representing the SF digits
+ *                to be sent to the channel.  Valid characters are
+ *                "0123456789".  Note: You can pass arguments 'f' or
+ *                'F', if you want to Flash the channel (if supported by the
+ *                channel), or 'w' or 'W' to add a wink (if supported by the
+ *                channel).
+ * \param between This is the number of milliseconds to wait in between each
+ *                SF digit.  If zero milliseconds is specified, then the
+ *                default value of 50 will be used.
+ * \param duration This is the duration that each numeric SF digit should have.
+ *                 Default value is 55.
+ * \param is_external 1 if called by a thread that is not the channel's media
+ *                handler thread, 0 if called by the channel's media handler
+ *                thread.
+ *
+ * \retval 0 on success.
+ * \retval -1 on failure or a channel hung up.
+ */
+int ast_sf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits, int frequency, int is_external);
+
+/*!
  * \brief Send a string of MF digits to a channel
  *
  * \param chan    The channel that will receive the MF digits.
diff --git a/main/app.c b/main/app.c
index 30e83c0..0774ee6 100644
--- a/main/app.c
+++ b/main/app.c
@@ -833,6 +833,145 @@
 	return 0;
 }
 
+static int sf_stream(struct ast_channel *chan, struct ast_channel *chan2, const char *digits, int frequency, int is_external)
+{
+	/* Bell System Technical Journal 39 (Nov. 1960) */
+	#define SF_ON 67
+	#define SF_OFF 33
+	#define SF_BETWEEN 600
+
+	const char *ptr;
+	int res;
+	struct ast_silence_generator *silgen = NULL, *silgen2 = NULL;
+	char *freq;
+	int (*my_sleep)(struct ast_channel *chan, int ms);
+
+	if (is_external) {
+		my_sleep = external_sleep;
+	} else {
+		my_sleep = ast_safe_sleep;
+	}
+
+	/* Need a quiet time before sending digits. */
+	if (ast_opt_transmit_silence) {
+		silgen = ast_channel_start_silence_generator(chan);
+		if (chan2) {
+			silgen2 = ast_channel_start_silence_generator(chan2);
+		}
+	}
+	if (chan2) {
+		ast_autoservice_start(chan2);
+	}
+	res = my_sleep(chan, 100);
+	if (chan2) {
+		ast_autoservice_stop(chan2);
+	}
+	if (res) {
+		goto sf_stream_cleanup;
+	}
+
+	freq = ast_malloc(32);
+	/* pauses need to send audio, so send 0 Hz */
+	snprintf(freq, 31, "%d/%d,%d/%d", frequency, SF_ON, 0, SF_OFF);
+
+	for (ptr = digits; *ptr; ptr++) {
+		if (*ptr == 'w') {
+			/* 'w' -- wait half a second */
+			res = my_sleep(chan, 500);
+			if (res) {
+				break;
+			}
+		} else if (*ptr == 'h' || *ptr == 'H') {
+			/* 'h' -- 2600 Hz for half a second, but
+				only to far end of trunk, not near end */
+			ast_playtones_start(chan, 0, "2600", 0);
+			if (chan2) {
+				ast_playtones_start(chan2, 0, "0", 0);
+				ast_autoservice_start(chan2);
+			}
+			res = my_sleep(chan, 250);
+			ast_senddigit_mf_end(chan);
+			if (chan2) {
+				ast_autoservice_stop(chan2);
+				ast_senddigit_mf_end(chan2);
+			}
+			if (res) {
+				break;
+			}
+		} else if (strchr("0123456789*#ABCDabcdwWfF", *ptr)) {
+			if (*ptr == 'f' || *ptr == 'F') {
+				/* ignore return values if not supported by channel */
+				ast_indicate(chan, AST_CONTROL_FLASH);
+			} else if (*ptr == 'W') {
+				/* ignore return values if not supported by channel */
+				ast_indicate(chan, AST_CONTROL_WINK);
+			} else {
+				/* Character represents valid SF */
+				int beeps;
+				if (*ptr == '*') {
+					beeps = 11;
+				} else if (*ptr == '#') {
+					beeps = 12;
+				} else if (*ptr == 'D') {
+					beeps = 13;
+				} else if (*ptr == 'C') {
+					beeps = 14;
+				} else if (*ptr == 'B') {
+					beeps = 15;
+				} else if (*ptr == 'A') {
+					beeps = 16;
+				} else {
+					beeps = (*ptr == '0') ? 10 : *ptr - '0';
+				}
+				while (beeps-- > 0) {
+					ast_playtones_start(chan, 0, freq, 0);
+					if (chan2) {
+						ast_playtones_start(chan2, 0, freq, 0);
+						ast_autoservice_start(chan2);
+					}
+					res = my_sleep(chan, SF_ON + SF_OFF);
+					ast_senddigit_mf_end(chan);
+					if (chan2) {
+						ast_autoservice_stop(chan2);
+						ast_senddigit_mf_end(chan2);
+					}
+					if (res) {
+						break;
+					}
+				}
+			}
+			/* pause between digits */
+			ast_playtones_start(chan, 0, "0", 0);
+			if (chan2) {
+				ast_playtones_start(chan2, 0, "0", 0);
+				ast_autoservice_start(chan2);
+			}
+			res = my_sleep(chan, SF_BETWEEN);
+			if (chan2) {
+				ast_autoservice_stop(chan2);
+				ast_senddigit_mf_end(chan2);
+			}
+			ast_senddigit_mf_end(chan);
+			if (res) {
+				break;
+			}
+		} else {
+			ast_log(LOG_WARNING, "Illegal SF character '%c' in string. (0-9A-DwWfFhH allowed)\n", *ptr);
+		}
+	}
+	ast_free(freq);
+
+sf_stream_cleanup:
+	if (silgen) {
+		ast_channel_stop_silence_generator(chan, silgen);
+	}
+	if (silgen2) {
+		ast_channel_stop_silence_generator(chan2, silgen2);
+	}
+
+	return res;
+}
+
 static int mf_stream(struct ast_channel *chan, struct ast_channel *chan2, const char *digits, int between, unsigned int duration,
 	unsigned int durationkp, unsigned int durationst, int is_external)
 {
@@ -1010,6 +1149,22 @@
 	return res;
 }
 
+int ast_sf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits, int frequency, int is_external)
+{
+	int res;
+	if (frequency <= 0) {
+		frequency = 2600;
+	}
+	if (!is_external && !chan2 && peer && ast_autoservice_start(peer)) {
+		return -1;
+	}
+	res = sf_stream(chan, chan2, digits, frequency, is_external);
+	if (!is_external && !chan2 && peer && ast_autoservice_stop(peer)) {
+		res = -1;
+	}
+	return res;
+}
+
 int ast_mf_stream(struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *chan2, const char *digits,
 	int between, unsigned int duration, unsigned int durationkp, unsigned int durationst, int is_external)
 {

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16484
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I311bbdf596eff79d50dffdc9175fb3f25c99c9ac
Gerrit-Change-Number: 16484
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210913/ac3e02a8/attachment-0001.html>


More information about the asterisk-code-review mailing list