Asterisk - The Open Source Telephony Project GIT-master-8f1982c
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Functions | Variables
stasis_endpoints.c File Reference

Stasis endpoint API. More...

#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_endpoints.h"
Include dependency graph for stasis_endpoints.c:

Go to the source code of this file.

Functions

struct stasis_messageast_endpoint_blob_create (struct ast_endpoint *endpoint, struct stasis_message_type *type, struct ast_json *blob)
 Creates a ast_endpoint_blob message. More...
 
void ast_endpoint_blob_publish (struct ast_endpoint *endpoint, struct stasis_message_type *type, struct ast_json *blob)
 Creates and publishes a ast_endpoint_blob message. More...
 
struct stasis_cacheast_endpoint_cache (void)
 Backend cache for ast_endpoint_topic_all_cached(). More...
 
struct stasis_cp_allast_endpoint_cache_all (void)
 
struct ast_endpoint_snapshotast_endpoint_latest_snapshot (const char *tech, const char *name)
 Retrieve the most recent snapshot for the endpoint with the given name. More...
 
struct ast_jsonast_endpoint_snapshot_to_json (const struct ast_endpoint_snapshot *snapshot, const struct stasis_message_sanitizer *sanitize)
 Build a JSON object from a ast_endpoint_snapshot. More...
 
int ast_endpoint_stasis_init (void)
 Initialization function for endpoint stasis support. More...
 
struct stasis_topicast_endpoint_topic_all (void)
 Topic for all endpoint related messages. More...
 
struct stasis_topicast_endpoint_topic_all_cached (void)
 Cached topic for all endpoint related messages. More...
 
static struct ast_manager_event_blobcontactstatus_to_ami (struct stasis_message *msg)
 
static struct ast_jsoncontactstatus_to_json (struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize)
 
static void endpoint_blob_dtor (void *obj)
 
static const char * endpoint_snapshot_get_id (struct stasis_message *message)
 Callback extract a unique identity from a snapshot message. More...
 
static void endpoints_stasis_cleanup (void)
 
static struct ast_manager_event_blobpeerstatus_to_ami (struct stasis_message *msg)
 
static struct ast_jsonpeerstatus_to_json (struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_endpoint_contact_state_type,.to_ami=contactstatus_to_ami,.to_json=contactstatus_to_json)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_endpoint_snapshot_type)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_endpoint_state_type,.to_ami=peerstatus_to_ami,.to_json=peerstatus_to_json,)
 

Variables

static struct stasis_cp_allendpoint_cache_all
 

Detailed Description

Stasis endpoint API.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file stasis_endpoints.c.

Function Documentation

◆ ast_endpoint_snapshot_to_json()

struct ast_json * ast_endpoint_snapshot_to_json ( const struct ast_endpoint_snapshot snapshot,
const struct stasis_message_sanitizer sanitize 
)

Build a JSON object from a ast_endpoint_snapshot.

Parameters
snapshotEndpoint snapshot.
sanitizeThe message sanitizer to use on the snapshot
Returns
JSON object representing endpoint snapshot.
Return values
NULLon error

Definition at line 404 of file stasis_endpoints.c.

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",
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}
const char * ast_endpoint_state_to_string(enum ast_endpoint_state state)
Returns a string representation of the given endpoint state.
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:327
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
#define NULL
Definition: resample.c:96
const ast_string_field tech
enum ast_endpoint_state state
const ast_string_field resource
Abstract JSON element (object, array, string, int, ...).
int(* channel_id)(const char *channel_id)
Callback which determines whether a channel should be sanitized from a message based on the channel's...
Definition: stasis.h:210
#define ast_assert(a)
Definition: utils.h:739

