-
Notifications
You must be signed in to change notification settings - Fork 17
/
UsbHidDevice.cs
199 lines (160 loc) · 5.48 KB
/
UsbHidDevice.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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using UsbHid.USB.Classes;
using UsbHid.USB.Classes.Messaging;
using UsbHid.USB.Structures;
namespace UsbHid
{
public class UsbHidDevice : IDisposable
{
#region Variables
private DeviceInformationStructure _deviceInformation;
public string DevicePath { get { return _deviceInformation.DevicePathName; } }
public bool IsDeviceConnected { get { return _deviceInformation.IsDeviceAttached; } }
public UsbDescriptorStrings DescriptorStrings { get { return _deviceInformation.DescriptorStrings; } }
private readonly BackgroundWorker _worker;
private FileStream _fsDeviceRead;
#endregion
#region Delegates
public delegate void DataReceivedDelegate(byte[] data);
public event DataReceivedDelegate DataReceived;
public delegate void ConnectedDelegate();
public event ConnectedDelegate OnConnected;
public delegate void DisConnectedDelegate();
public event DisConnectedDelegate OnDisConnected;
#endregion
#region Construction
public UsbHidDevice(string devicePath)
{
_deviceInformation.DevicePathName = devicePath;
_worker = new BackgroundWorker();
_worker.DoWork += WorkerDoWork;
if (DeviceDiscovery.FindTargetDevice(ref _deviceInformation))
{
_worker.RunWorkerAsync();
}
_deviceInformation.ConnectedChanged += DeviceConnectedChanged;
DeviceChangeNotifier.DeviceAttached += DeviceChangeNotifierDeviceAttached;
DeviceChangeNotifier.DeviceDetached += DeviceChangeNotifierDeviceDetached;
}
~UsbHidDevice()
{
Disconnect();
}
#endregion
#region Event Handlers
private void ReadCompleted(IAsyncResult iResult)
{
// Retrieve the stream and read buffer.
var syncObj = (SyncObjT)iResult.AsyncState;
try
{
// call end read : this throws any exceptions that happened during the read
syncObj.Fs.EndRead(iResult);
try
{
if (DataReceived != null) DataReceived(syncObj.Buf);
}
finally
{
// when all that is done, kick off another read for the next report
BeginAsyncRead(ref syncObj.Fs, syncObj.Buf.Length);
}
}
catch (IOException ex) // if we got an IO exception, the device was removed
{
Debug.WriteLine(ex.ToString());
}
}
private void DeviceConnectedChanged(bool isConnected)
{
if (isConnected)
{
ReportConnected();
_worker.RunWorkerAsync();
}
else
{
ReportDisConnected();
}
}
private void DeviceChangeNotifierDeviceDetached()
{
Disconnect();
}
private void DeviceChangeNotifierDeviceAttached()
{
if(IsDeviceConnected) Disconnect();
//TODO: FIX THIS!
}
#endregion
#region Methods
#region Public
public bool Connect()
{
//TODO: FIX THIS!
return IsDeviceConnected;
}
public void Disconnect()
{
if (_fsDeviceRead != null)
{
_fsDeviceRead.Close();
}
if (IsDeviceConnected)
{
_deviceInformation.HidHandle.Close();
_deviceInformation.ReadHandle.Close();
_deviceInformation.WriteHandle.Close();
_deviceInformation.IsDeviceAttached = false;
}
}
public bool SendMessage(IMesage message)
{
return DeviceCommunication.WriteRawReportToDevice(message.MessageData, ref _deviceInformation);
}
public bool SendCommandMessage(byte command)
{
var message = new CommandMessage(command);
return DeviceCommunication.WriteRawReportToDevice(message.MessageData, ref _deviceInformation);
}
#endregion
#region Private
private void BeginAsyncRead(ref FileStream fs, int iBufLen)
{
var syncObj = new SyncObjT { Fs = fs, Buf = new Byte[iBufLen] };
try
{
fs.BeginRead(syncObj.Buf, 0, iBufLen, ReadCompleted, syncObj);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void WorkerDoWork(object sender, DoWorkEventArgs e)
{
_fsDeviceRead = new FileStream(_deviceInformation.ReadHandle, FileAccess.Read, 0x1000, true);
BeginAsyncRead(ref _fsDeviceRead, _deviceInformation.Capabilities.InputReportByteLength);
}
private void ReportConnected()
{
if (OnConnected != null) OnConnected();
}
private void ReportDisConnected()
{
if (OnDisConnected != null) OnDisConnected();
}
#endregion
#endregion
#region IDisposable Members
public void Dispose()
{
Disconnect();
GC.SuppressFinalize(this);
}
#endregion
}
}