[asterisk-commits] russell: branch russell/events r65397 - in /team/russell/events: apps/ includ...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue May 22 06:25:51 MST 2007


Author: russell
Date: Tue May 22 08:25:50 2007
New Revision: 65397

URL: http://svn.digium.com/view/asterisk?view=rev&rev=65397
Log:
Commit my progress made during travel on converting device and extension states
to the common event system.

Modified:
    team/russell/events/apps/app_queue.c
    team/russell/events/include/asterisk.h
    team/russell/events/include/asterisk/devicestate.h
    team/russell/events/include/asterisk/event_defs.h
    team/russell/events/main/asterisk.c
    team/russell/events/main/devicestate.c
    team/russell/events/main/pbx.c

Modified: team/russell/events/apps/app_queue.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/apps/app_queue.c?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/apps/app_queue.c (original)
+++ team/russell/events/apps/app_queue.c Tue May 22 08:25:50 2007
@@ -92,6 +92,7 @@
 #include "asterisk/astdb.h"
 #include "asterisk/devicestate.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/event.h"
 
 enum {
 	QUEUE_STRATEGY_RINGALL = 0,
@@ -256,6 +257,9 @@
 
 /*! \brief queues.conf [general] option */
 static int montype_default = 0;
+
+/*! \brief Subscription to device state change events */
+static struct ast_event_sub *device_state_sub;
 
 enum queue_result {
 	QUEUE_UNKNOWN = 0,
@@ -656,7 +660,7 @@
 	return NULL;
 }
 
-static int statechange_queue(const char *dev, enum ast_device_state state, void *data)
+static int statechange_queue(const char *dev, enum ast_device_state state)
 {
 	/* Avoid potential for deadlocks by spawning a new thread to handle
 	   the event */
@@ -674,6 +678,22 @@
 	ast_mutex_unlock(&device_state.lock);
 
 	return 0;
+}
+
+static void device_state_cb(const struct ast_event *event, void *unused)
+{
+	enum ast_device_state state;
+	const char *device;
+
+	state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
+	device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
+
+	if (ast_strlen_zero(device)) {
+		ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
+		return;
+	}
+
+	statechange_queue(device, state);
 }
 
 static struct member *create_queue_member(const char *interface, const char *membername, int penalty, int paused)
@@ -4747,7 +4767,9 @@
 	res |= ast_custom_function_unregister(&queuemembercount_function);
 	res |= ast_custom_function_unregister(&queuememberlist_function);
 	res |= ast_custom_function_unregister(&queuewaitingcount_function);
-	ast_devstate_del(statechange_queue, NULL);
+
+	if (device_state_sub)
+		ast_event_unsubscribe(device_state_sub);
 
 	ast_module_user_hangup_all();
 
@@ -4788,7 +4810,9 @@
 	res |= ast_custom_function_register(&queuemembercount_function);
 	res |= ast_custom_function_register(&queuememberlist_function);
 	res |= ast_custom_function_register(&queuewaitingcount_function);
-	res |= ast_devstate_add(statechange_queue, NULL);
+
+	if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL, AST_EVENT_IE_END)))
+		res = -1;
 
 	return res;
 }

Modified: team/russell/events/include/asterisk.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk.h?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/include/asterisk.h (original)
+++ team/russell/events/include/asterisk.h Tue May 22 08:25:50 2007
@@ -87,6 +87,7 @@
 int dnsmgr_reload(void);			/*!< Provided by dnsmgr.c */
 void threadstorage_init(void);			/*!< Provided by threadstorage.c */
 void ast_event_init(void);          /*!< Provided by event.c */
+int ast_device_state_engine_init(void); /*!< Provided by devicestate.c */
 
 /* Many headers need 'ast_channel' to be defined */
 struct ast_channel;

Modified: team/russell/events/include/asterisk/devicestate.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/devicestate.h?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/include/asterisk/devicestate.h (original)
+++ team/russell/events/include/asterisk/devicestate.h Tue May 22 08:25:50 2007
@@ -19,6 +19,9 @@
 /*! \file
  * \brief Device state management
  *
+ * To subscribe to device state changes, use the generic ast_event_subscribe
+ * method.  For an example, see apps/app_queue.c.
+ *
  * \arg See \ref AstExtState
  */
 
