| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#include "asterisk.h" |
| 44 |
|
| 45 |
#include "asterisk/lock.h" |
| 46 |
#include "asterisk/file.h" |
| 47 |
#include "asterisk/channel.h" |
| 48 |
#include "asterisk/pbx.h" |
| 49 |
#include "asterisk/module.h" |
| 50 |
#include "asterisk/config.h" |
| 51 |
#include "asterisk/app.h" |
| 52 |
#include "asterisk/cli.h" |
| 53 |
#include "asterisk/utils.h" |
| 54 |
#include "asterisk/astobj2.h" |
| 55 |
#include "asterisk/devicestate.h" |
| 56 |
#include "asterisk/dial.h" |
| 57 |
#include "asterisk/causes.h" |
| 58 |
#include "asterisk/format_compatibility.h" |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
#define SLA_CONFIG_FILE "sla.conf" |
| 131 |
#define MAX_CONFNUM 80 |
| 132 |
|
| 133 |
static const char * const slastation_app = "SLAStation"; |
| 134 |
static const char * const slatrunk_app = "SLATrunk"; |
| 135 |
|
| 136 |
enum { |
| 137 |
|
| 138 |
CONFFLAG_QUIET = (1 << 0), |
| 139 |
|
| 140 |
CONFFLAG_MOH = (1 << 1), |
| 141 |
|
| 142 |
CONFFLAG_MARKEDEXIT = (1 << 2), |
| 143 |
|
| 144 |
CONFFLAG_MARKEDUSER = (1 << 3), |
| 145 |
|
| 146 |
CONFFLAG_PASS_DTMF = (1 << 4), |
| 147 |
CONFFLAG_SLA_STATION = (1 << 5), |
| 148 |
CONFFLAG_SLA_TRUNK = (1 << 6), |
| 149 |
}; |
| 150 |
|
| 151 |
enum { |
| 152 |
SLA_TRUNK_OPT_MOH = (1 << 0), |
| 153 |
}; |
| 154 |
|
| 155 |
enum { |
| 156 |
SLA_TRUNK_OPT_ARG_MOH_CLASS = 0, |
| 157 |
SLA_TRUNK_OPT_ARG_ARRAY_SIZE = 1, |
| 158 |
}; |
| 159 |
|
| 160 |
AST_APP_OPTIONS(sla_trunk_opts, BEGIN_OPTIONS |
| 161 |
AST_APP_OPTION_ARG('M', SLA_TRUNK_OPT_MOH, SLA_TRUNK_OPT_ARG_MOH_CLASS), |
| 162 |
END_OPTIONS ); |
| 163 |
|
| 164 |
enum sla_which_trunk_refs { |
| 165 |
ALL_TRUNK_REFS, |
| 166 |
INACTIVE_TRUNK_REFS, |
| 167 |
}; |
| 168 |
|
| 169 |
enum sla_trunk_state { |
| 170 |
SLA_TRUNK_STATE_IDLE, |
| 171 |
SLA_TRUNK_STATE_RINGING, |
| 172 |
SLA_TRUNK_STATE_UP, |
| 173 |
SLA_TRUNK_STATE_ONHOLD, |
| 174 |
SLA_TRUNK_STATE_ONHOLD_BYME, |
| 175 |
}; |
| 176 |
|
| 177 |
enum sla_hold_access { |
| 178 |
|
| 179 |
|
| 180 |
SLA_HOLD_OPEN, |
| 181 |
|
| 182 |
|
| 183 |
SLA_HOLD_PRIVATE, |
| 184 |
}; |
| 185 |
|
| 186 |
struct sla_trunk_ref; |
| 187 |
|
| 188 |
struct sla_station { |
| 189 |
AST_RWLIST_ENTRY(sla_station) entry; |
| 190 |
AST_DECLARE_STRING_FIELDS( |
| 191 |
AST_STRING_FIELD(name); |
| 192 |
AST_STRING_FIELD(device); |
| 193 |
AST_STRING_FIELD(autocontext); |
| 194 |
); |
| 195 |
AST_LIST_HEAD_NOLOCK(, sla_trunk_ref) trunks; |
| 196 |
struct ast_dial *dial; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
unsigned int ring_timeout; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
unsigned int ring_delay; |
| 205 |
|
| 206 |
|
| 207 |
unsigned int hold_access:1; |
| 208 |
|
| 209 |
unsigned int mark:1; |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
struct sla_station_ref { |
| 221 |
AST_LIST_ENTRY(sla_station_ref) entry; |
| 222 |
struct sla_station *station; |
| 223 |
|
| 224 |
unsigned int mark:1; |
| 225 |
}; |
| 226 |
|
| 227 |
struct sla_trunk { |
| 228 |
AST_DECLARE_STRING_FIELDS( |
| 229 |
AST_STRING_FIELD(name); |
| 230 |
AST_STRING_FIELD(device); |
| 231 |
AST_STRING_FIELD(autocontext); |
| 232 |
); |
| 233 |
AST_LIST_HEAD_NOLOCK(, sla_station_ref) stations; |
| 234 |
|
| 235 |
unsigned int num_stations; |
| 236 |
|
| 237 |
unsigned int active_stations; |
| 238 |
|
| 239 |
unsigned int hold_stations; |
| 240 |
struct ast_channel *chan; |
| 241 |
unsigned int ring_timeout; |
| 242 |
|
| 243 |
|
| 244 |
unsigned int barge_disabled:1; |
| 245 |
|
| 246 |
|
| 247 |
unsigned int hold_access:1; |
| 248 |
|
| 249 |
|
| 250 |
unsigned int on_hold:1; |
| 251 |
|
| 252 |
unsigned int mark:1; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
struct sla_trunk_ref { |
| 262 |
AST_LIST_ENTRY(sla_trunk_ref) entry; |
| 263 |
struct sla_trunk *trunk; |
| 264 |
enum sla_trunk_state state; |
| 265 |
struct ast_channel *chan; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
unsigned int ring_timeout; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
unsigned int ring_delay; |
| 274 |
|
| 275 |
unsigned int mark:1; |
| 276 |
}; |
| 277 |
|
| 278 |
static struct ao2_container *sla_stations; |
| 279 |
static struct ao2_container *sla_trunks; |
| 280 |
|
| 281 |
static const char sla_registrar[] = "SLA"; |
| 282 |
|
| 283 |
|
| 284 |
enum sla_event_type { |
| 285 |
|
| 286 |
SLA_EVENT_HOLD, |
| 287 |
|
| 288 |
SLA_EVENT_DIAL_STATE, |
| 289 |
|
| 290 |
SLA_EVENT_RINGING_TRUNK, |
| 291 |
}; |
| 292 |
|
| 293 |
struct sla_event { |
| 294 |
enum sla_event_type type; |
| 295 |
struct sla_station *station; |
| 296 |
struct sla_trunk_ref *trunk_ref; |
| 297 |
AST_LIST_ENTRY(sla_event) entry; |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
struct sla_failed_station { |
| 303 |
struct sla_station *station; |
| 304 |
struct timeval last_try; |
| 305 |
AST_LIST_ENTRY(sla_failed_station) entry; |
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
struct sla_ringing_trunk { |
| 310 |
struct sla_trunk *trunk; |
| 311 |
|
| 312 |
struct timeval ring_begin; |
| 313 |
AST_LIST_HEAD_NOLOCK(, sla_station_ref) timed_out_stations; |
| 314 |
AST_LIST_ENTRY(sla_ringing_trunk) entry; |
| 315 |
}; |
| 316 |
|
| 317 |
enum sla_station_hangup { |
| 318 |
SLA_STATION_HANGUP_NORMAL, |
| 319 |
SLA_STATION_HANGUP_TIMEOUT, |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
struct sla_ringing_station { |
| 324 |
struct sla_station *station; |
| 325 |
|
| 326 |
struct timeval ring_begin; |
| 327 |
AST_LIST_ENTRY(sla_ringing_station) entry; |
| 328 |
}; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static struct { |
| 334 |
|
| 335 |
pthread_t thread; |
| 336 |
ast_cond_t cond; |
| 337 |
ast_mutex_t lock; |
| 338 |
AST_LIST_HEAD_NOLOCK(, sla_ringing_trunk) ringing_trunks; |
| 339 |
AST_LIST_HEAD_NOLOCK(, sla_ringing_station) ringing_stations; |
| 340 |
AST_LIST_HEAD_NOLOCK(, sla_failed_station) failed_stations; |
| 341 |
AST_LIST_HEAD_NOLOCK(, sla_event) event_q; |
| 342 |
unsigned int stop:1; |
| 343 |
|
| 344 |
|
| 345 |
unsigned int attempt_callerid:1; |
| 346 |
} sla = { |
| 347 |
.thread = AST_PTHREADT_NULL, |
| 348 |
}; |
| 349 |
|
| 350 |
static const char *sla_hold_str(unsigned int hold_access) |
| 351 |
{ |
| 352 |
const char *hold = "Unknown"; |
| 353 |
|
| 354 |
switch (hold_access) { |
| 355 |
case SLA_HOLD_OPEN: |
| 356 |
hold = "Open"; |
| 357 |
break; |
| 358 |
case SLA_HOLD_PRIVATE: |
| 359 |
hold = "Private"; |
| 360 |
default: |
| 361 |
break; |
| 362 |
} |
| 363 |
|
| 364 |
return hold; |
| 365 |
} |
| 366 |
|
| 367 |
static char *sla_show_trunks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 368 |
{ |
| 369 |
struct ao2_iterator i; |
| 370 |
struct sla_trunk *trunk; |
| 371 |
|
| 372 |
switch (cmd) { |
| 373 |
case CLI_INIT: |
| 374 |
e->command = "sla show trunks"; |
| 375 |
e->usage = |
| 376 |
"Usage: sla show trunks\n" |
| 377 |
" This will list all trunks defined in sla.conf\n"; |
| 378 |
return NULL; |
| 379 |
case CLI_GENERATE: |
| 380 |
return NULL; |
| 381 |
} |
| 382 |
|
| 383 |
ast_cli(a->fd, "\n" |
| 384 |
"=============================================================\n" |
| 385 |
"=== Configured SLA Trunks ===================================\n" |
| 386 |
"=============================================================\n" |
| 387 |
"===\n"); |
| 388 |
i = ao2_iterator_init(sla_trunks, 0); |
| 389 |
for (; (trunk = ao2_iterator_next(&i)); ao2_ref(trunk, -1)) { |
| 390 |
struct sla_station_ref *station_ref; |
| 391 |
char ring_timeout[23] = "(none)"; |
| 392 |
|
| 393 |
ao2_lock(trunk); |
| 394 |
|
| 395 |
if (trunk->ring_timeout) { |
| 396 |
snprintf(ring_timeout, sizeof(ring_timeout), "%u Seconds", trunk->ring_timeout); |
| 397 |
} |
| 398 |
|
| 399 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n" |
| 400 |
"=== Trunk Name: %s\n" |
| 401 |
"=== ==> Device: %s\n" |
| 402 |
"=== ==> AutoContext: %s\n" |
| 403 |
"=== ==> RingTimeout: %s\n" |
| 404 |
"=== ==> BargeAllowed: %s\n" |
| 405 |
"=== ==> HoldAccess: %s\n" |
| 406 |
"=== ==> Stations ...\n", |
| 407 |
trunk->name, trunk->device, |
| 408 |
S_OR(trunk->autocontext, "(none)"), |
| 409 |
ring_timeout, |
| 410 |
trunk->barge_disabled ? "No" : "Yes", |
| 411 |
sla_hold_str(trunk->hold_access)); |
| 412 |
|
| 413 |
AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) { |
| 414 |
ast_cli(a->fd, "=== ==> Station name: %s\n", station_ref->station->name); |
| 415 |
} |
| 416 |
|
| 417 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n"); |
| 418 |
|
| 419 |
ao2_unlock(trunk); |
| 420 |
} |
| 421 |
ao2_iterator_destroy(&i); |
| 422 |
ast_cli(a->fd, "=============================================================\n\n"); |
| 423 |
|
| 424 |
return CLI_SUCCESS; |
| 425 |
} |
| 426 |
|
| 427 |
static const char *trunkstate2str(enum sla_trunk_state state) |
| 428 |
{ |
| 429 |
#define S(e) case e: return # e; |
| 430 |
switch (state) { |
| 431 |
S(SLA_TRUNK_STATE_IDLE) |
| 432 |
S(SLA_TRUNK_STATE_RINGING) |
| 433 |
S(SLA_TRUNK_STATE_UP) |
| 434 |
S(SLA_TRUNK_STATE_ONHOLD) |
| 435 |
S(SLA_TRUNK_STATE_ONHOLD_BYME) |
| 436 |
} |
| 437 |
return "Unknown State"; |
| 438 |
#undef S |
| 439 |
} |
| 440 |
|
| 441 |
static char *sla_show_stations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 442 |
{ |
| 443 |
struct ao2_iterator i; |
| 444 |
struct sla_station *station; |
| 445 |
|
| 446 |
switch (cmd) { |
| 447 |
case CLI_INIT: |
| 448 |
e->command = "sla show stations"; |
| 449 |
e->usage = |
| 450 |
"Usage: sla show stations\n" |
| 451 |
" This will list all stations defined in sla.conf\n"; |
| 452 |
return NULL; |
| 453 |
case CLI_GENERATE: |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
|
| 457 |
ast_cli(a->fd, "\n" |
| 458 |
"=============================================================\n" |
| 459 |
"=== Configured SLA Stations =================================\n" |
| 460 |
"=============================================================\n" |
| 461 |
"===\n"); |
| 462 |
i = ao2_iterator_init(sla_stations, 0); |
| 463 |
for (; (station = ao2_iterator_next(&i)); ao2_ref(station, -1)) { |
| 464 |
struct sla_trunk_ref *trunk_ref; |
| 465 |
char ring_timeout[16] = "(none)"; |
| 466 |
char ring_delay[16] = "(none)"; |
| 467 |
|
| 468 |
ao2_lock(station); |
| 469 |
|
| 470 |
if (station->ring_timeout) { |
| 471 |
snprintf(ring_timeout, sizeof(ring_timeout), "%u", station->ring_timeout); |
| 472 |
} |
| 473 |
if (station->ring_delay) { |
| 474 |
snprintf(ring_delay, sizeof(ring_delay), "%u", station->ring_delay); |
| 475 |
} |
| 476 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n" |
| 477 |
"=== Station Name: %s\n" |
| 478 |
"=== ==> Device: %s\n" |
| 479 |
"=== ==> AutoContext: %s\n" |
| 480 |
"=== ==> RingTimeout: %s\n" |
| 481 |
"=== ==> RingDelay: %s\n" |
| 482 |
"=== ==> HoldAccess: %s\n" |
| 483 |
"=== ==> Trunks ...\n", |
| 484 |
station->name, station->device, |
| 485 |
S_OR(station->autocontext, "(none)"), |
| 486 |
ring_timeout, ring_delay, |
| 487 |
sla_hold_str(station->hold_access)); |
| 488 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 489 |
if (trunk_ref->ring_timeout) { |
| 490 |
snprintf(ring_timeout, sizeof(ring_timeout), "%u", trunk_ref->ring_timeout); |
| 491 |
} else { |
| 492 |
strcpy(ring_timeout, "(none)"); |
| 493 |
} |
| 494 |
if (trunk_ref->ring_delay) { |
| 495 |
snprintf(ring_delay, sizeof(ring_delay), "%u", trunk_ref->ring_delay); |
| 496 |
} else { |
| 497 |
strcpy(ring_delay, "(none)"); |
| 498 |
} |
| 499 |
|
| 500 |
ast_cli(a->fd, "=== ==> Trunk Name: %s\n" |
| 501 |
"=== ==> State: %s\n" |
| 502 |
"=== ==> RingTimeout: %s\n" |
| 503 |
"=== ==> RingDelay: %s\n", |
| 504 |
trunk_ref->trunk->name, |
| 505 |
trunkstate2str(trunk_ref->state), |
| 506 |
ring_timeout, ring_delay); |
| 507 |
} |
| 508 |
ast_cli(a->fd, "=== ---------------------------------------------------------\n" |
| 509 |
"===\n"); |
| 510 |
|
| 511 |
ao2_unlock(station); |
| 512 |
} |
| 513 |
ao2_iterator_destroy(&i); |
| 514 |
ast_cli(a->fd, "============================================================\n" |
| 515 |
"\n"); |
| 516 |
|
| 517 |
return CLI_SUCCESS; |
| 518 |
} |
| 519 |
|
| 520 |
static struct ast_cli_entry cli_sla[] = { |
| 521 |
AST_CLI_DEFINE(sla_show_trunks, "Show SLA Trunks"), |
| 522 |
AST_CLI_DEFINE(sla_show_stations, "Show SLA Stations"), |
| 523 |
}; |
| 524 |
|
| 525 |
static void sla_queue_event_full(enum sla_event_type type, struct sla_trunk_ref *trunk_ref, struct sla_station *station, int lock) |
| 526 |
{ |
| 527 |
struct sla_event *event; |
| 528 |
|
| 529 |
if (sla.thread == AST_PTHREADT_NULL) { |
| 530 |
ao2_ref(station, -1); |
| 531 |
ao2_ref(trunk_ref, -1); |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
if (!(event = ast_calloc(1, sizeof(*event)))) { |
| 536 |
ao2_ref(station, -1); |
| 537 |
ao2_ref(trunk_ref, -1); |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
event->type = type; |
| 542 |
event->trunk_ref = trunk_ref; |
| 543 |
event->station = station; |
| 544 |
|
| 545 |
if (!lock) { |
| 546 |
AST_LIST_INSERT_TAIL(&sla.event_q, event, entry); |
| 547 |
return; |
| 548 |
} |
| 549 |
|
| 550 |
ast_mutex_lock(&sla.lock); |
| 551 |
AST_LIST_INSERT_TAIL(&sla.event_q, event, entry); |
| 552 |
ast_cond_signal(&sla.cond); |
| 553 |
ast_mutex_unlock(&sla.lock); |
| 554 |
} |
| 555 |
|
| 556 |
static void sla_queue_event_nolock(enum sla_event_type type) |
| 557 |
{ |
| 558 |
sla_queue_event_full(type, NULL, NULL, 0); |
| 559 |
} |
| 560 |
|
| 561 |
static void sla_queue_event(enum sla_event_type type) |
| 562 |
{ |
| 563 |
sla_queue_event_full(type, NULL, NULL, 1); |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
static void sla_queue_event_conf(enum sla_event_type type, struct ast_channel *chan, const char *confname) |
| 568 |
{ |
| 569 |
struct sla_station *station; |
| 570 |
struct sla_trunk_ref *trunk_ref = NULL; |
| 571 |
char *trunk_name; |
| 572 |
struct ao2_iterator i; |
| 573 |
|
| 574 |
trunk_name = ast_strdupa(confname); |
| 575 |
strsep(&trunk_name, "_"); |
| 576 |
if (ast_strlen_zero(trunk_name)) { |
| 577 |
ast_log(LOG_ERROR, "Invalid conference name for SLA - '%s'!\n", confname); |
| 578 |
return; |
| 579 |
} |
| 580 |
|
| 581 |
i = ao2_iterator_init(sla_stations, 0); |
| 582 |
while ((station = ao2_iterator_next(&i))) { |
| 583 |
ao2_lock(station); |
| 584 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 585 |
if (trunk_ref->chan == chan && !strcmp(trunk_ref->trunk->name, trunk_name)) { |
| 586 |
ao2_ref(trunk_ref, 1); |
| 587 |
break; |
| 588 |
} |
| 589 |
} |
| 590 |
ao2_unlock(station); |
| 591 |
if (trunk_ref) { |
| 592 |
|
| 593 |
break; |
| 594 |
} |
| 595 |
ao2_ref(station, -1); |
| 596 |
} |
| 597 |
ao2_iterator_destroy(&i); |
| 598 |
|
| 599 |
if (!trunk_ref) { |
| 600 |
ast_debug(1, "Trunk not found for event!\n"); |
| 601 |
return; |
| 602 |
} |
| 603 |
|
| 604 |
sla_queue_event_full(type, trunk_ref, station, 1); |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
struct sla_framehook_data { |
| 612 |
int framehook_id; |
| 613 |
char *confname; |
| 614 |
}; |
| 615 |
|
| 616 |
static const struct ast_datastore_info sla_framehook_datastore = { |
| 617 |
.type = "app_sla", |
| 618 |
}; |
| 619 |
|
| 620 |
static int remove_framehook(struct ast_channel *chan) |
| 621 |
{ |
| 622 |
struct ast_datastore *datastore = NULL; |
| 623 |
struct sla_framehook_data *data; |
| 624 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 625 |
|
| 626 |
datastore = ast_channel_datastore_find(chan, &sla_framehook_datastore, NULL); |
| 627 |
if (!datastore) { |
| 628 |
ast_log(AST_LOG_WARNING, "Cannot remove framehook from %s: HOLD_INTERCEPT not currently enabled\n", ast_channel_name(chan)); |
| 629 |
return -1; |
| 630 |
} |
| 631 |
data = datastore->data; |
| 632 |
|
| 633 |
ast_free(data->confname); |
| 634 |
|
| 635 |
if (ast_framehook_detach(chan, data->framehook_id)) { |
| 636 |
ast_log(AST_LOG_WARNING, "Failed to remove framehook from channel %s\n", ast_channel_name(chan)); |
| 637 |
return -1; |
| 638 |
} |
| 639 |
if (ast_channel_datastore_remove(chan, datastore)) { |
| 640 |
ast_log(AST_LOG_WARNING, "Failed to remove datastore from channel %s\n", ast_channel_name(chan)); |
| 641 |
return -1; |
| 642 |
} |
| 643 |
ast_datastore_free(datastore); |
| 644 |
|
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
static struct ast_frame *sla_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data) |
| 649 |
{ |
| 650 |
struct sla_framehook_data *sla_data = data; |
| 651 |
if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) { |
| 652 |
return f; |
| 653 |
} |
| 654 |
if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_HOLD) { |
| 655 |
sla_queue_event_conf(SLA_EVENT_HOLD, chan, sla_data->confname); |
| 656 |
} |
| 657 |
return f; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
static int sla_framehook_consume(void *data, enum ast_frame_type type) |
| 662 |
{ |
| 663 |
return (type == AST_FRAME_CONTROL ? 1 : 0); |
| 664 |
} |
| 665 |
|
| 666 |
static int attach_framehook(struct ast_channel *chan, const char *confname) |
| 667 |
{ |
| 668 |
struct ast_datastore *datastore; |
| 669 |
struct sla_framehook_data *data; |
| 670 |
static struct ast_framehook_interface sla_framehook_interface = { |
| 671 |
.version = AST_FRAMEHOOK_INTERFACE_VERSION, |
| 672 |
.event_cb = sla_framehook, |
| 673 |
.consume_cb = sla_framehook_consume, |
| 674 |
.disable_inheritance = 1, |
| 675 |
}; |
| 676 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 677 |
|
| 678 |
datastore = ast_channel_datastore_find(chan, &sla_framehook_datastore, NULL); |
| 679 |
if (datastore) { |
| 680 |
ast_log(AST_LOG_WARNING, "SLA framehook already set on '%s'\n", ast_channel_name(chan)); |
| 681 |
return 0; |
| 682 |
} |
| 683 |
|
| 684 |
datastore = ast_datastore_alloc(&sla_framehook_datastore, NULL); |
| 685 |
if (!datastore) { |
| 686 |
return -1; |
| 687 |
} |
| 688 |
|
| 689 |
data = ast_calloc(1, sizeof(*data)); |
| 690 |
if (!data) { |
| 691 |
ast_datastore_free(datastore); |
| 692 |
return -1; |
| 693 |
} |
| 694 |
|
| 695 |
data->framehook_id = ast_framehook_attach(chan, &sla_framehook_interface); |
| 696 |
data->confname = ast_strdup(confname); |
| 697 |
if (!data->confname || data->framehook_id < 0) { |
| 698 |
ast_log(AST_LOG_WARNING, "Failed to attach SLA framehook to '%s'\n", ast_channel_name(chan)); |
| 699 |
ast_datastore_free(datastore); |
| 700 |
ast_free(data); |
| 701 |
return -1; |
| 702 |
} |
| 703 |
datastore->data = data; |
| 704 |
|
| 705 |
ast_channel_datastore_add(chan, datastore); |
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
static struct sla_trunk *sla_find_trunk(const char *name) |
| 714 |
{ |
| 715 |
struct sla_trunk tmp_trunk = { |
| 716 |
.name = name, |
| 717 |
}; |
| 718 |
|
| 719 |
return ao2_find(sla_trunks, &tmp_trunk, OBJ_POINTER); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
static struct sla_station *sla_find_station(const char *name) |
| 727 |
{ |
| 728 |
struct sla_station tmp_station = { |
| 729 |
.name = name, |
| 730 |
}; |
| 731 |
|
| 732 |
return ao2_find(sla_stations, &tmp_station, OBJ_POINTER); |
| 733 |
} |
| 734 |
|
| 735 |
static int sla_check_station_hold_access(const struct sla_trunk *trunk, const struct sla_station *station) |
| 736 |
{ |
| 737 |
struct sla_station_ref *station_ref; |
| 738 |
struct sla_trunk_ref *trunk_ref; |
| 739 |
|
| 740 |
|
| 741 |
AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) { |
| 742 |
AST_LIST_TRAVERSE(&station_ref->station->trunks, trunk_ref, entry) { |
| 743 |
if (trunk_ref->trunk != trunk || station_ref->station == station) { |
| 744 |
continue; |
| 745 |
} |
| 746 |
if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME && station_ref->station->hold_access == SLA_HOLD_PRIVATE) { |
| 747 |
return 1; |
| 748 |
} |
| 749 |
return 0; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
static struct sla_trunk_ref *sla_find_trunk_ref_byname(const struct sla_station *station, const char *name) |
| 766 |
{ |
| 767 |
struct sla_trunk_ref *trunk_ref = NULL; |
| 768 |
|
| 769 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 770 |
if (strcasecmp(trunk_ref->trunk->name, name)) { |
| 771 |
continue; |
| 772 |
} |
| 773 |
|
| 774 |
if (trunk_ref->trunk->barge_disabled && trunk_ref->state == SLA_TRUNK_STATE_UP) { |
| 775 |
ast_debug(2, "Barge disabled, trunk not available\n"); |
| 776 |
trunk_ref = NULL; |
| 777 |
} else if (trunk_ref->trunk->hold_stations && trunk_ref->trunk->hold_access == SLA_HOLD_PRIVATE && trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) { |
| 778 |
ast_debug(2, "Private hold by another station\n"); |
| 779 |
trunk_ref = NULL; |
| 780 |
} else if (sla_check_station_hold_access(trunk_ref->trunk, station)) { |
| 781 |
ast_debug(2, "No hold access\n"); |
| 782 |
trunk_ref = NULL; |
| 783 |
} |
| 784 |
|
| 785 |
break; |
| 786 |
} |
| 787 |
|
| 788 |
if (trunk_ref) { |
| 789 |
ao2_ref(trunk_ref, 1); |
| 790 |
} |
| 791 |
|
| 792 |
return trunk_ref; |
| 793 |
} |
| 794 |
|
| 795 |
static void sla_station_ref_destructor(void *obj) |
| 796 |
{ |
| 797 |
struct sla_station_ref *station_ref = obj; |
| 798 |
|
| 799 |
if (station_ref->station) { |
| 800 |
ao2_ref(station_ref->station, -1); |
| 801 |
station_ref->station = NULL; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
static struct sla_station_ref *sla_create_station_ref(struct sla_station *station) |
| 806 |
{ |
| 807 |
struct sla_station_ref *station_ref; |
| 808 |
|
| 809 |
if (!(station_ref = ao2_alloc(sizeof(*station_ref), sla_station_ref_destructor))) { |
| 810 |
return NULL; |
| 811 |
} |
| 812 |
|
| 813 |
ao2_ref(station, 1); |
| 814 |
station_ref->station = station; |
| 815 |
|
| 816 |
return station_ref; |
| 817 |
} |
| 818 |
|
| 819 |
static struct sla_ringing_station *sla_create_ringing_station(struct sla_station *station) |
| 820 |
{ |
| 821 |
struct sla_ringing_station *ringing_station; |
| 822 |
|
| 823 |
if (!(ringing_station = ast_calloc(1, sizeof(*ringing_station)))) { |
| 824 |
return NULL; |
| 825 |
} |
| 826 |
|
| 827 |
ao2_ref(station, 1); |
| 828 |
ringing_station->station = station; |
| 829 |
ringing_station->ring_begin = ast_tvnow(); |
| 830 |
|
| 831 |
return ringing_station; |
| 832 |
} |
| 833 |
|
| 834 |
static void sla_ringing_station_destroy(struct sla_ringing_station *ringing_station) |
| 835 |
{ |
| 836 |
if (ringing_station->station) { |
| 837 |
ao2_ref(ringing_station->station, -1); |
| 838 |
ringing_station->station = NULL; |
| 839 |
} |
| 840 |
|
| 841 |
ast_free(ringing_station); |
| 842 |
} |
| 843 |
|
| 844 |
static struct sla_failed_station *sla_create_failed_station(struct sla_station *station) |
| 845 |
{ |
| 846 |
struct sla_failed_station *failed_station; |
| 847 |
|
| 848 |
if (!(failed_station = ast_calloc(1, sizeof(*failed_station)))) { |
| 849 |
return NULL; |
| 850 |
} |
| 851 |
|
| 852 |
ao2_ref(station, 1); |
| 853 |
failed_station->station = station; |
| 854 |
failed_station->last_try = ast_tvnow(); |
| 855 |
|
| 856 |
return failed_station; |
| 857 |
} |
| 858 |
|
| 859 |
static void sla_failed_station_destroy(struct sla_failed_station *failed_station) |
| 860 |
{ |
| 861 |
if (failed_station->station) { |
| 862 |
ao2_ref(failed_station->station, -1); |
| 863 |
failed_station->station = NULL; |
| 864 |
} |
| 865 |
|
| 866 |
ast_free(failed_station); |
| 867 |
} |
| 868 |
|
| 869 |
static enum ast_device_state sla_state_to_devstate(enum sla_trunk_state state) |
| 870 |
{ |
| 871 |
switch (state) { |
| 872 |
case SLA_TRUNK_STATE_IDLE: |
| 873 |
return AST_DEVICE_NOT_INUSE; |
| 874 |
case SLA_TRUNK_STATE_RINGING: |
| 875 |
return AST_DEVICE_RINGING; |
| 876 |
case SLA_TRUNK_STATE_UP: |
| 877 |
return AST_DEVICE_INUSE; |
| 878 |
case SLA_TRUNK_STATE_ONHOLD: |
| 879 |
case SLA_TRUNK_STATE_ONHOLD_BYME: |
| 880 |
return AST_DEVICE_ONHOLD; |
| 881 |
} |
| 882 |
|
| 883 |
return AST_DEVICE_UNKNOWN; |
| 884 |
} |
| 885 |
|
| 886 |
static void sla_change_trunk_state(const struct sla_trunk *trunk, enum sla_trunk_state state, |
| 887 |
enum sla_which_trunk_refs inactive_only, const struct sla_trunk_ref *exclude) |
| 888 |
{ |
| 889 |
struct sla_station *station; |
| 890 |
struct sla_trunk_ref *trunk_ref; |
| 891 |
struct ao2_iterator i; |
| 892 |
|
| 893 |
i = ao2_iterator_init(sla_stations, 0); |
| 894 |
while ((station = ao2_iterator_next(&i))) { |
| 895 |
ao2_lock(station); |
| 896 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 897 |
if (trunk_ref->trunk != trunk || (inactive_only ? trunk_ref->chan : 0) || trunk_ref == exclude) { |
| 898 |
continue; |
| 899 |
} |
| 900 |
trunk_ref->state = state; |
| 901 |
ast_devstate_changed(sla_state_to_devstate(state), AST_DEVSTATE_CACHABLE, "SLA:%s_%s", station->name, trunk->name); |
| 902 |
break; |
| 903 |
} |
| 904 |
ao2_unlock(station); |
| 905 |
ao2_ref(station, -1); |
| 906 |
} |
| 907 |
ao2_iterator_destroy(&i); |
| 908 |
} |
| 909 |
|
| 910 |
struct run_station_args { |
| 911 |
struct sla_station *station; |
| 912 |
struct sla_trunk_ref *trunk_ref; |
| 913 |
ast_mutex_t *cond_lock; |
| 914 |
ast_cond_t *cond; |
| 915 |
}; |
| 916 |
|
| 917 |
static void answer_trunk_chan(struct ast_channel *chan) |
| 918 |
{ |
| 919 |
ast_raw_answer(chan); |
| 920 |
ast_indicate(chan, -1); |
| 921 |
} |
| 922 |
|
| 923 |
static int conf_run(struct ast_channel *chan, const char *confname, struct ast_flags *confflags, char *optargs[]) |
| 924 |
{ |
| 925 |
char confbridge_args[256]; |
| 926 |
int res = 0; |
| 927 |
|
| 928 |
snprintf(confbridge_args, sizeof(confbridge_args), "%s", confname); |
| 929 |
|
| 930 |
res |= ast_func_write(chan, "CONFBRIDGE(user,quiet)", ast_test_flag(confflags, CONFFLAG_QUIET) ? "1" : "0"); |
| 931 |
res |= ast_func_write(chan, "CONFBRIDGE(user,dtmf_passthrough)", ast_test_flag(confflags, CONFFLAG_PASS_DTMF) ? "1" : "0"); |
| 932 |
res |= ast_func_write(chan, "CONFBRIDGE(user,marked)", ast_test_flag(confflags, CONFFLAG_MARKEDUSER) ? "1" : "0"); |
| 933 |
res |= ast_func_write(chan, "CONFBRIDGE(user,end_marked)", ast_test_flag(confflags, CONFFLAG_MARKEDEXIT) ? "1" : "0"); |
| 934 |
res |= ast_func_write(chan, "CONFBRIDGE(user,music_on_hold_when_empty)", ast_test_flag(confflags, CONFFLAG_MOH) ? "1" : "0"); |
| 935 |
if (ast_test_flag(confflags, CONFFLAG_MOH) && !ast_strlen_zero(optargs[SLA_TRUNK_OPT_ARG_MOH_CLASS])) { |
| 936 |
res |= ast_func_write(chan, "CONFBRIDGE(user,music_on_hold_class)", optargs[SLA_TRUNK_OPT_ARG_MOH_CLASS]); |
| 937 |
} |
| 938 |
|
| 939 |
if (res) { |
| 940 |
ast_log(LOG_ERROR, "Failed to set up conference, aborting\n"); |
| 941 |
return -1; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
if (ast_test_flag(confflags, CONFFLAG_SLA_STATION) && attach_framehook(chan, confname)) { |
| 946 |
return -1; |
| 947 |
} |
| 948 |
|
| 949 |
ast_debug(2, "Channel %s is running ConfBridge(%s)\n", ast_channel_name(chan), confbridge_args); |
| 950 |
res = ast_pbx_exec_application(chan, "ConfBridge", confbridge_args); |
| 951 |
|
| 952 |
if (ast_test_flag(confflags, CONFFLAG_SLA_STATION)) { |
| 953 |
remove_framehook(chan); |
| 954 |
} |
| 955 |
return res; |
| 956 |
} |
| 957 |
|
| 958 |
static int conf_kick_all(struct ast_channel *chan, const char *confname) |
| 959 |
{ |
| 960 |
char confkick_args[256]; |
| 961 |
|
| 962 |
snprintf(confkick_args, sizeof(confkick_args), "%s,all", confname); |
| 963 |
ast_debug(2, "Kicking all participants from conference %s\n", confname); |
| 964 |
|
| 965 |
if (chan) { |
| 966 |
return ast_pbx_exec_application(chan, "ConfKick", confkick_args); |
| 967 |
} else { |
| 968 |
|
| 969 |
chan = ast_dummy_channel_alloc(); |
| 970 |
if (!chan) { |
| 971 |
ast_log(LOG_WARNING, "Failed to allocate dummy channel\n"); |
| 972 |
return -1; |
| 973 |
} else { |
| 974 |
int res = ast_pbx_exec_application(chan, "ConfKick", confkick_args); |
| 975 |
ast_channel_unref(chan); |
| 976 |
return res; |
| 977 |
} |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
static void *run_station(void *data) |
| 982 |
{ |
| 983 |
RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup); |
| 984 |
RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup); |
| 985 |
struct ast_str *conf_name = ast_str_create(16); |
| 986 |
struct ast_flags conf_flags = { 0 }; |
| 987 |
|
| 988 |
{ |
| 989 |
struct run_station_args *args = data; |
| 990 |
station = args->station; |
| 991 |
trunk_ref = args->trunk_ref; |
| 992 |
ast_mutex_lock(args->cond_lock); |
| 993 |
ast_cond_signal(args->cond); |
| 994 |
ast_mutex_unlock(args->cond_lock); |
| 995 |
|
| 996 |
} |
| 997 |
|
| 998 |
ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1); |
| 999 |
ast_str_set(&conf_name, 0, "SLA_%s", trunk_ref->trunk->name); |
| 1000 |
ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION); |
| 1001 |
answer_trunk_chan(trunk_ref->chan); |
| 1002 |
|
| 1003 |
ast_debug(2, "Station %s joining conference %s\n", station->name, ast_str_buffer(conf_name)); |
| 1004 |
conf_run(trunk_ref->chan, ast_str_buffer(conf_name), &conf_flags, NULL); |
| 1005 |
|
| 1006 |
trunk_ref->chan = NULL; |
| 1007 |
if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) && |
| 1008 |
trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) { |
| 1009 |
conf_kick_all(NULL, ast_str_buffer(conf_name)); |
| 1010 |
trunk_ref->trunk->hold_stations = 0; |
| 1011 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL); |
| 1012 |
} |
| 1013 |
|
| 1014 |
ast_dial_join(station->dial); |
| 1015 |
ast_dial_destroy(station->dial); |
| 1016 |
station->dial = NULL; |
| 1017 |
ast_free(conf_name); |
| 1018 |
|
| 1019 |
return NULL; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static void sla_ringing_trunk_destroy(struct sla_ringing_trunk *ringing_trunk); |
| 1023 |
|
| 1024 |
static void sla_stop_ringing_trunk(struct sla_ringing_trunk *ringing_trunk) |
| 1025 |
{ |
| 1026 |
struct sla_station_ref *station_ref; |
| 1027 |
|
| 1028 |
conf_kick_all(ringing_trunk->trunk->chan, ringing_trunk->trunk->name); |
| 1029 |
sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL); |
| 1030 |
|
| 1031 |
while ((station_ref = AST_LIST_REMOVE_HEAD(&ringing_trunk->timed_out_stations, entry))) { |
| 1032 |
ao2_ref(station_ref, -1); |
| 1033 |
} |
| 1034 |
|
| 1035 |
sla_ringing_trunk_destroy(ringing_trunk); |
| 1036 |
} |
| 1037 |
|
| 1038 |
static void sla_stop_ringing_station(struct sla_ringing_station *ringing_station, enum sla_station_hangup hangup) |
| 1039 |
{ |
| 1040 |
struct sla_ringing_trunk *ringing_trunk; |
| 1041 |
struct sla_trunk_ref *trunk_ref; |
| 1042 |
struct sla_station_ref *station_ref; |
| 1043 |
|
| 1044 |
ast_dial_join(ringing_station->station->dial); |
| 1045 |
ast_dial_destroy(ringing_station->station->dial); |
| 1046 |
ringing_station->station->dial = NULL; |
| 1047 |
|
| 1048 |
if (hangup == SLA_STATION_HANGUP_NORMAL) { |
| 1049 |
goto done; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) { |
| 1058 |
AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) { |
| 1059 |
if (ringing_trunk->trunk == trunk_ref->trunk) { |
| 1060 |
break; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
if (!trunk_ref) { |
| 1064 |
continue; |
| 1065 |
} |
| 1066 |
if (!(station_ref = sla_create_station_ref(ringing_station->station))) { |
| 1067 |
continue; |
| 1068 |
} |
| 1069 |
AST_LIST_INSERT_TAIL(&ringing_trunk->timed_out_stations, station_ref, entry); |
| 1070 |
} |
| 1071 |
|
| 1072 |
done: |
| 1073 |
sla_ringing_station_destroy(ringing_station); |
| 1074 |
} |
| 1075 |
|
| 1076 |
static void sla_dial_state_callback(struct ast_dial *dial) |
| 1077 |
{ |
| 1078 |
sla_queue_event(SLA_EVENT_DIAL_STATE); |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk, const struct sla_station *station) |
| 1085 |
{ |
| 1086 |
struct sla_station_ref *timed_out_station; |
| 1087 |
|
| 1088 |
AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) { |
| 1089 |
if (station == timed_out_station->station) { |
| 1090 |
return 1; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
return 0; |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
static struct sla_ringing_trunk *sla_choose_ringing_trunk(struct sla_station *station, struct sla_trunk_ref **trunk_ref, int rm) |
| 1106 |
{ |
| 1107 |
struct sla_trunk_ref *s_trunk_ref; |
| 1108 |
struct sla_ringing_trunk *ringing_trunk = NULL; |
| 1109 |
|
| 1110 |
AST_LIST_TRAVERSE(&station->trunks, s_trunk_ref, entry) { |
| 1111 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) { |
| 1112 |
|
| 1113 |
if (s_trunk_ref->trunk != ringing_trunk->trunk) { |
| 1114 |
continue; |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
if (sla_check_timed_out_station(ringing_trunk, station)) { |
| 1120 |
continue; |
| 1121 |
} |
| 1122 |
|
| 1123 |
if (rm) { |
| 1124 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (trunk_ref) { |
| 1128 |
ao2_ref(s_trunk_ref, 1); |
| 1129 |
*trunk_ref = s_trunk_ref; |
| 1130 |
} |
| 1131 |
|
| 1132 |
break; |
| 1133 |
} |
| 1134 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1135 |
|
| 1136 |
if (ringing_trunk) { |
| 1137 |
break; |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
return ringing_trunk; |
| 1142 |
} |
| 1143 |
|
| 1144 |
static void sla_handle_dial_state_event(void) |
| 1145 |
{ |
| 1146 |
struct sla_ringing_station *ringing_station; |
| 1147 |
|
| 1148 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) { |
| 1149 |
RAII_VAR(struct sla_trunk_ref *, s_trunk_ref, NULL, ao2_cleanup); |
| 1150 |
struct sla_ringing_trunk *ringing_trunk = NULL; |
| 1151 |
struct run_station_args args; |
| 1152 |
enum ast_dial_result dial_res; |
| 1153 |
pthread_t dont_care; |
| 1154 |
ast_mutex_t cond_lock; |
| 1155 |
ast_cond_t cond; |
| 1156 |
|
| 1157 |
switch ((dial_res = ast_dial_state(ringing_station->station->dial))) { |
| 1158 |
case AST_DIAL_RESULT_HANGUP: |
| 1159 |
case AST_DIAL_RESULT_INVALID: |
| 1160 |
case AST_DIAL_RESULT_FAILED: |
| 1161 |
case AST_DIAL_RESULT_TIMEOUT: |
| 1162 |
case AST_DIAL_RESULT_UNANSWERED: |
| 1163 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1164 |
sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_NORMAL); |
| 1165 |
break; |
| 1166 |
case AST_DIAL_RESULT_ANSWERED: |
| 1167 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1168 |
|
| 1169 |
ast_mutex_lock(&sla.lock); |
| 1170 |
ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, &s_trunk_ref, 1); |
| 1171 |
ast_mutex_unlock(&sla.lock); |
| 1172 |
if (!ringing_trunk) { |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
ast_debug(1, "Found no ringing trunk for station '%s' to answer!\n", ringing_station->station->name); |
| 1179 |
ast_dial_join(ringing_station->station->dial); |
| 1180 |
ast_dial_destroy(ringing_station->station->dial); |
| 1181 |
ringing_station->station->dial = NULL; |
| 1182 |
sla_ringing_station_destroy(ringing_station); |
| 1183 |
break; |
| 1184 |
} |
| 1185 |
|
| 1186 |
s_trunk_ref->chan = ast_dial_answered(ringing_station->station->dial); |
| 1187 |
|
| 1188 |
answer_trunk_chan(ringing_trunk->trunk->chan); |
| 1189 |
sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL); |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
ao2_ref(s_trunk_ref, 1); |
| 1194 |
args.trunk_ref = s_trunk_ref; |
| 1195 |
ao2_ref(ringing_station->station, 1); |
| 1196 |
args.station = ringing_station->station; |
| 1197 |
args.cond = &cond; |
| 1198 |
args.cond_lock = &cond_lock; |
| 1199 |
sla_ringing_trunk_destroy(ringing_trunk); |
| 1200 |
sla_ringing_station_destroy(ringing_station); |
| 1201 |
ast_mutex_init(&cond_lock); |
| 1202 |
ast_cond_init(&cond, NULL); |
| 1203 |
ast_mutex_lock(&cond_lock); |
| 1204 |
ast_pthread_create_detached_background(&dont_care, NULL, run_station, &args); |
| 1205 |
ast_cond_wait(&cond, &cond_lock); |
| 1206 |
ast_mutex_unlock(&cond_lock); |
| 1207 |
ast_mutex_destroy(&cond_lock); |
| 1208 |
ast_cond_destroy(&cond); |
| 1209 |
break; |
| 1210 |
case AST_DIAL_RESULT_TRYING: |
| 1211 |
case AST_DIAL_RESULT_RINGING: |
| 1212 |
case AST_DIAL_RESULT_PROGRESS: |
| 1213 |
case AST_DIAL_RESULT_PROCEEDING: |
| 1214 |
break; |
| 1215 |
} |
| 1216 |
if (dial_res == AST_DIAL_RESULT_ANSWERED) { |
| 1217 |
|
| 1218 |
sla_queue_event(SLA_EVENT_RINGING_TRUNK); |
| 1219 |
sla_queue_event(SLA_EVENT_DIAL_STATE); |
| 1220 |
break; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
static int sla_check_ringing_station(const struct sla_station *station) |
| 1230 |
{ |
| 1231 |
struct sla_ringing_station *ringing_station; |
| 1232 |
|
| 1233 |
AST_LIST_TRAVERSE(&sla.ringing_stations, ringing_station, entry) { |
| 1234 |
if (station == ringing_station->station) { |
| 1235 |
return 1; |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
return 0; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
static int sla_check_failed_station(const struct sla_station *station) |
| 1246 |
{ |
| 1247 |
struct sla_failed_station *failed_station; |
| 1248 |
int res = 0; |
| 1249 |
|
| 1250 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.failed_stations, failed_station, entry) { |
| 1251 |
if (station != failed_station->station) { |
| 1252 |
continue; |
| 1253 |
} |
| 1254 |
if (ast_tvdiff_ms(ast_tvnow(), failed_station->last_try) > 1000) { |
| 1255 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1256 |
sla_failed_station_destroy(failed_station); |
| 1257 |
break; |
| 1258 |
} |
| 1259 |
res = 1; |
| 1260 |
} |
| 1261 |
AST_LIST_TRAVERSE_SAFE_END |
| 1262 |
|
| 1263 |
return res; |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
static int sla_ring_station(struct sla_ringing_trunk *ringing_trunk, struct sla_station *station) |
| 1270 |
{ |
| 1271 |
char *tech, *tech_data; |
| 1272 |
struct ast_dial *dial; |
| 1273 |
struct sla_ringing_station *ringing_station; |
| 1274 |
enum ast_dial_result res; |
| 1275 |
int caller_is_saved; |
| 1276 |
struct ast_party_caller caller; |
| 1277 |
|
| 1278 |
if (!(dial = ast_dial_create())) { |
| 1279 |
return -1; |
| 1280 |
} |
| 1281 |
|
| 1282 |
ast_dial_set_state_callback(dial, sla_dial_state_callback); |
| 1283 |
tech_data = ast_strdupa(station->device); |
| 1284 |
tech = strsep(&tech_data, "/"); |
| 1285 |
|
| 1286 |
if (ast_dial_append(dial, tech, tech_data, NULL) == -1) { |
| 1287 |
ast_dial_destroy(dial); |
| 1288 |
return -1; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
caller_is_saved = 0; |
| 1293 |
if (!sla.attempt_callerid) { |
| 1294 |
caller_is_saved = 1; |
| 1295 |
caller = *ast_channel_caller(ringing_trunk->trunk->chan); |
| 1296 |
ast_party_caller_init(ast_channel_caller(ringing_trunk->trunk->chan)); |
| 1297 |
} |
| 1298 |
|
| 1299 |
res = ast_dial_run(dial, ringing_trunk->trunk->chan, 1); |
| 1300 |
|
| 1301 |
|
| 1302 |
if (caller_is_saved) { |
| 1303 |
ast_party_caller_free(ast_channel_caller(ringing_trunk->trunk->chan)); |
| 1304 |
ast_channel_caller_set(ringing_trunk->trunk->chan, &caller); |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (res != AST_DIAL_RESULT_TRYING) { |
| 1308 |
struct sla_failed_station *failed_station; |
| 1309 |
ast_dial_destroy(dial); |
| 1310 |
if ((failed_station = sla_create_failed_station(station))) { |
| 1311 |
AST_LIST_INSERT_HEAD(&sla.failed_stations, failed_station, entry); |
| 1312 |
} |
| 1313 |
return -1; |
| 1314 |
} |
| 1315 |
if (!(ringing_station = sla_create_ringing_station(station))) { |
| 1316 |
ast_dial_join(dial); |
| 1317 |
ast_dial_destroy(dial); |
| 1318 |
return -1; |
| 1319 |
} |
| 1320 |
|
| 1321 |
station->dial = dial; |
| 1322 |
|
| 1323 |
AST_LIST_INSERT_HEAD(&sla.ringing_stations, ringing_station, entry); |
| 1324 |
|
| 1325 |
return 0; |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
static int sla_check_inuse_station(const struct sla_station *station) |
| 1331 |
{ |
| 1332 |
struct sla_trunk_ref *trunk_ref; |
| 1333 |
|
| 1334 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 1335 |
if (trunk_ref->chan) { |
| 1336 |
return 1; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
return 0; |
| 1341 |
} |
| 1342 |
|
| 1343 |
static struct sla_trunk_ref *sla_find_trunk_ref(const struct sla_station *station, const struct sla_trunk *trunk) |
| 1344 |
{ |
| 1345 |
struct sla_trunk_ref *trunk_ref = NULL; |
| 1346 |
|
| 1347 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 1348 |
if (trunk_ref->trunk == trunk) { |
| 1349 |
break; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
ao2_ref(trunk_ref, 1); |
| 1354 |
|
| 1355 |
return trunk_ref; |
| 1356 |
} |
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
static int sla_check_station_delay(struct sla_station *station, struct sla_ringing_trunk *ringing_trunk) |
| 1364 |
{ |
| 1365 |
RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup); |
| 1366 |
unsigned int delay = UINT_MAX; |
| 1367 |
int time_left, time_elapsed; |
| 1368 |
|
| 1369 |
if (!ringing_trunk) { |
| 1370 |
ringing_trunk = sla_choose_ringing_trunk(station, &trunk_ref, 0); |
| 1371 |
} else { |
| 1372 |
trunk_ref = sla_find_trunk_ref(station, ringing_trunk->trunk); |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (!ringing_trunk || !trunk_ref) { |
| 1376 |
return delay; |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
delay = trunk_ref->ring_delay; |
| 1383 |
if (!delay) { |
| 1384 |
delay = station->ring_delay; |
| 1385 |
} |
| 1386 |
if (!delay) { |
| 1387 |
return INT_MAX; |
| 1388 |
} |
| 1389 |
|
| 1390 |
time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin); |
| 1391 |
time_left = (delay * 1000) - time_elapsed; |
| 1392 |
|
| 1393 |
return time_left; |
| 1394 |
} |
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
static void sla_ring_stations(void) |
| 1400 |
{ |
| 1401 |
struct sla_station_ref *station_ref; |
| 1402 |
struct sla_ringing_trunk *ringing_trunk; |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) { |
| 1407 |
AST_LIST_TRAVERSE(&ringing_trunk->trunk->stations, station_ref, entry) { |
| 1408 |
int time_left; |
| 1409 |
|
| 1410 |
|
| 1411 |
if (sla_check_ringing_station(station_ref->station)) { |
| 1412 |
continue; |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
if (sla_check_inuse_station(station_ref->station)) { |
| 1417 |
continue; |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
if (sla_check_failed_station(station_ref->station)) { |
| 1423 |
continue; |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
if (sla_check_timed_out_station(ringing_trunk, station_ref->station)) { |
| 1429 |
continue; |
| 1430 |
} |
| 1431 |
|
| 1432 |
|
| 1433 |
time_left = sla_check_station_delay(station_ref->station, ringing_trunk); |
| 1434 |
if (time_left != INT_MAX && time_left > 0) { |
| 1435 |
continue; |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
sla_ring_station(ringing_trunk, station_ref->station); |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
} |
| 1444 |
|
| 1445 |
static void sla_hangup_stations(void) |
| 1446 |
{ |
| 1447 |
struct sla_trunk_ref *trunk_ref; |
| 1448 |
struct sla_ringing_station *ringing_station; |
| 1449 |
|
| 1450 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) { |
| 1451 |
AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) { |
| 1452 |
struct sla_ringing_trunk *ringing_trunk; |
| 1453 |
ast_mutex_lock(&sla.lock); |
| 1454 |
AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) { |
| 1455 |
if (trunk_ref->trunk == ringing_trunk->trunk) { |
| 1456 |
break; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
ast_mutex_unlock(&sla.lock); |
| 1460 |
if (ringing_trunk) { |
| 1461 |
break; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
if (!trunk_ref) { |
| 1465 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1466 |
ast_dial_join(ringing_station->station->dial); |
| 1467 |
ast_dial_destroy(ringing_station->station->dial); |
| 1468 |
ringing_station->station->dial = NULL; |
| 1469 |
sla_ringing_station_destroy(ringing_station); |
| 1470 |
} |
| 1471 |
} |
| 1472 |
AST_LIST_TRAVERSE_SAFE_END |
| 1473 |
} |
| 1474 |
|
| 1475 |
static void sla_handle_ringing_trunk_event(void) |
| 1476 |
{ |
| 1477 |
ast_mutex_lock(&sla.lock); |
| 1478 |
sla_ring_stations(); |
| 1479 |
ast_mutex_unlock(&sla.lock); |
| 1480 |
|
| 1481 |
|
| 1482 |
sla_hangup_stations(); |
| 1483 |
} |
| 1484 |
|
| 1485 |
static void sla_handle_hold_event(struct sla_event *event) |
| 1486 |
{ |
| 1487 |
ast_atomic_fetchadd_int((int *) &event->trunk_ref->trunk->hold_stations, 1); |
| 1488 |
event->trunk_ref->state = SLA_TRUNK_STATE_ONHOLD_BYME; |
| 1489 |
ast_devstate_changed(AST_DEVICE_ONHOLD, AST_DEVSTATE_CACHABLE, "SLA:%s_%s", event->station->name, event->trunk_ref->trunk->name); |
| 1490 |
sla_change_trunk_state(event->trunk_ref->trunk, SLA_TRUNK_STATE_ONHOLD, INACTIVE_TRUNK_REFS, event->trunk_ref); |
| 1491 |
|
| 1492 |
if (event->trunk_ref->trunk->active_stations == 1) { |
| 1493 |
|
| 1494 |
|
| 1495 |
event->trunk_ref->trunk->on_hold = 1; |
| 1496 |
ast_indicate(event->trunk_ref->trunk->chan, AST_CONTROL_HOLD); |
| 1497 |
} |
| 1498 |
|
| 1499 |
ast_softhangup(event->trunk_ref->chan, AST_SOFTHANGUP_DEV); |
| 1500 |
event->trunk_ref->chan = NULL; |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
static int sla_calc_trunk_timeouts(unsigned int *timeout) |
| 1508 |
{ |
| 1509 |
struct sla_ringing_trunk *ringing_trunk; |
| 1510 |
int res = 0; |
| 1511 |
|
| 1512 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) { |
| 1513 |
int time_left, time_elapsed; |
| 1514 |
if (!ringing_trunk->trunk->ring_timeout) { |
| 1515 |
continue; |
| 1516 |
} |
| 1517 |
time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin); |
| 1518 |
time_left = (ringing_trunk->trunk->ring_timeout * 1000) - time_elapsed; |
| 1519 |
if (time_left <= 0) { |
| 1520 |
pbx_builtin_setvar_helper(ringing_trunk->trunk->chan, "SLATRUNK_STATUS", "RINGTIMEOUT"); |
| 1521 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1522 |
sla_stop_ringing_trunk(ringing_trunk); |
| 1523 |
res = 1; |
| 1524 |
continue; |
| 1525 |
} |
| 1526 |
if (time_left < *timeout) { |
| 1527 |
*timeout = time_left; |
| 1528 |
} |
| 1529 |
} |
| 1530 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1531 |
|
| 1532 |
return res; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
static int sla_calc_station_timeouts(unsigned int *timeout) |
| 1540 |
{ |
| 1541 |
struct sla_ringing_trunk *ringing_trunk; |
| 1542 |
struct sla_ringing_station *ringing_station; |
| 1543 |
int res = 0; |
| 1544 |
|
| 1545 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) { |
| 1546 |
unsigned int ring_timeout = 0; |
| 1547 |
int time_elapsed, time_left = INT_MAX, final_trunk_time_left = INT_MIN; |
| 1548 |
struct sla_trunk_ref *trunk_ref; |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) { |
| 1554 |
struct sla_station_ref *station_ref; |
| 1555 |
int trunk_time_elapsed, trunk_time_left; |
| 1556 |
|
| 1557 |
AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) { |
| 1558 |
if (ringing_trunk->trunk == trunk_ref->trunk) { |
| 1559 |
break; |
| 1560 |
} |
| 1561 |
} |
| 1562 |
if (!ringing_trunk) { |
| 1563 |
continue; |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
if (!trunk_ref->ring_timeout) { |
| 1569 |
break; |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, station_ref, entry) { |
| 1576 |
if (station_ref->station == ringing_station->station) { |
| 1577 |
break; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
if (station_ref) { |
| 1581 |
continue; |
| 1582 |
} |
| 1583 |
|
| 1584 |
trunk_time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin); |
| 1585 |
trunk_time_left = (trunk_ref->ring_timeout * 1000) - trunk_time_elapsed; |
| 1586 |
if (trunk_time_left > final_trunk_time_left) { |
| 1587 |
final_trunk_time_left = trunk_time_left; |
| 1588 |
} |
| 1589 |
} |
| 1590 |
|
| 1591 |
|
| 1592 |
if (final_trunk_time_left == INT_MIN && !ringing_station->station->ring_timeout) { |
| 1593 |
continue; |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
if (ringing_station->station->ring_timeout) { |
| 1598 |
ring_timeout = ringing_station->station->ring_timeout; |
| 1599 |
time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_station->ring_begin); |
| 1600 |
time_left = (ring_timeout * 1000) - time_elapsed; |
| 1601 |
} |
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
if (final_trunk_time_left > INT_MIN && final_trunk_time_left < time_left) { |
| 1606 |
time_left = final_trunk_time_left; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
if (time_left <= 0) { |
| 1611 |
AST_LIST_REMOVE_CURRENT(entry); |
| 1612 |
sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_TIMEOUT); |
| 1613 |
res = 1; |
| 1614 |
continue; |
| 1615 |
} |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
if (time_left < *timeout) { |
| 1620 |
*timeout = time_left; |
| 1621 |
} |
| 1622 |
} |
| 1623 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1624 |
|
| 1625 |
return res; |
| 1626 |
} |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
static int sla_calc_station_delays(unsigned int *timeout) |
| 1632 |
{ |
| 1633 |
struct sla_station *station; |
| 1634 |
int res = 0; |
| 1635 |
struct ao2_iterator i; |
| 1636 |
|
| 1637 |
i = ao2_iterator_init(sla_stations, 0); |
| 1638 |
for (; (station = ao2_iterator_next(&i)); ao2_ref(station, -1)) { |
| 1639 |
struct sla_ringing_trunk *ringing_trunk; |
| 1640 |
int time_left; |
| 1641 |
|
| 1642 |
|
| 1643 |
if (sla_check_ringing_station(station)) { |
| 1644 |
continue; |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
if (sla_check_inuse_station(station)) { |
| 1649 |
continue; |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
if (!(ringing_trunk = sla_choose_ringing_trunk(station, NULL, 0))) { |
| 1654 |
continue; |
| 1655 |
} |
| 1656 |
|
| 1657 |
if ((time_left = sla_check_station_delay(station, ringing_trunk)) == INT_MAX) { |
| 1658 |
continue; |
| 1659 |
} |
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
if (time_left <= 0) { |
| 1665 |
res = 1; |
| 1666 |
continue; |
| 1667 |
} |
| 1668 |
|
| 1669 |
if (time_left < *timeout) { |
| 1670 |
*timeout = time_left; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
ao2_iterator_destroy(&i); |
| 1674 |
|
| 1675 |
return res; |
| 1676 |
} |
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
static int sla_process_timers(struct timespec *ts) |
| 1681 |
{ |
| 1682 |
unsigned int timeout = UINT_MAX; |
| 1683 |
struct timeval wait; |
| 1684 |
unsigned int change_made = 0; |
| 1685 |
|
| 1686 |
|
| 1687 |
if (sla_calc_trunk_timeouts(&timeout)) { |
| 1688 |
change_made = 1; |
| 1689 |
} |
| 1690 |
|
| 1691 |
|
| 1692 |
if (sla_calc_station_timeouts(&timeout)) { |
| 1693 |
change_made = 1; |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
if (sla_calc_station_delays(&timeout)) { |
| 1698 |
change_made = 1; |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
if (change_made) { |
| 1703 |
sla_queue_event_nolock(SLA_EVENT_RINGING_TRUNK); |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
if (timeout == UINT_MAX) { |
| 1708 |
return 0; |
| 1709 |
} |
| 1710 |
|
| 1711 |
if (ts) { |
| 1712 |
wait = ast_tvadd(ast_tvnow(), ast_samp2tv(timeout, 1000)); |
| 1713 |
ts->tv_sec = wait.tv_sec; |
| 1714 |
ts->tv_nsec = wait.tv_usec * 1000; |
| 1715 |
} |
| 1716 |
|
| 1717 |
return 1; |
| 1718 |
} |
| 1719 |
|
| 1720 |
static void sla_event_destroy(struct sla_event *event) |
| 1721 |
{ |
| 1722 |
if (event->trunk_ref) { |
| 1723 |
ao2_ref(event->trunk_ref, -1); |
| 1724 |
event->trunk_ref = NULL; |
| 1725 |
} |
| 1726 |
|
| 1727 |
if (event->station) { |
| 1728 |
ao2_ref(event->station, -1); |
| 1729 |
event->station = NULL; |
| 1730 |
} |
| 1731 |
|
| 1732 |
ast_free(event); |
| 1733 |
} |
| 1734 |
|
| 1735 |
static void *sla_thread(void *data) |
| 1736 |
{ |
| 1737 |
struct sla_failed_station *failed_station; |
| 1738 |
struct sla_ringing_station *ringing_station; |
| 1739 |
|
| 1740 |
ast_mutex_lock(&sla.lock); |
| 1741 |
|
| 1742 |
while (!sla.stop) { |
| 1743 |
struct sla_event *event; |
| 1744 |
struct timespec ts = { 0, }; |
| 1745 |
unsigned int have_timeout = 0; |
| 1746 |
|
| 1747 |
if (AST_LIST_EMPTY(&sla.event_q)) { |
| 1748 |
if ((have_timeout = sla_process_timers(&ts))) { |
| 1749 |
ast_cond_timedwait(&sla.cond, &sla.lock, &ts); |
| 1750 |
} else { |
| 1751 |
ast_cond_wait(&sla.cond, &sla.lock); |
| 1752 |
} |
| 1753 |
if (sla.stop) { |
| 1754 |
break; |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (have_timeout) { |
| 1759 |
sla_process_timers(NULL); |
| 1760 |
} |
| 1761 |
|
| 1762 |
while ((event = AST_LIST_REMOVE_HEAD(&sla.event_q, entry))) { |
| 1763 |
ast_mutex_unlock(&sla.lock); |
| 1764 |
switch (event->type) { |
| 1765 |
case SLA_EVENT_HOLD: |
| 1766 |
sla_handle_hold_event(event); |
| 1767 |
break; |
| 1768 |
case SLA_EVENT_DIAL_STATE: |
| 1769 |
sla_handle_dial_state_event(); |
| 1770 |
break; |
| 1771 |
case SLA_EVENT_RINGING_TRUNK: |
| 1772 |
sla_handle_ringing_trunk_event(); |
| 1773 |
break; |
| 1774 |
} |
| 1775 |
sla_event_destroy(event); |
| 1776 |
ast_mutex_lock(&sla.lock); |
| 1777 |
} |
| 1778 |
} |
| 1779 |
|
| 1780 |
ast_mutex_unlock(&sla.lock); |
| 1781 |
|
| 1782 |
while ((ringing_station = AST_LIST_REMOVE_HEAD(&sla.ringing_stations, entry))) { |
| 1783 |
sla_ringing_station_destroy(ringing_station); |
| 1784 |
} |
| 1785 |
|
| 1786 |
while ((failed_station = AST_LIST_REMOVE_HEAD(&sla.failed_stations, entry))) { |
| 1787 |
sla_failed_station_destroy(failed_station); |
| 1788 |
} |
| 1789 |
|
| 1790 |
return NULL; |
| 1791 |
} |
| 1792 |
|
| 1793 |
struct dial_trunk_args { |
| 1794 |
struct sla_trunk_ref *trunk_ref; |
| 1795 |
struct sla_station *station; |
| 1796 |
ast_mutex_t *cond_lock; |
| 1797 |
ast_cond_t *cond; |
| 1798 |
}; |
| 1799 |
|
| 1800 |
static void *dial_trunk(void *data) |
| 1801 |
{ |
| 1802 |
struct dial_trunk_args *args = data; |
| 1803 |
struct ast_dial *dial; |
| 1804 |
char *tech, *tech_data; |
| 1805 |
enum ast_dial_result dial_res; |
| 1806 |
char conf_name[MAX_CONFNUM]; |
| 1807 |
struct ast_flags conf_flags = { 0 }; |
| 1808 |
RAII_VAR(struct sla_trunk_ref *, trunk_ref, args->trunk_ref, ao2_cleanup); |
| 1809 |
RAII_VAR(struct sla_station *, station, args->station, ao2_cleanup); |
| 1810 |
int caller_is_saved; |
| 1811 |
struct ast_party_caller caller; |
| 1812 |
int last_state = 0; |
| 1813 |
int current_state = 0; |
| 1814 |
|
| 1815 |
if (!(dial = ast_dial_create())) { |
| 1816 |
ast_mutex_lock(args->cond_lock); |
| 1817 |
ast_cond_signal(args->cond); |
| 1818 |
ast_mutex_unlock(args->cond_lock); |
| 1819 |
return NULL; |
| 1820 |
} |
| 1821 |
|
| 1822 |
tech_data = ast_strdupa(trunk_ref->trunk->device); |
| 1823 |
tech = strsep(&tech_data, "/"); |
| 1824 |
if (ast_dial_append(dial, tech, tech_data, NULL) == -1) { |
| 1825 |
ast_mutex_lock(args->cond_lock); |
| 1826 |
ast_cond_signal(args->cond); |
| 1827 |
ast_mutex_unlock(args->cond_lock); |
| 1828 |
ast_dial_destroy(dial); |
| 1829 |
return NULL; |
| 1830 |
} |
| 1831 |
|
| 1832 |
|
| 1833 |
caller_is_saved = 0; |
| 1834 |
if (!sla.attempt_callerid) { |
| 1835 |
caller_is_saved = 1; |
| 1836 |
caller = *ast_channel_caller(trunk_ref->chan); |
| 1837 |
ast_party_caller_init(ast_channel_caller(trunk_ref->chan)); |
| 1838 |
} |
| 1839 |
|
| 1840 |
dial_res = ast_dial_run(dial, trunk_ref->chan, 1); |
| 1841 |
|
| 1842 |
|
| 1843 |
if (caller_is_saved) { |
| 1844 |
ast_party_caller_free(ast_channel_caller(trunk_ref->chan)); |
| 1845 |
ast_channel_caller_set(trunk_ref->chan, &caller); |
| 1846 |
} |
| 1847 |
|
| 1848 |
if (dial_res != AST_DIAL_RESULT_TRYING) { |
| 1849 |
ast_mutex_lock(args->cond_lock); |
| 1850 |
ast_cond_signal(args->cond); |
| 1851 |
ast_mutex_unlock(args->cond_lock); |
| 1852 |
ast_dial_destroy(dial); |
| 1853 |
return NULL; |
| 1854 |
} |
| 1855 |
|
| 1856 |
|
| 1857 |
while (ast_waitfor(trunk_ref->chan, 100)) { |
| 1858 |
unsigned int done = 0; |
| 1859 |
struct ast_frame *fr = ast_read(trunk_ref->chan); |
| 1860 |
|
| 1861 |
if (!fr) { |
| 1862 |
ast_debug(1, "Channel %s did not return a frame, must have hung up\n", ast_channel_name(trunk_ref->chan)); |
| 1863 |
done = 1; |
| 1864 |
break; |
| 1865 |
} |
| 1866 |
ast_frfree(fr); |
| 1867 |
|
| 1868 |
switch ((dial_res = ast_dial_state(dial))) { |
| 1869 |
case AST_DIAL_RESULT_ANSWERED: |
| 1870 |
trunk_ref->trunk->chan = ast_dial_answered(dial); |
| 1871 |
case AST_DIAL_RESULT_HANGUP: |
| 1872 |
case AST_DIAL_RESULT_INVALID: |
| 1873 |
case AST_DIAL_RESULT_FAILED: |
| 1874 |
case AST_DIAL_RESULT_TIMEOUT: |
| 1875 |
case AST_DIAL_RESULT_UNANSWERED: |
| 1876 |
done = 1; |
| 1877 |
break; |
| 1878 |
case AST_DIAL_RESULT_TRYING: |
| 1879 |
current_state = AST_CONTROL_PROGRESS; |
| 1880 |
break; |
| 1881 |
case AST_DIAL_RESULT_RINGING: |
| 1882 |
case AST_DIAL_RESULT_PROGRESS: |
| 1883 |
case AST_DIAL_RESULT_PROCEEDING: |
| 1884 |
current_state = AST_CONTROL_RINGING; |
| 1885 |
break; |
| 1886 |
} |
| 1887 |
if (done) { |
| 1888 |
break; |
| 1889 |
} |
| 1890 |
|
| 1891 |
|
| 1892 |
if (station && ast_device_state(station->device) == AST_DEVICE_NOT_INUSE) { |
| 1893 |
ast_debug(3, "Originating station device %s no longer active\n", station->device); |
| 1894 |
trunk_ref->trunk->chan = NULL; |
| 1895 |
break; |
| 1896 |
} |
| 1897 |
|
| 1898 |
|
| 1899 |
if (current_state != last_state) { |
| 1900 |
ast_debug(3, "Indicating State Change %d to channel %s\n", current_state, ast_channel_name(trunk_ref->chan)); |
| 1901 |
ast_indicate(trunk_ref->chan, current_state); |
| 1902 |
last_state = current_state; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
if (!trunk_ref->trunk->chan) { |
| 1907 |
ast_mutex_lock(args->cond_lock); |
| 1908 |
ast_cond_signal(args->cond); |
| 1909 |
ast_mutex_unlock(args->cond_lock); |
| 1910 |
ast_dial_join(dial); |
| 1911 |
ast_dial_destroy(dial); |
| 1912 |
return NULL; |
| 1913 |
} |
| 1914 |
|
| 1915 |
snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name); |
| 1916 |
ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_TRUNK); |
| 1917 |
|
| 1918 |
ast_mutex_lock(args->cond_lock); |
| 1919 |
ast_cond_signal(args->cond); |
| 1920 |
ast_mutex_unlock(args->cond_lock); |
| 1921 |
|
| 1922 |
ast_debug(2, "Trunk dial %s joining conference %s\n", trunk_ref->trunk->name, conf_name); |
| 1923 |
conf_run(trunk_ref->trunk->chan, conf_name, &conf_flags, NULL); |
| 1924 |
|
| 1925 |
|
| 1926 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL); |
| 1927 |
|
| 1928 |
trunk_ref->trunk->chan = NULL; |
| 1929 |
trunk_ref->trunk->on_hold = 0; |
| 1930 |
|
| 1931 |
ast_dial_join(dial); |
| 1932 |
ast_dial_destroy(dial); |
| 1933 |
|
| 1934 |
return NULL; |
| 1935 |
} |
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station) |
| 1942 |
{ |
| 1943 |
struct sla_trunk_ref *trunk_ref = NULL; |
| 1944 |
|
| 1945 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 1946 |
if (trunk_ref->state == SLA_TRUNK_STATE_IDLE) { |
| 1947 |
ao2_ref(trunk_ref, 1); |
| 1948 |
break; |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
return trunk_ref; |
| 1953 |
} |
| 1954 |
|
| 1955 |
static int sla_station_exec(struct ast_channel *chan, const char *data) |
| 1956 |
{ |
| 1957 |
char *station_name, *trunk_name; |
| 1958 |
RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup); |
| 1959 |
RAII_VAR(struct sla_trunk_ref *, trunk_ref, NULL, ao2_cleanup); |
| 1960 |
char conf_name[MAX_CONFNUM]; |
| 1961 |
struct ast_flags conf_flags = { 0 }; |
| 1962 |
|
| 1963 |
if (ast_strlen_zero(data)) { |
| 1964 |
ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n"); |
| 1965 |
pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE"); |
| 1966 |
return 0; |
| 1967 |
} |
| 1968 |
|
| 1969 |
trunk_name = ast_strdupa(data); |
| 1970 |
station_name = strsep(&trunk_name, "_"); |
| 1971 |
|
| 1972 |
if (ast_strlen_zero(station_name)) { |
| 1973 |
ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n"); |
| 1974 |
pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE"); |
| 1975 |
return 0; |
| 1976 |
} |
| 1977 |
|
| 1978 |
station = sla_find_station(station_name); |
| 1979 |
|
| 1980 |
if (!station) { |
| 1981 |
ast_log(LOG_WARNING, "Station '%s' not found!\n", station_name); |
| 1982 |
pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE"); |
| 1983 |
return 0; |
| 1984 |
} |
| 1985 |
|
| 1986 |
ao2_lock(station); |
| 1987 |
if (!ast_strlen_zero(trunk_name)) { |
| 1988 |
trunk_ref = sla_find_trunk_ref_byname(station, trunk_name); |
| 1989 |
} else { |
| 1990 |
trunk_ref = sla_choose_idle_trunk(station); |
| 1991 |
} |
| 1992 |
ao2_unlock(station); |
| 1993 |
|
| 1994 |
if (!trunk_ref) { |
| 1995 |
if (ast_strlen_zero(trunk_name)) { |
| 1996 |
ast_log(LOG_NOTICE, "No trunks available for call.\n"); |
| 1997 |
} else { |
| 1998 |
ast_log(LOG_NOTICE, "Can't join existing call on trunk '%s' due to access controls.\n", trunk_name); |
| 1999 |
} |
| 2000 |
pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION"); |
| 2001 |
return 0; |
| 2002 |
} |
| 2003 |
|
| 2004 |
if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME) { |
| 2005 |
if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->hold_stations) == 1) { |
| 2006 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL); |
| 2007 |
} else { |
| 2008 |
trunk_ref->state = SLA_TRUNK_STATE_UP; |
| 2009 |
ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, "SLA:%s_%s", station->name, trunk_ref->trunk->name); |
| 2010 |
} |
| 2011 |
} else if (trunk_ref->state == SLA_TRUNK_STATE_RINGING) { |
| 2012 |
struct sla_ringing_trunk *ringing_trunk; |
| 2013 |
|
| 2014 |
ast_mutex_lock(&sla.lock); |
| 2015 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) { |
| 2016 |
if (ringing_trunk->trunk == trunk_ref->trunk) { |
| 2017 |
AST_LIST_REMOVE_CURRENT(entry); |
| 2018 |
break; |
| 2019 |
} |
| 2020 |
} |
| 2021 |
AST_LIST_TRAVERSE_SAFE_END |
| 2022 |
ast_mutex_unlock(&sla.lock); |
| 2023 |
|
| 2024 |
if (ringing_trunk) { |
| 2025 |
answer_trunk_chan(ringing_trunk->trunk->chan); |
| 2026 |
sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL); |
| 2027 |
|
| 2028 |
sla_ringing_trunk_destroy(ringing_trunk); |
| 2029 |
|
| 2030 |
|
| 2031 |
sla_queue_event(SLA_EVENT_RINGING_TRUNK); |
| 2032 |
sla_queue_event(SLA_EVENT_DIAL_STATE); |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
trunk_ref->chan = chan; |
| 2037 |
|
| 2038 |
if (!trunk_ref->trunk->chan) { |
| 2039 |
ast_mutex_t cond_lock; |
| 2040 |
ast_cond_t cond; |
| 2041 |
pthread_t dont_care; |
| 2042 |
struct dial_trunk_args args = { |
| 2043 |
.trunk_ref = trunk_ref, |
| 2044 |
.station = station, |
| 2045 |
.cond_lock = &cond_lock, |
| 2046 |
.cond = &cond, |
| 2047 |
}; |
| 2048 |
ao2_ref(trunk_ref, 1); |
| 2049 |
ao2_ref(station, 1); |
| 2050 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL); |
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
ast_mutex_init(&cond_lock); |
| 2058 |
ast_cond_init(&cond, NULL); |
| 2059 |
ast_mutex_lock(&cond_lock); |
| 2060 |
ast_pthread_create_detached_background(&dont_care, NULL, dial_trunk, &args); |
| 2061 |
ast_cond_wait(&cond, &cond_lock); |
| 2062 |
ast_mutex_unlock(&cond_lock); |
| 2063 |
ast_mutex_destroy(&cond_lock); |
| 2064 |
ast_cond_destroy(&cond); |
| 2065 |
|
| 2066 |
if (!trunk_ref->trunk->chan) { |
| 2067 |
ast_debug(1, "Trunk didn't get created. chan: %lx\n", (unsigned long) trunk_ref->trunk->chan); |
| 2068 |
pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION"); |
| 2069 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL); |
| 2070 |
trunk_ref->chan = NULL; |
| 2071 |
return 0; |
| 2072 |
} |
| 2073 |
} |
| 2074 |
|
| 2075 |
if (ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1) == 0 && |
| 2076 |
trunk_ref->trunk->on_hold) { |
| 2077 |
trunk_ref->trunk->on_hold = 0; |
| 2078 |
ast_indicate(trunk_ref->trunk->chan, AST_CONTROL_UNHOLD); |
| 2079 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL); |
| 2080 |
} |
| 2081 |
|
| 2082 |
snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name); |
| 2083 |
ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION); |
| 2084 |
ast_answer(chan); |
| 2085 |
|
| 2086 |
ast_debug(2, "Station %s joining conference %s\n", station->name, conf_name); |
| 2087 |
conf_run(chan, conf_name, &conf_flags, NULL); |
| 2088 |
|
| 2089 |
trunk_ref->chan = NULL; |
| 2090 |
if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) && |
| 2091 |
trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) { |
| 2092 |
conf_kick_all(chan, conf_name); |
| 2093 |
trunk_ref->trunk->hold_stations = 0; |
| 2094 |
sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL); |
| 2095 |
} |
| 2096 |
|
| 2097 |
pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "SUCCESS"); |
| 2098 |
|
| 2099 |
return 0; |
| 2100 |
} |
| 2101 |
|
| 2102 |
static void sla_trunk_ref_destructor(void *obj) |
| 2103 |
{ |
| 2104 |
struct sla_trunk_ref *trunk_ref = obj; |
| 2105 |
|
| 2106 |
if (trunk_ref->trunk) { |
| 2107 |
ao2_ref(trunk_ref->trunk, -1); |
| 2108 |
trunk_ref->trunk = NULL; |
| 2109 |
} |
| 2110 |
} |
| 2111 |
|
| 2112 |
static struct sla_trunk_ref *create_trunk_ref(struct sla_trunk *trunk) |
| 2113 |
{ |
| 2114 |
struct sla_trunk_ref *trunk_ref; |
| 2115 |
|
| 2116 |
if (!(trunk_ref = ao2_alloc(sizeof(*trunk_ref), sla_trunk_ref_destructor))) { |
| 2117 |
return NULL; |
| 2118 |
} |
| 2119 |
|
| 2120 |
ao2_ref(trunk, 1); |
| 2121 |
trunk_ref->trunk = trunk; |
| 2122 |
|
| 2123 |
return trunk_ref; |
| 2124 |
} |
| 2125 |
|
| 2126 |
static struct sla_ringing_trunk *queue_ringing_trunk(struct sla_trunk *trunk) |
| 2127 |
{ |
| 2128 |
struct sla_ringing_trunk *ringing_trunk; |
| 2129 |
|
| 2130 |
if (!(ringing_trunk = ast_calloc(1, sizeof(*ringing_trunk)))) { |
| 2131 |
return NULL; |
| 2132 |
} |
| 2133 |
|
| 2134 |
ao2_ref(trunk, 1); |
| 2135 |
ringing_trunk->trunk = trunk; |
| 2136 |
ringing_trunk->ring_begin = ast_tvnow(); |
| 2137 |
|
| 2138 |
sla_change_trunk_state(trunk, SLA_TRUNK_STATE_RINGING, ALL_TRUNK_REFS, NULL); |
| 2139 |
|
| 2140 |
ast_mutex_lock(&sla.lock); |
| 2141 |
AST_LIST_INSERT_HEAD(&sla.ringing_trunks, ringing_trunk, entry); |
| 2142 |
ast_mutex_unlock(&sla.lock); |
| 2143 |
|
| 2144 |
sla_queue_event(SLA_EVENT_RINGING_TRUNK); |
| 2145 |
|
| 2146 |
return ringing_trunk; |
| 2147 |
} |
| 2148 |
|
| 2149 |
static void sla_ringing_trunk_destroy(struct sla_ringing_trunk *ringing_trunk) |
| 2150 |
{ |
| 2151 |
if (ringing_trunk->trunk) { |
| 2152 |
ao2_ref(ringing_trunk->trunk, -1); |
| 2153 |
ringing_trunk->trunk = NULL; |
| 2154 |
} |
| 2155 |
|
| 2156 |
ast_free(ringing_trunk); |
| 2157 |
} |
| 2158 |
|
| 2159 |
static int sla_trunk_exec(struct ast_channel *chan, const char *data) |
| 2160 |
{ |
| 2161 |
char conf_name[MAX_CONFNUM]; |
| 2162 |
struct ast_flags conf_flags = { 0 }; |
| 2163 |
RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup); |
| 2164 |
struct sla_ringing_trunk *ringing_trunk; |
| 2165 |
AST_DECLARE_APP_ARGS(args, |
| 2166 |
AST_APP_ARG(trunk_name); |
| 2167 |
AST_APP_ARG(options); |
| 2168 |
); |
| 2169 |
char *opts[SLA_TRUNK_OPT_ARG_ARRAY_SIZE] = { NULL, }; |
| 2170 |
struct ast_flags opt_flags = { 0 }; |
| 2171 |
char *parse; |
| 2172 |
|
| 2173 |
if (ast_strlen_zero(data)) { |
| 2174 |
ast_log(LOG_ERROR, "The SLATrunk application requires an argument, the trunk name\n"); |
| 2175 |
return -1; |
| 2176 |
} |
| 2177 |
|
| 2178 |
parse = ast_strdupa(data); |
| 2179 |
AST_STANDARD_APP_ARGS(args, parse); |
| 2180 |
if (args.argc == 2) { |
| 2181 |
if (ast_app_parse_options(sla_trunk_opts, &opt_flags, opts, args.options)) { |
| 2182 |
ast_log(LOG_ERROR, "Error parsing options for SLATrunk\n"); |
| 2183 |
return -1; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
|
| 2187 |
trunk = sla_find_trunk(args.trunk_name); |
| 2188 |
|
| 2189 |
if (!trunk) { |
| 2190 |
ast_log(LOG_ERROR, "SLA Trunk '%s' not found!\n", args.trunk_name); |
| 2191 |
pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE"); |
| 2192 |
return 0; |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (trunk->chan) { |
| 2196 |
ast_log(LOG_ERROR, "Call came in on %s, but the trunk is already in use!\n", args.trunk_name); |
| 2197 |
pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE"); |
| 2198 |
return 0; |
| 2199 |
} |
| 2200 |
|
| 2201 |
trunk->chan = chan; |
| 2202 |
|
| 2203 |
if (!(ringing_trunk = queue_ringing_trunk(trunk))) { |
| 2204 |
pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE"); |
| 2205 |
return 0; |
| 2206 |
} |
| 2207 |
|
| 2208 |
snprintf(conf_name, sizeof(conf_name), "SLA_%s", args.trunk_name); |
| 2209 |
ast_set_flag(&conf_flags, CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF); |
| 2210 |
|
| 2211 |
if (ast_test_flag(&opt_flags, SLA_TRUNK_OPT_MOH)) { |
| 2212 |
ast_indicate(chan, -1); |
| 2213 |
ast_set_flag(&conf_flags, CONFFLAG_MOH); |
| 2214 |
} else { |
| 2215 |
ast_indicate(chan, AST_CONTROL_RINGING); |
| 2216 |
} |
| 2217 |
|
| 2218 |
ast_debug(2, "Trunk %s joining conference %s\n", args.trunk_name, conf_name); |
| 2219 |
conf_run(chan, conf_name, &conf_flags, opts); |
| 2220 |
trunk->chan = NULL; |
| 2221 |
trunk->on_hold = 0; |
| 2222 |
|
| 2223 |
sla_change_trunk_state(trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL); |
| 2224 |
|
| 2225 |
if (!pbx_builtin_getvar_helper(chan, "SLATRUNK_STATUS")) { |
| 2226 |
pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "SUCCESS"); |
| 2227 |
} |
| 2228 |
|
| 2229 |
|
| 2230 |
ast_mutex_lock(&sla.lock); |
| 2231 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) { |
| 2232 |
if (ringing_trunk->trunk == trunk) { |
| 2233 |
AST_LIST_REMOVE_CURRENT(entry); |
| 2234 |
break; |
| 2235 |
} |
| 2236 |
} |
| 2237 |
AST_LIST_TRAVERSE_SAFE_END; |
| 2238 |
ast_mutex_unlock(&sla.lock); |
| 2239 |
if (ringing_trunk) { |
| 2240 |
sla_ringing_trunk_destroy(ringing_trunk); |
| 2241 |
pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "UNANSWERED"); |
| 2242 |
|
| 2243 |
|
| 2244 |
sla_queue_event(SLA_EVENT_RINGING_TRUNK); |
| 2245 |
} |
| 2246 |
|
| 2247 |
return 0; |
| 2248 |
} |
| 2249 |
|
| 2250 |
static enum ast_device_state sla_state(const char *data) |
| 2251 |
{ |
| 2252 |
char *buf, *station_name, *trunk_name; |
| 2253 |
RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup); |
| 2254 |
struct sla_trunk_ref *trunk_ref; |
| 2255 |
enum ast_device_state res = AST_DEVICE_INVALID; |
| 2256 |
|
| 2257 |
trunk_name = buf = ast_strdupa(data); |
| 2258 |
station_name = strsep(&trunk_name, "_"); |
| 2259 |
|
| 2260 |
station = sla_find_station(station_name); |
| 2261 |
if (station) { |
| 2262 |
ao2_lock(station); |
| 2263 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 2264 |
if (!strcasecmp(trunk_name, trunk_ref->trunk->name)) { |
| 2265 |
res = sla_state_to_devstate(trunk_ref->state); |
| 2266 |
break; |
| 2267 |
} |
| 2268 |
} |
| 2269 |
ao2_unlock(station); |
| 2270 |
} |
| 2271 |
|
| 2272 |
if (res == AST_DEVICE_INVALID) { |
| 2273 |
ast_log(LOG_ERROR, "Could not determine state for trunk %s on station %s!\n", trunk_name, station_name); |
| 2274 |
} |
| 2275 |
|
| 2276 |
return res; |
| 2277 |
} |
| 2278 |
|
| 2279 |
static int sla_trunk_release_refs(void *obj, void *arg, int flags) |
| 2280 |
{ |
| 2281 |
struct sla_trunk *trunk = obj; |
| 2282 |
struct sla_station_ref *station_ref; |
| 2283 |
|
| 2284 |
while ((station_ref = AST_LIST_REMOVE_HEAD(&trunk->stations, entry))) { |
| 2285 |
ao2_ref(station_ref, -1); |
| 2286 |
} |
| 2287 |
|
| 2288 |
return 0; |
| 2289 |
} |
| 2290 |
|
| 2291 |
static int sla_station_release_refs(void *obj, void *arg, int flags) |
| 2292 |
{ |
| 2293 |
struct sla_station *station = obj; |
| 2294 |
struct sla_trunk_ref *trunk_ref; |
| 2295 |
|
| 2296 |
while ((trunk_ref = AST_LIST_REMOVE_HEAD(&station->trunks, entry))) { |
| 2297 |
ao2_ref(trunk_ref, -1); |
| 2298 |
} |
| 2299 |
|
| 2300 |
return 0; |
| 2301 |
} |
| 2302 |
|
| 2303 |
static void sla_station_destructor(void *obj) |
| 2304 |
{ |
| 2305 |
struct sla_station *station = obj; |
| 2306 |
|
| 2307 |
ast_debug(1, "sla_station destructor for '%s'\n", station->name); |
| 2308 |
|
| 2309 |
if (!ast_strlen_zero(station->autocontext)) { |
| 2310 |
struct sla_trunk_ref *trunk_ref; |
| 2311 |
|
| 2312 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 2313 |
char exten[AST_MAX_EXTENSION]; |
| 2314 |
char hint[AST_MAX_EXTENSION + 5]; |
| 2315 |
snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name); |
| 2316 |
snprintf(hint, sizeof(hint), "SLA:%s", exten); |
| 2317 |
ast_context_remove_extension(station->autocontext, exten, 1, sla_registrar); |
| 2318 |
ast_context_remove_extension(station->autocontext, hint, PRIORITY_HINT, sla_registrar); |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
sla_station_release_refs(station, NULL, 0); |
| 2323 |
|
| 2324 |
ast_string_field_free_memory(station); |
| 2325 |
} |
| 2326 |
|
| 2327 |
static int sla_trunk_cmp(void *obj, void *arg, int flags) |
| 2328 |
{ |
| 2329 |
struct sla_trunk *trunk = obj, *trunk2 = arg; |
| 2330 |
|
| 2331 |
return !strcasecmp(trunk->name, trunk2->name) ? CMP_MATCH | CMP_STOP : 0; |
| 2332 |
} |
| 2333 |
|
| 2334 |
static int sla_station_cmp(void *obj, void *arg, int flags) |
| 2335 |
{ |
| 2336 |
struct sla_station *station = obj, *station2 = arg; |
| 2337 |
|
| 2338 |
return !strcasecmp(station->name, station2->name) ? CMP_MATCH | CMP_STOP : 0; |
| 2339 |
} |
| 2340 |
|
| 2341 |
static void sla_destroy(void) |
| 2342 |
{ |
| 2343 |
if (sla.thread != AST_PTHREADT_NULL) { |
| 2344 |
ast_mutex_lock(&sla.lock); |
| 2345 |
sla.stop = 1; |
| 2346 |
ast_cond_signal(&sla.cond); |
| 2347 |
ast_mutex_unlock(&sla.lock); |
| 2348 |
pthread_join(sla.thread, NULL); |
| 2349 |
} |
| 2350 |
|
| 2351 |
|
| 2352 |
ast_context_destroy(NULL, sla_registrar); |
| 2353 |
|
| 2354 |
ast_mutex_destroy(&sla.lock); |
| 2355 |
ast_cond_destroy(&sla.cond); |
| 2356 |
|
| 2357 |
ao2_callback(sla_trunks, 0, sla_trunk_release_refs, NULL); |
| 2358 |
ao2_callback(sla_stations, 0, sla_station_release_refs, NULL); |
| 2359 |
|
| 2360 |
ao2_ref(sla_trunks, -1); |
| 2361 |
sla_trunks = NULL; |
| 2362 |
|
| 2363 |
ao2_ref(sla_stations, -1); |
| 2364 |
sla_stations = NULL; |
| 2365 |
} |
| 2366 |
|
| 2367 |
static int sla_check_device(const char *device) |
| 2368 |
{ |
| 2369 |
char *tech, *tech_data; |
| 2370 |
|
| 2371 |
tech_data = ast_strdupa(device); |
| 2372 |
tech = strsep(&tech_data, "/"); |
| 2373 |
|
| 2374 |
if (ast_strlen_zero(tech) || ast_strlen_zero(tech_data)) { |
| 2375 |
return -1; |
| 2376 |
} |
| 2377 |
|
| 2378 |
return 0; |
| 2379 |
} |
| 2380 |
|
| 2381 |
static void sla_trunk_destructor(void *obj) |
| 2382 |
{ |
| 2383 |
struct sla_trunk *trunk = obj; |
| 2384 |
|
| 2385 |
ast_debug(1, "sla_trunk destructor for '%s'\n", trunk->name); |
| 2386 |
|
| 2387 |
if (!ast_strlen_zero(trunk->autocontext)) { |
| 2388 |
ast_context_remove_extension(trunk->autocontext, "s", 1, sla_registrar); |
| 2389 |
} |
| 2390 |
|
| 2391 |
sla_trunk_release_refs(trunk, NULL, 0); |
| 2392 |
|
| 2393 |
ast_string_field_free_memory(trunk); |
| 2394 |
} |
| 2395 |
|
| 2396 |
static int sla_build_trunk(struct ast_config *cfg, const char *cat) |
| 2397 |
{ |
| 2398 |
RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup); |
| 2399 |
struct ast_variable *var; |
| 2400 |
const char *dev; |
| 2401 |
int existing_trunk = 0; |
| 2402 |
|
| 2403 |
if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) { |
| 2404 |
ast_log(LOG_ERROR, "SLA Trunk '%s' defined with no device!\n", cat); |
| 2405 |
return -1; |
| 2406 |
} |
| 2407 |
|
| 2408 |
if (sla_check_device(dev)) { |
| 2409 |
ast_log(LOG_ERROR, "SLA Trunk '%s' defined with invalid device '%s'!\n", cat, dev); |
| 2410 |
return -1; |
| 2411 |
} |
| 2412 |
|
| 2413 |
if ((trunk = sla_find_trunk(cat))) { |
| 2414 |
trunk->mark = 0; |
| 2415 |
existing_trunk = 1; |
| 2416 |
} else if ((trunk = ao2_alloc(sizeof(*trunk), sla_trunk_destructor))) { |
| 2417 |
if (ast_string_field_init(trunk, 32)) { |
| 2418 |
return -1; |
| 2419 |
} |
| 2420 |
ast_string_field_set(trunk, name, cat); |
| 2421 |
} else { |
| 2422 |
return -1; |
| 2423 |
} |
| 2424 |
|
| 2425 |
ao2_lock(trunk); |
| 2426 |
|
| 2427 |
ast_string_field_set(trunk, device, dev); |
| 2428 |
|
| 2429 |
for (var = ast_variable_browse(cfg, cat); var; var = var->next) { |
| 2430 |
if (!strcasecmp(var->name, "autocontext")) { |
| 2431 |
ast_string_field_set(trunk, autocontext, var->value); |
| 2432 |
} else if (!strcasecmp(var->name, "ringtimeout")) { |
| 2433 |
if (sscanf(var->value, "%30u", &trunk->ring_timeout) != 1) { |
| 2434 |
ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for trunk '%s'\n", var->value, trunk->name); |
| 2435 |
trunk->ring_timeout = 0; |
| 2436 |
} |
| 2437 |
} else if (!strcasecmp(var->name, "barge")) { |
| 2438 |
trunk->barge_disabled = ast_false(var->value); |
| 2439 |
} else if (!strcasecmp(var->name, "hold")) { |
| 2440 |
if (!strcasecmp(var->value, "private")) { |
| 2441 |
trunk->hold_access = SLA_HOLD_PRIVATE; |
| 2442 |
} else if (!strcasecmp(var->value, "open")) { |
| 2443 |
trunk->hold_access = SLA_HOLD_OPEN; |
| 2444 |
} else { |
| 2445 |
ast_log(LOG_WARNING, "Invalid value '%s' for hold on trunk %s\n", var->value, trunk->name); |
| 2446 |
} |
| 2447 |
} else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) { |
| 2448 |
ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n", var->name, var->lineno, SLA_CONFIG_FILE); |
| 2449 |
} |
| 2450 |
} |
| 2451 |
|
| 2452 |
ao2_unlock(trunk); |
| 2453 |
|
| 2454 |
if (!ast_strlen_zero(trunk->autocontext)) { |
| 2455 |
if (!ast_context_find_or_create(NULL, NULL, trunk->autocontext, sla_registrar)) { |
| 2456 |
ast_log(LOG_ERROR, "Failed to automatically find or create context '%s' for SLA!\n", trunk->autocontext); |
| 2457 |
return -1; |
| 2458 |
} |
| 2459 |
|
| 2460 |
if (ast_add_extension(trunk->autocontext, 0 , "s", 1, |
| 2461 |
NULL, NULL, slatrunk_app, ast_strdup(trunk->name), ast_free_ptr, sla_registrar)) { |
| 2462 |
ast_log(LOG_ERROR, "Failed to automatically create extension for trunk '%s'!\n", trunk->name); |
| 2463 |
return -1; |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
if (!existing_trunk) { |
| 2468 |
ao2_link(sla_trunks, trunk); |
| 2469 |
} |
| 2470 |
|
| 2471 |
return 0; |
| 2472 |
} |
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
|
| 2478 |
static void sla_add_trunk_to_station(struct sla_station *station, struct ast_variable *var) |
| 2479 |
{ |
| 2480 |
RAII_VAR(struct sla_trunk *, trunk, NULL, ao2_cleanup); |
| 2481 |
struct sla_trunk_ref *trunk_ref = NULL; |
| 2482 |
struct sla_station_ref *station_ref; |
| 2483 |
char *trunk_name, *options, *cur; |
| 2484 |
int existing_trunk_ref = 0; |
| 2485 |
int existing_station_ref = 0; |
| 2486 |
|
| 2487 |
options = ast_strdupa(var->value); |
| 2488 |
trunk_name = strsep(&options, ","); |
| 2489 |
|
| 2490 |
trunk = sla_find_trunk(trunk_name); |
| 2491 |
if (!trunk) { |
| 2492 |
ast_log(LOG_ERROR, "Trunk '%s' not found!\n", var->value); |
| 2493 |
return; |
| 2494 |
} |
| 2495 |
|
| 2496 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 2497 |
if (trunk_ref->trunk == trunk) { |
| 2498 |
trunk_ref->mark = 0; |
| 2499 |
existing_trunk_ref = 1; |
| 2500 |
break; |
| 2501 |
} |
| 2502 |
} |
| 2503 |
|
| 2504 |
if (!trunk_ref && !(trunk_ref = create_trunk_ref(trunk))) { |
| 2505 |
return; |
| 2506 |
} |
| 2507 |
|
| 2508 |
trunk_ref->state = SLA_TRUNK_STATE_IDLE; |
| 2509 |
|
| 2510 |
while ((cur = strsep(&options, ","))) { |
| 2511 |
char *name, *value = cur; |
| 2512 |
name = strsep(&value, "="); |
| 2513 |
if (!strcasecmp(name, "ringtimeout")) { |
| 2514 |
if (sscanf(value, "%30u", &trunk_ref->ring_timeout) != 1) { |
| 2515 |
ast_log(LOG_WARNING, "Invalid ringtimeout value '%s' for trunk '%s' on station '%s'\n", value, trunk->name, station->name); |
| 2516 |
trunk_ref->ring_timeout = 0; |
| 2517 |
} |
| 2518 |
} else if (!strcasecmp(name, "ringdelay")) { |
| 2519 |
if (sscanf(value, "%30u", &trunk_ref->ring_delay) != 1) { |
| 2520 |
ast_log(LOG_WARNING, "Invalid ringdelay value '%s' for trunk '%s' on station '%s'\n", value, trunk->name, station->name); |
| 2521 |
trunk_ref->ring_delay = 0; |
| 2522 |
} |
| 2523 |
} else { |
| 2524 |
ast_log(LOG_WARNING, "Invalid option '%s' for trunk '%s' on station '%s'\n", name, trunk->name, station->name); |
| 2525 |
} |
| 2526 |
} |
| 2527 |
|
| 2528 |
AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) { |
| 2529 |
if (station_ref->station == station) { |
| 2530 |
station_ref->mark = 0; |
| 2531 |
existing_station_ref = 1; |
| 2532 |
break; |
| 2533 |
} |
| 2534 |
} |
| 2535 |
|
| 2536 |
if (!station_ref && !(station_ref = sla_create_station_ref(station))) { |
| 2537 |
if (!existing_trunk_ref) { |
| 2538 |
ao2_ref(trunk_ref, -1); |
| 2539 |
} else { |
| 2540 |
trunk_ref->mark = 1; |
| 2541 |
} |
| 2542 |
return; |
| 2543 |
} |
| 2544 |
|
| 2545 |
if (!existing_station_ref) { |
| 2546 |
ao2_lock(trunk); |
| 2547 |
AST_LIST_INSERT_TAIL(&trunk->stations, station_ref, entry); |
| 2548 |
ast_atomic_fetchadd_int((int *) &trunk->num_stations, 1); |
| 2549 |
ao2_unlock(trunk); |
| 2550 |
} |
| 2551 |
|
| 2552 |
if (!existing_trunk_ref) { |
| 2553 |
ao2_lock(station); |
| 2554 |
AST_LIST_INSERT_TAIL(&station->trunks, trunk_ref, entry); |
| 2555 |
ao2_unlock(station); |
| 2556 |
} |
| 2557 |
} |
| 2558 |
|
| 2559 |
static int sla_build_station(struct ast_config *cfg, const char *cat) |
| 2560 |
{ |
| 2561 |
RAII_VAR(struct sla_station *, station, NULL, ao2_cleanup); |
| 2562 |
struct ast_variable *var; |
| 2563 |
const char *dev; |
| 2564 |
int existing_station = 0; |
| 2565 |
|
| 2566 |
if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) { |
| 2567 |
ast_log(LOG_ERROR, "SLA Station '%s' defined with no device!\n", cat); |
| 2568 |
return -1; |
| 2569 |
} |
| 2570 |
|
| 2571 |
if ((station = sla_find_station(cat))) { |
| 2572 |
station->mark = 0; |
| 2573 |
existing_station = 1; |
| 2574 |
} else if ((station = ao2_alloc(sizeof(*station), sla_station_destructor))) { |
| 2575 |
if (ast_string_field_init(station, 32)) { |
| 2576 |
return -1; |
| 2577 |
} |
| 2578 |
ast_string_field_set(station, name, cat); |
| 2579 |
} else { |
| 2580 |
return -1; |
| 2581 |
} |
| 2582 |
|
| 2583 |
ao2_lock(station); |
| 2584 |
|
| 2585 |
ast_string_field_set(station, device, dev); |
| 2586 |
|
| 2587 |
for (var = ast_variable_browse(cfg, cat); var; var = var->next) { |
| 2588 |
if (!strcasecmp(var->name, "trunk")) { |
| 2589 |
ao2_unlock(station); |
| 2590 |
sla_add_trunk_to_station(station, var); |
| 2591 |
ao2_lock(station); |
| 2592 |
} else if (!strcasecmp(var->name, "autocontext")) { |
| 2593 |
ast_string_field_set(station, autocontext, var->value); |
| 2594 |
} else if (!strcasecmp(var->name, "ringtimeout")) { |
| 2595 |
if (sscanf(var->value, "%30u", &station->ring_timeout) != 1) { |
| 2596 |
ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for station '%s'\n", var->value, station->name); |
| 2597 |
station->ring_timeout = 0; |
| 2598 |
} |
| 2599 |
} else if (!strcasecmp(var->name, "ringdelay")) { |
| 2600 |
if (sscanf(var->value, "%30u", &station->ring_delay) != 1) { |
| 2601 |
ast_log(LOG_WARNING, "Invalid ringdelay '%s' specified for station '%s'\n", var->value, station->name); |
| 2602 |
station->ring_delay = 0; |
| 2603 |
} |
| 2604 |
} else if (!strcasecmp(var->name, "hold")) { |
| 2605 |
if (!strcasecmp(var->value, "private")) { |
| 2606 |
station->hold_access = SLA_HOLD_PRIVATE; |
| 2607 |
} else if (!strcasecmp(var->value, "open")) { |
| 2608 |
station->hold_access = SLA_HOLD_OPEN; |
| 2609 |
} else { |
| 2610 |
ast_log(LOG_WARNING, "Invalid value '%s' for hold on station %s\n", var->value, station->name); |
| 2611 |
} |
| 2612 |
} else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) { |
| 2613 |
ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n", var->name, var->lineno, SLA_CONFIG_FILE); |
| 2614 |
} |
| 2615 |
} |
| 2616 |
|
| 2617 |
ao2_unlock(station); |
| 2618 |
|
| 2619 |
if (!ast_strlen_zero(station->autocontext)) { |
| 2620 |
struct sla_trunk_ref *trunk_ref; |
| 2621 |
|
| 2622 |
if (!ast_context_find_or_create(NULL, NULL, station->autocontext, sla_registrar)) { |
| 2623 |
ast_log(LOG_ERROR, "Failed to automatically find or create context '%s' for SLA!\n", station->autocontext); |
| 2624 |
return -1; |
| 2625 |
} |
| 2626 |
|
| 2627 |
|
| 2628 |
if (ast_add_extension(station->autocontext, 0 , station->name, 1, |
| 2629 |
NULL, NULL, slastation_app, ast_strdup(station->name), ast_free_ptr, sla_registrar)) { |
| 2630 |
ast_log(LOG_ERROR, "Failed to automatically create extension for trunk '%s'!\n", station->name); |
| 2631 |
return -1; |
| 2632 |
} |
| 2633 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 2634 |
char exten[AST_MAX_EXTENSION]; |
| 2635 |
char hint[AST_MAX_EXTENSION + 5]; |
| 2636 |
snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name); |
| 2637 |
snprintf(hint, sizeof(hint), "SLA:%s", exten); |
| 2638 |
|
| 2639 |
|
| 2640 |
if (ast_add_extension(station->autocontext, 0 , exten, 1, |
| 2641 |
NULL, NULL, slastation_app, ast_strdup(exten), ast_free_ptr, sla_registrar)) { |
| 2642 |
ast_log(LOG_ERROR, "Failed to automatically create extension for trunk '%s'!\n", station->name); |
| 2643 |
return -1; |
| 2644 |
} |
| 2645 |
|
| 2646 |
|
| 2647 |
if (ast_add_extension(station->autocontext, 0 , exten, PRIORITY_HINT, |
| 2648 |
NULL, NULL, hint, NULL, NULL, sla_registrar)) { |
| 2649 |
ast_log(LOG_ERROR, "Failed to automatically create hint for trunk '%s'!\n", station->name); |
| 2650 |
return -1; |
| 2651 |
} |
| 2652 |
} |
| 2653 |
} |
| 2654 |
|
| 2655 |
if (!existing_station) { |
| 2656 |
ao2_link(sla_stations, station); |
| 2657 |
} |
| 2658 |
|
| 2659 |
return 0; |
| 2660 |
} |
| 2661 |
|
| 2662 |
static int sla_trunk_mark(void *obj, void *arg, int flags) |
| 2663 |
{ |
| 2664 |
struct sla_trunk *trunk = obj; |
| 2665 |
struct sla_station_ref *station_ref; |
| 2666 |
|
| 2667 |
ao2_lock(trunk); |
| 2668 |
|
| 2669 |
trunk->mark = 1; |
| 2670 |
|
| 2671 |
AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) { |
| 2672 |
station_ref->mark = 1; |
| 2673 |
} |
| 2674 |
|
| 2675 |
ao2_unlock(trunk); |
| 2676 |
|
| 2677 |
return 0; |
| 2678 |
} |
| 2679 |
|
| 2680 |
static int sla_station_mark(void *obj, void *arg, int flags) |
| 2681 |
{ |
| 2682 |
struct sla_station *station = obj; |
| 2683 |
struct sla_trunk_ref *trunk_ref; |
| 2684 |
|
| 2685 |
ao2_lock(station); |
| 2686 |
|
| 2687 |
station->mark = 1; |
| 2688 |
|
| 2689 |
AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) { |
| 2690 |
trunk_ref->mark = 1; |
| 2691 |
} |
| 2692 |
|
| 2693 |
ao2_unlock(station); |
| 2694 |
|
| 2695 |
return 0; |
| 2696 |
} |
| 2697 |
|
| 2698 |
static int sla_trunk_is_marked(void *obj, void *arg, int flags) |
| 2699 |
{ |
| 2700 |
struct sla_trunk *trunk = obj; |
| 2701 |
|
| 2702 |
ao2_lock(trunk); |
| 2703 |
|
| 2704 |
if (trunk->mark) { |
| 2705 |
|
| 2706 |
sla_trunk_release_refs(trunk, NULL, 0); |
| 2707 |
} else { |
| 2708 |
struct sla_station_ref *station_ref; |
| 2709 |
|
| 2710 |
|
| 2711 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&trunk->stations, station_ref, entry) { |
| 2712 |
if (!station_ref->mark) { |
| 2713 |
continue; |
| 2714 |
} |
| 2715 |
AST_LIST_REMOVE_CURRENT(entry); |
| 2716 |
ao2_ref(station_ref, -1); |
| 2717 |
} |
| 2718 |
AST_LIST_TRAVERSE_SAFE_END |
| 2719 |
} |
| 2720 |
|
| 2721 |
ao2_unlock(trunk); |
| 2722 |
|
| 2723 |
return trunk->mark ? CMP_MATCH : 0; |
| 2724 |
} |
| 2725 |
|
| 2726 |
static int sla_station_is_marked(void *obj, void *arg, int flags) |
| 2727 |
{ |
| 2728 |
struct sla_station *station = obj; |
| 2729 |
|
| 2730 |
ao2_lock(station); |
| 2731 |
|
| 2732 |
if (station->mark) { |
| 2733 |
|
| 2734 |
sla_station_release_refs(station, NULL, 0); |
| 2735 |
} else { |
| 2736 |
struct sla_trunk_ref *trunk_ref; |
| 2737 |
|
| 2738 |
|
| 2739 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&station->trunks, trunk_ref, entry) { |
| 2740 |
if (!trunk_ref->mark) { |
| 2741 |
continue; |
| 2742 |
} |
| 2743 |
AST_LIST_REMOVE_CURRENT(entry); |
| 2744 |
ao2_ref(trunk_ref, -1); |
| 2745 |
} |
| 2746 |
AST_LIST_TRAVERSE_SAFE_END |
| 2747 |
} |
| 2748 |
|
| 2749 |
ao2_unlock(station); |
| 2750 |
|
| 2751 |
return station->mark ? CMP_MATCH : 0; |
| 2752 |
} |
| 2753 |
|
| 2754 |
static int sla_in_use(void) |
| 2755 |
{ |
| 2756 |
return ao2_container_count(sla_trunks) || ao2_container_count(sla_stations); |
| 2757 |
} |
| 2758 |
|
| 2759 |
static int sla_load_config(int reload) |
| 2760 |
{ |
| 2761 |
struct ast_config *cfg; |
| 2762 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 2763 |
const char *cat = NULL; |
| 2764 |
int res = 0; |
| 2765 |
const char *val; |
| 2766 |
|
| 2767 |
if (!reload) { |
| 2768 |
ast_mutex_init(&sla.lock); |
| 2769 |
ast_cond_init(&sla.cond, NULL); |
| 2770 |
sla_trunks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, sla_trunk_cmp); |
| 2771 |
sla_stations = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, sla_station_cmp); |
| 2772 |
} |
| 2773 |
|
| 2774 |
if (!(cfg = ast_config_load(SLA_CONFIG_FILE, config_flags))) { |
| 2775 |
return 0; |
| 2776 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 2777 |
return 0; |
| 2778 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 2779 |
ast_log(LOG_ERROR, "Config file " SLA_CONFIG_FILE " is in an invalid format. Aborting.\n"); |
| 2780 |
return 0; |
| 2781 |
} |
| 2782 |
|
| 2783 |
if (reload) { |
| 2784 |
ao2_callback(sla_trunks, 0, sla_trunk_mark, NULL); |
| 2785 |
ao2_callback(sla_stations, 0, sla_station_mark, NULL); |
| 2786 |
} |
| 2787 |
|
| 2788 |
if ((val = ast_variable_retrieve(cfg, "general", "attemptcallerid"))) { |
| 2789 |
sla.attempt_callerid = ast_true(val); |
| 2790 |
} |
| 2791 |
|
| 2792 |
while ((cat = ast_category_browse(cfg, cat)) && !res) { |
| 2793 |
const char *type; |
| 2794 |
if (!strcasecmp(cat, "general")) { |
| 2795 |
continue; |
| 2796 |
} |
| 2797 |
if (!(type = ast_variable_retrieve(cfg, cat, "type"))) { |
| 2798 |
ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n", SLA_CONFIG_FILE); |
| 2799 |
continue; |
| 2800 |
} |
| 2801 |
if (!strcasecmp(type, "trunk")) { |
| 2802 |
res = sla_build_trunk(cfg, cat); |
| 2803 |
} else if (!strcasecmp(type, "station")) { |
| 2804 |
res = sla_build_station(cfg, cat); |
| 2805 |
} else { |
| 2806 |
ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'!\n", SLA_CONFIG_FILE, type); |
| 2807 |
} |
| 2808 |
} |
| 2809 |
|
| 2810 |
ast_config_destroy(cfg); |
| 2811 |
|
| 2812 |
if (reload) { |
| 2813 |
ao2_callback(sla_trunks, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, sla_trunk_is_marked, NULL); |
| 2814 |
ao2_callback(sla_stations, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, sla_station_is_marked, NULL); |
| 2815 |
} |
| 2816 |
|
| 2817 |
|
| 2818 |
if (sla.thread == AST_PTHREADT_NULL && sla_in_use()) { |
| 2819 |
ast_pthread_create(&sla.thread, NULL, sla_thread, NULL); |
| 2820 |
} |
| 2821 |
|
| 2822 |
return res; |
| 2823 |
} |
| 2824 |
|
| 2825 |
static int load_config(int reload) |
| 2826 |
{ |
| 2827 |
return sla_load_config(reload); |
| 2828 |
} |
| 2829 |
|
| 2830 |
static int unload_module(void) |
| 2831 |
{ |
| 2832 |
int res = 0; |
| 2833 |
|
| 2834 |
ast_cli_unregister_multiple(cli_sla, ARRAY_LEN(cli_sla)); |
| 2835 |
res |= ast_unregister_application(slastation_app); |
| 2836 |
res |= ast_unregister_application(slatrunk_app); |
| 2837 |
|
| 2838 |
ast_devstate_prov_del("SLA"); |
| 2839 |
|
| 2840 |
sla_destroy(); |
| 2841 |
|
| 2842 |
return res; |
| 2843 |
} |
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
static int load_module(void) |
| 2856 |
{ |
| 2857 |
int res = 0; |
| 2858 |
|
| 2859 |
res |= load_config(0); |
| 2860 |
|
| 2861 |
ast_cli_register_multiple(cli_sla, ARRAY_LEN(cli_sla)); |
| 2862 |
res |= ast_register_application_xml(slastation_app, sla_station_exec); |
| 2863 |
res |= ast_register_application_xml(slatrunk_app, sla_trunk_exec); |
| 2864 |
|
| 2865 |
res |= ast_devstate_prov_add("SLA", sla_state); |
| 2866 |
|
| 2867 |
return res; |
| 2868 |
} |
| 2869 |
|
| 2870 |
static int reload(void) |
| 2871 |
{ |
| 2872 |
return load_config(1); |
| 2873 |
} |
| 2874 |
|
| 2875 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Shared Line Appearances", |
| 2876 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 2877 |
.load = load_module, |
| 2878 |
.unload = unload_module, |
| 2879 |
.reload = reload, |
| 2880 |
.load_pri = AST_MODPRI_DEVSTATE_PROVIDER, |
| 2881 |
); |