Fix samples vs. length calculations for g722
[asterisk-bristuff.git] / funcs / func_cdr.c
blob74d9eec86c3821c58799b6534f63315211689948
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999-2006, Digium, Inc.
6 * Portions Copyright (C) 2005, Anthony Minessale II
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Call Detail Record related dialplan functions
23 * \author Anthony Minessale II
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
41 #include "asterisk/cdr.h"
43 enum {
44 OPT_RECURSIVE = (1 << 0),
45 OPT_UNPARSED = (1 << 1),
46 OPT_LAST = (1 << 2),
47 } cdr_option_flags;
49 AST_APP_OPTIONS(cdr_func_options, {
50 AST_APP_OPTION('l', OPT_LAST),
51 AST_APP_OPTION('r', OPT_RECURSIVE),
52 AST_APP_OPTION('u', OPT_UNPARSED),
53 });
55 static int cdr_read(struct ast_channel *chan, char *cmd, char *parse,
56 char *buf, size_t len)
58 char *ret;
59 struct ast_flags flags = { 0 };
60 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
61 AST_DECLARE_APP_ARGS(args,
62 AST_APP_ARG(variable);
63 AST_APP_ARG(options);
66 if (ast_strlen_zero(parse))
67 return -1;
69 if (!cdr)
70 return -1;
72 AST_STANDARD_APP_ARGS(args, parse);
74 if (!ast_strlen_zero(args.options))
75 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
77 if (ast_test_flag(&flags, OPT_LAST))
78 while (cdr->next)
79 cdr = cdr->next;
81 ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
82 ast_test_flag(&flags, OPT_RECURSIVE),
83 ast_test_flag(&flags, OPT_UNPARSED));
85 return 0;
88 static int cdr_write(struct ast_channel *chan, char *cmd, char *parse,
89 const char *value)
91 struct ast_flags flags = { 0 };
92 AST_DECLARE_APP_ARGS(args,
93 AST_APP_ARG(variable);
94 AST_APP_ARG(options);
97 if (ast_strlen_zero(parse) || !value || !chan)
98 return -1;
100 AST_STANDARD_APP_ARGS(args, parse);
102 if (!ast_strlen_zero(args.options))
103 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
105 if (!strcasecmp(args.variable, "accountcode"))
106 ast_cdr_setaccount(chan, value);
107 else if (!strcasecmp(args.variable, "userfield"))
108 ast_cdr_setuserfield(chan, value);
109 else if (!strcasecmp(args.variable, "amaflags"))
110 ast_cdr_setamaflags(chan, value);
111 else if (chan->cdr)
112 ast_cdr_setvar(chan->cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
113 /* No need to worry about the u flag, as all fields for which setting
114 * 'u' would do anything are marked as readonly. */
116 return 0;
119 static struct ast_custom_function cdr_function = {
120 .name = "CDR",
121 .synopsis = "Gets or sets a CDR variable",
122 .syntax = "CDR(<name>[|options])",
123 .read = cdr_read,
124 .write = cdr_write,
125 .desc =
126 "Options:\n"
127 " 'r' searches the entire stack of CDRs on the channel\n"
128 " 'u' retrieves the raw, unprocessed value\n"
129 " For example, 'start', 'answer', and 'end' will be retrieved as epoch\n"
130 " values, when the 'u' option is passed, but formatted as YYYY-MM-DD HH:MM:SS\n"
131 " otherwise. Similarly, disposition and amaflags will return their raw\n"
132 " integral values.\n"
133 " Here is a list of all the available cdr field names:\n"
134 " clid lastdata disposition\n"
135 " src start amaflags\n"
136 " dst answer accountcode\n"
137 " dcontext end uniqueid\n"
138 " dstchannel duration userfield\n"
139 " lastapp billsec channel\n"
140 " All of the above variables are read-only, except for accountcode,\n"
141 " userfield, and amaflags. You may, however, supply\n"
142 " a name not on the above list, and create your own\n"
143 " variable, whose value can be changed with this function,\n"
144 " and this variable will be stored on the cdr.\n"
145 " raw values for disposition:\n"
146 " 1 = NO ANSWER\n"
147 " 2 = BUSY\n"
148 " 3 = FAILED\n"
149 " 4 = ANSWERED\n"
150 " raw values for amaflags:\n"
151 " 1 = OMIT\n"
152 " 2 = BILLING\n"
153 " 3 = DOCUMENTATION\n",
156 static int unload_module(void)
158 return ast_custom_function_unregister(&cdr_function);
161 static int load_module(void)
163 return ast_custom_function_register(&cdr_function);
166 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "CDR dialplan function");