Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Functions | Variables
main/endpoints.c File Reference

Asterisk endpoint API. More...

#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/endpoints.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/stringfields.h"
#include "asterisk/_private.h"

Go to the source code of this file.

Data Structures

struct  ast_endpoint
 

Macros

#define ENDPOINT_BUCKETS   127
 
#define ENDPOINT_CHANNEL_BUCKETS   127
 
#define TECH_ENDPOINT_BUCKETS   11
 

Functions

int ast_endpoint_add_channel (struct ast_endpoint *endpoint, struct ast_channel *chan)
 Adds a channel to the given endpoint. More...
 
struct ast_endpointast_endpoint_create (const char *tech, const char *resource)
 Create an endpoint struct. More...
 
struct ast_endpointast_endpoint_find_by_id (const char *id)
 Finds the endpoint with the given tech[/resource] id. More...
 
const char * ast_endpoint_get_id (const struct ast_endpoint *endpoint)
 Gets the tech/resource id of the given endpoint. More...
 
const char * ast_endpoint_get_resource (const struct ast_endpoint *endpoint)
 Gets the resource name of the given endpoint. More...
 
enum ast_endpoint_state ast_endpoint_get_state (const struct ast_endpoint *endpoint)
 Gets the state of the given endpoint. More...
 
const char * ast_endpoint_get_tech (const struct ast_endpoint *endpoint)
 Gets the technology of the given endpoint. More...
 
int ast_endpoint_init (void)
 Endpoint support initialization. More...
 
void ast_endpoint_set_max_channels (struct ast_endpoint *endpoint, int max_channels)
 Updates the maximum number of channels an endpoint supports. More...
 
void ast_endpoint_set_state (struct ast_endpoint *endpoint, enum ast_endpoint_state state)
 Updates the state of the given endpoint. More...
 
void ast_endpoint_shutdown (struct ast_endpoint *endpoint)
 Shutsdown an ast_endpoint. More...
 
struct ast_endpoint_snapshotast_endpoint_snapshot_create (struct ast_endpoint *endpoint)
 Create a snapshot of an endpoint. More...
 
const char * ast_endpoint_state_to_string (enum ast_endpoint_state state)
 Returns a string representation of the given endpoint state. More...
 
struct stasis_topicast_endpoint_topic (struct ast_endpoint *endpoint)
 Returns the topic for a specific endpoint. More...
 
struct stasis_topicast_endpoint_topic_cached (struct ast_endpoint *endpoint)
 Returns the topic for a specific endpoint. More...
 
static struct stasis_messagecreate_endpoint_snapshot_message (struct ast_endpoint *endpoint)
 
