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/stasis_channels.h"

Include dependency graph for func_global.c:

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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
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 333 of file func_global.c.

static void __unreg_module ( void   )  [static]

Definition at line 333 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 125 of file func_global.c.

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

00126 {
00127    const char *var = pbx_builtin_getvar_helper(NULL, data);
00128 
00129    *buf = '\0';
00130 
00131    if (var)
00132       ast_copy_string(buf, var, len);
00133 
00134    return 0;
00135 }

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

Definition at line 137 of file func_global.c.

References NULL, and pbx_builtin_setvar_helper().

00138 {
00139    pbx_builtin_setvar_helper(NULL, data, value);
00140 
00141    return 0;
00142 }

static int load_module ( void   )  [static]

Definition at line 323 of file func_global.c.

References ast_custom_function_register.

00324 {
00325    int res = 0;
00326 
00327    res |= ast_custom_function_register(&global_function);
00328    res |= ast_custom_function_register(&shared_function);
00329 
00330    return res;
00331 }

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

Definition at line 150 of file func_global.c.

References args, ast_alloca, 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, NULL, prefix, and var.

00151 {
00152    struct ast_datastore *varstore;
00153    struct varshead *varshead;
00154    struct ast_var_t *var;
00155    AST_DECLARE_APP_ARGS(args,
00156       AST_APP_ARG(var);
00157       AST_APP_ARG(chan);
00158    );
00159    struct ast_channel *c_ref = NULL;
00160 
00161    if (ast_strlen_zero(data)) {
00162       ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
00163       return -1;
00164    }
00165 
00166    AST_STANDARD_APP_ARGS(args, data);
00167 
00168    if (!ast_strlen_zero(args.chan)) {
00169       char *prefix = ast_alloca(strlen(args.chan) + 2);
00170       sprintf(prefix, "%s-", args.chan);
00171       if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
00172          ast_log(LOG_ERROR, "Channel '%s' not found!  Variable '%s' will be blank.\n", args.chan, args.var);
00173          return -1;
00174       }
00175       chan = c_ref;
00176    } else if (!chan) {
00177       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00178       return -1;
00179    }
00180 
00181    ast_channel_lock(chan);
00182 
00183    if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
00184       ast_channel_unlock(chan);
00185       if (c_ref) {
00186          c_ref = ast_channel_unref(c_ref);
00187       }
00188       return -1;
00189    }
00190 
00191    varshead = varstore->data;
00192    *buf = '\0';
00193 
00194    /* Protected by the channel lock */
00195    AST_LIST_TRAVERSE(varshead, var, entries) {
00196       if (!strcmp(args.var, ast_var_name(var))) {
00197          ast_copy_string(buf, ast_var_value(var), len);
00198          break;
00199       }
00200    }
00201 
00202    ast_channel_unlock(chan);
00203 
00204    if (c_ref) {
00205       c_ref = ast_channel_unref(c_ref);
00206    }
00207 
00208    return 0;
00209 }

static void shared_variable_free ( void *  data  )  [static]

Definition at line 114 of file func_global.c.

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

00115 {
00116    struct varshead *varshead = data;
00117    struct ast_var_t *var;
00118 
00119    while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
00120       ast_var_delete(var);
00121    }
00122    ast_free(varshead);
00123 }

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

Definition at line 211 of file func_global.c.

References args, ast_alloca, 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_publish_varset(), ast_channel_unlock, ast_channel_unref, ast_datastore_alloc, ast_datastore_free(), AST_DECLARE_APP_ARGS, ast_free, AST_LIST_INSERT_HEAD, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_log, ast_malloc, AST_STANDARD_APP_ARGS, ast_strlen_zero, ast_var_assign(), ast_var_delete(), ast_var_name(), ast_datastore::data, len(), LOG_ERROR, LOG_WARNING, NULL, prefix, RAII_VAR, S_OR, and var.

00212 {
00213    struct ast_datastore *varstore;
00214    struct varshead *varshead;
00215    struct ast_var_t *var;
00216    AST_DECLARE_APP_ARGS(args,
00217       AST_APP_ARG(var);
00218       AST_APP_ARG(chan);
00219    );
00220    struct ast_channel *c_ref = NULL;
00221    int len;
00222    RAII_VAR(char *, shared_buffer, NULL, ast_free);
00223 
00224    if (ast_strlen_zero(data)) {
00225       ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
00226       return -1;
00227    }
00228 
00229    AST_STANDARD_APP_ARGS(args, data);
00230 
00231    if (!ast_strlen_zero(args.chan)) {
00232       char *prefix = ast_alloca(strlen(args.chan) + 2);
00233       sprintf(prefix, "%s-", args.chan);
00234       if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
00235          ast_log(LOG_ERROR, "Channel '%s' not found!  Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
00236          return -1;
00237       }
00238       chan = c_ref;
00239    } else if (!chan) {
00240       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00241       return -1;
00242    }
00243 
00244    len = 9 + strlen(args.var); /* SHARED() + var */
00245    shared_buffer = ast_malloc(len);
00246    if (!shared_buffer) {
00247       if (c_ref) {
00248          ast_channel_unref(c_ref);
00249       }
00250       return -1;
00251    }
00252 
00253    ast_channel_lock(chan);
00254 
00255    if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
00256       if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
00257          ast_log(LOG_ERROR, "Unable to allocate new datastore.  Shared variable not set.\n");
00258          ast_channel_unlock(chan);
00259          if (c_ref) {
00260             c_ref = ast_channel_unref(c_ref);
00261          }
00262          return -1;
00263       }
00264 
00265       if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
00266          ast_log(LOG_ERROR, "Unable to allocate variable structure.  Shared variable not set.\n");
00267          ast_datastore_free(varstore);
00268          ast_channel_unlock(chan);
00269          if (c_ref) {
00270             c_ref = ast_channel_unref(c_ref);
00271          }
00272          return -1;
00273       }
00274 
00275       varstore->data = varshead;
00276       ast_channel_datastore_add(chan, varstore);
00277    }
00278    varshead = varstore->data;
00279 
00280    /* Protected by the channel lock */
00281    AST_LIST_TRAVERSE_SAFE_BEGIN(varshead, var, entries) {
00282       /* If there's a previous value, remove it */
00283       if (!strcmp(args.var, ast_var_name(var))) {
00284          AST_LIST_REMOVE_CURRENT(entries);
00285          ast_var_delete(var);
00286          break;
00287       }
00288    }
00289    AST_LIST_TRAVERSE_SAFE_END;
00290 
00291    if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
00292       AST_LIST_INSERT_HEAD(varshead, var, entries);
00293 
00294       sprintf(shared_buffer, "SHARED(%s)", args.var);
00295       ast_channel_publish_varset(chan, shared_buffer, value);
00296    }
00297 
00298    ast_channel_unlock(chan);
00299 
00300    if (c_ref) {
00301       c_ref = ast_channel_unref(c_ref);
00302    }
00303 
00304    return 0;
00305 }

static int unload_module ( void   )  [static]

Definition at line 313 of file func_global.c.

References ast_custom_function_unregister().

00314 {
00315    int res = 0;
00316 
00317    res |= ast_custom_function_unregister(&global_function);
00318    res |= ast_custom_function_unregister(&shared_function);
00319 
00320    return res;
00321 }


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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, } [static]

Definition at line 333 of file func_global.c.

Definition at line 333 of file func_global.c.

Initial value:

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

Definition at line 144 of file func_global.c.

Initial value:

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

Definition at line 307 of file func_global.c.

Initial value:

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

Definition at line 109 of file func_global.c.


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