Asterisk - The Open Source Telephony Project  21.4.1
res_mwi_devstate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Schmooze Com, Inc.
5  *
6  * Jason Parker <jason.parker@schmoozecom.com>
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 
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);
50  } else {
51  ast_debug(1, "Sending not inuse devstate change for MWI:%s\n", 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))) {
77  }
78 
80  unload_module();
82  }
83 
85  unload_module();
87  }
88 
89  cached = stasis_cache_dump(ast_mwi_state_cache(), NULL);
90  if (!cached) {
91  unload_module();
93  }
94  ao2_callback(cached, OBJ_NODATA, mwi_cached_cb, NULL);
95  ao2_ref(cached, -1);
96 
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 );
Asterisk main include file. File version handling, generic pbx functions.
Device state management.
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
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.
Definition: stasis.c:1077
struct ao2_container * stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
Dump cached items to a subscription for the ast_eid_default entity.
Definition: stasis_cache.c:736
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:510
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct stasis_cache * ast_mwi_state_cache(void)
Backend cache for ast_mwi_topic_cached().
Definition: mwi.c:94
#define ast_debug(level,...)
Log a DEBUG message.
int new_msgs
Definition: mwi.h:459
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 stasis_subscription * stasis_unsubscribe(struct stasis_subscription *subscription)
Cancel a subscription.
Definition: stasis.c:971
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
const ast_string_field uniqueid
Definition: mwi.h:458
Asterisk MWI API.
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:1023
Generic container type.
struct stasis_topic * ast_mwi_topic_all(void)
Get the Stasis Message Bus API topic for MWI messages.
Definition: mwi.c:89
The structure that contains MWI state.
Definition: mwi.h:455
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.