Skip to content

Commit 542aacd

Browse files
committed
Merge remote-tracking branch 'hidSharp/master'
# Conflicts: # .gitignore # README.md
2 parents c990cc4 + a0a1f6c commit 542aacd

117 files changed

Lines changed: 17869 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

HidSharp/AsyncResult.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#region License
2+
/* Copyright 2012 James F. Bellinger <http://www.zer7.com/software/hidsharp>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License. */
16+
#endregion
17+
18+
using System;
19+
using System.Threading;
20+
21+
namespace HidSharp
22+
{
23+
sealed class AsyncResult<T> : IAsyncResult
24+
{
25+
volatile bool _isCompleted;
26+
ManualResetEvent _waitHandle;
27+
28+
AsyncResult(AsyncCallback callback, object state)
29+
{
30+
AsyncCallback = callback; AsyncState = state;
31+
}
32+
33+
void Complete()
34+
{
35+
lock (this)
36+
{
37+
if (_isCompleted) { return; } _isCompleted = true;
38+
if (_waitHandle != null) { _waitHandle.Set(); }
39+
}
40+
41+
if (AsyncCallback != null) { AsyncCallback(this); }
42+
}
43+
44+
internal delegate T OperationCallback();
45+
46+
internal static IAsyncResult BeginOperation(OperationCallback operation,
47+
AsyncCallback callback, object state)
48+
{
49+
var ar = new AsyncResult<T>(callback, state);
50+
ThreadPool.QueueUserWorkItem(delegate(object self)
51+
{
52+
try { ar.Result = operation(); }
53+
catch (Exception e) { ar.Exception = e; }
54+
ar.Complete();
55+
}, ar);
56+
return ar;
57+
}
58+
59+
internal T EndOperation()
60+
{
61+
while (true)
62+
{
63+
if (IsCompleted)
64+
{
65+
if (Exception != null) { throw Exception; }
66+
return Result;
67+
}
68+
AsyncWaitHandle.WaitOne();
69+
}
70+
}
71+
72+
internal static T EndOperation(IAsyncResult asyncResult)
73+
{
74+
Throw.If.Null(asyncResult);
75+
return ((AsyncResult<T>)asyncResult).EndOperation();
76+
}
77+
78+
public AsyncCallback AsyncCallback
79+
{
80+
get;
81+
private set;
82+
}
83+
84+
public object AsyncState
85+
{
86+
get;
87+
private set;
88+
}
89+
90+
public WaitHandle AsyncWaitHandle
91+
{
92+
get
93+
{
94+
lock (this)
95+
{
96+
if (_waitHandle == null)
97+
{
98+
_waitHandle = new ManualResetEvent(_isCompleted);
99+
}
100+
}
101+
102+
return _waitHandle;
103+
}
104+
}
105+
106+
public bool CompletedSynchronously
107+
{
108+
get { return false; }
109+
}
110+
111+
public bool IsCompleted
112+
{
113+
get { return _isCompleted; }
114+
}
115+
116+
Exception Exception
117+
{
118+
get;
119+
set;
120+
}
121+
122+
T Result
123+
{
124+
get;
125+
set;
126+
}
127+
}
128+
}

HidSharp/CommonException.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#region License
2+
/* Copyright 2019 James F. Bellinger <http://www.zer7.com/software/hidsharp>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License. */
16+
#endregion
17+
18+
using System;
19+
20+
namespace HidSharp
21+
{
22+
static class CommonException
23+
{
24+
public static ObjectDisposedException CreateClosedException()
25+
{
26+
return new ObjectDisposedException("Closed.", (Exception)null);
27+
}
28+
}
29+
}

