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