Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
mwi.c File Reference
#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/mwi.h"
#include "asterisk/stasis_channels.h"
Include dependency graph for mwi.c:

Go to the source code of this file.

Data Structures

struct  ast_mwi_publisher
 
struct  ast_mwi_subscriber
 
struct  mwi_handler_data
 

Functions

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. More...
 
int ast_mwi_add_observer (struct ast_mwi_observer *observer)
 Add an observer to receive MWI state related events. More...
 
struct ast_mwi_publisherast_mwi_add_publisher (const char *mailbox)
 Add an MWI state publisher to the mailbox. More...
 
struct ast_mwi_subscriberast_mwi_add_subscriber (const char *mailbox)
 Add an MWI state subscriber to the mailbox. More...
 
struct stasis_messageast_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. More...
 
struct ast_mwi_stateast_mwi_create (const char *mailbox, const char *context)
 Create a ast_mwi_state object. More...
 
int ast_mwi_publish (struct ast_mwi_publisher *pub, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
 Publish MWI for the given mailbox. More...
 
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. More...
 
void ast_mwi_remove_observer (struct ast_mwi_observer *observer)
 Remove an MWI state observer. More...
 
struct stasis_cacheast_mwi_state_cache (void)
 Backend cache for ast_mwi_topic_cached(). More...
 
void ast_mwi_state_callback_all (on_mwi_state handler, void *data)
 For each managed mailbox call the given handler. More...
 
void ast_mwi_state_callback_subscribed (on_mwi_state handler, void *data)
 For each managed mailbox that has a subscriber call the given handler. More...
 
struct ast_mwi_subscriberast_mwi_subscribe_pool (const char *mailbox, stasis_subscription_cb callback, void *data)
 Add an MWI state subscriber, and stasis subscription to the mailbox. More...
 
struct ast_mwi_stateast_mwi_subscriber_data (struct ast_mwi_subscriber *sub)
 Retrieves the state data object associated with the MWI subscriber. More...
 
struct stasis_subscriptionast_mwi_subscriber_subscription (struct ast_mwi_subscriber *sub)
 Retrieve the stasis MWI topic subscription if available. More...
 
struct stasis_topicast_mwi_subscriber_topic (struct ast_mwi_subscriber *sub)
 Retrieves the MWI subscriber's topic. More...
 
struct stasis_topicast_mwi_topic (const char *uniqueid)
 Get the Stasis Message Bus API topic for MWI messages on a unique ID. More...
 
struct stasis_topicast_mwi_topic_all (void)
 Get the Stasis Message Bus API topic for MWI messages. More...
 
struct stasis_topicast_mwi_topic_cached (void)
 Get the Stasis Message Bus API caching topic for MWI messages. More...
 
void * ast_mwi_unsubscribe (struct ast_mwi_subscriber *sub)
 Unsubscribe from the stasis topic and MWI. More...
 
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 from MWI. More...
 
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. More...
 
static int handle_mwi_state (const char *id, struct stasis_message *msg, void *user_data)
 
static void mwi_blob_dtor (void *obj)
 
static void mwi_cleanup (void)
 
static struct ast_mwi_statemwi_create_state (const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs)
 
int mwi_init (void)
 Initialize the mwi core. More...
 
static struct ast_mwi_statemwi_retrieve_then_create_state (const char *mailbox)
 
static struct stasis_messagemwi_state_create_message (const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
 
static void mwi_state_dtor (void *obj)
 
static const char * mwi_state_get_id (struct stasis_message *message)
 
static struct ast_eventmwi_to_event (struct stasis_message *message)
 Convert a MWI stasis_message to a ast_event. More...
 
 STASIS_MESSAGE_TYPE_DEFN (ast_mwi_state_type,.to_event=mwi_to_event,)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_mwi_vm_app_type)
 

Variables

static struct stasis_cachemwi_state_cache
 
static struct stasis_state_managermwi_state_manager
 
static struct stasis_caching_topicmwi_topic_cached
 

Function Documentation

◆ ast_delete_mwi_state_full()

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.

Since
12.2.0
Parameters
[in]mailboxThe mailbox identifier string.
[in]contextThe context this mailbox resides in (NULL or "" if only using mailbox)
[in]eidThe EID of the server that originally published the message
Return values
0Success
-1Failure

Definition at line 399 of file mwi.c.

References ao2_cleanup, ast_eid_default, ast_mwi_state_cache(), ast_mwi_state_type(), mwi_state_create_message(), NULL, RAII_VAR, stasis_cache_clear_create(), stasis_cache_get_by_eid(), stasis_message_data(), stasis_state_remove_publish_by_id(), and ast_mwi_state::uniqueid.

Referenced by free_user_final(), and mwi_handle_unsubscribe2().

400 {
401  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
402  struct stasis_message *cached_msg;
403  struct stasis_message *clear_msg;
404  struct ast_mwi_state *mwi_state;
405 
406  msg = mwi_state_create_message(mailbox, context, 0, 0, 0, NULL, eid);
407  if (!msg) {
408  return -1;
409  }
410 
411  mwi_state = stasis_message_data(msg);
412 
413  /*
414  * XXX As far as stasis is concerned, all MWI events are local.
415  *
416  * For now, it is assumed that there is only one entity
417  * maintaining the state of a particular mailbox.
418  *
419  * If we ever have multiple MWI event entities maintaining
420  * the same mailbox that wish to delete their cached entry
421  * we will need to do something about the race condition
422  * potential between checking the cache and removing the
423  * cache entry.
424  */
427  if (!cached_msg) {
428  /* Nothing to clear from the cache, but still need to remove state */
430  return -1;
431  }
432  ao2_cleanup(cached_msg);
433 
434  clear_msg = stasis_cache_clear_create(msg);
435  stasis_state_remove_publish_by_id(mwi_state_manager, mwi_state->uniqueid, eid, clear_msg);
436  ao2_cleanup(clear_msg);
437 
438  return 0;
439 }
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.
Definition: stasis_state.c:658
struct stasis_cache * ast_mwi_state_cache(void)
Backend cache for ast_mwi_topic_cached().
Definition: mwi.c:90
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
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.
Definition: stasis_cache.c:659
#define NULL
Definition: resample.c:96
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#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:911
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
const ast_string_field uniqueid
Definition: mwi.h:460
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
The structure that contains MWI state.
Definition: mwi.h:457
static struct stasis_message * mwi_state_create_message(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Definition: mwi.c:169

◆ ast_mwi_add_observer()

int ast_mwi_add_observer ( struct ast_mwi_observer observer)

Add an observer to receive MWI state related events.

Parameters
observerThe observer handling events
Return values
0if successfully registered, -1 otherwise
Since
13.28.0
16.5.0

Definition at line 296 of file mwi.c.

References stasis_state_add_observer().

Referenced by start_poll_thread(), and subscriptions_create().

297 {
299  (struct stasis_state_observer *)observer);
300 }
Managed stasis state event interface.
Definition: stasis_state.h:463
int stasis_state_add_observer(struct stasis_state_manager *manager, struct stasis_state_observer *observer)
Add an observer to receive managed state related events.
Definition: stasis_state.c:688
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32

◆ ast_mwi_add_publisher()

struct ast_mwi_publisher* ast_mwi_add_publisher ( const char *  mailbox)

Add an MWI state publisher to the mailbox.

Adding a publisher to a mailbox will create a stasis topic for the mailbox if one does not already exist. A publisher can be removed by releasing its reference. Doing so releases its underlying reference to the MWI state.

Parameters
mailboxThe mailbox id to publish to
Return values
AnMWI publisher object
NULlon error
Since
13.28.0
16.5.0

Definition at line 290 of file mwi.c.

References stasis_state_add_publisher().

Referenced by publishers_create().

291 {
294 }
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
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.
Definition: stasis_state.c:531

◆ ast_mwi_add_subscriber()

struct ast_mwi_subscriber* ast_mwi_add_subscriber ( const char *  mailbox)

Add an MWI state subscriber to the mailbox.

Adding a subscriber to a mailbox will create a stasis topic for the mailbox if one does not already exist. It does not however subscribe to the topic itself. This is done separately using a call to stasis_subscribe or stasis_subscribe_pool.

A subscriber can be removed by releasing its reference. Doing so releases its underlying reference to the MWI state. It does not unsubscribe from the topic. Unsubscribing from a topic should be done prior to unsubscribing the state.

Parameters
mailboxThe subscription state mailbox id
Return values
AnMWI subscriber object
NULLon error
Since
13.28.0
16.5.0

Definition at line 224 of file mwi.c.

References stasis_state_add_subscriber().

225 {
228 }
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.
Definition: stasis_state.c:412
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204

◆ ast_mwi_publish()

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.

Parameters
publisherThe publisher to publish a mailbox update on
urgent_msgsThe number of urgent messages in this mailbox
new_msgsThe number of new messages in this mailbox
old_msgsThe number of old messages in this mailbox
channel_idA unique identifier for a channel associated with this change in mailbox state
eidThe EID of the server that originally published the message
Return values
0on success
-1on failure
Since
13.28.0
16.5.0

Definition at line 353 of file mwi.c.

References ao2_ref, mwi_state_create_message(), NULL, stasis_state_publish(), and stasis_state_publisher_id().

Referenced by explicit_publish_cb().

355 {
356  struct stasis_state_publisher *p = (struct stasis_state_publisher *)pub;
358  NULL, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
359 
360  if (!msg) {
361  return -1;
362  }
363 
364  stasis_state_publish(p, msg);
365  ao2_ref(msg, -1);
366 
367  return 0;
368 }
const char * stasis_state_publisher_id(const struct stasis_state_publisher *pub)
Retrieve the publisher's underlying state's unique id.
Definition: stasis_state.c:552
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void stasis_state_publish(struct stasis_state_publisher *pub, struct stasis_message *msg)
Publish to a managed state (topic) using a publisher.
Definition: stasis_state.c:562
static struct stasis_message * mwi_state_create_message(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Definition: mwi.c:169

◆ ast_mwi_publish_by_mailbox()

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.

Parameters
mailboxThe mailbox identifier string.
contextThe context this mailbox resides in (NULL or "" if only using mailbox)
urgent_msgsThe number of urgent messages in this mailbox
new_msgsThe number of new messages in this mailbox
old_msgsThe number of old messages in this mailbox
channel_idA unique identifier for a channel associated with this change in mailbox state
eidThe EID of the server that originally published the message
Return values
0on success
-1on failure
Since
13.28.0
16.5.0

Definition at line 370 of file mwi.c.

References ao2_ref, mwi_state_create_message(), NULL, stasis_message_data(), stasis_state_publish_by_id(), and ast_mwi_state::uniqueid.

Referenced by ast_publish_mwi_state_full(), and implicit_publish_cb().

372 {
373  struct ast_mwi_state *mwi_state;
375  mailbox, context, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
376 
377  if (!msg) {
378  return -1;
379  }
380 
381  mwi_state = stasis_message_data(msg);
383  ao2_ref(msg, -1);
384 
385  return 0;
386 }
#define NULL
Definition: resample.c:96
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a 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.
Definition: stasis_state.c:638
const ast_string_field uniqueid
Definition: mwi.h:460
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
The structure that contains MWI state.
Definition: mwi.h:457
static struct stasis_message * mwi_state_create_message(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Definition: mwi.c:169

◆ ast_mwi_remove_observer()

void ast_mwi_remove_observer ( struct ast_mwi_observer observer)

Remove an MWI state observer.

Parameters
observerThe observer being removed
Since
13.28.0
16.5.0

Definition at line 302 of file mwi.c.

References stasis_state_remove_observer().

Referenced by stop_poll_thread(), and subscriptions_destroy().

303 {
305  (struct stasis_state_observer *)observer);
306 }
Managed stasis state event interface.
Definition: stasis_state.h:463
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).
Definition: stasis_state.c:700
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32

◆ ast_mwi_state_callback_all()

void ast_mwi_state_callback_all ( on_mwi_state  handler,
void *  data 
)

For each managed mailbox call the given handler.

Parameters
handlerThe mwi state handler to call for each managed mailbox
dataUser to data to pass on to the handler
Since
13.28.0
16.5.0

Definition at line 333 of file mwi.c.

References handle_mwi_state(), handler(), mwi_handler_data::handler, and stasis_state_callback_all().

Referenced by actual_load_config(), manager_voicemail_refresh(), publish(), and unload_module().

334 {
335  struct mwi_handler_data d = {
336  .handler = handler,
337  .data = data
338  };
339 
341 }
static struct test_val d
static int handle_mwi_state(const char *id, struct stasis_message *msg, void *user_data)
Definition: mwi.c:313
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
void stasis_state_callback_all(struct stasis_state_manager *manager, on_stasis_state handler, void *data)
For each managed state call the given handler.
Definition: stasis_state.c:740
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59
void * data
Definition: mwi.c:310

◆ ast_mwi_state_callback_subscribed()

void ast_mwi_state_callback_subscribed ( on_mwi_state  handler,
void *  data 
)

For each managed mailbox that has a subscriber call the given handler.

Parameters
handlerThe mwi state handler to call for each managed mailbox
dataUser to data to pass on to the handler
Since
13.28.0
16.5.0

Definition at line 343 of file mwi.c.

References handle_mwi_state(), handler(), mwi_handler_data::handler, and stasis_state_callback_subscribed().

Referenced by mb_poll_thread().

344 {
345  struct mwi_handler_data d = {
346  .handler = handler,
347  .data = data
348  };
349 
351 }
static struct test_val d
static int handle_mwi_state(const char *id, struct stasis_message *msg, void *user_data)
Definition: mwi.c:313
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
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.
Definition: stasis_state.c:763
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59
void * data
Definition: mwi.c:310

◆ ast_mwi_subscribe_pool()

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.

Adding a subscriber to a mailbox will create a stasis topic for the mailbox if one does not already exist. Once successfully create the underlying stasis topic is then subscribed to as well.

A subscriber can be removed by releasing its reference. Doing so releases its underlying reference to the MWI state. It does not unsubscribe from the topic. Unsubscribing from a topic should be done prior to unsubscribing the state.

Parameters
mailboxThe subscription state mailbox id
callbackThe stasis subscription callback
dataA user data object passed to the stasis subscription
Return values
AnMWI subscriber object
NULLon error
Since
13.28.0
16.5.0

Definition at line 230 of file mwi.c.

References ast_mwi_state_type(), ast_mwi_subscriber_subscription(), NULL, stasis_state_subscribe_pool(), stasis_subscription_accept_message_type(), STASIS_SUBSCRIPTION_FILTER_SELECTIVE, stasis_subscription_set_filter(), and sub.

Referenced by add_peer_mwi_subs(), build_gateway(), build_peer(), config_line(), mkintf(), mwi_stasis_subscription_alloc(), and subscriptions_create().

232 {
233  struct stasis_subscription *stasis_sub;
235  mwi_state_manager, mailbox, callback, data);
236 
237  if (!sub) {
238  return NULL;
239  }
240 
241  stasis_sub = ast_mwi_subscriber_subscription(sub);
242 
245 
246  return sub;
247 }
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.
Definition: stasis_state.c:446
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1079
#define NULL
Definition: resample.c:96
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
struct stasis_subscription * ast_mwi_subscriber_subscription(struct ast_mwi_subscriber *sub)
Retrieve the stasis MWI topic subscription if available.
Definition: mwi.c:272
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
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.
Definition: stasis.c:1025
struct stasis_forward * sub
Definition: res_corosync.c:240

