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