-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathWindowsHidDeviceFactoryExtensions.cs
More file actions
219 lines (199 loc) · 12.5 KB
/
WindowsHidDeviceFactoryExtensions.cs
File metadata and controls
219 lines (199 loc) · 12.5 KB
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
using Device.Net;
using Device.Net.Exceptions;
using Device.Net.Windows;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Hid.Net.Windows
{
/// <summary>
/// Instantiates Windows Hid Factories. Use these methods as extension methods with <see cref="FilterDeviceDefinition"/> or directly to get all devices
/// </summary>
public static class WindowsHidDeviceFactoryExtensions
{
#region Public Methods
/// <summary>
/// Creates a <see cref="IDeviceFactory"/> for Windows Hid devices
/// </summary>
/// <param name="loggerFactory"><see href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.iloggerfactory"/></param>
/// <param name="hidApiService">Abstraction for raw api level interation</param>
/// <param name="classGuid">Filters by specified class guid</param>
/// <param name="readBufferSize">Override the input report size</param>
/// <param name="writeBufferSize">Override the output report size</param>
/// <param name="getConnectedDeviceDefinitionsAsync">Override the default call for getting definitions</param>
/// <param name="readReportTransform">Allows you to manually convert the <see cref="Report"/> in to a <see cref="TransferResult"/> so that the Report Id is not discarded on ReadAsync. By default, this inserts the Report Id at index zero of the array.</param>
/// <param name="readTransferTransform">Exposes the raw data from the device (including Report Id) on reads and allows you to format the returned <see cref="TransferResult"/></param>
/// <param name="writeTransferTransform">Given the Report Id and data supplied for the write, allow you to format the raw data that is sent to the device</param>
/// <param name="writeReportTransform">Given the data supplied, allow you to divide the data in to a <see cref="Report"/></param>
/// <param name="createReadConnection">Allows you to specify the API level call to create the Read handle</param>
/// <param name="createWriteConnection">Allows you to specify the API level call to create the Write handle</param>
/// <returns>A factory which enumerates and instantiates devices</returns>
public static IDeviceFactory CreateWindowsHidDeviceFactory(
ILoggerFactory loggerFactory = null,
IHidApiService hidApiService = null,
Guid? classGuid = null,
ushort? readBufferSize = null,
ushort? writeBufferSize = null,
GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
Func<Report, TransferResult> readReportTransform = null,
Func<TransferResult, Report> readTransferTransform = null,
Func<byte[], byte, byte[]> writeTransferTransform = null,
WriteReportTransform writeReportTransform = null,
CreateConnection createReadConnection = null,
CreateConnection createWriteConnection = null)
{
return CreateWindowsHidDeviceFactory(
new ReadOnlyCollection<FilterDeviceDefinition>(new List<FilterDeviceDefinition>()),
loggerFactory,
hidApiService,
classGuid,
readBufferSize,
writeBufferSize,
getConnectedDeviceDefinitionsAsync,
readReportTransform,
readTransferTransform,
writeTransferTransform,
writeReportTransform: writeReportTransform,
createReadConnection,
createWriteConnection);
}
/// <summary>
/// Creates a <see cref="IDeviceFactory"/> for Windows Hid devices
/// </summary>
/// <param name="filterDeviceDefinition">Devices must match this</param>
/// <param name="loggerFactory"><see href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.iloggerfactory"/></param>
/// <param name="hidApiService">Abstraction for Hid interaction</param>
/// <param name="classGuid">Filters by specified class guid</param>
/// <param name="readBufferSize">Override the input report size</param>
/// <param name="writeBufferSize">Override the output report size</param>
/// <param name="writeReportTransform">Given the data supplied, allow you to divide the data in to a <see cref="Report"/></param>
/// <param name="getConnectedDeviceDefinitionsAsync">Override the default call for getting definitions</param>
/// <param name="readReportTransform">Allows you to manually convert the <see cref="Report"/> in to a <see cref="TransferResult"/> so that the Report Id is not discarded on ReadAsync. By default, this inserts the Report Id at index zero of the array.</param>
/// <param name="readTransferTransform">Exposes the raw data from the device (including Report Id) on reads and allows you to format the returned <see cref="TransferResult"/></param>
/// <param name="writeTransferTransform">Given the Report Id and data supplied for the write, allow you to format the raw data that is sent to the device</param>
/// <param name="createReadConnection">Allows you to specify the API level call to create the Read handle</param>
/// <param name="createWriteConnection">Allows you to specify the API level call to create the Write handle</param>
/// <returns>A factory which enumerates and instantiates devices</returns>
public static IDeviceFactory CreateWindowsHidDeviceFactory(
this FilterDeviceDefinition filterDeviceDefinition,
ILoggerFactory loggerFactory = null,
IHidApiService hidApiService = null,
Guid? classGuid = null,
ushort? readBufferSize = null,
ushort? writeBufferSize = null,
GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
Func<Report, TransferResult> readReportTransform = null,
Func<TransferResult, Report> readTransferTransform = null,
Func<byte[], byte, byte[]> writeTransferTransform = null,
WriteReportTransform writeReportTransform = null,
CreateConnection createReadConnection = null,
CreateConnection createWriteConnection = null)
{
return CreateWindowsHidDeviceFactory(
new ReadOnlyCollection<FilterDeviceDefinition>(new List<FilterDeviceDefinition> { filterDeviceDefinition }),
loggerFactory,
hidApiService,
classGuid,
readBufferSize,
writeBufferSize,
getConnectedDeviceDefinitionsAsync,
readReportTransform,
readTransferTransform,
writeTransferTransform,
writeReportTransform,
createReadConnection,
createWriteConnection);
}
/// <summary>
/// Creates a factory Hid devices
/// </summary>
/// <param name="filterDeviceDefinitions">Devices must match these</param>
/// <param name="loggerFactory"><see href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.iloggerfactory"/></param>
/// <param name="hidApiService">Abstraction for Hid interaction</param>
/// <param name="classGuid">Filters by specified class guid</param>
/// <param name="readBufferSize">Override the input report size</param>
/// <param name="writeBufferSize">Override the output report size</param>
/// <param name="getConnectedDeviceDefinitionsAsync">Override the default call for getting definitions</param>
/// <param name="readReportTransform">Allows you to manually convert the <see cref="Report"/> in to a <see cref="TransferResult"/> so that the Report Id is not discarded on ReadAsync. By default, this inserts the Report Id at index zero of the array.</param>
/// <param name="readTransferTransform">Exposes the raw data from the device (including Report Id) on reads and allows you to format the returned <see cref="TransferResult"/></param>
/// <param name="writeTransferTransform">Given the Report Id and data supplied for the write, allow you to format the raw data that is sent to the device</param>
/// <param name="writeReportTransform">Given the data supplied, allow you to divide the data in to a <see cref="Report"/></param>
/// <param name="createReadConnection">Allows you to specify the API level call to create the Read handle</param>
/// <param name="createWriteConnection">Allows you to specify the API level call to create the Write handle</param>
/// <returns>A factory which enumerates and instantiates devices</returns>
public static IDeviceFactory CreateWindowsHidDeviceFactory(
this IEnumerable<FilterDeviceDefinition> filterDeviceDefinitions,
ILoggerFactory loggerFactory = null,
IHidApiService hidApiService = null,
Guid? classGuid = null,
ushort? readBufferSize = null,
ushort? writeBufferSize = null,
GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
Func<Report, TransferResult> readReportTransform = null,
Func<TransferResult, Report> readTransferTransform = null,
Func<byte[], byte, byte[]> writeTransferTransform = null,
WriteReportTransform writeReportTransform = null,
CreateConnection createReadConnection = null,
CreateConnection createWriteConnection = null)
{
if (filterDeviceDefinitions == null) throw new ArgumentNullException(nameof(filterDeviceDefinitions));
loggerFactory ??= NullLoggerFactory.Instance;
var selectedHidApiService = hidApiService ?? new WindowsHidApiService(loggerFactory);
classGuid ??= selectedHidApiService.GetHidGuid();
if (getConnectedDeviceDefinitionsAsync == null)
{
var windowsDeviceEnumerator = new WindowsDeviceEnumerator(
loggerFactory.CreateLogger<WindowsDeviceEnumerator>(),
classGuid.Value,
(d, guid) => GetDeviceDefinition(d, selectedHidApiService, loggerFactory.CreateLogger(nameof(WindowsHidDeviceFactoryExtensions))),
c => Task.FromResult(!filterDeviceDefinitions.Any() || filterDeviceDefinitions.FirstOrDefault(f => f.IsDefinitionMatch(c, DeviceType.Hid)) != null)
);
getConnectedDeviceDefinitionsAsync = windowsDeviceEnumerator.GetConnectedDeviceDefinitionsAsync;
}
return new DeviceFactory(
loggerFactory,
getConnectedDeviceDefinitionsAsync,
(c, cancellationToken) => Task.FromResult<IDevice>(new HidDevice
(
new WindowsHidHandler(
c.DeviceId,
writeBufferSize,
readBufferSize,
hidApiService,
loggerFactory,
readTransferTransform,
writeTransferTransform,
createReadConnection,
createWriteConnection),
loggerFactory,
readReportTransform,
writeReportTransform
)),
(c, cancellationToken) => Task.FromResult(c.DeviceType == DeviceType.Hid));
}
#endregion Public Methods
#region Private Methods
private static ConnectedDeviceDefinition GetDeviceDefinition(string deviceId, IHidApiService HidService, ILogger logger)
{
logger ??= NullLogger.Instance;
using var logScope = logger.BeginScope("DeviceId: {deviceId} Call: {call}", deviceId, nameof(GetDeviceDefinition));
try
{
using var safeFileHandle = HidService.CreateReadConnection(deviceId, FileAccessRights.None);
if (safeFileHandle.IsInvalid) throw new DeviceException($"{nameof(HidService.CreateReadConnection)} call with Id of {deviceId} failed.");
logger.LogDebug(Messages.InformationMessageFoundDevice);
return HidService.GetDeviceDefinition(deviceId, safeFileHandle);
}
catch (Exception ex)
{
logger.LogError(ex, Messages.ErrorMessageCouldntGetDevice);
return null;
}
}
#endregion Private Methods
}
}