Commit | Line | Data |
---|---|---|
de713ce0 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de713ce0 JG |
3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
de713ce0 JG |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/COMPONENT" |
c2d9d9cf | 25 | #include "lib/logging.h" |
ab0d387b | 26 | |
e874da19 | 27 | #include "common/common.h" |
578e048b MJ |
28 | #include "common/assert.h" |
29 | #include "lib/assert-pre.h" | |
f6f301d7 | 30 | #include "lib/assert-post.h" |
3fadfbc0 | 31 | #include <babeltrace2/graph/self-component.h> |
43c59509 PP |
32 | #include <babeltrace2/graph/component.h> |
33 | #include <babeltrace2/graph/graph.h> | |
91d81473 | 34 | #include "common/macros.h" |
578e048b | 35 | #include "compat/compiler.h" |
3fadfbc0 MJ |
36 | #include <babeltrace2/types.h> |
37 | #include <babeltrace2/value.h> | |
578e048b | 38 | #include "lib/value.h" |
9ac68eb1 | 39 | #include <stdint.h> |
ab0d387b | 40 | #include <inttypes.h> |
de713ce0 | 41 | |
578e048b MJ |
42 | #include "component.h" |
43 | #include "component-class.h" | |
44 | #include "component-source.h" | |
45 | #include "component-filter.h" | |
46 | #include "component-sink.h" | |
47 | #include "connection.h" | |
48 | #include "graph.h" | |
49 | #include "message/iterator.h" | |
50 | #include "port.h" | |
d24d5663 | 51 | #include "lib/func-status.h" |
578e048b | 52 | |
7c7c0433 JG |
53 | static |
54 | struct bt_component * (* const component_create_funcs[])( | |
0d72b8c3 | 55 | const struct bt_component_class *) = { |
d3e4dcd8 PP |
56 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create, |
57 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create, | |
58 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create, | |
7c7c0433 JG |
59 | }; |
60 | ||
72b913fb PP |
61 | static |
62 | void (*component_destroy_funcs[])(struct bt_component *) = { | |
63 | [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy, | |
64 | [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy, | |
65 | [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy, | |
66 | }; | |
67 | ||
b8a06801 | 68 | static |
d94d92ac PP |
69 | void finalize_component(struct bt_component *comp) |
70 | { | |
71 | typedef void (*method_t)(void *); | |
72 | ||
73 | method_t method = NULL; | |
74 | ||
75 | BT_ASSERT(comp); | |
76 | ||
77 | switch (comp->class->type) { | |
78 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
79 | { | |
80 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
81 | ||
82 | method = (method_t) src_cc->methods.finalize; | |
83 | break; | |
84 | } | |
85 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
86 | { | |
87 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
88 | ||
89 | method = (method_t) flt_cc->methods.finalize; | |
90 | break; | |
91 | } | |
92 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
93 | { | |
94 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
95 | ||
96 | method = (method_t) sink_cc->methods.finalize; | |
97 | break; | |
98 | } | |
99 | default: | |
498e7994 | 100 | bt_common_abort(); |
d94d92ac PP |
101 | } |
102 | ||
103 | if (method) { | |
42a63165 SM |
104 | const struct bt_error *saved_error; |
105 | ||
106 | saved_error = bt_current_thread_take_error(); | |
107 | ||
3f7d4d90 | 108 | BT_LIB_LOGI("Calling user's component finalization method: " |
d94d92ac PP |
109 | "%![comp-]+c", comp); |
110 | method(comp); | |
42a63165 SM |
111 | BT_ASSERT_POST_NO_ERROR(); |
112 | ||
113 | if (saved_error) { | |
114 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error); | |
115 | } | |
d94d92ac PP |
116 | } |
117 | } | |
118 | ||
119 | static | |
120 | void destroy_component(struct bt_object *obj) | |
b8a06801 JG |
121 | { |
122 | struct bt_component *component = NULL; | |
3230ee6b | 123 | int i; |
b8a06801 JG |
124 | |
125 | if (!obj) { | |
126 | return; | |
127 | } | |
128 | ||
bd14d768 PP |
129 | /* |
130 | * The component's reference count is 0 if we're here. Increment | |
131 | * it to avoid a double-destroy (possibly infinitely recursive). | |
132 | * This could happen for example if the component's finalization | |
d94d92ac PP |
133 | * function does bt_object_get_ref() (or anything that causes |
134 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
135 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
136 | * count would go from 1 to 0 again and this function would be | |
137 | * called again. | |
bd14d768 | 138 | */ |
3fea54f6 | 139 | obj->ref_count++; |
b8a06801 | 140 | component = container_of(obj, struct bt_component, base); |
3f7d4d90 | 141 | BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g", |
d94d92ac | 142 | component, bt_component_borrow_graph(component)); |
3230ee6b PP |
143 | |
144 | /* Call destroy listeners in reverse registration order */ | |
ab0d387b PP |
145 | BT_LOGD_STR("Calling destroy listeners."); |
146 | ||
3230ee6b PP |
147 | for (i = component->destroy_listeners->len - 1; i >= 0; i--) { |
148 | struct bt_component_destroy_listener *listener = | |
149 | &g_array_index(component->destroy_listeners, | |
150 | struct bt_component_destroy_listener, i); | |
151 | ||
152 | listener->func(component, listener->data); | |
153 | } | |
154 | ||
7c7c0433 | 155 | /* |
36712f1d PP |
156 | * User data is destroyed first, followed by the concrete |
157 | * component instance. Do not finalize if the component's user | |
158 | * initialization method failed in the first place. | |
b8a06801 | 159 | */ |
d94d92ac PP |
160 | if (component->initialized) { |
161 | finalize_component(component); | |
7c7c0433 | 162 | } |
b8a06801 | 163 | |
ab09f844 | 164 | if (component->destroy) { |
ab0d387b | 165 | BT_LOGD_STR("Destroying type-specific data."); |
ab09f844 JG |
166 | component->destroy(component); |
167 | } | |
168 | ||
72b913fb | 169 | if (component->input_ports) { |
ab0d387b | 170 | BT_LOGD_STR("Destroying input ports."); |
72b913fb | 171 | g_ptr_array_free(component->input_ports, TRUE); |
d94d92ac | 172 | component->input_ports = NULL; |
72b913fb | 173 | } |
b8a06801 | 174 | |
72b913fb | 175 | if (component->output_ports) { |
ab0d387b | 176 | BT_LOGD_STR("Destroying output ports."); |
72b913fb | 177 | g_ptr_array_free(component->output_ports, TRUE); |
d94d92ac | 178 | component->output_ports = NULL; |
b8a06801 JG |
179 | } |
180 | ||
3230ee6b PP |
181 | if (component->destroy_listeners) { |
182 | g_array_free(component->destroy_listeners, TRUE); | |
d94d92ac | 183 | component->destroy_listeners = NULL; |
3230ee6b PP |
184 | } |
185 | ||
ab0d387b PP |
186 | if (component->name) { |
187 | g_string_free(component->name, TRUE); | |
d94d92ac | 188 | component->name = NULL; |
ab0d387b PP |
189 | } |
190 | ||
d94d92ac PP |
191 | BT_LOGD_STR("Putting component class."); |
192 | BT_OBJECT_PUT_REF_AND_RESET(component->class); | |
72b913fb | 193 | g_free(component); |
b8a06801 | 194 | } |
de713ce0 | 195 | |
890882ef | 196 | enum bt_component_class_type bt_component_get_class_type( |
0d72b8c3 | 197 | const struct bt_component *component) |
5645cd95 | 198 | { |
bdb288b3 | 199 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 200 | return component->class->type; |
5645cd95 JG |
201 | } |
202 | ||
72b913fb | 203 | static |
d24d5663 | 204 | enum bt_self_component_add_port_status add_port( |
72b913fb | 205 | struct bt_component *component, GPtrArray *ports, |
8cc56726 SM |
206 | enum bt_port_type port_type, const char *name, void *user_data, |
207 | struct bt_port **port) | |
72b913fb | 208 | { |
72b913fb | 209 | struct bt_port *new_port = NULL; |
1bf957a0 | 210 | struct bt_graph *graph = NULL; |
d24d5663 | 211 | enum bt_self_component_add_port_status status; |
72b913fb | 212 | |
d94d92ac PP |
213 | BT_ASSERT_PRE_NON_NULL(component, "Component"); |
214 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
215 | BT_ASSERT_PRE(strlen(name) > 0, "Name is empty"); | |
216 | graph = bt_component_borrow_graph(component); | |
5badd463 PP |
217 | BT_ASSERT_PRE( |
218 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
219 | "Component's graph is already configured: " |
220 | "%![comp-]+c, %![graph-]+g", component, graph); | |
72b913fb | 221 | |
d94d92ac | 222 | // TODO: Validate that the name is not already used. |
ab0d387b | 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 | ||
276 | BT_HIDDEN | |
0d72b8c3 | 277 | uint64_t bt_component_get_input_port_count(const struct bt_component *comp) |
72b913fb | 278 | { |
bdb288b3 | 279 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac | 280 | return (uint64_t) comp->input_ports->len; |
72b913fb PP |
281 | } |
282 | ||
283 | BT_HIDDEN | |
0d72b8c3 | 284 | uint64_t bt_component_get_output_port_count(const struct bt_component *comp) |
72b913fb | 285 | { |
bdb288b3 | 286 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac | 287 | return (uint64_t) comp->output_ports->len; |
72b913fb PP |
288 | } |
289 | ||
dd8a4547 | 290 | BT_HIDDEN |
d94d92ac | 291 | int bt_component_create(struct bt_component_class *component_class, |
e874da19 PP |
292 | const char *name, bt_logging_level log_level, |
293 | struct bt_component **user_component) | |
38b48196 | 294 | { |
d94d92ac | 295 | int ret = 0; |
38b48196 | 296 | struct bt_component *component = NULL; |
d3e4dcd8 | 297 | enum bt_component_class_type type; |
38b48196 | 298 | |
f6ccaed9 PP |
299 | BT_ASSERT(user_component); |
300 | BT_ASSERT(component_class); | |
301 | BT_ASSERT(name); | |
7c7c0433 | 302 | type = bt_component_class_get_type(component_class); |
3f7d4d90 | 303 | BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, " |
e874da19 PP |
304 | "comp-name=\"%s\", log-level=%s", component_class, name, |
305 | bt_common_logging_level_string(log_level)); | |
36712f1d | 306 | component = component_create_funcs[type](component_class); |
7c7c0433 | 307 | if (!component) { |
870631a2 PP |
308 | BT_LIB_LOGE_APPEND_CAUSE( |
309 | "Cannot create specific component object."); | |
d94d92ac | 310 | ret = -1; |
7c7c0433 JG |
311 | goto end; |
312 | } | |
313 | ||
d0fea130 | 314 | bt_object_init_shared_with_parent(&component->base, destroy_component); |
398454ed | 315 | component->class = component_class; |
6871026b | 316 | bt_object_get_ref_no_null_check(component->class); |
72b913fb | 317 | component->destroy = component_destroy_funcs[type]; |
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 | ||
0d72b8c3 | 359 | const char *bt_component_get_name(const struct bt_component *component) |
de713ce0 | 360 | { |
bdb288b3 | 361 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 362 | return component->name->str; |
de713ce0 JG |
363 | } |
364 | ||
1de6a2b0 | 365 | const struct bt_component_class *bt_component_borrow_class_const( |
0d72b8c3 | 366 | const struct bt_component *component) |
de713ce0 | 367 | { |
bdb288b3 | 368 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 369 | return component->class; |
de713ce0 JG |
370 | } |
371 | ||
0d72b8c3 | 372 | void *bt_self_component_get_data(const struct bt_self_component *self_comp) |
de713ce0 | 373 | { |
d94d92ac | 374 | struct bt_component *component = (void *) self_comp; |
890882ef | 375 | |
bdb288b3 | 376 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 377 | return component->user_data; |
de713ce0 JG |
378 | } |
379 | ||
d94d92ac | 380 | void bt_self_component_set_data(struct bt_self_component *self_comp, |
de713ce0 JG |
381 | void *data) |
382 | { | |
d94d92ac | 383 | struct bt_component *component = (void *) self_comp; |
ab0d387b | 384 | |
bdb288b3 | 385 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
de713ce0 | 386 | component->user_data = data; |
3f7d4d90 | 387 | BT_LIB_LOGD("Set component's user data: %!+c", component); |
de713ce0 | 388 | } |
366e034f JG |
389 | |
390 | BT_HIDDEN | |
f60c8b34 | 391 | void bt_component_set_graph(struct bt_component *component, |
366e034f JG |
392 | struct bt_graph *graph) |
393 | { | |
3fea54f6 PP |
394 | bt_object_set_parent(&component->base, |
395 | graph ? &graph->base : NULL); | |
366e034f JG |
396 | } |
397 | ||
72b913fb | 398 | static |
d94d92ac | 399 | struct bt_port *borrow_port_by_name(GPtrArray *ports, |
9ac68eb1 | 400 | const char *name) |
366e034f | 401 | { |
d94d92ac | 402 | uint64_t i; |
366e034f JG |
403 | struct bt_port *ret_port = NULL; |
404 | ||
f6ccaed9 | 405 | BT_ASSERT(name); |
72b913fb | 406 | |
366e034f JG |
407 | for (i = 0; i < ports->len; i++) { |
408 | struct bt_port *port = g_ptr_array_index(ports, i); | |
366e034f | 409 | |
2242b43d | 410 | if (strcmp(name, port->name->str) == 0) { |
d94d92ac | 411 | ret_port = port; |
366e034f JG |
412 | break; |
413 | } | |
414 | } | |
415 | ||
416 | return ret_port; | |
417 | } | |
418 | ||
419 | BT_HIDDEN | |
d94d92ac PP |
420 | struct bt_port_input *bt_component_borrow_input_port_by_name( |
421 | struct bt_component *comp, const char *name) | |
72b913fb | 422 | { |
f6ccaed9 | 423 | BT_ASSERT(comp); |
d94d92ac | 424 | return (void *) borrow_port_by_name(comp->input_ports, name); |
72b913fb PP |
425 | } |
426 | ||
427 | BT_HIDDEN | |
d94d92ac PP |
428 | struct bt_port_output *bt_component_borrow_output_port_by_name( |
429 | struct bt_component *comp, const char *name) | |
72b913fb | 430 | { |
bdb288b3 | 431 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac PP |
432 | return (void *) |
433 | borrow_port_by_name(comp->output_ports, name); | |
72b913fb PP |
434 | } |
435 | ||
436 | static | |
d94d92ac | 437 | struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index) |
366e034f | 438 | { |
d94d92ac PP |
439 | BT_ASSERT(index < ports->len); |
440 | return g_ptr_array_index(ports, index); | |
366e034f JG |
441 | } |
442 | ||
443 | BT_HIDDEN | |
d94d92ac PP |
444 | struct bt_port_input *bt_component_borrow_input_port_by_index( |
445 | struct bt_component *comp, uint64_t index) | |
366e034f | 446 | { |
bdb288b3 PP |
447 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
448 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp->input_ports->len); | |
d94d92ac PP |
449 | return (void *) |
450 | borrow_port_by_index(comp->input_ports, index); | |
72b913fb | 451 | } |
366e034f | 452 | |
72b913fb | 453 | BT_HIDDEN |
d94d92ac PP |
454 | struct bt_port_output *bt_component_borrow_output_port_by_index( |
455 | struct bt_component *comp, uint64_t index) | |
72b913fb | 456 | { |
bdb288b3 PP |
457 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
458 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp->output_ports->len); | |
d94d92ac PP |
459 | return (void *) |
460 | borrow_port_by_index(comp->output_ports, index); | |
72b913fb | 461 | } |
366e034f | 462 | |
72b913fb | 463 | BT_HIDDEN |
d24d5663 | 464 | enum bt_self_component_add_port_status bt_component_add_input_port( |
3e9b0023 | 465 | struct bt_component *component, const char *name, |
8cc56726 | 466 | void *user_data, struct bt_port **port) |
72b913fb | 467 | { |
d94d92ac | 468 | /* add_port() logs details */ |
8cc56726 SM |
469 | return add_port(component, component->input_ports, |
470 | BT_PORT_TYPE_INPUT, name, user_data, port); | |
72b913fb | 471 | } |
366e034f | 472 | |
72b913fb | 473 | BT_HIDDEN |
d24d5663 | 474 | enum bt_self_component_add_port_status bt_component_add_output_port( |
3e9b0023 | 475 | struct bt_component *component, const char *name, |
8cc56726 | 476 | void *user_data, struct bt_port **port) |
72b913fb | 477 | { |
d94d92ac | 478 | /* add_port() logs details */ |
8cc56726 SM |
479 | return add_port(component, component->output_ports, |
480 | BT_PORT_TYPE_OUTPUT, name, user_data, port); | |
72b913fb PP |
481 | } |
482 | ||
0d8b4d8e | 483 | BT_HIDDEN |
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; |
bf55043c | 495 | |
f6ccaed9 PP |
496 | BT_ASSERT(comp); |
497 | BT_ASSERT(self_port); | |
498 | BT_ASSERT(other_port); | |
0d8b4d8e | 499 | |
d94d92ac PP |
500 | switch (comp->class->type) { |
501 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
502 | { | |
503 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
504 | ||
505 | switch (self_port->type) { | |
506 | case BT_PORT_TYPE_OUTPUT: | |
507 | method = (method_t) src_cc->methods.output_port_connected; | |
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; | |
522 | break; | |
523 | case BT_PORT_TYPE_OUTPUT: | |
524 | method = (method_t) flt_cc->methods.output_port_connected; | |
525 | break; | |
526 | default: | |
498e7994 | 527 | bt_common_abort(); |
d94d92ac PP |
528 | } |
529 | ||
530 | break; | |
531 | } | |
532 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
533 | { | |
534 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
535 | ||
536 | switch (self_port->type) { | |
537 | case BT_PORT_TYPE_INPUT: | |
538 | method = (method_t) sink_cc->methods.input_port_connected; | |
539 | break; | |
540 | default: | |
498e7994 | 541 | bt_common_abort(); |
d94d92ac PP |
542 | } |
543 | ||
544 | break; | |
545 | } | |
546 | default: | |
498e7994 | 547 | bt_common_abort(); |
d94d92ac PP |
548 | } |
549 | ||
550 | if (method) { | |
551 | BT_LIB_LOGD("Calling user's \"port connected\" method: " | |
552 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", | |
553 | comp, self_port, other_port); | |
d24d5663 | 554 | status = (int) method(comp, self_port, (void *) other_port); |
d94d92ac | 555 | BT_LOGD("User method returned: status=%s", |
d24d5663 PP |
556 | bt_common_func_status_string(status)); |
557 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
558 | status == BT_FUNC_STATUS_ERROR || | |
559 | status == BT_FUNC_STATUS_MEMORY_ERROR, | |
4725a201 | 560 | "Unexpected returned component status: status=%s", |
d24d5663 | 561 | bt_common_func_status_string(status)); |
6ecdcca3 | 562 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
0d8b4d8e | 563 | } |
bf55043c PP |
564 | |
565 | return status; | |
0d8b4d8e PP |
566 | } |
567 | ||
3230ee6b PP |
568 | BT_HIDDEN |
569 | void bt_component_add_destroy_listener(struct bt_component *component, | |
570 | bt_component_destroy_listener_func func, void *data) | |
571 | { | |
572 | struct bt_component_destroy_listener listener; | |
573 | ||
f6ccaed9 PP |
574 | BT_ASSERT(component); |
575 | BT_ASSERT(func); | |
3230ee6b PP |
576 | listener.func = func; |
577 | listener.data = data; | |
578 | g_array_append_val(component->destroy_listeners, listener); | |
3f7d4d90 | 579 | BT_LIB_LOGD("Added destroy listener: %![comp-]+c, " |
ab0d387b | 580 | "func-addr=%p, data-addr=%p", |
d94d92ac | 581 | component, func, data); |
3230ee6b PP |
582 | } |
583 | ||
584 | BT_HIDDEN | |
585 | void bt_component_remove_destroy_listener(struct bt_component *component, | |
586 | bt_component_destroy_listener_func func, void *data) | |
587 | { | |
d94d92ac | 588 | uint64_t i; |
3230ee6b | 589 | |
f6ccaed9 PP |
590 | BT_ASSERT(component); |
591 | BT_ASSERT(func); | |
3230ee6b PP |
592 | |
593 | for (i = 0; i < component->destroy_listeners->len; i++) { | |
594 | struct bt_component_destroy_listener *listener = | |
595 | &g_array_index(component->destroy_listeners, | |
596 | struct bt_component_destroy_listener, i); | |
597 | ||
598 | if (listener->func == func && listener->data == data) { | |
599 | g_array_remove_index(component->destroy_listeners, i); | |
600 | i--; | |
3f7d4d90 | 601 | BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, " |
ab0d387b | 602 | "func-addr=%p, data-addr=%p", |
d94d92ac | 603 | component, func, data); |
3230ee6b PP |
604 | } |
605 | } | |
606 | } | |
c5b9b441 | 607 | |
e874da19 PP |
608 | bt_logging_level bt_component_get_logging_level( |
609 | const struct bt_component *component) | |
610 | { | |
bdb288b3 | 611 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
e874da19 PP |
612 | return component->log_level; |
613 | } | |
614 | ||
056deb59 PP |
615 | uint64_t bt_self_component_get_graph_mip_version( |
616 | bt_self_component *self_component) | |
617 | { | |
618 | struct bt_component *comp = (void *) self_component; | |
619 | ||
620 | BT_ASSERT_PRE_NON_NULL(self_component, "Component"); | |
621 | return bt_component_borrow_graph(comp)->mip_version; | |
622 | } | |
623 | ||
c5b9b441 PP |
624 | void bt_component_get_ref(const struct bt_component *component) |
625 | { | |
626 | bt_object_get_ref(component); | |
627 | } | |
628 | ||
629 | void bt_component_put_ref(const struct bt_component *component) | |
630 | { | |
631 | bt_object_put_ref(component); | |
632 | } |