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