Debian Codegraph @

main/mwi.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2019, Sangoma Technologies Corporation
5 *
6 * Kevin Harwell <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*** MODULEINFO
20 <support_level>core</support_level>
21 ***/
22
23 #include "asterisk.h"
24
25 #include "asterisk/app.h"
26 #include "asterisk/mwi.h"
27 #include "asterisk/stasis_channels.h"
28
29 /*!
30 * \brief Define \ref stasis topic objects
31 * @{
32 */
33 static struct stasis_state_manager *mwi_state_manager;
34 static struct stasis_cache *mwi_state_cache;
35 static struct stasis_caching_topic *mwi_topic_cached;
36
37 /*! @} */
38
39 /*! \brief Convert a MWI \ref stasis_message to a \ref ast_event */
40 static struct ast_event *mwi_to_event(struct stasis_message *message)
41 {
42 struct ast_event *event;
43 struct ast_mwi_state *mwi_state;
44 char *mailbox;
45 char *context;
46
47 if (!message) {
48 return NULL;
49 }
50
51 mwi_state = stasis_message_data(message);
52
53 /* Strip off @context */
54 context = mailbox = ast_strdupa(mwi_state->uniqueid);
55 strsep(&context, "@");
56 if (ast_strlen_zero(context)) {
57 context = "default";
58 }
59
60 event = ast_event_new(AST_EVENT_MWI,
61 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
62 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
63 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, mwi_state->new_msgs,
64 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, mwi_state->old_msgs,
65 AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, &mwi_state->eid, sizeof(mwi_state->eid),
66 AST_EVENT_IE_END);
67
68 return event;
69 }
70
71 /*!
72 * \brief Define \ref stasis message types for MWI
73 * @{
74 */
75 STASIS_MESSAGE_TYPE_DEFN(ast_mwi_state_type,
76 .to_event = mwi_to_event, );
77 STASIS_MESSAGE_TYPE_DEFN(ast_mwi_vm_app_type);
78
79 /*! @} */
80
81 static void mwi_state_dtor(void *obj)
82 {
83 struct ast_mwi_state *mwi_state = obj;
84 ast_string_field_free_memory(mwi_state);
85 ao2_cleanup(mwi_state->snapshot);
86 mwi_state->snapshot = NULL;
87 }
88
89 struct stasis_topic *ast_mwi_topic_all(void)
90 {
91 return stasis_state_all_topic(mwi_state_manager);
92 }
93
94 struct stasis_cache *ast_mwi_state_cache(void)
95 {
96 return mwi_state_cache;
97 }
98
99 struct stasis_topic *ast_mwi_topic_cached(void)
100 {
101 return stasis_caching_get_topic(mwi_topic_cached);
102 }
103
104 struct stasis_topic *ast_mwi_topic(const char *uniqueid)
105 {
106 return stasis_state_topic(mwi_state_manager, uniqueid);
107 }
108
109 static struct ast_mwi_state *mwi_create_state(const char *mailbox, const char *context,
110 int urgent_msgs, int new_msgs, int old_msgs)
111 {
112 struct ast_mwi_state *mwi_state;
113
114 ast_assert(!ast_strlen_zero(mailbox));
115
116 mwi_state = ao2_alloc(sizeof(*mwi_state), mwi_state_dtor);
117 if (!mwi_state) {
118 ast_log(LOG_ERROR, "Unable to create MWI state for mailbox '%s@%s'\n",
119 mailbox, ast_strlen_zero(context) ? "" : context);
120 return NULL;
121 }
122
123 if (ast_string_field_init(mwi_state, 256)) {
124 ast_log(LOG_ERROR, "Unable to initialize MWI state for mailbox '%s@%s'\n",
125 mailbox, ast_strlen_zero(context) ? "" : context);
126 ao2_ref(mwi_state, -1);
127 return NULL;
128 }
129 if (!ast_strlen_zero(context)) {
130 ast_string_field_build(mwi_state, uniqueid, "%s@%s", mailbox, context);
131 } else {
132 ast_string_field_set(mwi_state, uniqueid, mailbox);
133 }
134
135 mwi_state->urgent_msgs = urgent_msgs;
136 mwi_state->new_msgs = new_msgs;
137 mwi_state->old_msgs = old_msgs;
138
139 return mwi_state;
140 }
141
142 static struct ast_mwi_state *mwi_retrieve_then_create_state(const char *mailbox)
143 {
144 int urgent_msgs;
145 int new_msgs;
146 int old_msgs;
147
148 ast_app_inboxcount2(mailbox, &urgent_msgs, &new_msgs, &old_msgs);
149 return mwi_create_state(mailbox, NULL, urgent_msgs, new_msgs, old_msgs);
150 }
151
152 struct ast_mwi_state *ast_mwi_create(const char *mailbox, const char *context)
153 {
154 return mwi_create_state(mailbox, context, 0, 0, 0);
155 }
156
157 /*!
158 * \internal
159 * \brief Create a MWI state snapshot message.
160 * \since 12.2.0
161 *
162 * \param[in] mailbox The mailbox identifier string.
163 * \param[in] context The context this mailbox resides in (NULL or "" if only using mailbox)
164 * \param urgent_msgs
165 * \param[in] new_msgs The number of new messages in this mailbox
166 * \param[in] old_msgs The number of old messages in this mailbox
167 * \param[in] channel_id A unique identifier for a channel associated with this
168 * change in mailbox state
169 * \param[in] eid The EID of the server that originally published the message
170 *
171 * \return message on success. Use ao2_cleanup() when done with it.
172 * \retval NULL on error.
173 */
174 static struct stasis_message *mwi_state_create_message(
175 const char *mailbox,
176 const char *context,
177 int urgent_msgs,
178 int new_msgs,
179 int old_msgs,
180 const char *channel_id,
181 struct ast_eid *eid)
182 {
183 struct ast_mwi_state *mwi_state;
184 struct stasis_message *message;
185
186 if (!ast_mwi_state_type()) {
187 return NULL;
188 }
189
190 mwi_state = mwi_create_state(mailbox, context, urgent_msgs, new_msgs, old_msgs);
191 if (!mwi_state) {
192 return NULL;
193 }
194
195 if (!ast_strlen_zero(channel_id)) {
196 mwi_state->snapshot = ast_channel_snapshot_get_latest(channel_id);
197 }
198
199 if (eid) {
200 mwi_state->eid = *eid;
201 } else {
202 mwi_state->eid = ast_eid_default;
203 }
204
205 /*
206 * XXX As far as stasis is concerned, all MWI events are local.
207 *
208 * We may in the future want to make MWI aggregate local/remote
209 * message counts similar to how device state aggregates state.
210 */
211 message = stasis_message_create_full(ast_mwi_state_type(), mwi_state, &ast_eid_default);
212 ao2_cleanup(mwi_state);
213 return message;
214 }
215
216 /*!
217 * \internal
218 *
219 * This object currently acts as a typedef, but can also be thought of as a "child" object
220 * of the stasis_state_subscriber type. As such the "base" pointer should always be the
221 * first object attribute. Doing so allows this object to be easily type cast and used by
222 * the stasis_state code.
223 */
224 struct ast_mwi_subscriber {
225 /*! The "base" state subscriber. (Must be first object attribute) */
226 struct stasis_state_subscriber *base;
227 };
228
229 struct ast_mwi_subscriber *ast_mwi_add_subscriber(const char *mailbox)
230 {
231 return (struct ast_mwi_subscriber *)stasis_state_add_subscriber(
232 mwi_state_manager, mailbox);
233 }
234
235 struct ast_mwi_subscriber *ast_mwi_subscribe_pool(const char *mailbox,
236 stasis_subscription_cb callback, void *data)
237 {
238 struct stasis_subscription *stasis_sub;
239 struct ast_mwi_subscriber *sub = (struct ast_mwi_subscriber *)stasis_state_subscribe_pool(
240 mwi_state_manager, mailbox, callback, data);
241
242 if (!sub) {
243 return NULL;
244 }
245
246 stasis_sub = ast_mwi_subscriber_subscription(sub);
247
248 stasis_subscription_accept_message_type(stasis_sub, ast_mwi_state_type());
249 stasis_subscription_set_filter(stasis_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
250
251 return sub;
252 }
253
254 void *ast_mwi_unsubscribe(struct ast_mwi_subscriber *sub)
255 {
256 return stasis_state_unsubscribe((struct stasis_state_subscriber *)sub);
257 }
258
259 void *ast_mwi_unsubscribe_and_join(struct ast_mwi_subscriber *sub)
260 {
261 return stasis_state_unsubscribe_and_join((struct stasis_state_subscriber *)sub);
262 }
263
264 struct stasis_topic *ast_mwi_subscriber_topic(struct ast_mwi_subscriber *sub)
265 {
266 return stasis_state_subscriber_topic((struct stasis_state_subscriber *)sub);
267 }
268
269 struct ast_mwi_state *ast_mwi_subscriber_data(struct ast_mwi_subscriber *sub)
270 {
271 struct stasis_state_subscriber *s = (struct stasis_state_subscriber *)sub;
272 struct ast_mwi_state *mwi_state = stasis_state_subscriber_data(s);
273
274 return mwi_state ?: mwi_retrieve_then_create_state(stasis_state_subscriber_id(s));
275 }
276
277 struct stasis_subscription *ast_mwi_subscriber_subscription(struct ast_mwi_subscriber *sub)
278 {
279 return stasis_state_subscriber_subscription((struct stasis_state_subscriber *)sub);
280 }
281
282 /*!
283 * \internal
284 *
285 * This object currently acts as a typedef, but can also be thought of as a "child" object
286 * of the stasis_state_publisher type. As such the "base" pointer should always be the
287 * first object attribute. Doing so allows this object to be easily type cast and used by
288 * the stasis_state code.
289 */
290 struct ast_mwi_publisher {
291 /*! The "base" state publisher. (Must be first object attribute) */
292 struct stasis_state_publisher *base;
293 };
294
295 struct ast_mwi_publisher *ast_mwi_add_publisher(const char *mailbox)
296 {
297 return (struct ast_mwi_publisher *)stasis_state_add_publisher(
298 mwi_state_manager, mailbox);
299 }
300
301 int ast_mwi_add_observer(struct ast_mwi_observer *observer)
302 {
303 return stasis_state_add_observer(mwi_state_manager,
304 (struct stasis_state_observer *)observer);
305 }
306
307 void ast_mwi_remove_observer(struct ast_mwi_observer *observer)
308 {
309 stasis_state_remove_observer(mwi_state_manager,
310 (struct stasis_state_observer *)observer);
311 }
312
313 struct mwi_handler_data {
314 on_mwi_state handler;
315 void *data;
316 };
317
318 static int handle_mwi_state(const char *id, struct stasis_message *msg, void *user_data)
319 {
320 struct mwi_handler_data *d = user_data;
321 struct ast_mwi_state *mwi_state = stasis_message_data(msg);
322 int res;
323
324 if (mwi_state) {
325 return d->handler(mwi_state, d->data);
326 }
327
328 mwi_state = mwi_create_state(id, NULL, 0, 0, 0);
329 if (!mwi_state) {
330 return 0;
331 }
332
333 res = d->handler(mwi_state, d->data);
334 ao2_ref(mwi_state, -1);
335 return res;
336 }
337
338 void ast_mwi_state_callback_all(on_mwi_state handler, void *data)
339 {
340 struct mwi_handler_data d = {
341 .handler = handler,
342 .data = data
343 };
344
345 stasis_state_callback_all(mwi_state_manager, handle_mwi_state, &d);
346 }
347
348 void ast_mwi_state_callback_subscribed(on_mwi_state handler, void *data)
349 {
350 struct mwi_handler_data d = {
351 .handler = handler,
352 .data = data
353 };
354
355 stasis_state_callback_subscribed(mwi_state_manager, handle_mwi_state, &d);
356 }
357
358 int ast_mwi_publish(struct ast_mwi_publisher *pub, int urgent_msgs,
359 int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
360 {
361 struct stasis_state_publisher *p = (struct stasis_state_publisher *)pub;
362 struct stasis_message *msg = mwi_state_create_message(stasis_state_publisher_id(p),
363 NULL, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
364
365 if (!msg) {
366 return -1;
367 }
368
369 stasis_state_publish(p, msg);
370 ao2_ref(msg, -1);
371
372 return 0;
373 }
374
375 int ast_mwi_publish_by_mailbox(const char *mailbox, const char *context, int urgent_msgs,
376 int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
377 {
378 struct ast_mwi_state *mwi_state;
379 struct stasis_message *msg = mwi_state_create_message(
380 mailbox, context, urgent_msgs, new_msgs, old_msgs, channel_id, eid);
381
382 if (!msg) {
383 return -1;
384 }
385
386 mwi_state = stasis_message_data(msg);
387 stasis_state_publish_by_id(mwi_state_manager, mwi_state->uniqueid, NULL, msg);
388 ao2_ref(msg, -1);
389
390 return 0;
391 }
392
393 int ast_publish_mwi_state_full(
394 const char *mailbox,
395 const char *context,
396 int new_msgs,
397 int old_msgs,
398 const char *channel_id,
399 struct ast_eid *eid)
400 {
401 return ast_mwi_publish_by_mailbox(mailbox, context, 0, new_msgs, old_msgs, channel_id, eid);
402 }
403
404 int ast_delete_mwi_state_full(const char *mailbox, const char *context, struct ast_eid *eid)
405 {
406 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
407 struct stasis_message *cached_msg;
408 struct stasis_message *clear_msg;
409 struct ast_mwi_state *mwi_state;
410
411 msg = mwi_state_create_message(mailbox, context, 0, 0, 0, NULL, eid);
412 if (!msg) {
413 return -1;
414 }
415
416 mwi_state = stasis_message_data(msg);
417
418 /*
419 * XXX As far as stasis is concerned, all MWI events are local.
420 *
421 * For now, it is assumed that there is only one entity
422 * maintaining the state of a particular mailbox.
423 *
424 * If we ever have multiple MWI event entities maintaining
425 * the same mailbox that wish to delete their cached entry
426 * we will need to do something about the race condition
427 * potential between checking the cache and removing the
428 * cache entry.
429 */
430 cached_msg = stasis_cache_get_by_eid(ast_mwi_state_cache(),
431 ast_mwi_state_type(), mwi_state->uniqueid, &ast_eid_default);
432 if (!cached_msg) {
433 /* Nothing to clear from the cache, but still need to remove state */
434 stasis_state_remove_publish_by_id(mwi_state_manager, mwi_state->uniqueid, eid, NULL);
435 return -1;
436 }
437 ao2_cleanup(cached_msg);
438
439 clear_msg = stasis_cache_clear_create(msg);
440 stasis_state_remove_publish_by_id(mwi_state_manager, mwi_state->uniqueid, eid, clear_msg);
441 ao2_cleanup(clear_msg);
442
443 return 0;
444 }
445
446 static const char *mwi_state_get_id(struct stasis_message *message)
447 {
448 if (ast_mwi_state_type() == stasis_message_type(message)) {
449 struct ast_mwi_state *mwi_state = stasis_message_data(message);
450 return mwi_state->uniqueid;
451 } else if (stasis_subscription_change_type() == stasis_message_type(message)) {
452 struct stasis_subscription_change *change = stasis_message_data(message);
453 return change->uniqueid;
454 }
455
456 return NULL;
457 }
458
459 static void mwi_blob_dtor(void *obj)
460 {
461 struct ast_mwi_blob *mwi_blob = obj;
462
463 ao2_cleanup(mwi_blob->mwi_state);
464 ast_json_unref(mwi_blob->blob);
465 }
466
467 struct stasis_message *ast_mwi_blob_create(struct ast_mwi_state *mwi_state,
468 struct stasis_message_type *message_type,
469 struct ast_json *blob)
470 {
471 struct ast_mwi_blob *obj;
472 struct stasis_message *msg;
473
474 ast_assert(blob != NULL);
475
476 if (!message_type) {
477 return NULL;
478 }
479
480 obj = ao2_alloc(sizeof(*obj), mwi_blob_dtor);
481 if (!obj) {
482 return NULL;
483 }
484
485 obj->mwi_state = mwi_state;
486 ao2_ref(obj->mwi_state, +1);
487 obj->blob = ast_json_ref(blob);
488
489 /* This is not a normal MWI event. Only used by the MinivmNotify app. */
490 msg = stasis_message_create(message_type, obj);
491 ao2_ref(obj, -1);
492
493 return msg;
494 }
495
496 static void mwi_cleanup(void)
497 {
498 ao2_cleanup(mwi_state_cache);
499 mwi_state_cache = NULL;
500 mwi_topic_cached = stasis_caching_unsubscribe_and_join(mwi_topic_cached);
501 ao2_cleanup(mwi_state_manager);
502 mwi_state_manager = NULL;
503 STASIS_MESSAGE_TYPE_CLEANUP(ast_mwi_state_type);
504 STASIS_MESSAGE_TYPE_CLEANUP(ast_mwi_vm_app_type);
505 }
506
507 int mwi_init(void)
508 {
509 ast_register_cleanup(mwi_cleanup);
510
511 if (STASIS_MESSAGE_TYPE_INIT(ast_mwi_state_type) != 0) {
512 return -1;
513 }
514
515 if (STASIS_MESSAGE_TYPE_INIT(ast_mwi_vm_app_type) != 0) {
516 return -1;
517 }
518
519 mwi_state_manager = stasis_state_manager_create("mwi:all");
520 if (!mwi_state_manager) {
521 return -1;
522 }
523
524 mwi_state_cache = stasis_cache_create(mwi_state_get_id);
525 if (!mwi_state_cache) {
526 return -1;
527 }
528
529 mwi_topic_cached = stasis_caching_topic_create(ast_mwi_topic_all(), mwi_state_cache);
530 if (!mwi_topic_cached) {
531 return -1;
532 }
533
534 return 0;
535 }