HidSharp/Device.cs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#region License
2+
/* Copyright 2017 James F. Bellinger <http://www.zer7.com/software/hidsharp>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License. */
16+
#endregion
17+
18+
using System;
19+
using System.Diagnostics;
20+
using HidSharp.Utility;
21+
22+
namespace HidSharp
23+
{
24+
public abstract class Device
25+
{
26+
/// <summary>
27+
/// Makes a connection to the device, or throws an exception if the connection cannot be made.
28+
/// </summary>
29+
/// <returns>The stream to use to communicate with the device.</returns>
30+
public DeviceStream Open()
31+
{
32+
return Open(null);
33+
}
34+
35+
public DeviceStream Open(OpenConfiguration openConfig)
36+
{
37+
return OpenDeviceAndRestrictAccess(openConfig ?? new OpenConfiguration());
38+
}
39+
40+
protected virtual DeviceStream OpenDeviceAndRestrictAccess(OpenConfiguration openConfig)
41+
{
42+
bool exclusive = (bool)openConfig.GetOption(OpenOption.Exclusive);
43+
44+
DeviceOpenUtility openUtility = null;
45+
if (exclusive)
46+
{
47+
string streamPath = GetStreamPath(openConfig);
48+
openUtility = new DeviceOpenUtility(this, streamPath, openConfig);
49+
openUtility.Open();
50+
}
51+
52+
DeviceStream stream;
53+
try
54+
{
55+
stream = OpenDeviceDirectly(openConfig);
56+
if (exclusive)
57+
{
58+
stream.Closed += (sender, e) => openUtility.Close();
59+
openUtility.InterruptRequested += (sender, e) =>
60+
{
61+
stream.OnInterruptRequested();
62+
HidSharpDiagnostics.Trace("Delivered an interrupt request.");
63+
};
64+
}
65+
}
66+
catch
67+
{
68+
if (exclusive) { openUtility.Close(); }
69+
throw;
70+
}
71+
72+
return stream;
73+
}
74+
75+
protected abstract DeviceStream OpenDeviceDirectly(OpenConfiguration openConfig);
76+
77+
// Used for exclusion... and also may be used inside OpenDeviceDirectly if desired.
78+
protected virtual string GetStreamPath(OpenConfiguration openConfig)
79+
{
80+
return DevicePath;
81+
}
82+
83+
/// <summary>
84+
/// Tries to make a connection to the device.
85+
/// </summary>
86+
/// <param name="stream">The stream to use to communicate with the device.</param>
87+
/// <returns><c>true</c> if the connection was successful.</returns>
88+
public bool TryOpen(out DeviceStream stream)
89+
{
90+
return TryOpen(null, out stream);
91+
}
92+
93+
public bool TryOpen(OpenConfiguration openConfig, out DeviceStream stream)
94+
{
95+
Exception exception;
96+
return TryOpen(openConfig, out stream, out exception);
97+
}
98+
99+
public bool TryOpen(OpenConfiguration openConfig, out DeviceStream stream, out Exception exception)
100+
{
101+
try
102+
{
103+
stream = Open(openConfig); exception = null; return true;
104+
}
105+
catch (Exception e)
106+
{
107+
Debug.WriteLine(e);
108+
stream = null; exception = e; return false;
109+
}
110+
}
111+
112+
/// <summary>
113+
/// Returns the file system path of the device.
114+
/// This can be used to check permissions on Linux hidraw, for instance.
115+
/// </summary>
116+
/// <returns>The file system path.</returns>
117+
public abstract string GetFileSystemName();
118+
119+
/// <summary>
120+
/// Returns a name appropriate for display.
121+
/// </summary>
122+
/// <returns>The friendly name.</returns>
123+
public abstract string GetFriendlyName();
124+
125+
/// <summary>
126+
/// Checks if a particular implementation detail, such as the use of the Linux hidraw API, applies to this device.
127+
/// See <see cref="ImplementationDetail"/> for a list of possible details.
128+
/// </summary>
129+
/// <param name="detail">The detail to check.</param>
130+
/// <returns><c>true</c> if the implementation detail applies.</returns>
131+
public virtual bool HasImplementationDetail(Guid detail)
132+
{
133+
return false;
134+
}
135+
136+
/// <summary>
137+
/// The operating system's name for the device.
138+
///
139+
/// If you have multiple devices with the same Vendor ID, Product ID, Serial Number, etc.,
140+
/// this may be useful for differentiating them.
141+
/// </summary>
142+
public abstract string DevicePath
143+
{
144+
get;
145+
}
146+
}
147+
}

HidSharp/DeviceException.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#region License
2+
/* Copyright 2017 James F. Bellinger <http://www.zer7.com/software/hidsharp>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License. */
16+
#endregion
17+
18+
using System;
19+
using System.IO;
20+
21+
namespace HidSharp
22+
{
23+
public static class DeviceException
24+
{
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="IOException"/> class, and associates a <see cref="Device"/> with it.
27+
/// </summary>
28+
/// <param name="device">The device that caused the exception.</param>
29+
/// <param name="message">A description of the error.</param>
30+
/// <returns>The new <see cref="IOException"/>.</returns>
31+
public static IOException CreateIOException(Device device, string message)
32+
{
33+
return new Exceptions.DeviceIOException(device, message);
34+
}
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="IOException"/> class, and associates a <see cref="Device"/> with it.
38+
/// </summary>
39+
/// <param name="device">The device that caused the exception.</param>
40+
/// <param name="message">A description of the error.</param>
41+
/// <param name="hresult">An integer identifying the error that has occurred.</param>
42+
/// <returns>The new <see cref="IOException"/>.</returns>
43+
public static IOException CreateIOException(Device device, string message, int hresult)
44+
{
45+
return new Exceptions.DeviceIOException(device, message, hresult);
46+
}
47+
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="UnauthorizedAccessException"/> class, and associates a <see cref="Device"/> with it.
50+
/// </summary>
51+
/// <param name="device">The device that caused the exception.</param>
52+
/// <param name="message">A description of the error.</param>
53+
/// <returns>The new <see cref="UnauthorizedAccessException"/>.</returns>
54+
public static UnauthorizedAccessException CreateUnauthorizedAccessException(Device device, string message)
55+
{
56+
return new Exceptions.DeviceUnauthorizedAccessException(device, message);
57+
}
58+
59+
/// <summary>
60+
/// Gets the <see cref="Device"/> associated with the exception, if any.
61+
/// </summary>
62+
/// <param name="exception">The exception to get the associated <see cref="Device"/> for.</param>
63+
/// <returns>The associated <see cref="Device"/>, or null if none is associated with it.</returns>
64+
public static Device GetDevice(Exception exception)
65+
{
66+
var hidException = exception as Exceptions.IDeviceException;
67+
return hidException != null ? hidException.Device : null;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)