Wed Jan 8 2020 09:50:13

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 = "ac1f6a56484a8820659555499174e588" , .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_.nosp@m.glob.nosp@m.al__2.nosp@m.0060.nosp@m.5@the.nosp@m.-til.nosp@m.ghman.nosp@m..com

Definition in file func_global.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 308 of file func_global.c.

static void __unreg_module ( void  )
static

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

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

108 {
109  const char *var = pbx_builtin_getvar_helper(NULL, data);
110 
111  *buf = '\0';
112 
113  if (var)
114  ast_copy_string(buf, var, len);
115 
116  return 0;
117 }
#define var
Definition: ast_expr2f.c:606
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
Definition: pbx.c:10475
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static int global_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 119 of file func_global.c.

References pbx_builtin_setvar_helper().

120 {
121  pbx_builtin_setvar_helper(NULL, data, value);
122 
123  return 0;
124 }
int value
Definition: syslog.c:39
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
Definition: pbx.c:10546
static int load_module ( void  )
static

Definition at line 298 of file func_global.c.

References ast_custom_function_register.

299 {
300  int res = 0;
301 
304 
305  return res;
306 }
static struct ast_custom_function shared_function
Definition: func_global.c:282
static struct ast_custom_function global_function
Definition: func_global.c:126
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static int shared_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 132 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, prefix, and var.

133 {
134  struct ast_datastore *varstore;
135  struct varshead *varshead;
136  struct ast_var_t *var;
138  AST_APP_ARG(var);
139  AST_APP_ARG(chan);
140  );
141  struct ast_channel *c_ref = NULL;
142 
143  if (ast_strlen_zero(data)) {
144  ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
145  return -1;
146  }
147 
149 
150  if (!ast_strlen_zero(args.chan)) {
151  char *prefix = ast_alloca(strlen(args.chan) + 2);
152  sprintf(prefix, "%s-", args.chan);
153  if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
154  ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
155  return -1;
156  }
157  chan = c_ref;
158  } else if (!chan) {
159  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
160  return -1;
161  }
162 
163  ast_channel_lock(chan);
164 
165  if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
166  ast_channel_unlock(chan);
167  if (c_ref) {
168  c_ref = ast_channel_unref(c_ref);
169  }
170  return -1;
171  }
172 
173  varshead = varstore->data;
174  *buf = '\0';
175 
176  /* Protected by the channel lock */
177  AST_LIST_TRAVERSE(varshead, var, entries) {
178  if (!strcmp(args.var, ast_var_name(var))) {
179  ast_copy_string(buf, ast_var_value(var), len);
180  break;
181  }
182  }
183 
184  ast_channel_unlock(chan);
185 
186  if (c_ref) {
187  c_ref = ast_channel_unref(c_ref);
188  }
189 
190  return 0;
191 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
Main Channel structure associated with a channel.
Definition: channel.h:742
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:89
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:69
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
#define var
Definition: ast_expr2f.c:606
Structure for a data store object.
Definition: datastore.h:54
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2604
static struct ast_datastore_info shared_variable_info
Definition: func_global.c:91
const char * data
Definition: channel.h:755
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1808
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define ast_channel_unlock(chan)
Definition: channel.h:2467
void * data
Definition: datastore.h:56
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1803
static char prefix[MAX_PREFIX]
Definition: http.c:107
static void shared_variable_free ( void *  data)
static

Definition at line 96 of file func_global.c.

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

97 {
98  struct varshead *varshead = data;
99  struct ast_var_t *var;
100 
101  while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) {
102  ast_var_delete(var);
103  }
104  ast_free(varshead);
105 }
#define var
Definition: ast_expr2f.c:606
void ast_var_delete(struct ast_var_t *var)
Definition: chanvars.c:63
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
struct ast_var_t::@158 entries
#define ast_free(a)
Definition: astmm.h:97
static int shared_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 193 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_unlock, ast_channel_unref, ast_datastore_alloc(), ast_datastore_free(), AST_DECLARE_APP_ARGS, AST_LIST_INSERT_HEAD, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, 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, ast_channel::name, prefix, S_OR, ast_channel::uniqueid, var, and ast_channel::varshead.