static void endpoint_cache_clear (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 Handler for channel snapshot update.
 
static void endpoint_cleanup (void)
 
static void endpoint_dtor (void *obj)
 
static struct ast_endpointendpoint_internal_create (const char *tech, const char *resource)
 
static void endpoint_publish_snapshot (struct ast_endpoint *endpoint)
 
static void endpoint_snapshot_dtor (void *obj)
 
static void endpoint_subscription_change (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 

Variables

static struct ao2_containerendpoints
 
static struct ao2_containertech_endpoints
 

Detailed Description

Asterisk endpoint API.

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

Definition in file main/endpoints.c.

Macro Definition Documentation

#define ENDPOINT_BUCKETS   127

Buckets for endpoint hash. Keep it prime!

Definition at line 45 of file main/endpoints.c.

Referenced by ast_endpoint_init().

#define ENDPOINT_CHANNEL_BUCKETS   127

Buckets for endpoint->channel mappings. Keep it prime!

Definition at line 42 of file main/endpoints.c.

#define TECH_ENDPOINT_BUCKETS   11

Buckets for technology endpoints.

Definition at line 48 of file main/endpoints.c.

Referenced by ast_endpoint_init().

Function Documentation

int ast_endpoint_add_channel ( struct ast_endpoint endpoint,
struct ast_channel chan 
)

Adds a channel to the given endpoint.

Since
12 This updates the endpoint's statistics, as well as forwarding all of the channel's messages to the endpoint's topic.

The channel is automagically removed from the endpoint when it is disposed of.

Parameters
endpoint
chanChannel.
Return values
0on success.
Non-zeroon error.

Definition at line 164 of file main/endpoints.c.

References ast_channel_forward_endpoint(), ast_str_container_add(), ast_endpoint::channel_ids, and ast_endpoint::resource.

Referenced by __ast_channel_alloc_ap().

166 {
167  ast_assert(chan != NULL);
168  ast_assert(endpoint != NULL);
169  ast_assert(!ast_strlen_zero(endpoint->resource));
170 
171  ast_channel_forward_endpoint(chan, endpoint);
172 
173  ao2_lock(endpoint);
174  ast_str_container_add(endpoint->channel_ids, ast_channel_uniqueid(chan));
175  ao2_unlock(endpoint);
176 
177  endpoint_publish_snapshot(endpoint);
178 
179  return 0;
180 }
struct ao2_container * channel_ids
int ast_channel_forward_endpoint(struct ast_channel *chan, struct ast_endpoint *endpoint)
Forward channel stasis messages to the given endpoint.
const ast_string_field resource
int ast_str_container_add(struct ao2_container *str_container, const char *add)
Adds a string to a string container allocated by ast_str_container_alloc.
Definition: strings.c:205
struct ast_endpoint* ast_endpoint_create ( const char *  tech,
const char *  resource 
)

Create an endpoint struct.

The endpoint is created with a state of UNKNOWN and max_channels of -1 (unlimited). While ast_endpoint is AO2 managed, you have to shut it down with ast_endpoint_shutdown() to clean up references from subscriptions.

Parameters
techTechnology for this endpoint.
resourceName of this endpoint.
Returns
Newly created endpoint.
Return values
NULLon error.
Since
12

Definition at line 319 of file main/endpoints.c.

Referenced by build_peer(), and xmpp_client_alloc().

320 {
321  if (ast_strlen_zero(tech)) {
322  ast_log(LOG_ERROR, "Endpoint tech cannot be empty\n");
323  return NULL;
324  }
325 
326  if (ast_strlen_zero(resource)) {
327  ast_log(LOG_ERROR, "Endpoint resource cannot be empty\n");
328  return NULL;
329  }
330 
331  return endpoint_internal_create(tech, resource);
332 }
struct ast_endpoint* ast_endpoint_find_by_id ( const char *  id)

Finds the endpoint with the given tech[/resource] id.

Endpoints are refcounted, so ao2_cleanup() when you're done.

Note
The resource portion of an ID is optional. If not provided, an aggregate endpoint for the entire technology is returned. These endpoints must not be modified, but can be subscribed to in order to receive updates for all endpoints of a given technology.
Parameters
idTech[/resource] id to look for.
Returns
Associated endpoint.
Return values
NULLif not found.
Since
12

Definition at line 82 of file main/endpoints.c.

References OBJ_KEY.

Referenced by ast_ari_endpoints_list_by_tech(), and messaging_app_unsubscribe_endpoint().

83 {
84  struct ast_endpoint *endpoint = ao2_find(endpoints, id, OBJ_KEY);
85 
86  if (!endpoint) {
87  endpoint = ao2_find(tech_endpoints, id, OBJ_KEY);
88  }
89 
90  return endpoint;
91 }
#define OBJ_KEY
Definition: astobj2.h:1151
static struct stasis_rest_handlers endpoints
REST handler for /api-docs/endpoints.json.
const char* ast_endpoint_get_id ( const struct ast_endpoint endpoint)

Gets the tech/resource id of the given endpoint.

This is unique across all endpoints, and immutable.

Parameters
endpointThe endpoint.
Returns
Tech/resource id of the endpoint.
Return values
NULLif endpoint is NULL.
Since
12

Definition at line 391 of file main/endpoints.c.

References ast_endpoint::id.

Referenced by app_subscribe_endpoint(), forwards_create_endpoint(), messaging_app_subscribe_endpoint(), and messaging_app_unsubscribe_endpoint().

392 {
393  if (!endpoint) {
394  return NULL;
395  }
396  return endpoint->id;
397 }
const ast_string_field id
const char* ast_endpoint_get_resource ( const struct ast_endpoint endpoint)

Gets the resource name of the given endpoint.

This is unique for the endpoint's technology, and immutable.

Note
If the endpoint being queried is a technology aggregate endpoint, this will be an empty string.
Parameters
endpointThe endpoint.
Returns
Resource name of the endpoint.
Return values
NULLif endpoint is NULL.
Since
12

Definition at line 383 of file main/endpoints.c.

References ast_endpoint::resource.

Referenced by chan_pjsip_devicestate(), and messaging_app_unsubscribe_endpoint().

384 {
385  if (!endpoint) {
386  return NULL;
387  }
388  return endpoint->resource;
389 }
const ast_string_field resource
enum ast_endpoint_state ast_endpoint_get_state ( const struct ast_endpoint endpoint)

Gets the state of the given endpoint.

Parameters
endpointThe endpoint.
Returns
state.
Return values
AST_ENDPOINT_UNKNOWNif endpoint is NULL.
Since
13.4

Definition at line 399 of file main/endpoints.c.

References AST_ENDPOINT_UNKNOWN, and ast_endpoint::state.

400 {
401  if (!endpoint) {
402  return AST_ENDPOINT_UNKNOWN;
403  }
404  return endpoint->state;
405 }
enum ast_endpoint_state state
const char* ast_endpoint_get_tech ( const struct ast_endpoint endpoint)

Gets the technology of the given endpoint.

This is an immutable string describing the channel provider technology (SIP, IAX2, etc.).

Parameters
endpointThe endpoint.
Returns
Tec of the endpoint.
Return values
NULLif endpoint is NULL.
Since
12

Definition at line 375 of file main/endpoints.c.

References ast_endpoint::tech.

Referenced by chan_pjsip_devicestate().

376 {
377  if (!endpoint) {
378  return NULL;
379  }
380  return endpoint->tech;
381 }
const ast_string_field tech
int ast_endpoint_init ( void  )

Endpoint support initialization.

Returns
0 on success.
Non-zero on error.

Definition at line 496 of file main/endpoints.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ast_register_cleanup(), ENDPOINT_BUCKETS, and TECH_ENDPOINT_BUCKETS.

497 {
498  ast_register_cleanup(endpoint_cleanup);
499 
501  ast_endpoint_hash_fn, NULL, ast_endpoint_cmp_fn);
502  if (!endpoints) {
503  return -1;
504  }
505 
507  TECH_ENDPOINT_BUCKETS, ast_endpoint_hash_fn, NULL, ast_endpoint_cmp_fn);
508  if (!tech_endpoints) {
509  return -1;
510  }
511 
512  return 0;
513 }
#define ENDPOINT_BUCKETS
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define TECH_ENDPOINT_BUCKETS
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
static struct stasis_rest_handlers endpoints
REST handler for /api-docs/endpoints.json.
void ast_endpoint_set_max_channels ( struct ast_endpoint endpoint,
int  max_channels 
)

