├── .github
└── workflows
│ └── dotnet.yml
├── .gitignore
├── HidSharp.NETStandard.sln
├── HidSharp.Test
├── HidSharp.Test.NETStandard.csproj
├── HidSharp.Test.csproj
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── app.config
├── HidSharp.sln
├── HidSharp
├── AsyncResult.cs
├── CommonException.cs
├── Device.cs
├── DeviceException.cs
├── DeviceFilter.cs
├── DeviceList.cs
├── DeviceListChangedEventArgs.cs
├── DeviceOpenUtility.cs
├── DeviceStream.cs
├── DeviceTypes.cs
├── Exceptions
│ ├── DeviceIOException.cs
│ ├── DeviceUnauthorizedAccessException.cs
│ └── IDeviceException.cs
├── Experimental
│ ├── BleCccd.cs
│ ├── BleCharacteristic.cs
│ ├── BleCharacteristicProperties.cs
│ ├── BleDescriptor.cs
│ ├── BleDevice.cs
│ ├── BleDiscovery.cs
│ ├── BleEvent.cs
│ ├── BleRequestFlags.cs
│ ├── BleService.cs
│ ├── BleStream.cs
│ ├── BleUuid.cs
│ └── BleUuids.cs
├── FilteredDeviceList.cs
├── HidDevice.cs
├── HidDeviceLoader.cs
├── HidSharp.NETStandard.csproj
├── HidSharp.csproj
├── HidStream.cs
├── ImplementationDetail.cs
├── LocalDeviceList.cs
├── OpenConfiguration.cs
├── OpenOption.cs
├── OpenPriority.cs
├── Platform
│ ├── HidManager.cs
│ ├── HidSelector.cs
│ ├── Libusb
│ │ ├── LibusbHidManager.cs
│ │ └── NativeMethods.cs
│ ├── Linux
│ │ ├── LinuxHidDevice.cs
│ │ ├── LinuxHidManager.cs
│ │ ├── LinuxHidStream.cs
│ │ ├── LinuxSerialDevice.cs
│ │ ├── LinuxSerialStream.cs
│ │ ├── NativeMethods.cs
│ │ ├── NativeMethodsLibudev.cs
│ │ ├── NativeMethodsLibudev0.cs
│ │ └── NativeMethodsLibudev1.cs
│ ├── MacOS
│ │ ├── MacHidDevice.cs
│ │ ├── MacHidManager.cs
│ │ ├── MacHidStream.cs
│ │ ├── MacSerialDevice.cs
│ │ ├── MacSerialStream.cs
│ │ └── NativeMethods.cs
│ ├── SysBleStream.cs
│ ├── SysHidStream.cs
│ ├── SysRefCountHelper.cs
│ ├── SysSerialStream.cs
│ ├── SystemEvents.cs
│ ├── Unsupported
│ │ └── UnsupportedHidManager.cs
│ ├── Utf8Marshaler.cs
│ └── Windows
│ │ ├── NativeMethods.cs
│ │ ├── WinBleCharacteristic.cs
│ │ ├── WinBleDescriptor.cs
│ │ ├── WinBleDevice.cs
│ │ ├── WinBleService.cs
│ │ ├── WinBleStream.cs
│ │ ├── WinHidDevice.ReportDescriptorBuilder.cs
│ │ ├── WinHidDevice.ReportDescriptorReconstructor.cs
│ │ ├── WinHidDevice.cs
│ │ ├── WinHidManager.cs
│ │ ├── WinHidStream.cs
│ │ ├── WinSerialDevice.cs
│ │ └── WinSerialStream.cs
├── Properties
│ └── AssemblyInfo.cs
├── Reports
│ ├── DataConvert.cs
│ ├── DataItem.cs
│ ├── DataItemFlags.cs
│ ├── DataValue.cs
│ ├── DescriptorCollectionItem.cs
│ ├── DescriptorItem.cs
│ ├── DeviceItem.cs
│ ├── Encodings
│ │ ├── CollectionType.cs
│ │ ├── EncodedItem.cs
│ │ ├── GlobalItemTag.cs
│ │ ├── ItemType.cs
│ │ ├── LocalItemTag.cs
│ │ └── MainItemTag.cs
│ ├── ExpectedUsageType.cs
│ ├── IndexList.cs
│ ├── IndexRange.cs
│ ├── Indexes.cs
│ ├── Input
│ │ ├── DeviceItemInputParser.cs
│ │ └── HidDeviceInputReceiver.cs
│ ├── Report.cs
│ ├── ReportDescriptor.cs
│ ├── ReportDescriptorParseState.cs
│ ├── ReportType.cs
│ ├── Units
│ │ ├── CurrentUnit.cs
│ │ ├── LengthUnit.cs
│ │ ├── LuminousIntensityUnit.cs
│ │ ├── MassUnit.cs
│ │ ├── TemperatureUnit.cs
│ │ ├── TimeUnit.cs
│ │ ├── Unit.cs
│ │ ├── UnitKind.cs
│ │ └── UnitSystem.cs
│ └── Usage.cs
├── SerialDevice.cs
├── SerialParity.cs
├── SerialSettings.cs
├── SerialStream.cs
├── Throw.cs
└── Utility
│ ├── BcdHelper.cs
│ ├── HResult.cs
│ └── HidSharpDiagnostics.cs
├── License.txt
├── NOTICE.txt
└── README.md
/.github/workflows/dotnet.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a .NET project
2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
3 |
4 | name: .NET
5 |
6 | on:
7 | push:
8 | branches: [ "master" ]
9 | pull_request:
10 | branches: [ "master" ]
11 |
12 | jobs:
13 | build:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | steps:
18 | - uses: actions/checkout@v4
19 | - name: Setup .NET
20 | uses: actions/setup-dotnet@v4
21 | with:
22 | dotnet-version: 8.0.x
23 | - name: Restore dependencies
24 | run: dotnet restore HidSharp/HidSharp.csproj
25 | - name: Build
26 | run: dotnet build HidSharp/HidSharp.csproj --no-restore
27 |
--------------------------------------------------------------------------------
/HidSharp.NETStandard.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27428.2037
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidSharp", "HidSharp\HidSharp.NETStandard.csproj", "{0DB86674-2A7B-4BDC-93C1-3F7DC771426C}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidSharp.Test", "HidSharp.Test\HidSharp.Test.NETStandard.csproj", "{5E83ACC1-19FA-4996-BBAE-BF7DB4604812}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/HidSharp.Test/HidSharp.Test.NETStandard.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 | true
7 | false
8 | HidSharp.Test
9 | HidSharp.Test
10 | ..\bin
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/HidSharp.Test/HidSharp.Test.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net8.0
4 | Exe
5 | false
6 | ..\bin\
7 | Illusory Studios LLC
8 | Copyright © 2010-2013 James F. Bellinger <http://www.zer7.com/software/hidsharp>
9 | HID test program
10 | HidSharp
11 | HidSharp.Test
12 | 1.3.0.0
13 | 1.3.0.0
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/HidSharp.Test/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | [assembly: ComVisible(false)]
6 | [assembly: Guid("2F0DC374-5974-49E6-A7C6-5FFDEDE5E21D")]
7 |
--------------------------------------------------------------------------------
/HidSharp.Test/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/HidSharp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidSharp", "HidSharp\HidSharp.csproj", "{0DB86674-2A7B-4BDC-93C1-3F7DC771426C}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HidSharp.Test", "HidSharp.Test\HidSharp.Test.csproj", "{5E83ACC1-19FA-4996-BBAE-BF7DB4604812}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Debug|Mixed Platforms = Debug|Mixed Platforms
12 | Debug|x86 = Debug|x86
13 | Release|Any CPU = Release|Any CPU
14 | Release|Mixed Platforms = Release|Mixed Platforms
15 | Release|x86 = Release|x86
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
21 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
22 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Debug|x86.ActiveCfg = Debug|Any CPU
23 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
26 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
27 | {0DB86674-2A7B-4BDC-93C1-3F7DC771426C}.Release|x86.ActiveCfg = Release|Any CPU
28 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|Any CPU.Build.0 = Debug|Any CPU
30 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
31 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
32 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Debug|x86.ActiveCfg = Debug|Any CPU
33 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|Any CPU.ActiveCfg = Release|Any CPU
34 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|Any CPU.Build.0 = Release|Any CPU
35 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
36 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|Mixed Platforms.Build.0 = Release|Any CPU
37 | {5E83ACC1-19FA-4996-BBAE-BF7DB4604812}.Release|x86.ActiveCfg = Release|Any CPU
38 | EndGlobalSection
39 | GlobalSection(SolutionProperties) = preSolution
40 | HideSolutionNode = FALSE
41 | EndGlobalSection
42 | GlobalSection(MonoDevelopProperties) = preSolution
43 | StartupItem = HidSharp.Test\HidSharp.Test.csproj
44 | EndGlobalSection
45 | EndGlobal
46 |
--------------------------------------------------------------------------------
/HidSharp/AsyncResult.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2012 James F. Bellinger
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 : 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(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)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:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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 | }
30 |
--------------------------------------------------------------------------------
/HidSharp/Device.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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 | ///
27 | /// Makes a connection to the device, or throws an exception if the connection cannot be made.
28 | ///
29 | /// The stream to use to communicate with the device.
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 | ///
84 | /// Tries to make a connection to the device.
85 | ///
86 | /// The stream to use to communicate with the device.
87 | /// true if the connection was successful.
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 | ///
113 | /// Returns the file system path of the device.
114 | /// This can be used to check permissions on Linux hidraw, for instance.
115 | ///
116 | /// The file system path.
117 | public abstract string GetFileSystemName();
118 |
119 | ///
120 | /// Returns a name appropriate for display.
121 | ///
122 | /// The friendly name.
123 | public abstract string GetFriendlyName();
124 |
125 | ///
126 | /// Checks if a particular implementation detail, such as the use of the Linux hidraw API, applies to this device.
127 | /// See for a list of possible details.
128 | ///
129 | /// The detail to check.
130 | /// true if the implementation detail applies.
131 | public virtual bool HasImplementationDetail(Guid detail)
132 | {
133 | return false;
134 | }
135 |
136 | ///
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 | ///
142 | public abstract string DevicePath
143 | {
144 | get;
145 | }
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/HidSharp/DeviceException.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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 | ///
26 | /// Initializes a new instance of the class, and associates a with it.
27 | ///
28 | /// The device that caused the exception.
29 | /// A description of the error.
30 | /// The new .
31 | public static IOException CreateIOException(Device device, string message)
32 | {
33 | return new Exceptions.DeviceIOException(device, message);
34 | }
35 |
36 | ///
37 | /// Initializes a new instance of the class, and associates a with it.
38 | ///
39 | /// The device that caused the exception.
40 | /// A description of the error.
41 | /// An integer identifying the error that has occurred.
42 | /// The new .
43 | public static IOException CreateIOException(Device device, string message, int hresult)
44 | {
45 | return new Exceptions.DeviceIOException(device, message, hresult);
46 | }
47 |
48 | ///
49 | /// Initializes a new instance of the class, and associates a with it.
50 | ///
51 | /// The device that caused the exception.
52 | /// A description of the error.
53 | /// The new .
54 | public static UnauthorizedAccessException CreateUnauthorizedAccessException(Device device, string message)
55 | {
56 | return new Exceptions.DeviceUnauthorizedAccessException(device, message);
57 | }
58 |
59 | ///
60 | /// Gets the associated with the exception, if any.
61 | ///
62 | /// The exception to get the associated for.
63 | /// The associated , or null if none is associated with it.
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 | }
71 |
--------------------------------------------------------------------------------
/HidSharp/DeviceFilter.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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 | namespace HidSharp
19 | {
20 | public delegate bool DeviceFilter(Device device);
21 |
22 | public static class DeviceFilterHelper
23 | {
24 | public static bool MatchHidDevices(Device device, int? vendorID = null, int? productID = null, int? releaseNumberBcd = null, string serialNumber = null)
25 | {
26 | var hidDevice = device as HidDevice;
27 | if (hidDevice != null)
28 | {
29 | int vid = vendorID ?? -1, pid = productID ?? -1, ver = releaseNumberBcd ?? -1;
30 |
31 | if ((vid < 0 || hidDevice.VendorID == vendorID) &&
32 | (pid < 0 || hidDevice.ProductID == productID) &&
33 | (ver < 0 || hidDevice.ReleaseNumberBcd == releaseNumberBcd))
34 | {
35 | try
36 | {
37 | if (string.IsNullOrEmpty(serialNumber) || hidDevice.GetSerialNumber() == serialNumber) { return true; }
38 | }
39 | catch
40 | {
41 |
42 | }
43 | }
44 | }
45 |
46 | return false;
47 | }
48 |
49 | public static bool MatchSerialDevices(Device device, string portName = null)
50 | {
51 | var serialDevice = device as SerialDevice;
52 | if (serialDevice != null)
53 | {
54 | if (string.IsNullOrEmpty(portName) || serialDevice.DevicePath == portName)
55 | {
56 | return true;
57 | }
58 | }
59 |
60 | return false;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/HidSharp/DeviceListChangedEventArgs.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2016 James F. Bellinger
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 | public class DeviceListChangedEventArgs : EventArgs
23 | {
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/HidSharp/DeviceStream.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2016 James F. Bellinger
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 | using HidSharp.Utility;
21 |
22 | namespace HidSharp
23 | {
24 | public abstract class DeviceStream : Stream
25 | {
26 | ///
27 | /// Occurs when the stream is closed.
28 | ///
29 | public event EventHandler Closed;
30 |
31 | ///
32 | /// Occurs when is true and another process or thread with higher priority
33 | /// would like to open the stream.
34 | ///
35 | public event EventHandler InterruptRequested;
36 |
37 | ///
38 | protected DeviceStream(Device device)
39 | {
40 | Throw.If.Null(device);
41 | Device = device;
42 | }
43 |
44 | ///
45 | protected override void Dispose(bool disposing)
46 | {
47 | try
48 | {
49 | OnClosed();
50 | }
51 | catch (Exception e)
52 | {
53 | HidSharpDiagnostics.Trace("OnClosed threw an exception: {0}", e);
54 | }
55 |
56 | base.Dispose(disposing);
57 | }
58 |
59 | ///
60 | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
61 | {
62 | Throw.If.OutOfRange(buffer, offset, count);
63 | return AsyncResult.BeginOperation(delegate()
64 | {
65 | return Read(buffer, offset, count);
66 | }, callback, state);
67 | }
68 |
69 | ///
70 | public override int EndRead(IAsyncResult asyncResult)
71 | {
72 | return AsyncResult.EndOperation(asyncResult);
73 | }
74 |
75 | ///
76 | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
77 | {
78 | Throw.If.OutOfRange(buffer, offset, count);
79 | return AsyncResult.BeginOperation(delegate()
80 | {
81 | Write(buffer, offset, count); return 0;
82 | }, callback, state);
83 | }
84 |
85 | ///
86 | public override void EndWrite(IAsyncResult asyncResult)
87 | {
88 | AsyncResult.EndOperation(asyncResult);
89 | }
90 |
91 | ///
92 | public override long Seek(long offset, SeekOrigin origin)
93 | {
94 | throw new NotSupportedException();
95 | }
96 |
97 | ///
98 | public override void SetLength(long value)
99 | {
100 | throw new NotSupportedException();
101 | }
102 |
103 | protected virtual void OnClosed()
104 | {
105 | RaiseClosed();
106 | }
107 |
108 | protected void RaiseClosed()
109 | {
110 | var ev = Closed;
111 | if (ev != null) { ev(this, EventArgs.Empty); }
112 | }
113 |
114 | protected internal virtual void OnInterruptRequested()
115 | {
116 | RaiseInterruptRequested();
117 | }
118 |
119 | protected void RaiseInterruptRequested()
120 | {
121 | var ev = InterruptRequested;
122 | if (ev != null) { ev(this, EventArgs.Empty); }
123 | }
124 |
125 | ///
126 | public override bool CanRead
127 | {
128 | get { return true; }
129 | }
130 |
131 | ///
132 | public override bool CanSeek
133 | {
134 | get { return false; }
135 | }
136 |
137 | ///
138 | public override bool CanWrite
139 | {
140 | get { return true; }
141 | }
142 |
143 | ///
144 | public override bool CanTimeout
145 | {
146 | get { return true; }
147 | }
148 |
149 | ///
150 | /// Gets the associated with this stream.
151 | ///
152 | public Device Device
153 | {
154 | get;
155 | private set;
156 | }
157 |
158 | ///
159 | public override long Length
160 | {
161 | get { throw new NotSupportedException(); }
162 | }
163 |
164 | ///
165 | public override long Position
166 | {
167 | get { throw new NotSupportedException(); }
168 | set { throw new NotSupportedException(); }
169 | }
170 |
171 | ///
172 | /// The maximum amount of time, in milliseconds, to wait for the device to send some data.
173 | ///
174 | /// The default is 3000 milliseconds.
175 | /// To disable the timeout, set this to .
176 | ///
177 | public abstract override int ReadTimeout
178 | {
179 | get;
180 | set;
181 | }
182 |
183 | ///
184 | /// The maximum amount of time, in milliseconds, to wait for the device to receive the data.
185 | ///
186 | /// The default is 3000 milliseconds.
187 | /// To disable the timeout, set this to .
188 | ///
189 | public abstract override int WriteTimeout
190 | {
191 | get;
192 | set;
193 | }
194 |
195 | ///
196 | /// An object storing user-defined data about the stream.
197 | ///
198 | public object Tag
199 | {
200 | get;
201 | set;
202 | }
203 | }
204 | }
205 |
--------------------------------------------------------------------------------
/HidSharp/DeviceTypes.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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 | [Flags]
23 | public enum DeviceTypes
24 | {
25 | Hid = 1,
26 | Serial = 2,
27 | Ble = 4
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/HidSharp/Exceptions/DeviceIOException.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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.IO;
19 |
20 | namespace HidSharp.Exceptions
21 | {
22 | sealed class DeviceIOException : IOException, IDeviceException
23 | {
24 | public DeviceIOException(Device device, string message)
25 | : base(message)
26 | {
27 | Device = device;
28 | }
29 |
30 | public DeviceIOException(Device device, string message, int hresult)
31 | : base(message, hresult)
32 | {
33 | Device = device;
34 | }
35 |
36 | public Device Device
37 | {
38 | get;
39 | private set;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/HidSharp/Exceptions/DeviceUnauthorizedAccessException.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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.Exceptions
21 | {
22 | sealed class DeviceUnauthorizedAccessException : UnauthorizedAccessException, IDeviceException
23 | {
24 | public DeviceUnauthorizedAccessException(Device device, string message)
25 | : base(message)
26 | {
27 | Device = device;
28 | }
29 |
30 | public Device Device
31 | {
32 | get;
33 | private set;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/HidSharp/Exceptions/IDeviceException.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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 | namespace HidSharp.Exceptions
19 | {
20 | interface IDeviceException
21 | {
22 | Device Device { get; }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleCccd.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | [Flags]
23 | public enum BleCccd : ushort
24 | {
25 | None = 0,
26 | Notification = 1,
27 | Indication = 2
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleCharacteristic.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | public abstract class BleCharacteristic
23 | {
24 | public override string ToString()
25 | {
26 | return string.Format("{0} (properties: {1})", Uuid, Properties);
27 | }
28 |
29 | public abstract BleDescriptor[] GetDescriptors();
30 |
31 | public bool HasDescriptor(BleUuid uuid)
32 | {
33 | BleDescriptor descriptor;
34 | return TryGetDescriptor(uuid, out descriptor);
35 | }
36 |
37 | public BleDescriptor GetDescriptorOrNull(BleUuid uuid)
38 | {
39 | BleDescriptor descriptor;
40 | return TryGetDescriptor(uuid, out descriptor) ? descriptor : null;
41 | }
42 |
43 | public virtual bool TryGetDescriptor(BleUuid uuid, out BleDescriptor descriptor)
44 | {
45 | foreach (var d in GetDescriptors())
46 | {
47 | if (d.Uuid == uuid) { descriptor = d; return true; }
48 | }
49 |
50 | descriptor = null; return false;
51 | }
52 |
53 | public abstract BleUuid Uuid
54 | {
55 | get;
56 | }
57 |
58 | public abstract BleCharacteristicProperties Properties
59 | {
60 | get;
61 | }
62 |
63 | public bool IsReadable
64 | {
65 | get { return (Properties & BleCharacteristicProperties.Read) != 0; }
66 | }
67 |
68 | public bool IsWritable
69 | {
70 | get { return (Properties & BleCharacteristicProperties.Write) != 0; }
71 | }
72 |
73 | public bool IsWritableWithoutResponse
74 | {
75 | get { return (Properties & BleCharacteristicProperties.WriteWithoutResponse) != 0; }
76 | }
77 |
78 | public bool IsNotifiable
79 | {
80 | get { return (Properties & BleCharacteristicProperties.Notify) != 0; }
81 | }
82 |
83 | public bool IsIndicatable
84 | {
85 | get { return (Properties & BleCharacteristicProperties.Indicate) != 0; }
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleCharacteristicProperties.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | [Flags]
23 | public enum BleCharacteristicProperties : byte
24 | {
25 | None = 0x00,
26 | Broadcast = 0x01,
27 | Read = 0x02,
28 | WriteWithoutResponse = 0x04,
29 | Write = 0x08,
30 | Notify = 0x10,
31 | Indicate = 0x20,
32 | SignedWrite = 0x40,
33 | ExtendedProperties = 0x80
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleDescriptor.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | public abstract class BleDescriptor
23 | {
24 | public override string ToString()
25 | {
26 | return Uuid.ToString();
27 | }
28 |
29 | public abstract BleUuid Uuid
30 | {
31 | get;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleDevice.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.ComponentModel;
20 | using System.Runtime.CompilerServices;
21 | using System.Runtime.InteropServices;
22 |
23 | namespace HidSharp.Experimental
24 | {
25 | ///
26 | /// Represents a Bluetooth Low Energy device.
27 | ///
28 | [ComVisible(true), Guid("A7AEE7B8-893D-41B6-84F7-6BDA4EE3AA3F")]
29 | public abstract class BleDevice : Device
30 | {
31 | ///
32 | [EditorBrowsable(EditorBrowsableState.Never)]
33 | public new BleStream Open()
34 | {
35 | return (BleStream)base.Open();
36 | }
37 |
38 | ///
39 | [EditorBrowsable(EditorBrowsableState.Never)]
40 | public new BleStream Open(OpenConfiguration openConfig)
41 | {
42 | return (BleStream)base.Open(openConfig);
43 | }
44 |
45 | public BleStream Open(BleService service)
46 | {
47 | return Open(service, new OpenConfiguration());
48 | }
49 |
50 | public BleStream Open(BleService service, OpenConfiguration openConfig)
51 | {
52 | Throw.If.Null(service).Null(openConfig);
53 |
54 | openConfig = openConfig.Clone();
55 | openConfig.SetOption(OpenOption.BleService, service);
56 | return Open(openConfig);
57 | }
58 |
59 | /*
60 | public abstract bool GetConnectionState();
61 | */
62 |
63 | public abstract BleService[] GetServices();
64 |
65 | public BleService GetServiceOrNull(BleUuid uuid)
66 | {
67 | BleService service;
68 | return TryGetService(uuid, out service) ? service : null;
69 | }
70 |
71 | public virtual bool HasService(BleUuid uuid)
72 | {
73 | BleService service;
74 | return TryGetService(uuid, out service);
75 | }
76 |
77 | public virtual bool TryGetService(BleUuid uuid, out BleService service)
78 | {
79 | foreach (var s in GetServices())
80 | {
81 | if (s.Uuid == uuid) { service = s; return true; }
82 | }
83 |
84 | service = null; return false;
85 | }
86 |
87 | public override bool HasImplementationDetail(Guid detail)
88 | {
89 | return base.HasImplementationDetail(detail) || detail == ImplementationDetail.BleDevice;
90 | }
91 |
92 | public override string ToString()
93 | {
94 | string friendlyName = "(unknown friendly name)";
95 | try { friendlyName = GetFriendlyName(); }
96 | catch { }
97 |
98 | return friendlyName;
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleDiscovery.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | abstract class BleDiscovery : IDisposable
23 | {
24 | public abstract void StopDiscovery();
25 |
26 | void IDisposable.Dispose()
27 | {
28 | StopDiscovery();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleEvent.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace HidSharp.Experimental
7 | {
8 | public struct BleEvent
9 | {
10 | BleCharacteristic _characteristic;
11 | byte[] _value;
12 |
13 | public BleEvent(BleCharacteristic characteristic, byte[] value)
14 | {
15 | _characteristic = characteristic; _value = value;
16 | }
17 |
18 | public BleCharacteristic Characteristic
19 | {
20 | get { return _characteristic; }
21 | }
22 |
23 | public byte[] Value
24 | {
25 | get { return _value; }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleRequestFlags.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | [Flags]
23 | public enum BleRequestFlags
24 | {
25 | None = 0,
26 | Authenticated = 1,
27 | Encrypted = 2,
28 | Cacheable = 4
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleService.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Experimental
21 | {
22 | public abstract class BleService
23 | {
24 | public override string ToString()
25 | {
26 | return Uuid.ToString();
27 | }
28 |
29 | public abstract BleCharacteristic[] GetCharacteristics();
30 |
31 | public BleCharacteristic GetCharacteristicOrNull(BleUuid uuid)
32 | {
33 | BleCharacteristic characteristic;
34 | return TryGetCharacteristic(uuid, out characteristic) ? characteristic : null;
35 | }
36 |
37 | public virtual bool HasCharacteristic(BleUuid uuid)
38 | {
39 | BleCharacteristic characteristic;
40 | return TryGetCharacteristic(uuid, out characteristic);
41 | }
42 |
43 | public virtual bool TryGetCharacteristic(BleUuid uuid, out BleCharacteristic characteristic)
44 | {
45 | foreach (var c in GetCharacteristics())
46 | {
47 | if (c.Uuid == uuid) { characteristic = c; return true; }
48 | }
49 |
50 | characteristic = null; return false;
51 | }
52 |
53 | public abstract BleDevice Device
54 | {
55 | get;
56 | }
57 |
58 | public abstract BleUuid Uuid
59 | {
60 | get;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleUuid.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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.Globalization;
20 |
21 | namespace HidSharp.Experimental
22 | {
23 | public struct BleUuid : IEquatable
24 | {
25 | Guid _guid;
26 |
27 | public BleUuid(int uuid) : this()
28 | {
29 | Initialize(uuid);
30 | }
31 |
32 | public BleUuid(Guid guid) : this()
33 | {
34 | Initialize(guid);
35 | }
36 |
37 | public BleUuid(string uuid) : this()
38 | {
39 | Initialize(uuid);
40 | }
41 |
42 | void Initialize(int uuid)
43 | {
44 | _guid = new Guid(uuid, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB);
45 | }
46 |
47 | void Initialize(Guid guid)
48 | {
49 | _guid = guid;
50 | }
51 |
52 | void Initialize(string guid)
53 | {
54 | uint shortUuid;
55 | if (uint.TryParse(guid, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out shortUuid))
56 | {
57 | Initialize((int)shortUuid);
58 | }
59 | else
60 | {
61 | Initialize(new Guid(guid));
62 | }
63 | }
64 |
65 | public override bool Equals(object other)
66 | {
67 | return other is BleUuid && Equals((BleUuid)other);
68 | }
69 |
70 | public bool Equals(BleUuid other)
71 | {
72 | return _guid.Equals(other._guid);
73 | }
74 |
75 | public override int GetHashCode()
76 | {
77 | return _guid.GetHashCode();
78 | }
79 |
80 | public static implicit operator BleUuid(Guid guid)
81 | {
82 | return new BleUuid(guid);
83 | }
84 |
85 | public static implicit operator Guid(BleUuid uuid)
86 | {
87 | return uuid.ToGuid();
88 | }
89 |
90 | public int ToShortUuid()
91 | {
92 | if (!IsShortUuid) { throw new InvalidOperationException(); }
93 |
94 | byte[] bytes = _guid.ToByteArray();
95 | return (ushort)(bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24);
96 | }
97 |
98 | static void SwapNetworkOrder(byte[] guid)
99 | {
100 | byte temp;
101 | temp = guid[0]; guid[0] = guid[3]; guid[3] = temp;
102 | temp = guid[1]; guid[1] = guid[2]; guid[2] = temp;
103 | temp = guid[4]; guid[4] = guid[5]; guid[5] = temp;
104 | temp = guid[6]; guid[6] = guid[7]; guid[7] = temp;
105 | }
106 |
107 | public byte[] ToByteArray()
108 | {
109 | var guid = _guid.ToByteArray();
110 | SwapNetworkOrder(guid);
111 | return guid;
112 | }
113 |
114 | public Guid ToGuid()
115 | {
116 | return _guid;
117 | }
118 |
119 | public override string ToString()
120 | {
121 | return IsShortUuid ? ToShortUuid().ToString("X", CultureInfo.InvariantCulture) : ToGuid().ToString("D", CultureInfo.InvariantCulture);
122 | }
123 |
124 | public bool IsShortUuid
125 | {
126 | get
127 | {
128 | byte[] bytes = _guid.ToByteArray();
129 | return bytes[4] == 0x00 && bytes[5] == 0x00
130 | && bytes[6] == 0x00 && bytes[7] == 0x10
131 | && bytes[8] == 0x80 && bytes[9] == 0x00 && bytes[10] == 0x00 && bytes[11] == 0x80 && bytes[12] == 0x5F && bytes[13] == 0x9B && bytes[14] == 0x34 && bytes[15] == 0xFB;
132 | }
133 | }
134 |
135 | public static bool operator ==(BleUuid lhs, BleUuid rhs)
136 | {
137 | return lhs.Equals(rhs);
138 | }
139 |
140 | public static bool operator !=(BleUuid lhs, BleUuid rhs)
141 | {
142 | return !lhs.Equals(rhs);
143 | }
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/HidSharp/Experimental/BleUuids.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2019 James F. Bellinger
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 | namespace HidSharp.Experimental
19 | {
20 | static class BleUuids
21 | {
22 | public static readonly BleUuid Cccd = new BleUuid(0x2902);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/HidSharp/FilteredDeviceList.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2016-2017 James F. Bellinger
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.Collections.Generic;
20 | using System.Linq;
21 | using System.Threading;
22 |
23 | namespace HidSharp
24 | {
25 | public class FilteredDeviceList : DeviceList
26 | {
27 | int _dirty;
28 | List> _areDriversBeingInstalled;
29 | Dictionary _refCounts;
30 |
31 | public FilteredDeviceList()
32 | {
33 | _areDriversBeingInstalled = new List>();
34 | _refCounts = new Dictionary();
35 | }
36 |
37 | /*
38 | public override BleDiscovery BeginBleDiscovery()
39 | {
40 | throw new NotImplementedException();
41 | }
42 | */
43 |
44 | public void Add(Device device)
45 | {
46 | Throw.If.Null(device, "device");
47 |
48 | lock (_refCounts)
49 | {
50 | IncrementRefCount(device);
51 | }
52 |
53 | RaiseChangedIfDirty();
54 | }
55 |
56 | public void Add(DeviceList deviceList)
57 | {
58 | Add(deviceList, device => true);
59 | }
60 |
61 | public void Add(DeviceList deviceList, DeviceFilter filter)
62 | {
63 | Throw.If.Null(deviceList, "deviceList").Null(filter, "filter");
64 |
65 | var oldDevices = new Device[0];
66 | Action updateDeviceList = () =>
67 | {
68 | var newDevices = deviceList.GetAllDevices(filter).ToArray();
69 |
70 | lock (_refCounts)
71 | {
72 | foreach (var newDevice in newDevices)
73 | {
74 | IncrementRefCount(newDevice);
75 | }
76 |
77 | foreach (var oldDevice in oldDevices)
78 | {
79 | DecrementRefCount(oldDevice);
80 | }
81 | }
82 |
83 | oldDevices = newDevices;
84 | RaiseChangedIfDirty();
85 | };
86 |
87 | _areDriversBeingInstalled.Add(() => deviceList.AreDriversBeingInstalled);
88 | deviceList.Changed += (sender, e) => updateDeviceList();
89 | updateDeviceList();
90 | }
91 |
92 | ///
93 | public override IEnumerable GetAllDevices()
94 | {
95 | lock (_refCounts)
96 | {
97 | return _refCounts.Keys.ToList();
98 | }
99 | }
100 |
101 | void IncrementRefCount(Device device)
102 | {
103 | if (_refCounts.ContainsKey(device))
104 | {
105 | _refCounts[device]++;
106 | }
107 | else
108 | {
109 | _refCounts[device] = 1; _dirty = 1;
110 | }
111 | }
112 |
113 | void DecrementRefCount(Device device)
114 | {
115 | if (--_refCounts[device] == 0)
116 | {
117 | _refCounts.Remove(device); _dirty = 1;
118 | }
119 | }
120 |
121 | void RaiseChangedIfDirty()
122 | {
123 | if (1 == Interlocked.CompareExchange(ref _dirty, 0, 1))
124 | {
125 | RaiseChanged();
126 | }
127 | }
128 |
129 | ///
130 | public override bool AreDriversBeingInstalled
131 | {
132 | get { return _areDriversBeingInstalled.Any(callback => callback()); }
133 | }
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/HidSharp/HidDeviceLoader.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2010, 2013 James F. Bellinger
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.Collections;
20 | using System.Collections.Generic;
21 | using System.ComponentModel;
22 | using System.Runtime.InteropServices;
23 |
24 | namespace HidSharp
25 | {
26 | ///
27 | [ComVisible(true), Guid("CD7CBD7D-7204-473c-AA2A-2B9622CFC6CC")]
28 | [Obsolete, EditorBrowsable(EditorBrowsableState.Never)]
29 | public class HidDeviceLoader
30 | {
31 | ///
32 | [Obsolete, EditorBrowsable(EditorBrowsableState.Never)]
33 | public HidDeviceLoader()
34 | {
35 |
36 | }
37 |
38 | ///
39 | [Obsolete, EditorBrowsable(EditorBrowsableState.Never)]
40 | public IEnumerable GetDevicesVB()
41 | {
42 | return DeviceList.Local.GetHidDevices();
43 | }
44 |
45 | ///
46 | [Obsolete, EditorBrowsable(EditorBrowsableState.Never)]
47 | public IEnumerable GetDevices()
48 | {
49 | return DeviceList.Local.GetHidDevices();
50 | }
51 |
52 | ///
53 | [Obsolete, EditorBrowsable(EditorBrowsableState.Never)]
54 | public IEnumerable GetDevices(int? vendorID = null, int? productID = null, int? productVersion = null, string serialNumber = null)
55 | {
56 | return DeviceList.Local.GetHidDevices(vendorID, productID, productVersion, serialNumber);
57 | }
58 |
59 | ///
60 | [Obsolete, EditorBrowsable(EditorBrowsableState.Never)]
61 | public HidDevice GetDeviceOrDefault(int? vendorID = null, int? productID = null, int? productVersion = null, string serialNumber = null)
62 | {
63 | return DeviceList.Local.GetHidDeviceOrNull(vendorID, productID, productVersion, serialNumber);
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/HidSharp/HidSharp.NETStandard.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | true
6 | false
7 | HidSharp
8 | HidSharp
9 | ..\bin
10 | ..\bin\netstandard2.0\HidSharp.XML
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/HidSharp/HidSharp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net8.0
4 | Library
5 | false
6 | ..\bin\
7 | Illusory Studios LLC
8 | Copyright © 2010-2019 James F. Bellinger <http://www.zer7.com/software/hidsharp>
9 | C# HID wrappers
10 | HidSharp
11 | HidSharp
12 | 2.1.0.0
13 | 2.1.0.0
14 | 2.1.0
15 | True
16 |
17 |
--------------------------------------------------------------------------------
/HidSharp/HidStream.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2012-2013 James F. Bellinger
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.Runtime.InteropServices;
20 |
21 | #pragma warning disable 420
22 |
23 | namespace HidSharp
24 | {
25 | ///
26 | /// Communicates with a USB HID class device.
27 | ///
28 | [ComVisible(true), Guid("0C263D05-0D58-4c6c-AEA7-EB9E0C5338A2")]
29 | public abstract class HidStream : DeviceStream
30 | {
31 | ///
32 | protected HidStream(HidDevice device)
33 | : base(device)
34 | {
35 | ReadTimeout = 3000;
36 | WriteTimeout = 3000;
37 | }
38 |
39 | ///
40 | public override void Flush()
41 | {
42 |
43 | }
44 |
45 | ///
46 | /// Sends a Get Feature setup request.
47 | ///
48 | /// The buffer to fill. Place the Report ID in the first byte.
49 | public void GetFeature(byte[] buffer)
50 | {
51 | Throw.If.Null(buffer, "buffer");
52 | GetFeature(buffer, 0, buffer.Length);
53 | }
54 |
55 | ///
56 | /// Sends a Get Feature setup request.
57 | ///
58 | /// The buffer to fill. Place the Report ID in the byte at index .
59 | /// The index in the buffer to begin filling with data.
60 | /// The number of bytes in the feature request.
61 | public abstract void GetFeature(byte[] buffer, int offset, int count);
62 |
63 | ///
64 | /// Reads HID Input Reports.
65 | ///
66 | /// The data read.
67 | public byte[] Read()
68 | {
69 | byte[] buffer = new byte[Device.GetMaxInputReportLength()];
70 | int bytes = Read(buffer); Array.Resize(ref buffer, bytes);
71 | return buffer;
72 | }
73 |
74 | ///
75 | /// Reads HID Input Reports.
76 | ///
77 | /// The buffer to place the reports into.
78 | /// The number of bytes read.
79 | public int Read(byte[] buffer)
80 | {
81 | Throw.If.Null(buffer, "buffer");
82 | return Read(buffer, 0, buffer.Length);
83 | }
84 |
85 | ///
86 | /// Sends a Set Feature setup request.
87 | ///
88 | /// The buffer of data to send. Place the Report ID in the first byte.
89 | public void SetFeature(byte[] buffer)
90 | {
91 | Throw.If.Null(buffer, "buffer");
92 | SetFeature(buffer, 0, buffer.Length);
93 | }
94 |
95 | ///
96 | /// Sends a Set Feature setup request.
97 | ///
98 | /// The buffer of data to send. Place the Report ID in the byte at index .
99 | /// The index in the buffer to start the write from.
100 | /// The number of bytes in the feature request.
101 | public abstract void SetFeature(byte[] buffer, int offset, int count);
102 |
103 | ///
104 | /// Writes an HID Output Report to the device.
105 | ///
106 | /// The buffer containing the report. Place the Report ID in the first byte.
107 | public void Write(byte[] buffer)
108 | {
109 | Throw.If.Null(buffer, "buffer");
110 | Write(buffer, 0, buffer.Length);
111 | }
112 |
113 | ///
114 | /// Gets the associated with this stream.
115 | ///
116 | public new HidDevice Device
117 | {
118 | get { return (HidDevice)base.Device; }
119 | }
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/HidSharp/ImplementationDetail.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2017 James F. Bellinger
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 | ///
23 | /// Specifies the 's low-level implementation.
24 | ///
25 | public static class ImplementationDetail
26 | {
27 | ///
28 | /// The device is running on Windows.
29 | ///
30 | public static Guid Windows { get; private set; }
31 |
32 | ///
33 | /// The device is running on a Mac.
34 | ///
35 | public static Guid MacOS { get; private set; }
36 |
37 | ///
38 | /// The device is running on Linux.
39 | ///
40 | public static Guid Linux { get; private set; }
41 |
42 | ///
43 | /// The device is a Bluetooth Low Energy device.
44 | ///
45 | public static Guid BleDevice { get; private set; }
46 |
47 | ///
48 | /// The device is a HID device.
49 | ///
50 | public static Guid HidDevice { get; private set; }
51 |
52 | ///
53 | /// The device is a serial device.
54 | ///
55 | public static Guid SerialDevice { get; private set; }
56 |
57 | ///
58 | /// The device is implemented using the Linux hidraw API.
59 | ///
60 | public static Guid HidrawApi { get; private set; }
61 |
62 | static ImplementationDetail()
63 | {
64 | Windows = new Guid("{3540D886-E329-419F-8033-1D7355D53A7E}");
65 | MacOS = new Guid("{9FE992E5-F804-41B6-A35F-3B60F7CAC9E2}");
66 | Linux = new Guid("{A4123219-6BC8-49B7-84D3-699A66373109}");
67 |
68 | BleDevice = new Guid("{AAFD1479-29A0-42B8-A0A9-5C88A18B5504}");
69 | HidDevice = new Guid("{DFF209D7-131E-4958-8F47-C23DAC7B62DA}");
70 | SerialDevice = new Guid("{45A96DA9-AA48-4BF7-978D-A845F185F38C}");
71 |
72 | HidrawApi = new Guid("{1199D7C6-F99F-471F-9730-B16BA615938F}");
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/HidSharp/LocalDeviceList.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2018 James F. Bellinger
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.Collections.Generic;
19 |
20 | namespace HidSharp
21 | {
22 | sealed class LocalDeviceList : DeviceList
23 | {
24 | /*
25 | public override BleDiscovery BeginBleDiscovery()
26 | {
27 | return Platform.HidSelector.Instance.BeginBleDiscovery();
28 | }
29 | */
30 |
31 | public override IEnumerable GetDevices(DeviceTypes types)
32 | {
33 | return Platform.HidSelector.Instance.GetDevices(types);
34 | }
35 |
36 | public override IEnumerable GetAllDevices()
37 | {
38 | return GetDevices(DeviceTypes.Hid | DeviceTypes.Serial | DeviceTypes.Ble);
39 | }
40 |
41 | public override string ToString()
42 | {
43 | return Platform.HidSelector.Instance.FriendlyName; // This value is useful for debugging.
44 | }
45 |
46 | public override bool AreDriversBeingInstalled
47 | {
48 | get { return Platform.HidSelector.Instance.AreDriversBeingInstalled; }
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/HidSharp/OpenConfiguration.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2016, 2017 James F. Bellinger
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.Collections.Generic;
20 |
21 | namespace HidSharp
22 | {
23 | ///
24 | /// Describes all options for opening a device stream.
25 | ///
26 | public class OpenConfiguration : ICloneable
27 | {
28 | Dictionary _options;
29 |
30 | ///
31 | /// Initializes a new instance of the class.
32 | ///
33 | public OpenConfiguration()
34 | {
35 | _options = new Dictionary();
36 | }
37 |
38 | OpenConfiguration(Dictionary options)
39 | {
40 | _options = new Dictionary(options);
41 | }
42 |
43 | public OpenConfiguration Clone()
44 | {
45 | return new OpenConfiguration(_options);
46 | }
47 |
48 | object ICloneable.Clone()
49 | {
50 | return Clone();
51 | }
52 |
53 | ///
54 | /// Gets the current value of an option.
55 | ///
56 | /// The option.
57 | /// The option's value.
58 | public object GetOption(OpenOption option)
59 | {
60 | Throw.If.Null(option, "option");
61 |
62 | object value;
63 | return _options.TryGetValue(option, out value) ? value : option.DefaultValue;
64 | }
65 |
66 | ///
67 | /// Gets a list of all currently set options.
68 | ///
69 | /// The options list.
70 | public IEnumerable GetOptionsList()
71 | {
72 | return _options.Keys;
73 | }
74 |
75 | ///
76 | /// Checks if an option has been set.
77 | ///
78 | /// The option.
79 | /// true if the option has been set.
80 | public bool IsOptionSet(OpenOption option)
81 | {
82 | Throw.If.Null(option, "option");
83 |
84 | return _options.ContainsKey(option);
85 | }
86 |
87 | ///
88 | /// Sets the current value of an option.
89 | ///
90 | /// The option.
91 | /// The value to set it to.
92 | public void SetOption(OpenOption option, object value)
93 | {
94 | Throw.If.Null(option, "option");
95 |
96 | if (value != null)
97 | {
98 | _options[option] = value;
99 | }
100 | else
101 | {
102 | _options.Remove(option);
103 | }
104 | }
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/HidSharp/OpenPriority.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2016 James F. Bellinger
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 | namespace HidSharp
19 | {
20 | ///
21 | /// The priority at which to open a device stream.
22 | ///
23 | public enum OpenPriority
24 | {
25 | ///
26 | /// The lowest priority.
27 | ///
28 | Idle = -2,
29 |
30 | ///
31 | /// Very low priority.
32 | ///
33 | VeryLow = -1,
34 |
35 | ///
36 | /// Low priority.
37 | ///
38 | Low = 0,
39 |
40 | ///
41 | /// The default priority.
42 | ///
43 | Normal = 1,
44 |
45 | ///
46 | /// High priority.
47 | ///
48 | High = 2,
49 |
50 | ///
51 | /// The highest priority.
52 | ///
53 | VeryHigh = 3
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/HidSharp/Platform/HidManager.cs:
--------------------------------------------------------------------------------
1 | #region License
2 | /* Copyright 2012-2019 James F. Bellinger
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.Collections.Generic;
20 | using System.Linq;
21 | using System.Threading;
22 | using HidSharp.Experimental;
23 | using HidSharp.Utility;
24 |
25 | namespace HidSharp.Platform
26 | {
27 | abstract class HidManager
28 | {
29 | sealed class DeviceTypeInfo
30 | {
31 | public delegate object[] GetDeviceKeysCallbackType();
32 | public delegate bool TryCreateDeviceCallbackType(object key, out Device device);
33 |
34 | public object DevicesLock = new object();
35 | public Dictionary