Commit | Line | Data |
---|---|---|
34ac9eaf JG |
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 | ||
dc217684 PP |
29 | #define BT_LOG_TAG "COMP-FILTER" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
3d9990ac | 32 | #include <babeltrace/compiler-internal.h> |
34ac9eaf | 33 | #include <babeltrace/values.h> |
f7c3ac09 | 34 | #include <babeltrace/graph/private-component.h> |
b2e0c907 PP |
35 | #include <babeltrace/graph/component-filter-internal.h> |
36 | #include <babeltrace/graph/component-internal.h> | |
37 | #include <babeltrace/graph/component-class-internal.h> | |
38 | #include <babeltrace/graph/notification.h> | |
39 | #include <babeltrace/graph/notification-iterator-internal.h> | |
bd7cc15b | 40 | #include <babeltrace/graph/graph.h> |
34ac9eaf | 41 | |
72b913fb | 42 | BT_HIDDEN |
366e034f JG |
43 | void bt_component_filter_destroy(struct bt_component *component) |
44 | { | |
34a9ed19 JG |
45 | } |
46 | ||
366e034f JG |
47 | BT_HIDDEN |
48 | struct bt_component *bt_component_filter_create( | |
36712f1d | 49 | struct bt_component_class *class) |
34a9ed19 | 50 | { |
366e034f | 51 | struct bt_component_filter *filter = NULL; |
34a9ed19 | 52 | |
366e034f JG |
53 | filter = g_new0(struct bt_component_filter, 1); |
54 | if (!filter) { | |
dc217684 | 55 | BT_LOGE_STR("Failed to allocate one filter component."); |
34a9ed19 JG |
56 | goto end; |
57 | } | |
58 | ||
34a9ed19 | 59 | end: |
366e034f | 60 | return filter ? &filter->parent : NULL; |
34a9ed19 JG |
61 | } |
62 | ||
544d0515 | 63 | int64_t bt_component_filter_get_input_port_count( |
9ac68eb1 | 64 | struct bt_component *component) |
34a9ed19 | 65 | { |
544d0515 | 66 | int64_t ret; |
34a9ed19 | 67 | |
dc217684 PP |
68 | if (!component) { |
69 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
70 | ret = (int64_t) -1; | |
71 | goto end; | |
72 | } | |
73 | ||
74 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { | |
75 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
76 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
77 | component, bt_component_get_name(component), | |
78 | bt_component_class_type_string(component->class->type)); | |
79 | ret = (int64_t) -1; | |
34a9ed19 JG |
80 | goto end; |
81 | } | |
82 | ||
9ac68eb1 | 83 | ret = (int64_t) bt_component_get_input_port_count(component); |
dc217684 | 84 | |
366e034f | 85 | end: |
544d0515 | 86 | return ret; |
366e034f JG |
87 | } |
88 | ||
9ac68eb1 | 89 | struct bt_port *bt_component_filter_get_input_port_by_name( |
366e034f JG |
90 | struct bt_component *component, const char *name) |
91 | { | |
72b913fb | 92 | struct bt_port *port = NULL; |
366e034f | 93 | |
dc217684 PP |
94 | if (!component) { |
95 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
96 | goto end; | |
97 | } | |
98 | ||
99 | if (!name) { | |
100 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
101 | goto end; | |
102 | } | |
103 | ||
104 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { | |
105 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
106 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
107 | component, bt_component_get_name(component), | |
108 | bt_component_class_type_string(component->class->type)); | |
34a9ed19 JG |
109 | goto end; |
110 | } | |
111 | ||
dc217684 | 112 | /* bt_component_get_input_port_by_name() logs details/errors */ |
9ac68eb1 | 113 | port = bt_component_get_input_port_by_name(component, name); |
dc217684 | 114 | |
366e034f | 115 | end: |
72b913fb | 116 | return port; |
366e034f | 117 | } |
d3e4dcd8 | 118 | |
9ac68eb1 PP |
119 | struct bt_port *bt_component_filter_get_input_port_by_index( |
120 | struct bt_component *component, uint64_t index) | |
366e034f JG |
121 | { |
122 | struct bt_port *port = NULL; | |
366e034f | 123 | |
dc217684 PP |
124 | if (!component) { |
125 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
126 | goto end; | |
127 | } | |
128 | ||
129 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { | |
130 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
131 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
132 | component, bt_component_get_name(component), | |
133 | bt_component_class_type_string(component->class->type)); | |
366e034f | 134 | goto end; |
34a9ed19 JG |
135 | } |
136 | ||
dc217684 | 137 | /* bt_component_get_input_port_by_index() logs details/errors */ |
9ac68eb1 | 138 | port = bt_component_get_input_port_by_index(component, index); |
dc217684 | 139 | |
34a9ed19 | 140 | end: |
366e034f | 141 | return port; |
34a9ed19 JG |
142 | } |
143 | ||
544d0515 | 144 | int64_t bt_component_filter_get_output_port_count( |
9ac68eb1 | 145 | struct bt_component *component) |
526fc31a | 146 | { |
544d0515 | 147 | int64_t ret; |
526fc31a | 148 | |
dc217684 PP |
149 | if (!component) { |
150 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
151 | ret = (int64_t) -1; | |
526fc31a JG |
152 | goto end; |
153 | } | |
154 | ||
dc217684 PP |
155 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { |
156 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
157 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
158 | component, bt_component_get_name(component), | |
159 | bt_component_class_type_string(component->class->type)); | |
160 | ret = (int64_t) -1; | |
161 | goto end; | |
162 | } | |
163 | ||
164 | /* bt_component_get_output_port_count() logs details/errors */ | |
544d0515 | 165 | ret = bt_component_get_output_port_count(component); |
dc217684 | 166 | |
526fc31a | 167 | end: |
544d0515 | 168 | return ret; |
526fc31a JG |
169 | } |
170 | ||
9ac68eb1 | 171 | struct bt_port *bt_component_filter_get_output_port_by_name( |
366e034f | 172 | struct bt_component *component, const char *name) |
526fc31a | 173 | { |
72b913fb | 174 | struct bt_port *port = NULL; |
526fc31a | 175 | |
dc217684 PP |
176 | if (!component) { |
177 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
526fc31a JG |
178 | goto end; |
179 | } | |
180 | ||
dc217684 PP |
181 | if (!name) { |
182 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
183 | goto end; | |
184 | } | |
185 | ||
186 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { | |
187 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
188 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
189 | component, bt_component_get_name(component), | |
190 | bt_component_class_type_string(component->class->type)); | |
191 | goto end; | |
192 | } | |
193 | ||
194 | /* bt_component_get_output_port_by_name() logs details/errors */ | |
9ac68eb1 | 195 | port = bt_component_get_output_port_by_name(component, name); |
dc217684 | 196 | |
366e034f | 197 | end: |
72b913fb | 198 | return port; |
366e034f JG |
199 | } |
200 | ||
9ac68eb1 PP |
201 | struct bt_port *bt_component_filter_get_output_port_by_index( |
202 | struct bt_component *component, uint64_t index) | |
366e034f JG |
203 | { |
204 | struct bt_port *port = NULL; | |
366e034f | 205 | |
dc217684 PP |
206 | if (!component) { |
207 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
208 | goto end; | |
209 | } | |
210 | ||
211 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { | |
212 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
213 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
214 | component, bt_component_get_name(component), | |
215 | bt_component_class_type_string(component->class->type)); | |
526fc31a JG |
216 | goto end; |
217 | } | |
218 | ||
dc217684 | 219 | /* bt_component_get_output_port_by_index() logs details/errors */ |
9ac68eb1 | 220 | port = bt_component_get_output_port_by_index(component, index); |
dc217684 | 221 | |
366e034f JG |
222 | end: |
223 | return port; | |
224 | } | |
225 | ||
890882ef | 226 | struct bt_private_port * |
9ac68eb1 PP |
227 | bt_private_component_filter_get_input_private_port_by_index( |
228 | struct bt_private_component *private_component, uint64_t index) | |
890882ef | 229 | { |
dc217684 | 230 | /* bt_component_filter_get_input_port_by_index() logs details/errors */ |
890882ef | 231 | return bt_private_port_from_port( |
9ac68eb1 | 232 | bt_component_filter_get_input_port_by_index( |
41765449 | 233 | bt_component_borrow_from_private(private_component), index)); |
890882ef PP |
234 | } |
235 | ||
236 | struct bt_private_port * | |
b9d103be PP |
237 | bt_private_component_filter_get_input_private_port_by_name( |
238 | struct bt_private_component *private_component, | |
239 | const char *name) | |
890882ef | 240 | { |
dc217684 | 241 | /* bt_component_filter_get_input_port_by_name() logs details/errors */ |
890882ef | 242 | return bt_private_port_from_port( |
b9d103be | 243 | bt_component_filter_get_input_port_by_name( |
41765449 | 244 | bt_component_borrow_from_private(private_component), name)); |
890882ef PP |
245 | } |
246 | ||
147337a3 | 247 | enum bt_component_status bt_private_component_filter_add_input_private_port( |
890882ef | 248 | struct bt_private_component *private_component, |
147337a3 PP |
249 | const char *name, void *user_data, |
250 | struct bt_private_port **user_priv_port) | |
890882ef | 251 | { |
147337a3 | 252 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
890882ef PP |
253 | struct bt_port *port = NULL; |
254 | struct bt_component *component = | |
41765449 | 255 | bt_component_borrow_from_private(private_component); |
bd7cc15b | 256 | struct bt_graph *graph; |
890882ef | 257 | |
dc217684 PP |
258 | if (!component) { |
259 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
147337a3 | 260 | status = BT_COMPONENT_STATUS_INVALID; |
890882ef PP |
261 | goto end; |
262 | } | |
263 | ||
dc217684 PP |
264 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { |
265 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
266 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
267 | component, bt_component_get_name(component), | |
268 | bt_component_class_type_string(component->class->type)); | |
147337a3 | 269 | status = BT_COMPONENT_STATUS_INVALID; |
dc217684 PP |
270 | goto end; |
271 | } | |
272 | ||
bd7cc15b PP |
273 | graph = bt_component_borrow_graph(component); |
274 | ||
275 | if (graph && bt_graph_is_canceled(graph)) { | |
43ca7dcc | 276 | BT_LOGW("Cannot add input port to filter component: graph is canceled: " |
bd7cc15b PP |
277 | "comp-addr=%p, comp-name=\"%s\", graph-addr=%p", |
278 | component, bt_component_get_name(component), | |
279 | bt_component_borrow_graph(component)); | |
280 | status = BT_COMPONENT_STATUS_GRAPH_IS_CANCELED; | |
281 | goto end; | |
282 | } | |
283 | ||
dc217684 | 284 | /* bt_component_add_input_port() logs details/errors */ |
3e9b0023 | 285 | port = bt_component_add_input_port(component, name, user_data); |
147337a3 PP |
286 | if (!port) { |
287 | status = BT_COMPONENT_STATUS_NOMEM; | |
288 | goto end; | |
289 | } | |
290 | ||
291 | if (user_priv_port) { | |
292 | /* Move reference to user */ | |
293 | *user_priv_port = bt_private_port_from_port(port); | |
294 | port = NULL; | |
295 | } | |
dc217684 | 296 | |
890882ef | 297 | end: |
147337a3 PP |
298 | bt_put(port); |
299 | return status; | |
890882ef PP |
300 | } |
301 | ||
302 | struct bt_private_port * | |
9ac68eb1 PP |
303 | bt_private_component_filter_get_output_private_port_by_index( |
304 | struct bt_private_component *private_component, uint64_t index) | |
890882ef | 305 | { |
dc217684 | 306 | /* bt_component_filter_get_output_port_by_index() logs details/errors */ |
890882ef | 307 | return bt_private_port_from_port( |
9ac68eb1 | 308 | bt_component_filter_get_output_port_by_index( |
41765449 | 309 | bt_component_borrow_from_private(private_component), index)); |
890882ef PP |
310 | } |
311 | ||
312 | struct bt_private_port * | |
b9d103be PP |
313 | bt_private_component_filter_get_output_private_port_by_name( |
314 | struct bt_private_component *private_component, | |
315 | const char *name) | |
890882ef | 316 | { |
dc217684 | 317 | /* bt_component_filter_get_output_port_by_name() logs details/errors */ |
890882ef | 318 | return bt_private_port_from_port( |
b9d103be | 319 | bt_component_filter_get_output_port_by_name( |
41765449 | 320 | bt_component_borrow_from_private(private_component), name)); |
890882ef PP |
321 | } |
322 | ||
147337a3 | 323 | enum bt_component_status bt_private_component_filter_add_output_private_port( |
890882ef | 324 | struct bt_private_component *private_component, |
147337a3 PP |
325 | const char *name, void *user_data, |
326 | struct bt_private_port **user_priv_port) | |
366e034f | 327 | { |
147337a3 | 328 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
72b913fb | 329 | struct bt_port *port = NULL; |
890882ef | 330 | struct bt_component *component = |
41765449 | 331 | bt_component_borrow_from_private(private_component); |
bd7cc15b | 332 | struct bt_graph *graph; |
366e034f | 333 | |
dc217684 PP |
334 | if (!component) { |
335 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
147337a3 | 336 | status = BT_COMPONENT_STATUS_INVALID; |
526fc31a JG |
337 | goto end; |
338 | } | |
339 | ||
dc217684 PP |
340 | if (component->class->type != BT_COMPONENT_CLASS_TYPE_FILTER) { |
341 | BT_LOGW("Invalid parameter: component's class is not a filter component class: " | |
342 | "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s", | |
343 | component, bt_component_get_name(component), | |
344 | bt_component_class_type_string(component->class->type)); | |
147337a3 | 345 | status = BT_COMPONENT_STATUS_INVALID; |
dc217684 PP |
346 | goto end; |
347 | } | |
348 | ||
bd7cc15b PP |
349 | graph = bt_component_borrow_graph(component); |
350 | ||
351 | if (graph && bt_graph_is_canceled(graph)) { | |
352 | BT_LOGW("Cannot add output port to filter component: graph is canceled: " | |
353 | "comp-addr=%p, comp-name=\"%s\", graph-addr=%p", | |
354 | component, bt_component_get_name(component), | |
355 | bt_component_borrow_graph(component)); | |
356 | status = BT_COMPONENT_STATUS_GRAPH_IS_CANCELED; | |
357 | goto end; | |
358 | } | |
359 | ||
dc217684 | 360 | /* bt_component_add_output_port() logs details/errors */ |
3e9b0023 | 361 | port = bt_component_add_output_port(component, name, user_data); |
147337a3 PP |
362 | if (!port) { |
363 | status = BT_COMPONENT_STATUS_NOMEM; | |
364 | goto end; | |
365 | } | |
366 | ||
367 | if (user_priv_port) { | |
368 | /* Move reference to user */ | |
369 | *user_priv_port = bt_private_port_from_port(port); | |
370 | port = NULL; | |
371 | } | |
dc217684 | 372 | |
366e034f | 373 | end: |
147337a3 PP |
374 | bt_put(port); |
375 | return status; | |
366e034f | 376 | } |