Add ports to the source, filter and sink component interfaces
[babeltrace.git] / lib / component / filter.c
1 /*
2 * filter.c
3 *
4 * Babeltrace Filter Component
5 *
6 * Copyright 2016 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/component/component-filter-internal.h>
32 #include <babeltrace/component/component-internal.h>
33 #include <babeltrace/component/component-class-internal.h>
34 #include <babeltrace/component/notification/notification.h>
35 #include <babeltrace/component/notification/iterator-internal.h>
36
37 enum bt_component_status bt_component_filter_add_iterator(
38 struct bt_component *component,
39 struct bt_notification_iterator *iterator)
40 {
41 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
42 struct bt_component_class_filter *filter_class;
43
44 if (!component || !iterator) {
45 ret = BT_COMPONENT_STATUS_INVALID;
46 goto end;
47 }
48
49 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_FILTER) {
50 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
51 goto end;
52 }
53
54 /* TODO validate iterator count limits. */
55
56 filter_class = container_of(component->class,
57 struct bt_component_class_filter, parent);
58 if (filter_class->methods.add_iterator) {
59 ret = filter_class->methods.add_iterator(component, iterator);
60 if (ret != BT_COMPONENT_STATUS_OK) {
61 goto end;
62 }
63 }
64
65 end:
66 return ret;
67 }
68
69 struct bt_notification_iterator *bt_component_filter_create_notification_iterator(
70 struct bt_component *component)
71 {
72 return bt_component_create_iterator(component, NULL);
73 }
74
75 struct bt_notification_iterator *bt_component_filter_create_notification_iterator_with_init_method_data(
76 struct bt_component *component, void *init_method_data)
77 {
78 return bt_component_create_iterator(component, init_method_data);
79 }
80
81 static
82 void bt_component_filter_destroy(struct bt_component *component)
83 {
84 struct bt_component_filter *filter = container_of(component,
85 struct bt_component_filter, parent);
86
87 if (filter->input_ports) {
88 g_ptr_array_free(filter->input_ports, TRUE);
89 }
90
91 if (filter->output_ports) {
92 g_ptr_array_free(filter->output_ports, TRUE);
93 }
94 }
95
96 BT_HIDDEN
97 struct bt_component *bt_component_filter_create(
98 struct bt_component_class *class, struct bt_value *params)
99 {
100 int ret;
101 struct bt_component_filter *filter = NULL;
102 enum bt_component_status status;
103
104 filter = g_new0(struct bt_component_filter, 1);
105 if (!filter) {
106 goto end;
107 }
108
109 filter->parent.class = bt_get(class);
110 status = bt_component_init(&filter->parent, bt_component_filter_destroy);
111 if (status != BT_COMPONENT_STATUS_OK) {
112 goto error;
113 }
114
115 ret = bt_component_init_input_ports(&filter->parent,
116 &filter->input_ports);
117 if (ret) {
118 goto error;
119 }
120
121 ret = bt_component_init_output_ports(&filter->parent,
122 &filter->output_ports);
123 if (ret) {
124 goto error;
125 }
126
127 end:
128 return filter ? &filter->parent : NULL;
129 error:
130 BT_PUT(filter);
131 goto end;
132 }
133
134 BT_HIDDEN
135 enum bt_component_status bt_component_filter_validate(
136 struct bt_component *component)
137 {
138 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
139
140 if (!component) {
141 ret = BT_COMPONENT_STATUS_INVALID;
142 goto end;
143 }
144
145 if (!component->class) {
146 ret = BT_COMPONENT_STATUS_INVALID;
147 goto end;
148 }
149
150 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
151 ret = BT_COMPONENT_STATUS_INVALID;
152 goto end;
153 }
154
155 /* Enforce iterator limits. */
156 end:
157 return ret;
158 }
159
160 int bt_component_filter_get_input_port_count(struct bt_component *component)
161 {
162 int ret;
163 struct bt_component_filter *filter;
164
165 if (!component) {
166 ret = -1;
167 goto end;
168 }
169
170 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
171 ret = -1;
172 goto end;
173 }
174
175 filter = container_of(component, struct bt_component_filter, parent);
176 ret = filter->input_ports->len;
177 end:
178 return ret;
179 }
180
181 struct bt_port *bt_component_filter_get_input_port(
182 struct bt_component *component, const char *name)
183 {
184 struct bt_component_filter *filter;
185 struct bt_port *ret_port = NULL;
186
187 if (!component || !name ||
188 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
189 goto end;
190 }
191
192 filter = container_of(component, struct bt_component_filter, parent);
193 ret_port = bt_component_get_port(filter->input_ports, name);
194 end:
195 return ret_port;
196 }
197
198 struct bt_port *bt_component_filter_get_input_port_at_index(
199 struct bt_component *component, int index)
200 {
201 struct bt_port *port = NULL;
202 struct bt_component_filter *filter;
203
204 if (!component ||
205 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
206 goto end;
207 }
208
209 filter = container_of(component, struct bt_component_filter, parent);
210 port = bt_component_get_port_at_index(filter->input_ports, index);
211 end:
212 return port;
213 }
214
215 struct bt_port *bt_component_filter_get_default_input_port(
216 struct bt_component *component)
217 {
218 return bt_component_filter_get_input_port(component,
219 DEFAULT_INPUT_PORT_NAME);
220 }
221
222 struct bt_port *bt_component_filter_add_input_port(
223 struct bt_component *component, const char *name)
224 {
225 struct bt_port *port;
226 struct bt_component_filter *filter;
227
228 if (!component ||
229 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
230 port = NULL;
231 goto end;
232 }
233
234 filter = container_of(component, struct bt_component_filter, parent);
235 port = bt_component_add_port(component, filter->input_ports,
236 BT_PORT_TYPE_INPUT, name);
237 end:
238 return port;
239 }
240
241 enum bt_component_status bt_component_filter_remove_input_port(
242 struct bt_component *component, const char *name)
243 {
244 enum bt_component_status status;
245 struct bt_component_filter *filter;
246
247 if (!component ||
248 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
249 status = BT_COMPONENT_STATUS_INVALID;
250 goto end;
251 }
252
253 filter = container_of(component, struct bt_component_filter, parent);
254 status = bt_component_remove_port(component, filter->input_ports,
255 name);
256 end:
257 return status;
258 }
259
260 int bt_component_filter_get_output_port_count(struct bt_component *component)
261 {
262 int ret;
263 struct bt_component_filter *filter;
264
265 if (!component) {
266 ret = -1;
267 goto end;
268 }
269
270 if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
271 ret = -1;
272 goto end;
273 }
274
275 filter = container_of(component, struct bt_component_filter, parent);
276 ret = filter->output_ports->len;
277 end:
278 return ret;
279 }
280
281 struct bt_port *bt_component_filter_get_output_port(
282 struct bt_component *component, const char *name)
283 {
284 struct bt_component_filter *filter;
285 struct bt_port *ret_port = NULL;
286
287 if (!component || !name ||
288 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
289 goto end;
290 }
291
292 filter = container_of(component, struct bt_component_filter, parent);
293 ret_port = bt_component_get_port(filter->output_ports, name);
294 end:
295 return ret_port;
296 }
297
298 struct bt_port *bt_component_filter_get_output_port_at_index(
299 struct bt_component *component, int index)
300 {
301 struct bt_port *port = NULL;
302 struct bt_component_filter *filter;
303
304 if (!component ||
305 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
306 goto end;
307 }
308
309 filter = container_of(component, struct bt_component_filter, parent);
310 port = bt_component_get_port_at_index(filter->output_ports, index);
311 end:
312 return port;
313 }
314
315 struct bt_port *bt_component_filter_get_default_output_port(
316 struct bt_component *component)
317 {
318 return bt_component_filter_get_output_port(component,
319 DEFAULT_OUTPUT_PORT_NAME);
320 }
321
322 struct bt_port *bt_component_filter_add_output_port(
323 struct bt_component *component, const char *name)
324 {
325 struct bt_port *port;
326 struct bt_component_filter *filter;
327
328 if (!component ||
329 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
330 port = NULL;
331 goto end;
332 }
333
334 filter = container_of(component, struct bt_component_filter, parent);
335 port = bt_component_add_port(component, filter->output_ports,
336 BT_PORT_TYPE_OUTPUT, name);
337 end:
338 return port;
339 }
340
341 enum bt_component_status bt_component_filter_remove_output_port(
342 struct bt_component *component, const char *name)
343 {
344 enum bt_component_status status;
345 struct bt_component_filter *filter;
346
347 if (!component ||
348 component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) {
349 status = BT_COMPONENT_STATUS_INVALID;
350 goto end;
351 }
352
353 filter = container_of(component, struct bt_component_filter, parent);
354 status = bt_component_remove_port(component, filter->output_ports,
355 name);
356 end:
357 return status;
358 }
This page took 0.035744 seconds and 4 git commands to generate.