From 8403c9ee9364e6fb0774477dc8d46273d4446166 Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Sat, 13 Jan 2018 13:02:48 +1300 Subject: [PATCH] Restore jiggle detection. This is no longer taken care of by the constant acceleration detector, so we need to do it explicitly --- Upstream/Inc/build_config.h | 8 +++- Upstream/Src/upstream_hid_botdetect.c | 55 ++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/Upstream/Inc/build_config.h b/Upstream/Inc/build_config.h index 7ffc957..99e3caf 100644 --- a/Upstream/Inc/build_config.h +++ b/Upstream/Inc/build_config.h @@ -45,7 +45,8 @@ //Adjust these thresholds first to tune mouse bot detection. Lower values = more sensitive #define MOUSE_BOTDETECT_JUMP_VELOCITY_THRESHOLD 20 //Varies by mouse. Most short jumps are <= 10 velocity #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_LIMIT 20 //10 is ok for most mice. But some mice (or users!) generate longer sequences. - #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_CREDIT 40 //Non-constant-acceleration movements can build a credit that will be used before hitting the ACCEL_COUNT limit above. Handy for mice or users that exhibit constant velocity characteristics mid-movement. + #define MOUSE_BOTDETECT_LOCKOUT_CONSTANT_ACCEL_CREDIT 60 //Non-constant-acceleration movements can build a credit that will be used before hitting the ACCEL_COUNT limit above. Handy for mice or users that exhibit constant velocity characteristics mid-movement. + #define MOUSE_BOTDETECT_LOCKOUT_JIGGLE_BIN_THRESHOLD 4 //----------------------------------------------------------- #define MOUSE_BOTDETECT_VELOCITY_MULTIPLIER 10 @@ -58,6 +59,11 @@ #define MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE 12 #define MOUSE_BOTDETECT_VELOCITY_MATCH_BASE 256 #define MOUSE_BOTDETECT_VELOCITY_MATCH_ERROR 6 + + //Jiggle detection stuff + #define MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS 20 //20ms per bin + #define MOUSE_BOTDETECT_JIGGLE_BIN_COUNT 50 //50 bins at 20ms = 1 sec coverage, wrapped + #define MOUSE_BOTDETECT_JIGGLE_BIN_DIVIDER 4 #endif diff --git a/Upstream/Src/upstream_hid_botdetect.c b/Upstream/Src/upstream_hid_botdetect.c index e71f7ec..b358c71 100644 --- a/Upstream/Src/upstream_hid_botdetect.c +++ b/Upstream/Src/upstream_hid_botdetect.c @@ -65,9 +65,14 @@ volatile LockoutStateTypeDef LockoutState = LOCKOUT_STATE_INACTIVE; int32_t PreviousSmoothedAcceleration = 0; int8_t ConstantAccelerationCounter = 0; + //Jiggle detection stuff + uint8_t MouseStopIntervalBinDrainDivideCount = 0; + uint8_t MouseStopIntervalBinArray[MOUSE_BOTDETECT_JIGGLE_BIN_COUNT] = {0}; + //Debug: // int8_t ConstantAccelerationCounterMax = 0; -// int8_t ConstantAccelerationCounterMin = 0; +// int8_t ConstantAccelerationCounterMin = 0; +// uint8_t MouseStopIntervalBinArrayPeak = 0; static void Upstream_HID_BotDetectMouse_DoLockout(void); #endif @@ -387,6 +392,7 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) { uint32_t i; uint32_t now = HAL_GetTick(); + uint32_t moveDelay; uint32_t velocity; int8_t mouseX; int8_t mouseY; @@ -402,17 +408,49 @@ void Upstream_HID_BotDetectMouse(uint8_t* mouseInData) mouseY = mouseInData[2]; velocity = (sqrtf(((int32_t)mouseX * mouseX) + ((int32_t)mouseY * mouseY))) * MOUSE_BOTDETECT_VELOCITY_MULTIPLIER; //Multiply floating-point sqrt result to avoid integer rounding errors + moveDelay = now - LastMouseMoveTime; //Did the mouse stop moving? - if ((now - LastMouseMoveTime) > ((MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) + if (moveDelay > ((MOUSE_BOTDETECT_MOVEMENT_STOP_PERIODS * HID_FS_BINTERVAL) - (HID_FS_BINTERVAL / 2))) { - //Reset constant acceleration detection state - for (i = 0; i < MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE; i++) + //Is this the start of a new movement? + if (velocity != 0) { - MouseVelocityHistory[i] = 0; + //Reset constant acceleration detection state + for (i = 0; i < MOUSE_BOTDETECT_VELOCITY_HISTORY_SIZE; i++) + { + MouseVelocityHistory[i] = 0; + } + ConstantAccelerationCounter = 0; + + + //Jiggle detection: add stopped time to jiggle bins + moveDelay = moveDelay % (MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS * MOUSE_BOTDETECT_JIGGLE_BIN_COUNT); //Wrap stopped time into the array + MouseStopIntervalBinArray[(moveDelay / MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS)]++; + if (MouseStopIntervalBinArray[(moveDelay / MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS)] > MOUSE_BOTDETECT_LOCKOUT_JIGGLE_BIN_THRESHOLD) + { + Upstream_HID_BotDetectMouse_DoLockout(); + } + + //Debug: +// if (MouseStopIntervalBinArray[(moveDelay / MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS)] > MouseStopIntervalBinArrayPeak) +// { +// MouseStopIntervalBinArrayPeak = MouseStopIntervalBinArray[(moveDelay / MOUSE_BOTDETECT_JIGGLE_BIN_WIDTH_MS)]; +// } + + //Drain jiggle bins at specified rate + MouseStopIntervalBinDrainDivideCount++; + if (MouseStopIntervalBinDrainDivideCount >= MOUSE_BOTDETECT_JIGGLE_BIN_DIVIDER) + { + MouseStopIntervalBinDrainDivideCount = 0; + for (i = 0; i < MOUSE_BOTDETECT_JIGGLE_BIN_COUNT; i++) + { + if (MouseStopIntervalBinArray[i] > 0) MouseStopIntervalBinArray[i]--; + } + } } - ConstantAccelerationCounter = 0; + //Jump detection if (MouseIsMoving) @@ -521,6 +559,11 @@ static void Upstream_HID_BotDetectMouse_DoLockout(void) } ConstantAccelerationCounter = 0; + for (i = 0; i < MOUSE_BOTDETECT_JIGGLE_BIN_COUNT; i++) + { + MouseStopIntervalBinArray[i] = 0; + } + TemporaryLockoutTimeMs = 0; LockoutState = LOCKOUT_STATE_TEMPORARY_ACTIVE; LED_SetState(LED_STATUS_FLASH_BOTDETECT);