4 * Babeltrace Sink Component
6 * Copyright 2015 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-sink-internal.h>
32 #include <babeltrace/component/component-internal.h>
33 #include <babeltrace/component/notification/notification.h>
36 enum bt_component_status
bt_component_sink_validate(
37 struct bt_component
*component
)
39 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
40 struct bt_component_sink
*sink
;
43 ret
= BT_COMPONENT_STATUS_INVALID
;
47 if (!component
->class) {
48 ret
= BT_COMPONENT_STATUS_INVALID
;
52 if (component
->class->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
53 ret
= BT_COMPONENT_STATUS_INVALID
;
57 sink
= container_of(component
, struct bt_component_sink
, parent
);
58 ret
= component_input_validate(&sink
->input
);
67 void bt_component_sink_destroy(struct bt_component
*component
)
69 struct bt_component_sink
*sink
= container_of(component
,
70 struct bt_component_sink
, parent
);
72 component_input_fini(&sink
->input
);
76 struct bt_component
*bt_component_sink_create(
77 struct bt_component_class
*class, struct bt_value
*params
)
79 struct bt_component_sink
*sink
= NULL
;
80 enum bt_component_status ret
;
82 sink
= g_new0(struct bt_component_sink
, 1);
87 sink
->parent
.class = bt_get(class);
88 ret
= bt_component_init(&sink
->parent
, bt_component_sink_destroy
);
89 if (ret
!= BT_COMPONENT_STATUS_OK
) {
94 ret = bt_component_sink_register_notification_type(&sink->parent,
95 BT_NOTIFICATION_TYPE_EVENT);
96 if (ret != BT_COMPONENT_STATUS_OK) {
100 if (component_input_init(&sink
->input
)) {
104 return sink
? &sink
->parent
: NULL
;
111 enum bt_component_status
validate_inputs(struct bt_component_sink
*sink
)
113 size_t array_size
= sink
->input
.iterators
->len
;
115 if (array_size
< sink
->input
.min_count
||
116 array_size
> sink
->input
.max_count
) {
117 return BT_COMPONENT_STATUS_INVALID
;
120 return BT_COMPONENT_STATUS_OK
;
123 enum bt_component_status
bt_component_sink_consume(
124 struct bt_component
*component
)
126 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
127 struct bt_component_sink
*sink
= NULL
;
128 struct bt_component_class_sink
*sink_class
= NULL
;
131 ret
= BT_COMPONENT_STATUS_INVALID
;
135 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_SINK
) {
136 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
140 sink
= container_of(component
, struct bt_component_sink
, parent
);
141 if (!sink
->input
.validated
) {
142 ret
= validate_inputs(sink
);
143 if (ret
!= BT_COMPONENT_STATUS_OK
) {
146 sink
->input
.validated
= true;
149 sink_class
= container_of(component
->class, struct bt_component_class_sink
, parent
);
150 assert(sink_class
->methods
.consume
);
151 ret
= sink_class
->methods
.consume(component
);
157 enum bt_component_status bt_component_sink_register_notification_type(
158 struct bt_component *component, enum bt_notification_type type)
160 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
161 struct bt_component_sink *sink = NULL;
164 ret = BT_COMPONENT_STATUS_INVALID;
168 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
169 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
173 if (type <= BT_NOTIFICATION_TYPE_UNKNOWN ||
174 type >= BT_NOTIFICATION_TYPE_NR) {
175 ret = BT_COMPONENT_STATUS_INVALID;
178 sink = container_of(component, struct bt_component_sink, parent);
179 if (type == BT_NOTIFICATION_TYPE_ALL) {
180 sink->registered_notifications_mask = ~(notification_mask_t) 0;
182 sink->registered_notifications_mask |=
183 (notification_mask_t) 1 << type;
190 enum bt_component_status
bt_component_sink_set_minimum_input_count(
191 struct bt_component
*component
,
192 unsigned int minimum
)
194 struct bt_component_sink
*sink
;
195 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
198 ret
= BT_COMPONENT_STATUS_INVALID
;
202 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_SINK
) {
203 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
207 if (!component
->initializing
) {
208 ret
= BT_COMPONENT_STATUS_INVALID
;
212 sink
= container_of(component
, struct bt_component_sink
, parent
);
213 sink
->input
.min_count
= minimum
;
218 enum bt_component_status
bt_component_sink_set_maximum_input_count(
219 struct bt_component
*component
,
220 unsigned int maximum
)
222 struct bt_component_sink
*sink
;
223 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
226 ret
= BT_COMPONENT_STATUS_INVALID
;
230 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_SINK
) {
231 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
235 if (!component
->initializing
) {
236 ret
= BT_COMPONENT_STATUS_INVALID
;
240 sink
= container_of(component
, struct bt_component_sink
, parent
);
241 sink
->input
.max_count
= maximum
;
246 enum bt_component_status
247 bt_component_sink_get_input_count(struct bt_component
*component
,
250 struct bt_component_sink
*sink
;
251 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
253 if (!component
|| !count
) {
254 ret
= BT_COMPONENT_STATUS_INVALID
;
258 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_SINK
) {
259 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
263 sink
= container_of(component
, struct bt_component_sink
, parent
);
264 *count
= (unsigned int) sink
->input
.iterators
->len
;
269 enum bt_component_status
270 bt_component_sink_get_input_iterator(struct bt_component
*component
,
271 unsigned int input
, struct bt_notification_iterator
**iterator
)
273 struct bt_component_sink
*sink
;
274 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
276 if (!component
|| !iterator
) {
277 ret
= BT_COMPONENT_STATUS_INVALID
;
281 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_SINK
) {
282 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
286 sink
= container_of(component
, struct bt_component_sink
, parent
);
287 if (input
>= (unsigned int) sink
->input
.iterators
->len
) {
288 ret
= BT_COMPONENT_STATUS_INVALID
;
292 *iterator
= bt_get(g_ptr_array_index(sink
->input
.iterators
, input
));
297 enum bt_component_status
298 bt_component_sink_add_iterator(struct bt_component
*component
,
299 struct bt_notification_iterator
*iterator
)
301 struct bt_component_sink
*sink
;
302 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
303 struct bt_component_class_sink
*sink_class
;
305 if (!component
|| !iterator
) {
306 ret
= BT_COMPONENT_STATUS_INVALID
;
310 if (bt_component_get_class_type(component
) != BT_COMPONENT_CLASS_TYPE_SINK
) {
311 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
315 sink
= container_of(component
, struct bt_component_sink
, parent
);
316 if (sink
->input
.iterators
->len
== sink
->input
.max_count
) {
317 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
321 sink_class
= container_of(component
->class, struct bt_component_class_sink
, parent
);
323 if (sink_class
->methods
.add_iterator
) {
324 ret
= sink_class
->methods
.add_iterator(component
, iterator
);
325 if (ret
!= BT_COMPONENT_STATUS_OK
) {
330 g_ptr_array_add(sink
->input
.iterators
, bt_get(iterator
));