clang compiler warnings: Fix format specified in framehook
[asterisk/asterisk.git] / main / framehook.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <dvossel@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 /*! \file
20  *
21  * \brief FrameHooks Architecture
22  *
23  * \author David Vossel <dvossel@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/channel.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/framehook.h"
37 #include "asterisk/frame.h"
38
39 struct ast_framehook {
40         struct ast_framehook_interface i;
41         /*! This pointer to ast_channel the framehook is attached to. */
42         struct ast_channel *chan;
43         /*! the id representing this framehook on a channel */
44         unsigned int id;
45         /*! when set, this signals the read and write function to detach the hook */
46         int detach_and_destroy_me;
47         /*! list entry for ast_framehook_list object */
48         AST_LIST_ENTRY(ast_framehook) list;
49 };
50
51 struct ast_framehook_list {
52         /*! the number of hooks currently present */
53         unsigned int count;
54         /*! id for next framehook added */
55         unsigned int id_count;
56         AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
57 };
58
59 enum framehook_detachment_mode
60 {
61         /*! Destroy the framehook outright. */
62         FRAMEHOOK_DETACH_DESTROY = 0,
63         /*! Remove the framehook from the channel, but don't destroy the data since
64          *  it will be used by a replacement framehook on another channel. */
65         FRAMEHOOK_DETACH_PRESERVE,
66 };
67
68 static void framehook_detach(struct ast_framehook *framehook, enum framehook_detachment_mode mode)
69 {
70         struct ast_frame *frame;
71         frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
72         /* never assume anything about this function. If you can return a frame during
73          * the detached event, then assume someone will. */
74         if (frame) {
75                 ast_frfree(frame);
76         }
77         framehook->chan = NULL;
78
79         if (mode == FRAMEHOOK_DETACH_DESTROY && framehook->i.destroy_cb) {
80                 framehook->i.destroy_cb(framehook->i.data);
81         }
82         ast_free(framehook);
83 }
84
85 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
86 {
87         struct ast_framehook *framehook;
88         struct ast_frame *original_frame;
89         int *skip;
90         size_t skip_size;
91
92         if (!framehooks) {
93                 return frame;
94         }
95
96         skip_size = sizeof(int) * framehooks->count;
97         skip = ast_alloca(skip_size);
98         memset(skip, 0, skip_size);
99
100         do {
101                 unsigned int num = 0;
102                 original_frame = frame;
103
104                 AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
105                         if (framehook->detach_and_destroy_me) {
106                                 /* this guy is signaled for destruction */
107                                 AST_LIST_REMOVE_CURRENT(list);
108                                 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
109                                 continue;
110                         }
111
112                         /* If this framehook has been marked as needing to be skipped, do so */
113                         if (skip[num]) {
114                                 num++;
115                                 continue;
116                         }
117
118                         frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
119
120                         if (frame != original_frame) {
121                                 /* To prevent looping we skip any framehooks that have already provided a modified frame */
122                                 skip[num] = 1;
123                                 break;
124                         }
125
126                         num++;
127                 }
128                 AST_LIST_TRAVERSE_SAFE_END;
129         } while (frame != original_frame);
130
131         return frame;
132 }
133
134 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
135 {
136         struct ast_framehook *framehook;
137         struct ast_framehook_list *fh_list;
138         struct ast_frame *frame;
139         if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
140                 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%i)\n",
141                         i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
142                 return -1;
143         }
144         if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
145                 return -1;
146         }
147         framehook->i = *i;
148         framehook->chan = chan;
149
150         /* create the framehook list if it didn't already exist */
151         if (!ast_channel_framehooks(chan)) {
152                 if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
153                         ast_free(framehook);
154                         return -1;
155                 }
156                 ast_channel_framehooks_set(chan, fh_list);
157         }
158
159         ast_channel_framehooks(chan)->count++;
160         framehook->id = ++ast_channel_framehooks(chan)->id_count;
161         AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
162
163         /* Tell the event callback we're live and rocking */
164         frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
165
166         /* Never assume anything about this function. If you can return a frame during
167          * the attached event, then assume someone will. */
168         if (frame) {
169                 ast_frfree(frame);
170         }
171
172         if (ast_channel_is_bridged(chan)) {
173                 ast_channel_set_unbridged_nolock(chan, 1);
174         }
175
176         return framehook->id;
177 }
178
179 int ast_framehook_detach(struct ast_channel *chan, int id)
180 {
181         struct ast_framehook *framehook;
182         int res = -1;
183
184         if (!ast_channel_framehooks(chan)) {
185                 return res;
186         }
187
188         AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
189                 if (framehook->id == id) {
190                         /* we mark for detachment rather than doing explicitly here because
191                          * it needs to be safe for this function to be called within the
192                          * event callback.  If we allowed the hook to actually be destroyed
193                          * immediately here, the event callback would crash on exit. */
194                         framehook->detach_and_destroy_me = 1;
195                         res = 0;
196                         break;
197                 }
198         }
199         AST_LIST_TRAVERSE_SAFE_END;
200
201         if (!res && ast_channel_is_bridged(chan)) {
202                 ast_channel_set_unbridged_nolock(chan, 1);
203         }
204
205         return res;
206 }
207
208 int ast_framehook_list_destroy(struct ast_channel *chan)
209 {
210         struct ast_framehook *framehook;
211
212         if (!ast_channel_framehooks(chan)) {
213                 return 0;
214         }
215         AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
216                 AST_LIST_REMOVE_CURRENT(list);
217                 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
218         }
219         AST_LIST_TRAVERSE_SAFE_END;
220         ast_free(ast_channel_framehooks(chan));
221         ast_channel_framehooks_set(chan, NULL);
222         return 0;
223 }
224
225 void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan)
226 {
227         struct ast_framehook *framehook;
228         int moved_framehook_id;
229
230         if (ast_channel_framehooks(new_chan)) {
231                 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(new_chan)->list, framehook, list) {
232                         if (framehook->i.disable_inheritance) {
233                                 ast_framehook_detach(new_chan, framehook->id);
234                                 continue;
235                         }
236
237                         if (framehook->i.chan_breakdown_cb) {
238                                 framehook->i.chan_breakdown_cb(framehook->i.data, framehook->id,
239                                         old_chan, new_chan);
240                         }
241                 }
242                 AST_LIST_TRAVERSE_SAFE_END;
243         }
244
245         if (!ast_channel_framehooks(old_chan)) {
246                 return;
247         }
248
249         if (!AST_LIST_EMPTY(&ast_channel_framehooks(old_chan)->list)
250                 && ast_channel_is_bridged(old_chan)) {
251                 ast_channel_set_unbridged_nolock(old_chan, 1);
252         }
253         while ((framehook = AST_LIST_REMOVE_HEAD(&ast_channel_framehooks(old_chan)->list, list))) {
254                 /* If inheritance is not allowed for this framehook, just destroy it. */
255                 if (framehook->i.disable_inheritance) {
256                         framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
257                         continue;
258                 }
259
260                 /* Otherwise move it to the other channel and perform any fixups set by the framehook interface */
261                 moved_framehook_id = ast_framehook_attach(new_chan, &framehook->i);
262                 if (moved_framehook_id < 0) {
263                         ast_log(LOG_WARNING, "Failed framehook copy during masquerade. Expect loss of features.\n");
264                         framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
265                 } else {
266                         if (framehook->i.chan_fixup_cb) {
267                                 framehook->i.chan_fixup_cb(framehook->i.data, moved_framehook_id,
268                                         old_chan, new_chan);
269                         }
270
271                         framehook_detach(framehook, FRAMEHOOK_DETACH_PRESERVE);
272                 }
273         }
274 }
275
276 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
277 {
278         if (!framehooks) {
279                 return 1;
280         }
281         return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
282 }
283
284 int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
285 {
286         return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
287 }
288
289 int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
290         enum ast_frame_type type)
291 {
292         struct ast_framehook *cur;
293
294         if (!framehooks) {
295                 return 1;
296         }
297
298         if (AST_LIST_EMPTY(&framehooks->list)) {
299                 return 1;
300         }
301
302         AST_LIST_TRAVERSE(&framehooks->list, cur, list) {
303                 if (cur->detach_and_destroy_me) {
304                         continue;
305                 }
306                 if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
307                         continue;
308                 }
309                 return 0;
310         }
311
312         return 1;
313 }
314
315 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
316 {
317         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
318 }
319
320 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
321 {
322         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
323 }