4 * Babeltrace Notification Iterator
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #define BT_LOG_TAG "NOTIF-ITER"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/fields.h>
34 #include <babeltrace/ctf-ir/field-types.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/packet-internal.h>
38 #include <babeltrace/ctf-ir/stream-internal.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/connection-internal.h>
41 #include <babeltrace/graph/component.h>
42 #include <babeltrace/graph/component-source-internal.h>
43 #include <babeltrace/graph/component-class-internal.h>
44 #include <babeltrace/graph/component-class-sink-colander-internal.h>
45 #include <babeltrace/graph/component-sink.h>
46 #include <babeltrace/graph/notification.h>
47 #include <babeltrace/graph/notification-iterator.h>
48 #include <babeltrace/graph/notification-iterator-internal.h>
49 #include <babeltrace/graph/notification-internal.h>
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>
56 #include <babeltrace/graph/notification-discarded-elements-internal.h>
57 #include <babeltrace/graph/port.h>
58 #include <babeltrace/graph/graph-internal.h>
59 #include <babeltrace/types.h>
60 #include <babeltrace/assert-internal.h>
61 #include <babeltrace/assert-pre-internal.h>
66 struct discarded_elements_state
{
67 struct bt_clock_value
*cur_begin
;
72 struct bt_stream
*stream
; /* owned by this */
73 struct bt_packet
*cur_packet
; /* owned by this */
74 struct discarded_elements_state discarded_packets_state
;
75 struct discarded_elements_state discarded_events_state
;
76 uint64_t expected_notif_seq_num
;
82 void destroy_stream_state(struct stream_state
*stream_state
)
88 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
89 BT_LOGV_STR("Putting stream state's current packet.");
90 bt_put(stream_state
->cur_packet
);
91 BT_LOGV_STR("Putting stream state's stream.");
92 bt_put(stream_state
->stream
);
93 bt_put(stream_state
->discarded_packets_state
.cur_begin
);
94 bt_put(stream_state
->discarded_events_state
.cur_begin
);
100 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
102 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
105 BT_LOGE_STR("Failed to allocate one stream state.");
110 * The packet index is a monotonic counter which may not start
111 * at 0 at the beginning of the stream. We therefore need to
112 * have an internal object initial state of -1ULL to distinguish
113 * between initial state and having seen a packet with
114 * the sequence number 0.
116 stream_state
->discarded_packets_state
.cur_count
= -1ULL;
119 * We keep a reference to the stream until we know it's ended.
121 stream_state
->stream
= bt_get(stream
);
122 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
123 "stream-state-addr=%p",
124 stream
, bt_stream_get_name(stream
), stream_state
);
131 void destroy_base_notification_iterator(struct bt_object
*obj
)
138 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
140 struct bt_notification_iterator_private_connection
*iterator
;
145 * The notification iterator's reference count is 0 if we're
146 * here. Increment it to avoid a double-destroy (possibly
147 * infinitely recursive). This could happen for example if the
148 * notification iterator's finalization function does bt_get()
149 * (or anything that causes bt_get() to be called) on itself
150 * (ref. count goes from 0 to 1), and then bt_put(): the
151 * reference count would go from 1 to 0 again and this function
152 * would be called again.
155 iterator
= (void *) obj
;
156 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
158 bt_private_connection_notification_iterator_finalize(iterator
);
160 if (iterator
->stream_states
) {
162 * Remove our destroy listener from each stream which
163 * has a state in this iterator. Otherwise the destroy
164 * listener would be called with an invalid/other
165 * notification iterator object.
167 g_hash_table_destroy(iterator
->stream_states
);
170 if (iterator
->connection
) {
172 * Remove ourself from the originating connection so
173 * that it does not try to finalize a dangling pointer
176 bt_connection_remove_iterator(iterator
->connection
, iterator
);
179 destroy_base_notification_iterator(obj
);
183 void bt_private_connection_notification_iterator_finalize(
184 struct bt_notification_iterator_private_connection
*iterator
)
186 struct bt_component_class
*comp_class
= NULL
;
187 bt_component_class_notification_iterator_finalize_method
188 finalize_method
= NULL
;
192 switch (iterator
->state
) {
193 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
194 /* Skip user finalization if user initialization failed */
195 BT_LOGD("Not finalizing non-initialized notification iterator: "
196 "addr=%p", iterator
);
198 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
199 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
200 /* Already finalized */
201 BT_LOGD("Not finalizing notification iterator: already finalized: "
202 "addr=%p", iterator
);
208 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
210 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
) {
211 BT_LOGD("Updating notification iterator's state: "
212 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
213 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
215 BT_LOGD("Updating notification iterator's state: "
216 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
217 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
220 BT_ASSERT(iterator
->upstream_component
);
221 comp_class
= iterator
->upstream_component
->class;
223 /* Call user-defined destroy method */
224 switch (comp_class
->type
) {
225 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
227 struct bt_component_class_source
*source_class
;
229 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
230 finalize_method
= source_class
->methods
.iterator
.finalize
;
233 case BT_COMPONENT_CLASS_TYPE_FILTER
:
235 struct bt_component_class_filter
*filter_class
;
237 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
238 finalize_method
= filter_class
->methods
.iterator
.finalize
;
246 if (finalize_method
) {
247 BT_LOGD("Calling user's finalization method: addr=%p",
250 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
253 iterator
->upstream_component
= NULL
;
254 iterator
->upstream_port
= NULL
;
255 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
259 void bt_private_connection_notification_iterator_set_connection(
260 struct bt_notification_iterator_private_connection
*iterator
,
261 struct bt_connection
*connection
)
264 iterator
->connection
= connection
;
265 BT_LOGV("Set notification iterator's connection: "
266 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
270 void init_notification_iterator(struct bt_notification_iterator
*iterator
,
271 enum bt_notification_iterator_type type
,
272 bt_object_release_func destroy
)
274 bt_object_init_shared(&iterator
->base
, destroy
);
275 iterator
->type
= type
;
279 enum bt_connection_status
bt_private_connection_notification_iterator_create(
280 struct bt_component
*upstream_comp
,
281 struct bt_port
*upstream_port
,
282 struct bt_connection
*connection
,
283 struct bt_notification_iterator_private_connection
**user_iterator
)
285 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
286 enum bt_component_class_type type
;
287 struct bt_notification_iterator_private_connection
*iterator
= NULL
;
289 BT_ASSERT(upstream_comp
);
290 BT_ASSERT(upstream_port
);
291 BT_ASSERT(bt_port_is_connected(upstream_port
));
292 BT_ASSERT(user_iterator
);
293 BT_LOGD("Creating notification iterator on private connection: "
294 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
295 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
297 upstream_comp
, bt_component_get_name(upstream_comp
),
298 upstream_port
, bt_port_get_name(upstream_port
),
300 type
= bt_component_get_class_type(upstream_comp
);
301 BT_ASSERT(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
302 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
303 iterator
= g_new0(struct bt_notification_iterator_private_connection
, 1);
305 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
306 status
= BT_CONNECTION_STATUS_NOMEM
;
310 init_notification_iterator((void *) iterator
,
311 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
312 bt_private_connection_notification_iterator_destroy
);
314 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
315 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
316 if (!iterator
->stream_states
) {
317 BT_LOGE_STR("Failed to allocate a GHashTable.");
318 status
= BT_CONNECTION_STATUS_NOMEM
;
322 iterator
->upstream_component
= upstream_comp
;
323 iterator
->upstream_port
= upstream_port
;
324 iterator
->connection
= connection
;
325 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
326 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
327 BT_LOGD("Created notification iterator: "
328 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
329 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
330 "conn-addr=%p, iter-addr=%p",
331 upstream_comp
, bt_component_get_name(upstream_comp
),
332 upstream_port
, bt_port_get_name(upstream_port
),
333 connection
, iterator
);
335 /* Move reference to user */
336 *user_iterator
= iterator
;
344 void *bt_private_connection_private_notification_iterator_get_user_data(
345 struct bt_private_connection_private_notification_iterator
*private_iterator
)
347 struct bt_notification_iterator_private_connection
*iterator
= (void *)
348 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
350 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
351 return iterator
->user_data
;
354 enum bt_notification_iterator_status
355 bt_private_connection_private_notification_iterator_set_user_data(
356 struct bt_private_connection_private_notification_iterator
*private_iterator
,
359 struct bt_notification_iterator_private_connection
*iterator
= (void *)
360 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
362 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
363 iterator
->user_data
= data
;
364 BT_LOGV("Set notification iterator's user data: "
365 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
366 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
369 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
370 struct bt_private_connection_private_notification_iterator
*private_iterator
)
372 struct bt_notification_iterator_private_connection
*iterator
= (void *)
373 bt_private_connection_notification_iterator_borrow_from_private(
376 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
377 return iterator
->graph
;
382 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
383 struct bt_stream
**stream
, struct bt_packet
**packet
)
387 switch (notif
->type
) {
388 case BT_NOTIFICATION_TYPE_EVENT
:
389 *packet
= bt_event_borrow_packet(
390 bt_notification_event_borrow_event(notif
));
391 *stream
= bt_packet_borrow_stream(*packet
);
393 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
394 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
396 case BT_NOTIFICATION_TYPE_STREAM_END
:
397 *stream
= bt_notification_stream_end_borrow_stream(notif
);
399 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
400 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
401 *stream
= bt_packet_borrow_stream(*packet
);
403 case BT_NOTIFICATION_TYPE_PACKET_END
:
404 *packet
= bt_notification_packet_end_borrow_packet(notif
);
405 *stream
= bt_packet_borrow_stream(*packet
);
414 bool validate_notification(
415 struct bt_notification_iterator_private_connection
*iterator
,
416 struct bt_notification
*notif
)
418 bool is_valid
= true;
419 struct stream_state
*stream_state
;
420 struct bt_stream
*stream
= NULL
;
421 struct bt_packet
*packet
= NULL
;
424 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
427 /* we don't care about notifications not attached to streams */
431 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
434 * No stream state for this stream: this notification
435 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
436 * and its sequence number must be 0.
438 if (notif
->type
!= BT_NOTIFICATION_TYPE_STREAM_BEGIN
) {
439 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
440 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
441 "notification prior to this notification: "
442 "%![stream-]+s", stream
);
447 if (notif
->seq_num
== -1ULL) {
451 if (notif
->seq_num
!= 0) {
452 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
453 "number for this notification iterator: "
454 "this is the first notification for this "
455 "stream, expecting sequence number 0: "
456 "seq-num=%" PRIu64
", %![stream-]+s",
457 notif
->seq_num
, stream
);
462 stream_state
= create_stream_state(stream
);
467 g_hash_table_insert(iterator
->stream_states
, stream
,
469 stream_state
->expected_notif_seq_num
++;
473 if (stream_state
->is_ended
) {
475 * There's a new notification which has a reference to a
476 * stream which, from this iterator's point of view, is
477 * ended ("end of stream" notification was returned).
478 * This is bad: the API guarantees that it can never
481 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
487 if (notif
->seq_num
== -1ULL) {
488 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
491 if (notif
->seq_num
!= -1ULL &&
492 notif
->seq_num
!= stream_state
->expected_notif_seq_num
) {
493 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
494 "seq-num=%" PRIu64
", "
495 "expected-seq-num=%" PRIu64
", %![stream-]+s",
496 notif
->seq_num
, stream_state
->expected_notif_seq_num
,
502 switch (notif
->type
) {
503 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
504 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
505 "notification at this point: notif-seq-num=%" PRIu64
", "
506 "%![stream-]+s", notif
->seq_num
, stream
);
509 case BT_NOTIFICATION_TYPE_STREAM_END
:
510 if (stream_state
->cur_packet
) {
511 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
512 "notification: missing a "
513 "BT_NOTIFICATION_TYPE_PACKET_END notification "
514 "prior to this notification: "
515 "notif-seq-num=%" PRIu64
", "
516 "%![stream-]+s", notif
->seq_num
, stream
);
520 stream_state
->expected_notif_seq_num
++;
521 stream_state
->is_ended
= true;
523 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
524 if (stream_state
->cur_packet
) {
525 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
526 "notification at this point: missing a "
527 "BT_NOTIFICATION_TYPE_PACKET_END notification "
528 "prior to this notification: "
529 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
530 "%![packet-]+a", notif
->seq_num
, stream
,
535 stream_state
->expected_notif_seq_num
++;
536 stream_state
->cur_packet
= bt_get(packet
);
538 case BT_NOTIFICATION_TYPE_PACKET_END
:
539 if (!stream_state
->cur_packet
) {
540 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
541 "notification at this point: missing a "
542 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
543 "prior to this notification: "
544 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
545 "%![packet-]+a", notif
->seq_num
, stream
,
550 stream_state
->expected_notif_seq_num
++;
551 BT_PUT(stream_state
->cur_packet
);
553 case BT_NOTIFICATION_TYPE_EVENT
:
554 if (packet
!= stream_state
->cur_packet
) {
555 BT_ASSERT_PRE_MSG("Unexpected packet for "
556 "BT_NOTIFICATION_TYPE_EVENT notification: "
557 "notif-seq-num=%" PRIu64
", %![stream-]+s, "
558 "%![notif-packet-]+a, %![expected-packet-]+a",
559 notif
->seq_num
, stream
,
560 stream_state
->cur_packet
, packet
);
564 stream_state
->expected_notif_seq_num
++;
575 static inline bool priv_conn_notif_iter_can_end(
576 struct bt_notification_iterator_private_connection
*iterator
)
579 gpointer stream_key
, state_value
;
583 * Verify that this iterator received a
584 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
588 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
590 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
591 struct stream_state
*stream_state
= (void *) state_value
;
593 BT_ASSERT(stream_state
);
594 BT_ASSERT(stream_key
);
596 if (!stream_state
->is_ended
) {
597 BT_ASSERT_PRE_MSG("Ending notification iterator, "
598 "but stream is not ended: "
599 "%![stream-]s", stream_key
);
609 enum bt_notification_iterator_status
610 bt_private_connection_notification_iterator_next(
611 struct bt_notification_iterator
*user_iterator
,
612 struct bt_notification
**user_notif
)
614 struct bt_notification_iterator_private_connection
*iterator
=
615 (void *) user_iterator
;
616 struct bt_private_connection_private_notification_iterator
*priv_iterator
=
617 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
);
618 bt_component_class_notification_iterator_next_method next_method
= NULL
;
619 struct bt_notification_iterator_next_method_return next_return
= {
620 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
621 .notification
= NULL
,
623 enum bt_notification_iterator_status status
=
624 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
626 BT_ASSERT_PRE_NON_NULL(user_iterator
, "Notification iterator");
627 BT_ASSERT_PRE_NON_NULL(user_notif
, "Notification");
628 BT_ASSERT_PRE(user_iterator
->type
==
629 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
630 "Notification iterator was not created from a private connection: "
632 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
634 BT_ASSERT_PRE(iterator
->state
==
635 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
636 "Notification iterator's \"next\" called, but "
637 "iterator is in the wrong state: %!+i", iterator
);
638 BT_ASSERT(iterator
->upstream_component
);
639 BT_ASSERT(iterator
->upstream_component
->class);
641 /* Pick the appropriate "next" method */
642 switch (iterator
->upstream_component
->class->type
) {
643 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
645 struct bt_component_class_source
*source_class
=
646 container_of(iterator
->upstream_component
->class,
647 struct bt_component_class_source
, parent
);
649 BT_ASSERT(source_class
->methods
.iterator
.next
);
650 next_method
= source_class
->methods
.iterator
.next
;
653 case BT_COMPONENT_CLASS_TYPE_FILTER
:
655 struct bt_component_class_filter
*filter_class
=
656 container_of(iterator
->upstream_component
->class,
657 struct bt_component_class_filter
, parent
);
659 BT_ASSERT(filter_class
->methods
.iterator
.next
);
660 next_method
= filter_class
->methods
.iterator
.next
;
668 * Call the user's "next" method to get the next notification
671 BT_ASSERT(next_method
);
672 BT_LOGD_STR("Calling user's \"next\" method.");
673 next_return
= next_method(priv_iterator
);
674 BT_LOGD("User method returned: status=%s",
675 bt_notification_iterator_status_string(next_return
.status
));
676 if (next_return
.status
< 0) {
677 BT_LOGW_STR("User method failed.");
678 status
= next_return
.status
;
682 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
683 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
685 * The user's "next" method, somehow, cancelled its own
686 * notification iterator. This can happen, for example,
687 * when the user's method removes the port on which
688 * there's the connection from which the iterator was
689 * created. In this case, said connection is ended, and
690 * all its notification iterators are finalized.
692 * Only bt_put() the returned notification if
694 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
695 * otherwise this field could be garbage.
697 if (next_return
.status
==
698 BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
699 bt_put(next_return
.notification
);
702 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
706 switch (next_return
.status
) {
707 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
708 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
709 "Notification iterator cannot end at this point: "
711 BT_ASSERT(iterator
->state
==
712 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
713 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
;
714 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
715 BT_LOGD("Set new status: status=%s",
716 bt_notification_iterator_status_string(status
));
718 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
719 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
721 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
722 BT_ASSERT_PRE(next_return
.notification
,
723 "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: "
725 BT_ASSERT_PRE(validate_notification(iterator
,
726 next_return
.notification
),
727 "Notification is invalid at this point: "
728 "%![notif-iter-]+i, %![notif-]+n",
729 iterator
, next_return
.notification
);
730 *user_notif
= next_return
.notification
;
733 /* Unknown non-error status */
741 enum bt_notification_iterator_status
742 bt_output_port_notification_iterator_next(
743 struct bt_notification_iterator
*iterator
,
744 struct bt_notification
**user_notif
)
746 enum bt_notification_iterator_status status
;
747 struct bt_notification_iterator_output_port
*out_port_iter
=
749 enum bt_graph_status graph_status
;
751 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
752 BT_ASSERT_PRE_NON_NULL(user_notif
, "Notification");
753 BT_ASSERT_PRE(iterator
->type
==
754 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
755 "Notification iterator was not created from an output port: "
757 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
759 graph_status
= bt_graph_consume_sink_no_check(
760 out_port_iter
->graph
, out_port_iter
->colander
);
761 switch (graph_status
) {
762 case BT_GRAPH_STATUS_CANCELED
:
763 BT_ASSERT(!out_port_iter
->notif
);
764 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
766 case BT_GRAPH_STATUS_AGAIN
:
767 BT_ASSERT(!out_port_iter
->notif
);
768 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
770 case BT_GRAPH_STATUS_END
:
771 BT_ASSERT(!out_port_iter
->notif
);
772 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
774 case BT_GRAPH_STATUS_NOMEM
:
775 BT_ASSERT(!out_port_iter
->notif
);
776 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
778 case BT_GRAPH_STATUS_OK
:
779 BT_ASSERT(out_port_iter
->notif
);
780 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
781 *user_notif
= out_port_iter
->notif
;
782 out_port_iter
->notif
= NULL
;
786 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
792 struct bt_component
*bt_private_connection_notification_iterator_get_component(
793 struct bt_notification_iterator
*iterator
)
795 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
797 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
798 BT_ASSERT_PRE(iterator
->type
==
799 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
800 "Notification iterator was not created from a private connection: "
802 iter_priv_conn
= (void *) iterator
;
803 return bt_get(iter_priv_conn
->upstream_component
);
806 struct bt_private_component
*
807 bt_private_connection_private_notification_iterator_get_private_component(
808 struct bt_private_connection_private_notification_iterator
*private_iterator
)
810 return bt_private_component_from_component(
811 bt_private_connection_notification_iterator_get_component(
812 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator
)));
816 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
818 struct bt_notification_iterator_output_port
*iterator
=
819 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
821 BT_LOGD("Destroying output port notification iterator object: addr=%p",
823 BT_ASSERT(!iterator
->notif
);
824 BT_LOGD_STR("Putting graph.");
825 bt_put(iterator
->graph
);
826 BT_LOGD_STR("Putting colander sink component.");
827 bt_put(iterator
->colander
);
828 destroy_base_notification_iterator(obj
);
831 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
832 struct bt_port
*output_port
,
833 const char *colander_component_name
)
835 struct bt_notification_iterator_output_port
*iterator
= NULL
;
836 struct bt_component_class
*colander_comp_cls
= NULL
;
837 struct bt_component
*output_port_comp
= NULL
;
838 struct bt_component
*colander_comp
;
839 struct bt_graph
*graph
= NULL
;
840 enum bt_graph_status graph_status
;
841 const char *colander_comp_name
;
842 struct bt_port
*colander_in_port
= NULL
;
843 struct bt_component_class_sink_colander_data colander_data
;
845 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
846 BT_ASSERT_PRE(bt_port_get_type(output_port
) == BT_PORT_TYPE_OUTPUT
,
847 "Port is not an output port: %!+p", output_port
);
848 output_port_comp
= bt_port_get_component(output_port
);
849 BT_ASSERT_PRE(output_port_comp
,
850 "Output port has no component: %!+p", output_port
);
851 graph
= bt_component_get_graph(output_port_comp
);
854 /* Create notification iterator */
855 BT_LOGD("Creating notification iterator on output port: "
856 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
857 output_port_comp
, bt_component_get_name(output_port_comp
),
858 output_port
, bt_port_get_name(output_port
));
859 iterator
= g_new0(struct bt_notification_iterator_output_port
, 1);
861 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
865 init_notification_iterator((void *) iterator
,
866 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
867 bt_output_port_notification_iterator_destroy
);
869 /* Create colander component */
870 colander_comp_cls
= bt_component_class_sink_colander_get();
871 if (!colander_comp_cls
) {
872 BT_LOGW("Cannot get colander sink component class.");
876 BT_MOVE(iterator
->graph
, graph
);
878 colander_component_name
? colander_component_name
: "colander";
879 colander_data
.notification
= &iterator
->notif
;
880 graph_status
= bt_graph_add_component_with_init_method_data(
881 iterator
->graph
, colander_comp_cls
, colander_comp_name
,
882 NULL
, &colander_data
, &iterator
->colander
);
883 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
884 BT_LOGW("Cannot add colander sink component to graph: "
885 "graph-addr=%p, name=\"%s\", graph-status=%s",
886 iterator
->graph
, colander_comp_name
,
887 bt_graph_status_string(graph_status
));
892 * Connect provided output port to the colander component's
895 colander_in_port
= bt_component_sink_get_input_port_by_index(
896 iterator
->colander
, 0);
897 BT_ASSERT(colander_in_port
);
898 graph_status
= bt_graph_connect_ports(iterator
->graph
,
899 output_port
, colander_in_port
, NULL
);
900 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
901 BT_LOGW("Cannot add colander sink component to graph: "
902 "graph-addr=%p, name=\"%s\", graph-status=%s",
903 iterator
->graph
, colander_comp_name
,
904 bt_graph_status_string(graph_status
));
909 * At this point everything went fine. Make the graph
910 * nonconsumable forever so that only this notification iterator
911 * can consume (thanks to bt_graph_consume_sink_no_check()).
912 * This avoids leaking the notification created by the colander
913 * sink and moved to the notification iterator's notification
916 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
920 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
923 /* Remove created colander component from graph if any */
924 colander_comp
= iterator
->colander
;
925 BT_PUT(iterator
->colander
);
928 * At this point the colander component's reference
929 * count is 0 because iterator->colander was the only
930 * owner. We also know that it is not connected because
931 * this is the last operation before this function
934 * Since we honor the preconditions here,
935 * bt_graph_remove_unconnected_component() always
938 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
946 bt_put(colander_in_port
);
947 bt_put(colander_comp_cls
);
948 bt_put(output_port_comp
);
950 return (void *) iterator
;
953 struct bt_notification_iterator
*
954 bt_private_connection_notification_iterator_borrow_from_private(
955 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
957 return (void *) private_notification_iterator
;