| 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 |
#include "asterisk.h" |
| 62 |
|
| 63 |
#include "asterisk/_private.h" |
| 64 |
#include "asterisk/utils.h" |
| 65 |
#include "asterisk/lock.h" |
| 66 |
#include "asterisk/linkedlists.h" |
| 67 |
#include "asterisk/presencestate.h" |
| 68 |
#include "asterisk/pbx.h" |
| 69 |
#include "asterisk/app.h" |
| 70 |
#include "asterisk/test.h" |
| 71 |
|
| 72 |
|
| 73 |
static const struct { |
| 74 |
const char *string; |
| 75 |
enum ast_presence_state state; |
| 76 |
|
| 77 |
} state2string[] = { |
| 78 |
{ "not_set", AST_PRESENCE_NOT_SET}, |
| 79 |
{ "unavailable", AST_PRESENCE_UNAVAILABLE }, |
| 80 |
{ "available", AST_PRESENCE_AVAILABLE}, |
| 81 |
{ "away", AST_PRESENCE_AWAY}, |
| 82 |
{ "xa", AST_PRESENCE_XA}, |
| 83 |
{ "chat", AST_PRESENCE_CHAT}, |
| 84 |
{ "dnd", AST_PRESENCE_DND}, |
| 85 |
}; |
| 86 |
|
| 87 |
static struct ast_manager_event_blob *presence_state_to_ami(struct stasis_message *msg); |
| 88 |
|
| 89 |
STASIS_MESSAGE_TYPE_DEFN(ast_presence_state_message_type, |
| 90 |
.to_ami = presence_state_to_ami, |
| 91 |
); |
| 92 |
struct stasis_topic *presence_state_topic_all; |
| 93 |
struct stasis_cache *presence_state_cache; |
| 94 |
struct stasis_caching_topic *presence_state_topic_cached; |
| 95 |
|
| 96 |
|
| 97 |
struct presence_state_provider { |
| 98 |
char label[40]; |
| 99 |
ast_presence_state_prov_cb_type callback; |
| 100 |
AST_RWLIST_ENTRY(presence_state_provider) list; |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
static AST_RWLIST_HEAD_STATIC(presence_state_providers, presence_state_provider); |
| 105 |
|
| 106 |
const char *ast_presence_state2str(enum ast_presence_state state) |
| 107 |
{ |
| 108 |
int i; |
| 109 |
for (i = 0; i < ARRAY_LEN(state2string); i++) { |
| 110 |
if (state == state2string[i].state) { |
| 111 |
return state2string[i].string; |
| 112 |
} |
| 113 |
} |
| 114 |
return ""; |
| 115 |
} |
| 116 |
|
| 117 |
enum ast_presence_state ast_presence_state_val(const char *val) |
| 118 |
{ |
| 119 |
int i; |
| 120 |
for (i = 0; i < ARRAY_LEN(state2string); i++) { |
| 121 |
if (!strcasecmp(val, state2string[i].string)) { |
| 122 |
return state2string[i].state; |
| 123 |
} |
| 124 |
} |
| 125 |
return AST_PRESENCE_INVALID; |
| 126 |
} |
| 127 |
|
| 128 |
static enum ast_presence_state presence_state_cached(const char *presence_provider, char **subtype, char **message) |
| 129 |
{ |
| 130 |
enum ast_presence_state res = AST_PRESENCE_INVALID; |
| 131 |
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); |
| 132 |
struct ast_presence_state_message *presence_state; |
| 133 |
|
| 134 |
msg = stasis_cache_get(ast_presence_state_cache(), ast_presence_state_message_type(), presence_provider); |
| 135 |
|
| 136 |
if (!msg) { |
| 137 |
return res; |
| 138 |
} |
| 139 |
|
| 140 |
presence_state = stasis_message_data(msg); |
| 141 |
res = presence_state->state; |
| 142 |
|
| 143 |
*subtype = !ast_strlen_zero(presence_state->subtype) ? ast_strdup(presence_state->subtype) : NULL; |
| 144 |
*message = !ast_strlen_zero(presence_state->message) ? ast_strdup(presence_state->message) : NULL; |
| 145 |
|
| 146 |
return res; |
| 147 |
} |
| 148 |
|
| 149 |
static enum ast_presence_state ast_presence_state_helper(const char *presence_provider, char **subtype, char **message, int check_cache) |
| 150 |
{ |
| 151 |
char *labels = ast_strdupa(presence_provider); |
| 152 |
char *label; |
| 153 |
enum ast_presence_state state = AST_PRESENCE_INVALID; |
| 154 |
enum ast_presence_state state_order[] = { |
| 155 |
[AST_PRESENCE_INVALID] = 0, |
| 156 |
[AST_PRESENCE_NOT_SET] = 1, |
| 157 |
[AST_PRESENCE_AVAILABLE] = 2, |
| 158 |
[AST_PRESENCE_UNAVAILABLE] = 3, |
| 159 |
[AST_PRESENCE_CHAT] = 4, |
| 160 |
[AST_PRESENCE_AWAY] = 5, |
| 161 |
[AST_PRESENCE_XA] = 6, |
| 162 |
[AST_PRESENCE_DND] = 7 |
| 163 |
}; |
| 164 |
|
| 165 |
*subtype = NULL; |
| 166 |
*message = NULL; |
| 167 |
|
| 168 |
while ((label = strsep(&labels, "&"))) { |
| 169 |
enum ast_presence_state next_state = AST_PRESENCE_INVALID; |
| 170 |
char *next_subtype = NULL; |
| 171 |
char *next_message = NULL; |
| 172 |
|
| 173 |
if (check_cache) { |
| 174 |
next_state = presence_state_cached(label, &next_subtype, &next_message); |
| 175 |
} |
| 176 |
|
| 177 |
if (next_state == AST_PRESENCE_INVALID) { |
| 178 |
struct presence_state_provider *provider; |
| 179 |
const struct ast_channel_tech *chan_tech; |
| 180 |
char *address; |
| 181 |
|
| 182 |
if ((address = strchr(label, '/'))) { |
| 183 |
*address++ = '\0'; |
| 184 |
|
| 185 |
if ((chan_tech = ast_get_channel_tech(label)) && chan_tech->presencestate) { |
| 186 |
next_state = chan_tech->presencestate(address, &next_subtype, &next_message); |
| 187 |
} |
| 188 |
} else if ((address = strchr(label, ':'))) { |
| 189 |
*address++ = '\0'; |
| 190 |
|
| 191 |
AST_RWLIST_RDLOCK(&presence_state_providers); |
| 192 |
AST_RWLIST_TRAVERSE(&presence_state_providers, provider, list) { |
| 193 |
ast_debug(5, "Checking provider %s with %s\n", provider->label, label); |
| 194 |
|
| 195 |
if (!strcasecmp(provider->label, label)) { |
| 196 |
next_state = provider->callback(address, &next_subtype, &next_message); |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
AST_RWLIST_UNLOCK(&presence_state_providers); |
| 201 |
|
| 202 |
if (!provider) { |
| 203 |
ast_log(LOG_WARNING, "No provider found for label: %s\n", label); |
| 204 |
} |
| 205 |
} else { |
| 206 |
ast_log(LOG_WARNING, "No label found for presence state provider: %s\n", label); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (state_order[next_state] > state_order[state]) { |
| 211 |
state = next_state; |
| 212 |
|
| 213 |
ast_free(*subtype); |
| 214 |
ast_free(*message); |
| 215 |
|
| 216 |
*subtype = next_subtype; |
| 217 |
*message = next_message; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return state; |
| 222 |
} |
| 223 |
|
| 224 |
enum ast_presence_state ast_presence_state(const char *presence_provider, char **subtype, char **message) |
| 225 |
{ |
| 226 |
return ast_presence_state_helper(presence_provider, subtype, message, 1); |
| 227 |
} |
| 228 |
|
| 229 |
enum ast_presence_state ast_presence_state_nocache(const char *presence_provider, char **subtype, char **message) |
| 230 |
{ |
| 231 |
return ast_presence_state_helper(presence_provider, subtype, message, 0); |
| 232 |
} |
| 233 |
|
| 234 |
int ast_presence_state_prov_add(const char *label, ast_presence_state_prov_cb_type callback) |
| 235 |
{ |
| 236 |
struct presence_state_provider *provider; |
| 237 |
|
| 238 |
if (!callback || !(provider = ast_calloc(1, sizeof(*provider)))) { |
| 239 |
return -1; |
| 240 |
} |
| 241 |
|
| 242 |
provider->callback = callback; |
| 243 |
ast_copy_string(provider->label, label, sizeof(provider->label)); |
| 244 |
|
| 245 |
AST_RWLIST_WRLOCK(&presence_state_providers); |
| 246 |
AST_RWLIST_INSERT_HEAD(&presence_state_providers, provider, list); |
| 247 |
AST_RWLIST_UNLOCK(&presence_state_providers); |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
int ast_presence_state_prov_del(const char *label) |
| 252 |
{ |
| 253 |
struct presence_state_provider *provider; |
| 254 |
int res = -1; |
| 255 |
|
| 256 |
AST_RWLIST_WRLOCK(&presence_state_providers); |
| 257 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&presence_state_providers, provider, list) { |
| 258 |
if (!strcasecmp(provider->label, label)) { |
| 259 |
AST_RWLIST_REMOVE_CURRENT(list); |
| 260 |
ast_free(provider); |
| 261 |
res = 0; |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 266 |
AST_RWLIST_UNLOCK(&presence_state_providers); |
| 267 |
|
| 268 |
return res; |
| 269 |
} |
| 270 |
|
| 271 |
static void presence_state_dtor(void *obj) |
| 272 |
{ |
| 273 |
struct ast_presence_state_message *presence_state = obj; |
| 274 |
ast_string_field_free_memory(presence_state); |
| 275 |
} |
| 276 |
|
| 277 |
static struct ast_presence_state_message *presence_state_alloc(const char *provider, |
| 278 |
enum ast_presence_state state, |
| 279 |
const char *subtype, |
| 280 |
const char *message) |
| 281 |
{ |
| 282 |
RAII_VAR(struct ast_presence_state_message *, presence_state, ao2_alloc(sizeof(*presence_state), presence_state_dtor), ao2_cleanup); |
| 283 |
|
| 284 |
if (!presence_state || ast_string_field_init(presence_state, 256)) { |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
|
| 288 |
presence_state->state = state; |
| 289 |
ast_string_field_set(presence_state, provider, provider); |
| 290 |
ast_string_field_set(presence_state, subtype, S_OR(subtype, "")); |
| 291 |
ast_string_field_set(presence_state, message, S_OR(message, "")); |
| 292 |
|
| 293 |
ao2_ref(presence_state, +1); |
| 294 |
return presence_state; |
| 295 |
} |
| 296 |
|
| 297 |
static void presence_state_event(const char *provider, |
| 298 |
enum ast_presence_state state, |
| 299 |
const char *subtype, |
| 300 |
const char *message) |
| 301 |
{ |
| 302 |
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); |
| 303 |
RAII_VAR(struct ast_presence_state_message *, presence_state, NULL, ao2_cleanup); |
| 304 |
|
| 305 |
if (!ast_presence_state_message_type()) { |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
presence_state = presence_state_alloc(provider, state, subtype, message); |
| 310 |
if (!presence_state) { |
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
msg = stasis_message_create(ast_presence_state_message_type(), presence_state); |
| 315 |
if (!msg) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
stasis_publish(ast_presence_state_topic_all(), msg); |
| 320 |
} |
| 321 |
|
| 322 |
static void do_presence_state_change(const char *provider) |
| 323 |
{ |
| 324 |
char *subtype = NULL; |
| 325 |
char *message = NULL; |
| 326 |
enum ast_presence_state state; |
| 327 |
|
| 328 |
state = ast_presence_state_helper(provider, &subtype, &message, 0); |
| 329 |
|
| 330 |
if (state == AST_PRESENCE_INVALID) { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
presence_state_event(provider, state, subtype, message); |
| 335 |
ast_free(subtype); |
| 336 |
ast_free(message); |
| 337 |
} |
| 338 |
|
| 339 |
int ast_presence_state_changed_literal(enum ast_presence_state state, |
| 340 |
const char *subtype, |
| 341 |
const char *message, |
| 342 |
const char *presence_provider) |
| 343 |
{ |
| 344 |
if (state == AST_PRESENCE_NOT_SET) { |
| 345 |
do_presence_state_change(presence_provider); |
| 346 |
} else { |
| 347 |
presence_state_event(presence_provider, state, subtype, message); |
| 348 |
} |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
int ast_presence_state_changed(enum ast_presence_state state, |
| 354 |
const char *subtype, |
| 355 |
const char *message, |
| 356 |
const char *fmt, ...) |
| 357 |
{ |
| 358 |
char buf[AST_MAX_EXTENSION]; |
| 359 |
va_list ap; |
| 360 |
|
| 361 |
va_start(ap, fmt); |
| 362 |
vsnprintf(buf, sizeof(buf), fmt, ap); |
| 363 |
va_end(ap); |
| 364 |
|
| 365 |
return ast_presence_state_changed_literal(state, subtype, message, buf); |
| 366 |
} |
| 367 |
|
| 368 |
struct stasis_topic *ast_presence_state_topic_all(void) |
| 369 |
{ |
| 370 |
return presence_state_topic_all; |
| 371 |
} |
| 372 |
|
| 373 |
struct stasis_cache *ast_presence_state_cache(void) |
| 374 |
{ |
| 375 |
return presence_state_cache; |
| 376 |
} |
| 377 |
|
| 378 |
struct stasis_topic *ast_presence_state_topic_cached(void) |
| 379 |
{ |
| 380 |
return stasis_caching_get_topic(presence_state_topic_cached); |
| 381 |
} |
| 382 |
|
| 383 |
static const char *presence_state_get_id(struct stasis_message *msg) |
| 384 |
{ |
| 385 |
struct ast_presence_state_message *presence_state = stasis_message_data(msg); |
| 386 |
|
| 387 |
if (stasis_message_type(msg) != ast_presence_state_message_type()) { |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
return presence_state->provider; |
| 392 |
} |
| 393 |
|
| 394 |
#if defined(TEST_FRAMEWORK) |
| 395 |
|
| 396 |
#define TEST_CATEGORY "/main/presence/" |
| 397 |
|
| 398 |
static int presence_test_alice_state = AST_PRESENCE_UNAVAILABLE; |
| 399 |
static int presence_test_bob_state = AST_PRESENCE_UNAVAILABLE; |
| 400 |
|
| 401 |
static int presence_test_presencestate(const char *label, char **subtype, char **message) |
| 402 |
{ |
| 403 |
if (!strcmp(label, "Alice")) { |
| 404 |
return presence_test_alice_state; |
| 405 |
} else if (!strcmp(label, "Bob")) { |
| 406 |
return presence_test_bob_state; |
| 407 |
} else { |
| 408 |
return AST_PRESENCE_UNAVAILABLE; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
static struct ast_channel_tech presence_test_tech = { |
| 413 |
.type = "PresenceTestChannel", |
| 414 |
.description = "Presence test technology", |
| 415 |
.presencestate = presence_test_presencestate, |
| 416 |
}; |
| 417 |
|
| 418 |
AST_TEST_DEFINE(test_presence_chan) |
| 419 |
{ |
| 420 |
int res = AST_TEST_FAIL; |
| 421 |
char provider[80]; |
| 422 |
enum ast_presence_state state; |
| 423 |
char *subtype = NULL, *message = NULL; |
| 424 |
|
| 425 |
switch (cmd) { |
| 426 |
case TEST_INIT: |
| 427 |
info->name = "channel_presence"; |
| 428 |
info->category = TEST_CATEGORY; |
| 429 |
info->summary = "Channel presence state tests"; |
| 430 |
info->description = "Creates test channel technology and then test the presence state callback"; |
| 431 |
return AST_TEST_NOT_RUN; |
| 432 |
case TEST_EXECUTE: |
| 433 |
break; |
| 434 |
} |
| 435 |
|
| 436 |
if (ast_channel_register(&presence_test_tech)) { |
| 437 |
ast_log(LOG_WARNING, "Unable to register channel type '%s'\n", presence_test_tech.type); |
| 438 |
goto presence_test_cleanup; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
snprintf(provider, sizeof(provider), "%s/Alice", presence_test_tech.type); |
| 443 |
|
| 444 |
presence_test_alice_state = AST_PRESENCE_AVAILABLE; |
| 445 |
state = ast_presence_state_nocache(provider, &subtype, &message); |
| 446 |
|
| 447 |
if (state != presence_test_alice_state) { |
| 448 |
ast_log(LOG_WARNING, "Presence state of '%s' returned '%s' instead of the expected value '%s'\n", |
| 449 |
provider, ast_presence_state2str(state), ast_presence_state2str(presence_test_alice_state)); |
| 450 |
goto presence_test_cleanup; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
snprintf(provider, sizeof(provider), "%s/Alice&%s/Bob", presence_test_tech.type, presence_test_tech.type); |
| 455 |
|
| 456 |
presence_test_alice_state = AST_PRESENCE_DND; |
| 457 |
presence_test_bob_state = AST_PRESENCE_UNAVAILABLE; |
| 458 |
state = ast_presence_state_nocache(provider, &subtype, &message); |
| 459 |
|
| 460 |
if (state != presence_test_alice_state) { |
| 461 |
ast_log(LOG_WARNING, "Presence state of '%s' returned '%s' instead of the expected value '%s'\n", |
| 462 |
provider, ast_presence_state2str(state), ast_presence_state2str(presence_test_alice_state)); |
| 463 |
goto presence_test_cleanup; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
presence_test_alice_state = AST_PRESENCE_AVAILABLE; |
| 468 |
state = ast_presence_state_nocache(provider, &subtype, &message); |
| 469 |
|
| 470 |
if (state != presence_test_bob_state) { |
| 471 |
ast_log(LOG_WARNING, "Presence state of '%s' returned '%s' instead of the expected value '%s'\n", |
| 472 |
provider, ast_presence_state2str(state), ast_presence_state2str(presence_test_bob_state)); |
| 473 |
goto presence_test_cleanup; |
| 474 |
} |
| 475 |
|
| 476 |
res = AST_TEST_PASS; |
| 477 |
|
| 478 |
presence_test_cleanup: |
| 479 |
ast_channel_unregister(&presence_test_tech); |
| 480 |
ast_free(subtype); |
| 481 |
ast_free(message); |
| 482 |
|
| 483 |
return res; |
| 484 |
} |
| 485 |
#endif |
| 486 |
|
| 487 |
static void presence_state_engine_cleanup(void) |
| 488 |
{ |
| 489 |
ao2_cleanup(presence_state_topic_all); |
| 490 |
presence_state_topic_all = NULL; |
| 491 |
ao2_cleanup(presence_state_cache); |
| 492 |
presence_state_cache = NULL; |
| 493 |
presence_state_topic_cached = stasis_caching_unsubscribe_and_join(presence_state_topic_cached); |
| 494 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_presence_state_message_type); |
| 495 |
AST_TEST_UNREGISTER(test_presence_chan); |
| 496 |
} |
| 497 |
|
| 498 |
int ast_presence_state_engine_init(void) |
| 499 |
{ |
| 500 |
ast_register_cleanup(presence_state_engine_cleanup); |
| 501 |
|
| 502 |
if (STASIS_MESSAGE_TYPE_INIT(ast_presence_state_message_type) != 0) { |
| 503 |
return -1; |
| 504 |
} |
| 505 |
|
| 506 |
presence_state_topic_all = stasis_topic_create("presence_state:all"); |
| 507 |
if (!presence_state_topic_all) { |
| 508 |
return -1; |
| 509 |
} |
| 510 |
|
| 511 |
presence_state_cache = stasis_cache_create(presence_state_get_id); |
| 512 |
if (!presence_state_cache) { |
| 513 |
return -1; |
| 514 |
} |
| 515 |
|
| 516 |
presence_state_topic_cached = stasis_caching_topic_create(presence_state_topic_all, presence_state_cache); |
| 517 |
if (!presence_state_topic_cached) { |
| 518 |
return -1; |
| 519 |
} |
| 520 |
stasis_caching_accept_message_type(presence_state_topic_cached, ast_presence_state_message_type()); |
| 521 |
stasis_caching_set_filter(presence_state_topic_cached, STASIS_SUBSCRIPTION_FILTER_SELECTIVE); |
| 522 |
|
| 523 |
AST_TEST_REGISTER(test_presence_chan); |
| 524 |
|
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
static struct ast_manager_event_blob *presence_state_to_ami(struct stasis_message *msg) |
| 529 |
{ |
| 530 |
struct ast_presence_state_message *presence_state = stasis_message_data(msg); |
| 531 |
struct ast_manager_event_blob *res; |
| 532 |
|
| 533 |
char *subtype = ast_escape_c_alloc(presence_state->subtype); |
| 534 |
char *message = ast_escape_c_alloc(presence_state->message); |
| 535 |
|
| 536 |
res = ast_manager_event_blob_create(EVENT_FLAG_CALL, "PresenceStateChange", |
| 537 |
"Presentity: %s\r\n" |
| 538 |
"Status: %s\r\n" |
| 539 |
"Subtype: %s\r\n" |
| 540 |
"Message: %s\r\n", |
| 541 |
presence_state->provider, |
| 542 |
ast_presence_state2str(presence_state->state), |
| 543 |
subtype ?: "", |
| 544 |
message ?: ""); |
| 545 |
|
| 546 |
ast_free(subtype); |
| 547 |
ast_free(message); |
| 548 |
|
| 549 |
return res; |
| 550 |
} |