◆ ast_mwi_subscriber_data()

struct ast_mwi_state* ast_mwi_subscriber_data ( struct ast_mwi_subscriber sub)

Retrieves the state data object associated with the MWI subscriber.

Note
Returned data's reference count is incremented
Parameters
subAn MWI subscriber
Return values
Thestate data object
Since
13.28.0
16.5.0

Definition at line 264 of file mwi.c.

References mwi_retrieve_then_create_state(), stasis_state_subscriber_data(), and stasis_state_subscriber_id().

Referenced by get_message_count(), handle_validate(), mwi_handle_subscribe(), and mwi_handle_unsubscribe().

265 {
266  struct stasis_state_subscriber *s = (struct stasis_state_subscriber *)sub;
267  struct ast_mwi_state *mwi_state = stasis_state_subscriber_data(s);
268 
270 }
static struct ast_mwi_state * mwi_retrieve_then_create_state(const char *mailbox)
Definition: mwi.c:138
void * stasis_state_subscriber_data(struct stasis_state_subscriber *sub)
Retrieve the last known state stasis message payload for the subscriber.
Definition: stasis_state.c:497
const char * stasis_state_subscriber_id(const struct stasis_state_subscriber *sub)
Retrieve the underlying subscribed to state's unique id.
Definition: stasis_state.c:487
The structure that contains MWI state.
Definition: mwi.h:457

