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 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf-ir/event-internal.h>
31 #include <babeltrace/ctf-ir/packet-internal.h>
32 #include <babeltrace/ctf-ir/stream-internal.h>
33 #include <babeltrace/graph/connection-internal.h>
34 #include <babeltrace/graph/component.h>
35 #include <babeltrace/graph/component-source-internal.h>
36 #include <babeltrace/graph/component-class-internal.h>
37 #include <babeltrace/graph/notification.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/notification-iterator-internal.h>
40 #include <babeltrace/graph/notification-internal.h>
41 #include <babeltrace/graph/notification-event.h>
42 #include <babeltrace/graph/notification-event-internal.h>
43 #include <babeltrace/graph/notification-packet.h>
44 #include <babeltrace/graph/notification-packet-internal.h>
45 #include <babeltrace/graph/notification-stream.h>
46 #include <babeltrace/graph/notification-stream-internal.h>
47 #include <babeltrace/graph/port.h>
48 #include <babeltrace/types.h>
52 struct bt_ctf_stream
*stream
; /* owned by this */
53 struct bt_ctf_packet
*cur_packet
; /* owned by this */
58 ACTION_TYPE_PUSH_NOTIF
,
59 ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
,
60 ACTION_TYPE_ADD_STREAM_STATE
,
61 ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
,
62 ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
,
66 enum action_type type
;
68 /* ACTION_TYPE_PUSH_NOTIF */
70 struct bt_notification
*notif
; /* owned by this */
73 /* ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM */
75 struct bt_ctf_stream
*stream
; /* owned by this */
76 struct bt_component
*component
; /* owned by this */
77 struct bt_port
*port
; /* owned by this */
78 } map_port_to_comp_in_stream
;
80 /* ACTION_TYPE_ADD_STREAM_STATE */
82 struct bt_ctf_stream
*stream
; /* owned by this */
83 struct stream_state
*stream_state
; /* owned by this */
86 /* ACTION_TYPE_SET_STREAM_STATE_IS_ENDED */
88 struct stream_state
*stream_state
; /* weak */
89 } set_stream_state_is_ended
;
91 /* ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET */
93 struct stream_state
*stream_state
; /* weak */
94 struct bt_ctf_packet
*packet
; /* owned by this */
95 } set_stream_state_cur_packet
;
100 void stream_destroy_listener(struct bt_ctf_stream
*stream
, void *data
)
102 struct bt_notification_iterator
*iterator
= data
;
104 /* Remove associated stream state */
105 g_hash_table_remove(iterator
->stream_states
, stream
);
109 void destroy_stream_state(struct stream_state
*stream_state
)
115 bt_put(stream_state
->cur_packet
);
116 bt_put(stream_state
->stream
);
117 g_free(stream_state
);
121 void destroy_action(struct action
*action
)
125 switch (action
->type
) {
126 case ACTION_TYPE_PUSH_NOTIF
:
127 BT_PUT(action
->payload
.push_notif
.notif
);
129 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
:
130 BT_PUT(action
->payload
.map_port_to_comp_in_stream
.stream
);
131 BT_PUT(action
->payload
.map_port_to_comp_in_stream
.component
);
132 BT_PUT(action
->payload
.map_port_to_comp_in_stream
.port
);
134 case ACTION_TYPE_ADD_STREAM_STATE
:
135 BT_PUT(action
->payload
.add_stream_state
.stream
);
136 destroy_stream_state(
137 action
->payload
.add_stream_state
.stream_state
);
138 action
->payload
.add_stream_state
.stream_state
= NULL
;
140 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
:
141 BT_PUT(action
->payload
.set_stream_state_cur_packet
.packet
);
143 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
:
151 void add_action(struct bt_notification_iterator
*iterator
,
152 struct action
*action
)
154 g_array_append_val(iterator
->actions
, *action
);
158 void clear_actions(struct bt_notification_iterator
*iterator
)
162 for (i
= 0; i
< iterator
->actions
->len
; i
++) {
163 struct action
*action
= &g_array_index(iterator
->actions
,
166 destroy_action(action
);
169 g_array_set_size(iterator
->actions
, 0);
173 void apply_actions(struct bt_notification_iterator
*iterator
)
177 for (i
= 0; i
< iterator
->actions
->len
; i
++) {
178 struct action
*action
= &g_array_index(iterator
->actions
,
181 switch (action
->type
) {
182 case ACTION_TYPE_PUSH_NOTIF
:
183 /* Move notification to queue */
184 g_queue_push_head(iterator
->queue
,
185 action
->payload
.push_notif
.notif
);
186 bt_notification_freeze(
187 action
->payload
.push_notif
.notif
);
188 action
->payload
.push_notif
.notif
= NULL
;
190 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
:
191 bt_ctf_stream_map_component_to_port(
192 action
->payload
.map_port_to_comp_in_stream
.stream
,
193 action
->payload
.map_port_to_comp_in_stream
.component
,
194 action
->payload
.map_port_to_comp_in_stream
.port
);
196 case ACTION_TYPE_ADD_STREAM_STATE
:
197 /* Move stream state to hash table */
198 g_hash_table_insert(iterator
->stream_states
,
199 action
->payload
.add_stream_state
.stream
,
200 action
->payload
.add_stream_state
.stream_state
);
202 action
->payload
.add_stream_state
.stream_state
= NULL
;
204 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
:
206 * We know that this stream is ended. We need to
207 * remember this as long as the stream exists to
208 * enforce that the same stream does not end
211 * Here we add a destroy listener to the stream
212 * which we put after (becomes weak as the hash
213 * table key). If we were the last object to own
214 * this stream, the destroy listener is called
215 * when we call bt_put() which removes this
216 * stream state completely. This is important
217 * because the memory used by this stream object
218 * could be reused for another stream, and they
219 * must have different states.
221 bt_ctf_stream_add_destroy_listener(
222 action
->payload
.set_stream_state_is_ended
.stream_state
->stream
,
223 stream_destroy_listener
, iterator
);
224 action
->payload
.set_stream_state_is_ended
.stream_state
->is_ended
= BT_TRUE
;
225 BT_PUT(action
->payload
.set_stream_state_is_ended
.stream_state
->stream
);
227 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
:
228 /* Move packet to stream state's current packet */
229 BT_MOVE(action
->payload
.set_stream_state_cur_packet
.stream_state
->cur_packet
,
230 action
->payload
.set_stream_state_cur_packet
.packet
);
237 clear_actions(iterator
);
241 struct stream_state
*create_stream_state(struct bt_ctf_stream
*stream
)
243 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
250 * We keep a reference to the stream until we know it's ended
251 * because we need to be able to create an automatic "stream
252 * end" notification when the user's "next" method returns
253 * BT_NOTIFICATION_ITERATOR_STATUS_END.
255 * We put this reference when the stream is marked as ended.
257 stream_state
->stream
= bt_get(stream
);
264 void bt_notification_iterator_destroy(struct bt_object
*obj
)
266 struct bt_notification_iterator
*iterator
;
271 * The notification iterator's reference count is 0 if we're
272 * here. Increment it to avoid a double-destroy (possibly
273 * infinitely recursive). This could happen for example if the
274 * notification iterator's finalization function does bt_get()
275 * (or anything that causes bt_get() to be called) on itself
276 * (ref. count goes from 0 to 1), and then bt_put(): the
277 * reference count would go from 1 to 0 again and this function
278 * would be called again.
280 obj
->ref_count
.count
++;
281 iterator
= container_of(obj
, struct bt_notification_iterator
, base
);
282 bt_notification_iterator_finalize(iterator
);
284 if (iterator
->queue
) {
285 struct bt_notification
*notif
;
287 while ((notif
= g_queue_pop_tail(iterator
->queue
))) {
291 g_queue_free(iterator
->queue
);
294 if (iterator
->stream_states
) {
296 * Remove our destroy listener from each stream which
297 * has a state in this iterator. Otherwise the destroy
298 * listener would be called with an invalid/other
299 * notification iterator object.
301 GHashTableIter ht_iter
;
302 gpointer stream_gptr
, stream_state_gptr
;
304 g_hash_table_iter_init(&ht_iter
, iterator
->stream_states
);
306 while (g_hash_table_iter_next(&ht_iter
, &stream_gptr
, &stream_state_gptr
)) {
308 bt_ctf_stream_remove_destroy_listener(
309 (void *) stream_gptr
, stream_destroy_listener
,
313 g_hash_table_destroy(iterator
->stream_states
);
316 if (iterator
->actions
) {
317 g_array_free(iterator
->actions
, TRUE
);
320 if (iterator
->connection
) {
322 * Remove ourself from the originating connection so
323 * that it does not try to finalize a dangling pointer
326 bt_connection_remove_iterator(iterator
->connection
, iterator
);
329 bt_put(iterator
->current_notification
);
334 void bt_notification_iterator_finalize(
335 struct bt_notification_iterator
*iterator
)
337 struct bt_component_class
*comp_class
= NULL
;
338 bt_component_class_notification_iterator_finalize_method
339 finalize_method
= NULL
;
343 switch (iterator
->state
) {
344 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
345 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
346 /* Already finalized */
352 if (iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_ENDED
) {
353 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
355 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
358 assert(iterator
->upstream_component
);
359 comp_class
= iterator
->upstream_component
->class;
361 /* Call user-defined destroy method */
362 switch (comp_class
->type
) {
363 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
365 struct bt_component_class_source
*source_class
;
367 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
368 finalize_method
= source_class
->methods
.iterator
.finalize
;
371 case BT_COMPONENT_CLASS_TYPE_FILTER
:
373 struct bt_component_class_filter
*filter_class
;
375 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
376 finalize_method
= filter_class
->methods
.iterator
.finalize
;
384 if (finalize_method
) {
386 bt_private_notification_iterator_from_notification_iterator(iterator
));
389 iterator
->upstream_component
= NULL
;
390 iterator
->upstream_port
= NULL
;
394 void bt_notification_iterator_set_connection(
395 struct bt_notification_iterator
*iterator
,
396 struct bt_connection
*connection
)
399 iterator
->connection
= connection
;
403 int create_subscription_mask_from_notification_types(
404 struct bt_notification_iterator
*iterator
,
405 const enum bt_notification_type
*notif_types
)
407 const enum bt_notification_type
*notif_type
;
411 iterator
->subscription_mask
= 0;
413 for (notif_type
= notif_types
;
414 *notif_type
!= BT_NOTIFICATION_TYPE_SENTINEL
;
416 switch (*notif_type
) {
417 case BT_NOTIFICATION_TYPE_ALL
:
418 iterator
->subscription_mask
|=
419 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
|
420 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
|
421 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
|
422 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
|
423 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
|
424 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
426 case BT_NOTIFICATION_TYPE_EVENT
:
427 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
;
429 case BT_NOTIFICATION_TYPE_INACTIVITY
:
430 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
;
432 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
433 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
;
435 case BT_NOTIFICATION_TYPE_STREAM_END
:
436 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
;
438 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
439 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
;
441 case BT_NOTIFICATION_TYPE_PACKET_END
:
442 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
455 struct bt_notification_iterator
*bt_notification_iterator_create(
456 struct bt_component
*upstream_comp
,
457 struct bt_port
*upstream_port
,
458 const enum bt_notification_type
*notification_types
,
459 struct bt_connection
*connection
)
461 enum bt_component_class_type type
;
462 struct bt_notification_iterator
*iterator
= NULL
;
464 assert(upstream_comp
);
465 assert(upstream_port
);
466 assert(notification_types
);
467 assert(bt_port_is_connected(upstream_port
));
469 type
= bt_component_get_class_type(upstream_comp
);
471 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
472 case BT_COMPONENT_CLASS_TYPE_FILTER
:
478 iterator
= g_new0(struct bt_notification_iterator
, 1);
483 bt_object_init(iterator
, bt_notification_iterator_destroy
);
485 if (create_subscription_mask_from_notification_types(iterator
,
486 notification_types
)) {
490 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
491 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
492 if (!iterator
->stream_states
) {
496 iterator
->queue
= g_queue_new();
497 if (!iterator
->queue
) {
501 iterator
->actions
= g_array_new(FALSE
, FALSE
, sizeof(struct action
));
502 if (!iterator
->actions
) {
506 iterator
->upstream_component
= upstream_comp
;
507 iterator
->upstream_port
= upstream_port
;
508 iterator
->connection
= connection
;
509 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_ACTIVE
;
520 enum bt_notification_iterator_status
bt_notification_iterator_validate(
521 struct bt_notification_iterator
*iterator
)
523 enum bt_notification_iterator_status ret
=
524 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
527 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
534 void *bt_private_notification_iterator_get_user_data(
535 struct bt_private_notification_iterator
*private_iterator
)
537 struct bt_notification_iterator
*iterator
=
538 bt_notification_iterator_from_private(private_iterator
);
540 return iterator
? iterator
->user_data
: NULL
;
543 enum bt_notification_iterator_status
544 bt_private_notification_iterator_set_user_data(
545 struct bt_private_notification_iterator
*private_iterator
,
548 enum bt_notification_iterator_status ret
=
549 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
550 struct bt_notification_iterator
*iterator
=
551 bt_notification_iterator_from_private(private_iterator
);
554 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
558 iterator
->user_data
= data
;
563 struct bt_notification
*bt_notification_iterator_get_notification(
564 struct bt_notification_iterator
*iterator
)
566 struct bt_notification
*notification
= NULL
;
572 notification
= bt_get(iterator
->current_notification
);
579 enum bt_notification_iterator_notif_type
580 bt_notification_iterator_notif_type_from_notif_type(
581 enum bt_notification_type notif_type
)
583 enum bt_notification_iterator_notif_type iter_notif_type
;
585 switch (notif_type
) {
586 case BT_NOTIFICATION_TYPE_EVENT
:
587 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
;
589 case BT_NOTIFICATION_TYPE_INACTIVITY
:
590 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
;
592 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
593 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
;
595 case BT_NOTIFICATION_TYPE_STREAM_END
:
596 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
;
598 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
599 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
;
601 case BT_NOTIFICATION_TYPE_PACKET_END
:
602 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
608 return iter_notif_type
;
612 bt_bool
validate_notification(struct bt_notification_iterator
*iterator
,
613 struct bt_notification
*notif
,
614 struct bt_ctf_stream
*notif_stream
,
615 struct bt_ctf_packet
*notif_packet
)
617 bt_bool is_valid
= BT_TRUE
;
618 struct stream_state
*stream_state
;
619 struct bt_port
*stream_comp_cur_port
;
621 assert(notif_stream
);
622 stream_comp_cur_port
=
623 bt_ctf_stream_port_for_component(notif_stream
,
624 iterator
->upstream_component
);
625 if (!stream_comp_cur_port
) {
627 * This is the first time this notification iterator
628 * bumps into this stream. Add an action to map the
629 * iterator's upstream component to the iterator's
630 * upstream port in this stream.
632 struct action action
= {
633 .type
= ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
,
634 .payload
.map_port_to_comp_in_stream
= {
635 .stream
= bt_get(notif_stream
),
636 .component
= bt_get(iterator
->upstream_component
),
637 .port
= bt_get(iterator
->upstream_port
),
641 add_action(iterator
, &action
);
643 if (stream_comp_cur_port
!= iterator
->upstream_port
) {
645 * It looks like two different ports of the same
646 * component are emitting notifications which
647 * have references to the same stream. This is
648 * bad: the API guarantees that it can never
657 stream_state
= g_hash_table_lookup(iterator
->stream_states
,
660 if (stream_state
->is_ended
) {
662 * There's a new notification which has a
663 * reference to a stream which, from this
664 * iterator's point of view, is ended ("end of
665 * stream" notification was returned). This is
666 * bad: the API guarantees that it can never
673 switch (notif
->type
) {
674 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
676 * We already have a stream state, which means
677 * we already returned a "stream begin"
678 * notification: this is an invalid duplicate.
682 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
683 if (notif_packet
== stream_state
->cur_packet
) {
684 /* Duplicate "packet begin" notification */
699 bt_bool
is_subscribed_to_notification_type(struct bt_notification_iterator
*iterator
,
700 enum bt_notification_type notif_type
)
702 uint32_t iter_notif_type
=
703 (uint32_t) bt_notification_iterator_notif_type_from_notif_type(
706 return (iter_notif_type
& iterator
->subscription_mask
) ? BT_TRUE
: BT_FALSE
;
710 void add_action_push_notif(struct bt_notification_iterator
*iterator
,
711 struct bt_notification
*notif
)
713 struct action action
= {
714 .type
= ACTION_TYPE_PUSH_NOTIF
,
719 if (!is_subscribed_to_notification_type(iterator
, notif
->type
)) {
723 action
.payload
.push_notif
.notif
= bt_get(notif
);
724 add_action(iterator
, &action
);
728 int add_action_push_notif_stream_begin(
729 struct bt_notification_iterator
*iterator
,
730 struct bt_ctf_stream
*stream
)
733 struct bt_notification
*stream_begin_notif
= NULL
;
735 if (!is_subscribed_to_notification_type(iterator
,
736 BT_NOTIFICATION_TYPE_STREAM_BEGIN
)) {
741 stream_begin_notif
= bt_notification_stream_begin_create(stream
);
742 if (!stream_begin_notif
) {
746 add_action_push_notif(iterator
, stream_begin_notif
);
753 bt_put(stream_begin_notif
);
758 int add_action_push_notif_stream_end(
759 struct bt_notification_iterator
*iterator
,
760 struct bt_ctf_stream
*stream
)
763 struct bt_notification
*stream_end_notif
= NULL
;
765 if (!is_subscribed_to_notification_type(iterator
,
766 BT_NOTIFICATION_TYPE_STREAM_END
)) {
771 stream_end_notif
= bt_notification_stream_end_create(stream
);
772 if (!stream_end_notif
) {
776 add_action_push_notif(iterator
, stream_end_notif
);
783 bt_put(stream_end_notif
);
788 int add_action_push_notif_packet_begin(
789 struct bt_notification_iterator
*iterator
,
790 struct bt_ctf_packet
*packet
)
793 struct bt_notification
*packet_begin_notif
= NULL
;
795 if (!is_subscribed_to_notification_type(iterator
,
796 BT_NOTIFICATION_TYPE_PACKET_BEGIN
)) {
801 packet_begin_notif
= bt_notification_packet_begin_create(packet
);
802 if (!packet_begin_notif
) {
806 add_action_push_notif(iterator
, packet_begin_notif
);
813 bt_put(packet_begin_notif
);
818 int add_action_push_notif_packet_end(
819 struct bt_notification_iterator
*iterator
,
820 struct bt_ctf_packet
*packet
)
823 struct bt_notification
*packet_end_notif
= NULL
;
825 if (!is_subscribed_to_notification_type(iterator
,
826 BT_NOTIFICATION_TYPE_PACKET_END
)) {
831 packet_end_notif
= bt_notification_packet_end_create(packet
);
832 if (!packet_end_notif
) {
836 add_action_push_notif(iterator
, packet_end_notif
);
843 bt_put(packet_end_notif
);
848 void add_action_set_stream_state_is_ended(
849 struct bt_notification_iterator
*iterator
,
850 struct stream_state
*stream_state
)
852 struct action action
= {
853 .type
= ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
,
854 .payload
.set_stream_state_is_ended
= {
855 .stream_state
= stream_state
,
859 assert(stream_state
);
860 add_action(iterator
, &action
);
864 void add_action_set_stream_state_cur_packet(
865 struct bt_notification_iterator
*iterator
,
866 struct stream_state
*stream_state
,
867 struct bt_ctf_packet
*packet
)
869 struct action action
= {
870 .type
= ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
,
871 .payload
.set_stream_state_cur_packet
= {
872 .stream_state
= stream_state
,
873 .packet
= bt_get(packet
),
877 assert(stream_state
);
878 add_action(iterator
, &action
);
882 int ensure_stream_state_exists(struct bt_notification_iterator
*iterator
,
883 struct bt_notification
*stream_begin_notif
,
884 struct bt_ctf_stream
*notif_stream
,
885 struct stream_state
**stream_state
)
891 * The notification does not reference any stream: no
892 * need to get or create a stream state.
897 *stream_state
= g_hash_table_lookup(iterator
->stream_states
,
899 if (!*stream_state
) {
901 * This iterator did not bump into this stream yet:
902 * create a stream state and a "stream begin"
905 struct action action
= {
906 .type
= ACTION_TYPE_ADD_STREAM_STATE
,
907 .payload
.add_stream_state
= {
908 .stream
= bt_get(notif_stream
),
909 .stream_state
= NULL
,
913 *stream_state
= create_stream_state(notif_stream
);
918 action
.payload
.add_stream_state
.stream_state
=
920 add_action(iterator
, &action
);
922 if (stream_begin_notif
) {
923 add_action_push_notif(iterator
, stream_begin_notif
);
925 ret
= add_action_push_notif_stream_begin(iterator
,
936 destroy_stream_state(*stream_state
);
944 int handle_packet_switch(struct bt_notification_iterator
*iterator
,
945 struct bt_notification
*packet_begin_notif
,
946 struct bt_ctf_packet
*new_packet
,
947 struct stream_state
*stream_state
)
951 if (stream_state
->cur_packet
== new_packet
) {
955 if (stream_state
->cur_packet
) {
956 /* End of the current packet */
957 ret
= add_action_push_notif_packet_end(iterator
,
958 stream_state
->cur_packet
);
964 /* Beginning of the new packet */
965 if (packet_begin_notif
) {
966 add_action_push_notif(iterator
, packet_begin_notif
);
967 } else if (new_packet
) {
968 ret
= add_action_push_notif_packet_begin(iterator
,
975 add_action_set_stream_state_cur_packet(iterator
, stream_state
,
987 int handle_notif_stream_begin(
988 struct bt_notification_iterator
*iterator
,
989 struct bt_notification
*notif
,
990 struct bt_ctf_stream
*notif_stream
)
993 struct stream_state
*stream_state
;
995 assert(notif
->type
== BT_NOTIFICATION_TYPE_STREAM_BEGIN
);
996 assert(notif_stream
);
997 ret
= ensure_stream_state_exists(iterator
, notif
, notif_stream
,
1013 int handle_notif_stream_end(
1014 struct bt_notification_iterator
*iterator
,
1015 struct bt_notification
*notif
,
1016 struct bt_ctf_stream
*notif_stream
)
1019 struct stream_state
*stream_state
;
1021 assert(notif
->type
== BT_NOTIFICATION_TYPE_STREAM_END
);
1022 assert(notif_stream
);
1023 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1029 ret
= handle_packet_switch(iterator
, NULL
, NULL
, stream_state
);
1034 add_action_push_notif(iterator
, notif
);
1035 add_action_set_stream_state_is_ended(iterator
, stream_state
);
1046 int handle_notif_packet_begin(
1047 struct bt_notification_iterator
*iterator
,
1048 struct bt_notification
*notif
,
1049 struct bt_ctf_stream
*notif_stream
,
1050 struct bt_ctf_packet
*notif_packet
)
1053 struct stream_state
*stream_state
;
1055 assert(notif
->type
== BT_NOTIFICATION_TYPE_PACKET_BEGIN
);
1056 assert(notif_packet
);
1057 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1063 ret
= handle_packet_switch(iterator
, notif
, notif_packet
, stream_state
);
1078 int handle_notif_packet_end(
1079 struct bt_notification_iterator
*iterator
,
1080 struct bt_notification
*notif
,
1081 struct bt_ctf_stream
*notif_stream
,
1082 struct bt_ctf_packet
*notif_packet
)
1085 struct stream_state
*stream_state
;
1087 assert(notif
->type
== BT_NOTIFICATION_TYPE_PACKET_END
);
1088 assert(notif_packet
);
1089 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1095 ret
= handle_packet_switch(iterator
, NULL
, notif_packet
, stream_state
);
1100 /* End of the current packet */
1101 add_action_push_notif(iterator
, notif
);
1102 add_action_set_stream_state_cur_packet(iterator
, stream_state
, NULL
);
1113 int handle_notif_event(
1114 struct bt_notification_iterator
*iterator
,
1115 struct bt_notification
*notif
,
1116 struct bt_ctf_stream
*notif_stream
,
1117 struct bt_ctf_packet
*notif_packet
)
1120 struct stream_state
*stream_state
;
1122 assert(notif
->type
== BT_NOTIFICATION_TYPE_EVENT
);
1123 assert(notif_packet
);
1124 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1130 ret
= handle_packet_switch(iterator
, NULL
, notif_packet
, stream_state
);
1135 add_action_push_notif(iterator
, notif
);
1146 int enqueue_notification_and_automatic(
1147 struct bt_notification_iterator
*iterator
,
1148 struct bt_notification
*notif
)
1151 struct bt_ctf_event
*notif_event
= NULL
;
1152 struct bt_ctf_stream
*notif_stream
= NULL
;
1153 struct bt_ctf_packet
*notif_packet
= NULL
;
1157 // TODO: Skip most of this if the iterator is only subscribed
1158 // to event/inactivity notifications.
1160 /* Get the stream and packet referred by the notification */
1161 switch (notif
->type
) {
1162 case BT_NOTIFICATION_TYPE_EVENT
:
1163 notif_event
= bt_notification_event_borrow_event(notif
);
1164 assert(notif_event
);
1165 notif_packet
= bt_ctf_event_borrow_packet(notif_event
);
1166 assert(notif_packet
);
1168 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
1170 bt_notification_stream_begin_borrow_stream(notif
);
1171 assert(notif_stream
);
1173 case BT_NOTIFICATION_TYPE_STREAM_END
:
1174 notif_stream
= bt_notification_stream_end_borrow_stream(notif
);
1175 assert(notif_stream
);
1177 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
1179 bt_notification_packet_begin_borrow_packet(notif
);
1180 assert(notif_packet
);
1182 case BT_NOTIFICATION_TYPE_PACKET_END
:
1183 notif_packet
= bt_notification_packet_end_borrow_packet(notif
);
1184 assert(notif_packet
);
1186 case BT_NOTIFICATION_TYPE_INACTIVITY
:
1191 * Invalid type of notification. Only the notification
1192 * types above are allowed to be returned by a user
1199 notif_stream
= bt_ctf_packet_borrow_stream(notif_packet
);
1200 assert(notif_stream
);
1203 if (!notif_stream
) {
1205 * The notification has no reference to a stream: it
1206 * cannot cause the creation of automatic notifications.
1211 if (!validate_notification(iterator
, notif
, notif_stream
,
1217 switch (notif
->type
) {
1218 case BT_NOTIFICATION_TYPE_EVENT
:
1219 ret
= handle_notif_event(iterator
, notif
, notif_stream
,
1222 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
1223 ret
= handle_notif_stream_begin(iterator
, notif
, notif_stream
);
1225 case BT_NOTIFICATION_TYPE_STREAM_END
:
1226 ret
= handle_notif_stream_end(iterator
, notif
, notif_stream
);
1228 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
1229 ret
= handle_notif_packet_begin(iterator
, notif
, notif_stream
,
1232 case BT_NOTIFICATION_TYPE_PACKET_END
:
1233 ret
= handle_notif_packet_end(iterator
, notif
, notif_stream
,
1236 case BT_NOTIFICATION_TYPE_INACTIVITY
:
1237 add_action_push_notif(iterator
, notif
);
1247 apply_actions(iterator
);
1258 int handle_end(struct bt_notification_iterator
*iterator
)
1260 GHashTableIter stream_state_iter
;
1261 gpointer stream_gptr
, stream_state_gptr
;
1265 * Emit a "stream end" notification for each non-ended stream
1266 * known by this iterator and mark them as ended.
1268 g_hash_table_iter_init(&stream_state_iter
, iterator
->stream_states
);
1270 while (g_hash_table_iter_next(&stream_state_iter
, &stream_gptr
,
1271 &stream_state_gptr
)) {
1272 struct stream_state
*stream_state
= stream_state_gptr
;
1274 assert(stream_state_gptr
);
1276 if (stream_state
->is_ended
) {
1280 ret
= handle_packet_switch(iterator
, NULL
, NULL
, stream_state
);
1285 ret
= add_action_push_notif_stream_end(iterator
, stream_gptr
);
1290 add_action_set_stream_state_is_ended(iterator
, stream_state
);
1293 apply_actions(iterator
);
1304 enum bt_notification_iterator_status
ensure_queue_has_notifications(
1305 struct bt_notification_iterator
*iterator
)
1307 struct bt_private_notification_iterator
*priv_iterator
=
1308 bt_private_notification_iterator_from_notification_iterator(iterator
);
1309 bt_component_class_notification_iterator_next_method next_method
= NULL
;
1310 struct bt_notification_iterator_next_return next_return
= {
1311 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
1312 .notification
= NULL
,
1314 enum bt_notification_iterator_status status
=
1315 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
1320 if (iterator
->queue
->length
> 0) {
1321 /* We already have enough */
1325 switch (iterator
->state
) {
1326 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
1327 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
1329 case BT_NOTIFICATION_ITERATOR_STATE_ENDED
:
1330 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
1336 assert(iterator
->upstream_component
);
1337 assert(iterator
->upstream_component
->class);
1339 /* Pick the appropriate "next" method */
1340 switch (iterator
->upstream_component
->class->type
) {
1341 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1343 struct bt_component_class_source
*source_class
=
1344 container_of(iterator
->upstream_component
->class,
1345 struct bt_component_class_source
, parent
);
1347 assert(source_class
->methods
.iterator
.next
);
1348 next_method
= source_class
->methods
.iterator
.next
;
1351 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1353 struct bt_component_class_filter
*filter_class
=
1354 container_of(iterator
->upstream_component
->class,
1355 struct bt_component_class_filter
, parent
);
1357 assert(filter_class
->methods
.iterator
.next
);
1358 next_method
= filter_class
->methods
.iterator
.next
;
1367 * Call the user's "next" method to get the next notification
1370 assert(next_method
);
1372 while (iterator
->queue
->length
== 0) {
1373 next_return
= next_method(priv_iterator
);
1374 if (next_return
.status
< 0) {
1375 status
= next_return
.status
;
1379 switch (next_return
.status
) {
1380 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
1381 ret
= handle_end(iterator
);
1383 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
1387 if (iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
) {
1389 BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
1391 if (iterator
->queue
->length
== 0) {
1392 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
1396 BT_NOTIFICATION_ITERATOR_STATE_ENDED
;
1398 if (iterator
->queue
->length
== 0) {
1399 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
1403 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
1404 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
1406 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
1407 if (!next_return
.notification
) {
1408 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
1413 * We know the notification is valid. Before we
1414 * push it to the head of the queue, push the
1415 * appropriate automatic notifications if any.
1417 ret
= enqueue_notification_and_automatic(iterator
,
1418 next_return
.notification
);
1419 BT_PUT(next_return
.notification
);
1421 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
1426 /* Unknown non-error status */
1435 enum bt_notification_iterator_status
1436 bt_notification_iterator_next(struct bt_notification_iterator
*iterator
)
1438 enum bt_notification_iterator_status status
;
1441 status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
1446 * Make sure that the iterator's queue contains at least one
1449 status
= ensure_queue_has_notifications(iterator
);
1450 if (status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
1455 * Move the notification at the tail of the queue to the
1456 * iterator's current notification.
1458 assert(iterator
->queue
->length
> 0);
1459 bt_put(iterator
->current_notification
);
1460 iterator
->current_notification
= g_queue_pop_tail(iterator
->queue
);
1461 assert(iterator
->current_notification
);
1467 struct bt_component
*bt_notification_iterator_get_component(
1468 struct bt_notification_iterator
*iterator
)
1470 return bt_get(iterator
->upstream_component
);
1473 struct bt_private_component
*
1474 bt_private_notification_iterator_get_private_component(
1475 struct bt_private_notification_iterator
*private_iterator
)
1477 return bt_private_component_from_component(
1478 bt_notification_iterator_get_component(
1479 bt_notification_iterator_from_private(private_iterator
)));
1482 enum bt_notification_iterator_status
bt_notification_iterator_seek_time(
1483 struct bt_notification_iterator
*iterator
,
1484 enum bt_notification_iterator_seek_origin seek_origin
,
1487 enum bt_notification_iterator_status ret
=
1488 BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;