| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/astobj2.h" |
| 33 |
#include "asterisk/json.h" |
| 34 |
#include "asterisk/stasis.h" |
| 35 |
#include "asterisk/stasis_cache_pattern.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/stasis_bridges.h" |
| 38 |
#include "asterisk/stasis_channels.h" |
| 39 |
#include "asterisk/bridge.h" |
| 40 |
#include "asterisk/bridge_technology.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#define SNAPSHOT_CHANNELS_BUCKETS 1 |
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
static struct ast_json *attended_transfer_to_json(struct stasis_message *msg, |
| 151 |
const struct stasis_message_sanitizer *sanitize); |
| 152 |
static struct ast_manager_event_blob *attended_transfer_to_ami(struct stasis_message *message); |
| 153 |
static struct ast_json *blind_transfer_to_json(struct stasis_message *msg, |
| 154 |
const struct stasis_message_sanitizer *sanitize); |
| 155 |
static struct ast_manager_event_blob *blind_transfer_to_ami(struct stasis_message *message); |
| 156 |
static struct ast_json *ast_channel_entered_bridge_to_json( |
| 157 |
struct stasis_message *msg, |
| 158 |
const struct stasis_message_sanitizer *sanitize); |
| 159 |
static struct ast_json *ast_channel_left_bridge_to_json( |
| 160 |
struct stasis_message *msg, |
| 161 |
const struct stasis_message_sanitizer *sanitize); |
| 162 |
static struct ast_json *ast_bridge_merge_message_to_json( |
| 163 |
struct stasis_message *msg, |
| 164 |
const struct stasis_message_sanitizer *sanitize); |
| 165 |
|
| 166 |
static struct stasis_topic *bridge_topic_all; |
| 167 |
static struct stasis_topic_pool *bridge_topic_pool; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
STASIS_MESSAGE_TYPE_DEFN(ast_bridge_snapshot_type); |
| 173 |
STASIS_MESSAGE_TYPE_DEFN(ast_bridge_merge_message_type, |
| 174 |
.to_json = ast_bridge_merge_message_to_json); |
| 175 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_entered_bridge_type, |
| 176 |
.to_json = ast_channel_entered_bridge_to_json); |
| 177 |
STASIS_MESSAGE_TYPE_DEFN(ast_channel_left_bridge_type, |
| 178 |
.to_json = ast_channel_left_bridge_to_json); |
| 179 |
STASIS_MESSAGE_TYPE_DEFN(ast_blind_transfer_type, |
| 180 |
.to_json = blind_transfer_to_json, |
| 181 |
.to_ami = blind_transfer_to_ami); |
| 182 |
STASIS_MESSAGE_TYPE_DEFN(ast_attended_transfer_type, |
| 183 |
.to_json = attended_transfer_to_json, |
| 184 |
.to_ami = attended_transfer_to_ami); |
| 185 |
|
| 186 |
|
| 187 |
struct stasis_topic *ast_bridge_topic_all(void) |
| 188 |
{ |
| 189 |
return bridge_topic_all; |
| 190 |
} |
| 191 |
|
| 192 |
struct stasis_topic *ast_bridge_topic(struct ast_bridge *bridge) |
| 193 |
{ |
| 194 |
if (!bridge) { |
| 195 |
return ast_bridge_topic_all(); |
| 196 |
} |
| 197 |
|
| 198 |
return bridge->topic; |
| 199 |
} |
| 200 |
|
| 201 |
int ast_bridge_topic_exists(const char *uniqueid) |
| 202 |
{ |
| 203 |
char *topic_name; |
| 204 |
int ret; |
| 205 |
|
| 206 |
if (ast_strlen_zero(uniqueid)) { |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
ret = ast_asprintf(&topic_name, "bridge:%s", uniqueid); |
| 211 |
if (ret < 0) { |
| 212 |
return 0; |
| 213 |
} |
| 214 |
ret = stasis_topic_pool_topic_exists(bridge_topic_pool, topic_name); |
| 215 |
ast_free(topic_name); |
| 216 |
|
| 217 |
return ret; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
static void bridge_snapshot_dtor(void *obj) |
| 223 |
{ |
| 224 |
struct ast_bridge_snapshot *snapshot = obj; |
| 225 |
ast_string_field_free_memory(snapshot); |
| 226 |
ao2_cleanup(snapshot->channels); |
| 227 |
snapshot->channels = NULL; |
| 228 |
ast_var_list_destroy(snapshot->bridgevars); |
| 229 |
snapshot->bridgevars = NULL; |
| 230 |
} |
| 231 |
|
| 232 |
struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge) |
| 233 |
{ |
| 234 |
struct ast_bridge_snapshot *snapshot; |
| 235 |
struct ast_bridge_channel *bridge_channel; |
| 236 |
|
| 237 |
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
snapshot = ao2_alloc_options(sizeof(*snapshot), bridge_snapshot_dtor, |
| 242 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 243 |
if (!snapshot) { |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
if (ast_string_field_init(snapshot, 128)) { |
| 248 |
ao2_ref(snapshot, -1); |
| 249 |
|
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
snapshot->channels = ast_str_container_alloc(SNAPSHOT_CHANNELS_BUCKETS); |
| 254 |
if (!snapshot->channels) { |
| 255 |
ao2_ref(snapshot, -1); |
| 256 |
|
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) { |
| 261 |
if (ast_str_container_add(snapshot->channels, |
| 262 |
ast_channel_uniqueid(bridge_channel->chan))) { |
| 263 |
ao2_ref(snapshot, -1); |
| 264 |
|
| 265 |
return NULL; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
ast_string_field_set(snapshot, uniqueid, bridge->uniqueid); |
| 270 |
ast_string_field_set(snapshot, technology, bridge->technology->name); |
| 271 |
ast_string_field_set(snapshot, subclass, bridge->v_table->name); |
| 272 |
ast_string_field_set(snapshot, creator, bridge->creator); |
| 273 |
ast_string_field_set(snapshot, name, bridge->name); |
| 274 |
|
| 275 |
snapshot->feature_flags = bridge->feature_flags; |
| 276 |
snapshot->capabilities = bridge->technology->capabilities; |
| 277 |
snapshot->num_channels = bridge->num_channels; |
| 278 |
snapshot->num_active = bridge->num_active; |
| 279 |
snapshot->creationtime = bridge->creationtime; |
| 280 |
snapshot->video_mode = bridge->softmix.video_mode.mode; |
| 281 |
if (snapshot->video_mode == AST_BRIDGE_VIDEO_MODE_SINGLE_SRC |
| 282 |
&& bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) { |
| 283 |
ast_string_field_set(snapshot, video_source_id, |
| 284 |
ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc)); |
| 285 |
} else if (snapshot->video_mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC |
| 286 |
&& bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) { |
| 287 |
ast_string_field_set(snapshot, video_source_id, |
| 288 |
ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc)); |
| 289 |
} |
| 290 |
|
| 291 |
snapshot->bridgevars = ast_bridge_get_ari_reportable_variables(bridge); |
| 292 |
|
| 293 |
return snapshot; |
| 294 |
} |
| 295 |
|
| 296 |
static void bridge_snapshot_update_dtor(void *obj) |
| 297 |
{ |
| 298 |
struct ast_bridge_snapshot_update *update = obj; |
| 299 |
|
| 300 |
ast_debug(3, "Update: %p Old: %s New: %s\n", update, |
| 301 |
update->old_snapshot ? update->old_snapshot->uniqueid : "<none>", |
| 302 |
update->new_snapshot ? update->new_snapshot->uniqueid : "<none>"); |
| 303 |
ao2_cleanup(update->old_snapshot); |
| 304 |
ao2_cleanup(update->new_snapshot); |
| 305 |
} |
| 306 |
|
| 307 |
static struct ast_bridge_snapshot_update *bridge_snapshot_update_create( |
| 308 |
struct ast_bridge_snapshot *old, struct ast_bridge_snapshot *new) |
| 309 |
{ |
| 310 |
struct ast_bridge_snapshot_update *update; |
| 311 |
|
| 312 |
update = ao2_alloc_options(sizeof(*update), bridge_snapshot_update_dtor, |
| 313 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 314 |
if (!update) { |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
update->old_snapshot = ao2_bump(old); |
| 318 |
update->new_snapshot = ao2_bump(new); |
| 319 |
|
| 320 |
ast_debug(3, "Update: %p Old: %s New: %s\n", update, |
| 321 |
update->old_snapshot ? update->old_snapshot->uniqueid : "<none>", |
| 322 |
update->new_snapshot ? update->new_snapshot->uniqueid : "<none>"); |
| 323 |
|
| 324 |
return update; |
| 325 |
} |
| 326 |
|
| 327 |
int bridge_topics_init(struct ast_bridge *bridge) |
| 328 |
{ |
| 329 |
char *topic_name; |
| 330 |
int ret; |
| 331 |
|
| 332 |
if (ast_strlen_zero(bridge->uniqueid)) { |
| 333 |
ast_log(LOG_ERROR, "Bridge id initialization required\n"); |
| 334 |
return -1; |
| 335 |
} |
| 336 |
|
| 337 |
ret = ast_asprintf(&topic_name, "bridge:%s", bridge->uniqueid); |
| 338 |
if (ret < 0) { |
| 339 |
return -1; |
| 340 |
} |
| 341 |
|
| 342 |
bridge->topic = stasis_topic_pool_get_topic(bridge_topic_pool, topic_name); |
| 343 |
ast_free(topic_name); |
| 344 |
if (!bridge->topic) { |
| 345 |
return -1; |
| 346 |
} |
| 347 |
ao2_bump(bridge->topic); |
| 348 |
|
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
void bridge_topics_destroy(struct ast_bridge *bridge) |
| 353 |
{ |
| 354 |
struct ast_bridge_snapshot_update *update; |
| 355 |
struct stasis_message *msg; |
| 356 |
|
| 357 |
ast_assert(bridge != NULL); |
| 358 |
ast_debug(1, "Bridge " BRIDGE_PRINTF_SPEC ": destroying topics\n", |
| 359 |
BRIDGE_PRINTF_VARS(bridge)); |
| 360 |
|
| 361 |
if (!bridge->topic) { |
| 362 |
ast_log(LOG_WARNING, "Bridge " BRIDGE_PRINTF_SPEC " topic is NULL\n", |
| 363 |
BRIDGE_PRINTF_VARS(bridge)); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
if (!bridge->current_snapshot) { |
| 368 |
bridge->current_snapshot = ast_bridge_snapshot_create(bridge); |
| 369 |
if (!bridge->current_snapshot) { |
| 370 |
return; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
update = bridge_snapshot_update_create(bridge->current_snapshot, NULL); |
| 375 |
if (!update) { |
| 376 |
return; |
| 377 |
} |
| 378 |
|
| 379 |
msg = stasis_message_create(ast_bridge_snapshot_type(), update); |
| 380 |
ao2_ref(update, -1); |
| 381 |
if (!msg) { |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
stasis_publish(ast_bridge_topic(bridge), msg); |
| 386 |
ao2_ref(msg, -1); |
| 387 |
|
| 388 |
stasis_topic_pool_delete_topic(bridge_topic_pool, stasis_topic_name(ast_bridge_topic(bridge))); |
| 389 |
ao2_cleanup(bridge->topic); |
| 390 |
bridge->topic = NULL; |
| 391 |
} |
| 392 |
|
| 393 |
void ast_bridge_publish_state(struct ast_bridge *bridge) |
| 394 |
{ |
| 395 |
struct ast_bridge_snapshot *new_snapshot; |
| 396 |
struct ast_bridge_snapshot_update *update; |
| 397 |
struct stasis_message *msg; |
| 398 |
|
| 399 |
ast_assert(bridge != NULL); |
| 400 |
|
| 401 |
new_snapshot = ast_bridge_snapshot_create(bridge); |
| 402 |
if (!new_snapshot) { |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
update = bridge_snapshot_update_create(bridge->current_snapshot, new_snapshot); |
| 407 |
|
| 408 |
ao2_cleanup(bridge->current_snapshot); |
| 409 |
bridge->current_snapshot = new_snapshot; |
| 410 |
if (!update) { |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
msg = stasis_message_create(ast_bridge_snapshot_type(), update); |
| 415 |
ao2_ref(update, -1); |
| 416 |
if (!msg) { |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
stasis_publish(ast_bridge_topic(bridge), msg); |
| 421 |
ao2_ref(msg, -1); |
| 422 |
} |
| 423 |
|
| 424 |
static void bridge_publish_state_from_blob(struct ast_bridge *bridge, |
| 425 |
struct ast_bridge_blob *obj) |
| 426 |
{ |
| 427 |
struct ast_bridge_snapshot_update *update; |
| 428 |
struct stasis_message *msg; |
| 429 |
|
| 430 |
ast_assert(obj != NULL); |
| 431 |
|
| 432 |
update = bridge_snapshot_update_create(bridge->current_snapshot, obj->bridge); |
| 433 |
ao2_cleanup(bridge->current_snapshot); |
| 434 |
bridge->current_snapshot = ao2_bump(obj->bridge); |
| 435 |
if (!update) { |
| 436 |
return; |
| 437 |
} |
| 438 |
|
| 439 |
msg = stasis_message_create(ast_bridge_snapshot_type(), update); |
| 440 |
ao2_ref(update, -1); |
| 441 |
if (!msg) { |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
stasis_publish(ast_bridge_topic(bridge), msg); |
| 446 |
ao2_ref(msg, -1); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
static void bridge_merge_message_dtor(void *obj) |
| 451 |
{ |
| 452 |
struct ast_bridge_merge_message *msg = obj; |
| 453 |
|
| 454 |
ao2_cleanup(msg->to); |
| 455 |
msg->to = NULL; |
| 456 |
ao2_cleanup(msg->from); |
| 457 |
msg->from = NULL; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_bridge *to, struct ast_bridge *from) |
| 462 |
{ |
| 463 |
struct ast_bridge_merge_message *msg; |
| 464 |
|
| 465 |
msg = ao2_alloc(sizeof(*msg), bridge_merge_message_dtor); |
| 466 |
if (!msg) { |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
msg->to = ast_bridge_snapshot_create(to); |
| 471 |
msg->from = ast_bridge_snapshot_create(from); |
| 472 |
if (!msg->to || !msg->from) { |
| 473 |
ao2_ref(msg, -1); |
| 474 |
|
| 475 |
return NULL; |
| 476 |
} |
| 477 |
|
| 478 |
return msg; |
| 479 |
} |
| 480 |
|
| 481 |
static struct ast_json *ast_bridge_merge_message_to_json( |
| 482 |
struct stasis_message *msg, |
| 483 |
const struct stasis_message_sanitizer *sanitize) |
| 484 |
{ |
| 485 |
struct ast_bridge_merge_message *merge = stasis_message_data(msg); |
| 486 |
struct ast_json *json_bridge_to = ast_bridge_snapshot_to_json(merge->to, sanitize); |
| 487 |
struct ast_json *json_bridge_from = ast_bridge_snapshot_to_json(merge->from, sanitize); |
| 488 |
|
| 489 |
if (!json_bridge_to || !json_bridge_from) { |
| 490 |
ast_json_unref(json_bridge_to); |
| 491 |
ast_json_unref(json_bridge_from); |
| 492 |
|
| 493 |
return NULL; |
| 494 |
} |
| 495 |
|
| 496 |
return ast_json_pack("{s: s, s: o, s: o, s: o}", |
| 497 |
"type", "BridgeMerged", |
| 498 |
"timestamp", ast_json_timeval(*stasis_message_timestamp(msg), NULL), |
| 499 |
"bridge", json_bridge_to, |
| 500 |
"bridge_from", json_bridge_from); |
| 501 |
} |
| 502 |
|
| 503 |
void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from) |
| 504 |
{ |
| 505 |
struct ast_bridge_merge_message *merge_msg; |
| 506 |
struct stasis_message *msg; |
| 507 |
|
| 508 |
if (!ast_bridge_merge_message_type()) { |
| 509 |
return; |
| 510 |
} |
| 511 |
|
| 512 |
ast_assert(to != NULL); |
| 513 |
ast_assert(from != NULL); |
| 514 |
ast_assert(ast_test_flag(&to->feature_flags, AST_BRIDGE_FLAG_INVISIBLE) == 0); |
| 515 |
ast_assert(ast_test_flag(&from->feature_flags, AST_BRIDGE_FLAG_INVISIBLE) == 0); |
| 516 |
|
| 517 |
merge_msg = bridge_merge_message_create(to, from); |
| 518 |
if (!merge_msg) { |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
msg = stasis_message_create(ast_bridge_merge_message_type(), merge_msg); |
| 523 |
ao2_ref(merge_msg, -1); |
| 524 |
if (!msg) { |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
stasis_publish(ast_bridge_topic_all(), msg); |
| 529 |
ao2_ref(msg, -1); |
| 530 |
} |
| 531 |
|
| 532 |
static void bridge_blob_dtor(void *obj) |
| 533 |
{ |
| 534 |
struct ast_bridge_blob *event = obj; |
| 535 |
ao2_cleanup(event->bridge); |
| 536 |
event->bridge = NULL; |
| 537 |
ao2_cleanup(event->channel); |
| 538 |
event->channel = NULL; |
| 539 |
ast_json_unref(event->blob); |
| 540 |
event->blob = NULL; |
| 541 |
} |
| 542 |
|
| 543 |
struct stasis_message *ast_bridge_blob_create( |
| 544 |
struct stasis_message_type *message_type, |
| 545 |
struct ast_bridge *bridge, |
| 546 |
struct ast_channel *chan, |
| 547 |
struct ast_json *blob) |
| 548 |
{ |
| 549 |
struct ast_bridge_blob *obj; |
| 550 |
struct stasis_message *msg; |
| 551 |
|
| 552 |
if (!message_type) { |
| 553 |
return NULL; |
| 554 |
} |
| 555 |
|
| 556 |
obj = ao2_alloc(sizeof(*obj), bridge_blob_dtor); |
| 557 |
if (!obj) { |
| 558 |
return NULL; |
| 559 |
} |
| 560 |
|
| 561 |
if (bridge) { |
| 562 |
obj->bridge = ast_bridge_snapshot_create(bridge); |
| 563 |
if (obj->bridge == NULL) { |
| 564 |
ao2_ref(obj, -1); |
| 565 |
|
| 566 |
return NULL; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if (chan) { |
| 571 |
obj->channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)); |
| 572 |
if (obj->channel == NULL) { |
| 573 |
ao2_ref(obj, -1); |
| 574 |
|
| 575 |
return NULL; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
if (blob) { |
| 580 |
obj->blob = ast_json_ref(blob); |
| 581 |
} |
| 582 |
|
| 583 |
msg = stasis_message_create(message_type, obj); |
| 584 |
ao2_ref(obj, -1); |
| 585 |
|
| 586 |
return msg; |
| 587 |
} |
| 588 |
|
| 589 |
struct stasis_message *ast_bridge_blob_create_from_snapshots( |
| 590 |
struct stasis_message_type *message_type, |
| 591 |
struct ast_bridge_snapshot *bridge_snapshot, |
| 592 |
struct ast_channel_snapshot *chan_snapshot, |
| 593 |
struct ast_json *blob) |
| 594 |
{ |
| 595 |
struct ast_bridge_blob *obj; |
| 596 |
struct stasis_message *msg; |
| 597 |
|
| 598 |
if (!message_type) { |
| 599 |
return NULL; |
| 600 |
} |
| 601 |
|
| 602 |
obj = ao2_alloc(sizeof(*obj), bridge_blob_dtor); |
| 603 |
if (!obj) { |
| 604 |
return NULL; |
| 605 |
} |
| 606 |
|
| 607 |
if (bridge_snapshot) { |
| 608 |
obj->bridge = ao2_bump(bridge_snapshot); |
| 609 |
} |
| 610 |
|
| 611 |
if (chan_snapshot) { |
| 612 |
obj->channel = ao2_bump(chan_snapshot); |
| 613 |
} |
| 614 |
|
| 615 |
if (blob) { |
| 616 |
obj->blob = ast_json_ref(blob); |
| 617 |
} |
| 618 |
|
| 619 |
msg = stasis_message_create(message_type, obj); |
| 620 |
ao2_ref(obj, -1); |
| 621 |
|
| 622 |
return msg; |
| 623 |
} |
| 624 |
|
| 625 |
void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan, |
| 626 |
struct ast_channel *swap) |
| 627 |
{ |
| 628 |
struct stasis_message *msg; |
| 629 |
struct ast_json *blob = NULL; |
| 630 |
|
| 631 |
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { |
| 632 |
return; |
| 633 |
} |
| 634 |
|
| 635 |
if (swap) { |
| 636 |
blob = ast_json_pack("{s: s}", "swap", ast_channel_uniqueid(swap)); |
| 637 |
if (!blob) { |
| 638 |
return; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
msg = ast_bridge_blob_create(ast_channel_entered_bridge_type(), bridge, chan, blob); |
| 643 |
ast_json_unref(blob); |
| 644 |
if (!msg) { |
| 645 |
return; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
stasis_publish(ast_bridge_topic(bridge), msg); |
| 650 |
bridge_publish_state_from_blob(bridge, stasis_message_data(msg)); |
| 651 |
ao2_ref(msg, -1); |
| 652 |
} |
| 653 |
|
| 654 |
void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan) |
| 655 |
{ |
| 656 |
struct stasis_message *msg; |
| 657 |
|
| 658 |
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { |
| 659 |
return; |
| 660 |
} |
| 661 |
msg = ast_bridge_blob_create(ast_channel_left_bridge_type(), bridge, chan, NULL); |
| 662 |
if (!msg) { |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
bridge_publish_state_from_blob(bridge, stasis_message_data(msg)); |
| 668 |
stasis_publish(ast_bridge_topic(bridge), msg); |
| 669 |
ao2_ref(msg, -1); |
| 670 |
} |
| 671 |
|
| 672 |
static struct ast_json *simple_bridge_channel_event( |
| 673 |
const char *type, |
| 674 |
struct ast_bridge_snapshot *bridge_snapshot, |
| 675 |
struct ast_channel_snapshot *channel_snapshot, |
| 676 |
const struct timeval *tv, |
| 677 |
const struct stasis_message_sanitizer *sanitize) |
| 678 |
{ |
| 679 |
struct ast_json *json_bridge = ast_bridge_snapshot_to_json(bridge_snapshot, sanitize); |
| 680 |
struct ast_json *json_channel = ast_channel_snapshot_to_json(channel_snapshot, sanitize); |
| 681 |
|
| 682 |
if (!json_bridge || !json_channel) { |
| 683 |
ast_json_unref(json_bridge); |
| 684 |
ast_json_unref(json_channel); |
| 685 |
|
| 686 |
return NULL; |
| 687 |
} |
| 688 |
|
| 689 |
return ast_json_pack("{s: s, s: o, s: o, s: o}", |
| 690 |
"type", type, |
| 691 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 692 |
"bridge", json_bridge, |
| 693 |
"channel", json_channel); |
| 694 |
} |
| 695 |
|
| 696 |
struct ast_json *ast_channel_entered_bridge_to_json( |
| 697 |
struct stasis_message *msg, |
| 698 |
const struct stasis_message_sanitizer *sanitize) |
| 699 |
{ |
| 700 |
struct ast_bridge_blob *obj = stasis_message_data(msg); |
| 701 |
|
| 702 |
return simple_bridge_channel_event("ChannelEnteredBridge", obj->bridge, |
| 703 |
obj->channel, stasis_message_timestamp(msg), sanitize); |
| 704 |
} |
| 705 |
|
| 706 |
struct ast_json *ast_channel_left_bridge_to_json( |
| 707 |
struct stasis_message *msg, |
| 708 |
const struct stasis_message_sanitizer *sanitize) |
| 709 |
{ |
| 710 |
struct ast_bridge_blob *obj = stasis_message_data(msg); |
| 711 |
|
| 712 |
return simple_bridge_channel_event("ChannelLeftBridge", obj->bridge, |
| 713 |
obj->channel, stasis_message_timestamp(msg), sanitize); |
| 714 |
} |
| 715 |
|
| 716 |
static struct ast_json *container_to_json_array(struct ao2_container *items, |
| 717 |
const struct stasis_message_sanitizer *sanitize) |
| 718 |
{ |
| 719 |
struct ast_json *json_items = ast_json_array_create(); |
| 720 |
char *item; |
| 721 |
struct ao2_iterator it; |
| 722 |
|
| 723 |
if (!json_items) { |
| 724 |
return NULL; |
| 725 |
} |
| 726 |
|
| 727 |
for (it = ao2_iterator_init(items, 0); |
| 728 |
(item = ao2_iterator_next(&it)); ao2_cleanup(item)) { |
| 729 |
if (sanitize && sanitize->channel_id && sanitize->channel_id(item)) { |
| 730 |
continue; |
| 731 |
} |
| 732 |
|
| 733 |
if (ast_json_array_append(json_items, ast_json_string_create(item))) { |
| 734 |
ao2_cleanup(item); |
| 735 |
ao2_iterator_destroy(&it); |
| 736 |
ast_json_unref(json_items); |
| 737 |
|
| 738 |
return NULL; |
| 739 |
} |
| 740 |
} |
| 741 |
ao2_iterator_destroy(&it); |
| 742 |
|
| 743 |
return json_items; |
| 744 |
} |
| 745 |
|
| 746 |
static const char *capability2str(uint32_t capabilities) |
| 747 |
{ |
| 748 |
if (capabilities & AST_BRIDGE_CAPABILITY_HOLDING) { |
| 749 |
return "holding"; |
| 750 |
} else { |
| 751 |
return "mixing"; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
struct ast_json *ast_bridge_snapshot_to_json( |
| 756 |
const struct ast_bridge_snapshot *snapshot, |
| 757 |
const struct stasis_message_sanitizer *sanitize) |
| 758 |
{ |
| 759 |
struct ast_json *json_bridge; |
| 760 |
struct ast_json *json_channels; |
| 761 |
|
| 762 |
if (snapshot == NULL) { |
| 763 |
return NULL; |
| 764 |
} |
| 765 |
|
| 766 |
json_channels = container_to_json_array(snapshot->channels, sanitize); |
| 767 |
if (!json_channels) { |
| 768 |
return NULL; |
| 769 |
} |
| 770 |
|
| 771 |
json_bridge = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: o, s: o, s: s}", |
| 772 |
"id", snapshot->uniqueid, |
| 773 |
"technology", snapshot->technology, |
| 774 |
"bridge_type", capability2str(snapshot->capabilities), |
| 775 |
"bridge_class", snapshot->subclass, |
| 776 |
"creator", snapshot->creator, |
| 777 |
"name", snapshot->name, |
| 778 |
"channels", json_channels, |
| 779 |
"creationtime", ast_json_timeval(snapshot->creationtime, NULL), |
| 780 |
"video_mode", ast_bridge_video_mode_to_string(snapshot->video_mode)); |
| 781 |
if (!json_bridge) { |
| 782 |
return NULL; |
| 783 |
} |
| 784 |
|
| 785 |
if (snapshot->video_mode != AST_BRIDGE_VIDEO_MODE_NONE |
| 786 |
&& !ast_strlen_zero(snapshot->video_source_id)) { |
| 787 |
ast_json_object_set(json_bridge, "video_source_id", |
| 788 |
ast_json_string_create(snapshot->video_source_id)); |
| 789 |
} |
| 790 |
|
| 791 |
if (snapshot->bridgevars && !AST_LIST_EMPTY(snapshot->bridgevars)) { |
| 792 |
ast_json_object_set(json_bridge, "bridgevars", |
| 793 |
ast_json_channel_vars(snapshot->bridgevars)); |
| 794 |
} |
| 795 |
|
| 796 |
return json_bridge; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
static int bridge_channel_snapshot_pair_init(struct ast_channel *channel, struct ast_bridge *bridge, |
| 809 |
struct ast_bridge_channel_snapshot_pair *snapshot_pair) |
| 810 |
{ |
| 811 |
if (bridge) { |
| 812 |
ast_bridge_lock(bridge); |
| 813 |
snapshot_pair->bridge_snapshot = ast_bridge_snapshot_create(bridge); |
| 814 |
ast_bridge_unlock(bridge); |
| 815 |
if (!snapshot_pair->bridge_snapshot) { |
| 816 |
return -1; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
snapshot_pair->channel_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(channel)); |
| 821 |
if (!snapshot_pair->channel_snapshot) { |
| 822 |
return -1; |
| 823 |
} |
| 824 |
|
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
static void bridge_channel_snapshot_pair_cleanup(struct ast_bridge_channel_snapshot_pair *pair) |
| 835 |
{ |
| 836 |
ao2_cleanup(pair->bridge_snapshot); |
| 837 |
ao2_cleanup(pair->channel_snapshot); |
| 838 |
} |
| 839 |
|
| 840 |
static const char *result_strs[] = { |
| 841 |
[AST_BRIDGE_TRANSFER_FAIL] = "Fail", |
| 842 |
[AST_BRIDGE_TRANSFER_INVALID] = "Invalid", |
| 843 |
[AST_BRIDGE_TRANSFER_NOT_PERMITTED] = "Not Permitted", |
| 844 |
[AST_BRIDGE_TRANSFER_SUCCESS] = "Success", |
| 845 |
}; |
| 846 |
|
| 847 |
static struct ast_json *blind_transfer_to_json(struct stasis_message *msg, |
| 848 |
const struct stasis_message_sanitizer *sanitize) |
| 849 |
{ |
| 850 |
struct ast_blind_transfer_message *transfer_msg = stasis_message_data(msg); |
| 851 |
struct ast_json *json_transferer; |
| 852 |
struct ast_json *json_transferee = NULL; |
| 853 |
struct ast_json *out; |
| 854 |
struct ast_json *json_replace = NULL; |
| 855 |
const struct timeval *tv = stasis_message_timestamp(msg); |
| 856 |
|
| 857 |
json_transferer = ast_channel_snapshot_to_json(transfer_msg->transferer, sanitize); |
| 858 |
if (!json_transferer) { |
| 859 |
return NULL; |
| 860 |
} |
| 861 |
|
| 862 |
if (transfer_msg->transferee) { |
| 863 |
json_transferee = ast_channel_snapshot_to_json(transfer_msg->transferee, sanitize); |
| 864 |
if (!json_transferee) { |
| 865 |
ast_json_unref(json_transferer); |
| 866 |
return NULL; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
if (transfer_msg->replace_channel) { |
| 871 |
json_replace = ast_channel_snapshot_to_json(transfer_msg->replace_channel, sanitize); |
| 872 |
if (!json_replace) { |
| 873 |
ast_json_unref(json_transferee); |
| 874 |
ast_json_unref(json_transferer); |
| 875 |
return NULL; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
out = ast_json_pack("{s: s, s: o, s: o, s: s, s: s, s: s, s: o}", |
| 880 |
"type", "BridgeBlindTransfer", |
| 881 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 882 |
"channel", json_transferer, |
| 883 |
"exten", transfer_msg->exten, |
| 884 |
"context", transfer_msg->context, |
| 885 |
"result", result_strs[transfer_msg->result], |
| 886 |
"is_external", ast_json_boolean(transfer_msg->is_external)); |
| 887 |
|
| 888 |
if (!out) { |
| 889 |
ast_json_unref(json_transferee); |
| 890 |
ast_json_unref(json_replace); |
| 891 |
return NULL; |
| 892 |
} |
| 893 |
|
| 894 |
if (json_transferee && ast_json_object_set(out, "transferee", json_transferee)) { |
| 895 |
ast_json_unref(out); |
| 896 |
ast_json_unref(json_replace); |
| 897 |
return NULL; |
| 898 |
} |
| 899 |
|
| 900 |
if (json_replace && ast_json_object_set(out, "replace_channel", json_replace)) { |
| 901 |
ast_json_unref(out); |
| 902 |
return NULL; |
| 903 |
} |
| 904 |
|
| 905 |
if (transfer_msg->bridge) { |
| 906 |
struct ast_json *json_bridge = ast_bridge_snapshot_to_json( |
| 907 |
transfer_msg->bridge, sanitize); |
| 908 |
|
| 909 |
if (!json_bridge || ast_json_object_set(out, "bridge", json_bridge)) { |
| 910 |
ast_json_unref(out); |
| 911 |
return NULL; |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
return out; |
| 916 |
} |
| 917 |
|
| 918 |
static struct ast_manager_event_blob *blind_transfer_to_ami(struct stasis_message *msg) |
| 919 |
{ |
| 920 |
RAII_VAR(struct ast_str *, transferer_state, NULL, ast_free_ptr); |
| 921 |
RAII_VAR(struct ast_str *, bridge_state, NULL, ast_free_ptr); |
| 922 |
RAII_VAR(struct ast_str *, transferee_state, NULL, ast_free_ptr); |
| 923 |
struct ast_blind_transfer_message *transfer_msg = stasis_message_data(msg); |
| 924 |
|
| 925 |
if (!transfer_msg) { |
| 926 |
return NULL; |
| 927 |
} |
| 928 |
|
| 929 |
transferer_state = ast_manager_build_channel_state_string_prefix( |
| 930 |
transfer_msg->transferer, "Transferer"); |
| 931 |
if (!transferer_state) { |
| 932 |
return NULL; |
| 933 |
} |
| 934 |
|
| 935 |
if (transfer_msg->bridge) { |
| 936 |
bridge_state = ast_manager_build_bridge_state_string(transfer_msg->bridge); |
| 937 |
if (!bridge_state) { |
| 938 |
return NULL; |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
if (transfer_msg->transferee) { |
| 943 |
transferee_state = ast_manager_build_channel_state_string_prefix( |
| 944 |
transfer_msg->transferee, "Transferee"); |
| 945 |
if (!transferee_state) { |
| 946 |
return NULL; |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
return ast_manager_event_blob_create(EVENT_FLAG_CALL, "BlindTransfer", |
| 951 |
"Result: %s\r\n" |
| 952 |
"%s" |
| 953 |
"%s" |
| 954 |
"%s" |
| 955 |
"IsExternal: %s\r\n" |
| 956 |
"Context: %s\r\n" |
| 957 |
"Extension: %s\r\n", |
| 958 |
result_strs[transfer_msg->result], |
| 959 |
ast_str_buffer(transferer_state), |
| 960 |
transferee_state ? ast_str_buffer(transferee_state) : "", |
| 961 |
bridge_state ? ast_str_buffer(bridge_state) : "", |
| 962 |
transfer_msg->is_external ? "Yes" : "No", |
| 963 |
transfer_msg->context, |
| 964 |
transfer_msg->exten); |
| 965 |
} |
| 966 |
|
| 967 |
static void blind_transfer_dtor(void *obj) |
| 968 |
{ |
| 969 |
struct ast_blind_transfer_message *msg = obj; |
| 970 |
|
| 971 |
ao2_cleanup(msg->transferer); |
| 972 |
ao2_cleanup(msg->bridge); |
| 973 |
ao2_cleanup(msg->transferee); |
| 974 |
ao2_cleanup(msg->replace_channel); |
| 975 |
} |
| 976 |
|
| 977 |
struct ast_blind_transfer_message *ast_blind_transfer_message_create(int is_external, |
| 978 |
struct ast_channel *transferer, const char *exten, const char *context) |
| 979 |
{ |
| 980 |
struct ast_blind_transfer_message *msg; |
| 981 |
|
| 982 |
msg = ao2_alloc(sizeof(*msg), blind_transfer_dtor); |
| 983 |
if (!msg) { |
| 984 |
return NULL; |
| 985 |
} |
| 986 |
|
| 987 |
msg->transferer = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferer)); |
| 988 |
if (!msg->transferer) { |
| 989 |
ao2_cleanup(msg); |
| 990 |
return NULL; |
| 991 |
} |
| 992 |
|
| 993 |
msg->is_external = is_external; |
| 994 |
ast_copy_string(msg->context, context, sizeof(msg->context)); |
| 995 |
ast_copy_string(msg->exten, exten, sizeof(msg->exten)); |
| 996 |
|
| 997 |
return msg; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
void ast_bridge_publish_blind_transfer(struct ast_blind_transfer_message *transfer_message) |
| 1002 |
{ |
| 1003 |
struct stasis_message *stasis; |
| 1004 |
|
| 1005 |
stasis = stasis_message_create(ast_blind_transfer_type(), transfer_message); |
| 1006 |
if (!stasis) { |
| 1007 |
return; |
| 1008 |
} |
| 1009 |
|
| 1010 |
stasis_publish(ast_bridge_topic_all(), stasis); |
| 1011 |
|
| 1012 |
ao2_cleanup(stasis); |
| 1013 |
} |
| 1014 |
|
| 1015 |
static struct ast_json *attended_transfer_to_json(struct stasis_message *msg, |
| 1016 |
const struct stasis_message_sanitizer *sanitize) |
| 1017 |
{ |
| 1018 |
struct ast_attended_transfer_message *transfer_msg = stasis_message_data(msg); |
| 1019 |
RAII_VAR(struct ast_json *, out, NULL, ast_json_unref); |
| 1020 |
struct ast_json *json_transferer1, *json_transferer2, *json_bridge, *json_channel; |
| 1021 |
struct ast_json *json_transferee = NULL, *json_target = NULL; |
| 1022 |
const struct timeval *tv = stasis_message_timestamp(msg); |
| 1023 |
int res = 0; |
| 1024 |
|
| 1025 |
json_transferer1 = ast_channel_snapshot_to_json(transfer_msg->to_transferee.channel_snapshot, sanitize); |
| 1026 |
if (!json_transferer1) { |
| 1027 |
return NULL; |
| 1028 |
} |
| 1029 |
|
| 1030 |
json_transferer2 = ast_channel_snapshot_to_json(transfer_msg->to_transfer_target.channel_snapshot, sanitize); |
| 1031 |
if (!json_transferer2) { |
| 1032 |
ast_json_unref(json_transferer1); |
| 1033 |
return NULL; |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (transfer_msg->transferee) { |
| 1037 |
json_transferee = ast_channel_snapshot_to_json(transfer_msg->transferee, sanitize); |
| 1038 |
if (!json_transferee) { |
| 1039 |
ast_json_unref(json_transferer2); |
| 1040 |
ast_json_unref(json_transferer1); |
| 1041 |
return NULL; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (transfer_msg->target) { |
| 1046 |
json_target = ast_channel_snapshot_to_json(transfer_msg->target, sanitize); |
| 1047 |
if (!json_target) { |
| 1048 |
ast_json_unref(json_transferee); |
| 1049 |
ast_json_unref(json_transferer2); |
| 1050 |
ast_json_unref(json_transferer1); |
| 1051 |
return NULL; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
out = ast_json_pack("{s: s, s: o, s: o, s: o, s: s, s: o}", |
| 1056 |
"type", "BridgeAttendedTransfer", |
| 1057 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 1058 |
"transferer_first_leg", json_transferer1, |
| 1059 |
"transferer_second_leg", json_transferer2, |
| 1060 |
"result", result_strs[transfer_msg->result], |
| 1061 |
"is_external", ast_json_boolean(transfer_msg->is_external)); |
| 1062 |
if (!out) { |
| 1063 |
ast_json_unref(json_target); |
| 1064 |
ast_json_unref(json_transferee); |
| 1065 |
return NULL; |
| 1066 |
} |
| 1067 |
if (json_transferee && ast_json_object_set(out, "transferee", json_transferee)) { |
| 1068 |
ast_json_unref(json_target); |
| 1069 |
return NULL; |
| 1070 |
} |
| 1071 |
if (json_target && ast_json_object_set(out, "transfer_target", json_target)) { |
| 1072 |
return NULL; |
| 1073 |
} |
| 1074 |
|
| 1075 |
if (transfer_msg->to_transferee.bridge_snapshot) { |
| 1076 |
json_bridge = ast_bridge_snapshot_to_json(transfer_msg->to_transferee.bridge_snapshot, sanitize); |
| 1077 |
|
| 1078 |
if (!json_bridge) { |
| 1079 |
return NULL; |
| 1080 |
} |
| 1081 |
|
| 1082 |
res |= ast_json_object_set(out, "transferer_first_leg_bridge", json_bridge); |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (transfer_msg->to_transfer_target.bridge_snapshot) { |
| 1086 |
json_bridge = ast_bridge_snapshot_to_json(transfer_msg->to_transfer_target.bridge_snapshot, sanitize); |
| 1087 |
|
| 1088 |
if (!json_bridge) { |
| 1089 |
return NULL; |
| 1090 |
} |
| 1091 |
|
| 1092 |
res |= ast_json_object_set(out, "transferer_second_leg_bridge", json_bridge); |
| 1093 |
} |
| 1094 |
|
| 1095 |
switch (transfer_msg->dest_type) { |
| 1096 |
case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE: |
| 1097 |
res |= ast_json_object_set(out, "destination_type", ast_json_string_create("bridge")); |
| 1098 |
res |= ast_json_object_set(out, "destination_bridge", ast_json_string_create(transfer_msg->dest.bridge)); |
| 1099 |
break; |
| 1100 |
case AST_ATTENDED_TRANSFER_DEST_LOCAL_APP: |
| 1101 |
res |= ast_json_object_set(out, "replace_channel", ast_channel_snapshot_to_json(transfer_msg->replace_channel, sanitize)); |
| 1102 |
|
| 1103 |
case AST_ATTENDED_TRANSFER_DEST_APP: |
| 1104 |
res |= ast_json_object_set(out, "destination_type", ast_json_string_create("application")); |
| 1105 |
res |= ast_json_object_set(out, "destination_application", ast_json_string_create(transfer_msg->dest.app)); |
| 1106 |
break; |
| 1107 |
case AST_ATTENDED_TRANSFER_DEST_LINK: |
| 1108 |
res |= ast_json_object_set(out, "destination_type", ast_json_string_create("link")); |
| 1109 |
|
| 1110 |
json_channel = ast_channel_snapshot_to_json(transfer_msg->dest.links[0], sanitize); |
| 1111 |
if (!json_channel) { |
| 1112 |
return NULL; |
| 1113 |
} |
| 1114 |
res |= ast_json_object_set(out, "destination_link_first_leg", json_channel); |
| 1115 |
|
| 1116 |
json_channel = ast_channel_snapshot_to_json(transfer_msg->dest.links[1], sanitize); |
| 1117 |
if (!json_channel) { |
| 1118 |
return NULL; |
| 1119 |
} |
| 1120 |
res |= ast_json_object_set(out, "destination_link_second_leg", json_channel); |
| 1121 |
|
| 1122 |
break; |
| 1123 |
case AST_ATTENDED_TRANSFER_DEST_THREEWAY: |
| 1124 |
res |= ast_json_object_set(out, "destination_type", ast_json_string_create("threeway")); |
| 1125 |
|
| 1126 |
json_channel = ast_channel_snapshot_to_json(transfer_msg->dest.threeway.channel_snapshot, sanitize); |
| 1127 |
if (!json_channel) { |
| 1128 |
return NULL; |
| 1129 |
} |
| 1130 |
res |= ast_json_object_set(out, "destination_threeway_channel", json_channel); |
| 1131 |
|
| 1132 |
json_bridge = ast_bridge_snapshot_to_json(transfer_msg->dest.threeway.bridge_snapshot, sanitize); |
| 1133 |
if (!json_bridge) { |
| 1134 |
return NULL; |
| 1135 |
} |
| 1136 |
res |= ast_json_object_set(out, "destination_threeway_bridge", json_bridge); |
| 1137 |
|
| 1138 |
break; |
| 1139 |
case AST_ATTENDED_TRANSFER_DEST_FAIL: |
| 1140 |
res |= ast_json_object_set(out, "destination_type", ast_json_string_create("fail")); |
| 1141 |
break; |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (res) { |
| 1145 |
return NULL; |
| 1146 |
} |
| 1147 |
|
| 1148 |
return ast_json_ref(out); |
| 1149 |
} |
| 1150 |
|
| 1151 |
static struct ast_manager_event_blob *attended_transfer_to_ami(struct stasis_message *msg) |
| 1152 |
{ |
| 1153 |
RAII_VAR(struct ast_str *, variable_data, ast_str_create(64), ast_free_ptr); |
| 1154 |
RAII_VAR(struct ast_str *, transferer1_state, NULL, ast_free_ptr); |
| 1155 |
RAII_VAR(struct ast_str *, bridge1_state, NULL, ast_free_ptr); |
| 1156 |
RAII_VAR(struct ast_str *, transferer2_state, NULL, ast_free_ptr); |
| 1157 |
RAII_VAR(struct ast_str *, bridge2_state, NULL, ast_free_ptr); |
| 1158 |
RAII_VAR(struct ast_str *, local1_state, NULL, ast_free_ptr); |
| 1159 |
RAII_VAR(struct ast_str *, local2_state, NULL, ast_free_ptr); |
| 1160 |
RAII_VAR(struct ast_str *, transferee_state, NULL, ast_free_ptr); |
| 1161 |
RAII_VAR(struct ast_str *, target_state, NULL, ast_free_ptr); |
| 1162 |
struct ast_attended_transfer_message *transfer_msg = stasis_message_data(msg); |
| 1163 |
|
| 1164 |
if (!variable_data) { |
| 1165 |
return NULL; |
| 1166 |
} |
| 1167 |
|
| 1168 |
transferer1_state = ast_manager_build_channel_state_string_prefix(transfer_msg->to_transferee.channel_snapshot, "OrigTransferer"); |
| 1169 |
transferer2_state = ast_manager_build_channel_state_string_prefix(transfer_msg->to_transfer_target.channel_snapshot, "SecondTransferer"); |
| 1170 |
if (!transferer1_state || !transferer2_state) { |
| 1171 |
return NULL; |
| 1172 |
} |
| 1173 |
if (transfer_msg->transferee) { |
| 1174 |
transferee_state = ast_manager_build_channel_state_string_prefix(transfer_msg->transferee, "Transferee"); |
| 1175 |
if (!transferee_state) { |
| 1176 |
return NULL; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
if (transfer_msg->target) { |
| 1181 |
target_state = ast_manager_build_channel_state_string_prefix(transfer_msg->target, "TransferTarget"); |
| 1182 |
if (!target_state) { |
| 1183 |
return NULL; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (transfer_msg->to_transferee.bridge_snapshot) { |
| 1188 |
bridge1_state = ast_manager_build_bridge_state_string_prefix( |
| 1189 |
transfer_msg->to_transferee.bridge_snapshot, "Orig"); |
| 1190 |
if (!bridge1_state) { |
| 1191 |
return NULL; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (transfer_msg->to_transfer_target.bridge_snapshot) { |
| 1196 |
bridge2_state = ast_manager_build_bridge_state_string_prefix( |
| 1197 |
transfer_msg->to_transfer_target.bridge_snapshot, "Second"); |
| 1198 |
if (!bridge2_state) { |
| 1199 |
return NULL; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
switch (transfer_msg->dest_type) { |
| 1204 |
case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE: |
| 1205 |
ast_str_append(&variable_data, 0, "DestType: Bridge\r\n"); |
| 1206 |
ast_str_append(&variable_data, 0, "DestBridgeUniqueid: %s\r\n", transfer_msg->dest.bridge); |
| 1207 |
break; |
| 1208 |
case AST_ATTENDED_TRANSFER_DEST_APP: |
| 1209 |
case AST_ATTENDED_TRANSFER_DEST_LOCAL_APP: |
| 1210 |
ast_str_append(&variable_data, 0, "DestType: App\r\n"); |
| 1211 |
ast_str_append(&variable_data, 0, "DestApp: %s\r\n", transfer_msg->dest.app); |
| 1212 |
break; |
| 1213 |
case AST_ATTENDED_TRANSFER_DEST_LINK: |
| 1214 |
local1_state = ast_manager_build_channel_state_string_prefix(transfer_msg->dest.links[0], "LocalOne"); |
| 1215 |
local2_state = ast_manager_build_channel_state_string_prefix(transfer_msg->dest.links[1], "LocalTwo"); |
| 1216 |
if (!local1_state || !local2_state) { |
| 1217 |
return NULL; |
| 1218 |
} |
| 1219 |
ast_str_append(&variable_data, 0, "DestType: Link\r\n"); |
| 1220 |
ast_str_append(&variable_data, 0, "%s", ast_str_buffer(local1_state)); |
| 1221 |
ast_str_append(&variable_data, 0, "%s", ast_str_buffer(local2_state)); |
| 1222 |
break; |
| 1223 |
case AST_ATTENDED_TRANSFER_DEST_THREEWAY: |
| 1224 |
ast_str_append(&variable_data, 0, "DestType: Threeway\r\n"); |
| 1225 |
ast_str_append(&variable_data, 0, "DestBridgeUniqueid: %s\r\n", transfer_msg->dest.threeway.bridge_snapshot->uniqueid); |
| 1226 |
ast_str_append(&variable_data, 0, "DestTransfererChannel: %s\r\n", transfer_msg->dest.threeway.channel_snapshot->base->name); |
| 1227 |
break; |
| 1228 |
case AST_ATTENDED_TRANSFER_DEST_FAIL: |
| 1229 |
ast_str_append(&variable_data, 0, "DestType: Fail\r\n"); |
| 1230 |
break; |
| 1231 |
} |
| 1232 |
|
| 1233 |
return ast_manager_event_blob_create(EVENT_FLAG_CALL, "AttendedTransfer", |
| 1234 |
"Result: %s\r\n" |
| 1235 |
"%s" |
| 1236 |
"%s" |
| 1237 |
"%s" |
| 1238 |
"%s" |
| 1239 |
"%s" |
| 1240 |
"%s" |
| 1241 |
"IsExternal: %s\r\n" |
| 1242 |
"%s", |
| 1243 |
result_strs[transfer_msg->result], |
| 1244 |
ast_str_buffer(transferer1_state), |
| 1245 |
bridge1_state ? ast_str_buffer(bridge1_state) : "", |
| 1246 |
ast_str_buffer(transferer2_state), |
| 1247 |
bridge2_state ? ast_str_buffer(bridge2_state) : "", |
| 1248 |
transferee_state ? ast_str_buffer(transferee_state) : "", |
| 1249 |
target_state ? ast_str_buffer(target_state) : "", |
| 1250 |
transfer_msg->is_external ? "Yes" : "No", |
| 1251 |
ast_str_buffer(variable_data)); |
| 1252 |
} |
| 1253 |
|
| 1254 |
static void attended_transfer_dtor(void *obj) |
| 1255 |
{ |
| 1256 |
struct ast_attended_transfer_message *msg = obj; |
| 1257 |
int i; |
| 1258 |
|
| 1259 |
bridge_channel_snapshot_pair_cleanup(&msg->to_transferee); |
| 1260 |
bridge_channel_snapshot_pair_cleanup(&msg->to_transfer_target); |
| 1261 |
ao2_cleanup(msg->replace_channel); |
| 1262 |
ao2_cleanup(msg->transferee); |
| 1263 |
ao2_cleanup(msg->target); |
| 1264 |
|
| 1265 |
if (msg->dest_type != AST_ATTENDED_TRANSFER_DEST_LINK) { |
| 1266 |
return; |
| 1267 |
} |
| 1268 |
|
| 1269 |
for (i = 0; i < ARRAY_LEN(msg->dest.links); ++i) { |
| 1270 |
ao2_cleanup(msg->dest.links[i]); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
struct ast_attended_transfer_message *ast_attended_transfer_message_create(int is_external, |
| 1275 |
struct ast_channel *to_transferee, struct ast_bridge *transferee_bridge, |
| 1276 |
struct ast_channel *to_transfer_target, struct ast_bridge *target_bridge, |
| 1277 |
struct ast_channel *transferee, struct ast_channel *transfer_target) |
| 1278 |
{ |
| 1279 |
struct ast_attended_transfer_message *transfer_msg; |
| 1280 |
|
| 1281 |
transfer_msg = ao2_alloc(sizeof(*transfer_msg), attended_transfer_dtor); |
| 1282 |
if (!transfer_msg) { |
| 1283 |
return NULL; |
| 1284 |
} |
| 1285 |
|
| 1286 |
if (bridge_channel_snapshot_pair_init(to_transferee, transferee_bridge, &transfer_msg->to_transferee) || |
| 1287 |
bridge_channel_snapshot_pair_init(to_transfer_target, target_bridge, &transfer_msg->to_transfer_target)) { |
| 1288 |
ao2_cleanup(transfer_msg); |
| 1289 |
return NULL; |
| 1290 |
} |
| 1291 |
|
| 1292 |
if (transferee) { |
| 1293 |
transfer_msg->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee)); |
| 1294 |
if (!transfer_msg->transferee) { |
| 1295 |
ao2_cleanup(transfer_msg); |
| 1296 |
return NULL; |
| 1297 |
} |
| 1298 |
} else if (transferee_bridge) { |
| 1299 |
transferee = ast_bridge_peer(transferee_bridge, to_transferee); |
| 1300 |
if (transferee) { |
| 1301 |
transfer_msg->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee)); |
| 1302 |
ao2_cleanup(transferee); |
| 1303 |
if (!transfer_msg->transferee) { |
| 1304 |
ao2_cleanup(transfer_msg); |
| 1305 |
return NULL; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
if (transfer_target) { |
| 1311 |
transfer_msg->target = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transfer_target)); |
| 1312 |
if (!transfer_msg->target) { |
| 1313 |
ao2_cleanup(transfer_msg); |
| 1314 |
return NULL; |
| 1315 |
} |
| 1316 |
} else if (target_bridge) { |
| 1317 |
transfer_target = ast_bridge_peer(target_bridge, to_transfer_target); |
| 1318 |
if (transfer_target) { |
| 1319 |
transfer_msg->target = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transfer_target)); |
| 1320 |
ao2_cleanup(transfer_target); |
| 1321 |
if (!transfer_msg->target) { |
| 1322 |
ao2_cleanup(transfer_msg); |
| 1323 |
return NULL; |
| 1324 |
} |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
return transfer_msg; |
| 1329 |
} |
| 1330 |
|
| 1331 |
int ast_attended_transfer_message_add_merge(struct ast_attended_transfer_message *transfer_msg, |
| 1332 |
struct ast_bridge *final_bridge) |
| 1333 |
{ |
| 1334 |
transfer_msg->dest_type = AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE; |
| 1335 |
ast_copy_string(transfer_msg->dest.bridge, final_bridge->uniqueid, |
| 1336 |
sizeof(transfer_msg->dest.bridge)); |
| 1337 |
|
| 1338 |
return 0; |
| 1339 |
} |
| 1340 |
|
| 1341 |
int ast_attended_transfer_message_add_threeway(struct ast_attended_transfer_message *transfer_msg, |
| 1342 |
struct ast_channel *survivor_channel, struct ast_bridge *survivor_bridge) |
| 1343 |
{ |
| 1344 |
transfer_msg->dest_type = AST_ATTENDED_TRANSFER_DEST_THREEWAY; |
| 1345 |
|
| 1346 |
if (!strcmp(ast_channel_uniqueid(survivor_channel), transfer_msg->to_transferee.channel_snapshot->base->uniqueid)) { |
| 1347 |
transfer_msg->dest.threeway.channel_snapshot = transfer_msg->to_transferee.channel_snapshot; |
| 1348 |
} else { |
| 1349 |
transfer_msg->dest.threeway.channel_snapshot = transfer_msg->to_transfer_target.channel_snapshot; |
| 1350 |
} |
| 1351 |
|
| 1352 |
if (!strcmp(survivor_bridge->uniqueid, transfer_msg->to_transferee.bridge_snapshot->uniqueid)) { |
| 1353 |
transfer_msg->dest.threeway.bridge_snapshot = transfer_msg->to_transferee.bridge_snapshot; |
| 1354 |
} else { |
| 1355 |
transfer_msg->dest.threeway.bridge_snapshot = transfer_msg->to_transfer_target.bridge_snapshot; |
| 1356 |
} |
| 1357 |
|
| 1358 |
return 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
int ast_attended_transfer_message_add_app(struct ast_attended_transfer_message *transfer_msg, |
| 1362 |
const char *app, struct ast_channel *replace_channel) |
| 1363 |
{ |
| 1364 |
transfer_msg->dest_type = replace_channel ? AST_ATTENDED_TRANSFER_DEST_LOCAL_APP : AST_ATTENDED_TRANSFER_DEST_APP; |
| 1365 |
|
| 1366 |
if (replace_channel) { |
| 1367 |
transfer_msg->replace_channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(replace_channel)); |
| 1368 |
if (!transfer_msg->replace_channel) { |
| 1369 |
return -1; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
ast_copy_string(transfer_msg->dest.app, app, sizeof(transfer_msg->dest.app)); |
| 1374 |
|
| 1375 |
return 0; |
| 1376 |
} |
| 1377 |
|
| 1378 |
int ast_attended_transfer_message_add_link(struct ast_attended_transfer_message *transfer_msg, |
| 1379 |
struct ast_channel *locals[2]) |
| 1380 |
{ |
| 1381 |
int i; |
| 1382 |
|
| 1383 |
transfer_msg->dest_type = AST_ATTENDED_TRANSFER_DEST_LINK; |
| 1384 |
for (i = 0; i < 2; ++i) { |
| 1385 |
transfer_msg->dest.links[i] = ast_channel_snapshot_get_latest(ast_channel_uniqueid(locals[i])); |
| 1386 |
if (!transfer_msg->dest.links[i]) { |
| 1387 |
return -1; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
return 0; |
| 1392 |
} |
| 1393 |
|
| 1394 |
void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg) |
| 1395 |
{ |
| 1396 |
struct stasis_message *msg; |
| 1397 |
|
| 1398 |
msg = stasis_message_create(ast_attended_transfer_type(), transfer_msg); |
| 1399 |
if (!msg) { |
| 1400 |
return; |
| 1401 |
} |
| 1402 |
|
| 1403 |
stasis_publish(ast_bridge_topic_all(), msg); |
| 1404 |
ao2_ref(msg, -1); |
| 1405 |
} |
| 1406 |
|
| 1407 |
struct ast_bridge_snapshot *ast_bridge_get_snapshot_by_uniqueid(const char *uniqueid) |
| 1408 |
{ |
| 1409 |
struct ast_bridge *bridge; |
| 1410 |
struct ast_bridge_snapshot *snapshot; |
| 1411 |
|
| 1412 |
ast_assert(!ast_strlen_zero(uniqueid)); |
| 1413 |
|
| 1414 |
bridge = ast_bridge_find_by_id(uniqueid); |
| 1415 |
if (!bridge) { |
| 1416 |
return NULL; |
| 1417 |
} |
| 1418 |
ast_bridge_lock(bridge); |
| 1419 |
snapshot = ao2_bump(bridge->current_snapshot); |
| 1420 |
ast_bridge_unlock(bridge); |
| 1421 |
ao2_ref(bridge, -1); |
| 1422 |
|
| 1423 |
return snapshot; |
| 1424 |
} |
| 1425 |
|
| 1426 |
struct ast_bridge_snapshot *ast_bridge_get_snapshot(struct ast_bridge *bridge) |
| 1427 |
{ |
| 1428 |
struct ast_bridge_snapshot *snapshot; |
| 1429 |
|
| 1430 |
if (!bridge) { |
| 1431 |
return NULL; |
| 1432 |
} |
| 1433 |
ast_bridge_lock(bridge); |
| 1434 |
snapshot = ao2_bump(bridge->current_snapshot); |
| 1435 |
ast_bridge_unlock(bridge); |
| 1436 |
|
| 1437 |
return snapshot; |
| 1438 |
} |
| 1439 |
|
| 1440 |
static void stasis_bridging_cleanup(void) |
| 1441 |
{ |
| 1442 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_bridge_snapshot_type); |
| 1443 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_bridge_merge_message_type); |
| 1444 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_entered_bridge_type); |
| 1445 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_left_bridge_type); |
| 1446 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_blind_transfer_type); |
| 1447 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_attended_transfer_type); |
| 1448 |
|
| 1449 |
ao2_cleanup(bridge_topic_pool); |
| 1450 |
bridge_topic_pool = NULL; |
| 1451 |
ao2_cleanup(bridge_topic_all); |
| 1452 |
bridge_topic_all = NULL; |
| 1453 |
} |
| 1454 |
|
| 1455 |
int ast_stasis_bridging_init(void) |
| 1456 |
{ |
| 1457 |
int res = 0; |
| 1458 |
|
| 1459 |
ast_register_cleanup(stasis_bridging_cleanup); |
| 1460 |
|
| 1461 |
bridge_topic_all = stasis_topic_create("bridge:all"); |
| 1462 |
if (!bridge_topic_all) { |
| 1463 |
return -1; |
| 1464 |
} |
| 1465 |
bridge_topic_pool = stasis_topic_pool_create(bridge_topic_all); |
| 1466 |
if (!bridge_topic_pool) { |
| 1467 |
return -1; |
| 1468 |
} |
| 1469 |
|
| 1470 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_bridge_snapshot_type); |
| 1471 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_bridge_merge_message_type); |
| 1472 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_entered_bridge_type); |
| 1473 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_left_bridge_type); |
| 1474 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_blind_transfer_type); |
| 1475 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_attended_transfer_type); |
| 1476 |
|
| 1477 |
return res; |
| 1478 |
} |