| 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 |
#include "asterisk.h" |
| 38 |
|
| 39 |
#include "asterisk/astobj2.h" |
| 40 |
#include "asterisk/strings.h" |
| 41 |
#include "asterisk/ccss.h" |
| 42 |
#include "asterisk/channel.h" |
| 43 |
#include "asterisk/pbx.h" |
| 44 |
#include "asterisk/utils.h" |
| 45 |
#include "asterisk/taskprocessor.h" |
| 46 |
#include "asterisk/devicestate.h" |
| 47 |
#include "asterisk/module.h" |
| 48 |
#include "asterisk/app.h" |
| 49 |
#include "asterisk/cli.h" |
| 50 |
#include "asterisk/manager.h" |
| 51 |
#include "asterisk/causes.h" |
| 52 |
#include "asterisk/stasis_system.h" |
| 53 |
#include "asterisk/format_cache.h" |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static int global_enabled; |
| 119 |
|
| 120 |
#define RETURN_IF_NOT_ENABLED(rc) \ |
| 121 |
({ \ |
| 122 |
if (!global_enabled) { \ |
| 123 |
ast_debug(3, "CCSS disabled globally\n"); \ |
| 124 |
return rc; \ |
| 125 |
} \ |
| 126 |
}) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static struct ast_sched_context *cc_sched_context; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static int core_id_counter; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static struct ast_taskprocessor *cc_core_taskprocessor; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
static const char *CC_LOGGER_LEVEL_NAME = "CC"; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static int cc_logger_level; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static unsigned int global_cc_max_requests; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static int cc_request_count; |
| 158 |
|
| 159 |
static inline void *cc_ref(void *obj, const char *debug) |
| 160 |
{ |
| 161 |
ao2_t_ref(obj, +1, debug); |
| 162 |
return obj; |
| 163 |
} |
| 164 |
|
| 165 |
static inline void *cc_unref(void *obj, const char *debug) |
| 166 |
{ |
| 167 |
ao2_t_ref(obj, -1, debug); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
struct ast_cc_config_params { |
| 178 |
enum ast_cc_agent_policies cc_agent_policy; |
| 179 |
enum ast_cc_monitor_policies cc_monitor_policy; |
| 180 |
unsigned int cc_offer_timer; |
| 181 |
unsigned int ccnr_available_timer; |
| 182 |
unsigned int ccbs_available_timer; |
| 183 |
unsigned int cc_recall_timer; |
| 184 |
unsigned int cc_max_agents; |
| 185 |
unsigned int cc_max_monitors; |
| 186 |
char cc_callback_sub[AST_MAX_EXTENSION]; |
| 187 |
char cc_agent_dialstring[AST_MAX_EXTENSION]; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
enum cc_state { |
| 197 |
|
| 198 |
CC_AVAILABLE, |
| 199 |
|
| 200 |
CC_CALLER_OFFERED, |
| 201 |
|
| 202 |
|
| 203 |
CC_CALLER_REQUESTED, |
| 204 |
|
| 205 |
|
| 206 |
CC_ACTIVE, |
| 207 |
|
| 208 |
|
| 209 |
CC_CALLEE_READY, |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
CC_CALLER_BUSY, |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
CC_RECALLING, |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
CC_COMPLETE, |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
CC_FAILED, |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
struct cc_control_payload { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
const char *monitor_type; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
void *private_data; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
enum ast_cc_service_type service; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
struct ast_cc_config_params config_params; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
int parent_interface_id; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
char device_name[AST_CHANNEL_NAME]; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
char dialstring[AST_CHANNEL_NAME]; |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
AST_LIST_HEAD(cc_monitor_tree, ast_cc_monitor); |
| 340 |
|
| 341 |
static const int CC_CORE_INSTANCES_BUCKETS = 17; |
| 342 |
static struct ao2_container *cc_core_instances; |
| 343 |
|
| 344 |
struct cc_core_instance { |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
int core_id; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
enum cc_state current_state; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
struct ast_cc_agent *agent; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
struct cc_monitor_tree *monitors; |
| 361 |
}; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
static int __attribute__((format(printf, 3, 0))) cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan, |
| 396 |
struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data); |
| 397 |
|
| 398 |
static const struct { |
| 399 |
enum ast_cc_service_type service; |
| 400 |
const char *service_string; |
| 401 |
} cc_service_to_string_map[] = { |
| 402 |
{AST_CC_NONE, "NONE"}, |
| 403 |
{AST_CC_CCBS, "CCBS"}, |
| 404 |
{AST_CC_CCNR, "CCNR"}, |
| 405 |
{AST_CC_CCNL, "CCNL"}, |
| 406 |
}; |
| 407 |
|
| 408 |
static const struct { |
| 409 |
enum cc_state state; |
| 410 |
const char *state_string; |
| 411 |
} cc_state_to_string_map[] = { |
| 412 |
{CC_AVAILABLE, "CC is available"}, |
| 413 |
{CC_CALLER_OFFERED, "CC offered to caller"}, |
| 414 |
{CC_CALLER_REQUESTED, "CC requested by caller"}, |
| 415 |
{CC_ACTIVE, "CC accepted by callee"}, |
| 416 |
{CC_CALLEE_READY, "Callee has become available"}, |
| 417 |
{CC_CALLER_BUSY, "Callee was ready, but caller is now unavailable"}, |
| 418 |
{CC_RECALLING, "Caller is attempting to recall"}, |
| 419 |
{CC_COMPLETE, "Recall complete"}, |
| 420 |
{CC_FAILED, "CC has failed"}, |
| 421 |
}; |
| 422 |
|
| 423 |
static const char *cc_state_to_string(enum cc_state state) |
| 424 |
{ |
| 425 |
return cc_state_to_string_map[state].state_string; |
| 426 |
} |
| 427 |
|
| 428 |
static const char *cc_service_to_string(enum ast_cc_service_type service) |
| 429 |
{ |
| 430 |
return cc_service_to_string_map[service].service_string; |
| 431 |
} |
| 432 |
|
| 433 |
static int cc_core_instance_hash_fn(const void *obj, const int flags) |
| 434 |
{ |
| 435 |
const struct cc_core_instance *core_instance = obj; |
| 436 |
return core_instance->core_id; |
| 437 |
} |
| 438 |
|
| 439 |
static int cc_core_instance_cmp_fn(void *obj, void *arg, int flags) |
| 440 |
{ |
| 441 |
struct cc_core_instance *core_instance1 = obj; |
| 442 |
struct cc_core_instance *core_instance2 = arg; |
| 443 |
|
| 444 |
return core_instance1->core_id == core_instance2->core_id ? CMP_MATCH | CMP_STOP : 0; |
| 445 |
} |
| 446 |
|
| 447 |
static struct cc_core_instance *find_cc_core_instance(const int core_id) |
| 448 |
{ |
| 449 |
struct cc_core_instance finder = {.core_id = core_id,}; |
| 450 |
|
| 451 |
return ao2_t_find(cc_core_instances, &finder, OBJ_POINTER, "Finding a core_instance"); |
| 452 |
} |
| 453 |
|
| 454 |
struct cc_callback_helper { |
| 455 |
ao2_callback_fn *function; |
| 456 |
void *args; |
| 457 |
const char *type; |
| 458 |
}; |
| 459 |
|
| 460 |
static int cc_agent_callback_helper(void *obj, void *args, int flags) |
| 461 |
{ |
| 462 |
struct cc_core_instance *core_instance = obj; |
| 463 |
struct cc_callback_helper *helper = args; |
| 464 |
|
| 465 |
if (strcmp(core_instance->agent->callbacks->type, helper->type)) { |
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
return helper->function(core_instance->agent, helper->args, flags); |
| 470 |
} |
| 471 |
|
| 472 |
struct ast_cc_agent *ast_cc_agent_callback(int flags, ao2_callback_fn *function, void *args, const char * const type) |
| 473 |
{ |
| 474 |
struct cc_callback_helper helper = {.function = function, .args = args, .type = type}; |
| 475 |
struct cc_core_instance *core_instance; |
| 476 |
|
| 477 |
RETURN_IF_NOT_ENABLED(NULL); |
| 478 |
|
| 479 |
if ((core_instance = ao2_t_callback(cc_core_instances, flags, cc_agent_callback_helper, &helper, |
| 480 |
"Calling provided agent callback function"))) { |
| 481 |
struct ast_cc_agent *agent = cc_ref(core_instance->agent, "An outside entity needs the agent"); |
| 482 |
cc_unref(core_instance, "agent callback done with the core_instance"); |
| 483 |
return agent; |
| 484 |
} |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
enum match_flags { |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
MATCH_NO_REQUEST = (1 << 0), |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
MATCH_REQUEST = (1 << 1), |
| 497 |
}; |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
static int match_agent(void *obj, void *arg, void *data, int flags) |
| 512 |
{ |
| 513 |
struct cc_core_instance *core_instance = obj; |
| 514 |
const char *name = arg; |
| 515 |
unsigned long match_flags = *(unsigned long *)data; |
| 516 |
int possible_match = 0; |
| 517 |
|
| 518 |
if ((match_flags & MATCH_NO_REQUEST) && core_instance->current_state < CC_CALLER_REQUESTED) { |
| 519 |
possible_match = 1; |
| 520 |
} |
| 521 |
|
| 522 |
if ((match_flags & MATCH_REQUEST) && core_instance->current_state >= CC_CALLER_REQUESTED) { |
| 523 |
possible_match = 1; |
| 524 |
} |
| 525 |
|
| 526 |
if (!possible_match) { |
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
if (!strcmp(core_instance->agent->device_name, name)) { |
| 531 |
return CMP_MATCH | CMP_STOP; |
| 532 |
} |
| 533 |
return 0; |
| 534 |
} |
| 535 |
|
| 536 |
struct count_agents_cb_data { |
| 537 |
int count; |
| 538 |
int core_id_exception; |
| 539 |
}; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
static int count_agents_cb(void *obj, void *arg, void *data, int flags) |
| 550 |
{ |
| 551 |
struct cc_core_instance *core_instance = obj; |
| 552 |
const char *name = arg; |
| 553 |
struct count_agents_cb_data *cb_data = data; |
| 554 |
|
| 555 |
if (cb_data->core_id_exception == core_instance->core_id) { |
| 556 |
ast_log_dynamic_level(cc_logger_level, "Found agent with core_id %d but not counting it toward total\n", core_instance->core_id); |
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
if (core_instance->current_state >= CC_CALLER_REQUESTED && !strcmp(core_instance->agent->device_name, name)) { |
| 561 |
cb_data->count++; |
| 562 |
} |
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
#define CC_AVAILABLE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE |
| 569 |
#define CC_CALLER_OFFERED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE |
| 570 |
#define CC_CALLER_REQUESTED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE |
| 571 |
#define CC_ACTIVE_DEVSTATE_DEFAULT AST_DEVICE_INUSE |
| 572 |
#define CC_CALLEE_READY_DEVSTATE_DEFAULT AST_DEVICE_RINGING |
| 573 |
#define CC_CALLER_BUSY_DEVSTATE_DEFAULT AST_DEVICE_ONHOLD |
| 574 |
#define CC_RECALLING_DEVSTATE_DEFAULT AST_DEVICE_RINGING |
| 575 |
#define CC_COMPLETE_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE |
| 576 |
#define CC_FAILED_DEVSTATE_DEFAULT AST_DEVICE_NOT_INUSE |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
static enum ast_device_state cc_state_to_devstate_map[] = { |
| 583 |
[CC_AVAILABLE] = CC_AVAILABLE_DEVSTATE_DEFAULT, |
| 584 |
[CC_CALLER_OFFERED] = CC_CALLER_OFFERED_DEVSTATE_DEFAULT, |
| 585 |
[CC_CALLER_REQUESTED] = CC_CALLER_REQUESTED_DEVSTATE_DEFAULT, |
| 586 |
[CC_ACTIVE] = CC_ACTIVE_DEVSTATE_DEFAULT, |
| 587 |
[CC_CALLEE_READY] = CC_CALLEE_READY_DEVSTATE_DEFAULT, |
| 588 |
[CC_CALLER_BUSY] = CC_CALLER_BUSY_DEVSTATE_DEFAULT, |
| 589 |
[CC_RECALLING] = CC_RECALLING_DEVSTATE_DEFAULT, |
| 590 |
[CC_COMPLETE] = CC_COMPLETE_DEVSTATE_DEFAULT, |
| 591 |
[CC_FAILED] = CC_FAILED_DEVSTATE_DEFAULT, |
| 592 |
}; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
static enum ast_device_state cc_state_to_devstate(enum cc_state state) |
| 604 |
{ |
| 605 |
return cc_state_to_devstate_map[state]; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
static enum ast_device_state ccss_device_state(const char *device_name) |
| 620 |
{ |
| 621 |
struct cc_core_instance *core_instance; |
| 622 |
unsigned long match_flags; |
| 623 |
enum ast_device_state cc_current_state; |
| 624 |
|
| 625 |
match_flags = MATCH_NO_REQUEST; |
| 626 |
core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, |
| 627 |
(char *) device_name, &match_flags, |
| 628 |
"Find Core Instance for ccss_device_state request."); |
| 629 |
if (!core_instance) { |
| 630 |
ast_log_dynamic_level(cc_logger_level, |
| 631 |
"Couldn't find a core instance for caller %s\n", device_name); |
| 632 |
return cc_state_to_devstate(CC_FAILED); |
| 633 |
} |
| 634 |
|
| 635 |
ast_log_dynamic_level(cc_logger_level, |
| 636 |
"Core %d: Found core_instance for caller %s in state %s\n", |
| 637 |
core_instance->core_id, device_name, cc_state_to_string(core_instance->current_state)); |
| 638 |
|
| 639 |
if (strcmp(core_instance->agent->callbacks->type, "generic")) { |
| 640 |
ast_log_dynamic_level(cc_logger_level, |
| 641 |
"Core %d: Device State is only for generic agent types.\n", |
| 642 |
core_instance->core_id); |
| 643 |
cc_unref(core_instance, "Unref core_instance since ccss_device_state was called with native agent"); |
| 644 |
return cc_state_to_devstate(CC_FAILED); |
| 645 |
} |
| 646 |
cc_current_state = cc_state_to_devstate(core_instance->current_state); |
| 647 |
cc_unref(core_instance, "Unref core_instance done with ccss_device_state"); |
| 648 |
return cc_current_state; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
static void ccss_notify_device_state_change(const char *device, enum cc_state state) |
| 661 |
{ |
| 662 |
enum ast_device_state devstate; |
| 663 |
|
| 664 |
devstate = cc_state_to_devstate(state); |
| 665 |
|
| 666 |
ast_log_dynamic_level(cc_logger_level, |
| 667 |
"Notification of CCSS state change to '%s', device state '%s' for device '%s'\n", |
| 668 |
cc_state_to_string(state), ast_devstate2str(devstate), device); |
| 669 |
|
| 670 |
ast_devstate_changed(devstate, AST_DEVSTATE_CACHABLE, "ccss:%s", device); |
| 671 |
} |
| 672 |
|
| 673 |
#define CC_OFFER_TIMER_DEFAULT 20 |
| 674 |
#define CCNR_AVAILABLE_TIMER_DEFAULT 7200 |
| 675 |
#define CCBS_AVAILABLE_TIMER_DEFAULT 4800 |
| 676 |
#define CC_RECALL_TIMER_DEFAULT 20 |
| 677 |
#define CC_MAX_AGENTS_DEFAULT 5 |
| 678 |
#define CC_MAX_MONITORS_DEFAULT 5 |
| 679 |
#define GLOBAL_CC_MAX_REQUESTS_DEFAULT 20 |
| 680 |
|
| 681 |
static const struct ast_cc_config_params cc_default_params = { |
| 682 |
.cc_agent_policy = AST_CC_AGENT_NEVER, |
| 683 |
.cc_monitor_policy = AST_CC_MONITOR_NEVER, |
| 684 |
.cc_offer_timer = CC_OFFER_TIMER_DEFAULT, |
| 685 |
.ccnr_available_timer = CCNR_AVAILABLE_TIMER_DEFAULT, |
| 686 |
.ccbs_available_timer = CCBS_AVAILABLE_TIMER_DEFAULT, |
| 687 |
.cc_recall_timer = CC_RECALL_TIMER_DEFAULT, |
| 688 |
.cc_max_agents = CC_MAX_AGENTS_DEFAULT, |
| 689 |
.cc_max_monitors = CC_MAX_MONITORS_DEFAULT, |
| 690 |
.cc_callback_sub = "", |
| 691 |
.cc_agent_dialstring = "", |
| 692 |
}; |
| 693 |
|
| 694 |
void ast_cc_default_config_params(struct ast_cc_config_params *params) |
| 695 |
{ |
| 696 |
*params = cc_default_params; |
| 697 |
} |
| 698 |
|
| 699 |
struct ast_cc_config_params *__ast_cc_config_params_init(const char *file, int line, const char *function) |
| 700 |
{ |
| 701 |
struct ast_cc_config_params *params; |
| 702 |
|
| 703 |
RETURN_IF_NOT_ENABLED(NULL); |
| 704 |
|
| 705 |
params = __ast_malloc(sizeof(*params), file, line, function); |
| 706 |
if (!params) { |
| 707 |
return NULL; |
| 708 |
} |
| 709 |
|
| 710 |
ast_cc_default_config_params(params); |
| 711 |
return params; |
| 712 |
} |
| 713 |
|
| 714 |
void ast_cc_config_params_destroy(struct ast_cc_config_params *params) |
| 715 |
{ |
| 716 |
ast_free(params); |
| 717 |
} |
| 718 |
|
| 719 |
static enum ast_cc_agent_policies str_to_agent_policy(const char * const value) |
| 720 |
{ |
| 721 |
if (!strcasecmp(value, "never")) { |
| 722 |
return AST_CC_AGENT_NEVER; |
| 723 |
} else if (!strcasecmp(value, "native")) { |
| 724 |
return AST_CC_AGENT_NATIVE; |
| 725 |
} else if (!strcasecmp(value, "generic")) { |
| 726 |
return AST_CC_AGENT_GENERIC; |
| 727 |
} else { |
| 728 |
ast_log(LOG_WARNING, "%s is an invalid value for cc_agent_policy. Switching to 'never'\n", value); |
| 729 |
return AST_CC_AGENT_NEVER; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
static enum ast_cc_monitor_policies str_to_monitor_policy(const char * const value) |
| 734 |
{ |
| 735 |
if (!strcasecmp(value, "never")) { |
| 736 |
return AST_CC_MONITOR_NEVER; |
| 737 |
} else if (!strcasecmp(value, "native")) { |
| 738 |
return AST_CC_MONITOR_NATIVE; |
| 739 |
} else if (!strcasecmp(value, "generic")) { |
| 740 |
return AST_CC_MONITOR_GENERIC; |
| 741 |
} else if (!strcasecmp(value, "always")) { |
| 742 |
return AST_CC_MONITOR_ALWAYS; |
| 743 |
} else { |
| 744 |
ast_log(LOG_WARNING, "%s is an invalid value for cc_monitor_policy. Switching to 'never'\n", value); |
| 745 |
return AST_CC_MONITOR_NEVER; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
static const char *agent_policy_to_str(enum ast_cc_agent_policies policy) |
| 750 |
{ |
| 751 |
switch (policy) { |
| 752 |
case AST_CC_AGENT_NEVER: |
| 753 |
return "never"; |
| 754 |
case AST_CC_AGENT_NATIVE: |
| 755 |
return "native"; |
| 756 |
case AST_CC_AGENT_GENERIC: |
| 757 |
return "generic"; |
| 758 |
default: |
| 759 |
|
| 760 |
return ""; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
static const char *monitor_policy_to_str(enum ast_cc_monitor_policies policy) |
| 765 |
{ |
| 766 |
switch (policy) { |
| 767 |
case AST_CC_MONITOR_NEVER: |
| 768 |
return "never"; |
| 769 |
case AST_CC_MONITOR_NATIVE: |
| 770 |
return "native"; |
| 771 |
case AST_CC_MONITOR_GENERIC: |
| 772 |
return "generic"; |
| 773 |
case AST_CC_MONITOR_ALWAYS: |
| 774 |
return "always"; |
| 775 |
default: |
| 776 |
|
| 777 |
return ""; |
| 778 |
} |
| 779 |
} |
| 780 |
int ast_cc_get_param(struct ast_cc_config_params *params, const char * const name, |
| 781 |
char *buf, size_t buf_len) |
| 782 |
{ |
| 783 |
const char *value = NULL; |
| 784 |
|
| 785 |
if (!strcasecmp(name, "cc_callback_sub")) { |
| 786 |
value = ast_get_cc_callback_sub(params); |
| 787 |
} else if (!strcasecmp(name, "cc_agent_policy")) { |
| 788 |
value = agent_policy_to_str(ast_get_cc_agent_policy(params)); |
| 789 |
} else if (!strcasecmp(name, "cc_monitor_policy")) { |
| 790 |
value = monitor_policy_to_str(ast_get_cc_monitor_policy(params)); |
| 791 |
} else if (!strcasecmp(name, "cc_agent_dialstring")) { |
| 792 |
value = ast_get_cc_agent_dialstring(params); |
| 793 |
} |
| 794 |
if (value) { |
| 795 |
ast_copy_string(buf, value, buf_len); |
| 796 |
return 0; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
if (!strcasecmp(name, "cc_offer_timer")) { |
| 804 |
snprintf(buf, buf_len, "%u", ast_get_cc_offer_timer(params)); |
| 805 |
} else if (!strcasecmp(name, "ccnr_available_timer")) { |
| 806 |
snprintf(buf, buf_len, "%u", ast_get_ccnr_available_timer(params)); |
| 807 |
} else if (!strcasecmp(name, "ccbs_available_timer")) { |
| 808 |
snprintf(buf, buf_len, "%u", ast_get_ccbs_available_timer(params)); |
| 809 |
} else if (!strcasecmp(name, "cc_max_agents")) { |
| 810 |
snprintf(buf, buf_len, "%u", ast_get_cc_max_agents(params)); |
| 811 |
} else if (!strcasecmp(name, "cc_max_monitors")) { |
| 812 |
snprintf(buf, buf_len, "%u", ast_get_cc_max_monitors(params)); |
| 813 |
} else if (!strcasecmp(name, "cc_recall_timer")) { |
| 814 |
snprintf(buf, buf_len, "%u", ast_get_cc_recall_timer(params)); |
| 815 |
} else { |
| 816 |
ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name); |
| 817 |
return -1; |
| 818 |
} |
| 819 |
|
| 820 |
return 0; |
| 821 |
} |
| 822 |
|
| 823 |
int ast_cc_set_param(struct ast_cc_config_params *params, const char * const name, |
| 824 |
const char * const value) |
| 825 |
{ |
| 826 |
unsigned int value_as_uint; |
| 827 |
if (!strcasecmp(name, "cc_agent_policy")) { |
| 828 |
return ast_set_cc_agent_policy(params, str_to_agent_policy(value)); |
| 829 |
} else if (!strcasecmp(name, "cc_monitor_policy")) { |
| 830 |
return ast_set_cc_monitor_policy(params, str_to_monitor_policy(value)); |
| 831 |
} else if (!strcasecmp(name, "cc_agent_dialstring")) { |
| 832 |
ast_set_cc_agent_dialstring(params, value); |
| 833 |
} else if (!strcasecmp(name, "cc_callback_sub")) { |
| 834 |
ast_set_cc_callback_sub(params, value); |
| 835 |
return 0; |
| 836 |
} |
| 837 |
|
| 838 |
if (sscanf(value, "%30u", &value_as_uint) != 1) { |
| 839 |
return -1; |
| 840 |
} |
| 841 |
|
| 842 |
if (!strcasecmp(name, "cc_offer_timer")) { |
| 843 |
ast_set_cc_offer_timer(params, value_as_uint); |
| 844 |
} else if (!strcasecmp(name, "ccnr_available_timer")) { |
| 845 |
ast_set_ccnr_available_timer(params, value_as_uint); |
| 846 |
} else if (!strcasecmp(name, "ccbs_available_timer")) { |
| 847 |
ast_set_ccbs_available_timer(params, value_as_uint); |
| 848 |
} else if (!strcasecmp(name, "cc_max_agents")) { |
| 849 |
ast_set_cc_max_agents(params, value_as_uint); |
| 850 |
} else if (!strcasecmp(name, "cc_max_monitors")) { |
| 851 |
ast_set_cc_max_monitors(params, value_as_uint); |
| 852 |
} else if (!strcasecmp(name, "cc_recall_timer")) { |
| 853 |
ast_set_cc_recall_timer(params, value_as_uint); |
| 854 |
} else { |
| 855 |
ast_log(LOG_WARNING, "%s is not a valid CC parameter. Ignoring.\n", name); |
| 856 |
return -1; |
| 857 |
} |
| 858 |
|
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
int ast_cc_is_config_param(const char * const name) |
| 863 |
{ |
| 864 |
return (!strcasecmp(name, "cc_agent_policy") || |
| 865 |
!strcasecmp(name, "cc_monitor_policy") || |
| 866 |
!strcasecmp(name, "cc_offer_timer") || |
| 867 |
!strcasecmp(name, "ccnr_available_timer") || |
| 868 |
!strcasecmp(name, "ccbs_available_timer") || |
| 869 |
!strcasecmp(name, "cc_max_agents") || |
| 870 |
!strcasecmp(name, "cc_max_monitors") || |
| 871 |
!strcasecmp(name, "cc_callback_sub") || |
| 872 |
!strcasecmp(name, "cc_agent_dialstring") || |
| 873 |
!strcasecmp(name, "cc_recall_timer")); |
| 874 |
} |
| 875 |
|
| 876 |
void ast_cc_copy_config_params(struct ast_cc_config_params *dest, const struct ast_cc_config_params *src) |
| 877 |
{ |
| 878 |
if (dest && src) { |
| 879 |
*dest = *src; |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
enum ast_cc_agent_policies ast_get_cc_agent_policy(struct ast_cc_config_params *config) |
| 884 |
{ |
| 885 |
return config->cc_agent_policy; |
| 886 |
} |
| 887 |
|
| 888 |
int ast_set_cc_agent_policy(struct ast_cc_config_params *config, enum ast_cc_agent_policies value) |
| 889 |
{ |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
if (value < AST_CC_AGENT_NEVER || value > AST_CC_AGENT_GENERIC) { |
| 894 |
return -1; |
| 895 |
} |
| 896 |
config->cc_agent_policy = value; |
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
enum ast_cc_monitor_policies ast_get_cc_monitor_policy(struct ast_cc_config_params *config) |
| 901 |
{ |
| 902 |
return config->cc_monitor_policy; |
| 903 |
} |
| 904 |
|
| 905 |
int ast_set_cc_monitor_policy(struct ast_cc_config_params *config, enum ast_cc_monitor_policies value) |
| 906 |
{ |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
if (value < AST_CC_MONITOR_NEVER || value > AST_CC_MONITOR_ALWAYS) { |
| 911 |
return -1; |
| 912 |
} |
| 913 |
config->cc_monitor_policy = value; |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
unsigned int ast_get_cc_offer_timer(struct ast_cc_config_params *config) |
| 918 |
{ |
| 919 |
return config->cc_offer_timer; |
| 920 |
} |
| 921 |
|
| 922 |
void ast_set_cc_offer_timer(struct ast_cc_config_params *config, unsigned int value) |
| 923 |
{ |
| 924 |
|
| 925 |
if (value == 0) { |
| 926 |
ast_log(LOG_WARNING, "0 is an invalid value for cc_offer_timer. Retaining value as %u\n", config->cc_offer_timer); |
| 927 |
return; |
| 928 |
} |
| 929 |
config->cc_offer_timer = value; |
| 930 |
} |
| 931 |
|
| 932 |
unsigned int ast_get_ccnr_available_timer(struct ast_cc_config_params *config) |
| 933 |
{ |
| 934 |
return config->ccnr_available_timer; |
| 935 |
} |
| 936 |
|
| 937 |
void ast_set_ccnr_available_timer(struct ast_cc_config_params *config, unsigned int value) |
| 938 |
{ |
| 939 |
|
| 940 |
if (value == 0) { |
| 941 |
ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->ccnr_available_timer); |
| 942 |
return; |
| 943 |
} |
| 944 |
config->ccnr_available_timer = value; |
| 945 |
} |
| 946 |
|
| 947 |
unsigned int ast_get_cc_recall_timer(struct ast_cc_config_params *config) |
| 948 |
{ |
| 949 |
return config->cc_recall_timer; |
| 950 |
} |
| 951 |
|
| 952 |
void ast_set_cc_recall_timer(struct ast_cc_config_params *config, unsigned int value) |
| 953 |
{ |
| 954 |
|
| 955 |
if (value == 0) { |
| 956 |
ast_log(LOG_WARNING, "0 is an invalid value for ccnr_available_timer. Retaining value as %u\n", config->cc_recall_timer); |
| 957 |
return; |
| 958 |
} |
| 959 |
config->cc_recall_timer = value; |
| 960 |
} |
| 961 |
|
| 962 |
unsigned int ast_get_ccbs_available_timer(struct ast_cc_config_params *config) |
| 963 |
{ |
| 964 |
return config->ccbs_available_timer; |
| 965 |
} |
| 966 |
|
| 967 |
void ast_set_ccbs_available_timer(struct ast_cc_config_params *config, unsigned int value) |
| 968 |
{ |
| 969 |
|
| 970 |
if (value == 0) { |
| 971 |
ast_log(LOG_WARNING, "0 is an invalid value for ccbs_available_timer. Retaining value as %u\n", config->ccbs_available_timer); |
| 972 |
return; |
| 973 |
} |
| 974 |
config->ccbs_available_timer = value; |
| 975 |
} |
| 976 |
|
| 977 |
const char *ast_get_cc_agent_dialstring(struct ast_cc_config_params *config) |
| 978 |
{ |
| 979 |
return config->cc_agent_dialstring; |
| 980 |
} |
| 981 |
|
| 982 |
void ast_set_cc_agent_dialstring(struct ast_cc_config_params *config, const char *const value) |
| 983 |
{ |
| 984 |
if (ast_strlen_zero(value)) { |
| 985 |
config->cc_agent_dialstring[0] = '\0'; |
| 986 |
} else { |
| 987 |
ast_copy_string(config->cc_agent_dialstring, value, sizeof(config->cc_agent_dialstring)); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
unsigned int ast_get_cc_max_agents(struct ast_cc_config_params *config) |
| 992 |
{ |
| 993 |
return config->cc_max_agents; |
| 994 |
} |
| 995 |
|
| 996 |
void ast_set_cc_max_agents(struct ast_cc_config_params *config, unsigned int value) |
| 997 |
{ |
| 998 |
config->cc_max_agents = value; |
| 999 |
} |
| 1000 |
|
| 1001 |
unsigned int ast_get_cc_max_monitors(struct ast_cc_config_params *config) |
| 1002 |
{ |
| 1003 |
return config->cc_max_monitors; |
| 1004 |
} |
| 1005 |
|
| 1006 |
void ast_set_cc_max_monitors(struct ast_cc_config_params *config, unsigned int value) |
| 1007 |
{ |
| 1008 |
config->cc_max_monitors = value; |
| 1009 |
} |
| 1010 |
|
| 1011 |
const char *ast_get_cc_callback_sub(struct ast_cc_config_params *config) |
| 1012 |
{ |
| 1013 |
return config->cc_callback_sub; |
| 1014 |
} |
| 1015 |
|
| 1016 |
void ast_set_cc_callback_sub(struct ast_cc_config_params *config, const char * const value) |
| 1017 |
{ |
| 1018 |
if (ast_strlen_zero(value)) { |
| 1019 |
config->cc_callback_sub[0] = '\0'; |
| 1020 |
} else { |
| 1021 |
ast_copy_string(config->cc_callback_sub, value, sizeof(config->cc_callback_sub)); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
static int cc_publish(struct stasis_message_type *message_type, int core_id, struct ast_json *extras) |
| 1026 |
{ |
| 1027 |
struct ast_json *blob; |
| 1028 |
struct ast_json_payload *payload; |
| 1029 |
struct stasis_message *message; |
| 1030 |
|
| 1031 |
if (!message_type) { |
| 1032 |
return -1; |
| 1033 |
} |
| 1034 |
|
| 1035 |
blob = ast_json_pack("{s: i}", |
| 1036 |
"core_id", core_id); |
| 1037 |
if (!blob) { |
| 1038 |
return -1; |
| 1039 |
} |
| 1040 |
|
| 1041 |
if (extras) { |
| 1042 |
ast_json_object_update(blob, extras); |
| 1043 |
} |
| 1044 |
|
| 1045 |
payload = ast_json_payload_create(blob); |
| 1046 |
ast_json_unref(blob); |
| 1047 |
|
| 1048 |
if (!payload) { |
| 1049 |
return -1; |
| 1050 |
} |
| 1051 |
|
| 1052 |
message = stasis_message_create(message_type, payload); |
| 1053 |
ao2_ref(payload, -1); |
| 1054 |
|
| 1055 |
if (!message) { |
| 1056 |
return -1; |
| 1057 |
} |
| 1058 |
|
| 1059 |
stasis_publish(ast_system_topic(), message); |
| 1060 |
ao2_ref(message, -1); |
| 1061 |
|
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static void cc_publish_available(int core_id, const char *callee, const char *service) |
| 1066 |
{ |
| 1067 |
struct ast_json *extras; |
| 1068 |
|
| 1069 |
extras = ast_json_pack("{s: s, s: s}", |
| 1070 |
"callee", callee, |
| 1071 |
"service", service); |
| 1072 |
|
| 1073 |
cc_publish(ast_cc_available_type(), core_id, extras); |
| 1074 |
ast_json_unref(extras); |
| 1075 |
} |
| 1076 |
|
| 1077 |
static void cc_publish_offertimerstart(int core_id, const char *caller, unsigned int expires) |
| 1078 |
{ |
| 1079 |
struct ast_json *extras; |
| 1080 |
|
| 1081 |
extras = ast_json_pack("{s: s, s: I}", |
| 1082 |
"caller", caller, |
| 1083 |
"expires", (ast_json_int_t)expires); |
| 1084 |
|
| 1085 |
cc_publish(ast_cc_offertimerstart_type(), core_id, extras); |
| 1086 |
ast_json_unref(extras); |
| 1087 |
} |
| 1088 |
|
| 1089 |
static void cc_publish_requested(int core_id, const char *caller, const char *callee) |
| 1090 |
{ |
| 1091 |
struct ast_json *extras; |
| 1092 |
|
| 1093 |
extras = ast_json_pack("{s: s, s: s}", |
| 1094 |
"caller", caller, |
| 1095 |
"callee", callee); |
| 1096 |
|
| 1097 |
cc_publish(ast_cc_requested_type(), core_id, extras); |
| 1098 |
ast_json_unref(extras); |
| 1099 |
} |
| 1100 |
|
| 1101 |
static void cc_publish_requestacknowledged(int core_id, const char *caller) |
| 1102 |
{ |
| 1103 |
struct ast_json *extras; |
| 1104 |
|
| 1105 |
extras = ast_json_pack("{s: s}", |
| 1106 |
"caller", caller); |
| 1107 |
|
| 1108 |
cc_publish(ast_cc_requestacknowledged_type(), core_id, extras); |
| 1109 |
ast_json_unref(extras); |
| 1110 |
} |
| 1111 |
|
| 1112 |
static void cc_publish_callerstopmonitoring(int core_id, const char *caller) |
| 1113 |
{ |
| 1114 |
struct ast_json *extras; |
| 1115 |
|
| 1116 |
extras = ast_json_pack("{s: s}", |
| 1117 |
"caller", caller); |
| 1118 |
|
| 1119 |
cc_publish(ast_cc_callerstopmonitoring_type(), core_id, extras); |
| 1120 |
ast_json_unref(extras); |
| 1121 |
} |
| 1122 |
|
| 1123 |
static void cc_publish_callerstartmonitoring(int core_id, const char *caller) |
| 1124 |
{ |
| 1125 |
struct ast_json *extras; |
| 1126 |
|
| 1127 |
extras = ast_json_pack("{s: s}", |
| 1128 |
"caller", caller); |
| 1129 |
|
| 1130 |
cc_publish(ast_cc_callerstartmonitoring_type(), core_id, extras); |
| 1131 |
ast_json_unref(extras); |
| 1132 |
} |
| 1133 |
|
| 1134 |
static void cc_publish_callerrecalling(int core_id, const char *caller) |
| 1135 |
{ |
| 1136 |
struct ast_json *extras; |
| 1137 |
|
| 1138 |
extras = ast_json_pack("{s: s}", |
| 1139 |
"caller", caller); |
| 1140 |
|
| 1141 |
cc_publish(ast_cc_callerrecalling_type(), core_id, extras); |
| 1142 |
ast_json_unref(extras); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static void cc_publish_recallcomplete(int core_id, const char *caller) |
| 1146 |
{ |
| 1147 |
struct ast_json *extras; |
| 1148 |
|
| 1149 |
extras = ast_json_pack("{s: s}", |
| 1150 |
"caller", caller); |
| 1151 |
|
| 1152 |
cc_publish(ast_cc_recallcomplete_type(), core_id, extras); |
| 1153 |
ast_json_unref(extras); |
| 1154 |
} |
| 1155 |
|
| 1156 |
static void cc_publish_failure(int core_id, const char *caller, const char *reason) |
| 1157 |
{ |
| 1158 |
struct ast_json *extras; |
| 1159 |
|
| 1160 |
extras = ast_json_pack("{s: s, s: s}", |
| 1161 |
"caller", caller, |
| 1162 |
"reason", reason); |
| 1163 |
|
| 1164 |
cc_publish(ast_cc_failure_type(), core_id, extras); |
| 1165 |
ast_json_unref(extras); |
| 1166 |
} |
| 1167 |
|
| 1168 |
static void cc_publish_monitorfailed(int core_id, const char *callee) |
| 1169 |
{ |
| 1170 |
struct ast_json *extras; |
| 1171 |
|
| 1172 |
extras = ast_json_pack("{s: s}", |
| 1173 |
"callee", callee); |
| 1174 |
|
| 1175 |
cc_publish(ast_cc_monitorfailed_type(), core_id, extras); |
| 1176 |
ast_json_unref(extras); |
| 1177 |
} |
| 1178 |
|
| 1179 |
struct cc_monitor_backend { |
| 1180 |
AST_LIST_ENTRY(cc_monitor_backend) next; |
| 1181 |
const struct ast_cc_monitor_callbacks *callbacks; |
| 1182 |
}; |
| 1183 |
|
| 1184 |
AST_RWLIST_HEAD_STATIC(cc_monitor_backends, cc_monitor_backend); |
| 1185 |
|
| 1186 |
int ast_cc_monitor_register(const struct ast_cc_monitor_callbacks *callbacks) |
| 1187 |
{ |
| 1188 |
struct cc_monitor_backend *backend; |
| 1189 |
|
| 1190 |
RETURN_IF_NOT_ENABLED(0); |
| 1191 |
|
| 1192 |
backend = ast_calloc(1, sizeof(*backend)); |
| 1193 |
if (!backend) { |
| 1194 |
return -1; |
| 1195 |
} |
| 1196 |
|
| 1197 |
backend->callbacks = callbacks; |
| 1198 |
|
| 1199 |
AST_RWLIST_WRLOCK(&cc_monitor_backends); |
| 1200 |
AST_RWLIST_INSERT_TAIL(&cc_monitor_backends, backend, next); |
| 1201 |
AST_RWLIST_UNLOCK(&cc_monitor_backends); |
| 1202 |
return 0; |
| 1203 |
} |
| 1204 |
|
| 1205 |
static const struct ast_cc_monitor_callbacks *find_monitor_callbacks(const char * const type) |
| 1206 |
{ |
| 1207 |
struct cc_monitor_backend *backend; |
| 1208 |
const struct ast_cc_monitor_callbacks *callbacks = NULL; |
| 1209 |
|
| 1210 |
AST_RWLIST_RDLOCK(&cc_monitor_backends); |
| 1211 |
AST_RWLIST_TRAVERSE(&cc_monitor_backends, backend, next) { |
| 1212 |
if (!strcmp(backend->callbacks->type, type)) { |
| 1213 |
ast_log_dynamic_level(cc_logger_level, "Returning monitor backend %s\n", backend->callbacks->type); |
| 1214 |
callbacks = backend->callbacks; |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
AST_RWLIST_UNLOCK(&cc_monitor_backends); |
| 1219 |
return callbacks; |
| 1220 |
} |
| 1221 |
|
| 1222 |
void ast_cc_monitor_unregister(const struct ast_cc_monitor_callbacks *callbacks) |
| 1223 |
{ |
| 1224 |
struct cc_monitor_backend *backend; |
| 1225 |
|
| 1226 |
RETURN_IF_NOT_ENABLED(); |
| 1227 |
|
| 1228 |
AST_RWLIST_WRLOCK(&cc_monitor_backends); |
| 1229 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_monitor_backends, backend, next) { |
| 1230 |
if (backend->callbacks == callbacks) { |
| 1231 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 1232 |
ast_free(backend); |
| 1233 |
break; |
| 1234 |
} |
| 1235 |
} |
| 1236 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 1237 |
AST_RWLIST_UNLOCK(&cc_monitor_backends); |
| 1238 |
} |
| 1239 |
|
| 1240 |
struct cc_agent_backend { |
| 1241 |
AST_LIST_ENTRY(cc_agent_backend) next; |
| 1242 |
const struct ast_cc_agent_callbacks *callbacks; |
| 1243 |
}; |
| 1244 |
|
| 1245 |
AST_RWLIST_HEAD_STATIC(cc_agent_backends, cc_agent_backend); |
| 1246 |
|
| 1247 |
int ast_cc_agent_register(const struct ast_cc_agent_callbacks *callbacks) |
| 1248 |
{ |
| 1249 |
struct cc_agent_backend *backend; |
| 1250 |
|
| 1251 |
RETURN_IF_NOT_ENABLED(0); |
| 1252 |
|
| 1253 |
backend = ast_calloc(1, sizeof(*backend)); |
| 1254 |
if (!backend) { |
| 1255 |
return -1; |
| 1256 |
} |
| 1257 |
|
| 1258 |
backend->callbacks = callbacks; |
| 1259 |
AST_RWLIST_WRLOCK(&cc_agent_backends); |
| 1260 |
AST_RWLIST_INSERT_TAIL(&cc_agent_backends, backend, next); |
| 1261 |
AST_RWLIST_UNLOCK(&cc_agent_backends); |
| 1262 |
return 0; |
| 1263 |
} |
| 1264 |
|
| 1265 |
void ast_cc_agent_unregister(const struct ast_cc_agent_callbacks *callbacks) |
| 1266 |
{ |
| 1267 |
struct cc_agent_backend *backend; |
| 1268 |
|
| 1269 |
RETURN_IF_NOT_ENABLED(); |
| 1270 |
|
| 1271 |
AST_RWLIST_WRLOCK(&cc_agent_backends); |
| 1272 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&cc_agent_backends, backend, next) { |
| 1273 |
if (backend->callbacks == callbacks) { |
| 1274 |
AST_RWLIST_REMOVE_CURRENT(next); |
| 1275 |
ast_free(backend); |
| 1276 |
break; |
| 1277 |
} |
| 1278 |
} |
| 1279 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 1280 |
AST_RWLIST_UNLOCK(&cc_agent_backends); |
| 1281 |
} |
| 1282 |
|
| 1283 |
static const struct ast_cc_agent_callbacks *find_agent_callbacks(struct ast_channel *chan) |
| 1284 |
{ |
| 1285 |
struct cc_agent_backend *backend; |
| 1286 |
const struct ast_cc_agent_callbacks *callbacks = NULL; |
| 1287 |
struct ast_cc_config_params *cc_params; |
| 1288 |
char type[32]; |
| 1289 |
|
| 1290 |
cc_params = ast_channel_get_cc_config_params(chan); |
| 1291 |
if (!cc_params) { |
| 1292 |
return NULL; |
| 1293 |
} |
| 1294 |
switch (ast_get_cc_agent_policy(cc_params)) { |
| 1295 |
case AST_CC_AGENT_GENERIC: |
| 1296 |
ast_copy_string(type, "generic", sizeof(type)); |
| 1297 |
break; |
| 1298 |
case AST_CC_AGENT_NATIVE: |
| 1299 |
ast_channel_get_cc_agent_type(chan, type, sizeof(type)); |
| 1300 |
break; |
| 1301 |
default: |
| 1302 |
ast_log_dynamic_level(cc_logger_level, "Not returning agent callbacks since this channel is configured not to have a CC agent\n"); |
| 1303 |
return NULL; |
| 1304 |
} |
| 1305 |
|
| 1306 |
AST_RWLIST_RDLOCK(&cc_agent_backends); |
| 1307 |
AST_RWLIST_TRAVERSE(&cc_agent_backends, backend, next) { |
| 1308 |
if (!strcmp(backend->callbacks->type, type)) { |
| 1309 |
ast_log_dynamic_level(cc_logger_level, "Returning agent backend %s\n", backend->callbacks->type); |
| 1310 |
callbacks = backend->callbacks; |
| 1311 |
break; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
AST_RWLIST_UNLOCK(&cc_agent_backends); |
| 1315 |
return callbacks; |
| 1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
static int cc_generic_is_device_available(enum ast_device_state state) |
| 1328 |
{ |
| 1329 |
return state == AST_DEVICE_NOT_INUSE || state == AST_DEVICE_UNKNOWN; |
| 1330 |
} |
| 1331 |
|
| 1332 |
static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id); |
| 1333 |
static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor); |
| 1334 |
static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor); |
| 1335 |
static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id); |
| 1336 |
static void cc_generic_monitor_destructor(void *private_data); |
| 1337 |
|
| 1338 |
static struct ast_cc_monitor_callbacks generic_monitor_cbs = { |
| 1339 |
.type = "generic", |
| 1340 |
.request_cc = cc_generic_monitor_request_cc, |
| 1341 |
.suspend = cc_generic_monitor_suspend, |
| 1342 |
.unsuspend = cc_generic_monitor_unsuspend, |
| 1343 |
.cancel_available_timer = cc_generic_monitor_cancel_available_timer, |
| 1344 |
.destructor = cc_generic_monitor_destructor, |
| 1345 |
}; |
| 1346 |
|
| 1347 |
struct ao2_container *generic_monitors; |
| 1348 |
|
| 1349 |
struct generic_monitor_instance { |
| 1350 |
int core_id; |
| 1351 |
int is_suspended; |
| 1352 |
int monitoring; |
| 1353 |
AST_LIST_ENTRY(generic_monitor_instance) next; |
| 1354 |
}; |
| 1355 |
|
| 1356 |
struct generic_monitor_instance_list { |
| 1357 |
const char *device_name; |
| 1358 |
enum ast_device_state current_state; |
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
int fit_for_recall; |
| 1375 |
struct stasis_subscription *sub; |
| 1376 |
AST_LIST_HEAD_NOLOCK(, generic_monitor_instance) list; |
| 1377 |
}; |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
struct generic_monitor_pvt { |
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
const char *device_name; |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
int core_id; |
| 1395 |
}; |
| 1396 |
|
| 1397 |
AO2_STRING_FIELD_HASH_FN(generic_monitor_instance_list, device_name) |
| 1398 |
AO2_STRING_FIELD_CMP_FN(generic_monitor_instance_list, device_name) |
| 1399 |
|
| 1400 |
static struct generic_monitor_instance_list *find_generic_monitor_instance_list(const char * const device_name) |
| 1401 |
{ |
| 1402 |
struct generic_monitor_instance_list finder = {0}; |
| 1403 |
char *uppertech = ast_strdupa(device_name); |
| 1404 |
ast_tech_to_upper(uppertech); |
| 1405 |
finder.device_name = uppertech; |
| 1406 |
|
| 1407 |
return ao2_t_find(generic_monitors, &finder, OBJ_POINTER, "Finding generic monitor instance list"); |
| 1408 |
} |
| 1409 |
|
| 1410 |
static void generic_monitor_instance_list_destructor(void *obj) |
| 1411 |
{ |
| 1412 |
struct generic_monitor_instance_list *generic_list = obj; |
| 1413 |
struct generic_monitor_instance *generic_instance; |
| 1414 |
|
| 1415 |
generic_list->sub = stasis_unsubscribe(generic_list->sub); |
| 1416 |
while ((generic_instance = AST_LIST_REMOVE_HEAD(&generic_list->list, next))) { |
| 1417 |
ast_free(generic_instance); |
| 1418 |
} |
| 1419 |
ast_free((char *)generic_list->device_name); |
| 1420 |
} |
| 1421 |
|
| 1422 |
static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg); |
| 1423 |
static struct generic_monitor_instance_list *create_new_generic_list(struct ast_cc_monitor *monitor) |
| 1424 |
{ |
| 1425 |
struct generic_monitor_instance_list *generic_list = ao2_t_alloc(sizeof(*generic_list), |
| 1426 |
generic_monitor_instance_list_destructor, "allocate generic monitor instance list"); |
| 1427 |
char * device_name; |
| 1428 |
struct stasis_topic *device_specific_topic; |
| 1429 |
|
| 1430 |
if (!generic_list) { |
| 1431 |
return NULL; |
| 1432 |
} |
| 1433 |
|
| 1434 |
if (!(device_name = ast_strdup(monitor->interface->device_name))) { |
| 1435 |
cc_unref(generic_list, "Failed to strdup the monitor's device name"); |
| 1436 |
return NULL; |
| 1437 |
} |
| 1438 |
ast_tech_to_upper(device_name); |
| 1439 |
generic_list->device_name = device_name; |
| 1440 |
|
| 1441 |
device_specific_topic = ast_device_state_topic(device_name); |
| 1442 |
if (!device_specific_topic) { |
| 1443 |
return NULL; |
| 1444 |
} |
| 1445 |
|
| 1446 |
if (!(generic_list->sub = stasis_subscribe(device_specific_topic, generic_monitor_devstate_cb, NULL))) { |
| 1447 |
cc_unref(generic_list, "Failed to subscribe to device state"); |
| 1448 |
return NULL; |
| 1449 |
} |
| 1450 |
stasis_subscription_accept_message_type(generic_list->sub, ast_device_state_message_type()); |
| 1451 |
stasis_subscription_set_filter(generic_list->sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 1452 |
generic_list->current_state = ast_device_state(monitor->interface->device_name); |
| 1453 |
ao2_t_link(generic_monitors, generic_list, "linking new generic monitor instance list"); |
| 1454 |
return generic_list; |
| 1455 |
} |
| 1456 |
|
| 1457 |
static int generic_monitor_devstate_tp_cb(void *data) |
| 1458 |
{ |
| 1459 |
RAII_VAR(struct ast_device_state_message *, dev_state, data, ao2_cleanup); |
| 1460 |
enum ast_device_state new_state = dev_state->state; |
| 1461 |
enum ast_device_state previous_state; |
| 1462 |
struct generic_monitor_instance_list *generic_list; |
| 1463 |
struct generic_monitor_instance *generic_instance; |
| 1464 |
|
| 1465 |
if (!(generic_list = find_generic_monitor_instance_list(dev_state->device))) { |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
return 0; |
| 1471 |
} |
| 1472 |
|
| 1473 |
if (generic_list->current_state == new_state) { |
| 1474 |
|
| 1475 |
cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback"); |
| 1476 |
return 0; |
| 1477 |
} |
| 1478 |
|
| 1479 |
previous_state = generic_list->current_state; |
| 1480 |
generic_list->current_state = new_state; |
| 1481 |
|
| 1482 |
if (cc_generic_is_device_available(new_state) && |
| 1483 |
(previous_state == AST_DEVICE_INUSE || previous_state == AST_DEVICE_UNAVAILABLE || |
| 1484 |
previous_state == AST_DEVICE_BUSY)) { |
| 1485 |
AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) { |
| 1486 |
if (!generic_instance->is_suspended && generic_instance->monitoring) { |
| 1487 |
generic_instance->monitoring = 0; |
| 1488 |
generic_list->fit_for_recall = 1; |
| 1489 |
ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available"); |
| 1490 |
break; |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} |
| 1494 |
cc_unref(generic_list, "Kill reference of generic list in devstate taskprocessor callback"); |
| 1495 |
return 0; |
| 1496 |
} |
| 1497 |
|
| 1498 |
static void generic_monitor_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg) |
| 1499 |
{ |
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
struct ast_device_state_message *dev_state; |
| 1506 |
if (ast_device_state_message_type() != stasis_message_type(msg)) { |
| 1507 |
return; |
| 1508 |
} |
| 1509 |
|
| 1510 |
dev_state = stasis_message_data(msg); |
| 1511 |
if (dev_state->eid) { |
| 1512 |
|
| 1513 |
return; |
| 1514 |
} |
| 1515 |
|
| 1516 |
ao2_t_ref(dev_state, +1, "Bumping dev_state ref for cc_core_taskprocessor"); |
| 1517 |
if (ast_taskprocessor_push(cc_core_taskprocessor, generic_monitor_devstate_tp_cb, dev_state)) { |
| 1518 |
ao2_cleanup(dev_state); |
| 1519 |
return; |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
int ast_cc_available_timer_expire(const void *data) |
| 1524 |
{ |
| 1525 |
struct ast_cc_monitor *monitor = (struct ast_cc_monitor *) data; |
| 1526 |
int res; |
| 1527 |
|
| 1528 |
RETURN_IF_NOT_ENABLED(0); |
| 1529 |
|
| 1530 |
monitor->available_timer_id = -1; |
| 1531 |
res = ast_cc_monitor_failed(monitor->core_id, monitor->interface->device_name, "Available timer expired for monitor"); |
| 1532 |
cc_unref(monitor, "Unref reference from scheduler\n"); |
| 1533 |
return res; |
| 1534 |
} |
| 1535 |
|
| 1536 |
static int cc_generic_monitor_request_cc(struct ast_cc_monitor *monitor, int *available_timer_id) |
| 1537 |
{ |
| 1538 |
struct generic_monitor_instance_list *generic_list; |
| 1539 |
struct generic_monitor_instance *generic_instance; |
| 1540 |
struct generic_monitor_pvt *gen_mon_pvt; |
| 1541 |
enum ast_cc_service_type service = monitor->service_offered; |
| 1542 |
int when; |
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
if (!(gen_mon_pvt = ast_calloc(1, sizeof(*gen_mon_pvt)))) { |
| 1549 |
return -1; |
| 1550 |
} |
| 1551 |
|
| 1552 |
if (!(gen_mon_pvt->device_name = ast_strdup(monitor->interface->device_name))) { |
| 1553 |
ast_free(gen_mon_pvt); |
| 1554 |
return -1; |
| 1555 |
} |
| 1556 |
|
| 1557 |
gen_mon_pvt->core_id = monitor->core_id; |
| 1558 |
|
| 1559 |
monitor->private_data = gen_mon_pvt; |
| 1560 |
|
| 1561 |
if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) { |
| 1562 |
if (!(generic_list = create_new_generic_list(monitor))) { |
| 1563 |
return -1; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
if (!(generic_instance = ast_calloc(1, sizeof(*generic_instance)))) { |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
cc_unref(generic_list, "Generic monitor instance failed to allocate"); |
| 1572 |
return -1; |
| 1573 |
} |
| 1574 |
generic_instance->core_id = monitor->core_id; |
| 1575 |
generic_instance->monitoring = 1; |
| 1576 |
AST_LIST_INSERT_TAIL(&generic_list->list, generic_instance, next); |
| 1577 |
when = service == AST_CC_CCBS ? ast_get_ccbs_available_timer(monitor->interface->config_params) : |
| 1578 |
ast_get_ccnr_available_timer(monitor->interface->config_params); |
| 1579 |
|
| 1580 |
*available_timer_id = ast_sched_add(cc_sched_context, when * 1000, |
| 1581 |
ast_cc_available_timer_expire, cc_ref(monitor, "Give the scheduler a monitor reference")); |
| 1582 |
if (*available_timer_id == -1) { |
| 1583 |
cc_unref(monitor, "Failed to schedule available timer. (monitor)"); |
| 1584 |
cc_unref(generic_list, "Failed to schedule available timer. (generic_list)"); |
| 1585 |
return -1; |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
if (service == AST_CC_CCNR || service == AST_CC_CCNL) { |
| 1591 |
generic_list->fit_for_recall = 0; |
| 1592 |
} |
| 1593 |
ast_cc_monitor_request_acked(monitor->core_id, "Generic monitor for %s subscribed to device state.", |
| 1594 |
monitor->interface->device_name); |
| 1595 |
cc_unref(generic_list, "Finished with monitor instance reference in request cc callback"); |
| 1596 |
return 0; |
| 1597 |
} |
| 1598 |
|
| 1599 |
static int cc_generic_monitor_suspend(struct ast_cc_monitor *monitor) |
| 1600 |
{ |
| 1601 |
struct generic_monitor_instance_list *generic_list; |
| 1602 |
struct generic_monitor_instance *generic_instance; |
| 1603 |
enum ast_device_state state = ast_device_state(monitor->interface->device_name); |
| 1604 |
|
| 1605 |
if (!(generic_list = find_generic_monitor_instance_list(monitor->interface->device_name))) { |
| 1606 |
return -1; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) { |
| 1611 |
if (generic_instance->core_id == monitor->core_id) { |
| 1612 |
generic_instance->is_suspended = 1; |
| 1613 |
break; |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
if (!cc_generic_is_device_available(state)) { |
| 1621 |
cc_unref(generic_list, "Device is in use. Nothing to do. Unref generic list."); |
| 1622 |
return 0; |
| 1623 |
} |
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) { |
| 1631 |
if (!generic_instance->is_suspended) { |
| 1632 |
ast_cc_monitor_callee_available(generic_instance->core_id, "Generic monitored party has become available"); |
| 1633 |
break; |
| 1634 |
} |
| 1635 |
} |
| 1636 |
cc_unref(generic_list, "Done with generic list in suspend callback"); |
| 1637 |
return 0; |
| 1638 |
} |
| 1639 |
|
| 1640 |
static int cc_generic_monitor_unsuspend(struct ast_cc_monitor *monitor) |
| 1641 |
{ |
| 1642 |
struct generic_monitor_instance *generic_instance; |
| 1643 |
struct generic_monitor_instance_list *generic_list = find_generic_monitor_instance_list(monitor->interface->device_name); |
| 1644 |
enum ast_device_state state = ast_device_state(monitor->interface->device_name); |
| 1645 |
|
| 1646 |
if (!generic_list) { |
| 1647 |
return -1; |
| 1648 |
} |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
if (cc_generic_is_device_available(state)) { |
| 1653 |
ast_cc_monitor_callee_available(monitor->core_id, "Generic monitored party has become available"); |
| 1654 |
} |
| 1655 |
|
| 1656 |
|
| 1657 |
AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) { |
| 1658 |
if (generic_instance->core_id == monitor->core_id) { |
| 1659 |
generic_instance->is_suspended = 0; |
| 1660 |
generic_instance->monitoring = 1; |
| 1661 |
break; |
| 1662 |
} |
| 1663 |
} |
| 1664 |
cc_unref(generic_list, "Done with generic list in cc_generic_monitor_unsuspend"); |
| 1665 |
return 0; |
| 1666 |
} |
| 1667 |
|
| 1668 |
static int cc_generic_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id) |
| 1669 |
{ |
| 1670 |
ast_assert(sched_id != NULL); |
| 1671 |
|
| 1672 |
if (*sched_id == -1) { |
| 1673 |
return 0; |
| 1674 |
} |
| 1675 |
|
| 1676 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Canceling generic monitor available timer for monitor %s\n", |
| 1677 |
monitor->core_id, monitor->interface->device_name); |
| 1678 |
if (!ast_sched_del(cc_sched_context, *sched_id)) { |
| 1679 |
cc_unref(monitor, "Remove scheduler's reference to the monitor"); |
| 1680 |
} |
| 1681 |
*sched_id = -1; |
| 1682 |
return 0; |
| 1683 |
} |
| 1684 |
|
| 1685 |
static void cc_generic_monitor_destructor(void *private_data) |
| 1686 |
{ |
| 1687 |
struct generic_monitor_pvt *gen_mon_pvt = private_data; |
| 1688 |
struct generic_monitor_instance_list *generic_list; |
| 1689 |
struct generic_monitor_instance *generic_instance; |
| 1690 |
|
| 1691 |
if (!private_data) { |
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
return; |
| 1698 |
} |
| 1699 |
|
| 1700 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying generic monitor %s\n", |
| 1701 |
gen_mon_pvt->core_id, gen_mon_pvt->device_name); |
| 1702 |
|
| 1703 |
if (!(generic_list = find_generic_monitor_instance_list(gen_mon_pvt->device_name))) { |
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
ast_free((char *)gen_mon_pvt->device_name); |
| 1709 |
ast_free(gen_mon_pvt); |
| 1710 |
return; |
| 1711 |
} |
| 1712 |
|
| 1713 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&generic_list->list, generic_instance, next) { |
| 1714 |
if (generic_instance->core_id == gen_mon_pvt->core_id) { |
| 1715 |
AST_LIST_REMOVE_CURRENT(next); |
| 1716 |
ast_free(generic_instance); |
| 1717 |
break; |
| 1718 |
} |
| 1719 |
} |
| 1720 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1721 |
|
| 1722 |
if (AST_LIST_EMPTY(&generic_list->list)) { |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
ao2_t_unlink(generic_monitors, generic_list, "Generic list is empty. Unlink it from the container"); |
| 1727 |
} else { |
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
if (generic_list->fit_for_recall |
| 1739 |
&& cc_generic_is_device_available(generic_list->current_state)) { |
| 1740 |
AST_LIST_TRAVERSE(&generic_list->list, generic_instance, next) { |
| 1741 |
if (!generic_instance->is_suspended && generic_instance->monitoring) { |
| 1742 |
ast_cc_monitor_callee_available(generic_instance->core_id, "Signaling generic monitor " |
| 1743 |
"availability due to other instance's failure."); |
| 1744 |
break; |
| 1745 |
} |
| 1746 |
} |
| 1747 |
} |
| 1748 |
} |
| 1749 |
cc_unref(generic_list, "Done with generic list in generic monitor destructor"); |
| 1750 |
ast_free((char *)gen_mon_pvt->device_name); |
| 1751 |
ast_free(gen_mon_pvt); |
| 1752 |
} |
| 1753 |
|
| 1754 |
static void cc_interface_destroy(void *data) |
| 1755 |
{ |
| 1756 |
struct ast_cc_interface *interface = data; |
| 1757 |
ast_log_dynamic_level(cc_logger_level, "Destroying cc interface %s\n", interface->device_name); |
| 1758 |
ast_cc_config_params_destroy(interface->config_params); |
| 1759 |
} |
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
struct extension_child_dialstring { |
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
char original_dialstring[AST_CHANNEL_NAME]; |
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
char device_name[AST_CHANNEL_NAME]; |
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
int is_valid; |
| 1828 |
AST_LIST_ENTRY(extension_child_dialstring) next; |
| 1829 |
}; |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
struct extension_monitor_pvt { |
| 1835 |
AST_LIST_HEAD_NOLOCK(, extension_child_dialstring) child_dialstrings; |
| 1836 |
}; |
| 1837 |
|
| 1838 |
static void cc_extension_monitor_destructor(void *private_data) |
| 1839 |
{ |
| 1840 |
struct extension_monitor_pvt *extension_pvt = private_data; |
| 1841 |
struct extension_child_dialstring *child_dialstring; |
| 1842 |
|
| 1843 |
|
| 1844 |
if (!extension_pvt) { |
| 1845 |
return; |
| 1846 |
} |
| 1847 |
|
| 1848 |
while ((child_dialstring = AST_LIST_REMOVE_HEAD(&extension_pvt->child_dialstrings, next))) { |
| 1849 |
ast_free(child_dialstring); |
| 1850 |
} |
| 1851 |
ast_free(extension_pvt); |
| 1852 |
} |
| 1853 |
|
| 1854 |
static void cc_monitor_destroy(void *data) |
| 1855 |
{ |
| 1856 |
struct ast_cc_monitor *monitor = data; |
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Calling destructor for monitor %s\n", |
| 1864 |
monitor->core_id, monitor->interface->device_name); |
| 1865 |
if (monitor->interface->monitor_class == AST_CC_EXTENSION_MONITOR) { |
| 1866 |
cc_extension_monitor_destructor(monitor->private_data); |
| 1867 |
} |
| 1868 |
if (monitor->callbacks) { |
| 1869 |
monitor->callbacks->destructor(monitor->private_data); |
| 1870 |
} |
| 1871 |
cc_unref(monitor->interface, "Unreffing tree's reference to interface"); |
| 1872 |
ast_free(monitor->dialstring); |
| 1873 |
} |
| 1874 |
|
| 1875 |
static void cc_interface_tree_destroy(void *data) |
| 1876 |
{ |
| 1877 |
struct cc_monitor_tree *cc_interface_tree = data; |
| 1878 |
struct ast_cc_monitor *monitor; |
| 1879 |
while ((monitor = AST_LIST_REMOVE_HEAD(cc_interface_tree, next))) { |
| 1880 |
if (monitor->callbacks) { |
| 1881 |
monitor->callbacks->cancel_available_timer(monitor, &monitor->available_timer_id); |
| 1882 |
} |
| 1883 |
cc_unref(monitor, "Destroying all monitors"); |
| 1884 |
} |
| 1885 |
AST_LIST_HEAD_DESTROY(cc_interface_tree); |
| 1886 |
} |
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
static int dialed_cc_interface_counter; |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
struct dialed_cc_interfaces { |
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
unsigned int dial_parent_id; |
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
int core_id; |
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
char ignore; |
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
char is_original_caller; |
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
struct cc_monitor_tree *interface_tree; |
| 1947 |
}; |
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
static void dialed_cc_interfaces_destroy(void *data) |
| 1962 |
{ |
| 1963 |
struct dialed_cc_interfaces *cc_interfaces = data; |
| 1964 |
cc_unref(cc_interfaces->interface_tree, "Unref dial's ref to monitor tree"); |
| 1965 |
ast_free(cc_interfaces); |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
static void *dialed_cc_interfaces_duplicate(void *data) |
| 1982 |
{ |
| 1983 |
struct dialed_cc_interfaces *old_cc_interfaces = data; |
| 1984 |
struct dialed_cc_interfaces *new_cc_interfaces = ast_calloc(1, sizeof(*new_cc_interfaces)); |
| 1985 |
if (!new_cc_interfaces) { |
| 1986 |
return NULL; |
| 1987 |
} |
| 1988 |
new_cc_interfaces->ignore = old_cc_interfaces->ignore; |
| 1989 |
new_cc_interfaces->dial_parent_id = old_cc_interfaces->dial_parent_id; |
| 1990 |
new_cc_interfaces->is_original_caller = 0; |
| 1991 |
cc_ref(old_cc_interfaces->interface_tree, "New ref due to duplication of monitor tree"); |
| 1992 |
new_cc_interfaces->core_id = old_cc_interfaces->core_id; |
| 1993 |
new_cc_interfaces->interface_tree = old_cc_interfaces->interface_tree; |
| 1994 |
return new_cc_interfaces; |
| 1995 |
} |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
static const struct ast_datastore_info dialed_cc_interfaces_info = { |
| 2007 |
.type = "Dial CC Interfaces", |
| 2008 |
.duplicate = dialed_cc_interfaces_duplicate, |
| 2009 |
.destroy = dialed_cc_interfaces_destroy, |
| 2010 |
}; |
| 2011 |
|
| 2012 |
static struct extension_monitor_pvt *extension_monitor_pvt_init(void) |
| 2013 |
{ |
| 2014 |
struct extension_monitor_pvt *ext_pvt = ast_calloc(1, sizeof(*ext_pvt)); |
| 2015 |
if (!ext_pvt) { |
| 2016 |
return NULL; |
| 2017 |
} |
| 2018 |
AST_LIST_HEAD_INIT_NOLOCK(&ext_pvt->child_dialstrings); |
| 2019 |
return ext_pvt; |
| 2020 |
} |
| 2021 |
|
| 2022 |
void ast_cc_extension_monitor_add_dialstring(struct ast_channel *incoming, const char * const dialstring, const char * const device_name) |
| 2023 |
{ |
| 2024 |
struct ast_datastore *cc_datastore; |
| 2025 |
struct dialed_cc_interfaces *cc_interfaces; |
| 2026 |
struct ast_cc_monitor *monitor; |
| 2027 |
struct extension_monitor_pvt *extension_pvt; |
| 2028 |
struct extension_child_dialstring *child_dialstring; |
| 2029 |
struct cc_monitor_tree *interface_tree; |
| 2030 |
int id; |
| 2031 |
|
| 2032 |
RETURN_IF_NOT_ENABLED(); |
| 2033 |
|
| 2034 |
ast_channel_lock(incoming); |
| 2035 |
if (!(cc_datastore = ast_channel_datastore_find(incoming, &dialed_cc_interfaces_info, NULL))) { |
| 2036 |
ast_channel_unlock(incoming); |
| 2037 |
return; |
| 2038 |
} |
| 2039 |
|
| 2040 |
cc_interfaces = cc_datastore->data; |
| 2041 |
interface_tree = cc_interfaces->interface_tree; |
| 2042 |
id = cc_interfaces->dial_parent_id; |
| 2043 |
ast_channel_unlock(incoming); |
| 2044 |
|
| 2045 |
AST_LIST_LOCK(interface_tree); |
| 2046 |
AST_LIST_TRAVERSE(interface_tree, monitor, next) { |
| 2047 |
if (monitor->id == id) { |
| 2048 |
break; |
| 2049 |
} |
| 2050 |
} |
| 2051 |
|
| 2052 |
if (!monitor) { |
| 2053 |
AST_LIST_UNLOCK(interface_tree); |
| 2054 |
return; |
| 2055 |
} |
| 2056 |
|
| 2057 |
extension_pvt = monitor->private_data; |
| 2058 |
if (!(child_dialstring = ast_calloc(1, sizeof(*child_dialstring)))) { |
| 2059 |
AST_LIST_UNLOCK(interface_tree); |
| 2060 |
return; |
| 2061 |
} |
| 2062 |
ast_copy_string(child_dialstring->original_dialstring, dialstring, sizeof(child_dialstring->original_dialstring)); |
| 2063 |
ast_copy_string(child_dialstring->device_name, device_name, sizeof(child_dialstring->device_name)); |
| 2064 |
child_dialstring->is_valid = 1; |
| 2065 |
AST_LIST_INSERT_TAIL(&extension_pvt->child_dialstrings, child_dialstring, next); |
| 2066 |
AST_LIST_UNLOCK(interface_tree); |
| 2067 |
} |
| 2068 |
|
| 2069 |
static void cc_extension_monitor_change_is_valid(struct cc_core_instance *core_instance, unsigned int parent_id, const char * const device_name, int is_valid) |
| 2070 |
{ |
| 2071 |
struct ast_cc_monitor *monitor_iter; |
| 2072 |
struct extension_monitor_pvt *extension_pvt; |
| 2073 |
struct extension_child_dialstring *child_dialstring; |
| 2074 |
|
| 2075 |
AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) { |
| 2076 |
if (monitor_iter->id == parent_id) { |
| 2077 |
break; |
| 2078 |
} |
| 2079 |
} |
| 2080 |
|
| 2081 |
if (!monitor_iter) { |
| 2082 |
return; |
| 2083 |
} |
| 2084 |
extension_pvt = monitor_iter->private_data; |
| 2085 |
|
| 2086 |
AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) { |
| 2087 |
if (!strcmp(child_dialstring->device_name, device_name)) { |
| 2088 |
child_dialstring->is_valid = is_valid; |
| 2089 |
break; |
| 2090 |
} |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
static struct ast_cc_monitor *cc_extension_monitor_init(const char * const exten, const char * const context, const unsigned int parent_id) |
| 2109 |
{ |
| 2110 |
struct ast_str *str = ast_str_alloca(2 * AST_MAX_EXTENSION); |
| 2111 |
struct ast_cc_interface *cc_interface; |
| 2112 |
struct ast_cc_monitor *monitor; |
| 2113 |
|
| 2114 |
ast_str_set(&str, 0, "%s@%s", exten, context); |
| 2115 |
|
| 2116 |
if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + ast_str_strlen(str), cc_interface_destroy, |
| 2117 |
"Allocating new ast_cc_interface"))) { |
| 2118 |
return NULL; |
| 2119 |
} |
| 2120 |
|
| 2121 |
if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) { |
| 2122 |
cc_unref(cc_interface, "failed to allocate the monitor, so unref the interface"); |
| 2123 |
return NULL; |
| 2124 |
} |
| 2125 |
|
| 2126 |
if (!(monitor->private_data = extension_monitor_pvt_init())) { |
| 2127 |
cc_unref(monitor, "Failed to initialize extension monitor private data. uref monitor"); |
| 2128 |
cc_unref(cc_interface, "Failed to initialize extension monitor private data. unref cc_interface"); |
| 2129 |
} |
| 2130 |
|
| 2131 |
monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1); |
| 2132 |
monitor->parent_id = parent_id; |
| 2133 |
cc_interface->monitor_type = "extension"; |
| 2134 |
cc_interface->monitor_class = AST_CC_EXTENSION_MONITOR; |
| 2135 |
strcpy(cc_interface->device_name, ast_str_buffer(str)); |
| 2136 |
monitor->interface = cc_interface; |
| 2137 |
ast_log_dynamic_level(cc_logger_level, "Created an extension cc interface for '%s' with id %u and parent %u\n", cc_interface->device_name, monitor->id, monitor->parent_id); |
| 2138 |
return monitor; |
| 2139 |
} |
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
static int cc_interfaces_datastore_init(struct ast_channel *chan) { |
| 2158 |
struct dialed_cc_interfaces *interfaces; |
| 2159 |
struct ast_cc_monitor *monitor; |
| 2160 |
struct ast_datastore *dial_cc_datastore; |
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
if (!ast_cc_request_is_within_limits()) { |
| 2170 |
return 0; |
| 2171 |
} |
| 2172 |
|
| 2173 |
if (!(interfaces = ast_calloc(1, sizeof(*interfaces)))) { |
| 2174 |
return -1; |
| 2175 |
} |
| 2176 |
|
| 2177 |
if (!(monitor = cc_extension_monitor_init(ast_channel_exten(chan), ast_channel_context(chan), 0))) { |
| 2178 |
ast_free(interfaces); |
| 2179 |
return -1; |
| 2180 |
} |
| 2181 |
|
| 2182 |
if (!(dial_cc_datastore = ast_datastore_alloc(&dialed_cc_interfaces_info, NULL))) { |
| 2183 |
cc_unref(monitor, "Could not allocate the dialed interfaces datastore. Unreffing monitor"); |
| 2184 |
ast_free(interfaces); |
| 2185 |
return -1; |
| 2186 |
} |
| 2187 |
|
| 2188 |
if (!(interfaces->interface_tree = ao2_t_alloc(sizeof(*interfaces->interface_tree), cc_interface_tree_destroy, |
| 2189 |
"Allocate monitor tree"))) { |
| 2190 |
ast_datastore_free(dial_cc_datastore); |
| 2191 |
cc_unref(monitor, "Could not allocate monitor tree on dialed interfaces datastore. Unreffing monitor"); |
| 2192 |
ast_free(interfaces); |
| 2193 |
return -1; |
| 2194 |
} |
| 2195 |
|
| 2196 |
|
| 2197 |
AST_LIST_HEAD_INIT(interfaces->interface_tree); |
| 2198 |
AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next); |
| 2199 |
cc_ref(monitor, "List's reference to extension monitor"); |
| 2200 |
dial_cc_datastore->data = interfaces; |
| 2201 |
dial_cc_datastore->inheritance = DATASTORE_INHERIT_FOREVER; |
| 2202 |
interfaces->dial_parent_id = monitor->id; |
| 2203 |
interfaces->core_id = monitor->core_id = ast_atomic_fetchadd_int(&core_id_counter, +1); |
| 2204 |
interfaces->is_original_caller = 1; |
| 2205 |
ast_channel_lock(chan); |
| 2206 |
ast_channel_datastore_add(chan, dial_cc_datastore); |
| 2207 |
ast_channel_unlock(chan); |
| 2208 |
cc_unref(monitor, "Unreffing allocation's reference"); |
| 2209 |
return 0; |
| 2210 |
} |
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
static void call_destructor_with_no_monitor(const char * const monitor_type, void *private_data) |
| 2234 |
{ |
| 2235 |
const struct ast_cc_monitor_callbacks *monitor_callbacks = find_monitor_callbacks(monitor_type); |
| 2236 |
|
| 2237 |
if (!monitor_callbacks) { |
| 2238 |
return; |
| 2239 |
} |
| 2240 |
|
| 2241 |
monitor_callbacks->destructor(private_data); |
| 2242 |
} |
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
static struct ast_cc_monitor *cc_device_monitor_init(const char * const device_name, const char * const dialstring, const struct cc_control_payload *cc_data, int core_id) |
| 2269 |
{ |
| 2270 |
struct ast_cc_interface *cc_interface; |
| 2271 |
struct ast_cc_monitor *monitor; |
| 2272 |
size_t device_name_len = strlen(device_name); |
| 2273 |
int parent_id = cc_data->parent_interface_id; |
| 2274 |
|
| 2275 |
if (!(cc_interface = ao2_t_alloc(sizeof(*cc_interface) + device_name_len, cc_interface_destroy, |
| 2276 |
"Allocating new ast_cc_interface"))) { |
| 2277 |
return NULL; |
| 2278 |
} |
| 2279 |
|
| 2280 |
if (!(cc_interface->config_params = ast_cc_config_params_init())) { |
| 2281 |
cc_unref(cc_interface, "Failed to allocate config params, unref interface"); |
| 2282 |
return NULL; |
| 2283 |
} |
| 2284 |
|
| 2285 |
if (!(monitor = ao2_t_alloc(sizeof(*monitor), cc_monitor_destroy, "Allocating new ast_cc_monitor"))) { |
| 2286 |
cc_unref(cc_interface, "Failed to allocate monitor, unref interface"); |
| 2287 |
return NULL; |
| 2288 |
} |
| 2289 |
|
| 2290 |
if (!(monitor->dialstring = ast_strdup(dialstring))) { |
| 2291 |
cc_unref(monitor, "Failed to copy dialable name. Unref monitor"); |
| 2292 |
cc_unref(cc_interface, "Failed to copy dialable name"); |
| 2293 |
return NULL; |
| 2294 |
} |
| 2295 |
|
| 2296 |
if (!(monitor->callbacks = find_monitor_callbacks(cc_data->monitor_type))) { |
| 2297 |
cc_unref(monitor, "Failed to find monitor callbacks. Unref monitor"); |
| 2298 |
cc_unref(cc_interface, "Failed to find monitor callbacks"); |
| 2299 |
return NULL; |
| 2300 |
} |
| 2301 |
|
| 2302 |
strcpy(cc_interface->device_name, device_name); |
| 2303 |
monitor->id = ast_atomic_fetchadd_int(&dialed_cc_interface_counter, +1); |
| 2304 |
monitor->parent_id = parent_id; |
| 2305 |
monitor->core_id = core_id; |
| 2306 |
monitor->service_offered = cc_data->service; |
| 2307 |
monitor->private_data = cc_data->private_data; |
| 2308 |
cc_interface->monitor_type = cc_data->monitor_type; |
| 2309 |
cc_interface->monitor_class = AST_CC_DEVICE_MONITOR; |
| 2310 |
monitor->interface = cc_interface; |
| 2311 |
monitor->available_timer_id = -1; |
| 2312 |
ast_cc_copy_config_params(cc_interface->config_params, &cc_data->config_params); |
| 2313 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Created a device cc interface for '%s' with id %u and parent %u\n", |
| 2314 |
monitor->core_id, cc_interface->device_name, monitor->id, monitor->parent_id); |
| 2315 |
return monitor; |
| 2316 |
} |
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
void ast_handle_cc_control_frame(struct ast_channel *inbound, struct ast_channel *outbound, void *frame_data) |
| 2335 |
{ |
| 2336 |
char *device_name; |
| 2337 |
char *dialstring; |
| 2338 |
struct ast_cc_monitor *monitor; |
| 2339 |
struct ast_datastore *cc_datastore; |
| 2340 |
struct dialed_cc_interfaces *cc_interfaces; |
| 2341 |
struct cc_control_payload *cc_data = frame_data; |
| 2342 |
struct cc_core_instance *core_instance; |
| 2343 |
|
| 2344 |
RETURN_IF_NOT_ENABLED(); |
| 2345 |
|
| 2346 |
device_name = cc_data->device_name; |
| 2347 |
dialstring = cc_data->dialstring; |
| 2348 |
|
| 2349 |
ast_channel_lock(inbound); |
| 2350 |
if (!(cc_datastore = ast_channel_datastore_find(inbound, &dialed_cc_interfaces_info, NULL))) { |
| 2351 |
ast_log(LOG_WARNING, "Unable to retrieve CC datastore while processing CC frame from '%s'. CC services will be unavailable.\n", device_name); |
| 2352 |
ast_channel_unlock(inbound); |
| 2353 |
call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data); |
| 2354 |
return; |
| 2355 |
} |
| 2356 |
|
| 2357 |
cc_interfaces = cc_datastore->data; |
| 2358 |
|
| 2359 |
if (cc_interfaces->ignore) { |
| 2360 |
ast_channel_unlock(inbound); |
| 2361 |
call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data); |
| 2362 |
return; |
| 2363 |
} |
| 2364 |
|
| 2365 |
if (!cc_interfaces->is_original_caller) { |
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
ast_channel_unlock(inbound); |
| 2372 |
ast_indicate_data(inbound, AST_CONTROL_CC, cc_data, sizeof(*cc_data)); |
| 2373 |
return; |
| 2374 |
} |
| 2375 |
|
| 2376 |
core_instance = find_cc_core_instance(cc_interfaces->core_id); |
| 2377 |
if (!core_instance) { |
| 2378 |
core_instance = cc_core_init_instance(inbound, cc_interfaces->interface_tree, |
| 2379 |
cc_interfaces->core_id, cc_data); |
| 2380 |
if (!core_instance) { |
| 2381 |
cc_interfaces->ignore = 1; |
| 2382 |
ast_channel_unlock(inbound); |
| 2383 |
call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data); |
| 2384 |
return; |
| 2385 |
} |
| 2386 |
} |
| 2387 |
|
| 2388 |
ast_channel_unlock(inbound); |
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
AST_LIST_LOCK(cc_interfaces->interface_tree); |
| 2397 |
AST_LIST_TRAVERSE(cc_interfaces->interface_tree, monitor, next) { |
| 2398 |
if (!strcmp(monitor->interface->device_name, device_name)) { |
| 2399 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Device %s sent us multiple CC control frames. Ignoring those beyond the first.\n", |
| 2400 |
core_instance->core_id, device_name); |
| 2401 |
AST_LIST_UNLOCK(cc_interfaces->interface_tree); |
| 2402 |
cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance"); |
| 2403 |
call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data); |
| 2404 |
return; |
| 2405 |
} |
| 2406 |
} |
| 2407 |
AST_LIST_UNLOCK(cc_interfaces->interface_tree); |
| 2408 |
|
| 2409 |
if (!(monitor = cc_device_monitor_init(device_name, dialstring, cc_data, core_instance->core_id))) { |
| 2410 |
ast_log(LOG_WARNING, "Unable to create CC device interface for '%s'. CC services will be unavailable on this interface.\n", device_name); |
| 2411 |
cc_unref(core_instance, "Returning early from ast_handle_cc_control_frame. Unref core_instance"); |
| 2412 |
call_destructor_with_no_monitor(cc_data->monitor_type, cc_data->private_data); |
| 2413 |
return; |
| 2414 |
} |
| 2415 |
|
| 2416 |
AST_LIST_LOCK(cc_interfaces->interface_tree); |
| 2417 |
cc_ref(monitor, "monitor tree's reference to the monitor"); |
| 2418 |
AST_LIST_INSERT_TAIL(cc_interfaces->interface_tree, monitor, next); |
| 2419 |
AST_LIST_UNLOCK(cc_interfaces->interface_tree); |
| 2420 |
|
| 2421 |
cc_extension_monitor_change_is_valid(core_instance, monitor->parent_id, monitor->interface->device_name, 0); |
| 2422 |
|
| 2423 |
cc_publish_available(cc_interfaces->core_id, device_name, cc_service_to_string(cc_data->service)); |
| 2424 |
|
| 2425 |
cc_unref(core_instance, "Done with core_instance after handling CC control frame"); |
| 2426 |
cc_unref(monitor, "Unref reference from allocating monitor"); |
| 2427 |
} |
| 2428 |
|
| 2429 |
int ast_cc_call_init(struct ast_channel *chan, int *ignore_cc) |
| 2430 |
{ |
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
struct ast_datastore *cc_interfaces_datastore; |
| 2452 |
struct dialed_cc_interfaces *interfaces; |
| 2453 |
struct ast_cc_monitor *monitor; |
| 2454 |
struct ast_cc_config_params *cc_params; |
| 2455 |
|
| 2456 |
if (!global_enabled) { |
| 2457 |
ast_debug(3, "CCSS disabled globally\n"); |
| 2458 |
*ignore_cc = 1; |
| 2459 |
return 0; |
| 2460 |
} |
| 2461 |
|
| 2462 |
ast_channel_lock(chan); |
| 2463 |
|
| 2464 |
cc_params = ast_channel_get_cc_config_params(chan); |
| 2465 |
if (!cc_params) { |
| 2466 |
ast_channel_unlock(chan); |
| 2467 |
return -1; |
| 2468 |
} |
| 2469 |
if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_NEVER) { |
| 2470 |
|
| 2471 |
|
| 2472 |
*ignore_cc = 1; |
| 2473 |
ast_channel_unlock(chan); |
| 2474 |
ast_log_dynamic_level(cc_logger_level, "Agent policy for %s is 'never'. CC not possible\n", ast_channel_name(chan)); |
| 2475 |
return 0; |
| 2476 |
} |
| 2477 |
|
| 2478 |
if (!(cc_interfaces_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) { |
| 2479 |
|
| 2480 |
ast_channel_unlock(chan); |
| 2481 |
return cc_interfaces_datastore_init(chan); |
| 2482 |
} |
| 2483 |
interfaces = cc_interfaces_datastore->data; |
| 2484 |
ast_channel_unlock(chan); |
| 2485 |
|
| 2486 |
if (interfaces->ignore) { |
| 2487 |
|
| 2488 |
*ignore_cc = 1; |
| 2489 |
ast_log_dynamic_level(cc_logger_level, "Datastore is present with ignore flag set. Ignoring CC offers on this call\n"); |
| 2490 |
return 0; |
| 2491 |
} |
| 2492 |
|
| 2493 |
|
| 2494 |
if (!(monitor = cc_extension_monitor_init(ast_channel_exten(chan), |
| 2495 |
ast_channel_context(chan), |
| 2496 |
interfaces->dial_parent_id))) { |
| 2497 |
return -1; |
| 2498 |
} |
| 2499 |
monitor->core_id = interfaces->core_id; |
| 2500 |
AST_LIST_LOCK(interfaces->interface_tree); |
| 2501 |
cc_ref(monitor, "monitor tree's reference to the monitor"); |
| 2502 |
AST_LIST_INSERT_TAIL(interfaces->interface_tree, monitor, next); |
| 2503 |
AST_LIST_UNLOCK(interfaces->interface_tree); |
| 2504 |
interfaces->dial_parent_id = monitor->id; |
| 2505 |
cc_unref(monitor, "Unref monitor's allocation reference"); |
| 2506 |
return 0; |
| 2507 |
} |
| 2508 |
|
| 2509 |
int ast_cc_request_is_within_limits(void) |
| 2510 |
{ |
| 2511 |
return cc_request_count < global_cc_max_requests; |
| 2512 |
} |
| 2513 |
|
| 2514 |
int ast_cc_get_current_core_id(struct ast_channel *chan) |
| 2515 |
{ |
| 2516 |
struct ast_datastore *datastore; |
| 2517 |
struct dialed_cc_interfaces *cc_interfaces; |
| 2518 |
int core_id_return; |
| 2519 |
|
| 2520 |
RETURN_IF_NOT_ENABLED(-1); |
| 2521 |
|
| 2522 |
ast_channel_lock(chan); |
| 2523 |
if (!(datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) { |
| 2524 |
ast_channel_unlock(chan); |
| 2525 |
return -1; |
| 2526 |
} |
| 2527 |
|
| 2528 |
cc_interfaces = datastore->data; |
| 2529 |
core_id_return = cc_interfaces->ignore ? -1 : cc_interfaces->core_id; |
| 2530 |
ast_channel_unlock(chan); |
| 2531 |
return core_id_return; |
| 2532 |
|
| 2533 |
} |
| 2534 |
|
| 2535 |
static long count_agents(const char * const caller, const int core_id_exception) |
| 2536 |
{ |
| 2537 |
struct count_agents_cb_data data = {.core_id_exception = core_id_exception,}; |
| 2538 |
|
| 2539 |
ao2_t_callback_data(cc_core_instances, OBJ_NODATA, count_agents_cb, (char *)caller, &data, "Counting agents"); |
| 2540 |
ast_log_dynamic_level(cc_logger_level, "Counted %d agents\n", data.count); |
| 2541 |
return data.count; |
| 2542 |
} |
| 2543 |
|
| 2544 |
static void kill_duplicate_offers(char *caller) |
| 2545 |
{ |
| 2546 |
unsigned long match_flags = MATCH_NO_REQUEST; |
| 2547 |
struct ao2_iterator *dups_iter; |
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
dups_iter = ao2_t_callback_data(cc_core_instances, OBJ_MULTIPLE | OBJ_UNLINK, |
| 2554 |
match_agent, caller, &match_flags, "Killing duplicate offers"); |
| 2555 |
if (dups_iter) { |
| 2556 |
|
| 2557 |
ao2_iterator_destroy(dups_iter); |
| 2558 |
} |
| 2559 |
} |
| 2560 |
|
| 2561 |
static void check_callback_sanity(const struct ast_cc_agent_callbacks *callbacks) |
| 2562 |
{ |
| 2563 |
ast_assert(callbacks->init != NULL); |
| 2564 |
ast_assert(callbacks->start_offer_timer != NULL); |
| 2565 |
ast_assert(callbacks->stop_offer_timer != NULL); |
| 2566 |
ast_assert(callbacks->respond != NULL); |
| 2567 |
ast_assert(callbacks->status_request != NULL); |
| 2568 |
ast_assert(callbacks->start_monitoring != NULL); |
| 2569 |
ast_assert(callbacks->callee_available != NULL); |
| 2570 |
ast_assert(callbacks->destructor != NULL); |
| 2571 |
} |
| 2572 |
|
| 2573 |
static void agent_destroy(void *data) |
| 2574 |
{ |
| 2575 |
struct ast_cc_agent *agent = data; |
| 2576 |
|
| 2577 |
if (agent->callbacks) { |
| 2578 |
agent->callbacks->destructor(agent); |
| 2579 |
} |
| 2580 |
ast_cc_config_params_destroy(agent->cc_params); |
| 2581 |
} |
| 2582 |
|
| 2583 |
static struct ast_cc_agent *cc_agent_init(struct ast_channel *caller_chan, |
| 2584 |
const char * const caller_name, const int core_id, |
| 2585 |
struct cc_monitor_tree *interface_tree) |
| 2586 |
{ |
| 2587 |
struct ast_cc_agent *agent; |
| 2588 |
struct ast_cc_config_params *cc_params; |
| 2589 |
|
| 2590 |
if (!(agent = ao2_t_alloc(sizeof(*agent) + strlen(caller_name), agent_destroy, |
| 2591 |
"Allocating new ast_cc_agent"))) { |
| 2592 |
return NULL; |
| 2593 |
} |
| 2594 |
|
| 2595 |
agent->core_id = core_id; |
| 2596 |
strcpy(agent->device_name, caller_name); |
| 2597 |
|
| 2598 |
cc_params = ast_channel_get_cc_config_params(caller_chan); |
| 2599 |
if (!cc_params) { |
| 2600 |
cc_unref(agent, "Could not get channel config params."); |
| 2601 |
return NULL; |
| 2602 |
} |
| 2603 |
if (!(agent->cc_params = ast_cc_config_params_init())) { |
| 2604 |
cc_unref(agent, "Could not init agent config params."); |
| 2605 |
return NULL; |
| 2606 |
} |
| 2607 |
ast_cc_copy_config_params(agent->cc_params, cc_params); |
| 2608 |
|
| 2609 |
if (!(agent->callbacks = find_agent_callbacks(caller_chan))) { |
| 2610 |
cc_unref(agent, "Could not find agent callbacks."); |
| 2611 |
return NULL; |
| 2612 |
} |
| 2613 |
check_callback_sanity(agent->callbacks); |
| 2614 |
|
| 2615 |
if (agent->callbacks->init(agent, caller_chan)) { |
| 2616 |
cc_unref(agent, "Agent init callback failed."); |
| 2617 |
return NULL; |
| 2618 |
} |
| 2619 |
ast_log_dynamic_level(cc_logger_level, "Core %u: Created an agent for caller %s\n", |
| 2620 |
agent->core_id, agent->device_name); |
| 2621 |
return agent; |
| 2622 |
} |
| 2623 |
|
| 2624 |
|
| 2625 |
static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan); |
| 2626 |
static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent); |
| 2627 |
static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent); |
| 2628 |
static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason); |
| 2629 |
static int cc_generic_agent_status_request(struct ast_cc_agent *agent); |
| 2630 |
static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent); |
| 2631 |
static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent); |
| 2632 |
static int cc_generic_agent_recall(struct ast_cc_agent *agent); |
| 2633 |
static void cc_generic_agent_destructor(struct ast_cc_agent *agent); |
| 2634 |
|
| 2635 |
static struct ast_cc_agent_callbacks generic_agent_callbacks = { |
| 2636 |
.type = "generic", |
| 2637 |
.init = cc_generic_agent_init, |
| 2638 |
.start_offer_timer = cc_generic_agent_start_offer_timer, |
| 2639 |
.stop_offer_timer = cc_generic_agent_stop_offer_timer, |
| 2640 |
.respond = cc_generic_agent_respond, |
| 2641 |
.status_request = cc_generic_agent_status_request, |
| 2642 |
.stop_ringing = cc_generic_agent_stop_ringing, |
| 2643 |
.start_monitoring = cc_generic_agent_start_monitoring, |
| 2644 |
.callee_available = cc_generic_agent_recall, |
| 2645 |
.destructor = cc_generic_agent_destructor, |
| 2646 |
}; |
| 2647 |
|
| 2648 |
struct cc_generic_agent_pvt { |
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
struct stasis_subscription *sub; |
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
int offer_timer_id; |
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
char cid_num[AST_CHANNEL_NAME]; |
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
|
| 2679 |
char cid_name[AST_CHANNEL_NAME]; |
| 2680 |
|
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
char exten[AST_CHANNEL_NAME]; |
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
char context[AST_CHANNEL_NAME]; |
| 2696 |
}; |
| 2697 |
|
| 2698 |
static int cc_generic_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan) |
| 2699 |
{ |
| 2700 |
struct cc_generic_agent_pvt *generic_pvt = ast_calloc(1, sizeof(*generic_pvt)); |
| 2701 |
|
| 2702 |
if (!generic_pvt) { |
| 2703 |
return -1; |
| 2704 |
} |
| 2705 |
|
| 2706 |
generic_pvt->offer_timer_id = -1; |
| 2707 |
if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) { |
| 2708 |
ast_copy_string(generic_pvt->cid_num, ast_channel_caller(chan)->id.number.str, sizeof(generic_pvt->cid_num)); |
| 2709 |
} |
| 2710 |
if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) { |
| 2711 |
ast_copy_string(generic_pvt->cid_name, ast_channel_caller(chan)->id.name.str, sizeof(generic_pvt->cid_name)); |
| 2712 |
} |
| 2713 |
ast_copy_string(generic_pvt->exten, ast_channel_exten(chan), sizeof(generic_pvt->exten)); |
| 2714 |
ast_copy_string(generic_pvt->context, ast_channel_context(chan), sizeof(generic_pvt->context)); |
| 2715 |
agent->private_data = generic_pvt; |
| 2716 |
ast_set_flag(agent, AST_CC_AGENT_SKIP_OFFER); |
| 2717 |
return 0; |
| 2718 |
} |
| 2719 |
|
| 2720 |
static int offer_timer_expire(const void *data) |
| 2721 |
{ |
| 2722 |
struct ast_cc_agent *agent = (struct ast_cc_agent *) data; |
| 2723 |
struct cc_generic_agent_pvt *agent_pvt = agent->private_data; |
| 2724 |
ast_log_dynamic_level(cc_logger_level, "Core %u: Queuing change request because offer timer has expired.\n", |
| 2725 |
agent->core_id); |
| 2726 |
agent_pvt->offer_timer_id = -1; |
| 2727 |
ast_cc_failed(agent->core_id, "Generic agent %s offer timer expired", agent->device_name); |
| 2728 |
cc_unref(agent, "Remove scheduler's reference to the agent"); |
| 2729 |
return 0; |
| 2730 |
} |
| 2731 |
|
| 2732 |
static int cc_generic_agent_start_offer_timer(struct ast_cc_agent *agent) |
| 2733 |
{ |
| 2734 |
int when; |
| 2735 |
int sched_id; |
| 2736 |
struct cc_generic_agent_pvt *generic_pvt = agent->private_data; |
| 2737 |
|
| 2738 |
ast_assert(cc_sched_context != NULL); |
| 2739 |
ast_assert(agent->cc_params != NULL); |
| 2740 |
|
| 2741 |
when = ast_get_cc_offer_timer(agent->cc_params) * 1000; |
| 2742 |
ast_log_dynamic_level(cc_logger_level, "Core %u: About to schedule offer timer expiration for %d ms\n", |
| 2743 |
agent->core_id, when); |
| 2744 |
if ((sched_id = ast_sched_add(cc_sched_context, when, offer_timer_expire, cc_ref(agent, "Give scheduler an agent ref"))) == -1) { |
| 2745 |
return -1; |
| 2746 |
} |
| 2747 |
generic_pvt->offer_timer_id = sched_id; |
| 2748 |
return 0; |
| 2749 |
} |
| 2750 |
|
| 2751 |
static int cc_generic_agent_stop_offer_timer(struct ast_cc_agent *agent) |
| 2752 |
{ |
| 2753 |
struct cc_generic_agent_pvt *generic_pvt = agent->private_data; |
| 2754 |
|
| 2755 |
if (generic_pvt->offer_timer_id != -1) { |
| 2756 |
if (!ast_sched_del(cc_sched_context, generic_pvt->offer_timer_id)) { |
| 2757 |
cc_unref(agent, "Remove scheduler's reference to the agent"); |
| 2758 |
} |
| 2759 |
generic_pvt->offer_timer_id = -1; |
| 2760 |
} |
| 2761 |
return 0; |
| 2762 |
} |
| 2763 |
|
| 2764 |
static void cc_generic_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason) |
| 2765 |
{ |
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
return; |
| 2770 |
} |
| 2771 |
|
| 2772 |
static int cc_generic_agent_status_request(struct ast_cc_agent *agent) |
| 2773 |
{ |
| 2774 |
ast_cc_agent_status_response(agent->core_id, ast_device_state(agent->device_name)); |
| 2775 |
return 0; |
| 2776 |
} |
| 2777 |
|
| 2778 |
static int cc_generic_agent_stop_ringing(struct ast_cc_agent *agent) |
| 2779 |
{ |
| 2780 |
struct ast_channel *recall_chan = ast_channel_get_by_name_prefix(agent->device_name, strlen(agent->device_name)); |
| 2781 |
|
| 2782 |
if (!recall_chan) { |
| 2783 |
return 0; |
| 2784 |
} |
| 2785 |
|
| 2786 |
ast_softhangup(recall_chan, AST_SOFTHANGUP_EXPLICIT); |
| 2787 |
return 0; |
| 2788 |
} |
| 2789 |
|
| 2790 |
static void generic_agent_devstate_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg) |
| 2791 |
{ |
| 2792 |
struct ast_cc_agent *agent = userdata; |
| 2793 |
enum ast_device_state new_state; |
| 2794 |
struct ast_device_state_message *dev_state; |
| 2795 |
struct cc_generic_agent_pvt *generic_pvt = agent->private_data; |
| 2796 |
|
| 2797 |
if (stasis_subscription_final_message(sub, msg)) { |
| 2798 |
cc_unref(agent, "Done holding ref for subscription"); |
| 2799 |
return; |
| 2800 |
} else if (ast_device_state_message_type() != stasis_message_type(msg)) { |
| 2801 |
return; |
| 2802 |
} |
| 2803 |
|
| 2804 |
dev_state = stasis_message_data(msg); |
| 2805 |
if (dev_state->eid) { |
| 2806 |
|
| 2807 |
return; |
| 2808 |
} |
| 2809 |
|
| 2810 |
new_state = dev_state->state; |
| 2811 |
if (!cc_generic_is_device_available(new_state)) { |
| 2812 |
|
| 2813 |
return; |
| 2814 |
} |
| 2815 |
|
| 2816 |
generic_pvt->sub = stasis_unsubscribe(sub); |
| 2817 |
ast_cc_agent_caller_available(agent->core_id, "%s is no longer busy", agent->device_name); |
| 2818 |
} |
| 2819 |
|
| 2820 |
static int cc_generic_agent_start_monitoring(struct ast_cc_agent *agent) |
| 2821 |
{ |
| 2822 |
struct cc_generic_agent_pvt *generic_pvt = agent->private_data; |
| 2823 |
struct ast_str *str = ast_str_alloca(128); |
| 2824 |
struct stasis_topic *device_specific_topic; |
| 2825 |
|
| 2826 |
ast_assert(generic_pvt->sub == NULL); |
| 2827 |
ast_str_set(&str, 0, "Agent monitoring %s device state since it is busy\n", |
| 2828 |
agent->device_name); |
| 2829 |
|
| 2830 |
device_specific_topic = ast_device_state_topic(agent->device_name); |
| 2831 |
if (!device_specific_topic) { |
| 2832 |
return -1; |
| 2833 |
} |
| 2834 |
|
| 2835 |
if (!(generic_pvt->sub = stasis_subscribe(device_specific_topic, generic_agent_devstate_cb, agent))) { |
| 2836 |
return -1; |
| 2837 |
} |
| 2838 |
stasis_subscription_accept_message_type(generic_pvt->sub, ast_device_state_message_type()); |
| 2839 |
stasis_subscription_accept_message_type(generic_pvt->sub, stasis_subscription_change_type()); |
| 2840 |
stasis_subscription_set_filter(generic_pvt->sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 2841 |
cc_ref(agent, "Ref agent for subscription"); |
| 2842 |
return 0; |
| 2843 |
} |
| 2844 |
|
| 2845 |
static void *generic_recall(void *data) |
| 2846 |
{ |
| 2847 |
struct ast_cc_agent *agent = data; |
| 2848 |
struct cc_generic_agent_pvt *generic_pvt = agent->private_data; |
| 2849 |
char *interface = ast_strdupa(S_OR(ast_get_cc_agent_dialstring(agent->cc_params), agent->device_name)); |
| 2850 |
const char *tech; |
| 2851 |
char *target; |
| 2852 |
int reason; |
| 2853 |
struct ast_channel *chan; |
| 2854 |
const char *callback_sub = ast_get_cc_callback_sub(agent->cc_params); |
| 2855 |
unsigned int recall_timer = ast_get_cc_recall_timer(agent->cc_params) * 1000; |
| 2856 |
struct ast_format_cap *tmp_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 2857 |
|
| 2858 |
if (!tmp_cap) { |
| 2859 |
return NULL; |
| 2860 |
} |
| 2861 |
|
| 2862 |
tech = interface; |
| 2863 |
if ((target = strchr(interface, '/'))) { |
| 2864 |
*target++ = '\0'; |
| 2865 |
} |
| 2866 |
|
| 2867 |
ast_format_cap_append(tmp_cap, ast_format_slin, 0); |
| 2868 |
if (!(chan = ast_request_and_dial(tech, tmp_cap, NULL, NULL, target, recall_timer, &reason, generic_pvt->cid_num, generic_pvt->cid_name))) { |
| 2869 |
|
| 2870 |
|
| 2871 |
ast_log_dynamic_level(cc_logger_level, "Core %u: Failed to call back %s for reason %d\n", |
| 2872 |
agent->core_id, agent->device_name, reason); |
| 2873 |
ast_cc_failed(agent->core_id, "Failed to call back device %s/%s", tech, target); |
| 2874 |
ao2_ref(tmp_cap, -1); |
| 2875 |
return NULL; |
| 2876 |
} |
| 2877 |
ao2_ref(tmp_cap, -1); |
| 2878 |
|
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
ast_setup_cc_recall_datastore(chan, agent->core_id); |
| 2885 |
ast_cc_agent_set_interfaces_chanvar(chan); |
| 2886 |
|
| 2887 |
ast_channel_exten_set(chan, generic_pvt->exten); |
| 2888 |
ast_channel_context_set(chan, generic_pvt->context); |
| 2889 |
ast_channel_priority_set(chan, 1); |
| 2890 |
|
| 2891 |
pbx_builtin_setvar_helper(chan, "CC_EXTEN", generic_pvt->exten); |
| 2892 |
pbx_builtin_setvar_helper(chan, "CC_CONTEXT", generic_pvt->context); |
| 2893 |
|
| 2894 |
if (!ast_strlen_zero(callback_sub)) { |
| 2895 |
ast_log_dynamic_level(cc_logger_level, "Core %u: There's a callback subroutine configured for agent %s\n", |
| 2896 |
agent->core_id, agent->device_name); |
| 2897 |
if (ast_app_exec_sub(NULL, chan, callback_sub, 0)) { |
| 2898 |
ast_cc_failed(agent->core_id, "Callback subroutine to %s failed. Maybe a hangup?", agent->device_name); |
| 2899 |
ast_hangup(chan); |
| 2900 |
return NULL; |
| 2901 |
} |
| 2902 |
} |
| 2903 |
if (ast_pbx_start(chan)) { |
| 2904 |
ast_cc_failed(agent->core_id, "PBX failed to start for %s.", agent->device_name); |
| 2905 |
ast_hangup(chan); |
| 2906 |
return NULL; |
| 2907 |
} |
| 2908 |
ast_cc_agent_recalling(agent->core_id, "Generic agent %s is recalling", |
| 2909 |
agent->device_name); |
| 2910 |
return NULL; |
| 2911 |
} |
| 2912 |
|
| 2913 |
static int cc_generic_agent_recall(struct ast_cc_agent *agent) |
| 2914 |
{ |
| 2915 |
pthread_t clotho; |
| 2916 |
enum ast_device_state current_state = ast_device_state(agent->device_name); |
| 2917 |
|
| 2918 |
if (!cc_generic_is_device_available(current_state)) { |
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
ast_cc_agent_caller_busy(agent->core_id, "Generic agent caller %s is busy", agent->device_name); |
| 2923 |
return 0; |
| 2924 |
} |
| 2925 |
ast_pthread_create_detached_background(&clotho, NULL, generic_recall, agent); |
| 2926 |
return 0; |
| 2927 |
} |
| 2928 |
|
| 2929 |
static void cc_generic_agent_destructor(struct ast_cc_agent *agent) |
| 2930 |
{ |
| 2931 |
struct cc_generic_agent_pvt *agent_pvt = agent->private_data; |
| 2932 |
|
| 2933 |
if (!agent_pvt) { |
| 2934 |
|
| 2935 |
return; |
| 2936 |
} |
| 2937 |
|
| 2938 |
cc_generic_agent_stop_offer_timer(agent); |
| 2939 |
if (agent_pvt->sub) { |
| 2940 |
agent_pvt->sub = stasis_unsubscribe(agent_pvt->sub); |
| 2941 |
} |
| 2942 |
|
| 2943 |
ast_free(agent_pvt); |
| 2944 |
} |
| 2945 |
|
| 2946 |
static void cc_core_instance_destructor(void *data) |
| 2947 |
{ |
| 2948 |
struct cc_core_instance *core_instance = data; |
| 2949 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Destroying core instance\n", core_instance->core_id); |
| 2950 |
if (core_instance->agent) { |
| 2951 |
cc_unref(core_instance->agent, "Core instance is done with the agent now"); |
| 2952 |
} |
| 2953 |
if (core_instance->monitors) { |
| 2954 |
core_instance->monitors = cc_unref(core_instance->monitors, "Core instance is done with interface list"); |
| 2955 |
} |
| 2956 |
} |
| 2957 |
|
| 2958 |
static struct cc_core_instance *cc_core_init_instance(struct ast_channel *caller_chan, |
| 2959 |
struct cc_monitor_tree *called_tree, const int core_id, struct cc_control_payload *cc_data) |
| 2960 |
{ |
| 2961 |
char caller[AST_CHANNEL_NAME]; |
| 2962 |
struct cc_core_instance *core_instance; |
| 2963 |
struct ast_cc_config_params *cc_params; |
| 2964 |
long agent_count; |
| 2965 |
int recall_core_id; |
| 2966 |
|
| 2967 |
ast_channel_get_device_name(caller_chan, caller, sizeof(caller)); |
| 2968 |
cc_params = ast_channel_get_cc_config_params(caller_chan); |
| 2969 |
if (!cc_params) { |
| 2970 |
ast_log_dynamic_level(cc_logger_level, "Could not get CC parameters for %s\n", |
| 2971 |
caller); |
| 2972 |
return NULL; |
| 2973 |
} |
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
if (ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) { |
| 2978 |
kill_duplicate_offers(caller); |
| 2979 |
} |
| 2980 |
|
| 2981 |
ast_cc_is_recall(caller_chan, &recall_core_id, NULL); |
| 2982 |
agent_count = count_agents(caller, recall_core_id); |
| 2983 |
if (agent_count >= ast_get_cc_max_agents(cc_params)) { |
| 2984 |
ast_log_dynamic_level(cc_logger_level, "Caller %s already has the maximum number of agents configured\n", caller); |
| 2985 |
return NULL; |
| 2986 |
} |
| 2987 |
|
| 2988 |
|
| 2989 |
if (agent_count > 0 && ast_get_cc_agent_policy(cc_params) == AST_CC_AGENT_GENERIC) { |
| 2990 |
ast_log_dynamic_level(cc_logger_level, "Generic agents can only have a single outstanding request\n"); |
| 2991 |
return NULL; |
| 2992 |
} |
| 2993 |
|
| 2994 |
|
| 2995 |
if (!(core_instance = ao2_t_alloc(sizeof(*core_instance), cc_core_instance_destructor, "Creating core instance for CC"))) { |
| 2996 |
return NULL; |
| 2997 |
} |
| 2998 |
|
| 2999 |
core_instance->core_id = core_id; |
| 3000 |
if (!(core_instance->agent = cc_agent_init(caller_chan, caller, core_instance->core_id, called_tree))) { |
| 3001 |
cc_unref(core_instance, "Couldn't allocate agent, unref core_instance"); |
| 3002 |
return NULL; |
| 3003 |
} |
| 3004 |
|
| 3005 |
core_instance->monitors = cc_ref(called_tree, "Core instance getting ref to monitor tree"); |
| 3006 |
|
| 3007 |
ao2_t_link(cc_core_instances, core_instance, "Link core instance into container"); |
| 3008 |
|
| 3009 |
return core_instance; |
| 3010 |
} |
| 3011 |
|
| 3012 |
struct cc_state_change_args { |
| 3013 |
struct cc_core_instance *core_instance; |
| 3014 |
enum cc_state state; |
| 3015 |
int core_id; |
| 3016 |
char debug[1]; |
| 3017 |
}; |
| 3018 |
|
| 3019 |
static int is_state_change_valid(enum cc_state current_state, const enum cc_state new_state, struct ast_cc_agent *agent) |
| 3020 |
{ |
| 3021 |
int is_valid = 0; |
| 3022 |
switch (new_state) { |
| 3023 |
case CC_AVAILABLE: |
| 3024 |
ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to state %u? That should never happen.\n", |
| 3025 |
agent->core_id, new_state); |
| 3026 |
break; |
| 3027 |
case CC_CALLER_OFFERED: |
| 3028 |
if (current_state == CC_AVAILABLE) { |
| 3029 |
is_valid = 1; |
| 3030 |
} |
| 3031 |
break; |
| 3032 |
case CC_CALLER_REQUESTED: |
| 3033 |
if (current_state == CC_CALLER_OFFERED || |
| 3034 |
(current_state == CC_AVAILABLE && ast_test_flag(agent, AST_CC_AGENT_SKIP_OFFER))) { |
| 3035 |
is_valid = 1; |
| 3036 |
} |
| 3037 |
break; |
| 3038 |
case CC_ACTIVE: |
| 3039 |
if (current_state == CC_CALLER_REQUESTED || current_state == CC_CALLER_BUSY) { |
| 3040 |
is_valid = 1; |
| 3041 |
} |
| 3042 |
break; |
| 3043 |
case CC_CALLEE_READY: |
| 3044 |
if (current_state == CC_ACTIVE) { |
| 3045 |
is_valid = 1; |
| 3046 |
} |
| 3047 |
break; |
| 3048 |
case CC_CALLER_BUSY: |
| 3049 |
if (current_state == CC_CALLEE_READY) { |
| 3050 |
is_valid = 1; |
| 3051 |
} |
| 3052 |
break; |
| 3053 |
case CC_RECALLING: |
| 3054 |
if (current_state == CC_CALLEE_READY) { |
| 3055 |
is_valid = 1; |
| 3056 |
} |
| 3057 |
break; |
| 3058 |
case CC_COMPLETE: |
| 3059 |
if (current_state == CC_RECALLING) { |
| 3060 |
is_valid = 1; |
| 3061 |
} |
| 3062 |
break; |
| 3063 |
case CC_FAILED: |
| 3064 |
is_valid = 1; |
| 3065 |
break; |
| 3066 |
default: |
| 3067 |
ast_log_dynamic_level(cc_logger_level, "Core %u: Asked to change to unknown state %u\n", |
| 3068 |
agent->core_id, new_state); |
| 3069 |
break; |
| 3070 |
} |
| 3071 |
|
| 3072 |
return is_valid; |
| 3073 |
} |
| 3074 |
|
| 3075 |
static int cc_available(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3076 |
{ |
| 3077 |
|
| 3078 |
ast_log(LOG_WARNING, "Someone requested to change to CC_AVAILABLE? Ignoring.\n"); |
| 3079 |
return -1; |
| 3080 |
} |
| 3081 |
|
| 3082 |
static int cc_caller_offered(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3083 |
{ |
| 3084 |
if (core_instance->agent->callbacks->start_offer_timer(core_instance->agent)) { |
| 3085 |
ast_cc_failed(core_instance->core_id, "Failed to start the offer timer for %s\n", |
| 3086 |
core_instance->agent->device_name); |
| 3087 |
return -1; |
| 3088 |
} |
| 3089 |
cc_publish_offertimerstart(core_instance->core_id, core_instance->agent->device_name, core_instance->agent->cc_params->cc_offer_timer); |
| 3090 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Started the offer timer for the agent %s!\n", |
| 3091 |
core_instance->core_id, core_instance->agent->device_name); |
| 3092 |
return 0; |
| 3093 |
} |
| 3094 |
|
| 3095 |
|
| 3096 |
|
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
|
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
static int has_device_monitors(struct cc_core_instance *core_instance) |
| 3112 |
{ |
| 3113 |
struct ast_cc_monitor *iter; |
| 3114 |
int res = 0; |
| 3115 |
|
| 3116 |
AST_LIST_TRAVERSE(core_instance->monitors, iter, next) { |
| 3117 |
if (iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 3118 |
res = 1; |
| 3119 |
break; |
| 3120 |
} |
| 3121 |
} |
| 3122 |
|
| 3123 |
return res; |
| 3124 |
} |
| 3125 |
|
| 3126 |
static void request_cc(struct cc_core_instance *core_instance) |
| 3127 |
{ |
| 3128 |
struct ast_cc_monitor *monitor_iter; |
| 3129 |
AST_LIST_LOCK(core_instance->monitors); |
| 3130 |
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) { |
| 3131 |
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 3132 |
if (monitor_iter->callbacks->request_cc(monitor_iter, &monitor_iter->available_timer_id)) { |
| 3133 |
AST_LIST_REMOVE_CURRENT(next); |
| 3134 |
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id, |
| 3135 |
monitor_iter->interface->device_name, 1); |
| 3136 |
cc_unref(monitor_iter, "request_cc failed. Unref list's reference to monitor"); |
| 3137 |
} else { |
| 3138 |
cc_publish_requested(core_instance->core_id, core_instance->agent->device_name, monitor_iter->interface->device_name); |
| 3139 |
} |
| 3140 |
} |
| 3141 |
} |
| 3142 |
AST_LIST_TRAVERSE_SAFE_END; |
| 3143 |
|
| 3144 |
if (!has_device_monitors(core_instance)) { |
| 3145 |
ast_cc_failed(core_instance->core_id, "All device monitors failed to request CC"); |
| 3146 |
} |
| 3147 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 3148 |
} |
| 3149 |
|
| 3150 |
static int cc_caller_requested(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3151 |
{ |
| 3152 |
if (!ast_cc_request_is_within_limits()) { |
| 3153 |
ast_log(LOG_WARNING, "Cannot request CC since there is no more room for requests\n"); |
| 3154 |
core_instance->agent->callbacks->respond(core_instance->agent, |
| 3155 |
AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY); |
| 3156 |
ast_cc_failed(core_instance->core_id, "Too many requests in the system"); |
| 3157 |
return -1; |
| 3158 |
} |
| 3159 |
core_instance->agent->callbacks->stop_offer_timer(core_instance->agent); |
| 3160 |
request_cc(core_instance); |
| 3161 |
return 0; |
| 3162 |
} |
| 3163 |
|
| 3164 |
static void unsuspend(struct cc_core_instance *core_instance) |
| 3165 |
{ |
| 3166 |
struct ast_cc_monitor *monitor_iter; |
| 3167 |
AST_LIST_LOCK(core_instance->monitors); |
| 3168 |
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) { |
| 3169 |
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 3170 |
if (monitor_iter->callbacks->unsuspend(monitor_iter)) { |
| 3171 |
AST_LIST_REMOVE_CURRENT(next); |
| 3172 |
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id, |
| 3173 |
monitor_iter->interface->device_name, 1); |
| 3174 |
cc_unref(monitor_iter, "unsuspend failed. Unref list's reference to monitor"); |
| 3175 |
} |
| 3176 |
} |
| 3177 |
} |
| 3178 |
AST_LIST_TRAVERSE_SAFE_END; |
| 3179 |
|
| 3180 |
if (!has_device_monitors(core_instance)) { |
| 3181 |
ast_cc_failed(core_instance->core_id, "All device monitors failed to unsuspend CC"); |
| 3182 |
} |
| 3183 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 3184 |
} |
| 3185 |
|
| 3186 |
static int cc_active(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3187 |
{ |
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 3191 |
|
| 3192 |
|
| 3193 |
if (previous_state == CC_CALLER_REQUESTED) { |
| 3194 |
core_instance->agent->callbacks->respond(core_instance->agent, |
| 3195 |
AST_CC_AGENT_RESPONSE_SUCCESS); |
| 3196 |
cc_publish_requestacknowledged(core_instance->core_id, core_instance->agent->device_name); |
| 3197 |
} else if (previous_state == CC_CALLER_BUSY) { |
| 3198 |
cc_publish_callerstopmonitoring(core_instance->core_id, core_instance->agent->device_name); |
| 3199 |
unsuspend(core_instance); |
| 3200 |
} |
| 3201 |
|
| 3202 |
return 0; |
| 3203 |
} |
| 3204 |
|
| 3205 |
static int cc_callee_ready(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3206 |
{ |
| 3207 |
core_instance->agent->callbacks->callee_available(core_instance->agent); |
| 3208 |
return 0; |
| 3209 |
} |
| 3210 |
|
| 3211 |
static void suspend(struct cc_core_instance *core_instance) |
| 3212 |
{ |
| 3213 |
struct ast_cc_monitor *monitor_iter; |
| 3214 |
AST_LIST_LOCK(core_instance->monitors); |
| 3215 |
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) { |
| 3216 |
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 3217 |
if (monitor_iter->callbacks->suspend(monitor_iter)) { |
| 3218 |
AST_LIST_REMOVE_CURRENT(next); |
| 3219 |
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id, |
| 3220 |
monitor_iter->interface->device_name, 1); |
| 3221 |
cc_unref(monitor_iter, "suspend failed. Unref list's reference to monitor"); |
| 3222 |
} |
| 3223 |
} |
| 3224 |
} |
| 3225 |
AST_LIST_TRAVERSE_SAFE_END; |
| 3226 |
|
| 3227 |
if (!has_device_monitors(core_instance)) { |
| 3228 |
ast_cc_failed(core_instance->core_id, "All device monitors failed to suspend CC"); |
| 3229 |
} |
| 3230 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 3231 |
} |
| 3232 |
|
| 3233 |
static int cc_caller_busy(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3234 |
{ |
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
suspend(core_instance); |
| 3239 |
core_instance->agent->callbacks->start_monitoring(core_instance->agent); |
| 3240 |
cc_publish_callerstartmonitoring(core_instance->core_id, core_instance->agent->device_name); |
| 3241 |
return 0; |
| 3242 |
} |
| 3243 |
|
| 3244 |
static void cancel_available_timer(struct cc_core_instance *core_instance) |
| 3245 |
{ |
| 3246 |
struct ast_cc_monitor *monitor_iter; |
| 3247 |
AST_LIST_LOCK(core_instance->monitors); |
| 3248 |
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) { |
| 3249 |
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 3250 |
if (monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id)) { |
| 3251 |
AST_LIST_REMOVE_CURRENT(next); |
| 3252 |
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id, |
| 3253 |
monitor_iter->interface->device_name, 1); |
| 3254 |
cc_unref(monitor_iter, "cancel_available_timer failed. Unref list's reference to monitor"); |
| 3255 |
} |
| 3256 |
} |
| 3257 |
} |
| 3258 |
AST_LIST_TRAVERSE_SAFE_END; |
| 3259 |
|
| 3260 |
if (!has_device_monitors(core_instance)) { |
| 3261 |
ast_cc_failed(core_instance->core_id, "All device monitors failed to cancel their available timers"); |
| 3262 |
} |
| 3263 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 3264 |
} |
| 3265 |
|
| 3266 |
static int cc_recalling(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3267 |
{ |
| 3268 |
|
| 3269 |
|
| 3270 |
cancel_available_timer(core_instance); |
| 3271 |
cc_publish_callerrecalling(core_instance->core_id, core_instance->agent->device_name); |
| 3272 |
return 0; |
| 3273 |
} |
| 3274 |
|
| 3275 |
static int cc_complete(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3276 |
{ |
| 3277 |
|
| 3278 |
|
| 3279 |
cc_publish_recallcomplete(core_instance->core_id, core_instance->agent->device_name); |
| 3280 |
ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC recall has completed"); |
| 3281 |
return 0; |
| 3282 |
} |
| 3283 |
|
| 3284 |
static int cc_failed(struct cc_core_instance *core_instance, struct cc_state_change_args *args, enum cc_state previous_state) |
| 3285 |
{ |
| 3286 |
cc_publish_failure(core_instance->core_id, core_instance->agent->device_name, args->debug); |
| 3287 |
ao2_t_unlink(cc_core_instances, core_instance, "Unlink core instance since CC failed"); |
| 3288 |
return 0; |
| 3289 |
} |
| 3290 |
|
| 3291 |
static int (* const state_change_funcs [])(struct cc_core_instance *, struct cc_state_change_args *, enum cc_state previous_state) = { |
| 3292 |
[CC_AVAILABLE] = cc_available, |
| 3293 |
[CC_CALLER_OFFERED] = cc_caller_offered, |
| 3294 |
[CC_CALLER_REQUESTED] = cc_caller_requested, |
| 3295 |
[CC_ACTIVE] = cc_active, |
| 3296 |
[CC_CALLEE_READY] = cc_callee_ready, |
| 3297 |
[CC_CALLER_BUSY] = cc_caller_busy, |
| 3298 |
[CC_RECALLING] = cc_recalling, |
| 3299 |
[CC_COMPLETE] = cc_complete, |
| 3300 |
[CC_FAILED] = cc_failed, |
| 3301 |
}; |
| 3302 |
|
| 3303 |
static int cc_do_state_change(void *datap) |
| 3304 |
{ |
| 3305 |
struct cc_state_change_args *args = datap; |
| 3306 |
struct cc_core_instance *core_instance; |
| 3307 |
enum cc_state previous_state; |
| 3308 |
int res; |
| 3309 |
|
| 3310 |
ast_log_dynamic_level(cc_logger_level, "Core %d: State change to %u requested. Reason: %s\n", |
| 3311 |
args->core_id, args->state, args->debug); |
| 3312 |
|
| 3313 |
core_instance = args->core_instance; |
| 3314 |
|
| 3315 |
if (!is_state_change_valid(core_instance->current_state, args->state, core_instance->agent)) { |
| 3316 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Invalid state change requested. Cannot go from %s to %s\n", |
| 3317 |
args->core_id, cc_state_to_string(core_instance->current_state), cc_state_to_string(args->state)); |
| 3318 |
if (args->state == CC_CALLER_REQUESTED) { |
| 3319 |
|
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
core_instance->agent->callbacks->respond(core_instance->agent, |
| 3324 |
AST_CC_AGENT_RESPONSE_FAILURE_INVALID); |
| 3325 |
} |
| 3326 |
ast_free(args); |
| 3327 |
cc_unref(core_instance, "Unref core instance from when it was found earlier"); |
| 3328 |
return -1; |
| 3329 |
} |
| 3330 |
|
| 3331 |
|
| 3332 |
previous_state = core_instance->current_state; |
| 3333 |
core_instance->current_state = args->state; |
| 3334 |
res = state_change_funcs[core_instance->current_state](core_instance, args, previous_state); |
| 3335 |
|
| 3336 |
|
| 3337 |
if (!res && !strcmp(core_instance->agent->callbacks->type, "generic")) { |
| 3338 |
ccss_notify_device_state_change(core_instance->agent->device_name, core_instance->current_state); |
| 3339 |
} |
| 3340 |
|
| 3341 |
ast_free(args); |
| 3342 |
cc_unref(core_instance, "Unref since state change has completed"); |
| 3343 |
return res; |
| 3344 |
} |
| 3345 |
|
| 3346 |
static int cc_request_state_change(enum cc_state state, const int core_id, const char *debug, va_list ap) |
| 3347 |
{ |
| 3348 |
int res; |
| 3349 |
int debuglen; |
| 3350 |
char dummy[1]; |
| 3351 |
va_list aq; |
| 3352 |
struct cc_core_instance *core_instance; |
| 3353 |
struct cc_state_change_args *args; |
| 3354 |
|
| 3355 |
|
| 3356 |
|
| 3357 |
va_copy(aq, ap); |
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
debuglen = vsnprintf(dummy, sizeof(dummy), debug, aq) + 1; |
| 3362 |
va_end(aq); |
| 3363 |
|
| 3364 |
if (!(args = ast_calloc(1, sizeof(*args) + debuglen))) { |
| 3365 |
return -1; |
| 3366 |
} |
| 3367 |
|
| 3368 |
core_instance = find_cc_core_instance(core_id); |
| 3369 |
if (!core_instance) { |
| 3370 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Unable to find core instance.\n", |
| 3371 |
core_id); |
| 3372 |
ast_free(args); |
| 3373 |
return -1; |
| 3374 |
} |
| 3375 |
|
| 3376 |
args->core_instance = core_instance; |
| 3377 |
args->state = state; |
| 3378 |
args->core_id = core_id; |
| 3379 |
vsnprintf(args->debug, debuglen, debug, ap); |
| 3380 |
|
| 3381 |
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_do_state_change, args); |
| 3382 |
if (res) { |
| 3383 |
cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed"); |
| 3384 |
ast_free(args); |
| 3385 |
} |
| 3386 |
return res; |
| 3387 |
} |
| 3388 |
|
| 3389 |
struct cc_recall_ds_data { |
| 3390 |
int core_id; |
| 3391 |
char ignore; |
| 3392 |
char nested; |
| 3393 |
struct cc_monitor_tree *interface_tree; |
| 3394 |
}; |
| 3395 |
|
| 3396 |
static void *cc_recall_ds_duplicate(void *data) |
| 3397 |
{ |
| 3398 |
struct cc_recall_ds_data *old_data = data; |
| 3399 |
struct cc_recall_ds_data *new_data = ast_calloc(1, sizeof(*new_data)); |
| 3400 |
|
| 3401 |
if (!new_data) { |
| 3402 |
return NULL; |
| 3403 |
} |
| 3404 |
new_data->interface_tree = cc_ref(old_data->interface_tree, "Bump refcount of monitor tree for recall datastore duplicate"); |
| 3405 |
new_data->core_id = old_data->core_id; |
| 3406 |
new_data->nested = 1; |
| 3407 |
return new_data; |
| 3408 |
} |
| 3409 |
|
| 3410 |
static void cc_recall_ds_destroy(void *data) |
| 3411 |
{ |
| 3412 |
struct cc_recall_ds_data *recall_data = data; |
| 3413 |
recall_data->interface_tree = cc_unref(recall_data->interface_tree, "Unref recall monitor tree"); |
| 3414 |
ast_free(recall_data); |
| 3415 |
} |
| 3416 |
|
| 3417 |
static const struct ast_datastore_info recall_ds_info = { |
| 3418 |
.type = "cc_recall", |
| 3419 |
.duplicate = cc_recall_ds_duplicate, |
| 3420 |
.destroy = cc_recall_ds_destroy, |
| 3421 |
}; |
| 3422 |
|
| 3423 |
int ast_setup_cc_recall_datastore(struct ast_channel *chan, const int core_id) |
| 3424 |
{ |
| 3425 |
struct ast_datastore *recall_datastore; |
| 3426 |
struct cc_recall_ds_data *recall_data; |
| 3427 |
struct cc_core_instance *core_instance; |
| 3428 |
|
| 3429 |
RETURN_IF_NOT_ENABLED(0); |
| 3430 |
|
| 3431 |
recall_datastore = ast_datastore_alloc(&recall_ds_info, NULL); |
| 3432 |
if (!recall_datastore) { |
| 3433 |
return -1; |
| 3434 |
} |
| 3435 |
|
| 3436 |
if (!(recall_data = ast_calloc(1, sizeof(*recall_data)))) { |
| 3437 |
ast_datastore_free(recall_datastore); |
| 3438 |
return -1; |
| 3439 |
} |
| 3440 |
|
| 3441 |
if (!(core_instance = find_cc_core_instance(core_id))) { |
| 3442 |
ast_free(recall_data); |
| 3443 |
ast_datastore_free(recall_datastore); |
| 3444 |
return -1; |
| 3445 |
} |
| 3446 |
|
| 3447 |
recall_data->interface_tree = cc_ref(core_instance->monitors, |
| 3448 |
"Bump refcount for monitor tree for recall datastore"); |
| 3449 |
recall_data->core_id = core_id; |
| 3450 |
recall_datastore->data = recall_data; |
| 3451 |
recall_datastore->inheritance = DATASTORE_INHERIT_FOREVER; |
| 3452 |
ast_channel_lock(chan); |
| 3453 |
ast_channel_datastore_add(chan, recall_datastore); |
| 3454 |
ast_channel_unlock(chan); |
| 3455 |
cc_unref(core_instance, "Recall datastore set up. No need for core_instance ref"); |
| 3456 |
return 0; |
| 3457 |
} |
| 3458 |
|
| 3459 |
int ast_cc_is_recall(struct ast_channel *chan, int *core_id, const char * const monitor_type) |
| 3460 |
{ |
| 3461 |
struct ast_datastore *recall_datastore; |
| 3462 |
struct cc_recall_ds_data *recall_data; |
| 3463 |
struct cc_monitor_tree *interface_tree; |
| 3464 |
char device_name[AST_CHANNEL_NAME]; |
| 3465 |
struct ast_cc_monitor *device_monitor; |
| 3466 |
int core_id_candidate; |
| 3467 |
|
| 3468 |
RETURN_IF_NOT_ENABLED(0); |
| 3469 |
|
| 3470 |
ast_assert(core_id != NULL); |
| 3471 |
|
| 3472 |
*core_id = -1; |
| 3473 |
|
| 3474 |
ast_channel_lock(chan); |
| 3475 |
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) { |
| 3476 |
|
| 3477 |
ast_channel_unlock(chan); |
| 3478 |
return 0; |
| 3479 |
} |
| 3480 |
|
| 3481 |
recall_data = recall_datastore->data; |
| 3482 |
|
| 3483 |
if (recall_data->ignore) { |
| 3484 |
|
| 3485 |
|
| 3486 |
|
| 3487 |
|
| 3488 |
ast_channel_unlock(chan); |
| 3489 |
return 0; |
| 3490 |
} |
| 3491 |
|
| 3492 |
if (!recall_data->nested) { |
| 3493 |
|
| 3494 |
|
| 3495 |
|
| 3496 |
|
| 3497 |
|
| 3498 |
|
| 3499 |
*core_id = recall_data->core_id; |
| 3500 |
ast_channel_unlock(chan); |
| 3501 |
return 1; |
| 3502 |
} |
| 3503 |
|
| 3504 |
if (ast_strlen_zero(monitor_type)) { |
| 3505 |
|
| 3506 |
|
| 3507 |
|
| 3508 |
|
| 3509 |
|
| 3510 |
ast_channel_unlock(chan); |
| 3511 |
return 0; |
| 3512 |
} |
| 3513 |
|
| 3514 |
interface_tree = recall_data->interface_tree; |
| 3515 |
ast_channel_get_device_name(chan, device_name, sizeof(device_name)); |
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
core_id_candidate = recall_data->core_id; |
| 3522 |
ast_channel_unlock(chan); |
| 3523 |
|
| 3524 |
|
| 3525 |
|
| 3526 |
|
| 3527 |
|
| 3528 |
AST_LIST_LOCK(interface_tree); |
| 3529 |
AST_LIST_TRAVERSE(interface_tree, device_monitor, next) { |
| 3530 |
if (!strcmp(device_monitor->interface->device_name, device_name) && |
| 3531 |
!strcmp(device_monitor->interface->monitor_type, monitor_type)) { |
| 3532 |
|
| 3533 |
*core_id = core_id_candidate; |
| 3534 |
AST_LIST_UNLOCK(interface_tree); |
| 3535 |
return 1; |
| 3536 |
} |
| 3537 |
} |
| 3538 |
AST_LIST_UNLOCK(interface_tree); |
| 3539 |
return 0; |
| 3540 |
} |
| 3541 |
|
| 3542 |
struct ast_cc_monitor *ast_cc_get_monitor_by_recall_core_id(const int core_id, const char * const device_name) |
| 3543 |
{ |
| 3544 |
struct cc_core_instance *core_instance = find_cc_core_instance(core_id); |
| 3545 |
struct ast_cc_monitor *monitor_iter; |
| 3546 |
|
| 3547 |
RETURN_IF_NOT_ENABLED(NULL); |
| 3548 |
|
| 3549 |
if (!core_instance) { |
| 3550 |
return NULL; |
| 3551 |
} |
| 3552 |
|
| 3553 |
AST_LIST_LOCK(core_instance->monitors); |
| 3554 |
AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) { |
| 3555 |
if (!strcmp(monitor_iter->interface->device_name, device_name)) { |
| 3556 |
|
| 3557 |
cc_ref(monitor_iter, "Hand the requester of the monitor a reference"); |
| 3558 |
break; |
| 3559 |
} |
| 3560 |
} |
| 3561 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 3562 |
cc_unref(core_instance, "Done with core instance ref in ast_cc_get_monitor_by_recall_core_id"); |
| 3563 |
return monitor_iter; |
| 3564 |
} |
| 3565 |
|
| 3566 |
|
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
|
| 3573 |
|
| 3574 |
|
| 3575 |
|
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
|
| 3580 |
|
| 3581 |
|
| 3582 |
static void cc_unique_append(struct ast_str **str, const char *dialstring) |
| 3583 |
{ |
| 3584 |
char dialstring_search[AST_CHANNEL_NAME + 1]; |
| 3585 |
|
| 3586 |
if (ast_strlen_zero(dialstring)) { |
| 3587 |
|
| 3588 |
return; |
| 3589 |
} |
| 3590 |
snprintf(dialstring_search, sizeof(dialstring_search), "%s%c", dialstring, '&'); |
| 3591 |
if (strstr(ast_str_buffer(*str), dialstring_search)) { |
| 3592 |
return; |
| 3593 |
} |
| 3594 |
ast_str_append(str, 0, "%s", dialstring_search); |
| 3595 |
} |
| 3596 |
|
| 3597 |
|
| 3598 |
|
| 3599 |
|
| 3600 |
|
| 3601 |
|
| 3602 |
|
| 3603 |
|
| 3604 |
|
| 3605 |
|
| 3606 |
|
| 3607 |
|
| 3608 |
|
| 3609 |
|
| 3610 |
|
| 3611 |
static void build_cc_interfaces_chanvar(struct ast_cc_monitor *starting_point, struct ast_str **str) |
| 3612 |
{ |
| 3613 |
struct extension_monitor_pvt *extension_pvt; |
| 3614 |
struct extension_child_dialstring *child_dialstring; |
| 3615 |
struct ast_cc_monitor *monitor_iter = starting_point; |
| 3616 |
int top_level_id = starting_point->id; |
| 3617 |
size_t length; |
| 3618 |
|
| 3619 |
|
| 3620 |
ast_str_truncate(*str, 0); |
| 3621 |
|
| 3622 |
|
| 3623 |
|
| 3624 |
|
| 3625 |
|
| 3626 |
extension_pvt = starting_point->private_data; |
| 3627 |
AST_LIST_TRAVERSE(&extension_pvt->child_dialstrings, child_dialstring, next) { |
| 3628 |
if (child_dialstring->is_valid) { |
| 3629 |
cc_unique_append(str, child_dialstring->original_dialstring); |
| 3630 |
} |
| 3631 |
} |
| 3632 |
|
| 3633 |
|
| 3634 |
while ((monitor_iter = AST_LIST_NEXT(monitor_iter, next))) { |
| 3635 |
if (monitor_iter->parent_id == top_level_id) { |
| 3636 |
cc_unique_append(str, monitor_iter->dialstring); |
| 3637 |
} |
| 3638 |
} |
| 3639 |
|
| 3640 |
|
| 3641 |
|
| 3642 |
|
| 3643 |
length = ast_str_strlen(*str); |
| 3644 |
if (length) { |
| 3645 |
ast_str_truncate(*str, length - 1); |
| 3646 |
} |
| 3647 |
if (length <= 1) { |
| 3648 |
|
| 3649 |
ast_log(LOG_ERROR, "CC_INTERFACES is empty. starting device_name:'%s'\n", |
| 3650 |
starting_point->interface->device_name); |
| 3651 |
} |
| 3652 |
} |
| 3653 |
|
| 3654 |
int ast_cc_agent_set_interfaces_chanvar(struct ast_channel *chan) |
| 3655 |
{ |
| 3656 |
struct ast_datastore *recall_datastore; |
| 3657 |
struct cc_monitor_tree *interface_tree; |
| 3658 |
struct ast_cc_monitor *monitor; |
| 3659 |
struct cc_recall_ds_data *recall_data; |
| 3660 |
struct ast_str *str = ast_str_create(64); |
| 3661 |
int core_id; |
| 3662 |
|
| 3663 |
RETURN_IF_NOT_ENABLED(0); |
| 3664 |
|
| 3665 |
if (!str) { |
| 3666 |
return -1; |
| 3667 |
} |
| 3668 |
|
| 3669 |
ast_channel_lock(chan); |
| 3670 |
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) { |
| 3671 |
ast_channel_unlock(chan); |
| 3672 |
ast_free(str); |
| 3673 |
return -1; |
| 3674 |
} |
| 3675 |
recall_data = recall_datastore->data; |
| 3676 |
interface_tree = recall_data->interface_tree; |
| 3677 |
core_id = recall_data->core_id; |
| 3678 |
ast_channel_unlock(chan); |
| 3679 |
|
| 3680 |
AST_LIST_LOCK(interface_tree); |
| 3681 |
monitor = AST_LIST_FIRST(interface_tree); |
| 3682 |
build_cc_interfaces_chanvar(monitor, &str); |
| 3683 |
AST_LIST_UNLOCK(interface_tree); |
| 3684 |
|
| 3685 |
pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str)); |
| 3686 |
ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n", |
| 3687 |
core_id, ast_str_buffer(str)); |
| 3688 |
|
| 3689 |
ast_free(str); |
| 3690 |
return 0; |
| 3691 |
} |
| 3692 |
|
| 3693 |
int ast_set_cc_interfaces_chanvar(struct ast_channel *chan, const char * const extension) |
| 3694 |
{ |
| 3695 |
struct ast_datastore *recall_datastore; |
| 3696 |
struct cc_monitor_tree *interface_tree; |
| 3697 |
struct ast_cc_monitor *monitor_iter; |
| 3698 |
struct cc_recall_ds_data *recall_data; |
| 3699 |
struct ast_str *str = ast_str_create(64); |
| 3700 |
int core_id; |
| 3701 |
|
| 3702 |
RETURN_IF_NOT_ENABLED(0); |
| 3703 |
|
| 3704 |
if (!str) { |
| 3705 |
return -1; |
| 3706 |
} |
| 3707 |
|
| 3708 |
ast_channel_lock(chan); |
| 3709 |
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) { |
| 3710 |
ast_channel_unlock(chan); |
| 3711 |
ast_free(str); |
| 3712 |
return -1; |
| 3713 |
} |
| 3714 |
recall_data = recall_datastore->data; |
| 3715 |
interface_tree = recall_data->interface_tree; |
| 3716 |
core_id = recall_data->core_id; |
| 3717 |
ast_channel_unlock(chan); |
| 3718 |
|
| 3719 |
AST_LIST_LOCK(interface_tree); |
| 3720 |
AST_LIST_TRAVERSE(interface_tree, monitor_iter, next) { |
| 3721 |
if (!strcmp(monitor_iter->interface->device_name, extension)) { |
| 3722 |
break; |
| 3723 |
} |
| 3724 |
} |
| 3725 |
|
| 3726 |
if (!monitor_iter) { |
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
|
| 3731 |
AST_LIST_UNLOCK(interface_tree); |
| 3732 |
ast_free(str); |
| 3733 |
return -1; |
| 3734 |
} |
| 3735 |
|
| 3736 |
build_cc_interfaces_chanvar(monitor_iter, &str); |
| 3737 |
AST_LIST_UNLOCK(interface_tree); |
| 3738 |
|
| 3739 |
pbx_builtin_setvar_helper(chan, "CC_INTERFACES", ast_str_buffer(str)); |
| 3740 |
ast_log_dynamic_level(cc_logger_level, "Core %d: CC_INTERFACES set to %s\n", |
| 3741 |
core_id, ast_str_buffer(str)); |
| 3742 |
|
| 3743 |
ast_free(str); |
| 3744 |
return 0; |
| 3745 |
} |
| 3746 |
|
| 3747 |
void ast_ignore_cc(struct ast_channel *chan) |
| 3748 |
{ |
| 3749 |
struct ast_datastore *cc_datastore; |
| 3750 |
struct ast_datastore *cc_recall_datastore; |
| 3751 |
struct dialed_cc_interfaces *cc_interfaces; |
| 3752 |
struct cc_recall_ds_data *recall_cc_data; |
| 3753 |
|
| 3754 |
RETURN_IF_NOT_ENABLED(); |
| 3755 |
|
| 3756 |
ast_channel_lock(chan); |
| 3757 |
if ((cc_datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL))) { |
| 3758 |
cc_interfaces = cc_datastore->data; |
| 3759 |
cc_interfaces->ignore = 1; |
| 3760 |
} |
| 3761 |
|
| 3762 |
if ((cc_recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) { |
| 3763 |
recall_cc_data = cc_recall_datastore->data; |
| 3764 |
recall_cc_data->ignore = 1; |
| 3765 |
} |
| 3766 |
ast_channel_unlock(chan); |
| 3767 |
} |
| 3768 |
|
| 3769 |
static __attribute__((format(printf, 2, 3))) int cc_offer(const int core_id, const char * const debug, ...) |
| 3770 |
{ |
| 3771 |
va_list ap; |
| 3772 |
int res; |
| 3773 |
|
| 3774 |
va_start(ap, debug); |
| 3775 |
res = cc_request_state_change(CC_CALLER_OFFERED, core_id, debug, ap); |
| 3776 |
va_end(ap); |
| 3777 |
return res; |
| 3778 |
} |
| 3779 |
|
| 3780 |
int ast_cc_offer(struct ast_channel *caller_chan) |
| 3781 |
{ |
| 3782 |
int core_id; |
| 3783 |
int res = -1; |
| 3784 |
struct ast_datastore *datastore; |
| 3785 |
struct dialed_cc_interfaces *cc_interfaces; |
| 3786 |
char cc_is_offerable; |
| 3787 |
|
| 3788 |
RETURN_IF_NOT_ENABLED(0); |
| 3789 |
|
| 3790 |
ast_channel_lock(caller_chan); |
| 3791 |
if (!(datastore = ast_channel_datastore_find(caller_chan, &dialed_cc_interfaces_info, NULL))) { |
| 3792 |
ast_channel_unlock(caller_chan); |
| 3793 |
return res; |
| 3794 |
} |
| 3795 |
|
| 3796 |
cc_interfaces = datastore->data; |
| 3797 |
cc_is_offerable = cc_interfaces->is_original_caller; |
| 3798 |
core_id = cc_interfaces->core_id; |
| 3799 |
ast_channel_unlock(caller_chan); |
| 3800 |
|
| 3801 |
if (cc_is_offerable) { |
| 3802 |
res = cc_offer(core_id, "CC offered to caller %s", ast_channel_name(caller_chan)); |
| 3803 |
} |
| 3804 |
return res; |
| 3805 |
} |
| 3806 |
|
| 3807 |
int ast_cc_agent_accept_request(int core_id, const char * const debug, ...) |
| 3808 |
{ |
| 3809 |
va_list ap; |
| 3810 |
int res; |
| 3811 |
|
| 3812 |
RETURN_IF_NOT_ENABLED(0); |
| 3813 |
|
| 3814 |
va_start(ap, debug); |
| 3815 |
res = cc_request_state_change(CC_CALLER_REQUESTED, core_id, debug, ap); |
| 3816 |
va_end(ap); |
| 3817 |
return res; |
| 3818 |
} |
| 3819 |
|
| 3820 |
int ast_cc_monitor_request_acked(int core_id, const char * const debug, ...) |
| 3821 |
{ |
| 3822 |
va_list ap; |
| 3823 |
int res; |
| 3824 |
|
| 3825 |
RETURN_IF_NOT_ENABLED(0); |
| 3826 |
|
| 3827 |
va_start(ap, debug); |
| 3828 |
res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap); |
| 3829 |
va_end(ap); |
| 3830 |
return res; |
| 3831 |
} |
| 3832 |
|
| 3833 |
int ast_cc_monitor_callee_available(const int core_id, const char * const debug, ...) |
| 3834 |
{ |
| 3835 |
va_list ap; |
| 3836 |
int res; |
| 3837 |
|
| 3838 |
RETURN_IF_NOT_ENABLED(0); |
| 3839 |
|
| 3840 |
va_start(ap, debug); |
| 3841 |
res = cc_request_state_change(CC_CALLEE_READY, core_id, debug, ap); |
| 3842 |
va_end(ap); |
| 3843 |
return res; |
| 3844 |
} |
| 3845 |
|
| 3846 |
int ast_cc_agent_caller_busy(int core_id, const char * debug, ...) |
| 3847 |
{ |
| 3848 |
va_list ap; |
| 3849 |
int res; |
| 3850 |
|
| 3851 |
RETURN_IF_NOT_ENABLED(0); |
| 3852 |
|
| 3853 |
va_start(ap, debug); |
| 3854 |
res = cc_request_state_change(CC_CALLER_BUSY, core_id, debug, ap); |
| 3855 |
va_end(ap); |
| 3856 |
return res; |
| 3857 |
} |
| 3858 |
|
| 3859 |
int ast_cc_agent_caller_available(int core_id, const char * const debug, ...) |
| 3860 |
{ |
| 3861 |
va_list ap; |
| 3862 |
int res; |
| 3863 |
|
| 3864 |
RETURN_IF_NOT_ENABLED(0); |
| 3865 |
|
| 3866 |
va_start(ap, debug); |
| 3867 |
res = cc_request_state_change(CC_ACTIVE, core_id, debug, ap); |
| 3868 |
va_end(ap); |
| 3869 |
return res; |
| 3870 |
} |
| 3871 |
|
| 3872 |
int ast_cc_agent_recalling(int core_id, const char * const debug, ...) |
| 3873 |
{ |
| 3874 |
va_list ap; |
| 3875 |
int res; |
| 3876 |
|
| 3877 |
RETURN_IF_NOT_ENABLED(0); |
| 3878 |
|
| 3879 |
va_start(ap, debug); |
| 3880 |
res = cc_request_state_change(CC_RECALLING, core_id, debug, ap); |
| 3881 |
va_end(ap); |
| 3882 |
return res; |
| 3883 |
} |
| 3884 |
|
| 3885 |
int ast_cc_completed(struct ast_channel *chan, const char * const debug, ...) |
| 3886 |
{ |
| 3887 |
struct ast_datastore *recall_datastore; |
| 3888 |
struct cc_recall_ds_data *recall_data; |
| 3889 |
int core_id; |
| 3890 |
va_list ap; |
| 3891 |
int res; |
| 3892 |
|
| 3893 |
RETURN_IF_NOT_ENABLED(0); |
| 3894 |
|
| 3895 |
ast_channel_lock(chan); |
| 3896 |
if (!(recall_datastore = ast_channel_datastore_find(chan, &recall_ds_info, NULL))) { |
| 3897 |
|
| 3898 |
ast_channel_unlock(chan); |
| 3899 |
return -1; |
| 3900 |
} |
| 3901 |
recall_data = recall_datastore->data; |
| 3902 |
if (recall_data->nested || recall_data->ignore) { |
| 3903 |
|
| 3904 |
|
| 3905 |
|
| 3906 |
|
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
|
| 3912 |
|
| 3913 |
ast_channel_unlock(chan); |
| 3914 |
return -1; |
| 3915 |
} |
| 3916 |
core_id = recall_data->core_id; |
| 3917 |
ast_channel_unlock(chan); |
| 3918 |
va_start(ap, debug); |
| 3919 |
res = cc_request_state_change(CC_COMPLETE, core_id, debug, ap); |
| 3920 |
va_end(ap); |
| 3921 |
return res; |
| 3922 |
} |
| 3923 |
|
| 3924 |
int ast_cc_failed(int core_id, const char * const debug, ...) |
| 3925 |
{ |
| 3926 |
va_list ap; |
| 3927 |
int res; |
| 3928 |
|
| 3929 |
RETURN_IF_NOT_ENABLED(0); |
| 3930 |
|
| 3931 |
va_start(ap, debug); |
| 3932 |
res = cc_request_state_change(CC_FAILED, core_id, debug, ap); |
| 3933 |
va_end(ap); |
| 3934 |
return res; |
| 3935 |
} |
| 3936 |
|
| 3937 |
struct ast_cc_monitor_failure_data { |
| 3938 |
const char *device_name; |
| 3939 |
char *debug; |
| 3940 |
int core_id; |
| 3941 |
}; |
| 3942 |
|
| 3943 |
static int cc_monitor_failed(void *data) |
| 3944 |
{ |
| 3945 |
struct ast_cc_monitor_failure_data *failure_data = data; |
| 3946 |
struct cc_core_instance *core_instance; |
| 3947 |
struct ast_cc_monitor *monitor_iter; |
| 3948 |
|
| 3949 |
core_instance = find_cc_core_instance(failure_data->core_id); |
| 3950 |
if (!core_instance) { |
| 3951 |
|
| 3952 |
ast_log_dynamic_level(cc_logger_level, |
| 3953 |
"Core %d: Could not find core instance for device %s '%s'\n", |
| 3954 |
failure_data->core_id, failure_data->device_name, failure_data->debug); |
| 3955 |
ast_free((char *) failure_data->device_name); |
| 3956 |
ast_free((char *) failure_data->debug); |
| 3957 |
ast_free(failure_data); |
| 3958 |
return -1; |
| 3959 |
} |
| 3960 |
|
| 3961 |
AST_LIST_LOCK(core_instance->monitors); |
| 3962 |
AST_LIST_TRAVERSE_SAFE_BEGIN(core_instance->monitors, monitor_iter, next) { |
| 3963 |
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 3964 |
if (!strcmp(monitor_iter->interface->device_name, failure_data->device_name)) { |
| 3965 |
AST_LIST_REMOVE_CURRENT(next); |
| 3966 |
cc_extension_monitor_change_is_valid(core_instance, monitor_iter->parent_id, |
| 3967 |
monitor_iter->interface->device_name, 1); |
| 3968 |
monitor_iter->callbacks->cancel_available_timer(monitor_iter, &monitor_iter->available_timer_id); |
| 3969 |
cc_publish_monitorfailed(monitor_iter->core_id, monitor_iter->interface->device_name); |
| 3970 |
cc_unref(monitor_iter, "Monitor reported failure. Unref list's reference."); |
| 3971 |
} |
| 3972 |
} |
| 3973 |
} |
| 3974 |
AST_LIST_TRAVERSE_SAFE_END; |
| 3975 |
|
| 3976 |
if (!has_device_monitors(core_instance)) { |
| 3977 |
ast_cc_failed(core_instance->core_id, "All monitors have failed\n"); |
| 3978 |
} |
| 3979 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 3980 |
cc_unref(core_instance, "Finished with core_instance in cc_monitor_failed\n"); |
| 3981 |
|
| 3982 |
ast_free((char *) failure_data->device_name); |
| 3983 |
ast_free((char *) failure_data->debug); |
| 3984 |
ast_free(failure_data); |
| 3985 |
return 0; |
| 3986 |
} |
| 3987 |
|
| 3988 |
int ast_cc_monitor_failed(int core_id, const char *const monitor_name, const char * const debug, ...) |
| 3989 |
{ |
| 3990 |
struct ast_cc_monitor_failure_data *failure_data; |
| 3991 |
int res; |
| 3992 |
va_list ap; |
| 3993 |
|
| 3994 |
RETURN_IF_NOT_ENABLED(0); |
| 3995 |
|
| 3996 |
if (!(failure_data = ast_calloc(1, sizeof(*failure_data)))) { |
| 3997 |
return -1; |
| 3998 |
} |
| 3999 |
|
| 4000 |
if (!(failure_data->device_name = ast_strdup(monitor_name))) { |
| 4001 |
ast_free(failure_data); |
| 4002 |
return -1; |
| 4003 |
} |
| 4004 |
|
| 4005 |
va_start(ap, debug); |
| 4006 |
if (ast_vasprintf(&failure_data->debug, debug, ap) == -1) { |
| 4007 |
va_end(ap); |
| 4008 |
ast_free((char *)failure_data->device_name); |
| 4009 |
ast_free(failure_data); |
| 4010 |
return -1; |
| 4011 |
} |
| 4012 |
va_end(ap); |
| 4013 |
|
| 4014 |
failure_data->core_id = core_id; |
| 4015 |
|
| 4016 |
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_monitor_failed, failure_data); |
| 4017 |
if (res) { |
| 4018 |
ast_free((char *)failure_data->device_name); |
| 4019 |
ast_free((char *)failure_data->debug); |
| 4020 |
ast_free(failure_data); |
| 4021 |
} |
| 4022 |
return res; |
| 4023 |
} |
| 4024 |
|
| 4025 |
static int cc_status_request(void *data) |
| 4026 |
{ |
| 4027 |
struct cc_core_instance *core_instance= data; |
| 4028 |
int res; |
| 4029 |
|
| 4030 |
res = core_instance->agent->callbacks->status_request(core_instance->agent); |
| 4031 |
cc_unref(core_instance, "Status request finished. Unref core instance"); |
| 4032 |
return res; |
| 4033 |
} |
| 4034 |
|
| 4035 |
int ast_cc_monitor_status_request(int core_id) |
| 4036 |
{ |
| 4037 |
int res; |
| 4038 |
struct cc_core_instance *core_instance = find_cc_core_instance(core_id); |
| 4039 |
|
| 4040 |
RETURN_IF_NOT_ENABLED(0); |
| 4041 |
|
| 4042 |
if (!core_instance) { |
| 4043 |
return -1; |
| 4044 |
} |
| 4045 |
|
| 4046 |
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_request, core_instance); |
| 4047 |
if (res) { |
| 4048 |
cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed"); |
| 4049 |
} |
| 4050 |
return res; |
| 4051 |
} |
| 4052 |
|
| 4053 |
static int cc_stop_ringing(void *data) |
| 4054 |
{ |
| 4055 |
struct cc_core_instance *core_instance = data; |
| 4056 |
int res = 0; |
| 4057 |
|
| 4058 |
if (core_instance->agent->callbacks->stop_ringing) { |
| 4059 |
res = core_instance->agent->callbacks->stop_ringing(core_instance->agent); |
| 4060 |
} |
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
|
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
ast_cc_monitor_request_acked(core_instance->core_id, "Agent %s asked to stop ringing. Be prepared to be recalled again.", |
| 4069 |
core_instance->agent->device_name); |
| 4070 |
cc_unref(core_instance, "Stop ringing finished. Unref core_instance"); |
| 4071 |
return res; |
| 4072 |
} |
| 4073 |
|
| 4074 |
int ast_cc_monitor_stop_ringing(int core_id) |
| 4075 |
{ |
| 4076 |
int res; |
| 4077 |
struct cc_core_instance *core_instance = find_cc_core_instance(core_id); |
| 4078 |
|
| 4079 |
RETURN_IF_NOT_ENABLED(0); |
| 4080 |
|
| 4081 |
if (!core_instance) { |
| 4082 |
return -1; |
| 4083 |
} |
| 4084 |
|
| 4085 |
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_stop_ringing, core_instance); |
| 4086 |
if (res) { |
| 4087 |
cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed"); |
| 4088 |
} |
| 4089 |
return res; |
| 4090 |
} |
| 4091 |
|
| 4092 |
static int cc_party_b_free(void *data) |
| 4093 |
{ |
| 4094 |
struct cc_core_instance *core_instance = data; |
| 4095 |
int res = 0; |
| 4096 |
|
| 4097 |
if (core_instance->agent->callbacks->party_b_free) { |
| 4098 |
res = core_instance->agent->callbacks->party_b_free(core_instance->agent); |
| 4099 |
} |
| 4100 |
cc_unref(core_instance, "Party B free finished. Unref core_instance"); |
| 4101 |
return res; |
| 4102 |
} |
| 4103 |
|
| 4104 |
int ast_cc_monitor_party_b_free(int core_id) |
| 4105 |
{ |
| 4106 |
int res; |
| 4107 |
struct cc_core_instance *core_instance = find_cc_core_instance(core_id); |
| 4108 |
|
| 4109 |
RETURN_IF_NOT_ENABLED(0); |
| 4110 |
|
| 4111 |
if (!core_instance) { |
| 4112 |
return -1; |
| 4113 |
} |
| 4114 |
|
| 4115 |
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_party_b_free, core_instance); |
| 4116 |
if (res) { |
| 4117 |
cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed"); |
| 4118 |
} |
| 4119 |
return res; |
| 4120 |
} |
| 4121 |
|
| 4122 |
struct cc_status_response_args { |
| 4123 |
struct cc_core_instance *core_instance; |
| 4124 |
enum ast_device_state devstate; |
| 4125 |
}; |
| 4126 |
|
| 4127 |
static int cc_status_response(void *data) |
| 4128 |
{ |
| 4129 |
struct cc_status_response_args *args = data; |
| 4130 |
struct cc_core_instance *core_instance = args->core_instance; |
| 4131 |
struct ast_cc_monitor *monitor_iter; |
| 4132 |
enum ast_device_state devstate = args->devstate; |
| 4133 |
|
| 4134 |
ast_free(args); |
| 4135 |
|
| 4136 |
AST_LIST_LOCK(core_instance->monitors); |
| 4137 |
AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) { |
| 4138 |
if (monitor_iter->interface->monitor_class == AST_CC_DEVICE_MONITOR && |
| 4139 |
monitor_iter->callbacks->status_response) { |
| 4140 |
monitor_iter->callbacks->status_response(monitor_iter, devstate); |
| 4141 |
} |
| 4142 |
} |
| 4143 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 4144 |
cc_unref(core_instance, "Status response finished. Unref core instance"); |
| 4145 |
return 0; |
| 4146 |
} |
| 4147 |
|
| 4148 |
int ast_cc_agent_status_response(int core_id, enum ast_device_state devstate) |
| 4149 |
{ |
| 4150 |
struct cc_status_response_args *args; |
| 4151 |
struct cc_core_instance *core_instance; |
| 4152 |
int res; |
| 4153 |
|
| 4154 |
RETURN_IF_NOT_ENABLED(0); |
| 4155 |
|
| 4156 |
args = ast_calloc(1, sizeof(*args)); |
| 4157 |
if (!args) { |
| 4158 |
return -1; |
| 4159 |
} |
| 4160 |
|
| 4161 |
core_instance = find_cc_core_instance(core_id); |
| 4162 |
if (!core_instance) { |
| 4163 |
ast_free(args); |
| 4164 |
return -1; |
| 4165 |
} |
| 4166 |
|
| 4167 |
args->core_instance = core_instance; |
| 4168 |
args->devstate = devstate; |
| 4169 |
|
| 4170 |
res = ast_taskprocessor_push(cc_core_taskprocessor, cc_status_response, args); |
| 4171 |
if (res) { |
| 4172 |
cc_unref(core_instance, "Unref core instance. ast_taskprocessor_push failed"); |
| 4173 |
ast_free(args); |
| 4174 |
} |
| 4175 |
return res; |
| 4176 |
} |
| 4177 |
|
| 4178 |
static int cc_build_payload(struct ast_channel *chan, struct ast_cc_config_params *cc_params, |
| 4179 |
const char *monitor_type, const char * const device_name, const char * dialstring, |
| 4180 |
enum ast_cc_service_type service, void *private_data, struct cc_control_payload *payload) |
| 4181 |
{ |
| 4182 |
struct ast_datastore *datastore; |
| 4183 |
struct dialed_cc_interfaces *cc_interfaces; |
| 4184 |
int dial_parent_id; |
| 4185 |
|
| 4186 |
ast_channel_lock(chan); |
| 4187 |
datastore = ast_channel_datastore_find(chan, &dialed_cc_interfaces_info, NULL); |
| 4188 |
if (!datastore) { |
| 4189 |
ast_channel_unlock(chan); |
| 4190 |
return -1; |
| 4191 |
} |
| 4192 |
cc_interfaces = datastore->data; |
| 4193 |
dial_parent_id = cc_interfaces->dial_parent_id; |
| 4194 |
ast_channel_unlock(chan); |
| 4195 |
|
| 4196 |
payload->monitor_type = monitor_type; |
| 4197 |
payload->private_data = private_data; |
| 4198 |
payload->service = service; |
| 4199 |
ast_cc_copy_config_params(&payload->config_params, cc_params); |
| 4200 |
payload->parent_interface_id = dial_parent_id; |
| 4201 |
ast_copy_string(payload->device_name, device_name, sizeof(payload->device_name)); |
| 4202 |
ast_copy_string(payload->dialstring, dialstring, sizeof(payload->dialstring)); |
| 4203 |
return 0; |
| 4204 |
} |
| 4205 |
|
| 4206 |
int ast_queue_cc_frame(struct ast_channel *chan, const char *monitor_type, |
| 4207 |
const char * const dialstring, enum ast_cc_service_type service, void *private_data) |
| 4208 |
{ |
| 4209 |
struct ast_frame frame = {0,}; |
| 4210 |
char device_name[AST_CHANNEL_NAME]; |
| 4211 |
int retval; |
| 4212 |
struct ast_cc_config_params *cc_params; |
| 4213 |
|
| 4214 |
RETURN_IF_NOT_ENABLED(0); |
| 4215 |
|
| 4216 |
cc_params = ast_channel_get_cc_config_params(chan); |
| 4217 |
if (!cc_params) { |
| 4218 |
return -1; |
| 4219 |
} |
| 4220 |
ast_channel_get_device_name(chan, device_name, sizeof(device_name)); |
| 4221 |
if (ast_cc_monitor_count(device_name, monitor_type) >= ast_get_cc_max_monitors(cc_params)) { |
| 4222 |
ast_log(LOG_NOTICE, "Not queuing a CC frame for device %s since it already has its maximum monitors allocated\n", device_name); |
| 4223 |
return -1; |
| 4224 |
} |
| 4225 |
|
| 4226 |
if (ast_cc_build_frame(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, &frame)) { |
| 4227 |
|
| 4228 |
return -1; |
| 4229 |
} |
| 4230 |
retval = ast_queue_frame(chan, &frame); |
| 4231 |
ast_frfree(&frame); |
| 4232 |
return retval; |
| 4233 |
} |
| 4234 |
|
| 4235 |
int ast_cc_build_frame(struct ast_channel *chan, struct ast_cc_config_params *cc_params, |
| 4236 |
const char *monitor_type, const char * const device_name, |
| 4237 |
const char * const dialstring, enum ast_cc_service_type service, void *private_data, |
| 4238 |
struct ast_frame *frame) |
| 4239 |
{ |
| 4240 |
struct cc_control_payload *payload; |
| 4241 |
|
| 4242 |
RETURN_IF_NOT_ENABLED(0); |
| 4243 |
|
| 4244 |
payload = ast_calloc(1, sizeof(*payload)); |
| 4245 |
if (!payload) { |
| 4246 |
return -1; |
| 4247 |
} |
| 4248 |
if (cc_build_payload(chan, cc_params, monitor_type, device_name, dialstring, service, private_data, payload)) { |
| 4249 |
|
| 4250 |
ast_free(payload); |
| 4251 |
return -1; |
| 4252 |
} |
| 4253 |
frame->frametype = AST_FRAME_CONTROL; |
| 4254 |
frame->subclass.integer = AST_CONTROL_CC; |
| 4255 |
frame->data.ptr = payload; |
| 4256 |
frame->datalen = sizeof(*payload); |
| 4257 |
frame->mallocd = AST_MALLOCD_DATA; |
| 4258 |
return 0; |
| 4259 |
} |
| 4260 |
|
| 4261 |
void ast_cc_call_failed(struct ast_channel *incoming, struct ast_channel *outgoing, const char * const dialstring) |
| 4262 |
{ |
| 4263 |
char device_name[AST_CHANNEL_NAME]; |
| 4264 |
struct cc_control_payload payload; |
| 4265 |
struct ast_cc_config_params *cc_params; |
| 4266 |
|
| 4267 |
RETURN_IF_NOT_ENABLED(); |
| 4268 |
|
| 4269 |
if (ast_channel_hangupcause(outgoing) != AST_CAUSE_BUSY && ast_channel_hangupcause(outgoing) != AST_CAUSE_CONGESTION) { |
| 4270 |
|
| 4271 |
|
| 4272 |
|
| 4273 |
return; |
| 4274 |
} |
| 4275 |
|
| 4276 |
cc_params = ast_channel_get_cc_config_params(outgoing); |
| 4277 |
if (!cc_params) { |
| 4278 |
return; |
| 4279 |
} |
| 4280 |
if (ast_get_cc_monitor_policy(cc_params) != AST_CC_MONITOR_GENERIC) { |
| 4281 |
|
| 4282 |
|
| 4283 |
|
| 4284 |
return; |
| 4285 |
} |
| 4286 |
|
| 4287 |
ast_channel_get_device_name(outgoing, device_name, sizeof(device_name)); |
| 4288 |
if (cc_build_payload(outgoing, cc_params, AST_CC_GENERIC_MONITOR_TYPE, device_name, |
| 4289 |
dialstring, AST_CC_CCBS, NULL, &payload)) { |
| 4290 |
|
| 4291 |
return; |
| 4292 |
} |
| 4293 |
ast_handle_cc_control_frame(incoming, outgoing, &payload); |
| 4294 |
} |
| 4295 |
|
| 4296 |
void ast_cc_busy_interface(struct ast_channel *inbound, struct ast_cc_config_params *cc_params, |
| 4297 |
const char *monitor_type, const char * const device_name, const char * const dialstring, void *private_data) |
| 4298 |
{ |
| 4299 |
struct cc_control_payload payload; |
| 4300 |
|
| 4301 |
RETURN_IF_NOT_ENABLED(); |
| 4302 |
|
| 4303 |
if (cc_build_payload(inbound, cc_params, monitor_type, device_name, dialstring, AST_CC_CCBS, private_data, &payload)) { |
| 4304 |
|
| 4305 |
call_destructor_with_no_monitor(monitor_type, private_data); |
| 4306 |
return; |
| 4307 |
} |
| 4308 |
ast_handle_cc_control_frame(inbound, NULL, &payload); |
| 4309 |
} |
| 4310 |
|
| 4311 |
int ast_cc_callback(struct ast_channel *inbound, const char * const tech, const char * const dest, ast_cc_callback_fn callback) |
| 4312 |
{ |
| 4313 |
const struct ast_channel_tech *chantech; |
| 4314 |
|
| 4315 |
RETURN_IF_NOT_ENABLED(0); |
| 4316 |
|
| 4317 |
chantech = ast_get_channel_tech(tech); |
| 4318 |
if (chantech && chantech->cc_callback) { |
| 4319 |
chantech->cc_callback(inbound, dest, callback); |
| 4320 |
} |
| 4321 |
|
| 4322 |
return 0; |
| 4323 |
} |
| 4324 |
|
| 4325 |
static const char *ccreq_app = "CallCompletionRequest"; |
| 4326 |
|
| 4327 |
static int ccreq_exec(struct ast_channel *chan, const char *data) |
| 4328 |
{ |
| 4329 |
struct cc_core_instance *core_instance; |
| 4330 |
char device_name[AST_CHANNEL_NAME]; |
| 4331 |
unsigned long match_flags; |
| 4332 |
int res; |
| 4333 |
|
| 4334 |
ast_channel_get_device_name(chan, device_name, sizeof(device_name)); |
| 4335 |
|
| 4336 |
match_flags = MATCH_NO_REQUEST; |
| 4337 |
if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionRequest"))) { |
| 4338 |
ast_log_dynamic_level(cc_logger_level, "Couldn't find a core instance for caller %s\n", device_name); |
| 4339 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL"); |
| 4340 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NO_CORE_INSTANCE"); |
| 4341 |
return 0; |
| 4342 |
} |
| 4343 |
|
| 4344 |
ast_log_dynamic_level(cc_logger_level, "Core %d: Found core_instance for caller %s\n", |
| 4345 |
core_instance->core_id, device_name); |
| 4346 |
|
| 4347 |
if (strcmp(core_instance->agent->callbacks->type, "generic")) { |
| 4348 |
ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest is only for generic agent types.\n", |
| 4349 |
core_instance->core_id); |
| 4350 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL"); |
| 4351 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "NOT_GENERIC"); |
| 4352 |
cc_unref(core_instance, "Unref core_instance since CallCompletionRequest was called with native agent"); |
| 4353 |
return 0; |
| 4354 |
} |
| 4355 |
|
| 4356 |
if (!ast_cc_request_is_within_limits()) { |
| 4357 |
ast_log_dynamic_level(cc_logger_level, "Core %d: CallCompletionRequest failed. Too many requests in the system\n", |
| 4358 |
core_instance->core_id); |
| 4359 |
ast_cc_failed(core_instance->core_id, "Too many CC requests\n"); |
| 4360 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", "FAIL"); |
| 4361 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "TOO_MANY_REQUESTS"); |
| 4362 |
cc_unref(core_instance, "Unref core_instance since too many CC requests"); |
| 4363 |
return 0; |
| 4364 |
} |
| 4365 |
|
| 4366 |
res = ast_cc_agent_accept_request(core_instance->core_id, "CallCompletionRequest called by caller %s for core_id %d", device_name, core_instance->core_id); |
| 4367 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_RESULT", res ? "FAIL" : "SUCCESS"); |
| 4368 |
if (res) { |
| 4369 |
pbx_builtin_setvar_helper(chan, "CC_REQUEST_REASON", "UNSPECIFIED"); |
| 4370 |
} |
| 4371 |
|
| 4372 |
cc_unref(core_instance, "Done with CallCompletionRequest"); |
| 4373 |
return 0; |
| 4374 |
} |
| 4375 |
|
| 4376 |
static const char *cccancel_app = "CallCompletionCancel"; |
| 4377 |
|
| 4378 |
static int cccancel_exec(struct ast_channel *chan, const char *data) |
| 4379 |
{ |
| 4380 |
struct cc_core_instance *core_instance; |
| 4381 |
char device_name[AST_CHANNEL_NAME]; |
| 4382 |
unsigned long match_flags; |
| 4383 |
int res; |
| 4384 |
|
| 4385 |
ast_channel_get_device_name(chan, device_name, sizeof(device_name)); |
| 4386 |
|
| 4387 |
match_flags = MATCH_REQUEST; |
| 4388 |
if (!(core_instance = ao2_t_callback_data(cc_core_instances, 0, match_agent, device_name, &match_flags, "Find core instance for CallCompletionCancel"))) { |
| 4389 |
ast_log_dynamic_level(cc_logger_level, "Cannot find CC transaction to cancel for caller %s\n", device_name); |
| 4390 |
pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL"); |
| 4391 |
pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NO_CORE_INSTANCE"); |
| 4392 |
return 0; |
| 4393 |
} |
| 4394 |
|
| 4395 |
if (strcmp(core_instance->agent->callbacks->type, "generic")) { |
| 4396 |
ast_log(LOG_WARNING, "CallCompletionCancel may only be used for calls with a generic agent\n"); |
| 4397 |
cc_unref(core_instance, "Unref core instance found during CallCompletionCancel"); |
| 4398 |
pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", "FAIL"); |
| 4399 |
pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "NOT_GENERIC"); |
| 4400 |
return 0; |
| 4401 |
} |
| 4402 |
res = ast_cc_failed(core_instance->core_id, "Call completion request Cancelled for core ID %d by caller %s", |
| 4403 |
core_instance->core_id, device_name); |
| 4404 |
cc_unref(core_instance, "Unref core instance found during CallCompletionCancel"); |
| 4405 |
pbx_builtin_setvar_helper(chan, "CC_CANCEL_RESULT", res ? "FAIL" : "SUCCESS"); |
| 4406 |
if (res) { |
| 4407 |
pbx_builtin_setvar_helper(chan, "CC_CANCEL_REASON", "UNSPECIFIED"); |
| 4408 |
} |
| 4409 |
return 0; |
| 4410 |
} |
| 4411 |
|
| 4412 |
struct count_monitors_cb_data { |
| 4413 |
const char *device_name; |
| 4414 |
const char *monitor_type; |
| 4415 |
int count; |
| 4416 |
}; |
| 4417 |
|
| 4418 |
static int count_monitors_cb(void *obj, void *arg, int flags) |
| 4419 |
{ |
| 4420 |
struct cc_core_instance *core_instance = obj; |
| 4421 |
struct count_monitors_cb_data *cb_data = arg; |
| 4422 |
const char *device_name = cb_data->device_name; |
| 4423 |
const char *monitor_type = cb_data->monitor_type; |
| 4424 |
struct ast_cc_monitor *monitor_iter; |
| 4425 |
|
| 4426 |
AST_LIST_LOCK(core_instance->monitors); |
| 4427 |
AST_LIST_TRAVERSE(core_instance->monitors, monitor_iter, next) { |
| 4428 |
if (!strcmp(monitor_iter->interface->device_name, device_name) && |
| 4429 |
!strcmp(monitor_iter->interface->monitor_type, monitor_type)) { |
| 4430 |
cb_data->count++; |
| 4431 |
break; |
| 4432 |
} |
| 4433 |
} |
| 4434 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 4435 |
return 0; |
| 4436 |
} |
| 4437 |
|
| 4438 |
int ast_cc_monitor_count(const char * const name, const char * const type) |
| 4439 |
{ |
| 4440 |
struct count_monitors_cb_data data = {.device_name = name, .monitor_type = type,}; |
| 4441 |
|
| 4442 |
RETURN_IF_NOT_ENABLED(0); |
| 4443 |
|
| 4444 |
ao2_t_callback(cc_core_instances, OBJ_NODATA, count_monitors_cb, &data, "Counting agents"); |
| 4445 |
ast_log_dynamic_level(cc_logger_level, "Counted %d monitors\n", data.count); |
| 4446 |
return data.count; |
| 4447 |
} |
| 4448 |
|
| 4449 |
static void initialize_cc_max_requests(struct ast_config *cc_config) |
| 4450 |
{ |
| 4451 |
const char *cc_max_requests_str; |
| 4452 |
char *endptr; |
| 4453 |
|
| 4454 |
if (!(cc_max_requests_str = ast_variable_retrieve(cc_config, "general", "cc_max_requests"))) { |
| 4455 |
global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT; |
| 4456 |
return; |
| 4457 |
} |
| 4458 |
|
| 4459 |
global_cc_max_requests = strtol(cc_max_requests_str, &endptr, 10); |
| 4460 |
|
| 4461 |
if (!ast_strlen_zero(endptr)) { |
| 4462 |
ast_log(LOG_WARNING, "Invalid input given for cc_max_requests. Using default\n"); |
| 4463 |
global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT; |
| 4464 |
} |
| 4465 |
|
| 4466 |
return; |
| 4467 |
} |
| 4468 |
|
| 4469 |
|
| 4470 |
|
| 4471 |
|
| 4472 |
|
| 4473 |
static void initialize_cc_devstate_map_helper(struct ast_config *cc_config, enum cc_state state, const char *cc_setting) |
| 4474 |
{ |
| 4475 |
const char *cc_devstate_str; |
| 4476 |
enum ast_device_state this_devstate; |
| 4477 |
|
| 4478 |
if ((cc_devstate_str = ast_variable_retrieve(cc_config, "general", cc_setting))) { |
| 4479 |
this_devstate = ast_devstate_val(cc_devstate_str); |
| 4480 |
if (this_devstate != AST_DEVICE_UNKNOWN) { |
| 4481 |
cc_state_to_devstate_map[state] = this_devstate; |
| 4482 |
} |
| 4483 |
} |
| 4484 |
} |
| 4485 |
|
| 4486 |
|
| 4487 |
|
| 4488 |
|
| 4489 |
|
| 4490 |
|
| 4491 |
|
| 4492 |
|
| 4493 |
|
| 4494 |
|
| 4495 |
|
| 4496 |
|
| 4497 |
static void initialize_cc_devstate_map(struct ast_config *cc_config) |
| 4498 |
{ |
| 4499 |
initialize_cc_devstate_map_helper(cc_config, CC_AVAILABLE, "cc_available_devstate"); |
| 4500 |
initialize_cc_devstate_map_helper(cc_config, CC_CALLER_OFFERED, "cc_caller_offered_devstate"); |
| 4501 |
initialize_cc_devstate_map_helper(cc_config, CC_CALLER_REQUESTED, "cc_caller_requested_devstate"); |
| 4502 |
initialize_cc_devstate_map_helper(cc_config, CC_ACTIVE, "cc_active_devstate"); |
| 4503 |
initialize_cc_devstate_map_helper(cc_config, CC_CALLEE_READY, "cc_callee_ready_devstate"); |
| 4504 |
initialize_cc_devstate_map_helper(cc_config, CC_CALLER_BUSY, "cc_caller_busy_devstate"); |
| 4505 |
initialize_cc_devstate_map_helper(cc_config, CC_RECALLING, "cc_recalling_devstate"); |
| 4506 |
initialize_cc_devstate_map_helper(cc_config, CC_COMPLETE, "cc_complete_devstate"); |
| 4507 |
initialize_cc_devstate_map_helper(cc_config, CC_FAILED, "cc_failed_devstate"); |
| 4508 |
|
| 4509 |
} |
| 4510 |
|
| 4511 |
static int load_config(void) |
| 4512 |
{ |
| 4513 |
struct ast_flags config_flags = { 0, }; |
| 4514 |
const char *enabled = NULL; |
| 4515 |
struct ast_config *cc_config = ast_config_load2("ccss.conf", "ccss", config_flags); |
| 4516 |
|
| 4517 |
global_enabled = 1; |
| 4518 |
|
| 4519 |
if (!cc_config || cc_config == CONFIG_STATUS_FILEINVALID) { |
| 4520 |
ast_log(LOG_WARNING, "Could not find valid ccss.conf file. Using cc_max_requests and devstate defaults\n"); |
| 4521 |
global_cc_max_requests = GLOBAL_CC_MAX_REQUESTS_DEFAULT; |
| 4522 |
return 0; |
| 4523 |
} |
| 4524 |
|
| 4525 |
if ((enabled = ast_variable_retrieve(cc_config, "general", "enabled"))) { |
| 4526 |
global_enabled = ast_true(enabled); |
| 4527 |
} |
| 4528 |
|
| 4529 |
if (global_enabled) { |
| 4530 |
initialize_cc_max_requests(cc_config); |
| 4531 |
initialize_cc_devstate_map(cc_config); |
| 4532 |
} else { |
| 4533 |
ast_log(LOG_NOTICE, "CCSS disabled globally\n"); |
| 4534 |
} |
| 4535 |
ast_config_destroy(cc_config); |
| 4536 |
|
| 4537 |
return !global_enabled; |
| 4538 |
} |
| 4539 |
|
| 4540 |
int ast_cc_is_enabled(void) |
| 4541 |
{ |
| 4542 |
return global_enabled; |
| 4543 |
} |
| 4544 |
|
| 4545 |
static void cc_cli_print_monitor_stats(struct ast_cc_monitor *monitor, int fd, int parent_id) |
| 4546 |
{ |
| 4547 |
struct ast_cc_monitor *child_monitor_iter = monitor; |
| 4548 |
if (!monitor) { |
| 4549 |
return; |
| 4550 |
} |
| 4551 |
|
| 4552 |
ast_cli(fd, "\t\t|-->%s", monitor->interface->device_name); |
| 4553 |
if (monitor->interface->monitor_class == AST_CC_DEVICE_MONITOR) { |
| 4554 |
ast_cli(fd, "(%s)", cc_service_to_string(monitor->service_offered)); |
| 4555 |
} |
| 4556 |
ast_cli(fd, "\n"); |
| 4557 |
|
| 4558 |
while ((child_monitor_iter = AST_LIST_NEXT(child_monitor_iter, next))) { |
| 4559 |
if (child_monitor_iter->parent_id == monitor->id) { |
| 4560 |
cc_cli_print_monitor_stats(child_monitor_iter, fd, child_monitor_iter->id); |
| 4561 |
} |
| 4562 |
} |
| 4563 |
} |
| 4564 |
|
| 4565 |
static int print_stats_cb(void *obj, void *arg, int flags) |
| 4566 |
{ |
| 4567 |
int *cli_fd = arg; |
| 4568 |
struct cc_core_instance *core_instance = obj; |
| 4569 |
|
| 4570 |
ast_cli(*cli_fd, "%d\t\t%s\t\t%s\n", core_instance->core_id, core_instance->agent->device_name, |
| 4571 |
cc_state_to_string(core_instance->current_state)); |
| 4572 |
AST_LIST_LOCK(core_instance->monitors); |
| 4573 |
cc_cli_print_monitor_stats(AST_LIST_FIRST(core_instance->monitors), *cli_fd, 0); |
| 4574 |
AST_LIST_UNLOCK(core_instance->monitors); |
| 4575 |
return 0; |
| 4576 |
} |
| 4577 |
|
| 4578 |
static int cc_cli_output_status(void *data) |
| 4579 |
{ |
| 4580 |
int *cli_fd = data; |
| 4581 |
int count = ao2_container_count(cc_core_instances); |
| 4582 |
|
| 4583 |
if (!count) { |
| 4584 |
ast_cli(*cli_fd, "There are currently no active call completion transactions\n"); |
| 4585 |
} else { |
| 4586 |
ast_cli(*cli_fd, "%d Call completion transactions\n", count); |
| 4587 |
ast_cli(*cli_fd, "Core ID\t\tCaller\t\t\t\tStatus\n"); |
| 4588 |
ast_cli(*cli_fd, "----------------------------------------------------------------------------\n"); |
| 4589 |
ao2_t_callback(cc_core_instances, OBJ_NODATA, print_stats_cb, cli_fd, "Printing stats to CLI"); |
| 4590 |
} |
| 4591 |
ast_free(cli_fd); |
| 4592 |
return 0; |
| 4593 |
} |
| 4594 |
|
| 4595 |
static char *handle_cc_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4596 |
{ |
| 4597 |
int *cli_fd; |
| 4598 |
|
| 4599 |
switch (cmd) { |
| 4600 |
case CLI_INIT: |
| 4601 |
e->command = "cc report status"; |
| 4602 |
e->usage = |
| 4603 |
"Usage: cc report status\n" |
| 4604 |
" Report the current status of any ongoing CC transactions\n"; |
| 4605 |
return NULL; |
| 4606 |
case CLI_GENERATE: |
| 4607 |
return NULL; |
| 4608 |
} |
| 4609 |
|
| 4610 |
if (a->argc != 3) { |
| 4611 |
return CLI_SHOWUSAGE; |
| 4612 |
} |
| 4613 |
|
| 4614 |
cli_fd = ast_malloc(sizeof(*cli_fd)); |
| 4615 |
if (!cli_fd) { |
| 4616 |
return CLI_FAILURE; |
| 4617 |
} |
| 4618 |
|
| 4619 |
*cli_fd = a->fd; |
| 4620 |
|
| 4621 |
if (ast_taskprocessor_push(cc_core_taskprocessor, cc_cli_output_status, cli_fd)) { |
| 4622 |
ast_free(cli_fd); |
| 4623 |
return CLI_FAILURE; |
| 4624 |
} |
| 4625 |
return CLI_SUCCESS; |
| 4626 |
} |
| 4627 |
|
| 4628 |
static int kill_cores(void *obj, void *arg, int flags) |
| 4629 |
{ |
| 4630 |
int *core_id = arg; |
| 4631 |
struct cc_core_instance *core_instance = obj; |
| 4632 |
|
| 4633 |
if (!core_id || (core_instance->core_id == *core_id)) { |
| 4634 |
ast_cc_failed(core_instance->core_id, "CC transaction canceled administratively\n"); |
| 4635 |
} |
| 4636 |
return 0; |
| 4637 |
} |
| 4638 |
|
| 4639 |
static char *complete_core_id(const char *word) |
| 4640 |
{ |
| 4641 |
int wordlen = strlen(word); |
| 4642 |
struct ao2_iterator core_iter = ao2_iterator_init(cc_core_instances, 0); |
| 4643 |
struct cc_core_instance *core_instance; |
| 4644 |
|
| 4645 |
for (; (core_instance = ao2_t_iterator_next(&core_iter, "Next core instance")); |
| 4646 |
cc_unref(core_instance, "CLI tab completion iteration")) { |
| 4647 |
char core_id_str[20]; |
| 4648 |
snprintf(core_id_str, sizeof(core_id_str), "%d", core_instance->core_id); |
| 4649 |
if (!strncmp(word, core_id_str, wordlen)) { |
| 4650 |
if (ast_cli_completion_add(ast_strdup(core_id_str))) { |
| 4651 |
cc_unref(core_instance, "Found a matching core ID for CLI tab-completion"); |
| 4652 |
break; |
| 4653 |
} |
| 4654 |
} |
| 4655 |
} |
| 4656 |
ao2_iterator_destroy(&core_iter); |
| 4657 |
|
| 4658 |
return NULL; |
| 4659 |
} |
| 4660 |
|
| 4661 |
static char *handle_cc_kill(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4662 |
{ |
| 4663 |
switch (cmd) { |
| 4664 |
case CLI_INIT: |
| 4665 |
e->command = "cc cancel [core|all]"; |
| 4666 |
e->usage = |
| 4667 |
"Usage: cc cancel can be used in two ways.\n" |
| 4668 |
" 1. 'cc cancel core [core ID]' will cancel the CC transaction with\n" |
| 4669 |
" core ID equal to the specified core ID.\n" |
| 4670 |
" 2. 'cc cancel all' will cancel all active CC transactions.\n"; |
| 4671 |
return NULL; |
| 4672 |
case CLI_GENERATE: |
| 4673 |
if (a->pos == 3 && !strcasecmp(a->argv[2], "core")) { |
| 4674 |
return complete_core_id(a->word); |
| 4675 |
} |
| 4676 |
return NULL; |
| 4677 |
} |
| 4678 |
|
| 4679 |
if (a->argc == 4) { |
| 4680 |
int core_id; |
| 4681 |
char *endptr; |
| 4682 |
if (strcasecmp(a->argv[2], "core")) { |
| 4683 |
return CLI_SHOWUSAGE; |
| 4684 |
} |
| 4685 |
core_id = strtol(a->argv[3], &endptr, 10); |
| 4686 |
if ((errno != 0 && core_id == 0) || (endptr == a->argv[3])) { |
| 4687 |
return CLI_SHOWUSAGE; |
| 4688 |
} |
| 4689 |
ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, &core_id, "CLI Killing Core Id"); |
| 4690 |
} else if (a->argc == 3) { |
| 4691 |
if (strcasecmp(a->argv[2], "all")) { |
| 4692 |
return CLI_SHOWUSAGE; |
| 4693 |
} |
| 4694 |
ao2_t_callback(cc_core_instances, OBJ_NODATA, kill_cores, NULL, "CLI Killing all CC cores"); |
| 4695 |
} else { |
| 4696 |
return CLI_SHOWUSAGE; |
| 4697 |
} |
| 4698 |
|
| 4699 |
return CLI_SUCCESS; |
| 4700 |
} |
| 4701 |
|
| 4702 |
static struct ast_cli_entry cc_cli[] = { |
| 4703 |
AST_CLI_DEFINE(handle_cc_status, "Reports CC stats"), |
| 4704 |
AST_CLI_DEFINE(handle_cc_kill, "Kill a CC transaction"), |
| 4705 |
}; |
| 4706 |
|
| 4707 |
static int unload_module(void) |
| 4708 |
{ |
| 4709 |
if (!global_enabled) { |
| 4710 |
return 0; |
| 4711 |
} |
| 4712 |
|
| 4713 |
ast_devstate_prov_del("ccss"); |
| 4714 |
ast_cc_agent_unregister(&generic_agent_callbacks); |
| 4715 |
ast_cc_monitor_unregister(&generic_monitor_cbs); |
| 4716 |
ast_unregister_application(cccancel_app); |
| 4717 |
ast_unregister_application(ccreq_app); |
| 4718 |
ast_logger_unregister_level(CC_LOGGER_LEVEL_NAME); |
| 4719 |
ast_cli_unregister_multiple(cc_cli, ARRAY_LEN(cc_cli)); |
| 4720 |
|
| 4721 |
if (cc_sched_context) { |
| 4722 |
ast_sched_context_destroy(cc_sched_context); |
| 4723 |
cc_sched_context = NULL; |
| 4724 |
} |
| 4725 |
if (cc_core_taskprocessor) { |
| 4726 |
cc_core_taskprocessor = ast_taskprocessor_unreference(cc_core_taskprocessor); |
| 4727 |
} |
| 4728 |
|
| 4729 |
if (cc_core_instances) { |
| 4730 |
ao2_t_ref(cc_core_instances, -1, "Unref cc_core_instances container in cc_shutdown"); |
| 4731 |
cc_core_instances = NULL; |
| 4732 |
} |
| 4733 |
if (generic_monitors) { |
| 4734 |
ao2_t_ref(generic_monitors, -1, "Unref generic_monitor container in cc_shutdown"); |
| 4735 |
generic_monitors = NULL; |
| 4736 |
} |
| 4737 |
|
| 4738 |
return 0; |
| 4739 |
} |
| 4740 |
|
| 4741 |
static int load_module(void) |
| 4742 |
{ |
| 4743 |
int res; |
| 4744 |
|
| 4745 |
res = load_config(); |
| 4746 |
if (res != 0) { |
| 4747 |
return AST_MODULE_LOAD_DECLINE; |
| 4748 |
} |
| 4749 |
|
| 4750 |
cc_core_instances = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 4751 |
CC_CORE_INSTANCES_BUCKETS, |
| 4752 |
cc_core_instance_hash_fn, NULL, cc_core_instance_cmp_fn, |
| 4753 |
"Create core instance container"); |
| 4754 |
if (!cc_core_instances) { |
| 4755 |
return AST_MODULE_LOAD_FAILURE; |
| 4756 |
} |
| 4757 |
|
| 4758 |
generic_monitors = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 4759 |
CC_CORE_INSTANCES_BUCKETS, |
| 4760 |
generic_monitor_instance_list_hash_fn, NULL, generic_monitor_instance_list_cmp_fn, |
| 4761 |
"Create generic monitor container"); |
| 4762 |
if (!generic_monitors) { |
| 4763 |
return AST_MODULE_LOAD_FAILURE; |
| 4764 |
} |
| 4765 |
if (!(cc_core_taskprocessor = ast_taskprocessor_get("CCSS_core", TPS_REF_DEFAULT))) { |
| 4766 |
return AST_MODULE_LOAD_FAILURE; |
| 4767 |
} |
| 4768 |
if (!(cc_sched_context = ast_sched_context_create())) { |
| 4769 |
return AST_MODULE_LOAD_FAILURE; |
| 4770 |
} |
| 4771 |
if (ast_sched_start_thread(cc_sched_context)) { |
| 4772 |
return AST_MODULE_LOAD_FAILURE; |
| 4773 |
} |
| 4774 |
res = ast_register_application2(ccreq_app, ccreq_exec, NULL, NULL, NULL); |
| 4775 |
res |= ast_register_application2(cccancel_app, cccancel_exec, NULL, NULL, NULL); |
| 4776 |
res |= ast_cc_monitor_register(&generic_monitor_cbs); |
| 4777 |
res |= ast_cc_agent_register(&generic_agent_callbacks); |
| 4778 |
|
| 4779 |
ast_cli_register_multiple(cc_cli, ARRAY_LEN(cc_cli)); |
| 4780 |
cc_logger_level = ast_logger_register_level(CC_LOGGER_LEVEL_NAME); |
| 4781 |
dialed_cc_interface_counter = 1; |
| 4782 |
res |= ast_devstate_prov_add("ccss", ccss_device_state); |
| 4783 |
|
| 4784 |
return res ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS; |
| 4785 |
} |
| 4786 |
|
| 4787 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Call Completion Supplementary Services", |
| 4788 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 4789 |
.load = load_module, |
| 4790 |
.unload = unload_module, |
| 4791 |
.load_pri = AST_MODPRI_CORE, |
| 4792 |
); |