[asterisk-commits] russell: branch russell/func_devstate r54173 - in /team/russell/func_devstate...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Feb 13 04:06:41 MST 2007


Author: russell
Date: Tue Feb 13 05:06:40 2007
New Revision: 54173

URL: http://svn.digium.com/view/asterisk?view=rev&rev=54173
Log:
The best time to get your feature request fulfilled is when you catch a
developer on IRC in the middle of the night that can't sleep.  :)

This introduces a new dialplan function, DEVSTATE, which allows you to do some
pretty cool things.

First, you can get the device state of anything in the dialplan:
  NoOp(SIP/mypeer has state ${DEVSTATE(SIP/mypeer)})
  NoOp(The conference room 1234 has state ${DEVSTATE(MeetMe:1234)})

Most importantly, this allows you to create custom device state so you can
control blinky lights directly from the dialplan.
  Set(DEVSTATE(Custom:mycustomlamp)=BUSY)
  ...
  exten => mycustomlamp,hint,Custom:mycustomlamp

Added:
    team/russell/func_devstate/funcs/func_devstate.c   (with props)
Modified:
    team/russell/func_devstate/apps/app_meetme.c
    team/russell/func_devstate/apps/app_queue.c
    team/russell/func_devstate/include/asterisk/devicestate.h
    team/russell/func_devstate/main/devicestate.c
    team/russell/func_devstate/res/res_features.c

Modified: team/russell/func_devstate/apps/app_meetme.c
URL: http://svn.digium.com/view/asterisk/team/russell/func_devstate/apps/app_meetme.c?view=diff&rev=54173&r1=54172&r2=54173
==============================================================================
--- team/russell/func_devstate/apps/app_meetme.c (original)
+++ team/russell/func_devstate/apps/app_meetme.c Tue Feb 13 05:06:40 2007
@@ -2867,7 +2867,7 @@
 }
 
 /*! \brief Callback for devicestate providers */
-static int meetmestate(const char *data)
+static enum ast_device_state meetmestate(const char *data)
 {
 	struct ast_conference *conf;
 
@@ -3540,12 +3540,12 @@
 	return 0;
 }
 
-static int sla_state(const char *data)
+static enum ast_device_state sla_state(const char *data)
 {
 	char *buf, *station_name, *trunk_name;
 	struct sla_station *station;
 	struct sla_trunk_ref *trunk_ref;
-	int res = AST_DEVICE_INVALID;
+	enum ast_device_state res = AST_DEVICE_INVALID;
 
 	trunk_name = buf = ast_strdupa(data);
 	station_name = strsep(&trunk_name, "_");

Modified: team/russell/func_devstate/apps/app_queue.c
URL: http://svn.digium.com/view/asterisk/team/russell/func_devstate/apps/app_queue.c?view=diff&rev=54173&r1=54172&r2=54173
==============================================================================
--- team/russell/func_devstate/apps/app_queue.c (original)
+++ team/russell/func_devstate/apps/app_queue.c Tue Feb 13 05:06:40 2007
@@ -612,7 +612,7 @@
 	return NULL;
 }
 
