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.
21 * \brief Call Detail Record related dialplan functions
23 * \author Anthony Minessale II
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/app.h"
37 #include "asterisk/cdr.h"
40 <function name="CDR" language="en_US">
42 Gets or sets a CDR variable.
45 <parameter name="name" required="true">
46 <para>CDR field name:</para>
49 <para>Caller ID.</para>
51 <enum name="lastdata">
52 <para>Last application arguments.</para>
54 <enum name="disposition">
55 <para>ANSWERED, NO ANSWER, BUSY, FAILED.</para>
61 <para>Time the call started.</para>
63 <enum name="amaflags">
64 <para>DOCUMENTATION, BILL, IGNORE, etc.</para>
67 <para>Destination.</para>
70 <para>Time the call was answered.</para>
72 <enum name="accountcode">
73 <para>The channel's account code.</para>
75 <enum name="dcontext">
76 <para>Destination context.</para>
79 <para>Time the call ended.</para>
81 <enum name="uniqueid">
82 <para>The channel's unique id.</para>
84 <enum name="dstchannel">
85 <para>Destination channel.</para>
87 <enum name="duration">
88 <para>Duration of the call.</para>
90 <enum name="userfield">
91 <para>The channel's user specified field.</para>
94 <para>Last application.</para>
97 <para>Duration of the call once it was answered.</para>
100 <para>Channel name.</para>
102 <enum name="sequence">
103 <para>CDR sequence number.</para>
107 <parameter name="options" required="false">
110 <para>Uses the most recent CDR on a channel with multiple records</para>
113 <para>Searches the entire stack of CDRs on the channel.</para>
116 <para>Skips any CDR's that are marked 'LOCKED' due to forkCDR() calls.
117 (on setting/writing CDR vars only)</para>
120 <para>Retrieves the raw, unprocessed value.</para>
121 <para>For example, 'start', 'answer', and 'end' will be retrieved as epoch
122 values, when the <literal>u</literal> option is passed, but formatted as YYYY-MM-DD HH:MM:SS
123 otherwise. Similarly, disposition and amaflags will return their raw
124 integral values.</para>
130 <para>All of the CDR field names are read-only, except for <literal>accountcode</literal>,
131 <literal>userfield</literal>, and <literal>amaflags</literal>. You may, however, supply
132 a name not on the above list, and create your own variable, whose value can be changed
133 with this function, and this variable will be stored on the cdr.</para>
134 <note><para>For setting CDR values, the <literal>l</literal> flag does not apply to
135 setting the <literal>accountcode</literal>, <literal>userfield</literal>, or
136 <literal>amaflags</literal>.</para></note>
137 <para>Raw values for <literal>disposition</literal>:</para>
140 <para>NO ANSWER</para>
143 <para>NO ANSWER (NULL record)</para>
152 <para>ANSWERED</para>
155 <para>Raw values for <literal>amaflags</literal>:</para>
164 <para>DOCUMENTATION</para>
167 <para>Example: exten => 1,1,Set(CDR(userfield)=test)</para>
172 enum cdr_option_flags {
173 OPT_RECURSIVE = (1 << 0),
174 OPT_UNPARSED = (1 << 1),
176 OPT_SKIPLOCKED = (1 << 3),
179 AST_APP_OPTIONS(cdr_func_options, {
180 AST_APP_OPTION('l', OPT_LAST),
181 AST_APP_OPTION('r', OPT_RECURSIVE),
182 AST_APP_OPTION('s', OPT_SKIPLOCKED),
183 AST_APP_OPTION('u', OPT_UNPARSED),
186 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
187 char *buf, size_t len)
190 struct ast_flags flags = { 0 };
191 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
192 AST_DECLARE_APP_ARGS(args,
193 AST_APP_ARG(variable);
194 AST_APP_ARG(options);
197 if (ast_strlen_zero(parse))
203 AST_STANDARD_APP_ARGS(args, parse);
205 if (!ast_strlen_zero(args.options))
206 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
208 if (ast_test_flag(&flags, OPT_LAST))
212 if (ast_test_flag(&flags, OPT_SKIPLOCKED))
213 while (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED) && cdr->next)
216 ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
217 ast_test_flag(&flags, OPT_RECURSIVE),
218 ast_test_flag(&flags, OPT_UNPARSED));
223 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
226 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
227 struct ast_flags flags = { 0 };
228 AST_DECLARE_APP_ARGS(args,
229 AST_APP_ARG(variable);
230 AST_APP_ARG(options);
233 if (ast_strlen_zero(parse) || !value || !chan)
239 AST_STANDARD_APP_ARGS(args, parse);
241 if (!ast_strlen_zero(args.options))
242 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
244 if (ast_test_flag(&flags, OPT_LAST))
248 if (!strcasecmp(args.variable, "accountcode")) /* the 'l' flag doesn't apply to setting the accountcode, userfield, or amaflags */
249 ast_cdr_setaccount(chan, value);
250 else if (!strcasecmp(args.variable, "peeraccount"))
251 ast_cdr_setpeeraccount(chan, value);
252 else if (!strcasecmp(args.variable, "userfield"))
253 ast_cdr_setuserfield(chan, value);
254 else if (!strcasecmp(args.variable, "amaflags"))
255 ast_cdr_setamaflags(chan, value);
257 ast_cdr_setvar(cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
258 /* No need to worry about the u flag, as all fields for which setting
259 * 'u' would do anything are marked as readonly. */
264 static struct ast_custom_function cdr_function = {
270 static int unload_module(void)
272 return ast_custom_function_unregister(&cdr_function);
275 static int load_module(void)
277 return ast_custom_function_register(&cdr_function);
280 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan function");