| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#include "asterisk.h" |
| 47 |
|
| 48 |
#include <signal.h> |
| 49 |
#include <inttypes.h> |
| 50 |
|
| 51 |
#include "asterisk/lock.h" |
| 52 |
#include "asterisk/channel.h" |
| 53 |
#include "asterisk/cdr.h" |
| 54 |
#include "asterisk/callerid.h" |
| 55 |
#include "asterisk/manager.h" |
| 56 |
#include "asterisk/module.h" |
| 57 |
#include "asterisk/causes.h" |
| 58 |
#include "asterisk/linkedlists.h" |
| 59 |
#include "asterisk/utils.h" |
| 60 |
#include "asterisk/sched.h" |
| 61 |
#include "asterisk/config.h" |
| 62 |
#include "asterisk/cli.h" |
| 63 |
#include "asterisk/stringfields.h" |
| 64 |
#include "asterisk/config_options.h" |
| 65 |
#include "asterisk/json.h" |
| 66 |
#include "asterisk/parking.h" |
| 67 |
#include "asterisk/stasis.h" |
| 68 |
#include "asterisk/stasis_channels.h" |
| 69 |
#include "asterisk/stasis_bridges.h" |
| 70 |
#include "asterisk/stasis_message_router.h" |
| 71 |
#include "asterisk/astobj2.h" |
| 72 |
#include "asterisk/taskprocessor.h" |
| 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 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
#define DEFAULT_ENABLED "1" |
| 290 |
#define DEFAULT_BATCHMODE "0" |
| 291 |
#define DEFAULT_UNANSWERED "0" |
| 292 |
#define DEFAULT_CONGESTION "0" |
| 293 |
#define DEFAULT_END_BEFORE_H_EXTEN "1" |
| 294 |
#define DEFAULT_INITIATED_SECONDS "0" |
| 295 |
#define DEFAULT_CHANNEL_ENABLED "1" |
| 296 |
#define DEFAULT_IGNORE_STATE_CHANGES "0" |
| 297 |
#define DEFAULT_IGNORE_DIAL_CHANGES "0" |
| 298 |
|
| 299 |
#define DEFAULT_BATCH_SIZE "100" |
| 300 |
#define MAX_BATCH_SIZE 1000 |
| 301 |
#define DEFAULT_BATCH_TIME "300" |
| 302 |
#define MAX_BATCH_TIME 86400 |
| 303 |
#define DEFAULT_BATCH_SCHEDULER_ONLY "0" |
| 304 |
#define DEFAULT_BATCH_SAFE_SHUTDOWN "1" |
| 305 |
|
| 306 |
#define cdr_set_debug_mode(mod_cfg) \ |
| 307 |
do { \ |
| 308 |
cdr_debug_enabled = ast_test_flag(&(mod_cfg)->general->settings, CDR_DEBUG); \ |
| 309 |
} while (0) |
| 310 |
|
| 311 |
static int cdr_debug_enabled; |
| 312 |
static int dial_changes_ignored; |
| 313 |
|
| 314 |
#define CDR_DEBUG(fmt, ...) \ |
| 315 |
do { \ |
| 316 |
if (cdr_debug_enabled) { \ |
| 317 |
ast_verbose((fmt), ##__VA_ARGS__); \ |
| 318 |
} \ |
| 319 |
} while (0) |
| 320 |
|
| 321 |
static void cdr_detach(struct ast_cdr *cdr); |
| 322 |
static void cdr_submit_batch(int shutdown); |
| 323 |
static int cdr_toggle_runtime_options(void); |
| 324 |
|
| 325 |
|
| 326 |
struct module_config { |
| 327 |
struct ast_cdr_config *general; |
| 328 |
}; |
| 329 |
|
| 330 |
|
| 331 |
static AO2_GLOBAL_OBJ_STATIC(module_configs); |
| 332 |
|
| 333 |
|
| 334 |
static struct aco_type general_option = { |
| 335 |
.type = ACO_GLOBAL, |
| 336 |
.name = "general", |
| 337 |
.item_offset = offsetof(struct module_config, general), |
| 338 |
.category = "general", |
| 339 |
.category_match = ACO_WHITELIST_EXACT, |
| 340 |
}; |
| 341 |
|
| 342 |
|
| 343 |
static const char *ignore_categories[] = { |
| 344 |
"csv", |
| 345 |
"custom", |
| 346 |
"manager", |
| 347 |
"odbc", |
| 348 |
"pgsql", |
| 349 |
"radius", |
| 350 |
"sqlite", |
| 351 |
"tds", |
| 352 |
"mysql", |
| 353 |
NULL, |
| 354 |
}; |
| 355 |
|
| 356 |
static struct aco_type ignore_option = { |
| 357 |
.type = ACO_IGNORE, |
| 358 |
.name = "modules", |
| 359 |
.category = (const char*)ignore_categories, |
| 360 |
.category_match = ACO_WHITELIST_ARRAY, |
| 361 |
}; |
| 362 |
|
| 363 |
static void *module_config_alloc(void); |
| 364 |
static void module_config_destructor(void *obj); |
| 365 |
static void module_config_post_apply(void); |
| 366 |
|
| 367 |
|
| 368 |
static struct aco_file module_file_conf = { |
| 369 |
.filename = "cdr.conf", |
| 370 |
.types = ACO_TYPES(&general_option, &ignore_option), |
| 371 |
}; |
| 372 |
|
| 373 |
CONFIG_INFO_CORE("cdr", cfg_info, module_configs, module_config_alloc, |
| 374 |
.files = ACO_FILES(&module_file_conf), |
| 375 |
.post_apply_config = module_config_post_apply, |
| 376 |
); |
| 377 |
|
| 378 |
static struct aco_type *general_options[] = ACO_TYPES(&general_option); |
| 379 |
|
| 380 |
static void module_config_post_apply(void) |
| 381 |
{ |
| 382 |
struct module_config *mod_cfg; |
| 383 |
|
| 384 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 385 |
if (!mod_cfg) { |
| 386 |
return; |
| 387 |
} |
| 388 |
cdr_set_debug_mode(mod_cfg); |
| 389 |
ao2_cleanup(mod_cfg); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
static void module_config_destructor(void *obj) |
| 394 |
{ |
| 395 |
struct module_config *cfg = obj; |
| 396 |
|
| 397 |
if (!cfg) { |
| 398 |
return; |
| 399 |
} |
| 400 |
ao2_ref(cfg->general, -1); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
static void *module_config_alloc(void) |
| 405 |
{ |
| 406 |
struct module_config *mod_cfg; |
| 407 |
struct ast_cdr_config *cdr_config; |
| 408 |
|
| 409 |
mod_cfg = ao2_alloc(sizeof(*mod_cfg), module_config_destructor); |
| 410 |
if (!mod_cfg) { |
| 411 |
return NULL; |
| 412 |
} |
| 413 |
|
| 414 |
cdr_config = ao2_alloc(sizeof(*cdr_config), NULL); |
| 415 |
if (!cdr_config) { |
| 416 |
ao2_ref(cdr_config, -1); |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
mod_cfg->general = cdr_config; |
| 420 |
|
| 421 |
return mod_cfg; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
struct cdr_beitem { |
| 426 |
char name[20]; |
| 427 |
char desc[80]; |
| 428 |
ast_cdrbe be; |
| 429 |
AST_RWLIST_ENTRY(cdr_beitem) list; |
| 430 |
int suspended:1; |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
static AST_RWLIST_HEAD_STATIC(be_list, cdr_beitem); |
| 435 |
|
| 436 |
|
| 437 |
static AST_RWLIST_HEAD_STATIC(mo_list, cdr_beitem); |
| 438 |
|
| 439 |
|
| 440 |
struct cdr_batch_item { |
| 441 |
struct ast_cdr *cdr; |
| 442 |
struct cdr_batch_item *next; |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
static struct cdr_batch { |
| 447 |
int size; |
| 448 |
struct cdr_batch_item *head; |
| 449 |
struct cdr_batch_item *tail; |
| 450 |
} *batch = NULL; |
| 451 |
|
| 452 |
|
| 453 |
static int global_cdr_sequence = 0; |
| 454 |
|
| 455 |
|
| 456 |
static struct ast_sched_context *sched; |
| 457 |
static int cdr_sched = -1; |
| 458 |
AST_MUTEX_DEFINE_STATIC(cdr_sched_lock); |
| 459 |
static pthread_t cdr_thread = AST_PTHREADT_NULL; |
| 460 |
|
| 461 |
|
| 462 |
AST_MUTEX_DEFINE_STATIC(cdr_batch_lock); |
| 463 |
|
| 464 |
|
| 465 |
AST_MUTEX_DEFINE_STATIC(cdr_pending_lock); |
| 466 |
static ast_cond_t cdr_pending_cond; |
| 467 |
|
| 468 |
|
| 469 |
static struct ao2_container *active_cdrs_master; |
| 470 |
|
| 471 |
|
| 472 |
static struct ao2_container *active_cdrs_all; |
| 473 |
|
| 474 |
|
| 475 |
static struct stasis_message_router *stasis_router; |
| 476 |
|
| 477 |
|
| 478 |
static struct stasis_forward *bridge_subscription; |
| 479 |
|
| 480 |
|
| 481 |
static struct stasis_forward *channel_subscription; |
| 482 |
|
| 483 |
|
| 484 |
static struct stasis_forward *parking_subscription; |
| 485 |
|
| 486 |
|
| 487 |
static struct stasis_topic *cdr_topic; |
| 488 |
|
| 489 |
|
| 490 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_sync_message_type); |
| 491 |
|
| 492 |
struct cdr_object; |
| 493 |
|
| 494 |
|
| 495 |
enum process_bridge_enter_results { |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
BRIDGE_ENTER_ONLY_PARTY, |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
BRIDGE_ENTER_OBTAINED_PARTY_B, |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
BRIDGE_ENTER_NO_PARTY_B, |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
BRIDGE_ENTER_NEED_CDR, |
| 512 |
}; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
struct cdr_object_fn_table { |
| 521 |
|
| 522 |
const char *name; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
void (* const init_function)(struct cdr_object *cdr); |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
int (* const process_party_a)(struct cdr_object *cdr, |
| 542 |
struct ast_channel_snapshot *snapshot); |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
void (* const process_party_b)(struct cdr_object *cdr, |
| 551 |
struct ast_channel_snapshot *snapshot); |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
int (* const process_dial_begin)(struct cdr_object *cdr, |
| 567 |
struct ast_channel_snapshot *caller, |
| 568 |
struct ast_channel_snapshot *peer); |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
int (* const process_dial_end)(struct cdr_object *cdr, |
| 585 |
struct ast_channel_snapshot *caller, |
| 586 |
struct ast_channel_snapshot *peer, |
| 587 |
const char *dial_status); |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
enum process_bridge_enter_results (* const process_bridge_enter)( |
| 608 |
struct cdr_object *cdr, |
| 609 |
struct ast_bridge_snapshot *bridge, |
| 610 |
struct ast_channel_snapshot *channel); |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
int (* const process_parking_bridge_enter)(struct cdr_object *cdr, |
| 624 |
struct ast_bridge_snapshot *bridge, |
| 625 |
struct ast_channel_snapshot *channel); |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
int (* const process_bridge_leave)(struct cdr_object *cdr, |
| 638 |
struct ast_bridge_snapshot *bridge, |
| 639 |
struct ast_channel_snapshot *channel); |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
int (* const process_parked_channel)(struct cdr_object *cdr, |
| 651 |
struct ast_parked_call_payload *parking_info); |
| 652 |
}; |
| 653 |
|
| 654 |
static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot); |
| 655 |
static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 656 |
static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 657 |
static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status); |
| 658 |
static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info); |
| 659 |
|
| 660 |
static void single_state_init_function(struct cdr_object *cdr); |
| 661 |
static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot); |
| 662 |
static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer); |
| 663 |
static enum process_bridge_enter_results single_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 664 |
static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
struct cdr_object_fn_table single_state_fn_table = { |
| 678 |
.name = "Single", |
| 679 |
.init_function = single_state_init_function, |
| 680 |
.process_party_a = base_process_party_a, |
| 681 |
.process_party_b = single_state_process_party_b, |
| 682 |
.process_dial_begin = single_state_process_dial_begin, |
| 683 |
.process_dial_end = base_process_dial_end, |
| 684 |
.process_bridge_enter = single_state_process_bridge_enter, |
| 685 |
.process_parking_bridge_enter = single_state_process_parking_bridge_enter, |
| 686 |
.process_bridge_leave = base_process_bridge_leave, |
| 687 |
.process_parked_channel = base_process_parked_channel, |
| 688 |
}; |
| 689 |
|
| 690 |
static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot); |
| 691 |
static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer); |
| 692 |
static int dial_state_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status); |
| 693 |
static enum process_bridge_enter_results dial_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
struct cdr_object_fn_table dial_state_fn_table = { |
| 709 |
.name = "Dial", |
| 710 |
.process_party_a = base_process_party_a, |
| 711 |
.process_party_b = dial_state_process_party_b, |
| 712 |
.process_dial_begin = dial_state_process_dial_begin, |
| 713 |
.process_dial_end = dial_state_process_dial_end, |
| 714 |
.process_bridge_enter = dial_state_process_bridge_enter, |
| 715 |
.process_bridge_leave = base_process_bridge_leave, |
| 716 |
}; |
| 717 |
|
| 718 |
static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot); |
| 719 |
static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer); |
| 720 |
static enum process_bridge_enter_results dialed_pending_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 721 |
static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
struct cdr_object_fn_table dialed_pending_state_fn_table = { |
| 742 |
.name = "DialedPending", |
| 743 |
.process_party_a = dialed_pending_state_process_party_a, |
| 744 |
.process_dial_begin = dialed_pending_state_process_dial_begin, |
| 745 |
.process_bridge_enter = dialed_pending_state_process_bridge_enter, |
| 746 |
.process_parking_bridge_enter = dialed_pending_state_process_parking_bridge_enter, |
| 747 |
.process_bridge_leave = base_process_bridge_leave, |
| 748 |
.process_parked_channel = base_process_parked_channel, |
| 749 |
}; |
| 750 |
|
| 751 |
static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot); |
| 752 |
static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
struct cdr_object_fn_table bridge_state_fn_table = { |
| 764 |
.name = "Bridged", |
| 765 |
.process_party_a = base_process_party_a, |
| 766 |
.process_party_b = bridge_state_process_party_b, |
| 767 |
.process_bridge_leave = bridge_state_process_bridge_leave, |
| 768 |
.process_parked_channel = base_process_parked_channel, |
| 769 |
}; |
| 770 |
|
| 771 |
static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel); |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
struct cdr_object_fn_table parked_state_fn_table = { |
| 783 |
.name = "Parked", |
| 784 |
.process_party_a = base_process_party_a, |
| 785 |
.process_bridge_leave = parked_state_process_bridge_leave, |
| 786 |
.process_parked_channel = base_process_parked_channel, |
| 787 |
}; |
| 788 |
|
| 789 |
static void finalized_state_init_function(struct cdr_object *cdr); |
| 790 |
static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot); |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
struct cdr_object_fn_table finalized_state_fn_table = { |
| 799 |
.name = "Finalized", |
| 800 |
.init_function = finalized_state_init_function, |
| 801 |
.process_party_a = finalized_state_process_party_a, |
| 802 |
.process_bridge_enter = base_process_bridge_enter, |
| 803 |
}; |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
struct cdr_object_snapshot { |
| 809 |
struct ast_channel_snapshot *snapshot; |
| 810 |
char userfield[AST_MAX_USER_FIELD]; |
| 811 |
unsigned int flags; |
| 812 |
struct varshead variables; |
| 813 |
}; |
| 814 |
|
| 815 |
|
| 816 |
struct cdr_object { |
| 817 |
struct cdr_object_snapshot party_a; |
| 818 |
struct cdr_object_snapshot party_b; |
| 819 |
struct cdr_object_fn_table *fn_table; |
| 820 |
|
| 821 |
enum ast_cdr_disposition disposition; |
| 822 |
struct timeval start; |
| 823 |
struct timeval answer; |
| 824 |
struct timeval end; |
| 825 |
struct timeval lastevent; |
| 826 |
unsigned int sequence; |
| 827 |
struct ast_flags flags; |
| 828 |
AST_DECLARE_STRING_FIELDS( |
| 829 |
AST_STRING_FIELD(linkedid); |
| 830 |
AST_STRING_FIELD(uniqueid); |
| 831 |
AST_STRING_FIELD(tenantid); |
| 832 |
AST_STRING_FIELD(name); |
| 833 |
AST_STRING_FIELD(bridge); |
| 834 |
AST_STRING_FIELD(appl); |
| 835 |
AST_STRING_FIELD(data); |
| 836 |
AST_STRING_FIELD(context); |
| 837 |
AST_STRING_FIELD(exten); |
| 838 |
AST_STRING_FIELD(party_b_name); |
| 839 |
); |
| 840 |
struct cdr_object *next; |
| 841 |
struct cdr_object *last; |
| 842 |
int is_root; |
| 843 |
}; |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
static int copy_variables(struct varshead *to_list, struct varshead *from_list) |
| 852 |
{ |
| 853 |
struct ast_var_t *variables; |
| 854 |
struct ast_var_t *newvariable; |
| 855 |
const char *var; |
| 856 |
const char *val; |
| 857 |
int x = 0; |
| 858 |
|
| 859 |
AST_LIST_TRAVERSE(from_list, variables, entries) { |
| 860 |
var = ast_var_name(variables); |
| 861 |
if (ast_strlen_zero(var)) { |
| 862 |
continue; |
| 863 |
} |
| 864 |
val = ast_var_value(variables); |
| 865 |
if (ast_strlen_zero(val)) { |
| 866 |
continue; |
| 867 |
} |
| 868 |
newvariable = ast_var_assign(var, val); |
| 869 |
if (newvariable) { |
| 870 |
AST_LIST_INSERT_HEAD(to_list, newvariable, entries); |
| 871 |
++x; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
return x; |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
static void free_variables(struct varshead *headp) |
| 883 |
{ |
| 884 |
struct ast_var_t *vardata; |
| 885 |
|
| 886 |
while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) { |
| 887 |
ast_var_delete(vardata); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
static void cdr_object_snapshot_copy(struct cdr_object_snapshot *dst, struct cdr_object_snapshot *src) |
| 897 |
{ |
| 898 |
ao2_t_replace(dst->snapshot, src->snapshot, "CDR snapshot copy"); |
| 899 |
strcpy(dst->userfield, src->userfield); |
| 900 |
dst->flags = src->flags; |
| 901 |
copy_variables(&dst->variables, &src->variables); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
static void cdr_object_transition_state_init(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table, int do_init) |
| 911 |
{ |
| 912 |
CDR_DEBUG("%p - Transitioning CDR for %s from state %s to %s\n", |
| 913 |
cdr, cdr->party_a.snapshot->base->name, |
| 914 |
cdr->fn_table ? cdr->fn_table->name : "NONE", fn_table->name); |
| 915 |
cdr->fn_table = fn_table; |
| 916 |
|
| 917 |
if (cdr->fn_table->init_function && do_init) { |
| 918 |
cdr->fn_table->init_function(cdr); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
static void cdr_object_transition_state(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table) |
| 928 |
{ |
| 929 |
cdr_object_transition_state_init(cdr, fn_table, 1); |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
static int cdr_master_hash_fn(const void *obj, const int flags) |
| 937 |
{ |
| 938 |
const struct cdr_object *cdr; |
| 939 |
const char *key; |
| 940 |
|
| 941 |
switch (flags & OBJ_SEARCH_MASK) { |
| 942 |
case OBJ_SEARCH_KEY: |
| 943 |
key = obj; |
| 944 |
break; |
| 945 |
case OBJ_SEARCH_OBJECT: |
| 946 |
cdr = obj; |
| 947 |
key = cdr->uniqueid; |
| 948 |
break; |
| 949 |
default: |
| 950 |
ast_assert(0); |
| 951 |
return 0; |
| 952 |
} |
| 953 |
return ast_str_case_hash(key); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
static int cdr_master_cmp_fn(void *obj, void *arg, int flags) |
| 961 |
{ |
| 962 |
struct cdr_object *left = obj; |
| 963 |
struct cdr_object *right = arg; |
| 964 |
const char *right_key = arg; |
| 965 |
int cmp; |
| 966 |
|
| 967 |
switch (flags & OBJ_SEARCH_MASK) { |
| 968 |
case OBJ_SEARCH_OBJECT: |
| 969 |
right_key = right->uniqueid; |
| 970 |
|
| 971 |
case OBJ_SEARCH_KEY: |
| 972 |
cmp = strcmp(left->uniqueid, right_key); |
| 973 |
break; |
| 974 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
cmp = strncmp(left->uniqueid, right_key, strlen(right_key)); |
| 980 |
break; |
| 981 |
default: |
| 982 |
|
| 983 |
ast_assert(0); |
| 984 |
cmp = 0; |
| 985 |
break; |
| 986 |
} |
| 987 |
return cmp ? 0 : CMP_MATCH; |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
static int cdr_all_hash_fn(const void *obj, const int flags) |
| 995 |
{ |
| 996 |
const struct cdr_object *cdr; |
| 997 |
const char *key; |
| 998 |
|
| 999 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1000 |
case OBJ_SEARCH_KEY: |
| 1001 |
key = obj; |
| 1002 |
break; |
| 1003 |
case OBJ_SEARCH_OBJECT: |
| 1004 |
cdr = obj; |
| 1005 |
key = cdr->party_b_name; |
| 1006 |
break; |
| 1007 |
default: |
| 1008 |
ast_assert(0); |
| 1009 |
return 0; |
| 1010 |
} |
| 1011 |
return ast_str_case_hash(key); |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
static int cdr_all_cmp_fn(void *obj, void *arg, int flags) |
| 1019 |
{ |
| 1020 |
struct cdr_object *left = obj; |
| 1021 |
struct cdr_object *right = arg; |
| 1022 |
const char *right_key = arg; |
| 1023 |
int cmp; |
| 1024 |
|
| 1025 |
switch (flags & OBJ_SEARCH_MASK) { |
| 1026 |
case OBJ_SEARCH_OBJECT: |
| 1027 |
right_key = right->party_b_name; |
| 1028 |
|
| 1029 |
case OBJ_SEARCH_KEY: |
| 1030 |
cmp = strcasecmp(left->party_b_name, right_key); |
| 1031 |
break; |
| 1032 |
case OBJ_SEARCH_PARTIAL_KEY: |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
cmp = strncasecmp(left->party_b_name, right_key, strlen(right_key)); |
| 1038 |
break; |
| 1039 |
default: |
| 1040 |
|
| 1041 |
ast_assert(0); |
| 1042 |
cmp = 0; |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
return cmp ? 0 : CMP_MATCH; |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
static void cdr_all_relink(struct cdr_object *cdr) |
| 1054 |
{ |
| 1055 |
ao2_lock(active_cdrs_all); |
| 1056 |
if (cdr->party_b.snapshot) { |
| 1057 |
if (strcasecmp(cdr->party_b_name, cdr->party_b.snapshot->base->name)) { |
| 1058 |
ao2_unlink_flags(active_cdrs_all, cdr, OBJ_NOLOCK); |
| 1059 |
ast_string_field_set(cdr, party_b_name, cdr->party_b.snapshot->base->name); |
| 1060 |
ao2_link_flags(active_cdrs_all, cdr, OBJ_NOLOCK); |
| 1061 |
} |
| 1062 |
} else { |
| 1063 |
ao2_unlink_flags(active_cdrs_all, cdr, OBJ_NOLOCK); |
| 1064 |
ast_string_field_set(cdr, party_b_name, ""); |
| 1065 |
} |
| 1066 |
ao2_unlock(active_cdrs_all); |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
static void cdr_all_unlink(struct cdr_object *cdr) |
| 1075 |
{ |
| 1076 |
struct cdr_object *cur; |
| 1077 |
struct cdr_object *next; |
| 1078 |
|
| 1079 |
ast_assert(cdr->is_root); |
| 1080 |
|
| 1081 |
|
| 1082 |
ao2_ref(cdr, +1); |
| 1083 |
ao2_lock(active_cdrs_all); |
| 1084 |
for (cur = cdr; cur; cur = next) { |
| 1085 |
next = cur->next; |
| 1086 |
ao2_unlink_flags(active_cdrs_all, cur, OBJ_NOLOCK); |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
ast_string_field_set(cur, party_b_name, ""); |
| 1093 |
} |
| 1094 |
ao2_unlock(active_cdrs_all); |
| 1095 |
ao2_ref(cdr, -1); |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
static void cdr_object_dtor(void *obj) |
| 1102 |
{ |
| 1103 |
struct cdr_object *cdr = obj; |
| 1104 |
struct ast_var_t *it_var; |
| 1105 |
|
| 1106 |
ao2_cleanup(cdr->party_a.snapshot); |
| 1107 |
ao2_cleanup(cdr->party_b.snapshot); |
| 1108 |
while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_a.variables, entries))) { |
| 1109 |
ast_var_delete(it_var); |
| 1110 |
} |
| 1111 |
while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_b.variables, entries))) { |
| 1112 |
ast_var_delete(it_var); |
| 1113 |
} |
| 1114 |
ast_string_field_free_memory(cdr); |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
if (cdr->is_root) { |
| 1123 |
struct cdr_object *curr = cdr->next; |
| 1124 |
struct cdr_object *next; |
| 1125 |
|
| 1126 |
while (curr) { |
| 1127 |
next = curr->next; |
| 1128 |
ao2_cleanup(curr); |
| 1129 |
curr = next; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
static struct cdr_object *cdr_object_alloc(struct ast_channel_snapshot *chan, const struct timeval *event_time) |
| 1143 |
{ |
| 1144 |
struct cdr_object *cdr; |
| 1145 |
|
| 1146 |
ast_assert(chan != NULL); |
| 1147 |
|
| 1148 |
cdr = ao2_alloc(sizeof(*cdr), cdr_object_dtor); |
| 1149 |
if (!cdr) { |
| 1150 |
return NULL; |
| 1151 |
} |
| 1152 |
cdr->last = cdr; |
| 1153 |
if (ast_string_field_init(cdr, 64)) { |
| 1154 |
ao2_cleanup(cdr); |
| 1155 |
return NULL; |
| 1156 |
} |
| 1157 |
ast_string_field_set(cdr, uniqueid, chan->base->uniqueid); |
| 1158 |
ast_string_field_set(cdr, name, chan->base->name); |
| 1159 |
ast_string_field_set(cdr, linkedid, chan->peer->linkedid); |
| 1160 |
cdr->disposition = AST_CDR_NULL; |
| 1161 |
cdr->sequence = ast_atomic_fetchadd_int(&global_cdr_sequence, +1); |
| 1162 |
cdr->lastevent = *event_time; |
| 1163 |
|
| 1164 |
cdr->party_a.snapshot = chan; |
| 1165 |
ao2_t_ref(cdr->party_a.snapshot, +1, "bump snapshot during CDR creation"); |
| 1166 |
|
| 1167 |
CDR_DEBUG("%p - Created CDR for channel %s\n", cdr, chan->base->name); |
| 1168 |
|
| 1169 |
cdr_object_transition_state(cdr, &single_state_fn_table); |
| 1170 |
|
| 1171 |
return cdr; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
static struct cdr_object *cdr_object_create_and_append(struct cdr_object *cdr, const struct timeval *event_time) |
| 1180 |
{ |
| 1181 |
struct cdr_object *new_cdr; |
| 1182 |
struct cdr_object *it_cdr; |
| 1183 |
struct cdr_object *cdr_last; |
| 1184 |
|
| 1185 |
cdr_last = cdr->last; |
| 1186 |
new_cdr = cdr_object_alloc(cdr_last->party_a.snapshot, event_time); |
| 1187 |
if (!new_cdr) { |
| 1188 |
return NULL; |
| 1189 |
} |
| 1190 |
new_cdr->disposition = AST_CDR_NULL; |
| 1191 |
|
| 1192 |
|
| 1193 |
ast_string_field_set(new_cdr, linkedid, cdr_last->linkedid); |
| 1194 |
ast_string_field_set(new_cdr, appl, cdr_last->appl); |
| 1195 |
ast_string_field_set(new_cdr, data, cdr_last->data); |
| 1196 |
ast_string_field_set(new_cdr, context, cdr_last->context); |
| 1197 |
ast_string_field_set(new_cdr, exten, cdr_last->exten); |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
if (ast_test_flag(&cdr_last->flags, AST_CDR_FLAG_DISABLE_ALL)) { |
| 1204 |
ast_set_flag(&new_cdr->flags, AST_CDR_FLAG_DISABLE_ALL); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
cdr_object_snapshot_copy(&new_cdr->party_a, &cdr_last->party_a); |
| 1209 |
|
| 1210 |
|
| 1211 |
for (it_cdr = cdr; it_cdr->next; it_cdr = it_cdr->next) { |
| 1212 |
it_cdr->last = new_cdr; |
| 1213 |
} |
| 1214 |
it_cdr->last = new_cdr; |
| 1215 |
it_cdr->next = new_cdr; |
| 1216 |
|
| 1217 |
return new_cdr; |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
static int is_cdr_flag_set(unsigned int cdr_flag) |
| 1230 |
{ |
| 1231 |
struct module_config *mod_cfg; |
| 1232 |
int flag_set; |
| 1233 |
|
| 1234 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 1235 |
flag_set = mod_cfg && ast_test_flag(&mod_cfg->general->settings, cdr_flag); |
| 1236 |
ao2_cleanup(mod_cfg); |
| 1237 |
return flag_set; |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
static int snapshot_cep_changed(struct ast_channel_snapshot *old_snapshot, |
| 1251 |
struct ast_channel_snapshot *new_snapshot) |
| 1252 |
{ |
| 1253 |
|
| 1254 |
if (ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC) |
| 1255 |
&& is_cdr_flag_set(CDR_END_BEFORE_H_EXTEN)) { |
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
if (strcmp(new_snapshot->dialplan->appl, old_snapshot->dialplan->appl) |
| 1264 |
&& strncasecmp(new_snapshot->dialplan->appl, "appdial", 7) |
| 1265 |
&& (strcmp(new_snapshot->dialplan->context, old_snapshot->dialplan->context) |
| 1266 |
|| strcmp(new_snapshot->dialplan->exten, old_snapshot->dialplan->exten) |
| 1267 |
|| new_snapshot->dialplan->priority != old_snapshot->dialplan->priority)) { |
| 1268 |
return 1; |
| 1269 |
} |
| 1270 |
|
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
static int snapshot_is_dialed(struct ast_channel_snapshot *snapshot) |
| 1282 |
{ |
| 1283 |
return (ast_test_flag(&snapshot->flags, AST_FLAG_OUTGOING) |
| 1284 |
&& !(ast_test_flag(&snapshot->flags, AST_FLAG_ORIGINATED))); |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
static struct cdr_object_snapshot *cdr_object_pick_party_a(struct cdr_object_snapshot *left, struct cdr_object_snapshot *right) |
| 1295 |
{ |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
if (!snapshot_is_dialed(left->snapshot) && snapshot_is_dialed(right->snapshot)) { |
| 1300 |
return left; |
| 1301 |
} else if (snapshot_is_dialed(left->snapshot) && !snapshot_is_dialed(right->snapshot)) { |
| 1302 |
return right; |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
if (ast_test_flag(left, AST_CDR_FLAG_PARTY_A) && !ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) { |
| 1307 |
return left; |
| 1308 |
} else if (!ast_test_flag(left, AST_CDR_FLAG_PARTY_A) && ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) { |
| 1309 |
return right; |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
if (left->snapshot->base->creationtime.tv_sec < right->snapshot->base->creationtime.tv_sec) { |
| 1315 |
return left; |
| 1316 |
} else if (left->snapshot->base->creationtime.tv_sec > right->snapshot->base->creationtime.tv_sec) { |
| 1317 |
return right; |
| 1318 |
} else if (left->snapshot->base->creationtime.tv_usec > right->snapshot->base->creationtime.tv_usec) { |
| 1319 |
return right; |
| 1320 |
} else { |
| 1321 |
|
| 1322 |
return left; |
| 1323 |
} |
| 1324 |
} |
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
static long cdr_object_get_duration(struct cdr_object *cdr) |
| 1330 |
{ |
| 1331 |
return (long)(ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->start) / 1000); |
| 1332 |
} |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
static long cdr_object_get_billsec(struct cdr_object *cdr) |
| 1338 |
{ |
| 1339 |
long int ms; |
| 1340 |
|
| 1341 |
if (ast_tvzero(cdr->answer)) { |
| 1342 |
return 0; |
| 1343 |
} |
| 1344 |
|
| 1345 |
ms = ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->answer); |
| 1346 |
if (ms % 1000 >= 500 |
| 1347 |
&& is_cdr_flag_set(CDR_INITIATED_SECONDS)) { |
| 1348 |
ms = (ms / 1000) + 1; |
| 1349 |
} else { |
| 1350 |
ms = ms / 1000; |
| 1351 |
} |
| 1352 |
|
| 1353 |
return ms; |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
static void set_variable(struct varshead *headp, const char *name, const char *value) |
| 1365 |
{ |
| 1366 |
struct ast_var_t *newvariable; |
| 1367 |
|
| 1368 |
AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) { |
| 1369 |
if (!strcasecmp(ast_var_name(newvariable), name)) { |
| 1370 |
AST_LIST_REMOVE_CURRENT(entries); |
| 1371 |
ast_var_delete(newvariable); |
| 1372 |
break; |
| 1373 |
} |
| 1374 |
} |
| 1375 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1376 |
|
| 1377 |
if (value && (newvariable = ast_var_assign(name, value))) { |
| 1378 |
AST_LIST_INSERT_HEAD(headp, newvariable, entries); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr) |
| 1390 |
{ |
| 1391 |
struct ast_cdr *pub_cdr = NULL, *cdr_prev = NULL; |
| 1392 |
struct cdr_object *it_cdr; |
| 1393 |
struct ast_var_t *it_var, *it_copy_var; |
| 1394 |
struct ast_channel_snapshot *party_a; |
| 1395 |
struct ast_channel_snapshot *party_b; |
| 1396 |
|
| 1397 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 1398 |
struct ast_cdr *cdr_copy; |
| 1399 |
|
| 1400 |
|
| 1401 |
if (snapshot_is_dialed(it_cdr->party_a.snapshot) && !it_cdr->party_b.snapshot) { |
| 1402 |
ast_debug(1, "CDR for %s is dialed and has no Party B; discarding\n", |
| 1403 |
it_cdr->party_a.snapshot->base->name); |
| 1404 |
continue; |
| 1405 |
} |
| 1406 |
|
| 1407 |
cdr_copy = ast_calloc(1, sizeof(*cdr_copy)); |
| 1408 |
if (!cdr_copy) { |
| 1409 |
ast_free(pub_cdr); |
| 1410 |
return NULL; |
| 1411 |
} |
| 1412 |
|
| 1413 |
party_a = it_cdr->party_a.snapshot; |
| 1414 |
party_b = it_cdr->party_b.snapshot; |
| 1415 |
|
| 1416 |
|
| 1417 |
ast_assert(party_a != NULL); |
| 1418 |
ast_copy_string(cdr_copy->accountcode, party_a->base->accountcode, sizeof(cdr_copy->accountcode)); |
| 1419 |
cdr_copy->amaflags = party_a->amaflags; |
| 1420 |
ast_copy_string(cdr_copy->channel, party_a->base->name, sizeof(cdr_copy->channel)); |
| 1421 |
ast_callerid_merge(cdr_copy->clid, sizeof(cdr_copy->clid), party_a->caller->name, party_a->caller->number, ""); |
| 1422 |
ast_copy_string(cdr_copy->src, party_a->caller->number, sizeof(cdr_copy->src)); |
| 1423 |
ast_copy_string(cdr_copy->uniqueid, party_a->base->uniqueid, sizeof(cdr_copy->uniqueid)); |
| 1424 |
ast_copy_string(cdr_copy->lastapp, it_cdr->appl, sizeof(cdr_copy->lastapp)); |
| 1425 |
ast_copy_string(cdr_copy->lastdata, it_cdr->data, sizeof(cdr_copy->lastdata)); |
| 1426 |
ast_copy_string(cdr_copy->dst, it_cdr->exten, sizeof(cdr_copy->dst)); |
| 1427 |
ast_copy_string(cdr_copy->dcontext, it_cdr->context, sizeof(cdr_copy->dcontext)); |
| 1428 |
ast_copy_string(cdr_copy->tenantid, party_a->base->tenantid, sizeof(cdr_copy->tenantid)); |
| 1429 |
|
| 1430 |
|
| 1431 |
if (party_b) { |
| 1432 |
ast_copy_string(cdr_copy->dstchannel, party_b->base->name, sizeof(cdr_copy->dstchannel)); |
| 1433 |
ast_copy_string(cdr_copy->peeraccount, party_b->base->accountcode, sizeof(cdr_copy->peeraccount)); |
| 1434 |
if (!ast_strlen_zero(it_cdr->party_b.userfield)) { |
| 1435 |
snprintf(cdr_copy->userfield, sizeof(cdr_copy->userfield), "%s;%s", it_cdr->party_a.userfield, it_cdr->party_b.userfield); |
| 1436 |
} |
| 1437 |
ast_copy_string(cdr_copy->peertenantid, party_b->base->tenantid, sizeof(cdr_copy->peertenantid)); |
| 1438 |
} |
| 1439 |
if (ast_strlen_zero(cdr_copy->userfield) && !ast_strlen_zero(it_cdr->party_a.userfield)) { |
| 1440 |
ast_copy_string(cdr_copy->userfield, it_cdr->party_a.userfield, sizeof(cdr_copy->userfield)); |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
cdr_copy->start = it_cdr->start; |
| 1445 |
cdr_copy->answer = it_cdr->answer; |
| 1446 |
cdr_copy->end = it_cdr->end; |
| 1447 |
cdr_copy->billsec = cdr_object_get_billsec(it_cdr); |
| 1448 |
cdr_copy->duration = cdr_object_get_duration(it_cdr); |
| 1449 |
|
| 1450 |
|
| 1451 |
ast_copy_flags(cdr_copy, &it_cdr->flags, AST_FLAGS_ALL); |
| 1452 |
ast_copy_string(cdr_copy->linkedid, it_cdr->linkedid, sizeof(cdr_copy->linkedid)); |
| 1453 |
cdr_copy->disposition = it_cdr->disposition; |
| 1454 |
cdr_copy->sequence = it_cdr->sequence; |
| 1455 |
|
| 1456 |
|
| 1457 |
copy_variables(&cdr_copy->varshead, &it_cdr->party_a.variables); |
| 1458 |
AST_LIST_TRAVERSE(&it_cdr->party_b.variables, it_var, entries) { |
| 1459 |
int found = 0; |
| 1460 |
struct ast_var_t *newvariable; |
| 1461 |
AST_LIST_TRAVERSE(&cdr_copy->varshead, it_copy_var, entries) { |
| 1462 |
if (!strcasecmp(ast_var_name(it_var), ast_var_name(it_copy_var))) { |
| 1463 |
found = 1; |
| 1464 |
break; |
| 1465 |
} |
| 1466 |
} |
| 1467 |
if (!found && (newvariable = ast_var_assign(ast_var_name(it_var), ast_var_value(it_var)))) { |
| 1468 |
AST_LIST_INSERT_TAIL(&cdr_copy->varshead, newvariable, entries); |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
if (!pub_cdr) { |
| 1473 |
pub_cdr = cdr_copy; |
| 1474 |
cdr_prev = pub_cdr; |
| 1475 |
} else { |
| 1476 |
cdr_prev->next = cdr_copy; |
| 1477 |
cdr_prev = cdr_copy; |
| 1478 |
} |
| 1479 |
} |
| 1480 |
|
| 1481 |
return pub_cdr; |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
static void cdr_object_dispatch(struct cdr_object *cdr) |
| 1491 |
{ |
| 1492 |
struct ast_cdr *pub_cdr; |
| 1493 |
|
| 1494 |
CDR_DEBUG("%p - Dispatching CDR for Party A %s, Party B %s\n", cdr, |
| 1495 |
cdr->party_a.snapshot->base->name, |
| 1496 |
cdr->party_b.snapshot ? cdr->party_b.snapshot->base->name : "<none>"); |
| 1497 |
pub_cdr = cdr_object_create_public_records(cdr); |
| 1498 |
cdr_detach(pub_cdr); |
| 1499 |
} |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
static void cdr_object_set_disposition(struct cdr_object *cdr, int hangupcause) |
| 1507 |
{ |
| 1508 |
|
| 1509 |
switch (hangupcause) { |
| 1510 |
case AST_CAUSE_BUSY: |
| 1511 |
cdr->disposition = AST_CDR_BUSY; |
| 1512 |
break; |
| 1513 |
case AST_CAUSE_CONGESTION: |
| 1514 |
if (!is_cdr_flag_set(CDR_CONGESTION)) { |
| 1515 |
cdr->disposition = AST_CDR_FAILED; |
| 1516 |
} else { |
| 1517 |
cdr->disposition = AST_CDR_CONGESTION; |
| 1518 |
} |
| 1519 |
break; |
| 1520 |
case AST_CAUSE_NO_ROUTE_DESTINATION: |
| 1521 |
case AST_CAUSE_UNREGISTERED: |
| 1522 |
cdr->disposition = AST_CDR_FAILED; |
| 1523 |
break; |
| 1524 |
case AST_CAUSE_NORMAL_CLEARING: |
| 1525 |
case AST_CAUSE_NO_ANSWER: |
| 1526 |
cdr->disposition = AST_CDR_NOANSWER; |
| 1527 |
break; |
| 1528 |
default: |
| 1529 |
break; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
static void cdr_object_finalize(struct cdr_object *cdr) |
| 1545 |
{ |
| 1546 |
if (!ast_tvzero(cdr->end)) { |
| 1547 |
return; |
| 1548 |
} |
| 1549 |
cdr->end = cdr->lastevent; |
| 1550 |
|
| 1551 |
if (cdr->disposition == AST_CDR_NULL) { |
| 1552 |
if (!ast_tvzero(cdr->answer)) { |
| 1553 |
cdr->disposition = AST_CDR_ANSWERED; |
| 1554 |
} else if (cdr->party_a.snapshot->hangup->cause) { |
| 1555 |
cdr_object_set_disposition(cdr, cdr->party_a.snapshot->hangup->cause); |
| 1556 |
} else if (cdr->party_b.snapshot && cdr->party_b.snapshot->hangup->cause) { |
| 1557 |
cdr_object_set_disposition(cdr, cdr->party_b.snapshot->hangup->cause); |
| 1558 |
} else { |
| 1559 |
cdr->disposition = AST_CDR_FAILED; |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
ast_debug(1, "Finalized CDR for %s - start %ld.%06ld answer %ld.%06ld end %ld.%06ld dur %.3f bill %.3f dispo %s\n", |
| 1565 |
cdr->party_a.snapshot->base->name, |
| 1566 |
(long)cdr->start.tv_sec, |
| 1567 |
(long)cdr->start.tv_usec, |
| 1568 |
(long)cdr->answer.tv_sec, |
| 1569 |
(long)cdr->answer.tv_usec, |
| 1570 |
(long)cdr->end.tv_sec, |
| 1571 |
(long)cdr->end.tv_usec, |
| 1572 |
(double)ast_tvdiff_ms(cdr->end, cdr->start) / 1000.0, |
| 1573 |
(double)ast_tvdiff_ms(cdr->end, cdr->answer) / 1000.0, |
| 1574 |
ast_cdr_disp2str(cdr->disposition)); |
| 1575 |
} |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
static void cdr_object_check_party_a_hangup(struct cdr_object *cdr) |
| 1582 |
{ |
| 1583 |
if (ast_test_flag(&cdr->party_a.snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC) |
| 1584 |
&& is_cdr_flag_set(CDR_END_BEFORE_H_EXTEN)) { |
| 1585 |
cdr_object_finalize(cdr); |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (ast_test_flag(&cdr->party_a.snapshot->flags, AST_FLAG_DEAD) |
| 1589 |
&& cdr->fn_table != &finalized_state_fn_table) { |
| 1590 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 1591 |
} |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
static void cdr_object_check_party_a_answer(struct cdr_object *cdr) |
| 1599 |
{ |
| 1600 |
if (cdr->party_a.snapshot->state == AST_STATE_UP && ast_tvzero(cdr->answer)) { |
| 1601 |
cdr->answer = cdr->lastevent; |
| 1602 |
|
| 1603 |
CDR_DEBUG("%p - Set answered time to %ld.%06ld\n", cdr, |
| 1604 |
(long)cdr->answer.tv_sec, |
| 1605 |
(long)cdr->answer.tv_usec); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
static void cdr_object_update_cid(struct cdr_object_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot) |
| 1611 |
{ |
| 1612 |
if (!old_snapshot->snapshot) { |
| 1613 |
set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller->dnid); |
| 1614 |
set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller->subaddr); |
| 1615 |
set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->caller->dialed_subaddr); |
| 1616 |
return; |
| 1617 |
} |
| 1618 |
if (strcmp(old_snapshot->snapshot->caller->dnid, new_snapshot->caller->dnid)) { |
| 1619 |
set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller->dnid); |
| 1620 |
} |
| 1621 |
if (strcmp(old_snapshot->snapshot->caller->subaddr, new_snapshot->caller->subaddr)) { |
| 1622 |
set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller->subaddr); |
| 1623 |
} |
| 1624 |
if (strcmp(old_snapshot->snapshot->caller->dialed_subaddr, new_snapshot->caller->dialed_subaddr)) { |
| 1625 |
set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->caller->dialed_subaddr); |
| 1626 |
} |
| 1627 |
} |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
static void cdr_object_swap_snapshot(struct cdr_object_snapshot *old_snapshot, |
| 1636 |
struct ast_channel_snapshot *new_snapshot) |
| 1637 |
{ |
| 1638 |
cdr_object_update_cid(old_snapshot, new_snapshot); |
| 1639 |
ao2_t_replace(old_snapshot->snapshot, new_snapshot, "Swap CDR snapshot"); |
| 1640 |
} |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot) |
| 1645 |
{ |
| 1646 |
ast_assert(strcasecmp(snapshot->base->name, cdr->party_a.snapshot->base->name) == 0); |
| 1647 |
|
| 1648 |
|
| 1649 |
if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC) |
| 1650 |
&& is_cdr_flag_set(CDR_END_BEFORE_H_EXTEN)) { |
| 1651 |
cdr_object_finalize(cdr); |
| 1652 |
return 0; |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
if (!ast_test_flag(&snapshot->flags, AST_FLAG_SUBROUTINE_EXEC) |
| 1660 |
|| ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) { |
| 1661 |
if (strcmp(cdr->context, snapshot->dialplan->context)) { |
| 1662 |
ast_string_field_set(cdr, context, snapshot->dialplan->context); |
| 1663 |
} |
| 1664 |
if (strcmp(cdr->exten, snapshot->dialplan->exten)) { |
| 1665 |
ast_string_field_set(cdr, exten, snapshot->dialplan->exten); |
| 1666 |
} |
| 1667 |
} |
| 1668 |
|
| 1669 |
cdr_object_swap_snapshot(&cdr->party_a, snapshot); |
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
if (!ast_test_flag(&cdr->flags, AST_CDR_LOCK_APP) |
| 1677 |
&& !ast_strlen_zero(snapshot->dialplan->appl) |
| 1678 |
&& (strncasecmp(snapshot->dialplan->appl, "appdial", 7) || ast_strlen_zero(cdr->appl))) { |
| 1679 |
if (strcmp(cdr->appl, snapshot->dialplan->appl)) { |
| 1680 |
ast_string_field_set(cdr, appl, snapshot->dialplan->appl); |
| 1681 |
} |
| 1682 |
if (strcmp(cdr->data, snapshot->dialplan->data)) { |
| 1683 |
ast_string_field_set(cdr, data, snapshot->dialplan->data); |
| 1684 |
} |
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
if (!strcmp(snapshot->dialplan->appl, "Dial")) { |
| 1692 |
ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
if (strcmp(cdr->linkedid, snapshot->peer->linkedid)) { |
| 1697 |
ast_string_field_set(cdr, linkedid, snapshot->peer->linkedid); |
| 1698 |
} |
| 1699 |
cdr_object_check_party_a_answer(cdr); |
| 1700 |
cdr_object_check_party_a_hangup(cdr); |
| 1701 |
|
| 1702 |
return 0; |
| 1703 |
} |
| 1704 |
|
| 1705 |
static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 1706 |
{ |
| 1707 |
return 0; |
| 1708 |
} |
| 1709 |
|
| 1710 |
static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status) |
| 1711 |
{ |
| 1712 |
return 0; |
| 1713 |
} |
| 1714 |
|
| 1715 |
static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 1716 |
{ |
| 1717 |
|
| 1718 |
return BRIDGE_ENTER_NEED_CDR; |
| 1719 |
} |
| 1720 |
|
| 1721 |
static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info) |
| 1722 |
{ |
| 1723 |
char park_info[128]; |
| 1724 |
|
| 1725 |
ast_assert(!strcasecmp(parking_info->parkee->base->name, cdr->party_a.snapshot->base->name)); |
| 1726 |
|
| 1727 |
|
| 1728 |
cdr->fn_table->process_party_a(cdr, parking_info->parkee); |
| 1729 |
|
| 1730 |
|
| 1731 |
ast_string_field_set(cdr, appl, "Park"); |
| 1732 |
snprintf(park_info, sizeof(park_info), "%s:%u", parking_info->parkinglot, parking_info->parkingspace); |
| 1733 |
ast_string_field_set(cdr, data, park_info); |
| 1734 |
|
| 1735 |
|
| 1736 |
ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP); |
| 1737 |
|
| 1738 |
return 0; |
| 1739 |
} |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
static void single_state_init_function(struct cdr_object *cdr) |
| 1744 |
{ |
| 1745 |
cdr->start = cdr->lastevent; |
| 1746 |
cdr_object_check_party_a_answer(cdr); |
| 1747 |
} |
| 1748 |
|
| 1749 |
static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot) |
| 1750 |
{ |
| 1751 |
|
| 1752 |
ast_assert(cdr->party_b.snapshot == NULL); |
| 1753 |
ast_assert(0); |
| 1754 |
return; |
| 1755 |
} |
| 1756 |
|
| 1757 |
static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer) |
| 1758 |
{ |
| 1759 |
if (caller && !strcasecmp(cdr->party_a.snapshot->base->name, caller->base->name)) { |
| 1760 |
base_process_party_a(cdr, caller); |
| 1761 |
CDR_DEBUG("%p - Updated Party A %s snapshot\n", cdr, |
| 1762 |
cdr->party_a.snapshot->base->name); |
| 1763 |
cdr_object_swap_snapshot(&cdr->party_b, peer); |
| 1764 |
cdr_all_relink(cdr); |
| 1765 |
CDR_DEBUG("%p - Updated Party B %s snapshot\n", cdr, |
| 1766 |
cdr->party_b.snapshot->base->name); |
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP); |
| 1773 |
} else if (!strcasecmp(cdr->party_a.snapshot->base->name, peer->base->name)) { |
| 1774 |
|
| 1775 |
base_process_party_a(cdr, peer); |
| 1776 |
CDR_DEBUG("%p - Updated Party A %s snapshot\n", cdr, |
| 1777 |
cdr->party_a.snapshot->base->name); |
| 1778 |
} |
| 1779 |
|
| 1780 |
cdr_object_transition_state(cdr, &dial_state_fn_table); |
| 1781 |
return 0; |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
static int single_state_bridge_enter_comparison(struct cdr_object *cdr, |
| 1797 |
struct cdr_object *cand_cdr) |
| 1798 |
{ |
| 1799 |
struct cdr_object_snapshot *party_a; |
| 1800 |
|
| 1801 |
|
| 1802 |
if (!strcasecmp(cdr->party_a.snapshot->base->name, cand_cdr->party_a.snapshot->base->name)) { |
| 1803 |
return 1; |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a); |
| 1808 |
if (!strcasecmp(party_a->snapshot->base->name, cdr->party_a.snapshot->base->name)) { |
| 1809 |
CDR_DEBUG("%p - Party A %s has new Party B %s\n", |
| 1810 |
cdr, cdr->party_a.snapshot->base->name, cand_cdr->party_a.snapshot->base->name); |
| 1811 |
cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a); |
| 1812 |
cdr_all_relink(cdr); |
| 1813 |
if (!cand_cdr->party_b.snapshot) { |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
cdr_object_finalize(cand_cdr); |
| 1819 |
} |
| 1820 |
return 0; |
| 1821 |
} |
| 1822 |
|
| 1823 |
|
| 1824 |
if (!cand_cdr->party_b.snapshot |
| 1825 |
|| !strcasecmp(cdr->party_a.snapshot->base->name, cand_cdr->party_b.snapshot->base->name)) { |
| 1826 |
return 1; |
| 1827 |
} |
| 1828 |
party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_b); |
| 1829 |
if (!strcasecmp(party_a->snapshot->base->name, cdr->party_a.snapshot->base->name)) { |
| 1830 |
CDR_DEBUG("%p - Party A %s has new Party B %s\n", |
| 1831 |
cdr, cdr->party_a.snapshot->base->name, cand_cdr->party_b.snapshot->base->name); |
| 1832 |
cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_b); |
| 1833 |
cdr_all_relink(cdr); |
| 1834 |
return 0; |
| 1835 |
} |
| 1836 |
|
| 1837 |
return 1; |
| 1838 |
} |
| 1839 |
|
| 1840 |
static enum process_bridge_enter_results single_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 1841 |
{ |
| 1842 |
struct ao2_iterator it_cdrs; |
| 1843 |
char *channel_id; |
| 1844 |
int success = 0; |
| 1845 |
|
| 1846 |
ast_string_field_set(cdr, bridge, bridge->uniqueid); |
| 1847 |
|
| 1848 |
if (ao2_container_count(bridge->channels) == 1) { |
| 1849 |
|
| 1850 |
cdr_object_transition_state(cdr, &bridge_state_fn_table); |
| 1851 |
return BRIDGE_ENTER_ONLY_PARTY; |
| 1852 |
} |
| 1853 |
|
| 1854 |
for (it_cdrs = ao2_iterator_init(bridge->channels, 0); |
| 1855 |
!success && (channel_id = ao2_iterator_next(&it_cdrs)); |
| 1856 |
ao2_ref(channel_id, -1)) { |
| 1857 |
struct cdr_object *cand_cdr_master; |
| 1858 |
struct cdr_object *cand_cdr; |
| 1859 |
|
| 1860 |
cand_cdr_master = ao2_find(active_cdrs_master, channel_id, OBJ_SEARCH_KEY); |
| 1861 |
if (!cand_cdr_master) { |
| 1862 |
continue; |
| 1863 |
} |
| 1864 |
|
| 1865 |
ao2_lock(cand_cdr_master); |
| 1866 |
for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) { |
| 1867 |
|
| 1868 |
|
| 1869 |
if (cand_cdr->fn_table != &bridge_state_fn_table || |
| 1870 |
strcmp(cdr->bridge, cand_cdr->bridge)) { |
| 1871 |
continue; |
| 1872 |
} |
| 1873 |
|
| 1874 |
if (single_state_bridge_enter_comparison(cdr, cand_cdr)) { |
| 1875 |
continue; |
| 1876 |
} |
| 1877 |
|
| 1878 |
success = 1; |
| 1879 |
break; |
| 1880 |
} |
| 1881 |
ao2_unlock(cand_cdr_master); |
| 1882 |
ao2_cleanup(cand_cdr_master); |
| 1883 |
} |
| 1884 |
ao2_iterator_destroy(&it_cdrs); |
| 1885 |
|
| 1886 |
|
| 1887 |
cdr_object_transition_state(cdr, &bridge_state_fn_table); |
| 1888 |
|
| 1889 |
|
| 1890 |
if (success) { |
| 1891 |
return BRIDGE_ENTER_OBTAINED_PARTY_B; |
| 1892 |
} |
| 1893 |
|
| 1894 |
return BRIDGE_ENTER_NO_PARTY_B; |
| 1895 |
} |
| 1896 |
|
| 1897 |
static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 1898 |
{ |
| 1899 |
cdr_object_transition_state(cdr, &parked_state_fn_table); |
| 1900 |
return 0; |
| 1901 |
} |
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot) |
| 1907 |
{ |
| 1908 |
ast_assert(snapshot != NULL); |
| 1909 |
ast_assert(cdr->party_b.snapshot |
| 1910 |
&& !strcasecmp(cdr->party_b.snapshot->base->name, snapshot->base->name)); |
| 1911 |
|
| 1912 |
cdr_object_swap_snapshot(&cdr->party_b, snapshot); |
| 1913 |
|
| 1914 |
|
| 1915 |
if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) { |
| 1916 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer) |
| 1921 |
{ |
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
return 1; |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
static enum ast_cdr_disposition dial_status_to_disposition(const char *dial_status) |
| 1933 |
{ |
| 1934 |
if (!strcmp(dial_status, "ANSWER")) { |
| 1935 |
return AST_CDR_ANSWERED; |
| 1936 |
} else if (!strcmp(dial_status, "BUSY")) { |
| 1937 |
return AST_CDR_BUSY; |
| 1938 |
} else if (!strcmp(dial_status, "CANCEL")) { |
| 1939 |
if (!is_cdr_flag_set(CDR_CANCEL_DISPOSITION_ENABLED)) { |
| 1940 |
return AST_CDR_NOANSWER; |
| 1941 |
} else { |
| 1942 |
return AST_CDR_CANCEL; |
| 1943 |
} |
| 1944 |
} else if (!strcmp(dial_status, "NOANSWER")) { |
| 1945 |
return AST_CDR_NOANSWER; |
| 1946 |
} else if (!strcmp(dial_status, "CONGESTION")) { |
| 1947 |
if (!is_cdr_flag_set(CDR_CONGESTION)) { |
| 1948 |
return AST_CDR_FAILED; |
| 1949 |
} else { |
| 1950 |
return AST_CDR_CONGESTION; |
| 1951 |
} |
| 1952 |
} else if (!strcmp(dial_status, "FAILED")) { |
| 1953 |
return AST_CDR_FAILED; |
| 1954 |
} |
| 1955 |
return AST_CDR_FAILED; |
| 1956 |
} |
| 1957 |
|
| 1958 |
static int dial_state_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status) |
| 1959 |
{ |
| 1960 |
struct ast_channel_snapshot *party_a; |
| 1961 |
|
| 1962 |
if (caller) { |
| 1963 |
party_a = caller; |
| 1964 |
} else { |
| 1965 |
party_a = peer; |
| 1966 |
} |
| 1967 |
ast_assert(!strcasecmp(cdr->party_a.snapshot->base->name, party_a->base->name)); |
| 1968 |
cdr_object_swap_snapshot(&cdr->party_a, party_a); |
| 1969 |
|
| 1970 |
if (cdr->party_b.snapshot) { |
| 1971 |
if (strcasecmp(cdr->party_b.snapshot->base->name, peer->base->name)) { |
| 1972 |
|
| 1973 |
return 1; |
| 1974 |
} |
| 1975 |
cdr_object_swap_snapshot(&cdr->party_b, peer); |
| 1976 |
} |
| 1977 |
|
| 1978 |
|
| 1979 |
cdr->disposition = dial_status_to_disposition(dial_status); |
| 1980 |
if (cdr->disposition == AST_CDR_ANSWERED) { |
| 1981 |
|
| 1982 |
cdr_object_transition_state(cdr, &dialed_pending_state_fn_table); |
| 1983 |
} else { |
| 1984 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 1985 |
} |
| 1986 |
|
| 1987 |
return 0; |
| 1988 |
} |
| 1989 |
|
| 1990 |
static enum process_bridge_enter_results dial_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 1991 |
{ |
| 1992 |
int success = 0; |
| 1993 |
|
| 1994 |
ast_string_field_set(cdr, bridge, bridge->uniqueid); |
| 1995 |
|
| 1996 |
|
| 1997 |
if (ao2_container_count(bridge->channels) == 1) { |
| 1998 |
|
| 1999 |
cdr_object_transition_state(cdr, &bridge_state_fn_table); |
| 2000 |
return BRIDGE_ENTER_ONLY_PARTY; |
| 2001 |
} |
| 2002 |
|
| 2003 |
|
| 2004 |
if (cdr->party_b.snapshot) { |
| 2005 |
struct ao2_iterator it_cdrs; |
| 2006 |
char *channel_id; |
| 2007 |
|
| 2008 |
for (it_cdrs = ao2_iterator_init(bridge->channels, 0); |
| 2009 |
!success && (channel_id = ao2_iterator_next(&it_cdrs)); |
| 2010 |
ao2_ref(channel_id, -1)) { |
| 2011 |
struct cdr_object *cand_cdr_master; |
| 2012 |
struct cdr_object *cand_cdr; |
| 2013 |
|
| 2014 |
cand_cdr_master = ao2_find(active_cdrs_master, channel_id, OBJ_SEARCH_KEY); |
| 2015 |
if (!cand_cdr_master) { |
| 2016 |
continue; |
| 2017 |
} |
| 2018 |
|
| 2019 |
ao2_lock(cand_cdr_master); |
| 2020 |
for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) { |
| 2021 |
|
| 2022 |
|
| 2023 |
if (cand_cdr->fn_table != &bridge_state_fn_table |
| 2024 |
|| strcmp(cdr->bridge, cand_cdr->bridge)) { |
| 2025 |
continue; |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
if (strcasecmp(cdr->party_b.snapshot->base->name, cand_cdr->party_a.snapshot->base->name)) { |
| 2030 |
continue; |
| 2031 |
} |
| 2032 |
cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a); |
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
if (!cand_cdr->party_b.snapshot) { |
| 2038 |
cdr_object_finalize(cand_cdr); |
| 2039 |
} |
| 2040 |
success = 1; |
| 2041 |
break; |
| 2042 |
} |
| 2043 |
ao2_unlock(cand_cdr_master); |
| 2044 |
ao2_cleanup(cand_cdr_master); |
| 2045 |
} |
| 2046 |
ao2_iterator_destroy(&it_cdrs); |
| 2047 |
} |
| 2048 |
|
| 2049 |
|
| 2050 |
cdr_object_transition_state(cdr, &bridge_state_fn_table); |
| 2051 |
|
| 2052 |
|
| 2053 |
if (success) { |
| 2054 |
return BRIDGE_ENTER_OBTAINED_PARTY_B; |
| 2055 |
} |
| 2056 |
return BRIDGE_ENTER_NO_PARTY_B; |
| 2057 |
} |
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot) |
| 2062 |
{ |
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
if (snapshot_cep_changed(cdr->party_a.snapshot, snapshot)) { |
| 2067 |
if (cdr->party_b.snapshot) { |
| 2068 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 2069 |
cdr->fn_table->process_party_a(cdr, snapshot); |
| 2070 |
return 1; |
| 2071 |
} else { |
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
cdr_object_transition_state_init(cdr, &single_state_fn_table, 0); |
| 2078 |
cdr->fn_table->process_party_a(cdr, snapshot); |
| 2079 |
return 0; |
| 2080 |
} |
| 2081 |
} |
| 2082 |
base_process_party_a(cdr, snapshot); |
| 2083 |
return 0; |
| 2084 |
} |
| 2085 |
|
| 2086 |
static enum process_bridge_enter_results dialed_pending_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 2087 |
{ |
| 2088 |
cdr_object_transition_state(cdr, &dial_state_fn_table); |
| 2089 |
return cdr->fn_table->process_bridge_enter(cdr, bridge, channel); |
| 2090 |
} |
| 2091 |
|
| 2092 |
static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 2093 |
{ |
| 2094 |
if (cdr->party_b.snapshot) { |
| 2095 |
|
| 2096 |
return 1; |
| 2097 |
} |
| 2098 |
cdr_object_transition_state(cdr, &parked_state_fn_table); |
| 2099 |
return 0; |
| 2100 |
} |
| 2101 |
|
| 2102 |
static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer) |
| 2103 |
{ |
| 2104 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 2105 |
|
| 2106 |
|
| 2107 |
return 1; |
| 2108 |
} |
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot) |
| 2113 |
{ |
| 2114 |
ast_assert(cdr->party_b.snapshot |
| 2115 |
&& !strcasecmp(cdr->party_b.snapshot->base->name, snapshot->base->name)); |
| 2116 |
|
| 2117 |
cdr_object_swap_snapshot(&cdr->party_b, snapshot); |
| 2118 |
|
| 2119 |
|
| 2120 |
if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) { |
| 2121 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 2122 |
} |
| 2123 |
} |
| 2124 |
|
| 2125 |
static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 2126 |
{ |
| 2127 |
if (strcmp(cdr->bridge, bridge->uniqueid)) { |
| 2128 |
return 1; |
| 2129 |
} |
| 2130 |
if (strcasecmp(cdr->party_a.snapshot->base->name, channel->base->name) |
| 2131 |
&& cdr->party_b.snapshot |
| 2132 |
&& strcasecmp(cdr->party_b.snapshot->base->name, channel->base->name)) { |
| 2133 |
return 1; |
| 2134 |
} |
| 2135 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 2136 |
|
| 2137 |
return 0; |
| 2138 |
} |
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel) |
| 2143 |
{ |
| 2144 |
if (strcasecmp(cdr->party_a.snapshot->base->name, channel->base->name)) { |
| 2145 |
return 1; |
| 2146 |
} |
| 2147 |
cdr_object_transition_state(cdr, &finalized_state_fn_table); |
| 2148 |
|
| 2149 |
return 0; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
static void finalized_state_init_function(struct cdr_object *cdr) |
| 2155 |
{ |
| 2156 |
cdr_object_finalize(cdr); |
| 2157 |
} |
| 2158 |
|
| 2159 |
static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot) |
| 2160 |
{ |
| 2161 |
if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC) |
| 2162 |
&& is_cdr_flag_set(CDR_END_BEFORE_H_EXTEN)) { |
| 2163 |
return 0; |
| 2164 |
} |
| 2165 |
|
| 2166 |
|
| 2167 |
return 1; |
| 2168 |
} |
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
static int filter_channel_snapshot(struct ast_channel_snapshot *snapshot) |
| 2175 |
{ |
| 2176 |
return snapshot->base->tech_properties & AST_CHAN_TP_INTERNAL; |
| 2177 |
} |
| 2178 |
|
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
static int filter_channel_snapshot_message(struct ast_channel_snapshot *old_snapshot, |
| 2184 |
struct ast_channel_snapshot *new_snapshot) |
| 2185 |
{ |
| 2186 |
int ret = 0; |
| 2187 |
|
| 2188 |
|
| 2189 |
if (old_snapshot) { |
| 2190 |
ret |= filter_channel_snapshot(old_snapshot); |
| 2191 |
} |
| 2192 |
if (new_snapshot) { |
| 2193 |
ret |= filter_channel_snapshot(new_snapshot); |
| 2194 |
} |
| 2195 |
|
| 2196 |
return ret; |
| 2197 |
} |
| 2198 |
|
| 2199 |
static int dial_status_end(const char *dialstatus) |
| 2200 |
{ |
| 2201 |
return (strcmp(dialstatus, "RINGING") && |
| 2202 |
strcmp(dialstatus, "PROCEEDING") && |
| 2203 |
strcmp(dialstatus, "PROGRESS")); |
| 2204 |
} |
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
static void handle_dial_message(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 2215 |
{ |
| 2216 |
struct cdr_object *cdr; |
| 2217 |
struct ast_multi_channel_blob *payload = stasis_message_data(message); |
| 2218 |
struct ast_channel_snapshot *caller; |
| 2219 |
struct ast_channel_snapshot *peer; |
| 2220 |
struct cdr_object *it_cdr; |
| 2221 |
struct ast_json *dial_status_blob; |
| 2222 |
const char *dial_status = NULL; |
| 2223 |
int res = 1; |
| 2224 |
|
| 2225 |
caller = ast_multi_channel_blob_get_channel(payload, "caller"); |
| 2226 |
peer = ast_multi_channel_blob_get_channel(payload, "peer"); |
| 2227 |
if (!peer && !caller) { |
| 2228 |
return; |
| 2229 |
} |
| 2230 |
|
| 2231 |
if (peer && filter_channel_snapshot(peer)) { |
| 2232 |
return; |
| 2233 |
} |
| 2234 |
|
| 2235 |
if (caller && filter_channel_snapshot(caller)) { |
| 2236 |
return; |
| 2237 |
} |
| 2238 |
|
| 2239 |
dial_status_blob = ast_json_object_get(ast_multi_channel_blob_get_json(payload), "dialstatus"); |
| 2240 |
if (dial_status_blob) { |
| 2241 |
dial_status = ast_json_string_get(dial_status_blob); |
| 2242 |
} |
| 2243 |
|
| 2244 |
CDR_DEBUG("Dial %s message for %s, %s: %u.%08u\n", |
| 2245 |
ast_strlen_zero(dial_status) ? "Begin" : "End", |
| 2246 |
caller ? caller->base->name : "(none)", |
| 2247 |
peer ? peer->base->name : "(none)", |
| 2248 |
(unsigned int)stasis_message_timestamp(message)->tv_sec, |
| 2249 |
(unsigned int)stasis_message_timestamp(message)->tv_usec); |
| 2250 |
|
| 2251 |
|
| 2252 |
if (caller) { |
| 2253 |
cdr = ao2_find(active_cdrs_master, caller->base->uniqueid, OBJ_SEARCH_KEY); |
| 2254 |
} else { |
| 2255 |
cdr = ao2_find(active_cdrs_master, peer->base->uniqueid, OBJ_SEARCH_KEY); |
| 2256 |
} |
| 2257 |
if (!cdr) { |
| 2258 |
ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", caller ? caller->base->name : peer->base->name); |
| 2259 |
ast_assert(0); |
| 2260 |
return; |
| 2261 |
} |
| 2262 |
|
| 2263 |
ao2_lock(cdr); |
| 2264 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2265 |
it_cdr->lastevent = *stasis_message_timestamp(message); |
| 2266 |
if (ast_strlen_zero(dial_status)) { |
| 2267 |
if (!it_cdr->fn_table->process_dial_begin) { |
| 2268 |
continue; |
| 2269 |
} |
| 2270 |
if (dial_changes_ignored) { |
| 2271 |
CDR_DEBUG("%p - Ignoring Dial Begin message\n", it_cdr); |
| 2272 |
continue; |
| 2273 |
} |
| 2274 |
CDR_DEBUG("%p - Processing Dial Begin message for channel %s, peer %s\n", |
| 2275 |
it_cdr, |
| 2276 |
caller ? caller->base->name : "(none)", |
| 2277 |
peer ? peer->base->name : "(none)"); |
| 2278 |
res &= it_cdr->fn_table->process_dial_begin(it_cdr, |
| 2279 |
caller, |
| 2280 |
peer); |
| 2281 |
} else if (dial_status_end(dial_status)) { |
| 2282 |
if (!it_cdr->fn_table->process_dial_end) { |
| 2283 |
continue; |
| 2284 |
} |
| 2285 |
if (dial_changes_ignored) { |
| 2286 |
|
| 2287 |
it_cdr->disposition = dial_status_to_disposition(dial_status); |
| 2288 |
CDR_DEBUG("%p - Setting disposition and that's it (%s)\n", it_cdr, dial_status); |
| 2289 |
continue; |
| 2290 |
} |
| 2291 |
CDR_DEBUG("%p - Processing Dial End message for channel %s, peer %s\n", |
| 2292 |
it_cdr, |
| 2293 |
caller ? caller->base->name : "(none)", |
| 2294 |
peer ? peer->base->name : "(none)"); |
| 2295 |
it_cdr->fn_table->process_dial_end(it_cdr, |
| 2296 |
caller, |
| 2297 |
peer, |
| 2298 |
dial_status); |
| 2299 |
} |
| 2300 |
} |
| 2301 |
|
| 2302 |
|
| 2303 |
if (!dial_changes_ignored) { |
| 2304 |
|
| 2305 |
if (res && ast_strlen_zero(dial_status)) { |
| 2306 |
struct cdr_object *new_cdr; |
| 2307 |
|
| 2308 |
new_cdr = cdr_object_create_and_append(cdr, stasis_message_timestamp(message)); |
| 2309 |
if (new_cdr) { |
| 2310 |
new_cdr->fn_table->process_dial_begin(new_cdr, caller, peer); |
| 2311 |
} |
| 2312 |
} |
| 2313 |
} |
| 2314 |
|
| 2315 |
ao2_unlock(cdr); |
| 2316 |
ao2_cleanup(cdr); |
| 2317 |
} |
| 2318 |
|
| 2319 |
static int cdr_object_finalize_party_b(void *obj, void *arg, void *data, int flags) |
| 2320 |
{ |
| 2321 |
struct cdr_object *cdr = obj; |
| 2322 |
|
| 2323 |
if (!strcasecmp(cdr->party_b_name, arg)) { |
| 2324 |
#ifdef AST_DEVMODE |
| 2325 |
struct ast_channel_snapshot *party_b = data; |
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
ast_assert(cdr->party_b.snapshot |
| 2332 |
&& !strcasecmp(cdr->party_b.snapshot->base->name, party_b->base->name)); |
| 2333 |
#endif |
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
cdr_object_finalize(cdr); |
| 2339 |
} |
| 2340 |
return 0; |
| 2341 |
} |
| 2342 |
|
| 2343 |
static int cdr_object_update_party_b(void *obj, void *arg, void *data, int flags) |
| 2344 |
{ |
| 2345 |
struct cdr_object *cdr = obj; |
| 2346 |
|
| 2347 |
if (cdr->fn_table->process_party_b |
| 2348 |
&& !strcasecmp(cdr->party_b_name, arg)) { |
| 2349 |
struct ast_channel_snapshot *party_b = data; |
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
if (!cdr->party_b.snapshot |
| 2357 |
|| strcasecmp(cdr->party_b.snapshot->base->name, party_b->base->name)) { |
| 2358 |
ast_log(LOG_NOTICE, |
| 2359 |
"CDR for Party A %s(%s) has inconsistent Party B %s name. Message can be ignored but this shouldn't happen.\n", |
| 2360 |
cdr->linkedid, |
| 2361 |
cdr->party_a.snapshot->base->name, |
| 2362 |
cdr->party_b_name); |
| 2363 |
return 0; |
| 2364 |
} |
| 2365 |
|
| 2366 |
cdr->fn_table->process_party_b(cdr, party_b); |
| 2367 |
} |
| 2368 |
return 0; |
| 2369 |
} |
| 2370 |
|
| 2371 |
|
| 2372 |
static int check_new_cdr_needed(struct ast_channel_snapshot *old_snapshot, |
| 2373 |
struct ast_channel_snapshot *new_snapshot) |
| 2374 |
{ |
| 2375 |
|
| 2376 |
if (!new_snapshot |
| 2377 |
|| (ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC) |
| 2378 |
&& is_cdr_flag_set(CDR_END_BEFORE_H_EXTEN))) { |
| 2379 |
return 0; |
| 2380 |
} |
| 2381 |
|
| 2382 |
|
| 2383 |
if (ast_strlen_zero(new_snapshot->dialplan->appl)) { |
| 2384 |
return 0; |
| 2385 |
} |
| 2386 |
|
| 2387 |
if (old_snapshot && !snapshot_cep_changed(old_snapshot, new_snapshot)) { |
| 2388 |
return 0; |
| 2389 |
} |
| 2390 |
|
| 2391 |
return 1; |
| 2392 |
} |
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
static void handle_channel_snapshot_update_message(void *data, struct stasis_subscription *sub, struct stasis_message *message) |
| 2401 |
{ |
| 2402 |
struct cdr_object *cdr; |
| 2403 |
struct ast_channel_snapshot_update *update = stasis_message_data(message); |
| 2404 |
struct cdr_object *it_cdr; |
| 2405 |
|
| 2406 |
if (filter_channel_snapshot_message(update->old_snapshot, update->new_snapshot)) { |
| 2407 |
return; |
| 2408 |
} |
| 2409 |
|
| 2410 |
if (update->new_snapshot && !update->old_snapshot) { |
| 2411 |
struct module_config *mod_cfg = NULL; |
| 2412 |
|
| 2413 |
cdr = cdr_object_alloc(update->new_snapshot, stasis_message_timestamp(message)); |
| 2414 |
if (!cdr) { |
| 2415 |
return; |
| 2416 |
} |
| 2417 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 2418 |
cdr->is_root = 1; |
| 2419 |
ao2_link(active_cdrs_master, cdr); |
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
if (mod_cfg) { |
| 2424 |
if (!ast_test_flag(&mod_cfg->general->settings, CDR_CHANNEL_DEFAULT_ENABLED)) { |
| 2425 |
ast_debug(3, "Disable CDR by default\n"); |
| 2426 |
ast_set_flag(&cdr->flags, AST_CDR_FLAG_DISABLE_ALL); |
| 2427 |
} |
| 2428 |
ao2_cleanup(mod_cfg); |
| 2429 |
} |
| 2430 |
} else { |
| 2431 |
cdr = ao2_find(active_cdrs_master, update->new_snapshot->base->uniqueid, OBJ_SEARCH_KEY); |
| 2432 |
} |
| 2433 |
|
| 2434 |
|
| 2435 |
if (!cdr) { |
| 2436 |
ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", update->new_snapshot->base->name); |
| 2437 |
ast_assert(0); |
| 2438 |
} else { |
| 2439 |
int all_reject = 1; |
| 2440 |
|
| 2441 |
ao2_lock(cdr); |
| 2442 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2443 |
it_cdr->lastevent = *stasis_message_timestamp(message); |
| 2444 |
if (!it_cdr->fn_table->process_party_a) { |
| 2445 |
continue; |
| 2446 |
} |
| 2447 |
all_reject &= it_cdr->fn_table->process_party_a(it_cdr, update->new_snapshot); |
| 2448 |
} |
| 2449 |
if (all_reject && check_new_cdr_needed(update->old_snapshot, update->new_snapshot)) { |
| 2450 |
|
| 2451 |
struct cdr_object *new_cdr; |
| 2452 |
|
| 2453 |
new_cdr = cdr_object_create_and_append(cdr, stasis_message_timestamp(message)); |
| 2454 |
if (new_cdr) { |
| 2455 |
new_cdr->fn_table->process_party_a(new_cdr, update->new_snapshot); |
| 2456 |
} |
| 2457 |
} |
| 2458 |
ao2_unlock(cdr); |
| 2459 |
} |
| 2460 |
|
| 2461 |
if (ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) { |
| 2462 |
ao2_lock(cdr); |
| 2463 |
CDR_DEBUG("%p - Beginning finalize/dispatch for %s\n", cdr, update->old_snapshot->base->name); |
| 2464 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2465 |
it_cdr->lastevent = *stasis_message_timestamp(message); |
| 2466 |
cdr_object_finalize(it_cdr); |
| 2467 |
} |
| 2468 |
cdr_object_dispatch(cdr); |
| 2469 |
ao2_unlock(cdr); |
| 2470 |
|
| 2471 |
cdr_all_unlink(cdr); |
| 2472 |
ao2_unlink(active_cdrs_master, cdr); |
| 2473 |
} |
| 2474 |
|
| 2475 |
|
| 2476 |
if (update->new_snapshot) { |
| 2477 |
ao2_callback_data(active_cdrs_all, OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY, |
| 2478 |
cdr_object_update_party_b, (char *) update->new_snapshot->base->name, update->new_snapshot); |
| 2479 |
} |
| 2480 |
|
| 2481 |
if (ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) { |
| 2482 |
ao2_callback_data(active_cdrs_all, OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY, |
| 2483 |
cdr_object_finalize_party_b, (char *) update->new_snapshot->base->name, update->new_snapshot); |
| 2484 |
} |
| 2485 |
|
| 2486 |
ao2_cleanup(cdr); |
| 2487 |
} |
| 2488 |
|
| 2489 |
struct bridge_leave_data { |
| 2490 |
struct ast_bridge_snapshot *bridge; |
| 2491 |
struct ast_channel_snapshot *channel; |
| 2492 |
const struct timeval *lastevent; |
| 2493 |
}; |
| 2494 |
|
| 2495 |
|
| 2496 |
static int cdr_object_party_b_left_bridge_cb(void *obj, void *arg, void *data, int flags) |
| 2497 |
{ |
| 2498 |
struct cdr_object *cdr = obj; |
| 2499 |
struct bridge_leave_data *leave_data = data; |
| 2500 |
|
| 2501 |
if (cdr->fn_table == &bridge_state_fn_table |
| 2502 |
&& !strcmp(cdr->bridge, leave_data->bridge->uniqueid) |
| 2503 |
&& !strcasecmp(cdr->party_b_name, arg)) { |
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
ast_assert(cdr->party_b.snapshot |
| 2509 |
&& !strcasecmp(cdr->party_b.snapshot->base->name, leave_data->channel->base->name)); |
| 2510 |
|
| 2511 |
|
| 2512 |
|
| 2513 |
|
| 2514 |
cdr->lastevent = *leave_data->lastevent; |
| 2515 |
cdr_object_finalize(cdr); |
| 2516 |
} |
| 2517 |
return 0; |
| 2518 |
} |
| 2519 |
|
| 2520 |
|
| 2521 |
static int filter_bridge_messages(struct ast_bridge_snapshot *bridge) |
| 2522 |
{ |
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
if (!strcmp(bridge->technology, "holding_bridge") && strcmp(bridge->subclass, "parking")) { |
| 2527 |
return 1; |
| 2528 |
} |
| 2529 |
return 0; |
| 2530 |
} |
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
|
| 2538 |
static void handle_bridge_leave_message(void *data, struct stasis_subscription *sub, |
| 2539 |
struct stasis_message *message) |
| 2540 |
{ |
| 2541 |
struct ast_bridge_blob *update = stasis_message_data(message); |
| 2542 |
struct ast_bridge_snapshot *bridge = update->bridge; |
| 2543 |
struct ast_channel_snapshot *channel = update->channel; |
| 2544 |
struct cdr_object *cdr; |
| 2545 |
struct cdr_object *it_cdr; |
| 2546 |
struct bridge_leave_data leave_data = { |
| 2547 |
.bridge = bridge, |
| 2548 |
.channel = channel, |
| 2549 |
.lastevent = stasis_message_timestamp(message) |
| 2550 |
}; |
| 2551 |
int left_bridge = 0; |
| 2552 |
|
| 2553 |
if (filter_bridge_messages(bridge)) { |
| 2554 |
return; |
| 2555 |
} |
| 2556 |
|
| 2557 |
if (filter_channel_snapshot(channel)) { |
| 2558 |
return; |
| 2559 |
} |
| 2560 |
|
| 2561 |
CDR_DEBUG("Bridge Leave message for %s: %u.%08u\n", |
| 2562 |
channel->base->name, |
| 2563 |
(unsigned int)leave_data.lastevent->tv_sec, |
| 2564 |
(unsigned int)leave_data.lastevent->tv_usec); |
| 2565 |
|
| 2566 |
cdr = ao2_find(active_cdrs_master, channel->base->uniqueid, OBJ_SEARCH_KEY); |
| 2567 |
if (!cdr) { |
| 2568 |
ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->base->name); |
| 2569 |
ast_assert(0); |
| 2570 |
return; |
| 2571 |
} |
| 2572 |
|
| 2573 |
|
| 2574 |
ao2_lock(cdr); |
| 2575 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2576 |
it_cdr->lastevent = *leave_data.lastevent; |
| 2577 |
if (!it_cdr->fn_table->process_bridge_leave) { |
| 2578 |
continue; |
| 2579 |
} |
| 2580 |
CDR_DEBUG("%p - Processing Bridge Leave for %s\n", |
| 2581 |
it_cdr, channel->base->name); |
| 2582 |
if (!it_cdr->fn_table->process_bridge_leave(it_cdr, bridge, channel)) { |
| 2583 |
ast_string_field_set(it_cdr, bridge, ""); |
| 2584 |
left_bridge = 1; |
| 2585 |
} |
| 2586 |
} |
| 2587 |
ao2_unlock(cdr); |
| 2588 |
|
| 2589 |
|
| 2590 |
if (left_bridge |
| 2591 |
&& strcmp(bridge->subclass, "parking")) { |
| 2592 |
ao2_callback_data(active_cdrs_all, OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY, |
| 2593 |
cdr_object_party_b_left_bridge_cb, (char *) leave_data.channel->base->name, |
| 2594 |
&leave_data); |
| 2595 |
} |
| 2596 |
|
| 2597 |
ao2_cleanup(cdr); |
| 2598 |
} |
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
static void bridge_candidate_add_to_cdr(struct cdr_object *cdr, |
| 2607 |
struct cdr_object_snapshot *party_b) |
| 2608 |
{ |
| 2609 |
struct cdr_object *new_cdr; |
| 2610 |
|
| 2611 |
new_cdr = cdr_object_create_and_append(cdr, &cdr->lastevent); |
| 2612 |
if (!new_cdr) { |
| 2613 |
return; |
| 2614 |
} |
| 2615 |
cdr_object_snapshot_copy(&new_cdr->party_b, party_b); |
| 2616 |
cdr_all_relink(new_cdr); |
| 2617 |
cdr_object_check_party_a_answer(new_cdr); |
| 2618 |
ast_string_field_set(new_cdr, bridge, cdr->bridge); |
| 2619 |
cdr_object_transition_state(new_cdr, &bridge_state_fn_table); |
| 2620 |
CDR_DEBUG("%p - Party A %s has new Party B %s\n", |
| 2621 |
new_cdr, new_cdr->party_a.snapshot->base->name, |
| 2622 |
party_b->snapshot->base->name); |
| 2623 |
} |
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
static void bridge_candidate_process(struct cdr_object *cdr, struct cdr_object *base_cand_cdr) |
| 2638 |
{ |
| 2639 |
struct cdr_object_snapshot *party_a; |
| 2640 |
struct cdr_object *cand_cdr; |
| 2641 |
|
| 2642 |
ao2_lock(base_cand_cdr); |
| 2643 |
|
| 2644 |
for (cand_cdr = base_cand_cdr; cand_cdr; cand_cdr = cand_cdr->next) { |
| 2645 |
|
| 2646 |
if (strcmp(cand_cdr->bridge, cdr->bridge)) { |
| 2647 |
continue; |
| 2648 |
} |
| 2649 |
|
| 2650 |
|
| 2651 |
if (!strcasecmp(cdr->party_a.snapshot->base->name, cand_cdr->party_a.snapshot->base->name) |
| 2652 |
|| (cdr->party_b.snapshot |
| 2653 |
&& !strcasecmp(cdr->party_b.snapshot->base->name, cand_cdr->party_a.snapshot->base->name))) { |
| 2654 |
break; |
| 2655 |
} |
| 2656 |
|
| 2657 |
party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a); |
| 2658 |
|
| 2659 |
|
| 2660 |
if (!strcasecmp(party_a->snapshot->base->name, cdr->party_a.snapshot->base->name)) { |
| 2661 |
bridge_candidate_add_to_cdr(cdr, &cand_cdr->party_a); |
| 2662 |
break; |
| 2663 |
} |
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
if (cand_cdr->party_b.snapshot |
| 2668 |
&& strcasecmp(cand_cdr->party_b.snapshot->base->name, cdr->party_a.snapshot->base->name)) { |
| 2669 |
bridge_candidate_add_to_cdr(cand_cdr, &cdr->party_a); |
| 2670 |
} else { |
| 2671 |
CDR_DEBUG("%p - Party A %s has new Party B %s\n", |
| 2672 |
cand_cdr, cand_cdr->party_a.snapshot->base->name, |
| 2673 |
cdr->party_a.snapshot->base->name); |
| 2674 |
cdr_object_snapshot_copy(&cand_cdr->party_b, &cdr->party_a); |
| 2675 |
cdr_all_relink(cand_cdr); |
| 2676 |
|
| 2677 |
|
| 2678 |
|
| 2679 |
|
| 2680 |
memset(&cand_cdr->end, 0, sizeof(cand_cdr->end)); |
| 2681 |
} |
| 2682 |
|
| 2683 |
break; |
| 2684 |
} |
| 2685 |
|
| 2686 |
ao2_unlock(base_cand_cdr); |
| 2687 |
} |
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
static void handle_bridge_pairings(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge) |
| 2696 |
{ |
| 2697 |
struct ao2_iterator it_channels; |
| 2698 |
char *channel_id; |
| 2699 |
|
| 2700 |
it_channels = ao2_iterator_init(bridge->channels, 0); |
| 2701 |
while ((channel_id = ao2_iterator_next(&it_channels))) { |
| 2702 |
struct cdr_object *cand_cdr; |
| 2703 |
|
| 2704 |
cand_cdr = ao2_find(active_cdrs_master, channel_id, OBJ_SEARCH_KEY); |
| 2705 |
if (cand_cdr) { |
| 2706 |
bridge_candidate_process(cdr, cand_cdr); |
| 2707 |
ao2_ref(cand_cdr, -1); |
| 2708 |
} |
| 2709 |
|
| 2710 |
ao2_ref(channel_id, -1); |
| 2711 |
} |
| 2712 |
ao2_iterator_destroy(&it_channels); |
| 2713 |
} |
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
|
| 2721 |
static void handle_parking_bridge_enter_message(struct cdr_object *cdr, |
| 2722 |
struct ast_bridge_snapshot *bridge, |
| 2723 |
struct ast_channel_snapshot *channel, |
| 2724 |
const struct timeval *event_time) |
| 2725 |
{ |
| 2726 |
int res = 1; |
| 2727 |
struct cdr_object *it_cdr; |
| 2728 |
struct cdr_object *new_cdr; |
| 2729 |
|
| 2730 |
ao2_lock(cdr); |
| 2731 |
|
| 2732 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2733 |
it_cdr->lastevent = *event_time; |
| 2734 |
|
| 2735 |
if (it_cdr->fn_table->process_parking_bridge_enter) { |
| 2736 |
res &= it_cdr->fn_table->process_parking_bridge_enter(it_cdr, bridge, channel); |
| 2737 |
} |
| 2738 |
if (it_cdr->fn_table->process_party_a) { |
| 2739 |
CDR_DEBUG("%p - Updating Party A %s snapshot\n", it_cdr, |
| 2740 |
channel->base->name); |
| 2741 |
it_cdr->fn_table->process_party_a(it_cdr, channel); |
| 2742 |
} |
| 2743 |
} |
| 2744 |
|
| 2745 |
if (res) { |
| 2746 |
|
| 2747 |
new_cdr = cdr_object_create_and_append(cdr, event_time); |
| 2748 |
if (new_cdr) { |
| 2749 |
|
| 2750 |
cdr_object_transition_state(new_cdr, &single_state_fn_table); |
| 2751 |
new_cdr->fn_table->process_parking_bridge_enter(new_cdr, bridge, channel); |
| 2752 |
} |
| 2753 |
} |
| 2754 |
ao2_unlock(cdr); |
| 2755 |
} |
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
static void handle_standard_bridge_enter_message(struct cdr_object *cdr, |
| 2764 |
struct ast_bridge_snapshot *bridge, |
| 2765 |
struct ast_channel_snapshot *channel, |
| 2766 |
const struct timeval *event_time) |
| 2767 |
{ |
| 2768 |
enum process_bridge_enter_results result; |
| 2769 |
struct cdr_object *it_cdr; |
| 2770 |
struct cdr_object *new_cdr; |
| 2771 |
struct cdr_object *handled_cdr = NULL; |
| 2772 |
|
| 2773 |
ao2_lock(cdr); |
| 2774 |
|
| 2775 |
try_again: |
| 2776 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2777 |
it_cdr->lastevent = *event_time; |
| 2778 |
|
| 2779 |
if (it_cdr->fn_table->process_party_a) { |
| 2780 |
CDR_DEBUG("%p - Updating Party A %s snapshot\n", it_cdr, |
| 2781 |
channel->base->name); |
| 2782 |
it_cdr->fn_table->process_party_a(it_cdr, channel); |
| 2783 |
} |
| 2784 |
|
| 2785 |
|
| 2786 |
if (it_cdr->fn_table->process_bridge_enter) { |
| 2787 |
CDR_DEBUG("%p - Processing bridge enter for %s\n", it_cdr, |
| 2788 |
channel->base->name); |
| 2789 |
result = it_cdr->fn_table->process_bridge_enter(it_cdr, bridge, channel); |
| 2790 |
switch (result) { |
| 2791 |
case BRIDGE_ENTER_ONLY_PARTY: |
| 2792 |
|
| 2793 |
case BRIDGE_ENTER_OBTAINED_PARTY_B: |
| 2794 |
if (!handled_cdr) { |
| 2795 |
handled_cdr = it_cdr; |
| 2796 |
} |
| 2797 |
break; |
| 2798 |
case BRIDGE_ENTER_NEED_CDR: |
| 2799 |
|
| 2800 |
break; |
| 2801 |
case BRIDGE_ENTER_NO_PARTY_B: |
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
if (!handled_cdr) { |
| 2806 |
handled_cdr = it_cdr; |
| 2807 |
} |
| 2808 |
cdr_object_finalize(cdr); |
| 2809 |
break; |
| 2810 |
} |
| 2811 |
} |
| 2812 |
} |
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
|
| 2821 |
if (handled_cdr) { |
| 2822 |
handle_bridge_pairings(handled_cdr, bridge); |
| 2823 |
} else { |
| 2824 |
|
| 2825 |
new_cdr = cdr_object_create_and_append(cdr, event_time); |
| 2826 |
if (new_cdr) { |
| 2827 |
|
| 2828 |
|
| 2829 |
|
| 2830 |
goto try_again; |
| 2831 |
} |
| 2832 |
} |
| 2833 |
ao2_unlock(cdr); |
| 2834 |
} |
| 2835 |
|
| 2836 |
|
| 2837 |
|
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
static void handle_bridge_enter_message(void *data, struct stasis_subscription *sub, |
| 2844 |
struct stasis_message *message) |
| 2845 |
{ |
| 2846 |
struct ast_bridge_blob *update = stasis_message_data(message); |
| 2847 |
struct ast_bridge_snapshot *bridge = update->bridge; |
| 2848 |
struct ast_channel_snapshot *channel = update->channel; |
| 2849 |
struct cdr_object *cdr; |
| 2850 |
|
| 2851 |
if (filter_bridge_messages(bridge)) { |
| 2852 |
return; |
| 2853 |
} |
| 2854 |
|
| 2855 |
if (filter_channel_snapshot(channel)) { |
| 2856 |
return; |
| 2857 |
} |
| 2858 |
|
| 2859 |
CDR_DEBUG("Bridge Enter message for channel %s: %u.%08u\n", |
| 2860 |
channel->base->name, |
| 2861 |
(unsigned int)stasis_message_timestamp(message)->tv_sec, |
| 2862 |
(unsigned int)stasis_message_timestamp(message)->tv_usec); |
| 2863 |
|
| 2864 |
cdr = ao2_find(active_cdrs_master, channel->base->uniqueid, OBJ_SEARCH_KEY); |
| 2865 |
if (!cdr) { |
| 2866 |
ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->base->name); |
| 2867 |
ast_assert(0); |
| 2868 |
return; |
| 2869 |
} |
| 2870 |
|
| 2871 |
if (!strcmp(bridge->subclass, "parking")) { |
| 2872 |
handle_parking_bridge_enter_message(cdr, bridge, channel, stasis_message_timestamp(message)); |
| 2873 |
} else { |
| 2874 |
handle_standard_bridge_enter_message(cdr, bridge, channel, stasis_message_timestamp(message)); |
| 2875 |
} |
| 2876 |
ao2_cleanup(cdr); |
| 2877 |
} |
| 2878 |
|
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
|
| 2885 |
static void handle_parked_call_message(void *data, struct stasis_subscription *sub, |
| 2886 |
struct stasis_message *message) |
| 2887 |
{ |
| 2888 |
struct ast_parked_call_payload *payload = stasis_message_data(message); |
| 2889 |
struct ast_channel_snapshot *channel = payload->parkee; |
| 2890 |
struct cdr_object *cdr; |
| 2891 |
int unhandled = 1; |
| 2892 |
struct cdr_object *it_cdr; |
| 2893 |
|
| 2894 |
|
| 2895 |
if (payload->event_type != PARKED_CALL) { |
| 2896 |
return; |
| 2897 |
} |
| 2898 |
|
| 2899 |
|
| 2900 |
if (!channel) { |
| 2901 |
return; |
| 2902 |
} |
| 2903 |
|
| 2904 |
if (filter_channel_snapshot(channel)) { |
| 2905 |
return; |
| 2906 |
} |
| 2907 |
|
| 2908 |
CDR_DEBUG("Parked Call message for channel %s: %u.%08u\n", |
| 2909 |
channel->base->name, |
| 2910 |
(unsigned int)stasis_message_timestamp(message)->tv_sec, |
| 2911 |
(unsigned int)stasis_message_timestamp(message)->tv_usec); |
| 2912 |
|
| 2913 |
cdr = ao2_find(active_cdrs_master, channel->base->uniqueid, OBJ_SEARCH_KEY); |
| 2914 |
if (!cdr) { |
| 2915 |
ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->base->name); |
| 2916 |
ast_assert(0); |
| 2917 |
return; |
| 2918 |
} |
| 2919 |
|
| 2920 |
ao2_lock(cdr); |
| 2921 |
|
| 2922 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 2923 |
it_cdr->lastevent = *stasis_message_timestamp(message); |
| 2924 |
if (it_cdr->fn_table->process_parked_channel) { |
| 2925 |
unhandled &= it_cdr->fn_table->process_parked_channel(it_cdr, payload); |
| 2926 |
} |
| 2927 |
} |
| 2928 |
|
| 2929 |
if (unhandled) { |
| 2930 |
|
| 2931 |
struct cdr_object *new_cdr; |
| 2932 |
|
| 2933 |
new_cdr = cdr_object_create_and_append(cdr, stasis_message_timestamp(message)); |
| 2934 |
if (new_cdr) { |
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
new_cdr->fn_table->process_parked_channel(new_cdr, payload); |
| 2939 |
} |
| 2940 |
} |
| 2941 |
|
| 2942 |
ao2_unlock(cdr); |
| 2943 |
|
| 2944 |
ao2_cleanup(cdr); |
| 2945 |
} |
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
static void handle_cdr_sync_message(void *data, struct stasis_subscription *sub, |
| 2954 |
struct stasis_message *message) |
| 2955 |
{ |
| 2956 |
return; |
| 2957 |
} |
| 2958 |
|
| 2959 |
struct ast_cdr_config *ast_cdr_get_config(void) |
| 2960 |
{ |
| 2961 |
struct ast_cdr_config *general; |
| 2962 |
struct module_config *mod_cfg; |
| 2963 |
|
| 2964 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 2965 |
if (!mod_cfg) { |
| 2966 |
return NULL; |
| 2967 |
} |
| 2968 |
general = ao2_bump(mod_cfg->general); |
| 2969 |
ao2_cleanup(mod_cfg); |
| 2970 |
return general; |
| 2971 |
} |
| 2972 |
|
| 2973 |
void ast_cdr_set_config(struct ast_cdr_config *config) |
| 2974 |
{ |
| 2975 |
struct module_config *mod_cfg; |
| 2976 |
|
| 2977 |
if (!config) { |
| 2978 |
return; |
| 2979 |
} |
| 2980 |
|
| 2981 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 2982 |
if (!mod_cfg) { |
| 2983 |
return; |
| 2984 |
} |
| 2985 |
|
| 2986 |
ao2_replace(mod_cfg->general, config); |
| 2987 |
|
| 2988 |
cdr_set_debug_mode(mod_cfg); |
| 2989 |
cdr_toggle_runtime_options(); |
| 2990 |
|
| 2991 |
ao2_cleanup(mod_cfg); |
| 2992 |
} |
| 2993 |
|
| 2994 |
int ast_cdr_is_enabled(void) |
| 2995 |
{ |
| 2996 |
return is_cdr_flag_set(CDR_ENABLED); |
| 2997 |
} |
| 2998 |
|
| 2999 |
int ast_cdr_backend_suspend(const char *name) |
| 3000 |
{ |
| 3001 |
int success = -1; |
| 3002 |
struct cdr_beitem *i = NULL; |
| 3003 |
|
| 3004 |
AST_RWLIST_WRLOCK(&be_list); |
| 3005 |
AST_RWLIST_TRAVERSE(&be_list, i, list) { |
| 3006 |
if (!strcasecmp(name, i->name)) { |
| 3007 |
ast_debug(3, "Suspending CDR backend %s\n", i->name); |
| 3008 |
i->suspended = 1; |
| 3009 |
success = 0; |
| 3010 |
} |
| 3011 |
} |
| 3012 |
AST_RWLIST_UNLOCK(&be_list); |
| 3013 |
|
| 3014 |
return success; |
| 3015 |
} |
| 3016 |
|
| 3017 |
int ast_cdr_backend_unsuspend(const char *name) |
| 3018 |
{ |
| 3019 |
int success = -1; |
| 3020 |
struct cdr_beitem *i = NULL; |
| 3021 |
|
| 3022 |
AST_RWLIST_WRLOCK(&be_list); |
| 3023 |
AST_RWLIST_TRAVERSE(&be_list, i, list) { |
| 3024 |
if (!strcasecmp(name, i->name)) { |
| 3025 |
ast_debug(3, "Unsuspending CDR backend %s\n", i->name); |
| 3026 |
i->suspended = 0; |
| 3027 |
success = 0; |
| 3028 |
} |
| 3029 |
} |
| 3030 |
AST_RWLIST_UNLOCK(&be_list); |
| 3031 |
|
| 3032 |
return success; |
| 3033 |
} |
| 3034 |
|
| 3035 |
static int cdr_generic_register(struct be_list *generic_list, const char *name, const char *desc, ast_cdrbe be) |
| 3036 |
{ |
| 3037 |
struct cdr_beitem *i; |
| 3038 |
struct cdr_beitem *cur; |
| 3039 |
|
| 3040 |
if (!name) { |
| 3041 |
return -1; |
| 3042 |
} |
| 3043 |
|
| 3044 |
if (!be) { |
| 3045 |
ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name); |
| 3046 |
|
| 3047 |
return -1; |
| 3048 |
} |
| 3049 |
|
| 3050 |
i = ast_calloc(1, sizeof(*i)); |
| 3051 |
if (!i) { |
| 3052 |
return -1; |
| 3053 |
} |
| 3054 |
|
| 3055 |
i->be = be; |
| 3056 |
ast_copy_string(i->name, name, sizeof(i->name)); |
| 3057 |
ast_copy_string(i->desc, desc, sizeof(i->desc)); |
| 3058 |
|
| 3059 |
AST_RWLIST_WRLOCK(generic_list); |
| 3060 |
AST_RWLIST_TRAVERSE(generic_list, cur, list) { |
| 3061 |
if (!strcasecmp(name, cur->name)) { |
| 3062 |
ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name); |
| 3063 |
AST_RWLIST_UNLOCK(generic_list); |
| 3064 |
ast_free(i); |
| 3065 |
|
| 3066 |
return -1; |
| 3067 |
} |
| 3068 |
} |
| 3069 |
|
| 3070 |
AST_RWLIST_INSERT_HEAD(generic_list, i, list); |
| 3071 |
AST_RWLIST_UNLOCK(generic_list); |
| 3072 |
|
| 3073 |
return 0; |
| 3074 |
} |
| 3075 |
|
| 3076 |
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be) |
| 3077 |
{ |
| 3078 |
return cdr_generic_register(&be_list, name, desc, be); |
| 3079 |
} |
| 3080 |
|
| 3081 |
int ast_cdr_modifier_register(const char *name, const char *desc, ast_cdrbe be) |
| 3082 |
{ |
| 3083 |
return cdr_generic_register((struct be_list *)&mo_list, name, desc, be); |
| 3084 |
} |
| 3085 |
|
| 3086 |
static int ast_cdr_generic_unregister(struct be_list *generic_list, const char *name) |
| 3087 |
{ |
| 3088 |
struct cdr_beitem *match = NULL; |
| 3089 |
int active_count; |
| 3090 |
|
| 3091 |
AST_RWLIST_WRLOCK(generic_list); |
| 3092 |
AST_RWLIST_TRAVERSE(generic_list, match, list) { |
| 3093 |
if (!strcasecmp(name, match->name)) { |
| 3094 |
break; |
| 3095 |
} |
| 3096 |
} |
| 3097 |
|
| 3098 |
if (!match) { |
| 3099 |
AST_RWLIST_UNLOCK(generic_list); |
| 3100 |
return 0; |
| 3101 |
} |
| 3102 |
|
| 3103 |
active_count = ao2_container_count(active_cdrs_master); |
| 3104 |
|
| 3105 |
if (!match->suspended && active_count != 0) { |
| 3106 |
AST_RWLIST_UNLOCK(generic_list); |
| 3107 |
ast_log(AST_LOG_WARNING, "Unable to unregister CDR backend %s; %d CDRs are still active\n", |
| 3108 |
name, active_count); |
| 3109 |
return -1; |
| 3110 |
} |
| 3111 |
|
| 3112 |
AST_RWLIST_REMOVE(generic_list, match, list); |
| 3113 |
AST_RWLIST_UNLOCK(generic_list); |
| 3114 |
|
| 3115 |
ast_verb(5, "Unregistered '%s' CDR backend\n", name); |
| 3116 |
ast_free(match); |
| 3117 |
|
| 3118 |
return 0; |
| 3119 |
} |
| 3120 |
|
| 3121 |
int ast_cdr_unregister(const char *name) |
| 3122 |
{ |
| 3123 |
return ast_cdr_generic_unregister(&be_list, name); |
| 3124 |
} |
| 3125 |
|
| 3126 |
int ast_cdr_modifier_unregister(const char *name) |
| 3127 |
{ |
| 3128 |
return ast_cdr_generic_unregister((struct be_list *)&mo_list, name); |
| 3129 |
} |
| 3130 |
|
| 3131 |
struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr) |
| 3132 |
{ |
| 3133 |
struct ast_cdr *newcdr; |
| 3134 |
|
| 3135 |
if (!cdr) { |
| 3136 |
return NULL; |
| 3137 |
} |
| 3138 |
newcdr = ast_cdr_alloc(); |
| 3139 |
if (!newcdr) { |
| 3140 |
return NULL; |
| 3141 |
} |
| 3142 |
|
| 3143 |
*newcdr = *cdr; |
| 3144 |
AST_LIST_HEAD_INIT_NOLOCK(&newcdr->varshead); |
| 3145 |
copy_variables(&newcdr->varshead, &cdr->varshead); |
| 3146 |
newcdr->next = NULL; |
| 3147 |
|
| 3148 |
return newcdr; |
| 3149 |
} |
| 3150 |
|
| 3151 |
static const char *cdr_format_var_internal(struct ast_cdr *cdr, const char *name) |
| 3152 |
{ |
| 3153 |
struct ast_var_t *variables; |
| 3154 |
|
| 3155 |
if (ast_strlen_zero(name)) { |
| 3156 |
return NULL; |
| 3157 |
} |
| 3158 |
|
| 3159 |
AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) { |
| 3160 |
if (!strcasecmp(name, ast_var_name(variables))) { |
| 3161 |
return ast_var_value(variables); |
| 3162 |
} |
| 3163 |
} |
| 3164 |
|
| 3165 |
return NULL; |
| 3166 |
} |
| 3167 |
|
| 3168 |
static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize) |
| 3169 |
{ |
| 3170 |
if (fmt == NULL) { |
| 3171 |
snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec); |
| 3172 |
} else { |
| 3173 |
buf[0] = '\0'; |
| 3174 |
if (when.tv_sec) { |
| 3175 |
struct ast_tm tm; |
| 3176 |
|
| 3177 |
ast_localtime(&when, &tm, NULL); |
| 3178 |
ast_strftime(buf, bufsize, fmt, &tm); |
| 3179 |
} |
| 3180 |
} |
| 3181 |
} |
| 3182 |
|
| 3183 |
void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw) |
| 3184 |
{ |
| 3185 |
const char *fmt = "%Y-%m-%d %T"; |
| 3186 |
const char *varbuf; |
| 3187 |
|
| 3188 |
if (!cdr) { |
| 3189 |
return; |
| 3190 |
} |
| 3191 |
|
| 3192 |
*ret = NULL; |
| 3193 |
|
| 3194 |
if (!strcasecmp(name, "clid")) { |
| 3195 |
ast_copy_string(workspace, cdr->clid, workspacelen); |
| 3196 |
} else if (!strcasecmp(name, "src")) { |
| 3197 |
ast_copy_string(workspace, cdr->src, workspacelen); |
| 3198 |
} else if (!strcasecmp(name, "dst")) { |
| 3199 |
ast_copy_string(workspace, cdr->dst, workspacelen); |
| 3200 |
} else if (!strcasecmp(name, "dcontext")) { |
| 3201 |
ast_copy_string(workspace, cdr->dcontext, workspacelen); |
| 3202 |
} else if (!strcasecmp(name, "channel")) { |
| 3203 |
ast_copy_string(workspace, cdr->channel, workspacelen); |
| 3204 |
} else if (!strcasecmp(name, "dstchannel")) { |
| 3205 |
ast_copy_string(workspace, cdr->dstchannel, workspacelen); |
| 3206 |
} else if (!strcasecmp(name, "lastapp")) { |
| 3207 |
ast_copy_string(workspace, cdr->lastapp, workspacelen); |
| 3208 |
} else if (!strcasecmp(name, "lastdata")) { |
| 3209 |
ast_copy_string(workspace, cdr->lastdata, workspacelen); |
| 3210 |
} else if (!strcasecmp(name, "start")) { |
| 3211 |
cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen); |
| 3212 |
} else if (!strcasecmp(name, "answer")) { |
| 3213 |
cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen); |
| 3214 |
} else if (!strcasecmp(name, "end")) { |
| 3215 |
cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen); |
| 3216 |
} else if (!strcasecmp(name, "duration")) { |
| 3217 |
snprintf(workspace, workspacelen, "%ld", cdr->end.tv_sec != 0 ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000); |
| 3218 |
} else if (!strcasecmp(name, "billsec")) { |
| 3219 |
snprintf(workspace, workspacelen, "%ld", (cdr->billsec || !ast_tvzero(cdr->end) || ast_tvzero(cdr->answer)) ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000); |
| 3220 |
} else if (!strcasecmp(name, "disposition")) { |
| 3221 |
if (raw) { |
| 3222 |
snprintf(workspace, workspacelen, "%ld", cdr->disposition); |
| 3223 |
} else { |
| 3224 |
ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen); |
| 3225 |
} |
| 3226 |
} else if (!strcasecmp(name, "amaflags")) { |
| 3227 |
if (raw) { |
| 3228 |
snprintf(workspace, workspacelen, "%ld", cdr->amaflags); |
| 3229 |
} else { |
| 3230 |
ast_copy_string(workspace, ast_channel_amaflags2string(cdr->amaflags), workspacelen); |
| 3231 |
} |
| 3232 |
} else if (!strcasecmp(name, "accountcode")) { |
| 3233 |
ast_copy_string(workspace, cdr->accountcode, workspacelen); |
| 3234 |
} else if (!strcasecmp(name, "peeraccount")) { |
| 3235 |
ast_copy_string(workspace, cdr->peeraccount, workspacelen); |
| 3236 |
} else if (!strcasecmp(name, "uniqueid")) { |
| 3237 |
ast_copy_string(workspace, cdr->uniqueid, workspacelen); |
| 3238 |
} else if (!strcasecmp(name, "linkedid")) { |
| 3239 |
ast_copy_string(workspace, cdr->linkedid, workspacelen); |
| 3240 |
} else if (!strcasecmp(name, "tenantid")) { |
| 3241 |
ast_copy_string(workspace, cdr->tenantid, workspacelen); |
| 3242 |
} else if (!strcasecmp(name, "peertenantid")) { |
| 3243 |
ast_copy_string(workspace, cdr->peertenantid, workspacelen); |
| 3244 |
} else if (!strcasecmp(name, "userfield")) { |
| 3245 |
ast_copy_string(workspace, cdr->userfield, workspacelen); |
| 3246 |
} else if (!strcasecmp(name, "sequence")) { |
| 3247 |
snprintf(workspace, workspacelen, "%d", cdr->sequence); |
| 3248 |
} else if ((varbuf = cdr_format_var_internal(cdr, name))) { |
| 3249 |
ast_copy_string(workspace, varbuf, workspacelen); |
| 3250 |
} else { |
| 3251 |
workspace[0] = '\0'; |
| 3252 |
} |
| 3253 |
|
| 3254 |
if (!ast_strlen_zero(workspace)) { |
| 3255 |
*ret = workspace; |
| 3256 |
} |
| 3257 |
} |
| 3258 |
|
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 3262 |
|
| 3263 |
static int cdr_object_select_all_by_name_cb(void *obj, void *arg, int flags) |
| 3264 |
{ |
| 3265 |
struct cdr_object *cdr = obj; |
| 3266 |
const char *name = arg; |
| 3267 |
|
| 3268 |
if (!strcasecmp(cdr->party_a.snapshot->base->name, name) || |
| 3269 |
(cdr->party_b.snapshot && !strcasecmp(cdr->party_b.snapshot->base->name, name))) { |
| 3270 |
return CMP_MATCH; |
| 3271 |
} |
| 3272 |
return 0; |
| 3273 |
} |
| 3274 |
|
| 3275 |
|
| 3276 |
|
| 3277 |
|
| 3278 |
|
| 3279 |
static int cdr_object_get_by_name_cb(void *obj, void *arg, int flags) |
| 3280 |
{ |
| 3281 |
struct cdr_object *cdr = obj; |
| 3282 |
const char *name = arg; |
| 3283 |
|
| 3284 |
if (!strcasecmp(cdr->party_a.snapshot->base->name, name)) { |
| 3285 |
return CMP_MATCH; |
| 3286 |
} |
| 3287 |
return 0; |
| 3288 |
} |
| 3289 |
|
| 3290 |
|
| 3291 |
static const char * const cdr_readonly_vars[] = { |
| 3292 |
"clid", |
| 3293 |
"src", |
| 3294 |
"dst", |
| 3295 |
"dcontext", |
| 3296 |
"channel", |
| 3297 |
"dstchannel", |
| 3298 |
"lastapp", |
| 3299 |
"lastdata", |
| 3300 |
"start", |
| 3301 |
"answer", |
| 3302 |
"end", |
| 3303 |
"duration", |
| 3304 |
"billsec", |
| 3305 |
"disposition", |
| 3306 |
"amaflags", |
| 3307 |
"accountcode", |
| 3308 |
"uniqueid", |
| 3309 |
"linkedid", |
| 3310 |
"tenantid", |
| 3311 |
"userfield", |
| 3312 |
"sequence", |
| 3313 |
NULL |
| 3314 |
}; |
| 3315 |
|
| 3316 |
int ast_cdr_setvar(const char *channel_name, const char *name, const char *value) |
| 3317 |
{ |
| 3318 |
struct cdr_object *cdr; |
| 3319 |
struct cdr_object *it_cdr; |
| 3320 |
struct ao2_iterator *it_cdrs; |
| 3321 |
char *arg = ast_strdupa(channel_name); |
| 3322 |
int x; |
| 3323 |
|
| 3324 |
for (x = 0; cdr_readonly_vars[x]; x++) { |
| 3325 |
if (!strcasecmp(name, cdr_readonly_vars[x])) { |
| 3326 |
ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!\n", name); |
| 3327 |
return -1; |
| 3328 |
} |
| 3329 |
} |
| 3330 |
|
| 3331 |
it_cdrs = ao2_callback(active_cdrs_master, OBJ_MULTIPLE, cdr_object_select_all_by_name_cb, arg); |
| 3332 |
if (!it_cdrs) { |
| 3333 |
ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name); |
| 3334 |
return -1; |
| 3335 |
} |
| 3336 |
|
| 3337 |
for (; (cdr = ao2_iterator_next(it_cdrs)); ao2_unlock(cdr), ao2_cleanup(cdr)) { |
| 3338 |
ao2_lock(cdr); |
| 3339 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 3340 |
struct varshead *headp = NULL; |
| 3341 |
|
| 3342 |
if (it_cdr->fn_table == &finalized_state_fn_table && it_cdr->next != NULL) { |
| 3343 |
continue; |
| 3344 |
} |
| 3345 |
if (!strcasecmp(channel_name, it_cdr->party_a.snapshot->base->name)) { |
| 3346 |
headp = &it_cdr->party_a.variables; |
| 3347 |
} else if (it_cdr->party_b.snapshot |
| 3348 |
&& !strcasecmp(channel_name, it_cdr->party_b.snapshot->base->name)) { |
| 3349 |
headp = &it_cdr->party_b.variables; |
| 3350 |
} |
| 3351 |
if (headp) { |
| 3352 |
set_variable(headp, name, value); |
| 3353 |
} |
| 3354 |
} |
| 3355 |
} |
| 3356 |
ao2_iterator_destroy(it_cdrs); |
| 3357 |
|
| 3358 |
return 0; |
| 3359 |
} |
| 3360 |
|
| 3361 |
|
| 3362 |
|
| 3363 |
|
| 3364 |
static void cdr_object_format_var_internal(struct cdr_object *cdr, const char *name, char *value, size_t length) |
| 3365 |
{ |
| 3366 |
struct ast_var_t *variable; |
| 3367 |
|
| 3368 |
AST_LIST_TRAVERSE(&cdr->party_a.variables, variable, entries) { |
| 3369 |
if (!strcasecmp(name, ast_var_name(variable))) { |
| 3370 |
ast_copy_string(value, ast_var_value(variable), length); |
| 3371 |
return; |
| 3372 |
} |
| 3373 |
} |
| 3374 |
|
| 3375 |
*value = '\0'; |
| 3376 |
} |
| 3377 |
|
| 3378 |
|
| 3379 |
|
| 3380 |
|
| 3381 |
static int cdr_object_format_property(struct cdr_object *cdr_obj, const char *name, char *value, size_t length) |
| 3382 |
{ |
| 3383 |
struct ast_channel_snapshot *party_a = cdr_obj->party_a.snapshot; |
| 3384 |
struct ast_channel_snapshot *party_b = cdr_obj->party_b.snapshot; |
| 3385 |
|
| 3386 |
if (!strcasecmp(name, "clid")) { |
| 3387 |
ast_callerid_merge(value, length, party_a->caller->name, party_a->caller->number, ""); |
| 3388 |
} else if (!strcasecmp(name, "src")) { |
| 3389 |
ast_copy_string(value, party_a->caller->number, length); |
| 3390 |
} else if (!strcasecmp(name, "dst")) { |
| 3391 |
ast_copy_string(value, party_a->dialplan->exten, length); |
| 3392 |
} else if (!strcasecmp(name, "dcontext")) { |
| 3393 |
ast_copy_string(value, party_a->dialplan->context, length); |
| 3394 |
} else if (!strcasecmp(name, "channel")) { |
| 3395 |
ast_copy_string(value, party_a->base->name, length); |
| 3396 |
} else if (!strcasecmp(name, "dstchannel")) { |
| 3397 |
if (party_b) { |
| 3398 |
ast_copy_string(value, party_b->base->name, length); |
| 3399 |
} else { |
| 3400 |
ast_copy_string(value, "", length); |
| 3401 |
} |
| 3402 |
} else if (!strcasecmp(name, "lastapp")) { |
| 3403 |
ast_copy_string(value, party_a->dialplan->appl, length); |
| 3404 |
} else if (!strcasecmp(name, "lastdata")) { |
| 3405 |
ast_copy_string(value, party_a->dialplan->data, length); |
| 3406 |
} else if (!strcasecmp(name, "start")) { |
| 3407 |
cdr_get_tv(cdr_obj->start, NULL, value, length); |
| 3408 |
} else if (!strcasecmp(name, "answer")) { |
| 3409 |
cdr_get_tv(cdr_obj->answer, NULL, value, length); |
| 3410 |
} else if (!strcasecmp(name, "end")) { |
| 3411 |
cdr_get_tv(cdr_obj->end, NULL, value, length); |
| 3412 |
} else if (!strcasecmp(name, "duration")) { |
| 3413 |
snprintf(value, length, "%ld", cdr_object_get_duration(cdr_obj)); |
| 3414 |
} else if (!strcasecmp(name, "billsec")) { |
| 3415 |
snprintf(value, length, "%ld", cdr_object_get_billsec(cdr_obj)); |
| 3416 |
} else if (!strcasecmp(name, "disposition")) { |
| 3417 |
snprintf(value, length, "%u", cdr_obj->disposition); |
| 3418 |
} else if (!strcasecmp(name, "amaflags")) { |
| 3419 |
snprintf(value, length, "%d", party_a->amaflags); |
| 3420 |
} else if (!strcasecmp(name, "accountcode")) { |
| 3421 |
ast_copy_string(value, party_a->base->accountcode, length); |
| 3422 |
} else if (!strcasecmp(name, "peeraccount")) { |
| 3423 |
if (party_b) { |
| 3424 |
ast_copy_string(value, party_b->base->accountcode, length); |
| 3425 |
} else { |
| 3426 |
ast_copy_string(value, "", length); |
| 3427 |
} |
| 3428 |
} else if (!strcasecmp(name, "uniqueid")) { |
| 3429 |
ast_copy_string(value, party_a->base->uniqueid, length); |
| 3430 |
} else if (!strcasecmp(name, "linkedid")) { |
| 3431 |
ast_copy_string(value, cdr_obj->linkedid, length); |
| 3432 |
} else if (!strcasecmp(name, "tenantid")) { |
| 3433 |
ast_copy_string(value, party_a->base->tenantid, length); |
| 3434 |
} else if (!strcasecmp(name, "peertenantid")) { |
| 3435 |
if (party_b) { |
| 3436 |
ast_copy_string(value, party_b->base->tenantid, length); |
| 3437 |
} else { |
| 3438 |
ast_copy_string(value, "", length); |
| 3439 |
} |
| 3440 |
} else if (!strcasecmp(name, "userfield")) { |
| 3441 |
ast_copy_string(value, cdr_obj->party_a.userfield, length); |
| 3442 |
} else if (!strcasecmp(name, "sequence")) { |
| 3443 |
snprintf(value, length, "%u", cdr_obj->sequence); |
| 3444 |
} else { |
| 3445 |
return 1; |
| 3446 |
} |
| 3447 |
|
| 3448 |
return 0; |
| 3449 |
} |
| 3450 |
|
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
|
| 3455 |
|
| 3456 |
|
| 3457 |
|
| 3458 |
static struct cdr_object *cdr_object_get_by_name(const char *name) |
| 3459 |
{ |
| 3460 |
char *param; |
| 3461 |
|
| 3462 |
if (ast_strlen_zero(name)) { |
| 3463 |
return NULL; |
| 3464 |
} |
| 3465 |
|
| 3466 |
param = ast_strdupa(name); |
| 3467 |
return ao2_callback(active_cdrs_master, 0, cdr_object_get_by_name_cb, param); |
| 3468 |
} |
| 3469 |
|
| 3470 |
int ast_cdr_getvar(const char *channel_name, const char *name, char *value, size_t length) |
| 3471 |
{ |
| 3472 |
struct cdr_object *cdr; |
| 3473 |
struct cdr_object *cdr_obj; |
| 3474 |
|
| 3475 |
if (ast_strlen_zero(name)) { |
| 3476 |
return 1; |
| 3477 |
} |
| 3478 |
|
| 3479 |
cdr = cdr_object_get_by_name(channel_name); |
| 3480 |
if (!cdr) { |
| 3481 |
ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name); |
| 3482 |
return 1; |
| 3483 |
} |
| 3484 |
|
| 3485 |
ao2_lock(cdr); |
| 3486 |
|
| 3487 |
cdr_obj = cdr->last; |
| 3488 |
if (cdr_object_format_property(cdr_obj, name, value, length)) { |
| 3489 |
|
| 3490 |
cdr_object_format_var_internal(cdr_obj, name, value, length); |
| 3491 |
} |
| 3492 |
|
| 3493 |
ao2_unlock(cdr); |
| 3494 |
|
| 3495 |
ao2_cleanup(cdr); |
| 3496 |
return 0; |
| 3497 |
} |
| 3498 |
|
| 3499 |
int ast_cdr_serialize_variables(const char *channel_name, struct ast_str **buf, char delim, char sep) |
| 3500 |
{ |
| 3501 |
struct cdr_object *cdr; |
| 3502 |
struct cdr_object *it_cdr; |
| 3503 |
struct ast_var_t *variable; |
| 3504 |
const char *var; |
| 3505 |
char workspace[256]; |
| 3506 |
int total = 0, x = 0, i; |
| 3507 |
|
| 3508 |
cdr = cdr_object_get_by_name(channel_name); |
| 3509 |
if (!cdr) { |
| 3510 |
if (is_cdr_flag_set(CDR_ENABLED)) { |
| 3511 |
ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name); |
| 3512 |
} |
| 3513 |
return 0; |
| 3514 |
} |
| 3515 |
|
| 3516 |
ast_str_reset(*buf); |
| 3517 |
|
| 3518 |
ao2_lock(cdr); |
| 3519 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 3520 |
if (++x > 1) { |
| 3521 |
ast_str_append(buf, 0, "\n"); |
| 3522 |
} |
| 3523 |
|
| 3524 |
AST_LIST_TRAVERSE(&it_cdr->party_a.variables, variable, entries) { |
| 3525 |
if (!(var = ast_var_name(variable))) { |
| 3526 |
continue; |
| 3527 |
} |
| 3528 |
|
| 3529 |
if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variable), ""), sep) < 0) { |
| 3530 |
ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n"); |
| 3531 |
break; |
| 3532 |
} |
| 3533 |
|
| 3534 |
total++; |
| 3535 |
} |
| 3536 |
|
| 3537 |
for (i = 0; cdr_readonly_vars[i]; i++) { |
| 3538 |
if (cdr_object_format_property(it_cdr, cdr_readonly_vars[i], workspace, sizeof(workspace))) { |
| 3539 |
|
| 3540 |
ast_assert(0); |
| 3541 |
continue; |
| 3542 |
} |
| 3543 |
|
| 3544 |
if (!ast_strlen_zero(workspace) |
| 3545 |
&& ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, workspace, sep) < 0) { |
| 3546 |
ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n"); |
| 3547 |
break; |
| 3548 |
} |
| 3549 |
total++; |
| 3550 |
} |
| 3551 |
} |
| 3552 |
ao2_unlock(cdr); |
| 3553 |
ao2_cleanup(cdr); |
| 3554 |
return total; |
| 3555 |
} |
| 3556 |
|
| 3557 |
void ast_cdr_free(struct ast_cdr *cdr) |
| 3558 |
{ |
| 3559 |
while (cdr) { |
| 3560 |
struct ast_cdr *next = cdr->next; |
| 3561 |
|
| 3562 |
free_variables(&cdr->varshead); |
| 3563 |
ast_free(cdr); |
| 3564 |
cdr = next; |
| 3565 |
} |
| 3566 |
} |
| 3567 |
|
| 3568 |
struct ast_cdr *ast_cdr_alloc(void) |
| 3569 |
{ |
| 3570 |
struct ast_cdr *x; |
| 3571 |
|
| 3572 |
x = ast_calloc(1, sizeof(*x)); |
| 3573 |
return x; |
| 3574 |
} |
| 3575 |
|
| 3576 |
const char *ast_cdr_disp2str(int disposition) |
| 3577 |
{ |
| 3578 |
switch (disposition) { |
| 3579 |
case AST_CDR_NULL: |
| 3580 |
return "NO ANSWER"; |
| 3581 |
case AST_CDR_NOANSWER: |
| 3582 |
return "NO ANSWER"; |
| 3583 |
case AST_CDR_FAILED: |
| 3584 |
return "FAILED"; |
| 3585 |
case AST_CDR_BUSY: |
| 3586 |
return "BUSY"; |
| 3587 |
case AST_CDR_ANSWERED: |
| 3588 |
return "ANSWERED"; |
| 3589 |
case AST_CDR_CONGESTION: |
| 3590 |
return "CONGESTION"; |
| 3591 |
case AST_CDR_CANCEL: |
| 3592 |
return "CANCEL"; |
| 3593 |
} |
| 3594 |
return "UNKNOWN"; |
| 3595 |
} |
| 3596 |
|
| 3597 |
struct party_b_userfield_update { |
| 3598 |
const char *channel_name; |
| 3599 |
const char *userfield; |
| 3600 |
}; |
| 3601 |
|
| 3602 |
|
| 3603 |
static int cdr_object_update_party_b_userfield_cb(void *obj, void *arg, void *data, int flags) |
| 3604 |
{ |
| 3605 |
struct cdr_object *cdr = obj; |
| 3606 |
|
| 3607 |
if ((cdr->fn_table != &finalized_state_fn_table || !cdr->next) |
| 3608 |
&& !strcasecmp(cdr->party_b_name, arg)) { |
| 3609 |
struct party_b_userfield_update *info = data; |
| 3610 |
|
| 3611 |
|
| 3612 |
|
| 3613 |
|
| 3614 |
|
| 3615 |
ast_assert(cdr->party_b.snapshot |
| 3616 |
&& !strcasecmp(cdr->party_b.snapshot->base->name, info->channel_name)); |
| 3617 |
|
| 3618 |
ast_copy_string(cdr->party_b.userfield, info->userfield, |
| 3619 |
sizeof(cdr->party_b.userfield)); |
| 3620 |
} |
| 3621 |
|
| 3622 |
return 0; |
| 3623 |
} |
| 3624 |
|
| 3625 |
void ast_cdr_setuserfield(const char *channel_name, const char *userfield) |
| 3626 |
{ |
| 3627 |
struct cdr_object *cdr; |
| 3628 |
struct party_b_userfield_update party_b_info = { |
| 3629 |
.channel_name = channel_name, |
| 3630 |
.userfield = userfield, |
| 3631 |
}; |
| 3632 |
struct cdr_object *it_cdr; |
| 3633 |
|
| 3634 |
|
| 3635 |
cdr = cdr_object_get_by_name(channel_name); |
| 3636 |
if (cdr) { |
| 3637 |
ao2_lock(cdr); |
| 3638 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 3639 |
if (it_cdr->fn_table == &finalized_state_fn_table && it_cdr->next != NULL) { |
| 3640 |
continue; |
| 3641 |
} |
| 3642 |
ast_copy_string(it_cdr->party_a.userfield, userfield, |
| 3643 |
sizeof(it_cdr->party_a.userfield)); |
| 3644 |
} |
| 3645 |
ao2_unlock(cdr); |
| 3646 |
} |
| 3647 |
|
| 3648 |
|
| 3649 |
ao2_callback_data(active_cdrs_all, OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY, |
| 3650 |
cdr_object_update_party_b_userfield_cb, (char *) party_b_info.channel_name, |
| 3651 |
&party_b_info); |
| 3652 |
|
| 3653 |
ao2_cleanup(cdr); |
| 3654 |
} |
| 3655 |
|
| 3656 |
static void post_cdr(struct ast_cdr *cdr) |
| 3657 |
{ |
| 3658 |
struct module_config *mod_cfg; |
| 3659 |
struct cdr_beitem *i; |
| 3660 |
|
| 3661 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 3662 |
if (!mod_cfg) { |
| 3663 |
return; |
| 3664 |
} |
| 3665 |
|
| 3666 |
for (; cdr ; cdr = cdr->next) { |
| 3667 |
|
| 3668 |
if (!ast_test_flag(&mod_cfg->general->settings, CDR_UNANSWERED) && |
| 3669 |
cdr->disposition < AST_CDR_ANSWERED && |
| 3670 |
(ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) { |
| 3671 |
ast_debug(1, "Skipping CDR for %s since we weren't answered\n", cdr->channel); |
| 3672 |
continue; |
| 3673 |
} |
| 3674 |
|
| 3675 |
|
| 3676 |
AST_RWLIST_RDLOCK(&mo_list); |
| 3677 |
AST_RWLIST_TRAVERSE(&mo_list, i, list) { |
| 3678 |
i->be(cdr); |
| 3679 |
} |
| 3680 |
AST_RWLIST_UNLOCK(&mo_list); |
| 3681 |
|
| 3682 |
if (ast_test_flag(cdr, AST_CDR_FLAG_DISABLE)) { |
| 3683 |
continue; |
| 3684 |
} |
| 3685 |
AST_RWLIST_RDLOCK(&be_list); |
| 3686 |
AST_RWLIST_TRAVERSE(&be_list, i, list) { |
| 3687 |
if (!i->suspended) { |
| 3688 |
i->be(cdr); |
| 3689 |
} |
| 3690 |
} |
| 3691 |
AST_RWLIST_UNLOCK(&be_list); |
| 3692 |
} |
| 3693 |
ao2_cleanup(mod_cfg); |
| 3694 |
} |
| 3695 |
|
| 3696 |
int ast_cdr_set_property(const char *channel_name, enum ast_cdr_options option) |
| 3697 |
{ |
| 3698 |
struct cdr_object *cdr; |
| 3699 |
struct cdr_object *it_cdr; |
| 3700 |
|
| 3701 |
cdr = cdr_object_get_by_name(channel_name); |
| 3702 |
if (!cdr) { |
| 3703 |
return -1; |
| 3704 |
} |
| 3705 |
|
| 3706 |
ao2_lock(cdr); |
| 3707 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 3708 |
if (it_cdr->fn_table == &finalized_state_fn_table) { |
| 3709 |
continue; |
| 3710 |
} |
| 3711 |
|
| 3712 |
|
| 3713 |
|
| 3714 |
ast_set_flag(&it_cdr->flags, option); |
| 3715 |
ast_set_flag(&it_cdr->party_a, option); |
| 3716 |
} |
| 3717 |
ao2_unlock(cdr); |
| 3718 |
|
| 3719 |
ao2_cleanup(cdr); |
| 3720 |
return 0; |
| 3721 |
} |
| 3722 |
|
| 3723 |
int ast_cdr_clear_property(const char *channel_name, enum ast_cdr_options option) |
| 3724 |
{ |
| 3725 |
struct cdr_object *cdr; |
| 3726 |
struct cdr_object *it_cdr; |
| 3727 |
|
| 3728 |
cdr = cdr_object_get_by_name(channel_name); |
| 3729 |
if (!cdr) { |
| 3730 |
return -1; |
| 3731 |
} |
| 3732 |
|
| 3733 |
ao2_lock(cdr); |
| 3734 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 3735 |
if (it_cdr->fn_table == &finalized_state_fn_table) { |
| 3736 |
continue; |
| 3737 |
} |
| 3738 |
ast_clear_flag(&it_cdr->flags, option); |
| 3739 |
} |
| 3740 |
ao2_unlock(cdr); |
| 3741 |
|
| 3742 |
ao2_cleanup(cdr); |
| 3743 |
return 0; |
| 3744 |
} |
| 3745 |
|
| 3746 |
int ast_cdr_reset(const char *channel_name, int keep_variables) |
| 3747 |
{ |
| 3748 |
struct cdr_object *cdr; |
| 3749 |
struct ast_var_t *vardata; |
| 3750 |
struct cdr_object *it_cdr; |
| 3751 |
|
| 3752 |
cdr = cdr_object_get_by_name(channel_name); |
| 3753 |
if (!cdr) { |
| 3754 |
return -1; |
| 3755 |
} |
| 3756 |
|
| 3757 |
ao2_lock(cdr); |
| 3758 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 3759 |
|
| 3760 |
if (!keep_variables) { |
| 3761 |
while ((vardata = AST_LIST_REMOVE_HEAD(&it_cdr->party_a.variables, entries))) { |
| 3762 |
ast_var_delete(vardata); |
| 3763 |
} |
| 3764 |
if (cdr->party_b.snapshot) { |
| 3765 |
while ((vardata = AST_LIST_REMOVE_HEAD(&it_cdr->party_b.variables, entries))) { |
| 3766 |
ast_var_delete(vardata); |
| 3767 |
} |
| 3768 |
} |
| 3769 |
} |
| 3770 |
|
| 3771 |
|
| 3772 |
memset(&it_cdr->start, 0, sizeof(it_cdr->start)); |
| 3773 |
memset(&it_cdr->end, 0, sizeof(it_cdr->end)); |
| 3774 |
memset(&it_cdr->answer, 0, sizeof(it_cdr->answer)); |
| 3775 |
it_cdr->start = ast_tvnow(); |
| 3776 |
it_cdr->lastevent = it_cdr->start; |
| 3777 |
cdr_object_check_party_a_answer(it_cdr); |
| 3778 |
} |
| 3779 |
ao2_unlock(cdr); |
| 3780 |
|
| 3781 |
ao2_cleanup(cdr); |
| 3782 |
return 0; |
| 3783 |
} |
| 3784 |
|
| 3785 |
int ast_cdr_fork(const char *channel_name, struct ast_flags *options) |
| 3786 |
{ |
| 3787 |
RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup); |
| 3788 |
struct cdr_object *new_cdr; |
| 3789 |
struct cdr_object *it_cdr; |
| 3790 |
struct cdr_object *cdr_obj; |
| 3791 |
|
| 3792 |
if (!cdr) { |
| 3793 |
return -1; |
| 3794 |
} |
| 3795 |
|
| 3796 |
{ |
| 3797 |
SCOPED_AO2LOCK(lock, cdr); |
| 3798 |
struct timeval now = ast_tvnow(); |
| 3799 |
|
| 3800 |
cdr_obj = cdr->last; |
| 3801 |
if (cdr_obj->fn_table == &finalized_state_fn_table) { |
| 3802 |
|
| 3803 |
|
| 3804 |
|
| 3805 |
return -1; |
| 3806 |
} |
| 3807 |
|
| 3808 |
|
| 3809 |
|
| 3810 |
|
| 3811 |
ast_debug(1, "Forking CDR for channel %s\n", cdr->party_a.snapshot->base->name); |
| 3812 |
new_cdr = cdr_object_create_and_append(cdr, &now); |
| 3813 |
if (!new_cdr) { |
| 3814 |
return -1; |
| 3815 |
} |
| 3816 |
new_cdr->fn_table = cdr_obj->fn_table; |
| 3817 |
ast_string_field_set(new_cdr, bridge, cdr->bridge); |
| 3818 |
ast_string_field_set(new_cdr, appl, cdr->appl); |
| 3819 |
ast_string_field_set(new_cdr, data, cdr->data); |
| 3820 |
ast_string_field_set(new_cdr, context, cdr->context); |
| 3821 |
ast_string_field_set(new_cdr, exten, cdr->exten); |
| 3822 |
new_cdr->flags = cdr->flags; |
| 3823 |
|
| 3824 |
|
| 3825 |
|
| 3826 |
|
| 3827 |
ast_clear_flag(&new_cdr->flags, AST_CDR_LOCK_APP); |
| 3828 |
|
| 3829 |
|
| 3830 |
if (cdr_obj->party_b.snapshot) { |
| 3831 |
new_cdr->party_b.snapshot = cdr_obj->party_b.snapshot; |
| 3832 |
ao2_ref(new_cdr->party_b.snapshot, +1); |
| 3833 |
cdr_all_relink(new_cdr); |
| 3834 |
strcpy(new_cdr->party_b.userfield, cdr_obj->party_b.userfield); |
| 3835 |
new_cdr->party_b.flags = cdr_obj->party_b.flags; |
| 3836 |
if (ast_test_flag(options, AST_CDR_FLAG_KEEP_VARS)) { |
| 3837 |
copy_variables(&new_cdr->party_b.variables, &cdr_obj->party_b.variables); |
| 3838 |
} |
| 3839 |
} |
| 3840 |
new_cdr->start = cdr_obj->start; |
| 3841 |
new_cdr->answer = cdr_obj->answer; |
| 3842 |
new_cdr->lastevent = ast_tvnow(); |
| 3843 |
|
| 3844 |
|
| 3845 |
if (ast_test_flag(options, AST_CDR_FLAG_SET_ANSWER) |
| 3846 |
&& new_cdr->party_a.snapshot->state == AST_STATE_UP) { |
| 3847 |
new_cdr->answer = ast_tvnow(); |
| 3848 |
} |
| 3849 |
if (ast_test_flag(options, AST_CDR_FLAG_RESET)) { |
| 3850 |
new_cdr->answer = ast_tvnow(); |
| 3851 |
new_cdr->start = ast_tvnow(); |
| 3852 |
} |
| 3853 |
|
| 3854 |
|
| 3855 |
if (!ast_test_flag(options, AST_CDR_FLAG_KEEP_VARS)) { |
| 3856 |
free_variables(&new_cdr->party_a.variables); |
| 3857 |
} |
| 3858 |
|
| 3859 |
|
| 3860 |
if (ast_test_flag(options, AST_CDR_FLAG_FINALIZE)) { |
| 3861 |
for (it_cdr = cdr; it_cdr != new_cdr; it_cdr = it_cdr->next) { |
| 3862 |
if (it_cdr->fn_table == &finalized_state_fn_table) { |
| 3863 |
continue; |
| 3864 |
} |
| 3865 |
|
| 3866 |
|
| 3867 |
|
| 3868 |
cdr_object_finalize(it_cdr); |
| 3869 |
cdr_object_transition_state(it_cdr, &finalized_state_fn_table); |
| 3870 |
} |
| 3871 |
} |
| 3872 |
} |
| 3873 |
|
| 3874 |
return 0; |
| 3875 |
} |
| 3876 |
|
| 3877 |
|
| 3878 |
static void reset_batch(void) |
| 3879 |
{ |
| 3880 |
batch->size = 0; |
| 3881 |
batch->head = NULL; |
| 3882 |
batch->tail = NULL; |
| 3883 |
} |
| 3884 |
|
| 3885 |
|
| 3886 |
static int init_batch(void) |
| 3887 |
{ |
| 3888 |
|
| 3889 |
if (!(batch = ast_malloc(sizeof(*batch)))) |
| 3890 |
return -1; |
| 3891 |
|
| 3892 |
reset_batch(); |
| 3893 |
|
| 3894 |
return 0; |
| 3895 |
} |
| 3896 |
|
| 3897 |
static void *do_batch_backend_process(void *data) |
| 3898 |
{ |
| 3899 |
struct cdr_batch_item *processeditem; |
| 3900 |
struct cdr_batch_item *batchitem = data; |
| 3901 |
|
| 3902 |
|
| 3903 |
while (batchitem) { |
| 3904 |
post_cdr(batchitem->cdr); |
| 3905 |
ast_cdr_free(batchitem->cdr); |
| 3906 |
processeditem = batchitem; |
| 3907 |
batchitem = batchitem->next; |
| 3908 |
ast_free(processeditem); |
| 3909 |
} |
| 3910 |
|
| 3911 |
return NULL; |
| 3912 |
} |
| 3913 |
|
| 3914 |
static void cdr_submit_batch(int do_shutdown) |
| 3915 |
{ |
| 3916 |
struct module_config *mod_cfg; |
| 3917 |
struct cdr_batch_item *oldbatchitems = NULL; |
| 3918 |
pthread_t batch_post_thread = AST_PTHREADT_NULL; |
| 3919 |
|
| 3920 |
|
| 3921 |
if (!batch || !batch->head) { |
| 3922 |
return; |
| 3923 |
} |
| 3924 |
|
| 3925 |
|
| 3926 |
ast_mutex_lock(&cdr_batch_lock); |
| 3927 |
oldbatchitems = batch->head; |
| 3928 |
reset_batch(); |
| 3929 |
ast_mutex_unlock(&cdr_batch_lock); |
| 3930 |
|
| 3931 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
if (!mod_cfg |
| 3936 |
|| ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SCHEDULER_ONLY) |
| 3937 |
|| do_shutdown) { |
| 3938 |
ast_debug(1, "CDR single-threaded batch processing begins now\n"); |
| 3939 |
do_batch_backend_process(oldbatchitems); |
| 3940 |
} else { |
| 3941 |
if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) { |
| 3942 |
ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n"); |
| 3943 |
do_batch_backend_process(oldbatchitems); |
| 3944 |
} else { |
| 3945 |
ast_debug(1, "CDR multi-threaded batch processing begins now\n"); |
| 3946 |
} |
| 3947 |
} |
| 3948 |
|
| 3949 |
ao2_cleanup(mod_cfg); |
| 3950 |
} |
| 3951 |
|
| 3952 |
static int submit_scheduled_batch(const void *data) |
| 3953 |
{ |
| 3954 |
struct module_config *mod_cfg; |
| 3955 |
int nextms; |
| 3956 |
|
| 3957 |
cdr_submit_batch(0); |
| 3958 |
|
| 3959 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 3960 |
if (!mod_cfg) { |
| 3961 |
return 0; |
| 3962 |
} |
| 3963 |
|
| 3964 |
|
| 3965 |
nextms = mod_cfg->general->batch_settings.time * 1000; |
| 3966 |
|
| 3967 |
ao2_cleanup(mod_cfg); |
| 3968 |
|
| 3969 |
return nextms; |
| 3970 |
} |
| 3971 |
|
| 3972 |
|
| 3973 |
static void start_batch_mode(void) |
| 3974 |
{ |
| 3975 |
|
| 3976 |
ast_mutex_lock(&cdr_sched_lock); |
| 3977 |
|
| 3978 |
AST_SCHED_DEL(sched, cdr_sched); |
| 3979 |
|
| 3980 |
cdr_sched = ast_sched_add_variable(sched, 1, submit_scheduled_batch, NULL, 1); |
| 3981 |
ast_mutex_unlock(&cdr_sched_lock); |
| 3982 |
|
| 3983 |
|
| 3984 |
ast_mutex_lock(&cdr_pending_lock); |
| 3985 |
ast_cond_signal(&cdr_pending_cond); |
| 3986 |
ast_mutex_unlock(&cdr_pending_lock); |
| 3987 |
} |
| 3988 |
|
| 3989 |
static void cdr_detach(struct ast_cdr *cdr) |
| 3990 |
{ |
| 3991 |
struct cdr_batch_item *newtail; |
| 3992 |
int curr; |
| 3993 |
RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup); |
| 3994 |
int submit_batch = 0; |
| 3995 |
|
| 3996 |
if (!cdr) { |
| 3997 |
return; |
| 3998 |
} |
| 3999 |
|
| 4000 |
|
| 4001 |
if (!mod_cfg || !ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) { |
| 4002 |
ast_debug(1, "Dropping CDR !\n"); |
| 4003 |
ast_cdr_free(cdr); |
| 4004 |
return; |
| 4005 |
} |
| 4006 |
|
| 4007 |
|
| 4008 |
if (!ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4009 |
post_cdr(cdr); |
| 4010 |
ast_cdr_free(cdr); |
| 4011 |
return; |
| 4012 |
} |
| 4013 |
|
| 4014 |
|
| 4015 |
ast_debug(1, "CDR detaching from this thread\n"); |
| 4016 |
|
| 4017 |
|
| 4018 |
if (!(newtail = ast_calloc(1, sizeof(*newtail)))) { |
| 4019 |
post_cdr(cdr); |
| 4020 |
ast_cdr_free(cdr); |
| 4021 |
return; |
| 4022 |
} |
| 4023 |
|
| 4024 |
|
| 4025 |
ast_mutex_lock(&cdr_batch_lock); |
| 4026 |
if (!batch) |
| 4027 |
init_batch(); |
| 4028 |
if (!batch->head) { |
| 4029 |
|
| 4030 |
batch->head = newtail; |
| 4031 |
} else { |
| 4032 |
|
| 4033 |
batch->tail->next = newtail; |
| 4034 |
} |
| 4035 |
newtail->cdr = cdr; |
| 4036 |
batch->tail = newtail; |
| 4037 |
curr = batch->size++; |
| 4038 |
|
| 4039 |
|
| 4040 |
if (curr >= (mod_cfg->general->batch_settings.size - 1)) { |
| 4041 |
submit_batch = 1; |
| 4042 |
} |
| 4043 |
ast_mutex_unlock(&cdr_batch_lock); |
| 4044 |
|
| 4045 |
|
| 4046 |
if (submit_batch) { |
| 4047 |
start_batch_mode(); |
| 4048 |
} |
| 4049 |
} |
| 4050 |
|
| 4051 |
static void *do_cdr(void *data) |
| 4052 |
{ |
| 4053 |
struct timespec timeout; |
| 4054 |
int schedms; |
| 4055 |
int numevents = 0; |
| 4056 |
|
| 4057 |
for (;;) { |
| 4058 |
struct timeval now; |
| 4059 |
schedms = ast_sched_wait(sched); |
| 4060 |
|
| 4061 |
if (schedms < 0) |
| 4062 |
schedms = 1000; |
| 4063 |
now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000)); |
| 4064 |
timeout.tv_sec = now.tv_sec; |
| 4065 |
timeout.tv_nsec = now.tv_usec * 1000; |
| 4066 |
|
| 4067 |
ast_mutex_lock(&cdr_pending_lock); |
| 4068 |
ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout); |
| 4069 |
numevents = ast_sched_runq(sched); |
| 4070 |
ast_mutex_unlock(&cdr_pending_lock); |
| 4071 |
ast_debug(2, "Processed %d CDR batches from the run queue\n", numevents); |
| 4072 |
} |
| 4073 |
|
| 4074 |
return NULL; |
| 4075 |
} |
| 4076 |
|
| 4077 |
static char *handle_cli_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4078 |
{ |
| 4079 |
struct module_config *mod_cfg; |
| 4080 |
|
| 4081 |
switch (cmd) { |
| 4082 |
case CLI_INIT: |
| 4083 |
e->command = "cdr set debug [on|off]"; |
| 4084 |
e->usage = "Enable or disable extra debugging in the CDR Engine. Note\n" |
| 4085 |
"that this will dump debug information to the VERBOSE setting\n" |
| 4086 |
"and should only be used when debugging information from the\n" |
| 4087 |
"CDR engine is needed.\n"; |
| 4088 |
return NULL; |
| 4089 |
case CLI_GENERATE: |
| 4090 |
return NULL; |
| 4091 |
} |
| 4092 |
|
| 4093 |
if (a->argc != 4) { |
| 4094 |
return CLI_SHOWUSAGE; |
| 4095 |
} |
| 4096 |
|
| 4097 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 4098 |
if (!mod_cfg) { |
| 4099 |
ast_cli(a->fd, "Could not set CDR debugging mode\n"); |
| 4100 |
return CLI_SUCCESS; |
| 4101 |
} |
| 4102 |
if (!strcasecmp(a->argv[3], "on") |
| 4103 |
&& !ast_test_flag(&mod_cfg->general->settings, CDR_DEBUG)) { |
| 4104 |
ast_set_flag(&mod_cfg->general->settings, CDR_DEBUG); |
| 4105 |
ast_cli(a->fd, "CDR debugging enabled\n"); |
| 4106 |
} else if (!strcasecmp(a->argv[3], "off") |
| 4107 |
&& ast_test_flag(&mod_cfg->general->settings, CDR_DEBUG)) { |
| 4108 |
ast_clear_flag(&mod_cfg->general->settings, CDR_DEBUG); |
| 4109 |
ast_cli(a->fd, "CDR debugging disabled\n"); |
| 4110 |
} |
| 4111 |
cdr_set_debug_mode(mod_cfg); |
| 4112 |
ao2_cleanup(mod_cfg); |
| 4113 |
|
| 4114 |
return CLI_SUCCESS; |
| 4115 |
} |
| 4116 |
|
| 4117 |
|
| 4118 |
static char *cli_complete_show(struct ast_cli_args *a) |
| 4119 |
{ |
| 4120 |
int wordlen = strlen(a->word); |
| 4121 |
struct ao2_iterator it_cdrs; |
| 4122 |
struct cdr_object *cdr; |
| 4123 |
|
| 4124 |
it_cdrs = ao2_iterator_init(active_cdrs_master, 0); |
| 4125 |
while ((cdr = ao2_iterator_next(&it_cdrs))) { |
| 4126 |
if (!strncasecmp(a->word, cdr->party_a.snapshot->base->name, wordlen)) { |
| 4127 |
if (ast_cli_completion_add(ast_strdup(cdr->party_a.snapshot->base->name))) { |
| 4128 |
ao2_ref(cdr, -1); |
| 4129 |
break; |
| 4130 |
} |
| 4131 |
} |
| 4132 |
ao2_ref(cdr, -1); |
| 4133 |
} |
| 4134 |
ao2_iterator_destroy(&it_cdrs); |
| 4135 |
|
| 4136 |
return NULL; |
| 4137 |
} |
| 4138 |
|
| 4139 |
static void cli_show_channels(struct ast_cli_args *a) |
| 4140 |
{ |
| 4141 |
struct ao2_iterator it_cdrs; |
| 4142 |
struct cdr_object *cdr; |
| 4143 |
char start_time_buffer[64]; |
| 4144 |
char answer_time_buffer[64]; |
| 4145 |
char end_time_buffer[64]; |
| 4146 |
|
| 4147 |
#define TITLE_STRING "%-25.25s %-25.25s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8s %-8.8s\n" |
| 4148 |
#define FORMAT_STRING "%-25.25s %-25.25s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8ld %-8.8ld\n" |
| 4149 |
|
| 4150 |
ast_cli(a->fd, "\n"); |
| 4151 |
ast_cli(a->fd, "Channels with Call Detail Record (CDR) Information\n"); |
| 4152 |
ast_cli(a->fd, "--------------------------------------------------\n"); |
| 4153 |
ast_cli(a->fd, TITLE_STRING, "Channel", "Dst. Channel", "LastApp", "Start", "Answer", "End", "Billsec", "Duration"); |
| 4154 |
|
| 4155 |
it_cdrs = ao2_iterator_init(active_cdrs_master, 0); |
| 4156 |
for (; (cdr = ao2_iterator_next(&it_cdrs)); ao2_cleanup(cdr)) { |
| 4157 |
struct cdr_object *it_cdr; |
| 4158 |
struct timeval start_time = { 0, }; |
| 4159 |
struct timeval answer_time = { 0, }; |
| 4160 |
struct timeval end_time = { 0, }; |
| 4161 |
|
| 4162 |
SCOPED_AO2LOCK(lock, cdr); |
| 4163 |
|
| 4164 |
|
| 4165 |
|
| 4166 |
|
| 4167 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 4168 |
if (snapshot_is_dialed(it_cdr->party_a.snapshot)) { |
| 4169 |
continue; |
| 4170 |
} |
| 4171 |
if (ast_tvzero(start_time)) { |
| 4172 |
start_time = it_cdr->start; |
| 4173 |
} |
| 4174 |
if (!ast_tvzero(it_cdr->answer) && ast_tvzero(answer_time)) { |
| 4175 |
answer_time = it_cdr->answer; |
| 4176 |
} |
| 4177 |
} |
| 4178 |
|
| 4179 |
|
| 4180 |
if (ast_tvzero(start_time)) { |
| 4181 |
continue; |
| 4182 |
} |
| 4183 |
it_cdr = cdr->last; |
| 4184 |
|
| 4185 |
end_time = ast_tvzero(it_cdr->end) ? ast_tvnow() : it_cdr->end; |
| 4186 |
cdr_get_tv(start_time, "%T", start_time_buffer, sizeof(start_time_buffer)); |
| 4187 |
cdr_get_tv(answer_time, "%T", answer_time_buffer, sizeof(answer_time_buffer)); |
| 4188 |
cdr_get_tv(end_time, "%T", end_time_buffer, sizeof(end_time_buffer)); |
| 4189 |
ast_cli(a->fd, FORMAT_STRING, it_cdr->party_a.snapshot->base->name, |
| 4190 |
it_cdr->party_b.snapshot ? it_cdr->party_b.snapshot->base->name : "<none>", |
| 4191 |
it_cdr->appl, |
| 4192 |
start_time_buffer, |
| 4193 |
answer_time_buffer, |
| 4194 |
end_time_buffer, |
| 4195 |
ast_tvzero(answer_time) ? 0 : (long)ast_tvdiff_ms(end_time, answer_time) / 1000, |
| 4196 |
(long)ast_tvdiff_ms(end_time, start_time) / 1000); |
| 4197 |
} |
| 4198 |
ao2_iterator_destroy(&it_cdrs); |
| 4199 |
#undef FORMAT_STRING |
| 4200 |
#undef TITLE_STRING |
| 4201 |
} |
| 4202 |
|
| 4203 |
static void cli_show_channel(struct ast_cli_args *a) |
| 4204 |
{ |
| 4205 |
struct cdr_object *it_cdr; |
| 4206 |
char clid[64]; |
| 4207 |
char start_time_buffer[64]; |
| 4208 |
char answer_time_buffer[64]; |
| 4209 |
char end_time_buffer[64]; |
| 4210 |
const char *channel_name = a->argv[3]; |
| 4211 |
struct cdr_object *cdr; |
| 4212 |
|
| 4213 |
#define TITLE_STRING "%-10.10s %-20.20s %-25.25s %-15.15s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8s %-8.8s\n" |
| 4214 |
#define FORMAT_STRING "%-10.10s %-20.20s %-25.25s %-15.15s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8ld %-8.8ld\n" |
| 4215 |
|
| 4216 |
cdr = cdr_object_get_by_name(channel_name); |
| 4217 |
if (!cdr) { |
| 4218 |
ast_cli(a->fd, "Unknown channel: %s\n", channel_name); |
| 4219 |
return; |
| 4220 |
} |
| 4221 |
|
| 4222 |
ast_cli(a->fd, "\n"); |
| 4223 |
ast_cli(a->fd, "Call Detail Record (CDR) Information for %s\n", channel_name); |
| 4224 |
ast_cli(a->fd, "--------------------------------------------------\n"); |
| 4225 |
ast_cli(a->fd, TITLE_STRING, "AccountCode", "CallerID", "Dst. Channel", "LastApp", "Data", "Start", "Answer", "End", "Billsec", "Duration"); |
| 4226 |
|
| 4227 |
ao2_lock(cdr); |
| 4228 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 4229 |
struct timeval end; |
| 4230 |
|
| 4231 |
if (snapshot_is_dialed(it_cdr->party_a.snapshot)) { |
| 4232 |
continue; |
| 4233 |
} |
| 4234 |
ast_callerid_merge(clid, sizeof(clid), it_cdr->party_a.snapshot->caller->name, it_cdr->party_a.snapshot->caller->number, ""); |
| 4235 |
if (ast_tvzero(it_cdr->end)) { |
| 4236 |
end = ast_tvnow(); |
| 4237 |
} else { |
| 4238 |
end = it_cdr->end; |
| 4239 |
} |
| 4240 |
cdr_get_tv(it_cdr->start, "%T", start_time_buffer, sizeof(start_time_buffer)); |
| 4241 |
cdr_get_tv(it_cdr->answer, "%T", answer_time_buffer, sizeof(answer_time_buffer)); |
| 4242 |
cdr_get_tv(end, "%T", end_time_buffer, sizeof(end_time_buffer)); |
| 4243 |
ast_cli(a->fd, FORMAT_STRING, |
| 4244 |
it_cdr->party_a.snapshot->base->accountcode, |
| 4245 |
clid, |
| 4246 |
it_cdr->party_b.snapshot ? it_cdr->party_b.snapshot->base->name : "<none>", |
| 4247 |
it_cdr->appl, |
| 4248 |
it_cdr->data, |
| 4249 |
start_time_buffer, |
| 4250 |
answer_time_buffer, |
| 4251 |
end_time_buffer, |
| 4252 |
(long)ast_tvdiff_ms(end, it_cdr->answer) / 1000, |
| 4253 |
(long)ast_tvdiff_ms(end, it_cdr->start) / 1000); |
| 4254 |
} |
| 4255 |
ao2_unlock(cdr); |
| 4256 |
|
| 4257 |
ao2_cleanup(cdr); |
| 4258 |
|
| 4259 |
#undef FORMAT_STRING |
| 4260 |
#undef TITLE_STRING |
| 4261 |
} |
| 4262 |
|
| 4263 |
static char *handle_cli_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4264 |
{ |
| 4265 |
switch (cmd) { |
| 4266 |
case CLI_INIT: |
| 4267 |
e->command = "cdr show active"; |
| 4268 |
e->usage = |
| 4269 |
"Usage: cdr show active [channel]\n" |
| 4270 |
" Displays a summary of all Call Detail Records when [channel]\n" |
| 4271 |
" is omitted; displays all of the Call Detail Records\n" |
| 4272 |
" currently in flight for a given [channel] when [channel] is\n" |
| 4273 |
" specified.\n\n" |
| 4274 |
" Note that this will not display Call Detail Records that\n" |
| 4275 |
" have already been dispatched to a backend storage, nor for\n" |
| 4276 |
" channels that are no longer active.\n"; |
| 4277 |
return NULL; |
| 4278 |
case CLI_GENERATE: |
| 4279 |
return cli_complete_show(a); |
| 4280 |
} |
| 4281 |
|
| 4282 |
if (a->argc > 4) { |
| 4283 |
return CLI_SHOWUSAGE; |
| 4284 |
} else if (a->argc < 4) { |
| 4285 |
cli_show_channels(a); |
| 4286 |
} else { |
| 4287 |
cli_show_channel(a); |
| 4288 |
} |
| 4289 |
|
| 4290 |
return CLI_SUCCESS; |
| 4291 |
} |
| 4292 |
|
| 4293 |
static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4294 |
{ |
| 4295 |
struct cdr_beitem *beitem = NULL; |
| 4296 |
struct module_config *mod_cfg; |
| 4297 |
int cnt = 0; |
| 4298 |
long nextbatchtime = 0; |
| 4299 |
|
| 4300 |
switch (cmd) { |
| 4301 |
case CLI_INIT: |
| 4302 |
e->command = "cdr show status"; |
| 4303 |
e->usage = |
| 4304 |
"Usage: cdr show status\n" |
| 4305 |
" Displays the Call Detail Record engine system status.\n"; |
| 4306 |
return NULL; |
| 4307 |
case CLI_GENERATE: |
| 4308 |
return NULL; |
| 4309 |
} |
| 4310 |
|
| 4311 |
if (a->argc > 3) { |
| 4312 |
return CLI_SHOWUSAGE; |
| 4313 |
} |
| 4314 |
|
| 4315 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 4316 |
if (!mod_cfg) { |
| 4317 |
return CLI_FAILURE; |
| 4318 |
} |
| 4319 |
|
| 4320 |
ast_cli(a->fd, "\n"); |
| 4321 |
ast_cli(a->fd, "Call Detail Record (CDR) settings\n"); |
| 4322 |
ast_cli(a->fd, "----------------------------------\n"); |
| 4323 |
ast_cli(a->fd, " Logging: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED) ? "Enabled" : "Disabled"); |
| 4324 |
ast_cli(a->fd, " Mode: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE) ? "Batch" : "Simple"); |
| 4325 |
if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) { |
| 4326 |
ast_cli(a->fd, " Log calls by default: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_CHANNEL_DEFAULT_ENABLED) ? "Yes" : "No"); |
| 4327 |
ast_cli(a->fd, " Log unanswered calls: %s\n", ast_test_flag(&mod_cfg->general->settings, CDR_UNANSWERED) ? "Yes" : "No"); |
| 4328 |
ast_cli(a->fd, " Log congestion: %s\n\n", ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION) ? "Yes" : "No"); |
| 4329 |
ast_cli(a->fd, " Ignore bridging changes: %s\n\n", ast_test_flag(&mod_cfg->general->settings, CDR_IGNORE_STATE_CHANGES) ? "Yes" : "No"); |
| 4330 |
ast_cli(a->fd, " Ignore dial state changes: %s\n\n", ast_test_flag(&mod_cfg->general->settings, CDR_IGNORE_DIAL_CHANGES) ? "Yes" : "No"); |
| 4331 |
ast_cli(a->fd, " Cancel disposition enabled: %s\n\n", ast_test_flag(&mod_cfg->general->settings, CDR_CANCEL_DISPOSITION_ENABLED) ? "Yes" : "No"); |
| 4332 |
if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4333 |
ast_cli(a->fd, "* Batch Mode Settings\n"); |
| 4334 |
ast_cli(a->fd, " -------------------\n"); |
| 4335 |
if (batch) |
| 4336 |
cnt = batch->size; |
| 4337 |
if (cdr_sched > -1) |
| 4338 |
nextbatchtime = ast_sched_when(sched, cdr_sched); |
| 4339 |
ast_cli(a->fd, " Safe shutdown: %s\n", ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SAFE_SHUTDOWN) ? "Enabled" : "Disabled"); |
| 4340 |
ast_cli(a->fd, " Threading model: %s\n", ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SCHEDULER_ONLY) ? "Scheduler only" : "Scheduler plus separate threads"); |
| 4341 |
ast_cli(a->fd, " Current batch size: %d record%s\n", cnt, ESS(cnt)); |
| 4342 |
ast_cli(a->fd, " Maximum batch size: %u record%s\n", mod_cfg->general->batch_settings.size, ESS(mod_cfg->general->batch_settings.size)); |
| 4343 |
ast_cli(a->fd, " Maximum batch time: %u second%s\n", mod_cfg->general->batch_settings.time, ESS(mod_cfg->general->batch_settings.time)); |
| 4344 |
ast_cli(a->fd, " Next batch processing time: %ld second%s\n\n", nextbatchtime, ESS(nextbatchtime)); |
| 4345 |
} |
| 4346 |
ast_cli(a->fd, "* Registered Backends\n"); |
| 4347 |
ast_cli(a->fd, " -------------------\n"); |
| 4348 |
AST_RWLIST_RDLOCK(&be_list); |
| 4349 |
if (AST_RWLIST_EMPTY(&be_list)) { |
| 4350 |
ast_cli(a->fd, " (none)\n"); |
| 4351 |
} else { |
| 4352 |
AST_RWLIST_TRAVERSE(&be_list, beitem, list) { |
| 4353 |
ast_cli(a->fd, " %s%s\n", beitem->name, beitem->suspended ? " (suspended) " : ""); |
| 4354 |
} |
| 4355 |
} |
| 4356 |
AST_RWLIST_UNLOCK(&be_list); |
| 4357 |
ast_cli(a->fd, "\n"); |
| 4358 |
} |
| 4359 |
|
| 4360 |
ao2_cleanup(mod_cfg); |
| 4361 |
return CLI_SUCCESS; |
| 4362 |
} |
| 4363 |
|
| 4364 |
static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4365 |
{ |
| 4366 |
struct module_config *mod_cfg; |
| 4367 |
|
| 4368 |
switch (cmd) { |
| 4369 |
case CLI_INIT: |
| 4370 |
e->command = "cdr submit"; |
| 4371 |
e->usage = |
| 4372 |
"Usage: cdr submit\n" |
| 4373 |
"Posts all pending batched CDR data to the configured CDR\n" |
| 4374 |
"backend engine modules.\n"; |
| 4375 |
return NULL; |
| 4376 |
case CLI_GENERATE: |
| 4377 |
return NULL; |
| 4378 |
} |
| 4379 |
if (a->argc > 2) { |
| 4380 |
return CLI_SHOWUSAGE; |
| 4381 |
} |
| 4382 |
|
| 4383 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 4384 |
if (!mod_cfg) { |
| 4385 |
return CLI_FAILURE; |
| 4386 |
} |
| 4387 |
|
| 4388 |
if (!ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) { |
| 4389 |
ast_cli(a->fd, "Cannot submit CDR batch: CDR engine disabled.\n"); |
| 4390 |
} else if (!ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4391 |
ast_cli(a->fd, "Cannot submit CDR batch: batch mode not enabled.\n"); |
| 4392 |
} else { |
| 4393 |
start_batch_mode(); |
| 4394 |
ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n"); |
| 4395 |
} |
| 4396 |
ao2_cleanup(mod_cfg); |
| 4397 |
|
| 4398 |
return CLI_SUCCESS; |
| 4399 |
} |
| 4400 |
|
| 4401 |
static struct ast_cli_entry cli_commands[] = { |
| 4402 |
AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data"), |
| 4403 |
AST_CLI_DEFINE(handle_cli_status, "Display the CDR status"), |
| 4404 |
AST_CLI_DEFINE(handle_cli_show, "Display active CDRs for channels"), |
| 4405 |
AST_CLI_DEFINE(handle_cli_debug, "Enable debugging in the CDR engine"), |
| 4406 |
}; |
| 4407 |
|
| 4408 |
|
| 4409 |
|
| 4410 |
|
| 4411 |
|
| 4412 |
static int cdr_object_dispatch_all_cb(void *obj, void *arg, int flags) |
| 4413 |
{ |
| 4414 |
struct cdr_object *cdr = obj; |
| 4415 |
struct cdr_object *it_cdr; |
| 4416 |
|
| 4417 |
ao2_lock(cdr); |
| 4418 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 4419 |
cdr_object_transition_state(it_cdr, &finalized_state_fn_table); |
| 4420 |
} |
| 4421 |
cdr_object_dispatch(cdr); |
| 4422 |
ao2_unlock(cdr); |
| 4423 |
|
| 4424 |
cdr_all_unlink(cdr); |
| 4425 |
|
| 4426 |
return CMP_MATCH; |
| 4427 |
} |
| 4428 |
|
| 4429 |
static void finalize_batch_mode(void) |
| 4430 |
{ |
| 4431 |
if (cdr_thread == AST_PTHREADT_NULL) { |
| 4432 |
return; |
| 4433 |
} |
| 4434 |
|
| 4435 |
pthread_cancel(cdr_thread); |
| 4436 |
pthread_kill(cdr_thread, SIGURG); |
| 4437 |
pthread_join(cdr_thread, NULL); |
| 4438 |
cdr_thread = AST_PTHREADT_NULL; |
| 4439 |
ast_cond_destroy(&cdr_pending_cond); |
| 4440 |
ast_cdr_engine_term(); |
| 4441 |
} |
| 4442 |
|
| 4443 |
struct stasis_message_router *ast_cdr_message_router(void) |
| 4444 |
{ |
| 4445 |
if (!stasis_router) { |
| 4446 |
return NULL; |
| 4447 |
} |
| 4448 |
|
| 4449 |
ao2_bump(stasis_router); |
| 4450 |
return stasis_router; |
| 4451 |
} |
| 4452 |
|
| 4453 |
|
| 4454 |
|
| 4455 |
|
| 4456 |
static void destroy_subscriptions(void) |
| 4457 |
{ |
| 4458 |
channel_subscription = stasis_forward_cancel(channel_subscription); |
| 4459 |
bridge_subscription = stasis_forward_cancel(bridge_subscription); |
| 4460 |
parking_subscription = stasis_forward_cancel(parking_subscription); |
| 4461 |
} |
| 4462 |
|
| 4463 |
|
| 4464 |
|
| 4465 |
|
| 4466 |
static int create_subscriptions(void) |
| 4467 |
{ |
| 4468 |
if (!cdr_topic) { |
| 4469 |
return -1; |
| 4470 |
} |
| 4471 |
|
| 4472 |
if (channel_subscription || bridge_subscription || parking_subscription) { |
| 4473 |
return 0; |
| 4474 |
} |
| 4475 |
|
| 4476 |
channel_subscription = stasis_forward_all(ast_channel_topic_all(), cdr_topic); |
| 4477 |
if (!channel_subscription) { |
| 4478 |
return -1; |
| 4479 |
} |
| 4480 |
bridge_subscription = stasis_forward_all(ast_bridge_topic_all(), cdr_topic); |
| 4481 |
if (!bridge_subscription) { |
| 4482 |
return -1; |
| 4483 |
} |
| 4484 |
parking_subscription = stasis_forward_all(ast_parking_topic(), cdr_topic); |
| 4485 |
if (!parking_subscription) { |
| 4486 |
return -1; |
| 4487 |
} |
| 4488 |
|
| 4489 |
return 0; |
| 4490 |
} |
| 4491 |
|
| 4492 |
static int process_config(int reload) |
| 4493 |
{ |
| 4494 |
if (!reload) { |
| 4495 |
if (aco_info_init(&cfg_info)) { |
| 4496 |
return 1; |
| 4497 |
} |
| 4498 |
|
| 4499 |
aco_option_register(&cfg_info, "enable", ACO_EXACT, general_options, DEFAULT_ENABLED, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_ENABLED); |
| 4500 |
aco_option_register(&cfg_info, "debug", ACO_EXACT, general_options, 0, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_DEBUG); |
| 4501 |
aco_option_register(&cfg_info, "unanswered", ACO_EXACT, general_options, DEFAULT_UNANSWERED, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_UNANSWERED); |
| 4502 |
aco_option_register(&cfg_info, "congestion", ACO_EXACT, general_options, 0, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_CONGESTION); |
| 4503 |
aco_option_register(&cfg_info, "batch", ACO_EXACT, general_options, DEFAULT_BATCHMODE, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_BATCHMODE); |
| 4504 |
aco_option_register(&cfg_info, "endbeforehexten", ACO_EXACT, general_options, DEFAULT_END_BEFORE_H_EXTEN, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_END_BEFORE_H_EXTEN); |
| 4505 |
aco_option_register(&cfg_info, "initiatedseconds", ACO_EXACT, general_options, DEFAULT_INITIATED_SECONDS, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_INITIATED_SECONDS); |
| 4506 |
aco_option_register(&cfg_info, "scheduleronly", ACO_EXACT, general_options, DEFAULT_BATCH_SCHEDULER_ONLY, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, batch_settings.settings), BATCH_MODE_SCHEDULER_ONLY); |
| 4507 |
aco_option_register(&cfg_info, "safeshutdown", ACO_EXACT, general_options, DEFAULT_BATCH_SAFE_SHUTDOWN, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, batch_settings.settings), BATCH_MODE_SAFE_SHUTDOWN); |
| 4508 |
aco_option_register(&cfg_info, "size", ACO_EXACT, general_options, DEFAULT_BATCH_SIZE, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_cdr_config, batch_settings.size), 0, MAX_BATCH_SIZE); |
| 4509 |
aco_option_register(&cfg_info, "time", ACO_EXACT, general_options, DEFAULT_BATCH_TIME, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_cdr_config, batch_settings.time), 1, MAX_BATCH_TIME); |
| 4510 |
aco_option_register(&cfg_info, "channeldefaultenabled", ACO_EXACT, general_options, DEFAULT_CHANNEL_ENABLED, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_CHANNEL_DEFAULT_ENABLED); |
| 4511 |
aco_option_register(&cfg_info, "ignorestatechanges", ACO_EXACT, general_options, DEFAULT_IGNORE_STATE_CHANGES, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_IGNORE_STATE_CHANGES); |
| 4512 |
aco_option_register(&cfg_info, "ignoredialchanges", ACO_EXACT, general_options, DEFAULT_IGNORE_DIAL_CHANGES, OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_IGNORE_DIAL_CHANGES); |
| 4513 |
aco_option_register(&cfg_info, "canceldispositionenabled", ACO_EXACT, general_options, "0", OPT_BOOLFLAG_T, 1, FLDSET(struct ast_cdr_config, settings), CDR_CANCEL_DISPOSITION_ENABLED); |
| 4514 |
} |
| 4515 |
|
| 4516 |
if (aco_process_config(&cfg_info, reload) == ACO_PROCESS_ERROR) { |
| 4517 |
struct module_config *mod_cfg; |
| 4518 |
|
| 4519 |
if (reload) { |
| 4520 |
return 1; |
| 4521 |
} |
| 4522 |
|
| 4523 |
|
| 4524 |
|
| 4525 |
|
| 4526 |
mod_cfg = module_config_alloc(); |
| 4527 |
if (!mod_cfg |
| 4528 |
|| aco_set_defaults(&general_option, "general", mod_cfg->general)) { |
| 4529 |
ao2_cleanup(mod_cfg); |
| 4530 |
return 1; |
| 4531 |
} |
| 4532 |
ast_log(LOG_NOTICE, "Failed to process CDR configuration; using defaults\n"); |
| 4533 |
ao2_global_obj_replace_unref(module_configs, mod_cfg); |
| 4534 |
cdr_set_debug_mode(mod_cfg); |
| 4535 |
ao2_cleanup(mod_cfg); |
| 4536 |
} |
| 4537 |
|
| 4538 |
return 0; |
| 4539 |
} |
| 4540 |
|
| 4541 |
static void cdr_engine_shutdown(void) |
| 4542 |
{ |
| 4543 |
stasis_message_router_unsubscribe_and_join(stasis_router); |
| 4544 |
stasis_router = NULL; |
| 4545 |
|
| 4546 |
ao2_cleanup(cdr_topic); |
| 4547 |
cdr_topic = NULL; |
| 4548 |
|
| 4549 |
STASIS_MESSAGE_TYPE_CLEANUP(cdr_sync_message_type); |
| 4550 |
|
| 4551 |
ao2_callback(active_cdrs_master, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, |
| 4552 |
cdr_object_dispatch_all_cb, NULL); |
| 4553 |
finalize_batch_mode(); |
| 4554 |
ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 4555 |
ast_sched_context_destroy(sched); |
| 4556 |
sched = NULL; |
| 4557 |
ast_free(batch); |
| 4558 |
batch = NULL; |
| 4559 |
|
| 4560 |
aco_info_destroy(&cfg_info); |
| 4561 |
ao2_global_obj_release(module_configs); |
| 4562 |
|
| 4563 |
ao2_container_unregister("cdrs_master"); |
| 4564 |
ao2_cleanup(active_cdrs_master); |
| 4565 |
active_cdrs_master = NULL; |
| 4566 |
|
| 4567 |
ao2_container_unregister("cdrs_all"); |
| 4568 |
ao2_cleanup(active_cdrs_all); |
| 4569 |
active_cdrs_all = NULL; |
| 4570 |
} |
| 4571 |
|
| 4572 |
static void cdr_enable_batch_mode(struct ast_cdr_config *config) |
| 4573 |
{ |
| 4574 |
|
| 4575 |
if (cdr_thread == AST_PTHREADT_NULL) { |
| 4576 |
ast_cond_init(&cdr_pending_cond, NULL); |
| 4577 |
if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) { |
| 4578 |
ast_log(LOG_ERROR, "Unable to start CDR thread.\n"); |
| 4579 |
return; |
| 4580 |
} |
| 4581 |
} |
| 4582 |
|
| 4583 |
|
| 4584 |
start_batch_mode(); |
| 4585 |
|
| 4586 |
ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %u or time %u seconds.\n", |
| 4587 |
config->batch_settings.size, config->batch_settings.time); |
| 4588 |
} |
| 4589 |
|
| 4590 |
|
| 4591 |
|
| 4592 |
|
| 4593 |
|
| 4594 |
|
| 4595 |
|
| 4596 |
|
| 4597 |
|
| 4598 |
|
| 4599 |
static void cdr_master_print_fn(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 4600 |
{ |
| 4601 |
struct cdr_object *cdr = v_obj; |
| 4602 |
struct cdr_object *it_cdr; |
| 4603 |
|
| 4604 |
if (!cdr) { |
| 4605 |
return; |
| 4606 |
} |
| 4607 |
for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) { |
| 4608 |
prnt(where, "Party A: %s; Party B: %s; Bridge %s\n", |
| 4609 |
it_cdr->party_a.snapshot->base->name, |
| 4610 |
it_cdr->party_b.snapshot ? it_cdr->party_b.snapshot->base->name : "<unknown>", |
| 4611 |
it_cdr->bridge); |
| 4612 |
} |
| 4613 |
} |
| 4614 |
|
| 4615 |
|
| 4616 |
|
| 4617 |
|
| 4618 |
|
| 4619 |
|
| 4620 |
|
| 4621 |
|
| 4622 |
|
| 4623 |
|
| 4624 |
static void cdr_all_print_fn(void *v_obj, void *where, ao2_prnt_fn *prnt) |
| 4625 |
{ |
| 4626 |
struct cdr_object *cdr = v_obj; |
| 4627 |
|
| 4628 |
if (!cdr) { |
| 4629 |
return; |
| 4630 |
} |
| 4631 |
prnt(where, "Party A: %s; Party B: %s; Bridge %s", |
| 4632 |
cdr->party_a.snapshot->base->name, |
| 4633 |
cdr->party_b.snapshot ? cdr->party_b.snapshot->base->name : "<unknown>", |
| 4634 |
cdr->bridge); |
| 4635 |
} |
| 4636 |
|
| 4637 |
|
| 4638 |
|
| 4639 |
|
| 4640 |
static int cdr_toggle_runtime_options(void) |
| 4641 |
{ |
| 4642 |
struct module_config *mod_cfg; |
| 4643 |
|
| 4644 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 4645 |
if (mod_cfg |
| 4646 |
&& ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) { |
| 4647 |
if (create_subscriptions()) { |
| 4648 |
destroy_subscriptions(); |
| 4649 |
ast_log(AST_LOG_ERROR, "Failed to create Stasis subscriptions\n"); |
| 4650 |
ao2_cleanup(mod_cfg); |
| 4651 |
return -1; |
| 4652 |
} |
| 4653 |
if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4654 |
cdr_enable_batch_mode(mod_cfg->general); |
| 4655 |
} else { |
| 4656 |
ast_log(LOG_NOTICE, "CDR simple logging enabled.\n"); |
| 4657 |
} |
| 4658 |
} else { |
| 4659 |
destroy_subscriptions(); |
| 4660 |
ast_log(LOG_NOTICE, "CDR logging disabled.\n"); |
| 4661 |
} |
| 4662 |
ao2_cleanup(mod_cfg); |
| 4663 |
|
| 4664 |
return mod_cfg ? 0 : -1; |
| 4665 |
} |
| 4666 |
|
| 4667 |
static int unload_module(void) |
| 4668 |
{ |
| 4669 |
destroy_subscriptions(); |
| 4670 |
|
| 4671 |
return 0; |
| 4672 |
} |
| 4673 |
|
| 4674 |
static int load_module(void) |
| 4675 |
{ |
| 4676 |
struct module_config *mod_cfg = NULL; |
| 4677 |
if (process_config(0)) { |
| 4678 |
return AST_MODULE_LOAD_FAILURE; |
| 4679 |
} |
| 4680 |
|
| 4681 |
cdr_topic = stasis_topic_create("cdr:aggregator"); |
| 4682 |
if (!cdr_topic) { |
| 4683 |
return AST_MODULE_LOAD_FAILURE; |
| 4684 |
} |
| 4685 |
|
| 4686 |
stasis_router = stasis_message_router_create(cdr_topic); |
| 4687 |
if (!stasis_router) { |
| 4688 |
return AST_MODULE_LOAD_FAILURE; |
| 4689 |
} |
| 4690 |
stasis_message_router_set_congestion_limits(stasis_router, -1, |
| 4691 |
10 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL); |
| 4692 |
|
| 4693 |
if (STASIS_MESSAGE_TYPE_INIT(cdr_sync_message_type)) { |
| 4694 |
return AST_MODULE_LOAD_FAILURE; |
| 4695 |
} |
| 4696 |
|
| 4697 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 4698 |
|
| 4699 |
stasis_message_router_add(stasis_router, ast_channel_snapshot_type(), handle_channel_snapshot_update_message, NULL); |
| 4700 |
|
| 4701 |
|
| 4702 |
stasis_message_router_add(stasis_router, ast_channel_dial_type(), handle_dial_message, NULL); |
| 4703 |
if (!mod_cfg || !ast_test_flag(&mod_cfg->general->settings, CDR_IGNORE_DIAL_CHANGES)) { |
| 4704 |
dial_changes_ignored = 0; |
| 4705 |
} else { |
| 4706 |
dial_changes_ignored = 1; |
| 4707 |
CDR_DEBUG("Dial messages will be mostly ignored\n"); |
| 4708 |
} |
| 4709 |
|
| 4710 |
|
| 4711 |
if (!mod_cfg || !ast_test_flag(&mod_cfg->general->settings, CDR_IGNORE_STATE_CHANGES)) { |
| 4712 |
stasis_message_router_add(stasis_router, ast_channel_entered_bridge_type(), handle_bridge_enter_message, NULL); |
| 4713 |
stasis_message_router_add(stasis_router, ast_channel_left_bridge_type(), handle_bridge_leave_message, NULL); |
| 4714 |
stasis_message_router_add(stasis_router, ast_parked_call_type(), handle_parked_call_message, NULL); |
| 4715 |
} else { |
| 4716 |
CDR_DEBUG("All bridge and parking messages will be ignored\n"); |
| 4717 |
} |
| 4718 |
|
| 4719 |
stasis_message_router_add(stasis_router, cdr_sync_message_type(), handle_cdr_sync_message, NULL); |
| 4720 |
|
| 4721 |
if (mod_cfg) { |
| 4722 |
ao2_cleanup(mod_cfg); |
| 4723 |
} else { |
| 4724 |
ast_log(LOG_WARNING, "Unable to obtain CDR configuration during module load?\n"); |
| 4725 |
} |
| 4726 |
|
| 4727 |
active_cdrs_master = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 4728 |
AST_NUM_CHANNEL_BUCKETS, cdr_master_hash_fn, NULL, cdr_master_cmp_fn); |
| 4729 |
if (!active_cdrs_master) { |
| 4730 |
return AST_MODULE_LOAD_FAILURE; |
| 4731 |
} |
| 4732 |
ao2_container_register("cdrs_master", active_cdrs_master, cdr_master_print_fn); |
| 4733 |
|
| 4734 |
active_cdrs_all = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 4735 |
AST_NUM_CHANNEL_BUCKETS, cdr_all_hash_fn, NULL, cdr_all_cmp_fn); |
| 4736 |
if (!active_cdrs_all) { |
| 4737 |
return AST_MODULE_LOAD_FAILURE; |
| 4738 |
} |
| 4739 |
ao2_container_register("cdrs_all", active_cdrs_all, cdr_all_print_fn); |
| 4740 |
|
| 4741 |
sched = ast_sched_context_create(); |
| 4742 |
if (!sched) { |
| 4743 |
ast_log(LOG_ERROR, "Unable to create schedule context.\n"); |
| 4744 |
return AST_MODULE_LOAD_FAILURE; |
| 4745 |
} |
| 4746 |
|
| 4747 |
ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands)); |
| 4748 |
ast_register_atexit(cdr_engine_shutdown); |
| 4749 |
|
| 4750 |
return cdr_toggle_runtime_options() ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS; |
| 4751 |
} |
| 4752 |
|
| 4753 |
void ast_cdr_engine_term(void) |
| 4754 |
{ |
| 4755 |
RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup); |
| 4756 |
|
| 4757 |
|
| 4758 |
|
| 4759 |
|
| 4760 |
if (!mod_cfg) { |
| 4761 |
return; |
| 4762 |
} |
| 4763 |
|
| 4764 |
if (cdr_sync_message_type()) { |
| 4765 |
void *payload; |
| 4766 |
struct stasis_message *message; |
| 4767 |
|
| 4768 |
if (!stasis_router) { |
| 4769 |
return; |
| 4770 |
} |
| 4771 |
|
| 4772 |
|
| 4773 |
payload = ao2_alloc(sizeof(*payload), NULL); |
| 4774 |
if (!payload) { |
| 4775 |
return; |
| 4776 |
} |
| 4777 |
|
| 4778 |
ast_debug(1, "CDR Engine termination request received; waiting on messages...\n"); |
| 4779 |
|
| 4780 |
message = stasis_message_create(cdr_sync_message_type(), payload); |
| 4781 |
if (message) { |
| 4782 |
stasis_message_router_publish_sync(stasis_router, message); |
| 4783 |
} |
| 4784 |
ao2_cleanup(message); |
| 4785 |
ao2_cleanup(payload); |
| 4786 |
} |
| 4787 |
|
| 4788 |
if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4789 |
cdr_submit_batch(ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SAFE_SHUTDOWN)); |
| 4790 |
} |
| 4791 |
} |
| 4792 |
|
| 4793 |
static int reload_module(void) |
| 4794 |
{ |
| 4795 |
struct module_config *old_mod_cfg; |
| 4796 |
struct module_config *mod_cfg; |
| 4797 |
|
| 4798 |
old_mod_cfg = ao2_global_obj_ref(module_configs); |
| 4799 |
|
| 4800 |
if (!old_mod_cfg || process_config(1)) { |
| 4801 |
ao2_cleanup(old_mod_cfg); |
| 4802 |
return -1; |
| 4803 |
} |
| 4804 |
|
| 4805 |
mod_cfg = ao2_global_obj_ref(module_configs); |
| 4806 |
if (!mod_cfg |
| 4807 |
|| !ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED) |
| 4808 |
|| !ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4809 |
|
| 4810 |
if (ast_test_flag(&old_mod_cfg->general->settings, CDR_BATCHMODE)) { |
| 4811 |
finalize_batch_mode(); |
| 4812 |
} |
| 4813 |
} |
| 4814 |
ao2_cleanup(mod_cfg); |
| 4815 |
|
| 4816 |
ao2_cleanup(old_mod_cfg); |
| 4817 |
return cdr_toggle_runtime_options(); |
| 4818 |
} |
| 4819 |
|
| 4820 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "CDR Engine", |
| 4821 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 4822 |
.load = load_module, |
| 4823 |
.unload = unload_module, |
| 4824 |
.reload = reload_module, |
| 4825 |
.load_pri = AST_MODPRI_CORE, |
| 4826 |
.requires = "extconfig", |
| 4827 |
); |