From 8e3422781d8d73705756539982ec1f11946e434d Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Fri, 4 Mar 2022 16:57:20 +0100 Subject: [PATCH] sys/fido2: use ztimer instead of xtimer - for ctap hid timeouts xtimer was used, use ztimer64_msec instead since the code is using absolute times, an already using ztimer_msec - use event_timeout_ztimer instead of event_timeout to not pull in xtimer --- sys/Makefile.dep | 3 ++- sys/fido2/ctap/Kconfig | 2 +- sys/fido2/ctap/transport/ctap_transport.c | 7 +++--- sys/fido2/ctap/transport/hid/Kconfig | 2 ++ sys/fido2/ctap/transport/hid/ctap_hid.c | 23 ++++++++++--------- .../fido2/ctap/transport/hid/ctap_hid.h | 7 +++--- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 82271037cc9d..0958a164b9d3 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -874,6 +874,7 @@ ifneq (,$(filter fido2_ctap_%,$(USEMODULE))) USEMODULE += fido2_ctap_transport USEMODULE += fido2_ctap ifneq (,$(filter fido2_ctap_transport_hid,$(USEMODULE))) + USEMODULE += ztimer64_msec USEMODULE += usbus_hid DISABLE_MODULE += auto_init_usbus endif @@ -891,7 +892,7 @@ ifneq (,$(filter fido2_ctap,$(USEMODULE))) USEMODULE += mtd_write_page USEMODULE += ztimer_msec USEMODULE += event - USEMODULE += event_timeout + USEMODULE += event_timeout_ztimer USEMODULE += cipher_modes USEMODULE += crypto_aes_256 USEMODULE += hashes diff --git a/sys/fido2/ctap/Kconfig b/sys/fido2/ctap/Kconfig index d64921ec1ec1..3116f39bec73 100644 --- a/sys/fido2/ctap/Kconfig +++ b/sys/fido2/ctap/Kconfig @@ -17,7 +17,7 @@ menuconfig MODULE_FIDO2_CTAP select MODULE_PERIPH_GPIO select MODULE_PERIPH_GPIO_IRQ select MODULE_EVENT - select MODULE_EVENT_TIMEOUT + select MODULE_EVENT_TIMEOUT_ZTIMER select MODULE_ZTIMER select MODULE_ZTIMER_MSEC select MODULE_MTD diff --git a/sys/fido2/ctap/transport/ctap_transport.c b/sys/fido2/ctap/transport/ctap_transport.c index 224d4ccfa33e..753e9e03a36f 100644 --- a/sys/fido2/ctap/transport/ctap_transport.c +++ b/sys/fido2/ctap/transport/ctap_transport.c @@ -69,8 +69,9 @@ static void *_event_loop(void *arg) event_queue_init(&_queue); #if IS_USED(MODULE_FIDO2_CTAP_TRANSPORT_HID) - event_timeout_init(&_ctap_hid_event_timeout, &_queue, &_ctap_hid_timeout_event); - event_timeout_set(&_ctap_hid_event_timeout, CTAP_HID_TRANSACTION_TIMEOUT); + event_timeout_ztimer_init(&_ctap_hid_event_timeout, ZTIMER_MSEC, &_queue, + &_ctap_hid_timeout_event); + event_timeout_set(&_ctap_hid_event_timeout, CTAP_HID_TRANSACTION_TIMEOUT_MS); #endif event_loop(&_queue); @@ -83,7 +84,7 @@ static void _ctap_hid_timeout_cb(event_t *arg) { (void)arg; fido2_ctap_transport_hid_check_timeouts(); - event_timeout_set(&_ctap_hid_event_timeout, CTAP_HID_TRANSACTION_TIMEOUT); + event_timeout_set(&_ctap_hid_event_timeout, CTAP_HID_TRANSACTION_TIMEOUT_MS); } #endif diff --git a/sys/fido2/ctap/transport/hid/Kconfig b/sys/fido2/ctap/transport/hid/Kconfig index 20af9313c34c..27a8b40ab31f 100644 --- a/sys/fido2/ctap/transport/hid/Kconfig +++ b/sys/fido2/ctap/transport/hid/Kconfig @@ -10,6 +10,8 @@ menuconfig MODULE_FIDO2_CTAP_TRANSPORT_HID depends on TEST_KCONFIG select MODULE_ISRPIPE select MODULE_USBUS_HID + select MODULE_ZTIMER64 + select MODULE_ZTIMER64_MSEC help Configure a FIDO2 CTAP authenticator via KConfig. diff --git a/sys/fido2/ctap/transport/hid/ctap_hid.c b/sys/fido2/ctap/transport/hid/ctap_hid.c index 4ba81e100e8d..41b2bdc21a1f 100644 --- a/sys/fido2/ctap/transport/hid/ctap_hid.c +++ b/sys/fido2/ctap/transport/hid/ctap_hid.c @@ -17,7 +17,8 @@ #include -#include "xtimer.h" +#include "ztimer.h" +#include "ztimer64.h" #include "usb/usbus.h" #include "usb/usbus/hid.h" #include "usb/usbus/hid_io.h" @@ -528,12 +529,12 @@ bool fido2_ctap_transport_hid_should_cancel(void) void fido2_ctap_transport_hid_check_timeouts(void) { - uint64_t now = xtimer_now_usec64(); + uint64_t now = ztimer64_now(ZTIMER64_MSEC); for (uint8_t i = 0; i < CTAP_HID_CIDS_MAX; i++) { /* transaction timed out because cont packets didn't arrive in time */ if (_is_busy && g_cids[i].taken && - (now - g_cids[i].last_used) >= CTAP_HID_TRANSACTION_TIMEOUT && + (now - g_cids[i].last_used) >= CTAP_HID_TRANSACTION_TIMEOUT_MS && _state.cid == g_cids[i].cid && !_state.is_locked) { _send_error_response(g_cids[i].cid, CTAP_HID_ERR_MSG_TIMEOUT); @@ -547,14 +548,14 @@ void fido2_ctap_transport_hid_check_timeouts(void) static int8_t _add_cid(uint32_t cid) { - uint64_t oldest = xtimer_now_usec64(); + uint64_t oldest = ztimer64_now(ZTIMER64_MSEC); int8_t index_oldest = -1; for (int i = 0; i < CTAP_HID_CIDS_MAX; i++) { if (!g_cids[i].taken) { g_cids[i].taken = true; g_cids[i].cid = cid; - g_cids[i].last_used = xtimer_now_usec64(); + g_cids[i].last_used = ztimer64_now(ZTIMER64_MSEC); return CTAP_HID_OK; } @@ -569,7 +570,7 @@ static int8_t _add_cid(uint32_t cid) if (index_oldest > -1) { g_cids[index_oldest].taken = true; g_cids[index_oldest].cid = cid; - g_cids[index_oldest].last_used = xtimer_now_usec64(); + g_cids[index_oldest].last_used = ztimer64_now(ZTIMER64_MSEC); return CTAP_HID_OK; } @@ -580,7 +581,7 @@ static int8_t _refresh_cid(uint32_t cid) { for (int i = 0; i < CTAP_HID_CIDS_MAX; i++) { if (g_cids[i].cid == cid) { - g_cids[i].last_used = xtimer_now_usec64(); + g_cids[i].last_used = ztimer64_now(ZTIMER64_MSEC); return CTAP_HID_OK; } } @@ -622,19 +623,19 @@ static void _wink(uint32_t cid, uint8_t cmd) for (int i = 1; i <= 8; i++) { #ifdef LED0_TOGGLE LED0_TOGGLE; - xtimer_msleep(delay); + ztimer_sleep(ZTIMER_MSEC, delay); #endif #ifdef LED1_TOGGLE LED1_TOGGLE; - xtimer_msleep(delay); + ztimer_sleep(ZTIMER_MSEC, delay); #endif #ifdef LED2_TOGGLE LED2_TOGGLE; - xtimer_msleep(delay); + ztimer_sleep(ZTIMER_MSEC, delay); #endif #ifdef LED3_TOGGLE LED3_TOGGLE; - xtimer_msleep(delay); + ztimer_sleep(ZTIMER_MSEC, delay); #endif delay /= 2; } diff --git a/sys/include/fido2/ctap/transport/hid/ctap_hid.h b/sys/include/fido2/ctap/transport/hid/ctap_hid.h index c1ded965830e..48cdecd510bd 100644 --- a/sys/include/fido2/ctap/transport/hid/ctap_hid.h +++ b/sys/include/fido2/ctap/transport/hid/ctap_hid.h @@ -62,10 +62,9 @@ extern "C" { * @brief CTAP_HID transaction timeout in microseconds */ #ifdef CONFIG_FIDO2_CTAP_TRANSPORT_HID_TRANSACTION_TIMEOUT -#define CTAP_HID_TRANSACTION_TIMEOUT (CONFIG_FIDO2_CTAP_TRANSPORT_HID_TRANSACTION_TIMEOUT * \ - US_PER_MS) +#define CTAP_HID_TRANSACTION_TIMEOUT_MS (CONFIG_FIDO2_CTAP_TRANSPORT_HID_TRANSACTION_TIMEOUT) #else -#define CTAP_HID_TRANSACTION_TIMEOUT (500 * US_PER_MS) +#define CTAP_HID_TRANSACTION_TIMEOUT_MS (500) #endif /** @@ -237,7 +236,7 @@ void fido2_ctap_transport_hid_handle_packet(void *pkt_raw); * * CTAP specification (version 20190130) section 5.6 * - * @ref CTAP_HID_TRANSACTION_TIMEOUT + * @ref CTAP_HID_TRANSACTION_TIMEOUT_MS */ void fido2_ctap_transport_hid_check_timeouts(void);