From 5a4aff700823bfc38733a902fd5f2fe04576c17c Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 20 Sep 2022 15:31:34 +0200 Subject: [PATCH] Adding KEEP_ALIVE response. Signed-off-by: Pol Henarejos --- src/hsm.h | 2 ++ src/main.c | 9 ++++++++- src/usb/ccid/ccid.c | 2 ++ src/usb/hid/hid.c | 12 +++++++++++- src/usb/usb.c | 11 ++++++++--- src/usb/usb.h | 2 ++ 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/hsm.h b/src/hsm.h index c2776dc..fcc3f84 100644 --- a/src/hsm.h +++ b/src/hsm.h @@ -61,6 +61,8 @@ enum { }; extern void led_set_blink(uint32_t mode); +extern bool is_req_button_pending(); + #define SW_BYTES_REMAINING_00() set_res_sw (0x61, 0x00) #define SW_WARNING_STATE_UNCHANGED() set_res_sw (0x62, 0x00) #define SW_WARNING_CORRUPTED() set_res_sw (0x62, 0x81) diff --git a/src/main.c b/src/main.c index 44c9d25..60ecf24 100644 --- a/src/main.c +++ b/src/main.c @@ -67,11 +67,17 @@ void led_set_blink(uint32_t mode) { void execute_tasks(); +static bool req_button_pending = false; + +bool is_req_button_pending() { + return req_button_pending; +} + bool wait_button() { uint32_t start_button = board_millis(); bool timeout = false; led_set_blink((1000 << 16) | 100); - + req_button_pending = true; while (board_button_read() == false) { execute_tasks(); //sleep_ms(10); @@ -91,6 +97,7 @@ bool wait_button() { } } led_set_blink(BLINK_PROCESSING); + req_button_pending = false; return timeout; } diff --git a/src/usb/ccid/ccid.c b/src/usb/ccid/ccid.c index 7a2edd3..c666ea0 100644 --- a/src/usb/ccid/ccid.c +++ b/src/usb/ccid/ccid.c @@ -124,6 +124,8 @@ int driver_init() { ccid_response = (struct ccid_header *)usb_get_tx(); apdu.rdata = &ccid_response->apdu; + usb_set_timeout_counter(1500); + return CCID_OK; } diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index 24fe4ca..5370923 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -53,6 +53,8 @@ int driver_init() { ctap_resp = (CTAPHID_FRAME *)usb_get_tx(); apdu.rdata = ctap_resp->init.data; + usb_set_timeout_counter(200); + return 0; } void driver_task() { @@ -272,7 +274,15 @@ int driver_process_usb_packet(uint16_t read) { } void driver_exec_timeout() { - + ctap_resp = (CTAPHID_FRAME *)usb_get_tx(); + memset(ctap_resp, 0, sizeof(CTAPHID_FRAME)); + ctap_resp->cid = ctap_req->cid; + ctap_resp->init.cmd = CTAPHID_KEEPALIVE; + ctap_resp->init.bcntl = 1; + uint8_t bck = ctap_resp->init.data[0]; + ctap_resp->init.data[0] = is_req_button_pending() ? 2 : 1; + hid_write(64); + ctap_resp->init.data[0] = bck; } uint8_t *driver_prepare_response() { diff --git a/src/usb/usb.c b/src/usb/usb.c index 43f621a..2b64b29 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -38,6 +38,11 @@ static uint8_t rx_buffer[4096], tx_buffer[4096]; static uint16_t w_offset = 0, r_offset = 0; static uint16_t w_len = 0, tx_r_offset = 0; +static uint32_t timeout_counter = 0; + +void usb_set_timeout_counter(uint32_t v) { + timeout_counter = v; +} uint32_t usb_write_offset(uint16_t len, uint16_t offset) { uint8_t pkt_max = 64; @@ -147,7 +152,6 @@ static void card_init_core1(void) { size_t finished_data_size = 0; -void apdu_thread(); void card_start(void (*func)(void)) { multicore_reset_core1(); card_init_core1(); @@ -175,8 +179,9 @@ void usb_task() { if (m == EV_EXEC_FINISHED) { driver_exec_finished(finished_data_size); led_set_blink(BLINK_MOUNTED); + timeout_stop(); } - else if (m == EV_PRESS_BUTTON) { + else if (m == EV_PRESS_BUTTON) { uint32_t flag = wait_button() ? EV_BUTTON_TIMEOUT : EV_BUTTON_PRESSED; queue_try_add(&usb_to_card_q, &flag); } @@ -232,7 +237,7 @@ void usb_task() { } else { if (timeout > 0) { - if (timeout + 1500 < board_millis()) { + if (timeout + timeout_counter < board_millis()) { driver_exec_timeout(); timeout = board_millis(); } diff --git a/src/usb/usb.h b/src/usb/usb.h index 2190d4a..28c5281 100644 --- a/src/usb/usb.h +++ b/src/usb/usb.h @@ -57,4 +57,6 @@ extern uint8_t *usb_get_tx(); extern uint32_t usb_write_offset(uint16_t len, uint16_t offset); extern void usb_clear_rx(); extern size_t finished_data_size; +extern void usb_set_timeout_counter(uint32_t v); + #endif