Asterisk - The Open Source Telephony Project GIT-master-f36a736
Functions | Variables
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)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int global_delete_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int global_exists_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
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 const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_custom_function global_delete_function
 
static struct ast_custom_function global_exists_function
 
static struct ast_custom_function global_function
 
static struct ast_custom_function shared_function
 
static const 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

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 396 of file func_global.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 396 of file func_global.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 396 of file func_global.c.

◆ global_delete_write()

static int global_delete_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 182 of file func_global.c.

183{
185
186 return 0;
187}
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.
#define NULL
Definition: resample.c:96

References NULL, and pbx_builtin_setvar_helper().

◆ global_exists_read()

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

Definition at line 194 of file func_global.c.

196{
197 const char *var = pbx_builtin_getvar_helper(NULL, data);
198
199 strcpy(buf, var ? "1" : "0");
200
201 return 0;
202}
#define var
Definition: ast_expr2f.c:605
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.

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

◆ global_read()

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

Definition at line 157 of file func_global.c.

158{
159 const char *var = pbx_builtin_getvar_helper(NULL, data);
160
161 *buf = '\0';
162
163 if (var)
165
166 return 0;
167}
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:425

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

◆ global_write()

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

Definition at line 169 of file func_global.c.

170{
172
173 return 0;
174}
int value
Definition: syslog.c:37

References NULL, pbx_builtin_setvar_helper(), and value.

◆ load_module()

static int load_module ( void  )
static

Definition at line 384 of file func_global.c.

385{
386 int res = 0;
387
392
393 return res;
394}
static struct ast_custom_function shared_function
Definition: func_global.c:366
static struct ast_custom_function global_exists_function
Definition: func_global.c:204
static struct ast_custom_function global_function
Definition: func_global.c:176
static struct ast_custom_function global_delete_function
Definition: func_global.c:189
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558

References ast_custom_function_register, global_delete_function, global_exists_function, global_function, and shared_function.

◆ shared_read()

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

Definition at line 209 of file func_global.c.

210{
211 struct ast_datastore *varstore;
212 struct varshead *varshead;
213 struct ast_var_t *var;
216 AST_APP_ARG(chan);
217 );
218 struct ast_channel *c_ref = NULL;
219
220 if (ast_strlen_zero(data)) {
221 ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
222 return -1;
223 }
224
226
227 if (!ast_strlen_zero(args.chan)) {
228 char *prefix = ast_alloca(strlen(args.chan) + 2);
229 sprintf(prefix, "%s-", args.chan);
230 if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
231 ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var);
232 return -1;
233 }
234 chan = c_ref;
235 } else if (!chan) {
236 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
237 return -1;
238 }
239
240 ast_channel_lock(chan);
241
242 if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
243 ast_channel_unlock(chan);
244 if (c_ref) {
245 c_ref = ast_channel_unref(c_ref);
246 }
247 return -1;
248 }
249
250 varshead = varstore->data;
251 *buf = '\0';
252
253 /* Protected by the channel lock */
254 AST_LIST_TRAVERSE(varshead, var, entries) {
255 if (!strcmp(args.var, ast_var_name(var))) {
257 break;
258 }
259 }
260
261 ast_channel_unlock(chan);
262
263 if (c_ref) {
264 c_ref = ast_channel_unref(c_ref);
265 }
266
267 return 0;
268}
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_log
Definition: astobj2.c:42
#define ast_channel_lock(chan)
Definition: channel.h:2968
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:1453
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3004
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1473
#define ast_channel_unlock(chan)
Definition: channel.h:2969
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:2418
const char * ast_var_name(const struct ast_var_t *var)
Definition: chanvars.c:60
const char * ast_var_value(const struct ast_var_t *var)
Definition: chanvars.c:80
static const struct ast_datastore_info shared_variable_info
Definition: func_global.c:141
static char prefix[MAX_PREFIX]
Definition: http.c:144
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define LOG_ERROR
#define LOG_WARNING
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Main Channel structure associated with a channel.
const char * data
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
const char * args

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(), buf, ast_datastore::data, ast_channel::data, len(), LOG_ERROR, LOG_WARNING, NULL, prefix, shared_variable_info, and var.

◆ shared_variable_free()

