Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Data Structures | Macros | Functions | Variables
stasis_message.c File Reference

Stasis Message API. More...

#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/stasis.h"
#include "asterisk/utils.h"
#include "asterisk/hashtab.h"
Include dependency graph for stasis_message.c:

Go to the source code of this file.

Data Structures

struct  stasis_message
struct  stasis_message_type


#define HAS_VIRTUAL(fn, msg)
#define INVOKE_VIRTUAL(fn, ...)


static void message_type_dtor (void *obj)
int stasis_message_can_be_ami (struct stasis_message *msg)
 Determine if the given message can be converted to AMI. More...
struct stasis_messagestasis_message_create (struct stasis_message_type *type, void *data)
 Create a new message. More...
struct stasis_messagestasis_message_create_full (struct stasis_message_type *type, void *data, const struct ast_eid *eid)
 Create a new message for an entity. More...
void * stasis_message_data (const struct stasis_message *msg)
 Get the data contained in a message. More...
static void stasis_message_dtor (void *obj)
const struct ast_eidstasis_message_eid (const struct stasis_message *msg)
 Get the entity id for a stasis_message. More...
const struct timeval * stasis_message_timestamp (const struct stasis_message *msg)
 Get the time when a message was created. More...
struct ast_manager_event_blobstasis_message_to_ami (struct stasis_message *msg)
 Build the AMI representation of the message. More...
struct ast_eventstasis_message_to_event (struct stasis_message *msg)
 Build the Generic event system representation of the message. More...
struct ast_jsonstasis_message_to_json (struct stasis_message *msg, struct stasis_message_sanitizer *sanitize)
 Build the JSON representation of the message. More...
struct stasis_message_typestasis_message_type (const struct stasis_message *msg)
 Get the message type for a stasis_message. More...
enum stasis_subscription_message_formatters stasis_message_type_available_formatters (const struct stasis_message_type *type)
 Get a bitmap of available formatters for a message type. More...
int stasis_message_type_create (const char *name, struct stasis_message_vtable *vtable, struct stasis_message_type **result)
 Create a new message type. More...
unsigned int stasis_message_type_hash (const struct stasis_message_type *type)
 Gets the hash of a given message type. More...
int stasis_message_type_id (const struct stasis_message_type *type)
 Gets the id of a given message type. More...
const char * stasis_message_type_name (const struct stasis_message_type *type)
 Gets the name of a given message type. More...


static int message_type_id
static struct stasis_message_vtable null_vtable = {}

Detailed Description

Stasis Message API.

David M. Lee, II

Definition in file stasis_message.c.

Macro Definition Documentation


#define HAS_VIRTUAL (   fn,

Definition at line 241 of file stasis_message.c.


#define INVOKE_VIRTUAL (   fn,

Definition at line 211 of file stasis_message.c.

Function Documentation

◆ message_type_dtor()

static void message_type_dtor ( void *  obj)

Definition at line 49 of file stasis_message.c.

50 {
51  struct stasis_message_type *type = obj;
52  ast_free(type->name);
53  type->name = NULL;
54 }
#define ast_free(a)
Definition: astmm.h:180
static const char type[]
Definition: chan_ooh323.c:109
#define NULL
Definition: resample.c:96

References ast_free, NULL, and type.

Referenced by stasis_message_type_create().

◆ stasis_message_can_be_ami()

int stasis_message_can_be_ami ( struct stasis_message msg)

Determine if the given message can be converted to AMI.

msgMessage to see if can be converted to AMI.
Return values
0Cannot be converted
non-zeroCan be converted

Definition at line 251 of file stasis_message.c.

252 {
253  return HAS_VIRTUAL(to_ami, msg);
254 }
static void to_ami(struct ast_sip_subscription *sub, struct ast_str **buf)
#define HAS_VIRTUAL(fn, msg)

References HAS_VIRTUAL, and to_ami().

Referenced by manager_default_msg_cb().

◆ stasis_message_create()

