-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathLightspeedHidLoader.cs
201 lines (160 loc) · 7.43 KB
/
LightspeedHidLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using HidSharp;
using RGB.NET.Core;
using RGB.NET.HID;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace RGB.NET.Devices.Logitech.HID;
/// <summary>
/// Represents a loaded for logitech HID-devices.
/// </summary>
/// <typeparam name="TLed">The type of the identifier leds are mapped to.</typeparam>
/// <typeparam name="TData">The type of the custom data added to the HID-device.</typeparam>
public sealed class LightspeedHIDLoader<TLed, TData> : IEnumerable<HIDDeviceDefinition<TLed, TData>>
where TLed : notnull
{
#region Constants
private const int LOGITECH_PROTOCOL_TIMEOUT = 300;
private const int VENDOR_ID = 0x046D;
// ReSharper disable once StaticMemberInGenericType - This is used like a const
private static readonly List<int> RECEIVER_PIDS =
[
0xC539,
0xC53A,
0xC541,
0xC545,
0xC547
];
#endregion
#region Properties & Fields
private readonly Dictionary<int, HIDDeviceDefinition<TLed, TData>> _deviceDefinitions = [];
/// <summary>
/// Gets the vendor id used for this loader.
/// </summary>
public int VendorId => VENDOR_ID;
/// <summary>
/// Gets or sets the filter used to determine which devices should be loaded.
/// </summary>
public RGBDeviceType LoadFilter { get; set; } = RGBDeviceType.All;
#endregion
#region Methods
/// <summary>
/// Adds a new <see cref="HIDDeviceDefinition{TLed,TData}"/> to this loader.
/// </summary>
/// <param name="virtualPid">The virtual product id of the HID-device.</param>
/// <param name="deviceType">The type of the device.</param>
/// <param name="name">The name of the device.</param>
/// <param name="ledMapping">The mapping of the leds of the device.</param>
/// <param name="customData">Some custom data to attach to the device.</param>
public void Add(int virtualPid, RGBDeviceType deviceType, string name, LedMapping<TLed> ledMapping, TData customData)
=> _deviceDefinitions.Add(virtualPid, new HIDDeviceDefinition<TLed, TData>(virtualPid, deviceType, name, ledMapping, customData));
/// <summary>
/// Gets a enumerable containing all devices from the definition-list that are connected and match the <see cref="LoadFilter"/>.
/// </summary>
/// <returns>The enumerable containing the connected devices.</returns>
public IEnumerable<HIDDeviceDefinition<TLed, TData>> GetConnectedDevices()
{
foreach (int device in Detect())
{
if (_deviceDefinitions.TryGetValue(device, out HIDDeviceDefinition<TLed, TData>? definition))
if (LoadFilter.HasFlag(definition.DeviceType))
yield return definition;
}
}
/// <summary>
/// Gets a enumerable containing all the first device of each group of devices from the definition-list that are connected and match the <see cref="LoadFilter"/>.
/// The grouping is done by the specified function.
/// </summary>
/// <typeparam name="TKey">The type of the key used to group the devices.</typeparam>
/// <param name="groupBy">The function grouping the devices.</param>
/// <returns>The enumerable containing the selected devices.</returns>
public IEnumerable<HIDDeviceDefinition<TLed, TData>> GetConnectedDevices<TKey>(Func<HIDDeviceDefinition<TLed, TData>, TKey> groupBy)
=> GetConnectedDevices().GroupBy(groupBy)
.Select(group => group.First());
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <inheritdoc />
public IEnumerator<HIDDeviceDefinition<TLed, TData>> GetEnumerator() => _deviceDefinitions.Values.GetEnumerator();
#endregion
#region Private Methods
private IEnumerable<int> Detect() => RECEIVER_PIDS.SelectMany(Detect);
private IEnumerable<int> Detect(int pid)
{
Dictionary<byte, HidDevice> deviceUsages = DeviceList.Local
.GetHidDevices(VendorId, pid)
.Where(d => d.DevicePath.Contains("mi_02"))
.ToDictionary(x => (byte)x.GetUsage(), x => x);
foreach ((int wirelessPid, byte _) in GetWirelessDevices(deviceUsages))
yield return wirelessPid;
}
[SuppressMessage("ReSharper", "MustUseReturnValue")]
private Dictionary<int, byte> GetWirelessDevices(IReadOnlyDictionary<byte, HidDevice> deviceUsages)
{
const byte LOGITECH_RECEIVER_ADDRESS = 0xFF;
const byte LOGITECH_SET_REGISTER_REQUEST = 0x80;
const byte LOGITECH_GET_REGISTER_REQUEST = 0x81;
Dictionary<int, byte> map = [];
if (!deviceUsages.TryGetValue(1, out HidDevice? device) || !device.TryOpen(out HidStream stream))
return map;
int tries = 0;
const int MAX_TRIES = 5;
while (tries < MAX_TRIES)
{
try
{
stream.ReadTimeout = LOGITECH_PROTOCOL_TIMEOUT;
stream.WriteTimeout = LOGITECH_PROTOCOL_TIMEOUT;
FapResponse response = new();
FapShortRequest getConnectedDevices = new();
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
stream.Write(getConnectedDevices.AsSpan());
stream.Read(response.AsSpan());
bool wirelessNotifications = (response.Data01 & 1) == 1;
if (!wirelessNotifications)
{
response = new FapResponse();
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
getConnectedDevices.Data1 = 1;
stream.Write(getConnectedDevices.AsSpan());
stream.Read(response.AsSpan());
if (getConnectedDevices.FeatureIndex == 0x8f)
{
//error??
}
}
response = new FapResponse();
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_GET_REGISTER_REQUEST);
getConnectedDevices.FeatureCommand = 0x02;
stream.Write(getConnectedDevices.AsSpan());
stream.Read(response.AsSpan());
int deviceCount = response.Data01;
if (deviceCount <= 0)
return map;
//Add 1 to the device_count to include the receiver
deviceCount++;
getConnectedDevices.Init(LOGITECH_RECEIVER_ADDRESS, LOGITECH_SET_REGISTER_REQUEST);
getConnectedDevices.FeatureCommand = 0x02;
getConnectedDevices.Data0 = 0x02;
stream.Write(getConnectedDevices.AsSpan());
for (int i = 0; i < deviceCount; i++)
{
FapResponse devices = new();
stream.Read(devices.AsSpan());
int wirelessPid = (devices.Data02 << 8) | devices.Data01;
if (devices.DeviceIndex != 0xff)
map.Add(wirelessPid, devices.DeviceIndex);
}
break;
}
catch
{
tries++;
//This might timeout if LGS or GHUB interfere.
//Retry.
}
}
return map;
}
#endregion
}