@@ -29,7 +32,11 @@
 extern "C" {
 #endif
 
-/*! Device States */
+/*! Device States 
+ *  \note The order of these states may not change because they are included
+ *        in Asterisk events which may be transmitted across the network to
+ *        other servers.
+ */
 enum ast_device_state {
 	AST_DEVICE_UNKNOWN,      /*!< Device is valid but channel didn't know state */
 	AST_DEVICE_NOT_INUSE,    /*!< Device is not used */
@@ -41,9 +48,6 @@
 	AST_DEVICE_RINGINUSE,    /*!< Device is ringing *and* in use */
 	AST_DEVICE_ONHOLD,       /*!< Device is on hold */
 };
-
-/*! \brief Devicestate watcher call back */
-typedef int (*ast_devstate_cb_type)(const char *dev, enum ast_device_state state, void *data);
 
 /*!  \brief Devicestate provider call back */
 typedef enum ast_device_state (*ast_devstate_prov_cb_type)(const char *data);
@@ -93,7 +97,6 @@
 int ast_device_state_changed(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 
-
 /*! \brief Tells Asterisk the State for Device is changed 
  * \param device devicename like a dialstring
  * Asterisk polls the new extensionstates and calls the registered
@@ -101,22 +104,6 @@
  * Returns 0 on success, -1 on failure
  */
 int ast_device_state_changed_literal(const char *device);
-
-/*! \brief Registers a device state change callback 
- * \param callback Callback
- * \param data to pass to callback
- * The callback is called if the state for extension is changed
- * Return -1 on failure, ID on success
- */ 
-int ast_devstate_add(ast_devstate_cb_type callback, void *data);
-
-/*! \brief Unregisters a device state change callback 
- * \param callback Callback
- * \param data to pass to callback
- * The callback is called if the state for extension is changed
- * Return -1 on failure, ID on success
- */ 
-void ast_devstate_del(ast_devstate_cb_type callback, void *data);
 
 /*! \brief Add device state provider 
  * \param label to use in hint, like label:object
@@ -132,8 +119,6 @@
  */ 
 int ast_devstate_prov_del(const char *label);
 
-int ast_device_state_engine_init(void);
-
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/russell/events/include/asterisk/event_defs.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/event_defs.h?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/include/asterisk/event_defs.h (original)
+++ team/russell/events/include/asterisk/event_defs.h Tue May 22 08:25:50 2007
@@ -37,13 +37,15 @@
 	    unique to the event itself, not necessarily across all events. */
 	AST_EVENT_CUSTOM = 0x01,
 	/*! Voicemail message waiting indication */
-	AST_EVENT_MWI    = 0x02,
+	AST_EVENT_MWI          = 0x02,
 	/*! Someone has subscribed to events */
-	AST_EVENT_SUB    = 0x03,
+	AST_EVENT_SUB          = 0x03,
 	/*! Someone has unsubscribed from events */
-	AST_EVENT_UNSUB  = 0x04,
+	AST_EVENT_UNSUB        = 0x04,
+	/*! The state of a device has changed */
+	AST_EVENT_DEVICE_STATE = 0x05,
 	/*! Number of event types.  This should be the last event type + 1 */
-	AST_EVENT_TOTAL  = 0x05,
+	AST_EVENT_TOTAL        = 0x06,
 };
 
 /*! \brief Event Information Element types */
@@ -82,11 +84,25 @@
 	 */
 	AST_EVENT_IE_EVENTTYPE = 0x05,
 	/*!
-	 * \brief Hint that someone cares than an IE exists
+	 * \brief Hint that someone cares that an IE exists
 	 * Used by: AST_EVENT_SUB
 	 * Payload type: UINT (ast_event_ie_type)
 	 */
 	AST_EVENT_IE_EXISTS    = 0x06,
+	/*!
+	 * \brief Device Name
+	 * Used by AST_EVENT_DEVICE_STATE
+	 * Payload type: STR
+	 */
+	AST_EVENT_IE_DEVICE    = 0x07,
+	/*!
+	 * \brief Generic State IE
+	 * Used by AST_EVENT_DEVICE_STATE
+	 * Payload type: UINT
+	 * The actual state values depend on the event which
+	 * this IE is a part of.
+	 */
+	 AST_EVENT_IE_STATE    = 0x08,
 };
 
 /*!

Modified: team/russell/events/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/asterisk.c?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/main/asterisk.c (original)
+++ team/russell/events/main/asterisk.c Tue May 22 08:25:50 2007
@@ -2933,7 +2933,7 @@
 
 	threadstorage_init();
 
-	if (load_modules(1)) {		/* Load modules */
+	if (load_modules(1)) {		/* Load modules, pre-load only */
 		printf(term_quit());
 		exit(1);
 	}

Modified: team/russell/events/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/devicestate.c?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/main/devicestate.c (original)
+++ team/russell/events/main/devicestate.c Tue May 22 08:25:50 2007
@@ -126,6 +126,7 @@
 #include "asterisk/pbx.h"
 #include "asterisk/app.h"
 #include "asterisk/options.h"
