[Asterisk-code-review] func_frameintercept: Add FRAME_INTERCEPT function (asterisk[master])

N A asteriskteam at digium.com
Wed Dec 29 11:02:43 CST 2021


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


Change subject: func_frameintercept: Add FRAME_INTERCEPT function
......................................................................

func_frameintercept: Add FRAME_INTERCEPT function

Adds a function that can be used to intercept
certain control frames on a channel.

This function contains a superset of the functionality
in the existing HOLD_INTERCEPT function.

ASTERISK-29825 #close

Change-Id: I5ad83b95a4dce3b64b408a1497b0cd63051eb274
---
M configs/samples/stasis.conf.sample
A doc/CHANGES-staging/func_frameintercept.txt
A funcs/func_frameintercept.c
M include/asterisk/stasis_channels.h
M main/stasis.c
M main/stasis_channels.c
6 files changed, 298 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/16/17716/1

diff --git a/configs/samples/stasis.conf.sample b/configs/samples/stasis.conf.sample
index 6fadc74..b62d1c6 100644
--- a/configs/samples/stasis.conf.sample
+++ b/configs/samples/stasis.conf.sample
@@ -54,6 +54,7 @@
 ; decline=ast_channel_dtmf_begin_type
 ; decline=ast_channel_dtmf_end_type
 ; decline=ast_channel_flash_type
+; decline=ast_channel_wink_type
 ; decline=ast_channel_hold_type
 ; decline=ast_channel_unhold_type
 ; decline=ast_channel_chanspy_start_type
