Asterisk - The Open Source Telephony Project GIT-master-91e368c
Functions | Variables
manager_mwi.c File Reference

The Asterisk Management Interface - AMI (MWI event handling) More...

#include "asterisk.h"
#include "asterisk/manager.h"
#include "asterisk/app.h"
#include "asterisk/mwi.h"
#include "asterisk/channel.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/stasis.h"
Include dependency graph for manager_mwi.c:

Go to the source code of this file.


static int exclude_event_cb (const char *key)
 Callback function used by mwi_app_event_cb to weed out "Event" keys. More...
int manager_mwi_init (void)
 Initialize support for AMI MWI events. More...
static void manager_mwi_shutdown (void)
static void mwi_app_event_cb (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 Generic MWI event callback used for one-off events from voicemail modules. More...
static void mwi_update_cb (void *data, struct stasis_subscription *sub, struct stasis_message *message)


struct stasis_message_routermwi_state_router
static struct stasis_forwardtopic_forwarder
 The Stasis Message Bus API subscription returned by the forwarding of the MWI topic to the manager topic. More...

Detailed Description

The Asterisk Management Interface - AMI (MWI event handling)

Matt Jordan

Definition in file manager_mwi.c.

Function Documentation

◆ exclude_event_cb()

static int exclude_event_cb ( const char *  key)

Callback function used by mwi_app_event_cb to weed out "Event" keys.

Definition at line 46 of file manager_mwi.c.

48 if (!strcmp(key, "Event")) {
49 return -1;
50 }
51 return 0;

Referenced by mwi_app_event_cb().

◆ manager_mwi_init()

int manager_mwi_init ( void  )

Initialize support for AMI MWI events.

Return values
0on success
non-zeroon error

Definition at line 155 of file manager_mwi.c.

157 int ret = 0;
159 struct stasis_topic *mwi_topic;
160 struct stasis_message_router *message_router;
163 if (!manager_topic) {
164 return -1;
165 }
166 message_router = ast_manager_get_message_router();
167 if (!message_router) {
168 return -1;
169 }
170 mwi_topic = ast_mwi_topic_all();
171 if (!mwi_topic) {
172 return -1;
173 }
176 if (!topic_forwarder) {
177 return -1;
178 }
182 ret |= stasis_message_router_add(message_router,
185 NULL);
187 ret |= stasis_message_router_add(message_router,
190 NULL);
192 /* If somehow we failed to add any routes, just shut down the whole
193 * thing and fail it.
194 */
195 if (ret) {
197 return -1;
198 }
200 return 0;
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct stasis_message_router * ast_manager_get_message_router(void)
Get the stasis_message_router for AMI.
Definition: manager.c:1846
struct stasis_topic * ast_manager_get_topic(void)
Get the Stasis Message Bus API topic for AMI.
Definition: manager.c:1841
static struct stasis_topic * manager_topic
A stasis_topic that all topics AMI cares about will be forwarded to.
Definition: manager.c:1606
static void mwi_update_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: manager_mwi.c:86
static void mwi_app_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Generic MWI event callback used for one-off events from voicemail modules.
Definition: manager_mwi.c:55
static void manager_mwi_shutdown(void)
Definition: manager_mwi.c:149
static struct stasis_forward * topic_forwarder
The Stasis Message Bus API subscription returned by the forwarding of the MWI topic to the manager to...
Definition: manager_mwi.c:43
struct stasis_message_type * ast_mwi_state_type(void)
Get the Stasis Message Bus API message type 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.
struct stasis_topic * ast_mwi_topic_all(void)
Get the Stasis Message Bus API topic for MWI messages.
Definition: mwi.c:89
#define NULL
Definition: resample.c:96
struct stasis_forward * stasis_forward_all(struct stasis_topic *from_topic, struct stasis_topic *to_topic)
Create a subscription which forwards all messages from one topic to another.
Definition: stasis.c:1578
int stasis_message_router_add(struct stasis_message_router *router, struct stasis_message_type *message_type, stasis_subscription_cb callback, void *data)
Add a route to a message router.

References ast_manager_get_message_router(), ast_manager_get_topic(), ast_mwi_state_type(), ast_mwi_topic_all(), ast_mwi_vm_app_type(), ast_register_cleanup(), manager_mwi_shutdown(), manager_topic, mwi_app_event_cb(), mwi_update_cb(), NULL, stasis_forward_all(), stasis_message_router_add(), and topic_forwarder.

Referenced by subscribe_all().

◆ manager_mwi_shutdown()

static void manager_mwi_shutdown ( void  )

Definition at line 149 of file manager_mwi.c.

struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition: stasis.c:1548

References NULL, stasis_forward_cancel(), and topic_forwarder.

Referenced by manager_mwi_init().

◆ mwi_app_event_cb()

static void mwi_app_event_cb ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 

Generic MWI event callback used for one-off events from voicemail modules.

Definition at line 55 of file manager_mwi.c.

58 struct ast_mwi_blob *payload = stasis_message_data(message);
59 RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
60 RAII_VAR(struct ast_str *, event_buffer, NULL, ast_free);
61 struct ast_json *event_json = ast_json_object_get(payload->blob, "Event");
63 if (!event_json) {
64 return;
65 }
67 if (payload->mwi_state && payload->mwi_state->snapshot) {
68 channel_event_string = ast_manager_build_channel_state_string(payload->mwi_state->snapshot);
69 }
72 if (!event_buffer) {
73 ast_log(AST_LOG_WARNING, "Failed to create payload for event %s\n", ast_json_string_get(event_json));
74 return;
75 }
78 "Mailbox: %s\r\n"
79 "%s"
80 "%s",
81 payload->mwi_state ? payload->mwi_state->uniqueid : "Unknown",
82 ast_str_buffer(event_buffer),
83 channel_event_string ? ast_str_buffer(channel_event_string) : "");
#define ast_free(a)
Definition: astmm.h:180
#define ast_log
Definition: astobj2.c:42
struct ast_str * ast_manager_str_from_json_object(struct ast_json *blob, key_exclusion_cb exclusion_cb)
Convert a JSON object into an AMI compatible string.
Definition: manager.c:1942
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:253
struct ast_str * ast_manager_build_channel_state_string(const struct ast_channel_snapshot *snapshot)
Generate the AMI message body from a channel snapshot.
Definition: manager.h:76
static int exclude_event_cb(const char *key)
Callback function used by mwi_app_event_cb to weed out "Event" keys.
Definition: manager_mwi.c:46
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:758
Abstract JSON element (object, array, string, int, ...).
Object that represents an MWI update with some additional application defined data.
Definition: mwi.h:471
struct ast_mwi_state * mwi_state
Definition: mwi.h:472
struct ast_json * blob
Definition: mwi.h:473
struct ast_channel_snapshot * snapshot
Definition: mwi.h:462
const ast_string_field uniqueid
Definition: mwi.h:458
Support for dynamic strings.
Definition: strings.h:623
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ast_free, ast_json_object_get(), ast_json_string_get(), ast_log, AST_LOG_WARNING, ast_manager_build_channel_state_string(), ast_manager_str_from_json_object(), ast_str_buffer(), ast_mwi_blob::blob, EVENT_FLAG_CALL, exclude_event_cb(), manager_event, ast_mwi_blob::mwi_state, NULL, RAII_VAR, ast_mwi_state::snapshot, stasis_message_data(), and ast_mwi_state::uniqueid.

