From d04f1fd60a5f5a6e358fbe5a59ae8f42d21cbbdf Mon Sep 17 00:00:00 2001
From: Matthew Jordan <mjordan@digium.com>
Date: Fri, 17 May 2013 17:43:58 +0000
Subject: [PATCH] Publish the outbound channel's application/data when dialing

This patch does two things:
* It fixes a bug where the outbound channel's application/data set by the
  dialing API/app_dial is not communicated until the channel is hung up.
  If that happens, AMI would incorrectly send a NewExten event immediately
  after a Hangup. This isn't really AMI's fault, as the dialing APIs never
  communicated the 'helpful' app/data on the outbound channel until it was
  hungup.
* It makes public sending a stasis message about a change in channel state.
  This is useful enough that - for now at least - it should be public. If
  operations on a channel go to being more coarse-grained, this function
  could be made private again.

Review: https://reviewboard.asterisk.org/r/2548

Note that this problem was found and reported by Matt DiMeo.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@388976 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_dial.c                    |  3 +++
 include/asterisk/stasis_channels.h |  9 +++++++++
 main/channel.c                     | 32 ++++++------------------------
 main/dial.c                        |  1 +
 main/stasis_channels.c             | 24 ++++++++++++++++++++++
 5 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/apps/app_dial.c b/apps/app_dial.c
index 74f41c06dc..d3d37216aa 100644
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -972,6 +972,7 @@ static void do_forward(struct chanlist *o, struct cause_args *num,
 
 		ast_channel_appl_set(c, "AppDial");
 		ast_channel_data_set(c, "(Outgoing Line)");
+		ast_publish_channel_state(c);
 
 		ast_channel_unlock(in);
 		if (single && !ast_test_flag64(o, OPT_IGNORE_CONNECTEDLINE)) {
@@ -2453,6 +2454,8 @@ static int dial_exec_full(struct ast_channel *chan, const char *data, struct ast
 
 		ast_channel_appl_set(tc, "AppDial");
 		ast_channel_data_set(tc, "(Outgoing Line)");
+		ast_publish_channel_state(tc);
+
 		memset(ast_channel_whentohangup(tc), 0, sizeof(*ast_channel_whentohangup(tc)));
 
 		/* Determine CallerID to store in outgoing channel. */
diff --git a/include/asterisk/stasis_channels.h b/include/asterisk/stasis_channels.h
index e361fc680e..5ead93d3a5 100644
--- a/include/asterisk/stasis_channels.h
+++ b/include/asterisk/stasis_channels.h
@@ -297,6 +297,15 @@ void ast_channel_publish_dial(struct ast_channel *caller,
 		const char *dialstring,
 		const char *dialstatus);
 
+/*!
+ * \since 12
+ * \brief Publish in the \ref ast_channel_topic a \ref ast_channel_snapshot
+ * message indicating a change in channel state
+ *
+ * \param chan The channel whose state has changed
+ */
+void ast_publish_channel_state(struct ast_channel *chan);
+
 /*! @} */
 
 /*!
diff --git a/main/channel.c b/main/channel.c
index d63aeb2ba7..0b2dedd637 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -816,26 +816,6 @@ int ast_str2cause(const char *name)
 	return -1;
 }
 
-static void publish_channel_state(struct ast_channel *chan)
-{
-	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
-
-	snapshot = ast_channel_snapshot_create(chan);
-	if (!snapshot) {
-		ast_log(LOG_ERROR, "Allocation error\n");
-		return;
-	}
-
-	message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
-	if (!message) {
-		return;
-	}
-
-	ast_assert(ast_channel_topic(chan) != NULL);
-	stasis_publish(ast_channel_topic(chan), message);
-}
-
 static void publish_cache_clear(struct ast_channel *chan)
 {
 	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
@@ -1176,7 +1156,7 @@ __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char
 	 * a lot of data into this func to do it here!
 	 */
 	if (ast_get_channel_tech(tech) || (tech2 && ast_get_channel_tech(tech2))) {
-		publish_channel_state(tmp);
+		ast_publish_channel_state(tmp);
 	}
 
 	ast_channel_internal_finalize(tmp);
@@ -2885,7 +2865,7 @@ int ast_hangup(struct ast_channel *chan)
 
 	ast_cc_offer(chan);
 
-	publish_channel_state(chan);
+	ast_publish_channel_state(chan);
 	publish_cache_clear(chan);
 
 	if (ast_channel_cdr(chan) && !ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_BRIDGED) &&
@@ -7144,7 +7124,7 @@ void ast_do_masquerade(struct ast_channel *original)
 	ast_channel_redirecting_set(original, ast_channel_redirecting(clonechan));
 	ast_channel_redirecting_set(clonechan, &exchange.redirecting);
 
-	publish_channel_state(original);
+	ast_publish_channel_state(original);
 
 	/* Restore original timing file descriptor */
 	ast_channel_set_fd(original, AST_TIMING_FD, ast_channel_timingfd(original));
@@ -7310,7 +7290,7 @@ void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char
 		ast_cdr_setcid(ast_channel_cdr(chan), chan);
 	}
 
-	publish_channel_state(chan);
+	ast_publish_channel_state(chan);
 
 	ast_channel_unlock(chan);
 }
@@ -7336,7 +7316,7 @@ void ast_channel_set_caller_event(struct ast_channel *chan, const struct ast_par
 
 	ast_channel_lock(chan);
 	ast_party_caller_set(ast_channel_caller(chan), caller, update);
-	publish_channel_state(chan);
+	ast_publish_channel_state(chan);
 	if (ast_channel_cdr(chan)) {
 		ast_cdr_setcid(ast_channel_cdr(chan), chan);
 	}
@@ -7363,7 +7343,7 @@ int ast_setstate(struct ast_channel *chan, enum ast_channel_state state)
 	 * we override what they are saying the state is and things go amuck. */
 	ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), name);
 
-	publish_channel_state(chan);
+	ast_publish_channel_state(chan);
 
 	return 0;
 }
diff --git a/main/dial.c b/main/dial.c
index f8b0c97874..da02f7a236 100644
--- a/main/dial.c
+++ b/main/dial.c
@@ -287,6 +287,7 @@ static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_chann
 
 	ast_channel_appl_set(channel->owner, "AppDial2");
 	ast_channel_data_set(channel->owner, "(Outgoing Line)");
+	ast_publish_channel_state(channel->owner);
 	memset(ast_channel_whentohangup(channel->owner), 0, sizeof(*ast_channel_whentohangup(channel->owner)));
 
 	/* Inherit everything from he who spawned this dial */
diff --git a/main/stasis_channels.c b/main/stasis_channels.c
index acae819394..3df52d0b37 100644
--- a/main/stasis_channels.c
+++ b/main/stasis_channels.c
@@ -417,6 +417,30 @@ void ast_channel_publish_varset(struct ast_channel *chan, const char *name, cons
 	publish_message_for_channel_topics(msg, chan);
 }
 
+void ast_publish_channel_state(struct ast_channel *chan)
+{
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+
+	ast_assert(chan != NULL);
+	if (!chan) {
+		return;
+	}
+
+	snapshot = ast_channel_snapshot_create(chan);
+	if (!snapshot) {
+		return;
+	}
+
+	message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
+	if (!message) {
+		return;
+	}
+
+	ast_assert(ast_channel_topic(chan) != NULL);
+	stasis_publish(ast_channel_topic(chan), message);
+}
+
 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
 {
 	RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
-- 
GitLab