Wed Apr 6 11:30:04 2011

Asterisk developer's documentation


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"

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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .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 136 of file func_devstate.c.

00136      {
00137    HINT_OPT_NAME = (1 << 0),
00138 };


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 356 of file func_devstate.c.

static void __unreg_module ( void   )  [static]

Definition at line 356 of file func_devstate.c.

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

Definition at line 182 of file func_devstate.c.

References ast_db_get(), and ast_devstate_val().

Referenced by load_module().

00183 {
00184    char buf[256] = "";
00185 
00186    ast_db_get(astdb_family, data, buf, sizeof(buf));
00187 
00188    return ast_devstate_val(buf);
00189 }

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

Definition at line 100 of file func_devstate.c.

References ast_copy_string(), ast_device_state(), and ast_devstate_str().

00101 {
00102    ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
00103 
00104    return 0;
00105 }

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

Definition at line 107 of file func_devstate.c.

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

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

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

Definition at line 235 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_state(), AST_DEVICE_UNKNOWN, ast_devstate_changed(), ast_devstate_val(), ast_strlen_zero(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, len(), ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

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

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

Definition at line 191 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, and ast_cli_entry::usage.

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

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

Definition at line 144 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, and strsep().

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

static int load_module ( void   )  [static]

Definition at line 326 of file func_devstate.c.

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

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

static int unload_module ( void   )  [static]

Definition at line 314 of file func_devstate.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_custom_function_unregister(), ast_devstate_prov_del(), cli_funcdevstate, devstate_function, and hint_function.

00315 {
00316    int res = 0;
00317 
00318    res |= ast_custom_function_unregister(&devstate_function);
00319    res |= ast_custom_function_unregister(&hint_function);
00320    res |= ast_devstate_prov_del("Custom");
00321    res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
00322 
00323    return res;
00324 }


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 = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PROVIDER, } [static]

Definition at line 356 of file func_devstate.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 356 of file func_devstate.c.

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

Definition at line 98 of file func_devstate.c.

struct ast_cli_entry cli_funcdevstate[] [static]

Initial value:

 {
   { .handler =  handle_cli_devstate_list , .summary =  "List currently known custom device states" ,__VA_ARGS__ },
   { .handler =  handle_cli_devstate_change , .summary =  "Change a custom device state" ,__VA_ARGS__ },
}

Definition at line 298 of file func_devstate.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function devstate_function [static]

Initial value:

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

Definition at line 303 of file func_devstate.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function hint_function [static]

Initial value:

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

Definition at line 309 of file func_devstate.c.

Referenced by load_module(), and unload_module().

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

Definition at line 142 of file func_devstate.c.

Referenced by hint_read().


Generated on Wed Apr 6 11:30:04 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7