static void shared_variable_free ( void *  data)
static

Definition at line 146 of file func_global.c.

147{
148 struct varshead *varshead = data;
149 struct ast_var_t *var;
150
153 }
155}
#define ast_free(a)
Definition: astmm.h:180
void ast_var_delete(struct ast_var_t *var)
Definition: extconf.c:2471
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
struct ast_var_t::@211 entries

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

◆ shared_write()

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

Definition at line 270 of file func_global.c.

271{
272 struct ast_datastore *varstore;
273 struct varshead *varshead;
274 struct ast_var_t *var;
277 AST_APP_ARG(chan);
278 );
279 struct ast_channel *c_ref = NULL;
280 int len;
281 RAII_VAR(char *, shared_buffer, NULL, ast_free);
282
283 if (ast_strlen_zero(data)) {
284 ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n");
285 return -1;
286 }
287
289
290 if (!ast_strlen_zero(args.chan)) {
291 char *prefix = ast_alloca(strlen(args.chan) + 2);
292 sprintf(prefix, "%s-", args.chan);
293 if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) {
294 ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value);
295 return -1;
296 }
297 chan = c_ref;
298 } else if (!chan) {
299 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
300 return -1;
301 }
302
303 len = 9 + strlen(args.var); /* SHARED() + var */
304 shared_buffer = ast_malloc(len);
305 if (!shared_buffer) {
306 if (c_ref) {
307 ast_channel_unref(c_ref);
308 }
309 return -1;
310 }
311
312 ast_channel_lock(chan);
313
314 if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) {
315 if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) {
316 ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n");
317 ast_channel_unlock(chan);
318 if (c_ref) {
319 c_ref = ast_channel_unref(c_ref);
320 }
321 return -1;
322 }
323
324 if (!(varshead = ast_calloc(1, sizeof(*varshead)))) {
325 ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n");
326 ast_datastore_free(varstore);
327 ast_channel_unlock(chan);
328 if (c_ref) {
329 c_ref = ast_channel_unref(c_ref);
330 }
331 return -1;
332 }
333
334 varstore->data = varshead;
335 ast_channel_datastore_add(chan, varstore);
336 }
337 varshead = varstore->data;
338
339 /* Protected by the channel lock */
341 /* If there's a previous value, remove it */
342 if (!strcmp(args.var, ast_var_name(var))) {
345 break;
346 }
347 }
349
350 if ((var = ast_var_assign(args.var, S_OR(value, "")))) {
352
353 sprintf(shared_buffer, "SHARED(%s)", args.var);
354 ast_channel_publish_varset(chan, shared_buffer, value);
355 }
356
357 ast_channel_unlock(chan);
358
359 if (c_ref) {
360 c_ref = ast_channel_unref(c_ref);
361 }
362
363 return 0;
364}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2404
#define ast_var_assign(name, value)
Definition: chanvars.h:40
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
void ast_channel_publish_varset(struct ast_channel *chan, const char *variable, const char *value)
Publish a ast_channel_publish_varset for a channel.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
struct varshead varshead
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

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, ast_channel::data, len(), LOG_ERROR, LOG_WARNING, NULL, prefix, RAII_VAR, S_OR, shared_variable_info, value, var, and ast_channel::varshead.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 372 of file func_global.c.

373{
374 int res = 0;
375
380
381 return res;
382}
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ast_custom_function_unregister(), global_delete_function, global_exists_function, global_function, and shared_function.

Variable Documentation

◆ __mod_info

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 396 of file func_global.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 396 of file func_global.c.

◆ global_delete_function

struct ast_custom_function global_delete_function
static
Initial value:
= {
.name = "GLOBAL_DELETE",
}
static int global_delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Definition: func_global.c:182

Definition at line 189 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ global_exists_function

struct ast_custom_function global_exists_function
static
Initial value:
= {
.name = "GLOBAL_EXISTS",
}
static int global_exists_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_global.c:194

Definition at line 204 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ global_function

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

Definition at line 176 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ shared_function

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

Definition at line 366 of file func_global.c.

Referenced by load_module(), and unload_module().

◆ shared_variable_info

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

Definition at line 141 of file func_global.c.

Referenced by shared_read(), and shared_write().