| 1 | /* |
| 2 | * component.c |
| 3 | * |
| 4 | * Babeltrace Plugin 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 | #define BT_LOG_TAG "COMP" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/graph/private-component.h> |
| 33 | #include <babeltrace/graph/component.h> |
| 34 | #include <babeltrace/graph/component-internal.h> |
| 35 | #include <babeltrace/graph/component-class-internal.h> |
| 36 | #include <babeltrace/graph/component-source-internal.h> |
| 37 | #include <babeltrace/graph/component-filter-internal.h> |
| 38 | #include <babeltrace/graph/component-sink-internal.h> |
| 39 | #include <babeltrace/graph/private-connection.h> |
| 40 | #include <babeltrace/graph/connection-internal.h> |
| 41 | #include <babeltrace/graph/graph-internal.h> |
| 42 | #include <babeltrace/graph/notification-iterator-internal.h> |
| 43 | #include <babeltrace/graph/private-notification-iterator.h> |
| 44 | #include <babeltrace/babeltrace-internal.h> |
| 45 | #include <babeltrace/compiler-internal.h> |
| 46 | #include <babeltrace/ref.h> |
| 47 | #include <babeltrace/types.h> |
| 48 | #include <babeltrace/values.h> |
| 49 | #include <babeltrace/values-internal.h> |
| 50 | #include <stdint.h> |
| 51 | #include <inttypes.h> |
| 52 | |
| 53 | static |
| 54 | struct bt_component * (* const component_create_funcs[])( |
| 55 | struct bt_component_class *, struct bt_value *) = { |
| 56 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
| 57 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, |
| 58 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, |
| 59 | }; |
| 60 | |
| 61 | static |
| 62 | void (*component_destroy_funcs[])(struct bt_component *) = { |
| 63 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy, |
| 64 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy, |
| 65 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy, |
| 66 | }; |
| 67 | |
| 68 | static |
| 69 | enum bt_component_status (* const component_validation_funcs[])( |
| 70 | struct bt_component *) = { |
| 71 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_validate, |
| 72 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_validate, |
| 73 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_validate, |
| 74 | }; |
| 75 | |
| 76 | static |
| 77 | void bt_component_destroy(struct bt_object *obj) |
| 78 | { |
| 79 | struct bt_component *component = NULL; |
| 80 | struct bt_component_class *component_class = NULL; |
| 81 | int i; |
| 82 | |
| 83 | if (!obj) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * The component's reference count is 0 if we're here. Increment |
| 89 | * it to avoid a double-destroy (possibly infinitely recursive). |
| 90 | * This could happen for example if the component's finalization |
| 91 | * function does bt_get() (or anything that causes bt_get() to |
| 92 | * be called) on itself (ref. count goes from 0 to 1), and then |
| 93 | * bt_put(): the reference count would go from 1 to 0 again and |
| 94 | * this function would be called again. |
| 95 | */ |
| 96 | obj->ref_count.count++; |
| 97 | component = container_of(obj, struct bt_component, base); |
| 98 | BT_LOGD("Destroying component: addr=%p, name=\"%s\", graph-addr=%p", |
| 99 | component, bt_component_get_name(component), |
| 100 | obj->parent); |
| 101 | |
| 102 | /* Call destroy listeners in reverse registration order */ |
| 103 | BT_LOGD_STR("Calling destroy listeners."); |
| 104 | |
| 105 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
| 106 | struct bt_component_destroy_listener *listener = |
| 107 | &g_array_index(component->destroy_listeners, |
| 108 | struct bt_component_destroy_listener, i); |
| 109 | |
| 110 | listener->func(component, listener->data); |
| 111 | } |
| 112 | |
| 113 | component_class = component->class; |
| 114 | |
| 115 | /* |
| 116 | * User data is destroyed first, followed by the concrete component |
| 117 | * instance. |
| 118 | */ |
| 119 | if (component->class->methods.finalize) { |
| 120 | BT_LOGD_STR("Calling user's finalization method."); |
| 121 | component->class->methods.finalize( |
| 122 | bt_private_component_from_component(component)); |
| 123 | } |
| 124 | |
| 125 | if (component->destroy) { |
| 126 | BT_LOGD_STR("Destroying type-specific data."); |
| 127 | component->destroy(component); |
| 128 | } |
| 129 | |
| 130 | if (component->input_ports) { |
| 131 | BT_LOGD_STR("Destroying input ports."); |
| 132 | g_ptr_array_free(component->input_ports, TRUE); |
| 133 | } |
| 134 | |
| 135 | if (component->output_ports) { |
| 136 | BT_LOGD_STR("Destroying output ports."); |
| 137 | g_ptr_array_free(component->output_ports, TRUE); |
| 138 | } |
| 139 | |
| 140 | if (component->destroy_listeners) { |
| 141 | g_array_free(component->destroy_listeners, TRUE); |
| 142 | } |
| 143 | |
| 144 | if (component->name) { |
| 145 | g_string_free(component->name, TRUE); |
| 146 | } |
| 147 | |
| 148 | BT_LOGD("Putting component class."); |
| 149 | bt_put(component_class); |
| 150 | g_free(component); |
| 151 | } |
| 152 | |
| 153 | struct bt_component *bt_component_from_private_component( |
| 154 | struct bt_private_component *private_component) |
| 155 | { |
| 156 | return bt_get(bt_component_from_private(private_component)); |
| 157 | } |
| 158 | |
| 159 | enum bt_component_class_type bt_component_get_class_type( |
| 160 | struct bt_component *component) |
| 161 | { |
| 162 | return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN; |
| 163 | } |
| 164 | |
| 165 | static |
| 166 | struct bt_port *bt_component_add_port( |
| 167 | struct bt_component *component, GPtrArray *ports, |
| 168 | enum bt_port_type port_type, const char *name, void *user_data) |
| 169 | { |
| 170 | size_t i; |
| 171 | struct bt_port *new_port = NULL; |
| 172 | struct bt_graph *graph = NULL; |
| 173 | |
| 174 | if (!name) { |
| 175 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | if (strlen(name) == 0) { |
| 180 | BT_LOGW_STR("Invalid parameter: name is an empty string."); |
| 181 | goto end; |
| 182 | } |
| 183 | |
| 184 | BT_LOGD("Adding port to component: comp-addr=%p, comp-name=\"%s\", " |
| 185 | "port-type=%s, port-name=\"%s\"", component, |
| 186 | bt_component_get_name(component), |
| 187 | bt_port_type_string(port_type), name); |
| 188 | |
| 189 | /* Look for a port having the same name. */ |
| 190 | for (i = 0; i < ports->len; i++) { |
| 191 | const char *port_name; |
| 192 | struct bt_port *port = g_ptr_array_index(ports, i); |
| 193 | |
| 194 | port_name = bt_port_get_name(port); |
| 195 | assert(port_name); |
| 196 | |
| 197 | if (!strcmp(name, port_name)) { |
| 198 | /* Port name clash, abort. */ |
| 199 | BT_LOGW("Invalid parameter: another port with the same name already exists in the component: " |
| 200 | "other-port-addr=%p", port); |
| 201 | goto end; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | new_port = bt_port_create(component, port_type, name, user_data); |
| 206 | if (!new_port) { |
| 207 | BT_LOGE("Cannot create port object."); |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * No name clash, add the port. |
| 213 | * The component is now the port's parent; it should _not_ |
| 214 | * hold a reference to the port since the port's lifetime |
| 215 | * is now protected by the component's own lifetime. |
| 216 | */ |
| 217 | g_ptr_array_add(ports, new_port); |
| 218 | |
| 219 | /* |
| 220 | * Notify the graph's creator that a new port was added. |
| 221 | */ |
| 222 | graph = bt_component_get_graph(component); |
| 223 | if (graph) { |
| 224 | bt_graph_notify_port_added(graph, new_port); |
| 225 | BT_PUT(graph); |
| 226 | } |
| 227 | |
| 228 | BT_LOGD("Created and added port to component: comp-addr=%p, comp-name=\"%s\", " |
| 229 | "port-type=%s, port-name=\"%s\", port-addr=%p", component, |
| 230 | bt_component_get_name(component), |
| 231 | bt_port_type_string(port_type), name, new_port); |
| 232 | |
| 233 | end: |
| 234 | return new_port; |
| 235 | } |
| 236 | |
| 237 | BT_HIDDEN |
| 238 | int64_t bt_component_get_input_port_count(struct bt_component *comp) |
| 239 | { |
| 240 | assert(comp); |
| 241 | return (int64_t) comp->input_ports->len; |
| 242 | } |
| 243 | |
| 244 | BT_HIDDEN |
| 245 | int64_t bt_component_get_output_port_count(struct bt_component *comp) |
| 246 | { |
| 247 | assert(comp); |
| 248 | return (int64_t) comp->output_ports->len; |
| 249 | } |
| 250 | |
| 251 | struct bt_component *bt_component_create_with_init_method_data( |
| 252 | struct bt_component_class *component_class, const char *name, |
| 253 | struct bt_value *params, void *init_method_data) |
| 254 | { |
| 255 | int ret; |
| 256 | struct bt_component *component = NULL; |
| 257 | enum bt_component_class_type type; |
| 258 | |
| 259 | bt_get(params); |
| 260 | |
| 261 | if (!component_class) { |
| 262 | BT_LOGW_STR("Invalid parameter: component class is NULL."); |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | type = bt_component_class_get_type(component_class); |
| 267 | if (type <= BT_COMPONENT_CLASS_TYPE_UNKNOWN || |
| 268 | type > BT_COMPONENT_CLASS_TYPE_FILTER) { |
| 269 | BT_LOGW("Invalid parameter: unknown component class type: " |
| 270 | "type=%s", bt_component_class_type_string(type)); |
| 271 | goto end; |
| 272 | } |
| 273 | |
| 274 | BT_LOGD("Creating component from component class: " |
| 275 | "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", " |
| 276 | "params-addr=%p, init-method-data-addr=%p", |
| 277 | component_class, bt_component_class_type_string(type), |
| 278 | name, params, init_method_data); |
| 279 | |
| 280 | /* |
| 281 | * Parameters must be a map value, but we create a convenient |
| 282 | * empty one if it's NULL. |
| 283 | */ |
| 284 | if (params) { |
| 285 | if (!bt_value_is_map(params)) { |
| 286 | BT_LOGW("Invalid parameter: initialization parameters must be a map value: " |
| 287 | "type=%s", |
| 288 | bt_value_type_string(bt_value_get_type(params))); |
| 289 | goto end; |
| 290 | } |
| 291 | } else { |
| 292 | params = bt_value_map_create(); |
| 293 | if (!params) { |
| 294 | BT_LOGE_STR("Cannot create map value object."); |
| 295 | goto end; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | component = component_create_funcs[type](component_class, params); |
| 300 | if (!component) { |
| 301 | BT_LOGE_STR("Cannot create specific component object."); |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | bt_object_init(component, bt_component_destroy); |
| 306 | component->class = bt_get(component_class); |
| 307 | component->destroy = component_destroy_funcs[type]; |
| 308 | component->name = g_string_new(name); |
| 309 | if (!component->name) { |
| 310 | BT_LOGE_STR("Failed to allocate one GString."); |
| 311 | BT_PUT(component); |
| 312 | goto end; |
| 313 | } |
| 314 | |
| 315 | component->input_ports = g_ptr_array_new_with_free_func( |
| 316 | bt_object_release); |
| 317 | if (!component->input_ports) { |
| 318 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 319 | BT_PUT(component); |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | component->output_ports = g_ptr_array_new_with_free_func( |
| 324 | bt_object_release); |
| 325 | if (!component->output_ports) { |
| 326 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 327 | BT_PUT(component); |
| 328 | goto end; |
| 329 | } |
| 330 | |
| 331 | component->destroy_listeners = g_array_new(FALSE, TRUE, |
| 332 | sizeof(struct bt_component_destroy_listener)); |
| 333 | if (!component->destroy_listeners) { |
| 334 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 335 | BT_PUT(component); |
| 336 | goto end; |
| 337 | } |
| 338 | |
| 339 | component->initializing = BT_TRUE; |
| 340 | |
| 341 | if (component_class->methods.init) { |
| 342 | BT_LOGD_STR("Calling user's initialization method."); |
| 343 | ret = component_class->methods.init( |
| 344 | bt_private_component_from_component(component), params, |
| 345 | init_method_data); |
| 346 | BT_LOGD("User method returned: status=%s", |
| 347 | bt_component_status_string(ret)); |
| 348 | component->initializing = BT_FALSE; |
| 349 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 350 | BT_LOGW_STR("Initialization method failed."); |
| 351 | BT_PUT(component); |
| 352 | goto end; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | component->initializing = BT_FALSE; |
| 357 | ret = component_validation_funcs[type](component); |
| 358 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 359 | BT_LOGW("Component is invalid: status=%s", |
| 360 | bt_component_status_string(ret)); |
| 361 | BT_PUT(component); |
| 362 | goto end; |
| 363 | } |
| 364 | |
| 365 | BT_LOGD_STR("Freezing component class."); |
| 366 | bt_component_class_freeze(component->class); |
| 367 | BT_LOGD("Created component from component class: " |
| 368 | "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", " |
| 369 | "params-addr=%p, init-method-data-addr=%p, comp-addr=%p", |
| 370 | component_class, bt_component_class_type_string(type), |
| 371 | name, params, init_method_data, component); |
| 372 | |
| 373 | end: |
| 374 | bt_put(params); |
| 375 | return component; |
| 376 | } |
| 377 | |
| 378 | struct bt_component *bt_component_create( |
| 379 | struct bt_component_class *component_class, const char *name, |
| 380 | struct bt_value *params) |
| 381 | { |
| 382 | /* bt_component_create_with_init_method_data() logs details */ |
| 383 | return bt_component_create_with_init_method_data(component_class, name, |
| 384 | params, NULL); |
| 385 | } |
| 386 | |
| 387 | const char *bt_component_get_name(struct bt_component *component) |
| 388 | { |
| 389 | const char *ret = NULL; |
| 390 | |
| 391 | if (!component) { |
| 392 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 393 | goto end; |
| 394 | } |
| 395 | |
| 396 | ret = component->name->len == 0 ? NULL : component->name->str; |
| 397 | |
| 398 | end: |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | struct bt_component_class *bt_component_get_class( |
| 403 | struct bt_component *component) |
| 404 | { |
| 405 | return component ? bt_get(component->class) : NULL; |
| 406 | } |
| 407 | |
| 408 | void *bt_private_component_get_user_data( |
| 409 | struct bt_private_component *private_component) |
| 410 | { |
| 411 | struct bt_component *component = |
| 412 | bt_component_from_private(private_component); |
| 413 | |
| 414 | return component ? component->user_data : NULL; |
| 415 | } |
| 416 | |
| 417 | enum bt_component_status bt_private_component_set_user_data( |
| 418 | struct bt_private_component *private_component, |
| 419 | void *data) |
| 420 | { |
| 421 | struct bt_component *component = |
| 422 | bt_component_from_private(private_component); |
| 423 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 424 | |
| 425 | if (!component) { |
| 426 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 427 | ret = BT_COMPONENT_STATUS_INVALID; |
| 428 | goto end; |
| 429 | } |
| 430 | |
| 431 | if (!component->initializing) { |
| 432 | ret = BT_COMPONENT_STATUS_INVALID; |
| 433 | goto end; |
| 434 | } |
| 435 | |
| 436 | component->user_data = data; |
| 437 | BT_LOGV("Set component's user data: " |
| 438 | "comp-addr=%p, comp-name=\"%s\", user-data-addr=%p", |
| 439 | component, bt_component_get_name(component), data); |
| 440 | |
| 441 | end: |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | BT_HIDDEN |
| 446 | void bt_component_set_graph(struct bt_component *component, |
| 447 | struct bt_graph *graph) |
| 448 | { |
| 449 | struct bt_object *parent = bt_object_get_parent(&component->base); |
| 450 | |
| 451 | assert(!parent || parent == &graph->base); |
| 452 | if (!parent) { |
| 453 | bt_object_set_parent(component, &graph->base); |
| 454 | } |
| 455 | bt_put(parent); |
| 456 | } |
| 457 | |
| 458 | struct bt_graph *bt_component_get_graph( |
| 459 | struct bt_component *component) |
| 460 | { |
| 461 | return (struct bt_graph *) bt_object_get_parent(&component->base); |
| 462 | } |
| 463 | |
| 464 | static |
| 465 | struct bt_port *bt_component_get_port_by_name(GPtrArray *ports, |
| 466 | const char *name) |
| 467 | { |
| 468 | size_t i; |
| 469 | struct bt_port *ret_port = NULL; |
| 470 | |
| 471 | assert(name); |
| 472 | |
| 473 | for (i = 0; i < ports->len; i++) { |
| 474 | struct bt_port *port = g_ptr_array_index(ports, i); |
| 475 | const char *port_name = bt_port_get_name(port); |
| 476 | |
| 477 | if (!port_name) { |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | if (!strcmp(name, port_name)) { |
| 482 | ret_port = bt_get(port); |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return ret_port; |
| 488 | } |
| 489 | |
| 490 | BT_HIDDEN |
| 491 | struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp, |
| 492 | const char *name) |
| 493 | { |
| 494 | assert(comp); |
| 495 | |
| 496 | return bt_component_get_port_by_name(comp->input_ports, name); |
| 497 | } |
| 498 | |
| 499 | BT_HIDDEN |
| 500 | struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp, |
| 501 | const char *name) |
| 502 | { |
| 503 | assert(comp); |
| 504 | |
| 505 | return bt_component_get_port_by_name(comp->output_ports, name); |
| 506 | } |
| 507 | |
| 508 | static |
| 509 | struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index) |
| 510 | { |
| 511 | struct bt_port *port = NULL; |
| 512 | |
| 513 | if (index >= ports->len) { |
| 514 | BT_LOGW("Invalid parameter: index is out of bounds: " |
| 515 | "index=%" PRIu64 ", count=%u", |
| 516 | index, ports->len); |
| 517 | goto end; |
| 518 | } |
| 519 | |
| 520 | port = bt_get(g_ptr_array_index(ports, index)); |
| 521 | end: |
| 522 | return port; |
| 523 | } |
| 524 | |
| 525 | BT_HIDDEN |
| 526 | struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp, |
| 527 | uint64_t index) |
| 528 | { |
| 529 | assert(comp); |
| 530 | |
| 531 | return bt_component_get_port_by_index(comp->input_ports, index); |
| 532 | } |
| 533 | |
| 534 | BT_HIDDEN |
| 535 | struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp, |
| 536 | uint64_t index) |
| 537 | { |
| 538 | assert(comp); |
| 539 | |
| 540 | return bt_component_get_port_by_index(comp->output_ports, index); |
| 541 | } |
| 542 | |
| 543 | BT_HIDDEN |
| 544 | struct bt_port *bt_component_add_input_port( |
| 545 | struct bt_component *component, const char *name, |
| 546 | void *user_data) |
| 547 | { |
| 548 | /* bt_component_add_port() logs details */ |
| 549 | return bt_component_add_port(component, component->input_ports, |
| 550 | BT_PORT_TYPE_INPUT, name, user_data); |
| 551 | } |
| 552 | |
| 553 | BT_HIDDEN |
| 554 | struct bt_port *bt_component_add_output_port( |
| 555 | struct bt_component *component, const char *name, |
| 556 | void *user_data) |
| 557 | { |
| 558 | /* bt_component_add_port() logs details */ |
| 559 | return bt_component_add_port(component, component->output_ports, |
| 560 | BT_PORT_TYPE_OUTPUT, name, user_data); |
| 561 | } |
| 562 | |
| 563 | static |
| 564 | void bt_component_remove_port_by_index(struct bt_component *component, |
| 565 | GPtrArray *ports, size_t index) |
| 566 | { |
| 567 | struct bt_port *port; |
| 568 | struct bt_graph *graph; |
| 569 | |
| 570 | assert(ports); |
| 571 | assert(index < ports->len); |
| 572 | port = g_ptr_array_index(ports, index); |
| 573 | |
| 574 | BT_LOGD("Removing port from component: " |
| 575 | "comp-addr=%p, comp-name=\"%s\", " |
| 576 | "port-addr=%p, port-name=\"%s\"", |
| 577 | component, bt_component_get_name(component), |
| 578 | port, bt_port_get_name(port)); |
| 579 | |
| 580 | /* Disconnect both ports of this port's connection, if any */ |
| 581 | if (port->connection) { |
| 582 | bt_connection_disconnect_ports(port->connection); |
| 583 | } |
| 584 | |
| 585 | /* Remove from parent's array of ports (weak refs) */ |
| 586 | g_ptr_array_remove_index(ports, index); |
| 587 | |
| 588 | /* Detach port from its component parent */ |
| 589 | BT_PUT(port->base.parent); |
| 590 | |
| 591 | /* |
| 592 | * Notify the graph's creator that a port is removed. |
| 593 | */ |
| 594 | graph = bt_component_get_graph(component); |
| 595 | if (graph) { |
| 596 | bt_graph_notify_port_removed(graph, component, port); |
| 597 | BT_PUT(graph); |
| 598 | } |
| 599 | |
| 600 | BT_LOGD("Removed port from component: " |
| 601 | "comp-addr=%p, comp-name=\"%s\", " |
| 602 | "port-addr=%p, port-name=\"%s\"", |
| 603 | component, bt_component_get_name(component), |
| 604 | port, bt_port_get_name(port)); |
| 605 | } |
| 606 | |
| 607 | BT_HIDDEN |
| 608 | enum bt_component_status bt_component_remove_port( |
| 609 | struct bt_component *component, struct bt_port *port) |
| 610 | { |
| 611 | size_t i; |
| 612 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 613 | GPtrArray *ports = NULL; |
| 614 | |
| 615 | if (!component) { |
| 616 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
| 617 | status = BT_COMPONENT_STATUS_INVALID; |
| 618 | goto end; |
| 619 | } |
| 620 | |
| 621 | if (!port) { |
| 622 | BT_LOGW_STR("Invalid parameter: port is NULL."); |
| 623 | status = BT_COMPONENT_STATUS_INVALID; |
| 624 | goto end; |
| 625 | } |
| 626 | |
| 627 | if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) { |
| 628 | ports = component->input_ports; |
| 629 | } else if (bt_port_get_type(port) == BT_PORT_TYPE_OUTPUT) { |
| 630 | ports = component->output_ports; |
| 631 | } |
| 632 | |
| 633 | assert(ports); |
| 634 | |
| 635 | for (i = 0; i < ports->len; i++) { |
| 636 | struct bt_port *cur_port = g_ptr_array_index(ports, i); |
| 637 | |
| 638 | if (cur_port == port) { |
| 639 | bt_component_remove_port_by_index(component, |
| 640 | ports, i); |
| 641 | goto end; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | status = BT_COMPONENT_STATUS_NOT_FOUND; |
| 646 | BT_LOGW("Port to remove from component was not found: " |
| 647 | "comp-addr=%p, comp-name=\"%s\", " |
| 648 | "port-addr=%p, port-name=\"%s\"", |
| 649 | component, bt_component_get_name(component), |
| 650 | port, bt_port_get_name(port)); |
| 651 | |
| 652 | end: |
| 653 | return status; |
| 654 | } |
| 655 | |
| 656 | BT_HIDDEN |
| 657 | enum bt_component_status bt_component_accept_port_connection( |
| 658 | struct bt_component *comp, struct bt_port *self_port, |
| 659 | struct bt_port *other_port) |
| 660 | { |
| 661 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 662 | |
| 663 | assert(comp); |
| 664 | assert(self_port); |
| 665 | assert(other_port); |
| 666 | |
| 667 | if (comp->class->methods.accept_port_connection) { |
| 668 | BT_LOGD("Calling user's \"accept port connection\" method: " |
| 669 | "comp-addr=%p, comp-name=\"%s\", " |
| 670 | "self-port-addr=%p, self-port-name=\"%s\", " |
| 671 | "other-port-addr=%p, other-port-name=\"%s\"", |
| 672 | comp, bt_component_get_name(comp), |
| 673 | self_port, bt_port_get_name(self_port), |
| 674 | other_port, bt_port_get_name(other_port)); |
| 675 | status = comp->class->methods.accept_port_connection( |
| 676 | bt_private_component_from_component(comp), |
| 677 | bt_private_port_from_port(self_port), |
| 678 | other_port); |
| 679 | BT_LOGD("User method returned: status=%s", |
| 680 | bt_component_status_string(status)); |
| 681 | } |
| 682 | |
| 683 | return status; |
| 684 | } |
| 685 | |
| 686 | BT_HIDDEN |
| 687 | void bt_component_port_connected(struct bt_component *comp, |
| 688 | struct bt_port *self_port, struct bt_port *other_port) |
| 689 | { |
| 690 | assert(comp); |
| 691 | assert(self_port); |
| 692 | assert(other_port); |
| 693 | |
| 694 | if (comp->class->methods.port_connected) { |
| 695 | BT_LOGD("Calling user's \"port connected\" method: " |
| 696 | "comp-addr=%p, comp-name=\"%s\", " |
| 697 | "self-port-addr=%p, self-port-name=\"%s\", " |
| 698 | "other-port-addr=%p, other-port-name=\"%s\"", |
| 699 | comp, bt_component_get_name(comp), |
| 700 | self_port, bt_port_get_name(self_port), |
| 701 | other_port, bt_port_get_name(other_port)); |
| 702 | comp->class->methods.port_connected( |
| 703 | bt_private_component_from_component(comp), |
| 704 | bt_private_port_from_port(self_port), other_port); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | BT_HIDDEN |
| 709 | void bt_component_port_disconnected(struct bt_component *comp, |
| 710 | struct bt_port *port) |
| 711 | { |
| 712 | assert(comp); |
| 713 | assert(port); |
| 714 | |
| 715 | if (comp->class->methods.port_disconnected) { |
| 716 | BT_LOGD("Calling user's \"port disconnected\" method: " |
| 717 | "comp-addr=%p, comp-name=\"%s\", " |
| 718 | "port-addr=%p, port-name=\"%s\"", |
| 719 | comp, bt_component_get_name(comp), |
| 720 | port, bt_port_get_name(port)); |
| 721 | comp->class->methods.port_disconnected( |
| 722 | bt_private_component_from_component(comp), |
| 723 | bt_private_port_from_port(port)); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | BT_HIDDEN |
| 728 | void bt_component_add_destroy_listener(struct bt_component *component, |
| 729 | bt_component_destroy_listener_func func, void *data) |
| 730 | { |
| 731 | struct bt_component_destroy_listener listener; |
| 732 | |
| 733 | assert(component); |
| 734 | assert(func); |
| 735 | listener.func = func; |
| 736 | listener.data = data; |
| 737 | g_array_append_val(component->destroy_listeners, listener); |
| 738 | BT_LOGV("Added destroy listener: " |
| 739 | "comp-addr=%p, comp-name=\"%s\", " |
| 740 | "func-addr=%p, data-addr=%p", |
| 741 | component, bt_component_get_name(component), |
| 742 | func, data); |
| 743 | } |
| 744 | |
| 745 | BT_HIDDEN |
| 746 | void bt_component_remove_destroy_listener(struct bt_component *component, |
| 747 | bt_component_destroy_listener_func func, void *data) |
| 748 | { |
| 749 | size_t i; |
| 750 | |
| 751 | assert(component); |
| 752 | assert(func); |
| 753 | |
| 754 | for (i = 0; i < component->destroy_listeners->len; i++) { |
| 755 | struct bt_component_destroy_listener *listener = |
| 756 | &g_array_index(component->destroy_listeners, |
| 757 | struct bt_component_destroy_listener, i); |
| 758 | |
| 759 | if (listener->func == func && listener->data == data) { |
| 760 | g_array_remove_index(component->destroy_listeners, i); |
| 761 | i--; |
| 762 | BT_LOGV("Removed destroy listener: " |
| 763 | "comp-addr=%p, comp-name=\"%s\", " |
| 764 | "func-addr=%p, data-addr=%p", |
| 765 | component, bt_component_get_name(component), |
| 766 | func, data); |
| 767 | } |
| 768 | } |
| 769 | } |