+#include "asterisk/event.h"
 
 /*! \brief Device state strings for printing */
 static const char *devstatestring[] = {
@@ -149,16 +150,6 @@
 
 /*! \brief A list of providers */
 static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov);
-
-/*! \brief  A device state watcher (callback) */
-struct devstate_cb {
-	void *data;
-	ast_devstate_cb_type callback;	/*!< Where to report when state changes */
-	AST_RWLIST_ENTRY(devstate_cb) list;
-};
-
-/*! \brief A device state watcher list */
-static AST_RWLIST_HEAD_STATIC(devstate_cbs, devstate_cb);
 
 struct state_change {
 	AST_LIST_ENTRY(state_change) list;
@@ -380,58 +371,30 @@
 	return res;
 }
 
-/*! \brief Add device state watcher */
-int ast_devstate_add(ast_devstate_cb_type callback, void *data)
-{
-	struct devstate_cb *devcb;
-
-	if (!callback || !(devcb = ast_calloc(1, sizeof(*devcb))))
-		return -1;
-
-	devcb->data = data;
-	devcb->callback = callback;
-
-	AST_RWLIST_WRLOCK(&devstate_cbs);
-	AST_RWLIST_INSERT_HEAD(&devstate_cbs, devcb, list);
-	AST_RWLIST_UNLOCK(&devstate_cbs);
-
-	return 0;
-}
-
-/*! \brief Remove device state watcher */
-void ast_devstate_del(ast_devstate_cb_type callback, void *data)
-{
-	struct devstate_cb *devcb;
-
-	AST_RWLIST_WRLOCK(&devstate_cbs);
-	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_cbs, devcb, list) {
-		if ((devcb->callback == callback) && (devcb->data == data)) {
-			AST_RWLIST_REMOVE_CURRENT(&devstate_cbs, list);
-			free(devcb);
-			break;
-		}
-	}
-	AST_RWLIST_TRAVERSE_SAFE_END;
-	AST_RWLIST_UNLOCK(&devstate_cbs);
-}
-
 /*! \brief Notify callback watchers of change, and notify PBX core for hint updates
 	Normally executed within a separate thread
 */
 static void do_state_change(const char *device)
 {
-	int state;
-	struct devstate_cb *devcb;
+	enum ast_device_state state;
+	struct ast_event *event;
 
 	state = ast_device_state(device);
 	if (option_debug > 2)
 		ast_log(LOG_DEBUG, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
 
-	AST_RWLIST_RDLOCK(&devstate_cbs);
-	AST_RWLIST_TRAVERSE(&devstate_cbs, devcb, list)
-		devcb->callback(device, state, devcb->data);
-	AST_RWLIST_UNLOCK(&devstate_cbs);
-
+	if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE,
+			AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
+			AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
+			AST_EVENT_IE_END))) {
+		return;
+	}
+
+	ast_event_queue_and_cache(event,
+		AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
+		AST_EVENT_IE_END);
+
+	/*! \todo Convert hints to be device state subscribers. */
 	ast_hint_state_changed(device);
 }
 

Modified: team/russell/events/main/pbx.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/pbx.c?view=diff&rev=65397&r1=65396&r2=65397
==============================================================================
--- team/russell/events/main/pbx.c (original)
+++ team/russell/events/main/pbx.c Tue May 22 08:25:50 2007
@@ -63,6 +63,7 @@
 #include "asterisk/app.h"
 #include "asterisk/devicestate.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/event.h"
 
 /*!
  * \note I M P O R T A N T :
@@ -247,6 +248,9 @@
 static struct varshead globals = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
 
 static int autofallthrough = 1;
+
+/*! \brief Subscription for device state change events */
+static struct ast_event_sub *device_state_sub;
 
 AST_MUTEX_DEFINE_STATIC(maxcalllock);
 static int countcalls;
@@ -6050,6 +6054,22 @@
 	return res;
 }
 
+static void device_state_cb(const struct ast_event *event, void *unused)
+{
+	enum ast_device_state state;
+	const char *device;
+
+	state = ast_event_get_ie_uint(event, AST_EVENT_IE_STATE);
+	device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
+
+	if (ast_strlen_zero(device)) {
+		ast_log(LOG_ERROR, "Received invalid event that had no device IE\n");
+		return;
+	}
+
+	// XXX statechange_queue(device, state);
+}
+
 int load_pbx(void)
 {
 	int x;
@@ -6073,6 +6093,12 @@
 	
 	/* Register manager application */
 	ast_manager_register2("ShowDialPlan", EVENT_FLAG_CONFIG, manager_show_dialplan, "List dialplan", mandescr_show_dialplan);
+
+	if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL,
+			AST_EVENT_IE_END))) {
+		return -1;
+	}
+
 	return 0;
 }
 



More information about the asterisk-commits mailing list