27 #include "asterisk/stasis_channels.h"
55 strsep(&context,
"@");
56 if (ast_strlen_zero(context)) {
76 .to_event = mwi_to_event, );
81 static void mwi_state_dtor(
void *obj)
96 return mwi_state_cache;
109 static struct ast_mwi_state *mwi_create_state(
const char *mailbox,
const char *context,
114 ast_assert(!ast_strlen_zero(mailbox));
116 mwi_state = ao2_alloc(
sizeof(*mwi_state), mwi_state_dtor);
118 ast_log(LOG_ERROR,
"Unable to create MWI state for mailbox '%s@%s'\n",
119 mailbox, ast_strlen_zero(context) ?
"" : context);
124 ast_log(LOG_ERROR,
"Unable to initialize MWI state for mailbox '%s@%s'\n",
125 mailbox, ast_strlen_zero(context) ?
"" : context);
129 if (!ast_strlen_zero(context)) {
142 static struct ast_mwi_state *mwi_retrieve_then_create_state(
const char *mailbox)
149 return mwi_create_state(mailbox, NULL, urgent_msgs, new_msgs, old_msgs);
154 return mwi_create_state(mailbox, context, 0, 0, 0);
180 const char *channel_id,
190 mwi_state = mwi_create_state(mailbox, context, urgent_msgs, new_msgs, old_msgs);
195 if (!ast_strlen_zero(channel_id)) {
212 ao2_cleanup(mwi_state);
232 mwi_state_manager, mailbox);
240 mwi_state_manager, mailbox, callback, data);
298 mwi_state_manager, mailbox);
318 static int handle_mwi_state(
const char *
id,
struct stasis_message *msg,
void *user_data)
325 return d->handler(mwi_state, d->data);
328 mwi_state = mwi_create_state(
id, NULL, 0, 0, 0);
333 res = d->handler(mwi_state, d->data);
359 int new_msgs,
int old_msgs,
const char *channel_id,
struct ast_eid *eid)
363 NULL, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
376 int new_msgs,
int old_msgs,
const char *channel_id,
struct ast_eid *eid)
380 mailbox, context, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
398 const char *channel_id,
411 msg = mwi_state_create_message(mailbox, context, 0, 0, 0, NULL, eid);
437 ao2_cleanup(cached_msg);
441 ao2_cleanup(clear_msg);
446 static const char *mwi_state_get_id(
struct stasis_message *message)
459 static void mwi_blob_dtor(
void *obj)
474 ast_assert(blob != NULL);
480 obj = ao2_alloc(
sizeof(*obj), mwi_blob_dtor);
496 static void mwi_cleanup(
void)
498 ao2_cleanup(mwi_state_cache);
499 mwi_state_cache = NULL;
501 ao2_cleanup(mwi_state_manager);
502 mwi_state_manager = NULL;
520 if (!mwi_state_manager) {
525 if (!mwi_state_cache) {
530 if (!mwi_topic_cached) {
Managed stasis state event interface.
int ast_delete_mwi_state_full(const char *mailbox, const char *context, struct ast_eid *eid)
Delete MWI state cached by stasis with all parameters.
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
struct stasis_topic * ast_mwi_topic_cached(void)
Get the Stasis Message Bus API caching topic for MWI messages.
Asterisk main include file. File version handling, generic pbx functions.
void * ast_mwi_unsubscribe(struct ast_mwi_subscriber *sub)
Unsubscribe from the stasis topic and MWI.
struct stasis_state_subscriber * stasis_state_subscribe_pool(struct stasis_state_manager *manager, const char *id, stasis_subscription_cb callback, void *data)
Add a subscriber, and subscribe to its underlying stasis topic.
struct stasis_message * ast_mwi_blob_create(struct ast_mwi_state *mwi_state, struct stasis_message_type *message_type, struct ast_json *blob)
Creates a ast_mwi_blob message.
struct stasis_state_subscriber * stasis_state_add_subscriber(struct stasis_state_manager *manager, const char *id)
Add a subscriber to the managed stasis state for the given id.
void * stasis_state_unsubscribe(struct stasis_state_subscriber *sub)
Unsubscribe from the stasis topic and stasis state.
void stasis_state_remove_publish_by_id(struct stasis_state_manager *manager, const char *id, const struct ast_eid *eid, struct stasis_message *msg)
Publish to a managed named by id topic, and remove an implicit publisher.
void stasis_state_remove_observer(struct stasis_state_manager *manager, struct stasis_state_observer *observer)
Remove an observer (will no longer receive managed state related events).
struct stasis_topic * stasis_state_subscriber_topic(struct stasis_state_subscriber *sub)
Retrieve the subscriber's topic.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
struct ast_mwi_state * ast_mwi_create(const char *mailbox, const char *context)
Create a ast_mwi_state object.
struct stasis_state_subscriber * base
struct stasis_caching_topic * stasis_caching_topic_create(struct stasis_topic *original_topic, struct stasis_cache *cache)
Create a topic which monitors and caches messages from another topic.
const char * stasis_state_publisher_id(const struct stasis_state_publisher *pub)
Retrieve the publisher's underlying state's unique id.
struct stasis_subscription * ast_mwi_subscriber_subscription(struct ast_mwi_subscriber *sub)
Retrieve the stasis MWI topic subscription if available.
void * stasis_state_subscriber_data(struct stasis_state_subscriber *sub)
Retrieve the last known state stasis message payload for the subscriber.
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
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.
struct stasis_topic * ast_mwi_subscriber_topic(struct ast_mwi_subscriber *sub)
Retrieves the MWI subscriber's topic.
Number of new messages Used by: AST_EVENT_MWI Payload type: UINT.
Number of Used by: AST_EVENT_MWI Payload type: UINT.
const char * stasis_state_subscriber_id(const struct stasis_state_subscriber *sub)
Retrieve the underlying subscribed to state's unique id.
struct stasis_message * stasis_cache_get_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const char *id, const struct ast_eid *eid)
Retrieve an item from the cache for a specific entity.
struct ast_mwi_state * mwi_state
void ast_mwi_state_callback_subscribed(on_mwi_state handler, void *data)
For each managed mailbox that has a subscriber call the given handler.
struct ast_mwi_state * ast_mwi_subscriber_data(struct ast_mwi_subscriber *sub)
Retrieves the state data object associated with the MWI subscriber.
void * stasis_state_unsubscribe_and_join(struct stasis_state_subscriber *sub)
Unsubscribe from the stasis topic, block until the final message is received, and then unsubscribe fr...
int ast_mwi_add_observer(struct ast_mwi_observer *observer)
Add an observer to receive MWI state related events.
struct stasis_state_manager * stasis_state_manager_create(const char *topic_name)
Create a stasis state manager.
int stasis_state_add_observer(struct stasis_state_manager *manager, struct stasis_state_observer *observer)
Add an observer to receive managed state related events.
An Entity ID is essentially a MAC address, brief and unique.
struct stasis_caching_topic * stasis_caching_unsubscribe_and_join(struct stasis_caching_topic *caching_topic)
Unsubscribes a caching topic from its upstream topic, blocking until all messages have been forwarded...
Entity ID Used by All events Payload type: RAW This IE indicates which server the event originated fr...
int ast_app_inboxcount2(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs)
Determine number of urgent/new/old messages in a mailbox.
Context IE Used by AST_EVENT_MWI Payload type: str.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
void stasis_state_callback_all(struct stasis_state_manager *manager, on_stasis_state handler, void *data)
For each managed state call the given handler.
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
struct stasis_cache * ast_mwi_state_cache(void)
Backend cache for ast_mwi_topic_cached().
struct stasis_state_publisher * stasis_state_add_publisher(struct stasis_state_manager *manager, const char *id)
Add a publisher to the managed state for the given id.
int(* on_mwi_state)(struct ast_mwi_state *mwi_state, void *data)
The delegate called for each managed mailbox state.
struct stasis_topic * ast_mwi_topic(const char *uniqueid)
Get the Stasis Message Bus API topic for MWI messages on a unique ID.
int ast_publish_mwi_state_full(const char *mailbox, const char *context, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Publish a MWI state update via stasis with all parameters.
#define STASIS_MESSAGE_TYPE_DEFN(name,...)
Boiler-plate messaging macro for defining public message types.
void stasis_state_callback_subscribed(struct stasis_state_manager *manager, on_stasis_state handler, void *data)
For each managed, and explicitly subscribed state call the given handler.
int ast_mwi_publish(struct ast_mwi_publisher *publisher, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Publish MWI for the given mailbox.
struct ast_mwi_subscriber * ast_mwi_add_subscriber(const char *mailbox)
Add an MWI state subscriber to the mailbox.
void ast_mwi_remove_observer(struct ast_mwi_observer *observer)
Remove an MWI state observer.
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_state_publish_by_id(struct stasis_state_manager *manager, const char *id, const struct ast_eid *eid, struct stasis_message *msg)
Publish to a managed named by id topic, and add an implicit subscriber.
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
struct stasis_subscription * stasis_state_subscriber_subscription(struct stasis_state_subscriber *sub)
Retrieve the stasis topic subscription if available.
static struct sorcery_test_observer observer
Global scope observer structure for testing.
void(* stasis_subscription_cb)(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Callback function type for Stasis subscriptions.
int mwi_init(void)
Initialize the mwi core.
void ast_mwi_state_callback_all(on_mwi_state handler, void *data)
For each managed mailbox call the given handler.
void * ast_mwi_unsubscribe_and_join(struct ast_mwi_subscriber *sub)
Unsubscribe from the stasis topic, block until the final message is received, and then unsubscribe fr...
Holds details about changes to subscriptions for the specified topic.
struct stasis_topic * stasis_state_all_topic(struct stasis_state_manager *manager)
Retrieve the manager's topic (the topic that all state topics get forwarded to)
Object that represents an MWI update with some additional application defined data.
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
const ast_string_field uniqueid
struct ast_eid ast_eid_default
Global EID.
struct stasis_cache * stasis_cache_create(snapshot_get_id id_fn)
Create a cache.
struct ast_mwi_publisher * ast_mwi_add_publisher(const char *mailbox)
Add an MWI state publisher to the mailbox.
struct ast_channel_snapshot * ast_channel_snapshot_get_latest(const char *uniqueid)
Obtain the latest ast_channel_snapshot from the Stasis Message Bus API cache. This is an ao2 object...
struct ast_event * ast_event_new(enum ast_event_type event_type,...)
Create a new event.
MWI state event interface.
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
Abstract JSON element (object, array, string, int, ...).
int ast_mwi_publish_by_mailbox(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Publish MWI for the given mailbox.
struct stasis_message_type * stasis_subscription_change_type(void)
Gets the message type for subscription change notices.
struct stasis_state_publisher * base
struct stasis_topic * ast_mwi_topic_all(void)
Get the Stasis Message Bus API topic for MWI messages.
struct stasis_message_type * ast_mwi_vm_app_type(void)
Get the Stasis Message Bus API message type for voicemail application specific messages.
The structure that contains MWI state.
struct ast_channel_snapshot * snapshot
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 stasis_topic * stasis_caching_get_topic(struct stasis_caching_topic *caching_topic)
Returns the topic of cached events from a caching topics.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
struct ast_mwi_subscriber * ast_mwi_subscribe_pool(const char *mailbox, stasis_subscription_cb callback, void *data)
Add an MWI state subscriber, and stasis subscription to the mailbox.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
void stasis_state_publish(struct stasis_state_publisher *pub, struct stasis_message *msg)
Publish to a managed state (topic) using a publisher.
struct stasis_topic * stasis_state_topic(struct stasis_state_manager *manager, const char *id)
Retrieve a managed topic creating one if not currently managed.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.