Initial version
This commit is contained in:
parent
6b01ec5991
commit
28f25e8294
|
@ -0,0 +1,39 @@
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.10.34928.147
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EonaCat.HID", "EonaCat.HID\EonaCat.HID.csproj", "{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Debug|Mixed Platforms = Debug|Mixed Platforms
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
|
Debug|x86 = Debug|x86
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
Release|Mixed Platforms = Release|Mixed Platforms
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Release|x86 = Release|x86
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{9E8F1D50-74EA-4C60-BD5C-AB2C5B53BC66}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {3EA279F1-3C81-4710-A932-87EE335DC024}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
|
@ -0,0 +1,833 @@
|
||||||
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class Device : IDevice
|
||||||
|
{
|
||||||
|
public event InsertedEventHandler Inserted;
|
||||||
|
public event RemovedEventHandler Removed;
|
||||||
|
|
||||||
|
private readonly string _description;
|
||||||
|
private readonly string _devicePath;
|
||||||
|
private readonly HidDeviceAttributes _deviceAttributes;
|
||||||
|
|
||||||
|
private readonly HidDeviceCapabilities _deviceCapabilities;
|
||||||
|
private DeviceMode _deviceReadMode = DeviceMode.NonOverlapped;
|
||||||
|
private DeviceMode _deviceWriteMode = DeviceMode.NonOverlapped;
|
||||||
|
private ShareMode _deviceShareMode = ShareMode.ShareRead | ShareMode.ShareWrite;
|
||||||
|
|
||||||
|
private readonly DeviceEventMonitor _deviceEventMonitor;
|
||||||
|
|
||||||
|
private bool _monitorDeviceEvents;
|
||||||
|
protected delegate DeviceData ReadDelegate(int timeout);
|
||||||
|
protected delegate HidReport ReadReportDelegate(int timeout);
|
||||||
|
private delegate bool WriteDelegate(byte[] data, int timeout);
|
||||||
|
private delegate bool WriteReportDelegate(HidReport report, int timeout);
|
||||||
|
|
||||||
|
internal Device(string devicePath, string description = null)
|
||||||
|
{
|
||||||
|
_deviceEventMonitor = new DeviceEventMonitor(this);
|
||||||
|
_deviceEventMonitor.Inserted += DeviceEventMonitorInserted;
|
||||||
|
_deviceEventMonitor.Removed += DeviceEventMonitorRemoved;
|
||||||
|
|
||||||
|
_devicePath = devicePath;
|
||||||
|
_description = description;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
|
||||||
|
|
||||||
|
_deviceAttributes = GetDeviceAttributes(hidHandle);
|
||||||
|
_deviceCapabilities = GetDeviceCapabilities(hidHandle);
|
||||||
|
|
||||||
|
CloseDeviceIO(hidHandle);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("EonaCat HID: Error querying HID device '{0}'.", devicePath), exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntPtr ReadHandle { get; private set; }
|
||||||
|
public IntPtr WriteHandle { get; private set; }
|
||||||
|
public bool IsOpen { get; private set; }
|
||||||
|
public bool IsConnected { get { return Devices.IsConnected(_devicePath); } }
|
||||||
|
public string Description { get { return _description; } }
|
||||||
|
public HidDeviceCapabilities Capabilities { get { return _deviceCapabilities; } }
|
||||||
|
public HidDeviceAttributes Attributes { get { return _deviceAttributes; } }
|
||||||
|
public string DevicePath { get { return _devicePath; } }
|
||||||
|
|
||||||
|
public bool MonitorDeviceEvents
|
||||||
|
{
|
||||||
|
get { return _monitorDeviceEvents; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value & _monitorDeviceEvents == false)
|
||||||
|
{
|
||||||
|
_deviceEventMonitor.Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
_monitorDeviceEvents = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return string.Format("VendorID={0}, ProductID={1}, Version={2}, DevicePath={3}",
|
||||||
|
_deviceAttributes.VendorHexId,
|
||||||
|
_deviceAttributes.ProductHexId,
|
||||||
|
_deviceAttributes.Version,
|
||||||
|
_devicePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OpenDevice()
|
||||||
|
{
|
||||||
|
OpenDevice(DeviceMode.NonOverlapped, DeviceMode.NonOverlapped, ShareMode.ShareRead | ShareMode.ShareWrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OpenDevice(DeviceMode readMode, DeviceMode writeMode, ShareMode shareMode)
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_deviceReadMode = readMode;
|
||||||
|
_deviceWriteMode = writeMode;
|
||||||
|
_deviceShareMode = shareMode;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ReadHandle = OpenDeviceIO(_devicePath, readMode, NativeMethods.GENERIC_READ, shareMode);
|
||||||
|
WriteHandle = OpenDeviceIO(_devicePath, writeMode, NativeMethods.GENERIC_WRITE, shareMode);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
IsOpen = false;
|
||||||
|
throw new Exception("EonaCat HID: Error opening HID device.", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
IsOpen = (ReadHandle.ToInt32() != NativeMethods.INVALID_HANDLE_VALUE &&
|
||||||
|
WriteHandle.ToInt32() != NativeMethods.INVALID_HANDLE_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void CloseDevice()
|
||||||
|
{
|
||||||
|
if (!IsOpen)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseDeviceIO(ReadHandle);
|
||||||
|
CloseDeviceIO(WriteHandle);
|
||||||
|
IsOpen = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceData Read()
|
||||||
|
{
|
||||||
|
return Read(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceData Read(int timeout)
|
||||||
|
{
|
||||||
|
if (IsConnected)
|
||||||
|
{
|
||||||
|
if (IsOpen == false)
|
||||||
|
{
|
||||||
|
OpenDevice(_deviceReadMode, _deviceWriteMode, _deviceShareMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return ReadData(timeout);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return new DeviceData(DeviceData.ReadStatus.ReadError);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return new DeviceData(DeviceData.ReadStatus.NotConnected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Read(ReadCallback callback)
|
||||||
|
{
|
||||||
|
Read(callback, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Read(ReadCallback callback, int timeout)
|
||||||
|
{
|
||||||
|
var readDelegate = new ReadDelegate(Read);
|
||||||
|
var asyncState = new StateAsync(readDelegate, callback);
|
||||||
|
readDelegate.BeginInvoke(timeout, EndRead, asyncState);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<DeviceData> ReadAsync(int timeout = 0)
|
||||||
|
{
|
||||||
|
var readDelegate = new ReadDelegate(Read);
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
return await Task<DeviceData>.Factory.StartNew(() => readDelegate.Invoke(timeout));
|
||||||
|
#else
|
||||||
|
return await Task<DeviceData>.Factory.FromAsync(readDelegate.BeginInvoke, readDelegate.EndInvoke, timeout, null);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public HidReport ReadReport(int timeout = 0)
|
||||||
|
{
|
||||||
|
return new HidReport(Capabilities.InputReportByteLength, Read(timeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ReadReport(ReadReportCallback callback, int timeout = 0)
|
||||||
|
{
|
||||||
|
var readReportDelegate = new ReadReportDelegate(ReadReport);
|
||||||
|
var asyncState = new StateAsync(readReportDelegate, callback);
|
||||||
|
readReportDelegate.BeginInvoke(timeout, EndReadReport, asyncState);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<HidReport> ReadReportAsync(int timeout = 0)
|
||||||
|
{
|
||||||
|
var readReportDelegate = new ReadReportDelegate(ReadReport);
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
return await Task<HidReport>.Factory.StartNew(() => readReportDelegate.Invoke(timeout));
|
||||||
|
#else
|
||||||
|
return await Task<HidReport>.Factory.FromAsync(readReportDelegate.BeginInvoke, readReportDelegate.EndInvoke, timeout, null);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public HidReport ReadReportSync(byte reportId)
|
||||||
|
{
|
||||||
|
byte[] cmdBuffer = new byte[Capabilities.InputReportByteLength];
|
||||||
|
cmdBuffer[0] = reportId;
|
||||||
|
bool bSuccess = NativeMethods.HidD_GetInputReport(ReadHandle, cmdBuffer, cmdBuffer.Length);
|
||||||
|
DeviceData deviceData = new DeviceData(cmdBuffer, bSuccess ? DeviceData.ReadStatus.Success : DeviceData.ReadStatus.NoDataRead);
|
||||||
|
return new HidReport(Capabilities.InputReportByteLength, deviceData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ReadFeatureData(out byte[] data, byte reportId = 0)
|
||||||
|
{
|
||||||
|
if (_deviceCapabilities.FeatureReportByteLength <= 0)
|
||||||
|
{
|
||||||
|
data = new byte[0];
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
data = new byte[_deviceCapabilities.FeatureReportByteLength];
|
||||||
|
|
||||||
|
var buffer = CreateFeatureOutputBuffer();
|
||||||
|
buffer[0] = reportId;
|
||||||
|
|
||||||
|
IntPtr hidHandle = IntPtr.Zero;
|
||||||
|
bool success = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
hidHandle = ReadHandle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
success = NativeMethods.HidD_GetFeature(hidHandle, buffer, buffer.Length);
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
Array.Copy(buffer, 0, data, 0, Math.Min(data.Length, _deviceCapabilities.FeatureReportByteLength));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("EonaCat HID: Error accessing HID device '{0}'.", _devicePath), exception);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (hidHandle != IntPtr.Zero && hidHandle != ReadHandle)
|
||||||
|
{
|
||||||
|
CloseDeviceIO(hidHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ReadProduct()
|
||||||
|
{
|
||||||
|
if (ReadProduct(out byte[] data))
|
||||||
|
{
|
||||||
|
return System.Text.Encoding.ASCII.GetString(data).Replace("\0", string.Empty);
|
||||||
|
}
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ReadProduct(out byte[] data)
|
||||||
|
{
|
||||||
|
data = new byte[254];
|
||||||
|
IntPtr hidHandle = IntPtr.Zero;
|
||||||
|
bool success = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
hidHandle = ReadHandle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
success = NativeMethods.HidD_GetProductString(hidHandle, data, data.Length);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("EonaCat HID: Error accessing HID device '{0}'.", _devicePath), exception);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (hidHandle != IntPtr.Zero && hidHandle != ReadHandle)
|
||||||
|
{
|
||||||
|
CloseDeviceIO(hidHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ReadManufacturer()
|
||||||
|
{
|
||||||
|
if (ReadManufacturer(out byte[] data))
|
||||||
|
{
|
||||||
|
return System.Text.Encoding.ASCII.GetString(data).Replace("\0", string.Empty);
|
||||||
|
}
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ReadManufacturer(out byte[] data)
|
||||||
|
{
|
||||||
|
data = new byte[254];
|
||||||
|
IntPtr hidHandle = IntPtr.Zero;
|
||||||
|
bool success = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
hidHandle = ReadHandle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
success = NativeMethods.HidD_GetManufacturerString(hidHandle, data, data.Length);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("EonaCat HID: Error accessing HID device '{0}'.", _devicePath), exception);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (hidHandle != IntPtr.Zero && hidHandle != ReadHandle)
|
||||||
|
{
|
||||||
|
CloseDeviceIO(hidHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ReadSerialNumber()
|
||||||
|
{
|
||||||
|
if (ReadSerialNumber(out byte[] data))
|
||||||
|
{
|
||||||
|
return System.Text.Encoding.ASCII.GetString(data).Replace("\0", string.Empty);
|
||||||
|
}
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ReadSerialNumber(out byte[] data)
|
||||||
|
{
|
||||||
|
data = new byte[254];
|
||||||
|
IntPtr hidHandle = IntPtr.Zero;
|
||||||
|
bool success = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
hidHandle = ReadHandle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
success = NativeMethods.HidD_GetSerialNumberString(hidHandle, data, data.Length);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("EonaCat HID: Error accessing HID device '{0}'.", _devicePath), exception);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (hidHandle != IntPtr.Zero && hidHandle != ReadHandle)
|
||||||
|
{
|
||||||
|
CloseDeviceIO(hidHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Write(byte[] data)
|
||||||
|
{
|
||||||
|
return Write(data, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Write(byte[] data, int timeout)
|
||||||
|
{
|
||||||
|
if (IsConnected)
|
||||||
|
{
|
||||||
|
if (IsOpen == false)
|
||||||
|
{
|
||||||
|
OpenDevice(_deviceReadMode, _deviceWriteMode, _deviceShareMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return WriteData(data, timeout);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(byte[] data, WriteCallback callback)
|
||||||
|
{
|
||||||
|
Write(data, callback, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(byte[] data, WriteCallback callback, int timeout)
|
||||||
|
{
|
||||||
|
var writeDelegate = new WriteDelegate(Write);
|
||||||
|
var asyncState = new StateAsync(writeDelegate, callback);
|
||||||
|
writeDelegate.BeginInvoke(data, timeout, EndWrite, asyncState);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> WriteAsync(byte[] data, int timeout = 0)
|
||||||
|
{
|
||||||
|
var writeDelegate = new WriteDelegate(Write);
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
return await Task<bool>.Factory.StartNew(() => writeDelegate.Invoke(data, timeout));
|
||||||
|
#else
|
||||||
|
return await Task<bool>.Factory.FromAsync(writeDelegate.BeginInvoke, writeDelegate.EndInvoke, data, timeout, null);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool WriteReport(HidReport report)
|
||||||
|
{
|
||||||
|
return WriteReport(report, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool WriteReport(HidReport report, int timeout)
|
||||||
|
{
|
||||||
|
return Write(report.GetBytes(), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteReport(HidReport report, WriteCallback callback)
|
||||||
|
{
|
||||||
|
WriteReport(report, callback, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteReport(HidReport report, WriteCallback callback, int timeout)
|
||||||
|
{
|
||||||
|
var writeReportDelegate = new WriteReportDelegate(WriteReport);
|
||||||
|
var asyncState = new StateAsync(writeReportDelegate, callback);
|
||||||
|
writeReportDelegate.BeginInvoke(report, timeout, EndWriteReport, asyncState);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle data transfers on the control channel. This method places data on the control channel for devices
|
||||||
|
/// that do not support the interupt transfers
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="report">The outbound HID report</param>
|
||||||
|
/// <returns>The result of the tranfer request: true if successful otherwise false</returns>
|
||||||
|
///
|
||||||
|
public bool WriteReportSync(HidReport report)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (null != report)
|
||||||
|
{
|
||||||
|
byte[] buffer = report.GetBytes();
|
||||||
|
return (NativeMethods.HidD_SetOutputReport(WriteHandle, buffer, buffer.Length));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("The output report is null, it must be allocated before you call this method", "report");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> WriteReportAsync(HidReport report, int timeout = 0)
|
||||||
|
{
|
||||||
|
var writeReportDelegate = new WriteReportDelegate(WriteReport);
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
return await Task<bool>.Factory.StartNew(() => writeReportDelegate.Invoke(report, timeout));
|
||||||
|
#else
|
||||||
|
return await Task<bool>.Factory.FromAsync(writeReportDelegate.BeginInvoke, writeReportDelegate.EndInvoke, report, timeout, null);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public HidReport CreateReport()
|
||||||
|
{
|
||||||
|
return new HidReport(Capabilities.OutputReportByteLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool WriteFeatureData(byte[] data)
|
||||||
|
{
|
||||||
|
if (_deviceCapabilities.FeatureReportByteLength <= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer = CreateFeatureOutputBuffer();
|
||||||
|
|
||||||
|
Array.Copy(data, 0, buffer, 0, Math.Min(data.Length, _deviceCapabilities.FeatureReportByteLength));
|
||||||
|
|
||||||
|
|
||||||
|
IntPtr hidHandle = IntPtr.Zero;
|
||||||
|
bool success = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
hidHandle = WriteHandle;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
//var overlapped = new NativeOverlapped();
|
||||||
|
success = NativeMethods.HidD_SetFeature(hidHandle, buffer, buffer.Length);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("EonaCat HID: Error accessing HID device '{0}'.", _devicePath), exception);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (hidHandle != IntPtr.Zero && hidHandle != WriteHandle)
|
||||||
|
{
|
||||||
|
CloseDeviceIO(hidHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void EndRead(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
var hidAsyncState = (StateAsync)ar.AsyncState;
|
||||||
|
var callerDelegate = (ReadDelegate)hidAsyncState.CallerDelegate;
|
||||||
|
var callbackDelegate = (ReadCallback)hidAsyncState.CallbackDelegate;
|
||||||
|
var data = callerDelegate.EndInvoke(ar);
|
||||||
|
|
||||||
|
if ((callbackDelegate != null))
|
||||||
|
{
|
||||||
|
callbackDelegate.Invoke(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void EndReadReport(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
var hidAsyncState = (StateAsync)ar.AsyncState;
|
||||||
|
var callerDelegate = (ReadReportDelegate)hidAsyncState.CallerDelegate;
|
||||||
|
var callbackDelegate = (ReadReportCallback)hidAsyncState.CallbackDelegate;
|
||||||
|
var report = callerDelegate.EndInvoke(ar);
|
||||||
|
|
||||||
|
if ((callbackDelegate != null))
|
||||||
|
{
|
||||||
|
callbackDelegate.Invoke(report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EndWrite(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
var hidAsyncState = (StateAsync)ar.AsyncState;
|
||||||
|
var callerDelegate = (WriteDelegate)hidAsyncState.CallerDelegate;
|
||||||
|
var callbackDelegate = (WriteCallback)hidAsyncState.CallbackDelegate;
|
||||||
|
var result = callerDelegate.EndInvoke(ar);
|
||||||
|
|
||||||
|
if ((callbackDelegate != null))
|
||||||
|
{
|
||||||
|
callbackDelegate.Invoke(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EndWriteReport(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
var hidAsyncState = (StateAsync)ar.AsyncState;
|
||||||
|
var callerDelegate = (WriteReportDelegate)hidAsyncState.CallerDelegate;
|
||||||
|
var callbackDelegate = (WriteCallback)hidAsyncState.CallbackDelegate;
|
||||||
|
var result = callerDelegate.EndInvoke(ar);
|
||||||
|
|
||||||
|
if ((callbackDelegate != null))
|
||||||
|
{
|
||||||
|
callbackDelegate.Invoke(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] CreateInputBuffer()
|
||||||
|
{
|
||||||
|
return CreateBuffer(Capabilities.InputReportByteLength - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] CreateOutputBuffer()
|
||||||
|
{
|
||||||
|
return CreateBuffer(Capabilities.OutputReportByteLength - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] CreateFeatureOutputBuffer()
|
||||||
|
{
|
||||||
|
return CreateBuffer(Capabilities.FeatureReportByteLength - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] CreateBuffer(int length)
|
||||||
|
{
|
||||||
|
byte[] buffer = null;
|
||||||
|
Array.Resize(ref buffer, length + 1);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HidDeviceAttributes GetDeviceAttributes(IntPtr hidHandle)
|
||||||
|
{
|
||||||
|
var deviceAttributes = default(NativeMethods.HIDD_ATTRIBUTES);
|
||||||
|
deviceAttributes.Size = Marshal.SizeOf(deviceAttributes);
|
||||||
|
NativeMethods.HidD_GetAttributes(hidHandle, ref deviceAttributes);
|
||||||
|
return new HidDeviceAttributes(deviceAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HidDeviceCapabilities GetDeviceCapabilities(IntPtr hidHandle)
|
||||||
|
{
|
||||||
|
var capabilities = default(NativeMethods.HIDP_CAPS);
|
||||||
|
var preparsedDataPointer = default(IntPtr);
|
||||||
|
|
||||||
|
if (NativeMethods.HidD_GetPreparsedData(hidHandle, ref preparsedDataPointer))
|
||||||
|
{
|
||||||
|
NativeMethods.HidP_GetCaps(preparsedDataPointer, ref capabilities);
|
||||||
|
NativeMethods.HidD_FreePreparsedData(preparsedDataPointer);
|
||||||
|
}
|
||||||
|
return new HidDeviceCapabilities(capabilities);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool WriteData(byte[] data, int timeout)
|
||||||
|
{
|
||||||
|
if (_deviceCapabilities.OutputReportByteLength <= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer = CreateOutputBuffer();
|
||||||
|
uint bytesWritten;
|
||||||
|
|
||||||
|
Array.Copy(data, 0, buffer, 0, Math.Min(data.Length, _deviceCapabilities.OutputReportByteLength));
|
||||||
|
|
||||||
|
if (_deviceWriteMode == DeviceMode.Overlapped)
|
||||||
|
{
|
||||||
|
var security = new NativeMethods.SECURITY_ATTRIBUTES();
|
||||||
|
var overlapped = new NativeOverlapped();
|
||||||
|
|
||||||
|
var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;
|
||||||
|
|
||||||
|
security.lpSecurityDescriptor = IntPtr.Zero;
|
||||||
|
security.bInheritHandle = true;
|
||||||
|
security.nLength = Marshal.SizeOf(security);
|
||||||
|
|
||||||
|
overlapped.OffsetLow = 0;
|
||||||
|
overlapped.OffsetHigh = 0;
|
||||||
|
overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), "");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NativeMethods.WriteFile(WriteHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);
|
||||||
|
}
|
||||||
|
catch { return false; }
|
||||||
|
|
||||||
|
var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);
|
||||||
|
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case NativeMethods.WAIT_OBJECT_0:
|
||||||
|
return true;
|
||||||
|
case NativeMethods.WAIT_TIMEOUT:
|
||||||
|
return false;
|
||||||
|
case NativeMethods.WAIT_FAILED:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var overlapped = new NativeOverlapped();
|
||||||
|
return NativeMethods.WriteFile(WriteHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);
|
||||||
|
}
|
||||||
|
catch { return false; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DeviceData ReadData(int timeout)
|
||||||
|
{
|
||||||
|
var buffer = new byte[] { };
|
||||||
|
var status = DeviceData.ReadStatus.NoDataRead;
|
||||||
|
IntPtr nonManagedBuffer;
|
||||||
|
|
||||||
|
if (_deviceCapabilities.InputReportByteLength > 0)
|
||||||
|
{
|
||||||
|
uint bytesRead;
|
||||||
|
|
||||||
|
buffer = CreateInputBuffer();
|
||||||
|
nonManagedBuffer = Marshal.AllocHGlobal(buffer.Length);
|
||||||
|
|
||||||
|
if (_deviceReadMode == DeviceMode.Overlapped)
|
||||||
|
{
|
||||||
|
var security = new NativeMethods.SECURITY_ATTRIBUTES();
|
||||||
|
var overlapped = new NativeOverlapped();
|
||||||
|
var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;
|
||||||
|
|
||||||
|
security.lpSecurityDescriptor = IntPtr.Zero;
|
||||||
|
security.bInheritHandle = true;
|
||||||
|
security.nLength = Marshal.SizeOf(security);
|
||||||
|
|
||||||
|
overlapped.OffsetLow = 0;
|
||||||
|
overlapped.OffsetHigh = 0;
|
||||||
|
overlapped.EventHandle = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var success = NativeMethods.ReadFile(ReadHandle, nonManagedBuffer, (uint)buffer.Length, out bytesRead, ref overlapped);
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
status = DeviceData.ReadStatus.Success; // No check here to see if bytesRead > 0 . Perhaps not necessary?
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var result = NativeMethods.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);
|
||||||
|
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case NativeMethods.WAIT_OBJECT_0:
|
||||||
|
status = DeviceData.ReadStatus.Success;
|
||||||
|
NativeMethods.GetOverlappedResult(ReadHandle, ref overlapped, out bytesRead, false);
|
||||||
|
break;
|
||||||
|
case NativeMethods.WAIT_TIMEOUT:
|
||||||
|
status = DeviceData.ReadStatus.WaitTimedOut;
|
||||||
|
buffer = new byte[] { };
|
||||||
|
break;
|
||||||
|
case NativeMethods.WAIT_FAILED:
|
||||||
|
status = DeviceData.ReadStatus.WaitFail;
|
||||||
|
buffer = new byte[] { };
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
status = DeviceData.ReadStatus.NoDataRead;
|
||||||
|
buffer = new byte[] { };
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Marshal.Copy(nonManagedBuffer, buffer, 0, (int)bytesRead);
|
||||||
|
}
|
||||||
|
catch { status = DeviceData.ReadStatus.ReadError; }
|
||||||
|
finally {
|
||||||
|
CloseDeviceIO(overlapped.EventHandle);
|
||||||
|
Marshal.FreeHGlobal(nonManagedBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var overlapped = new NativeOverlapped();
|
||||||
|
|
||||||
|
NativeMethods.ReadFile(ReadHandle, nonManagedBuffer, (uint)buffer.Length, out bytesRead, ref overlapped);
|
||||||
|
status = DeviceData.ReadStatus.Success;
|
||||||
|
Marshal.Copy(nonManagedBuffer, buffer, 0, (int)bytesRead);
|
||||||
|
}
|
||||||
|
catch { status = DeviceData.ReadStatus.ReadError; }
|
||||||
|
finally { Marshal.FreeHGlobal(nonManagedBuffer); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new DeviceData(buffer, status);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IntPtr OpenDeviceIO(string devicePath, uint deviceAccess)
|
||||||
|
{
|
||||||
|
return OpenDeviceIO(devicePath, DeviceMode.NonOverlapped, deviceAccess, ShareMode.ShareRead | ShareMode.ShareWrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IntPtr OpenDeviceIO(string devicePath, DeviceMode deviceMode, uint deviceAccess, ShareMode shareMode)
|
||||||
|
{
|
||||||
|
var security = new NativeMethods.SECURITY_ATTRIBUTES();
|
||||||
|
var flags = 0;
|
||||||
|
|
||||||
|
if (deviceMode == DeviceMode.Overlapped)
|
||||||
|
{
|
||||||
|
flags = NativeMethods.FILE_FLAG_OVERLAPPED;
|
||||||
|
}
|
||||||
|
|
||||||
|
security.lpSecurityDescriptor = IntPtr.Zero;
|
||||||
|
security.bInheritHandle = true;
|
||||||
|
security.nLength = Marshal.SizeOf(security);
|
||||||
|
|
||||||
|
return NativeMethods.CreateFile(devicePath, deviceAccess, (int)shareMode, ref security, NativeMethods.OPEN_EXISTING, flags, hTemplateFile: IntPtr.Zero);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CloseDeviceIO(IntPtr handle)
|
||||||
|
{
|
||||||
|
if (Environment.OSVersion.Version.Major > 5)
|
||||||
|
{
|
||||||
|
NativeMethods.CancelIoEx(handle, IntPtr.Zero);
|
||||||
|
}
|
||||||
|
NativeMethods.CloseHandle(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DeviceEventMonitorInserted()
|
||||||
|
{
|
||||||
|
if (!IsOpen)
|
||||||
|
{
|
||||||
|
OpenDevice(_deviceReadMode, _deviceWriteMode, _deviceShareMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
Inserted?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DeviceEventMonitorRemoved()
|
||||||
|
{
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
CloseDevice();
|
||||||
|
}
|
||||||
|
|
||||||
|
Removed?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (MonitorDeviceEvents)
|
||||||
|
{
|
||||||
|
MonitorDeviceEvents = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsOpen)
|
||||||
|
{
|
||||||
|
CloseDevice();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class HidDeviceAttributes
|
||||||
|
{
|
||||||
|
internal HidDeviceAttributes(NativeMethods.HIDD_ATTRIBUTES attributes)
|
||||||
|
{
|
||||||
|
VendorId = attributes.VendorID;
|
||||||
|
ProductId = attributes.ProductID;
|
||||||
|
Version = attributes.VersionNumber;
|
||||||
|
|
||||||
|
VendorHexId = "0x" + attributes.VendorID.ToString("X4");
|
||||||
|
ProductHexId = "0x" + attributes.ProductID.ToString("X4");
|
||||||
|
}
|
||||||
|
|
||||||
|
public int VendorId { get; private set; }
|
||||||
|
public int ProductId { get; private set; }
|
||||||
|
public int Version { get; private set; }
|
||||||
|
public string VendorHexId { get; set; }
|
||||||
|
public string ProductHexId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class HidDeviceCapabilities
|
||||||
|
{
|
||||||
|
internal HidDeviceCapabilities(NativeMethods.HIDP_CAPS capabilities)
|
||||||
|
{
|
||||||
|
Usage = capabilities.Usage;
|
||||||
|
UsagePage = capabilities.UsagePage;
|
||||||
|
InputReportByteLength = capabilities.InputReportByteLength;
|
||||||
|
OutputReportByteLength = capabilities.OutputReportByteLength;
|
||||||
|
FeatureReportByteLength = capabilities.FeatureReportByteLength;
|
||||||
|
Reserved = capabilities.Reserved;
|
||||||
|
NumberLinkCollectionNodes = capabilities.NumberLinkCollectionNodes;
|
||||||
|
NumberInputButtonCaps = capabilities.NumberInputButtonCaps;
|
||||||
|
NumberInputValueCaps = capabilities.NumberInputValueCaps;
|
||||||
|
NumberInputDataIndices = capabilities.NumberInputDataIndices;
|
||||||
|
NumberOutputButtonCaps = capabilities.NumberOutputButtonCaps;
|
||||||
|
NumberOutputValueCaps = capabilities.NumberOutputValueCaps;
|
||||||
|
NumberOutputDataIndices = capabilities.NumberOutputDataIndices;
|
||||||
|
NumberFeatureButtonCaps = capabilities.NumberFeatureButtonCaps;
|
||||||
|
NumberFeatureValueCaps = capabilities.NumberFeatureValueCaps;
|
||||||
|
NumberFeatureDataIndices = capabilities.NumberFeatureDataIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public short Usage { get; private set; }
|
||||||
|
public short UsagePage { get; private set; }
|
||||||
|
public short InputReportByteLength { get; private set; }
|
||||||
|
public short OutputReportByteLength { get; private set; }
|
||||||
|
public short FeatureReportByteLength { get; private set; }
|
||||||
|
public short[] Reserved { get; private set; }
|
||||||
|
public short NumberLinkCollectionNodes { get; private set; }
|
||||||
|
public short NumberInputButtonCaps { get; private set; }
|
||||||
|
public short NumberInputValueCaps { get; private set; }
|
||||||
|
public short NumberInputDataIndices { get; private set; }
|
||||||
|
public short NumberOutputButtonCaps { get; private set; }
|
||||||
|
public short NumberOutputValueCaps { get; private set; }
|
||||||
|
public short NumberOutputDataIndices { get; private set; }
|
||||||
|
public short NumberFeatureButtonCaps { get; private set; }
|
||||||
|
public short NumberFeatureValueCaps { get; private set; }
|
||||||
|
public short NumberFeatureDataIndices { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class DeviceData
|
||||||
|
{
|
||||||
|
public enum ReadStatus
|
||||||
|
{
|
||||||
|
Success = 0,
|
||||||
|
WaitTimedOut = 1,
|
||||||
|
WaitFail = 2,
|
||||||
|
NoDataRead = 3,
|
||||||
|
ReadError = 4,
|
||||||
|
NotConnected = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceData(ReadStatus status)
|
||||||
|
{
|
||||||
|
Data = new byte[] {};
|
||||||
|
Status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceData(byte[] data, ReadStatus status)
|
||||||
|
{
|
||||||
|
Data = data;
|
||||||
|
Status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Data { get; private set; }
|
||||||
|
public ReadStatus Status { get; private set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
internal class DeviceEventMonitor
|
||||||
|
{
|
||||||
|
public event InsertedEventHandler Inserted;
|
||||||
|
public event RemovedEventHandler Removed;
|
||||||
|
|
||||||
|
public delegate void InsertedEventHandler();
|
||||||
|
public delegate void RemovedEventHandler();
|
||||||
|
|
||||||
|
private readonly Device _device;
|
||||||
|
private bool _wasConnected;
|
||||||
|
|
||||||
|
public DeviceEventMonitor(Device device)
|
||||||
|
{
|
||||||
|
_device = device;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init()
|
||||||
|
{
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
Task task = Task.Factory.StartNew(() => EventMonitor());
|
||||||
|
#else
|
||||||
|
var eventMonitor = new Action(EventMonitor);
|
||||||
|
eventMonitor.BeginInvoke(DisposeDeviceEventMonitor, eventMonitor);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EventMonitor()
|
||||||
|
{
|
||||||
|
var isConnected = _device.IsConnected;
|
||||||
|
|
||||||
|
if (isConnected != _wasConnected)
|
||||||
|
{
|
||||||
|
if (isConnected && Inserted != null)
|
||||||
|
{
|
||||||
|
Inserted();
|
||||||
|
}
|
||||||
|
else if (!isConnected && Removed != null)
|
||||||
|
{
|
||||||
|
Removed();
|
||||||
|
}
|
||||||
|
|
||||||
|
_wasConnected = isConnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.Sleep(500);
|
||||||
|
|
||||||
|
if (_device.MonitorDeviceEvents)
|
||||||
|
{
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DisposeDeviceEventMonitor(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
((Action)ar.AsyncState).EndInvoke(ar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,180 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class Devices
|
||||||
|
{
|
||||||
|
private static Guid _hidClassGuid = Guid.Empty;
|
||||||
|
|
||||||
|
public static bool IsConnected(string devicePath)
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Any(x => x.Path == devicePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Device GetDevice(string devicePath)
|
||||||
|
{
|
||||||
|
return Enumerate(devicePath).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Device> Enumerate()
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Select(x => new Device(x.Path, x.Description));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Device> Enumerate(string devicePath)
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Where(x => x.Path == devicePath).Select(x => new Device(x.Path, x.Description));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Device> Enumerate(int vendorId, params int[] productIds)
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Select(x => new Device(x.Path, x.Description)).Where(x => x.Attributes.VendorId == vendorId &&
|
||||||
|
productIds.Contains(x.Attributes.ProductId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Device> Enumerate(int vendorId, int productId, ushort UsagePage)
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Select(x => new Device(x.Path, x.Description)).Where(x => x.Attributes.VendorId == vendorId &&
|
||||||
|
productId == (ushort)x.Attributes.ProductId && (ushort)x.Capabilities.UsagePage == UsagePage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Device> Enumerate(int vendorId)
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Select(x => new Device(x.Path, x.Description)).Where(x => x.Attributes.VendorId == vendorId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Device> Enumerate(ushort UsagePage)
|
||||||
|
{
|
||||||
|
return EnumerateDevices().Select(x => new Device(x.Path, x.Description)).Where(x => (ushort)x.Capabilities.UsagePage == UsagePage);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class DeviceInfo { public string Path { get; set; } public string Description { get; set; } }
|
||||||
|
|
||||||
|
internal static IEnumerable<DeviceInfo> EnumerateDevices()
|
||||||
|
{
|
||||||
|
var devices = new List<DeviceInfo>();
|
||||||
|
var hidClass = HidClassGuid;
|
||||||
|
var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidClass, null, hwndParent: IntPtr.Zero, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);
|
||||||
|
|
||||||
|
if (deviceInfoSet.ToInt64() != NativeMethods.INVALID_HANDLE_VALUE)
|
||||||
|
{
|
||||||
|
var deviceInfoData = CreateDeviceInfoData();
|
||||||
|
var deviceIndex = 0;
|
||||||
|
|
||||||
|
while (NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, ref deviceInfoData))
|
||||||
|
{
|
||||||
|
deviceIndex += 1;
|
||||||
|
|
||||||
|
var deviceInterfaceData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
|
||||||
|
deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);
|
||||||
|
var deviceInterfaceIndex = 0;
|
||||||
|
|
||||||
|
while (NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, ref deviceInfoData, ref hidClass, deviceInterfaceIndex, ref deviceInterfaceData))
|
||||||
|
{
|
||||||
|
deviceInterfaceIndex++;
|
||||||
|
var devicePath = GetDevicePath(deviceInfoSet, deviceInterfaceData);
|
||||||
|
var description = GetBusReportedDeviceDescription(deviceInfoSet, ref deviceInfoData) ??
|
||||||
|
GetDeviceDescription(deviceInfoSet, ref deviceInfoData);
|
||||||
|
devices.Add(new DeviceInfo { Path = devicePath, Description = description });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
|
||||||
|
}
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static NativeMethods.SP_DEVINFO_DATA CreateDeviceInfoData()
|
||||||
|
{
|
||||||
|
var deviceInfoData = new NativeMethods.SP_DEVINFO_DATA();
|
||||||
|
|
||||||
|
deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);
|
||||||
|
deviceInfoData.DevInst = 0;
|
||||||
|
deviceInfoData.ClassGuid = Guid.Empty;
|
||||||
|
deviceInfoData.Reserved = IntPtr.Zero;
|
||||||
|
|
||||||
|
return deviceInfoData;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetDevicePath(IntPtr deviceInfoSet, NativeMethods.SP_DEVICE_INTERFACE_DATA deviceInterfaceData)
|
||||||
|
{
|
||||||
|
var bufferSize = 0;
|
||||||
|
var interfaceDetail = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA { Size = IntPtr.Size == 4 ? 4 + Marshal.SystemDefaultCharSize : 8 };
|
||||||
|
|
||||||
|
NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero);
|
||||||
|
|
||||||
|
return NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, ref interfaceDetail, bufferSize, ref bufferSize, IntPtr.Zero) ?
|
||||||
|
interfaceDetail.DevicePath : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Guid HidClassGuid
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_hidClassGuid.Equals(Guid.Empty))
|
||||||
|
{
|
||||||
|
NativeMethods.HidD_GetHidGuid(ref _hidClassGuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _hidClassGuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetDeviceDescription(IntPtr deviceInfoSet, ref NativeMethods.SP_DEVINFO_DATA devinfoData)
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
const int charCount = 1024;
|
||||||
|
var descriptionBuffer = stackalloc char[charCount];
|
||||||
|
|
||||||
|
var requiredSize = 0;
|
||||||
|
var type = 0;
|
||||||
|
|
||||||
|
if (NativeMethods.SetupDiGetDeviceRegistryProperty(deviceInfoSet,
|
||||||
|
ref devinfoData,
|
||||||
|
NativeMethods.SPDRP_DEVICEDESC,
|
||||||
|
ref type,
|
||||||
|
descriptionBuffer,
|
||||||
|
propertyBufferSize: charCount * sizeof(char),
|
||||||
|
ref requiredSize))
|
||||||
|
{
|
||||||
|
return new string(descriptionBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetBusReportedDeviceDescription(IntPtr deviceInfoSet, ref NativeMethods.SP_DEVINFO_DATA devinfoData)
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
const int charCount = 1024;
|
||||||
|
var descriptionBuffer = stackalloc char[charCount];
|
||||||
|
|
||||||
|
if (Environment.OSVersion.Version.Major > 5)
|
||||||
|
{
|
||||||
|
uint propertyType = 0;
|
||||||
|
var requiredSize = 0;
|
||||||
|
|
||||||
|
if (NativeMethods.SetupDiGetDeviceProperty(deviceInfoSet,
|
||||||
|
ref devinfoData,
|
||||||
|
ref NativeMethods.DEVPKEY_Device_BusReportedDeviceDesc,
|
||||||
|
ref propertyType,
|
||||||
|
descriptionBuffer,
|
||||||
|
propertyBufferSize: charCount * sizeof(char),
|
||||||
|
ref requiredSize,
|
||||||
|
0))
|
||||||
|
{
|
||||||
|
return new string(descriptionBuffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class Enumerator : IEnumerator
|
||||||
|
{
|
||||||
|
public bool IsConnected(string devicePath)
|
||||||
|
{
|
||||||
|
return Devices.IsConnected(devicePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDevice GetDevice(string devicePath)
|
||||||
|
{
|
||||||
|
return Devices.GetDevice(devicePath) as IDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate()
|
||||||
|
{
|
||||||
|
return Devices.Enumerate().
|
||||||
|
Select(d => d as IDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate(string devicePath)
|
||||||
|
{
|
||||||
|
return Devices.Enumerate(devicePath).
|
||||||
|
Select(d => d as IDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate(int vendorId, params int[] productIds)
|
||||||
|
{
|
||||||
|
return Devices.Enumerate(vendorId, productIds).
|
||||||
|
Select(d => d as IDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate(int vendorId)
|
||||||
|
{
|
||||||
|
return Devices.Enumerate(vendorId).
|
||||||
|
Select(d => d as IDevice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFrameworks>net20;net35;net40;net45;netstandard2</TargetFrameworks>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
|
<Company>EonaCat (Jeroen Saey)</Company>
|
||||||
|
<Copyright>Copyright 2024 EonaCat (Jeroen Saey)</Copyright>
|
||||||
|
|
||||||
|
<PackageId>EonaCat.HID</PackageId>
|
||||||
|
<Title>EonaCat.HID</Title>
|
||||||
|
<Authors>EonaCat (Jeroen Saey)</Authors>
|
||||||
|
<Description>.NET HID Devices</Description>
|
||||||
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
|
<PackageProjectUrl></PackageProjectUrl>
|
||||||
|
<PackageIcon>icon.png</PackageIcon>
|
||||||
|
<PackageProjectUrl>https://www.nuget.org/packages/EonaCat.HID/</PackageProjectUrl>
|
||||||
|
<PackageTags>usb hid; Jeroen;Saey</PackageTags>
|
||||||
|
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||||
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<EVRevisionFormat>1.0.0+{chash:10}.{c:ymd}</EVRevisionFormat>
|
||||||
|
<EVDefault>true</EVDefault>
|
||||||
|
<EVInfo>true</EVInfo>
|
||||||
|
<EVTagMatch>v[0-9]*</EVTagMatch>
|
||||||
|
<EVRemoveTagV>true</EVRemoveTagV>
|
||||||
|
<EVVcs>git</EVVcs>
|
||||||
|
<EVCheckAllAttributes>true</EVCheckAllAttributes>
|
||||||
|
<EVShowRevision>true</EVShowRevision>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
||||||
|
<ItemGroup Condition=" '$(TargetFramework)' != 'net45' AND '$(TargetFramework)' != 'netstandard2' ">
|
||||||
|
<PackageReference Include="Theraot.Core" Version="1.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="..\LICENSE">
|
||||||
|
<Pack>True</Pack>
|
||||||
|
<PackagePath>\</PackagePath>
|
||||||
|
</None>
|
||||||
|
<None Include="..\README.md">
|
||||||
|
<Pack>True</Pack>
|
||||||
|
<PackagePath>\</PackagePath>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="EonaCat.Versioning" Version="1.0.5">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="icon.png">
|
||||||
|
<Pack>True</Pack>
|
||||||
|
<PackagePath>\</PackagePath>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,94 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public delegate void InsertedEventHandler();
|
||||||
|
public delegate void RemovedEventHandler();
|
||||||
|
|
||||||
|
public enum DeviceMode
|
||||||
|
{
|
||||||
|
NonOverlapped = 0,
|
||||||
|
Overlapped = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
[Flags]
|
||||||
|
public enum ShareMode
|
||||||
|
{
|
||||||
|
Exclusive = 0,
|
||||||
|
ShareRead = NativeMethods.FILE_SHARE_READ,
|
||||||
|
ShareWrite = NativeMethods.FILE_SHARE_WRITE
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate void ReadCallback(DeviceData data);
|
||||||
|
public delegate void ReadReportCallback(HidReport report);
|
||||||
|
public delegate void WriteCallback(bool success);
|
||||||
|
|
||||||
|
public interface IDevice : IDisposable
|
||||||
|
{
|
||||||
|
event InsertedEventHandler Inserted;
|
||||||
|
event RemovedEventHandler Removed;
|
||||||
|
|
||||||
|
IntPtr ReadHandle { get; }
|
||||||
|
IntPtr WriteHandle { get; }
|
||||||
|
bool IsOpen { get; }
|
||||||
|
bool IsConnected { get; }
|
||||||
|
string Description { get; }
|
||||||
|
HidDeviceCapabilities Capabilities { get; }
|
||||||
|
HidDeviceAttributes Attributes { get; }
|
||||||
|
string DevicePath { get; }
|
||||||
|
|
||||||
|
bool MonitorDeviceEvents { get; set; }
|
||||||
|
|
||||||
|
void OpenDevice();
|
||||||
|
|
||||||
|
void OpenDevice(DeviceMode readMode, DeviceMode writeMode, ShareMode shareMode);
|
||||||
|
|
||||||
|
void CloseDevice();
|
||||||
|
|
||||||
|
DeviceData Read();
|
||||||
|
|
||||||
|
void Read(ReadCallback callback, int timeout = 0);
|
||||||
|
|
||||||
|
Task<DeviceData> ReadAsync(int timeout = 0);
|
||||||
|
|
||||||
|
DeviceData Read(int timeout);
|
||||||
|
|
||||||
|
void ReadReport(ReadReportCallback callback, int timeout = 0);
|
||||||
|
|
||||||
|
Task<HidReport> ReadReportAsync(int timeout = 0);
|
||||||
|
|
||||||
|
HidReport ReadReport(int timeout = 0);
|
||||||
|
bool ReadFeatureData(out byte[] data, byte reportId = 0);
|
||||||
|
|
||||||
|
bool ReadProduct(out byte[] data);
|
||||||
|
|
||||||
|
bool ReadManufacturer(out byte[] data);
|
||||||
|
|
||||||
|
bool ReadSerialNumber(out byte[] data);
|
||||||
|
|
||||||
|
void Write(byte[] data, WriteCallback callback);
|
||||||
|
|
||||||
|
bool Write(byte[] data);
|
||||||
|
|
||||||
|
bool Write(byte[] data, int timeout);
|
||||||
|
|
||||||
|
void Write(byte[] data, WriteCallback callback, int timeout);
|
||||||
|
|
||||||
|
Task<bool> WriteAsync(byte[] data, int timeout = 0);
|
||||||
|
|
||||||
|
void WriteReport(HidReport report, WriteCallback callback);
|
||||||
|
|
||||||
|
bool WriteReport(HidReport report);
|
||||||
|
|
||||||
|
bool WriteReport(HidReport report, int timeout);
|
||||||
|
|
||||||
|
void WriteReport(HidReport report, WriteCallback callback, int timeout);
|
||||||
|
|
||||||
|
Task<bool> WriteReportAsync(HidReport report, int timeout = 0);
|
||||||
|
|
||||||
|
HidReport CreateReport();
|
||||||
|
|
||||||
|
bool WriteFeatureData(byte[] data);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public interface IEnumerator
|
||||||
|
{
|
||||||
|
bool IsConnected(string devicePath);
|
||||||
|
IDevice GetDevice(string devicePath);
|
||||||
|
IEnumerable<IDevice> Enumerate();
|
||||||
|
IEnumerable<IDevice> Enumerate(string devicePath);
|
||||||
|
IEnumerable<IDevice> Enumerate(int vendorId, params int[] productIds);
|
||||||
|
IEnumerable<IDevice> Enumerate(int vendorId);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,220 @@
|
||||||
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
[module: DefaultCharSet(CharSet.Unicode)]
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
internal static class NativeMethods
|
||||||
|
{
|
||||||
|
internal const int FILE_FLAG_OVERLAPPED = 0x40000000;
|
||||||
|
internal const short FILE_SHARE_READ = 0x1;
|
||||||
|
internal const short FILE_SHARE_WRITE = 0x2;
|
||||||
|
internal const uint GENERIC_READ = 0x80000000;
|
||||||
|
internal const uint GENERIC_WRITE = 0x40000000;
|
||||||
|
internal const int ACCESS_NONE = 0;
|
||||||
|
internal const int INVALID_HANDLE_VALUE = -1;
|
||||||
|
internal const short OPEN_EXISTING = 3;
|
||||||
|
internal const int WAIT_TIMEOUT = 0x102;
|
||||||
|
internal const uint WAIT_OBJECT_0 = 0;
|
||||||
|
internal const uint WAIT_FAILED = 0xffffffff;
|
||||||
|
|
||||||
|
internal const int WAIT_INFINITE = -1;
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct SECURITY_ATTRIBUTES
|
||||||
|
{
|
||||||
|
public int nLength;
|
||||||
|
public IntPtr lpSecurityDescriptor;
|
||||||
|
public bool bInheritHandle;
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
|
||||||
|
static internal extern bool CancelIoEx(IntPtr hFile, IntPtr lpOverlapped);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
|
||||||
|
static internal extern bool CloseHandle(IntPtr hObject);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
static internal extern IntPtr CreateEvent(ref SECURITY_ATTRIBUTES securityAttributes, int bManualReset, int bInitialState, string lpName);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
|
static internal extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, ref SECURITY_ATTRIBUTES lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
|
static internal extern bool ReadFile(IntPtr hFile, IntPtr lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, [In] ref System.Threading.NativeOverlapped lpOverlapped);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
static internal extern uint WaitForSingleObject(IntPtr hHandle, int dwMilliseconds);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
|
static internal extern bool GetOverlappedResult(IntPtr hFile, [In] ref System.Threading.NativeOverlapped lpOverlapped, out uint lpNumberOfBytesTransferred, bool bWait);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll")]
|
||||||
|
static internal extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [In] ref System.Threading.NativeOverlapped lpOverlapped);
|
||||||
|
|
||||||
|
internal const short DIGCF_PRESENT = 0x2;
|
||||||
|
internal const short DIGCF_DEVICEINTERFACE = 0x10;
|
||||||
|
internal const int DIGCF_ALLCLASSES = 0x4;
|
||||||
|
|
||||||
|
internal const int SPDRP_ADDRESS = 0x1c;
|
||||||
|
internal const int SPDRP_BUSNUMBER = 0x15;
|
||||||
|
internal const int SPDRP_BUSTYPEGUID = 0x13;
|
||||||
|
internal const int SPDRP_CAPABILITIES = 0xf;
|
||||||
|
internal const int SPDRP_CHARACTERISTICS = 0x1b;
|
||||||
|
internal const int SPDRP_CLASS = 7;
|
||||||
|
internal const int SPDRP_CLASSGUID = 8;
|
||||||
|
internal const int SPDRP_COMPATIBLEIDS = 2;
|
||||||
|
internal const int SPDRP_CONFIGFLAGS = 0xa;
|
||||||
|
internal const int SPDRP_DEVICE_POWER_DATA = 0x1e;
|
||||||
|
internal const int SPDRP_DEVICEDESC = 0;
|
||||||
|
internal const int SPDRP_DEVTYPE = 0x19;
|
||||||
|
internal const int SPDRP_DRIVER = 9;
|
||||||
|
internal const int SPDRP_ENUMERATOR_NAME = 0x16;
|
||||||
|
internal const int SPDRP_EXCLUSIVE = 0x1a;
|
||||||
|
internal const int SPDRP_FRIENDLYNAME = 0xc;
|
||||||
|
internal const int SPDRP_HARDWAREID = 1;
|
||||||
|
internal const int SPDRP_LEGACYBUSTYPE = 0x14;
|
||||||
|
internal const int SPDRP_LOCATION_INFORMATION = 0xd;
|
||||||
|
internal const int SPDRP_LOWERFILTERS = 0x12;
|
||||||
|
internal const int SPDRP_MFG = 0xb;
|
||||||
|
internal const int SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0xe;
|
||||||
|
internal const int SPDRP_REMOVAL_POLICY = 0x1f;
|
||||||
|
internal const int SPDRP_REMOVAL_POLICY_HW_DEFAULT = 0x20;
|
||||||
|
internal const int SPDRP_REMOVAL_POLICY_OVERRIDE = 0x21;
|
||||||
|
internal const int SPDRP_SECURITY = 0x17;
|
||||||
|
internal const int SPDRP_SECURITY_SDS = 0x18;
|
||||||
|
internal const int SPDRP_SERVICE = 4;
|
||||||
|
internal const int SPDRP_UI_NUMBER = 0x10;
|
||||||
|
internal const int SPDRP_UI_NUMBER_DESC_FORMAT = 0x1d;
|
||||||
|
|
||||||
|
internal const int SPDRP_UPPERFILTERS = 0x11;
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct SP_DEVICE_INTERFACE_DATA
|
||||||
|
{
|
||||||
|
internal int cbSize;
|
||||||
|
internal System.Guid InterfaceClassGuid;
|
||||||
|
internal int Flags;
|
||||||
|
internal IntPtr Reserved;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct SP_DEVINFO_DATA
|
||||||
|
{
|
||||||
|
internal int cbSize;
|
||||||
|
internal Guid ClassGuid;
|
||||||
|
internal int DevInst;
|
||||||
|
internal IntPtr Reserved;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
|
||||||
|
{
|
||||||
|
internal int Size;
|
||||||
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||||
|
internal string DevicePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct DEVPROPKEY
|
||||||
|
{
|
||||||
|
public Guid fmtid;
|
||||||
|
public uint pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static DEVPROPKEY DEVPKEY_Device_BusReportedDeviceDesc =
|
||||||
|
new DEVPROPKEY { fmtid = new Guid(0x540b947e, 0x8b40, 0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2), pid = 4 };
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
public static extern unsafe bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, int propertyVal, ref int propertyRegDataType, void* propertyBuffer, int propertyBufferSize, ref int requiredSize);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll", SetLastError = true)]
|
||||||
|
public static extern unsafe bool SetupDiGetDeviceProperty(IntPtr deviceInfo, ref SP_DEVINFO_DATA deviceInfoData, ref DEVPROPKEY propkey, ref uint propertyDataType, void* propertyBuffer, int propertyBufferSize, ref int requiredSize, uint flags);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
static internal extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SP_DEVINFO_DATA deviceInfoData);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
static internal extern int SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
static internal extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, ref Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
static internal extern IntPtr SetupDiGetClassDevs(ref System.Guid classGuid, string enumerator, IntPtr hwndParent, int flags);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
static internal extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData);
|
||||||
|
|
||||||
|
[DllImport("setupapi.dll")]
|
||||||
|
static internal extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData);
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct HIDD_ATTRIBUTES
|
||||||
|
{
|
||||||
|
internal int Size;
|
||||||
|
internal ushort VendorID;
|
||||||
|
internal ushort ProductID;
|
||||||
|
internal short VersionNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct HIDP_CAPS
|
||||||
|
{
|
||||||
|
internal short Usage;
|
||||||
|
internal short UsagePage;
|
||||||
|
internal short InputReportByteLength;
|
||||||
|
internal short OutputReportByteLength;
|
||||||
|
internal short FeatureReportByteLength;
|
||||||
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 17)]
|
||||||
|
internal short[] Reserved;
|
||||||
|
internal short NumberLinkCollectionNodes;
|
||||||
|
internal short NumberInputButtonCaps;
|
||||||
|
internal short NumberInputValueCaps;
|
||||||
|
internal short NumberInputDataIndices;
|
||||||
|
internal short NumberOutputButtonCaps;
|
||||||
|
internal short NumberOutputValueCaps;
|
||||||
|
internal short NumberOutputDataIndices;
|
||||||
|
internal short NumberFeatureButtonCaps;
|
||||||
|
internal short NumberFeatureValueCaps;
|
||||||
|
internal short NumberFeatureDataIndices;
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_GetAttributes(IntPtr hidDeviceObject, ref HIDD_ATTRIBUTES attributes);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_GetFeature(IntPtr hidDeviceObject, byte[] lpReportBuffer, int reportBufferLength);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_GetInputReport(IntPtr hidDeviceObject, byte[] lpReportBuffer, int reportBufferLength);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern void HidD_GetHidGuid(ref Guid hidGuid);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_GetPreparsedData(IntPtr hidDeviceObject, ref IntPtr preparsedData);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_FreePreparsedData(IntPtr preparsedData);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_SetFeature(IntPtr hidDeviceObject, byte[] lpReportBuffer, int reportBufferLength);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern bool HidD_SetOutputReport(IntPtr hidDeviceObject, byte[] lpReportBuffer, int reportBufferLength);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
static internal extern int HidP_GetCaps(IntPtr preparsedData, ref HIDP_CAPS capabilities);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
internal static extern bool HidD_GetProductString(IntPtr hidDeviceObject, byte[] lpReportBuffer, int ReportBufferLength);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
internal static extern bool HidD_GetManufacturerString(IntPtr hidDeviceObject, byte[] lpReportBuffer, int ReportBufferLength);
|
||||||
|
|
||||||
|
[DllImport("hid.dll")]
|
||||||
|
internal static extern bool HidD_GetSerialNumberString(IntPtr hidDeviceObject, byte[] lpReportBuffer, int reportBufferLength);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class ReadDevice : Device
|
||||||
|
{
|
||||||
|
internal ReadDevice(string devicePath, string description = null)
|
||||||
|
: base(devicePath, description) { }
|
||||||
|
|
||||||
|
public DeviceData FastRead()
|
||||||
|
{
|
||||||
|
return FastRead(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceData FastRead(int timeout)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return ReadData(timeout);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return new DeviceData(DeviceData.ReadStatus.ReadError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void FastRead(ReadCallback callback)
|
||||||
|
{
|
||||||
|
FastRead(callback, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void FastRead(ReadCallback callback, int timeout)
|
||||||
|
{
|
||||||
|
var readDelegate = new ReadDelegate(FastRead);
|
||||||
|
var asyncState = new StateAsync(readDelegate, callback);
|
||||||
|
readDelegate.BeginInvoke(timeout, EndRead, asyncState);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<DeviceData> FastReadAsync(int timeout = 0)
|
||||||
|
{
|
||||||
|
var readDelegate = new ReadDelegate(FastRead);
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
return await Task<DeviceData>.Factory.StartNew(() => readDelegate.Invoke(timeout));
|
||||||
|
#else
|
||||||
|
return await Task<DeviceData>.Factory.FromAsync(readDelegate.BeginInvoke, readDelegate.EndInvoke, timeout, null);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
public HidReport FastReadReport()
|
||||||
|
{
|
||||||
|
return FastReadReport(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HidReport FastReadReport(int timeout)
|
||||||
|
{
|
||||||
|
return new HidReport(Capabilities.InputReportByteLength, FastRead(timeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void FastReadReport(ReadReportCallback callback)
|
||||||
|
{
|
||||||
|
FastReadReport(callback, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void FastReadReport(ReadReportCallback callback, int timeout)
|
||||||
|
{
|
||||||
|
var readReportDelegate = new ReadReportDelegate(FastReadReport);
|
||||||
|
var asyncState = new StateAsync(readReportDelegate, callback);
|
||||||
|
readReportDelegate.BeginInvoke(timeout, EndReadReport, asyncState);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<HidReport> FastReadReportAsync(int timeout = 0)
|
||||||
|
{
|
||||||
|
var readReportDelegate = new ReadReportDelegate(FastReadReport);
|
||||||
|
#if NET20 || NET35 || NET5_0_OR_GREATER
|
||||||
|
return await Task<HidReport>.Factory.StartNew(() => readReportDelegate.Invoke(timeout));
|
||||||
|
#else
|
||||||
|
return await Task<HidReport>.Factory.FromAsync(readReportDelegate.BeginInvoke, readReportDelegate.EndInvoke, timeout, null);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class ReadEnumerator : IEnumerator
|
||||||
|
{
|
||||||
|
public bool IsConnected(string devicePath)
|
||||||
|
{
|
||||||
|
return Devices.IsConnected(devicePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDevice GetDevice(string devicePath)
|
||||||
|
{
|
||||||
|
return Enumerate(devicePath).FirstOrDefault() as IDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate()
|
||||||
|
{
|
||||||
|
return Devices.EnumerateDevices().
|
||||||
|
Select(d => new ReadDevice(d.Path, d.Description) as IDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate(string devicePath)
|
||||||
|
{
|
||||||
|
return Devices.EnumerateDevices().Where(x => x.Path == devicePath).
|
||||||
|
Select(d => new ReadDevice(d.Path, d.Description) as IDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate(int vendorId, params int[] productIds)
|
||||||
|
{
|
||||||
|
return Devices.EnumerateDevices().Select(d => new ReadDevice(d.Path, d.Description)).
|
||||||
|
Where(f => f.Attributes.VendorId == vendorId && productIds.Contains(f.Attributes.ProductId)).
|
||||||
|
Select(d => d as IDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<IDevice> Enumerate(int vendorId)
|
||||||
|
{
|
||||||
|
return Devices.EnumerateDevices().Select(d => new ReadDevice(d.Path, d.Description)).
|
||||||
|
Where(f => f.Attributes.VendorId == vendorId).
|
||||||
|
Select(d => d as IDevice);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,85 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class HidReport
|
||||||
|
{
|
||||||
|
private byte _reportId;
|
||||||
|
private byte[] _data = new byte[] {};
|
||||||
|
|
||||||
|
private readonly DeviceData.ReadStatus _status;
|
||||||
|
|
||||||
|
public HidReport(int reportSize)
|
||||||
|
{
|
||||||
|
Array.Resize(ref _data, reportSize - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HidReport(int reportSize, DeviceData deviceData)
|
||||||
|
{
|
||||||
|
_status = deviceData.Status;
|
||||||
|
|
||||||
|
Array.Resize(ref _data, reportSize - 1);
|
||||||
|
|
||||||
|
if ((deviceData.Data != null))
|
||||||
|
{
|
||||||
|
|
||||||
|
if (deviceData.Data.Length > 0)
|
||||||
|
{
|
||||||
|
_reportId = deviceData.Data[0];
|
||||||
|
Exists = true;
|
||||||
|
|
||||||
|
if (deviceData.Data.Length > 1)
|
||||||
|
{
|
||||||
|
var dataLength = reportSize - 1;
|
||||||
|
if (deviceData.Data.Length < reportSize - 1)
|
||||||
|
{
|
||||||
|
dataLength = deviceData.Data.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Array.Copy(deviceData.Data, 1, _data, 0, dataLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Exists = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Exists = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Exists { get; private set; }
|
||||||
|
public DeviceData.ReadStatus ReadStatus { get { return _status; } }
|
||||||
|
|
||||||
|
public byte ReportId
|
||||||
|
{
|
||||||
|
get { return _reportId; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_reportId = value;
|
||||||
|
Exists = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Data
|
||||||
|
{
|
||||||
|
get { return _data; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_data = value;
|
||||||
|
Exists = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] GetBytes()
|
||||||
|
{
|
||||||
|
byte[] data = null;
|
||||||
|
Array.Resize(ref data, _data.Length + 1);
|
||||||
|
data[0] = _reportId;
|
||||||
|
Array.Copy(_data, 0, data, 1, _data.Length);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
namespace EonaCat.HID
|
||||||
|
{
|
||||||
|
public class StateAsync
|
||||||
|
{
|
||||||
|
private readonly object _callerDelegate;
|
||||||
|
private readonly object _callbackDelegate;
|
||||||
|
|
||||||
|
public StateAsync(object callerDelegate, object callbackDelegate)
|
||||||
|
{
|
||||||
|
_callerDelegate = callerDelegate;
|
||||||
|
_callbackDelegate = callbackDelegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object CallerDelegate { get { return _callerDelegate; } }
|
||||||
|
public object CallbackDelegate { get { return _callbackDelegate; } }
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 248 KiB |
Binary file not shown.
After Width: | Height: | Size: 88 KiB |
213
LICENSE
213
LICENSE
|
@ -1,73 +1,204 @@
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
https://EonaCat.com/license/
|
||||||
|
|
||||||
1. Definitions.
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
OF SOFTWARE BY EONACAT (JEROEN SAEY)
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
1. Definitions.
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
Copyright 2024 EonaCat
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Copyright [yyyy] [name of copyright owner]
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
Unless required by applicable law or agreed to in writing, software
|
||||||
See the License for the specific language governing permissions and
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
limitations under the License.
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
Loading…
Reference in New Issue