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/plugin/sink-internal.h>
32 #include <babeltrace/plugin/component-internal.h>
33 #include <babeltrace/plugin/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_TYPE_SINK
) {
53 ret
= BT_COMPONENT_STATUS_INVALID
;
57 sink
= container_of(component
, struct bt_component_sink
, parent
);
59 printf_error("Invalid sink component; no notification consumption callback defined.");
60 ret
= BT_COMPONENT_STATUS_INVALID
;
64 ret
= component_input_validate(&sink
->input
);
73 void bt_component_sink_destroy(struct bt_component
*component
)
75 struct bt_component_sink
*sink
= container_of(component
,
76 struct bt_component_sink
, parent
);
78 component_input_fini(&sink
->input
);
82 struct bt_component
*bt_component_sink_create(
83 struct bt_component_class
*class, struct bt_value
*params
)
85 struct bt_component_sink
*sink
= NULL
;
86 enum bt_component_status ret
;
88 sink
= g_new0(struct bt_component_sink
, 1);
93 sink
->parent
.class = bt_get(class);
94 ret
= bt_component_init(&sink
->parent
, bt_component_sink_destroy
);
95 if (ret
!= BT_COMPONENT_STATUS_OK
) {
100 ret = bt_component_sink_register_notification_type(&sink->parent,
101 BT_NOTIFICATION_TYPE_EVENT);
102 if (ret != BT_COMPONENT_STATUS_OK) {
106 if (component_input_init(&sink
->input
)) {
110 return sink
? &sink
->parent
: NULL
;
117 enum bt_component_status
validate_inputs(struct bt_component_sink
*sink
)
119 size_t array_size
= sink
->input
.iterators
->len
;
121 if (array_size
< sink
->input
.min_count
||
122 array_size
> sink
->input
.max_count
) {
123 return BT_COMPONENT_STATUS_INVALID
;
126 return BT_COMPONENT_STATUS_OK
;
129 enum bt_component_status
bt_component_sink_consume(
130 struct bt_component
*component
)
132 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
133 struct bt_component_sink
*sink
= NULL
;
136 ret
= BT_COMPONENT_STATUS_INVALID
;
140 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
141 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
145 sink
= container_of(component
, struct bt_component_sink
, parent
);
146 if (!sink
->input
.validated
) {
147 ret
= validate_inputs(sink
);
148 if (ret
!= BT_COMPONENT_STATUS_OK
) {
151 sink
->input
.validated
= true;
154 assert(sink
->consume
);
155 ret
= sink
->consume(component
);
161 enum bt_component_status bt_component_sink_register_notification_type(
162 struct bt_component *component, enum bt_notification_type type)
164 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
165 struct bt_component_sink *sink = NULL;
168 ret = BT_COMPONENT_STATUS_INVALID;
172 if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) {
173 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
177 if (type <= BT_NOTIFICATION_TYPE_UNKNOWN ||
178 type >= BT_NOTIFICATION_TYPE_NR) {
179 ret = BT_COMPONENT_STATUS_INVALID;
182 sink = container_of(component, struct bt_component_sink, parent);
183 if (type == BT_NOTIFICATION_TYPE_ALL) {
184 sink->registered_notifications_mask = ~(notification_mask_t) 0;
186 sink->registered_notifications_mask |=
187 (notification_mask_t) 1 << type;
193 enum bt_component_status
bt_component_sink_set_consume_cb(
194 struct bt_component
*component
,
195 bt_component_sink_consume_cb consume
)
197 struct bt_component_sink
*sink
;
198 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
201 ret
= BT_COMPONENT_STATUS_INVALID
;
205 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
206 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
210 if (!component
->initializing
) {
211 ret
= BT_COMPONENT_STATUS_INVALID
;
215 sink
= container_of(component
, struct bt_component_sink
, parent
);
216 sink
->consume
= consume
;
221 enum bt_component_status
bt_component_sink_set_add_iterator_cb(
222 struct bt_component
*component
,
223 bt_component_sink_add_iterator_cb add_iterator
)
225 struct bt_component_sink
*sink
;
226 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
229 ret
= BT_COMPONENT_STATUS_INVALID
;
233 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
234 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
238 if (!component
->initializing
) {
239 ret
= BT_COMPONENT_STATUS_INVALID
;
243 sink
= container_of(component
, struct bt_component_sink
, parent
);
244 sink
->add_iterator
= add_iterator
;
249 enum bt_component_status
bt_component_sink_set_minimum_input_count(
250 struct bt_component
*component
,
251 unsigned int minimum
)
253 struct bt_component_sink
*sink
;
254 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
257 ret
= BT_COMPONENT_STATUS_INVALID
;
261 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
262 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
266 if (!component
->initializing
) {
267 ret
= BT_COMPONENT_STATUS_INVALID
;
271 sink
= container_of(component
, struct bt_component_sink
, parent
);
272 sink
->input
.min_count
= minimum
;
277 enum bt_component_status
bt_component_sink_set_maximum_input_count(
278 struct bt_component
*component
,
279 unsigned int maximum
)
281 struct bt_component_sink
*sink
;
282 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
285 ret
= BT_COMPONENT_STATUS_INVALID
;
289 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
290 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
294 if (!component
->initializing
) {
295 ret
= BT_COMPONENT_STATUS_INVALID
;
299 sink
= container_of(component
, struct bt_component_sink
, parent
);
300 sink
->input
.max_count
= maximum
;
305 enum bt_component_status
306 bt_component_sink_get_input_count(struct bt_component
*component
,
309 struct bt_component_sink
*sink
;
310 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
312 if (!component
|| !count
) {
313 ret
= BT_COMPONENT_STATUS_INVALID
;
317 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
318 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
322 sink
= container_of(component
, struct bt_component_sink
, parent
);
323 *count
= (unsigned int) sink
->input
.iterators
->len
;
328 enum bt_component_status
329 bt_component_sink_get_input_iterator(struct bt_component
*component
,
330 unsigned int input
, struct bt_notification_iterator
**iterator
)
332 struct bt_component_sink
*sink
;
333 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
335 if (!component
|| !iterator
) {
336 ret
= BT_COMPONENT_STATUS_INVALID
;
340 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
341 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
345 sink
= container_of(component
, struct bt_component_sink
, parent
);
346 if (input
>= (unsigned int) sink
->input
.iterators
->len
) {
347 ret
= BT_COMPONENT_STATUS_INVALID
;
351 *iterator
= bt_get(g_ptr_array_index(sink
->input
.iterators
, input
));
356 enum bt_component_status
357 bt_component_sink_add_iterator(struct bt_component
*component
,
358 struct bt_notification_iterator
*iterator
)
360 struct bt_component_sink
*sink
;
361 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
363 if (!component
|| !iterator
) {
364 ret
= BT_COMPONENT_STATUS_INVALID
;
368 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
369 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
373 sink
= container_of(component
, struct bt_component_sink
, parent
);
374 if (sink
->input
.iterators
->len
== sink
->input
.max_count
) {
375 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
379 if (sink
->add_iterator
) {
380 ret
= sink
->add_iterator(component
, iterator
);
381 if (ret
!= BT_COMPONENT_STATUS_OK
) {
386 g_ptr_array_add(sink
->input
.iterators
, bt_get(iterator
));