Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib_standard_app/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
// removed from the application
#define SW_OK 0x9000
#define SW_WRONG_RESPONSE_LENGTH 0xB000
#define SW_WRONG_APP_STATE 0xC000

static uint8_t need_to_start_io;
static bool pending_apdu_response;

uint8_t G_io_seproxyhal_spi_buffer[OS_IO_SEPH_BUFFER_SIZE];

Expand Down Expand Up @@ -91,7 +93,7 @@ WEAK void io_init()
need_to_start_io = 1;
}

WEAK int io_recv_command()
WEAK int io_recv_command(void)
{
int status = 0;

Expand All @@ -112,7 +114,10 @@ WEAK int io_recv_command()
#endif
status = io_legacy_apdu_rx(1);
}

if (status > 0) {
// set the state of APDU as "pending response"
pending_apdu_response = true;
}
return status;
}

Expand Down Expand Up @@ -141,6 +146,8 @@ WEAK int io_send_response_buffers(const buffer_t *rdatalist, size_t count, uint1

write_u16_be(G_io_tx_buffer, length, sw);
length += 2;
// reset the state of APDU
pending_apdu_response = false;

#ifdef HAVE_SWAP
// If we are in swap mode and have validated a TX, we send it and immediately quit
Expand Down Expand Up @@ -183,3 +190,10 @@ WEAK bool io_recv_and_process_event(void)
return false;
}
#endif

void io_send_pending_response(void)
{
if (pending_apdu_response) {
io_send_sw(SW_WRONG_APP_STATE);
}
}
7 changes: 7 additions & 0 deletions lib_standard_app/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ static inline int io_send_sw(uint16_t sw)
{
return io_send_response_buffers(NULL, 0, sw);
}

/**
* @brief send automatic error response (0xC000) in case of potentially pending APDU
* @note to be called at application exit
*
*/
void io_send_pending_response(void);
2 changes: 2 additions & 0 deletions lib_standard_app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ bolos_ux_params_t G_ux_params;
*/
WEAK void __attribute__((noreturn)) app_exit(void)
{
// send automatic error response in case of potentially pending APDU
io_send_pending_response();
#ifndef USE_OS_IO_STACK
os_io_stop();
#endif // USE_OS_IO_STACK
Expand Down
Loading