Commit | Line | Data |
---|---|---|
de713ce0 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
e2f7325d | 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de713ce0 | 5 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
de713ce0 JG |
6 | */ |
7 | ||
350ad6c1 | 8 | #define BT_LOG_TAG "LIB/COMPONENT" |
c2d9d9cf | 9 | #include "lib/logging.h" |
ab0d387b | 10 | |
e874da19 | 11 | #include "common/common.h" |
578e048b MJ |
12 | #include "common/assert.h" |
13 | #include "lib/assert-pre.h" | |
f6f301d7 | 14 | #include "lib/assert-post.h" |
3fadfbc0 | 15 | #include <babeltrace2/graph/self-component.h> |
43c59509 PP |
16 | #include <babeltrace2/graph/component.h> |
17 | #include <babeltrace2/graph/graph.h> | |
91d81473 | 18 | #include "common/macros.h" |
578e048b | 19 | #include "compat/compiler.h" |
3fadfbc0 MJ |
20 | #include <babeltrace2/types.h> |
21 | #include <babeltrace2/value.h> | |
578e048b | 22 | #include "lib/value.h" |
9ac68eb1 | 23 | #include <stdint.h> |
ab0d387b | 24 | #include <inttypes.h> |
de713ce0 | 25 | |
578e048b MJ |
26 | #include "component.h" |
27 | #include "component-class.h" | |
28 | #include "component-source.h" | |
29 | #include "component-filter.h" | |
30 | #include "component-sink.h" | |
31 | #include "connection.h" | |
32 | #include "graph.h" | |
33 | #include "message/iterator.h" | |
34 | #include "port.h" | |
d24d5663 | 35 | #include "lib/func-status.h" |
578e048b | 36 | |
7c7c0433 JG |
37 | static |
38 | struct bt_component * (* const component_create_funcs[])( | |
0d72b8c3 | 39 | const struct bt_component_class *) = { |
d3e4dcd8 PP |
40 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
41 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, | |
42 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, | |
7c7c0433 JG |
43 | }; |
44 | ||
72b913fb PP |
45 | static |
46 | void (*component_destroy_funcs[])(struct bt_component *) = { | |
47 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy, | |
48 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy, | |
49 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy, | |
50 | }; | |
51 | ||
b8a06801 | 52 | static |
d94d92ac PP |
53 | void finalize_component(struct bt_component *comp) |
54 | { | |
55 | typedef void (*method_t)(void *); | |
56 | ||
57 | method_t method = NULL; | |
58 | ||
59 | BT_ASSERT(comp); | |
60 | ||
61 | switch (comp->class->type) { | |
62 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
63 | { | |
64 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
65 | ||
66 | method = (method_t) src_cc->methods.finalize; | |
67 | break; | |
68 | } | |
69 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
70 | { | |
71 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
72 | ||
73 | method = (method_t) flt_cc->methods.finalize; | |
74 | break; | |
75 | } | |
76 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
77 | { | |
78 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
79 | ||
80 | method = (method_t) sink_cc->methods.finalize; | |
81 | break; | |
82 | } | |
83 | default: | |
498e7994 | 84 | bt_common_abort(); |
d94d92ac PP |
85 | } |
86 | ||
87 | if (method) { | |
42a63165 SM |
88 | const struct bt_error *saved_error; |
89 | ||
90 | saved_error = bt_current_thread_take_error(); | |
91 | ||
3f7d4d90 | 92 | BT_LIB_LOGI("Calling user's component finalization method: " |
d94d92ac PP |
93 | "%![comp-]+c", comp); |
94 | method(comp); | |
42a63165 SM |
95 | BT_ASSERT_POST_NO_ERROR(); |
96 | ||
97 | if (saved_error) { | |
98 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error); | |
99 | } | |
d94d92ac PP |
100 | } |
101 | } | |
102 | ||
103 | static | |
104 | void destroy_component(struct bt_object *obj) | |
b8a06801 JG |
105 | { |
106 | struct bt_component *component = NULL; | |
3230ee6b | 107 | int i; |
b8a06801 JG |
108 | |
109 | if (!obj) { | |
110 | return; | |
111 | } | |
112 | ||
bd14d768 PP |
113 | /* |
114 | * The component's reference count is 0 if we're here. Increment | |
115 | * it to avoid a double-destroy (possibly infinitely recursive). | |
116 | * This could happen for example if the component's finalization | |
d94d92ac PP |
117 | * function does bt_object_get_ref() (or anything that causes |
118 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
119 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
120 | * count would go from 1 to 0 again and this function would be | |
121 | * called again. | |
bd14d768 | 122 | */ |
3fea54f6 | 123 | obj->ref_count++; |
b8a06801 | 124 | component = container_of(obj, struct bt_component, base); |
3f7d4d90 | 125 | BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g", |
d94d92ac | 126 | component, bt_component_borrow_graph(component)); |
3230ee6b PP |
127 | |
128 | /* Call destroy listeners in reverse registration order */ | |
ab0d387b PP |
129 | BT_LOGD_STR("Calling destroy listeners."); |
130 | ||
3230ee6b PP |
131 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
132 | struct bt_component_destroy_listener *listener = | |
133 | &g_array_index(component->destroy_listeners, | |
134 | struct bt_component_destroy_listener, i); | |
135 | ||
136 | listener->func(component, listener->data); | |
137 | } | |
138 | ||
7c7c0433 | 139 | /* |
36712f1d PP |
140 | * User data is destroyed first, followed by the concrete |
141 | * component instance. Do not finalize if the component's user | |
142 | * initialization method failed in the first place. | |
b8a06801 | 143 | */ |
d94d92ac PP |
144 | if (component->initialized) { |
145 | finalize_component(component); | |
7c7c0433 | 146 | } |
b8a06801 | 147 | |
ab09f844 | 148 | if (component->destroy) { |
ab0d387b | 149 | BT_LOGD_STR("Destroying type-specific data."); |
ab09f844 JG |
150 | component->destroy(component); |
151 | } | |
152 | ||
72b913fb | 153 | if (component->input_ports) { |
ab0d387b | 154 | BT_LOGD_STR("Destroying input ports."); |
72b913fb | 155 | g_ptr_array_free(component->input_ports, TRUE); |
d94d92ac | 156 | component->input_ports = NULL; |
72b913fb | 157 | } |
b8a06801 | 158 | |
72b913fb | 159 | if (component->output_ports) { |
ab0d387b | 160 | BT_LOGD_STR("Destroying output ports."); |
72b913fb | 161 | g_ptr_array_free(component->output_ports, TRUE); |
d94d92ac | 162 | component->output_ports = NULL; |
b8a06801 JG |
163 | } |
164 | ||
3230ee6b PP |
165 | if (component->destroy_listeners) { |
166 | g_array_free(component->destroy_listeners, TRUE); | |
d94d92ac | 167 | component->destroy_listeners = NULL; |
3230ee6b PP |
168 | } |
169 | ||
ab0d387b PP |
170 | if (component->name) { |
171 | g_string_free(component->name, TRUE); | |
d94d92ac | 172 | component->name = NULL; |
ab0d387b PP |
173 | } |
174 | ||
d94d92ac PP |
175 | BT_LOGD_STR("Putting component class."); |
176 | BT_OBJECT_PUT_REF_AND_RESET(component->class); | |
72b913fb | 177 | g_free(component); |
b8a06801 | 178 | } |
de713ce0 | 179 | |
890882ef | 180 | enum bt_component_class_type bt_component_get_class_type( |
0d72b8c3 | 181 | const struct bt_component *component) |
5645cd95 | 182 | { |
bdb288b3 | 183 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 184 | return component->class->type; |
5645cd95 JG |
185 | } |
186 | ||
72b913fb | 187 | static |
d24d5663 | 188 | enum bt_self_component_add_port_status add_port( |
72b913fb | 189 | struct bt_component *component, GPtrArray *ports, |
8cc56726 SM |
190 | enum bt_port_type port_type, const char *name, void *user_data, |
191 | struct bt_port **port) | |
72b913fb | 192 | { |
72b913fb | 193 | struct bt_port *new_port = NULL; |
1bf957a0 | 194 | struct bt_graph *graph = NULL; |
d24d5663 | 195 | enum bt_self_component_add_port_status status; |
72b913fb | 196 | |
d94d92ac PP |
197 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
198 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
199 | BT_ASSERT_PRE(strlen(name) > 0, "Name is empty"); | |
200 | graph = bt_component_borrow_graph(component); | |
5badd463 PP |
201 | BT_ASSERT_PRE( |
202 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
203 | "Component's graph is already configured: " |
204 | "%![comp-]+c, %![graph-]+g", component, graph); | |
72b913fb | 205 | |
d94d92ac | 206 | // TODO: Validate that the name is not already used. |
ab0d387b | 207 | |
3f7d4d90 | 208 | BT_LIB_LOGI("Adding port to component: %![comp-]+c, " |
ab0d387b | 209 | "port-type=%s, port-name=\"%s\"", component, |
ab0d387b PP |
210 | bt_port_type_string(port_type), name); |
211 | ||
3e9b0023 | 212 | new_port = bt_port_create(component, port_type, name, user_data); |
72b913fb | 213 | if (!new_port) { |
870631a2 | 214 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object."); |
d24d5663 | 215 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
8cc56726 | 216 | goto error; |
72b913fb PP |
217 | } |
218 | ||
219 | /* | |
220 | * No name clash, add the port. | |
221 | * The component is now the port's parent; it should _not_ | |
222 | * hold a reference to the port since the port's lifetime | |
223 | * is now protected by the component's own lifetime. | |
224 | */ | |
225 | g_ptr_array_add(ports, new_port); | |
1bf957a0 PP |
226 | |
227 | /* | |
228 | * Notify the graph's creator that a new port was added. | |
229 | */ | |
398454ed | 230 | graph = bt_component_borrow_graph(component); |
1bf957a0 | 231 | if (graph) { |
d24d5663 | 232 | enum bt_graph_listener_func_status listener_status; |
8cc56726 SM |
233 | |
234 | listener_status = bt_graph_notify_port_added(graph, new_port); | |
d24d5663 | 235 | if (listener_status != BT_FUNC_STATUS_OK) { |
8cc56726 | 236 | bt_graph_make_faulty(graph); |
d24d5663 | 237 | status = (int) listener_status; |
8cc56726 SM |
238 | goto error; |
239 | } | |
1bf957a0 PP |
240 | } |
241 | ||
3f7d4d90 | 242 | BT_LIB_LOGI("Created and added port to component: " |
d94d92ac | 243 | "%![comp-]+c, %![port-]+p", component, new_port); |
ab0d387b | 244 | |
8cc56726 | 245 | *port = new_port; |
d24d5663 | 246 | status = BT_FUNC_STATUS_OK; |
8cc56726 SM |
247 | |
248 | goto end; | |
249 | error: | |
250 | /* | |
251 | * We need to release the reference that we would otherwise have | |
252 | * returned to the caller. | |
253 | */ | |
254 | BT_PORT_PUT_REF_AND_RESET(new_port); | |
255 | ||
72b913fb | 256 | end: |
8cc56726 | 257 | return status; |
72b913fb PP |
258 | } |
259 | ||
260 | BT_HIDDEN | |
0d72b8c3 | 261 | uint64_t bt_component_get_input_port_count(const struct bt_component *comp) |
72b913fb | 262 | { |
bdb288b3 | 263 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac | 264 | return (uint64_t) comp->input_ports->len; |
72b913fb PP |
265 | } |
266 | ||
267 | BT_HIDDEN | |
0d72b8c3 | 268 | uint64_t bt_component_get_output_port_count(const struct bt_component *comp) |
72b913fb | 269 | { |
bdb288b3 | 270 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac | 271 | return (uint64_t) comp->output_ports->len; |
72b913fb PP |
272 | } |
273 | ||
dd8a4547 | 274 | BT_HIDDEN |
d94d92ac | 275 | int bt_component_create(struct bt_component_class *component_class, |
e874da19 PP |
276 | const char *name, bt_logging_level log_level, |
277 | struct bt_component **user_component) | |
38b48196 | 278 | { |
d94d92ac | 279 | int ret = 0; |
38b48196 | 280 | struct bt_component *component = NULL; |
d3e4dcd8 | 281 | enum bt_component_class_type type; |
38b48196 | 282 | |
f6ccaed9 PP |
283 | BT_ASSERT(user_component); |
284 | BT_ASSERT(component_class); | |
285 | BT_ASSERT(name); | |
7c7c0433 | 286 | type = bt_component_class_get_type(component_class); |
3f7d4d90 | 287 | BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, " |
e874da19 PP |
288 | "comp-name=\"%s\", log-level=%s", component_class, name, |
289 | bt_common_logging_level_string(log_level)); | |
36712f1d | 290 | component = component_create_funcs[type](component_class); |
7c7c0433 | 291 | if (!component) { |
870631a2 PP |
292 | BT_LIB_LOGE_APPEND_CAUSE( |
293 | "Cannot create specific component object."); | |
d94d92ac | 294 | ret = -1; |
7c7c0433 JG |
295 | goto end; |
296 | } | |
297 | ||
d0fea130 | 298 | bt_object_init_shared_with_parent(&component->base, destroy_component); |
398454ed | 299 | component->class = component_class; |
6871026b | 300 | bt_object_get_ref_no_null_check(component->class); |
72b913fb | 301 | component->destroy = component_destroy_funcs[type]; |
7c7c0433 | 302 | component->name = g_string_new(name); |
4b70dd83 | 303 | if (!component->name) { |
870631a2 | 304 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); |
d94d92ac | 305 | ret = -1; |
7c7c0433 JG |
306 | goto end; |
307 | } | |
308 | ||
e874da19 | 309 | component->log_level = log_level; |
72b913fb | 310 | component->input_ports = g_ptr_array_new_with_free_func( |
3fea54f6 | 311 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 312 | if (!component->input_ports) { |
870631a2 | 313 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
d94d92ac | 314 | ret = -1; |
72b913fb PP |
315 | goto end; |
316 | } | |
317 | ||
318 | component->output_ports = g_ptr_array_new_with_free_func( | |
3fea54f6 | 319 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 320 | if (!component->output_ports) { |
870631a2 | 321 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
d94d92ac | 322 | ret = -1; |
72b913fb PP |
323 | goto end; |
324 | } | |
325 | ||
3230ee6b PP |
326 | component->destroy_listeners = g_array_new(FALSE, TRUE, |
327 | sizeof(struct bt_component_destroy_listener)); | |
328 | if (!component->destroy_listeners) { | |
870631a2 | 329 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
d94d92ac | 330 | ret = -1; |
3230ee6b PP |
331 | goto end; |
332 | } | |
333 | ||
3f7d4d90 | 334 | BT_LIB_LOGI("Created empty component from component class: " |
d94d92ac | 335 | "%![cc-]+C, %![comp-]+c", component_class, component); |
65300d60 | 336 | BT_OBJECT_MOVE_REF(*user_component, component); |
ab0d387b | 337 | |
38b48196 | 338 | end: |
65300d60 | 339 | bt_object_put_ref(component); |
d94d92ac | 340 | return ret; |
6358c163 PP |
341 | } |
342 | ||
0d72b8c3 | 343 | const char *bt_component_get_name(const struct bt_component *component) |
de713ce0 | 344 | { |
bdb288b3 | 345 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 346 | return component->name->str; |
de713ce0 JG |
347 | } |
348 | ||
1de6a2b0 | 349 | const struct bt_component_class *bt_component_borrow_class_const( |
0d72b8c3 | 350 | const struct bt_component *component) |
de713ce0 | 351 | { |
bdb288b3 | 352 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 353 | return component->class; |
de713ce0 JG |
354 | } |
355 | ||
0d72b8c3 | 356 | void *bt_self_component_get_data(const struct bt_self_component *self_comp) |
de713ce0 | 357 | { |
d94d92ac | 358 | struct bt_component *component = (void *) self_comp; |
890882ef | 359 | |
bdb288b3 | 360 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 361 | return component->user_data; |
de713ce0 JG |
362 | } |
363 | ||
d94d92ac | 364 | void bt_self_component_set_data(struct bt_self_component *self_comp, |
de713ce0 JG |
365 | void *data) |
366 | { | |
d94d92ac | 367 | struct bt_component *component = (void *) self_comp; |
ab0d387b | 368 | |
bdb288b3 | 369 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
de713ce0 | 370 | component->user_data = data; |
3f7d4d90 | 371 | BT_LIB_LOGD("Set component's user data: %!+c", component); |
de713ce0 | 372 | } |
366e034f JG |
373 | |
374 | BT_HIDDEN | |
f60c8b34 | 375 | void bt_component_set_graph(struct bt_component *component, |
366e034f JG |
376 | struct bt_graph *graph) |
377 | { | |
3fea54f6 PP |
378 | bt_object_set_parent(&component->base, |
379 | graph ? &graph->base : NULL); | |
366e034f JG |
380 | } |
381 | ||
72b913fb | 382 | static |
d94d92ac | 383 | struct bt_port *borrow_port_by_name(GPtrArray *ports, |
9ac68eb1 | 384 | const char *name) |
366e034f | 385 | { |
d94d92ac | 386 | uint64_t i; |
366e034f JG |
387 | struct bt_port *ret_port = NULL; |
388 | ||
f6ccaed9 | 389 | BT_ASSERT(name); |
72b913fb | 390 | |
366e034f JG |
391 | for (i = 0; i < ports->len; i++) { |
392 | struct bt_port *port = g_ptr_array_index(ports, i); | |
366e034f | 393 | |
2242b43d | 394 | if (strcmp(name, port->name->str) == 0) { |
d94d92ac | 395 | ret_port = port; |
366e034f JG |
396 | break; |
397 | } | |
398 | } | |
399 | ||
400 | return ret_port; | |
401 | } | |
402 | ||
403 | BT_HIDDEN | |
d94d92ac PP |
404 | struct bt_port_input *bt_component_borrow_input_port_by_name( |
405 | struct bt_component *comp, const char *name) | |
72b913fb | 406 | { |
f6ccaed9 | 407 | BT_ASSERT(comp); |
d94d92ac | 408 | return (void *) borrow_port_by_name(comp->input_ports, name); |
72b913fb PP |
409 | } |
410 | ||
411 | BT_HIDDEN | |
d94d92ac PP |
412 | struct bt_port_output *bt_component_borrow_output_port_by_name( |
413 | struct bt_component *comp, const char *name) | |
72b913fb | 414 | { |
bdb288b3 | 415 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac PP |
416 | return (void *) |
417 | borrow_port_by_name(comp->output_ports, name); | |
72b913fb PP |
418 | } |
419 | ||
420 | static | |
d94d92ac | 421 | struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index) |
366e034f | 422 | { |
d94d92ac PP |
423 | BT_ASSERT(index < ports->len); |
424 | return g_ptr_array_index(ports, index); | |
366e034f JG |
425 | } |
426 | ||
427 | BT_HIDDEN | |
d94d92ac PP |
428 | struct bt_port_input *bt_component_borrow_input_port_by_index( |
429 | struct bt_component *comp, uint64_t index) | |
366e034f | 430 | { |
bdb288b3 PP |
431 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
432 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp->input_ports->len); | |
d94d92ac PP |
433 | return (void *) |
434 | borrow_port_by_index(comp->input_ports, index); | |
72b913fb | 435 | } |
366e034f | 436 | |
72b913fb | 437 | BT_HIDDEN |
d94d92ac PP |
438 | struct bt_port_output *bt_component_borrow_output_port_by_index( |
439 | struct bt_component *comp, uint64_t index) | |
72b913fb | 440 | { |
bdb288b3 PP |
441 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
442 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp->output_ports->len); | |
d94d92ac PP |
443 | return (void *) |
444 | borrow_port_by_index(comp->output_ports, index); | |
72b913fb | 445 | } |
366e034f | 446 | |
72b913fb | 447 | BT_HIDDEN |
d24d5663 | 448 | enum bt_self_component_add_port_status bt_component_add_input_port( |
3e9b0023 | 449 | struct bt_component *component, const char *name, |
8cc56726 | 450 | void *user_data, struct bt_port **port) |
72b913fb | 451 | { |
d94d92ac | 452 | /* add_port() logs details */ |
8cc56726 SM |
453 | return add_port(component, component->input_ports, |
454 | BT_PORT_TYPE_INPUT, name, user_data, port); | |
72b913fb | 455 | } |
366e034f | 456 | |
72b913fb | 457 | BT_HIDDEN |
d24d5663 | 458 | enum bt_self_component_add_port_status bt_component_add_output_port( |
3e9b0023 | 459 | struct bt_component *component, const char *name, |
8cc56726 | 460 | void *user_data, struct bt_port **port) |
72b913fb | 461 | { |
d94d92ac | 462 | /* add_port() logs details */ |
8cc56726 SM |
463 | return add_port(component, component->output_ports, |
464 | BT_PORT_TYPE_OUTPUT, name, user_data, port); | |
72b913fb PP |
465 | } |
466 | ||
157a98ed SM |
467 | BT_HIDDEN |
468 | bool bt_component_port_name_is_unique(GPtrArray *ports, const char *name) | |
469 | { | |
470 | guint i; | |
471 | bool unique; | |
472 | ||
473 | for (i = 0; i < ports->len; i++) { | |
474 | struct bt_port *port = g_ptr_array_index(ports, i); | |
475 | ||
476 | if (strcmp(port->name->str, name) == 0) { | |
477 | unique = false; | |
478 | goto end; | |
479 | } | |
480 | } | |
481 | ||
482 | unique = true; | |
483 | ||
484 | end: | |
485 | return unique; | |
486 | } | |
487 | ||
0d8b4d8e | 488 | BT_HIDDEN |
d24d5663 PP |
489 | enum bt_component_class_port_connected_method_status |
490 | bt_component_port_connected( | |
d94d92ac PP |
491 | struct bt_component *comp, struct bt_port *self_port, |
492 | struct bt_port *other_port) | |
0d8b4d8e | 493 | { |
d24d5663 | 494 | typedef enum bt_component_class_port_connected_method_status (*method_t)( |
0d72b8c3 | 495 | void *, void *, const void *); |
d94d92ac | 496 | |
9275bef4 | 497 | enum bt_component_class_port_connected_method_status status = |
d24d5663 | 498 | BT_FUNC_STATUS_OK; |
d94d92ac | 499 | method_t method = NULL; |
bf55043c | 500 | |
f6ccaed9 PP |
501 | BT_ASSERT(comp); |
502 | BT_ASSERT(self_port); | |
503 | BT_ASSERT(other_port); | |
0d8b4d8e | 504 | |
d94d92ac PP |
505 | switch (comp->class->type) { |
506 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
507 | { | |
508 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
509 | ||
510 | switch (self_port->type) { | |
511 | case BT_PORT_TYPE_OUTPUT: | |
512 | method = (method_t) src_cc->methods.output_port_connected; | |
513 | break; | |
514 | default: | |
498e7994 | 515 | bt_common_abort(); |
d94d92ac PP |
516 | } |
517 | ||
518 | break; | |
519 | } | |
520 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
521 | { | |
522 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
523 | ||
524 | switch (self_port->type) { | |
525 | case BT_PORT_TYPE_INPUT: | |
526 | method = (method_t) flt_cc->methods.input_port_connected; | |
527 | break; | |
528 | case BT_PORT_TYPE_OUTPUT: | |
529 | method = (method_t) flt_cc->methods.output_port_connected; | |
530 | break; | |
531 | default: | |
498e7994 | 532 | bt_common_abort(); |
d94d92ac PP |
533 | } |
534 | ||
535 | break; | |
536 | } | |
537 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
538 | { | |
539 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
540 | ||
541 | switch (self_port->type) { | |
542 | case BT_PORT_TYPE_INPUT: | |
543 | method = (method_t) sink_cc->methods.input_port_connected; | |
544 | break; | |
545 | default: | |
498e7994 | 546 | bt_common_abort(); |
d94d92ac PP |
547 | } |
548 | ||
549 | break; | |
550 | } | |
551 | default: | |
498e7994 | 552 | bt_common_abort(); |
d94d92ac PP |
553 | } |
554 | ||
555 | if (method) { | |
556 | BT_LIB_LOGD("Calling user's \"port connected\" method: " | |
557 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", | |
558 | comp, self_port, other_port); | |
d24d5663 | 559 | status = (int) method(comp, self_port, (void *) other_port); |
d94d92ac | 560 | BT_LOGD("User method returned: status=%s", |
d24d5663 PP |
561 | bt_common_func_status_string(status)); |
562 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
563 | status == BT_FUNC_STATUS_ERROR || | |
564 | status == BT_FUNC_STATUS_MEMORY_ERROR, | |
4725a201 | 565 | "Unexpected returned component status: status=%s", |
d24d5663 | 566 | bt_common_func_status_string(status)); |
6ecdcca3 | 567 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
0d8b4d8e | 568 | } |
bf55043c PP |
569 | |
570 | return status; | |
0d8b4d8e PP |
571 | } |
572 | ||
3230ee6b PP |
573 | BT_HIDDEN |
574 | void bt_component_add_destroy_listener(struct bt_component *component, | |
575 | bt_component_destroy_listener_func func, void *data) | |
576 | { | |
577 | struct bt_component_destroy_listener listener; | |
578 | ||
f6ccaed9 PP |
579 | BT_ASSERT(component); |
580 | BT_ASSERT(func); | |
3230ee6b PP |
581 | listener.func = func; |
582 | listener.data = data; | |
583 | g_array_append_val(component->destroy_listeners, listener); | |
3f7d4d90 | 584 | BT_LIB_LOGD("Added destroy listener: %![comp-]+c, " |
ab0d387b | 585 | "func-addr=%p, data-addr=%p", |
d94d92ac | 586 | component, func, data); |
3230ee6b PP |
587 | } |
588 | ||
589 | BT_HIDDEN | |
590 | void bt_component_remove_destroy_listener(struct bt_component *component, | |
591 | bt_component_destroy_listener_func func, void *data) | |
592 | { | |
d94d92ac | 593 | uint64_t i; |
3230ee6b | 594 | |
f6ccaed9 PP |
595 | BT_ASSERT(component); |
596 | BT_ASSERT(func); | |
3230ee6b PP |
597 | |
598 | for (i = 0; i < component->destroy_listeners->len; i++) { | |
599 | struct bt_component_destroy_listener *listener = | |
600 | &g_array_index(component->destroy_listeners, | |
601 | struct bt_component_destroy_listener, i); | |
602 | ||
603 | if (listener->func == func && listener->data == data) { | |
604 | g_array_remove_index(component->destroy_listeners, i); | |
605 | i--; | |
3f7d4d90 | 606 | BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, " |
ab0d387b | 607 | "func-addr=%p, data-addr=%p", |
d94d92ac | 608 | component, func, data); |
3230ee6b PP |
609 | } |
610 | } | |
611 | } | |
c5b9b441 | 612 | |
e874da19 PP |
613 | bt_logging_level bt_component_get_logging_level( |
614 | const struct bt_component *component) | |
615 | { | |
bdb288b3 | 616 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
e874da19 PP |
617 | return component->log_level; |
618 | } | |
619 | ||
056deb59 PP |
620 | uint64_t bt_self_component_get_graph_mip_version( |
621 | bt_self_component *self_component) | |
622 | { | |
623 | struct bt_component *comp = (void *) self_component; | |
624 | ||
625 | BT_ASSERT_PRE_NON_NULL(self_component, "Component"); | |
626 | return bt_component_borrow_graph(comp)->mip_version; | |
627 | } | |
628 | ||
c5b9b441 PP |
629 | void bt_component_get_ref(const struct bt_component *component) |
630 | { | |
631 | bt_object_get_ref(component); | |
632 | } | |
633 | ||
634 | void bt_component_put_ref(const struct bt_component *component) | |
635 | { | |
636 | bt_object_put_ref(component); | |
637 | } |