Revert -r430452 It needs to be redone for the next major AMI version change instead.
[asterisk/asterisk.git] / res / res_manager_devicestate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.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 /*** DOCUMENTATION
24         <manager name="DeviceStateList" language="en_US">
25                 <synopsis>
26                         List the current known device 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 device states in a
33                         sequence of <replaceable>DeviceStateChange</replaceable> events.
34                         When finished, a <replaceable>DeviceStateListComplete</replaceable> event
35                         will be emitted.</para>
36                 </description>
37                 <see-also>
38                         <ref type="managerEvent">DeviceStateChange</ref>
39                         <ref type="function">DEVICE_STATE</ref>
40                 </see-also>
41                 <responses>
42                         <list-elements>
43                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='DeviceStateChange'])" />
44                         </list-elements>
45                         <managerEvent name="DeviceStateListComplete" language="en_US">
46                                 <managerEventInstance class="EVENT_FLAG_COMMAND">
47                                         <synopsis>
48                                                 Indicates the end of the list the current known extension states.
49                                         </synopsis>
50                                         <syntax>
51                                                 <parameter name="EventList">
52                                                         <para>Conveys the status of the event list.</para>
53                                                 </parameter>
54                                                 <parameter name="ListItems">
55                                                         <para>Conveys the number of statuses reported.</para>
56                                                 </parameter>
57                                         </syntax>
58                                 </managerEventInstance>
59                         </managerEvent>
60                 </responses>
61         </manager>
62  ***/
63
64
65 #include "asterisk.h"
66 #include "asterisk/module.h"
67 #include "asterisk/manager.h"
68 #include "asterisk/stasis.h"
69 #include "asterisk/devicestate.h"
70
71 static struct stasis_forward *topic_forwarder;
72
73 static int action_devicestatelist(struct mansession *s, const struct message *m)
74 {
75         RAII_VAR(struct ao2_container *, device_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         device_states = stasis_cache_dump_by_eid(ast_device_state_cache(),
82                 ast_device_state_message_type(), NULL);
83         if (!device_states) {
84                 astman_send_error(s, m, "Memory Allocation Failure");
85                 return 0;
86         }
87
88         astman_send_listack(s, m, "Device State Changes will follow", "start");
89
90         it_states = ao2_iterator_init(device_states, 0);
91         for (; (msg = ao2_iterator_next(&it_states)); ao2_ref(msg, -1)) {
92                 struct ast_manager_event_blob *blob = stasis_message_to_ami(msg);
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, "DeviceStateListComplete", count);
110         astman_send_list_complete_end(s);
111
112         return 0;
113 }
114
115 static int unload_module(void)
116 {
117         topic_forwarder = stasis_forward_cancel(topic_forwarder);
118         ast_manager_unregister("DeviceStateList");
119
120         return 0;
121 }
122
123 static int load_module(void)
124 {
125         struct stasis_topic *manager_topic;
126
127         manager_topic = ast_manager_get_topic();
128         if (!manager_topic) {
129                 return AST_MODULE_LOAD_DECLINE;
130         }
131         topic_forwarder = stasis_forward_all(ast_device_state_topic_all(), manager_topic);
132         if (!topic_forwarder) {
133                 return AST_MODULE_LOAD_DECLINE;
134         }
135
136         if (ast_manager_register_xml("DeviceStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING,
137                                          action_devicestatelist)) {
138                 topic_forwarder = stasis_forward_cancel(topic_forwarder);
139                 return AST_MODULE_LOAD_DECLINE;
140         }
141
142         return AST_MODULE_LOAD_SUCCESS;
143 }
144
145 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Manager Device State Topic Forwarder",
146                 .support_level = AST_MODULE_SUPPORT_CORE,
147                 .load = load_module,
148                 .unload = unload_module,
149                 .load_pri = AST_MODPRI_DEVSTATE_CONSUMER,
150         );