Tue Aug 20 16:34:34 2013

Asterisk developer's documentation


func_realtime.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005-2006, BJ Weschke. All rights reserved.
00005  *
00006  * BJ Weschke <bweschke@btwtech.com>
00007  *
00008  * This code is released by the author with no restrictions on usage.
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  */
00017 
00018 /*** MODULEINFO
00019    <support_level>core</support_level>
00020  ***/
00021 
00022 /*! \file
00023  *
00024  * \brief REALTIME dialplan function
00025  *
00026  * \author BJ Weschke <bweschke@btwtech.com>
00027  *
00028  * \ingroup functions
00029  */
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 378375 $")
00034 
00035 #include "asterisk/file.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/config.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/lock.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/app.h"
00043 
00044 /*** DOCUMENTATION
00045    <function name="REALTIME" language="en_US">
00046       <synopsis>
00047          RealTime Read/Write Functions.
00048       </synopsis>
00049       <syntax>
00050          <parameter name="family" required="true" />
00051          <parameter name="fieldmatch" required="true" />
00052          <parameter name="matchvalue" />
00053          <parameter name="delim1|field">
00054             <para>Use <replaceable>delim1</replaceable> with <replaceable>delim2</replaceable> on
00055             read and <replaceable>field</replaceable> without <replaceable>delim2</replaceable> on
00056             write</para>
00057             <para>If we are reading and <replaceable>delim1</replaceable> is not specified, defaults
00058             to <literal>,</literal></para>
00059          </parameter>
00060          <parameter name="delim2">
00061             <para>Parameter only used when reading, if not specified defaults to <literal>=</literal></para>
00062          </parameter>
00063       </syntax>
00064       <description>
00065          <para>This function will read or write values from/to a RealTime repository.
00066          REALTIME(....) will read names/values from the repository, and
00067          REALTIME(....)= will write a new value/field to the repository. On a
00068          read, this function returns a delimited text string. The name/value
00069          pairs are delimited by <replaceable>delim1</replaceable>, and the name and value are delimited
00070          between each other with delim2.
00071          If there is no match, NULL will be returned by the function.
00072          On a write, this function will always return NULL.</para>
00073       </description>
00074       <see-also>
00075          <ref type="function">REALTIME_STORE</ref>
00076          <ref type="function">REALTIME_DESTROY</ref>
00077          <ref type="function">REALTIME_FIELD</ref>
00078          <ref type="function">REALTIME_HASH</ref>
00079       </see-also>
00080    </function>
00081    <function name="REALTIME_STORE" language="en_US">
00082       <synopsis>
00083          RealTime Store Function.
00084       </synopsis>
00085       <syntax>
00086          <parameter name="family" required="true" />
00087          <parameter name="field1" required="true" />
00088          <parameter name="fieldN" required="true" multiple="true" />
00089          <parameter name="field30" required="true" />
00090       </syntax>
00091       <description>
00092          <para>This function will insert a new set of values into the RealTime repository.
00093          If RT engine provides an unique ID of the stored record, REALTIME_STORE(...)=..
00094          creates channel variable named RTSTOREID, which contains value of unique ID.
00095          Currently, a maximum of 30 field/value pairs is supported.</para>
00096       </description>
00097       <see-also>
00098          <ref type="function">REALTIME</ref>
00099          <ref type="function">REALTIME_DESTROY</ref>
00100          <ref type="function">REALTIME_FIELD</ref>
00101          <ref type="function">REALTIME_HASH</ref>
00102       </see-also>
00103    </function>
00104    <function name="REALTIME_DESTROY" language="en_US">
00105       <synopsis>
00106          RealTime Destroy Function.
00107       </synopsis>
00108       <syntax>
00109          <parameter name="family" required="true" />
00110          <parameter name="fieldmatch" required="true" />
00111          <parameter name="matchvalue" />
00112          <parameter name="delim1" />
00113          <parameter name="delim2" />
00114       </syntax>
00115       <description>
00116          <para>This function acts in the same way as REALTIME(....) does, except that
00117          it destroys the matched record in the RT engine.</para>
00118       </description>
00119       <see-also>
00120          <ref type="function">REALTIME</ref>
00121          <ref type="function">REALTIME_STORE</ref>
00122          <ref type="function">REALTIME_FIELD</ref>
00123          <ref type="function">REALTIME_HASH</ref>
00124       </see-also>
00125    </function>
00126    <function name="REALTIME_FIELD" language="en_US">
00127       <synopsis>
00128          RealTime query function.
00129       </synopsis>
00130       <syntax>
00131          <parameter name="family" required="true" />
00132          <parameter name="fieldmatch" required="true" />
00133          <parameter name="matchvalue" required="true" />
00134          <parameter name="fieldname" required="true" />
00135       </syntax>
00136       <description>
00137          <para>This function retrieves a single item, <replaceable>fieldname</replaceable>
00138          from the RT engine, where <replaceable>fieldmatch</replaceable> contains the value
00139          <replaceable>matchvalue</replaceable>.  When written to, the REALTIME_FIELD() function
00140          performs identically to the REALTIME() function.</para>
00141       </description>
00142       <see-also>
00143          <ref type="function">REALTIME</ref>
00144          <ref type="function">REALTIME_STORE</ref>
00145          <ref type="function">REALTIME_DESTROY</ref>
00146          <ref type="function">REALTIME_HASH</ref>
00147       </see-also>
00148    </function>
00149    <function name="REALTIME_HASH" language="en_US">
00150       <synopsis>
00151          RealTime query function.
00152       </synopsis>
00153       <syntax>
00154          <parameter name="family" required="true" />
00155          <parameter name="fieldmatch" required="true" />
00156          <parameter name="matchvalue" required="true" />
00157       </syntax>
00158       <description>
00159          <para>This function retrieves a single record from the RT engine, where
00160          <replaceable>fieldmatch</replaceable> contains the value
00161          <replaceable>matchvalue</replaceable> and formats the output suitably, such that
00162          it can be assigned to the HASH() function.  The HASH() function then provides
00163          a suitable method for retrieving each field value of the record.</para>
00164       </description>
00165       <see-also>
00166          <ref type="function">REALTIME</ref>
00167          <ref type="function">REALTIME_STORE</ref>
00168          <ref type="function">REALTIME_DESTROY</ref>
00169          <ref type="function">REALTIME_FIELD</ref>
00170       </see-also>
00171    </function>
00172  ***/
00173 
00174 AST_THREADSTORAGE(buf1);
00175 AST_THREADSTORAGE(buf2);
00176 AST_THREADSTORAGE(buf3);
00177 
00178 static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00179 {
00180    struct ast_variable *var, *head;
00181    struct ast_str *out;
00182    size_t resultslen;
00183    int n;
00184    AST_DECLARE_APP_ARGS(args,
00185       AST_APP_ARG(family);
00186       AST_APP_ARG(fieldmatch);
00187       AST_APP_ARG(value);
00188       AST_APP_ARG(delim1);
00189       AST_APP_ARG(delim2);
00190    );
00191 
00192    if (ast_strlen_zero(data)) {
00193       ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
00194       return -1;
00195    }
00196 
00197    AST_STANDARD_APP_ARGS(args, data);
00198 
00199    if (!args.delim1)
00200       args.delim1 = ",";
00201    if (!args.delim2)
00202       args.delim2 = "=";
00203 
00204    if (chan)
00205       ast_autoservice_start(chan);
00206 
00207    head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
00208 
00209    if (!head) {
00210       if (chan)
00211          ast_autoservice_stop(chan);
00212       return -1;
00213    }
00214 
00215    resultslen = 0;
00216    n = 0;
00217    for (var = head; var; n++, var = var->next)
00218       resultslen += strlen(var->name) + strlen(var->value);
00219    /* add space for delimiters and final '\0' */
00220    resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
00221 
00222    if (resultslen > len) {
00223       ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
00224       return -1;
00225    }
00226 
00227    /* len is going to be sensible, so we don't need to check for stack
00228     * overflows here. */
00229    out = ast_str_alloca(resultslen);
00230    for (var = head; var; var = var->next)
00231       ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
00232    ast_copy_string(buf, ast_str_buffer(out), len);
00233 
00234    ast_variables_destroy(head);
00235 
00236    if (chan)
00237       ast_autoservice_stop(chan);
00238 
00239    return 0;
00240 }
00241 
00242 static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00243 {
00244    int res = 0;
00245    AST_DECLARE_APP_ARGS(args,
00246       AST_APP_ARG(family);
00247       AST_APP_ARG(fieldmatch);
00248       AST_APP_ARG(value);
00249       AST_APP_ARG(field);
00250    );
00251 
00252    if (ast_strlen_zero(data)) {
00253       ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
00254       return -1;
00255    }
00256 
00257    AST_STANDARD_APP_ARGS(args, data);
00258 
00259    if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
00260       ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
00261       return -1;
00262    }
00263 
00264    if (chan) {
00265       ast_autoservice_start(chan);
00266    }
00267 
00268    res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);
00269 
00270    if (res < 0) {
00271       ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
00272    }
00273 
00274    if (chan) {
00275       ast_autoservice_stop(chan);
00276    }
00277 
00278    return res;
00279 }
00280 
00281 static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00282 {
00283    struct ast_variable *var, *head;
00284    struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
00285    struct ast_str *fields = ast_str_thread_get(&buf2, 16);
00286    struct ast_str *values = ast_str_thread_get(&buf3, 16);
00287    int first = 0;
00288    enum { rtfield, rthash } which;
00289    AST_DECLARE_APP_ARGS(args,
00290       AST_APP_ARG(family);
00291       AST_APP_ARG(fieldmatch);
00292       AST_APP_ARG(value);
00293       AST_APP_ARG(fieldname);
00294    );
00295 
00296    if (!strcmp(cmd, "REALTIME_FIELD")) {
00297       which = rtfield;
00298    } else {
00299       which = rthash;
00300    }
00301 
00302    if (ast_strlen_zero(data)) {
00303       ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
00304       return -1;
00305    }
00306 
00307    AST_STANDARD_APP_ARGS(args, data);
00308 
00309    if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
00310       ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
00311       return -1;
00312    }
00313 
00314    if (chan) {
00315       ast_autoservice_start(chan);
00316    }
00317 
00318    if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
00319       if (chan) {
00320          ast_autoservice_stop(chan);
00321       }
00322       return -1;
00323    }
00324 
00325    ast_str_reset(fields);
00326    ast_str_reset(values);
00327 
00328    for (var = head; var; var = var->next) {
00329       if (which == rtfield) {
00330          ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
00331          if (!strcasecmp(var->name, args.fieldname)) {
00332             ast_debug(1, "Match! Value is %s\n", var->value);
00333             ast_copy_string(buf, var->value, len);
00334             break;
00335          }
00336       } else if (which == rthash) {
00337          ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
00338          ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
00339          ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
00340          first = 0;
00341       }
00342    }
00343    ast_variables_destroy(head);
00344 
00345    if (which == rthash) {
00346       pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
00347       ast_copy_string(buf, ast_str_buffer(values), len);
00348    }
00349 
00350    if (chan) {
00351       ast_autoservice_stop(chan);
00352    }
00353 
00354    return 0;
00355 }
00356 
00357 static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00358 {
00359    int res = 0;
00360    char storeid[32];
00361    char *valcopy;
00362    AST_DECLARE_APP_ARGS(a,
00363       AST_APP_ARG(family);
00364       AST_APP_ARG(f)[30]; /* fields */
00365    );
00366 
00367    AST_DECLARE_APP_ARGS(v,
00368       AST_APP_ARG(v)[30]; /* values */
00369    );
00370 
00371    if (ast_strlen_zero(data)) {
00372       ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
00373       return -1;
00374    }
00375 
00376    if (chan)
00377       ast_autoservice_start(chan);
00378 
00379    valcopy = ast_strdupa(value);
00380    AST_STANDARD_APP_ARGS(a, data);
00381    AST_STANDARD_APP_ARGS(v, valcopy);
00382 
00383    res = ast_store_realtime(a.family,
00384       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],
00385       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],
00386       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],
00387       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],
00388       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],
00389       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
00390    );
00391 
00392    if (res < 0) {
00393       ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
00394    } else {
00395       snprintf(storeid, sizeof(storeid), "%d", res);
00396       pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
00397    }
00398 
00399    if (chan)
00400       ast_autoservice_stop(chan);
00401 
00402    return 0;
00403 }
00404 
00405 static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00406 {
00407    struct ast_variable *var, *head;
00408    struct ast_str *out;
00409    size_t resultslen;
00410    int n;
00411    AST_DECLARE_APP_ARGS(args,
00412       AST_APP_ARG(family);
00413       AST_APP_ARG(fieldmatch);
00414       AST_APP_ARG(value);
00415       AST_APP_ARG(delim1);
00416       AST_APP_ARG(delim2);
00417    );
00418 
00419    if (ast_strlen_zero(data)) {
00420       ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
00421       return -1;
00422    }
00423 
00424    AST_STANDARD_APP_ARGS(args, data);
00425 
00426    if (!args.delim1)
00427       args.delim1 = ",";
00428    if (!args.delim2)
00429       args.delim2 = "=";
00430 
00431    if (chan)
00432       ast_autoservice_start(chan);
00433 
00434    head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
00435 
00436    if (!head) {
00437       if (chan)
00438          ast_autoservice_stop(chan);
00439       return -1;
00440    }
00441 
00442    resultslen = 0;
00443    n = 0;
00444    for (var = head; var; n++, var = var->next)
00445       resultslen += strlen(var->name) + strlen(var->value);
00446    /* add space for delimiters and final '\0' */
00447    resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
00448 
00449    if (resultslen > len) {
00450       /* Unfortunately this does mean that we cannot destroy the row
00451        * anymore. But OTOH, we're not destroying someones data without
00452        * giving him the chance to look at it. */
00453       ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
00454       return -1;
00455    }
00456 
00457    /* len is going to be sensible, so we don't need to check for stack
00458     * overflows here. */
00459    out = ast_str_alloca(resultslen);
00460    for (var = head; var; var = var->next) {
00461       ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
00462    }
00463    ast_copy_string(buf, ast_str_buffer(out), len);
00464 
00465    ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
00466    ast_variables_destroy(head);
00467 
00468    if (chan)
00469       ast_autoservice_stop(chan);
00470 
00471    return 0;
00472 }
00473 
00474 static struct ast_custom_function realtime_function = {
00475    .name = "REALTIME",
00476    .read = function_realtime_read,
00477    .write = function_realtime_write,
00478 };
00479 
00480 static struct ast_custom_function realtimefield_function = {
00481    .name = "REALTIME_FIELD",
00482    .read = realtimefield_read,
00483    .write = function_realtime_write,
00484 };
00485 
00486 static struct ast_custom_function realtimehash_function = {
00487    .name = "REALTIME_HASH",
00488    .read = realtimefield_read,
00489 };
00490 
00491 static struct ast_custom_function realtime_store_function = {
00492    .name = "REALTIME_STORE",
00493    .write = function_realtime_store,
00494 };
00495 
00496 static struct ast_custom_function realtime_destroy_function = {
00497    .name = "REALTIME_DESTROY",
00498    .read = function_realtime_readdestroy,
00499 };
00500 
00501 static int unload_module(void)
00502 {
00503    int res = 0;
00504    res |= ast_custom_function_unregister(&realtime_function);
00505    res |= ast_custom_function_unregister(&realtime_store_function);
00506    res |= ast_custom_function_unregister(&realtime_destroy_function);
00507    res |= ast_custom_function_unregister(&realtimefield_function);
00508    res |= ast_custom_function_unregister(&realtimehash_function);
00509    return res;
00510 }
00511 
00512 static int load_module(void)
00513 {
00514    int res = 0;
00515    res |= ast_custom_function_register(&realtime_function);
00516    res |= ast_custom_function_register(&realtime_store_function);
00517    res |= ast_custom_function_register(&realtime_destroy_function);
00518    res |= ast_custom_function_register(&realtimefield_function);
00519    res |= ast_custom_function_register(&realtimehash_function);
00520    return res;
00521 }
00522 
00523 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write/Store/Destroy values from a RealTime repository");

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1