From 189ad01248e1dc66083827db81e6b202d7a899d8 Mon Sep 17 00:00:00 2001 From: Yves Richard Date: Tue, 22 Oct 2024 15:34:55 +0200 Subject: [PATCH] Make sure NFC response callback is called once Only call cb with error flag set on card disconnection --- src/os_io_nfc.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/os_io_nfc.c b/src/os_io_nfc.c index 5c137c85c..daee467aa 100644 --- a/src/os_io_nfc.c +++ b/src/os_io_nfc.c @@ -150,26 +150,30 @@ void io_nfc_process_events(void) if (G_io_reader_ctx.response_received) { G_io_reader_ctx.response_received = false; if (G_io_reader_ctx.resp_callback != NULL) { - G_io_reader_ctx.resp_callback(false, - false, - ledger_protocol_data.rx_apdu_buffer, - ledger_protocol_data.rx_apdu_length); + nfc_resp_callback_t resp_cb = G_io_reader_ctx.resp_callback; + G_io_reader_ctx.resp_callback = NULL; + resp_cb(false, + false, + ledger_protocol_data.rx_apdu_buffer, + ledger_protocol_data.rx_apdu_length); } memset(ledger_protocol_data.rx_apdu_buffer, 0, ledger_protocol_data.rx_apdu_length); } if (G_io_reader_ctx.resp_callback != NULL && G_io_reader_ctx.remaining_ms == 0) { - G_io_reader_ctx.resp_callback(false, true, NULL, 0); + nfc_resp_callback_t resp_cb = G_io_reader_ctx.resp_callback; G_io_reader_ctx.resp_callback = NULL; + resp_cb(false, true, NULL, 0); } if (G_io_reader_ctx.event_happened) { G_io_reader_ctx.event_happened = 0; - // if in_progress, call the resp_callback with an error - if (G_io_reader_ctx.resp_callback != NULL) { - G_io_reader_ctx.resp_callback(true, false, NULL, 0); + // If card is removed during an APDU processing, call the resp_callback with an error + if (G_io_reader_ctx.resp_callback != NULL && G_io_reader_ctx.last_event == CARD_REMOVED) { + nfc_resp_callback_t resp_cb = G_io_reader_ctx.resp_callback; G_io_reader_ctx.resp_callback = NULL; + resp_cb(true, false, NULL, 0); } if (G_io_reader_ctx.evt_callback != NULL) {