| 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/stasis.h" |
| 34 |
#include "asterisk/stasis_endpoints.h" |
| 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 |
static struct stasis_cp_all *endpoint_cache_all; |
| 111 |
|
| 112 |
struct stasis_cp_all *ast_endpoint_cache_all(void) |
| 113 |
{ |
| 114 |
return endpoint_cache_all; |
| 115 |
} |
| 116 |
|
| 117 |
struct stasis_cache *ast_endpoint_cache(void) |
| 118 |
{ |
| 119 |
return stasis_cp_all_cache(endpoint_cache_all); |
| 120 |
} |
| 121 |
|
| 122 |
struct stasis_topic *ast_endpoint_topic_all(void) |
| 123 |
{ |
| 124 |
return stasis_cp_all_topic(endpoint_cache_all); |
| 125 |
} |
| 126 |
|
| 127 |
struct stasis_topic *ast_endpoint_topic_all_cached(void) |
| 128 |
{ |
| 129 |
return stasis_cp_all_topic_cached(endpoint_cache_all); |
| 130 |
} |
| 131 |
|
| 132 |
STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_snapshot_type); |
| 133 |
|
| 134 |
static struct ast_manager_event_blob *peerstatus_to_ami(struct stasis_message *msg) |
| 135 |
{ |
| 136 |
struct ast_endpoint_blob *obj = stasis_message_data(msg); |
| 137 |
RAII_VAR(struct ast_str *, peerstatus_event_string, ast_str_create(64), ast_free); |
| 138 |
const char *value; |
| 139 |
|
| 140 |
|
| 141 |
if (!(value = ast_json_string_get(ast_json_object_get(obj->blob, "peer_status")))) { |
| 142 |
return NULL; |
| 143 |
} |
| 144 |
ast_str_append(&peerstatus_event_string, 0, "PeerStatus: %s\r\n", value); |
| 145 |
|
| 146 |
if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "cause")))) { |
| 147 |
ast_str_append(&peerstatus_event_string, 0, "Cause: %s\r\n", value); |
| 148 |
} |
| 149 |
if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "address")))) { |
| 150 |
ast_str_append(&peerstatus_event_string, 0, "Address: %s\r\n", value); |
| 151 |
} |
| 152 |
if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "port")))) { |
| 153 |
ast_str_append(&peerstatus_event_string, 0, "Port: %s\r\n", value); |
| 154 |
} |
| 155 |
if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "time")))) { |
| 156 |
ast_str_append(&peerstatus_event_string, 0, "Time: %s\r\n", value); |
| 157 |
} |
| 158 |
|
| 159 |
return ast_manager_event_blob_create(EVENT_FLAG_SYSTEM, "PeerStatus", |
| 160 |
"ChannelType: %s\r\n" |
| 161 |
"Peer: %s/%s\r\n" |
| 162 |
"%s", |
| 163 |
obj->snapshot->tech, |
| 164 |
obj->snapshot->tech, |
| 165 |
obj->snapshot->resource, |
| 166 |
ast_str_buffer(peerstatus_event_string)); |
| 167 |
} |
| 168 |
|
| 169 |
static struct ast_json *peerstatus_to_json(struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize) |
| 170 |
{ |
| 171 |
struct ast_endpoint_blob *obj = stasis_message_data(msg); |
| 172 |
struct ast_json *json_endpoint; |
| 173 |
struct ast_json *json_peer; |
| 174 |
struct ast_json *json_final; |
| 175 |
const struct timeval *tv = stasis_message_timestamp(msg); |
| 176 |
|
| 177 |
json_endpoint = ast_endpoint_snapshot_to_json(obj->snapshot, NULL); |
| 178 |
if (!json_endpoint) { |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
|
| 182 |
json_peer = ast_json_object_create(); |
| 183 |
if (!json_peer) { |
| 184 |
ast_json_unref(json_endpoint); |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
ast_json_object_update(json_peer, obj->blob); |
| 190 |
|
| 191 |
json_final = ast_json_pack("{s: s, s: o, s: o, s: o }", |
| 192 |
"type", "PeerStatusChange", |
| 193 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 194 |
"endpoint", json_endpoint, |
| 195 |
"peer", json_peer); |
| 196 |
if (!json_final) { |
| 197 |
ast_json_unref(json_endpoint); |
| 198 |
ast_json_unref(json_peer); |
| 199 |
} |
| 200 |
|
| 201 |
return json_final; |
| 202 |
} |
| 203 |
|
| 204 |
STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_state_type, |
| 205 |
.to_ami = peerstatus_to_ami, |
| 206 |
.to_json = peerstatus_to_json, |
| 207 |
); |
| 208 |
|
| 209 |
static struct ast_manager_event_blob *contactstatus_to_ami(struct stasis_message *msg) |
| 210 |
{ |
| 211 |
struct ast_endpoint_blob *obj = stasis_message_data(msg); |
| 212 |
RAII_VAR(struct ast_str *, contactstatus_event_string, ast_str_create(64), ast_free); |
| 213 |
const char *value; |
| 214 |
|
| 215 |
if (!(value = ast_json_string_get(ast_json_object_get(obj->blob, "uri")))) { |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
ast_str_append(&contactstatus_event_string, 0, "URI: %s\r\n", value); |
| 219 |
|
| 220 |
if (!(value = ast_json_string_get(ast_json_object_get(obj->blob, "contact_status")))) { |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
ast_str_append(&contactstatus_event_string, 0, "ContactStatus: %s\r\n", value); |
| 224 |
|
| 225 |
if (!(value = ast_json_string_get(ast_json_object_get(obj->blob, "aor")))) { |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
ast_str_append(&contactstatus_event_string, 0, "AOR: %s\r\n", value); |
| 229 |
|
| 230 |
if (!(value = ast_json_string_get(ast_json_object_get(obj->blob, "endpoint_name")))) { |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
ast_str_append(&contactstatus_event_string, 0, "EndpointName: %s\r\n", value); |
| 234 |
|
| 235 |
if ((value = ast_json_string_get(ast_json_object_get(obj->blob, "roundtrip_usec")))) { |
| 236 |
ast_str_append(&contactstatus_event_string, 0, "RoundtripUsec: %s\r\n", value); |
| 237 |
} |
| 238 |
|
| 239 |
return ast_manager_event_blob_create(EVENT_FLAG_SYSTEM, "ContactStatus", |
| 240 |
"%s", ast_str_buffer(contactstatus_event_string)); |
| 241 |
} |
| 242 |
|
| 243 |
static struct ast_json *contactstatus_to_json(struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize) |
| 244 |
{ |
| 245 |
struct ast_endpoint_blob *obj = stasis_message_data(msg); |
| 246 |
struct ast_json *json_endpoint; |
| 247 |
struct ast_json *json_final; |
| 248 |
const char *rtt; |
| 249 |
const struct timeval *tv = stasis_message_timestamp(msg); |
| 250 |
|
| 251 |
json_endpoint = ast_endpoint_snapshot_to_json(obj->snapshot, NULL); |
| 252 |
if (!json_endpoint) { |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
rtt = ast_json_string_get(ast_json_object_get(obj->blob, "roundtrip_usec")); |
| 258 |
if (!ast_strlen_zero(rtt)) { |
| 259 |
json_final = ast_json_pack("{s: s, s: o, s: o, s: { s: s, s: s, s: s, s: s } } ", |
| 260 |
"type", "ContactStatusChange", |
| 261 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 262 |
"endpoint", json_endpoint, |
| 263 |
"contact_info", |
| 264 |
"uri", ast_json_string_get(ast_json_object_get(obj->blob, "uri")), |
| 265 |
"contact_status", ast_json_string_get(ast_json_object_get(obj->blob, |
| 266 |
"contact_status")), |
| 267 |
"aor", ast_json_string_get(ast_json_object_get(obj->blob, "aor")), |
| 268 |
"roundtrip_usec", rtt); |
| 269 |
} else { |
| 270 |
json_final = ast_json_pack("{s: s, s: o, s: o, s: { s: s, s: s, s: s } } ", |
| 271 |
"type", "ContactStatusChange", |
| 272 |
"timestamp", ast_json_timeval(*tv, NULL), |
| 273 |
"endpoint", json_endpoint, |
| 274 |
"contact_info", |
| 275 |
"uri", ast_json_string_get(ast_json_object_get(obj->blob, "uri")), |
| 276 |
"contact_status", ast_json_string_get(ast_json_object_get(obj->blob, |
| 277 |
"contact_status")), |
| 278 |
"aor", ast_json_string_get(ast_json_object_get(obj->blob, "aor"))); |
| 279 |
} |
| 280 |
if (!json_final) { |
| 281 |
ast_json_unref(json_endpoint); |
| 282 |
} |
| 283 |
|
| 284 |
return json_final; |
| 285 |
} |
| 286 |
|
| 287 |
STASIS_MESSAGE_TYPE_DEFN(ast_endpoint_contact_state_type, |
| 288 |
.to_ami = contactstatus_to_ami, |
| 289 |
.to_json = contactstatus_to_json |
| 290 |
); |
| 291 |
|
| 292 |
static void endpoint_blob_dtor(void *obj) |
| 293 |
{ |
| 294 |
struct ast_endpoint_blob *event = obj; |
| 295 |
ao2_cleanup(event->snapshot); |
| 296 |
ast_json_unref(event->blob); |
| 297 |
} |
| 298 |
|
| 299 |
struct stasis_message *ast_endpoint_blob_create(struct ast_endpoint *endpoint, |
| 300 |
struct stasis_message_type *type, struct ast_json *blob) |
| 301 |
{ |
| 302 |
struct ast_endpoint_blob *obj; |
| 303 |
struct stasis_message *msg; |
| 304 |
|
| 305 |
if (!type) { |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
if (!blob) { |
| 309 |
blob = ast_json_null(); |
| 310 |
} |
| 311 |
|
| 312 |
if (!(obj = ao2_alloc(sizeof(*obj), endpoint_blob_dtor))) { |
| 313 |
return NULL; |
| 314 |
} |
| 315 |
|
| 316 |
if (endpoint) { |
| 317 |
if (!(obj->snapshot = ast_endpoint_snapshot_create(endpoint))) { |
| 318 |
ao2_ref(obj, -1); |
| 319 |
|
| 320 |
return NULL; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
obj->blob = ast_json_ref(blob); |
| 325 |
msg = stasis_message_create(type, obj); |
| 326 |
ao2_ref(obj, -1); |
| 327 |
|
| 328 |
return msg; |
| 329 |
} |
| 330 |
|
| 331 |
void ast_endpoint_blob_publish(struct ast_endpoint *endpoint, struct stasis_message_type *type, |
| 332 |
struct ast_json *blob) |
| 333 |
{ |
| 334 |
struct stasis_message *message; |
| 335 |
|
| 336 |
if (!blob) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
message = ast_endpoint_blob_create(endpoint, type, blob); |
| 341 |
if (message) { |
| 342 |
stasis_publish(ast_endpoint_topic(endpoint), message); |
| 343 |
ao2_ref(message, -1); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
struct ast_endpoint_snapshot *ast_endpoint_latest_snapshot(const char *tech, |
| 348 |
const char *name) |
| 349 |
{ |
| 350 |
char *id = NULL; |
| 351 |
struct stasis_message *msg; |
| 352 |
struct ast_endpoint_snapshot *snapshot; |
| 353 |
|
| 354 |
if (ast_strlen_zero(name)) { |
| 355 |
ast_asprintf(&id, "%s", tech); |
| 356 |
} else { |
| 357 |
ast_asprintf(&id, "%s/%s", tech, name); |
| 358 |
} |
| 359 |
if (!id) { |
| 360 |
return NULL; |
| 361 |
} |
| 362 |
ast_tech_to_upper(id); |
| 363 |
|
| 364 |
msg = stasis_cache_get(ast_endpoint_cache(), ast_endpoint_snapshot_type(), id); |
| 365 |
ast_free(id); |
| 366 |
if (!msg) { |
| 367 |
return NULL; |
| 368 |
} |
| 369 |
|
| 370 |
snapshot = stasis_message_data(msg); |
| 371 |
ast_assert(snapshot != NULL); |
| 372 |
|
| 373 |
ao2_ref(snapshot, +1); |
| 374 |
ao2_ref(msg, -1); |
| 375 |
|
| 376 |
return snapshot; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
static const char *endpoint_snapshot_get_id(struct stasis_message *message) |
| 391 |
{ |
| 392 |
struct ast_endpoint_snapshot *snapshot; |
| 393 |
|
| 394 |
if (ast_endpoint_snapshot_type() != stasis_message_type(message)) { |
| 395 |
return NULL; |
| 396 |
} |
| 397 |
|
| 398 |
snapshot = stasis_message_data(message); |
| 399 |
|
| 400 |
return snapshot->id; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
struct ast_json *ast_endpoint_snapshot_to_json( |
| 405 |
const struct ast_endpoint_snapshot *snapshot, |
| 406 |
const struct stasis_message_sanitizer *sanitize) |
| 407 |
{ |
| 408 |
struct ast_json *json; |
| 409 |
struct ast_json *channel_array; |
| 410 |
int i; |
| 411 |
|
| 412 |
json = ast_json_pack("{s: s, s: s, s: s, s: []}", |
| 413 |
"technology", snapshot->tech, |
| 414 |
"resource", snapshot->resource, |
| 415 |
"state", ast_endpoint_state_to_string(snapshot->state), |
| 416 |
"channel_ids"); |
| 417 |
|
| 418 |
if (json == NULL) { |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
|
| 422 |
if (snapshot->max_channels != -1) { |
| 423 |
int res = ast_json_object_set(json, "max_channels", |
| 424 |
ast_json_integer_create(snapshot->max_channels)); |
| 425 |
if (res != 0) { |
| 426 |
ast_json_unref(json); |
| 427 |
|
| 428 |
return NULL; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
channel_array = ast_json_object_get(json, "channel_ids"); |
| 433 |
ast_assert(channel_array != NULL); |
| 434 |
for (i = 0; i < snapshot->num_channels; ++i) { |
| 435 |
int res; |
| 436 |
|
| 437 |
if (sanitize && sanitize->channel_id |
| 438 |
&& sanitize->channel_id(snapshot->channel_ids[i])) { |
| 439 |
continue; |
| 440 |
} |
| 441 |
|
| 442 |
res = ast_json_array_append(channel_array, |
| 443 |
ast_json_string_create(snapshot->channel_ids[i])); |
| 444 |
if (res != 0) { |
| 445 |
ast_json_unref(json); |
| 446 |
|
| 447 |
return NULL; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
return json; |
| 452 |
} |
| 453 |
|
| 454 |
static void endpoints_stasis_cleanup(void) |
| 455 |
{ |
| 456 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_snapshot_type); |
| 457 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_state_type); |
| 458 |
STASIS_MESSAGE_TYPE_CLEANUP(ast_endpoint_contact_state_type); |
| 459 |
|
| 460 |
ao2_cleanup(endpoint_cache_all); |
| 461 |
endpoint_cache_all = NULL; |
| 462 |
} |
| 463 |
|
| 464 |
int ast_endpoint_stasis_init(void) |
| 465 |
{ |
| 466 |
int res = 0; |
| 467 |
ast_register_cleanup(endpoints_stasis_cleanup); |
| 468 |
|
| 469 |
endpoint_cache_all = stasis_cp_all_create("endpoint:all", |
| 470 |
endpoint_snapshot_get_id); |
| 471 |
if (!endpoint_cache_all) { |
| 472 |
return -1; |
| 473 |
} |
| 474 |
|
| 475 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_snapshot_type); |
| 476 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_state_type); |
| 477 |
res |= STASIS_MESSAGE_TYPE_INIT(ast_endpoint_contact_state_type); |
| 478 |
|
| 479 |
return res; |
| 480 |
} |