-static int statechange_queue(const char *dev, int state, void *ign)
+static int statechange_queue(const char *dev, enum ast_device_state state, void *ign)
 {
 	/* Avoid potential for deadlocks by spawning a new thread to handle
 	   the event */

Added: team/russell/func_devstate/funcs/func_devstate.c
URL: http://svn.digium.com/view/asterisk/team/russell/func_devstate/funcs/func_devstate.c?view=auto&rev=54173
==============================================================================
--- team/russell/func_devstate/funcs/func_devstate.c (added)
+++ team/russell/func_devstate/funcs/func_devstate.c Tue Feb 13 05:06:40 2007
@@ -1,0 +1,137 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com> 
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Manually controlled blinky lights
+ *
+ * \author Russell Bryant <russell at digium.com> 
+ *
+ * \ingroup functions
+ *
+ * \note Props go out to Ahrimanes in #asterisk for requesting this at 4:30 AM
+ *       when I couldn't sleep.  :)
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdlib.h>
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/devicestate.h"
+
+struct custom_device {
+	int state;
+	AST_RWLIST_ENTRY(custom_device) entry;
+	char name[1];
+};
+
+static AST_RWLIST_HEAD_STATIC(custom_devices, custom_device);
+
+static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	ast_copy_string(buf, devstate2str(ast_device_state(data)), len);
+
+	return 0;
+}
+
+static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+	struct custom_device *dev;
+	int len = strlen("Custom:");
+
+	if (!strncasecmp(data, "Custom:", len)) {
+		ast_log(LOG_WARNING, "The DEVSTATE function can only be used to set 'Custom:' device state!\n");
+		return -1;
+	}
+	data += len;
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "DEVSTATE function called with no custom device name!\n");
+		return -1;
+	}
+
+	AST_RWLIST_WRLOCK(&custom_devices);
+	AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
+		if (!strcasecmp(dev->name, data))
+			break;
+	}
+	if (!dev) {
+		if (!(dev = ast_calloc(1, sizeof(*dev) + strlen(data) + 1))) {
+			AST_RWLIST_UNLOCK(&custom_devices);
+			return -1;
+		}
+		strcpy(dev->name, data);
+		AST_RWLIST_INSERT_HEAD(&custom_devices, dev, entry);
+	}
+	dev->state = ast_devstate_val(data);
+	ast_device_state_changed("Custom:%s", dev->name);
+	AST_RWLIST_UNLOCK(&custom_devices);
+
+	return 0;
+}
+
+static enum ast_device_state custom_devstate_callback(const char *data)
+{
+	struct custom_device *dev;
+	enum ast_device_state state = AST_DEVICE_UNKNOWN;
+
+	AST_RWLIST_RDLOCK(&custom_devices);
+	AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
+		if (!strcasecmp(dev->name, data))
+			state = dev->state;	
+	}
+	AST_RWLIST_UNLOCK(&custom_devices);
+
+	return state;
+}
+
+static struct ast_custom_function devstate_function = {
+	.name = "DEVSTATE",
+	.synopsis = "Get or Set a device state",
+	.syntax = "DEVSTATE(device)",
+	.read = devstate_read,
+	.write = devstate_write,
+};
+
+static int unload_module(void)
+{
+	int res = 0;
+
+	res |= ast_custom_function_unregister(&devstate_function);
+	ast_devstate_prov_del("Custom");
+
+	return res;
+}
+
+static int load_module(void)
+{
+	int res = 0;
+
+	res |= ast_custom_function_register(&devstate_function);
+	res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
+
+	return res;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Device state dialplan functions");

Propchange: team/russell/func_devstate/funcs/func_devstate.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/func_devstate/funcs/func_devstate.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/func_devstate/funcs/func_devstate.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/russell/func_devstate/include/asterisk/devicestate.h
URL: http://svn.digium.com/view/asterisk/team/russell/func_devstate/include/asterisk/devicestate.h?view=diff&rev=54173&r1=54172&r2=54173
==============================================================================
--- team/russell/func_devstate/include/asterisk/devicestate.h (original)
+++ team/russell/func_devstate/include/asterisk/devicestate.h Tue Feb 13 05:06:40 2007
@@ -28,29 +28,37 @@
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
 #endif
-/*! @name DeviceStates */
-/*! \@{ */
-#define AST_DEVICE_UNKNOWN	0 /*!< Device is valid but channel didn't know state */
-#define AST_DEVICE_NOT_INUSE	1 /*!< Device is not used */
-#define AST_DEVICE_INUSE	2 /*!< Device is in use */
-#define AST_DEVICE_BUSY		3 /*!< Device is busy */
-#define AST_DEVICE_INVALID	4 /*!< Device is invalid */
-#define AST_DEVICE_UNAVAILABLE	5 /*!< Device is unavailable */
-#define AST_DEVICE_RINGING	6 /*!< Device is ringing */
-#define AST_DEVICE_RINGINUSE	7 /*!< Device is ringing *and* in use */
-#define AST_DEVICE_ONHOLD	8 /*!< Device is on hold */
-/*! \@} */
+
+/*! Device States */
+enum ast_device_state {
+	AST_DEVICE_UNKNOWN,      /*!< Device is valid but channel didn't know state */
+	AST_DEVICE_NOT_INUSE,    /*!< Device is not used */
+	AST_DEVICE_INUSE,        /*!< Device is in use */
+	AST_DEVICE_BUSY,         /*!< Device is busy */
+	AST_DEVICE_INVALID,      /*!< Device is invalid */
+	AST_DEVICE_UNAVAILABLE,  /*!< Device is unavailable */
+	AST_DEVICE_RINGING,      /*!< Device is ringing */
+	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, int state, void *data);
+typedef int (*ast_devstate_cb_type)(const char *dev, enum ast_device_state state, void *data);
 
 /*!  \brief Devicestate provider call back */
-typedef int (*ast_devstate_prov_cb_type)(const char *data);
+typedef enum ast_device_state (*ast_devstate_prov_cb_type)(const char *data);
 
 /*! \brief Convert device state to text string for output 
  * \param devstate Current device state 
  */
-const char *devstate2str(int devstate);
+const char *devstate2str(enum ast_device_state devstate);
+
+/*! \brief Convert device state from text to integer value
+ * \param 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
  * \param device like a dialstring
@@ -59,7 +67,7 @@
  * Returns an AST_DEVICE_UNKNOWN if no channel found or
  * AST_DEVICE_INUSE if a channel is found
  */
-int ast_parse_device_state(const char *device);
+enum ast_device_state ast_parse_device_state(const char *device);
 
 /*! \brief Asks a channel for device state
  * \param device like a dialstring
@@ -69,7 +77,7 @@
  * active channels list for the device.
  * Returns an AST_DEVICE_??? state -1 on failure
  */
-int ast_device_state(const char *device);
+enum ast_device_state ast_device_state(const char *device);
 
 /*! \brief Tells Asterisk the State for Device is changed
  * \param fmt devicename like a dialstring with format parameters

Modified: team/russell/func_devstate/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/func_devstate/main/devicestate.c?view=diff&rev=54173&r1=54172&r2=54173
==============================================================================
--- team/russell/func_devstate/main/devicestate.c (original)
+++ team/russell/func_devstate/main/devicestate.c Tue Feb 13 05:06:40 2007
@@ -179,9 +179,31 @@
 static int getproviderstate(const char *provider, const char *address);
 
 /*! \brief Find devicestate as text message for output */
-const char *devstate2str(int devstate) 
+const char *devstate2str(enum ast_device_state devstate) 
 {
 	return devstatestring[devstate];
+}
+
+enum ast_device_state ast_devstate_val(const char *val)
+{
+	if (!strcasecmp(val, "NOT_INUSE"))
+		return AST_DEVICE_NOT_INUSE;
+	else if (!strcasecmp(val, "INUSE"))
+		return AST_DEVICE_INUSE;
+	else if (!strcasecmp(val, "BUSY"))
+		return AST_DEVICE_BUSY;
+	else if (!strcasecmp(val, "INVALID"))
+		return AST_DEVICE_INVALID;
+	else if (!strcasecmp(val, "UNAVAILABLE"))
+		return AST_DEVICE_UNAVAILABLE;
+	else if (!strcasecmp(val, "RINGING"))
+		return AST_DEVICE_RINGING;
+	else if (!strcasecmp(val, "RINGINUSE"))
+		return AST_DEVICE_RINGINUSE;
+	else if (!strcasecmp(val, "ONHOLD"))
+		return AST_DEVICE_ONHOLD;
+
+	return AST_DEVICE_UNKNOWN;
 }
 
 /*! \brief Find out if device is active in a call or not 
@@ -189,11 +211,11 @@
 	This function is only used for channels that does not implement 
 	devicestate natively
 */
-int ast_parse_device_state(const char *device)
+enum ast_device_state ast_parse_device_state(const char *device)
 {
 	struct ast_channel *chan;
 	char match[AST_CHANNEL_NAME];
-	int res;
+	enum ast_device_state res;
 
 	ast_copy_string(match, device, sizeof(match)-1);
 	strcat(match, "-");
@@ -213,12 +235,12 @@
 }
 
 /*! \brief Check device state through channel specific function or generic function */
-int ast_device_state(const char *device)
+enum ast_device_state ast_device_state(const char *device)
 {
 	char *buf;
 	char *number;
 	const struct ast_channel_tech *chan_tech;
-	int res = 0;
+	enum ast_device_state res = AST_DEVICE_UNKNOWN;
 	/*! \brief Channel driver that provides device state */
 	char *tech;
 	/*! \brief Another provider of device state */

Modified: team/russell/func_devstate/res/res_features.c
URL: http://svn.digium.com/view/asterisk/team/russell/func_devstate/res/res_features.c?view=diff&rev=54173&r1=54172&r2=54173
==============================================================================
--- team/russell/func_devstate/res/res_features.c (original)
+++ team/russell/func_devstate/res/res_features.c Tue Feb 13 05:06:40 2007
@@ -284,9 +284,9 @@
 }
 
 /*! \brief metermaids callback from devicestate.c */
-static int metermaidstate(const char *data)
-{
-	int res = AST_DEVICE_INVALID;
+static enum ast_device_state metermaidstate(const char *data)
+{
+	enum ast_device_state res = AST_DEVICE_INVALID;
 	char *context = ast_strdupa(data);
 	char *exten;
 
@@ -299,7 +299,7 @@
 
 	res = ast_exists_extension(NULL, context, exten, 1, NULL);
 
-	if (!res)
+	if (res == AST_DEVICE_UNKNOWN)
 		return AST_DEVICE_NOT_INUSE;
 	else
 		return AST_DEVICE_INUSE;



More information about the asterisk-commits mailing list