Commit | Line | Data |
---|---|---|
de713ce0 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de713ce0 JG |
3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
de713ce0 JG |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/COMPONENT" |
c2d9d9cf | 25 | #include "lib/logging.h" |
ab0d387b | 26 | |
e874da19 | 27 | #include "common/common.h" |
578e048b MJ |
28 | #include "common/assert.h" |
29 | #include "lib/assert-pre.h" | |
f6f301d7 | 30 | #include "lib/assert-post.h" |
3fadfbc0 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> | |
d24d5663 | 36 | #include <babeltrace2/graph/graph-const.h> |
91d81473 | 37 | #include "common/macros.h" |
578e048b | 38 | #include "compat/compiler.h" |
3fadfbc0 MJ |
39 | #include <babeltrace2/types.h> |
40 | #include <babeltrace2/value.h> | |
578e048b | 41 | #include "lib/value.h" |
9ac68eb1 | 42 | #include <stdint.h> |
ab0d387b | 43 | #include <inttypes.h> |
de713ce0 | 44 | |
578e048b 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" | |
d24d5663 | 54 | #include "lib/func-status.h" |
578e048b | 55 | |
7c7c0433 JG |
56 | static |
57 | struct bt_component * (* const component_create_funcs[])( | |
0d72b8c3 | 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 |
d94d92ac 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) { | |
3f7d4d90 | 107 | BT_LIB_LOGI("Calling user's component finalization method: " |
d94d92ac 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 | |
d94d92ac 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 | */ |
3fea54f6 | 133 | obj->ref_count++; |
b8a06801 | 134 | component = container_of(obj, struct bt_component, base); |
3f7d4d90 | 135 | BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g", |
d94d92ac | 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 | */ |
d94d92ac 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); |
d94d92ac | 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); |
d94d92ac | 172 | component->output_ports = NULL; |
b8a06801 JG |
173 | } |
174 | ||
3230ee6b PP |
175 | if (component->destroy_listeners) { |
176 | g_array_free(component->destroy_listeners, TRUE); | |
d94d92ac | 177 | component->destroy_listeners = NULL; |
3230ee6b PP |
178 | } |
179 | ||
ab0d387b PP |
180 | if (component->name) { |
181 | g_string_free(component->name, TRUE); | |
d94d92ac | 182 | component->name = NULL; |
ab0d387b PP |
183 | } |
184 | ||
d94d92ac 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( |
0d72b8c3 | 191 | const struct bt_component *component) |
5645cd95 | 192 | { |
bdb288b3 | 193 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 194 | return component->class->type; |
5645cd95 JG |
195 | } |
196 | ||
72b913fb | 197 | static |
d24d5663 | 198 | enum bt_self_component_add_port_status add_port( |
72b913fb | 199 | struct bt_component *component, GPtrArray *ports, |
8cc56726 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; |
d24d5663 | 205 | enum bt_self_component_add_port_status status; |
72b913fb | 206 | |
d94d92ac 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); | |
5badd463 PP |
211 | BT_ASSERT_PRE( |
212 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
213 | "Component's graph is already configured: " |
214 | "%![comp-]+c, %![graph-]+g", component, graph); | |
72b913fb | 215 | |
d94d92ac | 216 | // TODO: Validate that the name is not already used. |
ab0d387b | 217 | |
3f7d4d90 | 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) { |
870631a2 | 224 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object."); |
d24d5663 | 225 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
8cc56726 | 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 | */ | |
398454ed | 240 | graph = bt_component_borrow_graph(component); |
1bf957a0 | 241 | if (graph) { |
d24d5663 | 242 | enum bt_graph_listener_func_status listener_status; |
8cc56726 SM |
243 | |
244 | listener_status = bt_graph_notify_port_added(graph, new_port); | |
d24d5663 | 245 | if (listener_status != BT_FUNC_STATUS_OK) { |
8cc56726 | 246 | bt_graph_make_faulty(graph); |
d24d5663 | 247 | status = (int) listener_status; |
8cc56726 SM |
248 | goto error; |
249 | } | |
1bf957a0 PP |
250 | } |
251 | ||
3f7d4d90 | 252 | BT_LIB_LOGI("Created and added port to component: " |
d94d92ac | 253 | "%![comp-]+c, %![port-]+p", component, new_port); |
ab0d387b | 254 | |
8cc56726 | 255 | *port = new_port; |
d24d5663 | 256 | status = BT_FUNC_STATUS_OK; |
8cc56726 SM |
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: |
8cc56726 | 267 | return status; |
72b913fb PP |
268 | } |
269 | ||
270 | BT_HIDDEN | |
0d72b8c3 | 271 | uint64_t bt_component_get_input_port_count(const struct bt_component *comp) |
72b913fb | 272 | { |
bdb288b3 | 273 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac | 274 | return (uint64_t) comp->input_ports->len; |
72b913fb PP |
275 | } |
276 | ||
277 | BT_HIDDEN | |
0d72b8c3 | 278 | uint64_t bt_component_get_output_port_count(const struct bt_component *comp) |
72b913fb | 279 | { |
bdb288b3 | 280 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac | 281 | return (uint64_t) comp->output_ports->len; |
72b913fb PP |
282 | } |
283 | ||
dd8a4547 | 284 | BT_HIDDEN |
d94d92ac | 285 | int bt_component_create(struct bt_component_class *component_class, |
e874da19 PP |
286 | const char *name, bt_logging_level log_level, |
287 | struct bt_component **user_component) | |
38b48196 | 288 | { |
d94d92ac | 289 | int ret = 0; |
38b48196 | 290 | struct bt_component *component = NULL; |
d3e4dcd8 | 291 | enum bt_component_class_type type; |
38b48196 | 292 | |
f6ccaed9 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); |
3f7d4d90 | 297 | BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, " |
e874da19 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) { |
870631a2 PP |
302 | BT_LIB_LOGE_APPEND_CAUSE( |
303 | "Cannot create specific component object."); | |
d94d92ac | 304 | ret = -1; |
7c7c0433 JG |
305 | goto end; |
306 | } | |
307 | ||
d0fea130 | 308 | bt_object_init_shared_with_parent(&component->base, destroy_component); |
398454ed 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) { |
870631a2 | 314 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); |
d94d92ac | 315 | ret = -1; |
7c7c0433 JG |
316 | goto end; |
317 | } | |
318 | ||
e874da19 | 319 | component->log_level = log_level; |
72b913fb | 320 | component->input_ports = g_ptr_array_new_with_free_func( |
3fea54f6 | 321 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 322 | if (!component->input_ports) { |
870631a2 | 323 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
d94d92ac | 324 | ret = -1; |
72b913fb PP |
325 | goto end; |
326 | } | |
327 | ||
328 | component->output_ports = g_ptr_array_new_with_free_func( | |
3fea54f6 | 329 | (GDestroyNotify) bt_object_try_spec_release); |
72b913fb | 330 | if (!component->output_ports) { |
870631a2 | 331 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
d94d92ac | 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) { | |
870631a2 | 339 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
d94d92ac | 340 | ret = -1; |
3230ee6b PP |
341 | goto end; |
342 | } | |
343 | ||
3f7d4d90 | 344 | BT_LIB_LOGI("Created empty component from component class: " |
d94d92ac | 345 | "%![cc-]+C, %![comp-]+c", component_class, component); |
65300d60 | 346 | BT_OBJECT_MOVE_REF(*user_component, component); |
ab0d387b | 347 | |
38b48196 | 348 | end: |
65300d60 | 349 | bt_object_put_ref(component); |
d94d92ac | 350 | return ret; |
6358c163 PP |
351 | } |
352 | ||
0d72b8c3 | 353 | const char *bt_component_get_name(const struct bt_component *component) |
de713ce0 | 354 | { |
bdb288b3 | 355 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 356 | return component->name->str; |
de713ce0 JG |
357 | } |
358 | ||
1de6a2b0 | 359 | const struct bt_component_class *bt_component_borrow_class_const( |
0d72b8c3 | 360 | const struct bt_component *component) |
de713ce0 | 361 | { |
bdb288b3 | 362 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 363 | return component->class; |
de713ce0 JG |
364 | } |
365 | ||
0d72b8c3 | 366 | void *bt_self_component_get_data(const struct bt_self_component *self_comp) |
de713ce0 | 367 | { |
d94d92ac | 368 | struct bt_component *component = (void *) self_comp; |
890882ef | 369 | |
bdb288b3 | 370 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
d94d92ac | 371 | return component->user_data; |
de713ce0 JG |
372 | } |
373 | ||
d94d92ac | 374 | void bt_self_component_set_data(struct bt_self_component *self_comp, |
de713ce0 JG |
375 | void *data) |
376 | { | |
d94d92ac | 377 | struct bt_component *component = (void *) self_comp; |
ab0d387b | 378 | |
bdb288b3 | 379 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
de713ce0 | 380 | component->user_data = data; |
3f7d4d90 | 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 | { | |
3fea54f6 PP |
388 | bt_object_set_parent(&component->base, |
389 | graph ? &graph->base : NULL); | |
366e034f JG |
390 | } |
391 | ||
72b913fb | 392 | static |
d94d92ac | 393 | struct bt_port *borrow_port_by_name(GPtrArray *ports, |
9ac68eb1 | 394 | const char *name) |
366e034f | 395 | { |
d94d92ac | 396 | uint64_t i; |
366e034f JG |
397 | struct bt_port *ret_port = NULL; |
398 | ||
f6ccaed9 | 399 | BT_ASSERT(name); |
72b913fb | 400 | |
366e034f JG |
401 | for (i = 0; i < ports->len; i++) { |
402 | struct bt_port *port = g_ptr_array_index(ports, i); | |
366e034f | 403 | |
2242b43d | 404 | if (strcmp(name, port->name->str) == 0) { |
d94d92ac | 405 | ret_port = port; |
366e034f JG |
406 | break; |
407 | } | |
408 | } | |
409 | ||
410 | return ret_port; | |
411 | } | |
412 | ||
413 | BT_HIDDEN | |
d94d92ac PP |
414 | struct bt_port_input *bt_component_borrow_input_port_by_name( |
415 | struct bt_component *comp, const char *name) | |
72b913fb | 416 | { |
f6ccaed9 | 417 | BT_ASSERT(comp); |
d94d92ac | 418 | return (void *) borrow_port_by_name(comp->input_ports, name); |
72b913fb PP |
419 | } |
420 | ||
421 | BT_HIDDEN | |
d94d92ac PP |
422 | struct bt_port_output *bt_component_borrow_output_port_by_name( |
423 | struct bt_component *comp, const char *name) | |
72b913fb | 424 | { |
bdb288b3 | 425 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
d94d92ac PP |
426 | return (void *) |
427 | borrow_port_by_name(comp->output_ports, name); | |
72b913fb PP |
428 | } |
429 | ||
430 | static | |
d94d92ac | 431 | struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index) |
366e034f | 432 | { |
d94d92ac PP |
433 | BT_ASSERT(index < ports->len); |
434 | return g_ptr_array_index(ports, index); | |
366e034f JG |
435 | } |
436 | ||
437 | BT_HIDDEN | |
d94d92ac PP |
438 | struct bt_port_input *bt_component_borrow_input_port_by_index( |
439 | struct bt_component *comp, uint64_t index) | |
366e034f | 440 | { |
bdb288b3 PP |
441 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
442 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp->input_ports->len); | |
d94d92ac PP |
443 | return (void *) |
444 | borrow_port_by_index(comp->input_ports, index); | |
72b913fb | 445 | } |
366e034f | 446 | |
72b913fb | 447 | BT_HIDDEN |
d94d92ac PP |
448 | struct bt_port_output *bt_component_borrow_output_port_by_index( |
449 | struct bt_component *comp, uint64_t index) | |
72b913fb | 450 | { |
bdb288b3 PP |
451 | BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component"); |
452 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, comp->output_ports->len); | |
d94d92ac PP |
453 | return (void *) |
454 | borrow_port_by_index(comp->output_ports, index); | |
72b913fb | 455 | } |
366e034f | 456 | |
72b913fb | 457 | BT_HIDDEN |
d24d5663 | 458 | enum bt_self_component_add_port_status bt_component_add_input_port( |
3e9b0023 | 459 | struct bt_component *component, const char *name, |
8cc56726 | 460 | void *user_data, struct bt_port **port) |
72b913fb | 461 | { |
d94d92ac | 462 | /* add_port() logs details */ |
8cc56726 SM |
463 | return add_port(component, component->input_ports, |
464 | BT_PORT_TYPE_INPUT, name, user_data, port); | |
72b913fb | 465 | } |
366e034f | 466 | |
72b913fb | 467 | BT_HIDDEN |
d24d5663 | 468 | enum bt_self_component_add_port_status bt_component_add_output_port( |
3e9b0023 | 469 | struct bt_component *component, const char *name, |
8cc56726 | 470 | void *user_data, struct bt_port **port) |
72b913fb | 471 | { |
d94d92ac | 472 | /* add_port() logs details */ |
8cc56726 SM |
473 | return add_port(component, component->output_ports, |
474 | BT_PORT_TYPE_OUTPUT, name, user_data, port); | |
72b913fb PP |
475 | } |
476 | ||
0d8b4d8e | 477 | BT_HIDDEN |
d24d5663 PP |
478 | enum bt_component_class_port_connected_method_status |
479 | bt_component_port_connected( | |
d94d92ac PP |
480 | struct bt_component *comp, struct bt_port *self_port, |
481 | struct bt_port *other_port) | |
0d8b4d8e | 482 | { |
d24d5663 | 483 | typedef enum bt_component_class_port_connected_method_status (*method_t)( |
0d72b8c3 | 484 | void *, void *, const void *); |
d94d92ac | 485 | |
9275bef4 | 486 | enum bt_component_class_port_connected_method_status status = |
d24d5663 | 487 | BT_FUNC_STATUS_OK; |
d94d92ac | 488 | method_t method = NULL; |
bf55043c | 489 | |
f6ccaed9 PP |
490 | BT_ASSERT(comp); |
491 | BT_ASSERT(self_port); | |
492 | BT_ASSERT(other_port); | |
0d8b4d8e | 493 | |
d94d92ac PP |
494 | switch (comp->class->type) { |
495 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
496 | { | |
497 | struct bt_component_class_source *src_cc = (void *) comp->class; | |
498 | ||
499 | switch (self_port->type) { | |
500 | case BT_PORT_TYPE_OUTPUT: | |
501 | method = (method_t) src_cc->methods.output_port_connected; | |
502 | break; | |
503 | default: | |
504 | abort(); | |
505 | } | |
506 | ||
507 | break; | |
508 | } | |
509 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
510 | { | |
511 | struct bt_component_class_filter *flt_cc = (void *) comp->class; | |
512 | ||
513 | switch (self_port->type) { | |
514 | case BT_PORT_TYPE_INPUT: | |
515 | method = (method_t) flt_cc->methods.input_port_connected; | |
516 | break; | |
517 | case BT_PORT_TYPE_OUTPUT: | |
518 | method = (method_t) flt_cc->methods.output_port_connected; | |
519 | break; | |
520 | default: | |
521 | abort(); | |
522 | } | |
523 | ||
524 | break; | |
525 | } | |
526 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
527 | { | |
528 | struct bt_component_class_sink *sink_cc = (void *) comp->class; | |
529 | ||
530 | switch (self_port->type) { | |
531 | case BT_PORT_TYPE_INPUT: | |
532 | method = (method_t) sink_cc->methods.input_port_connected; | |
533 | break; | |
534 | default: | |
535 | abort(); | |
536 | } | |
537 | ||
538 | break; | |
539 | } | |
540 | default: | |
541 | abort(); | |
542 | } | |
543 | ||
544 | if (method) { | |
545 | BT_LIB_LOGD("Calling user's \"port connected\" method: " | |
546 | "%![comp-]+c, %![self-port-]+p, %![other-port-]+p", | |
547 | comp, self_port, other_port); | |
d24d5663 | 548 | status = (int) method(comp, self_port, (void *) other_port); |
d94d92ac | 549 | BT_LOGD("User method returned: status=%s", |
d24d5663 PP |
550 | bt_common_func_status_string(status)); |
551 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
552 | status == BT_FUNC_STATUS_ERROR || | |
553 | status == BT_FUNC_STATUS_MEMORY_ERROR, | |
4725a201 | 554 | "Unexpected returned component status: status=%s", |
d24d5663 | 555 | bt_common_func_status_string(status)); |
0d8b4d8e | 556 | } |
bf55043c PP |
557 | |
558 | return status; | |
0d8b4d8e PP |
559 | } |
560 | ||
3230ee6b PP |
561 | BT_HIDDEN |
562 | void bt_component_add_destroy_listener(struct bt_component *component, | |
563 | bt_component_destroy_listener_func func, void *data) | |
564 | { | |
565 | struct bt_component_destroy_listener listener; | |
566 | ||
f6ccaed9 PP |
567 | BT_ASSERT(component); |
568 | BT_ASSERT(func); | |
3230ee6b PP |
569 | listener.func = func; |
570 | listener.data = data; | |
571 | g_array_append_val(component->destroy_listeners, listener); | |
3f7d4d90 | 572 | BT_LIB_LOGD("Added destroy listener: %![comp-]+c, " |
ab0d387b | 573 | "func-addr=%p, data-addr=%p", |
d94d92ac | 574 | component, func, data); |
3230ee6b PP |
575 | } |
576 | ||
577 | BT_HIDDEN | |
578 | void bt_component_remove_destroy_listener(struct bt_component *component, | |
579 | bt_component_destroy_listener_func func, void *data) | |
580 | { | |
d94d92ac | 581 | uint64_t i; |
3230ee6b | 582 | |
f6ccaed9 PP |
583 | BT_ASSERT(component); |
584 | BT_ASSERT(func); | |
3230ee6b PP |
585 | |
586 | for (i = 0; i < component->destroy_listeners->len; i++) { | |
587 | struct bt_component_destroy_listener *listener = | |
588 | &g_array_index(component->destroy_listeners, | |
589 | struct bt_component_destroy_listener, i); | |
590 | ||
591 | if (listener->func == func && listener->data == data) { | |
592 | g_array_remove_index(component->destroy_listeners, i); | |
593 | i--; | |
3f7d4d90 | 594 | BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, " |
ab0d387b | 595 | "func-addr=%p, data-addr=%p", |
d94d92ac | 596 | component, func, data); |
3230ee6b PP |
597 | } |
598 | } | |
599 | } | |
c5b9b441 | 600 | |
e874da19 PP |
601 | bt_logging_level bt_component_get_logging_level( |
602 | const struct bt_component *component) | |
603 | { | |
bdb288b3 | 604 | BT_ASSERT_PRE_DEV_NON_NULL(component, "Component"); |
e874da19 PP |
605 | return component->log_level; |
606 | } | |
607 | ||
c5b9b441 PP |
608 | void bt_component_get_ref(const struct bt_component *component) |
609 | { | |
610 | bt_object_get_ref(component); | |
611 | } | |
612 | ||
613 | void bt_component_put_ref(const struct bt_component *component) | |
614 | { | |
615 | bt_object_put_ref(component); | |
616 | } |