Updates the maximum number of channels an endpoint supports.

Set to -1 for unlimited channels.

Parameters
endpointEndpoint to modify.
max_channelsMaximum number of concurrent channels this endpoint supports.

Definition at line 419 of file main/endpoints.c.

References ast_endpoint::max_channels, and ast_endpoint::resource.

421 {
422  ast_assert(endpoint != NULL);
423  ast_assert(!ast_strlen_zero(endpoint->resource));
424 
425  ao2_lock(endpoint);
426  endpoint->max_channels = max_channels;
427  ao2_unlock(endpoint);
428  endpoint_publish_snapshot(endpoint);
429 }
const ast_string_field resource
int max_channels
Max channels for this endpoint. -1 means unlimited or unknown.
void ast_endpoint_set_state ( struct ast_endpoint endpoint,
enum ast_endpoint_state  state 
)

Updates the state of the given endpoint.

Parameters
endpointEndpoint to modify.
stateNew state.
Since
12

Definition at line 407 of file main/endpoints.c.

References ast_endpoint::resource, and ast_endpoint::state.

Referenced by update_registry(), and xmpp_client_change_state().

409 {
410  ast_assert(endpoint != NULL);
411  ast_assert(!ast_strlen_zero(endpoint->resource));
412 
413  ao2_lock(endpoint);
414  endpoint->state = state;
415  ao2_unlock(endpoint);
416  endpoint_publish_snapshot(endpoint);
417 }
const ast_string_field resource
enum ast_endpoint_state state
void ast_endpoint_shutdown ( struct ast_endpoint endpoint)

Shutsdown an ast_endpoint.

Parameters
endpointEndpoint to shut down.
Since
12

Definition at line 350 of file main/endpoints.c.

References ao2_ref, ao2_unlink, ast_endpoint_topic(), RAII_VAR, ast_endpoint::router, stasis_cache_clear_create(), stasis_message_router_unsubscribe(), stasis_publish(), and ast_endpoint::tech_forward.

Referenced by xmpp_client_destructor().

351 {
352  RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
353 
354  if (endpoint == NULL) {
355  return;
356  }
357 
358  ao2_unlink(endpoints, endpoint);
359  endpoint->tech_forward = stasis_forward_cancel(endpoint->tech_forward);
360 
361  clear_msg = create_endpoint_snapshot_message(endpoint);
362  if (clear_msg) {
363  RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
364  message = stasis_cache_clear_create(clear_msg);
365  if (message) {
367  }
368  }
369 
370  /* Bump refcount to hold on to the router */
371  ao2_ref(endpoint->router, +1);
373 }
struct stasis_forward * tech_forward
struct stasis_topic * ast_endpoint_topic(struct ast_endpoint *endpoint)
Returns the topic for a specific endpoint.
struct stasis_message * stasis_cache_clear_create(struct stasis_message *message)
A message which instructs the caching topic to remove an entry from its cache.
Definition: stasis_cache.c:778
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
struct stasis_message_router * router
static struct stasis_rest_handlers endpoints
REST handler for /api-docs/endpoints.json.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1511
void stasis_message_router_unsubscribe(struct stasis_message_router *router)
Unsubscribe the router from the upstream topic.
#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
const char* ast_endpoint_state_to_string ( enum ast_endpoint_state  state)

Returns a string representation of the given endpoint state.

Parameters
stateEndpoint state.
Returns
String representation of state.
Return values
?if state isn't in ast_endpoint_state.

Definition at line 109 of file main/endpoints.c.

References AST_ENDPOINT_OFFLINE, AST_ENDPOINT_ONLINE, and AST_ENDPOINT_UNKNOWN.

Referenced by ast_endpoint_snapshot_to_json().

110 {
111  switch (state) {
113  return "unknown";
115  return "offline";
116  case AST_ENDPOINT_ONLINE:
117  return "online";
118  }
119  return "?";
120 }