00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 269153 $")
00031
00032 #include "asterisk/module.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/app.h"
00037 #include "asterisk/cdr.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 enum cdr_option_flags {
00176 OPT_RECURSIVE = (1 << 0),
00177 OPT_UNPARSED = (1 << 1),
00178 OPT_LAST = (1 << 2),
00179 OPT_SKIPLOCKED = (1 << 3),
00180 OPT_FLOAT = (1 << 4),
00181 };
00182
00183 AST_APP_OPTIONS(cdr_func_options, {
00184 AST_APP_OPTION('f', OPT_FLOAT),
00185 AST_APP_OPTION('l', OPT_LAST),
00186 AST_APP_OPTION('r', OPT_RECURSIVE),
00187 AST_APP_OPTION('s', OPT_SKIPLOCKED),
00188 AST_APP_OPTION('u', OPT_UNPARSED),
00189 });
00190
00191 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
00192 char *buf, size_t len)
00193 {
00194 char *ret;
00195 struct ast_flags flags = { 0 };
00196 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00197 AST_DECLARE_APP_ARGS(args,
00198 AST_APP_ARG(variable);
00199 AST_APP_ARG(options);
00200 );
00201
00202 if (ast_strlen_zero(parse))
00203 return -1;
00204
00205 if (!cdr)
00206 return -1;
00207
00208 AST_STANDARD_APP_ARGS(args, parse);
00209
00210 if (!ast_strlen_zero(args.options))
00211 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00212
00213 if (ast_test_flag(&flags, OPT_LAST))
00214 while (cdr->next)
00215 cdr = cdr->next;
00216
00217 if (ast_test_flag(&flags, OPT_SKIPLOCKED))
00218 while (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED) && cdr->next)
00219 cdr = cdr->next;
00220
00221 if (!strcasecmp("billsec", args.variable) && ast_test_flag(&flags, OPT_FLOAT)) {
00222 if (!ast_tvzero(cdr->answer)) {
00223 double hrtime;
00224
00225 if(!ast_tvzero(cdr->end))
00226 hrtime = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
00227 else
00228 hrtime = (double)(ast_tvdiff_us(ast_tvnow(), cdr->answer) / 1000000.0);
00229
00230 snprintf(buf, len, "%lf", hrtime);
00231 } else {
00232 snprintf(buf, len, "%lf", 0.0);
00233 }
00234 ret = buf;
00235 } else if (!strcasecmp("duration", args.variable) && ast_test_flag(&flags, OPT_FLOAT)) {
00236 double hrtime;
00237
00238 if(!ast_tvzero(cdr->end))
00239 hrtime = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
00240 else
00241 hrtime = (double)(ast_tvdiff_us(ast_tvnow(), cdr->start) / 1000000.0);
00242
00243 snprintf(buf, len, "%lf", hrtime);
00244
00245 if (!ast_strlen_zero(buf)) {
00246 ret = buf;
00247 }
00248 } else {
00249 ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
00250 ast_test_flag(&flags, OPT_RECURSIVE),
00251 ast_test_flag(&flags, OPT_UNPARSED));
00252 }
00253
00254 return ret ? 0 : -1;
00255 }
00256
00257 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
00258 const char *value)
00259 {
00260 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00261 struct ast_flags flags = { 0 };
00262 AST_DECLARE_APP_ARGS(args,
00263 AST_APP_ARG(variable);
00264 AST_APP_ARG(options);
00265 );
00266
00267 if (ast_strlen_zero(parse) || !value || !chan)
00268 return -1;
00269
00270 if (!cdr)
00271 return -1;
00272
00273 AST_STANDARD_APP_ARGS(args, parse);
00274
00275 if (!ast_strlen_zero(args.options))
00276 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00277
00278 if (ast_test_flag(&flags, OPT_LAST))
00279 while (cdr->next)
00280 cdr = cdr->next;
00281
00282 if (!strcasecmp(args.variable, "accountcode"))
00283 ast_cdr_setaccount(chan, value);
00284 else if (!strcasecmp(args.variable, "peeraccount"))
00285 ast_cdr_setpeeraccount(chan, value);
00286 else if (!strcasecmp(args.variable, "userfield"))
00287 ast_cdr_setuserfield(chan, value);
00288 else if (!strcasecmp(args.variable, "amaflags"))
00289 ast_cdr_setamaflags(chan, value);
00290 else
00291 ast_cdr_setvar(cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
00292
00293
00294
00295 return 0;
00296 }
00297
00298 static struct ast_custom_function cdr_function = {
00299 .name = "CDR",
00300 .read = cdr_read,
00301 .write = cdr_write,
00302 };
00303
00304 static int unload_module(void)
00305 {
00306 return ast_custom_function_unregister(&cdr_function);
00307 }
00308
00309 static int load_module(void)
00310 {
00311 return ast_custom_function_register(&cdr_function);
00312 }
00313
00314 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan function");