Wed Apr 6 11:30:04 2011

Asterisk developer's documentation


func_global.c File Reference

Global variable dialplan functions. More...

#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
#include "asterisk/manager.h"

Go to the source code of this file.

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int global_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int global_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int load_module (void)
static int shared_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static void shared_variable_free (void *data)
static int shared_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Variable dialplan functions" , .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_DEFAULT, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function global_function
static struct ast_custom_function shared_function
static struct ast_datastore_info shared_variable_info


Detailed Description

Global variable dialplan functions.

Author:
Tilghman Lesher <func_global__200605@the-tilghman.com>

Definition in file func_global.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 296 of file func_global.c.

static void __unreg_module ( void   )  [static]

Definition at line 296 of file func_global.c.

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

Definition at line 103 of file func_global.c.

References ast_copy_string(), pbx_builtin_getvar_helper(), and var.

00104 {
00105    const char *var = pbx_builtin_getvar_helper(NULL, data);
00106 
00107    *buf = '\0';
00108 
00109    if (var)
00110       ast_copy_string(buf, var, len);
00111 
00112    return 0;
00113 }

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

Definition at line 115 of file func_global.c.

References pbx_builtin_setvar_helper().

00116 {
00117    pbx_builtin_setvar_helper(NULL, data, value);
00118 
00119    return 0;
00120 }

static int load_module ( void   )  [static]

Definition at line 286 of file func_global.c.

References ast_custom_function_register, global_function, and shared_function.

00287 {
00288    int res = 0;
00289 
00290    res |= ast_custom_function_register(&global_function);
00291    res |= ast_custom_function_register(&shared_function);
00292 
00293    return res;
00294 }

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

Definition at line 128 of file func_global.c.

References args, AST_APP_ARG, ast_channel_datastore_find(), ast_channel_get_by_name(), ast_channel_get_by_name_prefix(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, ast_copy_string(), AST_DECLARE_APP_ARGS, AST_LIST_TRAVERSE, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_var_name(), ast_var_value(), ast_datastore::data, LOG_ERROR, LOG_WARNING, prefix, shared_variable_info, var, and ast_channel::varshead.

00129 {
00130    struct ast_datastore *varstore;
00131    struct varshead *varshead;
00132    struct ast_var_t *var;
00133    AST_DECLARE_APP_ARGS(args,
00134       AST_APP_ARG(var);
00135       AST_APP_ARG(chan);
00136    );
00137    struct ast_channel *c_ref = NULL;
00138 
00139    if (ast_strlen_zero(data)) {
00140       ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
00141       return -1;
00142    }
00143 
00144    AST_STANDARD_APP_ARGS(args, data);
00145 
00146    if (!ast_strlen_zero(args.chan)) {
00147       char *prefix = alloca(strlen(args.chan) + 2);
00148       sprintf(prefix, "%s-", args.chan);
00149       if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
00150          ast_log(LOG_ERROR, "Channel '%s' not found!  Variable '%s' will be blank.\n", args.chan, args.var);
00151          return -1;
00152       }
00153       chan = c_ref;
00154    }
00155 
00156    ast_channel_lock(chan);
00157 
00158    if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
00159       ast_channel_unlock(chan);
00160       if (c_ref) {
00161          c_ref = ast_channel_unref(c_ref);
00162       }
00163       return -1;
00164    }
00165 
00166    varshead = varstore->data;
00167    *buf = '\0';
00168 
00169    /* Protected by the channel lock */
00170    AST_LIST_TRAVERSE(varshead, var, entries) {
00171       if (!strcmp(args.var, ast_var_name(var))) {
00172          ast_copy_string(buf, ast_var_value(var), len);
00173          break;
00174       }
00175    }
00176 
00177    ast_channel_unlock(chan);
00178 
00179    if (c_ref) {
00180       c_ref = ast_channel_unref(c_ref);
00181    }
00182 
00183    return 0;
00184 }

static void shared_variable_free ( void *  data  )  [static]

Definition at line 92 of file func_global.c.

References ast_free, AST_LIST_REMOVE_HEAD, ast_var_delete(), ast_var_t::entries, and var.

00093 {
00094    struct varshead *varshead = data;
00095    struct ast_var_t *var;
00096 
00097    while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
00098       ast_var_delete(var);
00099    }
00100    ast_free(varshead);
00101 }

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

Definition at line 186 of file func_global.c.

