[asterisk-commits] russell: trunk r121501 - in /trunk: include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jun 10 09:06:30 CDT 2008


Author: russell
Date: Tue Jun 10 09:06:29 2008
New Revision: 121501

URL: http://svn.digium.com/view/asterisk?view=rev&rev=121501
Log:
Merge another change from team/russell/events

This commit breaks out some logic from pbx.c into a simple API.  The hint
processing code had logic for taking the state from multiple devices and
turning that into the state for a single extension.  So, I broke this out
and made an API that lets you take multiple device states and determine
the aggregate device state.  I needed this for some core device state changes
to support distributed device state.

Modified:
    trunk/include/asterisk/devicestate.h
    trunk/main/devicestate.c
    trunk/main/pbx.c

Modified: trunk/include/asterisk/devicestate.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/devicestate.h?view=diff&rev=121501&r1=121500&r2=121501
==============================================================================
--- trunk/include/asterisk/devicestate.h (original)
+++ trunk/include/asterisk/devicestate.h Tue Jun 10 09:06:29 2008
@@ -196,6 +196,54 @@
  */ 
 int ast_devstate_prov_del(const char *label);
 
+/*!
+ * \brief An object to hold state when calculating aggregate device state
+ */
+struct ast_devstate_aggregate;
+
+/*!
+ * \brief Initialize aggregate device state
+ *
+ * \arg agg the state object
+ *
+ * \return nothing
+ */
+void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg);
+
+/*!
+ * \brief Add a device state to the aggregate device state
+ *
+ * \arg agg the state object
+ * \arg state the state to add
+ *
+ * \return nothing
+ */
+void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state);
+
+/*!
+ * \brief Get the aggregate device state result
+ *
+ * \arg agg the state object
+ *
+ * \return the aggregate device state after adding some number of device states.
+ */
+enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg);
+
+/*! 
+ * \brief You shouldn't care about the contents of this struct 
+ *
+ * This struct is only here so that it can be easily declared on the stack.
+ */
+struct ast_devstate_aggregate {
+	unsigned int all_unavail:1;
+	unsigned int all_busy:1;
+	unsigned int all_free:1;
+	unsigned int all_on_hold:1;
+	unsigned int busy:1;
+	unsigned int in_use:1;
+	unsigned int ring:1;
+};
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: trunk/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/trunk/main/devicestate.c?view=diff&rev=121501&r1=121500&r2=121501
==============================================================================
--- trunk/main/devicestate.c (original)
+++ trunk/main/devicestate.c Tue Jun 10 09:06:29 2008
@@ -551,3 +551,91 @@
 
 	return 0;
 }
+
+void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
+{
+	memset(agg, 0, sizeof(*agg));
+
+	agg->all_unavail = 1;
+	agg->all_busy = 1;
+	agg->all_free = 1;
+	agg->all_on_hold = 1;
+}
+
+void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
+{
+	switch (state) {
+	case AST_DEVICE_NOT_INUSE:
+		agg->all_unavail = 0;
+		agg->all_busy = 0;
+		agg->all_on_hold = 0;
+		break;
+	case AST_DEVICE_INUSE:
+		agg->in_use = 1;
+		agg->all_busy = 0;
+		agg->all_unavail = 0;
+		agg->all_free = 0;
+		agg->all_on_hold = 0;
+		break;
+	case AST_DEVICE_RINGING:
+		agg->ring = 1;
+		agg->all_busy = 0;
+		agg->all_unavail = 0;
+		agg->all_free = 0;
+		agg->all_on_hold = 0;
+		break;
+	case AST_DEVICE_RINGINUSE:
+		agg->in_use = 1;
+		agg->ring = 1;
+		agg->all_busy = 0;
+		agg->all_unavail = 0;
+		agg->all_free = 0;
+		agg->all_on_hold = 0;
+		break;
+	case AST_DEVICE_ONHOLD:
+		agg->all_unavail = 0;
+		agg->all_free = 0;
+		break;
+	case AST_DEVICE_BUSY:
+		agg->all_unavail = 0;
+		agg->all_free = 0;
+		agg->all_on_hold = 0;
+		agg->busy = 1;
+		break;
+	case AST_DEVICE_UNAVAILABLE:
+	case AST_DEVICE_INVALID:
+		agg->all_busy = 0;
+		agg->all_free = 0;
+		agg->all_on_hold = 0;
+		break;
+	case AST_DEVICE_UNKNOWN:
+		break;
+	}
+}
+
+enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
+{
+	if (agg->all_free)
+		return AST_DEVICE_NOT_INUSE;
+	
+	if (agg->all_on_hold)
+		return AST_DEVICE_ONHOLD;
+	
+	if (agg->all_busy)
+		return AST_DEVICE_BUSY;
+
+	if (agg->all_unavail)
+		return AST_DEVICE_UNAVAILABLE;
+	
+	if (agg->ring)
+		return agg->in_use ? AST_DEVICE_RINGINUSE : AST_DEVICE_RINGING;
+
+	if (agg->in_use)
+		return AST_DEVICE_INUSE;
+
+	if (agg->busy)
+		return AST_DEVICE_BUSY;
+	
+	return AST_DEVICE_NOT_INUSE;
+}
+

