Commit | Line | Data |
---|---|---|
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> | |
fe7265b5 | 43 | #include <babeltrace/graph/private-connection-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> | |
8b45963b | 50 | #include <babeltrace/assert-internal.h> |
9ac68eb1 | 51 | #include <stdint.h> |
ab0d387b | 52 | #include <inttypes.h> |
de713ce0 | 53 | |
7c7c0433 JG |
54 | static |
55 | struct bt_component * (* const component_create_funcs[])( | |
36712f1d | 56 | struct bt_component_class *) = { |
d3e4dcd8 PP |
57 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
58 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, | |
59 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, | |
7c7c0433 JG |
60 | }; |
61 | ||
72b913fb PP |
62 | static |
63 | void (*component_destroy_funcs[])(struct bt_component *) = { | |
64 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy, | |
65 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy, | |
66 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy, | |
67 | }; | |
68 | ||
b8a06801 JG |
69 | static |
70 | void bt_component_destroy(struct bt_object *obj) | |
71 | { | |
72 | struct bt_component *component = NULL; | |
73 | struct bt_component_class *component_class = NULL; | |
3230ee6b | 74 | int i; |
b8a06801 JG |
75 | |
76 | if (!obj) { | |
77 | return; | |
78 | } | |
79 | ||
bd14d768 PP |
80 | /* |
81 | * The component's reference count is 0 if we're here. Increment | |
82 | * it to avoid a double-destroy (possibly infinitely recursive). | |
83 | * This could happen for example if the component's finalization | |
84 | * function does bt_get() (or anything that causes bt_get() to | |
85 | * be called) on itself (ref. count goes from 0 to 1), and then | |
86 | * bt_put(): the reference count would go from 1 to 0 again and | |
87 | * this function would be called again. | |
88 | */ | |
1d7bf349 | 89 | obj->ref_count++; |
b8a06801 | 90 | component = container_of(obj, struct bt_component, base); |
ab0d387b PP |
91 | BT_LOGD("Destroying component: addr=%p, name=\"%s\", graph-addr=%p", |
92 | component, bt_component_get_name(component), | |
93 | obj->parent); | |
3230ee6b PP |
94 | |
95 | /* Call destroy listeners in reverse registration order */ | |
ab0d387b PP |
96 | BT_LOGD_STR("Calling destroy listeners."); |
97 | ||
3230ee6b PP |
98 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
99 | struct bt_component_destroy_listener *listener = | |
100 | &g_array_index(component->destroy_listeners, | |
101 | struct bt_component_destroy_listener, i); | |
102 | ||
103 | listener->func(component, listener->data); | |
104 | } | |
105 | ||
7c7c0433 JG |
106 | component_class = component->class; |
107 | ||
108 | /* | |
36712f1d PP |
109 | * User data is destroyed first, followed by the concrete |
110 | * component instance. Do not finalize if the component's user | |
111 | * initialization method failed in the first place. | |
b8a06801 | 112 | */ |
36712f1d | 113 | if (component->initialized && component->class->methods.finalize) { |
ab0d387b | 114 | BT_LOGD_STR("Calling user's finalization method."); |
64cadc66 | 115 | component->class->methods.finalize( |
890882ef | 116 | bt_private_component_from_component(component)); |
7c7c0433 | 117 | } |
b8a06801 | 118 | |
ab09f844 | 119 | if (component->destroy) { |
ab0d387b | 120 | BT_LOGD_STR("Destroying type-specific data."); |
ab09f844 JG |
121 | component->destroy(component); |
122 | } | |
123 | ||
72b913fb | 124 | if (component->input_ports) { |
ab0d387b | 125 | BT_LOGD_STR("Destroying input ports."); |
72b913fb PP |
126 | g_ptr_array_free(component->input_ports, TRUE); |
127 | } | |
b8a06801 | 128 | |
72b913fb | 129 | if (component->output_ports) { |
ab0d387b | 130 | BT_LOGD_STR("Destroying output ports."); |
72b913fb | 131 | g_ptr_array_free(component->output_ports, TRUE); |
b8a06801 JG |
132 | } |
133 | ||
3230ee6b PP |
134 | if (component->destroy_listeners) { |
135 | g_array_free(component->destroy_listeners, TRUE); | |
136 | } | |
137 | ||
ab0d387b PP |
138 | if (component->name) { |
139 | g_string_free(component->name, TRUE); | |
140 | } | |
141 | ||
142 | BT_LOGD("Putting component class."); | |
72b913fb PP |
143 | bt_put(component_class); |
144 | g_free(component); | |
b8a06801 | 145 | } |
de713ce0 | 146 | |
f7c3ac09 | 147 | struct bt_component *bt_component_borrow_from_private( |
890882ef | 148 | struct bt_private_component *private_component) |
38b48196 | 149 | { |
f7c3ac09 | 150 | return (void *) private_component; |
38b48196 JG |
151 | } |
152 | ||
890882ef PP |
153 | enum bt_component_class_type bt_component_get_class_type( |
154 | struct bt_component *component) | |
5645cd95 | 155 | { |
890882ef | 156 | return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN; |
5645cd95 JG |
157 | } |
158 | ||
72b913fb PP |
159 | static |
160 | struct bt_port *bt_component_add_port( | |
161 | struct bt_component *component, GPtrArray *ports, | |
3e9b0023 | 162 | enum bt_port_type port_type, const char *name, void *user_data) |
72b913fb PP |
163 | { |
164 | size_t i; | |
165 | struct bt_port *new_port = NULL; | |
1bf957a0 | 166 | struct bt_graph *graph = NULL; |
72b913fb | 167 | |
ab0d387b PP |
168 | if (!name) { |
169 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
72b913fb PP |
170 | goto end; |
171 | } | |
172 | ||
ab0d387b PP |
173 | if (strlen(name) == 0) { |
174 | BT_LOGW_STR("Invalid parameter: name is an empty string."); | |
175 | goto end; | |
176 | } | |
177 | ||
178 | BT_LOGD("Adding port to component: comp-addr=%p, comp-name=\"%s\", " | |
179 | "port-type=%s, port-name=\"%s\"", component, | |
180 | bt_component_get_name(component), | |
181 | bt_port_type_string(port_type), name); | |
182 | ||
72b913fb PP |
183 | /* Look for a port having the same name. */ |
184 | for (i = 0; i < ports->len; i++) { | |
185 | const char *port_name; | |
ab0d387b | 186 | struct bt_port *port = g_ptr_array_index(ports, i); |
72b913fb PP |
187 | |
188 | port_name = bt_port_get_name(port); | |
8b45963b | 189 | BT_ASSERT(port_name); |
72b913fb PP |
190 | |
191 | if (!strcmp(name, port_name)) { | |
192 | /* Port name clash, abort. */ | |
ab0d387b PP |
193 | BT_LOGW("Invalid parameter: another port with the same name already exists in the component: " |
194 | "other-port-addr=%p", port); | |
72b913fb PP |
195 | goto end; |
196 | } | |
197 | } | |
198 | ||
3e9b0023 | 199 | new_port = bt_port_create(component, port_type, name, user_data); |
72b913fb | 200 | if (!new_port) { |
ab0d387b | 201 | BT_LOGE("Cannot create port object."); |
72b913fb PP |
202 | goto end; |
203 | } | |
204 | ||
205 | /* | |
206 | * No name clash, add the port. | |
207 | * The component is now the port's parent; it should _not_ | |
208 | * hold a reference to the port since the port's lifetime | |
209 | * is now protected by the component's own lifetime. | |
210 | */ | |
211 | g_ptr_array_add(ports, new_port); | |
1bf957a0 PP |
212 | |
213 | /* | |
214 | * Notify the graph's creator that a new port was added. | |
215 | */ | |
216 | graph = bt_component_get_graph(component); | |
217 | if (graph) { | |
218 | bt_graph_notify_port_added(graph, new_port); | |
219 | BT_PUT(graph); | |
220 | } | |
221 | ||
ab0d387b PP |
222 | BT_LOGD("Created and added port to component: comp-addr=%p, comp-name=\"%s\", " |
223 | "port-type=%s, port-name=\"%s\", port-addr=%p", component, | |
224 | bt_component_get_name(component), | |
225 | bt_port_type_string(port_type), name, new_port); | |
226 | ||
72b913fb PP |
227 | end: |
228 | return new_port; | |
229 | } | |
230 | ||
231 | BT_HIDDEN | |
544d0515 | 232 | int64_t bt_component_get_input_port_count(struct bt_component *comp) |
72b913fb | 233 | { |
8b45963b | 234 | BT_ASSERT(comp); |
9ac68eb1 | 235 | return (int64_t) comp->input_ports->len; |
72b913fb PP |
236 | } |
237 | ||
238 | BT_HIDDEN | |
544d0515 | 239 | int64_t bt_component_get_output_port_count(struct bt_component *comp) |
72b913fb | 240 | { |
8b45963b | 241 | BT_ASSERT(comp); |
9ac68eb1 | 242 | return (int64_t) comp->output_ports->len; |
72b913fb PP |
243 | } |
244 | ||
151a8109 | 245 | BT_HIDDEN |
36712f1d PP |
246 | enum bt_component_status bt_component_create( |
247 | struct bt_component_class *component_class, | |
248 | const char *name, struct bt_component **user_component) | |
38b48196 | 249 | { |
36712f1d | 250 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
38b48196 | 251 | struct bt_component *component = NULL; |
d3e4dcd8 | 252 | enum bt_component_class_type type; |
38b48196 | 253 | |
8b45963b PP |
254 | BT_ASSERT(user_component); |
255 | BT_ASSERT(component_class); | |
256 | BT_ASSERT(name); | |
38b48196 | 257 | |
7c7c0433 | 258 | type = bt_component_class_get_type(component_class); |
36712f1d PP |
259 | BT_LOGD("Creating empty component from component class: " |
260 | "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\"", | |
261 | component_class, bt_component_class_type_string(type), name); | |
262 | component = component_create_funcs[type](component_class); | |
7c7c0433 | 263 | if (!component) { |
ab0d387b | 264 | BT_LOGE_STR("Cannot create specific component object."); |
36712f1d | 265 | status = BT_COMPONENT_STATUS_NOMEM; |
7c7c0433 JG |
266 | goto end; |
267 | } | |
268 | ||
1d7bf349 PP |
269 | bt_object_init_shared_with_parent(&component->base, |
270 | bt_component_destroy); | |
72b913fb PP |
271 | component->class = bt_get(component_class); |
272 | component->destroy = component_destroy_funcs[type]; | |
7c7c0433 | 273 | component->name = g_string_new(name); |
4b70dd83 | 274 | if (!component->name) { |
ab0d387b | 275 | BT_LOGE_STR("Failed to allocate one GString."); |
36712f1d | 276 | status = BT_COMPONENT_STATUS_NOMEM; |
7c7c0433 JG |
277 | goto end; |
278 | } | |
279 | ||
72b913fb | 280 | component->input_ports = g_ptr_array_new_with_free_func( |
1d7bf349 | 281 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 282 | if (!component->input_ports) { |
ab0d387b | 283 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
36712f1d | 284 | status = BT_COMPONENT_STATUS_NOMEM; |
72b913fb PP |
285 | goto end; |
286 | } | |
287 | ||
288 | component->output_ports = g_ptr_array_new_with_free_func( | |
1d7bf349 | 289 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 290 | if (!component->output_ports) { |
ab0d387b | 291 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
36712f1d | 292 | status = BT_COMPONENT_STATUS_NOMEM; |
72b913fb PP |
293 | goto end; |
294 | } | |
295 | ||
3230ee6b PP |
296 | component->destroy_listeners = g_array_new(FALSE, TRUE, |
297 | sizeof(struct bt_component_destroy_listener)); | |
298 | if (!component->destroy_listeners) { | |
ab0d387b | 299 | BT_LOGE_STR("Failed to allocate one GArray."); |
36712f1d | 300 | status = BT_COMPONENT_STATUS_NOMEM; |
3230ee6b PP |
301 | goto end; |
302 | } | |
303 | ||
36712f1d PP |
304 | BT_LOGD("Created empty component from component class: " |
305 | "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", comp-addr=%p", | |
306 | component_class, bt_component_class_type_string(type), name, | |
307 | component); | |
308 | BT_MOVE(*user_component, component); | |
ab0d387b | 309 | |
38b48196 | 310 | end: |
36712f1d PP |
311 | bt_put(component); |
312 | return status; | |
6358c163 PP |
313 | } |
314 | ||
de713ce0 JG |
315 | const char *bt_component_get_name(struct bt_component *component) |
316 | { | |
317 | const char *ret = NULL; | |
318 | ||
319 | if (!component) { | |
ab0d387b | 320 | BT_LOGW_STR("Invalid parameter: component is NULL."); |
de713ce0 JG |
321 | goto end; |
322 | } | |
323 | ||
f1222e7d | 324 | ret = component->name->len == 0 ? NULL : component->name->str; |
ab0d387b | 325 | |
de713ce0 JG |
326 | end: |
327 | return ret; | |
328 | } | |
329 | ||
38b48196 JG |
330 | struct bt_component_class *bt_component_get_class( |
331 | struct bt_component *component) | |
de713ce0 | 332 | { |
38b48196 | 333 | return component ? bt_get(component->class) : NULL; |
de713ce0 JG |
334 | } |
335 | ||
890882ef PP |
336 | void *bt_private_component_get_user_data( |
337 | struct bt_private_component *private_component) | |
de713ce0 | 338 | { |
890882ef | 339 | struct bt_component *component = |
41765449 | 340 | bt_component_borrow_from_private(private_component); |
890882ef | 341 | |
38b48196 | 342 | return component ? component->user_data : NULL; |
de713ce0 JG |
343 | } |
344 | ||
890882ef PP |
345 | enum bt_component_status bt_private_component_set_user_data( |
346 | struct bt_private_component *private_component, | |
de713ce0 JG |
347 | void *data) |
348 | { | |
890882ef | 349 | struct bt_component *component = |
41765449 | 350 | bt_component_borrow_from_private(private_component); |
de713ce0 JG |
351 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
352 | ||
ab0d387b PP |
353 | if (!component) { |
354 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
355 | ret = BT_COMPONENT_STATUS_INVALID; | |
356 | goto end; | |
357 | } | |
358 | ||
de713ce0 | 359 | component->user_data = data; |
ab0d387b PP |
360 | BT_LOGV("Set component's user data: " |
361 | "comp-addr=%p, comp-name=\"%s\", user-data-addr=%p", | |
362 | component, bt_component_get_name(component), data); | |
363 | ||
de713ce0 JG |
364 | end: |
365 | return ret; | |
366 | } | |
366e034f JG |
367 | |
368 | BT_HIDDEN | |
f60c8b34 | 369 | void bt_component_set_graph(struct bt_component *component, |
366e034f JG |
370 | struct bt_graph *graph) |
371 | { | |
1d7bf349 PP |
372 | bt_object_set_parent(&component->base, |
373 | graph ? &graph->base : NULL); | |
366e034f JG |
374 | } |
375 | ||
f7c3ac09 | 376 | struct bt_graph *bt_component_borrow_graph(struct bt_component *component) |
366e034f | 377 | { |
f7c3ac09 | 378 | return (struct bt_graph *) bt_object_borrow_parent(&component->base); |
366e034f JG |
379 | } |
380 | ||
72b913fb | 381 | static |
9ac68eb1 PP |
382 | struct bt_port *bt_component_get_port_by_name(GPtrArray *ports, |
383 | const char *name) | |
366e034f JG |
384 | { |
385 | size_t i; | |
386 | struct bt_port *ret_port = NULL; | |
387 | ||
8b45963b | 388 | BT_ASSERT(name); |
72b913fb | 389 | |
366e034f JG |
390 | for (i = 0; i < ports->len; i++) { |
391 | struct bt_port *port = g_ptr_array_index(ports, i); | |
392 | const char *port_name = bt_port_get_name(port); | |
393 | ||
394 | if (!port_name) { | |
395 | continue; | |
396 | } | |
397 | ||
398 | if (!strcmp(name, port_name)) { | |
399 | ret_port = bt_get(port); | |
400 | break; | |
401 | } | |
402 | } | |
403 | ||
404 | return ret_port; | |
405 | } | |
406 | ||
407 | BT_HIDDEN | |
9ac68eb1 | 408 | struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp, |
72b913fb PP |
409 | const char *name) |
410 | { | |
8b45963b | 411 | BT_ASSERT(comp); |
72b913fb | 412 | |
9ac68eb1 | 413 | return bt_component_get_port_by_name(comp->input_ports, name); |
72b913fb PP |
414 | } |
415 | ||
416 | BT_HIDDEN | |
9ac68eb1 | 417 | struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp, |
72b913fb PP |
418 | const char *name) |
419 | { | |
8b45963b | 420 | BT_ASSERT(comp); |
72b913fb | 421 | |
9ac68eb1 | 422 | return bt_component_get_port_by_name(comp->output_ports, name); |
72b913fb PP |
423 | } |
424 | ||
425 | static | |
9ac68eb1 | 426 | struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index) |
366e034f JG |
427 | { |
428 | struct bt_port *port = NULL; | |
429 | ||
9ac68eb1 | 430 | if (index >= ports->len) { |
ab0d387b PP |
431 | BT_LOGW("Invalid parameter: index is out of bounds: " |
432 | "index=%" PRIu64 ", count=%u", | |
433 | index, ports->len); | |
366e034f JG |
434 | goto end; |
435 | } | |
436 | ||
437 | port = bt_get(g_ptr_array_index(ports, index)); | |
438 | end: | |
439 | return port; | |
440 | } | |
441 | ||
442 | BT_HIDDEN | |
9ac68eb1 PP |
443 | struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp, |
444 | uint64_t index) | |
366e034f | 445 | { |
8b45963b | 446 | BT_ASSERT(comp); |
366e034f | 447 | |
9ac68eb1 | 448 | return bt_component_get_port_by_index(comp->input_ports, index); |
72b913fb | 449 | } |
366e034f | 450 | |
72b913fb | 451 | BT_HIDDEN |
9ac68eb1 PP |
452 | struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp, |
453 | uint64_t index) | |
72b913fb | 454 | { |
8b45963b | 455 | BT_ASSERT(comp); |
366e034f | 456 | |
9ac68eb1 | 457 | return bt_component_get_port_by_index(comp->output_ports, index); |
72b913fb | 458 | } |
366e034f | 459 | |
72b913fb PP |
460 | BT_HIDDEN |
461 | struct bt_port *bt_component_add_input_port( | |
3e9b0023 PP |
462 | struct bt_component *component, const char *name, |
463 | void *user_data) | |
72b913fb | 464 | { |
ab0d387b | 465 | /* bt_component_add_port() logs details */ |
72b913fb | 466 | return bt_component_add_port(component, component->input_ports, |
3e9b0023 | 467 | BT_PORT_TYPE_INPUT, name, user_data); |
72b913fb | 468 | } |
366e034f | 469 | |
72b913fb PP |
470 | BT_HIDDEN |
471 | struct bt_port *bt_component_add_output_port( | |
3e9b0023 PP |
472 | struct bt_component *component, const char *name, |
473 | void *user_data) | |
72b913fb | 474 | { |
ab0d387b | 475 | /* bt_component_add_port() logs details */ |
72b913fb | 476 | return bt_component_add_port(component, component->output_ports, |
3e9b0023 | 477 | BT_PORT_TYPE_OUTPUT, name, user_data); |
72b913fb PP |
478 | } |
479 | ||
480 | static | |
9ac68eb1 | 481 | void bt_component_remove_port_by_index(struct bt_component *component, |
72b913fb PP |
482 | GPtrArray *ports, size_t index) |
483 | { | |
484 | struct bt_port *port; | |
1bf957a0 | 485 | struct bt_graph *graph; |
72b913fb | 486 | |
8b45963b PP |
487 | BT_ASSERT(ports); |
488 | BT_ASSERT(index < ports->len); | |
72b913fb PP |
489 | port = g_ptr_array_index(ports, index); |
490 | ||
ab0d387b PP |
491 | BT_LOGD("Removing port from component: " |
492 | "comp-addr=%p, comp-name=\"%s\", " | |
493 | "port-addr=%p, port-name=\"%s\"", | |
494 | component, bt_component_get_name(component), | |
495 | port, bt_port_get_name(port)); | |
496 | ||
72b913fb PP |
497 | /* Disconnect both ports of this port's connection, if any */ |
498 | if (port->connection) { | |
c42ea0af | 499 | bt_connection_end(port->connection, true); |
f60c8b34 JG |
500 | } |
501 | ||
72b913fb PP |
502 | /* Remove from parent's array of ports (weak refs) */ |
503 | g_ptr_array_remove_index(ports, index); | |
504 | ||
505 | /* Detach port from its component parent */ | |
506 | BT_PUT(port->base.parent); | |
507 | ||
1bf957a0 PP |
508 | /* |
509 | * Notify the graph's creator that a port is removed. | |
510 | */ | |
511 | graph = bt_component_get_graph(component); | |
512 | if (graph) { | |
513 | bt_graph_notify_port_removed(graph, component, port); | |
514 | BT_PUT(graph); | |
515 | } | |
ab0d387b PP |
516 | |
517 | BT_LOGD("Removed port from component: " | |
518 | "comp-addr=%p, comp-name=\"%s\", " | |
519 | "port-addr=%p, port-name=\"%s\"", | |
520 | component, bt_component_get_name(component), | |
521 | port, bt_port_get_name(port)); | |
366e034f JG |
522 | } |
523 | ||
524 | BT_HIDDEN | |
525 | enum bt_component_status bt_component_remove_port( | |
72b913fb | 526 | struct bt_component *component, struct bt_port *port) |
366e034f JG |
527 | { |
528 | size_t i; | |
529 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; | |
72b913fb | 530 | GPtrArray *ports = NULL; |
366e034f | 531 | |
ab0d387b PP |
532 | if (!component) { |
533 | BT_LOGW_STR("Invalid parameter: component is NULL."); | |
534 | status = BT_COMPONENT_STATUS_INVALID; | |
535 | goto end; | |
536 | } | |
537 | ||
538 | if (!port) { | |
539 | BT_LOGW_STR("Invalid parameter: port is NULL."); | |
366e034f JG |
540 | status = BT_COMPONENT_STATUS_INVALID; |
541 | goto end; | |
542 | } | |
543 | ||
72b913fb PP |
544 | if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) { |
545 | ports = component->input_ports; | |
546 | } else if (bt_port_get_type(port) == BT_PORT_TYPE_OUTPUT) { | |
547 | ports = component->output_ports; | |
548 | } | |
366e034f | 549 | |
8b45963b | 550 | BT_ASSERT(ports); |
366e034f | 551 | |
72b913fb PP |
552 | for (i = 0; i < ports->len; i++) { |
553 | struct bt_port *cur_port = g_ptr_array_index(ports, i); | |
554 | ||
555 | if (cur_port == port) { | |
9ac68eb1 | 556 | bt_component_remove_port_by_index(component, |
72b913fb | 557 | ports, i); |
366e034f JG |
558 | goto end; |
559 | } | |
560 | } | |
72b913fb | 561 | |
366e034f | 562 | status = BT_COMPONENT_STATUS_NOT_FOUND; |
ab0d387b PP |
563 | BT_LOGW("Port to remove from component was not found: " |
564 | "comp-addr=%p, comp-name=\"%s\", " | |
565 | "port-addr=%p, port-name=\"%s\"", | |
566 | component, bt_component_get_name(component), | |
567 | port, bt_port_get_name(port)); | |
568 | ||
366e034f JG |
569 | end: |
570 | return status; | |
571 | } | |
f60c8b34 JG |
572 | |
573 | BT_HIDDEN | |
72b913fb | 574 | enum bt_component_status bt_component_accept_port_connection( |
8f4799f7 PP |
575 | struct bt_component *comp, struct bt_port *self_port, |
576 | struct bt_port *other_port) | |
f60c8b34 JG |
577 | { |
578 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; | |
579 | ||
8b45963b PP |
580 | BT_ASSERT(comp); |
581 | BT_ASSERT(self_port); | |
582 | BT_ASSERT(other_port); | |
72b913fb PP |
583 | |
584 | if (comp->class->methods.accept_port_connection) { | |
ab0d387b PP |
585 | BT_LOGD("Calling user's \"accept port connection\" method: " |
586 | "comp-addr=%p, comp-name=\"%s\", " | |
587 | "self-port-addr=%p, self-port-name=\"%s\", " | |
588 | "other-port-addr=%p, other-port-name=\"%s\"", | |
589 | comp, bt_component_get_name(comp), | |
590 | self_port, bt_port_get_name(self_port), | |
591 | other_port, bt_port_get_name(other_port)); | |
72b913fb | 592 | status = comp->class->methods.accept_port_connection( |
890882ef | 593 | bt_private_component_from_component(comp), |
8f4799f7 PP |
594 | bt_private_port_from_port(self_port), |
595 | other_port); | |
ab0d387b PP |
596 | BT_LOGD("User method returned: status=%s", |
597 | bt_component_status_string(status)); | |
f60c8b34 JG |
598 | } |
599 | ||
600 | return status; | |
601 | } | |
72b913fb | 602 | |
0d8b4d8e | 603 | BT_HIDDEN |
634f394c | 604 | enum bt_component_status bt_component_port_connected(struct bt_component *comp, |
0d8b4d8e PP |
605 | struct bt_port *self_port, struct bt_port *other_port) |
606 | { | |
634f394c PP |
607 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
608 | ||
8b45963b PP |
609 | BT_ASSERT(comp); |
610 | BT_ASSERT(self_port); | |
611 | BT_ASSERT(other_port); | |
0d8b4d8e PP |
612 | |
613 | if (comp->class->methods.port_connected) { | |
ab0d387b PP |
614 | BT_LOGD("Calling user's \"port connected\" method: " |
615 | "comp-addr=%p, comp-name=\"%s\", " | |
616 | "self-port-addr=%p, self-port-name=\"%s\", " | |
617 | "other-port-addr=%p, other-port-name=\"%s\"", | |
618 | comp, bt_component_get_name(comp), | |
619 | self_port, bt_port_get_name(self_port), | |
620 | other_port, bt_port_get_name(other_port)); | |
634f394c | 621 | status = comp->class->methods.port_connected( |
0d8b4d8e PP |
622 | bt_private_component_from_component(comp), |
623 | bt_private_port_from_port(self_port), other_port); | |
624 | } | |
634f394c PP |
625 | |
626 | return status; | |
0d8b4d8e PP |
627 | } |
628 | ||
72b913fb PP |
629 | BT_HIDDEN |
630 | void bt_component_port_disconnected(struct bt_component *comp, | |
631 | struct bt_port *port) | |
632 | { | |
8b45963b PP |
633 | BT_ASSERT(comp); |
634 | BT_ASSERT(port); | |
72b913fb PP |
635 | |
636 | if (comp->class->methods.port_disconnected) { | |
ab0d387b PP |
637 | BT_LOGD("Calling user's \"port disconnected\" method: " |
638 | "comp-addr=%p, comp-name=\"%s\", " | |
639 | "port-addr=%p, port-name=\"%s\"", | |
640 | comp, bt_component_get_name(comp), | |
641 | port, bt_port_get_name(port)); | |
890882ef PP |
642 | comp->class->methods.port_disconnected( |
643 | bt_private_component_from_component(comp), | |
644 | bt_private_port_from_port(port)); | |
72b913fb PP |
645 | } |
646 | } | |
3230ee6b PP |
647 | |
648 | BT_HIDDEN | |
649 | void bt_component_add_destroy_listener(struct bt_component *component, | |
650 | bt_component_destroy_listener_func func, void *data) | |
651 | { | |
652 | struct bt_component_destroy_listener listener; | |
653 | ||
8b45963b PP |
654 | BT_ASSERT(component); |
655 | BT_ASSERT(func); | |
3230ee6b PP |
656 | listener.func = func; |
657 | listener.data = data; | |
658 | g_array_append_val(component->destroy_listeners, listener); | |
ab0d387b PP |
659 | BT_LOGV("Added destroy listener: " |
660 | "comp-addr=%p, comp-name=\"%s\", " | |
661 | "func-addr=%p, data-addr=%p", | |
662 | component, bt_component_get_name(component), | |
663 | func, data); | |
3230ee6b PP |
664 | } |
665 | ||
666 | BT_HIDDEN | |
667 | void bt_component_remove_destroy_listener(struct bt_component *component, | |
668 | bt_component_destroy_listener_func func, void *data) | |
669 | { | |
670 | size_t i; | |
671 | ||
8b45963b PP |
672 | BT_ASSERT(component); |
673 | BT_ASSERT(func); | |
3230ee6b PP |
674 | |
675 | for (i = 0; i < component->destroy_listeners->len; i++) { | |
676 | struct bt_component_destroy_listener *listener = | |
677 | &g_array_index(component->destroy_listeners, | |
678 | struct bt_component_destroy_listener, i); | |
679 | ||
680 | if (listener->func == func && listener->data == data) { | |
681 | g_array_remove_index(component->destroy_listeners, i); | |
682 | i--; | |
ab0d387b PP |
683 | BT_LOGV("Removed destroy listener: " |
684 | "comp-addr=%p, comp-name=\"%s\", " | |
685 | "func-addr=%p, data-addr=%p", | |
686 | component, bt_component_get_name(component), | |
687 | func, data); | |
3230ee6b PP |
688 | } |
689 | } | |
690 | } |