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