diff --git a/doc/CHANGES-staging/func_frameintercept.txt b/doc/CHANGES-staging/func_frameintercept.txt
new file mode 100644
index 0000000..babbe06
--- /dev/null
+++ b/doc/CHANGES-staging/func_frameintercept.txt
@@ -0,0 +1,7 @@
+Subject: func_frameintercept
+
+Adds the FRAME_INTERCEPT function, which can be used
+to intercept certain control frames on a channel.
+
+This function contains a superset of the functionality
+contained within the existing HOLD_INTERCEPT function.
diff --git a/funcs/func_frameintercept.c b/funcs/func_frameintercept.c
new file mode 100644
index 0000000..b4c2344
--- /dev/null
+++ b/funcs/func_frameintercept.c
@@ -0,0 +1,279 @@
+/*
+ * 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 Function that intercepts specified frames on channels
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+	<support_level>extended</support_level>
+ ***/
+
+/*! \todo deprecate/remove func_holdintercept in 21, since this module is a superset of its functionality */
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/framehook.h"
+#include "asterisk/stasis.h"
+#include "asterisk/stasis_channels.h"
+
+/*** DOCUMENTATION
+	<function name="FRAME_INTERCEPT" language="en_US">
+		<synopsis>
+			Intercepts specific frame types in the TX or RX direction on a channel and raises an event
+		</synopsis>
+		<syntax>
+			<parameter name="direction" required="true">
+				<para>List of frame types to be intercepted for the specified direction. Direction can be <literal>TX</literal> or <literal>RX</literal>. The <literal>TX</literal> direction will intercept to a channel, and the <literal>RX</literal> direction will intercept frames from a channel.</para>
+				<para>Subsequent calls to this function will replace previous settings, allowing certain frames to be intercepted only temporarily, for instance.</para>
+				<para>Below are the different types of frames that can be intercepted.</para>
+				<para>The following CONTROL frames can be intercepted:</para>
+				<enumlist>
+					<enum name = "FLASH" />
+					<enum name = "WINK" />
+					<enum name = "HOLD" />
+					<enum name = "UNHOLD" />
+				</enumlist>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Examples:</para>
+			<example title="Raise an event when a hold/unhold event is received on a channel from the called party">
+			same => n,Set(FRAME_INTERCEPT(TX)=HOLD,UNHOLD)
+			</example>
+			<example title="Raise an event when a wink is received on a channel from the called party">
+			same => n,Set(FRAME_INTERCEPT(TX)=WINK)
+			</example>
+			<example title="Disable frame interception">
+			same => n,Set(FRAME_INTERCEPT(TX)=)
+			</example>
+		</description>
+	</function>
+ ***/
+
+static struct {
+	int type;
+	const char *str;
+} controlframetype2str[] = {
+	{ AST_CONTROL_FLASH,   ",FLASH," },
+	{ AST_CONTROL_WINK,   ",WINK," },
+	{ AST_CONTROL_HOLD,   ",HOLD," },
+	{ AST_CONTROL_UNHOLD,   ",UNHOLD," },
+};
+
+enum direction {
+    TX = 0,
+    RX,
+};
+
+struct frame_intercept_data {
+	enum direction list_type;
+	int controlvalues[ARRAY_LEN(controlframetype2str)];
+};
+
+static void datastore_destroy_cb(void *data) {
+	ast_free(data);
+}
+
+static const struct ast_datastore_info frame_intercept_datastore = {
+	.type = "frame_intercept",
+	.destroy = datastore_destroy_cb
+};
+
+static void hook_destroy_cb(void *framedata)
+{
+	ast_free(framedata);
+}
+
+/*! \brief Frame hook that is called to intercept frames */
+static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
+{
+	int i, frame_type;
+	int intercept = 0;
+	struct frame_intercept_data *framedata = data;
+	if (!frame) {
+		return frame;
+	}
+	if (frame->frametype != AST_FRAME_CONTROL) {
+		return frame;
+	}
+	if (!((event == AST_FRAMEHOOK_EVENT_WRITE && framedata->list_type == TX) ||
+		(event == AST_FRAMEHOOK_EVENT_READ && framedata->list_type == RX))) {
+		return frame;
+	}
+
+	if (frame->frametype == AST_FRAME_CONTROL) {
+		for (i = 0; i < ARRAY_LEN(controlframetype2str); i++) {
+			if (frame->subclass.integer == controlframetype2str[i].type) {
+				if (framedata->controlvalues[i]) {
+					intercept = 1;
+					frame_type = frame->subclass.integer;
+				}
+				break;
+			}
+		}
+	}
+
+	if (intercept) {
+		struct stasis_message_type *message = NULL;
+		char *intercepted = NULL;
+
+		if (frame_type) {
+			switch (frame_type) {
+			case AST_CONTROL_FLASH:
+				message = ast_channel_flash_type();
+				intercepted = "flash";
+				break;
+			case AST_CONTROL_WINK:
+				message = ast_channel_wink_type();
+				intercepted = "wink";
+				break;
+			case AST_CONTROL_HOLD:
+				message = ast_channel_hold_type();
+				intercepted = "hold";
+				break;
+			case AST_CONTROL_UNHOLD:
+				message = ast_channel_unhold_type();
+				intercepted = "unhold";
+				break;
+			default:
+				ast_log(LOG_WARNING, "Frame subclass type %d not supported\n", frame_type); /* shouldn't happen */
+			}
+		}
+
+		if (message) {
+			/* Munch munch */
+			ast_frfree(frame);
+			frame = &ast_null_frame;
+			ast_channel_publish_cached_blob(chan, message, NULL);
+			ast_verb(3, "Intercepted a %s frame on channel %s\n", intercepted, ast_channel_name(chan));
+		}
+	}
+	return frame;
+}
+
+/*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
+static int frame_intercept_framehook_consume(void *data, enum ast_frame_type type)
+{
+	return (type == AST_FRAME_CONTROL ? 1 : 0);
+}
+
+/*! \internal \brief FRAME_INTERCEPT write function callback */
+static int frame_intercept_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+	char *buffer;
+	int intercepting = 0;
+	struct frame_intercept_data *framedata;
+	struct ast_datastore *datastore = NULL;
+	struct ast_framehook_interface interface = {
+		.version = AST_FRAMEHOOK_INTERFACE_VERSION,
+		.event_cb = hook_event_cb,
+		.destroy_cb = hook_destroy_cb,
+		.consume_cb = frame_intercept_framehook_consume,
+		.disable_inheritance = 1,
+	};
+	int i = 0;
+
+	if (!(framedata = ast_calloc(1, sizeof(*framedata)))) {
+		return 0;
+	}
+
+	interface.data = framedata;
+
+	if (!strcasecmp(data, "RX")) {
+		framedata->list_type = RX;
+	} else {
+		framedata->list_type = TX;
+	}
+
+	buffer = ast_malloc(sizeof(value) + 3); /* leading and trailing comma and null terminator */
+	snprintf(buffer, sizeof(value) + 2, ",%s,", value);
+
+	for (i = 0; i < ARRAY_LEN(controlframetype2str); i++) {
+		if (strcasestr(value, controlframetype2str[i].str)) {
+			framedata->controlvalues[i] = 1;
+			intercepting++;
+		}
+	}
+	ast_free(buffer);
+
+	ast_channel_lock(chan);
+	i = ast_framehook_attach(chan, &interface);
+	if (i >= 0) {
+		int *id;
+		if ((datastore = ast_channel_datastore_find(chan, &frame_intercept_datastore, NULL))) {
+			id = datastore->data;
+			ast_framehook_detach(chan, *id);
+			ast_channel_datastore_remove(chan, datastore);
+			ast_datastore_free(datastore);
+			if (!intercepting) { /* if we just removed all the intercepts, then we're not needed anymore */
+				ast_channel_unlock(chan);
+				return 0;
+			}
+		}
+
+		if (!(datastore = ast_datastore_alloc(&frame_intercept_datastore, NULL))) {
+			ast_log(LOG_WARNING, "Failed to setup FRAME_INTERCEPT framehook\n");
+			ast_framehook_detach(chan, i);
+			ast_channel_unlock(chan);
+			return 0;
+		}
+
+		if (!(id = ast_calloc(1, sizeof(int)))) {
+			ast_log(LOG_WARNING, "Failed to setup FRAME_INTERCEPT framehook\n");
+			ast_datastore_free(datastore);
+			ast_framehook_detach(chan, i);
+			ast_channel_unlock(chan);
+			return 0;
+		}
+
+		*id = i; /* Store off the id. The channel is still locked so it is safe to access this ptr. */
+		datastore->data = id;
+		ast_channel_datastore_add(chan, datastore);
+	}
+	ast_channel_unlock(chan);
+
+	return 0;
+}
+
+/*! \brief Definition of the FRAME_INTERCEPT function */
+static struct ast_custom_function frame_intercept_function = {
+	.name = "FRAME_INTERCEPT",
+	.write = frame_intercept_helper,
+};
+
+static int unload_module(void)
+{
+	return ast_custom_function_unregister(&frame_intercept_function);
+}
+
+static int load_module(void)
+{
+	int res = ast_custom_function_register(&frame_intercept_function);
+	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Function to intercept frames on a channel.");
diff --git a/include/asterisk/stasis_channels.h b/include/asterisk/stasis_channels.h
index f345228..80c79eb 100644
--- a/include/asterisk/stasis_channels.h
+++ b/include/asterisk/stasis_channels.h
@@ -529,6 +529,13 @@
 struct stasis_message_type *ast_channel_flash_type(void);
 
 /*!
+ * \brief Message type for when a wink occurs on a channel.
+ *
+ * \return A stasis message type
+ */
+struct stasis_message_type *ast_channel_wink_type(void);
+
+/*!
  * \since 12
  * \brief Message type for when a channel is placed on hold.
  *
diff --git a/main/stasis.c b/main/stasis.c
index a75fe66..cf89a99 100644
--- a/main/stasis.c
+++ b/main/stasis.c
@@ -121,6 +121,7 @@
 							<enum name="ast_channel_dtmf_begin_type" />
 							<enum name="ast_channel_dtmf_end_type" />
 							<enum name="ast_channel_flash_type" />
+							<enum name="ast_channel_wink_type" />
 							<enum name="ast_channel_hold_type" />
 							<enum name="ast_channel_unhold_type" />
 							<enum name="ast_channel_chanspy_start_type" />
diff --git a/main/stasis_channels.c b/main/stasis_channels.c
index 5e39c07..8becf96 100644
--- a/main/stasis_channels.c
+++ b/main/stasis_channels.c
@@ -1599,6 +1599,7 @@
 	.to_json = unhold_to_json,
 	);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_flash_type);
+STASIS_MESSAGE_TYPE_DEFN(ast_channel_wink_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_start_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_stop_type);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_fax_type);
@@ -1644,6 +1645,7 @@
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_flash_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_wink_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hold_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_unhold_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_chanspy_start_type);
@@ -1698,6 +1700,7 @@
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_flash_type);
+	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_wink_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hold_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_unhold_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_chanspy_start_type);

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I5ad83b95a4dce3b64b408a1497b0cd63051eb274
Gerrit-Change-Number: 17716
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/20211229/972b0775/attachment-0001.html>


More information about the asterisk-code-review mailing list