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