[asterisk-commits] kmoore: branch kmoore/channel-state-caching r381054 - /team/kmoore/channel-st...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Feb 7 13:19:16 CST 2013


Author: kmoore
Date: Thu Feb  7 13:19:14 2013
New Revision: 381054

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381054
Log:
Refactor the reworked AMI events a bit more

Channel-related AMI events will now have a standard set of parameters
instead of trading various parameters in and out depending on event.
Currently, this covers Newchannel, Newstate, and Hangup, but will
encompass more in the future.

Modified:
    team/kmoore/channel-state-caching/main/manager.c

Modified: team/kmoore/channel-state-caching/main/manager.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/channel-state-caching/main/manager.c?view=diff&rev=381054&r1=381053&r2=381054
==============================================================================
--- team/kmoore/channel-state-caching/main/manager.c (original)
+++ team/kmoore/channel-state-caching/main/manager.c Thu Feb  7 13:19:14 2013
@@ -963,6 +963,73 @@
                         manager.conf will be present upon starting a new session.</para>
 		</description>
 	</manager>
+	<managerEvent language="en_US" name="Newchannel">
+		<managerEventInstance class="EVENT_FLAG_CALL">
+			<synopsis>Raised when a new channel is created.</synopsis>
+			<syntax>
+				<parameter name="Channel">
+				</parameter>
+				<parameter name="ChannelState">
+					<para>A numeric code for the channel's current state, related to ChannelStateDesc</para>
+				</parameter>
+				<parameter name="ChannelStateDesc">
+					<enumlist>
+						<enum name="Down"/>
+						<enum name="Rsrvd"/>
+						<enum name="OffHook"/>
+						<enum name="Dialing"/>
+						<enum name="Ring"/>
+						<enum name="Ringing"/>
+						<enum name="Up"/>
+						<enum name="Busy"/>
+						<enum name="Dialing Offhook"/>
+						<enum name="Pre-ring"/>
+						<enum name="Unknown"/>
+					</enumlist>
+				</parameter>
+				<parameter name="CallerIDNum">
+				</parameter>
+				<parameter name="CallerIDName">
+				</parameter>
+				<parameter name="ConnectedLineNum">
+				</parameter>
+				<parameter name="ConnectedLineName">
+				</parameter>
+				<parameter name="AccountCode">
+				</parameter>
+				<parameter name="Context">
+				</parameter>
+				<parameter name="Exten">
+				</parameter>
+				<parameter name="Priority">
+				</parameter>
+				<parameter name="Uniqueid">
+				</parameter>
+				<parameter name="Cause">
+					<para>A numeric cause code for why the channel was hung up.</para>
+				</parameter>
+				<parameter name="Cause-txt">
+					<para>A description of why the channel was hung up.</para>
+				</parameter>
+			</syntax>
+		</managerEventInstance>
+	</managerEvent>
+	<managerEvent language="en_US" name="Newstate">
+		<managerEventInstance class="EVENT_FLAG_CALL">
+			<synopsis>Raised when a channel's state changes.</synopsis>
+			<syntax>
+				<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+			</syntax>
+		</managerEventInstance>
+	</managerEvent>
+	<managerEvent language="en_US" name="Hangup">
+		<managerEventInstance class="EVENT_FLAG_CALL">
+			<synopsis>Raised when a channel is hung up.</synopsis>
+			<syntax>
+				<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+			</syntax>
+		</managerEventInstance>
+	</managerEvent>
  ***/
 
 /*! \addtogroup Group_AMI AMI functions
@@ -7345,66 +7412,23 @@
 	AST_RWLIST_UNLOCK(&channelvars);
 }
 
-static void manager_newchannel(const struct ast_channel_snapshot *snapshot)
-{
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a new channel is created.</synopsis>
-			<syntax>
-				<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newstate']/managerEventInstance/syntax/parameter[@name='ChannelState'])" />
-				<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newstate']/managerEventInstance/syntax/parameter[@name='ChannelStateDesc'])" />
-			</syntax>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_CALL, "Newchannel",
-		"Channel: %s\r\n"
-		"ChannelState: %d\r\n"
-		"ChannelStateDesc: %s\r\n"
-		"CallerIDNum: %s\r\n"
-		"CallerIDName: %s\r\n"
-		"AccountCode: %s\r\n"
-		"Exten: %s\r\n"
-		"Context: %s\r\n"
-		"Uniqueid: %s\r\n",
-		snapshot->name,
-		snapshot->state,
-		ast_state2str(snapshot->state),
-		snapshot->caller_number,
-		snapshot->caller_name,
-		snapshot->accountcode,
-		snapshot->exten,
-		snapshot->context,
-		snapshot->uniqueid);
-}
-
-static void manager_newstate(const struct ast_channel_snapshot *snapshot)
-{
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a channel's state changes.</synopsis>
-			<syntax>
-				<parameter name="ChannelState">
-					<para>A numeric code for the channel's current state, related to ChannelStateDesc</para>
-				</parameter>
-				<parameter name="ChannelStateDesc">
-					<enumlist>
-						<enum name="Down"/>
-						<enum name="Rsrvd"/>
-						<enum name="OffHook"/>
-						<enum name="Dialing"/>
-						<enum name="Ring"/>
-						<enum name="Ringing"/>
-						<enum name="Up"/>
-						<enum name="Busy"/>
-						<enum name="Dialing Offhook"/>
-						<enum name="Pre-ring"/>
-						<enum name="Unknown"/>
-					</enumlist>
-				</parameter>
-			</syntax>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_CALL, "Newstate",
+/*!
+ * \brief Generate the AMI message body from a channel snapshot
+ * \internal
+ *
+ * \param snapshot the channel snapshot for which to generate an AMI message body
+ *
+ * \retval NULL on error
+ * \retval ast_str* on success (must be ast_freed by caller)
+ */
+static struct ast_str *manager_build_channel_state_string(const struct ast_channel_snapshot *snapshot)
+{
+	struct ast_str *out = ast_str_create(1024);
+	int res = 0;
+	if (!out) {
+		return NULL;
+	}
+	res = ast_str_set(&out, 0,
 		"Channel: %s\r\n"
 		"ChannelState: %d\r\n"
 		"ChannelStateDesc: %s\r\n"
@@ -7412,7 +7436,13 @@
 		"CallerIDName: %s\r\n"
 		"ConnectedLineNum: %s\r\n"
 		"ConnectedLineName: %s\r\n"
-		"Uniqueid: %s\r\n",
+		"AccountCode: %s\r\n"
+		"Context: %s\r\n"
+		"Exten: %s\r\n"
+		"Priority: %d\r\n"
+		"Uniqueid: %s\r\n"
+		"Cause: %d\r\n"
+		"Cause-txt: %s\r\n",
 		snapshot->name,
 		snapshot->state,
 		ast_state2str(snapshot->state),
@@ -7420,43 +7450,19 @@
 		snapshot->caller_name,
 		snapshot->connected_number,
 		snapshot->connected_name,
-		snapshot->uniqueid);
-}
-
-static void manager_hangup(const struct ast_channel_snapshot *snapshot)
-{
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a channel is hung up.</synopsis>
-				<syntax>
-					<parameter name="Cause">
-						<para>A numeric cause code for why the channel was hung up.</para>
-					</parameter>
-					<parameter name="Cause-txt">
-						<para>A description of why the channel was hung up.</para>
-					</parameter>
-				</syntax>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_CALL, "Hangup",
-		"Channel: %s\r\n"
-		"Uniqueid: %s\r\n"
-		"CallerIDNum: %s\r\n"
-		"CallerIDName: %s\r\n"
-		"ConnectedLineNum: %s\r\n"
-		"ConnectedLineName: %s\r\n"
-		"AccountCode: %s\r\n"
-		"Cause: %d\r\n"
-		"Cause-txt: %s\r\n",
-		snapshot->name,
+		snapshot->accountcode,
+		snapshot->context,
+		snapshot->exten,
+		snapshot->priority,
 		snapshot->uniqueid,
-		snapshot->caller_number,
-		snapshot->caller_name,
-		snapshot->connected_number,
-		snapshot->connected_name,
-		snapshot->accountcode,
 		snapshot->hangupcause,
 		ast_cause2str(snapshot->hangupcause));
