[asterisk-commits] kpfleming: trunk r191919 - in /trunk: include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun May 3 09:29:06 CDT 2009


Author: kpfleming
Date: Sun May  3 09:28:59 2009
New Revision: 191919

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=191919
Log:
Add 'bitflags'-style information elements to event framework

This patch add a new payload type for information elements, a set
of bit flags. The payload is transported as a 32-bit unsigned integer
but when matching is performed between events and subscribers,
the matching is done by using a bitwise AND instead of numeric value
comparison.

Review: http://reviewboard.asterisk.org/r/242/


Modified:
    trunk/include/asterisk/event.h
    trunk/include/asterisk/event_defs.h
    trunk/main/event.c

Modified: trunk/include/asterisk/event.h
URL: http://svn.digium.com/svn-view/asterisk/trunk/include/asterisk/event.h?view=diff&rev=191919&r1=191918&r2=191919
==============================================================================
--- trunk/include/asterisk/event.h (original)
+++ trunk/include/asterisk/event.h Sun May  3 09:28:59 2009
@@ -157,6 +157,20 @@
 	enum ast_event_ie_type ie_type, uint32_t uint);
 
 /*!
+ * \brief Append a bitflags parameter to a subscription
+ *
+ * \param sub the dynamic subscription allocated with ast_event_subscribe_new()
+ * \param ie_type the information element type for the parameter
+ * \param flags the flags that must be present in the event to match this subscription
+ *
+ * \retval 0 success
+ * \retval non-zero failure
+ * \since 1.6.3
+ */
+int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
+	enum ast_event_ie_type ie_type, uint32_t flags);
+
+/*!
  * \brief Append a string parameter to a subscription
  *
  * \param sub the dynamic subscription allocated with ast_event_subscribe_new()
@@ -446,6 +460,24 @@
 	uint32_t data);
 
 /*!
+ * \brief Append an information element that has a bitflags payload
+ *
+ * \param event the event that the IE will be appended to
+ * \param ie_type the type of IE to append
+ * \param flags the flags that are the payload of the IE
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ * \since 1.6.3
+ *
+ * The pointer to the event will get updated with the new location for the event
+ * that now contains the appended information element.  If the re-allocation of
+ * the memory for this event fails, it will be set to NULL.
+ */
+int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
+	uint32_t bitflags);
+
+/*!
  * \brief Append an information element that has a raw payload
  *
  * \param event the event that the IE will be appended to
@@ -476,6 +508,18 @@
 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type);
 
 /*!
+ * \brief Get the value of an information element that has a bitflags payload
+ *
+ * \param event The event to get the IE from
+ * \param ie_type the type of information element to retrieve
+ *
+ * \return This returns the payload of the information element with the given type.
+ *         However, an IE with a payload of 0, and the case where no IE is found
+ *         yield the same return value.
+ */
+uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+/*!
  * \brief Get the value of an information element that has a string payload
  *
  * \param event The event to get the IE from
@@ -614,13 +658,22 @@
 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator);
 
 /*!
- * \brief Get the value of the current IE in the ierator as an integer payload
+ * \brief Get the value of the current IE in the iterator as an integer payload
  *
  * \param iterator The iterator instance
  *
  * \return This returns the payload of the information element as a uint.
  */
 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator);
+
+/*!
+ * \brief Get the value of the current IE in the iterator as a bitflags payload
+ *
+ * \param iterator The iterator instance
+ *
+ * \return This returns the payload of the information element as bitflags.
+ */
+uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator);
 
 /*!
  * \brief Get the value of the current IE in the iterator as a string payload

Modified: trunk/include/asterisk/event_defs.h
URL: http://svn.digium.com/svn-view/asterisk/trunk/include/asterisk/event_defs.h?view=diff&rev=191919&r1=191918&r2=191919
==============================================================================
--- trunk/include/asterisk/event_defs.h (original)
+++ trunk/include/asterisk/event_defs.h Sun May  3 09:28:59 2009
@@ -137,6 +137,8 @@
 	AST_EVENT_IE_PLTYPE_STR,
 	/*! Raw data, compared with memcmp */
 	AST_EVENT_IE_PLTYPE_RAW,
+	/*! Bit flags (unsigned integer, compared using boolean logic) */
+	AST_EVENT_IE_PLTYPE_BITFLAGS,
 };
 
 /*!

Modified: trunk/main/event.c
URL: http://svn.digium.com/svn-view/asterisk/trunk/main/event.c?view=diff&rev=191919&r1=191918&r2=191919
==============================================================================
--- trunk/main/event.c (original)
+++ trunk/main/event.c Sun May  3 09:28:59 2009
@@ -311,6 +311,7 @@
 		ast_free(ie_val->payload.raw);
 		break;
 	case AST_EVENT_IE_PLTYPE_UINT:
+	case AST_EVENT_IE_PLTYPE_BITFLAGS:
 	case AST_EVENT_IE_PLTYPE_EXISTS:
 	case AST_EVENT_IE_PLTYPE_UNKNOWN:
 		break;
@@ -347,6 +348,9 @@
 		case AST_EVENT_IE_PLTYPE_UINT:
 			ie_value->payload.uint = va_arg(ap, uint32_t);
 			break;
+		case AST_EVENT_IE_PLTYPE_BITFLAGS:
+			ie_value->payload.uint = va_arg(ap, uint32_t);
+			break;
 		case AST_EVENT_IE_PLTYPE_STR:
 			ie_value->payload.str = va_arg(ap, const char *);
 			break;
@@ -391,6 +395,12 @@
 			switch (ie_val->ie_pltype) {
 			case AST_EVENT_IE_PLTYPE_UINT:
 				break_out = (ie_val->payload.uint != sub_ie_val->payload.uint);
+				break;
+			case AST_EVENT_IE_PLTYPE_BITFLAGS:
+				/* if the subscriber has requested *any* of the bitflags we are providing,
+				 * then it's a match
+				 */
+				break_out = (ie_val->payload.uint & sub_ie_val->payload.uint);
 				break;
 			case AST_EVENT_IE_PLTYPE_STR:
 				break_out = strcmp(ie_val->payload.str, sub_ie_val->payload.str);
