Fix: ctf plugin: returning bt_message_iterator_status from src.ctf.fs
[babeltrace.git] / 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
ab0d387b
PP
24#define BT_LOG_TAG "COMP"
25#include <babeltrace/lib-logging-internal.h>
26
c5b9b441
PP
27#include <babeltrace/assert-internal.h>
28#include <babeltrace/assert-pre-internal.h>
d94d92ac 29#include <babeltrace/graph/self-component.h>
0d72b8c3
PP
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>
b2e0c907
PP
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>
b2e0c907
PP
39#include <babeltrace/graph/connection-internal.h>
40#include <babeltrace/graph/graph-internal.h>
d6e69534 41#include <babeltrace/graph/message-iterator-internal.h>
d94d92ac 42#include <babeltrace/graph/port-internal.h>
de713ce0 43#include <babeltrace/babeltrace-internal.h>
3d9990ac 44#include <babeltrace/compiler-internal.h>
c55a9f58 45#include <babeltrace/types.h>
c6bd8523
PP
46#include <babeltrace/value.h>
47#include <babeltrace/value-internal.h>
9ac68eb1 48#include <stdint.h>
ab0d387b 49#include <inttypes.h>
de713ce0 50
7c7c0433
JG
51static
52struct bt_component * (* const component_create_funcs[])(
0d72b8c3 53 const struct bt_component_class *) = {
d3e4dcd8
PP
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,
7c7c0433
JG
57};
58
72b913fb
PP
59static
60void (*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
b8a06801 66static
d94d92ac
PP
67void 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
108static
109void destroy_component(struct bt_object *obj)
b8a06801
JG
110{
111 struct bt_component *component = NULL;
3230ee6b 112 int i;
b8a06801
JG
113
114 if (!obj) {
115 return;
116 }
117
bd14d768
PP
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
d94d92ac
PP
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.
bd14d768 127 */
3fea54f6 128 obj->ref_count++;
b8a06801 129 component = container_of(obj, struct bt_component, base);
d94d92ac
PP
130 BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g",
131 component, bt_component_borrow_graph(component));
3230ee6b
PP
132
133 /* Call destroy listeners in reverse registration order */
ab0d387b
PP
134 BT_LOGD_STR("Calling destroy listeners.");
135
3230ee6b
PP
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
7c7c0433 144 /*
36712f1d
PP
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.
b8a06801 148 */
d94d92ac
PP
149 if (component->initialized) {
150 finalize_component(component);
7c7c0433 151 }
b8a06801 152
ab09f844 153 if (component->destroy) {
ab0d387b 154 BT_LOGD_STR("Destroying type-specific data.");
ab09f844
JG
155 component->destroy(component);
156 }
157
72b913fb 158 if (component->input_ports) {
ab0d387b 159 BT_LOGD_STR("Destroying input ports.");
72b913fb 160 g_ptr_array_free(component->input_ports, TRUE);
d94d92ac 161 component->input_ports = NULL;
72b913fb 162 }
b8a06801 163
72b913fb 164 if (component->output_ports) {
ab0d387b 165 BT_LOGD_STR("Destroying output ports.");
72b913fb 166 g_ptr_array_free(component->output_ports, TRUE);
d94d92ac 167 component->output_ports = NULL;
b8a06801
JG
168 }
169
3230ee6b
PP
170 if (component->destroy_listeners) {
171 g_array_free(component->destroy_listeners, TRUE);
d94d92ac 172 component->destroy_listeners = NULL;
3230ee6b
PP
173 }
174
ab0d387b
PP
175 if (component->name) {
176 g_string_free(component->name, TRUE);
d94d92ac 177 component->name = NULL;
ab0d387b
PP
178 }
179
d94d92ac
PP
180 BT_LOGD_STR("Putting component class.");
181 BT_OBJECT_PUT_REF_AND_RESET(component->class);
72b913fb 182 g_free(component);
b8a06801 183}
de713ce0 184
890882ef 185enum bt_component_class_type bt_component_get_class_type(
0d72b8c3 186 const struct bt_component *component)
5645cd95 187{
d94d92ac
PP
188 BT_ASSERT_PRE_NON_NULL(component, "Component");
189 return component->class->type;
5645cd95
JG
190}
191
72b913fb 192static
d94d92ac 193struct bt_port *add_port(
72b913fb 194 struct bt_component *component, GPtrArray *ports,
3e9b0023 195 enum bt_port_type port_type, const char *name, void *user_data)
72b913fb 196{
72b913fb 197 struct bt_port *new_port = NULL;
1bf957a0 198 struct bt_graph *graph = NULL;
72b913fb 199
d94d92ac
PP
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);
72b913fb 207
d94d92ac 208 // TODO: Validate that the name is not already used.
ab0d387b 209
d94d92ac 210 BT_LIB_LOGD("Adding port to component: %![comp-]+c, "
ab0d387b 211 "port-type=%s, port-name=\"%s\"", component,
ab0d387b
PP
212 bt_port_type_string(port_type), name);
213
3e9b0023 214 new_port = bt_port_create(component, port_type, name, user_data);
72b913fb 215 if (!new_port) {
d94d92ac 216 BT_LOGE_STR("Cannot create port object.");
72b913fb
PP
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);
1bf957a0
PP
227
228 /*
229 * Notify the graph's creator that a new port was added.
230 */
398454ed
PP
231 bt_object_get_ref(bt_component_borrow_graph(component));
232 graph = bt_component_borrow_graph(component);
1bf957a0
PP
233 if (graph) {
234 bt_graph_notify_port_added(graph, new_port);
65300d60 235 BT_OBJECT_PUT_REF_AND_RESET(graph);
1bf957a0
PP
236 }
237
d94d92ac
PP
238 BT_LIB_LOGD("Created and added port to component: "
239 "%![comp-]+c, %![port-]+p", component, new_port);
ab0d387b 240
72b913fb
PP
241end:
242 return new_port;
243}
244
245BT_HIDDEN
0d72b8c3 246uint64_t bt_component_get_input_port_count(const struct bt_component *comp)
72b913fb 247{
d94d92ac
PP
248 BT_ASSERT_PRE_NON_NULL(comp, "Component");
249 return (uint64_t) comp->input_ports->len;
72b913fb
PP
250}
251
252BT_HIDDEN
0d72b8c3 253uint64_t bt_component_get_output_port_count(const struct bt_component *comp)
72b913fb 254{
d94d92ac
PP
255 BT_ASSERT_PRE_NON_NULL(comp, "Component");
256 return (uint64_t) comp->output_ports->len;
72b913fb
PP
257}
258
dd8a4547 259BT_HIDDEN
d94d92ac 260int bt_component_create(struct bt_component_class *component_class,
36712f1d 261 const char *name, struct bt_component **user_component)
38b48196 262{
d94d92ac 263 int ret = 0;
38b48196 264 struct bt_component *component = NULL;
d3e4dcd8 265 enum bt_component_class_type type;
38b48196 266
f6ccaed9
PP
267 BT_ASSERT(user_component);
268 BT_ASSERT(component_class);
269 BT_ASSERT(name);
7c7c0433 270 type = bt_component_class_get_type(component_class);
d94d92ac
PP
271 BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, "
272 "comp-name=\"%s\"", component_class, name);
36712f1d 273 component = component_create_funcs[type](component_class);
7c7c0433 274 if (!component) {
ab0d387b 275 BT_LOGE_STR("Cannot create specific component object.");
d94d92ac 276 ret = -1;
7c7c0433
JG
277 goto end;
278 }
279
d0fea130 280 bt_object_init_shared_with_parent(&component->base, destroy_component);
398454ed
PP
281 component->class = component_class;
282 bt_object_get_no_null_check(component->class);
72b913fb 283 component->destroy = component_destroy_funcs[type];
7c7c0433 284 component->name = g_string_new(name);
4b70dd83 285 if (!component->name) {
ab0d387b 286 BT_LOGE_STR("Failed to allocate one GString.");
d94d92ac 287 ret = -1;
7c7c0433
JG
288 goto end;
289 }
290
72b913fb 291 component->input_ports = g_ptr_array_new_with_free_func(
3fea54f6 292 (GDestroyNotify) bt_object_try_spec_release);
72b913fb 293 if (!component->input_ports) {
ab0d387b 294 BT_LOGE_STR("Failed to allocate one GPtrArray.");
d94d92ac 295 ret = -1;
72b913fb
PP
296 goto end;
297 }
298
299 component->output_ports = g_ptr_array_new_with_free_func(
3fea54f6 300 (GDestroyNotify) bt_object_try_spec_release);
72b913fb 301 if (!component->output_ports) {
ab0d387b 302 BT_LOGE_STR("Failed to allocate one GPtrArray.");
d94d92ac 303 ret = -1;
72b913fb
PP
304 goto end;
305 }
306
3230ee6b
PP
307 component->destroy_listeners = g_array_new(FALSE, TRUE,
308 sizeof(struct bt_component_destroy_listener));
309 if (!component->destroy_listeners) {
ab0d387b 310 BT_LOGE_STR("Failed to allocate one GArray.");
d94d92ac 311 ret = -1;
3230ee6b
PP
312 goto end;
313 }
314
d94d92ac
PP
315 BT_LIB_LOGD("Created empty component from component class: "
316 "%![cc-]+C, %![comp-]+c", component_class, component);
65300d60 317 BT_OBJECT_MOVE_REF(*user_component, component);
ab0d387b 318
38b48196 319end:
65300d60 320 bt_object_put_ref(component);
d94d92ac 321 return ret;
6358c163
PP
322}
323
0d72b8c3 324const char *bt_component_get_name(const struct bt_component *component)
de713ce0 325{
d94d92ac
PP
326 BT_ASSERT_PRE_NON_NULL(component, "Component");
327 return component->name->str;
de713ce0
JG
328}
329
d94d92ac 330struct bt_component_class *bt_component_borrow_class(
0d72b8c3 331 const struct bt_component *component)
de713ce0 332{
d94d92ac
PP
333 BT_ASSERT_PRE_NON_NULL(component, "Component");
334 return component->class;
de713ce0
JG
335}
336
0d72b8c3 337void *bt_self_component_get_data(const struct bt_self_component *self_comp)
de713ce0 338{
d94d92ac 339 struct bt_component *component = (void *) self_comp;
890882ef 340
d94d92ac
PP
341 BT_ASSERT_PRE_NON_NULL(component, "Component");
342 return component->user_data;
de713ce0
JG
343}
344
d94d92ac 345void bt_self_component_set_data(struct bt_self_component *self_comp,
de713ce0
JG
346 void *data)
347{
d94d92ac 348 struct bt_component *component = (void *) self_comp;
ab0d387b 349
d94d92ac 350 BT_ASSERT_PRE_NON_NULL(component, "Component");
de713ce0 351 component->user_data = data;
d94d92ac 352 BT_LIB_LOGV("Set component's user data: %!+c", component);
de713ce0 353}
366e034f
JG
354
355BT_HIDDEN
f60c8b34 356void bt_component_set_graph(struct bt_component *component,
366e034f
JG
357 struct bt_graph *graph)
358{
3fea54f6
PP
359 bt_object_set_parent(&component->base,
360 graph ? &graph->base : NULL);
366e034f
JG
361}
362
0d72b8c3 363bt_bool bt_component_graph_is_canceled(const struct bt_component *component)
366e034f 364{
e5be10ef
PP
365 return bt_graph_is_canceled(
366 (void *) bt_object_borrow_parent(&component->base));
366e034f
JG
367}
368
72b913fb 369static
d94d92ac 370struct bt_port *borrow_port_by_name(GPtrArray *ports,
9ac68eb1 371 const char *name)
366e034f 372{
d94d92ac 373 uint64_t i;
366e034f
JG
374 struct bt_port *ret_port = NULL;
375
f6ccaed9 376 BT_ASSERT(name);
72b913fb 377
366e034f
JG
378 for (i = 0; i < ports->len; i++) {
379 struct bt_port *port = g_ptr_array_index(ports, i);
366e034f 380
d94d92ac
PP
381 if (!strcmp(name, port->name->str)) {
382 ret_port = port;
366e034f
JG
383 break;
384 }
385 }
386
387 return ret_port;
388}
389
390BT_HIDDEN
d94d92ac
PP
391struct bt_port_input *bt_component_borrow_input_port_by_name(
392 struct bt_component *comp, const char *name)
72b913fb 393{
f6ccaed9 394 BT_ASSERT(comp);
d94d92ac 395 return (void *) borrow_port_by_name(comp->input_ports, name);
72b913fb
PP
396}
397
398BT_HIDDEN
d94d92ac
PP
399struct bt_port_output *bt_component_borrow_output_port_by_name(
400 struct bt_component *comp, const char *name)
72b913fb 401{
d94d92ac
PP
402 BT_ASSERT_PRE_NON_NULL(comp, "Component");
403 return (void *)
404 borrow_port_by_name(comp->output_ports, name);
72b913fb
PP
405}
406
407static
d94d92ac 408struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index)
366e034f 409{
d94d92ac
PP
410 BT_ASSERT(index < ports->len);
411 return g_ptr_array_index(ports, index);
366e034f
JG
412}
413
414BT_HIDDEN
d94d92ac
PP
415struct bt_port_input *bt_component_borrow_input_port_by_index(
416 struct bt_component *comp, uint64_t index)
366e034f 417{
d94d92ac
PP
418 BT_ASSERT_PRE_NON_NULL(comp, "Component");
419 BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len);
420 return (void *)
421 borrow_port_by_index(comp->input_ports, index);
72b913fb 422}
366e034f 423
72b913fb 424BT_HIDDEN
d94d92ac
PP
425struct bt_port_output *bt_component_borrow_output_port_by_index(
426 struct bt_component *comp, uint64_t index)
72b913fb 427{
d94d92ac
PP
428 BT_ASSERT_PRE_NON_NULL(comp, "Component");
429 BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len);
430 return (void *)
431 borrow_port_by_index(comp->output_ports, index);
72b913fb 432}
366e034f 433
72b913fb 434BT_HIDDEN
d94d92ac 435struct bt_port_input *bt_component_add_input_port(
3e9b0023
PP
436 struct bt_component *component, const char *name,
437 void *user_data)
72b913fb 438{
d94d92ac
PP
439 /* add_port() logs details */
440 return (void *)
441 add_port(component, component->input_ports,
442 BT_PORT_TYPE_INPUT, name, user_data);
72b913fb 443}
366e034f 444
72b913fb 445BT_HIDDEN
d94d92ac 446struct bt_port_output *bt_component_add_output_port(
3e9b0023
PP
447 struct bt_component *component, const char *name,
448 void *user_data)
72b913fb 449{
d94d92ac
PP
450 /* add_port() logs details */
451 return (void *)
452 add_port(component, component->output_ports,
453 BT_PORT_TYPE_OUTPUT, name, user_data);
72b913fb
PP
454}
455
456static
d94d92ac
PP
457void remove_port_by_index(struct bt_component *component,
458 GPtrArray *ports, uint64_t index)
72b913fb
PP
459{
460 struct bt_port *port;
1bf957a0 461 struct bt_graph *graph;
72b913fb 462
f6ccaed9
PP
463 BT_ASSERT(ports);
464 BT_ASSERT(index < ports->len);
72b913fb 465 port = g_ptr_array_index(ports, index);
d94d92ac
PP
466 BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p",
467 component, port);
ab0d387b 468
72b913fb
PP
469 /* Disconnect both ports of this port's connection, if any */
470 if (port->connection) {
c42ea0af 471 bt_connection_end(port->connection, true);
f60c8b34
JG
472 }
473
d94d92ac
PP
474 /*
475 * The port's current reference count can be 0 at this point,
476 * which means its parent (component) keeps it alive. We are
477 * about to remove the port from its parent's container (with
478 * the g_ptr_array_remove_index() call below), which in this
479 * case would destroy it. This is not good because we still
480 * need the port for the bt_graph_notify_port_removed() call
481 * below (in which its component is `NULL` as expected because
482 * of the bt_object_set_parent() call below).
483 *
d6e69534 484 * To avoid a destroyed port during the message callback,
d94d92ac
PP
485 * get a reference now, and put it (destroying the port if its
486 * reference count is 0 at this point) after notifying the
0d72b8c3 487 * graph's user.
d94d92ac
PP
488 */
489 bt_object_get_no_null_check(&port->base);
490
491 /*
492 * Remove from parent's array of ports (weak refs). This never
493 * destroys the port object because its reference count is at
494 * least 1 thanks to the bt_object_get_no_null_check() call
495 * above.
496 */
72b913fb
PP
497 g_ptr_array_remove_index(ports, index);
498
499 /* Detach port from its component parent */
d94d92ac 500 bt_object_set_parent(&port->base, NULL);
72b913fb 501
1bf957a0
PP
502 /*
503 * Notify the graph's creator that a port is removed.
504 */
d94d92ac 505 graph = bt_component_borrow_graph(component);
1bf957a0
PP
506 if (graph) {
507 bt_graph_notify_port_removed(graph, component, port);
1bf957a0 508 }
ab0d387b 509
d94d92ac
PP
510 BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p",
511 component, port);
512
513 /*
514 * Put the local reference. If this port's reference count was 0
515 * when entering this function, it is 1 now, so it is destroyed
516 * immediately.
517 */
518 bt_object_put_no_null_check(&port->base);
366e034f
JG
519}
520
521BT_HIDDEN
d94d92ac
PP
522void bt_component_remove_port(struct bt_component *component,
523 struct bt_port *port)
366e034f 524{
d94d92ac 525 uint64_t i;
72b913fb 526 GPtrArray *ports = NULL;
366e034f 527
d94d92ac
PP
528 BT_ASSERT(component);
529 BT_ASSERT(port);
366e034f 530
d94d92ac
PP
531 switch (port->type) {
532 case BT_PORT_TYPE_INPUT:
72b913fb 533 ports = component->input_ports;
d94d92ac
PP
534 break;
535 case BT_PORT_TYPE_OUTPUT:
72b913fb 536 ports = component->output_ports;
d94d92ac
PP
537 break;
538 default:
539 abort();
72b913fb 540 }
366e034f 541
f6ccaed9 542 BT_ASSERT(ports);
366e034f 543
72b913fb
PP
544 for (i = 0; i < ports->len; i++) {
545 struct bt_port *cur_port = g_ptr_array_index(ports, i);
546
547 if (cur_port == port) {
d0fea130 548 remove_port_by_index(component, ports, i);
366e034f
JG
549 goto end;
550 }
551 }
72b913fb 552
d94d92ac
PP
553 BT_LIB_LOGW("Port to remove from component was not found: "
554 "%![comp-]+c, %![port-]+p", component, port);
ab0d387b 555
366e034f 556end:
d94d92ac 557 return;
366e034f 558}
f60c8b34
JG
559
560BT_HIDDEN
d94d92ac 561enum bt_self_component_status bt_component_accept_port_connection(
8f4799f7
PP
562 struct bt_component *comp, struct bt_port *self_port,
563 struct bt_port *other_port)
f60c8b34 564{
d94d92ac 565 typedef enum bt_self_component_status (*method_t)(
0d72b8c3 566 void *, void *, const void *);
d94d92ac
PP
567
568 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
569 method_t method = NULL;
f60c8b34 570
f6ccaed9
PP
571 BT_ASSERT(comp);
572 BT_ASSERT(self_port);
573 BT_ASSERT(other_port);
72b913fb 574
d94d92ac
PP
575 switch (comp->class->type) {
576 case BT_COMPONENT_CLASS_TYPE_SOURCE:
577 {
578 struct bt_component_class_source *src_cc = (void *) comp->class;
579
580 switch (self_port->type) {
581 case BT_PORT_TYPE_OUTPUT:
582 method = (method_t) src_cc->methods.accept_output_port_connection;
583 break;
584 default:
585 abort();
586 }
587
588 break;
589 }
590 case BT_COMPONENT_CLASS_TYPE_FILTER:
591 {
592 struct bt_component_class_filter *flt_cc = (void *) comp->class;
593
594 switch (self_port->type) {
595 case BT_PORT_TYPE_INPUT:
596 method = (method_t) flt_cc->methods.accept_input_port_connection;
597 break;
598 case BT_PORT_TYPE_OUTPUT:
599 method = (method_t) flt_cc->methods.accept_output_port_connection;
600 break;
601 default:
602 abort();
603 }
604
605 break;
606 }
607 case BT_COMPONENT_CLASS_TYPE_SINK:
608 {
609 struct bt_component_class_sink *sink_cc = (void *) comp->class;
610
611 switch (self_port->type) {
612 case BT_PORT_TYPE_INPUT:
613 method = (method_t) sink_cc->methods.accept_input_port_connection;
614 break;
615 default:
616 abort();
617 }
618
619 break;
620 }
621 default:
622 abort();
623 }
624
625 if (method) {
626 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
627 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
628 comp, self_port, other_port);
0d72b8c3 629 status = method(comp, self_port, (void *) other_port);
ab0d387b 630 BT_LOGD("User method returned: status=%s",
d94d92ac 631 bt_self_component_status_string(status));
f60c8b34
JG
632 }
633
634 return status;
635}
72b913fb 636
0d8b4d8e 637BT_HIDDEN
d94d92ac
PP
638enum bt_self_component_status bt_component_port_connected(
639 struct bt_component *comp, struct bt_port *self_port,
640 struct bt_port *other_port)
0d8b4d8e 641{
d94d92ac 642 typedef enum bt_self_component_status (*method_t)(
0d72b8c3 643 void *, void *, const void *);
d94d92ac
PP
644
645 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
646 method_t method = NULL;
bf55043c 647
f6ccaed9
PP
648 BT_ASSERT(comp);
649 BT_ASSERT(self_port);
650 BT_ASSERT(other_port);
0d8b4d8e 651
d94d92ac
PP
652 switch (comp->class->type) {
653 case BT_COMPONENT_CLASS_TYPE_SOURCE:
654 {
655 struct bt_component_class_source *src_cc = (void *) comp->class;
656
657 switch (self_port->type) {
658 case BT_PORT_TYPE_OUTPUT:
659 method = (method_t) src_cc->methods.output_port_connected;
660 break;
661 default:
662 abort();
663 }
664
665 break;
666 }
667 case BT_COMPONENT_CLASS_TYPE_FILTER:
668 {
669 struct bt_component_class_filter *flt_cc = (void *) comp->class;
670
671 switch (self_port->type) {
672 case BT_PORT_TYPE_INPUT:
673 method = (method_t) flt_cc->methods.input_port_connected;
674 break;
675 case BT_PORT_TYPE_OUTPUT:
676 method = (method_t) flt_cc->methods.output_port_connected;
677 break;
678 default:
679 abort();
680 }
681
682 break;
683 }
684 case BT_COMPONENT_CLASS_TYPE_SINK:
685 {
686 struct bt_component_class_sink *sink_cc = (void *) comp->class;
687
688 switch (self_port->type) {
689 case BT_PORT_TYPE_INPUT:
690 method = (method_t) sink_cc->methods.input_port_connected;
691 break;
692 default:
693 abort();
694 }
695
696 break;
697 }
698 default:
699 abort();
700 }
701
702 if (method) {
703 BT_LIB_LOGD("Calling user's \"port connected\" method: "
704 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
705 comp, self_port, other_port);
0d72b8c3 706 status = method(comp, self_port, (void *) other_port);
d94d92ac
PP
707 BT_LOGD("User method returned: status=%s",
708 bt_self_component_status_string(status));
0d8b4d8e 709 }
bf55043c
PP
710
711 return status;
0d8b4d8e
PP
712}
713
72b913fb
PP
714BT_HIDDEN
715void bt_component_port_disconnected(struct bt_component *comp,
716 struct bt_port *port)
717{
d94d92ac
PP
718 typedef void (*method_t)(void *, void *);
719
720 method_t method = NULL;
721
f6ccaed9
PP
722 BT_ASSERT(comp);
723 BT_ASSERT(port);
72b913fb 724
d94d92ac
PP
725 switch (comp->class->type) {
726 case BT_COMPONENT_CLASS_TYPE_SOURCE:
727 {
728 struct bt_component_class_source *src_cc = (void *) comp->class;
729
730 switch (port->type) {
731 case BT_PORT_TYPE_OUTPUT:
732 method = (method_t) src_cc->methods.output_port_disconnected;
733 break;
734 default:
735 abort();
736 }
737
738 break;
739 }
740 case BT_COMPONENT_CLASS_TYPE_FILTER:
741 {
742 struct bt_component_class_filter *flt_cc = (void *) comp->class;
743
744 switch (port->type) {
745 case BT_PORT_TYPE_INPUT:
746 method = (method_t) flt_cc->methods.input_port_disconnected;
747 break;
748 case BT_PORT_TYPE_OUTPUT:
749 method = (method_t) flt_cc->methods.output_port_disconnected;
750 break;
751 default:
752 abort();
753 }
754
755 break;
756 }
757 case BT_COMPONENT_CLASS_TYPE_SINK:
758 {
759 struct bt_component_class_sink *sink_cc = (void *) comp->class;
760
761 switch (port->type) {
762 case BT_PORT_TYPE_INPUT:
763 method = (method_t) sink_cc->methods.input_port_disconnected;
764 break;
765 default:
766 abort();
767 }
768
769 break;
770 }
771 default:
772 abort();
773 }
774
775 if (method) {
776 BT_LIB_LOGD("Calling user's \"port disconnected\" method: "
777 "%![comp-]+c, %![port-]+p", comp, port);
778 method(comp, port);
72b913fb
PP
779 }
780}
3230ee6b
PP
781
782BT_HIDDEN
783void bt_component_add_destroy_listener(struct bt_component *component,
784 bt_component_destroy_listener_func func, void *data)
785{
786 struct bt_component_destroy_listener listener;
787
f6ccaed9
PP
788 BT_ASSERT(component);
789 BT_ASSERT(func);
3230ee6b
PP
790 listener.func = func;
791 listener.data = data;
792 g_array_append_val(component->destroy_listeners, listener);
d94d92ac 793 BT_LIB_LOGV("Added destroy listener: %![comp-]+c, "
ab0d387b 794 "func-addr=%p, data-addr=%p",
d94d92ac 795 component, func, data);
3230ee6b
PP
796}
797
798BT_HIDDEN
799void bt_component_remove_destroy_listener(struct bt_component *component,
800 bt_component_destroy_listener_func func, void *data)
801{
d94d92ac 802 uint64_t i;
3230ee6b 803
f6ccaed9
PP
804 BT_ASSERT(component);
805 BT_ASSERT(func);
3230ee6b
PP
806
807 for (i = 0; i < component->destroy_listeners->len; i++) {
808 struct bt_component_destroy_listener *listener =
809 &g_array_index(component->destroy_listeners,
810 struct bt_component_destroy_listener, i);
811
812 if (listener->func == func && listener->data == data) {
813 g_array_remove_index(component->destroy_listeners, i);
814 i--;
d94d92ac 815 BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, "
ab0d387b 816 "func-addr=%p, data-addr=%p",
d94d92ac 817 component, func, data);
3230ee6b
PP
818 }
819 }
820}
c5b9b441
PP
821
822void bt_component_get_ref(const struct bt_component *component)
823{
824 bt_object_get_ref(component);
825}
826
827void bt_component_put_ref(const struct bt_component *component)
828{
829 bt_object_put_ref(component);
830}
This page took 0.079585 seconds and 4 git commands to generate.