lib: rename `lib-logging.h` to `logging.h`
[babeltrace.git] / src / lib / graph / component.c
CommitLineData
de713ce0 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de713ce0
JG
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
de713ce0
JG
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
350ad6c1 24#define BT_LOG_TAG "LIB/COMPONENT"
c2d9d9cf 25#include "lib/logging.h"
ab0d387b 26
e874da19 27#include "common/common.h"
578e048b
MJ
28#include "common/assert.h"
29#include "lib/assert-pre.h"
3fadfbc0
MJ
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>
91d81473 35#include "common/macros.h"
578e048b 36#include "compat/compiler.h"
3fadfbc0
MJ
37#include <babeltrace2/types.h>
38#include <babeltrace2/value.h>
578e048b 39#include "lib/value.h"
9ac68eb1 40#include <stdint.h>
ab0d387b 41#include <inttypes.h>
de713ce0 42
578e048b
MJ
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
7c7c0433
JG
53static
54struct bt_component * (* const component_create_funcs[])(
0d72b8c3 55 const struct bt_component_class *) = {
d3e4dcd8
PP
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,
7c7c0433
JG
59};
60
72b913fb
PP
61static
62void (*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
b8a06801 68static
d94d92ac
PP
69void 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) {
3f7d4d90 104 BT_LIB_LOGI("Calling user's component finalization method: "
d94d92ac
PP
105 "%![comp-]+c", comp);
106 method(comp);
107 }
108}
109
110static
111void destroy_component(struct bt_object *obj)
b8a06801
JG
112{
113 struct bt_component *component = NULL;
3230ee6b 114 int i;
b8a06801
JG
115
116 if (!obj) {
117 return;
118 }
119
bd14d768
PP
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
d94d92ac
PP
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.
bd14d768 129 */
3fea54f6 130 obj->ref_count++;
b8a06801 131 component = container_of(obj, struct bt_component, base);
3f7d4d90 132 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
d94d92ac 133 component, bt_component_borrow_graph(component));
3230ee6b
PP
134
135 /* Call destroy listeners in reverse registration order */
ab0d387b
PP
136 BT_LOGD_STR("Calling destroy listeners.");
137
3230ee6b
PP
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
7c7c0433 146 /*
36712f1d
PP
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.
b8a06801 150 */
d94d92ac
PP
151 if (component->initialized) {
152 finalize_component(component);
7c7c0433 153 }
b8a06801 154
ab09f844 155 if (component->destroy) {
ab0d387b 156 BT_LOGD_STR("Destroying type-specific data.");
ab09f844
JG
157 component->destroy(component);
158 }
159
72b913fb 160 if (component->input_ports) {
ab0d387b 161 BT_LOGD_STR("Destroying input ports.");
72b913fb 162 g_ptr_array_free(component->input_ports, TRUE);
d94d92ac 163 component->input_ports = NULL;
72b913fb 164 }
b8a06801 165
72b913fb 166 if (component->output_ports) {
ab0d387b 167 BT_LOGD_STR("Destroying output ports.");
72b913fb 168 g_ptr_array_free(component->output_ports, TRUE);
d94d92ac 169 component->output_ports = NULL;
b8a06801
JG
170 }
171
3230ee6b
PP
172 if (component->destroy_listeners) {
173 g_array_free(component->destroy_listeners, TRUE);
d94d92ac 174 component->destroy_listeners = NULL;
3230ee6b
PP
175 }
176
ab0d387b
PP
177 if (component->name) {
178 g_string_free(component->name, TRUE);
d94d92ac 179 component->name = NULL;
ab0d387b
PP
180 }
181
d94d92ac
PP
182 BT_LOGD_STR("Putting component class.");
183 BT_OBJECT_PUT_REF_AND_RESET(component->class);
72b913fb 184 g_free(component);
b8a06801 185}
de713ce0 186
890882ef 187enum bt_component_class_type bt_component_get_class_type(
0d72b8c3 188 const struct bt_component *component)
5645cd95 189{
d94d92ac
PP
190 BT_ASSERT_PRE_NON_NULL(component, "Component");
191 return component->class->type;
5645cd95
JG
192}
193
72b913fb 194static
8cc56726 195enum bt_self_component_status add_port(
72b913fb 196 struct bt_component *component, GPtrArray *ports,
8cc56726
SM
197 enum bt_port_type port_type, const char *name, void *user_data,
198 struct bt_port **port)
72b913fb 199{
72b913fb 200 struct bt_port *new_port = NULL;
1bf957a0 201 struct bt_graph *graph = NULL;
8cc56726 202 enum bt_self_component_status status;
72b913fb 203
d94d92ac
PP
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);
5badd463
PP
211 BT_ASSERT_PRE(
212 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
4725a201
PP
213 "Component's graph is already configured: "
214 "%![comp-]+c, %![graph-]+g", component, graph);
72b913fb 215
d94d92ac 216 // TODO: Validate that the name is not already used.
ab0d387b 217
3f7d4d90 218 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
ab0d387b 219 "port-type=%s, port-name=\"%s\"", component,
ab0d387b
PP
220 bt_port_type_string(port_type), name);
221
3e9b0023 222 new_port = bt_port_create(component, port_type, name, user_data);
72b913fb 223 if (!new_port) {
d94d92ac 224 BT_LOGE_STR("Cannot create port object.");
8cc56726
SM
225 status = BT_SELF_COMPONENT_STATUS_NOMEM;
226 goto error;
72b913fb
PP
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);
1bf957a0
PP
236
237 /*
238 * Notify the graph's creator that a new port was added.
239 */
398454ed 240 graph = bt_component_borrow_graph(component);
1bf957a0 241 if (graph) {
8cc56726
SM
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 }
1bf957a0
PP
250 }
251
3f7d4d90 252 BT_LIB_LOGI("Created and added port to component: "
d94d92ac 253 "%![comp-]+c, %![port-]+p", component, new_port);
ab0d387b 254
8cc56726
SM
255 *port = new_port;
256 status = BT_SELF_COMPONENT_STATUS_OK;
257
258 goto end;
259error:
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
72b913fb 266end:
8cc56726 267 return status;
72b913fb
PP
268}
269
270BT_HIDDEN
0d72b8c3 271uint64_t bt_component_get_input_port_count(const struct bt_component *comp)
72b913fb 272{
d94d92ac
PP
273 BT_ASSERT_PRE_NON_NULL(comp, "Component");
274 return (uint64_t) comp->input_ports->len;
72b913fb
PP
275}
276
277BT_HIDDEN
0d72b8c3 278uint64_t bt_component_get_output_port_count(const struct bt_component *comp)
72b913fb 279{
d94d92ac
PP
280 BT_ASSERT_PRE_NON_NULL(comp, "Component");
281 return (uint64_t) comp->output_ports->len;
72b913fb
PP
282}
283
dd8a4547 284BT_HIDDEN
d94d92ac 285int bt_component_create(struct bt_component_class *component_class,
e874da19
PP
286 const char *name, bt_logging_level log_level,
287 struct bt_component **user_component)
38b48196 288{
d94d92ac 289 int ret = 0;
38b48196 290 struct bt_component *component = NULL;
d3e4dcd8 291 enum bt_component_class_type type;
38b48196 292
f6ccaed9
PP
293 BT_ASSERT(user_component);
294 BT_ASSERT(component_class);
295 BT_ASSERT(name);
7c7c0433 296 type = bt_component_class_get_type(component_class);
3f7d4d90 297 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
e874da19
PP
298 "comp-name=\"%s\", log-level=%s", component_class, name,
299 bt_common_logging_level_string(log_level));
36712f1d 300 component = component_create_funcs[type](component_class);
7c7c0433 301 if (!component) {
ab0d387b 302 BT_LOGE_STR("Cannot create specific component object.");
d94d92ac 303 ret = -1;
7c7c0433
JG
304 goto end;
305 }
306
d0fea130 307 bt_object_init_shared_with_parent(&component->base, destroy_component);
398454ed
PP
308 component->class = component_class;
309 bt_object_get_no_null_check(component->class);
72b913fb 310 component->destroy = component_destroy_funcs[type];
7c7c0433 311 component->name = g_string_new(name);
4b70dd83 312 if (!component->name) {
ab0d387b 313 BT_LOGE_STR("Failed to allocate one GString.");
d94d92ac 314 ret = -1;
7c7c0433
JG
315 goto end;
316 }
317
e874da19 318 component->log_level = log_level;
72b913fb 319 component->input_ports = g_ptr_array_new_with_free_func(
3fea54f6 320 (GDestroyNotify) bt_object_try_spec_release);
72b913fb 321 if (!component->input_ports) {
ab0d387b 322 BT_LOGE_STR("Failed to allocate one GPtrArray.");
d94d92ac 323 ret = -1;
72b913fb
PP
324 goto end;
325 }
326
327 component->output_ports = g_ptr_array_new_with_free_func(
3fea54f6 328 (GDestroyNotify) bt_object_try_spec_release);
72b913fb 329 if (!component->output_ports) {
ab0d387b 330 BT_LOGE_STR("Failed to allocate one GPtrArray.");
d94d92ac 331 ret = -1;
72b913fb
PP
332 goto end;
333 }
334
3230ee6b
PP
335 component->destroy_listeners = g_array_new(FALSE, TRUE,
336 sizeof(struct bt_component_destroy_listener));
337 if (!component->destroy_listeners) {
ab0d387b 338 BT_LOGE_STR("Failed to allocate one GArray.");
d94d92ac 339 ret = -1;
3230ee6b
PP
340 goto end;
341 }
342
3f7d4d90 343 BT_LIB_LOGI("Created empty component from component class: "
d94d92ac 344 "%![cc-]+C, %![comp-]+c", component_class, component);
65300d60 345 BT_OBJECT_MOVE_REF(*user_component, component);
ab0d387b 346
38b48196 347end:
65300d60 348 bt_object_put_ref(component);
d94d92ac 349 return ret;
6358c163
PP
350}
351
0d72b8c3 352const char *bt_component_get_name(const struct bt_component *component)
de713ce0 353{
d94d92ac
PP
354 BT_ASSERT_PRE_NON_NULL(component, "Component");
355 return component->name->str;
de713ce0
JG
356}
357
1de6a2b0 358const struct bt_component_class *bt_component_borrow_class_const(
0d72b8c3 359 const struct bt_component *component)
de713ce0 360{
d94d92ac
PP
361 BT_ASSERT_PRE_NON_NULL(component, "Component");
362 return component->class;
de713ce0
JG
363}
364
0d72b8c3 365void *bt_self_component_get_data(const struct bt_self_component *self_comp)
de713ce0 366{
d94d92ac 367 struct bt_component *component = (void *) self_comp;
890882ef 368
d94d92ac
PP
369 BT_ASSERT_PRE_NON_NULL(component, "Component");
370 return component->user_data;
de713ce0
JG
371}
372
d94d92ac 373void bt_self_component_set_data(struct bt_self_component *self_comp,
de713ce0
JG
374 void *data)
375{
d94d92ac 376 struct bt_component *component = (void *) self_comp;
ab0d387b 377
d94d92ac 378 BT_ASSERT_PRE_NON_NULL(component, "Component");
de713ce0 379 component->user_data = data;
3f7d4d90 380 BT_LIB_LOGD("Set component's user data: %!+c", component);
de713ce0 381}
366e034f
JG
382
383BT_HIDDEN
f60c8b34 384void bt_component_set_graph(struct bt_component *component,
366e034f
JG
385 struct bt_graph *graph)
386{
3fea54f6
PP
387 bt_object_set_parent(&component->base,
388 graph ? &graph->base : NULL);
366e034f
JG
389}
390
0d72b8c3 391bt_bool bt_component_graph_is_canceled(const struct bt_component *component)
366e034f 392{
e5be10ef
PP
393 return bt_graph_is_canceled(
394 (void *) bt_object_borrow_parent(&component->base));
366e034f
JG
395}
396
72b913fb 397static
d94d92ac 398struct bt_port *borrow_port_by_name(GPtrArray *ports,
9ac68eb1 399 const char *name)
366e034f 400{
d94d92ac 401 uint64_t i;
366e034f
JG
402 struct bt_port *ret_port = NULL;
403
f6ccaed9 404 BT_ASSERT(name);
72b913fb 405
366e034f
JG
406 for (i = 0; i < ports->len; i++) {
407 struct bt_port *port = g_ptr_array_index(ports, i);
366e034f 408
d94d92ac
PP
409 if (!strcmp(name, port->name->str)) {
410 ret_port = port;
366e034f
JG
411 break;
412 }
413 }
414
415 return ret_port;
416}
417
418BT_HIDDEN
d94d92ac
PP
419struct bt_port_input *bt_component_borrow_input_port_by_name(
420 struct bt_component *comp, const char *name)
72b913fb 421{
f6ccaed9 422 BT_ASSERT(comp);
d94d92ac 423 return (void *) borrow_port_by_name(comp->input_ports, name);
72b913fb
PP
424}
425
426BT_HIDDEN
d94d92ac
PP
427struct bt_port_output *bt_component_borrow_output_port_by_name(
428 struct bt_component *comp, const char *name)
72b913fb 429{
d94d92ac
PP
430 BT_ASSERT_PRE_NON_NULL(comp, "Component");
431 return (void *)
432 borrow_port_by_name(comp->output_ports, name);
72b913fb
PP
433}
434
435static
d94d92ac 436struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index)
366e034f 437{
d94d92ac
PP
438 BT_ASSERT(index < ports->len);
439 return g_ptr_array_index(ports, index);
366e034f
JG
440}
441
442BT_HIDDEN
d94d92ac
PP
443struct bt_port_input *bt_component_borrow_input_port_by_index(
444 struct bt_component *comp, uint64_t index)
366e034f 445{
d94d92ac
PP
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);
72b913fb 450}
366e034f 451
72b913fb 452BT_HIDDEN
d94d92ac
PP
453struct bt_port_output *bt_component_borrow_output_port_by_index(
454 struct bt_component *comp, uint64_t index)
72b913fb 455{
d94d92ac
PP
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);
72b913fb 460}
366e034f 461
72b913fb 462BT_HIDDEN
8cc56726 463enum bt_self_component_status bt_component_add_input_port(
3e9b0023 464 struct bt_component *component, const char *name,
8cc56726 465 void *user_data, struct bt_port **port)
72b913fb 466{
d94d92ac 467 /* add_port() logs details */
8cc56726
SM
468 return add_port(component, component->input_ports,
469 BT_PORT_TYPE_INPUT, name, user_data, port);
72b913fb 470}
366e034f 471
72b913fb 472BT_HIDDEN
8cc56726 473enum bt_self_component_status bt_component_add_output_port(
3e9b0023 474 struct bt_component *component, const char *name,
8cc56726 475 void *user_data, struct bt_port **port)
72b913fb 476{
d94d92ac 477 /* add_port() logs details */
8cc56726
SM
478 return add_port(component, component->output_ports,
479 BT_PORT_TYPE_OUTPUT, name, user_data, port);
72b913fb
PP
480}
481
f60c8b34 482BT_HIDDEN
d94d92ac 483enum bt_self_component_status bt_component_accept_port_connection(
8f4799f7
PP
484 struct bt_component *comp, struct bt_port *self_port,
485 struct bt_port *other_port)
f60c8b34 486{
d94d92ac 487 typedef enum bt_self_component_status (*method_t)(
0d72b8c3 488 void *, void *, const void *);
d94d92ac
PP
489
490 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
491 method_t method = NULL;
f60c8b34 492
f6ccaed9
PP
493 BT_ASSERT(comp);
494 BT_ASSERT(self_port);
495 BT_ASSERT(other_port);
72b913fb 496
d94d92ac
PP
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);
0d72b8c3 551 status = method(comp, self_port, (void *) other_port);
ab0d387b 552 BT_LOGD("User method returned: status=%s",
d94d92ac 553 bt_self_component_status_string(status));
f60c8b34
JG
554 }
555
556 return status;
557}
72b913fb 558
0d8b4d8e 559BT_HIDDEN
d94d92ac
PP
560enum bt_self_component_status bt_component_port_connected(
561 struct bt_component *comp, struct bt_port *self_port,
562 struct bt_port *other_port)
0d8b4d8e 563{
d94d92ac 564 typedef enum bt_self_component_status (*method_t)(
0d72b8c3 565 void *, void *, const void *);
d94d92ac
PP
566
567 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
568 method_t method = NULL;
bf55043c 569
f6ccaed9
PP
570 BT_ASSERT(comp);
571 BT_ASSERT(self_port);
572 BT_ASSERT(other_port);
0d8b4d8e 573
d94d92ac
PP
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);
0d72b8c3 628 status = method(comp, self_port, (void *) other_port);
d94d92ac
PP
629 BT_LOGD("User method returned: status=%s",
630 bt_self_component_status_string(status));
4725a201
PP
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));
0d8b4d8e 636 }
bf55043c
PP
637
638 return status;
0d8b4d8e
PP
639}
640
3230ee6b
PP
641BT_HIDDEN
642void 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
f6ccaed9
PP
647 BT_ASSERT(component);
648 BT_ASSERT(func);
3230ee6b
PP
649 listener.func = func;
650 listener.data = data;
651 g_array_append_val(component->destroy_listeners, listener);
3f7d4d90 652 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
ab0d387b 653 "func-addr=%p, data-addr=%p",
d94d92ac 654 component, func, data);
3230ee6b
PP
655}
656
657BT_HIDDEN
658void bt_component_remove_destroy_listener(struct bt_component *component,
659 bt_component_destroy_listener_func func, void *data)
660{
d94d92ac 661 uint64_t i;
3230ee6b 662
f6ccaed9
PP
663 BT_ASSERT(component);
664 BT_ASSERT(func);
3230ee6b
PP
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--;
3f7d4d90 674 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
ab0d387b 675 "func-addr=%p, data-addr=%p",
d94d92ac 676 component, func, data);
3230ee6b
PP
677 }
678 }
679}
c5b9b441 680
e874da19
PP
681bt_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
c5b9b441
PP
688void bt_component_get_ref(const struct bt_component *component)
689{
690 bt_object_get_ref(component);
691}
692
693void bt_component_put_ref(const struct bt_component *component)
694{
695 bt_object_put_ref(component);
696}
This page took 0.080377 seconds and 4 git commands to generate.