Fix: packet sequence number handling and discarded packet reporting
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032
JG
1/*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
5af447e5
PP
28#define BT_LOG_TAG "NOTIF-ITER"
29#include <babeltrace/lib-logging-internal.h>
30
3d9990ac 31#include <babeltrace/compiler-internal.h>
b8a06801 32#include <babeltrace/ref.h>
2ec84d26
PP
33#include <babeltrace/ctf-ir/fields.h>
34#include <babeltrace/ctf-ir/field-types.h>
35#include <babeltrace/ctf-ir/field-types-internal.h>
3230ee6b
PP
36#include <babeltrace/ctf-ir/event-internal.h>
37#include <babeltrace/ctf-ir/packet-internal.h>
38#include <babeltrace/ctf-ir/stream-internal.h>
73d5c1ad 39#include <babeltrace/graph/connection.h>
bd14d768 40#include <babeltrace/graph/connection-internal.h>
b2e0c907
PP
41#include <babeltrace/graph/component.h>
42#include <babeltrace/graph/component-source-internal.h>
43#include <babeltrace/graph/component-class-internal.h>
fa054faf 44#include <babeltrace/graph/notification.h>
b2e0c907
PP
45#include <babeltrace/graph/notification-iterator.h>
46#include <babeltrace/graph/notification-iterator-internal.h>
e7fa96c3 47#include <babeltrace/graph/notification-internal.h>
3230ee6b
PP
48#include <babeltrace/graph/notification-event.h>
49#include <babeltrace/graph/notification-event-internal.h>
50#include <babeltrace/graph/notification-packet.h>
51#include <babeltrace/graph/notification-packet-internal.h>
52#include <babeltrace/graph/notification-stream.h>
53#include <babeltrace/graph/notification-stream-internal.h>
2ec84d26 54#include <babeltrace/graph/notification-discarded-elements-internal.h>
3230ee6b 55#include <babeltrace/graph/port.h>
c55a9f58 56#include <babeltrace/types.h>
fa054faf 57#include <stdint.h>
2ec84d26 58#include <inttypes.h>
0fbb9a9f 59#include <stdlib.h>
3230ee6b 60
2ec84d26
PP
61struct discarded_elements_state {
62 struct bt_ctf_clock_value *cur_begin;
63 uint64_t cur_count;
64};
65
3230ee6b
PP
66struct stream_state {
67 struct bt_ctf_stream *stream; /* owned by this */
68 struct bt_ctf_packet *cur_packet; /* owned by this */
2ec84d26
PP
69 struct discarded_elements_state discarded_packets_state;
70 struct discarded_elements_state discarded_events_state;
c55a9f58 71 bt_bool is_ended;
3230ee6b
PP
72};
73
74enum action_type {
75 ACTION_TYPE_PUSH_NOTIF,
76 ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM,
77 ACTION_TYPE_ADD_STREAM_STATE,
78 ACTION_TYPE_SET_STREAM_STATE_IS_ENDED,
79 ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET,
2ec84d26
PP
80 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS,
81 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS,
3230ee6b
PP
82};
83
84struct action {
85 enum action_type type;
86 union {
87 /* ACTION_TYPE_PUSH_NOTIF */
88 struct {
89 struct bt_notification *notif; /* owned by this */
90 } push_notif;
91
92 /* ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM */
93 struct {
94 struct bt_ctf_stream *stream; /* owned by this */
95 struct bt_component *component; /* owned by this */
96 struct bt_port *port; /* owned by this */
97 } map_port_to_comp_in_stream;
98
99 /* ACTION_TYPE_ADD_STREAM_STATE */
100 struct {
101 struct bt_ctf_stream *stream; /* owned by this */
102 struct stream_state *stream_state; /* owned by this */
103 } add_stream_state;
104
105 /* ACTION_TYPE_SET_STREAM_STATE_IS_ENDED */
106 struct {
107 struct stream_state *stream_state; /* weak */
108 } set_stream_state_is_ended;
109
110 /* ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET */
111 struct {
112 struct stream_state *stream_state; /* weak */
113 struct bt_ctf_packet *packet; /* owned by this */
114 } set_stream_state_cur_packet;
2ec84d26
PP
115
116 /* ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS */
117 /* ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS */
118 struct {
119 struct stream_state *stream_state; /* weak */
120 struct bt_ctf_clock_value *cur_begin; /* owned by this */
121 uint64_t cur_count;
122 } update_stream_state_discarded_elements;
3230ee6b
PP
123 } payload;
124};
125
126static
127void stream_destroy_listener(struct bt_ctf_stream *stream, void *data)
128{
129 struct bt_notification_iterator *iterator = data;
130
131 /* Remove associated stream state */
132 g_hash_table_remove(iterator->stream_states, stream);
133}
134
135static
136void destroy_stream_state(struct stream_state *stream_state)
137{
138 if (!stream_state) {
139 return;
140 }
141
5af447e5
PP
142 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
143 BT_LOGV_STR("Putting stream state's current packet.");
3230ee6b 144 bt_put(stream_state->cur_packet);
5af447e5 145 BT_LOGV_STR("Putting stream state's stream.");
3230ee6b 146 bt_put(stream_state->stream);
2ec84d26
PP
147 bt_put(stream_state->discarded_packets_state.cur_begin);
148 bt_put(stream_state->discarded_events_state.cur_begin);
3230ee6b
PP
149 g_free(stream_state);
150}
151
152static
153void destroy_action(struct action *action)
154{
155 assert(action);
156
157 switch (action->type) {
158 case ACTION_TYPE_PUSH_NOTIF:
159 BT_PUT(action->payload.push_notif.notif);
160 break;
161 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM:
162 BT_PUT(action->payload.map_port_to_comp_in_stream.stream);
163 BT_PUT(action->payload.map_port_to_comp_in_stream.component);
164 BT_PUT(action->payload.map_port_to_comp_in_stream.port);
165 break;
166 case ACTION_TYPE_ADD_STREAM_STATE:
167 BT_PUT(action->payload.add_stream_state.stream);
168 destroy_stream_state(
169 action->payload.add_stream_state.stream_state);
170 action->payload.add_stream_state.stream_state = NULL;
171 break;
172 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET:
173 BT_PUT(action->payload.set_stream_state_cur_packet.packet);
174 break;
175 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED:
176 break;
2ec84d26
PP
177 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS:
178 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS:
179 BT_PUT(action->payload.update_stream_state_discarded_elements.cur_begin);
180 break;
3230ee6b 181 default:
2ec84d26 182 BT_LOGF("Unexpected action's type: type=%d", action->type);
0fbb9a9f 183 abort();
3230ee6b
PP
184 }
185}
186
187static
188void add_action(struct bt_notification_iterator *iterator,
189 struct action *action)
190{
191 g_array_append_val(iterator->actions, *action);
192}
193
194static
195void clear_actions(struct bt_notification_iterator *iterator)
196{
197 size_t i;
198
199 for (i = 0; i < iterator->actions->len; i++) {
200 struct action *action = &g_array_index(iterator->actions,
201 struct action, i);
202
203 destroy_action(action);
204 }
205
206 g_array_set_size(iterator->actions, 0);
207}
208
5af447e5
PP
209static inline
210const char *action_type_string(enum action_type type)
211{
212 switch (type) {
213 case ACTION_TYPE_PUSH_NOTIF:
214 return "ACTION_TYPE_PUSH_NOTIF";
215 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM:
216 return "ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM";
217 case ACTION_TYPE_ADD_STREAM_STATE:
218 return "ACTION_TYPE_ADD_STREAM_STATE";
219 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED:
220 return "ACTION_TYPE_SET_STREAM_STATE_IS_ENDED";
221 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET:
222 return "ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET";
2ec84d26
PP
223 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS:
224 return "ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS";
225 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS:
226 return "ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS";
5af447e5
PP
227 default:
228 return "(unknown)";
229 }
230}
231
3230ee6b
PP
232static
233void apply_actions(struct bt_notification_iterator *iterator)
234{
235 size_t i;
236
5af447e5
PP
237 BT_LOGV("Applying notification's iterator current actions: "
238 "count=%u", iterator->actions->len);
239
3230ee6b
PP
240 for (i = 0; i < iterator->actions->len; i++) {
241 struct action *action = &g_array_index(iterator->actions,
242 struct action, i);
243
5af447e5
PP
244 BT_LOGV("Applying action: index=%zu, type=%s",
245 i, action_type_string(action->type));
246
3230ee6b
PP
247 switch (action->type) {
248 case ACTION_TYPE_PUSH_NOTIF:
249 /* Move notification to queue */
250 g_queue_push_head(iterator->queue,
251 action->payload.push_notif.notif);
252 bt_notification_freeze(
253 action->payload.push_notif.notif);
254 action->payload.push_notif.notif = NULL;
255 break;
256 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM:
257 bt_ctf_stream_map_component_to_port(
258 action->payload.map_port_to_comp_in_stream.stream,
259 action->payload.map_port_to_comp_in_stream.component,
260 action->payload.map_port_to_comp_in_stream.port);
261 break;
262 case ACTION_TYPE_ADD_STREAM_STATE:
263 /* Move stream state to hash table */
264 g_hash_table_insert(iterator->stream_states,
265 action->payload.add_stream_state.stream,
266 action->payload.add_stream_state.stream_state);
267
268 action->payload.add_stream_state.stream_state = NULL;
269 break;
270 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED:
271 /*
272 * We know that this stream is ended. We need to
273 * remember this as long as the stream exists to
274 * enforce that the same stream does not end
275 * twice.
276 *
277 * Here we add a destroy listener to the stream
278 * which we put after (becomes weak as the hash
279 * table key). If we were the last object to own
280 * this stream, the destroy listener is called
281 * when we call bt_put() which removes this
282 * stream state completely. This is important
283 * because the memory used by this stream object
284 * could be reused for another stream, and they
285 * must have different states.
286 */
287 bt_ctf_stream_add_destroy_listener(
288 action->payload.set_stream_state_is_ended.stream_state->stream,
289 stream_destroy_listener, iterator);
c55a9f58 290 action->payload.set_stream_state_is_ended.stream_state->is_ended = BT_TRUE;
3230ee6b
PP
291 BT_PUT(action->payload.set_stream_state_is_ended.stream_state->stream);
292 break;
293 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET:
294 /* Move packet to stream state's current packet */
295 BT_MOVE(action->payload.set_stream_state_cur_packet.stream_state->cur_packet,
296 action->payload.set_stream_state_cur_packet.packet);
297 break;
2ec84d26
PP
298 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS:
299 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS:
300 {
301 struct discarded_elements_state *state;
302
303 if (action->type == ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS) {
304 state = &action->payload.update_stream_state_discarded_elements.stream_state->discarded_packets_state;
305 } else {
306 state = &action->payload.update_stream_state_discarded_elements.stream_state->discarded_events_state;
307 }
308
309 BT_MOVE(state->cur_begin,
310 action->payload.update_stream_state_discarded_elements.cur_begin);
311 state->cur_count = action->payload.update_stream_state_discarded_elements.cur_count;
312 break;
313 }
3230ee6b 314 default:
2ec84d26
PP
315 BT_LOGF("Unexpected action's type: type=%d",
316 action->type);
0fbb9a9f 317 abort();
3230ee6b
PP
318 }
319 }
320
321 clear_actions(iterator);
322}
323
324static
325struct stream_state *create_stream_state(struct bt_ctf_stream *stream)
326{
327 struct stream_state *stream_state = g_new0(struct stream_state, 1);
328
329 if (!stream_state) {
5af447e5 330 BT_LOGE_STR("Failed to allocate one stream state.");
3230ee6b
PP
331 goto end;
332 }
333
126215b4
MD
334 /*
335 * The packet index is a monotonic counter which may not start
336 * at 0 at the beginning of the stream. We therefore need to
337 * have an internal object initial state of -1ULL to distinguish
338 * between initial state and having seen a packet with
339 * seqnum = 0.
340 */
341 stream_state->discarded_packets_state.cur_count = -1ULL;
342
3230ee6b
PP
343 /*
344 * We keep a reference to the stream until we know it's ended
345 * because we need to be able to create an automatic "stream
346 * end" notification when the user's "next" method returns
347 * BT_NOTIFICATION_ITERATOR_STATUS_END.
348 *
349 * We put this reference when the stream is marked as ended.
350 */
351 stream_state->stream = bt_get(stream);
5af447e5
PP
352 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
353 "stream-state-addr=%p",
354 stream, bt_ctf_stream_get_name(stream), stream_state);
3230ee6b
PP
355
356end:
357 return stream_state;
358}
47e5a032
JG
359
360static
b8a06801 361void bt_notification_iterator_destroy(struct bt_object *obj)
47e5a032 362{
8738a040
JG
363 struct bt_notification_iterator *iterator;
364
b8a06801 365 assert(obj);
d3eb6e8f 366
bd14d768
PP
367 /*
368 * The notification iterator's reference count is 0 if we're
369 * here. Increment it to avoid a double-destroy (possibly
370 * infinitely recursive). This could happen for example if the
371 * notification iterator's finalization function does bt_get()
372 * (or anything that causes bt_get() to be called) on itself
373 * (ref. count goes from 0 to 1), and then bt_put(): the
374 * reference count would go from 1 to 0 again and this function
375 * would be called again.
376 */
377 obj->ref_count.count++;
378 iterator = container_of(obj, struct bt_notification_iterator, base);
5af447e5
PP
379 BT_LOGD("Destroying notification iterator object: addr=%p",
380 iterator);
bd14d768 381 bt_notification_iterator_finalize(iterator);
d3eb6e8f 382
3230ee6b
PP
383 if (iterator->queue) {
384 struct bt_notification *notif;
385
5af447e5
PP
386 BT_LOGD("Putting notifications in queue.");
387
3230ee6b
PP
388 while ((notif = g_queue_pop_tail(iterator->queue))) {
389 bt_put(notif);
390 }
391
392 g_queue_free(iterator->queue);
393 }
394
395 if (iterator->stream_states) {
396 /*
397 * Remove our destroy listener from each stream which
398 * has a state in this iterator. Otherwise the destroy
399 * listener would be called with an invalid/other
400 * notification iterator object.
401 */
402 GHashTableIter ht_iter;
403 gpointer stream_gptr, stream_state_gptr;
404
405 g_hash_table_iter_init(&ht_iter, iterator->stream_states);
406
407 while (g_hash_table_iter_next(&ht_iter, &stream_gptr, &stream_state_gptr)) {
408 assert(stream_gptr);
5af447e5
PP
409
410 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
3230ee6b
PP
411 bt_ctf_stream_remove_destroy_listener(
412 (void *) stream_gptr, stream_destroy_listener,
413 iterator);
414 }
415
416 g_hash_table_destroy(iterator->stream_states);
417 }
418
419 if (iterator->actions) {
420 g_array_free(iterator->actions, TRUE);
421 }
422
bd14d768
PP
423 if (iterator->connection) {
424 /*
425 * Remove ourself from the originating connection so
426 * that it does not try to finalize a dangling pointer
427 * later.
428 */
429 bt_connection_remove_iterator(iterator->connection, iterator);
430 }
431
5af447e5 432 BT_LOGD_STR("Putting current notification.");
3230ee6b 433 bt_put(iterator->current_notification);
8738a040 434 g_free(iterator);
47e5a032
JG
435}
436
bd14d768
PP
437BT_HIDDEN
438void bt_notification_iterator_finalize(
439 struct bt_notification_iterator *iterator)
440{
441 struct bt_component_class *comp_class = NULL;
442 bt_component_class_notification_iterator_finalize_method
443 finalize_method = NULL;
444
445 assert(iterator);
446
447 switch (iterator->state) {
448 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED:
449 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
450 /* Already finalized */
5af447e5
PP
451 BT_LOGD("Not finalizing notification iterator: already finalized: "
452 "addr=%p", iterator);
bd14d768
PP
453 return;
454 default:
455 break;
456 }
457
5af447e5
PP
458 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
459
df14f8af 460 if (iterator->state == BT_NOTIFICATION_ITERATOR_STATE_ENDED) {
5af447e5
PP
461 BT_LOGD("Updating notification iterator's state: "
462 "new-state=BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
df14f8af
MD
463 iterator->state = BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
464 } else {
5af447e5
PP
465 BT_LOGD("Updating notification iterator's state: "
466 "new-state=BT_NOTIFICATION_ITERATOR_STATE_FINALIZED");
df14f8af
MD
467 iterator->state = BT_NOTIFICATION_ITERATOR_STATE_FINALIZED;
468 }
469
bd14d768
PP
470 assert(iterator->upstream_component);
471 comp_class = iterator->upstream_component->class;
472
473 /* Call user-defined destroy method */
474 switch (comp_class->type) {
475 case BT_COMPONENT_CLASS_TYPE_SOURCE:
476 {
477 struct bt_component_class_source *source_class;
478
479 source_class = container_of(comp_class, struct bt_component_class_source, parent);
480 finalize_method = source_class->methods.iterator.finalize;
481 break;
482 }
483 case BT_COMPONENT_CLASS_TYPE_FILTER:
484 {
485 struct bt_component_class_filter *filter_class;
486
487 filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
488 finalize_method = filter_class->methods.iterator.finalize;
489 break;
490 }
491 default:
492 /* Unreachable */
0fbb9a9f 493 abort();
bd14d768
PP
494 }
495
496 if (finalize_method) {
5af447e5
PP
497 BT_LOGD("Calling user's finalization method: addr=%p",
498 iterator);
bd14d768
PP
499 finalize_method(
500 bt_private_notification_iterator_from_notification_iterator(iterator));
501 }
502
bd14d768
PP
503 iterator->upstream_component = NULL;
504 iterator->upstream_port = NULL;
5af447e5 505 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
bd14d768
PP
506}
507
508BT_HIDDEN
509void bt_notification_iterator_set_connection(
510 struct bt_notification_iterator *iterator,
511 struct bt_connection *connection)
512{
513 assert(iterator);
514 iterator->connection = connection;
5af447e5
PP
515 BT_LOGV("Set notification iterator's connection: "
516 "iter-addr=%p, conn-addr=%p", iterator, connection);
bd14d768
PP
517}
518
fa054faf
PP
519static
520int create_subscription_mask_from_notification_types(
521 struct bt_notification_iterator *iterator,
522 const enum bt_notification_type *notif_types)
523{
524 const enum bt_notification_type *notif_type;
525 int ret = 0;
526
527 assert(notif_types);
528 iterator->subscription_mask = 0;
529
530 for (notif_type = notif_types;
531 *notif_type != BT_NOTIFICATION_TYPE_SENTINEL;
532 notif_type++) {
533 switch (*notif_type) {
534 case BT_NOTIFICATION_TYPE_ALL:
535 iterator->subscription_mask |=
536 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT |
537 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY |
538 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN |
539 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END |
540 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN |
2ec84d26
PP
541 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END |
542 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS |
543 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS;
fa054faf
PP
544 break;
545 case BT_NOTIFICATION_TYPE_EVENT:
546 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT;
547 break;
548 case BT_NOTIFICATION_TYPE_INACTIVITY:
549 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY;
550 break;
551 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
552 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN;
553 break;
554 case BT_NOTIFICATION_TYPE_STREAM_END:
555 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END;
556 break;
557 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
558 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN;
559 break;
560 case BT_NOTIFICATION_TYPE_PACKET_END:
561 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END;
562 break;
2ec84d26
PP
563 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS:
564 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS;
565 break;
566 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS:
567 iterator->subscription_mask |= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS;
568 break;
fa054faf
PP
569 default:
570 ret = -1;
571 goto end;
572 }
5af447e5
PP
573
574 BT_LOGV("Added notification type to subscription mask: "
575 "type=%s, mask=%x",
576 bt_notification_type_string(*notif_type),
577 iterator->subscription_mask);
fa054faf
PP
578 }
579
580end:
581 return ret;
582}
583
47e5a032 584BT_HIDDEN
73d5c1ad 585enum bt_connection_status bt_notification_iterator_create(
3230ee6b 586 struct bt_component *upstream_comp,
fa054faf 587 struct bt_port *upstream_port,
bd14d768 588 const enum bt_notification_type *notification_types,
73d5c1ad
PP
589 struct bt_connection *connection,
590 struct bt_notification_iterator **user_iterator)
47e5a032 591{
73d5c1ad 592 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
d3e4dcd8 593 enum bt_component_class_type type;
47e5a032
JG
594 struct bt_notification_iterator *iterator = NULL;
595
3230ee6b
PP
596 assert(upstream_comp);
597 assert(upstream_port);
fa054faf 598 assert(notification_types);
3230ee6b 599 assert(bt_port_is_connected(upstream_port));
73d5c1ad 600 assert(user_iterator);
5af447e5
PP
601 BT_LOGD("Creating notification iterator: "
602 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
603 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
604 "conn-addr=%p",
605 upstream_comp, bt_component_get_name(upstream_comp),
606 upstream_port, bt_port_get_name(upstream_port),
607 connection);
3230ee6b 608 type = bt_component_get_class_type(upstream_comp);
ef2f7566
PP
609 assert(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
610 type == BT_COMPONENT_CLASS_TYPE_FILTER);
47e5a032
JG
611 iterator = g_new0(struct bt_notification_iterator, 1);
612 if (!iterator) {
5af447e5 613 BT_LOGE_STR("Failed to allocate one notification iterator.");
73d5c1ad
PP
614 status = BT_CONNECTION_STATUS_NOMEM;
615 goto end;
47e5a032
JG
616 }
617
b8a06801 618 bt_object_init(iterator, bt_notification_iterator_destroy);
3230ee6b 619
fa054faf
PP
620 if (create_subscription_mask_from_notification_types(iterator,
621 notification_types)) {
5af447e5 622 BT_LOGW_STR("Cannot create subscription mask from notification types.");
73d5c1ad
PP
623 status = BT_CONNECTION_STATUS_INVALID;
624 goto end;
fa054faf
PP
625 }
626
3230ee6b
PP
627 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
628 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
629 if (!iterator->stream_states) {
5af447e5 630 BT_LOGE_STR("Failed to allocate a GHashTable.");
73d5c1ad
PP
631 status = BT_CONNECTION_STATUS_NOMEM;
632 goto end;
3230ee6b
PP
633 }
634
635 iterator->queue = g_queue_new();
636 if (!iterator->queue) {
5af447e5 637 BT_LOGE_STR("Failed to allocate a GQueue.");
73d5c1ad
PP
638 status = BT_CONNECTION_STATUS_NOMEM;
639 goto end;
3230ee6b
PP
640 }
641
642 iterator->actions = g_array_new(FALSE, FALSE, sizeof(struct action));
643 if (!iterator->actions) {
5af447e5 644 BT_LOGE_STR("Failed to allocate a GArray.");
73d5c1ad
PP
645 status = BT_CONNECTION_STATUS_NOMEM;
646 goto end;
3230ee6b
PP
647 }
648
bd14d768
PP
649 iterator->upstream_component = upstream_comp;
650 iterator->upstream_port = upstream_port;
651 iterator->connection = connection;
652 iterator->state = BT_NOTIFICATION_ITERATOR_STATE_ACTIVE;
5af447e5
PP
653 BT_LOGD("Created notification iterator: "
654 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
655 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
656 "conn-addr=%p, iter-addr=%p",
657 upstream_comp, bt_component_get_name(upstream_comp),
658 upstream_port, bt_port_get_name(upstream_port),
659 connection, iterator);
1a6a376a
PP
660
661 /* Move reference to user */
662 *user_iterator = iterator;
663 iterator = NULL;
3230ee6b 664
47e5a032 665end:
73d5c1ad
PP
666 bt_put(iterator);
667 return status;
47e5a032
JG
668}
669
890882ef
PP
670void *bt_private_notification_iterator_get_user_data(
671 struct bt_private_notification_iterator *private_iterator)
ea8d3e58 672{
890882ef
PP
673 struct bt_notification_iterator *iterator =
674 bt_notification_iterator_from_private(private_iterator);
675
ea8d3e58
JG
676 return iterator ? iterator->user_data : NULL;
677}
678
679enum bt_notification_iterator_status
890882ef
PP
680bt_private_notification_iterator_set_user_data(
681 struct bt_private_notification_iterator *private_iterator,
682 void *data)
ea8d3e58
JG
683{
684 enum bt_notification_iterator_status ret =
685 BT_NOTIFICATION_ITERATOR_STATUS_OK;
890882ef
PP
686 struct bt_notification_iterator *iterator =
687 bt_notification_iterator_from_private(private_iterator);
ea8d3e58 688
d3eb6e8f 689 if (!iterator) {
5af447e5 690 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
fe8ad2b6 691 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
ea8d3e58
JG
692 goto end;
693 }
694
695 iterator->user_data = data;
5af447e5
PP
696 BT_LOGV("Set notification iterator's user data: "
697 "iter-addr=%p, user-data-addr=%p", iterator, data);
698
8738a040
JG
699end:
700 return ret;
701}
413bc2c4 702
53d45b87
JG
703struct bt_notification *bt_notification_iterator_get_notification(
704 struct bt_notification_iterator *iterator)
705{
41a2b7ae 706 struct bt_notification *notification = NULL;
d3eb6e8f 707
41a2b7ae 708 if (!iterator) {
5af447e5 709 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
41a2b7ae 710 goto end;
d3eb6e8f 711 }
d3eb6e8f 712
41a2b7ae 713 notification = bt_get(iterator->current_notification);
d3eb6e8f 714
41a2b7ae
PP
715end:
716 return notification;
53d45b87
JG
717}
718
fa054faf
PP
719static
720enum bt_notification_iterator_notif_type
721bt_notification_iterator_notif_type_from_notif_type(
722 enum bt_notification_type notif_type)
723{
724 enum bt_notification_iterator_notif_type iter_notif_type;
725
726 switch (notif_type) {
727 case BT_NOTIFICATION_TYPE_EVENT:
728 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT;
729 break;
730 case BT_NOTIFICATION_TYPE_INACTIVITY:
731 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY;
732 break;
733 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
734 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN;
735 break;
736 case BT_NOTIFICATION_TYPE_STREAM_END:
737 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END;
738 break;
739 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
740 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN;
741 break;
742 case BT_NOTIFICATION_TYPE_PACKET_END:
743 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END;
744 break;
2ec84d26
PP
745 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS:
746 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS;
747 break;
748 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS:
749 iter_notif_type = BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS;
750 break;
fa054faf 751 default:
0fbb9a9f 752 abort();
fa054faf
PP
753 }
754
755 return iter_notif_type;
756}
757
3230ee6b 758static
c55a9f58 759bt_bool validate_notification(struct bt_notification_iterator *iterator,
3230ee6b
PP
760 struct bt_notification *notif,
761 struct bt_ctf_stream *notif_stream,
762 struct bt_ctf_packet *notif_packet)
763{
c55a9f58 764 bt_bool is_valid = BT_TRUE;
3230ee6b
PP
765 struct stream_state *stream_state;
766 struct bt_port *stream_comp_cur_port;
767
768 assert(notif_stream);
769 stream_comp_cur_port =
770 bt_ctf_stream_port_for_component(notif_stream,
771 iterator->upstream_component);
772 if (!stream_comp_cur_port) {
773 /*
774 * This is the first time this notification iterator
775 * bumps into this stream. Add an action to map the
776 * iterator's upstream component to the iterator's
777 * upstream port in this stream.
778 */
779 struct action action = {
780 .type = ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM,
781 .payload.map_port_to_comp_in_stream = {
782 .stream = bt_get(notif_stream),
783 .component = bt_get(iterator->upstream_component),
784 .port = bt_get(iterator->upstream_port),
785 },
786 };
787
788 add_action(iterator, &action);
789 } else {
790 if (stream_comp_cur_port != iterator->upstream_port) {
791 /*
792 * It looks like two different ports of the same
793 * component are emitting notifications which
794 * have references to the same stream. This is
795 * bad: the API guarantees that it can never
796 * happen.
797 */
5af447e5
PP
798 BT_LOGW("Two different ports of the same component are emitting notifications which refer to the same stream: "
799 "stream-addr=%p, stream-name=\"%s\", "
800 "stream-comp-cur-port-addr=%p, "
801 "stream-comp-cur-port-name=%p, "
802 "iter-upstream-port-addr=%p, "
803 "iter-upstream-port-name=%s",
804 notif_stream,
805 bt_ctf_stream_get_name(notif_stream),
806 stream_comp_cur_port,
807 bt_port_get_name(stream_comp_cur_port),
808 iterator->upstream_port,
809 bt_port_get_name(iterator->upstream_port));
c55a9f58 810 is_valid = BT_FALSE;
3230ee6b
PP
811 goto end;
812 }
813
814 }
815
816 stream_state = g_hash_table_lookup(iterator->stream_states,
817 notif_stream);
818 if (stream_state) {
5af447e5
PP
819 BT_LOGV("Stream state already exists: "
820 "stream-addr=%p, stream-name=\"%s\", "
821 "stream-state-addr=%p",
822 notif_stream,
823 bt_ctf_stream_get_name(notif_stream), stream_state);
824
3230ee6b
PP
825 if (stream_state->is_ended) {
826 /*
827 * There's a new notification which has a
828 * reference to a stream which, from this
829 * iterator's point of view, is ended ("end of
830 * stream" notification was returned). This is
831 * bad: the API guarantees that it can never
832 * happen.
833 */
5af447e5
PP
834 BT_LOGW("Stream is already ended: "
835 "stream-addr=%p, stream-name=\"%s\"",
836 notif_stream,
837 bt_ctf_stream_get_name(notif_stream));
c55a9f58 838 is_valid = BT_FALSE;
3230ee6b
PP
839 goto end;
840 }
841
842 switch (notif->type) {
843 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
844 /*
845 * We already have a stream state, which means
846 * we already returned a "stream begin"
847 * notification: this is an invalid duplicate.
848 */
5af447e5
PP
849 BT_LOGW("Duplicate stream beginning notification: "
850 "stream-addr=%p, stream-name=\"%s\"",
851 notif_stream,
852 bt_ctf_stream_get_name(notif_stream));
c55a9f58 853 is_valid = BT_FALSE;
3230ee6b
PP
854 goto end;
855 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
856 if (notif_packet == stream_state->cur_packet) {
857 /* Duplicate "packet begin" notification */
5af447e5
PP
858 BT_LOGW("Duplicate stream beginning notification: "
859 "stream-addr=%p, stream-name=\"%s\", "
860 "packet-addr=%p",
861 notif_stream,
862 bt_ctf_stream_get_name(notif_stream),
863 notif_packet);
c55a9f58 864 is_valid = BT_FALSE;
3230ee6b
PP
865 goto end;
866 }
867 break;
868 default:
869 break;
870 }
871 }
872
873end:
874 return is_valid;
875}
876
fa054faf 877static
c55a9f58 878bt_bool is_subscribed_to_notification_type(struct bt_notification_iterator *iterator,
fa054faf
PP
879 enum bt_notification_type notif_type)
880{
881 uint32_t iter_notif_type =
882 (uint32_t) bt_notification_iterator_notif_type_from_notif_type(
883 notif_type);
884
c55a9f58 885 return (iter_notif_type & iterator->subscription_mask) ? BT_TRUE : BT_FALSE;
fa054faf
PP
886}
887
3230ee6b
PP
888static
889void add_action_push_notif(struct bt_notification_iterator *iterator,
890 struct bt_notification *notif)
891{
892 struct action action = {
893 .type = ACTION_TYPE_PUSH_NOTIF,
3230ee6b
PP
894 };
895
896 assert(notif);
fa054faf
PP
897
898 if (!is_subscribed_to_notification_type(iterator, notif->type)) {
899 return;
900 }
901
902 action.payload.push_notif.notif = bt_get(notif);
3230ee6b 903 add_action(iterator, &action);
5af447e5 904 BT_LOGV("Added \"push notification\" action: notif-addr=%p", notif);
3230ee6b
PP
905}
906
907static
908int add_action_push_notif_stream_begin(
909 struct bt_notification_iterator *iterator,
910 struct bt_ctf_stream *stream)
911{
912 int ret = 0;
913 struct bt_notification *stream_begin_notif = NULL;
914
fa054faf
PP
915 if (!is_subscribed_to_notification_type(iterator,
916 BT_NOTIFICATION_TYPE_STREAM_BEGIN)) {
5af447e5
PP
917 BT_LOGV("Not adding \"push stream beginning notification\" action: "
918 "notification iterator is not subscribed: addr=%p",
919 iterator);
fa054faf
PP
920 goto end;
921 }
922
3230ee6b
PP
923 assert(stream);
924 stream_begin_notif = bt_notification_stream_begin_create(stream);
925 if (!stream_begin_notif) {
5af447e5 926 BT_LOGE_STR("Cannot create stream beginning notification.");
3230ee6b
PP
927 goto error;
928 }
929
930 add_action_push_notif(iterator, stream_begin_notif);
5af447e5
PP
931 BT_LOGV("Added \"push stream beginning notification\" action: "
932 "stream-addr=%p, stream-name=\"%s\"",
933 stream, bt_ctf_stream_get_name(stream));
3230ee6b
PP
934 goto end;
935
936error:
937 ret = -1;
938
939end:
940 bt_put(stream_begin_notif);
941 return ret;
942}
943
944static
945int add_action_push_notif_stream_end(
946 struct bt_notification_iterator *iterator,
947 struct bt_ctf_stream *stream)
948{
949 int ret = 0;
950 struct bt_notification *stream_end_notif = NULL;
951
fa054faf
PP
952 if (!is_subscribed_to_notification_type(iterator,
953 BT_NOTIFICATION_TYPE_STREAM_END)) {
5af447e5
PP
954 BT_LOGV("Not adding \"push stream end notification\" action: "
955 "notification iterator is not subscribed: addr=%p",
956 iterator);
fa054faf
PP
957 goto end;
958 }
959
3230ee6b
PP
960 assert(stream);
961 stream_end_notif = bt_notification_stream_end_create(stream);
962 if (!stream_end_notif) {
5af447e5 963 BT_LOGE_STR("Cannot create stream end notification.");
3230ee6b
PP
964 goto error;
965 }
966
967 add_action_push_notif(iterator, stream_end_notif);
5af447e5
PP
968 BT_LOGV("Added \"push stream end notification\" action: "
969 "stream-addr=%p, stream-name=\"%s\"",
970 stream, bt_ctf_stream_get_name(stream));
3230ee6b
PP
971 goto end;
972
973error:
974 ret = -1;
975
976end:
977 bt_put(stream_end_notif);
978 return ret;
979}
980
981static
982int add_action_push_notif_packet_begin(
983 struct bt_notification_iterator *iterator,
984 struct bt_ctf_packet *packet)
985{
986 int ret = 0;
987 struct bt_notification *packet_begin_notif = NULL;
988
fa054faf
PP
989 if (!is_subscribed_to_notification_type(iterator,
990 BT_NOTIFICATION_TYPE_PACKET_BEGIN)) {
5af447e5
PP
991 BT_LOGV("Not adding \"push packet beginning notification\" action: "
992 "notification iterator is not subscribed: addr=%p",
993 iterator);
fa054faf
PP
994 goto end;
995 }
996
3230ee6b
PP
997 assert(packet);
998 packet_begin_notif = bt_notification_packet_begin_create(packet);
999 if (!packet_begin_notif) {
5af447e5 1000 BT_LOGE_STR("Cannot create packet beginning notification.");
3230ee6b
PP
1001 goto error;
1002 }
1003
1004 add_action_push_notif(iterator, packet_begin_notif);
5af447e5
PP
1005 BT_LOGV("Added \"push packet beginning notification\" action: "
1006 "packet-addr=%p", packet);
3230ee6b
PP
1007 goto end;
1008
1009error:
1010 ret = -1;
1011
1012end:
1013 bt_put(packet_begin_notif);
1014 return ret;
1015}
1016
1017static
1018int add_action_push_notif_packet_end(
1019 struct bt_notification_iterator *iterator,
1020 struct bt_ctf_packet *packet)
1021{
1022 int ret = 0;
1023 struct bt_notification *packet_end_notif = NULL;
1024
fa054faf
PP
1025 if (!is_subscribed_to_notification_type(iterator,
1026 BT_NOTIFICATION_TYPE_PACKET_END)) {
5af447e5
PP
1027 BT_LOGV("Not adding \"push packet end notification\" action: "
1028 "notification iterator is not subscribed: addr=%p",
1029 iterator);
fa054faf
PP
1030 goto end;
1031 }
1032
3230ee6b
PP
1033 assert(packet);
1034 packet_end_notif = bt_notification_packet_end_create(packet);
1035 if (!packet_end_notif) {
5af447e5 1036 BT_LOGE_STR("Cannot create packet end notification.");
3230ee6b
PP
1037 goto error;
1038 }
1039
1040 add_action_push_notif(iterator, packet_end_notif);
5af447e5
PP
1041 BT_LOGV("Added \"push packet end notification\" action: "
1042 "packet-addr=%p", packet);
3230ee6b
PP
1043 goto end;
1044
1045error:
1046 ret = -1;
1047
1048end:
1049 bt_put(packet_end_notif);
1050 return ret;
1051}
1052
1053static
1054void add_action_set_stream_state_is_ended(
1055 struct bt_notification_iterator *iterator,
1056 struct stream_state *stream_state)
1057{
1058 struct action action = {
1059 .type = ACTION_TYPE_SET_STREAM_STATE_IS_ENDED,
1060 .payload.set_stream_state_is_ended = {
1061 .stream_state = stream_state,
1062 },
1063 };
1064
1065 assert(stream_state);
1066 add_action(iterator, &action);
5af447e5
PP
1067 BT_LOGV("Added \"set stream state's ended\" action: "
1068 "stream-state-addr=%p", stream_state);
3230ee6b
PP
1069}
1070
1071static
1072void add_action_set_stream_state_cur_packet(
1073 struct bt_notification_iterator *iterator,
1074 struct stream_state *stream_state,
1075 struct bt_ctf_packet *packet)
1076{
1077 struct action action = {
1078 .type = ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET,
1079 .payload.set_stream_state_cur_packet = {
1080 .stream_state = stream_state,
1081 .packet = bt_get(packet),
1082 },
1083 };
1084
1085 assert(stream_state);
1086 add_action(iterator, &action);
5af447e5
PP
1087 BT_LOGV("Added \"set stream state's current packet\" action: "
1088 "stream-state-addr=%p, packet-addr=%p",
1089 stream_state, packet);
3230ee6b
PP
1090}
1091
2ec84d26
PP
1092static
1093void add_action_update_stream_state_discarded_elements(
1094 struct bt_notification_iterator *iterator,
1095 enum action_type type,
1096 struct stream_state *stream_state,
1097 struct bt_ctf_clock_value *cur_begin,
1098 uint64_t cur_count)
1099{
1100 struct action action = {
1101 .type = type,
1102 .payload.update_stream_state_discarded_elements = {
1103 .stream_state = stream_state,
1104 .cur_begin = bt_get(cur_begin),
1105 .cur_count = cur_count,
1106 },
1107 };
1108
1109 assert(stream_state);
1110 assert(type == ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS ||
1111 type == ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS);
1112 add_action(iterator, &action);
1113 if (type == ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS) {
1114 BT_LOGV("Added \"update stream state's discarded packets\" action: "
1115 "stream-state-addr=%p, cur-begin-addr=%p, cur-count=%" PRIu64,
1116 stream_state, cur_begin, cur_count);
1117 } else if (type == ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS) {
1118 BT_LOGV("Added \"update stream state's discarded events\" action: "
1119 "stream-state-addr=%p, cur-begin-addr=%p, cur-count=%" PRIu64,
1120 stream_state, cur_begin, cur_count);
1121 }
1122}
1123
3230ee6b
PP
1124static
1125int ensure_stream_state_exists(struct bt_notification_iterator *iterator,
1126 struct bt_notification *stream_begin_notif,
1127 struct bt_ctf_stream *notif_stream,
1128 struct stream_state **stream_state)
1129{
1130 int ret = 0;
1131
1132 if (!notif_stream) {
1133 /*
1134 * The notification does not reference any stream: no
1135 * need to get or create a stream state.
1136 */
1137 goto end;
1138 }
1139
1140 *stream_state = g_hash_table_lookup(iterator->stream_states,
1141 notif_stream);
1142 if (!*stream_state) {
1143 /*
1144 * This iterator did not bump into this stream yet:
1145 * create a stream state and a "stream begin"
1146 * notification.
1147 */
1148 struct action action = {
1149 .type = ACTION_TYPE_ADD_STREAM_STATE,
1150 .payload.add_stream_state = {
1151 .stream = bt_get(notif_stream),
1152 .stream_state = NULL,
1153 },
1154 };
1155
1156 *stream_state = create_stream_state(notif_stream);
1157 if (!stream_state) {
5af447e5 1158 BT_LOGE_STR("Cannot create stream state.");
3230ee6b
PP
1159 goto error;
1160 }
1161
1162 action.payload.add_stream_state.stream_state =
1163 *stream_state;
1164 add_action(iterator, &action);
1165
1166 if (stream_begin_notif) {
1167 add_action_push_notif(iterator, stream_begin_notif);
1168 } else {
1169 ret = add_action_push_notif_stream_begin(iterator,
1170 notif_stream);
1171 if (ret) {
5af447e5 1172 BT_LOGE_STR("Cannot add \"push stream beginning notification\" action.");
3230ee6b
PP
1173 goto error;
1174 }
1175 }
1176 }
1177
1178 goto end;
1179
1180error:
1181 destroy_stream_state(*stream_state);
1182 ret = -1;
1183
1184end:
1185 return ret;
1186}
1187
2ec84d26
PP
1188static
1189struct bt_ctf_field *get_struct_field_uint(struct bt_ctf_field *struct_field,
1190 const char *field_name)
1191{
1192 struct bt_ctf_field *field = NULL;
1193 struct bt_ctf_field_type *ft = NULL;
1194
1195 field = bt_ctf_field_structure_get_field_by_name(struct_field,
1196 field_name);
1197 if (!field) {
1198 BT_LOGV_STR("`%s` field does not exist.");
1199 goto end;
1200 }
1201
1202 if (!bt_ctf_field_is_integer(field)) {
1203 BT_LOGV("Skipping `%s` field because its type is not an integer field type: "
1204 "field-addr=%p, ft-addr=%p, ft-id=%s", field_name,
1205 field, ft, bt_ctf_field_type_id_string(
1206 bt_ctf_field_type_get_type_id(ft)));
1207 BT_PUT(field);
1208 goto end;
1209 }
1210
1211 ft = bt_ctf_field_get_type(field);
1212 assert(ft);
1213
1214 if (bt_ctf_field_type_integer_is_signed(ft)) {
1215 BT_LOGV("Skipping `%s` integer field because its type is signed: "
1216 "field-addr=%p, ft-addr=%p", field_name, field, ft);
1217 BT_PUT(field);
1218 goto end;
1219 }
1220
1221end:
1222 bt_put(ft);
1223 return field;
1224}
1225
1226static
1227uint64_t get_packet_context_events_discarded(struct bt_ctf_packet *packet)
1228{
1229 struct bt_ctf_field *packet_context = NULL;
1230 struct bt_ctf_field *field = NULL;
1231 uint64_t retval = -1ULL;
1232 int ret;
1233
1234 packet_context = bt_ctf_packet_get_context(packet);
1235 if (!packet_context) {
1236 goto end;
1237 }
1238
1239 field = get_struct_field_uint(packet_context, "events_discarded");
1240 if (!field) {
1241 BT_LOGV("`events_discarded` field does not exist in packet's context field: "
1242 "packet-addr=%p, packet-context-field-addr=%p",
1243 packet, packet_context);
1244 goto end;
1245 }
1246
1247 assert(bt_ctf_field_is_integer(field));
1248 ret = bt_ctf_field_unsigned_integer_get_value(field, &retval);
1249 if (ret) {
1250 BT_LOGV("Cannot get raw value of packet's context field's `events_discarded` integer field: "
1251 "packet-addr=%p, field-addr=%p",
1252 packet, field);
1253 retval = -1ULL;
1254 goto end;
1255 }
1256
1257end:
1258 bt_put(packet_context);
1259 bt_put(field);
1260 return retval;
1261}
1262
1263static
126215b4 1264uint64_t get_packet_context_packet_seq_num(struct bt_ctf_packet *packet)
2ec84d26 1265{
126215b4 1266 struct bt_ctf_field *packet_context = NULL;
2ec84d26
PP
1267 struct bt_ctf_field *field = NULL;
1268 uint64_t retval = -1ULL;
1269 int ret;
1270
126215b4
MD
1271 packet_context = bt_ctf_packet_get_context(packet);
1272 if (!packet_context) {
2ec84d26
PP
1273 goto end;
1274 }
1275
126215b4 1276 field = get_struct_field_uint(packet_context, "packet_seq_num");
2ec84d26 1277 if (!field) {
126215b4
MD
1278 BT_LOGV("`packet_seq_num` field does not exist in packet's context field: "
1279 "packet-addr=%p, packet-context-field-addr=%p",
1280 packet, packet_context);
2ec84d26
PP
1281 goto end;
1282 }
1283
1284 assert(bt_ctf_field_is_integer(field));
1285 ret = bt_ctf_field_unsigned_integer_get_value(field, &retval);
1286 if (ret) {
126215b4 1287 BT_LOGV("Cannot get raw value of packet's context field's `packet_seq_num` integer field: "
2ec84d26
PP
1288 "packet-addr=%p, field-addr=%p",
1289 packet, field);
1290 retval = -1ULL;
1291 goto end;
1292 }
1293
1294end:
126215b4 1295 bt_put(packet_context);
2ec84d26
PP
1296 bt_put(field);
1297 return retval;
1298}
1299
1300static
1301int handle_discarded_packets(struct bt_notification_iterator *iterator,
1302 struct bt_ctf_packet *packet,
1303 struct bt_ctf_clock_value *ts_begin,
1304 struct bt_ctf_clock_value *ts_end,
1305 struct stream_state *stream_state)
1306{
1307 struct bt_notification *notif = NULL;
1308 uint64_t diff;
126215b4 1309 uint64_t prev_count, next_count;
2ec84d26
PP
1310 int ret = 0;
1311
126215b4 1312 next_count = get_packet_context_packet_seq_num(packet);
2ec84d26 1313 if (next_count == -1ULL) {
126215b4
MD
1314 /*
1315 * Stream does not have seqnum field, skip discarded
1316 * packets feature.
1317 */
2ec84d26
PP
1318 goto end;
1319 }
126215b4 1320 prev_count = stream_state->discarded_packets_state.cur_count;
2ec84d26 1321
126215b4
MD
1322 if (prev_count != -1ULL) {
1323 if (next_count < prev_count) {
1324 BT_LOGW("Current value of packet's context field's `packet_seq_num` field is lesser than the previous value for the same stream: "
1325 "not updating the stream state's current value: "
1326 "packet-addr=%p, prev-count=%" PRIu64 ", "
1327 "cur-count=%" PRIu64,
1328 packet, prev_count, next_count);
1329 goto end;
1330 }
1331 if (next_count == prev_count) {
1332 BT_LOGW("Current value of packet's context field's `packet_seq_num` field is equal to the previous value for the same stream: "
1333 "not updating the stream state's current value: "
1334 "packet-addr=%p, prev-count=%" PRIu64 ", "
1335 "cur-count=%" PRIu64,
1336 packet, prev_count, next_count);
2ec84d26
PP
1337 goto end;
1338 }
1339
126215b4
MD
1340 diff = next_count - prev_count;
1341 if (diff > 1) {
1342 /*
1343 * Add a discarded packets notification. The packets
1344 * are considered to be lost between the state's last time
1345 * and the current packet's beginning time.
1346 * The counter is expected to monotonically increase of
1347 * 1 for each packet. Therefore, the number of missing
1348 * packets is 'diff - 1'.
1349 */
1350 notif = bt_notification_discarded_elements_create(
1351 BT_NOTIFICATION_TYPE_DISCARDED_PACKETS,
1352 stream_state->stream,
1353 stream_state->discarded_packets_state.cur_begin,
1354 ts_begin, diff - 1);
1355 if (!notif) {
1356 BT_LOGE_STR("Cannot create discarded packets notification.");
1357 ret = -1;
1358 goto end;
1359 }
1360
1361 add_action_push_notif(iterator, notif);
1362 }
2ec84d26
PP
1363 }
1364
2ec84d26
PP
1365 add_action_update_stream_state_discarded_elements(iterator,
1366 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS,
1367 stream_state, ts_end, next_count);
1368
1369end:
1370 bt_put(notif);
1371 return ret;
1372}
1373
1374static
1375int handle_discarded_events(struct bt_notification_iterator *iterator,
1376 struct bt_ctf_packet *packet,
1377 struct bt_ctf_clock_value *ts_begin,
1378 struct bt_ctf_clock_value *ts_end,
1379 struct stream_state *stream_state)
1380{
1381 struct bt_notification *notif = NULL;
1382 uint64_t diff;
1383 uint64_t next_count;
1384 int ret = 0;
1385
1386 next_count = get_packet_context_events_discarded(packet);
1387 if (next_count == -1ULL) {
1388 next_count = stream_state->discarded_events_state.cur_count;
1389 goto update_state;
1390 }
1391
1392 if (next_count < stream_state->discarded_events_state.cur_count) {
1393 BT_LOGW("Current value of packet's context field's `events_discarded` field is lesser than the previous value for the same stream: "
1394 "not updating the stream state's current value: "
1395 "packet-addr=%p, prev-count=%" PRIu64 ", "
1396 "cur-count=%" PRIu64,
1397 packet, stream_state->discarded_events_state.cur_count,
1398 next_count);
1399 goto end;
1400 }
1401
1402 diff = next_count - stream_state->discarded_events_state.cur_count;
1403 if (diff > 0) {
1404 /*
1405 * Add a discarded events notification. The events are
1406 * considered to be lost betweem the state's last time
1407 * and the current packet's end time.
1408 */
1409 notif = bt_notification_discarded_elements_create(
1410 BT_NOTIFICATION_TYPE_DISCARDED_EVENTS,
1411 stream_state->stream,
1412 stream_state->discarded_events_state.cur_begin,
1413 ts_end, diff);
1414 if (!notif) {
1415 BT_LOGE_STR("Cannot create discarded events notification.");
1416 ret = -1;
1417 goto end;
1418 }
1419
1420 add_action_push_notif(iterator, notif);
1421 }
1422
1423update_state:
1424 add_action_update_stream_state_discarded_elements(iterator,
1425 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS,
1426 stream_state, ts_end, next_count);
1427
1428end:
1429 bt_put(notif);
1430 return ret;
1431}
1432
1433static
1434int get_field_clock_value(struct bt_ctf_field *root_field,
1435 const char *field_name,
1436 struct bt_ctf_clock_value **user_clock_val)
1437{
1438 struct bt_ctf_field *field;
1439 struct bt_ctf_field_type *ft = NULL;
1440 struct bt_ctf_clock_class *clock_class = NULL;
1441 struct bt_ctf_clock_value *clock_value = NULL;
1442 uint64_t val;
1443 int ret = 0;
1444
1445 field = get_struct_field_uint(root_field, field_name);
1446 if (!field) {
1447 /* Not an error: skip this */
1448 goto end;
1449 }
1450
1451 ft = bt_ctf_field_get_type(field);
1452 assert(ft);
1453 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(ft);
1454 if (!clock_class) {
1455 BT_LOGW("Integer field type has no mapped clock class but it's expected to have one: "
1456 "ft-addr=%p", ft);
1457 ret = -1;
1458 goto end;
1459 }
1460
1461 ret = bt_ctf_field_unsigned_integer_get_value(field, &val);
1462 if (ret) {
1463 BT_LOGW("Cannot get integer field's raw value: "
1464 "field-addr=%p", field);
1465 ret = -1;
1466 goto end;
1467 }
1468
1469 clock_value = bt_ctf_clock_value_create(clock_class, val);
1470 if (!clock_value) {
1471 BT_LOGE_STR("Cannot create clock value from clock class.");
1472 ret = -1;
1473 goto end;
1474 }
1475
1476 /* Move clock value to user */
1477 *user_clock_val = clock_value;
1478 clock_value = NULL;
1479
1480end:
1481 bt_put(field);
1482 bt_put(ft);
1483 bt_put(clock_class);
1484 bt_put(clock_value);
1485 return ret;
1486}
1487
1488static
1489int get_ts_begin_ts_end_from_packet(struct bt_ctf_packet *packet,
1490 struct bt_ctf_clock_value **user_ts_begin,
1491 struct bt_ctf_clock_value **user_ts_end)
1492{
1493 struct bt_ctf_field *packet_context = NULL;
1494 struct bt_ctf_clock_value *ts_begin = NULL;
1495 struct bt_ctf_clock_value *ts_end = NULL;
1496 int ret = 0;
1497
1498 packet_context = bt_ctf_packet_get_context(packet);
1499 if (!packet_context) {
1500 goto end;
1501 }
1502
1503 ret = get_field_clock_value(packet_context, "timestamp_begin",
1504 &ts_begin);
1505 if (ret) {
1506 BT_LOGW("Cannot create clock value for packet context's `timestamp_begin` field: "
1507 "packet-addr=%p, packet-context-field-addr=%p",
1508 packet, packet_context);
1509 goto end;
1510 }
1511
1512 ret = get_field_clock_value(packet_context, "timestamp_end",
1513 &ts_end);
1514 if (ret) {
1515 BT_LOGW("Cannot create clock value for packet context's `timestamp_begin` field: "
1516 "packet-addr=%p, packet-context-field-addr=%p",
1517 packet, packet_context);
1518 goto end;
1519 }
1520
1521 /* Move clock values to user */
1522 *user_ts_begin = ts_begin;
1523 ts_begin = NULL;
1524 *user_ts_end = ts_end;
1525 ts_end = NULL;
1526
1527end:
1528 bt_put(packet_context);
1529 bt_put(ts_begin);
1530 bt_put(ts_end);
1531 return ret;
1532}
1533
1534static
1535int handle_discarded_elements(struct bt_notification_iterator *iterator,
1536 struct bt_ctf_packet *packet, struct stream_state *stream_state)
1537{
1538 struct bt_ctf_clock_value *ts_begin = NULL;
1539 struct bt_ctf_clock_value *ts_end = NULL;
1540 int ret;
1541
1542 ret = get_ts_begin_ts_end_from_packet(packet, &ts_begin, &ts_end);
1543 if (ret) {
1544 BT_LOGW("Cannot get packet's beginning or end clock values: "
1545 "packet-addr=%p, ret=%d", packet, ret);
1546 ret = -1;
1547 goto end;
1548 }
1549
1550 ret = handle_discarded_packets(iterator, packet, ts_begin, ts_end,
1551 stream_state);
1552 if (ret) {
1553 BT_LOGW("Cannot handle discarded packets for packet: "
1554 "packet-addr=%p, ret=%d", packet, ret);
1555 ret = -1;
1556 goto end;
1557 }
1558
1559 ret = handle_discarded_events(iterator, packet, ts_begin, ts_end,
1560 stream_state);
1561 if (ret) {
1562 BT_LOGW("Cannot handle discarded events for packet: "
1563 "packet-addr=%p, ret=%d", packet, ret);
1564 ret = -1;
1565 goto end;
1566 }
1567
1568end:
1569 bt_put(ts_begin);
1570 bt_put(ts_end);
1571 return ret;
1572}
1573
3230ee6b
PP
1574static
1575int handle_packet_switch(struct bt_notification_iterator *iterator,
1576 struct bt_notification *packet_begin_notif,
1577 struct bt_ctf_packet *new_packet,
1578 struct stream_state *stream_state)
1579{
1580 int ret = 0;
1581
1582 if (stream_state->cur_packet == new_packet) {
1583 goto end;
1584 }
1585
5af447e5
PP
1586 BT_LOGV("Handling packet switch: "
1587 "cur-packet-addr=%p, new-packet-addr=%p",
1588 stream_state->cur_packet, new_packet);
1589
3230ee6b
PP
1590 if (stream_state->cur_packet) {
1591 /* End of the current packet */
1592 ret = add_action_push_notif_packet_end(iterator,
1593 stream_state->cur_packet);
1594 if (ret) {
5af447e5 1595 BT_LOGE_STR("Cannot add \"push packet end notification\" action.");
3230ee6b
PP
1596 goto error;
1597 }
1598 }
1599
2ec84d26 1600 /*
126215b4
MD
1601 * Check the new packet's context fields for discarded packets
1602 * and events to emit those automatic notifications.
2ec84d26
PP
1603 */
1604 ret = handle_discarded_elements(iterator, new_packet, stream_state);
1605 if (ret) {
1606 BT_LOGE_STR("Cannot handle discarded elements for new packet.");
1607 goto error;
1608 }
1609
3230ee6b
PP
1610 /* Beginning of the new packet */
1611 if (packet_begin_notif) {
1612 add_action_push_notif(iterator, packet_begin_notif);
1613 } else if (new_packet) {
1614 ret = add_action_push_notif_packet_begin(iterator,
1615 new_packet);
1616 if (ret) {
5af447e5 1617 BT_LOGE_STR("Cannot add \"push packet beginning notification\" action.");
3230ee6b
PP
1618 goto error;
1619 }
1620 }
1621
1622 add_action_set_stream_state_cur_packet(iterator, stream_state,
1623 new_packet);
1624 goto end;
1625
1626error:
1627 ret = -1;
1628
1629end:
1630 return ret;
1631}
1632
1633static
1634int handle_notif_stream_begin(
1635 struct bt_notification_iterator *iterator,
1636 struct bt_notification *notif,
1637 struct bt_ctf_stream *notif_stream)
1638{
1639 int ret = 0;
1640 struct stream_state *stream_state;
1641
1642 assert(notif->type == BT_NOTIFICATION_TYPE_STREAM_BEGIN);
1643 assert(notif_stream);
1644 ret = ensure_stream_state_exists(iterator, notif, notif_stream,
1645 &stream_state);
1646 if (ret) {
5af447e5 1647 BT_LOGE_STR("Cannot ensure that stream state exists.");
3230ee6b
PP
1648 goto error;
1649 }
1650
1651 goto end;
1652
1653error:
1654 ret = -1;
1655
1656end:
1657 return ret;
1658}
1659
1660static
1661int handle_notif_stream_end(
1662 struct bt_notification_iterator *iterator,
1663 struct bt_notification *notif,
1664 struct bt_ctf_stream *notif_stream)
1665{
1666 int ret = 0;
1667 struct stream_state *stream_state;
1668
1669 assert(notif->type == BT_NOTIFICATION_TYPE_STREAM_END);
1670 assert(notif_stream);
1671 ret = ensure_stream_state_exists(iterator, NULL, notif_stream,
1672 &stream_state);
1673 if (ret) {
5af447e5 1674 BT_LOGE_STR("Cannot ensure that stream state exists.");
3230ee6b
PP
1675 goto error;
1676 }
1677
1678 ret = handle_packet_switch(iterator, NULL, NULL, stream_state);
1679 if (ret) {
5af447e5 1680 BT_LOGE_STR("Cannot handle packet switch.");
3230ee6b
PP
1681 goto error;
1682 }
1683
1684 add_action_push_notif(iterator, notif);
1685 add_action_set_stream_state_is_ended(iterator, stream_state);
1686 goto end;
1687
1688error:
1689 ret = -1;
1690
1691end:
1692 return ret;
1693}
1694
2ec84d26
PP
1695static
1696int handle_notif_discarded_elements(
1697 struct bt_notification_iterator *iterator,
1698 struct bt_notification *notif,
1699 struct bt_ctf_stream *notif_stream)
1700{
1701 int ret = 0;
1702 struct stream_state *stream_state;
1703
1704 assert(notif->type == BT_NOTIFICATION_TYPE_DISCARDED_EVENTS ||
1705 notif->type == BT_NOTIFICATION_TYPE_DISCARDED_PACKETS);
1706 assert(notif_stream);
1707 ret = ensure_stream_state_exists(iterator, NULL, notif_stream,
1708 &stream_state);
1709 if (ret) {
1710 BT_LOGE_STR("Cannot ensure that stream state exists.");
1711 goto error;
1712 }
1713
1714 add_action_push_notif(iterator, notif);
1715 goto end;
1716
1717error:
1718 ret = -1;
1719
1720end:
1721 return ret;
1722}
1723
3230ee6b
PP
1724static
1725int handle_notif_packet_begin(
1726 struct bt_notification_iterator *iterator,
1727 struct bt_notification *notif,
1728 struct bt_ctf_stream *notif_stream,
1729 struct bt_ctf_packet *notif_packet)
1730{
1731 int ret = 0;
1732 struct stream_state *stream_state;
1733
1734 assert(notif->type == BT_NOTIFICATION_TYPE_PACKET_BEGIN);
1735 assert(notif_packet);
1736 ret = ensure_stream_state_exists(iterator, NULL, notif_stream,
1737 &stream_state);
1738 if (ret) {
5af447e5 1739 BT_LOGE_STR("Cannot ensure that stream state exists.");
3230ee6b
PP
1740 goto error;
1741 }
1742
1743 ret = handle_packet_switch(iterator, notif, notif_packet, stream_state);
1744 if (ret) {
5af447e5 1745 BT_LOGE_STR("Cannot handle packet switch.");
3230ee6b
PP
1746 goto error;
1747 }
1748
1749 goto end;
1750
1751error:
1752 ret = -1;
1753
1754end:
1755 return ret;
1756}
1757
1758static
1759int handle_notif_packet_end(
1760 struct bt_notification_iterator *iterator,
1761 struct bt_notification *notif,
1762 struct bt_ctf_stream *notif_stream,
1763 struct bt_ctf_packet *notif_packet)
1764{
1765 int ret = 0;
1766 struct stream_state *stream_state;
1767
1768 assert(notif->type == BT_NOTIFICATION_TYPE_PACKET_END);
1769 assert(notif_packet);
1770 ret = ensure_stream_state_exists(iterator, NULL, notif_stream,
1771 &stream_state);
1772 if (ret) {
5af447e5 1773 BT_LOGE_STR("Cannot ensure that stream state exists.");
3230ee6b
PP
1774 goto error;
1775 }
1776
1777 ret = handle_packet_switch(iterator, NULL, notif_packet, stream_state);
1778 if (ret) {
5af447e5 1779 BT_LOGE_STR("Cannot handle packet switch.");
3230ee6b
PP
1780 goto error;
1781 }
1782
1783 /* End of the current packet */
1784 add_action_push_notif(iterator, notif);
1785 add_action_set_stream_state_cur_packet(iterator, stream_state, NULL);
1786 goto end;
1787
1788error:
1789 ret = -1;
1790
1791end:
1792 return ret;
1793}
1794
1795static
1796int handle_notif_event(
1797 struct bt_notification_iterator *iterator,
1798 struct bt_notification *notif,
1799 struct bt_ctf_stream *notif_stream,
1800 struct bt_ctf_packet *notif_packet)
1801{
1802 int ret = 0;
1803 struct stream_state *stream_state;
1804
1805 assert(notif->type == BT_NOTIFICATION_TYPE_EVENT);
1806 assert(notif_packet);
1807 ret = ensure_stream_state_exists(iterator, NULL, notif_stream,
1808 &stream_state);
1809 if (ret) {
5af447e5 1810 BT_LOGE_STR("Cannot ensure that stream state exists.");
3230ee6b
PP
1811 goto error;
1812 }
1813
1814 ret = handle_packet_switch(iterator, NULL, notif_packet, stream_state);
1815 if (ret) {
5af447e5 1816 BT_LOGE_STR("Cannot handle packet switch.");
3230ee6b
PP
1817 goto error;
1818 }
1819
1820 add_action_push_notif(iterator, notif);
1821 goto end;
1822
1823error:
1824 ret = -1;
1825
1826end:
1827 return ret;
1828}
1829
1830static
1831int enqueue_notification_and_automatic(
1832 struct bt_notification_iterator *iterator,
1833 struct bt_notification *notif)
1834{
1835 int ret = 0;
1836 struct bt_ctf_event *notif_event = NULL;
1837 struct bt_ctf_stream *notif_stream = NULL;
1838 struct bt_ctf_packet *notif_packet = NULL;
1839
1840 assert(notif);
1841
5af447e5
PP
1842 BT_LOGV("Enqueuing user notification and automatic notifications: "
1843 "iter-addr=%p, notif-addr=%p", iterator, notif);
1844
fa054faf
PP
1845 // TODO: Skip most of this if the iterator is only subscribed
1846 // to event/inactivity notifications.
1847
3230ee6b
PP
1848 /* Get the stream and packet referred by the notification */
1849 switch (notif->type) {
1850 case BT_NOTIFICATION_TYPE_EVENT:
1851 notif_event = bt_notification_event_borrow_event(notif);
1852 assert(notif_event);
1853 notif_packet = bt_ctf_event_borrow_packet(notif_event);
1854 assert(notif_packet);
1855 break;
1856 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
1857 notif_stream =
1858 bt_notification_stream_begin_borrow_stream(notif);
1859 assert(notif_stream);
1860 break;
1861 case BT_NOTIFICATION_TYPE_STREAM_END:
1862 notif_stream = bt_notification_stream_end_borrow_stream(notif);
1863 assert(notif_stream);
1864 break;
1865 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
1866 notif_packet =
1867 bt_notification_packet_begin_borrow_packet(notif);
1868 assert(notif_packet);
1869 break;
1870 case BT_NOTIFICATION_TYPE_PACKET_END:
1871 notif_packet = bt_notification_packet_end_borrow_packet(notif);
1872 assert(notif_packet);
1873 break;
1874 case BT_NOTIFICATION_TYPE_INACTIVITY:
1875 /* Always valid */
7cdc2bab 1876 goto handle_notif;
3230ee6b
PP
1877 default:
1878 /*
1879 * Invalid type of notification. Only the notification
1880 * types above are allowed to be returned by a user
1881 * component.
1882 */
2ec84d26
PP
1883 BT_LOGF("Unexpected notification type at this point: "
1884 "notif-addr=%p, notif-type=%s", notif,
1885 bt_notification_type_string(notif->type));
1886 abort();
3230ee6b
PP
1887 }
1888
1889 if (notif_packet) {
1890 notif_stream = bt_ctf_packet_borrow_stream(notif_packet);
1891 assert(notif_stream);
1892 }
1893
1894 if (!notif_stream) {
1895 /*
1896 * The notification has no reference to a stream: it
1897 * cannot cause the creation of automatic notifications.
1898 */
5af447e5 1899 BT_LOGV_STR("Notification has no reference to any stream: skipping automatic notification generation.");
3230ee6b
PP
1900 goto end;
1901 }
1902
1903 if (!validate_notification(iterator, notif, notif_stream,
1904 notif_packet)) {
5af447e5 1905 BT_LOGW_STR("Invalid notification.");
3230ee6b
PP
1906 goto error;
1907 }
1908
7cdc2bab 1909handle_notif:
3230ee6b
PP
1910 switch (notif->type) {
1911 case BT_NOTIFICATION_TYPE_EVENT:
1912 ret = handle_notif_event(iterator, notif, notif_stream,
1913 notif_packet);
1914 break;
1915 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
1916 ret = handle_notif_stream_begin(iterator, notif, notif_stream);
1917 break;
1918 case BT_NOTIFICATION_TYPE_STREAM_END:
1919 ret = handle_notif_stream_end(iterator, notif, notif_stream);
1920 break;
1921 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
1922 ret = handle_notif_packet_begin(iterator, notif, notif_stream,
1923 notif_packet);
1924 break;
1925 case BT_NOTIFICATION_TYPE_PACKET_END:
1926 ret = handle_notif_packet_end(iterator, notif, notif_stream,
1927 notif_packet);
1928 break;
2ec84d26
PP
1929 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS:
1930 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS:
1931 ret = handle_notif_discarded_elements(iterator, notif,
1932 notif_stream);
1933 break;
7cdc2bab
MD
1934 case BT_NOTIFICATION_TYPE_INACTIVITY:
1935 add_action_push_notif(iterator, notif);
1936 break;
3230ee6b
PP
1937 default:
1938 break;
1939 }
1940
1941 if (ret) {
5af447e5 1942 BT_LOGW_STR("Failed to handle notification for automatic notification generation.");
3230ee6b
PP
1943 goto error;
1944 }
1945
1946 apply_actions(iterator);
5af447e5
PP
1947 BT_LOGV("Enqueued user notification and automatic notifications: "
1948 "iter-addr=%p, notif-addr=%p", iterator, notif);
3230ee6b
PP
1949 goto end;
1950
1951error:
1952 ret = -1;
1953
1954end:
1955 return ret;
1956}
1957
1958static
1959int handle_end(struct bt_notification_iterator *iterator)
1960{
1961 GHashTableIter stream_state_iter;
1962 gpointer stream_gptr, stream_state_gptr;
1963 int ret = 0;
1964
5af447e5
PP
1965 BT_LOGV("Handling end of iteration: addr=%p", iterator);
1966
3230ee6b
PP
1967 /*
1968 * Emit a "stream end" notification for each non-ended stream
1969 * known by this iterator and mark them as ended.
1970 */
1971 g_hash_table_iter_init(&stream_state_iter, iterator->stream_states);
1972
1973 while (g_hash_table_iter_next(&stream_state_iter, &stream_gptr,
1974 &stream_state_gptr)) {
1975 struct stream_state *stream_state = stream_state_gptr;
1976
1977 assert(stream_state_gptr);
1978
1979 if (stream_state->is_ended) {
1980 continue;
1981 }
1982
1983 ret = handle_packet_switch(iterator, NULL, NULL, stream_state);
1984 if (ret) {
5af447e5 1985 BT_LOGE_STR("Cannot handle packet switch.");
3230ee6b
PP
1986 goto error;
1987 }
1988
1989 ret = add_action_push_notif_stream_end(iterator, stream_gptr);
1990 if (ret) {
5af447e5 1991 BT_LOGE_STR("Cannot add \"push stream end notification\" action.");
3230ee6b
PP
1992 goto error;
1993 }
1994
1995 add_action_set_stream_state_is_ended(iterator, stream_state);
1996 }
1997
1998 apply_actions(iterator);
5af447e5 1999 BT_LOGV("Handled end of iteration: addr=%p", iterator);
3230ee6b
PP
2000 goto end;
2001
2002error:
2003 ret = -1;
2004
2005end:
2006 return ret;
2007}
2008
2009static
2010enum bt_notification_iterator_status ensure_queue_has_notifications(
2011 struct bt_notification_iterator *iterator)
53d45b87 2012{
890882ef
PP
2013 struct bt_private_notification_iterator *priv_iterator =
2014 bt_private_notification_iterator_from_notification_iterator(iterator);
d3eb6e8f 2015 bt_component_class_notification_iterator_next_method next_method = NULL;
fe8ad2b6
PP
2016 struct bt_notification_iterator_next_return next_return = {
2017 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
2018 .notification = NULL,
2019 };
3230ee6b
PP
2020 enum bt_notification_iterator_status status =
2021 BT_NOTIFICATION_ITERATOR_STATUS_OK;
2022 int ret;
41a2b7ae 2023
3230ee6b 2024 assert(iterator);
5af447e5 2025 BT_LOGD("Ensuring that notification iterator's queue has at least one notification: "
8f9d7550
PP
2026 "iter-addr=%p, queue-size=%u, iter-state=%s",
2027 iterator, iterator->queue->length,
2028 bt_notification_iterator_state_string(iterator->state));
3230ee6b
PP
2029
2030 if (iterator->queue->length > 0) {
8f9d7550
PP
2031 /*
2032 * We already have enough. Even if this notification
2033 * iterator is finalized, its user can still flush its
2034 * current queue's content by calling its "next" method
2035 * since this content is local and has no impact on what
2036 * used to be the iterator's upstream component.
2037 */
5af447e5 2038 BT_LOGD_STR("Queue already has at least one notification.");
3230ee6b
PP
2039 goto end;
2040 }
2041
bd14d768
PP
2042 switch (iterator->state) {
2043 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
8f9d7550 2044 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED:
5af447e5 2045 BT_LOGD_STR("Notification iterator's \"next\" called, but it is finalized.");
bd14d768
PP
2046 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
2047 goto end;
2048 case BT_NOTIFICATION_ITERATOR_STATE_ENDED:
5af447e5 2049 BT_LOGD_STR("Notification iterator is ended.");
3230ee6b 2050 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
41a2b7ae 2051 goto end;
bd14d768
PP
2052 default:
2053 break;
41a2b7ae 2054 }
d3eb6e8f 2055
3230ee6b
PP
2056 assert(iterator->upstream_component);
2057 assert(iterator->upstream_component->class);
d3eb6e8f 2058
3230ee6b
PP
2059 /* Pick the appropriate "next" method */
2060 switch (iterator->upstream_component->class->type) {
d3eb6e8f
PP
2061 case BT_COMPONENT_CLASS_TYPE_SOURCE:
2062 {
2063 struct bt_component_class_source *source_class =
3230ee6b 2064 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
2065 struct bt_component_class_source, parent);
2066
2067 assert(source_class->methods.iterator.next);
2068 next_method = source_class->methods.iterator.next;
2069 break;
2070 }
2071 case BT_COMPONENT_CLASS_TYPE_FILTER:
2072 {
2073 struct bt_component_class_filter *filter_class =
3230ee6b 2074 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
2075 struct bt_component_class_filter, parent);
2076
2077 assert(filter_class->methods.iterator.next);
2078 next_method = filter_class->methods.iterator.next;
2079 break;
2080 }
2081 default:
0fbb9a9f 2082 abort();
d3eb6e8f
PP
2083 }
2084
3230ee6b
PP
2085 /*
2086 * Call the user's "next" method to get the next notification
fa054faf 2087 * and status.
3230ee6b 2088 */
d3eb6e8f 2089 assert(next_method);
3230ee6b 2090
fa054faf 2091 while (iterator->queue->length == 0) {
5af447e5 2092 BT_LOGD_STR("Calling user's \"next\" method.");
fa054faf 2093 next_return = next_method(priv_iterator);
5af447e5
PP
2094 BT_LOGD("User method returned: status=%s",
2095 bt_notification_iterator_status_string(next_return.status));
fa054faf 2096 if (next_return.status < 0) {
5af447e5 2097 BT_LOGW_STR("User method failed.");
fa054faf 2098 status = next_return.status;
3230ee6b
PP
2099 goto end;
2100 }
2101
8cf27cc5
PP
2102 if (iterator->state == BT_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
2103 iterator->state == BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
2104 /*
2105 * The user's "next" method, somehow, cancelled
2106 * its own notification iterator. This can
2107 * happen, for example, when the user's method
2108 * removes the port on which there's the
2109 * connection from which the iterator was
2110 * created. In this case, said connection is
2111 * ended, and all its notification iterators are
2112 * finalized.
2113 *
2114 * Only bt_put() the returned notification if
2115 * the status is
2116 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
2117 * otherwise this field could be garbage.
2118 */
2119 if (next_return.status ==
2120 BT_NOTIFICATION_ITERATOR_STATUS_OK) {
2121 bt_put(next_return.notification);
2122 }
2123
2124 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
2125 goto end;
2126 }
2127
fa054faf
PP
2128 switch (next_return.status) {
2129 case BT_NOTIFICATION_ITERATOR_STATUS_END:
2130 ret = handle_end(iterator);
2131 if (ret) {
5af447e5 2132 BT_LOGW_STR("Cannot handle end of iteration.");
fa054faf
PP
2133 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
2134 goto end;
2135 }
3230ee6b 2136
8f9d7550
PP
2137 assert(iterator->state ==
2138 BT_NOTIFICATION_ITERATOR_STATE_ACTIVE);
2139 iterator->state = BT_NOTIFICATION_ITERATOR_STATE_ENDED;
bd14d768 2140
8f9d7550
PP
2141 if (iterator->queue->length == 0) {
2142 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
bd14d768 2143 }
5af447e5
PP
2144
2145 BT_LOGD("Set new status: status=%s",
2146 bt_notification_iterator_status_string(status));
3230ee6b 2147 goto end;
fa054faf
PP
2148 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
2149 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
2150 goto end;
2151 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
2152 if (!next_return.notification) {
5af447e5 2153 BT_LOGW_STR("User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL.");
fa054faf
PP
2154 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
2155 goto end;
2156 }
2157
2ec84d26
PP
2158 /*
2159 * Ignore some notifications which are always
2160 * automatically generated by the notification
2161 * iterator to make sure they have valid values.
2162 */
2163 switch (next_return.notification->type) {
2164 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS:
2165 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS:
2166 BT_LOGV("Ignoring discarded elements notification returned by notification iterator's \"next\" method: "
2167 "notif-type=%s",
2168 bt_notification_type_string(next_return.notification->type));
2169 BT_PUT(next_return.notification);
2170 continue;
2171 default:
2172 break;
2173 }
2174
fa054faf
PP
2175 /*
2176 * We know the notification is valid. Before we
2177 * push it to the head of the queue, push the
2178 * appropriate automatic notifications if any.
2179 */
2180 ret = enqueue_notification_and_automatic(iterator,
2181 next_return.notification);
2182 BT_PUT(next_return.notification);
2183 if (ret) {
5af447e5 2184 BT_LOGW("Cannot enqueue notification and automatic notifications.");
fa054faf
PP
2185 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
2186 goto end;
2187 }
2188 break;
2189 default:
2190 /* Unknown non-error status */
0fbb9a9f 2191 abort();
3230ee6b 2192 }
41a2b7ae
PP
2193 }
2194
2195end:
3230ee6b
PP
2196 return status;
2197}
2198
2199enum bt_notification_iterator_status
2200bt_notification_iterator_next(struct bt_notification_iterator *iterator)
2201{
2202 enum bt_notification_iterator_status status;
2203
2204 if (!iterator) {
5af447e5 2205 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
3230ee6b
PP
2206 status = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
2207 goto end;
2208 }
2209
5af447e5
PP
2210 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator);
2211
3230ee6b
PP
2212 /*
2213 * Make sure that the iterator's queue contains at least one
2214 * notification.
2215 */
2216 status = ensure_queue_has_notifications(iterator);
2217 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
5af447e5 2218 /* Not an error */
3230ee6b
PP
2219 goto end;
2220 }
2221
2222 /*
2223 * Move the notification at the tail of the queue to the
2224 * iterator's current notification.
2225 */
2226 assert(iterator->queue->length > 0);
2227 bt_put(iterator->current_notification);
2228 iterator->current_notification = g_queue_pop_tail(iterator->queue);
2229 assert(iterator->current_notification);
2230
2231end:
2232 return status;
53d45b87
JG
2233}
2234
413bc2c4
JG
2235struct bt_component *bt_notification_iterator_get_component(
2236 struct bt_notification_iterator *iterator)
2237{
3230ee6b 2238 return bt_get(iterator->upstream_component);
413bc2c4
JG
2239}
2240
91457551
PP
2241struct bt_private_component *
2242bt_private_notification_iterator_get_private_component(
2243 struct bt_private_notification_iterator *private_iterator)
2244{
2245 return bt_private_component_from_component(
2246 bt_notification_iterator_get_component(
2247 bt_notification_iterator_from_private(private_iterator)));
2248}
2249
9531634f
JG
2250enum bt_notification_iterator_status bt_notification_iterator_seek_time(
2251 struct bt_notification_iterator *iterator,
2252 enum bt_notification_iterator_seek_origin seek_origin,
2253 int64_t time)
2254{
b7726e32
MD
2255 enum bt_notification_iterator_status ret =
2256 BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED;
9531634f
JG
2257 return ret;
2258}
This page took 0.139007 seconds and 4 git commands to generate.