Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UsbDevice.Common.cs « LibWinUsb - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95ba24eef0176d335286ebab19972f74c6312dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright © 2006-2010 Travis Robinson. All rights reserved.
// 
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail:  libusbdotnet@gmail.com
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or 
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. or 
// visit www.gnu.org.
// 
// 
using System;
using System.Collections.Generic;
using LibUsbDotNet.Internal.LibUsb;
using LibUsbDotNet.LibUsb;
using LibUsbDotNet.Main;
using LibUsbDotNet.LudnMonoLibUsb;
using LibUsbDotNet.WinUsb.Internal;
using Debug=System.Diagnostics.Debug;

namespace LibUsbDotNet
{
    public abstract partial class UsbDevice
    {
        private static LibUsbAPI _libUsbApi;
        private static WinUsbAPI _winUsbApi;
        private static object mHasWinUsbDriver;
        private static object mHasLibUsbWinBackDriver;

        private static LibUsbKernelType mLibUsbKernelType;
        private static UsbKernelVersion mUsbKernelVersion;

        /// <summary>
        /// Gets a list of all available USB devices (WinUsb, LibUsb, Linux LibUsb v1.x).
        /// </summary>
        /// <remarks>
        /// Use this property to get a list of USB device that can be accessed by LibUsbDotNet.
        /// Using this property as opposed to <see cref="AllLibUsbDevices"/> and <see cref="AllWinUsbDevices"/>
        /// will ensure your source code is platform-independent.
        /// </remarks>
        public static UsbRegDeviceList AllDevices
        {
            get
            {
                UsbRegDeviceList regDevReturnList = new UsbRegDeviceList();

                UsbRegDeviceList winUsbList = AllWinUsbDevices;
                foreach (UsbRegistry winUsbRegistry in winUsbList)
                    regDevReturnList.Add(winUsbRegistry);

                UsbRegDeviceList libUsbList = AllLibUsbDevices;
                foreach (UsbRegistry libUsbRegistry in libUsbList)
                    regDevReturnList.Add(libUsbRegistry);

                return regDevReturnList;
            }
        }

        /// <summary>
        /// Gets a list of all available libusb-win32 USB devices.
        /// </summary>
        /// <remarks>
        /// <para>
        /// On windows, gets a list of libusb-win32 USB devices . If <see cref="ForceLibUsbWinBack"/> 
        /// is true, gets a list of libusb-1.0 devices.
        /// </para>
        /// <para>
        /// On linux/mac, gets a list of libusb-1.0 devices.
        /// </para>
        /// </remarks>
        public static UsbRegDeviceList AllLibUsbDevices
        {
            get
            {
                UsbRegDeviceList regDevList = new UsbRegDeviceList();
                if (HasLibUsbWinBackDriver && ForceLibUsbWinBack)
                {
                    List<MonoUsbDevice> deviceList = MonoUsbDevice.MonoUsbDeviceList;
                    foreach (MonoUsbDevice usbDevice in deviceList)
                    {
                        regDevList.Add(new LegacyUsbRegistry(usbDevice));
                    }
                }
                else
                {
                    if (!ForceLegacyLibUsb && KernelType == LibUsbKernelType.NativeLibUsb)
                    {
                        List<LibUsbRegistry> libUsbRegistry = LibUsbRegistry.DeviceList;
                        foreach (LibUsbRegistry usbRegistry in libUsbRegistry)
                            regDevList.Add(usbRegistry);
                    }
                    else 
                    {
                        List<LegacyUsbRegistry> libUsbRegistry = LegacyUsbRegistry.DeviceList;
                        foreach (LegacyUsbRegistry usbRegistry in libUsbRegistry)
                            regDevList.Add(usbRegistry);
                    }
                }
                return regDevList;
            }
        }

        /// <summary>
        /// Returns the last error number reported by LibUsbDotNet.
        /// </summary>
        public static int LastErrorNumber
        {
            get { return UsbError.mLastErrorNumber; }
        }

        /// <summary>
        /// Returns the last error string reported by LibUsbDotNet.
        /// </summary>
        public static string LastErrorString
        {
            get { return UsbError.mLastErrorString; }
        }

