| 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 "LIB/COMPONENT" |
| 25 | #include "lib/lib-logging.h" |
| 26 | |
| 27 | #include "common/common.h" |
| 28 | #include "common/assert.h" |
| 29 | #include "lib/assert-pre.h" |
| 30 | #include <babeltrace2/graph/self-component.h> |
| 31 | #include <babeltrace2/graph/component-const.h> |
| 32 | #include <babeltrace2/graph/component-source-const.h> |
| 33 | #include <babeltrace2/graph/component-filter-const.h> |
| 34 | #include <babeltrace2/graph/component-sink-const.h> |
| 35 | #include "common/macros.h" |
| 36 | #include "compat/compiler.h" |
| 37 | #include <babeltrace2/types.h> |
| 38 | #include <babeltrace2/value.h> |
| 39 | #include "lib/value.h" |
| 40 | #include <stdint.h> |
| 41 | #include <inttypes.h> |
| 42 | |
| 43 | #include "component.h" |
| 44 | #include "component-class.h" |
| 45 | #include "component-source.h" |
| 46 | #include "component-filter.h" |
| 47 | #include "component-sink.h" |
| 48 | #include "connection.h" |
| 49 | #include "graph.h" |
| 50 | #include "message/iterator.h" |
| 51 | #include "port.h" |
| 52 | |
| 53 | static |
| 54 | struct bt_component * (* const component_create_funcs[])( |
| 55 | const struct bt_component_class *) = { |
| 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 | void finalize_component(struct bt_component *comp) |
| 70 | { |
| 71 | typedef void (*method_t)(void *); |
| 72 | |
| 73 | method_t method = NULL; |
| 74 | |
| 75 | BT_ASSERT(comp); |
| 76 | |
| 77 | switch (comp->class->type) { |
| 78 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 79 | { |
| 80 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 81 | |
| 82 | method = (method_t) src_cc->methods.finalize; |
| 83 | break; |
| 84 | } |
| 85 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 86 | { |
| 87 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 88 | |
| 89 | method = (method_t) flt_cc->methods.finalize; |
| 90 | break; |
| 91 | } |
| 92 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 93 | { |
| 94 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 95 | |
| 96 | method = (method_t) sink_cc->methods.finalize; |
| 97 | break; |
| 98 | } |
| 99 | default: |
| 100 | abort(); |
| 101 | } |
| 102 | |
| 103 | if (method) { |
| 104 | BT_LIB_LOGI("Calling user's component finalization method: " |
| 105 | "%![comp-]+c", comp); |
| 106 | method(comp); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static |
| 111 | void destroy_component(struct bt_object *obj) |
| 112 | { |
| 113 | struct bt_component *component = NULL; |
| 114 | int i; |
| 115 | |
| 116 | if (!obj) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * The component's reference count is 0 if we're here. Increment |
| 122 | * it to avoid a double-destroy (possibly infinitely recursive). |
| 123 | * This could happen for example if the component's finalization |
| 124 | * function does bt_object_get_ref() (or anything that causes |
| 125 | * bt_object_get_ref() to be called) on itself (ref. count goes |
| 126 | * from 0 to 1), and then bt_object_put_ref(): the reference |
| 127 | * count would go from 1 to 0 again and this function would be |
| 128 | * called again. |
| 129 | */ |
| 130 | obj->ref_count++; |
| 131 | component = container_of(obj, struct bt_component, base); |
| 132 | BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g", |
| 133 | component, bt_component_borrow_graph(component)); |
| 134 | |
| 135 | /* Call destroy listeners in reverse registration order */ |
| 136 | BT_LOGD_STR("Calling destroy listeners."); |
| 137 | |
| 138 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
| 139 | struct bt_component_destroy_listener *listener = |
| 140 | &g_array_index(component->destroy_listeners, |
| 141 | struct bt_component_destroy_listener, i); |
| 142 | |
| 143 | listener->func(component, listener->data); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * User data is destroyed first, followed by the concrete |
| 148 | * component instance. Do not finalize if the component's user |
| 149 | * initialization method failed in the first place. |
| 150 | */ |
| 151 | if (component->initialized) { |
| 152 | finalize_component(component); |
| 153 | } |
| 154 | |
| 155 | if (component->destroy) { |
| 156 | BT_LOGD_STR("Destroying type-specific data."); |
| 157 | component->destroy(component); |
| 158 | } |
| 159 | |
| 160 | if (component->input_ports) { |
| 161 | BT_LOGD_STR("Destroying input ports."); |
| 162 | g_ptr_array_free(component->input_ports, TRUE); |
| 163 | component->input_ports = NULL; |
| 164 | } |
| 165 | |
| 166 | if (component->output_ports) { |
| 167 | BT_LOGD_STR("Destroying output ports."); |
| 168 | g_ptr_array_free(component->output_ports, TRUE); |
| 169 | component->output_ports = NULL; |
| 170 | } |
| 171 | |
| 172 | if (component->destroy_listeners) { |
| 173 | g_array_free(component->destroy_listeners, TRUE); |
| 174 | component->destroy_listeners = NULL; |
| 175 | } |
| 176 | |
| 177 | if (component->name) { |
| 178 | g_string_free(component->name, TRUE); |
| 179 | component->name = NULL; |
| 180 | } |
| 181 | |
| 182 | BT_LOGD_STR("Putting component class."); |
| 183 | BT_OBJECT_PUT_REF_AND_RESET(component->class); |
| 184 | g_free(component); |
| 185 | } |
| 186 | |
| 187 | enum bt_component_class_type bt_component_get_class_type( |
| 188 | const struct bt_component *component) |
| 189 | { |
| 190 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 191 | return component->class->type; |
| 192 | } |
| 193 | |
| 194 | static |
| 195 | enum bt_self_component_status add_port( |
| 196 | struct bt_component *component, GPtrArray *ports, |
| 197 | enum bt_port_type port_type, const char *name, void *user_data, |
| 198 | struct bt_port **port) |
| 199 | { |
| 200 | struct bt_port *new_port = NULL; |
| 201 | struct bt_graph *graph = NULL; |
| 202 | enum bt_self_component_status status; |
| 203 | |
| 204 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 205 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
| 206 | BT_ASSERT_PRE(strlen(name) > 0, "Name is empty"); |
| 207 | graph = bt_component_borrow_graph(component); |
| 208 | BT_ASSERT_PRE(graph && !bt_graph_is_canceled(graph), |
| 209 | "Component's graph is canceled: %![comp-]+c, %![graph-]+g", |
| 210 | component, graph); |
| 211 | BT_ASSERT_PRE( |
| 212 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, |
| 213 | "Component's graph is already configured: " |
| 214 | "%![comp-]+c, %![graph-]+g", component, graph); |
| 215 | |
| 216 | // TODO: Validate that the name is not already used. |
| 217 | |
| 218 | BT_LIB_LOGI("Adding port to component: %![comp-]+c, " |
| 219 | "port-type=%s, port-name=\"%s\"", component, |
| 220 | bt_port_type_string(port_type), name); |
| 221 | |
| 222 | new_port = bt_port_create(component, port_type, name, user_data); |
| 223 | if (!new_port) { |
| 224 | BT_LOGE_STR("Cannot create port object."); |
| 225 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * No name clash, add the port. |
| 231 | * The component is now the port's parent; it should _not_ |
| 232 | * hold a reference to the port since the port's lifetime |
| 233 | * is now protected by the component's own lifetime. |
| 234 | */ |
| 235 | g_ptr_array_add(ports, new_port); |
| 236 | |
| 237 | /* |
| 238 | * Notify the graph's creator that a new port was added. |
| 239 | */ |
| 240 | graph = bt_component_borrow_graph(component); |
| 241 | if (graph) { |
| 242 | enum bt_graph_listener_status listener_status; |
| 243 | |
| 244 | listener_status = bt_graph_notify_port_added(graph, new_port); |
| 245 | if (listener_status != BT_GRAPH_LISTENER_STATUS_OK) { |
| 246 | bt_graph_make_faulty(graph); |
| 247 | status = listener_status; |
| 248 | goto error; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | BT_LIB_LOGI("Created and added port to component: " |
| 253 | "%![comp-]+c, %![port-]+p", component, new_port); |
| 254 | |
| 255 | *port = new_port; |
| 256 | status = BT_SELF_COMPONENT_STATUS_OK; |
| 257 | |
| 258 | goto end; |
| 259 | error: |
| 260 | /* |
| 261 | * We need to release the reference that we would otherwise have |
| 262 | * returned to the caller. |
| 263 | */ |
| 264 | BT_PORT_PUT_REF_AND_RESET(new_port); |
| 265 | |
| 266 | end: |
| 267 | return status; |
| 268 | } |
| 269 | |
| 270 | BT_HIDDEN |
| 271 | uint64_t bt_component_get_input_port_count(const struct bt_component *comp) |
| 272 | { |
| 273 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 274 | return (uint64_t) comp->input_ports->len; |
| 275 | } |
| 276 | |
| 277 | BT_HIDDEN |
| 278 | uint64_t bt_component_get_output_port_count(const struct bt_component *comp) |
| 279 | { |
| 280 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 281 | return (uint64_t) comp->output_ports->len; |
| 282 | } |
| 283 | |
| 284 | BT_HIDDEN |
| 285 | int bt_component_create(struct bt_component_class *component_class, |
| 286 | const char *name, bt_logging_level log_level, |
| 287 | struct bt_component **user_component) |
| 288 | { |
| 289 | int ret = 0; |
| 290 | struct bt_component *component = NULL; |
| 291 | enum bt_component_class_type type; |
| 292 | |
| 293 | BT_ASSERT(user_component); |
| 294 | BT_ASSERT(component_class); |
| 295 | BT_ASSERT(name); |
| 296 | type = bt_component_class_get_type(component_class); |
| 297 | BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, " |
| 298 | "comp-name=\"%s\", log-level=%s", component_class, name, |
| 299 | bt_common_logging_level_string(log_level)); |
| 300 | component = component_create_funcs[type](component_class); |
| 301 | if (!component) { |
| 302 | BT_LOGE_STR("Cannot create specific component object."); |
| 303 | ret = -1; |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | bt_object_init_shared_with_parent(&component->base, destroy_component); |
| 308 | component->class = component_class; |
| 309 | bt_object_get_no_null_check(component->class); |
| 310 | component->destroy = component_destroy_funcs[type]; |
| 311 | component->name = g_string_new(name); |
| 312 | if (!component->name) { |
| 313 | BT_LOGE_STR("Failed to allocate one GString."); |
| 314 | ret = -1; |
| 315 | goto end; |
| 316 | } |
| 317 | |
| 318 | component->log_level = log_level; |
| 319 | component->input_ports = g_ptr_array_new_with_free_func( |
| 320 | (GDestroyNotify) bt_object_try_spec_release); |
| 321 | if (!component->input_ports) { |
| 322 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 323 | ret = -1; |
| 324 | goto end; |
| 325 | } |
| 326 | |
| 327 | component->output_ports = g_ptr_array_new_with_free_func( |
| 328 | (GDestroyNotify) bt_object_try_spec_release); |
| 329 | if (!component->output_ports) { |
| 330 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 331 | ret = -1; |
| 332 | goto end; |
| 333 | } |
| 334 | |
| 335 | component->destroy_listeners = g_array_new(FALSE, TRUE, |
| 336 | sizeof(struct bt_component_destroy_listener)); |
| 337 | if (!component->destroy_listeners) { |
| 338 | BT_LOGE_STR("Failed to allocate one GArray."); |
| 339 | ret = -1; |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | BT_LIB_LOGI("Created empty component from component class: " |
| 344 | "%![cc-]+C, %![comp-]+c", component_class, component); |
| 345 | BT_OBJECT_MOVE_REF(*user_component, component); |
| 346 | |
| 347 | end: |
| 348 | bt_object_put_ref(component); |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | const char *bt_component_get_name(const struct bt_component *component) |
| 353 | { |
| 354 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 355 | return component->name->str; |
| 356 | } |
| 357 | |
| 358 | const struct bt_component_class *bt_component_borrow_class_const( |
| 359 | const struct bt_component *component) |
| 360 | { |
| 361 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 362 | return component->class; |
| 363 | } |
| 364 | |
| 365 | void *bt_self_component_get_data(const struct bt_self_component *self_comp) |
| 366 | { |
| 367 | struct bt_component *component = (void *) self_comp; |
| 368 | |
| 369 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 370 | return component->user_data; |
| 371 | } |
| 372 | |
| 373 | void bt_self_component_set_data(struct bt_self_component *self_comp, |
| 374 | void *data) |
| 375 | { |
| 376 | struct bt_component *component = (void *) self_comp; |
| 377 | |
| 378 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 379 | component->user_data = data; |
| 380 | BT_LIB_LOGD("Set component's user data: %!+c", component); |
| 381 | } |
| 382 | |
| 383 | BT_HIDDEN |
| 384 | void bt_component_set_graph(struct bt_component *component, |
| 385 | struct bt_graph *graph) |
| 386 | { |
| 387 | bt_object_set_parent(&component->base, |
| 388 | graph ? &graph->base : NULL); |
| 389 | } |
| 390 | |
| 391 | bt_bool bt_component_graph_is_canceled(const struct bt_component *component) |
| 392 | { |
| 393 | return bt_graph_is_canceled( |
| 394 | (void *) bt_object_borrow_parent(&component->base)); |
| 395 | } |
| 396 | |
| 397 | static |
| 398 | struct bt_port *borrow_port_by_name(GPtrArray *ports, |
| 399 | const char *name) |
| 400 | { |
| 401 | uint64_t i; |
| 402 | struct bt_port *ret_port = NULL; |
| 403 | |
| 404 | BT_ASSERT(name); |
| 405 | |
| 406 | for (i = 0; i < ports->len; i++) { |
| 407 | struct bt_port *port = g_ptr_array_index(ports, i); |
| 408 | |
| 409 | if (!strcmp(name, port->name->str)) { |
| 410 | ret_port = port; |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return ret_port; |
| 416 | } |
| 417 | |
| 418 | BT_HIDDEN |
| 419 | struct bt_port_input *bt_component_borrow_input_port_by_name( |
| 420 | struct bt_component *comp, const char *name) |
| 421 | { |
| 422 | BT_ASSERT(comp); |
| 423 | return (void *) borrow_port_by_name(comp->input_ports, name); |
| 424 | } |
| 425 | |
| 426 | BT_HIDDEN |
| 427 | struct bt_port_output *bt_component_borrow_output_port_by_name( |
| 428 | struct bt_component *comp, const char *name) |
| 429 | { |
| 430 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 431 | return (void *) |
| 432 | borrow_port_by_name(comp->output_ports, name); |
| 433 | } |
| 434 | |
| 435 | static |
| 436 | struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index) |
| 437 | { |
| 438 | BT_ASSERT(index < ports->len); |
| 439 | return g_ptr_array_index(ports, index); |
| 440 | } |
| 441 | |
| 442 | BT_HIDDEN |
| 443 | struct bt_port_input *bt_component_borrow_input_port_by_index( |
| 444 | struct bt_component *comp, uint64_t index) |
| 445 | { |
| 446 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 447 | BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len); |
| 448 | return (void *) |
| 449 | borrow_port_by_index(comp->input_ports, index); |
| 450 | } |
| 451 | |
| 452 | BT_HIDDEN |
| 453 | struct bt_port_output *bt_component_borrow_output_port_by_index( |
| 454 | struct bt_component *comp, uint64_t index) |
| 455 | { |
| 456 | BT_ASSERT_PRE_NON_NULL(comp, "Component"); |
| 457 | BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len); |
| 458 | return (void *) |
| 459 | borrow_port_by_index(comp->output_ports, index); |
| 460 | } |
| 461 | |
| 462 | BT_HIDDEN |
| 463 | enum bt_self_component_status bt_component_add_input_port( |
| 464 | struct bt_component *component, const char *name, |
| 465 | void *user_data, struct bt_port **port) |
| 466 | { |
| 467 | /* add_port() logs details */ |
| 468 | return add_port(component, component->input_ports, |
| 469 | BT_PORT_TYPE_INPUT, name, user_data, port); |
| 470 | } |
| 471 | |
| 472 | BT_HIDDEN |
| 473 | enum bt_self_component_status bt_component_add_output_port( |
| 474 | struct bt_component *component, const char *name, |
| 475 | void *user_data, struct bt_port **port) |
| 476 | { |
| 477 | /* add_port() logs details */ |
| 478 | return add_port(component, component->output_ports, |
| 479 | BT_PORT_TYPE_OUTPUT, name, user_data, port); |
| 480 | } |
| 481 | |
| 482 | BT_HIDDEN |
| 483 | enum bt_self_component_status bt_component_accept_port_connection( |
| 484 | struct bt_component *comp, struct bt_port *self_port, |
| 485 | struct bt_port *other_port) |
| 486 | { |
| 487 | typedef enum bt_self_component_status (*method_t)( |
| 488 | void *, void *, const void *); |
| 489 | |
| 490 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 491 | method_t method = NULL; |
| 492 | |
| 493 | BT_ASSERT(comp); |
| 494 | BT_ASSERT(self_port); |
| 495 | BT_ASSERT(other_port); |
| 496 | |
| 497 | switch (comp->class->type) { |
| 498 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 499 | { |
| 500 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 501 | |
| 502 | switch (self_port->type) { |
| 503 | case BT_PORT_TYPE_OUTPUT: |
| 504 | method = (method_t) src_cc->methods.accept_output_port_connection; |
| 505 | break; |
| 506 | default: |
| 507 | abort(); |
| 508 | } |
| 509 | |
| 510 | break; |
| 511 | } |
| 512 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 513 | { |
| 514 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 515 | |
| 516 | switch (self_port->type) { |
| 517 | case BT_PORT_TYPE_INPUT: |
| 518 | method = (method_t) flt_cc->methods.accept_input_port_connection; |
| 519 | break; |
| 520 | case BT_PORT_TYPE_OUTPUT: |
| 521 | method = (method_t) flt_cc->methods.accept_output_port_connection; |
| 522 | break; |
| 523 | default: |
| 524 | abort(); |
| 525 | } |
| 526 | |
| 527 | break; |
| 528 | } |
| 529 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 530 | { |
| 531 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 532 | |
| 533 | switch (self_port->type) { |
| 534 | case BT_PORT_TYPE_INPUT: |
| 535 | method = (method_t) sink_cc->methods.accept_input_port_connection; |
| 536 | break; |
| 537 | default: |
| 538 | abort(); |
| 539 | } |
| 540 | |
| 541 | break; |
| 542 | } |
| 543 | default: |
| 544 | abort(); |
| 545 | } |
| 546 | |
| 547 | if (method) { |
| 548 | BT_LIB_LOGD("Calling user's \"accept port connection\" method: " |
| 549 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", |
| 550 | comp, self_port, other_port); |
| 551 | status = method(comp, self_port, (void *) other_port); |
| 552 | BT_LOGD("User method returned: status=%s", |
| 553 | bt_self_component_status_string(status)); |
| 554 | } |
| 555 | |
| 556 | return status; |
| 557 | } |
| 558 | |
| 559 | BT_HIDDEN |
| 560 | enum bt_self_component_status bt_component_port_connected( |
| 561 | struct bt_component *comp, struct bt_port *self_port, |
| 562 | struct bt_port *other_port) |
| 563 | { |
| 564 | typedef enum bt_self_component_status (*method_t)( |
| 565 | void *, void *, const void *); |
| 566 | |
| 567 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 568 | method_t method = NULL; |
| 569 | |
| 570 | BT_ASSERT(comp); |
| 571 | BT_ASSERT(self_port); |
| 572 | BT_ASSERT(other_port); |
| 573 | |
| 574 | switch (comp->class->type) { |
| 575 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 576 | { |
| 577 | struct bt_component_class_source *src_cc = (void *) comp->class; |
| 578 | |
| 579 | switch (self_port->type) { |
| 580 | case BT_PORT_TYPE_OUTPUT: |
| 581 | method = (method_t) src_cc->methods.output_port_connected; |
| 582 | break; |
| 583 | default: |
| 584 | abort(); |
| 585 | } |
| 586 | |
| 587 | break; |
| 588 | } |
| 589 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 590 | { |
| 591 | struct bt_component_class_filter *flt_cc = (void *) comp->class; |
| 592 | |
| 593 | switch (self_port->type) { |
| 594 | case BT_PORT_TYPE_INPUT: |
| 595 | method = (method_t) flt_cc->methods.input_port_connected; |
| 596 | break; |
| 597 | case BT_PORT_TYPE_OUTPUT: |
| 598 | method = (method_t) flt_cc->methods.output_port_connected; |
| 599 | break; |
| 600 | default: |
| 601 | abort(); |
| 602 | } |
| 603 | |
| 604 | break; |
| 605 | } |
| 606 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 607 | { |
| 608 | struct bt_component_class_sink *sink_cc = (void *) comp->class; |
| 609 | |
| 610 | switch (self_port->type) { |
| 611 | case BT_PORT_TYPE_INPUT: |
| 612 | method = (method_t) sink_cc->methods.input_port_connected; |
| 613 | break; |
| 614 | default: |
| 615 | abort(); |
| 616 | } |
| 617 | |
| 618 | break; |
| 619 | } |
| 620 | default: |
| 621 | abort(); |
| 622 | } |
| 623 | |
| 624 | if (method) { |
| 625 | BT_LIB_LOGD("Calling user's \"port connected\" method: " |
| 626 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", |
| 627 | comp, self_port, other_port); |
| 628 | status = method(comp, self_port, (void *) other_port); |
| 629 | BT_LOGD("User method returned: status=%s", |
| 630 | bt_self_component_status_string(status)); |
| 631 | BT_ASSERT_PRE(status == BT_SELF_COMPONENT_STATUS_OK || |
| 632 | status == BT_SELF_COMPONENT_STATUS_ERROR || |
| 633 | status == BT_SELF_COMPONENT_STATUS_NOMEM, |
| 634 | "Unexpected returned component status: status=%s", |
| 635 | bt_self_component_status_string(status)); |
| 636 | } |
| 637 | |
| 638 | return status; |
| 639 | } |
| 640 | |
| 641 | BT_HIDDEN |
| 642 | void bt_component_add_destroy_listener(struct bt_component *component, |
| 643 | bt_component_destroy_listener_func func, void *data) |
| 644 | { |
| 645 | struct bt_component_destroy_listener listener; |
| 646 | |
| 647 | BT_ASSERT(component); |
| 648 | BT_ASSERT(func); |
| 649 | listener.func = func; |
| 650 | listener.data = data; |
| 651 | g_array_append_val(component->destroy_listeners, listener); |
| 652 | BT_LIB_LOGD("Added destroy listener: %![comp-]+c, " |
| 653 | "func-addr=%p, data-addr=%p", |
| 654 | component, func, data); |
| 655 | } |
| 656 | |
| 657 | BT_HIDDEN |
| 658 | void bt_component_remove_destroy_listener(struct bt_component *component, |
| 659 | bt_component_destroy_listener_func func, void *data) |
| 660 | { |
| 661 | uint64_t i; |
| 662 | |
| 663 | BT_ASSERT(component); |
| 664 | BT_ASSERT(func); |
| 665 | |
| 666 | for (i = 0; i < component->destroy_listeners->len; i++) { |
| 667 | struct bt_component_destroy_listener *listener = |
| 668 | &g_array_index(component->destroy_listeners, |
| 669 | struct bt_component_destroy_listener, i); |
| 670 | |
| 671 | if (listener->func == func && listener->data == data) { |
| 672 | g_array_remove_index(component->destroy_listeners, i); |
| 673 | i--; |
| 674 | BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, " |
| 675 | "func-addr=%p, data-addr=%p", |
| 676 | component, func, data); |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | bt_logging_level bt_component_get_logging_level( |
| 682 | const struct bt_component *component) |
| 683 | { |
| 684 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
| 685 | return component->log_level; |
| 686 | } |
| 687 | |
| 688 | void bt_component_get_ref(const struct bt_component *component) |
| 689 | { |
| 690 | bt_object_get_ref(component); |
| 691 | } |
| 692 | |
| 693 | void bt_component_put_ref(const struct bt_component *component) |
| 694 | { |
| 695 | bt_object_put_ref(component); |
| 696 | } |