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