Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032
JG
1/*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
5af447e5
PP
28#define BT_LOG_TAG "NOTIF-ITER"
29#include <babeltrace/lib-logging-internal.h>
30
3d9990ac 31#include <babeltrace/compiler-internal.h>
b8a06801 32#include <babeltrace/ref.h>
2ec84d26
PP
33#include <babeltrace/ctf-ir/fields.h>
34#include <babeltrace/ctf-ir/field-types.h>
35#include <babeltrace/ctf-ir/field-types-internal.h>
3230ee6b
PP
36#include <babeltrace/ctf-ir/event-internal.h>
37#include <babeltrace/ctf-ir/packet-internal.h>
38#include <babeltrace/ctf-ir/stream-internal.h>
73d5c1ad 39#include <babeltrace/graph/connection.h>
bd14d768 40#include <babeltrace/graph/connection-internal.h>
b2e0c907
PP
41#include <babeltrace/graph/component.h>
42#include <babeltrace/graph/component-source-internal.h>
43#include <babeltrace/graph/component-class-internal.h>
8ed535b5
PP
44#include <babeltrace/graph/component-class-sink-colander-internal.h>
45#include <babeltrace/graph/component-sink.h>
fa054faf 46#include <babeltrace/graph/notification.h>
b2e0c907
PP
47#include <babeltrace/graph/notification-iterator.h>
48#include <babeltrace/graph/notification-iterator-internal.h>
e7fa96c3 49#include <babeltrace/graph/notification-internal.h>
3230ee6b
PP
50#include <babeltrace/graph/notification-event.h>
51#include <babeltrace/graph/notification-event-internal.h>
52#include <babeltrace/graph/notification-packet.h>
53#include <babeltrace/graph/notification-packet-internal.h>
54#include <babeltrace/graph/notification-stream.h>
55#include <babeltrace/graph/notification-stream-internal.h>
2ec84d26 56#include <babeltrace/graph/notification-discarded-elements-internal.h>
3230ee6b 57#include <babeltrace/graph/port.h>
8ed535b5 58#include <babeltrace/graph/graph-internal.h>
c55a9f58 59#include <babeltrace/types.h>
f6ccaed9 60#include <babeltrace/assert-internal.h>
f42867e2 61#include <babeltrace/assert-pre-internal.h>
fa054faf 62#include <stdint.h>
2ec84d26 63#include <inttypes.h>
0fbb9a9f 64#include <stdlib.h>
3230ee6b 65
2ec84d26 66struct discarded_elements_state {
50842bdc 67 struct bt_clock_value *cur_begin;
2ec84d26
PP
68 uint64_t cur_count;
69};
70
3230ee6b 71struct stream_state {
50842bdc
PP
72 struct bt_stream *stream; /* owned by this */
73 struct bt_packet *cur_packet; /* owned by this */
2ec84d26
PP
74 struct discarded_elements_state discarded_packets_state;
75 struct discarded_elements_state discarded_events_state;
f42867e2 76 uint64_t expected_notif_seq_num;
c55a9f58 77 bt_bool is_ended;
3230ee6b
PP
78};
79
3230ee6b 80static
3dca2276 81void stream_destroy_listener(struct bt_stream_common *stream, void *data)
3230ee6b 82{
90157d89 83 struct bt_notification_iterator_private_connection *iterator = data;
3230ee6b
PP
84
85 /* Remove associated stream state */
86 g_hash_table_remove(iterator->stream_states, stream);
87}
88
89static
90void destroy_stream_state(struct stream_state *stream_state)
91{
92 if (!stream_state) {
93 return;
94 }
95
5af447e5
PP
96 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
97 BT_LOGV_STR("Putting stream state's current packet.");
3230ee6b 98 bt_put(stream_state->cur_packet);
5af447e5 99 BT_LOGV_STR("Putting stream state's stream.");
3230ee6b 100 bt_put(stream_state->stream);
2ec84d26
PP
101 bt_put(stream_state->discarded_packets_state.cur_begin);
102 bt_put(stream_state->discarded_events_state.cur_begin);
3230ee6b
PP
103 g_free(stream_state);
104}
105
3230ee6b 106static
50842bdc 107struct stream_state *create_stream_state(struct bt_stream *stream)
3230ee6b
PP
108{
109 struct stream_state *stream_state = g_new0(struct stream_state, 1);
110
111 if (!stream_state) {
5af447e5 112 BT_LOGE_STR("Failed to allocate one stream state.");
3230ee6b
PP
113 goto end;
114 }
115
126215b4
MD
116 /*
117 * The packet index is a monotonic counter which may not start
118 * at 0 at the beginning of the stream. We therefore need to
119 * have an internal object initial state of -1ULL to distinguish
120 * between initial state and having seen a packet with
f42867e2 121 * the sequence number 0.
126215b4
MD
122 */
123 stream_state->discarded_packets_state.cur_count = -1ULL;
124
3230ee6b 125 /*
f42867e2 126 * We keep a reference to the stream until we know it's ended.
3230ee6b
PP
127 */
128 stream_state->stream = bt_get(stream);
5af447e5
PP
129 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
130 "stream-state-addr=%p",
50842bdc 131 stream, bt_stream_get_name(stream), stream_state);
3230ee6b
PP
132
133end:
134 return stream_state;
135}
47e5a032 136
8ed535b5
PP
137static
138void destroy_base_notification_iterator(struct bt_object *obj)
139{
140 struct bt_notification_iterator *iterator =
141 container_of(obj, struct bt_notification_iterator, base);
142
143 BT_LOGD_STR("Putting current notification.");
144 bt_put(iterator->current_notification);
145 g_free(iterator);
146}
147
47e5a032 148static
90157d89 149void bt_private_connection_notification_iterator_destroy(struct bt_object *obj)
47e5a032 150{
90157d89 151 struct bt_notification_iterator_private_connection *iterator;
8738a040 152
f6ccaed9 153 BT_ASSERT(obj);
d3eb6e8f 154
bd14d768
PP
155 /*
156 * The notification iterator's reference count is 0 if we're
157 * here. Increment it to avoid a double-destroy (possibly
158 * infinitely recursive). This could happen for example if the
159 * notification iterator's finalization function does bt_get()
160 * (or anything that causes bt_get() to be called) on itself
161 * (ref. count goes from 0 to 1), and then bt_put(): the
162 * reference count would go from 1 to 0 again and this function
163 * would be called again.
164 */
165 obj->ref_count.count++;
90157d89 166 iterator = (void *) container_of(obj, struct bt_notification_iterator, base);
8ed535b5 167 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
5af447e5 168 iterator);
90157d89 169 bt_private_connection_notification_iterator_finalize(iterator);
d3eb6e8f 170
3230ee6b
PP
171 if (iterator->stream_states) {
172 /*
173 * Remove our destroy listener from each stream which
174 * has a state in this iterator. Otherwise the destroy
175 * listener would be called with an invalid/other
176 * notification iterator object.
177 */
178 GHashTableIter ht_iter;
179 gpointer stream_gptr, stream_state_gptr;
180
181 g_hash_table_iter_init(&ht_iter, iterator->stream_states);
182
183 while (g_hash_table_iter_next(&ht_iter, &stream_gptr, &stream_state_gptr)) {
f6ccaed9 184 BT_ASSERT(stream_gptr);
5af447e5
PP
185
186 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
3dca2276 187 bt_stream_common_remove_destroy_listener(
3230ee6b
PP
188 (void *) stream_gptr, stream_destroy_listener,
189 iterator);
190 }
191
192 g_hash_table_destroy(iterator->stream_states);
193 }
194
bd14d768
PP
195 if (iterator->connection) {
196 /*
197 * Remove ourself from the originating connection so
198 * that it does not try to finalize a dangling pointer
199 * later.
200 */
201 bt_connection_remove_iterator(iterator->connection, iterator);
202 }
203
8ed535b5 204 destroy_base_notification_iterator(obj);
47e5a032
JG
205}
206
bd14d768 207BT_HIDDEN
90157d89
PP
208void bt_private_connection_notification_iterator_finalize(
209 struct bt_notification_iterator_private_connection *iterator)
bd14d768
PP
210{
211 struct bt_component_class *comp_class = NULL;
212 bt_component_class_notification_iterator_finalize_method
213 finalize_method = NULL;
214
f6ccaed9 215 BT_ASSERT(iterator);
bd14d768
PP
216
217 switch (iterator->state) {
90157d89 218 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
088d4023
PP
219 /* Skip user finalization if user initialization failed */
220 BT_LOGD("Not finalizing non-initialized notification iterator: "
221 "addr=%p", iterator);
222 return;
90157d89
PP
223 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
224 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
bd14d768 225 /* Already finalized */
5af447e5
PP
226 BT_LOGD("Not finalizing notification iterator: already finalized: "
227 "addr=%p", iterator);
bd14d768
PP
228 return;
229 default:
230 break;
231 }
232
5af447e5
PP
233 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
234
90157d89 235 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) {
5af447e5 236 BT_LOGD("Updating notification iterator's state: "
90157d89
PP
237 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
238 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
df14f8af 239 } else {
5af447e5 240 BT_LOGD("Updating notification iterator's state: "
90157d89
PP
241 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
242 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED;
df14f8af
MD
243 }
244
f6ccaed9 245 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
246 comp_class = iterator->upstream_component->class;
247
248 /* Call user-defined destroy method */
249 switch (comp_class->type) {
250 case BT_COMPONENT_CLASS_TYPE_SOURCE:
251 {
252 struct bt_component_class_source *source_class;
253
254 source_class = container_of(comp_class, struct bt_component_class_source, parent);
255 finalize_method = source_class->methods.iterator.finalize;
256 break;
257 }
258 case BT_COMPONENT_CLASS_TYPE_FILTER:
259 {
260 struct bt_component_class_filter *filter_class;
261
262 filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
263 finalize_method = filter_class->methods.iterator.finalize;
264 break;
265 }
266 default:
267 /* Unreachable */
0fbb9a9f 268 abort();
bd14d768
PP
269 }
270
271 if (finalize_method) {
5af447e5
PP
272 BT_LOGD("Calling user's finalization method: addr=%p",
273 iterator);
bd14d768 274 finalize_method(
90157d89 275 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator));
bd14d768
PP
276 }
277
bd14d768
PP
278 iterator->upstream_component = NULL;
279 iterator->upstream_port = NULL;
5af447e5 280 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
bd14d768
PP
281}
282
283BT_HIDDEN
90157d89
PP
284void bt_private_connection_notification_iterator_set_connection(
285 struct bt_notification_iterator_private_connection *iterator,
bd14d768
PP
286 struct bt_connection *connection)
287{
f6ccaed9 288 BT_ASSERT(iterator);
bd14d768 289 iterator->connection = connection;
5af447e5
PP
290 BT_LOGV("Set notification iterator's connection: "
291 "iter-addr=%p, conn-addr=%p", iterator, connection);
bd14d768
PP
292}
293
90157d89
PP
294static
295void init_notification_iterator(struct bt_notification_iterator *iterator,
296 enum bt_notification_iterator_type type,
297 bt_object_release_func destroy)
298{
299 bt_object_init(iterator, destroy);
300 iterator->type = type;
301}
302
47e5a032 303BT_HIDDEN
90157d89 304enum bt_connection_status bt_private_connection_notification_iterator_create(
3230ee6b 305 struct bt_component *upstream_comp,
fa054faf 306 struct bt_port *upstream_port,
73d5c1ad 307 struct bt_connection *connection,
90157d89 308 struct bt_notification_iterator_private_connection **user_iterator)
47e5a032 309{
73d5c1ad 310 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
d3e4dcd8 311 enum bt_component_class_type type;
90157d89 312 struct bt_notification_iterator_private_connection *iterator = NULL;
47e5a032 313
f6ccaed9
PP
314 BT_ASSERT(upstream_comp);
315 BT_ASSERT(upstream_port);
f6ccaed9
PP
316 BT_ASSERT(bt_port_is_connected(upstream_port));
317 BT_ASSERT(user_iterator);
8ed535b5 318 BT_LOGD("Creating notification iterator on private connection: "
5af447e5
PP
319 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
320 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
321 "conn-addr=%p",
322 upstream_comp, bt_component_get_name(upstream_comp),
323 upstream_port, bt_port_get_name(upstream_port),
324 connection);
3230ee6b 325 type = bt_component_get_class_type(upstream_comp);
f6ccaed9 326 BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
ef2f7566 327 type == BT_COMPONENT_CLASS_TYPE_FILTER);
90157d89 328 iterator = g_new0(struct bt_notification_iterator_private_connection, 1);
47e5a032 329 if (!iterator) {
8ed535b5 330 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
73d5c1ad
PP
331 status = BT_CONNECTION_STATUS_NOMEM;
332 goto end;
47e5a032
JG
333 }
334
90157d89
PP
335 init_notification_iterator((void *) iterator,
336 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
337 bt_private_connection_notification_iterator_destroy);
3230ee6b
PP
338
339 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
340 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
341 if (!iterator->stream_states) {
5af447e5 342 BT_LOGE_STR("Failed to allocate a GHashTable.");
73d5c1ad
PP
343 status = BT_CONNECTION_STATUS_NOMEM;
344 goto end;
3230ee6b
PP
345 }
346
bd14d768
PP
347 iterator->upstream_component = upstream_comp;
348 iterator->upstream_port = upstream_port;
349 iterator->connection = connection;
90157d89 350 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
5af447e5
PP
351 BT_LOGD("Created notification iterator: "
352 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
353 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
354 "conn-addr=%p, iter-addr=%p",
355 upstream_comp, bt_component_get_name(upstream_comp),
356 upstream_port, bt_port_get_name(upstream_port),
357 connection, iterator);
1a6a376a
PP
358
359 /* Move reference to user */
360 *user_iterator = iterator;
361 iterator = NULL;
3230ee6b 362
47e5a032 363end:
73d5c1ad
PP
364 bt_put(iterator);
365 return status;
47e5a032
JG
366}
367
90157d89
PP
368void *bt_private_connection_private_notification_iterator_get_user_data(
369 struct bt_private_connection_private_notification_iterator *private_iterator)
ea8d3e58 370{
90157d89 371 struct bt_notification_iterator_private_connection *iterator =
6d137876 372 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
890882ef 373
f42867e2
PP
374 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
375 return iterator->user_data;
ea8d3e58
JG
376}
377
378enum bt_notification_iterator_status
90157d89
PP
379bt_private_connection_private_notification_iterator_set_user_data(
380 struct bt_private_connection_private_notification_iterator *private_iterator,
890882ef 381 void *data)
ea8d3e58 382{
90157d89 383 struct bt_notification_iterator_private_connection *iterator =
6d137876 384 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
ea8d3e58 385
f42867e2 386 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
ea8d3e58 387 iterator->user_data = data;
5af447e5
PP
388 BT_LOGV("Set notification iterator's user data: "
389 "iter-addr=%p, user-data-addr=%p", iterator, data);
f42867e2 390 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
8738a040 391}
413bc2c4 392
53d45b87
JG
393struct bt_notification *bt_notification_iterator_get_notification(
394 struct bt_notification_iterator *iterator)
395{
f42867e2
PP
396 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
397 return bt_get(
8ed535b5 398 bt_notification_iterator_borrow_current_notification(iterator));
53d45b87
JG
399}
400
f42867e2
PP
401BT_ASSERT_PRE_FUNC
402static inline
403void bt_notification_borrow_packet_stream(struct bt_notification *notif,
404 struct bt_stream **stream, struct bt_packet **packet)
fa054faf 405{
f42867e2 406 BT_ASSERT(notif);
fa054faf 407
f42867e2 408 switch (notif->type) {
fa054faf 409 case BT_NOTIFICATION_TYPE_EVENT:
f42867e2
PP
410 *packet = bt_event_borrow_packet(
411 bt_notification_event_borrow_event(notif));
412 *stream = bt_packet_borrow_stream(*packet);
fa054faf
PP
413 break;
414 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
f42867e2 415 *stream = bt_notification_stream_begin_borrow_stream(notif);
fa054faf
PP
416 break;
417 case BT_NOTIFICATION_TYPE_STREAM_END:
f42867e2 418 *stream = bt_notification_stream_end_borrow_stream(notif);
fa054faf
PP
419 break;
420 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
f42867e2
PP
421 *packet = bt_notification_packet_begin_borrow_packet(notif);
422 *stream = bt_packet_borrow_stream(*packet);
fa054faf
PP
423 break;
424 case BT_NOTIFICATION_TYPE_PACKET_END:
f42867e2
PP
425 *packet = bt_notification_packet_end_borrow_packet(notif);
426 *stream = bt_packet_borrow_stream(*packet);
2ec84d26 427 break;
fa054faf 428 default:
f42867e2 429 break;
fa054faf 430 }
fa054faf
PP
431}
432
f42867e2
PP
433BT_ASSERT_PRE_FUNC
434static inline
435bool validate_notification(
90157d89 436 struct bt_notification_iterator_private_connection *iterator,
f42867e2 437 struct bt_notification *notif)
3230ee6b 438{
f42867e2 439 bool is_valid = true;
3230ee6b 440 struct stream_state *stream_state;
f42867e2
PP
441 struct bt_stream *stream = NULL;
442 struct bt_packet *packet = NULL;
443
444 BT_ASSERT(notif);
445 bt_notification_borrow_packet_stream(notif, &stream, &packet);
446
447 if (!stream) {
448 /* we don't care about notifications not attached to streams */
449 goto end;
450 }
3230ee6b 451
f42867e2
PP
452 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
453 if (!stream_state) {
3230ee6b 454 /*
f42867e2
PP
455 * No stream state for this stream: this notification
456 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
457 * and its sequence number must be 0.
3230ee6b 458 */
f42867e2
PP
459 if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
460 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
461 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
462 "notification prior to this notification: "
463 "%![stream-]+s", stream);
464 is_valid = false;
3230ee6b
PP
465 goto end;
466 }
467
f42867e2
PP
468 if (notif->seq_num == -1ULL) {
469 notif->seq_num = 0;
3230ee6b
PP
470 }
471
f42867e2
PP
472 if (notif->seq_num != 0) {
473 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
474 "number for this notification iterator: "
475 "this is the first notification for this "
476 "stream, expecting sequence number 0: "
477 "seq-num=%" PRIu64 ", %![stream-]+s",
478 notif->seq_num, stream);
479 is_valid = false;
3230ee6b 480 goto end;
3230ee6b 481 }
3230ee6b 482
f42867e2
PP
483 stream_state = create_stream_state(stream);
484 if (!stream_state) {
485 abort();
486 }
fa054faf 487
f42867e2
PP
488 g_hash_table_insert(iterator->stream_states, stream,
489 stream_state);
490 stream_state->expected_notif_seq_num++;
491 goto end;
fa054faf
PP
492 }
493
f42867e2
PP
494 if (stream_state->is_ended) {
495 /*
496 * There's a new notification which has a reference to a
497 * stream which, from this iterator's point of view, is
498 * ended ("end of stream" notification was returned).
499 * This is bad: the API guarantees that it can never
500 * happen.
501 */
502 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
503 stream);
504 is_valid = false;
fa054faf
PP
505 goto end;
506 }
507
f42867e2
PP
508 if (notif->seq_num == -1ULL) {
509 notif->seq_num = stream_state->expected_notif_seq_num;
3230ee6b
PP
510 }
511
f42867e2
PP
512 if (notif->seq_num != -1ULL &&
513 notif->seq_num != stream_state->expected_notif_seq_num) {
514 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
515 "seq-num=%" PRIu64 ", "
516 "expected-seq-num=%" PRIu64 ", %![stream-]+s",
517 notif->seq_num, stream_state->expected_notif_seq_num,
518 stream);
519 is_valid = false;
fa054faf
PP
520 goto end;
521 }
522
f42867e2
PP
523 switch (notif->type) {
524 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
525 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
526 "notification at this point: notif-seq-num=%" PRIu64 ", "
527 "%![stream-]+s", notif->seq_num, stream);
528 is_valid = false;
529 goto end;
530 case BT_NOTIFICATION_TYPE_STREAM_END:
531 if (stream_state->cur_packet) {
532 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
533 "notification: missing a "
534 "BT_NOTIFICATION_TYPE_PACKET_END notification "
535 "prior to this notification: "
536 "notif-seq-num=%" PRIu64 ", "
537 "%![stream-]+s", notif->seq_num, stream);
538 is_valid = false;
539 goto end;
540 }
541 stream_state->expected_notif_seq_num++;
542 stream_state->is_ended = true;
543 goto end;
544 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
545 if (stream_state->cur_packet) {
546 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
547 "notification at this point: missing a "
548 "BT_NOTIFICATION_TYPE_PACKET_END notification "
549 "prior to this notification: "
550 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
551 "%![packet-]+a", notif->seq_num, stream,
552 packet);
553 is_valid = false;
554 goto end;
555 }
556 stream_state->expected_notif_seq_num++;
557 stream_state->cur_packet = bt_get(packet);
558 goto end;
559 case BT_NOTIFICATION_TYPE_PACKET_END:
560 if (!stream_state->cur_packet) {
561 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
562 "notification at this point: missing a "
563 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
564 "prior to this notification: "
565 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
566 "%![packet-]+a", notif->seq_num, stream,
567 packet);
568 is_valid = false;
569 goto end;
570 }
571 stream_state->expected_notif_seq_num++;
572 BT_PUT(stream_state->cur_packet);
573 goto end;
574 case BT_NOTIFICATION_TYPE_EVENT:
575 if (packet != stream_state->cur_packet) {
576 BT_ASSERT_PRE_MSG("Unexpected packet for "
577 "BT_NOTIFICATION_TYPE_EVENT notification: "
578 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
579 "%![notif-packet-]+a, %![expected-packet-]+a",
580 notif->seq_num, stream,
581 stream_state->cur_packet, packet);
582 is_valid = false;
583 goto end;
584 }
585 stream_state->expected_notif_seq_num++;
586 goto end;
587 default:
588 break;
3230ee6b
PP
589 }
590
3230ee6b 591end:
f42867e2 592 return is_valid;
3230ee6b
PP
593}
594
f42867e2
PP
595BT_ASSERT_PRE_FUNC
596static inline bool priv_conn_notif_iter_can_end(
597 struct bt_notification_iterator_private_connection *iterator)
3230ee6b 598{
f42867e2
PP
599 GHashTableIter iter;
600 gpointer stream_key, state_value;
601 bool ret = true;
3230ee6b 602
f42867e2
PP
603 /*
604 * Verify that this iterator received a
605 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
606 * which has a state.
607 */
3230ee6b 608
f42867e2 609 g_hash_table_iter_init(&iter, iterator->stream_states);
3230ee6b 610
f42867e2
PP
611 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
612 struct stream_state *stream_state = (void *) state_value;
3230ee6b 613
f42867e2
PP
614 BT_ASSERT(stream_state);
615 BT_ASSERT(stream_key);
fa054faf 616
f42867e2
PP
617 if (!stream_state->is_ended) {
618 BT_ASSERT_PRE_MSG("Ending notification iterator, "
619 "but stream is not ended: "
620 "%![stream-]s", stream_key);
621 ret = false;
622 goto end;
623 }
3230ee6b
PP
624 }
625
3230ee6b 626end:
3230ee6b
PP
627 return ret;
628}
629
630static
f42867e2
PP
631enum bt_notification_iterator_status
632bt_priv_conn_private_notification_iterator_next(
633 struct bt_notification_iterator_private_connection *iterator)
3230ee6b 634{
f42867e2
PP
635 struct bt_private_connection_private_notification_iterator *priv_iterator =
636 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator);
637 bt_component_class_notification_iterator_next_method next_method = NULL;
638 struct bt_notification_iterator_next_method_return next_return = {
639 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
640 .notification = NULL,
3230ee6b 641 };
f42867e2
PP
642 enum bt_notification_iterator_status status =
643 BT_NOTIFICATION_ITERATOR_STATUS_OK;
3230ee6b 644
f42867e2
PP
645 BT_ASSERT(iterator);
646 BT_LIB_LOGD("Getting next notification iterator's notification: %!+i",
647 iterator);
648 BT_ASSERT_PRE(iterator->state ==
649 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE,
650 "Notification iterator's \"next\" called, but "
651 "iterator is in the wrong state: %!+i", iterator);
652 BT_ASSERT(iterator->upstream_component);
653 BT_ASSERT(iterator->upstream_component->class);
d3eb6e8f 654
3230ee6b
PP
655 /* Pick the appropriate "next" method */
656 switch (iterator->upstream_component->class->type) {
d3eb6e8f
PP
657 case BT_COMPONENT_CLASS_TYPE_SOURCE:
658 {
659 struct bt_component_class_source *source_class =
3230ee6b 660 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
661 struct bt_component_class_source, parent);
662
f6ccaed9 663 BT_ASSERT(source_class->methods.iterator.next);
d3eb6e8f
PP
664 next_method = source_class->methods.iterator.next;
665 break;
666 }
667 case BT_COMPONENT_CLASS_TYPE_FILTER:
668 {
669 struct bt_component_class_filter *filter_class =
3230ee6b 670 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
671 struct bt_component_class_filter, parent);
672
f6ccaed9 673 BT_ASSERT(filter_class->methods.iterator.next);
d3eb6e8f
PP
674 next_method = filter_class->methods.iterator.next;
675 break;
676 }
677 default:
0fbb9a9f 678 abort();
d3eb6e8f
PP
679 }
680
3230ee6b
PP
681 /*
682 * Call the user's "next" method to get the next notification
fa054faf 683 * and status.
3230ee6b 684 */
f6ccaed9 685 BT_ASSERT(next_method);
f42867e2
PP
686 BT_LOGD_STR("Calling user's \"next\" method.");
687 next_return = next_method(priv_iterator);
688 BT_LOGD("User method returned: status=%s",
689 bt_notification_iterator_status_string(next_return.status));
690 if (next_return.status < 0) {
691 BT_LOGW_STR("User method failed.");
692 status = next_return.status;
693 goto end;
694 }
3230ee6b 695
f42867e2
PP
696 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
697 iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
698 /*
699 * The user's "next" method, somehow, cancelled its own
700 * notification iterator. This can happen, for example,
701 * when the user's method removes the port on which
702 * there's the connection from which the iterator was
703 * created. In this case, said connection is ended, and
704 * all its notification iterators are finalized.
705 *
706 * Only bt_put() the returned notification if
707 * the status is
708 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
709 * otherwise this field could be garbage.
710 */
711 if (next_return.status ==
712 BT_NOTIFICATION_ITERATOR_STATUS_OK) {
713 bt_put(next_return.notification);
3230ee6b
PP
714 }
715
f42867e2
PP
716 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
717 goto end;
718 }
8cf27cc5 719
f42867e2
PP
720 switch (next_return.status) {
721 case BT_NOTIFICATION_ITERATOR_STATUS_END:
722 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator),
723 "Notification iterator cannot end at this point: "
724 "%!+i", iterator);
725 BT_ASSERT(iterator->state ==
726 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE);
727 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED;
728 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
729 BT_LOGD("Set new status: status=%s",
730 bt_notification_iterator_status_string(status));
731 goto end;
732 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
733 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
734 goto end;
735 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
736 BT_ASSERT_PRE(next_return.notification,
737 "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: "
738 "%!+i", iterator);
739 BT_ASSERT_PRE(validate_notification(iterator,
740 next_return.notification),
741 "Notification is invalid at this point: "
742 "%![notif-iter-]+i, %![notif-]+n",
743 iterator, next_return.notification);
744 bt_notification_iterator_replace_current_notification(
745 (void *) iterator, next_return.notification);
746 bt_put(next_return.notification);
747 break;
748 default:
749 /* Unknown non-error status */
750 abort();
41a2b7ae
PP
751 }
752
753end:
3230ee6b
PP
754 return status;
755}
756
757enum bt_notification_iterator_status
758bt_notification_iterator_next(struct bt_notification_iterator *iterator)
759{
760 enum bt_notification_iterator_status status;
761
f42867e2 762 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
5af447e5 763 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator);
f42867e2
PP
764 BT_ASSERT(iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION ||
765 iterator->type == BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT);
5af447e5 766
90157d89
PP
767 switch (iterator->type) {
768 case BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION:
769 {
770 struct bt_notification_iterator_private_connection *priv_conn_iter =
771 (void *) iterator;
3230ee6b 772
90157d89
PP
773 /*
774 * Make sure that the iterator's queue contains at least
775 * one notification.
776 */
f42867e2
PP
777 status = bt_priv_conn_private_notification_iterator_next(
778 priv_conn_iter);
8ed535b5
PP
779 break;
780 }
781 case BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT:
782 {
783 struct bt_notification_iterator_output_port *out_port_iter =
784 (void *) iterator;
785
786 /*
787 * Keep current notification in case there's an error:
788 * restore this notification so that the current
789 * notification is not changed from the user's point of
790 * view.
791 */
792 struct bt_notification *old_notif =
793 bt_get(bt_notification_iterator_borrow_current_notification(iterator));
794 enum bt_graph_status graph_status;
795
796 /*
797 * Put current notification since it's possibly
798 * about to be replaced by a new one by the
799 * colander sink.
800 */
801 bt_notification_iterator_replace_current_notification(
802 iterator, NULL);
803 graph_status = bt_graph_consume_sink_no_check(
f42867e2 804 out_port_iter->graph, out_port_iter->colander);
8ed535b5
PP
805 switch (graph_status) {
806 case BT_GRAPH_STATUS_CANCELED:
807 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
808 break;
809 case BT_GRAPH_STATUS_AGAIN:
810 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
811 break;
812 case BT_GRAPH_STATUS_END:
813 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
814 break;
815 case BT_GRAPH_STATUS_NOMEM:
816 status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
817 break;
818 case BT_GRAPH_STATUS_OK:
819 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
f6ccaed9 820 BT_ASSERT(bt_notification_iterator_borrow_current_notification(iterator));
8ed535b5
PP
821 break;
822 default:
823 /* Other errors */
824 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
825 }
826
827 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
828 /* Error/exception: restore old notification */
829 bt_notification_iterator_replace_current_notification(
830 iterator, old_notif);
831 }
832
833 bt_put(old_notif);
90157d89
PP
834 break;
835 }
836 default:
90157d89
PP
837 abort();
838 }
3230ee6b 839
3230ee6b 840 return status;
53d45b87
JG
841}
842
90157d89 843struct bt_component *bt_private_connection_notification_iterator_get_component(
413bc2c4
JG
844 struct bt_notification_iterator *iterator)
845{
90157d89
PP
846 struct bt_notification_iterator_private_connection *iter_priv_conn;
847
f42867e2
PP
848 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
849 BT_ASSERT_PRE(iterator->type ==
850 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
851 "Notification iterator was not created from a private connection: "
852 "%!+i", iterator);
90157d89 853 iter_priv_conn = (void *) iterator;
f42867e2 854 return bt_get(iter_priv_conn->upstream_component);
413bc2c4
JG
855}
856
91457551 857struct bt_private_component *
90157d89
PP
858bt_private_connection_private_notification_iterator_get_private_component(
859 struct bt_private_connection_private_notification_iterator *private_iterator)
91457551
PP
860{
861 return bt_private_component_from_component(
90157d89 862 bt_private_connection_notification_iterator_get_component(
6d137876 863 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
91457551 864}
8ed535b5
PP
865
866static
867void bt_output_port_notification_iterator_destroy(struct bt_object *obj)
868{
869 struct bt_notification_iterator_output_port *iterator =
870 (void *) container_of(obj, struct bt_notification_iterator, base);
871
872 BT_LOGD("Destroying output port notification iterator object: addr=%p",
873 iterator);
874 BT_LOGD_STR("Putting graph.");
875 bt_put(iterator->graph);
876 BT_LOGD_STR("Putting output port.");
877 bt_put(iterator->output_port);
878 BT_LOGD_STR("Putting colander sink component.");
879 bt_put(iterator->colander);
880 destroy_base_notification_iterator(obj);
881}
882
883struct bt_notification_iterator *bt_output_port_notification_iterator_create(
884 struct bt_port *output_port,
f42867e2 885 const char *colander_component_name)
8ed535b5
PP
886{
887 struct bt_notification_iterator *iterator_base = NULL;
888 struct bt_notification_iterator_output_port *iterator = NULL;
889 struct bt_component_class *colander_comp_cls = NULL;
890 struct bt_component *output_port_comp = NULL;
891 struct bt_component *colander_comp;
892 struct bt_graph *graph = NULL;
893 enum bt_graph_status graph_status;
894 const char *colander_comp_name;
895 struct bt_port *colander_in_port = NULL;
896 struct bt_component_class_sink_colander_data colander_data;
897
f42867e2
PP
898 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
899 BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT,
900 "Port is not an output port: %!+p", output_port);
8ed535b5 901 output_port_comp = bt_port_get_component(output_port);
f42867e2
PP
902 BT_ASSERT_PRE(output_port_comp,
903 "Output port has no component: %!+p", output_port);
8ed535b5 904 graph = bt_component_get_graph(output_port_comp);
f6ccaed9 905 BT_ASSERT(graph);
8ed535b5
PP
906
907 /* Create notification iterator */
908 BT_LOGD("Creating notification iterator on output port: "
909 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
910 output_port_comp, bt_component_get_name(output_port_comp),
911 output_port, bt_port_get_name(output_port));
912 iterator = g_new0(struct bt_notification_iterator_output_port, 1);
913 if (!iterator) {
914 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
915 goto error;
916 }
917
918 init_notification_iterator((void *) iterator,
919 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
920 bt_output_port_notification_iterator_destroy);
921
922 /* Create colander component */
923 colander_comp_cls = bt_component_class_sink_colander_get();
924 if (!colander_comp_cls) {
925 BT_LOGW("Cannot get colander sink component class.");
926 goto error;
927 }
928
929 BT_MOVE(iterator->graph, graph);
930 iterator_base = (void *) iterator;
931 colander_comp_name =
932 colander_component_name ? colander_component_name : "colander";
933 colander_data.notification = &iterator_base->current_notification;
8ed535b5
PP
934 graph_status = bt_graph_add_component_with_init_method_data(
935 iterator->graph, colander_comp_cls, colander_comp_name,
936 NULL, &colander_data, &iterator->colander);
937 if (graph_status != BT_GRAPH_STATUS_OK) {
938 BT_LOGW("Cannot add colander sink component to graph: "
939 "graph-addr=%p, name=\"%s\", graph-status=%s",
940 iterator->graph, colander_comp_name,
941 bt_graph_status_string(graph_status));
942 goto error;
943 }
944
945 /*
946 * Connect provided output port to the colander component's
947 * input port.
948 */
949 colander_in_port = bt_component_sink_get_input_port_by_index(
950 iterator->colander, 0);
f6ccaed9 951 BT_ASSERT(colander_in_port);
8ed535b5
PP
952 graph_status = bt_graph_connect_ports(iterator->graph,
953 output_port, colander_in_port, NULL);
954 if (graph_status != BT_GRAPH_STATUS_OK) {
955 BT_LOGW("Cannot add colander sink component to graph: "
956 "graph-addr=%p, name=\"%s\", graph-status=%s",
957 iterator->graph, colander_comp_name,
958 bt_graph_status_string(graph_status));
959 goto error;
960 }
961
962 /*
963 * At this point everything went fine. Make the graph
964 * nonconsumable forever so that only this notification iterator
965 * can consume (thanks to bt_graph_consume_sink_no_check()).
966 * This avoids leaking the notification created by the colander
967 * sink and moved to the base notification iterator's current
968 * notification member.
969 */
970 bt_graph_set_can_consume(iterator->graph, BT_FALSE);
971 goto end;
972
973error:
974 if (iterator && iterator->graph && iterator->colander) {
975 int ret;
976
977 /* Remove created colander component from graph if any */
978 colander_comp = iterator->colander;
979 BT_PUT(iterator->colander);
980
981 /*
982 * At this point the colander component's reference
983 * count is 0 because iterator->colander was the only
984 * owner. We also know that it is not connected because
985 * this is the last operation before this function
986 * succeeds.
987 *
988 * Since we honor the preconditions here,
989 * bt_graph_remove_unconnected_component() always
990 * succeeds.
991 */
992 ret = bt_graph_remove_unconnected_component(iterator->graph,
993 colander_comp);
f6ccaed9 994 BT_ASSERT(ret == 0);
8ed535b5
PP
995 }
996
997 BT_PUT(iterator);
998
999end:
1000 bt_put(colander_in_port);
1001 bt_put(colander_comp_cls);
1002 bt_put(output_port_comp);
1003 bt_put(graph);
1004 return (void *) iterator;
1005}
25b68514
PP
1006
1007struct bt_notification_iterator *
1008bt_private_connection_notification_iterator_from_private(
1009 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
1010{
1011 return bt_get(
1012 bt_private_connection_notification_iterator_borrow_from_private(
1013 private_notification_iterator));
1014}
This page took 0.087065 seconds and 4 git commands to generate.