@@ -436,14 +446,26 @@
 static int match_ie_val(const struct ast_event *event,
 		const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
 {
-	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT) {
+	switch (ie_val->ie_pltype) {
+	case AST_EVENT_IE_PLTYPE_UINT:
+	{
 		uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
-		if (val == ast_event_get_ie_uint(event, ie_val->ie_type))
-			return 1;
-		return 0;
-	}
-
-	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
+
+		return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
+	}
+
+	case AST_EVENT_IE_PLTYPE_BITFLAGS:
+	{
+		uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
+
+		/* if the subscriber has requested *any* of the bitflags that this event provides,
+		 * then it's a match
+		 */
+		return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
+	}
+
+	case AST_EVENT_IE_PLTYPE_STR:
+	{
 		const char *str;
 		uint32_t hash;
 
@@ -460,16 +482,19 @@
 		return 0;
 	}
 
-	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
+	case AST_EVENT_IE_PLTYPE_RAW:
+	{
 		const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
-		if (buf && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_val->raw_datalen))
-			return 1;
-		return 0;
-	}
-
-	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
-		if (ast_event_get_ie_raw(event, ie_val->ie_type))
-			return 1;
+
+		return (buf && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_val->raw_datalen)) ? 1 : 0;
+	}
+
+	case AST_EVENT_IE_PLTYPE_EXISTS:
+	{
+		return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
+	}
+
+	case AST_EVENT_IE_PLTYPE_UNKNOWN:
 		return 0;
 	}
 
@@ -527,6 +552,9 @@
 		case AST_EVENT_IE_PLTYPE_UINT:
 			ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
 			break;
+		case AST_EVENT_IE_PLTYPE_BITFLAGS:
+			ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
+			break;
 		case AST_EVENT_IE_PLTYPE_STR:
 			ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
 			break;
@@ -619,6 +647,26 @@
 	ie_val->ie_type = ie_type;
 	ie_val->payload.uint = unsigned_int;
 	ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
+
+	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
+
+	return 0;
+}
+
+int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
+	enum ast_event_ie_type ie_type, uint32_t flags)
+{
+	struct ast_event_ie_val *ie_val;
+
+	if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
+		return -1;
+
+	if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
+		return -1;
+
+	ie_val->ie_type = ie_type;
+	ie_val->payload.uint = flags;
+	ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
 
 	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
 
@@ -753,6 +801,12 @@
 			ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
 			break;
 		}
+		case AST_EVENT_IE_PLTYPE_BITFLAGS:
+		{
+			uint32_t unsigned_int = va_arg(ap, uint32_t);
+			ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
+			break;
+		}
 		case AST_EVENT_IE_PLTYPE_STR:
 		{
 			const char *str = va_arg(ap, const char *);
@@ -839,6 +893,11 @@
 	return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
 }
 
+uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
+{
+	return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
+}
+
 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
 {
 	const struct ast_event_ie_str_payload *str_payload;
@@ -859,6 +918,15 @@
 }
 
 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+	const uint32_t *ie_val;
+
+	ie_val = ast_event_get_ie_raw(event, ie_type);
+
+	return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
+}
+
+uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
 {
 	const uint32_t *ie_val;
 
@@ -919,6 +987,13 @@
 {
 	data = htonl(data);
 	return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
+}
+
+int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
+	uint32_t flags)
+{
+	flags = htonl(flags);
+	return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
 }
 
 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
@@ -974,6 +1049,9 @@
 		case AST_EVENT_IE_PLTYPE_UINT:
 			ie_value->payload.uint = va_arg(ap, uint32_t);
 			break;
+		case AST_EVENT_IE_PLTYPE_BITFLAGS:
+			ie_value->payload.uint = va_arg(ap, uint32_t);
+			break;
 		case AST_EVENT_IE_PLTYPE_STR:
 			ie_value->payload.str = va_arg(ap, const char *);
 			break;
@@ -1013,6 +1091,9 @@
 			break;
 		case AST_EVENT_IE_PLTYPE_UINT:
 			ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
+			break;
+		case AST_EVENT_IE_PLTYPE_BITFLAGS:
+			ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
 			break;
 		case AST_EVENT_IE_PLTYPE_RAW:
 			ast_event_append_ie_raw(&event, ie_val->ie_type,
@@ -1108,6 +1189,9 @@
 		case AST_EVENT_IE_PLTYPE_UINT:
 			ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
 			break;
+		case AST_EVENT_IE_PLTYPE_BITFLAGS:
+			ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
+			break;
 		case AST_EVENT_IE_PLTYPE_STR:
 			ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
 			break;




More information about the asterisk-commits mailing list