Prefix {source,filter,sink}*.h file names with component-
[babeltrace.git] / lib / component / sink.c
CommitLineData
0d884c50
JG
1/*
2 * sink.c
3 *
47e5a032 4 * Babeltrace Sink Component
0d884c50
JG
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29#include <babeltrace/compiler.h>
fec2a9f2 30#include <babeltrace/values.h>
d71dcf2c 31#include <babeltrace/component/component-sink-internal.h>
33b34c43
PP
32#include <babeltrace/component/component-internal.h>
33#include <babeltrace/component/notification/notification.h>
0d884c50 34
7c7c0433
JG
35BT_HIDDEN
36enum bt_component_status bt_component_sink_validate(
37 struct bt_component *component)
38{
9defb2e2
JG
39 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
40 struct bt_component_sink *sink;
41
4f0a761c
JG
42 if (!component) {
43 ret = BT_COMPONENT_STATUS_INVALID;
44 goto end;
45 }
46
47 if (!component->class) {
48 ret = BT_COMPONENT_STATUS_INVALID;
49 goto end;
50 }
51
d3e4dcd8 52 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
4f0a761c
JG
53 ret = BT_COMPONENT_STATUS_INVALID;
54 goto end;
55 }
56
9defb2e2 57 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19
JG
58 ret = component_input_validate(&sink->input);
59 if (ret) {
9defb2e2
JG
60 goto end;
61 }
62end:
63 return ret;
0d884c50
JG
64}
65
fec2a9f2
JG
66static
67void bt_component_sink_destroy(struct bt_component *component)
68{
69 struct bt_component_sink *sink = container_of(component,
70 struct bt_component_sink, parent);
71
34a9ed19 72 component_input_fini(&sink->input);
fec2a9f2
JG
73}
74
8738a040 75BT_HIDDEN
fb2dcc52 76struct bt_component *bt_component_sink_create(
7c7c0433 77 struct bt_component_class *class, struct bt_value *params)
0d884c50
JG
78{
79 struct bt_component_sink *sink = NULL;
80 enum bt_component_status ret;
81
0d884c50
JG
82 sink = g_new0(struct bt_component_sink, 1);
83 if (!sink) {
84 goto end;
85 }
86
9e9504c7 87 sink->parent.class = bt_get(class);
fec2a9f2 88 ret = bt_component_init(&sink->parent, bt_component_sink_destroy);
0d884c50 89 if (ret != BT_COMPONENT_STATUS_OK) {
9e9504c7
JG
90 goto error;
91 }
92
fec2a9f2 93/*
9e9504c7
JG
94 ret = bt_component_sink_register_notification_type(&sink->parent,
95 BT_NOTIFICATION_TYPE_EVENT);
96 if (ret != BT_COMPONENT_STATUS_OK) {
97 goto error;
0d884c50 98 }
fec2a9f2 99*/
34a9ed19 100 if (component_input_init(&sink->input)) {
fec2a9f2
JG
101 goto error;
102 }
0d884c50
JG
103end:
104 return sink ? &sink->parent : NULL;
9e9504c7
JG
105error:
106 BT_PUT(sink);
107 return NULL;
0d884c50 108}
fa55ed98 109
fec2a9f2
JG
110static
111enum bt_component_status validate_inputs(struct bt_component_sink *sink)
112{
34a9ed19 113 size_t array_size = sink->input.iterators->len;
fec2a9f2 114
34a9ed19
JG
115 if (array_size < sink->input.min_count ||
116 array_size > sink->input.max_count) {
fec2a9f2
JG
117 return BT_COMPONENT_STATUS_INVALID;
118 }
119
120 return BT_COMPONENT_STATUS_OK;
121}
122
123enum bt_component_status bt_component_sink_consume(
124 struct bt_component *component)
fa55ed98
JG
125{
126 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
127 struct bt_component_sink *sink = NULL;
d3e4dcd8 128 struct bt_component_class_sink *sink_class = NULL;
fa55ed98 129
fec2a9f2 130 if (!component) {
30d619df 131 ret = BT_COMPONENT_STATUS_INVALID;
fa55ed98
JG
132 goto end;
133 }
134
d3e4dcd8 135 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
fa55ed98
JG
136 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
137 goto end;
138 }
139
140 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19 141 if (!sink->input.validated) {
fec2a9f2
JG
142 ret = validate_inputs(sink);
143 if (ret != BT_COMPONENT_STATUS_OK) {
144 goto end;
145 }
34a9ed19 146 sink->input.validated = true;
fec2a9f2
JG
147 }
148
d3e4dcd8
PP
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);
fa55ed98
JG
152end:
153 return ret;
154}
fec2a9f2
JG
155/*
156static
30d619df
JG
157enum bt_component_status bt_component_sink_register_notification_type(
158 struct bt_component *component, enum bt_notification_type type)
159{
160 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
161 struct bt_component_sink *sink = NULL;
162
163 if (!component) {
164 ret = BT_COMPONENT_STATUS_INVALID;
165 goto end;
166 }
167
d3e4dcd8 168 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
30d619df
JG
169 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
170 goto end;
171 }
172
173 if (type <= BT_NOTIFICATION_TYPE_UNKNOWN ||
174 type >= BT_NOTIFICATION_TYPE_NR) {
175 ret = BT_COMPONENT_STATUS_INVALID;
176 goto end;
177 }
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;
181 } else {
182 sink->registered_notifications_mask |=
183 (notification_mask_t) 1 << type;
184 }
185end:
186 return ret;
187}
fec2a9f2 188*/
34a9ed19 189
fec2a9f2 190enum bt_component_status bt_component_sink_set_minimum_input_count(
952ebade 191 struct bt_component *component,
fec2a9f2 192 unsigned int minimum)
952ebade 193{
fec2a9f2 194 struct bt_component_sink *sink;
952ebade 195 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
952ebade
JG
196
197 if (!component) {
198 ret = BT_COMPONENT_STATUS_INVALID;
199 goto end;
200 }
201
d3e4dcd8 202 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
952ebade
JG
203 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
204 goto end;
205 }
206
fec2a9f2
JG
207 if (!component->initializing) {
208 ret = BT_COMPONENT_STATUS_INVALID;
209 goto end;
210 }
211
952ebade 212 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19 213 sink->input.min_count = minimum;
fec2a9f2
JG
214end:
215 return ret;
216}
217
218enum bt_component_status bt_component_sink_set_maximum_input_count(
219 struct bt_component *component,
220 unsigned int maximum)
221{
222 struct bt_component_sink *sink;
223 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
224
225 if (!component) {
226 ret = BT_COMPONENT_STATUS_INVALID;
227 goto end;
228 }
229
d3e4dcd8 230 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
fec2a9f2
JG
231 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
232 goto end;
233 }
234
235 if (!component->initializing) {
236 ret = BT_COMPONENT_STATUS_INVALID;
237 goto end;
238 }
239
240 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19 241 sink->input.max_count = maximum;
fec2a9f2
JG
242end:
243 return ret;
244}
245
246enum bt_component_status
247bt_component_sink_get_input_count(struct bt_component *component,
248 unsigned int *count)
249{
250 struct bt_component_sink *sink;
251 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
252
253 if (!component || !count) {
254 ret = BT_COMPONENT_STATUS_INVALID;
255 goto end;
256 }
257
d3e4dcd8 258 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
fec2a9f2
JG
259 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
260 goto end;
261 }
262
263 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19 264 *count = (unsigned int) sink->input.iterators->len;
fec2a9f2
JG
265end:
266 return ret;
267}
268
269enum bt_component_status
270bt_component_sink_get_input_iterator(struct bt_component *component,
271 unsigned int input, struct bt_notification_iterator **iterator)
272{
273 struct bt_component_sink *sink;
274 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
275
276 if (!component || !iterator) {
277 ret = BT_COMPONENT_STATUS_INVALID;
278 goto end;
279 }
280
d3e4dcd8 281 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
fec2a9f2
JG
282 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
283 goto end;
284 }
285
286 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19 287 if (input >= (unsigned int) sink->input.iterators->len) {
fec2a9f2
JG
288 ret = BT_COMPONENT_STATUS_INVALID;
289 goto end;
290 }
291
34a9ed19 292 *iterator = bt_get(g_ptr_array_index(sink->input.iterators, input));
fec2a9f2
JG
293end:
294 return ret;
295}
296
297enum bt_component_status
298bt_component_sink_add_iterator(struct bt_component *component,
299 struct bt_notification_iterator *iterator)
300{
301 struct bt_component_sink *sink;
302 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
d3e4dcd8 303 struct bt_component_class_sink *sink_class;
fec2a9f2
JG
304
305 if (!component || !iterator) {
306 ret = BT_COMPONENT_STATUS_INVALID;
307 goto end;
308 }
309
d3e4dcd8 310 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
fec2a9f2
JG
311 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
312 goto end;
313 }
314
315 sink = container_of(component, struct bt_component_sink, parent);
34a9ed19 316 if (sink->input.iterators->len == sink->input.max_count) {
fec2a9f2
JG
317 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
318 goto end;
319 }
320
d3e4dcd8
PP
321 sink_class = container_of(component->class, struct bt_component_class_sink, parent);
322
323 if (sink_class->methods.add_iterator) {
324 ret = sink_class->methods.add_iterator(component, iterator);
fec2a9f2
JG
325 if (ret != BT_COMPONENT_STATUS_OK) {
326 goto end;
327 }
328 }
329
34a9ed19 330 g_ptr_array_add(sink->input.iterators, bt_get(iterator));
952ebade
JG
331end:
332 return ret;
333}
This page took 0.040094 seconds and 4 git commands to generate.