| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #define BT_LOG_TAG "COMP" |
| 25 | #include <babeltrace/lib-logging-internal.h> |
| 26 | |
| 27 | #include <babeltrace/assert-internal.h> |
| 28 | #include <babeltrace/assert-pre-internal.h> |
| 29 | #include <babeltrace/graph/self-component.h> |
| 30 | #include <babeltrace/graph/component-const.h> |
| 31 | #include <babeltrace/graph/component-source-const.h> |
| 32 | #include <babeltrace/graph/component-filter-const.h> |
| 33 | #include <babeltrace/graph/component-sink-const.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/connection-internal.h> |
| 40 | #include <babeltrace/graph/graph-internal.h> |
| 41 | #include <babeltrace/graph/message-iterator-internal.h> |
| 42 | #include <babeltrace/graph/port-internal.h> |
| 43 | #include <babeltrace/babeltrace-internal.h> |
| 44 | #include <babeltrace/compiler-internal.h> |
| 45 | #include <babeltrace/types.h> |
| 46 | #include <babeltrace/value.h> |
| 47 | #include <babeltrace/value-internal.h> |
| 48 | #include <stdint.h> |
| 49 | #include <inttypes.h> |
| 50 | |
| 51 | static |
| 52 | struct bt_component * (* const component_create_funcs[])( |
| 53 | const struct bt_component_class *) = { |
| 54 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
| 55 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, |
| 56 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, |
| 57 | }; |
| 58 | |
| 59 | static |
| 60 | void (*component_destroy_funcs[])(struct bt_component *) = { |
| 61 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy, |
| 62 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy, |
| 63 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy, |
| 64 | }; |
| 65 | |
| 66 | static |
| 67 | void finalize_component(struct bt_component *comp) |
| 68 | { |
| 69 | typedef void (*method_t)(void *); |
| 70 | |
| 71 | method_t method = NULL; |
| 72 | |
| 73 | BT_ASSERT(comp); |
| 74 | |
| 75 | switch (comp->class->type) { |
| 76 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 77 | { |
| 78 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 79 | |
| 80 | method = (method_t) src_cc->methods.finalize; |
| 81 | break; |
| 82 | } |
| 83 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 84 | { |
| 85 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 86 | |
| 87 | method = (method_t) flt_cc->methods.finalize; |
| 88 | break; |
| 89 | } |
| 90 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 91 | { |
| 92 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 93 | |
| 94 | method = (method_t) sink_cc->methods.finalize; |
| 95 | break; |
| 96 | } |
| 97 | default: |
| 98 | abort(); |
| 99 | } |
| 100 | |
| 101 | if (method) { |
| 102 | BT_LIB_LOGD("Calling user's finalization method: " |
| 103 | "%![comp-]+c", comp); |
| 104 | method(comp); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | void destroy_component(struct bt_object *obj) |
| 110 | { |
| 111 | struct bt_component *component = NULL; |
| 112 | int i; |
| 113 | |
| 114 | if (!obj) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * The component's reference count is 0 if we're here. Increment |
| 120 | * it to avoid a double-destroy (possibly infinitely recursive). |
| 121 | * This could happen for example if the component's finalization |
| 122 | * function does bt_object_get_ref() (or anything that causes |
| 123 | * bt_object_get_ref() to be called) on itself (ref. count goes |
| 124 | * from 0 to 1), and then bt_object_put_ref(): the reference |
| 125 | * count would go from 1 to 0 again and this function would be |
| 126 | * called again. |
| 127 | */ |
| 128 | obj->ref_count++; |
| 129 | component = container_of(obj, struct bt_component, base); |
| 130 | BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g", |
| 131 | component, bt_component_borrow_graph(component)); |
| 132 | |
| 133 | /* Call destroy listeners in reverse registration order */ |
| 134 | BT_LOGD_STR("Calling destroy listeners."); |
| 135 | |
| 136 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
| 137 | struct bt_component_destroy_listener *listener = |
| 138 | &g_array_index(component->destroy_listeners, |
| 139 | struct bt_component_destroy_listener, i); |
| 140 | |
| 141 | listener->func(component, listener->data); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * User data is destroyed first, followed by the concrete |
| 146 | * component instance. Do not finalize if the component's user |
| 147 | * initialization method failed in the first place. |
| 148 | */ |
| 149 | if (component->initialized) { |
| 150 | finalize_component(component); |
| 151 | } |
| 152 | |
| 153 | if (component->destroy) { |
| 154 | BT_LOGD_STR("Destroying type-specific data."); |
| 155 | component->destroy(component); |
| 156 | } |
| 157 | |
| 158 | if (component->input_ports) { |
| 159 | BT_LOGD_STR("Destroying input ports."); |
| 160 | g_ptr_array_free(component->input_ports, TRUE); |
| 161 | component->input_ports = NULL; |
| 162 | } |
| 163 | |
| 164 | if (component->output_ports) { |
| 165 | BT_LOGD_STR("Destroying output ports."); |
| 166 | g_ptr_array_free(component->output_ports, TRUE); |
| 167 | component->output_ports = NULL; |
| 168 | } |
| 169 | |
| 170 | if (component->destroy_listeners) { |
| 171 | g_array_free(component->destroy_listeners, TRUE); |
| 172 | component->destroy_listeners = NULL; |
| 173 | } |
| 174 | |
| 175 | if (component->name) { |
| 176 | g_string_free(component->name, TRUE); |
| 177 | component->name = NULL; |
| 178 | } |
| 179 | |
| 180 | BT_LOGD_STR("Putting component class."); |
| 181 | BT_OBJECT_PUT_REF_AND_RESET(component->class); |
| 182 | g_free(component); |
| 183 | } |
| 184 | |
| 185 | enum bt_component_class_type bt_component_get_class_type( |
| 186 | const struct bt_component *component) |
| 187 | { |
| 188 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 189 | return component->class->type; |
| 190 | } |
| 191 | |
| 192 | static |
| 193 | struct bt_port *add_port( |
| 194 | struct bt_component *component, GPtrArray *ports, |
| 195 | enum bt_port_type port_type, const char *name, void *user_data) |
| 196 | { |
| 197 | struct bt_port *new_port = NULL; |
| 198 | struct bt_graph *graph = NULL; |
| 199 | |
| 200 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 201 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 202 | BT_ASSERT_PRE(strlen(name) > 0, "Name is empty"); |
| 203 | graph = bt_component_borrow_graph(component); |
| 204 | BT_ASSERT_PRE(graph && !bt_graph_is_canceled(graph), |
| 205 | "Component's graph is canceled: %![comp-]+c, %![graph-]+g", |
| 206 | component, graph); |
| 207 | |
| 208 | // TODO: Validate that the name is not already used. |
| 209 | |
| 210 | BT_LIB_LOGD("Adding port to component: %![comp-]+c, " |
| 211 | "port-type=%s, port-name=\"%s\"", component, |
| 212 | bt_port_type_string(port_type), name); |
| 213 | |
| 214 | new_port = bt_port_create(component, port_type, name, user_data); |
| 215 | if (!new_port) { |
| 216 | BT_LOGE_STR("Cannot create port object."); |
| 217 | goto end; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * No name clash, add the port. |
| 222 | * The component is now the port's parent; it should _not_ |
| 223 | * hold a reference to the port since the port's lifetime |
| 224 | * is now protected by the component's own lifetime. |
| 225 | */ |
| 226 | g_ptr_array_add(ports, new_port); |
| 227 | |
| 228 | /* |
| 229 | * Notify the graph's creator that a new port was added. |
| 230 | */ |
| 231 | bt_object_get_ref(bt_component_borrow_graph(component)); |
| 232 | graph = bt_component_borrow_graph(component); |
| 233 | if (graph) { |
| 234 | bt_graph_notify_port_added(graph, new_port); |
| 235 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
| 236 | } |
| 237 | |
| 238 | BT_LIB_LOGD("Created and added port to component: " |
| 239 | "%![comp-]+c, %![port-]+p", component, new_port); |
| 240 | |
| 241 | end: |
| 242 | return new_port; |
| 243 | } |
| 244 | |
| 245 | BT_HIDDEN |
| 246 | uint64_t bt_component_get_input_port_count(const struct bt_component *comp) |
| 247 | { |
| 248 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 249 | return (uint64_t) comp->input_ports->len; |
| 250 | } |
| 251 | |
| 252 | BT_HIDDEN |
| 253 | uint64_t bt_component_get_output_port_count(const struct bt_component *comp) |
| 254 | { |
| 255 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 256 | return (uint64_t) comp->output_ports->len; |
| 257 | } |
| 258 | |
| 259 | BT_HIDDEN |
| 260 | int bt_component_create(struct bt_component_class *component_class, |
| 261 | const char *name, struct bt_component **user_component) |
| 262 | { |
| 263 | int ret = 0; |
| 264 | struct bt_component *component = NULL; |
| 265 | enum bt_component_class_type type; |
| 266 | |
| 267 | BT_ASSERT(user_component); |
| 268 | BT_ASSERT(component_class); |
| 269 | BT_ASSERT(name); |
| 270 | |
| 271 | type = bt_component_class_get_type(component_class); |
| 272 | BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, " |
| 273 | "comp-name=\"%s\"", component_class, name); |
| 274 | component = component_create_funcs[type](component_class); |
| 275 | if (!component) { |
| 276 | BT_LOGE_STR("Cannot create specific component object."); |
| 277 | ret = -1; |
| 278 | goto end; |
| 279 | } |
| 280 | |
| 281 | bt_object_init_shared_with_parent(&component->base, |
| 282 | destroy_component); |
| 283 | component->class = component_class; |
| 284 | bt_object_get_no_null_check(component->class); |
| 285 | component->destroy = component_destroy_funcs[type]; |
| 286 | component->name = g_string_new(name); |
| 287 | if (!component->name) { |
| 288 | BT_LOGE_STR("Failed to allocate one GString."); |
| 289 | ret = -1; |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | component->input_ports = g_ptr_array_new_with_free_func( |
| 294 | (GDestroyNotify) bt_object_try_spec_release); |
| 295 | if (!component->input_ports) { |
| 296 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 297 | ret = -1; |
| 298 | goto end; |
| 299 | } |
| 300 | |
| 301 | component->output_ports = g_ptr_array_new_with_free_func( |
| 302 | (GDestroyNotify) bt_object_try_spec_release); |
| 303 | if (!component->output_ports) { |
| 304 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 305 | ret = -1; |
| 306 | goto end; |
| 307 | } |
| 308 | |
| 309 | component->destroy_listeners = g_array_new(FALSE, TRUE, |
| 310 | sizeof(struct bt_component_destroy_listener)); |
| 311 | if (!component->destroy_listeners) { |
| 312 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 313 | ret = -1; |
| 314 | goto end; |
| 315 | } |
| 316 | |
| 317 | BT_LIB_LOGD("Created empty component from component class: " |
| 318 | "%![cc-]+C, %![comp-]+c", component_class, component); |
| 319 | BT_OBJECT_MOVE_REF(*user_component, component); |
| 320 | |
| 321 | end: |
| 322 | bt_object_put_ref(component); |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | const char *bt_component_get_name(const struct bt_component *component) |
| 327 | { |
| 328 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 329 | return component->name->str; |
| 330 | } |
| 331 | |
| 332 | struct bt_component_class *bt_component_borrow_class( |
| 333 | const struct bt_component *component) |
| 334 | { |
| 335 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 336 | return component->class; |
| 337 | } |
| 338 | |
| 339 | void *bt_self_component_get_data(const struct bt_self_component *self_comp) |
| 340 | { |
| 341 | struct bt_component *component = (void *) self_comp; |
| 342 | |
| 343 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 344 | return component->user_data; |
| 345 | } |
| 346 | |
| 347 | void bt_self_component_set_data(struct bt_self_component *self_comp, |
| 348 | void *data) |
| 349 | { |
| 350 | struct bt_component *component = (void *) self_comp; |
| 351 | |
| 352 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 353 | component->user_data = data; |
| 354 | BT_LIB_LOGV("Set component's user data: %!+c", component); |
| 355 | } |
| 356 | |
| 357 | BT_HIDDEN |
| 358 | void bt_component_set_graph(struct bt_component *component, |
| 359 | struct bt_graph *graph) |
| 360 | { |
| 361 | bt_object_set_parent(&component->base, |
| 362 | graph ? &graph->base : NULL); |
| 363 | } |
| 364 | |
| 365 | bt_bool bt_component_graph_is_canceled(const struct bt_component *component) |
| 366 | { |
| 367 | return bt_graph_is_canceled( |
| 368 | (void *) bt_object_borrow_parent(&component->base)); |
| 369 | } |
| 370 | |
| 371 | static |
| 372 | struct bt_port *borrow_port_by_name(GPtrArray *ports, |
| 373 | const char *name) |
| 374 | { |
| 375 | uint64_t i; |
| 376 | struct bt_port *ret_port = NULL; |
| 377 | |
| 378 | BT_ASSERT(name); |
| 379 | |
| 380 | for (i = 0; i < ports->len; i++) { |
| 381 | struct bt_port *port = g_ptr_array_index(ports, i); |
| 382 | |
| 383 | if (!strcmp(name, port->name->str)) { |
| 384 | ret_port = port; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return ret_port; |
| 390 | } |
| 391 | |
| 392 | BT_HIDDEN |
| 393 | struct bt_port_input *bt_component_borrow_input_port_by_name( |
| 394 | struct bt_component *comp, const char *name) |
| 395 | { |
| 396 | BT_ASSERT(comp); |
| 397 | return (void *) borrow_port_by_name(comp->input_ports, name); |
| 398 | } |
| 399 | |
| 400 | BT_HIDDEN |
| 401 | struct bt_port_output *bt_component_borrow_output_port_by_name( |
| 402 | struct bt_component *comp, const char *name) |
| 403 | { |
| 404 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 405 | return (void *) |
| 406 | borrow_port_by_name(comp->output_ports, name); |
| 407 | } |
| 408 | |
| 409 | static |
| 410 | struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index) |
| 411 | { |
| 412 | BT_ASSERT(index < ports->len); |
| 413 | return g_ptr_array_index(ports, index); |
| 414 | } |
| 415 | |
| 416 | BT_HIDDEN |
| 417 | struct bt_port_input *bt_component_borrow_input_port_by_index( |
| 418 | struct bt_component *comp, uint64_t index) |
| 419 | { |
| 420 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 421 | BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len); |
| 422 | return (void *) |
| 423 | borrow_port_by_index(comp->input_ports, index); |
| 424 | } |
| 425 | |
| 426 | BT_HIDDEN |
| 427 | struct bt_port_output *bt_component_borrow_output_port_by_index( |
| 428 | struct bt_component *comp, uint64_t index) |
| 429 | { |
| 430 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 431 | BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len); |
| 432 | return (void *) |
| 433 | borrow_port_by_index(comp->output_ports, index); |
| 434 | } |
| 435 | |
| 436 | BT_HIDDEN |
| 437 | struct bt_port_input *bt_component_add_input_port( |
| 438 | struct bt_component *component, const char *name, |
| 439 | void *user_data) |
| 440 | { |
| 441 | /* add_port() logs details */ |
| 442 | return (void *) |
| 443 | add_port(component, component->input_ports, |
| 444 | BT_PORT_TYPE_INPUT, name, user_data); |
| 445 | } |
| 446 | |
| 447 | BT_HIDDEN |
| 448 | struct bt_port_output *bt_component_add_output_port( |
| 449 | struct bt_component *component, const char *name, |
| 450 | void *user_data) |
| 451 | { |
| 452 | /* add_port() logs details */ |
| 453 | return (void *) |
| 454 | add_port(component, component->output_ports, |
| 455 | BT_PORT_TYPE_OUTPUT, name, user_data); |
| 456 | } |
| 457 | |
| 458 | static |
| 459 | void remove_port_by_index(struct bt_component *component, |
| 460 | GPtrArray *ports, uint64_t index) |
| 461 | { |
| 462 | struct bt_port *port; |
| 463 | struct bt_graph *graph; |
| 464 | |
| 465 | BT_ASSERT(ports); |
| 466 | BT_ASSERT(index < ports->len); |
| 467 | port = g_ptr_array_index(ports, index); |
| 468 | BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p", |
| 469 | component, port); |
| 470 | |
| 471 | /* Disconnect both ports of this port's connection, if any */ |
| 472 | if (port->connection) { |
| 473 | bt_connection_end(port->connection, true); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * The port's current reference count can be 0 at this point, |
| 478 | * which means its parent (component) keeps it alive. We are |
| 479 | * about to remove the port from its parent's container (with |
| 480 | * the g_ptr_array_remove_index() call below), which in this |
| 481 | * case would destroy it. This is not good because we still |
| 482 | * need the port for the bt_graph_notify_port_removed() call |
| 483 | * below (in which its component is `NULL` as expected because |
| 484 | * of the bt_object_set_parent() call below). |
| 485 | * |
| 486 | * To avoid a destroyed port during the message callback, |
| 487 | * get a reference now, and put it (destroying the port if its |
| 488 | * reference count is 0 at this point) after notifying the |
| 489 | * graph's user. |
| 490 | */ |
| 491 | bt_object_get_no_null_check(&port->base); |
| 492 | |
| 493 | /* |
| 494 | * Remove from parent's array of ports (weak refs). This never |
| 495 | * destroys the port object because its reference count is at |
| 496 | * least 1 thanks to the bt_object_get_no_null_check() call |
| 497 | * above. |
| 498 | */ |
| 499 | g_ptr_array_remove_index(ports, index); |
| 500 | |
| 501 | /* Detach port from its component parent */ |
| 502 | bt_object_set_parent(&port->base, NULL); |
| 503 | |
| 504 | /* |
| 505 | * Notify the graph's creator that a port is removed. |
| 506 | */ |
| 507 | graph = bt_component_borrow_graph(component); |
| 508 | if (graph) { |
| 509 | bt_graph_notify_port_removed(graph, component, port); |
| 510 | } |
| 511 | |
| 512 | BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p", |
| 513 | component, port); |
| 514 | |
| 515 | /* |
| 516 | * Put the local reference. If this port's reference count was 0 |
| 517 | * when entering this function, it is 1 now, so it is destroyed |
| 518 | * immediately. |
| 519 | */ |
| 520 | bt_object_put_no_null_check(&port->base); |
| 521 | } |
| 522 | |
| 523 | BT_HIDDEN |
| 524 | void bt_component_remove_port(struct bt_component *component, |
| 525 | struct bt_port *port) |
| 526 | { |
| 527 | uint64_t i; |
| 528 | GPtrArray *ports = NULL; |
| 529 | |
| 530 | BT_ASSERT(component); |
| 531 | BT_ASSERT(port); |
| 532 | |
| 533 | switch (port->type) { |
| 534 | case BT_PORT_TYPE_INPUT: |
| 535 | ports = component->input_ports; |
| 536 | break; |
| 537 | case BT_PORT_TYPE_OUTPUT: |
| 538 | ports = component->output_ports; |
| 539 | break; |
| 540 | default: |
| 541 | abort(); |
| 542 | } |
| 543 | |
| 544 | BT_ASSERT(ports); |
| 545 | |
| 546 | for (i = 0; i < ports->len; i++) { |
| 547 | struct bt_port *cur_port = g_ptr_array_index(ports, i); |
| 548 | |
| 549 | if (cur_port == port) { |
| 550 | remove_port_by_index(component, |
| 551 | ports, i); |
| 552 | goto end; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | BT_LIB_LOGW("Port to remove from component was not found: " |
| 557 | "%![comp-]+c, %![port-]+p", component, port); |
| 558 | |
| 559 | end: |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | BT_HIDDEN |
| 564 | enum bt_self_component_status bt_component_accept_port_connection( |
| 565 | struct bt_component *comp, struct bt_port *self_port, |
| 566 | struct bt_port *other_port) |
| 567 | { |
| 568 | typedef enum bt_self_component_status (*method_t)( |
| 569 | void *, void *, const void *); |
| 570 | |
| 571 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 572 | method_t method = NULL; |
| 573 | |
| 574 | BT_ASSERT(comp); |
| 575 | BT_ASSERT(self_port); |
| 576 | BT_ASSERT(other_port); |
| 577 | |
| 578 | switch (comp->class->type) { |
| 579 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 580 | { |
| 581 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 582 | |
| 583 | switch (self_port->type) { |
| 584 | case BT_PORT_TYPE_OUTPUT: |
| 585 | method = (method_t) src_cc->methods.accept_output_port_connection; |
| 586 | break; |
| 587 | default: |
| 588 | abort(); |
| 589 | } |
| 590 | |
| 591 | break; |
| 592 | } |
| 593 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 594 | { |
| 595 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 596 | |
| 597 | switch (self_port->type) { |
| 598 | case BT_PORT_TYPE_INPUT: |
| 599 | method = (method_t) flt_cc->methods.accept_input_port_connection; |
| 600 | break; |
| 601 | case BT_PORT_TYPE_OUTPUT: |
| 602 | method = (method_t) flt_cc->methods.accept_output_port_connection; |
| 603 | break; |
| 604 | default: |
| 605 | abort(); |
| 606 | } |
| 607 | |
| 608 | break; |
| 609 | } |
| 610 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 611 | { |
| 612 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 613 | |
| 614 | switch (self_port->type) { |
| 615 | case BT_PORT_TYPE_INPUT: |
| 616 | method = (method_t) sink_cc->methods.accept_input_port_connection; |
| 617 | break; |
| 618 | default: |
| 619 | abort(); |
| 620 | } |
| 621 | |
| 622 | break; |
| 623 | } |
| 624 | default: |
| 625 | abort(); |
| 626 | } |
| 627 | |
| 628 | if (method) { |
| 629 | BT_LIB_LOGD("Calling user's \"accept port connection\" method: " |
| 630 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", |
| 631 | comp, self_port, other_port); |
| 632 | status = method(comp, self_port, (void *) other_port); |
| 633 | BT_LOGD("User method returned: status=%s", |
| 634 | bt_self_component_status_string(status)); |
| 635 | } |
| 636 | |
| 637 | return status; |
| 638 | } |
| 639 | |
| 640 | BT_HIDDEN |
| 641 | enum bt_self_component_status bt_component_port_connected( |
| 642 | struct bt_component *comp, struct bt_port *self_port, |
| 643 | struct bt_port *other_port) |
| 644 | { |
| 645 | typedef enum bt_self_component_status (*method_t)( |
| 646 | void *, void *, const void *); |
| 647 | |
| 648 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 649 | method_t method = NULL; |
| 650 | |
| 651 | BT_ASSERT(comp); |
| 652 | BT_ASSERT(self_port); |
| 653 | BT_ASSERT(other_port); |
| 654 | |
| 655 | switch (comp->class->type) { |
| 656 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 657 | { |
| 658 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 659 | |
| 660 | switch (self_port->type) { |
| 661 | case BT_PORT_TYPE_OUTPUT: |
| 662 | method = (method_t) src_cc->methods.output_port_connected; |
| 663 | break; |
| 664 | default: |
| 665 | abort(); |
| 666 | } |
| 667 | |
| 668 | break; |
| 669 | } |
| 670 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 671 | { |
| 672 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 673 | |
| 674 | switch (self_port->type) { |
| 675 | case BT_PORT_TYPE_INPUT: |
| 676 | method = (method_t) flt_cc->methods.input_port_connected; |
| 677 | break; |
| 678 | case BT_PORT_TYPE_OUTPUT: |
| 679 | method = (method_t) flt_cc->methods.output_port_connected; |
| 680 | break; |
| 681 | default: |
| 682 | abort(); |
| 683 | } |
| 684 | |
| 685 | break; |
| 686 | } |
| 687 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 688 | { |
| 689 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 690 | |
| 691 | switch (self_port->type) { |
| 692 | case BT_PORT_TYPE_INPUT: |
| 693 | method = (method_t) sink_cc->methods.input_port_connected; |
| 694 | break; |
| 695 | default: |
| 696 | abort(); |
| 697 | } |
| 698 | |
| 699 | break; |
| 700 | } |
| 701 | default: |
| 702 | abort(); |
| 703 | } |
| 704 | |
| 705 | if (method) { |
| 706 | BT_LIB_LOGD("Calling user's \"port connected\" method: " |
| 707 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", |
| 708 | comp, self_port, other_port); |
| 709 | status = method(comp, self_port, (void *) other_port); |
| 710 | BT_LOGD("User method returned: status=%s", |
| 711 | bt_self_component_status_string(status)); |
| 712 | } |
| 713 | |
| 714 | return status; |
| 715 | } |
| 716 | |
| 717 | BT_HIDDEN |
| 718 | void bt_component_port_disconnected(struct bt_component *comp, |
| 719 | struct bt_port *port) |
| 720 | { |
| 721 | typedef void (*method_t)(void *, void *); |
| 722 | |
| 723 | method_t method = NULL; |
| 724 | |
| 725 | BT_ASSERT(comp); |
| 726 | BT_ASSERT(port); |
| 727 | |
| 728 | switch (comp->class->type) { |
| 729 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 730 | { |
| 731 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 732 | |
| 733 | switch (port->type) { |
| 734 | case BT_PORT_TYPE_OUTPUT: |
| 735 | method = (method_t) src_cc->methods.output_port_disconnected; |
| 736 | break; |
| 737 | default: |
| 738 | abort(); |
| 739 | } |
| 740 | |
| 741 | break; |
| 742 | } |
| 743 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 744 | { |
| 745 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 746 | |
| 747 | switch (port->type) { |
| 748 | case BT_PORT_TYPE_INPUT: |
| 749 | method = (method_t) flt_cc->methods.input_port_disconnected; |
| 750 | break; |
| 751 | case BT_PORT_TYPE_OUTPUT: |
| 752 | method = (method_t) flt_cc->methods.output_port_disconnected; |
| 753 | break; |
| 754 | default: |
| 755 | abort(); |
| 756 | } |
| 757 | |
| 758 | break; |
| 759 | } |
| 760 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 761 | { |
| 762 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 763 | |
| 764 | switch (port->type) { |
| 765 | case BT_PORT_TYPE_INPUT: |
| 766 | method = (method_t) sink_cc->methods.input_port_disconnected; |
| 767 | break; |
| 768 | default: |
| 769 | abort(); |
| 770 | } |
| 771 | |
| 772 | break; |
| 773 | } |
| 774 | default: |
| 775 | abort(); |
| 776 | } |
| 777 | |
| 778 | if (method) { |
| 779 | BT_LIB_LOGD("Calling user's \"port disconnected\" method: " |
| 780 | "%![comp-]+c, %![port-]+p", comp, port); |
| 781 | method(comp, port); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | BT_HIDDEN |
| 786 | void bt_component_add_destroy_listener(struct bt_component *component, |
| 787 | bt_component_destroy_listener_func func, void *data) |
| 788 | { |
| 789 | struct bt_component_destroy_listener listener; |
| 790 | |
| 791 | BT_ASSERT(component); |
| 792 | BT_ASSERT(func); |
| 793 | listener.func = func; |
| 794 | listener.data = data; |
| 795 | g_array_append_val(component->destroy_listeners, listener); |
| 796 | BT_LIB_LOGV("Added destroy listener: %![comp-]+c, " |
| 797 | "func-addr=%p, data-addr=%p", |
| 798 | component, func, data); |
| 799 | } |
| 800 | |
| 801 | BT_HIDDEN |
| 802 | void bt_component_remove_destroy_listener(struct bt_component *component, |
| 803 | bt_component_destroy_listener_func func, void *data) |
| 804 | { |
| 805 | uint64_t i; |
| 806 | |
| 807 | BT_ASSERT(component); |
| 808 | BT_ASSERT(func); |
| 809 | |
| 810 | for (i = 0; i < component->destroy_listeners->len; i++) { |
| 811 | struct bt_component_destroy_listener *listener = |
| 812 | &g_array_index(component->destroy_listeners, |
| 813 | struct bt_component_destroy_listener, i); |
| 814 | |
| 815 | if (listener->func == func && listener->data == data) { |
| 816 | g_array_remove_index(component->destroy_listeners, i); |
| 817 | i--; |
| 818 | BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, " |
| 819 | "func-addr=%p, data-addr=%p", |
| 820 | component, func, data); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | void bt_component_get_ref(const struct bt_component *component) |
| 826 | { |
| 827 | bt_object_get_ref(component); |
| 828 | } |
| 829 | |
| 830 | void bt_component_put_ref(const struct bt_component *component) |
| 831 | { |
| 832 | bt_object_put_ref(component); |
| 833 | } |