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/plugin/input.h>
32 #include <babeltrace/plugin/filter-internal.h>
33 #include <babeltrace/plugin/component-internal.h>
34 #include <babeltrace/plugin/notification/notification.h>
35 #include <babeltrace/plugin/notification/iterator-internal.h>
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
)
41 struct bt_component_filter
*filter
;
42 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
44 if (component
->class->type
!= BT_COMPONENT_TYPE_FILTER
||
45 !component
->initializing
) {
46 ret
= BT_COMPONENT_STATUS_INVALID
;
50 filter
= container_of(component
, struct bt_component_filter
, parent
);
51 filter
->init_iterator
= init_iterator
;
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
)
60 struct bt_component_filter
*filter
;
61 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
64 ret
= BT_COMPONENT_STATUS_INVALID
;
68 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_FILTER
) {
69 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
73 if (!component
->initializing
) {
74 ret
= BT_COMPONENT_STATUS_INVALID
;
78 filter
= container_of(component
, struct bt_component_filter
, parent
);
79 filter
->add_iterator
= add_iterator
;
84 enum bt_component_status
bt_component_filter_set_minimum_input_count(
85 struct bt_component
*component
,
88 struct bt_component_filter
*filter
;
89 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
92 ret
= BT_COMPONENT_STATUS_INVALID
;
96 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_FILTER
) {
97 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
101 if (!component
->initializing
) {
102 ret
= BT_COMPONENT_STATUS_INVALID
;
106 filter
= container_of(component
, struct bt_component_filter
, parent
);
107 filter
->input
.min_count
= minimum
;
112 enum bt_component_status
bt_component_filter_set_maximum_input_count(
113 struct bt_component
*component
,
114 unsigned int maximum
)
116 struct bt_component_filter
*filter
;
117 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
120 ret
= BT_COMPONENT_STATUS_INVALID
;
124 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_FILTER
) {
125 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
129 if (!component
->initializing
) {
130 ret
= BT_COMPONENT_STATUS_INVALID
;
134 filter
= container_of(component
, struct bt_component_filter
, parent
);
135 filter
->input
.max_count
= maximum
;
140 enum bt_component_status
141 bt_component_filter_get_input_count(struct bt_component
*component
,
144 struct bt_component_filter
*filter
;
145 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
147 if (!component
|| !count
) {
148 ret
= BT_COMPONENT_STATUS_INVALID
;
152 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_FILTER
) {
153 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
157 filter
= container_of(component
, struct bt_component_filter
, parent
);
158 *count
= (unsigned int) filter
->input
.iterators
->len
;
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
)
167 struct bt_component_filter
*filter
;
168 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
170 if (!component
|| !iterator
) {
171 ret
= BT_COMPONENT_STATUS_INVALID
;
175 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_FILTER
) {
176 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
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
;
186 *iterator
= bt_get(g_ptr_array_index(filter
->input
.iterators
, input
));
191 enum bt_component_status
bt_component_filter_add_iterator(
192 struct bt_component
*component
,
193 struct bt_notification_iterator
*iterator
)
195 struct bt_component_filter
*filter
;
196 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
198 if (!component
|| !iterator
) {
199 ret
= BT_COMPONENT_STATUS_INVALID
;
203 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_FILTER
) {
204 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
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
;
214 if (filter
->add_iterator
) {
215 ret
= filter
->add_iterator(component
, iterator
);
216 if (ret
!= BT_COMPONENT_STATUS_OK
) {
221 g_ptr_array_add(filter
->input
.iterators
, bt_get(iterator
));
226 struct bt_notification_iterator
*bt_component_filter_create_iterator(
227 struct bt_component
*component
)
229 return bt_component_create_iterator(component
);
233 void bt_component_filter_destroy(struct bt_component
*component
)
235 struct bt_component_filter
*filter
= container_of(component
,
236 struct bt_component_filter
, parent
);
238 component_input_fini(&filter
->input
);
242 struct bt_component
*bt_component_filter_create(
243 struct bt_component_class
*class, struct bt_value
*params
)
245 struct bt_component_filter
*filter
= NULL
;
246 enum bt_component_status ret
;
248 filter
= g_new0(struct bt_component_filter
, 1);
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
) {
259 if (component_input_init(&filter
->input
)) {
263 return filter
? &filter
->parent
: NULL
;
270 enum bt_component_status
bt_component_filter_validate(
271 struct bt_component
*component
)
273 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
274 struct bt_component_filter
*filter
;
277 ret
= BT_COMPONENT_STATUS_INVALID
;
281 if (!component
->class) {
282 ret
= BT_COMPONENT_STATUS_INVALID
;
286 if (component
->class->type
!= BT_COMPONENT_TYPE_FILTER
) {
287 ret
= BT_COMPONENT_STATUS_INVALID
;
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
;
298 ret
= component_input_validate(&filter
->input
);