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) {