+
+	if (!res) {
+		return NULL;
+	}
+
+	return out;
 }
 
 static int channel_state_hash_fn(const void *obj, const int flags)
@@ -7484,6 +7490,7 @@
 	struct ast_channel_snapshot *old_snapshot = NULL, *ao2_snapshot = NULL;
 	int is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
 	int find_flags = OBJ_KEY;
+	char *manager_event = NULL;
 
 	/* Only allocate for a new AO2 object if the channel is not hung up */
 	if (!is_hungup) {
@@ -7510,21 +7517,29 @@
 
 	/* Generate a Newchannel event when there is no cached state and the channel has not been hung up. */
 	if (!old_snapshot && !is_hungup) {
-		manager_newchannel(new_snapshot);
+		manager_event = "Newchannel";
 	}
 
 	/* Generate a Newstate event when the old state is known and has changed. */
 	if (old_snapshot && new_snapshot->state != old_snapshot->state) {
-		manager_newstate(new_snapshot);
+		manager_event = "Newstate";
 	}
 
 	/* Generate a Hangup event when the old state is known and the channel has been hung up. */
 	if (old_snapshot && is_hungup) {
-		manager_hangup(new_snapshot);
+		manager_event = "Hangup";
 		/* drop the cache for this channel */
 		ast_event_drop_cached(AST_EVENT_CHANNEL_STATE,
 			AST_EVENT_IE_CEL_CHANNAME,        AST_EVENT_IE_PLTYPE_STR, new_snapshot->uniqueid,
 			AST_EVENT_IE_END);
+	}
+
+	if (manager_event) {
+		struct ast_str *channel_event_string = manager_build_channel_state_string(new_snapshot);
+		if (channel_event_string) {
+			manager_event(EVENT_FLAG_CALL, manager_event, "%s", ast_str_buffer(channel_event_string));
+			ast_free(channel_event_string);
+		}
 	}
 
 	/* Destroy the snapshot removed from the cache.




More information about the asterisk-commits mailing list