From bebed508ddd7d12c235722c45dde688f0772ed70 Mon Sep 17 00:00:00 2001 From: Alexander Couzens Date: Sat, 11 Jul 2026 23:38:15 +0200 Subject: [PATCH] firmware: Drop CEMU_TX_DATA when sim is in-active When the emulated sim is in reset or without power, drop CEMU_TX_DATA. Change-Id: I796e548225a742cddfde12bd6a60e4d2e5c04c8f --- firmware/libcommon/include/card_emu.h | 1 + firmware/libcommon/source/card_emu.c | 13 +++++++++++++ firmware/libcommon/source/mode_cardemu.c | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/firmware/libcommon/include/card_emu.h b/firmware/libcommon/include/card_emu.h index c2d3b687..8edcecab 100644 --- a/firmware/libcommon/include/card_emu.h +++ b/firmware/libcommon/include/card_emu.h @@ -72,6 +72,7 @@ void card_emu_uart_wait_tx_idle(uint8_t uart_chan); void card_emu_uart_interrupt(uint8_t uart_chan); int card_emu_get_vcc(uint8_t uart_chan); +bool card_ready(struct card_handle *ch); struct cardemu_usb_msg_config; int card_emu_set_config(struct card_handle *ch, const struct cardemu_usb_msg_config *scfg, diff --git a/firmware/libcommon/source/card_emu.c b/firmware/libcommon/source/card_emu.c index ab54fc5f..6b104130 100644 --- a/firmware/libcommon/source/card_emu.c +++ b/firmware/libcommon/source/card_emu.c @@ -215,6 +215,19 @@ struct card_handle { } stats; }; +/* if the card should enque messages */ +bool card_ready(struct card_handle *ch) +{ + switch (ch->state) { + case ISO_S_IN_PTS: + case ISO_S_WAIT_TPDU: + case ISO_S_IN_TPDU: + return true; + default: + return false; + } +} + /* reset all the 'dynamic' state of the card handle to the initial/default values */ static void card_handle_reset(struct card_handle *ch) { diff --git a/firmware/libcommon/source/mode_cardemu.c b/firmware/libcommon/source/mode_cardemu.c index a5485e4d..a0b4aa0f 100644 --- a/firmware/libcommon/source/mode_cardemu.c +++ b/firmware/libcommon/source/mode_cardemu.c @@ -736,6 +736,12 @@ static void dispatch_usb_command_cardem(struct msgb *msg, struct cardem_inst *ci hdr = (struct simtrace_msg_hdr *) msg->l1h; switch (hdr->msg_type) { case SIMTRACE_MSGT_DT_CEMU_TX_DATA: + /* drop message when sim is in-active */ + if (!card_ready(ci->ch)) { + /* FIXME: enqueue an error message for IN */ + usb_buf_free(msg); + return; + } queue = card_emu_get_uart_tx_queue(ci->ch); llist_add_tail(&msg->list, queue); card_emu_have_new_uart_tx(ci->ch);