Referenced by manager_mwi_init().

◆ mwi_update_cb()

static void mwi_update_cb ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 

Definition at line 86 of file manager_mwi.c.

89 struct ast_mwi_state *mwi_state;
90 RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
93 return;
94 }
96 mwi_state = stasis_message_data(message);
97 if (!mwi_state) {
98 return;
99 }
101 if (mwi_state->snapshot) {
102 channel_event_string = ast_manager_build_channel_state_string(mwi_state->snapshot);
103 }
106 <managerEvent language="en_US" name="MessageWaiting">
107 <managerEventInstance class="EVENT_FLAG_CALL">
108 <synopsis>Raised when the state of messages in a voicemail mailbox
109 has changed or when a channel has finished interacting with a
110 mailbox.</synopsis>
111 <syntax>
112 <channel_snapshot/>
113 <parameter name="Mailbox">
114 <para>The mailbox with the new message, specified as <literal>mailbox</literal>@<literal>context</literal></para>
115 </parameter>
116 <parameter name="Waiting">
117 <para>Whether or not the mailbox has messages waiting for it.</para>
118 </parameter>
119 <parameter name="New">
120 <para>The number of new messages.</para>
121 </parameter>
122 <parameter name="Old">
123 <para>The number of old messages.</para>
124 </parameter>
125 </syntax>
126 <description>
127 <note><para>The Channel related parameters are only present if a
128 channel was involved in the manipulation of a mailbox. If no
129 channel is involved, the parameters are not included with the
130 event.</para>
131 </note>
132 </description>
133 </managerEventInstance>
134 </managerEvent>
135 ***/
136 manager_event(EVENT_FLAG_CALL, "MessageWaiting",
137 "%s"
138 "Mailbox: %s\r\n"
139 "Waiting: %d\r\n"
140 "New: %d\r\n"
141 "Old: %d\r\n",
142 AS_OR(channel_event_string, ""),
143 mwi_state->uniqueid,
145 mwi_state->new_msgs,
146 mwi_state->old_msgs);
int ast_app_has_voicemail(const char *mailboxes, const char *folder)
Determine if a given mailbox has any voicemail If folder is NULL, defaults to "INBOX"....
Definition: main/app.c:582
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
#define AS_OR(a, b)
Definition: strings.h:49
The structure that contains MWI state.
Definition: mwi.h:455
int old_msgs
Definition: mwi.h:460
int new_msgs
Definition: mwi.h:459

References AS_OR, ast_app_has_voicemail(), ast_free, ast_manager_build_channel_state_string(), ast_mwi_state_type(), EVENT_FLAG_CALL, manager_event, ast_mwi_state::new_msgs, NULL, ast_mwi_state::old_msgs, RAII_VAR, ast_mwi_state::snapshot, stasis_message_data(), stasis_message_type(), and ast_mwi_state::uniqueid.

Referenced by manager_mwi_init().

Variable Documentation

◆ mwi_state_router

struct stasis_message_router* mwi_state_router

Definition at line 35 of file manager_mwi.c.

◆ topic_forwarder

struct stasis_forward* topic_forwarder

The Stasis Message Bus API subscription returned by the forwarding of the MWI topic to the manager topic.

Definition at line 43 of file manager_mwi.c.

Referenced by manager_mwi_init(), and manager_mwi_shutdown().