| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/utils.h" |
| 38 |
#include "asterisk/app.h" |
| 39 |
#include "asterisk/cdr.h" |
| 40 |
#include "asterisk/stasis.h" |
| 41 |
#include "asterisk/stasis_message_router.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
enum cdr_option_flags { |
| 204 |
OPT_UNPARSED = (1 << 1), |
| 205 |
OPT_FLOAT = (1 << 2), |
| 206 |
}; |
| 207 |
|
| 208 |
AST_APP_OPTIONS(cdr_func_options, { |
| 209 |
AST_APP_OPTION('f', OPT_FLOAT), |
| 210 |
AST_APP_OPTION('u', OPT_UNPARSED), |
| 211 |
}); |
| 212 |
|
| 213 |
struct cdr_func_payload { |
| 214 |
struct ast_channel *chan; |
| 215 |
const char *cmd; |
| 216 |
const char *arguments; |
| 217 |
const char *value; |
| 218 |
void *data; |
| 219 |
}; |
| 220 |
|
| 221 |
struct cdr_func_data { |
| 222 |
char *buf; |
| 223 |
size_t len; |
| 224 |
}; |
| 225 |
|
| 226 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_read_message_type); |
| 227 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_write_message_type); |
| 228 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_prop_write_message_type); |
| 229 |
|
| 230 |
static struct timeval cdr_retrieve_time(struct ast_channel *chan, const char *time_name) |
| 231 |
{ |
| 232 |
struct timeval time = { 0 }; |
| 233 |
char *value = NULL; |
| 234 |
char tempbuf[128]; |
| 235 |
long int tv_sec; |
| 236 |
long int tv_usec; |
| 237 |
|
| 238 |
if (ast_strlen_zero(ast_channel_name(chan))) { |
| 239 |
|
| 240 |
ast_cdr_format_var(ast_channel_cdr(chan), time_name, &value, tempbuf, sizeof(tempbuf), 1); |
| 241 |
} else { |
| 242 |
ast_cdr_getvar(ast_channel_name(chan), time_name, tempbuf, sizeof(tempbuf)); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
if (sscanf(tempbuf, "%ld.%ld", &tv_sec, &tv_usec) == 2) { |
| 247 |
time.tv_sec = tv_sec; |
| 248 |
time.tv_usec = tv_usec; |
| 249 |
} else { |
| 250 |
ast_log(AST_LOG_WARNING, "Failed to fully extract '%s' from CDR\n", time_name); |
| 251 |
} |
| 252 |
|
| 253 |
return time; |
| 254 |
} |
| 255 |
|
| 256 |
static void cdr_read_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 257 |
{ |
| 258 |
struct cdr_func_payload *payload = stasis_message_data(message); |
| 259 |
struct cdr_func_data *output; |
| 260 |
char *info; |
| 261 |
char *value = NULL; |
| 262 |
struct ast_flags flags = { 0 }; |
| 263 |
char tempbuf[512]; |
| 264 |
AST_DECLARE_APP_ARGS(args, |
| 265 |
AST_APP_ARG(variable); |
| 266 |
AST_APP_ARG(options); |
| 267 |
); |
| 268 |
|
| 269 |
if (cdr_read_message_type() != stasis_message_type(message)) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
ast_assert(payload != NULL); |
| 274 |
output = payload->data; |
| 275 |
ast_assert(output != NULL); |
| 276 |
|
| 277 |
if (ast_strlen_zero(payload->arguments)) { |
| 278 |
ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable[,option]))\n)", |
| 279 |
payload->cmd, payload->cmd); |
| 280 |
return; |
| 281 |
} |
| 282 |
info = ast_strdupa(payload->arguments); |
| 283 |
AST_STANDARD_APP_ARGS(args, info); |
| 284 |
|
| 285 |
if (!ast_strlen_zero(args.options)) { |
| 286 |
ast_app_parse_options(cdr_func_options, &flags, NULL, args.options); |
| 287 |
} |
| 288 |
|
| 289 |
if (ast_strlen_zero(ast_channel_name(payload->chan))) { |
| 290 |
|
| 291 |
ast_cdr_format_var(ast_channel_cdr(payload->chan), args.variable, &value, tempbuf, sizeof(tempbuf), ast_test_flag(&flags, OPT_UNPARSED)); |
| 292 |
if (ast_strlen_zero(value)) { |
| 293 |
return; |
| 294 |
} |
| 295 |
ast_copy_string(tempbuf, value, sizeof(tempbuf)); |
| 296 |
ast_set_flag(&flags, OPT_UNPARSED); |
| 297 |
} else if (ast_cdr_getvar(ast_channel_name(payload->chan), args.variable, tempbuf, sizeof(tempbuf))) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
if (ast_test_flag(&flags, OPT_FLOAT) |
| 302 |
&& (!strcasecmp("billsec", args.variable) || !strcasecmp("duration", args.variable))) { |
| 303 |
struct timeval start = cdr_retrieve_time(payload->chan, !strcasecmp("billsec", args.variable) ? "answer" : "start"); |
| 304 |
struct timeval finish = cdr_retrieve_time(payload->chan, "end"); |
| 305 |
double delta; |
| 306 |
|
| 307 |
if (ast_tvzero(finish)) { |
| 308 |
finish = ast_tvnow(); |
| 309 |
} |
| 310 |
|
| 311 |
if (ast_tvzero(start)) { |
| 312 |
delta = 0.0; |
| 313 |
} else { |
| 314 |
delta = (double)(ast_tvdiff_us(finish, start) / 1000000.0); |
| 315 |
} |
| 316 |
snprintf(tempbuf, sizeof(tempbuf), "%lf", delta); |
| 317 |
|
| 318 |
} else if (!ast_test_flag(&flags, OPT_UNPARSED)) { |
| 319 |
if (!strcasecmp("start", args.variable) |
| 320 |
|| !strcasecmp("end", args.variable) |
| 321 |
|| !strcasecmp("answer", args.variable)) { |
| 322 |
struct timeval fmt_time; |
| 323 |
struct ast_tm tm; |
| 324 |
|
| 325 |
long int tv_sec; |
| 326 |
long int tv_usec; |
| 327 |
|
| 328 |
if (sscanf(tempbuf, "%ld.%ld", &tv_sec, &tv_usec) != 2) { |
| 329 |
ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n", |
| 330 |
args.variable, tempbuf, ast_channel_name(payload->chan)); |
| 331 |
return; |
| 332 |
} |
| 333 |
if (tv_sec) { |
| 334 |
fmt_time.tv_sec = tv_sec; |
| 335 |
fmt_time.tv_usec = tv_usec; |
| 336 |
ast_localtime(&fmt_time, &tm, NULL); |
| 337 |
ast_strftime(tempbuf, sizeof(tempbuf), "%Y-%m-%d %T", &tm); |
| 338 |
} else { |
| 339 |
tempbuf[0] = '\0'; |
| 340 |
} |
| 341 |
} else if (!strcasecmp("disposition", args.variable)) { |
| 342 |
int disposition; |
| 343 |
|
| 344 |
if (sscanf(tempbuf, "%8d", &disposition) != 1) { |
| 345 |
ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n", |
| 346 |
args.variable, tempbuf, ast_channel_name(payload->chan)); |
| 347 |
return; |
| 348 |
} |
| 349 |
snprintf(tempbuf, sizeof(tempbuf), "%s", ast_cdr_disp2str(disposition)); |
| 350 |
} else if (!strcasecmp("amaflags", args.variable)) { |
| 351 |
int amaflags; |
| 352 |
|
| 353 |
if (sscanf(tempbuf, "%8d", &amaflags) != 1) { |
| 354 |
ast_log(AST_LOG_WARNING, "Unable to parse %s (%s) from the CDR for channel %s\n", |
| 355 |
args.variable, tempbuf, ast_channel_name(payload->chan)); |
| 356 |
return; |
| 357 |
} |
| 358 |
snprintf(tempbuf, sizeof(tempbuf), "%s", ast_channel_amaflags2string(amaflags)); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
ast_copy_string(output->buf, tempbuf, output->len); |
| 363 |
} |
| 364 |
|
| 365 |
static void cdr_write_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 366 |
{ |
| 367 |
struct cdr_func_payload *payload; |
| 368 |
struct ast_flags flags = { 0 }; |
| 369 |
AST_DECLARE_APP_ARGS(args, |
| 370 |
AST_APP_ARG(variable); |
| 371 |
AST_APP_ARG(options); |
| 372 |
); |
| 373 |
char *parse; |
| 374 |
|
| 375 |
if (cdr_write_message_type() != stasis_message_type(message)) { |
| 376 |
return; |
| 377 |
} |
| 378 |
payload = stasis_message_data(message); |
| 379 |
if (!payload) { |
| 380 |
return; |
| 381 |
} |
| 382 |
if (ast_strlen_zero(payload->arguments) |
| 383 |
|| !payload->value) { |
| 384 |
|
| 385 |
ast_assert(0); |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
parse = ast_strdupa(payload->arguments); |
| 390 |
AST_STANDARD_APP_ARGS(args, parse); |
| 391 |
|
| 392 |
if (!ast_strlen_zero(args.options)) { |
| 393 |
ast_app_parse_options(cdr_func_options, &flags, NULL, args.options); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
ast_assert(strcasecmp(args.variable, "accountcode") |
| 398 |
&& strcasecmp(args.variable, "peeraccount") |
| 399 |
&& strcasecmp(args.variable, "amaflags")); |
| 400 |
|
| 401 |
if (!strcasecmp(args.variable, "userfield")) { |
| 402 |
ast_cdr_setuserfield(ast_channel_name(payload->chan), payload->value); |
| 403 |
} else { |
| 404 |
ast_cdr_setvar(ast_channel_name(payload->chan), args.variable, payload->value); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
static void cdr_prop_write_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 409 |
{ |
| 410 |
struct cdr_func_payload *payload = stasis_message_data(message); |
| 411 |
enum ast_cdr_options option; |
| 412 |
char *parse; |
| 413 |
AST_DECLARE_APP_ARGS(args, |
| 414 |
AST_APP_ARG(variable); |
| 415 |
AST_APP_ARG(options); |
| 416 |
); |
| 417 |
|
| 418 |
if (cdr_prop_write_message_type() != stasis_message_type(message)) { |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
if (!payload) { |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
if (ast_strlen_zero(payload->arguments)) { |
| 427 |
ast_log(AST_LOG_WARNING, "%s requires a variable (%s(variable)=value)\n)", |
| 428 |
payload->cmd, payload->cmd); |
| 429 |
return; |
| 430 |
} |
| 431 |
if (ast_strlen_zero(payload->value)) { |
| 432 |
ast_log(AST_LOG_WARNING, "%s requires a value (%s(variable)=value)\n)", |
| 433 |
payload->cmd, payload->cmd); |
| 434 |
return; |
| 435 |
} |
| 436 |
parse = ast_strdupa(payload->arguments); |
| 437 |
AST_STANDARD_APP_ARGS(args, parse); |
| 438 |
|
| 439 |
if (!strcasecmp("party_a", args.variable)) { |
| 440 |
option = AST_CDR_FLAG_PARTY_A; |
| 441 |
} else if (!strcasecmp("disable", args.variable)) { |
| 442 |
option = AST_CDR_FLAG_DISABLE_ALL; |
| 443 |
} else { |
| 444 |
ast_log(AST_LOG_WARNING, "Unknown option %s used with %s\n", args.variable, payload->cmd); |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
if (ast_true(payload->value)) { |
| 449 |
ast_cdr_set_property(ast_channel_name(payload->chan), option); |
| 450 |
} else { |
| 451 |
ast_cdr_clear_property(ast_channel_name(payload->chan), option); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
static int cdr_read(struct ast_channel *chan, const char *cmd, char *parse, |
| 457 |
char *buf, size_t len) |
| 458 |
{ |
| 459 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 460 |
RAII_VAR(struct cdr_func_payload *, payload, NULL, ao2_cleanup); |
| 461 |
struct cdr_func_data output = { 0, }; |
| 462 |
|
| 463 |
if (!chan) { |
| 464 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 465 |
return -1; |
| 466 |
} |
| 467 |
|
| 468 |
if (!cdr_read_message_type()) { |
| 469 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: message type not available\n", |
| 470 |
ast_channel_name(chan)); |
| 471 |
return -1; |
| 472 |
} |
| 473 |
|
| 474 |
payload = ao2_alloc(sizeof(*payload), NULL); |
| 475 |
if (!payload) { |
| 476 |
return -1; |
| 477 |
} |
| 478 |
payload->chan = chan; |
| 479 |
payload->cmd = cmd; |
| 480 |
payload->arguments = parse; |
| 481 |
payload->data = &output; |
| 482 |
|
| 483 |
buf[0] = '\0'; |
| 484 |
output.buf = buf; |
| 485 |
output.len = len; |
| 486 |
|
| 487 |
message = stasis_message_create(cdr_read_message_type(), payload); |
| 488 |
if (!message) { |
| 489 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n", |
| 490 |
ast_channel_name(chan)); |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
if (ast_strlen_zero(ast_channel_name(chan))) { |
| 499 |
cdr_read_callback(NULL, NULL, message); |
| 500 |
} else { |
| 501 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 502 |
|
| 503 |
if (!router) { |
| 504 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n", |
| 505 |
ast_channel_name(chan)); |
| 506 |
return -1; |
| 507 |
} |
| 508 |
stasis_message_router_publish_sync(router, message); |
| 509 |
} |
| 510 |
|
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
static int cdr_write(struct ast_channel *chan, const char *cmd, char *arguments, |
| 515 |
const char *value) |
| 516 |
{ |
| 517 |
struct stasis_message *message; |
| 518 |
struct cdr_func_payload *payload; |
| 519 |
struct stasis_message_router *router; |
| 520 |
AST_DECLARE_APP_ARGS(args, |
| 521 |
AST_APP_ARG(variable); |
| 522 |
AST_APP_ARG(options); |
| 523 |
); |
| 524 |
char *parse; |
| 525 |
|
| 526 |
if (!chan) { |
| 527 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 528 |
return -1; |
| 529 |
} |
| 530 |
if (ast_strlen_zero(arguments)) { |
| 531 |
ast_log(LOG_WARNING, "%s requires a variable (%s(variable)=value)\n)", |
| 532 |
cmd, cmd); |
| 533 |
return -1; |
| 534 |
} |
| 535 |
if (!value) { |
| 536 |
ast_log(LOG_WARNING, "%s requires a value (%s(variable)=value)\n)", |
| 537 |
cmd, cmd); |
| 538 |
return -1; |
| 539 |
} |
| 540 |
|
| 541 |
parse = ast_strdupa(arguments); |
| 542 |
AST_STANDARD_APP_ARGS(args, parse); |
| 543 |
|
| 544 |
|
| 545 |
if (!strcasecmp(args.variable, "accountcode")) { |
| 546 |
ast_log(LOG_WARNING, "Using the %s function to set 'accountcode' is deprecated. Please use the CHANNEL function instead.\n", |
| 547 |
cmd); |
| 548 |
ast_channel_lock(chan); |
| 549 |
ast_channel_accountcode_set(chan, value); |
| 550 |
ast_channel_unlock(chan); |
| 551 |
return 0; |
| 552 |
} |
| 553 |
if (!strcasecmp(args.variable, "amaflags")) { |
| 554 |
int amaflags; |
| 555 |
|
| 556 |
ast_log(LOG_WARNING, "Using the %s function to set 'amaflags' is deprecated. Please use the CHANNEL function instead.\n", |
| 557 |
cmd); |
| 558 |
if (isdigit(*value)) { |
| 559 |
if (sscanf(value, "%30d", &amaflags) != 1) { |
| 560 |
amaflags = AST_AMA_NONE; |
| 561 |
} |
| 562 |
} else { |
| 563 |
amaflags = ast_channel_string2amaflag(value); |
| 564 |
} |
| 565 |
ast_channel_lock(chan); |
| 566 |
ast_channel_amaflags_set(chan, amaflags); |
| 567 |
ast_channel_unlock(chan); |
| 568 |
return 0; |
| 569 |
} |
| 570 |
if (!strcasecmp(args.variable, "peeraccount")) { |
| 571 |
ast_log(LOG_WARNING, "The 'peeraccount' setting is not supported. Please set the 'accountcode' on the appropriate channel using the CHANNEL function.\n"); |
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
if (!cdr_write_message_type()) { |
| 577 |
ast_log(LOG_WARNING, "Failed to manipulate CDR for channel %s: message type not available\n", |
| 578 |
ast_channel_name(chan)); |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
payload = ao2_alloc(sizeof(*payload), NULL); |
| 583 |
if (!payload) { |
| 584 |
return -1; |
| 585 |
} |
| 586 |
payload->chan = chan; |
| 587 |
payload->cmd = cmd; |
| 588 |
payload->arguments = arguments; |
| 589 |
payload->value = value; |
| 590 |
|
| 591 |
message = stasis_message_create(cdr_write_message_type(), payload); |
| 592 |
ao2_ref(payload, -1); |
| 593 |
if (!message) { |
| 594 |
ast_log(LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n", |
| 595 |
ast_channel_name(chan)); |
| 596 |
return -1; |
| 597 |
} |
| 598 |
router = ast_cdr_message_router(); |
| 599 |
if (!router) { |
| 600 |
ast_log(LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n", |
| 601 |
ast_channel_name(chan)); |
| 602 |
ao2_ref(message, -1); |
| 603 |
return -1; |
| 604 |
} |
| 605 |
stasis_message_router_publish_sync(router, message); |
| 606 |
ao2_ref(router, -1); |
| 607 |
ao2_ref(message, -1); |
| 608 |
|
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
static int cdr_prop_write(struct ast_channel *chan, const char *cmd, char *parse, |
| 613 |
const char *value) |
| 614 |
{ |
| 615 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 616 |
RAII_VAR(struct cdr_func_payload *, payload, NULL, ao2_cleanup); |
| 617 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 618 |
|
| 619 |
if (!chan) { |
| 620 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 621 |
return -1; |
| 622 |
} |
| 623 |
|
| 624 |
if (!router) { |
| 625 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n", |
| 626 |
ast_channel_name(chan)); |
| 627 |
return -1; |
| 628 |
} |
| 629 |
|
| 630 |
if (!cdr_prop_write_message_type()) { |
| 631 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: message type not available\n", |
| 632 |
ast_channel_name(chan)); |
| 633 |
return -1; |
| 634 |
} |
| 635 |
|
| 636 |
payload = ao2_alloc(sizeof(*payload), NULL); |
| 637 |
if (!payload) { |
| 638 |
return -1; |
| 639 |
} |
| 640 |
payload->chan = chan; |
| 641 |
payload->cmd = cmd; |
| 642 |
payload->arguments = parse; |
| 643 |
payload->value = value; |
| 644 |
|
| 645 |
message = stasis_message_create(cdr_prop_write_message_type(), payload); |
| 646 |
if (!message) { |
| 647 |
ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n", |
| 648 |
ast_channel_name(chan)); |
| 649 |
return -1; |
| 650 |
} |
| 651 |
stasis_message_router_publish_sync(router, message); |
| 652 |
|
| 653 |
return 0; |
| 654 |
} |
| 655 |
|
| 656 |
static struct ast_custom_function cdr_function = { |
| 657 |
.name = "CDR", |
| 658 |
.read = cdr_read, |
| 659 |
.write = cdr_write, |
| 660 |
}; |
| 661 |
|
| 662 |
static struct ast_custom_function cdr_prop_function = { |
| 663 |
.name = "CDR_PROP", |
| 664 |
.read = NULL, |
| 665 |
.write = cdr_prop_write, |
| 666 |
}; |
| 667 |
|
| 668 |
static int unload_module(void) |
| 669 |
{ |
| 670 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 671 |
int res = 0; |
| 672 |
|
| 673 |
if (router) { |
| 674 |
stasis_message_router_remove(router, cdr_prop_write_message_type()); |
| 675 |
stasis_message_router_remove(router, cdr_write_message_type()); |
| 676 |
stasis_message_router_remove(router, cdr_read_message_type()); |
| 677 |
} |
| 678 |
STASIS_MESSAGE_TYPE_CLEANUP(cdr_read_message_type); |
| 679 |
STASIS_MESSAGE_TYPE_CLEANUP(cdr_write_message_type); |
| 680 |
STASIS_MESSAGE_TYPE_CLEANUP(cdr_prop_write_message_type); |
| 681 |
res |= ast_custom_function_unregister(&cdr_function); |
| 682 |
res |= ast_custom_function_unregister(&cdr_prop_function); |
| 683 |
|
| 684 |
return res; |
| 685 |
} |
| 686 |
|
| 687 |
static int load_module(void) |
| 688 |
{ |
| 689 |
RAII_VAR(struct stasis_message_router *, router, ast_cdr_message_router(), ao2_cleanup); |
| 690 |
int res = 0; |
| 691 |
|
| 692 |
if (!router) { |
| 693 |
return AST_MODULE_LOAD_DECLINE; |
| 694 |
} |
| 695 |
|
| 696 |
res |= STASIS_MESSAGE_TYPE_INIT(cdr_read_message_type); |
| 697 |
res |= STASIS_MESSAGE_TYPE_INIT(cdr_write_message_type); |
| 698 |
res |= STASIS_MESSAGE_TYPE_INIT(cdr_prop_write_message_type); |
| 699 |
res |= ast_custom_function_register(&cdr_function); |
| 700 |
res |= ast_custom_function_register(&cdr_prop_function); |
| 701 |
res |= stasis_message_router_add(router, cdr_prop_write_message_type(), |
| 702 |
cdr_prop_write_callback, NULL); |
| 703 |
res |= stasis_message_router_add(router, cdr_write_message_type(), |
| 704 |
cdr_write_callback, NULL); |
| 705 |
res |= stasis_message_router_add(router, cdr_read_message_type(), |
| 706 |
cdr_read_callback, NULL); |
| 707 |
|
| 708 |
if (res) { |
| 709 |
unload_module(); |
| 710 |
return AST_MODULE_LOAD_DECLINE; |
| 711 |
} |
| 712 |
return AST_MODULE_LOAD_SUCCESS; |
| 713 |
} |
| 714 |
|
| 715 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Call Detail Record (CDR) dialplan functions", |
| 716 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 717 |
.load = load_module, |
| 718 |
.unload = unload_module, |
| 719 |
.requires = "cdr", |
| 720 |
); |