| 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 | #define BT_LOG_TAG "COMP-FILTER" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/compiler-internal.h> |
| 33 | #include <babeltrace/values.h> |
| 34 | #include <babeltrace/graph/private-component.h> |
| 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> |
| 40 | #include <babeltrace/graph/graph.h> |
| 41 | |
| 42 | BT_HIDDEN |
| 43 | void bt_component_filter_destroy(struct bt_component *component) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | BT_HIDDEN |
| 48 | struct bt_component *bt_component_filter_create( |
| 49 | struct bt_component_class *class) |
| 50 | { |
| 51 | struct bt_component_filter *filter = NULL; |
| 52 | |
| 53 | filter = g_new0(struct bt_component_filter, 1); |
| 54 | if (!filter) { |
| 55 | BT_LOGE_STR("Failed to allocate one filter component."); |
| 56 | goto end; |
| 57 | } |
| 58 | |
| 59 | end: |
| 60 | return filter ? &filter->parent : NULL; |
| 61 | } |
| 62 | |
| 63 | int64_t bt_component_filter_get_input_port_count( |
| 64 | struct bt_component *component) |
| 65 | { |
| 66 | int64_t ret; |
| 67 | |
| 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; |
| 80 | goto end; |
| 81 | } |
| 82 | |
| 83 | ret = (int64_t) bt_component_get_input_port_count(component); |
| 84 | |
| 85 | end: |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | struct bt_port *bt_component_filter_get_input_port_by_name( |
| 90 | struct bt_component *component, const char *name) |
| 91 | { |
| 92 | struct bt_port *port = NULL; |
| 93 | |
| 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)); |
| 109 | goto end; |
| 110 | } |
| 111 | |
| 112 | /* bt_component_get_input_port_by_name() logs details/errors */ |
| 113 | port = bt_component_get_input_port_by_name(component, name); |
| 114 | |
| 115 | end: |
| 116 | return port; |
| 117 | } |
| 118 | |
| 119 | struct bt_port *bt_component_filter_get_input_port_by_index( |
| 120 | struct bt_component *component, uint64_t index) |
| 121 | { |
| 122 | struct bt_port *port = NULL; |
| 123 | |
| 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)); |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | /* bt_component_get_input_port_by_index() logs details/errors */ |
| 138 | port = bt_component_get_input_port_by_index(component, index); |
| 139 | |
| 140 | end: |
| 141 | return port; |
| 142 | } |
| 143 | |
| 144 | int64_t bt_component_filter_get_output_port_count( |
| 145 | struct bt_component *component) |
| 146 | { |
| 147 | int64_t ret; |
| 148 | |
| 149 | if (!component) { |
| 150 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 151 | ret = (int64_t) -1; |
| 152 | goto end; |
| 153 | } |
| 154 | |
| 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 */ |
| 165 | ret = bt_component_get_output_port_count(component); |
| 166 | |
| 167 | end: |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | struct bt_port *bt_component_filter_get_output_port_by_name( |
| 172 | struct bt_component *component, const char *name) |
| 173 | { |
| 174 | struct bt_port *port = NULL; |
| 175 | |
| 176 | if (!component) { |
| 177 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 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 */ |
| 195 | port = bt_component_get_output_port_by_name(component, name); |
| 196 | |
| 197 | end: |
| 198 | return port; |
| 199 | } |
| 200 | |
| 201 | struct bt_port *bt_component_filter_get_output_port_by_index( |
| 202 | struct bt_component *component, uint64_t index) |
| 203 | { |
| 204 | struct bt_port *port = NULL; |
| 205 | |
| 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)); |
| 216 | goto end; |
| 217 | } |
| 218 | |
| 219 | /* bt_component_get_output_port_by_index() logs details/errors */ |
| 220 | port = bt_component_get_output_port_by_index(component, index); |
| 221 | |
| 222 | end: |
| 223 | return port; |
| 224 | } |
| 225 | |
| 226 | struct bt_private_port * |
| 227 | bt_private_component_filter_get_input_private_port_by_index( |
| 228 | struct bt_private_component *private_component, uint64_t index) |
| 229 | { |
| 230 | /* bt_component_filter_get_input_port_by_index() logs details/errors */ |
| 231 | return bt_private_port_from_port( |
| 232 | bt_component_filter_get_input_port_by_index( |
| 233 | bt_component_borrow_from_private(private_component), index)); |
| 234 | } |
| 235 | |
| 236 | struct bt_private_port * |
| 237 | bt_private_component_filter_get_input_private_port_by_name( |
| 238 | struct bt_private_component *private_component, |
| 239 | const char *name) |
| 240 | { |
| 241 | /* bt_component_filter_get_input_port_by_name() logs details/errors */ |
| 242 | return bt_private_port_from_port( |
| 243 | bt_component_filter_get_input_port_by_name( |
| 244 | bt_component_borrow_from_private(private_component), name)); |
| 245 | } |
| 246 | |
| 247 | enum bt_component_status bt_private_component_filter_add_input_private_port( |
| 248 | struct bt_private_component *private_component, |
| 249 | const char *name, void *user_data, |
| 250 | struct bt_private_port **user_priv_port) |
| 251 | { |
| 252 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 253 | struct bt_port *port = NULL; |
| 254 | struct bt_component *component = |
| 255 | bt_component_borrow_from_private(private_component); |
| 256 | struct bt_graph *graph; |
| 257 | |
| 258 | if (!component) { |
| 259 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 260 | status = BT_COMPONENT_STATUS_INVALID; |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 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)); |
| 269 | status = BT_COMPONENT_STATUS_INVALID; |
| 270 | goto end; |
| 271 | } |
| 272 | |
| 273 | graph = bt_component_borrow_graph(component); |
| 274 | |
| 275 | if (graph && bt_graph_is_canceled(graph)) { |
| 276 | BT_LOGW("Cannot add input port to filter component: graph is canceled: " |
| 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 | |
| 284 | /* bt_component_add_input_port() logs details/errors */ |
| 285 | port = bt_component_add_input_port(component, name, user_data); |
| 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 | } |
| 296 | |
| 297 | end: |
| 298 | bt_put(port); |
| 299 | return status; |
| 300 | } |
| 301 | |
| 302 | struct bt_private_port * |
| 303 | bt_private_component_filter_get_output_private_port_by_index( |
| 304 | struct bt_private_component *private_component, uint64_t index) |
| 305 | { |
| 306 | /* bt_component_filter_get_output_port_by_index() logs details/errors */ |
| 307 | return bt_private_port_from_port( |
| 308 | bt_component_filter_get_output_port_by_index( |
| 309 | bt_component_borrow_from_private(private_component), index)); |
| 310 | } |
| 311 | |
| 312 | struct bt_private_port * |
| 313 | bt_private_component_filter_get_output_private_port_by_name( |
| 314 | struct bt_private_component *private_component, |
| 315 | const char *name) |
| 316 | { |
| 317 | /* bt_component_filter_get_output_port_by_name() logs details/errors */ |
| 318 | return bt_private_port_from_port( |
| 319 | bt_component_filter_get_output_port_by_name( |
| 320 | bt_component_borrow_from_private(private_component), name)); |
| 321 | } |
| 322 | |
| 323 | enum bt_component_status bt_private_component_filter_add_output_private_port( |
| 324 | struct bt_private_component *private_component, |
| 325 | const char *name, void *user_data, |
| 326 | struct bt_private_port **user_priv_port) |
| 327 | { |
| 328 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 329 | struct bt_port *port = NULL; |
| 330 | struct bt_component *component = |
| 331 | bt_component_borrow_from_private(private_component); |
| 332 | struct bt_graph *graph; |
| 333 | |
| 334 | if (!component) { |
| 335 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 336 | status = BT_COMPONENT_STATUS_INVALID; |
| 337 | goto end; |
| 338 | } |
| 339 | |
| 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)); |
| 345 | status = BT_COMPONENT_STATUS_INVALID; |
| 346 | goto end; |
| 347 | } |
| 348 | |
| 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 | |
| 360 | /* bt_component_add_output_port() logs details/errors */ |
| 361 | port = bt_component_add_output_port(component, name, user_data); |
| 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 | } |
| 372 | |
| 373 | end: |
| 374 | bt_put(port); |
| 375 | return status; |
| 376 | } |