Fix verbose and debug mode options
[babeltrace.git] / lib / plugin-system / sink.c
1 /*
2 * sink.c
3 *
4 * Babeltrace Sink Component
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>
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>
34
35 BT_HIDDEN
36 enum bt_component_status bt_component_sink_validate(
37 struct bt_component *component)
38 {
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);
43 if (!sink->consume) {
44 printf_error("Invalid sink component; no notification consumption callback defined.");
45 ret = BT_COMPONENT_STATUS_INVALID;
46 goto end;
47 }
48
49 ret = component_input_validate(&sink->input);
50 if (ret) {
51 goto end;
52 }
53 end:
54 return ret;
55 }
56
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
63 component_input_fini(&sink->input);
64 }
65
66 BT_HIDDEN
67 struct bt_component *bt_component_sink_create(
68 struct bt_component_class *class, struct bt_value *params)
69 {
70 struct bt_component_sink *sink = NULL;
71 enum bt_component_status ret;
72
73 sink = g_new0(struct bt_component_sink, 1);
74 if (!sink) {
75 goto end;
76 }
77
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) {
81 goto error;
82 }
83
84 /*
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;
89 }
90 */
91 if (component_input_init(&sink->input)) {
92 goto error;
93 }
94 end:
95 return sink ? &sink->parent : NULL;
96 error:
97 BT_PUT(sink);
98 return NULL;
99 }
100
101 static
102 enum bt_component_status validate_inputs(struct bt_component_sink *sink)
103 {
104 size_t array_size = sink->input.iterators->len;
105
106 if (array_size < sink->input.min_count ||
107 array_size > sink->input.max_count) {
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)
116 {
117 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
118 struct bt_component_sink *sink = NULL;
119
120 if (!component) {
121 ret = BT_COMPONENT_STATUS_INVALID;
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);
131 if (!sink->input.validated) {
132 ret = validate_inputs(sink);
133 if (ret != BT_COMPONENT_STATUS_OK) {
134 goto end;
135 }
136 sink->input.validated = true;
137 }
138
139 assert(sink->consume);
140 ret = sink->consume(component);
141 end:
142 return ret;
143 }
144 /*
145 static
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 }
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;
184
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
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
234 enum bt_component_status bt_component_sink_set_minimum_input_count(
235 struct bt_component *component,
236 unsigned int minimum)
237 {
238 struct bt_component_sink *sink;
239 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
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
251 if (!component->initializing) {
252 ret = BT_COMPONENT_STATUS_INVALID;
253 goto end;
254 }
255
256 sink = container_of(component, struct bt_component_sink, parent);
257 sink->input.min_count = minimum;
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);
285 sink->input.max_count = maximum;
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);
308 *count = (unsigned int) sink->input.iterators->len;
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);
331 if (input >= (unsigned int) sink->input.iterators->len) {
332 ret = BT_COMPONENT_STATUS_INVALID;
333 goto end;
334 }
335
336 *iterator = bt_get(g_ptr_array_index(sink->input.iterators, input));
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);
359 if (sink->input.iterators->len == sink->input.max_count) {
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
371 g_ptr_array_add(sink->input.iterators, bt_get(iterator));
372 end:
373 return ret;
374 }
This page took 0.104317 seconds and 4 git commands to generate.