Skip to content

Commit

Permalink
bug fix: touch event being consumed by "pass" before "u2f"
Browse files Browse the repository at this point in the history
  • Loading branch information
z4yx committed Jun 9, 2024
1 parent 4db8c54 commit 4fb369f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static inline void start_quick_blinking(uint8_t sec) {
}
void stop_blinking(void);
uint8_t device_is_blinking(void);
bool device_allow_kbd_touch(void);
void fm11_init(void);
uint8_t fm_read_reg(uint16_t reg);
void fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len);
Expand Down
6 changes: 3 additions & 3 deletions interfaces/USB/class/kbdhid/kbdhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ uint8_t KBDHID_Init() {
}

uint8_t KBDHID_Loop(void) {
if (state == KBDHID_Idle) {
if (state == KBDHID_Idle && device_allow_kbd_touch()) {
const uint8_t touch = get_touch_result();
if (touch != TOUCH_NO) {
const int len = pass_handle_touch(touch, key_sequence);
Expand All @@ -150,9 +150,9 @@ uint8_t KBDHID_Loop(void) {
key_sequence[len] = 0;
key_seq_position = 0;
state = KBDHID_Typing;
DBG_MSG("Start typing %s", key_sequence);
DBG_MSG("Start typing %s\n", key_sequence);
set_touch_result(TOUCH_NO);
}
set_touch_result(TOUCH_NO);
} else {
KBDHID_TypeKeySeq();
}
Expand Down
28 changes: 19 additions & 9 deletions src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,31 @@

volatile static uint8_t touch_result;
static uint8_t has_rf;
static uint32_t last_blink = UINT32_MAX, blink_timeout, blink_interval;
static uint32_t last_blink, blink_timeout, blink_interval;
static enum { ON, OFF } led_status;
typedef enum { WAIT_NONE = 1, WAIT_CCID, WAIT_CTAPHID, WAIT_DEEP, WAIT_DEEP_TOUCHED, WAIT_DEEP_CANCEL } wait_status_t;
volatile static wait_status_t wait_status = WAIT_NONE; // WAIT_NONE is not 0, hence inited

uint8_t device_is_blinking(void) { return last_blink != UINT32_MAX; }
uint8_t device_is_blinking(void) { return blink_timeout != 0; }

void device_loop(uint8_t has_touch) {
CCID_Loop();
CTAPHID_Loop(0);
WebUSB_Loop();
if (has_touch && // hardware features the touch pad
!device_is_blinking() && // applets are not waiting for touch
device_get_tick() > 2000 // ignore touch for the first 2 seconds
)
KBDHID_Loop();
KBDHID_Loop();
}

bool device_allow_kbd_touch(void) {
uint32_t now = device_get_tick();
if (!device_is_blinking() && // applets are not waiting for touch
now > 2000 && // ignore touch for the first 2 seconds
now - 1000 > last_blink &&
get_touch_result() != TOUCH_NO
) {
DBG_MSG("now=%lu last_blink=%lu\n", now, last_blink);
return true;
}
return false;
}

uint8_t get_touch_result(void) {
Expand Down Expand Up @@ -140,8 +149,9 @@ static void toggle_led(void) {

void device_update_led(void) {
uint32_t now = device_get_tick();
if (!device_is_blinking()) return;
if (now > blink_timeout) stop_blinking();
if (now >= last_blink && now - last_blink >= blink_interval) {
else if (now >= last_blink && now - last_blink >= blink_interval) {
last_blink = now;
toggle_led();
}
Expand All @@ -160,7 +170,7 @@ void start_blinking_interval(uint8_t sec, uint32_t interval) {
}

void stop_blinking(void) {
last_blink = UINT32_MAX;
blink_timeout = 0;
if (cfg_is_led_normally_on()) {
led_on();
led_status = ON;
Expand Down

0 comments on commit 4fb369f

Please sign in to comment.