Add ports to the source, filter and sink component interfaces
[babeltrace.git] / lib / component / filter.c
CommitLineData
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>
d71dcf2c 31#include <babeltrace/component/component-filter-internal.h>
33b34c43 32#include <babeltrace/component/component-internal.h>
d3e4dcd8 33#include <babeltrace/component/component-class-internal.h>
33b34c43
PP
34#include <babeltrace/component/notification/notification.h>
35#include <babeltrace/component/notification/iterator-internal.h>
34ac9eaf 36
366e034f 37enum bt_component_status bt_component_filter_add_iterator(
34a9ed19 38 struct bt_component *component,
366e034f 39 struct bt_notification_iterator *iterator)
34a9ed19 40{
34a9ed19 41 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
366e034f 42 struct bt_component_class_filter *filter_class;
34a9ed19 43
366e034f 44 if (!component || !iterator) {
34a9ed19
JG
45 ret = BT_COMPONENT_STATUS_INVALID;
46 goto end;
47 }
48
d3e4dcd8 49 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
50 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
51 goto end;
52 }
53
366e034f
JG
54 /* TODO validate iterator count limits. */
55
56 filter_class = container_of(component->class,
57 struct bt_component_class_filter, parent);
58 if (filter_class->methods.add_iterator) {
59 ret = filter_class->methods.add_iterator(component, iterator);
60 if (ret != BT_COMPONENT_STATUS_OK) {
61 goto end;
62 }
34a9ed19
JG
63 }
64
34a9ed19
JG
65end:
66 return ret;
67}
68
366e034f
JG
69struct bt_notification_iterator *bt_component_filter_create_notification_iterator(
70 struct bt_component *component)
34a9ed19 71{
366e034f
JG
72 return bt_component_create_iterator(component, NULL);
73}
34a9ed19 74
366e034f
JG
75struct bt_notification_iterator *bt_component_filter_create_notification_iterator_with_init_method_data(
76 struct bt_component *component, void *init_method_data)
77{
78 return bt_component_create_iterator(component, init_method_data);
79}
34a9ed19 80
366e034f
JG
81static
82void bt_component_filter_destroy(struct bt_component *component)
83{
84 struct bt_component_filter *filter = container_of(component,
85 struct bt_component_filter, parent);
34a9ed19 86
366e034f
JG
87 if (filter->input_ports) {
88 g_ptr_array_free(filter->input_ports, TRUE);
34a9ed19
JG
89 }
90
366e034f
JG
91 if (filter->output_ports) {
92 g_ptr_array_free(filter->output_ports, TRUE);
93 }
34a9ed19
JG
94}
95
366e034f
JG
96BT_HIDDEN
97struct bt_component *bt_component_filter_create(
98 struct bt_component_class *class, struct bt_value *params)
34a9ed19 99{
366e034f
JG
100 int ret;
101 struct bt_component_filter *filter = NULL;
102 enum bt_component_status status;
34a9ed19 103
366e034f
JG
104 filter = g_new0(struct bt_component_filter, 1);
105 if (!filter) {
34a9ed19
JG
106 goto end;
107 }
108
366e034f
JG
109 filter->parent.class = bt_get(class);
110 status = bt_component_init(&filter->parent, bt_component_filter_destroy);
111 if (status != BT_COMPONENT_STATUS_OK) {
112 goto error;
113 }
114
115 ret = bt_component_init_input_ports(&filter->parent,
116 &filter->input_ports);
117 if (ret) {
118 goto error;
119 }
120
121 ret = bt_component_init_output_ports(&filter->parent,
122 &filter->output_ports);
123 if (ret) {
124 goto error;
34a9ed19
JG
125 }
126
34a9ed19 127end:
366e034f
JG
128 return filter ? &filter->parent : NULL;
129error:
130 BT_PUT(filter);
131 goto end;
34a9ed19
JG
132}
133
366e034f
JG
134BT_HIDDEN
135enum bt_component_status bt_component_filter_validate(
136 struct bt_component *component)
34a9ed19 137{
34a9ed19
JG
138 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
139
366e034f 140 if (!component) {
34a9ed19
JG
141 ret = BT_COMPONENT_STATUS_INVALID;
142 goto end;
143 }
144
366e034f
JG
145 if (!component->class) {
146 ret = BT_COMPONENT_STATUS_INVALID;
34a9ed19
JG
147 goto end;
148 }
149
366e034f 150 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
151 ret = BT_COMPONENT_STATUS_INVALID;
152 goto end;
153 }
154
366e034f 155 /* Enforce iterator limits. */
34a9ed19
JG
156end:
157 return ret;
158}
159
366e034f 160int bt_component_filter_get_input_port_count(struct bt_component *component)
34a9ed19 161{
366e034f 162 int ret;
34a9ed19 163 struct bt_component_filter *filter;
34a9ed19 164
366e034f
JG
165 if (!component) {
166 ret = -1;
34a9ed19
JG
167 goto end;
168 }
169
366e034f
JG
170 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
171 ret = -1;
34a9ed19
JG
172 goto end;
173 }
174
175 filter = container_of(component, struct bt_component_filter, parent);
366e034f
JG
176 ret = filter->input_ports->len;
177end:
178 return ret;
179}
180
181struct bt_port *bt_component_filter_get_input_port(
182 struct bt_component *component, const char *name)
183{
184 struct bt_component_filter *filter;
185 struct bt_port *ret_port = NULL;
186
187 if (!component || !name ||
188 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
34a9ed19
JG
189 goto end;
190 }
191
366e034f
JG
192 filter = container_of(component, struct bt_component_filter, parent);
193 ret_port = bt_component_get_port(filter->input_ports, name);
194end:
195 return ret_port;
196}
d3e4dcd8 197
366e034f
JG
198struct bt_port *bt_component_filter_get_input_port_at_index(
199 struct bt_component *component, int index)
200{
201 struct bt_port *port = NULL;
202 struct bt_component_filter *filter;
203
204 if (!component ||
205 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
206 goto end;
34a9ed19
JG
207 }
208
366e034f
JG
209 filter = container_of(component, struct bt_component_filter, parent);
210 port = bt_component_get_port_at_index(filter->input_ports, index);
34a9ed19 211end:
366e034f 212 return port;
34a9ed19
JG
213}
214
366e034f 215struct bt_port *bt_component_filter_get_default_input_port(
34a9ed19
JG
216 struct bt_component *component)
217{
366e034f
JG
218 return bt_component_filter_get_input_port(component,
219 DEFAULT_INPUT_PORT_NAME);
8b0ce102
PP
220}
221
366e034f
JG
222struct bt_port *bt_component_filter_add_input_port(
223 struct bt_component *component, const char *name)
8b0ce102 224{
366e034f
JG
225 struct bt_port *port;
226 struct bt_component_filter *filter;
227
228 if (!component ||
229 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
230 port = NULL;
231 goto end;
232 }
233
234 filter = container_of(component, struct bt_component_filter, parent);
235 port = bt_component_add_port(component, filter->input_ports,
236 BT_PORT_TYPE_INPUT, name);
237end:
238 return port;
34a9ed19 239}
526fc31a 240
366e034f
JG
241enum bt_component_status bt_component_filter_remove_input_port(
242 struct bt_component *component, const char *name)
526fc31a 243{
366e034f
JG
244 enum bt_component_status status;
245 struct bt_component_filter *filter;
526fc31a 246
366e034f
JG
247 if (!component ||
248 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
249 status = BT_COMPONENT_STATUS_INVALID;
250 goto end;
251 }
252
253 filter = container_of(component, struct bt_component_filter, parent);
254 status = bt_component_remove_port(component, filter->input_ports,
255 name);
256end:
257 return status;
526fc31a
JG
258}
259
366e034f 260int bt_component_filter_get_output_port_count(struct bt_component *component)
526fc31a 261{
366e034f
JG
262 int ret;
263 struct bt_component_filter *filter;
526fc31a 264
366e034f
JG
265 if (!component) {
266 ret = -1;
526fc31a
JG
267 goto end;
268 }
269
366e034f
JG
270 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
271 ret = -1;
272 goto end;
526fc31a
JG
273 }
274
366e034f
JG
275 filter = container_of(component, struct bt_component_filter, parent);
276 ret = filter->output_ports->len;
526fc31a 277end:
366e034f 278 return ret;
526fc31a
JG
279}
280
366e034f
JG
281struct bt_port *bt_component_filter_get_output_port(
282 struct bt_component *component, const char *name)
526fc31a 283{
526fc31a 284 struct bt_component_filter *filter;
366e034f 285 struct bt_port *ret_port = NULL;
526fc31a 286
366e034f
JG
287 if (!component || !name ||
288 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
526fc31a
JG
289 goto end;
290 }
291
366e034f
JG
292 filter = container_of(component, struct bt_component_filter, parent);
293 ret_port = bt_component_get_port(filter->output_ports, name);
294end:
295 return ret_port;
296}
297
298struct bt_port *bt_component_filter_get_output_port_at_index(
299 struct bt_component *component, int index)
300{
301 struct bt_port *port = NULL;
302 struct bt_component_filter *filter;
303
304 if (!component ||
305 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
526fc31a
JG
306 goto end;
307 }
308
366e034f
JG
309 filter = container_of(component, struct bt_component_filter, parent);
310 port = bt_component_get_port_at_index(filter->output_ports, index);
311end:
312 return port;
313}
314
315struct bt_port *bt_component_filter_get_default_output_port(
316 struct bt_component *component)
317{
318 return bt_component_filter_get_output_port(component,
319 DEFAULT_OUTPUT_PORT_NAME);
320}
321
322struct bt_port *bt_component_filter_add_output_port(
323 struct bt_component *component, const char *name)
324{
325 struct bt_port *port;
326 struct bt_component_filter *filter;
327
328 if (!component ||
329 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
330 port = NULL;
526fc31a
JG
331 goto end;
332 }
333
334 filter = container_of(component, struct bt_component_filter, parent);
366e034f
JG
335 port = bt_component_add_port(component, filter->output_ports,
336 BT_PORT_TYPE_OUTPUT, name);
337end:
338 return port;
339}
340
341enum bt_component_status bt_component_filter_remove_output_port(
342 struct bt_component *component, const char *name)
343{
344 enum bt_component_status status;
345 struct bt_component_filter *filter;
346
347 if (!component ||
348 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
349 status = BT_COMPONENT_STATUS_INVALID;
526fc31a
JG
350 goto end;
351 }
366e034f
JG
352
353 filter = container_of(component, struct bt_component_filter, parent);
354 status = bt_component_remove_port(component, filter->output_ports,
355 name);
526fc31a 356end:
366e034f 357 return status;
526fc31a 358}
This page took 0.041099 seconds and 4 git commands to generate.