References ast_assert, ast_endpoint_state_to_string(), ast_json_array_append(), ast_json_integer_create(), ast_json_object_get(), ast_json_object_set(), ast_json_pack(), ast_json_string_create(), ast_json_unref(), stasis_message_sanitizer::channel_id, ast_endpoint_snapshot::channel_ids, ast_endpoint_snapshot::max_channels, NULL, ast_endpoint_snapshot::num_channels, ast_endpoint_snapshot::resource, ast_endpoint_snapshot::state, and ast_endpoint_snapshot::tech.

Referenced by ast_ari_endpoints_get(), ast_ari_endpoints_list(), ast_ari_endpoints_list_by_tech(), contactstatus_to_json(), message_received_handler(), multi_user_event_to_json(), peerstatus_to_json(), and simple_endpoint_event().

◆ ast_endpoint_stasis_init()

int ast_endpoint_stasis_init ( void  )

Initialization function for endpoint stasis support.

Returns
0 on success.
non-zero on error.
Since
12

Definition at line 464 of file stasis_endpoints.c.

465{
466 int res = 0;
468
471 if (!endpoint_cache_all) {
472 return -1;
473 }
474
478
479 return res;
480}
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct stasis_message_type * ast_endpoint_contact_state_type(void)
Message type for endpoint contact state changes.
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
struct stasis_message_type * ast_endpoint_state_type(void)
Message type for endpoint state changes.
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
struct stasis_cp_all * stasis_cp_all_create(const char *name, snapshot_get_id id_fn)
Create an all instance of the cache pattern.
static const char * endpoint_snapshot_get_id(struct stasis_message *message)
Callback extract a unique identity from a snapshot message.
static void endpoints_stasis_cleanup(void)
static struct stasis_cp_all * endpoint_cache_all

References ast_endpoint_contact_state_type(), ast_endpoint_snapshot_type(), ast_endpoint_state_type(), ast_register_cleanup(), endpoint_cache_all, endpoint_snapshot_get_id(), endpoints_stasis_cleanup(), stasis_cp_all_create(), and STASIS_MESSAGE_TYPE_INIT.

Referenced by asterisk_daemon().

◆ contactstatus_to_ami()

static struct ast_manager_event_blob * contactstatus_to_ami ( struct stasis_message msg)
static

Definition at line 209 of file stasis_endpoints.c.

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}
#define ast_free(a)
Definition: astmm.h:180
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
#define EVENT_FLAG_SYSTEM
Definition: manager.h:75
struct ast_manager_event_blob * ast_manager_event_blob_create(int event_flags, const char *manager_event, const char *extra_fields_fmt,...)
Construct a ast_manager_event_blob.
Definition: manager.c:10237
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
Blob of data associated with an endpoint.
struct ast_json * blob
Support for dynamic strings.
Definition: strings.h:623
int value
Definition: syslog.c:37
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ast_free, ast_json_object_get(), ast_json_string_get(), ast_manager_event_blob_create(), ast_str_append(), ast_str_buffer(), ast_str_create, ast_endpoint_blob::blob, EVENT_FLAG_SYSTEM, NULL, RAII_VAR, stasis_message_data(), and value.

◆ contactstatus_to_json()

static struct ast_json * contactstatus_to_json ( struct stasis_message msg,
const struct stasis_message_sanitizer sanitize 
)
static

Definition at line 243 of file stasis_endpoints.c.

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 /* The roundtrip time is optional. */
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}
struct ast_json * ast_json_timeval(const struct timeval tv, const char *zone)
Construct a timeval as JSON.
Definition: json.c:670
const struct timeval * stasis_message_timestamp(const struct stasis_message *msg)
Get the time when a message was created.
struct ast_json * ast_endpoint_snapshot_to_json(const struct ast_endpoint_snapshot *snapshot, const struct stasis_message_sanitizer *sanitize)
Build a JSON object from a ast_endpoint_snapshot.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
struct ast_endpoint_snapshot * snapshot