◆ ast_mwi_subscriber_subscription()

struct stasis_subscription* ast_mwi_subscriber_subscription ( struct ast_mwi_subscriber sub)

Retrieve the stasis MWI topic subscription if available.

Parameters
subAn MWI subscriber
Return values
Thesubscriber's stasis subscription
NULLif no subscription available
Since
13.28.0
16.5.0

Definition at line 272 of file mwi.c.

References stasis_state_subscriber_subscription().

Referenced by add_peer_mwi_subs(), ast_mwi_subscribe_pool(), and mwi_stasis_subscription_alloc().

273 {
275 }
struct stasis_subscription * stasis_state_subscriber_subscription(struct stasis_state_subscriber *sub)
Retrieve the stasis topic subscription if available.
Definition: stasis_state.c:513

◆ ast_mwi_subscriber_topic()

struct stasis_topic* ast_mwi_subscriber_topic ( struct ast_mwi_subscriber sub)

Retrieves the MWI subscriber's topic.

Note
Returned topic's reference count is NOT incremented. However, the topic is guaranteed to live for the lifetime of the subscriber.
Parameters
subAn MWI subscriber
Return values
Astasis topic subscribed to by the subscriber
Since
13.28.0
16.5.0

Definition at line 259 of file mwi.c.

References stasis_state_subscriber_topic().