194 {
195  struct ast_datastore *varstore;
196  struct varshead *varshead;
197  struct ast_var_t *var;
199  AST_APP_ARG(var);
200  AST_APP_ARG(chan);
201  );
202  struct ast_channel *c_ref = NULL;
203 
204  if (ast_strlen_zero(data)) {
205  ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
206  return -1;
207  }
208 
210 
211  if (!ast_strlen_zero(args.chan)) {
212  char *prefix = ast_alloca(strlen(args.chan) + 2);
213  sprintf(prefix, "%s-", args.chan);
214  if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
215  ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
216  return -1;
217  }
218  chan = c_ref;
219  } else if (!chan) {
220  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
221  return -1;
222  }
223 
224  ast_channel_lock(chan);
225 
226  if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
227  if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
228  ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
229  ast_channel_unlock(chan);
230  if (c_ref) {
231  c_ref = ast_channel_unref(c_ref);
232  }
233  return -1;
234  }
235 
236  if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
237  ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
238  ast_datastore_free(varstore);
239  ast_channel_unlock(chan);
240  if (c_ref) {
241  c_ref = ast_channel_unref(c_ref);
242  }
243  return -1;
244  }
245 
246  varstore->data = varshead;
247  ast_channel_datastore_add(chan, varstore);
248  }
249  varshead = varstore->data;
250 
251  /* Protected by the channel lock */
252  AST_LIST_TRAVERSE_SAFE_BEGIN(varshead, var, entries) {
253  /* If there's a previous value, remove it */
254  if (!strcmp(args.var, ast_var_name(var))) {
255  AST_LIST_REMOVE_CURRENT(entries);
256  ast_var_delete(var);
257  break;
258  }
259  }
261 
262  if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
263  AST_LIST_INSERT_HEAD(varshead, var, entries);
265  "Channel: %s\r\n"
266  "Variable: SHARED(%s)\r\n"
267  "Value: %s\r\n"
268  "Uniqueid: %s\r\n",
269  chan ? chan->name : "none", args.var, value,
270  chan ? chan->uniqueid : "none");
271  }
272 
273  ast_channel_unlock(chan);
274 
275  if (c_ref) {
276  c_ref = ast_channel_unref(c_ref);
277  }
278 
279  return 0;
280 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
Main Channel structure associated with a channel.
Definition: channel.h:742
const ast_string_field uniqueid
Definition: channel.h:787
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
struct ast_var_t * ast_var_assign(const char *name, const char *value)
Definition: chanvars.c:41
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:69
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
#define var
Definition: ast_expr2f.c:606
Structure for a data store object.
Definition: datastore.h:54
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2604
static struct ast_datastore_info shared_variable_info
Definition: func_global.c:91
const char * data
Definition: channel.h:755
int value
Definition: syslog.c:39
#define EVENT_FLAG_DIALPLAN
Definition: manager.h:82
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:600
void ast_var_delete(struct ast_var_t *var)
Definition: chanvars.c:63
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:65
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1808
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:554
#define LOG_ERROR
Definition: logger.h:155
static struct @350 args
const ast_string_field name
Definition: channel.h:787
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_datastore * ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
Definition: datastore.c:98
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:696
#define ast_channel_unlock(chan)
Definition: channel.h:2467
void * data
Definition: datastore.h:56
#define ast_calloc(a, b)
Definition: astmm.h:82
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:528
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:219
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1803
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2590
struct varshead varshead
Definition: channel.h:817
static char prefix[MAX_PREFIX]
Definition: http.c:107
static int unload_module ( void  )
static

Definition at line 288 of file func_global.c.

References ast_custom_function_unregister().

289 {
290  int res = 0;
291 
294 
295  return res;
296 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ast_custom_function shared_function
Definition: func_global.c:282
static struct ast_custom_function global_function
Definition: func_global.c:126

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 308 of file func_global.c.

Definition at line 308 of file func_global.c.

struct ast_custom_function global_function
static
Initial value:
= {
.name = "GLOBAL",
.read = global_read,
.write = global_write,
}
static int global_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_global.c:107
static int global_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_global.c:119

Definition at line 126 of file func_global.c.

struct ast_custom_function shared_function
static
Initial value:
= {
.name = "SHARED",
.read = shared_read,
.write = shared_write,
}
static int shared_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_global.c:193
static int shared_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_global.c:132

Definition at line 282 of file func_global.c.

struct ast_datastore_info shared_variable_info
static
Initial value:
= {
.type = "SHARED_VARIABLES",
}
static void shared_variable_free(void *data)
Definition: func_global.c:96

Definition at line 91 of file func_global.c.