read

suspend fun read(transport: ApduTransport, readAgainAllowed: Boolean = true, systemEnd: suspend (stoppedBy: NfcError) -> NfcError? = { null }, useTimeLimit: suspend (ChipCommandTimeLimit) -> Unit = {}, handedOver: () -> Boolean = { false }): ChipReadAttemptOutcome

Runs one attempt on transport, already connected, and decides what follows. Call it from the context that may block on the transport (never the main thread).

When the attempt ends with a lost link that the policy allows to follow, the read publishes ChipReadState.LinkLost and returns ChipReadAttemptOutcome.LINK_LOST: release this link and wait for the document. Otherwise the ending is delivered (unless another cause ended the read first) and the result is ChipReadAttemptOutcome.ENDED.

When the attempt ends with a lost link before it sent the first command that uses the key, and the platform allows another try, the read publishes ChipReadState.NoAnswerYet and returns ChipReadAttemptOutcome.NO_ANSWER_YET: no restart is used, and the next call repeats this attempt. When the platform allows no other try and no try of the read reached that command, the read ends with ChipReadEnding.Failed and NfcNoAnswer.

When the attempt stopped with a lost link that the platform made on purpose (handedOver answers true), under LinkLossPolicy.Resume, the read returns ChipReadAttemptOutcome.HANDED_OVER: no restart is used, no ChipReadState.LinkLost is published, and the next call repeats this attempt and continues from what it read, as a resume does (EF.SOD compared byte for byte, a cut-off data group continued from its byte offset, access run again with the same key). Until that next try is past where this one stopped, the read publishes nothing: no ChipReadState.DocumentFound, ChipReadState.Connecting or ChipReadState.OpeningAccess, no file it had already done, no ChipReadState.AuthenticatingChip it had already shown, so the progress and the file count never go back.

When the chip refuses the key (ChipAccessDenied.Reason.KEY_NOT_MATCHED or ChipAccessDenied.Reason.CHIP_REFUSED) and this is the first refusal in the read, before any try reached the command that uses the key, with an MRZ key whose check digits match or a card access number, the read runs the flow once more on transport before it decides: the application is selected again and the access protocol runs again with fresh random values. What that second run ends with is what follows.

Cancelling the calling coroutine ends the read with ChipReadEnding.Cancelled.By.APP when it has not ended yet, then rethrows.

Parameters

transport

The connected link to the chip.

readAgainAllowed

false when the platform cannot start again after this attempt (for example too little session time left); the read then ends at a lost link.

systemEnd

Asked once, on the read's own context, when the attempt stopped on a lost link (NfcTagLost, NfcTimeout) or on a link that closed while the read had not ended (NfcUnavailable.Reason.CLOSED: the platform invalidated it, for example), with that error, before the read decides to start again or to end. It returns why the system ended the read, such as NFC switched off, or null when the platform sees no such cause. A non-null answer ends the read, never with a new attempt: ChipReadEnding.Cancelled by ChipReadEnding.Cancelled.By.PERSON for NfcUnavailable.Reason.CANCELLED_BY_USER, ChipReadEnding.EndedBySystem with that reason otherwise. It may wait briefly for the platform's own report (a closed link is often reported before the session's end); an exception it throws counts as null. A closed link with no answer ends the read as ChipReadEnding.EndedBySystem with the closed-link error, never as an app cancel: an app cancel ends the read before its link closes.

useTimeLimit

Called right before a command whose kind (ChipCommandTimeLimit) differs from the one before it in this attempt: the platform sets that kind's time limit on transport here. The attempt starts with ChipCommandTimeLimit.BEFORE_ACCESS, so the platform opens the link with that limit and is not called for it. Up to the first command that uses the key (BAC's EXTERNAL AUTHENTICATE, PACE's first GENERAL AUTHENTICATE; for a plain chip that needs no access protocol, the first command after EF.COM) every command is ChipCommandTimeLimit.BEFORE_ACCESS; a loss of the link before that command is a ChipReadAttemptOutcome.NO_ANSWER_YET. From it on, each command is ChipCommandTimeLimit.LONG or ChipCommandTimeLimit.READ. An exception it throws stops the attempt like a failed command.

handedOver

Asked once, when the attempt stopped with a lost link (NfcTagLost or NfcTimeout) and the platform named no system end: true when the platform stopped the attempt itself, between two commands, to get a fresh connection to the same document (on iPhone, before the system's limit on one tag connection). Honoured only under LinkLossPolicy.Resume; the platform never hands over under another policy. An exception it throws counts as false.