Mercurial > hg > ucis.core
comparison USBLib/Windows/USB/UsbDevice.cs @ 58:fd63c453ff65
Improved Windows USB enumeration classes
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 09 Oct 2013 20:54:15 +0200 |
parents | 5b14fed54a89 |
children | 3424fa5a12c9 |
comparison
equal
deleted
inserted
replaced
57:b1efeada517e | 58:fd63c453ff65 |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.ComponentModel; | |
3 using System.Runtime.InteropServices; | 4 using System.Runtime.InteropServices; |
4 using System.Text; | 5 using System.Text; |
5 using Microsoft.Win32.SafeHandles; | 6 using Microsoft.Win32.SafeHandles; |
6 using UCIS.HWLib.Windows.Devices; | 7 using UCIS.HWLib.Windows.Devices; |
8 using UCIS.USBLib.Communication; | |
9 using UCIS.USBLib.Descriptor; | |
7 using UCIS.USBLib.Internal.Windows; | 10 using UCIS.USBLib.Internal.Windows; |
8 | 11 |
9 namespace UCIS.HWLib.Windows.USB { | 12 namespace UCIS.HWLib.Windows.USB { |
10 public class UsbDevice { | 13 public class UsbDevice : IUsbInterface { |
14 internal static SafeFileHandle OpenHandle(String path) { | |
15 SafeFileHandle handle = Kernel32.CreateFile(path, Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero); | |
16 if (handle.IsInvalid) throw new Win32Exception(Marshal.GetLastWin32Error()); | |
17 return handle; | |
18 } | |
19 internal static Boolean GetNodeInformation(SafeFileHandle handle, out USB_NODE_INFORMATION nodeInfo) { | |
20 nodeInfo = new USB_NODE_INFORMATION(); | |
21 int nBytes = Marshal.SizeOf(typeof(USB_NODE_INFORMATION)); | |
22 return Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref nodeInfo, nBytes, out nodeInfo, nBytes, out nBytes, IntPtr.Zero); | |
23 } | |
24 internal static Boolean GetNodeConnectionInformation(SafeFileHandle handle, UInt32 port, out USB_NODE_CONNECTION_INFORMATION_EX nodeConnection) { | |
25 int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_INFORMATION_EX)); | |
26 nodeConnection = new USB_NODE_CONNECTION_INFORMATION_EX(); | |
27 nodeConnection.ConnectionIndex = port; | |
28 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, ref nodeConnection, nBytes, out nodeConnection, nBytes, out nBytes, IntPtr.Zero)) | |
29 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
30 return true; | |
31 } | |
32 internal static String GetNodeConnectionName(SafeFileHandle handle, UInt32 port) { | |
33 int nBytes = Marshal.SizeOf(typeof(USB_NODE_CONNECTION_NAME)); | |
34 USB_NODE_CONNECTION_NAME nameConnection = new USB_NODE_CONNECTION_NAME(); | |
35 nameConnection.ConnectionIndex = port; | |
36 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_NAME, ref nameConnection, nBytes, out nameConnection, nBytes, out nBytes, IntPtr.Zero)) | |
37 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
38 return nameConnection.NodeName; | |
39 } | |
40 internal unsafe static int GetDescriptor(SafeFileHandle handle, UInt32 port, byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) { | |
41 int szRequest = Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)); | |
42 USB_DESCRIPTOR_REQUEST request = new USB_DESCRIPTOR_REQUEST(); | |
43 request.ConnectionIndex = port; | |
44 request.SetupPacket.wValue = (ushort)((descriptorType << 8) + index); | |
45 request.SetupPacket.wIndex = (ushort)langId; | |
46 request.SetupPacket.wLength = (ushort)length; | |
47 int nBytes = length + szRequest; | |
48 Byte[] bigbuffer = new Byte[nBytes]; | |
49 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ref request, Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)), bigbuffer, nBytes, out nBytes, IntPtr.Zero)) { | |
50 int err = Marshal.GetLastWin32Error(); | |
51 if (err != 2 && err != 31 && err != 87) throw new Win32Exception(err); | |
52 return 0; | |
53 } | |
54 nBytes -= szRequest; | |
55 if (nBytes > length) nBytes = length; | |
56 if (nBytes < 0) return 0; | |
57 if (nBytes > 0) Buffer.BlockCopy(bigbuffer, szRequest, buffer, offset, nBytes); | |
58 return nBytes; | |
59 } | |
60 internal unsafe static String GetRootHubName(SafeFileHandle handle) { | |
61 USB_ROOT_HUB_NAME rootHubName = new USB_ROOT_HUB_NAME(); | |
62 int nBytesReturned; | |
63 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, out rootHubName, Marshal.SizeOf(rootHubName), out nBytesReturned, IntPtr.Zero)) | |
64 throw new Win32Exception(Marshal.GetLastWin32Error()); | |
65 if (rootHubName.ActualLength <= 0) return null; | |
66 return rootHubName.RootHubName; | |
67 } | |
68 internal unsafe static String GetNodeConnectionDriverKey(SafeFileHandle handle, UInt32 port) { | |
69 USB_NODE_CONNECTION_DRIVERKEY_NAME DriverKeyStruct = new USB_NODE_CONNECTION_DRIVERKEY_NAME(); | |
70 int nBytes = Marshal.SizeOf(DriverKeyStruct); | |
71 DriverKeyStruct.ConnectionIndex = port; | |
72 if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ref DriverKeyStruct, nBytes, out DriverKeyStruct, nBytes, out nBytes, IntPtr.Zero)) | |
73 return null; | |
74 return DriverKeyStruct.DriverKeyName; | |
75 } | |
76 | |
11 public UsbDevice Parent { get; protected set; } | 77 public UsbDevice Parent { get; protected set; } |
78 public string DevicePath { get; private set; } | |
79 public uint AdapterNumber { get; private set; } | |
12 internal USB_NODE_CONNECTION_INFORMATION_EX NodeConnectionInfo { get; set; } | 80 internal USB_NODE_CONNECTION_INFORMATION_EX NodeConnectionInfo { get; set; } |
13 internal USB_DEVICE_DESCRIPTOR DeviceDescriptor { get; private set; } | 81 internal USB_DEVICE_DESCRIPTOR DeviceDescriptor { get { return NodeConnectionInfo.DeviceDescriptor; } } |
14 internal IList<USB_CONFIGURATION_DESCRIPTOR> ConfigurationDescriptor { get; private set; } | 82 |
15 internal IList<USB_INTERFACE_DESCRIPTOR> InterfaceDescriptor { get; private set; } | 83 public bool IsHub { get { return NodeConnectionInfo.DeviceIsHub != 0; } } |
16 internal IList<USB_ENDPOINT_DESCRIPTOR> EndpointDescriptor { get; private set; } | 84 public bool IsConnected { get { return NodeConnectionInfo.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected; } } |
17 internal IList<HID_DESCRIPTOR> HdiDescriptor { get; private set; } | 85 public string Status { get { return NodeConnectionInfo.ConnectionStatus.ToString(); } } |
18 public string DevicePath { get; private set; } | 86 public string Speed { get { return NodeConnectionInfo.Speed.ToString(); } } |
19 public uint AdapterNumber { get; internal set; } | 87 |
20 public string DriverKey { get; private set; } | 88 SafeFileHandle OpenHandle() { |
21 | 89 return OpenHandle(DevicePath); |
22 public virtual string DeviceDescription { get { return DeviceNode.GetPropertyString(CMRDP.DEVICEDESC); } } | 90 } |
23 public string DeviceID { get { return DeviceNode.DeviceID; } } | 91 |
24 | 92 public int NumConfigurations { get { return DeviceDescriptor == null ? 0 : DeviceDescriptor.bNumConfigurations; } } |
25 public bool IsConnected { get; internal set; } | 93 public int VendorID { get { return DeviceDescriptor == null ? 0 : DeviceDescriptor.idVendor; } } |
26 public bool IsHub { get; internal set; } | 94 public int ProductID { get { return DeviceDescriptor == null ? 0 : DeviceDescriptor.idProduct; } } |
27 public string Status { get; internal set; } | 95 |
28 public string Speed { get; internal set; } | 96 private String GetStringSafe(Byte id) { |
29 | 97 if (id == 0) return null; |
30 public string Manufacturer { get; private set; } | 98 String s = GetStringDescriptor(id); |
31 public string SerialNumber { get; private set; } | 99 if (s == null) return s; |
32 public string Product { get; private set; } | 100 return s.Trim(' ', '\0'); |
33 | 101 } |
34 public int VendorID { get { return DeviceDescriptor.idVendor; } } | 102 |
35 public int ProductID { get { return DeviceDescriptor.idProduct; } } | 103 public string Manufacturer { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iManufacturer); } } |
104 public string Product { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iProduct); } } | |
105 public string SerialNumber { get { return DeviceDescriptor == null ? null : GetStringSafe(DeviceDescriptor.iSerialNumber); } } | |
106 public virtual string DriverKey { get { using (SafeFileHandle handle = OpenHandle(DevicePath)) return UsbHub.GetNodeConnectionDriverKey(handle, AdapterNumber); } } | |
107 | |
108 public virtual string DeviceDescription { get { return DeviceNode == null ? null : DeviceNode.GetPropertyString(CMRDP.DEVICEDESC); } } | |
109 public string DeviceID { get { return DeviceNode == null ? null : DeviceNode.DeviceID; } } | |
36 | 110 |
37 private DeviceNode mDeviceNode; | 111 private DeviceNode mDeviceNode; |
38 public DeviceNode DeviceNode { | 112 public DeviceNode DeviceNode { |
39 get { | 113 get { |
40 if (mDeviceNode == null && DriverKey != null) { | 114 String dk = DriverKey; |
115 if (mDeviceNode == null && dk != null) { | |
41 foreach (DeviceNode node in DeviceNode.GetDevices("USB")) { | 116 foreach (DeviceNode node in DeviceNode.GetDevices("USB")) { |
42 if (DriverKey.Equals(node.DriverKey, StringComparison.InvariantCultureIgnoreCase)) { | 117 if (dk.Equals(node.DriverKey, StringComparison.InvariantCultureIgnoreCase)) { |
43 mDeviceNode = node; | 118 mDeviceNode = node; |
44 break; | 119 break; |
45 } | 120 } |
46 } | 121 } |
47 } | 122 } |
48 return mDeviceNode; | 123 return mDeviceNode; |
49 } | 124 } |
50 } | 125 } |
51 | 126 |
52 internal UsbDevice(UsbDevice parent, USB_DEVICE_DESCRIPTOR deviceDescriptor, uint adapterNumber) | 127 internal UsbDevice(UsbDevice parent, USB_NODE_CONNECTION_INFORMATION_EX nci, uint port, string devicePath) { |
53 : this(parent, deviceDescriptor, adapterNumber, null) { } | |
54 unsafe internal UsbDevice(UsbDevice parent, USB_DEVICE_DESCRIPTOR deviceDescriptor, uint adapterNumber, string devicePath) { | |
55 this.Parent = parent; | 128 this.Parent = parent; |
56 this.AdapterNumber = adapterNumber; | 129 this.NodeConnectionInfo = nci; |
57 this.DeviceDescriptor = deviceDescriptor; | |
58 this.DevicePath = devicePath; | 130 this.DevicePath = devicePath; |
59 ConfigurationDescriptor = new List<USB_CONFIGURATION_DESCRIPTOR>(); | 131 this.AdapterNumber = port; |
60 InterfaceDescriptor = new List<USB_INTERFACE_DESCRIPTOR>(); | |
61 HdiDescriptor = new List<HID_DESCRIPTOR>(); | |
62 EndpointDescriptor = new List<USB_ENDPOINT_DESCRIPTOR>(); | |
63 if (devicePath == null) return; | 132 if (devicePath == null) return; |
64 | 133 } |
65 using (SafeFileHandle handel = Kernel32.CreateFile(devicePath, Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero)) { | 134 |
66 if (handel.IsInvalid) throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); | 135 private String GetStringDescriptor(Byte index) { |
67 int nBytesReturned; | 136 return UsbStringDescriptor.GetStringFromDevice(this, index, 0); //0x409 |
68 int nBytes = UsbApi.MAX_BUFFER_SIZE; | 137 } |
69 | 138 |
70 USB_DESCRIPTOR_REQUEST Request1 = new USB_DESCRIPTOR_REQUEST(); | 139 static UsbDevice GetUsbDevice(DeviceNode node, out Boolean isHostController) { |
71 Request1.ConnectionIndex = adapterNumber;// portCount; | 140 String[] hciinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER); |
72 Request1.SetupPacket.wValue = (ushort)((UsbApi.USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)); | 141 if (hciinterface != null && hciinterface.Length > 0) { |
73 Request1.SetupPacket.wLength = (ushort)(nBytes - Marshal.SizeOf(Request1)); | 142 isHostController = true; |
74 Request1.SetupPacket.wIndex = 0; // 0x409; // Language Code | 143 return (new UsbController(null, node, hciinterface[0])).RootHub; |
75 | 144 } |
76 // Use an IOCTL call to request the String Descriptor | 145 isHostController = false; |
77 Byte[] buffer = new Byte[nBytes]; | 146 DeviceNode parent = node.GetParent(); |
78 fixed (Byte* bufferptr = buffer) { | 147 Boolean isHostControllerA; |
79 if (!Kernel32.DeviceIoControl(handel, UsbApi.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ref Request1, Marshal.SizeOf(typeof(USB_DESCRIPTOR_REQUEST)), (IntPtr)bufferptr, nBytes, out nBytesReturned, IntPtr.Zero)) { | 148 UsbDevice usbdev = GetUsbDevice(parent, out isHostControllerA); |
80 int err = Marshal.GetLastWin32Error(); | 149 if (isHostControllerA) return usbdev; |
81 Console.WriteLine("IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION returned {0} {1}", err, (new System.ComponentModel.Win32Exception(err)).Message); | 150 UsbHub usbhub = usbdev as UsbHub; |
82 //SerialNumber = (new System.ComponentModel.Win32Exception(err)).Message; | 151 if (usbhub == null) return null; |
83 if (err != 2 && err != 31 && err != 87) throw new System.ComponentModel.Win32Exception(err); | 152 String driverkey = node.DriverKey; |
84 } else { | 153 foreach (UsbDevice child in usbhub.Devices) { |
85 if (nBytesReturned > nBytes) throw new IndexOutOfRangeException("IOCtl returned too much data"); | 154 if (driverkey.Equals(child.DriverKey, StringComparison.InvariantCultureIgnoreCase)) return child; |
86 if (nBytesReturned < 0) throw new IndexOutOfRangeException("IOCtl returned insufficient data"); | 155 } |
87 Byte* ptr = bufferptr + Marshal.SizeOf(Request1); | 156 return null; |
88 nBytesReturned -= Marshal.SizeOf(Request1); | 157 } |
89 if (nBytesReturned < 0) throw new IndexOutOfRangeException("IOCtl returned insufficient data"); | 158 public static UsbDevice GetUsbDevice(DeviceNode node) { |
90 | 159 Boolean isHostController; |
91 int offset = 0; | 160 return GetUsbDevice(node, out isHostController); |
92 while (offset < nBytesReturned) { | 161 /* |
93 if (offset + Marshal.SizeOf(typeof(USB_DESCRIPTOR)) >= nBytesReturned) throw new IndexOutOfRangeException("Error in configuration descriptor"); | 162 |
94 USB_DESCRIPTOR* desc = (USB_DESCRIPTOR*)(ptr + offset); | 163 String[] hubinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_HUB); |
95 offset += desc->bLength; | 164 if (hubinterface != null && hubinterface.Length > 0) { |
96 if (offset > nBytesReturned) throw new IndexOutOfRangeException("Error in configuration descriptor"); | 165 USB_NODE_CONNECTION_INFORMATION_EX nodeConnection; |
97 Console.WriteLine("Descriptor type {0} length {1}", desc->bDescriptorType, desc->bLength); | 166 using (SafeFileHandle handle = OpenHandle(hubinterface[0])) { |
98 if (desc->bDescriptorType == USB_DESCRIPTOR_TYPE.ConfigurationDescriptorType) { | 167 if (!GetNodeConnectionInformation(handle, 0, out nodeConnection)) return null; |
99 if (desc->bLength < 9) throw new IndexOutOfRangeException("Error in configuration descriptor"); | |
100 USB_CONFIGURATION_DESCRIPTOR configurationDescriptor = *(USB_CONFIGURATION_DESCRIPTOR*)desc; | |
101 ConfigurationDescriptor.Add(configurationDescriptor); | |
102 } else if (desc->bDescriptorType == USB_DESCRIPTOR_TYPE.InterfaceDescriptorType) { | |
103 if (desc->bLength < 9) throw new IndexOutOfRangeException("Error in configuration descriptor"); | |
104 USB_INTERFACE_DESCRIPTOR interfaceDescriptor = *(USB_INTERFACE_DESCRIPTOR*)desc; | |
105 InterfaceDescriptor.Add(interfaceDescriptor); | |
106 } else if (desc->bDescriptorType == USB_DESCRIPTOR_TYPE.EndpointDescriptorType) { | |
107 if (desc->bLength < 7) throw new IndexOutOfRangeException("Error in configuration descriptor"); | |
108 USB_ENDPOINT_DESCRIPTOR endpointDescriptor1 = *(USB_ENDPOINT_DESCRIPTOR*)desc; | |
109 EndpointDescriptor.Add(endpointDescriptor1); | |
110 } | |
111 } | |
112 } | |
113 // The iManufacturer, iProduct and iSerialNumber entries in the | |
114 // device descriptor are really just indexes. So, we have to | |
115 // request a string descriptor to get the values for those strings. | |
116 if (DeviceDescriptor != null) { | |
117 if (DeviceDescriptor.iManufacturer > 0) Manufacturer = GetStringDescriptor(handel, DeviceDescriptor.iManufacturer); | |
118 if (DeviceDescriptor.iProduct > 0) Product = GetStringDescriptor(handel, DeviceDescriptor.iProduct); | |
119 if (DeviceDescriptor.iSerialNumber > 0) SerialNumber = GetStringDescriptor(handel, DeviceDescriptor.iSerialNumber); | |
120 } | |
121 } | 168 } |
122 // Get the Driver Key Name (usefull in locating a device) | 169 return new UsbHub(null, nodeConnection, hubinterface[0], false); |
123 USB_NODE_CONNECTION_DRIVERKEY_NAME DriverKeyStruct = new USB_NODE_CONNECTION_DRIVERKEY_NAME(); | 170 } |
124 DriverKeyStruct.ConnectionIndex = adapterNumber; | |
125 // Use an IOCTL call to request the Driver Key Name | |
126 if (Kernel32.DeviceIoControl(handel, UsbApi.IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, ref DriverKeyStruct, Marshal.SizeOf(DriverKeyStruct), out DriverKeyStruct, Marshal.SizeOf(DriverKeyStruct), out nBytesReturned, IntPtr.Zero)) { | |
127 DriverKey = DriverKeyStruct.DriverKeyName; | |
128 } | |
129 } | |
130 } | |
131 | |
132 private unsafe String GetStringDescriptor(SafeFileHandle handel, Byte id) { | |
133 int nBytes = UsbApi.MAX_BUFFER_SIZE; | |
134 int nBytesReturned = 0; | |
135 Byte[] buffer = new Byte[nBytes]; | |
136 fixed (Byte* bufferptr = buffer) { | |
137 // Build a request for string descriptor. | |
138 USB_DESCRIPTOR_REQUEST Request = new USB_DESCRIPTOR_REQUEST(); | |
139 Request.ConnectionIndex = AdapterNumber; | |
140 Request.SetupPacket.wValue = (ushort)((UsbApi.USB_STRING_DESCRIPTOR_TYPE << 8) + id); | |
141 Request.SetupPacket.wLength = (ushort)(nBytes - Marshal.SizeOf(Request)); | |
142 Request.SetupPacket.wIndex = 0x409; // The language code. | |
143 // Use an IOCTL call to request the string descriptor. | |
144 if (Kernel32.DeviceIoControl(handel, UsbApi.IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, ref Request, Marshal.SizeOf(Request), (IntPtr)bufferptr, nBytes, out nBytesReturned, IntPtr.Zero)) { | |
145 if (nBytesReturned < nBytes) buffer[nBytesReturned] = 0; | |
146 if (nBytesReturned + 1 < nBytes) buffer[nBytesReturned + 1] = 0; | |
147 // The location of the string descriptor is immediately after | |
148 // the Request structure. Because this location is not "covered" | |
149 // by the structure allocation, we're forced to zero out this | |
150 // chunk of memory by using the StringToHGlobalAuto() hack above | |
151 //*(UsbApi.USB_STRING_DESCRIPTOR*)(bufferptr + Marshal.SizeOf(Request)); | |
152 USB_STRING_DESCRIPTOR StringDesc = (USB_STRING_DESCRIPTOR)Marshal.PtrToStructure((IntPtr)(bufferptr + Marshal.SizeOf(Request)), typeof(USB_STRING_DESCRIPTOR)); | |
153 //return StringDesc.bString; | |
154 int len = Math.Min(nBytesReturned - Marshal.SizeOf(Request) - 2, StringDesc.bLength); | |
155 return Encoding.Unicode.GetString(buffer, 2 + Marshal.SizeOf(Request), len); | |
156 } else { | |
157 return null; | |
158 } | |
159 } | |
160 } | |
161 | |
162 /*public static UsbDevice GetUsbDevice(DeviceNode node) { | |
163 String[] hubinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_HUB); | |
164 if (hubinterface != null && hubinterface.Length > 0) return new UsbHub(null, hubinterface[0], false); | |
165 String[] devinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_DEVICE); | 171 String[] devinterface = node.GetInterfaces(UsbApi.GUID_DEVINTERFACE_USB_DEVICE); |
166 if (devinterface == null || devinterface.Length == 0) throw new InvalidOperationException("Device is not an USB device"); | 172 if (devinterface == null || devinterface.Length == 0) return null; |
167 DeviceNode parent = node.GetParent(); | 173 DeviceNode parent = node.GetParent(); |
168 if (parent == null) throw new InvalidOperationException("Could not find parent hub device"); | 174 if (parent == null) return null; |
169 UsbHub usbhub = GetUsbDevice(parent) as UsbHub; | 175 UsbHub usbhub = GetUsbDevice(parent) as UsbHub; |
170 if (usbhub == null) throw new InvalidOperationException("Could not find parent hub device"); | 176 if (usbhub == null) return null; |
171 String driverkey = node.DriverKey; | 177 String driverkey = node.DriverKey; |
172 foreach (UsbDevice usbdev in usbhub.Devices) { | 178 foreach (UsbDevice usbdev in usbhub.Devices) { |
173 if (driverkey.Equals(usbdev.DriverKey, StringComparison.InvariantCultureIgnoreCase)) return usbdev; | 179 if (driverkey.Equals(usbdev.DriverKey, StringComparison.InvariantCultureIgnoreCase)) return usbdev; |
174 } | 180 } |
175 throw new InvalidOperationException("Could not find device on parent hub"); | 181 return null;*/ |
176 //return null; | 182 } |
177 }*/ | 183 |
184 #region IUsbInterface Members | |
185 byte IUsbInterface.Configuration { get { throw new NotImplementedException(); } } | |
186 void IUsbInterface.Close() { } | |
187 public virtual int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) { | |
188 using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath)) return UsbHub.GetDescriptor(handle, AdapterNumber, descriptorType, index, langId, buffer, offset, length); | |
189 } | |
190 string IUsbInterface.GetString(short langId, byte stringIndex) { | |
191 return UsbStringDescriptor.GetStringFromDevice(this, stringIndex, langId); | |
192 } | |
193 int IUsbInterface.BulkWrite(byte endpoint, byte[] buffer, int offset, int length) { throw new NotImplementedException(); } | |
194 int IUsbInterface.BulkRead(byte endpoint, byte[] buffer, int offset, int length) { throw new NotImplementedException(); } | |
195 void IUsbInterface.BulkReset(byte endpoint) { throw new NotImplementedException(); } | |
196 int IUsbInterface.InterruptWrite(byte endpoint, byte[] buffer, int offset, int length) { throw new NotImplementedException(); } | |
197 int IUsbInterface.InterruptRead(byte endpoint, byte[] buffer, int offset, int length) { throw new NotImplementedException(); } | |
198 void IUsbInterface.InterruptReset(byte endpoint) { throw new NotImplementedException(); } | |
199 int IUsbInterface.ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { throw new NotImplementedException(); } | |
200 int IUsbInterface.ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length) { throw new NotImplementedException(); } | |
201 UsbPipeStream IUsbInterface.GetBulkStream(byte endpoint) { throw new NotImplementedException(); } | |
202 UsbPipeStream IUsbInterface.GetInterruptStream(byte endpoint) { throw new NotImplementedException(); } | |
203 void IDisposable.Dispose() { } | |
204 #endregion | |
178 } | 205 } |
206 | |
179 } | 207 } |