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