Add a "devstate change" CLI command to control custom device states. Also,
authorRussell Bryant <russell@russellbryant.com>
Sat, 1 Mar 2008 00:53:25 +0000 (00:53 +0000)
committerRussell Bryant <russell@russellbryant.com>
Sat, 1 Mar 2008 00:53:25 +0000 (00:53 +0000)
do some additional code cleanup and improvement in passing.

(closes issue #12106)
Reported by: nizon
Patches:
      devstate-patch.txt uploaded by nizon (license 415)
        -- Updated to trunk, and tab completion added by me

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@105461 65c4cc65-6c06-0410-ace0-fbb531ad65f3

CHANGES
funcs/func_devstate.c

diff --git a/CHANGES b/CHANGES
index 56e536f..0c0a8d9 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -97,6 +97,9 @@ CLI Changes
   * New CLI command: "core set chanvar" to set a channel variable from the CLI.
   * Added an easy way to execute Asterisk CLI commands at startup.  Any commands
     listed in the startup_commands section of cli.conf will get executed.
+  * Added a CLI command, "devstate change", which allows you to set custom device
+     states from the func_devstate module that provides the DEVICE_STATE() function
+     and handling of the "Custom:" devices.
 
 SIP changes
 -----------
@@ -166,7 +169,7 @@ Skinny changes
   * Proper codec support in chan_skinny.
   * Added settings for IP and Ethernet QoS requests
 
-MGCP changes
+
 ------------
   * Added separate settings for media QoS in mgcp.conf
 
index 220fd50..e809287 100644 (file)
@@ -54,6 +54,7 @@ static int devstate_read(struct ast_channel *chan, const char *cmd, char *data,
 static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
 {
        size_t len = strlen("Custom:");
+       enum ast_device_state state_val;
 
        if (strncasecmp(data, "Custom:", len)) {
                ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
@@ -65,9 +66,16 @@ static int devstate_write(struct ast_channel *chan, const char *cmd, char *data,
                return -1;
        }
 
+       state_val = ast_devstate_val(value);
+
+       if (state_val == AST_DEVICE_UNKNOWN) {
+               ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
+               return -1;
+       }
+
        ast_db_put(astdb_family, data, value);
 
-       ast_devstate_changed(ast_devstate_val(value), "Custom:%s", data);
+       ast_devstate_changed(state_val, "Custom:%s", data);
 
        return 0;
 }
@@ -215,9 +223,73 @@ static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct a
        return CLI_SUCCESS;
 }
 
+static char *handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+    size_t len;
+       const char *dev, *state;
+       enum ast_device_state state_val;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "devstate change";
+               e->usage =
+                       "Usage: devstate change <device> <state>\n"
+                       "       Change a custom device to a new state.\n"
+                       "       The possible values for the state are:\n"
+                       "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
+                       "RINGINUSE | ONHOLD\n",
+                       "\n"
+                       "Examples:\n"
+                       "       devstate change Custom:mystate1 INUSE\n"
+                       "       devstate change Custom:mystate1 NOT_INUSE\n"
+                       "       \n";
+               return NULL;
+       case CLI_GENERATE:
+       {
+               static char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
+                       "UNAVAILALBE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
+
+               if (a->pos == e->args + 1)
+                       return ast_cli_complete(a->word, cmds, a->n);
+
+               return NULL;
+       }
+       }
+
+       if (a->argc != e->args + 2)
+               return CLI_SHOWUSAGE;
+
+       len = strlen("Custom:");
+       dev = a->argv[e->args];
+       state = a->argv[e->args + 1];
+
+       if (strncasecmp(dev, "Custom:", len)) {
+               ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
+               return CLI_FAILURE;
+       }
+
+       dev += len;
+       if (ast_strlen_zero(dev))
+               return CLI_SHOWUSAGE;
+
+       state_val = ast_devstate_val(state);
+
+       if (state_val == AST_DEVICE_UNKNOWN)
+               return CLI_SHOWUSAGE;
+
+       ast_cli(a->fd, "Changing %s to %s\n", dev, state);
+
+       ast_db_put(astdb_family, dev, state);
+
+       ast_devstate_changed(state_val, "Custom:%s", dev);
+
+       return CLI_SUCCESS;
+}
+
 static struct ast_cli_entry cli_funcdevstate_list_deprecated = AST_CLI_DEFINE(handle_cli_funcdevstate_list, "List currently known custom device states");
 static struct ast_cli_entry cli_funcdevstate[] = {
        AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states", .deprecate_cmd = &cli_funcdevstate_list_deprecated),
+       AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
 };
 
 static struct ast_custom_function devstate_function = {