[asterisk-commits] russell: branch russell/events r73845 - in /team/russell/events: include/aste...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Jul 7 23:29:51 CDT 2007


Author: russell
Date: Sat Jul  7 23:29:50 2007
New Revision: 73845

URL: http://svn.digium.com/view/asterisk?view=rev&rev=73845
Log:
Add the ability to be able to include the state of a device when calling one of
the devstate functions to indicate that the state of a device has changed.
This is a much more efficient way to handle things, because it removes the need
for another thread to have to go back and call the device state provider's
callback to figure out what the state changed to.

Modified:
    team/russell/events/include/asterisk/devicestate.h
    team/russell/events/main/devicestate.c

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=73845&r1=73844&r2=73845
==============================================================================
--- team/russell/events/include/asterisk/devicestate.h (original)
+++ team/russell/events/include/asterisk/devicestate.h Sat Jul  7 23:29:50 2007
@@ -58,75 +58,142 @@
 	AST_DEVICE_ONHOLD,       /*!< Device is on hold */
 };
 
-/*!  \brief Devicestate provider call back */
+/*! \brief Devicestate provider call back */
 typedef enum ast_device_state (*ast_devstate_prov_cb_type)(const char *data);
 
-/*! \brief Convert device state to text string for output 
+/*! 
+ * \brief Convert device state to text string for output 
+ *
  * \param devstate Current device state 
  */
 const char *devstate2str(enum ast_device_state devstate);
 
-/*! \brief Convert device state to text string that is easier to parse 
+/*! 
+ * \brief Convert device state to text string that is easier to parse 
+ *
  * \param devstate Current device state 
  */
 const char *ast_devstate_str(enum ast_device_state devstate);
 
-/*! \brief Convert device state from text to integer value
+/*! 
+ * \brief Convert device state from text to integer value
+ *
  * \param val The text representing the device state.  Valid values are anything
  *        that comes after AST_DEVICE_ in one of the defined values.
+ *
  * \return The AST_DEVICE_ integer value
  */
 enum ast_device_state ast_devstate_val(const char *val);
 
-/*! \brief Search the Channels by Name
+/*! 
+ * \brief Search the Channels by Name
+ *
  * \param device like a dial string
+ *
  * Search the Device in active channels by compare the channel name against 
  * the device name. Compared are only the first chars to the first '-' char.
+ *
  * \retval AST_DEVICE_UNKNOWN if no channel found
  * \retval AST_DEVICE_INUSE if a channel is found
  */
 enum ast_device_state ast_parse_device_state(const char *device);
 
-/*! \brief Asks a channel for device state
+/*! 
+ * \brief Asks a channel for device state
+ *
  * \param device like a dial string
+ *
  * Asks a channel for device state, data is normally a number from a dial string
  * used by the low level module
  * Tries the channel device state callback if not supported search in the
  * active channels list for the device.
+ *
  * \retval an AST_DEVICE_??? state 
  * \retval -1 on failure
  */
 enum ast_device_state ast_device_state(const char *device);
 
-/*! \brief Tells Asterisk the State for Device is changed
+/*! 
+ * \brief Tells Asterisk the State for Device is changed
+ *
+ * \param state the new state of the device
  * \param fmt device name like a dial string with format parameters
+ *
+ * The new state of the device will be sent off to any subscribers
+ * of device states.  It will also be stored in the internal event
+ * cache.
+ *
+ * \retval 0 on success 
+ * \retval -1 on failure
+ *
+ * \note This is deprecated in favor of ast_devstate_changed()
+ */
+int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
+	__attribute__ ((format (printf, 2, 3)));
+
+/*! 
+ * \brief Tells Asterisk the State for Device is changed
+ *
+ * \param state the new state of the device
+ * \param fmt device name like a dial string with format parameters
+ *
+ * The new state of the device will be sent off to any subscribers
+ * of device states.  It will also be stored in the internal event
+ * cache.
+ *
+ * \retval 0 on success 
+ * \retval -1 on failure
+ *
+ * \note This is deprecated in favor of ast_devstate_changed()
+ */
+int ast_devstate_changed_literal(enum ast_device_state state, const char *device);
+
+/*! 
+ * \brief Tells Asterisk the State for Device is changed
+ *
+ * \param fmt device name like a dial string with format parameters
+ *
  * Asterisk polls the new extension states and calls the registered
  * callbacks for the changed extensions
- * \retval 0 on success 
- * \retval -1 on failure
+ *
+ * \retval 0 on success 
+ * \retval -1 on failure
+ *
+ * \note This is deprecated in favor of ast_devstate_changed()
  */
 int ast_device_state_changed(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
 
 /*! \brief Tells Asterisk the State for Device is changed 
+ *
  * \param device device name like a dial string
+ *
  * Asterisk polls the new extension states and calls the registered
  * callbacks for the changed extensions
- * \retval 0 on success 
- * \retval -1 on failure
+ *
+ * \retval 0 on success 
+ * \retval -1 on failure
+ *
+ * \note This is deprecated in favor of ast_devstate_changed_literal()
  */
 int ast_device_state_changed_literal(const char *device);
 
-/*! \brief Add device state provider 
+/*! 
+ * \brief Add device state provider 
+ *
  * \param label to use in hint, like label:object
  * \param callback Callback
+ *
  * \retval -1 failure
  * \retval 0 success
  */ 
 int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback);
 
