[asterisk-commits] dlee: trunk r385734 - in /trunk: apps/ include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Apr 15 11:22:08 CDT 2013


Author: dlee
Date: Mon Apr 15 11:22:03 2013
New Revision: 385734

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385734
Log:
DTMF events are now published on a channel's stasis_topic. AMI was
refactored to use these events rather than producing the events directly
in channel.c. Finally, the code was added to app_stasis to produce
DTMF events on the WebSocket.

The AMI events are completely backward compatible, including sending
events on transmitted DTMF, and sending DTMF start events.

The Stasis-HTTP events are somewhat simplified. Since DTMF start and
DTMF send events are generally less useful, Stasis-HTTP will only send
events on received DTMF end.

(closes issue ASTERISK-21282)
(closes issue ASTERISK-21359)
Review: https://reviewboard.asterisk.org/r/2439

Modified:
    trunk/apps/app_stasis.c
    trunk/include/asterisk/cli.h
    trunk/include/asterisk/strings.h
    trunk/main/channel.c
    trunk/main/manager_channels.c

Modified: trunk/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/trunk/apps/app_stasis.c?view=diff&rev=385734&r1=385733&r2=385734
==============================================================================
--- trunk/apps/app_stasis.c (original)
+++ trunk/apps/app_stasis.c Mon Apr 15 11:22:03 2013
@@ -358,6 +358,43 @@
 	return 0;
 }
 
+static void dtmf_handler(struct app *app, struct ast_channel_blob *obj)
+{
+	RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+	const char *direction;
+
+	/* To simplify events, we'll only generate on receive */
+	direction = ast_json_string_get(
+		ast_json_object_get(obj->blob, "direction"));
+
+	if (strcmp("Received", direction) != 0) {
+		return;
+	}
+
+	extra = ast_json_pack(
+		"{s: o}",
+		"digit", ast_json_ref(ast_json_object_get(obj->blob, "digit")));
+	if (!extra) {
+		return;
+	}
+
+	msg = app_event_create("dtmf-received", obj->snapshot, extra);
+	if (!msg) {
+		return;
+	}
+
+	app_send(app, msg);
+}
+
+static void blob_handler(struct app *app, struct ast_channel_blob *blob)
+{
+	/* To simplify events, we'll only generate on DTMF end */
+	if (strcmp(ast_channel_blob_json_type(blob), "dtmf_end") == 0) {
+		dtmf_handler(app, blob);
+	}
+}
+
 static void sub_handler(void *data, struct stasis_subscription *sub,
 			struct stasis_topic *topic,
 			struct stasis_message *message)
@@ -373,6 +410,9 @@
 			return;
 		}
 		app_send(app, msg);
+	} else if (ast_channel_blob_type() == stasis_message_type(message)) {
+		struct ast_channel_blob *blob = stasis_message_data(message);
+		blob_handler(app, blob);
 	}
 	if (stasis_subscription_final_message(sub, message)) {
 		ao2_cleanup(data);

Modified: trunk/include/asterisk/cli.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/cli.h?view=diff&rev=385734&r1=385733&r2=385734
==============================================================================
--- trunk/include/asterisk/cli.h (original)
+++ trunk/include/asterisk/cli.h Mon Apr 15 11:22:03 2013
@@ -28,6 +28,7 @@
 #endif
 
 #include "asterisk/linkedlists.h"
+#include "asterisk/strings.h"
 
 void ast_cli(int fd, const char *fmt, ...)
 	__attribute__((format(printf, 2, 3)));
@@ -57,12 +58,17 @@
  */
 #define ESS(x) ((x) == 1 ? "" : "s")
 
-/*! \brief return Yes or No depending on the argument.
- * This is used in many places in CLI command, having a function to generate
- * this helps maintaining a consistent output (and possibly emitting the
- * output in other languages, at some point).
- */
-#define AST_CLI_YESNO(x) (x) ? "Yes" : "No"
+/*!
+ * \brief Return Yes or No depending on the argument.
+ *
+ * Note that this should probably still be used for CLI commands instead of
+ * AST_YESNO(), in the off chance we someday want to translate the CLI.
+ *
+ * \param x Boolean value
+ * \return "Yes" if x is true (non-zero)
+ * \return "No" if x is false (zero)
+ */
+#define AST_CLI_YESNO(x) AST_YESNO(x)
 
 /*! \brief return On or Off depending on the argument.
  * This is used in many places in CLI command, having a function to generate

Modified: trunk/include/asterisk/strings.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/strings.h?view=diff&rev=385734&r1=385733&r2=385734
==============================================================================
--- trunk/include/asterisk/strings.h (original)
+++ trunk/include/asterisk/strings.h Mon Apr 15 11:22:03 2013
@@ -83,6 +83,18 @@
 #define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
 
 /*!
+ * \brief return Yes or No depending on the argument.
+ *
+ * Note that this macro is used my AMI, where a literal "Yes" and "No" are
+ * expected, and translations would cause problems.
+ *
+ * \param x Boolean value
+ * \return "Yes" if x is true (non-zero)
+ * \return "No" if x is false (zero)
+ */
+#define AST_YESNO(x) ((x) ? "Yes" : "No")
+
+/*!
   \brief Gets a pointer to the first non-whitespace character in a string.
   \param str the input string
   \return a pointer to the first non-whitespace character

Modified: trunk/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/channel.c?view=diff&rev=385734&r1=385733&r2=385734
==============================================================================
--- trunk/main/channel.c (original)
+++ trunk/main/channel.c Mon Apr 15 11:22:03 2013
@@ -3714,42 +3714,56 @@
 	return 0; /* Time is up */
 }
 
