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