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