References ast_endpoint_snapshot_to_json(), ast_json_object_get(), ast_json_pack(), ast_json_string_get(), ast_json_timeval(), ast_json_unref(), ast_strlen_zero(), ast_endpoint_blob::blob, NULL, ast_endpoint_blob::snapshot, stasis_message_data(), and stasis_message_timestamp().

◆ endpoint_blob_dtor()

static void endpoint_blob_dtor ( void *  obj)
static

Definition at line 292 of file stasis_endpoints.c.

293{
294 struct ast_endpoint_blob *event = obj;
295 ao2_cleanup(event->snapshot);
296 ast_json_unref(event->blob);
297}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
Definition: astman.c:222

References ao2_cleanup, and ast_json_unref().

Referenced by ast_endpoint_blob_create().

◆ endpoint_snapshot_get_id()

static const char * endpoint_snapshot_get_id ( struct stasis_message message)
static

Callback extract a unique identity from a snapshot message.

This identity is unique to the underlying object of the snapshot, such as the UniqueId field of a channel.

Parameters
messageMessage to extract id from.
Returns
String representing the snapshot's id.
Return values
NULLif the message_type of the message isn't a handled snapshot.
Since
12

Definition at line 390 of file stasis_endpoints.c.

391{
392 struct ast_endpoint_snapshot *snapshot;
393
395 return NULL;
396 }
397
398 snapshot = stasis_message_data(message);
399
400 return snapshot->id;
401}
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
A snapshot of an endpoint's state.
const ast_string_field id

References ast_endpoint_snapshot_type(), ast_endpoint_snapshot::id, NULL, stasis_message_data(), and stasis_message_type().

Referenced by ast_endpoint_stasis_init().

◆ endpoints_stasis_cleanup()

static void endpoints_stasis_cleanup ( void  )
static

◆ peerstatus_to_ami()

static struct ast_manager_event_blob * peerstatus_to_ami ( struct stasis_message msg)
static

Definition at line 134 of file stasis_endpoints.c.

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 /* peer_status is the only *required* thing */
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
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}

References ast_free, ast_json_object_get(), ast_json_string_get(), ast_manager_event_blob_create(), ast_str_append(), ast_str_buffer(), ast_str_create, ast_endpoint_blob::blob, EVENT_FLAG_SYSTEM, NULL, RAII_VAR, ast_endpoint_snapshot::resource, ast_endpoint_blob::snapshot, stasis_message_data(), ast_endpoint_snapshot::tech, and value.

◆ peerstatus_to_json()

static struct ast_json * peerstatus_to_json ( struct stasis_message msg,
const struct stasis_message_sanitizer sanitize 
)
static

Definition at line 169 of file stasis_endpoints.c.

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 /* Copy all fields from the blob */
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}
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
int ast_json_object_update(struct ast_json *object, struct ast_json *other)
Update object with all of the fields of other.
Definition: json.c:426

References ast_endpoint_snapshot_to_json(), ast_json_object_create(), ast_json_object_update(), ast_json_pack(), ast_json_timeval(), ast_json_unref(), ast_endpoint_blob::blob, NULL, ast_endpoint_blob::snapshot, stasis_message_data(), and stasis_message_timestamp().

◆ STASIS_MESSAGE_TYPE_DEFN() [1/3]

STASIS_MESSAGE_TYPE_DEFN ( ast_endpoint_contact_state_type  ,
to_ami = contactstatus_to_ami,
to_json = contactstatus_to_json 
)

◆ STASIS_MESSAGE_TYPE_DEFN() [2/3]

STASIS_MESSAGE_TYPE_DEFN ( ast_endpoint_snapshot_type  )

◆ STASIS_MESSAGE_TYPE_DEFN() [3/3]

STASIS_MESSAGE_TYPE_DEFN ( ast_endpoint_state_type  ,
to_ami = peerstatus_to_ami,
to_json = peerstatus_to_json 
)

Variable Documentation

◆ endpoint_cache_all

struct stasis_cp_all* endpoint_cache_all
static