Debian Codegraph @

res/res_mwi_devstate.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Schmooze Com, Inc.
5 *
6 * Jason Parker <[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/mwi.h"
26 #include "asterisk/devicestate.h"
27 #include "asterisk/module.h"
28 #include "asterisk/stasis_message_router.h"
29 #include "asterisk/stasis.h"
30
31 static struct stasis_subscription *mwi_sub;
32
33 static void mwi_update_cb(void *data, struct stasis_subscription *sub,
34 struct stasis_message *msg)
35 {
36 struct ast_mwi_state *mwi_state;
37
38 if (ast_mwi_state_type() != stasis_message_type(msg)) {
39 return;
40 }
41
42 mwi_state = stasis_message_data(msg);
43 if (!mwi_state) {
44 return;
45 }
46
47 if (mwi_state->new_msgs > 0) {
48 ast_debug(1, "Sending inuse devstate change for MWI:%s\n", mwi_state->uniqueid);
49 ast_devstate_changed(AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, "MWI:%s", mwi_state->uniqueid);
50 } else {
51 ast_debug(1, "Sending not inuse devstate change for MWI:%s\n", mwi_state->uniqueid);
52 ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "MWI:%s", mwi_state->uniqueid);
53 }
54 }
55
56 static int mwi_cached_cb(void *obj, void *arg, int flags)
57 {
58 struct stasis_message *msg = obj;
59 mwi_update_cb(NULL, mwi_sub, msg);
60
61 return 0;
62 }
63
64 static int unload_module(void)
65 {
66 mwi_sub = stasis_unsubscribe(mwi_sub);
67
68 return 0;
69 }
70
71 static int load_module(void)
72 {
73 struct ao2_container *cached;
74
75 if (!(mwi_sub = stasis_subscribe(ast_mwi_topic_all(), mwi_update_cb, NULL))) {
76 return AST_MODULE_LOAD_DECLINE;
77 }
78
79 if (stasis_subscription_accept_message_type(mwi_sub, ast_mwi_state_type())) {
80 unload_module();
81 return AST_MODULE_LOAD_DECLINE;
82 }
83
84 if (stasis_subscription_set_filter(mwi_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE)) {
85 unload_module();
86 return AST_MODULE_LOAD_DECLINE;
87 }
88
89 cached = stasis_cache_dump(ast_mwi_state_cache(), NULL);
90 if (!cached) {
91 unload_module();
92 return AST_MODULE_LOAD_DECLINE;
93 }
94 ao2_callback(cached, OBJ_NODATA, mwi_cached_cb, NULL);
95 ao2_ref(cached, -1);
96
97 return AST_MODULE_LOAD_SUCCESS;
98 }
99
100 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "MWI Device State Subscriptions",
101 .support_level = AST_MODULE_SUPPORT_CORE,
102 .load = load_module,
103 .unload = unload_module,
104 .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
105 );