260 {
262 }
struct stasis_topic * stasis_state_subscriber_topic(struct stasis_state_subscriber *sub)
Retrieve the subscriber's topic.
Definition: stasis_state.c:492

◆ ast_mwi_unsubscribe()

void* ast_mwi_unsubscribe ( struct ast_mwi_subscriber sub)

Unsubscribe from the stasis topic and MWI.

Parameters
subAn MWI subscriber
Return values
NULL
Since
13.28.0
16.5.0

Definition at line 249 of file mwi.c.

References stasis_state_unsubscribe().

Referenced by destroy_dahdi_pvt(), destroy_endpoint(), peer_destructor(), and skinny_reload().

250 {
252 }
void * stasis_state_unsubscribe(struct stasis_state_subscriber *sub)
Unsubscribe from the stasis topic and stasis state.
Definition: stasis_state.c:470

◆ ast_mwi_unsubscribe_and_join()

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 from MWI.

Parameters
subAn MWI subscriber
Return values
NULL
Since
13.28.0
16.5.0

Definition at line 254 of file mwi.c.

References stasis_state_unsubscribe_and_join().

Referenced by destroy_mailbox(), subscriptions_destroy(), unload_module(), and unsubscribe_stasis().

255 {
257 }
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...
Definition: stasis_state.c:477

