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