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
;
42 sink
= container_of(component
, struct bt_component_sink
, parent
);
44 printf_error("Invalid sink component; no notification consumption callback defined.");
45 ret
= BT_COMPONENT_STATUS_INVALID
;
49 ret
= component_input_validate(&sink
->input
);
58 void bt_component_sink_destroy(struct bt_component
*component
)
60 struct bt_component_sink
*sink
= container_of(component
,
61 struct bt_component_sink
, parent
);
63 component_input_fini(&sink
->input
);
67 struct bt_component
*bt_component_sink_create(
68 struct bt_component_class
*class, struct bt_value
*params
)
70 struct bt_component_sink
*sink
= NULL
;
71 enum bt_component_status ret
;
73 sink
= g_new0(struct bt_component_sink
, 1);
78 sink
->parent
.class = bt_get(class);
79 ret
= bt_component_init(&sink
->parent
, bt_component_sink_destroy
);
80 if (ret
!= BT_COMPONENT_STATUS_OK
) {
85 ret = bt_component_sink_register_notification_type(&sink->parent,
86 BT_NOTIFICATION_TYPE_EVENT);
87 if (ret != BT_COMPONENT_STATUS_OK) {
91 if (component_input_init(&sink
->input
)) {
95 return sink
? &sink
->parent
: NULL
;
102 enum bt_component_status
validate_inputs(struct bt_component_sink
*sink
)
104 size_t array_size
= sink
->input
.iterators
->len
;
106 if (array_size
< sink
->input
.min_count
||
107 array_size
> sink
->input
.max_count
) {
108 return BT_COMPONENT_STATUS_INVALID
;
111 return BT_COMPONENT_STATUS_OK
;
114 enum bt_component_status
bt_component_sink_consume(
115 struct bt_component
*component
)
117 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
118 struct bt_component_sink
*sink
= NULL
;
121 ret
= BT_COMPONENT_STATUS_INVALID
;
125 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
126 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
130 sink
= container_of(component
, struct bt_component_sink
, parent
);
131 if (!sink
->input
.validated
) {
132 ret
= validate_inputs(sink
);
133 if (ret
!= BT_COMPONENT_STATUS_OK
) {
136 sink
->input
.validated
= true;
139 assert(sink
->consume
);
140 ret
= sink
->consume(component
);
146 enum bt_component_status bt_component_sink_register_notification_type(
147 struct bt_component *component, enum bt_notification_type type)
149 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
150 struct bt_component_sink *sink = NULL;
153 ret = BT_COMPONENT_STATUS_INVALID;
157 if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) {
158 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
162 if (type <= BT_NOTIFICATION_TYPE_UNKNOWN ||
163 type >= BT_NOTIFICATION_TYPE_NR) {
164 ret = BT_COMPONENT_STATUS_INVALID;
167 sink = container_of(component, struct bt_component_sink, parent);
168 if (type == BT_NOTIFICATION_TYPE_ALL) {
169 sink->registered_notifications_mask = ~(notification_mask_t) 0;
171 sink->registered_notifications_mask |=
172 (notification_mask_t) 1 << type;
178 enum bt_component_status
bt_component_sink_set_consume_cb(
179 struct bt_component
*component
,
180 bt_component_sink_consume_cb consume
)
182 struct bt_component_sink
*sink
;
183 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
186 ret
= BT_COMPONENT_STATUS_INVALID
;
190 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
191 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
195 if (!component
->initializing
) {
196 ret
= BT_COMPONENT_STATUS_INVALID
;
200 sink
= container_of(component
, struct bt_component_sink
, parent
);
201 sink
->consume
= consume
;
206 enum bt_component_status
bt_component_sink_set_add_iterator_cb(
207 struct bt_component
*component
,
208 bt_component_sink_add_iterator_cb add_iterator
)
210 struct bt_component_sink
*sink
;
211 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
214 ret
= BT_COMPONENT_STATUS_INVALID
;
218 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
219 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
223 if (!component
->initializing
) {
224 ret
= BT_COMPONENT_STATUS_INVALID
;
228 sink
= container_of(component
, struct bt_component_sink
, parent
);
229 sink
->add_iterator
= add_iterator
;
234 enum bt_component_status
bt_component_sink_set_minimum_input_count(
235 struct bt_component
*component
,
236 unsigned int minimum
)
238 struct bt_component_sink
*sink
;
239 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
242 ret
= BT_COMPONENT_STATUS_INVALID
;
246 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
247 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
251 if (!component
->initializing
) {
252 ret
= BT_COMPONENT_STATUS_INVALID
;
256 sink
= container_of(component
, struct bt_component_sink
, parent
);
257 sink
->input
.min_count
= minimum
;
262 enum bt_component_status
bt_component_sink_set_maximum_input_count(
263 struct bt_component
*component
,
264 unsigned int maximum
)
266 struct bt_component_sink
*sink
;
267 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
270 ret
= BT_COMPONENT_STATUS_INVALID
;
274 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
275 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
279 if (!component
->initializing
) {
280 ret
= BT_COMPONENT_STATUS_INVALID
;
284 sink
= container_of(component
, struct bt_component_sink
, parent
);
285 sink
->input
.max_count
= maximum
;
290 enum bt_component_status
291 bt_component_sink_get_input_count(struct bt_component
*component
,
294 struct bt_component_sink
*sink
;
295 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
297 if (!component
|| !count
) {
298 ret
= BT_COMPONENT_STATUS_INVALID
;
302 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
303 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
307 sink
= container_of(component
, struct bt_component_sink
, parent
);
308 *count
= (unsigned int) sink
->input
.iterators
->len
;
313 enum bt_component_status
314 bt_component_sink_get_input_iterator(struct bt_component
*component
,
315 unsigned int input
, struct bt_notification_iterator
**iterator
)
317 struct bt_component_sink
*sink
;
318 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
320 if (!component
|| !iterator
) {
321 ret
= BT_COMPONENT_STATUS_INVALID
;
325 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
326 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
330 sink
= container_of(component
, struct bt_component_sink
, parent
);
331 if (input
>= (unsigned int) sink
->input
.iterators
->len
) {
332 ret
= BT_COMPONENT_STATUS_INVALID
;
336 *iterator
= bt_get(g_ptr_array_index(sink
->input
.iterators
, input
));
341 enum bt_component_status
342 bt_component_sink_add_iterator(struct bt_component
*component
,
343 struct bt_notification_iterator
*iterator
)
345 struct bt_component_sink
*sink
;
346 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
348 if (!component
|| !iterator
) {
349 ret
= BT_COMPONENT_STATUS_INVALID
;
353 if (bt_component_get_type(component
) != BT_COMPONENT_TYPE_SINK
) {
354 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
358 sink
= container_of(component
, struct bt_component_sink
, parent
);
359 if (sink
->input
.iterators
->len
== sink
->input
.max_count
) {
360 ret
= BT_COMPONENT_STATUS_UNSUPPORTED
;
364 if (sink
->add_iterator
) {
365 ret
= sink
->add_iterator(component
, iterator
);
366 if (ret
!= BT_COMPONENT_STATUS_OK
) {
371 g_ptr_array_add(sink
->input
.iterators
, bt_get(iterator
));