-/*! \brief Remove device state provider 
+/*! 
+ * \brief Remove device state provider 
+ *
  * \param label to use in hint, like label:object
+ *
  * \retval -1 on failure 
  * \retval 0 on success
  */ 

Modified: team/russell/events/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/devicestate.c?view=diff&rev=73845&r1=73844&r2=73845
==============================================================================
--- team/russell/events/main/devicestate.c (original)
+++ team/russell/events/main/devicestate.c Sat Jul  7 23:29:50 2007
@@ -393,16 +393,9 @@
 	return res;
 }
 
-/*! \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)
-{
-	enum ast_device_state state;
+static void devstate_event(const char *device, enum ast_device_state state)
+{
 	struct ast_event *event;
-
-	state = ast_device_state(device);
-	ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
 
 	if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE,
 			AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
@@ -416,7 +409,20 @@
 		AST_EVENT_IE_END);
 }
 
-static int __ast_device_state_changed_literal(char *buf)
+/*! Called by the state change thread to find out what the state is, and then
+ *  to queue up the state change event */
+static void do_state_change(const char *device)
+{
+	enum ast_device_state state;
+
+	state = ast_device_state(device);
+
+	ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
+
+	devstate_event(device, state);
+}
+
+static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf)
 {
 	char *device;
 	struct state_change *change;
@@ -429,8 +435,10 @@
 	tmp = strrchr(device, '-');
 	if (tmp)
 		*tmp = '\0';
-	
-	if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
+
+	if (state != AST_DEVICE_UNKNOWN) {
+		devstate_event(device, state);
+	} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
 		/* we could not allocate a change struct, or */
 		/* there is no background thread, so process the change now */
 		do_state_change(device);
@@ -446,15 +454,25 @@
 	return 1;
 }
 
+int ast_devstate_changed_literal(enum ast_device_state state, const char *dev)
+{
+	char *buf;
+
+	buf = ast_strdupa(dev);
+
+	return __ast_devstate_changed_literal(state, buf);
+}
+
 int ast_device_state_changed_literal(const char *dev)
 {
 	char *buf;
+
 	buf = ast_strdupa(dev);
-	return __ast_device_state_changed_literal(buf);
-}
-
-/*! \brief Accept change notification, add it to change queue */
-int ast_device_state_changed(const char *fmt, ...) 
+
+	return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
+}
+
+int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...) 
 {
 	char buf[AST_MAX_EXTENSION];
 	va_list ap;
@@ -462,7 +480,21 @@
 	va_start(ap, fmt);
 	vsnprintf(buf, sizeof(buf), fmt, ap);
 	va_end(ap);
-	return __ast_device_state_changed_literal(buf);
+
+	return __ast_devstate_changed_literal(state, buf);
+}
+
+/*! \brief Accept change notification, add it to change queue */
+int ast_device_state_changed(const char *fmt, ...) 
+{
+	char buf[AST_MAX_EXTENSION];
+	va_list ap;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	va_end(ap);
+
+	return __ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, buf);
 }
 
 /*! \brief Go through the dev state change queue and update changes in the dev state thread */




More information about the asterisk-commits mailing list