Asterisk - The Open Source Telephony Project  18.5.0
res_manager_presencestate.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Mark Michelson <[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 /*** DOCUMENTATION
24  <manager name="PresenceStateList" language="en_US">
25  <synopsis>
26  List the current known presence states.
27  </synopsis>
28  <syntax>
29  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
30  </syntax>
31  <description>
32  <para>This will list out all known presence states in a
33  sequence of <replaceable>PresenceStateChange</replaceable> events.
34  When finished, a <replaceable>PresenceStateListComplete</replaceable> event
35  will be emitted.</para>
36  </description>
37  <see-also>
38  <ref type="manager">PresenceState</ref>
39  <ref type="managerEvent">PresenceStatus</ref>
40  <ref type="function">PRESENCE_STATE</ref>
41  </see-also>
42  <responses>
43  <list-elements>
44  <xi:include xpointer="xpointer(/docs/managerEvent[@name='PresenceStateChange'])" />
45  </list-elements>
46  <managerEvent name="PresenceStateListComplete" language="en_US">
47  <managerEventInstance class="EVENT_FLAG_COMMAND">
48  <synopsis>
49  Indicates the end of the list the current known extension states.
50  </synopsis>
51  <syntax>
52  <parameter name="EventList">
53  <para>Conveys the status of the event list.</para>
54  </parameter>
55  <parameter name="ListItems">
56  <para>Conveys the number of statuses reported.</para>
57  </parameter>
58  </syntax>
59  </managerEventInstance>
60  </managerEvent>
61  </responses>
62  </manager>
63  ***/
64 
65 #include "asterisk.h"
66 #include "asterisk/module.h"
67 #include "asterisk/manager.h"
68 #include "asterisk/stasis.h"
69 #include "asterisk/presencestate.h"
70 
72 
73 static int action_presencestatelist(struct mansession *s, const struct message *m)
74 {
75  RAII_VAR(struct ao2_container *, presence_states, NULL, ao2_cleanup);
76  const char *action_id = astman_get_header(m, "ActionID");
77  struct stasis_message *msg;
78  struct ao2_iterator it_states;
79  int count = 0;
80 
81  presence_states = stasis_cache_dump(ast_presence_state_cache(),
83  if (!presence_states) {
84  astman_send_error(s, m, "Memory Allocation Failure");
85  return 0;
86  }
87 
88  astman_send_listack(s, m, "Presence State Changes will follow", "start");
89 
90  it_states = ao2_iterator_init(presence_states, 0);
91  for (; (msg = ao2_iterator_next(&it_states)); ao2_ref(msg, -1)) {
93 
94  if (!blob) {
95  continue;
96  }
97 
98  count++;
99 
100  astman_append(s, "Event: %s\r\n", blob->manager_event);
101  if (!ast_strlen_zero(action_id)) {
102  astman_append(s, "ActionID: %s\r\n", action_id);
103  }
104  astman_append(s, "%s\r\n", blob->extra_fields);
105  ao2_ref(blob, -1);
106  }
107  ao2_iterator_destroy(&it_states);
108 
109  astman_send_list_complete_start(s, m, "PresenceStateListComplete", count);
111 
112  return 0;
113 }
114 
115 static int unload_module(void)
116 {
117  ast_manager_unregister("PresenceStateList");
118  topic_forwarder = stasis_forward_cancel(topic_forwarder);
119  return 0;
120 }
121 
122 static int load_module(void)
123 {
124  struct stasis_topic *manager_topic;
125 
126  manager_topic = ast_manager_get_topic();
127  if (!manager_topic) {
129  }
130  topic_forwarder = stasis_forward_all(ast_presence_state_topic_all(), manager_topic);
131  if (!topic_forwarder) {
133  }
134 
137  topic_forwarder = stasis_forward_cancel(topic_forwarder);
139  }
140 
142 }
143 
144 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Manager Presence State Topic Forwarder",
145  .support_level = AST_MODULE_SUPPORT_CORE,
146  .load = load_module,
147  .unload = unload_module,
148  .load_pri = AST_MODPRI_DEVSTATE_CONSUMER,
149 );
Struct containing info for an AMI event to send out.
Definition: manager.h:491
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:3080
Asterisk main include file. File version handling, generic pbx functions.
const ast_string_field extra_fields
Definition: manager.h:496
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition: manager.c:3237
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
static int load_module(void)
#define EVENT_FLAG_CALL
Definition: manager.h:72
static int action_presencestatelist(struct mansession *s, const struct message *m)
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
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
#define NULL
Definition: resample.c:96
static struct stasis_topic * manager_topic
A stasis_topic that all topics AMI cares about will be forwarded to.
Definition: manager.c:1490
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:2820
#define ast_strlen_zero(foo)
Definition: strings.h:52
const char * manager_event
Definition: manager.h:493
struct ast_manager_event_blob * stasis_message_to_ami(struct stasis_message *msg)
Build the AMI representation of the message.
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:3245
#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
#define ao2_ref(o, delta)
Definition: astobj2.h:464
In case you didn&#39;t read that giant block of text above the mansession_session struct, the struct mansession is named this solely to keep the API the same in Asterisk. This structure really represents data that is different from Manager action to Manager action. The mansession_session pointer contained within points to session-specific data.
Definition: manager.c:1625
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7258
struct stasis_cache * ast_presence_state_cache(void)
Backend cache for ast_presence_state_topic_cached()
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
Presence state management.
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
static struct stasis_forward * topic_forwarder
struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition: stasis.c:1548
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct stasis_topic * ast_manager_get_topic(void)
Get the Stasis Message Bus API topic for AMI.
Definition: manager.c:1720
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
#define EVENT_FLAG_REPORTING
Definition: manager.h:80
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static int unload_module(void)
struct stasis_message_type * ast_presence_state_message_type(void)
Get presence state message type.
Forwarding information.
Definition: stasis.c:1531
Generic container type.
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
struct stasis_topic * ast_presence_state_topic_all(void)
Get presence state topic.
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:186
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:3159
Asterisk module definitions.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition: manager.c:3201