-static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
-{
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a DTMF digit has started or ended on a channel.</synopsis>
-				<syntax>
-					<parameter name="Direction">
-						<enumlist>
-							<enum name="Received"/>
-							<enum name="Sent"/>
-						</enumlist>
-					</parameter>
-					<parameter name="Begin">
-						<enumlist>
-							<enum name="Yes"/>
-							<enum name="No"/>
-						</enumlist>
-					</parameter>
-					<parameter name="End">
-						<enumlist>
-							<enum name="Yes"/>
-							<enum name="No"/>
-						</enumlist>
-					</parameter>
-				</syntax>
-		</managerEventInstance>
-	***/
-	ast_manager_event(chan, EVENT_FLAG_DTMF,
-			"DTMF",
-			"Channel: %s\r\n"
-			"Uniqueid: %s\r\n"
-			"Digit: %c\r\n"
-			"Direction: %s\r\n"
-			"Begin: %s\r\n"
-			"End: %s\r\n",
-			ast_channel_name(chan), ast_channel_uniqueid(chan), digit, direction, begin, end);
+enum DtmfDirection {
+	DTMF_RECEIVED,
+	DTMF_SENT
+};
+
+static const char *dtmf_direction_to_string(enum DtmfDirection direction)
+{
+	switch (direction) {
+	case DTMF_RECEIVED:
+		return "Received";
+	case DTMF_SENT:
+		return "Sent";
+	}
+
+	return "?";
+}
+
+static void send_dtmf_begin_event(struct ast_channel *chan,
+	enum DtmfDirection direction, const char digit)
+{
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	char digit_str[] = { digit, '\0' };
+
+	blob = ast_json_pack("{ s: s, s: s, s: s }",
+		"type", "dtmf_begin",
+		"digit", digit_str,
+		"direction", dtmf_direction_to_string(direction));
+	if (!blob) {
+		return;
+	}
+
+	publish_channel_blob(chan, blob);
+}
+
+static void send_dtmf_end_event(struct ast_channel *chan,
+	enum DtmfDirection direction, const char digit, long duration_ms)
+{
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	char digit_str[] = { digit, '\0' };
+
+	blob = ast_json_pack("{ s: s, s: s, s: s, s: i }",
+		"type", "dtmf_end",
+		"digit", digit_str,
+		"direction", dtmf_direction_to_string(direction),
+		"duration_ms", duration_ms);
+	if (!blob) {
+		return;
+	}
+
+	publish_channel_blob(chan, blob);
 }
 
 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
@@ -4104,7 +4118,7 @@
 			}
 			break;
 		case AST_FRAME_DTMF_END:
