Sat Apr 26 2014 22:02:52

Asterisk developer's documentation


func_realtime.c File Reference

REALTIME dialplan function. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
Include dependency graph for func_realtime.c:

Go to the source code of this file.

Functions

static void __init_buf1 (void)
static void __init_buf2 (void)
static void __init_buf3 (void)
static void __reg_module (void)
static void __unreg_module (void)
static int function_realtime_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_store (struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int function_realtime_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
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 is disabled.
static int load_module (void)
static int realtimefield_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Read/Write/Store/Destroy values from a RealTime repository" , .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, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_threadstorage buf1 = { .once = PTHREAD_ONCE_INIT , .key_init = __init_buf1 , .custom_init = NULL , }
static struct ast_threadstorage buf2 = { .once = PTHREAD_ONCE_INIT , .key_init = __init_buf2 , .custom_init = NULL , }
static struct ast_threadstorage buf3 = { .once = PTHREAD_ONCE_INIT , .key_init = __init_buf3 , .custom_init = NULL , }
static struct ast_custom_function realtime_destroy_function
static struct ast_custom_function realtime_function
static struct ast_custom_function realtime_store_function
static struct ast_custom_function realtimefield_function
static struct ast_custom_function realtimehash_function

Detailed Description

REALTIME dialplan function.

Author:
BJ Weschke <bweschke@btwtech.com>

Definition in file func_realtime.c.


Function Documentation

static void __init_buf1 ( void  ) [static]

Definition at line 180 of file func_realtime.c.

{
static void __init_buf2 ( void  ) [static]

Definition at line 181 of file func_realtime.c.

{
static void __init_buf3 ( void  ) [static]

Definition at line 182 of file func_realtime.c.

{
static void __reg_module ( void  ) [static]

Definition at line 543 of file func_realtime.c.

static void __unreg_module ( void  ) [static]

Definition at line 543 of file func_realtime.c.

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

Definition at line 184 of file func_realtime.c.

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), AST_DECLARE_APP_ARGS, ast_load_realtime_all(), ast_log(), AST_STANDARD_APP_ARGS, ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_strlen_zero(), ast_variables_destroy(), LOG_WARNING, ast_variable::next, SENTINEL, value, and var.

{
   struct ast_variable *var, *head;
   struct ast_str *out;
   size_t resultslen;
   int n;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(family);
      AST_APP_ARG(fieldmatch);
      AST_APP_ARG(value);
      AST_APP_ARG(delim1);
      AST_APP_ARG(delim2);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
      return -1;
   }

   AST_STANDARD_APP_ARGS(args, data);

   if (!args.delim1)
      args.delim1 = ",";
   if (!args.delim2)
      args.delim2 = "=";

   if (chan)
      ast_autoservice_start(chan);

   head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);

   if (!head) {
      if (chan)
         ast_autoservice_stop(chan);
      return -1;
   }

   resultslen = 0;
   n = 0;
   for (var = head; var; n++, var = var->next)
      resultslen += strlen(var->name) + strlen(var->value);
   /* add space for delimiters and final '\0' */
   resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;

   if (resultslen > len) {
      ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
      return -1;
   }

   /* len is going to be sensible, so we don't need to check for stack
    * overflows here. */
   out = ast_str_alloca(resultslen);
   for (var = head; var; var = var->next)
      ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
   ast_copy_string(buf, ast_str_buffer(out), len);

   ast_variables_destroy(head);

   if (chan)
      ast_autoservice_stop(chan);

   return 0;
}
static int function_realtime_readdestroy ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 411 of file func_realtime.c.

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), AST_DECLARE_APP_ARGS, ast_destroy_realtime(), ast_load_realtime_all(), ast_log(), AST_STANDARD_APP_ARGS, ast_str_alloca, ast_str_append(), ast_str_buffer(), ast_strlen_zero(), ast_variables_destroy(), LOG_WARNING, ast_variable::next, SENTINEL, value, and var.

Referenced by function_realtime_writedestroy().

{
   struct ast_variable *var, *head;
   struct ast_str *out;
   size_t resultslen;
   int n;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(family);
      AST_APP_ARG(fieldmatch);
      AST_APP_ARG(value);
      AST_APP_ARG(delim1);
      AST_APP_ARG(delim2);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
      return -1;
   }

   AST_STANDARD_APP_ARGS(args, data);

   if (!args.delim1)
      args.delim1 = ",";
   if (!args.delim2)
      args.delim2 = "=";

   if (chan)
      ast_autoservice_start(chan);

   head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);

   if (!head) {
      if (chan)
         ast_autoservice_stop(chan);
      return -1;
   }

   if (len > 0) {
      resultslen = 0;
      n = 0;
      for (var = head; var; n++, var = var->next) {
         resultslen += strlen(var->name) + strlen(var->value);
      }
      /* add space for delimiters and final '\0' */
      resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;

      if (resultslen > len) {
         /* Unfortunately this does mean that we cannot destroy
          * the row anymore. But OTOH, we're not destroying
          * someones data without giving him the chance to look
          * at it. */
         ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
         return -1;
      }

      /* len is going to be sensible, so we don't need to check for
       * stack overflows here. */
      out = ast_str_alloca(resultslen);
      for (var = head; var; var = var->next) {
         ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
      }
      ast_copy_string(buf, ast_str_buffer(out), len);
   }

   ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
   ast_variables_destroy(head);

   if (chan)
      ast_autoservice_stop(chan);

   return 0;
}
static int function_realtime_store ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Definition at line 363 of file func_realtime.c.

References AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, ast_store_realtime(), ast_strlen_zero(), f, LOG_WARNING, pbx_builtin_setvar_helper(), and SENTINEL.

{
   int res = 0;
   char storeid[32];
   char *valcopy;
   AST_DECLARE_APP_ARGS(a,
      AST_APP_ARG(family);
      AST_APP_ARG(f)[30]; /* fields */
   );

   AST_DECLARE_APP_ARGS(v,
      AST_APP_ARG(v)[30]; /* values */
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
      return -1;
   }

   if (chan)
      ast_autoservice_start(chan);

   valcopy = ast_strdupa(value);
   AST_STANDARD_APP_ARGS(a, data);
   AST_STANDARD_APP_ARGS(v, valcopy);

   res = ast_store_realtime(a.family,
      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],
      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],
      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],
      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],
      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],
      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
   );

   if (res < 0) {
      ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
   } else {
      snprintf(storeid, sizeof(storeid), "%d", res);
      pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
   }

   if (chan)
      ast_autoservice_stop(chan);

   return 0;
}
static int function_realtime_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Definition at line 248 of file func_realtime.c.

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_update_realtime(), LOG_WARNING, and SENTINEL.

{
   int res = 0;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(family);
      AST_APP_ARG(fieldmatch);
      AST_APP_ARG(value);
      AST_APP_ARG(field);
   );

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
      return -1;
   }

   AST_STANDARD_APP_ARGS(args, data);

   if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
      ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
      return -1;
   }

   if (chan) {
      ast_autoservice_start(chan);
   }

   res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);

   if (res < 0) {
      ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
   }

   if (chan) {
      ast_autoservice_stop(chan);
   }

   return res;
}
static int function_realtime_writedestroy ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Wrapper to execute REALTIME_DESTROY from a write operation. Allows execution even if live_dangerously is disabled.

Definition at line 488 of file func_realtime.c.

References function_realtime_readdestroy().

{
   return function_realtime_readdestroy(chan, cmd, data, NULL, 0);
}
static int realtimefield_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 287 of file func_realtime.c.

References args, AST_APP_ARG, ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), ast_debug, AST_DECLARE_APP_ARGS, ast_load_realtime_all(), ast_log(), AST_STANDARD_APP_ARGS, ast_str_append(), ast_str_buffer(), ast_str_reset(), ast_str_set_escapecommas(), ast_str_thread_get(), ast_strlen_zero(), ast_variables_destroy(), buf1, buf2, buf3, first, LOG_WARNING, ast_variable::name, ast_variable::next, pbx_builtin_setvar_helper(), SENTINEL, value, ast_variable::value, and var.

{
   struct ast_variable *var, *head;
   struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
   struct ast_str *fields = ast_str_thread_get(&buf2, 16);
   struct ast_str *values = ast_str_thread_get(&buf3, 16);
   int first = 0;
   enum { rtfield, rthash } which;
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(family);
      AST_APP_ARG(fieldmatch);
      AST_APP_ARG(value);
      AST_APP_ARG(fieldname);
   );

   if (!strcmp(cmd, "REALTIME_FIELD")) {
      which = rtfield;
   } else {
      which = rthash;
   }

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
      return -1;
   }

   AST_STANDARD_APP_ARGS(args, data);

   if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
      ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
      return -1;
   }

   if (chan) {
      ast_autoservice_start(chan);
   }

   if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
      if (chan) {
         ast_autoservice_stop(chan);
      }
      return -1;
   }

   ast_str_reset(fields);
   ast_str_reset(values);

   for (var = head; var; var = var->next) {
      if (which == rtfield) {
         ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
         if (!strcasecmp(var->name, args.fieldname)) {
            ast_debug(1, "Match! Value is %s\n", var->value);
            ast_copy_string(buf, var->value, len);
            break;
         }
      } else if (which == rthash) {
         ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
         ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
         ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
         first = 0;
      }
   }
   ast_variables_destroy(head);

   if (which == rthash) {
      pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
      ast_copy_string(buf, ast_str_buffer(values), len);
   }

   if (chan) {
      ast_autoservice_stop(chan);
   }

   return 0;
}

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Read/Write/Store/Destroy values from a RealTime repository" , .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, } [static]

Definition at line 543 of file func_realtime.c.

Definition at line 543 of file func_realtime.c.

struct ast_threadstorage buf3 = { .once = PTHREAD_ONCE_INIT , .key_init = __init_buf3 , .custom_init = NULL , } [static]

Definition at line 182 of file func_realtime.c.

Referenced by realtimefield_read().

Initial value:
 {
   .name = "REALTIME_DESTROY",
   .read = function_realtime_readdestroy,
   .write = function_realtime_writedestroy,
}

Definition at line 515 of file func_realtime.c.

Initial value:
 {
   .name = "REALTIME",
   .read = function_realtime_read,
   .write = function_realtime_write,
}

Definition at line 493 of file func_realtime.c.

Initial value:
 {
   .name = "REALTIME_STORE",
   .write = function_realtime_store,
}

Definition at line 510 of file func_realtime.c.

Initial value:
 {
   .name = "REALTIME_FIELD",
   .read = realtimefield_read,
   .write = function_realtime_write,
}

Definition at line 499 of file func_realtime.c.

Initial value:
 {
   .name = "REALTIME_HASH",
   .read = realtimefield_read,
}

Definition at line 505 of file func_realtime.c.