◆ ast_publish_mwi_state_full()

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.

Since
12
Parameters
[in]mailboxThe mailbox identifier string.
[in]contextThe context this mailbox resides in (NULL or "" if only using mailbox)
[in]new_msgsThe number of new messages in this mailbox
[in]old_msgsThe number of old messages in this mailbox
[in]channel_idA unique identifier for a channel associated with this change in mailbox state
[in]eidThe EID of the server that originally published the message
Return values
0Success
-1Failure

Definition at line 388 of file mwi.c.

References ast_mwi_publish_by_mailbox().

Referenced by asterisk_publication_mailboxstate(), publish_mwi_to_stasis(), and xmpp_pubsub_handle_event().

395 {
396  return ast_mwi_publish_by_mailbox(mailbox, context, 0, new_msgs, old_msgs, channel_id, eid);
397 }
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
int old_msgs
Definition: mwi.h:462
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.
Definition: mwi.c:370
int new_msgs
Definition: mwi.h:461
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ handle_mwi_state()

static int handle_mwi_state ( const char *  id,
struct stasis_message msg,
void *  user_data 
)
static

Definition at line 313 of file mwi.c.

References ao2_ref, d, mwi_handler_data::data, mwi_handler_data::handler, mwi_create_state(), NULL, and stasis_message_data().

Referenced by ast_mwi_state_callback_all(), and ast_mwi_state_callback_subscribed().

