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