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