From 93b6c360d67b4fb34184e141b7aa370f5221a2dc Mon Sep 17 00:00:00 2001 From: IceStormNG Date: Sun, 30 Jul 2023 11:10:31 +0200 Subject: [PATCH] Handling of error responses from the mouse and some mice spam packets through the interface. Read again, until you get the matching USB packet or a timeout happens (aka: No data left in the buffer). --- app/Peripherals/Mouse/AsusMouse.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/Peripherals/Mouse/AsusMouse.cs b/app/Peripherals/Mouse/AsusMouse.cs index 69f61d6e..5f9ab099 100644 --- a/app/Peripherals/Mouse/AsusMouse.cs +++ b/app/Peripherals/Mouse/AsusMouse.cs @@ -240,6 +240,10 @@ namespace GHelper.Peripherals.Mouse #endif } + protected virtual bool IsMouseError(byte[] packet) + { + return packet[1] == 0xFF && packet[2] == 0xAA; + } protected virtual long MeasuredIO(Action ioFunc, byte[] param) { @@ -269,6 +273,23 @@ namespace GHelper.Peripherals.Mouse if (IsPacketLoggerEnabled()) Logger.WriteLine(GetDisplayName() + ": Read packet: " + ByteArrayToString(response)); + + if (IsMouseError(response)) + { + Logger.WriteLine(GetDisplayName() + ": Mouse returned error (FF AA). Packet probably not supported by mouse firmware."); + //Error. Mouse could not understand or process the sent packet + return response; + } + + //Not the response we were looking for, continue reading + while (response[0] != packet[0] || response[1] != packet[1] || response[2] != packet[2]) + { + Logger.WriteLine(GetDisplayName() + ": Read wrong packet left in buffer: " + ByteArrayToString(response) + ". Retrying..."); + //Read again + time = MeasuredIO(Read, response); + Logger.WriteLine(GetDisplayName() + ": Read took " + time + "ms"); + } + } catch (IOException e) {