Commit | Line | Data |
---|---|---|
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> |
0d884c50 JG |
31 | #include <babeltrace/plugin/sink-internal.h> |
32 | #include <babeltrace/plugin/component-internal.h> | |
30d619df | 33 | #include <babeltrace/plugin/notification/notification.h> |
0d884c50 | 34 | |
7c7c0433 JG |
35 | BT_HIDDEN |
36 | enum 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 | ||
52 | if (component->class->type != BT_COMPONENT_TYPE_SINK) { | |
53 | ret = BT_COMPONENT_STATUS_INVALID; | |
54 | goto end; | |
55 | } | |
56 | ||
9defb2e2 | 57 | sink = container_of(component, struct bt_component_sink, parent); |
fec2a9f2 JG |
58 | if (!sink->consume) { |
59 | printf_error("Invalid sink component; no notification consumption callback defined."); | |
9defb2e2 JG |
60 | ret = BT_COMPONENT_STATUS_INVALID; |
61 | goto end; | |
62 | } | |
63 | ||
34a9ed19 JG |
64 | ret = component_input_validate(&sink->input); |
65 | if (ret) { | |
9defb2e2 JG |
66 | goto end; |
67 | } | |
68 | end: | |
69 | return ret; | |
0d884c50 JG |
70 | } |
71 | ||
fec2a9f2 JG |
72 | static |
73 | void bt_component_sink_destroy(struct bt_component *component) | |
74 | { | |
75 | struct bt_component_sink *sink = container_of(component, | |
76 | struct bt_component_sink, parent); | |
77 | ||
34a9ed19 | 78 | component_input_fini(&sink->input); |
fec2a9f2 JG |
79 | } |
80 | ||
8738a040 | 81 | BT_HIDDEN |
fb2dcc52 | 82 | struct bt_component *bt_component_sink_create( |
7c7c0433 | 83 | struct bt_component_class *class, struct bt_value *params) |
0d884c50 JG |
84 | { |
85 | struct bt_component_sink *sink = NULL; | |
86 | enum bt_component_status ret; | |
87 | ||
0d884c50 JG |
88 | sink = g_new0(struct bt_component_sink, 1); |
89 | if (!sink) { | |
90 | goto end; | |
91 | } | |
92 | ||
9e9504c7 | 93 | sink->parent.class = bt_get(class); |
fec2a9f2 | 94 | ret = bt_component_init(&sink->parent, bt_component_sink_destroy); |
0d884c50 | 95 | if (ret != BT_COMPONENT_STATUS_OK) { |
9e9504c7 JG |
96 | goto error; |
97 | } | |
98 | ||
fec2a9f2 | 99 | /* |
9e9504c7 JG |
100 | ret = bt_component_sink_register_notification_type(&sink->parent, |
101 | BT_NOTIFICATION_TYPE_EVENT); | |
102 | if (ret != BT_COMPONENT_STATUS_OK) { | |
103 | goto error; | |
0d884c50 | 104 | } |
fec2a9f2 | 105 | */ |
34a9ed19 | 106 | if (component_input_init(&sink->input)) { |
fec2a9f2 JG |
107 | goto error; |
108 | } | |
0d884c50 JG |
109 | end: |
110 | return sink ? &sink->parent : NULL; | |
9e9504c7 JG |
111 | error: |
112 | BT_PUT(sink); | |
113 | return NULL; | |
0d884c50 | 114 | } |
fa55ed98 | 115 | |
fec2a9f2 JG |
116 | static |
117 | enum bt_component_status validate_inputs(struct bt_component_sink *sink) | |
118 | { | |
34a9ed19 | 119 | size_t array_size = sink->input.iterators->len; |
fec2a9f2 | 120 | |
34a9ed19 JG |
121 | if (array_size < sink->input.min_count || |
122 | array_size > sink->input.max_count) { | |
fec2a9f2 JG |
123 | return BT_COMPONENT_STATUS_INVALID; |
124 | } | |
125 | ||
126 | return BT_COMPONENT_STATUS_OK; | |
127 | } | |
128 | ||
129 | enum bt_component_status bt_component_sink_consume( | |
130 | struct bt_component *component) | |
fa55ed98 JG |
131 | { |
132 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
133 | struct bt_component_sink *sink = NULL; | |
134 | ||
fec2a9f2 | 135 | if (!component) { |
30d619df | 136 | ret = BT_COMPONENT_STATUS_INVALID; |
fa55ed98 JG |
137 | goto end; |
138 | } | |
139 | ||
140 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
141 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
142 | goto end; | |
143 | } | |
144 | ||
145 | sink = container_of(component, struct bt_component_sink, parent); | |
34a9ed19 | 146 | if (!sink->input.validated) { |
fec2a9f2 JG |
147 | ret = validate_inputs(sink); |
148 | if (ret != BT_COMPONENT_STATUS_OK) { | |
149 | goto end; | |
150 | } | |
34a9ed19 | 151 | sink->input.validated = true; |
fec2a9f2 JG |
152 | } |
153 | ||
154 | assert(sink->consume); | |
155 | ret = sink->consume(component); | |
fa55ed98 JG |
156 | end: |
157 | return ret; | |
158 | } | |
fec2a9f2 JG |
159 | /* |
160 | static | |
30d619df JG |
161 | enum bt_component_status bt_component_sink_register_notification_type( |
162 | struct bt_component *component, enum bt_notification_type type) | |
163 | { | |
164 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
165 | struct bt_component_sink *sink = NULL; | |
166 | ||
167 | if (!component) { | |
168 | ret = BT_COMPONENT_STATUS_INVALID; | |
169 | goto end; | |
170 | } | |
171 | ||
172 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
173 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
174 | goto end; | |
175 | } | |
176 | ||
177 | if (type <= BT_NOTIFICATION_TYPE_UNKNOWN || | |
178 | type >= BT_NOTIFICATION_TYPE_NR) { | |
179 | ret = BT_COMPONENT_STATUS_INVALID; | |
180 | goto end; | |
181 | } | |
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; | |
185 | } else { | |
186 | sink->registered_notifications_mask |= | |
187 | (notification_mask_t) 1 << type; | |
188 | } | |
189 | end: | |
190 | return ret; | |
191 | } | |
fec2a9f2 JG |
192 | */ |
193 | enum bt_component_status bt_component_sink_set_consume_cb( | |
194 | struct bt_component *component, | |
195 | bt_component_sink_consume_cb consume) | |
196 | { | |
197 | struct bt_component_sink *sink; | |
198 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
952ebade | 199 | |
fec2a9f2 JG |
200 | if (!component) { |
201 | ret = BT_COMPONENT_STATUS_INVALID; | |
202 | goto end; | |
203 | } | |
204 | ||
205 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
206 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
207 | goto end; | |
208 | } | |
209 | ||
210 | if (!component->initializing) { | |
211 | ret = BT_COMPONENT_STATUS_INVALID; | |
212 | goto end; | |
213 | } | |
214 | ||
215 | sink = container_of(component, struct bt_component_sink, parent); | |
216 | sink->consume = consume; | |
217 | end: | |
218 | return ret; | |
219 | } | |
220 | ||
34a9ed19 JG |
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) | |
224 | { | |
225 | struct bt_component_sink *sink; | |
226 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
227 | ||
228 | if (!component) { | |
229 | ret = BT_COMPONENT_STATUS_INVALID; | |
230 | goto end; | |
231 | } | |
232 | ||
233 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
234 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
235 | goto end; | |
236 | } | |
237 | ||
238 | if (!component->initializing) { | |
239 | ret = BT_COMPONENT_STATUS_INVALID; | |
240 | goto end; | |
241 | } | |
242 | ||
243 | sink = container_of(component, struct bt_component_sink, parent); | |
244 | sink->add_iterator = add_iterator; | |
245 | end: | |
246 | return ret; | |
247 | } | |
248 | ||
fec2a9f2 | 249 | enum bt_component_status bt_component_sink_set_minimum_input_count( |
952ebade | 250 | struct bt_component *component, |
fec2a9f2 | 251 | unsigned int minimum) |
952ebade | 252 | { |
fec2a9f2 | 253 | struct bt_component_sink *sink; |
952ebade | 254 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
952ebade JG |
255 | |
256 | if (!component) { | |
257 | ret = BT_COMPONENT_STATUS_INVALID; | |
258 | goto end; | |
259 | } | |
260 | ||
261 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
262 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
263 | goto end; | |
264 | } | |
265 | ||
fec2a9f2 JG |
266 | if (!component->initializing) { |
267 | ret = BT_COMPONENT_STATUS_INVALID; | |
268 | goto end; | |
269 | } | |
270 | ||
952ebade | 271 | sink = container_of(component, struct bt_component_sink, parent); |
34a9ed19 | 272 | sink->input.min_count = minimum; |
fec2a9f2 JG |
273 | end: |
274 | return ret; | |
275 | } | |
276 | ||
277 | enum bt_component_status bt_component_sink_set_maximum_input_count( | |
278 | struct bt_component *component, | |
279 | unsigned int maximum) | |
280 | { | |
281 | struct bt_component_sink *sink; | |
282 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
283 | ||
284 | if (!component) { | |
285 | ret = BT_COMPONENT_STATUS_INVALID; | |
286 | goto end; | |
287 | } | |
288 | ||
289 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
290 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
291 | goto end; | |
292 | } | |
293 | ||
294 | if (!component->initializing) { | |
295 | ret = BT_COMPONENT_STATUS_INVALID; | |
296 | goto end; | |
297 | } | |
298 | ||
299 | sink = container_of(component, struct bt_component_sink, parent); | |
34a9ed19 | 300 | sink->input.max_count = maximum; |
fec2a9f2 JG |
301 | end: |
302 | return ret; | |
303 | } | |
304 | ||
305 | enum bt_component_status | |
306 | bt_component_sink_get_input_count(struct bt_component *component, | |
307 | unsigned int *count) | |
308 | { | |
309 | struct bt_component_sink *sink; | |
310 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
311 | ||
312 | if (!component || !count) { | |
313 | ret = BT_COMPONENT_STATUS_INVALID; | |
314 | goto end; | |
315 | } | |
316 | ||
317 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
318 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
319 | goto end; | |
320 | } | |
321 | ||
322 | sink = container_of(component, struct bt_component_sink, parent); | |
34a9ed19 | 323 | *count = (unsigned int) sink->input.iterators->len; |
fec2a9f2 JG |
324 | end: |
325 | return ret; | |
326 | } | |
327 | ||
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) | |
331 | { | |
332 | struct bt_component_sink *sink; | |
333 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
334 | ||
335 | if (!component || !iterator) { | |
336 | ret = BT_COMPONENT_STATUS_INVALID; | |
337 | goto end; | |
338 | } | |
339 | ||
340 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
341 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
342 | goto end; | |
343 | } | |
344 | ||
345 | sink = container_of(component, struct bt_component_sink, parent); | |
34a9ed19 | 346 | if (input >= (unsigned int) sink->input.iterators->len) { |
fec2a9f2 JG |
347 | ret = BT_COMPONENT_STATUS_INVALID; |
348 | goto end; | |
349 | } | |
350 | ||
34a9ed19 | 351 | *iterator = bt_get(g_ptr_array_index(sink->input.iterators, input)); |
fec2a9f2 JG |
352 | end: |
353 | return ret; | |
354 | } | |
355 | ||
356 | enum bt_component_status | |
357 | bt_component_sink_add_iterator(struct bt_component *component, | |
358 | struct bt_notification_iterator *iterator) | |
359 | { | |
360 | struct bt_component_sink *sink; | |
361 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
362 | ||
363 | if (!component || !iterator) { | |
364 | ret = BT_COMPONENT_STATUS_INVALID; | |
365 | goto end; | |
366 | } | |
367 | ||
368 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { | |
369 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
370 | goto end; | |
371 | } | |
372 | ||
373 | sink = container_of(component, struct bt_component_sink, parent); | |
34a9ed19 | 374 | if (sink->input.iterators->len == sink->input.max_count) { |
fec2a9f2 JG |
375 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
376 | goto end; | |
377 | } | |
378 | ||
379 | if (sink->add_iterator) { | |
380 | ret = sink->add_iterator(component, iterator); | |
381 | if (ret != BT_COMPONENT_STATUS_OK) { | |
382 | goto end; | |
383 | } | |
384 | } | |
385 | ||
34a9ed19 | 386 | g_ptr_array_add(sink->input.iterators, bt_get(iterator)); |
952ebade JG |
387 | end: |
388 | return ret; | |
389 | } |