Commit | Line | Data |
---|---|---|
34ac9eaf JG |
1 | /* |
2 | * filter.c | |
3 | * | |
4 | * Babeltrace Filter Component | |
5 | * | |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/compiler.h> | |
30 | #include <babeltrace/values.h> | |
526fc31a | 31 | #include <babeltrace/plugin/input.h> |
34ac9eaf JG |
32 | #include <babeltrace/plugin/filter-internal.h> |
33 | #include <babeltrace/plugin/component-internal.h> | |
34 | #include <babeltrace/plugin/notification/notification.h> | |
34a9ed19 | 35 | #include <babeltrace/plugin/notification/iterator-internal.h> |
34ac9eaf | 36 | |
34a9ed19 JG |
37 | enum bt_component_status bt_component_filter_set_iterator_init_cb( |
38 | struct bt_component *component, | |
39 | bt_component_filter_init_iterator_cb init_iterator) | |
40 | { | |
41 | struct bt_component_filter *filter; | |
42 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
34ac9eaf | 43 | |
34a9ed19 JG |
44 | if (component->class->type != BT_COMPONENT_TYPE_FILTER || |
45 | !component->initializing) { | |
46 | ret = BT_COMPONENT_STATUS_INVALID; | |
47 | goto end; | |
48 | } | |
49 | ||
50 | filter = container_of(component, struct bt_component_filter, parent); | |
51 | filter->init_iterator = init_iterator; | |
52 | end: | |
53 | return ret; | |
54 | } | |
55 | ||
56 | enum bt_component_status bt_component_filter_set_add_iterator_cb( | |
57 | struct bt_component *component, | |
58 | bt_component_filter_add_iterator_cb add_iterator) | |
59 | { | |
60 | struct bt_component_filter *filter; | |
61 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
62 | ||
63 | if (!component) { | |
64 | ret = BT_COMPONENT_STATUS_INVALID; | |
65 | goto end; | |
66 | } | |
67 | ||
68 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
69 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
70 | goto end; | |
71 | } | |
72 | ||
73 | if (!component->initializing) { | |
74 | ret = BT_COMPONENT_STATUS_INVALID; | |
75 | goto end; | |
76 | } | |
77 | ||
78 | filter = container_of(component, struct bt_component_filter, parent); | |
79 | filter->add_iterator = add_iterator; | |
80 | end: | |
81 | return ret; | |
82 | } | |
83 | ||
84 | enum bt_component_status bt_component_filter_set_minimum_input_count( | |
85 | struct bt_component *component, | |
86 | unsigned int minimum) | |
87 | { | |
88 | struct bt_component_filter *filter; | |
89 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
90 | ||
91 | if (!component) { | |
92 | ret = BT_COMPONENT_STATUS_INVALID; | |
93 | goto end; | |
94 | } | |
95 | ||
96 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
97 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
98 | goto end; | |
99 | } | |
100 | ||
101 | if (!component->initializing) { | |
102 | ret = BT_COMPONENT_STATUS_INVALID; | |
103 | goto end; | |
104 | } | |
105 | ||
106 | filter = container_of(component, struct bt_component_filter, parent); | |
107 | filter->input.min_count = minimum; | |
108 | end: | |
109 | return ret; | |
110 | } | |
111 | ||
112 | enum bt_component_status bt_component_filter_set_maximum_input_count( | |
113 | struct bt_component *component, | |
114 | unsigned int maximum) | |
115 | { | |
116 | struct bt_component_filter *filter; | |
117 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
118 | ||
119 | if (!component) { | |
120 | ret = BT_COMPONENT_STATUS_INVALID; | |
121 | goto end; | |
122 | } | |
123 | ||
124 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
125 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
126 | goto end; | |
127 | } | |
128 | ||
129 | if (!component->initializing) { | |
130 | ret = BT_COMPONENT_STATUS_INVALID; | |
131 | goto end; | |
132 | } | |
133 | ||
134 | filter = container_of(component, struct bt_component_filter, parent); | |
135 | filter->input.max_count = maximum; | |
136 | end: | |
137 | return ret; | |
138 | } | |
139 | ||
140 | enum bt_component_status | |
141 | bt_component_filter_get_input_count(struct bt_component *component, | |
142 | unsigned int *count) | |
143 | { | |
144 | struct bt_component_filter *filter; | |
145 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
146 | ||
147 | if (!component || !count) { | |
148 | ret = BT_COMPONENT_STATUS_INVALID; | |
149 | goto end; | |
150 | } | |
151 | ||
152 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
153 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
154 | goto end; | |
155 | } | |
156 | ||
157 | filter = container_of(component, struct bt_component_filter, parent); | |
158 | *count = (unsigned int) filter->input.iterators->len; | |
159 | end: | |
160 | return ret; | |
161 | } | |
162 | ||
163 | enum bt_component_status | |
164 | bt_component_filter_get_input_iterator(struct bt_component *component, | |
165 | unsigned int input, struct bt_notification_iterator **iterator) | |
166 | { | |
167 | struct bt_component_filter *filter; | |
168 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
169 | ||
170 | if (!component || !iterator) { | |
171 | ret = BT_COMPONENT_STATUS_INVALID; | |
172 | goto end; | |
173 | } | |
174 | ||
175 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
176 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
177 | goto end; | |
178 | } | |
179 | ||
180 | filter = container_of(component, struct bt_component_filter, parent); | |
181 | if (input >= (unsigned int) filter->input.iterators->len) { | |
182 | ret = BT_COMPONENT_STATUS_INVALID; | |
183 | goto end; | |
184 | } | |
185 | ||
186 | *iterator = bt_get(g_ptr_array_index(filter->input.iterators, input)); | |
187 | end: | |
188 | return ret; | |
189 | } | |
190 | ||
191 | enum bt_component_status bt_component_filter_add_iterator( | |
192 | struct bt_component *component, | |
193 | struct bt_notification_iterator *iterator) | |
194 | { | |
195 | struct bt_component_filter *filter; | |
196 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
197 | ||
198 | if (!component || !iterator) { | |
199 | ret = BT_COMPONENT_STATUS_INVALID; | |
200 | goto end; | |
201 | } | |
202 | ||
203 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
204 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
205 | goto end; | |
206 | } | |
207 | ||
208 | filter = container_of(component, struct bt_component_filter, parent); | |
209 | if (filter->input.iterators->len == filter->input.max_count) { | |
210 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
211 | goto end; | |
212 | } | |
213 | ||
214 | if (filter->add_iterator) { | |
215 | ret = filter->add_iterator(component, iterator); | |
216 | if (ret != BT_COMPONENT_STATUS_OK) { | |
217 | goto end; | |
218 | } | |
219 | } | |
220 | ||
221 | g_ptr_array_add(filter->input.iterators, bt_get(iterator)); | |
222 | end: | |
223 | return ret; | |
224 | } | |
225 | ||
226 | struct bt_notification_iterator *bt_component_filter_create_iterator( | |
227 | struct bt_component *component) | |
228 | { | |
5645cd95 | 229 | return bt_component_create_iterator(component); |
34a9ed19 | 230 | } |
526fc31a JG |
231 | |
232 | static | |
233 | void bt_component_filter_destroy(struct bt_component *component) | |
234 | { | |
235 | struct bt_component_filter *filter = container_of(component, | |
236 | struct bt_component_filter, parent); | |
237 | ||
238 | component_input_fini(&filter->input); | |
239 | } | |
240 | ||
241 | BT_HIDDEN | |
242 | struct bt_component *bt_component_filter_create( | |
243 | struct bt_component_class *class, struct bt_value *params) | |
244 | { | |
245 | struct bt_component_filter *filter = NULL; | |
246 | enum bt_component_status ret; | |
247 | ||
248 | filter = g_new0(struct bt_component_filter, 1); | |
249 | if (!filter) { | |
250 | goto end; | |
251 | } | |
252 | ||
253 | filter->parent.class = bt_get(class); | |
254 | ret = bt_component_init(&filter->parent, bt_component_filter_destroy); | |
255 | if (ret != BT_COMPONENT_STATUS_OK) { | |
256 | goto error; | |
257 | } | |
258 | ||
259 | if (component_input_init(&filter->input)) { | |
260 | goto error; | |
261 | } | |
262 | end: | |
263 | return filter ? &filter->parent : NULL; | |
264 | error: | |
265 | BT_PUT(filter); | |
266 | goto end; | |
267 | } | |
268 | ||
269 | BT_HIDDEN | |
270 | enum bt_component_status bt_component_filter_validate( | |
271 | struct bt_component *component) | |
272 | { | |
273 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
274 | struct bt_component_filter *filter; | |
275 | ||
276 | if (!component) { | |
277 | ret = BT_COMPONENT_STATUS_INVALID; | |
278 | goto end; | |
279 | } | |
280 | ||
281 | if (!component->class) { | |
282 | ret = BT_COMPONENT_STATUS_INVALID; | |
283 | goto end; | |
284 | } | |
285 | ||
286 | if (component->class->type != BT_COMPONENT_TYPE_FILTER) { | |
287 | ret = BT_COMPONENT_STATUS_INVALID; | |
288 | goto end; | |
289 | } | |
290 | ||
291 | filter = container_of(component, struct bt_component_filter, parent); | |
292 | if (!filter->init_iterator) { | |
293 | printf_error("Invalid filter component; no iterator initialization callback defined."); | |
294 | ret = BT_COMPONENT_STATUS_INVALID; | |
295 | goto end; | |
296 | } | |
297 | ||
298 | ret = component_input_validate(&filter->input); | |
299 | if (ret) { | |
300 | goto end; | |
301 | } | |
302 | end: | |
303 | return ret; | |
304 | } |