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