From ccf2ae9eed9c38a94643ba2727f70eba2692c451 Mon Sep 17 00:00:00 2001 From: Serge <5920850+seerge@users.noreply.github.com> Date: Sun, 19 Nov 2023 13:17:07 +0100 Subject: [PATCH] Asus HID fixes https://github.com/seerge/g-helper/issues/1616 --- app/USB/AsusHid.cs | 76 ++++++++++++++++++++++++---------------------- app/USB/Aura.cs | 11 +++---- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/app/USB/AsusHid.cs b/app/USB/AsusHid.cs index 03872dd8..72e3ee53 100644 --- a/app/USB/AsusHid.cs +++ b/app/USB/AsusHid.cs @@ -12,34 +12,36 @@ public static class AsusHid static int[] deviceIds = { 0x1a30, 0x1854, 0x1869, 0x1866, 0x19b6, 0x1822, 0x1837, 0x1854, 0x184a, 0x183d, 0x8502, 0x1807, 0x17e0, 0x18c6, 0x1abe }; - static HidStream auraStream; + static HidStream? auraStream; - public static HidStream FindHidStream(byte reportId, int minFeatureLength = 1) + public static IEnumerable? FindDevices(byte reportId, int minFeatureLength = 1) { HidDeviceLoader loader = new HidDeviceLoader(); + IEnumerable deviceList; + try { - var deviceList = loader.GetDevices(ASUS_ID).Where(device => deviceIds.Contains(device.ProductID) && device.CanOpen); - foreach (var device in deviceList) - { - var config = new OpenConfiguration(); - config.SetOption(OpenOption.Interruptible, false); - config.SetOption(OpenOption.Exclusive, false); - config.SetOption(OpenOption.Priority, 10); - HidStream hidStream = device.Open(); + deviceList = loader.GetDevices(ASUS_ID).Where( + device => deviceIds.Contains(device.ProductID) && + device.CanOpen && + device.GetMaxFeatureReportLength() >= minFeatureLength); + } + catch (Exception ex) + { + Debug.WriteLine($"Error enumerating HID devices: {ex.Message}"); + yield break; + } - if (device.GetMaxFeatureReportLength() >= minFeatureLength) - { - var reportDescriptor = device.GetReportDescriptor(); - if (reportDescriptor.TryGetReport(ReportType.Feature, reportId, out _)) - { - return hidStream; - } - } + foreach (var device in deviceList) + if (device.GetReportDescriptor().TryGetReport(ReportType.Feature, reportId, out _)) + yield return device; + } - hidStream.Close(); - hidStream.Dispose(); - } + public static HidStream? FindHidStream(byte reportId, int minFeatureLength = 1) + { + try + { + return FindDevices(reportId, minFeatureLength)?.FirstOrDefault()?.Open(); } catch (Exception ex) { @@ -49,29 +51,31 @@ public static class AsusHid return null; } - static void WriteData(HidStream stream, byte[] data, string log = "USB") + public static void Write(byte[] data, byte reportId = AURA_ID, string log = "USB") { + Write(new List { data }, reportId, log); + } + + public static void Write(List dataList, byte reportId = AURA_ID, string log = "USB") + { + var devices = FindDevices(reportId); + if (devices is null) return; + try { - stream.Write(data); - Logger.WriteLine($"{log} " + stream.Device.ProductID + ": " + BitConverter.ToString(data)); + foreach (var device in devices) + using (var stream = device.Open()) + foreach (var data in dataList) + { + stream.Write(data); + Logger.WriteLine($"{log} " + device.ProductID.ToString("X") + ": " + BitConverter.ToString(data)); + } } catch (Exception ex) { - Debug.WriteLine($"Error writing {log} to HID device: {ex.Message} {BitConverter.ToString(data)}"); + Debug.WriteLine($"Error writing {log} to HID device: {ex.Message}"); } - } - public static void Write(byte[] data, byte reportId = AURA_ID, string log = "USB") - { - using (var stream = FindHidStream(reportId)) - WriteData(stream, data, log); - } - public static void Write(List dataList, byte reportId = AURA_ID) - { - using (var stream = FindHidStream(reportId)) - foreach (var data in dataList) - WriteData(stream, data); } public static void WriteAura(byte[] data) diff --git a/app/USB/Aura.cs b/app/USB/Aura.cs index f491d128..be9e111e 100644 --- a/app/USB/Aura.cs +++ b/app/USB/Aura.cs @@ -128,12 +128,11 @@ namespace GHelper.USB isSingleColor = AppConfig.IsSingleColor(); // Mono Color if (AppConfig.ContainsModel("GA402X") || AppConfig.ContainsModel("GA402N")) - using (var stream = AsusHid.FindHidStream(AsusHid.AURA_ID)) - { - if (stream is null) return; - if (stream.Device.ReleaseNumberBcd == 22 || stream.Device.ReleaseNumberBcd == 23) isSingleColor = true; - stream.Close(); - } + { + var device = AsusHid.FindDevices(AsusHid.AURA_ID).FirstOrDefault(); + if (device is null) return; + if (device.ReleaseNumberBcd == 22 || device.ReleaseNumberBcd == 23) isSingleColor = true; + } } public static Dictionary GetSpeeds()