314 {
315  struct mwi_handler_data *d = user_data;
316  struct ast_mwi_state *mwi_state = stasis_message_data(msg);
317  int res;
318 
319  if (mwi_state) {
320  return d->handler(mwi_state, d->data);
321  }
322 
323  mwi_state = mwi_create_state(id, NULL, 0, 0, 0);
324  if (!mwi_state) {
325  return 0;
326  }
327 
328  res = d->handler(mwi_state, d->data);
329  ao2_ref(mwi_state, -1);
330  return res;
331 }
static struct test_val d
on_mwi_state handler
Definition: mwi.c:309
static struct ast_mwi_state * mwi_create_state(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs)
Definition: mwi.c:105
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
The structure that contains MWI state.
Definition: mwi.h:457
void * data
Definition: mwi.c:310

◆ mwi_blob_dtor()

static void mwi_blob_dtor ( void *  obj)
static

Definition at line 454 of file mwi.c.

References ao2_cleanup, ast_json_unref(), ast_mwi_blob::blob, and ast_mwi_blob::mwi_state.

Referenced by ast_mwi_blob_create().

455 {
456  struct ast_mwi_blob *mwi_blob = obj;
457 
458  ao2_cleanup(mwi_blob->mwi_state);
459  ast_json_unref(mwi_blob->blob);
460 }
struct ast_mwi_state * mwi_state
Definition: mwi.h:474
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * blob
Definition: mwi.h:475
Object that represents an MWI update with some additional application defined data.
Definition: mwi.h:473
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ mwi_cleanup()

static void mwi_cleanup ( void  )
static

Definition at line 491 of file mwi.c.

References ao2_cleanup, ast_mwi_state_type(), ast_mwi_vm_app_type(), NULL, stasis_caching_unsubscribe_and_join(), and STASIS_MESSAGE_TYPE_CLEANUP.

Referenced by mwi_init().

492 {
500 }
static struct stasis_cache * mwi_state_cache
Definition: mwi.c:33
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
#define NULL
Definition: resample.c:96
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...
Definition: stasis_cache.c:146
static struct stasis_state_manager * mwi_state_manager
Definition: mwi.c:32
static struct stasis_caching_topic * mwi_topic_cached
Definition: mwi.c:34
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct stasis_message_type * ast_mwi_vm_app_type(void)
Get the Stasis Message Bus API message type for voicemail application specific messages.

◆ mwi_create_state()

static struct ast_mwi_state* mwi_create_state ( const char *  mailbox,
const char *  context,
int  urgent_msgs,
int  new_msgs,
int  old_msgs 
)
static

Definition at line 105 of file mwi.c.

References ao2_alloc, ao2_ref, ast_assert, ast_log, ast_string_field_build, ast_string_field_init, ast_string_field_set, ast_strlen_zero, LOG_ERROR, mwi_state_dtor(), ast_mwi_state::new_msgs, NULL, ast_mwi_state::old_msgs, ast_mwi_state::uniqueid, and ast_mwi_state::urgent_msgs.

Referenced by ast_mwi_create(), handle_mwi_state(), mwi_retrieve_then_create_state(), and mwi_state_create_message().

