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