-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathKeyboardDeviceManager.cs
286 lines (268 loc) · 11 KB
/
KeyboardDeviceManager.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using HidSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GK6X
{
public delegate void KeyboardDeviceConnected(KeyboardDevice device);
public delegate void KeyboardDeviceDisconnected(KeyboardDevice device);
public static class KeyboardDeviceManager
{
// vendorid, productids
internal static Dictionary<ushort, ushort[]> knownProducts = new Dictionary<ushort, ushort[]>()
{
{ 7847,// (0x1EA7) SEMITEK INTERNATIONAL (HK) HOLDING LTD.
new ushort[]
{
2311,// (0x907) GK6X
}
},
};
/// <summary>
/// Device path (string) -> KeyboardDevice
/// </summary>
private static Dictionary<string, KeyboardDevice> connectedDevices = new Dictionary<string, KeyboardDevice>();
private static HashSet<string> ignoredDevices = new HashSet<string>();
public static event KeyboardDeviceConnected Connected;
public static event KeyboardDeviceConnected Disconnected;
private static bool isListening = false;
public static void StartListener()
{
lock (connectedDevices)
{
if (!isListening)
{
RefreshConnectedDevices();
DeviceList.Local.Changed += Local_Changed;
isListening = true;
}
}
}
public static void StopListener()
{
lock (connectedDevices)
{
if (isListening)
{
DeviceList.Local.Changed -= Local_Changed;
// Create a copy of the connected devices collection so that we can remove the devices as we iterate
foreach (KeyboardDevice device in connectedDevices.Values.ToList())
{
device.Close();
}
isListening = false;
}
}
}
private static void Local_Changed(object sender, DeviceListChangedEventArgs e)
{
RefreshConnectedDevices();
}
public static KeyboardDevice[] GetConnectedDevices()
{
lock (connectedDevices)
{
return connectedDevices.Values.ToArray();
}
}
public static void RefreshConnectedDevices()
{
lock (connectedDevices)
{
foreach (HidDevice device in DeviceList.Local.GetHidDevices())
{
if (connectedDevices.ContainsKey(device.DevicePath) || ignoredDevices.Contains(device.DevicePath))
{
continue;
}
bool validDevice = false;
try
{
// I *think* 65 is used by all GK6X keyboards
const int reportLength = 65;
ushort[] productIds;
if (device.GetMaxInputReportLength() == reportLength &&
device.GetMaxOutputReportLength() == reportLength &&
knownProducts.TryGetValue((ushort)device.VendorID, out productIds) &&
productIds.Contains((ushort)device.ProductID))
{
validDevice = true;
}
}
catch
{
ignoredDevices.Add(device.DevicePath);
}
if (validDevice)
{
HidStream stream;
if (device.TryOpen(out stream))
{
if (!stream.CanWrite)
{
stream.Close();
continue;
}
KeyboardState keyboardState = Handshake(stream);
if (keyboardState != null)
{
KeyboardDevice keyboardDevice = new KeyboardDevice();
keyboardDevice.State = keyboardState;
keyboardDevice.stream = stream;
keyboardDevice.device = device;
connectedDevices[device.DevicePath] = keyboardDevice;
stream.Closed += (object sender, EventArgs e) =>
{
keyboardDevice.Close();
lock (connectedDevices)
{
connectedDevices.Remove(device.DevicePath);
}
if (Disconnected != null)
{
Disconnected(keyboardDevice);
}
};
if (Connected != null)
{
Connected(keyboardDevice);
}
keyboardDevice.StartPingThread();
}
else
{
Console.WriteLine("Keyboard handshake failed");
stream.Close();
}
}
}
}
}
}
private static KeyboardState Handshake(HidStream stream)
{
try
{
byte bufferSizeA;
byte bufferSizeB;
uint firmwareId;
byte firmwareMinorVersion;
byte firmwareMajorVersion;
uint modelId;
using (Packet packet = WriteSimplePacket(stream, 0x0901))
{
if (packet == null)
{
LogHandshakeFailed(stream.Device, "opcode 01 09");
return null;
}
bufferSizeA = packet.ReadByte();
bufferSizeB = packet.ReadByte();
if (bufferSizeA == 0 || bufferSizeB == 0)
{
LogHandshakeFailed(stream.Device, "Bad buffer size");
return null;
}
}
using (Packet packet = WriteSimplePacket(stream, 0x0101))
{
if (packet == null)
{
LogHandshakeFailed(stream.Device, "opcode 01 01");
return null;
}
firmwareId = packet.ReadUInt32();
firmwareMinorVersion = packet.ReadByte();
firmwareMajorVersion = packet.ReadByte();
if (firmwareId == 0 || (firmwareMinorVersion == 0 && firmwareMajorVersion == 0))
{
LogHandshakeFailed(stream.Device, "Bad firmware id");
return null;
}
}
using (Packet packet = WriteSimplePacket(stream, 0x0801))
{
if (packet == null)
{
LogHandshakeFailed(stream.Device, "opcode 01 08");
return null;
}
byte[] crcBytes = packet.ReadBytes(6);
ushort calculatedModelIdCrc = Crc16.GetCrc(crcBytes);
packet.Index -= 6;
modelId = packet.ReadUInt32();
if (modelId == 0)
{
LogHandshakeFailed(stream.Device, "Bad keyboard model id");
return null;
}
ushort crcValidation1 = packet.ReadUInt16();
ushort modelIdCrc = packet.ReadUInt16();
if (calculatedModelIdCrc != modelIdCrc)
{
LogHandshakeFailed(stream.Device, "Bad keyboard model crc");
return null;
}
}
// OpCodes_Info.Unk_02 should probably also be sent? Not sure what it's used for though...
KeyboardState result = KeyboardState.GetKeyboardState(modelId);
if (result == null || result.FirmwareId != firmwareId)
{
LogHandshakeFailed(stream.Device, "Couldn't find data for keyboard");
return null;
}
result.FirmwareMinorVersion = firmwareMinorVersion;
result.FirmwareMajorVersion = firmwareMajorVersion;
result.InitializeBuffers(bufferSizeA, bufferSizeB);
return result;
}
catch (Exception e)
{
LogHandshakeFailed(stream.Device, "Exception occured. " + e);
return null;
}
}
private static Packet WriteSimplePacket(HidStream stream, ushort opcode)
{
using (Packet packet = new Packet())
{
packet.WriteByte(0);// report id
packet.WriteUInt16(opcode);
packet.WriteBytes(new byte[62]);
byte[] buffer = packet.GetWrittenBuffer();
Crc16.InsertCrc(buffer, 1, 7);// offset for the report id byte
stream.Write(buffer);
byte[] resultBufferWithReportId = new byte[65];
stream.Read(resultBufferWithReportId);
if (resultBufferWithReportId[0] != 0)
{
return null;
}
byte[] resultBuffer = new byte[64];
Buffer.BlockCopy(resultBufferWithReportId, 1, resultBuffer, 0, resultBuffer.Length);
// All handshake packets should have a result code of 1
if (resultBuffer[2] != 1 ||
resultBuffer[0] != (byte)opcode || resultBuffer[1] != opcode >> 8)
{
return null;
}
if (!Crc16.ValidateCrc(resultBuffer))
{
return null;
}
Packet result = new Packet(true, resultBuffer);
result.Index = 8;
return result;
}
}
private static void LogHandshakeFailed(HidDevice device, string str)
{
Log("Handshake failed (" + device.GetFriendlyName() + ") " + str);
}
internal static void Log(string str)
{
Program.Log(str);
}
}
}