Remove component's initialization state flag
[babeltrace.git] / lib / graph / component.c
CommitLineData
de713ce0
JG
1/*
2 * component.c
3 *
4 * Babeltrace Plugin Component
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
ab0d387b
PP
29#define BT_LOG_TAG "COMP"
30#include <babeltrace/lib-logging-internal.h>
31
b2e0c907
PP
32#include <babeltrace/graph/private-component.h>
33#include <babeltrace/graph/component.h>
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>
39#include <babeltrace/graph/private-connection.h>
40#include <babeltrace/graph/connection-internal.h>
41#include <babeltrace/graph/graph-internal.h>
42#include <babeltrace/graph/notification-iterator-internal.h>
43#include <babeltrace/graph/private-notification-iterator.h>
de713ce0 44#include <babeltrace/babeltrace-internal.h>
3d9990ac 45#include <babeltrace/compiler-internal.h>
b8a06801 46#include <babeltrace/ref.h>
c55a9f58 47#include <babeltrace/types.h>
ab0d387b
PP
48#include <babeltrace/values.h>
49#include <babeltrace/values-internal.h>
9ac68eb1 50#include <stdint.h>
ab0d387b 51#include <inttypes.h>
de713ce0 52
7c7c0433
JG
53static
54struct bt_component * (* const component_create_funcs[])(
55 struct bt_component_class *, struct bt_value *) = {
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
7c7c0433
JG
68static
69enum bt_component_status (* const component_validation_funcs[])(
70 struct bt_component *) = {
d3e4dcd8
PP
71 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_validate,
72 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_validate,
73 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_validate,
7c7c0433
JG
74};
75
b8a06801
JG
76static
77void bt_component_destroy(struct bt_object *obj)
78{
79 struct bt_component *component = NULL;
80 struct bt_component_class *component_class = NULL;
3230ee6b 81 int i;
b8a06801
JG
82
83 if (!obj) {
84 return;
85 }
86
bd14d768
PP
87 /*
88 * The component's reference count is 0 if we're here. Increment
89 * it to avoid a double-destroy (possibly infinitely recursive).
90 * This could happen for example if the component's finalization
91 * function does bt_get() (or anything that causes bt_get() to
92 * be called) on itself (ref. count goes from 0 to 1), and then
93 * bt_put(): the reference count would go from 1 to 0 again and
94 * this function would be called again.
95 */
96 obj->ref_count.count++;
b8a06801 97 component = container_of(obj, struct bt_component, base);
ab0d387b
PP
98 BT_LOGD("Destroying component: addr=%p, name=\"%s\", graph-addr=%p",
99 component, bt_component_get_name(component),
100 obj->parent);
3230ee6b
PP
101
102 /* Call destroy listeners in reverse registration order */
ab0d387b
PP
103 BT_LOGD_STR("Calling destroy listeners.");
104
3230ee6b
PP
105 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
106 struct bt_component_destroy_listener *listener =
107 &g_array_index(component->destroy_listeners,
108 struct bt_component_destroy_listener, i);
109
110 listener->func(component, listener->data);
111 }
112
7c7c0433
JG
113 component_class = component->class;
114
115 /*
b8a06801
JG
116 * User data is destroyed first, followed by the concrete component
117 * instance.
118 */
64cadc66 119 if (component->class->methods.finalize) {
ab0d387b 120 BT_LOGD_STR("Calling user's finalization method.");
64cadc66 121 component->class->methods.finalize(
890882ef 122 bt_private_component_from_component(component));
7c7c0433 123 }
b8a06801 124
ab09f844 125 if (component->destroy) {
ab0d387b 126 BT_LOGD_STR("Destroying type-specific data.");
ab09f844
JG
127 component->destroy(component);
128 }
129
72b913fb 130 if (component->input_ports) {
ab0d387b 131 BT_LOGD_STR("Destroying input ports.");
72b913fb
PP
132 g_ptr_array_free(component->input_ports, TRUE);
133 }
b8a06801 134
72b913fb 135 if (component->output_ports) {
ab0d387b 136 BT_LOGD_STR("Destroying output ports.");
72b913fb 137 g_ptr_array_free(component->output_ports, TRUE);
b8a06801
JG
138 }
139
3230ee6b
PP
140 if (component->destroy_listeners) {
141 g_array_free(component->destroy_listeners, TRUE);
142 }
143
ab0d387b
PP
144 if (component->name) {
145 g_string_free(component->name, TRUE);
146 }
147
148 BT_LOGD("Putting component class.");
72b913fb
PP
149 bt_put(component_class);
150 g_free(component);
b8a06801 151}
de713ce0 152
890882ef
PP
153struct bt_component *bt_component_from_private_component(
154 struct bt_private_component *private_component)
38b48196 155{
890882ef 156 return bt_get(bt_component_from_private(private_component));
38b48196
JG
157}
158
890882ef
PP
159enum bt_component_class_type bt_component_get_class_type(
160 struct bt_component *component)
5645cd95 161{
890882ef 162 return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN;
5645cd95
JG
163}
164
72b913fb
PP
165static
166struct bt_port *bt_component_add_port(
167 struct bt_component *component, GPtrArray *ports,
3e9b0023 168 enum bt_port_type port_type, const char *name, void *user_data)
72b913fb
PP
169{
170 size_t i;
171 struct bt_port *new_port = NULL;
1bf957a0 172 struct bt_graph *graph = NULL;
72b913fb 173
ab0d387b
PP
174 if (!name) {
175 BT_LOGW_STR("Invalid parameter: name is NULL.");
72b913fb
PP
176 goto end;
177 }
178
ab0d387b
PP
179 if (strlen(name) == 0) {
180 BT_LOGW_STR("Invalid parameter: name is an empty string.");
181 goto end;
182 }
183
184 BT_LOGD("Adding port to component: comp-addr=%p, comp-name=\"%s\", "
185 "port-type=%s, port-name=\"%s\"", component,
186 bt_component_get_name(component),
187 bt_port_type_string(port_type), name);
188
72b913fb
PP
189 /* Look for a port having the same name. */
190 for (i = 0; i < ports->len; i++) {
191 const char *port_name;
ab0d387b 192 struct bt_port *port = g_ptr_array_index(ports, i);
72b913fb
PP
193
194 port_name = bt_port_get_name(port);
ab0d387b 195 assert(port_name);
72b913fb
PP
196
197 if (!strcmp(name, port_name)) {
198 /* Port name clash, abort. */
ab0d387b
PP
199 BT_LOGW("Invalid parameter: another port with the same name already exists in the component: "
200 "other-port-addr=%p", port);
72b913fb
PP
201 goto end;
202 }
203 }
204
3e9b0023 205 new_port = bt_port_create(component, port_type, name, user_data);
72b913fb 206 if (!new_port) {
ab0d387b 207 BT_LOGE("Cannot create port object.");
72b913fb
PP
208 goto end;
209 }
210
211 /*
212 * No name clash, add the port.
213 * The component is now the port's parent; it should _not_
214 * hold a reference to the port since the port's lifetime
215 * is now protected by the component's own lifetime.
216 */
217 g_ptr_array_add(ports, new_port);
1bf957a0
PP
218
219 /*
220 * Notify the graph's creator that a new port was added.
221 */
222 graph = bt_component_get_graph(component);
223 if (graph) {
224 bt_graph_notify_port_added(graph, new_port);
225 BT_PUT(graph);
226 }
227
ab0d387b
PP
228 BT_LOGD("Created and added port to component: comp-addr=%p, comp-name=\"%s\", "
229 "port-type=%s, port-name=\"%s\", port-addr=%p", component,
230 bt_component_get_name(component),
231 bt_port_type_string(port_type), name, new_port);
232
72b913fb
PP
233end:
234 return new_port;
235}
236
237BT_HIDDEN
544d0515 238int64_t bt_component_get_input_port_count(struct bt_component *comp)
72b913fb
PP
239{
240 assert(comp);
9ac68eb1 241 return (int64_t) comp->input_ports->len;
72b913fb
PP
242}
243
244BT_HIDDEN
544d0515 245int64_t bt_component_get_output_port_count(struct bt_component *comp)
72b913fb
PP
246{
247 assert(comp);
9ac68eb1 248 return (int64_t) comp->output_ports->len;
72b913fb
PP
249}
250
6358c163 251struct bt_component *bt_component_create_with_init_method_data(
7c7c0433 252 struct bt_component_class *component_class, const char *name,
6358c163 253 struct bt_value *params, void *init_method_data)
38b48196 254{
7c7c0433 255 int ret;
38b48196 256 struct bt_component *component = NULL;
d3e4dcd8 257 enum bt_component_class_type type;
38b48196 258
0df078e5
PP
259 bt_get(params);
260
38b48196 261 if (!component_class) {
ab0d387b 262 BT_LOGW_STR("Invalid parameter: component class is NULL.");
38b48196
JG
263 goto end;
264 }
265
7c7c0433 266 type = bt_component_class_get_type(component_class);
d3e4dcd8
PP
267 if (type <= BT_COMPONENT_CLASS_TYPE_UNKNOWN ||
268 type > BT_COMPONENT_CLASS_TYPE_FILTER) {
ab0d387b
PP
269 BT_LOGW("Invalid parameter: unknown component class type: "
270 "type=%s", bt_component_class_type_string(type));
7c7c0433
JG
271 goto end;
272 }
273
ab0d387b
PP
274 BT_LOGD("Creating component from component class: "
275 "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", "
276 "params-addr=%p, init-method-data-addr=%p",
277 component_class, bt_component_class_type_string(type),
278 name, params, init_method_data);
279
0df078e5
PP
280 /*
281 * Parameters must be a map value, but we create a convenient
282 * empty one if it's NULL.
283 */
284 if (params) {
285 if (!bt_value_is_map(params)) {
ab0d387b
PP
286 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
287 "type=%s",
288 bt_value_type_string(bt_value_get_type(params)));
0df078e5
PP
289 goto end;
290 }
291 } else {
292 params = bt_value_map_create();
293 if (!params) {
ab0d387b 294 BT_LOGE_STR("Cannot create map value object.");
0df078e5
PP
295 goto end;
296 }
297 }
298
7c7c0433
JG
299 component = component_create_funcs[type](component_class, params);
300 if (!component) {
ab0d387b 301 BT_LOGE_STR("Cannot create specific component object.");
7c7c0433
JG
302 goto end;
303 }
304
305 bt_object_init(component, bt_component_destroy);
72b913fb
PP
306 component->class = bt_get(component_class);
307 component->destroy = component_destroy_funcs[type];
7c7c0433 308 component->name = g_string_new(name);
4b70dd83 309 if (!component->name) {
ab0d387b 310 BT_LOGE_STR("Failed to allocate one GString.");
7c7c0433
JG
311 BT_PUT(component);
312 goto end;
313 }
314
72b913fb
PP
315 component->input_ports = g_ptr_array_new_with_free_func(
316 bt_object_release);
317 if (!component->input_ports) {
ab0d387b 318 BT_LOGE_STR("Failed to allocate one GPtrArray.");
72b913fb
PP
319 BT_PUT(component);
320 goto end;
321 }
322
323 component->output_ports = g_ptr_array_new_with_free_func(
324 bt_object_release);
325 if (!component->output_ports) {
ab0d387b 326 BT_LOGE_STR("Failed to allocate one GPtrArray.");
72b913fb
PP
327 BT_PUT(component);
328 goto end;
329 }
330
3230ee6b
PP
331 component->destroy_listeners = g_array_new(FALSE, TRUE,
332 sizeof(struct bt_component_destroy_listener));
333 if (!component->destroy_listeners) {
ab0d387b 334 BT_LOGE_STR("Failed to allocate one GArray.");
3230ee6b
PP
335 BT_PUT(component);
336 goto end;
337 }
338
d3e4dcd8 339 if (component_class->methods.init) {
ab0d387b 340 BT_LOGD_STR("Calling user's initialization method.");
890882ef
PP
341 ret = component_class->methods.init(
342 bt_private_component_from_component(component), params,
6358c163 343 init_method_data);
ab0d387b
PP
344 BT_LOGD("User method returned: status=%s",
345 bt_component_status_string(ret));
d3e4dcd8 346 if (ret != BT_COMPONENT_STATUS_OK) {
ab0d387b 347 BT_LOGW_STR("Initialization method failed.");
d3e4dcd8
PP
348 BT_PUT(component);
349 goto end;
350 }
528debdf 351 }
d3e4dcd8 352
7c7c0433 353 ret = component_validation_funcs[type](component);
692b38d2 354 if (ret != BT_COMPONENT_STATUS_OK) {
ab0d387b
PP
355 BT_LOGW("Component is invalid: status=%s",
356 bt_component_status_string(ret));
7c7c0433 357 BT_PUT(component);
38b48196
JG
358 goto end;
359 }
1e4d8103 360
ab0d387b 361 BT_LOGD_STR("Freezing component class.");
1e4d8103 362 bt_component_class_freeze(component->class);
ab0d387b
PP
363 BT_LOGD("Created component from component class: "
364 "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", "
365 "params-addr=%p, init-method-data-addr=%p, comp-addr=%p",
366 component_class, bt_component_class_type_string(type),
367 name, params, init_method_data, component);
368
38b48196 369end:
0df078e5 370 bt_put(params);
38b48196
JG
371 return component;
372}
373
6358c163
PP
374struct bt_component *bt_component_create(
375 struct bt_component_class *component_class, const char *name,
376 struct bt_value *params)
377{
ab0d387b 378 /* bt_component_create_with_init_method_data() logs details */
6358c163
PP
379 return bt_component_create_with_init_method_data(component_class, name,
380 params, NULL);
381}
382
de713ce0
JG
383const char *bt_component_get_name(struct bt_component *component)
384{
385 const char *ret = NULL;
386
387 if (!component) {
ab0d387b 388 BT_LOGW_STR("Invalid parameter: component is NULL.");
de713ce0
JG
389 goto end;
390 }
391
f1222e7d 392 ret = component->name->len == 0 ? NULL : component->name->str;
ab0d387b 393
de713ce0
JG
394end:
395 return ret;
396}
397
38b48196
JG
398struct bt_component_class *bt_component_get_class(
399 struct bt_component *component)
de713ce0 400{
38b48196 401 return component ? bt_get(component->class) : NULL;
de713ce0
JG
402}
403
890882ef
PP
404void *bt_private_component_get_user_data(
405 struct bt_private_component *private_component)
de713ce0 406{
890882ef
PP
407 struct bt_component *component =
408 bt_component_from_private(private_component);
409
38b48196 410 return component ? component->user_data : NULL;
de713ce0
JG
411}
412
890882ef
PP
413enum bt_component_status bt_private_component_set_user_data(
414 struct bt_private_component *private_component,
de713ce0
JG
415 void *data)
416{
890882ef
PP
417 struct bt_component *component =
418 bt_component_from_private(private_component);
de713ce0
JG
419 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
420
ab0d387b
PP
421 if (!component) {
422 BT_LOGW_STR("Invalid parameter: component is NULL.");
423 ret = BT_COMPONENT_STATUS_INVALID;
424 goto end;
425 }
426
de713ce0 427 component->user_data = data;
ab0d387b
PP
428 BT_LOGV("Set component's user data: "
429 "comp-addr=%p, comp-name=\"%s\", user-data-addr=%p",
430 component, bt_component_get_name(component), data);
431
de713ce0
JG
432end:
433 return ret;
434}
366e034f
JG
435
436BT_HIDDEN
f60c8b34 437void bt_component_set_graph(struct bt_component *component,
366e034f
JG
438 struct bt_graph *graph)
439{
f60c8b34
JG
440 struct bt_object *parent = bt_object_get_parent(&component->base);
441
442 assert(!parent || parent == &graph->base);
443 if (!parent) {
444 bt_object_set_parent(component, &graph->base);
445 }
446 bt_put(parent);
366e034f
JG
447}
448
449struct bt_graph *bt_component_get_graph(
450 struct bt_component *component)
451{
452 return (struct bt_graph *) bt_object_get_parent(&component->base);
453}
454
72b913fb 455static
9ac68eb1
PP
456struct bt_port *bt_component_get_port_by_name(GPtrArray *ports,
457 const char *name)
366e034f
JG
458{
459 size_t i;
460 struct bt_port *ret_port = NULL;
461
72b913fb
PP
462 assert(name);
463
366e034f
JG
464 for (i = 0; i < ports->len; i++) {
465 struct bt_port *port = g_ptr_array_index(ports, i);
466 const char *port_name = bt_port_get_name(port);
467
468 if (!port_name) {
469 continue;
470 }
471
472 if (!strcmp(name, port_name)) {
473 ret_port = bt_get(port);
474 break;
475 }
476 }
477
478 return ret_port;
479}
480
481BT_HIDDEN
9ac68eb1 482struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp,
72b913fb
PP
483 const char *name)
484{
485 assert(comp);
486
9ac68eb1 487 return bt_component_get_port_by_name(comp->input_ports, name);
72b913fb
PP
488}
489
490BT_HIDDEN
9ac68eb1 491struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp,
72b913fb
PP
492 const char *name)
493{
494 assert(comp);
495
9ac68eb1 496 return bt_component_get_port_by_name(comp->output_ports, name);
72b913fb
PP
497}
498
499static
9ac68eb1 500struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index)
366e034f
JG
501{
502 struct bt_port *port = NULL;
503
9ac68eb1 504 if (index >= ports->len) {
ab0d387b
PP
505 BT_LOGW("Invalid parameter: index is out of bounds: "
506 "index=%" PRIu64 ", count=%u",
507 index, ports->len);
366e034f
JG
508 goto end;
509 }
510
511 port = bt_get(g_ptr_array_index(ports, index));
512end:
513 return port;
514}
515
516BT_HIDDEN
9ac68eb1
PP
517struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp,
518 uint64_t index)
366e034f 519{
72b913fb 520 assert(comp);
366e034f 521
9ac68eb1 522 return bt_component_get_port_by_index(comp->input_ports, index);
72b913fb 523}
366e034f 524
72b913fb 525BT_HIDDEN
9ac68eb1
PP
526struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp,
527 uint64_t index)
72b913fb
PP
528{
529 assert(comp);
366e034f 530
9ac68eb1 531 return bt_component_get_port_by_index(comp->output_ports, index);
72b913fb 532}
366e034f 533
72b913fb
PP
534BT_HIDDEN
535struct bt_port *bt_component_add_input_port(
3e9b0023
PP
536 struct bt_component *component, const char *name,
537 void *user_data)
72b913fb 538{
ab0d387b 539 /* bt_component_add_port() logs details */
72b913fb 540 return bt_component_add_port(component, component->input_ports,
3e9b0023 541 BT_PORT_TYPE_INPUT, name, user_data);
72b913fb 542}
366e034f 543
72b913fb
PP
544BT_HIDDEN
545struct bt_port *bt_component_add_output_port(
3e9b0023
PP
546 struct bt_component *component, const char *name,
547 void *user_data)
72b913fb 548{
ab0d387b 549 /* bt_component_add_port() logs details */
72b913fb 550 return bt_component_add_port(component, component->output_ports,
3e9b0023 551 BT_PORT_TYPE_OUTPUT, name, user_data);
72b913fb
PP
552}
553
554static
9ac68eb1 555void bt_component_remove_port_by_index(struct bt_component *component,
72b913fb
PP
556 GPtrArray *ports, size_t index)
557{
558 struct bt_port *port;
1bf957a0 559 struct bt_graph *graph;
72b913fb
PP
560
561 assert(ports);
562 assert(index < ports->len);
563 port = g_ptr_array_index(ports, index);
564
ab0d387b
PP
565 BT_LOGD("Removing port from component: "
566 "comp-addr=%p, comp-name=\"%s\", "
567 "port-addr=%p, port-name=\"%s\"",
568 component, bt_component_get_name(component),
569 port, bt_port_get_name(port));
570
72b913fb
PP
571 /* Disconnect both ports of this port's connection, if any */
572 if (port->connection) {
2038affb 573 bt_connection_disconnect_ports(port->connection);
f60c8b34
JG
574 }
575
72b913fb
PP
576 /* Remove from parent's array of ports (weak refs) */
577 g_ptr_array_remove_index(ports, index);
578
579 /* Detach port from its component parent */
580 BT_PUT(port->base.parent);
581
1bf957a0
PP
582 /*
583 * Notify the graph's creator that a port is removed.
584 */
585 graph = bt_component_get_graph(component);
586 if (graph) {
587 bt_graph_notify_port_removed(graph, component, port);
588 BT_PUT(graph);
589 }
ab0d387b
PP
590
591 BT_LOGD("Removed port from component: "
592 "comp-addr=%p, comp-name=\"%s\", "
593 "port-addr=%p, port-name=\"%s\"",
594 component, bt_component_get_name(component),
595 port, bt_port_get_name(port));
366e034f
JG
596}
597
598BT_HIDDEN
599enum bt_component_status bt_component_remove_port(
72b913fb 600 struct bt_component *component, struct bt_port *port)
366e034f
JG
601{
602 size_t i;
603 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
72b913fb 604 GPtrArray *ports = NULL;
366e034f 605
ab0d387b
PP
606 if (!component) {
607 BT_LOGW_STR("Invalid parameter: component is NULL.");
608 status = BT_COMPONENT_STATUS_INVALID;
609 goto end;
610 }
611
612 if (!port) {
613 BT_LOGW_STR("Invalid parameter: port is NULL.");
366e034f
JG
614 status = BT_COMPONENT_STATUS_INVALID;
615 goto end;
616 }
617
72b913fb
PP
618 if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
619 ports = component->input_ports;
620 } else if (bt_port_get_type(port) == BT_PORT_TYPE_OUTPUT) {
621 ports = component->output_ports;
622 }
366e034f 623
72b913fb 624 assert(ports);
366e034f 625
72b913fb
PP
626 for (i = 0; i < ports->len; i++) {
627 struct bt_port *cur_port = g_ptr_array_index(ports, i);
628
629 if (cur_port == port) {
9ac68eb1 630 bt_component_remove_port_by_index(component,
72b913fb 631 ports, i);
366e034f
JG
632 goto end;
633 }
634 }
72b913fb 635
366e034f 636 status = BT_COMPONENT_STATUS_NOT_FOUND;
ab0d387b
PP
637 BT_LOGW("Port to remove from component was not found: "
638 "comp-addr=%p, comp-name=\"%s\", "
639 "port-addr=%p, port-name=\"%s\"",
640 component, bt_component_get_name(component),
641 port, bt_port_get_name(port));
642
366e034f
JG
643end:
644 return status;
645}
f60c8b34
JG
646
647BT_HIDDEN
72b913fb 648enum bt_component_status bt_component_accept_port_connection(
8f4799f7
PP
649 struct bt_component *comp, struct bt_port *self_port,
650 struct bt_port *other_port)
f60c8b34
JG
651{
652 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
653
72b913fb 654 assert(comp);
8f4799f7
PP
655 assert(self_port);
656 assert(other_port);
72b913fb
PP
657
658 if (comp->class->methods.accept_port_connection) {
ab0d387b
PP
659 BT_LOGD("Calling user's \"accept port connection\" method: "
660 "comp-addr=%p, comp-name=\"%s\", "
661 "self-port-addr=%p, self-port-name=\"%s\", "
662 "other-port-addr=%p, other-port-name=\"%s\"",
663 comp, bt_component_get_name(comp),
664 self_port, bt_port_get_name(self_port),
665 other_port, bt_port_get_name(other_port));
72b913fb 666 status = comp->class->methods.accept_port_connection(
890882ef 667 bt_private_component_from_component(comp),
8f4799f7
PP
668 bt_private_port_from_port(self_port),
669 other_port);
ab0d387b
PP
670 BT_LOGD("User method returned: status=%s",
671 bt_component_status_string(status));
f60c8b34
JG
672 }
673
674 return status;
675}
72b913fb 676
0d8b4d8e
PP
677BT_HIDDEN
678void bt_component_port_connected(struct bt_component *comp,
679 struct bt_port *self_port, struct bt_port *other_port)
680{
681 assert(comp);
682 assert(self_port);
683 assert(other_port);
684
685 if (comp->class->methods.port_connected) {
ab0d387b
PP
686 BT_LOGD("Calling user's \"port connected\" method: "
687 "comp-addr=%p, comp-name=\"%s\", "
688 "self-port-addr=%p, self-port-name=\"%s\", "
689 "other-port-addr=%p, other-port-name=\"%s\"",
690 comp, bt_component_get_name(comp),
691 self_port, bt_port_get_name(self_port),
692 other_port, bt_port_get_name(other_port));
0d8b4d8e
PP
693 comp->class->methods.port_connected(
694 bt_private_component_from_component(comp),
695 bt_private_port_from_port(self_port), other_port);
696 }
697}
698
72b913fb
PP
699BT_HIDDEN
700void bt_component_port_disconnected(struct bt_component *comp,
701 struct bt_port *port)
702{
703 assert(comp);
704 assert(port);
705
706 if (comp->class->methods.port_disconnected) {
ab0d387b
PP
707 BT_LOGD("Calling user's \"port disconnected\" method: "
708 "comp-addr=%p, comp-name=\"%s\", "
709 "port-addr=%p, port-name=\"%s\"",
710 comp, bt_component_get_name(comp),
711 port, bt_port_get_name(port));
890882ef
PP
712 comp->class->methods.port_disconnected(
713 bt_private_component_from_component(comp),
714 bt_private_port_from_port(port));
72b913fb
PP
715 }
716}
3230ee6b
PP
717
718BT_HIDDEN
719void bt_component_add_destroy_listener(struct bt_component *component,
720 bt_component_destroy_listener_func func, void *data)
721{
722 struct bt_component_destroy_listener listener;
723
724 assert(component);
725 assert(func);
726 listener.func = func;
727 listener.data = data;
728 g_array_append_val(component->destroy_listeners, listener);
ab0d387b
PP
729 BT_LOGV("Added destroy listener: "
730 "comp-addr=%p, comp-name=\"%s\", "
731 "func-addr=%p, data-addr=%p",
732 component, bt_component_get_name(component),
733 func, data);
3230ee6b
PP
734}
735
736BT_HIDDEN
737void bt_component_remove_destroy_listener(struct bt_component *component,
738 bt_component_destroy_listener_func func, void *data)
739{
740 size_t i;
741
742 assert(component);
743 assert(func);
744
745 for (i = 0; i < component->destroy_listeners->len; i++) {
746 struct bt_component_destroy_listener *listener =
747 &g_array_index(component->destroy_listeners,
748 struct bt_component_destroy_listener, i);
749
750 if (listener->func == func && listener->data == data) {
751 g_array_remove_index(component->destroy_listeners, i);
752 i--;
ab0d387b
PP
753 BT_LOGV("Removed destroy listener: "
754 "comp-addr=%p, comp-name=\"%s\", "
755 "func-addr=%p, data-addr=%p",
756 component, bt_component_get_name(component),
757 func, data);
3230ee6b
PP
758 }
759 }
760}
This page took 0.064011 seconds and 4 git commands to generate.