Fix some opaquification-related compiler warnings
[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 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/channel.h"
31 #include "asterisk/linkedlists.h"
32 #include "asterisk/framehook.h"
33 #include "asterisk/frame.h"
34
35 struct ast_framehook {
36         struct ast_framehook_interface i;
37         /*! This pointer to ast_channel the framehook is attached to. */
38         struct ast_channel *chan;
39         /*! the id representing this framehook on a channel */
40         unsigned int id;
41         /*! when set, this signals the read and write function to detach the hook */
42         int detach_and_destroy_me;
43         /*! list entry for ast_framehook_list object */
44         AST_LIST_ENTRY(ast_framehook) list;
45 };
46
47 struct ast_framehook_list {
48         unsigned int id_count;
49         AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
50 };
51
52 static void framehook_detach_and_destroy(struct ast_framehook *framehook)
53 {
54         struct ast_frame *frame;
55         frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
56         /* never assume anything about this function. If you can return a frame during
57          * the detached event, then assume someone will. */
58         if (frame) {
59                 ast_frfree(frame);
60         }
61         framehook->chan = NULL;
62
63         if (framehook->i.destroy_cb) {
64                 framehook->i.destroy_cb(framehook->i.data);
65         }
66         ast_free(framehook);
67 }
68
69 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
70 {
71         struct ast_framehook *framehook;
72
73         if (!framehooks) {
74                 return frame;
75         }
76
77         AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
78                 if (framehook->detach_and_destroy_me) {
79                         /* this guy is signaled for destruction */
80                         AST_LIST_REMOVE_CURRENT(list);
81                         framehook_detach_and_destroy(framehook);
82                 } else {
83                         frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
84                 }
85         }
86         AST_LIST_TRAVERSE_SAFE_END;
87         return frame;
88 }
89
90 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
91 {
92         struct ast_framehook *framehook;
93         struct ast_framehook_list *fh_list;
94         struct ast_frame *frame;
95         if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
96                 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
97                         i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
98                 return -1;
99         }
100         if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
101                 return -1;
102         }
103         framehook->i = *i;
104         framehook->chan = chan;
105
106         /* create the framehook list if it didn't already exist */
107         if (!ast_channel_framehooks(chan)) {
108                 if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
109                         ast_free(framehook);
110                         return -1;
111                 }
112                 ast_channel_framehooks_set(chan, fh_list);
113         }
114
115         framehook->id = ++ast_channel_framehooks(chan)->id_count;
116         AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
117
118         /* Tell the event callback we're live and rocking */
119         frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
120
121         /* Never assume anything about this function. If you can return a frame during
122          * the attached event, then assume someone will. */
123         if (frame) {
124                 ast_frfree(frame);
125         }
126
127         return framehook->id;
128 }
129
130 int ast_framehook_detach(struct ast_channel *chan, int id)
131 {
132         struct ast_framehook *framehook;
133         int res = -1;
134
135         if (!ast_channel_framehooks(chan)) {
136                 return res;
137         }
138
139         AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
140                 if (framehook->id == id) {
141                         /* we mark for detachment rather than doing explicitly here because
142                          * it needs to be safe for this function to be called within the
143                          * event callback.  If we allowed the hook to actually be destroyed
144                          * immediately here, the event callback would crash on exit. */
145                         framehook->detach_and_destroy_me = 1;
146                         res = 0;
147                         break;
148                 }
149         }
150         AST_LIST_TRAVERSE_SAFE_END;
151
152         return res;
153 }
154
155 int ast_framehook_list_destroy(struct ast_channel *chan)
156 {
157         struct ast_framehook *framehook;
158
159         if (!ast_channel_framehooks(chan)) {
160                 return 0;
161         }
162         AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
163                 AST_LIST_REMOVE_CURRENT(list);
164                 framehook_detach_and_destroy(framehook);
165         }
166         AST_LIST_TRAVERSE_SAFE_END;
167         ast_free(ast_channel_framehooks(chan));
168         ast_channel_framehooks_set(chan, NULL);
169         return 0;
170 }
171
172 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
173 {
174         if (!framehooks) {
175                 return 1;
176         }
177         return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
178 }
179
180 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
181 {
182         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
183 }
184
185 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
186 {
187         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
188 }