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