Commit | Line | Data |
---|---|---|
958f7d11 PP |
1 | /* |
2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
fed72692 PP |
23 | #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT" |
24 | #include "logging.h" | |
25 | ||
958f7d11 | 26 | #include <babeltrace/babeltrace-internal.h> |
282c8cd0 | 27 | #include <babeltrace/compat/uuid-internal.h> |
9d408fca PP |
28 | #include <babeltrace/babeltrace.h> |
29 | #include <babeltrace/values-internal.h> | |
fed72692 | 30 | #include <babeltrace/graph/component-internal.h> |
fed72692 | 31 | #include <babeltrace/graph/notification-iterator-internal.h> |
fed72692 | 32 | #include <babeltrace/graph/connection-internal.h> |
958f7d11 PP |
33 | #include <plugins-common.h> |
34 | #include <glib.h> | |
c55a9f58 | 35 | #include <stdbool.h> |
fed72692 | 36 | #include <inttypes.h> |
f6ccaed9 | 37 | #include <babeltrace/assert-internal.h> |
351704a7 | 38 | #include <babeltrace/common-internal.h> |
0fbb9a9f | 39 | #include <stdlib.h> |
282c8cd0 | 40 | #include <string.h> |
958f7d11 | 41 | |
c3acd5f3 | 42 | #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes" |
65ee897d | 43 | |
958f7d11 | 44 | struct muxer_comp { |
90157d89 PP |
45 | /* |
46 | * Array of struct | |
47 | * bt_private_connection_private_notification_iterator * | |
48 | * (weak refs) | |
49 | */ | |
958f7d11 PP |
50 | GPtrArray *muxer_notif_iters; |
51 | ||
52 | /* Weak ref */ | |
53 | struct bt_private_component *priv_comp; | |
54 | unsigned int next_port_num; | |
55 | size_t available_input_ports; | |
a09c6b95 | 56 | bool initializing_muxer_notif_iter; |
282c8cd0 | 57 | bool assume_absolute_clock_classes; |
958f7d11 PP |
58 | }; |
59 | ||
60 | struct muxer_upstream_notif_iter { | |
ab11110e | 61 | /* Owned by this, NULL if ended */ |
958f7d11 PP |
62 | struct bt_notification_iterator *notif_iter; |
63 | ||
d4393e08 PP |
64 | /* Contains `struct bt_notification *`, owned by this */ |
65 | GQueue *notifs; | |
958f7d11 PP |
66 | }; |
67 | ||
282c8cd0 PP |
68 | enum muxer_notif_iter_clock_class_expectation { |
69 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0, | |
70 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE, | |
71 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID, | |
72 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID, | |
73 | }; | |
74 | ||
958f7d11 | 75 | struct muxer_notif_iter { |
ab11110e PP |
76 | /* |
77 | * Array of struct muxer_upstream_notif_iter * (owned by this). | |
78 | * | |
79 | * NOTE: This array is searched in linearly to find the youngest | |
80 | * current notification. Keep this until benchmarks confirm that | |
81 | * another data structure is faster than this for our typical | |
82 | * use cases. | |
83 | */ | |
958f7d11 PP |
84 | GPtrArray *muxer_upstream_notif_iters; |
85 | ||
ab11110e | 86 | /* |
06a2cb0d | 87 | * List of "recently" connected input ports (weak) to |
ab11110e PP |
88 | * handle by this muxer notification iterator. |
89 | * muxer_port_connected() adds entries to this list, and the | |
90 | * entries are removed when a notification iterator is created | |
91 | * on the port's connection and put into | |
92 | * muxer_upstream_notif_iters above by | |
93 | * muxer_notif_iter_handle_newly_connected_ports(). | |
94 | */ | |
95 | GList *newly_connected_priv_ports; | |
958f7d11 | 96 | |
958f7d11 PP |
97 | /* Last time returned in a notification */ |
98 | int64_t last_returned_ts_ns; | |
282c8cd0 PP |
99 | |
100 | /* Clock class expectation state */ | |
101 | enum muxer_notif_iter_clock_class_expectation clock_class_expectation; | |
102 | ||
103 | /* | |
104 | * Expected clock class UUID, only valid when | |
105 | * clock_class_expectation is | |
106 | * MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID. | |
107 | */ | |
108 | unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN]; | |
958f7d11 PP |
109 | }; |
110 | ||
ab11110e PP |
111 | static |
112 | void destroy_muxer_upstream_notif_iter( | |
113 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) | |
114 | { | |
115 | if (!muxer_upstream_notif_iter) { | |
116 | return; | |
117 | } | |
118 | ||
fed72692 | 119 | BT_LOGD("Destroying muxer's upstream notification iterator wrapper: " |
d4393e08 | 120 | "addr=%p, notif-iter-addr=%p, queue-len=%u", |
fed72692 PP |
121 | muxer_upstream_notif_iter, |
122 | muxer_upstream_notif_iter->notif_iter, | |
d4393e08 | 123 | muxer_upstream_notif_iter->notifs->length); |
e2317362 | 124 | bt_object_put_ref(muxer_upstream_notif_iter->notif_iter); |
d4393e08 PP |
125 | |
126 | if (muxer_upstream_notif_iter->notifs) { | |
127 | struct bt_notification *notif; | |
128 | ||
129 | while ((notif = g_queue_pop_head( | |
130 | muxer_upstream_notif_iter->notifs))) { | |
e2317362 | 131 | bt_object_put_ref(notif); |
d4393e08 PP |
132 | } |
133 | ||
134 | g_queue_free(muxer_upstream_notif_iter->notifs); | |
135 | } | |
136 | ||
ab11110e PP |
137 | g_free(muxer_upstream_notif_iter); |
138 | } | |
139 | ||
958f7d11 PP |
140 | static |
141 | struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter( | |
142 | struct muxer_notif_iter *muxer_notif_iter, | |
143 | struct bt_notification_iterator *notif_iter, | |
144 | struct bt_private_port *priv_port) | |
145 | { | |
146 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = | |
147 | g_new0(struct muxer_upstream_notif_iter, 1); | |
148 | ||
149 | if (!muxer_upstream_notif_iter) { | |
fed72692 | 150 | BT_LOGE_STR("Failed to allocate one muxer's upstream notification iterator wrapper."); |
958f7d11 PP |
151 | goto end; |
152 | } | |
153 | ||
e2317362 | 154 | muxer_upstream_notif_iter->notif_iter = bt_object_get_ref(notif_iter); |
d4393e08 PP |
155 | muxer_upstream_notif_iter->notifs = g_queue_new(); |
156 | if (!muxer_upstream_notif_iter->notifs) { | |
157 | BT_LOGE_STR("Failed to allocate a GQueue."); | |
158 | ||
159 | goto end; | |
160 | } | |
161 | ||
958f7d11 PP |
162 | g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters, |
163 | muxer_upstream_notif_iter); | |
fed72692 PP |
164 | BT_LOGD("Added muxer's upstream notification iterator wrapper: " |
165 | "addr=%p, muxer-notif-iter-addr=%p, notif-iter-addr=%p", | |
166 | muxer_upstream_notif_iter, muxer_notif_iter, | |
167 | notif_iter); | |
958f7d11 PP |
168 | |
169 | end: | |
170 | return muxer_upstream_notif_iter; | |
171 | } | |
172 | ||
958f7d11 | 173 | static |
147337a3 PP |
174 | enum bt_component_status ensure_available_input_port( |
175 | struct bt_private_component *priv_comp) | |
958f7d11 PP |
176 | { |
177 | struct muxer_comp *muxer_comp = | |
178 | bt_private_component_get_user_data(priv_comp); | |
147337a3 | 179 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
958f7d11 | 180 | GString *port_name = NULL; |
958f7d11 | 181 | |
f6ccaed9 | 182 | BT_ASSERT(muxer_comp); |
958f7d11 PP |
183 | |
184 | if (muxer_comp->available_input_ports >= 1) { | |
185 | goto end; | |
186 | } | |
187 | ||
188 | port_name = g_string_new("in"); | |
189 | if (!port_name) { | |
fed72692 | 190 | BT_LOGE_STR("Failed to allocate a GString."); |
147337a3 | 191 | status = BT_COMPONENT_STATUS_NOMEM; |
958f7d11 PP |
192 | goto end; |
193 | } | |
194 | ||
195 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); | |
aa4d5da5 | 196 | status = bt_private_component_filter_add_input_port( |
147337a3 PP |
197 | priv_comp, port_name->str, NULL, NULL); |
198 | if (status != BT_COMPONENT_STATUS_OK) { | |
fed72692 PP |
199 | BT_LOGE("Cannot add input port to muxer component: " |
200 | "port-name=\"%s\", comp-addr=%p, status=%s", | |
201 | port_name->str, priv_comp, | |
202 | bt_component_status_string(status)); | |
958f7d11 PP |
203 | goto end; |
204 | } | |
205 | ||
206 | muxer_comp->available_input_ports++; | |
207 | muxer_comp->next_port_num++; | |
fed72692 PP |
208 | BT_LOGD("Added one input port to muxer component: " |
209 | "port-name=\"%s\", comp-addr=%p", | |
210 | port_name->str, priv_comp); | |
958f7d11 PP |
211 | end: |
212 | if (port_name) { | |
213 | g_string_free(port_name, TRUE); | |
214 | } | |
215 | ||
147337a3 | 216 | return status; |
958f7d11 PP |
217 | } |
218 | ||
958f7d11 | 219 | static |
147337a3 PP |
220 | enum bt_component_status create_output_port( |
221 | struct bt_private_component *priv_comp) | |
958f7d11 | 222 | { |
aa4d5da5 | 223 | return bt_private_component_filter_add_output_port( |
147337a3 | 224 | priv_comp, "out", NULL, NULL); |
958f7d11 PP |
225 | } |
226 | ||
227 | static | |
228 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) | |
229 | { | |
230 | if (!muxer_comp) { | |
231 | return; | |
232 | } | |
233 | ||
fed72692 PP |
234 | BT_LOGD("Destroying muxer component: muxer-comp-addr=%p, " |
235 | "muxer-notif-iter-count=%u", muxer_comp, | |
af18c5e7 JG |
236 | muxer_comp->muxer_notif_iters ? |
237 | muxer_comp->muxer_notif_iters->len : 0); | |
fed72692 | 238 | |
958f7d11 PP |
239 | if (muxer_comp->muxer_notif_iters) { |
240 | g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE); | |
241 | } | |
242 | ||
243 | g_free(muxer_comp); | |
244 | } | |
245 | ||
65ee897d | 246 | static |
351704a7 | 247 | struct bt_private_value *get_default_params(void) |
65ee897d | 248 | { |
351704a7 | 249 | struct bt_private_value *params; |
65ee897d PP |
250 | int ret; |
251 | ||
351704a7 | 252 | params = bt_private_value_map_create(); |
65ee897d | 253 | if (!params) { |
fed72692 | 254 | BT_LOGE_STR("Cannot create a map value object."); |
65ee897d PP |
255 | goto error; |
256 | } | |
257 | ||
351704a7 | 258 | ret = bt_private_value_map_insert_bool_entry(params, |
c3acd5f3 | 259 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false); |
65ee897d | 260 | if (ret) { |
fed72692 | 261 | BT_LOGE_STR("Cannot add boolean value to map value object."); |
65ee897d PP |
262 | goto error; |
263 | } | |
264 | ||
265 | goto end; | |
266 | ||
267 | error: | |
e2317362 | 268 | BT_OBJECT_PUT_REF_AND_RESET(params); |
65ee897d PP |
269 | |
270 | end: | |
271 | return params; | |
272 | } | |
273 | ||
274 | static | |
275 | int configure_muxer_comp(struct muxer_comp *muxer_comp, struct bt_value *params) | |
276 | { | |
351704a7 PP |
277 | struct bt_private_value *default_params = NULL; |
278 | struct bt_private_value *real_params = NULL; | |
282c8cd0 | 279 | struct bt_value *assume_absolute_clock_classes = NULL; |
65ee897d | 280 | int ret = 0; |
c55a9f58 | 281 | bt_bool bool_val; |
65ee897d PP |
282 | |
283 | default_params = get_default_params(); | |
284 | if (!default_params) { | |
fed72692 PP |
285 | BT_LOGE("Cannot get default parameters: " |
286 | "muxer-comp-addr=%p", muxer_comp); | |
65ee897d PP |
287 | goto error; |
288 | } | |
289 | ||
605c62ba | 290 | ret = bt_value_map_extend(&real_params, |
351704a7 | 291 | bt_value_borrow_from_private(default_params), params); |
605c62ba | 292 | if (ret) { |
fed72692 PP |
293 | BT_LOGE("Cannot extend default parameters map value: " |
294 | "muxer-comp-addr=%p, def-params-addr=%p, " | |
295 | "params-addr=%p", muxer_comp, default_params, | |
296 | params); | |
65ee897d PP |
297 | goto error; |
298 | } | |
299 | ||
351704a7 PP |
300 | assume_absolute_clock_classes = bt_value_map_borrow_entry_value( |
301 | bt_value_borrow_from_private(real_params), | |
c3acd5f3 | 302 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME); |
f6ccaed9 PP |
303 | if (assume_absolute_clock_classes && |
304 | !bt_value_is_bool(assume_absolute_clock_classes)) { | |
fed72692 PP |
305 | BT_LOGE("Expecting a boolean value for the `%s` parameter: " |
306 | "muxer-comp-addr=%p, value-type=%s", | |
307 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp, | |
351704a7 | 308 | bt_common_value_type_string( |
fed72692 | 309 | bt_value_get_type(assume_absolute_clock_classes))); |
65ee897d PP |
310 | goto error; |
311 | } | |
312 | ||
605c62ba | 313 | bool_val = bt_value_bool_get(assume_absolute_clock_classes); |
282c8cd0 | 314 | muxer_comp->assume_absolute_clock_classes = (bool) bool_val; |
fed72692 PP |
315 | BT_LOGD("Configured muxer component: muxer-comp-addr=%p, " |
316 | "assume-absolute-clock-classes=%d", | |
317 | muxer_comp, muxer_comp->assume_absolute_clock_classes); | |
65ee897d PP |
318 | goto end; |
319 | ||
320 | error: | |
321 | ret = -1; | |
322 | ||
323 | end: | |
e2317362 PP |
324 | bt_object_put_ref(default_params); |
325 | bt_object_put_ref(real_params); | |
65ee897d PP |
326 | return ret; |
327 | } | |
328 | ||
958f7d11 PP |
329 | BT_HIDDEN |
330 | enum bt_component_status muxer_init( | |
331 | struct bt_private_component *priv_comp, | |
332 | struct bt_value *params, void *init_data) | |
333 | { | |
334 | int ret; | |
335 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; | |
336 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); | |
337 | ||
fed72692 PP |
338 | BT_LOGD("Initializing muxer component: " |
339 | "comp-addr=%p, params-addr=%p", priv_comp, params); | |
340 | ||
958f7d11 | 341 | if (!muxer_comp) { |
fed72692 | 342 | BT_LOGE_STR("Failed to allocate one muxer component."); |
958f7d11 PP |
343 | goto error; |
344 | } | |
345 | ||
65ee897d PP |
346 | ret = configure_muxer_comp(muxer_comp, params); |
347 | if (ret) { | |
fed72692 PP |
348 | BT_LOGE("Cannot configure muxer component: " |
349 | "muxer-comp-addr=%p, params-addr=%p", | |
350 | muxer_comp, params); | |
65ee897d PP |
351 | goto error; |
352 | } | |
353 | ||
958f7d11 PP |
354 | muxer_comp->muxer_notif_iters = g_ptr_array_new(); |
355 | if (!muxer_comp->muxer_notif_iters) { | |
fed72692 | 356 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
357 | goto error; |
358 | } | |
359 | ||
360 | muxer_comp->priv_comp = priv_comp; | |
361 | ret = bt_private_component_set_user_data(priv_comp, muxer_comp); | |
f6ccaed9 | 362 | BT_ASSERT(ret == 0); |
147337a3 PP |
363 | status = ensure_available_input_port(priv_comp); |
364 | if (status != BT_COMPONENT_STATUS_OK) { | |
fed72692 PP |
365 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
366 | "muxer-comp-addr=%p, status=%s", | |
367 | muxer_comp, | |
368 | bt_component_status_string(status)); | |
958f7d11 PP |
369 | goto error; |
370 | } | |
371 | ||
fed72692 PP |
372 | status = create_output_port(priv_comp); |
373 | if (status) { | |
374 | BT_LOGE("Cannot create muxer component's output port: " | |
375 | "muxer-comp-addr=%p, status=%s", | |
376 | muxer_comp, | |
377 | bt_component_status_string(status)); | |
958f7d11 PP |
378 | goto error; |
379 | } | |
380 | ||
fed72692 PP |
381 | BT_LOGD("Initialized muxer component: " |
382 | "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p", | |
383 | priv_comp, params, muxer_comp); | |
384 | ||
958f7d11 PP |
385 | goto end; |
386 | ||
387 | error: | |
388 | destroy_muxer_comp(muxer_comp); | |
389 | ret = bt_private_component_set_user_data(priv_comp, NULL); | |
f6ccaed9 | 390 | BT_ASSERT(ret == 0); |
147337a3 PP |
391 | |
392 | if (status == BT_COMPONENT_STATUS_OK) { | |
393 | status = BT_COMPONENT_STATUS_ERROR; | |
394 | } | |
958f7d11 PP |
395 | |
396 | end: | |
397 | return status; | |
398 | } | |
399 | ||
400 | BT_HIDDEN | |
401 | void muxer_finalize(struct bt_private_component *priv_comp) | |
402 | { | |
403 | struct muxer_comp *muxer_comp = | |
404 | bt_private_component_get_user_data(priv_comp); | |
405 | ||
fed72692 PP |
406 | BT_LOGD("Finalizing muxer component: comp-addr=%p", |
407 | priv_comp); | |
958f7d11 PP |
408 | destroy_muxer_comp(muxer_comp); |
409 | } | |
410 | ||
411 | static | |
412 | struct bt_notification_iterator *create_notif_iter_on_input_port( | |
413 | struct bt_private_port *priv_port, int *ret) | |
414 | { | |
5c563278 | 415 | struct bt_port *port = bt_port_borrow_from_private(priv_port); |
958f7d11 PP |
416 | struct bt_notification_iterator *notif_iter = NULL; |
417 | struct bt_private_connection *priv_conn = NULL; | |
73d5c1ad | 418 | enum bt_connection_status conn_status; |
958f7d11 | 419 | |
f6ccaed9 | 420 | BT_ASSERT(ret); |
958f7d11 | 421 | *ret = 0; |
f6ccaed9 PP |
422 | BT_ASSERT(port); |
423 | BT_ASSERT(bt_port_is_connected(port)); | |
aa4d5da5 | 424 | priv_conn = bt_private_port_get_connection(priv_port); |
f6ccaed9 | 425 | BT_ASSERT(priv_conn); |
958f7d11 | 426 | |
ab11110e PP |
427 | // TODO: Advance the iterator to >= the time of the latest |
428 | // returned notification by the muxer notification | |
429 | // iterator which creates it. | |
73d5c1ad | 430 | conn_status = bt_private_connection_create_notification_iterator( |
f42867e2 | 431 | priv_conn, ¬if_iter); |
73d5c1ad | 432 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
fed72692 PP |
433 | BT_LOGE("Cannot create upstream notification iterator on input port's connection: " |
434 | "port-addr=%p, port-name=\"%s\", conn-addr=%p, " | |
435 | "status=%s", | |
436 | port, bt_port_get_name(port), priv_conn, | |
437 | bt_connection_status_string(conn_status)); | |
958f7d11 PP |
438 | *ret = -1; |
439 | goto end; | |
440 | } | |
441 | ||
fed72692 PP |
442 | BT_LOGD("Created upstream notification iterator on input port's connection: " |
443 | "port-addr=%p, port-name=\"%s\", conn-addr=%p, " | |
444 | "notif-iter-addr=%p", | |
445 | port, bt_port_get_name(port), priv_conn, notif_iter); | |
446 | ||
958f7d11 | 447 | end: |
e2317362 | 448 | bt_object_put_ref(priv_conn); |
958f7d11 PP |
449 | return notif_iter; |
450 | } | |
451 | ||
ab11110e | 452 | static |
089717de | 453 | enum bt_notification_iterator_status muxer_upstream_notif_iter_next( |
ab11110e PP |
454 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) |
455 | { | |
089717de | 456 | enum bt_notification_iterator_status status; |
d4393e08 PP |
457 | bt_notification_array notifs; |
458 | uint64_t i; | |
459 | uint64_t count; | |
ab11110e | 460 | |
fed72692 PP |
461 | BT_LOGV("Calling upstream notification iterator's \"next\" method: " |
462 | "muxer-upstream-notif-iter-wrap-addr=%p, notif-iter-addr=%p", | |
463 | muxer_upstream_notif_iter, | |
464 | muxer_upstream_notif_iter->notif_iter); | |
07245ac2 | 465 | status = bt_private_connection_notification_iterator_next( |
d4393e08 | 466 | muxer_upstream_notif_iter->notif_iter, ¬ifs, &count); |
fed72692 PP |
467 | BT_LOGV("Upstream notification iterator's \"next\" method returned: " |
468 | "status=%s", bt_notification_iterator_status_string(status)); | |
ab11110e | 469 | |
089717de | 470 | switch (status) { |
ab11110e | 471 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
089717de | 472 | /* |
d4393e08 PP |
473 | * Notification iterator's current notification is |
474 | * valid: it must be considered for muxing operations. | |
089717de | 475 | */ |
fed72692 | 476 | BT_LOGV_STR("Validated upstream notification iterator wrapper."); |
d4393e08 PP |
477 | BT_ASSERT(count > 0); |
478 | ||
479 | /* Move notifications to our queue */ | |
480 | for (i = 0; i < count; i++) { | |
481 | /* | |
482 | * Push to tail in order; other side | |
483 | * (muxer_notif_iter_do_next_one()) consumes | |
484 | * from the head first. | |
485 | */ | |
486 | g_queue_push_tail(muxer_upstream_notif_iter->notifs, | |
487 | notifs[i]); | |
488 | } | |
ab11110e PP |
489 | break; |
490 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
089717de PP |
491 | /* |
492 | * Notification iterator's current notification is not | |
493 | * valid anymore. Return | |
d4393e08 | 494 | * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN immediately. |
089717de | 495 | */ |
ab11110e | 496 | break; |
beed0223 MD |
497 | case BT_NOTIFICATION_ITERATOR_STATUS_END: /* Fall-through. */ |
498 | case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: | |
ab11110e PP |
499 | /* |
500 | * Notification iterator reached the end: release it. It | |
501 | * won't be considered again to find the youngest | |
502 | * notification. | |
503 | */ | |
e2317362 | 504 | BT_OBJECT_PUT_REF_AND_RESET(muxer_upstream_notif_iter->notif_iter); |
089717de PP |
505 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
506 | break; | |
ab11110e PP |
507 | default: |
508 | /* Error or unsupported status code */ | |
fed72692 PP |
509 | BT_LOGE("Error or unsupported status code: " |
510 | "status-code=%d", status); | |
089717de PP |
511 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
512 | break; | |
ab11110e PP |
513 | } |
514 | ||
089717de | 515 | return status; |
ab11110e PP |
516 | } |
517 | ||
518 | static | |
089717de | 519 | int muxer_notif_iter_handle_newly_connected_ports( |
ab11110e PP |
520 | struct muxer_notif_iter *muxer_notif_iter) |
521 | { | |
ab11110e PP |
522 | int ret = 0; |
523 | ||
fed72692 PP |
524 | BT_LOGV("Handling newly connected ports: " |
525 | "muxer-notif-iter-addr=%p", muxer_notif_iter); | |
526 | ||
ab11110e PP |
527 | /* |
528 | * Here we create one upstream notification iterator for each | |
d4393e08 | 529 | * newly connected port. We do NOT perform an initial "next" on |
089717de PP |
530 | * those new upstream notification iterators: they are |
531 | * invalidated, to be validated later. The list of newly | |
532 | * connected ports to handle here is updated by | |
533 | * muxer_port_connected(). | |
ab11110e PP |
534 | */ |
535 | while (true) { | |
536 | GList *node = muxer_notif_iter->newly_connected_priv_ports; | |
537 | struct bt_private_port *priv_port; | |
538 | struct bt_port *port; | |
539 | struct bt_notification_iterator *upstream_notif_iter = NULL; | |
540 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter; | |
541 | ||
542 | if (!node) { | |
543 | break; | |
544 | } | |
545 | ||
546 | priv_port = node->data; | |
5c563278 | 547 | port = bt_port_borrow_from_private(priv_port); |
f6ccaed9 | 548 | BT_ASSERT(port); |
ab11110e PP |
549 | |
550 | if (!bt_port_is_connected(port)) { | |
551 | /* | |
552 | * Looks like this port is not connected | |
553 | * anymore: we can't create an upstream | |
089717de PP |
554 | * notification iterator on its (non-existing) |
555 | * connection in this case. | |
ab11110e PP |
556 | */ |
557 | goto remove_node; | |
558 | } | |
559 | ||
ab11110e PP |
560 | upstream_notif_iter = create_notif_iter_on_input_port(priv_port, |
561 | &ret); | |
562 | if (ret) { | |
fed72692 | 563 | /* create_notif_iter_on_input_port() logs errors */ |
f6ccaed9 | 564 | BT_ASSERT(!upstream_notif_iter); |
ab11110e PP |
565 | goto error; |
566 | } | |
567 | ||
568 | muxer_upstream_notif_iter = | |
569 | muxer_notif_iter_add_upstream_notif_iter( | |
570 | muxer_notif_iter, upstream_notif_iter, | |
571 | priv_port); | |
e2317362 | 572 | BT_OBJECT_PUT_REF_AND_RESET(upstream_notif_iter); |
ab11110e | 573 | if (!muxer_upstream_notif_iter) { |
fed72692 PP |
574 | /* |
575 | * muxer_notif_iter_add_upstream_notif_iter() | |
576 | * logs errors. | |
577 | */ | |
ab11110e PP |
578 | goto error; |
579 | } | |
580 | ||
ab11110e | 581 | remove_node: |
e2317362 | 582 | bt_object_put_ref(upstream_notif_iter); |
ab11110e PP |
583 | muxer_notif_iter->newly_connected_priv_ports = |
584 | g_list_delete_link( | |
585 | muxer_notif_iter->newly_connected_priv_ports, | |
586 | node); | |
587 | } | |
588 | ||
589 | goto end; | |
590 | ||
591 | error: | |
089717de | 592 | if (ret >= 0) { |
ab11110e PP |
593 | ret = -1; |
594 | } | |
595 | ||
596 | end: | |
ab11110e PP |
597 | return ret; |
598 | } | |
599 | ||
958f7d11 PP |
600 | static |
601 | int get_notif_ts_ns(struct muxer_comp *muxer_comp, | |
282c8cd0 | 602 | struct muxer_notif_iter *muxer_notif_iter, |
958f7d11 PP |
603 | struct bt_notification *notif, int64_t last_returned_ts_ns, |
604 | int64_t *ts_ns) | |
605 | { | |
50842bdc PP |
606 | struct bt_clock_class *clock_class = NULL; |
607 | struct bt_clock_value *clock_value = NULL; | |
608 | struct bt_event *event = NULL; | |
958f7d11 | 609 | int ret = 0; |
282c8cd0 | 610 | const unsigned char *cc_uuid; |
fed72692 | 611 | const char *cc_name; |
61f59358 | 612 | enum bt_clock_value_status cv_status = BT_CLOCK_VALUE_STATUS_KNOWN; |
958f7d11 | 613 | |
f6ccaed9 PP |
614 | BT_ASSERT(notif); |
615 | BT_ASSERT(ts_ns); | |
958f7d11 | 616 | |
fed72692 PP |
617 | BT_LOGV("Getting notification's timestamp: " |
618 | "muxer-notif-iter-addr=%p, notif-addr=%p, " | |
619 | "last-returned-ts=%" PRId64, | |
620 | muxer_notif_iter, notif, last_returned_ts_ns); | |
621 | ||
958f7d11 PP |
622 | switch (bt_notification_get_type(notif)) { |
623 | case BT_NOTIFICATION_TYPE_EVENT: | |
e22b45d0 PP |
624 | event = bt_notification_event_borrow_event(notif); |
625 | BT_ASSERT(event); | |
61f59358 PP |
626 | cv_status = bt_event_borrow_default_clock_value(event, |
627 | &clock_value); | |
958f7d11 PP |
628 | break; |
629 | ||
630 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
e22b45d0 PP |
631 | clock_value = |
632 | bt_notification_inactivity_borrow_default_clock_value( | |
958f7d11 PP |
633 | notif); |
634 | break; | |
635 | default: | |
089717de | 636 | /* All the other notifications have a higher priority */ |
fed72692 | 637 | BT_LOGV_STR("Notification has no timestamp: using the last returned timestamp."); |
958f7d11 PP |
638 | *ts_ns = last_returned_ts_ns; |
639 | goto end; | |
640 | } | |
641 | ||
61f59358 PP |
642 | if (cv_status != BT_CLOCK_VALUE_STATUS_KNOWN) { |
643 | BT_LOGE_STR("Unsupported unknown clock value."); | |
644 | ret = -1; | |
645 | goto end; | |
646 | } | |
647 | ||
958f7d11 | 648 | /* |
e22b45d0 PP |
649 | * If the clock value is missing, then we consider that this |
650 | * notification has no time. In this case it's always the | |
651 | * youngest. | |
958f7d11 | 652 | */ |
e22b45d0 PP |
653 | if (!clock_value) { |
654 | BT_LOGV_STR("Notification's default clock value is missing: " | |
fed72692 | 655 | "using the last returned timestamp."); |
958f7d11 PP |
656 | *ts_ns = last_returned_ts_ns; |
657 | goto end; | |
658 | } | |
659 | ||
61f59358 | 660 | clock_class = bt_clock_value_borrow_clock_class(clock_value); |
e22b45d0 | 661 | BT_ASSERT(clock_class); |
50842bdc PP |
662 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
663 | cc_name = bt_clock_class_get_name(clock_class); | |
282c8cd0 PP |
664 | |
665 | if (muxer_notif_iter->clock_class_expectation == | |
666 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
667 | /* | |
668 | * This is the first clock class that this muxer | |
669 | * notification iterator encounters. Its properties | |
670 | * determine what to expect for the whole lifetime of | |
671 | * the iterator without a true | |
672 | * `assume-absolute-clock-classes` parameter. | |
673 | */ | |
50842bdc | 674 | if (bt_clock_class_is_absolute(clock_class)) { |
282c8cd0 PP |
675 | /* Expect absolute clock classes */ |
676 | muxer_notif_iter->clock_class_expectation = | |
677 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; | |
678 | } else { | |
679 | if (cc_uuid) { | |
680 | /* | |
681 | * Expect non-absolute clock classes | |
682 | * with a specific UUID. | |
683 | */ | |
684 | muxer_notif_iter->clock_class_expectation = | |
685 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID; | |
686 | memcpy(muxer_notif_iter->expected_clock_class_uuid, | |
687 | cc_uuid, BABELTRACE_UUID_LEN); | |
688 | } else { | |
689 | /* | |
690 | * Expect non-absolute clock classes | |
691 | * with no UUID. | |
692 | */ | |
693 | muxer_notif_iter->clock_class_expectation = | |
694 | MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; | |
695 | } | |
696 | } | |
697 | } | |
698 | ||
699 | if (!muxer_comp->assume_absolute_clock_classes) { | |
700 | switch (muxer_notif_iter->clock_class_expectation) { | |
701 | case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: | |
50842bdc | 702 | if (!bt_clock_class_is_absolute(clock_class)) { |
fed72692 PP |
703 | BT_LOGE("Expecting an absolute clock class, " |
704 | "but got a non-absolute one: " | |
705 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
706 | clock_class, cc_name); | |
282c8cd0 PP |
707 | goto error; |
708 | } | |
709 | break; | |
710 | case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: | |
50842bdc | 711 | if (bt_clock_class_is_absolute(clock_class)) { |
fed72692 PP |
712 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
713 | "but got an absolute one: " | |
714 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
715 | clock_class, cc_name); | |
282c8cd0 PP |
716 | goto error; |
717 | } | |
718 | ||
719 | if (cc_uuid) { | |
fed72692 PP |
720 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
721 | "but got one with a UUID: " | |
722 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
723 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
724 | clock_class, cc_name, | |
725 | (unsigned int) cc_uuid[0], | |
726 | (unsigned int) cc_uuid[1], | |
727 | (unsigned int) cc_uuid[2], | |
728 | (unsigned int) cc_uuid[3], | |
729 | (unsigned int) cc_uuid[4], | |
730 | (unsigned int) cc_uuid[5], | |
731 | (unsigned int) cc_uuid[6], | |
732 | (unsigned int) cc_uuid[7], | |
733 | (unsigned int) cc_uuid[8], | |
734 | (unsigned int) cc_uuid[9], | |
735 | (unsigned int) cc_uuid[10], | |
736 | (unsigned int) cc_uuid[11], | |
737 | (unsigned int) cc_uuid[12], | |
738 | (unsigned int) cc_uuid[13], | |
739 | (unsigned int) cc_uuid[14], | |
740 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
741 | goto error; |
742 | } | |
743 | break; | |
744 | case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: | |
50842bdc | 745 | if (bt_clock_class_is_absolute(clock_class)) { |
fed72692 PP |
746 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
747 | "but got an absolute one: " | |
748 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
749 | clock_class, cc_name); | |
282c8cd0 PP |
750 | goto error; |
751 | } | |
752 | ||
753 | if (!cc_uuid) { | |
fed72692 PP |
754 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
755 | "but got one with no UUID: " | |
756 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
757 | clock_class, cc_name); | |
282c8cd0 PP |
758 | goto error; |
759 | } | |
760 | ||
761 | if (memcmp(muxer_notif_iter->expected_clock_class_uuid, | |
762 | cc_uuid, BABELTRACE_UUID_LEN) != 0) { | |
fed72692 PP |
763 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
764 | "but got one with different UUID: " | |
765 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
766 | "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " | |
767 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
768 | clock_class, cc_name, | |
769 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[0], | |
770 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[1], | |
771 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[2], | |
772 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[3], | |
773 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[4], | |
774 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[5], | |
775 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[6], | |
776 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[7], | |
777 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[8], | |
778 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[9], | |
779 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[10], | |
780 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[11], | |
781 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[12], | |
782 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[13], | |
783 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[14], | |
784 | (unsigned int) muxer_notif_iter->expected_clock_class_uuid[15], | |
785 | (unsigned int) cc_uuid[0], | |
786 | (unsigned int) cc_uuid[1], | |
787 | (unsigned int) cc_uuid[2], | |
788 | (unsigned int) cc_uuid[3], | |
789 | (unsigned int) cc_uuid[4], | |
790 | (unsigned int) cc_uuid[5], | |
791 | (unsigned int) cc_uuid[6], | |
792 | (unsigned int) cc_uuid[7], | |
793 | (unsigned int) cc_uuid[8], | |
794 | (unsigned int) cc_uuid[9], | |
795 | (unsigned int) cc_uuid[10], | |
796 | (unsigned int) cc_uuid[11], | |
797 | (unsigned int) cc_uuid[12], | |
798 | (unsigned int) cc_uuid[13], | |
799 | (unsigned int) cc_uuid[14], | |
800 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
801 | goto error; |
802 | } | |
803 | break; | |
804 | default: | |
805 | /* Unexpected */ | |
fed72692 PP |
806 | BT_LOGF("Unexpected clock class expectation: " |
807 | "expectation-code=%d", | |
808 | muxer_notif_iter->clock_class_expectation); | |
282c8cd0 PP |
809 | abort(); |
810 | } | |
958f7d11 PP |
811 | } |
812 | ||
61f59358 | 813 | ret = bt_clock_value_get_ns_from_origin(clock_value, ts_ns); |
958f7d11 | 814 | if (ret) { |
fed72692 PP |
815 | BT_LOGE("Cannot get nanoseconds from Epoch of clock value: " |
816 | "clock-value-addr=%p", clock_value); | |
958f7d11 PP |
817 | goto error; |
818 | } | |
819 | ||
820 | goto end; | |
821 | ||
822 | error: | |
823 | ret = -1; | |
824 | ||
825 | end: | |
fed72692 PP |
826 | if (ret == 0) { |
827 | BT_LOGV("Found notification's timestamp: " | |
828 | "muxer-notif-iter-addr=%p, notif-addr=%p, " | |
829 | "last-returned-ts=%" PRId64 ", ts=%" PRId64, | |
830 | muxer_notif_iter, notif, last_returned_ts_ns, | |
831 | *ts_ns); | |
832 | } | |
833 | ||
958f7d11 PP |
834 | return ret; |
835 | } | |
836 | ||
ab11110e PP |
837 | /* |
838 | * This function finds the youngest available notification amongst the | |
839 | * non-ended upstream notification iterators and returns the upstream | |
840 | * notification iterator which has it, or | |
841 | * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available | |
842 | * notification. | |
843 | * | |
844 | * This function does NOT: | |
845 | * | |
846 | * * Update any upstream notification iterator. | |
847 | * * Check for newly connected ports. | |
848 | * * Check the upstream notification iterators to retry. | |
849 | * | |
850 | * On sucess, this function sets *muxer_upstream_notif_iter to the | |
851 | * upstream notification iterator of which the current notification is | |
852 | * the youngest, and sets *ts_ns to its time. | |
853 | */ | |
958f7d11 PP |
854 | static |
855 | enum bt_notification_iterator_status | |
856 | muxer_notif_iter_youngest_upstream_notif_iter( | |
857 | struct muxer_comp *muxer_comp, | |
858 | struct muxer_notif_iter *muxer_notif_iter, | |
859 | struct muxer_upstream_notif_iter **muxer_upstream_notif_iter, | |
860 | int64_t *ts_ns) | |
861 | { | |
862 | size_t i; | |
863 | int ret; | |
864 | int64_t youngest_ts_ns = INT64_MAX; | |
865 | enum bt_notification_iterator_status status = | |
866 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
867 | ||
f6ccaed9 PP |
868 | BT_ASSERT(muxer_comp); |
869 | BT_ASSERT(muxer_notif_iter); | |
870 | BT_ASSERT(muxer_upstream_notif_iter); | |
958f7d11 PP |
871 | *muxer_upstream_notif_iter = NULL; |
872 | ||
873 | for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) { | |
874 | struct bt_notification *notif; | |
875 | struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter = | |
876 | g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i); | |
877 | int64_t notif_ts_ns; | |
878 | ||
879 | if (!cur_muxer_upstream_notif_iter->notif_iter) { | |
ab11110e | 880 | /* This upstream notification iterator is ended */ |
fed72692 PP |
881 | BT_LOGV("Skipping ended upstream notification iterator: " |
882 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
883 | cur_muxer_upstream_notif_iter); | |
958f7d11 PP |
884 | continue; |
885 | } | |
886 | ||
d4393e08 PP |
887 | BT_ASSERT(cur_muxer_upstream_notif_iter->notifs->length > 0); |
888 | notif = g_queue_peek_head(cur_muxer_upstream_notif_iter->notifs); | |
f6ccaed9 | 889 | BT_ASSERT(notif); |
282c8cd0 | 890 | ret = get_notif_ts_ns(muxer_comp, muxer_notif_iter, notif, |
958f7d11 | 891 | muxer_notif_iter->last_returned_ts_ns, ¬if_ts_ns); |
958f7d11 | 892 | if (ret) { |
fed72692 | 893 | /* get_notif_ts_ns() logs errors */ |
958f7d11 PP |
894 | *muxer_upstream_notif_iter = NULL; |
895 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
896 | goto end; | |
897 | } | |
898 | ||
899 | if (notif_ts_ns <= youngest_ts_ns) { | |
900 | *muxer_upstream_notif_iter = | |
901 | cur_muxer_upstream_notif_iter; | |
902 | youngest_ts_ns = notif_ts_ns; | |
903 | *ts_ns = youngest_ts_ns; | |
904 | } | |
905 | } | |
906 | ||
907 | if (!*muxer_upstream_notif_iter) { | |
908 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
909 | *ts_ns = INT64_MIN; | |
910 | } | |
911 | ||
912 | end: | |
913 | return status; | |
914 | } | |
915 | ||
916 | static | |
089717de PP |
917 | enum bt_notification_iterator_status validate_muxer_upstream_notif_iter( |
918 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) | |
958f7d11 | 919 | { |
089717de PP |
920 | enum bt_notification_iterator_status status = |
921 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
958f7d11 | 922 | |
fed72692 PP |
923 | BT_LOGV("Validating muxer's upstream notification iterator wrapper: " |
924 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
925 | muxer_upstream_notif_iter); | |
926 | ||
d4393e08 | 927 | if (muxer_upstream_notif_iter->notifs->length > 0 || |
089717de | 928 | !muxer_upstream_notif_iter->notif_iter) { |
d4393e08 PP |
929 | BT_LOGV("Already valid or not considered: " |
930 | "queue-len=%u, upstream-notif-iter-addr=%p", | |
931 | muxer_upstream_notif_iter->notifs->length, | |
932 | muxer_upstream_notif_iter->notif_iter); | |
ab11110e PP |
933 | goto end; |
934 | } | |
935 | ||
fed72692 | 936 | /* muxer_upstream_notif_iter_next() logs details/errors */ |
089717de PP |
937 | status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter); |
938 | ||
939 | end: | |
940 | return status; | |
941 | } | |
942 | ||
943 | static | |
944 | enum bt_notification_iterator_status validate_muxer_upstream_notif_iters( | |
d4393e08 | 945 | struct muxer_notif_iter *muxer_notif_iter) |
089717de PP |
946 | { |
947 | enum bt_notification_iterator_status status = | |
948 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
949 | size_t i; | |
950 | ||
fed72692 PP |
951 | BT_LOGV("Validating muxer's upstream notification iterator wrappers: " |
952 | "muxer-notif-iter-addr=%p", muxer_notif_iter); | |
953 | ||
089717de PP |
954 | for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) { |
955 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = | |
956 | g_ptr_array_index( | |
957 | muxer_notif_iter->muxer_upstream_notif_iters, | |
958 | i); | |
959 | ||
960 | status = validate_muxer_upstream_notif_iter( | |
961 | muxer_upstream_notif_iter); | |
962 | if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
fed72692 PP |
963 | if (status < 0) { |
964 | BT_LOGE("Cannot validate muxer's upstream notification iterator wrapper: " | |
965 | "muxer-notif-iter-addr=%p, " | |
966 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
967 | muxer_notif_iter, | |
968 | muxer_upstream_notif_iter); | |
969 | } else { | |
970 | BT_LOGV("Cannot validate muxer's upstream notification iterator wrapper: " | |
971 | "muxer-notif-iter-addr=%p, " | |
972 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
973 | muxer_notif_iter, | |
974 | muxer_upstream_notif_iter); | |
975 | } | |
976 | ||
089717de PP |
977 | goto end; |
978 | } | |
744ba28b PP |
979 | |
980 | /* | |
981 | * Remove this muxer upstream notification iterator | |
982 | * if it's ended or canceled. | |
983 | */ | |
984 | if (!muxer_upstream_notif_iter->notif_iter) { | |
985 | /* | |
986 | * Use g_ptr_array_remove_fast() because the | |
987 | * order of those elements is not important. | |
988 | */ | |
fed72692 PP |
989 | BT_LOGV("Removing muxer's upstream notification iterator wrapper: ended or canceled: " |
990 | "muxer-notif-iter-addr=%p, " | |
991 | "muxer-upstream-notif-iter-wrap-addr=%p", | |
992 | muxer_notif_iter, muxer_upstream_notif_iter); | |
744ba28b PP |
993 | g_ptr_array_remove_index_fast( |
994 | muxer_notif_iter->muxer_upstream_notif_iters, | |
995 | i); | |
996 | i--; | |
997 | } | |
089717de PP |
998 | } |
999 | ||
1000 | end: | |
1001 | return status; | |
1002 | } | |
1003 | ||
d4393e08 PP |
1004 | static inline |
1005 | enum bt_notification_iterator_status muxer_notif_iter_do_next_one( | |
089717de | 1006 | struct muxer_comp *muxer_comp, |
d4393e08 PP |
1007 | struct muxer_notif_iter *muxer_notif_iter, |
1008 | struct bt_notification **notif) | |
089717de | 1009 | { |
d4393e08 PP |
1010 | enum bt_notification_iterator_status status = |
1011 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
089717de | 1012 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL; |
089717de PP |
1013 | int64_t next_return_ts; |
1014 | ||
1015 | while (true) { | |
1016 | int ret = muxer_notif_iter_handle_newly_connected_ports( | |
1017 | muxer_notif_iter); | |
1018 | ||
1019 | if (ret) { | |
fed72692 PP |
1020 | BT_LOGE("Cannot handle newly connected input ports for muxer's notification iterator: " |
1021 | "muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1022 | "ret=%d", | |
1023 | muxer_comp, muxer_notif_iter, ret); | |
d4393e08 | 1024 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
089717de PP |
1025 | goto end; |
1026 | } | |
1027 | ||
d4393e08 PP |
1028 | status = validate_muxer_upstream_notif_iters(muxer_notif_iter); |
1029 | if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
3823b499 | 1030 | /* validate_muxer_upstream_notif_iters() logs details */ |
089717de PP |
1031 | goto end; |
1032 | } | |
ab11110e | 1033 | |
958f7d11 | 1034 | /* |
089717de PP |
1035 | * At this point, we know that all the existing upstream |
1036 | * notification iterators are valid. However the | |
1037 | * operations to validate them (during | |
1038 | * validate_muxer_upstream_notif_iters()) may have | |
1039 | * connected new ports. If no ports were connected | |
1040 | * during this operation, exit the loop. | |
958f7d11 | 1041 | */ |
089717de | 1042 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
fed72692 PP |
1043 | BT_LOGV("Not breaking this loop: muxer's notification iterator still has newly connected input ports to handle: " |
1044 | "muxer-comp-addr=%p", muxer_comp); | |
089717de PP |
1045 | break; |
1046 | } | |
958f7d11 PP |
1047 | } |
1048 | ||
f6ccaed9 | 1049 | BT_ASSERT(!muxer_notif_iter->newly_connected_priv_ports); |
089717de | 1050 | |
958f7d11 | 1051 | /* |
089717de PP |
1052 | * At this point we know that all the existing upstream |
1053 | * notification iterators are valid. We can find the one, | |
1054 | * amongst those, of which the current notification is the | |
1055 | * youngest. | |
958f7d11 | 1056 | */ |
d4393e08 | 1057 | status = muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp, |
958f7d11 | 1058 | muxer_notif_iter, &muxer_upstream_notif_iter, |
089717de | 1059 | &next_return_ts); |
d4393e08 PP |
1060 | if (status < 0 || status == BT_NOTIFICATION_ITERATOR_STATUS_END || |
1061 | status == BT_NOTIFICATION_ITERATOR_STATUS_CANCELED) { | |
1062 | if (status < 0) { | |
fed72692 PP |
1063 | BT_LOGE("Cannot find the youngest upstream notification iterator wrapper: " |
1064 | "status=%s", | |
d4393e08 | 1065 | bt_notification_iterator_status_string(status)); |
fed72692 PP |
1066 | } else { |
1067 | BT_LOGV("Cannot find the youngest upstream notification iterator wrapper: " | |
1068 | "status=%s", | |
d4393e08 | 1069 | bt_notification_iterator_status_string(status)); |
fed72692 PP |
1070 | } |
1071 | ||
958f7d11 PP |
1072 | goto end; |
1073 | } | |
1074 | ||
089717de | 1075 | if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) { |
fed72692 PP |
1076 | BT_LOGE("Youngest upstream notification iterator wrapper's timestamp is less than muxer's notification iterator's last returned timestamp: " |
1077 | "muxer-notif-iter-addr=%p, ts=%" PRId64 ", " | |
1078 | "last-returned-ts=%" PRId64, | |
1079 | muxer_notif_iter, next_return_ts, | |
1080 | muxer_notif_iter->last_returned_ts_ns); | |
d4393e08 | 1081 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1082 | goto end; |
1083 | } | |
1084 | ||
fed72692 PP |
1085 | BT_LOGV("Found youngest upstream notification iterator wrapper: " |
1086 | "muxer-notif-iter-addr=%p, " | |
1087 | "muxer-upstream-notif-iter-wrap-addr=%p, " | |
1088 | "ts=%" PRId64, | |
1089 | muxer_notif_iter, muxer_upstream_notif_iter, next_return_ts); | |
d4393e08 | 1090 | BT_ASSERT(status == BT_NOTIFICATION_ITERATOR_STATUS_OK); |
f6ccaed9 | 1091 | BT_ASSERT(muxer_upstream_notif_iter); |
958f7d11 PP |
1092 | |
1093 | /* | |
d4393e08 PP |
1094 | * Consume from the queue's head: other side |
1095 | * (muxer_upstream_notif_iter_next()) writes to the tail. | |
958f7d11 | 1096 | */ |
d4393e08 PP |
1097 | *notif = g_queue_pop_head(muxer_upstream_notif_iter->notifs); |
1098 | BT_ASSERT(*notif); | |
089717de | 1099 | muxer_notif_iter->last_returned_ts_ns = next_return_ts; |
958f7d11 PP |
1100 | |
1101 | end: | |
d4393e08 PP |
1102 | return status; |
1103 | } | |
1104 | ||
1105 | static | |
1106 | enum bt_notification_iterator_status muxer_notif_iter_do_next( | |
1107 | struct muxer_comp *muxer_comp, | |
1108 | struct muxer_notif_iter *muxer_notif_iter, | |
1109 | bt_notification_array notifs, uint64_t capacity, | |
1110 | uint64_t *count) | |
1111 | { | |
1112 | enum bt_notification_iterator_status status = | |
1113 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
1114 | uint64_t i = 0; | |
1115 | ||
1116 | while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
1117 | status = muxer_notif_iter_do_next_one(muxer_comp, | |
1118 | muxer_notif_iter, ¬ifs[i]); | |
1119 | if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
1120 | i++; | |
1121 | } | |
1122 | } | |
1123 | ||
1124 | if (i > 0) { | |
1125 | /* | |
1126 | * Even if muxer_notif_iter_do_next_one() returned | |
1127 | * something else than | |
1128 | * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated | |
1129 | * notification objects in the output notification | |
1130 | * array, so we need to return | |
1131 | * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are | |
1132 | * transfered to downstream. This other status occurs | |
1133 | * again the next time muxer_notif_iter_do_next() is | |
1134 | * called, possibly without any accumulated | |
1135 | * notification, in which case we'll return it. | |
1136 | */ | |
1137 | *count = i; | |
1138 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
1139 | } | |
1140 | ||
1141 | return status; | |
958f7d11 PP |
1142 | } |
1143 | ||
1144 | static | |
ab11110e PP |
1145 | void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter) |
1146 | { | |
ab11110e PP |
1147 | if (!muxer_notif_iter) { |
1148 | return; | |
1149 | } | |
1150 | ||
fed72692 PP |
1151 | BT_LOGD("Destroying muxer component's notification iterator: " |
1152 | "muxer-notif-iter-addr=%p", muxer_notif_iter); | |
1153 | ||
ab11110e | 1154 | if (muxer_notif_iter->muxer_upstream_notif_iters) { |
fed72692 | 1155 | BT_LOGD_STR("Destroying muxer's upstream notification iterator wrappers."); |
ab11110e PP |
1156 | g_ptr_array_free( |
1157 | muxer_notif_iter->muxer_upstream_notif_iters, TRUE); | |
1158 | } | |
1159 | ||
ab11110e PP |
1160 | g_list_free(muxer_notif_iter->newly_connected_priv_ports); |
1161 | g_free(muxer_notif_iter); | |
1162 | } | |
1163 | ||
1164 | static | |
1165 | int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp, | |
958f7d11 PP |
1166 | struct muxer_notif_iter *muxer_notif_iter) |
1167 | { | |
ab11110e | 1168 | struct bt_component *comp; |
544d0515 PP |
1169 | int64_t count; |
1170 | int64_t i; | |
ab11110e | 1171 | int ret = 0; |
958f7d11 | 1172 | |
ab11110e PP |
1173 | /* |
1174 | * Add the connected input ports to this muxer notification | |
1175 | * iterator's list of newly connected ports. They will be | |
1176 | * handled by muxer_notif_iter_handle_newly_connected_ports(). | |
1177 | */ | |
5c563278 | 1178 | comp = bt_component_borrow_from_private(muxer_comp->priv_comp); |
f6ccaed9 | 1179 | BT_ASSERT(comp); |
544d0515 PP |
1180 | count = bt_component_filter_get_input_port_count(comp); |
1181 | if (count < 0) { | |
fed72692 PP |
1182 | BT_LOGD("No input port to initialize for muxer component's notification iterator: " |
1183 | "muxer-comp-addr=%p, muxer-notif-iter-addr=%p", | |
1184 | muxer_comp, muxer_notif_iter); | |
ab11110e PP |
1185 | goto end; |
1186 | } | |
958f7d11 PP |
1187 | |
1188 | for (i = 0; i < count; i++) { | |
1189 | struct bt_private_port *priv_port = | |
aa4d5da5 | 1190 | bt_private_component_filter_get_input_port_by_index( |
958f7d11 PP |
1191 | muxer_comp->priv_comp, i); |
1192 | struct bt_port *port; | |
958f7d11 | 1193 | |
f6ccaed9 | 1194 | BT_ASSERT(priv_port); |
5c563278 | 1195 | port = bt_port_borrow_from_private(priv_port); |
f6ccaed9 | 1196 | BT_ASSERT(port); |
958f7d11 PP |
1197 | |
1198 | if (!bt_port_is_connected(port)) { | |
fed72692 PP |
1199 | BT_LOGD("Skipping input port: not connected: " |
1200 | "muxer-comp-addr=%p, port-addr=%p, port-name\"%s\"", | |
1201 | muxer_comp, port, bt_port_get_name(port)); | |
e2317362 | 1202 | bt_object_put_ref(priv_port); |
958f7d11 PP |
1203 | continue; |
1204 | } | |
1205 | ||
e2317362 | 1206 | bt_object_put_ref(priv_port); |
ab11110e PP |
1207 | muxer_notif_iter->newly_connected_priv_ports = |
1208 | g_list_append( | |
1209 | muxer_notif_iter->newly_connected_priv_ports, | |
958f7d11 | 1210 | priv_port); |
ab11110e | 1211 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
fed72692 PP |
1212 | BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: " |
1213 | "port-addr=%p, port-name=\"%s\", " | |
1214 | "muxer-notif-iter-addr=%p", port, | |
1215 | bt_port_get_name(port), muxer_notif_iter); | |
ab11110e PP |
1216 | ret = -1; |
1217 | goto end; | |
958f7d11 | 1218 | } |
fed72692 PP |
1219 | |
1220 | BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: " | |
1221 | "port-addr=%p, port-name=\"%s\", " | |
1222 | "muxer-notif-iter-addr=%p", port, | |
1223 | bt_port_get_name(port), muxer_notif_iter); | |
958f7d11 PP |
1224 | } |
1225 | ||
1226 | end: | |
958f7d11 PP |
1227 | return ret; |
1228 | } | |
1229 | ||
958f7d11 PP |
1230 | BT_HIDDEN |
1231 | enum bt_notification_iterator_status muxer_notif_iter_init( | |
90157d89 | 1232 | struct bt_private_connection_private_notification_iterator *priv_notif_iter, |
958f7d11 PP |
1233 | struct bt_private_port *output_priv_port) |
1234 | { | |
1235 | struct muxer_comp *muxer_comp = NULL; | |
1236 | struct muxer_notif_iter *muxer_notif_iter = NULL; | |
1237 | struct bt_private_component *priv_comp = NULL; | |
1238 | enum bt_notification_iterator_status status = | |
1239 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
1240 | int ret; | |
1241 | ||
90157d89 | 1242 | priv_comp = bt_private_connection_private_notification_iterator_get_private_component( |
958f7d11 | 1243 | priv_notif_iter); |
f6ccaed9 | 1244 | BT_ASSERT(priv_comp); |
958f7d11 | 1245 | muxer_comp = bt_private_component_get_user_data(priv_comp); |
f6ccaed9 | 1246 | BT_ASSERT(muxer_comp); |
fed72692 PP |
1247 | BT_LOGD("Initializing muxer component's notification iterator: " |
1248 | "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p", | |
1249 | priv_comp, muxer_comp, priv_notif_iter); | |
a09c6b95 PP |
1250 | |
1251 | if (muxer_comp->initializing_muxer_notif_iter) { | |
1252 | /* | |
089717de PP |
1253 | * Weird, unhandled situation detected: downstream |
1254 | * creates a muxer notification iterator while creating | |
1255 | * another muxer notification iterator (same component). | |
a09c6b95 | 1256 | */ |
fed72692 PP |
1257 | BT_LOGE("Recursive initialization of muxer component's notification iterator: " |
1258 | "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p", | |
1259 | priv_comp, muxer_comp, priv_notif_iter); | |
958f7d11 PP |
1260 | goto error; |
1261 | } | |
1262 | ||
a09c6b95 PP |
1263 | muxer_comp->initializing_muxer_notif_iter = true; |
1264 | muxer_notif_iter = g_new0(struct muxer_notif_iter, 1); | |
1265 | if (!muxer_notif_iter) { | |
fed72692 | 1266 | BT_LOGE_STR("Failed to allocate one muxer component's notification iterator."); |
ab11110e PP |
1267 | goto error; |
1268 | } | |
1269 | ||
958f7d11 PP |
1270 | muxer_notif_iter->last_returned_ts_ns = INT64_MIN; |
1271 | muxer_notif_iter->muxer_upstream_notif_iters = | |
1272 | g_ptr_array_new_with_free_func( | |
1273 | (GDestroyNotify) destroy_muxer_upstream_notif_iter); | |
1274 | if (!muxer_notif_iter->muxer_upstream_notif_iters) { | |
fed72692 | 1275 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
1276 | goto error; |
1277 | } | |
1278 | ||
a09c6b95 PP |
1279 | /* |
1280 | * Add the muxer notification iterator to the component's array | |
1281 | * of muxer notification iterators here because | |
1282 | * muxer_notif_iter_init_newly_connected_ports() can cause | |
1283 | * muxer_port_connected() to be called, which adds the newly | |
1284 | * connected port to each muxer notification iterator's list of | |
1285 | * newly connected ports. | |
1286 | */ | |
1287 | g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter); | |
1288 | ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp, | |
1289 | muxer_notif_iter); | |
1290 | if (ret) { | |
fed72692 PP |
1291 | BT_LOGE("Cannot initialize newly connected input ports for muxer component's notification iterator: " |
1292 | "comp-addr=%p, muxer-comp-addr=%p, " | |
1293 | "muxer-notif-iter-addr=%p, notif-iter-addr=%p, ret=%d", | |
1294 | priv_comp, muxer_comp, muxer_notif_iter, | |
1295 | priv_notif_iter, ret); | |
a09c6b95 PP |
1296 | goto error; |
1297 | } | |
1298 | ||
90157d89 | 1299 | ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter, |
958f7d11 | 1300 | muxer_notif_iter); |
f6ccaed9 | 1301 | BT_ASSERT(ret == 0); |
fed72692 PP |
1302 | BT_LOGD("Initialized muxer component's notification iterator: " |
1303 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1304 | "notif-iter-addr=%p", | |
1305 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
958f7d11 PP |
1306 | goto end; |
1307 | ||
1308 | error: | |
a09c6b95 PP |
1309 | if (g_ptr_array_index(muxer_comp->muxer_notif_iters, |
1310 | muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) { | |
1311 | g_ptr_array_remove_index(muxer_comp->muxer_notif_iters, | |
1312 | muxer_comp->muxer_notif_iters->len - 1); | |
1313 | } | |
1314 | ||
958f7d11 | 1315 | destroy_muxer_notif_iter(muxer_notif_iter); |
90157d89 | 1316 | ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter, |
958f7d11 | 1317 | NULL); |
f6ccaed9 | 1318 | BT_ASSERT(ret == 0); |
958f7d11 PP |
1319 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
1320 | ||
1321 | end: | |
a09c6b95 | 1322 | muxer_comp->initializing_muxer_notif_iter = false; |
e2317362 | 1323 | bt_object_put_ref(priv_comp); |
958f7d11 PP |
1324 | return status; |
1325 | } | |
1326 | ||
1327 | BT_HIDDEN | |
1328 | void muxer_notif_iter_finalize( | |
90157d89 | 1329 | struct bt_private_connection_private_notification_iterator *priv_notif_iter) |
958f7d11 PP |
1330 | { |
1331 | struct muxer_notif_iter *muxer_notif_iter = | |
90157d89 | 1332 | bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter); |
958f7d11 PP |
1333 | struct bt_private_component *priv_comp = NULL; |
1334 | struct muxer_comp *muxer_comp = NULL; | |
1335 | ||
90157d89 | 1336 | priv_comp = bt_private_connection_private_notification_iterator_get_private_component( |
958f7d11 | 1337 | priv_notif_iter); |
f6ccaed9 | 1338 | BT_ASSERT(priv_comp); |
958f7d11 | 1339 | muxer_comp = bt_private_component_get_user_data(priv_comp); |
fed72692 PP |
1340 | BT_LOGD("Finalizing muxer component's notification iterator: " |
1341 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1342 | "notif-iter-addr=%p", | |
1343 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
958f7d11 PP |
1344 | |
1345 | if (muxer_comp) { | |
1346 | (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters, | |
1347 | muxer_notif_iter); | |
1348 | destroy_muxer_notif_iter(muxer_notif_iter); | |
1349 | } | |
1350 | ||
e2317362 | 1351 | bt_object_put_ref(priv_comp); |
958f7d11 PP |
1352 | } |
1353 | ||
1354 | BT_HIDDEN | |
d4393e08 PP |
1355 | enum bt_notification_iterator_status muxer_notif_iter_next( |
1356 | struct bt_private_connection_private_notification_iterator *priv_notif_iter, | |
1357 | bt_notification_array notifs, uint64_t capacity, | |
1358 | uint64_t *count) | |
958f7d11 | 1359 | { |
d4393e08 | 1360 | enum bt_notification_iterator_status status; |
958f7d11 | 1361 | struct muxer_notif_iter *muxer_notif_iter = |
90157d89 | 1362 | bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter); |
958f7d11 PP |
1363 | struct bt_private_component *priv_comp = NULL; |
1364 | struct muxer_comp *muxer_comp = NULL; | |
958f7d11 | 1365 | |
f6ccaed9 | 1366 | BT_ASSERT(muxer_notif_iter); |
90157d89 | 1367 | priv_comp = bt_private_connection_private_notification_iterator_get_private_component( |
958f7d11 | 1368 | priv_notif_iter); |
f6ccaed9 | 1369 | BT_ASSERT(priv_comp); |
958f7d11 | 1370 | muxer_comp = bt_private_component_get_user_data(priv_comp); |
f6ccaed9 | 1371 | BT_ASSERT(muxer_comp); |
fed72692 PP |
1372 | BT_LOGV("Muxer component's notification iterator's \"next\" method called: " |
1373 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1374 | "notif-iter-addr=%p", | |
1375 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter); | |
1376 | ||
d4393e08 PP |
1377 | status = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter, |
1378 | notifs, capacity, count); | |
1379 | if (status < 0) { | |
fed72692 PP |
1380 | BT_LOGE("Cannot get next notification: " |
1381 | "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, " | |
1382 | "notif-iter-addr=%p, status=%s", | |
1383 | priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter, | |
d4393e08 | 1384 | bt_notification_iterator_status_string(status)); |
fed72692 PP |
1385 | } else { |
1386 | BT_LOGV("Returning from muxer component's notification iterator's \"next\" method: " | |
d4393e08 PP |
1387 | "status=%s", |
1388 | bt_notification_iterator_status_string(status)); | |
fed72692 | 1389 | } |
958f7d11 | 1390 | |
e2317362 | 1391 | bt_object_put_ref(priv_comp); |
d4393e08 | 1392 | return status; |
958f7d11 PP |
1393 | } |
1394 | ||
1395 | BT_HIDDEN | |
bf55043c | 1396 | enum bt_component_status muxer_port_connected( |
958f7d11 PP |
1397 | struct bt_private_component *priv_comp, |
1398 | struct bt_private_port *self_private_port, | |
1399 | struct bt_port *other_port) | |
1400 | { | |
bf55043c | 1401 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
958f7d11 | 1402 | struct bt_port *self_port = |
5c563278 | 1403 | bt_port_borrow_from_private(self_private_port); |
958f7d11 PP |
1404 | struct muxer_comp *muxer_comp = |
1405 | bt_private_component_get_user_data(priv_comp); | |
1406 | size_t i; | |
06a2cb0d | 1407 | int ret; |
958f7d11 | 1408 | |
f6ccaed9 PP |
1409 | BT_ASSERT(self_port); |
1410 | BT_ASSERT(muxer_comp); | |
fed72692 PP |
1411 | BT_LOGD("Port connected: " |
1412 | "comp-addr=%p, muxer-comp-addr=%p, " | |
1413 | "port-addr=%p, port-name=\"%s\", " | |
1414 | "other-port-addr=%p, other-port-name=\"%s\"", | |
1415 | priv_comp, muxer_comp, self_port, bt_port_get_name(self_port), | |
1416 | other_port, bt_port_get_name(other_port)); | |
958f7d11 | 1417 | |
06a2cb0d PP |
1418 | if (bt_port_get_type(self_port) == BT_PORT_TYPE_OUTPUT) { |
1419 | goto end; | |
958f7d11 PP |
1420 | } |
1421 | ||
1422 | for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) { | |
1423 | struct muxer_notif_iter *muxer_notif_iter = | |
1424 | g_ptr_array_index(muxer_comp->muxer_notif_iters, i); | |
1425 | ||
1426 | /* | |
ab11110e PP |
1427 | * Add this port to the list of newly connected ports |
1428 | * for this muxer notification iterator. We append at | |
1429 | * the end of this list while | |
1430 | * muxer_notif_iter_handle_newly_connected_ports() | |
1431 | * removes the nodes from the beginning. | |
958f7d11 | 1432 | */ |
ab11110e PP |
1433 | muxer_notif_iter->newly_connected_priv_ports = |
1434 | g_list_append( | |
1435 | muxer_notif_iter->newly_connected_priv_ports, | |
06a2cb0d | 1436 | self_private_port); |
ab11110e | 1437 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
fed72692 PP |
1438 | BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: " |
1439 | "port-addr=%p, port-name=\"%s\", " | |
1440 | "muxer-notif-iter-addr=%p", self_port, | |
1441 | bt_port_get_name(self_port), muxer_notif_iter); | |
bf55043c | 1442 | status = BT_COMPONENT_STATUS_ERROR; |
958f7d11 PP |
1443 | goto end; |
1444 | } | |
fed72692 PP |
1445 | |
1446 | BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: " | |
1447 | "port-addr=%p, port-name=\"%s\", " | |
1448 | "muxer-notif-iter-addr=%p", self_port, | |
1449 | bt_port_get_name(self_port), muxer_notif_iter); | |
958f7d11 PP |
1450 | } |
1451 | ||
06a2cb0d PP |
1452 | /* One less available input port */ |
1453 | muxer_comp->available_input_ports--; | |
1454 | ret = ensure_available_input_port(priv_comp); | |
1455 | if (ret) { | |
1456 | /* | |
1457 | * Only way to report an error later since this | |
1458 | * method does not return anything. | |
1459 | */ | |
fed72692 PP |
1460 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
1461 | "muxer-comp-addr=%p, status=%s", | |
1462 | muxer_comp, bt_component_status_string(ret)); | |
bf55043c | 1463 | status = BT_COMPONENT_STATUS_ERROR; |
06a2cb0d PP |
1464 | goto end; |
1465 | } | |
1466 | ||
958f7d11 | 1467 | end: |
bf55043c | 1468 | return status; |
958f7d11 PP |
1469 | } |
1470 | ||
1471 | BT_HIDDEN | |
1472 | void muxer_port_disconnected(struct bt_private_component *priv_comp, | |
1473 | struct bt_private_port *priv_port) | |
1474 | { | |
5c563278 | 1475 | struct bt_port *port = bt_port_borrow_from_private(priv_port); |
958f7d11 PP |
1476 | struct muxer_comp *muxer_comp = |
1477 | bt_private_component_get_user_data(priv_comp); | |
1478 | ||
f6ccaed9 PP |
1479 | BT_ASSERT(port); |
1480 | BT_ASSERT(muxer_comp); | |
fed72692 PP |
1481 | BT_LOGD("Port disconnected: " |
1482 | "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, " | |
1483 | "port-name=\"%s\"", priv_comp, muxer_comp, | |
1484 | port, bt_port_get_name(port)); | |
958f7d11 | 1485 | |
ab11110e PP |
1486 | /* |
1487 | * There's nothing special to do when a port is disconnected | |
1488 | * because this component deals with upstream notification | |
1489 | * iterators which were already created thanks to connected | |
1490 | * ports. The fact that the port is disconnected does not cancel | |
1491 | * the upstream notification iterators created using its | |
089717de PP |
1492 | * connection: they still exist, even if the connection is dead. |
1493 | * The only way to remove an upstream notification iterator is | |
1494 | * for its "next" operation to return | |
1495 | * BT_NOTIFICATION_ITERATOR_STATUS_END. | |
ab11110e | 1496 | */ |
958f7d11 PP |
1497 | if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) { |
1498 | /* One more available input port */ | |
1499 | muxer_comp->available_input_ports++; | |
fed72692 PP |
1500 | BT_LOGD("Leaving disconnected input port available for future connections: " |
1501 | "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, " | |
1502 | "port-name=\"%s\", avail-input-port-count=%zu", | |
1503 | priv_comp, muxer_comp, port, bt_port_get_name(port), | |
1504 | muxer_comp->available_input_ports); | |
958f7d11 | 1505 | } |
958f7d11 | 1506 | } |