struct stasis_message* stasis_message_create ( struct stasis_message_type type,
void *  data 

Create a new message.

This message is an ao2 object, and must be ao2_cleanup()'ed when you are done with it. Messages are also immutable, and must not be modified after they are initialized. Especially the data in the message.

typeType of the message
dataImmutable data that is the actual contents of the message
New message
Return values
NULLon error

Definition at line 174 of file stasis_message.c.

175 {
177 }
struct stasis_message * stasis_message_create_full(struct stasis_message_type *type, void *data, const struct ast_eid *eid)
Create a new message for an entity.
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93

References ast_eid_default, stasis_message::data, stasis_message_create_full(), and type.

Referenced by aoc_publish_blob(), ast_bridge_blob_create(), ast_bridge_blob_create_from_snapshots(), ast_bridge_publish_attended_transfer(), ast_bridge_publish_blind_transfer(), ast_bridge_publish_merge(), ast_bridge_publish_state(), ast_cdr_engine_term(), ast_channel_publish_dial_internal(), ast_channel_publish_final_snapshot(), ast_channel_publish_snapshot(), ast_endpoint_blob_create(), ast_manager_publish_event(), ast_multi_object_blob_single_channel_publish(), ast_mwi_blob_create(), ast_rtp_publish_rtcp_message(), ast_system_publish_registry(), AST_TEST_DEFINE(), bridge_publish_state_from_blob(), bridge_topics_destroy(), cc_publish(), cdr_read(), cdr_write(), create_channel_blob_message(), create_endpoint_snapshot_message(), endpoint_publish_snapshot(), forkcdr_exec(), handle_security_event(), local_optimization_finished_cb(), local_optimization_started_cb(), presence_state_event(), publish_acl_change(), publish_app_cdr_message(), publish_chanspy_message(), publish_cluster_discovery_to_stasis_full(), publish_corosync_ping_to_stasis(), publish_format_update(), publish_hint_change(), publish_hint_remove(), publish_load_message_type(), publish_local_bridge_message(), publish_parked_call(), publish_parked_call_failure(), queue_publish_member_blob(), queue_publish_multi_channel_snapshot_blob(), send_call_pickup_stasis_message(), send_msg(), send_start_msg_snapshots(), send_subscription_subscribe(), send_subscription_unsubscribe(), stasis_cache_clear_create(), stasis_test_message_create(), stun_monitor_request(), and update_create().

◆ stasis_message_create_full()

struct stasis_message* stasis_message_create_full ( struct stasis_message_type type,
void *  data,
const struct ast_eid eid 

Create a new message for an entity.

This message is an ao2 object, and must be ao2_cleanup()'ed when you are done with it. Messages are also immutable, and must not be modified after they are initialized. Especially the data in the message.

typeType of the message
dataImmutable data that is the actual contents of the message
eidWhat entity originated this message. (NULL for aggregate)
An aggregate message is a combined representation of the local and remote entities publishing the message data. e.g., An aggregate device state represents the combined device state from the local and any remote entities publishing state for a device. e.g., An aggregate MWI message is the old/new MWI counts accumulated from the local and any remote entities publishing to a mailbox.
Return values
NULLon error

Definition at line 140 of file stasis_message.c.

141 {
142  struct stasis_message *message;
144  if (type == NULL || data == NULL) {
145  return NULL;
146  }
150  if (message == NULL) {
151  return NULL;
152  }
154  message->timestamp = ast_tvnow();
155  /*
156  * XXX Normal ao2 ref counting rules says we should increment the message
157  * type ref here and decrement it in stasis_message_dtor(). However, the
158  * stasis message could be cached and legitimately cause the type ref count
159  * to hit the excessive ref count assertion. Since the message type
160  * practically has to be a global object anyway, we can get away with not
161  * holding a ref in the stasis message.
162  */
163  message->type = type;
164  ao2_ref(data, +1);
165  message->data = data;
166  if (eid) {
167  message->eid_ptr = &message->eid;
168  message->eid = *eid;
169  }
171  return message;
172 }
Definition: astobj2.h:367
#define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg)
Allocate and initialize an object.
Definition: astobj2.h:402
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static void stasis_message_dtor(void *obj)
struct ast_eid eid
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:157

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_ref, ao2_t_alloc_options, ast_tvnow(), stasis_message::data, stasis_message::eid, NULL, stasis_message_dtor(), and type.

Referenced by ast_publish_device_state_full(), AST_TEST_DEFINE(), cache_test_message_create_full(), create_foo_type_message(), device_state_aggregate_calc(), mwi_state_create_message(), and stasis_message_create().

◆ stasis_message_data()

void* stasis_message_data ( const struct stasis_message msg)

Get the data contained in a message.

Immutable data pointer
Return values
NULLif msg is NULL.

Definition at line 195 of file stasis_message.c.

196 {
197  if (msg == NULL) {
198  return NULL;
199  }
200  return msg->data;
201 }

References stasis_message::data, and NULL.

Referenced by agent_login_to_ami(), agent_logoff_to_ami(), agi_channel_to_ami(), aoc_to_ami(), appcdr_callback(), ast_ari_endpoints_list(), ast_ari_endpoints_list_by_tech(), ast_bridge_merge_message_to_json(), ast_bridge_publish_enter(), ast_bridge_publish_leave(), ast_channel_entered_bridge_to_json(), ast_channel_left_bridge_to_json(), ast_delete_mwi_state_full(), ast_endpoint_latest_snapshot(), ast_mwi_publish_by_mailbox(), AST_TEST_DEFINE(), asterisk_publisher_devstate_cb(), asterisk_publisher_mwistate_cb(), attended_transfer_to_ami(), attended_transfer_to_json(), blind_transfer_to_ami(), blind_transfer_to_json(), bridge_attended_transfer_handler(), bridge_blind_transfer_handler(), bridge_merge_cb(), bridge_merge_handler(), bridge_snapshot_update(), cache_test_aggregate_calc_fn(), cache_test_data_id(), cache_update(), cache_update_cb(), caching_topic_exec(), call_forwarded_handler(), call_pickup_to_ami(), cc_available_to_ami(), cc_callerrecalling_to_ami(), cc_callerstartmonitoring_to_ami(), cc_callerstopmonitoring_to_ami(), cc_failure_to_ami(), cc_monitorfailed_to_ami(), cc_offertimerstart_to_ami(), cc_recallcomplete_to_ami(), cc_requestacknowledged_to_ami(), cc_requested_to_ami(), cdr_prop_write_callback(), cdr_read_callback(), cdr_write_callback(), cel_attended_transfer_cb(), cel_blind_transfer_cb(), cel_bridge_enter_cb(), cel_bridge_leave_cb(), cel_dial_cb(), cel_generic_cb(), cel_local_cb(), cel_parking_cb(), cel_pickup_cb(), cel_snapshot_update_cb(), channel_blob_to_json(), channel_chanspy_start_cb(), channel_chanspy_stop_cb(), channel_dial_cb(), channel_dtmf_begin_cb(), channel_dtmf_end_cb(), channel_enter_cb(), channel_fax_cb(), channel_flash_cb(), channel_hangup_handler_cb(), channel_hangup_request_cb(), channel_hold_cb(), channel_leave_cb(), channel_mixmonitor_mute_cb(), channel_mixmonitor_start_cb(), channel_mixmonitor_stop_cb(), channel_moh_start_cb(), channel_moh_stop_cb(), channel_monitor_start_cb(), channel_monitor_stop_cb(), channel_snapshot_update(), channel_unhold_cb(), channel_wink_cb(), check_cache_aggregate(), conf_send_event_to_participants(), confbridge_atxfer_cb(), confbridge_publish_manager_event(), confbridge_talking_cb(), consumer_exec(), contactstatus_to_ami(), contactstatus_to_json(), corosync_ping_to_event(), dahdichannel_to_ami(), device_state_aggregate_calc(), device_state_cb(), device_state_get_id(), devstate_change_cb(), devstate_to_ami(), dial_to_json(), dtmf_end_to_json(), dump_cache_load(), dump_cache_unload(), dump_consumer(), endpoint_cache_clear(), endpoint_snapshot_get_id(), explicit_publish_cb(), fake_ami(), fake_json(), find_route(), forkcdr_callback(), generic_agent_devstate_cb(), generic_monitor_devstate_cb(), get_bool_header(), get_cached_mwi(), handle_attended_transfer(), handle_blind_transfer(), handle_bridge_enter(), handle_bridge_enter_message(), handle_bridge_leave_message(), handle_channel_snapshot_update_message(), handle_dial_message(), handle_hangup(), handle_hint_change_message_type(), handle_local_optimization_begin(), handle_local_optimization_end(), handle_masquerade(), handle_mwi_state(), handle_parked_call_message(), has_voicemail(), hold_to_json(), implicit_publish_cb(), is_msg(), local_message_to_ami(), manager_generic_msg_cb(), meetme_stasis_cb(), moh_post_start(), moh_post_stop(), multi_user_event_to_ami(), multi_user_event_to_json(), mwi_app_event_cb(), mwi_event_cb(), mwi_startup_event_cb(), mwi_state_get_id(), mwi_to_event(), mwi_update_cb(), park_announce_update_cb(), parker_update_cb(), parking_event_cb(), peerstatus_to_ami(), peerstatus_to_json(), playback_to_json(), presence_state_get_id(), presence_state_to_ami(), queue_agent_cb(), queue_channel_to_ami(), queue_member_to_ami(), queue_multi_channel_to_ami(), recording_to_json(), refer_progress_bridge(), remove_device_states_cb(), rtcp_report_to_ami(), rtcp_report_to_json(), security_event_to_ami(), security_stasis_cb(), session_timeout_to_ami(), startup_event_cb(), stasis_end_to_json(), stasis_start_to_json(), stasis_state_subscriber_data(), stasis_subscription_final_message(), sub_bridge_update_handler(), sub_channel_update_handler(), sub_endpoint_update_handler(), subscription_persistence_event_cb(), system_registry_to_ami(), talking_start_to_ami(), talking_stop_to_ami(), unhold_to_json(), unistim_send_mwi_to_peer(), update_registry(), updates(), varset_to_ami(), xmpp_pubsub_devstate_cb(), and xmpp_pubsub_mwi_cb().

◆ stasis_message_dtor()

static void stasis_message_dtor ( void *  obj)

Definition at line 134 of file stasis_message.c.

135 {
136  struct stasis_message *message = obj;
137  ao2_cleanup(message->data);
138 }
#define ao2_cleanup(obj)
Definition: astobj2.h:1934

References ao2_cleanup.

Referenced by stasis_message_create_full().

◆ stasis_message_eid()

const struct ast_eid* stasis_message_eid ( const struct stasis_message msg)

Get the entity id for a stasis_message.

msgMessage to get eid.
Return values
Entityid of msg
NULLif msg is an aggregate or msg is NULL.

Definition at line 179 of file stasis_message.c.

180 {
181  if (msg == NULL) {
182  return NULL;
183  }
184  return msg->eid_ptr;
185 }
const struct ast_eid * eid_ptr

References stasis_message::eid_ptr, and NULL.

Referenced by AST_TEST_DEFINE(), cache_entry_by_eid(), cache_entry_create(), cache_put(), cache_remove(), caching_topic_exec(), and clear_node_cache().

◆ stasis_message_timestamp()

const struct timeval* stasis_message_timestamp ( const struct stasis_message msg)

◆ stasis_message_to_ami()

struct ast_manager_event_blob* stasis_message_to_ami ( struct stasis_message msg)

Build the AMI representation of the message.

May return NULL, to indicate no representation. The returned object should be ao2_cleanup()'ed.

msgMessage to convert to AMI.
Return values
NULLif AMI format is not supported.

Definition at line 224 of file stasis_message.c.

225 {
226  return INVOKE_VIRTUAL(to_ami, msg);
227 }
#define INVOKE_VIRTUAL(fn,...)

References INVOKE_VIRTUAL, and to_ami().

Referenced by action_devicestatelist(), action_presencestatelist(), AST_TEST_DEFINE(), and manager_default_msg_cb().

◆ stasis_message_to_event()

struct ast_event* stasis_message_to_event ( struct stasis_message msg)

Build the Generic event system representation of the message.

May return NULL, to indicate no representation. The returned object should be disposed of via ast_event_destroy.

msgMessage to convert to AMI.
Return values
NULLif AMI format is not supported.

Definition at line 236 of file stasis_message.c.

237 {
238  return INVOKE_VIRTUAL(to_event, msg);
239 }


Referenced by publish_to_corosync().

◆ stasis_message_to_json()

struct ast_json* stasis_message_to_json ( struct stasis_message msg,
struct stasis_message_sanitizer sanitize 

Build the JSON representation of the message.

May return NULL, to indicate no representation. The returned object should be ast_json_unref()'ed.

msgMessage to convert to JSON string.
sanitizeSnapshot sanitization callback.
Newly allocated string with JSON message.
Return values
NULLif JSON format is not supported.

Definition at line 229 of file stasis_message.c.

232 {
233  return INVOKE_VIRTUAL(to_json, msg, sanitize);
234 }


Referenced by AST_TEST_DEFINE(), and rtcp_message_handler().

◆ stasis_message_type()

struct stasis_message_type* stasis_message_type ( const struct stasis_message msg)

Get the message type for a stasis_message.

msgMessage to type
Type of msg
Return values
NULLif msg is NULL.

Definition at line 187 of file stasis_message.c.

188 {
189  if (msg == NULL) {
190  return NULL;
191  }
192  return msg->type;
193 }
struct stasis_message_type * type

References NULL, and stasis_message::type.

Referenced by acl_change_stasis_cb(), appcdr_callback(), AST_TEST_DEFINE(), asterisk_publisher_devstate_cb(), asterisk_publisher_mwistate_cb(), cache_entry_create(), cache_simple(), cache_test_aggregate_calc_fn(), cache_test_data_id(), cache_update(), caching_topic_exec(), cdr_prop_write_callback(), cdr_read_callback(), cdr_write_callback(), conf_send_event_to_participants(), confbridge_publish_manager_event(), device_state_cb(), device_state_get_id(), devstate_change_cb(), dispatch_message(), dump_consumer(), endpoint_snapshot_get_id(), find_route(), forkcdr_callback(), generic_agent_devstate_cb(), generic_monitor_devstate_cb(), handle_hint_change_message_type(), is_msg(), local_message_to_ami(), meetme_stasis_cb(), message_sink_cb(), mwi_event_cb(), mwi_startup_event_cb(), mwi_stasis_cb(), mwi_state_get_id(), mwi_update_cb(), network_change_stasis_cb(), pack_bridge_and_channels(), park_announce_update_cb(), parker_update_cb(), parking_event_cb(), presence_state_get_id(), publish_msg(), queue_agent_cb(), refer_progress_bridge(), rtcp_report_to_ami(), rtcp_report_to_json(), rtp_topic_handler(), security_event_to_ami(), security_stasis_cb(), startup_event_cb(), stasis_subscription_final_message(), statsmaker(), sub_endpoint_update_handler(), subscription_invoke(), subscription_persistence_event_cb(), update_create(), xmpp_pubsub_devstate_cb(), and xmpp_pubsub_mwi_cb().

◆ stasis_message_type_available_formatters()

enum stasis_subscription_message_formatters stasis_message_type_available_formatters ( const struct stasis_message_type message_type)

Get a bitmap of available formatters for a message type.

message_typeMessage type
A bitmap of stasis_subscription_message_formatters

Definition at line 109 of file stasis_message.c.

116 {
117  return type->available_formatters;
118 }

References type.

Referenced by dispatch_message().

◆ stasis_message_type_create()

int stasis_message_type_create ( const char *  name,
struct stasis_message_vtable vtable,
struct stasis_message_type **  result 

Create a new message type.

stasis_message_type is an AO2 object, so ao2_cleanup() when you're done with it.

nameName of the new type.
vtableVirtual table of message methods. May be NULL.
[out]resultThe location where the new message type will be placed
Stasis message type creation may be declined if the message type is disabled
A stasis_message_type_result enum

Definition at line 56 of file stasis_message.c.

59 {
60  struct stasis_message_type *type;
62  /* Check for declination */
65  }
69  if (!type) {
71  }
72  if (!vtable) {
73  /* Null object pattern, FTW! */
75  }
77  type->name = ast_strdup(name);
78  if (!type->name) {
81  }
83  type->vtable = vtable;
84  if (vtable->to_json) {
85  type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_JSON;
86  }
87  if (vtable->to_ami) {
88  type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_AMI;
89  }
90  if (vtable->to_event) {
91  type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_EVENT;
92  }
94  *result = type;
97 }
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
static PGresult * result
Definition: cel_pgsql.c:84
static const char name[]
Definition: format_mp3.c:68
unsigned int ast_hashtab_hash_string(const void *obj)
Hashes a string to a number.
Definition: hashtab.c:153
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
Definition: stasis.h:312
Definition: stasis.h:311
Definition: stasis.h:310
int stasis_message_type_declined(const char *name)
Check whether a message type is declined.
Definition: stasis.c:2285
Definition: stasis.h:288
Definition: stasis.h:286
Definition: stasis.h:287
static void message_type_dtor(void *obj)
static int message_type_id
static struct stasis_message_vtable null_vtable
struct stasis_message_vtable * vtable
struct ast_json *(* to_json)(struct stasis_message *message, const struct stasis_message_sanitizer *sanitize)
Build the JSON representation of the message.
Definition: stasis.h:252
struct ast_manager_event_blob *(* to_ami)(struct stasis_message *message)
Build the AMI representation of the message.
Definition: stasis.h:264
struct ast_event *(* to_event)(struct stasis_message *message)
Build the ast_event representation of the message.
Definition: stasis.h:278

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_cleanup, ao2_t_alloc_options, ast_atomic_fetchadd_int(), ast_hashtab_hash_string(), ast_strdup, message_type_dtor(), message_type_id, name, null_vtable, result, STASIS_MESSAGE_TYPE_DECLINED, stasis_message_type_declined(), STASIS_MESSAGE_TYPE_ERROR, STASIS_MESSAGE_TYPE_SUCCESS, STASIS_SUBSCRIPTION_FORMATTER_AMI, STASIS_SUBSCRIPTION_FORMATTER_EVENT, STASIS_SUBSCRIPTION_FORMATTER_JSON, stasis_message_vtable::to_ami, stasis_message_vtable::to_event, stasis_message_vtable::to_json, type, and stasis_message_type::vtable.

Referenced by AST_TEST_DEFINE(), and create_message_types().

◆ stasis_message_type_hash()

unsigned int stasis_message_type_hash ( const struct stasis_message_type type)

Gets the hash of a given message type.

typeThe type to get the hash of.
The hash

Definition at line 104 of file stasis_message.c.

105 {
106  return type->hash;
107 }

References type.

Referenced by cache_entry_compute_hash().

◆ stasis_message_type_id()

int stasis_message_type_id ( const struct stasis_message_type type)

Gets the id of a given message type.

typeThe type to get the id of.
The id

Definition at line 109 of file stasis_message.c.

110 {
111  return type->id;
112 }

Referenced by dispatch_message(), publish_msg(), stasis_subscription_accept_message_type(), stasis_subscription_decline_message_type(), and subscription_invoke().

◆ stasis_message_type_name()

const char* stasis_message_type_name ( const struct stasis_message_type type)

Gets the name of a given message type.

typeThe type to get.
Name of the type.
Return values
NULLif type is NULL.

Definition at line 99 of file stasis_message.c.

100 {
101  return type->name;
102 }

References type.

Referenced by AST_TEST_DEFINE(), cache_find(), cache_simple(), cache_test_data_id(), caching_topic_exec(), dump_consumer(), print_cache_entry(), send_msg(), stasis_subscription_accept_message_type(), stasis_subscription_decline_message_type(), and statsmaker().

Variable Documentation

◆ message_type_id

int message_type_id

Definition at line 47 of file stasis_message.c.

Referenced by publish_msg(), stasis_message_type_create(), and subscription_invoke().

◆ null_vtable

struct stasis_message_vtable null_vtable = {}

Definition at line 1 of file stasis_message.c.

Referenced by stasis_message_type_create().