| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#include "asterisk.h" |
| 148 |
|
| 149 |
#include "asterisk/_private.h" |
| 150 |
#include "asterisk/channel.h" |
| 151 |
#include "asterisk/utils.h" |
| 152 |
#include "asterisk/lock.h" |
| 153 |
#include "asterisk/linkedlists.h" |
| 154 |
#include "asterisk/devicestate.h" |
| 155 |
#include "asterisk/pbx.h" |
| 156 |
#include "asterisk/app.h" |
| 157 |
#include "asterisk/astobj2.h" |
| 158 |
#include "asterisk/stasis.h" |
| 159 |
#include "asterisk/devicestate.h" |
| 160 |
|
| 161 |
#define DEVSTATE_TOPIC_BUCKETS 57 |
| 162 |
|
| 163 |
|
| 164 |
static const char * const devstatestring[][2] = { |
| 165 |
{ "Unknown", "UNKNOWN" }, |
| 166 |
{ "Not in use", "NOT_INUSE" }, |
| 167 |
{ "In use", "INUSE" }, |
| 168 |
{ "Busy", "BUSY" }, |
| 169 |
{ "Invalid", "INVALID" }, |
| 170 |
{ "Unavailable", "UNAVAILABLE" }, |
| 171 |
{ "Ringing", "RINGING" }, |
| 172 |
{ "Ring+Inuse", "RINGINUSE" }, |
| 173 |
{ "On Hold", "ONHOLD" }, |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
static const struct chan2dev { |
| 178 |
enum ast_channel_state chan; |
| 179 |
enum ast_device_state dev; |
| 180 |
} chan2dev[] = { |
| 181 |
{ AST_STATE_DOWN, AST_DEVICE_NOT_INUSE }, |
| 182 |
{ AST_STATE_RESERVED, AST_DEVICE_INUSE }, |
| 183 |
{ AST_STATE_OFFHOOK, AST_DEVICE_INUSE }, |
| 184 |
{ AST_STATE_DIALING, AST_DEVICE_INUSE }, |
| 185 |
{ AST_STATE_RING, AST_DEVICE_INUSE }, |
| 186 |
{ AST_STATE_RINGING, AST_DEVICE_RINGING }, |
| 187 |
{ AST_STATE_UP, AST_DEVICE_INUSE }, |
| 188 |
{ AST_STATE_BUSY, AST_DEVICE_BUSY }, |
| 189 |
{ AST_STATE_DIALING_OFFHOOK, AST_DEVICE_INUSE }, |
| 190 |
{ AST_STATE_PRERING, AST_DEVICE_RINGING }, |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
struct devstate_prov { |
| 195 |
char label[40]; |
| 196 |
ast_devstate_prov_cb_type callback; |
| 197 |
AST_RWLIST_ENTRY(devstate_prov) list; |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
static AST_RWLIST_HEAD_STATIC(devstate_provs, devstate_prov); |
| 202 |
|
| 203 |
struct state_change { |
| 204 |
AST_LIST_ENTRY(state_change) list; |
| 205 |
enum ast_devstate_cache cachable; |
| 206 |
char device[1]; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static AST_LIST_HEAD_STATIC(state_changes, state_change); |
| 212 |
|
| 213 |
|
| 214 |
static pthread_t change_thread = AST_PTHREADT_NULL; |
| 215 |
|
| 216 |
|
| 217 |
static ast_cond_t change_pending; |
| 218 |
static volatile int shuttingdown; |
| 219 |
|
| 220 |
struct stasis_subscription *devstate_message_sub; |
| 221 |
|
| 222 |
static struct stasis_topic *device_state_topic_all; |
| 223 |
static struct stasis_cache *device_state_cache; |
| 224 |
static struct stasis_caching_topic *device_state_topic_cached; |
| 225 |
static struct stasis_topic_pool *device_state_topic_pool; |
| 226 |
|
| 227 |
static struct ast_manager_event_blob *devstate_to_ami(struct stasis_message *msg); |
| 228 |
static struct ast_event *devstate_to_event(struct stasis_message *msg); |
| 229 |
|
| 230 |
|
| 231 |
STASIS_MESSAGE_TYPE_DEFN(ast_device_state_message_type, |
| 232 |
.to_ami = devstate_to_ami, |
| 233 |
.to_event = devstate_to_event, |
| 234 |
); |
| 235 |
|
| 236 |
|
| 237 |
static int getproviderstate(const char *provider, const char *address); |
| 238 |
|
| 239 |
|
| 240 |
const char *ast_devstate2str(enum ast_device_state devstate) |
| 241 |
{ |
| 242 |
return devstatestring[devstate][0]; |
| 243 |
} |
| 244 |
|
| 245 |
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate) |
| 246 |
{ |
| 247 |
int i; |
| 248 |
chanstate &= 0xFFFF; |
| 249 |
for (i = 0; i < ARRAY_LEN(chan2dev); i++) { |
| 250 |
if (chan2dev[i].chan == chanstate) { |
| 251 |
return chan2dev[i].dev; |
| 252 |
} |
| 253 |
} |
| 254 |
return AST_DEVICE_UNKNOWN; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
const char *ast_devstate_str(enum ast_device_state state) |
| 259 |
{ |
| 260 |
return devstatestring[state][1]; |
| 261 |
} |
| 262 |
|
| 263 |
enum ast_device_state ast_devstate_val(const char *val) |
| 264 |
{ |
| 265 |
if (!strcasecmp(val, "NOT_INUSE")) |
| 266 |
return AST_DEVICE_NOT_INUSE; |
| 267 |
else if (!strcasecmp(val, "INUSE")) |
| 268 |
return AST_DEVICE_INUSE; |
| 269 |
else if (!strcasecmp(val, "BUSY")) |
| 270 |
return AST_DEVICE_BUSY; |
| 271 |
else if (!strcasecmp(val, "INVALID")) |
| 272 |
return AST_DEVICE_INVALID; |
| 273 |
else if (!strcasecmp(val, "UNAVAILABLE")) |
| 274 |
return AST_DEVICE_UNAVAILABLE; |
| 275 |
else if (!strcasecmp(val, "RINGING")) |
| 276 |
return AST_DEVICE_RINGING; |
| 277 |
else if (!strcasecmp(val, "RINGINUSE")) |
| 278 |
return AST_DEVICE_RINGINUSE; |
| 279 |
else if (!strcasecmp(val, "ONHOLD")) |
| 280 |
return AST_DEVICE_ONHOLD; |
| 281 |
|
| 282 |
return AST_DEVICE_UNKNOWN; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
enum ast_device_state ast_parse_device_state(const char *device) |
| 291 |
{ |
| 292 |
struct ast_channel *chan; |
| 293 |
char match[AST_CHANNEL_NAME]; |
| 294 |
enum ast_device_state res; |
| 295 |
|
| 296 |
snprintf(match, sizeof(match), "%s-", device); |
| 297 |
|
| 298 |
if (!(chan = ast_channel_get_by_name_prefix(match, strlen(match)))) { |
| 299 |
return AST_DEVICE_UNKNOWN; |
| 300 |
} |
| 301 |
|
| 302 |
if (ast_channel_hold_state(chan) == AST_CONTROL_HOLD) { |
| 303 |
res = AST_DEVICE_ONHOLD; |
| 304 |
} else { |
| 305 |
res = ast_state_chan2dev(ast_channel_state(chan)); |
| 306 |
} |
| 307 |
ast_channel_unref(chan); |
| 308 |
|
| 309 |
return res; |
| 310 |
} |
| 311 |
|
| 312 |
static enum ast_device_state devstate_cached(const char *device, const struct ast_eid *eid) |
| 313 |
{ |
| 314 |
struct stasis_message *cached_msg; |
| 315 |
struct ast_device_state_message *device_state; |
| 316 |
enum ast_device_state state; |
| 317 |
|
| 318 |
cached_msg = stasis_cache_get_by_eid(ast_device_state_cache(), |
| 319 |
ast_device_state_message_type(), device, eid); |
| 320 |
if (!cached_msg) { |
| 321 |
return AST_DEVICE_UNKNOWN; |
| 322 |
} |
| 323 |
device_state = stasis_message_data(cached_msg); |
| 324 |
state = device_state->state; |
| 325 |
ao2_cleanup(cached_msg); |
| 326 |
|
| 327 |
return state; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
static enum ast_device_state _ast_device_state(const char *device, int check_cache) |
| 332 |
{ |
| 333 |
char *number; |
| 334 |
const struct ast_channel_tech *chan_tech; |
| 335 |
enum ast_device_state res; |
| 336 |
|
| 337 |
char *tech; |
| 338 |
|
| 339 |
|
| 340 |
if (check_cache) { |
| 341 |
res = devstate_cached(device, NULL); |
| 342 |
if (res != AST_DEVICE_UNKNOWN) { |
| 343 |
return res; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
number = ast_strdupa(device); |
| 348 |
tech = strsep(&number, "/"); |
| 349 |
if (!number) { |
| 350 |
|
| 351 |
char *provider; |
| 352 |
|
| 353 |
provider = strsep(&tech, ":"); |
| 354 |
if (!tech) { |
| 355 |
return AST_DEVICE_INVALID; |
| 356 |
} |
| 357 |
|
| 358 |
number = tech; |
| 359 |
|
| 360 |
ast_debug(3, "Checking if I can find provider for \"%s\" - number: %s\n", provider, number); |
| 361 |
return getproviderstate(provider, number); |
| 362 |
} |
| 363 |
|
| 364 |
ast_debug(4, "No provider found, checking channel drivers for %s - %s\n", tech, number); |
| 365 |
|
| 366 |
chan_tech = ast_get_channel_tech(tech); |
| 367 |
if (!chan_tech) { |
| 368 |
return AST_DEVICE_INVALID; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
if (!chan_tech->devicestate) { |
| 373 |
|
| 374 |
return ast_parse_device_state(device); |
| 375 |
} |
| 376 |
|
| 377 |
res = chan_tech->devicestate(number); |
| 378 |
if (res == AST_DEVICE_UNKNOWN) { |
| 379 |
res = ast_parse_device_state(device); |
| 380 |
} |
| 381 |
|
| 382 |
return res; |
| 383 |
} |
| 384 |
|
| 385 |
enum ast_device_state ast_device_state(const char *device) |
| 386 |
{ |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
return _ast_device_state(device, 1); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback) |
| 395 |
{ |
| 396 |
struct devstate_prov *devcb; |
| 397 |
struct devstate_prov *devprov; |
| 398 |
|
| 399 |
if (!callback || !(devprov = ast_calloc(1, sizeof(*devprov)))) |
| 400 |
return -1; |
| 401 |
|
| 402 |
devprov->callback = callback; |
| 403 |
ast_copy_string(devprov->label, label, sizeof(devprov->label)); |
| 404 |
|
| 405 |
AST_RWLIST_WRLOCK(&devstate_provs); |
| 406 |
AST_RWLIST_TRAVERSE(&devstate_provs, devcb, list) { |
| 407 |
if (!strcasecmp(devcb->label, label)) { |
| 408 |
ast_log(LOG_WARNING, "Device state provider '%s' already registered\n", label); |
| 409 |
ast_free(devprov); |
| 410 |
AST_RWLIST_UNLOCK(&devstate_provs); |
| 411 |
return -1; |
| 412 |
} |
| 413 |
} |
| 414 |
AST_RWLIST_INSERT_HEAD(&devstate_provs, devprov, list); |
| 415 |
AST_RWLIST_UNLOCK(&devstate_provs); |
| 416 |
|
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
int ast_devstate_prov_del(const char *label) |
| 422 |
{ |
| 423 |
struct devstate_prov *devcb; |
| 424 |
int res = -1; |
| 425 |
|
| 426 |
AST_RWLIST_WRLOCK(&devstate_provs); |
| 427 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&devstate_provs, devcb, list) { |
| 428 |
if (!strcasecmp(devcb->label, label)) { |
| 429 |
AST_RWLIST_REMOVE_CURRENT(list); |
| 430 |
ast_free(devcb); |
| 431 |
res = 0; |
| 432 |
break; |
| 433 |
} |
| 434 |
} |
| 435 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 436 |
AST_RWLIST_UNLOCK(&devstate_provs); |
| 437 |
|
| 438 |
return res; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
static int getproviderstate(const char *provider, const char *address) |
| 443 |
{ |
| 444 |
struct devstate_prov *devprov; |
| 445 |
int res = AST_DEVICE_INVALID; |
| 446 |
|
| 447 |
AST_RWLIST_RDLOCK(&devstate_provs); |
| 448 |
AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) { |
| 449 |
ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider); |
| 450 |
|
| 451 |
if (!strcasecmp(devprov->label, provider)) { |
| 452 |
res = devprov->callback(address); |
| 453 |
break; |
| 454 |
} |
| 455 |
} |
| 456 |
AST_RWLIST_UNLOCK(&devstate_provs); |
| 457 |
|
| 458 |
return res; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
static void do_state_change(const char *device, enum ast_devstate_cache cachable) |
| 464 |
{ |
| 465 |
enum ast_device_state state; |
| 466 |
|
| 467 |
state = _ast_device_state(device, 0); |
| 468 |
|
| 469 |
ast_debug(3, "Changing state for %s - state %u (%s)\n", device, state, ast_devstate2str(state)); |
| 470 |
|
| 471 |
ast_publish_device_state(device, state, cachable); |
| 472 |
} |
| 473 |
|
| 474 |
int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device) |
| 475 |
{ |
| 476 |
struct state_change *change; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if (state != AST_DEVICE_UNKNOWN) { |
| 495 |
ast_publish_device_state(device, state, cachable); |
| 496 |
} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) { |
| 497 |
|
| 498 |
|
| 499 |
do_state_change(device, cachable); |
| 500 |
} else { |
| 501 |
|
| 502 |
strcpy(change->device, device); |
| 503 |
change->cachable = cachable; |
| 504 |
AST_LIST_LOCK(&state_changes); |
| 505 |
AST_LIST_INSERT_TAIL(&state_changes, change, list); |
| 506 |
ast_cond_signal(&change_pending); |
| 507 |
AST_LIST_UNLOCK(&state_changes); |
| 508 |
} |
| 509 |
|
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt, ...) |
| 514 |
{ |
| 515 |
char buf[AST_MAX_EXTENSION]; |
| 516 |
va_list ap; |
| 517 |
|
| 518 |
va_start(ap, fmt); |
| 519 |
vsnprintf(buf, sizeof(buf), fmt, ap); |
| 520 |
va_end(ap); |
| 521 |
|
| 522 |
return ast_devstate_changed_literal(state, cachable, buf); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
static void *do_devstate_changes(void *data) |
| 527 |
{ |
| 528 |
struct state_change *next, *current; |
| 529 |
|
| 530 |
while (!shuttingdown) { |
| 531 |
|
| 532 |
AST_LIST_LOCK(&state_changes); |
| 533 |
if (AST_LIST_EMPTY(&state_changes)) |
| 534 |
ast_cond_wait(&change_pending, &state_changes.lock); |
| 535 |
next = AST_LIST_FIRST(&state_changes); |
| 536 |
AST_LIST_HEAD_INIT_NOLOCK(&state_changes); |
| 537 |
AST_LIST_UNLOCK(&state_changes); |
| 538 |
|
| 539 |
|
| 540 |
while ((current = next)) { |
| 541 |
next = AST_LIST_NEXT(current, list); |
| 542 |
do_state_change(current->device, current->cachable); |
| 543 |
ast_free(current); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
return NULL; |
| 548 |
} |
| 549 |
|
| 550 |
static struct ast_device_state_message *device_state_alloc(const char *device, enum ast_device_state state, enum ast_devstate_cache cachable, const struct ast_eid *eid) |
| 551 |
{ |
| 552 |
struct ast_device_state_message *new_device_state; |
| 553 |
char *pos; |
| 554 |
size_t stuff_len; |
| 555 |
|
| 556 |
ast_assert(!ast_strlen_zero(device)); |
| 557 |
|
| 558 |
stuff_len = strlen(device) + 1; |
| 559 |
if (eid) { |
| 560 |
stuff_len += sizeof(*eid); |
| 561 |
} |
| 562 |
new_device_state = ao2_alloc_options(sizeof(*new_device_state) + stuff_len, NULL, |
| 563 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 564 |
if (!new_device_state) { |
| 565 |
return NULL; |
| 566 |
} |
| 567 |
|
| 568 |
if (eid) { |
| 569 |
|
| 570 |
new_device_state->stuff[0] = *eid; |
| 571 |
new_device_state->eid = &new_device_state->stuff[0]; |
| 572 |
pos = (char *) &new_device_state->stuff[1]; |
| 573 |
} else { |
| 574 |
pos = (char *) &new_device_state->stuff[0]; |
| 575 |
} |
| 576 |
|
| 577 |
strcpy(pos, device); |
| 578 |
new_device_state->device = pos; |
| 579 |
|
| 580 |
new_device_state->state = state; |
| 581 |
new_device_state->cachable = cachable; |
| 582 |
|
| 583 |
return new_device_state; |
| 584 |
} |
| 585 |
|
| 586 |
static void devstate_change_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg) |
| 587 |
{ |
| 588 |
struct ast_device_state_message *device_state; |
| 589 |
|
| 590 |
if (ast_device_state_message_type() != stasis_message_type(msg)) { |
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
device_state = stasis_message_data(msg); |
| 595 |
if (device_state->cachable == AST_DEVSTATE_CACHABLE || !device_state->eid) { |
| 596 |
|
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
ast_publish_device_state_full(device_state->device, device_state->state, |
| 605 |
device_state->cachable, NULL); |
| 606 |
} |
| 607 |
|
| 608 |
static void device_state_engine_cleanup(void) |
| 609 |
{ |
| 610 |
shuttingdown = 1; |
| 611 |
AST_LIST_LOCK(&state_changes); |
| 612 |
ast_cond_signal(&change_pending); |
| 613 |
AST_LIST_UNLOCK(&state_changes); |
| 614 |
|
| 615 |
if (change_thread != AST_PTHREADT_NULL) { |
| 616 |
pthread_join(change_thread, NULL); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
int ast_device_state_engine_init(void) |
| 622 |
{ |
| 623 |
ast_cond_init(&change_pending, NULL); |
| 624 |
if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) { |
| 625 |
ast_log(LOG_ERROR, "Unable to start device state change thread.\n"); |
| 626 |
return -1; |
| 627 |
} |
| 628 |
ast_register_cleanup(device_state_engine_cleanup); |
| 629 |
|
| 630 |
return 0; |
| 631 |
} |
| 632 |
|
| 633 |
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg) |
| 634 |
{ |
| 635 |
memset(agg, 0, sizeof(*agg)); |
| 636 |
agg->state = AST_DEVICE_INVALID; |
| 637 |
} |
| 638 |
|
| 639 |
void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state) |
| 640 |
{ |
| 641 |
static enum ast_device_state state_order[] = { |
| 642 |
1, |
| 643 |
3, |
| 644 |
6, |
| 645 |
7, |
| 646 |
0, |
| 647 |
2, |
| 648 |
5, |
| 649 |
8, |
| 650 |
4, |
| 651 |
}; |
| 652 |
|
| 653 |
if (state == AST_DEVICE_RINGING) { |
| 654 |
agg->ringing = 1; |
| 655 |
} else if (state == AST_DEVICE_INUSE || state == AST_DEVICE_ONHOLD || state == AST_DEVICE_BUSY) { |
| 656 |
agg->inuse = 1; |
| 657 |
} |
| 658 |
|
| 659 |
if (agg->ringing && agg->inuse) { |
| 660 |
agg->state = AST_DEVICE_RINGINUSE; |
| 661 |
} else if (state_order[state] > state_order[agg->state]) { |
| 662 |
agg->state = state; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg) |
| 667 |
{ |
| 668 |
return agg->state; |
| 669 |
} |
| 670 |
|
| 671 |
struct stasis_topic *ast_device_state_topic_all(void) |
| 672 |
{ |
| 673 |
return device_state_topic_all; |
| 674 |
} |
| 675 |
|
| 676 |
struct stasis_cache *ast_device_state_cache(void) |
| 677 |
{ |
| 678 |
return device_state_cache; |
| 679 |
} |
| 680 |
|
| 681 |
struct stasis_topic *ast_device_state_topic_cached(void) |
| 682 |
{ |
| 683 |
return stasis_caching_get_topic(device_state_topic_cached); |
| 684 |
} |
| 685 |
|
| 686 |
struct stasis_topic *ast_device_state_topic(const char *device) |
| 687 |
{ |
| 688 |
return stasis_topic_pool_get_topic(device_state_topic_pool, device); |
| 689 |
} |
| 690 |
|
| 691 |
int ast_device_state_clear_cache(const char *device) |
| 692 |
{ |
| 693 |
struct stasis_message *cached_msg; |
| 694 |
struct stasis_message *msg; |
| 695 |
|
| 696 |
cached_msg = stasis_cache_get_by_eid(ast_device_state_cache(), |
| 697 |
ast_device_state_message_type(), device, &ast_eid_default); |
| 698 |
if (!cached_msg) { |
| 699 |
|
| 700 |
return -1; |
| 701 |
} |
| 702 |
|
| 703 |
msg = stasis_cache_clear_create(cached_msg); |
| 704 |
if (msg) { |
| 705 |
stasis_publish(ast_device_state_topic(device), msg); |
| 706 |
} |
| 707 |
ao2_cleanup(msg); |
| 708 |
ao2_cleanup(cached_msg); |
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
int ast_publish_device_state_full( |
| 713 |
const char *device, |
| 714 |
enum ast_device_state state, |
| 715 |
enum ast_devstate_cache cachable, |
| 716 |
struct ast_eid *eid) |
| 717 |
{ |
| 718 |
RAII_VAR(struct ast_device_state_message *, device_state, NULL, ao2_cleanup); |
| 719 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 720 |
struct stasis_topic *topic; |
| 721 |
|
| 722 |
ast_assert(!ast_strlen_zero(device)); |
| 723 |
|
| 724 |
if (!ast_device_state_message_type()) { |
| 725 |
return -1; |
| 726 |
} |
| 727 |
|
| 728 |
if (cachable) { |
| 729 |
enum ast_device_state res; |
| 730 |
|
| 731 |
|
| 732 |
res = devstate_cached(device, eid); |
| 733 |
if (res == state) { |
| 734 |
return 0; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
device_state = device_state_alloc(device, state, cachable, eid); |
| 739 |
if (!device_state) { |
| 740 |
return -1; |
| 741 |
} |
| 742 |
|
| 743 |
message = stasis_message_create_full(ast_device_state_message_type(), device_state, |
| 744 |
eid); |
| 745 |
if (!message) { |
| 746 |
return -1; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
if (cachable || stasis_topic_pool_topic_exists(device_state_topic_pool, device)) { |
| 761 |
topic = ast_device_state_topic(device); |
| 762 |
} else { |
| 763 |
topic = ast_device_state_topic_all(); |
| 764 |
} |
| 765 |
|
| 766 |
if (!topic) { |
| 767 |
return -1; |
| 768 |
} |
| 769 |
|
| 770 |
stasis_publish(topic, message); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
|
| 774 |
static const char *device_state_get_id(struct stasis_message *message) |
| 775 |
{ |
| 776 |
struct ast_device_state_message *device_state; |
| 777 |
|
| 778 |
if (ast_device_state_message_type() != stasis_message_type(message)) { |
| 779 |
return NULL; |
| 780 |
} |
| 781 |
|
| 782 |
device_state = stasis_message_data(message); |
| 783 |
if (device_state->cachable == AST_DEVSTATE_NOT_CACHABLE) { |
| 784 |
return NULL; |
| 785 |
} |
| 786 |
|
| 787 |
return device_state->device; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
static void device_state_aggregate_publish(struct stasis_topic *cache_topic, struct stasis_message *aggregate) |
| 799 |
{ |
| 800 |
const char *device; |
| 801 |
struct stasis_topic *device_specific_topic; |
| 802 |
|
| 803 |
device = device_state_get_id(aggregate); |
| 804 |
if (!device) { |
| 805 |
return; |
| 806 |
} |
| 807 |
device_specific_topic = ast_device_state_topic(device); |
| 808 |
if (!device_specific_topic) { |
| 809 |
return; |
| 810 |
} |
| 811 |
|
| 812 |
stasis_publish(device_specific_topic, aggregate); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
static struct stasis_message *device_state_aggregate_calc(struct stasis_cache_entry *entry, struct stasis_message *new_snapshot) |
| 830 |
{ |
| 831 |
struct stasis_message *aggregate_snapshot; |
| 832 |
struct stasis_message *snapshot; |
| 833 |
struct ast_device_state_message *device_state; |
| 834 |
const char *device = NULL; |
| 835 |
struct ast_devstate_aggregate aggregate; |
| 836 |
int idx; |
| 837 |
|
| 838 |
if (!ast_device_state_message_type()) { |
| 839 |
return NULL; |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
ast_devstate_aggregate_init(&aggregate); |
| 844 |
snapshot = stasis_cache_entry_get_local(entry); |
| 845 |
if (snapshot) { |
| 846 |
device_state = stasis_message_data(snapshot); |
| 847 |
device = device_state->device; |
| 848 |
ast_devstate_aggregate_add(&aggregate, device_state->state); |
| 849 |
} |
| 850 |
for (idx = 0; ; ++idx) { |
| 851 |
snapshot = stasis_cache_entry_get_remote(entry, idx); |
| 852 |
if (!snapshot) { |
| 853 |
break; |
| 854 |
} |
| 855 |
|
| 856 |
device_state = stasis_message_data(snapshot); |
| 857 |
device = device_state->device; |
| 858 |
ast_devstate_aggregate_add(&aggregate, device_state->state); |
| 859 |
} |
| 860 |
|
| 861 |
if (!device) { |
| 862 |
|
| 863 |
return NULL; |
| 864 |
} |
| 865 |
|
| 866 |
snapshot = stasis_cache_entry_get_aggregate(entry); |
| 867 |
if (snapshot) { |
| 868 |
device_state = stasis_message_data(snapshot); |
| 869 |
if (device_state->state == ast_devstate_aggregate_result(&aggregate)) { |
| 870 |
|
| 871 |
return ao2_bump(snapshot); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
device_state = device_state_alloc(device, ast_devstate_aggregate_result(&aggregate), |
| 876 |
AST_DEVSTATE_CACHABLE, NULL); |
| 877 |
if (!device_state) { |
| 878 |
|
| 879 |
return ao2_bump(snapshot); |
| 880 |
} |
| 881 |
aggregate_snapshot = stasis_message_create_full(ast_device_state_message_type(), |
| 882 |
device_state, NULL); |
| 883 |
ao2_cleanup(device_state); |
| 884 |
if (!aggregate_snapshot) { |
| 885 |
|
| 886 |
return ao2_bump(snapshot); |
| 887 |
} |
| 888 |
|
| 889 |
return aggregate_snapshot; |
| 890 |
} |
| 891 |
|
| 892 |
static void devstate_cleanup(void) |
| 893 |
{ |
| 894 |
devstate_message_sub = stasis_unsubscribe_and_join(devstate_message_sub); |
| 895 |
device_state_topic_cached = stasis_caching_unsubscribe_and_join(device_state_topic_cached); |
| 896 |
|
| 897 |
ao2_cleanup(device_state_cache); |
| 898 |
device_state_cache = NULL; |
| 899 |
|
| 900 |
ao2_cleanup(device_state_topic_pool); |
| 901 |
device_state_topic_pool = NULL; |
| 902 |
|
| 903 |
ao2_cleanup(device_state_topic_all); |
| 904 |
device_state_topic_all = NULL; |
| 905 |
|
| 906 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_device_state_message_type); |
| 907 |
} |
| 908 |
|
| 909 |
int devstate_init(void) |
| 910 |
{ |
| 911 |
ast_register_cleanup(devstate_cleanup); |
| 912 |
|
| 913 |
if (STASIS_MESSAGE_TYPE_INIT(ast_device_state_message_type) != 0) { |
| 914 |
return -1; |
| 915 |
} |
| 916 |
device_state_topic_all = stasis_topic_create("devicestate:all"); |
| 917 |
if (!device_state_topic_all) { |
| 918 |
return -1; |
| 919 |
} |
| 920 |
device_state_topic_pool = stasis_topic_pool_create(ast_device_state_topic_all()); |
| 921 |
if (!device_state_topic_pool) { |
| 922 |
return -1; |
| 923 |
} |
| 924 |
device_state_cache = stasis_cache_create_full(device_state_get_id, |
| 925 |
device_state_aggregate_calc, device_state_aggregate_publish); |
| 926 |
if (!device_state_cache) { |
| 927 |
return -1; |
| 928 |
} |
| 929 |
device_state_topic_cached = stasis_caching_topic_create(ast_device_state_topic_all(), |
| 930 |
device_state_cache); |
| 931 |
if (!device_state_topic_cached) { |
| 932 |
return -1; |
| 933 |
} |
| 934 |
stasis_caching_accept_message_type(device_state_topic_cached, ast_device_state_message_type()); |
| 935 |
stasis_caching_set_filter(device_state_topic_cached, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 936 |
|
| 937 |
devstate_message_sub = stasis_subscribe(ast_device_state_topic_all(), |
| 938 |
devstate_change_cb, NULL); |
| 939 |
if (!devstate_message_sub) { |
| 940 |
ast_log(LOG_ERROR, "Failed to create subscription creating uncached device state aggregate events.\n"); |
| 941 |
return -1; |
| 942 |
} |
| 943 |
stasis_subscription_accept_message_type(devstate_message_sub, ast_device_state_message_type()); |
| 944 |
stasis_subscription_set_filter(devstate_message_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 945 |
|
| 946 |
return 0; |
| 947 |
} |
| 948 |
|
| 949 |
static struct ast_manager_event_blob *devstate_to_ami(struct stasis_message *msg) |
| 950 |
{ |
| 951 |
struct ast_device_state_message *dev_state; |
| 952 |
|
| 953 |
dev_state = stasis_message_data(msg); |
| 954 |
|
| 955 |
|
| 956 |
if (dev_state->eid) { |
| 957 |
return NULL; |
| 958 |
} |
| 959 |
|
| 960 |
return ast_manager_event_blob_create(EVENT_FLAG_CALL, "DeviceStateChange", |
| 961 |
"Device: %s\r\n" |
| 962 |
"State: %s\r\n", |
| 963 |
dev_state->device, ast_devstate_str(dev_state->state)); |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
static struct ast_event *devstate_to_event(struct stasis_message *message) |
| 968 |
{ |
| 969 |
struct ast_event *event; |
| 970 |
struct ast_device_state_message *device_state; |
| 971 |
|
| 972 |
if (!message) { |
| 973 |
return NULL; |
| 974 |
} |
| 975 |
|
| 976 |
device_state = stasis_message_data(message); |
| 977 |
|
| 978 |
if (device_state->eid) { |
| 979 |
event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE, |
| 980 |
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device_state->device, |
| 981 |
AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, device_state->state, |
| 982 |
AST_EVENT_IE_CACHABLE, AST_EVENT_IE_PLTYPE_UINT, device_state->cachable, |
| 983 |
AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, device_state->eid, sizeof(*device_state->eid), |
| 984 |
AST_EVENT_IE_END); |
| 985 |
} else { |
| 986 |
event = ast_event_new(AST_EVENT_DEVICE_STATE, |
| 987 |
AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device_state->device, |
| 988 |
AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, device_state->state, |
| 989 |
AST_EVENT_IE_CACHABLE, AST_EVENT_IE_PLTYPE_UINT, device_state->cachable, |
| 990 |
AST_EVENT_IE_END); |
| 991 |
} |
| 992 |
|
| 993 |
return event; |
| 994 |
} |