4 * Babeltrace Filter Component
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/values.h>
31 #include <babeltrace/component/component-filter-internal.h>
32 #include <babeltrace/component/component-internal.h>
33 #include <babeltrace/component/component-class-internal.h>
34 #include <babeltrace/component/notification/notification.h>
35 #include <babeltrace/component/notification/iterator-internal.h>
37 enum bt_component_status
bt_component_filter_add_iterator(
38 struct bt_component
*component
,
39 struct bt_notification_iterator
*iterator
)
41 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
42 struct bt_component_class_filter
*filter_class
;
44 if (!component
|| !iterator
) {
45 ret
= BT_COMPONENT_STATUS_INVALID
;
49 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_FILTER
) {
50 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
54 /* TODO validate iterator count limits. */
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
) {
69 struct bt_notification_iterator
*bt_component_filter_create_notification_iterator(
70 struct bt_component
*component
)
72 return bt_component_create_iterator(component
, NULL
);
75 struct bt_notification_iterator
*bt_component_filter_create_notification_iterator_with_init_method_data(
76 struct bt_component
*component
, void *init_method_data
)
78 return bt_component_create_iterator(component
, init_method_data
);
82 void bt_component_filter_destroy(struct bt_component
*component
)
84 struct bt_component_filter
*filter
= container_of(component
,
85 struct bt_component_filter
, parent
);
87 if (filter
->input_ports
) {
88 g_ptr_array_free(filter
->input_ports
, TRUE
);
91 if (filter
->output_ports
) {
92 g_ptr_array_free(filter
->output_ports
, TRUE
);
97 struct bt_component
*bt_component_filter_create(
98 struct bt_component_class
*class, struct bt_value
*params
)
101 struct bt_component_filter
*filter
= NULL
;
102 enum bt_component_status status
;
104 filter
= g_new0(struct bt_component_filter
, 1);
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
) {
115 ret
= bt_component_init_input_ports(&filter
->parent
,
116 &filter
->input_ports
);
121 ret
= bt_component_init_output_ports(&filter
->parent
,
122 &filter
->output_ports
);
128 return filter
? &filter
->parent
: NULL
;
135 enum bt_component_status
bt_component_filter_validate(
136 struct bt_component
*component
)
138 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
141 ret
= BT_COMPONENT_STATUS_INVALID
;
145 if (!component
->class) {
146 ret
= BT_COMPONENT_STATUS_INVALID
;
150 if (component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
151 ret
= BT_COMPONENT_STATUS_INVALID
;
155 /* Enforce iterator limits. */
160 int bt_component_filter_get_input_port_count(struct bt_component
*component
)
163 struct bt_component_filter
*filter
;
170 if (component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
175 filter
= container_of(component
, struct bt_component_filter
, parent
);
176 ret
= filter
->input_ports
->len
;
181 struct bt_port
*bt_component_filter_get_input_port(
182 struct bt_component
*component
, const char *name
)
184 struct bt_component_filter
*filter
;
185 struct bt_port
*ret_port
= NULL
;
187 if (!component
|| !name
||
188 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
192 filter
= container_of(component
, struct bt_component_filter
, parent
);
193 ret_port
= bt_component_get_port(filter
->input_ports
, name
);
198 struct bt_port
*bt_component_filter_get_input_port_at_index(
199 struct bt_component
*component
, int index
)
201 struct bt_port
*port
= NULL
;
202 struct bt_component_filter
*filter
;
205 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
209 filter
= container_of(component
, struct bt_component_filter
, parent
);
210 port
= bt_component_get_port_at_index(filter
->input_ports
, index
);
215 struct bt_port
*bt_component_filter_get_default_input_port(
216 struct bt_component
*component
)
218 return bt_component_filter_get_input_port(component
,
219 DEFAULT_INPUT_PORT_NAME
);
222 struct bt_port
*bt_component_filter_add_input_port(
223 struct bt_component
*component
, const char *name
)
225 struct bt_port
*port
;
226 struct bt_component_filter
*filter
;
229 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
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
);
241 enum bt_component_status
bt_component_filter_remove_input_port(
242 struct bt_component
*component
, const char *name
)
244 enum bt_component_status status
;
245 struct bt_component_filter
*filter
;
248 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
249 status
= BT_COMPONENT_STATUS_INVALID
;
253 filter
= container_of(component
, struct bt_component_filter
, parent
);
254 status
= bt_component_remove_port(component
, filter
->input_ports
,
260 int bt_component_filter_get_output_port_count(struct bt_component
*component
)
263 struct bt_component_filter
*filter
;
270 if (component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
275 filter
= container_of(component
, struct bt_component_filter
, parent
);
276 ret
= filter
->output_ports
->len
;
281 struct bt_port
*bt_component_filter_get_output_port(
282 struct bt_component
*component
, const char *name
)
284 struct bt_component_filter
*filter
;
285 struct bt_port
*ret_port
= NULL
;
287 if (!component
|| !name
||
288 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
292 filter
= container_of(component
, struct bt_component_filter
, parent
);
293 ret_port
= bt_component_get_port(filter
->output_ports
, name
);
298 struct bt_port
*bt_component_filter_get_output_port_at_index(
299 struct bt_component
*component
, int index
)
301 struct bt_port
*port
= NULL
;
302 struct bt_component_filter
*filter
;
305 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
309 filter
= container_of(component
, struct bt_component_filter
, parent
);
310 port
= bt_component_get_port_at_index(filter
->output_ports
, index
);
315 struct bt_port
*bt_component_filter_get_default_output_port(
316 struct bt_component
*component
)
318 return bt_component_filter_get_output_port(component
,
319 DEFAULT_OUTPUT_PORT_NAME
);
322 struct bt_port
*bt_component_filter_add_output_port(
323 struct bt_component
*component
, const char *name
)
325 struct bt_port
*port
;
326 struct bt_component_filter
*filter
;
329 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
334 filter
= container_of(component
, struct bt_component_filter
, parent
);
335 port
= bt_component_add_port(component
, filter
->output_ports
,
336 BT_PORT_TYPE_OUTPUT
, name
);
341 enum bt_component_status
bt_component_filter_remove_output_port(
342 struct bt_component
*component
, const char *name
)
344 enum bt_component_status status
;
345 struct bt_component_filter
*filter
;
348 component
->class->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
) {
349 status
= BT_COMPONENT_STATUS_INVALID
;
353 filter
= container_of(component
, struct bt_component_filter
, parent
);
354 status
= bt_component_remove_port(component
, filter
->output_ports
,