- switch (iterator->state) {
- case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
- case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED:
- BT_LOGD_STR("Notification iterator's \"next\" called, but it is finalized.");
- status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
- goto end;
- case BT_NOTIFICATION_ITERATOR_STATE_ENDED:
- BT_LOGD_STR("Notification iterator is ended.");
- status = BT_NOTIFICATION_ITERATOR_STATUS_END;
- goto end;
- default:
- break;
- }
-
- assert(iterator->upstream_component);
- assert(iterator->upstream_component->class);
-
- /* Pick the appropriate "next" method */
- switch (iterator->upstream_component->class->type) {
- case BT_COMPONENT_CLASS_TYPE_SOURCE:
- {
- struct bt_component_class_source *source_class =
- container_of(iterator->upstream_component->class,
- struct bt_component_class_source, parent);
-
- assert(source_class->methods.iterator.next);
- next_method = source_class->methods.iterator.next;
- break;
- }
- case BT_COMPONENT_CLASS_TYPE_FILTER:
- {
- struct bt_component_class_filter *filter_class =
- container_of(iterator->upstream_component->class,
- struct bt_component_class_filter, parent);
-
- assert(filter_class->methods.iterator.next);
- next_method = filter_class->methods.iterator.next;
- break;
- }
- default:
- abort();
- }
-
- /*
- * Call the user's "next" method to get the next notification
- * and status.
- */
- assert(next_method);
-
- while (iterator->queue->length == 0) {
- BT_LOGD_STR("Calling user's \"next\" method.");
- next_return = next_method(priv_iterator);
- BT_LOGD("User method returned: status=%s",
- bt_notification_iterator_status_string(next_return.status));
- if (next_return.status < 0) {
- BT_LOGW_STR("User method failed.");
- status = next_return.status;
- goto end;
- }
-
- if (iterator->state == BT_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
- iterator->state == BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
- /*
- * The user's "next" method, somehow, cancelled
- * its own notification iterator. This can
- * happen, for example, when the user's method
- * removes the port on which there's the
- * connection from which the iterator was
- * created. In this case, said connection is
- * ended, and all its notification iterators are
- * finalized.
- *
- * Only bt_put() the returned notification if
- * the status is
- * BT_NOTIFICATION_ITERATOR_STATUS_OK because
- * otherwise this field could be garbage.
- */
- if (next_return.status ==
- BT_NOTIFICATION_ITERATOR_STATUS_OK) {
- bt_put(next_return.notification);
- }
-
- status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
- goto end;
- }
-
- switch (next_return.status) {
- case BT_NOTIFICATION_ITERATOR_STATUS_END:
- ret = handle_end(iterator);
- if (ret) {
- BT_LOGW_STR("Cannot handle end of iteration.");
- status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
- goto end;
- }
-
- assert(iterator->state ==
- BT_NOTIFICATION_ITERATOR_STATE_ACTIVE);
- iterator->state = BT_NOTIFICATION_ITERATOR_STATE_ENDED;
-
- if (iterator->queue->length == 0) {
- status = BT_NOTIFICATION_ITERATOR_STATUS_END;
- }
-
- BT_LOGD("Set new status: status=%s",
- bt_notification_iterator_status_string(status));
- goto end;
- case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
- status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
- goto end;
- case BT_NOTIFICATION_ITERATOR_STATUS_OK:
- if (!next_return.notification) {
- BT_LOGW_STR("User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL.");
- status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
- goto end;
- }
-
- /*
- * We know the notification is valid. Before we
- * push it to the head of the queue, push the
- * appropriate automatic notifications if any.
- */
- ret = enqueue_notification_and_automatic(iterator,
- next_return.notification);
- BT_PUT(next_return.notification);
- if (ret) {
- BT_LOGW("Cannot enqueue notification and automatic notifications.");
- status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
- goto end;
- }
- break;
- default:
- /* Unknown non-error status */
- abort();
- }
- }
-
-end:
- return status;
-}
-
-enum bt_notification_iterator_status
-bt_notification_iterator_next(struct bt_notification_iterator *iterator)
-{
- enum bt_notification_iterator_status status;
-
- if (!iterator) {
- BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
- status = BT_NOTIFICATION_ITERATOR_STATUS_INVALID;
- goto end;
- }
-
- BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator);
-
- /*
- * Make sure that the iterator's queue contains at least one
- * notification.
- */
- status = ensure_queue_has_notifications(iterator);
- if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
- /* Not an error */
- goto end;
- }
-
- /*
- * Move the notification at the tail of the queue to the
- * iterator's current notification.
- */
- assert(iterator->queue->length > 0);
- bt_put(iterator->current_notification);
- iterator->current_notification = g_queue_pop_tail(iterator->queue);
- assert(iterator->current_notification);