forked from nefarius/ScpToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScpDevice.cs
More file actions
746 lines (604 loc) · 25.2 KB
/
ScpDevice.cs
File metadata and controls
746 lines (604 loc) · 25.2 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using log4net;
using ScpControl.Driver;
using ScpControl.Usb;
namespace ScpControl
{
/// <summary>
/// Low-level representation of an Scp-compatible Usb device.
/// </summary>
public partial class ScpDevice
{
protected static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#region Ctors
protected ScpDevice()
{
}
protected ScpDevice(Guid Class)
{
this._class = Class;
}
#endregion
protected bool IsActive { get; set; }
public string Path { get; protected set; }
public short VendorId { get; protected set; }
public short ProductId { get; protected set; }
protected void GetHardwareId(string devicePath)
{
short vid, pid;
GetHardwareId(devicePath, out vid, out pid);
// get values
VendorId = vid;
ProductId = pid;
}
public static void GetHardwareId(string devicePath, out short vendorId, out short productId)
{
// regex to extract vendor ID and product ID from hardware ID string
var regex = new Regex("VID_([0-9A-Z]{4})&PID_([0-9A-Z]{4})", RegexOptions.IgnoreCase);
// matched groups
var matches = regex.Match(devicePath).Groups;
// very basic check
if (matches.Count < 3)
{
vendorId = productId = 0;
return;
}
// get values
vendorId = short.Parse(matches[1].Value, NumberStyles.HexNumber);
productId = short.Parse(matches[2].Value, NumberStyles.HexNumber);
}
public virtual bool Open(int instance = 0)
{
var devicePath = string.Empty;
if (FindDevice(_class, ref devicePath, instance))
{
Open(devicePath);
}
return IsActive;
}
public virtual bool Open(string devicePath)
{
GetHardwareId(devicePath);
Path = devicePath.ToUpper();
if (GetDeviceHandle(Path))
{
if (WinUsbWrapper.Initialize(FileHandle, ref _winUsbHandle))
{
if (InitializeDevice())
{
IsActive = true;
}
else
{
WinUsbWrapper.Free(_winUsbHandle);
_winUsbHandle = (IntPtr)INVALID_HANDLE_VALUE;
}
}
else
{
CloseHandle(FileHandle);
}
}
return IsActive;
}
public virtual bool Start()
{
return IsActive;
}
public virtual bool Stop()
{
IsActive = false;
if (!(_winUsbHandle == (IntPtr)INVALID_HANDLE_VALUE))
{
WinUsbWrapper.AbortPipe(_winUsbHandle, IntIn);
WinUsbWrapper.AbortPipe(_winUsbHandle, BulkIn);
WinUsbWrapper.AbortPipe(_winUsbHandle, BulkOut);
WinUsbWrapper.Free(_winUsbHandle);
_winUsbHandle = (IntPtr)INVALID_HANDLE_VALUE;
}
if (FileHandle != IntPtr.Zero)
{
CloseHandle(FileHandle);
FileHandle = IntPtr.Zero;
}
return true;
}
public virtual bool Close()
{
return Stop();
}
protected static ushort ToValue(UsbHidClassDescriptorType type, byte index = 0x00)
{
return BitConverter.ToUInt16(new[] { (byte)index, (byte)type }, 0);
}
protected static ushort ToValue(UsbHidReportRequestType type)
{
return (ushort) ((byte) type << 8 | (byte) 0x00);
}
protected static ushort ToValue(UsbHidReportRequestType type, UsbHidReportRequestId id)
{
return BitConverter.ToUInt16(new[] { (byte)id, (byte)type }, 0);
}
protected bool IsBitSet(byte value, int offset)
{
return ((value >> offset) & 1) == 0x01;
}
#region WinUSB wrapper methods
protected bool ReadIntPipe(byte[] buffer, int length, ref int transfered)
{
return IsActive && WinUsbWrapper.ReadPipe(_winUsbHandle, IntIn, buffer, length, ref transfered, IntPtr.Zero);
}
protected bool ReadBulkPipe(byte[] buffer, int length, ref int transfered)
{
return IsActive && WinUsbWrapper.ReadPipe(_winUsbHandle, BulkIn, buffer, length, ref transfered, IntPtr.Zero);
}
protected bool WriteIntPipe(byte[] buffer, int length, ref int transfered)
{
return IsActive && WinUsbWrapper.WritePipe(_winUsbHandle, IntOut, buffer, length, ref transfered, IntPtr.Zero);
}
protected bool WriteBulkPipe(byte[] buffer, int length, ref int transfered)
{
return IsActive && WinUsbWrapper.WritePipe(_winUsbHandle, BulkOut, buffer, length, ref transfered, IntPtr.Zero);
}
protected bool SendTransfer(UsbHidRequestType requestType, UsbHidRequest request, ushort value, byte[] buffer,
ref int transfered)
{
return SendTransfer((byte)requestType, (byte)request, value, buffer, ref transfered);
}
protected bool SendTransfer(byte requestType, byte request, ushort value, byte[] buffer, ref int transfered)
{
if (!IsActive) return false;
var setup = new WINUSB_SETUP_PACKET
{
RequestType = requestType,
Request = request,
Value = value,
Index = 0,
Length = (ushort)buffer.Length
};
return WinUsbWrapper.ControlTransfer(_winUsbHandle, setup, buffer, buffer.Length, ref transfered, IntPtr.Zero);
}
#endregion
#region Constant and Structure Definitions
public const int SERVICE_CONTROL_STOP = 0x00000001;
public const int SERVICE_CONTROL_SHUTDOWN = 0x00000005;
public const int SERVICE_CONTROL_DEVICEEVENT = 0x0000000B;
public const int SERVICE_CONTROL_POWEREVENT = 0x0000000D;
public const int DBT_DEVICEARRIVAL = 0x8000;
public const int DBT_DEVICEQUERYREMOVE = 0x8001;
public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int DBT_DEVTYP_DEVICEINTERFACE = 0x0005;
public const int DBT_DEVTYP_HANDLE = 0x0006;
public const int PBT_APMRESUMEAUTOMATIC = 0x0012;
public const int PBT_APMSUSPEND = 0x0004;
public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x0000;
public const int DEVICE_NOTIFY_SERVICE_HANDLE = 0x0001;
public const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 0x0004;
public const int WM_DEVICECHANGE = 0x0219;
public const int DIGCF_PRESENT = 0x0002;
public const int DIGCF_DEVICEINTERFACE = 0x0010;
public delegate int ServiceControlHandlerEx(int Control, int Type, IntPtr Data, IntPtr Context);
[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_DEVICEINTERFACE
{
internal int dbcc_size;
internal int dbcc_devicetype;
internal int dbcc_reserved;
internal Guid dbcc_classguid;
internal short dbcc_name;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class DEV_BROADCAST_DEVICEINTERFACE_M
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 16)]
public byte[]
dbcc_classguid;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
public char[] dbcc_name;
}
[StructLayout(LayoutKind.Sequential)]
public class DEV_BROADCAST_HDR
{
public int dbch_size;
public int dbch_devicetype;
public int dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
protected struct SP_DEVICE_INTERFACE_DATA
{
internal int cbSize;
internal Guid InterfaceClassGuid;
internal int Flags;
internal IntPtr Reserved;
}
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
private const uint FILE_FLAG_OVERLAPPED = 0x40000000;
private const uint FILE_SHARE_READ = 1;
private const uint FILE_SHARE_WRITE = 2;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int INVALID_HANDLE_VALUE = -1;
private const uint OPEN_EXISTING = 3;
protected const uint DEVICE_SPEED = 1;
protected const byte USB_ENDPOINT_DIRECTION_MASK = 0x80;
protected enum POLICY_TYPE
{
SHORT_PACKET_TERMINATE = 1,
AUTO_CLEAR_STALL = 2,
PIPE_TRANSFER_TIMEOUT = 3,
IGNORE_SHORT_PACKETS = 4,
ALLOW_PARTIAL_READS = 5,
AUTO_FLUSH = 6,
RAW_IO = 7
}
protected enum USB_DEVICE_SPEED
{
UsbLowSpeed = 1,
UsbFullSpeed = 2,
UsbHighSpeed = 3
}
[StructLayout(LayoutKind.Sequential)]
protected struct USB_CONFIGURATION_DESCRIPTOR
{
internal byte bLength;
internal byte bDescriptorType;
internal ushort wTotalLength;
internal byte bNumInterfaces;
internal byte bConfigurationValue;
internal byte iConfiguration;
internal byte bmAttributes;
internal byte MaxPower;
}
protected const int DIF_PROPERTYCHANGE = 0x12;
protected const int DICS_ENABLE = 1;
protected const int DICS_DISABLE = 2;
protected const int DICS_PROPCHANGE = 3;
protected const int DICS_FLAG_GLOBAL = 1;
[StructLayout(LayoutKind.Sequential)]
protected struct SP_CLASSINSTALL_HEADER
{
internal int cbSize;
internal int InstallFunction;
}
[StructLayout(LayoutKind.Sequential)]
protected struct SP_PROPCHANGE_PARAMS
{
internal SP_CLASSINSTALL_HEADER ClassInstallHeader;
internal int StateChange;
internal int Scope;
internal int HwProfile;
}
public enum WmDeviceChangeEvent : int
{
/// <summary>
/// A request to change the current configuration (dock or undock) has been canceled.
/// </summary>
DBT_CONFIGCHANGECANCELED = 0x0019,
/// <summary>
/// The current configuration has changed, due to a dock or undock.
/// </summary>
DBT_CONFIGCHANGED = 0x0018,
/// <summary>
/// A custom event has occurred.
/// </summary>
DBT_CUSTOMEVENT = 0x8006,
/// <summary>
/// A device or piece of media has been inserted and is now available.
/// </summary>
DBT_DEVICEARRIVAL = 0x8000,
/// <summary>
/// Permission is requested to remove a device or piece of media. Any application can deny this request and cancel the
/// removal.
/// </summary>
DBT_DEVICEQUERYREMOVE = 0x8001,
/// <summary>
/// A request to remove a device or piece of media has been canceled.
/// </summary>
DBT_DEVICEQUERYREMOVEFAILED = 0x8002,
/// <summary>
/// A device or piece of media has been removed.
/// </summary>
DBT_DEVICEREMOVECOMPLETE = 0x8004,
/// <summary>
/// A device or piece of media is about to be removed. Cannot be denied.
/// </summary>
DBT_DEVICEREMOVEPENDING = 0x8003,
/// <summary>
/// A device-specific event has occurred.
/// </summary>
DBT_DEVICETYPESPECIFIC = 0x8005,
/// <summary>
/// A device has been added to or removed from the system.
/// </summary>
DBT_DEVNODES_CHANGED = 0x0007,
/// <summary>
/// Permission is requested to change the current configuration (dock or undock).
/// </summary>
DBT_QUERYCHANGECONFIG = 0x0017,
/// <summary>
/// The meaning of this message is user-defined.
/// </summary>
DBT_USERDEFINED = 0xFFFF
}
#endregion
#region Protected Data Members
private Guid _class = Guid.Empty;
protected IntPtr FileHandle = IntPtr.Zero;
private IntPtr _winUsbHandle = (IntPtr)INVALID_HANDLE_VALUE;
protected byte IntIn = 0xFF;
protected byte IntOut = 0xFF;
protected byte BulkIn = 0xFF;
protected byte BulkOut = 0xFF;
#endregion
#region Static Helper Methods
public enum Notified
{
Ignore = 0x0000,
Arrival = 0x8000,
QueryRemove = 0x8001,
Removal = 0x8004
};
public static bool RegisterNotify(IntPtr form, Guid Class, ref IntPtr handle, bool window = true)
{
var devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;
try
{
var devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
var size = Marshal.SizeOf(devBroadcastDeviceInterface);
devBroadcastDeviceInterface.dbcc_size = size;
devBroadcastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcastDeviceInterface.dbcc_reserved = 0;
devBroadcastDeviceInterface.dbcc_classguid = Class;
devBroadcastDeviceInterfaceBuffer = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, true);
handle = RegisterDeviceNotification(form, devBroadcastDeviceInterfaceBuffer,
window ? DEVICE_NOTIFY_WINDOW_HANDLE : DEVICE_NOTIFY_SERVICE_HANDLE);
Marshal.PtrToStructure(devBroadcastDeviceInterfaceBuffer, devBroadcastDeviceInterface);
return handle != IntPtr.Zero;
}
catch (Exception ex)
{
Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
finally
{
if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
}
}
}
public static bool UnregisterNotify(IntPtr handle)
{
try
{
return UnregisterDeviceNotification(handle);
}
catch (Exception ex)
{
Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
}
#endregion
#region Protected Methods
protected static bool FindDevice(Guid target, ref string path, int instance = 0)
{
var detailDataBuffer = IntPtr.Zero;
var deviceInfoSet = IntPtr.Zero;
try
{
SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(),
da = new SP_DEVICE_INTERFACE_DATA();
int bufferSize = 0, memberIndex = 0;
deviceInfoSet = SetupDiGetClassDevs(ref target, IntPtr.Zero, IntPtr.Zero,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
deviceInterfaceData.cbSize = da.cbSize = Marshal.SizeOf(deviceInterfaceData);
while (SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref target, memberIndex,
ref deviceInterfaceData))
{
SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0,
ref bufferSize, ref da);
{
detailDataBuffer = Marshal.AllocHGlobal(bufferSize);
Marshal.WriteInt32(detailDataBuffer,
(IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);
if (SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, detailDataBuffer,
bufferSize, ref bufferSize, ref da))
{
var pDevicePathName = detailDataBuffer + 4;
path = (Marshal.PtrToStringAuto(pDevicePathName) ?? "ERROR").ToUpper();
Marshal.FreeHGlobal(detailDataBuffer);
if (memberIndex == instance) return true;
}
else Marshal.FreeHGlobal(detailDataBuffer);
}
memberIndex++;
}
}
catch (Exception ex)
{
Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
finally
{
if (deviceInfoSet != IntPtr.Zero)
{
SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
}
return false;
}
protected virtual bool GetDeviceInstance(ref string instance)
{
var detailDataBuffer = IntPtr.Zero;
var deviceInfoSet = IntPtr.Zero;
try
{
SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(),
da = new SP_DEVICE_INTERFACE_DATA();
int bufferSize = 0, memberIndex = 0;
deviceInfoSet = SetupDiGetClassDevs(ref _class, IntPtr.Zero, IntPtr.Zero,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
deviceInterfaceData.cbSize = da.cbSize = Marshal.SizeOf(deviceInterfaceData);
while (SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref _class, memberIndex,
ref deviceInterfaceData))
{
SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0,
ref bufferSize, ref da);
{
detailDataBuffer = Marshal.AllocHGlobal(bufferSize);
Marshal.WriteInt32(detailDataBuffer,
(IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);
if (SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, detailDataBuffer,
bufferSize, ref bufferSize, ref da))
{
var pDevicePathName = detailDataBuffer + 4;
var current = (Marshal.PtrToStringAuto(pDevicePathName) ?? "ERROR").ToUpper();
Marshal.FreeHGlobal(detailDataBuffer);
if (current == Path)
{
const int nBytes = 256;
var ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
CM_Get_Device_ID(da.Flags, ptrInstanceBuf, nBytes, 0);
instance = (Marshal.PtrToStringAuto(ptrInstanceBuf) ?? "ERROR").ToUpper();
Marshal.FreeHGlobal(ptrInstanceBuf);
return true;
}
}
else Marshal.FreeHGlobal(detailDataBuffer);
}
memberIndex++;
}
}
catch (Exception ex)
{
Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
finally
{
if (deviceInfoSet != IntPtr.Zero)
{
SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
}
return false;
}
protected virtual bool GetDeviceHandle(string path)
{
FileHandle = CreateFile(path, (GENERIC_WRITE | GENERIC_READ), FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0);
if (FileHandle == IntPtr.Zero || FileHandle == (IntPtr)INVALID_HANDLE_VALUE)
{
FileHandle = IntPtr.Zero;
var lastError = GetLastError();
Log.DebugFormat("LastError = {0}", lastError);
}
return !(FileHandle == IntPtr.Zero);
}
protected virtual bool UsbEndpointDirectionIn(int addr)
{
return (addr & 0x80) == 0x80;
}
protected virtual bool UsbEndpointDirectionOut(int addr)
{
return (addr & 0x80) == 0x00;
}
protected virtual bool InitializeDevice()
{
try
{
var ifaceDescriptor = new USB_INTERFACE_DESCRIPTOR();
var pipeInfo = new WINUSB_PIPE_INFORMATION();
if (WinUsbWrapper.QueryInterfaceSettings(_winUsbHandle, 0, ref ifaceDescriptor))
{
for (var i = 0; i < ifaceDescriptor.bNumEndpoints; i++)
{
WinUsbWrapper.QueryPipe(_winUsbHandle, 0, Convert.ToByte(i), ref pipeInfo);
if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) &
UsbEndpointDirectionIn(pipeInfo.PipeId)))
{
BulkIn = pipeInfo.PipeId;
WinUsbWrapper.FlushPipe(_winUsbHandle, BulkIn);
}
else if (((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeBulk) &
UsbEndpointDirectionOut(pipeInfo.PipeId)))
{
BulkOut = pipeInfo.PipeId;
WinUsbWrapper.FlushPipe(_winUsbHandle, BulkOut);
}
else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) &
UsbEndpointDirectionIn(pipeInfo.PipeId))
{
IntIn = pipeInfo.PipeId;
WinUsbWrapper.FlushPipe(_winUsbHandle, IntIn);
}
else if ((pipeInfo.PipeType == USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) &
UsbEndpointDirectionOut(pipeInfo.PipeId))
{
IntOut = pipeInfo.PipeId;
WinUsbWrapper.FlushPipe(_winUsbHandle, IntOut);
}
}
return true;
}
return false;
}
catch (Exception ex)
{
Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
}
protected virtual bool RestartDevice(string instanceId)
{
var deviceInfoSet = IntPtr.Zero;
try
{
var deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);
deviceInfoSet = SetupDiGetClassDevs(ref _class, IntPtr.Zero, IntPtr.Zero,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (SetupDiOpenDeviceInfo(deviceInfoSet, instanceId, IntPtr.Zero, 0, ref deviceInterfaceData))
{
var props = new SP_PROPCHANGE_PARAMS();
props.ClassInstallHeader = new SP_CLASSINSTALL_HEADER();
props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader);
props.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
props.Scope = DICS_FLAG_GLOBAL;
props.StateChange = DICS_PROPCHANGE;
props.HwProfile = 0x00;
if (SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInterfaceData, ref props,
Marshal.SizeOf(props)))
{
return SetupDiChangeState(deviceInfoSet, ref deviceInterfaceData);
}
}
}
catch (Exception ex)
{
Log.ErrorFormat("{0} {1}", ex.HelpLink, ex.Message);
throw;
}
finally
{
if (deviceInfoSet != IntPtr.Zero)
{
SetupDiDestroyDeviceInfoList(deviceInfoSet);
}
}
return false;
}
#endregion
}
}