[Asterisk-code-review] chan_dahdi: Add POLARITY function. (asterisk[master])

N A asteriskteam at digium.com
Fri Apr 1 15:20:07 CDT 2022


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


Change subject: chan_dahdi: Add POLARITY function.
......................................................................

chan_dahdi: Add POLARITY function.

Adds a POLARITY function which can be used to
retrieve the current polarity of an FXS channel
as well as set the polarity of an FXS channel
to idle or reverse at any point during a call.

ASTERISK-30000 #close

Change-Id: If6f50998f723e4484bf68e2473f5cedfeaf9b8f1
---
M channels/chan_dahdi.c
A doc/CHANGES-staging/chan_dahdi_polarity.txt
2 files changed, 125 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/62/18362/1

diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index ad7a8e5..b072166 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -177,6 +177,35 @@
 			<para>This application will Accept the R2 call either with charge or no charge.</para>
 		</description>
 	</application>
+	<function name="POLARITY" language="en_US">
+		<synopsis>
+			Set or get the polarity of a DAHDI channel.
+		</synopsis>
+		<syntax />
+		<description>
+			<para>The POLARITY function can be used to set the polarity of a DAHDI channel.</para>
+			<para>Applies only to FXS channels (using FXO signalling) with supporting hardware.</para>
+			<para>The polarity can be set to the following numeric or named values:</para>
+			<enumlist>
+				<enum name="0" />
+				<enum name="idle" />
+				<enum name="1" />
+				<enum name="reverse" />
+			</enumlist>
+			<para>However, when read, the function will always return 0 or 1.</para>
+			<example title="Set idle polarity">
+			same => n,Set(POLARITY()=0)
+			</example>
+			<example title="Set reverse polarity">
+			same => n,NoOp(Current Polarity: ${POLARITY()})
+			same => n,Set(POLARITY()=reverse)
+			same => n,NoOp(New Polarity: ${POLARITY()})
+			</example>
+			<example title="Reverse the polarity from whatever it is currently">
+			same => n,Set(POLARITY()=${IF($[ "${POLARITY()}" = "1" ]?0:1)})
+			</example>
+		</description>
+	</function>
 	<info name="CHANNEL" language="en_US" tech="DAHDI">
 		<enumlist>
 			<enum name="dahdi_channel">
@@ -2641,6 +2670,93 @@
 	}
 }
 
+static int polarity_read(struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen)
+{
+	int res;
+	struct dahdi_params dahdip;
+	struct dahdi_pvt *pvt;
+
+	if (strcasecmp(ast_channel_tech(chan)->type, "DAHDI")) {
+		ast_log(LOG_WARNING, "%s is not a DAHDI channel\n", ast_channel_name(chan));
+		return -1;
+	}
+
+	memset(&dahdip, 0, sizeof(dahdip));
+	res = ioctl(ast_channel_fd(chan, 0), DAHDI_GET_PARAMS, &dahdip);
+
+	if (res) {
+		ast_log(LOG_WARNING, "Unable to get parameters of %s: %s\n", ast_channel_name(chan), strerror(errno));
+		return -1;
+	}
+	if (!(dahdip.sigtype & __DAHDI_SIG_FXO)) {
+		ast_log(LOG_WARNING, "%s is not an FXS Channel\n", ast_channel_name(chan));
+		return -1;
+	}
+
+	pvt = ast_channel_tech_pvt(chan);
+	if (!dahdi_analog_lib_handles(pvt->sig, 0, 0)) {
+		ast_log(LOG_WARNING, "Channel signalling is not analog");
+		return -1;
+	}
+
+	snprintf(buffer, buflen, "%d", pvt->polarity);
+
+	return 0;
+}
+
+static int polarity_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+	int res;
+	struct dahdi_params dahdip;
+	struct dahdi_pvt *pvt;
+	int polarity;
+
+	if (strcasecmp(ast_channel_tech(chan)->type, "DAHDI")) {
+		ast_log(LOG_WARNING, "%s is not a DAHDI channel\n", ast_channel_name(chan));
+		return -1;
+	}
+
+	memset(&dahdip, 0, sizeof(dahdip));
+	res = ioctl(ast_channel_fd(chan, 0), DAHDI_GET_PARAMS, &dahdip);
+
+	if (res) {
+		ast_log(LOG_WARNING, "Unable to get parameters of %s: %s\n", ast_channel_name(chan), strerror(errno));
+		return -1;
+	}
+	if (!(dahdip.sigtype & __DAHDI_SIG_FXO)) {
+		ast_log(LOG_WARNING, "%s is not an FXS Channel\n", ast_channel_name(chan));
+		return -1;
+	}
+
+	pvt = ast_channel_tech_pvt(chan);
+	if (!dahdi_analog_lib_handles(pvt->sig, 0, 0)) {
+		ast_log(LOG_WARNING, "Channel signalling is not analog");
+		return -1;
+	}
+
+	if (!strcasecmp(value, "idle")) {
+		polarity = 0;
+	} else if (!strcasecmp(value, "reverse")) {
+		polarity = 1;
+	} else {
+		polarity = atoi(value);
+	}
+
+	if (polarity != 0 && polarity != 1) {
+		ast_log(LOG_WARNING, "Invalid polarity: '%s'\n", value);
+		return -1;
+	}
+
+	my_set_polarity(pvt, polarity);
+	return 0;
+}
+
+static struct ast_custom_function polarity_function = {
+	.name = "POLARITY",
+	.write = polarity_write,
+	.read = polarity_read,
+};
+
 static int my_start(void *pvt)
 {
 	struct dahdi_pvt *p = pvt;
@@ -17542,6 +17658,8 @@
 	ast_unregister_application(dahdi_accept_r2_call_app);
 #endif
 
+	ast_custom_function_unregister(&polarity_function);
+
 	ast_cli_unregister_multiple(dahdi_cli, ARRAY_LEN(dahdi_cli));
 	ast_manager_unregister("DAHDIDialOffhook");
 	ast_manager_unregister("DAHDIHangup");
@@ -19727,6 +19845,8 @@
 	ast_register_application_xml(dahdi_accept_r2_call_app, dahdi_accept_r2_call_exec);
 #endif
 
+	ast_custom_function_register(&polarity_function);
+
 	ast_cli_register_multiple(dahdi_cli, ARRAY_LEN(dahdi_cli));
 	memset(round_robin, 0, sizeof(round_robin));
 	ast_manager_register_xml("DAHDITransfer", 0, action_transfer);
diff --git a/doc/CHANGES-staging/chan_dahdi_polarity.txt b/doc/CHANGES-staging/chan_dahdi_polarity.txt
new file mode 100644
index 0000000..365ab20
--- /dev/null
+++ b/doc/CHANGES-staging/chan_dahdi_polarity.txt
@@ -0,0 +1,5 @@
+Subject: chan_dahdi
+
+A POLARITY function is now available that allows
+getting or setting the polarity on a channel
+from the dialplan.

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: If6f50998f723e4484bf68e2473f5cedfeaf9b8f1
Gerrit-Change-Number: 18362
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/20220401/aede89c7/attachment-0001.html>


More information about the asterisk-code-review mailing list