-			send_dtmf_event(chan, "Received", f->subclass.integer, "No", "Yes");
+			send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
 			ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
 			/* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
 			if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
@@ -4190,7 +4204,7 @@
 			}
 			break;
 		case AST_FRAME_DTMF_BEGIN:
-			send_dtmf_event(chan, "Received", f->subclass.integer, "Yes", "No");
+			send_dtmf_begin_event(chan, DTMF_RECEIVED, f->subclass.integer);
 			ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
 			if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_END_DTMF_ONLY | AST_FLAG_EMULATE_DTMF) ||
 			    (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
@@ -5042,7 +5056,7 @@
 			if (old_frame != fr)
 				f = fr;
 		}
-		send_dtmf_event(chan, "Sent", fr->subclass.integer, "Yes", "No");
+		send_dtmf_begin_event(chan, DTMF_SENT, fr->subclass.integer);
 		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
 		ast_channel_unlock(chan);
 		res = ast_senddigit_begin(chan, fr->subclass.integer);
@@ -5058,7 +5072,7 @@
 				ast_frfree(new_frame);
 			}
 		}
-		send_dtmf_event(chan, "Sent", fr->subclass.integer, "No", "Yes");
+		send_dtmf_end_event(chan, DTMF_SENT, fr->subclass.integer, fr->len);
 		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
 		ast_channel_unlock(chan);
 		res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);

Modified: trunk/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/manager_channels.c?view=diff&rev=385734&r1=385733&r2=385734
==============================================================================
--- trunk/main/manager_channels.c (original)
+++ trunk/main/manager_channels.c Mon Apr 15 11:22:03 2013
@@ -275,8 +275,8 @@
 		struct ast_var_t *var;
 		AST_LIST_TRAVERSE(snapshot->manager_vars, var, entries) {
 			ast_str_append(&out, 0, "ChanVariable%s: %s=%s\r\n",
-						   suffix,
-						   var->name, var->value);
+				       suffix,
+				       var->name, var->value);
 		}
 	}
 
@@ -657,6 +657,90 @@
 		      ast_str_buffer(extra));
 }
 
+static void channel_dtmf_begin(struct ast_channel_blob *obj)
+{
+	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+	const char *digit =
+		ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
+	const char *direction =
+		ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
+
+	channel_event_string = manager_build_channel_state_string(obj->snapshot);
+
+	if (!channel_event_string) {
+		return;
+	}
+
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>Raised when a DTMF digit has started on a channel.</synopsis>
+				<syntax>
+					<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+					<parameter name="Digit">
+						<para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
+					</parameter>
+					<parameter name="Direction">
+						<enumlist>
+							<enum name="Received"/>
+							<enum name="Sent"/>
+						</enumlist>
+					</parameter>
+				</syntax>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_DTMF, "DTMFBegin",
+		"%s"
+		"Digit: %s\r\n"
+		"Direction: %s\r\n",
+		ast_str_buffer(channel_event_string),
+		digit, direction);
+}
+
+static void channel_dtmf_end(struct ast_channel_blob *obj)
+{
+	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+	const char *digit =
+		ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
+	const char *direction =
+		ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
+	long duration_ms =
+		ast_json_integer_get(ast_json_object_get(obj->blob, "duration_ms"));
+
+	channel_event_string = manager_build_channel_state_string(obj->snapshot);
+
+	if (!channel_event_string) {
+		return;
+	}
+
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>Raised when a DTMF digit has ended on a channel.</synopsis>
+				<syntax>
+					<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+					<parameter name="Digit">
+						<para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
+					</parameter>
+					<parameter name="DurationMs">
+						<para>Duration (in milliseconds) DTMF was sent/received</para>
+					</parameter>
+					<parameter name="Direction">
+						<enumlist>
+							<enum name="Received"/>
+							<enum name="Sent"/>
+						</enumlist>
+					</parameter>
+				</syntax>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_DTMF, "DTMFEnd",
+		"%s"
+		"Digit: %s\r\n"
+		"DurationMs: %ld\r\n"
+		"Direction: %s\r\n",
+		ast_str_buffer(channel_event_string),
+		digit, duration_ms, direction);
+}
+
 /*!
  * \brief Callback processing messages on the channel topic.
  */
@@ -672,6 +756,10 @@
 		channel_userevent(obj);
 	} else if (strcmp("hangup_request", ast_channel_blob_json_type(obj)) == 0) {
 		channel_hangup_request(obj);
+	} else if (strcmp("dtmf_begin", ast_channel_blob_json_type(obj)) == 0) {
+		channel_dtmf_begin(obj);
+	} else if (strcmp("dtmf_end", ast_channel_blob_json_type(obj)) == 0) {
+		channel_dtmf_end(obj);
 	}
 }
 




More information about the asterisk-commits mailing list