Modified: trunk/main/pbx.c
URL: http://svn.digium.com/view/asterisk/trunk/main/pbx.c?view=diff&rev=121501&r1=121500&r2=121501
==============================================================================
--- trunk/main/pbx.c (original)
+++ trunk/main/pbx.c Tue Jun 10 09:06:29 2008
@@ -2798,7 +2798,7 @@
 					} else
 						ast_log(LOG_ERROR, "Unable to allocate bogus channel for variable substitution.  Function results may be blank.\n");
 				}
-				ast_debug(1, "Function result is '%s'\n", cp4 ? cp4 : "(null)");
+				ast_debug(2, "Function result is '%s'\n", cp4 ? cp4 : "(null)");
 			} else {
 				/* Retrieve variable value */
 				pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp);
@@ -3041,82 +3041,41 @@
 {
 	char hint[AST_MAX_EXTENSION] = "";
 	char *cur, *rest;
-	int allunavailable = 1, allbusy = 1, allfree = 1, allonhold = 1;
-	int busy = 0, inuse = 0, ring = 0;
+	struct ast_devstate_aggregate agg;
+	enum ast_device_state state;
 
 	if (!e)
 		return -1;
 
+	ast_devstate_aggregate_init(&agg);
+
 	ast_copy_string(hint, ast_get_extension_app(e), sizeof(hint));
 
 	rest = hint;	/* One or more devices separated with a & character */
-	while ( (cur = strsep(&rest, "&")) ) {
-		int res = ast_device_state(cur);
-		switch (res) {
-		case AST_DEVICE_NOT_INUSE:
-			allunavailable = 0;
-			allbusy = 0;
-			allonhold = 0;
-			break;
-		case AST_DEVICE_INUSE:
-			inuse = 1;
-			allunavailable = 0;
-			allfree = 0;
-			allonhold = 0;
-			break;
-		case AST_DEVICE_RINGING:
-			ring = 1;
-			allunavailable = 0;
-			allfree = 0;
-			allonhold = 0;
-			break;
-		case AST_DEVICE_RINGINUSE:
-			inuse = 1;
-			ring = 1;
-			allunavailable = 0;
-			allfree = 0;
-			allonhold = 0;
-			break;
-		case AST_DEVICE_ONHOLD:
-			allunavailable = 0;
-			allfree = 0;
-			break;
-		case AST_DEVICE_BUSY:
-			allunavailable = 0;
-			allfree = 0;
-			allonhold = 0;
-			busy = 1;
-			break;
-		case AST_DEVICE_UNAVAILABLE:
-		case AST_DEVICE_INVALID:
-			allbusy = 0;
-			allfree = 0;
-			allonhold = 0;
-			break;
-		default:
-			allunavailable = 0;
-			allbusy = 0;
-			allfree = 0;
-			allonhold = 0;
-		}
-	}
-
-	if (!inuse && ring)
+
+	while ( (cur = strsep(&rest, "&")) )
+		ast_devstate_aggregate_add(&agg, ast_device_state(cur));
+
+	state = ast_devstate_aggregate_result(&agg);
+
+	switch (state) {
+	case AST_DEVICE_ONHOLD:
+		return AST_EXTENSION_ONHOLD;
+	case AST_DEVICE_BUSY:
+		return AST_EXTENSION_BUSY;
+	case AST_DEVICE_UNAVAILABLE:
+		return AST_EXTENSION_UNAVAILABLE;
+	case AST_DEVICE_RINGINUSE:
+		return (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING);
+	case AST_DEVICE_RINGING:
 		return AST_EXTENSION_RINGING;
-	if (inuse && ring)
-		return (AST_EXTENSION_INUSE | AST_EXTENSION_RINGING);
-	if (inuse)
+	case AST_DEVICE_INUSE:
 		return AST_EXTENSION_INUSE;
-	if (allfree)
+	case AST_DEVICE_UNKNOWN:
+	case AST_DEVICE_INVALID:
+	case AST_DEVICE_NOT_INUSE:
 		return AST_EXTENSION_NOT_INUSE;
-	if (allonhold)
-		return AST_EXTENSION_ONHOLD;
-	if (allbusy)
-		return AST_EXTENSION_BUSY;
-	if (allunavailable)
-		return AST_EXTENSION_UNAVAILABLE;
-	if (busy)
-		return AST_EXTENSION_INUSE;
+	}
 
 	return AST_EXTENSION_NOT_INUSE;
 }




More information about the asterisk-commits mailing list