Fri Jul 15 2011 12:01:53

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"
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_DEFAULT , .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, }
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 273 of file func_global.c.

static void __unreg_module ( void  ) [static]

Definition at line 273 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.

{
   const char *var = pbx_builtin_getvar_helper(NULL, data);

   *buf = '\0';

   if (var)
      ast_copy_string(buf, var, len);

   return 0;
}
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().

{
   pbx_builtin_setvar_helper(NULL, data, value);

   return 0;
}
static int load_module ( void  ) [static]

Definition at line 263 of file func_global.c.

References ast_custom_function_register.

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 AST_APP_ARG, ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_get_channel_by_name_locked(), ast_get_channel_by_name_prefix_locked(), AST_LIST_TRAVERSE, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_var_name(), ast_var_value(), ast_datastore::data, ast_var_t::entries, LOG_ERROR, LOG_WARNING, prefix, and var.

{
   struct ast_datastore *varstore;
   struct varshead *varshead;
   struct ast_var_t *var;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(var);
      AST_APP_ARG(chan);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
      return -1;
   }

   AST_STANDARD_APP_ARGS(args, data);

   if (!ast_strlen_zero(args.chan)) {
      char *prefix = alloca(strlen(args.chan) + 2);
      sprintf(prefix, "%s-", args.chan);
      if (!(chan = ast_get_channel_by_name_locked(args.chan)) && !(chan = ast_get_channel_by_name_prefix_locked(prefix, strlen(prefix)))) {
         ast_log(LOG_ERROR, "Channel '%s' not found!  Variable '%s' will be blank.\n", args.chan, args.var);
         return -1;
      }
   } else
      ast_channel_lock(chan);

   if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
      ast_channel_unlock(chan);
      return -1;
   }

   varshead = varstore->data;
   *buf = '\0';

   /* Protected by the channel lock */
   AST_LIST_TRAVERSE(varshead, var, entries) {
      if (!strcmp(args.var, ast_var_name(var))) {
         ast_copy_string(buf, ast_var_value(var), len);
         break;
      }
   }

   ast_channel_unlock(chan);

   return 0;
}
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.

{
   struct varshead *varshead = data;
   struct ast_var_t *var;

   while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
      ast_var_delete(var);
   }
   ast_free(varshead);
}
static int shared_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Definition at line 176 of file func_global.c.

References AST_APP_ARG, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc(), ast_datastore_free(), AST_DECLARE_APP_ARGS, ast_get_channel_by_name_locked(), ast_get_channel_by_name_prefix_locked(), 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, ast_var_t::entries, EVENT_FLAG_DIALPLAN, LOG_ERROR, LOG_WARNING, manager_event, ast_channel::name, prefix, S_OR, ast_channel::uniqueid, and var.

{
   struct ast_datastore *varstore;
   struct varshead *varshead;
   struct ast_var_t *var;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(var);
      AST_APP_ARG(chan);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
      return -1;
   }

   AST_STANDARD_APP_ARGS(args, data);

   if (!ast_strlen_zero(args.chan)) {
      char *prefix = alloca(strlen(args.chan) + 2);
      sprintf(prefix, "%s-", args.chan);
      if (!(chan = ast_get_channel_by_name_locked(args.chan)) && !(chan = ast_get_channel_by_name_prefix_locked(prefix, strlen(prefix)))) {
         ast_log(LOG_ERROR, "Channel '%s' not found!  Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
         return -1;
      }
   } else
      ast_channel_lock(chan);

   if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
      if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
         ast_log(LOG_ERROR, "Unable to allocate new datastore.  Shared variable not set.\n");
         ast_channel_unlock(chan);
         return -1;
      }

      if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
         ast_log(LOG_ERROR, "Unable to allocate variable structure.  Shared variable not set.\n");
         ast_datastore_free(varstore);
         ast_channel_unlock(chan);
         return -1;
      }

      varstore->data = varshead;
      ast_channel_datastore_add(chan, varstore);
   }
   varshead = varstore->data;

   /* Protected by the channel lock */
   AST_LIST_TRAVERSE(varshead, var, entries) {
      /* If there's a previous value, remove it */
      if (!strcmp(args.var, ast_var_name(var))) {
         AST_LIST_REMOVE(varshead, var, entries);
         ast_var_delete(var);
         break;
      }
   }

   var = ast_var_assign(args.var, S_OR(value, ""));
   AST_LIST_INSERT_HEAD(varshead, var, entries);
   manager_event(EVENT_FLAG_DIALPLAN, "VarSet", 
      "Channel: %s\r\n"
      "Variable: SHARED(%s)\r\n"
      "Value: %s\r\n"
      "Uniqueid: %s\r\n", 
      chan ? chan->name : "none", args.var, value, 
      chan ? chan->uniqueid : "none");

   ast_channel_unlock(chan);

   return 0;
}
static int unload_module ( void  ) [static]

Definition at line 253 of file func_global.c.

References ast_custom_function_unregister().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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, } [static]

Definition at line 273 of file func_global.c.

Definition at line 273 of file func_global.c.

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

Definition at line 122 of file func_global.c.

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

Definition at line 247 of file func_global.c.

Initial value:
 {
   .type = "SHARED_VARIABLES",
   .destroy = shared_variable_free,
}

Definition at line 87 of file func_global.c.