Debian Codegraph @

funcs/func_holdintercept.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Digium, Inc.
5 *
6 * Matt Jordan <[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 /*! \file
20 *
21 * \brief Function that intercepts HOLD frames from channels and raises events
22 *
23 * \author Matt Jordan <[email protected]>
24 *
25 * \ingroup functions
26 */
27
28 /*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/module.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/app.h"
38 #include "asterisk/frame.h"
39 #include "asterisk/stasis.h"
40 #include "asterisk/stasis_channels.h"
41
42 /*** DOCUMENTATION
43 <function name="HOLD_INTERCEPT" language="en_US">
44 <since>
45 <version>13.7.0</version>
46 </since>
47 <synopsis>
48 Intercepts hold frames on a channel and raises an event instead of passing the frame on
49 </synopsis>
50 <syntax>
51 <parameter name="action" required="true">
52 <optionlist>
53 <option name="remove">
54 <para>W/O. Removes the hold interception function.</para>
55 </option>
56 <option name="set">
57 <para>W/O. Enable hold interception on the channel. When
58 enabled, the channel will intercept any hold action that
59 is signalled from the device, and instead simply raise an
60 event (AMI/ARI) indicating that the channel wanted to put other
61 parties on hold.</para>
62 </option>
63 </optionlist>
64 </parameter>
65 </syntax>
66 </function>
67 ***/
68
69 /*! \brief Private data structure used with the function's datastore */
70 struct hold_intercept_data {
71 int framehook_id;
72 };
73
74 /*! \brief The channel datastore the function uses to store state */
75 static const struct ast_datastore_info hold_intercept_datastore = {
76 .type = "hold_intercept",
77 };
78
79 /*! \internal \brief Disable hold interception on the channel */
80 static int remove_hold_intercept(struct ast_channel *chan)
81 {
82 struct ast_datastore *datastore = NULL;
83 struct hold_intercept_data *data;
84 SCOPED_CHANNELLOCK(chan_lock, chan);
85
86 datastore = ast_channel_datastore_find(chan, &hold_intercept_datastore, NULL);
87 if (!datastore) {
88 ast_log(AST_LOG_WARNING, "Cannot remove HOLD_INTERCEPT from %s: HOLD_INTERCEPT not currently enabled\n",
89 ast_channel_name(chan));
90 return -1;
91 }
92 data = datastore->data;
93
94 if (ast_framehook_detach(chan, data->framehook_id)) {
95 ast_log(AST_LOG_WARNING, "Failed to remove HOLD_INTERCEPT framehook from channel %s\n",
96 ast_channel_name(chan));
97 return -1;
98 }
99
100 if (ast_channel_datastore_remove(chan, datastore)) {
101 ast_log(AST_LOG_WARNING, "Failed to remove HOLD_INTERCEPT datastore from channel %s\n",
102 ast_channel_name(chan));
103 return -1;
104 }
105 ast_datastore_free(datastore);
106
107 return 0;
108 }
109
110 /*! \brief Frame hook that is called to intercept hold/unhold */
111 static struct ast_frame *hold_intercept_framehook(struct ast_channel *chan,
112 struct ast_frame *f, enum ast_framehook_event event, void *data)
113 {
114 int frame_type;
115
116 if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
117 return f;
118 }
119
120 if (f->frametype != AST_FRAME_CONTROL) {
121 return f;
122 }
123
124 frame_type = f->subclass.integer;
125 if (frame_type != AST_CONTROL_HOLD && frame_type != AST_CONTROL_UNHOLD) {
126 return f;
127 }
128
129 /* Munch munch */
130 ast_frfree(f);
131 f = &ast_null_frame;
132
133 ast_channel_publish_cached_blob(chan,
134 frame_type == AST_CONTROL_HOLD ? ast_channel_hold_type() : ast_channel_unhold_type(),
135 NULL);
136
137 return f;
138 }
139
140 /*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
141 static int hold_intercept_framehook_consume(void *data, enum ast_frame_type type)
142 {
143 return (type == AST_FRAME_CONTROL ? 1 : 0);
144 }
145
146 /*! \internal \brief Enable hold interception on the channel */
147 static int set_hold_intercept(struct ast_channel *chan)
148 {
149 struct ast_datastore *datastore;
150 struct hold_intercept_data *data;
151 static struct ast_framehook_interface hold_framehook_interface = {
152 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
153 .event_cb = hold_intercept_framehook,
154 .consume_cb = hold_intercept_framehook_consume,
155 .disable_inheritance = 1,
156 };
157 SCOPED_CHANNELLOCK(chan_lock, chan);
158
159 datastore = ast_channel_datastore_find(chan, &hold_intercept_datastore, NULL);
160 if (datastore) {
161 ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT already set on '%s'\n",
162 ast_channel_name(chan));
163 return 0;
164 }
165
166 datastore = ast_datastore_alloc(&hold_intercept_datastore, NULL);
167 if (!datastore) {
168 return -1;
169 }
170
171 data = ast_calloc(1, sizeof(*data));
172 if (!data) {
173 ast_datastore_free(datastore);
174 return -1;
175 }
176
177 data->framehook_id = ast_framehook_attach(chan, &hold_framehook_interface);
178 if (data->framehook_id < 0) {
179 ast_log(AST_LOG_WARNING, "Failed to attach HOLD_INTERCEPT framehook to '%s'\n",
180 ast_channel_name(chan));
181 ast_datastore_free(datastore);
182 ast_free(data);
183 return -1;
184 }
185 datastore->data = data;
186
187 ast_channel_datastore_add(chan, datastore);
188
189 return 0;
190 }
191
192 /*! \internal \brief HOLD_INTERCEPT write function callback */
193 static int hold_intercept_fn_write(struct ast_channel *chan, const char *function,
194 char *data, const char *value)
195 {
196 int res;
197
198 if (!chan) {
199 return -1;
200 }
201
202 if (ast_strlen_zero(data)) {
203 ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT requires an argument\n");
204 return -1;
205 }
206
207 if (!strcasecmp(data, "set")) {
208 res = set_hold_intercept(chan);
209 } else if (!strcasecmp(data, "remove")) {
210 res = remove_hold_intercept(chan);
211 } else {
212 ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT: unknown option %s\n", data);
213 res = -1;
214 }
215
216 return res;
217 }
218
219 /*! \brief Definition of the HOLD_INTERCEPT function */
220 static struct ast_custom_function hold_intercept_function = {
221 .name = "HOLD_INTERCEPT",
222 .write = hold_intercept_fn_write,
223 };
224
225 /*! \internal \brief Unload the module */
226 static int unload_module(void)
227 {
228 return ast_custom_function_unregister(&hold_intercept_function);
229 }
230
231 /*! \internal \brief Load the module */
232 static int load_module(void)
233 {
234 return ast_custom_function_register(&hold_intercept_function) ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
235 }
236
237 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hold interception dialplan function");