func_devstate.c File Reference

Manually controlled blinky lights. More...

#include "asterisk.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"
#include "asterisk/cli.h"
#include "asterisk/astdb.h"
#include "asterisk/app.h"

Include dependency graph for func_devstate.c:

Go to the source code of this file.

Enumerations

enum  { HINT_OPT_NAME = (1 << 0) }

Functions

static void __reg_module (void)
static void __unreg_module (void)
static enum ast_device_state custom_devstate_callback (const char *data)
static int devstate_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int devstate_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
static char * handle_cli_devstate_change (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_devstate_list (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int hint_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Gets or sets a device state in the dialplan" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PROVIDER, }
static struct ast_module_infoast_module_info = &__mod_info
static const char astdb_family [] = "CustomDevstate"
static struct ast_cli_entry cli_funcdevstate []
static struct ast_custom_function devstate_function
static struct ast_custom_function hint_function
static struct ast_app_option hint_options [128] = { [ 'n' ] = { .flag = HINT_OPT_NAME }, }


Detailed Description

Manually controlled blinky lights.

Author:
Russell Bryant <russell@digium.com>
Todo:
Delete the entry from AstDB when set to nothing like Set(DEVICE_STATE(Custom:lamp1)=)
Note:
Props go out to Ahrimanes in #asterisk for requesting this at 4:30 AM when I couldn't sleep. :)

Definition in file func_devstate.c.


Enumeration Type Documentation

anonymous enum

Enumerator:
HINT_OPT_NAME 

Definition at line 140 of file func_devstate.c.

00140      {
00141    HINT_OPT_NAME = (1 << 0),
00142 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 362 of file func_devstate.c.

static void __unreg_module ( void   )  [static]

Definition at line 362 of file func_devstate.c.

static enum ast_device_state custom_devstate_callback ( const char *  data  )  [static]

Definition at line 186 of file func_devstate.c.

References ast_db_get(), ast_devstate_val(), and buf.

Referenced by load_module().

00187 {
00188    char buf[256] = "";
00189 
00190    /* Ignore check_return warning from Coverity fow ast_db_get below */
00191    ast_db_get(astdb_family, data, buf, sizeof(buf));
00192 
00193    return ast_devstate_val(buf);
00194 }

static int devstate_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 104 of file func_devstate.c.

References ast_copy_string(), and ast_devstate_str().

00105 {
00106    ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
00107 
00108    return 0;
00109 }

static int devstate_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Definition at line 111 of file func_devstate.c.

References ast_db_put(), AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_val(), ast_log, ast_strlen_zero, len(), LOG_ERROR, and LOG_WARNING.

00112 {
00113    size_t len = strlen("Custom:");
00114    enum ast_device_state state_val;
00115 
00116    if (strncasecmp(data, "Custom:", len)) {
00117       ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
00118       return -1;
00119    }
00120    data += len;
00121    if (ast_strlen_zero(data)) {
00122       ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
00123       return -1;
00124    }
00125 
00126    state_val = ast_devstate_val(value);
00127 
00128    if (state_val == AST_DEVICE_UNKNOWN) {
00129       ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
00130       return -1;
00131    }
00132 
00133    ast_db_put(astdb_family, data, value);
00134 
00135    ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data);
00136 
00137    return 0;
00138 }

static char* handle_cli_devstate_change ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 240 of file func_devstate.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_cli_complete(), ast_db_put(), AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_val(), ast_strlen_zero, CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, cmds, ast_cli_entry::command, ast_cli_args::fd, len(), ast_cli_args::n, NULL, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

00241 {
00242     size_t len;
00243    const char *dev, *state;
00244    enum ast_device_state state_val;
00245 
00246    switch (cmd) {
00247    case CLI_INIT:
00248       e->command = "devstate change";
00249       e->usage =
00250          "Usage: devstate change <device> <state>\n"
00251          "       Change a custom device to a new state.\n"
00252          "       The possible values for the state are:\n"
00253          "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
00254          "RINGINUSE | ONHOLD\n"
00255          "\n"
00256          "Examples:\n"
00257          "       devstate change Custom:mystate1 INUSE\n"
00258          "       devstate change Custom:mystate1 NOT_INUSE\n"
00259          "       \n";
00260       return NULL;
00261    case CLI_GENERATE:
00262    {
00263       static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
00264                        "UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
00265 
00266       if (a->pos == e->args + 1)
00267          return ast_cli_complete(a->word, cmds, a->n);
00268 
00269       return NULL;
00270    }
00271    }
00272 
00273    if (a->argc != e->args + 2)
00274       return CLI_SHOWUSAGE;
00275 
00276    len = strlen("Custom:");
00277    dev = a->argv[e->args];
00278    state = a->argv[e->args + 1];
00279 
00280    if (strncasecmp(dev, "Custom:", len)) {
00281       ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
00282       return CLI_FAILURE;
00283    }
00284 
00285    dev += len;
00286    if (ast_strlen_zero(dev))
00287       return CLI_SHOWUSAGE;
00288 
00289    state_val = ast_devstate_val(state);
00290 
00291    if (state_val == AST_DEVICE_UNKNOWN)
00292       return CLI_SHOWUSAGE;
00293 
00294    ast_cli(a->fd, "Changing %s to %s\n", dev, state);
00295 
00296    ast_db_put(astdb_family, dev, state);
00297 
00298    ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev);
00299 
00300    return CLI_SUCCESS;
00301 }

static char* handle_cli_devstate_list ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 196 of file func_devstate.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli(), ast_db_freetree(), ast_db_gettree(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_db_entry::data, ast_cli_args::fd, ast_db_entry::key, ast_db_entry::next, NULL, and ast_cli_entry::usage.

00197 {
00198    struct ast_db_entry *db_entry, *db_tree;
00199 
00200    switch (cmd) {
00201    case CLI_INIT:
00202       e->command = "devstate list";
00203       e->usage =
00204          "Usage: devstate list\n"
00205          "       List all custom device states that have been set by using\n"
00206          "       the DEVICE_STATE dialplan function.\n";
00207       return NULL;
00208    case CLI_GENERATE:
00209       return NULL;
00210    }
00211 
00212    if (a->argc != e->args)
00213       return CLI_SHOWUSAGE;
00214 
00215    ast_cli(a->fd, "\n"
00216            "---------------------------------------------------------------------\n"
00217            "--- Custom Device States --------------------------------------------\n"
00218            "---------------------------------------------------------------------\n"
00219            "---\n");
00220 
00221    db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
00222    for (; db_entry; db_entry = db_entry->next) {
00223       const char *dev_name = strrchr(db_entry->key, '/') + 1;
00224       if (dev_name <= (const char *) 1)
00225          continue;
00226       ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
00227                      "---\n", dev_name, db_entry->data);
00228    }
00229    ast_db_freetree(db_tree);
00230    db_tree = NULL;
00231 
00232    ast_cli(a->fd,
00233            "---------------------------------------------------------------------\n"
00234            "---------------------------------------------------------------------\n"
00235            "\n");
00236 
00237    return CLI_SUCCESS;
00238 }

static int hint_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 148 of file func_devstate.c.

References args, AST_APP_ARG, ast_app_parse_options(), AST_DECLARE_APP_ARGS, ast_get_hint(), ast_log, AST_STANDARD_APP_ARGS, ast_strlen_zero, ast_test_flag, context, exten, HINT_OPT_NAME, hint_options, LOG_WARNING, NULL, and strsep().

00149 {
00150    char *exten, *context;
00151    AST_DECLARE_APP_ARGS(args,
00152       AST_APP_ARG(exten);
00153       AST_APP_ARG(options);
00154    );
00155    struct ast_flags opts = { 0, };
00156    int res;
00157 
00158    if (ast_strlen_zero(data)) {
00159       ast_log(LOG_WARNING, "The HINT function requires an extension\n");
00160       return -1;
00161    }
00162 
00163    AST_STANDARD_APP_ARGS(args, data);
00164 
00165    if (ast_strlen_zero(args.exten)) {
00166       ast_log(LOG_WARNING, "The HINT function requires an extension\n");
00167       return -1;
00168    }
00169 
00170    context = exten = args.exten;
00171    strsep(&context, "@");
00172    if (ast_strlen_zero(context))
00173       context = "default";
00174 
00175    if (!ast_strlen_zero(args.options))
00176       ast_app_parse_options(hint_options, &opts, NULL, args.options);
00177 
00178    if (ast_test_flag(&opts, HINT_OPT_NAME))
00179       res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
00180    else
00181       res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
00182 
00183    return !res; /* ast_get_hint returns non-zero on success */
00184 }

static int load_module ( void   )  [static]

Definition at line 331 of file func_devstate.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_custom_function_register, ast_db_freetree(), ast_db_gettree(), AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_prov_add(), ast_devstate_val(), custom_devstate_callback(), ast_db_entry::data, ast_db_entry::key, ast_db_entry::next, and NULL.

00332 {
00333    int res = 0;
00334    struct ast_db_entry *db_entry, *db_tree;
00335 
00336    /* Populate the device state cache on the system with all of the currently
00337     * known custom device states. */
00338    db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
00339    for (; db_entry; db_entry = db_entry->next) {
00340       const char *dev_name = strrchr(db_entry->key, '/') + 1;
00341       if (dev_name <= (const char *) 1)
00342          continue;
00343       ast_devstate_changed(ast_devstate_val(db_entry->data),
00344          AST_DEVSTATE_CACHABLE, "Custom:%s\n", dev_name);
00345    }
00346    ast_db_freetree(db_tree);
00347    db_tree = NULL;
00348 
00349    res |= ast_custom_function_register(&devstate_function);
00350    res |= ast_custom_function_register(&hint_function);
00351    res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
00352    res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
00353 
00354    return res;
00355 }

static int unload_module ( void   )  [static]

Definition at line 319 of file func_devstate.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_custom_function_unregister(), and ast_devstate_prov_del().

00320 {
00321    int res = 0;
00322 
00323    res |= ast_custom_function_unregister(&devstate_function);
00324    res |= ast_custom_function_unregister(&hint_function);
00325    res |= ast_devstate_prov_del("Custom");
00326    res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
00327 
00328    return res;
00329 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Gets or sets a device state in the dialplan" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PROVIDER, } [static]

Definition at line 362 of file func_devstate.c.

Definition at line 362 of file func_devstate.c.

const char astdb_family[] = "CustomDevstate" [static]

Definition at line 102 of file func_devstate.c.

struct ast_cli_entry cli_funcdevstate[] [static]

Initial value:

 {
   AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"),
   AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
}

Definition at line 303 of file func_devstate.c.

Initial value:

 {
   .name = "DEVICE_STATE",
   .read = devstate_read,
   .write = devstate_write,
}

Definition at line 308 of file func_devstate.c.

Initial value:

 {
   .name = "HINT",
   .read = hint_read,
}

Definition at line 314 of file func_devstate.c.

struct ast_app_option hint_options[128] = { [ 'n' ] = { .flag = HINT_OPT_NAME }, } [static]

Definition at line 146 of file func_devstate.c.

Referenced by hint_read().


Generated on Thu Apr 16 06:31:15 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6