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
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00035
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/cdr.h"
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
00176
00177
00178
00179 enum cdr_option_flags {
00180 OPT_RECURSIVE = (1 << 0),
00181 OPT_UNPARSED = (1 << 1),
00182 OPT_LAST = (1 << 2),
00183 OPT_SKIPLOCKED = (1 << 3),
00184 OPT_FLOAT = (1 << 4),
00185 };
00186
00187 AST_APP_OPTIONS(cdr_func_options, {
00188 AST_APP_OPTION('f', OPT_FLOAT),
00189 AST_APP_OPTION('l', OPT_LAST),
00190 AST_APP_OPTION('r', OPT_RECURSIVE),
00191 AST_APP_OPTION('s', OPT_SKIPLOCKED),
00192 AST_APP_OPTION('u', OPT_UNPARSED),
00193 });
00194
00195 static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse,
00196 char *buf, size_t len)
00197 {
00198 char *ret;
00199 struct ast_flags flags = { 0 };
00200 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00201 AST_DECLARE_APP_ARGS(args,
00202 AST_APP_ARG(variable);
00203 AST_APP_ARG(options);
00204 );
00205
00206 if (ast_strlen_zero(parse))
00207 return -1;
00208
00209 if (!cdr)
00210 return -1;
00211
00212 AST_STANDARD_APP_ARGS(args, parse);
00213
00214 if (!ast_strlen_zero(args.options))
00215 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00216
00217 if (ast_test_flag(&flags, OPT_LAST))
00218 while (cdr->next)
00219 cdr = cdr->next;
00220
00221 if (ast_test_flag(&flags, OPT_SKIPLOCKED))
00222 while (ast_test_flag(cdr, AST_CDR_FLAG_LOCKED) && cdr->next)
00223 cdr = cdr->next;
00224
00225 if (!strcasecmp("billsec", args.variable) && ast_test_flag(&flags, OPT_FLOAT)) {
00226 if (!ast_tvzero(cdr->answer)) {
00227 double hrtime;
00228
00229 if(!ast_tvzero(cdr->end))
00230 hrtime = (double)(ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
00231 else
00232 hrtime = (double)(ast_tvdiff_us(ast_tvnow(), cdr->answer) / 1000000.0);
00233
00234 snprintf(buf, len, "%lf", hrtime);
00235 } else {
00236 snprintf(buf, len, "%lf", 0.0);
00237 }
00238 ret = buf;
00239 } else if (!strcasecmp("duration", args.variable) && ast_test_flag(&flags, OPT_FLOAT)) {
00240 double hrtime;
00241
00242 if(!ast_tvzero(cdr->end))
00243 hrtime = (double)(ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
00244 else
00245 hrtime = (double)(ast_tvdiff_us(ast_tvnow(), cdr->start) / 1000000.0);
00246
00247 snprintf(buf, len, "%lf", hrtime);
00248
00249 if (!ast_strlen_zero(buf)) {
00250 ret = buf;
00251 }
00252 } else {
00253 ast_cdr_getvar(cdr, args.variable, &ret, buf, len,
00254 ast_test_flag(&flags, OPT_RECURSIVE),
00255 ast_test_flag(&flags, OPT_UNPARSED));
00256 }
00257
00258 return ret ? 0 : -1;
00259 }
00260
00261 static int cdr_write(struct ast_channel *chan, const char *cmd, char *parse,
00262 const char *value)
00263 {
00264 struct ast_cdr *cdr = chan ? chan->cdr : NULL;
00265 struct ast_flags flags = { 0 };
00266 AST_DECLARE_APP_ARGS(args,
00267 AST_APP_ARG(variable);
00268 AST_APP_ARG(options);
00269 );
00270
00271 if (ast_strlen_zero(parse) || !value || !chan)
00272 return -1;
00273
00274 if (!cdr)
00275 return -1;
00276
00277 AST_STANDARD_APP_ARGS(args, parse);
00278
00279 if (!ast_strlen_zero(args.options))
00280 ast_app_parse_options(cdr_func_options, &flags, NULL, args.options);
00281
00282 if (ast_test_flag(&flags, OPT_LAST))
00283 while (cdr->next)
00284 cdr = cdr->next;
00285
00286 if (!strcasecmp(args.variable, "accountcode"))
00287 ast_cdr_setaccount(chan, value);
00288 else if (!strcasecmp(args.variable, "peeraccount"))
00289 ast_cdr_setpeeraccount(chan, value);
00290 else if (!strcasecmp(args.variable, "userfield"))
00291 ast_cdr_setuserfield(chan, value);
00292 else if (!strcasecmp(args.variable, "amaflags"))
00293 ast_cdr_setamaflags(chan, value);
00294 else
00295 ast_cdr_setvar(cdr, args.variable, value, ast_test_flag(&flags, OPT_RECURSIVE));
00296
00297
00298
00299 return 0;
00300 }
00301
00302 static struct ast_custom_function cdr_function = {
00303 .name = "CDR",
00304 .read = cdr_read,
00305 .write = cdr_write,
00306 };
00307
00308 static int unload_module(void)
00309 {
00310 return ast_custom_function_unregister(&cdr_function);
00311 }
00312
00313 static int load_module(void)
00314 {
00315 return ast_custom_function_register(&cdr_function);
00316 }
00317
00318 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Detail Record (CDR) dialplan function");