        internal static LibUsbAPI LibUsbApi
        {
            get
            {
                if (ReferenceEquals(_libUsbApi, null))
                    _libUsbApi = new LibUsbAPI();
                return _libUsbApi;
            }
        }

        internal static WinUsbAPI WinUsbApi
        {
            get
            {
                if (ReferenceEquals(_winUsbApi, null))
                    _winUsbApi = new WinUsbAPI();
                return _winUsbApi;
            }
        }



        /// <summary>
        /// Opens the usb device that matches the <see cref="UsbDeviceFinder"/>.
        /// </summary>
        /// <param name="usbDeviceFinder">The <see cref="UsbDeviceFinder"/> class used to find the usb device.</param>
        /// <returns>An valid/open usb device class if the device was found or Null if the device was not found.</returns>
        public static UsbDevice OpenUsbDevice(UsbDeviceFinder usbDeviceFinder) 
        {
            return OpenUsbDevice((Predicate<UsbRegistry>) usbDeviceFinder.Check);
        }

        /// <summary>
        /// Opens the usb device that matches the find predicate.
        /// </summary>
        /// <param name="findDevicePredicate">The predicate function used to find the usb device.</param>
        /// <returns>An valid/open usb device class if the device was found or Null if the device was not found.</returns>
        public static UsbDevice OpenUsbDevice(Predicate<UsbRegistry> findDevicePredicate)
        {
            UsbDevice usbDeviceFound;

            UsbRegDeviceList allDevices = AllDevices;
            UsbRegistry regDeviceFound = allDevices.Find(findDevicePredicate);

            if (ReferenceEquals(regDeviceFound, null)) return null;

            usbDeviceFound = regDeviceFound.Device;

            return usbDeviceFound;
        }

        /// <summary>
        /// Opens a WinUsb device by its DeviceInterfaceGUID.
        /// </summary>
        /// <remarks>
        /// This is the Microsoft-recommended way for opening a WinUsb device.  
        /// LibUsb device can be opened in this way as well.  In order to open
        /// LibUsb devices in this manner, an entry must be added to the driver
        /// inf file:
        /// <para>[Install.HW]</para>
        /// <para>Addreg=Add_LibUsb_Guid_Reg</para>
        /// <para>[Add_LibUsb_Guid_Reg]</para>
        /// <para>HKR,,LibUsbInterfaceGUIDs,0x10000,"{Your-Unique-Guid-String}"</para>
        /// </remarks>
        /// <param name="devInterfaceGuid">Device Interface GUID of the usb device to open.</param>
        /// <param name="usbDevice">On success, a new <see cref="UsbDevice"/> instance.</param>
        /// <returns>True on success.</returns>
        public static bool OpenUsbDevice(ref Guid devInterfaceGuid, out UsbDevice usbDevice)
        {
            usbDevice = null;
            UsbRegDeviceList usbRegDevices = AllDevices;
            foreach (UsbRegistry usbRegistry in usbRegDevices)
            {
                foreach (Guid guid in usbRegistry.DeviceInterfaceGuids)
                {
                    if (guid == devInterfaceGuid)
                    {
                        usbDevice = usbRegistry.Device;
                        if (usbDevice != null) return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Global static error event for all Usb errors.
        /// </summary>
        /// <example>
        /// Sample code to reset an endpoint if a critical error occurs.
        /// <code>
        /// // Hook the usb error handler function
        /// UsbGlobals.UsbErrorEvent += UsbErrorEvent;
        ///private void UsbErrorEvent(object sender, UsbError e)
        ///{
        /// // If the error is from a usb endpoint
        /// if (sender is UsbEndpointBase)
        /// {
        ///     // If the endpoint transfer failed
        ///     if (e.Win32ErrorNumber == 31)
        ///     {
        ///         // If the USB device is still open, connected, and valid
        ///         if (usb.IsOpen)
        ///         {
        ///             // Try to reset then endpoint
        ///             if (((UsbEndpointBase) sender).Reset())
        ///             {
        ///                 // Endpoint reset successful.
        ///                 // Tell LibUsbDotNet to ignore this error and continue.
        ///                 e.Handled = true;
        ///             }
        ///         }
        ///     }
        /// }
        /// }
        /// </code>
        /// </example>
        public static event EventHandler<UsbError> UsbErrorEvent;
    }
}