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