-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxHidStream.cs
269 lines (236 loc) · 8.25 KB
/
LinuxHidStream.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
#region License
/* Copyright 2012 James F. Bellinger <http://www.zer7.com/software/hidsharp>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
namespace HidSharp.Platform.Linux
{
class LinuxHidStream : HidStream
{
Queue<byte[]> _inputQueue;
Queue<CommonOutputReport> _outputQueue;
LinuxHidDevice _device;
int _handle;
Thread _readThread, _writeThread;
volatile bool _shutdown;
internal LinuxHidStream()
{
_inputQueue = new Queue<byte[]>();
_outputQueue = new Queue<CommonOutputReport>();
_handle = -1;
_readThread = new Thread(ReadThread);
_readThread.IsBackground = true;
_writeThread = new Thread(WriteThread);
_writeThread.IsBackground = true;
}
static int DeviceHandleFromPath(string path)
{
IntPtr udev = NativeMethods.udev_new();
if (IntPtr.Zero != udev)
{
try
{
IntPtr device = NativeMethods.udev_device_new_from_syspath(udev, path);
if (IntPtr.Zero != device)
{
try
{
string devnode = NativeMethods.udev_device_get_devnode(device);
if (devnode != null)
{
int handle = NativeMethods.retry(() => NativeMethods.open
(devnode, NativeMethods.oflag.RDWR | NativeMethods.oflag.NONBLOCK));
if (handle < 0)
{
var error = (NativeMethods.error)Marshal.GetLastWin32Error();
if (error == NativeMethods.error.EACCES)
{
throw new UnauthorizedAccessException("Not permitted to open HID class device at " + devnode + ".");
}
else
{
throw new IOException("Unable to open HID class device (" + error.ToString() + ").");
}
}
return handle;
}
}
finally
{
NativeMethods.udev_device_unref(device);
}
}
}
finally
{
NativeMethods.udev_unref(udev);
}
}
throw new FileNotFoundException("HID class device not found.");
}
internal void Init(string path, LinuxHidDevice device)
{
int handle;
handle = DeviceHandleFromPath(path);
_device = device;
_handle = handle;
HandleInitAndOpen();
_readThread.Start();
_writeThread.Start();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!HandleClose()) { return; }
_shutdown = true;
try { lock (_inputQueue) { Monitor.PulseAll(_inputQueue); } } catch { }
try { lock (_outputQueue) { Monitor.PulseAll(_outputQueue); } } catch { }
try { _readThread.Join(); } catch { }
try { _writeThread.Join(); } catch { }
HandleRelease();
}
internal override void HandleFree()
{
NativeMethods.retry(() => NativeMethods.close(_handle)); _handle = -1;
}
unsafe void ReadThread()
{
if (!HandleAcquire()) { return; }
try
{
lock (_inputQueue)
{
while (true)
{
var fds = new NativeMethods.pollfd[1];
fds[0].fd = _handle;
fds[0].events = NativeMethods.pollev.IN;
while (!_shutdown)
{
tryReadAgain:
int ret;
Monitor.Exit(_inputQueue);
try { ret = NativeMethods.retry(() => NativeMethods.poll(fds, (IntPtr)1, 250)); }
finally { Monitor.Enter(_inputQueue); }
if (ret != 1) { continue; }
if (0 != (fds[0].revents & (NativeMethods.pollev.ERR | NativeMethods.pollev.HUP))) { break; }
if (0 != (fds[0].revents & NativeMethods.pollev.IN))
{
// Linux doesn't provide a Report ID if the device doesn't use one.
int inputLength = _device.MaxInputReportLength;
if (inputLength > 0 && !_device.ReportsUseID) { inputLength--; }
byte[] inputReport = new byte[inputLength];
fixed (byte* inputBytes = inputReport)
{
var inputBytesPtr = (IntPtr)inputBytes;
IntPtr length = NativeMethods.retry(() => NativeMethods.read
(_handle, inputBytesPtr, (IntPtr)inputReport.Length));
if ((long)length < 0)
{
var error = (NativeMethods.error)Marshal.GetLastWin32Error();
if (error != NativeMethods.error.EAGAIN) { break; }
goto tryReadAgain;
}
Array.Resize(ref inputReport, (int)length); // No Report ID? First byte becomes Report ID 0.
if (!_device.ReportsUseID) { inputReport = new byte[1].Concat(inputReport).ToArray(); }
_inputQueue.Enqueue(inputReport); Monitor.PulseAll(_inputQueue);
}
}
}
while (!_shutdown && _inputQueue.Count == 0) { Monitor.Wait(_inputQueue); }
if (_shutdown) { break; }
_inputQueue.Dequeue();
}
}
}
finally
{
HandleRelease();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return CommonRead(buffer, offset, count, _inputQueue);
}
public override void GetFeature(byte[] buffer, int offset, int count)
{
throw new NotSupportedException(); // TODO
}
unsafe void WriteThread()
{
if (!HandleAcquire()) { return; }
try
{
lock (_outputQueue)
{
while (true)
{
while (!_shutdown && _outputQueue.Count == 0) { Monitor.Wait(_outputQueue); }
if (_shutdown) { break; }
CommonOutputReport outputReport = _outputQueue.Peek();
// Linux doesn't expect a Report ID if the device doesn't use one.
byte[] outputBytesRaw = outputReport.Bytes;
if (!_device.ReportsUseID && outputBytesRaw.Length > 0) { outputBytesRaw = outputBytesRaw.Skip(1).ToArray(); }
try
{
fixed (byte* outputBytes = outputBytesRaw)
{
// hidraw is apparently blocking for output, even when O_NONBLOCK is used.
// See for yourself at drivers/hid/hidraw.c...
IntPtr length;
Monitor.Exit(_outputQueue);
try
{
var outputBytesPtr = (IntPtr)outputBytes;
length = NativeMethods.retry(() => NativeMethods.write
(_handle, outputBytesPtr, (IntPtr)outputBytesRaw.Length));
if ((long)length == outputBytesRaw.Length) { outputReport.DoneOK = true; }
}
finally
{
Monitor.Enter(_outputQueue);
}
}
}
finally
{
_outputQueue.Dequeue();
outputReport.Done = true;
Monitor.PulseAll(_outputQueue);
}
}
}
}
finally
{
HandleRelease();
}
}
public override void Write(byte[] buffer, int offset, int count)
{
CommonWrite(buffer, offset, count, _outputQueue, false, _device.MaxOutputReportLength);
}
public override void SetFeature(byte[] buffer, int offset, int count)
{
throw new NotSupportedException(); // TODO
}
public override HidDevice Device
{
get { return _device; }
}
}
}