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