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
;
81 void stream_destroy_listener(struct bt_stream_common
*stream
, void *data
)
83 struct bt_notification_iterator_private_connection
*iterator
= data
;
85 /* Remove associated stream state */
86 g_hash_table_remove(iterator
->stream_states
, stream
);
90 void destroy_stream_state(struct stream_state
*stream_state
)
96 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
97 BT_LOGV_STR("Putting stream state's current packet.");
98 bt_put(stream_state
->cur_packet
);
99 BT_LOGV_STR("Putting stream state's stream.");
100 bt_put(stream_state
->stream
);
101 bt_put(stream_state
->discarded_packets_state
.cur_begin
);
102 bt_put(stream_state
->discarded_events_state
.cur_begin
);
103 g_free(stream_state
);
107 struct stream_state
*create_stream_state(struct bt_stream
*stream
)
109 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
112 BT_LOGE_STR("Failed to allocate one stream state.");
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
121 * the sequence number 0.
123 stream_state
->discarded_packets_state
.cur_count
= -1ULL;
126 * We keep a reference to the stream until we know it's ended.
128 stream_state
->stream
= bt_get(stream
);
129 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
130 "stream-state-addr=%p",
131 stream
, bt_stream_get_name(stream
), stream_state
);
138 void destroy_base_notification_iterator(struct bt_object
*obj
)
145 void bt_private_connection_notification_iterator_destroy(struct bt_object
*obj
)
147 struct bt_notification_iterator_private_connection
*iterator
;
152 * The notification iterator's reference count is 0 if we're
153 * here. Increment it to avoid a double-destroy (possibly
154 * infinitely recursive). This could happen for example if the
155 * notification iterator's finalization function does bt_get()
156 * (or anything that causes bt_get() to be called) on itself
157 * (ref. count goes from 0 to 1), and then bt_put(): the
158 * reference count would go from 1 to 0 again and this function
159 * would be called again.
162 iterator
= (void *) obj
;
163 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
165 bt_private_connection_notification_iterator_finalize(iterator
);
167 if (iterator
->stream_states
) {
169 * Remove our destroy listener from each stream which
170 * has a state in this iterator. Otherwise the destroy
171 * listener would be called with an invalid/other
172 * notification iterator object.
174 GHashTableIter ht_iter
;
175 gpointer stream_gptr
, stream_state_gptr
;
177 g_hash_table_iter_init(&ht_iter
, iterator
->stream_states
);
179 while (g_hash_table_iter_next(&ht_iter
, &stream_gptr
, &stream_state_gptr
)) {
180 BT_ASSERT(stream_gptr
);
182 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
183 bt_stream_common_remove_destroy_listener(
184 (void *) stream_gptr
, stream_destroy_listener
,
188 g_hash_table_destroy(iterator
->stream_states
);
191 if (iterator
->connection
) {
193 * Remove ourself from the originating connection so
194 * that it does not try to finalize a dangling pointer
197 bt_connection_remove_iterator(iterator
->connection
, iterator
);
200 destroy_base_notification_iterator(obj
);
204 void bt_private_connection_notification_iterator_finalize(
205 struct bt_notification_iterator_private_connection
*iterator
)
207 struct bt_component_class
*comp_class
= NULL
;
208 bt_component_class_notification_iterator_finalize_method
209 finalize_method
= NULL
;
213 switch (iterator
->state
) {
214 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
215 /* Skip user finalization if user initialization failed */
216 BT_LOGD("Not finalizing non-initialized notification iterator: "
217 "addr=%p", iterator
);
219 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
220 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
221 /* Already finalized */
222 BT_LOGD("Not finalizing notification iterator: already finalized: "
223 "addr=%p", iterator
);
229 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
231 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
) {
232 BT_LOGD("Updating notification iterator's state: "
233 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
234 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
236 BT_LOGD("Updating notification iterator's state: "
237 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
238 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
241 BT_ASSERT(iterator
->upstream_component
);
242 comp_class
= iterator
->upstream_component
->class;
244 /* Call user-defined destroy method */
245 switch (comp_class
->type
) {
246 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
248 struct bt_component_class_source
*source_class
;
250 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
251 finalize_method
= source_class
->methods
.iterator
.finalize
;
254 case BT_COMPONENT_CLASS_TYPE_FILTER
:
256 struct bt_component_class_filter
*filter_class
;
258 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
259 finalize_method
= filter_class
->methods
.iterator
.finalize
;
267 if (finalize_method
) {
268 BT_LOGD("Calling user's finalization method: addr=%p",
271 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
));
274 iterator
->upstream_component
= NULL
;
275 iterator
->upstream_port
= NULL
;
276 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
280 void bt_private_connection_notification_iterator_set_connection(
281 struct bt_notification_iterator_private_connection
*iterator
,
282 struct bt_connection
*connection
)
285 iterator
->connection
= connection
;
286 BT_LOGV("Set notification iterator's connection: "
287 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
291 void init_notification_iterator(struct bt_notification_iterator
*iterator
,
292 enum bt_notification_iterator_type type
,
293 bt_object_release_func destroy
)
295 bt_object_init_shared(&iterator
->base
, destroy
);
296 iterator
->type
= type
;
300 enum bt_connection_status
bt_private_connection_notification_iterator_create(
301 struct bt_component
*upstream_comp
,
302 struct bt_port
*upstream_port
,
303 struct bt_connection
*connection
,
304 struct bt_notification_iterator_private_connection
**user_iterator
)
306 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
307 enum bt_component_class_type type
;
308 struct bt_notification_iterator_private_connection
*iterator
= NULL
;
310 BT_ASSERT(upstream_comp
);
311 BT_ASSERT(upstream_port
);
312 BT_ASSERT(bt_port_is_connected(upstream_port
));
313 BT_ASSERT(user_iterator
);
314 BT_LOGD("Creating notification iterator on private connection: "
315 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
316 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
318 upstream_comp
, bt_component_get_name(upstream_comp
),
319 upstream_port
, bt_port_get_name(upstream_port
),
321 type
= bt_component_get_class_type(upstream_comp
);
322 BT_ASSERT(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
323 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
324 iterator
= g_new0(struct bt_notification_iterator_private_connection
, 1);
326 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
327 status
= BT_CONNECTION_STATUS_NOMEM
;
331 init_notification_iterator((void *) iterator
,
332 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
333 bt_private_connection_notification_iterator_destroy
);
335 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
336 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
337 if (!iterator
->stream_states
) {
338 BT_LOGE_STR("Failed to allocate a GHashTable.");
339 status
= BT_CONNECTION_STATUS_NOMEM
;
343 iterator
->upstream_component
= upstream_comp
;
344 iterator
->upstream_port
= upstream_port
;
345 iterator
->connection
= connection
;
346 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
347 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
348 BT_LOGD("Created notification iterator: "
349 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
350 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
351 "conn-addr=%p, iter-addr=%p",
352 upstream_comp
, bt_component_get_name(upstream_comp
),
353 upstream_port
, bt_port_get_name(upstream_port
),
354 connection
, iterator
);
356 /* Move reference to user */
357 *user_iterator
= iterator
;
365 void *bt_private_connection_private_notification_iterator_get_user_data(
366 struct bt_private_connection_private_notification_iterator
*private_iterator
)
368 struct bt_notification_iterator_private_connection
*iterator
= (void *)
369 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
371 BT_ASSERT_PRE_NON_NULL(private_iterator
, "Notification iterator");
372 return iterator
->user_data
;
375 enum bt_notification_iterator_status
376 bt_private_connection_private_notification_iterator_set_user_data(
377 struct bt_private_connection_private_notification_iterator
*private_iterator
,
380 struct bt_notification_iterator_private_connection
*iterator
= (void *)
381 bt_private_connection_notification_iterator_borrow_from_private(private_iterator
);
383 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
384 iterator
->user_data
= data
;
385 BT_LOGV("Set notification iterator's user data: "
386 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
387 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
390 struct bt_graph
*bt_private_connection_private_notification_iterator_borrow_graph(
391 struct bt_private_connection_private_notification_iterator
*private_iterator
)
393 struct bt_notification_iterator_private_connection
*iterator
= (void *)
394 bt_private_connection_notification_iterator_borrow_from_private(
397 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
398 return iterator
->graph
;
403 void bt_notification_borrow_packet_stream(struct bt_notification
*notif
,
404 struct bt_stream
**stream
, struct bt_packet
**packet
)
408 switch (notif
->type
) {
409 case BT_NOTIFICATION_TYPE_EVENT
:
410 *packet
= bt_event_borrow_packet(
411 bt_notification_event_borrow_event(notif
));
412 *stream
= bt_packet_borrow_stream(*packet
);
414 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
415 *stream
= bt_notification_stream_begin_borrow_stream(notif
);
417 case BT_NOTIFICATION_TYPE_STREAM_END
:
418 *stream
= bt_notification_stream_end_borrow_stream(notif
);
420 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
421 *packet
= bt_notification_packet_begin_borrow_packet(notif
);
422 *stream
= bt_packet_borrow_stream(*packet
);
424 case BT_NOTIFICATION_TYPE_PACKET_END
:
425 *packet
= bt_notification_packet_end_borrow_packet(notif
);
426 *stream
= bt_packet_borrow_stream(*packet
);
435 bool validate_notification(
436 struct bt_notification_iterator_private_connection
*iterator
,
437 struct bt_notification
*notif
)
439 bool is_valid
= true;
440 struct stream_state
*stream_state
;
441 struct bt_stream
*stream
= NULL
;
442 struct bt_packet
*packet
= NULL
;
445 bt_notification_borrow_packet_stream(notif
, &stream
, &packet
);
448 /* we don't care about notifications not attached to streams */
452 stream_state
= g_hash_table_lookup(iterator
->stream_states
, stream
);
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.
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
);
468 if (notif
->seq_num
== -1ULL) {
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
);
483 stream_state
= create_stream_state(stream
);
488 g_hash_table_insert(iterator
->stream_states
, stream
,
490 stream_state
->expected_notif_seq_num
++;
494 if (stream_state
->is_ended
) {
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
502 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
508 if (notif
->seq_num
== -1ULL) {
509 notif
->seq_num
= stream_state
->expected_notif_seq_num
;
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
,
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
);
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
);
541 stream_state
->expected_notif_seq_num
++;
542 stream_state
->is_ended
= true;
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
,
556 stream_state
->expected_notif_seq_num
++;
557 stream_state
->cur_packet
= bt_get(packet
);
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
,
571 stream_state
->expected_notif_seq_num
++;
572 BT_PUT(stream_state
->cur_packet
);
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
);
585 stream_state
->expected_notif_seq_num
++;
596 static inline bool priv_conn_notif_iter_can_end(
597 struct bt_notification_iterator_private_connection
*iterator
)
600 gpointer stream_key
, state_value
;
604 * Verify that this iterator received a
605 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
609 g_hash_table_iter_init(&iter
, iterator
->stream_states
);
611 while (g_hash_table_iter_next(&iter
, &stream_key
, &state_value
)) {
612 struct stream_state
*stream_state
= (void *) state_value
;
614 BT_ASSERT(stream_state
);
615 BT_ASSERT(stream_key
);
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
);
630 enum bt_notification_iterator_status
631 bt_private_connection_notification_iterator_next(
632 struct bt_notification_iterator
*user_iterator
,
633 struct bt_notification
**user_notif
)
635 struct bt_notification_iterator_private_connection
*iterator
=
636 (void *) user_iterator
;
637 struct bt_private_connection_private_notification_iterator
*priv_iterator
=
638 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator
);
639 bt_component_class_notification_iterator_next_method next_method
= NULL
;
640 struct bt_notification_iterator_next_method_return next_return
= {
641 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
642 .notification
= NULL
,
644 enum bt_notification_iterator_status status
=
645 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
647 BT_ASSERT_PRE_NON_NULL(user_iterator
, "Notification iterator");
648 BT_ASSERT_PRE_NON_NULL(user_notif
, "Notification");
649 BT_ASSERT_PRE(user_iterator
->type
==
650 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
651 "Notification iterator was not created from a private connection: "
653 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
655 BT_ASSERT_PRE(iterator
->state
==
656 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
,
657 "Notification iterator's \"next\" called, but "
658 "iterator is in the wrong state: %!+i", iterator
);
659 BT_ASSERT(iterator
->upstream_component
);
660 BT_ASSERT(iterator
->upstream_component
->class);
662 /* Pick the appropriate "next" method */
663 switch (iterator
->upstream_component
->class->type
) {
664 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
666 struct bt_component_class_source
*source_class
=
667 container_of(iterator
->upstream_component
->class,
668 struct bt_component_class_source
, parent
);
670 BT_ASSERT(source_class
->methods
.iterator
.next
);
671 next_method
= source_class
->methods
.iterator
.next
;
674 case BT_COMPONENT_CLASS_TYPE_FILTER
:
676 struct bt_component_class_filter
*filter_class
=
677 container_of(iterator
->upstream_component
->class,
678 struct bt_component_class_filter
, parent
);
680 BT_ASSERT(filter_class
->methods
.iterator
.next
);
681 next_method
= filter_class
->methods
.iterator
.next
;
689 * Call the user's "next" method to get the next notification
692 BT_ASSERT(next_method
);
693 BT_LOGD_STR("Calling user's \"next\" method.");
694 next_return
= next_method(priv_iterator
);
695 BT_LOGD("User method returned: status=%s",
696 bt_notification_iterator_status_string(next_return
.status
));
697 if (next_return
.status
< 0) {
698 BT_LOGW_STR("User method failed.");
699 status
= next_return
.status
;
703 if (iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
704 iterator
->state
== BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
706 * The user's "next" method, somehow, cancelled its own
707 * notification iterator. This can happen, for example,
708 * when the user's method removes the port on which
709 * there's the connection from which the iterator was
710 * created. In this case, said connection is ended, and
711 * all its notification iterators are finalized.
713 * Only bt_put() the returned notification if
715 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
716 * otherwise this field could be garbage.
718 if (next_return
.status
==
719 BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
720 bt_put(next_return
.notification
);
723 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
727 switch (next_return
.status
) {
728 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
729 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator
),
730 "Notification iterator cannot end at this point: "
732 BT_ASSERT(iterator
->state
==
733 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
734 iterator
->state
= BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED
;
735 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
736 BT_LOGD("Set new status: status=%s",
737 bt_notification_iterator_status_string(status
));
739 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
740 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
742 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
743 BT_ASSERT_PRE(next_return
.notification
,
744 "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: "
746 BT_ASSERT_PRE(validate_notification(iterator
,
747 next_return
.notification
),
748 "Notification is invalid at this point: "
749 "%![notif-iter-]+i, %![notif-]+n",
750 iterator
, next_return
.notification
);
751 *user_notif
= next_return
.notification
;
754 /* Unknown non-error status */
762 enum bt_notification_iterator_status
763 bt_output_port_notification_iterator_next(
764 struct bt_notification_iterator
*iterator
,
765 struct bt_notification
**user_notif
)
767 enum bt_notification_iterator_status status
;
768 struct bt_notification_iterator_output_port
*out_port_iter
=
770 enum bt_graph_status graph_status
;
772 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
773 BT_ASSERT_PRE_NON_NULL(user_notif
, "Notification");
774 BT_ASSERT_PRE(iterator
->type
==
775 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
776 "Notification iterator was not created from an output port: "
778 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
780 graph_status
= bt_graph_consume_sink_no_check(
781 out_port_iter
->graph
, out_port_iter
->colander
);
782 switch (graph_status
) {
783 case BT_GRAPH_STATUS_CANCELED
:
784 BT_ASSERT(!out_port_iter
->notif
);
785 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
787 case BT_GRAPH_STATUS_AGAIN
:
788 BT_ASSERT(!out_port_iter
->notif
);
789 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
791 case BT_GRAPH_STATUS_END
:
792 BT_ASSERT(!out_port_iter
->notif
);
793 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
795 case BT_GRAPH_STATUS_NOMEM
:
796 BT_ASSERT(!out_port_iter
->notif
);
797 status
= BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
799 case BT_GRAPH_STATUS_OK
:
800 BT_ASSERT(out_port_iter
->notif
);
801 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
802 *user_notif
= out_port_iter
->notif
;
803 out_port_iter
->notif
= NULL
;
807 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
813 struct bt_component
*bt_private_connection_notification_iterator_get_component(
814 struct bt_notification_iterator
*iterator
)
816 struct bt_notification_iterator_private_connection
*iter_priv_conn
;
818 BT_ASSERT_PRE_NON_NULL(iterator
, "Notification iterator");
819 BT_ASSERT_PRE(iterator
->type
==
820 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION
,
821 "Notification iterator was not created from a private connection: "
823 iter_priv_conn
= (void *) iterator
;
824 return bt_get(iter_priv_conn
->upstream_component
);
827 struct bt_private_component
*
828 bt_private_connection_private_notification_iterator_get_private_component(
829 struct bt_private_connection_private_notification_iterator
*private_iterator
)
831 return bt_private_component_from_component(
832 bt_private_connection_notification_iterator_get_component(
833 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator
)));
837 void bt_output_port_notification_iterator_destroy(struct bt_object
*obj
)
839 struct bt_notification_iterator_output_port
*iterator
=
840 (void *) container_of(obj
, struct bt_notification_iterator
, base
);
842 BT_LOGD("Destroying output port notification iterator object: addr=%p",
844 BT_ASSERT(!iterator
->notif
);
845 BT_LOGD_STR("Putting graph.");
846 bt_put(iterator
->graph
);
847 BT_LOGD_STR("Putting colander sink component.");
848 bt_put(iterator
->colander
);
849 destroy_base_notification_iterator(obj
);
852 struct bt_notification_iterator
*bt_output_port_notification_iterator_create(
853 struct bt_port
*output_port
,
854 const char *colander_component_name
)
856 struct bt_notification_iterator_output_port
*iterator
= NULL
;
857 struct bt_component_class
*colander_comp_cls
= NULL
;
858 struct bt_component
*output_port_comp
= NULL
;
859 struct bt_component
*colander_comp
;
860 struct bt_graph
*graph
= NULL
;
861 enum bt_graph_status graph_status
;
862 const char *colander_comp_name
;
863 struct bt_port
*colander_in_port
= NULL
;
864 struct bt_component_class_sink_colander_data colander_data
;
866 BT_ASSERT_PRE_NON_NULL(output_port
, "Output port");
867 BT_ASSERT_PRE(bt_port_get_type(output_port
) == BT_PORT_TYPE_OUTPUT
,
868 "Port is not an output port: %!+p", output_port
);
869 output_port_comp
= bt_port_get_component(output_port
);
870 BT_ASSERT_PRE(output_port_comp
,
871 "Output port has no component: %!+p", output_port
);
872 graph
= bt_component_get_graph(output_port_comp
);
875 /* Create notification iterator */
876 BT_LOGD("Creating notification iterator on output port: "
877 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
878 output_port_comp
, bt_component_get_name(output_port_comp
),
879 output_port
, bt_port_get_name(output_port
));
880 iterator
= g_new0(struct bt_notification_iterator_output_port
, 1);
882 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
886 init_notification_iterator((void *) iterator
,
887 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT
,
888 bt_output_port_notification_iterator_destroy
);
890 /* Create colander component */
891 colander_comp_cls
= bt_component_class_sink_colander_get();
892 if (!colander_comp_cls
) {
893 BT_LOGW("Cannot get colander sink component class.");
897 BT_MOVE(iterator
->graph
, graph
);
899 colander_component_name
? colander_component_name
: "colander";
900 colander_data
.notification
= &iterator
->notif
;
901 graph_status
= bt_graph_add_component_with_init_method_data(
902 iterator
->graph
, colander_comp_cls
, colander_comp_name
,
903 NULL
, &colander_data
, &iterator
->colander
);
904 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
905 BT_LOGW("Cannot add colander sink component to graph: "
906 "graph-addr=%p, name=\"%s\", graph-status=%s",
907 iterator
->graph
, colander_comp_name
,
908 bt_graph_status_string(graph_status
));
913 * Connect provided output port to the colander component's
916 colander_in_port
= bt_component_sink_get_input_port_by_index(
917 iterator
->colander
, 0);
918 BT_ASSERT(colander_in_port
);
919 graph_status
= bt_graph_connect_ports(iterator
->graph
,
920 output_port
, colander_in_port
, NULL
);
921 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
922 BT_LOGW("Cannot add colander sink component to graph: "
923 "graph-addr=%p, name=\"%s\", graph-status=%s",
924 iterator
->graph
, colander_comp_name
,
925 bt_graph_status_string(graph_status
));
930 * At this point everything went fine. Make the graph
931 * nonconsumable forever so that only this notification iterator
932 * can consume (thanks to bt_graph_consume_sink_no_check()).
933 * This avoids leaking the notification created by the colander
934 * sink and moved to the notification iterator's notification
937 bt_graph_set_can_consume(iterator
->graph
, BT_FALSE
);
941 if (iterator
&& iterator
->graph
&& iterator
->colander
) {
944 /* Remove created colander component from graph if any */
945 colander_comp
= iterator
->colander
;
946 BT_PUT(iterator
->colander
);
949 * At this point the colander component's reference
950 * count is 0 because iterator->colander was the only
951 * owner. We also know that it is not connected because
952 * this is the last operation before this function
955 * Since we honor the preconditions here,
956 * bt_graph_remove_unconnected_component() always
959 ret
= bt_graph_remove_unconnected_component(iterator
->graph
,
967 bt_put(colander_in_port
);
968 bt_put(colander_comp_cls
);
969 bt_put(output_port_comp
);
971 return (void *) iterator
;
974 struct bt_notification_iterator
*
975 bt_private_connection_notification_iterator_borrow_from_private(
976 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
978 return (void *) private_notification_iterator
;