[asterisk-commits] dlee: branch dlee/stasis-dtmf r385237 - /team/dlee/stasis-dtmf/main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 10 10:38:28 CDT 2013


Author: dlee
Date: Wed Apr 10 10:38:26 2013
New Revision: 385237

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385237
Log:
Port DTMF events to stasis

Modified:
    team/dlee/stasis-dtmf/main/channel.c
    team/dlee/stasis-dtmf/main/manager_channels.c

Modified: team/dlee/stasis-dtmf/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-dtmf/main/channel.c?view=diff&rev=385237&r1=385236&r2=385237
==============================================================================
--- team/dlee/stasis-dtmf/main/channel.c (original)
+++ team/dlee/stasis-dtmf/main/channel.c Wed Apr 10 10:38:26 2013
@@ -3714,42 +3714,41 @@
 	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_event(struct ast_channel *chan,
+			    enum DtmfDirection direction, const char digit,
+			    int begin, int end)
+{
+	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: b, s: b }",
+			     "type", "dtmf",
+			     "digit", digit_str,
+			     "direction", dtmf_direction_to_string(direction),
+			     "begin", begin,
+			     "end", end);
+	if (!blob) {
+		return;
+	}
+
+	publish_channel_blob(chan, blob);
 }
 
 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
@@ -4104,7 +4103,7 @@
 			}
 			break;
 		case AST_FRAME_DTMF_END:
-			send_dtmf_event(chan, "Received", f->subclass.integer, "No", "Yes");
+			send_dtmf_event(chan, DTMF_RECEIVED, f->subclass.integer, 0, 1);
 			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 +4189,7 @@
 			}
 			break;
 		case AST_FRAME_DTMF_BEGIN:
-			send_dtmf_event(chan, "Received", f->subclass.integer, "Yes", "No");
+			send_dtmf_event(chan, DTMF_RECEIVED, f->subclass.integer, 1, 0);
 			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 +5041,7 @@
 			if (old_frame != fr)
 				f = fr;
 		}
-		send_dtmf_event(chan, "Sent", fr->subclass.integer, "Yes", "No");
+		send_dtmf_event(chan, DTMF_SENT, fr->subclass.integer, 1, 0);
 		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
 		ast_channel_unlock(chan);
 		res = ast_senddigit_begin(chan, fr->subclass.integer);
@@ -5058,7 +5057,7 @@
 				ast_frfree(new_frame);
 			}
 		}
-		send_dtmf_event(chan, "Sent", fr->subclass.integer, "No", "Yes");
+		send_dtmf_event(chan, DTMF_SENT, fr->subclass.integer, 0, 1);
 		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: team/dlee/stasis-dtmf/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-dtmf/main/manager_channels.c?view=diff&rev=385237&r1=385236&r2=385237
==============================================================================
--- team/dlee/stasis-dtmf/main/manager_channels.c (original)
+++ team/dlee/stasis-dtmf/main/manager_channels.c Wed Apr 10 10:38:26 2013
@@ -231,6 +231,20 @@
 		</managerEventInstance>
 	</managerEvent>
  ***/
+
+/*!
+ * \brief Convert a boolean to a Yes or No.
+ * \param v Boolean value
+ * \return "Yes" if v is true (non-zero)
+ * \return "No" if v is false (zero)
+ */
+static const char *yes_no(int v)
+{
+	if (v) {
+		return "Yes";
+	}
+	return "No";
+}
 
 /*!
  * \brief Generate the AMI message body from a channel snapshot
@@ -286,8 +300,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);
 		}
 	}
 
@@ -678,6 +692,57 @@
 		      ast_str_buffer(extra));
 }
 
+static void channel_dtmf(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"));
+	int begin = ast_json_is_true(ast_json_object_get(obj->blob, "begin"));
+	int end = ast_json_is_true(ast_json_object_get(obj->blob, "end"));
+
+	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 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>
+	***/
+	manager_event(EVENT_FLAG_DTMF, "DTMF",
+		      "%s"
+		      "Digit: %s\r\n"
+		      "Direction: %s\r\n"
+		      "Begin: %s\r\n"
+		      "End: %s\r\n",
+		      ast_str_buffer(channel_event_string),
+		      digit, direction, yes_no(begin), yes_no(end));
+}
+
 /*!
  * \brief Callback processing messages on the channel topic.
  */
@@ -693,6 +758,8 @@
 		channel_userevent(obj);
 	} else if (strcmp("hangup_request", ast_channel_blob_json_type(obj)) == 0) {
 		channel_hangup_request(obj);
+	} else if (strcmp("dtmf", ast_channel_blob_json_type(obj)) == 0) {
+		channel_dtmf(obj);
 	}
 }
 




More information about the asterisk-commits mailing list