Mon May 14 04:42:58 2007

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 /*! \file
00019  *
00020  * \brief REALTIME dialplan function
00021  * 
00022  * \author BJ Weschke <bweschke@btwtech.com>
00023  * 
00024  * \ingroup functions
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <unistd.h>
00034 #include <string.h>
00035 #include <sys/types.h>
00036 
00037 #include "asterisk/file.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/options.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/app.h"
00047 
00048 static int function_realtime_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00049 {
00050    struct ast_variable *var, *head;
00051         struct ast_module_user *u;
00052    char *results, *result_begin;
00053    size_t resultslen = 0;
00054    AST_DECLARE_APP_ARGS(args,
00055       AST_APP_ARG(family);
00056       AST_APP_ARG(fieldmatch);
00057       AST_APP_ARG(value);
00058       AST_APP_ARG(delim1);
00059       AST_APP_ARG(delim2);
00060    );
00061 
00062 
00063    if (ast_strlen_zero(data)) {
00064       ast_log(LOG_WARNING, "Syntax: REALTIME(family|fieldmatch[|value[|delim1[|delim2]]]) - missing argument!\n");
00065       return -1;
00066    }
00067 
00068    u = ast_module_user_add(chan);
00069 
00070    AST_STANDARD_APP_ARGS(args, data);
00071 
00072    if (!args.delim1)
00073       args.delim1 = "|";
00074    if (!args.delim2)
00075       args.delim2 = "=";
00076 
00077    head = ast_load_realtime(args.family, args.fieldmatch, args.value, NULL);
00078 
00079    if (!head) {
00080       ast_module_user_remove(u);
00081       return -1;
00082    }
00083    for (var = head; var; var = var->next)
00084       resultslen += strlen(var->name) + strlen(var->value) + 2;
00085 
00086    result_begin = results = alloca(resultslen);
00087    for (var = head; var; var = var->next)
00088       ast_build_string(&results, &resultslen, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
00089    ast_copy_string(buf, result_begin, len);
00090 
00091    ast_module_user_remove(u);
00092 
00093    return 0;
00094 }
00095 
00096 static int function_realtime_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
00097 {
00098         struct ast_module_user *u;
00099    int res = 0;
00100    AST_DECLARE_APP_ARGS(args,
00101       AST_APP_ARG(family);
00102       AST_APP_ARG(fieldmatch);
00103       AST_APP_ARG(value);
00104       AST_APP_ARG(field);
00105    );
00106 
00107    if (ast_strlen_zero(data)) {
00108       ast_log(LOG_WARNING, "Syntax: REALTIME(family|fieldmatch|value|newcol) - missing argument!\n");
00109       return -1;
00110    }
00111 
00112    u = ast_module_user_add(chan);
00113 
00114    AST_STANDARD_APP_ARGS(args, data);
00115 
00116    res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, NULL);
00117 
00118    if (res < 0) {
00119       ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
00120    }
00121 
00122    ast_module_user_remove(u);
00123 
00124    return 0;
00125 }
00126 
00127 struct ast_custom_function realtime_function = {
00128    .name = "REALTIME",
00129    .synopsis = "RealTime Read/Write Functions",
00130    .syntax = "REALTIME(family|fieldmatch[|value[|delim1[|delim2]]]) on read\n"
00131         "REALTIME(family|fieldmatch|value|field) on write\n",
00132    .desc = "This function will read or write values from/to a RealTime repository.\n"
00133       "REALTIME(....) will read names/values from the repository, and \n"
00134       "REALTIME(....)= will write a new value/field to the repository. On a\n"
00135       "read, this function returns a delimited text string. The name/value \n"
00136       "pairs are delimited by delim1, and the name and value are delimited \n"
00137       "between each other with delim2. The default for delim1 is '|' and   \n"
00138       "the default for delim2 is '='. If there is no match, NULL will be   \n"
00139       "returned by the function. On a write, this function will always     \n"
00140       "return NULL. \n",
00141    .read = function_realtime_read,
00142    .write = function_realtime_write,
00143 };
00144 
00145 static int unload_module(void)
00146 {
00147         int res = ast_custom_function_unregister(&realtime_function);
00148 
00149    ast_module_user_hangup_all();
00150 
00151         return res;
00152 }
00153 
00154 static int load_module(void)
00155 {
00156         int res = ast_custom_function_register(&realtime_function);
00157 
00158         return res;
00159 }
00160 
00161 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write values from a RealTime repository");

Generated on Mon May 14 04:42:58 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1