107 {
108  struct ast_mwi_state *mwi_state;
109 
111 
112  mwi_state = ao2_alloc(sizeof(*mwi_state), mwi_state_dtor);
113  if (!mwi_state) {
114  ast_log(LOG_ERROR, "Unable to create MWI state for mailbox '%s@%s'\n",
116  return NULL;
117  }
118 
119  if (ast_string_field_init(mwi_state, 256)) {
120  ast_log(LOG_ERROR, "Unable to initialize MWI state for mailbox '%s@%s'\n",
122  ao2_ref(mwi_state, -1);
123  return NULL;
124  }
125  if (!ast_strlen_zero(context)) {
126  ast_string_field_build(mwi_state, uniqueid, "%s@%s", mailbox, context);
127  } else {
129  }
130 
131  mwi_state->urgent_msgs = urgent_msgs;
132  mwi_state->new_msgs = new_msgs;
133  mwi_state->old_msgs = old_msgs;
134 
135  return mwi_state;
136 }
static void mwi_state_dtor(void *obj)
Definition: mwi.c:77
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
#define ast_log
Definition: astobj2.c:42
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define ao2_ref(o, delta)
Definition: astobj2.h:464
int old_msgs
Definition: mwi.h:462
#define LOG_ERROR
Definition: logger.h:285
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
int urgent_msgs
Definition: mwi.h:466
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:550
int new_msgs
Definition: mwi.h:461
const ast_string_field uniqueid
Definition: mwi.h:460
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
The structure that contains MWI state.
Definition: mwi.h:457
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ mwi_retrieve_then_create_state()

static struct ast_mwi_state* mwi_retrieve_then_create_state ( const char *  mailbox)
static

Definition at line 138 of file mwi.c.

References ast_app_inboxcount2(), mwi_create_state(), ast_mwi_state::new_msgs, NULL, ast_mwi_state::old_msgs, and ast_mwi_state::urgent_msgs.

Referenced by ast_mwi_subscriber_data().

139 {
140  int urgent_msgs;
141  int new_msgs;
142  int old_msgs;
143 
144  ast_app_inboxcount2(mailbox, &urgent_msgs, &new_msgs, &old_msgs);
145  return mwi_create_state(mailbox, NULL, urgent_msgs, new_msgs, old_msgs);
146 }
static struct ast_mwi_state * mwi_create_state(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs)
Definition: mwi.c:105
#define NULL
Definition: resample.c:96
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
int ast_app_inboxcount2(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs)
Determine number of urgent/new/old messages in a mailbox.
Definition: main/app.c:692
int old_msgs
Definition: mwi.h:462
int urgent_msgs
Definition: mwi.h:466
int new_msgs
Definition: mwi.h:461

◆ mwi_state_create_message()

static struct stasis_message* mwi_state_create_message ( const char *  mailbox,
const char *  context,
int  urgent_msgs,
int  new_msgs,
int  old_msgs,
const char *  channel_id,
struct ast_eid eid 
)
static

Definition at line 169 of file mwi.c.

References ao2_cleanup, ast_channel_snapshot_get_latest(), ast_eid_default, ast_mwi_state_type(), ast_strlen_zero, stasis_message::eid, ast_mwi_state::eid, mwi_create_state(), NULL, ast_mwi_state::snapshot, and stasis_message_create_full().

Referenced by ast_delete_mwi_state_full(), ast_mwi_publish(), and ast_mwi_publish_by_mailbox().

177 {
178  struct ast_mwi_state *mwi_state;
179  struct stasis_message *message;
180 
181  if (!ast_mwi_state_type()) {
182  return NULL;
183  }
184 
185  mwi_state = mwi_create_state(mailbox, context, urgent_msgs, new_msgs, old_msgs);
186  if (!mwi_state) {
187  return NULL;
188  }
189 
190  if (!ast_strlen_zero(channel_id)) {
191  mwi_state->snapshot = ast_channel_snapshot_get_latest(channel_id);
192  }
193 
194  if (eid) {
195  mwi_state->eid = *eid;
196  } else {
197  mwi_state->eid = ast_eid_default;
198  }
199 
200  /*
201  * XXX As far as stasis is concerned, all MWI events are local.
202  *
203  * We may in the future want to make MWI aggregate local/remote
204  * message counts similar to how device state aggregates state.
205  */
207  ao2_cleanup(mwi_state);
208  return message;
209 }
struct ast_eid eid
static struct ast_mwi_state * mwi_create_state(const char *mailbox, const char *context, int urgent_msgs, int new_msgs, int old_msgs)
Definition: mwi.c:105
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
struct ast_channel_snapshot * snapshot
Definition: mwi.h:464
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
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...
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
The structure that contains MWI state.
Definition: mwi.h:457
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 eid
Definition: mwi.h:465

◆ mwi_state_dtor()

static void mwi_state_dtor ( void *  obj)
static

Definition at line 77 of file mwi.c.

References ao2_cleanup, ast_string_field_free_memory, NULL, and ast_mwi_state::snapshot.

Referenced by mwi_create_state().

78 {
79  struct ast_mwi_state *mwi_state = obj;
81  ao2_cleanup(mwi_state->snapshot);
82  mwi_state->snapshot = NULL;
83 }
#define NULL
Definition: resample.c:96
struct ast_channel_snapshot * snapshot
Definition: mwi.h:464
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
The structure that contains MWI state.
Definition: mwi.h:457
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368

◆ mwi_state_get_id()

static const char* mwi_state_get_id ( struct stasis_message message)
static

Definition at line 441 of file mwi.c.

References ast_mwi_state_type(), NULL, stasis_message_data(), stasis_message_type(), stasis_subscription_change_type(), ast_mwi_state::uniqueid, and stasis_subscription_change::uniqueid.

Referenced by mwi_init().

442 {
443  if (ast_mwi_state_type() == stasis_message_type(message)) {
444  struct ast_mwi_state *mwi_state = stasis_message_data(message);
445  return mwi_state->uniqueid;
446  } else if (stasis_subscription_change_type() == stasis_message_type(message)) {
447  struct stasis_subscription_change *change = stasis_message_data(message);
448  return change->uniqueid;
449  }
450 
451  return NULL;
452 }
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
#define NULL
Definition: resample.c:96
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
Holds details about changes to subscriptions for the specified topic.
Definition: stasis.h:892
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type for MWI messages.
const ast_string_field uniqueid
Definition: mwi.h:460
struct stasis_message_type * stasis_subscription_change_type(void)
Gets the message type for subscription change notices.
The structure that contains MWI state.
Definition: mwi.h:457

◆ mwi_to_event()

static struct ast_event* mwi_to_event ( struct stasis_message message)
static

Convert a MWI stasis_message to a ast_event.

Definition at line 38 of file mwi.c.

References AST_EVENT_IE_CONTEXT, AST_EVENT_IE_EID, AST_EVENT_IE_END, AST_EVENT_IE_MAILBOX, AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_RAW, AST_EVENT_IE_PLTYPE_STR, AST_EVENT_IE_PLTYPE_UINT, AST_EVENT_MWI, ast_event_new(), ast_mwi_state_type(), ast_mwi_vm_app_type(), ast_strdupa, ast_strlen_zero, context, ast_mwi_state::eid, mailbox, ast_mwi_state::new_msgs, NULL, ast_mwi_state::old_msgs, stasis_message_data(), STASIS_MESSAGE_TYPE_DEFN(), strsep(), and ast_mwi_state::uniqueid.

39 {
40  struct ast_event *event;
41  struct ast_mwi_state *mwi_state;
42  char *mailbox;
43  char *context;
44 
45  if (!message) {
46  return NULL;
47  }
48 
49  mwi_state = stasis_message_data(message);
50 
51  /* Strip off @context */
52  context = mailbox = ast_strdupa(mwi_state->uniqueid);
53  strsep(&context, "@");
54  if (ast_strlen_zero(context)) {
55  context = "default";
56  }
57 
63  AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, &mwi_state->eid, sizeof(mwi_state->eid),
65 
66  return event;
67 }
An event.
Definition: event.c:81
Definition: astman.c:222
Number of new messages Used by: AST_EVENT_MWI Payload type: UINT.
Definition: event_defs.h:77
Number of Used by: AST_EVENT_MWI Payload type: UINT.
Definition: event_defs.h:83
#define NULL
Definition: resample.c:96
Entity ID Used by All events Payload type: RAW This IE indicates which server the event originated fr...
Definition: event_defs.h:272
#define ast_strlen_zero(foo)
Definition: strings.h:52
static char mailbox[AST_MAX_MAILBOX_UNIQUEID]
Definition: chan_mgcp.c:204
Context IE Used by AST_EVENT_MWI Payload type: str.
Definition: event_defs.h:127
int old_msgs
Definition: mwi.h:462
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
char * strsep(char **str, const char *delims)
struct ast_event * ast_event_new(enum ast_event_type event_type,...)
Create a new event.
Definition: event.c:402
int new_msgs
Definition: mwi.h:461
const ast_string_field uniqueid
Definition: mwi.h:460
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
The structure that contains MWI state.
Definition: mwi.h:457
struct ast_eid eid
Definition: mwi.h:465
Mailbox name.
Definition: event_defs.h:89

◆ STASIS_MESSAGE_TYPE_DEFN() [1/2]

STASIS_MESSAGE_TYPE_DEFN ( ast_mwi_state_type  ,
to_event = mwi_to_event 
)

Referenced by mwi_to_event().

◆ STASIS_MESSAGE_TYPE_DEFN() [2/2]

STASIS_MESSAGE_TYPE_DEFN ( ast_mwi_vm_app_type  )

Variable Documentation

◆ mwi_state_cache

struct stasis_cache* mwi_state_cache
static

Definition at line 33 of file mwi.c.

Referenced by ast_mwi_state_cache().

◆ mwi_state_manager

struct stasis_state_manager* mwi_state_manager
static

Definition at line 32 of file mwi.c.

◆ mwi_topic_cached

struct stasis_caching_topic* mwi_topic_cached
static

Definition at line 34 of file mwi.c.