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

UsbDeviceFinder.cs « Main « LibWinUsb - github.com/ClusterM/clovershell-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c3e6321f3af636374aaf97d86e92e75ada1b804 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// 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 System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace LibUsbDotNet.Main
{
    /// <summary>
    /// Finds and identifies usb devices. Used for easily locating  
    /// </summary>
    /// <remarks>
    /// <list type="bullet">
    /// <item>
    /// Instances of this class can optionally be passed directly into 
    /// <see cref="UsbDevice.OpenUsbDevice(LibUsbDotNet.Main.UsbDeviceFinder)"/> 
    /// to quickly find and open a specific usb device in one step.
    /// </item>
    /// <item>
    /// Pass instances of this class into the 
    /// <see cref="UsbRegDeviceList.Find(UsbDeviceFinder)"/>, 
    /// <see cref="UsbRegDeviceList.FindAll(UsbDeviceFinder)"/>,  
    /// or <see cref="UsbRegDeviceList.FindLast(UsbDeviceFinder)"/> 
    /// functions of a  <see cref="UsbRegDeviceList"/> 
    /// instance to find connected usb devices without opening devices or interrogating the bus.
    /// After locating the required <see cref="UsbRegistry"/> instance, call the 
    /// <see cref="UsbRegistry.Open"/> method to start using the <see cref="UsbDevice"/> instance.
    /// </item>
    /// </list>
    /// </remarks>
    /// <example>
    /// <code source="..\Examples\Show.Info\ShowInfo.cs" lang="cs"/>
    /// </example>
    public class UsbDeviceFinder : ISerializable
    {
        ///<summary> The "exclude from search" value for <see cref="Pid"/>. </summary>
        public const int NO_PID = int.MaxValue;

        ///<summary> The "exclude from search" value for <see cref="Revision"/>. </summary>
        public const int NO_REV = int.MaxValue;

        ///<summary> The "exclude from search" value for <see cref="SerialNumber"/>. </summary>
        public const string NO_SERIAL = null;

        ///<summary> The "exclude from search" value for <see cref="Vid"/>. </summary>
        public const int NO_VID = int.MaxValue;

        ///<summary>  The "exclude from search" value for <see cref="DeviceInterfaceGuid"/>. </summary>
        public static readonly Guid NO_GUID = Guid.Empty;

        private Guid mDeviceInterfaceGuid = Guid.Empty;
        private int mPid = int.MaxValue;
        private int mRevision = int.MaxValue;
        private string mSerialNumber;
        private int mVid = int.MaxValue;

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating and identifying usb devices.
        /// </summary>
        /// <param name="vid">The vendor id of the usb device to find, or <see cref="int.MaxValue"/> to ignore.</param>
        /// <param name="pid">The product id of the usb device to find, or <see cref="int.MaxValue"/> to ignore.</param>
        /// <param name="revision">The revision number of the usb device to find, or <see cref="int.MaxValue"/> to ignore.</param>
        /// <param name="serialNumber">The serial number of the usb device to find, or null to ignore.</param>
        /// <param name="deviceInterfaceGuid">The unique guid of the usb device to find, or <see cref="Guid.Empty"/> to ignore.</param>
        public UsbDeviceFinder(int vid, int pid, int revision, string serialNumber, Guid deviceInterfaceGuid)
        {
            mVid = vid;
            mPid = pid;
            mRevision = revision;
            mSerialNumber = serialNumber;
            mDeviceInterfaceGuid = deviceInterfaceGuid;
        }

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating usb devices by VendorID, ProductID, and Serial number.
        /// </summary>
        /// <param name="vid">The vendor id of the usb device to find.</param>
        /// <param name="pid">The product id of the usb device to find.</param>
        /// <param name="serialNumber">The serial number of the usb device to find.</param>
        public UsbDeviceFinder(int vid, int pid, string serialNumber)
            : this(vid, pid, int.MaxValue, serialNumber, Guid.Empty) { }

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating usb devices by VendorID, ProuctID, and Revision code.
        /// </summary>
        /// <param name="vid">The vendor id of the usb device to find.</param>
        /// <param name="pid">The product id of the usb device to find.</param>
        /// <param name="revision">The revision number of the usb device to find.</param>
        public UsbDeviceFinder(int vid, int pid, int revision)
            : this(vid, pid, revision, null, Guid.Empty) { }

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating usb devices vendor and product ID.
        /// </summary>
        /// <param name="vid">The vendor id of the usb device to find.</param>
        /// <param name="pid">The product id of the usb device to find.</param>
        public UsbDeviceFinder(int vid, int pid)
            : this(vid, pid, int.MaxValue, null, Guid.Empty) { }

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating usb devices.
        /// </summary>
        /// <param name="vid">The vendor id of the usb device to find.</param>
        public UsbDeviceFinder(int vid)
            : this(vid, int.MaxValue, int.MaxValue, null, Guid.Empty) { }

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating usb devices by a serial number.
        /// </summary>
        /// <param name="serialNumber">The serial number of the usb device to find.</param>
        public UsbDeviceFinder(string serialNumber)
            : this(int.MaxValue, int.MaxValue, int.MaxValue, serialNumber, Guid.Empty) { }

        /// <summary>
        /// Creates a UsbDeviceFinder class for locating usb devices by a unique <see cref="Guid"/> string.
        /// </summary>
        /// <param name="deviceInterfaceGuid">The unique <see cref="Guid"/> to find.</param>
        public UsbDeviceFinder(Guid deviceInterfaceGuid)
            : this(int.MaxValue, int.MaxValue, int.MaxValue, null, deviceInterfaceGuid) { }

        /// <summary>
        /// Use a serialization stream to fill the <see cref="UsbDeviceFinder"/> class. 
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected UsbDeviceFinder(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            mVid = (int) info.GetValue("Vid", typeof (int));
            mPid = (int) info.GetValue("Pid", typeof (int));
            mRevision = (int) info.GetValue("Revision", typeof (int));
            mSerialNumber = (string) info.GetValue("SerialNumber", typeof (string));
            mDeviceInterfaceGuid = (Guid) info.GetValue("DeviceInterfaceGuid", typeof (Guid));
        }

        /// <summary>
        /// 
        /// </summary>
        protected UsbDeviceFinder() { }

        /// <summary>
        /// The device interface guid string to find, or <see cref="String.Empty"/> to ignore.
        /// </summary>
        public Guid DeviceInterfaceGuid
        {
            get { return mDeviceInterfaceGuid; }
            set { mDeviceInterfaceGuid = value; }
        }

        /// <summary>
        /// The serial number of the device to find.
        /// </summary>
        /// <remarks>
        /// Set to null to ignore.
        /// </remarks>
        public string SerialNumber
        {
            get { return mSerialNumber; }
            set { mSerialNumber = value; }
        }

        /// <summary>
        /// The revision number of the device to find.
        /// </summary>
        /// <remarks>
        /// Set to <see cref="int.MaxValue"/> to ignore.
        /// </remarks>
        public int Revision
        {
            get { return mRevision; }
            set { mRevision = value; }
        }

        /// <summary>
        /// The product id of the device to find.
        /// </summary>
        /// <remarks>
        /// Set to <see cref="int.MaxValue"/> to ignore.
        /// </remarks>
        public int Pid
        {
            get { return mPid; }
            set { mPid = value; }
        }

        /// <summary>
        /// The vendor id of the device to find.
        /// </summary>
        /// <remarks>
        /// Set to <see cref="int.MaxValue"/> to ignore.
        /// </remarks>
        public int Vid
        {
            get { return mVid; }
            set { mVid = value; }
        }

        #region ISerializable Members

        /// <summary>
        /// Store this class as a binary serializtion object.
        /// </summary>
        /// <param name="info">The serialization instance to populate.</param>
        /// <param name="context"></param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException("info");

            info.AddValue("Vid", mVid);
            info.AddValue("Pid", mPid);
            info.AddValue("Revision", mRevision);
            info.AddValue("SerialNumber", mSerialNumber);
            info.AddValue("DeviceInterfaceGuid", mDeviceInterfaceGuid);
        }

        #endregion

        /// <summary>
        /// Load usb device finder properties from a binary stream.
        /// </summary>
        /// <param name="deviceFinderStream">The binary stream containing a
        /// <see cref="UsbDeviceFinder"/> </param> instance.
        /// <returns>A pre-loaded <see cref="UsbDeviceFinder"/> instance.</returns>
        public static UsbDeviceFinder Load(Stream deviceFinderStream)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            return formatter.Deserialize(deviceFinderStream) as UsbDeviceFinder;
        }

        /// <summary>
        /// Saves a <see cref="UsbDeviceFinder"/> instance to a stream.
        /// </summary>
        /// <param name="usbDeviceFinder"></param>
        /// <param name="outStream"></param>
        public static void Save(UsbDeviceFinder usbDeviceFinder, Stream outStream)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(outStream, usbDeviceFinder);
        }

        /// <summary>
        /// Dynamic predicate find function. Pass this function into any method that has a <see cref="Predicate{UsbRegistry}"/> parameter.
        /// </summary>
        /// <remarks>
        /// Override this member when inheriting the <see cref="UsbDeviceFinder"/> class to change/alter the matching behavior.
        /// </remarks>
        /// <param name="usbRegistry">The UsbRegistry device to check.</param>
        /// <returns>True if the <see cref="UsbRegistry"/> instance matches the <see cref="UsbDeviceFinder"/> properties.</returns>
        public virtual bool Check(UsbRegistry usbRegistry)
        {
            if (mVid != int.MaxValue)
                if (usbRegistry.Vid != mVid) return false;
            if (mPid != int.MaxValue)
                if (usbRegistry.Pid != mPid) return false;
            if (mRevision != int.MaxValue)
                if (usbRegistry.Rev != mRevision) return false;

            if (!String.IsNullOrEmpty(mSerialNumber))
            {
                if (String.IsNullOrEmpty(usbRegistry.SymbolicName)) return false;

                UsbSymbolicName usbSymbolicName = UsbSymbolicName.Parse(usbRegistry.SymbolicName);
                if (mSerialNumber != usbSymbolicName.SerialNumber) return false;
            }
            if (mDeviceInterfaceGuid != Guid.Empty)
            {
                List<Guid> deviceGuids = new List<Guid>(usbRegistry.DeviceInterfaceGuids);
                if (!deviceGuids.Contains(mDeviceInterfaceGuid)) return false;
            }
            return true;
        }
        /// <summary>
        /// Dynamic predicate find function. Pass this function into any method that has a <see cref="Predicate{UsbDevice}"/> parameter.
        /// </summary>
        /// <remarks>
        /// Override this member when inheriting the <see cref="UsbDeviceFinder"/> class to change/alter the matching behavior.
        /// </remarks>
        /// <param name="usbDevice">The UsbDevice to check.</param>
        /// <returns>True if the <see cref="UsbDevice"/> instance matches the <see cref="UsbDeviceFinder"/> properties.</returns>
        public virtual bool Check(UsbDevice usbDevice)
        {
            if (mVid != int.MaxValue)
                if (((ushort)usbDevice.Info.Descriptor.VendorID) != mVid) return false;
            if (mPid != int.MaxValue)
                if (((ushort)usbDevice.Info.Descriptor.ProductID) != mPid) return false;
            if (mRevision != int.MaxValue)
                if (((ushort)usbDevice.Info.Descriptor.BcdDevice) != mRevision) return false;

            if (!String.IsNullOrEmpty(mSerialNumber))
                if (mSerialNumber!=usbDevice.Info.SerialString) return false;

            return true;
        }
    }
}