Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_realtime.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005-2006, BJ Weschke. All rights reserved.
5 *
6 * BJ Weschke <bweschke@btwtech.com>
7 *
8 * This code is released by the author with no restrictions on usage.
9 *
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 */
17
18/*! \file
19 *
20 * \brief REALTIME dialplan function
21 *
22 * \author BJ Weschke <bweschke@btwtech.com>
23 *
24 * \ingroup functions
25 */
26
27/*** MODULEINFO
28 <support_level>core</support_level>
29 ***/
30
31#include "asterisk.h"
32
33#include "asterisk/file.h"
34#include "asterisk/channel.h"
35#include "asterisk/pbx.h"
36#include "asterisk/config.h"
37#include "asterisk/module.h"
38#include "asterisk/lock.h"
39#include "asterisk/utils.h"
40#include "asterisk/app.h"
41
42/*** DOCUMENTATION
43 <function name="REALTIME" language="en_US">
44 <since>
45 <version>1.4.0</version>
46 </since>
47 <synopsis>
48 RealTime Read/Write Functions.
49 </synopsis>
50 <syntax>
51 <parameter name="family" required="true" />
52 <parameter name="fieldmatch" required="true" />
53 <parameter name="matchvalue" />
54 <parameter name="delim1|field">
55 <para>Use <replaceable>delim1</replaceable> with <replaceable>delim2</replaceable> on
56 read and <replaceable>field</replaceable> without <replaceable>delim2</replaceable> on
57 write</para>
58 <para>If we are reading and <replaceable>delim1</replaceable> is not specified, defaults
59 to <literal>,</literal></para>
60 </parameter>
61 <parameter name="delim2">
62 <para>Parameter only used when reading, if not specified defaults to <literal>=</literal></para>
63 </parameter>
64 </syntax>
65 <description>
66 <para>This function will read or write values from/to a RealTime repository.
67 REALTIME(....) will read names/values from the repository, and
68 REALTIME(....)= will write a new value/field to the repository. On a
69 read, this function returns a delimited text string. The name/value
70 pairs are delimited by <replaceable>delim1</replaceable>, and the name and value are delimited
71 between each other with delim2.
72 If there is no match, NULL will be returned by the function.
73 On a write, this function will always return NULL.</para>
74 </description>
75 <see-also>
76 <ref type="function">REALTIME_STORE</ref>
77 <ref type="function">REALTIME_DESTROY</ref>
78 <ref type="function">REALTIME_FIELD</ref>
79 <ref type="function">REALTIME_HASH</ref>
80 </see-also>
81 </function>
82 <function name="REALTIME_STORE" language="en_US">
83 <since>
84 <version>1.6.0</version>
85 </since>
86 <synopsis>
87 RealTime Store Function.
88 </synopsis>
89 <syntax>
90 <parameter name="family" required="true" />
91 <parameter name="field1" required="true" />
92 <parameter name="fieldN" required="true" multiple="true" />
93 <parameter name="field30" required="true" />
94 </syntax>
95 <description>
96 <para>This function will insert a new set of values into the RealTime repository.
97 If RT engine provides an unique ID of the stored record, REALTIME_STORE(...)=..
98 creates channel variable named RTSTOREID, which contains value of unique ID.
99 Currently, a maximum of 30 field/value pairs is supported.</para>
100 </description>
101 <see-also>
102 <ref type="function">REALTIME</ref>
103 <ref type="function">REALTIME_DESTROY</ref>
104 <ref type="function">REALTIME_FIELD</ref>
105 <ref type="function">REALTIME_HASH</ref>
106 </see-also>
107 </function>
108 <function name="REALTIME_DESTROY" language="en_US">
109 <since>
110 <version>1.6.0</version>
111 </since>
112 <synopsis>
113 RealTime Destroy Function.
114 </synopsis>
115 <syntax>
116 <parameter name="family" required="true" />
117 <parameter name="fieldmatch" required="true" />
118 <parameter name="matchvalue" />
119 <parameter name="delim1" />
120 <parameter name="delim2" />
121 </syntax>
122 <description>
123 <para>This function acts in the same way as REALTIME(....) does, except that
124 it destroys the matched record in the RT engine.</para>
125 <note>
126 <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
127 is set to <literal>no</literal>, this function can only be read from the
128 dialplan, and not directly from external protocols. It can, however, be
129 executed as a write operation (<literal>REALTIME_DESTROY(family, fieldmatch)=ignored</literal>)</para>
130 </note>
131 </description>
132 <see-also>
133 <ref type="function">REALTIME</ref>
134 <ref type="function">REALTIME_STORE</ref>
135 <ref type="function">REALTIME_FIELD</ref>
136 <ref type="function">REALTIME_HASH</ref>
137 </see-also>
138 </function>
139 <function name="REALTIME_FIELD" language="en_US">
140 <since>
141 <version>1.6.2.0</version>
142 </since>
143 <synopsis>
144 RealTime query function.
145 </synopsis>
146 <syntax>
147 <parameter name="family" required="true" />
148 <parameter name="fieldmatch" required="true" />
149 <parameter name="matchvalue" required="true" />
150 <parameter name="fieldname" required="true" />
151 </syntax>
152 <description>
153 <para>This function retrieves a single item, <replaceable>fieldname</replaceable>
154 from the RT engine, where <replaceable>fieldmatch</replaceable> contains the value
155 <replaceable>matchvalue</replaceable>. When written to, the REALTIME_FIELD() function
156 performs identically to the REALTIME() function.</para>
157 </description>
158 <see-also>
159 <ref type="function">REALTIME</ref>
160 <ref type="function">REALTIME_STORE</ref>
161 <ref type="function">REALTIME_DESTROY</ref>
162 <ref type="function">REALTIME_HASH</ref>
163 </see-also>
164 </function>
165 <function name="REALTIME_HASH" language="en_US">
166 <since>
167 <version>1.6.2.0</version>
168 </since>
169 <synopsis>
170 RealTime query function.
171 </synopsis>
172 <syntax>
173 <parameter name="family" required="true" />
174 <parameter name="fieldmatch" required="true" />
175 <parameter name="matchvalue" required="true" />
176 </syntax>
177 <description>
178 <para>This function retrieves a single record from the RT engine, where
179 <replaceable>fieldmatch</replaceable> contains the value
180 <replaceable>matchvalue</replaceable> and formats the output suitably, such that
181 it can be assigned to the HASH() function. The HASH() function then provides
182 a suitable method for retrieving each field value of the record.</para>
183 </description>
184 <see-also>
185 <ref type="function">REALTIME</ref>
186 <ref type="function">REALTIME_STORE</ref>
187 <ref type="function">REALTIME_DESTROY</ref>
188 <ref type="function">REALTIME_FIELD</ref>
189 </see-also>
190 </function>
191 ***/
192
196
197static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
198{
199 struct ast_variable *var, *head;
200 struct ast_str *out;
201 size_t resultslen;
202 int n;
204 AST_APP_ARG(family);
205 AST_APP_ARG(fieldmatch);
207 AST_APP_ARG(delim1);
208 AST_APP_ARG(delim2);
209 );
210
211 if (ast_strlen_zero(data)) {
212 ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
213 return -1;
214 }
215
217
218 if (!args.delim1)
219 args.delim1 = ",";
220 if (!args.delim2)
221 args.delim2 = "=";
222
223 if (chan)
225
226 head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
227
228 if (!head) {
229 if (chan)
231 return -1;
232 }
233
234 resultslen = 0;
235 n = 0;
236 for (var = head; var; n++, var = var->next)
237 resultslen += strlen(var->name) + strlen(var->value);
238 /* add space for delimiters and final '\0' */
239 resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
240
241 if (resultslen > len) {
242 ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
243 if (chan) {
245 }
246 return -1;
247 }
248
249 /* len is going to be sensible, so we don't need to check for stack
250 * overflows here. */
251 out = ast_str_alloca(resultslen);
252 for (var = head; var; var = var->next)
253 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
255
257
258 if (chan)
260
261 return 0;
262}
263
264static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
265{
266 int res = 0;
268 AST_APP_ARG(family);
269 AST_APP_ARG(fieldmatch);
271 AST_APP_ARG(field);
272 );
273
274 if (ast_strlen_zero(data)) {
275 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
276 return -1;
277 }
278
280
281 if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
282 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
283 return -1;
284 }
285
286 if (chan) {
288 }
289
290 res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);
291
292 if (res < 0) {
293 ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
294 }
295
296 if (chan) {
298 }
299
300 return res;
301}
302
303static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
304{
305 struct ast_variable *var, *head;
306 struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
307 struct ast_str *fields = ast_str_thread_get(&buf2, 16);
308 struct ast_str *values = ast_str_thread_get(&buf3, 16);
309 int first = 0;
310 enum { rtfield, rthash } which;
312 AST_APP_ARG(family);
313 AST_APP_ARG(fieldmatch);
315 AST_APP_ARG(fieldname);
316 );
317
318 if (!strcmp(cmd, "REALTIME_FIELD")) {
319 which = rtfield;
320 } else {
321 which = rthash;
322 }
323
324 if (ast_strlen_zero(data)) {
325 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
326 return -1;
327 }
328
330
331 if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
332 ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
333 return -1;
334 }
335
336 if (chan) {
338 }
339
340 if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
341 if (chan) {
343 }
344 return -1;
345 }
346
347 ast_str_reset(fields);
349
350 for (var = head; var; var = var->next) {
351 if (which == rtfield) {
352 ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
353 if (!strcasecmp(var->name, args.fieldname)) {
354 ast_debug(1, "Match! Value is %s\n", var->value);
355 ast_copy_string(buf, var->value, len);
356 break;
357 }
358 } else if (which == rthash) {
359 ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
360 ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
361 ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
362 first = 0;
363 }
364 }
366
367 if (which == rthash) {
368 pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
370 }
371
372 if (chan) {
374 }
375
376 return 0;
377}
378
379static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)
380{
381 int res = 0;
382 char storeid[32];
383 char *valcopy;
385 AST_APP_ARG(family);
386 AST_APP_ARG(f)[30]; /* fields */
387 );
388
390 AST_APP_ARG(v)[30]; /* values */
391 );
392
393 if (ast_strlen_zero(data)) {
394 ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
395 return -1;
396 }
397
398 if (chan)
400
401 valcopy = ast_strdupa(value);
403 AST_STANDARD_APP_ARGS(v, valcopy);
404
405 res = ast_store_realtime(a.family,
406 a.f[0], v.v[0], a.f[1], v.v[1], a.f[2], v.v[2], a.f[3], v.v[3], a.f[4], v.v[4],
407 a.f[5], v.v[5], a.f[6], v.v[6], a.f[7], v.v[7], a.f[8], v.v[8], a.f[9], v.v[9],
408 a.f[10], v.v[10], a.f[11], v.v[11], a.f[12], v.v[12], a.f[13], v.v[13], a.f[14], v.v[14],
409 a.f[15], v.v[15], a.f[16], v.v[16], a.f[17], v.v[17], a.f[18], v.v[18], a.f[19], v.v[19],
410 a.f[20], v.v[20], a.f[21], v.v[21], a.f[22], v.v[22], a.f[23], v.v[23], a.f[24], v.v[24],
411 a.f[25], v.v[25], a.f[26], v.v[26], a.f[27], v.v[27], a.f[28], v.v[28], a.f[29], v.v[29], SENTINEL
412 );
413
414 if (res < 0) {
415 ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
416 } else {
417 snprintf(storeid, sizeof(storeid), "%d", res);
418 pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
419 }
420
421 if (chan)
423
424 return 0;
425}
426
427static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
428{
429 struct ast_variable *var, *head;
430 struct ast_str *out;
431 size_t resultslen;
432 int n;
434 AST_APP_ARG(family);
435 AST_APP_ARG(fieldmatch);
437 AST_APP_ARG(delim1);
438 AST_APP_ARG(delim2);
439 );
440
441 if (ast_strlen_zero(data)) {
442 ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
443 return -1;
444 }
445
447
448 if (!args.delim1)
449 args.delim1 = ",";
450 if (!args.delim2)
451 args.delim2 = "=";
452
453 if (chan)
455
456 head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
457
458 if (!head) {
459 if (chan)
461 return -1;
462 }
463
464 if (len > 0) {
465 resultslen = 0;
466 n = 0;
467 for (var = head; var; n++, var = var->next) {
468 resultslen += strlen(var->name) + strlen(var->value);
469 }
470 /* add space for delimiters and final '\0' */
471 resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
472
473 if (resultslen > len) {
474 /* Unfortunately this does mean that we cannot destroy
475 * the row anymore. But OTOH, we're not destroying
476 * someones data without giving him the chance to look
477 * at it. */
478 ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
479 if (chan) {
481 }
482 return -1;
483 }
484
485 /* len is going to be sensible, so we don't need to check for
486 * stack overflows here. */
487 out = ast_str_alloca(resultslen);
488 for (var = head; var; var = var->next) {
489 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
490 }
492 }
493
494 ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
496
497 if (chan)
499
500 return 0;
501}
502
503/*!
504 * \brief Wrapper to execute REALTIME_DESTROY from a write operation. Allows
505 * execution even if live_dangerously is disabled.
506 */
507static int function_realtime_writedestroy(struct ast_channel *chan, const char *cmd, char *data, const char *value)
508{
509 return function_realtime_readdestroy(chan, cmd, data, NULL, 0);
510}
511
513 .name = "REALTIME",
516};
517
519 .name = "REALTIME_FIELD",
520 .read = realtimefield_read,
522};
523
525 .name = "REALTIME_HASH",
526 .read = realtimefield_read,
527};
528
530 .name = "REALTIME_STORE",
532};
533
535 .name = "REALTIME_DESTROY",
538};
539
540static int unload_module(void)
541{
542 int res = 0;
548 return res;
549}
550
551static int load_module(void)
552{
553 int res = 0;
559 return res;
560}
561
562AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write/Store/Destroy values from a RealTime repository");
struct sla_ringing_trunk * first
Definition: app_sla.c:338
#define var
Definition: ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
int ast_autoservice_stop(struct ast_channel *chan)
Stop servicing a channel for us...
Definition: autoservice.c:266
int ast_autoservice_start(struct ast_channel *chan)
Automatically service a channel for us...
Definition: autoservice.c:200
#define SENTINEL
Definition: compiler.h:87
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Generic File Format Support. Should be included by clients of the file handling routines....
static struct ast_threadstorage buf2
static int function_realtime_writedestroy(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Wrapper to execute REALTIME_DESTROY from a write operation. Allows execution even if live_dangerously...
static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static struct ast_custom_function realtime_store_function
static struct ast_custom_function realtime_destroy_function
static struct ast_threadstorage buf3
static struct ast_custom_function realtimehash_function
static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int load_module(void)
static struct ast_custom_function realtime_function
static int unload_module(void)
static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static struct ast_threadstorage buf1
static struct ast_custom_function realtimefield_function
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#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.
Configuration File Parser.
struct ast_variable * ast_load_realtime_all(const char *family,...) attribute_sentinel
Definition: main/config.c:3674
int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Destroy realtime configuration.
Definition: main/config.c:3996
int ast_update_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Update realtime configuration.
Definition: main/config.c:3879
int ast_store_realtime(const char *family,...) attribute_sentinel
Create realtime configuration.
Definition: main/config.c:3960
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
Asterisk locking-related definitions:
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Core PBX routines and definitions.
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 ast_custom_function_register_escalating(acf, escalation)
Register a custom function which requires escalated privileges.
Definition: pbx.h:1568
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
@ AST_CFE_READ
Definition: pbx.h:1551
#define NULL
Definition: resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
char * ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Set a dynamic string to a non-NULL terminated substring, with escaping of commas.
Definition: strings.h:1069
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_alloca(init_len)
Definition: strings.h:848
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:909
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
int value
Definition: syslog.c:37
const char * args
static struct test_val a
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86
FILE * out
Definition: utils/frame.c:33
Utility functions.