References args, AST_APP_ARG, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_get_by_name(), ast_channel_get_by_name_prefix(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, ast_datastore_alloc, ast_datastore_free(), AST_DECLARE_APP_ARGS, AST_LIST_INSERT_HEAD, AST_LIST_REMOVE, AST_LIST_TRAVERSE, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_var_assign(), ast_var_delete(), ast_var_name(), ast_datastore::data, EVENT_FLAG_DIALPLAN, LOG_ERROR, LOG_WARNING, manager_event, prefix, S_OR, shared_variable_info, var, and ast_channel::varshead.

00187 {
00188    struct ast_datastore *varstore;
00189    struct varshead *varshead;
00190    struct ast_var_t *var;
00191    AST_DECLARE_APP_ARGS(args,
00192       AST_APP_ARG(var);
00193       AST_APP_ARG(chan);
00194    );
00195    struct ast_channel *c_ref = NULL;
00196 
00197    if (ast_strlen_zero(data)) {
00198       ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
00199       return -1;
00200    }
00201 
00202    AST_STANDARD_APP_ARGS(args, data);
00203 
00204    if (!ast_strlen_zero(args.chan)) {
00205       char *prefix = alloca(strlen(args.chan) + 2);
00206       sprintf(prefix, "%s-", args.chan);
00207       if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
00208          ast_log(LOG_ERROR, "Channel '%s' not found!  Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
00209          return -1;
00210       }
00211       chan = c_ref;
00212    }
00213 
00214    ast_channel_lock(chan);
00215 
00216    if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
00217       if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
00218          ast_log(LOG_ERROR, "Unable to allocate new datastore.  Shared variable not set.\n");
00219          ast_channel_unlock(chan);
00220          if (c_ref) {
00221             c_ref = ast_channel_unref(c_ref);
00222          }
00223          return -1;
00224       }
00225 
00226       if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
00227          ast_log(LOG_ERROR, "Unable to allocate variable structure.  Shared variable not set.\n");
00228          ast_datastore_free(varstore);
00229          ast_channel_unlock(chan);
00230          if (c_ref) {
00231             c_ref = ast_channel_unref(c_ref);
00232          }
00233          return -1;
00234       }
00235 
00236       varstore->data = varshead;
00237       ast_channel_datastore_add(chan, varstore);
00238    }
00239    varshead = varstore->data;
00240 
00241    /* Protected by the channel lock */
00242    AST_LIST_TRAVERSE(varshead, var, entries) {
00243       /* If there's a previous value, remove it */
00244       if (!strcmp(args.var, ast_var_name(var))) {
00245          AST_LIST_REMOVE(varshead, var, entries);
00246          ast_var_delete(var);
00247          break;
00248       }
00249    }
00250 
00251    var = ast_var_assign(args.var, S_OR(value, ""));
00252    AST_LIST_INSERT_HEAD(varshead, var, entries);
00253    manager_event(EVENT_FLAG_DIALPLAN, "VarSet", 
00254       "Channel: %s\r\n"
00255       "Variable: SHARED(%s)\r\n"
00256       "Value: %s\r\n"
00257       "Uniqueid: %s\r\n", 
00258       chan ? chan->name : "none", args.var, value, 
00259       chan ? chan->uniqueid : "none");
00260 
00261    ast_channel_unlock(chan);
00262 
00263    if (c_ref) {
00264       c_ref = ast_channel_unref(c_ref);
00265    }
00266 
00267    return 0;
00268 }

static int unload_module ( void   )  [static]

Definition at line 276 of file func_global.c.

References ast_custom_function_unregister(), global_function, and shared_function.

00277 {
00278    int res = 0;
00279 
00280    res |= ast_custom_function_unregister(&global_function);
00281    res |= ast_custom_function_unregister(&shared_function);
00282 
00283    return res;
00284 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Variable dialplan functions" , .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_DEFAULT, } [static]

Definition at line 296 of file func_global.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 296 of file func_global.c.

struct ast_custom_function global_function [static]

Initial value:

 {
   .name = "GLOBAL",
   .read = global_read,
   .write = global_write,
}

Definition at line 122 of file func_global.c.

Referenced by load_module(), and unload_module().

struct ast_custom_function shared_function [static]

Initial value:

 {
   .name = "SHARED",
   .read = shared_read,
   .write = shared_write,
}

Definition at line 270 of file func_global.c.

Referenced by load_module(), and unload_module().

struct ast_datastore_info shared_variable_info [static]

Initial value:

 {
   .type = "SHARED_VARIABLES",
   .destroy = shared_variable_free,
}

Definition at line 87 of file func_global.c.

Referenced by shared_read(), and shared_write().


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