From f2085b836b1c9f4a12f211d99a99c6a6d12ae6fd Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sun, 19 Nov 2023 15:15:19 +0100 Subject: [PATCH] Aura HID fixes https://github.com/seerge/g-helper/issues/1616 --- app/USB/AsusHid.cs | 49 ++++++++++++++++++++++++++++++++-------------- app/USB/Aura.cs | 4 ++-- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/app/USB/AsusHid.cs b/app/USB/AsusHid.cs index 72e3ee53..1563ab3b 100644 --- a/app/USB/AsusHid.cs +++ b/app/USB/AsusHid.cs @@ -14,21 +14,18 @@ public static class AsusHid static HidStream? auraStream; - public static IEnumerable? FindDevices(byte reportId, int minFeatureLength = 1) + public static IEnumerable? FindDevices(byte reportId) { HidDeviceLoader loader = new HidDeviceLoader(); IEnumerable deviceList; try { - deviceList = loader.GetDevices(ASUS_ID).Where( - device => deviceIds.Contains(device.ProductID) && - device.CanOpen && - device.GetMaxFeatureReportLength() >= minFeatureLength); + deviceList = loader.GetDevices(ASUS_ID).Where(device => deviceIds.Contains(device.ProductID) && device.CanOpen && device.GetMaxFeatureReportLength() > 0); } catch (Exception ex) { - Debug.WriteLine($"Error enumerating HID devices: {ex.Message}"); + Logger.WriteLine($"Error enumerating HID devices: {ex.Message}"); yield break; } @@ -37,28 +34,50 @@ public static class AsusHid yield return device; } - public static HidStream? FindHidStream(byte reportId, int minFeatureLength = 1) + public static HidStream? FindHidStream(byte reportId) { try { - return FindDevices(reportId, minFeatureLength)?.FirstOrDefault()?.Open(); + return FindDevices(reportId)?.FirstOrDefault()?.Open(); } catch (Exception ex) { - Debug.WriteLine($"Error accessing HID device: {ex.Message}"); + Logger.WriteLine($"Error accessing HID device: {ex.Message}"); } return null; } - public static void Write(byte[] data, byte reportId = AURA_ID, string log = "USB") + public static void WriteInput(byte[] data, string log = "USB") { - Write(new List { data }, reportId, log); + foreach (var device in FindDevices(INPUT_ID)) + { + try + { + using (var stream = device.Open()) + { + var payload = new byte[device.GetMaxFeatureReportLength()]; + Array.Copy(data, payload, data.Length); + stream.SetFeature(payload); + Logger.WriteLine($"{log} Feature {device.ProductID.ToString("X")}|{device.GetMaxFeatureReportLength()}: {BitConverter.ToString(data)}"); + } + } + catch (Exception ex) + { + Logger.WriteLine($"Error setting feature {device.GetMaxFeatureReportLength()} {device.DevicePath}: {BitConverter.ToString(data)} {ex.Message}"); + + } + } } - public static void Write(List dataList, byte reportId = AURA_ID, string log = "USB") + public static void Write(byte[] data, string log = "USB") { - var devices = FindDevices(reportId); + Write(new List { data }, log); + } + + public static void Write(List dataList, string log = "USB") + { + var devices = FindDevices(AURA_ID); if (devices is null) return; try @@ -68,12 +87,12 @@ public static class AsusHid foreach (var data in dataList) { stream.Write(data); - Logger.WriteLine($"{log} " + device.ProductID.ToString("X") + ": " + BitConverter.ToString(data)); + Logger.WriteLine($"{log} {device.ProductID.ToString("X")}: {BitConverter.ToString(data)}"); } } catch (Exception ex) { - Debug.WriteLine($"Error writing {log} to HID device: {ex.Message}"); + Logger.WriteLine($"Error writing {log}: {ex.Message}"); } } diff --git a/app/USB/Aura.cs b/app/USB/Aura.cs index be9e111e..a1e860a3 100644 --- a/app/USB/Aura.cs +++ b/app/USB/Aura.cs @@ -263,9 +263,9 @@ namespace GHelper.USB if (delay) await Task.Delay(TimeSpan.FromSeconds(1)); if (isACPI) Program.acpi.TUFKeyboardBrightness(brightness); - AsusHid.Write(new byte[] { AsusHid.AURA_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.AURA_ID, log); + AsusHid.Write(new byte[] { AsusHid.AURA_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, log); if (AppConfig.ContainsModel("GA503")) - AsusHid.Write(new byte[] { AsusHid.INPUT_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, AsusHid.INPUT_ID, log); + AsusHid.WriteInput(new byte[] { AsusHid.INPUT_ID, 0xba, 0xc5, 0xc4, (byte)brightness }, log); });